Skip to contentSkip to conversation

media: tegra-video: vi: fix invalid u32 return value in format lookup

PATCH Superseded Hungyu Lin 3 participants 3 messages Apr 12 – May 6

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

PATCH 1/1 media: tegra-video: vi: fix invalid u32 return value in format lookup

tegra_get_format_fourcc_by_idx() returns a u32 but uses -EINVAL to signal an out-of-bounds index. This results in a large unsigned value being returned, which may be interpreted as a valid fourcc.

Return 0 instead to indicate an invalid format.

Callers assign the return value directly to pixelformat, so returning an error code encoded in u32 is unsafe.

---
 drivers/staging/media/tegra-video/vi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/media/tegra-video/vi.c +1 -1
index 9c0b38585d63..966792a6ec19 100644
@@ -81,7 +81,7 @@ static u32 tegra_get_format_fourcc_by_idx(struct tegra_vi *vi,
 					  unsigned int index)
 {
 	if (index >= vi->soc->nformats)
-		return -EINVAL;
+		return 0;
 
 	return vi->soc->video_formats[index].fourcc;
 }
Signature · 1 line
2.34.1
#1 lore raw
Quoted 19 lines · Hungyu Lin

tegra_get_format_fourcc_by_idx() returns a u32 but uses -EINVAL to signal an out-of-bounds index. This results in a large unsigned value being returned, which may be interpreted as a valid fourcc.

Return 0 instead to indicate an invalid format.

Callers assign the return value directly to pixelformat, so returning an error code encoded in u32 is unsafe.

---
 drivers/staging/media/tegra-video/vi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/media/tegra-video/vi.c +0 -0
index 9c0b38585d63..966792a6ec19 100644

drivers/staging/media/tegra-video/vi.c static u32 tegra_get_format_fourcc_by_idx(struct tegra_vi *vi, · line 84

Quoted 5 lines
drivers/staging/media/tegra-video/vi.c +1 -1
@@ -81,7 +81,7 @@ static u32 tegra_get_format_fourcc_by_idx(struct tegra_vi *vi,
 					  unsigned int index)
 {
 	if (index >= vi->soc->nformats)
-		return -EINVAL;
+		return 0;

Someone else just sent a patch for this which added error checking to the caller. I liked that approach better. (I haven't checked to see if your approach works).

But either way this needs further review to see if the bug is actually real and if so what is the Fixes tag?

regards,
dan carpenter

#2 lore raw
In reply to Hungyu Lin
Quoted 19 lines · Hungyu Lin

tegra_get_format_fourcc_by_idx() returns a u32 but uses -EINVAL to signal an out-of-bounds index. This results in a large unsigned value being returned, which may be interpreted as a valid fourcc.

Return 0 instead to indicate an invalid format.

Callers assign the return value directly to pixelformat, so returning an error code encoded in u32 is unsafe.

---
 drivers/staging/media/tegra-video/vi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/media/tegra-video/vi.c +0 -0
index 9c0b38585d63..966792a6ec19 100644

drivers/staging/media/tegra-video/vi.c static u32 tegra_get_format_fourcc_by_idx(struct tegra_vi *vi, · line 84

Quoted 5 lines
drivers/staging/media/tegra-video/vi.c +1 -1
@@ -81,7 +81,7 @@ static u32 tegra_get_format_fourcc_by_idx(struct tegra_vi *vi,
 					  unsigned int index)
 {
 	if (index >= vi->soc->nformats)
-		return -EINVAL;
+		return 0;

Returning 0 is not a valid fourcc either.

This should never happen, so use WARN_ON_ONCE in the 'if' and return vi->soc->video_formats[0].fourcc;

That's at least better than the current code.

Regards,

Hans

Quoted 3 lines
drivers/staging/media/tegra-video/vi.c +0 -0
 	return vi->soc->video_formats[index].fourcc;
 }