mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 net 0/2] net/ncsi: fix RCU list entry lifetime on package and channel removal
@ 2026-11-20 23:54 Tristan Madani
  2026-11-20 23:54 ` [PATCH v2 net 1/2] net/ncsi: add missing rcu_read_lock to netlink and device list walks Tristan Madani
  2026-11-20 23:54 ` [PATCH v2 net 2/2] net/ncsi: defer free of removed list entries to an RCU grace period Tristan Madani
  0 siblings, 2 replies; 3+ messages in thread
From: Tristan Madani @ 2026-11-20 23:54 UTC (permalink / raw)
  To: Samuel Mendoza-Jonas, Paul Fertser
  Cc: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, netdev, linux-kernel, stable, Tristan Madani

From: Tristan Madani <tristan@talencesecurity.com>

The NCSI package/channel/device removal paths call list_del_rcu()
followed by immediate kfree().  This races with concurrent readers
(primarily the unprivileged NCSI_CMD_PKG_INFO netlink handler) that
walk the same lists with list_for_each_entry_rcu() but never enter
an RCU read-side critical section.

Patch 1 adds the missing rcu_read_lock()/rcu_read_unlock() to the
reader paths so kfree_rcu() on the write side has matching readers
to wait for.

Patch 2 defers the free side: channels use call_rcu() with a
callback that also frees filter sub-allocations, packages and the
device structure use kfree_rcu_mightsleep(), and the work is
quiesced before packages are removed.

Pre-existing issues noted by sashiko-bot review -- dangling
active_channel/hot_channel/preferred_channel pointers, unquiesced
per-request timers, and the ftgmac100 unregister ordering -- are not
addressed here.  They affect the overall NCSI teardown design and
are better handled in a dedicated series.

Tristan Madani (2):
  net/ncsi: add missing rcu_read_lock to netlink and device list walks
  net/ncsi: defer free of removed list entries to an RCU grace period

 net/ncsi/internal.h     |  1 +
 net/ncsi/ncsi-manage.c  | 39 ++++++++++++++++++++++++---------------
 net/ncsi/ncsi-netlink.c | 32 +++++++++++++++++++++-----------
 3 files changed, 46 insertions(+), 26 deletions(-)

---

Changes in v2:
- Rework as a 2-patch series fixing both reader and writer sides
  instead of a single patch that only fixed the writer side
- Add rcu_read_lock() on the reader side: ncsi_find_dev(),
  ncsi_write_package_info(), ncsi_pkg_info_all_nl() (sashiko-bot)
- Defer channel sub-allocation frees (mac_filter.addrs,
  vlan_filter.vids) to the RCU callback so readers cannot follow
  stale pointers during the grace period (sashiko-bot)
- Move disable_work_sync() before package removal in
  ncsi_unregister_dev() so the work function is quiesced before
  any structures are freed (sashiko-bot)
- Fix Fixes: tag hash from 2d283bdd5b20 to 2d283bdd079c
  ("net/ncsi: Resource management") (sashiko-bot)
- Include discovery method per sashiko-bot request

-- 
2.47.3

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 net 1/2] net/ncsi: add missing rcu_read_lock to netlink and device list walks
  2026-11-20 23:54 [PATCH v2 net 0/2] net/ncsi: fix RCU list entry lifetime on package and channel removal Tristan Madani
@ 2026-11-20 23:54 ` Tristan Madani
  2026-11-20 23:54 ` [PATCH v2 net 2/2] net/ncsi: defer free of removed list entries to an RCU grace period Tristan Madani
  1 sibling, 0 replies; 3+ messages in thread
From: Tristan Madani @ 2026-11-20 23:54 UTC (permalink / raw)
  To: Samuel Mendoza-Jonas, Paul Fertser
  Cc: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, netdev, linux-kernel, stable, Tristan Madani

From: Tristan Madani <tristan@talencesecurity.com>

The NCSI_FOR_EACH_DEV, NCSI_FOR_EACH_PACKAGE and NCSI_FOR_EACH_CHANNEL
macros wrap list_for_each_entry_rcu(), but several callers never enter
an RCU read-side critical section.

ncsi_find_dev() walks the global ncsi_dev_list without rcu_read_lock().
ncsi_write_package_info() walks the package and channel lists in process
context from the NCSI_CMD_PKG_INFO netlink handler, which does not
require GENL_ADMIN_PERM -- any unprivileged user can trigger the walk.
The dumpit callback ncsi_pkg_info_all_nl() has the same issue in its
initial package lookup.

Without RCU read-side protection these walks race with
ncsi_unregister_dev() which removes entries via list_del_rcu().
kfree_rcu() on the free side would be ineffective without matching
rcu_read_lock() on the reader side.

Add rcu_read_lock()/rcu_read_unlock() around the list walks in
ncsi_find_dev(), ncsi_write_package_info() (converting the early returns
to a goto to consolidate the unlock), and ncsi_pkg_info_all_nl().

The GENL_ADMIN_PERM handlers (set_interface, clear_interface,
set_channel_mask) also walk these lists without RCU protection; they are
less critical since they require CAP_NET_ADMIN, but should be addressed
in a follow-up series.

Found by manual code inspection; not triggered at runtime.

Fixes: 955dc68cb9b23 ("net/ncsi: Add generic netlink family")
Cc: stable@vger.kernel.org
Signed-off-by: Tristan Madani <tristan@talencesecurity.com>
---
 net/ncsi/ncsi-manage.c  | 11 ++++++++---
 net/ncsi/ncsi-netlink.c | 32 +++++++++++++++++++++-----------
 2 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/net/ncsi/ncsi-manage.c b/net/ncsi/ncsi-manage.c
index 1d63958c4429b..ec7c0d8cf3d9f 100644
--- a/net/ncsi/ncsi-manage.c
+++ b/net/ncsi/ncsi-manage.c
@@ -419,13 +419,18 @@ void ncsi_free_request(struct ncsi_request *nr)
 struct ncsi_dev *ncsi_find_dev(struct net_device *dev)
 {
 	struct ncsi_dev_priv *ndp;
+	struct ncsi_dev *nd = NULL;
 
+	rcu_read_lock();
 	NCSI_FOR_EACH_DEV(ndp) {
-		if (ndp->ndev.dev == dev)
-			return &ndp->ndev;
+		if (ndp->ndev.dev == dev) {
+			nd = &ndp->ndev;
+			break;
+		}
 	}
+	rcu_read_unlock();
 
-	return NULL;
+	return nd;
 }
 
 static void ncsi_request_timeout(struct timer_list *t)
diff --git a/net/ncsi/ncsi-netlink.c b/net/ncsi/ncsi-netlink.c
index 8cc538358f6a3..f3b403e1a2f2e 100644
--- a/net/ncsi/ncsi-netlink.c
+++ b/net/ncsi/ncsi-netlink.c
@@ -106,37 +106,42 @@ static int ncsi_write_package_info(struct sk_buff *skb,
 	}
 
 	found = false;
