Blob Blame History Raw
From: Tejun Heo <tj@kernel.org>
Date: Thu, 6 Jan 2022 11:02:28 -1000
Subject: cgroup: Use open-time credentials for process migraton perm checks
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Git-commit: 1756d7994ad85c2479af6ae5a9750b92324685af
Patch-mainline: v5.16
References: bsc#1194302 CVE-2021-4197


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>
[mkoutny: Put the logic to the single __cgroup_procs_write handler used both for v1 and v2 migrations]
Acked-by: Michal Koutný <mkoutny@suse.com>
---
 kernel/cgroup/cgroup.c |    8 ++++++++
 1 file changed, 8 insertions(+)

--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -2391,6 +2391,11 @@ static int cgroup_procs_write_permission
 			iput(inode);
 		}
 	} else {
+		/*
+		 * Check permissions using the credentials from file open to
+		 * protect against inherited fd attacks, see override_creds()
+		 * in __cgroup_procs_write().
+		 */
 		const struct cred *cred = current_cred();
 		const struct cred *tcred = get_task_cred(task);
 
@@ -2419,6 +2424,7 @@ ssize_t __cgroup_procs_write(struct kern
 	struct task_struct *tsk;
 	struct cgroup_subsys *ss;
 	struct cgroup *cgrp;
+	const struct cred *saved_cred;
 	pid_t pid;
 	int ssid, ret;
 
@@ -2458,7 +2464,9 @@ ssize_t __cgroup_procs_write(struct kern
 	get_task_struct(tsk);
 	rcu_read_unlock();
 
+	saved_cred = override_creds(of->file->f_cred);
 	ret = cgroup_procs_write_permission(tsk, cgrp, of);
+	revert_creds(saved_cred);
 	if (!ret)
 		ret = cgroup_attach_task(cgrp, tsk, threadgroup);