[PATCH] lkdtm: add checks for the return of memory allocation functions

From: xkernel . wang
Date: Fri Mar 04 2022 - 05:31:03 EST


From: Xiaoke Wang <xkernel.wang@xxxxxxxxxxx>

kmalloc() and vmalloc() are all memory allocation functions which can
return NULL when some internal errors happen. So it is better to check
the return value of it for better testing the preset purpose.

Signed-off-by: Xiaoke Wang <xkernel.wang@xxxxxxxxxxx>
---
drivers/misc/lkdtm/perms.c | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/drivers/misc/lkdtm/perms.c b/drivers/misc/lkdtm/perms.c
index 2dede2e..3eabc6f 100644
--- a/drivers/misc/lkdtm/perms.c
+++ b/drivers/misc/lkdtm/perms.c
@@ -138,6 +138,10 @@ void lkdtm_EXEC_STACK(void)
void lkdtm_EXEC_KMALLOC(void)
{
u32 *kmalloc_area = kmalloc(EXEC_SIZE, GFP_KERNEL);
+ if (!kmalloc_area) {
+ pr_warn("Failed to allocate memory\n");
+ return;
+ }
execute_location(kmalloc_area, CODE_WRITE);
kfree(kmalloc_area);
}
@@ -145,6 +149,10 @@ void lkdtm_EXEC_KMALLOC(void)
void lkdtm_EXEC_VMALLOC(void)
{
u32 *vmalloc_area = vmalloc(EXEC_SIZE);
+ if (!vmalloc_area) {
+ pr_warn("Failed to allocate memory\n");
+ return;
+ }
execute_location(vmalloc_area, CODE_WRITE);
vfree(vmalloc_area);
}
--