* [PATCH net v4] net/ncsi: fix use-after-free of ncsi_channel in ncsi_remove_channel()
@ 2026-11-21 6:56 Binbin Deng
2026-11-25 7:16 ` netdev-bot+sashiko
0 siblings, 1 reply; 2+ messages in thread
From: Binbin Deng @ 2026-11-21 6:56 UTC (permalink / raw)
To: sam, fercerpav, davem, edumazet, kuba, pabeni, horms
Cc: linux-kernel, netdev, Binbin Deng
ncsi_remove_channel() frees the channel filter arrays and the channel
itself immediately after list_del_rcu():
kfree(nc->mac_filter.addrs);
kfree(nc->vlan_filter.vids);
...
list_del_rcu(&nc->node);
...
kfree(nc);
The generic netlink package info walkers iterate np->channels with the
list_for_each_entry_rcu() based NCSI_FOR_EACH_CHANNEL() and
dereference deep into the channel: ncsi_write_channel_info() reads
nc->version.fw_name with nla_put_string() (strlen) and iterates
nc->vlan_filter.vids[]. No NCSI genl handler enters an RCU read-side
critical section -- genl doit and dumpit callbacks run under
genl_mutex and cb_mutex -- so a walk that overlaps the teardown reads
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()
-> ncsi_remove_package() -> ncsi_remove_channel()
reader: sendmsg(NETLINK_GENERIC, NCSI_CMD_PKG_INFO)
-> ncsi_pkg_info_nl() -> ncsi_write_package_info()
KASAN reports (arm multi_v7, QEMU ast2600-evb, KASAN + SLUB + PREEMPT,
four threads looping PKG_INFO doit/dump while cycling insmod/rmmod):
BUG: KASAN: slab-use-after-free in ncsi_write_package_info+0xa5c/0xf84
Read of size 1 at addr c69019e0 by task poc_dump/68
The buggy address belongs to the object at c6901800
which belongs to the cache kmalloc-1k of size 1024
The buggy address is located 480 bytes inside of
freed 1024-byte region [c6901800, c6901c00)
The 480-byte offset is nc->version.fw_name, read by the strlen() of
the nla_put_string() in ncsi_write_channel_info() inlined into the
walker; kmalloc-1k is the size class of struct ncsi_channel. The
dump variant of the same reader is caught reading the freed filter
array instead:
BUG: KASAN: slab-use-after-free in ncsi_write_package_info+0xb6c/0xf84
Read of size 2 at addr c6b03600 by task poc_dump/72
The buggy address belongs to the object at c6b03600
which belongs to the cache kmalloc-64 of size 64
The buggy address is located 0 bytes inside of
freed 64-byte region [c6b03600, c6b03640)
which is the u16 vlan_filter.vids[] walk of the same handler.
Fix both halves of the contract for the channel:
* The free of the channel and of the filter arrays, which are only
reachable through the channel, moves into an RCU callback.
* All seven genl handlers now resolve the device and walk the lists
inside an RCU read-side critical section, so the grace period of
that callback protects them. The critical sections are
non-sleeping: the reply skb of the PKG_INFO doit is allocated
before the lock is taken (genlmsg_new(GFP_KERNEL) must not run
under rcu_read_lock()) and its unicast reply is sent after the
walk; NCSI_CMD_SEND_CMD runs ncsi_xmit_cmd() under the lock (it
only uses GFP_ATOMIC command skbs, spinlocks and
dev_queue_xmit()) and defers its error notification past the
unlock, as nlmsg_unicast() may sleep; the net_device the
notification uses is pinned by a dev_hold() taken inside the
critical section and released once the notification has been
sent.
* The handlers used get_net(sock_net(skb->sk)), which takes a
reference on init_net that is never dropped anywhere in
net/ncsi/; plain sock_net() is enough as the socket pins the
netns.
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")
Fixes: 955dc68cb9b2 ("net/ncsi: Add generic netlink family")
Signed-off-by: Binbin Deng <18983559317@163.com>
---
Changes in v4:
- Pin the net_device used by the SEND_CMD error notification with
dev_hold()/dev_put(); the notification runs after the RCU unlock,
where the pointer captured inside the critical section no longer
has any protection.
- Reword the commit message and the comment in ncsi_pkg_info_nl() so
they describe only the lifetime rules this patch itself establishes,
without referring to other in-flight changes.
net/ncsi/internal.h | 1 +
net/ncsi/ncsi-manage.c | 20 +++++---
net/ncsi/ncsi-netlink.c | 102 +++++++++++++++++++++++++++++++---------
3 files changed, 95 insertions(+), 28 deletions(-)
diff --git a/net/ncsi/internal.h b/net/ncsi/internal.h
index adee6dc..530834d 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 54d0df0..2b117cf 100644
--- a/net/ncsi/ncsi-manage.c
+++ b/net/ncsi/ncsi-manage.c
@@ -242,17 +242,21 @@ struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np, unsigned char id)
return nc;
}
+static void ncsi_channel_release(struct rcu_head *rcu)
+{
+ struct ncsi_channel *nc = container_of(rcu, 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);
-
nc->state = NCSI_CHANNEL_INACTIVE;
spin_unlock_irqrestore(&nc->lock, flags);
ncsi_stop_channel_monitor(nc);
@@ -263,7 +267,11 @@ static void ncsi_remove_channel(struct ncsi_channel *nc)
np->channel_num--;
spin_unlock_irqrestore(&np->lock, flags);
- kfree(nc);
+ /* Netlink walkers may still hold the channel or its filter
+ * arrays; the arrays are only reachable through @nc and share
+ * its grace period.
+ */
+ call_rcu(&nc->rcu, ncsi_channel_release);
}
struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp,
diff --git a/net/ncsi/ncsi-netlink.c b/net/ncsi/ncsi-netlink.c
index 8cc5383..3310a9c 100644
--- a/net/ncsi/ncsi-netlink.c
+++ b/net/ncsi/ncsi-netlink.c
@@ -169,11 +169,6 @@ static int ncsi_pkg_info_nl(struct sk_buff *msg, struct genl_info *info)
if (!info->attrs[NCSI_ATTR_PACKAGE_ID])
return -EINVAL;
- ndp = ndp_from_ifindex(genl_info_net(info),
- nla_get_u32(info->attrs[NCSI_ATTR_IFINDEX]));
- if (!ndp)
- return -ENODEV;
-
skb = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
if (!skb)
return -ENOMEM;
@@ -192,7 +187,23 @@ static int ncsi_pkg_info_nl(struct sk_buff *msg, struct genl_info *info)
kfree_skb(skb);
return -EMSGSIZE;
}
+
+ /* Resolve the device and walk its lists inside an RCU read-side
+ * critical section: the channel and its filter arrays are freed
+ * from an RCU callback, so the walker is protected by that
+ * grace period.
+ */
+ rcu_read_lock();
+ ndp = ndp_from_ifindex(genl_info_net(info),
+ nla_get_u32(info->attrs[NCSI_ATTR_IFINDEX]));
+ if (!ndp) {
+ rcu_read_unlock();
+ kfree_skb(skb);
+ return -ENODEV;
+ }
+
rc = ncsi_write_package_info(skb, ndp, package_id);
+ rcu_read_unlock();
if (rc) {
nla_nest_cancel(skb, attr);
@@ -228,20 +239,25 @@ static int ncsi_pkg_info_all_nl(struct sk_buff *skb,
if (!attrs[NCSI_ATTR_IFINDEX])
return -EINVAL;
- ndp = ndp_from_ifindex(get_net(sock_net(skb->sk)),
- nla_get_u32(attrs[NCSI_ATTR_IFINDEX]));
+ package_id = cb->args[0];
- if (!ndp)
+ rcu_read_lock();
+ ndp = ndp_from_ifindex(sock_net(skb->sk),
+ nla_get_u32(attrs[NCSI_ATTR_IFINDEX]));
+ if (!ndp) {
+ rcu_read_unlock();
return -ENODEV;
+ }
- package_id = cb->args[0];
package = NULL;
NCSI_FOR_EACH_PACKAGE(ndp, np)
if (np->id == package_id)
package = np;
- if (!package)
+ if (!package) {
+ rcu_read_unlock();
return 0; /* done */
+ }
hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
&ncsi_genl_family, NLM_F_MULTI, NCSI_CMD_PKG_INFO);
@@ -266,8 +282,10 @@ static int ncsi_pkg_info_all_nl(struct sk_buff *skb,
cb->args[0] = package_id + 1;
+ rcu_read_unlock();
return skb->len;
err:
+ rcu_read_unlock();
genlmsg_cancel(skb, hdr);
return rc;
}
@@ -289,10 +307,13 @@ static int ncsi_set_interface_nl(struct sk_buff *msg, struct genl_info *info)
if (!info->attrs[NCSI_ATTR_PACKAGE_ID])
return -EINVAL;
- ndp = ndp_from_ifindex(get_net(sock_net(msg->sk)),
+ rcu_read_lock();
+ ndp = ndp_from_ifindex(sock_net(msg->sk),
nla_get_u32(info->attrs[NCSI_ATTR_IFINDEX]));
- if (!ndp)
+ if (!ndp) {
+ rcu_read_unlock();
return -ENODEV;
+ }
package_id = nla_get_u32(info->attrs[NCSI_ATTR_PACKAGE_ID]);
package = NULL;
@@ -302,6 +323,7 @@ static int ncsi_set_interface_nl(struct sk_buff *msg, struct genl_info *info)
package = np;
if (!package) {
/* The user has set a package that does not exist */
+ rcu_read_unlock();
return -ERANGE;
}
@@ -317,6 +339,7 @@ static int ncsi_set_interface_nl(struct sk_buff *msg, struct genl_info *info)
netdev_info(ndp->ndev.dev,
"NCSI: Channel %u does not exist!\n",
channel_id);
+ rcu_read_unlock();
return -ERANGE;
}
}
@@ -350,6 +373,7 @@ static int ncsi_set_interface_nl(struct sk_buff *msg, struct genl_info *info)
if (!(ndp->flags & NCSI_DEV_RESET))
ncsi_reset_dev(&ndp->ndev);
+ rcu_read_unlock();
return 0;
}
@@ -365,10 +389,13 @@ static int ncsi_clear_interface_nl(struct sk_buff *msg, struct genl_info *info)
if (!info->attrs[NCSI_ATTR_IFINDEX])
return -EINVAL;
- ndp = ndp_from_ifindex(get_net(sock_net(msg->sk)),
+ rcu_read_lock();
+ ndp = ndp_from_ifindex(sock_net(msg->sk),
nla_get_u32(info->attrs[NCSI_ATTR_IFINDEX]));
- if (!ndp)
+ if (!ndp) {
+ rcu_read_unlock();
return -ENODEV;
+ }
/* Reset any whitelists and disable multi mode */
spin_lock_irqsave(&ndp->lock, flags);
@@ -389,12 +416,14 @@ static int ncsi_clear_interface_nl(struct sk_buff *msg, struct genl_info *info)
if (!(ndp->flags & NCSI_DEV_RESET))
ncsi_reset_dev(&ndp->ndev);
+ rcu_read_unlock();
return 0;
}
static int ncsi_send_cmd_nl(struct sk_buff *msg, struct genl_info *info)
{
struct ncsi_dev_priv *ndp;
+ struct net_device *dev;
struct ncsi_pkt_hdr *hdr;
struct ncsi_cmd_arg nca;
unsigned char *data;
@@ -427,17 +456,26 @@ static int ncsi_send_cmd_nl(struct sk_buff *msg, struct genl_info *info)
goto out;
}
- ndp = ndp_from_ifindex(get_net(sock_net(msg->sk)),
+ rcu_read_lock();
+ ndp = ndp_from_ifindex(sock_net(msg->sk),
nla_get_u32(info->attrs[NCSI_ATTR_IFINDEX]));
if (!ndp) {
+ rcu_read_unlock();
ret = -ENODEV;
goto out;
}
+ dev = ndp->ndev.dev;
+ /* The error notification below runs after the unlock and may
+ * sleep; pin the net_device for it while still inside the
+ * read-side critical section.
+ */
+ dev_hold(dev);
package_id = nla_get_u32(info->attrs[NCSI_ATTR_PACKAGE_ID]);
channel_id = nla_get_u32(info->attrs[NCSI_ATTR_CHANNEL_ID]);
if (package_id >= NCSI_MAX_PACKAGE || channel_id >= NCSI_MAX_CHANNEL) {
+ rcu_read_unlock();
ret = -ERANGE;
goto out_netlink;
}
@@ -446,6 +484,7 @@ static int ncsi_send_cmd_nl(struct sk_buff *msg, struct genl_info *info)
if (len < sizeof(struct ncsi_pkt_hdr)) {
netdev_info(ndp->ndev.dev, "NCSI: no command to send %u\n",
package_id);
+ rcu_read_unlock();
ret = -EINVAL;
goto out_netlink;
} else {
@@ -462,23 +501,31 @@ static int ncsi_send_cmd_nl(struct sk_buff *msg, struct genl_info *info)
nca.info = info;
nca.payload = ntohs(hdr->length);
if (nca.payload > len - sizeof(*hdr)) {
+ rcu_read_unlock();
ret = -EINVAL;
goto out_netlink;
}
nca.data = data + sizeof(*hdr);
+ /* ncsi_xmit_cmd() is safe to run under the RCU read-side
+ * critical section: it only uses GFP_ATOMIC command skbs,
+ * spinlocks and dev_queue_xmit().
+ */
ret = ncsi_xmit_cmd(&nca);
+ rcu_read_unlock();
+
out_netlink:
if (ret != 0) {
- netdev_err(ndp->ndev.dev,
+ netdev_err(dev,
"NCSI: Error %d sending command\n",
ret);
- ncsi_send_netlink_err(ndp->ndev.dev,
+ ncsi_send_netlink_err(dev,
info->snd_seq,
info->snd_portid,
info->nlhdr,
ret);
}
+ dev_put(dev);
out:
return ret;
}
@@ -608,10 +655,13 @@ static int ncsi_set_package_mask_nl(struct sk_buff *msg,
if (!info->attrs[NCSI_ATTR_PACKAGE_MASK])
return -EINVAL;
- ndp = ndp_from_ifindex(get_net(sock_net(msg->sk)),
+ rcu_read_lock();
+ ndp = ndp_from_ifindex(sock_net(msg->sk),
nla_get_u32(info->attrs[NCSI_ATTR_IFINDEX]));
- if (!ndp)
+ if (!ndp) {
+ rcu_read_unlock();
return -ENODEV;
+ }
spin_lock_irqsave(&ndp->lock, flags);
if (nla_get_flag(info->attrs[NCSI_ATTR_MULTI_FLAG])) {
@@ -639,6 +689,7 @@ static int ncsi_set_package_mask_nl(struct sk_buff *msg,
ncsi_reset_dev(&ndp->ndev);
}
+ rcu_read_unlock();
return rc;
}
@@ -663,10 +714,13 @@ static int ncsi_set_channel_mask_nl(struct sk_buff *msg,
if (!info->attrs[NCSI_ATTR_CHANNEL_MASK])
return -EINVAL;
- ndp = ndp_from_ifindex(get_net(sock_net(msg->sk)),
+ rcu_read_lock();
+ ndp = ndp_from_ifindex(sock_net(msg->sk),
nla_get_u32(info->attrs[NCSI_ATTR_IFINDEX]));
- if (!ndp)
+ if (!ndp) {
+ rcu_read_unlock();
return -ENODEV;
+ }
package_id = nla_get_u32(info->attrs[NCSI_ATTR_PACKAGE_ID]);
package = NULL;
@@ -675,8 +729,10 @@ static int ncsi_set_channel_mask_nl(struct sk_buff *msg,
package = np;
break;
}
- if (!package)
+ if (!package) {
+ rcu_read_unlock();
return -ERANGE;
+ }
spin_lock_irqsave(&package->lock, flags);
@@ -690,6 +746,7 @@ static int ncsi_set_channel_mask_nl(struct sk_buff *msg,
}
if (!channel) {
spin_unlock_irqrestore(&package->lock, flags);
+ rcu_read_unlock();
return -ERANGE;
}
netdev_dbg(ndp->ndev.dev,
@@ -721,6 +778,7 @@ static int ncsi_set_channel_mask_nl(struct sk_buff *msg,
if (!(ndp->flags & NCSI_DEV_RESET))
ncsi_reset_dev(&ndp->ndev);
+ rcu_read_unlock();
return 0;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH net v4] net/ncsi: fix use-after-free of ncsi_channel in ncsi_remove_channel()
2026-11-21 6:56 [PATCH net v4] net/ncsi: fix use-after-free of ncsi_channel in ncsi_remove_channel() Binbin Deng
@ 2026-11-25 7:16 ` netdev-bot+sashiko
0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-25 7:16 UTC (permalink / raw)
To: 18983559317
Cc: sam, fercerpav, davem, edumazet, kuba, pabeni, horms,
linux-kernel, netdev
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.
Critical: 0 · High: 1 (2) · Medium: 0 · Low: 0
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] The fix is incomplete.
Pre-existing issues:
- [High] This bug predates the patch.
- [High] This bug predates the patch.
--
net/ncsi: fix use-after-free of ncsi_channel in ncsi_remove_channel()
The patch moves the free of ncsi_channel and its filter arrays into an
RCU callback. It also wraps the device lookup and the list walks of all
NCSI genl handlers in rcu_read_lock() sections. Separately, it drops the
leaked get_net() references and pins the net_device for the SEND_CMD
error notification.
> diff --git a/net/ncsi/ncsi-manage.c b/net/ncsi/ncsi-manage.c
> index 1d63958c4429b..79b2a56c5a935 100644
> --- a/net/ncsi/ncsi-manage.c
> +++ b/net/ncsi/ncsi-manage.c
> @@ -242,17 +242,21 @@ struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np, unsigned char id)
[ ... ]
> 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);
> -
> nc->state = NCSI_CHANNEL_INACTIVE;
> spin_unlock_irqrestore(&nc->lock, flags);
> ncsi_stop_channel_monitor(nc);
> @@ -263,7 +267,11 @@ static void ncsi_remove_channel(struct ncsi_channel *nc)
> np->channel_num--;
> spin_unlock_irqrestore(&np->lock, flags);
>
> - kfree(nc);
> + /* Netlink walkers may still hold the channel or its filter
> + * arrays; the arrays are only reachable through @nc and share
> + * its grace period.
> + */
> + call_rcu(&nc->rcu, ncsi_channel_release);
> }
[Severity: High]
Only the channel free is deferred here. The package and the
ncsi_dev_priv on the same teardown path look like they are still freed
right after list_del_rcu(), with no grace period:
net/ncsi/ncsi-manage.c:ncsi_remove_package() {
...
spin_lock_irqsave(&ndp->lock, flags);
list_del_rcu(&np->node);
ndp->package_num--;
spin_unlock_irqrestore(&ndp->lock, flags);
kfree(np);
}
net/ncsi/ncsi-manage.c:ncsi_unregister_dev() {
...
dev_remove_pack(&ndp->ptype);
list_for_each_entry_safe(np, tmp, &ndp->packages, node)
ncsi_remove_package(np);
spin_lock_irqsave(&ncsi_dev_lock, flags);
list_del_rcu(&ndp->node);
spin_unlock_irqrestore(&ncsi_dev_lock, flags);
disable_work_sync(&ndp->work);
kfree(ndp);
}
The synchronize_net() in dev_remove_pack() runs before any package or
the ndp is unlinked. disable_work_sync() does not wait for RCU readers.
The new rcu_read_lock() sections still walk and dereference these
objects:
- ncsi_pkg_info_nl() and ncsi_pkg_info_all_nl() call
ncsi_write_package_info(). It reads ndp->package_num and
ndp->package_whitelist, and walks NCSI_FOR_EACH_PACKAGE() through
np->id, np->node.next and np->channels.
- ncsi_write_channel_info() reads nc->package->preferred_channel. The
channel now survives the grace period, but its parent package does
not.
- ncsi_set_channel_mask_nl() and ncsi_set_interface_nl() take
package->lock and write package fields.
- ncsi_set_package_mask_nl() and ncsi_clear_interface_nl() take
ndp->lock.
- ncsi_send_cmd_nl()->ncsi_xmit_cmd()->ncsi_alloc_request() takes
ndp->lock and writes ndp->requests[].
ndp_from_ifindex() returns ndp without taking any reference on it.
ftgmac100_remove() also calls ncsi_unregister_dev() before
unregister_netdev():
if (priv->ndev)
ncsi_unregister_dev(priv->ndev);
unregister_netdev(netdev);
So dev_get_by_index() keeps succeeding, and ncsi_find_dev() keeps
returning ndp for the whole teardown.
NCSI_CMD_PKG_INFO has no GENL_ADMIN_PERM. Can the unprivileged
PKG_INFO vs rmmod race from the commit message still hit a
slab-use-after-free, only now on the ncsi_package or ncsi_dev_priv
object instead of the kmalloc-1k channel?
If so, these two claims would not hold for np and ndp:
- the comment added in ncsi_pkg_info_nl(): "the walker is protected
by that grace period"
- the commit message: "All seven genl handlers now resolve the device
and walk the lists inside an RCU read-side critical section, so the
grace period of that callback protects them"
Should np also be freed through kfree_rcu() or call_rcu()? And should a
grace period (synchronize_rcu() or kfree_rcu()) come before
kfree(ndp)?
[Severity: High]
This isn't a bug introduced by this patch, but ncsi_unregister_dev()
removes all packages and channels before it calls
disable_work_sync(&ndp->work). That means ncsi_dev_work can still run
during this teardown.
Can that leave an armed monitor timer inside a channel that
ncsi_channel_release() then frees? Here is the sequence:
rmmod
ncsi_unregister_dev()
ncsi_remove_package()
ncsi_remove_channel()
ncsi_stop_channel_monitor(nc)
...
call_rcu(&nc->rcu, ncsi_channel_release)
ncsi_dev_work, running at the same time with nc == ndp->active_channel
ncsi_configure_channel()
ncsi_start_channel_monitor(nc)
mod_timer(&nc->monitor.timer, jiffies + HZ)
Nothing cancels the timer again before nc is freed. So
ncsi_channel_monitor() could run on freed memory.
The work also uses ndp->active_package and ndp->active_channel outside
any RCU read-side section. Neither ncsi_remove_channel() nor
ncsi_remove_package() clears them, and neither clears hot_channel or
package->preferred_channel.
Could the work then dereference a package that ncsi_remove_package()
already kfree'd, or a channel whose RCU callback has already run?
The work can be pending or running when ncsi_unregister_dev() starts.
It can also be queued during teardown through
ncsi_request_timeout()->ncsi_free_request()->schedule_work(). And
ftgmac100 calls ncsi_unregister_dev() while the netdev is still up.
Would calling disable_work_sync() before tearing down the packages and
channels close this window?
> diff --git a/net/ncsi/ncsi-netlink.c b/net/ncsi/ncsi-netlink.c
> index 8cc538358f6a3..3310a9ca2de9b 100644
> --- a/net/ncsi/ncsi-netlink.c
> +++ b/net/ncsi/ncsi-netlink.c
[ ... ]
> @@ -462,23 +501,31 @@ static int ncsi_send_cmd_nl(struct sk_buff *msg, struct genl_info *info)
> nca.info = info;
> nca.payload = ntohs(hdr->length);
> if (nca.payload > len - sizeof(*hdr)) {
> + rcu_read_unlock();
> ret = -EINVAL;
> goto out_netlink;
> }
> nca.data = data + sizeof(*hdr);
>
> + /* ncsi_xmit_cmd() is safe to run under the RCU read-side
> + * critical section: it only uses GFP_ATOMIC command skbs,
> + * spinlocks and dev_queue_xmit().
> + */
> ret = ncsi_xmit_cmd(&nca);
> + rcu_read_unlock();
[Severity: High]
This isn't a bug introduced by this patch, but is the request timer
that ncsi_xmit_cmd() arms ever cancelled when the device is
unregistered?
net/ncsi/ncsi-cmd.c:ncsi_xmit_cmd() {
...
nr->enabled = true;
mod_timer(&nr->timer, jiffies + 1 * HZ);
...
}
The only timer_delete_sync() for request timers is in
ncsi_free_request(). That runs only when a response arrives or the
timer fires.
ncsi_unregister_dev() calls dev_remove_pack() first, so no more
responses get processed. It then frees the packages, calls
disable_work_sync() and does kfree(ndp). ndp embeds requests[256] and
their timer_list objects.
If a request is still in flight at that point, could
ncsi_request_timeout() fire up to a second later on freed memory? It
would read nr->ndp and take ndp->lock. For event-driven requests,
ncsi_free_request() would also call schedule_work(&ndp->work). The
pending timer_list would stay linked into the timer wheel from inside
the freed object.
This seems likely during probe, where commands to absent packages
always wait out the one second timeout, so the insmod/rmmod cycling in
the commit message would hit it. An outstanding monitor request, or a
NCSI_CMD_SEND_CMD request sent from this path, can trigger it too.
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261121065625.27934-1-18983559317%40163.com
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-25 7:16 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-21 6:56 [PATCH net v4] net/ncsi: fix use-after-free of ncsi_channel in ncsi_remove_channel() Binbin Deng
2026-11-25 7:16 ` netdev-bot+sashiko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox