cgroup: pids: fix invalid get/put usage
authorAleksa Sarai <cyphar@cyphar.com>
Tue, 25 Aug 2015 02:50:44 +0000 (12:50 +1000)
committerTejun Heo <tj@kernel.org>
Tue, 25 Aug 2015 18:19:25 +0000 (14:19 -0400)
Fix incorrect usage of css_get and css_put to put a different css in
pids_{cancel_,}attach() than the one grabbed in pids_can_attach(). This
could lead to quite serious memory leakage (and unsafe operations on the
putted css).

tj: minor comment update

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
kernel/cgroup_pids.c

index d75488824ae222f5aa55d13c963333bc7aa4e4f8..806cd7693ac88b0307173ab7bdaf1a8bc44ab173 100644 (file)
@@ -173,11 +173,11 @@ static int pids_can_attach(struct cgroup_subsys_state *css,
                struct pids_cgroup *old_pids;
 
                /*
-                * Grab a ref to each task's css. We don't drop the ref until
-                * we either fail and hit ->cancel_attach() or succeed and hit
-                * ->attach().
+                * No need to pin @old_css between here and cancel_attach()
+                * because cgroup core protects it from being freed before
+                * the migration completes or fails.
                 */
-               old_css = task_get_css(task, pids_cgrp_id);
+               old_css = task_css(task, pids_cgrp_id);
                old_pids = css_pids(old_css);
 
                pids_charge(pids, 1);
@@ -202,19 +202,9 @@ static void pids_cancel_attach(struct cgroup_subsys_state *css,
 
                pids_charge(old_pids, 1);
                pids_uncharge(pids, 1);
-               css_put(old_css);
        }
 }
 
-static void pids_attach(struct cgroup_subsys_state *css,
-                       struct cgroup_taskset *tset)
-{
-       struct task_struct *task;
-
-       cgroup_taskset_for_each(task, tset)
-               css_put(task_css(task, pids_cgrp_id));
-}
-
 static int pids_can_fork(struct task_struct *task, void **priv_p)
 {
        struct cgroup_subsys_state *css;
@@ -354,7 +344,6 @@ static struct cftype pids_files[] = {
 struct cgroup_subsys pids_cgrp_subsys = {
        .css_alloc      = pids_css_alloc,
        .css_free       = pids_css_free,
-       .attach         = pids_attach,
        .can_attach     = pids_can_attach,
        .cancel_attach  = pids_cancel_attach,
        .can_fork       = pids_can_fork,