[PATCH v2 1/6] media: uvcvideo: Reject payloads with a short header
From: Ricardo Ribalda
Date: Mon Nov 16 2026 - 12:53:00 EST
The UVC header starts with a byte containing the length of the header
itself, and then a byte indicating which optional fields are present in
the header.
Currently, uvc_video_stats_decode() checks that the header is big enough
to contain the optional fields, uvc_video_clock_decode() only checks the
packet size (allowing payload data to be read as timestamps), and
uvc_video_decode_meta() fails to check it altogether (leading to
out-of-bounds reads).
Instead of adding the same check again and again, check the header
earlier in uvc_video_decode_start(), dropping non-compliant packets with
a warning.
Note that this changes the behaviour for non-compliant devices: until
now, the payload data of such packets was still copied into the video
buffer, now it is dropped together with the header. On bulk endpoints,
the rest of the payload is skipped as well.
Reported-by: Sinan Turunc <enscrpt@xxxxx>
Closes: https://lore.kernel.org/linux-media/20261115121701.189-1-enscrpt@xxxxx/
Fixes: 66847ef013cc ("[media] uvcvideo: Add UVC timestamps support")
Fixes: 088ead255245 ("media: uvcvideo: Add a metadata device node")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Ricardo Ribalda <ribalda@xxxxxxxxxxxx>
---
drivers/media/usb/uvc/uvc_video.c | 24 +++++++++++++++++++++++-
drivers/media/usb/uvc/uvcvideo.h | 1 +
2 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
index a92a986ab714..e801874e272e 100644
--- a/drivers/media/usb/uvc/uvc_video.c
+++ b/drivers/media/usb/uvc/uvc_video.c
@@ -553,6 +553,18 @@ static inline u16 sof_diff(u16 a, u16 b)
return (a - b) & 2047;
}
+static unsigned int uvc_video_min_header_len(u8 header_info)
+{
+ unsigned int min_header_len = 2;
+
+ if (header_info & UVC_STREAM_PTS)
+ min_header_len += 4;
+ if (header_info & UVC_STREAM_SCR)
+ min_header_len += 6;
+
+ return min_header_len;
+}
+
static void
uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf,
const u8 *data, int len)
@@ -1192,7 +1204,9 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,
* Sanity checks:
* - packet must be at least 2 bytes long
* - bHeaderLength value must be at least 2 bytes (see above)
- * - bHeaderLength value can't be larger than the packet size.
+ * - bHeaderLength value can't be larger than the packet size
+ * - bHeaderLength value must be large enough to contain the PTS and
+ * SCR fields signalled in bmHeaderInfo.
*/
if (len < 2 || data[0] < 2 || data[0] > len) {
stream->stats.frame.nb_invalid++;
@@ -1200,6 +1214,14 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,
}
header_len = data[0];
+ if (header_len < uvc_video_min_header_len(data[1])) {
+ uvc_warn_once(stream->dev, UVC_WARN_SHORT_HEADER,
+ "UVC non compliance: payload header length %u is shorter than expected (flags 0x%02x), dropping packet.\n",
+ header_len, data[1]);
+ stream->stats.frame.nb_invalid++;
+ return -EINVAL;
+ }
+
fid = data[1] & UVC_STREAM_FID;
/*
diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h
index af8cef94ffff..278c7b0e535c 100644
--- a/drivers/media/usb/uvc/uvcvideo.h
+++ b/drivers/media/usb/uvc/uvcvideo.h
@@ -661,6 +661,7 @@ static inline struct uvc_fh *to_uvc_fh(struct file *filp)
#define UVC_WARN_PROBE_DEF 1
#define UVC_WARN_XU_GET_RES 2
#define UVC_WARN_QUERY_CTRL 3
+#define UVC_WARN_SHORT_HEADER 4
extern unsigned int uvc_clock_param;
extern unsigned int uvc_no_drop_param;
--
2.56.0.679.gc527c65306-goog