All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Media: video: uvc: integer overflow in uvc_ioctl_ctrl_map()
@ 2011-11-29 21:32 Haogang Chen
  2011-11-30  1:22 ` Laurent Pinchart
  0 siblings, 1 reply; 6+ messages in thread
From: Haogang Chen @ 2011-11-29 21:32 UTC (permalink / raw)
  To: laurent.pinchart, mchehab; +Cc: linux-media, linux-kernel, Haogang Chen

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.

Signed-off-by: Haogang Chen <haogangchen@gmail.com>
---
 drivers/media/video/uvc/uvc_v4l2.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

diff --git a/drivers/media/video/uvc/uvc_v4l2.c b/drivers/media/video/uvc/uvc_v4l2.c
index dadf11f..9a180d6 100644
--- a/drivers/media/video/uvc/uvc_v4l2.c
+++ b/drivers/media/video/uvc/uvc_v4l2.c
@@ -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) {
-- 
1.7.5.4


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] Media: video: uvc: integer overflow in uvc_ioctl_ctrl_map()
  2011-11-29 21:32 [PATCH] Media: video: uvc: integer overflow in uvc_ioctl_ctrl_map() Haogang Chen
@ 2011-11-30  1:22 ` Laurent Pinchart
  2011-11-30  2:28   ` Haogang Chen
  2011-12-11 10:22   ` Mauro Carvalho Chehab
  0 siblings, 2 replies; 6+ messages in thread
From: Laurent Pinchart @ 2011-11-30  1:22 UTC (permalink / raw)
  To: Haogang Chen; +Cc: mchehab, linux-media, linux-kernel

Hi Haogang,

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.

> Signed-off-by: Haogang Chen <haogangchen@gmail.com>
> ---
>  drivers/media/video/uvc/uvc_v4l2.c |    6 ++++++
>  1 files changed, 6 insertions(+), 0 deletions(-)
> 
> diff --git a/drivers/media/video/uvc/uvc_v4l2.c
> b/drivers/media/video/uvc/uvc_v4l2.c index dadf11f..9a180d6 100644
> --- a/drivers/media/video/uvc/uvc_v4l2.c
> +++ b/drivers/media/video/uvc/uvc_v4l2.c
> @@ -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.

> +			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.

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

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Media: video: uvc: integer overflow in uvc_ioctl_ctrl_map()
  2011-11-30  1:22 ` Laurent Pinchart
@ 2011-11-30  2:28   ` Haogang Chen
  2011-11-30  9:14     ` Laurent Pinchart
  2011-12-11 10:22   ` Mauro Carvalho Chehab
  1 sibling, 1 reply; 6+ messages in thread
From: Haogang Chen @ 2011-11-30  2:28 UTC (permalink / raw)
  To: Laurent Pinchart; +Cc: mchehab, linux-media, linux-kernel

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




On Tue, Nov 29, 2011 at 8:22 PM, Laurent Pinchart
<laurent.pinchart@ideasonboard.com> wrote:
> Hi Haogang,
>
> 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.
>
>> Signed-off-by: Haogang Chen <haogangchen@gmail.com>
>> ---
>>  drivers/media/video/uvc/uvc_v4l2.c |    6 ++++++
>>  1 files changed, 6 insertions(+), 0 deletions(-)
>>
>> diff --git a/drivers/media/video/uvc/uvc_v4l2.c
>> b/drivers/media/video/uvc/uvc_v4l2.c index dadf11f..9a180d6 100644
>> --- a/drivers/media/video/uvc/uvc_v4l2.c
>> +++ b/drivers/media/video/uvc/uvc_v4l2.c
>> @@ -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.
>
>> +                     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.
>
>> +             }
>> +
>>               size = xmap->menu_count * sizeof(*map->menu_info);
>>               map->menu_info = kmalloc(size, GFP_KERNEL);
>>               if (map->menu_info == NULL) {
>
> --
> Regards,
>
> Laurent Pinchart
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Media: video: uvc: integer overflow in uvc_ioctl_ctrl_map()
  2011-11-30  2:28   ` Haogang Chen
@ 2011-11-30  9:14     ` Laurent Pinchart
  0 siblings, 0 replies; 6+ messages in thread
From: Laurent Pinchart @ 2011-11-30  9:14 UTC (permalink / raw)
  To: Haogang Chen; +Cc: mchehab, linux-media, linux-kernel

Hi Haogang,

On Wednesday 30 November 2011 03:28:32 Haogang Chen wrote:
> The hard limit sounds good to me.

OK.

> 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.

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Media: video: uvc: integer overflow in uvc_ioctl_ctrl_map()
  2011-11-30  1:22 ` Laurent Pinchart
  2011-11-30  2:28   ` Haogang Chen
@ 2011-12-11 10:22   ` Mauro Carvalho Chehab
  2011-12-11 10:31     ` Laurent Pinchart
  1 sibling, 1 reply; 6+ messages in thread
From: Mauro Carvalho Chehab @ 2011-12-11 10:22 UTC (permalink / raw)
  To: Laurent Pinchart; +Cc: Haogang Chen, linux-media, linux-kernel

On 29-11-2011 23:22, Laurent Pinchart wrote:
> Hi Haogang,
>
> 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.

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.

>
>> Signed-off-by: Haogang Chen<haogangchen@gmail.com>
>> ---
>>   drivers/media/video/uvc/uvc_v4l2.c |    6 ++++++
>>   1 files changed, 6 insertions(+), 0 deletions(-)
>>
>> diff --git a/drivers/media/video/uvc/uvc_v4l2.c
>> b/drivers/media/video/uvc/uvc_v4l2.c index dadf11f..9a180d6 100644
>> --- a/drivers/media/video/uvc/uvc_v4l2.c
>> +++ b/drivers/media/video/uvc/uvc_v4l2.c
>> @@ -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.
>
>> +			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.
>
>> +		}
>> +
>>   		size = xmap->menu_count * sizeof(*map->menu_info);
>>   		map->menu_info = kmalloc(size, GFP_KERNEL);
>>   		if (map->menu_info == NULL) {
>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Media: video: uvc: integer overflow in uvc_ioctl_ctrl_map()
  2011-12-11 10:22   ` Mauro Carvalho Chehab
@ 2011-12-11 10:31     ` Laurent Pinchart
  0 siblings, 0 replies; 6+ messages in thread
From: Laurent Pinchart @ 2011-12-11 10:31 UTC (permalink / raw)
  To: Mauro Carvalho Chehab; +Cc: Haogang Chen, linux-media, linux-kernel

Hi Mauro,

On Sunday 11 December 2011 11:22:29 Mauro Carvalho Chehab wrote:
> On 29-11-2011 23:22, Laurent Pinchart wrote:
> > Hi Haogang,
> > 
> > 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.
> 
> 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 stable@kernel.org in the commit message. I've updated 
the uvcvideo-next branch, but have you already pulled from it ? If so, could 
you add Cc: stable@kernel.org to the patch, or should I send a v2 for the pull 
request ?

> >> Signed-off-by: Haogang Chen<haogangchen@gmail.com>
> >> ---
> >> 
> >>   drivers/media/video/uvc/uvc_v4l2.c |    6 ++++++
> >>   1 files changed, 6 insertions(+), 0 deletions(-)
> >> 
> >> diff --git a/drivers/media/video/uvc/uvc_v4l2.c
> >> b/drivers/media/video/uvc/uvc_v4l2.c index dadf11f..9a180d6 100644
> >> --- a/drivers/media/video/uvc/uvc_v4l2.c
> >> +++ b/drivers/media/video/uvc/uvc_v4l2.c
> >> @@ -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.
> > 
> >> +			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.
> > 
> >> +		}
> >> +
> >> 
> >>   		size = xmap->menu_count * sizeof(*map->menu_info);
> >>   		map->menu_info = kmalloc(size, GFP_KERNEL);
> >>   		if (map->menu_info == NULL) {

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2011-12-11 10:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-11-29 21:32 [PATCH] Media: video: uvc: integer overflow in uvc_ioctl_ctrl_map() Haogang Chen
2011-11-30  1:22 ` Laurent Pinchart
2011-11-30  2:28   ` Haogang Chen
2011-11-30  9:14     ` Laurent Pinchart
2011-12-11 10:22   ` Mauro Carvalho Chehab
2011-12-11 10:31     ` Laurent Pinchart

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.