Netdev List
 help / color / mirror / Atom feed
From: Bin Luo via B4 Relay <devnull+luob87709.gmail.com@kernel.org>
To: David Heidelberg <david@ixit.cz>,
	 "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	 Jakub Kicinski <kuba@kernel.org>,
	Paolo Abeni <pabeni@redhat.com>,  Simon Horman <horms@kernel.org>
Cc: Krzysztof Kozlowski <krzk@kernel.org>,
	oe-linux-nfc@lists.linux.dev,  netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org,  stable@vger.kernel.org
Subject: [PATCH net v3 2/3] nfc: nfcsim: stop link and works before unregistering digital device
Date: Sun, 06 Dec 2026 10:52:15 -0000	[thread overview]
Message-ID: <20261206105215.479-3-luob87709@gmail.com> (raw)
In-Reply-To: <20261206105215.479-1-luob87709@gmail.com>

From: Bin Luo <luob87709@gmail.com>

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@vger.kernel.org
Signed-off-by: Bin Luo <luob87709@gmail.com>
---
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




  reply	other threads:[~2026-12-06 10:52 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06 10:52 [PATCH net v3 0/3] nfc: fix LLCP, nfcsim, and digital teardown ordering races Bin Luo via B4 Relay
2026-12-06 10:52 ` Bin Luo via B4 Relay [this message]
2026-12-06 10:52 ` [PATCH net v3 1/3] nfc: llcp: fix work and timer teardown order in local_cleanup() Bin Luo via B4 Relay
2026-12-06 10:52 ` [PATCH net v3 3/3] nfc: digital: stop works and drain commands before nfc_remove_device() Bin Luo via B4 Relay

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261206105215.479-3-luob87709@gmail.com \
    --to=devnull+luob87709.gmail.com@kernel.org \
    --cc=davem@davemloft.net \
    --cc=david@ixit.cz \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=krzk@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luob87709@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=oe-linux-nfc@lists.linux.dev \
    --cc=pabeni@redhat.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox