Skip to contentSkip to conversation

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

PATCH In review Romain Gantois 3 participants 3 messages Feb 17 – Mar 3

lore
Share & tools
lore
#0 lore raw

PATCH v2 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.

---
Changes in v2:
- Added back a dropped cast from cont void* to void*
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 drivers/core/ofnode.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/core/ofnode.c +9 -4
index cf1cf8abfbe..3a36b6fdd03 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 = (void *)gd->fdt_blob;
+
+	assert(fdt);
 
-	/* Use the control FDT by default */
-	return (void *)gd->fdt_blob;
+	return fdt;
 }
 
 /**

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

Best regards,

Signature · 1 line
Romain Gantois <[email protected]>
Reviewed-by on v2 1/1
#1 lore raw
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.

Quoted 7 lines

---
Changes in v2:
- Added back a dropped cast from cont void* to void*
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 drivers/core/ofnode.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
Quoted 39 lines
drivers/core/ofnode.c +9 -5
index cf1cf8abfbe..3a36b6fdd03 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 = (void *)gd->fdt_blob;
+
+       assert(fdt);
 
-       /* Use the control FDT by default */
-       return (void *)gd->fdt_blob;
+       return fdt;
 }
 
 /**
 
---

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

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

#2 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.

[...]

Applied to u-boot/master, thanks!

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

      commit: 39e1ccc777563d8c769531f1dfce24d38d023086
Signature · 1 line
Tom