🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #4704

[PATCH net v2 1/2] net/ncsi: fix use-after-free of ncsi_dev_priv in ncsi_unregister_dev()

#4704
From: Binbin Deng
Date: Mon, 19 Oct 2026 01:37
152 lines
11811 bytes
ncsi_unregister_dev() releases every package and channel, unlinks the
device from the global ncsi_dev_list with list_del_rcu() and
immediately frees it with kfree().  The generic netlink handlers
resolve the device through that list (ndp_from_ifindex() ->
ncsi_find_dev()) and walk ndp->packages with no lock and no RCU
protection, so a reader that overlaps the teardown dereferences freed
memory.  NCSI_CMD_PKG_INFO has no GENL_ADMIN_PERM, so an
unprivileged user can race it against rmmod of the driver:

  writer: rmmod ftgmac100
            -> ftgmac100_remove() -> ncsi_unregister_dev() -> kfree(ndp)
  reader: sendmsg(NETLINK_GENERIC, NCSI_CMD_PKG_INFO)
            -> ncsi_pkg_info_nl() -> ncsi_write_package_info()

The genlmsg_new(GFP_KERNEL) allocation between the resolution and the
walk makes the window wide enough to hit even without preemption.

KASAN report (arm multi_v7, QEMU ast2600-evb, KASAN + SLUB + PREEMPT):

  BUG: KASAN: use-after-free in ncsi_write_package_info+0xf50/0xf84
  Read of size 4 at addr c9f100a0 by task poc_dump/62

which is ndp->package_num at offset 160, the bounds check at the
entry of ncsi_write_package_info(); the page had already been
returned to the buddy allocator.  A second capture of the same access
shows the object as an 8K kmalloc region still in the slab.

Two kernel-side users keep running into the objects as well:

  * The per-channel monitor timers keep running while the removal
    loop frees the packages: ncsi_remove_channel() stops the monitor
    only of the channel it is about to free, so the monitor of a
    channel on a later package can fire concurrently with the loop.

  * Every outstanding request has a one second timeout timer in
    ndp->requests[].  dev_remove_pack() has already cut off the
    responses, so these timers are all certain to fire, and
    ncsi_request_timeout() would run on an ndp that the deferred
    free has already released, with the timer_list still linked into
    the timer wheel.  Their command and response skbs leak.

Quiesce everything that walks the objects before releasing them, and
defer the final free:

    dev_remove_pack(&ndp->ptype);   no more responses
    disable_work_sync(&ndp->work);  worker cannot re-arm monitors
    stop every channel monitor;
    ncsi_free_request() every used request
                                     (deletes its timer, frees skbs);
    ncsi_remove_package() loop;
    list_del_rcu(&ndp->node);
    kfree_rcu(ndp, rcu);

The rcu_head sits at the head of struct ncsi_dev_priv because
kfree_rcu() requires its offset to be below 4096 and the structure is
about 17 KB on arm32 (requests[256]).

This is the free side of the fix for the device object: the packages
and channels are deferred by the companion series "net/ncsi: fix
use-after-free of ncsi_package in ncsi_remove_package()" and
"net/ncsi: fix use-after-free of ncsi_channel in ncsi_remove_channel()",
which also adds the rcu_read_lock() to the genl readers.  The driver
side that walks the released objects from ftgmac100_stop() during
unregister_netdev() is fixed by the second patch of this series.

Found by a Coccinelle rule matching kfree() immediately after
list_del_rcu(); the code inspection and the fix were LLM-assisted,
and the race was confirmed at runtime with KASAN as above.

Fixes: 2d283bdd079c ("net/ncsi: Resource management")
Signed-off-by: Binbin Deng <18983559317@163.com>
---
 net/ncsi/internal.h    |  4 ++++
 net/ncsi/ncsi-manage.c | 36 +++++++++++++++++++++++++++++++++---
 2 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/net/ncsi/internal.h b/net/ncsi/internal.h
index cb41953..3630ee4 100644
--- a/net/ncsi/internal.h
+++ b/net/ncsi/internal.h
@@ -317,6 +317,10 @@ struct vlan_vid {
 
 struct ncsi_dev_priv {
 	struct ncsi_dev     ndev;            /* Associated NCSI device     */
+	/* RCU deferred free; must sit near the head of the struct as
+	 * kfree_rcu() requires the offset to be below 4096
+	 */
+	struct rcu_head     rcu;
 	unsigned int        flags;           /* NCSI device flags          */
 #define NCSI_DEV_PROBED		1            /* Finalized NCSI topology    */
 #define NCSI_DEV_HWA		2            /* Enabled HW arbitration     */
diff --git a/net/ncsi/ncsi-manage.c b/net/ncsi/ncsi-manage.c
index 237e041..70cb31e 100644
--- a/net/ncsi/ncsi-manage.c
+++ b/net/ncsi/ncsi-manage.c
@@ -1956,11 +1956,42 @@ int ncsi_reset_dev(struct ncsi_dev *nd)
 void ncsi_unregister_dev(struct ncsi_dev *nd)
 {
 	struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
+	struct ncsi_request *nr;
 	struct ncsi_package *np, *tmp;
+	struct ncsi_channel *nc;
 	unsigned long flags;
+	bool used;
+	int i;
 
 	dev_remove_pack(&ndp->ptype);
 
+	/* Quiesce everything that walks the packages and channels
+	 * before releasing them.  The monitors are stopped after the
+	 * worker so that ncsi_configure_channel() cannot re-arm them.
+	 */
+	disable_work_sync(&ndp->work);
+	NCSI_FOR_EACH_PACKAGE(ndp, np)
+		NCSI_FOR_EACH_CHANNEL(np, nc)
+			ncsi_stop_channel_monitor(nc);
+
+	/* With the Rx handler gone no response can complete an
+	 * outstanding request any more.  Free every request so that
+	 * its timer is deleted and its command and response skbs are
+	 * released; otherwise ncsi_request_timeout() would fire on
+	 * @ndp after the deferred free below, with the timer still
+	 * linked into the timer wheel.
+	 */
+	for (i = 0; i < ARRAY_SIZE(ndp->requests); i++) {
+		nr = &ndp->requests[i];
+
+		spin_lock_irqsave(&ndp->lock, flags);
+		used = nr->used;
+		spin_unlock_irqrestore(&ndp->lock, flags);
+
+		if (used)
+			ncsi_free_request(nr);
+	}
+
 	list_for_each_entry_safe(np, tmp, &ndp->packages, node)
 		ncsi_remove_package(np);
 
@@ -1968,8 +1999,7 @@ void ncsi_unregister_dev(struct ncsi_dev *nd)
 	list_del_rcu(&ndp->node);
 	spin_unlock_irqrestore(&ncsi_dev_lock, flags);
 
-	disable_work_sync(&ndp->work);
-
-	kfree(ndp);
+	/* Netlink readers may still hold the device */
+	kfree_rcu(ndp, rcu);
 }
 EXPORT_SYMBOL_GPL(ncsi_unregister_dev);
-- 
2.43.0
Technical Details
Message-ID: <20261019013739.450017-1-18983559317@163.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-348233-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-348233-linux-kernel-netdev=dewy.ru
From: 18983559317@163.com (Binbin Deng)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net v2 1/2] net/ncsi: fix use-after-free of ncsi_dev_priv in ncsi_unregister_dev()
Date: Mon, 19 Oct 2026 09:37:39 +0800
Organization: Usenet Network
Message-ID: <20261019013739.450017-1-18983559317@163.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="3478793"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:lgTCg9ugOgULy/h5fvPU/CNceiM= sha256:9h9eJpP81G1pln/OadVok6Sy3bhMnNCc+69dikgKskA=
	sha1:gsiCh74Wl/TVPTc2UZP367jjjXc= sha256:JeQbBkH+8ZINZji0yTyAKdUZFGKO6aY2ygBx5DjyrJc=
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 0539C1108DE; Mon, 19 Oct 2026 04:38:47 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -2.716
X-Spam-Status: No, score=-2.716 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, FROM_LOCAL_DIGITS=0.001, FROM_LOCAL_HEX=0.331,
	HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1,
	RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001,
	UNPARSEABLE_RELAY=0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (1024-bit key)
	header.d=163.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 TsZkrixK1YQl;
	Mon, 19 Oct 2026 04:38:43 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-348233-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=163.com
X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
	by mail.dewy.ru (Postfix) with ESMTPS id 45EA2110892; Mon, 19 Oct 2026 04:38:34 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.90.174.1])
	by tor.lore.kernel.org (Postfix) with ESMTP id 4248F2AA57; Mon, 19 Oct 2026 01:38:31 +0000 (UTC)
X-Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id F1CCC2DEA6B;
	Mon, 19 Oct 2026 01:38:29 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="HjnntV9y"
X-Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.4])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 671FE29B8E1;
	Mon, 19 Oct 2026 01:38:25 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=220.197.31.4
X-ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
	t=1792373909; cv=none; b=E2HLgcZ6ozaIDT9TglNkjGt4HnDstNScjWPb+nZr8JGh2WoJcZONO23j35IHfuXJjQqxZmbsDfGCRGdm34mzTwEqoj64iaqow0x/JYOsvVRKSfmu6W5t9K5FXmPBX5zJ0sSAcefP1s9N2TWkQfimrQMLZDOjVXIwjDQMiIMyl3Q=
X-ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
	s=arc-20240116; t=1792373909; c=relaxed/simple;
	bh=xZp69AcqqcLYdgTjDKNCo6arObT7ZYaZKk2Uqmy/dbo=;
	h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=d97o96I1ZWwtj3ZbreTLplsqm1y72xmSCjz/qFxFK2Tmet2vexlbHFlXE85AC8oZ0J6KTmdn3jT7N1aqEiYBxEESK020XZHWOVUs66XM6T4eOgxHOC9eTg/zImO3j8Ke7YMrUgVzcQ70wKszxCXFzm5Mg2SYy3O4e2AKdBzWKHs=
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=HjnntV9y; arc=none smtp.client-ip=220.197.31.4
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com;
	s=s110527; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=C1
	hlcy8uosj/hXxugJg3frVYFqDhWTyUwRZR33eXhdo=; b=HjnntV9yRRunbt4eM3
	B52dNuBqX/ZXpKtEOMQy9VR7TMGRl3bJrnvybbptxKfMoRJRHlHVeP00KUpjUBNq
	K4EKS+NfTp7PVqxvLhNb12H4MAQ0Bfnc4xDKZ30OZq6FKuC4oO9VCBxfeX0sY6+L
	bONlg3G4K05MUe55PscafdUac=
X-Received: from colol4bi5.localdomain (unknown [])
	by gzga-smtp-mtada-g0-3 (Coremail) with SMTP id _____wDn14ZldNVqANBiBA--.31905S2;
	Mon, 19 Oct 2026 09:37:41 +0800 (CST)
X-Cc: netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Binbin Deng <18983559317@163.com>
X-Mailer: git-send-email 2.43.0
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>
X-CM-TRANSID: _____wDn14ZldNVqANBiBA--.31905S2
X-Coremail-Antispam: 1Uf129KBjvJXoW3XF1fJr18AF48Ar1rWF1DAwb_yoW7Kr4DpF
	W3Ja43K3ykZr1jgrZ3Zr43uF1rXrs7Zw17C3sIgw4aya45G34jvryIyay09Fy5GFZ7Ga12
	vr4DWws09rs8X3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2
	9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0ziiL08UUUUU=
X-CM-SenderInfo: jprymmytvvmjmrxbiqqrwthudrp/xtbC3wZGKmrVdGYvugAA3N