mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrii Tereshchenko <opensource@andrii.net>
To: Steffen Klassert <steffen.klassert@secunet.com>,
	Herbert Xu <herbert@gondor.apana.org.au>,
	"David S. Miller" <davem@davemloft.net>
Cc: Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>,
	Willem de Bruijn <willemb@google.com>,
	Christian Hopps <chopps@labn.net>,
	netdev@vger.kernel.org, ipsec@lists.linux.dev,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: [PATCH ipsec 1/3] xfrm: iptfs: don't let the inner MTU wrap around
Date: Sat, 28 Nov 2026 21:50:09 -0700	[thread overview]
Message-ID: <10ceac4a-a93a-4ed9-9ea5-356c931664fe@mid.andrii.net> (raw)
In-Reply-To: <8cfaaaca-0668-485f-bc87-f9fde6c4b25d@mid.andrii.net>

__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


  reply	other threads:[~2026-11-29  4:50 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=10ceac4a-a93a-4ed9-9ea5-356c931664fe@mid.andrii.net \
    --to=opensource@andrii.net \
    --cc=chopps@labn.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=horms@kernel.org \
    --cc=ipsec@lists.linux.dev \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=stable@vger.kernel.org \
    --cc=steffen.klassert@secunet.com \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®