Skip to contentSkip to conversation

mm/kasan: Fix false positive invalid-free reports with CONFIG_KASAN_SW_TAGS=y

PATCH Applied Andrey Ryabinin 2 participants 2 messages Aug 19, 2019 – Aug 20, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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 loose 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.

Fixes 7f94ffbc4c6a ("kasan: add hooks implementation for tag-based mode")
---
 mm/kasan/common.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
mm/kasan/common.c +8 -2
index 895dc5e2b3d5..3b8cde0cb5b2 100644
@@ -406,8 +406,14 @@ static inline bool shadow_invalid(u8 tag, s8 shadow_byte)
 	if (IS_ENABLED(CONFIG_KASAN_GENERIC))
 		return shadow_byte < 0 ||
 			shadow_byte >= KASAN_SHADOW_SCALE_SIZE;
-	else
-		return tag != (u8)shadow_byte;
+
+	/* else CONFIG_KASAN_SW_TAGS: */
+	if ((u8)shadow_byte == KASAN_TAG_INVALID)
+		return true;
+	if ((tag != KASAN_TAG_KERNEL) && (tag != (u8)shadow_byte))
+		return true;
+
+	return false;
 }
 
 static bool __kasan_slab_free(struct kmem_cache *cache, void *object,
Signature · 1 line
2.21.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 19 lines · Andrey Ryabinin

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 loose 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.

Quoted 8 lines

2) If pointer tag is different from 0xFF, make sure that tag in the shadow is the same as in the pointer.

Fixes 7f94ffbc4c6a ("kasan: add hooks implementation for tag-based mode")
Quoted 28 lines
---
 mm/kasan/common.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
mm/kasan/common.c +8 -3
index 895dc5e2b3d5..3b8cde0cb5b2 100644
@@ -406,8 +406,14 @@ static inline bool shadow_invalid(u8 tag, s8 shadow_byte)
        if (IS_ENABLED(CONFIG_KASAN_GENERIC))
                return shadow_byte < 0 ||
                        shadow_byte >= KASAN_SHADOW_SCALE_SIZE;
-       else
-               return tag != (u8)shadow_byte;
+
+       /* else CONFIG_KASAN_SW_TAGS: */
+       if ((u8)shadow_byte == KASAN_TAG_INVALID)
+               return true;
+       if ((tag != KASAN_TAG_KERNEL) && (tag != (u8)shadow_byte))
+               return true;
+
+       return false;
 }
 
 static bool __kasan_slab_free(struct kmem_cache *cache, void *object,
--

2.21.0