Linux-Media Archive on lore.kernel.org
 help / Atom feed
* [PATCH 1/1] v4l: ioctl: Validate num_planes before using it
@ 2019-01-10 12:43 Sakari Ailus
  2019-01-10 12:51 ` Thierry Reding
                   ` (3 more replies)
  0 siblings, 4 replies; 11+ messages in thread
From: Sakari Ailus @ 2019-01-10 12:43 UTC (permalink / raw)
  To: linux-media; +Cc: hverkuil, Thierry Reding, Ezequiel Garcia

The for loop to reset the memory of the plane reserved fields runs over
num_planes provided by the user without validating it. Ensure num_planes
is no more than VIDEO_MAX_PLANES before the loop.

Fixes: 4e1e0eb0e074 ("media: v4l2-ioctl: Zero v4l2_plane_pix_format reserved fields")
Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
---
Hi folks,

This patch goes on top of Thierry's patch "media: v4l2-ioctl: Clear only
per-plane reserved fields".

 drivers/media/v4l2-core/v4l2-ioctl.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c
index 392f1228af7b5..9e68a608ac6d3 100644
--- a/drivers/media/v4l2-core/v4l2-ioctl.c
+++ b/drivers/media/v4l2-core/v4l2-ioctl.c
@@ -1551,6 +1551,8 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
 		if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
 			break;
 		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
+		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
+			break;
 		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
 			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
 		return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
@@ -1581,6 +1583,8 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
 		if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
 			break;
 		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
+		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
+			break;
 		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
 			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
 		return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
@@ -1648,6 +1652,8 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
 		if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
 			break;
 		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
+		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
+			break;
 		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
 			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
 		return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
@@ -1678,6 +1684,8 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
 		if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
 			break;
 		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
+		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
+			break;
 		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
 			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
 		return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
-- 
2.11.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/1] v4l: ioctl: Validate num_planes before using it
  2019-01-10 12:43 [PATCH 1/1] v4l: ioctl: Validate num_planes before using it Sakari Ailus
@ 2019-01-10 12:51 ` Thierry Reding
  2019-01-10 13:02 ` Hans Verkuil
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 11+ messages in thread
From: Thierry Reding @ 2019-01-10 12:51 UTC (permalink / raw)
  To: Sakari Ailus; +Cc: linux-media, hverkuil, Ezequiel Garcia

[-- Attachment #1: Type: text/plain, Size: 2836 bytes --]

On Thu, Jan 10, 2019 at 02:43:19PM +0200, Sakari Ailus wrote:
> The for loop to reset the memory of the plane reserved fields runs over
> num_planes provided by the user without validating it. Ensure num_planes
> is no more than VIDEO_MAX_PLANES before the loop.
> 
> Fixes: 4e1e0eb0e074 ("media: v4l2-ioctl: Zero v4l2_plane_pix_format reserved fields")
> Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
> ---
> Hi folks,
> 
> This patch goes on top of Thierry's patch "media: v4l2-ioctl: Clear only
> per-plane reserved fields".
> 
>  drivers/media/v4l2-core/v4l2-ioctl.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c
> index 392f1228af7b5..9e68a608ac6d3 100644
> --- a/drivers/media/v4l2-core/v4l2-ioctl.c
> +++ b/drivers/media/v4l2-core/v4l2-ioctl.c
> @@ -1551,6 +1551,8 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
>  		if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
>  			break;
>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> +			break;
>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>  		return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
> @@ -1581,6 +1583,8 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
>  		if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
>  			break;
>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> +			break;
>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>  		return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
> @@ -1648,6 +1652,8 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
>  		if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
>  			break;
>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> +			break;
>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>  		return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
> @@ -1678,6 +1684,8 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
>  		if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
>  			break;
>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> +			break;
>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>  		return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);

Do we also want to validate the instance in v4l_print_format() before
using it?

Reviewed-by: Thierry Reding <treding@nvidia.com>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/1] v4l: ioctl: Validate num_planes before using it
  2019-01-10 12:43 [PATCH 1/1] v4l: ioctl: Validate num_planes before using it Sakari Ailus
  2019-01-10 12:51 ` Thierry Reding
@ 2019-01-10 13:02 ` Hans Verkuil
  2019-01-10 13:30   ` Sakari Ailus
  2019-01-10 13:41   ` Sakari Ailus
  2019-01-10 14:12 ` Hans Verkuil
  2019-02-15 15:52 ` Ezequiel Garcia
  3 siblings, 2 replies; 11+ messages in thread
From: Hans Verkuil @ 2019-01-10 13:02 UTC (permalink / raw)
  To: Sakari Ailus, linux-media; +Cc: Thierry Reding, Ezequiel Garcia

On 01/10/19 13:43, Sakari Ailus wrote:
> The for loop to reset the memory of the plane reserved fields runs over
> num_planes provided by the user without validating it. Ensure num_planes
> is no more than VIDEO_MAX_PLANES before the loop.
> 
> Fixes: 4e1e0eb0e074 ("media: v4l2-ioctl: Zero v4l2_plane_pix_format reserved fields")
> Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
> ---
> Hi folks,
> 
> This patch goes on top of Thierry's patch "media: v4l2-ioctl: Clear only
> per-plane reserved fields".
> 
>  drivers/media/v4l2-core/v4l2-ioctl.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c
> index 392f1228af7b5..9e68a608ac6d3 100644
> --- a/drivers/media/v4l2-core/v4l2-ioctl.c
> +++ b/drivers/media/v4l2-core/v4l2-ioctl.c
> @@ -1551,6 +1551,8 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
>  		if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
>  			break;
>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> +			break;

I would check this not here but in check_fmt: all *_FMT ioctls call that function
first, so it makes sense to do the check there.

v4l_print_format should also be adjusted (take the minimum of num_planes and
VIDEO_MAX_PLANES), since it can still be called even if check_fmt returns an
error if num_planes is too large.

In fact, the change to v4l_print_format should be a separate patch since that
should be backported. It can leak memory in the kernel log if num_planes is
too large.

Regards,

	Hans

>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>  		return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
> @@ -1581,6 +1583,8 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
>  		if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
>  			break;
>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> +			break;
>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>  		return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
> @@ -1648,6 +1652,8 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
>  		if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
>  			break;
>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> +			break;
>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>  		return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
> @@ -1678,6 +1684,8 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
>  		if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
>  			break;
>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> +			break;
>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>  		return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
> 


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/1] v4l: ioctl: Validate num_planes before using it
  2019-01-10 13:02 ` Hans Verkuil
@ 2019-01-10 13:30   ` Sakari Ailus
  2019-01-10 13:41   ` Sakari Ailus
  1 sibling, 0 replies; 11+ messages in thread
From: Sakari Ailus @ 2019-01-10 13:30 UTC (permalink / raw)
  To: Hans Verkuil; +Cc: linux-media, Thierry Reding, Ezequiel Garcia

Hi Hans,

On Thu, Jan 10, 2019 at 02:02:14PM +0100, Hans Verkuil wrote:
> On 01/10/19 13:43, Sakari Ailus wrote:
> > The for loop to reset the memory of the plane reserved fields runs over
> > num_planes provided by the user without validating it. Ensure num_planes
> > is no more than VIDEO_MAX_PLANES before the loop.
> > 
> > Fixes: 4e1e0eb0e074 ("media: v4l2-ioctl: Zero v4l2_plane_pix_format reserved fields")
> > Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
> > ---
> > Hi folks,
> > 
> > This patch goes on top of Thierry's patch "media: v4l2-ioctl: Clear only
> > per-plane reserved fields".
> > 
> >  drivers/media/v4l2-core/v4l2-ioctl.c | 8 ++++++++
> >  1 file changed, 8 insertions(+)
> > 
> > diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c
> > index 392f1228af7b5..9e68a608ac6d3 100644
> > --- a/drivers/media/v4l2-core/v4l2-ioctl.c
> > +++ b/drivers/media/v4l2-core/v4l2-ioctl.c
> > @@ -1551,6 +1551,8 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
> >  		if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
> >  			break;
> >  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> > +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> > +			break;
> 
> I would check this not here but in check_fmt: all *_FMT ioctls call that function
> first, so it makes sense to do the check there.

check_fmt() just takes the buffer type, no more parameters than that. But
I'm fine with introducing another function for the purpose, perhaps
renaming check_fmt() as check_buf_type().

I'll send v2.

> 
> v4l_print_format should also be adjusted (take the minimum of num_planes and
> VIDEO_MAX_PLANES), since it can still be called even if check_fmt returns an
> error if num_planes is too large.
> 
> In fact, the change to v4l_print_format should be a separate patch since that
> should be backported. It can leak memory in the kernel log if num_planes is
> too large.

Yes. I'm trying to figure out the affected versions. The code has been
mangled multiple times over the years so it's not entirely trivial.

> 
> Regards,
> 
> 	Hans
> 
> >  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
> >  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
> >  		return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
> > @@ -1581,6 +1583,8 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
> >  		if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
> >  			break;
> >  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> > +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> > +			break;
> >  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
> >  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
> >  		return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
> > @@ -1648,6 +1652,8 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
> >  		if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
> >  			break;
> >  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> > +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> > +			break;
> >  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
> >  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
> >  		return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
> > @@ -1678,6 +1684,8 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
> >  		if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
> >  			break;
> >  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> > +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> > +			break;
> >  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
> >  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
> >  		return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
> > 
> 

-- 
Sakari Ailus
sakari.ailus@linux.intel.com

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/1] v4l: ioctl: Validate num_planes before using it
  2019-01-10 13:02 ` Hans Verkuil
  2019-01-10 13:30   ` Sakari Ailus
@ 2019-01-10 13:41   ` Sakari Ailus
  2019-01-10 14:11     ` Hans Verkuil
  1 sibling, 1 reply; 11+ messages in thread
From: Sakari Ailus @ 2019-01-10 13:41 UTC (permalink / raw)
  To: Hans Verkuil; +Cc: linux-media, Thierry Reding, Ezequiel Garcia

On Thu, Jan 10, 2019 at 02:02:14PM +0100, Hans Verkuil wrote:
> On 01/10/19 13:43, Sakari Ailus wrote:
> > The for loop to reset the memory of the plane reserved fields runs over
> > num_planes provided by the user without validating it. Ensure num_planes
> > is no more than VIDEO_MAX_PLANES before the loop.
> > 
> > Fixes: 4e1e0eb0e074 ("media: v4l2-ioctl: Zero v4l2_plane_pix_format reserved fields")
> > Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
> > ---
> > Hi folks,
> > 
> > This patch goes on top of Thierry's patch "media: v4l2-ioctl: Clear only
> > per-plane reserved fields".
> > 
> >  drivers/media/v4l2-core/v4l2-ioctl.c | 8 ++++++++
> >  1 file changed, 8 insertions(+)
> > 
> > diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c
> > index 392f1228af7b5..9e68a608ac6d3 100644
> > --- a/drivers/media/v4l2-core/v4l2-ioctl.c
> > +++ b/drivers/media/v4l2-core/v4l2-ioctl.c
> > @@ -1551,6 +1551,8 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
> >  		if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
> >  			break;
> >  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> > +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> > +			break;
> 
> I would check this not here but in check_fmt: all *_FMT ioctls call that function
> first, so it makes sense to do the check there.

Even G_FMT? I'm not saying no though. There's just a slight chance of
breaking something as it hasn't been a problem to call G_FMT with incorrect
number of planes in the mplane format; the number would be overwritten
anyway.

Apart from that, this leaves just the four locations --- putting this to a
separate function will reduce the calls to that function to just two.

> 
> v4l_print_format should also be adjusted (take the minimum of num_planes and
> VIDEO_MAX_PLANES), since it can still be called even if check_fmt returns an
> error if num_planes is too large.
> 
> In fact, the change to v4l_print_format should be a separate patch since that
> should be backported. It can leak memory in the kernel log if num_planes is
> too large.
> 
> Regards,
> 
> 	Hans
> 
> >  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
> >  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
> >  		return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
> > @@ -1581,6 +1583,8 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
> >  		if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
> >  			break;
> >  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> > +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> > +			break;
> >  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
> >  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
> >  		return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
> > @@ -1648,6 +1652,8 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
> >  		if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
> >  			break;
> >  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> > +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> > +			break;
> >  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
> >  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
> >  		return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
> > @@ -1678,6 +1684,8 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
> >  		if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
> >  			break;
> >  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> > +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> > +			break;
> >  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
> >  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
> >  		return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
> > 
> 

-- 
Sakari Ailus
sakari.ailus@linux.intel.com

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/1] v4l: ioctl: Validate num_planes before using it
  2019-01-10 13:41   ` Sakari Ailus
@ 2019-01-10 14:11     ` Hans Verkuil
  2019-01-10 14:18       ` Sakari Ailus
  0 siblings, 1 reply; 11+ messages in thread
From: Hans Verkuil @ 2019-01-10 14:11 UTC (permalink / raw)
  To: Sakari Ailus; +Cc: linux-media, Thierry Reding, Ezequiel Garcia

On 01/10/19 14:41, Sakari Ailus wrote:
> On Thu, Jan 10, 2019 at 02:02:14PM +0100, Hans Verkuil wrote:
>> On 01/10/19 13:43, Sakari Ailus wrote:
>>> The for loop to reset the memory of the plane reserved fields runs over
>>> num_planes provided by the user without validating it. Ensure num_planes
>>> is no more than VIDEO_MAX_PLANES before the loop.
>>>
>>> Fixes: 4e1e0eb0e074 ("media: v4l2-ioctl: Zero v4l2_plane_pix_format reserved fields")
>>> Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
>>> ---
>>> Hi folks,
>>>
>>> This patch goes on top of Thierry's patch "media: v4l2-ioctl: Clear only
>>> per-plane reserved fields".
>>>
>>>  drivers/media/v4l2-core/v4l2-ioctl.c | 8 ++++++++
>>>  1 file changed, 8 insertions(+)
>>>
>>> diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c
>>> index 392f1228af7b5..9e68a608ac6d3 100644
>>> --- a/drivers/media/v4l2-core/v4l2-ioctl.c
>>> +++ b/drivers/media/v4l2-core/v4l2-ioctl.c
>>> @@ -1551,6 +1551,8 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
>>>  		if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
>>>  			break;
>>>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
>>> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
>>> +			break;
>>
>> I would check this not here but in check_fmt: all *_FMT ioctls call that function
>> first, so it makes sense to do the check there.
> 
> Even G_FMT? I'm not saying no though. There's just a slight chance of
> breaking something as it hasn't been a problem to call G_FMT with incorrect
> number of planes in the mplane format; the number would be overwritten
> anyway.

Not G_FMT since everything after the type field is zeroed.

> Apart from that, this leaves just the four locations --- putting this to a
> separate function will reduce the calls to that function to just two.

I was a bit too quick with my comment about check_fmt. Your original patch
is fine.

I'll Ack your patch.

Regards,

	Hans

> 
>>
>> v4l_print_format should also be adjusted (take the minimum of num_planes and
>> VIDEO_MAX_PLANES), since it can still be called even if check_fmt returns an
>> error if num_planes is too large.
>>
>> In fact, the change to v4l_print_format should be a separate patch since that
>> should be backported. It can leak memory in the kernel log if num_planes is
>> too large.
>>
>> Regards,
>>
>> 	Hans
>>
>>>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>>>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>>>  		return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
>>> @@ -1581,6 +1583,8 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
>>>  		if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
>>>  			break;
>>>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
>>> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
>>> +			break;
>>>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>>>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>>>  		return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
>>> @@ -1648,6 +1652,8 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
>>>  		if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
>>>  			break;
>>>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
>>> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
>>> +			break;
>>>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>>>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>>>  		return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
>>> @@ -1678,6 +1684,8 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
>>>  		if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
>>>  			break;
>>>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
>>> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
>>> +			break;
>>>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>>>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>>>  		return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
>>>
>>
> 


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/1] v4l: ioctl: Validate num_planes before using it
  2019-01-10 12:43 [PATCH 1/1] v4l: ioctl: Validate num_planes before using it Sakari Ailus
  2019-01-10 12:51 ` Thierry Reding
  2019-01-10 13:02 ` Hans Verkuil
@ 2019-01-10 14:12 ` Hans Verkuil
  2019-02-15 15:52 ` Ezequiel Garcia
  3 siblings, 0 replies; 11+ messages in thread
From: Hans Verkuil @ 2019-01-10 14:12 UTC (permalink / raw)
  To: Sakari Ailus, linux-media; +Cc: Thierry Reding, Ezequiel Garcia

On 01/10/19 13:43, Sakari Ailus wrote:
> The for loop to reset the memory of the plane reserved fields runs over
> num_planes provided by the user without validating it. Ensure num_planes
> is no more than VIDEO_MAX_PLANES before the loop.
> 
> Fixes: 4e1e0eb0e074 ("media: v4l2-ioctl: Zero v4l2_plane_pix_format reserved fields")
> Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>

Reviewed-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>

Regards,

	Hans

> ---
> Hi folks,
> 
> This patch goes on top of Thierry's patch "media: v4l2-ioctl: Clear only
> per-plane reserved fields".
> 
>  drivers/media/v4l2-core/v4l2-ioctl.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c
> index 392f1228af7b5..9e68a608ac6d3 100644
> --- a/drivers/media/v4l2-core/v4l2-ioctl.c
> +++ b/drivers/media/v4l2-core/v4l2-ioctl.c
> @@ -1551,6 +1551,8 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
>  		if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
>  			break;
>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> +			break;
>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>  		return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
> @@ -1581,6 +1583,8 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
>  		if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
>  			break;
>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> +			break;
>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>  		return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
> @@ -1648,6 +1652,8 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
>  		if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
>  			break;
>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> +			break;
>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>  		return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
> @@ -1678,6 +1684,8 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
>  		if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
>  			break;
>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> +			break;
>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>  		return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
> 


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/1] v4l: ioctl: Validate num_planes before using it
  2019-01-10 14:11     ` Hans Verkuil
@ 2019-01-10 14:18       ` Sakari Ailus
  0 siblings, 0 replies; 11+ messages in thread
From: Sakari Ailus @ 2019-01-10 14:18 UTC (permalink / raw)
  To: Hans Verkuil; +Cc: linux-media, Thierry Reding, Ezequiel Garcia

On Thu, Jan 10, 2019 at 03:11:35PM +0100, Hans Verkuil wrote:
> On 01/10/19 14:41, Sakari Ailus wrote:
> > On Thu, Jan 10, 2019 at 02:02:14PM +0100, Hans Verkuil wrote:
> >> On 01/10/19 13:43, Sakari Ailus wrote:
> >>> The for loop to reset the memory of the plane reserved fields runs over
> >>> num_planes provided by the user without validating it. Ensure num_planes
> >>> is no more than VIDEO_MAX_PLANES before the loop.
> >>>
> >>> Fixes: 4e1e0eb0e074 ("media: v4l2-ioctl: Zero v4l2_plane_pix_format reserved fields")
> >>> Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
> >>> ---
> >>> Hi folks,
> >>>
> >>> This patch goes on top of Thierry's patch "media: v4l2-ioctl: Clear only
> >>> per-plane reserved fields".
> >>>
> >>>  drivers/media/v4l2-core/v4l2-ioctl.c | 8 ++++++++
> >>>  1 file changed, 8 insertions(+)
> >>>
> >>> diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c
> >>> index 392f1228af7b5..9e68a608ac6d3 100644
> >>> --- a/drivers/media/v4l2-core/v4l2-ioctl.c
> >>> +++ b/drivers/media/v4l2-core/v4l2-ioctl.c
> >>> @@ -1551,6 +1551,8 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
> >>>  		if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
> >>>  			break;
> >>>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> >>> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> >>> +			break;
> >>
> >> I would check this not here but in check_fmt: all *_FMT ioctls call that function
> >> first, so it makes sense to do the check there.
> > 
> > Even G_FMT? I'm not saying no though. There's just a slight chance of
> > breaking something as it hasn't been a problem to call G_FMT with incorrect
> > number of planes in the mplane format; the number would be overwritten
> > anyway.
> 
> Not G_FMT since everything after the type field is zeroed.
> 
> > Apart from that, this leaves just the four locations --- putting this to a
> > separate function will reduce the calls to that function to just two.
> 
> I was a bit too quick with my comment about check_fmt. Your original patch
> is fine.
> 
> I'll Ack your patch.

Thanks. Could you pick this to your tree as you did Thierry's patch?

I'll submit the other patch in a moment.

> 
> Regards,
> 
> 	Hans
> 
> > 
> >>
> >> v4l_print_format should also be adjusted (take the minimum of num_planes and
> >> VIDEO_MAX_PLANES), since it can still be called even if check_fmt returns an
> >> error if num_planes is too large.
> >>
> >> In fact, the change to v4l_print_format should be a separate patch since that
> >> should be backported. It can leak memory in the kernel log if num_planes is
> >> too large.
> >>
> >> Regards,
> >>
> >> 	Hans
> >>
> >>>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
> >>>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
> >>>  		return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
> >>> @@ -1581,6 +1583,8 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
> >>>  		if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
> >>>  			break;
> >>>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> >>> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> >>> +			break;
> >>>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
> >>>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
> >>>  		return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
> >>> @@ -1648,6 +1652,8 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
> >>>  		if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
> >>>  			break;
> >>>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> >>> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> >>> +			break;
> >>>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
> >>>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
> >>>  		return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
> >>> @@ -1678,6 +1684,8 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
> >>>  		if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
> >>>  			break;
> >>>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> >>> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> >>> +			break;
> >>>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
> >>>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
> >>>  		return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
> >>>
> >>
> > 
> 

-- 
Sakari Ailus
sakari.ailus@linux.intel.com

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/1] v4l: ioctl: Validate num_planes before using it
  2019-01-10 12:43 [PATCH 1/1] v4l: ioctl: Validate num_planes before using it Sakari Ailus
                   ` (2 preceding siblings ...)
  2019-01-10 14:12 ` Hans Verkuil
@ 2019-02-15 15:52 ` Ezequiel Garcia
  2019-02-15 16:05   ` Hans Verkuil
  3 siblings, 1 reply; 11+ messages in thread
From: Ezequiel Garcia @ 2019-02-15 15:52 UTC (permalink / raw)
  To: Sakari Ailus, linux-media, hverkuil; +Cc: Thierry Reding

On Thu, 2019-01-10 at 14:43 +0200, Sakari Ailus wrote:
> The for loop to reset the memory of the plane reserved fields runs over
> num_planes provided by the user without validating it. Ensure num_planes
> is no more than VIDEO_MAX_PLANES before the loop.
> 
> Fixes: 4e1e0eb0e074 ("media: v4l2-ioctl: Zero v4l2_plane_pix_format reserved fields")
> Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
> ---
> Hi folks,
> 
> This patch goes on top of Thierry's patch "media: v4l2-ioctl: Clear only
> per-plane reserved fields".
> 
>  drivers/media/v4l2-core/v4l2-ioctl.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c
> index 392f1228af7b5..9e68a608ac6d3 100644
> --- a/drivers/media/v4l2-core/v4l2-ioctl.c
> +++ b/drivers/media/v4l2-core/v4l2-ioctl.c
> @@ -1551,6 +1551,8 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
>  		if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
>  			break;
>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> +			break;
>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>  		return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
> @@ -1581,6 +1583,8 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
>  		if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
>  			break;
>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> +			break;
>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>  		return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
> @@ -1648,6 +1652,8 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
>  		if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
>  			break;
>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> +			break;
>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>  		return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
> @@ -1678,6 +1684,8 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
>  		if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
>  			break;
>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> +			break;
>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>  		return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);

I'm under the impression this commit is causing the following v4l2-compliance warning:

TRY_FMT cannot handle an invalid pixelformat.
This may or may not be a problem. For more information see:
http://www.mail-archive.com/linux-media@vger.kernel.org/msg56550.html

This applies to try_fmt and s_fmt.

After checking the v4l2 spec, it seems we can only error on a bad type field [1]:

"Drivers should not return an error code unless the type field is invalid".

Perhaps we can clamp num_planes to VIDEO_MAX_PLANES, instead of erroring out?

[1] https://www.kernel.org/doc/html/latest/media/uapi/v4l/vidioc-g-fmt.html

Regards,
Eze


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/1] v4l: ioctl: Validate num_planes before using it
  2019-02-15 15:52 ` Ezequiel Garcia
@ 2019-02-15 16:05   ` Hans Verkuil
  2019-02-15 16:14     ` Ezequiel Garcia
  0 siblings, 1 reply; 11+ messages in thread
From: Hans Verkuil @ 2019-02-15 16:05 UTC (permalink / raw)
  To: Ezequiel Garcia, Sakari Ailus, linux-media; +Cc: Thierry Reding

On 2/15/19 4:52 PM, Ezequiel Garcia wrote:
> On Thu, 2019-01-10 at 14:43 +0200, Sakari Ailus wrote:
>> The for loop to reset the memory of the plane reserved fields runs over
>> num_planes provided by the user without validating it. Ensure num_planes
>> is no more than VIDEO_MAX_PLANES before the loop.
>>
>> Fixes: 4e1e0eb0e074 ("media: v4l2-ioctl: Zero v4l2_plane_pix_format reserved fields")
>> Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
>> ---
>> Hi folks,
>>
>> This patch goes on top of Thierry's patch "media: v4l2-ioctl: Clear only
>> per-plane reserved fields".
>>
>>  drivers/media/v4l2-core/v4l2-ioctl.c | 8 ++++++++
>>  1 file changed, 8 insertions(+)
>>
>> diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c
>> index 392f1228af7b5..9e68a608ac6d3 100644
>> --- a/drivers/media/v4l2-core/v4l2-ioctl.c
>> +++ b/drivers/media/v4l2-core/v4l2-ioctl.c
>> @@ -1551,6 +1551,8 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
>>  		if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
>>  			break;
>>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
>> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
>> +			break;
>>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>>  		return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
>> @@ -1581,6 +1583,8 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
>>  		if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
>>  			break;
>>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
>> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
>> +			break;
>>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>>  		return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
>> @@ -1648,6 +1652,8 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
>>  		if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
>>  			break;
>>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
>> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
>> +			break;
>>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>>  		return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
>> @@ -1678,6 +1684,8 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
>>  		if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
>>  			break;
>>  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
>> +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
>> +			break;
>>  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
>>  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
>>  		return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
> 
> I'm under the impression this commit is causing the following v4l2-compliance warning:
> 
> TRY_FMT cannot handle an invalid pixelformat.
> This may or may not be a problem. For more information see:
> http://www.mail-archive.com/linux-media@vger.kernel.org/msg56550.html
> 
> This applies to try_fmt and s_fmt.
> 
> After checking the v4l2 spec, it seems we can only error on a bad type field [1]:
> 
> "Drivers should not return an error code unless the type field is invalid".
> 
> Perhaps we can clamp num_planes to VIDEO_MAX_PLANES, instead of erroring out?

Yes, that would be the right approach.

Ezequiel, can you clamp num_planes and test the patch? And post it if it fixes the
warning?

This really should have been caught by testing with e.g. vivid and v4l2-compliance.

Regards,

	Hans

> 
> [1] https://www.kernel.org/doc/html/latest/media/uapi/v4l/vidioc-g-fmt.html
> 
> Regards,
> Eze
> 


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/1] v4l: ioctl: Validate num_planes before using it
  2019-02-15 16:05   ` Hans Verkuil
@ 2019-02-15 16:14     ` Ezequiel Garcia
  0 siblings, 0 replies; 11+ messages in thread
From: Ezequiel Garcia @ 2019-02-15 16:14 UTC (permalink / raw)
  To: Hans Verkuil, Sakari Ailus, linux-media; +Cc: Thierry Reding

On Fri, 2019-02-15 at 17:05 +0100, Hans Verkuil wrote:
> On 2/15/19 4:52 PM, Ezequiel Garcia wrote:
> > On Thu, 2019-01-10 at 14:43 +0200, Sakari Ailus wrote:
> > > The for loop to reset the memory of the plane reserved fields runs over
> > > num_planes provided by the user without validating it. Ensure num_planes
> > > is no more than VIDEO_MAX_PLANES before the loop.
> > > 
> > > Fixes: 4e1e0eb0e074 ("media: v4l2-ioctl: Zero v4l2_plane_pix_format reserved fields")
> > > Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
> > > ---
> > > Hi folks,
> > > 
> > > This patch goes on top of Thierry's patch "media: v4l2-ioctl: Clear only
> > > per-plane reserved fields".
> > > 
> > >  drivers/media/v4l2-core/v4l2-ioctl.c | 8 ++++++++
> > >  1 file changed, 8 insertions(+)
> > > 
> > > diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c
> > > index 392f1228af7b5..9e68a608ac6d3 100644
> > > --- a/drivers/media/v4l2-core/v4l2-ioctl.c
> > > +++ b/drivers/media/v4l2-core/v4l2-ioctl.c
> > > @@ -1551,6 +1551,8 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
> > >  		if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
> > >  			break;
> > >  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> > > +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> > > +			break;
> > >  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
> > >  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
> > >  		return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
> > > @@ -1581,6 +1583,8 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
> > >  		if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
> > >  			break;
> > >  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> > > +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> > > +			break;
> > >  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
> > >  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
> > >  		return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
> > > @@ -1648,6 +1652,8 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
> > >  		if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
> > >  			break;
> > >  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> > > +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> > > +			break;
> > >  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
> > >  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
> > >  		return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
> > > @@ -1678,6 +1684,8 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
> > >  		if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
> > >  			break;
> > >  		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
> > > +		if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
> > > +			break;
> > >  		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
> > >  			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], bytesperline);
> > >  		return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
> > 
> > I'm under the impression this commit is causing the following v4l2-compliance warning:
> > 
> > TRY_FMT cannot handle an invalid pixelformat.
> > This may or may not be a problem. For more information see:
> > http://www.mail-archive.com/linux-media@vger.kernel.org/msg56550.html
> > 
> > This applies to try_fmt and s_fmt.
> > 
> > After checking the v4l2 spec, it seems we can only error on a bad type field [1]:
> > 
> > "Drivers should not return an error code unless the type field is invalid".
> > 
> > Perhaps we can clamp num_planes to VIDEO_MAX_PLANES, instead of erroring out?
> 
> Yes, that would be the right approach.
> 
> Ezequiel, can you clamp num_planes and test the patch? And post it if it fixes the
> warning?
> 

I can try :-)

> This really should have been caught by testing with e.g. vivid and v4l2-compliance.
> 

We need a CI!

> Regards,
> 
> 	Hans
> 
> > [1] https://www.kernel.org/doc/html/latest/media/uapi/v4l/vidioc-g-fmt.html
> > 
> > Regards,
> > Eze
> > 



^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, back to index

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-10 12:43 [PATCH 1/1] v4l: ioctl: Validate num_planes before using it Sakari Ailus
2019-01-10 12:51 ` Thierry Reding
2019-01-10 13:02 ` Hans Verkuil
2019-01-10 13:30   ` Sakari Ailus
2019-01-10 13:41   ` Sakari Ailus
2019-01-10 14:11     ` Hans Verkuil
2019-01-10 14:18       ` Sakari Ailus
2019-01-10 14:12 ` Hans Verkuil
2019-02-15 15:52 ` Ezequiel Garcia
2019-02-15 16:05   ` Hans Verkuil
2019-02-15 16:14     ` Ezequiel Garcia

Linux-Media Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-media/0 linux-media/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-media linux-media/ https://lore.kernel.org/linux-media \
		linux-media@vger.kernel.org linux-media@archiver.kernel.org
	public-inbox-index linux-media


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-media


AGPL code for this site: git clone https://public-inbox.org/ public-inbox