From: Artem Dinaburg <artem@trailofbits.com>
To: mathias.nyman@intel.com, stern@rowland.harvard.edu,
gregkh@linuxfoundation.org
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] usb: xhci: reject bulk transfers beyond mapped scatterlists
Date: Tue, 1 Dec 2026 15:24:40 -0500 [thread overview]
Message-ID: <20261201202438.32470.9674.04-xhci-sg-length-patch@trailofbits.com> (raw)
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
next reply other threads:[~2026-12-01 20:24 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 20:24 Artem Dinaburg [this message]
2026-12-01 20:43 ` Alan Stern
2026-12-01 20:54 ` Artem Dinaburg
2026-12-01 21:45 ` Michal Pecio
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=20261201202438.32470.9674.04-xhci-sg-length-patch@trailofbits.com \
--to=artem@trailofbits.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=mathias.nyman@intel.com \
--cc=stern@rowland.harvard.edu \
/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
all inboxes | Powered by JetHome®