* [PATCH v2] usb: core: reject SG URBs longer than their scatterlist
@ 2026-12-02 4:12 Artem Dinaburg
2026-12-02 4:19 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Artem Dinaburg @ 2026-12-02 4:12 UTC (permalink / raw)
To: gregkh, stern, mathias.nyman; +Cc: linux-usb, linux-kernel
A bulk or interrupt URB that uses a scatterlist can request more bytes
than the scatterlist covers. usb_sg_init() copies its caller's length
into transfer_buffer_length without comparing it to the scatterlist,
and usb_submit_urb() does not check it either. xhci-hcd queues TRBs
until the requested length is reached; after the final entry it
continues from the last DMA address and length, so the controller
transfers data outside the mapping. A USB device can cause this
mismatch.
Reject such URBs in usb_submit_urb() so that every HCD is covered. The
existing scatter-gather check is skipped when the bus has no SG
constraint, as is the case for xHCI, so sum the entry lengths for every
SG URB and keep the maxpacket check conditional.
Found and fixed with AI assistance.
Suggested-by: Alan Stern <stern@rowland.harvard.edu>
Fixes: 86065c2719a5 ("xhci: don't rely on precalculated value of needed trbs in the enqueue loop")
Cc: stable@vger.kernel.org
Signed-off-by: Artem Dinaburg <artem@trailofbits.com>
---
v2: Move the check from xhci-hcd into usb_submit_urb(), as suggested
by Alan Stern.
v1: https://lore.kernel.org/r/20261201202438.32470.9674.04-xhci-sg-length-patch@trailofbits.com
Tested on mainline 19cc08c1a6a6 in QEMU (q35, KASAN): an emulated
storage device that makes the driver submit an SG URB longer than its
scatterlist now gets -EINVAL from usb_submit_urb(). 1 GiB direct I/O
over usb-storage and UAS on xHCI, usb-storage on EHCI, and xHCI with an
Intel vIOMMU showed no regressions. Built with W=1 on mainline and
usb-next.
drivers/usb/core/urb.c | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/drivers/usb/core/urb.c b/drivers/usb/core/urb.c
index ae0d61b5081f..2a3b317f2de8 100644
--- a/drivers/usb/core/urb.c
+++ b/drivers/usb/core/urb.c
@@ -493,13 +493,24 @@ int usb_submit_urb(struct urb *urb, gfp_t mem_flags)
urb->iso_frame_desc[n].status = -EXDEV;
urb->iso_frame_desc[n].actual_length = 0;
}
- } else if (urb->num_sgs && !urb->dev->bus->no_sg_constraint) {
+ } else if (urb->num_sgs) {
struct scatterlist *sg;
+ u64 sg_len = 0;
int i;
- for_each_sg(urb->sg, sg, urb->num_sgs - 1, i)
+ for_each_sg(urb->sg, sg, urb->num_sgs, i) {
+ sg_len += sg->length;
+
+ /* Some HCDs need whole packets in every entry but the last */
+ if (urb->dev->bus->no_sg_constraint || i == urb->num_sgs - 1)
+ continue;
if (sg->length % max)
return -EINVAL;
+ }
+
+ /* Don't let the HCD transfer past the end of the scatterlist */
+ if (urb->transfer_buffer_length > sg_len)
+ return -EINVAL;
}
/* the I/O buffer must be mapped/unmapped, except when length=0 */
--
2.55.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2] usb: core: reject SG URBs longer than their scatterlist
2026-12-02 4:12 [PATCH v2] usb: core: reject SG URBs longer than their scatterlist Artem Dinaburg
@ 2026-12-02 4:19 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02 4:19 UTC (permalink / raw)
To: Artem Dinaburg; +Cc: linux-usb
> A bulk or interrupt URB that uses a scatterlist can request more bytes
> than the scatterlist covers. usb_sg_init() copies its caller's length
> into transfer_buffer_length without comparing it to the scatterlist,
> and usb_submit_urb() does not check it either. xhci-hcd queues TRBs
> until the requested length is reached; after the final entry it
> continues from the last DMA address and length, so the controller
> transfers data outside the mapping. A USB device can cause this
> mismatch.
>
> Reject such URBs in usb_submit_urb() so that every HCD is covered. The
> existing scatter-gather check is skipped when the bus has no SG
> constraint, as is the case for xHCI, so sum the entry lengths for every
> SG URB and keep the maxpacket check conditional.
>
> Found and fixed with AI assistance.
>
> Suggested-by: Alan Stern <stern@rowland.harvard.edu>
> Fixes: 86065c2719a5 ("xhci: don't rely on precalculated value of needed trbs in the enqueue loop")
> Cc: stable@vger.kernel.org
> Signed-off-by: Artem Dinaburg <artem@trailofbits.com>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202041214.97186-1-artem@trailofbits.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-02 4:19 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 4:12 [PATCH v2] usb: core: reject SG URBs longer than their scatterlist Artem Dinaburg
2026-12-02 4:19 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox