Skip to content

Commit

Permalink
kasan: fix quarantine conflicting with init_on_free
Browse files Browse the repository at this point in the history
KASAN's quarantine might save its metadata inside freed objects.  As
this happens after the memory is zeroed by the slab allocator when
init_on_free is enabled, the memory coming out of quarantine is not
properly zeroed.

This causes lib/test_meminit.c tests to fail with Generic KASAN.

Zero the metadata when the object is removed from quarantine.

Link: https://lkml.kernel.org/r/2805da5df4b57138fdacd671f5d227d58950ba54.1640037083.git.andreyknvl@google.com
Fixes: 6471384 ("mm: security: introduce init_on_alloc=1 and init_on_free=1 boot options")
Signed-off-by: Andrey Konovalov <[email protected]>
Reviewed-by: Marco Elver <[email protected]>
Cc: Alexander Potapenko <[email protected]>
Cc: Andrey Konovalov <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: Andrey Ryabinin <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
xairy authored and torvalds committed Jan 15, 2022
1 parent f98f966 commit 26dca99
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions mm/kasan/quarantine.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,22 @@ static void *qlink_to_object(struct qlist_node *qlink, struct kmem_cache *cache)
static void qlink_free(struct qlist_node *qlink, struct kmem_cache *cache)
{
void *object = qlink_to_object(qlink, cache);
struct kasan_free_meta *meta = kasan_get_free_meta(cache, object);
unsigned long flags;

if (IS_ENABLED(CONFIG_SLAB))
local_irq_save(flags);

/*
* If init_on_free is enabled and KASAN's free metadata is stored in
* the object, zero the metadata. Otherwise, the object's memory will
* not be properly zeroed, as KASAN saves the metadata after the slab
* allocator zeroes the object.
*/
if (slab_want_init_on_free(cache) &&
cache->kasan_info.free_meta_offset == 0)
memzero_explicit(meta, sizeof(*meta));

/*
* As the object now gets freed from the quarantine, assume that its
* free track is no longer valid.
Expand Down

0 comments on commit 26dca99

Please sign in to comment.