BUG: KASAN: vmalloc-out-of-bounds in vrealloc_noprof+0x195/0x220 at running fortify_kunit (v6.15-rc1, x86_64)
Subsystems (2)
Greetings!
I gave v6.15-rc1 a test ride on my Ryzen 5950 system with some debugging options turned on, getting a KASAN vmalloc-out-of-bounds hit at running fortify_kunit test:
[...]
TAP version 1
1..1
KTAP version 1
# Subtest: fortify
# module: fortify_kunit
1..26
ok 1 fortify_test_known_sizes
ok 2 fortify_test_control_flow_split
ok 3 fortify_test_alloc_size_kmalloc_const
ok 4 fortify_test_alloc_size_kmalloc_dynamic
ok 5 fortify_test_alloc_size_vmalloc_const
ok 6 fortify_test_alloc_size_vmalloc_dynamic
==================================================================
BUG: KASAN: vmalloc-out-of-bounds in vrealloc_noprof+0x195/0x220
Read of size 6291456 at addr ffffc90015c00000 by task kunit_try_catch/4334
CPU: 15 UID: 0 PID: 4334 Comm: kunit_try_catch Tainted: G N 6.15.0-rc1-Zen3 #6 PREEMPT
Tainted: [N]=TEST
Hardware name: To Be Filled By O.E.M. B550M Pro4/B550M Pro4, BIOS L3.46 08/20/2024
Call Trace:
<TASK>
dump_stack_lvl+0x2a/0x90
print_report+0x17a/0x520
? srso_alias_return_thunk+0x5/0xfbef5
? vrealloc_noprof+0x195/0x220
kasan_report+0xb9/0x100
? vrealloc_noprof+0x195/0x220
kasan_check_range+0x184/0x190
? vrealloc_noprof+0x195/0x220
__asan_memcpy+0x25/0x70
vrealloc_noprof+0x195/0x220
? fortify_test_alloc_size_kvmalloc_const+0x2eae/0x7170 [fortify_kunit]
fortify_test_alloc_size_kvmalloc_const+0x2eae/0x7170 [fortify_kunit]
kunit_try_run_case+0x119/0x340 [kunit]
? kunit_cleanup+0x120/0x120 [kunit]
kunit_generic_run_threadfn_adapter+0x73/0x100 [kunit]
kthread+0x46a/0x570
? kunit_try_catch_run+0x620/0x620 [kunit]
? kthread_blkcg+0xb0/0xb0
ret_from_fork+0x3c/0x70
? kthread_blkcg+0xb0/0xb0
ret_from_fork_asm+0x11/0x20
</TASK>
The buggy address belongs to the virtual mapping at
[ffffc90015c00000, ffffc90016201000) created by:
fortify_test_alloc_size_kvmalloc_const+0x2dfb/0x7170 [fortify_kunit]
The buggy address belongs to the physical page:
page: refcount:1 mapcount:0 mapping:0000000000000000 index:0x7f281927d pfn:0x128600
flags: 0x4000000000000000(zone=1)
raw: 4000000000000000 0000000000000000 dead000000000122 0000000000000000
raw: 00000007f281927d 0000000000000000 00000001ffffffff 0000000000000000
page dumped because: kasan: bad access detected
Memory state around the buggy address:
ffffc90016000f00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
ffffc90016000f80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
>ffffc90016001000: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8
^
ffffc90016001080: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8
ffffc90016001100: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8
==================================================================Disabling lock debugging due to kernel taint
not ok 7 fortify_test_alloc_size_kvmalloc_const ok 8 fortify_test_alloc_size_kvmalloc_dynamic ok 9 fortify_test_alloc_size_devm_kmalloc_const ok 10 fortify_test_alloc_size_devm_kmalloc_dynamic ok 11 fortify_test_realloc_size
ok 12 fortify_test_strlen
ok 13 fortify_test_strnlen
ok 14 fortify_test_strcpy
ok 15 fortify_test_strncpy
ok 16 fortify_test_strscpy
ok 17 fortify_test_strcat
ok 18 fortify_test_strncat
ok 19 fortify_test_strlcat
ok 20 fortify_test_memcpy
ok 21 fortify_test_memmove
ok 22 fortify_test_memscan
ok 23 fortify_test_memchr
ok 24 fortify_test_memchr_inv
ok 25 fortify_test_memcmp
ok 26 fortify_test_kmemdup
# fortify: pass:25 fail:1 skip:0 total:26
# Totals: pass:25 fail:1 skip:0 total:26
not ok 1 fortify
Kernel .config attached.
Regards,
Erhard
Greetings!
fortify_test_alloc_size_kvmalloc_const test failure still in v6.15-rc3, also with a 'GCC14 -O2'-built kernel:
==================================================================
BUG: KASAN: vmalloc-out-of-bounds in vrealloc_noprof+0x2a2/0x370
Read of size 6291456 at addr ffffc9000e200000 by task kunit_try_catch/4317
CPU: 21 UID: 0 PID: 4317 Comm: kunit_try_catch Tainted: G N 6.15.0-rc3-Zen3 #11 PREEMPT
Tainted: [N]=TEST
Hardware name: To Be Filled By O.E.M. B550M Pro4/B550M Pro4, BIOS L3.46 08/20/2024
Call Trace:
<TASK>
dump_stack_lvl+0x4a/0x70
print_report+0x132/0x4e0
? __rwlock_init+0x120/0x120
? vrealloc_noprof+0x2a2/0x370
kasan_report+0xd9/0x110
? vrealloc_noprof+0x2a2/0x370
? fortify_test_alloc_size_kvmalloc_const+0x4892/0xa3d0 [fortify_kunit]
kasan_check_range+0x113/0x210
__asan_memcpy+0x1f/0x70
vrealloc_noprof+0x2a2/0x370
? srso_alias_return_thunk+0x5/0xfbef5
fortify_test_alloc_size_kvmalloc_const+0x4892/0xa3d0 [fortify_kunit]
? fortify_test_alloc_size_vmalloc_const+0x2a30/0x2a30 [fortify_kunit]
? srso_alias_return_thunk+0x5/0xfbef5
? srso_alias_return_thunk+0x5/0xfbef5
? ktime_get_ts64+0x7a/0x220
? fortify_test_init+0x2be/0x460 [fortify_kunit]
kunit_try_run_case+0x199/0x2b0 [kunit]
? kunit_try_run_case_cleanup+0xe0/0xe0 [kunit]
? srso_alias_return_thunk+0x5/0xfbef5
? do_raw_spin_unlock+0x4f/0x220
? kunit_try_run_case_cleanup+0xe0/0xe0 [kunit]
? kunit_mem_assert_format+0x460/0x460 [kunit]
kunit_generic_run_threadfn_adapter+0x7b/0xe0 [kunit]
kthread+0x349/0x6c0
? kthread_is_per_cpu+0xd0/0xd0
? kthread_is_per_cpu+0xd0/0xd0
? kthread_is_per_cpu+0xd0/0xd0
ret_from_fork+0x2b/0x70
? kthread_is_per_cpu+0xd0/0xd0
ret_from_fork_asm+0x11/0x20
</TASK>
The buggy address belongs to the virtual mapping at
[ffffc9000e200000, ffffc9000e801000) created by:
fortify_test_alloc_size_kvmalloc_const+0x4788/0xa3d0 [fortify_kunit]
The buggy address belongs to the physical page:
page: refcount:1 mapcount:0 mapping:0000000000000000 index:0x7fab41f0a pfn:0x128000
flags: 0x4000000000000000(zone=1)
raw: 4000000000000000 0000000000000000 dead000000000122 0000000000000000
raw: 00000007fab41f0a 0000000000000000 00000001ffffffff 0000000000000000
page dumped because: kasan: bad access detected
Memory state around the buggy address:
ffffc9000e600f00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
ffffc9000e600f80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
>ffffc9000e601000: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8
^
ffffc9000e601080: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8
ffffc9000e601100: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8
==================================================================Disabling lock debugging due to kernel taint
not ok 7 fortify_test_alloc_size_kvmalloc_const
[...]
Regards,
Erhard
Quoted 95 lines · On Tue, 8 Apr 2025 19:25:03 +0200 Erhard Furtner <[email protected]> wrote:
Greetings!
I gave v6.15-rc1 a test ride on my Ryzen 5950 system with some debugging options turned on, getting a KASAN vmalloc-out-of-bounds hit at running
fortify_kunittest:[...]
TAP version 1
1..1
KTAP version 1
# Subtest: fortify
# module:fortify_kunit
1..26
ok 1fortify_test_known_sizes
ok 2fortify_test_control_flow_split
ok 3fortify_test_alloc_size_kmalloc_const
ok 4fortify_test_alloc_size_kmalloc_dynamic
ok 5fortify_test_alloc_size_vmalloc_const
ok 6fortify_test_alloc_size_vmalloc_dynamic
==================================================================BUG: KASAN: vmalloc-out-of-bounds in vrealloc_noprof+0x195/0x220 Read of size 6291456 at addr ffffc90015c00000 by task kunit_try_catch/4334 CPU: 15 UID: 0 PID: 4334 Comm: kunit_try_catch Tainted: G N 6.15.0-rc1-Zen3 #6 PREEMPT Tainted: [N]=TEST Hardware name: To Be Filled By O.E.M. B550M Pro4/B550M Pro4, BIOS L3.46 08/20/2024 Call Trace: <TASK> dump_stack_lvl+0x2a/0x90 print_report+0x17a/0x520 ? srso_alias_return_thunk+0x5/0xfbef5 ? vrealloc_noprof+0x195/0x220 kasan_report+0xb9/0x100 ? vrealloc_noprof+0x195/0x220 kasan_check_range+0x184/0x190 ? vrealloc_noprof+0x195/0x220 __asan_memcpy+0x25/0x70 vrealloc_noprof+0x195/0x220 ? fortify_test_alloc_size_kvmalloc_const+0x2eae/0x7170 [fortify_kunit] fortify_test_alloc_size_kvmalloc_const+0x2eae/0x7170 [fortify_kunit] kunit_try_run_case+0x119/0x340 [kunit] ? kunit_cleanup+0x120/0x120 [kunit] kunit_generic_run_threadfn_adapter+0x73/0x100 [kunit] kthread+0x46a/0x570 ? kunit_try_catch_run+0x620/0x620 [kunit] ? kthread_blkcg+0xb0/0xb0 ret_from_fork+0x3c/0x70 ? kthread_blkcg+0xb0/0xb0 ret_from_fork_asm+0x11/0x20 </TASK> The buggy address belongs to the virtual mapping at [ffffc90015c00000, ffffc90016201000) created by: fortify_test_alloc_size_kvmalloc_const+0x2dfb/0x7170 [fortify_kunit] The buggy address belongs to the physical page: page: refcount:1 mapcount:0 mapping:0000000000000000 index:0x7f281927d pfn:0x128600 flags: 0x4000000000000000(zone=1) raw: 4000000000000000 0000000000000000 dead000000000122 0000000000000000 raw: 00000007f281927d 0000000000000000 00000001ffffffff 0000000000000000 page dumped because: kasan: bad access detected Memory state around the buggy address: ffffc90016000f00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ffffc90016000f80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 >ffffc90016001000: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 ^ ffffc90016001080: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 ffffc90016001100: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 ==================================================================Disabling lock debugging due to kernel taint
not ok 7fortify_test_alloc_size_kvmalloc_constok 8fortify_test_alloc_size_kvmalloc_dynamicok 9fortify_test_alloc_size_devm_kmalloc_constok 10fortify_test_alloc_size_devm_kmalloc_dynamicok 11fortify_test_realloc_size
ok 12fortify_test_strlen
ok 13fortify_test_strnlen
ok 14fortify_test_strcpy
ok 15fortify_test_strncpy
ok 16fortify_test_strscpy
ok 17fortify_test_strcat
ok 18fortify_test_strncat
ok 19fortify_test_strlcat
ok 20fortify_test_memcpy
ok 21fortify_test_memmove
ok 22fortify_test_memscan
ok 23fortify_test_memchr
ok 24fortify_test_memchr_inv
ok 25fortify_test_memcmp
ok 26fortify_test_kmemdup
# fortify: pass:25 fail:1 skip:0 total:26
# Totals: pass:25 fail:1 skip:0 total:26
not ok 1 fortifyKernel .config attached.
Quoted 7 lines · Erhard Furtner
fortify_test_alloc_size_kvmalloc_consttest failure still in v6.15-rc3, also with a 'GCC14 -O2'-built kernel:
[...]BUG: KASAN: vmalloc-out-of-bounds in vrealloc_noprof+0x2a2/0x370[...]
not ok 7fortify_test_alloc_size_kvmalloc_const
[...]Quoted 1 line
I gave v6.15-rc1 a test ride on my Ryzen 5950 system with some debugging options turned on, getting a KASAN vmalloc-out-of-bounds hit at running
fortify_kunittest:
I'm not able to reproduce this yet. What does your .config look like?
I tried this on Linus's latest and next-20250422:
$ gcc --version gcc (GCC) 14.2.1 20250110 (Red Hat 14.2.1-7) ... $ ./tools/testing/kunit/kunit.py run \ --kconfig_add CONFIG_KASAN=y \ --kconfig_add CONFIG_KASAN_VMALLOC=y \ fortify
both showing passes:
[16:14:44] Testing complete. Ran 26 tests: passed: 26
What other debugging do you have enabled?
Signature · 1 line
Kees Cook
Quoted 21 lines · On Tue, 22 Apr 2025 09:50:24 -0700 Kees Cook <[email protected]> wrote:
Quoted 7 lines · Erhard Furtner
fortify_test_alloc_size_kvmalloc_consttest failure still in v6.15-rc3, also with a 'GCC14 -O2'-built kernel:
[...]BUG: KASAN: vmalloc-out-of-bounds in vrealloc_noprof+0x2a2/0x370[...]
not ok 7fortify_test_alloc_size_kvmalloc_const
[...]Quoted 1 line
I gave v6.15-rc1 a test ride on my Ryzen 5950 system with some debugging options turned on, getting a KASAN vmalloc-out-of-bounds hit at running
fortify_kunittest:I'm not able to reproduce this yet. What does your .config look like?
I tried this on Linus's latest and next-20250422:
$ gcc --version gcc (GCC) 14.2.1 20250110 (Red Hat 14.2.1-7) ... $ ./tools/testing/kunit/kunit.py run \ --kconfig_add CONFIG_KASAN=y \ --kconfig_add CONFIG_KASAN_VMALLOC=y \ fortify
Quoted 7 lines
both showing passes:
[16:14:44] Testing complete. Ran 26 tests: passed: 26What other debugging do you have enabled?
Signature · 1 line
Kees Cook
Hi!
Sorry, I forgot to attach my v6.15-rc3 kernel .config. It's basically the same as the -rc1 one from my first report (https://lore.kernel.org/all/[email protected]/), but with GCC 14 and -O2 instead of Clang 19 and -Os.
Regards,
Erhard
Quoted 10 lines · Erhard Furtner
Quoted 9 lines · On Tue, 22 Apr 2025 09:50:24 -0700 Kees Cook <[email protected]> wrote:
Quoted 7 lines · Erhard Furtner
fortify_test_alloc_size_kvmalloc_consttest failure still in v6.15-rc3, also with a 'GCC14 -O2'-built kernel:
[...]BUG: KASAN: vmalloc-out-of-bounds in vrealloc_noprof+0x2a2/0x370[...]
not ok 7fortify_test_alloc_size_kvmalloc_const
[...]Quoted 1 line
I gave v6.15-rc1 a test ride on my Ryzen 5950 system with some debugging options turned on, getting a KASAN vmalloc-out-of-bounds hit at running
fortify_kunittest:
Quoted 7 lines
Quoted 3 lines
I'm not able to reproduce this yet. What does your .config look like? [...]
What other debugging do you have enabled?Hi!
Sorry, I forgot to attach my v6.15-rc3 kernel .config. It's basically the same as the -rc1 one from my first report (https://lore.kernel.org/all/[email protected]/), but with GCC 14 and -O2 instead of Clang 19 and -Os.
Thanks! I was able to reproduce this by not using UML and forcing on CONFIG_FORTIFY_SOURCE=y:
(Bug #1: CONFIG_FORTIFY_SOURCE should be enabled by KUnit for x86_64)
$ ./tools/testing/kunit/kunit.py run --arch=x86_64 \ --kconfig_add CONFIG_KASAN=y \ --kconfig_add CONFIG_KASAN_VMALLOC=y \ --kconfig_add CONFIG_FORTIFY_SOURCE=y \ fortify ... [22:52:53] BUG: KASAN: vmalloc-out-of-bounds in vrealloc_noprof+0x1a4/0x200 [22:52:53] Read of size 6291456 at addr ffffc90000200000 by task kunit_try_catch/41
As it turns out, I had to go back to v6.11 before this passed again. Doing a git bisect lands me on commit 590b9d576cae ("mm: kvmalloc: align kvrealloc() with krealloc()"), which certainly sounds like something that might break the fortify_test_alloc_size_kvmalloc_const test. :)
$ ./scripts/faddr2line .kunit/vmlinux vrealloc_noprof+0x1a4/0x200 vrealloc_noprof+0x1a4/0x200: vrealloc_noprof at mm/vmalloc.c:4106
4104: if (p) {
4105: memcpy(n, p, old_size);
4106: vfree(p);
4107: }
This seems to think it's the vfree(), but I think this is off by a line and it's probably the memcpy()...
(Bug #2: KASAN is reporting the wrong crash location)
Looking at the commit, though, I think it is just exposing the use of vrealloc(), which was introduced in commit 3ddc2fefe6f3 ("mm: vmalloc:
implement vrealloc()").
The KUnit test is attempting to allocate these # of pages:
TEST_alloc(check_const, 1, 1); \
TEST_alloc(check_const, 128, 128); \
TEST_alloc(check_const, 1023, 1023); \
TEST_alloc(check_const, 1025, 1025); \
TEST_alloc(check_const, 4096, 4096); \
TEST_alloc(check_const, 4097, 4097); \The realloc test doubles it for the realloc:
checker(((expected_pages) * PAGE_SIZE) * 2, \
kvrealloc(orig, ((alloc_pages) * PAGE_SIZE) * 2, gfp), \
kvfree(p)); \So I'm not sure where the 6291456 bytes from KASAN is coming from -- that's not one of the potential sizes.
I bet this is KASAN precision vs get_vm_area_size(). i.e. KASAN marks the exact number of bytes requested ("size"):
area->addr = kasan_unpoison_vmalloc(area->addr, size, kasan_flags);
but the allocation in __get_vm_area_node() is going to round up:
size = ALIGN(size, 1ul << shift);
Instrumenting the test for some more details, and I can confirm:
[23:27:36] # fortify_test_alloc_size_kvmalloc_const: Allocated kmem for 0 bytes [23:27:36] # fortify_test_alloc_size_kvmalloc_const: Allocated kmem for 4096 bytes [23:27:36] # fortify_test_alloc_size_kvmalloc_const: Allocated kmem for 524288 bytes [23:27:36] # fortify_test_alloc_size_kvmalloc_const: Allocated kmem for 4190208 bytes [23:27:36] # fortify_test_alloc_size_kvmalloc_const: Allocated vma for 4198400 bytes, got 6291456 byte area (2093056 unused)
[23:27:36]
==================================================================
[23:27:36] BUG: KASAN: vmalloc-out-of-bounds in vrealloc_noprof+0x1a4/0x200 [23:27:36] Read of size 6291456 at addr ffffc90000200000 by task kunit_try_catch/41
Ta-da.
(Bug #3: vrealloc attempts to memcpy the entire area, even though only the originally requested size has been unpoisoned by KASAN)
I'm not sure what to do here, as KASAN is technically correct. Can we store the _requested_ allocation size somewhere in struct vm_struct ? This likely totally insufficient hack solves the crash, for example:
include/linux/vmalloc.h +1 -0
@@ -53,6 +53,7 @@ struct vm_struct {struct vm_struct *next;void *addr;unsigned long size; +unsigned long requested_size;unsigned long flags;struct page **pages;#ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
mm/vmalloc.c +2 -0
@@ -3859,6 +3859,7 @@ void *__vmalloc_node_range_noprof(unsigned long size, unsigned long align, kasan_flags |=KASAN_VMALLOC_INIT ;/* KASAN_VMALLOC_PROT_NORMAL already set if required. */ area->addr =kasan_unpoison_vmalloc (area->addr, size, kasan_flags); + area->requested_size = size;/* * In this function, newly allocated vm_struct has VM_UNINITIALIZED
@@ -4081,6 +4082,7 @@ void *vrealloc_noprof(const void *p, size_t size, gfp_t flags) } old_size =get_vm_area_size (vm); + old_size = vm->requested_size; }/*
-Kees
Signature · 1 line
Kees Cook