🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
5 messages
5 total messages Started by Ralf Lici Fri, 27 Nov 2026 16:48
[PATCH ipsec v2 0/2] xfrm: advance the route generation when policies are removed
#23658
Author: Ralf Lici
Date: Fri, 27 Nov 2026 16:48
40 lines
10304 bytes
Hi,

Deleting, flushing or expiring an xfrm policy does not advance the IPv4
and IPv6 route generations, so a socket that cached a plain route while
an allow policy matched its flow keeps sending in plaintext after that
policy is removed, although a policy requiring IPsec now matches.

This was triggered at runtime in a VM test setup (not observed in
production): a connected UDP socket keeps sending unencrypted datagrams
after the allow policy that exempted its flow is deleted, deleted by
index or expires, and keeps sending after a flush with the output
default set to block. Patch 2 adds that reproducer as a selftest, which
fails in all these cases without patch 1, for both IPv4 and IPv6.

Thanks,

Ralf Lici
Mandelbit Srl

---
Changes since v1 https://lore.kernel.org/netdev/20261126111516.317545-1-ralf@mandelbit.com/
- Add a selftest. (Nicolas)
- Cc ipsec@lists.linux.dev.

Ralf Lici (2):
  xfrm: advance the route generation when policies are removed
  selftests: net: add a test for xfrm policy removal

 net/xfrm/xfrm_policy.c                        |  34 ++-
 tools/testing/selftests/net/Makefile          |   1 +
 tools/testing/selftests/net/config            |   1 +
 .../selftests/net/xfrm_policy_removal.sh      | 212 ++++++++++++++++++
 4 files changed, 238 insertions(+), 10 deletions(-)
 create mode 100755 tools/testing/selftests/net/xfrm_policy_removal.sh


base-commit: 1213df0bc5993e479706c5eba5830243d778f164
-- 
2.56.0
[PATCH ipsec v2 1/2] xfrm: advance the route generation when policies are removed
#23659
Author: Ralf Lici
Date: Fri, 27 Nov 2026 16:48
118 lines
13123 bytes
Only xfrm_policy_insert and xfrm_set_default advance the IPv4 and IPv6
route generations. Deleting, flushing or expiring a policy does not, so
a route that a socket cached while an allow policy without templates
matched its flow keeps validating after that policy is removed, and the
socket keeps sending in plaintext although a policy requiring IPsec now
matches. xfrm_policy_insert also advances the generation before
unlinking the policy that the new one replaces, which can still be
matched until then, since the new policy can be linked behind it.

Advance the generation once a policy is unlinked by a delete or by its
expiry, once after all policies are unlinked by a flush, and after the
replaced policy is unlinked by an insert. Socket policies only apply to
their own socket and are left out.

Fixes: ee8372dd1989 ("xfrm: invalidate dst on policy insertion/deletion")
Signed-off-by: Ralf Lici <ralf@mandelbit.com>
---
 net/xfrm/xfrm_policy.c | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)

diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c
index d2e938d4db9a..47379da24e99 100644
--- a/net/xfrm/xfrm_policy.c
+++ b/net/xfrm/xfrm_policy.c
@@ -1569,6 +1569,14 @@ static struct xfrm_policy *xfrm_policy_insert_list(struct hlist_head *chain,
 	return delpol;
 }
 
+static void xfrm_policy_bump_rt_genid(struct net *net, unsigned short family)
+{
+	if (family == AF_INET)
+		rt_genid_bump_ipv4(net);
+	else
+		rt_genid_bump_ipv6(net);
+}
+
 int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
 {
 	struct net *net = xp_net(policy);
@@ -1589,16 +1597,13 @@ int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
 
 	__xfrm_policy_link(policy, dir);
 
-	/* After previous checking, family can either be AF_INET or AF_INET6 */
-	if (policy->family == AF_INET)
-		rt_genid_bump_ipv4(net);
-	else
-		rt_genid_bump_ipv6(net);
-
 	if (delpol) {
 		xfrm_policy_requeue(delpol, policy);
 		__xfrm_policy_unlink(delpol, dir);
 	}
+
+	/* Advance the route generation only once the replaced policy is gone */
+	xfrm_policy_bump_rt_genid(net, policy->family);
 	policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
 	hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
 	policy->curlft.add_time = ktime_get_real_seconds();
@@ -1695,6 +1700,7 @@ xfrm_policy_bysel_ctx(struct net *net, const struct xfrm_mark *mark, u32 if_id,
 				return pol;
 			}
 			__xfrm_policy_unlink(pol, dir);
+			xfrm_policy_bump_rt_genid(net, pol->family);
 		}
 		ret = pol;
 	}
@@ -1735,6 +1741,7 @@ xfrm_policy_byid(struct net *net, const struct xfrm_mark *mark, u32 if_id,
 					return pol;
 				}
 				__xfrm_policy_unlink(pol, dir);
+				xfrm_policy_bump_rt_genid(net, pol->family);
 			}
 			ret = pol;
 			break;
@@ -1835,10 +1842,12 @@ int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
 		spin_lock_bh(&net->xfrm.xfrm_policy_lock);
 		goto again;
 	}
-	if (cnt)
+	if (cnt) {
+		rt_genid_bump_all(net);
 		__xfrm_policy_inexact_flush(net);
-	else
+	} else {
 		err = -ESRCH;
+	}
 out:
 	spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
 	return err;
@@ -1875,10 +1884,12 @@ int xfrm_dev_policy_flush(struct net *net, struct net_device *dev,
 		spin_lock_bh(&net->xfrm.xfrm_policy_lock);
 		goto again;
 	}
-	if (cnt)
+	if (cnt) {
+		rt_genid_bump_all(net);
 		__xfrm_policy_inexact_flush(net);
-	else
+	} else {
 		err = -ESRCH;
+	}
 out:
 	spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
 	return err;
