Skip to contentSkip to conversation

ipv4: don't let the transport header offset wrap

PATCH In review Chris Mason 3 participants 3 messages Nov 18

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ipv4: don't let the transport header offset wrap

From: Josef Bacik <[email protected]>

skb->network_header and skb->transport_header are 16-bit offsets from skb->head. ip_rcv_core() computes transport_header as network_header + ihl * 4.

These offsets are normally small. But a tunnel pulls its outer headers and hands the same skb back to the stack. skb->head stays where it is, so skb->data moves further away from it with each decapsulation. After nested decapsulations the end of the inner IP header can lie beyond what 16 bits can hold.

The fix used here is to set the offset with skb_set_transport_header_careful(), and to drop the packet if the offset does not fit.

The helper computes the offset from skb->data, which points to the IP header in ip_rcv_core(). It does not use network_header. So it also catches a packet whose network_header was already truncated, where network_header + ihl * 4 would look valid.

Found by LLM code inspection, and confirmed with LLM generated reproducers on an unmodified kernel. Fixed by LLM, and tested with the reproducers and existing kernel selftests.

Fixes 21d1196a35f5 ("ipv4: set transport header earlier")
Assisted-by LLM
---
 net/ipv4/ip_input.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
net/ipv4/ip_input.c +5 -1
index 9860178752b8..397043893376 100644
@@ -572,7 +572,11 @@ static struct sk_buff *ip_rcv_core(struct sk_buff *skb, struct net *net)
 	}
 
 	iph = ip_hdr(skb);
-	skb->transport_header = skb->network_header + iph->ihl*4;
+	if (!skb_set_transport_header_careful(skb, iph->ihl * 4)) {
+		drop_reason = SKB_DROP_REASON_PKT_TOO_BIG;
+		__IP_INC_STATS(net, IPSTATS_MIB_INDISCARDS);
+		goto drop;
+	}
 
 	/* Remove any debris in the socket control block */
 	memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
Signature · 1 line
2.55.0
maintainer
#1 lore raw
Quoted 21 lines · Chris Mason

From: Josef Bacik <[email protected]>

skb->network_header and skb->transport_header are 16-bit offsets from skb->head. ip_rcv_core() computes transport_header as network_header + ihl * 4.

These offsets are normally small. But a tunnel pulls its outer headers and hands the same skb back to the stack. skb->head stays where it is, so skb->data moves further away from it with each decapsulation. After nested decapsulations the end of the inner IP header can lie beyond what 16 bits can hold.

The fix used here is to set the offset with skb_set_transport_header_careful(), and to drop the packet if the offset does not fit.

The helper computes the offset from skb->data, which points to the IP header in ip_rcv_core(). It does not use network_header. So it also catches a packet whose network_header was already truncated, where network_header + ihl * 4 would look valid.

Quoted 8 lines

Found by LLM code inspection, and confirmed with LLM generated reproducers on an unmodified kernel. Fixed by LLM, and tested with the reproducers and existing kernel selftests.

Fixes 21d1196a35f5 ("ipv4: set transport header earlier")
Assisted-by LLM

Chris Arges is going to post a more comprehensive patch soon.

Quoted 24 lines
---
 net/ipv4/ip_input.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
net/ipv4/ip_input.c +5 -1
index 9860178752b8..397043893376 100644
@@ -572,7 +572,11 @@ static struct sk_buff *ip_rcv_core(struct sk_buff *skb, struct net *net)
 	}
 
 	iph = ip_hdr(skb);
-	skb->transport_header = skb->network_header + iph->ihl*4;
+	if (!skb_set_transport_header_careful(skb, iph->ihl * 4)) {
+		drop_reason = SKB_DROP_REASON_PKT_TOO_BIG;
+		__IP_INC_STATS(net, IPSTATS_MIB_INDISCARDS);
+		goto drop;
+	}
 
 	/* Remove any debris in the socket control block */
 	memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
Signature · 1 line
2.55.0
#2 lore raw
In reply to Chris Mason
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

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

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

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 IPv4 xfrm transport-mode (ESP/AH) receive path still lets the transport header offset wrap, and the wrapped value still reaches…
- [Medium] Sibling receive paths still compute the transport header with the unchecked 16-bit addition this patch removes from ip_rcv_core().
- [Low] The new drop path in ip_rcv_core() (net/ipv4/ip_input.c:576) reports SKB_DROP_REASON_PKT_TOO_BIG.

--

ipv4: don't let the transport header offset wrap

This patch replaces the unchecked 16-bit transport_header computation in ip_rcv_core() with skb_set_transport_header_careful(). If the offset from skb->head doesn't fit, the packet is dropped. The helper computes the offset from skb->data, so it also catches a network_header that was already truncated.

net/ipv4/ip_input.c static struct sk_buff *ip_rcv_core(struct sk_buff *skb, struct net *net) · line 575

Quoted 10 lines
net/ipv4/ip_input.c +1 -1
index 9860178752b8c..3970438933764 100644
@@ -572,7 +572,11 @@ static struct sk_buff *ip_rcv_core(struct sk_buff *skb, struct net *net)
 	}
 
 	iph = ip_hdr(skb);
-	skb->transport_header = skb->network_header + iph->ihl*4;
+	if (!skb_set_transport_header_careful(skb, iph->ihl * 4)) {

[Severity: High]
After this check passes, can the transport header offset still wrap on the IPv4 xfrm transport-mode (ESP/AH) receive path?

ip_rcv_core() only validates the outer header. After that, esp_input_done2() pulls the ESP header and IV, and then sets the transport header relative to the new skb->data:

net/ipv4/esp4.c:esp_input_done2() {
    ...
	skb_pull_rcsum(skb, hlen);
	if (x->props.mode == XFRM_MODE_TUNNEL ||
	    x->props.mode == XFRM_MODE_IPTFS)
		skb_reset_transport_header(skb);
	else
		skb_set_transport_header(skb, -ihl);
    ...
}

Next, xfrm4_transport_input() moves the IP header and resets the transport header without a check:

net/xfrm/xfrm_input.c:xfrm4_transport_input() {
    ...
		skb->network_header = skb->transport_header;
	}
	ip_hdr(skb)->tot_len = htons(skb->len + ihl);
	skb_reset_transport_header(skb);
    ...
}

This stores (u16)(N + ihl + hlen), where N is the outer IP header offset from skb->head. Suppose N + ihl <= 65534, which passes the new check, but N + ihl + hlen >= 65536. For ihl = 20 and hlen = 16 that is N in [65500, 65514]. Then transport_header wraps to a small value while network_header still fits.

The skb is then re-injected without going back through ip_rcv_core():

xfrm4_transport_finish()
NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, ...)

    xfrm4_rcv_encap_finish()
      dst_input()
        ip_local_deliver()
          ip_local_deliver_finish()
            __skb_pull(skb, skb_network_header_len(skb));

With the wrapped transport_header, skb_network_header_len() returns 0xFFFF0000 + ihl as a u32. __skb_pull() then raises skb->len by 65536 - ihl, so the skb->len < skb->data_len BUG() never fires. It also moves skb->data about 4 GiB forward on 64-bit, or wraps it back toward skb->head on 32-bit. The protocol handler then dereferences skb->data.

This needs a transport-mode SA and an ESP/AH packet that authenticates against it. It also needs a head offset near 64 KiB, which the commit message says nested decapsulation produces. It hasn't been reproduced at runtime.

The subject says this wrap is fixed for IPv4. Should the series also cover xfrm4_transport_input() and the esp4/ah4 transport-mode paths?

[Severity: Medium]
The unchecked 16-bit addition that this patch removes from ip_rcv_core() is still present in several sibling receive paths:

net/bridge/br_netfilter_hooks.c:br_nf_pre_routing() {
    ...
	skb->transport_header = skb->network_header + ip_hdr(skb)->ihl * 4;
    ...
}
net/bridge/br_netfilter_ipv6.c:br_nf_pre_routing_ipv6() {
    ...
	skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
    ...
}
net/ipv6/ip6_input.c:ip6_rcv_core() {
    ...
	skb->transport_header = skb->network_header + sizeof(*hdr);
    ...
}

The commit message says:

So it also catches a packet whose network_header was already truncated, where network_header + ihl * 4 would look valid.

Does that hold only for native IPv4 input?

After VXLAN, GENEVE or gretap decapsulation, bridged traffic reaches NF_HOOK(NF_INET_PRE_ROUTING) from br_nf_pre_routing() and never passes through ip_rcv_core(). The outer header can pass the new check just under the limit, and the tunnel then pulls about 50 more bytes.

br_validate_ipv4() is a copy of the ip_rcv_core() checks, but it still validates ip_hdr(skb), which is based on network_header. If network_header was truncated, it validates stale bytes, and the netfilter hooks see a wrapped transport_header.

For IPv6, transport_header can wrap to 0..39, below network_header. That looks like it would cause:

- ipv6_parse_hopopts() in net/ipv6/exthdrs.c reads skb_transport_header(skb)[1] from stale bytes at the start of the head buffer.
- ip6_parse_tlv() starts from a negative skb_network_header_len() offset, so the jumbo, RA and IOAM handlers act on stale bytes.
- Local delivery drops the packet, because
pskb_pull(skb, skb_transport_offset(skb)) in ip6_protocol_deliver_rcu() gets a huge length.

If network_header is truncated, IPv6 validation, route lookup and PRE_ROUTING/FORWARD filtering all run on stale bytes. Forwarding still sends the real bytes at skb->data.

Could filtering and routing decisions be made on one header while different bytes are forwarded? Should br_nf_pre_routing(), br_nf_pre_routing_ipv6() and ip6_rcv_core() get the same treatment in this series?

net/ipv4/ip_input.c static struct sk_buff *ip_rcv_core(struct sk_buff *skb, struct net *net) · line 576

Quoted 1 line
net/ipv4/ip_input.c +1 -0
+		drop_reason = SKB_DROP_REASON_PKT_TOO_BIG;

[Severity: Low]
This isn't a bug, but is SKB_DROP_REASON_PKT_TOO_BIG the right reason here? include/net/dropreason-core.h documents it as:

* @SKB_DROP_REASON_PKT_TOO_BIG: packet size is too big (maybe exceed the
* MTU)

By this point the packet has been trimmed to tot_len and has passed every length check, so it can be a minimal 20-byte datagram. The failing condition is that skb->data - skb->head + ihl * 4 doesn't fit in the 16-bit transport_header. That is about buffer layout, not packet size.

The other in-tree users of this reason are size or MTU checks, for example in ip6_output.c, openvswitch actions.c and xfrm_iptfs.c. As a result, dropwatch and the kfree_skb tracepoint would report these drops as size/MTU drops.

Quoted 3 lines
net/ipv4/ip_input.c +3 -0
+		__IP_INC_STATS(net, IPSTATS_MIB_INDISCARDS);
+		goto drop;
+	}

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118004850.52808-1-mason%40kernel.org