Skip to contentSkip to conversation

Relax ns_can_attach checks to allow attaching to grandchild cgroups

PATCH Andrew Morton 3 participants 4 messages Dec 19, 2008 5 messages not in the archive cgroup

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): 20081219120954.GB27472-yp6mvK3Bdd2rDJvtcaxF/[email protected]

Andrew Morton

author Reviewed-by Li Zefan
#2 lore raw

(cc [email protected])

Please don't send patches via private email!

From: Grzegorz Nosek <[email protected]>

The ns_proxy cgroup allows moving processes to child cgroups only one level deep at a time. This commit relaxes this restriction and makes it possible to attach tasks directly to grandchild cgroups, e.g.:

($pid is in the root cgroup)
echo $pid > /cgroup/CG1/CG2/tasks

Previously this operation would fail with -EPERM and would have to be performed as two steps:
echo $pid > /cgroup/CG1/tasks
echo $pid > /cgroup/CG1/CG2/tasks

Cc Paul Menage <[email protected]>
Cc Balbir Singh <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
Cc Pavel Emelyanov <[email protected]>

---

 include/linux/cgroup.h |    4 ++--
 kernel/cgroup.c        |   12 +++++++-----
 kernel/ns_cgroup.c     |    9 +++------
 3 files changed, 12 insertions(+), 13 deletions(-)
include/linux/cgroup.h renamed from include/linux/cgroup.h~relax-ns_can_attach-checks-to-allow-attaching-to-grandchild-cgroups +2 -2
diff -puN include/linux/cgroup.h~relax-ns_can_attach-checks-to-allow-attaching-to-grandchild-cgroups include/linux/cgroup.h
@@ -344,8 +344,8 @@ int cgroup_path(const struct cgroup *cgr
 
 int cgroup_task_count(const struct cgroup *cgrp);
 
-/* Return true if the cgroup is a descendant of the current cgroup */
-int cgroup_is_descendant(const struct cgroup *cgrp);
+/* Return true if cgrp is a descendant of the task's cgroup */
+int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task);
 
 /* Control Group subsystem type. See Documentation/cgroups.txt for details */
 
kernel/cgroup.c renamed from kernel/cgroup.c~relax-ns_can_attach-checks-to-allow-attaching-to-grandchild-cgroups +7 -5
diff -puN kernel/cgroup.c~relax-ns_can_attach-checks-to-allow-attaching-to-grandchild-cgroups kernel/cgroup.c
@@ -3072,18 +3072,19 @@ int cgroup_clone(struct task_struct *tsk
 }
 
 /**
- * cgroup_is_descendant - see if @cgrp is a descendant of current task's cgrp
+ * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
  * @cgrp: the cgroup in question
+ * @task: the task in question
  *
- * See if @cgrp is a descendant of the current task's cgroup in
- * the appropriate hierarchy.
+ * See if @cgrp is a descendant of @task's cgroup in the appropriate
+ * hierarchy.
  *
  * If we are sending in dummytop, then presumably we are creating
  * the top cgroup in the subsystem.
  *
  * Called only by the ns (nsproxy) cgroup.
  */
