rtnetlink: use dev_isalive() in rtnl_getlink()
Subsystems (2)
PATCH 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 include/linux/netdevice.h
Return -ENODEV if rtnl_getlink() finds a device which is currently being dismantled and RTNL is requested.
--- include/linux/netdevice.h | 6 ++++++ net/core/net-sysfs.c | 6 ------ net/core/rtnetlink.c | 5 +++++ 3 files changed, 11 insertions(+), 6 deletions(-)
include/linux/netdevice.h +6 -0
@@ -5660,6 +5660,12 @@ static inline const char *netdev_name(const struct net_device *dev)return dev->name; } +/* 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 ; +} +static inline const char *netdev_reg_state (const struct net_device *dev) {u8 reg_state =READ_ONCE (dev->reg_state);
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 +5 -0
@@ -4265,6 +4265,10 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, retry:if (need_rtnl) {rtnl_lock (); +if (!dev_isalive (dev)) { + err = -ENODEV ; + 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 +4286,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.929.g9b7fa37559-goog
Quoted 59 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 ininclude/linux/netdevice.hReturn
-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]>--- include/linux/netdevice.h | 6 ++++++ net/core/net-sysfs.c | 6 ------ net/core/rtnetlink.c | 5 +++++ 3 files changed, 11 insertions(+), 6 deletions(-)include/linux/netdevice.h +6
-0@@ -5660,6 +5660,12 @@ static inline const char *netdev_name(const struct net_device *dev)return dev->name; } +/* 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 ; +} +static inline const char *netdev_reg_state (const struct net_device *dev) {u8 reg_state =READ_ONCE (dev->reg_state);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 +0
-0
net/core/rtnetlink.c static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, · line 4270
Quoted 6 lines
net/core/rtnetlink.c +3
-0@@ -4265,6 +4265,10 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, retry:if (need_rtnl) {rtnl_lock (); +if (!dev_isalive (dev)) { + err = -ENODEV ; + goto unlock;
This does fix the macvlan UAF, thanks.
But 'nskb' is used uninitialized, so the kfree_skb(nskb) below frees an uninitialized pointer.
+unlock:
if (need_rtnl)
rtnl_unlock();
if (err < 0) {
kfree_skb(nskb); /* nskb is uninitialized when reached via goto unlock */
...
}
Quoted 12 lines
net/core/rtnetlink.c +2
-0+ }/* 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 +4286,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 ();
Quoted 76 lines · Jiayuan Chen
Quoted 59 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 ininclude/linux/netdevice.hReturn
-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]>--- include/linux/netdevice.h | 6 ++++++ net/core/net-sysfs.c | 6 ------ net/core/rtnetlink.c | 5 +++++ 3 files changed, 11 insertions(+), 6 deletions(-)include/linux/netdevice.h +6
-0@@ -5660,6 +5660,12 @@ static inline const char *netdev_name(const struct net_device *dev)return dev->name; } +/* 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 ; +} +static inline const char *netdev_reg_state (const struct net_device *dev) {u8 reg_state =READ_ONCE (dev->reg_state);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 +0
-0net/core/rtnetlink.c static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, · line 4270
Quoted 6 lines
net/core/rtnetlink.c +3
-0@@ -4265,6 +4265,10 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, retry:if (need_rtnl) {rtnl_lock (); +if (!dev_isalive (dev)) { + err = -ENODEV ; + goto unlock;This does fix the macvlan UAF, thanks.
But 'nskb' is used uninitialized, so the
kfree_skb(nskb)below frees an uninitialized pointer.+unlock:
Quoted 8 lines
if (need_rtnl)
rtnl_unlock();
if (err < 0) {
kfree_skb(nskb); /* nskb is uninitialized when reached
via goto unlock */
...
}Oh right, last minute refactoring change, I will send a V2,
Jakub Kicinski
maintainerinclude/linux/netdevice.h static inline const char *netdev_name(const struct net_device *dev) · line 5667
Quoted 13 lines · On Tue, 2 Jun 2026 09:13:19 +0000 Eric Dumazet wrote:
include/linux/netdevice.h +5
-0@@ -5660,6 +5660,12 @@ static inline const char *netdev_name(const struct net_device *dev)return dev->name; } +/* 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 ; +}
nit: could you put it in net/core/dev.h ?
Or alternatively rename to netif_isalive(). We should try to avoid using the dev_ prefix on APIs we export via include/linux/
Quoted 10 lines · Jakub Kicinski
Quoted 8 lines · On Tue, 2 Jun 2026 09:13:19 +0000 Eric Dumazet wrote:
include/linux/netdevice.h +0
-0@@ -5660,6 +5660,12 @@ static inline const char *netdev_name(const struct net_device *dev)return dev->name; }
Quoted 8 lines
include/linux/netdevice.h static inline const char *netdev_name(const struct net_device *dev) · line 5667
Quoted 5 lines
include/linux/netdevice.h +5
-0+/* 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 ; +}nit: could you put it in
net/core/dev.h?
Sure thing.
Quoted 2 lines
Or alternatively rename to
netif_isalive(). We should try to avoid using thedev_prefix on APIs we export viainclude/linux/