* [PATCH ipsec 1/2] xfrm: iptfs: don't modify the shared info of cloned skbs on output
[not found] <b56bbced-a8b0-4ae9-80b4-1958ea36684c@mid.andrii.net>
@ 2026-11-18 0:54 ` Andrii Tereshchenko
2026-11-22 1:38 ` netdev-bot+sashiko
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, 1 reply; 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 related [flat|nested] 3+ messages in thread
* [PATCH ipsec 2/2] xfrm: iptfs: hold the dst while a packet waits on the output queue
[not found] <b56bbced-a8b0-4ae9-80b4-1958ea36684c@mid.andrii.net>
2026-11-18 0:54 ` [PATCH ipsec 1/2] xfrm: iptfs: don't modify the shared info of cloned skbs on output 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 related [flat|nested] 3+ messages in thread
* Re: [PATCH ipsec 1/2] xfrm: iptfs: don't modify the shared info of cloned skbs on output
2026-11-18 0:54 ` [PATCH ipsec 1/2] xfrm: iptfs: don't modify the shared info of cloned skbs on output Andrii Tereshchenko
@ 2026-11-22 1:38 ` netdev-bot+sashiko
0 siblings, 0 replies; 3+ messages in thread
From: netdev-bot+sashiko @ 2026-11-22 1:38 UTC (permalink / raw)
To: opensource
Cc: steffen.klassert, herbert, davem, edumazet, kuba, pabeni, horms,
chopps, netdev, ipsec, linux-kernel, security, stable
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] The fix is incomplete.
- [Low] The commit message says: "ESP copies a cloned head anyway, so
this moves that copy earlier and does not add one." That is not true…
--
xfrm: iptfs: don't modify the shared info of cloned skbs on output
This patch changes how iptfs_output_queued() handles a cloned first skb.
It unclones the skb before appending inner packets to its frag_list, or
sends it on its own if it already has a frag_list or cannot be uncloned.
It also stops aggregation at a cloned skb2 that has a frag_list, and
keeps iptfs_consume_frags() from taking the page frags of a cloned skb2.
> diff --git a/net/xfrm/xfrm_iptfs.c b/net/xfrm/xfrm_iptfs.c
> index 0313a43aa8a97..95ba7301324b9 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;
> + }
[Severity: High]
Does this guard hold the "they are never appended" rule from the comment
above? It only tests skb_cloned(skb). It also runs after
iptfs_first_skb(), which may already have uncloned the head.
For a CHECKSUM_PARTIAL head, iptfs_first_skb() does:
if (skb->ip_summed == CHECKSUM_PARTIAL) {
err = skb_checksum_help(skb);
On a cloned skb, skb_checksum_help()->skb_ensure_writable()->
pskb_expand_head() calls skb_clone_fraglist(). That only takes a
reference on each frag_list member. It then clears skb->cloned and
resets dataref to 1:
net/core/skbuff.c:pskb_expand_head() {
...
if (skb_has_frag_list(skb))
skb_clone_fraglist(skb);
...
skb->cloned = 0;
...
}
After that, skb_cloned(skb) is false, but the original still links the
frag_list members.
Wouldn't two later steps then write into sk_buffs the other owner still
links? The first is the re-home loop, where iptfs_rehome_fraglist()
rewrites the members' frag_list, len and data_len. The second is this
append:
*nextp = skb2;
The ->next of the last shared member would then point to skb2, which the
other owner's len and data_len do not count.
There seem to be a few other ways to reach the same state.
The new break in the inner loop leaves a cloned skb2 that has a
frag_list for the next head. On the next pass that skb goes through
iptfs_first_skb() again, so it can be uncloned the same way and then
aggregated anyway.
Heads can also arrive already uncloned but with shared frag_list
members. For example, nf_dup_ipv4() calls pskb_copy(), and
__pskb_copy_fclone() does:
if (skb_has_frag_list(skb)) {
skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
skb_clone_fraglist(n);
}
A reassembled forwarded packet and its duplicate are then both
non-cloned but share their frag_list members. skb_cow() in ip_forward()
on a cloned skb has the same effect.
A non-cloned skb2 whose frag_list members are shared also gets past the
new skb_cloned(skb2) && skb_has_frag_list(skb2) check. Its members are
then linked into the head's chain, and later appends write their ->next.
When the other owner frees its skb, can kfree_skb_list() follow the
modified ->next into skb2? By then skb2 may already be freed, or
consumed by ESP's skb_cow_data(). The result would be a use-after-free,
a double free, or the same skb_over_panic described in the commit
message.
For comparison, __pskb_pull_tail() forks shared list members with
skb_clone(), and skb_cow_data() copies them.
Could the code refuse to aggregate onto, or re-home, any frag_list with
an skb_shared() member, whether or not the head is cloned? Or could the
clone/frag_list decision be made before skb_checksum_help() can unclone
the head?
[Severity: Low]
The commit message says:
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.
Is that accurate for the local TCP case this patch targets?
skb_unclone() here does pskb_expand_head(skb, 0, 0), which adds no
tailroom. The following skb2s are TCP clones, so share_ok becomes false
and they go onto the frag_list.
In esp_output_head() the head is no longer cloned. It still has a
frag_list, though, so this check fails and it goes to cow:
} else if ((skb_shinfo(skb)->nr_frags < MAX_SKB_FRAGS)
&& !skb_has_frag_list(skb)) {
skb_cow_data() then sees nr_frags != 0:
if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
!__pskb_pull_tail(skb, __skb_pagelen(skb)))
If the pulled data does not fit in the tailroom, __pskb_pull_tail() does
a second pskb_expand_head(). Before the patch, the cloned head needed
only one pskb_expand_head(skb, 0, eat + 128) inside __pskb_pull_tail().
Doesn't this add a head reallocation and copy for each aggregated outer
packet? When nothing is aggregated, the uncloned head can take ESP's
page-frag path and a copy is saved instead. Either way, the cost depends
on the traffic.
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/b56bbced-a8b0-4ae9-80b4-1958ea36684c%40mid.andrii.net
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-22 1:38 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <b56bbced-a8b0-4ae9-80b4-1958ea36684c@mid.andrii.net>
2026-11-18 0:54 ` [PATCH ipsec 1/2] xfrm: iptfs: don't modify the shared info of cloned skbs on output Andrii Tereshchenko
2026-11-22 1:38 ` netdev-bot+sashiko
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;
as well as URLs for NNTP newsgroup(s).