From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796457390; b=hUj3OOOTBNdkllVYo+9eNJd41TMG0yZHJ6jP5FgMwapbf/ZYAwGEJeSOsvhPe7HvVBZj 6mwS/Nb0daEZBzeLEdXom80+SnLCpAuizTz/ZyJOE43EQdQEQeTR6POV/tI0eIeX+iwaR P6PyxsErLosv/TDmC39cKxiaLtu3xA0LJcbGX1WVyTBlO4SqrxuAYsQbw5jXgfNlA1PgZ je0g4RaCbvJx3+H9bC2XX9HOP2oohTFgS//AHzfPbrpDHSXI1EYGVfxlQ5MwEvmkwddNC 32JiBdGRDwBoymLTK5cm8bk5u9vWhZt/s7bIWqS1FHM2vVxoweEYFmh6Cua1pR9WVfA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796457390; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=NcSa3Mpe53zeEYleNcdtla+NPsx7FoZG+6B2W1Syr5k=; b=Pml7t/2ViFRF52whgwQP3plVGT2WoOkKC5DSwPZT9rOg1ckLcDEUSiI5BnpSOsBC/B3n SzMb/Sq7ofFhgdGfnJVAr6595bhKm2/QBKr1UFqv61revjqW0gZwWfE/6ZWMAPtKMgLsm 9PHAXL36szTmNAaBmHe+VWZ9MLI4uiGDkdqvxwyR9/z5d/wHzoGUodZ+TOGqCerVhjnr2 GbPQQtBSboYdTWTgk/0/7H1mmkgq8Xu8mpiIYV431tx4k+9pA3oahvHBW/aTH+GH+6Bnu Re7kyRqzR28kGNtgO9OLRkKRPJd0tQV7ZKJW52PtXXveZ84MnPhzBTeJ4Mez2gn/wOw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=VbaZ7UEG; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6DA9C606A322 for ; Sat, 5 Dec 2026 07:56:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=VbaZ7UEG Received: by smtp.kernel.org (Postfix) with ESMTPSA id D1BF71F0092B; Sat, 5 Dec 2026 07:56:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796457389; bh=NcSa3Mpe53zeEYleNcdtla+NPsx7FoZG+6B2W1Syr5k=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=VbaZ7UEGhDKyQrOZHLb+7fwIbeEokQO7I64Ua1teSii7MmsP7soSVBcuA3jQoXGiP 43Tv4nAy5dgb4AD762kuITxGJBkGrN+dkUXrcqrEykYlDl9ffRo6hSRFjtbMD1Lv4a ZhutlOlbMQQ/pDm2wCFo3s/zu3wQ34oo6fyrM8pFftI8cujig6nL77rcu051sjlVO/ MK0yfaw1Y1qUaVFS9iTErh/HJ1r6tNnKIIIX5B+4XFyW7vy48wwpvytWeR7XSeX9aA QXTjQLsl5RgR4/+Il0OCyvXesXTNQNy8d2dvV8fMU88amBFy6iIsXiJUp/NPNhIgZC TgXAMlS3E2NIA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH] usb: dwc2: hcd: Fix DMA overrun of short IN buffers Reply-To: sashiko-reviews@lists.linux.dev To: "Hunter Laux" Cc: linux-usb@vger.kernel.org In-Reply-To: <20261205073050.1510448-1-hunterlaux@gmail.com> References: <20261205073050.1510448-1-hunterlaux@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Sat, 05 Dec 2026 07:56:29 +0000 X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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, > =20 > #define DWC2_USB_DMA_ALIGN 4 > =20 > +/* > + * 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() h= as > + * 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 =3D urb->transfer_buffer_length; > + unsigned int maxp; > + > + if (usb_urb_dir_in(urb)) { > + maxp =3D usb_endpoint_maxp(&urb->ep->desc); > + if (maxp) > + len =3D 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 =3D 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) > =20 > /* 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 =3D ... 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 =3D urb->transfer_buffer_length + > - (dma_get_cache_alignment() - 1) + > + kmalloc_size =3D len + (dma_get_cache_alignment() - 1) + > sizeof(urb->transfer_buffer); > =20 > kmalloc_ptr =3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261205073050.1510= 448-1-hunterlaux@gmail.com?part=3D1