Netdev List
 help / color / mirror / Atom feed
* [PATCH net] ipv4: reject header length beyond linear data in ip_do_fragment()
@ 2026-11-26  6:43 Lin Jiapeng
  2026-11-27  6:48 ` netdev-bot+sashiko
  0 siblings, 1 reply; 2+ messages in thread
From: Lin Jiapeng @ 2026-11-26  6:43 UTC (permalink / raw)
  To: dsahern, idosch, davem, edumazet, kuba, pabeni, horms, netdev
  Cc: Lin Jiapeng, TencentOS Corvus AI

From: Lin Jiapeng <jiapenglin@tencent.com>

ip_do_fragment() splits an oversized IPv4 datagram into link-MTU-sized
fragments. It reads the header length from iph->ihl and hands the packet
to ip_frag_init()/ip_frag_next(), which assume the whole IPv4 header is
still present in the skb's linear area.

Callers that strip or rebase link-layer headers before fragmenting can
break that assumption. Open vSwitch prepare_frag() pulls the L2 stack
(up to MAX_L2_LEN = 30 bytes) off the skb after a TRUNC action has cut
the frame, so the remaining linear data can be shorter than ihl * 4.
ip_frag_init() then computes state->left = skb->len - hlen as an
unsigned underflow, and ip_frag_next() fails its skb_copy_bits() bounds
check and executes BUG(), turning a single unprivileged OVS execute
request (user+network namespace, PUSH_MPLS x4 + TRUNC + MRU) into a
deterministic kernel panic under a KASAN build:
[   23.501259] kernel BUG at net/ipv4/ip_output.c:725!
[   23.503346] CPU: 3 UID: 1000 PID: 110 Comm: poc Not tainted 7.4.0-rc4-ovs-hlen-ab #1 PREEMPT(lazy)
[   23.504370] RIP: 0010:ip_frag_next+0xa71/0xb10
[   23.533285] Call Trace:
[   23.534004]  ip_do_fragment+0xdf1/0x1a40
[   23.534004]  ovs_fragment+0x6d0/0x850
[   23.534004]  do_execute_actions+0x14a8/0x1660
[   23.534004]  ovs_execute_actions+0x64/0x1e0
[   23.534004]  ovs_packet_cmd_execute+0x2b8/0x300
[   23.534004] Kernel panic - not syncing: Fatal exception in interrupt

Validate the IPv4 header length against skb_headlen() before any use of
hlen, rejecting malformed packets with -EINVAL. Well-formed skbs always
satisfy hlen <= skb_headlen(), so normal fragmentation is unaffected;
the existing undersized-MTU check keeps handling mtu < hlen + 8 after
the header itself is known to be present.

Closes: https://lore.kernel.org/netdev/20260922030310.8684-3-habte.yibelo@gmail.com/
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Reported-by: TencentOS Corvus AI <corvus@tencent.com>
Signed-off-by: Lin Jiapeng <jiapenglin@tencent.com>
---

Note: this overlaps the still-unmerged hlen half of Paulos Yibelo's
[PATCH net v6 2/2] (20260922030310.8684-3-habte.yibelo@gmail.com).
The CHECKSUM_PARTIAL equivalent logic already landed as 6785011f8b16;
the hlen guard did not. Verified with an unprivileged OVS
TRUNC+PUSH_MPLS reproducer (3/3 panic on A, clean on B with -EINVAL
at the guard).

 net/ipv4/ip_output.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/net/ipv4/ip_output.c b/net/ipv4/ip_output.c
index d42d776af273..d5ea596724d7 100644
--- a/net/ipv4/ip_output.c
+++ b/net/ipv4/ip_output.c
@@ -784,6 +784,11 @@ int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
 	 */
 
 	iph = ip_hdr(skb);
+	hlen = iph->ihl * 4;
+	if (unlikely(hlen < sizeof(*iph) || hlen > skb_headlen(skb))) {
+		err = -EINVAL;
+		goto fail;
+	}
 
 	mtu = ip_skb_dst_mtu(sk, skb);
 	if (IPCB(skb)->frag_max_size && IPCB(skb)->frag_max_size < mtu)
@@ -793,7 +798,6 @@ int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
 	 *	Setup starting values.
 	 */
 
-	hlen = iph->ihl * 4;
 	if (mtu < hlen + 8) {
 		err = -EMSGSIZE;
 		goto fail;

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

* Re: [PATCH net] ipv4: reject header length beyond linear data in ip_do_fragment()
  2026-11-26  6:43 [PATCH net] ipv4: reject header length beyond linear data in ip_do_fragment() Lin Jiapeng
@ 2026-11-27  6:48 ` netdev-bot+sashiko
  0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27  6:48 UTC (permalink / raw)
  To: ljp1205831794
  Cc: dsahern, idosch, davem, edumazet, kuba, pabeni, horms, netdev,
	jiapenglin, corvus

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

Critical: 0 · High: 1 · Medium: 1 · 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] ipv4: ip_do_fragment() uses iph->ihl before validating
  skb_headlen() and hlen
- [Medium] The new guard in ip_do_fragment()
  (net/ipv4/ip_output.c:787-791) measures against the wrong base.
- [Low] The commit message understates the impact.
- [Low] The trailers misuse tags and drop attribution.

--

ipv4: reject header length beyond linear data in ip_do_fragment()

This commit moves the hlen computation in ip_do_fragment() earlier. It
adds a check that rejects the packet with -EINVAL when the IPv4 header
length is below 20 bytes or larger than skb_headlen(). The goal is to stop
the BUG() in ip_frag_next(), which the Open vSwitch fragmentation path can
reach with PUSH_MPLS + TRUNC + MRU.

Closes: https://lore.kernel.org/netdev/20260922030310.8684-3-habte.yibelo@gmail.com/

> request (user+network namespace, PUSH_MPLS x4 + TRUNC + MRU) into a
> deterministic kernel panic under a KASAN build:

[Severity: Low]
Is "under a KASAN build" accurate? The BUG() in ip_frag_next() does not
depend on any condition:

	if (skb_copy_bits(skb, state->ptr, skb_transport_header(skb2), len))
		BUG();

The quoted trace ends with "Fatal exception in interrupt", so it fires
with BH disabled. That means it would panic on any kernel config, not only
on KASAN builds.

The message also leaves out an earlier step. Before that BUG(),
skb_copy_from_linear_data(skb, ..., hlen) in ip_frag_next() reads hlen
bytes from a linear area that is shorter than hlen, so it reads past
skb->tail.

Could the description be updated to reflect both points?

> Closes: https://lore.kernel.org/netdev/20260922030310.8684-3-habte.yibelo@gmail.com/
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Reported-by: TencentOS Corvus AI <corvus@tencent.com>
> Signed-off-by: Lin Jiapeng <jiapenglin@tencent.com>

[Severity: Low]
The Closes: link points at Paulos Yibelo's pending [PATCH net v6 2/2],
not at a bug report. Reported-by: should also be followed by a Closes:
link to that report, but here it is followed directly by Signed-off-by:.

This patch resubmits the hlen half of that series. The overlap is only
mentioned in the note below the --- line, and that note is dropped when
the patch is applied, so git history will not credit Paulos Yibelo.

For comparison, the companion commit 6785011f8b16 has
"Reported-by: Paulos Yibelo" and a Closes: link to his posting.

Should this patch credit Paulos Yibelo with Reported-by:, Suggested-by:
or Co-developed-by:? Or should it be coordinated with the pending series
instead?

