Skip to contentSkip to conversation

Possible double-free in the usbnet driver

PATCH In review Linus Torvalds 12 participants 23 messages Mar 4, 2016 – Mar 8, 2016 13 messages not in the archive

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

[ Moving this to proper lists ]

Quoted 2 lines · Andrey Konovalov

I found another double-free, this time in the usbnet driver.

Hmm. It doesn't look like a double free to me, at least from the logs you attached.

Quoted 5 lines

Whenever the bind() function fails (drivers/net/usb/usbnet.c:1676) when called from usbnet_probe() (and it can fail due to a invalid usb descriptor), free_netdev() is called for the net device (drivers/net/usb/usbnet.c:1772). Then, free_netdev(net) is called again in usbnet_disconnect() (drivers/net/usb/usbnet.c:1570) causing a double-free.

The KASAN report says that it's a use-after-free in the kworker
thread: the net device got free'd at the end of usbnet_probe(), but some work-struct was apparently active at the time.

There might be a double free later that isn't in your report, though. Do you have the data for that?

But I didn't think we even called the disconnect() function if the "bind()" failed, so I don't think that one should free it. Greg?

So it *sounds* to me like the usbnet "bind()" routine ended up returning an error, but doing so *after* it had already activated the structure somehow.

Which particular usbnet bind routine is this? There are multiple sub-drivers for usbnet that all do different things.

For example, it *looks* like the cdc_ncm_bind() will have done a usbnet_link_change() even if the bind fails. So now we've done a usbnet_defer_kevent() even though we're failing, and then that sets the ball rolling to later touch the netdev that we're freeing due to the failure.

But I may be *entirely* misreading this thing.

Anyway, I'm cc'ing the usbnet people who actually know the code (and netdev).

The proper fix may be to just cancel any work that might have been set up before freeing. Or maybe that netdev *does* get free'd later some other way properly. Let's see what the experts on the usbnet driver say.

Linus
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): CA+55aFxqwjs5gs6Fw2jmTteWM4hZTnr7Ls111ExNTieObLs82Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#2 lore raw
Quoted 15 lines · Linus Torvalds

[ Moving this to proper lists ]

Quoted 2 lines · Andrey Konovalov

I found another double-free, this time in the usbnet driver.

Hmm. It doesn't look like a double free to me, at least from the logs you attached.

Quoted 5 lines

Whenever the bind() function fails (drivers/net/usb/usbnet.c:1676) when called from usbnet_probe() (and it can fail due to a invalid usb descriptor), free_netdev() is called for the net device (drivers/net/usb/usbnet.c:1772). Then, free_netdev(net) is called again in usbnet_disconnect() (drivers/net/usb/usbnet.c:1570) causing a double-free.

Quoted 6 lines

The KASAN report says that it's a use-after-free in the kworker
thread: the net device got free'd at the end of usbnet_probe(), but some work-struct was apparently active at the time.

There might be a double free later that isn't in your report, though. Do you have the data for that?

I uploaded full kernel log here:
https://gist.github.com/xairy/6a244871959187209fdb

My initial idea was that an object is freed by free_netdev(), then allocated somewhere during execution of kworker-related code and then this object gets freed by free_netdev() again and that's why we have such use-after-free reports. That didn't explain the deallocation stack trace in the report, but I thought this was due to a racy-use-after-free.

I just added the following debug output:

drivers/net/usb/usbnet.c +2 -0
index 0b0ba7e..f7e1415 100644
@@ -1567,6 +1567,7 @@ void usbnet_disconnect (struct usb_interface *intf)
        usb_free_urb(dev->interrupt);
        kfree(dev->padding_pkt);
 
+       pr_err("usbnet_disconnect(): freeing netdev: %p\n", net);
        free_netdev(net);
 }
 EXPORT_SYMBOL_GPL(usbnet_disconnect);
@@ -1769,6 +1770,7 @@ out3:
        if (info->unbind)
                info->unbind (dev, udev);
 out1:
+       pr_err("usbnet_probe(): freeing netdev: %p\n", net);
        free_netdev(net);
 out:
        return status;

and when I run the vm and connect the device I get:

[   23.672662] cdc_ncm 1-1:1.6: bind() failure
[   23.673447] usbnet_probe(): freeing netdev: ffff88006ab48000
[   23.675822] usbnet_probe(): freeing netdev: ffff88006ab48000

So this seems to be a double-free (or at least a double free_netdev() call), but the object gets freed twice from usbnet_probe() and not from usbnet_disconnect(), so you're right that the latter doesn't get called. I'm not sure how usbnet_probe() ends up being called twice.

Quoted 10 lines

But I didn't think we even called the disconnect() function if the "bind()" failed, so I don't think that one should free it. Greg?

So it *sounds* to me like the usbnet "bind()" routine ended up returning an error, but doing so *after* it had already activated the structure somehow.

Which particular usbnet bind routine is this? There are multiple sub-drivers for usbnet that all do different things.

cdc_ncm_bind()

Quoted 9 lines

For example, it *looks* like the cdc_ncm_bind() will have done a usbnet_link_change() even if the bind fails. So now we've done a usbnet_defer_kevent() even though we're failing, and then that sets the ball rolling to later touch the netdev that we're freeing due to the failure.

But I may be *entirely* misreading this thing.

Quoted 8 lines

Anyway, I'm cc'ing the usbnet people who actually know the code (and netdev).

The proper fix may be to just cancel any work that might have been set up before freeing. Or maybe that netdev *does* get free'd later some other way properly. Let's see what the experts on the usbnet driver say.

Linus

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

A message that is not in the archive (sent off-list or not yet received): CA+fCnZfS51F7WZEM1YTSPDMWSzvBTJWGf5cRWv5LrNCSOf_-qA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#4 lore raw
Quoted 10 lines · Andrey Konovalov

and when I run the vm and connect the device I get:

[   23.672662] cdc_ncm 1-1:1.6: bind() failure
[   23.673447] usbnet_probe(): freeing netdev: ffff88006ab48000
[   23.675822] usbnet_probe(): freeing netdev: ffff88006ab48000

So this seems to be a double-free (or at least a double free_netdev() call), but the object gets freed twice from usbnet_probe() and not from usbnet_disconnect(), so you're right that the latter doesn't get called. I'm not sure how usbnet_probe() ends up being called twice.

Do you have lsusb?

Regards
Oliver

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

A message that is not in the archive (sent off-list or not yet received): [email protected]

#6 lore raw
Quoted 7 lines · Oliver Neukum
Quoted 6 lines · Andrey Konovalov

and when I run the vm and connect the device I get:

[   23.672662] cdc_ncm 1-1:1.6: bind() failure
[   23.673447] usbnet_probe(): freeing netdev: ffff88006ab48000
[   23.675822] usbnet_probe(): freeing netdev: ffff88006ab48000
Quoted 6 lines
Quoted 4 lines

So this seems to be a double-free (or at least a double free_netdev() call), but the object gets freed twice from usbnet_probe() and not from usbnet_disconnect(), so you're right that the latter doesn't get called. I'm not sure how usbnet_probe() ends up being called twice.

Do you have lsusb?

You mean inside the vm?
I do.

Quoted 5 lines

Regards
Oliver

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

#7 lore raw
Quoted 13 lines · Andrey Konovalov
Quoted 12 lines · Oliver Neukum
Quoted 10 lines · Andrey Konovalov

and when I run the vm and connect the device I get:

[   23.672662] cdc_ncm 1-1:1.6: bind() failure
[   23.673447] usbnet_probe(): freeing netdev: ffff88006ab48000
[   23.675822] usbnet_probe(): freeing netdev: ffff88006ab48000

So this seems to be a double-free (or at least a double free_netdev() call), but the object gets freed twice from usbnet_probe() and not from usbnet_disconnect(), so you're right that the latter doesn't get called. I'm not sure how usbnet_probe() ends up being called twice.

Quoted 4 lines
Quoted 1 line

Do you have lsusb?

You mean inside the vm?
I do.

Or did you want the faulty device descriptor itself?

I used this:

Speed High
Bus 004 Device 003: ID 0bdb:1911
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 2.00

  bDeviceClass             2 Communications
  bDeviceSubClass          0
  bDeviceProtocol          0
  bMaxPacketSize0         64
  idVendor            0x0000
  idProduct           0x0000
  bcdDevice             0.00
  iManufacturer            1
  iProduct                 2
  iSerial                  3
  bNumConfigurations       1

Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 371
bNumInterfaces 11
bConfigurationValue 1
iConfiguration 4
bmAttributes 0xe0
Self Powered
Remote Wakeup
bMaxPower 0mA
Interface Descriptor:

  bLength                  9
  bDescriptorType          4
  bInterfaceNumber         6
  bAlternateSetting        0
  bNumEndpoints            1
  bInterfaceClass          2 Communications

bInterfaceSubClass 13
bInterfaceProtocol 0
iInterface 11

Quoted 6 lines
Quoted 6 lines

Regards
Oliver

#8 lore raw
In reply to Andrey Konovalov
Quoted 11 lines · Andrey Konovalov

and when I run the vm and connect the device I get:

[   23.672662] cdc_ncm 1-1:1.6: bind() failure
[   23.673447] usbnet_probe(): freeing netdev: ffff88006ab48000
[   23.675822] usbnet_probe(): freeing netdev: ffff88006ab48000

So this seems to be a double-free (or at least a double free_netdev() call), but the object gets freed twice from usbnet_probe() and not from usbnet_disconnect(), so you're right that the latter doesn't get called. I'm not sure how usbnet_probe() ends up being called twice.

I still don't think it's a double free. I think the probe thing is called twice, and that's why to different allocations get free'd twice (and the reason it's the same pointer is that the same allocation got reused)

But I don't know that driver, really.

Quoted 4 lines
Quoted 2 lines

Which particular usbnet bind routine is this? There are multiple sub-drivers for usbnet that all do different things.

cdc_ncm_bind()

Ok, so that matches my theory.

Does this attached stupid patch make the warning go away? Because from what I can tell, usbnet_link_change() really will start using that "dev" that simply isn't valid - and will be free'd - if the bind fails.

So you have usbnet_defer_kevent() getting triggered, which in turn ends up using "usbnet->kevent"

But somebody like Oliver is really the right person to check this. For example, it's entirely possible that we should just instead do

        cancel_work_sync(&dev->kevent);

before the "free_netdev(net)" in the "out1" label.

And there might be other things that bind() can have touched than the kevent workqueue.

Linus

 drivers/net/usb/cdc_ncm.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/usb/cdc_ncm.c +3 -1
index dc0212c3cc28..5878b54cc563 100644
@@ -995,6 +995,8 @@ static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
 	 * placed NDP.
 	 */
 	ret = cdc_ncm_bind_common(dev, intf, CDC_NCM_DATA_ALTSETTING_NCM, 0);
+	if (ret < 0)
+		return ret;
 
 	/*
 	 * We should get an event when network connection is "connected" or
@@ -1003,7 +1005,7 @@ static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
 	 * start IPv6 negotiation and more.
 	 */
 	usbnet_link_change(dev, 0, 0);
-	return ret;
+	return 0;
 }
 
 static void cdc_ncm_align_tail(struct sk_buff *skb, size_t modulus, size_t remainder, size_t max)
#9 lore raw
Quoted 23 lines · Linus Torvalds
Quoted 11 lines · Andrey Konovalov

and when I run the vm and connect the device I get:

[   23.672662] cdc_ncm 1-1:1.6: bind() failure
[   23.673447] usbnet_probe(): freeing netdev: ffff88006ab48000
[   23.675822] usbnet_probe(): freeing netdev: ffff88006ab48000

So this seems to be a double-free (or at least a double free_netdev() call), but the object gets freed twice from usbnet_probe() and not from usbnet_disconnect(), so you're right that the latter doesn't get called. I'm not sure how usbnet_probe() ends up being called twice.

I still don't think it's a double free. I think the probe thing is called twice, and that's why to different allocations get free'd twice (and the reason it's the same pointer is that the same allocation got reused)

But I don't know that driver, really.

Quoted 3 lines
Quoted 2 lines

Which particular usbnet bind routine is this? There are multiple sub-drivers for usbnet that all do different things.

Quoted 8 lines
Quoted 1 line

cdc_ncm_bind()

Ok, so that matches my theory.

Does this attached stupid patch make the warning go away? Because from what I can tell, usbnet_link_change() really will start using that "dev" that simply isn't valid - and will be free'd - if the bind fails.

Yes, KASAN doesn't report anything with your patch.

Quoted 15 lines

So you have usbnet_defer_kevent() getting triggered, which in turn ends up using "usbnet->kevent"

But somebody like Oliver is really the right person to check this. For example, it's entirely possible that we should just instead do

        cancel_work_sync(&dev->kevent);

before the "free_netdev(net)" in the "out1" label.

And there might be other things that bind() can have touched than the kevent workqueue.

Linus

maintainer
#10 lore raw
In reply to Linus Torvalds
Quoted 9 lines · Linus Torvalds

So you have usbnet_defer_kevent() getting triggered, which in turn ends up using "usbnet->kevent"

But somebody like Oliver is really the right person to check this. For example, it's entirely possible that we should just instead do

        cancel_work_sync(&dev->kevent);

before the "free_netdev(net)" in the "out1" label.

Hi Bjorn,

I thinbk Linus has analyzed this correctly, but the fix really needs to cancel the work, because we can also fail later after bind() has already run. However, still cdc-ncm and the other drivers should clean up after themselves if bind() fails, as usbnet really cannot known what the subdrivers have done.

So in conclusion, I think Linus' fix should also go into cdc-ncm.

Regards
Oliver

A message that is not in the archive (sent off-list or not yet received): 1457193090.8935.7.camel-IBi9RG/[email protected]

#12 lore raw
Quoted 15 lines · Oliver Neukum
Quoted 4 lines · Linus Torvalds

So you have usbnet_defer_kevent() getting triggered, which in turn ends up using "usbnet->kevent"

But somebody like Oliver is really the right person to check this.

For

Quoted 5 lines

example, it's entirely possible that we should just instead do

        cancel_work_sync(&dev->kevent);

before the "free_netdev(net)" in the "out1" label.

Hi Bjorn,

Quoted 7 lines

I thinbk Linus has analyzed this correctly, but the fix really needs to cancel the work, because we can also fail later after bind() has already run. However, still cdc-ncm and the other drivers should clean up after themselves if bind() fails, as usbnet really cannot known what the subdrivers have done.

So in conclusion, I think Linus' fix should also go into cdc-ncm.

Definitely. The patch is so obviously correct that we can only wonder how it was possible to miss it it the first place :)

Will take a look to see if we could do a better job cleaning up in other places.

(I do also wonder a bit about the failure to bind - is that expected or is there some bug in the cdc_ncm descriptor parsing?)

Bjørn

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

A message that is not in the archive (sent off-list or not yet received): DBDB517D-E4A4-4422-AECE-52194FE2AED0-yOkvZcmFvRU@public.gmane.org

#14 lore raw
Quoted 5 lines · Bjørn Mork

Definitely. The patch is so obviously correct that we can only wonder how it was possible to miss it it the first place :)

Will take a look to see if we could do a better job cleaning up in other places.

What should I do for 4.5? Will there be a pull request for this, or should I just commit my cdc_ncm_bind() patch directly?

Linus
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): CA+55aFw43uKkUK-h=VMwXcb9NM+g2AdeB960kWb4YihmjQ8DRw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#16 lore raw

From: Linus Torvalds <torvalds-de/[email protected]>
Date: Mon, 7 Mar 2016 10:13:09 -0800

Quoted 9 lines · Bjørn Mork
Quoted 5 lines

Definitely. The patch is so obviously correct that we can only wonder how it was possible to miss it it the first place :)

Will take a look to see if we could do a better job cleaning up in other places.

What should I do for 4.5? Will there be a pull request for this, or should I just commit my cdc_ncm_bind() patch directly?

Yes I plan to send you a pull request today with Oliver's fix.

Assuming this is what you guys are referring to:

commit 1666984c8625b3db19a9abc298931d35ab7bc64b

Author: Oliver Neukum <oneukum-IBi9RG/[email protected]>
Date:   Mon Mar 7 11:31:10 2016 +0100

usbnet: cleanup after bind() in probe()

In case bind() works, but a later error forces bailing in probe() in error cases work and a timer may be scheduled. They must be killed. This fixes an error case related to the double free reported in
http://www.spinics.net/lists/netdev/msg367669.html
and needs to go on top of Linus' fix to cdc-ncm.

drivers/net/usb/usbnet.c +7 -0
index 0b0ba7e..1079812 100644
@@ -1769,6 +1769,13 @@ out3:
 	if (info->unbind)
 		info->unbind (dev, udev);
 out1:
+	/* subdrivers must undo all they did in bind() if they
+	 * fail it, but we may fail later and a deferred kevent
+	 * may trigger an error resubmitting itself and, worse,
+	 * schedule a timer. So we kill it all just in case.
+	 */
+	cancel_work_sync(&dev->kevent);
+	del_timer_sync(&dev->delay);
 	free_netdev(net);
 out:
 	return status;

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

A message that is not in the archive (sent off-list or not yet received): 20160307.141100.1511700720120062677.davem-fT/PcQaiUtIeIZ0/[email protected]

#18 lore raw
Quoted 30 lines · David Miller

From: Linus Torvalds <torvalds-de/[email protected]>
Date: Mon, 7 Mar 2016 10:13:09 -0800

Quoted 9 lines · Bjørn Mork
Quoted 5 lines

Definitely. The patch is so obviously correct that we can only wonder how it was possible to miss it it the first place :)

Will take a look to see if we could do a better job cleaning up in other places.

What should I do for 4.5? Will there be a pull request for this, or should I just commit my cdc_ncm_bind() patch directly?

Yes I plan to send you a pull request today with Oliver's fix.

Assuming this is what you guys are referring to:

commit 1666984c8625b3db19a9abc298931d35ab7bc64b

Author: Oliver Neukum <oneukum-IBi9RG/[email protected]>
Date:   Mon Mar 7 11:31:10 2016 +0100

usbnet: cleanup after bind() in probe()

In case bind() works, but a later error forces bailing in probe() in error cases work and a timer may be scheduled. They must be killed. This fixes an error case related to the double free reported in
http://www.spinics.net/lists/netdev/msg367669.html
and needs to go on top of Linus' fix to cdc-ncm.

Quoted 2 lines

Could you also add:

?

Thanks in advance.

Quoted 9 lines
drivers/net/usb/usbnet.c +0 -0
index 0b0ba7e..1079812 100644
@@ -1769,6 +1769,13 @@ out3:
        if (info->unbind)
                info->unbind (dev, udev);
 out1:

drivers/net/usb/usbnet.c out3: · line 1781

Quoted 10 lines
drivers/net/usb/usbnet.c +7 -0
+       /* subdrivers must undo all they did in bind() if they
+        * fail it, but we may fail later and a deferred kevent
+        * may trigger an error resubmitting itself and, worse,
+        * schedule a timer. So we kill it all just in case.
+        */
+       cancel_work_sync(&dev->kevent);
+       del_timer_sync(&dev->delay);
        free_netdev(net);
 out:
        return status;

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

A message that is not in the archive (sent off-list or not yet received): CA+fCnZdurxGBsOrANb_m5BLK1BKzH3J_GmZ=dbH=ABThgFNGxg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#20 lore raw

From: Andrey Konovalov <[email protected]>
Date: Mon, 7 Mar 2016 22:50:41 +0300

Quoted 31 lines · David Miller
Quoted 30 lines

From: Linus Torvalds <torvalds-de/[email protected]>
Date: Mon, 7 Mar 2016 10:13:09 -0800

Quoted 9 lines · Bjørn Mork
Quoted 5 lines

Definitely. The patch is so obviously correct that we can only wonder how it was possible to miss it it the first place :)

Will take a look to see if we could do a better job cleaning up in other places.

What should I do for 4.5? Will there be a pull request for this, or should I just commit my cdc_ncm_bind() patch directly?

Yes I plan to send you a pull request today with Oliver's fix.

Assuming this is what you guys are referring to:

commit 1666984c8625b3db19a9abc298931d35ab7bc64b

Author: Oliver Neukum <oneukum-IBi9RG/[email protected]>
Date:   Mon Mar 7 11:31:10 2016 +0100

usbnet: cleanup after bind() in probe()

In case bind() works, but a later error forces bailing in probe() in error cases work and a timer may be scheduled. They must be killed. This fixes an error case related to the double free reported in
http://www.spinics.net/lists/netdev/msg367669.html
and needs to go on top of Linus' fix to cdc-ncm.

Quoted 6 lines
Quoted 2 lines

Could you also add:

?

Sorry it's already committed to my tree and I can't redo the commit message once that happens since my tree has static history.
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#21 lore raw

PATCH 1/1 cdc_ncm: do not call usbnet_link_change from cdc_ncm_bind

usbnet_link_change will call schedule_work and should be avoided if bind is failing. Otherwise we will end up with scheduled work referring to a netdev which has gone away.

Instead of making the call conditional, we can just defer it to usbnet_probe, using the driver_info flag made for this purpose.

Fixes 8a34b0ae8778 ("usbnet: cdc_ncm: apply usbnet_link_change")

---

Quoted 8 lines · David Miller

From: Andrey Konovalov <[email protected]>

Quoted 3 lines

Could you also add:

?

Sorry it's already committed to my tree and I can't redo the commit message once that happens since my tree has static history.

Even with Oliver's generic fix we should still fix the inconsistency in cdc_ncm, as pointed out by Linus.

This is a slightly different approach than the patch proposed by Linus. When I started looking at this I couldn't figure out why we were doing this differently in this driver from all the other usbnet drivers disabling the link at probe time. So let's make it consistent. Then at least we get consistent bugs :)

Bjørn

 drivers/net/usb/cdc_ncm.c | 20 +++++---------------
 1 file changed, 5 insertions(+), 15 deletions(-)
drivers/net/usb/cdc_ncm.c +5 -15
index be927964375b..86ba30ba35e8 100644
@@ -988,8 +988,6 @@ EXPORT_SYMBOL_GPL(cdc_ncm_select_altsetting);
 
 static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
 {
-	int ret;
-
 	/* MBIM backwards compatible function? */
 	if (cdc_ncm_select_altsetting(intf) != CDC_NCM_COMM_ALTSETTING_NCM)
 		return -ENODEV;
@@ -998,16 +996,7 @@ static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
 	 * Additionally, generic NCM devices are assumed to accept arbitrarily
 	 * placed NDP.
 	 */
-	ret = cdc_ncm_bind_common(dev, intf, CDC_NCM_DATA_ALTSETTING_NCM, 0);
-
-	/*
-	 * We should get an event when network connection is "connected" or
-	 * "disconnected". Set network connection in "disconnected" state
-	 * (carrier is OFF) during attach, so the IP network stack does not
-	 * start IPv6 negotiation and more.
-	 */
-	usbnet_link_change(dev, 0, 0);
-	return ret;
+	return cdc_ncm_bind_common(dev, intf, CDC_NCM_DATA_ALTSETTING_NCM, 0);
 }
 
 static void cdc_ncm_align_tail(struct sk_buff *skb, size_t modulus, size_t remainder, size_t max)
