All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ARM: mvebu: check the validation of memory allocation
@ 2022-03-25  8:55 ` xkernel.wang
  0 siblings, 0 replies; 6+ messages in thread
From: xkernel.wang @ 2022-03-25  8:55 UTC (permalink / raw)
  To: andrew, gregory.clement, sebastian.hesselbarth
  Cc: linux, linux-arm-kernel, linux-kernel, Xiaoke Wang

From: Xiaoke Wang <xkernel.wang@foxmail.com>

kzalloc() and kstrdup() are memory allocation functions which can return
NULL when some internal memory errors happen. So it is better to check
the return value of them to prevent potential wrong memory access or
memory leak.

Signed-off-by: Xiaoke Wang <xkernel.wang@foxmail.com>
---
 arch/arm/mach-mvebu/board-v7.c  | 8 ++++++++
 arch/arm/mach-mvebu/coherency.c | 6 ++++++
 2 files changed, 14 insertions(+)

diff --git a/arch/arm/mach-mvebu/board-v7.c b/arch/arm/mach-mvebu/board-v7.c
index d2df5ef..86d1f4e 100644
--- a/arch/arm/mach-mvebu/board-v7.c
+++ b/arch/arm/mach-mvebu/board-v7.c
@@ -128,11 +128,19 @@ static void __init i2c_quirk(void)
 		struct property *new_compat;
 
 		new_compat = kzalloc(sizeof(*new_compat), GFP_KERNEL);
+		if (!new_compat)
+			continue;
 
 		new_compat->name = kstrdup("compatible", GFP_KERNEL);
 		new_compat->length = sizeof("marvell,mv78230-a0-i2c");
 		new_compat->value = kstrdup("marvell,mv78230-a0-i2c",
 						GFP_KERNEL);
+		if (!new_compat->name || !new_compat->value) {
+			kfree(new_compat->name);
+			kfree(new_compat->value);
+			kfree(new_compat);
+			continue;
+		}
 
 		of_update_property(np, new_compat);
 	}
diff --git a/arch/arm/mach-mvebu/coherency.c b/arch/arm/mach-mvebu/coherency.c
index 49e3c8d..eb6b349 100644
--- a/arch/arm/mach-mvebu/coherency.c
+++ b/arch/arm/mach-mvebu/coherency.c
@@ -194,7 +194,13 @@ static void __init armada_375_380_coherency_init(struct device_node *np)
 		struct property *p;
 
 		p = kzalloc(sizeof(*p), GFP_KERNEL);
+		if (!p)
+			continue;
 		p->name = kstrdup("arm,io-coherent", GFP_KERNEL);
+		if (!p->name) {
+			kfree(p);
+			continue;
+		}
 		of_add_property(cache_dn, p);
 	}
 }
-- 

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH] ARM: mvebu: check the validation of memory allocation
@ 2022-03-25  8:55 ` xkernel.wang
  0 siblings, 0 replies; 6+ messages in thread
From: xkernel.wang @ 2022-03-25  8:55 UTC (permalink / raw)
  To: andrew, gregory.clement, sebastian.hesselbarth
  Cc: linux, linux-arm-kernel, linux-kernel, Xiaoke Wang

From: Xiaoke Wang <xkernel.wang@foxmail.com>

kzalloc() and kstrdup() are memory allocation functions which can return
NULL when some internal memory errors happen. So it is better to check
the return value of them to prevent potential wrong memory access or
memory leak.

Signed-off-by: Xiaoke Wang <xkernel.wang@foxmail.com>
---
 arch/arm/mach-mvebu/board-v7.c  | 8 ++++++++
 arch/arm/mach-mvebu/coherency.c | 6 ++++++
 2 files changed, 14 insertions(+)

diff --git a/arch/arm/mach-mvebu/board-v7.c b/arch/arm/mach-mvebu/board-v7.c
index d2df5ef..86d1f4e 100644
--- a/arch/arm/mach-mvebu/board-v7.c
+++ b/arch/arm/mach-mvebu/board-v7.c
@@ -128,11 +128,19 @@ static void __init i2c_quirk(void)
 		struct property *new_compat;
 
 		new_compat = kzalloc(sizeof(*new_compat), GFP_KERNEL);
+		if (!new_compat)
+			continue;
 
 		new_compat->name = kstrdup("compatible", GFP_KERNEL);
 		new_compat->length = sizeof("marvell,mv78230-a0-i2c");
 		new_compat->value = kstrdup("marvell,mv78230-a0-i2c",
 						GFP_KERNEL);
+		if (!new_compat->name || !new_compat->value) {
+			kfree(new_compat->name);
+			kfree(new_compat->value);
+			kfree(new_compat);
+			continue;
+		}
 
 		of_update_property(np, new_compat);
 	}
