All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alan Stern <stern@rowland.harvard.edu>
To: Greg KH <gregkh@linuxfoundation.org>
Cc: linux-usb@vger.kernel.org, Andrey Konovalov <andreyknvl@google.com>
Subject: USB: core: only clean up what we allocated
Date: Tue, 12 Dec 2017 10:41:04 -0500 (EST)	[thread overview]
Message-ID: <Pine.LNX.4.44L0.1712121034500.1358-100000@iolanthe.rowland.org> (raw)

On Mon, 11 Dec 2017, Greg KH wrote:

> From: Andrey Konovalov <andreyknvl@google.com>
> 
> When cleaning up the configurations, make sure we only free the number
> of configurations and interfaces that we could have allocated.
> 
> Reported-by: Andrey Konovalov <andreyknvl@google.com>
> Cc: stable <stable@vger.kernel.org>
> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> 
> diff --git a/drivers/usb/core/config.c b/drivers/usb/core/config.c
> index 55b198ba629b..93b38471754e 100644
> --- a/drivers/usb/core/config.c
> +++ b/drivers/usb/core/config.c
> @@ -764,18 +764,21 @@ void usb_destroy_configuration(struct usb_device *dev)
>  		return;
>  
>  	if (dev->rawdescriptors) {
> -		for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
> +		for (i = 0; i < dev->descriptor.bNumConfigurations &&
> +				i < USB_MAXCONFIG; i++)
>  			kfree(dev->rawdescriptors[i]);
>  
>  		kfree(dev->rawdescriptors);
>  		dev->rawdescriptors = NULL;
>  	}
>  
> -	for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
> +	for (c = 0; c < dev->descriptor.bNumConfigurations &&
> +			c < USB_MAXCONFIG; c++) {
>  		struct usb_host_config *cf = &dev->config[c];
>  
>  		kfree(cf->string);
> -		for (i = 0; i < cf->desc.bNumInterfaces; i++) {
> +		for (i = 0; i < cf->desc.bNumInterfaces &&
> +				i < USB_MAXINTERFACES; i++) {
>  			if (cf->intf_cache[i])
>  				kref_put(&cf->intf_cache[i]->ref,
>  					  usb_release_interface_cache);

None of these changes are necessary.  The code is careful to reduce
dev->descriptor.bNumConfigurations and config->desc.bNumInterfaces when
necessary.

In usb_get_configuration() (line 806 on my system):

	if (ncfg > USB_MAXCONFIG) {
		dev_warn(ddev, "too many configurations: %d, "
		    "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
		dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
	}

In usb_parse_configuration() (line 676 on my system):

	if (n != nintf)
		dev_warn(ddev, "config %d has %d interface%s, different from "
		    "the descriptor's value: %d\n",
		    cfgno, n, plural(n), nintf_orig);
	else if (n == 0)
		dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
	config->desc.bNumInterfaces = nintf = n;

Alan Stern
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

             reply	other threads:[~2017-12-12 15:41 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-12 15:41 Alan Stern [this message]
  -- strict thread matches above, loose matches on Subject: below --
2017-12-12 18:41 USB: core: only clean up what we allocated Andrey Konovalov
2017-12-12 16:12 Alan Stern
2017-12-12 15:44 Andrey Konovalov
2017-12-11 21:48 Greg Kroah-Hartman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.44L0.1712121034500.1358-100000@iolanthe.rowland.org \
    --to=stern@rowland.harvard.edu \
    --cc=andreyknvl@google.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-usb@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.