]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
cgroup: Use open-time credentials for process migraton perm checks
authorTejun Heo <tj@kernel.org>
Thu, 6 Jan 2022 21:02:28 +0000 (11:02 -1000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 13 Apr 2022 19:01:10 +0000 (21:01 +0200)
commit 1756d7994ad85c2479af6ae5a9750b92324685af upstream.

cgroup process migration permission checks are performed at write time as
whether a given operation is allowed or not is dependent on the content of
the write - the PID. This currently uses current's credentials which is a
potential security weakness as it may allow scenarios where a less
privileged process tricks a more privileged one into writing into a fd that
it created.

This patch makes both cgroup2 and cgroup1 process migration interfaces to
use the credentials saved at the time of open (file->f_cred) instead of
current's.

Reported-by: "Eric W. Biederman" <ebiederm@xmission.com>
Suggested-by: Linus Torvalds <torvalds@linuxfoundation.org>
Fixes: 187fe84067bd ("cgroup: require write perm on common ancestor when moving processes on the default hierarchy")
Reviewed-by: Michal Koutný <mkoutny@suse.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
[OP: apply original __cgroup_procs_write() changes to cgroup_threads_write()
and cgroup_procs_write(), as the refactoring commit da70862efe006 ("cgroup:
cgroup.{procs,threads} factor out common parts") is not present in 5.10-stable]
Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
kernel/cgroup/cgroup-v1.c
kernel/cgroup/cgroup.c

index 8f0ea12d7cee2e551db6c6d9f6bf9893eb5945ad..1a0a9f820c69bda9a1335ec98a12a7a3a1d24c1f 100644 (file)
@@ -505,10 +505,11 @@ static ssize_t __cgroup1_procs_write(struct kernfs_open_file *of,
                goto out_unlock;
 
        /*
-        * Even if we're attaching all tasks in the thread group, we only
-        * need to check permissions on one of them.
+        * Even if we're attaching all tasks in the thread group, we only need
+        * to check permissions on one of them. Check permissions using the
+        * credentials from file open to protect against inherited fd attacks.
         */
-       cred = current_cred();
+       cred = of->file->f_cred;
        tcred = get_task_cred(task);
        if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
            !uid_eq(cred->euid, tcred->uid) &&
index 3f8447a5393e9c35effbf10270137f312fcfbc2b..0853289d321a532a524f7d6408a7c6d2b83a3679 100644 (file)
@@ -4788,6 +4788,7 @@ static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
        struct cgroup_file_ctx *ctx = of->priv;
        struct cgroup *src_cgrp, *dst_cgrp;
        struct task_struct *task;
+       const struct cred *saved_cred;
        ssize_t ret;
        bool locked;
 
@@ -4805,9 +4806,16 @@ static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
        src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
        spin_unlock_irq(&css_set_lock);
 
+       /*
+        * Process and thread migrations follow same delegation rule. Check
+        * permissions using the credentials from file open to protect against
+        * inherited fd attacks.
+        */
+       saved_cred = override_creds(of->file->f_cred);
        ret = cgroup_attach_permissions(src_cgrp, dst_cgrp,
                                        of->file->f_path.dentry->d_sb, true,
                                        ctx->ns);
+       revert_creds(saved_cred);
        if (ret)
                goto out_finish;
 
@@ -4832,6 +4840,7 @@ static ssize_t cgroup_threads_write(struct kernfs_open_file *of,
        struct cgroup_file_ctx *ctx = of->priv;
        struct cgroup *src_cgrp, *dst_cgrp;
        struct task_struct *task;
+       const struct cred *saved_cred;
        ssize_t ret;
        bool locked;
 
@@ -4851,10 +4860,16 @@ static ssize_t cgroup_threads_write(struct kernfs_open_file *of,
        src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
        spin_unlock_irq(&css_set_lock);
 
-       /* thread migrations follow the cgroup.procs delegation rule */
+       /*
+        * Process and thread migrations follow same delegation rule. Check
+        * permissions using the credentials from file open to protect against
+        * inherited fd attacks.
+        */
+       saved_cred = override_creds(of->file->f_cred);
        ret = cgroup_attach_permissions(src_cgrp, dst_cgrp,
                                        of->file->f_path.dentry->d_sb, false,
                                        ctx->ns);
+       revert_creds(saved_cred);
        if (ret)
                goto out_finish;