Skip to contentSkip to conversation

net: validate IPv4 checksum start in skb_csum_hwoffload_help()

PATCH In review Dmitrii Iakunin 4 participants 4 messages Nov 11 – Nov 15

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

PATCH v2 1/1 net: validate IPv4 checksum start in skb_csum_hwoffload_help()

Commit 6785011f8b16 ("ipv4: validate checksum_start before completing checksum") added checks so that malformed CHECKSUM_PARTIAL metadata cannot make software checksum completion write the folded checksum into the IPv4 header.

skb_csum_hwoffload_help() completes checksums in software when the device features cannot satisfy the request (e.g. via validate_xmit_skb(), ieee80211_tx_skb_fixup(), openvswitch queue_userspace_packet() or ppp_bridge_xmit()). Packets built by AF_PACKET with PACKET_VNET_HDR or by tap with VIRTIO_NET_HDR_F_NEEDS_CSUM can reach it with a csum_start that points inside the IPv4 header, and this path was not covered by the earlier validation.

At the sw_checksum label, for IPv4 packets, require the fixed IPv4 header to be pullable and reject csum_start values that fall before the end of the IPv4 header. Return -EINVAL so callers drop the packet.

Unlike the sites added by that commit, this path cannot rely on earlier validation, so:

- Locate the IPv4 header at the depth returned by __vlan_get_protocol() rather than at network_header. After skb_vlan_untag() pops only the outer tag of a QinQ frame and the skb is redirected by tc mirred or bpf_redirect, network_header still points at the inner VLAN tag, and ihl would be read from the TCI.

- Reject ihl < 5. ip_rcv_core() has not checked it on these paths, and with a sender-chosen ihl of 0..4 a csum_start inside the fixed 20-byte header would otherwise pass the check.

- Skip the check when the network offset is negative. On the PPP channel-bridge path (L2TP or PPTP), the outer headers have been pulled while network_header and protocol still describe the outer IPv4 header. The IPv4 header of the packet being checksummed cannot be located there, and those packets used to be forwarded.

Hardware-offloaded checksums and SCTP CRC handling are unchanged.

This change only covers the software fallback in skb_csum_hwoffload_help(). Other places that call skb_checksum_help() directly are not covered: drivers that fall back to software after the core offered hardware offload (e.g. wireguard, igb, tg3), and core callers such as xsk tx_sw_csum and netem corruption. As the earlier commit notes, validating the metadata on input remains the complete fix.

Fixes f43798c27684 ("tun: Allow GSO using virtio_net_hdr")
Fixes bfd5f4a3d605 ("packet: Add GSO/csum offload support.")
Assisted-by claude-opus-5.5

---
v2:
- Locate the IPv4 header using the parsed VLAN depth.
- Reject IPv4 headers with ihl < 5.
- Skip validation when the network offset is negative.
- Narrow the subject and document software-fallback paths not covered.
- Update the Fixes tags to identify the original introduction points.

v1: https://lore.kernel.org/lkml/[email protected]/

 net/core/dev.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
net/core/dev.c +23 -0
index df1a840b0334..fafcb8f7ff9b 100644
@@ -4111,6 +4111,9 @@ static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
 int skb_csum_hwoffload_help(struct sk_buff *skb,
 			    const netdev_features_t features)
 {
+	__be16 protocol;
+	int offset;
+
 	if (unlikely(skb_csum_is_sctp(skb)))
 		return !!(features & NETIF_F_SCTP_CRC) ? 0 :
 			skb_crc32c_csum_help(skb);
@@ -4130,6 +4133,26 @@ int skb_csum_hwoffload_help(struct sk_buff *skb,
 	}
 
 sw_checksum:
+	offset = skb_network_offset(skb);
+	protocol = skb->protocol;
+	if (eth_type_vlan(protocol))
+		protocol = __vlan_get_protocol(skb, protocol, &offset);
+
+	/* Use the parsed VLAN depth: network_header may point at an inner
+	 * tag. If the network offset is negative, network_header refers
+	 * to bytes preceding the current packet data and cannot be used
+	 * to locate the IPv4 header being validated.
+	 */
+	if (offset >= 0 && protocol == htons(ETH_P_IP)) {
+		const struct iphdr *iph;
+
+		if (!pskb_may_pull(skb, offset + sizeof(*iph)))
+			return -EINVAL;
+		iph = (const struct iphdr *)(skb->data + offset);
+		if (iph->ihl < 5 ||
+		    skb_checksum_start_offset(skb) < offset + iph->ihl * 4)
+			return -EINVAL;
+	}
 	return skb_checksum_help(skb);
 }
 EXPORT_SYMBOL(skb_csum_hwoffload_help);
Signature · 1 line
2.52.0
#1 lore raw
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

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.

- Whether the issue was actually triggered, or is only theoretical (e.g. found by LLM and not proven at runtime). If it was triggered please include the symptoms, like the stack trace or kernel messages.

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. If the series needs another revision for other reasons, please include the information in the commit messages then.

The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.

#2 lore raw
In reply to Dmitrii Iakunin
Quoted 4 lines · Dmitrii Iakunin

Commit 6785011f8b16 ("ipv4: validate checksum_start before completing checksum") added checks so that malformed CHECKSUM_PARTIAL metadata cannot make software checksum completion write the folded checksum into the IPv4 header.

... after validation but before ip_do_fragment received?

Quoted 7 lines

skb_csum_hwoffload_help() completes checksums in software when the device features cannot satisfy the request (e.g. via validate_xmit_skb(), ieee80211_tx_skb_fixup(), openvswitch queue_userspace_packet() or ppp_bridge_xmit()). Packets built by AF_PACKET with PACKET_VNET_HDR or by tap with VIRTIO_NET_HDR_F_NEEDS_CSUM can reach it with a csum_start that points inside the IPv4 header, and this path was not covered by the earlier validation.

And so what? it doesn't go to ip_do_fragment. if af_packet wants to inject malformed packets, it already can.

Quoted 24 lines

At the sw_checksum label, for IPv4 packets, require the fixed IPv4 header to be pullable and reject csum_start values that fall before the end of the IPv4 header. Return -EINVAL so callers drop the packet.

Unlike the sites added by that commit, this path cannot rely on earlier validation, so:

- Locate the IPv4 header at the depth returned by __vlan_get_protocol() rather than at network_header. After skb_vlan_untag() pops only the outer tag of a QinQ frame and the skb is redirected by tc mirred or bpf_redirect, network_header still points at the inner VLAN tag, and ihl would be read from the TCI.

- Reject ihl < 5. ip_rcv_core() has not checked it on these paths, and with a sender-chosen ihl of 0..4 a csum_start inside the fixed 20-byte header would otherwise pass the check.

- Skip the check when the network offset is negative. On the PPP channel-bridge path (L2TP or PPTP), the outer headers have been pulled while network_header and protocol still describe the outer IPv4 header. The IPv4 header of the packet being checksummed cannot be located there, and those packets used to be forwarded.

Quoted 7 lines

Hardware-offloaded checksums and SCTP CRC handling are unchanged.

This change only covers the software fallback in skb_csum_hwoffload_help(). Other places that call skb_checksum_help() directly are not covered: drivers that fall back to software after the core offered hardware offload (e.g. wireguard, igb, tg3), and core callers such as xsk tx_sw_csum and netem corruption.

what does this mean?

Quoted 67 lines

As the earlier
commit notes, validating the metadata on input remains the complete fix.

Fixes f43798c27684 ("tun: Allow GSO using virtio_net_hdr")
Fixes bfd5f4a3d605 ("packet: Add GSO/csum offload support.")
Assisted-by claude-opus-5.5

---
v2:
- Locate the IPv4 header using the parsed VLAN depth.
- Reject IPv4 headers with ihl < 5.
- Skip validation when the network offset is negative.
- Narrow the subject and document software-fallback paths not covered.
- Update the Fixes tags to identify the original introduction points.

v1: https://lore.kernel.org/lkml/[email protected]/

 net/core/dev.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
net/core/dev.c +23 -0
index df1a840b0334..fafcb8f7ff9b 100644
@@ -4111,6 +4111,9 @@ static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
 int skb_csum_hwoffload_help(struct sk_buff *skb,
 			    const netdev_features_t features)
 {
+	__be16 protocol;
+	int offset;
+
 	if (unlikely(skb_csum_is_sctp(skb)))
 		return !!(features & NETIF_F_SCTP_CRC) ? 0 :
 			skb_crc32c_csum_help(skb);
@@ -4130,6 +4133,26 @@ int skb_csum_hwoffload_help(struct sk_buff *skb,
 	}
 
 sw_checksum:
+	offset = skb_network_offset(skb);
+	protocol = skb->protocol;
+	if (eth_type_vlan(protocol))
+		protocol = __vlan_get_protocol(skb, protocol, &offset);
+
+	/* Use the parsed VLAN depth: network_header may point at an inner
+	 * tag. If the network offset is negative, network_header refers
+	 * to bytes preceding the current packet data and cannot be used
+	 * to locate the IPv4 header being validated.
+	 */
+	if (offset >= 0 && protocol == htons(ETH_P_IP)) {
+		const struct iphdr *iph;
+
+		if (!pskb_may_pull(skb, offset + sizeof(*iph)))
+			return -EINVAL;
+		iph = (const struct iphdr *)(skb->data + offset);
+		if (iph->ihl < 5 ||
+		    skb_checksum_start_offset(skb) < offset + iph->ihl * 4)
+			return -EINVAL;
+	}
 	return skb_checksum_help(skb);
 }
 EXPORT_SYMBOL(skb_csum_hwoffload_help);
Signature · 1 line
2.52.0
#3 lore raw
In reply to Dmitrii Iakunin
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

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

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

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 new check in skb_csum_hwoffload_help() (net/core/dev.c:4136-4153) decides whether a packet is IPv4, and where…
- [Medium] When skb->protocol is 802.1Q/802.1AD, the new check finds the IPv4 header with __vlan_get_protocol() (net/core/dev.c:4139).

--

net: validate IPv4 checksum start in skb_csum_hwoffload_help()

This patch adds a check at the sw_checksum label of skb_csum_hwoffload_help(). It rejects IPv4 packets whose csum_start falls inside the IPv4 header, and returns -EINVAL so callers drop them. The IPv4 header is found at the depth returned by __vlan_get_protocol(), ihl < 5 is rejected, and the check is skipped when the network offset is negative.

Quoted 4 lines
net/core/dev.c +0 -0
index 3c58db732761a..095b1fd38b8b2 100644

[ ... ]

net/core/dev.c int skb_csum_hwoffload_help(struct sk_buff *skb, · line 4139

Quoted 8 lines
net/core/dev.c +4 -0
@@ -4130,6 +4133,26 @@ int skb_csum_hwoffload_help(struct sk_buff *skb,
 	}
 
 sw_checksum:
+	offset = skb_network_offset(skb);
+	protocol = skb->protocol;
+	if (eth_type_vlan(protocol))
+		protocol = __vlan_get_protocol(skb, protocol, &offset);

[Severity: Medium]
Can skb->mac_len be relied on at this point? __vlan_get_protocol() starts parsing at skb->mac_len - VLAN_HLEN:

net/core/skbuff.c:__vlan_get_protocol_offset() {
	unsigned int vlan_depth = skb->mac_len, parse_depth = VLAN_MAX_DEPTH;
    ...
}

Nothing on the transmit path keeps mac_len current. In validate_xmit_vlan()->__vlan_hwaccel_push_inside(), the tag is pushed and protocol is set to 0x8100, but mac_len is left unchanged. __dev_queue_xmit() only resets mac_header. __skb_gso_segment() calls skb_reset_mac_len() before doing the same kind of parse; this code does not.

GRE devices that use ipgre_header_ops (NBMA GRE with no remote, or multicast GRE) are one source of a stale mac_len. __ipgre_rcv() calls skb_pop_mac_header(), which leaves mac_header at the outer IPv4 header. dev_gro_receive() or __netif_receive_skb_core() then set mac_len to 20 plus the GRE header length (24 or 32).

Suppose such a CHECKSUM_PARTIAL packet (e.g. from a local veth or virtio sender) is forwarded out a VLAN subinterface. The real device has no TX VLAN insertion and ends up at sw_checksum. One way to get there is a real device with only NETIF_F_IPV6_CSUM: vlan_dev_fix_features() promotes the VLAN device to HW_CSUM, and harmonize_features() strips IPv4 checksum on the real device. The egress frame then looks like this:

MAC 0-11, TPID 12-13, TCI 14-15, 0x0800 16-17, IPv4 18-37, TCP 38-

With mac_len=24, the parser reads a vlan_hdr at offset 20, so the encapsulated protocol comes from the IPv4 ID field. When the ID is 0x0800, protocol becomes ETH_P_IP at offset 24. ihl then comes from the low nibble of the frag_off high byte, which is 0 with DF or no flags, so the packet gets -EINVAL.

With mac_len=32, the parser reads saddr[0..1]. Any source in 8.0.0.0/16 gives ETH_P_IP at offset 32, and every such packet is dropped.

Before this patch, these forwarded packets were checksummed in software and sent. Would calling skb_reset_mac_len() first, as __skb_gso_segment() does, avoid this?

Quoted 5 lines
net/core/dev.c +5 -0
+
+	/* Use the parsed VLAN depth: network_header may point at an inner
+	 * tag. If the network offset is negative, network_header refers
+	 * to bytes preceding the current packet data and cannot be used
+	 * to locate the IPv4 header being validated.

net/core/dev.c int skb_csum_hwoffload_help(struct sk_buff *skb, · line 4154

Quoted 10 lines
net/core/dev.c +10 -0
+	 */
+	if (offset >= 0 && protocol == htons(ETH_P_IP)) {
+		const struct iphdr *iph;
+
+		if (!pskb_may_pull(skb, offset + sizeof(*iph)))
+			return -EINVAL;
+		iph = (const struct iphdr *)(skb->data + offset);
+		if (iph->ihl < 5 ||
+		    skb_checksum_start_offset(skb) < offset + iph->ihl * 4)
+			return -EINVAL;

[Severity: High]
Can the sender steer this check both ways? For AF_PACKET, and for tun IFF_TUN with packet info, the sender sets both skb->protocol and network_header. Nothing compares them with the frame bytes:

net/packet/af_packet.c:packet_snd() {
    ...
	if (proto != htons(ETH_P_ALL))
		skb->protocol = proto;
    ...
}
net/packet/af_packet.c:packet_parse_headers() {
    ...
	if (!skb->protocol && sock->type == SOCK_RAW)
		skb->protocol = dev_parse_header_protocol(skb);
    ...
}
drivers/net/tun.c:tun_get_user() {
    ...
	skb->protocol = pi.proto;
    ...
}

packet_parse_headers() reads the real ethertype only when skb->protocol is 0. Its VLAN network_header fixup runs only when skb->protocol is already a VLAN type. For non-GSO NEEDS_CSUM, __virtio_net_hdr_to_skb() checks csum_start and csum_offset only against headlen.

The first problem is new drops. Take a SOCK_RAW packet socket bound to ETH_P_IP with PACKET_VNET_HDR. It sends an 802.1Q-tagged IPv4/TCP frame (csum_start=38, csum_offset=16) on a device without checksum offload. eth_type_vlan(ETH_P_IP) is false, so offset stays at 14. ihl is then read from the low nibble of the TCI high byte (VID bits 11..8):

VID < 0x500: ihl < 5, rejected
VID >= 0x700: 38 < 14 + ihl * 4, rejected

An IPv6 frame labelled ETH_P_IP is rejected the same way, because ihl becomes the traffic class nibble.

Before this patch, skb_checksum_help() checksummed these frames correctly. Now validate_xmit_skb() drops them on -EINVAL. Is that intended?

The second problem is a bypass. A PACKET_VNET_HDR sender puts a real IPv4 header in the frame (ethertype 0x0800). It sets sll_protocol to ETH_P_IPV6, or to a non-IP value such as ETH_P_802_3, and points csum_start inside the IPv4 header (e.g. 24). protocol == htons(ETH_P_IP) is false, so the check is skipped. skb_checksum_help() still writes the folded checksum into the IPv4 header.

That looks like the case the commit message says is now covered for AF_PACKET with PACKET_VNET_HDR.

In the mislabelled VLAN case, the sender also chooses the TCI bits that ihl is read from. VID 0x5xx gives ihl=5, so any csum_start >= 34 passes. That includes 34..37, which are inside the real IPv4 header at 18..37.

The commit message also says:

"network_header still points at the inner VLAN tag, and ihl would be read from the TCI."

That avoidance only holds when skb->protocol matches the frame.

Could the IPv4 header be located from data the sender doesn't control instead of skb->protocol? For example, use the frame's own ethertype on Ethernet devices plus an iph->version == 4 check.

Quoted 4 lines
net/core/dev.c +1 -0
+	}
 	return skb_checksum_help(skb);
 }
 EXPORT_SYMBOL(skb_csum_hwoffload_help);
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261111120448.49979-1-zeil%40nebius.com