2020-03-04 09:35:06 +00:00
#!/usr/bin/env bash
2019-11-13 10:32:24 -08:00
set -ex
set -o pipefail
systemd-analyze log-level debug
runas( ) {
declare userid = $1
shift
2020-01-03 18:25:51 +01:00
su " $userid " -s /bin/sh -c 'XDG_RUNTIME_DIR=/run/user/$UID exec "$@"' -- sh " $@ "
2019-11-13 10:32:24 -08:00
}
2020-05-26 12:57:29 +02:00
runas testuser systemd-run --wait --user --unit= test-private-users \
2019-11-13 10:32:24 -08:00
-p PrivateUsers = yes -P echo hello
2020-05-26 12:57:29 +02:00
runas testuser systemd-run --wait --user --unit= test-private-tmp-innerfile \
2019-11-13 10:32:24 -08: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-05-26 12:57:29 +02:00
runas testuser systemd-run --wait --user --unit= test-private-tmp-outerfile \
2019-11-13 10:32:24 -08:00
-p PrivateUsers = yes -p PrivateTmp = yes \
-P test ! -e /tmp/outerfile.txt
# Confirm that creating a file in home works
2020-05-26 12:57:29 +02:00
runas testuser systemd-run --wait --user --unit= test-unprotected-home \
2020-01-03 18:27:14 +01:00
-P touch /home/testuser/works.txt
test -e /home/testuser/works.txt
2019-11-13 10:32:24 -08:00
# Confirm that creating a file in home is blocked under read-only
2020-05-26 12:57:29 +02:00
runas testuser systemd-run --wait --user --unit= test-protect-home-read-only \
2019-11-13 10:32:24 -08:00
-p PrivateUsers = yes -p ProtectHome = read-only \
-P bash -c '
2020-01-03 18:27:14 +01:00
test -e /home/testuser/works.txt
! touch /home/testuser/blocked.txt
2019-11-13 10:32:24 -08:00
'
2020-01-03 18:27:14 +01:00
test ! -e /home/testuser/blocked.txt
2019-11-13 10:32:24 -08:00
# Check that tmpfs hides the whole directory
2020-05-26 12:57:29 +02:00
runas testuser systemd-run --wait --user --unit= test-protect-home-tmpfs \
2019-11-13 10:32:24 -08:00
-p PrivateUsers = yes -p ProtectHome = tmpfs \
2020-01-03 18:27:14 +01:00
-P test ! -e /home/testuser
2019-11-13 10:32:24 -08:00
2019-11-19 14:24:52 -08:00
# Confirm that home, /root, and /run/user are inaccessible under "yes"
2020-05-26 12:57:29 +02:00
runas testuser systemd-run --wait --user --unit= test-protect-home-yes \
2019-11-19 14:24:52 -08: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 10:32:24 -08: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-05-26 12:57:29 +02:00
! runas testuser systemd-run --wait --user --unit= test-group-fail \
2019-11-13 10:32:24 -08:00
-p PrivateUsers = yes -p Group = daemon \
-P true
systemd-analyze log-level info
echo OK > /testok
exit 0