🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #8955

[PATCH net v2] tun: bound the NAPI receive queue

#8955
From: Antonio Quartulli
Date: Fri, 30 Oct 2026 15:25
72 lines
11400 bytes
With IFF_NAPI, tun_get_user() queues every written packet for NAPI on
an unbounded list. When the poll runs asynchronously (threaded NAPI)
and is slower than the writer, the queue grows until the system runs
out of memory. An unprivileged user can do this from its own user and
network namespaces, and the queued skbs are not charged to its memcg:
in a test VM, a uid 1000 process limited to 128 MB caused a global OOM
in ~6 seconds.

Drop packets written while netdev_max_backlog of them are queued,
before allocating them, and count them as rx_dropped. The sendmsg()
path, whose callers pass zerocopy buffers, is left alone.

Still open: the limit counts packets rather than bytes and applies per
queue, and the queued skbs are still not charged to the writer's memcg.

Found, fixed and tested with the help of an AI coding assistant
(Claude Code).

Fixes: 943170998b20 ("tun: enable NAPI for TUN/TAP driver")
Signed-off-by: Antonio Quartulli <antonio@mandelbit.com>
---

v2:
- Cc'ong netdev and maintainers

Notes:
- Affected since 943170998b20 (v4.15). Reproduced on mainline
  7be4bc52fd9e; the code is unchanged in net/main 94cea342e891, which
  this patch is based on and was build-tested (W=1) against. The
  before/after runtime tests were done on 7be4bc52fd9e.
- Conditions: unprivileged user namespaces allowed, /dev/net/tun
  accessible (0666 with the usual udev rule), threaded NAPI.
- Mitigation: disallow unprivileged user namespaces or restrict
  access to /dev/net/tun.
- A reproducer is available on request.

 drivers/net/tun.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index 770b8fe085c3..1ace0c1f22f2 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -60,6 +60,7 @@
 #include <linux/rcupdate.h>
 #include <net/net_namespace.h>
 #include <net/netns/generic.h>
+#include <net/hotdata.h>
 #include <net/rtnetlink.h>
 #include <net/sock.h>
 #include <net/xdp.h>
@@ -1856,6 +1857,17 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 			zerocopy = true;
 	}
 
+	/* In NAPI mode packets are queued here and processed by the NAPI
+	 * poll, possibly in a kthread or ksoftirqd. Bound that queue like
+	 * the per-CPU backlog, and drop before allocating and copying.
+	 */
+	if (tfile->napi_enabled && !frags && !msg_control &&
+	    unlikely(skb_queue_len_lockless(&tfile->sk.sk_write_queue) >=
+		     READ_ONCE(net_hotdata.max_backlog))) {
+		dev_core_stats_rx_dropped_inc(tun->dev);
+		return total_len;
+	}
+
 	if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) {
 		/* For the packet that is not easy to be processed
 		 * (e.g gso or jumbo packet), we will do it at after
-- 
2.56.0
Technical Details
Message-ID: <20261030152540.1409884-1-antonio@mandelbit.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-353828-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-353828-linux-kernel-netdev=dewy.ru
From: antonio@mandelbit.com (Antonio Quartulli)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net v2] tun: bound the NAPI receive queue
Date: Fri, 30 Oct 2026 16:25:36 +0100
Organization: Usenet Network
Message-ID: <20261030152540.1409884-1-antonio@mandelbit.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2264257"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:itUZP3mpZLmklBbFJ+lTIOv558w= sha256:EChblRFqwXhvWKPgtTCTs5h1GIyi/39uCbmAEgNNuhg=
	sha1:HbvzTq11kZjG7AXQX2/n1FzC9OA= sha256:EBaoqvLRemWYnxWoRLA7eo6wMko9Dq0NYMVfq3SZpwU=
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 0AEB6125CA2; Fri, 30 Oct 2026 18:26:28 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.149
X-Spam-Status: No, score=-3.149 tagged_above=-9999 required=6.31
	tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,
	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,
	URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=mandelbit.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 hlfJgv95mRB2;
	Fri, 30 Oct 2026 18:26:21 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.232.135.74; helo=sto.lore.kernel.org; envelope-from=netdev+bounces-353828-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=mandelbit.com
X-Received: from sto.lore.kernel.org (sto.lore.kernel.org [172.232.135.74])
	by mail.dewy.ru (Postfix) with ESMTPS id C63A1125C9F; Fri, 30 Oct 2026 18:26:11 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sto.lore.kernel.org (Postfix) with ESMTP id 8BB6E601180; Fri, 30 Oct 2026 15:26:07 +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=1793373965;
	b=pHmwYlBMMACYnFB8fVeshJZijjdAW3R4av5nWHExKuAkV8/8NS+E/te/hU+rCiv0PrOE
	 twqwLa7p7E4tn7uZivcGmD8BZUuVQRedmP3/StQKktxEG4kxQpyycFQWinQpaJVJ34hMK
	 rPxyhV2lxKypOyv282umTUKY2cu14Yrqp1Z6IWuSr/NEVOFRC7W3yJxk0Dk4zHCpgab9l
	 QqDaeeDY36h0ltlabOYySb8JqkvVnLW+FSe+BZHmd7O8lYbCZ5N7TAV4kAAn8J3qHgI7Q
	 zZ/mxP9gOlNqcZxH+8Vx9pjFnRBwx4yUGx3wdBJRSYsrHl9X7DF7NxoElMSWwYxLZeQ==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793373965;
	h=X-Original-To:Received:DMARC-Filter: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:X-Rspamd-Queue-Id;
	bh=+Co41UmDhvgfOSUvONitKvKJqzl8wj5ABYHigltlJj0=;
	b=quonKsDTrwvGqheEA509U2nCWXkKsBWppmRYWcgidNrxuaWcBOJ8plRzjRZz51YcrQ6w
	 Gyz70jzjIp06jc4tJPmRMnjZ4Ke+a2yheTUqHOu8KNzR5pVcdYFRpJhwch/Zee0n3VsG/
	 RFHTJr6jiRSCOWvrcwLJV8B3xex0S/egy40JQdv1vYhN6uQNdKPQbOgBZldvXcStF/nLB
	 JpXKt/VOoTb16Gvudn2alzJm1IXB5VnrtAh1zPPvTZFl0VHl7HkN1s3h5+x4PFScwY0jo
	 HRuzU8PA36pw4oB3GGM8/3Nyn787lV19Ezm/BKof6BO7oKHkS6ZN/+e2xd4q9uEYc8w==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=EBU7UUqB;
	dmarc=none header.from=mandelbit.com;
	spf=pass smtp.mailfrom=mandelbit.com;
	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 0C159606A321;
	Fri, 30 Oct 2026 15:26:05 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key, unprotected) header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=EBU7UUqB
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=195.10.208.51
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1793373963;
	b=nnoLrJfah5PEMk/FlyXj00U94F8yxI3s496TfvkV5f/rizassGnmM74bdcllNA2t3Ef9
	 O2Kp8F4bYdnbxzo8wd7/ey8bZrShFdEcHI3NXngi6La+OKOD+HnMpKAWk0YX2wRysnzLT
	 9rkxBW69Fq3mFYi6gNcxTlHrGO21U6p9l3vzbL8T+kcvbr2L9JSay1y3xTpZy+BkuBjf0
	 FuoXC818VVn3+VoKFKqqSMZett/EOkKZ3sFXCQN2r3KwysOfD3jonqju+v/d5MaKLVwzj
	 LAUUPoc5JMStCE8VN5wvh1KJXL+MM770H6cuHFNEVe920nKAQ4n2Zl5VZKL0Ww8z9lQ==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793373963;
	h=DMARC-Filter:Received:DKIM-Signature:From:To:Cc:Subject:Date:
	 Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding:
	 X-Rspamd-Queue-Id;
	bh=+Co41UmDhvgfOSUvONitKvKJqzl8wj5ABYHigltlJj0=;
	b=FRx+82xrIzoYk2aCLFs+tsNWYP1/1iGfDr1iFIW3ZUB2QdtsL1MYv+P7kFKl+3C8IigT
	 vs9lr3wiAEwfkxr+HoTEF/Z5zWNiaJw5vWO8Yrk48PCsAvsomtCLvPvojZuoDkxIWAIqr
	 QMm440CfwKSo61ToHTTgFKUJsBJMWT45c1UH6uMT0VbzBsMItgUX6AvXe/jQVw52vyNA7
	 Z/aApXBA9cIunY1Ej6tW9FD84vZiOTnort8xWiafDHkGyEsolxsrbk2bAxgwuy+ghtd8p
	 pNQQC6129itW+wEFVHzWACuEGSElY9ARWz2sNsUE5MZ/fMTw5bCAlBEoJt8iPGYplug==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=none header.from=mandelbit.com;
	spf=pass smtp.mailfrom=mandelbit.com;
	dkim=pass header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=EBU7UUqB;
	arc=none smtp.remote-ip=195.10.208.51
X-Received: from mout-b-206.mailbox.org (mout-b-206.mailbox.org [195.10.208.51])
	(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 61A87606A31A
	for <netdev@vger.kernel.org>; Fri, 30 Oct 2026 15:26:00 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 61A87606A31A
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=mandelbit.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mandelbit.com
X-Received: from smtp202.mailbox.org (smtp202.mailbox.org [IPv6:2001:67c:2050:b231:465::202])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519MLKEM768 server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(No client certificate requested)
	by mout-b-206.mailbox.org (Postfix) with ESMTPS id 4jGQ0b20NCzKF;
	Fri, 30 Oct 2026 16:25:51 +0100 (CET)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandelbit.com;
	s=MBO0001; t=1793373951;
	h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
	 to:to:cc:cc:mime-version:mime-version:
	 content-transfer-encoding:content-transfer-encoding;
	bh=+Co41UmDhvgfOSUvONitKvKJqzl8wj5ABYHigltlJj0=;
	b=EBU7UUqBWFp3EjpNiwIVsdQmO28OOW5G+BY9IJTPKlNTryx9WPB4V1rxjHKf0INDQoPXIc
	3IBdbgG/sY9EYHxMOgNa1Y5gteYQac66JnRddmmrnlpkFzm3oHZt6NflYFcCNfRBMsMN3r
	uQvqTLLCXWv8bno4MxG8lciEPI3SgVEWEoXklYSFEsxuopTLR0AuiqBUWgdx/N0Ze99Mcz
	8rcnvIvRYMAsL09aBLhp0E7+9MurX/iKGxZZsFHl1a30QYU+X5TDL5yeuqvgIMHJk2gF5+
	UnFd7EPEMA6xGd8EuXxlrDoX/WwCMMrbDQepNu1LuNqeUMt+Sqmayjym/3wxbA==
X-Authentication-Results: outgoing_mbo_mout;
	dkim=none;
	spf=pass (outgoing_mbo_mout: domain of antonio@mandelbit.com designates 2001:67c:2050:b231:465::202 as permitted sender) smtp.mailfrom=antonio@mandelbit.com
X-Cc: Antonio Quartulli <antonio@mandelbit.com>,
	andrew+netdev@lunn.ch,
	davem@davemloft.net,
	edumazet@kernel.org,
	kuba@kernel.org,
	pabeni@redhat.com,
	netdev@vger.kernel.org
X-Mailer: git-send-email 2.56.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
X-Rspamd-Queue-Id: 4jGQ0b20NCzKF