diff mbox series

[v2] usb: gadget: uvc: Fix ERR_PTR dereference in uvc_v4l2.c

Message ID 20240815071416.585559-1-abhishektamboli9@gmail.com (mailing list archive)
State Superseded
Headers show
Series [v2] usb: gadget: uvc: Fix ERR_PTR dereference in uvc_v4l2.c | expand

Commit Message

Abhishek Tamboli Aug. 15, 2024, 7:14 a.m. UTC
Fix potential dereferencing of ERR_PTR() in find_format_by_pix()
and uvc_v4l2_enum_format().

Fix the following smatch errors:

drivers/usb/gadget/function/uvc_v4l2.c:124 find_format_by_pix()
error: 'fmtdesc' dereferencing possible ERR_PTR()

drivers/usb/gadget/function/uvc_v4l2.c:392 uvc_v4l2_enum_format()
error: 'fmtdesc' dereferencing possible ERR_PTR()

Also, fix similar issue in uvc_v4l2_try_format() for potential
dereferencing of ERR_PTR().

Fixes: 588b9e85609b ("usb: gadget: uvc: add v4l2 enumeration api calls")
Fixes: e219a712bc06 ("usb: gadget: uvc: add v4l2 try_format api call")
Signed-off-by: Abhishek Tamboli <abhishektamboli9@gmail.com>
---
Changes in v2:
- Add check for dereferencing of ERR_PTR() in uvc_v4l2_try_format()

 drivers/usb/gadget/function/uvc_v4l2.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

--
2.34.1

Comments

Greg Kroah-Hartman Aug. 15, 2024, 7:59 a.m. UTC | #1
On Thu, Aug 15, 2024 at 12:44:16PM +0530, Abhishek Tamboli wrote:
> Fix potential dereferencing of ERR_PTR() in find_format_by_pix()
> and uvc_v4l2_enum_format().
> 
> Fix the following smatch errors:
> 
> drivers/usb/gadget/function/uvc_v4l2.c:124 find_format_by_pix()
> error: 'fmtdesc' dereferencing possible ERR_PTR()
> 
> drivers/usb/gadget/function/uvc_v4l2.c:392 uvc_v4l2_enum_format()
> error: 'fmtdesc' dereferencing possible ERR_PTR()
> 
> Also, fix similar issue in uvc_v4l2_try_format() for potential
> dereferencing of ERR_PTR().
> 
> Fixes: 588b9e85609b ("usb: gadget: uvc: add v4l2 enumeration api calls")
> Fixes: e219a712bc06 ("usb: gadget: uvc: add v4l2 try_format api call")
> Signed-off-by: Abhishek Tamboli <abhishektamboli9@gmail.com>
> ---
> Changes in v2:
> - Add check for dereferencing of ERR_PTR() in uvc_v4l2_try_format()

You sent 2 copies of this, which is the correct one?

thanks,

