Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH net v2 1/3] nfc: llcp: fix work and timer teardown order in local_cleanup()
       [not found] <20261204060656.503-1-luob87709@gmail.com>
@ 2026-12-04  6:06 ` Bin Luo via B4 Relay
  2026-12-04  6:06 ` [PATCH net v2 2/3] nfc: nfcsim: stop link and works before unregistering digital device Bin Luo via B4 Relay
  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
  2 siblings, 0 replies; 3+ messages in thread
From: Bin Luo via B4 Relay @ 2026-12-04  6:06 UTC (permalink / raw)
  To: David Heidelberg, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Simon Horman
  Cc: Krzysztof Kozlowski, oe-linux-nfc, netdev, linux-kernel, stable

From: "Bin Luo (University of Electronic Science and Technology of China)" <luob87709@gmail.com>

In net/nfc/llcp_core.c, the LLCP worker and timer dependency chains are:

  1. rx_work -> schedules tx_work -> arms link_timer ->
     schedules timeout_work
  2. sdreq_timer <-> sdreq_timeout_work (mutually re-arming when
     pending_sdreqs is non-empty)

However, local_cleanup() currently deletes link_timer before cancelling
tx_work, purges tx_queue before cancelling tx_work (which can put an
skb back via skb_queue_head() when !llcp_sock->remote_ready), and
cancels tx_work before cancelling rx_work.  Likewise, it calls
timer_delete_sync(&local->sdreq_timer) before cancelling
sdreq_timeout_work, which can re-arm sdreq_timer via mod_timer().

As a result, an in-flight rx_work can re-queue tx_work after
cancel_work_sync(&local->tx_work), tx_work can re-arm link_timer after
timer_delete_sync(&local->link_timer) or leave a re-queued skb in
tx_queue, and sdreq_timeout_work can re-arm sdreq_timer after
timer_delete_sync(&local->sdreq_timer), leaving active works, timers,
or leaked skbs on struct nfc_llcp_local when local_release_work() frees
it.

Tear down the work items and timers in producer-to-consumer order using
disable_work_sync() and timer_shutdown_sync(), which permanently
prevent re-queuing and re-arming once local_cleanup() begins, and purge
tx_queue after tx_work is disabled.

Discovered by static analysis and AI-assisted code review; not triggered
standalone without delay injection, and regression-tested in QEMU KVM
(KASAN, LOCKDEP, DEBUG_OBJECTS_TIMERS, DEBUG_OBJECTS_WORK) across 200
nfcsim module load/unload cycles with active LLCP links and against
tools/testing/selftests/nci/nci_dev (8/8 passed).

Fixes: c7aa12252f51 ("NFC: Take a reference on the LLCP local pointer when creating a socket")
Fixes: 40213fa8513c ("NFC: llcp: Add cleanup support for unreplied SNL requests")
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.

 net/nfc/llcp_core.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/net/nfc/llcp_core.c b/net/nfc/llcp_core.c
index 74bf817..32530a2 100644
--- a/net/nfc/llcp_core.c
+++ b/net/nfc/llcp_core.c
@@ -173,15 +173,15 @@ static struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local)
 static void local_cleanup(struct nfc_llcp_local *local)
 {
 	nfc_llcp_socket_release(local, false, ENXIO);
-	timer_delete_sync(&local->link_timer);
+	disable_work_sync(&local->rx_work);
+	disable_work_sync(&local->tx_work);
 	skb_queue_purge(&local->tx_queue);
-	cancel_work_sync(&local->tx_work);
-	cancel_work_sync(&local->rx_work);
-	cancel_work_sync(&local->timeout_work);
+	timer_shutdown_sync(&local->link_timer);
+	disable_work_sync(&local->timeout_work);
 	kfree_skb(local->rx_pending);
 	local->rx_pending = NULL;
-	timer_delete_sync(&local->sdreq_timer);
-	cancel_work_sync(&local->sdreq_timeout_work);
+	timer_shutdown_sync(&local->sdreq_timer);
+	disable_work_sync(&local->sdreq_timeout_work);
 	nfc_llcp_free_sdp_tlv_list(&local->pending_sdreqs);
 }
 
-- 
2.43.0




^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH net v2 2/3] nfc: nfcsim: stop link and works before unregistering digital device
       [not found] <20261204060656.503-1-luob87709@gmail.com>
  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
  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
  2 siblings, 0 replies; 3+ messages in thread
