The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH ipsec v3 0/2] xfrm: advance the route generation when policies are removed
@ 2026-12-02  8:52 Ralf Lici
  2026-12-02  8:52 ` [PATCH ipsec v3 1/2] " Ralf Lici
  2026-12-02  8:52 ` [PATCH ipsec v3 2/2] selftests: net: add a test for xfrm policy removal Ralf Lici
  0 siblings, 2 replies; 3+ messages in thread
From: Ralf Lici @ 2026-12-02  8:52 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Simon Horman, Shuah Khan, Nicolas Dichtel,
	Sabrina Dubroca, netdev, ipsec, linux-kselftest, linux-kernel

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/cover.1795797791.git.ralf@mandelbit.com/
- 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/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

 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
-- 
2.56.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH ipsec v3 1/2] xfrm: advance the route generation when policies are removed
  2026-12-02  8:52 [PATCH ipsec v3 0/2] xfrm: advance the route generation when policies are removed Ralf Lici
@ 2026-12-02  8:52 ` Ralf Lici
  2026-12-02  8:52 ` [PATCH ipsec v3 2/2] selftests: net: add a test for xfrm policy removal Ralf Lici
  1 sibling, 0 replies; 3+ messages in thread
From: Ralf Lici @ 2026-12-02  8:52 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Simon Horman, Shuah Khan, Nicolas Dichtel,
	Sabrina Dubroca, netdev, ipsec, linux-kselftest, linux-kernel

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")
Signed-off-by: Ralf Lici <ralf@mandelbit.com>
---
 include/net/netns/xfrm.h |  1 +
 net/xfrm/xfrm_policy.c   | 58 +++++++++++++++++++++++++++++++++-------
 2 files changed, 49 insertions(+), 10 deletions(-)

diff --git a/include/net/netns/xfrm.h b/include/net/netns/xfrm.h
index b73983a17e08..56dc62c0c148 100644
--- a/include/net/netns/xfrm.h
+++ b/include/net/netns/xfrm.h
@@ -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;
 
diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c
index d2e938d4db9a..82d24a9e7425 100644
--- a/net/xfrm/xfrm_policy.c
+++ b/net/xfrm/xfrm_policy.c
@@ -1569,6 +1569,25 @@ 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) {
+		/* Order the policy change before the new generation */
+		smp_mb__before_atomic();
+		rt_genid_bump_ipv4(net);
+	} else {
+		rt_genid_bump_ipv6(net);
+	}
+}
+
+static void xfrm_policy_genid_ipv6_work(struct work_struct *work)
+{
+	struct net *net = container_of(work, struct net,
+				       xfrm.policy_genid_ipv6_work);
+
+	xfrm_policy_bump_rt_genid(net, AF_INET6);
+}
+
 int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
 {
 	struct net *net = xp_net(policy);
@@ -1589,16 +1608,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 +1711,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 +1752,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 +1853,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 +1895,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 +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);
+		else
+			schedule_work(&net->xfrm.policy_genid_ipv6_work);
+	}
 	spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
 	if (pol) {
 		xfrm_policy_kill(pol);
@@ -4296,6 +4328,8 @@ static int __net_init xfrm_policy_init(struct net *net)
 	INIT_LIST_HEAD(&net->xfrm.inexact_bins);
 	INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
 	INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
+	INIT_WORK(&net->xfrm.policy_genid_ipv6_work,
+		  xfrm_policy_genid_ipv6_work);
 	return 0;
 
 out_bydst:
@@ -4318,6 +4352,10 @@ static void __net_exit xfrm_net_pre_exit(struct net *net)
 	xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
 #endif
 	xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
+	/* Before IPv6 goes away. A policy deleted by its timer before the
+	 * flush queued the work under the policy lock.
+	 */
+	disable_work_sync(&net->xfrm.policy_genid_ipv6_work);
 }
 
 static void xfrm_policy_fini(struct net *net)
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH ipsec v3 2/2] selftests: net: add a test for xfrm policy removal
  2026-12-02  8:52 [PATCH ipsec v3 0/2] xfrm: advance the route generation when policies are removed Ralf Lici
  2026-12-02  8:52 ` [PATCH ipsec v3 1/2] " Ralf Lici
@ 2026-12-02  8:52 ` Ralf Lici
  1 sibling, 0 replies; 3+ messages in thread
From: Ralf Lici @ 2026-12-02  8:52 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Simon Horman, Shuah Khan, Nicolas Dichtel,
	Sabrina Dubroca, netdev, ipsec, linux-kselftest, linux-kernel

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.

Signed-off-by: Ralf Lici <ralf@mandelbit.com>
---
 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

diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile
index 9180b9bec515..bdc31252a6fc 100644
--- a/tools/testing/selftests/net/Makefile
+++ b/tools/testing/selftests/net/Makefile
@@ -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
 
diff --git a/tools/testing/selftests/net/config b/tools/testing/selftests/net/config
index d4416f8bdcfd..d5d289089059 100644
--- a/tools/testing/selftests/net/config
+++ b/tools/testing/selftests/net/config
@@ -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
diff --git a/tools/testing/selftests/net/xfrm_policy_removal.sh b/tools/testing/selftests/net/xfrm_policy_removal.sh
new file mode 100755
index 000000000000..bbd8e67d57ff
--- /dev/null
+++ b/tools/testing/selftests/net/xfrm_policy_removal.sh
@@ -0,0 +1,236 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# Test that removing an xfrm policy invalidates the routes that sockets
+# cached while it was in place.
+#
+# An allow policy without templates exempts UDP to one port from an ESP
+# policy with a lower priority. A connected UDP socket sends a datagram
+# through the allow policy, caching a plain route, then the allow policy is
+# deleted, deleted by index, expires or is flushed, and the socket sends
+# again. The datagrams sent after the removal must be encrypted, as counted
+# by the SA. For the flush, the output default is block instead of the ESP
+# policy, so these sends must fail.
+
+source lib.sh
+
+NS=""
+SRC=""
+DST=""
+PORT=5002
+COUNT=10
+KEY=0x0102030405060708090a0b0c0d0e0f1011121314
+
+cleanup() {
+	stop_sender
+	cleanup_all_ns
+}
+
+trap cleanup EXIT
+
+# setup creates the namespace, a dummy device with the IPv$1 sender
+# address and the state and policies for removal $2. It returns
+# $ksft_skip if the system lacks something the test needs, and fails if
+# a policy is rejected.
+setup() {
+	local family=$1
+	local mode=$2
+	local addr=(10.0.0.1/24)
+	local limit=()
+
+	SRC=10.0.0.1
+	DST=10.0.0.2
+	if [ "$family" = 6 ]; then
+		SRC=2001:db8::1
+		DST=2001:db8::2
+		addr=(2001:db8::1/64 nodad)
+	fi
+
+	setup_ns NS || return "$ksft_skip"
+	# The datagrams only need to leave the sender: a dummy device drops
+	# them and needs no neighbour resolution for the peer.
+	ip -n "$NS" link add dummy0 type dummy || return "$ksft_skip"
+	# No link-local address: adding its routes would advance the route
+	# generation and hide a missing invalidation.
+	ip -n "$NS" link set dummy0 addrgenmode none
+	ip -n "$NS" link set dummy0 up
+	ip -n "$NS" addr add "${addr[@]}" dev dummy0
+
+	# The SA that the ESP policy requires. Its packet counter tells
+	# which datagrams were encrypted.
+	ip -n "$NS" xfrm state add src "$SRC" dst "$DST" proto esp spi 0x1000 \
+		reqid 1 mode transport aead 'rfc4106(gcm(aes))' "$KEY" 128 ||
+		return "$ksft_skip"
+	if [ "$mode" = flush ]; then
+		# A flush also removes the ESP policy, so rely on the output
+		# default instead.
+		ip -n "$NS" xfrm policy setdefault in accept fwd accept \
+			out block || return "$ksft_skip"
+	else
+		ip -n "$NS" xfrm policy add src "$SRC" dst "$DST" dir out \
+			priority 100 tmpl src "$SRC" dst "$DST" proto esp \
+			reqid 1 mode transport || return 1
+	fi
+	# The allow policy, which takes precedence for the test port.
+	if [ "$mode" = expire ]; then
+		limit=(limit time-hard 3)
+	fi
+	ip -n "$NS" xfrm policy add src "$SRC" dst "$DST" proto udp \
+		dport "$PORT" dir out priority 10 action allow "${limit[@]}" ||
+		return 1
+}
+
+# sender runs in the namespace and keeps one UDP socket connected to
+# $1 port $2, so that the route it caches survives across the removal.
+# For each count it reads, it sends that many datagrams and prints how
+# many sends failed.
+# shellcheck disable=SC2329 # run in the namespace through declare -f
+sender() {
+	local fails
+	local n
+	local i
+
+	exec 3>"/dev/udp/$1/$2" || exit 1
+	while read -r n; do
+		fails=0
+		for ((i = 0; i < n; i++)); do
+			echo >&3 2>/dev/null || fails=$((fails + 1))
+		done
+		echo "$fails"
+	done
+}
+
+start_sender() {
+	coproc SENDER {
+		ip netns exec "$NS" bash -c \
+			"$(declare -f sender); sender $DST $PORT"
+	}
+}
+
+stop_sender() {
+	if [ -n "$SENDER_PID" ]; then
+		kill "$SENDER_PID" 2>/dev/null
+		wait "$SENDER_PID" 2>/dev/null
+	fi
+}
+
+send_datagrams() {
+	local fails
+
+	# Ask the sender for $1 datagrams
+	echo "$1" >&"${SENDER[1]}" || return 1
+	# and print how many of its sends failed.
+	read -r -t 5 fails <&"${SENDER[0]}" || return 1
+	echo "$fails"
+}
+
+# sa_packets prints the number of packets that the SA has encrypted.
+sa_packets() {
+	ip -n "$NS" -s xfrm state |
+		sed -nE 's/^\s*[0-9]+\(bytes\), ([0-9]+)\(packets\)$/\1/p'
+}
+
+# sent_encrypted sends one datagram and succeeds if the SA encrypted it.
+# shellcheck disable=SC2329 # called through slowwait
+sent_encrypted() {
+	local esp
+
+	esp=$(sa_packets)
+	send_datagrams 1 >/dev/null
+	[ "$(sa_packets)" -gt "$esp" ]
+}
+
+# allow_policy_gone succeeds once the allow policy is no longer installed.
+allow_policy_gone() {
+	! ip -n "$NS" xfrm policy list dir out | grep -q "dport $PORT"
+}
+
+remove_allow_policy() {
+	local mode=$1
+	local index
+
+	case $mode in
+	delete)
+		ip -n "$NS" xfrm policy delete src "$SRC" dst "$DST" \
+			proto udp dport "$PORT" dir out
+		;;
+	index)
+		index=$(ip -n "$NS" -s xfrm policy list dir out |
+			grep -A1 "dport $PORT" |
+			sed -nE 's/.* index ([0-9]+) .*/\1/p')
+		ip -n "$NS" xfrm policy delete dir out index "$index"
+		;;
+	expire)
+		slowwait 10 allow_policy_gone
+		;;
+	flush)
+		ip -n "$NS" xfrm policy flush
+		;;
+	esac
+	allow_policy_gone
+}
+
+run_test() {
+	local family=$1
+	local mode=$2
+	local name="IPv$family: send after $3"
+	local plain
+	local fails
+	local sent
+	local esp
+	local ret
+
+	RET=0
+
+	setup "$family" "$mode"
+	ret=$?
+	if [ "$ret" = "$ksft_skip" ]; then
+		log_test_skip "$name"
+		cleanup_all_ns
+		return
+	elif [ "$ret" != 0 ]; then
+		check_err 1 "a policy was rejected"
+		log_test "$name"
+		cleanup_all_ns
+		return
+	fi
+
+	start_sender
+	fails=$(send_datagrams 1)
+	check_err $? "the sender did not answer"
+	check_err "$fails" "the datagram through the allow policy failed"
+	esp=$(sa_packets)
+	check_err "$esp" "the allow policy did not apply"
+
+	remove_allow_policy "$mode"
+	check_err $? "the allow policy was not removed"
+	if [ "$mode" = expire ]; then
+		# An IPv6 expiry invalidates the routes from a work item
+		slowwait 5 sent_encrypted
+		esp=$(sa_packets)
+	fi
+
+	fails=$(send_datagrams "$COUNT")
+	check_err $? "the sender did not answer"
+	if [ "$mode" = flush ]; then
+		sent=$((COUNT - fails))
+		check_err "$sent" "$sent of $COUNT datagrams not blocked"
+	else
+		plain=$((COUNT - fails - $(sa_packets) + esp))
+		check_err "$fails" "$fails of $COUNT sends failed"
+		check_err "$plain" \
+			"$plain of $COUNT datagrams sent in plaintext"
+	fi
+
+	log_test "$name"
+	cleanup
+}
+
+for family in 4 6; do
+	run_test "$family" delete "policy deletion"
+	run_test "$family" index "policy deletion by index"
+	run_test "$family" expire "policy expiry"
+	run_test "$family" flush "policy flush"
+done
+
+exit "$EXIT_STATUS"
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-02  8:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  8:52 [PATCH ipsec v3 0/2] xfrm: advance the route generation when policies are removed Ralf Lici
2026-12-02  8:52 ` [PATCH ipsec v3 1/2] " Ralf Lici
2026-12-02  8:52 ` [PATCH ipsec v3 2/2] selftests: net: add a test for xfrm policy removal Ralf Lici

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox