usb: sl811-hcd: delete the driver timer on stop to avoid use-after-free
Subsystems (2)
PATCH 1/1 usb: sl811-hcd: delete the driver timer on stop to avoid use-after-free
From: Hyunjung Ko <[email protected]>
sl811h_hub_control() and the transfer scheduler arm sl811->timer (sl811h_timer) with mod_timer(); the timer callback dereferences the hcd-private struct sl811 (sl811->ctrl1, sl811->lock, sl811->port1, and register I/O via sl811).
sl811h_stop() deletes only the USB core's root-hub timer (hcd->rh_timer); it never deletes sl811->timer, and nothing else does either. struct sl811 lives in the HCD private area and is freed by usb_put_hcd() after sl811h_remove() calls usb_remove_hcd(). If sl811->timer is still pending when the controller is stopped and the HCD is freed, sl811h_timer() runs against the freed struct sl811 (use-after-free).
Delete sl811->timer in sl811h_stop(), after the port is powered down so it cannot be re-armed, mirroring the existing hcd->rh_timer teardown. sl811h_timer() does not re-arm itself, so a single timer_delete_sync() drains it.
Found by static analysis and code review; not tested on real hardware (needs an SL811HS USB host controller).
--- drivers/usb/host/sl811-hcd.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/usb/host/sl811-hcd.c +2 -0
@@ -1520,6 +1520,8 @@ sl811h_stop(struct usb_hcd *hcd)spin_lock_irqsave (&sl811->lock, flags);port_power (sl811,0 );spin_unlock_irqrestore (&sl811->lock, flags); + +timer_delete_sync (&sl811->timer); } static int
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] usb: sl811-hcd: sl811h_stop() deletes timer after port_power() halt --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] usb: sl811-hcd: sl811h_stop() deletes timer after port_power() halt
--
commit 2938f64d61ba1aa6f4f9800e4e15b39f1ccc6b42
Author: Hyunjung Ko <[email protected]>
usb: sl811-hcd: delete the driver timer on stop to avoid use-after-free
This commit calls timer_delete_sync(&sl811->timer) in sl811h_stop() so that a pending port timer does not fire after the HCD and struct sl811 are freed.
drivers/usb/host/sl811-hcd.c sl811h_stop(struct usb_hcd *hcd) · line 1527
Quoted 13 lines
drivers/usb/host/sl811-hcd.c +2
-0@@ -1520,6 +1520,8 @@ sl811h_stop(struct usb_hcd *hcd)spin_lock_irqsave (&sl811->lock, flags);port_power (sl811,0 );spin_unlock_irqrestore (&sl811->lock, flags); + +timer_delete_sync (&sl811->timer); } static int
[Severity: Medium]
Can sl811h_timer() still run in the window between spin_unlock_irqrestore() and timer_delete_sync(&sl811->timer), re-enabling the controller after port_power(sl811, 0) has halted it?
CPU0 CPU1 (<softirq>)sl811h_stop()
spin_lock_irqsave(&sl811->lock, ...)
port_power(sl811, 0)
hcd->state = HC_STATE_HALT;
sl811_write(..., SL11H_IRQ_ENABLE, 0);
sl811h_timer()
spin_lock_irqsave(&sl811->lock, ...)
spin_unlock_irqrestore(&sl811->lock, ...)
sl811->ctrl1 |= SL11H_CTL1MASK_SOF_ENA;
sl811_write(..., SL11H_HOSTCTLREG,
SL11H_HCTLMASK_ARM);
sl811_write(..., SL11H_IRQ_ENABLE,
sl811->irq_enable);
<-- re-arms halted controller timer_delete_sync(&sl811->timer)
sl811h_timer() does not check whether the port is powered or whether hcd->state is HC_STATE_HALT. If a port reset or resume timer expires while port_power(sl811, 0) is running (which holds sl811->lock across a 20 ms mdelay() when board->reset is NULL), sl811h_timer() runs as soon as sl811->lock is released and re-enables SOF generation, Endpoint A, and hardware interrupts in SL11H_IRQ_ENABLE on the halted controller.
Only sl811h_hub_control() arms sl811->timer (the transfer scheduler never touches it), and usb_remove_hcd() has already stopped root-hub activity before calling sl811h_stop(). Deleting sl811->timer alongside hcd->rh_timer before port_power(sl811, 0) would drain any pending callback before halting the hardware; the commit message should also be updated not to mention the transfer scheduler.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1