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=1792768191; b=E1J6uXy/KShXykucdf9MWOMxhJcva6k1ub4U8flDUOwz+3OL6rwGjbx30qSuku2VJQR0 azP/O5p/CziRZUrjBFb3FQjyNuUz6J1263xRJbN0kP3vDAzUdPddQ0Xe1a9FabwBV1FOl y5r0ibbztfhgHebN75Wsj7rMskcJO71ITBx2s4qy14PnlzeErwAz69G5komIOMo0OJdOd qBBiICDZUqs8YFCyBWt/YQZDGZUBQkFg1e1IkmSfXnwJi7ot0ePFjbc0hMwPWmJhaB6Am sA/3yeqBK6p2tyGmRKuiDP4XqXIg9EC7at0FDv4RS6NiZsMjfu8oI2Ivhrs0gGL3vUA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792768191; 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=mXloVW6rZxDahnxqb4ykjI1/+DaQ13fITP+bGDCTCpc=; b=k5Zk9PVsnKSP/68WWHEpseKQITjcbMqnxZnzRDxWYWZiEOsGkJHMJHOhDeuntWaqZ7Rc rT6u3zxg/hA9ZlaMvyhNNLTKTpvTPo3P7pzD3hudzt2KxzTtcZ12Zjfe12NRBmONegkwc qhIJIhNISuQoXaflVgHCwUgRj0ysSIKjp7puJCBNqfwMyN/6Todwd8riZkgf3Wvsu1Eo2 leNXB2GE1O/tHcVgpH27zl/tfXtkrkKjokOFtWPfKQAu40hoIAcpxzdrWgTOH1jQYwPoZ MFt3FI2V0NMmJWSdehFTsvCd5LcpOFkM7M/Ggd5wphKbnWCa5HcQ9Kco+zA4YsSO49Q== 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=GTa+GYQB; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=ROZeIx9Z; 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 77BE760A5BD4 for ; Fri, 23 Oct 2026 15:09:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 77BE760A5BD4 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=GTa+GYQB; dkim=pass (2048-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=ROZeIx9Z DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1792768189; 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=mXloVW6rZxDahnxqb4ykjI1/+DaQ13fITP+bGDCTCpc=; b=GTa+GYQBllQ2SeWAakdN0ndJte3F8GoPV9yma1UPUbGnzPmq+ScZPX/CZynt014MYq8fth a9yvz+LTpY1UvpuAFDa1HV+RFkFrfW27083sc1WS07U/DFWqnmkDE/ug1S1FYVD9zBADhk 9DuxJZSvOXj8+THvc2wNQf9CBXnCi70= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-131-84WzpgJ3NBGyFlejTtY62Q-1; Fri, 23 Oct 2026 11:09:47 -0400 X-MC-Unique: 84WzpgJ3NBGyFlejTtY62Q-1 X-Mimecast-MFC-AGG-ID: 84WzpgJ3NBGyFlejTtY62Q_1792768187 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-48e29c25101so568008f8f.3 for ; Fri, 23 Oct 2026 08:09:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1792768186; x=1793372986; 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=mXloVW6rZxDahnxqb4ykjI1/+DaQ13fITP+bGDCTCpc=; b=ROZeIx9Zy7GrFAYkbsTwF9vx6uOK96PH2uGUK/SuI+LF8h7emSWMLa0UzAnxblOYZZ cGMkjxd+MDjN+Dc0brn+7Qk5y4fSS1SB709Gh0LCRtr4DV+P3PvHrHv3tJ/9FP5YIj+A 52PbbHd08f9BTK68vOTQfJiq/ivc+qPQ+nwBl/y4Rg0sG5xkjOZcYr6KCZn+5ufRv36D NlVNdCw7eTVsxPGTdHaAkYwCPFpj2pe7bc0Xby/d7MKYZQSVKHlSLcyUxdAd1o4INuPt OAKi1pSEY6ZSr8dwxNFaLhQGwpWFvbbpSs+zhLWF6470KrMeEjDTaACYdsZkN0PBVpGS a0aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792768186; x=1793372986; 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=mXloVW6rZxDahnxqb4ykjI1/+DaQ13fITP+bGDCTCpc=; b=xTpdn+tnCAmaezihUTDmnBeU+B5EvyHeqHvLgtXF8AdlBbPGNssvJ8mc4R+ky7P/AN sexHMC/dnFl7fi6lTq6x4fV/wEdRSDsU7d5EaLVhUio/jV1d2RWX4oxTVh9M6hhtTvCO toErvTvHIMgRlgoAbzNF4b8utZH/GKID9xuC3DaOBZFGl4QnjQpP265EvozxyKspqNyB S1atEOi2/eljpspmqV3dVG0zTvY8W2n+tX4D68miG+f+KUC5CzLd31fMcPhNx7gGxu7e W9/ItKth3v801SPsKIeVmaDfCqmfSfox5Yw/rJo07ZHVUemMYJHgbd41z+1kVbQoy8lP juTg== X-Gm-Message-State: AFq9FYLvOvS1fHar7pkco6DIG/LDnPB4vVRpaAsrBLXYdS3ZsaOZnAYL TjvPgqP4mfT/d2SpyQqo52ZDkGg1Y2HDmPEoJ89bw7AO4aSUsZrvlo819op6E/H6uqaASlGux2N 8PK3YNYduDIifsEB4OZbFQWZUkX0RA52I8+jo/WPvAizkbXdGZ/INiaoYsFlaBTpbdYutGv7T6G PCCsK1+L71nw4G/l1jc/q6iQq82lNrlErBJfwN/UxECuI= X-Gm-Gg: AYBFou3JuxIshgcYAMkcxO2lvLT6l3ERlEXzkaY74cIWtzzNiYYqz1zOZrnBvBS1SWp C1ctXpJffLNwrmoGmXvIHSAFB98QfbrGWmht/dfCE1/DnsbOv2dex+e7GX7QECi5ZdT67dimdyk ouUgqE9I291GSecZQbIN0Mdjpah4ir3P72FvDgDNja18Zjn5jsOfwxiE5I+50mKTgnxq/mgp6mU LxwgeXYi3leaQBojYkpZFKK2UtvItsC8zBWzNiLAbR02cKKVpZpiPf9kta6miMtFDARfo5ulC4A lWW4hFN5k+5Jdxh7vXg4f89nDOfhmdBdtWiyTiTvX00K3i7B1r1YS25PW7xrALELL/+5aZk= X-Received: by 2002:a5d:584f:0:b0:48e:261b:a49d with SMTP id ffacd0b85a97d-48e38587cd3mr4057121f8f.15.1792768186377; Fri, 23 Oct 2026 08:09:46 -0700 (PDT) X-Received: by 2002:a5d:584f:0:b0:48e:261b:a49d with SMTP id ffacd0b85a97d-48e38587cd3mr4057050f8f.15.1792768185685; Fri, 23 Oct 2026 08:09:45 -0700 (PDT) Received: from redhat.com ([2a06:c701:73f6:2400:30a8:9e33:7d82:c035]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-48e3d410708sm305580f8f.38.2026.10.23.08.09.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Oct 2026 08:09:43 -0700 (PDT) Date: Fri, 23 Oct 2026 11:09:41 -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 v6 5/5] net: add a sysctl to opt out of virtio net hdr checksum validation Message-ID: 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 | 25 ++++++++++++ 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, 103 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 14120710d075..c6b5957b3248 100644 --- a/include/linux/virtio_net.h +++ b/include/linux/virtio_net.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -85,6 +86,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? * Handles Ethernet and VLAN headers; other devices use the * network header offset the caller set up. @@ -198,6 +211,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; @@ -240,6 +259,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; @@ -531,6 +553,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