From ec67ecc805bb00dd2c6ec10a3bcd106d7b56a96a Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 20 Nov 2026 17:00:05 +0100 Subject: [PATCH] tmpfiles: add the : and ~ prefixes for mode, user, and group A mode with a prefix was not read as a number, so the entry was taken to have no mode, and a user with one was looked up with the colon in the name, which skipped the entry: d /var/lib/app :0750 :app :app With : the mode, user, or group is only set on a path the entry creates, an existing one keeps what it has. With ~ the mode loses read, write, or execute when no one has that on the existing path, and setuid, setgid, and sticky when it is not a directory. Mode and owner now go through perm_set() in tmpfiles, which knows the entry created the path or not. Signed-off-by: Joachim Wiberg --- doc/ChangeLog.md | 3 + doc/tmpfiles-todo.md | 24 +++----- src/tmpfiles.c | 124 +++++++++++++++++++++++++++++++++++++----- test/tmpfiles-perm.sh | 13 +++++ 4 files changed, 132 insertions(+), 32 deletions(-) diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index 48fb72e4..e18d7504 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -15,6 +15,9 @@ All relevant changes are documented in this file. skipped unless the new `--boot` option is given. Finit gives it at boot, and now also `--remove`, so `r`, `R`, and `D` entries take effect at boot and no longer need a separate call +- tmpfiles: add the `:` prefix for mode, user, and group, to set them only + on a path the entry creates, and the `~` prefix for the mode, to drop + read, write, or execute where the existing path has it for no one - tmpfiles: the `x` and `X` entries now keep paths from being cleaned by `tmpfiles --clean`, before they were read and ignored - tmpfiles: add the `a`, `a+`, `A`, and `A+` entries for POSIX ACLs, used diff --git a/doc/tmpfiles-todo.md b/doc/tmpfiles-todo.md index 1d5112ea..791779fe 100644 --- a/doc/tmpfiles-todo.md +++ b/doc/tmpfiles-todo.md @@ -21,17 +21,7 @@ user to call it from cron. glob in the path is not cleaned -2. Prefixes that are silently dropped -------------------------------------- - -* Mode `~0755`, mask with the mode the path has, and `:0755`, only - when creating: neither is parsed, the entry is read as having no - mode -* User and group `:name`, only when creating: looked up as the name - `:name`, which fails. For `d` the directory is then not created - - -3. Mode and owner not applied +2. Mode and owner not applied ----------------------------- * `f` on a file that exists: the entry is skipped, tmpfiles.d(5) @@ -41,7 +31,7 @@ user to call it from cron. * `z`, `Z`: mode and owner, and they do nothing without `restorecon` -4. Device nodes with major or minor 0 +3. Device nodes with major or minor 0 ------------------------------------- `parse_mm()` rejects a 0 in either number, so an entry like this one @@ -50,7 +40,7 @@ is skipped: c /dev/tty 0666 root tty - 5:0 -5. Subvolumes +4. Subvolumes ------------- `v`, `q`, and `Q` are skipped as unsupported. systemd makes a plain @@ -58,7 +48,7 @@ directory when the file system is not btrfs, doing the same gets distribution files with these entries to work. -6. Specifiers +5. Specifiers ------------- A path with `%` in it is skipped, also the escaped `%%`. The ones that @@ -68,7 +58,7 @@ and `%V`. This is the largest gap for using such files unchanged, and the most work. -7. Remaining types and modifiers +6. Remaining types and modifiers -------------------------------- * `L?`, and `C+`, which is read as `C` @@ -78,7 +68,7 @@ the most work. * Quoted paths, a path with a space in it cannot be written -8. Command line +7. Command line --------------- Missing options: `--prefix`, `--root`, `--replace`, `--dry-run`, @@ -87,7 +77,7 @@ is the useful one, for packages that run `tmpfiles --create` on only their own paths. -9. Smaller things +8. Smaller things ------------------ * Files are processed one directory at a time. tmpfiles.d(5) sorts diff --git a/src/tmpfiles.c b/src/tmpfiles.c index a4efdde0..ec0404c7 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -349,6 +349,86 @@ static int parse_gid(const char *group) return getgroup(group); } +/* Mode and owner of an entry, mode 0 and id -1 when not given */ +struct perm { + mode_t mode; + int uid, gid; + int mask; /* ~, see perm_set() */ + int new_mode, new_uid, new_gid; /* :, only for a path the entry creates */ +}; + +/* 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. + */ +static int perm_set(int fd, const struct perm *p, int created) +{ + mode_t mode = p->mode; + int uid = p->uid, gid = p->gid; + struct stat st; + + if (fstat(fd, &st)) + return -1; + if (S_ISLNK(st.st_mode)) + return 0; + + if (!created) { + if (p->new_mode) + mode = 0; + if (p->new_uid) + uid = -1; + if (p->new_gid) + gid = -1; + + /* Keep read, write, and execute only where the path has it for someone */ + if (mode && p->mask) { + for (mode_t bits = 0111; bits <= 0444; bits <<= 1) { + if (!(st.st_mode & bits)) + mode &= ~bits; + } + if (!S_ISDIR(st.st_mode)) + mode &= ~07000; + } + } + + if (mode && (st.st_mode & 07777) != mode && fd_chmod(fd, mode)) + return -1; + if ((uid != -1 && st.st_uid != (uid_t)uid) || (gid != -1 && st.st_gid != (gid_t)gid)) + return fchownat(fd, "", uid, gid, AT_EMPTY_PATH); + + return 0; +} + +/* Never follows a link at @path, see also dirperm() */ +static int perm_path(const char *path, const struct perm *p, int created, int flags) +{ + int fd, rc; + + fd = open(path, flags | O_NOFOLLOW | O_CLOEXEC); + if (fd < 0) + return -1; + + rc = perm_set(fd, p, created); + close(fd); + + return rc; +} + /* What the x and X entries keep from being cleaned */ struct keep { char *pattern; @@ -765,9 +845,8 @@ static void tmpfiles(char *line) struct stat st, ast; int major, minor; int strc, rc = 0; + struct perm perm = { 0 }; int plus = 0, boot = 0; - int uid, gid; - mode_t mode = 0; FILE *fp = NULL; char buf[1024]; glob_t gl; @@ -803,16 +882,31 @@ static void tmpfiles(char *line) token = strtok(NULL, "\t "); if (token) { + for (; *token == '~' || *token == ':'; token++) { + if (*token == '~') + perm.mask = 1; + else + perm.new_mode = 1; + } + errno = 0; - mode = strtoul(token, &arg, 8); + perm.mode = strtoul(token, &arg, 8); if (errno || arg == token) - mode = 0; + perm.mode = 0; } user = strtok(NULL, "\t "); + if (user && user[0] == ':') { + perm.new_uid = 1; + user++; + } if (user && !strcmp(user, "-")) user = NULL; group = strtok(NULL, "\t "); + if (group && group[0] == ':') { + perm.new_gid = 1; + group++; + } if (group && !strcmp(group, "-")) group = NULL; @@ -820,14 +914,14 @@ static void tmpfiles(char *line) * Quiet about it, Finit's own files have entries for users of * services that may not be installed, and for the utmp group */ - uid = parse_uid(user); - if (user && uid < 0) { + perm.uid = parse_uid(user); + if (user && perm.uid < 0) { if (debug) warnx("Unknown user %s, skipping %s", user, path); return; } - gid = parse_gid(group); - if (group && gid < 0 && debug) + perm.gid = parse_gid(group); + if (group && perm.gid < 0 && debug) warnx("Unknown group %s, not setting the group of %s", group, path); age = strtok(NULL, "\t "); @@ -923,7 +1017,7 @@ static void tmpfiles(char *line) erase(path); } mkparent(path, 0755); - rc = blkdev(path, mode ?: 0644, major, minor); + rc = blkdev(path, perm.mode ?: 0644, major, minor); break; case 'c': rc = parse_mm(arg, &major, &minor); @@ -935,7 +1029,7 @@ static void tmpfiles(char *line) erase(path); } mkparent(path, 0755); - rc = chardev(path, mode ?: 0644, major, minor); + rc = chardev(path, perm.mode ?: 0644, major, minor); break; case 'C': if (!arg) { @@ -964,8 +1058,8 @@ static void tmpfiles(char *line) mkparent(path, 0755); omask = umask(0); - rc = makedir(path, mode ?: 0755); - if (!rc && dirperm(path, mode, uid, gid)) + rc = makedir(path, perm.mode ?: 0755); + if (!rc && perm_path(path, &perm, strc, O_RDONLY | O_DIRECTORY)) warn("Failed setting mode/owner on %s", path); umask(omask); break; @@ -978,7 +1072,7 @@ static void tmpfiles(char *line) char *p = gl.gl_pathv[i]; /* skip non-directories */ - if (dirperm(p, mode, uid, gid) && errno != ENOTDIR) + if (perm_path(p, &perm, 0, O_RDONLY | O_DIRECTORY) && errno != ENOTDIR) warn("Failed setting mode/owner on %s", p); } globfree(&gl); @@ -1012,7 +1106,7 @@ static void tmpfiles(char *line) write_arg(fp, arg); - if (fdperm(fd, mode, uid, gid)) + if (perm_set(fd, &perm, strc)) warn("Failed setting mode/owner on %s", path); rc = fclose(fp); @@ -1063,7 +1157,7 @@ static void tmpfiles(char *line) erase(path); } mkparent(path, 0755); - rc = mkfifo(path, mode ?: 0644); + rc = mkfifo(path, perm.mode ?: 0644); break; case 'r': case 'R': diff --git a/test/tmpfiles-perm.sh b/test/tmpfiles-perm.sh index 48bc2796..cad0b0b2 100755 --- a/test/tmpfiles-perm.sh +++ b/test/tmpfiles-perm.sh @@ -50,3 +50,16 @@ CONF run "/libexec/finit/tmpfiles --create $CONF" assert "Only the entry with a known user is created" "$(texec ls $DIR)" = "group" assert "With its mode and user" "$(texec stat -c '%a %U:%G' $DIR/group)" = "750 bin:root" + +sep +say 'With : mode and owner are only for a path the entry creates' +run "rm -rf $DIR; mkdir $DIR; chmod 1777 $DIR" +assert_perm "d $DIR :0700 :bin :bin" 1777 root:root +run "rmdir $DIR" +assert_perm "d $DIR :0700 :bin :bin" 700 bin:bin + +sep +say 'With ~ the mode loses what no one has on the path' +run "chmod 500 $DIR" +assert_perm "e $DIR ~0777" 555 bin:bin +assert_perm "e $DIR :~0700 :sys" 555 bin:bin