@@ -1590,7 +1579,8 @@ static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
 
 static const struct driver_info cdc_ncm_info = {
 	.description = "CDC NCM",
-	.flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET,
+	.flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
+			| FLAG_LINK_INTR,
 	.bind = cdc_ncm_bind,
 	.unbind = cdc_ncm_unbind,
 	.manage_power = usbnet_manage_power,
@@ -1603,7 +1593,7 @@ static const struct driver_info cdc_ncm_info = {
 static const struct driver_info wwan_info = {
 	.description = "Mobile Broadband Network Device",
 	.flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
-			| FLAG_WWAN,
+			| FLAG_LINK_INTR | FLAG_WWAN,
 	.bind = cdc_ncm_bind,
 	.unbind = cdc_ncm_unbind,
 	.manage_power = usbnet_manage_power,
@@ -1616,7 +1606,7 @@ static const struct driver_info wwan_info = {
 static const struct driver_info wwan_noarp_info = {
 	.description = "Mobile Broadband Network Device (NO ARP)",
 	.flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
-			| FLAG_WWAN | FLAG_NOARP,
+			| FLAG_LINK_INTR | FLAG_WWAN | FLAG_NOARP,
 	.bind = cdc_ncm_bind,
 	.unbind = cdc_ncm_unbind,
 	.manage_power = usbnet_manage_power,
Signature · 1 line
2.1.4

A message that is not in the archive (sent off-list or not yet received): 87k2le815w.fsf_-_-lbf33ChDnrE/[email protected]

#23 lore raw

Re: [PATCH] cdc_ncm: do not call usbnet_link_change from cdc_ncm_bind

From: Bjørn Mork <[email protected]>
Date: Mon, 7 Mar 2016 21:15:36 +0100

Quoted 12 lines

usbnet_link_change will call schedule_work and should be avoided if bind is failing. Otherwise we will end up with scheduled work referring to a netdev which has gone away.

Instead of making the call conditional, we can just defer it to usbnet_probe, using the driver_info flag made for this purpose.

Fixes 8a34b0ae8778 ("usbnet: cdc_ncm: apply usbnet_link_change")

...

Quoted 8 lines

Even with Oliver's generic fix we should still fix the inconsistency in cdc_ncm, as pointed out by Linus.

This is a slightly different approach than the patch proposed by Linus. When I started looking at this I couldn't figure out why we were doing this differently in this driver from all the other usbnet drivers disabling the link at probe time. So let's make it consistent. Then at least we get consistent bugs :)

Fair enough, applied and queued up for -stable.

Thanks.
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#24 lore raw

Re: [PATCH] cdc_ncm: do not call usbnet_link_change from cdc_ncm_bind

In reply to Bjørn Mork
Quoted 7 lines · Bjørn Mork

usbnet_link_change will call schedule_work and should be avoided if bind is failing. Otherwise we will end up with scheduled work referring to a netdev which has gone away.

Instead of making the call conditional, we can just defer it to usbnet_probe, using the driver_info flag made for this purpose.

So looking at this, I wonder...

Why is that FLAG_LINK_INTR thing not just always used?

The _only_ thing that FLAG_LINK_INTR does is to cause

        usbnet_link_change(dev, 0, 0);

to be called after network device attach. That doesn't seem to be controversial.

Looking at some examples, we have ax88179_178a.c that doesn't set the flag, but instead does that usbnet_link_change() call at the end of ax88179_bind().

There are a few drivers that seem to never call that usbnet_link_change() at all, and don't have that FLAG_LINK_INTR flag. Would they break?

Stupid grep:

git grep -lw FLAG_ETHER |
xargs grep -L FLAG_LINK_INTR |
xargs grep -L usbnet_link_change |
sed 's:drivers/net/usb/::'

gives

    cdc_eem.c
    ch9200.c
    cx82310_eth.c
    int51x1.c
    rndis_host.c

so maybe that FLAG_LINK_INTR si required.

Why is it called "FLAG_LINK_INTR" anyway? There doesn't seem to be anything "INTR" about it.

Linus

A message that is not in the archive (sent off-list or not yet received): CA+55aFxt7zWW+-EkwCbAWCb9wkgVswYJNAz86bc_QRcv1pBHZw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#26 lore raw

Re: [PATCH] cdc_ncm: do not call usbnet_link_change from cdc_ncm_bind

Quoted 8 lines · Linus Torvalds

Why is that FLAG_LINK_INTR thing not just always used?

The _only_ thing that FLAG_LINK_INTR does is to cause

        usbnet_link_change(dev, 0, 0);

to be called after network device attach. That doesn't seem to be controversial.

It depends on the devices' capabilities.
For a few old devices that would be deadly, because they cannot indicate that a link goes up again.

Quoted 7 lines

Looking at some examples, we have ax88179_178a.c that doesn't set the flag, but instead does that usbnet_link_change() call at the end of ax88179_bind().

There are a few drivers that seem to never call that usbnet_link_change() at all, and don't have that FLAG_LINK_INTR flag. Would they break?

Yes. If we did the call unconditionally. We could not do it, then we'd see some spurious detection of interfaces being up, but something breaks. Today I'd probably require a flag for those cases that do not want the call to be made, but the distinction as such is necessary.

Regards
Oliver

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

#27 lore raw

Re: [PATCH] cdc_ncm: do not call usbnet_link_change from cdc_ncm_bind

In reply to a message not in the archive
Quoted 9 lines · Linus Torvalds

So looking at this, I wonder...

Why is that FLAG_LINK_INTR thing not just always used?

The _only_ thing that FLAG_LINK_INTR does is to cause

        usbnet_link_change(dev, 0, 0);

to be called after network device attach. That doesn't seem to be controversial.

Not all usbnet drivers support carrier detection, which is required to ever bring the link up again.

Quoted 7 lines

Looking at some examples, we have ax88179_178a.c that doesn't set the flag, but instead does that usbnet_link_change() call at the end of ax88179_bind().

There are a few drivers that seem to never call that usbnet_link_change() at all, and don't have that FLAG_LINK_INTR flag. Would they break?

Yes. Drivers without carrier detection will be "down" forever.

Quoted 2 lines

Why is it called "FLAG_LINK_INTR" anyway? There doesn't seem to be anything "INTR" about it.

Beats me. I can only say that I always find naming difficult... We could ask Ben, who introduced it in:

commit 37e8273cd30592d3a82bcb70cbb1bdc4eaeb6b71

Author: Ben Hutchings <ben-/[email protected]>
Date:   Wed Nov 4 15:29:52 2009 +0000

usbnet: Set link down initially for drivers that update link state

Some usbnet drivers update link state while others do not due to hardware limitations. Add a flag to distinguish those that do, and set the link down initially for their devices.

This is intended to fix this bug: http://bugs.debian.org/444043

But I guess it doesn't really matter.

Bjørn
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 871t7k7mgo.fsf-lbf33ChDnrE/[email protected]

#29 lore raw

Re: [PATCH] cdc_ncm: do not call usbnet_link_change from cdc_ncm_bind

Quoted 5 lines · Bjørn Mork
Quoted 2 lines

Why is it called "FLAG_LINK_INTR" anyway? There doesn't seem to be anything "INTR" about it.

Beats me. I can only say that I always find naming difficult... We could ask Ben, who introduced it in:

It used to be done over USB interrupt endpoints.

Regards
Oliver

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

#30 lore raw

Re: [PATCH] cdc_ncm: do not call usbnet_link_change from cdc_ncm_bind

In reply to Bjørn Mork
Quoted 23 lines · Bjørn Mork
Quoted 10 lines · Linus Torvalds

So looking at this, I wonder...

Why is that FLAG_LINK_INTR thing not just always used?

The _only_ thing that FLAG_LINK_INTR does is to cause

        usbnet_link_change(dev, 0, 0);

to be called after network device attach. That doesn't seem to be controversial.

Not all usbnet drivers support carrier detection, which is required to ever bring the link up again.

Quoted 8 lines

Looking at some examples, we have ax88179_178a.c that doesn't set the flag, but instead does that usbnet_link_change() call at the end of ax88179_bind().

There are a few drivers that seem to never call that usbnet_link_change() at all, and don't have that FLAG_LINK_INTR flag. Would they break?

Quoted 7 lines

Yes.  Drivers without carrier detection will be "down" forever.

Quoted 3 lines

Why is it called "FLAG_LINK_INTR" anyway? There doesn't seem to be anything "INTR" about it.

Beats me.  I can only say that I always find naming difficult... We could ask Ben, who introduced it in:

[...]

It is supposed to imply that the device generates link-change interrupts.  Of course it is also possible for a device driver to satisfy the requirement by polling the link state.

Ben.

Signature · 2 lines
Ben Hutchings
Sturgeon's Law: Ninety percent of everything is crap.
#31 lore raw
In reply to a message not in the archive
Quoted 2 lines · Andrey Konovalov

Could you also add:

Well, the exact bug you reported is fixed in Bjorn's patch the way Linus suggested. I'm fixing just a further race that would require an error condition on top of what you have seen.
So I don't think your Reported-by would be totally appropriate.

Regards
Oliver

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

A message that is not in the archive (sent off-list or not yet received): 1457386754.3404.15.camel-IBi9RG/[email protected]

#33 lore raw
Quoted 10 lines · Oliver Neukum
Quoted 2 lines · Andrey Konovalov

Could you also add:

Well, the exact bug you reported is fixed in Bjorn's patch the way Linus suggested. I'm fixing just a further race that would require an error condition on top of what you have seen.
So I don't think your Reported-by would be totally appropriate.

Oh, OK, Sorry. I thought this was a part of the same fix.

Quoted 5 lines

Regards
Oliver

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

A message that is not in the archive (sent off-list or not yet received): CA+55aFwxbs_hLG58Q_xSK2vpufjmwMk-xkqxTNh_5h-A8y4vbg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#35 lore raw
Quoted 13 lines · Linus Torvalds
Quoted 11 lines · Andrey Konovalov

and when I run the vm and connect the device I get:

[   23.672662] cdc_ncm 1-1:1.6: bind() failure
[   23.673447] usbnet_probe(): freeing netdev: ffff88006ab48000
[   23.675822] usbnet_probe(): freeing netdev: ffff88006ab48000

So this seems to be a double-free (or at least a double free_netdev() call), but the object gets freed twice from usbnet_probe() and not from usbnet_disconnect(), so you're right that the latter doesn't get called. I'm not sure how usbnet_probe() ends up being called twice.

Quoted 4 lines

I still don't think it's a double free. I think the probe thing is called twice, and that's why to different allocations get free'd twice (and the reason it's the same pointer is that the same allocation got reused)

FYI, we have a KASAN patch in flight that adds "quarantine" for freed memory (memory is reused only after a significant delay). It should help to easily differentiate between use-after-free, double-free and heap-out-of-bound. Yes, now it is confusing.

Quoted 20 lines

But I don't know that driver, really.

Quoted 4 lines
Quoted 2 lines

Which particular usbnet bind routine is this? There are multiple sub-drivers for usbnet that all do different things.

cdc_ncm_bind()

Ok, so that matches my theory.

Does this attached stupid patch make the warning go away? Because from what I can tell, usbnet_link_change() really will start using that "dev" that simply isn't valid - and will be free'd - if the bind fails.

So you have usbnet_defer_kevent() getting triggered, which in turn ends up using "usbnet->kevent"

But somebody like Oliver is really the right person to check this. For example, it's entirely possible that we should just instead do

Quoted 8 lines
        cancel_work_sync(&dev->kevent);

before the "free_netdev(net)" in the "out1" label.

And there might be other things that bind() can have touched than the kevent workqueue.

Linus

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