Skip to content

Commit 1756d79

Browse files
committed
cgroup: Use open-time credentials for process migraton perm checks
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" <[email protected]> Suggested-by: Linus Torvalds <[email protected]> Fixes: 187fe84 ("cgroup: require write perm on common ancestor when moving processes on the default hierarchy") Reviewed-by: Michal Koutný <[email protected]> Signed-off-by: Tejun Heo <[email protected]>
1 parent 75acfdb commit 1756d79

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

kernel/cgroup/cgroup-v1.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,10 +504,11 @@ static ssize_t __cgroup1_procs_write(struct kernfs_open_file *of,
504504
goto out_unlock;
505505

506506
/*
507-
* Even if we're attaching all tasks in the thread group, we only
508-
* need to check permissions on one of them.
507+
* Even if we're attaching all tasks in the thread group, we only need
508+
* to check permissions on one of them. Check permissions using the
509+
* credentials from file open to protect against inherited fd attacks.
509510
*/
510-
cred = current_cred();
511+
cred = of->file->f_cred;
511512
tcred = get_task_cred(task);
512513
if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
513514
!uid_eq(cred->euid, tcred->uid) &&

kernel/cgroup/cgroup.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4892,6 +4892,7 @@ static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
48924892
{
48934893
struct cgroup *src_cgrp, *dst_cgrp;
48944894
struct task_struct *task;
4895+
const struct cred *saved_cred;
48954896
ssize_t ret;
48964897
bool locked;
48974898

@@ -4909,9 +4910,15 @@ static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
49094910
src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
49104911
spin_unlock_irq(&css_set_lock);
49114912

4912-
/* process and thread migrations follow same delegation rule */
4913+
/*
4914+
* Process and thread migrations follow same delegation rule. Check
4915+
* permissions using the credentials from file open to protect against
4916+
* inherited fd attacks.
4917+
*/
4918+
saved_cred = override_creds(of->file->f_cred);
49134919
ret = cgroup_attach_permissions(src_cgrp, dst_cgrp,
49144920
of->file->f_path.dentry->d_sb, threadgroup);
4921+
revert_creds(saved_cred);
49154922
if (ret)
49164923
goto out_finish;
49174924

0 commit comments

Comments
 (0)