public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/6] media: Fix new smatch warnings
@ 2026-05-01 11:32 Ricardo Ribalda
  2026-05-01 11:32 ` [PATCH v2 1/6] media: v4l2-dev: Add range check for vdev->minor Ricardo Ribalda
                   ` (5 more replies)
  0 siblings, 6 replies; 13+ messages in thread
From: Ricardo Ribalda @ 2026-05-01 11:32 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Laurent Pinchart, Sakari Ailus,
	Hans Verkuil, Nas Chung, Jackson Lee, Bingbu Cao, Tianshu Qiu,
	Greg Kroah-Hartman, Keke Li, Yong Zhi, Jacopo Mondi
  Cc: linux-media, linux-kernel, linux-staging, Mauro Carvalho Chehab,
	Ricardo Ribalda, Hans Verkuil, stable

Current version of smatch triggers some warnings for the media tree.
Most of them are inoffensive, but we would like to have zero smatch
warnings.

drivers/media/platform/amlogic/c3/isp/c3-isp-params.c:111 c3_isp_params_awb_wt() error: buffer overflow 'cfg->zone_weight' 768 <= u32max
drivers/media/platform/amlogic/c3/isp/c3-isp-params.c:111 c3_isp_params_awb_wt() error: buffer overflow 'cfg->zone_weight' 768 <= u32max
drivers/media/platform/amlogic/c3/isp/c3-isp-params.c:227 c3_isp_params_ae_wt() error: buffer overflow 'cfg->zone_weight' 255 <= u32max
drivers/media/platform/amlogic/c3/isp/c3-isp-params.c:227 c3_isp_params_ae_wt() error: buffer overflow 'cfg->zone_weight' 255 <= u32max
drivers/media/v4l2-core/v4l2-dev.c:1036 __video_register_device() error: buffer overflow 'video_devices' 256 <= 288
drivers/media/v4l2-core/v4l2-dev.c:1043 __video_register_device() error: buffer overflow 'video_devices' 256 <= 288
drivers/media/v4l2-core/v4l2-dev.c:1101 __video_register_device() error: buffer overflow 'video_devices' 256 <= 288
drivers/media/platform/chips-media/wave5/wave5-vpuapi.c:588 wave5_vpu_dec_get_output_info() error: buffer overflow 'inst->frame_buf' 64 <= 127
drivers/staging/media/ipu3/ipu3-css-params.c:1792 imgu_css_cfg_acc_stripe() warn: 'acc->stripe.bds_out_stripes[0]->width - 2 * f' 4294967168 can't fit into 65535 'acc->stripe.bds_out_stripes[1]->offset'
drivers/media/i2c/adv7604.c:3672 adv76xx_probe() error: buffer overflow 'state->pads' 7 <= 4294967294
drivers/media/i2c/adv7604.c:3673 adv76xx_probe() error: buffer overflow 'state->pads' 7 <= u32max
drivers/media/i2c/mt9p031.c:799 mt9p031_s_ctrl() warn: assigning (-1952) to unsigned variable 'data'

Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
Changes in v2:
- Remove WARN_ON() in user triggerable checks.
- Add fixes for user triggerable errors.
- Remove pr_err in v4l-dev
- Link to v1: https://lore.kernel.org/r/20260428-smatch-7-1-v1-0-46890dffb611@chromium.org

---
Ricardo Ribalda (6):
      media: v4l2-dev: Add range check for vdev->minor
      media: i2c: mt9p031: Rewrite a bitwise mask
      media: i2c: adv7604: Add range checks for chip info
      media: chips-media: wave5: Add range checks for dec_output_info
      media: staging: ipu3-imgu: Add range check for imgu_css_cfg_acc_stripe
      media: amlogic-c3: Add validations for ae and awb config

 drivers/media/i2c/adv7604.c                             |  6 ++++++
 drivers/media/i2c/mt9p031.c                             |  2 +-
 drivers/media/platform/amlogic/c3/isp/c3-isp-params.c   |  4 ++++
 drivers/media/platform/chips-media/wave5/wave5-vpuapi.c | 11 +++++++++--
 drivers/media/v4l2-core/v4l2-dev.c                      |  5 +++++
 drivers/staging/media/ipu3/ipu3-css-params.c            |  8 ++++++--
 6 files changed, 31 insertions(+), 5 deletions(-)
---
base-commit: 254f49634ee16a731174d2ae34bc50bd5f45e731
change-id: 20260428-smatch-7-1-d969299dd3cf

Best regards,
-- 
Ricardo Ribalda <ribalda@chromium.org>


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

* [PATCH v2 1/6] media: v4l2-dev: Add range check for vdev->minor
  2026-05-01 11:32 [PATCH v2 0/6] media: Fix new smatch warnings Ricardo Ribalda
@ 2026-05-01 11:32 ` Ricardo Ribalda
  2026-05-04  8:12   ` Sakari Ailus
  2026-05-01 11:32 ` [PATCH v2 2/6] media: i2c: mt9p031: Rewrite a bitwise mask Ricardo Ribalda
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 13+ messages in thread
From: Ricardo Ribalda @ 2026-05-01 11:32 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Laurent Pinchart, Sakari Ailus,
	Hans Verkuil, Nas Chung, Jackson Lee, Bingbu Cao, Tianshu Qiu,
	Greg Kroah-Hartman, Keke Li, Yong Zhi, Jacopo Mondi
  Cc: linux-media, linux-kernel, linux-staging, Mauro Carvalho Chehab,
	Ricardo Ribalda

If the fixed minor ranges are not properly set we could end up in a
situation where the calculated minor is invalid. Add a check for this in
the code to make it more robust.

This check also fixes the following false positive smatch warning:

drivers/media/v4l2-core/v4l2-dev.c:1036 __video_register_device() error: buffer overflow 'video_devices' 256 <= 288
drivers/media/v4l2-core/v4l2-dev.c:1043 __video_register_device() error: buffer overflow 'video_devices' 256 <= 288
drivers/media/v4l2-core/v4l2-dev.c:1101 __video_register_device() error: buffer overflow 'video_devices' 256 <= 288

Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
 drivers/media/v4l2-core/v4l2-dev.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/media/v4l2-core/v4l2-dev.c b/drivers/media/v4l2-core/v4l2-dev.c
index 6ce623a1245a..5516b2bbb08f 100644
--- a/drivers/media/v4l2-core/v4l2-dev.c
+++ b/drivers/media/v4l2-core/v4l2-dev.c
@@ -1032,6 +1032,11 @@ int __video_register_device(struct video_device *vdev,
 	vdev->minor = i + minor_offset;
 	vdev->num = nr;
 
+	if (WARN_ON(vdev->minor >= VIDEO_NUM_DEVICES)) {
+		mutex_unlock(&videodev_lock);
+		return -EINVAL;
+	}
+
 	/* Should not happen since we thought this minor was free */
 	if (WARN_ON(video_devices[vdev->minor])) {
 		mutex_unlock(&videodev_lock);

-- 
2.54.0.545.g6539524ca2-goog


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

* [PATCH v2 2/6] media: i2c: mt9p031: Rewrite a bitwise mask
  2026-05-01 11:32 [PATCH v2 0/6] media: Fix new smatch warnings Ricardo Ribalda
  2026-05-01 11:32 ` [PATCH v2 1/6] media: v4l2-dev: Add range check for vdev->minor Ricardo Ribalda
@ 2026-05-01 11:32 ` Ricardo Ribalda
  2026-05-01 20:19   ` Laurent Pinchart
  2026-05-01 11:32 ` [PATCH v2 3/6] media: i2c: adv7604: Add range checks for chip info Ricardo Ribalda
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 13+ messages in thread
From: Ricardo Ribalda @ 2026-05-01 11:32 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Laurent Pinchart, Sakari Ailus,
	Hans Verkuil, Nas Chung, Jackson Lee, Bingbu Cao, Tianshu Qiu,
	Greg Kroah-Hartman, Keke Li, Yong Zhi, Jacopo Mondi
  Cc: linux-media, linux-kernel, linux-staging, Mauro Carvalho Chehab,
	Ricardo Ribalda

The current code makes smatch a bit uncomfortable:
drivers/media/i2c/mt9p031.c:799 mt9p031_s_ctrl() warn: assigning (-1952) to unsigned variable 'data'

Probably because smatch is not clever enough (yet). Do a simple rewrite
to make sure that smatch understands what we are doing here.

Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
 drivers/media/i2c/mt9p031.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/media/i2c/mt9p031.c b/drivers/media/i2c/mt9p031.c
index ea5d43d925ff..5c9dff030b4d 100644
--- a/drivers/media/i2c/mt9p031.c
+++ b/drivers/media/i2c/mt9p031.c
@@ -795,7 +795,7 @@ static int mt9p031_s_ctrl(struct v4l2_ctrl *ctrl)
 			ctrl->val &= ~1;
 			data = (1 << 6) | (ctrl->val >> 1);
 		} else {
-			ctrl->val &= ~7;
+			ctrl->val -= ctrl->val % 8;
 			data = ((ctrl->val - 64) << 5) | (1 << 6) | 32;
 		}
 

-- 
2.54.0.545.g6539524ca2-goog


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

* [PATCH v2 3/6] media: i2c: adv7604: Add range checks for chip info
  2026-05-01 11:32 [PATCH v2 0/6] media: Fix new smatch warnings Ricardo Ribalda
  2026-05-01 11:32 ` [PATCH v2 1/6] media: v4l2-dev: Add range check for vdev->minor Ricardo Ribalda
  2026-05-01 11:32 ` [PATCH v2 2/6] media: i2c: mt9p031: Rewrite a bitwise mask Ricardo Ribalda
@ 2026-05-01 11:32 ` Ricardo Ribalda
  2026-05-01 11:32 ` [PATCH v2 4/6] media: chips-media: wave5: Add range checks for dec_output_info Ricardo Ribalda
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 13+ messages in thread
From: Ricardo Ribalda @ 2026-05-01 11:32 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Laurent Pinchart, Sakari Ailus,
	Hans Verkuil, Nas Chung, Jackson Lee, Bingbu Cao, Tianshu Qiu,
	Greg Kroah-Hartman, Keke Li, Yong Zhi, Jacopo Mondi
  Cc: linux-media, linux-kernel, linux-staging, Mauro Carvalho Chehab,
	Ricardo Ribalda, Hans Verkuil

If the driver's chip information is invalid we can end up accessing an
invalid memory region.

This fixes the following false positive smatch errors:
drivers/media/i2c/adv7604.c:3672 adv76xx_probe() error: buffer overflow 'state->pads' 7 <= 4294967294
drivers/media/i2c/adv7604.c:3673 adv76xx_probe() error: buffer overflow 'state->pads' 7 <= u32max

Reviewed-by: Hans Verkuil <hverkuil+cisco@kernel.org>
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
 drivers/media/i2c/adv7604.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/media/i2c/adv7604.c b/drivers/media/i2c/adv7604.c
index 67116a4ef134..ae75982fb514 100644
--- a/drivers/media/i2c/adv7604.c
+++ b/drivers/media/i2c/adv7604.c
@@ -3668,6 +3668,12 @@ static int adv76xx_probe(struct i2c_client *client)
 
 	state->source_pad = state->info->num_dv_ports
 			  + (state->info->has_afe ? 2 : 0);
+	if (WARN_ON(state->source_pad >= ADV76XX_PAD_MAX)) {
+		err = -EINVAL;
+		v4l2_err(sd, "invalid chip info\n");
+		goto err_i2c;
+	}
+
 	for (i = 0; i < state->source_pad; ++i)
 		state->pads[i].flags = MEDIA_PAD_FL_SINK;
 	state->pads[state->source_pad].flags = MEDIA_PAD_FL_SOURCE;

-- 
2.54.0.545.g6539524ca2-goog


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

* [PATCH v2 4/6] media: chips-media: wave5: Add range checks for dec_output_info
  2026-05-01 11:32 [PATCH v2 0/6] media: Fix new smatch warnings Ricardo Ribalda
                   ` (2 preceding siblings ...)
  2026-05-01 11:32 ` [PATCH v2 3/6] media: i2c: adv7604: Add range checks for chip info Ricardo Ribalda
@ 2026-05-01 11:32 ` Ricardo Ribalda
  2026-05-01 11:32 ` [PATCH v2 5/6] media: staging: ipu3-imgu: Add range check for imgu_css_cfg_acc_stripe Ricardo Ribalda
  2026-05-01 11:32 ` [PATCH v2 6/6] media: amlogic-c3: Add validations for ae and awb config Ricardo Ribalda
  5 siblings, 0 replies; 13+ messages in thread
From: Ricardo Ribalda @ 2026-05-01 11:32 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Laurent Pinchart, Sakari Ailus,
	Hans Verkuil, Nas Chung, Jackson Lee, Bingbu Cao, Tianshu Qiu,
	Greg Kroah-Hartman, Keke Li, Yong Zhi, Jacopo Mondi
  Cc: linux-media, linux-kernel, linux-staging, Mauro Carvalho Chehab,
	Ricardo Ribalda

If the driver's dec_output_info contains invalid data the driver can
write in invalid memory. Add a range check for that.

This fixes this smatch error:
drivers/media/platform/chips-media/wave5/wave5-vpuapi.c:588 wave5_vpu_dec_get_output_info() error: buffer overflow 'inst->frame_buf' 64 <= 127

Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
 drivers/media/platform/chips-media/wave5/wave5-vpuapi.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/drivers/media/platform/chips-media/wave5/wave5-vpuapi.c b/drivers/media/platform/chips-media/wave5/wave5-vpuapi.c
index d26ffc942219..f77abd5e122a 100644
--- a/drivers/media/platform/chips-media/wave5/wave5-vpuapi.c
+++ b/drivers/media/platform/chips-media/wave5/wave5-vpuapi.c
@@ -584,8 +584,15 @@ int wave5_vpu_dec_get_output_info(struct vpu_instance *inst, struct dec_output_i
 		p_dec_info->num_of_decoding_fbs : p_dec_info->num_of_display_fbs;
 
 	if (info->index_frame_display >= 0 &&
-	    info->index_frame_display < (int)max_dec_index)
-		info->disp_frame = inst->frame_buf[val + info->index_frame_display];
+	    info->index_frame_display < (int)max_dec_index) {
+		u32 idx = val + info->index_frame_display;
+
+		if (WARN_ON(idx >= MAX_REG_FRAME)) {
+			ret = -EINVAL;
+			goto err_out;
+		}
+		info->disp_frame = inst->frame_buf[idx];
+	}
 
 	info->rd_ptr = p_dec_info->stream_rd_ptr;
 	info->wr_ptr = p_dec_info->stream_wr_ptr;

-- 
2.54.0.545.g6539524ca2-goog


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

* [PATCH v2 5/6] media: staging: ipu3-imgu: Add range check for imgu_css_cfg_acc_stripe
  2026-05-01 11:32 [PATCH v2 0/6] media: Fix new smatch warnings Ricardo Ribalda
                   ` (3 preceding siblings ...)
  2026-05-01 11:32 ` [PATCH v2 4/6] media: chips-media: wave5: Add range checks for dec_output_info Ricardo Ribalda
@ 2026-05-01 11:32 ` Ricardo Ribalda
  2026-05-04  8:22   ` Sakari Ailus
  2026-05-01 11:32 ` [PATCH v2 6/6] media: amlogic-c3: Add validations for ae and awb config Ricardo Ribalda
  5 siblings, 1 reply; 13+ messages in thread
From: Ricardo Ribalda @ 2026-05-01 11:32 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Laurent Pinchart, Sakari Ailus,
	Hans Verkuil, Nas Chung, Jackson Lee, Bingbu Cao, Tianshu Qiu,
	Greg Kroah-Hartman, Keke Li, Yong Zhi, Jacopo Mondi
  Cc: linux-media, linux-kernel, linux-staging, Mauro Carvalho Chehab,
	Ricardo Ribalda, stable

If the driver's stripe information is invalid it can result in an integer
overflow. Add a range check with a WARN_ON to expose this kind of
error.

This patch fixes the following smatch error:
drivers/staging/media/ipu3/ipu3-css-params.c:1792 imgu_css_cfg_acc_stripe() warn: 'acc->stripe.bds_out_stripes[0]->width - 2 * f' 4294967168 can't fit into 65535 'acc->stripe.bds_out_stripes[1]->offset'

Cc: stable@vger.kernel.org
Fixes: e11110a5b744 ("media: staging/intel-ipu3: css: Compute and program ccs")
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
 drivers/staging/media/ipu3/ipu3-css-params.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/staging/media/ipu3/ipu3-css-params.c b/drivers/staging/media/ipu3/ipu3-css-params.c
index 2c48d57a3180..92cce31e35c5 100644
--- a/drivers/staging/media/ipu3/ipu3-css-params.c
+++ b/drivers/staging/media/ipu3/ipu3-css-params.c
@@ -1770,6 +1770,8 @@ static int imgu_css_cfg_acc_stripe(struct imgu_css *css, unsigned int pipe,
 		acc->stripe.bds_out_stripes[0].width =
 			ALIGN(css_pipe->rect[IPU3_CSS_RECT_BDS].width, f);
 	} else {
+		u32 offset;
+
 		/* Image processing is divided into two stripes */
 		acc->stripe.bds_out_stripes[0].width =
 			acc->stripe.bds_out_stripes[1].width =
@@ -1788,8 +1790,10 @@ static int imgu_css_cfg_acc_stripe(struct imgu_css *css, unsigned int pipe,
 			acc->stripe.bds_out_stripes[1].width += f;
 		}
 		/* Overlap between stripes is IPU3_UAPI_ISP_VEC_ELEMS * 4 */
-		acc->stripe.bds_out_stripes[1].offset =
-			acc->stripe.bds_out_stripes[0].width - 2 * f;
+		offset = acc->stripe.bds_out_stripes[0].width - 2 * f;
+		if (offset > 65535)
+			return -EINVAL;
+		acc->stripe.bds_out_stripes[1].offset = offset;
 	}
 
 	acc->stripe.effective_stripes[0].height =

-- 
2.54.0.545.g6539524ca2-goog


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

* [PATCH v2 6/6] media: amlogic-c3: Add validations for ae and awb config
  2026-05-01 11:32 [PATCH v2 0/6] media: Fix new smatch warnings Ricardo Ribalda
                   ` (4 preceding siblings ...)
  2026-05-01 11:32 ` [PATCH v2 5/6] media: staging: ipu3-imgu: Add range check for imgu_css_cfg_acc_stripe Ricardo Ribalda
@ 2026-05-01 11:32 ` Ricardo Ribalda
  5 siblings, 0 replies; 13+ messages in thread
From: Ricardo Ribalda @ 2026-05-01 11:32 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Laurent Pinchart, Sakari Ailus,
	Hans Verkuil, Nas Chung, Jackson Lee, Bingbu Cao, Tianshu Qiu,
	Greg Kroah-Hartman, Keke Li, Yong Zhi, Jacopo Mondi
  Cc: linux-media, linux-kernel, linux-staging, Mauro Carvalho Chehab,
	Ricardo Ribalda, stable

Avoid invalid memory access if the zones_num is bigger than
zone_weight.

This patch fixes the following smatch errors:
drivers/media/platform/amlogic/c3/isp/c3-isp-params.c:111 c3_isp_params_awb_wt() error: buffer overflow 'cfg->zone_weight' 768 <= u32max
drivers/media/platform/amlogic/c3/isp/c3-isp-params.c:111 c3_isp_params_awb_wt() error: buffer overflow 'cfg->zone_weight' 768 <= u32max
drivers/media/platform/amlogic/c3/isp/c3-isp-params.c:227 c3_isp_params_ae_wt() error: buffer overflow 'cfg->zone_weight' 255 <= u32max
drivers/media/platform/amlogic/c3/isp/c3-isp-params.c:227 c3_isp_params_ae_wt() error: buffer overflow 'cfg->zone_weight' 255 <= u32max

Cc: stable@vger.kernel.org
Fixes: fb2e135208f3 ("media: platform: Add C3 ISP driver")
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
 drivers/media/platform/amlogic/c3/isp/c3-isp-params.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/media/platform/amlogic/c3/isp/c3-isp-params.c b/drivers/media/platform/amlogic/c3/isp/c3-isp-params.c
index 6f9ca7a7dd88..aec3eed0e443 100644
--- a/drivers/media/platform/amlogic/c3/isp/c3-isp-params.c
+++ b/drivers/media/platform/amlogic/c3/isp/c3-isp-params.c
@@ -104,6 +104,8 @@ static void c3_isp_params_awb_wt(struct c3_isp_device *isp,
 	c3_isp_write(isp, ISP_AWB_BLK_WT_ADDR, 0);
 
 	zones_num = cfg->horiz_zones_num * cfg->vert_zones_num;
+	if (zones_num > C3_ISP_AWB_MAX_ZONES)
+		zones_num = C3_ISP_AWB_MAX_ZONES;
 
 	/* Need to write 8 weights at once */
 	for (i = 0; i < zones_num / 8; i++) {
@@ -220,6 +222,8 @@ static void c3_isp_params_ae_wt(struct c3_isp_device *isp,
 	c3_isp_write(isp, ISP_AE_BLK_WT_ADDR, 0);
 
 	zones_num = cfg->horiz_zones_num * cfg->vert_zones_num;
+	if (zones_num > C3_ISP_AE_MAX_ZONES)
+		zones_num = C3_ISP_AE_MAX_ZONES;
 
 	/* Need to write 8 weights at once */
 	for (i = 0; i < zones_num / 8; i++) {

-- 
2.54.0.545.g6539524ca2-goog


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

* Re: [PATCH v2 2/6] media: i2c: mt9p031: Rewrite a bitwise mask
  2026-05-01 11:32 ` [PATCH v2 2/6] media: i2c: mt9p031: Rewrite a bitwise mask Ricardo Ribalda
@ 2026-05-01 20:19   ` Laurent Pinchart
  2026-05-02  7:56     ` Ricardo Ribalda
  0 siblings, 1 reply; 13+ messages in thread
From: Laurent Pinchart @ 2026-05-01 20:19 UTC (permalink / raw)
  To: Ricardo Ribalda
  Cc: Mauro Carvalho Chehab, Sakari Ailus, Hans Verkuil, Nas Chung,
	Jackson Lee, Bingbu Cao, Tianshu Qiu, Greg Kroah-Hartman, Keke Li,
	Yong Zhi, Jacopo Mondi, linux-media, linux-kernel, linux-staging,
	Mauro Carvalho Chehab

On Fri, May 01, 2026 at 11:32:47AM +0000, Ricardo Ribalda wrote:
> The current code makes smatch a bit uncomfortable:
> drivers/media/i2c/mt9p031.c:799 mt9p031_s_ctrl() warn: assigning (-1952) to unsigned variable 'data'
> 
> Probably because smatch is not clever enough (yet). Do a simple rewrite
> to make sure that smatch understands what we are doing here.
> 
> Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> ---
>  drivers/media/i2c/mt9p031.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/media/i2c/mt9p031.c b/drivers/media/i2c/mt9p031.c
> index ea5d43d925ff..5c9dff030b4d 100644
> --- a/drivers/media/i2c/mt9p031.c
> +++ b/drivers/media/i2c/mt9p031.c
> @@ -795,7 +795,7 @@ static int mt9p031_s_ctrl(struct v4l2_ctrl *ctrl)
>  			ctrl->val &= ~1;
>  			data = (1 << 6) | (ctrl->val >> 1);
>  		} else {
> -			ctrl->val &= ~7;
> +			ctrl->val -= ctrl->val % 8;
>  			data = ((ctrl->val - 64) << 5) | (1 << 6) | 32;

I'd still like to keep the ~7 (and, while at it, making the register
computation easier to read). I previously proposed

			ctrl->val &= ~7;
			data = (ctrl->val - 64) >> 3;
			data = (data << 8) | (1 << 6) | 32;

which didn't quite appease smatch. We could use an explicit mask:

			ctrl->val &= ~7;
			data = ((ctrl->val - 64) >> 3) & 0xff;
			data = (data << 8) | (1 << 6) | 32;

>  		}
>  
> 

-- 
Regards,

Laurent Pinchart

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

* Re: [PATCH v2 2/6] media: i2c: mt9p031: Rewrite a bitwise mask
  2026-05-01 20:19   ` Laurent Pinchart
@ 2026-05-02  7:56     ` Ricardo Ribalda
  2026-05-02 16:54       ` Laurent Pinchart
  0 siblings, 1 reply; 13+ messages in thread
From: Ricardo Ribalda @ 2026-05-02  7:56 UTC (permalink / raw)
  To: Laurent Pinchart
  Cc: Mauro Carvalho Chehab, Sakari Ailus, Hans Verkuil, Nas Chung,
	Jackson Lee, Bingbu Cao, Tianshu Qiu, Greg Kroah-Hartman, Keke Li,
	Yong Zhi, Jacopo Mondi, linux-media, linux-kernel, linux-staging,
	Mauro Carvalho Chehab

Hi Laurent

On Fri, 1 May 2026 at 22:19, Laurent Pinchart
<laurent.pinchart@ideasonboard.com> wrote:
>
> On Fri, May 01, 2026 at 11:32:47AM +0000, Ricardo Ribalda wrote:
> > The current code makes smatch a bit uncomfortable:
> > drivers/media/i2c/mt9p031.c:799 mt9p031_s_ctrl() warn: assigning (-1952) to unsigned variable 'data'
> >
> > Probably because smatch is not clever enough (yet). Do a simple rewrite
> > to make sure that smatch understands what we are doing here.
> >
> > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> > ---
> >  drivers/media/i2c/mt9p031.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/drivers/media/i2c/mt9p031.c b/drivers/media/i2c/mt9p031.c
> > index ea5d43d925ff..5c9dff030b4d 100644
> > --- a/drivers/media/i2c/mt9p031.c
> > +++ b/drivers/media/i2c/mt9p031.c
> > @@ -795,7 +795,7 @@ static int mt9p031_s_ctrl(struct v4l2_ctrl *ctrl)
> >                       ctrl->val &= ~1;
> >                       data = (1 << 6) | (ctrl->val >> 1);
> >               } else {
> > -                     ctrl->val &= ~7;
> > +                     ctrl->val -= ctrl->val % 8;
> >                       data = ((ctrl->val - 64) << 5) | (1 << 6) | 32;
>
> I'd still like to keep the ~7 (and, while at it, making the register
> computation easier to read). I previously proposed
>
>                         ctrl->val &= ~7;
>                         data = (ctrl->val - 64) >> 3;
>                         data = (data << 8) | (1 << 6) | 32;
>
> which didn't quite appease smatch. We could use an explicit mask:
>
>                         ctrl->val &= ~7;
>                         data = ((ctrl->val - 64) >> 3) & 0xff;

Why 0xff and not 0x7f?

If I understand it correctly the max is 1024 and (1024-64) >> 3 is < 127.


Anyway... following the mask idea what about:

ctrl->val &= ~7;
data = (((ctrl->val - 64) & 0x3ff) << 5) | (3 << 5);

?


>                         data = (data << 8) | (1 << 6) | 32;
>
> >               }
> >
> >
>
> --
> Regards,
>
> Laurent Pinchart



-- 
Ricardo Ribalda

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

* Re: [PATCH v2 2/6] media: i2c: mt9p031: Rewrite a bitwise mask
  2026-05-02  7:56     ` Ricardo Ribalda
@ 2026-05-02 16:54       ` Laurent Pinchart
  0 siblings, 0 replies; 13+ messages in thread
From: Laurent Pinchart @ 2026-05-02 16:54 UTC (permalink / raw)
  To: Ricardo Ribalda
  Cc: Mauro Carvalho Chehab, Sakari Ailus, Hans Verkuil, Nas Chung,
	Jackson Lee, Bingbu Cao, Tianshu Qiu, Greg Kroah-Hartman, Keke Li,
	Yong Zhi, Jacopo Mondi, linux-media, linux-kernel, linux-staging,
	Mauro Carvalho Chehab

On Sat, May 02, 2026 at 09:56:19AM +0200, Ricardo Ribalda wrote:
> On Fri, 1 May 2026 at 22:19, Laurent Pinchart wrote:
> > On Fri, May 01, 2026 at 11:32:47AM +0000, Ricardo Ribalda wrote:
> > > The current code makes smatch a bit uncomfortable:
> > > drivers/media/i2c/mt9p031.c:799 mt9p031_s_ctrl() warn: assigning (-1952) to unsigned variable 'data'
> > >
> > > Probably because smatch is not clever enough (yet). Do a simple rewrite
> > > to make sure that smatch understands what we are doing here.
> > >
> > > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> > > ---
> > >  drivers/media/i2c/mt9p031.c | 2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/drivers/media/i2c/mt9p031.c b/drivers/media/i2c/mt9p031.c
> > > index ea5d43d925ff..5c9dff030b4d 100644
> > > --- a/drivers/media/i2c/mt9p031.c
> > > +++ b/drivers/media/i2c/mt9p031.c
> > > @@ -795,7 +795,7 @@ static int mt9p031_s_ctrl(struct v4l2_ctrl *ctrl)
> > >                       ctrl->val &= ~1;
> > >                       data = (1 << 6) | (ctrl->val >> 1);
> > >               } else {
> > > -                     ctrl->val &= ~7;
> > > +                     ctrl->val -= ctrl->val % 8;
> > >                       data = ((ctrl->val - 64) << 5) | (1 << 6) | 32;
> >
> > I'd still like to keep the ~7 (and, while at it, making the register
> > computation easier to read). I previously proposed
> >
> >                         ctrl->val &= ~7;
> >                         data = (ctrl->val - 64) >> 3;
> >                         data = (data << 8) | (1 << 6) | 32;
> >
> > which didn't quite appease smatch. We could use an explicit mask:
> >
> >                         ctrl->val &= ~7;
> >                         data = ((ctrl->val - 64) >> 3) & 0xff;
> 
> Why 0xff and not 0x7f?
> 
> If I understand it correctly the max is 1024 and (1024-64) >> 3 is < 127.

You're right. This won't matter in practice given that the control value
is bound by the minimum and maximum gains specified when creating the
control.

> Anyway... following the mask idea what about:
> 
> ctrl->val &= ~7;
> data = (((ctrl->val - 64) & 0x3ff) << 5) | (3 << 5);
> 
> ?

The digital gain is a 7-bit value stored in bits [14:8]. As the value
has to be divided by 8, the driver shifts left by 5. This is correct,
but I find it confusing for the reader (including myself, I had to pause
when reading this patch to understand the code). Hence the proposal to
improve readability.

Separating the (1 << 6) and 32 is also meant to make the code readable.
Bit 6 is the 1-bit second analog gain stage, and bits [5:0] store the
first stage analog gain.

> >                         data = (data << 8) | (1 << 6) | 32;
> >
> > >               }
> > >
> > >

