Skip to contentSkip to conversation

kasan: add test for invalid size in memmove

PATCH In review Walter Wu 1 participant 1 message Nov 4, 2019

Subsystems (2)
lore
Share & tools
lore

Walter Wu

author
#0 lore raw

PATCH v3 2/2 kasan: add test for invalid size in memmove

Test negative size in memmove in order to verify whether it correctly get KASAN report.

Casting negative numbers to size_t would indeed turn up as a large size_t, so it will have out-of-bounds bug and be detected by KASAN.

---
 lib/test_kasan.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
lib/test_kasan.c +18 -0
index 49cc4d570a40..06942cf585cc 100644
@@ -283,6 +283,23 @@ static noinline void __init kmalloc_oob_in_memset(void)
 	kfree(ptr);
 }
 
+static noinline void __init kmalloc_memmove_invalid_size(void)
+{
+	char *ptr;
+	size_t size = 64;
+
+	pr_info("invalid size in memmove\n");
+	ptr = kmalloc(size, GFP_KERNEL);
+	if (!ptr) {
+		pr_err("Allocation failed\n");
+		return;
+	}
+
+	memset((char *)ptr, 0, 64);
+	memmove((char *)ptr, (char *)ptr + 4, -2);
+	kfree(ptr);
+}
+
 static noinline void __init kmalloc_uaf(void)
 {
 	char *ptr;
@@ -773,6 +790,7 @@ static int __init kmalloc_tests_init(void)
 	kmalloc_oob_memset_4();
 	kmalloc_oob_memset_8();
 	kmalloc_oob_memset_16();
+	kmalloc_memmove_invalid_size();
 	kmalloc_uaf();
 	kmalloc_uaf_memset();
 	kmalloc_uaf2();
Signature · 1 line
2.18.0