* [PATCH] usb: gadget: uvc: cap isoc payload at request size
@ 2026-12-01 7:27 Keisuke Tsukuda
2026-12-01 7:39 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Keisuke Tsukuda @ 2026-12-01 7:27 UTC (permalink / raw)
To: nkapron@google.com, gregkh@linuxfoundation.org
Cc: linux-usb@vger.kernel.org, xu.yang_2@nxp.com, mgr@kernel.org,
Frank.Li@nxp.com, farhad.alemi@berkeley.edu, Keisuke Tsukuda,
stable@vger.kernel.org
uvc_buffer_prepare() records a payload size when userspace queues a
frame. If QBUF precedes the first STREAMON, reqs_per_frame is zero
and the payload size is set to max_req_size. That fallback avoids
the earlier zero-divisor calculation, but STREAMON can subsequently
allocate smaller isochronous requests.
After STREAMOFF, reqs_per_frame and req_size are not reset, so a
buffer queued for the next stream can also use the previous stream's
request size.
The copying isochronous encoder uses the stored payload size as its
write limit, while its request buffer is allocated with video->req_size.
For example, a 320x180 YUY2 frame at a 666666 (100 ns) interval with
max_req_size of 512 bytes can leave a pre-STREAMON buffer at 512 while
STREAMON allocates 228-byte requests. The encoder can then write past
the allocation. On the observed device, stream startup produced
list_del warnings in uvcg_video_pump and uvcg_video_hw_submit, followed
by an Oops.
Cap the payload size at the current video->req_size in the pump, after
request sizing and immediately before encoding. This prevents the
copying encoder from exceeding its allocation. The common pump also
applies the bound to scatter-gather request lengths.
Fixes: 2edc1acb1a25 ("usb: gadget: uvc: fix req_payload_size calculation")
Reported-by: Farhad Alemi <farhad.alemi@berkeley.edu>
Closes: https://lore.kernel.org/r/CA+0ovCh5uOgB5CQm5eqZFoWE11Uqs4=t4yES-7E1_oH60HtaKw@mail.gmail.com
Cc: stable@vger.kernel.org
Signed-off-by: Keisuke Tsukuda <tkdkei@outlook.jp>
---
Hardware validation: Raspberry Pi CM4 (dwc2) to a Linux USB host,
320x180 YUY2. The unmodified module produced list_del corruption and
an Oops at stream start; the patched module delivered 12 frames without
an Oops. The first captured frame was displaced in its lower portion;
later frames were intact.
diff --git a/drivers/usb/gadget/function/uvc_video.c b/drivers/usb/gadget/function/uvc_video.c
--- a/drivers/usb/gadget/function/uvc_video.c
+++ b/drivers/usb/gadget/function/uvc_video.c
@@ -651,6 +651,11 @@
break;
}
+ /* Buffer payload size may predate the current request size. */
+ if (!is_bulk)
+ buf->req_payload_size = min(buf->req_payload_size,
+ video->req_size);
+
video->encode(req, video, buf);
spin_unlock_irqrestore(&queue->irqlock, flags);
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH] usb: gadget: uvc: cap isoc payload at request size
2026-12-01 7:27 [PATCH] usb: gadget: uvc: cap isoc payload at request size Keisuke Tsukuda
@ 2026-12-01 7:39 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-01 7:39 UTC (permalink / raw)
To: Keisuke Tsukuda; +Cc: linux-usb@vger.kernel.org
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] usb: gadget: uvc: min() clamp in uvcg_video_pump() leaves
payload 0 or stale
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [Critical] usb: gadget: uvc: division by zero in
uvc_video_prep_requests() [1]
--
commit eb52cb730fce5dcb9e4c868a1cd37f6e22f56c05
Author: Keisuke Tsukuda <tkdkei@outlook.jp>
usb: gadget: uvc: cap isoc payload at request size
This commit caps buf->req_payload_size at video->req_size for isochronous
transfers in uvcg_video_pump() before invoking video->encode().
> diff --git a/drivers/usb/gadget/function/uvc_video.c b/drivers/usb/gadget/function/uvc_video.c
> index 002afca9141ee..7d05c5f1f769e 100644
> --- a/drivers/usb/gadget/function/uvc_video.c
> +++ b/drivers/usb/gadget/function/uvc_video.c
> @@ -651,6 +651,11 @@ static void uvcg_video_pump(struct work_struct *work)
> break;
> }
>
> + /* Buffer payload size may predate the current request size. */
> + if (!is_bulk)
> + buf->req_payload_size = min(buf->req_payload_size,
> + video->req_size);
> +
> video->encode(req, video, buf);
>
> spin_unlock_irqrestore(&queue->irqlock, flags);
[Severity: High]
Can clamping downward with min() leave buf->req_payload_size at 0 or stuck at
a previous stream's smaller request size?
video->max_req_size is only initialized in uvc_function_set_alt() when the
host selects alternate setting 1. If userspace calls VIDIOC_QBUF before the
first SET_INTERFACE(alt=1), both video->reqs_per_frame and
video->max_req_size are 0, so uvc_buffer_prepare() sets
buf->req_payload_size = 0. Once streaming starts, min(0, video->req_size)
stays 0; uvc_video_encode_isoc() then subtracts the header length from
int len = 0, and uvc_video_encode_data() casts the negative len to unsigned
int in min_t(), copying the entire frame into the video->req_size request
buffer and overflowing the heap buffer (or underflowing unsigned int len in
uvc_video_encode_isoc_sg()).
Similarly, because uvcg_video_disable() does not reset video->reqs_per_frame
or video->req_size on VIDIOC_STREAMOFF, buffers queued before the next
VIDIOC_STREAMON are prepared against the old stream's parameters. If the new
stream uses a larger video->req_size, min() keeps buf->req_payload_size
clamped to the previous stream's smaller request size; and for compressed
frames queued before the first VIDIOC_STREAMON, min() bounds the payload to
video->req_size rather than dividing buf->bytesused across
video->reqs_per_frame.
Recomputing buf->req_payload_size from buf->bytesused, video->reqs_per_frame,
and video->req_size when encoding starts would handle both pre-stream
queueing and stream transitions.
[Severity: Critical]
This problem wasn't introduced by this patch, but can uvc_video_prep_requests()
divide by zero when computing the request size [1]?
uvc_v4l2_s_parm() writes the user-supplied frame interval directly into
video->interval without checking for zero. If userspace sets a zero interval
via VIDIOC_S_PARM and then calls VIDIOC_STREAMON, uvc_video_prep_requests()
computes nreq = 0 and divides by nreq:
drivers/usb/gadget/function/uvc_video.c:uvc_video_prep_requests() {
...
nreq = DIV_ROUND_UP(video->interval, interval_duration);
header_size = nreq * UVCG_REQUEST_HEADER_LEN;
req_size = DIV_ROUND_UP(video->imagesize + header_size, nreq);
...
}
This triggers a divide-by-zero oops in kernel mode.
[1] https://sashiko.dev/#/bug/linux-51fdd0db-93bb-4ef0-9a91-e82a361b6db7
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201072659.94982-1-tkdkei@outlook.jp?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-01 7:39 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 7:27 [PATCH] usb: gadget: uvc: cap isoc payload at request size Keisuke Tsukuda
2026-12-01 7:39 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox