Skip to contentSkip to conversation

lkdtm/perms: Check possible NULL pointer returned by kmalloc(),vmalloc()

PATCH In review Ren Yu 3 participants 6 messages Jul 25, 2022 – Jul 27, 2022

Subsystems (2)
lore
Share & tools
lore

Ren Yu

author
#0 lore raw

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

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

---
 drivers/misc/lkdtm/perms.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/misc/lkdtm/perms.c +6 -0
index b93404d65650..34b43b9ea1f1 100644
@@ -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;
+
 	execute_location(kmalloc_area, CODE_WRITE);
 	kfree(kmalloc_area);
 }
@@ -187,6 +190,9 @@ static void lkdtm_EXEC_KMALLOC(void)
 static void lkdtm_EXEC_VMALLOC(void)
 {
 	u32 *vmalloc_area = vmalloc(EXEC_SIZE);
+	if (!vmalloc_area)
+		return;
+
 	execute_location(vmalloc_area, CODE_WRITE);
 	vfree(vmalloc_area);
 }
Signature · 1 line
2.11.0
maintainer
#1 lore raw
Quoted 13 lines · Ren Yu

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

---
 drivers/misc/lkdtm/perms.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/misc/lkdtm/perms.c +0 -0
index b93404d65650..34b43b9ea1f1 100644

drivers/misc/lkdtm/perms.c static void lkdtm_EXEC_STACK(void) · line 185

Quoted 6 lines
drivers/misc/lkdtm/perms.c +3 -0
@@ -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

author
#2 lore raw

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.

Quoted 31 lines · Greg KH
Quoted 13 lines · Ren Yu

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

---
  drivers/misc/lkdtm/perms.c | 6 ++++++
  1 file changed, 6 insertions(+)
drivers/misc/lkdtm/perms.c +0 -0
index b93404d65650..34b43b9ea1f1 100644

drivers/misc/lkdtm/perms.c static void lkdtm_EXEC_STACK(void) · line 185

Quoted 6 lines
drivers/misc/lkdtm/perms.c +3 -0
@@ -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

maintainer
#3 lore raw

A: http://en.wikipedia.org/wiki/Top_post
Q: Were do I find info about this thing called top-posting?
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?

A: No.
Q: Should I include quotations after my reply?

http://daringfireball.net/2007/07/on_top

Quoted 1 line · tury

When there is insufficient memory, the allocation will fail.

And have you ever seen that happen here? The issue is for small allocations, they never will fail.

Quoted 1 line

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

Why?

Quoted 4 lines

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.

Please wrap your lines properly, and use '.' correctly.

thanks,

greg k-h

author
#4 lore raw
Quoted 12 lines · Greg KH

A: http://en.wikipedia.org/wiki/Top_post
Q: Were do I find info about this thing called top-posting?
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?

A: No.
Q: Should I include quotations after my reply?

http://daringfireball.net/2007/07/on_top

Quoted 7 lines
Quoted 1 line · tury

When there is insufficient memory, the allocation will fail.

And have you ever seen that happen here? The issue is for small allocations, they never will fail.

Quoted 1 line

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

Why?

Because the function lkdtm_EXEC_KMALLOC() declaration is void,and The return value is not checked elsewhere.
Should I add some warning messages?

Quoted 10 lines
Quoted 5 lines

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.

Please wrap your lines properly, and use '.' correctly.

thanks,

greg k-h

Thank you for your help and patience,

Quoted 1 line
#5 lore raw
Quoted 15 lines · tury
Quoted 14 lines · Greg KH

A: http://en.wikipedia.org/wiki/Top_post
Q: Were do I find info about this thing called top-posting?
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?

A: No.
Q: Should I include quotations after my reply?

http://daringfireball.net/2007/07/on_top

Quoted 1 line · tury

When there is insufficient memory, the allocation will fail.

Quoted 8 lines
Quoted 5 lines

And have you ever seen that happen here? The issue is for small allocations, they never will fail.

Quoted 1 line

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

Why?

Because the function lkdtm_EXEC_KMALLOC() declaration is void,and The
return value is not checked elsewhere.
Should I add some warning messages?

Memory allocation failures will already be reported by the allocator, so there is normally no need for an additional message.

Signature · 1 line
Kees Cook