Skip to contentSkip to conversation

dm: core: Don't allow ofnode_to_fdt() to return NULL

PATCH Superseded Romain Gantois 4 participants 5 messages Feb 10 – Feb 17

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dm: core: Don't allow ofnode_to_fdt() to return NULL

The ofnode_to_fdt() function may return a NULL pointer in multiple cases. Or, this function's return value is often passed directly to functions such as fdt_getprop() which end up dereferencing it, thus causing a NULL pointer exception.

Don't allow ofnode_to_fdt() to return NULL, to avoid a NULL pointer dereference.

---
 drivers/core/ofnode.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/core/ofnode.c +9 -4
index cf1cf8abfbe..af24a2c533c 100644
@@ -164,15 +164,20 @@ void *ofnode_lookup_fdt(ofnode node)
 
 void *ofnode_to_fdt(ofnode node)
 {
+	void *fdt;
+
 #ifdef OF_CHECKS
 	if (of_live_active())
-		return NULL;
+		panic("%s called with live tree in use!\n", __func__);
 #endif
 	if (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) && ofnode_valid(node))
-		return ofnode_lookup_fdt(node);
+		fdt = ofnode_lookup_fdt(node);
+	else
+		fdt = gd->fdt_blob;
+
+	assert(fdt);
 
-	/* Use the control FDT by default */
-	return (void *)gd->fdt_blob;
+	return fdt;
 }
 
 /**

---
base-commit: b99da05e1538b8fa153322da82917af2aa27e1d6
change-id: 20260210-ofnode-to-fdt-null-95a9f357819e

Best regards,

Signature · 1 line
Romain Gantois <[email protected]>
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Romain Gantois

The ofnode_to_fdt() function may return a NULL pointer in multiple cases. Or, this function's return value is often passed directly to functions such as fdt_getprop() which end up dereferencing it, thus causing a NULL pointer exception.

Don't allow ofnode_to_fdt() to return NULL, to avoid a NULL pointer dereference.

---

Hi Romain,

Best regards,
Raphaël

Quoted 38 lines
 drivers/core/ofnode.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/core/ofnode.c +9 -5
index cf1cf8abfbe..af24a2c533c 100644
@@ -164,15 +164,20 @@ void *ofnode_lookup_fdt(ofnode node)
 
 void *ofnode_to_fdt(ofnode node)
 {
+	void *fdt;
+
 #ifdef OF_CHECKS
 	if (of_live_active())
-		return NULL;
+		panic("%s called with live tree in use!\n", __func__);
 #endif
 	if (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) && ofnode_valid(node))
-		return ofnode_lookup_fdt(node);
+		fdt = ofnode_lookup_fdt(node);
+	else
+		fdt = gd->fdt_blob;
+
+	assert(fdt);
 
-	/* Use the control FDT by default */
-	return (void *)gd->fdt_blob;
+	return fdt;
 }
 
 /**
 
---

base-commit: b99da05e1538b8fa153322da82917af2aa27e1d6
change-id: 20260210-ofnode-to-fdt-null-95a9f357819e

Best regards,

Reviewed-by on v1 1/1
#2 lore raw
In reply to Romain Gantois

Hi Romain,

Quoted 48 lines · Romain Gantois

The ofnode_to_fdt() function may return a NULL pointer in multiple cases. Or, this function's return value is often passed directly to functions such as fdt_getprop() which end up dereferencing it, thus causing a NULL pointer exception.

Don't allow ofnode_to_fdt() to return NULL, to avoid a NULL pointer dereference.

---
 drivers/core/ofnode.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/core/ofnode.c +9 -5
index cf1cf8abfbe..af24a2c533c 100644
@@ -164,15 +164,20 @@ void *ofnode_lookup_fdt(ofnode node)
 
 void *ofnode_to_fdt(ofnode node)
 {
+       void *fdt;
+
 #ifdef OF_CHECKS
        if (of_live_active())
-               return NULL;
+               panic("%s called with live tree in use!\n", __func__);
 #endif
        if (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) && ofnode_valid(node))
-               return ofnode_lookup_fdt(node);
+               fdt = ofnode_lookup_fdt(node);
+       else
+               fdt = gd->fdt_blob;
+
+       assert(fdt);
 
-       /* Use the control FDT by default */
-       return (void *)gd->fdt_blob;
+       return fdt;
 }
 
 /**
 
---

base-commit: b99da05e1538b8fa153322da82917af2aa27e1d6
change-id: 20260210-ofnode-to-fdt-null-95a9f357819e

Quoted 4 lines

Best regards,
--
Romain Gantois <[email protected]>

This seems to me to be a hole in the checking in fdt_check_node_offset_()

Regards,
Simon

#3 lore raw
In reply to Romain Gantois
Quoted 9 lines · Romain Gantois

The ofnode_to_fdt() function may return a NULL pointer in multiple cases. Or, this function's return value is often passed directly to functions such as fdt_getprop() which end up dereferencing it, thus causing a NULL pointer exception.

Don't allow ofnode_to_fdt() to return NULL, to avoid a NULL pointer dereference.

This leads to failures to build such as:
+(neu6a-io-rk3588) drivers/core/ofnode.c: In function 'ofnode_to_fdt':
+(neu6a-io-rk3588) drivers/core/ofnode.c:176:21: error: assignment discards 'const' qualifie from pointer target type [-Werror=discarded-qualifiers]
+(neu6a-io-rk3588) 176 | fdt = gd->fdt_blob;
+(neu6a-io-rk3588) | ^
+(neu6a-io-rk3588) cc1: all warnings being treated as errors

Signature · 1 line
Tom
#4 lore raw

Hi Tom,

Quoted 17 lines · On Monday, 16 February 2026 18:50:20 CET Tom Rini wrote:
Quoted 11 lines · Romain Gantois

The ofnode_to_fdt() function may return a NULL pointer in multiple cases. Or, this function's return value is often passed directly to functions such
as fdt_getprop() which end up dereferencing it, thus causing a NULL pointer
exception.

Don't allow ofnode_to_fdt() to return NULL, to avoid a NULL pointer dereference.

This leads to failures to build such as:

(quoted diff) +2 -0
+(neu6a-io-rk3588) drivers/core/ofnode.c: In function 'ofnode_to_fdt':
+(neu6a-io-rk3588) drivers/core/ofnode.c:176:21: error: assignment discards

'const' qualifie from pointer target type [-Werror=discarded-qualifiers]

Review

Quoted 3 lines
(quoted diff) +3 -0
+(neu6a-io-rk3588)   176 |                 fdt = gd->fdt_blob;
+(neu6a-io-rk3588)       |                     ^
+(neu6a-io-rk3588) cc1: all warnings being treated as errors

Indeed, looks like the dropped (void *) cast is responsible for that.

Thanks,

Signature · 3 lines
Romain Gantois, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com