[PATCH] media: uvcvideo: reject descriptor walks that cannot advance

From: Shirong Zhao

Date: Wed Nov 18 2026 - 00:47:01 EST


uvc_driver.c walks the extra descriptor data four times, advancing
the cursor by buffer[0] each round: the frame descriptor loop in
uvc_parse_format(), the skip loop and the counting pre-pass in
uvc_parse_streaming(), and the format descriptor loop after them.

buffer[0] is bLength from a descriptor the USB device supplied, and
nothing constrains it. A device that reports bLength 0 leaves both
buflen and buffer unchanged, so the loop condition is still true on the
next iteration and the same descriptor is parsed again forever, on the
enumeration path. A bLength above the remaining extra data walks the
cursor past the buffer instead.

Add uvc_desc_walkable(), which requires the two-byte descriptor minimum
and that the descriptor still fits in what is left, and use it in the
four loop conditions.

uvc_parse_control() is left alone: it already carries
"if (buflen < buffer[0] || buffer[0] < 3) return -EINVAL;" at the top of
its loop, which covers the same case there.

Found by static analysis of the descriptor loops; not observed at
runtime.
Signed-off-by: Shirong Zhao <shxzhaosr@xxxxxxx>
---
drivers/media/usb/uvc/uvc_driver.c | 25 ++++++++++++++++++++-----
1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c
index cbc1456..57437e1 100644
--- a/drivers/media/usb/uvc/uvc_driver.c
+++ b/drivers/media/usb/uvc/uvc_driver.c
@@ -332,6 +332,18 @@ static int uvc_parse_frame(struct uvc_device *dev,
return buffer[0];
}

+/*
+ * A descriptor's length is supplied by the device, and nothing constrains it
+ * before the walks below step the cursor by it. Below the two-byte
+ * descriptor minimum the cursor does not move at all and the walk spins on
+ * data the device chose; longer than the extra data still unparsed and the
+ * cursor would step outside that buffer.
+ */
+static bool uvc_desc_walkable(const unsigned char *buffer, int buflen)
+{
+ return buffer[0] >= 2 && buffer[0] <= buflen;
+}
+
static int uvc_parse_format(struct uvc_device *dev,
struct uvc_streaming *streaming, struct uvc_format *format,
struct uvc_frame *frames, u32 **intervals, const unsigned char *buffer,
@@ -486,8 +498,8 @@ static int uvc_parse_format(struct uvc_device *dev,
* based formats have frame descriptors.
*/
if (ftype) {
- while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
- buffer[2] == ftype) {
+ while (buflen > 2 && uvc_desc_walkable(buffer, buflen) &&
+ buffer[1] == USB_DT_CS_INTERFACE && buffer[2] == ftype) {
frame = &frames[format->nframes];
ret = uvc_parse_frame(dev, streaming, format, frame,
intervals, ftype, width_multiplier,
@@ -591,7 +603,8 @@ static int uvc_parse_streaming(struct uvc_device *dev,
}

/* Skip the standard interface descriptors. */
- while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
+ while (buflen > 2 && uvc_desc_walkable(buffer, buflen) &&
+ buffer[1] != USB_DT_CS_INTERFACE) {
buflen -= buffer[0];
buffer += buffer[0];
}
@@ -658,7 +671,8 @@ static int uvc_parse_streaming(struct uvc_device *dev,
_buflen = buflen;

/* Count the format and frame descriptors. */
- while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
+ while (_buflen > 2 && uvc_desc_walkable(_buffer, _buflen) &&
+ _buffer[1] == USB_DT_CS_INTERFACE) {
switch (_buffer[2]) {
case UVC_VS_FORMAT_UNCOMPRESSED:
case UVC_VS_FORMAT_MJPEG:
@@ -734,7 +748,8 @@ static int uvc_parse_streaming(struct uvc_device *dev,
streaming->nformats = 0;

/* Parse the format descriptors. */
- while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
+ while (buflen > 2 && uvc_desc_walkable(buffer, buflen) &&
+ buffer[1] == USB_DT_CS_INTERFACE) {
switch (buffer[2]) {
case UVC_VS_FORMAT_UNCOMPRESSED:
case UVC_VS_FORMAT_MJPEG: