ipv6: f6i->fib6_siblings and rt->fib6_nsiblings fixes
Subsystems (3)
cover ipv6: f6i->fib6_siblings and rt->fib6_nsiblings fixes
Series based on an internal syzbot report with a repro.
After fixing (in the first patch) the original minor issue, I found that syzbot repro was able to trigger a second more serious bug in rt6_nlmsg_size().
Code review then led to the two final patches.
I have not released the syzbot bug, because other issues still need investigations.
Eric Dumazet (4):
ipv6: add a retry logic in net6_rt_notify()
ipv6: prevent infinite loop in rt6_nlmsg_size()
ipv6: fix possible infinite loop in fib6_info_uses_dev()
ipv6: annotate data-races around rt->fib6_nsiblings
net/ipv6/ip6_fib.c | 24 ++++++++++------ net/ipv6/route.c | 71 +++++++++++++++++++++++++++------------------- 2 files changed, 57 insertions(+), 38 deletions(-)
Signature · 1 line
2.50.1.470.g6ba607880d-goog
PATCH 1/4 ipv6: add a retry logic in net6_rt_notify()
inet6_rt_notify() can be called under RCU protection only. This means the route could be changed concurrently and rt6_fill_node() could return -EMSGSIZE.
Re-size the skb when this happens and retry, removing one WARN_ON() that syzbot was able to trigger:
WARNING: CPU: 3 PID: 6291 at net/ipv6/route.c:6342 inet6_rt_notify+0x475/0x4b0 net/ipv6/route.c:6342 Modules linked in: CPU: 3 UID: 0 PID: 6291 Comm: syz.0.77 Not tainted 6.16.0-rc7-syzkaller #0 PREEMPT(full) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.3-debian-1.16.3-2~bpo12+1 04/01/2014 RIP: 0010:inet6_rt_notify+0x475/0x4b0 net/ipv6/route.c:6342 Code: fc ff ff e8 6d 52 ea f7 e9 47 fc ff ff 48 8b 7c 24 08 4c 89 04 24 e8 5a 52 ea f7 4c 8b 04 24 e9 94 fd ff ff e8 9c fe 84 f7 90 <0f> 0b 90 e9 bd fd ff ff e8 6e 52 ea f7 e9 bb fb ff ff 48 89 df e8 RSP: 0018:ffffc900035cf1d8 EFLAGS: 00010293 RAX: 0000000000000000 RBX: ffffc900035cf540 RCX: ffffffff8a36e790 RDX: ffff88802f7e8000 RSI: ffffffff8a36e9d4 RDI: 0000000000000005 RBP: ffff88803c230f00 R08: 0000000000000005 R09: 00000000ffffffa6 R10: 00000000ffffffa6 R11: 0000000000000001 R12: 00000000ffffffa6 R13: 0000000000000900 R14: ffff888032ea4100 R15: 0000000000000000 FS: 00007fac7b89a6c0(0000) GS:ffff8880d6a20000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007fac7b899f98 CR3: 0000000034b3f000 CR4: 0000000000352ef0 Call Trace: <TASK> ip6_route_mpath_notify+0xde/0x280 net/ipv6/route.c:5356 ip6_route_multipath_add+0x1181/0x1bd0 net/ipv6/route.c:5536 inet6_rtm_newroute+0xe4/0x1a0 net/ipv6/route.c:5647 rtnetlink_rcv_msg+0x95e/0xe90 net/core/rtnetlink.c:6944 netlink_rcv_skb+0x155/0x420 net/netlink/af_netlink.c:2552 netlink_unicast_kernel net/netlink/af_netlink.c:1320 [inline] netlink_unicast+0x58d/0x850 net/netlink/af_netlink.c:1346 netlink_sendmsg+0x8d1/0xdd0 net/netlink/af_netlink.c:1896 sock_sendmsg_nosec net/socket.c:712 [inline] __sock_sendmsg net/socket.c:727 [inline] ____sys_sendmsg+0xa95/0xc70 net/socket.c:2566 ___sys_sendmsg+0x134/0x1d0 net/socket.c:2620
--- net/ipv6/route.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
net/ipv6/route.c +10 -5
@@ -6321,8 +6321,9 @@ static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,void inet6_rt_notify (int event,struct fib6_info *rt,struct nl_info *info,unsigned int nlm_flags) { -struct sk_buff *skb;struct net *net = info->nl_net; +struct sk_buff *skb; +size_t sz;u32 seq;int err;
@@ -6330,17 +6331,21 @@ void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info, seq = info->nlh ? info->nlh->nlmsg_seq :0 ;rcu_read_lock (); - - skb =nlmsg_new (rt6_nlmsg_size (rt),GFP_ATOMIC ); + sz =rt6_nlmsg_size (rt); +retry: + skb =nlmsg_new (sz,GFP_ATOMIC );if (!skb) goto errout; err =rt6_fill_node (net, skb, rt,NULL ,NULL ,NULL ,0 , event, info->portid, seq, nlm_flags);if (err <0 ) { -/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ -WARN_ON (err == -EMSGSIZE );kfree_skb (skb); +/* -EMSGSIZE implies needed space grew under us. */ +if (err == -EMSGSIZE ) { + sz =max (rt6_nlmsg_size (rt), sz <<1 ); + goto retry; + } goto errout; }
Signature · 1 line
2.50.1.470.g6ba607880d-goog
PATCH 2/4 ipv6: prevent infinite loop in rt6_nlmsg_size()
In reply to Eric DumazetWhile testing prior patch, I was able to trigger an infinite loop in rt6_nlmsg_size() in the following place:
list_for_each_entry_rcu(sibling, &f6i->fib6_siblings,
fib6_siblings) {
rt6_nh_nlmsg_size(sibling->fib6_nh, &nexthop_len);
}This is because fib6_del_route() and fib6_add_rt2node() uses list_del_rcu(), which can confuse rcu readers, because they might no longer see the head of the list.
Restart the loop if f6i->fib6_nsiblings is zero.
--- net/ipv6/ip6_fib.c | 4 ++-- net/ipv6/route.c | 34 ++++++++++++++++++---------------- 2 files changed, 20 insertions(+), 18 deletions(-)
net/ipv6/ip6_fib.c +2 -2
@@ -1265,7 +1265,7 @@ static int fib6_add_rt2node(struct fib6_node *fn, struct fib6_info *rt, &rt->fib6_siblings, fib6_siblings) sibling->fib6_nsiblings--; - rt->fib6_nsiblings =0 ; +WRITE_ONCE (rt->fib6_nsiblings,0 );list_del_rcu (&rt->fib6_siblings);rcu_read_lock ();rt6_multipath_rebalance (next_sibling);
@@ -2015,7 +2015,7 @@ static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,list_for_each_entry_safe (sibling, next_sibling, &rt->fib6_siblings, fib6_siblings) sibling->fib6_nsiblings--; - rt->fib6_nsiblings =0 ; +WRITE_ONCE (rt->fib6_nsiblings,0 );list_del_rcu (&rt->fib6_siblings);rt6_multipath_rebalance (next_sibling); }
net/ipv6/route.c +18 -16
@@ -5670,32 +5670,34 @@ static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)static size_t rt6_nlmsg_size (struct fib6_info *f6i) { +struct fib6_info *sibling; +struct fib6_nh *nh;int nexthop_len;if (f6i->nh) { nexthop_len =nla_total_size (4 );/* RTA_NH_ID */ nexthop_for_each_fib6_nh (f6i->nh, rt6_nh_nlmsg_size, &nexthop_len); - }else { -struct fib6_nh *nh = f6i->fib6_nh; -struct fib6_info *sibling; - - nexthop_len =0 ; -if (f6i->fib6_nsiblings) { -rt6_nh_nlmsg_size (nh, &nexthop_len); - -rcu_read_lock (); + goto common; + } -list_for_each_entry_rcu (sibling, &f6i->fib6_siblings, - fib6_siblings) { -rt6_nh_nlmsg_size (sibling->fib6_nh, &nexthop_len); - } +rcu_read_lock (); +retry: + nh = f6i->fib6_nh; + nexthop_len =0 ; +if (READ_ONCE (f6i->fib6_nsiblings)) { +rt6_nh_nlmsg_size (nh, &nexthop_len); -rcu_read_unlock (); +list_for_each_entry_rcu (sibling, &f6i->fib6_siblings, + fib6_siblings) { +rt6_nh_nlmsg_size (sibling->fib6_nh, &nexthop_len); +if (!READ_ONCE (f6i->fib6_nsiblings)) + goto retry; } - nexthop_len +=lwtunnel_get_encap_size (nh->fib_nh_lws); } - +rcu_read_unlock (); + nexthop_len +=lwtunnel_get_encap_size (nh->fib_nh_lws); +common:return NLMSG_ALIGN (sizeof (struct rtmsg )) +nla_total_size (16 )/* RTA_SRC */ +nla_total_size (16 )/* RTA_DST */
Signature · 1 line
2.50.1.470.g6ba607880d-goog
PATCH 3/4 ipv6: fix possible infinite loop in fib6_info_uses_dev()
In reply to Eric Dumazetfib6_info_uses_dev() seems to rely on RCU without an explicit protection.
Like the prior fix in rt6_nlmsg_size(),
we need to make sure fib6_del_route() or fib6_add_rt2node() have not removed the anchor from the list, or we risk an infinite loop.
--- net/ipv6/route.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)
net/ipv6/route.c +11 -6
@@ -5958,16 +5958,21 @@ static bool fib6_info_uses_dev(const struct fib6_info *f6i,if (f6i->fib6_nh->fib_nh_dev == dev)return true; -if (f6i->fib6_nsiblings) { -struct fib6_info *sibling, *next_sibling; +if (READ_ONCE (f6i->fib6_nsiblings)) { +const struct fib6_info *sibling; -list_for_each_entry_safe (sibling, next_sibling, - &f6i->fib6_siblings, fib6_siblings) { -if (sibling->fib6_nh->fib_nh_dev == dev) +rcu_read_lock (); +list_for_each_entry_rcu (sibling, &f6i->fib6_siblings, + fib6_siblings) { +if (sibling->fib6_nh->fib_nh_dev == dev) { +rcu_read_unlock ();return true; + } +if (!READ_ONCE (f6i->fib6_nsiblings)) +break ; } +rcu_read_unlock (); } -return false; }
Signature · 1 line
2.50.1.470.g6ba607880d-goog
PATCH 4/4 ipv6: annotate data-races around rt->fib6_nsiblings
In reply to Eric Dumazetrt->fib6_nsiblings can be read locklessly, add corresponding READ_ONCE() and WRITE_ONCE() annotations.
--- net/ipv6/ip6_fib.c | 20 +++++++++++++------- net/ipv6/route.c | 5 +++-- 2 files changed, 16 insertions(+), 9 deletions(-)
net/ipv6/ip6_fib.c +13 -7
@@ -445,15 +445,17 @@ struct fib6_dump_arg {static int fib6_rt_dump (struct fib6_info *rt,struct fib6_dump_arg *arg) {enum fib_event_type fib_event =FIB_EVENT_ENTRY_REPLACE ; +unsigned int nsiblings;int err;if (!rt || rt == arg->net->ipv6.fib6_null_entry)return 0 ; -if (rt->fib6_nsiblings) + nsiblings =READ_ONCE (rt->fib6_nsiblings); +if (nsiblings) err =call_fib6_multipath_entry_notifier (arg->nb, fib_event, rt, - rt->fib6_nsiblings, + nsiblings, arg->extack);else err = call_fib6_entry_notifier(arg->nb, fib_event, rt,
@@ -1138,7 +1140,7 @@ static int fib6_add_rt2node(struct fib6_node *fn, struct fib6_info *rt,if (rt6_duplicate_nexthop (iter, rt)) {if (rt->fib6_nsiblings) - rt->fib6_nsiblings =0 ; +WRITE_ONCE (rt->fib6_nsiblings,0 );if (!(iter->fib6_flags &RTF_EXPIRES ))return -EEXIST ;if (!(rt->fib6_flags &RTF_EXPIRES )) {
@@ -1167,7 +1169,8 @@ static int fib6_add_rt2node(struct fib6_node *fn, struct fib6_info *rt,*/ if (rt_can_ecmp &&rt6_qualify_for_ecmp (iter)) - rt->fib6_nsiblings++; +WRITE_ONCE (rt->fib6_nsiblings, + rt->fib6_nsiblings +1 ); }if (iter->fib6_metric > rt->fib6_metric)
@@ -1217,7 +1220,8 @@ static int fib6_add_rt2node(struct fib6_node *fn, struct fib6_info *rt, fib6_nsiblings =0 ;list_for_each_entry_safe (sibling, temp_sibling, &rt->fib6_siblings, fib6_siblings) { - sibling->fib6_nsiblings++; +WRITE_ONCE (sibling->fib6_nsiblings, + sibling->fib6_nsiblings +1 );BUG_ON (sibling->fib6_nsiblings != rt->fib6_nsiblings); fib6_nsiblings++; }
@@ -1264,7 +1268,8 @@ static int fib6_add_rt2node(struct fib6_node *fn, struct fib6_info *rt,list_for_each_entry_safe (sibling, next_sibling, &rt->fib6_siblings, fib6_siblings) - sibling->fib6_nsiblings--; +WRITE_ONCE (sibling->fib6_nsiblings, + sibling->fib6_nsiblings -1 );WRITE_ONCE (rt->fib6_nsiblings,0 );list_del_rcu (&rt->fib6_siblings);rcu_read_lock ();
@@ -2014,7 +2019,8 @@ static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn, notify_del = true;list_for_each_entry_safe (sibling, next_sibling, &rt->fib6_siblings, fib6_siblings) - sibling->fib6_nsiblings--; +WRITE_ONCE (sibling->fib6_nsiblings, + sibling->fib6_nsiblings -1 );WRITE_ONCE (rt->fib6_nsiblings,0 );list_del_rcu (&rt->fib6_siblings);rt6_multipath_rebalance (next_sibling);
net/ipv6/route.c +3 -2
@@ -5346,7 +5346,8 @@ static void ip6_route_mpath_notify(struct fib6_info *rt,*/ rcu_read_lock (); -if ((nlflags &NLM_F_APPEND ) && rt_last && rt_last->fib6_nsiblings) { +if ((nlflags &NLM_F_APPEND ) && rt_last && +READ_ONCE (rt_last->fib6_nsiblings)) { rt =list_first_or_null_rcu (&rt_last->fib6_siblings, struct fib6_info, fib6_siblings);
@@ -5856,7 +5857,7 @@ static int rt6_fill_node(struct net *net, struct sk_buff *skb,if (dst->lwtstate &&lwtunnel_fill_encap (skb, dst->lwtstate,RTA_ENCAP ,RTA_ENCAP_TYPE ) <0 ) goto nla_put_failure; - }else if (rt->fib6_nsiblings) { + }else if (READ_ONCE (rt->fib6_nsiblings)) {struct fib6_info *sibling;struct nlattr *mp;
Signature · 1 line
2.50.1.470.g6ba607880d-goog
Jakub Kicinski
maintainerQuoted 10 lines · On Fri, 25 Jul 2025 14:07:21 +0000 Eric Dumazet wrote:
Series based on an internal syzbot report with a repro.
After fixing (in the first patch) the original minor issue, I found that syzbot repro was able to trigger a second more serious bug in
rt6_nlmsg_size().Code review then led to the two final patches.
I have not released the syzbot bug, because other issues still need investigations.
We should probably spend more time on tests that modify things concurrently for the RCU conversions :$
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 9 lines · On Fri, 25 Jul 2025 14:07:21 +0000 you wrote:
Series based on an internal syzbot report with a repro.
After fixing (in the first patch) the original minor issue, I found that syzbot repro was able to trigger a second more serious bug in
rt6_nlmsg_size().Code review then led to the two final patches.
[...]
Here is the summary with links:
- [net,1/4] ipv6: add a retry logic in net6_rt_notify()
https://git.kernel.org/netdev/net/c/ea2f921db7a4 - [net,2/4] ipv6: prevent infinite loop in rt6_nlmsg_size() https://git.kernel.org/netdev/net/c/54e6fe9dd3b0 - [net,3/4] ipv6: fix possible infinite loop in fib6_info_uses_dev() https://git.kernel.org/netdev/net/c/f8d8ce1b515a - [net,4/4] ipv6: annotate data-races around rt->fib6_nsiblings https://git.kernel.org/netdev/net/c/31d7d67ba127
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html