Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: ralf@mandelbit.com
Cc: netdev@vger.kernel.org, dsahern@kernel.org, idosch@nvidia.com,
	davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
	pabeni@redhat.com, horms@kernel.org, ncardwell@google.com,
	kuniyu@google.com, steffen.klassert@secunet.com,
	herbert@gondor.apana.org.au, fw@strlen.de,
	yanxia@cmss.chinamobile.com, sd@queasysnail.net,
	antonio@mandelbit.com, linux-kernel@vger.kernel.org
Subject: Re: [PATCH net-next v4 4/5] ipv6: add ip6_dst_lookup_flow_cookie
Date: Tue, 24 Nov 2026 10:26:54 +0000	[thread overview]
Message-ID: <179551601473.3135.7741252633833955764@kernel.org> (raw)
In-Reply-To: <65536a8e6733b8fb94614de2e104548a921a52dd.1795166508.git.ralf@mandelbit.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 2 · Medium: 0 · Low: 0

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).

- [High] ip6_dst_lookup_flow_cookie() (net/ipv6/ip6_output.c) only closes
  the lookup/cookie race when the returned dst is a plain route.
- [High] The generation check in ip6_dst_lookup_flow_cookie() is only
  sound if every writer publishes its full routing/policy change before…

--

ipv6: add ip6_dst_lookup_flow_cookie

This adds ip6_dst_lookup_flow_cookie(). Like ip6_dst_lookup_flow(), it
does the route and xfrm policy lookup, and it also returns the dst
validation cookie. The cookie is sampled between two reads of the IPv6
generation and forced to 0 if the generation changed. A comment is also
added to fib6_new_sernum() describing the writer side ordering that the
helper relies on.

> diff --git a/net/ipv6/ip6_fib.c b/net/ipv6/ip6_fib.c
> index e1d49897d6ab0..1915d31136d65 100644
> --- a/net/ipv6/ip6_fib.c
> +++ b/net/ipv6/ip6_fib.c
> @@ -123,6 +123,10 @@ static void fib6_walkers_skip_rt(struct net *net, struct fib6_info *rt)
>  	fib6_walkers_replace_leaf(net, rt, next);
>  }
>  
> +/* The successful atomic_try_cmpxchg is fully ordered: it publishes the
> + * routing change before the new generation, and the new generation before
> + * the nodes stamped with it. ip6_dst_lookup_flow_cookie relies on both.
> + */

[Severity: High]
Does xfrm_policy_insert() follow this ordering when it replaces an
existing policy (excl == 0)?

In xfrm_policy_insert(), the IPv6 generation is advanced before the
replaced policy is unlinked:

net/xfrm/xfrm_policy.c:xfrm_policy_insert() {
    ...
	__xfrm_policy_link(policy, dir);
    ...
	else
		rt_genid_bump_ipv6(net);

	if (delpol) {
		xfrm_policy_requeue(delpol, policy);
		__xfrm_policy_unlink(delpol, dir);
	}
    ...
}

Nothing advances the generation after the unlink. xfrm_policy_kill(delpol)
only does atomic_inc(&policy->genid), and that only affects bundles.

Suppose the new policy has a larger priority value than the old one, and
a later entry in the chain has priority <= the new priority. Then
xfrm_policy_insert_list() links the new policy behind that entry, which
puts it after the old policy. During this window, xfrm_policy_lookup_bytype()
takes the first match, so it keeps selecting the old policy.

Can this sequence happen?

CPU2 xfrm_policy_insert()        CPU1 ip6_dst_lookup_flow_cookie()
  __xfrm_policy_link(P_new)
  rt_genid_bump_ipv6()  /* G+1 */
                                   gen0 = G+1
                                   xfrm_lookup() matches P_old
                                   (allow, no templates), returns
                                   dst_orig untransformed
                                   *cookie = rt6_get_cookie(rt) = G+1
                                   gen1 = G+1, cookie is kept
  __xfrm_policy_unlink(P_old)

The socket's cached plain route would then keep passing ip6_dst_check().
Traffic would go out in plaintext even though P_new requires IPsec or
blocks the flow. This lasts until an unrelated IPv6 generation change.

The following patch, "ipv6: use lookup cookies in selected socket dst
cache paths", relies on lookup cookies to prevent this kind of plaintext
bypass. No later patch in the series changes xfrm_policy_insert().

> diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c
> index 3cc911f6618e7..083ffdc8f4046 100644
> --- a/net/ipv6/ip6_output.c
> +++ b/net/ipv6/ip6_output.c

[ ... ]

> @@ -1300,6 +1299,60 @@ struct dst_entry *ip6_dst_lookup_flow(struct net *net, const struct sock *sk, st
>  }
>  EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow);
>  

[ ... ]

