Skip to contentSkip to conversation

cpuset: Allow setscheduler regardless of manipulated task

PATCH Superseded Michal Koutný 2 participants 8 messages Jun 29, 2023 – Jun 30, 2023

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Michal Koutný

author maintainer
#0 lore raw

cover cpuset: Allow setscheduler regardless of manipulated task

Changes in v1:
- added selftests
- comments rewording

RFC in https://lore.kernel.org/r/20220623124944.2753-1-mkoutny-IBi9RG/[email protected]

Michal Koutný (3):
cpuset: Allow setscheduler regardless of manipulated task
selftests: cgroup: Minor code reorganizations
selftests: cgroup: Add cpuset migrations testcase

 MAINTAINERS                                   |   2 +
 kernel/cgroup/cpuset.c                        |   7 +
 tools/testing/selftests/cgroup/.gitignore     |   1 +
 tools/testing/selftests/cgroup/Makefile       |   2 +
 tools/testing/selftests/cgroup/cgroup_util.c  |   2 +
 tools/testing/selftests/cgroup/cgroup_util.h  |   2 +
 tools/testing/selftests/cgroup/test_core.c    |   2 +-
 tools/testing/selftests/cgroup/test_cpuset.c  | 272 ++++++++++++++++++
 .../selftests/cgroup/test_cpuset_prs.sh       |   2 +-
 9 files changed, 290 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/cgroup/test_cpuset.c

base-commit: 6995e2de6891c724bfeb2db33d7b87775f913ad1

Signature · 1 line
2.41.0
author maintainer
#1 lore raw

PATCH 1/3 cpuset: Allow setscheduler regardless of manipulated task

When we migrate a task between two cgroups, one of the checks is a verification whether we can modify task's scheduler settings (cap_task_setscheduler()).

An implicit migration occurs also when enabling a controller on the unified hierarchy (think of parent to child migration). The aforementioned check may be problematic if the caller of the migration (enabling a controller) has no permissions over migrated tasks. For instance, a user's cgroup that ends up running a process of a different user. Although cgroup permissions are configured favorably, the enablement fails due to the foreign process [1].

Change the behavior by relaxing the permissions check on the unified hierarchy (or in v2 mode). This is in accordance with unified hierarchy attachment behavior when permissions of the source to target cgroups are decisive whereas the migrated task is opaque (as opposed to more restrictive check in __cgroup1_procs_write()).

[1] https://github.com/systemd/systemd/issues/18293#issuecomment-831205649

---
 kernel/cgroup/cpuset.c | 7 +++++++
 1 file changed, 7 insertions(+)
kernel/cgroup/cpuset.c +7 -0
index e4ca2dd2b764..3b5f87a9a150 100644
@@ -2495,6 +2495,13 @@ static int cpuset_can_attach(struct cgroup_taskset *tset)
 		ret = task_can_attach(task, cs->effective_cpus);
 		if (ret)
 			goto out_unlock;
+
+		/*
+		 * Skip rights over task check in v2, migration permission derives
+		 * from hierarchy ownership in cgroup_procs_write_permission()).
+		 */
+		if (is_in_v2_mode())
+			continue;
 		ret = security_task_setscheduler(task);
 		if (ret)
 			goto out_unlock;
Signature · 1 line
2.41.0
author maintainer
#6 lore raw

PATCH 2/3 selftests: cgroup: Minor code reorganizations

In reply to Michal Koutný

No functional change intended, these small changes are merged into one commit and they serve as a preparation for an upcoming new testcase.

---
 MAINTAINERS                                       | 1 +
 tools/testing/selftests/cgroup/cgroup_util.c      | 2 ++
 tools/testing/selftests/cgroup/cgroup_util.h      | 2 ++
 tools/testing/selftests/cgroup/test_core.c        | 2 +-
 tools/testing/selftests/cgroup/test_cpuset_prs.sh | 2 +-
 5 files changed, 7 insertions(+), 2 deletions(-)
MAINTAINERS +1 -0
index 35e19594640d..50e7056b4383 100644
@@ -5259,6 +5259,7 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup.git
 F:	Documentation/admin-guide/cgroup-v1/cpusets.rst
 F:	include/linux/cpuset.h
 F:	kernel/cgroup/cpuset.c
+F:	tools/testing/selftests/cgroup/test_cpuset_prs.sh
 
 CONTROL GROUP - MEMORY RESOURCE CONTROLLER (MEMCG)
 M:	Johannes Weiner <[email protected]>