-- 
Regards,

Laurent Pinchart

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

* Re: [PATCH v2 1/6] media: v4l2-dev: Add range check for vdev->minor
  2026-05-01 11:32 ` [PATCH v2 1/6] media: v4l2-dev: Add range check for vdev->minor Ricardo Ribalda
@ 2026-05-04  8:12   ` Sakari Ailus
  0 siblings, 0 replies; 13+ messages in thread
From: Sakari Ailus @ 2026-05-04  8:12 UTC (permalink / raw)
  To: Ricardo Ribalda
  Cc: Mauro Carvalho Chehab, Laurent Pinchart, Hans Verkuil, Nas Chung,
	Jackson Lee, Bingbu Cao, Tianshu Qiu, Greg Kroah-Hartman, Keke Li,
	Yong Zhi, Jacopo Mondi, linux-media, linux-kernel, linux-staging,
	Mauro Carvalho Chehab

On Fri, May 01, 2026 at 11:32:46AM +0000, Ricardo Ribalda wrote:
> If the fixed minor ranges are not properly set we could end up in a
> situation where the calculated minor is invalid. Add a check for this in
> the code to make it more robust.
> 
> This check also fixes the following false positive smatch warning:
> 
> drivers/media/v4l2-core/v4l2-dev.c:1036 __video_register_device() error: buffer overflow 'video_devices' 256 <= 288
> drivers/media/v4l2-core/v4l2-dev.c:1043 __video_register_device() error: buffer overflow 'video_devices' 256 <= 288
> drivers/media/v4l2-core/v4l2-dev.c:1101 __video_register_device() error: buffer overflow 'video_devices' 256 <= 288
> 
> Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>

Reviewed-by: Sakari Ailus <sakari.ailus@linux.intel.com>

-- 
Sakari Ailus

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

* Re: [PATCH v2 5/6] media: staging: ipu3-imgu: Add range check for imgu_css_cfg_acc_stripe
  2026-05-01 11:32 ` [PATCH v2 5/6] media: staging: ipu3-imgu: Add range check for imgu_css_cfg_acc_stripe Ricardo Ribalda
@ 2026-05-04  8:22   ` Sakari Ailus
  2026-05-04  8:25     ` Ricardo Ribalda
  0 siblings, 1 reply; 13+ messages in thread
From: Sakari Ailus @ 2026-05-04  8:22 UTC (permalink / raw)
  To: Ricardo Ribalda
  Cc: Mauro Carvalho Chehab, Laurent Pinchart, Hans Verkuil, Nas Chung,
	Jackson Lee, Bingbu Cao, Tianshu Qiu, Greg Kroah-Hartman, Keke Li,
	Yong Zhi, Jacopo Mondi, linux-media, linux-kernel, linux-staging,
	Mauro Carvalho Chehab, stable

Hi Ricardo,

On Fri, May 01, 2026 at 11:32:50AM +0000, Ricardo Ribalda wrote:
> If the driver's stripe information is invalid it can result in an integer
> overflow. Add a range check with a WARN_ON to expose this kind of
> error.

This would be an underflow, not overflow. There's also no longer a
WARN_ON() here.

I presume this might not be the only such issue in the driver.

> 
> This patch fixes the following smatch error:
> drivers/staging/media/ipu3/ipu3-css-params.c:1792 imgu_css_cfg_acc_stripe() warn: 'acc->stripe.bds_out_stripes[0]->width - 2 * f' 4294967168 can't fit into 65535 'acc->stripe.bds_out_stripes[1]->offset'
> 
> Cc: stable@vger.kernel.org
> Fixes: e11110a5b744 ("media: staging/intel-ipu3: css: Compute and program ccs")
> Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> ---
>  drivers/staging/media/ipu3/ipu3-css-params.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/staging/media/ipu3/ipu3-css-params.c b/drivers/staging/media/ipu3/ipu3-css-params.c
> index 2c48d57a3180..92cce31e35c5 100644
> --- a/drivers/staging/media/ipu3/ipu3-css-params.c
> +++ b/drivers/staging/media/ipu3/ipu3-css-params.c
> @@ -1770,6 +1770,8 @@ static int imgu_css_cfg_acc_stripe(struct imgu_css *css, unsigned int pipe,
>  		acc->stripe.bds_out_stripes[0].width =
>  			ALIGN(css_pipe->rect[IPU3_CSS_RECT_BDS].width, f);
>  	} else {
> +		u32 offset;
> +
>  		/* Image processing is divided into two stripes */
>  		acc->stripe.bds_out_stripes[0].width =
>  			acc->stripe.bds_out_stripes[1].width =
> @@ -1788,8 +1790,10 @@ static int imgu_css_cfg_acc_stripe(struct imgu_css *css, unsigned int pipe,
>  			acc->stripe.bds_out_stripes[1].width += f;
>  		}
>  		/* Overlap between stripes is IPU3_UAPI_ISP_VEC_ELEMS * 4 */
> -		acc->stripe.bds_out_stripes[1].offset =
> -			acc->stripe.bds_out_stripes[0].width - 2 * f;
> +		offset = acc->stripe.bds_out_stripes[0].width - 2 * f;
> +		if (offset > 65535)
> +			return -EINVAL;
> +		acc->stripe.bds_out_stripes[1].offset = offset;
>  	}
>  
>  	acc->stripe.effective_stripes[0].height =
> 

-- 
Regards,

Sakari Ailus

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

* Re: [PATCH v2 5/6] media: staging: ipu3-imgu: Add range check for imgu_css_cfg_acc_stripe
  2026-05-04  8:22   ` Sakari Ailus
@ 2026-05-04  8:25     ` Ricardo Ribalda
  0 siblings, 0 replies; 13+ messages in thread
From: Ricardo Ribalda @ 2026-05-04  8:25 UTC (permalink / raw)
  To: Sakari Ailus
  Cc: Mauro Carvalho Chehab, Laurent Pinchart, Hans Verkuil, Nas Chung,
	Jackson Lee, Bingbu Cao, Tianshu Qiu, Greg Kroah-Hartman, Keke Li,
	Yong Zhi, Jacopo Mondi, linux-media, linux-kernel, linux-staging,
	Mauro Carvalho Chehab, stable

Hi Sakari

Thanks for the review

On Mon, 4 May 2026 at 10:22, Sakari Ailus <sakari.ailus@linux.intel.com> wrote:
>
> Hi Ricardo,
>
> On Fri, May 01, 2026 at 11:32:50AM +0000, Ricardo Ribalda wrote:
> > If the driver's stripe information is invalid it can result in an integer
> > overflow. Add a range check with a WARN_ON to expose this kind of
> > error.
>
> This would be an underflow, not overflow. There's also no longer a
> WARN_ON() here.
>
> I presume this might not be the only such issue in the driver.

I have updated the commit message in my local tree. Will repost in a
couple of days to allow more comments.

Regards!

>
> >
> > This patch fixes the following smatch error:
> > drivers/staging/media/ipu3/ipu3-css-params.c:1792 imgu_css_cfg_acc_stripe() warn: 'acc->stripe.bds_out_stripes[0]->width - 2 * f' 4294967168 can't fit into 65535 'acc->stripe.bds_out_stripes[1]->offset'
> >
> > Cc: stable@vger.kernel.org
> > Fixes: e11110a5b744 ("media: staging/intel-ipu3: css: Compute and program ccs")
> > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> > ---
> >  drivers/staging/media/ipu3/ipu3-css-params.c | 8 ++++++--
> >  1 file changed, 6 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/staging/media/ipu3/ipu3-css-params.c b/drivers/staging/media/ipu3/ipu3-css-params.c
> > index 2c48d57a3180..92cce31e35c5 100644
> > --- a/drivers/staging/media/ipu3/ipu3-css-params.c
> > +++ b/drivers/staging/media/ipu3/ipu3-css-params.c
> > @@ -1770,6 +1770,8 @@ static int imgu_css_cfg_acc_stripe(struct imgu_css *css, unsigned int pipe,
> >               acc->stripe.bds_out_stripes[0].width =
> >                       ALIGN(css_pipe->rect[IPU3_CSS_RECT_BDS].width, f);
> >       } else {
> > +             u32 offset;
> > +
> >               /* Image processing is divided into two stripes */
> >               acc->stripe.bds_out_stripes[0].width =
> >                       acc->stripe.bds_out_stripes[1].width =
> > @@ -1788,8 +1790,10 @@ static int imgu_css_cfg_acc_stripe(struct imgu_css *css, unsigned int pipe,
> >                       acc->stripe.bds_out_stripes[1].width += f;
> >               }
> >               /* Overlap between stripes is IPU3_UAPI_ISP_VEC_ELEMS * 4 */
> > -             acc->stripe.bds_out_stripes[1].offset =
> > -                     acc->stripe.bds_out_stripes[0].width - 2 * f;
> > +             offset = acc->stripe.bds_out_stripes[0].width - 2 * f;
> > +             if (offset > 65535)
> > +                     return -EINVAL;
> > +             acc->stripe.bds_out_stripes[1].offset = offset;
> >       }
> >
> >       acc->stripe.effective_stripes[0].height =
> >
>
> --
> Regards,
>
> Sakari Ailus



-- 
Ricardo Ribalda

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

end of thread, other threads:[~2026-05-04  8:25 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-01 11:32 [PATCH v2 0/6] media: Fix new smatch warnings Ricardo Ribalda
2026-05-01 11:32 ` [PATCH v2 1/6] media: v4l2-dev: Add range check for vdev->minor Ricardo Ribalda
2026-05-04  8:12   ` Sakari Ailus
2026-05-01 11:32 ` [PATCH v2 2/6] media: i2c: mt9p031: Rewrite a bitwise mask Ricardo Ribalda
2026-05-01 20:19   ` Laurent Pinchart
2026-05-02  7:56     ` Ricardo Ribalda
2026-05-02 16:54       ` Laurent Pinchart
2026-05-01 11:32 ` [PATCH v2 3/6] media: i2c: adv7604: Add range checks for chip info Ricardo Ribalda
2026-05-01 11:32 ` [PATCH v2 4/6] media: chips-media: wave5: Add range checks for dec_output_info Ricardo Ribalda
2026-05-01 11:32 ` [PATCH v2 5/6] media: staging: ipu3-imgu: Add range check for imgu_css_cfg_acc_stripe Ricardo Ribalda
2026-05-04  8:22   ` Sakari Ailus
2026-05-04  8:25     ` Ricardo Ribalda
2026-05-01 11:32 ` [PATCH v2 6/6] media: amlogic-c3: Add validations for ae and awb config Ricardo Ribalda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox