Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=182.48.54.220 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795622828; b=0hQCvdW1b1QDZyZR9rFI86PPVQv1D6VMOXqJCs2MqZp3Uw8KDEVrrr475d5MyQ/Mh/sK JbdiJD8620X+pFX9kxlbIT04AVnipp6JnHP1h+Wbprr0EnHCZlanwa+yrSpLP/JGBOZmB owN3jEpaDaUZyGtCVDt6G/BWxf9Sjvxa+BXmYlfZmp5V0MD9bAQg7xVw69g1jEjlr/1ey XzeOXE9WHfbA6KzG7gfIcf8suNh/YCB25uXOQfGZ+UD9HyYUfTErqXoGcDiFfMmY4fKHX fkir7WbnZCr5Na/iC+mvkbxAAJgtSfAIYlupjktKJRTPe/ilNLQBTg07RsKtJhwwL5Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795622828; h=DMARC-Filter:Received:From:To:Cc:Subject:Date:Message-ID:In-Reply-To: References:MIME-Version:Content-Type:Content-Transfer-Encoding; bh=Dmy2LgHJEV7Mn+snEVPLXuYdS+Vdto5ilTP9Oy67R00=; b=eAuCVY5NqRgJOhI0wneONZZyfP2w3JE7FXLrGiSxbp4SpW0ECNkyz8QFjcVbKFB2nrKr TtlkC5VG1cGOZyAozI2hSUbRZ1fy2o6BZ0wAtT6rM9j0iFimNcXk9ETmqTFvHZTjc9Gj2 TpzCI9hOGAyEgKvrZYjmNPx9OJCEOEuARO2QnQORa47roF2o1x/vt8uefSkUOvqgQln86 79FjfkBU8JL3MEK4GtITDAgqelmzYQYz32lkvndv57XFp/XE1gkan8cXUZZfW4qqT4uIF S3z7Xmojwx+T6JkhH7osbvbOB06t3kzBTf+IUEs+1awdPaZMLQA1/fZOc7i42fFgmPA== 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 Received: from bambi.snowrabbit.org (bambi.snowrabbit.org [182.48.54.220]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 05B3F606A685; Wed, 25 Nov 2026 16:07:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 05B3F606A685 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=snowrabbit.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=snowrabbit.org Received: by bambi.snowrabbit.org (Postfix, from userid 3001) id BE2F3C9372; Thu, 26 Nov 2026 01:07:07 +0900 (JST) From: Showta Ishizaki To: "Michael S. Tsirkin" , Willem de Bruijn , Jason Wang , netdev@vger.kernel.org Cc: Eric Dumazet , Jakub Kicinski , Paolo Abeni , "David S. Miller" , Andrew Lunn , Simon Horman , =?utf-8?q?Eugenio_P=C3=A9rez?= , Xuan Zhuo , Richard Weinberger , Anton Ivanov , Johannes Berg , Jonathan Corbet , Shuah Khan , Randy Dunlap , Chris Arges , Paulos Yibelo , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , virtualization@lists.linux.dev, linux-um@lists.infradead.org, linux-doc@vger.kernel.org, Showta Ishizaki Subject: [PATCH RFC net v8 6/6] net: add a sysctl to opt out of virtio net hdr checksum validation Date: Thu, 26 Nov 2026 01:06:47 +0900 Message-ID: <20261126.rfcv8.99797.6.zakinko@snowrabbit.org> In-Reply-To: <20261126.rfcv8.99797.0.zakinko@snowrabbit.org> References: <20261126.rfcv8.99797.0.zakinko@snowrabbit.org> X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: "Michael S. Tsirkin" 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 Cc: Willem de Bruijn Signed-off-by: Michael S. Tsirkin [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 --- 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 fe43e8595..3d89772cb 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 5d5049418..234631b3e 100644 --- a/include/linux/virtio_net.h +++ b/include/linux/virtio_net.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -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 @@ -200,6 +213,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; @@ -243,6 +262,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 9ef3d70e5..02ce232fe 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 cd0796a1d..bdef4403b 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -128,6 +128,7 @@ #include #include #include +#include #include #include #include @@ -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 217021eea..8c61892d9 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 473c16273..04cd2d758 100644 --- a/net/core/sysctl_net_core.c +++ b/net/core/sysctl_net_core.c @@ -18,6 +18,8 @@ #include #include #include +#include +#include #include #include @@ -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)