]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/fs-util.c
fs-util: Strip O_NOFOLLOW in xopenat() when calling fd_reopen()
[thirdparty/systemd.git] / src / basic / fs-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <stddef.h>
5 #include <stdlib.h>
6 #include <sys/file.h>
7 #include <linux/falloc.h>
8 #include <linux/magic.h>
9 #include <unistd.h>
10
11 #include "alloc-util.h"
12 #include "dirent-util.h"
13 #include "fd-util.h"
14 #include "fileio.h"
15 #include "fs-util.h"
16 #include "hostname-util.h"
17 #include "lock-util.h"
18 #include "log.h"
19 #include "macro.h"
20 #include "missing_fcntl.h"
21 #include "missing_fs.h"
22 #include "missing_syscall.h"
23 #include "mkdir.h"
24 #include "parse-util.h"
25 #include "path-util.h"
26 #include "process-util.h"
27 #include "random-util.h"
28 #include "ratelimit.h"
29 #include "stat-util.h"
30 #include "stdio-util.h"
31 #include "string-util.h"
32 #include "strv.h"
33 #include "time-util.h"
34 #include "tmpfile-util.h"
35 #include "umask-util.h"
36 #include "user-util.h"
37
38 int rmdir_parents(const char *path, const char *stop) {
39 char *p;
40 int r;
41
42 assert(path);
43 assert(stop);
44
45 if (!path_is_safe(path))
46 return -EINVAL;
47
48 if (!path_is_safe(stop))
49 return -EINVAL;
50
51 p = strdupa_safe(path);
52
53 for (;;) {
54 char *slash = NULL;
55
56 /* skip the last component. */
57 r = path_find_last_component(p, /* accept_dot_dot= */ false, (const char **) &slash, NULL);
58 if (r <= 0)
59 return r;
60 if (slash == p)
61 return 0;
62
63 assert(*slash == '/');
64 *slash = '\0';
65
66 if (path_startswith_full(stop, p, /* accept_dot_dot= */ false))
67 return 0;
68
69 if (rmdir(p) < 0 && errno != ENOENT)
70 return -errno;
71 }
72 }
73
74 int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath) {
75 int r;
76
77 /* Try the ideal approach first */
78 if (renameat2(olddirfd, oldpath, newdirfd, newpath, RENAME_NOREPLACE) >= 0)
79 return 0;
80
81 /* renameat2() exists since Linux 3.15, btrfs and FAT added support for it later. If it is not implemented,
82 * fall back to a different method. */
83 if (!ERRNO_IS_NOT_SUPPORTED(errno) && errno != EINVAL)
84 return -errno;
85
86 /* Let's try to use linkat()+unlinkat() as fallback. This doesn't work on directories and on some file systems
87 * that do not support hard links (such as FAT, most prominently), but for files it's pretty close to what we
88 * want — though not atomic (i.e. for a short period both the new and the old filename will exist). */
89 if (linkat(olddirfd, oldpath, newdirfd, newpath, 0) >= 0) {
90
91 r = RET_NERRNO(unlinkat(olddirfd, oldpath, 0));
92 if (r < 0) {
93 (void) unlinkat(newdirfd, newpath, 0);
94 return r;
95 }
96
97 return 0;
98 }
99
100 if (!ERRNO_IS_NOT_SUPPORTED(errno) && !IN_SET(errno, EINVAL, EPERM)) /* FAT returns EPERM on link()… */
101 return -errno;
102
103 /* OK, neither RENAME_NOREPLACE nor linkat()+unlinkat() worked. Let's then fall back to the racy TOCTOU
104 * vulnerable accessat(F_OK) check followed by classic, replacing renameat(), we have nothing better. */
105
106 if (faccessat(newdirfd, newpath, F_OK, AT_SYMLINK_NOFOLLOW) >= 0)
107 return -EEXIST;
108 if (errno != ENOENT)
109 return -errno;
110
111 return RET_NERRNO(renameat(olddirfd, oldpath, newdirfd, newpath));
112 }
113
114 int readlinkat_malloc(int fd, const char *p, char **ret) {
115 size_t l = PATH_MAX;
116
117 assert(p);
118
119 for (;;) {
120 _cleanup_free_ char *c = NULL;
121 ssize_t n;
122
123 c = new(char, l+1);
124 if (!c)
125 return -ENOMEM;
126
127 n = readlinkat(fd, p, c, l);
128 if (n < 0)
129 return -errno;
130
131 if ((size_t) n < l) {
132 c[n] = 0;
133
134 if (ret)
135 *ret = TAKE_PTR(c);
136
137 return 0;
138 }
139
140 if (l > (SSIZE_MAX-1)/2) /* readlinkat() returns an ssize_t, and we want an extra byte for a
141 * trailing NUL, hence do an overflow check relative to SSIZE_MAX-1
142 * here */
143 return -EFBIG;
144
145 l *= 2;
146 }
147 }
148
149 int readlink_malloc(const char *p, char **ret) {
150 return readlinkat_malloc(AT_FDCWD, p, ret);
151 }
152
153 int readlink_value(const char *p, char **ret) {
154 _cleanup_free_ char *link = NULL, *name = NULL;
155 int r;
156
157 assert(p);
158 assert(ret);
159
160 r = readlink_malloc(p, &link);
161 if (r < 0)
162 return r;
163
164 r = path_extract_filename(link, &name);
165 if (r < 0)
166 return r;
167 if (r == O_DIRECTORY)
168 return -EINVAL;
169
170 *ret = TAKE_PTR(name);
171 return 0;
172 }
173
174 int readlink_and_make_absolute(const char *p, char **ret) {
175 _cleanup_free_ char *target = NULL;
176 int r;
177
178 assert(p);
179 assert(ret);
180
181 r = readlink_malloc(p, &target);
182 if (r < 0)
183 return r;
184
185 return file_in_same_dir(p, target, ret);
186 }
187
188 int chmod_and_chown_at(int dir_fd, const char *path, mode_t mode, uid_t uid, gid_t gid) {
189 _cleanup_close_ int fd = -EBADF;
190
191 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
192
193 if (path) {
194 /* Let's acquire an O_PATH fd, as precaution to change mode/owner on the same file */
195 fd = openat(dir_fd, path, O_PATH|O_CLOEXEC|O_NOFOLLOW);
196 if (fd < 0)
197 return -errno;
198 dir_fd = fd;
199
200 } else if (dir_fd == AT_FDCWD) {
201 /* Let's acquire an O_PATH fd of the current directory */
202 fd = openat(dir_fd, ".", O_PATH|O_CLOEXEC|O_NOFOLLOW|O_DIRECTORY);
203 if (fd < 0)
204 return -errno;
205 dir_fd = fd;
206 }
207
208 return fchmod_and_chown(dir_fd, mode, uid, gid);
209 }
210
211 int fchmod_and_chown_with_fallback(int fd, const char *path, mode_t mode, uid_t uid, gid_t gid) {
212 bool do_chown, do_chmod;
213 struct stat st;
214 int r;
215
216 /* Change ownership and access mode of the specified fd. Tries to do so safely, ensuring that at no
217 * point in time the access mode is above the old access mode under the old ownership or the new
218 * access mode under the new ownership. Note: this call tries hard to leave the access mode
219 * unaffected if the uid/gid is changed, i.e. it undoes implicit suid/sgid dropping the kernel does
220 * on chown().
221 *
222 * This call is happy with O_PATH fds.
223 *
224 * If path is given, allow a fallback path which does not use /proc/self/fd/. On any normal system
225 * /proc will be mounted, but in certain improperly assembled environments it might not be. This is
226 * less secure (potential TOCTOU), so should only be used after consideration. */
227
228 if (fstat(fd, &st) < 0)
229 return -errno;
230
231 do_chown =
232 (uid != UID_INVALID && st.st_uid != uid) ||
233 (gid != GID_INVALID && st.st_gid != gid);
234
235 do_chmod =
236 !S_ISLNK(st.st_mode) && /* chmod is not defined on symlinks */
237 ((mode != MODE_INVALID && ((st.st_mode ^ mode) & 07777) != 0) ||
238 do_chown); /* If we change ownership, make sure we reset the mode afterwards, since chown()
239 * modifies the access mode too */
240
241 if (mode == MODE_INVALID)
242 mode = st.st_mode; /* If we only shall do a chown(), save original mode, since chown() might break it. */
243 else if ((mode & S_IFMT) != 0 && ((mode ^ st.st_mode) & S_IFMT) != 0)
244 return -EINVAL; /* insist on the right file type if it was specified */
245
246 if (do_chown && do_chmod) {
247 mode_t minimal = st.st_mode & mode; /* the subset of the old and the new mask */
248
249 if (((minimal ^ st.st_mode) & 07777) != 0) {
250 r = fchmod_opath(fd, minimal & 07777);
251 if (r < 0) {
252 if (!path || r != -ENOSYS)
253 return r;
254
255 /* Fallback path which doesn't use /proc/self/fd/. */
256 if (chmod(path, minimal & 07777) < 0)
257 return -errno;
258 }
259 }
260 }
261
262 if (do_chown)
263 if (fchownat(fd, "", uid, gid, AT_EMPTY_PATH) < 0)
264 return -errno;
265
266 if (do_chmod) {
267 r = fchmod_opath(fd, mode & 07777);
268 if (r < 0) {
269 if (!path || r != -ENOSYS)
270 return r;
271
272 /* Fallback path which doesn't use /proc/self/fd/. */
273 if (chmod(path, mode & 07777) < 0)
274 return -errno;
275 }
276 }
277
278 return do_chown || do_chmod;
279 }
280
281 int fchmod_umask(int fd, mode_t m) {
282 _cleanup_umask_ mode_t u = umask(0777);
283
284 return RET_NERRNO(fchmod(fd, m & (~u)));
285 }
286
287 int fchmod_opath(int fd, mode_t m) {
288 /* This function operates also on fd that might have been opened with
289 * O_PATH. Indeed fchmodat() doesn't have the AT_EMPTY_PATH flag like
290 * fchownat() does. */
291
292 if (chmod(FORMAT_PROC_FD_PATH(fd), m) < 0) {
293 if (errno != ENOENT)
294 return -errno;
295
296 if (proc_mounted() == 0)
297 return -ENOSYS; /* if we have no /proc/, the concept is not implementable */
298
299 return -ENOENT;
300 }
301
302 return 0;
303 }
304
305 int futimens_opath(int fd, const struct timespec ts[2]) {
306 /* Similar to fchmod_path() but for futimens() */
307
308 if (utimensat(AT_FDCWD, FORMAT_PROC_FD_PATH(fd), ts, 0) < 0) {
309 if (errno != ENOENT)
310 return -errno;
311
312 if (proc_mounted() == 0)
313 return -ENOSYS; /* if we have no /proc/, the concept is not implementable */
314
315 return -ENOENT;
316 }
317
318 return 0;
319 }
320
321 int stat_warn_permissions(const char *path, const struct stat *st) {
322 assert(path);
323 assert(st);
324
325 /* Don't complain if we are reading something that is not a file, for example /dev/null */
326 if (!S_ISREG(st->st_mode))
327 return 0;
328
329 if (st->st_mode & 0111)
330 log_warning("Configuration file %s is marked executable. Please remove executable permission bits. Proceeding anyway.", path);
331
332 if (st->st_mode & 0002)
333 log_warning("Configuration file %s is marked world-writable. Please remove world writability permission bits. Proceeding anyway.", path);
334
335 if (getpid_cached() == 1 && (st->st_mode & 0044) != 0044)
336 log_warning("Configuration file %s is marked world-inaccessible. This has no effect as configuration data is accessible via APIs without restrictions. Proceeding anyway.", path);
337
338 return 0;
339 }
340
341 int fd_warn_permissions(const char *path, int fd) {
342 struct stat st;
343
344 assert(path);
345 assert(fd >= 0);
346
347 if (fstat(fd, &st) < 0)
348 return -errno;
349
350 return stat_warn_permissions(path, &st);
351 }
352
353 int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode) {
354 _cleanup_close_ int fd = -EBADF;
355 int r, ret;
356
357 assert(path);
358
359 /* Note that touch_file() does not follow symlinks: if invoked on an existing symlink, then it is the symlink
360 * itself which is updated, not its target
361 *
362 * Returns the first error we encounter, but tries to apply as much as possible. */
363
364 if (parents)
365 (void) mkdir_parents(path, 0755);
366
367 /* Initially, we try to open the node with O_PATH, so that we get a reference to the node. This is useful in
368 * case the path refers to an existing device or socket node, as we can open it successfully in all cases, and
369 * won't trigger any driver magic or so. */
370 fd = open(path, O_PATH|O_CLOEXEC|O_NOFOLLOW);
371 if (fd < 0) {
372 if (errno != ENOENT)
373 return -errno;
374
375 /* if the node doesn't exist yet, we create it, but with O_EXCL, so that we only create a regular file
376 * here, and nothing else */
377 fd = open(path, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, IN_SET(mode, 0, MODE_INVALID) ? 0644 : mode);
378 if (fd < 0)
379 return -errno;
380 }
381
382 /* Let's make a path from the fd, and operate on that. With this logic, we can adjust the access mode,
383 * ownership and time of the file node in all cases, even if the fd refers to an O_PATH object — which is
384 * something fchown(), fchmod(), futimensat() don't allow. */
385 ret = fchmod_and_chown(fd, mode, uid, gid);
386
387 if (stamp != USEC_INFINITY) {
388 struct timespec ts[2];
389
390 timespec_store(&ts[0], stamp);
391 ts[1] = ts[0];
392 r = futimens_opath(fd, ts);
393 } else
394 r = futimens_opath(fd, NULL);
395 if (r < 0 && ret >= 0)
396 return r;
397
398 return ret;
399 }
400
401 int symlink_idempotent(const char *from, const char *to, bool make_relative) {
402 _cleanup_free_ char *relpath = NULL;
403 int r;
404
405 assert(from);
406 assert(to);
407
408 if (make_relative) {
409 r = path_make_relative_parent(to, from, &relpath);
410 if (r < 0)
411 return r;
412
413 from = relpath;
414 }
415
416 if (symlink(from, to) < 0) {
417 _cleanup_free_ char *p = NULL;
418
419 if (errno != EEXIST)
420 return -errno;
421
422 r = readlink_malloc(to, &p);
423 if (r == -EINVAL) /* Not a symlink? In that case return the original error we encountered: -EEXIST */
424 return -EEXIST;
425 if (r < 0) /* Any other error? In that case propagate it as is */
426 return r;
427
428 if (!streq(p, from)) /* Not the symlink we want it to be? In that case, propagate the original -EEXIST */
429 return -EEXIST;
430 }
431
432 return 0;
433 }
434
435 int symlinkat_atomic_full(const char *from, int atfd, const char *to, bool make_relative) {
436 _cleanup_free_ char *relpath = NULL, *t = NULL;
437 int r;
438
439 assert(from);
440 assert(to);
441
442 if (make_relative) {
443 r = path_make_relative_parent(to, from, &relpath);
444 if (r < 0)
445 return r;
446
447 from = relpath;
448 }
449
450 r = tempfn_random(to, NULL, &t);
451 if (r < 0)
452 return r;
453
454 if (symlinkat(from, atfd, t) < 0)
455 return -errno;
456
457 r = RET_NERRNO(renameat(atfd, t, atfd, to));
458 if (r < 0) {
459 (void) unlinkat(atfd, t, 0);
460 return r;
461 }
462
463 return 0;
464 }
465
466 int mknodat_atomic(int atfd, const char *path, mode_t mode, dev_t dev) {
467 _cleanup_free_ char *t = NULL;
468 int r;
469
470 assert(path);
471
472 r = tempfn_random(path, NULL, &t);
473 if (r < 0)
474 return r;
475
476 if (mknodat(atfd, t, mode, dev) < 0)
477 return -errno;
478
479 r = RET_NERRNO(renameat(atfd, t, atfd, path));
480 if (r < 0) {
481 (void) unlinkat(atfd, t, 0);
482 return r;
483 }
484
485 return 0;
486 }
487
488 int mkfifoat_atomic(int atfd, const char *path, mode_t mode) {
489 _cleanup_free_ char *t = NULL;
490 int r;
491
492 assert(path);
493
494 /* We're only interested in the (random) filename. */
495 r = tempfn_random(path, NULL, &t);
496 if (r < 0)
497 return r;
498
499 if (mkfifoat(atfd, t, mode) < 0)
500 return -errno;
501
502 r = RET_NERRNO(renameat(atfd, t, atfd, path));
503 if (r < 0) {
504 (void) unlinkat(atfd, t, 0);
505 return r;
506 }
507
508 return 0;
509 }
510
511 int get_files_in_directory(const char *path, char ***list) {
512 _cleanup_strv_free_ char **l = NULL;
513 _cleanup_closedir_ DIR *d = NULL;
514 size_t n = 0;
515
516 assert(path);
517
518 /* Returns all files in a directory in *list, and the number
519 * of files as return value. If list is NULL returns only the
520 * number. */
521
522 d = opendir(path);
523 if (!d)
524 return -errno;
525
526 FOREACH_DIRENT_ALL(de, d, return -errno) {
527 if (!dirent_is_file(de))
528 continue;
529
530 if (list) {
531 /* one extra slot is needed for the terminating NULL */
532 if (!GREEDY_REALLOC(l, n + 2))
533 return -ENOMEM;
534
535 l[n] = strdup(de->d_name);
536 if (!l[n])
537 return -ENOMEM;
538
539 l[++n] = NULL;
540 } else
541 n++;
542 }
543
544 if (list)
545 *list = TAKE_PTR(l);
546
547 return n;
548 }
549
550 static int getenv_tmp_dir(const char **ret_path) {
551 int r, ret = 0;
552
553 assert(ret_path);
554
555 /* We use the same order of environment variables python uses in tempfile.gettempdir():
556 * https://docs.python.org/3/library/tempfile.html#tempfile.gettempdir */
557 FOREACH_STRING(n, "TMPDIR", "TEMP", "TMP") {
558 const char *e;
559
560 e = secure_getenv(n);
561 if (!e)
562 continue;
563 if (!path_is_absolute(e)) {
564 r = -ENOTDIR;
565 goto next;
566 }
567 if (!path_is_normalized(e)) {
568 r = -EPERM;
569 goto next;
570 }
571
572 r = is_dir(e, true);
573 if (r < 0)
574 goto next;
575 if (r == 0) {
576 r = -ENOTDIR;
577 goto next;
578 }
579
580 *ret_path = e;
581 return 1;
582
583 next:
584 /* Remember first error, to make this more debuggable */
585 if (ret >= 0)
586 ret = r;
587 }
588
589 if (ret < 0)
590 return ret;
591
592 *ret_path = NULL;
593 return ret;
594 }
595
596 static int tmp_dir_internal(const char *def, const char **ret) {
597 const char *e;
598 int r, k;
599
600 assert(def);
601 assert(ret);
602
603 r = getenv_tmp_dir(&e);
604 if (r > 0) {
605 *ret = e;
606 return 0;
607 }
608
609 k = is_dir(def, true);
610 if (k == 0)
611 k = -ENOTDIR;
612 if (k < 0)
613 return r < 0 ? r : k;
614
615 *ret = def;
616 return 0;
617 }
618
619 int var_tmp_dir(const char **ret) {
620
621 /* Returns the location for "larger" temporary files, that is backed by physical storage if available, and thus
622 * even might survive a boot: /var/tmp. If $TMPDIR (or related environment variables) are set, its value is
623 * returned preferably however. Note that both this function and tmp_dir() below are affected by $TMPDIR,
624 * making it a variable that overrides all temporary file storage locations. */
625
626 return tmp_dir_internal("/var/tmp", ret);
627 }
628
629 int tmp_dir(const char **ret) {
630
631 /* Similar to var_tmp_dir() above, but returns the location for "smaller" temporary files, which is usually
632 * backed by an in-memory file system: /tmp. */
633
634 return tmp_dir_internal("/tmp", ret);
635 }
636
637 int unlink_or_warn(const char *filename) {
638 if (unlink(filename) < 0 && errno != ENOENT)
639 /* If the file doesn't exist and the fs simply was read-only (in which
640 * case unlink() returns EROFS even if the file doesn't exist), don't
641 * complain */
642 if (errno != EROFS || access(filename, F_OK) >= 0)
643 return log_error_errno(errno, "Failed to remove \"%s\": %m", filename);
644
645 return 0;
646 }
647
648 int access_fd(int fd, int mode) {
649 /* Like access() but operates on an already open fd */
650
651 if (access(FORMAT_PROC_FD_PATH(fd), mode) < 0) {
652 if (errno != ENOENT)
653 return -errno;
654
655 /* ENOENT can mean two things: that the fd does not exist or that /proc is not mounted. Let's
656 * make things debuggable and distinguish the two. */
657
658 if (proc_mounted() == 0)
659 return -ENOSYS; /* /proc is not available or not set up properly, we're most likely in some chroot
660 * environment. */
661
662 return -EBADF; /* The directory exists, hence it's the fd that doesn't. */
663 }
664
665 return 0;
666 }
667
668 void unlink_tempfilep(char (*p)[]) {
669 /* If the file is created with mkstemp(), it will (almost always)
670 * change the suffix. Treat this as a sign that the file was
671 * successfully created. We ignore both the rare case where the
672 * original suffix is used and unlink failures. */
673 if (!endswith(*p, ".XXXXXX"))
674 (void) unlink(*p);
675 }
676
677 int unlinkat_deallocate(int fd, const char *name, UnlinkDeallocateFlags flags) {
678 _cleanup_close_ int truncate_fd = -EBADF;
679 struct stat st;
680 off_t l, bs;
681
682 assert((flags & ~(UNLINK_REMOVEDIR|UNLINK_ERASE)) == 0);
683
684 /* Operates like unlinkat() but also deallocates the file contents if it is a regular file and there's no other
685 * link to it. This is useful to ensure that other processes that might have the file open for reading won't be
686 * able to keep the data pinned on disk forever. This call is particular useful whenever we execute clean-up
687 * jobs ("vacuuming"), where we want to make sure the data is really gone and the disk space released and
688 * returned to the free pool.
689 *
690 * Deallocation is preferably done by FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE (👊) if supported, which means
691 * the file won't change size. That's a good thing since we shouldn't needlessly trigger SIGBUS in other
692 * programs that have mmap()ed the file. (The assumption here is that changing file contents to all zeroes
693 * underneath those programs is the better choice than simply triggering SIGBUS in them which truncation does.)
694 * However if hole punching is not implemented in the kernel or file system we'll fall back to normal file
695 * truncation (🔪), as our goal of deallocating the data space trumps our goal of being nice to readers (💐).
696 *
697 * Note that we attempt deallocation, but failure to succeed with that is not considered fatal, as long as the
698 * primary job – to delete the file – is accomplished. */
699
700 if (!FLAGS_SET(flags, UNLINK_REMOVEDIR)) {
701 truncate_fd = openat(fd, name, O_WRONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW|O_NONBLOCK);
702 if (truncate_fd < 0) {
703
704 /* If this failed because the file doesn't exist propagate the error right-away. Also,
705 * AT_REMOVEDIR wasn't set, and we tried to open the file for writing, which means EISDIR is
706 * returned when this is a directory but we are not supposed to delete those, hence propagate
707 * the error right-away too. */
708 if (IN_SET(errno, ENOENT, EISDIR))
709 return -errno;
710
711 if (errno != ELOOP) /* don't complain if this is a symlink */
712 log_debug_errno(errno, "Failed to open file '%s' for deallocation, ignoring: %m", name);
713 }
714 }
715
716 if (unlinkat(fd, name, FLAGS_SET(flags, UNLINK_REMOVEDIR) ? AT_REMOVEDIR : 0) < 0)
717 return -errno;
718
719 if (truncate_fd < 0) /* Don't have a file handle, can't do more ☹️ */
720 return 0;
721
722 if (fstat(truncate_fd, &st) < 0) {
723 log_debug_errno(errno, "Failed to stat file '%s' for deallocation, ignoring: %m", name);
724 return 0;
725 }
726
727 if (!S_ISREG(st.st_mode))
728 return 0;
729
730 if (FLAGS_SET(flags, UNLINK_ERASE) && st.st_size > 0 && st.st_nlink == 0) {
731 uint64_t left = st.st_size;
732 char buffer[64 * 1024];
733
734 /* If erasing is requested, let's overwrite the file with random data once before deleting
735 * it. This isn't going to give you shred(1) semantics, but hopefully should be good enough
736 * for stuff backed by tmpfs at least.
737 *
738 * Note that we only erase like this if the link count of the file is zero. If it is higher it
739 * is still linked by someone else and we'll leave it to them to remove it securely
740 * eventually! */
741
742 random_bytes(buffer, sizeof(buffer));
743
744 while (left > 0) {
745 ssize_t n;
746
747 n = write(truncate_fd, buffer, MIN(sizeof(buffer), left));
748 if (n < 0) {
749 log_debug_errno(errno, "Failed to erase data in file '%s', ignoring.", name);
750 break;
751 }
752
753 assert(left >= (size_t) n);
754 left -= n;
755 }
756
757 /* Let's refresh metadata */
758 if (fstat(truncate_fd, &st) < 0) {
759 log_debug_errno(errno, "Failed to stat file '%s' for deallocation, ignoring: %m", name);
760 return 0;
761 }
762 }
763
764 /* Don't dallocate if there's nothing to deallocate or if the file is linked elsewhere */
765 if (st.st_blocks == 0 || st.st_nlink > 0)
766 return 0;
767
768 /* If this is a regular file, it actually took up space on disk and there are no other links it's time to
769 * punch-hole/truncate this to release the disk space. */
770
771 bs = MAX(st.st_blksize, 512);
772 l = DIV_ROUND_UP(st.st_size, bs) * bs; /* Round up to next block size */
773
774 if (fallocate(truncate_fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, 0, l) >= 0)
775 return 0; /* Successfully punched a hole! 😊 */
776
777 /* Fall back to truncation */
778 if (ftruncate(truncate_fd, 0) < 0) {
779 log_debug_errno(errno, "Failed to truncate file to 0, ignoring: %m");
780 return 0;
781 }
782
783 return 0;
784 }
785
786 int open_parent_at(int dir_fd, const char *path, int flags, mode_t mode) {
787 _cleanup_free_ char *parent = NULL;
788 int r;
789
790 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
791 assert(path);
792
793 r = path_extract_directory(path, &parent);
794 if (r == -EDESTADDRREQ) {
795 parent = strdup(".");
796 if (!parent)
797 return -ENOMEM;
798 } else if (r == -EADDRNOTAVAIL) {
799 parent = strdup(path);
800 if (!parent)
801 return -ENOMEM;
802 } else if (r < 0)
803 return r;
804
805 /* Let's insist on O_DIRECTORY since the parent of a file or directory is a directory. Except if we open an
806 * O_TMPFILE file, because in that case we are actually create a regular file below the parent directory. */
807
808 if (FLAGS_SET(flags, O_PATH))
809 flags |= O_DIRECTORY;
810 else if (!FLAGS_SET(flags, O_TMPFILE))
811 flags |= O_DIRECTORY|O_RDONLY;
812
813 return RET_NERRNO(openat(dir_fd, parent, flags, mode));
814 }
815
816 int conservative_renameat(
817 int olddirfd, const char *oldpath,
818 int newdirfd, const char *newpath) {
819
820 _cleanup_close_ int old_fd = -EBADF, new_fd = -EBADF;
821 struct stat old_stat, new_stat;
822
823 /* Renames the old path to thew new path, much like renameat() — except if both are regular files and
824 * have the exact same contents and basic file attributes already. In that case remove the new file
825 * instead. This call is useful for reducing inotify wakeups on files that are updated but don't
826 * actually change. This function is written in a style that we rather rename too often than suppress
827 * too much. i.e. whenever we are in doubt we rather rename than fail. After all reducing inotify
828 * events is an optimization only, not more. */
829
830 old_fd = openat(olddirfd, oldpath, O_CLOEXEC|O_RDONLY|O_NOCTTY|O_NOFOLLOW);
831 if (old_fd < 0)
832 goto do_rename;
833
834 new_fd = openat(newdirfd, newpath, O_CLOEXEC|O_RDONLY|O_NOCTTY|O_NOFOLLOW);
835 if (new_fd < 0)
836 goto do_rename;
837
838 if (fstat(old_fd, &old_stat) < 0)
839 goto do_rename;
840
841 if (!S_ISREG(old_stat.st_mode))
842 goto do_rename;
843
844 if (fstat(new_fd, &new_stat) < 0)
845 goto do_rename;
846
847 if (stat_inode_same(&new_stat, &old_stat))
848 goto is_same;
849
850 if (old_stat.st_mode != new_stat.st_mode ||
851 old_stat.st_size != new_stat.st_size ||
852 old_stat.st_uid != new_stat.st_uid ||
853 old_stat.st_gid != new_stat.st_gid)
854 goto do_rename;
855
856 for (;;) {
857 uint8_t buf1[16*1024];
858 uint8_t buf2[sizeof(buf1)];
859 ssize_t l1, l2;
860
861 l1 = read(old_fd, buf1, sizeof(buf1));
862 if (l1 < 0)
863 goto do_rename;
864
865 if (l1 == sizeof(buf1))
866 /* Read the full block, hence read a full block in the other file too */
867
868 l2 = read(new_fd, buf2, l1);
869 else {
870 assert((size_t) l1 < sizeof(buf1));
871
872 /* Short read. This hence was the last block in the first file, and then came
873 * EOF. Read one byte more in the second file, so that we can verify we hit EOF there
874 * too. */
875
876 assert((size_t) (l1 + 1) <= sizeof(buf2));
877 l2 = read(new_fd, buf2, l1 + 1);
878 }
879 if (l2 != l1)
880 goto do_rename;
881
882 if (memcmp(buf1, buf2, l1) != 0)
883 goto do_rename;
884
885 if ((size_t) l1 < sizeof(buf1)) /* We hit EOF on the first file, and the second file too, hence exit
886 * now. */
887 break;
888 }
889
890 is_same:
891 /* Everything matches? Then don't rename, instead remove the source file, and leave the existing
892 * destination in place */
893
894 if (unlinkat(olddirfd, oldpath, 0) < 0)
895 goto do_rename;
896
897 return 0;
898
899 do_rename:
900 if (renameat(olddirfd, oldpath, newdirfd, newpath) < 0)
901 return -errno;
902
903 return 1;
904 }
905
906 int posix_fallocate_loop(int fd, uint64_t offset, uint64_t size) {
907 RateLimit rl;
908 int r;
909
910 r = posix_fallocate(fd, offset, size); /* returns positive errnos on error */
911 if (r != EINTR)
912 return -r; /* Let's return negative errnos, like common in our codebase */
913
914 /* On EINTR try a couple of times more, but protect against busy looping
915 * (not more than 16 times per 10s) */
916 rl = (const RateLimit) { 10 * USEC_PER_SEC, 16 };
917 while (ratelimit_below(&rl)) {
918 r = posix_fallocate(fd, offset, size);
919 if (r != EINTR)
920 return -r;
921 }
922
923 return -EINTR;
924 }
925
926 int parse_cifs_service(
927 const char *s,
928 char **ret_host,
929 char **ret_service,
930 char **ret_path) {
931
932 _cleanup_free_ char *h = NULL, *ss = NULL, *x = NULL;
933 const char *p, *e, *d;
934 char delimiter;
935
936 /* Parses a CIFS service in form of //host/service/path… and splitting it in three parts. The last
937 * part is optional, in which case NULL is returned there. To maximize compatibility syntax with
938 * backslashes instead of slashes is accepted too. */
939
940 if (!s)
941 return -EINVAL;
942
943 p = startswith(s, "//");
944 if (!p) {
945 p = startswith(s, "\\\\");
946 if (!p)
947 return -EINVAL;
948 }
949
950 delimiter = s[0];
951 e = strchr(p, delimiter);
952 if (!e)
953 return -EINVAL;
954
955 h = strndup(p, e - p);
956 if (!h)
957 return -ENOMEM;
958
959 if (!hostname_is_valid(h, 0))
960 return -EINVAL;
961
962 e++;
963
964 d = strchrnul(e, delimiter);
965
966 ss = strndup(e, d - e);
967 if (!ss)
968 return -ENOMEM;
969
970 if (!filename_is_valid(ss))
971 return -EINVAL;
972
973 if (!isempty(d)) {
974 x = strdup(skip_leading_chars(d, CHAR_TO_STR(delimiter)));
975 if (!x)
976 return -EINVAL;
977
978 /* Make sure to convert Windows-style "\" → Unix-style / */
979 for (char *i = x; *i; i++)
980 if (*i == delimiter)
981 *i = '/';
982
983 if (!path_is_valid(x))
984 return -EINVAL;
985
986 path_simplify(x);
987 if (!path_is_normalized(x))
988 return -EINVAL;
989 }
990
991 if (ret_host)
992 *ret_host = TAKE_PTR(h);
993 if (ret_service)
994 *ret_service = TAKE_PTR(ss);
995 if (ret_path)
996 *ret_path = TAKE_PTR(x);
997
998 return 0;
999 }
1000
1001 int open_mkdir_at(int dirfd, const char *path, int flags, mode_t mode) {
1002 _cleanup_close_ int fd = -EBADF, parent_fd = -EBADF;
1003 _cleanup_free_ char *fname = NULL;
1004 int r;
1005
1006 /* Creates a directory with mkdirat() and then opens it, in the "most atomic" fashion we can
1007 * do. Guarantees that the returned fd refers to a directory. If O_EXCL is specified will fail if the
1008 * dir already exists. Otherwise will open an existing dir, but only if it is one. */
1009
1010 if (flags & ~(O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_EXCL|O_NOATIME|O_NOFOLLOW|O_PATH))
1011 return -EINVAL;
1012 if ((flags & O_ACCMODE) != O_RDONLY)
1013 return -EINVAL;
1014
1015 /* Note that O_DIRECTORY|O_NOFOLLOW is implied, but we allow specifying it anyway. The following
1016 * flags actually make sense to specify: O_CLOEXEC, O_EXCL, O_NOATIME, O_PATH */
1017
1018 if (isempty(path))
1019 return -EINVAL;
1020
1021 if (!filename_is_valid(path)) {
1022 _cleanup_free_ char *parent = NULL;
1023
1024 /* If this is not a valid filename, it's a path. Let's open the parent directory then, so
1025 * that we can pin it, and operate below it. */
1026
1027 r = path_extract_directory(path, &parent);
1028 if (r < 0)
1029 return r;
1030
1031 r = path_extract_filename(path, &fname);
1032 if (r < 0)
1033 return r;
1034
1035 parent_fd = openat(dirfd, parent, O_PATH|O_DIRECTORY|O_CLOEXEC);
1036 if (parent_fd < 0)
1037 return -errno;
1038
1039 dirfd = parent_fd;
1040 path = fname;
1041 }
1042
1043 fd = xopenat(dirfd, path, flags|O_CREAT|O_DIRECTORY|O_NOFOLLOW, mode);
1044 if (IN_SET(fd, -ELOOP, -ENOTDIR))
1045 return -EEXIST;
1046 if (fd < 0)
1047 return fd;
1048
1049 return TAKE_FD(fd);
1050 }
1051
1052 int openat_report_new(int dirfd, const char *pathname, int flags, mode_t mode, bool *ret_newly_created) {
1053 unsigned attempts = 7;
1054 int fd;
1055
1056 /* Just like openat(), but adds one thing: optionally returns whether we created the file anew or if
1057 * it already existed before. This is only relevant if O_CREAT is set without O_EXCL, and thus will
1058 * shortcut to openat() otherwise */
1059
1060 if (!ret_newly_created)
1061 return RET_NERRNO(openat(dirfd, pathname, flags, mode));
1062
1063 if (!FLAGS_SET(flags, O_CREAT) || FLAGS_SET(flags, O_EXCL)) {
1064 fd = openat(dirfd, pathname, flags, mode);
1065 if (fd < 0)
1066 return -errno;
1067
1068 *ret_newly_created = FLAGS_SET(flags, O_CREAT);
1069 return fd;
1070 }
1071
1072 for (;;) {
1073 /* First, attempt to open without O_CREAT/O_EXCL, i.e. open existing file */
1074 fd = openat(dirfd, pathname, flags & ~(O_CREAT | O_EXCL), mode);
1075 if (fd >= 0) {
1076 *ret_newly_created = false;
1077 return fd;
1078 }
1079 if (errno != ENOENT)
1080 return -errno;
1081
1082 /* So the file didn't exist yet, hence create it with O_CREAT/O_EXCL. */
1083 fd = openat(dirfd, pathname, flags | O_CREAT | O_EXCL, mode);
1084 if (fd >= 0) {
1085 *ret_newly_created = true;
1086 return fd;
1087 }
1088 if (errno != EEXIST)
1089 return -errno;
1090
1091 /* Hmm, so now we got EEXIST? So it apparently exists now? If so, let's try to open again
1092 * without the two flags. But let's not spin forever, hence put a limit on things */
1093
1094 if (--attempts == 0) /* Give up eventually, somebody is playing with us */
1095 return -EEXIST;
1096 }
1097 }
1098
1099 int xopenat(int dir_fd, const char *path, int flags, mode_t mode) {
1100 _cleanup_close_ int fd = -EBADF;
1101 bool made = false;
1102 int r;
1103
1104 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
1105 assert(path);
1106
1107 if (isempty(path)) {
1108 assert(!FLAGS_SET(flags, O_CREAT|O_EXCL));
1109 return fd_reopen(dir_fd, flags & ~O_NOFOLLOW);
1110 }
1111
1112 if (FLAGS_SET(flags, O_DIRECTORY|O_CREAT)) {
1113 r = RET_NERRNO(mkdirat(dir_fd, path, mode));
1114 if (r == -EEXIST) {
1115 if (FLAGS_SET(flags, O_EXCL))
1116 return -EEXIST;
1117
1118 made = false;
1119 } else if (r < 0)
1120 return r;
1121 else
1122 made = true;
1123
1124 flags &= ~(O_EXCL|O_CREAT);
1125 }
1126
1127 fd = RET_NERRNO(openat(dir_fd, path, flags, mode));
1128 if (fd < 0) {
1129 if (IN_SET(fd,
1130 /* We got ENOENT? then someone else immediately removed it after we
1131 * created it. In that case let's return immediately without unlinking
1132 * anything, because there simply isn't anything to unlink anymore. */
1133 -ENOENT,
1134 /* is a symlink? exists already → created by someone else, don't unlink */
1135 -ELOOP,
1136 /* not a directory? exists already → created by someone else, don't unlink */
1137 -ENOTDIR))
1138 return fd;
1139
1140 if (made)
1141 (void) unlinkat(dir_fd, path, AT_REMOVEDIR);
1142
1143 return fd;
1144 }
1145
1146 return TAKE_FD(fd);
1147 }
1148
1149 int xopenat_lock(int dir_fd, const char *path, int flags, mode_t mode, LockType locktype, int operation) {
1150 _cleanup_close_ int fd = -EBADF;
1151 int r;
1152
1153 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
1154 assert(path);
1155 assert(IN_SET(operation & ~LOCK_NB, LOCK_EX, LOCK_SH));
1156
1157 /* POSIX/UNPOSIX locks don't work on directories (errno is set to -EBADF so let's return early with
1158 * the same error here). */
1159 if (FLAGS_SET(flags, O_DIRECTORY) && locktype != LOCK_BSD)
1160 return -EBADF;
1161
1162 for (;;) {
1163 struct stat st;
1164
1165 fd = xopenat(dir_fd, path, flags, mode);
1166 if (fd < 0)
1167 return fd;
1168
1169 r = lock_generic(fd, locktype, operation);
1170 if (r < 0)
1171 return r;
1172
1173 /* If we acquired the lock, let's check if the file/directory still exists in the file
1174 * system. If not, then the previous exclusive owner removed it and then closed it. In such a
1175 * case our acquired lock is worthless, hence try again. */
1176
1177 if (fstat(fd, &st) < 0)
1178 return -errno;
1179 if (st.st_nlink > 0)
1180 break;
1181
1182 fd = safe_close(fd);
1183 }
1184
1185 return TAKE_FD(fd);
1186 }