Skip to content

Commit 42c7732

Browse files
committed
selinux: improve error checking in sel_write_load()
Move our existing input sanity checking to the top of sel_write_load() and add a check to ensure the buffer size is non-zero. Move a local variable initialization from the declaration to before it is used. Minor style adjustments. Reported-by: Sam Sun <[email protected]> Signed-off-by: Paul Moore <[email protected]>
1 parent e6b5ebc commit 42c7732

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

security/selinux/selinuxfs.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -571,38 +571,41 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,
571571
size_t count, loff_t *ppos)
572572

573573
{
574-
struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
574+
struct selinux_fs_info *fsi;
575575
struct selinux_load_state load_state;
576576
ssize_t length;
577577
void *data = NULL;
578578

579+
/* no partial writes */
580+
if (*ppos)
581+
return -EINVAL;
582+
/* no empty policies */
583+
if (!count)
584+
return -EINVAL;
585+
579586
mutex_lock(&selinux_state.policy_mutex);
580587

581588
length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
582589
SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
583590
if (length)
584591
goto out;
585592

586-
/* No partial writes. */
587-
length = -EINVAL;
588-
if (*ppos != 0)
589-
goto out;
590-
591-
length = -ENOMEM;
592593
data = vmalloc(count);
593-
if (!data)
594+
if (!data) {
595+
length = -ENOMEM;
594596
goto out;
595-
596-
length = -EFAULT;
597-
if (copy_from_user(data, buf, count) != 0)
597+
}
598+
if (copy_from_user(data, buf, count) != 0) {
599+
length = -EFAULT;
598600
goto out;
601+
}
599602

600603
length = security_load_policy(data, count, &load_state);
601604
if (length) {
602605
pr_warn_ratelimited("SELinux: failed to load policy\n");
603606
goto out;
604607
}
605-
608+
fsi = file_inode(file)->i_sb->s_fs_info;
606609
length = sel_make_policy_nodes(fsi, load_state.policy);
607610
if (length) {
608611
pr_warn_ratelimited("SELinux: failed to initialize selinuxfs\n");
@@ -611,13 +614,12 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,
611614
}
612615

613616
selinux_policy_commit(&load_state);
614-
615617
length = count;
616-
617618
audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
618619
"auid=%u ses=%u lsm=selinux res=1",
619620
from_kuid(&init_user_ns, audit_get_loginuid(current)),
620621
audit_get_sessionid(current));
622+
621623
out:
622624
mutex_unlock(&selinux_state.policy_mutex);
623625
vfree(data);

0 commit comments

Comments
 (0)