Skip to contentSkip to conversation

cmd: extension: Fix possible null pointer dereference

PATCH In review Kory Maincent 3 participants 4 messages Nov 12, 2025 – Nov 17, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cmd: extension: Fix possible null pointer dereference

From: "Kory Maincent (TI.com)" <[email protected]>

Add a null check for extension_list before calling alist_get_ptr() to prevent a potential null pointer dereference. If extension_get_list() returns NULL, alist_get_ptr() would attempt to dereference it, leading to undefined behavior.

The fix ensures both extension_list and its first element are checked before proceeding with the list operation.

Fixes 2d12958ee71b ("boot: Remove legacy extension board support")
---
 cmd/extension_board.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cmd/extension_board.c +1 -1
index 86e4795ba8a..1b92be82611 100644
@@ -99,7 +99,7 @@ static int do_extension_list(struct cmd_tbl *cmdtp, int flag,
 	int i = 0;
 
 	extension_list = extension_get_list();
-	if (!alist_get_ptr(extension_list, 0)) {
+	if (!extension_list || !alist_get_ptr(extension_list, 0)) {
 		printf("No extension registered - Please run \"extension scan\"\n");
 		return CMD_RET_SUCCESS;
 	}
Signature · 1 line
2.43.0
#1 lore raw

Hi Köry,

Quoted 22 lines · Kory Maincent

From: "Kory Maincent (TI.com)" <[email protected]>

Add a null check for extension_list before calling alist_get_ptr() to prevent a potential null pointer dereference. If extension_get_list() returns NULL, alist_get_ptr() would attempt to dereference it, leading to undefined behavior.

The fix ensures both extension_list and its first element are checked before proceeding with the list operation.

Fixes 2d12958ee71b ("boot: Remove legacy extension board support")
---
  cmd/extension_board.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
cmd/extension_board.c +0 -0
index 86e4795ba8a..1b92be82611 100644

cmd/extension_board.c static int do_extension_list(struct cmd_tbl *cmdtp, int flag, · line 102

Quoted 5 lines
cmd/extension_board.c +1 -1
@@ -99,7 +99,7 @@ static int do_extension_list(struct cmd_tbl *cmdtp, int flag,
  	int i = 0;
 
  	extension_list = extension_get_list();
-	if (!alist_get_ptr(extension_list, 0)) {
+	if (!extension_list || !alist_get_ptr(extension_list, 0)) {

Is it possible that one runs extension scan which returns no supported extension (alist_empty(), the default state before calling the scan() ops for the extension uclass driver)?

In which case, we should only check whether extension_list is NULL and not whether there's at least one extension?

Or at least have a different error code between "scan wasn't run" and "scan returned no extension", if that is a distinction we can make?

Cheers,
Quentin

#2 lore raw
Quoted 27 lines · On Wed, 12 Nov 2025 15:12:34 +0100 Quentin Schulz <[email protected]>…

Hi Köry,

Quoted 24 lines · Kory Maincent

From: "Kory Maincent (TI.com)" <[email protected]>

Add a null check for extension_list before calling alist_get_ptr() to prevent a potential null pointer dereference. If extension_get_list() returns NULL, alist_get_ptr() would attempt to dereference it, leading to undefined behavior.

The fix ensures both extension_list and its first element are checked before proceeding with the list operation.

Fixes 2d12958ee71b ("boot: Remove legacy extension board support")
---
  cmd/extension_board.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
cmd/extension_board.c +0 -0
index 86e4795ba8a..1b92be82611 100644
@@ -99,7 +99,7 @@ static int do_extension_list(struct cmd_tbl *cmdtp, int
 flag, int i = 0;
 
Quoted 7 lines

cmd/extension_board.c static int do_extension_list(struct cmd_tbl *cmdtp, int flag, · line 102

Quoted 3 lines
cmd/extension_board.c +1 -1
  	extension_list = extension_get_list();
-	if (!alist_get_ptr(extension_list, 0)) {
+	if (!extension_list || !alist_get_ptr(extension_list, 0)) {  

Is it possible that one runs extension scan which returns no supported extension (alist_empty(), the default state before calling the scan() ops for the extension uclass driver)?

Yes, we can have no extension plugged, therefore the scan won't find any extension. i.e. empty list.

Quoted 2 lines

In which case, we should only check whether extension_list is NULL and not whether there's at least one extension?

extension_list being NULL mean that there is no support for board specific extension driver. Maybe we could have another error message for that case. "Extension not supported"

Quoted 2 lines

Or at least have a different error code between "scan wasn't run" and "scan returned no extension", if that is a distinction we can make?

We could plug or unplug an extension. So it could change over time. I wrote it similarly to mmc rescan command.

Regards,

Signature · 3 lines
Köry Maincent, Bootlin
Embedded Linux and kernel engineering
https://bootlin.com
#3 lore raw
In reply to Kory Maincent
Quoted 7 lines · Kory Maincent

From: "Kory Maincent (TI.com)" <[email protected]>

Add a null check for extension_list before calling alist_get_ptr() to prevent a potential null pointer dereference. If extension_get_list() returns NULL, alist_get_ptr() would attempt to dereference it, leading to undefined behavior.

Quoted 5 lines

The fix ensures both extension_list and its first element are checked before proceeding with the list operation.

Fixes 2d12958ee71b ("boot: Remove legacy extension board support")

Please, add

Addresses-Coverity-ID: 638557 - Null pointer dereferences
(NULL_RETURNS)

so that we know that the Coverity issue was addressed.

Quoted 10 lines
---
  cmd/extension_board.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
cmd/extension_board.c +0 -0
index 86e4795ba8a..1b92be82611 100644

cmd/extension_board.c static int do_extension_list(struct cmd_tbl *cmdtp, int flag, · line 104

Quoted 7 lines
cmd/extension_board.c +1 -1
@@ -99,7 +99,7 @@ static int do_extension_list(struct cmd_tbl *cmdtp, int flag,
  	int i = 0;
 
  	extension_list = extension_get_list();
-	if (!alist_get_ptr(extension_list, 0)) {
+	if (!extension_list || !alist_get_ptr(extension_list, 0)) {
  		printf("No extension registered - Please run \"extension scan\"\n");
  		return CMD_RET_SUCCESS;

If extension_list is NULL scanning, there is no extension device and nothing to scan. We should separate the two cases.

See my suggested patch

[PATCH 1/1] cmd/extension: avoid NULL pointer dereference
https://lore.kernel.org/u-boot/[email protected]/

Best regards

Heinrich

Quoted 1 line
cmd/extension_board.c +0 -0
  	}