Thread View: linux.kernel.netdev
3 messages
3 total messages
Started by Henry Martin
Thu, 05 Nov 2026 11:47
[PATCH] nexthop: fix null-deref in nexthop_flush_dev on UNREGISTER rebroadcast
Author: Henry Martin
Date: Thu, 05 Nov 2026 11:47
Date: Thu, 05 Nov 2026 11:47
56 lines
12317 bytes
12317 bytes
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/0xa90
The 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.
Fixes: 8a4fc54b07d75 ("net: get rid of rtnl_lock_unregistering()")
Cc: stable@vger.kernel.org
Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
---
net/ipv4/nexthop.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/net/ipv4/nexthop.c b/net/ipv4/nexthop.c
index 42e55b5a755ee..1eba7026abcae 100644
--- a/net/ipv4/nexthop.c
+++ b/net/ipv4/nexthop.c
@@ -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:
--
2.43.7
Re: [PATCH] nexthop: fix null-deref in nexthop_flush_dev on UNREGISTER rebroadcast
Author: Eric Dumazet
Date: Thu, 05 Nov 2026 14:20
Date: Thu, 05 Nov 2026 14:20
107 lines
13301 bytes
13301 bytes
Le jeu. 5 nov. 2026 à 12:47, Henry Martin <bsdhenrymartin@gmail.com> a écrit :
>
> 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/0xa90
>
> The 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.
>
> Fixes: 8a4fc54b07d75 ("net: get rid of rtnl_lock_unregistering()")
> Cc: stable@vger.kernel.org
> Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
> ---
> net/ipv4/nexthop.c | 9 +++++++++
> 1 file changed, 9 insertions(+)
>
> diff --git a/net/ipv4/nexthop.c b/net/ipv4/nexthop.c
> index 42e55b5a755ee..1eba7026abcae 100644
> --- a/net/ipv4/nexthop.c
> +++ b/net/ipv4/nexthop.c
> @@ -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:
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
Re: [PATCH] nexthop: fix null-deref in nexthop_flush_dev on UNREGISTER rebroadcast
Author: henry martin
Date: Thu, 05 Nov 2026 14:36
Date: Thu, 05 Nov 2026 14:36
120 lines
17139 bytes
17139 bytes
Hi Eric,
Agreed, thanks for taking it to the core. Your fix covers the whole
notifier class - happy to see it land.
Thanks,
Henry
Eric Dumazet <edumazet@kernel.org> 于2026年11月5日周四 22:20写道:
>
> Le jeu. 5 nov. 2026 à 12:47, Henry Martin <bsdhenrymartin@gmail.com> a écrit :
> >
> > 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/0xa90
> >
> > The 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.
> >
> > Fixes: 8a4fc54b07d75 ("net: get rid of rtnl_lock_unregistering()")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
> > ---
> > net/ipv4/nexthop.c | 9 +++++++++
> > 1 file changed, 9 insertions(+)
> >
> > diff --git a/net/ipv4/nexthop.c b/net/ipv4/nexthop.c
> > index 42e55b5a755ee..1eba7026abcae 100644
> > --- a/net/ipv4/nexthop.c
> > +++ b/net/ipv4/nexthop.c
> > @@ -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:
>
> 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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads