[PATCH v2 net 1/2] net/ncsi: add missing rcu_read_lock to netlink and device list walks
From: Tristan Madani
Date: Fri Nov 20 2026 - 18:55:05 EST
From: Tristan Madani <tristan@xxxxxxxxxxxxxxxxxxx>
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@xxxxxxxxxxxxxxx
Signed-off-by: Tristan Madani <tristan@xxxxxxxxxxxxxxxxxxx>
---
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