tmpfiles: keep the mode of an existing directory when none is given

A d entry without a mode reset an existing directory to 0755.  On
finix a bare "d /tmp" line took the sticky bit and the write
permission for others off the /tmp mount, so nobody but root could
create files there:

    mkdir -p /tmp/t && chmod 1777 /tmp/t
    echo 'd /tmp/t' > t.conf
    tmpfiles --create t.conf
    stat -c %a /tmp/t

This gave 755.  Commit 9bfbdc6b made the chmod of an existing
directory work, and it used the 0755 default for a new directory as
the mode to set.

The default is now only for creating the directory.  With no mode in
the entry, dirperm() leaves the mode alone.

Fixes #523

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-20 11:03:15 +01:00
1 parent 73ebc7fb36
commit 94bdccd200
4 files changed
+45 -5

No files matched your search

+3 -2
View File
@@ -233,8 +233,9 @@ All relevant changes are documented in this file.
- Fix misspelled `SIGUNKOWN` returned by `sig_name()` for unknown signal - Fix misspelled `SIGUNKOWN` returned by `sig_name()` for unknown signal
numbers, now spelled correctly as `SIGUNKNOWN`. Surfaced by the new restart numbers, now spelled correctly as `SIGUNKNOWN`. Surfaced by the new restart
log above log above
- Fix mode of existing `d`/`D` directories in tmpfiles, the chmod branch - Fix mode of existing `d`/`D` directories in tmpfiles when the entry gives
never ran because the directory creation swallowed `EEXIST`. Mode and owner one, the chmod branch never ran because the directory creation swallowed
`EEXIST`. Mode and owner
of `d`/`D`, `e`, and `f`/`F` entries, and of the service directories of `d`/`D`, `e`, and `f`/`F` entries, and of the service directories
`mksubsysd()` creates, are now set through an open fd and only when they `mksubsysd()` creates, are now set through an open fd and only when they
differ, so an immutable directory like `/var/empty` no longer warns on every differ, so an immutable directory like `/var/empty` no longer warns on every
+1 -3
View File
@@ -558,9 +558,7 @@ static void tmpfiles(char *line)
if (gid < 0) if (gid < 0)
gid = 0; gid = 0;
if (!mode) rc = makedir(path, mode ?: 0755);
mode = 0755;
rc = makedir(path, mode);
if (!rc && dirperm(path, mode, uid, gid)) if (!rc && dirperm(path, mode, uid, gid))
warn("Failed setting mode/owner on %s", path); warn("Failed setting mode/owner on %s", path);
} }
+2
View File
@@ -81,6 +81,7 @@ EXTRA_DIST += start-stop-sysv.sh
EXTRA_DIST += start-stop-serv.sh EXTRA_DIST += start-stop-serv.sh
EXTRA_DIST += signal-service.sh EXTRA_DIST += signal-service.sh
EXTRA_DIST += testserv.sh EXTRA_DIST += testserv.sh
EXTRA_DIST += tmpfiles-perm.sh
EXTRA_DIST += unexpected-restart.sh EXTRA_DIST += unexpected-restart.sh
EXTRA_DIST += dbus-auth.sh EXTRA_DIST += dbus-auth.sh
EXTRA_DIST += dbus-authz.sh EXTRA_DIST += dbus-authz.sh
@@ -158,6 +159,7 @@ TESTS += start-stop-serv.sh
TESTS += signal-service.sh TESTS += signal-service.sh
if TESTSERV if TESTSERV
TESTS += testserv.sh TESTS += testserv.sh
TESTS += tmpfiles-perm.sh
endif endif
TESTS += unexpected-restart.sh TESTS += unexpected-restart.sh
if DBUS if DBUS
+39
View File
@@ -0,0 +1,39 @@
#!/bin/sh
# Verify that tmpfiles leaves the mode of an existing directory alone
# when the entry does not give one, issue #523
set -eu
TEST_DIR=$(dirname "$0")
DIR=/tmp/perm
CONF=/tmp/perm.conf
# Run tmpfiles with entry $1, then check mode $2 and owner $3 of $DIR
assert_perm()
{
run "echo '$1' > $CONF"
run "/libexec/finit/tmpfiles --create $CONF"
assert "Entry '$1' gives $2 $3" "$(texec stat -c '%a %U:%G' $DIR)" = "$2 $3"
}
test_teardown()
{
say "Running test teardown."
run "rm -rf $DIR $CONF"
}
# shellcheck source=/dev/null
. "$TEST_DIR/lib/setup.sh"
sep
say 'A new directory gets the default mode and owner'
assert_perm "d $DIR" 755 root:root
sep
say 'An existing directory keeps its mode when the entry gives none'
run "chmod 1777 $DIR"
assert_perm "d $DIR" 1777 root:root
assert_perm "d $DIR - - -" 1777 root:root
assert_perm "d $DIR 0750" 750 root:root
assert_perm "e $DIR" 750 root:root
assert_perm "d $DIR 1777 root root" 1777 root:root