[Linux Kernel Bug] UBSAN: shift-out-of-bounds in fault_around_bytes_set

From: Sam Sun
Date: Fri Mar 01 2024 - 02:43:34 EST


Dear developers and maintainers,

We found a shift-out-of-bounds bug in mm/memory.c. Kernel commit is b401b621758.
Kernel config and C repro are attached to this email.
UBSAN report is listed below.
```
UBSAN: shift-out-of-bounds in /home/sy/linux-original/include/linux/log2.h:67:13
shift exponent 4294967295 is too large for 64-bit type 'long unsigned int'
CPU: 0 PID: 8091 Comm: syz-executor371 Not tainted 6.7.0-rc7 #1
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014
Call Trace:
<TASK>
__dump_stack lib/dump_stack.c:88 [inline]
dump_stack_lvl+0x136/0x150 lib/dump_stack.c:106
ubsan_epilogue lib/ubsan.c:217 [inline]
__ubsan_handle_shift_out_of_bounds+0x24b/0x430 lib/ubsan.c:387
__rounddown_pow_of_two include/linux/log2.h:67 [inline]
fault_around_bytes_set.cold+0x19/0x1e mm/memory.c:4527
simple_attr_write_xsigned.constprop.0.isra.0+0x1ed/0x2d0 fs/libfs.c:1301
debugfs_attr_write_xsigned fs/debugfs/file.c:485 [inline]
debugfs_attr_write+0x74/0xa0 fs/debugfs/file.c:493
vfs_write+0x2a9/0xd80 fs/read_write.c:582
ksys_write+0x122/0x250 fs/read_write.c:637
do_syscall_x64 arch/x86/entry/common.c:52 [inline]
do_syscall_64+0x40/0x110 arch/x86/entry/common.c:83
entry_SYSCALL_64_after_hwframe+0x63/0x6b
RIP: 0033:0x7fa30d5d7fcd
Code: 28 c3 e8 46 1e 00 00 66 0f 1f 44 00 00 f3 0f 1e fa 48 89 f8 48
89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d
01 f0 ff ff 73 01 c3 48 c7 c1 b8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007ffc8b7ee1b8 EFLAGS: 00000246 ORIG_RAX: 0000000000000001
RAX: ffffffffffffffda RBX: 00007ffc8b7ee3b8 RCX: 00007fa30d5d7fcd
RDX: 0000000000000002 RSI: 0000000020000040 RDI: 0000000000000003
RBP: 0000000000000001 R08: 0000000000000000 R09: 00007ffc8b7ee3b8
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000001
R13: 00007ffc8b7ee3a8 R14: 00007fa30d655530 R15: 0000000000000001
</TASK>
================================================================================
```
In function simple_attr_write_xsigned, a user controlled string "buf"
is copied and
turned to long type by function "kstrtoll". If buf is "0", val passed
to function
fault_around_bytes_set is 0, which would trigger shift-out-of-bound bug.

If you have any questions, please contact us.
Reported by Yue Sun <samsun1006219@xxxxxxxxx>

Best Regards,
Yue

Attachment: config
Description: Binary data

#define _GNU_SOURCE

#include <endian.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>

uint64_t r[1] = {0xffffffffffffffff};

int main(void)
{
syscall(__NR_mmap, /*addr=*/0x1ffff000ul, /*len=*/0x1000ul, /*prot=*/0ul,
/*flags=*/0x32ul, /*fd=*/-1, /*offset=*/0ul);
syscall(__NR_mmap, /*addr=*/0x20000000ul, /*len=*/0x1000000ul, /*prot=*/7ul,
/*flags=*/0x32ul, /*fd=*/-1, /*offset=*/0ul);
syscall(__NR_mmap, /*addr=*/0x21000000ul, /*len=*/0x1000ul, /*prot=*/0ul,
/*flags=*/0x32ul, /*fd=*/-1, /*offset=*/0ul);
intptr_t res = 0;
memcpy((void*)0x20000000, "/sys/kernel/debug/fault_around_bytes\000", 37);
res = syscall(__NR_openat, /*fd=*/0xffffffffffffff9cul, /*file=*/0x20000000ul,
/*flags=*/1ul, /*mode=*/0ul);
if (res != -1)
r[0] = res;
memcpy((void*)0x20000040, "0\000", 2);
syscall(__NR_write, /*fd=*/r[0], /*val=*/0x20000040ul, /*len=*/2ul);
return 0;
}