nexthop: fix null-deref in nexthop_flush_dev on UNREGISTER rebroadcast
Subsystems (4)
PATCH 1/1 nexthop: fix null-deref in nexthop_flush_dev on UNREGISTER rebroadcast
A device unregistered outside its netns teardown can be stuck in netdev_wait_allrefs_any() while the pernet exit handlers of its netns run to completion. nexthop_net_exit() frees devhash and sets it to NULL. The rebroadcasts then hit nexthop_flush_dev(), which dereferences the NULL pointer, e.g. on mainline v7.4-rc1 (KASAN build):
Oops: general protection fault, probably for non-canonical
address 0xdffffc0000000004: 0000 [#1] SMP KASAN NOPTI
KASAN: null-ptr-deref in range [0x0000000000000020-0x27]
RIP: 0010:nexthop_flush_dev+0xb8/0x240
Call Trace:
nh_netdev_event+0x250/0x330
netdev_run_todo+0xaa2/0xef0
rtnl_dellink+0x35f/0xa90The faulting task holds RTNL, so the network stack is left wedged.
Skip the rebroadcasts, which are the only NETDEV_UNREGISTER events delivered with reg_state == NETREG_UNREGISTERED. This mirrors commit fd0909e963ed8 ("netdev-genl: ignore NETDEV_UNREGISTER rebroadcasts").
This issue was discovered by Tencent CodeBuddy Security.
--- net/ipv4/nexthop.c | 9 +++++++++ 1 file changed, 9 insertions(+)
net/ipv4/nexthop.c +9 -0
@@ -3996,7 +3996,16 @@ static int nh_netdev_event(struct notifier_block *this,switch (event) {case NETDEV_DOWN : +nexthop_flush_dev (dev, event); +break ;case NETDEV_UNREGISTER : +/* netdev_wait_allrefs_any() rebroadcasts NETDEV_UNREGISTER +* while waiting for the last references to be dropped; the +* netns and its nexthop pernet state may already be torn +* down by then. +*/ +if (READ_ONCE (dev->reg_state) ==NETREG_UNREGISTERED ) +break ;nexthop_flush_dev (dev, event);break ;case NETDEV_CHANGE :
Signature · 1 line
2.43.7
Eric Dumazet
maintainerQuoted 44 lines · Henry Martin
A device unregistered outside its netns teardown can be stuck in
netdev_wait_allrefs_any()while the pernet exit handlers of its netns run to completion.nexthop_net_exit()frees devhash and sets it to NULL. The rebroadcasts then hitnexthop_flush_dev(), which dereferences the NULL pointer, e.g. on mainline v7.4-rc1 (KASAN build):Oops: general protection fault, probably for non-canonical address 0xdffffc0000000004: 0000 [#1] SMP KASAN NOPTI KASAN: null-ptr-deref in range [0x0000000000000020-0x27] RIP: 0010:nexthop_flush_dev+0xb8/0x240 Call Trace: nh_netdev_event+0x250/0x330 netdev_run_todo+0xaa2/0xef0 rtnl_dellink+0x35f/0xa90The faulting task holds RTNL, so the network stack is left wedged.
Skip the rebroadcasts, which are the only
NETDEV_UNREGISTERevents delivered withreg_state==NETREG_UNREGISTERED. This mirrors commitfd0909e963ed8("netdev-genl: ignoreNETDEV_UNREGISTERrebroadcasts").This issue was discovered by Tencent CodeBuddy Security.
Fixes 8a4fc54b07d75 ("net: get rid of rtnl_lock_unregistering()")Signed-off-by Henry Martin <[email protected]>--- net/ipv4/nexthop.c | 9 +++++++++ 1 file changed, 9 insertions(+)net/ipv4/nexthop.c +2
-0@@ -3996,7 +3996,16 @@ static int nh_netdev_event(struct notifier_block *this,switch (event) {case NETDEV_DOWN : +nexthop_flush_dev (dev, event); +break ;case NETDEV_UNREGISTER :
net/ipv4/nexthop.c static int nh_netdev_event(struct notifier_block *this, · line 4011
Quoted 10 lines
net/ipv4/nexthop.c +7
-0+/* netdev_wait_allrefs_any() rebroadcasts NETDEV_UNREGISTER +* while waiting for the last references to be dropped; the +* netns and its nexthop pernet state may already be torn +* down by then. +*/ +if (READ_ONCE (dev->reg_state) ==NETREG_UNREGISTERED ) +break ;nexthop_flush_dev (dev, event);break ;case NETDEV_CHANGE :
Thanks for the report Henry, I was able to reproduce this crash.
Your patch is correct for nexthop: the first NETDEV_UNREGISTER already removed all nexthops using the device, and no new one can be attached to an unlisted device.
But I would prefer to fix this in the core instead, for these reasons:
1. This is the second instance of the same bug after netdev-genl, and it will not be the last. Any netdevice notifier using pernet state of dev_net(dev) in its NETDEV_UNREGISTER handler is exposed. Adding a reg_state check in all of them is whack-a-mole.
2. The NULL deref is only the first symptom. Once cleanup_net() completes, struct net itself is freed, while the stuck device still points to it. Following rebroadcasts then hit a use-after-free:
- netdev_wait_allrefs_any() itself: __rtnl_net_lock(dev_net(dev))
with CONFIG_DEBUG_NET_SMALL_RTNL=y.
- Any notifier reading dev_net(dev) before checking reg_state, for
instance ipmr_device_event() walking net->ipv4.mr_tables.
A reg_state check in nexthop (or netdev-genl) does not help there.
3. The root cause is my commit 8a4fc54b07d7 ("net: get rid of rtnl_lock_unregistering()"). The barrier it removed had been added by 50624c934db1 ("net: Delay default_device_exit_batch until no devices are unregistering v2") precisely to prevent this situation. I only considered the loopback dependency when removing it, and missed the notifiers one.
4. Restoring this guarantee in the core is cheap: a per-netns counter of devices sitting in netdev_run_todo(), and default_device_exit_batch() waiting for it to reach zero once all devices of the dying netns are unlisted. In the common case the counter is already zero and there is no wait.
Then all NETDEV_UNREGISTER handlers, including rebroadcasts, can
again assume that dev_net(dev) and its pernet state are alive, and nexthop needs no change.
I tested this with a KASAN kernel, using your scenario (a nexthop on a veth whose peer is deleted from another netns while a reference is held, then the netns is deleted). The crash goes away.
I will send the patch shortly, with your Reported-by: tag.
Thanks.
pw-bot: rejected
Hi Eric,
Agreed, thanks for taking it to the core. Your fix covers the whole notifier class - happy to see it land.
Thanks,
Henry
Quoted 103 lines · Eric Dumazet <[email protected]> 于2026年11月5日周四 22:20写道:
Quoted 44 lines · Henry Martin
A device unregistered outside its netns teardown can be stuck in
netdev_wait_allrefs_any()while the pernet exit handlers of its netns run to completion.nexthop_net_exit()frees devhash and sets it to NULL. The rebroadcasts then hitnexthop_flush_dev(), which dereferences the NULL pointer, e.g. on mainline v7.4-rc1 (KASAN build):Oops: general protection fault, probably for non-canonical address 0xdffffc0000000004: 0000 [#1] SMP KASAN NOPTI KASAN: null-ptr-deref in range [0x0000000000000020-0x27] RIP: 0010:nexthop_flush_dev+0xb8/0x240 Call Trace: nh_netdev_event+0x250/0x330 netdev_run_todo+0xaa2/0xef0 rtnl_dellink+0x35f/0xa90The faulting task holds RTNL, so the network stack is left wedged.
Skip the rebroadcasts, which are the only
NETDEV_UNREGISTERevents delivered withreg_state==NETREG_UNREGISTERED. This mirrors commitfd0909e963ed8("netdev-genl: ignoreNETDEV_UNREGISTERrebroadcasts").This issue was discovered by Tencent CodeBuddy Security.
Fixes 8a4fc54b07d75 ("net: get rid of rtnl_lock_unregistering()")Signed-off-by Henry Martin <[email protected]>--- net/ipv4/nexthop.c | 9 +++++++++ 1 file changed, 9 insertions(+)net/ipv4/nexthop.c +2
-0@@ -3996,7 +3996,16 @@ static int nh_netdev_event(struct notifier_block *this,switch (event) {case NETDEV_DOWN : +nexthop_flush_dev (dev, event); +break ;case NETDEV_UNREGISTER :net/ipv4/nexthop.c static int nh_netdev_event(struct notifier_block *this, · line 4011
Quoted 10 lines
net/ipv4/nexthop.c +7
-0+/* netdev_wait_allrefs_any() rebroadcasts NETDEV_UNREGISTER +* while waiting for the last references to be dropped; the +* netns and its nexthop pernet state may already be torn +* down by then. +*/ +if (READ_ONCE (dev->reg_state) ==NETREG_UNREGISTERED ) +break ;nexthop_flush_dev (dev, event);break ;case NETDEV_CHANGE :Thanks for the report Henry, I was able to reproduce this crash.
Your patch is correct for nexthop: the first
NETDEV_UNREGISTERalready removed all nexthops using the device, and no new one can be attached to an unlisted device.But I would prefer to fix this in the core instead, for these reasons:
1. This is the second instance of the same bug after netdev-genl, and it will not be the last. Any netdevice notifier using pernet state of
dev_net(dev)in itsNETDEV_UNREGISTERhandler is exposed. Adding areg_statecheck in all of them is whack-a-mole.
2. The NULL deref is only the first symptom. Oncecleanup_net()completes, struct net itself is freed, while the stuck device still points to it. Following rebroadcasts then hit a use-after-free:
-netdev_wait_allrefs_any()itself: __rtnl_net_lock(dev_net(dev))
withCONFIG_DEBUG_NET_SMALL_RTNL=y.
- Any notifier readingdev_net(dev)before checkingreg_state, for
instanceipmr_device_event()walkingnet->ipv4.mr_tables.A
reg_statecheck in nexthop (or netdev-genl) does not help there.
3. The root cause is my commit8a4fc54b07d7("net: get rid ofrtnl_lock_unregistering()"). The barrier it removed had been added by50624c934db1("net: Delaydefault_device_exit_batchuntil no devices are unregistering v2") precisely to prevent this situation. I only considered the loopback dependency when removing it, and missed the notifiers one.
4. Restoring this guarantee in the core is cheap: a per-netns counter of devices sitting innetdev_run_todo(), anddefault_device_exit_batch()waiting for it to reach zero once all devices of the dying netns are unlisted. In the common case the counter is already zero and there is no wait.Then all
NETDEV_UNREGISTERhandlers, including rebroadcasts, can
again assume thatdev_net(dev)and its pernet state are alive, and nexthop needs no change.I tested this with a KASAN kernel, using your scenario (a nexthop on a veth whose peer is deleted from another netns while a reference is held, then the netns is deleted). The crash goes away.
I will send the patch shortly, with your Reported-by: tag.
Thanks.
pw-bot: rejected