Skip to content

Commit

Permalink
mempool: use new mempool KASAN hooks
Browse files Browse the repository at this point in the history
Update the mempool code to use the new mempool KASAN hooks.

Rely on the return value of kasan_mempool_poison_object and
kasan_mempool_poison_pages to prevent double-free and invalid-free bugs.

Link: https://lkml.kernel.org/r/d36fc4a6865bdbd297cadb46b67641d436849f4c.1703024586.git.andreyknvl@google.com
Signed-off-by: Andrey Konovalov <[email protected]>
Cc: Alexander Lobakin <[email protected]>
Cc: Alexander Potapenko <[email protected]>
Cc: Andrey Ryabinin <[email protected]>
Cc: Breno Leitao <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: Evgenii Stepanov <[email protected]>
Cc: Marco Elver <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
  • Loading branch information
xairy authored and akpm00 committed Dec 29, 2023
1 parent 7d4847d commit 413643f
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions mm/mempool.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,32 +112,34 @@ static inline void poison_element(mempool_t *pool, void *element)
}
#endif /* CONFIG_DEBUG_SLAB || CONFIG_SLUB_DEBUG_ON */

static __always_inline void kasan_poison_element(mempool_t *pool, void *element)
static __always_inline bool kasan_poison_element(mempool_t *pool, void *element)
{
if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)
kasan_mempool_poison_object(element);
return kasan_mempool_poison_object(element);
else if (pool->alloc == mempool_alloc_pages)
kasan_poison_pages(element, (unsigned long)pool->pool_data,
false);
return kasan_mempool_poison_pages(element,
(unsigned long)pool->pool_data);
return true;
}

static void kasan_unpoison_element(mempool_t *pool, void *element)
{
if (pool->alloc == mempool_kmalloc)
kasan_unpoison_range(element, (size_t)pool->pool_data);
kasan_mempool_unpoison_object(element, (size_t)pool->pool_data);
else if (pool->alloc == mempool_alloc_slab)
kasan_unpoison_range(element, kmem_cache_size(pool->pool_data));
kasan_mempool_unpoison_object(element,
kmem_cache_size(pool->pool_data));
else if (pool->alloc == mempool_alloc_pages)
kasan_unpoison_pages(element, (unsigned long)pool->pool_data,
false);
kasan_mempool_unpoison_pages(element,
(unsigned long)pool->pool_data);
}

static __always_inline void add_element(mempool_t *pool, void *element)
{
BUG_ON(pool->curr_nr >= pool->min_nr);
poison_element(pool, element);
kasan_poison_element(pool, element);
pool->elements[pool->curr_nr++] = element;
if (kasan_poison_element(pool, element))
pool->elements[pool->curr_nr++] = element;
}

static void *remove_element(mempool_t *pool)
Expand Down

0 comments on commit 413643f

Please sign in to comment.