Skip to contentSkip to conversation

media: uvcvideo: Fix buffer overflow in uvc_mapping_get_menu_value()

PATCH In review Dan Carpenter 3 participants 8 messages Sep 18 – Oct 8

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: uvcvideo: Fix buffer overflow in uvc_mapping_get_menu_value()

The "idx" value is a user controlled u32 so we have to bounds check it before calling test_bit() to avoid reading beyond the end of the bitmap.

Fixes 4e15c535659b ("media: uvcvideo: Support any size for mapping get/set")
---
 drivers/media/usb/uvc/uvc_ctrl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/usb/uvc/uvc_ctrl.c +1 -1
index 3ca108b83f1d..f157ed99be4e 100644
@@ -538,7 +538,7 @@ static void uvc_mapping_set_s32(struct uvc_control_mapping *mapping,
 static int uvc_mapping_get_menu_value(const struct uvc_control_mapping *mapping,
 				      u32 idx)
 {
-	if (!test_bit(idx, &mapping->menu_mask))
+	if (idx >= BITS_PER_LONG || !test_bit(idx, &mapping->menu_mask))
 		return -EINVAL;
 
 	if (mapping->menu_mapping)
Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#1 lore raw

Hi Dan

I believe that for all the uses of uvc_mapping_get_menu_value we are already doing bound checks:

index >= BITS_PER_TYPE(mapping->menu_mask) in uvc_query_v4l2_menu() value> fls(mapping->menu_mask) -1 in uvc_ctrl_clamp() BIT(i) <= mapping->menu_mask in uvc_menu_to_v4l2_menu()

In any case, I think this patch is still worthwhile. It will help us avoiding bugs in the future (have you found this with a new test for smatch?)
and if we go that way it is probably a good idea to also "fix" uvc_mapping_get_menu_name()

Quoted 3 lines · Dan Carpenter

The "idx" value is a user controlled u32 so we have to bounds check it before calling test_bit() to avoid reading beyond the end of the bitmap.

Maybe change the commit message as well to avoid stable cherry picking it blindly (sorry seems like I am more picky than usual :) )

Quoted 2 lines
Fixes 4e15c535659b ("media: uvcvideo: Support any size for mapping get/set")

I believe this should be:

Fixes 40140eda661e ("media: uvcvideo: Implement mask for V4L2_CTRL_TYPE_MENU")

or no Fixes at all.

Quoted 10 lines
---
 drivers/media/usb/uvc/uvc_ctrl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/usb/uvc/uvc_ctrl.c +0 -0
index 3ca108b83f1d..f157ed99be4e 100644

drivers/media/usb/uvc/uvc_ctrl.c static void uvc_mapping_set_s32(struct uvc_control_mapping *mapping, · line 542

Quoted 6 lines
drivers/media/usb/uvc/uvc_ctrl.c +1 -1
@@ -538,7 +538,7 @@ static void uvc_mapping_set_s32(struct uvc_control_mapping *mapping,
 static int uvc_mapping_get_menu_value(const struct uvc_control_mapping *mapping,
                                      u32 idx)
 {
-       if (!test_bit(idx, &mapping->menu_mask))
+       if (idx >= BITS_PER_LONG || !test_bit(idx, &mapping->menu_mask))
                return -EINVAL;

What about using BITS_PER_TYPE(mapping->menu_mask) to be consistent?

Quoted 5 lines
drivers/media/usb/uvc/uvc_ctrl.c +0 -1
        if (mapping->menu_mapping)
--

2.53.0

with those changes:

Signature · 1 line
Ricardo Ribalda
#2 lore raw
Quoted 9 lines · Ricardo Ribalda

Hi Dan

I believe that for all the uses of uvc_mapping_get_menu_value we are already doing bound checks:

index >= BITS_PER_TYPE(mapping->menu_mask) in uvc_query_v4l2_menu() value> fls(mapping->menu_mask) -1 in uvc_ctrl_clamp() BIT(i) <= mapping->menu_mask in uvc_menu_to_v4l2_menu()

The problematic caller is uvc_set_le_value().

	value = *(s32 *)v4l2_in;

Smatch thinks that is called from uvc_mapping_set_xctrl_compound().

drivers/media/usb/uvc/uvc_ctrl.c
2772 data = memdup_user(xctrl->ptr, size);
2773 if (IS_ERR(data))
2774 return PTR_ERR(data);
2775
2776 return mapping->set(mapping, size, data,
2777 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));

But maybe that mapping->set can't point to uvc_set_le_value.

Quoted 7 lines

In any case, I think this patch is still worthwhile. It will help us avoiding bugs in the future (have you found this with a new test for smatch?)
and if we go that way it is probably a good idea to also "fix" uvc_mapping_get_menu_name()

Quoted 7 lines
Quoted 3 lines · Dan Carpenter

The "idx" value is a user controlled u32 so we have to bounds check it before calling test_bit() to avoid reading beyond the end of the bitmap.

Maybe change the commit message as well to avoid stable cherry picking it blindly (sorry seems like I am more picky than usual :) )

I mean if uvc_set_le_value() turns out not to be a bug then, of course.

Quoted 13 lines
Quoted 2 lines
Fixes 4e15c535659b ("media: uvcvideo: Support any size for mapping get/set")

I believe this should be:

Fixes 40140eda661e ("media: uvcvideo: Implement mask for V4L2_CTRL_TYPE_MENU")

or no Fixes at all.

Quoted 5 lines
---
 drivers/media/usb/uvc/uvc_ctrl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

drivers/media/usb/uvc/uvc_ctrl.c static void uvc_mapping_set_s32(struct uvc_control_mapping *mapping, · line 542

Quoted 11 lines
drivers/media/usb/uvc/uvc_ctrl.c +1 -1
index 3ca108b83f1d..f157ed99be4e 100644
@@ -538,7 +538,7 @@ static void uvc_mapping_set_s32(struct uvc_control_mapping *mapping,
 static int uvc_mapping_get_menu_value(const struct uvc_control_mapping *mapping,
                                      u32 idx)
 {
-       if (!test_bit(idx, &mapping->menu_mask))
+       if (idx >= BITS_PER_LONG || !test_bit(idx, &mapping->menu_mask))
                return -EINVAL;

What about using BITS_PER_TYPE(mapping->menu_mask) to be consistent?

Sure.

regards,
dan carpenter

#3 lore raw

Hi Dan

Quoted 12 lines · Dan Carpenter
Quoted 9 lines · Ricardo Ribalda

Hi Dan

I believe that for all the uses of uvc_mapping_get_menu_value we are already doing bound checks:

index >= BITS_PER_TYPE(mapping->menu_mask) in uvc_query_v4l2_menu() value> fls(mapping->menu_mask) -1 in uvc_ctrl_clamp() BIT(i) <= mapping->menu_mask in uvc_menu_to_v4l2_menu()

Quoted 5 lines

The problematic caller is uvc_set_le_value().

        value = *(s32 *)v4l2_in;

Smatch thinks that is called from uvc_mapping_set_xctrl_compound().

uvc_mapping_set_xctrl_compound() are only called if v4l2_type >= V4L2_CTRL_TYPE_RECT and then v4l2_type != V4L2_CTRL_TYPE_MENU. So I think we are safe.

Quoted 61 lines

drivers/media/usb/uvc/uvc_ctrl.c
2772 data = memdup_user(xctrl->ptr, size);
2773 if (IS_ERR(data))
2774 return PTR_ERR(data);
2775
2776 return mapping->set(mapping, size, data,
2777 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));

But maybe that mapping->set can't point to uvc_set_le_value.

Quoted 7 lines

In any case, I think this patch is still worthwhile. It will help us avoiding bugs in the future (have you found this with a new test for smatch?)
and if we go that way it is probably a good idea to also "fix" uvc_mapping_get_menu_name()

Quoted 7 lines
Quoted 3 lines · Dan Carpenter

The "idx" value is a user controlled u32 so we have to bounds check it before calling test_bit() to avoid reading beyond the end of the bitmap.

Maybe change the commit message as well to avoid stable cherry picking it blindly (sorry seems like I am more picky than usual :) )

I mean if uvc_set_le_value() turns out not to be a bug then, of course.

Quoted 13 lines
Quoted 2 lines
Fixes 4e15c535659b ("media: uvcvideo: Support any size for mapping get/set")

I believe this should be:

Fixes 40140eda661e ("media: uvcvideo: Implement mask for V4L2_CTRL_TYPE_MENU")

or no Fixes at all.

Quoted 5 lines
---
 drivers/media/usb/uvc/uvc_ctrl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

drivers/media/usb/uvc/uvc_ctrl.c static void uvc_mapping_set_s32(struct uvc_control_mapping *mapping, · line 542

Quoted 11 lines
drivers/media/usb/uvc/uvc_ctrl.c +1 -1
index 3ca108b83f1d..f157ed99be4e 100644
@@ -538,7 +538,7 @@ static void uvc_mapping_set_s32(struct uvc_control_mapping *mapping,
 static int uvc_mapping_get_menu_value(const struct uvc_control_mapping *mapping,
                                      u32 idx)
 {
-       if (!test_bit(idx, &mapping->menu_mask))
+       if (idx >= BITS_PER_LONG || !test_bit(idx, &mapping->menu_mask))
                return -EINVAL;

What about using BITS_PER_TYPE(mapping->menu_mask) to be consistent?

Sure.

regards,
dan carpenter

Signature · 1 line
Ricardo Ribalda
#4 lore raw
Quoted 17 lines · Ricardo Ribalda

Hi Dan

Quoted 14 lines · Dan Carpenter
Quoted 9 lines · Ricardo Ribalda

Hi Dan

I believe that for all the uses of uvc_mapping_get_menu_value we are already doing bound checks:

index >= BITS_PER_TYPE(mapping->menu_mask) in uvc_query_v4l2_menu() value> fls(mapping->menu_mask) -1 in uvc_ctrl_clamp() BIT(i) <= mapping->menu_mask in uvc_menu_to_v4l2_menu()

The problematic caller is uvc_set_le_value().

Quoted 8 lines
Quoted 3 lines
        value = *(s32 *)v4l2_in;

Smatch thinks that is called from uvc_mapping_set_xctrl_compound().

uvc_mapping_set_xctrl_compound() are only called if v4l2_type >= V4L2_CTRL_TYPE_RECT and then v4l2_type != V4L2_CTRL_TYPE_MENU. So I think we are safe.

Yeah... I have reprimanded ChatGPT and it says it has updated the warning review skill.

This is difficult to silence. But I'm going to ask AI to create an tool to automatically rebuild a second temporary database which only checks the problematic call tree and points any impossible constraints. That would have flagged this warning as a false positive.

regards,
dan carpenter

maintainer
#5 lore raw
In reply to Dan Carpenter
Quoted 5 lines · Dan Carpenter

The "idx" value is a user controlled u32 so we have to bounds check it before calling test_bit() to avoid reading beyond the end of the bitmap.

Fixes 4e15c535659b ("media: uvcvideo: Support any size for mapping get/set")

What's the status of this patch ? The issue is a false positive, do you still think there's value in this redundant test ?

Quoted 17 lines
---
 drivers/media/usb/uvc/uvc_ctrl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/usb/uvc/uvc_ctrl.c +1 -1
index 3ca108b83f1d..f157ed99be4e 100644
@@ -538,7 +538,7 @@ static void uvc_mapping_set_s32(struct uvc_control_mapping *mapping,
 static int uvc_mapping_get_menu_value(const struct uvc_control_mapping *mapping,
 				      u32 idx)
 {
-	if (!test_bit(idx, &mapping->menu_mask))
+	if (idx >= BITS_PER_LONG || !test_bit(idx, &mapping->menu_mask))
 		return -EINVAL;
 
 	if (mapping->menu_mapping)
Signature · 3 lines
Regards,

Laurent Pinchart
#6 lore raw
Quoted 10 lines · Laurent Pinchart
Quoted 5 lines · Dan Carpenter

The "idx" value is a user controlled u32 so we have to bounds check it before calling test_bit() to avoid reading beyond the end of the bitmap.

Fixes 4e15c535659b ("media: uvcvideo: Support any size for mapping get/set")

What's the status of this patch ? The issue is a false positive, do you still think there's value in this redundant test ?

No, it's a false positive.

With how Smatch works, I don't have an easy way to silence it in Smatch, but I'm going to publish my AI skills file for reviewing static checker warnings. Also I've started writing a new tool to help filter false positives.

regards,
dan carpenter

#7 lore raw

Hi all

Quoted 13 lines · Dan Carpenter
Quoted 10 lines · Laurent Pinchart
Quoted 5 lines · Dan Carpenter

The "idx" value is a user controlled u32 so we have to bounds check it before calling test_bit() to avoid reading beyond the end of the bitmap.

Fixes 4e15c535659b ("media: uvcvideo: Support any size for mapping get/set")

What's the status of this patch ? The issue is a false positive, do you still think there's value in this redundant test ?

Quoted 6 lines

No, it's a false positive.

With how Smatch works, I don't have an easy way to silence it in Smatch, but I'm going to publish my AI skills file for reviewing static checker warnings. Also I've started writing a new tool to help filter false positives.

Even if it is a false positive, I think it is a good addition, it makes the code more robust.

Do you mind if I make a respin of it with my comments (keeping you as author)? (Or if you want to send a v2 that would be awesome as well :) )

Thanks!

Quoted 4 lines

regards,
dan carpenter

Signature · 1 line
Ricardo Ribalda