Netdev List
 help / color / mirror / Atom feed
* [PATCH] ipv6: reject datagrams that leave no room past the fragment header
@ 2026-11-21  6:40 Dai Wenjie
  2026-11-21  6:43 ` netdev-bot+sinfo
  0 siblings, 1 reply; 3+ messages in thread
From: Dai Wenjie @ 2026-11-21  6:40 UTC (permalink / raw)
  To: netdev
  Cc: David S . Miller, Jakub Kicinski, Eric Dumazet, Paolo Abeni,
	Dai Wenjie

__ip6_append_data() does not check that fragmenting can make progress.
When the MTU leaves only 8..15 bytes past fragheaderlen, maxfraglen
collapses to fragheaderlen and the clamp below reduces datalen to zero:

    if (datalen > (cork->length <= mtu ? mtu : maxfraglen) - fragheaderlen)
            datalen = maxfraglen - fragheaderlen - rt->dst.trailer_len;

The paged allocation path (MSG_SPLICE_PAGES, MSG_ZEROCOPY) then computes

    pagedlen = datalen - transhdrlen - fraggap     (underflows)
    copy     = datalen - transhdrlen - fraggap - pagedlen

where the two underflows cancel and copy evaluates to zero, while
skb_put(skb, fraglen - pagedlen) wraps back to a valid size.  length -=
copy + transhdrlen therefore consumes nothing, and the loop re-enters
alloc_new_skb, allocating one skb per iteration until the socket wmem
budget (2 * sk_sndbuf) is exhausted; it then fails with -ENOBUFS and
ip6_flush_pending_frames() drops the queued skbs.

A 64-byte UDPv6 datagram with IPV6_MTU=1280 and a 1232-byte routing
header (fragheaderlen 1272) reproduces this on 6.12.96: with SO_SNDBUF
set to 128M, splice() spends 152 ms on-CPU and allocates 514 MB of
transient kernel memory before returning ENOBUFS.

The non-paged path is not affected: pagedlen is zero there and the
existing copy < 0 check returns -EINVAL.

Reject the datagram with -EMSGSIZE when the room left after the
fragment headers cannot hold rt->dst.trailer_len followed by
transhdrlen and fraggap.  The arithmetic stays unsigned throughout:
maxfraglen, fragheaderlen and fraggap are unsigned, and the subtractions
are guarded so that neither maxfraglen - fragheaderlen nor
room - trailer can wrap.

The check has to sit inside the clamp instead of next to the earlier
maxfraglen validation: a zero-payload datagram of exactly mtu bytes
fits in a single packet, is never clamped, and must keep working.

Signed-off-by: Dai Wenjie <3596417740@qq.com>
---
 net/ipv6/ip6_output.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c
index 50b41be5d..de033a35e 100644
--- a/net/ipv6/ip6_output.c
+++ b/net/ipv6/ip6_output.c
@@ -1610,8 +1610,18 @@ static int __ip6_append_data(struct sock *sk,
 			 */
 			datalen = length + fraggap;
 
-			if (datalen > (cork->length <= mtu ? mtu : maxfraglen) - fragheaderlen)
-				datalen = maxfraglen - fragheaderlen - rt->dst.trailer_len;
+			if (datalen > (cork->length <= mtu ? mtu : maxfraglen) - fragheaderlen) {
+				unsigned int need = (unsigned int)transhdrlen + fraggap;
+				unsigned int room = maxfraglen - fragheaderlen;
+				unsigned int trailer = (unsigned int)rt->dst.trailer_len;
+
+				if (maxfraglen <= fragheaderlen || room <= trailer ||
+				    room - trailer < need) {
+					err = -EMSGSIZE;
+					goto error;
+				}
+				datalen = room - trailer;
+			}
 			fraglen = datalen + fragheaderlen;
 			pagedlen = 0;
 
-- 
2.43.0


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

end of thread, other threads:[~2026-11-21  6:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-21  6:40 [PATCH] ipv6: reject datagrams that leave no room past the fragment header Dai Wenjie
2026-11-21  6:43 ` netdev-bot+sinfo
2026-11-21  6:59   ` 逐彡梦

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