Files
finit/test/tmpfiles-root.sh
T
Joachim Wiberg a629c8add3 tmpfiles: skip a C entry with a relative path to copy from
The argument of a C entry was used as it is, so a relative one was
copied from the directory tmpfiles happened to be started in.  With
--root that directory is outside the new root, and

    C /etc/copy - - - - ../../../etc/shadow

in a file below the root copied a file of the running system into it.

systemd skips a C line when the path to copy from is not absolute.
Do the same, with a warning.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-21 19:23:51 +01:00

66 lines
2.1 KiB
Bash
Executable File

#!/bin/sh
# Verify tmpfiles --root, which runs on the system below a directory:
# its tmpfiles.d files, its users, and its paths
set -eu
TEST_DIR=$(dirname "$0")
ROOT=/tmp/root
CONF=/tmp/root.conf
assert_stat()
{
assert "$1 is $2" "$(texec stat -c '%a %u %g' "$ROOT$1")" = "$2"
}
test_teardown()
{
say "Running test teardown."
run "rm -rf $ROOT $CONF"
}
# shellcheck source=/dev/null
. "$TEST_DIR/lib/setup.sh"
run "mkdir -p $ROOT/etc/tmpfiles.d $ROOT/usr/share/factory/etc"
run "echo 'app:x:1:1::/:/bin/sh' > $ROOT/etc/passwd"
run "echo 'grp:x:2:' > $ROOT/etc/group"
run "echo factory > $ROOT/usr/share/factory/etc/issue"
run "cat > $ROOT/etc/tmpfiles.d/root.conf" <<CONF
d /var/lib/root-test 0750 app grp
f /var/lib/root-test/file 0600 - - - inside
f /var/lib/root-test/other 0600
z /var/lib/root-test/other 0644
L /link - - - - /var/lib/root-test
f /link/through 0640
C /etc/issue
C /etc/outside - - - - ../../../etc/passwd
d /skipped 0700 daemon
CONF
run "echo 'd /from/cmdline 0711' > $CONF"
# The leak check of a sanitizer build needs /proc, the root has none
tmpfiles()
{
run "ASAN_OPTIONS=detect_leaks=0 /libexec/finit/tmpfiles --create --root=$ROOT $*"
}
tmpfiles
say 'The tmpfiles.d files, users, and groups are those below the root'
assert_stat /var/lib/root-test "750 1 2"
assert "File content" "$(texec cat $ROOT/var/lib/root-test/file)" = "inside"
assert "No user daemon there" "$(texec sh -c "ls $ROOT | grep -c skipped")" = "0"
sep
say 'So are the paths, also what a link points to'
assert_stat /var/lib/root-test/other "644 0 0"
assert_stat /var/lib/root-test/through "640 0 0"
assert "Copied from its factory" "$(texec cat $ROOT/etc/issue)" = "factory"
assert "No copy from outside it" "$(texec sh -c "ls $ROOT/etc | grep -c outside")" = "0"
assert "Nothing outside it" "$(texec sh -c "ls /var/lib | grep -c root-test")" = "0"
sep
say 'A file on the command line is not below the root'
tmpfiles $CONF
assert_stat /from/cmdline "711 0 0"