Netdev List
 help / color / mirror / Atom feed
* [PATCH] xfrm: iptfs: bound the reassembly copy by the buffer it copies into
@ 2026-11-19 14:23 Piyus Kedia
  2026-11-19 14:28 ` netdev-bot+sinfo
  0 siblings, 1 reply; 2+ messages in thread
From: Piyus Kedia @ 2026-11-19 14:23 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu, David S . Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Christian Hopps
  Cc: netdev, linux-kernel

iptfs_reassem_cont() takes the length to copy from the inner packet's own
IP total-length field:

	ipremain = __iptfs_iplen(newskb->data);
	...
	ipremain -= newskb->len;
	...
	copylen = min(fraglen, ipremain);

and then puts that into the reassembly buffer:

	if (skb_copy_seq_read(st, data, skb_put(newskb, copylen), copylen))

Nothing ties that claimed length to the size of newskb on the path this
takes, although the file states that it must.  iptfs_input_ordered() has
three ways to obtain the skb that becomes xtfs->ra_newskb.  The reuse path
checks the requirement and its comment gives the rule:

	} else if (data + iphlen <= skb_headlen(skb) &&
		   skb_tailroom(skb) + tail - data >= iplen) {
		/* Reuse the received skb.
		 ...
		 * For linear buffer space we only require that
		 * linear buffer space is large enough to
		 * eventually hold the entire reassembled
		 * packet (by including tailroom in the check).
		 */

The fallback allocates for it outright, iptfs_pskb_extract_seq(iplen, ...).
The third does neither: iptfs_pskb_add_frags() is preferred over the
allocating one and builds an skb whose capacity reflects capturelen, the
bytes that arrived, not iplen.  Its result becomes the reassembly buffer
just the same:

	if (skb->len < iplen) {
		/* Start reassembly */
		...
		xtfs->ra_newskb = skb;

So when the sharing path wins, the buffer carries no guarantee about iplen
and the invariant the reuse path enforces is simply absent.  A later
continuation then computes copylen from a header field that can be much
larger than the buffer, and skb_put() walks off the end of it.  skb_linearize() flattens the fragments already present;
it does not grow the tailroom to whatever the header asked for.
skb_over_panic() catches the overrun and BUG()s:

  skbuff: skb_over_panic: text:... len:4000 put:3000 head:... data:...
                          tail:0xff0 end:0x6c0 dev:tap0
  kernel BUG at net/core/skbuff.c:214!
  Oops: invalid opcode: 0000 [#1] SMP KASAN PTI
  RIP: 0010:skb_over_panic+0xcd/0xd0 net/core/skbuff.c:219
  Call Trace:
   skb_put+0xd4/0xe0 net/core/skbuff.c:2641
   iptfs_reassem_cont+0xa30/0xb30 net/xfrm/xfrm_iptfs.c:920
   iptfs_input_ordered+0x479/0x4f0 net/xfrm/xfrm_iptfs.c:1288
   iptfs_input+0x161/0x2b0 net/xfrm/xfrm_iptfs.c:1733
   xfrm_inner_mode_input+0xe0/0x190 net/xfrm/xfrm_input.c:453
   xfrm_input+0xac9/0x1320 net/xfrm/xfrm_input.c:705
   xfrmi_input+0x13a/0x170 net/xfrm/xfrm_interface_core.c:331
   xfrm4_esp_rcv+0x7f/0x1e0 net/ipv4/xfrm4_protocol.c:104
   ip_protocol_deliver_rcu+0x261/0x380 net/ipv4/ip_input.c:207
   ...

tail is 4080 against an end of 1728: a 2352-byte overrun of a buffer sized
for what arrived rather than for what the header claimed.

skb_put() tests tail against end before returning the pointer, and BUG()
does not return, so the copy never runs and nothing is written out of
bounds - this is a reachable BUG()/oops rather than memory corruption.  It
is reached from xfrm_input(), i.e. after ESP processing, so it needs a peer
that can produce packets the SA accepts.

Note that skb->tail and skb->len are already incremented when the check
fires, and the abandon path is not taken, so without panic_on_oops the
task is killed while xtfs->ra_newskb is left pointing at an skb whose tail
is past its end and whose len exceeds its buffer, still reachable from the
SA.

Reproduced deterministically: eighteen payloads on one SA - one declaring
3800 bytes with 1000 carried, sixteen 100-byte continuations that fill the
fragment array to MAX_SKB_FRAGS so iptfs_skb_can_add_frags() must fail, and
one declaring block_offset 1200.  skb_linearize() then pulls into a
kmalloc-4096 buffer (SKB_WITH_OVERHEAD 3776, 80 bytes of headroom, len 2600,
tailroom 1096) and skb_put(1200) is 104 bytes past the end, which is the
len:3800 put:1200 tail:0xf28 end:0xec0 above.  The same values appear on
every run.

Bound both copies by the tailroom actually available and abandon the
reassembly otherwise, which is how the function already handles a stream it
cannot complete.  This restores at the point of use the invariant the reuse
path states, without disabling the sharing optimisation.  The second check has to come after skb_linearize(), since
that may reallocate and so change the tailroom the copy has to fit in.  The
comparison is made signed because copylen is u32 while skb_tailroom()
returns int: promoting a negative tailroom to a large unsigned would defeat
the check in exactly the leftover state described above.

Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
---
--- a/net/xfrm/xfrm_iptfs.c
+++ b/net/xfrm/xfrm_iptfs.c
@@ -875,6 +875,20 @@
 		}
 		fraglen = min(blkoff, remaining);
 		copylen = min(fraglen, iphremain);
+		/* copylen is bounded only by the length the inner header
+		 * claims, which says nothing about the size of the buffer being
+		 * reassembled into: iptfs_input_ordered() prefers
+		 * iptfs_pskb_add_frags(), whose capacity is the bytes that
+		 * arrived, over iptfs_pskb_extract_seq(), which allocates the
+		 * claimed length. Cast because copylen is u32 and skb_tailroom()
+		 * returns int, so the comparison must not promote a negative
+		 * tailroom into a large unsigned.
+		 */
+		if (skb_tailroom(newskb) < (int)copylen) {
+			XFRM_INC_STATS(xs_net(xtfs->x),
+				       LINUX_MIB_XFRMINBUFFERERROR);
+			goto abandon;
+		}
 		if (skb_copy_seq_read(st, data, skb_put(newskb, copylen),
 				      copylen)) {
 			XFRM_INC_STATS(xs_net(xtfs->x),
@@ -913,6 +927,15 @@
 			XFRM_INC_STATS(xs_net(xtfs->x),
 				       LINUX_MIB_XFRMINBUFFERERROR);
 			goto abandon;
+		}
+
+		/* Re-checked after skb_linearize(), which may reallocate and so
+		 * change the tailroom this copy has to fit in.
+		 */
+		if (skb_tailroom(newskb) < (int)copylen) {
+			XFRM_INC_STATS(xs_net(xtfs->x),
+				       LINUX_MIB_XFRMINBUFFERERROR);
+			goto abandon;
 		}
 
 		/* copy fragment data into newskb */
-- 
2.43.0

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

end of thread, other threads:[~2026-11-19 14:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 14:23 [PATCH] xfrm: iptfs: bound the reassembly copy by the buffer it copies into Piyus Kedia
2026-11-19 14:28 ` netdev-bot+sinfo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox