🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #25603

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

#25603
From: Showta Ishizaki
Date: Wed, 02 Dec 2026 04:00
250 lines
16140 bytes
From: "Michael S. Tsirkin" <mst@redhat.com>

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 <bjorn@kernel.org>
Cc: Willem de Bruijn <willemb@google.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
[showta: rebased; virtio_net_tnl_offsets_ok() is not subject to the knob:
 the tree checks the outer headers unconditionally]
Signed-off-by: Showta Ishizaki <zakinko@snowrabbit.org>
---
 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(+)

diff --git a/Documentation/admin-guide/sysctl/net.rst b/Documentation/admin-guide/sysctl/net.rst
index fe43e8595958..3d89772cb7fe 100644
--- a/Documentation/admin-guide/sysctl/net.rst
+++ b/Documentation/admin-guide/sysctl/net.rst
@@ -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
 ------------
 
diff --git a/include/linux/virtio_net.h b/include/linux/virtio_net.h
index 9fb71a5ee991..198a141d0c02 100644
--- a/include/linux/virtio_net.h
+++ b/include/linux/virtio_net.h
@@ -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;
 
diff --git a/include/net/netns/core.h b/include/net/netns/core.h
index 9ef3d70e5e9c..02ce232fe9a9 100644
--- a/include/net/netns/core.h
+++ b/include/net/netns/core.h
@@ -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;
diff --git a/net/core/dev.c b/net/core/dev.c
index cd0796a1dbfd..bdef4403b7e4 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -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;
diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c
index 217021eeab13..8c61892d94c6 100644
--- a/net/core/net_namespace.c
+++ b/net/core/net_namespace.c
@@ -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);
 }
 
diff --git a/net/core/sysctl_net_core.c b/net/core/sysctl_net_core.c
index 473c162736d6..04cd2d758ccc 100644
--- a/net/core/sysctl_net_core.c
+++ b/net/core/sysctl_net_core.c
@@ -18,6 +18,8 @@
 #include <linux/slab.h>
 #include <linux/sched/isolation.h>
 #include <linux/hex.h>
+#include <linux/virtio_net.h>
+#include <linux/jump_label.h>
 
 #include <net/ip.h>
 #include <net/sock.h>
@@ -693,6 +695,43 @@ static struct ctl_table net_core_table[] = {
 	},
 };
 
