All of lore.kernel.org
 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 2/2] xhci: sideband: check virt device under xhci lock when stopping endpoints
Date: Wed, 18 Nov 2026 13:16:25 +0000	[thread overview]
Message-ID: <sashiko-outbox-220991@kernel.org> (raw)
In-Reply-To: <20261118125109.1429538-3-mathias.nyman@linux.intel.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] xhci: sideband: xhci->lock does not serialize against vdev
  free in xhci_resume()
- [Medium] xhci: sideband: dangling ep->sideband on command alloc
  failure in remove path
--

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

xhci: sideband: check virt device under xhci lock when stopping endpoints

This commit moves xhci_stop_endpoint_sync() into xhci-sideband.c as
sideband_stop_endpoint_sync() so that virt device and endpoint checks,
endpoint removal, and stop endpoint command queuing happen under xhci->lock
and sb->mutex.

> diff --git a/drivers/usb/host/xhci-sideband.c b/drivers/usb/host/xhci-sideband.c
> index ad4849903df3b..39d790bb6a2b2 100644
> --- a/drivers/usb/host/xhci-sideband.c
> +++ b/drivers/usb/host/xhci-sideband.c
> @@ -92,19 +92,72 @@ sideband_to_vdev(struct xhci_sideband *sb)
>  }
>  
>  /* Caller must hold sb->mutex */
> -static void
> -__xhci_sideband_remove_endpoint(struct xhci_sideband *sb, struct xhci_virt_ep *ep)
> +static int
> +sideband_stop_endpoint_sync(struct xhci_sideband *sb, u32 ep_index, bool remove)
>  {
> +	struct xhci_virt_device *vdev;
> +	struct xhci_command *cmd;
> +	struct xhci_virt_ep *ep;
> +	struct xhci_ep_ctx *ctx;
> +	unsigned long flags;
> +	bool queued = false;
> +	int ret = -ENODEV;
> +
>  	lockdep_assert_held(&sb->mutex);
>  
> -	/*
> -	 * Issue a stop endpoint command when an endpoint is removed.
> -	 * The stop ep cmd handler will handle the ring cleanup.
> -	 */
> -	xhci_stop_endpoint_sync(sb->xhci, ep, 0, GFP_KERNEL);
> +	cmd = xhci_alloc_command(sb->xhci, true, GFP_KERNEL);
> +	if (!cmd)
> +		return -ENOMEM;

[Severity: Medium]
Does returning -ENOMEM here before clearing sb->eps[ep_index] and
ep->sideband leave a dangling ep->sideband pointer when remove is true?

Previously, __xhci_sideband_remove_endpoint() cleared ep->sideband and
sb->eps[ep->ep_index] even if xhci_alloc_command() failed inside
xhci_stop_endpoint_sync().  With this regression, if xhci_alloc_command()
fails during xhci_sideband_unregister(), the caller ignores the error and
frees sb via kfree(sb) while vdev->eps[i].sideband still points to it.  A
later device reset in xhci_discover_or_reset_device() will then pass that
dangling ep->sideband pointer to xhci_sideband_notify_ep_ring_free(),
causing a use-after-free.

> +
> +	spin_lock_irqsave(&sb->xhci->lock, flags);
> +
> +	vdev = sideband_to_vdev(sb);
> +	ep = sb->eps[ep_index];
> +
> +	if (remove)
> +		sb->eps[ep_index] = NULL;
> +
> +	/* validate xhci didn't free or replace vdev during resume */
> +	if (!vdev || !ep || ep->sideband != sb) {
> +		spin_unlock_irqrestore(&sb->xhci->lock, flags);
> +		goto out;
> +	}
> +
> +	if (remove) {
> +		ep->sideband = NULL;
> +		/* endpoint is removed, not running is not an error */
> +		ret = 0;
> +	}
> +
> +	/* make sure endpoint is running before stopping it */
> +	if (ep->ring) {
> +		ctx = xhci_get_ep_ctx(sb->xhci, vdev->out_ctx, ep_index);
> +		if (GET_EP_CTX_STATE(ctx) == EP_STATE_RUNNING) {

[Severity: Medium]
Does holding sb->xhci->lock actually prevent vdev and its contexts from
being freed concurrently during xhci_resume()?

When xhci_resume() resets the host controller, it drops xhci->lock before
freeing all virt devices via xhci_free_virt_devices_depth_first() ->
xhci_free_virt_device().  Inside xhci_free_virt_device(), dev->eps[i].ring
(which is not set to NULL) and dev->out_ctx are freed before
dev->udev->slot_id and xhci->devs[slot_id] are cleared:

drivers/usb/host/xhci-mem.c:xhci_free_virt_device() {
	...
	for (i = 0; i < 31; i++) {
		if (dev->eps[i].ring)
			xhci_ring_free(xhci, dev->eps[i].ring);
	...
	if (dev->out_ctx)
		xhci_free_container_ctx(xhci, dev->out_ctx);

	if (dev->udev && dev->udev->slot_id)
		dev->udev->slot_id = 0;
	if (dev->rhub_port && dev->rhub_port->slot_id == slot_id)
		dev->rhub_port->slot_id = 0;
	if (xhci->devs[slot_id] == dev)
		xhci->devs[slot_id] = NULL;
	kfree(dev);
}

This allows the following race:

CPU0                              CPU1
xhci_resume()
  spin_unlock_irq(&xhci->lock)
  ...
  xhci_free_virt_device()
    xhci_ring_free(..., ring)
    xhci_free_container_ctx(...,
                            out_ctx)
                                  sideband_stop_endpoint_sync()
                                    spin_lock_irqsave(&sb->xhci->lock)
                                    vdev = sideband_to_vdev(sb)
                                    if (ep->ring)
                                      ctx = xhci_get_ep_ctx(...,
                                                            vdev->out_ctx,
                                                            ...)
                                      GET_EP_CTX_STATE(ctx)
                                        <-- use-after-free
    dev->udev->slot_id = 0
    xhci->devs[slot_id] = NULL
    kfree(dev)

The same race also applies to xhci_sideband_unregister() clearing
vdev->sideband under xhci->lock while xhci_free_virt_device() runs
kfree(dev) without holding xhci->lock.

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

      reply	other threads:[~2026-11-18 13:16 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
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 [this message]

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-220991@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.