cgroup: "cgroup.subtree_control" should be writeable by delegatee
Subsystems (2)
Tejun Heo
maintainerPATCH 1/3 cgroup: "cgroup.subtree_control" should be writeable by delegatee
"cgroup.subtree_control" determines which resource types a cgroup wants to control. Unlike actual resource knobs, this is an attribute which belongs to the cgroup itself instead of its parent and thus should be writeable by the delegatee in a delegated cgroup.
Update delegation documentation accordingly.
--- Documentation/cgroup-v2.txt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
Documentation/cgroup-v2.txt +5 -4
@@ -309,10 +309,11 @@ file. 2-5-1. Model of Delegation A cgroup can be delegated to a less privileged user by granting write -access of the directory and its "cgroup.procs" file to the user. Note -that resource control interface files in a given directory control the -distribution of the parent's resources and thus must not be delegated -along with the directory. +access of the directory and its "cgroup.procs" and +"cgroup.subtree_control" files to the user. Note that resource +control interface files in a given directory control the distribution +of the parent's resources and thus must not be delegated along with +the directory. Once delegated, the user can build sub-hierarchy under the directory, organize processes as it sees fit and further distribute the resources
Tejun Heo
maintainerPATCH 2/3 cgroup: restructure cgroup_procs_write_permission()
Restructure cgroup_procs_write_permission() to make extending permission logic easier.
This patch doesn't cause any functional changes.
--- kernel/cgroup/cgroup.c | 57 ++++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 24 deletions(-)
kernel/cgroup/cgroup.c +33 -24
@@ -2363,27 +2363,12 @@ static int cgroup_procs_write_permissionstruct cgroup *dst_cgrp, struct kernfs_open_file *of) { - int ret = 0; + struct super_block *sb = of->file->f_path.dentry->d_sb; +struct cgroup *src_cgrp, *com_cgrp; +struct inode *inode; +int ret; -if (cgroup_on_dfl (dst_cgrp)) { -struct super_block *sb = of->file->f_path.dentry->d_sb; -struct cgroup *cgrp; -struct inode *inode; - -spin_lock_irq (&css_set_lock); - cgrp =task_cgroup_from_root (task, &cgrp_dfl_root); -spin_unlock_irq (&css_set_lock); - -while (!cgroup_is_descendant (dst_cgrp, cgrp)) - cgrp =cgroup_parent (cgrp); - - ret = -ENOMEM ; - inode =kernfs_get_inode (sb, cgrp->procs_file.kn); -if (inode) { - ret =inode_permission (inode,MAY_WRITE ); -iput (inode); - } - }else { +if (!cgroup_on_dfl (dst_cgrp)) {const struct cred *cred =current_cred ();const struct cred *tcred =get_task_cred (task);
@@ -2391,14 +2376,38 @@ static int cgroup_procs_write_permission* even if we're attaching all tasks in the thread group, * we only need to check permissions on one of them. */ -if (!uid_eq (cred->euid,GLOBAL_ROOT_UID ) && - !uid_eq (cred->euid, tcred->uid) && - !uid_eq (cred->euid, tcred->suid)) +if (uid_eq (cred->euid,GLOBAL_ROOT_UID ) || +uid_eq (cred->euid, tcred->uid) || +uid_eq (cred->euid, tcred->suid)) + ret =0 ; +else ret = -EACCES ; +put_cred (tcred); +return ret; } -return ret; +/* find the source cgroup */ +spin_lock_irq (&css_set_lock); + src_cgrp =task_cgroup_from_root (task, &cgrp_dfl_root); +spin_unlock_irq (&css_set_lock); + +/* and the common ancestor */ + com_cgrp = src_cgrp; +while (!cgroup_is_descendant (dst_cgrp, com_cgrp)) + com_cgrp =cgroup_parent (com_cgrp); + +/* %current should be authorized to migrate to the common ancestor */ + inode =kernfs_get_inode (sb, com_cgrp->procs_file.kn); +if (!inode) +return -ENOMEM ; + + ret =inode_permission (inode,MAY_WRITE ); +iput (inode); +if (ret) +return ret; + +return 0 ; }/*
Tejun Heo
maintainerPATCH 3/3 cgroup: implement "nsdelegate" mount option
Currently, cgroup only supports delegation to !root users and cgroup namespaces don't get any special treatments. This limits the usefulness of cgroup namespaces as they by themselves can't be safe delegation boundaries. A process inside a cgroup can change the resource control knobs of the parent in the namespace root and may move processes in and out of the namespace if cgroups outside its namespace are visible somehow.
This patch adds a new mount option "nsdelegate" which makes cgroup namespaces delegation boundaries. If set, cgroup behaves as if write permission based delegation took place at namespace boundaries - writes to the resource control knobs from the namespace root are denied and migration crossing the namespace boundary aren't allowed from inside the namespace.
This allows cgroup namespace to function as a delegation boundary by itself.
--- Documentation/cgroup-v2.txt | 60 +++++++++++++++++++-------- include/linux/cgroup-defs.h | 9 ++++ kernel/cgroup/cgroup.c | 96 +++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 142 insertions(+), 23 deletions(-)
Documentation/cgroup-v2.txt +42 -18
@@ -149,6 +149,15 @@ during boot, before manual intervention and experimenting easier, the kernel parameter cgroup_no_v1= allows disabling controllers in v1 and make them always available in v2. +cgroup v2 currently supports the following mount options. + + nsdelegate + + Consider cgroup namespaces as delegation boundaries. This + option is system wide and can only be set on mount or modified + through remount from the init namespace. Please refer to the + Delegation section for details. + 2-2. Organizing Processes
@@ -308,19 +317,27 @@ file. 2-5-1. Model of Delegation -A cgroup can be delegated to a less privileged user by granting write -access of the directory and its "cgroup.procs" and -"cgroup.subtree_control" files to the user. Note that resource -control interface files in a given directory control the distribution -of the parent's resources and thus must not be delegated along with -the directory. - -Once delegated, the user can build sub-hierarchy under the directory, -organize processes as it sees fit and further distribute the resources -it received from the parent. The limits and other settings of all -resource controllers are hierarchical and regardless of what happens -in the delegated sub-hierarchy, nothing can escape the resource -restrictions imposed by the parent. +A cgroup can be delegated in two ways. First, to a less privileged +user by granting write access of the directory and its "cgroup.procs" +and "cgroup.subtree_control" files to the user. Second, if the +"nsdelegate" mount option is set, automatically to a cgroup namespace +on namespace creation. + +Because the resource control interface files in a given directory +control the distribution of the parent's resources, the delegatee +shouldn't be allowed to write to them. For the first method, this is +achieved by not granting access to these files. For the second, the +kernel rejects writes to all files other than "cgroup.procs" and +"cgroup.subtree_control" on a namespace root from inside the +namespace. + +The end results are equivalent for both delegation types. Once +delegated, the user can build sub-hierarchy under the directory, +organize processes inside it as it sees fit and further distribute the +resources it received from the parent. The limits and other settings +of all resource controllers are hierarchical and regardless of what +happens in the delegated sub-hierarchy, nothing can escape the +resource restrictions imposed by the parent. Currently, cgroup doesn't impose any restrictions on the number of cgroups in or nesting depth of a delegated sub-hierarchy; however,
@@ -330,10 +347,12 @@ this may be limited explicitly in the fu 2-5-2. Delegation Containment A delegated sub-hierarchy is contained in the sense that processes -can't be moved into or out of the sub-hierarchy by the delegatee. For -a process with a non-root euid to migrate a target process into a -cgroup by writing its PID to the "cgroup.procs" file, the following -conditions must be met. +can't be moved into or out of the sub-hierarchy by the delegatee. + +For delegations to a less privileged user, this is achieved by +requiring the following conditions for a process with a non-root euid +to migrate a target process into a cgroup by writing its PID to the +"cgroup.procs" file. - The writer must have write access to the "cgroup.procs" file.
@@ -360,6 +379,11 @@ destination cgroup C00 is above the poin not have write access to its "cgroup.procs" files and thus the write will be denied with -EACCES. +For delegations to namespaces, containment is achieved by requiring +that both the source and destination cgroups are reachable from the +namespace of the process which is attempting the migration. If either +is not reachable, the migration is rejected with -ENOENT. + 2-6. Guidelines
@@ -1414,7 +1438,7 @@ D. Deprecated v1 Core Features - Multiple hierarchies including named ones are not supported. -- All mount options and remounting are not supported. +- All v1 mount options are not supported. - The "tasks" file is removed and "cgroup.procs" is not sorted.
include/linux/cgroup-defs.h +9 -0
@@ -67,12 +67,21 @@ enum {enum {CGRP_ROOT_NOPREFIX = (1 <<1 ),/* mounted subsystems have no named prefix */ CGRP_ROOT_XATTR = (1 <<2 ),/* supports extended attributes */ + +/* +* Consider namespaces as delegation boundaries. If this flag is +* set, controller specific interface files in a namespace root +* aren't writeable from inside the namespace. +*/ +CGRP_ROOT_NS_DELEGATE = (1 <<3 ), };/* cftype->flags */ enum {CFTYPE_ONLY_ON_ROOT = (1 <<0 ),/* only create on root cgrp */ CFTYPE_NOT_ON_ROOT = (1 <<1 ),/* don't create on root cgrp */ +CFTYPE_NS_DELEGATABLE = (1 <<2 ),/* writeable beyond delegation boundaries */ +CFTYPE_NO_PREFIX = (1 <<3 ),/* (DON'T USE FOR NEW FILES) no subsys prefix */ CFTYPE_WORLD_WRITABLE = (1 <<4 ),/* (DON'T USE FOR NEW FILES) S_IWUGO */
kernel/cgroup/cgroup.c +91 -5
164 lines not on this page · raw message
Tejun Heo
maintainerPATCH v2 3/3 cgroup: implement "nsdelegate" mount option
Currently, cgroup only supports delegation to !root users and cgroup namespaces don't get any special treatments. This limits the usefulness of cgroup namespaces as they by themselves can't be safe delegation boundaries. A process inside a cgroup can change the resource control knobs of the parent in the namespace root and may move processes in and out of the namespace if cgroups outside its namespace are visible somehow.
This patch adds a new mount option "nsdelegate" which makes cgroup namespaces delegation boundaries. If set, cgroup behaves as if write permission based delegation took place at namespace boundaries - writes to the resource control knobs from the namespace root are denied and migration crossing the namespace boundary aren't allowed from inside the namespace.
This allows cgroup namespace to function as a delegation boundary by itself.
v2: Silently ignore nsdelegate specified on !init mounts.
--- Documentation/cgroup-v2.txt | 61 +++++++++++++++++++++--------- include/linux/cgroup-defs.h | 9 ++++ kernel/cgroup/cgroup.c | 88 +++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 135 insertions(+), 23 deletions(-)
Documentation/cgroup-v2.txt +43 -18
@@ -149,6 +149,16 @@ during boot, before manual intervention and experimenting easier, the kernel parameter cgroup_no_v1= allows disabling controllers in v1 and make them always available in v2. +cgroup v2 currently supports the following mount options. + + nsdelegate + + Consider cgroup namespaces as delegation boundaries. This + option is system wide and can only be set on mount or modified + through remount from the init namespace. The mount option is + ignored on non-init namespace mounts. Please refer to the + Delegation section for details. + 2-2. Organizing Processes
@@ -308,19 +318,27 @@ file. 2-5-1. Model of Delegation -A cgroup can be delegated to a less privileged user by granting write -access of the directory and its "cgroup.procs" and -"cgroup.subtree_control" files to the user. Note that resource -control interface files in a given directory control the distribution -of the parent's resources and thus must not be delegated along with -the directory. - -Once delegated, the user can build sub-hierarchy under the directory, -organize processes as it sees fit and further distribute the resources -it received from the parent. The limits and other settings of all -resource controllers are hierarchical and regardless of what happens -in the delegated sub-hierarchy, nothing can escape the resource -restrictions imposed by the parent. +A cgroup can be delegated in two ways. First, to a less privileged +user by granting write access of the directory and its "cgroup.procs" +and "cgroup.subtree_control" files to the user. Second, if the +"nsdelegate" mount option is set, automatically to a cgroup namespace +on namespace creation. + +Because the resource control interface files in a given directory +control the distribution of the parent's resources, the delegatee +shouldn't be allowed to write to them. For the first method, this is +achieved by not granting access to these files. For the second, the +kernel rejects writes to all files other than "cgroup.procs" and +"cgroup.subtree_control" on a namespace root from inside the +namespace. + +The end results are equivalent for both delegation types. Once +delegated, the user can build sub-hierarchy under the directory, +organize processes inside it as it sees fit and further distribute the +resources it received from the parent. The limits and other settings +of all resource controllers are hierarchical and regardless of what +happens in the delegated sub-hierarchy, nothing can escape the +resource restrictions imposed by the parent. Currently, cgroup doesn't impose any restrictions on the number of cgroups in or nesting depth of a delegated sub-hierarchy; however,
@@ -330,10 +348,12 @@ this may be limited explicitly in the fu 2-5-2. Delegation Containment A delegated sub-hierarchy is contained in the sense that processes -can't be moved into or out of the sub-hierarchy by the delegatee. For -a process with a non-root euid to migrate a target process into a -cgroup by writing its PID to the "cgroup.procs" file, the following -conditions must be met. +can't be moved into or out of the sub-hierarchy by the delegatee. + +For delegations to a less privileged user, this is achieved by +requiring the following conditions for a process with a non-root euid +to migrate a target process into a cgroup by writing its PID to the +"cgroup.procs" file. - The writer must have write access to the "cgroup.procs" file.
@@ -360,6 +380,11 @@ destination cgroup C00 is above the poin not have write access to its "cgroup.procs" files and thus the write will be denied with -EACCES. +For delegations to namespaces, containment is achieved by requiring +that both the source and destination cgroups are reachable from the +namespace of the process which is attempting the migration. If either +is not reachable, the migration is rejected with -ENOENT. + 2-6. Guidelines
@@ -1414,7 +1439,7 @@ D. Deprecated v1 Core Features - Multiple hierarchies including named ones are not supported. -- All mount options and remounting are not supported. +- All v1 mount options are not supported. - The "tasks" file is removed and "cgroup.procs" is not sorted.
include/linux/cgroup-defs.h +9 -0
@@ -67,12 +67,21 @@ enum {enum {CGRP_ROOT_NOPREFIX = (1 <<1 ),/* mounted subsystems have no named prefix */ CGRP_ROOT_XATTR = (1 <<2 ),/* supports extended attributes */ + +/* +* Consider namespaces as delegation boundaries. If this flag is +* set, controller specific interface files in a namespace root +* aren't writeable from inside the namespace. +*/ +CGRP_ROOT_NS_DELEGATE = (1 <<3 ), };/* cftype->flags */ enum {CFTYPE_ONLY_ON_ROOT = (1 <<0 ),/* only create on root cgrp */ CFTYPE_NOT_ON_ROOT = (1 <<1 ),/* don't create on root cgrp */ +CFTYPE_NS_DELEGATABLE = (1 <<2 ),/* writeable beyond delegation boundaries */ +CFTYPE_NO_PREFIX = (1 <<3 ),/* (DON'T USE FOR NEW FILES) no subsys prefix */ CFTYPE_WORLD_WRITABLE = (1 <<4 ),/* (DON'T USE FOR NEW FILES) S_IWUGO */
kernel/cgroup/cgroup.c +83 -5
156 lines not on this page · raw message
Tejun Heo
maintainerRe: [PATCH v2 3/3] cgroup: implement "nsdelegate" mount option
Quoted 18 lines · Tejun Heo
Currently, cgroup only supports delegation to !root users and cgroup namespaces don't get any special treatments. This limits the usefulness of cgroup namespaces as they by themselves can't be safe delegation boundaries. A process inside a cgroup can change the resource control knobs of the parent in the namespace root and may move processes in and out of the namespace if cgroups outside its namespace are visible somehow.
This patch adds a new mount option "nsdelegate" which makes cgroup namespaces delegation boundaries. If set, cgroup behaves as if write permission based delegation took place at namespace boundaries - writes to the resource control knobs from the namespace root are denied and migration crossing the namespace boundary aren't allowed from inside the namespace.
This allows cgroup namespace to function as a delegation boundary by itself.
Quoted 6 lines
v2: Silently ignore nsdelegate specified on !init mounts.
Signed-off-by Tejun Heo <[email protected]>Cc Aravind Anbudurai <[email protected]>Cc Serge Hallyn <[email protected]>Cc Eric Biederman <ebiederm-aS9lmoZGLiVWk0Htik3J/[email protected]>
Applying 2-3 to cgroup/for-4.13.
Thanks.
Signature · 1 line
tejun
Tejun Heo
maintainerQuoted 8 lines · Tejun Heo
"cgroup.subtree_control" determines which resource types a cgroup wants to control. Unlike actual resource knobs, this is an attribute which belongs to the cgroup itself instead of its parent and thus should be writeable by the delegatee in a delegated cgroup.
Update delegation documentation accordingly.
Signed-off-by Tejun Heo <[email protected]>
Applying to cgroup/for-4.13. I've been thinking that the doc listed both files all along. Oh well, better late than never.
Thanks.
Signature · 1 line
tejun