mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH ipsec 0/3] xfrm: iptfs: fix the outer packet size arithmetic
@ 2026-11-29  4:50 Andrii Tereshchenko
  2026-11-29  4:50 ` [PATCH ipsec 1/3] xfrm: iptfs: don't let the inner MTU wrap around Andrii Tereshchenko
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Andrii Tereshchenko @ 2026-11-29  4:50 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu, David S. Miller
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Willem de Bruijn, Christian Hopps, netdev, ipsec, linux-kernel,
	stable

Three bugs in how IP-TFS sizes its outer packets:

1/3 the inner MTU wraps around when the SA's overhead exceeds the path
    MTU; with ESP offload an unprivileged user can panic the kernel.
2/3 ESP pads the IP-TFS header that the inner MTU leaves out, so full
    outer packets of AES-CBC SAs exceed the MTU and are dropped.
3/3 pkt_size leaves out ESP's padding, trailer and ICV, and is not
    converted again when the SA is migrated.

All three were found by an AI code review agent and reproduced on
ipsec master in KVM guests; each patch says how it was tested. 1/3 was
reported privately to the IPsec maintainers and security@kernel.org on
2026-11-20. A selftest for all three follows once 1/3 is in the
stable trees.

Andrii Tereshchenko

Andrii Tereshchenko (3):
  xfrm: iptfs: don't let the inner MTU wrap around
  xfrm: iptfs: count the IP-TFS header in the ESP padding
  xfrm: iptfs: keep outer packets within pkt_size

 net/xfrm/xfrm_iptfs.c | 50 +++++++++++++++++++++++++++----------------
 1 file changed, 32 insertions(+), 18 deletions(-)


base-commit: 1213df0bc5993e479706c5eba5830243d778f164
-- 
2.53.0


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

* [PATCH ipsec 1/3] xfrm: iptfs: don't let the inner MTU wrap around
  2026-11-29  4:50 [PATCH ipsec 0/3] xfrm: iptfs: fix the outer packet size arithmetic Andrii Tereshchenko
@ 2026-11-29  4:50 ` Andrii Tereshchenko
  2026-11-29  4:50 ` [PATCH ipsec 2/3] xfrm: iptfs: count the IP-TFS header in the ESP padding Andrii Tereshchenko
  2026-11-29  4:50 ` [PATCH ipsec 3/3] xfrm: iptfs: keep outer packets within pkt_size Andrii Tereshchenko
  2 siblings, 0 replies; 4+ messages in thread
From: Andrii Tereshchenko @ 2026-11-29  4:50 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu, David S. Miller
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Willem de Bruijn, Christian Hopps, netdev, ipsec, linux-kernel,
	stable

__iptfs_get_inner_mtu() subtracts the SA's overhead from the outer MTU
without checking that it fits. With an IPv4 MTU of 68 and AES-CBC-256
with HMAC-SHA512-256 (80 bytes of overhead) it wraps to almost 4G,
IP-TFS builds oversized packets, and with ESP offload esp_xmit() panics
on a fragment of one. An unprivileged user can set this up in a user
namespace:

  skbuff: skb_under_panic: ... len:66 put:-14 ... dev:v0
   esp_xmit+0x398/0x7c0
   validate_xmit_xfrm+0x65d/0x710

Return 0 when no data fits and drop such packets; the get_inner_mtu
callback then returns 1, as xfrm_state_mtu() does.

Made with an AI coding assistant, reviewed by the submitter.

Fixes: 4b3faf610cc6 ("xfrm: iptfs: add new iptfs xfrm mode impl")
Cc: stable@vger.kernel.org
Signed-off-by: Andrii Tereshchenko <opensource@andrii.net>
---
Found by an AI code review agent.
Validated in KVM guests on unmodified ipsec master with ESP offload
built in, without kernel changes or fault injection: an SA that uid
65534 set up in namespaces of its own over a 68-byte MTU panics the
kernel on 2 of 2 boots. The reproducer is available to the maintainers
on request.
Tested: with this series the same setup survives 2 of 2 boots with
panic_on_warn=1, and IP-TFS drops the packets (XfrmOutError).
Not tested: an IPv6 outer header, hardware ESP offload, the existing
net selftests.

 net/xfrm/xfrm_iptfs.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/net/xfrm/xfrm_iptfs.c b/net/xfrm/xfrm_iptfs.c
index 0313a43aa8a97..c204f41a57f59 100644
--- a/net/xfrm/xfrm_iptfs.c
+++ b/net/xfrm/xfrm_iptfs.c
@@ -2215,10 +2215,11 @@ static void iptfs_output_queued(struct xfrm_state *x, struct sk_buff_head *list)
 		skb->protocol = x->outer_mode.family == AF_INET ? htons(ETH_P_IP) :
 								  htons(ETH_P_IPV6);
 
-		if (skb->len > mtu && xtfs->cfg.dont_frag) {
+		if (!mtu || (skb->len > mtu && xtfs->cfg.dont_frag)) {
 			/* We handle this case before enqueueing so we are only
 			 * here b/c MTU changed after we enqueued before we
-			 * dequeued, just drop these.
+			 * dequeued, just drop these. An mtu of 0 means that the
+			 * outer MTU cannot carry any data at all.
 			 */
 			XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTERROR);
 
@@ -2490,17 +2491,25 @@ static int iptfs_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
  * @x: xfrm state.
  * @outer_mtu: the outer mtu
  *
- * Return: Correct MTU taking in to account the encap overhead.
+ * Return: Correct MTU taking in to account the encap overhead, or 0 if the
+ * outer MTU leaves no room for data.
  */
 static u32 __iptfs_get_inner_mtu(struct xfrm_state *x, int outer_mtu)
 {
 	struct crypto_aead *aead;
-	u32 blksize;
+	u32 blksize, overhead, payload;
 
 	aead = x->data;
 	blksize = ALIGN(crypto_aead_blocksize(aead), 4);
-	return ((outer_mtu - x->props.header_len - crypto_aead_authsize(aead)) &
-		~(blksize - 1)) - 2;
+	overhead = x->props.header_len + crypto_aead_authsize(aead);
+	if (outer_mtu <= overhead)
+		return 0;
+
+	payload = (outer_mtu - overhead) & ~(blksize - 1);
+	if (payload <= 2)
+		return 0;
+
+	return payload - 2;
 }
 
 /**
@@ -2517,7 +2526,8 @@ static u32 iptfs_get_inner_mtu(struct xfrm_state *x, int outer_mtu)
 	/* If not dont-frag we have no MTU */
 	if (!xtfs->cfg.dont_frag)
 		return x->outer_mode.family == AF_INET ? IP_MAX_MTU : IP6_MAX_MTU;
-	return __iptfs_get_inner_mtu(x, outer_mtu);
+	/* Like xfrm_state_mtu() for the other modes, never return 0 */
+	return __iptfs_get_inner_mtu(x, outer_mtu) ?: 1;
 }
 
 /**
-- 
2.53.0


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

* [PATCH ipsec 2/3] xfrm: iptfs: count the IP-TFS header in the ESP padding
  2026-11-29  4:50 [PATCH ipsec 0/3] xfrm: iptfs: fix the outer packet size arithmetic Andrii Tereshchenko
  2026-11-29  4:50 ` [PATCH ipsec 1/3] xfrm: iptfs: don't let the inner MTU wrap around Andrii Tereshchenko
@ 2026-11-29  4:50 ` Andrii Tereshchenko
  2026-11-29  4:50 ` [PATCH ipsec 3/3] xfrm: iptfs: keep outer packets within pkt_size Andrii Tereshchenko
  2 siblings, 0 replies; 4+ messages in thread
From: Andrii Tereshchenko @ 2026-11-29  4:50 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu, David S. Miller
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Willem de Bruijn, Christian Hopps, netdev, ipsec, linux-kernel,
	stable

__iptfs_get_inner_mtu() aligns the room left for data to the cipher
block size without the 4-byte IP-TFS header, which header_len counts but
which ESP pads together with the data. With AES-CBC suites, ESP then
pads full outer packets past the MTU by up to 12 bytes: 1512 bytes on a
1500-byte IPv4 path with HMAC-SHA1-96. Their outer header has DF set,
so they are dropped, and forwarded traffic that fills outer packets is
lost; a TCP connection through such an SA moves no data. AEADs such as
AES-GCM are not affected.

Count the IP-TFS header (x->props.enc_hdr_len) in the aligned region,
as ESP does.

Made with an AI coding assistant, reviewed by the submitter.

Fixes: 4b3faf610cc6 ("xfrm: iptfs: add new iptfs xfrm mode impl")
Cc: stable@vger.kernel.org
Signed-off-by: Andrii Tereshchenko <opensource@andrii.net>
---
Found by an AI code review agent.
Validated in KVM guests on unmodified ipsec master, without kernel
changes: of 200 datagrams of 1500 bytes that a gateway forwarded into
an AES-CBC, HMAC-SHA1-96 SA over 1500-byte links, none arrived.
Tested: with this patch all 200 arrive, and the largest outer packet
is 1496 bytes.
Not tested: hardware ESP offload, the existing net selftests.

 net/xfrm/xfrm_iptfs.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/net/xfrm/xfrm_iptfs.c b/net/xfrm/xfrm_iptfs.c
index c204f41a57f59..bd4d445428d6c 100644
--- a/net/xfrm/xfrm_iptfs.c
+++ b/net/xfrm/xfrm_iptfs.c
@@ -2501,15 +2501,20 @@ static u32 __iptfs_get_inner_mtu(struct xfrm_state *x, int outer_mtu)
 
 	aead = x->data;
 	blksize = ALIGN(crypto_aead_blocksize(aead), 4);
-	overhead = x->props.header_len + crypto_aead_authsize(aead);
+	/* ESP pads the IP-TFS header (enc_hdr_len), the data and its own
+	 * 2-byte trailer together to blksize, so the IP-TFS header belongs
+	 * to the aligned payload, not to the overhead.
+	 */
+	overhead = x->props.header_len - x->props.enc_hdr_len +
+		   crypto_aead_authsize(aead);
 	if (outer_mtu <= overhead)
 		return 0;
 
 	payload = (outer_mtu - overhead) & ~(blksize - 1);
