🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #8334

[PATCH net] net/packet: check TX_RING fragments before memory accounting

#8334
From: Eric Dumazet
Date: Wed, 28 Oct 2026 18:14
62 lines
9912 bytes
From: Kyle Zeng <kylebot@openai.com>

tpacket_fill_skb() adds the full non-linear payload length to
skb->truesize and sk_wmem_alloc before checking MAX_SKB_FRAGS. With
PACKET_VNET_HDR, userspace can submit a ring-sized frame even when it
greatly exceeds the device MTU.

A near-INT_MAX frame can saturate sk_wmem_alloc before the fragment
loop rejects it. A subsequent refcount_add(0x40000001) can temporarily
turn REFCOUNT_SATURATED into 1. A concurrent TX completion can then
drop that apparent last reference and free the still-live packet
socket before refcount_warn_saturate() restores the counter.

Check that the remaining payload fits in MAX_SKB_FRAGS pages before
accounting it. Include the initial page offset in the calculation,
and move the existing -EFAULT handling out of the fragment loop.
This preserves the accepted packet sizes and error code while
preventing an invalid frame from injecting an oversized byte charge.

Fixes: 69e3c75f4d54 ("net: TX_RING and packet mmap")
Cc: stable@vger.kernel.org
Cc: Willem de Bruijn <willemdebruijn.kernel@gmail.com>
Signed-off-by: Kyle Zeng <kylebot@openai.com>
[edumazet: use net_warn_ratelimited() instead of pr_err(), add a Fixes: tag]
Signed-off-by: Eric Dumazet <edumazet@kernel.org>
---
 net/packet/af_packet.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
index 823e0911c910fa95d85bdf187eddef802cf65726..cf6429f09477263534b631199aae85c30de2fffe 100644
--- a/net/packet/af_packet.c
+++ b/net/packet/af_packet.c
@@ -2655,6 +2655,12 @@ static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
 	}
 
 	offset = offset_in_page(data);
+	if (unlikely(to_write > MAX_SKB_FRAGS * PAGE_SIZE - offset)) {
+		net_warn_ratelimited("Packet exceed the number of skb frags(%u)\n",
+				     (unsigned int)MAX_SKB_FRAGS);
+		return -EFAULT;
+	}
+
 	len_max = PAGE_SIZE - offset;
 	len = ((to_write > len_max) ? len_max : to_write);
 
@@ -2666,12 +2672,6 @@ static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
 	while (likely(to_write)) {
 		nr_frags = skb_shinfo(skb)->nr_frags;
 
-		if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
-			pr_err("Packet exceed the number of skb frags(%u)\n",
-			       (unsigned int)MAX_SKB_FRAGS);
-			return -EFAULT;
-		}
-
 		page = pgv_to_page(data);
 		data += len;
 		flush_dcache_page(page);
-- 
2.53.0
Technical Details
Message-ID: <20261028181450.2221013-1-edumazet@kernel.org>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-352985-linux-kernel-netdev=dewy.ru

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-352985-linux-kernel-netdev=dewy.ru
From: edumazet@kernel.org (Eric Dumazet)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net] net/packet: check TX_RING fragments before memory accounting
Date: Wed, 28 Oct 2026 19:14:50 +0100
Organization: Usenet Network
Message-ID: <20261028181450.2221013-1-edumazet@kernel.org>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="1839909"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:l20K9VNQAfvLtfD/Dr0zLR7jvyk= sha256:74bw8feRpQxcwD+zodJC5AOftSZLVdb8vPy+NjiUhZQ=
	sha1:+mNsoadRpKNXUn4Cn/dq5eAxLWM= sha256:VhtlofMoY0lMRph4B2Eje0s+dPrjtx3b3aLVr+4fgpY=
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 7BAEC125C44; Wed, 28 Oct 2026 21:46:07 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.401
X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1,
	DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
	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=kernel.org
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 J_V491KkrW3P;
	Wed, 28 Oct 2026 21:46:04 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-352985-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
	by mail.dewy.ru (Postfix) with ESMTPS id A843F125C40; Wed, 28 Oct 2026 21:45:55 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by tor.lore.kernel.org (Postfix) with ESMTP id 558F33974C; Wed, 28 Oct 2026 18:16:39 +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=1793211304;
	b=utiR3LhCS90qTCb5NOPo34QkjxtmWghuQLEVIfyk8tyfjABlcL8jkcAeHYvPjt9IheVT
	 BY7TVdnfKhqMqz2J4EZHUHI4GPFJrq38hxloIeGidBDnMU6m6QSrojpShWNRuWZOQjoyj
	 +0wRE7Ew1LZvrXSxxKojLFpGue2PD9S3+RFrBTksRSIoFPWhDnkypBQjbLSDG4XRnYbep
	 kjnieXIT2neL4xzF4vSoBt9NgeQrE11q3hfhEXPEwGTRamMZrC55UNpYzMJEfIXolIUd6
	 c7agQY5K5qoKAloOYid+FxthCSivmkyIiKlQwn1xxWaX4fGquGsUvEmgqdYq6akmasw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793211304;
	h=X-Original-To:Received:Received:DKIM-Signature:From:To:Cc:Subject:
	 Date:Message-ID:X-Mailer:X-Mailing-List:List-Id:List-Post:List-Help:
	 List-Subscribe:List-Unsubscribe:List-Owner:Precedence:MIME-Version:
	 Content-Transfer-Encoding;
	bh=ORByZDQUFgn0JJEnKfTmOTXNubEmxRahmOuQvTF/0L0=;
	b=1qH5SvQeUshpfQTtsO9VL3xCcYHoEhb+bg/bByuWy8rryliKQUdtEauvnkxR0Glv2nuK
	 zZhNena95jV+IxUYvBPIMMiXeVSJmINTxvppXivxVeZHOL4oDQwNInPaF8Ynwc4bo7Kl8
	 aGlmdn5HI3rljOrce++NPRg35fViuuLHv74cx/4/YDNhvVoAaY9UyCx5OEME30kN6Di0c
	 HnPtcybfU+7JJD5pJCaqBNq5TZmrekU3Ej8NpHJqg8WgwtJyvEoJ6iiz28TPuZLXExhjn
	 AbzmUrr7hB9iRZ4KAmbU5vD8KsEHgQfBO2N3Fwdr9yWCQ3Y43WFR1hW/cEugW9zXwsw==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=e6jEoKv5;
	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 14A57606B44F;
	Wed, 28 Oct 2026 18:15:04 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=e6jEoKv5
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1793211298;
	b=snhADt2RhF12Wjn1IAWGpF1MeBLS1QQIXErdQyDkeN0teoCsctcFVKImW4kdROy1KC5S
	 9hhbnnTuFubQPLfo4cm3MW2SxjvjtP+xaqsKFtyj2wa27TJAnxjfvuAk5ZbpoNTcIZ3dl
	 bmZGo/p6LZuCqyZt28GKf6MOc80sxLTzIfwGloefbyE0uW98taTMjs8Ah7Sf+9hMWwcWm
	 sgS8aGF0gZItXKGm6E1IfPuq8QwQDfVuwNWDXZMWk6EEMs7UBT3YXBskvomoi2cJDQLwo
	 4ISQ+QRzWpz8KR0sAC5Q7HMftzIAL8IUAbkjSUeJQHiTNyCf2u053QbkF97j5Etznlw==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793211298;
	h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer:
	 MIME-Version:Content-Transfer-Encoding;
	bh=ORByZDQUFgn0JJEnKfTmOTXNubEmxRahmOuQvTF/0L0=;
	b=q5ZlgIw3lIfzKFfdAlw1Ik00LuBSmLJzvXC7X9UcJs0fg2RDhovdyA7IJY7Y4SxR5rrp
	 b80QVnWZqUeZgXE2jq+OTjHb43ZMKb++t6/paD4X2bcGfTPq7fd3t95JJviDwNAHoHo3/
	 OtruKyplJkHQs1Xe5rbxHoM78K9zDUd45ciyZEu1odCnxGUgpBTr9B3Z2zyJ9AXl7OMxl
	 3mpzROhcmC2fzC+C8ZvKn3xChqpQfO12mvQOvBbkVvwCBtwJ1AqCav3cqD70jNAP/8M9b
	 Vu+YY41h6EtS/E1UYS+u4eAjjOMaiS7UWofwK+qHvo+Ym0rEXs8VXPDSk3UzCBTcPoA==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=e6jEoKv5;
	arc=none smtp.remote-ip=100.103.45.18
X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id A10A760A5BDE;
	Wed, 28 Oct 2026 18:14:57 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3B6871F01828;
	Wed, 28 Oct 2026 18:14:55 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1793211297;
	bh=ORByZDQUFgn0JJEnKfTmOTXNubEmxRahmOuQvTF/0L0=;
	h=From:To:Cc:Subject:Date;
	b=e6jEoKv5aihwlkTjfD/G9eavbW9A8L1euwv8Uc0zriHawl8aZ+coOF9wVDkyD8liK
	 YdRCERmtCBeu1U40bs604IkmoGzeG/ex3tDTSFkOubog3PzY5PIwKb+B6sR/v657dN
	 nFy2+WhGWxk8yqDe+7RDKrm1mHppJZUpn2B7GWMLeHop4+pFvp9yf0Evz5aP84o+ya
	 8SOBQMfXLFJivNwccIHZY5UFpUZkyKLIqqAldiUbb1+5Fs0odqFpspL7xYxbkyWmnJ
	 Un59KKuAdwHL/sJtvyvJOjjK75VPwMGGoeekEa50p1oFYwsYCyRhQR4x7SkgNXreRO
	 G7j7iLZfX5Dfg==
X-Cc: Simon Horman <horms@kernel.org>,
	Willem de Bruijn <willemb@google.com>,
	netdev@vger.kernel.org,
	edumazet@kernel.org,
	Kyle Zeng <kylebot@openai.com>,
	stable@vger.kernel.org,
	Willem de Bruijn <willemdebruijn.kernel@gmail.com>
X-Mailer: git-send-email 2.53.0
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