[PATCH ipsec 1/3] xfrm: iptfs: don't let the inner MTU wrap around

From: Andrii Tereshchenko

Date: Sat Nov 28 2026 - 23:50:35 EST


__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@xxxxxxxxxxxxxxx
Signed-off-by: Andrii Tereshchenko <opensource@xxxxxxxxxx>
---
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