2021-12-20 21:56:07

by andrey.konovalov

[permalink] [raw]
Subject: [PATCH v2] kasan: fix quarantine conflicting with init_on_free

From: Andrey Konovalov <[email protected]>

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.

Fixes: 6471384af2a6 ("mm: security: introduce init_on_alloc=1 and init_on_free=1 boot options")
Signed-off-by: Andrey Konovalov <[email protected]>

---

Changes v1->v2:
- Use memzero_explicit() instead of memset().
---
mm/kasan/quarantine.c | 11 +++++++++++
1 file changed, 11 insertions(+)

diff --git a/mm/kasan/quarantine.c b/mm/kasan/quarantine.c
index 587da8995f2d..08291ed33e93 100644
--- a/mm/kasan/quarantine.c
+++ b/mm/kasan/quarantine.c
@@ -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.
--
2.25.1



2021-12-21 07:00:57

by Marco Elver

[permalink] [raw]
Subject: Re: [PATCH v2] kasan: fix quarantine conflicting with init_on_free

On Mon, 20 Dec 2021 at 22:56, <[email protected]> wrote:
>
> From: Andrey Konovalov <[email protected]>
>
> 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.
>
> Fixes: 6471384af2a6 ("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]>

Thank you.

> ---
>
> Changes v1->v2:
> - Use memzero_explicit() instead of memset().
> ---
> mm/kasan/quarantine.c | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
> diff --git a/mm/kasan/quarantine.c b/mm/kasan/quarantine.c
> index 587da8995f2d..08291ed33e93 100644
> --- a/mm/kasan/quarantine.c
> +++ b/mm/kasan/quarantine.c
> @@ -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.
> --
> 2.25.1
>