Skip to content

Commit ee7eb12

Browse files
Thadeu Lima de Souza CascardoBrian Maly
authored andcommitted
fs/binfmt_misc.c: do not allow offset overflow
commit 5cc41e0 upstream. WHen registering a new binfmt_misc handler, it is possible to overflow the offset to get a negative value, which might crash the system, or possibly leak kernel data. Here is a crash log when 2500000000 was used as an offset: BUG: unable to handle kernel paging request at ffff989cfd6edca0 IP: load_misc_binary+0x22b/0x470 [binfmt_misc] PGD 1ef3e067 P4D 1ef3e067 PUD 0 Oops: 0000 [#1] SMP NOPTI Modules linked in: binfmt_misc kvm_intel ppdev kvm irqbypass joydev input_leds serio_raw mac_hid parport_pc qemu_fw_cfg parpy CPU: 0 PID: 2499 Comm: bash Not tainted 4.15.0-22-generic #24-Ubuntu Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.11.1-1 04/01/2014 RIP: 0010:load_misc_binary+0x22b/0x470 [binfmt_misc] Call Trace: search_binary_handler+0x97/0x1d0 do_execveat_common.isra.34+0x667/0x810 SyS_execve+0x31/0x40 do_syscall_64+0x73/0x130 entry_SYSCALL_64_after_hwframe+0x3d/0xa2 Use kstrtoint instead of simple_strtoul. It will work as the code already set the delimiter byte to '\0' and we only do it when the field is not empty. Tested with offsets -1, 2500000000, UINT_MAX and INT_MAX. Also tested with examples documented at Documentation/admin-guide/binfmt-misc.rst and other registrations from packages on Ubuntu. Link: http://lkml.kernel.org/r/[email protected] Fixes: 1da177e ("Linux-2.6.12-rc2") Signed-off-by: Thadeu Lima de Souza Cascardo <[email protected]> Reviewed-by: Andrew Morton <[email protected]> Cc: Alexander Viro <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> (cherry picked from commit 250edf9) Orabug: 31588258 Signed-off-by: Sherry Yang <[email protected]> Reviewed-by: Somasundaram Krishnasamy <[email protected]> Signed-off-by: Brian Maly <[email protected]>
1 parent 5827713 commit ee7eb12

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

fs/binfmt_misc.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,13 @@ static Node *create_entry(const char __user *buffer, size_t count)
369369
s = strchr(p, del);
370370
if (!s)
371371
goto einval;
372-
*s++ = '\0';
373-
e->offset = simple_strtoul(p, &p, 10);
372+
*s = '\0';
373+
if (p != s) {
374+
int r = kstrtoint(p, 10, &e->offset);
375+
if (r != 0 || e->offset < 0)
376+
goto einval;
377+
}
378+
p = s;
374379
if (*p++)
375380
goto einval;
376381
pr_debug("register: offset: %#x\n", e->offset);
@@ -410,7 +415,8 @@ static Node *create_entry(const char __user *buffer, size_t count)
410415
if (e->mask &&
411416
string_unescape_inplace(e->mask, UNESCAPE_HEX) != e->size)
412417
goto einval;
413-
if (e->size + e->offset > BINPRM_BUF_SIZE)
418+
if (e->size > BINPRM_BUF_SIZE ||
419+
BINPRM_BUF_SIZE - e->size < e->offset)
414420
goto einval;
415421
pr_debug("register: magic/mask length: %i\n", e->size);
416422
if (USE_DEBUG) {

0 commit comments

Comments
 (0)