diff mbox series

[PATCHv2] media: v4l2-ctrls-core.c: check min/max for menu, controls

Message ID e57fb278-4808-4e12-9b61-005c3e45365e@xs4all.nl (mailing list archive)
State New, archived
Headers show
Series [PATCHv2] media: v4l2-ctrls-core.c: check min/max for menu, controls | expand

Commit Message

Hans Verkuil Feb. 5, 2024, 11:10 a.m. UTC
Menu controls that use the menu_skip_mask require that the min-max range is
inside 0-63.

Negative values obviously make no sense for menu controls, and the maximum
value is currently limited by the number of bits of the menu_skip_mask
value. However, if menu_skip_mask == 0, then larger menus are fine.

If we ever need to add support for larger menus that support the skip
mask, then more work is needed.

In the places where the menu_skip_mask is checked, use BIT_ULL to get the
bit to check and check if the bit number is < BITS_PER_LONG_LONG to avoid
shifting out of range. With the new check in check_range this should
never happen, but it is better to be safe and avoid static analyzer
warnings.

Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
---
 drivers/media/v4l2-core/v4l2-ctrls-api.c  | 2 +-
 drivers/media/v4l2-core/v4l2-ctrls-core.c | 5 +++--
 2 files changed, 4 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/drivers/media/v4l2-core/v4l2-ctrls-api.c b/drivers/media/v4l2-core/v4l2-ctrls-api.c
index 002ea6588edf..d9a422017bd9 100644
--- a/drivers/media/v4l2-core/v4l2-ctrls-api.c
+++ b/drivers/media/v4l2-core/v4l2-ctrls-api.c
@@ -1179,7 +1179,7 @@  int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
 		return -EINVAL;

 	/* Use mask to see if this menu item should be skipped */
-	if (ctrl->menu_skip_mask & (1ULL << i))
+	if (i < BITS_PER_LONG_LONG && (ctrl->menu_skip_mask & BIT_ULL(i)))
 		return -EINVAL;
 	/* Empty menu items should also be skipped */
 	if (ctrl->type == V4L2_CTRL_TYPE_MENU) {
diff --git a/drivers/media/v4l2-core/v4l2-ctrls-core.c b/drivers/media/v4l2-core/v4l2-ctrls-core.c
index 0accb96001db..c4d995f32191 100644
--- a/drivers/media/v4l2-core/v4l2-ctrls-core.c
+++ b/drivers/media/v4l2-core/v4l2-ctrls-core.c
@@ -1504,11 +1504,12 @@  int check_range(enum v4l2_ctrl_type type,
 		return 0;
 	case V4L2_CTRL_TYPE_MENU:
 	case V4L2_CTRL_TYPE_INTEGER_MENU:
-		if (min > max || def < min || def > max)
+		if (min > max || def < min || def > max ||
+		    min < 0 || (step && max >= BITS_PER_LONG_LONG))
 			return -ERANGE;
 		/* Note: step == menu_skip_mask for menu controls.
 		   So here we check if the default value is masked out. */
-		if (step && ((1 << def) & step))
+		if (def < BITS_PER_LONG_LONG && (step & BIT_ULL(def)))
 			return -EINVAL;
 		return 0;
 	case V4L2_CTRL_TYPE_STRING: