Skip to contentSkip to conversation

more virtio-net csum hardening

RFC PATCH Superseded Showta Ishizaki 4 participants 13 messages Dec 3 – Dec 4

Subsystems (8)

v10 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v10 cover more virtio-net csum hardening

Michael's series again, now v10. Patches 1-4 and 6 are his; patch 5 is mine. The only change since v9 is in patch 5.

Changes since v9:
- Patch 5: the non-GSO fallback summed skb->len - outer_th for the outer UDP checksum, which also covers trailing Ethernet padding, so a frame with non-zero padding got a wrong outer checksum. Sum uh->len instead, bounded so a length from the frame cannot over-read. Chris Arges found this in testing and sent the fix.

The two issues Michael listed as open in v7 are still open, hence the
RFC: the inner packet of a tunnel can have a valid header at inner_nh_offset that overlaps the real transport header, and completing the checksum at input breaks nf/BPF programs that edit the payload and rely on CHECKSUM_PARTIAL.

Patches 1-4 and 6 are unchanged from v9 and were tested as described there. For patch 5 I sent, through a tap bridged to a tap with no offloads, a non-GSO UDP tunnel frame whose inner checksum is completed in software (an inner more-fragments bit) with non-zero Ethernet padding; without the fix the outer UDP checksum comes out wrong for both a v4 and a v6 outer, with it they verify. Based on net e6474ead54b8, the same base as v9; it also applies to current net (c1c39137dcc3).

Michael S. Tsirkin (5):
net: account for ingress-resolved virtio net hdr state
um: vector: trim skbs before verifying the header
virtio_net: parse tunnel csum_start little-endian
net: resolve uncommon partial checksums at ingress
net: add a sysctl to opt out of virtio net hdr checksum validation

Showta Ishizaki (1):
net: virtio: compute the outer checksum of non-GSO tunnel packets

 Documentation/admin-guide/sysctl/net.rst |  19 ++
 arch/um/drivers/vector_kern.c            |   6 +-
 arch/um/drivers/vector_transports.c      |   4 +-
 drivers/net/tap.c                        |   4 +-
 drivers/net/tun.c                        |  44 +++-
 drivers/net/tun_vnet.h                   |   4 +-
 drivers/net/virtio_net.c                 |   2 +-
 include/linux/virtio_net.h               | 263 ++++++++++++++++++++++-
 include/net/netns/core.h                 |   1 +
 net/core/dev.c                           |   7 +
 net/core/net_namespace.c                 |   1 +
 net/core/sysctl_net_core.c               |  50 +++++
 net/packet/af_packet.c                   |  21 +-
 13 files changed, 402 insertions(+), 24 deletions(-)

base-commit: e6474ead54b8acecb8b0462dde2d77f46d2b7dc1

Signature · 1 line
2.54.0 (Apple Git-157)
#1 lore raw

RFC PATCH v10 1/6 net: account for ingress-resolved virtio net hdr state

From: Michael S. Tsirkin <[email protected]>

A later patch will add ingress resolution of uncommon virtio net hdr checksum metadata (software checksum completion at ingress) which adds performance overhead to the packet processing.

We should make it visible, so a hypothetical workload relying on uncommon metadata can be distinguished from none at all. This way issues in the parsing will be reported and not quietly pay the host-CPU checksum overhead instead of offload.

Flip every call to virtio_net_hdr_to_skb to test for negative errno only. The um vector raw transport even ignored the return value
altogether: rejected packets went on into the stack with their metadata unvalidated, so check it there as well.

Have tun count these events per device as rx_csum_sw, exposed via ethtool -S.

[showta: rebased on the has_eth argument of virtio_net_hdr_tnl_to_skb()]
---
 arch/um/drivers/vector_transports.c |  4 ++-
 drivers/net/tap.c                   |  4 +--
 drivers/net/tun.c                   | 44 ++++++++++++++++++++++++++---
 drivers/net/virtio_net.c            |  2 +-
 net/packet/af_packet.c              |  4 +--
 5 files changed, 48 insertions(+), 10 deletions(-)
arch/um/drivers/vector_transports.c +3 -1
index e1fb2a76fdff..8b7548458663 100644
@@ -210,7 +210,9 @@ static int raw_verify_header(
 		return 1;
 
 	skb_set_network_header(skb, ETH_HLEN);
-	virtio_net_hdr_to_skb(skb, vheader, virtio_legacy_is_little_endian());
+	if (virtio_net_hdr_to_skb(skb, vheader,
+			virtio_legacy_is_little_endian()) < 0)
+		return -1;
 	return 0;
 }
 
drivers/net/tap.c +2 -2
index 2f19fb07bb2d..101994870bac 100644
@@ -650,7 +650,7 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,
 
 	if (vnet_hdr_len) {
 		err = tun_vnet_hdr_to_skb(q->flags, skb, &vnet_hdr);
-		if (err) {
+		if (err < 0) {
 			rcu_read_unlock();
 			drop_reason = SKB_DROP_REASON_DEV_HDR;
 			goto err_kfree;
@@ -1090,7 +1090,7 @@ static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp)
 
 	if (vnet_hdr_len) {
 		err = tun_vnet_hdr_to_skb(q->flags, skb, gso);
-		if (err) {
+		if (err < 0) {
 			rcu_read_unlock();
 			goto err_kfree;
 		}
drivers/net/tun.c +40 -4

76 lines not on this page · raw message

drivers/net/virtio_net.c +1 -1
index 1cd64f9e12e7..fb9359ef48ab 100644
@@ -2548,7 +2548,7 @@ static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue *
 	skb_set_network_header(skb, ETH_HLEN);
 	if (virtio_net_hdr_tnl_to_skb(skb, &hdr->tnl_hdr, true, vi->rx_tnl,
 				      vi->rx_tnl_csum,
-				      virtio_is_little_endian(vi->vdev))) {
+				      virtio_is_little_endian(vi->vdev)) < 0) {
 		net_warn_ratelimited("%s: bad gso: type: %x, size: %u, flags %x tunnel %d tnl csum %d\n",
 				     dev->name, hdr->hdr.gso_type,
 				     hdr->hdr.gso_size, hdr->hdr.flags,
net/packet/af_packet.c +2 -2
index e0ebd0627509..930e85e4bf73 100644
@@ -2941,7 +2941,7 @@ tpacket_error:
 		}
 
 		if (has_vnet_hdr) {
-			if (virtio_net_hdr_to_skb(skb, &vnet_hdr, vio_le())) {
+			if (virtio_net_hdr_to_skb(skb, &vnet_hdr, vio_le()) < 0) {
 				tp_len = -EINVAL;
 				goto tpacket_error;
 			}
@@ -3164,7 +3164,7 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
 
 	if (vnet_hdr_sz) {
 		err = virtio_net_hdr_to_skb(skb, &vnet_hdr, vio_le());
-		if (err)
+		if (err < 0)
 			goto out_free;
 		len += vnet_hdr_sz;
 	}
Signature · 1 line
2.54.0 (Apple Git-157)
#2 lore raw

RFC PATCH v10 2/6 um: vector: trim skbs before verifying the header

In reply to Showta Ishizaki

From: Michael S. Tsirkin <[email protected]>

The vector receive path over-allocates the skb and only trims it to the received length after the transport header is verified.

A follow up patch will complete the packet checksum in software within virtio_net_hdr_to_skb so we need the packet trimmed (otherwise the checksum would be computed incorrectly over the unreceived tail of the buffer).

Trim first, so the import sees the packet as it was received.

---
 arch/um/drivers/vector_kern.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/um/drivers/vector_kern.c +3 -3
index e902e78c5ff6..828bd61995b8 100644
@@ -873,6 +873,7 @@ static int vector_legacy_rx(struct vector_private *vp)
 
 	if (skb != NULL) {
 		if (pkt_len > vp->header_size) {
+			pskb_trim(skb, pkt_len - vp->rx_header_size);
 			if (vp->header_size > 0) {
 				header_check = vp->verify_header(
 					vp->header_rxbuffer, skb, vp);
@@ -887,7 +888,6 @@ static int vector_legacy_rx(struct vector_private *vp)
 					skb->ip_summed = CHECKSUM_UNNECESSARY;
 				}
 			}
-			pskb_trim(skb, pkt_len - vp->rx_header_size);
 			skb->protocol = eth_type_trans(skb, skb->dev);
 			vp->dev->stats.rx_bytes += skb->len;
 			vp->dev->stats.rx_packets++;
@@ -989,6 +989,8 @@ static int vector_mmsg_rx(struct vector_private *vp, int budget)
 	for (i = 0; i < packet_count; i++) {
 		skb = (*skbuff_vector);
 		if (mmsg_vector->msg_len > vp->header_size) {
+			pskb_trim(skb,
+				mmsg_vector->msg_len - vp->rx_header_size);
 			if (vp->header_size > 0) {
 				header_check = vp->verify_header(
 					mmsg_vector->msg_hdr.msg_iov->iov_base,
@@ -1013,8 +1015,6 @@ static int vector_mmsg_rx(struct vector_private *vp, int budget)
 					skb->ip_summed = CHECKSUM_UNNECESSARY;
 				}
 			}
-			pskb_trim(skb,
-				mmsg_vector->msg_len - vp->rx_header_size);
 			skb->protocol = eth_type_trans(skb, skb->dev);
 			/*
 			 * We do not need to lock on updating stats here
Signature · 1 line
2.54.0 (Apple Git-157)
#3 lore raw

RFC PATCH v10 3/6 virtio_net: parse tunnel csum_start little-endian

In reply to Showta Ishizaki

From: Michael S. Tsirkin <[email protected]>

All tunnel related virtio features are > 32, so none of this functionality can be enabled on legacy mixed-endian device.

Accordingly, all tunnel header fields, csum_start included, are little-endian.

The nested virtio net header import parses csum_start that way, but the inner transport offset is parsed with the endianness specified by the interface.

If tun is misconfigured to legacy big-endian mode while the tunnel features are enabled, the two parses of this field disagree, and the value stored in skb->inner_transport_header - which is only bounds-checked from below - can point past the end of the packet, where GSO segmentation later dereferences it.

To fix, parse the field little-endian in both places.

Fixes a2fb4bc4e2a6 ("net: implement virtio helpers to handle UDP GSO tunneling.")
Cc Paolo Abeni <[email protected]>
---
 include/linux/virtio_net.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/virtio_net.h +1 -1
index fc9667176f23..3135dae031a4 100644
@@ -490,7 +490,7 @@ virtio_net_hdr_tnl_to_skb(struct sk_buff *skb,
 	inner_l3min = virtio_l3min(inner_isv6);
 	outer_l3min = ETH_HLEN + virtio_l3min(outer_isv6);
 
-	inner_th = __virtio16_to_cpu(little_endian, hdr->csum_start);
+	inner_th = __virtio16_to_cpu(true, hdr->csum_start);
 	inner_nh = le16_to_cpu(vhdr->inner_nh_offset);
 	outer_th = le16_to_cpu(vhdr->outer_th_offset);
 	if (outer_th < outer_l3min ||
Signature · 1 line
2.54.0 (Apple Git-157)
#4 lore raw

RFC PATCH v10 4/6 net: resolve uncommon partial checksums at ingress

In reply to Showta Ishizaki

From: Michael S. Tsirkin <[email protected]>

As reported first by Paulos Yibelo <[email protected]> (after which others, credited below, found more ways the bug can affect users) a virtio net header can place CHECKSUM_PARTIAL at any in-packet offset. If that state is retained, later checksum completion can modify the packet after protocol processing has made decisions from it.

Let's fix that: let's validate csum_start and csum_offset and only keep CHECKSUM_PARTIAL when csum_start and csum_offset match a parsed TCP or UDP header, while handling VLAN headers, IPv4 options and IPv6 extension headers. Complete other non-GSO checksums immediately, before any protocol processing runs.

We can't easily "complete the checksum" for GSO we don't recognize - GSO assumes knowledge of the protocol. We could segment ignoring the checksum then do whatever csum_start and csum_offset say, but that seems like an overkill for something that shouldn't happen.

There's some risk here:
- non-gso packets which can't be parsed get checksum calculated in software, adding performance overhead
- gso packets which can't be parsed get dropped

Note that for zero copy, userspace can change the data after the checksum has been calculated. This is fundamental to zero copy by design, and the only outcome is a bad checksum and a packet drop.

To mitigate the risks, a later patch will add a sysctl knob to bypass the resolution. This is reasonable if the setup is such that there is no protocol processing at all.

UDP tunnel headers get the same handling on the inner header.

Packets without VIRTIO_NET_HDR_F_NEEDS_CSUM are unaffected.

Note a fundamental limitation of this approach: an nf/BPF program that tweaks the payload of a packet in flight may rely on the checksum being fixed up later, assuming the packet carries CHECKSUM_PARTIAL.

For metadata that fails validation the checksum
is now completed at input, so such edits leave a stale checksum and the packet is dropped at the receiver. Protocols whose transport header we cannot parse (anything but TCP and UDP) are affected even when the metadata is self-consistent.

The sysctl added later in the series is the escape hatch for these setups, but it is a very blunt tool.

Fixes f43798c27684 ("tun: Allow GSO using virtio_net_hdr")

[showta: rebased, the parsing helpers are in the tree already; has_eth
passed to virtio_net_hdr_to_skb() instead of using the device type; the tnl_csum_negotiated check kept ahead of the validation; a tunnel packet with bad inner metadata is dropped here, the next patch handles the ones too small to segment; v9: derive has_eth from dev_has_header() so TUNSETLINK cannot desync it from the frame layout, and record skb->protocol during validation so a later set_proto() cannot relabel the frame]

---
 arch/um/drivers/vector_transports.c |   2 +-
 drivers/net/tun_vnet.h              |   4 +-
 include/linux/virtio_net.h          | 140 ++++++++++++++++++++++++++--
 net/packet/af_packet.c              |  19 +++-
 4 files changed, 153 insertions(+), 12 deletions(-)
arch/um/drivers/vector_transports.c +1 -1
index 8b7548458663..2dc4cf992877 100644
@@ -210,7 +210,7 @@ static int raw_verify_header(
 		return 1;
 
 	skb_set_network_header(skb, ETH_HLEN);
-	if (virtio_net_hdr_to_skb(skb, vheader,
+	if (virtio_net_hdr_to_skb(skb, vheader, true,
 			virtio_legacy_is_little_endian()) < 0)
 		return -1;
 	return 0;
drivers/net/tun_vnet.h +3 -1
index 3364e08dd6e0..5411258eeef6 100644
@@ -180,7 +180,9 @@ static inline int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
 static inline int tun_vnet_hdr_to_skb(unsigned int flags, struct sk_buff *skb,
 				      const struct virtio_net_hdr *hdr)
 {
-	return virtio_net_hdr_to_skb(skb, hdr, tun_vnet_is_little_endian(flags));
+	/* Only tap.c, where a frame always starts with an Ethernet header. */
+	return virtio_net_hdr_to_skb(skb, hdr, true,
+				     tun_vnet_is_little_endian(flags));
 }
 
 /*
include/linux/virtio_net.h +132 -8

195 lines not on this page · raw message

net/packet/af_packet.c +17 -2
index 930e85e4bf73..1362da6232ab 100644
@@ -2778,6 +2778,21 @@ 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.
+ */
+static int packet_vnet_hdr_to_skb(struct sk_buff *skb,
+				  const struct virtio_net_hdr *vnet_hdr)
+{
+	bool has_eth = dev_has_header(skb->dev) &&
+		       (skb->dev->type == ARPHRD_ETHER ||
+			skb->dev->type == ARPHRD_LOOPBACK);
+
+	return virtio_net_hdr_to_skb(skb, vnet_hdr, has_eth, vio_le());
+}
+
 static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
 {
 	struct sk_buff *skb = NULL;
@@ -2941,7 +2956,7 @@ tpacket_error:
 		}
 
 		if (has_vnet_hdr) {
-			if (virtio_net_hdr_to_skb(skb, &vnet_hdr, vio_le()) < 0) {
+			if (packet_vnet_hdr_to_skb(skb, &vnet_hdr) < 0) {
 				tp_len = -EINVAL;
 				goto tpacket_error;
 			}
@@ -3163,7 +3178,7 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
 		skb->no_fcs = 1;
 
 	if (vnet_hdr_sz) {
-		err = virtio_net_hdr_to_skb(skb, &vnet_hdr, vio_le());
+		err = packet_vnet_hdr_to_skb(skb, &vnet_hdr);
 		if (err < 0)
 			goto out_free;
 		len += vnet_hdr_sz;
Signature · 1 line
2.54.0 (Apple Git-157)
#5 lore raw

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

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [Medium] net: L2 header and nhoff mismatch in packet_vnet_hdr_to_skb() and l3_start() - [Medium]…

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [Medium] net: L2 header and nhoff mismatch in packet_vnet_hdr_to_skb() and l3_start()
- [Medium] virtio_net: unrestored skb->protocol in virtio_net_hdr_to_skb() on mismatch
- [Medium] virtio_net: sub-MSS packets with gso_type bypass mismatch rejection
- [Medium] virtio_net: virtio_net_hdr_to_skb() restores stale transport_header on frags
--

commit 76b1117821e72d51ddb2945bbe6b430d1606893a
Author: Michael S. Tsirkin <[email protected]>

net: resolve uncommon partial checksums at ingress

This commit validates virtio_net_hdr csum_start and csum_offset against parsed TCP and UDP headers on ingress, completing non-GSO checksums in software and dropping GSO packets when the checksum metadata does not match.

Quoted 18 lines
include/linux/virtio_net.h +6 -0
index 3135dae031a4d..714b20acd38f0 100644
@@ -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)) {

include/linux/virtio_net.h virtio_net_l3_start(struct sk_buff *skb, bool has_eth, int *nhoff, · line 154

Quoted 8 lines
include/linux/virtio_net.h +2 -0
@@ -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]
Does setting skb->protocol here leave it mutated when virtio_net_csum_partial_ok() subsequently fails?

virtio_net_l3_start() writes skb->protocol before VLAN, L3, and L4 header validation completes, and virtio_net_hdr_to_skb() only restores skb->transport_header before falling back to software checksum completion in __virtio_net_resolve_csum_partial_mismatch(). On an AF_PACKET socket sent with proto == htons(ETH_P_ALL) (where packet_snd() and tpacket_fill_skb() intentionally leave skb->protocol as 0), a failed validation leaves skb->protocol polluted with an unvalidated value (such as htons(ETH_P_ALL) on SOCK_DGRAM) and causes packet_parse_headers() to skip dev_parse_header_protocol(skb) on SOCK_RAW.

Quoted 3 lines
include/linux/virtio_net.h +0 -0
@@ -190,6 +198,77 @@ virtio_net_l4_start(struct sk_buff *skb, unsigned int nhoff, __be16 proto,
 	return false;
 }

[ ... ]

include/linux/virtio_net.h virtio_net_l4_start(struct sk_buff *skb, unsigned int nhoff, __be16 proto, · line 270

Quoted 12 lines
include/linux/virtio_net.h +12 -0
+/*
+ * 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]
Can a packet with hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE bypass this GSO rejection when its payload does not exceed hdr->gso_size?

In __virtio_net_hdr_to_skb(), shinfo->gso_size is only populated when skb->len - nh_off > gso_size, so skb_is_gso(skb) is false for a packet with a non-zero hdr->gso_type and a large gso_size. When virtio_net_csum_partial_ok() fails on such a packet, completing the checksum and returning 1 instead of -EINVAL allows AF_PACKET callers to bypass the !vnet_hdr.gso_type MTU check in packet_snd() and tpacket_snd(), and if skb->protocol is still 0 (for example when virtio_net_l3_start() failed before setting it), the subsequent virtio_net_hdr_set_proto() call still relabels skb->protocol from the unvalidated vnet_hdr.gso_type.

Quoted 29 lines
include/linux/virtio_net.h +20 -2
@@ -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;

include/linux/virtio_net.h static inline int __virtio_net_hdr_to_skb(struct sk_buff *skb, · line 450

Quoted 10 lines
include/linux/virtio_net.h +9 -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 leave skb->transport_header pointing at a stale offset from a previous packet on IFF_NAPI_FRAGS TAP devices?

When tun_get_user() allocates an skb via tun_napi_alloc_frags() -> napi_get_frags(), napi_reuse_skb() recycles tfile->napi.skb after a GRO merge without resetting skb->transport_header. Previously, skb_partial_csum_set() always overwrote that stale offset when VIRTIO_NET_HDR_F_NEEDS_CSUM was set. Restoring old_transport on mismatch re-exposes the previous frame's transport_header offset, which makes skb_transport_header_was_set(skb) true and causes the subsequent skb_probe_transport_header(skb) in tun_get_user() to skip probing the new frame. Unsetting skb->transport_header to ~0U on mismatch would avoid keeping the stale offset.

[ ... ]

Quoted 12 lines
net/packet/af_packet.c +4 -0
index 930e85e4bf739..1362da6232abb 100644
@@ -2778,6 +2778,21 @@ 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.

net/packet/af_packet.c static int tpacket_parse_header(struct packet_sock *po, void *frame, · line 2794

Quoted 10 lines
net/packet/af_packet.c +10 -0
+ */
+static int packet_vnet_hdr_to_skb(struct sk_buff *skb,
+				  const struct virtio_net_hdr *vnet_hdr)
+{
+	bool has_eth = dev_has_header(skb->dev) &&
+		       (skb->dev->type == ARPHRD_ETHER ||
+			skb->dev->type == ARPHRD_LOOPBACK);
+
+	return virtio_net_hdr_to_skb(skb, vnet_hdr, has_eth, vio_le());
+}

[Severity: Medium]
Does checking skb->dev->type here still allow TUNSETLINK on an IFF_TAP device (or any non-Ethernet device with hard_header_len > 0) to skip L2 header validation?

__tun_chr_ioctl() allows TUNSETLINK on an IFF_TAP device as well, which changes dev->type while leaving dev->header_ops as &eth_header_ops and dev->hard_header_len as ETH_HLEN. On such a TAP device, packet_snd() and tpacket_fill_skb() still place a 14-byte Ethernet header at the start of skb->data and set skb_network_offset(skb) to 14, but has_eth evaluates to false. Because the !has_eth branch of virtio_net_l3_start() starts at *nhoff = skb_network_offset(skb) without requiring *nhoff == 0, it skips the leading 14 bytes without checking eth_hdr(skb)->h_proto or VLAN tags, sets skb->protocol from the IP version nibble at offset 14 (causing packet_parse_headers() to skip dev_parse_header_protocol()), and keeps CHECKSUM_PARTIAL and GSO enabled.

In addition, setting has_eth = true for ARPHRD_LOOPBACK allows virtio_net_l3_start() to validate VLAN-tagged frames and retain CHECKSUM_PARTIAL, whereas packet_parse_headers() only advances skb->network_header past VLAN tags when skb->dev->type == ARPHRD_ETHER.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#6 lore raw

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

Thanks. I tried each of these in a VM, on the base of the series, on v10 and on v11.

The has_eth one is real. On a tap that TUNSETLINK made ARPHRD_NONE, sending through AF_PACKET with a vnet header, a frame with EtherType 0x88cc and an IPv4/UDP header after it kept CHECKSUM_PARTIAL and reached __dev_queue_xmit() as ETH_P_IP, and an 802.1Q-tagged IPv4/UDP frame had its valid partial checksum completed. v11 also treats a device with eth_header_ops as having an Ethernet header; with that the first is completed and stays 0x88cc, and the second keeps CHECKSUM_PARTIAL. Plain Ethernet frames came out the same on both, since packet_snd() puts the network header after the link-layer header. A tagged frame sent on lo left the same way on all three kernels, so I left ARPHRD_LOOPBACK as it is.

For skb->protocol after a failed validation, the SOCK_DGRAM case does not arise, as PACKET_VNET_HDR is accepted on SOCK_RAW only. On a tap and on an IFF_TUN device, from a socket bound to ETH_P_ALL, skb->protocol came out the same with and without the series, whether validation passed or failed. I did find one difference, on a vlan device sent a frame that carries a tag of its own: without the series it leaves as ETH_P_IP with the network header on the tag, with the series as ETH_P_8021Q with the network header after the tag, pass or fail. That is what a plain Ethernet device gives for the same frame without the series too, and what the comment at the fill-in is after, so I left it.

A packet with a gso_type that is no larger than gso_size was not GSO before the series either. The same frames were accepted, larger than the MTU as well, and left with the same skb->protocol on all three; a csum_offset that does not fit the gso_type is still refused.

For the stale transport header, tun calls napi_complete() after every write, and as far as I can see nothing on that path arms the GRO flush timeout. With gro_flush_timeout and napi_defer_hard_irqs set on the device no frame was merged, and the transport offset of a mismatched frame came out right each time. A frame with no NEEDS_CSUM leaves the transport header alone without the series as well, so I left this too.

Showta

#7 lore raw

RFC PATCH v10 5/6 net: virtio: compute the outer checksum of non-GSO tunnel packets

In reply to Showta Ishizaki

A packet may be tagged as UDP tunnel GSO and still be too small to segment. Then nothing consumes the pseudo-header seed the sender left in the outer UDP checksum. The GSO layer is not run, and transmit checksum offload completes only the inner checksum, so the seed goes out as the outer checksum.

Zeroing it is not an option either: IPv6 receivers drop a zero UDP checksum unless the tunnel opted in (RFC 6935, RFC 6936).

Compute the outer checksum when VIRTIO_NET_HDR_F_UDP_TUNNEL_CSUM asks for one: with local checksum offload while the inner checksum is still partial, or by a full sum once the inner one was completed in software because its metadata did not match the packet. Such packets no longer need to be dropped.

The addresses come from the outer header in the frame and the length from the UDP header. udp_set_csum() is not used: it is built with CONFIG_INET, which virtio_net does not depend on.

Fixes a2fb4bc4e2a6 ("net: implement virtio helpers to handle UDP GSO tunneling.")
---
 include/linux/virtio_net.h | 113 ++++++++++++++++++++++++++++++++++---
 1 file changed, 106 insertions(+), 7 deletions(-)
include/linux/virtio_net.h +106 -7
index 714b20acd38f..6fe0c5921717 100644
@@ -8,8 +8,10 @@
 #include <linux/netdevice.h>
 #include <linux/udp.h>
 #include <net/ip.h>
+#include <net/ip6_checksum.h>
 #include <net/ipv6.h>
 #include <net/tcp.h>
+#include <net/udp.h>
 #include <uapi/linux/tcp.h>
 #include <uapi/linux/virtio_net.h>
 
@@ -553,6 +555,72 @@ virtio_net_tnl_offsets_ok(struct sk_buff *skb, bool has_eth, bool outer_isv6,
 	return ip_proto == IPPROTO_UDP && outer_th == thoff;
 }
 
+/* Fill in the outer UDP checksum of a tunnel packet that is not GSO.
+ * While the inner checksum is still CHECKSUM_PARTIAL this is local
+ * checksum offload, as udp_set_csum() does for udp_tunnel_xmit_skb(),
+ * and needs the transport header at @outer_th.  Once the inner
+ * checksum has been completed the datagram is summed in full.
+ *
+ * The pseudo-header comes from the frame, not from the seed the sender
+ * put in uh->check: the addresses of the outer IP header and the
+ * length of the UDP header, which skb->len exceeds when the frame is
+ * padded.
+ *
+ * udp_set_csum() itself is not used: it is built with CONFIG_INET,
+ * which virtio_net does not depend on.
+ */
+static inline int
+virtio_net_tnl_set_outer_csum(struct sk_buff *skb, bool has_eth,
+			      bool outer_isv6, unsigned int outer_th)
+{
+	unsigned int thoff;
+	struct udphdr *uh;
+	int ip_proto, nhoff;
+	__be16 proto;
+	__wsum csum;
+	int len, err;
+
+	/* virtio_net_tnl_offsets_ok() has passed; this finds nhoff. */
+	if (!virtio_net_l3_start(skb, has_eth, &nhoff, &proto) ||
+	    !virtio_net_l4_start(skb, nhoff, proto, &thoff, &ip_proto))
+		return -EINVAL;
+
+	err = skb_ensure_writable(skb, outer_th + sizeof(*uh));
+	if (err)
+		return err;
+
+	uh = (struct udphdr *)(skb->data + outer_th);
+	len = ntohs(uh->len);
+	uh->check = 0;
+	if (skb->ip_summed == CHECKSUM_PARTIAL) {
+		csum = lco_csum(skb);
+	} else {
+		/* Sum the datagram alone: uh->len, not skb->len - outer_th,
+		 * which also counts non-zero Ethernet padding and so
+		 * miscomputes the outer checksum.  len is from the frame,
+		 * so bound it before reading that many bytes.
+		 */
+		if (len < sizeof(*uh) || len > skb->len - outer_th)
+			return -EINVAL;
+		csum = skb_checksum(skb, outer_th, len, 0);
+	}
+
+	if (outer_isv6) {
+		const struct ipv6hdr *ip6h = (void *)(skb->data + nhoff);
+
+		uh->check = udp_v6_check(len, &ip6h->saddr, &ip6h->daddr,
+					 csum);
+	} else {
+		const struct iphdr *iph = (void *)(skb->data + nhoff);
+
+		uh->check = udp_v4_check(len, iph->saddr, iph->daddr, csum);
+	}
+	if (uh->check == 0)
+		uh->check = CSUM_MANGLED_0;
+
+	return 0;
+}
+
 /*
  * Return value:
  * 0 - success
@@ -568,6 +636,7 @@ virtio_net_hdr_tnl_to_skb(struct sk_buff *skb,
 			  bool little_endian)
 {
 	const struct virtio_net_hdr *hdr = (const struct virtio_net_hdr *)vhdr;
+	typeof(skb->transport_header) old_transport = skb->transport_header;
 	unsigned int inner_nh, outer_th, inner_th;
 	unsigned int inner_l3min, outer_l3min;
 	u8 gso_inner_type, gso_tunnel_type;
@@ -639,15 +708,26 @@ virtio_net_hdr_tnl_to_skb(struct sk_buff *skb,
 
 	inner_proto = inner_isv6 ? htons(ETH_P_IPV6) : htons(ETH_P_IP);
 
-	/* NEEDS_CSUM is set, so this is CHECKSUM_PARTIAL.  A packet whose
-	 * inner metadata does not match cannot be segmented, and completing
-	 * the inner checksum alone would leave the outer one to the seed
-	 * the sender put there.
-	 */
+	/* NEEDS_CSUM is set, so this is CHECKSUM_PARTIAL. */
 	if (unlikely(!virtio_net_csum_partial_ok_at_offset(skb, gso_inner_type,
 							   inner_nh,
-							   inner_proto)))
-		return -EINVAL;
+							   inner_proto))) {
+		/* __virtio_net_hdr_to_skb set the transport header to
+		 * csum_start, restore it to a known good value.
+		 */
+		skb->transport_header = old_transport;
+
+		ret = __virtio_net_resolve_csum_partial_mismatch(skb);
+		if (ret > 0 && hdr->flags & VIRTIO_NET_HDR_F_UDP_TUNNEL_CSUM) {
+			int err = virtio_net_tnl_set_outer_csum(skb, has_eth,
+								outer_isv6,
+								outer_th);
+
+			if (err)
+				return err;
+		}
+		return ret;
+	}
 
 	skb_set_inner_protocol(skb, inner_proto);
 	if (hdr->flags & VIRTIO_NET_HDR_F_UDP_TUNNEL_CSUM) {
@@ -661,6 +741,25 @@ virtio_net_hdr_tnl_to_skb(struct sk_buff *skb,
 	skb->inner_mac_header = inner_nh + skb_headroom(skb);
 	skb->transport_header = outer_th + skb_headroom(skb);
 	skb->encapsulation = 1;
+
+	if (!skb_is_gso(skb)) {
+		/* Too small to segment.  Senders do tag small packets with
+		 * GSO headers, e.g. packetdrill sets gso_type and gso_size
+		 * with --mss, whatever the packet size.  Nothing consumes
+		 * the seed the sender left in the outer uh->check without
+		 * GSO, so compute the outer checksum if one was asked for.
+		 */
+		skb_shinfo(skb)->gso_type &= ~(SKB_GSO_UDP_TUNNEL_CSUM |
+					       SKB_GSO_UDP_TUNNEL);
+		if (hdr->flags & VIRTIO_NET_HDR_F_UDP_TUNNEL_CSUM) {
+			ret = virtio_net_tnl_set_outer_csum(skb, has_eth,
+							    outer_isv6,
+							    outer_th);
+			if (ret)
+				return ret;
+		}
+	}
+
 	return 0;
 }
 
Signature · 1 line
2.54.0 (Apple Git-157)
#8 lore raw

RFC PATCH v10 6/6 net: add a sysctl to opt out of virtio net hdr checksum validation

In reply to Showta Ishizaki

From: Michael S. Tsirkin <[email protected]>

The ingress validation as implemented earlier in this series is stricter than what it replaced: GSO
packets whose checksum metadata matches no transport header are dropped, and non-GSO ones get their checksum completed in place. Such packets used to pass with the metadata unvalidated, and with no overhead.

I judge that this should not affect any deployments, but there is still some risk that it does. In case a deployment depends on them anyway, provide a per-netns switch to get the old behavior back:

echo 0 > /proc/sys/net/core/virtio_net_hdr_validate

The default is validating.

Cc Björn Töpel <[email protected]>
Cc Willem de Bruijn <[email protected]>

[showta: rebased; virtio_net_tnl_offsets_ok() is not subject to the knob:
the tree checks the outer headers unconditionally]

---
 Documentation/admin-guide/sysctl/net.rst | 19 +++++++++
 include/linux/virtio_net.h               | 22 +++++++++++
 include/net/netns/core.h                 |  1 +
 net/core/dev.c                           |  7 ++++
 net/core/net_namespace.c                 |  1 +
 net/core/sysctl_net_core.c               | 50 ++++++++++++++++++++++++
 6 files changed, 100 insertions(+)
Documentation/admin-guide/sysctl/net.rst +19 -0
index fe43e8595958..3d89772cb7fe 100644
@@ -246,6 +246,25 @@ processes are dropped unless socket option SOF_TIMESTAMPING_OPT_TSONLY is set.
 Default: 1 (on)
 
 
+virtio_net_hdr_validate
+-----------------------
+
+When processing packets with a virtio net header (through tun, tap, a
+packet socket or a virtio network device) that request checksum offload
+(VIRTIO_NET_HDR_F_NEEDS_CSUM), validate the packet: complete the checksum
+in software, or drop the packet where the header does not match it.
+
+When disabled, the header is trusted not to corrupt the packet, and the
+checksum is offloaded to hardware (or not verified if it never reaches
+hardware). This is only safe if no protocol is processing the packets.
+
+The setting is per network namespace, but only writable by the host
+root: root inside a network namespace created by an unprivileged user
+cannot disable the validation.
+
+Default: 1 (on)
+
+
 wmem_default
 ------------
 
include/linux/virtio_net.h +22 -0
index 6fe0c5921717..9e4b4440d062 100644
@@ -5,6 +5,7 @@
 #include <linux/if_vlan.h>
 #include <linux/ip.h>
 #include <linux/ipv6.h>
+#include <linux/jump_label.h>
 #include <linux/netdevice.h>
 #include <linux/udp.h>
 #include <net/ip.h>
@@ -88,6 +89,18 @@ static inline struct iphdr *virtio_net_ip4_parse(struct sk_buff *skb,
 	return (struct iphdr *)(skb->data + nhoff);
 }
 
+/* Defined in net/core/dev.c, enabled while any network namespace
+ * has net.core.virtio_net_hdr_validate set to 0.
+ */
+DECLARE_STATIC_KEY_FALSE(virtio_net_hdr_validate_disabled_key);
+
+/* The caller must keep the netns of skb->dev alive. */
+static inline bool virtio_net_hdr_validate_disabled(struct sk_buff *skb)
+{
+	return static_branch_unlikely(&virtio_net_hdr_validate_disabled_key) &&
+	       !READ_ONCE(dev_net(skb->dev)->core.sysctl_virtio_net_hdr_validate);
+}
+
 /* Where does the L3 header start, and what protocol is it?
  * With @has_eth, the frame starts with an Ethernet header, possibly
  * followed by VLAN tags. Otherwise the L3 header starts at the network
@@ -208,6 +221,12 @@ virtio_net_csum_partial_ok_at_offset(struct sk_buff *skb, u8 hdr_gso_type,
 	unsigned int thoff;
 	int ip_proto;
 
+	/* Failsafe: a namespace opted out of the validation,
+	 * trust the checksum metadata as before.
+	 */
+	if (virtio_net_hdr_validate_disabled(skb))
+		return true;
+
 	if (!virtio_net_l4_start(skb, nhoff, proto, &thoff, &ip_proto))
 		return false;
 
@@ -251,6 +270,9 @@ virtio_net_csum_partial_ok(struct sk_buff *skb, bool has_eth,
 	int nhoff;
 	__be16 proto;
 
+	if (virtio_net_hdr_validate_disabled(skb))
+		return true;
+
 	if (!virtio_net_l3_start(skb, has_eth, &nhoff, &proto))
 		return false;
 
include/net/netns/core.h +1 -0
index 9ef3d70e5e9c..02ce232fe9a9 100644
@@ -18,6 +18,7 @@ struct netns_core {
 	u8	sysctl_txrehash;
 	u8	sysctl_tstamp_allow_data;
 	u8	sysctl_bypass_prot_mem;
+	u8	sysctl_virtio_net_hdr_validate;
 
 #ifdef CONFIG_PROC_FS
 	struct prot_inuse __percpu *prot_inuse;
net/core/dev.c +7 -0
index cd0796a1dbfd..bdef4403b7e4 100644
@@ -128,6 +128,7 @@
 #include <linux/if_vlan.h>
 #include <linux/ip.h>
 #include <net/ip.h>
+#include <linux/virtio_net.h>
 #include <net/mpls.h>
 #include <net/route.h>
 #include <linux/ipv6.h>
@@ -2429,6 +2430,12 @@ EXPORT_SYMBOL(tcf_sw_enabled_key);
 
 DEFINE_STATIC_KEY_FALSE(netstamp_needed_key);
 EXPORT_SYMBOL(netstamp_needed_key);
+
+/* Enabled while any network namespace has net.core.virtio_net_hdr_validate
+ * set to 0.
+ */
+DEFINE_STATIC_KEY_FALSE(virtio_net_hdr_validate_disabled_key);
+EXPORT_SYMBOL(virtio_net_hdr_validate_disabled_key);
 #ifdef CONFIG_JUMP_LABEL
 static atomic_t netstamp_needed_deferred;
 static atomic_t netstamp_wanted;
net/core/net_namespace.c +1 -0
index 217021eeab13..8c61892d94c6 100644
@@ -401,6 +401,7 @@ static __net_init void preinit_net_sysctl(struct net *net)
 	net->core.sysctl_optmem_max = 128 * 1024;
 	net->core.sysctl_txrehash = SOCK_TXREHASH_ENABLED;
 	net->core.sysctl_tstamp_allow_data = 1;
+	net->core.sysctl_virtio_net_hdr_validate = 1;
 	net->core.sysctl_txq_reselection = msecs_to_jiffies(1000);
 }
 
net/core/sysctl_net_core.c +50 -0

74 lines not on this page · raw message

Signature · 1 line
2.54.0 (Apple Git-157)
#9 lore raw
In reply to Showta Ishizaki
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:

- 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.

*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.

Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.
#10 lore raw

Thanks. This is Michael's series; I'm carrying it, and the issue it hardens is not theoretical and was not found by an LLM -- it was first reported by Paulos Yibelo, with further variants from the people credited in patch 4.

I triggered it at runtime. Injecting, through a tap with a virtio_net_hdr, a frame whose csum_start/csum_offset point away from the real transport checksum, a kprobe on __dev_queue_xmit showed the skb reaching transmit still CHECKSUM_PARTIAL with csum_start inside the payload; the stack then completes the checksum at that offset, past the protocol processing that already read those bytes. With the series the metadata is validated at ingress and such a frame is resolved or dropped.

For patch 5, Chris Arges triggered a wrong outer UDP checksum with non-zero Ethernet padding and I reproduced it for a v4 and a v6 outer; the v10 I posted folds in his fix.

I can put these symptoms in the cover letter or the commit messages on the next repost if that would help.

Showta

#11 lore raw
Quoted 14 lines · Showta Ishizaki

Thanks. This is Michael's series; I'm carrying it, and the issue it hardens is not theoretical and was not found by an LLM -- it was first reported by Paulos Yibelo, with further variants from the people credited in patch 4.

I triggered it at runtime. Injecting, through a tap with a virtio_net_hdr, a frame whose csum_start/csum_offset point away from the real transport checksum, a kprobe on __dev_queue_xmit showed the skb reaching transmit still CHECKSUM_PARTIAL with csum_start inside the payload; the stack then completes the checksum at that offset, past the protocol processing that already read those bytes. With the series the metadata is validated at ingress and such a frame is resolved or dropped.

Quoted 3 lines

For patch 5, Chris Arges triggered a wrong outer UDP checksum with non-zero Ethernet padding and I reproduced it for a v4 and a v6 outer; the v10 I posted folds in his fix.

Just to put things into perspective, we are talking about users who already can inject packets with bad checksums.

Quoted 4 lines

I can put these symptoms in the cover letter or the commit messages on the next repost if that would help.

Showta

#12 lore raw
In reply to Showta Ishizaki
Sashiko has finished reviewing this 6-patch series:

Sashiko has finished reviewing this 6-patch series:

- Patches 1-3, 5, and 6: no issues found.
- Patch 4 ("net: resolve uncommon partial checksums at ingress"): 4 potential issues (4 Medium)

A detailed review has been sent as a separate reply to patch 4.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]