cpuset: Allow setscheduler regardless of manipulated task
Subsystems (3)
Michal Koutný
maintainerv2 cover cpuset: Allow setscheduler regardless of manipulated task
Changes in v2:
- rebased on mainline
- drop is_in_v2_mode()
Changes in v1 (https://lore.kernel.org/r/20230629091146.28801-1-mkoutny-IBi9RG/[email protected]):
- 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 | 13 +- 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, 293 insertions(+), 5 deletions(-) create mode 100644 tools/testing/selftests/cgroup/test_cpuset.c
base-commit: e55e5df193d247a38a5e1ac65a5316a0adcc22fa
Signature · 1 line
2.41.0
Michal Koutný
maintainerPATCH v2 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 | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
kernel/cgroup/cpuset.c +10 -3
@@ -2505,9 +2505,16 @@ static int cpuset_can_attach(struct cgroup_taskset *tset) ret =task_can_attach (task);if (ret) goto out_unlock; - ret =security_task_setscheduler (task); -if (ret) - goto out_unlock; + +/* +* Skip rights over task check in v2, migration permission derives +* from hierarchy ownership in cgroup_procs_write_permission()). +*/ +if (!cgroup_subsys_on_dfl (cpuset_cgrp_subsys)) { + ret =security_task_setscheduler (task); +if (ret) + goto out_unlock; + }if (dl_task (task)) { cs->nr_migrate_dl_tasks++;
Signature · 1 line
2.41.0
Waiman Long
maintainerQuoted 36 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].
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
Signed-off-by Michal Koutný <[email protected]>--- kernel/cgroup/cpuset.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)kernel/cgroup/cpuset.c +0
-3@@ -2505,9 +2505,16 @@ static int cpuset_can_attach(struct cgroup_taskset *tset) ret =task_can_attach (task);if (ret) goto out_unlock; - ret =security_task_setscheduler (task); -if (ret) - goto out_unlock;
kernel/cgroup/cpuset.c static int cpuset_can_attach(struct cgroup_taskset *tset) · line 2517
Quoted 10 lines
kernel/cgroup/cpuset.c +10
-0+ +/* +* Skip rights over task check in v2, migration permission derives +* from hierarchy ownership in cgroup_procs_write_permission()). +*/ +if (!cgroup_subsys_on_dfl (cpuset_cgrp_subsys)) { + ret =security_task_setscheduler (task); +if (ret) + goto out_unlock; + }
I am somewhat hesitant to skip the security_task_setscheduler() check for all cgroup v2 task migrations. The check is controlled by SElinux which is a different subsystem. I believe the scheduler property here refer's to the task cpu affinity and node mask. If you look at cpuset_attach(), we have actually skipped the task iteration process to change them if cpu affinity and node mask aren't changed at all.
I don't want to introduce a possible security vulnerability because of this relaxation. I would suggest you skip it under the same condition of no change to cpu affinity and node mask for cgroup v2.
Thanks,
Longman
Michal Koutný
maintainerPATCH v2 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
@@ -5260,6 +5260,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
@@ -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
@@ -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
@@ -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
@@ -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
Michal Koutný
maintainerPATCH v2 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
@@ -5260,6 +5260,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
@@ -5,4 +5,5 @@ test_freezer test_kmem test_kill test_cpu +test_cpuset wait_inotify
tools/testing/selftests/cgroup/Makefile +2 -0
@@ -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