USB: Fix NULLPTR dereference when serial# is unset
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.
--- drivers/usb/gadget/g_dnl.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/gadget/g_dnl.c +2 -1
@@ -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
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 ?
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 ?
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
@@ -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);