Article View: linux.kernel.netdev
Article #3699Re: [PATCH net-next v2 5/7] net: add a sysctl to opt out of virtio net hdr checksum validation
From: Willem de Bruijn
Date: Thu, 15 Oct 2026 17:50
Date: Thu, 15 Oct 2026 17:50
193 lines
15917 bytes
15917 bytes
Michael S. Tsirkin wrote:
> The ingress validation as implemented by the previous
> patch 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_csum_check
>
> The default is validating.
>
> Assisted-by: LLM
> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
> ---
> Documentation/admin-guide/sysctl/net.rst | 15 +++++++
> include/linux/virtio_net.h | 23 ++++++++++
> include/net/netns/core.h | 1 +
> net/core/dev.c | 6 +++
> net/core/net_namespace.c | 1 +
> net/core/sysctl_net_core.c | 54 ++++++++++++++++++++++++
> 6 files changed, 100 insertions(+)
>
> diff --git a/Documentation/admin-guide/sysctl/net.rst b/Documentation/admin-guide/sysctl/net.rst
> index e586e17fc7a5..7f230929c37e 100644
> --- a/Documentation/admin-guide/sysctl/net.rst
> +++ b/Documentation/admin-guide/sysctl/net.rst
> @@ -246,6 +246,21 @@ processes are dropped unless socket option SOF_TIMESTAMPING_OPT_TSONLY is set.
> Default: 1 (on)
>
>
> +virtio_net_hdr_csum_check
> +-------------------------
Make more generic, virtio_net_hdr_validate?
In case we add more validation in the future (as we have in the past),
without having to introduce multiple such knobs.
> +
> +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.
> +
> +Default: 1 (on)
> +
> +
> wmem_default
> ------------
>
> diff --git a/include/linux/virtio_net.h b/include/linux/virtio_net.h
> index 357115ff0ec2..aa2f8ac4657c 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/gso.h>
> @@ -78,6 +79,19 @@ static inline struct iphdr *virtio_net_ip4_parse(struct sk_buff *skb,
> *thoff = nhoff + iph->ihl * sizeof(u32);
> return iph;
> }
> +/* Defined in net/core/dev.c, enabled while any network namespace
> + * has net.core.virtio_net_hdr_csum_check set to 0.
> + */
Should this be a global sysctl, rather than per netns?
Even if per-netns, most likely do not want it writable by netns root
with user namespaces.
That is the equivalent of ns_capable(CAP_NET_RAW), which is the cause
of many crashes due to bad packets ingested by, e.g., a packet socket.
A global sysctl also allows using proc_do_static_key and avoids the
custom logic in virtio_net_hdr_csum_check_sysctl.
> /* Where does the L3 header start, and what protocol is it?
> * Handles Ethernet and VLAN headers; other devices use the
> @@ -152,6 +166,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.
> + */
nit: as before, imho the code here is clear and comment unnecessary.
> + if (virtio_net_hdr_csum_check_disabled(skb))
> + return true;
> +
> if (!virtio_net_l4_start(skb, nhoff, proto, &thoff, &ip_proto))
> return false;
>
> @@ -483,6 +503,9 @@ virtio_net_tnl_offsets_ok(struct sk_buff *skb, bool outer_isv6,
> unsigned int thoff;
> int ip_proto;
>
> + if (virtio_net_hdr_csum_check_disabled(skb))
> + return true;
> +
> if (!virtio_net_l3_start(skb, &nhoff, &proto))
> return false;
> if (outer_isv6 != (proto == htons(ETH_P_IPV6)))
> +/* Defined in net/core/dev.c, declared for the gate in
> + * include/linux/virtio_net.h.
> + */
> +DECLARE_STATIC_KEY_FALSE(virtio_net_hdr_csum_check_disabled_key);
> +
> +/*
> + * Protects changes to virtio_net_hdr_csum_check_disabled_key and each
> + * net->core.sysctl_virtio_net_hdr_csum_check. Readers don't use a lock.
> + */
> +static DEFINE_MUTEX(virtio_net_hdr_csum_check_mutex);
> +
> +static int virtio_net_hdr_csum_check_sysctl(const struct ctl_table *table,
> + int write, void *buffer,
> + size_t *lenp, loff_t *ppos)
> +{
> + struct net *net = (struct net *)table->data;
> + /* checkpatch warns but this can't be const: tmp.data is set below */
> + struct ctl_table tmp = *table;
> + u8 old;
> + int ret;
> +
> + /*
> + * Make sure virtio_net_hdr_csum_check_disabled_key counts zero values of
> + * net->core.sysctl_virtio_net_hdr_csum_check.
> + */
> + mutex_lock(&virtio_net_hdr_csum_check_mutex);
> +
> + old = net->core.sysctl_virtio_net_hdr_csum_check;
> + tmp.data = &net->core.sysctl_virtio_net_hdr_csum_check;
> + ret = proc_dou8vec_minmax(&tmp, write, buffer, lenp, ppos);
> + if (write && !ret && old != net->core.sysctl_virtio_net_hdr_csum_check) {
> + if (net->core.sysctl_virtio_net_hdr_csum_check)
> + static_branch_dec(&virtio_net_hdr_csum_check_disabled_key);
> + else
> + static_branch_inc(&virtio_net_hdr_csum_check_disabled_key);
> + }
> +
> + mutex_unlock(&virtio_net_hdr_csum_check_mutex);
> +
> + return ret;
> +}
> +
> static const struct ctl_table netns_core_table[] = {
> #if IS_ENABLED(CONFIG_RPS)
> {
> @@ -737,6 +780,15 @@ static const struct ctl_table netns_core_table[] = {
> .extra1 = SYSCTL_ZERO,
> .extra2 = SYSCTL_ONE
> },
> + {
> + .procname = "virtio_net_hdr_csum_check",
> + .data = &init_net,
> + .maxlen = sizeof(u8),
> + .mode = 0644,
> + .proc_handler = virtio_net_hdr_csum_check_sysctl,
> + .extra1 = SYSCTL_ZERO,
> + .extra2 = SYSCTL_ONE
> + },
> /* sysctl_core_net_init() will set the values after this
> * to readonly in network namespaces
> */
> @@ -841,6 +893,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_csum_check)
> + static_branch_dec(&virtio_net_hdr_csum_check_disabled_key);
> #if IS_ENABLED(CONFIG_RPS)
> kfree(net->core.rps_default_mask);
> #endif
> --
> MST
>
Technical Details
Message-ID:
<willemdebruijn.kernel.3a7d04e1c306e@gmail.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-346859-linux-kernel-netdev=dewy.ruReferences:
<cover.1792067836.git.mst@redhat.com> <ad05a40681719a579691a1da6413f0dd7064f2a0.1792067836.git.mst@redhat.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-346859-linux-kernel-netdev=dewy.ru
From: willemdebruijn.kernel@gmail.com (Willem de Bruijn)
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH net-next v2 5/7] net: add a sysctl to opt out of virtio
net hdr checksum validation
Date: Thu, 15 Oct 2026 13:50:54 -0400
Organization: Usenet Network
Message-ID: <willemdebruijn.kernel.3a7d04e1c306e@gmail.com>
References: <cover.1792067836.git.mst@redhat.com>
<ad05a40681719a579691a1da6413f0dd7064f2a0.1792067836.git.mst@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain;
charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Info: usenet.network;
logging-data="2602738"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:A9E0GtesyL0+AliGrdeiP8OQTpg= sha256:f9bQdBLGVjnlq8eezMQAsk6n+HkQkpdaHTg6UZssVA8=
sha1:x3npy3j1Lepy9tPAsirp6bkz0jA= sha256:AIb7iGfHWidYR1pLLECf1SPQ4QnZBdZDsaGfUKRKDns=
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 AC99B1103AE; Thu, 15 Oct 2026 22:18:55 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.047
X-Spam-Status: No, score=-3.047 tagged_above=-9999 required=6.31
tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
DKIM_VALID_AU=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.001,
FREEMAIL_FROM=0.001, FSL_HELO_FAKE=0.001,
HEADER_FROM_DIFFERENT_DOMAINS=0.25, 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-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
header.d=gmail.com
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 05ycQr-54EOz;
Thu, 15 Oct 2026 22:18:53 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-346859-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN>
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
by mail.dewy.ru (Postfix) with ESMTPS id B2C9B11039D; Thu, 15 Oct 2026 22:18:43 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.90.174.1])
by sea.lore.kernel.org (Postfix) with ESMTP id 72AA6C590E; Thu, 15 Oct 2026 17:51:21 +0000 (UTC)
X-Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
by smtp.subspace.kernel.org (Postfix) with ESMTP id 1243956C620;
Thu, 15 Oct 2026 17:51:09 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="qQ+6hQ+N"
X-Received: from mail-vk1-f169.google.com (mail-vk1-f169.google.com [209.85.221.169])
(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
(No client certificate requested)
by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC74D560ADD
for <netdev@vger.kernel.org>; Thu, 15 Oct 2026 17:50:57 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.169
X-ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
t=1792086668; cv=none; b=dahrs0QMcNjBdAUorQc/igWbX8jKxdUe54WUmnPEo9jVcGRLuReGK3PYluNSxBbCts0F/vb42KQgpGlYuiYqRt/1O+0YYyI3LOPrwzZt6AWKbpeI1KVxaDTjNiPUxHhtVB+26zG60tcorhE9LBxzRSJzQ+BdGjlGC1/mz+KbXRw=
X-ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
s=arc-20240116; t=1792086668; c=relaxed/simple;
bh=fMqpqs1u8LBcIFnBHT9B79hdJta82WzO1YoJAMI+N7M=;
h=Date:From:To:Cc:Message-ID:In-Reply-To:References:Subject:
MIME-Version:Content-Type; b=tG1VQ/NBdXxjLcsTGIUaPYfR+qqTqGIYQ5zBi70d3ruRQdMEo4nix6kdjP20x2CGow3CLEAL/oCl5O39zFtsW1KwFlv4QXTJLr7Cxwe8wdE4hsbSUIH8gGekGYTTOwzhLv7NDSkU9868iQs6A+aG2tklKQQ3nOhLejhgXqJbd1k=
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=qQ+6hQ+N; arc=none smtp.client-ip=209.85.221.169
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-vk1-f169.google.com with SMTP id 71dfb90a1353d-5d4d307fab6so2568826e0c.1
for <netdev@vger.kernel.org>; Thu, 15 Oct 2026 10:50:57 -0700 (PDT)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20251104; t=1792086656; x=1792691456; darn=vger.kernel.org;
h=content-transfer-encoding:content-type:mime-version:subject
:references:in-reply-to:message-id:cc:to:from:date:from:to:cc
:subject:date:message-id:reply-to:content-type;
bh=i8+0UTRzQqbINpMv2vf6iArLkPondg+9Xp/w3H+DSS4=;
b=qQ+6hQ+N+JP7CD/z5gwoOG894MOrFmKyno0zks6XEUmtUmmiWCRUo+ZVCKlMqR/OBl
dZPn7qvtV6AA+8z4/JsCn/ptrnKc+fqHxF1O3DbfWFjBKILH+YPh3r7zb75tjLC7rH86
Wqrq4oVCk6xlX7iEeF7ZEzreqtWiYq91hvmrTg3kS6pe8a9TqGS5AUV2m6aXGBSOEukk
jb07MC2IijNzuFQW6SV5Nq9LAZKechRgaOfitBq1KtXHhllWWZpj8OOsyfQmPTdXTlq4
edlBAn+7FmE4yLtXrdn4AJqAyb9W95hhjaPhnXPIqRVJ/i5EsRytRDs7r2ygNg2zJW5L
YYMQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1792086656; x=1792691456;
h=content-transfer-encoding:content-type:mime-version:subject
:references:in-reply-to:message-id:cc:to:from:date:x-gm-gg
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to
:content-type;
bh=i8+0UTRzQqbINpMv2vf6iArLkPondg+9Xp/w3H+DSS4=;
b=fqEwDW+Q1YQY2+Wa8lhPHKN6J8o9CiptyxSW5ZK67AW28F0ijEehA4sYnPEaH+42e5
ieKHvyOhxFg/9je3FZkkU/S/YcwVFwnHhsHLw0sGGD2s35ks5IhcJmNX/IPlt8lnGd5Q
w3Qyk6TyCsp/U1yV45qhu8MIzrLbJk6Nn1Hj8bT9UQGEMgiEKhn0o04TW+GSJ48EKTdK
3zEQ4UlMMr9w3k4mGycNtXxbHooycuChphI37Wo8CiyLFvCeD3ryE1P1nSkQWSHc/Rw/
CXHX+gg0wRDDLrGl0+QxyJNqlo0O5uABcYIp0wk0dDFUKBBn/whH7A6Uss8HUzMUs9Y4
neyA==
X-Forwarded-Encrypted: i=1; AKwUvBw0wm6jed0XA63V4w306yVbGk9isy/FnUMEWk3zDhsZb6O3DJR8Khp2XCtDH8PDE5kixNNV88o=@vger.kernel.org
X-Gm-Message-State: AFq9FYKKFNuInnR0ApMbALFf4XLXr1HfVPXiy2jYxOWATGKyOHg5kyxg
5PKZEqvBWkobeW4KDDttH8N4ACVP+EbEMJHM4sGJMQVH4rqOAWzj0efp
X-Gm-Gg: AYBFou1VtlJ1zLYVZXoy2gUl8051xBRGFBMrdTYQiPp0TMXB5D7TwrU49I+E2hQ9EuO
7dUFjDMbvJbQcOnnETvGj6iGMXtv8jMHWfRXXsebrF7KMfIV8W7gn1qKDzvmKLn1PKInz8KdKfJ
NMQ2SUBXpl/rtRQThNe9F7w5K3dBTyAWekZZypFJ+bHPuOPdMGHNlfFsheD60WeEp54JTYOYCvT
q8FyCIDK58gW0J+Cae+XyZyMEf1o2bA71AzmX2ed4Q9dksxj1Lw0xSnUjbKM/9mI3PyhRn+AIbl
/JveuUoZFAw3fPdcQzIxTMx0DIZx2DEFqA7jx8PDMeiT4lrz9zRyBChqaqLpltx2oLklxtJ7Epk
MOtC1I2OZSmp0SqhRfB3yvV4QtrdGkdV3Y7fDskvpaXTmOpMJy3x/QxLGz0lm+YMPLWq6NfDrO3
AHjGLZmV3s5B2kbla2xbuB1zQWWxFwAtOplw7Fm3omeEjI4dsrb5AUCeNlDXJ6HENk6p0ewLORx
hprQD2DUhkT3Yp5tTV7WNuYjd/XtGQ+h4mVSTw9qoo0aPLuZtV7fIG5mRq94rCe2oH6paSx4uBw
KuTtlOtv673pdFEId0et7/+CKvcgA/icsCdMvIot/NK22zsQ
X-Received: by 2002:a05:6122:3d14:b0:5e7:fc09:890a with SMTP id 71dfb90a1353d-5e91f48135bmr6780539e0c.11.1792086656401;
Thu, 15 Oct 2026 10:50:56 -0700 (PDT)
X-Received: from gmail.com (111.46.245.35.bc.googleusercontent.com. [35.245.46.111])
by smtp.gmail.com with ESMTPSA id 71dfb90a1353d-5ea994fcf47sm222175e0c.15.2026.10.15.10.50.55
(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
Thu, 15 Oct 2026 10:50:55 -0700 (PDT)
X-Cc: Willem de Bruijn <willemdebruijn.kernel@gmail.com>,
Jason Wang <jasowangio@gmail.com>,
Eugenio PΓ©rez <eperezma@redhat.com>,
Xuan Zhuo <xuanzhuo@linux.alibaba.com>,
Andrew Lunn <andrew+netdev@lunn.ch>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>,
Paolo Abeni <pabeni@redhat.com>,
netdev@vger.kernel.org,
virtualization@lists.linux.dev,
Rusty Russell <rusty@rustcorp.com.au>,
Max Krasnyansky <maxk@qualcomm.com>,
Jonathan Corbet <corbet@lwn.net>,
Shuah Khan <skhan@linuxfoundation.org>,
Randy Dunlap <rdunlap@infradead.org>,
Simon Horman <horms@kernel.org>,
linux-doc@vger.kernel.org
X-In-Reply-To: <ad05a40681719a579691a1da6413f0dd7064f2a0.1792067836.git.mst@redhat.com>
X-Precedence: bulk
X-Mailing-List: netdev@vger.kernel.org
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>