2018-06-05 00:25:09

by Nicolas Dufresne

[permalink] [raw]
Subject: [PATCH] uvcvideo: Also validate buffers in BULK mode

Just like for ISOC, validate the decoded BULK buffer size when possible.
This avoids sending corrupted or partial buffers to userspace, which may
lead to application crash or run-time failure.

Signed-off-by: Nicolas Dufresne <[email protected]>
---
drivers/media/usb/uvc/uvc_video.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
index aa0082fe5833..46df4d01e31b 100644
--- a/drivers/media/usb/uvc/uvc_video.c
+++ b/drivers/media/usb/uvc/uvc_video.c
@@ -1307,8 +1307,10 @@ static void uvc_video_decode_bulk(struct urb *urb, struct uvc_streaming *stream,
if (stream->bulk.header_size == 0 && !stream->bulk.skip_payload) {
do {
ret = uvc_video_decode_start(stream, buf, mem, len);
- if (ret == -EAGAIN)
+ if (ret == -EAGAIN) {
+ uvc_video_validate_buffer(stream, buf);
uvc_video_next_buffers(stream, &buf, &meta_buf);
+ }
} while (ret == -EAGAIN);

/* If an error occurred skip the rest of the payload. */
@@ -1342,8 +1344,10 @@ static void uvc_video_decode_bulk(struct urb *urb, struct uvc_streaming *stream,
if (!stream->bulk.skip_payload && buf != NULL) {
uvc_video_decode_end(stream, buf, stream->bulk.header,
stream->bulk.payload_size);
- if (buf->state == UVC_BUF_STATE_READY)
+ if (buf->state == UVC_BUF_STATE_READY) {
+ uvc_video_validate_buffer(stream, buf);
uvc_video_next_buffers(stream, &buf, &meta_buf);
+ }
}

stream->bulk.header_size = 0;
--
2.17.1



2018-06-05 08:53:02

by Laurent Pinchart

[permalink] [raw]
Subject: Re: [PATCH] uvcvideo: Also validate buffers in BULK mode

Hi Nicolas,

Thank you for the patch.

On Tuesday, 5 June 2018 03:24:15 EEST Nicolas Dufresne wrote:
> Just like for ISOC, validate the decoded BULK buffer size when possible.
> This avoids sending corrupted or partial buffers to userspace, which may
> lead to application crash or run-time failure.
>
> Signed-off-by: Nicolas Dufresne <[email protected]>
> ---
> drivers/media/usb/uvc/uvc_video.c | 8 ++++++--
> 1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/media/usb/uvc/uvc_video.c
> b/drivers/media/usb/uvc/uvc_video.c index aa0082fe5833..46df4d01e31b 100644
> --- a/drivers/media/usb/uvc/uvc_video.c
> +++ b/drivers/media/usb/uvc/uvc_video.c
> @@ -1307,8 +1307,10 @@ static void uvc_video_decode_bulk(struct urb *urb,
> struct uvc_streaming *stream, if (stream->bulk.header_size == 0 &&
> !stream->bulk.skip_payload) { do {
> ret = uvc_video_decode_start(stream, buf, mem, len);
> - if (ret == -EAGAIN)
> + if (ret == -EAGAIN) {
> + uvc_video_validate_buffer(stream, buf);
> uvc_video_next_buffers(stream, &buf, &meta_buf);

Wouldn't it be simpler to move the uvc_video_validate_buffer() call to
uvc_video_next_buffers() ?

> + }
> } while (ret == -EAGAIN);
>
> /* If an error occurred skip the rest of the payload. */
> @@ -1342,8 +1344,10 @@ static void uvc_video_decode_bulk(struct urb *urb,
> struct uvc_streaming *stream, if (!stream->bulk.skip_payload && buf !=
> NULL) {
> uvc_video_decode_end(stream, buf, stream->bulk.header,
> stream->bulk.payload_size);
> - if (buf->state == UVC_BUF_STATE_READY)
> + if (buf->state == UVC_BUF_STATE_READY) {
> + uvc_video_validate_buffer(stream, buf);
> uvc_video_next_buffers(stream, &buf, &meta_buf);
> + }
> }
>
> stream->bulk.header_size = 0;

--
Regards,

Laurent Pinchart




2018-06-05 14:08:29

by Nicolas Dufresne

[permalink] [raw]
Subject: Re: [PATCH] uvcvideo: Also validate buffers in BULK mode

Le mardi 05 juin 2018 à 11:52 +0300, Laurent Pinchart a écrit :
> Hi Nicolas,
>
> Thank you for the patch.
>
> On Tuesday, 5 June 2018 03:24:15 EEST Nicolas Dufresne wrote:
> > Just like for ISOC, validate the decoded BULK buffer size when possible.
> > This avoids sending corrupted or partial buffers to userspace, which may
> > lead to application crash or run-time failure.
> >
> > Signed-off-by: Nicolas Dufresne <[email protected]>
> > ---
> > drivers/media/usb/uvc/uvc_video.c | 8 ++++++--
> > 1 file changed, 6 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/media/usb/uvc/uvc_video.c
> > b/drivers/media/usb/uvc/uvc_video.c index aa0082fe5833..46df4d01e31b 100644
> > --- a/drivers/media/usb/uvc/uvc_video.c
> > +++ b/drivers/media/usb/uvc/uvc_video.c
> > @@ -1307,8 +1307,10 @@ static void uvc_video_decode_bulk(struct urb *urb,
> > struct uvc_streaming *stream, if (stream->bulk.header_size == 0 &&
> > !stream->bulk.skip_payload) { do {
> > ret = uvc_video_decode_start(stream, buf, mem, len);
> > - if (ret == -EAGAIN)
> > + if (ret == -EAGAIN) {
> > + uvc_video_validate_buffer(stream, buf);
> > uvc_video_next_buffers(stream, &buf, &meta_buf);
>
> Wouldn't it be simpler to move the uvc_video_validate_buffer() call to
> uvc_video_next_buffers() ?

Sounds like a good idea, it will prevent forgetting about it if this
code get extended.

>
> > + }
> > } while (ret == -EAGAIN);
> >
> > /* If an error occurred skip the rest of the payload. */
> > @@ -1342,8 +1344,10 @@ static void uvc_video_decode_bulk(struct urb *urb,
> > struct uvc_streaming *stream, if (!stream->bulk.skip_payload && buf !=
> > NULL) {
> > uvc_video_decode_end(stream, buf, stream->bulk.header,
> > stream->bulk.payload_size);
> > - if (buf->state == UVC_BUF_STATE_READY)
> > + if (buf->state == UVC_BUF_STATE_READY) {
> > + uvc_video_validate_buffer(stream, buf);
> > uvc_video_next_buffers(stream, &buf, &meta_buf);
> > + }
> > }
> >
> > stream->bulk.header_size = 0;
>
>


Attachments:
signature.asc (201.00 B)
This is a digitally signed message part

2018-06-05 23:46:56

by Nicolas Dufresne

[permalink] [raw]
Subject: [PATCH v2] uvcvideo: Also validate buffers in BULK mode

Just like for ISOC, validate the decoded BULK buffer size when possible.
This avoids sending corrupted or partial buffers to userspace, which may
lead to application crash or run-time failure.

Signed-off-by: Nicolas Dufresne <[email protected]>
---
drivers/media/usb/uvc/uvc_video.c | 9 +++------
1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
index aa0082fe5833..025ffac196f3 100644
--- a/drivers/media/usb/uvc/uvc_video.c
+++ b/drivers/media/usb/uvc/uvc_video.c
@@ -1234,6 +1234,7 @@ static void uvc_video_next_buffers(struct uvc_streaming *stream,
*meta_buf = uvc_queue_next_buffer(&stream->meta.queue,
*meta_buf);
}
+ uvc_video_validate_buffer(stream, *video_buf);
*video_buf = uvc_queue_next_buffer(&stream->queue, *video_buf);
}

@@ -1258,10 +1259,8 @@ static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
do {
ret = uvc_video_decode_start(stream, buf, mem,
urb->iso_frame_desc[i].actual_length);
- if (ret == -EAGAIN) {
- uvc_video_validate_buffer(stream, buf);
+ if (ret == -EAGAIN)
uvc_video_next_buffers(stream, &buf, &meta_buf);
- }
} while (ret == -EAGAIN);

if (ret < 0)
@@ -1277,10 +1276,8 @@ static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
uvc_video_decode_end(stream, buf, mem,
urb->iso_frame_desc[i].actual_length);

- if (buf->state == UVC_BUF_STATE_READY) {
- uvc_video_validate_buffer(stream, buf);
+ if (buf->state == UVC_BUF_STATE_READY)
uvc_video_next_buffers(stream, &buf, &meta_buf);
- }
}
}

--
2.17.1


2019-12-10 02:28:38

by Nicolas Dufresne

[permalink] [raw]
Subject: Re: [PATCH v2] uvcvideo: Also validate buffers in BULK mode

Le mardi 05 juin 2018 à 19:46 -0400, Nicolas Dufresne a écrit :
> Just like for ISOC, validate the decoded BULK buffer size when possible.
> This avoids sending corrupted or partial buffers to userspace, which may
> lead to application crash or run-time failure.
>
> Signed-off-by: Nicolas Dufresne <[email protected]>

Ping. That was a year and a half ago and still applies.

> ---
> drivers/media/usb/uvc/uvc_video.c | 9 +++------
> 1 file changed, 3 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
> index aa0082fe5833..025ffac196f3 100644
> --- a/drivers/media/usb/uvc/uvc_video.c
> +++ b/drivers/media/usb/uvc/uvc_video.c
> @@ -1234,6 +1234,7 @@ static void uvc_video_next_buffers(struct uvc_streaming *stream,
> *meta_buf = uvc_queue_next_buffer(&stream->meta.queue,
> *meta_buf);
> }
> + uvc_video_validate_buffer(stream, *video_buf);
> *video_buf = uvc_queue_next_buffer(&stream->queue, *video_buf);
> }
>
> @@ -1258,10 +1259,8 @@ static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
> do {
> ret = uvc_video_decode_start(stream, buf, mem,
> urb->iso_frame_desc[i].actual_length);
> - if (ret == -EAGAIN) {
> - uvc_video_validate_buffer(stream, buf);
> + if (ret == -EAGAIN)
> uvc_video_next_buffers(stream, &buf, &meta_buf);
> - }
> } while (ret == -EAGAIN);
>
> if (ret < 0)
> @@ -1277,10 +1276,8 @@ static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
> uvc_video_decode_end(stream, buf, mem,
> urb->iso_frame_desc[i].actual_length);
>
> - if (buf->state == UVC_BUF_STATE_READY) {
> - uvc_video_validate_buffer(stream, buf);
> + if (buf->state == UVC_BUF_STATE_READY)
> uvc_video_next_buffers(stream, &buf, &meta_buf);
> - }
> }
> }
>


Attachments:
signature.asc (201.00 B)
This is a digitally signed message part

2019-12-10 02:32:00

by Nicolas Dufresne

[permalink] [raw]
Subject: Re: [PATCH v2] uvcvideo: Also validate buffers in BULK mode

Le lundi 09 décembre 2019 à 21:27 -0500, Nicolas Dufresne a écrit :
> Le mardi 05 juin 2018 à 19:46 -0400, Nicolas Dufresne a écrit :
> > Just like for ISOC, validate the decoded BULK buffer size when possible.
> > This avoids sending corrupted or partial buffers to userspace, which may
> > lead to application crash or run-time failure.
> >
> > Signed-off-by: Nicolas Dufresne <[email protected]>
>
> Ping. That was a year and a half ago and still applies.

Please ignore, I was looking into the wrong branch by accident. Please,
update patchwork, that might also help to avoid confusion.

>
> > ---
> > drivers/media/usb/uvc/uvc_video.c | 9 +++------
> > 1 file changed, 3 insertions(+), 6 deletions(-)
> >
> > diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
> > index aa0082fe5833..025ffac196f3 100644
> > --- a/drivers/media/usb/uvc/uvc_video.c
> > +++ b/drivers/media/usb/uvc/uvc_video.c
> > @@ -1234,6 +1234,7 @@ static void uvc_video_next_buffers(struct uvc_streaming *stream,
> > *meta_buf = uvc_queue_next_buffer(&stream->meta.queue,
> > *meta_buf);
> > }
> > + uvc_video_validate_buffer(stream, *video_buf);
> > *video_buf = uvc_queue_next_buffer(&stream->queue, *video_buf);
> > }
> >
> > @@ -1258,10 +1259,8 @@ static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
> > do {
> > ret = uvc_video_decode_start(stream, buf, mem,
> > urb->iso_frame_desc[i].actual_length);
> > - if (ret == -EAGAIN) {
> > - uvc_video_validate_buffer(stream, buf);
> > + if (ret == -EAGAIN)
> > uvc_video_next_buffers(stream, &buf, &meta_buf);
> > - }
> > } while (ret == -EAGAIN);
> >
> > if (ret < 0)
> > @@ -1277,10 +1276,8 @@ static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
> > uvc_video_decode_end(stream, buf, mem,
> > urb->iso_frame_desc[i].actual_length);
> >
> > - if (buf->state == UVC_BUF_STATE_READY) {
> > - uvc_video_validate_buffer(stream, buf);
> > + if (buf->state == UVC_BUF_STATE_READY)
> > uvc_video_next_buffers(stream, &buf, &meta_buf);
> > - }
> > }
> > }
> >


