From 7b3b217e603418c002c03a468457b800456b7de7 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Sat, 21 Nov 2026 17:01:39 +0100 Subject: [PATCH] tmpfiles: fix C entries, never copy over what exists A C entry for a directory does the opposite of tmpfiles.d(5): C /etc/pam.d - - - - When /etc/pam.d does not exist the entry fails with "Not a directory", the files of the source are copied to a file with the name of the path, one over the other. When it is an empty directory nothing is copied. When it has files in it, they are overwritten with the ones from /usr/share/factory, at every boot. For a file, an existing one is overwritten as well. is_dir_empty() returns true for a directory that is not empty, and rsync() from libite copies over what is there and needs a slash at the end of a destination directory that is missing. Copy with a tree walk of our own instead. It copies to a path that is missing or an empty directory, overwrites nothing, and does not follow links. Mode, owner, and modification time are those of the source. Signed-off-by: Joachim Wiberg --- doc/ChangeLog.md | 6 ++ src/tmpfiles.c | 223 ++++++++++++++++++++++++++++++++++++------ test/Makefile.am | 2 + test/tmpfiles-copy.sh | 56 +++++++++++ 4 files changed, 258 insertions(+), 29 deletions(-) create mode 100755 test/tmpfiles-copy.sh diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index 0d4fa799..073e4272 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -337,6 +337,12 @@ All relevant changes are documented in this file. - Fix tmpfiles `w` entries for a regular file, the file was truncated. As in tmpfiles.d(5), the argument is now written over the start of what the file holds +- Fix tmpfiles `C` entries. A directory was not copied to a path that + did not exist, or to an empty directory, and was copied over a + directory that had files in it, at every boot, as with the `/etc/pam.d` + entry in Finit's own `etc.conf`. A file was copied over an existing + one. As in tmpfiles.d(5), nothing that exists is now overwritten, and + links are copied as links - Fix logins refused after boot on a system with systemd's tmpfiles.d files installed. They create `/run/nologin` at boot, and on systemd it is removed by systemd-user-sessions. Finit now removes it when leaving diff --git a/src/tmpfiles.c b/src/tmpfiles.c index bf87a3f5..81375a1c 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -131,7 +131,7 @@ static int is_dir_empty(const char *path) free(namelist[i]); free(namelist); - return num >= 3; + return num < 3; } static int glob_do(const char *path, int (*cb)(const char *)) @@ -183,6 +183,195 @@ static void mkparent(char *path, mode_t mode) mkpath(dirname(strdupa(path)), mode); } +static int fd_chmod(int fd, mode_t mode) +{ + char proc[32]; + + if (!fchmod(fd, mode)) + return 0; + if (errno != EBADF) + return -1; + + snprintf(proc, sizeof(proc), "/proc/self/fd/%d", fd); + + return chmod(proc, mode); +} + +static int copy_errno; + +static void copy_fail(void) +{ + if (!copy_errno) + copy_errno = errno; +} + +/* Owner before mode, as in perm_set(). Not root when run by hand */ +static void copy_perm(int fd, const struct stat *st) +{ + if ((fchownat(fd, "", st->st_uid, st->st_gid, AT_EMPTY_PATH) && errno != EPERM) || + fd_chmod(fd, st->st_mode & 07777)) + copy_fail(); +} + +static void copy_file(int sdir, const char *sname, int ddir, const char *dname, const struct stat *st) +{ + struct timespec tv[2] = { st->st_atim, st->st_mtim }; + char buf[BUFSIZ]; + ssize_t len = -1; + int in, out; + + in = openat(sdir, sname, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); + if (in < 0) { + copy_fail(); + return; + } + + out = openat(ddir, dname, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0600); + if (out < 0) { + if (errno != EEXIST) + copy_fail(); + close(in); + return; + } + + while ((len = read(in, buf, sizeof(buf))) > 0) { + for (ssize_t off = 0, num; off < len; off += num) { + num = write(out, &buf[off], len - off); + if (num < 0) { + len = -1; + goto done; + } + } + } +done: + if (len < 0) + copy_fail(); + else + copy_perm(out, st); + futimens(out, tv); + close(in); + close(out); +} + +/* + * Copy @sname in the directory @sdir to @dname in @ddir, with all that + * is below it. What exists at the destination is left as it is, and + * only a real directory there is gone into, never a link to one. + */ +static void copy_node(int sdir, const char *sname, int ddir, const char *dname) +{ + char target[PATH_MAX]; + int sfd, dfd, created; + struct dirent *d; + struct stat st; + ssize_t len; + DIR *dir; + + if (fstatat(sdir, sname, &st, AT_SYMLINK_NOFOLLOW)) { + copy_fail(); + return; + } + + switch (st.st_mode & S_IFMT) { + case S_IFDIR: + break; + case S_IFREG: + copy_file(sdir, sname, ddir, dname, &st); + return; + case S_IFLNK: + len = readlinkat(sdir, sname, target, sizeof(target) - 1); + if (len < 0) { + copy_fail(); + return; + } + target[len] = 0; + if (symlinkat(target, ddir, dname)) { + if (errno != EEXIST) + copy_fail(); + return; + } + if (fchownat(ddir, dname, st.st_uid, st.st_gid, AT_SYMLINK_NOFOLLOW) && errno != EPERM) + copy_fail(); + return; + default: + if (mknodat(ddir, dname, (st.st_mode & S_IFMT) | 0600, st.st_rdev)) { + if (errno != EEXIST) + copy_fail(); + return; + } + dfd = openat(ddir, dname, O_PATH | O_NOFOLLOW | O_CLOEXEC); + if (dfd < 0) { + copy_fail(); + return; + } + copy_perm(dfd, &st); + close(dfd); + return; + } + + created = !mkdirat(ddir, dname, 0700); + if (!created && errno != EEXIST) { + copy_fail(); + return; + } + + dfd = openat(ddir, dname, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); + if (dfd < 0) { + if (created) + copy_fail(); + return; + } + + sfd = openat(sdir, sname, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); + dir = sfd < 0 ? NULL : fdopendir(sfd); + if (!dir) { + copy_fail(); + if (sfd >= 0) + close(sfd); + close(dfd); + return; + } + + while ((d = readdir(dir))) { + if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) + continue; + copy_node(sfd, d->d_name, dfd, d->d_name); + } + closedir(dir); + + if (created) + copy_perm(dfd, &st); + close(dfd); +} + +/* + * Copy @src, of which @st tells the kind, to @dst. Links are not + * followed. A file is not copied when @dst exists. A directory is + * when @dst is missing or an empty directory, with @merge also when + * @dst has something in it, to add what is missing. + */ +static int copy_tree(const char *src, const struct stat *st, char *dst, int merge) +{ + struct stat dt; + + if (!lstat(dst, &dt)) { + if (!S_ISDIR(st->st_mode) || !S_ISDIR(dt.st_mode)) + return 0; + if (!merge && !is_dir_empty(dst)) + return 0; + } + + mkparent(dst, 0755); + copy_errno = 0; + copy_node(AT_FDCWD, src, AT_FDCWD, dst); + if (copy_errno) { + errno = copy_errno; + return -1; + } + + return 0; +} + static char *write_hex(FILE *fp, char *p) { unsigned char val; @@ -495,20 +684,6 @@ struct perm { }; /* Also for an fd opened with O_PATH, which fchmod() does not take */ -static int fd_chmod(int fd, mode_t mode) -{ - char proc[32]; - - if (!fchmod(fd, mode)) - return 0; - if (errno != EBADF) - return -1; - - snprintf(proc, sizeof(proc), "/proc/self/fd/%d", fd); - - return chmod(proc, mode); -} - /* * Set mode and owner on an open fd, only what differs is touched, the * path may be immutable. @created tells if the entry made the path. @@ -1739,22 +1914,12 @@ static void tmpfiles(char *line) paste(buf, sizeof(buf), "/usr/share/factory", path); arg = buf; } - if (fisdir(path) && !is_dir_empty(path)) + if (lstat(arg, &ast)) break; - if (fisdir(arg) && !fisslashdir(arg)) { - size_t len = strlen(arg) + 2; - dst = malloc(len); - if (!dst) - break; - snprintf(dst, len, "%s/", arg); - arg = dst; - } - mkparent(path, 0755); - rc = rsync(arg, path, LITE_FOPT_KEEP_MTIME, NULL); - if (rc && errno == ENOENT) - rc = 0; - else if (!rc && (strc || S_ISDIR(st.st_mode)) && perm_path(path, &perm, strc, O_PATH)) + rc = copy_tree(arg, &ast, path, 0); + if (!rc && !lstat(path, &st) && !S_ISLNK(st.st_mode) && !((st.st_mode ^ ast.st_mode) & S_IFMT) && + perm_path(path, &perm, strc, O_PATH)) warn("Failed setting mode/owner on %s", path); break; case 'd': diff --git a/test/Makefile.am b/test/Makefile.am index 0d643faa..e31c308c 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -87,6 +87,7 @@ EXTRA_DIST += tmpfiles-attr.sh EXTRA_DIST += tmpfiles-perm.sh EXTRA_DIST += tmpfiles-boot.sh EXTRA_DIST += tmpfiles-clean.sh +EXTRA_DIST += tmpfiles-copy.sh EXTRA_DIST += tmpfiles-order.sh EXTRA_DIST += tmpfiles-specifier.sh EXTRA_DIST += tmpfiles-unsupported.sh @@ -173,6 +174,7 @@ TESTS += tmpfiles-attr.sh TESTS += tmpfiles-perm.sh TESTS += tmpfiles-boot.sh TESTS += tmpfiles-clean.sh +TESTS += tmpfiles-copy.sh TESTS += tmpfiles-order.sh TESTS += tmpfiles-specifier.sh TESTS += tmpfiles-unsupported.sh diff --git a/test/tmpfiles-copy.sh b/test/tmpfiles-copy.sh new file mode 100755 index 00000000..06ca4941 --- /dev/null +++ b/test/tmpfiles-copy.sh @@ -0,0 +1,56 @@ +#!/bin/sh +# Verify the C entry of tmpfiles, copy a file or a tree +set -eu + +TEST_DIR=$(dirname "$0") + +DIR=/tmp/copy +SRC=/tmp/copy-src +CONF=/tmp/copy.conf + +content() +{ + texec sh -c "cat $1 2>/dev/null || echo none" +} + +test_teardown() +{ + say "Running test teardown." + run "rm -rf $DIR $SRC $CONF" +} + +# shellcheck source=/dev/null +. "$TEST_DIR/lib/setup.sh" + +run "mkdir -p $SRC/sub $DIR/empty $DIR/full/sub $DIR/merge/sub" +run "echo new > $SRC/file; echo new > $SRC/sub/file; echo new > $SRC/sub/other; ln -s file $SRC/link; chmod 640 $SRC/file" +run "echo old > $DIR/full/file; echo old > $DIR/merge/sub/file; echo old > $DIR/one-old" + +run "cat > $CONF" <