Re: [PATCH RFC net v11 4/6] net: resolve uncommon partial checksums at ingress

From
sashiko-bot@kernel.org
Date
2026-12-05 04:01:53
Reply to
Showta Ishizaki, 2026-12-04
Thank you for your contribution! Sashiko AI review found 8 potential
issues to consider:
- [High] net/packet: unexported eth_header_ops in
  packet_vnet_hdr_to_skb()
- [High] net: non-Ethernet link-header devices bypass
  virtio_net_l3_start() check
- [Medium] net: virtio_net_csum_partial_ok_at_offset() pulls unread
  tcphdr bytes on non-GSO
- [Medium] net: virtio_net_csum_partial_ok_at_offset() drops NSH GSO and
  breaks nsh_gso.sh
- [Medium] virtio_net: virtio_net_l3_start() mutates skb->protocol on
  failed validation
- [Medium] virtio_net: virtio_net_hdr_to_skb() restores stale
  transport_header
- [Medium] tun: shared atomic64_t rx_csum_sw false-shares hot cacheline
  in tun_struct
- [Medium] packet: unlocked dev->header_ops/type reads in
  packet_vnet_hdr_to_skb()
--

commit bbc09c06c4fccf93de5eb92627de4a572115b649
Author: Michael S. Tsirkin [off-list ref]

net: resolve uncommon partial checksums at ingress

This commit validates virtio_net_hdr CHECKSUM_PARTIAL metadata against the
parsed TCP or UDP header at ingress, dropping unrecognized GSO packets and
completing unmatched non-GSO checksums in software.
quoted hunk ↗ jump to hunk
diff --git a/include/linux/virtio_net.h b/include/linux/virtio_net.h
index 3135dae031a4d..714b20acd38f0 100644
--- a/include/linux/virtio_net.h
+++ b/include/linux/virtio_net.h
@@ -111,6 +111,12 @@ virtio_net_l3_start(struct sk_buff *skb, bool has_eth, int *nhoff,
 		 */
 		if (skb->protocol && skb->protocol != h_proto)
 			return false;
+		/* Fill in a missing claim from the frame, so that a later
+		 * guess (virtio_net_hdr_set_proto()) cannot disagree
+		 * with what is validated here.
+		 */
+		if (!skb->protocol)
+			skb->protocol = h_proto;
 		*nhoff = ETH_HLEN;
 		*proto = h_proto;
 		if (eth_type_vlan(*proto)) {
@@ -141,6 +147,8 @@ virtio_net_l3_start(struct sk_buff *skb, bool has_eth, int *nhoff,
 			return false;
 		if (skb->protocol && skb->protocol != *proto)
 			return false;
+		if (!skb->protocol)
+			skb->protocol = *proto;
 	}
 	return true;
 }
[Severity: Medium]
Can virtio_net_l3_start() leave skb->protocol mutated when checksum
validation fails?

virtio_net_l3_start() sets skb->protocol before VLAN, L3, and L4 validation
succeed, and virtio_net_hdr_to_skb() only restores skb->transport_header on
mismatch before completing the checksum in software.  On an IFF_TUN device in
PI mode, if userspace sends pi.proto == 0 with VIRTIO_NET_HDR_F_NEEDS_CSUM
and a short non-IP frame starting with 0x60, virtio_net_l3_start() sets
skb->protocol to htons(ETH_P_IPV6) from the first byte before
virtio_net_l4_start() fails.  tun_get_user() then injects the frame into
ipv6_rcv() instead of leaving skb->protocol as 0.  On AF_PACKET SOCK_RAW with
proto == 0 or ETH_P_ALL, the mutated skb->protocol also causes
packet_parse_headers() to skip dev_parse_header_protocol().

Saving and restoring skb->protocol on mismatch in virtio_net_hdr_to_skb(), or
only writing skb->protocol after validation succeeds, would avoid this.
quoted hunk ↗ jump to hunk
@@ -190,6 +198,77 @@ virtio_net_l4_start(struct sk_buff *skb, unsigned int nhoff, __be16 proto,
 	return false;
 }
 
+/* Does checksum metadata match a known TCP or UDP header? */
+static inline bool
+virtio_net_csum_partial_ok_at_offset(struct sk_buff *skb, u8 hdr_gso_type,
+				     unsigned int nhoff, __be16 proto)
+{
+	unsigned int thoff;
+	int ip_proto;
+
+	if (!virtio_net_l4_start(skb, nhoff, proto, &thoff, &ip_proto))
+		return false;
[Severity: Medium]
Does this drop valid NSH-encapsulated GSO packets and break the in-tree
tools/testing/selftests/net/nsh_gso.sh selftest?

nsh_gso.c sends ETH_P_NSH frames with VIRTIO_NET_HDR_F_NEEDS_CSUM and
VIRTIO_NET_HDR_GSO_TCPV4 over AF_PACKET PACKET_VNET_HDR.  Because
virtio_net_l4_start() only handles ETH_P_IP and ETH_P_IPV6,
virtio_net_csum_partial_ok_at_offset() returns false and
__virtio_net_resolve_csum_partial_mismatch() drops the GSO packet with
-EINVAL, so nsh_gso.sh transmits 0 packets instead of 4 under the default
configuration.
quoted hunk ↗ jump to hunk
+	if (skb_checksum_start_offset(skb) != thoff)
+		return false;
+
+	switch (ip_proto) {
+	case IPPROTO_TCP:
+		if (skb->csum_offset != offsetof(struct tcphdr, check) ||
+		    !pskb_may_pull(skb, thoff + sizeof(struct tcphdr)))
+			return false;
+		break;
+	case IPPROTO_UDP:
+		if (skb->csum_offset != offsetof(struct udphdr, check) ||
+		    !pskb_may_pull(skb, thoff + sizeof(struct udphdr)))
+			return false;
+		break;
[Severity: Medium]
Does pskb_may_pull(skb, thoff + sizeof(struct tcphdr)) trigger an unnecessary
__pskb_pull_tail() on every non-GSO TCP packet on tun/tap zerocopy TX?

When hdr->hdr_len is 0 (the default from virtio_net_hdr_from_skb()),
__tun_vnet_hdr_get() computes hdr_len as csum_start + csum_offset + 2, which
stops right after tcphdr->check and leaves skb_headlen(skb) 2 bytes short of
thoff + sizeof(struct tcphdr) in tun_get_user() and tap_get_user().  Neither
this helper nor virtio_net_hdr_to_skb() dereferences struct tcphdr, so
checking skb->len < thoff + sizeof(struct tcphdr) (and sizeof(struct udphdr))
would enforce the same length check without pulling unread fragment data.

[ ... ]
quoted hunk ↗ jump to hunk
+/*
+ * Handle a packet whose checksum metadata does not match the packet:
+ * GSO packets are dropped, as segmentation needs one checksum per
+ * segment; others are completed in software.
+ */
+static inline int __virtio_net_resolve_csum_partial_mismatch(struct sk_buff *skb)
+{
+	if (skb_is_gso(skb))
+		return -EINVAL;
+
+	return skb_checksum_help(skb) ?: 1;
+}
[Severity: Medium]
Returning 1 here makes atomic64_inc(&tun->rx_csum_sw) in tun_get_user() and
tun_xdp_one() reachable for the first time, which can cause cacheline false
sharing on multiqueue tun devices.

In struct tun_struct (added in commit "net: account for ingress-resolved
virtio net hdr state"), rx_csum_sw sits in a contiguous 44-byte span with
rx_batched, xdp_prog, steering_prog, and filter_prog, which are read on every
RX and TX packet across all queues.  Unmatched non-GSO CHECKSUM_PARTIAL
traffic on one queue will bounce that cacheline across CPUs handling healthy
queues.  Using a per-CPU counter summed in tun_get_ethtool_stats() would
avoid the contention and false sharing.
quoted hunk ↗ jump to hunk
@@ -336,11 +415,38 @@ static inline int __virtio_net_hdr_to_skb(struct sk_buff *skb,
 	return 0;
 }
 
+/*
+ * @has_eth: the frame starts with an Ethernet header, see
+ * virtio_net_l3_start().
+ *
+ * Return value:
+ * 0 - success
+ * >0 - checksum was completed in software (slow path)
+ * <0 - failure
+ */
 static inline int virtio_net_hdr_to_skb(struct sk_buff *skb,
 					const struct virtio_net_hdr *hdr,
-					bool little_endian)
+					bool has_eth, bool little_endian)
 {
-	return __virtio_net_hdr_to_skb(skb, hdr, little_endian, hdr->gso_type);
+	typeof(skb->transport_header) old_transport = skb->transport_header;
+	int ret = __virtio_net_hdr_to_skb(skb, hdr, little_endian,
+					  hdr->gso_type);
+
+	if (ret < 0)
+		return ret;
+
+	if (likely(skb->ip_summed != CHECKSUM_PARTIAL ||
+		   virtio_net_csum_partial_ok(skb, has_eth, hdr->gso_type)))
+		return 0;
+
+	/*
+	 * Here we do not trust the checksum metadata.
+	 * __virtio_net_hdr_to_skb set the transport header to csum_start
+	 * from virtio hdr, restore it to a known good value.
+	 */
+	skb->transport_header = old_transport;
+
+	return __virtio_net_resolve_csum_partial_mismatch(skb);
 }
[Severity: Medium]
Can restoring old_transport re-expose a stale transport_header offset on
recycled skbs?

On an IFF_TAP | IFF_NAPI | IFF_NAPI_FRAGS device with gro_flush_timeout and
napi_defer_hard_irqs configured (where busy_poll_stop() or tun_napi_poll()
keeps packets held in tfile->napi.gro across tun_get_user() calls),
napi_reuse_skb() recycles GRO-merged skbs into tfile->napi.skb without
unsetting transport_header, and tun_napi_alloc_frags() only clears
skb->protocol.  Restoring that stale offset on mismatch leaves
skb_transport_header_was_set(skb) true, so skb_probe_transport_header() in
tun_get_user() returns immediately and passes a wrong or out-of-bounds
transport_header downstream.

Calling skb_unset_transport_header(skb) on mismatch (here and in
virtio_net_hdr_tnl_to_skb() in commit "net: virtio: compute the outer
checksum of non-GSO tunnel packets") would avoid preserving stale offsets.

[ ... ]
quoted hunk ↗ jump to hunk
diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
index 930e85e4bf739..7ae70ca1556e4 100644
--- a/net/packet/af_packet.c
+++ b/net/packet/af_packet.c
@@ -2778,6 +2778,24 @@ static int tpacket_parse_header(struct packet_sock *po, void *frame,
 	return tp_len;
 }
 
+/* The device says how its frames start.  dev->type alone does not:
+ * TUNSETLINK can make an IFF_TUN device ARPHRD_ETHER while its frames
+ * still start with the L3 header, and such a device has no link layer
+ * header to create.  It can also make an IFF_TAP device something
+ * else, while the device still builds and parses Ethernet headers.
+ */
+static int packet_vnet_hdr_to_skb(struct sk_buff *skb,
+				  const struct virtio_net_hdr *vnet_hdr)
+{
+	const struct net_device *dev = skb->dev;
+	bool has_eth = dev_has_header(dev) &&
+		       (dev->type == ARPHRD_ETHER ||
+			dev->type == ARPHRD_LOOPBACK ||
+			dev->header_ops == &eth_header_ops);
+
+	return virtio_net_hdr_to_skb(skb, vnet_hdr, has_eth, vio_le());
+}
[Severity: High]
Does referencing eth_header_ops break the build when CONFIG_PACKET=m?

eth_header_ops in net/ethernet/eth.c is not exported with EXPORT_SYMBOL(), so
modpost fails with an undefined symbol for af_packet.ko.

[Severity: High]
Can non-Ethernet devices that still have link-layer headers (such as ipgre or
ipoib) bypass the ingress CHECKSUM_PARTIAL validation here?

On an NBMA ipgre device (ARPHRD_IPGRE, ipgre_header_ops, hard_header_len ==
24), has_eth evaluates to false, routing the skb into the raw-L3 branch of
virtio_net_l3_start().  For SOCK_RAW frames with len >= 64 (or tpacket_snd()),
skb_network_offset(skb) is 24, so virtio_net_l3_start() skips the leading
24-byte tnl_params unvalidated and validates an inner IP/UDP/TCP header with
csum_start >= 44.  ipgre_xmit() then pulls those 24 bytes, encapsulates with
attacker-controlled tnl_params->protocol, and forwards across veth into the
host stack with CHECKSUM_PARTIAL still misaligned into the outer payload.
(For short SOCK_RAW packets where packet_snd() resets network_header to 0,
virtio_net_l3_start() validates the GRE pseudo-header itself as an IPv4
header with options.)

Non-Ethernet devices with dev_has_header(dev) should not fall through to the
raw-L3 validation branch and should resolve the checksum in software instead.

[Severity: Medium]
Can these unlocked reads of dev->header_ops and dev->type race with
concurrent netdevice type changes and cause a NULL pointer dereference in
dev_has_header()?

packet_snd() and tpacket_snd() hold neither rtnl_lock() nor rcu_read_lock().
When a non-Ethernet port with NULL header_ops (such as nlmon or bareudp) is
added to an IFF_UP team device, team_port_add() -> team_setup_by_port()
clears dev->header_ops and updates dev->type concurrently.  Because
dev_has_header() evaluates dev->header_ops && dev->header_ops->create without
READ_ONCE(), a compiler reload between the check and dereference can oops:

CPU0                              CPU1
packet_snd()
  packet_vnet_hdr_to_skb()
    dev_has_header(dev)
      if (dev->header_ops)
                                  team_port_add()
                                    ...
                                      team_setup_by_port()
                                        dev->header_ops = NULL
      dev->header_ops->create     <-- NULL pointer dereference

Even without a reload, the multiple plain reads of dev->header_ops and
dev->type are data races that can compute an inconsistent has_eth.
Snapshotting READ_ONCE(dev->header_ops) and READ_ONCE(dev->type) once into
locals (paired with WRITE_ONCE() on the writers) would prevent this.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204.rfcv11.99731.0.zakinko@snowrabbit.org?part=4
Thread (14 messages) 14 messages, 3 authors, last reply 1d ago
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help