]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
cgroup: use credential guards in cgroup_attach_permissions()
authorChristian Brauner <brauner@kernel.org>
Mon, 3 Nov 2025 11:27:03 +0000 (12:27 +0100)
committerChristian Brauner <brauner@kernel.org>
Tue, 4 Nov 2025 11:36:50 +0000 (12:36 +0100)
Use credential guards for scoped credential override with automatic
restoration on scope exit.

Link: https://patch.msgid.link/20251103-work-creds-guards-simple-v1-15-a3e156839e7f@kernel.org
Reviewed-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>
kernel/cgroup/cgroup.c

index fdee387f0d6be40b0b79987d23cdc9981afb4226..9f61f7cfc8d172d9b24b55f11c6832ffef80bfb9 100644 (file)
@@ -5363,7 +5363,6 @@ static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
        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;
        enum cgroup_attach_lock_mode lock_mode;
 
@@ -5386,11 +5385,10 @@ static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
         * 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,
-                                       threadgroup, ctx->ns);
-       revert_creds(saved_cred);
+       scoped_with_creds(of->file->f_cred)
+               ret = cgroup_attach_permissions(src_cgrp, dst_cgrp,
+                                               of->file->f_path.dentry->d_sb,
+                                               threadgroup, ctx->ns);
        if (ret)
                goto out_finish;