Files
finit/test/tmpfiles-perm.sh
T
Joachim Wiberg 7ee991daf2 tmpfiles: make a new file 0644 when the f entry has no mode
A file from an f entry without a mode got what the umask left of
0666, so the result depended on who called tmpfiles:

    f /run/app/state

tmpfiles.d(5) has 0644 as the default.  Use that for a file the entry
creates.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-20 19:10:03 +01:00

123 lines
4.3 KiB
Bash
Executable File

#!/bin/sh
# Verify that tmpfiles leaves the mode and owner of an existing
# directory alone when the entry does not give them, 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 what the entry does not give'
run "chmod 1777 $DIR; chown daemon:daemon $DIR"
assert_perm "d $DIR" 1777 daemon:daemon
assert_perm "d $DIR - - -" 1777 daemon:daemon
assert_perm "d $DIR 0750" 750 daemon:daemon
assert_perm "d $DIR - bin" 750 bin:daemon
assert_perm "d $DIR - - sys" 750 bin:sys
assert_perm "e $DIR" 750 bin:sys
assert_perm "d $DIR 1777 root root" 1777 root:root
sep
say 'An unknown user skips the entry, an unknown group only the group'
run "cat > $CONF" <<CONF
d $DIR/user 0755 nosuchuser
f $DIR/file 0644 nosuchuser
d $DIR/group 0750 bin nosuchgroup
CONF
run "/libexec/finit/tmpfiles --create $CONF"
assert "Only the entry with a known user is created" "$(texec ls $DIR)" = "group"
assert "With its mode and user" "$(texec stat -c '%a %U:%G' $DIR/group)" = "750 bin:root"
sep
say 'With : mode and owner are only for a path the entry creates'
run "rm -rf $DIR; mkdir $DIR; chmod 1777 $DIR"
assert_perm "d $DIR :0700 :bin :bin" 1777 root:root
run "rmdir $DIR"
assert_perm "d $DIR :0700 :bin :bin" 700 bin:bin
sep
say 'With ~ the mode loses what no one has on the path'
run "chmod 500 $DIR"
assert_perm "e $DIR ~0777" 555 bin:bin
assert_perm "e $DIR :~0700 :sys" 555 bin:bin
sep
say 'Mode and owner of an f entry are set on an existing file too'
run "echo keep > $DIR/file; chmod 600 $DIR/file"
run "echo 'f $DIR/file 0644 bin sys - new' > $CONF; /libexec/finit/tmpfiles --create $CONF"
assert "Mode and owner" "$(texec stat -c '%a %U:%G' $DIR/file)" = "644 bin:sys"
assert "Content is kept" "$(texec cat $DIR/file)" = "keep"
sep
say 'Mode and owner of p, C, z, and Z entries'
run "mkdir -p $DIR/tree/sub; touch $DIR/tree/sub/file"
run "cat > $CONF" <<CONF
p $DIR/fifo 0660 bin sys
C $DIR/copy 0600 bin sys - /etc/group
z $DIR/file 0600 sys bin
Z $DIR/tree - bin sys
CONF
run "umask 077; /libexec/finit/tmpfiles --create $CONF"
assert "p" "$(texec stat -c '%a %U:%G' $DIR/fifo)" = "660 bin:sys"
assert "C" "$(texec stat -c '%a %U:%G' $DIR/copy)" = "600 bin:sys"
assert "z" "$(texec stat -c '%a %U:%G' $DIR/file)" = "600 sys:bin"
assert "Z" "$(texec stat -c '%U:%G' $DIR/tree/sub/file)" = "bin:sys"
sep
say 'A p entry leaves a path that is not a fifo alone'
run "echo 'p $DIR/file 0644 root root' > $CONF; /libexec/finit/tmpfiles --create $CONF"
assert "Not a fifo" "$(texec stat -c '%a %U:%G' $DIR/file)" = "600 sys:bin"
sep
say 'Setuid survives a change of owner, and ~ can take every bit'
run "touch $DIR/suid $DIR/none; chmod 4755 $DIR/suid; chmod 111 $DIR/none"
run "cat > $CONF" <<CONF
f $DIR/suid 4755 bin sys
f $DIR/keep - sys bin
f $DIR/none ~0600
CONF
run "touch $DIR/keep; chmod 2755 $DIR/keep; /libexec/finit/tmpfiles --create $CONF"
assert "Mode given" "$(texec stat -c '%a %U:%G' $DIR/suid)" = "4755 bin:sys"
assert "Mode not given" "$(texec stat -c '%a %U:%G' $DIR/keep)" = "2755 sys:bin"
assert "Masked to 0" "$(texec stat -c '%a' $DIR/none)" = "0"
sep
say 'v, q, and Q entries make a plain directory, like d'
run "cat > $CONF" <<CONF
v $DIR/v 0750 bin sys
q $DIR/q 0700 sys bin
Q $DIR/Q
CONF
run "/libexec/finit/tmpfiles --create $CONF"
assert "v" "$(texec stat -c '%F %a %U:%G' $DIR/v)" = "directory 750 bin:sys"
assert "q" "$(texec stat -c '%F %a %U:%G' $DIR/q)" = "directory 700 sys:bin"
assert "Q" "$(texec stat -c '%F %a %U:%G' $DIR/Q)" = "directory 755 root:root"
sep
say 'A new file without a mode is 0644, whatever the umask'
run "echo 'f $DIR/default' > $CONF; umask 077; /libexec/finit/tmpfiles --create $CONF"
assert "Default mode" "$(texec stat -c '%a' $DIR/default)" = "644"