From: Bin Luo via B4 Relay @ 2026-12-04  6:06 UTC (permalink / raw)
  To: David Heidelberg, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Simon Horman
  Cc: Krzysztof Kozlowski, oe-linux-nfc, netdev, linux-kernel, stable

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




^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH net v2 3/3] nfc: digital: stop works and drain commands before nfc_remove_device()
       [not found] <20261204060656.503-1-luob87709@gmail.com>
  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 ` [PATCH net v2 2/3] nfc: nfcsim: stop link and works before unregistering digital device Bin Luo via B4 Relay
@ 2026-12-04  6:06 ` Bin Luo via B4 Relay
  2 siblings, 0 replies; 3+ messages in thread
From: Bin Luo via B4 Relay @ 2026-12-04  6:06 UTC (permalink / raw)
  To: David Heidelberg, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Simon Horman
  Cc: Krzysztof Kozlowski, oe-linux-nfc, netdev, linux-kernel, stable

From: "Bin Luo (University of Electronic Science and Technology of China)" <luob87709@gmail.com>

nfc_digital_unregister_device() currently calls nfc_unregister_device()
before cancelling poll_work, cmd_work, and cmd_complete_work and before
draining ddev->cmd_queue.

nfc_unregister_device() calls nfc_llcp_unregister_device(), which
tears down and releases struct nfc_llcp_local (dropping its reference
and scheduling local_release_work() to kfree(local)).  Afterwards,
still-running cmd_complete_work or the cmd_queue drain loop invokes
cmd->cmd_cb(ddev, cmd->cb_context, ...) -> nfc_llcp_recv(local, ...),
passing a struct nfc_llcp_local pointer that has already been
unregistered and may already be freed.  In addition,
digital_wq_cmd_complete() can re-schedule cmd_work after
cancel_work_sync(&ddev->cmd_work) returns.

Mirror nci_unregister_device(): call nfc_unregister_rfkill() first to
set dev->shutting_down and prevent new core operations, permanently
stop poll_work, cmd_work, and cmd_complete_work with
disable_delayed_work_sync()/disable_work_sync(), drain cmd_queue while
LLCP is still registered, and finally call nfc_remove_device().

Discovered by AI-assisted code review after observing nfc_llcp_recv()
callbacks running after nfc_llcp_unregister_device() during nfcsim
module unload; regression-tested in QEMU KVM (KASAN, LOCKDEP,
DEBUG_OBJECTS) across 200 nfcsim load/unload cycles and against
tools/testing/selftests/nci/nci_dev (8/8 passed).

Fixes: 4b10884eb428 ("NFC: Digital Protocol stack implementation")
Fixes: 82e57952869f ("NFC: digital: Call pending command callbacks at device unregister")
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.

 net/nfc/digital_core.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/net/nfc/digital_core.c b/net/nfc/digital_core.c
index 1823622..7b8d72f 100644
--- a/net/nfc/digital_core.c
+++ b/net/nfc/digital_core.c
@@ -836,15 +836,15 @@ void nfc_digital_unregister_device(struct nfc_digital_dev *ddev)
 {
 	struct digital_cmd *cmd, *n;
 
-	nfc_unregister_device(ddev->nfc_dev);
+	nfc_unregister_rfkill(ddev->nfc_dev);
 
 	mutex_lock(&ddev->poll_lock);
 	ddev->poll_tech_count = 0;
 	mutex_unlock(&ddev->poll_lock);
 
-	cancel_delayed_work_sync(&ddev->poll_work);
-	cancel_work_sync(&ddev->cmd_work);
-	cancel_work_sync(&ddev->cmd_complete_work);
+	disable_delayed_work_sync(&ddev->poll_work);
+	disable_work_sync(&ddev->cmd_work);
+	disable_work_sync(&ddev->cmd_complete_work);
 
 	list_for_each_entry_safe(cmd, n, &ddev->cmd_queue, queue) {
 		list_del(&cmd->queue);
@@ -859,6 +859,8 @@ void nfc_digital_unregister_device(struct nfc_digital_dev *ddev)
 		kfree(cmd->mdaa_params);
 		kfree(cmd);
 	}
+
+	nfc_remove_device(ddev->nfc_dev);
 }
 EXPORT_SYMBOL(nfc_digital_unregister_device);
 
-- 
2.43.0




^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-04  6:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20261204060656.503-1-luob87709@gmail.com>
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 ` [PATCH net v2 2/3] nfc: nfcsim: stop link and works before unregistering digital device Bin Luo via B4 Relay
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox