mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH ipsec 0/2] xfrm: iptfs: fix a panic and a use-after-free on output
@ 2026-11-18  0:54 Andrii Tereshchenko
  2026-11-18  0:54 ` [PATCH ipsec 1/2] xfrm: iptfs: don't modify the shared info of cloned skbs " Andrii Tereshchenko
  2026-11-18  0:54 ` [PATCH ipsec 2/2] xfrm: iptfs: hold the dst while a packet waits on the output queue Andrii Tereshchenko
  0 siblings, 2 replies; 3+ messages in thread
From: Andrii Tereshchenko @ 2026-11-18  0:54 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu, David S. Miller
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Christian Hopps, netdev, ipsec, linux-kernel, security,
	Andrii Tereshchenko

TCP that a host sends through its own IP-TFS SA can panic the kernel.

The bugs are in the IP-TFS output path, net/xfrm/xfrm_iptfs.c. They
exist since v6.14 and reproduce on mainline v7.4-rc3, commit
aebb72744764 ("Linux 7.4-rc3"). Ordinary TCP retransmissions trigger
them; no crafted packet is involved. The kernel either panics with
skb_over_panic in the ESP output path, or the IP-TFS delay timer
reads a dst that was already freed. Where unprivileged user
namespaces are allowed, a user without capabilities can set up such
an SA in its own namespaces and panic the kernel. This series fixes
both bugs.

Details:

The panic: iptfs_output_queued() appends inner packets to
skb_shinfo(skb)->frag_list of the first skb of an aggregate without
owning that shared info. For local TCP the first skb is a clone of the
skb TCP keeps for retransmission, so the original gets a frag_list
that its len and data_len do not count. When TCP retransmits it and
the new clone is aggregated behind another skb,
iptfs_rehome_fraglist() subtracts the stale list's length and len goes
negative. ESP then copies the skb and skb_put() panics:

  skbuff: skb_over_panic: ... len:-340 put:-340 ... end:0x180
  kernel BUG at net/core/skbuff.c:215!
  Call Trace:
   skb_put
   skb_copy
   skb_cow_data
   esp_output_head
   esp_output
   xfrm_output_resume
   iptfs_delay_timer

The use-after-free: iptfs_output_collect() queues the skb for the
delay timer without taking a reference on its dst. Local TCP attaches
its dst with skb_dst_set_noref(), and RCU can free it while the skb
waits:

  BUG: KASAN: slab-use-after-free in iptfs_delay_timer
  The buggy address belongs to the cache xfrm_dst of size 296

The input path needs no such fix: xfrm_input() calls skb_dst_force()
before the type and mode input handlers run.

The series:

  1/2  Unclone the first skb before appending to it. Send a cloned
       skb that has a frag_list on its own, and do not take page
       frags from a cloned skb.
  2/2  Take the dst reference with skb_dst_force() in
       iptfs_output_collect(), before the skb is queued.

Affected: since commit 0e4fbf013fa5 ("xfrm: iptfs: add user packet
(tunnel ingress) handling"), v6.14. iptfs_output_collect(),
iptfs_enqueue(), iptfs_rehome_fraglist() and iptfs_output_queued()
are unchanged from v6.14 to:

  mainline    aebb72744764 (v7.4-rc3)
  ipsec       f0e68014f4ac
  ipsec-next  7b6ab4dbc156

CONFIG_XFRM_IPTFS defaults to n. Current Ubuntu, Fedora, Arch and
openSUSE Tumbleweed kernels build it as a module.

Conditions: CONFIG_XFRM_IPTFS, an SA in iptfs mode that carries TCP
originated on the host itself, and retransmissions, which any path
loss causes. TCP that the host only forwards into the tunnel did not
trigger either bug in 8 runs. Creating the SA needs CAP_NET_ADMIN,
which a user has inside its own user and network namespaces: on
v7.4-rc3, uid 65534 set up the SAs that way and hit the panic. Only
the crash is verified. Whether the use-after-free can be exploited
further was not assessed.

Discovery: the panic was observed on a machine in real use during the
andrii.cloud (ARIN: AS402801) rollout. That was a distribution kernel
tainted by an out-of-tree video card module, so it was reproduced on
an untainted distribution kernel and then on mainline. The
use-after-free appeared under KASAN while reproducing the panic on
mainline.

Reproduction: v7.4-rc3 on x86_64, defconfig plus kvm_guest.config
with KASAN, IP-TFS built in, in a KVM guest, not tainted. No kernel
modification and no fault injection. Three network namespaces form a
client, an IP-TFS gateway and an HTTPS server that is the other
IP-TFS endpoint. Clients fetch a 50 KB page for 40 seconds while
netem drops 5% of the packets sent to the client, so the server
retransmits through its SA. init-delay is 20 ms. The kernel config
and full console logs are available on request.

Fix verification: most boots of the unpatched kernel crashed, with
the use-after-free or, in a few boots, the panic. No boot with the
series crashed, and KASAN reported no use-after-free in any boot that
had 2/2.

The series applies to ipsec f0e68014f4ac and builds there and on
v7.4-rc3 without W=1 warnings. The boots ran v7.4-rc3, where
net/xfrm/xfrm_iptfs.c is identical. Kernel selftests were not run;
none of them covers IP-TFS.

A reproducer is available to maintainers on request.

Mitigation: use tunnel mode instead of iptfs mode on SAs that carry
locally generated TCP.

Related work: this is not commit d042487dc118 ("xfrm: iptfs: fix stack
OOB read in iptfs_skb_reset_frag_walk()"), CVE-2026-98372, and not the
pending series "xfrm: iptfs: fix queued receive state lifetime".
Neither touches the output path.

Link: https://lore.kernel.org/20260930050307.1978654-1-roshaen09@gmail.com

The crash analysis, the patches, the reproducer and this text were
produced with an AI coding assistant (without CVP) and reviewed by 
the submitter.

Andrii Tereshchenko (2):
  xfrm: iptfs: don't modify the shared info of cloned skbs on output
  xfrm: iptfs: hold the dst while a packet waits on the output queue

 net/xfrm/xfrm_iptfs.c | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)


base-commit: f0e68014f4ac1653b2980ab76744f4dfdffdbac5
-- 
2.53.0


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

* [PATCH ipsec 1/2] xfrm: iptfs: don't modify the shared info of cloned skbs on output
  2026-11-18  0:54 [PATCH ipsec 0/2] xfrm: iptfs: fix a panic and a use-after-free on output Andrii Tereshchenko
@ 2026-11-18  0:54 ` Andrii Tereshchenko
  2026-11-18  0:54 ` [PATCH ipsec 2/2] xfrm: iptfs: hold the dst while a packet waits on the output queue Andrii Tereshchenko
  1 sibling, 0 replies; 3+ messages in thread
From: Andrii Tereshchenko @ 2026-11-18  0:54 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu, David S. Miller
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Christian Hopps, netdev, ipsec, linux-kernel, security,
	Andrii Tereshchenko, stable

TCP that a host sends through its own IP-TFS SA can panic the kernel
when segments are retransmitted.

iptfs_output_queued() aggregates inner packets by appending them to
skb_shinfo(skb)->frag_list of the first one. It appends even when that
skb is a clone, which shares its skb_shared_info with the original.
For local TCP it is one: tcp_transmit_skb() sends a clone of the skb
that stays on the retransmit queue. Appending to the clone therefore
sets a frag_list on TCP's skb that its len and data_len do not count.
ESP copies the head before sending, but the original keeps the stale
list.

When TCP retransmits that skb, the new clone carries the stale
frag_list. If the clone is then appended behind another first skb,
iptfs_rehome_fraglist() moves the stale list to that skb and subtracts
its length from the clone's len and data_len, which wrap below zero
when the list is longer than the segment. ESP copies the clone in
skb_cow_data() and skb_put() panics:

  skbuff: skb_over_panic: ... len:-340 put:-340 ... end:0x180 dev:<NULL>
  kernel BUG at net/core/skbuff.c:215!
  CPU: 3 UID: 0 PID: 0 Comm: swapper/3 Not tainted 7.4.0-rc3 #5
  RIP: 0010:skb_panic+0xbe/0xc0
  Call Trace:
   <IRQ>
   skb_put.cold+0x1f/0x1f
   skb_copy+0xfc/0x180
   skb_cow_data+0xc5/0x630
   esp_output_head+0x38a/0xb40
   esp_output+0x1c5/0x390
   xfrm_output_resume+0x113c/0x22a0
   iptfs_delay_timer+0xdb7/0x1990
   __hrtimer_run_queues+0x16d/0x340
   hrtimer_run_softirq+0x136/0x210
   handle_softirqs+0x162/0x420

Unclone the first skb before appending to it. ESP copies a cloned head
anyway, so this moves that copy earlier and does not add one. Send a
cloned first skb on its own when it already has a frag_list or cannot
be uncloned: uncloning the head does not unshare the skbs on its
frag_list, and appending writes to the last of them. For the same
reason stop aggregating at a cloned skb that has a frag_list; it
becomes the next first skb. Do not let iptfs_consume_frags() take the
page frags of a cloned skb, because that empties the frags array the
original still uses.

A first skb that was a clone can now take the page frags of the skbs
behind it, where it always used the frag_list before.

The panic was observed on a machine in real use while rolling out
andrii.cloud. It was reproduced on an unmodified v7.4-rc3 built with
KASAN, in a KVM guest, without fault injection. Three network
namespaces form a client, an IP-TFS gateway and an HTTPS server that
is the other IP-TFS endpoint. Clients fetch a 50 KB page for 40
seconds and netem drops 5% of the packets sent to the client, so that
the server retransmits through its SA. The boots ran with the server's
normal segment size or with the client advertising an MSS of 130. Most
boots of the unpatched kernel crashed: some with this panic, the
others with the use-after-free that the next patch fixes. With only
the next patch applied, a few boots at MSS 130 still crashed, all with
this panic. With the series applied, no boot crashed. Kernel selftests
were not run; none of them covers IP-TFS.

The analysis and the patch were produced with an AI coding assistant
and reviewed by the submitter.

Fixes: 0e4fbf013fa5 ("xfrm: iptfs: add user packet (tunnel ingress) handling")
Cc: stable@vger.kernel.org
Signed-off-by: Andrii Tereshchenko <opensource@andrii.net>
---
 net/xfrm/xfrm_iptfs.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/net/xfrm/xfrm_iptfs.c b/net/xfrm/xfrm_iptfs.c
index 0313a43aa..95ba73013 100644
--- a/net/xfrm/xfrm_iptfs.c
+++ b/net/xfrm/xfrm_iptfs.c
@@ -2234,6 +2234,19 @@ static void iptfs_output_queued(struct xfrm_state *x, struct sk_buff_head *list)
 		if (iptfs_first_skb(&skb, xtfs, mtu))
 			continue;
 
+		/* A clone shares skb_shinfo(), and with it the frag_list we
+		 * are about to append to, with its original, e.g. the skb TCP
+		 * keeps for retransmission. A cloned head that has a frag_list,
+		 * or that we cannot unclone, is sent on its own rather than
+		 * aggregated; the skbs on a frag_list stay shared with the
+		 * original even after an unclone, so they are never appended.
+		 */
+		if (skb_cloned(skb) &&
+		    (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.
@@ -2273,6 +2286,13 @@ static void iptfs_output_queued(struct xfrm_state *x, struct sk_buff_head *list)
 			if (skb2->len > remaining)
 				break;
 
+			/* Appending re-homes skb2's frag_list, which a clone
+			 * shares with its original. Leave such an skb to be
+			 * sent on its own as the next head.
+			 */
+			if (skb_cloned(skb2) && skb_has_frag_list(skb2))
+				break;
+
 			__skb_unlink(skb2, list);
 
 			/* Consider the buffer Tx'd and no longer owned */
@@ -2292,10 +2312,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) || shi2->frag_list ||
 			     (!skb2->head_frag && skb_headlen(skb)) ||
 			     skb->pp_recycle != skb2->pp_recycle ||
 			     skb_zcopy(skb2) ||
-- 
2.53.0


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

* [PATCH ipsec 2/2] xfrm: iptfs: hold the dst while a packet waits on the output queue
  2026-11-18  0:54 [PATCH ipsec 0/2] xfrm: iptfs: fix a panic and a use-after-free on output Andrii Tereshchenko
  2026-11-18  0:54 ` [PATCH ipsec 1/2] xfrm: iptfs: don't modify the shared info of cloned skbs " Andrii Tereshchenko
@ 2026-11-18  0:54 ` Andrii Tereshchenko
  1 sibling, 0 replies; 3+ messages in thread
From: Andrii Tereshchenko @ 2026-11-18  0:54 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu, David S. Miller
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Christian Hopps, netdev, ipsec, linux-kernel, security,
	Andrii Tereshchenko, stable

iptfs_output_collect() queues the skb on xtfs->queue for the delay
timer without taking a reference on its dst. Locally generated TCP
attaches the socket's cached route with skb_dst_set_noref(), which is
valid only inside the RCU read-side critical section of the transmit
path. The skb outlives that section on the queue. When TCP drops the
cached route, as it does from the retransmit timer, RCU frees the dst
while the skb still points to it. iptfs_output_queued() then reads the
freed dst, first in iptfs_get_cur_pmtu() and then in xfrm_output():

  BUG: KASAN: slab-use-after-free in iptfs_delay_timer+0x2ef/0x1900
  Read of size 4 at addr ffff888100cff01c by task swapper/1/0
  CPU: 1 UID: 0 PID: 0 Comm: swapper/1 Not tainted 7.4.0-rc3 #1
  Call Trace:
   <IRQ>
   iptfs_delay_timer+0x2ef/0x1900
   __hrtimer_run_queues+0x16d/0x340
   hrtimer_run_softirq+0x136/0x210
   handle_softirqs+0x162/0x420
  Allocated by task 0:
   dst_alloc+0x4f/0x250
   xfrm_bundle_create+0x236/0x1010
   xfrm_resolve_and_create_bundle+0x13c/0x1d0
   xfrm_lookup_with_ifid+0x303/0xe20
   xfrm_lookup_route+0x1c/0xf0
   ip_route_output_flow+0x15b/0x170
   inet_sk_rebuild_header+0x4c0/0xa40
   __tcp_retransmit_skb+0xe8/0xda0
  Freed by task 0:
   dst_destroy+0xe8/0x140
   rcu_core+0x3f6/0xea0
  The buggy address belongs to the object at ffff888100cfef00
   which belongs to the cache xfrm_dst of size 296

A NULL pointer dereference in xfrm_output_resume() follows the
reports.

Take the reference with skb_dst_force() in iptfs_output_collect(),
before the skb is queued. If the dst is already being destroyed, drop
the packet and count XfrmOutError. xfrm_output_one() takes the same
reference before it calls the type output handler; with IP-TFS the skb
leaves the RCU section earlier, at the queue.

KASAN reported this while the panic fixed by the previous patch was
being reproduced, on an unmodified v7.4-rc3 and without fault
injection. In the setup described there, KASAN reported the
use-after-free in most boots of the unpatched kernel and in no boot
with the series. Kernel selftests were not run; none of them covers
IP-TFS.

The analysis and the patch were produced with an AI coding assistant
and reviewed by the submitter.

Fixes: 0e4fbf013fa5 ("xfrm: iptfs: add user packet (tunnel ingress) handling")
Cc: stable@vger.kernel.org
Signed-off-by: Andrii Tereshchenko <opensource@andrii.net>
---
 net/xfrm/xfrm_iptfs.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/net/xfrm/xfrm_iptfs.c b/net/xfrm/xfrm_iptfs.c
index 95ba73013..f5260bc51 100644
--- a/net/xfrm/xfrm_iptfs.c
+++ b/net/xfrm/xfrm_iptfs.c
@@ -1914,6 +1914,20 @@ static int iptfs_output_collect(struct net *net, struct sock *sk, struct sk_buff
 			continue;
 		}
 
+		/* The skb will wait on xtfs->queue until the delay timer fires,
+		 * and iptfs_output_queued() -> xfrm_output() then dereferences
+		 * its dst. skb_dst() can be a no-ref dst (e.g. the bundle built
+		 * for a TCP retransmission) that RCU frees while the skb is
+		 * queued, so pin it before queuing; if it is already being
+		 * destroyed, drop the packet rather than queue a dangling
+		 * pointer.
+		 */
+		if (unlikely(!skb_dst_force(skb))) {
+			XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
+			kfree_skb_reason(skb, SKB_DROP_REASON_NOT_SPECIFIED);
+			continue;
+		}
+
 		/* Enqueue to send in tunnel */
 		ok = iptfs_enqueue(xtfs, skb);
 		if (!ok)
-- 
2.53.0


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

end of thread, other threads:[~2026-11-18  0:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18  0:54 [PATCH ipsec 0/2] xfrm: iptfs: fix a panic and a use-after-free on output Andrii Tereshchenko
2026-11-18  0:54 ` [PATCH ipsec 1/2] xfrm: iptfs: don't modify the shared info of cloned skbs " Andrii Tereshchenko
2026-11-18  0:54 ` [PATCH ipsec 2/2] xfrm: iptfs: hold the dst while a packet waits on the output queue 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®