-int cgroup_is_descendant(const struct cgroup *cgrp)
+int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
 {
 	int ret;
 	struct cgroup *target;
@@ -3093,7 +3094,8 @@ int cgroup_is_descendant(const struct cg
 		return 1;
 
 	get_first_subsys(cgrp, NULL, &subsys_id);
-	target = task_cgroup(current, subsys_id);
+	target = task_cgroup(task, subsys_id);
+
 	while (cgrp != target && cgrp!= cgrp->top_cgroup)
 		cgrp = cgrp->parent;
 	ret = (cgrp == target);
kernel/ns_cgroup.c renamed from kernel/ns_cgroup.c~relax-ns_can_attach-checks-to-allow-attaching-to-grandchild-cgroups +3 -6
diff -puN kernel/ns_cgroup.c~relax-ns_can_attach-checks-to-allow-attaching-to-grandchild-cgroups kernel/ns_cgroup.c
@@ -45,21 +45,18 @@ int ns_cgroup_clone(struct task_struct *
 static int ns_can_attach(struct cgroup_subsys *ss,
 		struct cgroup *new_cgroup, struct task_struct *task)
 {
-	struct cgroup *orig;
-
 	if (current != task) {
 		if (!capable(CAP_SYS_ADMIN))
 			return -EPERM;
 
-		if (!cgroup_is_descendant(new_cgroup))
+		if (!cgroup_is_descendant(new_cgroup, current))
 			return -EPERM;
 	}
 
 	if (atomic_read(&new_cgroup->count) != 0)
 		return -EPERM;
 
-	orig = task_cgroup(task, ns_subsys_id);
-	if (orig && orig != new_cgroup->parent)
+	if (!cgroup_is_descendant(new_cgroup, task))
 		return -EPERM;
 
 	return 0;
@@ -77,7 +74,7 @@ static struct cgroup_subsys_state *ns_cr
 
 	if (!capable(CAP_SYS_ADMIN))
 		return ERR_PTR(-EPERM);
-	if (!cgroup_is_descendant(cgroup))
+	if (!cgroup_is_descendant(cgroup, current))
 		return ERR_PTR(-EPERM);
 
 	ns_cgroup = kzalloc(sizeof(*ns_cgroup), GFP_KERNEL);

_

A message that is not in the archive (sent off-list or not yet received): 20081219131641.cafc65ac.akpm-de/[email protected]

#4 lore raw
Quoted 20 lines · Andrew Morton

(cc [email protected])

Please don't send patches via private email!

From: Grzegorz Nosek <[email protected]>

The ns_proxy cgroup allows moving processes to child cgroups only one level deep at a time. This commit relaxes this restriction and makes it possible to attach tasks directly to grandchild cgroups, e.g.:

($pid is in the root cgroup)
echo $pid > /cgroup/CG1/CG2/tasks

Previously this operation would fail with -EPERM and would have to be performed as two steps:
echo $pid > /cgroup/CG1/tasks
echo $pid > /cgroup/CG1/CG2/tasks

Quoted 7 lines
Cc Paul Menage <[email protected]>
Cc Balbir Singh <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
Cc Pavel Emelyanov <[email protected]>

I trust (since you're not removing it) that the restriction that the target cgroup be empty is not a problem?

Also, 'rule 1' in the comment above ns_can_attach should be modified accordingly (s/child/descendant).

thanks,
-serge

Quoted 100 lines

---

 include/linux/cgroup.h |    4 ++--
 kernel/cgroup.c        |   12 +++++++-----
 kernel/ns_cgroup.c     |    9 +++------
 3 files changed, 12 insertions(+), 13 deletions(-)

diff -puN include/linux/cgroup.h~relax-ns_can_attach-checks-to-allow-attaching-to-grandchild-cgroups include/linux/cgroup.h

include/linux/cgroup.h renamed from include/linux/cgroup.h~relax-ns_can_attach-checks-to-allow-attaching-to-grandchild-cgroups +2 -2
@@ -344,8 +344,8 @@ int cgroup_path(const struct cgroup *cgr
 
 int cgroup_task_count(const struct cgroup *cgrp);
 
-/* Return true if the cgroup is a descendant of the current cgroup */
-int cgroup_is_descendant(const struct cgroup *cgrp);
+/* Return true if cgrp is a descendant of the task's cgroup */
+int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task);
 
 /* Control Group subsystem type. See Documentation/cgroups.txt for details */
 

diff -puN kernel/cgroup.c~relax-ns_can_attach-checks-to-allow-attaching-to-grandchild-cgroups kernel/cgroup.c

kernel/cgroup.c renamed from kernel/cgroup.c~relax-ns_can_attach-checks-to-allow-attaching-to-grandchild-cgroups +7 -5
@@ -3072,18 +3072,19 @@ int cgroup_clone(struct task_struct *tsk
 }
 
 /**
- * cgroup_is_descendant - see if @cgrp is a descendant of current task's cgrp
+ * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
  * @cgrp: the cgroup in question
+ * @task: the task in question
  *
- * See if @cgrp is a descendant of the current task's cgroup in
- * the appropriate hierarchy.
+ * See if @cgrp is a descendant of @task's cgroup in the appropriate
+ * hierarchy.
  *
  * If we are sending in dummytop, then presumably we are creating
  * the top cgroup in the subsystem.
  *
  * Called only by the ns (nsproxy) cgroup.
  */
-int cgroup_is_descendant(const struct cgroup *cgrp)
+int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
 {
 	int ret;
 	struct cgroup *target;
@@ -3093,7 +3094,8 @@ int cgroup_is_descendant(const struct cg
 		return 1;
 
 	get_first_subsys(cgrp, NULL, &subsys_id);
-	target = task_cgroup(current, subsys_id);
+	target = task_cgroup(task, subsys_id);
+
 	while (cgrp != target && cgrp!= cgrp->top_cgroup)
 		cgrp = cgrp->parent;
 	ret = (cgrp == target);

diff -puN kernel/ns_cgroup.c~relax-ns_can_attach-checks-to-allow-attaching-to-grandchild-cgroups kernel/ns_cgroup.c

kernel/ns_cgroup.c renamed from kernel/ns_cgroup.c~relax-ns_can_attach-checks-to-allow-attaching-to-grandchild-cgroups +3 -6
@@ -45,21 +45,18 @@ int ns_cgroup_clone(struct task_struct *
 static int ns_can_attach(struct cgroup_subsys *ss,
 		struct cgroup *new_cgroup, struct task_struct *task)
 {
-	struct cgroup *orig;
-
 	if (current != task) {
 		if (!capable(CAP_SYS_ADMIN))
 			return -EPERM;
 
-		if (!cgroup_is_descendant(new_cgroup))
+		if (!cgroup_is_descendant(new_cgroup, current))
 			return -EPERM;
 	}
 
 	if (atomic_read(&new_cgroup->count) != 0)
 		return -EPERM;
 
-	orig = task_cgroup(task, ns_subsys_id);
-	if (orig && orig != new_cgroup->parent)
+	if (!cgroup_is_descendant(new_cgroup, task))
 		return -EPERM;
 
 	return 0;
@@ -77,7 +74,7 @@ static struct cgroup_subsys_state *ns_cr
 
 	if (!capable(CAP_SYS_ADMIN))
 		return ERR_PTR(-EPERM);
-	if (!cgroup_is_descendant(cgroup))
+	if (!cgroup_is_descendant(cgroup, current))
 		return ERR_PTR(-EPERM);
 
 	ns_cgroup = kzalloc(sizeof(*ns_cgroup), GFP_KERNEL);

_

_______________________________________________
Containers mailing list Containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
https://lists.linux-foundation.org/mailman/listinfo/containers

A message that is not in the archive (sent off-list or not yet received): 20081219222304.GA25828-r/[email protected]

#6 lore raw
Quoted 4 lines · Serge E. Hallyn
Quoted 3 lines · Andrew Morton

(cc [email protected])

Please don't send patches via private email!

My apologies.

Quoted 2 lines

I trust (since you're not removing it) that the restriction that the target cgroup be empty is not a problem?

Sigh, good catch. I'm building my lxc-based environment slowly and I'm only testing the most basic stuff currently, so I'd bug you about it eventually.

Frankly, I don't understand the reason behind these restrictions and feel like I'm missing some important piece of a puzzle. In my tests all the tasks in question are living in the same namespace (though it won't always be so), so I'd guess I should be able to move the tasks freely between cgroups. Why exactly does the target cgroup have to be empty?

Also, should we remember the task->nsproxy pointer in the cgroup data and ignore hierarchy if it matches? I guess it would be safe to store the raw pointer without refcounting it in any way as we'd never dereference it (could keep it as uintptr_t to reinforce the idea) but only compare with another pointer.

Does that make any sense? Or should I simply mount the cgroup fs without the ns subsystem and forget the whole thing? What exactly do I lose by doing so?

Quoted 2 lines

Also, 'rule 1' in the comment above ns_can_attach should be modified accordingly (s/child/descendant).

Indeed. Will resend after receiving some enlightenment about the above.

Thank you for your comments.

Best regards,
Grzegorz Nosek
_______________________________________________
Containers mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/containers

A message that is not in the archive (sent off-list or not yet received): 20081219230330.GA30623-IaEwMO9oKu/77SC2UrCW1JJg/dWx8T/[email protected]

#8 lore raw
Quoted 15 lines · Grzegorz Nosek
Quoted 4 lines · Serge E. Hallyn
Quoted 3 lines · Andrew Morton

(cc [email protected])

Please don't send patches via private email!

My apologies.

Quoted 2 lines

I trust (since you're not removing it) that the restriction that the target cgroup be empty is not a problem?

Sigh, good catch. I'm building my lxc-based environment slowly and I'm only testing the most basic stuff currently, so I'd bug you about it eventually.

Quoted 5 lines

Frankly, I don't understand the reason behind these restrictions and feel like I'm missing some important piece of a puzzle. In my tests all the tasks in question are living in the same namespace (though it won't always be so), so I'd guess I should be able to move the tasks freely between cgroups. Why exactly does the target cgroup have to be empty?

The reasoning goes back to one motivation of the ns cgroup being to facilitate actual moves between namespaces. Since that is no longer being considered, easing the restrictions is ok.

On the other hand, the only remaining use for the ns cgroup is to provide some locking of tasks/containers into cgroups. So the main restriction I'd like to keep in place is that you can only go downward in cgroup hierarchy. (Think devices whitelist cgroup).

Now maybe it makes sense to split the two things ns_cgroup does into 2+ cgroups: one (nstrack) would simply clone a new child cgroup every time a task does an unshare, another (if mounted) prevents a task from moving to a cgroup which isn't a desendent, while a third could do more complicated controls, i.e. a task could be locked into cgroup:/a/b, after which it could freely move up and down under cgroup:/a/b, (i.e. to switch between cgroup:/a/b/c1 and cgroup:/a/b/c2), but could never escape cgroup:/a/b. Then you could choose which if any movement-controlling and movement-tracking cgroups to compose.

I actually rather like that idea, but I think we'd have to keep the ns cgroup the way it is, while using new cgroups to offer the new functionality.

Quoted 5 lines

Also, should we remember the task->nsproxy pointer in the cgroup data and ignore hierarchy if it matches? I guess it would be safe to store the raw pointer without refcounting it in any way as we'd never dereference it (could keep it as uintptr_t to reinforce the idea) but only compare with another pointer.

No, I'm thinking that despite the name, since we wont' use it to actually enter namespaces, we should keep it decoupled from nsproxy.

Quoted 3 lines

Does that make any sense? Or should I simply mount the cgroup fs without the ns subsystem and forget the whole thing? What exactly do I lose by doing so?

With liblxc I think you might lose the way that it keeps track of containers. Not sure though - give it a shot.

Quoted 9 lines
Quoted 2 lines

Also, 'rule 1' in the comment above ns_can_attach should be modified accordingly (s/child/descendant).

Indeed. Will resend after receiving some enlightenment about the above.

Thank you for your comments.

Best regards,
Grzegorz Nosek

_______________________________________________
Containers mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/containers