From 27f8f873313a79862c9fe4de732fbc78d21aea04 Mon Sep 17 00:00:00 2001 From: wangmingrong1 Date: Fri, 6 Sep 2024 17:27:45 +0800 Subject: [PATCH] mm/kasan: Fix the issue where Kasan Global cannot be used 1. The shadow area of global variables is different from heap 2. This modification has little impact on performance under O3 optimization Signed-off-by: wangmingrong1 --- mm/kasan/generic.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index fc2bb98820..a0ab366df3 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -92,7 +92,8 @@ extern const unsigned char g_globals_region[]; ****************************************************************************/ static FAR uintptr_t *kasan_mem_to_shadow(FAR const void *ptr, size_t size, - unsigned int *bit) + FAR unsigned int *bit, + FAR size_t *align) { FAR struct kasan_region_s *region; uintptr_t addr = (uintptr_t)ptr; @@ -103,6 +104,7 @@ static FAR uintptr_t *kasan_mem_to_shadow(FAR const void *ptr, size_t size, { DEBUGASSERT(addr + size <= region->end); addr -= region->begin; + *align = KASAN_SHADOW_SCALE; addr /= KASAN_SHADOW_SCALE; *bit = addr % KASAN_BITS_PER_WORD; return ®ion->shadow[addr / KASAN_BITS_PER_WORD]; @@ -118,6 +120,7 @@ static FAR uintptr_t *kasan_mem_to_shadow(FAR const void *ptr, size_t size, { DEBUGASSERT(addr + size <= region->end); addr -= region->begin; + *align = KASAN_GLOBAL_SHADOW_SCALE; addr /= KASAN_GLOBAL_SHADOW_SCALE; *bit = addr % KASAN_BITS_PER_WORD; return ®ion->shadow[addr / KASAN_BITS_PER_WORD]; @@ -136,8 +139,9 @@ static void kasan_set_poison(FAR const void *addr, size_t size, unsigned int bit; unsigned int nbit; uintptr_t mask; + size_t align; - p = kasan_mem_to_shadow(addr, size, &bit); + p = kasan_mem_to_shadow(addr, size, &bit, &align); if (p == NULL) { return; @@ -145,7 +149,7 @@ static void kasan_set_poison(FAR const void *addr, size_t size, nbit = KASAN_BITS_PER_WORD - bit % KASAN_BITS_PER_WORD; mask = KASAN_FIRST_WORD_MASK(bit); - size /= KASAN_SHADOW_SCALE; + size /= align; flags = spin_lock_irqsave(&g_lock); while (size >= nbit) @@ -197,22 +201,23 @@ bool kasan_is_poisoned(FAR const void *addr, size_t size) unsigned int bit; unsigned int nbit; uintptr_t mask; + size_t align; - p = kasan_mem_to_shadow(addr, size, &bit); + p = kasan_mem_to_shadow(addr, size, &bit, &align); if (p == NULL) { return false; } - if (size <= KASAN_SHADOW_SCALE) + if (size <= align) { return ((*p >> bit) & 1); } nbit = KASAN_BITS_PER_WORD - bit % KASAN_BITS_PER_WORD; mask = KASAN_FIRST_WORD_MASK(bit); - size = ALIGN_UP(size, KASAN_SHADOW_SCALE); - size /= KASAN_SHADOW_SCALE; + size = ALIGN_UP(size, align); + size /= align; while (size >= nbit) {