* [[RFT PATCH v2] 1/2] xhci: sideband: avoid use-after-free of stale virt device pointer
2026-11-19 21:48 [[RFT PATCH v2] 0/2] xhci: sideband: fix use-after-free of stale virt device pointer Mathias Nyman
@ 2026-11-19 21:48 ` Mathias Nyman
2026-11-19 21:53 ` sashiko-bot
2026-11-19 21:48 ` [[RFT PATCH v2] 2/2] xhci: sideband: check virt device under xhci lock when stopping endpoints Mathias Nyman
1 sibling, 1 reply; 5+ messages in thread
From: Mathias Nyman @ 2026-11-19 21:48 UTC (permalink / raw)
To: quic_wcheng, hulianqin, selvarasu.g; +Cc: linux-usb, Mathias Nyman
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 when called 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 an invalid
vdev.
This solution isn't airtight. It only validates vdev when entering
sideband API functions. It does not serialize the sideband API
functions against the code in the xhci driver that reallocates vdev,
so vdev may still be freed in the middle of a sideband call. It's still
a big improvement and a first step in resolving this.
The following patch starts working on the serialization.
Fixes: de66754e9f80 ("xhci: sideband: add initial api to register a secondary interrupter entity")
Signed-off-by: Mathias Nyman <mathias.nyman@linux.intel.com>
---
drivers/usb/host/xhci-sideband.c | 46 +++++++++++++++++++++----------
include/linux/usb/xhci-sideband.h | 4 +--
2 files changed, 34 insertions(+), 16 deletions(-)
diff --git a/drivers/usb/host/xhci-sideband.c b/drivers/usb/host/xhci-sideband.c
index a5deeee4d5dc..e4dd298b80f1 100644
--- a/drivers/usb/host/xhci-sideband.c
+++ b/drivers/usb/host/xhci-sideband.c
@@ -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,7 +348,7 @@ xhci_sideband_create_interrupter(struct xhci_sideband *sb, int num_seg,
guard(mutex)(&sb->mutex);
- if (!sb->vdev)
+ if (!sideband_to_vdev(sb))
return -ENODEV;
if (sb->ir)
@@ -436,7 +455,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 +490,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);
diff --git a/include/linux/usb/xhci-sideband.h b/include/linux/usb/xhci-sideband.h
index 005257085dcb..2d153cb57831 100644
--- a/include/linux/usb/xhci-sideband.h
+++ b/include/linux/usb/xhci-sideband.h
@@ -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;
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [[RFT PATCH v2] 2/2] xhci: sideband: check virt device under xhci lock when stopping endpoints
2026-11-19 21:48 [[RFT PATCH v2] 0/2] xhci: sideband: fix use-after-free of stale virt device pointer Mathias Nyman
2026-11-19 21:48 ` [[RFT PATCH v2] 1/2] xhci: sideband: avoid " Mathias Nyman
@ 2026-11-19 21:48 ` Mathias Nyman
2026-11-19 21:54 ` sashiko-bot
1 sibling, 1 reply; 5+ messages in thread
From: Mathias Nyman @ 2026-11-19 21:48 UTC (permalink / raw)
To: quic_wcheng, hulianqin, selvarasu.g; +Cc: linux-usb, Mathias Nyman
Start work to protect sideband API functions against the xhci driver
freeing or reallocating the virt device (vdev) in the middle of an API
call.
Make the following sideband API calls hold xhci->lock while validating
the vdev and its endpoint, and while queueing the stop endpoint command:
xhci_sideband_unregister()
xhci_sideband_stop_endpoint()
xhci_sideband_remove_endpoint()
Locking changes are still needed in several xhci 'core' driver vdev free
paths before the above calls are fully serialized against vdev free.
Cases involve xHC reset in resume, enumeration retry, and reset-resume.
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.
xhci_stop_endpoint_sync() has no other users, so remove it.
Also take sb->mutex in xhci_sideband_stop_endpoint(), and keep holding
it in xhci_sideband_unregister() until the sideband is detached from the
vdev.
Fixes: de66754e9f80 ("xhci: sideband: add initial api to register a secondary interrupter entity")
Signed-off-by: Mathias Nyman <mathias.nyman@linux.intel.com>
---
drivers/usb/host/xhci-sideband.c | 121 ++++++++++++++++++++++---------
drivers/usb/host/xhci.c | 55 --------------
drivers/usb/host/xhci.h | 2 -
3 files changed, 87 insertions(+), 91 deletions(-)
diff --git a/drivers/usb/host/xhci-sideband.c b/drivers/usb/host/xhci-sideband.c
index e4dd298b80f1..6f7712f95033 100644
--- a/drivers/usb/host/xhci-sideband.c
+++ b/drivers/usb/host/xhci-sideband.c
@@ -91,20 +91,78 @@ sideband_to_vdev(struct xhci_sideband *sb)
return NULL;
}
-/* Caller must hold sb->mutex */
-static void
-__xhci_sideband_remove_endpoint(struct xhci_sideband *sb, struct xhci_virt_ep *ep)
+/*
+ * Synchronous xHCI stop endpoint helper. Issues a stop endpoint command and
+ * waits for the command completion before returning. This does not call
+ * xhci_handle_cmd_stop_ep(), which has additional handling for 'context error'
+ * cases, along with transfer ring cleanup.
+ *
+ * Caller must hold sb->mutex
+ */
+static int
+sideband_stop_endpoint_sync(struct xhci_sideband *sb, unsigned int 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;
+ 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);
+
+ spin_lock_irqsave(&sb->xhci->lock, flags);
+
+ vdev = sideband_to_vdev(sb);
+ ep = sb->eps[ep_index];
+
+ /* validate that xhci didn't reallocate vdev during reset or resume */
+ if (!vdev || !ep || ep->sideband != sb)
+ goto out_unlock;
+
+ if (remove) {
+ ep->sideband = NULL;
+ /* endpoint is removed, not running is not an error */
+ ret = 0;
+ }
+
+ if (!ep->ring)
+ goto out_unlock;
+
+ /* Stop the endpoint if it's running */
+ ctx = xhci_get_ep_ctx(sb->xhci, vdev->out_ctx, ep_index);
+ if (GET_EP_CTX_STATE(ctx) == EP_STATE_RUNNING) {
+ if (!cmd) {
+ ret = -ENOMEM;
+ goto out_unlock;
+ }
+ ret = xhci_queue_stop_endpoint(sb->xhci, cmd, vdev->slot_id,
+ ep_index, 0);
+ if (ret)
+ goto out_unlock;
+
+ xhci_ring_cmd_db(sb->xhci);
+ spin_unlock_irqrestore(&sb->xhci->lock, flags);
+ wait_for_completion(cmd->completion);
+
+ 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;
+ }
+ xhci_free_command(sb->xhci, cmd);
+ return ret;
+ }
- ep->sideband = NULL;
- sb->eps[ep->ep_index] = NULL;
+out_unlock:
+ spin_unlock_irqrestore(&sb->xhci->lock, flags);
+ if (cmd)
+ xhci_free_command(sb->xhci, cmd);
+
+ return ret;
}
/* Caller must hold sb->mutex */
@@ -212,21 +270,17 @@ int
xhci_sideband_remove_endpoint(struct xhci_sideband *sb,
struct usb_host_endpoint *host_ep)
{
- struct xhci_virt_ep *ep;
unsigned int ep_index;
+ int ret;
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);
+ ret = sideband_stop_endpoint_sync(sb, ep_index, true);
+ sb->eps[ep_index] = NULL;
- return 0;
+ return ret;
}
EXPORT_SYMBOL_GPL(xhci_sideband_remove_endpoint);
@@ -234,16 +288,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);
@@ -489,24 +540,26 @@ 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);
-
- sb->udev = NULL;
+ for (i = 0; i < EP_CTX_PER_DEV; i++) {
+ if (sb->eps[i]) {
+ sideband_stop_endpoint_sync(sb, i, true);
+ sb->eps[i] = 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);
diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
index 30ac1bc4559b..bcb482795bbc 100644
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -2883,61 +2883,6 @@ static int xhci_reserve_bandwidth(struct xhci_hcd *xhci,
return -ENOMEM;
}
-/*
- * Synchronous XHCI stop endpoint helper. Issues the stop endpoint command and
- * waits for the command completion before returning. This does not call
- * xhci_handle_cmd_stop_ep(), which has additional handling for 'context error'
- * cases, along with transfer ring cleanup.
- *
- * xhci_stop_endpoint_sync() is intended to be utilized by clients that manage
- * their own transfer ring, such as offload situations.
- */
-int xhci_stop_endpoint_sync(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, int suspend,
- gfp_t gfp_flags)
-{
- struct xhci_command *command;
- struct xhci_ep_ctx *ep_ctx;
- unsigned long flags;
- int ret = -ENODEV;
-
- command = xhci_alloc_command(xhci, true, gfp_flags);
- if (!command)
- return -ENOMEM;
-
- spin_lock_irqsave(&xhci->lock, flags);
-
- /* make sure endpoint exists and is running before stopping it */
- if (ep->ring) {
- ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep->ep_index);
- if (GET_EP_CTX_STATE(ep_ctx) == EP_STATE_RUNNING)
- ret = xhci_queue_stop_endpoint(xhci, command,
- ep->vdev->slot_id,
- ep->ep_index, suspend);
- }
-
- if (ret < 0) {
- spin_unlock_irqrestore(&xhci->lock, flags);
- goto out;
- }
-
- xhci_ring_cmd_db(xhci);
- spin_unlock_irqrestore(&xhci->lock, flags);
-
- wait_for_completion(command->completion);
-
- /* No handling for COMP_CONTEXT_STATE_ERROR done at command completion*/
- if (command->status == COMP_COMMAND_ABORTED ||
- command->status == COMP_COMMAND_RING_STOPPED) {
- xhci_warn(xhci, "Timeout while waiting for stop endpoint command\n");
- ret = -ETIME;
- }
-out:
- xhci_free_command(xhci, command);
-
- return ret;
-}
-EXPORT_SYMBOL_GPL(xhci_stop_endpoint_sync);
-
/*
* xhci_usb_endpoint_maxp - get endpoint max packet size
* @host_ep: USB host endpoint to be checked
diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h
index ec4bfeb4887c..6201b6b24252 100644
--- a/drivers/usb/host/xhci.h
+++ b/drivers/usb/host/xhci.h
@@ -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,
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread