public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] usb: use kcalloc instead of kzalloc
@ 2014-08-19 10:38 Arjun Sreedharan
  2014-08-19 10:56 ` Clemens Ladisch
  2014-08-19 14:41 ` Alan Stern
  0 siblings, 2 replies; 3+ messages in thread
From: Arjun Sreedharan @ 2014-08-19 10:38 UTC (permalink / raw)
  To: gregkh; +Cc: linux-usb, linux-kernel

kcalloc has protection from integer overflows
which could arise from the multiplication of
number of elements and size.

Signed-off-by: Arjun Sreedharan <arjun024@gmail.com>
---
 drivers/usb/core/config.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/drivers/usb/core/config.c b/drivers/usb/core/config.c
index b2a540b..abde67d 100644
--- a/drivers/usb/core/config.c
+++ b/drivers/usb/core/config.c
@@ -318,7 +318,7 @@ static int usb_parse_interface(struct device *ddev, int cfgno,
 	struct usb_interface_cache *intfc;
 	struct usb_host_interface *alt;
 	int i, n;
-	int len, retval;
+	int retval;
 	int num_ep, num_ep_orig;
 
 	d = (struct usb_interface_descriptor *) buffer;
@@ -380,8 +380,7 @@ static int usb_parse_interface(struct device *ddev, int cfgno,
 
 	if (num_ep > 0) {
 		/* Can't allocate 0 bytes */
-		len = sizeof(struct usb_host_endpoint) * num_ep;
-		alt->endpoint = kzalloc(len, GFP_KERNEL);
+		alt->endpoint = kcalloc(num_ep, sizeof(struct usb_host_endpoint), GFP_KERNEL);
 		if (!alt->endpoint)
 			return -ENOMEM;
 	}
@@ -683,13 +682,11 @@ int usb_get_configuration(struct usb_device *dev)
 		return -EINVAL;
 	}
 
-	length = ncfg * sizeof(struct usb_host_config);
-	dev->config = kzalloc(length, GFP_KERNEL);
+	dev->config = kcalloc(ncfg, sizeof(struct usb_host_config), GFP_KERNEL);
 	if (!dev->config)
 		goto err2;
 
-	length = ncfg * sizeof(char *);
-	dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
+	dev->rawdescriptors = kcalloc(ncfg, sizeof(char *), GFP_KERNEL);
 	if (!dev->rawdescriptors)
 		goto err2;
 
-- 
1.8.1.msysgit.1


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

* Re: [PATCH] usb: use kcalloc instead of kzalloc
  2014-08-19 10:38 [PATCH] usb: use kcalloc instead of kzalloc Arjun Sreedharan
@ 2014-08-19 10:56 ` Clemens Ladisch
  2014-08-19 14:41 ` Alan Stern
  1 sibling, 0 replies; 3+ messages in thread
From: Clemens Ladisch @ 2014-08-19 10:56 UTC (permalink / raw)
  To: Arjun Sreedharan; +Cc: gregkh, linux-usb, linux-kernel

Arjun Sreedharan wrote:
> kcalloc has protection from integer overflows
> which could arise from the multiplication of
> number of elements and size.

You are implying that such an overflow could arise in this code.
This is false.

> @@ -380,8 +380,7 @@ static int usb_parse_interface(struct device *ddev, int cfgno,
>
>  	if (num_ep > 0) {
>  		/* Can't allocate 0 bytes */
> -		len = sizeof(struct usb_host_endpoint) * num_ep;
> -		alt->endpoint = kzalloc(len, GFP_KERNEL);
> +		alt->endpoint = kcalloc(num_ep, sizeof(struct usb_host_endpoint), GFP_KERNEL);

bNumEndpoints is an unsigned 8-bit integer.

And even if you did not notice this, you should have noticed the if()
directly before this one.

> @@ -683,13 +682,11 @@ int usb_get_configuration(struct usb_device *dev)
>  		return -EINVAL;
>  	}
>
> -	length = ncfg * sizeof(struct usb_host_config);
> -	dev->config = kzalloc(length, GFP_KERNEL);
> +	dev->config = kcalloc(ncfg, sizeof(struct usb_host_config), GFP_KERNEL);
>  	if (!dev->config)
>  		goto err2;
>
> -	length = ncfg * sizeof(char *);
> -	dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
> +	dev->rawdescriptors = kcalloc(ncfg, sizeof(char *), GFP_KERNEL);

Same oversights here.


Regards,
Clemens

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

* Re: [PATCH] usb: use kcalloc instead of kzalloc
  2014-08-19 10:38 [PATCH] usb: use kcalloc instead of kzalloc Arjun Sreedharan
  2014-08-19 10:56 ` Clemens Ladisch
@ 2014-08-19 14:41 ` Alan Stern
  1 sibling, 0 replies; 3+ messages in thread
From: Alan Stern @ 2014-08-19 14:41 UTC (permalink / raw)
  To: Arjun Sreedharan; +Cc: gregkh, linux-usb, linux-kernel

On Tue, 19 Aug 2014, Arjun Sreedharan wrote:

> kcalloc has protection from integer overflows
> which could arise from the multiplication of
> number of elements and size.
> 
> Signed-off-by: Arjun Sreedharan <arjun024@gmail.com>

As Clemens pointed out, integer overflows cannot occur here.  This 
patch is not needed.

Alan Stern


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

end of thread, other threads:[~2014-08-19 14:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-08-19 10:38 [PATCH] usb: use kcalloc instead of kzalloc Arjun Sreedharan
2014-08-19 10:56 ` Clemens Ladisch
2014-08-19 14:41 ` Alan Stern

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox