tmpfiles: fix root handling and target resolution (#43031)

This commit is contained in:
Yu Watanabe
2026-07-24 21:15:11 +09:00
committed by GitHub
4 changed files with 139 additions and 20 deletions
+28
View File
@@ -5,6 +5,9 @@
set -eux
set -o pipefail
# shellcheck source=test/units/util.sh
. "$(dirname "$0")"/util.sh
rm -fr /tmp/{f,F,w}
mkdir /tmp/{f,F,w}
touch /tmp/file-owned-by-root
@@ -190,6 +193,31 @@ F /tmp/F/daemon/unsafe-symlink/exploit 0644 daemon daemon - -
EOF
test ! -e /tmp/F/daemon/unsafe-symlink/exploit
#
# 'C'
#
rm -rf /tmp/C-copy-failure-src /tmp/C-copy-failure-dst
mkdir /tmp/C-copy-failure-src /tmp/C-copy-failure-dst
printf payload >/tmp/C-copy-failure-src/file
chmod 555 /tmp/C-copy-failure-dst
(! runas nobody systemd-tmpfiles --create - <<EOF
C /tmp/C-copy-failure-dst - - - - /tmp/C-copy-failure-src
EOF
)
test ! -e /tmp/C-copy-failure-dst/file
chmod 755 /tmp/C-copy-failure-dst
rm -rf /tmp/C-copy-failure-src /tmp/C-copy-failure-dst
rm -f /tmp/C-copy-link-src /tmp/C-copy-link-dst
ln -s missing-target /tmp/C-copy-link-src
systemd-tmpfiles --create - <<EOF
C /tmp/C-copy-link-dst - - - - /tmp/C-copy-link-src
EOF
test -L /tmp/C-copy-link-dst
test "$(readlink /tmp/C-copy-link-dst)" = "missing-target"
rm -f /tmp/C-copy-link-src /tmp/C-copy-link-dst
#
# 'w'
#
+31
View File
@@ -204,3 +204,34 @@ test ! -d /tmp/ageby/d3
# Cleanup the test directory (fail if not empty).
rmdir /tmp/ageby
# Removing an old child directory should restore the parent directory mtime.
rm -rf /tmp/ageby-mtime
mkdir -p /tmp/ageby-mtime/old-child
touch --date "2020-01-01 00:00:00" /tmp/ageby-mtime/old-child /tmp/ageby-mtime
before="$(stat -c %Y /tmp/ageby-mtime)"
systemd-tmpfiles --clean - <<-EOF
d /tmp/ageby-mtime - - - M:1s
EOF
after="$(stat -c %Y /tmp/ageby-mtime)"
test "$before" = "$after"
test ! -e /tmp/ageby-mtime/old-child
rmdir /tmp/ageby-mtime
# X entries inherit the parent cleanup age and its age-by fields.
rm -rf /tmp/ageby-x-inherit
mkdir -p /tmp/ageby-x-inherit/parent/child /tmp/ageby-x-inherit/parent/other
printf old >/tmp/ageby-x-inherit/parent/child/file
printf old >/tmp/ageby-x-inherit/parent/other/file
touch --date "3 days ago" /tmp/ageby-x-inherit/parent/child/file /tmp/ageby-x-inherit/parent/other/file
systemd-tmpfiles --clean - <<-EOF
d /tmp/ageby-x-inherit/parent - - - m:1d
X /tmp/ageby-x-inherit/parent/child - - - -
EOF
test ! -e /tmp/ageby-x-inherit/parent/child/file
test ! -e /tmp/ageby-x-inherit/parent/other/file
rm -rf /tmp/ageby-x-inherit
+45
View File
@@ -42,3 +42,48 @@ systemd-tmpfiles --create --root="$root" - <<EOF
L %h - - - -
EOF
test "$(readlink "$dst")" = "$src"
# Check that directory specifiers are not prefixed with --root twice.
root='/tmp/L/3'
rm -rf "$root"
mkdir -p "$root"
output="$(systemd-tmpfiles --create --dry-run --root="$root" - 2>&1 <<EOF
d %t/test - - - -
EOF
)"
[[ "$output" == *"Would create directory $root/run/test"* ]]
[[ "$output" != *"$root$root"* ]]
# Check that L? resolves relative targets from the symlink's parent directory.
root='/tmp/L/4'
rm -rf "$root"
mkdir -p "$root"
touch "$root/target"
systemd-tmpfiles --create - <<EOF
L? $root/link - - - - target
EOF
test "$(readlink "$root/link")" = "target"
rm -f "$root/link" "$root/target"
systemd-tmpfiles --create - <<EOF
L? $root/link - - - - target
EOF
test ! -e "$root/link"
# Check the same relative target lookup with --root=.
rm -rf "$root"
mkdir -p "$root/dir"
touch "$root/dir/target"
systemd-tmpfiles --create --root="$root" - <<EOF
L? /dir/link - - - - target
EOF
test "$(readlink "$root/dir/link")" = "target"
rm -f "$root/dir/link" "$root/dir/target"
systemd-tmpfiles --create --root="$root" - <<EOF
L? /dir/link - - - - target
EOF
test ! -e "$root/dir/link"