Skip to contentSkip to conversation

lib/test_kasan: add roundtrip tests

PATCH Superseded Mark Rutland 3 participants 5 messages Aug 19, 2019 – Aug 24, 2019 Library code

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 lib/test_kasan: add roundtrip tests

In several places we need to be able to operate on pointers which have gone via a roundtrip:

	virt -> {phys,page} -> virt

With KASAN_SW_TAGS, we can't preserve the tag for SLUB objects, and the {phys,page} -> virt conversion will use KASAN_TAG_KERNEL.

This patch adds tests to ensure that this works as expected, without false positives which have recently been spotted [1,2] in testing.

[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
[2] https://lore.kernel.org/linux-arm-kernel/[email protected]/

Cc Alexander Potapenko <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Andrey Ryabinin <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Will Deacon <[email protected]>
---
 lib/test_kasan.c | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

Since v1:
* Spin as a separate patch
* Fix typo
* Note examples in commit message.

Mark.

lib/test_kasan.c +40 -0
index b63b367a94e8..cf7b93f0d90c 100644
@@ -19,6 +19,8 @@
 #include <linux/string.h>
 #include <linux/uaccess.h>
 
+#include <asm/page.h>
+
 /*
  * Note: test functions are marked noinline so that their names appear in
  * reports.
@@ -337,6 +339,42 @@ static noinline void __init kmalloc_uaf2(void)
 	kfree(ptr2);
 }
 
+static noinline void __init kfree_via_page(void)
+{
+	char *ptr;
+	size_t size = 8;
+	struct page *page;
+	unsigned long offset;
+
+	pr_info("invalid-free false positive (via page)\n");
+	ptr = kmalloc(size, GFP_KERNEL);
+	if (!ptr) {
+		pr_err("Allocation failed\n");
+		return;
+	}
+
+	page = virt_to_page(ptr);
+	offset = offset_in_page(ptr);
+	kfree(page_address(page) + offset);
+}
+
+static noinline void __init kfree_via_phys(void)
+{
+	char *ptr;
+	size_t size = 8;
+	phys_addr_t phys;
+
+	pr_info("invalid-free false positive (via phys)\n");
+	ptr = kmalloc(size, GFP_KERNEL);
+	if (!ptr) {
+		pr_err("Allocation failed\n");
+		return;
+	}
+
+	phys = virt_to_phys(ptr);
+	kfree(phys_to_virt(phys));
+}
+
 static noinline void __init kmem_cache_oob(void)
 {
 	char *p;
@@ -737,6 +775,8 @@ static int __init kmalloc_tests_init(void)
 	kmalloc_uaf();
 	kmalloc_uaf_memset();
 	kmalloc_uaf2();
+	kfree_via_page();
+	kfree_via_phys();
 	kmem_cache_oob();
 	memcg_accounted_kmem_cache();
 	kasan_stack_oob();
Signature · 1 line
2.11.0
Acked-by on v2 1/1
#1 lore raw
Quoted 22 lines · Mark Rutland

In several places we need to be able to operate on pointers which have gone via a roundtrip:

	virt -> {phys,page} -> virt

With KASAN_SW_TAGS, we can't preserve the tag for SLUB objects, and the {phys,page} -> virt conversion will use KASAN_TAG_KERNEL.

This patch adds tests to ensure that this works as expected, without false positives which have recently been spotted [1,2] in testing.

[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
[2] https://lore.kernel.org/linux-arm-kernel/[email protected]/

Cc Alexander Potapenko <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Andrey Ryabinin <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Will Deacon <[email protected]>
Quoted 1 line

---

maintainer
#2 lore raw
In reply to Mark Rutland
Quoted 8 lines · On Mon, 19 Aug 2019 17:14:49 +0100 Mark Rutland <[email protected]> wrote:

In several places we need to be able to operate on pointers which have gone via a roundtrip:

	virt -> {phys,page} -> virt

With KASAN_SW_TAGS, we can't preserve the tag for SLUB objects, and the {phys,page} -> virt conversion will use KASAN_TAG_KERNEL.

Quoted 9 lines

This patch adds tests to ensure that this works as expected, without false positives which have recently been spotted [1,2] in testing.

[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
[2] https://lore.kernel.org/linux-arm-kernel/[email protected]/

...

The only change I'm seeing from v1 is:

lib/test_kasan.c renamed from lib/test_kasan.c~lib-test_kasan-add-roundtrip-tests-v2 +0 -1
@@ -19,7 +19,6 @@
 #include <linux/string.h>
 #include <linux/uaccess.h>
 
-#include <asm/io.h>
 #include <asm/page.h>
 
 /*

which is really kinda wrong. We should strictly include linux/io.h for things like virt_to_phys().

So I think I'll stick with v1 plus my fixlet:

lib/test_kasan.c renamed from lib/test_kasan.c~lib-test_kasan-add-roundtrip-tests-checkpatch-fixes +1 -1
@@ -18,8 +18,8 @@
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/uaccess.h>
+#include <linux/io.h>
 
-#include <asm/io.h>
 #include <asm/page.h>
 
 /*
Quoted 4 lines

Since v1:
* Spin as a separate patch
* Fix typo
* Note examples in commit message.

So I'm not sure what happened to these things. Did you send the correct patch?

#3 lore raw

Hi Andrew,

Quoted 25 lines · Andrew Morton
Quoted 8 lines · On Mon, 19 Aug 2019 17:14:49 +0100 Mark Rutland <[email protected]> wrote:

In several places we need to be able to operate on pointers which have gone via a roundtrip:

	virt -> {phys,page} -> virt

With KASAN_SW_TAGS, we can't preserve the tag for SLUB objects, and the {phys,page} -> virt conversion will use KASAN_TAG_KERNEL.

Quoted 9 lines

This patch adds tests to ensure that this works as expected, without false positives which have recently been spotted [1,2] in testing.

[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
[2] https://lore.kernel.org/linux-arm-kernel/[email protected]/

...

The only change I'm seeing from v1 is:

lib/test_kasan.c line 22

Quoted 5 lines
lib/test_kasan.c renamed from lib/test_kasan.c~lib-test_kasan-add-roundtrip-tests-v2 +0 -1
@@ -19,7 +19,6 @@
 #include <linux/string.h>
 #include <linux/uaccess.h>
 
-#include <asm/io.h>
 #include <asm/page.h>

I think you've confused v1 with v3 (which was the first version to include <asm/io.h>).

v1: https://lore.kernel.org/linux-arm-kernel/[email protected]/
v3: https://lore.kernel.org/linux-arm-kernel/[email protected]/

I guess as v1 was part of a reply (without the mail subject matching) that might have confused things? Sorry about that if so!

Quoted 12 lines
lib/test_kasan.c renamed from lib/test_kasan.c~lib-test_kasan-add-roundtrip-tests-v2 +0 -0
 /*
 

which is really kinda wrong. We should strictly include linux/io.h for things like virt_to_phys().

So I think I'll stick with v1 plus my fixlet:

lib/test_kasan.c line 25

Quoted 9 lines
lib/test_kasan.c renamed from lib/test_kasan.c~lib-test_kasan-add-roundtrip-tests-checkpatch-fixes +1 -1
@@ -18,8 +18,8 @@
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/uaccess.h>
+#include <linux/io.h>
 
-#include <asm/io.h>
 #include <asm/page.h>
 
 /*

Assuming that you mean *v3* with that fix, that looks good to me!

Thanks,
Mark.

maintainer
#4 lore raw
Quoted 16 lines · On Fri, 23 Aug 2019 11:41:08 +0100 Mark Rutland <[email protected]> wrote:
Quoted 16 lines

/*

which is really kinda wrong. We should strictly include linux/io.h for things like virt_to_phys().

So I think I'll stick with v1 plus my fixlet:

lib/test_kasan.c renamed from lib/test_kasan.c~lib-test_kasan-add-roundtrip-tests-checkpatch-fixes +1 -0
@@ -18,8 +18,8 @@
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/uaccess.h>
+#include <linux/io.h>
 
Quoted 7 lines

lib/test_kasan.c line 24

Quoted 5 lines
lib/test_kasan.c renamed from lib/test_kasan.c~lib-test_kasan-add-roundtrip-tests-checkpatch-fixes +0 -1
-#include <asm/io.h>
 #include <asm/page.h>
 
 /*

Assuming that you mean *v3* with that fix, that looks good to me!

Yes, that's what we have.