Skip to content

Commit

Permalink
mm/heap: add coloration after free to detect use after free issue
Browse files Browse the repository at this point in the history
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
  • Loading branch information
Donny9 authored and acassis committed Sep 11, 2023
1 parent a7d0b6c commit 36e3d32
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions mm/mempool/mempool.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ FAR void *mempool_alloc(FAR struct mempool_s *pool)
}
}

#ifdef CONFIG_MM_FILL_ALLOCATIONS
memset(blk, 0xaa, pool->blocksize);
#endif

#if CONFIG_MM_BACKTRACE >= 0
mempool_add_backtrace(pool, (FAR struct mempool_backtrace_s *)
((FAR char *)blk + pool->blocksize));
Expand Down Expand Up @@ -312,6 +316,10 @@ void mempool_free(FAR struct mempool_s *pool, FAR void *blk)
pool->nalloc--;
#endif

#ifdef CONFIG_MM_FILL_ALLOCATIONS
memset(blk, 0x55, pool->blocksize);
#endif

if (pool->interruptsize > blocksize)
{
if ((FAR char *)blk >= pool->ibase &&
Expand Down
4 changes: 4 additions & 0 deletions mm/mm_heap/mm_free.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem)
return;
}

#ifdef CONFIG_MM_FILL_ALLOCATIONS
memset(mem, 0x55, mm_malloc_size(heap, mem));
#endif

kasan_poison(mem, mm_malloc_size(heap, mem));

/* Map the memory chunk into a free node */
Expand Down
8 changes: 8 additions & 0 deletions mm/tlsf/mm_tlsf.c
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,10 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem)

if (mm_lock(heap) == 0)
{
#ifdef CONFIG_MM_FILL_ALLOCATIONS
memset(mem, 0x55, mm_malloc_size(heap, mem));
#endif

kasan_poison(mem, mm_malloc_size(heap, mem));

/* Pass, return to the tlsf pool */
Expand Down Expand Up @@ -1064,6 +1068,10 @@ FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size)
memdump_backtrace(heap, buf);
#endif
kasan_unpoison(ret, mm_malloc_size(heap, ret));

#ifdef CONFIG_MM_FILL_ALLOCATIONS
memset(ret, 0xaa, mm_malloc_size(heap, ret));
#endif
}

return ret;
Expand Down

0 comments on commit 36e3d32

Please sign in to comment.