mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net v1] net/ncsi: fix use-after-free of ncsi_package in ncsi_remove_package()
@ 2026-10-17  1:24 Binbin Deng
  2026-10-18  2:05 ` netdev-bot+sashiko
  0 siblings, 1 reply; 2+ messages in thread
From: Binbin Deng @ 2026-10-17  1:24 UTC (permalink / raw)
  To: sam, fercerpav, davem, edumazet, kuba, pabeni, horms, gwshan, joel
  Cc: netdev, linux-kernel, Binbin Deng

ncsi_remove_package() unlinks the package from ndp->packages with
list_del_rcu() and immediately kfree()s it, after having released all
of its channels the same way.  As with the channels, the generic
netlink package info walkers iterate ndp->packages with
list_for_each_entry_rcu() and dereference the package in the loop body
(nla_put_u8(NCSI_PKG_ATTR_ID, np->id), np->multi_channel,
np->channels, ...), so an immediate kfree() turns a mid-walk reader
into a use-after-free the moment a teardown overlaps it.

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

Free the package with kfree_rcu() so that it survives the grace period
the readers rely on.

Tested on an arm multi_v7 kernel (KASAN + SLUB + PREEMPT) running as a
QEMU ast2600-evb guest with a host-side NC-SI responder; the PoC
hammers NCSI_CMD_PKG_INFO while cycling insmod/rmmod ftgmac100:

  BUG: KASAN: slab-use-after-free in ncsi_write_package_info+0xe3c/0xf84
  Read of size 1 at addr c69e2580 by task poc_dump/69
   ncsi_write_package_info from ncsi_pkg_info_nl+0x320/0x63c
   netlink_unicast from netlink_sendmsg+0x62c/0xb98
  Freed by task 65:
   which belongs to the cache kmalloc-64 of size 64
  The buggy address is located 0 bytes inside of

struct ncsi_package is 60 bytes and lands in kmalloc-64; a size-1 read
at offset 0 of that cache can only be np->id in this walker (the
u16 vids[] array reads show up as "Read of size 2"), pinning the report
to the kfree() fixed here.

Found during a static-analysis-assisted audit: candidate sites were
surfaced by a Coccinelle rule, and the reader/writer paths were
confirmed by LLM-assisted (GLM-5.3) code inspection.

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

diff --git a/net/ncsi/internal.h b/net/ncsi/internal.h
index adee6dcabdc3..082862581ea6 100644
--- a/net/ncsi/internal.h
+++ b/net/ncsi/internal.h
@@ -253,6 +253,7 @@ struct ncsi_package {
 	bool                 multi_channel; /* Enable multiple channels  */
 	u32                  channel_whitelist; /* Channels to configure */
 	struct ncsi_channel  *preferred_channel; /* Primary channel      */
+	struct rcu_head      rcu;
 };
 
 struct ncsi_request {
diff --git a/net/ncsi/ncsi-manage.c b/net/ncsi/ncsi-manage.c
index 54d0df0a9efe..76b2cb9efe4f 100644
--- a/net/ncsi/ncsi-manage.c
+++ b/net/ncsi/ncsi-manage.c
@@ -326,7 +326,7 @@ void ncsi_remove_package(struct ncsi_package *np)
 	ndp->package_num--;
 	spin_unlock_irqrestore(&ndp->lock, flags);
 
-	kfree(np);
+	kfree_rcu(np, rcu);
 }
 
 void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp,
-- 
2.43.0


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

end of thread, other threads:[~2026-10-18  2:05 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-17  1:24 [PATCH net v1] net/ncsi: fix use-after-free of ncsi_package in ncsi_remove_package() Binbin Deng
2026-10-18  2:05 ` 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

all inboxes | Powered by JetHome®