[PATCH] usb: acpi: Prevent null pointer dereference in usb_acpi_add_usb4_devlink()

Chenyuan Yang posted 1 patch 9 months, 3 weeks ago
drivers/usb/core/usb-acpi.c | 2 ++
1 file changed, 2 insertions(+)
[PATCH] usb: acpi: Prevent null pointer dereference in usb_acpi_add_usb4_devlink()
Posted by Chenyuan Yang 9 months, 3 weeks ago
As demonstrated by the fix for update_port_device_state,
commit 12783c0b9e2c ("usb: core: Prevent null pointer dereference in update_port_device_state"), 
usb_hub_to_struct_hub() can return NULL in certain scenarios, 
such as during hub driver unbind or teardown race conditions, 
even if the underlying usb_device structure exists.

Plus, all other places that call usb_hub_to_struct_hub() in the same file
do check for NULL return values.

If usb_hub_to_struct_hub() returns NULL, the subsequent access to
hub->ports[udev->portnum - 1] will cause a null pointer dereference.

Signed-off-by: Chenyuan Yang <chenyuan0y@gmail.com>
Fixes: f1bfb4a6fed6 ("usb: acpi: add device link between tunneled USB3 device and USB4 Host Interface")

---
 drivers/usb/core/usb-acpi.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/usb/core/usb-acpi.c b/drivers/usb/core/usb-acpi.c
index 935c0efea0b6..ea1ce8beb0cb 100644
--- a/drivers/usb/core/usb-acpi.c
+++ b/drivers/usb/core/usb-acpi.c
@@ -165,6 +165,8 @@ static int usb_acpi_add_usb4_devlink(struct usb_device *udev)
 		return 0;
 
 	hub = usb_hub_to_struct_hub(udev->parent);
+	if (!hub)
+		return 0;
 	port_dev = hub->ports[udev->portnum - 1];
 
 	struct fwnode_handle *nhi_fwnode __free(fwnode_handle) =
-- 
2.34.1
Re: [PATCH] usb: acpi: Prevent null pointer dereference in usb_acpi_add_usb4_devlink()
Posted by Alan Stern 9 months, 3 weeks ago
On Thu, Apr 17, 2025 at 02:50:32PM -0500, Chenyuan Yang wrote:
> As demonstrated by the fix for update_port_device_state,
> commit 12783c0b9e2c ("usb: core: Prevent null pointer dereference in update_port_device_state"), 
> usb_hub_to_struct_hub() can return NULL in certain scenarios, 
> such as during hub driver unbind or teardown race conditions, 
> even if the underlying usb_device structure exists.

Those are not the conditions addressed by that commit.  The commit was 
specifically meant to handle a bizarre situation created by the lvstest 
driver (a child device added "by hand" after deconfiguring the parent 
hub).

> Plus, all other places that call usb_hub_to_struct_hub() in the same file
> do check for NULL return values.
> 
> If usb_hub_to_struct_hub() returns NULL, the subsequent access to
> hub->ports[udev->portnum - 1] will cause a null pointer dereference.
> 
> Signed-off-by: Chenyuan Yang <chenyuan0y@gmail.com>
> Fixes: f1bfb4a6fed6 ("usb: acpi: add device link between tunneled USB3 device and USB4 Host Interface")
> 
> ---
>  drivers/usb/core/usb-acpi.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/usb/core/usb-acpi.c b/drivers/usb/core/usb-acpi.c
> index 935c0efea0b6..ea1ce8beb0cb 100644
> --- a/drivers/usb/core/usb-acpi.c
> +++ b/drivers/usb/core/usb-acpi.c
> @@ -165,6 +165,8 @@ static int usb_acpi_add_usb4_devlink(struct usb_device *udev)
>  		return 0;
>  
>  	hub = usb_hub_to_struct_hub(udev->parent);
> +	if (!hub)
> +		return 0;
>  	port_dev = hub->ports[udev->portnum - 1];

While this test may not be strictly necessary, it doesn't hurt since 
this isn't a hot path.

Acked-by: Alan Stern <stern@rowland.harvard.edu>

Alan Stern