From: Nas Chung nas.chung@chipsnmedia.com
[ Upstream commit ad2698efce37e910dcf3c3914263e6cb3e86f8cd ]
Explicitly compare a buffer type only with valid buffer types, to avoid matching a buffer type outside of the valid buffer type set.
Signed-off-by: Nas Chung nas.chung@chipsnmedia.com Reviewed-by: Michael Tretter m.tretter@pengutronix.de Signed-off-by: Sebastian Fricke sebastian.fricke@collabora.com Signed-off-by: Hans Verkuil hverkuil@xs4all.nl Signed-off-by: Sasha Levin sashal@kernel.org ---
**YES** This commit should be backported to stable kernel trees based on the following analysis: ## Code Change Analysis The commit fixes a logical flaw in the `V4L2_TYPE_IS_CAPTURE` macro in `include/uapi/linux/videodev2.h`. **Before the patch:** ```c #define V4L2_TYPE_IS_CAPTURE(type) (!V4L2_TYPE_IS_OUTPUT(type)) ``` **After the patch:** ```c #define V4L2_TYPE_IS_VALID(type) \ ((type) >= V4L2_BUF_TYPE_VIDEO_CAPTURE && (type) <= V4L2_BUF_TYPE_META_OUTPUT) #define V4L2_TYPE_IS_CAPTURE(type) \ (V4L2_TYPE_IS_VALID(type) && !V4L2_TYPE_IS_OUTPUT(type)) ``` ## The Bug and Its Impact The original implementation has a critical flaw: **invalid buffer type values incorrectly return true for `V4L2_TYPE_IS_CAPTURE`**. Here's why: 1. For invalid buffer types (e.g., 0, 15, 100, 0x80), `V4L2_TYPE_IS_OUTPUT()` returns false 2. The negation `!V4L2_TYPE_IS_OUTPUT()` makes these invalid types appear as "capture" types 3. This can lead to incorrect code paths being taken in media drivers throughout the kernel ## Why This Should Be Backported 1. **Affects User-Facing API**: This is a UAPI header that defines kernel-userspace interface behavior. Incorrect behavior here can affect any V4L2 application. 2. **Potential Security/Stability Risk**: The bug could lead to: - Wrong buffer handling paths in media drivers - Potential out-of-bounds access or incorrect memory management - Driver state corruption when invalid buffer types are misclassified 3. **Small, Contained Fix**: The change is minimal and contained to macro definitions with clear semantics. It only adds proper validation without changing valid type behavior. 4. **No Regression Risk**: The fix only affects the handling of invalid buffer types, making them correctly return false instead of incorrectly returning true. Valid buffer types retain their existing behavior. 5. **Wide Impact**: Looking at the kernel tree, `V4L2_TYPE_IS_CAPTURE` is used extensively in media drivers: - `/drivers/media/v4l2-core/v4l2-mem2mem.c` - Multiple platform-specific drivers (Samsung, Mediatek, Verisilicon, etc.) - Any incorrect behavior propagates to all these drivers 6. **Consistent with Historical Pattern**: Looking at similar commits like commit 4b837c6d7ee7 ("media: v4l: uAPI: V4L2_BUF_TYPE_META_OUTPUT is an output buffer type"), UAPI fixes for buffer type handling have been backported before. ## Backport Criteria Met - ✅ **Fixes important bugs**: Corrects fundamental macro logic affecting buffer type validation - ✅ **Minimal risk**: Only adds validation, doesn't change existing valid behavior - ✅ **Small and contained**: Changes only macro definitions in header file - ✅ **No architectural changes**: Pure bug fix without feature additions - ✅ **Affects critical subsystem**: Media/V4L2 is widely used across many device drivers The fix ensures that only valid buffer types (1-14) can be classified as capture types, preventing potential mishandling of invalid values throughout the media subsystem.
include/uapi/linux/videodev2.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/include/uapi/linux/videodev2.h b/include/uapi/linux/videodev2.h index 23fa102ea9f4c..270f8fee8ba77 100644 --- a/include/uapi/linux/videodev2.h +++ b/include/uapi/linux/videodev2.h @@ -153,10 +153,18 @@ enum v4l2_buf_type { V4L2_BUF_TYPE_SDR_OUTPUT = 12, V4L2_BUF_TYPE_META_CAPTURE = 13, V4L2_BUF_TYPE_META_OUTPUT = 14, + /* + * Note: V4L2_TYPE_IS_VALID and V4L2_TYPE_IS_OUTPUT must + * be updated if a new type is added. + */ /* Deprecated, do not use */ V4L2_BUF_TYPE_PRIVATE = 0x80, };
+#define V4L2_TYPE_IS_VALID(type) \ + ((type) >= V4L2_BUF_TYPE_VIDEO_CAPTURE &&\ + (type) <= V4L2_BUF_TYPE_META_OUTPUT) + #define V4L2_TYPE_IS_MULTIPLANAR(type) \ ((type) == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE \ || (type) == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) @@ -170,7 +178,8 @@ enum v4l2_buf_type { || (type) == V4L2_BUF_TYPE_SDR_OUTPUT \ || (type) == V4L2_BUF_TYPE_META_OUTPUT)
-#define V4L2_TYPE_IS_CAPTURE(type) (!V4L2_TYPE_IS_OUTPUT(type)) +#define V4L2_TYPE_IS_CAPTURE(type) \ + (V4L2_TYPE_IS_VALID(type) && !V4L2_TYPE_IS_OUTPUT(type))
enum v4l2_tuner_type { V4L2_TUNER_RADIO = 1,