Skip to contentSkip to conversation

kasan: add tests for call_rcu stack recording

PATCH Superseded Walter Wu 1 participant 1 message Jun 1, 2020

Subsystems (3)

v7 of 8 Superseded 2 of 4 applied Latest is v8 →

lore
Share & tools
lore

Walter Wu

author
#0 lore raw

PATCH v7 3/4 kasan: add tests for call_rcu stack recording

Test call_rcu() call stack recording and verify whether it correctly is printed in KASAN report.

Reviewed-and-tested-by Dmitry Vyukov <[email protected]>
Cc Andrey Ryabinin <[email protected]>
Cc Alexander Potapenko <[email protected]>
Cc Matthias Brugger <[email protected]>

---

Changes since v6:
- renamed the variable name in testcase

---
 lib/test_kasan.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
lib/test_kasan.c +30 -0
index e3087d90e00d..19c72c1501ef 100644
@@ -792,6 +792,35 @@ static noinline void __init vmalloc_oob(void)
 static void __init vmalloc_oob(void) {}
 #endif
 
+static struct kasan_rcu_info {
+	int i;
+	struct rcu_head rcu;
+} *global_rcu_ptr;
+
+static noinline void __init kasan_rcu_reclaim(struct rcu_head *rp)
+{
+	struct kasan_rcu_info *fp = container_of(rp,
+						struct kasan_rcu_info, rcu);
+
+	kfree(fp);
+	fp->i = 1;
+}
+
+static noinline void __init kasan_rcu_uaf(void)
+{
+	struct kasan_rcu_info *ptr;
+
+	pr_info("use-after-free in kasan_rcu_reclaim\n");
+	ptr = kmalloc(sizeof(struct kasan_rcu_info), GFP_KERNEL);
+	if (!ptr) {
+		pr_err("Allocation failed\n");
+		return;
+	}
+
+	global_rcu_ptr = rcu_dereference_protected(ptr, NULL);
+	call_rcu(&global_rcu_ptr->rcu, kasan_rcu_reclaim);
+}
+
 static int __init kmalloc_tests_init(void)
 {
 	/*
@@ -839,6 +868,7 @@ static int __init kmalloc_tests_init(void)
 	kasan_bitops();
 	kmalloc_double_kzfree();
 	vmalloc_oob();
+	kasan_rcu_uaf();
 
 	kasan_restore_multi_shot(multishot);
 
Signature · 1 line
2.18.0