Skip to content

Commit ef28df5

Browse files
committed
selinux: ensure the context is NUL terminated in security_context_to_sid_core()
The syzbot/syzkaller automated tests found a problem in security_context_to_sid_core() during early boot (before we load the SELinux policy) where we could potentially feed context strings without NUL terminators into the strcmp() function. We already guard against this during normal operation (after the SELinux policy has been loaded) by making a copy of the context strings and explicitly adding a NUL terminator to the end. The patch extends this protection to the early boot case (no loaded policy) by moving the context copy earlier in security_context_to_sid_core(). Reported-by: syzbot <syzkaller@googlegroups.com> Signed-off-by: Paul Moore <paul@paul-moore.com> Reviewed-By: William Roberts <william.c.roberts@intel.com>
1 parent 4f0753e commit ef28df5

1 file changed

Lines changed: 8 additions & 10 deletions

File tree

‎security/selinux/ss/services.c‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,27 +1413,25 @@ static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
14131413
if (!scontext_len)
14141414
return -EINVAL;
14151415

1416+
/* Copy the string to allow changes and ensure a NUL terminator */
1417+
scontext2 = kmemdup_nul(scontext, scontext_len, gfp_flags);
1418+
if (!scontext2)
1419+
return -ENOMEM;
1420+
14161421
if (!ss_initialized) {
14171422
int i;
14181423

14191424
for (i = 1; i < SECINITSID_NUM; i++) {
1420-
if (!strcmp(initial_sid_to_string[i], scontext)) {
1425+
if (!strcmp(initial_sid_to_string[i], scontext2)) {
14211426
*sid = i;
1422-
return 0;
1427+
goto out;
14231428
}
14241429
}
14251430
*sid = SECINITSID_KERNEL;
1426-
return 0;
1431+
goto out;
14271432
}
14281433
*sid = SECSID_NULL;
14291434

1430-
/* Copy the string so that we can modify the copy as we parse it. */
1431-
scontext2 = kmalloc(scontext_len + 1, gfp_flags);
1432-
if (!scontext2)
1433-
return -ENOMEM;
1434-
memcpy(scontext2, scontext, scontext_len);
1435-
scontext2[scontext_len] = 0;
1436-
14371435
if (force) {
14381436
/* Save another copy for storing in uninterpreted form */
14391437
rc = -ENOMEM;

0 commit comments

Comments
 (0)