cgroup: Add generic cgroup subsystem permission checks.
authorSan Mehat <san@google.com>
Thu, 21 May 2009 21:10:06 +0000 (14:10 -0700)
committerColin Cross <ccross@android.com>
Thu, 30 Sep 2010 00:49:18 +0000 (17:49 -0700)
    Rather than using explicit euid == 0 checks when trying to move
tasks into a cgroup via CFS, move permission checks into each
specific cgroup subsystem. If a subsystem does not specify a
'can_attach' handler, then we fall back to doing our checks the old way.

    This way non-root processes can add arbitrary processes to
a cgroup if all the registered subsystems on that cgroup agree.

    Also change explicit euid == 0 check to CAP_SYS_ADMIN

Signed-off-by: San Mehat <san@google.com>
kernel/cgroup.c
kernel/cgroup_freezer.c
kernel/cpuset.c
kernel/sched.c

index c9483d8f6140ed6cb4e06fa6139e2aeb907b3d47..f3d6f30c8cd181059f23296ea9731cd1a417a030 100644 (file)
@@ -58,6 +58,7 @@
 #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
 #include <linux/eventfd.h>
 #include <linux/poll.h>
+#include <linux/capability.h>
 
 #include <asm/atomic.h>
 
@@ -1722,6 +1723,14 @@ int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
                                 */
                                failed_ss = ss;
                                goto out;
+               } else if (!capable(CAP_SYS_ADMIN)) {
+                       const struct cred *cred = current_cred(), *tcred;
+
+                       /* No can_attach() - check perms generically */
+                       tcred = __task_cred(tsk);
+                       if (cred->euid != tcred->uid &&
+                           cred->euid != tcred->suid) {
+                               return -EACCES;
                        }
                }
        }
@@ -1821,7 +1830,6 @@ EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
 static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
 {
        struct task_struct *tsk;
-       const struct cred *cred = current_cred(), *tcred;
        int ret;
 
        if (pid) {
@@ -1831,14 +1839,6 @@ static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
                        rcu_read_unlock();
                        return -ESRCH;
                }
-
-               tcred = __task_cred(tsk);
-               if (cred->euid &&
-                   cred->euid != tcred->uid &&
-                   cred->euid != tcred->suid) {
-                       rcu_read_unlock();
-                       return -EACCES;
-               }
                get_task_struct(tsk);
                rcu_read_unlock();
        } else {
index ce71ed53e88fbd1e648de49242d9fa63cc570685..8e4c614d955301e386c5f361e994e3f45b40ac01 100644 (file)
@@ -172,6 +172,14 @@ static int freezer_can_attach(struct cgroup_subsys *ss,
 {
        struct freezer *freezer;
 
+       if ((current != task) && (!capable(CAP_SYS_ADMIN))) {
+               const struct cred *cred = current_cred(), *tcred;
+
+               tcred = __task_cred(task);
+               if (cred->euid != tcred->uid && cred->euid != tcred->suid)
+                       return -EPERM;
+       }
+
        /*
         * Anything frozen can't move or be moved to/from.
         *
index b23c0979bbe7212a748a9aac70697e92ee54f448..328f1e7ed7829caae9ae9d2285ca500b301cb264 100644 (file)
@@ -1383,6 +1383,13 @@ static int cpuset_can_attach(struct cgroup_subsys *ss, struct cgroup *cont,
        int ret;
        struct cpuset *cs = cgroup_cs(cont);
 
+       if ((current != task) && (!capable(CAP_SYS_ADMIN))) {
+               const struct cred *cred = current_cred(), *tcred;
+
+               if (cred->euid != tcred->uid && cred->euid != tcred->suid)
+                       return -EPERM;
+       }
        if (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
                return -ENOSPC;
 
index bbf29d6dbc5c08c1cb87a0d46d68124188446b86..39e1666fb72dc197a281284f4b282264ffb89d23 100644 (file)
@@ -8725,6 +8725,15 @@ cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
 static int
 cpu_cgroup_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
 {
+       if ((current != tsk) && (!capable(CAP_SYS_NICE))) {
+               const struct cred *cred = current_cred(), *tcred;
+
+               tcred = __task_cred(tsk);
+
+               if (cred->euid != tcred->uid && cred->euid != tcred->suid)
+                       return -EPERM;
+       }
+
 #ifdef CONFIG_RT_GROUP_SCHED
        if (!sched_rt_can_attach(cgroup_tg(cgrp), tsk))
                return -EINVAL;