]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
rtnetlink: use dev_isalive() in rtnl_getlink()
authorEric Dumazet <edumazet@google.com>
Wed, 3 Jun 2026 18:08:31 +0000 (18:08 +0000)
committerJakub Kicinski <kuba@kernel.org>
Fri, 5 Jun 2026 01:16:14 +0000 (18:16 -0700)
rtnl_getlink() uses an RCU lookup to get the netdevice pointer.

When/If rtnl_lock() is used, we should check if the netdevice is not
being dismantled before potentially perform illegal actions.

Move dev_isalive() out of net/core/net-sysfs.c and make it available
in net/core/dev.h.

Return -ENODEV if rtnl_getlink() finds a device which is currently
being dismantled and RTNL is requested.

Fixes: e896e5c0734b ("rtnetlink: do not acquire RTNL in rtnl_getlink() with RTEXT_FILTER_NAME_ONLY")
Signed-off-by: Eric Dumazet <edumazet@google.com>
Suggested-by: Jakub Kicinski <kuba@kernel.org>
Reviewed-by: Jiayuan Chen <jiayuan.chen@linux.dev>
Link: https://patch.msgid.link/20260603180831.1024716-1-edumazet@google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
net/core/dev.h
net/core/net-sysfs.c
net/core/rtnetlink.c

index 0cf24b8f5008a951f569ff1b51da91a0e19b347f..9e94314408695be763320aa8282340842f7303e2 100644 (file)
@@ -396,4 +396,10 @@ int dev_get_hwtstamp_phylib(struct net_device *dev,
                            struct kernel_hwtstamp_config *cfg);
 int net_hwtstamp_validate(const struct kernel_hwtstamp_config *cfg);
 
+/* Caller holds RTNL, netdev->lock or RCU */
+static inline bool dev_isalive(const struct net_device *dev)
+{
+       return READ_ONCE(dev->reg_state) <= NETREG_REGISTERED;
+}
+
 #endif
index 3318b5666e438fe5bab2bcfed2bb260c7b4d5a63..0e71c9ed41e81d85af33a4339f556a0c5d760243 100644 (file)
@@ -37,12 +37,6 @@ static const char fmt_uint[] = "%u\n";
 static const char fmt_ulong[] = "%lu\n";
 static const char fmt_u64[] = "%llu\n";
 
-/* Caller holds RTNL, netdev->lock or RCU */
-static inline int dev_isalive(const struct net_device *dev)
-{
-       return READ_ONCE(dev->reg_state) <= NETREG_REGISTERED;
-}
-
 /* There is a possible ABBA deadlock between rtnl_lock and kernfs_node->active,
  * when unregistering a net device and accessing associated sysfs files. The
  * potential deadlock is as follow:
index 652dd008955a90691403de9a54d8693d64ea7799..61d095ce1b3b98a69649d03833fc0dcb86437554 100644 (file)
@@ -4265,6 +4265,11 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
 retry:
        if (need_rtnl) {
                rtnl_lock();
+               if (!dev_isalive(dev)) {
+                       err = -ENODEV;
+                       nskb = NULL;
+                       goto unlock;
+               }
                /* Synchronize the carrier state so we don't report a state
                 * that we're not actually going to honour immediately; if
                 * the driver just did a carrier off->on transition, we can
@@ -4282,6 +4287,7 @@ retry:
                                       nlh->nlmsg_seq, 0, 0, ext_filter_mask,
                                       0, NULL, 0, netnsid, GFP_KERNEL);
 
+unlock:
        if (need_rtnl)
                rtnl_unlock();