Skip to content

Commit 9b47d4e

Browse files
aryabininakpm00
authored andcommitted
mm/kasan: fix KASAN poisoning in vrealloc()
A KASAN warning can be triggered when vrealloc() changes the requested size to a value that is not aligned to KASAN_GRANULE_SIZE. ------------[ cut here ]------------ WARNING: CPU: 2 PID: 1 at mm/kasan/shadow.c:174 kasan_unpoison+0x40/0x48 ... pc : kasan_unpoison+0x40/0x48 lr : __kasan_unpoison_vmalloc+0x40/0x68 Call trace: kasan_unpoison+0x40/0x48 (P) vrealloc_node_align_noprof+0x200/0x320 bpf_patch_insn_data+0x90/0x2f0 convert_ctx_accesses+0x8c0/0x1158 bpf_check+0x1488/0x1900 bpf_prog_load+0xd20/0x1258 __sys_bpf+0x96c/0xdf0 __arm64_sys_bpf+0x50/0xa0 invoke_syscall+0x90/0x160 Introduce a dedicated kasan_vrealloc() helper that centralizes KASAN handling for vmalloc reallocations. The helper accounts for KASAN granule alignment when growing or shrinking an allocation and ensures that partial granules are handled correctly. Use this helper from vrealloc_node_align_noprof() to fix poisoning logic. [ryabinin.a.a@gmail.com: move kasan_enabled() check, fix build] Link: https://lkml.kernel.org/r/20260119144509.32767-1-ryabinin.a.a@gmail.com Link: https://lkml.kernel.org/r/20260113191516.31015-1-ryabinin.a.a@gmail.com Fixes: d699440 ("mm: fix vrealloc()'s KASAN poisoning logic") Signed-off-by: Andrey Ryabinin <ryabinin.a.a@gmail.com> Reported-by: Maciej Żenczykowski <maze@google.com> Reported-by: <joonki.min@samsung-slsi.corp-partner.google.com> Closes: https://lkml.kernel.org/r/CANP3RGeuRW53vukDy7WDO3FiVgu34-xVJYkfpm08oLO3odYFrA@mail.gmail.com Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com> Tested-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com> Cc: Alexander Potapenko <glider@google.com> Cc: Dmitriy Vyukov <dvyukov@google.com> Cc: Dmitry Vyukov <dvyukov@google.com> Cc: Uladzislau Rezki <urezki@gmail.com> Cc: Vincenzo Frascino <vincenzo.frascino@arm.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 8a1968b commit 9b47d4e

3 files changed

Lines changed: 37 additions & 5 deletions

File tree

include/linux/kasan.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,17 @@ kasan_unpoison_vmap_areas(struct vm_struct **vms, int nr_vms,
641641
__kasan_unpoison_vmap_areas(vms, nr_vms, flags);
642642
}
643643

644+
void __kasan_vrealloc(const void *start, unsigned long old_size,
645+
unsigned long new_size);
646+
647+
static __always_inline void kasan_vrealloc(const void *start,
648+
unsigned long old_size,
649+
unsigned long new_size)
650+
{
651+
if (kasan_enabled())
652+
__kasan_vrealloc(start, old_size, new_size);
653+
}
654+
644655
#else /* CONFIG_KASAN_VMALLOC */
645656

646657
static inline void kasan_populate_early_vm_area_shadow(void *start,
@@ -670,6 +681,9 @@ kasan_unpoison_vmap_areas(struct vm_struct **vms, int nr_vms,
670681
kasan_vmalloc_flags_t flags)
671682
{ }
672683

684+
static inline void kasan_vrealloc(const void *start, unsigned long old_size,
685+
unsigned long new_size) { }
686+
673687
#endif /* CONFIG_KASAN_VMALLOC */
674688

675689
#if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \

mm/kasan/common.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,4 +606,25 @@ void __kasan_unpoison_vmap_areas(struct vm_struct **vms, int nr_vms,
606606
__kasan_unpoison_vmalloc(addr, size, flags | KASAN_VMALLOC_KEEP_TAG);
607607
}
608608
}
609+
610+
void __kasan_vrealloc(const void *addr, unsigned long old_size,
611+
unsigned long new_size)
612+
{
613+
if (new_size < old_size) {
614+
kasan_poison_last_granule(addr, new_size);
615+
616+
new_size = round_up(new_size, KASAN_GRANULE_SIZE);
617+
old_size = round_up(old_size, KASAN_GRANULE_SIZE);
618+
if (new_size < old_size)
619+
__kasan_poison_vmalloc(addr + new_size,
620+
old_size - new_size);
621+
} else if (new_size > old_size) {
622+
old_size = round_down(old_size, KASAN_GRANULE_SIZE);
623+
__kasan_unpoison_vmalloc(addr + old_size,
624+
new_size - old_size,
625+
KASAN_VMALLOC_PROT_NORMAL |
626+
KASAN_VMALLOC_VM_ALLOC |
627+
KASAN_VMALLOC_KEEP_TAG);
628+
}
629+
}
609630
#endif

mm/vmalloc.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4322,24 +4322,21 @@ void *vrealloc_node_align_noprof(const void *p, size_t size, unsigned long align
43224322
if (want_init_on_free() || want_init_on_alloc(flags))
43234323
memset((void *)p + size, 0, old_size - size);
43244324
vm->requested_size = size;
4325-
kasan_poison_vmalloc(p + size, old_size - size);
4325+
kasan_vrealloc(p, old_size, size);
43264326
return (void *)p;
43274327
}
43284328

43294329
/*
43304330
* We already have the bytes available in the allocation; use them.
43314331
*/
43324332
if (size <= alloced_size) {
4333-
kasan_unpoison_vmalloc(p + old_size, size - old_size,
4334-
KASAN_VMALLOC_PROT_NORMAL |
4335-
KASAN_VMALLOC_VM_ALLOC |
4336-
KASAN_VMALLOC_KEEP_TAG);
43374333
/*
43384334
* No need to zero memory here, as unused memory will have
43394335
* already been zeroed at initial allocation time or during
43404336
* realloc shrink time.
43414337
*/
43424338
vm->requested_size = size;
4339+
kasan_vrealloc(p, old_size, size);
43434340
return (void *)p;
43444341
}
43454342

0 commit comments

Comments
 (0)