From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1792673916; b=Ic3mHRYQGDr32+SFKdMM4fAbA9x+n0iFDpLPvyxg0knxCqP5+Y2WzQpfmW50oETyLd9j 6ilnCbEK3Z8r29TFfPuVRnBLbiuGrG4pRUdHShmPaSrk+UHIWbrg25wnnWqoJDAyz7JVh CvNwgvfezhgjd7QqKyF43m1DTwNZP6PZaKZmf1FvZnihL7VifoArnJGooauBnrPhG+Jcf DLavR5W1xuqL3WkJ87vlTOf+Ivsfshr+Qa3kZtv2aC8xLYSHMeQSL+w7zfmj7b84Fss81 RvK2iU+JpNwrHjFQOpmkxKfIQaTfWQiulTqOHQ9rbyCCapbBGfC/QYIjipmo2ttITjw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792673916; h=DMARC-Filter:DKIM-Signature:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:X-Received:Received:Date:From: To:Cc:Subject:Message-ID:References:MIME-Version:Content-Type: Content-Disposition:Content-Transfer-Encoding:In-Reply-To:X-Mailer: X-Mutt-Fcc; bh=n2146TWUgdwHvS4zjmu5EqVSZcyKohA+l2jbGNsx8S4=; b=Gs/1o5Wr62OGIHpN414Cb2bN7Bm/lCx590SNYl99TJzD0rCrbSIGg9FGYYOV8JnUxfas bKymnOFdGRLSXMYnGr3/XsMtjBBy4Xd+7S6buM4xwGhOclsZSysGI490FFiI8uHGMiU54 IlVZGrl4hPF0V8JX8I+JEN3+JfefwibbfmEk9jK1unJFRdzMWTAeyIwLDddKXcRjx98TC cDOTsiPQVWv6Cp5f48nTYXayURqxrnxuUBwwMTPfWl8DJj1ZBWLOp9gn7dqgFq+Qn+SKn Tw9ddrLI83ucU9VvDEGW4bY6OXT9HrbeWgzPVWzv4lSpG2UY2A7/9PPL3ZSGSZ/2uBQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=O9or39Vr; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=MpJlP5E9; arc=none smtp.remote-ip=170.10.129.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 80C8760A2604 for ; Thu, 22 Oct 2026 12:58:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 80C8760A2604 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=O9or39Vr; dkim=pass (2048-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=MpJlP5E9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1792673914; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=n2146TWUgdwHvS4zjmu5EqVSZcyKohA+l2jbGNsx8S4=; b=O9or39Vrb3GGgJ2zHzHuonrZjAv10bKSToF5UrlSYxxbsvHB4PoWf4j1dZswiFjsbwZu1S PVPjoF6nWonWZxioyEswxMYaBDEgDF2O09uoD9G8CnGGjC6Kzd61+tn4YAAd3SdhG0dTFN Z7fxwQLrnOBY0F5yan8VMzlz5kQ+T3Y= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-505-gVwlB4I-P_y_iyt3I_GFfQ-1; Thu, 22 Oct 2026 12:58:31 +0000 X-MC-Unique: gVwlB4I-P_y_iyt3I_GFfQ-1 X-Mimecast-MFC-AGG-ID: gVwlB4I-P_y_iyt3I_GFfQ_1792673910 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-48e00f3929bso633525f8f.0 for ; Thu, 22 Oct 2026 05:58:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1792673910; x=1793278710; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :content-type:mime-version:references:message-id:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to:content-type; bh=n2146TWUgdwHvS4zjmu5EqVSZcyKohA+l2jbGNsx8S4=; b=MpJlP5E9duoQMHTdqOlS484UdpyR2kEAttkD0SJ2jlLZ7g/YptXm3vFUqFw2aI+fMd xvBHPckKcFrZLqMxI0oMysbLMWmRcL8rpZ90okYUayMBWd6OmUMjFINxPlrI48FARXHw L749cnhAlhQF4zbcpYio+jfzCXPrgtLhKoUYiZUYLr/K0Utoxt/sngOzH9dwWnEjCbfT nyMG4TL3GD1+VV7EwupFGsyoBhs8cxlVjXOrOdB85Feq66X8lbtSWsLtFGKcq0FL5o+l FvxjKREfjdvbMZnaQdnUapkynHb/cNCERjUjK6HIA7MaVG/cEy6bfCgwQzPK7sznZi68 J/MA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792673910; x=1793278710; h=in-reply-to:content-transfer-encoding:content-disposition :content-type:mime-version:references:message-id:subject:cc:to:from :date:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to:content-type; bh=n2146TWUgdwHvS4zjmu5EqVSZcyKohA+l2jbGNsx8S4=; b=ob84mYXC7kA+8R6A2GusaNDczJzoQbyNHSt0beBRTGozDjLLwqpJG701wMVD9q/1xc v+W97fUlvivJagcgGysuIJsvKbLR6Orv4Q+Nw9dxsgNj4GXtT6qNTSJdXh9zJ/EKM6OZ Zvn55uCqcTCrVtUyZtVAHzNdKPe8z2rNhPdn4U+1Ft81QUyyHQ8RyXgFBXN5uFEpooPU mLVT/dGFHC14aWXie46YDSSpQ+cx+R7YWMRoDmjzA4KcJM87NGiju5szh06BsxLzT2xY WZO+IP4+6CcuD0d1zMQFtGckVQBBblz8Cod/dPEQlpaHPnkdiLnCqOTh4lSt0YPa7A+1 ow8g== X-Gm-Message-State: AFq9FYKxF+vBnY+i1uLhPHeKv22o9wQYxPBwSGfUPnN84WJTQ019YmiY dZvaR1DYdVERPMI02b+tmZC9YGT2Cnt+I4KST1T5NkQHmueJAAd71K5Mh6wMxDuQRJmEj8t+jS4 lSKIOXXoLiP8vdAqAiUgroYvFr0kipZ8RsU8gehvgI2aBm+XwfIdapd88cRgKdylLR1WnA1Mbih gZ8DBfRAz0+3E7BBJeZSk33p+wyaOmLBm5RG96wKKqwo4= X-Gm-Gg: AYBFou2OSiE3811t1fp/ZBlEXzVehzv+NB1+39p0IrP1mxtODdb4sGD7xUQplOcHe2S P1vFKOAWMg6KE+6EUlRld1O0VaQ3ZPgLOOLan8sK9F4e58uimWxwz6ZN2nvUaR0FpgvyixfXXS5 6UrZnVi0i3ayFTdz7P8NL5P10CToFhc7EZ3B+4V3OGVDdN9dbNylPKxleMQi+vtQdQ5nd8yheC7 6Cnj9Ih3xGUyH7fan26yf0CDoK2CQP2LH8p98PT3sfzWma61vrbvq6ySAIFdiFlrgVh2VVP4t/5 Zr2I35e9mPCqLfJNQty7e2ChMTmFfR6tje4LyOBedfgdQgNqY357ypwKuhNPFxeAavg= X-Received: by 2002:a05:600c:34d6:b0:4a2:bbd:ea3a with SMTP id 5b1f17b1804b1-4a20bbdea84mr19221575e9.6.1792673909744; Thu, 22 Oct 2026 05:58:29 -0700 (PDT) X-Received: by 2002:a05:600c:34d6:b0:4a2:bbd:ea3a with SMTP id 5b1f17b1804b1-4a20bbdea84mr19221005e9.6.1792673909044; Thu, 22 Oct 2026 05:58:29 -0700 (PDT) Received: from redhat.com ([2a0d:6fc0:1536:fc00:b838:383:1c7:6e81]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4a203b29907sm65756795e9.3.2026.10.22.05.58.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Oct 2026 05:58:28 -0700 (PDT) Date: Thu, 22 Oct 2026 08:58:24 -0400 From: "Michael S. Tsirkin" To: linux-kernel@vger.kernel.org Cc: Willem de Bruijn , Chris Arges , =?utf-8?B?QmrDtnJuIFTDtnBlbA==?= , Phil Sutter , Jason Wang , Eugenio =?utf-8?B?UMOpcmV6?= , Xuan Zhuo , Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , netdev@vger.kernel.org, virtualization@lists.linux.dev, Max Krasnyansky , Rusty Russell , Willem de Bruijn , Jonathan Corbet , Shuah Khan , Randy Dunlap , Simon Horman , linux-doc@vger.kernel.org Subject: [PATCH net v5 5/7] net: add a sysctl to opt out of virtio net hdr checksum validation Message-ID: <2beba4c8b84c811120b6100bcccd8b90e7458787.1792668082.git.mst@redhat.com> References: X-Mailing-List: linux-kernel@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-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Mailer: git-send-email 2.51.2.2891.g4157995a80.dirty X-Mutt-Fcc: =sent 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_validate The default is validating. Reviewed-by: Björn Töpel Reviewed-by: Willem de Bruijn Signed-off-by: Michael S. Tsirkin --- Notes (changelog): v4->v5: document the netns lifetime requirement (sashiko) Documentation/admin-guide/sysctl/net.rst | 19 +++++++++ include/linux/virtio_net.h | 27 +++++++++++++ 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, 105 insertions(+) diff --git a/Documentation/admin-guide/sysctl/net.rst b/Documentation/admin-guide/sysctl/net.rst index e586e17fc7a5..3c0223575438 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 52852a33a2db..cea70ccf0ed4 100644 --- a/include/linux/virtio_net.h +++ b/include/linux/virtio_net.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -78,6 +79,20 @@ 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_validate set to 0. + */ +DECLARE_STATIC_KEY_FALSE(virtio_net_hdr_validate_disabled_key); + +/* Failsafe: a namespace can disable the check with + * net.core.virtio_net_hdr_validate. + * 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? * Handles Ethernet and VLAN headers; other devices use the @@ -189,6 +204,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; @@ -231,6 +252,9 @@ virtio_net_csum_partial_ok(struct sk_buff *skb, u8 hdr_gso_type) int nhoff; __be16 proto; + if (virtio_net_hdr_validate_disabled(skb)) + return true; + if (!virtio_net_l3_start(skb, &nhoff, &proto)) return false; @@ -522,6 +546,9 @@ virtio_net_tnl_offsets_ok(struct sk_buff *skb, bool outer_isv6, unsigned int thoff; int ip_proto; + if (virtio_net_hdr_validate_disabled(skb)) + return true; + if (!virtio_net_l3_start(skb, &nhoff, &proto)) return false; if (outer_isv6 != (proto == htons(ETH_P_IPV6))) 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 e76762e29360..43b8823c83f6 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -128,6 +128,7 @@ #include #include #include +#include #include #include #include @@ -2389,6 +2390,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 da5f881fbd3b..8a21baaa7e6d 100644 --- a/net/core/net_namespace.c +++ b/net/core/net_namespace.c @@ -396,6 +396,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 eb35da3556f4..3e4037b82a97 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 @@ -678,6 +680,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) { @@ -737,6 +776,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 */ @@ -841,6 +889,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 -- MST