Skip to contentSkip to conversation

media: tegra-video: vi: validate format index before bitmap_set

PATCH In review Hungyu Lin 2 participants 2 messages Apr 12 – Apr 13

Subsystems (4)
lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

PATCH 1/1 media: tegra-video: vi: validate format index before bitmap_set

tegra_get_format_idx_by_code() returns -1 when no matching format is found. vi_tpg_fmts_bitmap_init() used the returned index directly in bitmap_set(), which may lead to an out-of-bounds access when the format is not present.

This can occur when TPG is enabled on SoCs whose video_formats[] table does not include the requested media bus formats.

Validate the index before calling bitmap_set().

---
 drivers/staging/media/tegra-video/vi.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/staging/media/tegra-video/vi.c +4 -2
index 9c0b38585d63..dd8911640d98 100644
@@ -1020,12 +1020,14 @@ 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);
-	bitmap_set(chan->tpg_fmts_bitmap, index, 1);
+	if (index >= 0)
+		bitmap_set(chan->tpg_fmts_bitmap, index, 1);
 
 	index = tegra_get_format_idx_by_code(chan->vi,
 					     MEDIA_BUS_FMT_RGB888_1X32_PADHI,
 					     0);
-	bitmap_set(chan->tpg_fmts_bitmap, index, 1);
+	if (index >= 0)
+		bitmap_set(chan->tpg_fmts_bitmap, index, 1);
 }
 
 static int vi_fmts_bitmap_init(struct tegra_vi_channel *chan)
Signature · 1 line
2.34.1
#1 lore raw
Quoted 5 lines · Hungyu Lin

tegra_get_format_idx_by_code() returns -1 when no matching format is found. vi_tpg_fmts_bitmap_init() used the returned index directly in bitmap_set(), which may lead to an out-of-bounds access when the format is not present.

Quoted 7 lines

This can occur when TPG is enabled on SoCs whose video_formats[] table does not include the requested media bus formats.

Validate the index before calling bitmap_set().

---

This isn't a bug which can occur in real life since the index is always found... If there were really a bug, like say someone were writing a new driver and messed up, then this would just paper over the issue and make the bug harder to find, so in a way it makes the code worse.

regards,
dan carpenter