mm: move kasna_start/stop to hook.c

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
This commit is contained in:
yinshengkai 2024-06-06 17:04:31 +08:00 committed by Xiang Xiao
parent 2d8b582ce6
commit ae7a3d7071
4 changed files with 44 additions and 51 deletions

View File

@ -42,9 +42,7 @@
# define kasan_unpoison(addr, size) addr # define kasan_unpoison(addr, size) addr
# define kasan_register(addr, size) # define kasan_register(addr, size)
# define kasan_unregister(addr) # define kasan_unregister(addr)
# define kasan_stop()
# define kasan_reset_tag(addr) addr # define kasan_reset_tag(addr) addr
# define kasan_start()
#else #else
/**************************************************************************** /****************************************************************************
@ -146,6 +144,21 @@ void kasan_register(FAR void *addr, FAR size_t *size);
void kasan_unregister(FAR void *addr); 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 * Name: kasan_start
* *
@ -180,24 +193,9 @@ void kasan_start(void);
void kasan_stop(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 #undef EXTERN
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* CONFIG_MM_KASAN */
#endif /* __INCLUDE_NUTTX_MM_KASAN_H */ #endif /* __INCLUDE_NUTTX_MM_KASAN_H */

View File

@ -79,7 +79,6 @@ struct kasan_region_s
static spinlock_t g_lock; static spinlock_t g_lock;
static FAR struct kasan_region_s *g_region; static FAR struct kasan_region_s *g_region;
static uint32_t g_region_init;
/**************************************************************************** /****************************************************************************
* Public Data * 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; FAR struct kasan_region_s *region;
uintptr_t addr = (uintptr_t)ptr; 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) for (region = g_region; region != NULL; region = region->next)
{ {
if (addr >= region->begin && addr < region->end) 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) void kasan_register(FAR void *addr, FAR size_t *size)
{ {
FAR struct kasan_region_s *region; FAR struct kasan_region_s *region;
irqstate_t flags;
region = (FAR struct kasan_region_s *) region = (FAR struct kasan_region_s *)
((FAR char *)addr + *size - KASAN_REGION_SIZE(*size)); ((FAR char *)addr + *size - KASAN_REGION_SIZE(*size));
region->begin = (uintptr_t)addr; region->begin = (uintptr_t)addr;
region->end = region->begin + *size; region->end = region->begin + *size;
flags = spin_lock_irqsave(&g_lock);
region->next = g_region; region->next = g_region;
g_region = region; g_region = region;
g_region_init = KASAN_INIT_VALUE; spin_unlock_irqrestore(&g_lock, flags);
kasan_start();
kasan_poison(addr, *size); kasan_poison(addr, *size);
*size -= KASAN_REGION_SIZE(*size); *size -= KASAN_REGION_SIZE(*size);
} }
@ -302,13 +300,3 @@ void kasan_unregister(FAR void *addr)
spin_unlock_irqrestore(&g_lock, flags); spin_unlock_irqrestore(&g_lock, flags);
} }
void kasan_start(void)
{
g_region_init = KASAN_INIT_VALUE;
}
void kasan_stop(void)
{
g_region_init = 0;
}

View File

@ -83,6 +83,14 @@
# define MM_KASAN_DISABLE_WRITE_PANIC 0 # define MM_KASAN_DISABLE_WRITE_PANIC 0
#endif #endif
#define KASAN_INIT_VALUE 0xdeadcafe
/****************************************************************************
* Private Data
****************************************************************************/
static uint32_t g_region_init;
/**************************************************************************** /****************************************************************************
* Private Functions * Private Functions
****************************************************************************/ ****************************************************************************/
@ -160,6 +168,11 @@ static inline void kasan_check_report(FAR const void *addr, size_t size,
bool is_write, bool is_write,
FAR void *return_address) FAR void *return_address)
{ {
if (size == 0 || g_region_init != KASAN_INIT_VALUE)
{
return;
}
if (kasan_is_poisoned(addr, size)) if (kasan_is_poisoned(addr, size))
{ {
kasan_report(addr, size, is_write, return_address); 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 * 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) void __asan_before_dynamic_init(FAR const void *module_name)
{ {
/* Shut up compiler complaints */ /* Shut up compiler complaints */

View File

@ -51,8 +51,6 @@
#define KASAN_REGION_SIZE(size) \ #define KASAN_REGION_SIZE(size) \
(sizeof(struct kasan_region_s) + KASAN_SHADOW_SIZE(size)) (sizeof(struct kasan_region_s) + KASAN_SHADOW_SIZE(size))
#define KASAN_INIT_VALUE 0xdeadcafe
/**************************************************************************** /****************************************************************************
* Private Types * Private Types
****************************************************************************/ ****************************************************************************/
@ -71,7 +69,6 @@ struct kasan_region_s
static spinlock_t g_lock; static spinlock_t g_lock;
static FAR struct kasan_region_s *g_region; static FAR struct kasan_region_s *g_region;
static uint32_t g_region_init;
/**************************************************************************** /****************************************************************************
* Private Functions * Private Functions
@ -83,10 +80,6 @@ static FAR uint8_t *kasan_mem_to_shadow(FAR const void *ptr, size_t size)
uintptr_t addr; uintptr_t addr;
addr = (uintptr_t)kasan_reset_tag(ptr); 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) 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; g_region = region;
spin_unlock_irqrestore(&g_lock, flags); spin_unlock_irqrestore(&g_lock, flags);
g_region_init = KASAN_INIT_VALUE; kasan_start();
kasan_poison(addr, *size); kasan_poison(addr, *size);
*size -= KASAN_REGION_SIZE(*size); *size -= KASAN_REGION_SIZE(*size);
} }
void kasan_start(void)
{
g_region_init = KASAN_INIT_VALUE;
}
void kasan_stop(void)
{
g_region_init = 0;
}