From: Michal Pecio <michal.pecio@gmail.com>
To: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
Cc: Mathias Nyman <mathias.nyman@intel.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org, Bin Yu <byu@xidian.edu.cn>,
Cheng Wen <wencheng@xidian.edu.cn>
Subject: Re: [PATCH v3] usb: xhci: Fix use-after-free race in xhci_stop_device()
Date: Wed, 4 Nov 2026 00:00:31 +0100 [thread overview]
Message-ID: <20261104000031.679944e7.michal.pecio@gmail.com> (raw)
In-Reply-To: <20261103160100.23190-1-25031212351@stu.xidian.edu.cn>
On Wed, 4 Nov 2026 00:01:00 +0800, YuanZhe Liu wrote:
> A KASAN slab-use-after-free read was observed in xhci_stop_device():
>
> BUG: KASAN: slab-use-after-free in xhci_stop_device.constprop.0.isra.0+0x395/0x430
> drivers/usb/host/xhci-hub.c:479
> Read of size 8 at addr ffff88804e85d2f0
>
> Freed by task 585700:
> kfree+0xf8/0x390 mm/slub.c:6566
> xhci_free_virt_device+0x3f/0x50 drivers/usb/host/xhci-mem.c:881
> xhci_free_dev+0x286/0x350 drivers/usb/host/xhci.c:4131
> usb_disconnect+0x6b6/0x910 drivers/usb/core/hub.c:2391
> ...
> usbdev_ioctl+0x6f05/0x86d0 drivers/usb/core/devio.c:2826
>
> Allocated by task 572283:
> xhci_alloc_virt_device+0x171/0xb70 drivers/usb/host/xhci-mem.c:986
> xhci_alloc_dev+0x475/0x840 drivers/usb/host/xhci.c:4271
> usb_alloc_dev+0xe8/0xdc0 drivers/usb/core/usb.c:661
> hub_event+0x1c82/0x4050 drivers/usb/core/hub.c:5953
>
> xhci_stop_device() reads xhci->devs[slot_id] without xhci->lock and
> keeps using the pointer across xhci_alloc_command(GFP_NOIO), which may
> sleep. All callers (the port suspend and bus suspend paths in
> xhci_hub_control() and xhci_bus_suspend()) drop xhci->lock before
> calling it, so a concurrent usb_disconnect() -> xhci_free_dev() can
> clear xhci->devs[slot_id] and free virt_dev under xhci->lock in that
> window. The endpoint loop then dereferences the freed virt_dev.
Hi,
Good job crashing a real driver and not just dummy-hcd as usual.
But is it not a bug in the first place that we are suspending a device
which is being deleted or deleting one which is being suspended?
Would you mind revealing your secret sauce and posting complete stack
traces of how you got there, including xhci_stop_device()? What kernel
version was that? Those line numbers don't match current code.
As you are .edu, perhaps you have a reproducer for this bug?
> commit 88716a93766b ("usb: xhci: Fix panic if disconnect") added a
> NULL check for this case, but that only helps when the device was
> already freed before the check; it does not close the window.
That's an old bug, not sure how such things can still be happening?
It seems that xhci_free_dev() doesn't return until everything is
cleared, and I would naively expect USB core not to suspend the bus
until all devices are suspended or finished disconnecting.
In testing, I see xhci_bus_suspend() 100 ms after disconnection, so
this race seems tricky to trigger. How have you done it?
Regards,
Michal
> Look up xhci->devs[slot_id] only after taking xhci->lock, and bail out
> if it is NULL or if the device is already being torn down. The
> tracepoint reloads virt_dev->udev, so it moves under the lock and only
> runs while the device is still attached.
>
> xhci_free_dev() clears virt_dev->udev without xhci->lock, and only
> later detaches the device from xhci->devs[] in xhci_free_virt_device(),
> also under the lock. Take xhci->lock when clearing ->udev so that
> xhci_stop_device() can rely on it while holding the same lock.
> xhci_free_virt_device() clears xhci->devs[slot_id] and frees the device
> under xhci->lock, so a device found there while holding the lock stays
> alive until xhci_stop_device() drops the lock, after which virt_dev is
> not used anymore.
>
> Fixes: be88fe4f4dda93e3 ("USB: xHCI: port power management implementation")
> Cc: stable@vger.kernel.org
> Co-developed-by: Bin Yu <byu@xidian.edu.cn>
> Signed-off-by: Bin Yu <byu@xidian.edu.cn>
> Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
> Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
> Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
next prev parent reply other threads:[~2026-11-03 23:00 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-26 12:36 [PATCH v2] usb: xhci: Fix use-after-free race in xhci_stop_device() YuanZhe Liu
2026-10-26 12:48 ` sashiko-bot
2026-10-30 6:01 ` YuanZhe Liu
2026-11-03 14:56 ` Mathias Nyman
2026-11-03 16:01 ` [PATCH v3] " YuanZhe Liu
2026-11-03 23:00 ` Michal Pecio [this message]
2026-11-04 1:32 ` [PATCH v4] " YuanZhe Liu
2026-11-04 1:48 ` sashiko-bot
2026-11-04 3:56 ` [PATCH v5] " YuanZhe Liu
2026-11-04 4:10 ` sashiko-bot
2026-11-04 15:02 ` Alan Stern
2026-11-04 16:59 ` Michal Pecio
2026-11-04 15:15 ` YuanZhe Liu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261104000031.679944e7.michal.pecio@gmail.com \
--to=michal.pecio@gmail.com \
--cc=25031212351@stu.xidian.edu.cn \
--cc=byu@xidian.edu.cn \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=mathias.nyman@intel.com \
--cc=stable@vger.kernel.org \
--cc=wencheng@xidian.edu.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox