tmpfiles: keep the owner of an existing path when none is given

An entry without a user or group, or with "-", made root the owner of
an existing directory or file:

    mkdir -p /tmp/t && chown daemon:daemon /tmp/t
    echo 'd /tmp/t' > t.conf
    tmpfiles --create t.conf
    stat -c %U:%G /tmp/t

This gave root:root.  A missing field was read as "root" when parsing
the entry, the same as if it had been written out.  tmpfiles.d(5)
only adjusts the owner when the entry gives one.

A missing user or group is now -1, and fdperm() looks at the two ids
separately, so "d /tmp/t - - sys" changes the group and keeps the
owner.  mksubsys() and the service directories used uid -1 to skip
the chown altogether, they now pass -1 for the group as well.

Issue #523

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-20 11:23:36 +01:00
1 parent 94bdccd200
commit f95db7e6df
5 files changed
+41 -35

No files matched your search

+15 -18
View File
@@ -300,14 +300,14 @@ static time_t parse_age(const char *age)
/**
* Parse user string - supports both names and numeric UIDs.
* Returns UID on success, -1 on failure.
* Returns UID on success, -1 on failure or when no user is given.
*/
static int parse_uid(const char *user)
{
long val;
if (!user || !user[0])
return 0;
if (!user)
return -1;
/* Check if it's a numeric UID */
val = atonum(user);
@@ -320,14 +320,15 @@ static int parse_uid(const char *user)
/**
* Parse group string - supports both names and numeric GIDs.
* Returns GID on success, -1 on failure.
* Returns GID on success, 0 for an unknown group, and -1 when no
* group is given.
*/
static int parse_gid(const char *group)
{
long val;
if (!group || !group[0])
return 0;
if (!group)
return -1;
/* Check if it's a numeric GID */
val = atonum(group);
@@ -335,7 +336,9 @@ static int parse_gid(const char *group)
return val;
/* Not numeric, look up by name */
return getgroup(group);
val = getgroup(group);
return val < 0 ? 0 : val;
}
/* Globals for do_clean() callback - nftw doesn't support user data */
@@ -419,11 +422,11 @@ static void tmpfiles(char *line)
}
user = strtok(NULL, "\t ");
if (!user || !strcmp(user, "-"))
user = "root";
if (user && !strcmp(user, "-"))
user = NULL;
group = strtok(NULL, "\t ");
if (!group || !strcmp(group, "-"))
group = "root";
if (group && !strcmp(group, "-"))
group = NULL;
age = strtok(NULL, "\t ");
arg = strtok(NULL, "\n");
@@ -553,10 +556,8 @@ static void tmpfiles(char *line)
mkparent(path, 0755);
omask = umask(0);
uid = parse_uid(user);
if (uid >= 0) {
if (!user || uid >= 0) {
gid = parse_gid(group);
if (gid < 0)
gid = 0;
rc = makedir(path, mode ?: 0755);
if (!rc && dirperm(path, mode, uid, gid))
@@ -573,8 +574,6 @@ static void tmpfiles(char *line)
uid = parse_uid(user);
gid = parse_gid(group);
if (gid < 0)
gid = 0;
for (size_t i = 0; i < gl.gl_pathc; i++) {
char *p = gl.gl_pathv[i];
@@ -617,8 +616,6 @@ static void tmpfiles(char *line)
uid = parse_uid(user);
gid = parse_gid(group);
if (gid < 0)
gid = 0;
if (fdperm(fd, mode, uid, gid))
warn("Failed setting mode/owner on %s", path);