Skip to contentSkip to conversation

USB: Fix NULLPTR dereference when serial# is unset

PATCH Superseded Michael Ferolito 2 participants 4 messages Jan 27, 2025 USB

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 USB: Fix NULLPTR dereference when serial# is unset

The current behaviour of this function will dereference a null pointer if the serial# environment variable is unset. This was discovered on a board where U-Boot did not have access to the first 256MB of ram, resulting in a board crash.
In the event that U-Boot has full access to memory, it will still read from address 0, which is probably not optimal.
This simple check is enough to fix it.

Cc Marek Vasut <[email protected]>
Cc Heiko Schocher <[email protected]>
Cc Lukasz Majewski <[email protected]>
Cc Kyungmin Park <[email protected]>
---
 drivers/usb/gadget/g_dnl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/gadget/g_dnl.c +2 -1
index 631969b340..d56f9c485e 100644
@@ -50,7 +50,8 @@ static const char manufacturer[] = CONFIG_USB_GADGET_MANUFACTURER;
 void g_dnl_set_serialnumber(char *s)
 {
 	memset(g_dnl_serial, 0, MAX_STRING_SERIAL);
-	strncpy(g_dnl_serial, s, MAX_STRING_SERIAL - 1);
+	if (s)
+		strncpy(g_dnl_serial, s, MAX_STRING_SERIAL - 1);
 }
 
 static struct usb_device_descriptor device_desc = {
Signature · 1 line
2.48.1
#1 lore raw
Quoted 7 lines · Michael Ferolito

The current behaviour of this function will dereference a null pointer if the serial# environment variable is unset. This was discovered on a board where U-Boot did not have access to the first 256MB of ram, resulting in a board crash.
In the event that U-Boot has full access to memory, it will still read from address 0, which is probably not optimal.
This simple check is enough to fix it.

How does one trigger this problem ?

author
#2 lore raw

The problem can be reproduced with U-Boot's sandbox with the default config. Then, run the following commands at the shell:
Hit any key to stop autoboot: 0
=> env set serial# test
=> env default -f serial#
at which point the program will crash.

The following change will show that the env default -f command causes the null pointer dereference

void g_dnl_set_serialnumber(char *s)

 {
        memset(g_dnl_serial, 0, MAX_STRING_SERIAL);
+       printf("Reading serial from address %p\n", s);
        strncpy(g_dnl_serial, s, MAX_STRING_SERIAL - 1);

}

Quoted 10 lines · Marek Vasut
Quoted 7 lines · Michael Ferolito

The current behaviour of this function will dereference a null pointer if the serial# environment variable is unset. This was discovered on a board where U-Boot did not have access to the first 256MB of ram, resulting in a board crash.
In the event that U-Boot has full access to memory, it will still read from address 0, which is probably not optimal.
This simple check is enough to fix it.

How does one trigger this problem ?

#3 lore raw
Quoted 6 lines · Michael

The problem can be reproduced with U-Boot's sandbox with the default config. Then, run the following commands at the shell:
Hit any key to stop autoboot: 0
=> env set serial# test
=> env default -f serial#
at which point the program will crash.

Maybe the fix needs to be one level higher ?

drivers/usb/gadget/g_dnl.c +0 -0
index 631969b3405..f2540eb6ded 100644
@@ -207,7 +207,8 @@ void g_dnl_clear_detach(void)
 static int on_serialno(const char *name, const char *value, enum 

env_op op,
int flags)
{
- g_dnl_set_serialnumber((char *)value);
+ if (value)

+               g_dnl_set_serialnumber((char *)value);
        return 0;

}
U_BOOT_ENV_CALLBACK(serialno, on_serialno);