+	rcu_read_lock();
 	NCSI_FOR_EACH_PACKAGE(ndp, np) {
 		if (np->id != id)
 			continue;
 		pnest = nla_nest_start_noflag(skb, NCSI_PKG_ATTR);
-		if (!pnest)
-			return -ENOMEM;
+		if (!pnest) {
+			rc = -ENOMEM;
+			goto out_rcu;
+		}
 		rc = nla_put_u32(skb, NCSI_PKG_ATTR_ID, np->id);
 		if (rc) {
 			nla_nest_cancel(skb, pnest);
-			return rc;
+			goto out_rcu;
 		}
 		if ((0x1 << np->id) == ndp->package_whitelist)
 			nla_put_flag(skb, NCSI_PKG_ATTR_FORCED);
 		cnest = nla_nest_start_noflag(skb, NCSI_PKG_ATTR_CHANNEL_LIST);
 		if (!cnest) {
 			nla_nest_cancel(skb, pnest);
-			return -ENOMEM;
+			rc = -ENOMEM;
+			goto out_rcu;
 		}
 		NCSI_FOR_EACH_CHANNEL(np, nc) {
 			nest = nla_nest_start_noflag(skb, NCSI_CHANNEL_ATTR);
 			if (!nest) {
 				nla_nest_cancel(skb, cnest);
 				nla_nest_cancel(skb, pnest);
-				return -ENOMEM;
+				rc = -ENOMEM;
+				goto out_rcu;
 			}
 			rc = ncsi_write_channel_info(skb, ndp, nc);
 			if (rc) {
 				nla_nest_cancel(skb, nest);
 				nla_nest_cancel(skb, cnest);
 				nla_nest_cancel(skb, pnest);
-				return rc;
+				goto out_rcu;
 			}
 			nla_nest_end(skb, nest);
 		}
@@ -145,10 +150,11 @@ static int ncsi_write_package_info(struct sk_buff *skb,
 		found = true;
 	}
 
-	if (!found)
-		return -ENODEV;
+	rc = found ? 0 : -ENODEV;
 
-	return 0;
+out_rcu:
+	rcu_read_unlock();
+	return rc;
 }
 
 static int ncsi_pkg_info_nl(struct sk_buff *msg, struct genl_info *info)
@@ -236,9 +242,13 @@ static int ncsi_pkg_info_all_nl(struct sk_buff *skb,
 
 	package_id = cb->args[0];
 	package = NULL;
+	rcu_read_lock();
 	NCSI_FOR_EACH_PACKAGE(ndp, np)
-		if (np->id == package_id)
+		if (np->id == package_id) {
 			package = np;
+			break;
+		}
+	rcu_read_unlock();
 
 	if (!package)
 		return 0; /* done */
@@ -255,7 +265,7 @@ static int ncsi_pkg_info_all_nl(struct sk_buff *skb,
 		rc = -EMSGSIZE;
 		goto err;
 	}
-	rc = ncsi_write_package_info(skb, ndp, package->id);
+	rc = ncsi_write_package_info(skb, ndp, package_id);
 	if (rc) {
 		nla_nest_cancel(skb, attr);
 		goto err;
-- 
2.47.3


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 net 2/2] net/ncsi: defer free of removed list entries to an RCU grace period
  2026-11-20 23:54 [PATCH v2 net 0/2] net/ncsi: fix RCU list entry lifetime on package and channel removal Tristan Madani
  2026-11-20 23:54 ` [PATCH v2 net 1/2] net/ncsi: add missing rcu_read_lock to netlink and device list walks Tristan Madani
@ 2026-11-20 23:54 ` Tristan Madani
  1 sibling, 0 replies; 3+ messages in thread
From: Tristan Madani @ 2026-11-20 23:54 UTC (permalink / raw)
  To: Samuel Mendoza-Jonas, Paul Fertser
  Cc: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, netdev, linux-kernel, stable, Tristan Madani

From: Tristan Madani <tristan@talencesecurity.com>

ncsi_remove_channel(), ncsi_remove_package() and ncsi_unregister_dev()
each call list_del_rcu() and then immediately kfree() the entry.
Concurrent RCU readers that found the entry before the removal can
still dereference the freed object.

ncsi_remove_channel() also kfree()s the channel filter buffers
(mac_filter.addrs, vlan_filter.vids) while the channel is still on
the list.  An unprivileged NCSI_CMD_PKG_INFO caller walking
ncsi_write_channel_info() can follow vlan_filter.vids into freed
memory.

Additionally, ncsi_unregister_dev() frees all packages and channels
before calling disable_work_sync(), so the work function can run on
already-freed structures.

Add struct rcu_head to ncsi_channel and free channels through a
call_rcu() callback that releases the filter buffers and the channel
together, so the memory remains valid for the full RCU grace period.
Use kfree_rcu_mightsleep() for packages and the device structure
which have no sub-allocations accessed by RCU readers.  Move
disable_work_sync() before the package removal loop so the work
function is quiesced before any structures are unlinked.

Found by manual code inspection; not triggered at runtime.

Fixes: 2d283bdd079c ("net/ncsi: Resource management")
Cc: stable@vger.kernel.org
Signed-off-by: Tristan Madani <tristan@talencesecurity.com>
---
 net/ncsi/internal.h    |  1 +
 net/ncsi/ncsi-manage.c | 28 ++++++++++++++++------------
 2 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/net/ncsi/internal.h b/net/ncsi/internal.h
index 2c9d1f22c16a9..2cb1cf653164c 100644
--- a/net/ncsi/internal.h
+++ b/net/ncsi/internal.h
@@ -239,6 +239,7 @@ struct ncsi_channel {
 	} monitor;
 	struct list_head            node;
 	struct list_head            link;
+	struct rcu_head             rcu;
 };
 
 struct ncsi_package {
diff --git a/net/ncsi/ncsi-manage.c b/net/ncsi/ncsi-manage.c
index ec7c0d8cf3d9f..d872351d40245 100644
--- a/net/ncsi/ncsi-manage.c
+++ b/net/ncsi/ncsi-manage.c
@@ -242,28 +242,33 @@ struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np, unsigned char id)
 	return nc;
 }
 
+static void ncsi_channel_free_rcu(struct rcu_head *head)
+{
+	struct ncsi_channel *nc = container_of(head, struct ncsi_channel, rcu);
+
+	kfree(nc->mac_filter.addrs);
+	kfree(nc->vlan_filter.vids);
+	kfree(nc);
+}
+
 static void ncsi_remove_channel(struct ncsi_channel *nc)
 {
 	struct ncsi_package *np = nc->package;
 	unsigned long flags;
 
-	spin_lock_irqsave(&nc->lock, flags);
-
-	/* Release filters */
-	kfree(nc->mac_filter.addrs);
-	kfree(nc->vlan_filter.vids);
+	ncsi_stop_channel_monitor(nc);
 
+	spin_lock_irqsave(&nc->lock, flags);
 	nc->state = NCSI_CHANNEL_INACTIVE;
 	spin_unlock_irqrestore(&nc->lock, flags);
-	ncsi_stop_channel_monitor(nc);
 
-	/* Remove and free channel */
+	/* Remove from list and defer free until after RCU grace period */
 	spin_lock_irqsave(&np->lock, flags);
 	list_del_rcu(&nc->node);
 	np->channel_num--;
 	spin_unlock_irqrestore(&np->lock, flags);
 
-	kfree(nc);
+	call_rcu(&nc->rcu, ncsi_channel_free_rcu);
 }
 
 struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp,
@@ -326,7 +331,7 @@ void ncsi_remove_package(struct ncsi_package *np)
 	ndp->package_num--;
 	spin_unlock_irqrestore(&ndp->lock, flags);
 
-	kfree(np);
+	kfree_rcu_mightsleep(np);
 }
 
 void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp,
@@ -1964,6 +1969,7 @@ void ncsi_unregister_dev(struct ncsi_dev *nd)
 	unsigned long flags;
 
 	dev_remove_pack(&ndp->ptype);
+	disable_work_sync(&ndp->work);
 
 	list_for_each_entry_safe(np, tmp, &ndp->packages, node)
 		ncsi_remove_package(np);
@@ -1972,8 +1978,6 @@ 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);
+	kfree_rcu_mightsleep(ndp);
 }
 EXPORT_SYMBOL_GPL(ncsi_unregister_dev);
-- 
2.47.3


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-20 23:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20 23:54 [PATCH v2 net 0/2] net/ncsi: fix RCU list entry lifetime on package and channel removal Tristan Madani
2026-11-20 23:54 ` [PATCH v2 net 1/2] net/ncsi: add missing rcu_read_lock to netlink and device list walks Tristan Madani
2026-11-20 23:54 ` [PATCH v2 net 2/2] net/ncsi: defer free of removed list entries to an RCU grace period Tristan Madani

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®