2020-03-04 12:35:06 +03:00
#!/usr/bin/env bash
2021-04-09 20:39:41 +03:00
set -eux
2019-11-13 21:32:24 +03:00
set -o pipefail
systemd-analyze log-level debug
runas( ) {
declare userid = $1
shift
2021-04-09 20:56:12 +03:00
# shellcheck disable=SC2016
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-05-26 13:57:29 +03:00
runas testuser systemd-run --wait --user --unit= test-private-users \
2019-11-13 21:32:24 +03:00
-p PrivateUsers = yes -P echo hello
2020-05-26 13:57:29 +03:00
runas testuser systemd-run --wait --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-05-26 13:57:29 +03:00
runas testuser systemd-run --wait --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-05-26 13:57:29 +03:00
runas testuser systemd-run --wait --user --unit= test-unprotected-home \
2020-01-03 20:27:14 +03:00
-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-05-26 13:57:29 +03:00
runas testuser systemd-run --wait --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 '
2021-04-08 10:45:28 +03:00
test -e /home/testuser/works.txt || exit 10
touch /home/testuser/blocked.txt && exit 11
' \
&& { echo 'unexpected success' ; exit 1; }
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-05-26 13:57:29 +03:00
runas testuser systemd-run --wait --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"
2021-04-09 20:56:12 +03:00
# shellcheck disable=SC2016
2020-05-26 13:57:29 +03:00
runas testuser systemd-run --wait --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)
2021-04-08 10:45:28 +03:00
runas testuser systemd-run --wait --user --unit= test-group-fail \
2019-11-13 21:32:24 +03:00
-p PrivateUsers = yes -p Group = daemon \
2021-04-08 10:45:28 +03:00
-P true \
&& { echo 'unexpected success' ; exit 1; }
2019-11-13 21:32:24 +03:00
systemd-analyze log-level info
2021-04-08 01:09:55 +03:00
echo OK >/testok
2019-11-13 21:32:24 +03:00
exit 0