From: Mathias Nyman <mathias.nyman@linux.intel.com>
To: <gregkh@linuxfoundation.org>
Cc: <linux-usb@vger.kernel.org>,
Mathias Nyman <mathias.nyman@linux.intel.com>,
stable@vger.kernel.org,
Selvarasu Ganesan <selvarasu.g@samsung.com>,
Akash M <akash.m5@samsung.com>,
Wesley Cheng <wesley.cheng@oss.qualcomm.com>
Subject: [PATCH 05/10] xhci: sideband: avoid use-after-free of stale virt device pointer
Date: Fri, 27 Nov 2026 14:17:50 +0200 [thread overview]
Message-ID: <20261127121755.281420-6-mathias.nyman@linux.intel.com> (raw)
In-Reply-To: <20261127121755.281420-1-mathias.nyman@linux.intel.com>
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")
Cc: stable@vger.kernel.org
Reviewed-by: Selvarasu Ganesan <selvarasu.g@samsung.com>
Tested-by: Akash M <akash.m5@samsung.com>
Tested-by: Wesley Cheng <wesley.cheng@oss.qualcomm.com>
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
next prev parent reply other threads:[~2026-11-27 12:18 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 12:17 [PATCH 00/10] xhci fixes for usb-linus Mathias Nyman
2026-11-27 12:17 ` [PATCH 01/10] usb: xhci: Fix ep0 maxpacket update on enabled slots Mathias Nyman
2026-11-27 12:17 ` [PATCH 02/10] xhci: check for halted host before accessing virt devices in xhci_check_args() Mathias Nyman
2026-11-27 12:17 ` [PATCH 03/10] xhci: Give back URB under lock in xhci_urb_dequeue() Mathias Nyman
2026-11-27 12:17 ` [PATCH 04/10] xhci: Fix reporting incorrect supported speeds in roothub BOS descriptor Mathias Nyman
2026-11-27 12:17 ` Mathias Nyman [this message]
2026-11-27 12:17 ` [PATCH 06/10] xhci: sideband: check virt device under xhci lock when stopping endpoints Mathias Nyman
2026-11-27 12:17 ` [PATCH 07/10] usb: xhci: tegra: Fix missing semi-colon Mathias Nyman
2026-11-27 12:17 ` [PATCH 08/10] usb: xhci: Demote isoch buffer overrun message to debug level Mathias Nyman
2026-11-27 12:17 ` [PATCH 09/10] xhci: fix use-after-free for errors mid TD on empty halted rings Mathias Nyman
2026-11-27 12:17 ` [PATCH 10/10] xhci: fix NULL pointer dereference of rhub_port in xhci_urb_enqueue() Mathias Nyman
2026-11-27 12:28 ` [PATCH 00/10] xhci fixes for usb-linus 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=20261127121755.281420-6-mathias.nyman@linux.intel.com \
--to=mathias.nyman@linux.intel.com \
--cc=akash.m5@samsung.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-usb@vger.kernel.org \
--cc=selvarasu.g@samsung.com \
--cc=stable@vger.kernel.org \
--cc=wesley.cheng@oss.qualcomm.com \
/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