From a629c8add398e71a91eecbc45fe445fae30b033f Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Sat, 21 Nov 2026 19:23:47 +0100 Subject: [PATCH] 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 --- doc/ChangeLog.md | 3 +++ src/tmpfiles.c | 4 ++++ test/tmpfiles-root.sh | 2 ++ 3 files changed, 9 insertions(+) diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index 547abc58..3eeb8933 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -375,6 +375,9 @@ All relevant changes are documented in this file. - Fix tmpfiles entries with a relative path, they were run on whatever directory `tmpfiles` was started in. As in tmpfiles.d(5), such an entry is now skipped with a warning +- Fix tmpfiles `C` entries with a relative path to copy from, it was + taken from whatever directory `tmpfiles` was started in. As in + tmpfiles.d(5), such an entry is now skipped with a warning - Fix logins refused after boot on a system with systemd's tmpfiles.d files installed. They create `/run/nologin` at boot, and on systemd it is removed by systemd-user-sessions. Finit now removes it when leaving diff --git a/src/tmpfiles.c b/src/tmpfiles.c index 7c17d98a..c03d9013 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -2001,6 +2001,10 @@ static int parse(char *line) return -1; } } + if (e.type[0] == 'C' && e.arg && e.arg[0] != '/') { + warnx("Path to copy from is not absolute, skipping %s", e.path); + return -1; + } if (e.path == pathbuf) e.path = xpath = strdup(pathbuf); diff --git a/test/tmpfiles-root.sh b/test/tmpfiles-root.sh index 10c950b1..57e44089 100755 --- a/test/tmpfiles-root.sh +++ b/test/tmpfiles-root.sh @@ -34,6 +34,7 @@ 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" @@ -55,6 +56,7 @@ 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