mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Tristan Madani <tristmd@gmail.com>
To: Samuel Mendoza-Jonas <sam@mendozajonas.com>,
	Paul Fertser <fercerpav@gmail.com>
Cc: "David S . Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org,
	Tristan Madani <tristan@talencesecurity.com>
Subject: [PATCH v2 net 1/2] net/ncsi: add missing rcu_read_lock to netlink and device list walks
Date: Fri, 20 Nov 2026 23:54:06 +0000	[thread overview]
Message-ID: <20261120235407.1911365-2-tristmd@gmail.com> (raw)
In-Reply-To: <20261120235407.1911365-1-tristmd@gmail.com>

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


  reply	other threads:[~2026-11-20 23:54 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261120235407.1911365-2-tristmd@gmail.com \
    --to=tristmd@gmail.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=fercerpav@gmail.com \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sam@mendozajonas.com \
    --cc=stable@vger.kernel.org \
    --cc=tristan@talencesecurity.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®