Skip to contentSkip to conversation

xhci: Cancel cmd_timer before cleanup the command queue in xhci_hc_died()

PATCH In review Jeffy Chen 3 participants 4 messages Sep 29, 2017 – Nov 1, 2017

Subsystems (2)
lore
Share & tools
lore

Jeffy Chen

author
#0 lore raw

PATCH 1/1 xhci: Cancel cmd_timer before cleanup the command queue in xhci_hc_died()

When the cmd_timer fired, it would try to access the command struct. So cancel it before cleanup the command queue in xhci_hc_died(), to avoid use-after-free reported by KASAN:

[  176.952537] BUG: KASAN: use-after-free in xhci_handle_command_timeout+0x68/0x224
[  176.960846] Write of size 4 at addr ffffffc0cbb01608 by task kworker/3:3/1680
...
[  177.180644] Freed by task 0:
[  177.183882]  kasan_slab_free+0x90/0x15c
[  177.188194]  kfree+0x114/0x28c
[  177.191630]  xhci_cleanup_command_queue+0xc8/0xf8
[  177.196916]  xhci_hc_died+0x84/0x358
Fixes d9f11ba9f107 ("xhci: Rework how we handle unresponsive or hoptlug removed hosts")

---

 drivers/usb/host/xhci-ring.c | 1 +
 1 file changed, 1 insertion(+)
drivers/usb/host/xhci-ring.c +1 -0
index a9443651ce0f..69ac3deffb7b 100644
@@ -914,6 +914,7 @@ void xhci_hc_died(struct xhci_hcd *xhci)
 	xhci_err(xhci, "xHCI host controller not responding, assume dead\n");
 	xhci->xhc_state |= XHCI_STATE_DYING;
 
+	cancel_delayed_work_sync(&xhci->cmd_timer);
 	xhci_cleanup_command_queue(xhci);
 
 	/* return any pending urbs, remove may be waiting for them */
Signature · 1 line
2.11.0
#1 lore raw
Quoted 12 lines · Jeffy Chen

When the cmd_timer fired, it would try to access the command struct. So cancel it before cleanup the command queue in xhci_hc_died(), to avoid use-after-free reported by KASAN:

[  176.952537] BUG: KASAN: use-after-free in xhci_handle_command_timeout+0x68/0x224
[  176.960846] Write of size 4 at addr ffffffc0cbb01608 by task kworker/3:3/1680
...
[  177.180644] Freed by task 0:
[  177.183882]  kasan_slab_free+0x90/0x15c
[  177.188194]  kfree+0x114/0x28c
[  177.191630]  xhci_cleanup_command_queue+0xc8/0xf8
[  177.196916]  xhci_hc_died+0x84/0x358
Quoted 3 lines
Fixes d9f11ba9f107 ("xhci: Rework how we handle unresponsive or hoptlug removed hosts")

---

Mathias, did you miss this one? Want me to queue it up?

thanks,

greg k-h

maintainer
#2 lore raw
Quoted 13 lines · Greg Kroah-Hartman
Quoted 12 lines · Jeffy Chen

When the cmd_timer fired, it would try to access the command struct. So cancel it before cleanup the command queue in xhci_hc_died(), to avoid use-after-free reported by KASAN:

[  176.952537] BUG: KASAN: use-after-free in xhci_handle_command_timeout+0x68/0x224
[  176.960846] Write of size 4 at addr ffffffc0cbb01608 by task kworker/3:3/1680
...
[  177.180644] Freed by task 0:
[  177.183882]  kasan_slab_free+0x90/0x15c
[  177.188194]  kfree+0x114/0x28c
[  177.191630]  xhci_cleanup_command_queue+0xc8/0xf8
[  177.196916]  xhci_hc_died+0x84/0x358
Quoted 6 lines
Quoted 3 lines
Fixes d9f11ba9f107 ("xhci: Rework how we handle unresponsive or hoptlug removed hosts")

---

Mathias, did you miss this one? Want me to queue it up?

There is a v2 of that patch already upstreamed.

d1aad52 xhci: Cleanup current_cmd in xhci_cleanup_command_queue()

https://marc.info/?l=linux-usb&m=150668331802206&w=2

A bit confusing as patch subject changed as well in v2

-Mathias

#3 lore raw
Quoted 21 lines · Mathias Nyman
Quoted 13 lines · Greg Kroah-Hartman
Quoted 12 lines · Jeffy Chen

When the cmd_timer fired, it would try to access the command struct. So cancel it before cleanup the command queue in xhci_hc_died(), to avoid use-after-free reported by KASAN:

[  176.952537] BUG: KASAN: use-after-free in xhci_handle_command_timeout+0x68/0x224
[  176.960846] Write of size 4 at addr ffffffc0cbb01608 by task kworker/3:3/1680
...
[  177.180644] Freed by task 0:
[  177.183882]  kasan_slab_free+0x90/0x15c
[  177.188194]  kfree+0x114/0x28c
[  177.191630]  xhci_cleanup_command_queue+0xc8/0xf8
[  177.196916]  xhci_hc_died+0x84/0x358
Quoted 6 lines
Quoted 3 lines
Fixes d9f11ba9f107 ("xhci: Rework how we handle unresponsive or hoptlug removed hosts")

---

Mathias, did you miss this one? Want me to queue it up?

Quoted 7 lines

There is a v2 of that patch already upstreamed.

d1aad52 xhci: Cleanup current_cmd in xhci_cleanup_command_queue()

https://marc.info/?l=linux-usb&m=150668331802206&w=2

A bit confusing as patch subject changed as well in v2

Ah, thanks, missed that.

greg k-h