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

+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: