2019-11-13 21:32:24 +03:00
#!/bin/bash
set -ex
set -o pipefail
systemd-analyze log-level debug
runas( ) {
declare userid = $1
shift
2020-01-03 20:25:51 +03:00
su " $userid " -s /bin/sh -c 'XDG_RUNTIME_DIR=/run/user/$UID exec "$@"' -- sh " $@ "
2019-11-13 21:32:24 +03:00
}
2020-01-03 20:27:14 +03:00
runas testuser systemctl --user --wait is-system-running
2019-11-13 21:32:24 +03:00
2020-01-03 20:27:14 +03:00
runas testuser systemd-run --user --unit= test-private-users \
2019-11-13 21:32:24 +03:00
-p PrivateUsers = yes -P echo hello
2020-01-03 20:27:14 +03:00
runas testuser systemd-run --user --unit= test-private-tmp-innerfile \
2019-11-13 21:32:24 +03:00
-p PrivateUsers = yes -p PrivateTmp = yes \
-P touch /tmp/innerfile.txt
# File should not exist outside the job's tmp directory.
test ! -e /tmp/innerfile.txt
touch /tmp/outerfile.txt
# File should not appear in unit's private tmp.
2020-01-03 20:27:14 +03:00
runas testuser systemd-run --user --unit= test-private-tmp-outerfile \
2019-11-13 21:32:24 +03:00
-p PrivateUsers = yes -p PrivateTmp = yes \
-P test ! -e /tmp/outerfile.txt
# Confirm that creating a file in home works
2020-01-03 20:27:14 +03:00
runas testuser systemd-run --user --unit= test-unprotected-home \
-P touch /home/testuser/works.txt
test -e /home/testuser/works.txt
2019-11-13 21:32:24 +03:00
# Confirm that creating a file in home is blocked under read-only
2020-01-03 20:27:14 +03:00
runas testuser systemd-run --user --unit= test-protect-home-read-only \
2019-11-13 21:32:24 +03:00
-p PrivateUsers = yes -p ProtectHome = read-only \
-P bash -c '
2020-01-03 20:27:14 +03:00
test -e /home/testuser/works.txt
! touch /home/testuser/blocked.txt
2019-11-13 21:32:24 +03:00
'
2020-01-03 20:27:14 +03:00
test ! -e /home/testuser/blocked.txt
2019-11-13 21:32:24 +03:00
# Check that tmpfs hides the whole directory
2020-01-03 20:27:14 +03:00
runas testuser systemd-run --user --unit= test-protect-home-tmpfs \
2019-11-13 21:32:24 +03:00
-p PrivateUsers = yes -p ProtectHome = tmpfs \
2020-01-03 20:27:14 +03:00
-P test ! -e /home/testuser
2019-11-13 21:32:24 +03:00
2019-11-20 01:24:52 +03:00
# Confirm that home, /root, and /run/user are inaccessible under "yes"
2020-01-03 20:27:14 +03:00
runas testuser systemd-run --user --unit= test-protect-home-yes \
2019-11-20 01:24:52 +03:00
-p PrivateUsers = yes -p ProtectHome = yes \
-P bash -c '
test " $( stat -c %a /home) " = "0"
test " $( stat -c %a /root) " = "0"
test " $( stat -c %a /run/user) " = "0"
'
2019-11-13 21:32:24 +03:00
# Confirm we cannot change groups because we only have one mapping in the user
# namespace (no CAP_SETGID in the parent namespace to write the additional
# mapping of the user supplied group and thus cannot change groups to an
# unmapped group ID)
2020-01-03 20:27:14 +03:00
! runas testuser systemd-run --user --unit= test-group-fail \
2019-11-13 21:32:24 +03:00
-p PrivateUsers = yes -p Group = daemon \
-P true
systemd-analyze log-level info
echo OK > /testok
exit 0