> +struct dst_entry *ip6_dst_lookup_flow_cookie(struct net *net,
> +					     const struct sock *sk,
> +					     struct flowi6 *fl6,
> +					     const struct in6_addr *final_dst,
> +					     u32 *cookie)
> +{
> +	const struct rt6_info *rt;
> +	struct dst_entry *dst;
> +	int gen0, gen1;
> +
> +	gen0 = rt_genid_ipv6(net);
> +	/* read the generation before looking at the FIB or xfrm policies,
> +	 * pairs with the full barrier in fib6_new_sernum
> +	 */
> +	smp_rmb();
> +
> +	dst = ip6_dst_lookup_flow(net, sk, fl6, final_dst);
> +	if (IS_ERR(dst))
> +		return dst;
> +
> +	rt = dst_rt6_info(dst);
> +	*cookie = rt->sernum ? gen0 : rt6_get_cookie(rt);
> +
> +	/* keep the FIB and cookie reads before the final generation read,
> +	 * pairs with the full barrier in fib6_new_sernum
> +	 */
> +	smp_rmb();
> +	gen1 = rt_genid_ipv6(net);
> +	if (unlikely(gen0 != gen1))
> +		*cookie = 0;

[Severity: High]
If ip6_dst_lookup_flow() returns an xfrm bundle, does zeroing the cookie
here change anything?

xfrm_dst_check() never reads the cookie:

net/xfrm/xfrm_policy.c:xfrm_dst_check() {
    ...
	if (READ_ONCE(dst->obsolete) < 0 && !stale_bundle(dst))
		return dst;
    ...
}

xfrm_bundle_ok() checks only dst_check(path, path_cookie) and
dst_check(xdst->route, route_cookie), plus the policy and state genids.

Those two cookies are sampled with rt6_get_cookie() after the route and
policy lookups, which is inside the gen0..gen1 window. That is the same
late sampling that the commit message describes as the bug:

net/ipv6/xfrm6_policy.c:xfrm6_fill_dst() {
    ...
	xdst->route_cookie = rt6_get_cookie(rt);
    ...
}

net/xfrm/xfrm_policy.c:xfrm_init_path() {
    ...
		path->path_cookie = rt6_get_cookie(dst_rt6_info(dst));
    ...
}

Could this interleaving occur?

1. A reader (tcp_v6_connect(), ip6_datagram_dst_update() or
   ip6_sk_dst_lookup_flow() after the following patch) reads gen0 = G,
   looks up route R, and xfrm_lookup() selects the existing policy P_old.

2. A higher precedence policy P_new with a different selector is
   inserted, so P_old is not killed and its genid does not change.
   xfrm_policy_insert() calls rt_genid_bump_ipv6() -> fib6_flush_trees(),
   which sets fib6_sernum to G+1 and stamps every node with G+1.

3. The reader's xfrm_bundle_create() then reads route_cookie and
   path_cookie as G+1.

4. Since gen1 != gen0, *cookie is set to 0, but the bundle ignores it.
   The socket caches the bundle. Later dst_check() calls pass because
   fn_sernum is G+1 and P_old->genid has not changed.

The flow would keep using P_old until some unrelated generation change.
That means the old tunnel or SA stays in use, even if P_new is a block
policy. A concurrent fib6_add() of a more specific route could pair the
old xdst->route with a new serial in the same way.

The commit message says:

    xfrm bundles retain their existing validation through their own route
    and path cookies.

That validation depends on the same racy sampling this patch is meant to
fix. The cookie value alone cannot invalidate a bundle, so does the
gen0 != gen1 case need something more? For example, a result telling the
caller not to cache the dst, or marking the bundle obsolete.

> +
> +	return dst;
> +}
> +EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow_cookie);

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795166508.git.ralf%40mandelbit.com

  reply	other threads:[~2026-11-24 10:26 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20  9:36 [PATCH net-next v4 0/5] ipv6: tie the dst cookie to the lookup that returned the dst Ralf Lici
2026-11-20  9:36 ` [PATCH net-next v4 1/5] ipv6: stamp the default route node with a fresh serial on link failure Ralf Lici
2026-11-24 10:26   ` netdev-bot+sashiko
2026-11-20  9:36 ` [PATCH net-next v4 2/5] ipv6: advance the serial after rebalancing in fib6_ifup and fib6_ifdown Ralf Lici
2026-11-20  9:36 ` [PATCH net-next v4 3/5] ipv6: validate the cookie of nexthop-object dsts in ip6_dst_check Ralf Lici
2026-11-24 10:26   ` netdev-bot+sashiko
2026-11-20  9:36 ` [PATCH net-next v4 4/5] ipv6: add ip6_dst_lookup_flow_cookie Ralf Lici
2026-11-24 10:26   ` netdev-bot+sashiko [this message]
2026-11-20  9:36 ` [PATCH net-next v4 5/5] ipv6: use lookup cookies in selected socket dst cache paths Ralf Lici

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179551601473.3135.7741252633833955764@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=antonio@mandelbit.com \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@kernel.org \
    --cc=fw@strlen.de \
    --cc=herbert@gondor.apana.org.au \
    --cc=horms@kernel.org \
    --cc=idosch@nvidia.com \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ncardwell@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=ralf@mandelbit.com \
    --cc=sd@queasysnail.net \
    --cc=steffen.klassert@secunet.com \
    --cc=yanxia@cmss.chinamobile.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox