Skip to content

Commit 00fb24a

Browse files
aryabinintorvalds
authored andcommitted
mm/kasan: fix false positive invalid-free reports with CONFIG_KASAN_SW_TAGS=y
The code like this: ptr = kmalloc(size, GFP_KERNEL); page = virt_to_page(ptr); offset = offset_in_page(ptr); kfree(page_address(page) + offset); may produce false-positive invalid-free reports on the kernel with CONFIG_KASAN_SW_TAGS=y. In the example above we lose the original tag assigned to 'ptr', so kfree() gets the pointer with 0xFF tag. In kfree() we check that 0xFF tag is different from the tag in shadow hence print false report. Instead of just comparing tags, do the following: 1) Check that shadow doesn't contain KASAN_TAG_INVALID. Otherwise it's double-free and it doesn't matter what tag the pointer have. 2) If pointer tag is different from 0xFF, make sure that tag in the shadow is the same as in the pointer. Link: http://lkml.kernel.org/r/[email protected] Fixes: 7f94ffb ("kasan: add hooks implementation for tag-based mode") Signed-off-by: Andrey Ryabinin <[email protected]> Reported-by: Walter Wu <[email protected]> Reported-by: Mark Rutland <[email protected]> Reviewed-by: Andrey Konovalov <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: Dmitry Vyukov <[email protected]> Cc: Catalin Marinas <[email protected]> Cc: Will Deacon <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 701d678 commit 00fb24a

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

mm/kasan/common.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,14 @@ static inline bool shadow_invalid(u8 tag, s8 shadow_byte)
407407
if (IS_ENABLED(CONFIG_KASAN_GENERIC))
408408
return shadow_byte < 0 ||
409409
shadow_byte >= KASAN_SHADOW_SCALE_SIZE;
410-
else
411-
return tag != (u8)shadow_byte;
410+
411+
/* else CONFIG_KASAN_SW_TAGS: */
412+
if ((u8)shadow_byte == KASAN_TAG_INVALID)
413+
return true;
414+
if ((tag != KASAN_TAG_KERNEL) && (tag != (u8)shadow_byte))
415+
return true;
416+
417+
return false;
412418
}
413419

414420
static bool __kasan_slab_free(struct kmem_cache *cache, void *object,

0 commit comments

Comments
 (0)