Skip to contentSkip to conversation

xhci: sideband: fix use-after-free of stale virt device pointer

PATCH Superseded Mathias Nyman 2 participants 5 messages Nov 18

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover xhci: sideband: fix use-after-free of stale virt device pointer

Hi

These two patches attempt to handle the disappearing vdev issue while sideband stays active.

I don't have real harware to test this on so all review comments and feedback from testing is welcome.

There are still some gaps in how xhci driver uses locks when disabling slots and freeing vdev that is not part of this series

Thanks
Mathias

Mathias Nyman (2):
xhci: sideband: avoid use-after-free of stale virt device pointer
xhci: sideband: check virt device under xhci lock when stopping
endpoints

 drivers/usb/host/xhci-sideband.c  | 141 +++++++++++++++++++++---------
 drivers/usb/host/xhci.c           |  55 ------------
 drivers/usb/host/xhci.h           |   2 -
 include/linux/usb/xhci-sideband.h |   4 +-
 4 files changed, 101 insertions(+), 101 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 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.

---
 drivers/usb/host/xhci-sideband.c  | 47 ++++++++++++++++++++-----------
 include/linux/usb/xhci-sideband.h |  4 +--
 2 files changed, 33 insertions(+), 18 deletions(-)
drivers/usb/host/xhci-sideband.c +31 -16
index a5deeee4d5dc..ad4849903df3 100644
@@ -75,6 +75,22 @@ xhci_ring_to_sgtable(struct xhci_sideband *sb, struct xhci_ring *ring)
 	return NULL;
 }
 
+/* Return the current vdev of sb->udev, or NULL if it was freed or reallocated */
+static struct xhci_virt_device *
+sideband_to_vdev(struct xhci_sideband *sb)
+{
+	struct xhci_virt_device *vdev;
+
+	if (!sb->udev || !sb->udev->slot_id)
+		return NULL;
+
+	vdev = sb->xhci->devs[sb->udev->slot_id];
+	if (vdev && vdev->sideband == sb)
+		return vdev;
+
+	return NULL;
+}
+
 /* Caller must hold sb->mutex */
 static void
 __xhci_sideband_remove_endpoint(struct xhci_sideband *sb, struct xhci_virt_ep *ep)
@@ -146,16 +162,18 @@ int
 xhci_sideband_add_endpoint(struct xhci_sideband *sb,
 			   struct usb_host_endpoint *host_ep)
 {
+	struct xhci_virt_device *vdev;
 	struct xhci_virt_ep *ep;
 	unsigned int ep_index;
 
 	guard(mutex)(&sb->mutex);
 
-	if (!sb->vdev)
+	vdev = sideband_to_vdev(sb);
+	if (!vdev)
 		return -ENODEV;
 
 	ep_index = xhci_get_endpoint_index(&host_ep->desc);
-	ep = &sb->vdev->eps[ep_index];
+	ep = &vdev->eps[ep_index];
 
 	if (ep->ep_state & EP_HAS_STREAMS)
 		return -EINVAL;
@@ -202,9 +220,10 @@ xhci_sideband_remove_endpoint(struct xhci_sideband *sb,
 	ep_index = xhci_get_endpoint_index(&host_ep->desc);
 	ep = sb->eps[ep_index];
 
-	if (!ep || !ep->sideband || ep->sideband != sb)
+	if (!sideband_to_vdev(sb) || !ep || ep->sideband != sb) {
+		sb->eps[ep_index] = NULL;
 		return -ENODEV;
-
+	}
 	__xhci_sideband_remove_endpoint(sb, ep);
 
 	return 0;
@@ -221,7 +240,7 @@ xhci_sideband_stop_endpoint(struct xhci_sideband *sb,
 	ep_index = xhci_get_endpoint_index(&host_ep->desc);
 	ep = sb->eps[ep_index];
 
-	if (!ep || !ep->sideband || ep->sideband != sb)
+	if (!sideband_to_vdev(sb) || !ep || ep->sideband != sb)
 		return -EINVAL;
 
 	return xhci_stop_endpoint_sync(sb->xhci, ep, 0, GFP_KERNEL);
@@ -251,7 +270,7 @@ xhci_sideband_get_endpoint_buffer(struct xhci_sideband *sb,
 	ep_index = xhci_get_endpoint_index(&host_ep->desc);
 	ep = sb->eps[ep_index];
 
-	if (!ep || !ep->ring || !ep->sideband || ep->sideband != sb)
+	if (!sideband_to_vdev(sb) || !ep || !ep->ring || ep->sideband != sb)
 		return NULL;
 
 	return xhci_ring_to_sgtable(sb, ep->ring);
@@ -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;
 
@@ -436,7 +452,7 @@ xhci_sideband_register(struct usb_interface *intf, enum xhci_sideband_type type,
 	}
 
 	sb->xhci = xhci;
-	sb->vdev = vdev;
+	sb->udev = udev;
 	sb->intf = intf;
 	sb->type = type;
 	sb->notify_client = notify_client;
@@ -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]);
 
 		__xhci_sideband_remove_interrupter(sb);
 
-		sb->vdev = NULL;
+		sb->udev = NULL;
 	}
 
 	spin_lock_irq(&xhci->lock);
 	sb->xhci = NULL;
-	vdev->sideband = NULL;
+	if (vdev)
+		vdev->sideband = NULL;
 	spin_unlock_irq(&xhci->lock);
 
 	kfree(sb);
include/linux/usb/xhci-sideband.h +2 -2
index 005257085dcb..2d153cb57831 100644
@@ -39,7 +39,7 @@ struct xhci_sideband_event {
 /**
  * struct xhci_sideband - representation of a sideband accessed usb device.
  * @xhci: The xhci host controller the usb device is connected to
- * @vdev: the usb device accessed via sideband
+ * @udev: the usb device accessed via sideband
  * @eps: array of endpoints controlled via sideband
  * @ir: event handling and buffer for sideband accessed device
  * @type: xHCI sideband type
@@ -51,7 +51,7 @@ struct xhci_sideband_event {
  */
 struct xhci_sideband {
 	struct xhci_hcd                 *xhci;
-	struct xhci_virt_device         *vdev;
+	struct usb_device		*udev;
 	struct xhci_virt_ep             *eps[EP_CTX_PER_DEV];
 	struct xhci_interrupter         *ir;
 	enum xhci_sideband_type		type;
Signature · 1 line
2.43.0
#2 lore raw

Re: [RFT PATCH 1/2] xhci: sideband: avoid use-after-free of stale virt device pointer

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…

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 <[email protected]>

Quoted 22 lines

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.

Quoted 5 lines

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.

[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")
Quoted 4 lines
drivers/usb/host/xhci-sideband.c +0 -0
index a5deeee4d5dce..ad4849903df3b 100644

[ ... ]

drivers/usb/host/xhci-sideband.c xhci_sideband_create_interrupter(struct xhci_sideband *sb, int num_seg, · line 353

Quoted 10 lines
drivers/usb/host/xhci-sideband.c +0 -3
@@ -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.

[ ... ]

drivers/usb/host/xhci-sideband.c xhci_sideband_unregister(struct xhci_sideband *sb) · line 494

Quoted 13 lines
drivers/usb/host/xhci-sideband.c +2 -4
@@ -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

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH 2/2 xhci: sideband: check virt device under xhci lock when stopping endpoints

In reply to Mathias Nyman

The sideband virt device lookup and the endpoint checks in xhci_sideband_stop_endpoint(), xhci_sideband_remove_endpoint() and xhci_sideband_unregister() are done without holding the xhci lock. The xhci driver may free or reallocate the virt device between the check and the actual stop endpoint command. This can happen during xHC reset in resume, reset-resume, or retried enumeration.

Move the stop endpoint code from xhci_stop_endpoint_sync() in xhci.c to a new sideband_stop_endpoint_sync() helper in xhci-sideband.c. It verifies the virt device and endpoint, and queues the stop endpoint command, while holding the xhci lock. Removing an endpoint from the sideband is done in the same locked section.

xhci_stop_endpoint_sync() has no other users, so remove it.

Also take the sideband mutex in xhci_sideband_stop_endpoint(), and keep holding it in xhci_sideband_unregister() until the sideband is detached from the virt device. Doing so serializes these with the other sideband API calls.

---
 drivers/usb/host/xhci-sideband.c | 110 +++++++++++++++++++++----------
 drivers/usb/host/xhci.c          |  55 ----------------
 drivers/usb/host/xhci.h          |   2 -
 3 files changed, 76 insertions(+), 91 deletions(-)
drivers/usb/host/xhci-sideband.c +76 -34
index ad4849903df3..39d790bb6a2b 100644
@@ -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;
+
+	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) {
+			ret = xhci_queue_stop_endpoint(sb->xhci, cmd,
+						       vdev->slot_id,
+						       ep_index, 0);
+			queued = !ret;
+			if (queued)
+				xhci_ring_cmd_db(sb->xhci);
+		}
+	}
+
+	spin_unlock_irqrestore(&sb->xhci->lock, flags);
+
+	if (!queued)
+		goto out;
+
+	wait_for_completion(cmd->completion);
 
-	ep->sideband = NULL;
-	sb->eps[ep->ep_index] = NULL;
+	if (cmd->status == COMP_COMMAND_ABORTED ||
+	    cmd->status == COMP_COMMAND_RING_STOPPED) {
+		xhci_warn(sb->xhci, "Timeout while waiting for stop endpoint command\n");
+		ret = -ETIME;
+	}
+out:
+	xhci_free_command(sb->xhci, cmd);
+
+	return ret;
 }
 
 /* Caller must hold sb->mutex */
@@ -212,21 +265,13 @@ int
 xhci_sideband_remove_endpoint(struct xhci_sideband *sb,
 			      struct usb_host_endpoint *host_ep)
 {
-	struct xhci_virt_ep *ep;
 	unsigned int ep_index;
 
 	guard(mutex)(&sb->mutex);
 
 	ep_index = xhci_get_endpoint_index(&host_ep->desc);
-	ep = sb->eps[ep_index];
 
-	if (!sideband_to_vdev(sb) || !ep || ep->sideband != sb) {
-		sb->eps[ep_index] = NULL;
-		return -ENODEV;
-	}
-	__xhci_sideband_remove_endpoint(sb, ep);
-
-	return 0;
+	return sideband_stop_endpoint_sync(sb, ep_index, true);
 }
 EXPORT_SYMBOL_GPL(xhci_sideband_remove_endpoint);
 
@@ -234,16 +279,13 @@ int
 xhci_sideband_stop_endpoint(struct xhci_sideband *sb,
 			    struct usb_host_endpoint *host_ep)
 {
-	struct xhci_virt_ep *ep;
 	unsigned int ep_index;
 
-	ep_index = xhci_get_endpoint_index(&host_ep->desc);
-	ep = sb->eps[ep_index];
+	guard(mutex)(&sb->mutex);
 
-	if (!sideband_to_vdev(sb) || !ep || ep->sideband != sb)
-		return -EINVAL;
+	ep_index = xhci_get_endpoint_index(&host_ep->desc);
 
-	return xhci_stop_endpoint_sync(sb->xhci, ep, 0, GFP_KERNEL);
+	return sideband_stop_endpoint_sync(sb, ep_index, false);
 }
 EXPORT_SYMBOL_GPL(xhci_sideband_stop_endpoint);
 
@@ -486,24 +528,24 @@ xhci_sideband_unregister(struct xhci_sideband *sb)
 
 	xhci = sb->xhci;
 
-	scoped_guard(mutex, &sb->mutex) {
-		vdev = sideband_to_vdev(sb);
-
-		for (i = 0; i < EP_CTX_PER_DEV; i++)
-			if (vdev && sb->eps[i])
-				__xhci_sideband_remove_endpoint(sb, sb->eps[i]);
+	mutex_lock(&sb->mutex);
 
-		__xhci_sideband_remove_interrupter(sb);
+	for (i = 0; i < EP_CTX_PER_DEV; i++)
+		if (sb->eps[i])
+			sideband_stop_endpoint_sync(sb, i, true);
 
-		sb->udev = NULL;
-	}
+	__xhci_sideband_remove_interrupter(sb);
 
 	spin_lock_irq(&xhci->lock);
-	sb->xhci = NULL;
+	vdev = sideband_to_vdev(sb);
 	if (vdev)
 		vdev->sideband = NULL;
+	sb->udev = NULL;
+	sb->xhci = NULL;
 	spin_unlock_irq(&xhci->lock);
 
+	mutex_unlock(&sb->mutex);
+
 	kfree(sb);
 }
 EXPORT_SYMBOL_GPL(xhci_sideband_unregister);
drivers/usb/host/xhci.c +0 -55

61 lines not on this page · raw message

drivers/usb/host/xhci.h +0 -2
index ec4bfeb4887c..6201b6b24252 100644
@@ -1963,8 +1963,6 @@ void xhci_cleanup_command_queue(struct xhci_hcd *xhci);
 void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring);
 unsigned int count_trbs(u64 addr, u64 len);
 unsigned int xhci_num_trbs_free(struct xhci_ring *ring);
-int xhci_stop_endpoint_sync(struct xhci_hcd *xhci, struct xhci_virt_ep *ep,
-			    int suspend, gfp_t gfp_flags);
 void xhci_process_cancelled_tds(struct xhci_hcd *xhci, struct xhci_virt_ep *ep);
 void xhci_update_erst_dequeue(struct xhci_hcd *xhci,
 			      struct xhci_interrupter *ir,
Signature · 1 line
2.43.0
#4 lore raw

Re: [RFT PATCH 2/2] xhci: sideband: check virt device under xhci lock when stopping endpoints

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…

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 <[email protected]>

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.

Quoted 21 lines
drivers/usb/host/xhci-sideband.c +10 -2
index ad4849903df3b..39d790bb6a2b2 100644
@@ -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;
+

drivers/usb/host/xhci-sideband.c sideband_to_vdev(struct xhci_sideband *sb) · line 110

Quoted 10 lines
drivers/usb/host/xhci-sideband.c +3 -5
 	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.

Quoted 15 lines
drivers/usb/host/xhci-sideband.c +15 -0
+
+	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;
+	}
+

drivers/usb/host/xhci-sideband.c sideband_to_vdev(struct xhci_sideband *sb) · line 135

Quoted 10 lines
drivers/usb/host/xhci-sideband.c +10 -0
+	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.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2