Re: [PATCH 3/3] media: uvcvideo: Fix unaligned and OOB access

From: Ricardo Ribalda

Date: Mon Nov 16 2026 - 09:27:35 EST


Hi Laurent


On Mon, 16 Nov 2026 at 15:01, Laurent Pinchart
<laurent.pinchart@xxxxxxxxxxxxxxxx> 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@xxxxxxxxxxxxxxx
> > Signed-off-by: Ricardo Ribalda <ribalda@xxxxxxxxxxxx>
> > ---
> > 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