Skip to content

Commit 1e8e18f

Browse files
Zhen Leitorvalds
Zhen Lei
authored andcommitted
kasan: fix shadow_size calculation error in kasan_module_alloc
There is a special case that the size is "(N << KASAN_SHADOW_SCALE_SHIFT) Pages plus X", the value of X is [1, KASAN_SHADOW_SCALE_SIZE-1]. The operation "size >> KASAN_SHADOW_SCALE_SHIFT" will drop X, and the roundup operation can not retrieve the missed one page. For example: size=0x28006, PAGE_SIZE=0x1000, KASAN_SHADOW_SCALE_SHIFT=3, we will get shadow_size=0x5000, but actually we need 6 pages. shadow_size = round_up(size >> KASAN_SHADOW_SCALE_SHIFT, PAGE_SIZE); This can lead to a kernel crash when kasan is enabled and the value of mod->core_layout.size or mod->init_layout.size is like above. Because the shadow memory of X has not been allocated and mapped. move_module: ptr = module_alloc(mod->core_layout.size); ... memset(ptr, 0, mod->core_layout.size); //crashed Unable to handle kernel paging request at virtual address ffff0fffff97b000 ...... Call trace: __asan_storeN+0x174/0x1a8 memset+0x24/0x48 layout_and_allocate+0xcd8/0x1800 load_module+0x190/0x23e8 SyS_finit_module+0x148/0x180 Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Zhen Lei <[email protected]> Reviewed-by: Dmitriy Vyukov <[email protected]> Acked-by: Andrey Ryabinin <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: Hanjun Guo <[email protected]> Cc: Libin <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 520495f commit 1e8e18f

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

mm/kasan/kasan.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,12 +619,13 @@ void kasan_kfree_large(void *ptr, unsigned long ip)
619619
int kasan_module_alloc(void *addr, size_t size)
620620
{
621621
void *ret;
622+
size_t scaled_size;
622623
size_t shadow_size;
623624
unsigned long shadow_start;
624625

625626
shadow_start = (unsigned long)kasan_mem_to_shadow(addr);
626-
shadow_size = round_up(size >> KASAN_SHADOW_SCALE_SHIFT,
627-
PAGE_SIZE);
627+
scaled_size = (size + KASAN_SHADOW_MASK) >> KASAN_SHADOW_SCALE_SHIFT;
628+
shadow_size = round_up(scaled_size, PAGE_SIZE);
628629

629630
if (WARN_ON(!PAGE_ALIGNED(shadow_start)))
630631
return -EINVAL;

0 commit comments

Comments
 (0)