summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2025-01-08 13:55:07 +0100
committerLennart Poettering <lennart@poettering.net>2025-01-08 15:27:25 +0100
commit1d5fd2e6211502fc48d5df425dd067c6b368dab3 (patch)
tree013bc8650cc0cdab67d48a82b620a171e2400cd3 /test
parentcgroup: when we fail to clean up a cgroup, let's ask PID 1 for help (diff)
downloadsystemd-1d5fd2e6211502fc48d5df425dd067c6b368dab3.tar.xz
systemd-1d5fd2e6211502fc48d5df425dd067c6b368dab3.zip
test: add testcase that verifies we can safely delete subcgroups owned by other users if we own the parent
This is a test for the previous commits: we create an unpriv, delegated cgroup in --user mode, then create a subcgroup that is owned by some other user (to mimic the case where an unpriv user got a userns with delegated UIDs assigned), and then try to stop the unit. traditionally this would fail, because our unpriv systemd --user instance can't remove the subcrroup owned by someone else. With the earlier patches this is addressed.
Diffstat (limited to 'test')
-rwxr-xr-xtest/units/TEST-19-CGROUP.delegate.sh34
1 files changed, 34 insertions, 0 deletions
diff --git a/test/units/TEST-19-CGROUP.delegate.sh b/test/units/TEST-19-CGROUP.delegate.sh
index 7f3a7059e2..986b9ac2f6 100755
--- a/test/units/TEST-19-CGROUP.delegate.sh
+++ b/test/units/TEST-19-CGROUP.delegate.sh
@@ -1,5 +1,6 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: LGPL-2.1-or-later
+# shellcheck disable=SC2235
set -eux
set -o pipefail
@@ -87,6 +88,39 @@ testcase_scope_unpriv_delegation() {
-w /sys/fs/cgroup/workload.slice/test-workload0.scope/cgroup.subtree_control
}
+testcase_user_unpriv_delegation() {
+ # Check that delegation works for unpriv users, and that we can insert a
+ # subcgroup owned by a different user (which can happen in case unpriv
+ # userns where a UID range was delegated), which is still cleaned up
+ # correctly when it goes down.
+
+ run0 -u testuser systemd-run --user \
+ --property="Delegate=yes" \
+ --unit=test-chown-subcgroup \
+ --service-type=exec \
+ sleep infinity
+
+ TESTUID=$(id -u testuser)
+ CGROUP="/sys/fs/cgroup/user.slice/user-$TESTUID.slice/user@$TESTUID.service/app.slice/test-chown-subcgroup.service"
+ test -d "$CGROUP"
+
+ # Create a subcgroup, and make it owned by some unrelated user
+ SUBCGROUP="$CGROUP/subcgroup"
+ mkdir "$SUBCGROUP"
+ chown 1:1 "$SUBCGROUP"
+
+ # Make sure the subcgroup is not empty (empty dirs owned by other users can
+ # be removed if one owns the dir they are contained in, after all)
+ mkdir "$SUBCGROUP"/filler
+
+ run0 -u testuser systemctl stop --user test-chown-subcgroup.service
+
+ # Verify that the subcgroup got correctly removed
+ (! test -e "$CGROUP")
+
+ systemctl stop user@testuser.service
+}
+
testcase_subgroup() {
# Verify that DelegateSubgroup= affects ownership correctly
unit="test-subgroup-$RANDOM.service"