From: Paolo Abeni <pabeni@redhat.com>
To: Lukas Wunner <lukas@wunner.de>, Oliver Neukum <oneukum@suse.com>,
"David S. Miller" <davem@davemloft.net>,
Jakub Kicinski <kuba@kernel.org>, Jann Horn <jannh@google.com>,
Oleksij Rempel <o.rempel@pengutronix.de>,
Eric Dumazet <edumazet@google.com>
Cc: netdev@vger.kernel.org, linux-usb@vger.kernel.org,
Andrew Lunn <andrew@lunn.ch>, 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>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: Re: [PATCH] net: linkwatch: ignore events for unregistered netdevs
Date: Thu, 21 Apr 2022 10:02:43 +0200 [thread overview]
Message-ID: <9325d344e8a6b1a4720022697792a84e545fef62.camel@redhat.com> (raw)
In-Reply-To: <18b3541e5372bc9b9fc733d422f4e698c089077c.1650177997.git.lukas@wunner.de>
On Sun, 2022-04-17 at 09:04 +0200, Lukas Wunner wrote:
> 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,
> but the gist is that USB Ethernet drivers call linkwatch_fire_event()
> between unregister_netdev() and free_netdev(). The asynchronous work
> linkwatch_event() may thus access the netdev after it's been freed.
>
> USB Ethernet may not even be the only culprit. To address the problem
> in the most general way, ignore link events once a netdev's state has
> been set to NETREG_UNREGISTERED.
>
> That happens in netdev_run_todo() immediately before the call to
> linkwatch_forget_dev(). Note that lweventlist_lock (and its implied
> memory barrier) guarantees that a linkwatch_add_event() running after
> linkwatch_forget_dev() will see the netdev's new state and bail out.
> An unregistered netdev is therefore never added to link_watch_list
> (but may have its __LINK_STATE_LINKWATCH_PENDING bit set, which should
> not matter). That obviates the need to invoke linkwatch_run_queue() in
> netdev_wait_allrefs(), so drop it.
>
> In a sense, the present commit is to *no longer* registered netdevs as
> commit b47300168e77 ("net: Do not fire linkwatch events until the device
> is registered.") is to *not yet* registered netdevs.
>
> 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: Eric Dumazet <edumazet@google.com>
> Cc: Oliver Neukum <oneukum@suse.com>
> Cc: Andrew Lunn <andrew@lunn.ch>
> ---
> include/linux/netdevice.h | 2 --
> net/core/dev.c | 17 -----------------
> net/core/link_watch.c | 10 ++--------
> 3 files changed, 2 insertions(+), 27 deletions(-)
>
> diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
> index 59e27a2b7bf0..5d950b45b59d 100644
> --- a/include/linux/netdevice.h
> +++ b/include/linux/netdevice.h
> @@ -4805,8 +4805,6 @@ extern const struct kobj_ns_type_operations net_ns_type_operations;
>
> const char *netdev_drivername(const struct net_device *dev);
>
> -void linkwatch_run_queue(void);
> -
> static inline netdev_features_t netdev_intersect_features(netdev_features_t f1,
> netdev_features_t f2)
> {
> diff --git a/net/core/dev.c b/net/core/dev.c
> index 8c6c08446556..0ee56965ff76 100644
> --- a/net/core/dev.c
> +++ b/net/core/dev.c
> @@ -10140,23 +10140,6 @@ static struct net_device *netdev_wait_allrefs_any(struct list_head *list)
> list_for_each_entry(dev, list, todo_list)
> call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
>
> - __rtnl_unlock();
> - rcu_barrier();
> - rtnl_lock();
> -
> - list_for_each_entry(dev, list, todo_list)
> - if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
> - &dev->state)) {
> - /* We must not have linkwatch events
> - * pending on unregister. If this
> - * happens, we simply run the queue
> - * unscheduled, resulting in a noop
> - * for this device.
> - */
> - linkwatch_run_queue();
> - break;
> - }
> -
> __rtnl_unlock();
>
> rebroadcast_time = jiffies;
> diff --git a/net/core/link_watch.c b/net/core/link_watch.c
> index 95098d1a49bd..9a0ea7cd68e4 100644
> --- a/net/core/link_watch.c
> +++ b/net/core/link_watch.c
> @@ -107,7 +107,8 @@ static void linkwatch_add_event(struct net_device *dev)
> unsigned long flags;
>
> spin_lock_irqsave(&lweventlist_lock, flags);
> - if (list_empty(&dev->link_watch_list)) {
> + if (list_empty(&dev->link_watch_list) &&
> + dev->reg_state < NETREG_UNREGISTERED) {
> list_add_tail(&dev->link_watch_list, &lweventlist);
> dev_hold_track(dev, &dev->linkwatch_dev_tracker, GFP_ATOMIC);
>
What about testing dev->reg_state in linkwatch_fire_event() before
setting the __LINK_STATE_LINKWATCH_PENDING bit, so that we don't leave
the device in an unexpected state?
Other than that, it looks good to me, but potentially quite risky.
Looking at the original report it looks like the issue could be
resolved with a more usb-specific change: e.g. it looks like
usbnet_defer_kevent() is not acquiring a dev reference as it should.
Have you considered that path?
Thanks,
Paolo
next prev parent reply other threads:[~2022-04-21 8:02 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-17 7:04 [PATCH] net: linkwatch: ignore events for unregistered netdevs Lukas Wunner
2022-04-21 8:02 ` Paolo Abeni [this message]
2022-04-23 16:07 ` Lukas Wunner
2022-04-23 19:35 ` Lukas Wunner
2022-04-25 14:41 ` Jakub Kicinski
2022-04-25 14:49 ` Jann Horn
2022-04-25 15:00 ` Jakub Kicinski
2022-04-25 15:13 ` Eric Dumazet
2022-04-25 15:18 ` Jann Horn
2022-04-25 15:23 ` Eric Dumazet
2022-04-25 17:20 ` Lukas Wunner
2022-04-25 17:24 ` Eric Dumazet
2022-04-25 15:28 ` Jakub Kicinski
2022-04-25 15:31 ` Eric Dumazet
2022-04-25 15:36 ` Jakub Kicinski
2022-04-25 21:18 ` Lukas Wunner
2022-04-25 21:39 ` Eric Dumazet
2022-04-30 10:05 ` Lukas Wunner
2022-04-30 10:09 ` Lukas Wunner
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=9325d344e8a6b1a4720022697792a84e545fef62.camel@redhat.com \
--to=pabeni@redhat.com \
--cc=LinoSanfilippo@gmx.de \
--cc=andrew@lunn.ch \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=gregkh@linuxfoundation.org \
--cc=hkallweit1@gmail.com \
--cc=jackychou@asix.com.tw \
--cc=jannh@google.com \
--cc=kuba@kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=lukas@wunner.de \
--cc=netdev@vger.kernel.org \
--cc=o.rempel@pengutronix.de \
--cc=oneukum@suse.com \
--cc=p.rosenberger@kunbus.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).