+/* Protects changes to virtio_net_hdr_validate_disabled_key and each
+ * net->core.sysctl_virtio_net_hdr_validate.  The packet path reads
+ * the value without a lock.
+ */
+static DEFINE_MUTEX(virtio_net_hdr_validate_mutex);
+
+static int virtio_net_hdr_validate_sysctl(const struct ctl_table *table,
+					  int write, void *buffer,
+					  size_t *lenp, loff_t *ppos)
+{
+	u8 *value = table->data;
+	u8 old, new;
+	int ret;
+
+	/* Root in a user namespace must not be able to weaken the
+	 * validation for the network namespace it controls.
+	 */
+	if (write && !capable(CAP_NET_ADMIN))
+		return -EPERM;
+
+	mutex_lock(&virtio_net_hdr_validate_mutex);
+
+	old = READ_ONCE(*value);
+	ret = proc_dou8vec_minmax(table, write, buffer, lenp, ppos);
+	new = READ_ONCE(*value);
+	if (write && !ret && old != new) {
+		if (new)
+			static_branch_dec(&virtio_net_hdr_validate_disabled_key);
+		else
+			static_branch_inc(&virtio_net_hdr_validate_disabled_key);
+	}
+
+	mutex_unlock(&virtio_net_hdr_validate_mutex);
+
+	return ret;
+}
+
 static const struct ctl_table netns_core_table[] = {
 #if IS_ENABLED(CONFIG_RPS)
 	{
@@ -752,6 +791,15 @@ static const struct ctl_table netns_core_table[] = {
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_ONE
 	},
+	{
+		.procname	= "virtio_net_hdr_validate",
+		.data		= &init_net.core.sysctl_virtio_net_hdr_validate,
+		.maxlen		= sizeof(u8),
+		.mode		= 0644,
+		.proc_handler	= virtio_net_hdr_validate_sysctl,
+		.extra1		= SYSCTL_ZERO,
+		.extra2		= SYSCTL_ONE
+	},
 	/* sysctl_core_net_init() will set the values after this
 	 * to readonly in network namespaces
 	 */
@@ -856,6 +904,8 @@ static __net_exit void sysctl_core_net_exit(struct net *net)
 	tbl = net->core.sysctl_hdr->ctl_table_arg;
 	unregister_net_sysctl_table(net->core.sysctl_hdr);
 	BUG_ON(tbl == netns_core_table);
+	if (!net->core.sysctl_virtio_net_hdr_validate)
+		static_branch_dec(&virtio_net_hdr_validate_disabled_key);
 #if IS_ENABLED(CONFIG_RPS)
 	kfree(net->core.rps_default_mask);
 #endif
-- 
2.54.0 (Apple Git-157)
Technical Details
Message-ID: <20261202.rfcv9.41657.6.zakinko@snowrabbit.org>
Path: mbox-mo1.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!feeder2.feed.ams11.usenet.farm!feeder4.usenet.farm!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375537-linux-kernel-netdev=dewy.ru
References: <20261202.rfcv9.41657.0.zakinko@snowrabbit.org>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!feeder2.feed.ams11.usenet.farm!feeder4.usenet.farm!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375537-linux-kernel-netdev=dewy.ru
From: zakinko@snowrabbit.org (Showta Ishizaki)
Newsgroups: linux.kernel.netdev
Subject: [PATCH RFC net v9 6/6] net: add a sysctl to opt out of virtio net hdr checksum validation
Date: Wed, 02 Dec 2026 13:00:00 +0900
Organization: Usenet Network
Message-ID: <20261202.rfcv9.41657.6.zakinko@snowrabbit.org>
References: <20261202.rfcv9.41657.0.zakinko@snowrabbit.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network; logging-data="1881081"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:C2Mn2+7/9UpwOX/mLNMOnL3ySe4= sha256:9yZDLZCqkCH9MqRl/E9gYjHovcwSYGfywBICFs00oRc= sha1:9h5rggBpyX+3C+yI2HNq0Shu5i8= sha256:fdt1KRC612+sk4ax8UyGytjuVl8V6dsE+90di5NVWsc=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id 63951117551; Wed,  2 Dec 2026 06:50:54 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -2.951
X-Spam-Status: No, score=-2.951 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id aEFU079MOQy6; Wed,  2 Dec 2026 06:50:51 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-375537-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN>
X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=snowrabbit.org
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id 4631A1171E1; Wed,  2 Dec 2026 06:50:47 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id 3F5362FAF6; Wed,  2 Dec 2026 03:50:31 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796183423; b=OtgMPiwgiIGnb43nnGX6EiBBY2RdAzFWJtjI0c59k2WZhXvk6xo0cx2zJJ9dJIVLblUA y5QbyOjOijTnMIFEP3/xRzx1V/qyzw8ZiVVV9UkFVsyk5hOtEtE2uxsOCBNEK/vKy2z9z XYFiZvhsifaWF2KQCUqq+XXRY7TTbSpecMcQjkgH+l5A4/NM7EreEG4QiFnRiJOqbSZ21 PpoyAc+/brZZ6WQKSYSkzj+nfgjE3pFJppoFXLZZQozqRxKIAjuKVbQFcjNdk1fkmqjsn mo2/5P+NS/O8lL/kg68nVvzJZ+tce1ZyJcGxgU2jsDSLESSi2zXp3kkgSaZ36e5qqHw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796183423; h=X-Original-To:Received:DMARC-Filter:Received:From:To:Cc:Subject:Date: Message-ID:In-Reply-To:References:X-Mailing-List:List-Id:List-Post: List-Help:List-Subscribe:List-Unsubscribe:List-Owner:Precedence: MIME-Version:Content-Type:Content-Transfer-Encoding; bh=R5fy4OY2oU9BkdLuDiENENtBCcbNsPJz5YVRL05x5Fs=; b=WDs2XbsNtmdq7VyAVWWMUcOLwjx1UEFb4UdVx+QESkJHYRATUeiCjAuIOZ18e/pg/nm3 +08EdRn86uYD7GhZ+UuYNGevAl7oF1YdWjr/cWdY/eNCyl2kJKauQWoGQIwHFX5hEVupN hDGxDiPvWfqq8VKqqh6uCneExUj8WFrrLgzeSUX5edsEVPhg0dLY+MN05xE2ZgQhCip3/ kj14/LsGoBAWFpLuS7tFJ6qSL6AnddiiAIngDngBizqDAA9DX6hlBoRP1u8dascE6bSDq Yx7qLEhHqc8XXBrbn92/umOBnohwlA4iR3NZmeo/2jEWWwXn0713Naf+j5cKQDGNoUw==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=none header.from=snowrabbit.org; spf=pass smtp.mailfrom=snowrabbit.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7529F60A260D; Wed,  2 Dec 2026 03:50:23 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=182.48.54.220
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796183422; b=UyjXz7vP8A2A35GmmdMIFfiaVyhP8rD7jNJf+V2TGC4yWlY1aCwIDHtxhVHBLjImomEM wKwGkIohvWiPxCP4ouPlcZPCnWIr6GsKhK9d/tgrBq6asyMyNytaAsYG/BmODTdtzPm8D YJBwMhwYB+gSUcMtq3XZZBhMf6E7DQ+gjM9ZbhLtx0UFvfDnAwJlQI7Ibam7ayXx8NmZJ 9jgYu5PJtf7zb3E/XzFbuqSXYxaXy31uupsDdLBt/u6m+qLXNrG+FTFU7tBnVclo5VudE Hz7kdQz6yzOBWSAmaiE/F7AdtMSiyqP/SeDxlC3hpgLpDi1eq5On9E7rjaX41GD/lPQ==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796183422; h=DMARC-Filter:Received:From:To:Cc:Subject:Date:Message-ID:In-Reply-To: References:MIME-Version:Content-Type:Content-Transfer-Encoding; bh=R5fy4OY2oU9BkdLuDiENENtBCcbNsPJz5YVRL05x5Fs=; b=Y2BgPnSnn5rkbXzv6CKP8fQoygS/78YfhCTTkT6q221ii0M/an/YYVaj371o6blmA/TU Mob7+P1P/5G9R3OHGa4Rd4mefv6QLgFcT5x1IoErPoZ20ruqa+SQBzMzm49m0CZW4NQmm e6Od9z+CpZqw4/W+TIdTBXgJSlemzER/W3H1FReqyEvOHkgrSOR9yrURPEkptok3Tvd5j OOeqSsW5y2+c1MYiS46mVhQNnPD7yQn8nZ/aGnMn7N1o+mGgWfZV559dG7A6XCiQGiEo/ c8hACPSs/pRZsTkPSjfVM/fNFl+sVgsKi9Z8tYfbVcVkiwEs1+QsKILjBf3VYbnp4Hw==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=snowrabbit.org; spf=pass smtp.mailfrom=snowrabbit.org; arc=none smtp.remote-ip=182.48.54.220
X-Received: from bambi.snowrabbit.org (bambi.snowrabbit.org [182.48.54.220]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8274D606A6B1; Wed,  2 Dec 2026 03:50:21 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8274D606A6B1
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=snowrabbit.org
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=snowrabbit.org
X-Received: by bambi.snowrabbit.org (Postfix, from userid 3001) id 9BDC3C9361; Wed,  2 Dec 2026 12:50:18 +0900 (JST)
X-Cc: Eric Dumazet <edumazet@kernel.org>, Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>, "David S. Miller" <davem@davemloft.net>, Andrew Lunn <andrew+netdev@lunn.ch>, Simon Horman <horms@kernel.org>, Eugenio Pérez <eperezma@redhat.com>, Xuan Zhuo <xuanzhuo@linux.alibaba.com>, Richard Weinberger <richard@nod.at>, Anton Ivanov <anton.ivanov@cambridgegreys.com>, Johannes Berg <johannes@sipsolutions.net>, Jonathan Corbet <corbet@lwn.net>, Shuah Khan <skhan@linuxfoundation.org>, Randy Dunlap <rdunlap@infradead.org>, Chris Arges <carges@cloudflare.com>, Paulos Yibelo <habte.yibelo@gmail.com>, Björn Töpel <bjorn@kernel.org>, virtualization@lists.linux.dev, linux-um@lists.infradead.org, linux-doc@vger.kernel.org, Showta Ishizaki <zakinko@snowrabbit.org>
X-In-Reply-To: <20261202.rfcv9.41657.0.zakinko@snowrabbit.org>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list