Commit 14c8e80e authored by Ezequiel Garcia's avatar Ezequiel Garcia Committed by Mauro Carvalho Chehab

media: v4l: ioctl: Sanitize num_planes before using it

The linked commit changed s_fmt/try_fmt to fail if num_planes is bogus.
This, however, is against the spec, which mandates drivers
to return a proper num_planes value, without an error.

Replace the num_planes check and instead clamp it to a sane value,
so we still make sure we don't overflow the planes array by accident.

Fixes: 9048b2e1 ("media: v4l: ioctl: Validate num_planes before using it")
Signed-off-by: default avatarEzequiel Garcia <ezequiel@collabora.com>
Reviewed-by: default avatarSakari Ailus <sakari.ailus@linux.intel.com>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab+samsung@kernel.org>
parent 5f09bc8c
...@@ -1017,6 +1017,12 @@ static void v4l_sanitize_format(struct v4l2_format *fmt) ...@@ -1017,6 +1017,12 @@ static void v4l_sanitize_format(struct v4l2_format *fmt)
{ {
unsigned int offset; unsigned int offset;
/* Make sure num_planes is not bogus */
if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
fmt->fmt.pix_mp.num_planes = min_t(u32, fmt->fmt.pix_mp.num_planes,
VIDEO_MAX_PLANES);
/* /*
* The v4l2_pix_format structure has been extended with fields that were * The v4l2_pix_format structure has been extended with fields that were
* not previously required to be set to zero by applications. The priv * not previously required to be set to zero by applications. The priv
...@@ -1553,8 +1559,6 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops, ...@@ -1553,8 +1559,6 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane)) if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
break; break;
CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func); 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++) for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
bytesperline); bytesperline);
...@@ -1586,8 +1590,6 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops, ...@@ -1586,8 +1590,6 @@ static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane)) if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
break; break;
CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func); 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++) for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
bytesperline); bytesperline);
...@@ -1656,8 +1658,6 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops, ...@@ -1656,8 +1658,6 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane)) if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
break; break;
CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func); 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++) for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
bytesperline); bytesperline);
...@@ -1689,8 +1689,6 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops, ...@@ -1689,8 +1689,6 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane)) if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
break; break;
CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func); 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++) for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i], CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
bytesperline); bytesperline);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment