[PATCH ipsec v2] xfrm: iptfs: don't modify skbs it doesn't own when aggregating
From: Andrii Tereshchenko
Date: Thu Nov 26 2026 - 22:43:37 EST
iptfs_output_queued() aggregates inner packets into the first skb,
writing to its shared info and relinking frag_lists. But a first skb
can share those with someone else: a clone (the original TCP keeps
for retransmission), a frag_list shared without cloning (skb_cow(),
pskb_copy()), or a nested frag_list (conntrack). The result is memory
corruption, a CPU-hanging list cycle, or leaked skbs:
skbuff: skb_over_panic: ... len:-340 put:-340 ...
skb_copy+0xfc/0x180
esp_output_head+0x38a/0xb40
Aggregate only into what IP-TFS owns: unclone the first skb, send a
first skb that has a frag_list on its own, and stop at a following
skb that has one. Nothing is un-nested any more, so
iptfs_rehome_fraglist() goes.
The two frag_list cases were found by an AI code review agent after
v1 was posted. The patch was written with an AI coding assistant and
reviewed by the submitter.
Fixes: 0e4fbf013fa5 ("xfrm: iptfs: add user packet (tunnel ingress) handling")
Link: https://lore.kernel.org/all/179171627713.434549.11629734394381610294@xxxxxxxxxx/
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Andrii Tereshchenko <opensource@xxxxxxxxxx>
---
v2:
- Apply the frag_list rule to every skb that has one, not only to
clones. v1 missed lists shared through skb_cow() or pskb_copy() and
nested ones from conntrack. iptfs_rehome_fraglist() and the
un-nesting loop are removed.
- The netdev AI review of v1 [1] reported the same gap and one more way
into it: skb_checksum_help() in iptfs_first_skb() unclones the first
skb before v1's check. Testing skb_has_frag_list() covers that too.
- v1 said that uncloning the first skb adds no copy. As that review
showed, this was wrong when packets are aggregated: ESP's
skb_cow_data() can then reallocate the head again. v2 does not change
that cost.
- Drop v1's 2/2: Nguyen Ngoc Thang's same fix is upstream as
b88c75488ecc ("xfrm: iptfs: force a dst reference before queueing
an skb").
v1: https://lore.kernel.org/all/c1e999b5-15ee-468d-81c5-530a560e84df@xxxxxxxxxxxxxx/
[1] https://netdev-ai.bots.linux.dev/sashiko/#/patchset/b56bbced-a8b0-4ae9-80b4-1958ea36684c%40mid.andrii.net
Affected: v6.14 and later (0e4fbf013fa5 first shipped in v6.14-rc1);
needs CONFIG_XFRM_IPTFS and an IP-TFS SA carrying aggregated traffic.
Mitigation until the fix lands: no IP-TFS SAs, or the xfrm_iptfs
module left unloaded (it is tristate).
Found: a production panic (the cloned head), then an AI coding
assistant review after v1 (the frag_list cases). Validated: a test
module driving the CHECKSUM_PARTIAL route, 50 of 50 rounds. Tested:
the private selftest (nested FAIL to PASS, shared hang to PASS) on
ipsec master 2803867cf3de, two boots each, panic_on_warn=1 on the
fixed kernel.
net/xfrm/xfrm_iptfs.c | 61 ++++++++++++++++++-------------------------
1 file changed, 26 insertions(+), 35 deletions(-)
diff --git a/net/xfrm/xfrm_iptfs.c b/net/xfrm/xfrm_iptfs.c
index 0313a43aa8a97..c6bdfbd9a1a8b 100644
--- a/net/xfrm/xfrm_iptfs.c
+++ b/net/xfrm/xfrm_iptfs.c
@@ -2139,27 +2139,6 @@ static int iptfs_first_skb(struct sk_buff **skbp, struct xfrm_iptfs_data *xtfs,
return iptfs_copy_create_frags(skbp, xtfs, mtu);
}
-static struct sk_buff **iptfs_rehome_fraglist(struct sk_buff **nextp, struct sk_buff *child)
-{
- u32 fllen = 0;
-
- /* It might be possible to account for a frag list in addition to page
- * fragment if it's a valid state to be in. The page fragments size
- * should be kept as data_len so only the frag_list size is removed,
- * this must be done above as well.
- */
- *nextp = skb_shinfo(child)->frag_list;
- while (*nextp) {
- fllen += (*nextp)->len;
- nextp = &(*nextp)->next;
- }
- skb_frag_list_init(child);
- child->len -= fllen;
- child->data_len -= fllen;
-
- return nextp;
-}
-
static void iptfs_consume_frags(struct sk_buff *to, struct sk_buff *from)
{
struct skb_shared_info *fromi = skb_shinfo(from);
@@ -2234,6 +2213,21 @@ static void iptfs_output_queued(struct xfrm_state *x, struct sk_buff_head *list)
if (iptfs_first_skb(&skb, xtfs, mtu))
continue;
+ /* Aggregating writes to the first skb's shared info and, when
+ * it has a frag_list, to the skbs on that list. A clone shares
+ * its shared info with the original, e.g. the skb TCP keeps
+ * for retransmission, so unclone it first. The skbs on a
+ * frag_list can be shared with another skb whatever the first
+ * skb is: pskb_copy() and uncloning share them, and the list
+ * of a datagram that conntrack reassembled can nest. Send a
+ * first skb that has a frag_list, or that we cannot unclone,
+ * on its own.
+ */
+ if (skb_has_frag_list(skb) || skb_unclone(skb, GFP_ATOMIC)) {
+ xfrm_output(NULL, skb);
+ continue;
+ }
+
/* If fragmentation was required the returned skb is the last
* IPTFS fragment in the chain, and it's IPTFS header blkoff has
* been set just past the end of the fragment data.
@@ -2246,18 +2240,8 @@ static void iptfs_output_queued(struct xfrm_state *x, struct sk_buff_head *list)
*/
remaining = mtu - (skb->len - sizeof(struct ip_iptfs_hdr));
- /* Re-home (un-nest) nested fragment lists. We need to do this
- * b/c we will simply be appending any following aggregated
- * inner packets using the frag list.
- */
shi = skb_shinfo(skb);
nextp = &shi->frag_list;
- while (*nextp) {
- if (skb_has_frag_list(*nextp))
- nextp = iptfs_rehome_fraglist(&(*nextp)->next, *nextp);
- else
- nextp = &(*nextp)->next;
- }
if (shi->frag_list || skb_cloned(skb) || skb_shared(skb))
share_ok = false;
@@ -2273,6 +2257,13 @@ static void iptfs_output_queued(struct xfrm_state *x, struct sk_buff_head *list)
if (skb2->len > remaining)
break;
+ /* Appending would relink skb2's frag_list, which may
+ * be shared. Leave skb2 to be sent on its own as the
+ * next first skb.
+ */
+ if (skb_has_frag_list(skb2))
+ break;
+
__skb_unlink(skb2, list);
/* Consider the buffer Tx'd and no longer owned */
@@ -2292,10 +2283,13 @@ static void iptfs_output_queued(struct xfrm_state *x, struct sk_buff_head *list)
/* skb->pp_recycle is passed to __skb_flag_unref for all
* frag pages so we can only share pages with skb's who
* match ourselves.
+ *
+ * iptfs_consume_frags() empties skb2's frags, which a
+ * clone shares with its original.
*/
shi2 = skb_shinfo(skb2);
if (share_ok &&
- (shi2->frag_list ||
+ (skb_cloned(skb2) ||
(!skb2->head_frag && skb_headlen(skb)) ||
skb->pp_recycle != skb2->pp_recycle ||
skb_zcopy(skb2) ||
@@ -2315,9 +2309,6 @@ static void iptfs_output_queued(struct xfrm_state *x, struct sk_buff_head *list)
/* Append to the frag_list */
*nextp = skb2;
nextp = &skb2->next;
- if (skb_has_frag_list(skb2))
- nextp = iptfs_rehome_fraglist(nextp,
- skb2);
skb->truesize += skb2->truesize;
}
}
base-commit: 2803867cf3defa294710088a4663b434e12b5e93
--
2.53.0