Attachments:
signature.asc (201.00 B)
This is a digitally signed message part

2019-12-13 00:14:24

by Laurent Pinchart

[permalink] [raw]
Subject: Re: [PATCH v2] uvcvideo: Also validate buffers in BULK mode

Hi Nicolas,

On Mon, Dec 09, 2019 at 09:30:44PM -0500, Nicolas Dufresne wrote:
> Le lundi 09 décembre 2019 à 21:27 -0500, Nicolas Dufresne a écrit :
> > Le mardi 05 juin 2018 à 19:46 -0400, Nicolas Dufresne a écrit :
> > > Just like for ISOC, validate the decoded BULK buffer size when possible.
> > > This avoids sending corrupted or partial buffers to userspace, which may
> > > lead to application crash or run-time failure.
> > >
> > > Signed-off-by: Nicolas Dufresne <[email protected]>
> >
> > Ping. That was a year and a half ago and still applies.
>
> Please ignore, I was looking into the wrong branch by accident. Please,
> update patchwork, that might also help to avoid confusion.

Unless someone has changed the patch status in the last few days without
telling me, it was already marked as accepted.

> > > ---
> > > drivers/media/usb/uvc/uvc_video.c | 9 +++------
> > > 1 file changed, 3 insertions(+), 6 deletions(-)
> > >
> > > diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
> > > index aa0082fe5833..025ffac196f3 100644
> > > --- a/drivers/media/usb/uvc/uvc_video.c
> > > +++ b/drivers/media/usb/uvc/uvc_video.c
> > > @@ -1234,6 +1234,7 @@ static void uvc_video_next_buffers(struct uvc_streaming *stream,
> > > *meta_buf = uvc_queue_next_buffer(&stream->meta.queue,
> > > *meta_buf);
> > > }
> > > + uvc_video_validate_buffer(stream, *video_buf);
> > > *video_buf = uvc_queue_next_buffer(&stream->queue, *video_buf);
> > > }
> > >
> > > @@ -1258,10 +1259,8 @@ static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
> > > do {
> > > ret = uvc_video_decode_start(stream, buf, mem,
> > > urb->iso_frame_desc[i].actual_length);
> > > - if (ret == -EAGAIN) {
> > > - uvc_video_validate_buffer(stream, buf);
> > > + if (ret == -EAGAIN)
> > > uvc_video_next_buffers(stream, &buf, &meta_buf);
> > > - }
> > > } while (ret == -EAGAIN);
> > >
> > > if (ret < 0)
> > > @@ -1277,10 +1276,8 @@ static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
> > > uvc_video_decode_end(stream, buf, mem,
> > > urb->iso_frame_desc[i].actual_length);
> > >
> > > - if (buf->state == UVC_BUF_STATE_READY) {
> > > - uvc_video_validate_buffer(stream, buf);
> > > + if (buf->state == UVC_BUF_STATE_READY)
> > > uvc_video_next_buffers(stream, &buf, &meta_buf);
> > > - }
> > > }
> > > }
> > >

--
Regards,

Laurent Pinchart