@@ -2365,6 +2376,9 @@ int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
 
 	spin_lock_bh(&net->xfrm.xfrm_policy_lock);
 	pol = __xfrm_policy_unlink(pol, dir);
+	/* Socket policies only apply to their own socket. */
+	if (pol && dir < XFRM_POLICY_MAX)
+		xfrm_policy_bump_rt_genid(net, pol->family);
 	spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
 	if (pol) {
 		xfrm_policy_kill(pol);
-- 
2.56.0
Re: [PATCH ipsec v2 1/2] xfrm: advance the route generation when policies are removed
#24547
Author: Nicolas Dichtel
Date: Mon, 30 Nov 2026 10:08
20 lines
12598 bytes
Le 27/11/2026 à 17:48, Ralf Lici a écrit :> Only xfrm_policy_insert and
xfrm_set_default advance the IPv4 and IPv6
> route generations. Deleting, flushing or expiring a policy does not, so
> a route that a socket cached while an allow policy without templates
> matched its flow keeps validating after that policy is removed, and the
> socket keeps sending in plaintext although a policy requiring IPsec now
> matches. xfrm_policy_insert also advances the generation before
> unlinking the policy that the new one replaces, which can still be
> matched until then, since the new policy can be linked behind it.
> 
> Advance the generation once a policy is unlinked by a delete or by its
> expiry, once after all policies are unlinked by a flush, and after the
> replaced policy is unlinked by an insert. Socket policies only apply to
> their own socket and are left out.
> 
> Fixes: ee8372dd1989 ("xfrm: invalidate dst on policy insertion/deletion")
> Signed-off-by: Ralf Lici <ralf@mandelbit.com>

Reviewed-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
Re: [PATCH ipsec v2 1/2] xfrm: advance the route generation when policies are removed
#24697
Author: Ralf Lici
Date: Mon, 30 Nov 2026 16:51
62 lines
12127 bytes
On Mon, 30 Nov 2026 11:08:21 +0100, Nicolas Dichtel <nicolas.dichtel@6wind.com> wrote:
> Le 27/11/2026 à 17:48, Ralf Lici a écrit :> Only xfrm_policy_insert and
> xfrm_set_default advance the IPv4 and IPv6
> > route generations. Deleting, flushing or expiring a policy does not, so
> > a route that a socket cached while an allow policy without templates
> > matched its flow keeps validating after that policy is removed, and the
> > socket keeps sending in plaintext although a policy requiring IPsec now
> > matches. xfrm_policy_insert also advances the generation before
> > unlinking the policy that the new one replaces, which can still be
> > matched until then, since the new policy can be linked behind it.
> > 
> > Advance the generation once a policy is unlinked by a delete or by its
> > expiry, once after all policies are unlinked by a flush, and after the
> > replaced policy is unlinked by an insert. Socket policies only apply to
> > their own socket and are left out.
> > 
> > Fixes: ee8372dd1989 ("xfrm: invalidate dst on policy insertion/deletion")
> > Signed-off-by: Ralf Lici <ralf@mandelbit.com>
>
> Reviewed-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
>

Thanks Nicolas.

Sashiko raised three points on this patch [1]:

- The IPv6 cookie is still read after the policy lookup, so a lookup
  that matched the allow policy before the unlink can store the new
  serial. That is the late cookie sampling race in general, which the
  writer side cannot close. "ipv6: validate cached dsts against the
  netns route generation" [2] closes it by sampling the generation
  before the lookup.

- rt_genid_bump_ipv4 is a plain atomic_inc, so the unlink is not
  ordered before it on weakly ordered architectures. I'll add
  smp_mb__before_atomic in v3.

- xfrm_policy_delete also runs from the policy timer, in softirq
  context, and for IPv6 the bump is fib6_flush_trees, a walk of every
  route. Policies that expire together would walk the FIB once each in a
  single softirq, and both policies and routes only need CAP_NET_ADMIN
  in the netns. [2] turns the IPv6 bump into a counter increment, but
  that is net-next.

For the last one I see three options for net:

a) defer the bump in xfrm_policy_delete to a per-netns work item, like
   policy_hash_work, so that expiries coalesce into one walk in process
   context;
b) keep the bump synchronous, as in this version; or
c) leave expiry out of this fix.

The first option seems the right fix to me but let me know if you prefer
a more "minimalistic" approach for a bugfix commit.

[1] https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795797791.git.ralf%40mandelbit.com
[2] https://lore.kernel.org/netdev/cover.1795642432.git.dahern@nvidia.com/

-- 
Ralf Lici
Mandelbit Srl
Re: [PATCH ipsec v2 1/2] xfrm: advance the route generation when policies are removed
#25372
Author: Nicolas Dichtel
Date: Tue, 01 Dec 2026 16:59
21 lines
12600 bytes
Le 30/11/2026 à 17:51, Ralf Lici a écrit :[snip]

> - xfrm_policy_delete also runs from the policy timer, in softirq
>   context, and for IPv6 the bump is fib6_flush_trees, a walk of every
>   route. Policies that expire together would walk the FIB once each in a
>   single softirq, and both policies and routes only need CAP_NET_ADMIN
>   in the netns. [2] turns the IPv6 bump into a counter increment, but
>   that is net-next.
> 
> For the last one I see three options for net:
> 
> a) defer the bump in xfrm_policy_delete to a per-netns work item, like
>    policy_hash_work, so that expiries coalesce into one walk in process
>    context;
> b) keep the bump synchronous, as in this version; or
> c) leave expiry out of this fix.
> 
> The first option seems the right fix to me but let me know if you prefer
> a more "minimalistic" approach for a bugfix commit.
Ok, let's try this option.
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