* [PATCH net-next v5 4/4] ipv6: validate dsts against the state their lookup found
[not found] <cover.1795537128.git.ralf@mandelbit.com>
@ 2026-11-24 16:27 ` Ralf Lici
2026-11-25 16:27 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Ralf Lici @ 2026-11-24 16:27 UTC (permalink / raw)
To: netdev
Cc: Ralf Lici, David Ahern, Ido Schimmel, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
Kuniyuki Iwashima, Steffen Klassert, Herbert Xu, Florian Westphal,
Xia Yan, Sabrina Dubroca, Antonio Quartulli,
Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
linux-rt-devel, linux-kernel
Validate dsts in ip6_dst_check against the stamp of the lookup that
built them instead of the cookie passed by their holder, which may have
been read after a FIB change the lookup did not see. A stamp of 0,
recorded when the generation changed during the lookup or the route was
unlinked, never validates. Holders are covered without changes,
including dst_cache users and the route and path dsts of xfrm bundles,
and invalidation keeps its per-node scope: a FIB change only invalidates
dsts of routes under the nodes it stamps.
pcpu dsts are shared by all their holders, so they are never
re-stamped, which would revalidate a stale dst for every holder. A pcpu
dst is reused only if it carries the stamp of the current lookup.
Otherwise it is replaced, so a lookup that selected a route before a FIB
change cannot return a pcpu dst that another lookup rebuilt after it.
On PREEMPT_RT, a pcpu dst that another task installed while this lookup
built its own follows the same rule.
A replaced pcpu dst is released with dst_dev_put as before, and since
pcpu dsts of nexthop-object routes are validated against the IPv6
generation, now the one read before the lookup, they are also rejected
once released. Lookups may have read the slot without taking a
reference, and an xfrm bundle holding the dst as its child drops its
reference without waiting for a grace period, so the slot's reference
is dropped only after one (__fib6_drop_pcpu_from is unchanged).
Not covered yet: a holder that sends through a released dst without
validating it again, such as a corked UDP socket, loses those packets,
now also when a FIB change in the route's subtree replaces the pcpu
dst. Exceptions keep being validated against the holder's cookie, and
so keep the late sampling, as refreshing them needs their PMTU updates
serialized with the replacement.
Suggested-by: David Ahern <dsahern@kernel.org>
Signed-off-by: Ralf Lici <ralf@mandelbit.com>
---
New in v5
- Replaces the lookup cookie API and its users from v4. (David)
net/ipv6/route.c | 78 +++++++++++++++++++++++++++++++++++++++---------
1 file changed, 64 insertions(+), 14 deletions(-)
diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index 8b98ee945c58..be9bf7a4e780 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -1501,7 +1501,7 @@ static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
pcpu_rt->rt6i_flags |= RTF_PCPU;
if (f6i->nh)
- pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev));
+ pcpu_rt->sernum = res->gen;
return pcpu_rt;
}
@@ -1511,6 +1511,33 @@ static bool rt6_is_valid(const struct rt6_info *rt6)
return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev));
}
+static bool rt6_stamp_matches(const struct rt6_info *rt,
+ const struct fib6_result *res)
+{
+ if (rt->sernum || res->f6i->nh)
+ return res->gen && rt->sernum == res->gen;
+
+ return res->cookie && rt->cookie == res->cookie;
+}
+
+static void rt6_pcpu_release_rcu(struct rcu_head *head)
+{
+ struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
+
+ dst_release(dst);
+}
+
+/* Release a pcpu dst taken out of its slot. Lookups may have read the slot
+ * without taking a reference, so drop the slot's own only after a grace
+ * period: an xfrm bundle holding the dst as its child releases its
+ * reference without waiting for one.
+ */
+static void rt6_pcpu_release(struct rt6_info *rt)
+{
+ dst_dev_put(&rt->dst);
+ call_rcu(&rt->dst.rcu_head, rt6_pcpu_release_rcu);
+}
+
/* It should be called with rcu_read_lock() acquired */
static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
{
@@ -1518,16 +1545,19 @@ static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
- if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) {
+ /* A pcpu dst is shared by all its holders, so it is never re-stamped.
+ * Reuse it only if it was built under the state this lookup found,
+ * otherwise replace it: a dst rebuilt by another lookup after a FIB
+ * change must not be returned to a lookup that selected before it.
+ */
+ if (pcpu_rt && !rt6_stamp_matches(pcpu_rt, res)) {
struct rt6_info *prev, **p;
p = this_cpu_ptr(res->nh->rt6i_pcpu);
/* Paired with READ_ONCE() in __fib6_drop_pcpu_from() */
prev = xchg(p, NULL);
- if (prev) {
- dst_dev_put(&prev->dst);
- dst_release(&prev->dst);
- }
+ if (prev)
+ rt6_pcpu_release(prev);
pcpu_rt = NULL;
}
@@ -1550,13 +1580,21 @@ static struct rt6_info *rt6_make_pcpu_route(struct net *net,
/*
* Another task on this CPU already installed a pcpu_rt.
* This can happen on PREEMPT_RT where preemption is possible.
- * Free our allocation and return the existing one.
+ * Free our allocation and return the existing one if it was
+ * built under the state this lookup found, otherwise replace
+ * it as rt6_get_pcpu_route does.
*/
WARN_ON_ONCE(!IS_ENABLED(CONFIG_PREEMPT_RT));
- dst_dev_put(&pcpu_rt->dst);
- dst_release(&pcpu_rt->dst);
- return prev;
+ if (rt6_stamp_matches(prev, res)) {
+ dst_dev_put(&pcpu_rt->dst);
+ dst_release(&pcpu_rt->dst);
+ return prev;
+ }
+
+ prev = xchg(p, pcpu_rt);
+ if (prev)
+ rt6_pcpu_release(prev);
}
if (res->f6i->fib6_destroying) {
@@ -2864,11 +2902,16 @@ INDIRECT_CALLABLE_SCOPE struct dst_entry *ip6_dst_check(struct dst_entry *dst,
struct dst_entry *dst_ret;
struct fib6_info *from;
struct rt6_info *rt;
+ bool exception;
rt = dst_rt6_info(dst);
+ /* a nexthop-object pcpu dst replaced by another lookup has been
+ * released with dst_dev_put and must fail even if its stamp is current
+ */
if (rt->sernum)
- return rt6_is_valid(rt) ? dst : NULL;
+ return (READ_ONCE(rt->dst.obsolete) == DST_OBSOLETE_FORCE_CHK &&
+ rt6_is_valid(rt)) ? dst : NULL;
rcu_read_lock();
@@ -2879,9 +2922,16 @@ INDIRECT_CALLABLE_SCOPE struct dst_entry *ip6_dst_check(struct dst_entry *dst,
from = rcu_dereference(rt->from);
- if (from && (rt->rt6i_flags & RTF_PCPU ||
- unlikely(rt->dst.rt_uncached_list)))
- dst_ret = rt6_dst_from_check(rt, from, cookie);
+ /* Exceptions are validated against the holder's cookie, every other
+ * dst against the stamp of the lookup that built it rather than the
+ * cookie its holder read afterwards, which may postdate a FIB change
+ * the lookup did not see.
+ */
+ exception = (rt->rt6i_flags & RTF_CACHE) && !rt->dst.rt_uncached_list;
+ if (from && !exception)
+ dst_ret = rt->cookie ?
+ rt6_dst_from_check(rt, from, rt->cookie) :
+ NULL;
else
dst_ret = rt6_check(rt, from, cookie);
--
2.56.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH net-next v5 4/4] ipv6: validate dsts against the state their lookup found
2026-11-24 16:27 ` [PATCH net-next v5 4/4] ipv6: validate dsts against the state their lookup found Ralf Lici
@ 2026-11-25 16:27 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-25 16:27 UTC (permalink / raw)
To: Ralf Lici; +Cc: linux-rt-devel, linux-kernel
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] ipv6: deleted nexthop-object pcpu dsts stay valid in
ip6_dst_check()
- [Medium] ipv6: PREEMPT_RT race killing or returning dead pcpu dsts in
route.c
--
commit 4fc9718ac0ef513c19b112ac560ff9d40fa15b7c
Author: Ralf Lici <ralf@mandelbit.com>
ipv6: validate dsts against the state their lookup found
This commit validates IPv6 dst entries in ip6_dst_check() and reuses or
replaces cached per-CPU dsts based on the lookup stamp recorded when the
dst was built rather than the cookie passed by the holder.
> diff --git a/net/ipv6/route.c b/net/ipv6/route.c
> index 8b98ee945c583..be9bf7a4e7808 100644
> --- a/net/ipv6/route.c
> +++ b/net/ipv6/route.c
> @@ -1501,7 +1501,7 @@ static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
> pcpu_rt->rt6i_flags |= RTF_PCPU;
>
> if (f6i->nh)
> - pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev));
> + pcpu_rt->sernum = res->gen;
>
> return pcpu_rt;
> }
> @@ -1511,6 +1511,33 @@ static bool rt6_is_valid(const struct rt6_info *rt6)
> return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev));
> }
>
> +static bool rt6_stamp_matches(const struct rt6_info *rt,
> + const struct fib6_result *res)
> +{
> + if (rt->sernum || res->f6i->nh)
> + return res->gen && rt->sernum == res->gen;
> +
> + return res->cookie && rt->cookie == res->cookie;
> +}
> +
> +static void rt6_pcpu_release_rcu(struct rcu_head *head)
> +{
> + struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
> +
> + dst_release(dst);
> +}
> +
> +/* Release a pcpu dst taken out of its slot. Lookups may have read the slot
> + * without taking a reference, so drop the slot's own only after a grace
> + * period: an xfrm bundle holding the dst as its child releases its
> + * reference without waiting for one.
> + */
> +static void rt6_pcpu_release(struct rt6_info *rt)
> +{
> + dst_dev_put(&rt->dst);
> + call_rcu(&rt->dst.rcu_head, rt6_pcpu_release_rcu);
> +}
> +
> /* It should be called with rcu_read_lock() acquired */
> static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
> {
> @@ -1518,16 +1545,19 @@ static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
>
> pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
>
> - if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) {
> + /* A pcpu dst is shared by all its holders, so it is never re-stamped.
> + * Reuse it only if it was built under the state this lookup found,
> + * otherwise replace it: a dst rebuilt by another lookup after a FIB
> + * change must not be returned to a lookup that selected before it.
> + */
> + if (pcpu_rt && !rt6_stamp_matches(pcpu_rt, res)) {
> struct rt6_info *prev, **p;
>
> p = this_cpu_ptr(res->nh->rt6i_pcpu);
> /* Paired with READ_ONCE() in __fib6_drop_pcpu_from() */
> prev = xchg(p, NULL);
> - if (prev) {
> - dst_dev_put(&prev->dst);
> - dst_release(&prev->dst);
> - }
> + if (prev)
> + rt6_pcpu_release(prev);
>
> pcpu_rt = NULL;
> }
> @@ -1550,13 +1580,21 @@ static struct rt6_info *rt6_make_pcpu_route(struct net *net,
> /*
> * Another task on this CPU already installed a pcpu_rt.
> * This can happen on PREEMPT_RT where preemption is possible.
> - * Free our allocation and return the existing one.
> + * Free our allocation and return the existing one if it was
> + * built under the state this lookup found, otherwise replace
> + * it as rt6_get_pcpu_route does.
> */
> WARN_ON_ONCE(!IS_ENABLED(CONFIG_PREEMPT_RT));
>
> - dst_dev_put(&pcpu_rt->dst);
> - dst_release(&pcpu_rt->dst);
> - return prev;
> + if (rt6_stamp_matches(prev, res)) {
> + dst_dev_put(&pcpu_rt->dst);
> + dst_release(&pcpu_rt->dst);
> + return prev;
> + }
> +
> + prev = xchg(p, pcpu_rt);
> + if (prev)
> + rt6_pcpu_release(prev);
> }
[Severity: Medium]
On CONFIG_PREEMPT_RT, can concurrent lookups on the same CPU kill a live
pcpu_rt or return one that has already been released?
local_bh_disable() in ip6_pol_route() pins the task to the CPU, but tasks
on the same CPU can still preempt one another inside rt6_get_pcpu_route()
and rt6_make_pcpu_route():
rt6_get_pcpu_route() [task A]
pcpu_rt = this_cpu_read(*p)
!rt6_stamp_matches(pcpu_rt, res)
<preempted by task B on same CPU>
rt6_get_pcpu_route()
xchg(p, NULL)
rt6_make_pcpu_route()
cmpxchg(p, NULL, new_rt)
return new_rt
<task A resumes>
prev = xchg(p, NULL)
rt6_pcpu_release(prev)
dst_dev_put(&new_rt->dst) <-- kills live new_rt held by task B
The same unconditional xchg(p, pcpu_rt) overwrite exists in
rt6_make_pcpu_route() when !rt6_stamp_matches(prev, res). Conversely, if
task A gets prev from cmpxchg(p, NULL, pcpu_rt) in rt6_make_pcpu_route()
and is preempted before returning it, task B can replace prev and call
rt6_pcpu_release(prev). Because rt6_pcpu_release() leaves prev->cookie
and prev->sernum intact and defers dst_release() via call_rcu(), task A
resumes, sees rt6_stamp_matches(prev, res), and returns the dead dst
(which still passes ip6_hold_safe() before the grace period elapses).
> @@ -2864,11 +2902,16 @@ INDIRECT_CALLABLE_SCOPE struct dst_entry *ip6_dst_check(struct dst_entry *dst,
> struct dst_entry *dst_ret;
> struct fib6_info *from;
> struct rt6_info *rt;
> + bool exception;
>
> rt = dst_rt6_info(dst);
>
> + /* a nexthop-object pcpu dst replaced by another lookup has been
> + * released with dst_dev_put and must fail even if its stamp is current
> + */
> if (rt->sernum)
> - return rt6_is_valid(rt) ? dst : NULL;
> + return (READ_ONCE(rt->dst.obsolete) == DST_OBSOLETE_FORCE_CHK &&
> + rt6_is_valid(rt)) ? dst : NULL;
[Severity: High]
Does a nexthop-object pcpu dst stay valid in ip6_dst_check() and
rt6_stamp_matches() after its route is deleted?
Deleting a route via fib6_del() -> fib6_del_route() -> fib6_purge_rt()
does not call fib6_new_sernum(), so rt_genid_ipv6() is unchanged.
Instead, __fib6_drop_pcpu_from() (and the fib6_destroying path in
rt6_make_pcpu_route()) invalidates the cached per-CPU dst only by
clearing pcpu_rt->from to NULL, leaving pcpu_rt in res->nh->rt6i_pcpu
with dst.obsolete == DST_OBSOLETE_FORCE_CHK and its non-zero sernum:
net/ipv6/ip6_fib.c:__fib6_drop_pcpu_from() {
...
if (pcpu_rt && rcu_access_pointer(pcpu_rt->from) == match) {
struct fib6_info *from;
from = unrcu_pointer(xchg(&pcpu_rt->from, NULL));
fib6_info_release(from);
}
...
}
Because ip6_dst_check() returns early when rt->sernum is non-zero before
checking rcu_dereference(rt->from), holders keep validating and sending
through the deleted route's dst. In addition, because rt6_stamp_matches()
checks rt->sernum == res->gen without checking rcu_access_pointer(rt->from),
later lookups on the same CPU matching another route that shares the
nexthop object reuse the pcpu_rt whose from is NULL, causing
inet6_rtm_getroute() to fail with -ENETUNREACH and PMTU updates in
__ip6_rt_update_pmtu() to silently abort.
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795537128.git.ralf@mandelbit.com?part=4
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-25 16:27 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <cover.1795537128.git.ralf@mandelbit.com>
2026-11-24 16:27 ` [PATCH net-next v5 4/4] ipv6: validate dsts against the state their lookup found Ralf Lici
2026-11-25 16:27 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox