tmpfiles: skip any entry with an unknown user

What an unknown user in an entry did depended on the type.  A d entry
was skipped, but this one made a file owned by root:

    f /run/frr/state 0640 frr frr

The user was looked up in each type on its own, and only d checked
the result.

Look it up once for the entry, and skip the entry when the user is
not known.  An unknown group leaves the group of the path as it is,
for a new path that is the group of root, as before.  Both are only
reported with --debug, Finit's own files have entries for services
that may not be installed.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-20 18:02:36 +01:00
1 parent f5e7946489
commit a726aff30b
4 files changed
+36 -27

No files matched your search

+3
View File
@@ -263,6 +263,9 @@ All relevant changes are documented in this file.
file or in the files after it. Such an entry is now skipped with a warning file or in the files after it. Such an entry is now skipped with a warning
- Fix `tmpfiles --clean` cleaning other file systems mounted below the - Fix `tmpfiles --clean` cleaning other file systems mounted below the
directory of an entry, it now stays on the file system of the directory directory of an entry, it now stays on the file system of the directory
- Fix tmpfiles entries with an unknown user: `d` skipped the entry, but `f`
created the file owned by root and `e` set the group anyway. All of them
now skip the entry. An unknown group still only leaves the group as it is
- Fix age `0` in a tmpfiles entry, it was read as no age. As in - Fix age `0` in a tmpfiles entry, it was read as no age. As in
tmpfiles.d(5), `tmpfiles --clean` now empties the directory of such an tmpfiles.d(5), `tmpfiles --clean` now empties the directory of such an
entry entry
-3
View File
@@ -29,9 +29,6 @@ user to call it from cron.
mode mode
* User and group `:name`, only when creating: looked up as the name * User and group `:name`, only when creating: looked up as the name
`:name`, which fails. For `d` the directory is then not created `:name`, which fails. For `d` the directory is then not created
* An unknown group becomes root, an unknown user makes `d` skip the
entry while `e` and `f` go on. Warn and leave the owner alone in
all of them
3. Mode and owner not applied 3. Mode and owner not applied
+22 -24
View File
@@ -331,8 +331,7 @@ static int parse_uid(const char *user)
/** /**
* Parse group string - supports both names and numeric GIDs. * Parse group string - supports both names and numeric GIDs.
* Returns GID on success, 0 for an unknown group, and -1 when no * Returns GID on success, -1 on failure or when no group is given.
* group is given.
*/ */
static int parse_gid(const char *group) static int parse_gid(const char *group)
{ {
@@ -347,9 +346,7 @@ static int parse_gid(const char *group)
return val; return val;
/* Not numeric, look up by name */ /* Not numeric, look up by name */
val = getgroup(group); return getgroup(group);
return val < 0 ? 0 : val;
} }
/* What the x and X entries keep from being cleaned */ /* What the x and X entries keep from being cleaned */
@@ -769,6 +766,7 @@ static void tmpfiles(char *line)
int major, minor; int major, minor;
int strc, rc = 0; int strc, rc = 0;
int plus = 0, boot = 0; int plus = 0, boot = 0;
int uid, gid;
mode_t mode = 0; mode_t mode = 0;
FILE *fp = NULL; FILE *fp = NULL;
char buf[1024]; char buf[1024];
@@ -818,6 +816,20 @@ static void tmpfiles(char *line)
if (group && !strcmp(group, "-")) if (group && !strcmp(group, "-"))
group = NULL; group = NULL;
/*
* 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) {
if (debug)
warnx("Unknown user %s, skipping %s", user, path);
return;
}
gid = parse_gid(group);
if (group && gid < 0 && debug)
warnx("Unknown group %s, not setting the group of %s", group, path);
age = strtok(NULL, "\t "); age = strtok(NULL, "\t ");
arg = strtok(NULL, "\n"); arg = strtok(NULL, "\n");
if (arg) { if (arg) {
@@ -948,31 +960,20 @@ static void tmpfiles(char *line)
break; break;
case 'd': case 'd':
case 'D': { case 'D': {
int uid, gid;
mode_t omask; mode_t omask;
mkparent(path, 0755); mkparent(path, 0755);
omask = umask(0); omask = umask(0);
uid = parse_uid(user); rc = makedir(path, mode ?: 0755);
if (!user || uid >= 0) { if (!rc && dirperm(path, mode, uid, gid))
gid = parse_gid(group); warn("Failed setting mode/owner on %s", path);
rc = makedir(path, mode ?: 0755);
if (!rc && dirperm(path, mode, uid, gid))
warn("Failed setting mode/owner on %s", path);
}
umask(omask); umask(omask);
break; break;
} }
case 'e': { case 'e':
int uid, gid;
if (glob(path, GLOB_NOESCAPE, NULL, &gl)) if (glob(path, GLOB_NOESCAPE, NULL, &gl))
break; break;
uid = parse_uid(user);
gid = parse_gid(group);
for (size_t i = 0; i < gl.gl_pathc; i++) { for (size_t i = 0; i < gl.gl_pathc; i++) {
char *p = gl.gl_pathv[i]; char *p = gl.gl_pathv[i];
@@ -982,11 +983,10 @@ static void tmpfiles(char *line)
} }
globfree(&gl); globfree(&gl);
break; break;
}
case 'f': case 'f':
case 'F': { case 'F': {
int flags = O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC; int flags = O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC;
int fd, uid, gid; int fd;
if (plus || type[0] == 'F') { if (plus || type[0] == 'F') {
flags |= O_TRUNC; /* create or truncate */ flags |= O_TRUNC; /* create or truncate */
@@ -1012,8 +1012,6 @@ static void tmpfiles(char *line)
write_arg(fp, arg); write_arg(fp, arg);
uid = parse_uid(user);
gid = parse_gid(group);
if (fdperm(fd, mode, uid, gid)) if (fdperm(fd, mode, uid, gid))
warn("Failed setting mode/owner on %s", path); warn("Failed setting mode/owner on %s", path);
+11
View File
@@ -39,3 +39,14 @@ assert_perm "d $DIR - bin" 750 bin:daemon
assert_perm "d $DIR - - sys" 750 bin:sys assert_perm "d $DIR - - sys" 750 bin:sys
assert_perm "e $DIR" 750 bin:sys assert_perm "e $DIR" 750 bin:sys
assert_perm "d $DIR 1777 root root" 1777 root:root assert_perm "d $DIR 1777 root root" 1777 root:root
sep
say 'An unknown user skips the entry, an unknown group only the group'
run "cat > $CONF" <<CONF
d $DIR/user 0755 nosuchuser
f $DIR/file 0644 nosuchuser
d $DIR/group 0750 bin nosuchgroup
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"