ipv4: fib_rule: Move fib4_rules_exit() to ->exit().
Subsystems (3)
PATCH 1/1 ipv4: fib_rule: Move fib4_rules_exit() to ->exit().
syzbot reported use-after-free of net->ipv4.rules_ops. [0]
It can be reproduced with these commands:
while true; do
ip netns add ns1
ip -n ns1 link set dev lo up
ip -n ns1 address add 192.0.2.1/24 dev lo
ip -n ns1 link add name dummy1 up type dummy
ip -n ns1 address add 198.51.100.1/24 dev dummy1
ip -n ns1 rule add ipproto tcp sport 12345 table 12345
ip -n ns1 fou add port 5555 ipproto 47 local 192.0.2.1 peer 198.51.100.2 peer_port 54321 ip netns del ns1
done
The cited commit moved fib4_rules_exit() earlier to ->exit_rtnl(), but the kernel socket destroyed in ->exit() could eventually reach __fib_lookup().
I left fib4_rules_exit() in ->exit_rtnl() because fib4_rule_delete() calls fib_unmerge(), which requires RTNL.
However, when ->delete() is called, ->configure() has already been called, thus fib_unmerge() in ->delete() has no effect.
Let's remove fib_unmerge() in fib4_rule_delete() and move fib4_rules_exit() to ->exit().
Many thanks to Ido Schimmel for providing the nice repro very quickly.
Note that we can make fib_rules_ops.delete() return void once net-next opens.
[0]:
BUG: KASAN: slab-use-after-free in fib_rules_lookup+0x15e/0xeb0 net/core/fib_rules.c:321 Read of size 8 at addr ffff88804ec4c680 by task kworker/u8:21/12641 CPU: 0 UID: 0 PID: 12641 Comm: kworker/u8:21 Not tainted syzkaller #0 PREEMPT(full) Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 05/09/2026 Workqueue: netns cleanup_net Call Trace: <TASK> dump_stack_lvl+0xe8/0x150 lib/dump_stack.c:120 print_address_description+0x55/0x1e0 mm/kasan/report.c:378 print_report+0x58/0x70 mm/kasan/report.c:482 kasan_report+0x117/0x150 mm/kasan/report.c:595 fib_rules_lookup+0x15e/0xeb0 net/core/fib_rules.c:321 __fib_lookup+0x106/0x210 net/ipv4/fib_rules.c:96 ip_route_output_key_hash_rcu+0x294/0x2720 net/ipv4/route.c:2811 ip_route_output_key_hash+0x18d/0x2a0 net/ipv4/route.c:2702 __ip_route_output_key include/net/route.h:169 [inline] ip_route_output_flow+0x2a/0x150 net/ipv4/route.c:2929 ip4_datagram_release_cb+0x89d/0xbe0 net/ipv4/datagram.c:118 release_sock+0x206/0x260 net/core/sock.c:3861 inet_shutdown+0x2b1/0x390 net/ipv4/af_inet.c:950 udp_tunnel_sock_release+0x6d/0x80 net/ipv4/udp_tunnel_core.c:197 fou_release net/ipv4/fou_core.c:562 [inline] fou_exit_net+0x17d/0x1f0 net/ipv4/fou_core.c:1230 ops_exit_list net/core/net_namespace.c:199 [inline] ops_undo_list+0x43d/0x8d0 net/core/net_namespace.c:252 cleanup_net+0x572/0x810 net/core/net_namespace.c:702 process_one_work kernel/workqueue.c:3314 [inline] process_scheduled_works+0xa8e/0x14e0 kernel/workqueue.c:3397 worker_thread+0xa47/0xfb0 kernel/workqueue.c:3478 kthread+0x389/0x470 kernel/kthread.c:436 ret_from_fork+0x514/0xb70 arch/x86/kernel/process.c:158 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245 </TASK>
--- net/ipv4/fib_frontend.c | 10 ++++++---- net/ipv4/fib_rules.c | 11 ++--------- 2 files changed, 8 insertions(+), 13 deletions(-)
net/ipv4/fib_frontend.c +6 -4
@@ -1612,10 +1612,6 @@ static void ip_fib_net_exit(struct net *net)fib_free_table (tb); } } - -#ifdef CONFIG_IP_MULTIPLE_TABLES -fib4_rules_exit (net); -#endif }static int __net_init fib_net_init (struct net *net)
@@ -1652,6 +1648,9 @@ static int __net_init fib_net_init(struct net *net)ip_fib_net_exit (net);rtnl_net_unlock (net); +#ifdef CONFIG_IP_MULTIPLE_TABLES +fib4_rules_exit (net); +#endif kfree (net->ipv4.fib_table_hash);fib4_notifier_exit (net); goto out;
@@ -1671,6 +1670,9 @@ static void __net_exit fib_net_exit_rtnl(struct net *net, static void __net_exit fib_net_exit(struct net *net) { +#ifdef CONFIG_IP_MULTIPLE_TABLES +fib4_rules_exit (net); +#endif kfree (net->ipv4.fib_table_hash);fib4_notifier_exit (net);fib4_semantics_exit (net);
net/ipv4/fib_rules.c +2 -9
@@ -352,24 +352,17 @@ static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,static int fib4_rule_delete (struct fib_rule *rule) {struct net *net = rule->fr_net; -int err; - -/* split local/main if they are not already split */ - err =fib_unmerge (net); -if (err) - goto errout;#ifdef CONFIG_IP_ROUTE_CLASSID if (((struct fib4_rule *)rule)->tclassid)atomic_dec (&net->ipv4.fib_num_tclassid_users);#endif - net->ipv4.fib_has_custom_rules = true;if (net->ipv4.fib_rules_require_fldissect &&fib_rule_requires_fldissect (rule)) net->ipv4.fib_rules_require_fldissect--; -errout: -return err; + +return 0 ; } static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
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 15 lines · On Tue, 16 Jun 2026 19:13:48 +0000 you wrote:
syzbot reported use-after-free of
net->ipv4.rules_ops. [0]It can be reproduced with these commands:
while true; do
ip netns add ns1
ip -n ns1 link set dev lo up
ip -n ns1 address add 192.0.2.1/24 dev lo
ip -n ns1 link add name dummy1 up type dummy
ip -n ns1 address add 198.51.100.1/24 dev dummy1
ip -n ns1 rule add ipproto tcp sport 12345 table 12345
ip -n ns1 fou add port 5555 ipproto 47 local 192.0.2.1 peer 198.51.100.2peer_port54321 ip netns del ns1
done
Quoted 1 line
[...]
Here is the summary with links:
- [v1,net-next] ipv4: fib_rule: Move fib4_rules_exit() to ->exit().
https://git.kernel.org/netdev/net-next/c/d954a67a7dfa
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
Quoted 23 lines · On Tue, Jun 16, 2026 at 3:50 PM <[email protected]> wrote:
Hello:
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 16 lines · On Tue, 16 Jun 2026 19:13:48 +0000 you wrote:
syzbot reported use-after-free of
net->ipv4.rules_ops. [0]It can be reproduced with these commands:
while true; do
ip netns add ns1
ip -n ns1 link set dev lo up
ip -n ns1 address add 192.0.2.1/24 dev lo
ip -n ns1 link add name dummy1 up type dummy
ip -n ns1 address add 198.51.100.1/24 dev dummy1
ip -n ns1 rule add ipproto tcp sport 12345 table 12345
ip -n ns1 fou add port 5555 ipproto 47 local 192.0.2.1 peer 198.51.100.2peer_port54321 ip netns del ns1
done[...]
Note that even with both patches:
fib_net_ops runs its exit handler -> frees rules_ops and fib_table_hash.
The devices are still fully UP (because default_device_exit_batch() has not run yet).
An external packet (e.g., from the host side of a veth pair) arrives. In enqueue_to_backlog():
Since the device is still UP, netif_running(dev) is still true. Our newly merged lock-serialized check will pass, and the packet is queued.
The softirq processes the packet and calls __fib_lookup() or fib_get_table() on the dying netns.
It will hit a UAF on the already-freed rules_ops or fib_table_hash.
If we want to make the netns exit 100% bulletproof against this remaining window (and not revert Kuniyuki patches)
I have patches hardeniin the routing layers (RCU-safety), making rules_ops and fib6_table RCU-safe and checking for NULL), and additionally make fib_table_hash RCU-safe.
This way, even if a packet arrives during this window, the lookup will safely fail-safe (returning -ENETUNREACH) instead of crashing.
I had these patches written yesterday before realizing the issue I was looking at was generically fixed in enqueue_to_backlog()
Quoted 24 lines · Eric Dumazet
Quoted 22 lines · On Tue, Jun 16, 2026 at 3:50 PM <[email protected]> wrote:
Hello:
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 15 lines · On Tue, 16 Jun 2026 19:13:48 +0000 you wrote:
syzbot reported use-after-free of
net->ipv4.rules_ops. [0]It can be reproduced with these commands:
while true; do
ip netns add ns1
ip -n ns1 link set dev lo up
ip -n ns1 address add 192.0.2.1/24 dev lo
ip -n ns1 link add name dummy1 up type dummy
ip -n ns1 address add 198.51.100.1/24 dev dummy1
ip -n ns1 rule add ipproto tcp sport 12345 table 12345
ip -n ns1 fou add port 5555 ipproto 47 local 192.0.2.1 peer 198.51.100.2peer_port54321 ip netns del ns1
done
Quoted 8 lines
Quoted 1 line
Quoted 1 line
[...]
Note that even with both patches:
fib_net_opsruns its exit handler -> freesrules_opsandfib_table_hash.The devices are still fully UP (because
default_device_exit_batch()has not run yet).
I think this does not happen thanks to register_pernet_device() and register_pernet_subsys() ordering.
During netns dismantle, we traverse pernet_list in the reverse order, and we should hit default_device_exit_batch() first and the rest of _subsys ops.
Last year, I was writing patches to clarify this and move default_device_exit_batch() into the same RTNL section with ->exit_batch_rtnl().
https://github.com/q2ven/linux/commit/d96f837a0007c8acc4ba1dc0d2f5b7ba02dd6478
I posted a part of them as the ->exit_rtnl() series but left default_device_exit_batch() as is since I saw an issue (some _device ops->exit() must run before it ...AFAIR )
Quoted 20 lines
An external packet (e.g., from the host side of a veth pair) arrives. In
enqueue_to_backlog():Since the device is still UP,
netif_running(dev)is still true. Our newly merged lock-serialized check will pass, and the packet is queued.The softirq processes the packet and calls
__fib_lookup()orfib_get_table()on the dying netns.
It will hit a UAF on the already-freedrules_opsorfib_table_hash.If we want to make the netns exit 100% bulletproof against this remaining window (and not revert Kuniyuki patches)
I have patches hardeniin the routing layers (RCU-safety), makingrules_opsandfib6_tableRCU-safe and checking for NULL), and additionally makefib_table_hashRCU-safe.
This way, even if a packet arrives during this window, the lookup will safely fail-safe (returning-ENETUNREACH) instead of crashing.I had these patches written yesterday before realizing the issue I was looking at was generically fixed in
enqueue_to_backlog()
Quoted 30 lines · Kuniyuki Iwashima
Quoted 28 lines · Eric Dumazet
Quoted 23 lines · On Tue, Jun 16, 2026 at 3:50 PM <[email protected]> wrote:
Hello:
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 16 lines · On Tue, 16 Jun 2026 19:13:48 +0000 you wrote:
syzbot reported use-after-free of
net->ipv4.rules_ops. [0]It can be reproduced with these commands:
while true; do
ip netns add ns1
ip -n ns1 link set dev lo up
ip -n ns1 address add 192.0.2.1/24 dev lo
ip -n ns1 link add name dummy1 up type dummy
ip -n ns1 address add 198.51.100.1/24 dev dummy1
ip -n ns1 rule add ipproto tcp sport 12345 table 12345
ip -n ns1 fou add port 5555 ipproto 47 local 192.0.2.1 peer 198.51.100.2peer_port54321 ip netns del ns1
done[...]
Note that even with both patches:
Quoted 8 lines
Quoted 4 lines
fib_net_opsruns its exit handler -> freesrules_opsandfib_table_hash.The devices are still fully UP (because
default_device_exit_batch()has not run yet).I think this does not happen thanks to
register_pernet_device()andregister_pernet_subsys()ordering.
Oh right, thanks for clarifying this.
Quoted 34 lines
During netns dismantle, we traverse
pernet_listin the reverse order, and we should hitdefault_device_exit_batch()first and the rest of_subsysops.Last year, I was writing patches to clarify this and move
default_device_exit_batch()into the same RTNL section with ->exit_batch_rtnl().
https://github.com/q2ven/linux/commit/d96f837a0007c8acc4ba1dc0d2f5b7ba02dd6478I posted a part of them as the ->exit_rtnl() series but left
default_device_exit_batch()as is since I saw an issue (some_deviceops->exit()must run before it ...AFAIR )Quoted 20 lines
An external packet (e.g., from the host side of a veth pair) arrives. In
enqueue_to_backlog():Since the device is still UP,
netif_running(dev)is still true. Our newly merged lock-serialized check will pass, and the packet is queued.The softirq processes the packet and calls
__fib_lookup()orfib_get_table()on the dying netns.
It will hit a UAF on the already-freedrules_opsorfib_table_hash.If we want to make the netns exit 100% bulletproof against this remaining window (and not revert Kuniyuki patches)
I have patches hardeniin the routing layers (RCU-safety), makingrules_opsandfib6_tableRCU-safe and checking for NULL), and additionally makefib_table_hashRCU-safe.
This way, even if a packet arrives during this window, the lookup will safely fail-safe (returning-ENETUNREACH) instead of crashing.I had these patches written yesterday before realizing the issue I was looking at was generically fixed in
enqueue_to_backlog()