]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: add testcase that verifies we can safely delete subcgroups owned by other users... 35922/head
authorLennart Poettering <lennart@poettering.net>
Wed, 8 Jan 2025 12:55:07 +0000 (13:55 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 8 Jan 2025 14:27:25 +0000 (15:27 +0100)
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.

test/units/TEST-19-CGROUP.delegate.sh

index 7f3a7059e2cd61c77b9ee133e362bd0498b8bf4d..986b9ac2f64b03f654302bcdc7168df39a0b5981 100755 (executable)
@@ -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"