greg k-h
Greg Kroah-Hartman Aug. 15, 2024, 7:59 a.m. UTC | #2
On Thu, Aug 15, 2024 at 12:44:16PM +0530, Abhishek Tamboli wrote:
> Fix potential dereferencing of ERR_PTR() in find_format_by_pix()
> and uvc_v4l2_enum_format().
> 
> Fix the following smatch errors:
> 
> drivers/usb/gadget/function/uvc_v4l2.c:124 find_format_by_pix()
> error: 'fmtdesc' dereferencing possible ERR_PTR()
> 
> drivers/usb/gadget/function/uvc_v4l2.c:392 uvc_v4l2_enum_format()
> error: 'fmtdesc' dereferencing possible ERR_PTR()
> 
> Also, fix similar issue in uvc_v4l2_try_format() for potential
> dereferencing of ERR_PTR().
> 
> Fixes: 588b9e85609b ("usb: gadget: uvc: add v4l2 enumeration api calls")
> Fixes: e219a712bc06 ("usb: gadget: uvc: add v4l2 try_format api call")
> Signed-off-by: Abhishek Tamboli <abhishektamboli9@gmail.com>
> ---
> Changes in v2:
> - Add check for dereferencing of ERR_PTR() in uvc_v4l2_try_format()
> 
>  drivers/usb/gadget/function/uvc_v4l2.c | 12 +++++++++++-
>  1 file changed, 11 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/gadget/function/uvc_v4l2.c b/drivers/usb/gadget/function/uvc_v4l2.c
> index a024aecb76dc..8bb88c864b60 100644
> --- a/drivers/usb/gadget/function/uvc_v4l2.c
> +++ b/drivers/usb/gadget/function/uvc_v4l2.c
> @@ -121,6 +121,9 @@ static struct uvcg_format *find_format_by_pix(struct uvc_device *uvc,
>  	list_for_each_entry(format, &uvc->header->formats, entry) {
>  		const struct uvc_format_desc *fmtdesc = to_uvc_format(format->fmt);
> 
> +		if (IS_ERR(fmtdesc))
> +			continue;
> +
>  		if (fmtdesc->fcc == pixelformat) {
>  			uformat = format->fmt;
>  			break;
> @@ -240,6 +243,7 @@ uvc_v4l2_try_format(struct file *file, void *fh, struct v4l2_format *fmt)
>  	struct uvc_video *video = &uvc->video;
>  	struct uvcg_format *uformat;
>  	struct uvcg_frame *uframe;
> +	const struct uvc_format_desc *fmtdesc;
>  	u8 *fcc;
> 
>  	if (fmt->type != video->queue.queue.type)
> @@ -277,7 +281,10 @@ uvc_v4l2_try_format(struct file *file, void *fh, struct v4l2_format *fmt)
>  		fmt->fmt.pix.height = uframe->frame.w_height;
>  		fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(uformat, uframe);
>  		fmt->fmt.pix.sizeimage = uvc_get_frame_size(uformat, uframe);
> -		fmt->fmt.pix.pixelformat = to_uvc_format(uformat)->fcc;
> +		fmtdesc = to_uvc_format(uformat);
> +		if (IS_ERR(fmtdesc))
> +			return -EINVAL;
> +		fmt->fmt.pix.pixelformat = fmtdesc->fcc;
>  	}
>  	fmt->fmt.pix.field = V4L2_FIELD_NONE;
>  	fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
> @@ -389,6 +396,9 @@ uvc_v4l2_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
>  		return -EINVAL;
> 
>  	fmtdesc = to_uvc_format(uformat);
> +	if (IS_ERR(fmtdesc))
> +		return -EINVAL;
> +
>  	f->pixelformat = fmtdesc->fcc;
> 
>  	return 0;
> --
> 2.34.1
> 
> 

Hi,

This is the friendly patch-bot of Greg Kroah-Hartman.  You have sent him
a patch that has triggered this response.  He used to manually respond
to these common problems, but in order to save his sanity (he kept
writing the same thing over and over, yet to different people), I was
created.  Hopefully you will not take offence and will fix the problem
in your patch and resubmit it so that it can be accepted into the Linux
kernel tree.

You are receiving this message because of the following common error(s)
as indicated below:

- You have marked a patch with a "Fixes:" tag for a commit that is in an
  older released kernel, yet you do not have a cc: stable line in the
  signed-off-by area at all, which means that the patch will not be
  applied to any older kernel releases.  To properly fix this, please
  follow the documented rules in the
  Documentation/process/stable-kernel-rules.rst file for how to resolve
  this.

If you wish to discuss this problem further, or you have questions about
how to resolve this issue, please feel free to respond to this email and
Greg will reply once he has dug out from the pending patches received
from other developers.

thanks,

greg k-h's patch email bot
Greg Kroah-Hartman Aug. 15, 2024, 8 a.m. UTC | #3
On Thu, Aug 15, 2024 at 12:44:16PM +0530, Abhishek Tamboli wrote:
> Fix potential dereferencing of ERR_PTR() in find_format_by_pix()
> and uvc_v4l2_enum_format().
> 
> Fix the following smatch errors:
> 
> drivers/usb/gadget/function/uvc_v4l2.c:124 find_format_by_pix()
> error: 'fmtdesc' dereferencing possible ERR_PTR()
> 
> drivers/usb/gadget/function/uvc_v4l2.c:392 uvc_v4l2_enum_format()
> error: 'fmtdesc' dereferencing possible ERR_PTR()
> 
> Also, fix similar issue in uvc_v4l2_try_format() for potential
> dereferencing of ERR_PTR().
> 
> Fixes: 588b9e85609b ("usb: gadget: uvc: add v4l2 enumeration api calls")
> Fixes: e219a712bc06 ("usb: gadget: uvc: add v4l2 try_format api call")
> Signed-off-by: Abhishek Tamboli <abhishektamboli9@gmail.com>
> ---
> Changes in v2:
> - Add check for dereferencing of ERR_PTR() in uvc_v4l2_try_format()
> 
>  drivers/usb/gadget/function/uvc_v4l2.c | 12 +++++++++++-
>  1 file changed, 11 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/gadget/function/uvc_v4l2.c b/drivers/usb/gadget/function/uvc_v4l2.c
> index a024aecb76dc..8bb88c864b60 100644
> --- a/drivers/usb/gadget/function/uvc_v4l2.c
> +++ b/drivers/usb/gadget/function/uvc_v4l2.c
> @@ -121,6 +121,9 @@ static struct uvcg_format *find_format_by_pix(struct uvc_device *uvc,
>  	list_for_each_entry(format, &uvc->header->formats, entry) {
>  		const struct uvc_format_desc *fmtdesc = to_uvc_format(format->fmt);
> 
> +		if (IS_ERR(fmtdesc))
> +			continue;
> +
>  		if (fmtdesc->fcc == pixelformat) {
>  			uformat = format->fmt;
>  			break;
> @@ -240,6 +243,7 @@ uvc_v4l2_try_format(struct file *file, void *fh, struct v4l2_format *fmt)
>  	struct uvc_video *video = &uvc->video;
>  	struct uvcg_format *uformat;
>  	struct uvcg_frame *uframe;
> +	const struct uvc_format_desc *fmtdesc;
>  	u8 *fcc;
> 
>  	if (fmt->type != video->queue.queue.type)
> @@ -277,7 +281,10 @@ uvc_v4l2_try_format(struct file *file, void *fh, struct v4l2_format *fmt)
>  		fmt->fmt.pix.height = uframe->frame.w_height;
>  		fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(uformat, uframe);
>  		fmt->fmt.pix.sizeimage = uvc_get_frame_size(uformat, uframe);
> -		fmt->fmt.pix.pixelformat = to_uvc_format(uformat)->fcc;
> +		fmtdesc = to_uvc_format(uformat);
> +		if (IS_ERR(fmtdesc))
> +			return -EINVAL;

Why not return the error given to you?

> +		fmt->fmt.pix.pixelformat = fmtdesc->fcc;
>  	}
>  	fmt->fmt.pix.field = V4L2_FIELD_NONE;
>  	fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
> @@ -389,6 +396,9 @@ uvc_v4l2_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
>  		return -EINVAL;
> 
>  	fmtdesc = to_uvc_format(uformat);
> +	if (IS_ERR(fmtdesc))
> +		return -EINVAL;

Same here.

thanks,

greg k-h
Abhishek Tamboli Aug. 15, 2024, 8:53 a.m. UTC | #4
On Thu, Aug 15, 2024 at 09:59:35AM +0200, Greg KH wrote:
> On Thu, Aug 15, 2024 at 12:44:16PM +0530, Abhishek Tamboli wrote:
> > Fix potential dereferencing of ERR_PTR() in find_format_by_pix()
> > and uvc_v4l2_enum_format().
> > 
> > Fix the following smatch errors:
> > 
> > drivers/usb/gadget/function/uvc_v4l2.c:124 find_format_by_pix()
> > error: 'fmtdesc' dereferencing possible ERR_PTR()
> > 
> > drivers/usb/gadget/function/uvc_v4l2.c:392 uvc_v4l2_enum_format()
> > error: 'fmtdesc' dereferencing possible ERR_PTR()
> > 
> > Also, fix similar issue in uvc_v4l2_try_format() for potential
> > dereferencing of ERR_PTR().
> > 
> > Fixes: 588b9e85609b ("usb: gadget: uvc: add v4l2 enumeration api calls")
> > Fixes: e219a712bc06 ("usb: gadget: uvc: add v4l2 try_format api call")
> > Signed-off-by: Abhishek Tamboli <abhishektamboli9@gmail.com>
> > ---
> > Changes in v2:
> > - Add check for dereferencing of ERR_PTR() in uvc_v4l2_try_format()
> 
> You sent 2 copies of this, which is the correct one?
Sorry about that. Actually, I forgot to add the maintainer: Laurent Pinchart 
in my first Patch v2 mail.

Regards,
Abhishek
Abhishek Tamboli Aug. 15, 2024, 9:19 a.m. UTC | #5
Hi Greg,
Thank you for the feedback.

On Thu, Aug 15, 2024 at 10:00:27AM +0200, Greg KH wrote:
> On Thu, Aug 15, 2024 at 12:44:16PM +0530, Abhishek Tamboli wrote:
> > Fix potential dereferencing of ERR_PTR() in find_format_by_pix()
> > and uvc_v4l2_enum_format().
> > 
> > Fix the following smatch errors:
> > 
> > drivers/usb/gadget/function/uvc_v4l2.c:124 find_format_by_pix()
> > error: 'fmtdesc' dereferencing possible ERR_PTR()
> > 
> > drivers/usb/gadget/function/uvc_v4l2.c:392 uvc_v4l2_enum_format()
> > error: 'fmtdesc' dereferencing possible ERR_PTR()
> > 
> > Also, fix similar issue in uvc_v4l2_try_format() for potential
> > dereferencing of ERR_PTR().
> > 
> > Fixes: 588b9e85609b ("usb: gadget: uvc: add v4l2 enumeration api calls")
> > Fixes: e219a712bc06 ("usb: gadget: uvc: add v4l2 try_format api call")
> > Signed-off-by: Abhishek Tamboli <abhishektamboli9@gmail.com>
> > ---
> > Changes in v2:
> > - Add check for dereferencing of ERR_PTR() in uvc_v4l2_try_format()
> > 
> >  drivers/usb/gadget/function/uvc_v4l2.c | 12 +++++++++++-
> >  1 file changed, 11 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/usb/gadget/function/uvc_v4l2.c b/drivers/usb/gadget/function/uvc_v4l2.c
> > index a024aecb76dc..8bb88c864b60 100644
> > --- a/drivers/usb/gadget/function/uvc_v4l2.c
> > +++ b/drivers/usb/gadget/function/uvc_v4l2.c
> > @@ -121,6 +121,9 @@ static struct uvcg_format *find_format_by_pix(struct uvc_device *uvc,
> >  	list_for_each_entry(format, &uvc->header->formats, entry) {
> >  		const struct uvc_format_desc *fmtdesc = to_uvc_format(format->fmt);
> > 
> > +		if (IS_ERR(fmtdesc))
> > +			continue;
> > +
> >  		if (fmtdesc->fcc == pixelformat) {
> >  			uformat = format->fmt;
> >  			break;
> > @@ -240,6 +243,7 @@ uvc_v4l2_try_format(struct file *file, void *fh, struct v4l2_format *fmt)
> >  	struct uvc_video *video = &uvc->video;
> >  	struct uvcg_format *uformat;
> >  	struct uvcg_frame *uframe;
> > +	const struct uvc_format_desc *fmtdesc;
> >  	u8 *fcc;
> > 
> >  	if (fmt->type != video->queue.queue.type)
> > @@ -277,7 +281,10 @@ uvc_v4l2_try_format(struct file *file, void *fh, struct v4l2_format *fmt)
> >  		fmt->fmt.pix.height = uframe->frame.w_height;
> >  		fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(uformat, uframe);
> >  		fmt->fmt.pix.sizeimage = uvc_get_frame_size(uformat, uframe);
> > -		fmt->fmt.pix.pixelformat = to_uvc_format(uformat)->fcc;
> > +		fmtdesc = to_uvc_format(uformat);
> > +		if (IS_ERR(fmtdesc))
> > +			return -EINVAL;
> 
> Why not return the error given to you?
Returning -EINVAL directly was based on the current implementation of to_uvc_format(), 
which only returns ERR_PTR(-EINVAL) in case of error.
> 
> > +		fmt->fmt.pix.pixelformat = fmtdesc->fcc;
> >  	}
> >  	fmt->fmt.pix.field = V4L2_FIELD_NONE;
> >  	fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
> > @@ -389,6 +396,9 @@ uvc_v4l2_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
> >  		return -EINVAL;
> > 
> >  	fmtdesc = to_uvc_format(uformat);
> > +	if (IS_ERR(fmtdesc))
> > +		return -EINVAL;
> 
> Same here.
If you'd like me to make the changes to use PTR_ERR()? 
I'll update the patch.

Regards,
Abhishek
Greg Kroah-Hartman Aug. 15, 2024, 9:20 a.m. UTC | #6
On Thu, Aug 15, 2024 at 02:23:09PM +0530, Abhishek Tamboli wrote:
> On Thu, Aug 15, 2024 at 09:59:35AM +0200, Greg KH wrote:
> > On Thu, Aug 15, 2024 at 12:44:16PM +0530, Abhishek Tamboli wrote:
> > > Fix potential dereferencing of ERR_PTR() in find_format_by_pix()
> > > and uvc_v4l2_enum_format().
> > > 
> > > Fix the following smatch errors:
> > > 
> > > drivers/usb/gadget/function/uvc_v4l2.c:124 find_format_by_pix()
> > > error: 'fmtdesc' dereferencing possible ERR_PTR()
> > > 
> > > drivers/usb/gadget/function/uvc_v4l2.c:392 uvc_v4l2_enum_format()
> > > error: 'fmtdesc' dereferencing possible ERR_PTR()
> > > 
> > > Also, fix similar issue in uvc_v4l2_try_format() for potential
> > > dereferencing of ERR_PTR().
> > > 
> > > Fixes: 588b9e85609b ("usb: gadget: uvc: add v4l2 enumeration api calls")
> > > Fixes: e219a712bc06 ("usb: gadget: uvc: add v4l2 try_format api call")
> > > Signed-off-by: Abhishek Tamboli <abhishektamboli9@gmail.com>
> > > ---
> > > Changes in v2:
> > > - Add check for dereferencing of ERR_PTR() in uvc_v4l2_try_format()
> > 
> > You sent 2 copies of this, which is the correct one?
> Sorry about that. Actually, I forgot to add the maintainer: Laurent Pinchart 
> in my first Patch v2 mail.

Ok, then that would be a v3, right?  Please remember we get hundreds of
patches a day and keeping them all straight is important :)

thanks,

greg k-h
Greg Kroah-Hartman Aug. 15, 2024, 9:32 a.m. UTC | #7
On Thu, Aug 15, 2024 at 02:49:36PM +0530, Abhishek Tamboli wrote:
> Hi Greg,
> Thank you for the feedback.
> 
> On Thu, Aug 15, 2024 at 10:00:27AM +0200, Greg KH wrote:
> > On Thu, Aug 15, 2024 at 12:44:16PM +0530, Abhishek Tamboli wrote:
> > > Fix potential dereferencing of ERR_PTR() in find_format_by_pix()
> > > and uvc_v4l2_enum_format().
> > > 
> > > Fix the following smatch errors:
> > > 
> > > drivers/usb/gadget/function/uvc_v4l2.c:124 find_format_by_pix()
> > > error: 'fmtdesc' dereferencing possible ERR_PTR()
> > > 
> > > drivers/usb/gadget/function/uvc_v4l2.c:392 uvc_v4l2_enum_format()
> > > error: 'fmtdesc' dereferencing possible ERR_PTR()
> > > 
> > > Also, fix similar issue in uvc_v4l2_try_format() for potential
> > > dereferencing of ERR_PTR().
> > > 
> > > Fixes: 588b9e85609b ("usb: gadget: uvc: add v4l2 enumeration api calls")
> > > Fixes: e219a712bc06 ("usb: gadget: uvc: add v4l2 try_format api call")
> > > Signed-off-by: Abhishek Tamboli <abhishektamboli9@gmail.com>
> > > ---
> > > Changes in v2:
> > > - Add check for dereferencing of ERR_PTR() in uvc_v4l2_try_format()
> > > 
> > >  drivers/usb/gadget/function/uvc_v4l2.c | 12 +++++++++++-
> > >  1 file changed, 11 insertions(+), 1 deletion(-)
> > > 
> > > diff --git a/drivers/usb/gadget/function/uvc_v4l2.c b/drivers/usb/gadget/function/uvc_v4l2.c
> > > index a024aecb76dc..8bb88c864b60 100644
> > > --- a/drivers/usb/gadget/function/uvc_v4l2.c
> > > +++ b/drivers/usb/gadget/function/uvc_v4l2.c
> > > @@ -121,6 +121,9 @@ static struct uvcg_format *find_format_by_pix(struct uvc_device *uvc,
> > >  	list_for_each_entry(format, &uvc->header->formats, entry) {
> > >  		const struct uvc_format_desc *fmtdesc = to_uvc_format(format->fmt);
> > > 
> > > +		if (IS_ERR(fmtdesc))
> > > +			continue;
> > > +
> > >  		if (fmtdesc->fcc == pixelformat) {
> > >  			uformat = format->fmt;
> > >  			break;
> > > @@ -240,6 +243,7 @@ uvc_v4l2_try_format(struct file *file, void *fh, struct v4l2_format *fmt)
> > >  	struct uvc_video *video = &uvc->video;
> > >  	struct uvcg_format *uformat;
> > >  	struct uvcg_frame *uframe;
> > > +	const struct uvc_format_desc *fmtdesc;
> > >  	u8 *fcc;
> > > 
> > >  	if (fmt->type != video->queue.queue.type)
> > > @@ -277,7 +281,10 @@ uvc_v4l2_try_format(struct file *file, void *fh, struct v4l2_format *fmt)
> > >  		fmt->fmt.pix.height = uframe->frame.w_height;
> > >  		fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(uformat, uframe);
> > >  		fmt->fmt.pix.sizeimage = uvc_get_frame_size(uformat, uframe);
> > > -		fmt->fmt.pix.pixelformat = to_uvc_format(uformat)->fcc;
> > > +		fmtdesc = to_uvc_format(uformat);
> > > +		if (IS_ERR(fmtdesc))
> > > +			return -EINVAL;
> > 
> > Why not return the error given to you?
> Returning -EINVAL directly was based on the current implementation of to_uvc_format(), 
> which only returns ERR_PTR(-EINVAL) in case of error.
> > 
> > > +		fmt->fmt.pix.pixelformat = fmtdesc->fcc;
> > >  	}
> > >  	fmt->fmt.pix.field = V4L2_FIELD_NONE;
> > >  	fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
> > > @@ -389,6 +396,9 @@ uvc_v4l2_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
> > >  		return -EINVAL;
> > > 
> > >  	fmtdesc = to_uvc_format(uformat);
> > > +	if (IS_ERR(fmtdesc))
> > > +		return -EINVAL;
> > 
> > Same here.
> If you'd like me to make the changes to use PTR_ERR()? 

Yes please.
diff mbox series

Patch

diff --git a/drivers/usb/gadget/function/uvc_v4l2.c b/drivers/usb/gadget/function/uvc_v4l2.c
index a024aecb76dc..8bb88c864b60 100644
--- a/drivers/usb/gadget/function/uvc_v4l2.c
+++ b/drivers/usb/gadget/function/uvc_v4l2.c
@@ -121,6 +121,9 @@  static struct uvcg_format *find_format_by_pix(struct uvc_device *uvc,
 	list_for_each_entry(format, &uvc->header->formats, entry) {
 		const struct uvc_format_desc *fmtdesc = to_uvc_format(format->fmt);

+		if (IS_ERR(fmtdesc))
+			continue;
+
 		if (fmtdesc->fcc == pixelformat) {
 			uformat = format->fmt;
 			break;
@@ -240,6 +243,7 @@  uvc_v4l2_try_format(struct file *file, void *fh, struct v4l2_format *fmt)
 	struct uvc_video *video = &uvc->video;
 	struct uvcg_format *uformat;
 	struct uvcg_frame *uframe;
+	const struct uvc_format_desc *fmtdesc;
 	u8 *fcc;

 	if (fmt->type != video->queue.queue.type)
@@ -277,7 +281,10 @@  uvc_v4l2_try_format(struct file *file, void *fh, struct v4l2_format *fmt)
 		fmt->fmt.pix.height = uframe->frame.w_height;
 		fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(uformat, uframe);
 		fmt->fmt.pix.sizeimage = uvc_get_frame_size(uformat, uframe);
-		fmt->fmt.pix.pixelformat = to_uvc_format(uformat)->fcc;
+		fmtdesc = to_uvc_format(uformat);
+		if (IS_ERR(fmtdesc))
+			return -EINVAL;
+		fmt->fmt.pix.pixelformat = fmtdesc->fcc;
 	}
 	fmt->fmt.pix.field = V4L2_FIELD_NONE;
 	fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
@@ -389,6 +396,9 @@  uvc_v4l2_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
 		return -EINVAL;

 	fmtdesc = to_uvc_format(uformat);
+	if (IS_ERR(fmtdesc))
+		return -EINVAL;
+
 	f->pixelformat = fmtdesc->fcc;

 	return 0;