* [PATCH 0/3] media: uvcvideo: Improve checks for medatata size
@ 2026-11-16 11:56 Ricardo Ribalda
2026-11-16 11:56 ` [PATCH 1/3] media: uvcvideo: Reject short metadata Ricardo Ribalda
` (2 more replies)
0 siblings, 3 replies; 10+ messages in thread
From: Ricardo Ribalda @ 2026-11-16 11:56 UTC (permalink / raw)
To: Laurent Pinchart, Hans de Goede, Mauro Carvalho Chehab,
Guennadi Liakhovetski
Cc: linux-media, linux-kernel, Ricardo Ribalda, Sinan Turunc, stable
In some parts of the code we have not been very strict when checking the
payload header size; fix this.
While doing that, I also spotted an unaligned and OOB access in a debug
message, so I added a fix for it to this series.
This work was triggered by:
https://lore.kernel.org/linux-media/20261115121701.189-1-enscrpt@pm.me
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
Ricardo Ribalda (3):
media: uvcvideo: Reject short metadata
media: uvcvideo: Check header length in uvc_video_clock_decode()
media: uvcvideo: Fix unaligned and OOB access
drivers/media/usb/uvc/uvc_video.c | 20 ++++++++++++++++----
drivers/media/usb/uvc/uvcvideo.h | 1 +
2 files changed, 17 insertions(+), 4 deletions(-)
---
base-commit: 48fe5a7834cc10081887d7bcd9ff0a4cadc3fa8c
change-id: 20261116-uvc-shortmeta-75ccb6707d9d
Best regards,
--
Ricardo Ribalda <ribalda@chromium.org>
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH 1/3] media: uvcvideo: Reject short metadata
2026-11-16 11:56 [PATCH 0/3] media: uvcvideo: Improve checks for medatata size Ricardo Ribalda
@ 2026-11-16 11:56 ` Ricardo Ribalda
2026-11-16 13:08 ` Laurent Pinchart
2026-11-16 11:56 ` [PATCH 2/3] media: uvcvideo: Check header length in uvc_video_clock_decode() Ricardo Ribalda
2026-11-16 11:56 ` [PATCH 3/3] media: uvcvideo: Fix unaligned and OOB access Ricardo Ribalda
2 siblings, 1 reply; 10+ messages in thread
From: Ricardo Ribalda @ 2026-11-16 11:56 UTC (permalink / raw)
To: Laurent Pinchart, Hans de Goede, Mauro Carvalho Chehab,
Guennadi Liakhovetski
Cc: linux-media, linux-kernel, Ricardo Ribalda, Sinan Turunc, stable
The UVC header has two bits saying whether the packet contains the
optional PTS or SCR fields.
If the header says that those fields are there, but the header is not
big enough to hold them, there is something wrong going on and we could
read out of bounds.
Mark the metadata as invalid and move on.
Reported-by: Sinan Turunc <enscrpt@pm.me>
Closes: https://lore.kernel.org/linux-media/20261115121701.189-1-enscrpt@pm.me/
Fixes: 088ead255245 ("media: uvcvideo: Add a metadata device node")
Cc: stable@vger.kernel.org
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
drivers/media/usb/uvc/uvc_video.c | 8 ++++++++
drivers/media/usb/uvc/uvcvideo.h | 1 +
2 files changed, 9 insertions(+)
diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
index a92a986ab714..9cc5d83acbc2 100644
--- a/drivers/media/usb/uvc/uvc_video.c
+++ b/drivers/media/usb/uvc/uvc_video.c
@@ -1487,6 +1487,14 @@ static void uvc_video_decode_meta(struct uvc_streaming *stream,
if (has_scr)
len_std += 6;
+ if (length < len_std) {
+ uvc_warn_once(stream->dev, UVC_WARN_META_SHORT,
+ "UVC non compliance: payload header length %u is shorter than expected %zu (flags 0x%02x).\n",
+ length, len_std, mem[1]);
+ meta_buf->error = 1;
+ return;
+ }
+
if (stream->meta.format == V4L2_META_FMT_UVC)
length = len_std;
diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h
index af8cef94ffff..59e23e55bc2e 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_META_SHORT 4
extern unsigned int uvc_clock_param;
extern unsigned int uvc_no_drop_param;
--
2.56.0.679.gc527c65306-goog
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH 2/3] media: uvcvideo: Check header length in uvc_video_clock_decode()
2026-11-16 11:56 [PATCH 0/3] media: uvcvideo: Improve checks for medatata size Ricardo Ribalda
2026-11-16 11:56 ` [PATCH 1/3] media: uvcvideo: Reject short metadata Ricardo Ribalda
@ 2026-11-16 11:56 ` Ricardo Ribalda
2026-11-16 13:09 ` Laurent Pinchart
2026-11-16 11:56 ` [PATCH 3/3] media: uvcvideo: Fix unaligned and OOB access Ricardo Ribalda
2 siblings, 1 reply; 10+ messages in thread
From: Ricardo Ribalda @ 2026-11-16 11:56 UTC (permalink / raw)
To: Laurent Pinchart, Hans de Goede, Mauro Carvalho Chehab,
Guennadi Liakhovetski
Cc: linux-media, linux-kernel, Ricardo Ribalda, stable
In UVC, the header size is defined in data[0]; all the bytes beyond that
length are pixels.
Currently, uvc_video_clock_decode() could parse pixels as PTS/SCR
because it checks the bounds against the USB packet size, not the header
size.
Fix it.
Fixes: 66847ef013cc ("[media] uvcvideo: Add UVC timestamps support")
Cc: stable@vger.kernel.org
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
drivers/media/usb/uvc/uvc_video.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
index 9cc5d83acbc2..e9be0a627033 100644
--- a/drivers/media/usb/uvc/uvc_video.c
+++ b/drivers/media/usb/uvc/uvc_video.c
@@ -582,8 +582,12 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf,
}
/* Check for invalid headers. */
- if (len < header_size)
+ if (len < header_size || data[0] < header_size) {
+ uvc_warn_once(stream->dev, UVC_WARN_META_SHORT,
+ "UVC non compliance: payload header length %u is shorter than expected %u (flags 0x%02x).\n",
+ data[0], header_size, data[1]);
return;
+ }
/*
* Extract the timestamps:
--
2.56.0.679.gc527c65306-goog
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH 3/3] media: uvcvideo: Fix unaligned and OOB access
2026-11-16 11:56 [PATCH 0/3] media: uvcvideo: Improve checks for medatata size Ricardo Ribalda
2026-11-16 11:56 ` [PATCH 1/3] media: uvcvideo: Reject short metadata Ricardo Ribalda
2026-11-16 11:56 ` [PATCH 2/3] media: uvcvideo: Check header length in uvc_video_clock_decode() Ricardo Ribalda
@ 2026-11-16 11:56 ` Ricardo Ribalda
2026-11-16 14:01 ` Laurent Pinchart
2 siblings, 1 reply; 10+ messages in thread
From: Ricardo Ribalda @ 2026-11-16 11:56 UTC (permalink / raw)
To: Laurent Pinchart, Hans de Goede, Mauro Carvalho Chehab,
Guennadi Liakhovetski
Cc: linux-media, linux-kernel, Ricardo Ribalda, stable
meta->buf and scr point to packed metadata and USB packet memory that do
not need to be aligned to anything. Also, scr + 4 is a 16-bit field, so
reading it as a u32 reads 2 bytes out of bounds.
We use *_unaligned() in the rest of uvc_video_decode_meta(), but not in
the debug print.
Fix this.
Fixes: 088ead255245 ("media: uvcvideo: Add a metadata device node")
Cc: stable@vger.kernel.org
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
drivers/media/usb/uvc/uvc_video.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
index e9be0a627033..3e3296ec5c65 100644
--- a/drivers/media/usb/uvc/uvc_video.c
+++ b/drivers/media/usb/uvc/uvc_video.c
@@ -1532,9 +1532,9 @@ static void uvc_video_decode_meta(struct uvc_streaming *stream,
"%s(): t-sys %lluns, SOF %u, len %u, flags 0x%x, PTS %u, STC %u frame SOF %u\n",
__func__, ktime_to_ns(time), meta->sof, meta->length,
meta->flags,
- has_pts ? *(u32 *)meta->buf : 0,
- has_scr ? *(u32 *)scr : 0,
- has_scr ? *(u32 *)(scr + 4) & 0x7ff : 0);
+ has_pts ? get_unaligned_le32(meta->buf) : 0,
+ has_scr ? get_unaligned_le32(scr) : 0,
+ has_scr ? get_unaligned_le16(scr + 4) & 0x7ff : 0);
}
/* ------------------------------------------------------------------------
--
2.56.0.679.gc527c65306-goog
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 1/3] media: uvcvideo: Reject short metadata
2026-11-16 11:56 ` [PATCH 1/3] media: uvcvideo: Reject short metadata Ricardo Ribalda
@ 2026-11-16 13:08 ` Laurent Pinchart
2026-11-16 13:23 ` Ricardo Ribalda
0 siblings, 1 reply; 10+ messages in thread
From: Laurent Pinchart @ 2026-11-16 13:08 UTC (permalink / raw)
To: Ricardo Ribalda
Cc: Hans de Goede, Mauro Carvalho Chehab, Guennadi Liakhovetski,
linux-media, linux-kernel, Sinan Turunc, stable
On Mon, Nov 16, 2026 at 11:56:37AM +0000, Ricardo Ribalda wrote:
> The UVC header has two bits saying whether the packet contains the
> optional PTS or SCR fields.
>
> If the header says that those fields are there, but the header is not
> big enough to hold them, there is something wrong going on and we could
> read out of bounds.
There are multiple checks scattered through the code for this already,
some of them causing the whole data packet to be dropped, others just
short-circuiting operations such as clock and stats updates. Did you ned
see those ? It's getting too messy.
> Mark the metadata as invalid and move on.
>
> Reported-by: Sinan Turunc <enscrpt@pm.me>
> Closes: https://lore.kernel.org/linux-media/20261115121701.189-1-enscrpt@pm.me/
> Fixes: 088ead255245 ("media: uvcvideo: Add a metadata device node")
> Cc: stable@vger.kernel.org
> Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> ---
> drivers/media/usb/uvc/uvc_video.c | 8 ++++++++
> drivers/media/usb/uvc/uvcvideo.h | 1 +
> 2 files changed, 9 insertions(+)
>
> diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
> index a92a986ab714..9cc5d83acbc2 100644
> --- a/drivers/media/usb/uvc/uvc_video.c
> +++ b/drivers/media/usb/uvc/uvc_video.c
> @@ -1487,6 +1487,14 @@ static void uvc_video_decode_meta(struct uvc_streaming *stream,
> if (has_scr)
> len_std += 6;
>
> + if (length < len_std) {
> + uvc_warn_once(stream->dev, UVC_WARN_META_SHORT,
> + "UVC non compliance: payload header length %u is shorter than expected %zu (flags 0x%02x).\n",
> + length, len_std, mem[1]);
> + meta_buf->error = 1;
> + return;
> + }
> +
> if (stream->meta.format == V4L2_META_FMT_UVC)
> length = len_std;
>
> diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h
> index af8cef94ffff..59e23e55bc2e 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_META_SHORT 4
>
> extern unsigned int uvc_clock_param;
> extern unsigned int uvc_no_drop_param;
--
Regards,
Laurent Pinchart
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 2/3] media: uvcvideo: Check header length in uvc_video_clock_decode()
2026-11-16 11:56 ` [PATCH 2/3] media: uvcvideo: Check header length in uvc_video_clock_decode() Ricardo Ribalda
@ 2026-11-16 13:09 ` Laurent Pinchart
0 siblings, 0 replies; 10+ messages in thread
From: Laurent Pinchart @ 2026-11-16 13:09 UTC (permalink / raw)
To: Ricardo Ribalda
Cc: Hans de Goede, Mauro Carvalho Chehab, Guennadi Liakhovetski,
linux-media, linux-kernel, stable
On Mon, Nov 16, 2026 at 11:56:38AM +0000, Ricardo Ribalda wrote:
> In UVC, the header size is defined in data[0]; all the bytes beyond that
> length are pixels.
>
> Currently, uvc_video_clock_decode() could parse pixels as PTS/SCR
> because it checks the bounds against the USB packet size, not the header
> size.
So you have seen those checks :-)
> Fix it.
>
> Fixes: 66847ef013cc ("[media] uvcvideo: Add UVC timestamps support")
> Cc: stable@vger.kernel.org
> Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> ---
> drivers/media/usb/uvc/uvc_video.c | 6 +++++-
> 1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
> index 9cc5d83acbc2..e9be0a627033 100644
> --- a/drivers/media/usb/uvc/uvc_video.c
> +++ b/drivers/media/usb/uvc/uvc_video.c
> @@ -582,8 +582,12 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf,
> }
>
> /* Check for invalid headers. */
> - if (len < header_size)
> + if (len < header_size || data[0] < header_size) {
> + uvc_warn_once(stream->dev, UVC_WARN_META_SHORT,
> + "UVC non compliance: payload header length %u is shorter than expected %u (flags 0x%02x).\n",
> + data[0], header_size, data[1]);
> return;
> + }
>
> /*
> * Extract the timestamps:
--
Regards,
Laurent Pinchart
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 1/3] media: uvcvideo: Reject short metadata
2026-11-16 13:08 ` Laurent Pinchart
@ 2026-11-16 13:23 ` Ricardo Ribalda
2026-11-16 14:06 ` Laurent Pinchart
0 siblings, 1 reply; 10+ messages in thread
From: Ricardo Ribalda @ 2026-11-16 13:23 UTC (permalink / raw)
To: Laurent Pinchart
Cc: Hans de Goede, Mauro Carvalho Chehab, Guennadi Liakhovetski,
linux-media, linux-kernel, Sinan Turunc, stable
Hi Laurent
On Mon, 16 Nov 2026 at 14:08, Laurent Pinchart
<laurent.pinchart@ideasonboard.com> wrote:
>
> On Mon, Nov 16, 2026 at 11:56:37AM +0000, Ricardo Ribalda wrote:
> > The UVC header has two bits saying whether the packet contains the
> > optional PTS or SCR fields.
> >
> > If the header says that those fields are there, but the header is not
> > big enough to hold them, there is something wrong going on and we could
> > read out of bounds.
>
> There are multiple checks scattered through the code for this already,
> some of them causing the whole data packet to be dropped, others just
> short-circuiting operations such as clock and stats updates. Did you ned
> see those ? It's getting too messy.
Yes I saw them :P.
I can move the tests to uvc_video_decode_start(). The question is what
we should do for non compliant cameras.
I would love to just drop the packet altogether (with a non compliance
warning), and if someone complains... we can add a quirk or some
heuristics.
Will send a v2
>
> > Mark the metadata as invalid and move on.
> >
> > Reported-by: Sinan Turunc <enscrpt@pm.me>
> > Closes: https://lore.kernel.org/linux-media/20261115121701.189-1-enscrpt@pm.me/
> > Fixes: 088ead255245 ("media: uvcvideo: Add a metadata device node")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> > ---
> > drivers/media/usb/uvc/uvc_video.c | 8 ++++++++
> > drivers/media/usb/uvc/uvcvideo.h | 1 +
> > 2 files changed, 9 insertions(+)
> >
> > diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
> > index a92a986ab714..9cc5d83acbc2 100644
> > --- a/drivers/media/usb/uvc/uvc_video.c
> > +++ b/drivers/media/usb/uvc/uvc_video.c
> > @@ -1487,6 +1487,14 @@ static void uvc_video_decode_meta(struct uvc_streaming *stream,
> > if (has_scr)
> > len_std += 6;
> >
> > + if (length < len_std) {
> > + uvc_warn_once(stream->dev, UVC_WARN_META_SHORT,
> > + "UVC non compliance: payload header length %u is shorter than expected %zu (flags 0x%02x).\n",
> > + length, len_std, mem[1]);
> > + meta_buf->error = 1;
> > + return;
> > + }
> > +
> > if (stream->meta.format == V4L2_META_FMT_UVC)
> > length = len_std;
> >
> > diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h
> > index af8cef94ffff..59e23e55bc2e 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_META_SHORT 4
> >
> > extern unsigned int uvc_clock_param;
> > extern unsigned int uvc_no_drop_param;
>
> --
> Regards,
>
> Laurent Pinchart
--
Ricardo Ribalda
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 3/3] media: uvcvideo: Fix unaligned and OOB access
2026-11-16 11:56 ` [PATCH 3/3] media: uvcvideo: Fix unaligned and OOB access Ricardo Ribalda
@ 2026-11-16 14:01 ` Laurent Pinchart
2026-11-16 14:27 ` Ricardo Ribalda
0 siblings, 1 reply; 10+ messages in thread
From: Laurent Pinchart @ 2026-11-16 14:01 UTC (permalink / raw)
To: Ricardo Ribalda
Cc: Hans de Goede, Mauro Carvalho Chehab, Guennadi Liakhovetski,
linux-media, linux-kernel, stable
On Mon, Nov 16, 2026 at 11:56:39AM +0000, Ricardo Ribalda wrote:
> meta->buf and scr point to packed metadata and USB packet memory that do
> not need to be aligned to anything. Also, scr + 4 is a 16-bit field, so
> reading it as a u32 reads 2 bytes out of bounds.
>
> We use *_unaligned() in the rest of uvc_video_decode_meta(), but not in
> the debug print.
>
> Fix this.
>
> Fixes: 088ead255245 ("media: uvcvideo: Add a metadata device node")
> Cc: stable@vger.kernel.org
> Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> ---
> drivers/media/usb/uvc/uvc_video.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
> index e9be0a627033..3e3296ec5c65 100644
> --- a/drivers/media/usb/uvc/uvc_video.c
> +++ b/drivers/media/usb/uvc/uvc_video.c
> @@ -1532,9 +1532,9 @@ static void uvc_video_decode_meta(struct uvc_streaming *stream,
> "%s(): t-sys %lluns, SOF %u, len %u, flags 0x%x, PTS %u, STC %u frame SOF %u\n",
> __func__, ktime_to_ns(time), meta->sof, meta->length,
> meta->flags,
> - has_pts ? *(u32 *)meta->buf : 0,
> - has_scr ? *(u32 *)scr : 0,
> - has_scr ? *(u32 *)(scr + 4) & 0x7ff : 0);
> + has_pts ? get_unaligned_le32(meta->buf) : 0,
> + has_scr ? get_unaligned_le32(scr) : 0,
> + has_scr ? get_unaligned_le16(scr + 4) & 0x7ff : 0);
I wonder if things would look cleaner if we introduced a struct uvc_scr
instead of fiddling with manual access to buffer offsets. What do you
think N It would look like this:
diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
index ce99795cfea4..30c6516f0340 100644
--- a/drivers/media/usb/uvc/uvc_video.c
+++ b/drivers/media/usb/uvc/uvc_video.c
@@ -943,8 +943,7 @@ static void uvc_video_stats_decode(struct uvc_streaming *stream,
unsigned int header_size;
bool has_pts = false;
bool has_scr = false;
- u16 scr_sof;
- u32 scr_stc;
+ struct uvc_scr scr;
u32 pts;
if (stream->stats.stream.nb_frames == 0 &&
@@ -981,8 +980,8 @@ static void uvc_video_stats_decode(struct uvc_streaming *stream,
pts = get_unaligned_le32(&data[2]);
if (has_scr) {
- scr_stc = get_unaligned_le32(&data[header_size - 6]);
- scr_sof = get_unaligned_le16(&data[header_size - 2]);
+ scr.stc = get_unaligned_le32(&data[header_size - 6]);
+ scr.sof = get_unaligned_le16(&data[header_size - 2]);
}
/* Is PTS constant through the whole frame ? */
@@ -1012,7 +1011,7 @@ static void uvc_video_stats_decode(struct uvc_streaming *stream,
/* Do the SCR.STC and SCR.SOF fields vary through the frame ? */
if (has_scr && stream->stats.frame.nb_scr) {
- if (stream->stats.frame.scr_stc != scr_stc)
+ if (stream->stats.frame.scr.stc != scr.stc)
stream->stats.frame.nb_scr_diffs++;
}
@@ -1021,17 +1020,16 @@ static void uvc_video_stats_decode(struct uvc_streaming *stream,
if (stream->stats.stream.nb_frames > 0 ||
stream->stats.frame.nb_scr > 0)
stream->stats.stream.scr_sof_count +=
- (scr_sof - stream->stats.stream.scr_sof) % 2048;
- stream->stats.stream.scr_sof = scr_sof;
+ (scr.sof - stream->stats.stream.scr_sof) % 2048;
+ stream->stats.stream.scr_sof = scr.sof;
stream->stats.frame.nb_scr++;
- stream->stats.frame.scr_stc = scr_stc;
- stream->stats.frame.scr_sof = scr_sof;
+ stream->stats.frame.scr = scr;
- if (scr_sof < stream->stats.stream.min_sof)
- stream->stats.stream.min_sof = scr_sof;
- if (scr_sof > stream->stats.stream.max_sof)
- stream->stats.stream.max_sof = scr_sof;
+ if (scr.sof < stream->stats.stream.min_sof)
+ stream->stats.stream.min_sof = scr.sof;
+ if (scr.sof > stream->stats.stream.max_sof)
+ stream->stats.stream.max_sof = scr.sof;
}
/* Record the first non-empty packet number. */
@@ -1062,7 +1060,7 @@ static void uvc_video_stats_update(struct uvc_streaming *stream)
frame->has_early_pts ? "" : "!",
frame->has_initial_pts ? "" : "!",
frame->nb_scr_diffs, frame->nb_scr,
- frame->pts, frame->scr_stc, frame->scr_sof);
+ frame->pts, frame->scr.stc, frame->scr.sof);
stream->stats.stream.nb_frames++;
stream->stats.stream.nb_packets += stream->stats.frame.nb_packets;
@@ -1468,8 +1466,9 @@ static void uvc_video_decode_meta(struct uvc_streaming *stream,
bool has_pts, has_scr;
unsigned long flags;
unsigned int sof;
+ struct uvc_scr scr;
ktime_t time;
- const u8 *scr;
+ const u8 *scr_data;
if (length <= 2 || !meta_buf || meta_buf->state != UVC_BUF_STATE_ACTIVE)
return;
@@ -1479,9 +1478,9 @@ static void uvc_video_decode_meta(struct uvc_streaming *stream,
if (has_pts) {
len_std += 4;
- scr = mem + 6;
+ scr_data = mem + 6;
} else {
- scr = mem + 2;
+ scr_data = mem + 2;
}
if (has_scr)
@@ -1490,8 +1489,13 @@ static void uvc_video_decode_meta(struct uvc_streaming *stream,
if (stream->meta.format == V4L2_META_FMT_UVC)
length = len_std;
+ if (has_scr) {
+ scr.stc = get_unaligned_le32(scr_data);
+ scr.sof = get_unaligned_le16(scr_data + 4);
+ }
+
if (length == len_std && (!has_scr ||
- !memcmp(scr, stream->clock.last_scr, 6)))
+ !memcmp(&scr, &stream->clock.last_scr, sizeof(scr))))
return;
if (meta_buf->length - meta_buf->bytesused <
@@ -1509,7 +1513,7 @@ static void uvc_video_decode_meta(struct uvc_streaming *stream,
put_unaligned(sof, &meta->sof);
if (has_scr)
- memcpy(stream->clock.last_scr, scr, 6);
+ stream->clock.last_scr = scr;
meta->length = mem[0];
meta->flags = mem[1];
@@ -1521,8 +1525,8 @@ static void uvc_video_decode_meta(struct uvc_streaming *stream,
__func__, ktime_to_ns(time), meta->sof, meta->length,
meta->flags,
has_pts ? *(u32 *)meta->buf : 0,
- has_scr ? *(u32 *)scr : 0,
- has_scr ? *(u32 *)(scr + 4) & 0x7ff : 0);
+ has_scr ? scr.stc : 0,
+ has_scr ? scr.sof & 0x7ff : 0);
}
/* ------------------------------------------------------------------------
diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h
index af8cef94ffff..a3e1c0fb129f 100644
--- a/drivers/media/usb/uvc/uvcvideo.h
+++ b/drivers/media/usb/uvc/uvcvideo.h
@@ -302,6 +302,11 @@ struct uvc_streaming_header {
u8 bTriggerUsage;
};
+struct uvc_scr {
+ u32 stc;
+ u16 sof;
+} __packed;
+
enum uvc_buffer_state {
UVC_BUF_STATE_IDLE = 0,
UVC_BUF_STATE_QUEUED = 1,
@@ -383,8 +388,7 @@ struct uvc_stats_frame {
unsigned int nb_scr; /* Number of packets with a SCR timestamp */
unsigned int nb_scr_diffs; /* Number of SCR.STC differences inside a frame */
- u16 scr_sof; /* SCR.SOF of the last packet */
- u32 scr_stc; /* SCR.STC of the last packet */
+ struct uvc_scr scr; /* SCR of the last packet */
};
struct uvc_stats_stream {
@@ -525,7 +529,7 @@ struct uvc_streaming {
u16 last_sof_raw;
u16 sof_offset;
- u8 last_scr[6];
+ struct uvc_scr last_scr;
spinlock_t lock;
} clock;
Actually, thinking more about this, I think we can also simplify
uvc_video_decode_meta() to only compare SCR.STC:
diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
index 30c6516f0340..7513083fadad 100644
--- a/drivers/media/usb/uvc/uvc_video.c
+++ b/drivers/media/usb/uvc/uvc_video.c
@@ -1454,8 +1454,8 @@ static int uvc_video_encode_data(struct uvc_streaming *stream,
*
* Ideally we want to capture all payload headers for each frame. However, their
* number is unknown and unbound. We thus drop headers that contain no vendor
- * data and that either contain no SCR value or an SCR value identical to the
- * previous header.
+ * data and that either contain no SCR value or an SCR.STC value identical to
+ * the previous header.
*/
static void uvc_video_decode_meta(struct uvc_streaming *stream,
struct uvc_buffer *meta_buf,
@@ -1494,8 +1494,7 @@ static void uvc_video_decode_meta(struct uvc_streaming *stream,
scr.sof = get_unaligned_le16(scr_data + 4);
}
- if (length == len_std && (!has_scr ||
- !memcmp(&scr, &stream->clock.last_scr, sizeof(scr))))
+ if (length == len_std && (!has_scr || scr.stc == stream->clock.last_stc))
return;
if (meta_buf->length - meta_buf->bytesused <
@@ -1513,7 +1512,7 @@ static void uvc_video_decode_meta(struct uvc_streaming *stream,
put_unaligned(sof, &meta->sof);
if (has_scr)
- stream->clock.last_scr = scr;
+ stream->clock.last_stc = scr.stc;
meta->length = mem[0];
meta->flags = mem[1];
diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h
index a3e1c0fb129f..b2d39c742e34 100644
--- a/drivers/media/usb/uvc/uvcvideo.h
+++ b/drivers/media/usb/uvc/uvcvideo.h
@@ -529,7 +529,7 @@ struct uvc_streaming {
u16 last_sof_raw;
u16 sof_offset;
- struct uvc_scr last_scr;
+ u32 last_stc;
spinlock_t lock;
} clock;
I can send patches with proper commit messages.
> }
>
> /* ------------------------------------------------------------------------
--
Regards,
Laurent Pinchart
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 1/3] media: uvcvideo: Reject short metadata
2026-11-16 13:23 ` Ricardo Ribalda
@ 2026-11-16 14:06 ` Laurent Pinchart
0 siblings, 0 replies; 10+ messages in thread
From: Laurent Pinchart @ 2026-11-16 14:06 UTC (permalink / raw)
To: Ricardo Ribalda
Cc: Hans de Goede, Mauro Carvalho Chehab, Guennadi Liakhovetski,
linux-media, linux-kernel, Sinan Turunc, stable
On Mon, Nov 16, 2026 at 02:23:34PM +0100, Ricardo Ribalda wrote:
> On Mon, 16 Nov 2026 at 14:08, Laurent Pinchart wrote:
> > On Mon, Nov 16, 2026 at 11:56:37AM +0000, Ricardo Ribalda wrote:
> > > The UVC header has two bits saying whether the packet contains the
> > > optional PTS or SCR fields.
> > >
> > > If the header says that those fields are there, but the header is not
> > > big enough to hold them, there is something wrong going on and we could
> > > read out of bounds.
> >
> > There are multiple checks scattered through the code for this already,
> > some of them causing the whole data packet to be dropped, others just
> > short-circuiting operations such as clock and stats updates. Did you ned
> > see those ? It's getting too messy.
>
> Yes I saw them :P.
>
> I can move the tests to uvc_video_decode_start(). The question is what
> we should do for non compliant cameras.
>
> I would love to just drop the packet altogether (with a non compliance
> warning), and if someone complains... we can add a quirk or some
> heuristics.
It's always the same issue with UVC, right ?
There are only two options: dropping the packet, or working around the
problem. The first option risks breaking some cameras. I would hope the
probability would be very close to zero, but I really have no idea.
If we work around the problem, I would centralize the logic that
computes the standard header size, extracts the PTS and SCR, and
validates the buffer size to a separate function. The checks will still
be run multiple times, but the logic will be centralized.
> Will send a v2
>
> > > Mark the metadata as invalid and move on.
> > >
> > > Reported-by: Sinan Turunc <enscrpt@pm.me>
> > > Closes: https://lore.kernel.org/linux-media/20261115121701.189-1-enscrpt@pm.me/
> > > Fixes: 088ead255245 ("media: uvcvideo: Add a metadata device node")
> > > Cc: stable@vger.kernel.org
> > > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> > > ---
> > > drivers/media/usb/uvc/uvc_video.c | 8 ++++++++
> > > drivers/media/usb/uvc/uvcvideo.h | 1 +
> > > 2 files changed, 9 insertions(+)
> > >
> > > diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
> > > index a92a986ab714..9cc5d83acbc2 100644
> > > --- a/drivers/media/usb/uvc/uvc_video.c
> > > +++ b/drivers/media/usb/uvc/uvc_video.c
> > > @@ -1487,6 +1487,14 @@ static void uvc_video_decode_meta(struct uvc_streaming *stream,
> > > if (has_scr)
> > > len_std += 6;
> > >
> > > + if (length < len_std) {
> > > + uvc_warn_once(stream->dev, UVC_WARN_META_SHORT,
> > > + "UVC non compliance: payload header length %u is shorter than expected %zu (flags 0x%02x).\n",
> > > + length, len_std, mem[1]);
> > > + meta_buf->error = 1;
> > > + return;
> > > + }
> > > +
> > > if (stream->meta.format == V4L2_META_FMT_UVC)
> > > length = len_std;
> > >
> > > diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h
> > > index af8cef94ffff..59e23e55bc2e 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_META_SHORT 4
> > >
> > > extern unsigned int uvc_clock_param;
> > > extern unsigned int uvc_no_drop_param;
--
Regards,
Laurent Pinchart
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 3/3] media: uvcvideo: Fix unaligned and OOB access
2026-11-16 14:01 ` Laurent Pinchart
@ 2026-11-16 14:27 ` Ricardo Ribalda
0 siblings, 0 replies; 10+ messages in thread
From: Ricardo Ribalda @ 2026-11-16 14:27 UTC (permalink / raw)
To: Laurent Pinchart
Cc: Hans de Goede, Mauro Carvalho Chehab, Guennadi Liakhovetski,
linux-media, linux-kernel, stable
Hi Laurent
On Mon, 16 Nov 2026 at 15:01, Laurent Pinchart
<laurent.pinchart@ideasonboard.com> wrote:
>
> On Mon, Nov 16, 2026 at 11:56:39AM +0000, Ricardo Ribalda wrote:
> > meta->buf and scr point to packed metadata and USB packet memory that do
> > not need to be aligned to anything. Also, scr + 4 is a 16-bit field, so
> > reading it as a u32 reads 2 bytes out of bounds.
> >
> > We use *_unaligned() in the rest of uvc_video_decode_meta(), but not in
> > the debug print.
> >
> > Fix this.
> >
> > Fixes: 088ead255245 ("media: uvcvideo: Add a metadata device node")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> > ---
> > drivers/media/usb/uvc/uvc_video.c | 6 +++---
> > 1 file changed, 3 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
> > index e9be0a627033..3e3296ec5c65 100644
> > --- a/drivers/media/usb/uvc/uvc_video.c
> > +++ b/drivers/media/usb/uvc/uvc_video.c
> > @@ -1532,9 +1532,9 @@ static void uvc_video_decode_meta(struct uvc_streaming *stream,
> > "%s(): t-sys %lluns, SOF %u, len %u, flags 0x%x, PTS %u, STC %u frame SOF %u\n",
> > __func__, ktime_to_ns(time), meta->sof, meta->length,
> > meta->flags,
> > - has_pts ? *(u32 *)meta->buf : 0,
> > - has_scr ? *(u32 *)scr : 0,
> > - has_scr ? *(u32 *)(scr + 4) & 0x7ff : 0);
> > + has_pts ? get_unaligned_le32(meta->buf) : 0,
> > + has_scr ? get_unaligned_le32(scr) : 0,
> > + has_scr ? get_unaligned_le16(scr + 4) & 0x7ff : 0);
>
> I wonder if things would look cleaner if we introduced a struct uvc_scr
> instead of fiddling with manual access to buffer offsets. What do you
> think N It would look like this:
I do not see a big point in introducing the struct: we still do the
manual offset math and get_unaligned_le*() in the callers (and meta->buf
is still unaligned), and marking an in-memory struct __packed just
makes slower for no reason. I think it would make more sense to have a
helper like uvc_video_parse_stc_sof_pts(*data, *stc, *sof, *pts) that
processes the header for clock, stats, and meta.
Comparing only SCR.STC in uvc_video_decode_meta() does look nicer
though.
Let me give it a try, and if I think that it looks nicer I will send a
v3 with it; otherwise I will send a version of your proposal with my own
added personal and funny bugs.
> --
> Regards,
>
> Laurent Pinchart
--
Ricardo Ribalda
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2026-11-16 14:27 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16 11:56 [PATCH 0/3] media: uvcvideo: Improve checks for medatata size Ricardo Ribalda
2026-11-16 11:56 ` [PATCH 1/3] media: uvcvideo: Reject short metadata Ricardo Ribalda
2026-11-16 13:08 ` Laurent Pinchart
2026-11-16 13:23 ` Ricardo Ribalda
2026-11-16 14:06 ` Laurent Pinchart
2026-11-16 11:56 ` [PATCH 2/3] media: uvcvideo: Check header length in uvc_video_clock_decode() Ricardo Ribalda
2026-11-16 13:09 ` Laurent Pinchart
2026-11-16 11:56 ` [PATCH 3/3] media: uvcvideo: Fix unaligned and OOB access Ricardo Ribalda
2026-11-16 14:01 ` Laurent Pinchart
2026-11-16 14:27 ` Ricardo Ribalda
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®