dm: core: Don't allow ofnode_to_fdt() to return NULL
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
@@ -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]>
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 asfdt_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.Signed-off-by Romain Gantois <[email protected]>---
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@@ -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-95a9f357819eBest regards,
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 asfdt_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.Signed-off-by Romain Gantois <[email protected]>--- drivers/core/ofnode.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)drivers/core/ofnode.c +9
-5@@ -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
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 asfdt_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.Signed-off-by Romain Gantois <[email protected]>
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
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
asfdt_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.Signed-off-by Romain Gantois <[email protected]>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