From: sashiko-bot@kernel.org
To: "Hunter Laux" <hunterlaux@gmail.com>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH] usb: dwc2: hcd: Fix DMA overrun of short IN buffers
Date: Sat, 05 Dec 2026 07:56:29 +0000 [thread overview]
Message-ID: <sashiko-outbox-251251@kernel.org> (raw)
In-Reply-To: <20261205073050.1510448-1-hunterlaux@gmail.com>
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] usb: dwc2: ISO IN overrun overwrites stored_xfer_buffer pointer
- [High] usb: dwc2: uninitialized bounce buffer leaks heap on ISO IN
URBs
- [Medium] usb: dwc2: dwc2_dma_buffer_len() relies on mutable ep0
wMaxPacketSize
--
commit 5a449b618789413bb34400a4c7e64af670f621f3
Author: Hunter Laux <hunterlaux@gmail.com>
usb: dwc2: hcd: Fix DMA overrun of short IN buffers
This commit extends dwc2's DMA bounce buffer path to also bounce IN URBs
whose transfer_buffer_length is not a whole number of max-size packets,
rounding the bounce buffer size up to whole packets while skipping URBs
with URB_NO_TRANSFER_DMA_MAP set.
Link: https://lore.kernel.org/linux-usb/20200226210414.28133-2-linux@roeck-us.net/
> diff --git a/drivers/usb/dwc2/hcd.c b/drivers/usb/dwc2/hcd.c
> index 40ab50538334a..ac12b3cc8d213 100644
> --- a/drivers/usb/dwc2/hcd.c
> +++ b/drivers/usb/dwc2/hcd.c
> @@ -2452,6 +2452,30 @@ static int dwc2_alloc_split_dma_aligned_buf(struct dwc2_hsotg *hsotg,
>
> #define DWC2_USB_DMA_ALIGN 4
>
> +/*
> + * IN transfers are programmed as a whole number of max-size packets (see
> + * dwc2_hc_start_transfer()), and after a short packet the controller may
> + * write past the bytes it received, up to that size. The DMA buffer of an
> + * IN transfer is therefore a whole number of packets long.
> + *
> + * The size comes from urb->ep, not from usb_maxpacket(): the URBs of an
> + * endpoint being disabled are given back after usb_disable_endpoint() has
> + * cleared it in udev->ep_in[], and dwc2_free_dma_aligned_buffer() must get
> + * the same length as dwc2_alloc_dma_aligned_buffer() did.
> + */
> +static size_t dwc2_dma_buffer_len(struct urb *urb)
> +{
> + size_t len = urb->transfer_buffer_length;
> + unsigned int maxp;
> +
> + if (usb_urb_dir_in(urb)) {
> + maxp = usb_endpoint_maxp(&urb->ep->desc);
> + if (maxp)
> + len = roundup(len, maxp);
> + }
> + return len;
> +}
[Severity: High]
Can an isochronous IN transfer overrun into stored_xfer_buffer here?
In Buffer DMA mode, dwc2_hc_init_xfer() programs each isochronous frame at
urb->dma + frame_desc->offset, and dwc2_hc_start_transfer() rounds each
frame's xfer_len up to max_packet. A frame can therefore write up to
frame_desc->offset + roundup(frame_desc->length, maxp), while
dwc2_dma_buffer_len() only rounds the total transfer_buffer_length.
For example, on a 512-byte maxp endpoint, a usbfs USBDEVFS_URB_TYPE_ISO IN
URB with frame 0 of 504 bytes and frame 1 of 4 bytes has
transfer_buffer_length = 508. dwc2_dma_buffer_len() returns 512, placing
stored_xfer_buffer at offset 512, while frame 1 is programmed for [504,
1016) and overwrites it. On completion, dwc2_free_dma_aligned_buffer()
copies to that corrupted pointer and restores it into urb->transfer_buffer.
Conversely, if the frame lengths sum to a multiple of maxp (such as 504
and 8 bytes), dwc2_dma_buffer_len() equals transfer_buffer_length and
skips bouncing while the last frame still writes past the buffer. For
usb_pipeisoc(urb->pipe), taking the maximum of len and
iso_frame_desc[i].offset + roundup(iso_frame_desc[i].length, maxp) across
urb->number_of_packets would cover both cases.
> @@ -2462,7 +2486,7 @@ static void dwc2_free_dma_aligned_buffer(struct urb *urb)
>
> /* Restore urb->transfer_buffer from the end of the allocated area */
> memcpy(&stored_xfer_buffer,
> - PTR_ALIGN(urb->transfer_buffer + urb->transfer_buffer_length,
> + PTR_ALIGN(urb->transfer_buffer + dwc2_dma_buffer_len(urb),
> dma_get_cache_alignment()),
> sizeof(urb->transfer_buffer));
[Severity: Medium]
Can urb->ep->desc.wMaxPacketSize change while an ep0 IN URB is mapped,
causing dwc2_free_dma_aligned_buffer() to read stored_xfer_buffer from the
wrong offset?
If a non-conforming device reports mismatched bMaxPacketSize0 values
during initial enumeration so udev->ep0.desc.wMaxPacketSize differs from
udev->descriptor.bMaxPacketSize0, a later usb_reset_device() reaches the
wMaxPacketSize update in hub_port_init() before usb_ep0_reinit() flushes
in-flight ep0 URBs. Meanwhile, an unlocked caller such as
do_proc_control() can submit an ep0 IN URB that gets bounced using the old
wMaxPacketSize:
CPU0 CPU1
usb_reset_device()
...
hub_port_init()
do_proc_control()
usb_unlock_device(dev)
usbfs_start_wait_urb()
dwc2_alloc_dma_aligned_buffer()
/* uses old wMaxPacketSize */
udev->ep0.desc.wMaxPacketSize = ...
usb_ep0_reinit(udev)
...
dwc2_free_dma_aligned_buffer() <-- reads wrong offset
Depending on whether wMaxPacketSize increased or decreased, this reads
past the slab allocation or out of the bounce buffer payload, corrupting
urb->transfer_buffer and writing through a bogus pointer in memcpy() if
actual_length > 0. Storing the original transfer_buffer pointer at a
fixed cache-line-aligned prefix before the DMA buffer would avoid
depending on wMaxPacketSize on the free path.
[ ... ]
> @@ -2495,8 +2528,7 @@ static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
> * pointer. This allocation is guaranteed to be aligned properly for
> * DMA
> */
> - kmalloc_size = urb->transfer_buffer_length +
> - (dma_get_cache_alignment() - 1) +
> + kmalloc_size = len + (dma_get_cache_alignment() - 1) +
> sizeof(urb->transfer_buffer);
>
> kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
[Severity: High]
Does bouncing aligned isochronous IN URBs through an uninitialized
kmalloc() buffer leak kernel heap memory to userspace?
proc_do_submiturb() in drivers/usb/core/devio.c allocates aligned
kmalloc() buffers for USBDEVFS_URB_TYPE_ISO IN transfers and explicitly
zeroes them so short-packet gaps do not disclose kernel memory when
copy_urb_data_to_user() copies the full transfer_buffer_length. Because
those buffers are now bounced whenever transfer_buffer_length is not a
multiple of maxp, dwc2_free_dma_aligned_buffer() copies the full
transfer_buffer_length from the uninitialized bounce buffer back over the
zeroed gaps, leaking slab contents to userspace whenever actual_length > 0.
Zeroing the bounce buffer with kzalloc() for isochronous IN URBs, or
copying back only each frame's iso_frame_desc[i].actual_length bytes at
iso_frame_desc[i].offset, would prevent the leak.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261205073050.1510448-1-hunterlaux@gmail.com?part=1
prev parent reply other threads:[~2026-12-05 7:56 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 7:30 [PATCH] usb: dwc2: hcd: Fix DMA overrun of short IN buffers Hunter Laux
2026-12-05 7:56 ` sashiko-bot [this message]
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=sashiko-outbox-251251@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=hunterlaux@gmail.com \
--cc=linux-usb@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.