linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] xhci: Cleanup current_cmd in xhci_cleanup_command_queue()
@ 2017-09-29 11:07 Jeffy Chen
  2017-10-02 11:48 ` Mathias Nyman
  0 siblings, 1 reply; 2+ messages in thread
From: Jeffy Chen @ 2017-09-29 11:07 UTC (permalink / raw)
  To: linux-kernel; +Cc: Jeffy Chen, Greg Kroah-Hartman, linux-usb, Mathias Nyman

KASAN reported use-after-free bug when xhci host controller died:
[  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

Problem here is that when the cmd_timer fired, it would try to access
current_cmd while the command queue is already freed by xhci_hc_died().

Cleanup current_cmd in xhci_cleanup_command_queue() to avoid that.

Fixes: d9f11ba9f107 ("xhci: Rework how we handle unresponsive or hoptlug removed hosts")
Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
---

Changes in v2:
We cannot cancel cmd_timer in xhci_hc_died(), which would cause
might_sleep warning.

 drivers/usb/host/xhci-ring.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index a9443651ce0f..48ae15afa59e 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -1309,6 +1309,7 @@ static void xhci_complete_del_and_free_cmd(struct xhci_command *cmd, u32 status)
 void xhci_cleanup_command_queue(struct xhci_hcd *xhci)
 {
 	struct xhci_command *cur_cmd, *tmp_cmd;
+	xhci->current_cmd = NULL;
 	list_for_each_entry_safe(cur_cmd, tmp_cmd, &xhci->cmd_list, cmd_list)
 		xhci_complete_del_and_free_cmd(cur_cmd, COMP_COMMAND_ABORTED);
 }
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] xhci: Cleanup current_cmd in xhci_cleanup_command_queue()
  2017-09-29 11:07 [PATCH v2] xhci: Cleanup current_cmd in xhci_cleanup_command_queue() Jeffy Chen
@ 2017-10-02 11:48 ` Mathias Nyman
  0 siblings, 0 replies; 2+ messages in thread
From: Mathias Nyman @ 2017-10-02 11:48 UTC (permalink / raw)
  To: Jeffy Chen, linux-kernel; +Cc: Greg Kroah-Hartman, linux-usb, Mathias Nyman

On 29.09.2017 14:07, Jeffy Chen wrote:
> KASAN reported use-after-free bug when xhci host controller died:
> [  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
>
> Problem here is that when the cmd_timer fired, it would try to access
> current_cmd while the command queue is already freed by xhci_hc_died().
>
> Cleanup current_cmd in xhci_cleanup_command_queue() to avoid that.
>
> Fixes: d9f11ba9f107 ("xhci: Rework how we handle unresponsive or hoptlug removed hosts")
> Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
> ---
>
>

Thanks, adding patch to queue

-Mathias

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-10-02 11:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-29 11:07 [PATCH v2] xhci: Cleanup current_cmd in xhci_cleanup_command_queue() Jeffy Chen
2017-10-02 11:48 ` Mathias Nyman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).