tmpfiles: set mode and owner for the entries that ignored them

Several types read the mode and owner of an entry and did nothing
with them.  An f entry was skipped when the file was there:

    f /var/log/app.log 0640 app adm

This left a file with another mode or owner as it was.  The owner of
p, b, and c entries was never set, and their mode was cut by the
umask.  C, z, and Z entries had neither applied.

Set them for all of these, Z for everything below the path.  Fifos
and device nodes are opened with O_PATH, so nothing is opened for
real.  A path that is not what the entry makes, like a directory
where a p entry wants a fifo, is left alone.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-20 18:03:30 +01:00
1 parent ec67ecc805
commit adf7080fca
4 files changed
+107 -37

No files matched your search

+3
View File
@@ -269,6 +269,9 @@ All relevant changes are documented in this file.
- 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 mode and owner of tmpfiles entries that ignored them: an `f` entry for
a file that exists, the owner of `p`, `b`, and `c` entries, and both for
`C`, `z`, and `Z` entries
- 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
entry
+12 -16
View File
@@ -21,17 +21,7 @@ user to call it from cron.
glob in the path is not cleaned
2. Mode and owner not applied
-----------------------------
* `f` on a file that exists: the entry is skipped, tmpfiles.d(5)
still adjusts mode and owner
* `p`, `b`, `c`: owner
* `C`: mode and owner
* `z`, `Z`: mode and owner, and they do nothing without `restorecon`
3. Device nodes with major or minor 0
2. Device nodes with major or minor 0
-------------------------------------
`parse_mm()` rejects a 0 in either number, so an entry like this one
@@ -40,7 +30,7 @@ is skipped:
c /dev/tty 0666 root tty - 5:0
4. Subvolumes
3. Subvolumes
-------------
`v`, `q`, and `Q` are skipped as unsupported. systemd makes a plain
@@ -48,7 +38,7 @@ directory when the file system is not btrfs, doing the same gets
distribution files with these entries to work.
5. Specifiers
4. Specifiers
-------------
A path with `%` in it is skipped, also the escaped `%%`. The ones that
@@ -58,17 +48,18 @@ and `%V`. This is the largest gap for using such files unchanged, and
the most work.
6. Remaining types and modifiers
5. Remaining types and modifiers
--------------------------------
* `L?`, and `C+`, which is read as `C`
* `z` and `Z` only set the SELinux label when `restorecon` is installed
* `t`, `T` for extended attributes and `h`, `H` for file attributes
* Modifiers `-` (do not fail), `=` (remove what is of another type),
`~` (base64 argument), and `^` (credential as argument)
* Quoted paths, a path with a space in it cannot be written
7. Command line
6. Command line
---------------
Missing options: `--prefix`, `--root`, `--replace`, `--dry-run`,
@@ -77,7 +68,7 @@ is the useful one, for packages that run `tmpfiles --create` on only
their own paths.
8. Smaller things
7. Smaller things
------------------
* Files are processed one directory at a time. tmpfiles.d(5) sorts
@@ -85,4 +76,9 @@ their own paths.
in `/etc` should run before `50-y.conf` in `/usr/lib`
* `a` and `A` handle an ACL of at most 32 entries, a path with more is
left as it is, with a warning
* A new `f` file without a mode gets what the umask leaves of 0666,
it should be 0644
* A `d` or `e` entry for a path that is a link to a directory warns
that mode and owner could not be set
* An age so large that it overflows is not caught
* `mksubsys()` in `src/util.c` is unused
+65 -21
View File
@@ -429,6 +429,19 @@ static int perm_path(const char *path, const struct perm *p, int created, int fl
return rc;
}
/* Global for perm_cb(), nftw() cannot pass user data */
static const struct perm *perm_tree;
static int perm_cb(const char *path, const struct stat *st, int tflag, struct FTW *ftw)
{
(void)st; (void)tflag; (void)ftw;
if (perm_path(path, perm_tree, 0, O_PATH))
warn("Failed setting mode/owner on %s", path);
return 0;
}
/* What the x and X entries keep from being cleaned */
struct keep {
char *pattern;
@@ -1011,25 +1024,35 @@ static void tmpfiles(char *line)
rc = parse_mm(arg, &major, &minor);
if (rc)
break;
if (!strc) {
if (!plus)
break;
if (!strc && plus) {
erase(path);
strc = -1;
}
mkparent(path, 0755);
rc = blkdev(path, perm.mode ?: 0644, major, minor);
if (strc) {
mkparent(path, 0755);
rc = blkdev(path, perm.mode ?: 0644, major, minor);
} else if (!S_ISBLK(st.st_mode)) {
break;
}
if (!rc && perm_path(path, &perm, strc, O_PATH))
warn("Failed setting mode/owner on %s", path);
break;
case 'c':
rc = parse_mm(arg, &major, &minor);
if (rc)
break;
if (!strc) {
if (!plus)
break;
if (!strc && plus) {
erase(path);
strc = -1;
}
mkparent(path, 0755);
rc = chardev(path, perm.mode ?: 0644, major, minor);
if (strc) {
mkparent(path, 0755);
rc = chardev(path, perm.mode ?: 0644, major, minor);
} else if (!S_ISCHR(st.st_mode)) {
break;
}
if (!rc && perm_path(path, &perm, strc, O_PATH))
warn("Failed setting mode/owner on %s", path);
break;
case 'C':
if (!arg) {
@@ -1051,6 +1074,8 @@ static void tmpfiles(char *line)
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))
warn("Failed setting mode/owner on %s", path);
break;
case 'd':
case 'D': {
@@ -1085,8 +1110,11 @@ static void tmpfiles(char *line)
if (plus || type[0] == 'F') {
flags |= O_TRUNC; /* create or truncate */
} else {
if (!strc)
break; /* create only if missing */
if (!strc) { /* create only if missing */
if (perm_path(path, &perm, 0, O_PATH))
warn("Failed setting mode/owner on %s", path);
break;
}
flags |= O_EXCL;
}
@@ -1151,13 +1179,18 @@ static void tmpfiles(char *line)
rc = 0;
break;
case 'p':
if (!strc) {
if (!plus)
break;
if (!strc && plus) {
erase(path);
strc = -1;
}
mkparent(path, 0755);
rc = mkfifo(path, perm.mode ?: 0644);
if (strc) {
mkparent(path, 0755);
rc = mkfifo(path, perm.mode ?: 0644);
} else if (!S_ISFIFO(st.st_mode)) {
break;
}
if (!rc && perm_path(path, &perm, strc, O_PATH))
warn("Failed setting mode/owner on %s", path);
break;
case 'r':
case 'R':
@@ -1186,13 +1219,24 @@ static void tmpfiles(char *line)
opts = "-R";
/* fallthrough */
case 'z':
if (!whichp("restorecon"))
break;
if (glob(path, GLOB_NOESCAPE, NULL, &gl))
break;
for (size_t i = 0; i < gl.gl_pathc; i++)
systemf("restorecon %s %s", opts, gl.gl_pathv[i]);
for (size_t i = 0; i < gl.gl_pathc; i++) {
char *p = gl.gl_pathv[i];
if (type[0] == 'Z') {
/* Nothing to set, no need to walk the tree */
if (perm.mode || perm.uid != -1 || perm.gid != -1) {
perm_tree = &perm;
nftw(p, perm_cb, 20, FTW_PHYS);
}
} else if (perm_path(p, &perm, 0, O_PATH)) {
warn("Failed setting mode/owner on %s", p);
}
if (whichp("restorecon"))
systemf("restorecon %s %s", opts, p);
}
globfree(&gl);
break;
default:
+27
View File
@@ -63,3 +63,30 @@ 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
sep
say 'Mode and owner of an f entry are set on an existing file too'
run "echo keep > $DIR/file; chmod 600 $DIR/file"
run "echo 'f $DIR/file 0644 bin sys - new' > $CONF; /libexec/finit/tmpfiles --create $CONF"
assert "Mode and owner" "$(texec stat -c '%a %U:%G' $DIR/file)" = "644 bin:sys"
assert "Content is kept" "$(texec cat $DIR/file)" = "keep"
sep
say 'Mode and owner of p, C, z, and Z entries'
run "mkdir -p $DIR/tree/sub; touch $DIR/tree/sub/file"
run "cat > $CONF" <<CONF
p $DIR/fifo 0660 bin sys
C $DIR/copy 0600 bin sys - /etc/group
z $DIR/file 0600 sys bin
Z $DIR/tree - bin sys
CONF
run "umask 077; /libexec/finit/tmpfiles --create $CONF"
assert "p" "$(texec stat -c '%a %U:%G' $DIR/fifo)" = "660 bin:sys"
assert "C" "$(texec stat -c '%a %U:%G' $DIR/copy)" = "600 bin:sys"
assert "z" "$(texec stat -c '%a %U:%G' $DIR/file)" = "600 sys:bin"
assert "Z" "$(texec stat -c '%U:%G' $DIR/tree/sub/file)" = "bin:sys"
sep
say 'A p entry leaves a path that is not a fifo alone'
run "echo 'p $DIR/file 0644 root root' > $CONF; /libexec/finit/tmpfiles --create $CONF"
assert "Not a fifo" "$(texec stat -c '%a %U:%G' $DIR/file)" = "600 sys:bin"