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
  2026-11-25  7:00 ` netdev-bot+sashiko
  0 siblings, 2 replies; 4+ 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] 4+ messages in thread

* Re: [PATCH] ipv6: reject datagrams that leave no room past the fragment header
  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   ` 逐彡梦
  2026-11-25  7:00 ` netdev-bot+sashiko
  1 sibling, 1 reply; 4+ messages in thread
From: netdev-bot+sinfo @ 2026-11-21  6:43 UTC (permalink / raw)
  To: Dai Wenjie
  Cc: netdev, David S . Miller, Jakub Kicinski, Eric Dumazet,
	Paolo Abeni

Hi!

This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:

 - How the issue was discovered, e.g. hit in production, hit during
   development, syzbot report, manual code inspection, LLM or static
   analysis tool scan.

Please do not repost the series just to address the above. Instead,
reply to this email with the missing information, so that reviewers
can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include
the relevant info in the cover letter or commit messages.
-- 
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.

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

* Re: [PATCH] ipv6: reject datagrams that leave no room past the fragment header
  2026-11-21  6:43 ` netdev-bot+sinfo
@ 2026-11-21  6:59   ` 逐彡梦
  0 siblings, 0 replies; 4+ messages in thread
From: 逐彡梦 @ 2026-11-21  6:59 UTC (permalink / raw)
  To: netdev-bot+sinfo
  Cc: netdev, David S . Miller, Jakub Kicinski, Eric Dumazet,
	Paolo Abeni

Hi,

thanks for the triage. Answering the question directly: this was not hit
in production and is not from syzbot. It came out of a manual audit of
__ip6_append_data() that started from CVE-2026-53362 (736b380e), with LLM
assistance for the initial code reading; the arithmetic, the reproducer
and the fix were then derived and verified by hand on a real kernel.

Concretely:

- I went through the paged-allocation accounting again after the fraggap
  fix, looking for other values that can make the loop fail to make
  progress.

- The degenerate case is maxfraglen == fragheaderlen, i.e.
  mtu - fragheaderlen in [8,15]. The clamp then leaves zero capacity,
  pagedlen underflows, copy cancels back to zero, and
  length -= copy + transhdrlen consumes nothing; the loop keeps
  allocating one skb per iteration until the socket wmem budget
  (2 * sk_sndbuf) is exhausted and returns -ENOBUFS.

- Reproduced on v6.12.96 with a standalone C program: IPV6_MTU=1280 plus
  a 1232-byte routing header (fragheaderlen = 1272), then one splice()
  with SPLICE_F_MORE. With SO_SNDBUF=128M the call spends 152 ms on-CPU
  and allocates about 514 MB of transient kernel memory before returning
  ENOBUFS; with the patch it returns -EMSGSIZE in 0.03 ms without
  allocating anything.

- The patch was build-tested on v6.12.96, and three cases were verified on
  the patched kernel: the degenerate geometry (returns -EMSGSIZE), a
  zero-payload datagram of exactly mtu bytes (still sends), and a normal
  fragmentation path (unaffected). The series also applies cleanly to
  current mainline.

I can post the reproducer if that is useful.

Thanks,
Dai Wenjie



原始邮件
发件人:netdev-bot+sinfo <netdev-bot+sinfo@kernel.org>
发件时间:2026年11月21日 14:43
收件人:Dai Wenjie <3596417740@qq.com>
抄送:netdev <netdev@vger.kernel.org>, David S . Miller <davem@davemloft.net>, Jakub Kicinski <kuba@kernel.org>, Eric Dumazet <edumazet@google.com>, Paolo Abeni <pabeni@redhat.com>
主题:Re: [PATCH] ipv6: reject datagrams that leave no room past the fragment header


Hi!

This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:

 - How the issue was discovered, e.g. hit in production, hit during
   development, syzbot report, manual code inspection, LLM or static
   analysis tool scan.

Please do not repost the series just to address the above. Instead,
reply to this email with the missing information, so that reviewers
can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include
the relevant info in the cover letter or commit messages.
-- 
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.


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

* Re: [PATCH] ipv6: reject datagrams that leave no room past the fragment header
  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-25  7:00 ` netdev-bot+sashiko
  1 sibling, 0 replies; 4+ messages in thread
From: netdev-bot+sashiko @ 2026-11-25  7:00 UTC (permalink / raw)
  To: 3596417740; +Cc: netdev, davem, kuba, edumazet, pabeni

Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 2 · Low: 2

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.
- [Medium] The new rejection in __ip6_append_data() (`room <= trailer ||
  room - trailer < need`) is stricter than the non-paged path needs, and…
- [Medium] The commit message says 'The non-paged path is not affected:
  pagedlen is zero there and the existing copy < 0 check returns…
- [Low] The patch fixes a user-triggerable resource-exhaustion loop in
  __ip6_append_data() (net/ipv6/ip6_output.c) that the author reproduced…
- [Low] The new -EMSGSIZE rejection in __ip6_append_data() exits through
  the generic `error:` label, not `emsgsize:`.

--

ipv6: reject datagrams that leave no room past the fragment header

This patch adds a check inside the datalen clamp in __ip6_append_data().
The check returns -EMSGSIZE when the space left after the fragment headers
cannot hold the trailer plus transhdrlen and fraggap. The goal is to stop
the paged allocation path from looping through alloc_new_skb without
making progress until the socket wmem budget runs out.

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

[Severity: Medium]
Does this hold when transhdrlen and fraggap are both zero?

rawv6_sendmsg() calls ip6_append_data() with transhdrlen == 0:

net/ipv6/raw.c:rawv6_sendmsg() {
    ...
	err = ip6_append_data(sk, raw6_getfrag, &rfv,
		len, 0, &ipc6, &fl6, dst_rt6_info(dst),
		msg->msg_flags);
    ...
}

Take fragheaderlen = 1272 and mtu = 1280. The clamp sets datalen to 0 and
the non-paged branch computes copy = 0 - 0 - 0 - 0 = 0, so the copy < 0
check does not fire. alloc_skb() then runs under the 2 * sk_sndbuf gate,
and length -= 0.

On the next pass, copy = 1280 - 1272 = 8, which is less than length. So
copy = maxfraglen - skb->len = 0, and the code goes back to
alloc_new_skb. This repeats until -ENOBUFS.

Doesn't that mean any CAP_NET_RAW holder, including one inside a user
namespace, can reach the loop without MSG_SPLICE_PAGES or MSG_ZEROCOPY?

The new check does stop this case. The commit message also doesn't say
that UDP and ping sends with this geometry on the non-paged path now get
-EMSGSIZE instead of -EINVAL, which userspace can see.

> Signed-off-by: Dai Wenjie <3596417740@qq.com>

[Severity: Low]
This fixes a loop that userspace can trigger, and it was reproduced on
6.12.96. Should it carry a Fixes: tag and Cc: stable?

The paged-branch arithmetic quoted in the message
(pagedlen = datalen - transhdrlen - fraggap) comes from 736b380e28d0
("ipv6: account for fraggap on the paged allocation path"). The clamp line
comes from 0c1833797a5a and e57a34478586. The raw socket
(transhdrlen == 0) version of the loop is older than both.

> diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c
> index cb648c8394310..b9887b9d60a39 100644
> --- a/net/ipv6/ip6_output.c
> +++ b/net/ipv6/ip6_output.c
> @@ -1651,8 +1651,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) {

[Severity: High]
Can this branch condition still wrap, so the new guard never runs?

The early check in __ip6_append_data() only rejects mtu <= fragheaderlen:

	if (mtu <= fragheaderlen ||
	    ((mtu - fragheaderlen) & ~7) + fragheaderlen <= sizeof(struct frag_hdr))
		goto emsgsize;

	maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen -
		     sizeof(struct frag_hdr);

When mtu = fragheaderlen + k with k in 1..7, maxfraglen ends up as
fragheaderlen - 8. If cork->length > mtu, the maxfraglen - fragheaderlen
in the condition above wraps to 0xfffffff8. The condition is then false,
so the maxfraglen <= fragheaderlen test below never runs.

Without the clamp, both the paged path (MSG_SPLICE_PAGES) and the
non-paged SG large-alloc path do this:

	alloclen = fragheaderlen + transhdrlen + fraggap;
	pagedlen = datalen - transhdrlen - fraggap;
	...
	copy = datalen - transhdrlen - fraggap - pagedlen;

The result:

- copy is 0 on every pass.
- On later passes, copy = maxfraglen - skb->len is negative, so the code
  goes back to alloc_new_skb.
- fraggap grows by 8 each time and length never decreases.
- pskb_trim_unique(skb_prev, maxfraglen) cuts each previous skb below
  fragheaderlen.

The loop only ends when the 2 * sk_sndbuf gate returns -ENOBUFS.

One way to reach this is l2tp_ip6_sendmsg(). It passes transhdrlen = 4,
ip_generic_getfrag and msg->msg_flags, so splice() sets MSG_SPLICE_PAGES.
Set IPV6_MTU to 1284..1287 and add an unprivileged type 4 IPV6_RTHDR so
that fragheaderlen = 1280. Then headersize + transhdrlen <= mtu passes for
k = 4..7, and a datagram larger than mtu gives cork->length > mtu.

rawv6_sendmsg() passes transhdrlen = 0, so k = 1..7 works there. That
needs an SG device and a send length that forces
fraglen + alloc_extra >= SKB_MAX_ALLOC.

ip6_append_data_mtu() also recomputes maxfraglen for the first skb (xfrm
transport mode) and does not compare it with fragheaderlen again:

	*mtu = orig_mtu - rt->dst.header_len;
	...
	*maxfraglen = ((*mtu - fragheaderlen) & ~7)
		      + fragheaderlen - sizeof(struct frag_hdr);

The commit message says:

    the subtractions are guarded so that neither maxfraglen - fragheaderlen
    nor room - trailer can wrap.

The copy of that subtraction in the branch condition is not guarded,
though.

Would it be better to reject maxfraglen <= fragheaderlen, together with
the trailer and transhdrlen room check, before this condition? That would
be right after maxfraglen is computed and after ip6_append_data_mtu().
Another option is to evaluate the condition without unsigned wrap.

There may also be a worse case that I have not verified. With MSG_MORE on
a non-SG device, alloclen = mtu while the unclamped fraglen is
length + fragheaderlen. Could skb_put() hit skb_over_panic() in that case?

> +				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) {

[Severity: Medium]
Is this bound stricter than the non-paged path needs?

On the non-paged path, after the clamp, the existing code adds the
trailer back for every fragment except the last one:

	if (datalen != length + fraggap) {
		/*
		 * this is not the last fragment, the trailer
		 * space is regarded as data space.
		 */
		datalen += rt->dst.trailer_len;
	}
	...
	copy = datalen - transhdrlen - fraggap - pagedlen;

So datalen ends up as room and copy = room - need. alloclen already
includes trailer_len through alloc_extra, so skb_put() of maxfraglen
fits.

Before this patch, any non-paged send with room >= need fragmented
correctly. When room < trailer, the add-back undid the unsigned datalen
wrap.

With this patch, routes with a nonzero dst.trailer_len, such as an ESP
xfrm dst, fail with -EMSGSIZE when need <= room < need + trailer. For
example, take an ESP GCM trailer of 21, room of 16 and a UDP need of 8.
That used to send 8 bytes in each first fragment and is now rejected.
Raw sends (need == 0) with 0 < room <= trailer fail the same way.

Only the paged path, where pagedlen = room - trailer - need can
underflow, needs the stricter bound. Could the trailer part of the check
apply only there? The commit message also doesn't mention this change in
behaviour.

> +					err = -EMSGSIZE;
> +					goto error;

[Severity: Low]
Should this rejection also report through the error queue?

Every other MTU and header sizing rejection in __ip6_append_data() goes
to the emsgsize label:

emsgsize:
	pmtu = max_t(int, mtu - headersize + sizeof(struct ipv6hdr), 0);
	ipv6_local_error(sk, EMSGSIZE, fl6, pmtu);
	return -EMSGSIZE;

The error label never queues an error-queue entry, and it increments
IPSTATS_MIB_OUTDISCARDS:

error:
	net_zcopy_put_abort(uarg, extra_uref);
	cork->length -= length;
	IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);

So an IPV6_RECVERR application gets EMSGSIZE from sendmsg() or splice()
but finds nothing on MSG_ERRQUEUE for this one failure. The drop is also
counted in OUTDISCARDS.

Jumping straight to emsgsize from inside the loop would skip the
cork->length and uarg cleanup. Would calling ipv6_local_error()
explicitly before the goto error keep this consistent with the other
sites?

> +				}
> +				datalen = room - trailer;
> +			}

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/tencent_BE8DC9842FFF55349D2FCCDE456B9C6EB709%40qq.com

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

end of thread, other threads:[~2026-11-25  7:00 UTC | newest]

Thread overview: 4+ 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   ` 逐彡梦
2026-11-25  7:00 ` netdev-bot+sashiko

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