๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
2 messages
2 total messages Started by Qiliang Yuan Wed, 18 Nov 2026 02:28
[PATCH net-next v4 1/4] ipv4: draw rt_genid from a sequence shared by all netns
#18258
Author: Qiliang Yuan
Date: Wed, 18 Nov 2026 02:28
93 lines
11954 bytes
An IPv4 route records the rt_genid of the netns it was created in, and
rt_is_expired() compares it with the rt_genid of the netns its device
belongs to when the route is checked. Every netns starts its rt_genid
at 0 and increments it on each cache flush, so different netns often
hold the same value.

When a device moves to another netns, routes cached through it remain
usable until dst_dev_put() points them to blackhole_netdev. For a route
using a nexthop object this happens in nexthop_free_rcu(), which
free_fib_info_rcu() queues from inside an RCU callback, so the
rcu_barrier() in __dev_change_net_namespace() does not wait for it. If
the rt_genid of the new netns matches the cached one in that window,
ipv4_dst_check() accepts the route and a socket in the old netns keeps
sending through a device that now belongs to another netns.

Take every rt_genid bump from one sequence shared by all netns, which
never hands out 0, the value every netns starts with. Inserting a route
bumps rt_genid, so the routes a netns caches carry values drawn from the
sequence, which no other netns holds until it wraps after 2^32 bumps. A
moved device sends again only once it is up in its new netns, and
NETDEV_UP flushes the route cache there, so that netns no longer holds 0
either.
Concurrent bumps of one netns can leave the older of two values, so
rt_genid is no longer monotonic per netns. It is only compared for
equality, so this costs at most an extra route lookup.

A connected UDP socket in netns A caches a route through a nexthop
object on a veth. The veth is moved to netns B and brought up with one
RTM_NEWLINK, then the socket sends 200000 datagrams. B's rt_genid is
bumped 0 to 20 times before the move, 63 trials:

                                     before   after   change
  trials sending after move (count)       1       0    -100%
  datagrams sent after move (count)     661       0    -100%

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
 include/net/net_namespace.h |  5 +----
 net/ipv4/route.c            | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h
index 4a4d72080acdc..11133ba130480 100644
--- a/include/net/net_namespace.h
+++ b/include/net/net_namespace.h
@@ -558,10 +558,7 @@ static inline int rt_genid_ipv6(const struct net *net)
 }
 #endif
 
-static inline void rt_genid_bump_ipv4(struct net *net)
-{
-	atomic_inc(&net->ipv4.rt_genid);
-}
+void rt_genid_bump_ipv4(struct net *net);
 
 extern void (*__fib6_flush_trees)(struct net *net);
 static inline void rt_genid_bump_ipv6(struct net *net)
diff --git a/net/ipv4/route.c b/net/ipv4/route.c
index b9f5397336d34..294c9c278c403 100644
--- a/net/ipv4/route.c
+++ b/net/ipv4/route.c
@@ -393,6 +393,27 @@ static inline int ip_rt_proc_init(void)
 }
 #endif /* CONFIG_PROC_FS */
 
+/*
+ * Draw rt_genid bumps from one sequence shared by all netns. A route keeps
+ * the genid of the netns it was created in, and is checked against the netns
+ * its device is in now, so distinct values make that check fail once the
+ * device has moved. Every netns starts at 0, which the sequence never hands
+ * out, and inserting a route bumps the genid, so cached routes carry values
+ * drawn from the sequence.
+ */
+static atomic_t rt_genid_seq;
+
+void rt_genid_bump_ipv4(struct net *net)
+{
+	int genid;
+
+	do {
+		genid = atomic_inc_return(&rt_genid_seq);
+	} while (!genid);
+	atomic_set(&net->ipv4.rt_genid, genid);
+}
+EXPORT_SYMBOL_GPL(rt_genid_bump_ipv4);
+
 static inline bool rt_is_expired(const struct rtable *rth)
 {
 	bool res;

-- 
2.43.0
Re: [PATCH net-next v4 1/4] ipv4: draw rt_genid from a sequence shared by all netns
#20330
Author: netdev-bot+sashiko
Date: Sun, 22 Nov 2026 03:14
143 lines
13495 bytes
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 ยท High: 0 ยท Medium: 1 ยท Low: 1

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] The fix depends on one guarantee: no other netns holds a given
  rt_genid value until the global 32-bit rt_genid_seq wraps.
- [Low] The commit fixes a cross-netns isolation bug that already exists
  in the current tree but has no Fixes: tag and no Cc: stable.

--

ipv4: draw rt_genid from a sequence shared by all netns

This patch moves rt_genid_bump_ipv4() out of line and takes every IPv4
rt_genid value from one global atomic sequence shared by all netns. That
sequence never returns 0. The aim is to make sure a route cached in one
netns cannot match the rt_genid of the netns its device has moved to.

> When a device moves to another netns, routes cached through it remain
> usable until dst_dev_put() points them to blackhole_netdev. For a route
> using a nexthop object this happens in nexthop_free_rcu(), which
> free_fib_info_rcu() queues from inside an RCU callback, so the
> rcu_barrier() in __dev_change_net_namespace() does not wait for it. If
> the rt_genid of the new netns matches the cached one in that window,
> ipv4_dst_check() accepts the route and a socket in the old netns keeps
> sending through a device that now belongs to another netns.

[Severity: Low]
Should this carry a Fixes: tag and a Cc: stable?

The premise holds in the current tree. For a fib_info that uses a nexthop
object, free_fib_info_rcu() in net/ipv4/fib_semantics.c only drops the
nexthop reference:

    if (fi->nh) {
        nexthop_put(fi->nh);
    } else {

The cached rtables are released later, through this chain:

    nexthop_free_rcu()->nexthop_free_single()->fib_nh_release()

That runs from an RCU callback queued inside another RCU callback, so the
rcu_barrier() in __dev_change_net_namespace() does not wait for it.

The old rt_genid_bump_ipv4() was a plain atomic_inc() from 0 in every
netns, so this cross-netns leak is visible to users in released kernels.
It most likely dates from when nexthop object support was added to
fib_info.

Without a Fixes: tag, how can backporters tell which stable trees need
this?

[ ... ]

> diff --git a/net/ipv4/route.c b/net/ipv4/route.c
> index b9f5397336d34..294c9c278c403 100644
> --- a/net/ipv4/route.c
> +++ b/net/ipv4/route.c
> @@ -393,6 +393,27 @@ static inline int ip_rt_proc_init(void)
>  }
>  #endif /* CONFIG_PROC_FS */
>  
> +/*
> + * Draw rt_genid bumps from one sequence shared by all netns. A route keeps
> + * the genid of the netns it was created in, and is checked against the netns
> + * its device is in now, so distinct values make that check fail once the
> + * device has moved. Every netns starts at 0, which the sequence never hands
> + * out, and inserting a route bumps the genid, so cached routes carry values
> + * drawn from the sequence.
> + */
> +static atomic_t rt_genid_seq;
> +
> +void rt_genid_bump_ipv4(struct net *net)
> +{
> +	int genid;
> +
> +	do {
> +		genid = atomic_inc_return(&rt_genid_seq);
> +	} while (!genid);
> +	atomic_set(&net->ipv4.rt_genid, genid);
> +}

[Severity: Medium]
Can an unprivileged user force rt_genid_seq to wrap and bring back the
collision this patch is meant to close?

The fix assumes no other netns holds a given value until the 32-bit
sequence wraps. However, sysctl_route_net_init() deliberately keeps the
per-netns route flush sysctl for netns owned by non-init user namespaces:

    /* Don't export non-whitelisted sysctls to unprivileged users */
    if (net->user_ns != &init_user_ns) {
        if (tbl[0].procname != ipv4_route_flush_procname)
            table_size = 0;
    }

Each write to that sysctl advances the global counter, with no rate limit:

    ipv4_sysctl_rtcache_flush()->rt_cache_flush()->rt_genid_bump_ipv4()

Route add/delete loops and xfrm_policy_insert() also advance it. The
period is 2^32-1 nonzero values, so a few threads could cycle it in
anywhere from minutes to about an hour.

After a wrap, the NETDEV_UP rt_cache_flush() in the device's new netns B
can hand out the same value that a route cached in the old netns A
carries. rt_is_expired() only tests for equality:

    res = rth->rt_genid != rt_genid_ipv4(dev_net_rcu(rth->dst.dev));

so ipv4_dst_check() would accept the stale route. A socket in A would
then send through a device that belongs to B until dst_dev_put() runs.

Lining up the exact count between the two draws without being able to
read the counter is uncertain. Still, the commit message's "no other
netns holds until it wraps after 2^32 bumps" seems to treat the wrap as
unreachable.

This matters more later in the series. "net: remove rcu_barrier() from
__dev_change_net_namespace()" removes the barrier, so rt_genid uniqueness
becomes the only guard for all IPv4 cached routes, not just nexthop
object routes, until the deferred dst_dev_put() runs.

Would comparing netns identity, or using a wider sequence, avoid relying
on a value that unprivileged users can cycle?

> +EXPORT_SYMBOL_GPL(rt_genid_bump_ipv4);
> +
>  static inline bool rt_is_expired(const struct rtable *rth)
>  {
>  	bool res;

-- 
Sashiko AI review ยท https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261117-bug-net-netns-move-rcu-barrier-v4-0-43e1ca0aac7f%40linux.dev
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