public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] USB: don't read past config->interface[] if usb_control_msg() fails in usb_reset_configuration()
@ 2010-02-09 13:00 Roel Kluin
  2010-02-09 13:08 ` Greg KH
  2010-02-09 15:12 ` Alan Stern
  0 siblings, 2 replies; 11+ messages in thread
From: Roel Kluin @ 2010-02-09 13:00 UTC (permalink / raw)
  To: Greg Kroah-Hartman, sarah.a.sharp, linux-usb, Andrew Morton, LKML

After the loop `for (i = 0; i < config->desc.bNumInterfaces; i++)' if no
break occurred, i equals config->desc.bNumInterfaces. so if
usb_control_msg() failed then after goto reset_old_alts we read from
config->interface[config->desc.bNumInterfaces].

Signed-off-by: Roel Kluin <roel.kluin@gmail.com>
---
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index 9bc95fe..00b49bc 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -1466,10 +1466,15 @@ int usb_reset_configuration(struct usb_device *dev)
 			retval = usb_hcd_alloc_bandwidth(dev, NULL,
 					intf->cur_altsetting, alt);
 		if (retval < 0)
-			break;
+			/* If not, reinstate the old alternate settings */
+			goto reset_old_alts;
 	}
-	/* If not, reinstate the old alternate settings */
+	retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
+			USB_REQ_SET_CONFIGURATION, 0,
+			config->desc.bConfigurationValue, 0,
+			NULL, 0, USB_CTRL_SET_TIMEOUT);
 	if (retval < 0) {
+		i--;
 reset_old_alts:
 		for (; i >= 0; i--) {
 			struct usb_interface *intf = config->interface[i];
@@ -1485,12 +1490,6 @@ reset_old_alts:
 		mutex_unlock(&hcd->bandwidth_mutex);
 		return retval;
 	}
-	retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
-			USB_REQ_SET_CONFIGURATION, 0,
-			config->desc.bConfigurationValue, 0,
-			NULL, 0, USB_CTRL_SET_TIMEOUT);
-	if (retval < 0)
-		goto reset_old_alts;
 	mutex_unlock(&hcd->bandwidth_mutex);
 
 	/* re-init hc/hcd interface/endpoint state */

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

* Re: [PATCH] USB: don't read past config->interface[] if usb_control_msg() fails in usb_reset_configuration()
  2010-02-09 13:00 [PATCH] USB: don't read past config->interface[] if usb_control_msg() fails in usb_reset_configuration() Roel Kluin
@ 2010-02-09 13:08 ` Greg KH
  2010-02-09 13:15   ` roel kluin
  2010-02-09 15:12 ` Alan Stern
  1 sibling, 1 reply; 11+ messages in thread
From: Greg KH @ 2010-02-09 13:08 UTC (permalink / raw)
  To: Roel Kluin; +Cc: sarah.a.sharp, linux-usb, Andrew Morton, LKML

On Tue, Feb 09, 2010 at 02:00:05PM +0100, Roel Kluin wrote:
> After the loop `for (i = 0; i < config->desc.bNumInterfaces; i++)' if no
> break occurred, i equals config->desc.bNumInterfaces. so if
> usb_control_msg() failed then after goto reset_old_alts we read from
> config->interface[config->desc.bNumInterfaces].
> 
> Signed-off-by: Roel Kluin <roel.kluin@gmail.com>

Have you seen this happen on real hardware?

curious,

greg k-h

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

* Re: [PATCH] USB: don't read past config->interface[] if  usb_control_msg() fails in usb_reset_configuration()
  2010-02-09 13:08 ` Greg KH
@ 2010-02-09 13:15   ` roel kluin
  0 siblings, 0 replies; 11+ messages in thread
From: roel kluin @ 2010-02-09 13:15 UTC (permalink / raw)
  To: Greg KH; +Cc: sarah.a.sharp, linux-usb, Andrew Morton, LKML

On Tue, Feb 9, 2010 at 2:08 PM, Greg KH <gregkh@suse.de> wrote:
> On Tue, Feb 09, 2010 at 02:00:05PM +0100, Roel Kluin wrote:
>> After the loop `for (i = 0; i < config->desc.bNumInterfaces; i++)' if no
>> break occurred, i equals config->desc.bNumInterfaces. so if
>> usb_control_msg() failed then after goto reset_old_alts we read from
>> config->interface[config->desc.bNumInterfaces].
>>
>> Signed-off-by: Roel Kluin <roel.kluin@gmail.com>
>
> Have you seen this happen on real hardware?
>
> curious,
>
> greg k-h
>

No, I just found it in the code,

Thanks, Roel

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

* Re: [PATCH] USB: don't read past config->interface[] if usb_control_msg() fails in usb_reset_configuration()
  2010-02-09 13:00 [PATCH] USB: don't read past config->interface[] if usb_control_msg() fails in usb_reset_configuration() Roel Kluin
  2010-02-09 13:08 ` Greg KH
@ 2010-02-09 15:12 ` Alan Stern
  2010-02-09 16:01   ` Roel Kluin
  1 sibling, 1 reply; 11+ messages in thread
From: Alan Stern @ 2010-02-09 15:12 UTC (permalink / raw)
  To: Roel Kluin
  Cc: Greg Kroah-Hartman, sarah.a.sharp, linux-usb, Andrew Morton, LKML

On Tue, 9 Feb 2010, Roel Kluin wrote:

> After the loop `for (i = 0; i < config->desc.bNumInterfaces; i++)' if no
> break occurred, i equals config->desc.bNumInterfaces. so if
> usb_control_msg() failed then after goto reset_old_alts we read from
> config->interface[config->desc.bNumInterfaces].

You correctly identified a problem, but your fix is wrong -- or at 
least, it is much too complicated.  The proper fix goes like this:

 	/* If not, reinstate the old alternate settings */
 	if (retval < 0) {
 reset_old_alts:
-		for (; i >= 0; i--) {
+		for (i--; i >= 0; i--) {
 			struct usb_interface *intf = config->interface[i];
 			struct usb_host_interface *alt;
 

Alan Stern


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

* Re: [PATCH] USB: don't read past config->interface[] if usb_control_msg() fails in usb_reset_configuration()
  2010-02-09 15:12 ` Alan Stern
@ 2010-02-09 16:01   ` Roel Kluin
  2010-02-09 22:01     ` Sarah Sharp
  2010-02-09 22:01     ` Roel Kluin
  0 siblings, 2 replies; 11+ messages in thread
From: Roel Kluin @ 2010-02-09 16:01 UTC (permalink / raw)
  To: Alan Stern
  Cc: Greg Kroah-Hartman, sarah.a.sharp, linux-usb, Andrew Morton, LKML

After the loop `for (i = 0; i < config->desc.bNumInterfaces; i++)' if no
break occurred, i equals config->desc.bNumInterfaces. so if
usb_control_msg() failed then after goto reset_old_alts we read from
config->interface[config->desc.bNumInterfaces].
We can safely decrement i as well if the break occurred.

Signed-off-by: Roel Kluin <roel.kluin@gmail.com>
Acked-by: Alan Stern <stern@rowland.harvard.edu>
---

> You correctly identified a problem, but your fix is wrong -- or at 
> least, it is much too complicated.

Ok,

 drivers/usb/core/message.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index 9bc95fe..1a48aac 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -1471,7 +1471,7 @@ int usb_reset_configuration(struct usb_device *dev)
 	/* If not, reinstate the old alternate settings */
 	if (retval < 0) {
 reset_old_alts:
-		for (; i >= 0; i--) {
+		for (i--; i >= 0; i--) {
 			struct usb_interface *intf = config->interface[i];
 			struct usb_host_interface *alt;
 

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

* Re: [PATCH] USB: don't read past config->interface[] if usb_control_msg() fails in usb_reset_configuration()
  2010-02-09 16:01   ` Roel Kluin
@ 2010-02-09 22:01     ` Sarah Sharp
  2010-02-09 22:09       ` roel kluin
  2010-02-09 22:01     ` Roel Kluin
  1 sibling, 1 reply; 11+ messages in thread
From: Sarah Sharp @ 2010-02-09 22:01 UTC (permalink / raw)
  To: Roel Kluin; +Cc: Alan Stern, Greg Kroah-Hartman, linux-usb, Andrew Morton, LKML

On Tue, Feb 09, 2010 at 05:01:53PM +0100, Roel Kluin wrote:
> After the loop `for (i = 0; i < config->desc.bNumInterfaces; i++)' if no
> break occurred, i equals config->desc.bNumInterfaces. so if
> usb_control_msg() failed then after goto reset_old_alts we read from
> config->interface[config->desc.bNumInterfaces].
> We can safely decrement i as well if the break occurred.
> 
> Signed-off-by: Roel Kluin <roel.kluin@gmail.com>
> Acked-by: Alan Stern <stern@rowland.harvard.edu>

Bah, yes, you're right. :)  Good catch.

Signed-off-by: Sarah Sharp <sarah.a.sharp@linux.intel.com>

> ---
> 
> > You correctly identified a problem, but your fix is wrong -- or at 
> > least, it is much too complicated.
> 
> Ok,
> 
>  drivers/usb/core/message.c |    2 +-
>  1 files changed, 1 insertions(+), 1 deletions(-)
> 
> diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
> index 9bc95fe..1a48aac 100644
> --- a/drivers/usb/core/message.c
> +++ b/drivers/usb/core/message.c
> @@ -1471,7 +1471,7 @@ int usb_reset_configuration(struct usb_device *dev)
>  	/* If not, reinstate the old alternate settings */
>  	if (retval < 0) {
>  reset_old_alts:
> -		for (; i >= 0; i--) {
> +		for (i--; i >= 0; i--) {
>  			struct usb_interface *intf = config->interface[i];
>  			struct usb_host_interface *alt;
>  
> --
> 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

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

* Re: [PATCH] USB: don't read past config->interface[] if usb_control_msg() fails in usb_reset_configuration()
  2010-02-09 16:01   ` Roel Kluin
  2010-02-09 22:01     ` Sarah Sharp
@ 2010-02-09 22:01     ` Roel Kluin
  2010-02-09 22:52       ` Sarah Sharp
  1 sibling, 1 reply; 11+ messages in thread
From: Roel Kluin @ 2010-02-09 22:01 UTC (permalink / raw)
  To: Roel Kluin
  Cc: Alan Stern, Greg Kroah-Hartman, sarah.a.sharp, linux-usb,
	Andrew Morton, LKML

After the loop `for (i = 0; i < config->desc.bNumInterfaces; i++)' if no
break occurred, i equals config->desc.bNumInterfaces. so if
usb_control_msg() failed then after goto reset_old_alts we read from
config->interface[config->desc.bNumInterfaces].

Reported-by: "Juha Leppanen" <juha_motorsportcom@luukku.com>
Signed-off-by: Roel Kluin <roel.kluin@gmail.com>
---

>> You correctly identified a problem, but your fix is wrong -- or at 
>> least, it is much too complicated. The proper fix goes like this:
> 
>  	/* If not, reinstate the old alternate settings */
>  	if (retval < 0) {
>  reset_old_alts:
> -		for (; i >= 0; i--) {
> +		for (i--; i >= 0; i--) {
>  			struct usb_interface *intf = config->interface[i];
>  			struct usb_host_interface *alt;


Are you really sure this is better? If usb_hcd_alloc_bandwidth() fails,
in the loop _before_ the reset_old_alts label, don't we still have to
reinstate the old alternate settings for that usb_interface
config->interface[i]? This was what my initial patch tried to do.

Alternatively usb_hcd_alloc_bandwidth() could undo what it does if an
error occurs there, then I think i could be decremented.

IOW isn't this the proper fix?

Roel

diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index 9bc95fe..4327eab 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -1489,8 +1489,10 @@ reset_old_alts:
 			USB_REQ_SET_CONFIGURATION, 0,
 			config->desc.bConfigurationValue, 0,
 			NULL, 0, USB_CTRL_SET_TIMEOUT);
-	if (retval < 0)
+	if (retval < 0) {
+		i--;
 		goto reset_old_alts;
+	}
 	mutex_unlock(&hcd->bandwidth_mutex);
 
 	/* re-init hc/hcd interface/endpoint state */

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

* Re: [PATCH] USB: don't read past config->interface[] if  usb_control_msg() fails in usb_reset_configuration()
  2010-02-09 22:01     ` Sarah Sharp
@ 2010-02-09 22:09       ` roel kluin
  2010-02-17 23:40         ` Greg KH
  0 siblings, 1 reply; 11+ messages in thread
From: roel kluin @ 2010-02-09 22:09 UTC (permalink / raw)
  To: Sarah Sharp
  Cc: Alan Stern, Greg Kroah-Hartman, linux-usb, Andrew Morton, LKML

On Tue, Feb 9, 2010 at 11:01 PM, Sarah Sharp
<sarah.a.sharp@linux.intel.com> wrote:
> On Tue, Feb 09, 2010 at 05:01:53PM +0100, Roel Kluin wrote:
>> After the loop `for (i = 0; i < config->desc.bNumInterfaces; i++)' if no
>> break occurred, i equals config->desc.bNumInterfaces. so if
>> usb_control_msg() failed then after goto reset_old_alts we read from
>> config->interface[config->desc.bNumInterfaces].
>> We can safely decrement i as well if the break occurred.
>>
>> Signed-off-by: Roel Kluin <roel.kluin@gmail.com>
>> Acked-by: Alan Stern <stern@rowland.harvard.edu>
>
> Bah, yes, you're right. :)  Good catch.

Could you please confirm whether this patch is the better or the
other (in this same thread)?

Thanks,
Roel

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

* Re: [PATCH] USB: don't read past config->interface[] if usb_control_msg() fails in usb_reset_configuration()
  2010-02-09 22:01     ` Roel Kluin
@ 2010-02-09 22:52       ` Sarah Sharp
  0 siblings, 0 replies; 11+ messages in thread
From: Sarah Sharp @ 2010-02-09 22:52 UTC (permalink / raw)
  To: Roel Kluin; +Cc: Alan Stern, Greg Kroah-Hartman, linux-usb, Andrew Morton, LKML

On Tue, Feb 09, 2010 at 11:01:24PM +0100, Roel Kluin wrote:
> After the loop `for (i = 0; i < config->desc.bNumInterfaces; i++)' if no
> break occurred, i equals config->desc.bNumInterfaces. so if
> usb_control_msg() failed then after goto reset_old_alts we read from
> config->interface[config->desc.bNumInterfaces].
> 
> Reported-by: "Juha Leppanen" <juha_motorsportcom@luukku.com>
> Signed-off-by: Roel Kluin <roel.kluin@gmail.com>
> ---
> 
> >> You correctly identified a problem, but your fix is wrong -- or at 
> >> least, it is much too complicated. The proper fix goes like this:
> > 
> >  	/* If not, reinstate the old alternate settings */
> >  	if (retval < 0) {
> >  reset_old_alts:
> > -		for (; i >= 0; i--) {
> > +		for (i--; i >= 0; i--) {
> >  			struct usb_interface *intf = config->interface[i];
> >  			struct usb_host_interface *alt;
> 
> 
> Are you really sure this is better?

Yes.

> If usb_hcd_alloc_bandwidth() fails, in the loop _before_ the
> reset_old_alts label, don't we still have to reinstate the old
> alternate settings for that usb_interface config->interface[i]? This
> was what my initial patch tried to do.

No, you do not.  Take a look at usb_hcd_alloc_bandwidth() in
drivers/usb/core/hcd.c.  If an allocation fails for interface i when
the HCD's check_bandwidth() function is called, then
hcd->driver->reset_bandwidth is called.  We only need to clean up the
interfaces that have successfully been allocated bandwidth (0 to i - 1).

> Alternatively usb_hcd_alloc_bandwidth() could undo what it does if an
> error occurs there, then I think i could be decremented.

Yes, that's what usb_hcd_alloc_bandwidth() does at the reset label.

Sarah

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

* Re: [PATCH] USB: don't read past config->interface[] if usb_control_msg() fails in usb_reset_configuration()
  2010-02-09 22:09       ` roel kluin
@ 2010-02-17 23:40         ` Greg KH
  2010-02-18  1:36           ` Roel Kluin
  0 siblings, 1 reply; 11+ messages in thread
From: Greg KH @ 2010-02-17 23:40 UTC (permalink / raw)
  To: roel kluin
  Cc: Sarah Sharp, Alan Stern, Greg Kroah-Hartman, linux-usb,
	Andrew Morton, LKML

On Tue, Feb 09, 2010 at 11:09:45PM +0100, roel kluin wrote:
> On Tue, Feb 9, 2010 at 11:01 PM, Sarah Sharp
> <sarah.a.sharp@linux.intel.com> wrote:
> > On Tue, Feb 09, 2010 at 05:01:53PM +0100, Roel Kluin wrote:
> >> After the loop `for (i = 0; i < config->desc.bNumInterfaces; i++)' if no
> >> break occurred, i equals config->desc.bNumInterfaces. so if
> >> usb_control_msg() failed then after goto reset_old_alts we read from
> >> config->interface[config->desc.bNumInterfaces].
> >> We can safely decrement i as well if the break occurred.
> >>
> >> Signed-off-by: Roel Kluin <roel.kluin@gmail.com>
> >> Acked-by: Alan Stern <stern@rowland.harvard.edu>
> >
> > Bah, yes, you're right. :)  Good catch.
> 
> Could you please confirm whether this patch is the better or the
> other (in this same thread)?

Can someone tell me which one is the correct one to apply, but resending
it to me?

thanks,

greg k-h

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

* Re: [PATCH] USB: don't read past config->interface[] if usb_control_msg() fails in usb_reset_configuration()
  2010-02-17 23:40         ` Greg KH
@ 2010-02-18  1:36           ` Roel Kluin
  0 siblings, 0 replies; 11+ messages in thread
From: Roel Kluin @ 2010-02-18  1:36 UTC (permalink / raw)
  To: Greg KH
  Cc: Sarah Sharp, Alan Stern, Greg Kroah-Hartman, linux-usb,
	Andrew Morton, LKML

While looping over the interfaces, if usb_hcd_alloc_bandwidth() fails it calls
hcd->driver->reset_bandwidth(), so there was no need to reinstate the interface
again.

If no break occurred, the index equals config->desc.bNumInterfaces. A
subsequent usb_control_msg() failure resulted in a read from
config->interface[config->desc.bNumInterfaces] at label reset_old_alts.

In either case the last interface should be skipped.

Signed-off-by: Roel Kluin <roel.kluin@gmail.com>
Acked-by: Alan Stern <stern@rowland.harvard.edu>
Acked-by: Sarah Sharp <sarah.a.sharp@linux.intel.com>
---
>> Could you please confirm whether this patch is the better or the
>> other (in this same thread)?
> 
> Can someone tell me which one is the correct one to apply, but resending
> it to me?

This is the one as Sarah kindly explained to me.

Thanks, Roel

diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index 9bc95fe..1a48aac 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -1471,7 +1471,7 @@ int usb_reset_configuration(struct usb_device *dev)
 	/* If not, reinstate the old alternate settings */
 	if (retval < 0) {
 reset_old_alts:
-		for (; i >= 0; i--) {
+		for (i--; i >= 0; i--) {
 			struct usb_interface *intf = config->interface[i];
 			struct usb_host_interface *alt;
 

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

end of thread, other threads:[~2010-02-18  1:28 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-02-09 13:00 [PATCH] USB: don't read past config->interface[] if usb_control_msg() fails in usb_reset_configuration() Roel Kluin
2010-02-09 13:08 ` Greg KH
2010-02-09 13:15   ` roel kluin
2010-02-09 15:12 ` Alan Stern
2010-02-09 16:01   ` Roel Kluin
2010-02-09 22:01     ` Sarah Sharp
2010-02-09 22:09       ` roel kluin
2010-02-17 23:40         ` Greg KH
2010-02-18  1:36           ` Roel Kluin
2010-02-09 22:01     ` Roel Kluin
2010-02-09 22:52       ` Sarah Sharp

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