> diff --git a/net/ipv4/ip_output.c b/net/ipv4/ip_output.c
> index d42d776af273e..d5ea596724d7b 100644
> --- a/net/ipv4/ip_output.c
> +++ b/net/ipv4/ip_output.c
> @@ -784,6 +784,11 @@ int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
>  	 */
>  
>  	iph = ip_hdr(skb);
> +	hlen = iph->ihl * 4;
> +	if (unlikely(hlen < sizeof(*iph) || hlen > skb_headlen(skb))) {

[Severity: High]
The commit message says the header length is validated before any use
of hlen, but the CHECKSUM_PARTIAL block above still runs first.
ip_check_csum_start() uses ip_hdrlen(skb), which is the same value as
iph->ihl * 4. skb_checksum_help() then runs on a header that has not
been validated yet.

In the OVS TRUNC + prepare_frag() case from the changelog, a
CHECKSUM_PARTIAL skb keeps its original csum_start, because
pskb_trim() does not touch ip_summed. If csum_start was at or past
network_header + hlen, ip_check_csum_start() returns 0, and
skb_checksum_help() is reached with skb_checksum_start_offset(skb) >=
skb_headlen(skb). That path calls skb_dump() and WARN_ONCE("offset
(%d) >= skb_headlen() (%u)"). With panic_on_warn, the same reproducer
still takes the machine down before the new check can reject the
packet.

The opposite case is also open. With iph->ihl < 5, ip_hdrlen() can be
0, so a csum_start equal to the network header passes
ip_check_csum_start(). skb_checksum_help() then stores the folded
checksum inside the IPv4 header itself, possibly over version/ihl,
before hlen < sizeof(*iph) is ever checked.

Separately, iph->ihl is dereferenced before anything checks
skb_headlen(skb) >= sizeof(*iph). If TRUNC leaves only the 30 bytes of
Ethernet + MPLS and prepare_frag() pulls all of them, skb_headlen() is
0 and ip_hdr(skb) points at skb->tail. The new check still rejects the
packet, but only after reading a stale or uninitialized byte past the
tail, which KMSAN would flag.

Would it make sense to do the validation ahead of the CHECKSUM_PARTIAL
block and to check skb_headlen(skb) >= sizeof(*iph) before reading
ihl? iph = ip_hdr(skb) would then need to be re-read after
skb_checksum_help(), since that call can reallocate skb->head.

[Severity: Medium]
Is skb_headlen() the right bound here? iph comes from
skb_network_header(), but skb_headlen() counts from skb->data. The check
only holds when skb_network_offset(skb) is zero, and nothing here enforces
that.

The Open vSwitch caller named in the commit message does not seem to
guarantee a zero offset. In do_output(), TRUNC only trims down to the MAC
header length (ETH_HLEN). The MPLS labels are not included in that floor:

net/openvswitch/actions.c:do_output() {
    ...
		if (unlikely(cutlen < skb->len))
			pskb_trim(skb, max(cutlen, ovs_mac_header_len(key)));
    ...
}

ovs_fragment() then moves the network header to the inner network header.
With four MPLS labels it ends up at offset 30, which the MAX_L2_LEN check
still allows:

	if (eth_p_mpls(skb->protocol)) {
		orig_network_offset = skb_network_offset(skb);
		skb->network_header = skb->inner_network_header;
	}

prepare_frag() then ignores the return value of skb_pull():

	memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
	skb_pull(skb, hlen);

Take skb->len == 29. skb_pull(skb, 30) returns NULL and leaves skb->data
where it was. ip_hdr(skb) then points 30 bytes past skb->data, which is
past skb->tail.

Now add an MRU of 28 and an output vport with hard_header_len 0. A
TUN-mode tun device qualifies, since tun_net_initialize() sets
dev->hard_header_len = 0. do_output() still calls ovs_fragment() because
29 > 28 + 0.

If the stale byte after the tail holds ihl == 5, then hlen = 20, which is
not above skb_headlen() = 29. The new check passes, and so does the
mtu < hlen + 8 check. A longer original packet sent before TRUNC can
control that stale byte.

>From there, ip_frag_init() reads iph->frag_off from beyond the tail.
ip_frag_next() copies the header from skb->data rather than from the
network header:

	skb_copy_from_linear_data(skb, skb_network_header(skb2), state->hlen);

As a result, every emitted fragment carries Ethernet/MPLS bytes as its
IPv4 header, and the stale frag_off bits end up in its frag_off field.

The BUG() in ip_frag_next() no longer looks reachable, because
hlen <= skb_headlen() keeps state->left and skb_copy_bits() in range.
However, in this case the network offset plus hlen is 50 against a
headlen of 29, and the packet is still not rejected.

Would checking skb_network_offset(skb) + hlen against skb_headlen(skb),
or requiring a zero network offset, cover this case? Another option would
be for prepare_frag() to handle the skb_pull() failure.

> +		err = -EINVAL;
> +		goto fail;
> +	}
>  
>  	mtu = ip_skb_dst_mtu(sk, skb);
>  	if (IPCB(skb)->frag_max_size && IPCB(skb)->frag_max_size < mtu)

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126064445.57665-1-ljp1205831794%40gmail.com

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

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

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26  6:43 [PATCH net] ipv4: reject header length beyond linear data in ip_do_fragment() Lin Jiapeng
2026-11-27  6:48 ` 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