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 v2 2/3] nfc: nfcsim: stop link and works before unregistering digital device
Date: Fri, 04 Dec 2026 14:06:58 +0800	[thread overview]
Message-ID: <20261204060656.503-3-luob87709@gmail.com> (raw)
In-Reply-To: <20261204060656.503-1-luob87709@gmail.com>

From: "Bin Luo (University of Electronic Science and Technology of China)" <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 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) <luob87709@gmail.com>
---
Changes in v2:
- Resend cleanly via b4 web relay (v1 patch was line-wrapped by Gmail)
  and include all three NFC teardown fixes as a threaded series.

 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




  parent reply	other threads:[~2026-12-04  6:07 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  6:06 [PATCH net v2 0/3] nfc: fix LLCP, nfcsim, and digital device teardown ordering Bin Luo via B4 Relay
2026-12-04  6:06 ` [PATCH net v2 1/3] nfc: llcp: fix work and timer teardown order in local_cleanup() Bin Luo via B4 Relay
2026-12-04  6:06 ` Bin Luo via B4 Relay [this message]
2026-12-04  6:06 ` [PATCH net v2 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=20261204060656.503-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