[PATCH] arm: mach: fix missing NULL checks and memory leaks in DT property setup
From: Li Jun
Date: Wed Mar 18 2026 - 06:31:49 EST
Add proper NULL pointer checks after memory allocation calls (kzalloc
and kstrdup) in ARM machine-specific device tree property manipulation
code. Previously, allocation failures would result in NULL pointer
dereferences when accessing the properties, or memory leaks when partial
allocations succeeded.
Changes:
- board-v7.c: Check kzalloc/kstrdup returns, free resources on failure
- coherency.c: Check kzalloc/kstrdup returns, free property on failure
- versatile.c: Check kstrdup returns, free property on failure.
Signed-off-by: Li Jun <lijun01@xxxxxxxxxx>
---
arch/arm/mach-mvebu/board-v7.c | 10 ++++++++++
arch/arm/mach-mvebu/coherency.c | 6 ++++++
arch/arm/mach-versatile/versatile.c | 8 ++++++++
3 files changed, 24 insertions(+)
diff --git a/arch/arm/mach-mvebu/board-v7.c b/arch/arm/mach-mvebu/board-v7.c
index 04ad651d13a0..b56ac48b1759 100644
--- a/arch/arm/mach-mvebu/board-v7.c
+++ b/arch/arm/mach-mvebu/board-v7.c
@@ -128,11 +128,21 @@ 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);
+ if (!new_compat->name) {
+ kfree(new_compat);
+ return;
+ }
new_compat->length = sizeof("marvell,mv78230-a0-i2c");
new_compat->value = kstrdup("marvell,mv78230-a0-i2c",
GFP_KERNEL);
+ if (!new_compat->value) {
+ kfree(new_compat);
+ return;
+ }
of_update_property(np, new_compat);
}
diff --git a/arch/arm/mach-mvebu/coherency.c b/arch/arm/mach-mvebu/coherency.c
index a6b621ff0b87..f9411e16ddbb 100644
--- a/arch/arm/mach-mvebu/coherency.c
+++ b/arch/arm/mach-mvebu/coherency.c
@@ -191,7 +191,13 @@ 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) {
+ kfree(p);
+ return;
+ }
of_add_property(cache_dn, p);
}
}
diff --git a/arch/arm/mach-versatile/versatile.c b/arch/arm/mach-versatile/versatile.c
index f0c80d4663ca..ee9238a88b8d 100644
--- a/arch/arm/mach-versatile/versatile.c
+++ b/arch/arm/mach-versatile/versatile.c
@@ -147,7 +147,15 @@ static void __init versatile_dt_pci_init(void)
goto out_put_node;
newprop->name = kstrdup("status", GFP_KERNEL);
+ if (!newprop->name) {
+ kfree(newprop);
+ goto out_put_node;
+ }
newprop->value = kstrdup("disabled", GFP_KERNEL);
+ if (!newprop->value) {
+ kfree(newprop);
+ goto out_put_node;
+ }
newprop->length = sizeof("disabled");
of_update_property(np, newprop);
--
2.25.1