linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] kasan: modify the exception handling if kmalloc or krealloc return NULL
@ 2018-04-18 14:17 jingwei.liuxi
  0 siblings, 0 replies; only message in thread
From: jingwei.liuxi @ 2018-04-18 14:17 UTC (permalink / raw)
  To: aryabinin, glider, dvyukov; +Cc: kasan-dev, linux-kernel, Victor Liu

From: Victor Liu <jingwei.liuxi@gmail.com>

Both kmalloc and krealloc may return NULL(!ptr1 || !ptr2), and we do not
know which one is.

Signed-off-by: Victor Liu <jingwei.liuxi@gmail.com>
---
 lib/test_kasan.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/lib/test_kasan.c b/lib/test_kasan.c
index ec65710..afa10bf 100644
--- a/lib/test_kasan.c
+++ b/lib/test_kasan.c
@@ -153,9 +153,13 @@ static noinline void __init kmalloc_oob_krealloc_more(void)
 
 	pr_info("out-of-bounds after krealloc more\n");
 	ptr1 = kmalloc(size1, GFP_KERNEL);
+	if (!ptr1) {
+		pr_err("Allocation ptr1 failed\n");
+		return;
+	}
 	ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
-	if (!ptr1 || !ptr2) {
-		pr_err("Allocation failed\n");
+	if (!ptr2) {
+		pr_err("Allocation ptr2 failed\n");
 		kfree(ptr1);
 		return;
 	}
@@ -172,9 +176,13 @@ static noinline void __init kmalloc_oob_krealloc_less(void)
 
 	pr_info("out-of-bounds after krealloc less\n");
 	ptr1 = kmalloc(size1, GFP_KERNEL);
+	if (!ptr1) {
+		pr_err("Allocation ptr1 failed\n");
+		return;
+	}
 	ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
-	if (!ptr1 || !ptr2) {
-		pr_err("Allocation failed\n");
+	if (!ptr2) {
+		pr_err("Allocation ptr2 failed\n");
 		kfree(ptr1);
 		return;
 	}
@@ -190,11 +198,14 @@ static noinline void __init kmalloc_oob_16(void)
 
 	pr_info("kmalloc out-of-bounds for 16-bytes access\n");
 	ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
+	if (!ptr1) {
+		pr_err("Allocation ptr1 failed\n");
+		return;
+	}
 	ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
-	if (!ptr1 || !ptr2) {
-		pr_err("Allocation failed\n");
+	if (!ptr2) {
+		pr_err("Allocation ptr2 failed\n");
 		kfree(ptr1);
-		kfree(ptr2);
 		return;
 	}
 	*ptr1 = *ptr2;
-- 
2.7.4

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2018-04-18 14:17 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-18 14:17 [PATCH] kasan: modify the exception handling if kmalloc or krealloc return NULL jingwei.liuxi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).