rtnetlink: use dev_isalive() in rtnl_getlink()
Subsystems (2)
PATCH v2 1/1 rtnetlink: use dev_isalive() in rtnl_getlink()
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.
---
v2: clear nskb (Jiayuan Chen)
move dev_isalive() to net/core/dev.h (Jakub Kicinski)
net/core/dev.h | 6 ++++++ net/core/net-sysfs.c | 6 ------ net/core/rtnetlink.c | 6 ++++++ 3 files changed, 12 insertions(+), 6 deletions(-)
net/core/dev.h +6 -0
@@ -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
net/core/net-sysfs.c +0 -6
@@ -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:
net/core/rtnetlink.c +6 -0
@@ -4267,6 +4267,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
@@ -4284,6 +4289,7 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, nlh->nlmsg_seq,0 ,0 , ext_filter_mask, 0, NULL, 0, netnsid, GFP_KERNEL); +unlock:if (need_rtnl)rtnl_unlock ();
Signature · 1 line
2.54.0.1032.g2f8565e1d1-goog
Quoted 8 lines · Eric Dumazet
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 ofnet/core/net-sysfs.cand make it available innet/core/dev.h.
Quoted 7 lines
Return
-ENODEVifrtnl_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 <[email protected]>Suggested-by Jakub Kicinski <[email protected]>Cc Jiayuan Chen <[email protected]>
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 9 lines · On Wed, 3 Jun 2026 18:08:31 +0000 you wrote:
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 ofnet/core/net-sysfs.cand make it available innet/core/dev.h.[...]
Here is the summary with links:
- [v2,net-next] rtnetlink: use dev_isalive() in rtnl_getlink()
https://git.kernel.org/netdev/net-next/c/f3c496105472
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html