Skip to contentSkip to conversation

xfrm: advance the route generation when policies are removed

PATCH In review Ralf Lici 2 participants 6 messages Dec 2 – Dec 3

Subsystems (4)
lore
Share & tools
lore

Ralf Lici

author
#0 lore raw

v3 cover xfrm: advance the route generation when policies are removed

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 v2 https://lore.kernel.org/netdev/[email protected]/
- Leave the IPv6 route generation bump in xfrm_policy_delete, which also runs from the policy timer in softirq context, to a work item, which coalesces the expiries into one FIB walk. (Sashiko, Nicolas)
- Order the policy unlink before the IPv4 bump. (Sashiko)
- Drop Nicolas's Reviewed-by on 1/2, since the delete path changed.
- selftest: pass nodad for IPv6 only and give the expiring policy a
longer lifetime. (Nicolas)
- selftest: fail rather than skip when a policy is rejected or the first datagram fails, and wait for the deferred invalidation after an
expiry. (Sashiko)

Changes since v1 https://lore.kernel.org/netdev/[email protected]/
- Add a selftest. (Nicolas)
- Cc [email protected].

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

 include/net/netns/xfrm.h                      |   1 +
 net/xfrm/xfrm_policy.c                        |  58 ++++-
 tools/testing/selftests/net/Makefile          |   1 +
 tools/testing/selftests/net/config            |   1 +
 .../selftests/net/xfrm_policy_removal.sh      | 236 ++++++++++++++++++
 5 files changed, 287 insertions(+), 10 deletions(-)
 create mode 100755 tools/testing/selftests/net/xfrm_policy_removal.sh

base-commit: bb65a0da99bf4861dce01149ba64e875e47ae5a3

Signature · 1 line
2.56.1
author
#1 lore raw

PATCH v3 1/2 xfrm: advance the route generation when policies are removed

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, once after all policies are unlinked by a flush, and after the replaced policy is unlinked by an insert. xfrm_policy_delete also runs from the policy timer, in softirq context, where the IPv6 bump, a walk of the FIB, would run once for each expiring policy, so leave it to a work item, which coalesces the expiries into one walk in process context. The IPv4 bump is a plain atomic_inc, so order the unlink before it. Socket policies only apply to their own socket and are left out.

Fixes ee8372dd1989 ("xfrm: invalidate dst on policy insertion/deletion")
---
 include/net/netns/xfrm.h |  1 +
 net/xfrm/xfrm_policy.c   | 58 +++++++++++++++++++++++++++++++++-------
 2 files changed, 49 insertions(+), 10 deletions(-)
include/net/netns/xfrm.h +1 -0
index b73983a17e08..56dc62c0c148 100644
@@ -55,6 +55,7 @@ struct netns_xfrm {
 	struct xfrm_policy_hash	policy_bydst[XFRM_POLICY_MAX];
 	unsigned int		policy_count[XFRM_POLICY_MAX * 2];
 	struct work_struct	policy_hash_work;
+	struct work_struct	policy_genid_ipv6_work;
 	struct xfrm_policy_hthresh policy_hthresh;
 	struct list_head	inexact_bins;
 
net/xfrm/xfrm_policy.c +48 -10

120 lines not on this page · raw message

Signature · 1 line
2.56.1
#2 lore raw

Le 02/12/2026 à 09:52, Ralf Lici a écrit :> Only xfrm_policy_insert and xfrm_set_default advance the IPv4 and IPv6

Quoted 19 lines

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, once after all policies are unlinked by a flush, and after the replaced policy is unlinked by an insert. xfrm_policy_delete also runs from the policy timer, in softirq context, where the IPv6 bump, a walk of the FIB, would run once for each expiring policy, so leave it to a work item, which coalesces the expiries into one walk in process context. The IPv4 bump is a plain atomic_inc, so order the unlink before it. Socket policies only apply to their own socket and are left out.

Fixes ee8372dd1989 ("xfrm: invalidate dst on policy insertion/deletion")

[snip]

Quoted 4 lines
net/xfrm/xfrm_policy.c +0 -0
@@ -2365,6 +2387,16 @@ int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
 
 	spin_lock_bh(&net->xfrm.xfrm_policy_lock);
 	pol = __xfrm_policy_unlink(pol, dir);

net/xfrm/xfrm_policy.c int xfrm_policy_delete(struct xfrm_policy *pol, int dir) · line 2399

Quoted 10 lines
net/xfrm/xfrm_policy.c +10 -0
+	/* The policy timer calls this in softirq context, so leave the IPv6
+	 * FIB walk to process context, once for all the policies that expire
+	 * together. Socket policies only apply to their own socket.
+	 */
+	if (pol && dir < XFRM_POLICY_MAX) {
+		if (pol->family == AF_INET)
+			xfrm_policy_bump_rt_genid(net, AF_INET);
+		else
+			schedule_work(&net->xfrm.policy_genid_ipv6_work);
+	}

With this, an IPv6 socket can still send in plaintext after the policy is removed, until the work runs. This also applies to a hard XFRM_MSG_POLEXPIRE, which goes through xfrm_policy_delete() too. The window is probably short, and the selftest tolerates it, but given the nature of the bug, I think the commit log should mention it.

Regards,
Nicolas

author
#3 lore raw
Quoted 35 lines · Nicolas Dichtel

Le 02/12/2026 à 09:52, Ralf Lici a écrit :> Only xfrm_policy_insert and xfrm_set_default advance the IPv4 and IPv6

Quoted 19 lines

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, once after all policies are unlinked by a flush, and after the replaced policy is unlinked by an insert. xfrm_policy_delete also runs from the policy timer, in softirq context, where the IPv6 bump, a walk of the FIB, would run once for each expiring policy, so leave it to a work item, which coalesces the expiries into one walk in process context. The IPv4 bump is a plain atomic_inc, so order the unlink before it. Socket policies only apply to their own socket and are left out.

Fixes ee8372dd1989 ("xfrm: invalidate dst on policy insertion/deletion")

[snip]

Quoted 11 lines
net/xfrm/xfrm_policy.c +7 -0
@@ -2365,6 +2387,16 @@ int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
 
 	spin_lock_bh(&net->xfrm.xfrm_policy_lock);
 	pol = __xfrm_policy_unlink(pol, dir);
+	/* The policy timer calls this in softirq context, so leave the IPv6
+	 * FIB walk to process context, once for all the policies that expire
+	 * together. Socket policies only apply to their own socket.
+	 */
+	if (pol && dir < XFRM_POLICY_MAX) {
+		if (pol->family == AF_INET)
+			xfrm_policy_bump_rt_genid(net, AF_INET);
Quoted 9 lines

net/xfrm/xfrm_policy.c int xfrm_policy_delete(struct xfrm_policy *pol, int dir) · line 2399

Quoted 3 lines
net/xfrm/xfrm_policy.c +3 -0
+		else
+			schedule_work(&net->xfrm.policy_genid_ipv6_work);
+	}

With this, an IPv6 socket can still send in plaintext after the policy is removed, until the work runs. This also applies to a hard XFRM_MSG_POLEXPIRE, which goes through xfrm_policy_delete() too. The window is probably short, and the selftest tolerates it, but given the nature of the bug, I think the commit log should mention it.

Right, I'll mention it in v4, together with the hard XFRM_MSG_POLEXPIRE case. Thanks for the review!

Signature · 2 lines
Ralf Lici
Mandelbit Srl
author
#4 lore raw

PATCH v3 2/2 selftests: net: add a test for xfrm policy removal

In reply to Ralf Lici

Check that a connected UDP socket that cached its route through an allow policy stops sending in plaintext once that policy is deleted, deleted by index, expires or is flushed.

---
 tools/testing/selftests/net/Makefile          |   1 +
 tools/testing/selftests/net/config            |   1 +
 .../selftests/net/xfrm_policy_removal.sh      | 236 ++++++++++++++++++
 3 files changed, 238 insertions(+)
 create mode 100755 tools/testing/selftests/net/xfrm_policy_removal.sh
tools/testing/selftests/net/Makefile +1 -0
index 9180b9bec515..bdc31252a6fc 100644
@@ -136,6 +136,7 @@ TEST_PROGS := \
 	vrf_route_leaking.sh \
 	vrf_strict_mode_test.sh \
 	xfrm_policy.sh \
+	xfrm_policy_removal.sh \
 	xfrm_state.sh \
 # end of TEST_PROGS
 
tools/testing/selftests/net/config +1 -0
index d4416f8bdcfd..d5d289089059 100644
@@ -18,6 +18,7 @@ CONFIG_DUMMY=y
 CONFIG_GENEVE=m
 CONFIG_GTP=m
 CONFIG_IFB=y
+CONFIG_INET6_ESP=y
 CONFIG_INET_DIAG=y
 CONFIG_INET_ESP=y
 CONFIG_INET_ESP_OFFLOAD=y
tools/testing/selftests/net/xfrm_policy_removal.sh new file +236 -0

236 lines not on this page · raw message

Signature · 1 line
2.56.1
Reviewed-by on v3 2/2
#5 lore raw

Re: [PATCH ipsec v3 2/2] selftests: net: add a test for xfrm policy removal

Le 02/12/2026 à 09:52, Ralf Lici a écrit :> Check that a connected UDP socket that cached its route through an allow

Quoted 4 lines

policy stops sending in plaintext once that policy is deleted, deleted by index, expires or is flushed.