Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Mathias Nyman" <mathias.nyman@linux.intel.com>
Cc: linux-usb@vger.kernel.org
Subject: Re: [RFT PATCH 1/2] xhci: sideband: avoid use-after-free of stale virt device pointer
Date: Wed, 18 Nov 2026 13:13:41 +0000	[thread overview]
Message-ID: <sashiko-outbox-220988@kernel.org> (raw)
In-Reply-To: <20261118125109.1429538-2-mathias.nyman@linux.intel.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] xhci: sideband: unregister race in
  xhci_sideband_create_interrupter()
- [Low] xhci: sideband: missing Fixes trailer for sb->vdev
  use-after-free fix
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [Critical] xhci: use-after-free of sideband in
  xhci_discover_or_reset_device() [1]
--

commit 9c6f07fbe23d89be989882c6d8b9eb96113b85e0
Author: Mathias Nyman <mathias.nyman@linux.intel.com>

> xhci: sideband: avoid use-after-free of stale virt device pointer
> 
> The sideband structure caches vdev, an xhci_virt_device pointer that
> the xhci driver may free and reallocate while the sideband is still
> registered, leaving the sideband with a dangling sb->vdev pointer.
> 
> This can happen during resume, either when the xhci driver detects xHC
> power loss and resets the xHC, freeing all virt devices without
> informing class drivers, or during reset-resume when usb core
> re-enumerates the device and xhci frees and allocates a new virt device.
> 
> Store the usb_device pointer (udev) instead. It stays valid across
> reset-resume and xHC reset.
> Look up the current virt device and ensure it still points to this
> sideband before using and dereferencing it in sideband API calls.
> 
> Make sure xhci_sideband_unregister() works properly with an invalid
> vdev. Don't dereference it, or ask the xHC to stop non-existing
> endpoints, but make sure the sideband is properly freed.
> Fail other relevant sideband API calls that would access invalid
> vdev with -ENODEV.
> 
> This solution isn't airtight. It's a huge improvement over the current
> situation, but there is still a window between verifying vdev and
> actually dereferencing it in many sideband API calls.
> 
> Signed-off-by: Mathias Nyman <mathias.nyman@linux.intel.com>

[Severity: Low]
This fixes a use-after-free of sb->vdev.  Should this include:

  Fixes: de66754e9f80 ("xhci: sideband: add initial api to register a secondary interrupter entity")

> diff --git a/drivers/usb/host/xhci-sideband.c b/drivers/usb/host/xhci-sideband.c
> index a5deeee4d5dce..ad4849903df3b 100644
> --- a/drivers/usb/host/xhci-sideband.c
> +++ b/drivers/usb/host/xhci-sideband.c
[ ... ]
> @@ -329,9 +348,6 @@ xhci_sideband_create_interrupter(struct xhci_sideband *sb, int num_seg,
>  
>  	guard(mutex)(&sb->mutex);
>  
> -	if (!sb->vdev)
> -		return -ENODEV;
> -
>  	if (sb->ir)
>  		return -EBUSY;
>  

[Severity: High]
Does removing the !sb->vdev check here without replacing it with
!sideband_to_vdev(sb) reintroduce the race fixed in 8c13a7323b84 ("xhci:
sideband: Fix race condition in sideband unregister")?

CPU0                              CPU1
xhci_sideband_unregister()
  mutex_lock(&sb->mutex)
                                  xhci_sideband_create_interrupter()
                                    if (!sb || !sb->xhci)
                                    mutex_lock(&sb->mutex)
  __xhci_sideband_remove_interrupter()
  sb->udev = NULL
  mutex_unlock(&sb->mutex)
  sb->xhci = NULL
  kfree(sb)
                                    xhci_create_secondary_interrupter(
                                      xhci_to_hcd(sb->xhci), ...)
                                      <-- NULL pointer dereference / UAF

Without checking sideband_to_vdev(sb) after acquiring sb->mutex, CPU1
proceeds after xhci_sideband_unregister() releases the mutex, dereferencing
a NULL sb->xhci or freed sb.  Even without a concurrent unregister,
xhci_sideband_create_interrupter() will allocate a secondary interrupter
when vdev has been invalidated instead of returning -ENODEV.

[ ... ]

> @@ -471,22 +487,21 @@ xhci_sideband_unregister(struct xhci_sideband *sb)
>  	xhci = sb->xhci;
>  
>  	scoped_guard(mutex, &sb->mutex) {
> -		vdev = sb->vdev;
> -		if (!vdev)
> -			return;
> +		vdev = sideband_to_vdev(sb);
>  
>  		for (i = 0; i < EP_CTX_PER_DEV; i++)
> -			if (sb->eps[i])
> +			if (vdev && sb->eps[i])
>  				__xhci_sideband_remove_endpoint(sb, sb->eps[i]);

[Severity: Critical]
This problem wasn't introduced by this patch, but nothing serializes
[1] xhci_sideband_unregister() against xhci_discover_or_reset_device()
reading ep->sideband and calling xhci_sideband_notify_ep_ring_free().

CPU0 (device reset)               CPU1 (sideband unregister)
xhci_discover_or_reset_device()
  if (ep->sideband)
    xhci_sideband_notify_ep_ring_free(
      ep->sideband, i)
                                  xhci_sideband_unregister()
                                    __xhci_sideband_remove_endpoint()
                                      ep->sideband = NULL
                                    kfree(sb)
      sb->notify_client(sb->intf, &evt)
        <-- use-after-free

xhci_discover_or_reset_device() holds only the USB device lock when it
reads ep->sideband and invokes xhci_sideband_notify_ep_ring_free() without
holding xhci->lock or sb->mutex:

drivers/usb/host/xhci.c:xhci_discover_or_reset_device() {
    ...
		if (ep->ring) {
			if (ep->sideband)
				xhci_sideband_notify_ep_ring_free(ep->sideband, i);
			xhci_debugfs_remove_endpoint(xhci, virt_dev, i);
			xhci_free_endpoint_ring(xhci, virt_dev, i);
		}
    ...
}

Meanwhile xhci_sideband_unregister() runs without the USB device lock,
clears ep->sideband under sb->mutex, and frees sb.  If CPU0 loads
ep->sideband before CPU1 clears it, CPU0 dereferences the freed sb.

[1] https://sashiko.dev/#/bug/linux-6cdffda2-7589-4947-aad7-6f07714b5916

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261118125109.1429538-1-mathias.nyman@linux.intel.com?part=1

  reply	other threads:[~2026-11-18 13:13 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 12:51 [RFT PATCH 0/2] xhci: sideband: fix use-after-free of stale virt device pointer Mathias Nyman
2026-11-18 12:51 ` [RFT PATCH 1/2] xhci: sideband: avoid " Mathias Nyman
2026-11-18 13:13   ` sashiko-bot [this message]
2026-11-18 12:51 ` [RFT PATCH 2/2] xhci: sideband: check virt device under xhci lock when stopping endpoints Mathias Nyman
2026-11-18 13:16   ` sashiko-bot

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=sashiko-outbox-220988@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=mathias.nyman@linux.intel.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /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