-	if (payload <= 2)
+	if (payload <= x->props.enc_hdr_len + 2)
 		return 0;
 
-	return payload - 2;
+	return payload - x->props.enc_hdr_len - 2;
 }
 
 /**
-- 
2.53.0


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

* [PATCH ipsec 3/3] xfrm: iptfs: keep outer packets within pkt_size
  2026-11-29  4:50 [PATCH ipsec 0/3] xfrm: iptfs: fix the outer packet size arithmetic Andrii Tereshchenko
  2026-11-29  4:50 ` [PATCH ipsec 1/3] xfrm: iptfs: don't let the inner MTU wrap around Andrii Tereshchenko
  2026-11-29  4:50 ` [PATCH ipsec 2/3] xfrm: iptfs: count the IP-TFS header in the ESP padding Andrii Tereshchenko
@ 2026-11-29  4:50 ` Andrii Tereshchenko
  2 siblings, 0 replies; 4+ messages in thread
From: Andrii Tereshchenko @ 2026-11-29  4:50 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu, David S. Miller
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Willem de Bruijn, Christian Hopps, netdev, ipsec, linux-kernel,
	stable

XFRMA_IPTFS_PKT_SIZE is the size of the outer packet, but
iptfs_user_init() turns it into a data budget by taking away only
header_len, not ESP's padding, trailer and ICV. Full outer packets are
therefore larger than pkt_size, 1020 bytes for 1000 with AES-GCM, and
on a path that pkt_size was set to fit, for example one that filters
ICMP, they are dropped without a trace. The budget is also kept when
the SA is migrated to another family or encapsulation, which changes
header_len: moving it to IPv6 adds another 20 bytes.

Drop payload_mtu, convert pkt_size with __iptfs_get_inner_mtu() each
time it is used, and refuse a pkt_size that leaves no room for data.

Made with an AI coding assistant, reviewed by the submitter.

Fixes: 4b3faf610cc6 ("xfrm: iptfs: add new iptfs xfrm mode impl")
Cc: stable@vger.kernel.org
Signed-off-by: Andrii Tereshchenko <opensource@andrii.net>
---
Found by an AI code review agent.
Validated in KVM guests on unmodified ipsec master, without kernel
changes: with AES-GCM and a pkt_size of 1000, none of 200 forwarded
datagrams crossed a router whose next link has an MTU of 1000 and
which sends no ICMP errors, and a pkt_size of 50 was accepted.
Tested: with this patch all 200 cross and a pkt_size of 50 is refused.
The migration case was tested with a KUnit case of my own, not posted.
Not tested: hardware ESP offload, the existing net selftests.

 net/xfrm/xfrm_iptfs.c | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/net/xfrm/xfrm_iptfs.c b/net/xfrm/xfrm_iptfs.c
index bd4d445428d6c..49b761d0a6be1 100644
--- a/net/xfrm/xfrm_iptfs.c
+++ b/net/xfrm/xfrm_iptfs.c
@@ -133,7 +133,6 @@ struct skb_wseq {
  * @init_delay_ns: nanoseconds to wait to send initial IPTFS packet.
  * @iptfs_timer: output timer.
  * @iptfs_settime: time the output timer was set.
- * @payload_mtu: max payload size.
  * @w_seq_set: true after first seq received.
  * @w_wantseq: waiting for this seq number as next to process (in order).
  * @w_saved: the saved buf array (reorder window).
@@ -158,7 +157,6 @@ struct xfrm_iptfs_data {
 	u64 init_delay_ns;	    /* nanoseconds */
 	struct hrtimer iptfs_timer; /* output timer */
 	time64_t iptfs_settime;	    /* time timer was set */
-	u32 payload_mtu;	    /* max payload size */
 
 	/* Tunnel input reordering */
 	bool w_seq_set;		  /* true after first seq received */
@@ -1804,11 +1802,13 @@ static int iptfs_get_cur_pmtu(struct xfrm_state *x, struct xfrm_iptfs_data *xtfs
 			      struct sk_buff *skb)
 {
 	struct xfrm_dst *xdst = (struct xfrm_dst *)skb_dst(skb);
-	u32 payload_mtu = xtfs->payload_mtu;
 	u32 pmtu = __iptfs_get_inner_mtu(x, xdst->child_mtu_cached);
 
-	if (payload_mtu && payload_mtu < pmtu)
-		pmtu = payload_mtu;
+	/* Converted here rather than once, as migration can change
+	 * header_len.
+	 */
+	if (xtfs->cfg.pkt_size)
+		pmtu = min(pmtu, __iptfs_get_inner_mtu(x, xtfs->cfg.pkt_size));
 
 	return pmtu;
 }
@@ -2574,13 +2574,12 @@ static int iptfs_user_init(struct net *net, struct xfrm_state *x,
 	}
 	if (attrs[XFRMA_IPTFS_PKT_SIZE]) {
 		xc->pkt_size = nla_get_u32(attrs[XFRMA_IPTFS_PKT_SIZE]);
-		if (!xc->pkt_size) {
-			xtfs->payload_mtu = 0;
-		} else if (xc->pkt_size > x->props.header_len) {
-			xtfs->payload_mtu = xc->pkt_size - x->props.header_len;
-		} else {
+		/* pkt_size is the whole outer packet, ESP padding, trailer
+		 * and ICV included; iptfs_get_cur_pmtu() converts it.
+		 */
+		if (xc->pkt_size && !__iptfs_get_inner_mtu(x, xc->pkt_size)) {
 			NL_SET_ERR_MSG(extack,
-				       "Packet size must be 0 or greater than IPTFS/ESP header length");
+				       "Packet size must be 0 or greater than IPTFS/ESP overhead");
 			return -EINVAL;
 		}
 	}
-- 
2.53.0


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

end of thread, other threads:[~2026-11-29  4:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-29  4:50 [PATCH ipsec 0/3] xfrm: iptfs: fix the outer packet size arithmetic Andrii Tereshchenko
2026-11-29  4:50 ` [PATCH ipsec 1/3] xfrm: iptfs: don't let the inner MTU wrap around Andrii Tereshchenko
2026-11-29  4:50 ` [PATCH ipsec 2/3] xfrm: iptfs: count the IP-TFS header in the ESP padding Andrii Tereshchenko
2026-11-29  4:50 ` [PATCH ipsec 3/3] xfrm: iptfs: keep outer packets within pkt_size Andrii Tereshchenko

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

all inboxes | Powered by JetHome®