Skip to contentSkip to conversation

staging: media: tegra-video: add missing error checks in vi_tpg_fmts_bitmap_init()

PATCH In review Alexandru Hossu 2 participants 2 messages Apr 12 – Apr 13 1 message not in the archive

Subsystems (4)
lore
Share & tools
lore
#1 lore raw

PATCH 2/2 staging: media: tegra-video: add missing error checks in vi_tpg_fmts_bitmap_init()

In reply to a message not in the archive

tegra_get_format_idx_by_code() returns -1 when the requested format is not found in the SoC format table. vi_tpg_fmts_bitmap_init() does not check this return value before passing it to bitmap_set(). A negative index converted to unsigned would result in an out-of-bounds memory access, corrupting adjacent kernel memory.

Add WARN_ON() guards so that any future SoC addition or Kconfig change that exposes this path fails loudly rather than silently corrupting memory.

---
 drivers/staging/media/tegra-video/vi.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
drivers/staging/media/tegra-video/vi.c +14 -2
index afc7327ef318..d1d934e361f7 100644
@@ -1017,7 +1017,7 @@ static int tegra_channel_setup_ctrl_handler(struct tegra_vi_channel *chan)
 }
 
 /* VI only support 2 formats in TPG mode */
-static void vi_tpg_fmts_bitmap_init(struct tegra_vi_channel *chan)
+static int vi_tpg_fmts_bitmap_init(struct tegra_vi_channel *chan)
 {
 	int index;
 
@@ -1025,12 +1025,22 @@ static void vi_tpg_fmts_bitmap_init(struct tegra_vi_channel *chan)
 
 	index = tegra_get_format_idx_by_code(chan->vi,
 					     MEDIA_BUS_FMT_SRGGB10_1X10, 0);
+	if (index < 0) {
+		dev_err(chan->vi->dev, "format SRGGB10_1X10 not found\n");
+		return -EINVAL;
+	}
 	bitmap_set(chan->tpg_fmts_bitmap, index, 1);
 
 	index = tegra_get_format_idx_by_code(chan->vi,
 					     MEDIA_BUS_FMT_RGB888_1X32_PADHI,
 					     0);
+	if (index < 0) {
+		dev_err(chan->vi->dev, "format RGB888_1X32_PADHI not found\n");
+		return -EINVAL;
+	}
 	bitmap_set(chan->tpg_fmts_bitmap, index, 1);
+
+	return 0;
 }
 
 static int vi_fmts_bitmap_init(struct tegra_vi_channel *chan)
@@ -1410,7 +1420,9 @@ int tegra_v4l2_nodes_setup_tpg(struct tegra_video_device *vid)
 			goto cleanup;
 
 		v4l2_set_subdev_hostdata(&csi_chan->subdev, vi_chan);
-		vi_tpg_fmts_bitmap_init(vi_chan);
+		ret = vi_tpg_fmts_bitmap_init(vi_chan);
+		if (ret < 0)
+			goto cleanup;
 		csi_chan = list_next_entry(csi_chan, list);
 	}
 
Signature · 1 line
2.53.0
#2 lore raw
Quoted 11 lines · Alexandru Hossu

tegra_get_format_idx_by_code() returns -1 when the requested format is not found in the SoC format table. vi_tpg_fmts_bitmap_init() does not check this return value before passing it to bitmap_set(). A negative index converted to unsigned would result in an out-of-bounds memory access, corrupting adjacent kernel memory.

Add WARN_ON() guards so that any future SoC addition or Kconfig change that exposes this path fails loudly rather than silently corrupting memory.

---

patch 1 is missing. Also the little change log under the --- is missing.
https://staticthinking.wordpress.com/2022/07/27/how-to-send-a-v2-patch/
And I want you do change the commit message to say that it's not a real life bugfix.

Quoted 17 lines
 drivers/staging/media/tegra-video/vi.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
drivers/staging/media/tegra-video/vi.c +1 -1
index afc7327ef318..d1d934e361f7 100644
@@ -1017,7 +1017,7 @@ static int tegra_channel_setup_ctrl_handler(struct tegra_vi_channel *chan)
 }
 
 /* VI only support 2 formats in TPG mode */
-static void vi_tpg_fmts_bitmap_init(struct tegra_vi_channel *chan)
+static int vi_tpg_fmts_bitmap_init(struct tegra_vi_channel *chan)
 {
 	int index;
 

drivers/staging/media/tegra-video/vi.c static void vi_tpg_fmts_bitmap_init(struct tegra_vi_channel *chan) · line 1030

Quoted 6 lines
drivers/staging/media/tegra-video/vi.c +3 -0
@@ -1025,12 +1025,22 @@ static void vi_tpg_fmts_bitmap_init(struct tegra_vi_channel *chan)
 
 	index = tegra_get_format_idx_by_code(chan->vi,
 					     MEDIA_BUS_FMT_SRGGB10_1X10, 0);
+	if (index < 0) {
+		dev_err(chan->vi->dev, "format SRGGB10_1X10 not found\n");
+		return -EINVAL;

Change tegra_get_format_idx_by_code() to return -EINVAL instead.

regards,
dan carpenter