Skip to contentSkip to conversation

Media: video: uvc: integer overflow in uvc_ioctl_ctrl_map()

PATCH In review Haogang Chen 3 participants 6 messages Nov 29, 2011 – Dec 11, 2011 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Media: video: uvc: integer overflow in uvc_ioctl_ctrl_map()

There is a potential integer overflow in uvc_ioctl_ctrl_map(). When a large xmap->menu_count is passed from the userspace, the subsequent call to kmalloc() will allocate a buffer smaller than expected. map->menu_count and map->menu_info would later be used in a loop (e.g. in uvc_query_v4l2_ctrl), which leads to out-of-bound access.

The patch checks the ioctl argument and returns -EINVAL for zero or too large values in xmap->menu_count.

---
 drivers/media/video/uvc/uvc_v4l2.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
drivers/media/video/uvc/uvc_v4l2.c +6 -0
index dadf11f..9a180d6 100644
@@ -58,6 +58,12 @@ static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
 		break;
 
 	case V4L2_CTRL_TYPE_MENU:
+		if (xmap->menu_count == 0 ||
+		    xmap->menu_count > INT_MAX / sizeof(*map->menu_info)) {
+			kfree(map);
+			return -EINVAL;
+		}
+
 		size = xmap->menu_count * sizeof(*map->menu_info);
 		map->menu_info = kmalloc(size, GFP_KERNEL);
 		if (map->menu_info == NULL) {
Signature · 1 line
1.7.5.4
#1 lore raw

Hi Haogang,

Quoted 8 lines · On Tuesday 29 November 2011 22:32:25 Haogang Chen wrote:

There is a potential integer overflow in uvc_ioctl_ctrl_map(). When a large xmap->menu_count is passed from the userspace, the subsequent call to kmalloc() will allocate a buffer smaller than expected. map->menu_count and map->menu_info would later be used in a loop (e.g. in uvc_query_v4l2_ctrl), which leads to out-of-bound access.

The patch checks the ioctl argument and returns -EINVAL for zero or too large values in xmap->menu_count.

Thanks for the patch.

Quoted 10 lines
---
 drivers/media/video/uvc/uvc_v4l2.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
(quoted diff) +0 -0

b/drivers/media/video/uvc/uvc_v4l2.c index dadf11f..9a180d6 100644

drivers/media/video/uvc/uvc_v4l2.c static int uvc_ioctl_ctrl_map(struct uvc_video_chain · line 62

Quoted 5 lines
drivers/media/video/uvc/uvc_v4l2.c +2 -0
@@ -58,6 +58,12 @@ static int uvc_ioctl_ctrl_map(struct uvc_video_chain
 *chain, break;
 
 	case V4L2_CTRL_TYPE_MENU:
+		if (xmap->menu_count == 0 ||
+		    xmap->menu_count > INT_MAX / sizeof(*map->menu_info)) {

I'd like to prevent excessive memory consumption by limiting the number of menu entries, similarly to how the driver limits the number of mappings. Defining UVC_MAX_CONTROL_MENU_ENTRIES to 32 in uvcvideo.h should be a reasonable value.

drivers/media/video/uvc/uvc_v4l2.c static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain, · line 64

Quoted 2 lines
drivers/media/video/uvc/uvc_v4l2.c +2 -0
+			kfree(map);
+			return -EINVAL;

I'd rather do

	ret = -EINVAL;
	goto done;

to centralize error handling.

If you're fine with both changes I can modify the patch, there's no need to resubmit.

Quoted 5 lines
drivers/media/video/uvc/uvc_v4l2.c +2 -0
+		}
+
 		size = xmap->menu_count * sizeof(*map->menu_info);
 		map->menu_info = kmalloc(size, GFP_KERNEL);
 		if (map->menu_info == NULL) {
Signature · 3 lines
Regards,

Laurent Pinchart
#2 lore raw

The hard limit sounds good to me. But if you want to centralize error handling, please make sure that "goto done" only frees map, but not map->menu_info in that case.

- Haogang

Quoted 59 lines · Laurent Pinchart

Hi Haogang,

Quoted 8 lines · On Tuesday 29 November 2011 22:32:25 Haogang Chen wrote:

There is a potential integer overflow in uvc_ioctl_ctrl_map(). When a large xmap->menu_count is passed from the userspace, the subsequent call to kmalloc() will allocate a buffer smaller than expected. map->menu_count and map->menu_info would later be used in a loop (e.g. in uvc_query_v4l2_ctrl), which leads to out-of-bound access.

The patch checks the ioctl argument and returns -EINVAL for zero or too large values in xmap->menu_count.

Thanks for the patch.

Quoted 12 lines

---
 drivers/media/video/uvc/uvc_v4l2.c |    6 ++++++

 1 files changed, 6 insertions(+), 0 deletions(-)
(quoted diff) +0 -0

b/drivers/media/video/uvc/uvc_v4l2.c index dadf11f..9a180d6 100644

drivers/media/video/uvc/uvc_v4l2.c +0 -0
@@ -58,6 +58,12 @@ static int uvc_ioctl_ctrl_map(struct uvc_video_chain
 *chain, break;
 
Quoted 3 lines

      case V4L2_CTRL_TYPE_MENU:
+             if (xmap->menu_count == 0 ||
+                 xmap->menu_count > INT_MAX / sizeof(*map->menu_info)) {

I'd like to prevent excessive memory consumption by limiting the number of menu entries, similarly to how the driver limits the number of mappings. Defining UVC_MAX_CONTROL_MENU_ENTRIES to 32 in uvcvideo.h should be a reasonable value.

Quoted 2 lines

+                     kfree(map);
+                     return -EINVAL;

I'd rather do

       ret = -EINVAL;
       goto done;

to centralize error handling.

If you're fine with both changes I can modify the patch, there's no need to resubmit.

Quoted 5 lines

+             }
+
              size = xmap->menu_count * sizeof(*map->menu_info); map->menu_info = kmalloc(size, GFP_KERNEL); if (map->menu_info == NULL) {

--
Regards,

Laurent Pinchart

#3 lore raw

Hi Haogang,

Quoted 1 line · On Wednesday 30 November 2011 03:28:32 Haogang Chen wrote:

The hard limit sounds good to me.

OK.

Quoted 2 lines

But if you want to centralize error handling, please make sure that "goto done" only frees map, but not map->menu_info in that case.

map->menu_info will be NULL, so it's safe to kfree() it.

Signature · 3 lines
Regards,

Laurent Pinchart
#4 lore raw
In reply to Laurent Pinchart
Quoted 9 lines · Laurent Pinchart

Hi Haogang,

Quoted 6 lines · On Tuesday 29 November 2011 22:32:25 Haogang Chen wrote:

There is a potential integer overflow in uvc_ioctl_ctrl_map(). When a large xmap->menu_count is passed from the userspace, the subsequent call to kmalloc() will allocate a buffer smaller than expected. map->menu_count and map->menu_info would later be used in a loop (e.g. in uvc_query_v4l2_ctrl), which leads to out-of-bound access.

Quoted 4 lines
Quoted 2 lines

The patch checks the ioctl argument and returns -EINVAL for zero or too large values in xmap->menu_count.

Thanks for the patch.

I'm assuming that either one of you will re-send the patches with the pointed changes, so, I'm marking this one with "changes requested" at patchwork.

Quoted 41 lines
Quoted 11 lines
---
  drivers/media/video/uvc/uvc_v4l2.c |    6 ++++++
  1 files changed, 6 insertions(+), 0 deletions(-)
(quoted diff) +0 -0

b/drivers/media/video/uvc/uvc_v4l2.c index dadf11f..9a180d6 100644

drivers/media/video/uvc/uvc_v4l2.c static int uvc_ioctl_ctrl_map(struct uvc_video_chain · line 62

Quoted 5 lines
drivers/media/video/uvc/uvc_v4l2.c +2 -0
@@ -58,6 +58,12 @@ static int uvc_ioctl_ctrl_map(struct uvc_video_chain
 *chain, break;
 
  	case V4L2_CTRL_TYPE_MENU:
+		if (xmap->menu_count == 0 ||
+		    xmap->menu_count>  INT_MAX / sizeof(*map->menu_info)) {

I'd like to prevent excessive memory consumption by limiting the number of menu entries, similarly to how the driver limits the number of mappings. Defining UVC_MAX_CONTROL_MENU_ENTRIES to 32 in uvcvideo.h should be a reasonable value.

drivers/media/video/uvc/uvc_v4l2.c static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain, · line 64

Quoted 2 lines
drivers/media/video/uvc/uvc_v4l2.c +2 -0
+			kfree(map);
+			return -EINVAL;

I'd rather do

	ret = -EINVAL;
	goto done;

to centralize error handling.

If you're fine with both changes I can modify the patch, there's no need to resubmit.

Quoted 5 lines
drivers/media/video/uvc/uvc_v4l2.c +2 -0
+		}
+
  		size = xmap->menu_count * sizeof(*map->menu_info);
  		map->menu_info = kmalloc(size, GFP_KERNEL);
  		if (map->menu_info == NULL) {
#5 lore raw

Hi Mauro,

Quoted 10 lines · On Sunday 11 December 2011 11:22:29 Mauro Carvalho Chehab wrote:
Quoted 9 lines · Laurent Pinchart

Hi Haogang,

Quoted 6 lines · On Tuesday 29 November 2011 22:32:25 Haogang Chen wrote:

There is a potential integer overflow in uvc_ioctl_ctrl_map(). When a large xmap->menu_count is passed from the userspace, the subsequent call to kmalloc() will allocate a buffer smaller than expected. map->menu_count and map->menu_info would later be used in a loop (e.g. in uvc_query_v4l2_ctrl), which leads to out-of-bound access.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

The patch checks the ioctl argument and returns -EINVAL for zero or too large values in xmap->menu_count.

Thanks for the patch.

I'm assuming that either one of you will re-send the patches with the pointed changes, so, I'm marking this one with "changes requested" at patchwork.

The modified patch is included in my latest pull request.

I had forgotten to CC [email protected] in the commit message. I've updated the uvcvideo-next branch, but have you already pulled from it ? If so, could you add Cc: [email protected] to the patch, or should I send a v2 for the pull request ?

Quoted 41 lines
Quoted 41 lines
Quoted 11 lines

---

  drivers/media/video/uvc/uvc_v4l2.c |    6 ++++++
  1 files changed, 6 insertions(+), 0 deletions(-)
(quoted diff) +0 -0

b/drivers/media/video/uvc/uvc_v4l2.c index dadf11f..9a180d6 100644

drivers/media/video/uvc/uvc_v4l2.c static int uvc_ioctl_ctrl_map(struct uvc_video_chain · line 62

Quoted 5 lines
drivers/media/video/uvc/uvc_v4l2.c +2 -0
@@ -58,6 +58,12 @@ static int uvc_ioctl_ctrl_map(struct uvc_video_chain
 *chain, break;
 
  	case V4L2_CTRL_TYPE_MENU:
+		if (xmap->menu_count == 0 ||
+		    xmap->menu_count>  INT_MAX / sizeof(*map->menu_info)) {

I'd like to prevent excessive memory consumption by limiting the number of menu entries, similarly to how the driver limits the number of mappings. Defining UVC_MAX_CONTROL_MENU_ENTRIES to 32 in uvcvideo.h should be a reasonable value.

drivers/media/video/uvc/uvc_v4l2.c static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain, · line 64

Quoted 2 lines
drivers/media/video/uvc/uvc_v4l2.c +2 -0
+			kfree(map);
+			return -EINVAL;

I'd rather do

	ret = -EINVAL;
	goto done;

to centralize error handling.

If you're fine with both changes I can modify the patch, there's no need to resubmit.

Quoted 6 lines
drivers/media/video/uvc/uvc_v4l2.c +2 -0
+		}
+
 
  		size = xmap->menu_count * sizeof(*map->menu_info);
  		map->menu_info = kmalloc(size, GFP_KERNEL);
  		if (map->menu_info == NULL) {
Signature · 3 lines
Regards,

Laurent Pinchart