[PATCH 2/2] net/ncsi: fix use-after-free in ncsi_unregister_dev()
From: Potin Lai <hidden>
Date: 2026-09-07 12:18:34
Also in:
lkml, netdev
Subsystem:
ncsi library, networking [general], the rest · Maintainers:
Samuel Mendoza-Jonas, "David S. Miller", Eric Dumazet, Jakub Kicinski, Paolo Abeni, Linus Torvalds
From: Adrian Ambrozewicz <redacted>
ncsi_unregister_dev() frees the ncsi_dev_priv structure while timers
and workqueue may still be accessing it, causing use-after-free.
The problem involves two async mechanisms:
1. Request timers (ncsi_request_timeout) - fire when NCSI responses
are not received in time
2. Workqueue (ncsi_dev_work) - processes NCSI state machine
These can cascade: timer handlers call ncsi_free_request() which may
call schedule_work(), and work can send commands that arm new timers.
The fix adds proper synchronization before kfree():
dev_remove_pack() - stop packet reception
del_timer_sync() x 256 - cancel all request timers
cancel_work_sync() - wait for workqueue to complete
kfree(ndp)
Order matters: timers must be cancelled before work because timer
handlers may schedule new work via ncsi_free_request().
Note: ncsi_dev_work() is non-blocking - it sends a command, arms a
timer, and returns immediately. It does not wait for timer completion.
The timer firing later triggers schedule_work() for the next state.
So cancel_work_sync() will not hang waiting for cancelled timers.
This relies on ncsi_stop_dev() being called first (guaranteed by the
network device lifecycle). ncsi_stop_dev() sets state to
ncsi_dev_state_functional, which causes ncsi_dev_work() to exit
immediately without sending commands or arming timers. This breaks
the timer<->work cycle and ensures the synchronization terminates.
Timeline showing the race (without fix):
CPU 0 (unregister) CPU 1 (async)
------------------ -------------
ncsi_unregister_dev()
dev_remove_pack()
ncsi_request_timeout()
ncsi_free_request()
schedule_work()
kfree(ndp)
ncsi_dev_work()
ndp->... <- UAF!
With fix:
CPU 0 (unregister) CPU 1 (async)
------------------ -------------
ncsi_unregister_dev()
dev_remove_pack()
del_timer_sync() x 256 <- waits for timer handlers
ncsi_request_timeout()
ncsi_free_request()
schedule_work()
cancel_work_sync() <- waits for work
ncsi_dev_work()
state=0x100, exits immediately
kfree(ndp) <- safe
Signed-off-by: Adrian Ambrozewicz <redacted>
Signed-off-by: Potin Lai <redacted>
---
net/ncsi/ncsi-manage.c | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
diff --git a/net/ncsi/ncsi-manage.c b/net/ncsi/ncsi-manage.c
index 54d0df0a9efe..dc5f2a76a0a5 100644
--- a/net/ncsi/ncsi-manage.c
+++ b/net/ncsi/ncsi-manage.c@@ -1957,9 +1957,28 @@ void ncsi_unregister_dev(struct ncsi_dev *nd) struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd); struct ncsi_package *np, *tmp; unsigned long flags; + int i; dev_remove_pack(&ndp->ptype); + /* + * Synchronize with async operations before freeing ndp. + * + * Note: The caller must have called ncsi_stop_dev() first, which + * sets nd->state to ncsi_dev_state_functional (0x100). This causes + * any running or scheduled ncsi_dev_work() to exit immediately + * without sending commands or arming new timers, breaking the + * potential cycle of: work -> arm timer -> timer -> schedule work. + * + * Order matters: + * 1. del_timer_sync() - cancel timers, handlers may schedule work + * 2. cancel_work_sync() - cancel work scheduled by timer handlers + */ + for (i = 0; i < ARRAY_SIZE(ndp->requests); i++) + del_timer_sync(&ndp->requests[i].timer); + + cancel_work_sync(&ndp->work); + list_for_each_entry_safe(np, tmp, &ndp->packages, node) ncsi_remove_package(np);
--
2.52.0