From: Lukas Wunner <lukas@wunner.de>
To: Oliver Neukum <oneukum@suse.com>,
"David S. Miller" <davem@davemloft.net>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Jann Horn <jannh@google.com>,
Oleksij Rempel <o.rempel@pengutronix.de>
Cc: netdev@vger.kernel.org, linux-usb@vger.kernel.org,
Andrew Lunn <andrew@lunn.ch>, Eric Dumazet <edumazet@google.com>,
Jacky Chou <jackychou@asix.com.tw>, Willy Tarreau <w@1wt.eu>,
Lino Sanfilippo <LinoSanfilippo@gmx.de>,
Philipp Rosenberger <p.rosenberger@kunbus.com>,
Heiner Kallweit <hkallweit1@gmail.com>
Subject: [PATCH] usbnet: Fix use-after-free on disconnect
Date: Wed, 13 Apr 2022 16:16:19 +0200 [thread overview]
Message-ID: <127121d9d933ebe3fc13f9f91cc33363d6a8a8ac.1649859147.git.lukas@wunner.de> (raw)
Jann Horn reports a use-after-free on disconnect of a USB Ethernet
(ax88179_178a.c). Oleksij Rempel has witnessed the same issue with a
different driver (ax88172a.c).
Jann's report (linked below) explains the root cause in great detail.
Briefly, USB Ethernet drivers schedule work (usbnet_deferred_kevent())
which in turn schedules another work (linkwatch_event()). The problem
is that usbnet_disconnect() first synchronizes with linkwatch_event()
and only then with usbnet_deferred_kevent(). That allows
usbnet_deferred_kevent() to schedule another linkwatch_event() after
synchronization with the latter. In other words, scheduling happens
in AB order and synchronization on disconnect happens in BA order.
The correct order is to first synchronize with usbnet_deferred_kevent()
(and prevent any future execution), then with linkwatch_event(), i.e.
in AB order.
Reported-by: Jann Horn <jannh@google.com>
Link: https://lore.kernel.org/netdev/CAG48ez0MHBbENX5gCdHAUXZ7h7s20LnepBF-pa5M=7Bi-jZrEA@mail.gmail.com/
Reported-by: Oleksij Rempel <o.rempel@pengutronix.de>
Link: https://lore.kernel.org/netdev/20220315113841.GA22337@pengutronix.de/
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Cc: stable@vger.kernel.org
Cc: Oliver Neukum <oneukum@suse.com>
Cc: Andrew Lunn <andrew@lunn.ch>
---
drivers/net/usb/usbnet.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/drivers/net/usb/usbnet.c b/drivers/net/usb/usbnet.c
index 9a6450f796dc..6c67ae48afeb 100644
--- a/drivers/net/usb/usbnet.c
+++ b/drivers/net/usb/usbnet.c
@@ -469,6 +469,9 @@ static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb,
*/
void usbnet_defer_kevent (struct usbnet *dev, int work)
{
+ if (dev->intf->condition == USB_INTERFACE_UNBINDING)
+ return;
+
set_bit (work, &dev->flags);
if (!schedule_work (&dev->kevent))
netdev_dbg(dev->net, "kevent %s may have been dropped\n", usbnet_event_names[work]);
@@ -1619,11 +1622,11 @@ void usbnet_disconnect (struct usb_interface *intf)
if (dev->driver_info->unbind)
dev->driver_info->unbind(dev, intf);
+ cancel_work_sync(&dev->kevent);
+
net = dev->net;
unregister_netdev (net);
- cancel_work_sync(&dev->kevent);
-
usb_scuttle_anchored_urbs(&dev->deferred);
usb_kill_urb(dev->interrupt);
--
2.35.2
next reply other threads:[~2022-04-13 14:17 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-13 14:16 Lukas Wunner [this message]
2022-04-13 18:59 ` [PATCH] usbnet: Fix use-after-free on disconnect Oliver Neukum
2022-04-14 10:58 ` Lukas Wunner
2022-04-14 11:07 ` Greg Kroah-Hartman
2022-04-17 7:28 ` Lukas Wunner
2022-04-14 11:20 ` Oliver Neukum
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=127121d9d933ebe3fc13f9f91cc33363d6a8a8ac.1649859147.git.lukas@wunner.de \
--to=lukas@wunner.de \
--cc=LinoSanfilippo@gmx.de \
--cc=andrew@lunn.ch \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=hkallweit1@gmail.com \
--cc=jackychou@asix.com.tw \
--cc=jannh@google.com \
--cc=kuba@kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=o.rempel@pengutronix.de \
--cc=oneukum@suse.com \
--cc=p.rosenberger@kunbus.com \
--cc=pabeni@redhat.com \
--cc=w@1wt.eu \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).