2022-03-24 18:47:32

by QintaoShen

[permalink] [raw]
Subject: [PATCH v1] lkdtm/fortify: Check for NULL return of kmalloc()

The memory allocation function kmalloc() may return a NULL pointer.
If 'src' is NULL, the strscpy() would copy the string to a NULL address.

Therefore, it is better to check the return value of kmalloc() to avoid the confusion.

Signed-off-by: QintaoShen <[email protected]>
---
drivers/misc/lkdtm/fortify.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/drivers/misc/lkdtm/fortify.c b/drivers/misc/lkdtm/fortify.c
index ab33bb5..93eeaef 100644
--- a/drivers/misc/lkdtm/fortify.c
+++ b/drivers/misc/lkdtm/fortify.c
@@ -41,6 +41,9 @@ void lkdtm_FORTIFIED_SUBOBJECT(void)
char *src;

src = kmalloc(size, GFP_KERNEL);
+ if (!src)
+ return ;
+
strscpy(src, "over ten bytes", size);
size = strlen(src) + 1;

--
2.7.4