Skip to contentSkip to conversation

media: pispbe: Protect against left-shift wrap in V4L2_COLORSPACE_MASK()

PATCH In review Naushir Patuck 3 participants 6 messages Jul 15, 2024 – Jul 18, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: pispbe: Protect against left-shift wrap in V4L2_COLORSPACE_MASK()

Ensure that the user requested colorspace value does not wrap when using the V4L2_COLORSPACE_MASK() macro. If the requested colorspace value >= BIT_PER_LONG, revert to the default colorspace for the given format.

---
 drivers/media/platform/raspberrypi/pisp_be/pisp_be.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/media/platform/raspberrypi/pisp_be/pisp_be.c +3 -2
index e74df5b116dc..bd5d77c691d3 100644
@@ -1124,8 +1124,9 @@ static void pispbe_try_format(struct v4l2_format *f, struct pispbe_node *node)
 	 * not supported. This also catches the case when the "default"
 	 * colour space was requested (as that's never in the mask).
 	 */
-	if (!(V4L2_COLORSPACE_MASK(f->fmt.pix_mp.colorspace) &
-	    fmt->colorspace_mask))
+	if (f->fmt.pix_mp.colorspace >= BITS_PER_LONG ||
+	    !(V4L2_COLORSPACE_MASK(f->fmt.pix_mp.colorspace) &
+	      fmt->colorspace_mask))
 		f->fmt.pix_mp.colorspace = fmt->colorspace_default;
 
 	/* In all cases, we only support the defaults for these: */
Signature · 1 line
2.34.1
maintainer
#1 lore raw

Hi Naush

Quoted 7 lines · Naushir Patuck

Ensure that the user requested colorspace value does not wrap when using the V4L2_COLORSPACE_MASK() macro. If the requested colorspace value >= BIT_PER_LONG, revert to the default colorspace for the given format.

Thanks for handling this

Quoted 9 lines
---
 drivers/media/platform/raspberrypi/pisp_be/pisp_be.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/media/platform/raspberrypi/pisp_be/pisp_be.c +0 -0
index e74df5b116dc..bd5d77c691d3 100644

drivers/media/platform/raspberrypi/pisp_be/pisp_be.c static void pispbe_try_format(struct v4l2_format *f, struct pispbe_node *node) · line 1130

Quoted 9 lines
drivers/media/platform/raspberrypi/pisp_be/pisp_be.c +3 -2
@@ -1124,8 +1124,9 @@ static void pispbe_try_format(struct v4l2_format *f, struct pispbe_node *node)
 	 * not supported. This also catches the case when the "default"
 	 * colour space was requested (as that's never in the mask).
 	 */
-	if (!(V4L2_COLORSPACE_MASK(f->fmt.pix_mp.colorspace) &
-	    fmt->colorspace_mask))
+	if (f->fmt.pix_mp.colorspace >= BITS_PER_LONG ||
+	    !(V4L2_COLORSPACE_MASK(f->fmt.pix_mp.colorspace) &
+	      fmt->colorspace_mask))
 		f->fmt.pix_mp.colorspace = fmt->colorspace_default;

Isn't it better handled in the macro definition itself so that future usages of the V4L2_COLORSPACE_MASK() macro won't need to be protected like this one ?

Would this silence the smatch warning ?

-#define V4L2_COLORSPACE_MASK(colorspace) BIT(colorspace)
+#define V4L2_COLORSPACE_MASK(c) BIT((c) < V4L2_COLORSPACE_LAST ? \
+ (c) : V4L2_COLORSPACE_LAST)

Quoted 5 lines
drivers/media/platform/raspberrypi/pisp_be/pisp_be.c +0 -1
 	/* In all cases, we only support the defaults for these: */
--

2.34.1

#2 lore raw

Hi Jacopo,

Quoted 40 lines · Jacopo Mondi

Hi Naush

Quoted 7 lines · Naushir Patuck

Ensure that the user requested colorspace value does not wrap when using the V4L2_COLORSPACE_MASK() macro. If the requested colorspace value >= BIT_PER_LONG, revert to the default colorspace for the given format.

Thanks for handling this

Quoted 9 lines
---
 drivers/media/platform/raspberrypi/pisp_be/pisp_be.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/media/platform/raspberrypi/pisp_be/pisp_be.c +0 -0
index e74df5b116dc..bd5d77c691d3 100644

drivers/media/platform/raspberrypi/pisp_be/pisp_be.c static void pispbe_try_format(struct v4l2_format *f, struct pispbe_node *node) · line 1130

Quoted 9 lines
drivers/media/platform/raspberrypi/pisp_be/pisp_be.c +3 -2
@@ -1124,8 +1124,9 @@ static void pispbe_try_format(struct v4l2_format *f, struct pispbe_node *node)
       * not supported. This also catches the case when the "default"
       * colour space was requested (as that's never in the mask).
       */
-     if (!(V4L2_COLORSPACE_MASK(f->fmt.pix_mp.colorspace) &
-         fmt->colorspace_mask))
+     if (f->fmt.pix_mp.colorspace >= BITS_PER_LONG ||
+         !(V4L2_COLORSPACE_MASK(f->fmt.pix_mp.colorspace) &
+           fmt->colorspace_mask))
              f->fmt.pix_mp.colorspace = fmt->colorspace_default;

Isn't it better handled in the macro definition itself so that future usages of the V4L2_COLORSPACE_MASK() macro won't need to be protected like this one ?

Would this silence the smatch warning ?

Review

Quoted 3 lines
(quoted diff) +2 -1
-#define V4L2_COLORSPACE_MASK(colorspace) BIT(colorspace)
+#define V4L2_COLORSPACE_MASK(c)        BIT((c) < V4L2_COLORSPACE_LAST ? \
+                                           (c) : V4L2_COLORSPACE_LAST)

I did consider this, but did not like the undesired behavior it would introduce. With your suggested change above, we now switch the color space to an unsupported value (V4L2_COLORSPACE_DCI_P3, assuming we shift by V4L2_COLORSPACE_LAST - 1) if the user passed an invalid value. Of course, this does subsequently get fixed when used in pispbe_try_format(), but not for the usage in pisp_be_formats.h. In my original change, we revert to the default for the requested format instead. Although, perhaps my test should be if (!v4l2_is_colorspace_valid(f->fmt.pix_mp.colorspace) ... instead.

I'm ok with changing the macro considering the UAPI is unlikely to change, and usage in pisp_be_formats.h will likely not hit the problem any time. Thoughts?

Naush

Quoted 7 lines
Quoted 7 lines
drivers/media/platform/raspberrypi/pisp_be/pisp_be.c +0 -1
      /* In all cases, we only support the defaults for these: */
--

2.34.1

maintainer
#3 lore raw

Hi Naush

Quoted 43 lines · Naushir Patuck

Hi Jacopo,

Quoted 39 lines · Jacopo Mondi

Hi Naush

Quoted 7 lines · Naushir Patuck

Ensure that the user requested colorspace value does not wrap when using the V4L2_COLORSPACE_MASK() macro. If the requested colorspace value >= BIT_PER_LONG, revert to the default colorspace for the given format.

Thanks for handling this

Quoted 9 lines
---
 drivers/media/platform/raspberrypi/pisp_be/pisp_be.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/media/platform/raspberrypi/pisp_be/pisp_be.c +0 -0
index e74df5b116dc..bd5d77c691d3 100644

drivers/media/platform/raspberrypi/pisp_be/pisp_be.c static void pispbe_try_format(struct v4l2_format *f, struct pispbe_node *node) · line 1130

Quoted 9 lines
drivers/media/platform/raspberrypi/pisp_be/pisp_be.c +3 -2
@@ -1124,8 +1124,9 @@ static void pispbe_try_format(struct v4l2_format *f, struct pispbe_node *node)
       * not supported. This also catches the case when the "default"
       * colour space was requested (as that's never in the mask).
       */
-     if (!(V4L2_COLORSPACE_MASK(f->fmt.pix_mp.colorspace) &
-         fmt->colorspace_mask))
+     if (f->fmt.pix_mp.colorspace >= BITS_PER_LONG ||
+         !(V4L2_COLORSPACE_MASK(f->fmt.pix_mp.colorspace) &
+           fmt->colorspace_mask))
              f->fmt.pix_mp.colorspace = fmt->colorspace_default;

Isn't it better handled in the macro definition itself so that future usages of the V4L2_COLORSPACE_MASK() macro won't need to be protected like this one ?

Would this silence the smatch warning ?

Quoted 14 lines

Review

Quoted 4 lines
(quoted diff) +2 -1
-#define V4L2_COLORSPACE_MASK(colorspace) BIT(colorspace)
+#define V4L2_COLORSPACE_MASK(c)        BIT((c) < V4L2_COLORSPACE_LAST ? \
+                                           (c) : V4L2_COLORSPACE_LAST)

I did consider this, but did not like the undesired behavior it would introduce. With your suggested change above, we now switch the color space to an unsupported value (V4L2_COLORSPACE_DCI_P3, assuming we shift by V4L2_COLORSPACE_LAST - 1) if the user passed an invalid value. Of course, this does subsequently get fixed when used in pispbe_try_format(), but not for the usage in pisp_be_formats.h. In my original change, we revert to the default for the requested format instead. Although, perhaps my test should be if (!v4l2_is_colorspace_valid(f->fmt.pix_mp.colorspace) ... instead.

Keep in mind the core 'sanitizes' the colorspace for you (see the usage of v4l_sanitize_colorspace() in drivers/media/v4l2-core/v4l2-ioctl.c) so a fix is need only to silence smatch not to actually address any run-time issue.

Quoted 4 lines

I'm ok with changing the macro considering the UAPI is unlikely to change, and usage in pisp_be_formats.h will likely not hit the problem any time. Thoughts?

We could re-use v4l_sanitize_colorspace() but again, this change only serve to please smatch.

Thanks
j

Quoted 10 lines

Naush

Quoted 7 lines
Quoted 7 lines
drivers/media/platform/raspberrypi/pisp_be/pisp_be.c +0 -1
      /* In all cases, we only support the defaults for these: */
--

2.34.1

#4 lore raw
Quoted 10 lines · Jacopo Mondi
Quoted 9 lines

I did consider this, but did not like the undesired behavior it would introduce. With your suggested change above, we now switch the color space to an unsupported value (V4L2_COLORSPACE_DCI_P3, assuming we shift by V4L2_COLORSPACE_LAST - 1) if the user passed an invalid value. Of course, this does subsequently get fixed when used in pispbe_try_format(), but not for the usage in pisp_be_formats.h. In my original change, we revert to the default for the requested format instead. Although, perhaps my test should be if (!v4l2_is_colorspace_valid(f->fmt.pix_mp.colorspace) ... instead.

Quoted 5 lines

Keep in mind the core 'sanitizes' the colorspace for you (see the usage of v4l_sanitize_colorspace() in drivers/media/v4l2-core/v4l2-ioctl.c) so a fix is need only to silence smatch not to actually address any run-time issue.

Ah. That's true. Let's just ignore this warning in that case...

Smatch would have an easier time parsing these if we passed p instead of arg to return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg) in v4l_try_fmt().

	struct v4l2_format *p = arg;

	v4l_sanitize_format(p);

	return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);

I can just filter out that call to say that when v4l_try_fmt() calls ops->vidioc_try_fmt_vid_cap_mplane() "arg" variable is trusted data.

regards,
dan carpenter

#5 lore raw
Quoted 24 lines · Dan Carpenter
Quoted 10 lines · Jacopo Mondi
Quoted 9 lines

I did consider this, but did not like the undesired behavior it would introduce. With your suggested change above, we now switch the color space to an unsupported value (V4L2_COLORSPACE_DCI_P3, assuming we shift by V4L2_COLORSPACE_LAST - 1) if the user passed an invalid value. Of course, this does subsequently get fixed when used in pispbe_try_format(), but not for the usage in pisp_be_formats.h. In my original change, we revert to the default for the requested format instead. Although, perhaps my test should be if (!v4l2_is_colorspace_valid(f->fmt.pix_mp.colorspace) ... instead.

Quoted 5 lines

Keep in mind the core 'sanitizes' the colorspace for you (see the usage of v4l_sanitize_colorspace() in drivers/media/v4l2-core/v4l2-ioctl.c) so a fix is need only to silence smatch not to actually address any run-time issue.

Ah. That's true. Let's just ignore this warning in that case...

Smatch would have an easier time parsing these if we passed p instead of arg to return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg) in v4l_try_fmt().

Quoted 8 lines
        struct v4l2_format *p = arg;

        v4l_sanitize_format(p);

        return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);

I can just filter out that call to say that when v4l_try_fmt() calls ops->vidioc_try_fmt_vid_cap_mplane() "arg" variable is trusted data.

Sounds good, I'll drop this patch then.

Naush