mm/kasan: Fix incorrect unpoisoning in vrealloc for KASAN
Subsystems (3)
PATCH 1/1 mm/kasan: Fix incorrect unpoisoning in vrealloc for KASAN
Syzkaller reported a memory out-of-bounds bug [1]. This patch fixes two issues:
1. In vrealloc, we were missing the KASAN_VMALLOC_VM_ALLOC flag when unpoisoning the extended region. This flag is required to correctly associate the allocation with KASAN's vmalloc tracking.
Note: In contrast, vzalloc (via __vmalloc_node_range_noprof) explicitly sets KASAN_VMALLOC_VM_ALLOC and calls kasan_unpoison_vmalloc() with it. vrealloc must behave consistently — especially when reusing existing vmalloc regions — to ensure KASAN can track allocations correctly.
2. When vrealloc reuses an existing vmalloc region (without allocating new pages), KASAN previously generated a new tag, which broke tag-based memory access tracking. We now add a 'reuse_tag' parameter to __kasan_unpoison_vmalloc() to preserve the original tag in such cases.
A new helper kasan_unpoison_vralloc() is introduced to handle this reuse scenario, ensuring consistent tag behavior during reallocation.
[1]: https://syzkaller.appspot.com/bug?extid=997752115a851cb0cf36
--- include/linux/kasan.h | 21 +++++++++++++++++++-- mm/kasan/hw_tags.c | 4 ++-- mm/kasan/shadow.c | 6 ++++-- mm/vmalloc.c | 4 ++-- 4 files changed, 27 insertions(+), 8 deletions(-)
include/linux/kasan.h +19 -2
@@ -612,13 +612,23 @@ static inline void kasan_release_vmalloc(unsigned long start,#endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */ void *__kasan_unpoison_vmalloc (const void *start,unsigned long size, -kasan_vmalloc_flags_t flags); +kasan_vmalloc_flags_t flags,bool reuse_tag); + +static __always_inline void *kasan_unpoison_vrealloc(const void *start, +unsigned long size, +kasan_vmalloc_flags_t flags) +{ +if (kasan_enabled ()) +return __kasan_unpoison_vmalloc (start, size, flags, true); +return (void *)start; +} + static __always_inline void *kasan_unpoison_vmalloc(const void *start,unsigned long size,kasan_vmalloc_flags_t flags) {if (kasan_enabled ()) -return __kasan_unpoison_vmalloc (start, size, flags); +return __kasan_unpoison_vmalloc (start, size, flags, false);return (void *)start; }
@@ -645,6 +655,13 @@ static inline void kasan_release_vmalloc(unsigned long start, unsigned long free_region_end, unsigned long flags) { } +static inline void *kasan_unpoison_vrealloc (const void *start, +unsigned long size, +kasan_vmalloc_flags_t flags) +{ +return (void *)start; +} +static inline void *kasan_unpoison_vmalloc (const void *start,unsigned long size,kasan_vmalloc_flags_t flags)
mm/kasan/hw_tags.c +2 -2
@@ -317,7 +317,7 @@ static void init_vmalloc_pages(const void *start, unsigned long size) }void *__kasan_unpoison_vmalloc (const void *start,unsigned long size, -kasan_vmalloc_flags_t flags) +kasan_vmalloc_flags_t flags,bool reuse_tag) {u8 tag;unsigned long redzone_start, redzone_size;
@@ -361,7 +361,7 @@ void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,return (void *)start; } - tag =kasan_random_tag (); + tag = reuse_tag ?get_tag (start) :kasan_random_tag (); start =set_tag (start, tag);/* Unpoison and initialize memory up to size. */
mm/kasan/shadow.c +4 -2
@@ -611,7 +611,7 @@ void __kasan_release_vmalloc(unsigned long start, unsigned long end, }void *__kasan_unpoison_vmalloc (const void *start,unsigned long size, -kasan_vmalloc_flags_t flags) +kasan_vmalloc_flags_t flags,bool reuse_tag) {/* * Software KASAN modes unpoison both VM_ALLOC and non-VM_ALLOC
@@ -631,7 +631,9 @@ void *__kasan_unpoison_vmalloc(const void *start, unsigned long size, !(flags & KASAN_VMALLOC_PROT_NORMAL))return (void *)start; - start =set_tag (start,kasan_random_tag ()); +if (!reuse_tag) + start =set_tag (start,kasan_random_tag ()); +kasan_unpoison (start, size, false);return (void *)start; }
mm/vmalloc.c +2 -2
@@ -4330,8 +4330,8 @@ void *vrealloc_node_align_noprof(const void *p, size_t size, unsigned long align* We already have the bytes available in the allocation; use them. */ if (size <= alloced_size) { -kasan_unpoison_vmalloc (p + old_size, size - old_size, -KASAN_VMALLOC_PROT_NORMAL ); +kasan_unpoison_vrealloc (p, size, +KASAN_VMALLOC_PROT_NORMAL |KASAN_VMALLOC_VM_ALLOC );/* * No need to zero memory here, as unused memory will have * already been zeroed at initial allocation time or during
Signature · 1 line
2.43.0
Hi, I'm working on [1]. As Andrew pointed out to me the patches are quite similar. I was wondering if you mind if the reuse_tag was an actual tag value? Instead of just bool toggling the usage of kasan_random_tag()?
I tested the problem I'm seeing, with your patch and the tags end up being reset. That's because the vms[area] pointers that I want to unpoison don't have a tag set, but generating a different random tag for each vms[] pointer crashes the kernel down the line. So __kasan_unpoison_vmalloc() needs to be called on each one but with the same tag.
Arguably I noticed my series also just resets the tags right now, but I'm working to correct it at the moment. I can send a fixed version tomorrow. Just wanted to ask if having __kasan_unpoison_vmalloc() set an actual predefined tag is a problem from your point of view?
[1] https://lore.kernel.org/all/[email protected]/
Quoted 142 lines · Jiayuan Chen
Syzkaller reported a memory out-of-bounds bug [1]. This patch fixes two issues:
1. In vrealloc, we were missing the
KASAN_VMALLOC_VM_ALLOCflag when unpoisoning the extended region. This flag is required to correctly associate the allocation with KASAN's vmalloc tracking.Note: In contrast, vzalloc (via
__vmalloc_node_range_noprof) explicitly setsKASAN_VMALLOC_VM_ALLOCand callskasan_unpoison_vmalloc()with it. vrealloc must behave consistently — especially when reusing existing vmalloc regions — to ensure KASAN can track allocations correctly.2. When vrealloc reuses an existing vmalloc region (without allocating new pages), KASAN previously generated a new tag, which broke tag-based memory access tracking. We now add a '
reuse_tag' parameter to__kasan_unpoison_vmalloc()to preserve the original tag in such cases.A new helper
kasan_unpoison_vralloc()is introduced to handle this reuse scenario, ensuring consistent tag behavior during reallocation.[1]: https://syzkaller.appspot.com/bug?extid=997752115a851cb0cf36
Fixes a0309faf1cb0 ("mm: vmalloc: support more granular vrealloc() sizing")Reported-by [email protected]Signed-off-by Jiayuan Chen <[email protected]>---
include/linux/kasan.h | 21 +++++++++++++++++++-- mm/kasan/hw_tags.c | 4 ++-- mm/kasan/shadow.c | 6 ++++-- mm/vmalloc.c | 4 ++-- 4 files changed, 27 insertions(+), 8 deletions(-)include/linux/kasan.h +19
-2@@ -612,13 +612,23 @@ static inline void kasan_release_vmalloc(unsigned long start,#endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */ void *__kasan_unpoison_vmalloc (const void *start,unsigned long size, -kasan_vmalloc_flags_t flags); +kasan_vmalloc_flags_t flags,bool reuse_tag); + +static __always_inline void *kasan_unpoison_vrealloc(const void *start, +unsigned long size, +kasan_vmalloc_flags_t flags) +{ +if (kasan_enabled ()) +return __kasan_unpoison_vmalloc (start, size, flags, true); +return (void *)start; +} + static __always_inline void *kasan_unpoison_vmalloc(const void *start,unsigned long size,kasan_vmalloc_flags_t flags) {if (kasan_enabled ()) -return __kasan_unpoison_vmalloc (start, size, flags); +return __kasan_unpoison_vmalloc (start, size, flags, false);return (void *)start; }@@ -645,6 +655,13 @@ static inline void kasan_release_vmalloc(unsigned long start, unsigned long free_region_end, unsigned long flags) { } +static inline void *kasan_unpoison_vrealloc (const void *start, +unsigned long size, +kasan_vmalloc_flags_t flags) +{ +return (void *)start; +} +static inline void *kasan_unpoison_vmalloc (const void *start,unsigned long size,kasan_vmalloc_flags_t flags)mm/kasan/hw_tags.c +2
-2@@ -317,7 +317,7 @@ static void init_vmalloc_pages(const void *start, unsigned long size) }void *__kasan_unpoison_vmalloc (const void *start,unsigned long size, -kasan_vmalloc_flags_t flags) +kasan_vmalloc_flags_t flags,bool reuse_tag) {u8 tag;unsigned long redzone_start, redzone_size;@@ -361,7 +361,7 @@ void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,return (void *)start; } - tag =kasan_random_tag (); + tag = reuse_tag ?get_tag (start) :kasan_random_tag (); start =set_tag (start, tag);/* Unpoison and initialize memory up to size. */ mm/kasan/shadow.c +4
-2@@ -611,7 +611,7 @@ void __kasan_release_vmalloc(unsigned long start, unsigned long end, }void *__kasan_unpoison_vmalloc (const void *start,unsigned long size, -kasan_vmalloc_flags_t flags) +kasan_vmalloc_flags_t flags,bool reuse_tag) {/* * Software KASAN modes unpoison both VM_ALLOC and non-VM_ALLOC @@ -631,7 +631,9 @@ void *__kasan_unpoison_vmalloc(const void *start, unsigned long size, !(flags & KASAN_VMALLOC_PROT_NORMAL))return (void *)start; - start =set_tag (start,kasan_random_tag ()); +if (!reuse_tag) + start =set_tag (start,kasan_random_tag ()); +kasan_unpoison (start, size, false);return (void *)start; }mm/vmalloc.c +2
-2@@ -4330,8 +4330,8 @@ void *vrealloc_node_align_noprof(const void *p, size_t size, unsigned long align* We already have the bytes available in the allocation; use them. */ if (size <= alloced_size) { -kasan_unpoison_vmalloc (p + old_size, size - old_size, -KASAN_VMALLOC_PROT_NORMAL ); +kasan_unpoison_vrealloc (p, size, +KASAN_VMALLOC_PROT_NORMAL |KASAN_VMALLOC_VM_ALLOC );/* * No need to zero memory here, as unused memory will have * already been zeroed at initial allocation time or during Signature · 1 line
2.43.0
Signature · 2 lines
Kind regards Maciej Wieczór-Retman
Quoted 11 lines · Maciej Wieczor-Retman
Hi, I'm working on [1]. As Andrew pointed out to me the patches are quite similar. I was wondering if you mind if the
reuse_tagwas an actual tag value? Instead of just bool toggling the usage ofkasan_random_tag()?I tested the problem I'm seeing, with your patch and the tags end up being reset. That's because the vms[area] pointers that I want to unpoison don't have a tag set, but generating a different random tag for each vms[] pointer crashes the kernel down the line. So
__kasan_unpoison_vmalloc()needs to be called on each one but with the same tag.
Quoted 7 lines
Arguably I noticed my series also just resets the tags right now, but I'm working to correct it at the moment. I can send a fixed version tomorrow. Just wanted to ask if having
__kasan_unpoison_vmalloc()set an actual predefined tag is a problem from your point of view?
Hi Maciej,
It seems we're focusing on different issues, but feel free to reuse or modify the 'reuse_tag'. It's intended to preserve the tag in one 'vma'.
I'd also be happy to help reproduce and test your changes to ensure the issue I encountered isn't regressed once you send a patch based on mine.
Thanks.
Quoted 17 lines · Jiayuan Chen
Syzkaller reported a memory out-of-bounds bug [1]. This patch fixes two issues:
1. In vrealloc, we were missing the
KASAN_VMALLOC_VM_ALLOCflag when unpoisoning the extended region. This flag is required to correctly associate the allocation with KASAN's vmalloc tracking.Note: In contrast, vzalloc (via
__vmalloc_node_range_noprof) explicitly setsKASAN_VMALLOC_VM_ALLOCand callskasan_unpoison_vmalloc()with it. vrealloc must behave consistently — especially when reusing existing vmalloc regions — to ensure KASAN can track allocations correctly.2. When vrealloc reuses an existing vmalloc region (without allocating new pages), KASAN previously generated a new tag, which broke tag-based memory access tracking. We now add a '
reuse_tag' parameter to__kasan_unpoison_vmalloc()to preserve the original tag in such cases.
Quoted 6 lines
A new helper
kasan_unpoison_vralloc()is introduced to handle this reuse scenario, ensuring consistent tag behavior during reallocation.[1]: https://syzkaller.appspot.com/bug?extid=997752115a851cb0cf36
Fixes a0309faf1cb0 ("mm: vmalloc: support more granular vrealloc() sizing")
Is this the right Fixes tag? I didn't change the kasan logic meaningfully in the above patch, perhaps it should be commit d699440f58ce ("mm:
fix vrealloc()'s KASAN poisoning logic")
Signature · 1 line
Kees Cook
Quoted 24 lines · Kees Cook
Quoted 21 lines · Jiayuan Chen
Syzkaller reported a memory out-of-bounds bug [1]. This patch fixes two
issues:1. In vrealloc, we were missing the
KASAN_VMALLOC_VM_ALLOCflag when unpoisoning the extended region. This flag is required to correctly associate the allocation with KASAN's vmalloc tracking.Note: In contrast, vzalloc (via
__vmalloc_node_range_noprof) explicitly setsKASAN_VMALLOC_VM_ALLOCand callskasan_unpoison_vmalloc()with it. vrealloc must behave consistently — especially when reusing existing vmalloc regions — to ensure KASAN can track allocations correctly.2. When vrealloc reuses an existing vmalloc region (without allocating new pages), KASAN previously generated a new tag, which broke tag-based memory access tracking. We now add a '
reuse_tag' parameter to__kasan_unpoison_vmalloc()to preserve the original tag in such cases.A new helper
kasan_unpoison_vralloc()is introduced to handle this reuse scenario, ensuring consistent tag behavior during reallocation.
Quoted 7 lines
Quoted 4 lines
[1]: https://syzkaller.appspot.com/bug?extid=997752115a851cb0cf36
Fixes a0309faf1cb0 ("mm: vmalloc: support more granular vrealloc() sizing")Is this the right Fixes tag? I didn't change the kasan logic meaningfully in the above patch, perhaps it should be commit
d699440f58ce("mm:
fix vrealloc()'s KASAN poisoning logic")
The tag you provide is about shrinking but the issue I encountered was about expanding(Grow the vm_area) and kasan_unpoison_vmalloc() didn't work well with expanding.
Thanks.