[PATCH net v3 2/3] nfc: nfcsim: stop link and works before unregistering digital device

From: Bin Luo via B4 Relay

Date: Sun Dec 06 2026 - 05:53:14 EST


From: Bin Luo <luob87709@xxxxxxxxx>

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 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, nfcsim_recv_wq() can still
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]

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(). Keep dev->up = false after
nfc_digital_unregister_device() so any digital_wq_cmd() invocation that
races before unregister stops cmd_work still receives rc = 0 and stays
on ddev->cmd_queue to be properly completed with -ENODEV by the drain
loop (avoiding digital_data_exch and upper-layer callback leaks).

Also update nfcsim_link_recv_skb() to wake on READ_ONCE(link->shutdown)
and check link->shutdown before validating skb, so a late recv_work
instance cannot sleep for the full receive timeout (up to 5049 ms)
during module unload.

Triggered in QEMU KVM 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
(zero warnings, zero stalls).

Fixes: 5e7b30d24a5b ("nfc: nfcsim: fix use after free during module unload")
Fixes: 204bddcb508f ("NFC: nfcsim: Make use of the Digital layer")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Bin Luo <luob87709@xxxxxxxxx>
---
Changes in v3:
- Keep dev->up = false after nfc_digital_unregister_device() so
in-flight digital_wq_cmd() requests are not silently dropped without
invoking cmd_cb().
- Make nfcsim_link_recv_skb() wake on READ_ONCE(link->shutdown) (set
via WRITE_ONCE() in nfcsim_link_shutdown()) and return -ENODEV
immediately so disable_work_sync(&dev->recv_work) cannot stall for a
full receive timeout.
- Normalize From:/Signed-off-by: author metadata.

Changes in v2:
- Resend cleanly via b4 web relay and include all three NFC teardown
fixes as a threaded series.

drivers/nfc/nfcsim.c | 23 ++++++++++++-----------
1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/drivers/nfc/nfcsim.c b/drivers/nfc/nfcsim.c
index f8e5c4d..a4e653a 100644
--- a/drivers/nfc/nfcsim.c
+++ b/drivers/nfc/nfcsim.c
@@ -116,7 +116,7 @@ static void nfcsim_link_shutdown(struct nfcsim_link *link)
{
mutex_lock(&link->lock);

- link->shutdown = 1;
+ WRITE_ONCE(link->shutdown, 1);
link->mode = NFCSIM_MODE_NONE;

mutex_unlock(&link->lock);
@@ -131,7 +131,8 @@ static struct sk_buff *nfcsim_link_recv_skb(struct nfcsim_link *link,
struct sk_buff *skb;

rc = wait_event_interruptible_timeout(link->recv_wait,
- link->cond,
+ link->cond ||
+ READ_ONCE(link->shutdown),
msecs_to_jiffies(timeout));

mutex_lock(&link->lock);
@@ -144,13 +145,13 @@ static struct sk_buff *nfcsim_link_recv_skb(struct nfcsim_link *link,
goto done;
}

- if (!skb || link->rf_tech != rf_tech || link->mode = mode) {
- rc = -EINVAL;
+ if (link->shutdown) {
+ rc = -ENODEV;
goto done;
}

- if (link->shutdown) {
- rc = -ENODEV;
+ if (!skb || link->rf_tech != rf_tech || link->mode = mode) {
+ rc = -EINVAL;
goto done;
}

@@ -411,14 +412,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);
+ nfcsim_link_shutdown(dev->link_in);

- dev->up = false;
+ disable_delayed_work_sync(&dev->send_work);
+ disable_work_sync(&dev->recv_work);

- nfcsim_link_shutdown(dev->link_in);
+ nfc_digital_unregister_device(dev->nfc_digital_dev);

- cancel_delayed_work_sync(&dev->send_work);
- cancel_work_sync(&dev->recv_work);
+ dev->up = false;

nfc_digital_free_device(dev->nfc_digital_dev);

--
2.43.0