Re: [PATCH 2/3] lkdtm/perms: Check possible NULL pointer returned by kmalloc(),vmalloc()

From: tury
Date: Mon Jul 25 2022 - 05:54:25 EST


When there is insufficient memory, the allocation will fail.

the return value is void,so i think it is ok .

should i have changed comment to something like this ?

As the possible alloc failure of the kmalloc() and vmalloc(),
the return pointer could be NULL.therefore it should be better to check it.


在 2022年07月25日 16:42, Greg KH 写道:
On Mon, Jul 25, 2022 at 04:11:53PM +0800, Ren Yu wrote:
As the possible alloc failure of the kmalloc() and vmalloc(),the
return pointer could be NULL.therefore it should be better to check it.

Signed-off-by: Ren Yu <renyu@xxxxxxxxxxxx>
---
drivers/misc/lkdtm/perms.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/drivers/misc/lkdtm/perms.c b/drivers/misc/lkdtm/perms.c
index b93404d65650..34b43b9ea1f1 100644
--- a/drivers/misc/lkdtm/perms.c
+++ b/drivers/misc/lkdtm/perms.c
@@ -180,6 +180,9 @@ static void lkdtm_EXEC_STACK(void)
static void lkdtm_EXEC_KMALLOC(void)
{
u32 *kmalloc_area = kmalloc(EXEC_SIZE, GFP_KERNEL);
+ if (!kmalloc_area)
+ return;
+
Always run checkpatch on your patches so that grumpy maintainers do not
have to tell you to run checkpatch on your patches...

Also, shouldn't this return an error?

But most importantly, how can this ever fail?

thanks,

greg k-h