Skip to content

Commit 6509d77

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 3bdbb95 commit 6509d77

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
@@ -583,34 +583,31 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,
583583
if (!count)
584584
return -EINVAL;
585585

586-
mutex_lock(&selinux_state.policy_mutex);
587-
588586
length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
589587
SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
590588
if (length)
591-
goto out;
589+
return length;
592590

593591
data = vmalloc(count);
594-
if (!data) {
595-
length = -ENOMEM;
596-
goto out;
597-
}
592+
if (!data)
593+
return -ENOMEM;
598594
if (copy_from_user(data, buf, count) != 0) {
599595
length = -EFAULT;
600596
goto out;
601597
}
602598

599+
mutex_lock(&selinux_state.policy_mutex);
603600
length = security_load_policy(data, count, &load_state);
604601
if (length) {
605602
pr_warn_ratelimited("SELinux: failed to load policy\n");
606-
goto out;
603+
goto out_unlock;
607604
}
608605
fsi = file_inode(file)->i_sb->s_fs_info;
609606
length = sel_make_policy_nodes(fsi, load_state.policy);
610607
if (length) {
611608
pr_warn_ratelimited("SELinux: failed to initialize selinuxfs\n");
612609
selinux_policy_cancel(&load_state);
613-
goto out;
610+
goto out_unlock;
614611
}
615612

616613
selinux_policy_commit(&load_state);
@@ -620,8 +617,9 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,
620617
from_kuid(&init_user_ns, audit_get_loginuid(current)),
621618
audit_get_sessionid(current));
622619

623-
out:
620+
out_unlock:
624621
mutex_unlock(&selinux_state.policy_mutex);
622+
out:
625623
vfree(data);
626624
return length;
627625
}

0 commit comments

Comments
 (0)