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 <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-20 18:02:37 +01:00
1 parent a726aff30b
commit ec67ecc805
4 files changed
+132 -32

No files matched your search

+3
View File
@@ -15,6 +15,9 @@ All relevant changes are documented in this file.
skipped unless the new `--boot` option is given. Finit gives it at 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 boot, and now also `--remove`, so `r`, `R`, and `D` entries take effect
at boot and no longer need a separate call 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: the `x` and `X` entries now keep paths from being cleaned by
`tmpfiles --clean`, before they were read and ignored `tmpfiles --clean`, before they were read and ignored
- tmpfiles: add the `a`, `a+`, `A`, and `A+` entries for POSIX ACLs, used - tmpfiles: add the `a`, `a+`, `A`, and `A+` entries for POSIX ACLs, used
+7 -17
View File
@@ -21,17 +21,7 @@ user to call it from cron.
glob in the path is not cleaned glob in the path is not cleaned
2. Prefixes that are silently dropped 2. Mode and owner not applied
-------------------------------------
* 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
----------------------------- -----------------------------
* `f` on a file that exists: the entry is skipped, tmpfiles.d(5) * `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` * `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 `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 c /dev/tty 0666 root tty - 5:0
5. Subvolumes 4. Subvolumes
------------- -------------
`v`, `q`, and `Q` are skipped as unsupported. systemd makes a plain `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. distribution files with these entries to work.
6. Specifiers 5. Specifiers
------------- -------------
A path with `%` in it is skipped, also the escaped `%%`. The ones that 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. the most work.
7. Remaining types and modifiers 6. Remaining types and modifiers
-------------------------------- --------------------------------
* `L?`, and `C+`, which is read as `C` * `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 * 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`, 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. their own paths.
9. Smaller things 8. Smaller things
------------------ ------------------
* Files are processed one directory at a time. tmpfiles.d(5) sorts * Files are processed one directory at a time. tmpfiles.d(5) sorts
+109 -15
View File
@@ -349,6 +349,86 @@ static int parse_gid(const char *group)
return getgroup(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 */ /* What the x and X entries keep from being cleaned */
struct keep { struct keep {
char *pattern; char *pattern;
@@ -765,9 +845,8 @@ static void tmpfiles(char *line)
struct stat st, ast; struct stat st, ast;
int major, minor; int major, minor;
int strc, rc = 0; int strc, rc = 0;
struct perm perm = { 0 };
int plus = 0, boot = 0; int plus = 0, boot = 0;
int uid, gid;
mode_t mode = 0;
FILE *fp = NULL; FILE *fp = NULL;
char buf[1024]; char buf[1024];
glob_t gl; glob_t gl;
@@ -803,16 +882,31 @@ static void tmpfiles(char *line)
token = strtok(NULL, "\t "); token = strtok(NULL, "\t ");
if (token) { if (token) {
for (; *token == '~' || *token == ':'; token++) {
if (*token == '~')
perm.mask = 1;
else
perm.new_mode = 1;
}
errno = 0; errno = 0;
mode = strtoul(token, &arg, 8); perm.mode = strtoul(token, &arg, 8);
if (errno || arg == token) if (errno || arg == token)
mode = 0; perm.mode = 0;
} }
user = strtok(NULL, "\t "); user = strtok(NULL, "\t ");
if (user && user[0] == ':') {
perm.new_uid = 1;
user++;
}
if (user && !strcmp(user, "-")) if (user && !strcmp(user, "-"))
user = NULL; user = NULL;
group = strtok(NULL, "\t "); group = strtok(NULL, "\t ");
if (group && group[0] == ':') {
perm.new_gid = 1;
group++;
}
if (group && !strcmp(group, "-")) if (group && !strcmp(group, "-"))
group = NULL; group = NULL;
@@ -820,14 +914,14 @@ static void tmpfiles(char *line)
* Quiet about it, Finit's own files have entries for users of * Quiet about it, Finit's own files have entries for users of
* services that may not be installed, and for the utmp group * services that may not be installed, and for the utmp group
*/ */
uid = parse_uid(user); perm.uid = parse_uid(user);
if (user && uid < 0) { if (user && perm.uid < 0) {
if (debug) if (debug)
warnx("Unknown user %s, skipping %s", user, path); warnx("Unknown user %s, skipping %s", user, path);
return; return;
} }
gid = parse_gid(group); perm.gid = parse_gid(group);
if (group && gid < 0 && debug) if (group && perm.gid < 0 && debug)
warnx("Unknown group %s, not setting the group of %s", group, path); warnx("Unknown group %s, not setting the group of %s", group, path);
age = strtok(NULL, "\t "); age = strtok(NULL, "\t ");
@@ -923,7 +1017,7 @@ static void tmpfiles(char *line)
erase(path); erase(path);
} }
mkparent(path, 0755); mkparent(path, 0755);
rc = blkdev(path, mode ?: 0644, major, minor); rc = blkdev(path, perm.mode ?: 0644, major, minor);
break; break;
case 'c': case 'c':
rc = parse_mm(arg, &major, &minor); rc = parse_mm(arg, &major, &minor);
@@ -935,7 +1029,7 @@ static void tmpfiles(char *line)
erase(path); erase(path);
} }
mkparent(path, 0755); mkparent(path, 0755);
rc = chardev(path, mode ?: 0644, major, minor); rc = chardev(path, perm.mode ?: 0644, major, minor);
break; break;
case 'C': case 'C':
if (!arg) { if (!arg) {
@@ -964,8 +1058,8 @@ static void tmpfiles(char *line)
mkparent(path, 0755); mkparent(path, 0755);
omask = umask(0); omask = umask(0);
rc = makedir(path, mode ?: 0755); rc = makedir(path, perm.mode ?: 0755);
if (!rc && dirperm(path, mode, uid, gid)) if (!rc && perm_path(path, &perm, strc, O_RDONLY | O_DIRECTORY))
warn("Failed setting mode/owner on %s", path); warn("Failed setting mode/owner on %s", path);
umask(omask); umask(omask);
break; break;
@@ -978,7 +1072,7 @@ static void tmpfiles(char *line)
char *p = gl.gl_pathv[i]; char *p = gl.gl_pathv[i];
/* skip non-directories */ /* 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); warn("Failed setting mode/owner on %s", p);
} }
globfree(&gl); globfree(&gl);
@@ -1012,7 +1106,7 @@ static void tmpfiles(char *line)
write_arg(fp, arg); write_arg(fp, arg);
if (fdperm(fd, mode, uid, gid)) if (perm_set(fd, &perm, strc))
warn("Failed setting mode/owner on %s", path); warn("Failed setting mode/owner on %s", path);
rc = fclose(fp); rc = fclose(fp);
@@ -1063,7 +1157,7 @@ static void tmpfiles(char *line)
erase(path); erase(path);
} }
mkparent(path, 0755); mkparent(path, 0755);
rc = mkfifo(path, mode ?: 0644); rc = mkfifo(path, perm.mode ?: 0644);
break; break;
case 'r': case 'r':
case 'R': case 'R':
+13
View File
@@ -50,3 +50,16 @@ CONF
run "/libexec/finit/tmpfiles --create $CONF" run "/libexec/finit/tmpfiles --create $CONF"
assert "Only the entry with a known user is created" "$(texec ls $DIR)" = "group" 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" 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