From: Bin Luo (University of Electronic Science and Technology of China) Commit 5e7b30d24a5b ("nfc: nfcsim: fix use after free during module unload") partially addressed a use-after-free in digital_send_cmd_complete() by returning early in nfcsim_recv_wq() when !dev->up, but it did not close the race completely: nfcsim_device_free() still calls nfc_digital_unregister_device() before setting dev->up = false, shutting down link_in, and stopping recv_work. While nfc_digital_unregister_device() drains and frees every pending struct digital_cmd from ddev->cmd_queue, dev->up is still true and nfcsim_recv_wq() can wake up on an incoming frame from the peer device (or timeout) and invoke dev->cb() -> digital_send_cmd_complete() on the freed struct digital_cmd: BUG: KASAN: slab-use-after-free in digital_send_cmd_complete+0x76/0xa0 Write of size 8 at addr ffff888007d9bb20 by task kworker/3:4/66 Workqueue: events nfcsim_recv_wq [nfcsim] Call Trace: digital_send_cmd_complete+0x76/0xa0 nfcsim_recv_wq+0x30a/0x6c0 [nfcsim] Freed by task 64: kfree+0x308/0x530 nfc_digital_unregister_device+0x1e9/0x2e0 nfcsim_device_free+0x2d/0x1c0 [nfcsim] Mark dev->up = false, shut down dev->link_in, and permanently stop send_work and recv_work with disable_delayed_work_sync() and disable_work_sync() before calling nfc_digital_unregister_device(). Triggered in QEMU KVM testing without kernel delay injection while unloading nfcsim during active LLCP traffic, and verified fixed across 200 nfcsim load/unload cycles with KASAN, LOCKDEP, and DEBUG_OBJECTS enabled. Fixes: 5e7b30d24a5b ("nfc: nfcsim: fix use after free during module unload") Fixes: 204bddcb508f ("NFC: nfcsim: Make use of the Digital layer") Cc: stable@vger.kernel.org Signed-off-by: Bin Luo (University of Electronic Science and Technology of China) --- drivers/nfc/nfcsim.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/nfc/nfcsim.c b/drivers/nfc/nfcsim.c index f8e5c4d..f3ddae6 100644 --- a/drivers/nfc/nfcsim.c +++ b/drivers/nfc/nfcsim.c @@ -411,14 +411,14 @@ static struct nfcsim *nfcsim_device_new(struct nfcsim_link *link_in, static void nfcsim_device_free(struct nfcsim *dev) { - nfc_digital_unregister_device(dev->nfc_digital_dev); - dev->up = false; nfcsim_link_shutdown(dev->link_in); - cancel_delayed_work_sync(&dev->send_work); - cancel_work_sync(&dev->recv_work); + disable_delayed_work_sync(&dev->send_work); + disable_work_sync(&dev->recv_work); + + nfc_digital_unregister_device(dev->nfc_digital_dev); nfc_digital_free_device(dev->nfc_digital_dev); -- 2.43.0