diff --git a/include/nuttx/mm/kasan.h b/include/nuttx/mm/kasan.h index d856daebde..8db1a23928 100644 --- a/include/nuttx/mm/kasan.h +++ b/include/nuttx/mm/kasan.h @@ -42,9 +42,7 @@ # define kasan_unpoison(addr, size) addr # define kasan_register(addr, size) # define kasan_unregister(addr) -# define kasan_stop() # define kasan_reset_tag(addr) addr -# define kasan_start() #else /**************************************************************************** @@ -146,6 +144,21 @@ void kasan_register(FAR void *addr, FAR size_t *size); void kasan_unregister(FAR void *addr); +/**************************************************************************** + * Name: kasan_reset_tag + * + * Input Parameters: + * addr - The address of the memory to reset the tag. + * + * Returned Value: + * Unlabeled address + * + ****************************************************************************/ + +FAR void *kasan_reset_tag(FAR const void *addr); + +#endif /* CONFIG_MM_KASAN */ + /**************************************************************************** * Name: kasan_start * @@ -180,24 +193,9 @@ void kasan_start(void); void kasan_stop(void); -/**************************************************************************** - * Name: kasan_reset_tag - * - * Input Parameters: - * addr - The address of the memory to reset the tag. - * - * Returned Value: - * Unlabeled address - * - ****************************************************************************/ - -FAR void *kasan_reset_tag(FAR const void *addr); - #undef EXTERN #ifdef __cplusplus } #endif -#endif /* CONFIG_MM_KASAN */ - #endif /* __INCLUDE_NUTTX_MM_KASAN_H */ diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index f79edad850..5b6128f023 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -79,7 +79,6 @@ struct kasan_region_s static spinlock_t g_lock; static FAR struct kasan_region_s *g_region; -static uint32_t g_region_init; /**************************************************************************** * Public Data @@ -99,11 +98,6 @@ static FAR uintptr_t *kasan_mem_to_shadow(FAR const void *ptr, size_t size, FAR struct kasan_region_s *region; uintptr_t addr = (uintptr_t)ptr; - if (size == 0 || g_region_init != KASAN_INIT_VALUE) - { - return NULL; - } - for (region = g_region; region != NULL; region = region->next) { if (addr >= region->begin && addr < region->end) @@ -260,16 +254,20 @@ FAR void *kasan_unpoison(FAR const void *addr, size_t size) void kasan_register(FAR void *addr, FAR size_t *size) { FAR struct kasan_region_s *region; + irqstate_t flags; region = (FAR struct kasan_region_s *) ((FAR char *)addr + *size - KASAN_REGION_SIZE(*size)); region->begin = (uintptr_t)addr; region->end = region->begin + *size; + + flags = spin_lock_irqsave(&g_lock); region->next = g_region; g_region = region; - g_region_init = KASAN_INIT_VALUE; + spin_unlock_irqrestore(&g_lock, flags); + kasan_start(); kasan_poison(addr, *size); *size -= KASAN_REGION_SIZE(*size); } @@ -302,13 +300,3 @@ void kasan_unregister(FAR void *addr) spin_unlock_irqrestore(&g_lock, flags); } - -void kasan_start(void) -{ - g_region_init = KASAN_INIT_VALUE; -} - -void kasan_stop(void) -{ - g_region_init = 0; -} diff --git a/mm/kasan/hook.c b/mm/kasan/hook.c index f8fa9e653a..2ed8b34cb6 100644 --- a/mm/kasan/hook.c +++ b/mm/kasan/hook.c @@ -83,6 +83,14 @@ # define MM_KASAN_DISABLE_WRITE_PANIC 0 #endif +#define KASAN_INIT_VALUE 0xdeadcafe + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static uint32_t g_region_init; + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -160,6 +168,11 @@ static inline void kasan_check_report(FAR const void *addr, size_t size, bool is_write, FAR void *return_address) { + if (size == 0 || g_region_init != KASAN_INIT_VALUE) + { + return; + } + if (kasan_is_poisoned(addr, size)) { kasan_report(addr, size, is_write, return_address); @@ -170,6 +183,16 @@ static inline void kasan_check_report(FAR const void *addr, size_t size, * Public Functions ****************************************************************************/ +void kasan_start(void) +{ + g_region_init = KASAN_INIT_VALUE; +} + +void kasan_stop(void) +{ + g_region_init = 0; +} + void __asan_before_dynamic_init(FAR const void *module_name) { /* Shut up compiler complaints */ diff --git a/mm/kasan/sw_tags.c b/mm/kasan/sw_tags.c index 4c1158f48d..94ac57aa12 100644 --- a/mm/kasan/sw_tags.c +++ b/mm/kasan/sw_tags.c @@ -51,8 +51,6 @@ #define KASAN_REGION_SIZE(size) \ (sizeof(struct kasan_region_s) + KASAN_SHADOW_SIZE(size)) -#define KASAN_INIT_VALUE 0xdeadcafe - /**************************************************************************** * Private Types ****************************************************************************/ @@ -71,7 +69,6 @@ struct kasan_region_s static spinlock_t g_lock; static FAR struct kasan_region_s *g_region; -static uint32_t g_region_init; /**************************************************************************** * Private Functions @@ -83,10 +80,6 @@ static FAR uint8_t *kasan_mem_to_shadow(FAR const void *ptr, size_t size) uintptr_t addr; addr = (uintptr_t)kasan_reset_tag(ptr); - if (size == 0 || g_region_init != KASAN_INIT_VALUE) - { - return NULL; - } for (region = g_region; region != NULL; region = region->next) { @@ -187,17 +180,8 @@ void kasan_register(FAR void *addr, FAR size_t *size) g_region = region; spin_unlock_irqrestore(&g_lock, flags); - g_region_init = KASAN_INIT_VALUE; + kasan_start(); kasan_poison(addr, *size); *size -= KASAN_REGION_SIZE(*size); } -void kasan_start(void) -{ - g_region_init = KASAN_INIT_VALUE; -} - -void kasan_stop(void) -{ - g_region_init = 0; -}