mm/slub: reset KASAN tag in defer_free() before accessing freed memory
Subsystems (2)
PATCH 1/1 mm/slub: reset KASAN tag in defer_free() before accessing freed memory
When CONFIG_SLUB_TINY is enabled, kfree_nolock() calls kasan_slab_free() before defer_free(). On ARM64 with MTE (Memory Tagging Extension), kasan_slab_free() poisons the memory and changes the tag from the original (e.g., 0xf3) to a poison tag (0xfe).
When defer_free() then tries to write to the freed object to build the deferred free list via llist_add(), the pointer still has the old tag, causing a tag mismatch and triggering a KASAN use-after-free report:
BUG: KASAN: slab-use-after-free in defer_free+0x3c/0xbc mm/slub.c:6537 Write at addr f3f000000854f020 by task kworker/u8:6/983 Pointer tag: [f3], memory tag: [fe]
Fix this by calling kasan_reset_tag() before accessing the freed memory. This is safe because defer_free() is part of the allocator itself and is expected to manipulate freed memory for bookkeeping purposes.
--- mm/slub.c | 2 ++ 1 file changed, 2 insertions(+)
mm/slub.c +2 -0
@@ -6534,6 +6534,8 @@ static void defer_free(struct kmem_cache *s, void *head)guard (preempt)(); + head =kasan_reset_tag (head); + df =this_cpu_ptr (&defer_free_objects);if (llist_add (head + s->offset, &df->objects))irq_work_queue (&df->work);
Signature · 1 line
2.43.0
Quoted 32 lines · Deepanshu Kartikey
When
CONFIG_SLUB_TINYis enabled,kfree_nolock()callskasan_slab_free()beforedefer_free(). On ARM64 with MTE (Memory Tagging Extension),kasan_slab_free()poisons the memory and changes the tag from the original (e.g.,0xf3) to a poison tag (0xfe).When
defer_free()then tries to write to the freed object to build the deferred free list viallist_add(), the pointer still has the old tag, causing a tag mismatch and triggering a KASAN use-after-free report:BUG: KASAN: slab-use-after-free in defer_free+0x3c/0xbc mm/slub.c:6537 Write at addr f3f000000854f020 by task kworker/u8:6/983 Pointer tag: [f3], memory tag: [fe]Fix this by calling
kasan_reset_tag()before accessing the freed memory. This is safe becausedefer_free()is part of the allocator itself and is expected to manipulate freed memory for bookkeeping purposes.Fixes af92793e52c3 ("slab: Introduce kmalloc_nolock() and kfree_nolock().")Reported-by [email protected]Tested-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>--- mm/slub.c | 2 ++ 1 file changed, 2 insertions(+)mm/slub.c +0
-0
mm/slub.c static void defer_free(struct kmem_cache *s, void *head) · line 6541
Quoted 8 lines
mm/slub.c +2
-0@@ -6534,6 +6534,8 @@ static void defer_free(struct kmem_cache *s, void *head)guard (preempt)(); + head =kasan_reset_tag (head); + df =this_cpu_ptr (&defer_free_objects);if (llist_add (head + s->offset, &df->objects))irq_work_queue (&df->work);
makes sense to me.
Quoted 17 lines · Deepanshu Kartikey
When
CONFIG_SLUB_TINYis enabled,kfree_nolock()callskasan_slab_free()beforedefer_free(). On ARM64 with MTE (Memory Tagging Extension),kasan_slab_free()poisons the memory and changes the tag from the original (e.g.,0xf3) to a poison tag (0xfe).When
defer_free()then tries to write to the freed object to build the deferred free list viallist_add(), the pointer still has the old tag, causing a tag mismatch and triggering a KASAN use-after-free report:BUG: KASAN: slab-use-after-free in defer_free+0x3c/0xbc mm/slub.c:6537 Write at addr f3f000000854f020 by task kworker/u8:6/983 Pointer tag: [f3], memory tag: [fe]Fix this by calling
kasan_reset_tag()before accessing the freed memory. This is safe becausedefer_free()is part of the allocator itself and is expected to manipulate freed memory for bookkeeping purposes.
Quoted 5 lines
Fixes af92793e52c3 ("slab: Introduce kmalloc_nolock() and kfree_nolock().")Reported-by [email protected]Tested-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>
Thanks, added cc: stable and pushed to slab/for-next-fixes
Quoted 17 lines
--- mm/slub.c | 2 ++ 1 file changed, 2 insertions(+)mm/slub.c +2
-0@@ -6534,6 +6534,8 @@ static void defer_free(struct kmem_cache *s, void *head)guard (preempt)(); + head =kasan_reset_tag (head); + df =this_cpu_ptr (&defer_free_objects);if (llist_add (head + s->offset, &df->objects))irq_work_queue (&df->work);