[PATCH] kasan: modify the exception handling if kmalloc or krealloc return NULL

From: jingwei . liuxi
Date: Wed Apr 18 2018 - 10:17:38 EST


From: Victor Liu <jingwei.liuxi@xxxxxxxxx>

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

Signed-off-by: Victor Liu <jingwei.liuxi@xxxxxxxxx>
---
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