Skip to contentSkip to conversation

cgroup-v1: Correct privileges check in release_agent writes

PATCH Applied Michal Koutný 3 participants 3 messages Feb 17, 2022 – Feb 22, 2022

Subsystems (2)
lore
Share & tools
lore

Michal Koutný

author maintainer
#0 lore raw

PATCH 1/1 cgroup-v1: Correct privileges check in release_agent writes

The idea is to check: a) the owning user_ns of cgroup_ns, b) capabilities in init_user_ns.

The commit 24f600856418 ("cgroup-v1: Require capabilities to set release_agent") got this wrong in the write handler of release_agent since it checked user_ns of the opener (may be different from the owning user_ns of cgroup_ns).
Secondly, to avoid possibly confused deputy, the capability of the opener must be checked.

---
 kernel/cgroup/cgroup-v1.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/cgroup/cgroup-v1.c +4 -2
index 0e877dbcfeea..afc6c0e9c966 100644
@@ -546,6 +546,7 @@ static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
 					  char *buf, size_t nbytes, loff_t off)
 {
 	struct cgroup *cgrp;
+	struct cgroup_file_ctx *ctx;
 
 	BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
 
@@ -553,8 +554,9 @@ static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
 	 * Release agent gets called with all capabilities,
 	 * require capabilities to set release agent.
 	 */
-	if ((of->file->f_cred->user_ns != &init_user_ns) ||
-	    !capable(CAP_SYS_ADMIN))
+	ctx = of->priv;
+	if ((ctx->ns->user_ns != &init_user_ns) ||
+	    !file_ns_capable(of->file, &init_user_ns, CAP_SYS_ADMIN))
 		return -EPERM;
 
 	cgrp = cgroup_kn_lock_live(of->kn, false);
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 44 lines · Michal Koutný

The idea is to check: a) the owning user_ns of cgroup_ns, b) capabilities in init_user_ns.

The commit 24f600856418 ("cgroup-v1: Require capabilities to set release_agent") got this wrong in the write handler of release_agent since it checked user_ns of the opener (may be different from the owning user_ns of cgroup_ns).
Secondly, to avoid possibly confused deputy, the capability of the opener must be checked.

---
 kernel/cgroup/cgroup-v1.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/cgroup/cgroup-v1.c +4 -3
index 0e877dbcfeea..afc6c0e9c966 100644
@@ -546,6 +546,7 @@ static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
                                          char *buf, size_t nbytes, loff_t off)
 {
        struct cgroup *cgrp;
+       struct cgroup_file_ctx *ctx;
 
        BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
 
@@ -553,8 +554,9 @@ static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
         * Release agent gets called with all capabilities,
         * require capabilities to set release agent.
         */
-       if ((of->file->f_cred->user_ns != &init_user_ns) ||
-           !capable(CAP_SYS_ADMIN))
+       ctx = of->priv;
+       if ((ctx->ns->user_ns != &init_user_ns) ||
+           !file_ns_capable(of->file, &init_user_ns, CAP_SYS_ADMIN))
                return -EPERM;
 
        cgrp = cgroup_kn_lock_live(of->kn, false);
--
Quoted 1 line

2.34.1

Thank you. Looks good to me.

maintainer Applied
#2 lore raw
In reply to Michal Koutný
Quoted 10 lines · Michal Koutný

The idea is to check: a) the owning user_ns of cgroup_ns, b) capabilities in init_user_ns.

The commit 24f600856418 ("cgroup-v1: Require capabilities to set release_agent") got this wrong in the write handler of release_agent since it checked user_ns of the opener (may be different from the owning user_ns of cgroup_ns).
Secondly, to avoid possibly confused deputy, the capability of the opener must be checked.

Quoted 4 lines
Fixes 24f600856418 ("cgroup-v1: Require capabilities to set release_agent")

Applied to cgroup/for-5.17-fixes.

Thanks.

Signature · 1 line
tejun