Skip to contentSkip to conversation

usb: dwc3: host: Mark dwc->xhci as null if xhci probe fails

PATCH In review Prashanth K 3 participants 4 messages Oct 14

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: dwc3: host: Mark dwc->xhci as null if xhci probe fails

Currently dwc->xhci is not cleared when xhci_plat_probe fails, leading to null pointer dereference in dwc3_qcom_read_usb2_speed. Mark the xhci pointer as NULL to prevent this.

---
 drivers/usb/dwc3/host.c | 1 +
 1 file changed, 1 insertion(+)
drivers/usb/dwc3/host.c +1 -0
index c5674161b2b0..b01cf49454c2 100644
@@ -219,6 +219,7 @@ int dwc3_host_init(struct dwc3 *dwc)
 	return 0;
 err:
 	platform_device_put(xhci);
+	dwc->xhci = NULL;
 	return ret;
 }
 EXPORT_SYMBOL_GPL(dwc3_host_init);
Signature · 1 line
2.34.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] usb: dwc3: NULL pointer offset dereference in dwc3_host_exit() during role switch --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] usb: dwc3: NULL pointer offset dereference in dwc3_host_exit() during role switch --

commit 24753f0ba9a09a8a782231f3b06d19d819bc300c
Author: Prashanth K <[email protected]>

usb: dwc3: host: Mark dwc->xhci as null if xhci probe fails

Currently dwc->xhci is not cleared when xhci_plat_probe fails, leading to a use-after-free or null pointer dereference later. This commit sets the pointer to NULL in the error path of dwc3_host_init() to prevent this issue.

drivers/usb/dwc3/host.c int dwc3_host_init(struct dwc3 *dwc) · line 222

Quoted 9 lines
drivers/usb/dwc3/host.c +1 -0
index c5674161b2b08..b01cf49454c2e 100644
@@ -219,6 +219,7 @@ int dwc3_host_init(struct dwc3 *dwc)
 	return 0;
 err:
 	platform_device_put(xhci);
+	dwc->xhci = NULL;

[Severity: High]
Could this lead to an invalid pointer dereference during subsequent role switches?

In drivers/usb/dwc3/core.c:__dwc3_set_mode(), the role is updated to host mode before dwc3_host_init() is called, and is never reverted on failure:

__dwc3_set_mode() {
    ...
    dwc3_set_prtcap(dwc, mode);
    dwc->current_dr_role = mode;
    ...
    ret = dwc3_host_init(dwc);
    ...
}

If dwc3_host_init() fails, the role remains set to host mode, and dwc->xhci is now explicitly NULL. When the system later attempts to switch roles (e.g., back to device mode), __dwc3_set_mode() evaluates dwc->current_dr_role and invokes dwc3_host_exit(dwc).

Inside dwc3_host_exit(), if dwc->sys_wakeup is true, it unconditionally evaluates the address of dev inside xhci:

dwc3_host_exit() {
    ...
    if (dwc->sys_wakeup)
        device_init_wakeup(&dwc->xhci->dev, false);
    ...
}

Because dwc->xhci is NULL, &dwc->xhci->dev evaluates to a small non-zero offset. This bypasses the !dev check inside device_init_wakeup() and can cause a fault when attempting to read dev->power.can_wakeup.

Quoted 3 lines
drivers/usb/dwc3/host.c +0 -0
 	return ret;
 }
 EXPORT_SYMBOL_GPL(dwc3_host_init);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Prashanth K
Quoted 2 lines · Prashanth K

Currently dwc->xhci is not cleared when xhci_plat_probe fails, leading to null pointer dereference in dwc3_qcom_read_usb2_speed.

Why would that happen? What causes it and requires it?

Quoted 3 lines

Mark the xhci pointer as NULL to prevent this.

What commit id does this fix?
How was it tested?

thanks,

greg k-h

#3 lore raw
Quoted 8 lines · Greg Kroah-Hartman
Quoted 2 lines · Prashanth K

Currently dwc->xhci is not cleared when xhci_plat_probe fails, leading to null pointer dereference in dwc3_qcom_read_usb2_speed.

Why would that happen? What causes it and requires it?

Quoted 2 lines

Mark the xhci pointer as NULL to prevent this.

Quoted 8 lines
Quoted 1 line

What commit id does this fix?
How was it tested?

thanks,

greg k-h

This might not fix the issue, because it doesn't take the 'err' path, just realized that platform_device_add() doesn't return error value if the device probe fails.

The crash happens because xhci_halt failed (not sure what caused it), thats a functional failure from USB, but later the device crashed while trying to perform system suspend.

Unable to handle kernel NULL pointer dereference at virtual address

0000000000000058

Call trace:

dwc3_qcom_pm_suspend
dpm_run_callback
device_suspend
dpm_suspend
dpm_suspend_start
suspend_devices_and_enter
pm_suspend

Sorry for the spam,
Prashanth K