Skip to content

Commit acc599e

Browse files
pcmooregregkh
authored andcommitted
selinux: improve error checking in sel_write_load()
[ Upstream commit 42c7732 ] 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 <samsun1006219@gmail.com> Signed-off-by: Paul Moore <paul@paul-moore.com> [cascardo: keep fsi initialization at its declaration point as it is used earlier] [cascardo: keep check for 64MiB size limit] Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@igalia.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 7abd221 commit acc599e

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

security/selinux/selinuxfs.c

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,16 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,
536536
ssize_t length;
537537
void *data = NULL;
538538

539+
/* no partial writes */
540+
if (*ppos)
541+
return -EINVAL;
542+
/* no empty policies */
543+
if (!count)
544+
return -EINVAL;
545+
546+
if (count > 64 * 1024 * 1024)
547+
return -EFBIG;
548+
539549
mutex_lock(&fsi->mutex);
540550

541551
length = avc_has_perm(&selinux_state,
@@ -544,23 +554,15 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,
544554
if (length)
545555
goto out;
546556

547-
/* No partial writes. */
548-
length = -EINVAL;
549-
if (*ppos != 0)
550-
goto out;
551-
552-
length = -EFBIG;
553-
if (count > 64 * 1024 * 1024)
554-
goto out;
555-
556-
length = -ENOMEM;
557557
data = vmalloc(count);
558-
if (!data)
558+
if (!data) {
559+
length = -ENOMEM;
559560
goto out;
560-
561-
length = -EFAULT;
562-
if (copy_from_user(data, buf, count) != 0)
561+
}
562+
if (copy_from_user(data, buf, count) != 0) {
563+
length = -EFAULT;
563564
goto out;
565+
}
564566

565567
length = security_load_policy(fsi->state, data, count);
566568
if (length) {
@@ -579,6 +581,7 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,
579581
"auid=%u ses=%u lsm=selinux res=1",
580582
from_kuid(&init_user_ns, audit_get_loginuid(current)),
581583
audit_get_sessionid(current));
584+
582585
out:
583586
mutex_unlock(&fsi->mutex);
584587
vfree(data);

0 commit comments

Comments
 (0)