Skip to content

Commit 2c61b30

Browse files
stephensmalleygregkh
authored andcommitted
selinux: shrink critical section in sel_write_load()
commit 868f31e upstream. 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> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 9ba2a2c commit 2c61b30

1 file changed

Lines changed: 8 additions & 10 deletions

File tree

security/selinux/selinuxfs.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -594,34 +594,31 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,
594594
if (!count)
595595
return -EINVAL;
596596

597-
mutex_lock(&selinux_state.policy_mutex);
598-
599597
length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
600598
SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
601599
if (length)
602-
goto out;
600+
return length;
603601

604602
data = vmalloc(count);
605-
if (!data) {
606-
length = -ENOMEM;
607-
goto out;
608-
}
603+
if (!data)
604+
return -ENOMEM;
609605
if (copy_from_user(data, buf, count) != 0) {
610606
length = -EFAULT;
611607
goto out;
612608
}
613609

610+
mutex_lock(&selinux_state.policy_mutex);
614611
length = security_load_policy(data, count, &load_state);
615612
if (length) {
616613
pr_warn_ratelimited("SELinux: failed to load policy\n");
617-
goto out;
614+
goto out_unlock;
618615
}
619616
fsi = file_inode(file)->i_sb->s_fs_info;
620617
length = sel_make_policy_nodes(fsi, load_state.policy);
621618
if (length) {
622619
pr_warn_ratelimited("SELinux: failed to initialize selinuxfs\n");
623620
selinux_policy_cancel(&load_state);
624-
goto out;
621+
goto out_unlock;
625622
}
626623

627624
selinux_policy_commit(&load_state);
@@ -631,8 +628,9 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,
631628
from_kuid(&init_user_ns, audit_get_loginuid(current)),
632629
audit_get_sessionid(current));
633630

634-
out:
631+
out_unlock:
635632
mutex_unlock(&selinux_state.policy_mutex);
633+
out:
636634
vfree(data);
637635
return length;
638636
}

0 commit comments

Comments
 (0)