* [PATCH] usb: xhci: reject bulk transfers beyond mapped scatterlists
@ 2026-12-01 20:24 Artem Dinaburg
2026-12-01 20:33 ` sashiko-bot
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Artem Dinaburg @ 2026-12-01 20:24 UTC (permalink / raw)
To: mathias.nyman, stern, gregkh; +Cc: linux-usb, linux-kernel
A bulk or interrupt URB can request more bytes than its mapped
scatterlist covers. count_sg_trbs_needed() counts only the mapped
entries, but xhci_queue_bulk_tx() 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 the mismatch in the existing counting pass, before
prepare_transfer() changes the ring. This also rejects an SG URB with no
mapped entries.
Found and fixed with AI assistance.
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>
---
Compile-tested only: xhci-ring.o builds for x86_64 (allmodconfig, W=1)
without warnings, and checkpatch --strict is clean. The unpatched
behaviour was confirmed at runtime in a QEMU guest; a reproducer is
available on request.
drivers/usb/host/xhci-ring.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index a1fa188680f5..a5053c8b49fa 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -3446,7 +3446,7 @@ static inline unsigned int count_trbs_needed(struct urb *urb)
return count_trbs(urb->transfer_dma, urb->transfer_buffer_length);
}
-static unsigned int count_sg_trbs_needed(struct urb *urb)
+static int count_sg_trbs_needed(struct urb *urb)
{
struct scatterlist *sg;
unsigned int i, len, full_len, num_trbs = 0;
@@ -3462,6 +3462,9 @@ static unsigned int count_sg_trbs_needed(struct urb *urb)
break;
}
+ if (full_len || !num_trbs)
+ return -EINVAL;
+
return num_trbs;
}
@@ -3691,9 +3694,12 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
if (urb->num_sgs && !(urb->transfer_flags & URB_DMA_MAP_SINGLE)) {
num_sgs = urb->num_mapped_sgs;
sg = urb->sg;
+ ret = count_sg_trbs_needed(urb);
+ if (ret < 0)
+ return ret;
+ num_trbs = ret;
addr = (u64) sg_dma_address(sg);
block_len = sg_dma_len(sg);
- num_trbs = count_sg_trbs_needed(urb);
} else {
num_trbs = count_trbs_needed(urb);
addr = (u64) urb->transfer_dma;
--
2.39.5
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] usb: xhci: reject bulk transfers beyond mapped scatterlists
2026-12-01 20:24 [PATCH] usb: xhci: reject bulk transfers beyond mapped scatterlists Artem Dinaburg
@ 2026-12-01 20:33 ` sashiko-bot
2026-12-01 20:43 ` Alan Stern
2026-12-01 21:45 ` Michal Pecio
2 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-01 20:33 UTC (permalink / raw)
To: Artem Dinaburg; +Cc: linux-usb
Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] usb: xhci: ignored prepare_transfer() error in
xhci_queue_bulk_tx() [1]
--
commit e3051e84fd8a91e56973b850c546c8ac5a2336b8
Author: Artem Dinaburg <artem@trailofbits.com>
usb: xhci: reject bulk transfers beyond mapped scatterlists
This commit makes count_sg_trbs_needed() return -EINVAL if an URB's
transfer_buffer_length exceeds the mapped scatterlist length or if no
TRBs are counted, and checks this error in xhci_queue_bulk_tx() before
calling prepare_transfer().
> diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
> index 7ccd89a4b9951..071b3ba684d70 100644
> --- a/drivers/usb/host/xhci-ring.c
> +++ b/drivers/usb/host/xhci-ring.c
[ ... ]
> @@ -3692,9 +3695,12 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
> if (urb->num_sgs && !(urb->transfer_flags & URB_DMA_MAP_SINGLE)) {
> num_sgs = urb->num_mapped_sgs;
> sg = urb->sg;
> + ret = count_sg_trbs_needed(urb);
> + if (ret < 0)
> + return ret;
> + num_trbs = ret;
> addr = (u64) sg_dma_address(sg);
> block_len = sg_dma_len(sg);
> - num_trbs = count_sg_trbs_needed(urb);
> } else {
> num_trbs = count_trbs_needed(urb);
> addr = (u64) urb->transfer_dma;
[Severity: Critical]
This problem wasn't introduced by this patch, but later in
xhci_queue_bulk_tx() does the need_zero_pkt block ignore a
prepare_transfer() failure [1]?
drivers/usb/host/xhci-ring.c:xhci_queue_bulk_tx() {
...
if (need_zero_pkt) {
ret = prepare_transfer(xhci, xhci->devs[slot_id],
ep_index, urb->stream_id,
1, urb, 1, mem_flags);
urb_priv->td[1].end_trb = ring->enqueue;
urb_priv->td[1].end_seg = ring->enq_seg;
field = TRB_TYPE(TRB_NORMAL) | ring->cycle_state | TRB_IOC;
queue_trb(xhci, ring, 0, 0, 0, TRB_INTR_TARGET(0), field);
}
...
}
If the payload TRBs fill the ring and prepare_transfer() fails to expand
it, queue_trb() still writes the zero-length packet TRB into an
unreserved slot, overwriting active or link TRBs, and leaves
urb_priv->td[1] off the endpoint's td_list so the URB never completes.
[1] https://sashiko.dev/#/bug/linux-d1207da1-af8c-42d8-b0aa-53d3000029e7
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201202438.32470.9674.04-xhci-sg-length-patch@trailofbits.com?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] usb: xhci: reject bulk transfers beyond mapped scatterlists
2026-12-01 20:24 [PATCH] usb: xhci: reject bulk transfers beyond mapped scatterlists Artem Dinaburg
2026-12-01 20:33 ` sashiko-bot
@ 2026-12-01 20:43 ` Alan Stern
2026-12-01 20:54 ` Artem Dinaburg
2026-12-01 21:45 ` Michal Pecio
2 siblings, 1 reply; 5+ messages in thread
From: Alan Stern @ 2026-12-01 20:43 UTC (permalink / raw)
To: Artem Dinaburg; +Cc: mathias.nyman, gregkh, linux-usb, linux-kernel
On Tue, Dec 01, 2026 at 03:24:40PM -0500, Artem Dinaburg wrote:
> A bulk or interrupt URB can request more bytes than its mapped
> scatterlist covers. count_sg_trbs_needed() counts only the mapped
> entries, but xhci_queue_bulk_tx() 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 the mismatch in the existing counting pass, before
> prepare_transfer() changes the ring. This also rejects an SG URB with no
> mapped entries.
Why are you trying to fix this by modifying the xhci-hcd driver? In
theory the same thing could happen to any HCD.
This possible error should be handled in the USB core, in
usb_submit_urb(). That routine already has a section specifically for
checking transfers that use scatter-gather; it's the perfect place to
add your check.
Alan Stern
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] usb: xhci: reject bulk transfers beyond mapped scatterlists
2026-12-01 20:43 ` Alan Stern
@ 2026-12-01 20:54 ` Artem Dinaburg
0 siblings, 0 replies; 5+ messages in thread
From: Artem Dinaburg @ 2026-12-01 20:54 UTC (permalink / raw)
To: Alan Stern; +Cc: mathias.nyman, gregkh, linux-usb, linux-kernel
Hi Alan,
Thanks for the quick review!
You're right, usb_submit_urb() is a better place for this check, and
it covers every HCD instead of just xHCI. I'll prepare and test a v2
patch that moves the check there.
Thanks,
Artem Dinaburg
On Tue, Dec 1, 2026 at 3:43 PM Alan Stern <stern@rowland.harvard.edu> wrote:
>
> On Tue, Dec 01, 2026 at 03:24:40PM -0500, Artem Dinaburg wrote:
> > A bulk or interrupt URB can request more bytes than its mapped
> > scatterlist covers. count_sg_trbs_needed() counts only the mapped
> > entries, but xhci_queue_bulk_tx() 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 the mismatch in the existing counting pass, before
> > prepare_transfer() changes the ring. This also rejects an SG URB with no
> > mapped entries.
>
> Why are you trying to fix this by modifying the xhci-hcd driver? In
> theory the same thing could happen to any HCD.
>
> This possible error should be handled in the USB core, in
> usb_submit_urb(). That routine already has a section specifically for
> checking transfers that use scatter-gather; it's the perfect place to
> add your check.
>
> Alan Stern
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] usb: xhci: reject bulk transfers beyond mapped scatterlists
2026-12-01 20:24 [PATCH] usb: xhci: reject bulk transfers beyond mapped scatterlists Artem Dinaburg
2026-12-01 20:33 ` sashiko-bot
2026-12-01 20:43 ` Alan Stern
@ 2026-12-01 21:45 ` Michal Pecio
2 siblings, 0 replies; 5+ messages in thread
From: Michal Pecio @ 2026-12-01 21:45 UTC (permalink / raw)
To: Artem Dinaburg; +Cc: mathias.nyman, stern, gregkh, linux-usb, linux-kernel
On Tue, 1 Dec 2026 15:24:40 -0500, Artem Dinaburg wrote:
> A bulk or interrupt URB can request more bytes than its mapped
> scatterlist covers. count_sg_trbs_needed() counts only the mapped
> entries, but xhci_queue_bulk_tx() 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.
Which one and how exactly?
That's a driver bug, albeit one which could conceivably
be triggered by a nonconforming device.
Regards,
Michal
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-01 21:45 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 20:24 [PATCH] usb: xhci: reject bulk transfers beyond mapped scatterlists Artem Dinaburg
2026-12-01 20:33 ` sashiko-bot
2026-12-01 20:43 ` Alan Stern
2026-12-01 20:54 ` Artem Dinaburg
2026-12-01 21:45 ` Michal Pecio
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox