]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selinux: shrink critical section in sel_write_load()
authorStephen Smalley <stephen.smalley.work@gmail.com>
Thu, 30 Apr 2026 18:36:52 +0000 (14:36 -0400)
committerPaul Moore <paul@paul-moore.com>
Tue, 5 May 2026 20:02:28 +0000 (16:02 -0400)
Currently sel_write_load() takes the policy mutex earlier than
necessary. Move the taking of the mutex later. This avoids
holding it unnecessarily across the vmalloc() and copy_from_user()
of the policy data.

Cc: stable@vger.kernel.org
Signed-off-by: Stephen Smalley <stephen.smalley.work@gmail.com>
Signed-off-by: Paul Moore <paul@paul-moore.com>
security/selinux/selinuxfs.c

index a43a38a3ae25beef82aacddace2e0eb17db3c3fa..25ca7d71401440338846fc2470513ac937079e1b 100644 (file)
@@ -553,34 +553,31 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,
        if (!count)
                return -EINVAL;
 
-       mutex_lock(&selinux_state.policy_mutex);
-
        length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
                              SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
        if (length)
-               goto out;
+               return length;
 
        data = vmalloc(count);
-       if (!data) {
-               length = -ENOMEM;
-               goto out;
-       }
+       if (!data)
+               return -ENOMEM;
        if (copy_from_user(data, buf, count) != 0) {
                length = -EFAULT;
                goto out;
        }
 
+       mutex_lock(&selinux_state.policy_mutex);
        length = security_load_policy(data, count, &load_state);
        if (length) {
                pr_warn_ratelimited("SELinux: failed to load policy\n");
-               goto out;
+               goto out_unlock;
        }
        fsi = file_inode(file)->i_sb->s_fs_info;
        length = sel_make_policy_nodes(fsi, load_state.policy);
        if (length) {
                pr_warn_ratelimited("SELinux: failed to initialize selinuxfs\n");
                selinux_policy_cancel(&load_state);
-               goto out;
+               goto out_unlock;
        }
 
        selinux_policy_commit(&load_state);
@@ -590,8 +587,9 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,
                from_kuid(&init_user_ns, audit_get_loginuid(current)),
                audit_get_sessionid(current));
 
-out:
+out_unlock:
        mutex_unlock(&selinux_state.policy_mutex);
+out:
        vfree(data);
        return length;
 }