]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/fileio.c
fileio: read_full_file_full() also warns when file is world readable and secure flag...
[thirdparty/systemd.git] / src / basic / fileio.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <ctype.h>
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <limits.h>
7 #include <stdarg.h>
8 #include <stdint.h>
9 #include <stdio_ext.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/stat.h>
13 #include <sys/types.h>
14 #include <unistd.h>
15
16 #include "alloc-util.h"
17 #include "fd-util.h"
18 #include "fileio.h"
19 #include "fs-util.h"
20 #include "log.h"
21 #include "macro.h"
22 #include "missing.h"
23 #include "parse-util.h"
24 #include "path-util.h"
25 #include "stdio-util.h"
26 #include "string-util.h"
27 #include "tmpfile-util.h"
28
29 #define READ_FULL_BYTES_MAX (4U*1024U*1024U)
30
31 int write_string_stream_ts(
32 FILE *f,
33 const char *line,
34 WriteStringFileFlags flags,
35 struct timespec *ts) {
36
37 bool needs_nl;
38 int r;
39
40 assert(f);
41 assert(line);
42
43 if (ferror(f))
44 return -EIO;
45
46 needs_nl = !(flags & WRITE_STRING_FILE_AVOID_NEWLINE) && !endswith(line, "\n");
47
48 if (needs_nl && (flags & WRITE_STRING_FILE_DISABLE_BUFFER)) {
49 /* If STDIO buffering was disabled, then let's append the newline character to the string itself, so
50 * that the write goes out in one go, instead of two */
51
52 line = strjoina(line, "\n");
53 needs_nl = false;
54 }
55
56 if (fputs(line, f) == EOF)
57 return -errno;
58
59 if (needs_nl)
60 if (fputc('\n', f) == EOF)
61 return -errno;
62
63 if (flags & WRITE_STRING_FILE_SYNC)
64 r = fflush_sync_and_check(f);
65 else
66 r = fflush_and_check(f);
67 if (r < 0)
68 return r;
69
70 if (ts) {
71 struct timespec twice[2] = {*ts, *ts};
72
73 if (futimens(fileno(f), twice) < 0)
74 return -errno;
75 }
76
77 return 0;
78 }
79
80 static int write_string_file_atomic(
81 const char *fn,
82 const char *line,
83 WriteStringFileFlags flags,
84 struct timespec *ts) {
85
86 _cleanup_fclose_ FILE *f = NULL;
87 _cleanup_free_ char *p = NULL;
88 int r;
89
90 assert(fn);
91 assert(line);
92
93 r = fopen_temporary(fn, &f, &p);
94 if (r < 0)
95 return r;
96
97 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
98 (void) fchmod_umask(fileno(f), 0644);
99
100 r = write_string_stream_ts(f, line, flags, ts);
101 if (r < 0)
102 goto fail;
103
104 if (rename(p, fn) < 0) {
105 r = -errno;
106 goto fail;
107 }
108
109 return 0;
110
111 fail:
112 (void) unlink(p);
113 return r;
114 }
115
116 int write_string_file_ts(
117 const char *fn,
118 const char *line,
119 WriteStringFileFlags flags,
120 struct timespec *ts) {
121
122 _cleanup_fclose_ FILE *f = NULL;
123 int q, r;
124
125 assert(fn);
126 assert(line);
127
128 /* We don't know how to verify whether the file contents was already on-disk. */
129 assert(!((flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE) && (flags & WRITE_STRING_FILE_SYNC)));
130
131 if (flags & WRITE_STRING_FILE_ATOMIC) {
132 assert(flags & WRITE_STRING_FILE_CREATE);
133
134 r = write_string_file_atomic(fn, line, flags, ts);
135 if (r < 0)
136 goto fail;
137
138 return r;
139 } else
140 assert(!ts);
141
142 if (flags & WRITE_STRING_FILE_CREATE) {
143 f = fopen(fn, "we");
144 if (!f) {
145 r = -errno;
146 goto fail;
147 }
148 } else {
149 int fd;
150
151 /* We manually build our own version of fopen(..., "we") that
152 * works without O_CREAT */
153 fd = open(fn, O_WRONLY|O_CLOEXEC|O_NOCTTY | ((flags & WRITE_STRING_FILE_NOFOLLOW) ? O_NOFOLLOW : 0));
154 if (fd < 0) {
155 r = -errno;
156 goto fail;
157 }
158
159 f = fdopen(fd, "w");
160 if (!f) {
161 r = -errno;
162 safe_close(fd);
163 goto fail;
164 }
165 }
166
167 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
168
169 if (flags & WRITE_STRING_FILE_DISABLE_BUFFER)
170 setvbuf(f, NULL, _IONBF, 0);
171
172 r = write_string_stream_ts(f, line, flags, ts);
173 if (r < 0)
174 goto fail;
175
176 return 0;
177
178 fail:
179 if (!(flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE))
180 return r;
181
182 f = safe_fclose(f);
183
184 /* OK, the operation failed, but let's see if the right
185 * contents in place already. If so, eat up the error. */
186
187 q = verify_file(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE));
188 if (q <= 0)
189 return r;
190
191 return 0;
192 }
193
194 int write_string_filef(
195 const char *fn,
196 WriteStringFileFlags flags,
197 const char *format, ...) {
198
199 _cleanup_free_ char *p = NULL;
200 va_list ap;
201 int r;
202
203 va_start(ap, format);
204 r = vasprintf(&p, format, ap);
205 va_end(ap);
206
207 if (r < 0)
208 return -ENOMEM;
209
210 return write_string_file(fn, p, flags);
211 }
212
213 int read_one_line_file(const char *fn, char **line) {
214 _cleanup_fclose_ FILE *f = NULL;
215
216 assert(fn);
217 assert(line);
218
219 f = fopen(fn, "re");
220 if (!f)
221 return -errno;
222
223 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
224
225 return read_line(f, LONG_LINE_MAX, line);
226 }
227
228 int verify_file(const char *fn, const char *blob, bool accept_extra_nl) {
229 _cleanup_fclose_ FILE *f = NULL;
230 _cleanup_free_ char *buf = NULL;
231 size_t l, k;
232
233 assert(fn);
234 assert(blob);
235
236 l = strlen(blob);
237
238 if (accept_extra_nl && endswith(blob, "\n"))
239 accept_extra_nl = false;
240
241 buf = malloc(l + accept_extra_nl + 1);
242 if (!buf)
243 return -ENOMEM;
244
245 f = fopen(fn, "re");
246 if (!f)
247 return -errno;
248
249 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
250
251 /* We try to read one byte more than we need, so that we know whether we hit eof */
252 errno = 0;
253 k = fread(buf, 1, l + accept_extra_nl + 1, f);
254 if (ferror(f))
255 return errno > 0 ? -errno : -EIO;
256
257 if (k != l && k != l + accept_extra_nl)
258 return 0;
259 if (memcmp(buf, blob, l) != 0)
260 return 0;
261 if (k > l && buf[l] != '\n')
262 return 0;
263
264 return 1;
265 }
266
267 int read_full_stream_full(
268 FILE *f,
269 const char *filename,
270 ReadFullFileFlags flags,
271 char **ret_contents,
272 size_t *ret_size) {
273
274 _cleanup_free_ char *buf = NULL;
275 struct stat st;
276 size_t n, n_next, l;
277 int fd, r;
278
279 assert(f);
280 assert(ret_contents);
281
282 n_next = LINE_MAX; /* Start size */
283
284 fd = fileno(f);
285 if (fd >= 0) { /* If the FILE* object is backed by an fd (as opposed to memory or such, see fmemopen(), let's
286 * optimize our buffering) */
287
288 if (fstat(fd, &st) < 0)
289 return -errno;
290
291 if (S_ISREG(st.st_mode)) {
292
293 /* Safety check */
294 if (st.st_size > READ_FULL_BYTES_MAX)
295 return -E2BIG;
296
297 /* Start with the right file size, but be prepared for files from /proc which generally report a file
298 * size of 0. Note that we increase the size to read here by one, so that the first read attempt
299 * already makes us notice the EOF. */
300 if (st.st_size > 0)
301 n_next = st.st_size + 1;
302
303 if (flags & READ_FULL_FILE_SECURE)
304 (void) warn_file_is_world_accessible(filename, &st, NULL, 0);
305 }
306 }
307
308 n = l = 0;
309 for (;;) {
310 char *t;
311 size_t k;
312
313 if (flags & READ_FULL_FILE_SECURE) {
314 t = malloc(n_next + 1);
315 if (!t) {
316 r = -ENOMEM;
317 goto finalize;
318 }
319 memcpy_safe(t, buf, n);
320 explicit_bzero_safe(buf, n);
321 } else {
322 t = realloc(buf, n_next + 1);
323 if (!t)
324 return -ENOMEM;
325 }
326
327 buf = t;
328 n = n_next;
329
330 errno = 0;
331 k = fread(buf + l, 1, n - l, f);
332 if (k > 0)
333 l += k;
334
335 if (ferror(f)) {
336 r = errno > 0 ? -errno : -EIO;
337 goto finalize;
338 }
339
340 if (feof(f))
341 break;
342
343 /* We aren't expecting fread() to return a short read outside
344 * of (error && eof), assert buffer is full and enlarge buffer.
345 */
346 assert(l == n);
347
348 /* Safety check */
349 if (n >= READ_FULL_BYTES_MAX) {
350 r = -E2BIG;
351 goto finalize;
352 }
353
354 n_next = MIN(n * 2, READ_FULL_BYTES_MAX);
355 }
356
357 if (!ret_size) {
358 /* Safety check: if the caller doesn't want to know the size of what we just read it will rely on the
359 * trailing NUL byte. But if there's an embedded NUL byte, then we should refuse operation as otherwise
360 * there'd be ambiguity about what we just read. */
361
362 if (memchr(buf, 0, l)) {
363 r = -EBADMSG;
364 goto finalize;
365 }
366 }
367
368 buf[l] = 0;
369 *ret_contents = TAKE_PTR(buf);
370
371 if (ret_size)
372 *ret_size = l;
373
374 return 0;
375
376 finalize:
377 if (flags & READ_FULL_FILE_SECURE)
378 explicit_bzero_safe(buf, n);
379
380 return r;
381 }
382
383 int read_full_file_full(const char *filename, ReadFullFileFlags flags, char **contents, size_t *size) {
384 _cleanup_fclose_ FILE *f = NULL;
385
386 assert(filename);
387 assert(contents);
388
389 f = fopen(filename, "re");
390 if (!f)
391 return -errno;
392
393 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
394
395 return read_full_stream_full(f, filename, flags, contents, size);
396 }
397
398 int executable_is_script(const char *path, char **interpreter) {
399 _cleanup_free_ char *line = NULL;
400 size_t len;
401 char *ans;
402 int r;
403
404 assert(path);
405
406 r = read_one_line_file(path, &line);
407 if (r == -ENOBUFS) /* First line overly long? if so, then it's not a script */
408 return 0;
409 if (r < 0)
410 return r;
411
412 if (!startswith(line, "#!"))
413 return 0;
414
415 ans = strstrip(line + 2);
416 len = strcspn(ans, " \t");
417
418 if (len == 0)
419 return 0;
420
421 ans = strndup(ans, len);
422 if (!ans)
423 return -ENOMEM;
424
425 *interpreter = ans;
426 return 1;
427 }
428
429 /**
430 * Retrieve one field from a file like /proc/self/status. pattern
431 * should not include whitespace or the delimiter (':'). pattern matches only
432 * the beginning of a line. Whitespace before ':' is skipped. Whitespace and
433 * zeros after the ':' will be skipped. field must be freed afterwards.
434 * terminator specifies the terminating characters of the field value (not
435 * included in the value).
436 */
437 int get_proc_field(const char *filename, const char *pattern, const char *terminator, char **field) {
438 _cleanup_free_ char *status = NULL;
439 char *t, *f;
440 size_t len;
441 int r;
442
443 assert(terminator);
444 assert(filename);
445 assert(pattern);
446 assert(field);
447
448 r = read_full_file(filename, &status, NULL);
449 if (r < 0)
450 return r;
451
452 t = status;
453
454 do {
455 bool pattern_ok;
456
457 do {
458 t = strstr(t, pattern);
459 if (!t)
460 return -ENOENT;
461
462 /* Check that pattern occurs in beginning of line. */
463 pattern_ok = (t == status || t[-1] == '\n');
464
465 t += strlen(pattern);
466
467 } while (!pattern_ok);
468
469 t += strspn(t, " \t");
470 if (!*t)
471 return -ENOENT;
472
473 } while (*t != ':');
474
475 t++;
476
477 if (*t) {
478 t += strspn(t, " \t");
479
480 /* Also skip zeros, because when this is used for
481 * capabilities, we don't want the zeros. This way the
482 * same capability set always maps to the same string,
483 * irrespective of the total capability set size. For
484 * other numbers it shouldn't matter. */
485 t += strspn(t, "0");
486 /* Back off one char if there's nothing but whitespace
487 and zeros */
488 if (!*t || isspace(*t))
489 t--;
490 }
491
492 len = strcspn(t, terminator);
493
494 f = strndup(t, len);
495 if (!f)
496 return -ENOMEM;
497
498 *field = f;
499 return 0;
500 }
501
502 DIR *xopendirat(int fd, const char *name, int flags) {
503 int nfd;
504 DIR *d;
505
506 assert(!(flags & O_CREAT));
507
508 nfd = openat(fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags, 0);
509 if (nfd < 0)
510 return NULL;
511
512 d = fdopendir(nfd);
513 if (!d) {
514 safe_close(nfd);
515 return NULL;
516 }
517
518 return d;
519 }
520
521 static int search_and_fopen_internal(const char *path, const char *mode, const char *root, char **search, FILE **_f) {
522 char **i;
523
524 assert(path);
525 assert(mode);
526 assert(_f);
527
528 if (!path_strv_resolve_uniq(search, root))
529 return -ENOMEM;
530
531 STRV_FOREACH(i, search) {
532 _cleanup_free_ char *p = NULL;
533 FILE *f;
534
535 if (root)
536 p = strjoin(root, *i, "/", path);
537 else
538 p = strjoin(*i, "/", path);
539 if (!p)
540 return -ENOMEM;
541
542 f = fopen(p, mode);
543 if (f) {
544 *_f = f;
545 return 0;
546 }
547
548 if (errno != ENOENT)
549 return -errno;
550 }
551
552 return -ENOENT;
553 }
554
555 int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f) {
556 _cleanup_strv_free_ char **copy = NULL;
557
558 assert(path);
559 assert(mode);
560 assert(_f);
561
562 if (path_is_absolute(path)) {
563 FILE *f;
564
565 f = fopen(path, mode);
566 if (f) {
567 *_f = f;
568 return 0;
569 }
570
571 return -errno;
572 }
573
574 copy = strv_copy((char**) search);
575 if (!copy)
576 return -ENOMEM;
577
578 return search_and_fopen_internal(path, mode, root, copy, _f);
579 }
580
581 int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f) {
582 _cleanup_strv_free_ char **s = NULL;
583
584 if (path_is_absolute(path)) {
585 FILE *f;
586
587 f = fopen(path, mode);
588 if (f) {
589 *_f = f;
590 return 0;
591 }
592
593 return -errno;
594 }
595
596 s = strv_split_nulstr(search);
597 if (!s)
598 return -ENOMEM;
599
600 return search_and_fopen_internal(path, mode, root, s, _f);
601 }
602
603 int fflush_and_check(FILE *f) {
604 assert(f);
605
606 errno = 0;
607 fflush(f);
608
609 if (ferror(f))
610 return errno > 0 ? -errno : -EIO;
611
612 return 0;
613 }
614
615 int fflush_sync_and_check(FILE *f) {
616 int r;
617
618 assert(f);
619
620 r = fflush_and_check(f);
621 if (r < 0)
622 return r;
623
624 if (fsync(fileno(f)) < 0)
625 return -errno;
626
627 r = fsync_directory_of_file(fileno(f));
628 if (r < 0)
629 return r;
630
631 return 0;
632 }
633
634 int write_timestamp_file_atomic(const char *fn, usec_t n) {
635 char ln[DECIMAL_STR_MAX(n)+2];
636
637 /* Creates a "timestamp" file, that contains nothing but a
638 * usec_t timestamp, formatted in ASCII. */
639
640 if (n <= 0 || n >= USEC_INFINITY)
641 return -ERANGE;
642
643 xsprintf(ln, USEC_FMT "\n", n);
644
645 return write_string_file(fn, ln, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
646 }
647
648 int read_timestamp_file(const char *fn, usec_t *ret) {
649 _cleanup_free_ char *ln = NULL;
650 uint64_t t;
651 int r;
652
653 r = read_one_line_file(fn, &ln);
654 if (r < 0)
655 return r;
656
657 r = safe_atou64(ln, &t);
658 if (r < 0)
659 return r;
660
661 if (t <= 0 || t >= (uint64_t) USEC_INFINITY)
662 return -ERANGE;
663
664 *ret = (usec_t) t;
665 return 0;
666 }
667
668 int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space) {
669 int r;
670
671 assert(s);
672
673 /* Outputs the specified string with fputs(), but optionally prefixes it with a separator. The *space parameter
674 * when specified shall initially point to a boolean variable initialized to false. It is set to true after the
675 * first invocation. This call is supposed to be use in loops, where a separator shall be inserted between each
676 * element, but not before the first one. */
677
678 if (!f)
679 f = stdout;
680
681 if (space) {
682 if (!separator)
683 separator = " ";
684
685 if (*space) {
686 r = fputs(separator, f);
687 if (r < 0)
688 return r;
689 }
690
691 *space = true;
692 }
693
694 return fputs(s, f);
695 }
696
697 /* A bitmask of the EOL markers we know */
698 typedef enum EndOfLineMarker {
699 EOL_NONE = 0,
700 EOL_ZERO = 1 << 0, /* \0 (aka NUL) */
701 EOL_TEN = 1 << 1, /* \n (aka NL, aka LF) */
702 EOL_THIRTEEN = 1 << 2, /* \r (aka CR) */
703 } EndOfLineMarker;
704
705 static EndOfLineMarker categorize_eol(char c, ReadLineFlags flags) {
706
707 if (!IN_SET(flags, READ_LINE_ONLY_NUL)) {
708 if (c == '\n')
709 return EOL_TEN;
710 if (c == '\r')
711 return EOL_THIRTEEN;
712 }
713
714 if (c == '\0')
715 return EOL_ZERO;
716
717 return EOL_NONE;
718 }
719
720 DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, funlockfile);
721
722 int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret) {
723 size_t n = 0, allocated = 0, count = 0;
724 _cleanup_free_ char *buffer = NULL;
725 int r;
726
727 assert(f);
728
729 /* Something like a bounded version of getline().
730 *
731 * Considers EOF, \n, \r and \0 end of line delimiters (or combinations of these), and does not include these
732 * delimiters in the string returned. Specifically, recognizes the following combinations of markers as line
733 * endings:
734 *
735 * • \n (UNIX)
736 * • \r (old MacOS)
737 * • \0 (C strings)
738 * • \n\0
739 * • \r\0
740 * • \r\n (Windows)
741 * • \n\r
742 * • \r\n\0
743 * • \n\r\0
744 *
745 * Returns the number of bytes read from the files (i.e. including delimiters — this hence usually differs from
746 * the number of characters in the returned string). When EOF is hit, 0 is returned.
747 *
748 * The input parameter limit is the maximum numbers of characters in the returned string, i.e. excluding
749 * delimiters. If the limit is hit we fail and return -ENOBUFS.
750 *
751 * If a line shall be skipped ret may be initialized as NULL. */
752
753 if (ret) {
754 if (!GREEDY_REALLOC(buffer, allocated, 1))
755 return -ENOMEM;
756 }
757
758 {
759 _unused_ _cleanup_(funlockfilep) FILE *flocked = f;
760 EndOfLineMarker previous_eol = EOL_NONE;
761 flockfile(f);
762
763 for (;;) {
764 EndOfLineMarker eol;
765 char c;
766
767 if (n >= limit)
768 return -ENOBUFS;
769
770 if (count >= INT_MAX) /* We couldn't return the counter anymore as "int", hence refuse this */
771 return -ENOBUFS;
772
773 r = safe_fgetc(f, &c);
774 if (r < 0)
775 return r;
776 if (r == 0) /* EOF is definitely EOL */
777 break;
778
779 eol = categorize_eol(c, flags);
780
781 if (FLAGS_SET(previous_eol, EOL_ZERO) ||
782 (eol == EOL_NONE && previous_eol != EOL_NONE) ||
783 (eol != EOL_NONE && (previous_eol & eol) != 0)) {
784 /* Previous char was a NUL? This is not an EOL, but the previous char was? This type of
785 * EOL marker has been seen right before? In either of these three cases we are
786 * done. But first, let's put this character back in the queue. (Note that we have to
787 * cast this to (unsigned char) here as ungetc() expects a positive 'int', and if we
788 * are on an architecture where 'char' equals 'signed char' we need to ensure we don't
789 * pass a negative value here. That said, to complicate things further ungetc() is
790 * actually happy with most negative characters and implicitly casts them back to
791 * positive ones as needed, except for \xff (aka -1, aka EOF), which it refuses. What a
792 * godawful API!) */
793 assert_se(ungetc((unsigned char) c, f) != EOF);
794 break;
795 }
796
797 count++;
798
799 if (eol != EOL_NONE) {
800 previous_eol |= eol;
801 continue;
802 }
803
804 if (ret) {
805 if (!GREEDY_REALLOC(buffer, allocated, n + 2))
806 return -ENOMEM;
807
808 buffer[n] = c;
809 }
810
811 n++;
812 }
813 }
814
815 if (ret) {
816 buffer[n] = 0;
817
818 *ret = TAKE_PTR(buffer);
819 }
820
821 return (int) count;
822 }
823
824 int safe_fgetc(FILE *f, char *ret) {
825 int k;
826
827 assert(f);
828
829 /* A safer version of plain fgetc(): let's propagate the error that happened while reading as such, and
830 * separate the EOF condition from the byte read, to avoid those confusion signed/unsigned issues fgetc()
831 * has. */
832
833 errno = 0;
834 k = fgetc(f);
835 if (k == EOF) {
836 if (ferror(f))
837 return errno > 0 ? -errno : -EIO;
838
839 if (ret)
840 *ret = 0;
841
842 return 0;
843 }
844
845 if (ret)
846 *ret = k;
847
848 return 1;
849 }
850
851 int warn_file_is_world_accessible(const char *filename, struct stat *st, const char *unit, unsigned line) {
852 struct stat _st;
853
854 if (!filename)
855 return 0;
856
857 if (!st) {
858 if (stat(filename, &_st) < 0)
859 return -errno;
860 st = &_st;
861 }
862
863 if ((st->st_mode & S_IRWXO) == 0)
864 return 0;
865
866 if (unit)
867 log_syntax(unit, LOG_WARNING, filename, line, 0,
868 "%s has %04o mode that is too permissive, please adjust the access mode.",
869 filename, st->st_mode & 07777);
870 else
871 log_warning("%s has %04o mode that is too permissive, please adjust the access mode.",
872 filename, st->st_mode & 07777);
873 return 0;
874 }