xfrm: iptfs: make room before copying a continuation in reassembly
Subsystems (4)
PATCH 1/1 xfrm: iptfs: make room before copying a continuation in reassembly
When iptfs_reassem_cont() cannot share a continuation's data as page frags, it linearizes the in-progress skb and copies the data in with skb_put(). The tailroom for that copy is never checked.
An in-progress skb started by iptfs_pskb_add_frags() has only the inner IP header in its linear area; the rest of the first piece is attached as page frags. Linearizing it with skb_linearize() grows the head only by what the frags need plus 128 bytes, so the following skb_put(newskb, copylen) runs past the end as soon as copylen is larger than that slack, and the kernel panics in softirq.
The copy path is taken whenever a continuation cannot be shared: the outer skb has a frag_list, has a kmalloc'd head with linear data, has a different pp_recycle, or the in-progress skb has used up MAX_SKB_FRAGS. So it is enough for the first piece of an inner packet to arrive in a shareable outer skb and the next piece in one that is not.
Commit 0b352f83cabf ("xfrm: iptfs: fix skb_put() panic on non-linear skb during reassembly") added the skb_linearize() on the assumption that the in-progress skb was allocated with room for the whole inner packet. That holds for skbs from iptfs_pskb_extract_seq() but not for those from iptfs_pskb_add_frags(). The netdev AI review of "xfrm: iptfs:
fix runt reassembly panic from short inner tot_len" asked about this missing check, and it was left for a follow-up.
Piyus Kedia reported the same overrun and posted a fix that bounds the copy by the tailroom and abandons the reassembly when it does not fit. That stops the panic, but the inner packet, which is valid, is lost. Making room delivers it.
Make room before linearizing, in one reallocation, for the frag data that skb_linearize() pulls in and for the rest of the inner packet, so that skb_linearize() itself allocates nothing; abandon the reassembly and count XfrmInBufferError if that fails. Sizing for ipremain rather than copylen means later copies of the same packet need no further expansion. The room is measured as end minus tail, because skb_tailroom() is 0 for the non-linear skb. The reallocation is __GFP_NOWARN: a peer chooses how large it is, and its failure is handled.
skbuff: skb_over_panic: ... len:2884 put:1442 ... end:0x6c0 dev:eth1 kernel BUG at net/core/skbuff.c:215!
CPU: 2 UID: 0 PID: 0 Comm: swapper/2 Not tainted 7.4.0-rc3 #1 Call Trace: <IRQ> skb_put.cold+0x1f/0x1f iptfs_reassem_cont+0x242/0x760 iptfs_input_ordered+0x382/0x3e0 iptfs_input+0x24d/0x630 xfrm_input+0x12a5/0x29f0 xfrm4_esp_rcv+0x4d/0x90 ip_protocol_deliver_rcu+0x1d2/0x1e0 ip_local_deliver_finish+0x13c/0x210 ip_local_deliver+0xf9/0x1d0
An AI code review found this. It reproduces without fault injection on unmodified kernels: on v7.4-rc3 when a packet socket on the receiver clones every second ESP packet, as ESP input gives each cloned packet a kmalloc'd head, and at ipsec commit f0e68014f4ac ("xfrm: hold migrated states through rollback") in a selftest written for it. With this patch both run clean, and failslab on the new allocation only drops the packet. No selftest in the tree covers IP-TFS. xfrm_iptfs.o builds without warnings with W=1 under allmodconfig and allyesconfig.
The analysis and the patch were produced with an AI coding assistant and reviewed by the submitter.
---
I sent this privately to the IPsec maintainers and [email protected] on 2026-11-20, before I had seen Piyus Kedia's patch. Changed since:
the credit to him and the paragraph comparing the two fixes, the subject of the runt fix, the build sentence, and the stable address. The code is the same.
On ipsec master with Piyus Kedia's patch, the selftest no longer panics but fails: one of its two inner packets is delivered. With this patch both are. The selftest is not posted, as Documentation/process/security-bugs.rst asks for a bug found with AI; the IPsec maintainers have it.
More fixes for the IP-TFS receive path follow as a separate series.
net/xfrm/xfrm_iptfs.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
net/xfrm/xfrm_iptfs.c +12 -1
@@ -909,7 +909,18 @@ static u32 iptfs_reassem_cont(struct xfrm_iptfs_data *xtfs, u64 seq, iptfs_skb_can_add_frags(newskb, fragwalk, data, copylen)) { iptfs_skb_add_frags(newskb, fragwalk, data, copylen); } else { -if (skb_linearize (newskb)) { +/* A newskb started by iptfs_pskb_add_frags() holds only the +* inner IP header in its linear area. Make room, in one +* reallocation, for the frag data that linearizing pulls in +* and for the rest of the inner packet. +*/ +int room =skb_end_pointer (newskb) -skb_tail_pointer (newskb); +int need = (int )(newskb->data_len + ipremain) - room; + +if ((need >0 && +pskb_expand_head (newskb,0 , need, +GFP_ATOMIC | __GFP_NOWARN)) || +skb_linearize (newskb)) {XFRM_INC_STATS (xs_net (xtfs->x),LINUX_MIB_XFRMINBUFFERERROR ); goto abandon;
base-commit: f0e68014f4ac1653b2980ab76744f4dfdffdbac5
Signature · 1 line
2.53.0
Quoted 64 lines · Andrii Tereshchenko
When
iptfs_reassem_cont()cannot share a continuation's data as page frags, it linearizes the in-progress skb and copies the data in withskb_put(). The tailroom for that copy is never checked.An in-progress skb started by
iptfs_pskb_add_frags()has only the inner IP header in its linear area; the rest of the first piece is attached as page frags. Linearizing it withskb_linearize()grows the head only by what the frags need plus 128 bytes, so the followingskb_put(newskb, copylen)runs past the end as soon as copylen is larger than that slack, and the kernel panics in softirq.The copy path is taken whenever a continuation cannot be shared: the outer skb has a
frag_list, has a kmalloc'd head with linear data, has a differentpp_recycle, or the in-progress skb has used upMAX_SKB_FRAGS. So it is enough for the first piece of an inner packet to arrive in a shareable outer skb and the next piece in one that is not.Commit
0b352f83cabf("xfrm: iptfs: fixskb_put()panic on non-linear skb during reassembly") added theskb_linearize()on the assumption that the in-progress skb was allocated with room for the whole inner packet. That holds for skbs fromiptfs_pskb_extract_seq()but not for those fromiptfs_pskb_add_frags(). The netdev AI review of "xfrm: iptfs:
fix runt reassembly panic from short innertot_len" asked about this missing check, and it was left for a follow-up.Piyus Kedia reported the same overrun and posted a fix that bounds the copy by the tailroom and abandons the reassembly when it does not fit. That stops the panic, but the inner packet, which is valid, is lost. Making room delivers it.
Make room before linearizing, in one reallocation, for the frag data that
skb_linearize()pulls in and for the rest of the inner packet, so thatskb_linearize()itself allocates nothing; abandon the reassembly and count XfrmInBufferError if that fails. Sizing for ipremain rather than copylen means later copies of the same packet need no further expansion. The room is measured as end minus tail, becauseskb_tailroom()is 0 for the non-linear skb. The reallocation is__GFP_NOWARN: a peer chooses how large it is, and its failure is handled.skbuff: skb_over_panic: ... len:2884 put:1442 ... end:0x6c0 dev:eth1 kernel BUG at net/core/skbuff.c:215! CPU: 2 UID: 0 PID: 0 Comm: swapper/2 Not tainted 7.4.0-rc3 #1 Call Trace: <IRQ> skb_put.cold+0x1f/0x1f iptfs_reassem_cont+0x242/0x760 iptfs_input_ordered+0x382/0x3e0 iptfs_input+0x24d/0x630 xfrm_input+0x12a5/0x29f0 xfrm4_esp_rcv+0x4d/0x90 ip_protocol_deliver_rcu+0x1d2/0x1e0 ip_local_deliver_finish+0x13c/0x210 ip_local_deliver+0xf9/0x1d0An AI code review found this. It reproduces without fault injection on unmodified kernels: on v7.4-rc3 when a packet socket on the receiver clones every second ESP packet, as ESP input gives each cloned packet a kmalloc'd head, and at ipsec commit
f0e68014f4ac("xfrm: hold migrated states through rollback") in a selftest written for it. With this patch both run clean, and failslab on the new allocation only drops the packet. No selftest in the tree covers IP-TFS. xfrm_iptfs.o builds without warnings with W=1 under allmodconfig and allyesconfig.
Quoted 2 lines
The analysis and the patch were produced with an AI coding assistant and reviewed by the submitter.
This commit message is too verbose IMHO. It is quite hard to read and understand.
Quoted 39 lines
Fixes 5f2b6a909574 ("xfrm: iptfs: add skb-fragment sharing code")Fixes 0b352f83cabf ("xfrm: iptfs: fix skb_put() panic on non-linear skb during reassembly")Reported-by Piyus Kedia <[email protected]>Signed-off-by Andrii Tereshchenko <[email protected]>---
I sent this privately to the IPsec maintainers and [email protected] on 2026-11-20, before I had seen Piyus Kedia's patch. Changed since:
the credit to him and the paragraph comparing the two fixes, the subject of the runt fix, the build sentence, and the stable address. The code is the same.On ipsec master with Piyus Kedia's patch, the selftest no longer panics but fails: one of its two inner packets is delivered. With this patch both are. The selftest is not posted, as
Documentation/process/security-bugs.rstasks for a bug found with AI; the IPsec maintainers have it.More fixes for the IP-TFS receive path follow as a separate series.
net/xfrm/xfrm_iptfs.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)net/xfrm/xfrm_iptfs.c +4
-1@@ -909,7 +909,18 @@ static u32 iptfs_reassem_cont(struct xfrm_iptfs_data *xtfs, u64 seq, iptfs_skb_can_add_frags(newskb, fragwalk, data, copylen)) { iptfs_skb_add_frags(newskb, fragwalk, data, copylen); } else { -if (skb_linearize (newskb)) { +/* A newskb started by iptfs_pskb_add_frags() holds only the +* inner IP header in its linear area. Make room, in one +* reallocation, for the frag data that linearizing pulls in +* and for the rest of the inner packet.
net/xfrm/xfrm_iptfs.c static u32 iptfs_reassem_cont(struct xfrm_iptfs_data *xtfs, u64 seq, · line 925
Quoted 10 lines
net/xfrm/xfrm_iptfs.c +8
-0+*/ +int room =skb_end_pointer (newskb) -skb_tail_pointer (newskb); +int need = (int )(newskb->data_len + ipremain) - room; + +if ((need >0 && +pskb_expand_head (newskb,0 , need, +GFP_ATOMIC | __GFP_NOWARN)) || +skb_linearize (newskb)) {XFRM_INC_STATS (xs_net (xtfs->x),LINUX_MIB_XFRMINBUFFERERROR );
Manually accounting room and need allow us to perform a single allocation but I do wonder, is it necessary? This is a very specific case.. maybe we can just call skb_linearize() first and then check the if there is enough tailroom and perform pskb_expand_head() if needed.
Sure, that will mean two different allocations in the worst scenario. But I do wonder if that is just fine for such case.
Thanks,
Fernando.
Quoted 3 lines
net/xfrm/xfrm_iptfs.c +0
-0goto abandon;base-commit:
f0e68014f4ac1653b2980ab76744f4dfdffdbac5
Quoted 2 lines · Fernando Fernandez Mancera
This commit message is too verbose IMHO. It is quite hard to read and understand.
Agreed - I will shorten it.
Quoted 4 lines
Manually accounting room and need allow us to perform a single allocation but I do wonder, is it necessary? This is a very specific case.. maybe we can just call
skb_linearize()first and then check the if there is enough tailroom and performpskb_expand_head()if needed.
Agreed. In my tests regular traffic (HTTP, ssh, scp, ping, UDP on six NIC drivers) never took the second allocation; only packets arranged for it do. There it costs one more reallocation and copy, but the code reads better. The first version of this fix did what you describe. For v2:
net/xfrm/xfrm_iptfs.c +9 -1
@@ -909,7 +909,15 @@ static u32 iptfs_reassem_cont(struct xfrm_iptfs_data *xtfs, u64 seq, iptfs_skb_can_add_frags(newskb, fragwalk, data, copylen)) { iptfs_skb_add_frags(newskb, fragwalk, data, copylen); } else { -if (skb_linearize (newskb)) { +/* A newskb started by iptfs_pskb_add_frags() has little +* tailroom after linearizing. Make room for the rest of +* the inner packet before copying. +*/ +if (skb_linearize (newskb) || + (skb_tailroom (newskb) < ipremain && +pskb_expand_head (newskb,0 , + ipremain -skb_tailroom (newskb), +GFP_ATOMIC | __GFP_NOWARN))) {XFRM_INC_STATS (xs_net (xtfs->x),LINUX_MIB_XFRMINBUFFERERROR ); goto abandon;
Thanks for the review.