Skip to contentSkip to conversation

ARM: mvebu: potential dereference of null pointer

PATCH In review [email protected] 3 participants 3 messages Jan 19, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ARM: mvebu: potential dereference of null pointer

From: Minghao Chi <[email protected]>

The return value of kzalloc() needs to be checked. To avoid use of null pointer in case of the failure of alloc.

---
 arch/arm/mach-mvebu/board-v7.c  | 4 ++++
 arch/arm/mach-mvebu/coherency.c | 4 ++++
 2 files changed, 8 insertions(+)
arch/arm/mach-mvebu/board-v7.c +4 -0
index fd5d0c8ff695..3c031b2efe16 100644
@@ -125,11 +125,15 @@ static void __init i2c_quirk(void)
 		struct property *new_compat;
 
 		new_compat = kzalloc(sizeof(*new_compat), GFP_KERNEL);
+		if (!new_compat)
+			return;
 
 		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)
+			return;
 
 		of_update_property(np, new_compat);
 	}
arch/arm/mach-mvebu/coherency.c +4 -0
index a6b621ff0b87..8291185c52cc 100644
@@ -191,7 +191,11 @@ static void __init armada_375_380_coherency_init(struct device_node *np)
 		struct property *p;
 
 		p = kzalloc(sizeof(*p), GFP_KERNEL);
+		if (!p)
+			return;
 		p->name = kstrdup("arm,io-coherent", GFP_KERNEL);
+		if (!p->name)
+			return;
 		of_add_property(cache_dn, p);
 	}
 }
Signature · 1 line
2.25.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · On Thu, Jan 19, 2023 at 10:51:18AM +0800, [email protected] wrote:

From: Minghao Chi <[email protected]>

The return value of kzalloc() needs to be checked. To avoid use of null pointer in case of the failure of alloc.

Andrew

#2 lore raw
In reply to [email protected]
Quoted 20 lines · On Thu, Jan 19, 2023 at 10:51:18AM +0800, [email protected] wrote:

From: Minghao Chi <[email protected]>

The return value of kzalloc() needs to be checked. To avoid use of null pointer in case of the failure of alloc.

---
 arch/arm/mach-mvebu/board-v7.c  | 4 ++++
 arch/arm/mach-mvebu/coherency.c | 4 ++++
 2 files changed, 8 insertions(+)
arch/arm/mach-mvebu/board-v7.c +0 -0
index fd5d0c8ff695..3c031b2efe16 100644
@@ -125,11 +125,15 @@ static void __init i2c_quirk(void)
 		struct property *new_compat;
 

arch/arm/mach-mvebu/board-v7.c static void __init i2c_quirk(void) · line 136

Quoted 10 lines
arch/arm/mach-mvebu/board-v7.c +4 -0
 		new_compat = kzalloc(sizeof(*new_compat), GFP_KERNEL);
+		if (!new_compat)
+			return;
 
 		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)
+			return;

... and then someone else comes along and spots that "new_compat" gets leaked, so we get another patch to add a kfree() for new_compat.

Why not do the job properly first time around?

Quoted 7 lines
arch/arm/mach-mvebu/board-v7.c +0 -0
 		of_update_property(np, new_compat);
 	}
arch/arm/mach-mvebu/coherency.c +0 -0
index a6b621ff0b87..8291185c52cc 100644

arch/arm/mach-mvebu/coherency.c static void __init armada_375_380_coherency_init(struct device_node *np) · line 198

Quoted 8 lines
arch/arm/mach-mvebu/coherency.c +4 -0
@@ -191,7 +191,11 @@ static void __init armada_375_380_coherency_init(struct device_node *np)
 		struct property *p;
 
 		p = kzalloc(sizeof(*p), GFP_KERNEL);
+		if (!p)
+			return;
 		p->name = kstrdup("arm,io-coherent", GFP_KERNEL);
+		if (!p->name)
+			return;

Same problem here.

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