Netdev List
 help / color / mirror / Atom feed
* [PATCH ipsec] xfrm: don't encrypt two copies of an ESP packet under one sequence number
@ 2026-12-06  2:37 Andrii Tereshchenko
  2026-12-06  2:42 ` netdev-bot+sinfo
  0 siblings, 1 reply; 2+ messages in thread
From: Andrii Tereshchenko @ 2026-12-06  2:37 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu, David S. Miller
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Willem de Bruijn,
	Simon Horman, netdev, ipsec, stable

A copy made after xfrm_output() shares the original's secpath and
so its ESP sequence numbers; both copies then encrypt under one
nonce. That exposes the XOR of the two plaintexts under any
sequence-derived nonce (AES-GCM and the other seqno-nonce AEADs),
and yields GCM's authentication key; CBC and NULL only drop the
replayed copy.
Give a still-shared copy a private secpath and new numbers before
encryption, and each GSO segment its own secpath.

Fixes: fca11ebde3f0 ("esp4: Reorganize esp_output")
Fixes: 383d0350f2cc ("esp6: Reorganize esp_output")
Cc: stable@vger.kernel.org
Signed-off-by: Andrii Tereshchenko <opensource@andrii.net>
---
The reuse needs a qdisc copy (e.g. netem duplicate) or forwarded
GSO traffic. In testing, a packet forged with a recovered key was
accepted by the receiver. A copied segment's numbers run into the
following packets', which drop as replays, so sustained
duplication may stall the connection. Present since v4.12 (both
Fixes: tags).

Tested at ipsec master 23e2765f3805, ksft config (ESP and ESP6
offload, netem): every case fails without this patch and passes
with it, packet cases and live forgery both ways, xfrm_policy and
xfrm_state green, no splats under KASAN + lockdep. NIPA GREEN.
Uncopied traffic is unaffected: the unshare early-outs when the
secpath is not shared, and only GSO ESP transmit pays per-segment
secpath allocation. No hardware offload device was tested
(software path only).

Jeremy Jean's pending ESP IV/ESN series
(https://lore.kernel.org/all/20261108142652.1330952-1-Jeremy.Jean@oss.cyber.gouv.fr/)
fixes nonce reuse in the output path without any copy; this patch
covers copies made later, at the qdisc. Disjoint sites, no
ordering needed. Applies to 7.3.y and 7.2.y as-is; 7.1.y and
older need the header hunk placed by hand.

An AI coding assistant was used to write this patch; I reviewed and
tested the result.

A reproducer is available to the maintainers on request.

 include/net/xfrm.h      |  2 ++
 net/ipv4/esp4_offload.c | 10 +++++++---
 net/ipv6/esp6_offload.c | 10 +++++++---
 net/xfrm/xfrm_device.c  | 78 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 98 insertions(+), 2 deletions(-)

diff --git a/include/net/xfrm.h b/include/net/xfrm.h
index 445223a8dc6f1..b6d3d5a88942a 100644
--- a/include/net/xfrm.h
+++ b/include/net/xfrm.h
@@ -2145,6 +2145,8 @@ void __init xfrm_dev_init(void);
 void xfrm_dev_resume(struct sk_buff *skb);
 void xfrm_dev_backlog(struct softnet_data *sd);
 struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features, bool *again);
+int xfrm_dev_seq_unshare(struct xfrm_state *x, struct sk_buff *skb, bool hdr);
+struct sk_buff *xfrm_dev_segs_unshare(struct sk_buff *segs);
 int xfrm_dev_state_add(struct net *net, struct xfrm_state *x,
 		       const struct xfrm_user_offload *xuo,
 		       struct netlink_ext_ack *extack);
diff --git a/net/ipv4/esp4_offload.c b/net/ipv4/esp4_offload.c
index abd77162f5e75..ee6669beb0971 100644
--- a/net/ipv4/esp4_offload.c
+++ b/net/ipv4/esp4_offload.c
@@ -212,6 +212,8 @@ static struct sk_buff *esp4_gso_segment(struct sk_buff *skb,
 	netdev_features_t esp_features = features;
 	struct xfrm_offload *xo = xfrm_offload(skb);
 	struct sec_path *sp;
+	struct sk_buff *segs;
+	int err;
 
 	if (!xo)
 		return ERR_PTR(-EINVAL);
@@ -221,6 +223,11 @@ static struct sk_buff *esp4_gso_segment(struct sk_buff *skb,
 
 	sp = skb_sec_path(skb);
 	x = sp->xvec[sp->len - 1];
+	err = xfrm_dev_seq_unshare(x, skb, false);
+	if (err)
+		return ERR_PTR(err);
+	xo = xfrm_offload(skb);
+
 	aead = x->data;
 	esph = ip_esp_hdr(skb);
 
@@ -245,7 +252,8 @@ static struct sk_buff *esp4_gso_segment(struct sk_buff *skb,
 
 	xo->flags |= XFRM_GSO_SEGMENT;
 
-	return xfrm4_outer_mode_gso_segment(x, skb, esp_features);
+	segs = xfrm4_outer_mode_gso_segment(x, skb, esp_features);
+	return xfrm_dev_segs_unshare(segs);
 }
 
 static int esp_input_tail(struct xfrm_state *x, struct sk_buff *skb)
diff --git a/net/ipv6/esp6_offload.c b/net/ipv6/esp6_offload.c
index 22895521a57d0..8cbb74651354f 100644
--- a/net/ipv6/esp6_offload.c
+++ b/net/ipv6/esp6_offload.c
@@ -252,6 +252,8 @@ static struct sk_buff *esp6_gso_segment(struct sk_buff *skb,
 	netdev_features_t esp_features = features;
 	struct xfrm_offload *xo = xfrm_offload(skb);
 	struct sec_path *sp;
+	struct sk_buff *segs;
+	int err;
 
 	if (!xo)
 		return ERR_PTR(-EINVAL);
@@ -261,6 +263,11 @@ static struct sk_buff *esp6_gso_segment(struct sk_buff *skb,
 
 	sp = skb_sec_path(skb);
 	x = sp->xvec[sp->len - 1];
+	err = xfrm_dev_seq_unshare(x, skb, false);
+	if (err)
+		return ERR_PTR(err);
+	xo = xfrm_offload(skb);
+
 	aead = x->data;
 	esph = ip_esp_hdr(skb);
 
@@ -283,7 +290,8 @@ static struct sk_buff *esp6_gso_segment(struct sk_buff *skb,
 
 	xo->flags |= XFRM_GSO_SEGMENT;
 
-	return xfrm6_outer_mode_gso_segment(x, skb, esp_features);
+	segs = xfrm6_outer_mode_gso_segment(x, skb, esp_features);
+	return xfrm_dev_segs_unshare(segs);
 }
 
 static int esp6_input_tail(struct xfrm_state *x, struct sk_buff *skb)
diff --git a/net/xfrm/xfrm_device.c b/net/xfrm/xfrm_device.c
index f153bf695b9dd..61d43141f27eb 100644
--- a/net/xfrm/xfrm_device.c
+++ b/net/xfrm/xfrm_device.c
@@ -15,6 +15,7 @@
 #include <linux/slab.h>
 #include <linux/spinlock.h>
 #include <net/dst.h>
+#include <net/esp.h>
 #include <net/gso.h>
 #include <net/xfrm.h>
 #include <linux/notifier.h>
@@ -112,6 +113,75 @@ static inline bool xmit_xfrm_check_overflow(struct sk_buff *skb)
 	return false;
 }
 
+/* A copy of the skb made after xfrm_output(), such as a qdisc's duplicate,
+ * shares its secpath and so the sequence numbers in xo->seq. Two different
+ * packets encrypted with them would reuse the nonce. Give a copy that is
+ * still shared a private secpath and new sequence numbers, and its ESP
+ * header the new number if @hdr (GSO segments take theirs in ->xmit()).
+ */
+int xfrm_dev_seq_unshare(struct xfrm_state *x, struct sk_buff *skb, bool hdr)
+{
+	struct xfrm_skb_cb *cb = XFRM_SKB_CB(skb);
+	typeof(cb->seq) seq = cb->seq;
+	int err;
+
+	if (x->xso.type == XFRM_DEV_OFFLOAD_PACKET ||
+	    refcount_read(&skb->extensions->refcnt) == 1)
+		return 0;
+
+	if (!secpath_set(skb) || (hdr && skb_cow_head(skb, 0))) {
+		XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTERROR);
+		return -ENOMEM;
+	}
+
+	spin_lock_bh(&x->lock);
+	err = xfrm_replay_overflow(x, skb);
+	spin_unlock_bh(&x->lock);
+	/* It also set XFRM_SKB_CB(skb)->seq, which overlaps SKB_GSO_CB(skb). */
+	cb->seq = seq;
+	if (err) {
+		XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATESEQERROR);
+		return err;
+	}
+
+	if (hdr)
+		ip_esp_hdr(skb)->seq_no = htonl(xfrm_offload(skb)->seq.low);
+	return 0;
+}
+EXPORT_SYMBOL_GPL(xfrm_dev_seq_unshare);
+
+/* The segments of an ESP GSO packet share its secpath, and ->xmit() hands
+ * them its sequence numbers in turn. Give each its own secpath and numbers
+ * instead, so that a copy of one segment is seen by xfrm_dev_seq_unshare().
+ */
+struct sk_buff *xfrm_dev_segs_unshare(struct sk_buff *segs)
+{
+	struct xfrm_offload *xo;
+	struct sk_buff *seg;
+	u32 low, hi;
+
+	if (IS_ERR_OR_NULL(segs))
+		return segs;
+
+	xo = xfrm_offload(segs);
+	low = xo->seq.low;
+	hi = xo->seq.hi;
+	for (seg = segs; seg; seg = seg->next) {
+		if (!secpath_set(seg)) {
+			kfree_skb_list(segs);
+			return ERR_PTR(-ENOMEM);
+		}
+		xo = xfrm_offload(seg);
+		xo->seq.low = low;
+		xo->seq.hi = hi;
+		low += skb_is_gso(seg) ? skb_shinfo(seg)->gso_segs : 1;
+		if (low < xo->seq.low)
+			hi++;
+	}
+	return segs;
+}
+EXPORT_SYMBOL_GPL(xfrm_dev_segs_unshare);
+
 struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features, bool *again)
 {
 	int err;
@@ -155,6 +225,14 @@ struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t featur
 		return skb;
 	}
 
+	/* A segment's transport header is not the ESP header yet. */
+	if (xfrm_dev_seq_unshare(x, skb, !(xo->flags & XFRM_GSO_SEGMENT))) {
+		kfree_skb_list(skb);
+		dev_core_stats_tx_dropped_inc(dev);
+		return NULL;
+	}
+	xo = xfrm_offload(skb);
+
 	if (skb_is_gso(skb) && (unlikely(x->xso.dev != dev) ||
 				unlikely(xmit_xfrm_check_overflow(skb)))) {
 		struct sk_buff *segs;

base-commit: 23e2765f3805a334b6b6dda454df2ff7b7cb1889
-- 
2.53.0


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

end of thread, other threads:[~2026-12-06  2:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06  2:37 [PATCH ipsec] xfrm: don't encrypt two copies of an ESP packet under one sequence number Andrii Tereshchenko
2026-12-06  2:42 ` netdev-bot+sinfo

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