mm/heap: memory alignment before executing kasan_register

The unaligned address is used in kasan_register, but the aligned address is used in kasan_unregister.
The mismatch between the addr value and mm_heapstart will result in a crash due to the inability to unregister correctly.

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
This commit is contained in:
yinshengkai 2024-06-28 13:34:21 +08:00 committed by GUIDINGLI
parent ed71aa810e
commit c158ed2c32
1 changed files with 9 additions and 5 deletions

View File

@ -141,10 +141,6 @@ void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart,
memset(heapstart, MM_INIT_MAGIC, heapsize); memset(heapstart, MM_INIT_MAGIC, heapsize);
#endif #endif
/* Register to KASan for access check */
kasan_register(heapstart, &heapsize);
/* Adjust the provided heap start and size. /* Adjust the provided heap start and size.
* *
* Note: (uintptr_t)node + MM_SIZEOF_ALLOCNODE is what's actually * Note: (uintptr_t)node + MM_SIZEOF_ALLOCNODE is what's actually
@ -154,7 +150,15 @@ void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart,
heapbase = MM_ALIGN_UP((uintptr_t)heapstart + 2 * MM_SIZEOF_ALLOCNODE) - heapbase = MM_ALIGN_UP((uintptr_t)heapstart + 2 * MM_SIZEOF_ALLOCNODE) -
2 * MM_SIZEOF_ALLOCNODE; 2 * MM_SIZEOF_ALLOCNODE;
heapend = MM_ALIGN_DOWN((uintptr_t)heapstart + (uintptr_t)heapsize); heapsize = heapsize - (heapbase - (uintptr_t)heapstart);
/* Register KASan for access rights check. We need to register after
* address alignment.
*/
kasan_register((void *)heapbase, &heapsize);
heapend = MM_ALIGN_DOWN((uintptr_t)heapbase + (uintptr_t)heapsize);
heapsize = heapend - heapbase; heapsize = heapend - heapbase;
#if defined(CONFIG_FS_PROCFS) && \ #if defined(CONFIG_FS_PROCFS) && \