Skip to contentSkip to conversation

usb: gadget: g_dnl: Fix NULLPTR dereference when serial# is unset

PATCH In review Michael Ferolito 3 participants 6 messages Jan 28, 2025 – Feb 6, 2025 USB

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 usb: gadget: g_dnl: 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 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..f2540eb6de 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);
Signature · 1 line
2.48.1
Reviewed-by on v4 1/1
#1 lore raw

Hi Michael,

Quoted 8 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

Quoted 7 lines
Cc Marek Vasut <[email protected]>
Cc Heiko Schocher <[email protected]>
Cc Kyungmin Park <[email protected]>
---
  drivers/usb/gadget/g_dnl.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)

Nitpick:
A changelog would be nice to get an idea what has changed from version to version ...

Quoted 6 lines
drivers/usb/gadget/g_dnl.c +0 -0
index 631969b340..f2540eb6de 100644

drivers/usb/gadget/g_dnl.c void g_dnl_clear_detach(void) · line 214

Quoted 9 lines
drivers/usb/gadget/g_dnl.c +2 -1
@@ -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);

Good catch!

bye,
Heiko

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Erika Unter
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52   Fax: +49-8142-66989-80   Email: [email protected]
Reviewed-by on v4 1/1
#2 lore raw
In reply to Michael Ferolito

Hi Michael,

Thank you for the patch.

Quoted 8 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

Quoted 4 lines
Cc Marek Vasut <[email protected]>
Cc Heiko Schocher <[email protected]>
Cc Kyungmin Park <[email protected]>
Quoted 20 lines
---
 drivers/usb/gadget/g_dnl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/gadget/g_dnl.c +2 -1
index 631969b340..f2540eb6de 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);
Signature · 1 line
2.48.1
author
#3 lore raw

Are there any other action items I should take care of?

Quoted 42 lines · Mattijs Korpershoek

Hi Michael,

Thank you for the patch.

Quoted 8 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

Quoted 4 lines
Cc Marek Vasut <[email protected]>
Cc Heiko Schocher <[email protected]>
Cc Kyungmin Park <[email protected]>
Quoted 20 lines
---
 drivers/usb/gadget/g_dnl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/gadget/g_dnl.c +2 -2
index 631969b340..f2540eb6de 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);
--

2.48.1

#4 lore raw

Hi Michael,

Quoted 1 line · Michael

Are there any other action items I should take care of?

TLDR: no.

Details below:
Not at the moment. Usually we give the community some time to review. This time depends on the custodian(maintainer) responsible for taking in the patch.

Since I'm responsible for fastboot, I usually give between 1 and 2 weeks before I apply the changes to my tree: https://source.denx.de/u-boot/custodians/u-boot-dfu

When the change is applied, CI will test your patch. If something fails, I might reach out to you again to help troubleshooting.

If it passes, I will later on send a pull request to Tom with this patch included.
Once Tom accepts the pull request, this change will be merged into master.

More details on the process here:
https://docs.u-boot.org/en/latest/develop/process.html#work-flow-of-a-custodian

Hope that clarifies things a bit, and thank you for contributing! Mattijs

Quoted 46 lines
Quoted 42 lines · Mattijs Korpershoek

Hi Michael,

Thank you for the patch.

Quoted 8 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

Quoted 4 lines
Cc Marek Vasut <[email protected]>
Cc Heiko Schocher <[email protected]>
Cc Kyungmin Park <[email protected]>
Quoted 20 lines
---
 drivers/usb/gadget/g_dnl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/gadget/g_dnl.c +2 -2
index 631969b340..f2540eb6de 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);
--

2.48.1

#5 lore raw
In reply to Michael Ferolito

Hi,

Quoted 9 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

[...]

Thanks, Applied to https://source.denx.de/u-boot/custodians/u-boot-dfu (u-boot-dfu)

[1/1] usb: gadget: g_dnl: Fix NULLPTR dereference when serial# is unset

      https://source.denx.de/u-boot/custodians/u-boot-dfu/-/commit/dcf1c627cf436191919c5a3b153d1033245b54b7

--
Mattijs