diff --git a/arch/arm/mach-mvebu/coherency.c b/arch/arm/mach-mvebu/coherency.c
index 49e3c8d..eb6b349 100644
--- a/arch/arm/mach-mvebu/coherency.c
+++ b/arch/arm/mach-mvebu/coherency.c
@@ -194,7 +194,13 @@ static void __init armada_375_380_coherency_init(struct device_node *np)
 		struct property *p;
 
 		p = kzalloc(sizeof(*p), GFP_KERNEL);
+		if (!p)
+			continue;
 		p->name = kstrdup("arm,io-coherent", GFP_KERNEL);
+		if (!p->name) {
+			kfree(p);
+			continue;
+		}
 		of_add_property(cache_dn, p);
 	}
 }
-- 

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] ARM: mvebu: check the validation of memory allocation
  2022-03-25  8:55 ` xkernel.wang
@ 2022-04-29 15:19   ` Russell King (Oracle)
  -1 siblings, 0 replies; 6+ messages in thread
From: Russell King (Oracle) @ 2022-04-29 15:19 UTC (permalink / raw)
  To: xkernel.wang
  Cc: andrew, gregory.clement, sebastian.hesselbarth, linux-arm-kernel,
	linux-kernel

On Fri, Mar 25, 2022 at 04:55:18PM +0800, xkernel.wang@foxmail.com wrote:
> From: Xiaoke Wang <xkernel.wang@foxmail.com>
> 
> kzalloc() and kstrdup() are memory allocation functions which can return
> NULL when some internal memory errors happen. So it is better to check
> the return value of them to prevent potential wrong memory access or
> memory leak.

Are these failures silent, or will they be reported in the kernel log to
make their failure obvious to those who debug the kernel?

-- 
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] ARM: mvebu: check the validation of memory allocation
@ 2022-04-29 15:19   ` Russell King (Oracle)
  0 siblings, 0 replies; 6+ messages in thread
From: Russell King (Oracle) @ 2022-04-29 15:19 UTC (permalink / raw)
  To: xkernel.wang
  Cc: andrew, gregory.clement, sebastian.hesselbarth, linux-arm-kernel,
	linux-kernel

On Fri, Mar 25, 2022 at 04:55:18PM +0800, xkernel.wang@foxmail.com wrote:
> From: Xiaoke Wang <xkernel.wang@foxmail.com>
> 
> kzalloc() and kstrdup() are memory allocation functions which can return
> NULL when some internal memory errors happen. So it is better to check
> the return value of them to prevent potential wrong memory access or
> memory leak.

Are these failures silent, or will they be reported in the kernel log to
make their failure obvious to those who debug the kernel?

-- 
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] ARM: mvebu: check the validation of memory allocation
  2022-04-29 15:19   ` Russell King (Oracle)
@ 2022-05-01  7:01     ` xkernel.wang
  -1 siblings, 0 replies; 6+ messages in thread
From: xkernel.wang @ 2022-05-01  7:01 UTC (permalink / raw)
  To: linux
  Cc: andrew, gregory.clement, sebastian.hesselbarth, linux-arm-kernel,
	linux-kernel

On Friday, April 29, 2022 11:19 PM +0800, linux@armlinux.org.uk wrote:
> Are these failures silent, or will they be reported in the kernel log to
> make their failure obvious to those who debug the kernel?

According to chapter 14 of the coding style document:
"These generic allocation functions all emit a stack dump on failure when used
without __GFP_NOWARN so there is no use in emitting an additional failure
message when NULL is returned."

I think that these failures are not silent since __GFP_NOWARN is not
enabled.

Regards,
Xiaoke Wang


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] ARM: mvebu: check the validation of memory allocation
@ 2022-05-01  7:01     ` xkernel.wang
  0 siblings, 0 replies; 6+ messages in thread
From: xkernel.wang @ 2022-05-01  7:01 UTC (permalink / raw)
  To: linux
  Cc: andrew, gregory.clement, sebastian.hesselbarth, linux-arm-kernel,
	linux-kernel

On Friday, April 29, 2022 11:19 PM +0800, linux@armlinux.org.uk wrote:
> Are these failures silent, or will they be reported in the kernel log to
> make their failure obvious to those who debug the kernel?

According to chapter 14 of the coding style document:
"These generic allocation functions all emit a stack dump on failure when used
without __GFP_NOWARN so there is no use in emitting an additional failure
message when NULL is returned."

I think that these failures are not silent since __GFP_NOWARN is not
enabled.

Regards,
Xiaoke Wang


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-05-01  7:03 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-25  8:55 [PATCH] ARM: mvebu: check the validation of memory allocation xkernel.wang
2022-03-25  8:55 ` xkernel.wang
2022-04-29 15:19 ` Russell King (Oracle)
2022-04-29 15:19   ` Russell King (Oracle)
2022-05-01  7:01   ` xkernel.wang
2022-05-01  7:01     ` xkernel.wang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.