tools/testing/selftests/cgroup/cgroup_util.c +2 -0
index e8bbbdb77e0d..0340d4ca8f51 100644
@@ -286,6 +286,8 @@ int cg_destroy(const char *cgroup)
 {
 	int ret;
 
+	if (!cgroup)
+		return 0;
 retry:
 	ret = rmdir(cgroup);
 	if (ret && errno == EBUSY) {
tools/testing/selftests/cgroup/cgroup_util.h +2 -0
index c92df4e5d395..1df7f202214a 100644
@@ -11,6 +11,8 @@
 #define USEC_PER_SEC	1000000L
 #define NSEC_PER_SEC	1000000000L
 
+#define TEST_UID	65534 /* usually nobody, any !root is fine */
+
 /*
  * Checks if two given values differ by less than err% of their sum.
  */
tools/testing/selftests/cgroup/test_core.c +1 -1
index 600123503063..80aa6b2373b9 100644
@@ -683,7 +683,7 @@ static int test_cgcore_thread_migration(const char *root)
  */
 static int test_cgcore_lesser_euid_open(const char *root)
 {
-	const uid_t test_euid = 65534;	/* usually nobody, any !root is fine */
+	const uid_t test_euid = TEST_UID;
 	int ret = KSFT_FAIL;
 	char *cg_test_a = NULL, *cg_test_b = NULL;
 	char *cg_test_a_procs = NULL, *cg_test_b_procs = NULL;
tools/testing/selftests/cgroup/test_cpuset_prs.sh +1 -1
index 2b5215cc599f..4afb132e4e4f 100755
@@ -10,7 +10,7 @@
 skip_test() {
 	echo "$1"
 	echo "Test SKIPPED"
-	exit 0
+	exit 4 # ksft_skip
 }
 
 [[ $(id -u) -eq 0 ]] || skip_test "Test must be run as root!"
Signature · 1 line
2.41.0
author maintainer
#7 lore raw

PATCH 3/3 selftests: cgroup: Add cpuset migrations testcase

In reply to Michal Koutný

Add a separate testfile to verify treating permissions when tasks are migrated on cgroup v2 hierarchy between cpuset cgroups.

In accordance with v2 design, migration should be allowed based on delegation boundaries (i.e. cgroup.procs permissions) and does not depend on the migrated object (i.e. unprivileged process can migrate another process (even privileged) as long as it remains in the original dedicated scope).

---
 MAINTAINERS                                  |   1 +
 tools/testing/selftests/cgroup/.gitignore    |   1 +
 tools/testing/selftests/cgroup/Makefile      |   2 +
 tools/testing/selftests/cgroup/test_cpuset.c | 272 +++++++++++++++++++
 4 files changed, 276 insertions(+)
 create mode 100644 tools/testing/selftests/cgroup/test_cpuset.c
MAINTAINERS +1 -0
index 50e7056b4383..51a31aab077f 100644
@@ -5259,6 +5259,7 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup.git
 F:	Documentation/admin-guide/cgroup-v1/cpusets.rst
 F:	include/linux/cpuset.h
 F:	kernel/cgroup/cpuset.c
+F:	tools/testing/selftests/cgroup/test_cpuset.c
 F:	tools/testing/selftests/cgroup/test_cpuset_prs.sh
 
 CONTROL GROUP - MEMORY RESOURCE CONTROLLER (MEMCG)
tools/testing/selftests/cgroup/.gitignore +1 -0
index c4a57e69f749..8443a8d46a1c 100644
@@ -5,4 +5,5 @@ test_freezer
 test_kmem
 test_kill
 test_cpu
+test_cpuset
 wait_inotify
tools/testing/selftests/cgroup/Makefile +2 -0
index 3d263747d2ad..dee0f013c7f4 100644
@@ -12,6 +12,7 @@ TEST_GEN_PROGS += test_core
 TEST_GEN_PROGS += test_freezer
 TEST_GEN_PROGS += test_kill
 TEST_GEN_PROGS += test_cpu
+TEST_GEN_PROGS += test_cpuset
 
 LOCAL_HDRS += $(selfdir)/clone3/clone3_selftests.h $(selfdir)/pidfd/pidfd.h
 
@@ -23,3 +24,4 @@ $(OUTPUT)/test_core: cgroup_util.c
 $(OUTPUT)/test_freezer: cgroup_util.c
 $(OUTPUT)/test_kill: cgroup_util.c
 $(OUTPUT)/test_cpu: cgroup_util.c
+$(OUTPUT)/test_cpuset: cgroup_util.c
tools/testing/selftests/cgroup/test_cpuset.c new file +272 -0

272 lines not on this page · raw message

Signature · 1 line
2.41.0
maintainer
#2 lore raw
In reply to Michal Koutný
Quoted 12 lines · Michal Koutný

When we migrate a task between two cgroups, one of the checks is a verification whether we can modify task's scheduler settings (cap_task_setscheduler()).

An implicit migration occurs also when enabling a controller on the unified hierarchy (think of parent to child migration). The aforementioned check may be problematic if the caller of the migration (enabling a controller) has no permissions over migrated tasks. For instance, a user's cgroup that ends up running a process of a different user. Although cgroup permissions are configured favorably, the enablement fails due to the foreign process [1].

Quoted 5 lines

Change the behavior by relaxing the permissions check on the unified hierarchy (or in v2 mode). This is in accordance with unified hierarchy attachment behavior when permissions of the source to target cgroups are decisive whereas the migrated task is opaque (as opposed to more restrictive check in __cgroup1_procs_write()).

The is_in_v2_mode() check is for supporting the v2 mode in cgroup v1. However, there is no controller enabling in v1. So I think you should just use cgroup_subsys_on_dfl(cpuset_cgrp_subsys) as the v2 check if your focus is just to prevent problem when enabling cpuset controller.

Quoted 16 lines

[1] https://github.com/systemd/systemd/issues/18293#issuecomment-831205649

---
  kernel/cgroup/cpuset.c | 7 +++++++
  1 file changed, 7 insertions(+)
kernel/cgroup/cpuset.c +0 -0
index e4ca2dd2b764..3b5f87a9a150 100644
@@ -2495,6 +2495,13 @@ static int cpuset_can_attach(struct cgroup_taskset *tset)
  		ret = task_can_attach(task, cs->effective_cpus);
  		if (ret)
  			goto out_unlock;

kernel/cgroup/cpuset.c static int cpuset_can_attach(struct cgroup_taskset *tset) · line 2507

Quoted 10 lines
kernel/cgroup/cpuset.c +7 -0
+
+		/*
+		 * Skip rights over task check in v2, migration permission derives
+		 * from hierarchy ownership in cgroup_procs_write_permission()).
+		 */
+		if (is_in_v2_mode())
+			continue;
  		ret = security_task_setscheduler(task);
  		if (ret)
  			goto out_unlock;

This change will likely conflict with the latest cpuset change on tracking # of dl tasks in a cpuset. You will have to, at least, move the dl task check before the security_task_setscheduler() check.

Another fact about cpuset controller enabling is that both cpus_allowed and mems_allowed are empty at that point. You may also add these checks as a preconditions for disabling the security_task_setscheduler check.

Cheers,
Longman

author maintainer
#3 lore raw
Quoted 3 lines · Waiman Long

So I think you should just use cgroup_subsys_on_dfl(cpuset_cgrp_subsys) as the v2 check if your focus is just to prevent problem when enabling cpuset controller.

I thought the bare cgroup_subsys_on_dfl(cpuset_cgrp_subsys) is not used in cpuset.c but I was wrong -- yes, I'll change this.

Quoted 7 lines

This change will likely conflict with the latest cpuset change on tracking # of dl tasks in a cpuset. You will have to, at least, move the dl task check before the security_task_setscheduler() check.

Another fact about cpuset controller enabling is that both cpus_allowed and mems_allowed are empty at that point. You may also add these checks as a preconditions for disabling the security_task_setscheduler check.

Ah, I will rebase on fresh mainline (or do you mean another reference?).

Thanks for the hints,
Michal

maintainer
#4 lore raw
Quoted 7 lines · Michal Koutný
Quoted 3 lines · Waiman Long

So I think you should just use cgroup_subsys_on_dfl(cpuset_cgrp_subsys) as the v2 check if your focus is just to prevent problem when enabling cpuset controller.

I thought the bare cgroup_subsys_on_dfl(cpuset_cgrp_subsys) is not used in cpuset.c but I was wrong -- yes, I'll change this.

Quoted 8 lines
Quoted 7 lines

This change will likely conflict with the latest cpuset change on tracking # of dl tasks in a cpuset. You will have to, at least, move the dl task check before the security_task_setscheduler() check.

Another fact about cpuset controller enabling is that both cpus_allowed and mems_allowed are empty at that point. You may also add these checks as a preconditions for disabling the security_task_setscheduler check.

Ah, I will rebase on fresh mainline (or do you mean another reference?).

Yes, those changes have just been merged into the mainline.

Cheers,
Longman

author maintainer
#5 lore raw
In reply to Waiman Long
Quoted 3 lines · Waiman Long

Another fact about cpuset controller enabling is that both cpus_allowed and mems_allowed are empty at that point. You may also add these checks as a preconditions for disabling the security_task_setscheduler check.

I considered relying on that, however, there is more generic case when migrating between two sibling that should be allowed in v2 too. See the added test_cpuset_perms_object(). (Admittedly, it doesn't stress the case when the two siblings had different CPUs but it could.)

Anyway, let's move on to v2 (where I addressed remaining comments).

Thanks,
Michal