From: Jann Horn <jannh@google.com>
To: "David S . Miller" <davem@davemloft.net>,
Jakub Kicinski <kuba@kernel.org>,
netdev@vger.kernel.org, Eric Dumazet <edumazet@google.com>
Cc: linux-kernel@vger.kernel.org, Oliver Neukum <oneukum@suse.com>,
Jann Horn <jannh@google.com>
Subject: [PATCH net] net: dev: Detect dev_hold() after netdev_wait_allrefs()
Date: Fri, 28 Jan 2022 02:43:03 +0100 [thread overview]
Message-ID: <20220128014303.2334568-1-jannh@google.com> (raw)
I've run into a bug where dev_hold() was being called after
netdev_wait_allrefs(). But at that point, the device is already going
away, and dev_hold() can't stop that anymore.
To make such problems easier to diagnose in the future:
- For CONFIG_PCPU_DEV_REFCNT builds: Recheck in free_netdev() whether
the net refcount has been elevated. If this is detected, WARN() and
leak the object (to prevent worse consequences from a
use-after-free).
- For builds without CONFIG_PCPU_DEV_REFCNT: Set the refcount to zero.
This signals to the generic refcount infrastructure that any attempt
to increment the refcount later is a bug.
Signed-off-by: Jann Horn <jannh@google.com>
---
net/core/dev.c | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)
diff --git a/net/core/dev.c b/net/core/dev.c
index 1baab07820f6..f7916c0d226d 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -9949,8 +9949,18 @@ void netdev_run_todo(void)
netdev_wait_allrefs(dev);
+ /* Drop the netdev refcount (which should be 1 at this point)
+ * to zero. If we're using the generic refcount code, this will
+ * tell it that any dev_hold() after this point is a bug.
+ */
+#ifdef CONFIG_PCPU_DEV_REFCNT
+ this_cpu_dec(*dev->pcpu_refcnt);
+ BUG_ON(netdev_refcnt_read(dev) != 0);
+#else
+ BUG_ON(!refcount_dec_and_test(&dev->dev_refcnt));
+#endif
+
/* paranoia */
- BUG_ON(netdev_refcnt_read(dev) != 1);
BUG_ON(!list_empty(&dev->ptype_all));
BUG_ON(!list_empty(&dev->ptype_specific));
WARN_ON(rcu_access_pointer(dev->ip_ptr));
@@ -10293,6 +10303,12 @@ void free_netdev(struct net_device *dev)
free_percpu(dev->xdp_bulkq);
dev->xdp_bulkq = NULL;
+ /* Recheck in case someone called dev_hold() between
+ * netdev_wait_allrefs() and here.
+ */
+ if (WARN_ON(netdev_refcnt_read(dev) != 0))
+ return; /* leak memory, otherwise we might get UAF */
+
/* Compatibility with error handling in drivers */
if (dev->reg_state == NETREG_UNINITIALIZED) {
netdev_freemem(dev);
base-commit: 23a46422c56144939c091c76cf389aa863ce9c18
--
2.35.0.rc0.227.g00780c9af4-goog
next reply other threads:[~2022-01-28 1:43 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-28 1:43 Jann Horn [this message]
2022-01-28 2:09 ` [PATCH net] net: dev: Detect dev_hold() after netdev_wait_allrefs() Eric Dumazet
2022-01-28 2:14 ` Jann Horn
2022-01-28 2:22 ` Jakub Kicinski
2022-01-28 2:23 ` Eric Dumazet
2022-01-28 2:27 ` Jann Horn
2022-01-28 2:30 ` Eric Dumazet
2022-01-28 2:24 ` Eric Dumazet
2022-01-28 2:48 ` Jann Horn
2022-01-28 2:53 ` Eric Dumazet
2022-01-28 3:00 ` Jann Horn
2022-01-28 17:58 ` Jann Horn
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=20220128014303.2334568-1-jannh@google.com \
--to=jannh@google.com \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=oneukum@suse.com \
/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).