* [PATCH net v2 0/3] nfc: fix LLCP, nfcsim, and digital device teardown ordering
@ 2026-12-04 6:06 ` Bin Luo via B4 Relay
0 siblings, 0 replies; 11+ messages in thread
From: Bin Luo (University of Electronic Science and Technology of China) @ 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
This series fixes three teardown ordering issues across LLCP, nfcsim,
and the NFC Digital stack:
1. Patch 1/3 fixes local_cleanup() in net/nfc/llcp_core.c so work items
and timers are shut down in producer-to-consumer order using
disable_work_sync() and timer_shutdown_sync(), and purges tx_queue
after tx_work is disabled so an skb put back via skb_queue_head() is
not leaked.
2. Patch 2/3 completes commit 5e7b30d24a5b ("nfc: nfcsim: fix use after
free during module unload") by marking dev->up = false, shutting
down link_in, and disabling send_work/recv_work before calling
nfc_digital_unregister_device(), fixing a KASAN slab-use-after-free
write in digital_send_cmd_complete() triggered during nfcsim unload.
3. Patch 3/3 aligns nfc_digital_unregister_device() with
nci_unregister_device() by calling nfc_unregister_rfkill() first,
disabling poll_work/cmd_work/cmd_complete_work, draining cmd_queue
while struct nfc_llcp_local is still registered, and calling
nfc_remove_device() last.
Changes in v2:
- Resend via b4 web relay (v1 had broken diff lines from Gmail wrapping)
and send all three teardown fixes together as a threaded series.
Tested in QEMU KVM (KASAN, LOCKDEP, DEBUG_OBJECTS_TIMERS,
DEBUG_OBJECTS_WORK) across 200 nfcsim load/unload cycles with 32
concurrent raw LLCP sockets and active LLCP traffic, and passed
tools/testing/selftests/nci/nci_dev (8/8).
Bin Luo (University of Electronic Science and Technology of China) (3):
nfc: llcp: fix work and timer teardown order in local_cleanup()
nfc: nfcsim: stop link and works before unregistering digital device
nfc: digital: stop works and drain commands before nfc_remove_device()
drivers/nfc/nfcsim.c | 8 ++++----
net/nfc/digital_core.c | 10 ++++++----
net/nfc/llcp_core.c | 12 ++++++------
3 files changed, 16 insertions(+), 14 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH net v2 0/3] nfc: fix LLCP, nfcsim, and digital device teardown ordering
@ 2026-12-04 6:06 ` Bin Luo via B4 Relay
0 siblings, 0 replies; 11+ 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
This series fixes three teardown ordering issues across LLCP, nfcsim,
and the NFC Digital stack:
1. Patch 1/3 fixes local_cleanup() in net/nfc/llcp_core.c so work items
and timers are shut down in producer-to-consumer order using
disable_work_sync() and timer_shutdown_sync(), and purges tx_queue
after tx_work is disabled so an skb put back via skb_queue_head() is
not leaked.
2. Patch 2/3 completes commit 5e7b30d24a5b ("nfc: nfcsim: fix use after
free during module unload") by marking dev->up = false, shutting
down link_in, and disabling send_work/recv_work before calling
nfc_digital_unregister_device(), fixing a KASAN slab-use-after-free
write in digital_send_cmd_complete() triggered during nfcsim unload.
3. Patch 3/3 aligns nfc_digital_unregister_device() with
nci_unregister_device() by calling nfc_unregister_rfkill() first,
disabling poll_work/cmd_work/cmd_complete_work, draining cmd_queue
while struct nfc_llcp_local is still registered, and calling
nfc_remove_device() last.
Changes in v2:
- Resend via b4 web relay (v1 had broken diff lines from Gmail wrapping)
and send all three teardown fixes together as a threaded series.
Tested in QEMU KVM (KASAN, LOCKDEP, DEBUG_OBJECTS_TIMERS,
DEBUG_OBJECTS_WORK) across 200 nfcsim load/unload cycles with 32
concurrent raw LLCP sockets and active LLCP traffic, and passed
tools/testing/selftests/nci/nci_dev (8/8).
Bin Luo (University of Electronic Science and Technology of China) (3):
nfc: llcp: fix work and timer teardown order in local_cleanup()
nfc: nfcsim: stop link and works before unregistering digital device
nfc: digital: stop works and drain commands before nfc_remove_device()
drivers/nfc/nfcsim.c | 8 ++++----
net/nfc/digital_core.c | 10 ++++++----
net/nfc/llcp_core.c | 12 ++++++------
3 files changed, 16 insertions(+), 14 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH net v2 1/3] nfc: llcp: fix work and timer teardown order in local_cleanup()
2026-12-04 6:06 ` Bin Luo via B4 Relay
@ 2026-12-04 6:06 ` Bin Luo (University of Electronic Science and Technology of China)
-1 siblings, 0 replies; 11+ 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] 11+ messages in thread
* [PATCH net v2 1/3] nfc: llcp: fix work and timer teardown order in local_cleanup()
@ 2026-12-04 6:06 ` Bin Luo (University of Electronic Science and Technology of China)
0 siblings, 0 replies; 11+ messages in thread
From: Bin Luo (University of Electronic Science and Technology of China) @ 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
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] 11+ messages in thread
* [PATCH net v2 2/3] nfc: nfcsim: stop link and works before unregistering digital device
2026-12-04 6:06 ` Bin Luo via B4 Relay
@ 2026-12-04 6:06 ` Bin Luo (University of Electronic Science and Technology of China)
-1 siblings, 0 replies; 11+ 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] 11+ messages in thread
* [PATCH net v2 2/3] nfc: nfcsim: stop link and works before unregistering digital device
@ 2026-12-04 6:06 ` Bin Luo (University of Electronic Science and Technology of China)
0 siblings, 0 replies; 11+ messages in thread
From: Bin Luo (University of Electronic Science and Technology of China) @ 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
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] 11+ messages in thread
* [PATCH net v2 3/3] nfc: digital: stop works and drain commands before nfc_remove_device()
2026-12-04 6:06 ` Bin Luo via B4 Relay
@ 2026-12-04 6:06 ` Bin Luo (University of Electronic Science and Technology of China)
-1 siblings, 0 replies; 11+ 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] 11+ messages in thread
* [PATCH net v2 3/3] nfc: digital: stop works and drain commands before nfc_remove_device()
@ 2026-12-04 6:06 ` Bin Luo (University of Electronic Science and Technology of China)
0 siblings, 0 replies; 11+ messages in thread
From: Bin Luo (University of Electronic Science and Technology of China) @ 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
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] 11+ messages in thread
* Re: [PATCH net v2 1/3] nfc: llcp: fix work and timer teardown order in local_cleanup()
2026-12-04 6:06 ` Bin Luo (University of Electronic Science and Technology of China)
(?)
@ 2026-12-05 6:57 ` netdev-bot+sashiko
-1 siblings, 0 replies; 11+ messages in thread
From: netdev-bot+sashiko @ 2026-12-05 6:57 UTC (permalink / raw)
To: luob87709
Cc: david, davem, edumazet, kuba, pabeni, horms, krzk, oe-linux-nfc,
netdev, linux-kernel, stable
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The commit author has no real name.
- [Low] nfc: llcp: Fixes tag cites c7aa12252f51 instead of d646960f7986
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but
local_cleanup() (net/nfc/llcp_core.c:185) frees local->pending_sdreqs…
- [High] This problem wasn't introduced by this patch, but the data-
exchange completion context is a raw, unreferenced struct…
--
nfc: llcp: fix work and timer teardown order in local_cleanup()
This patch reorders local_cleanup(). rx_work and tx_work are now disabled
before tx_queue is purged and before link_timer and timeout_work are shut
down. It also switches to disable_work_sync() and timer_shutdown_sync(), so
the works and timers cannot be re-queued or re-armed once cleanup has
started.
> Signed-off-by: Bin Luo (University of Electronic Science and Technology of China) <luob87709@gmail.com>
[Severity: Low]
This isn't a bug, but the author name on this commit is only the email
address:
Author: luob87709@gmail.com <luob87709@gmail.com>
The Signed-off-by uses "Bin Luo (University of Electronic Science and
Technology of China)" as the name. checkpatch reports this as
FROM_SIGN_OFF_MISMATCH. If the patch is applied as sent, git history will
record the email address as the author name.
The other two patches in the series have the same metadata:
"nfc: nfcsim: stop link and works before unregistering digital device"
"nfc: digital: stop works and drain commands before nfc_remove_device()"
Could the git author identity be fixed, or a "From: Bin Luo
<luob87709@gmail.com>" line be added to the body?
> diff --git a/net/nfc/llcp_core.c b/net/nfc/llcp_core.c
> index 74bf817007cf2..32530a29fb0cd 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);
[Severity: Low]
This isn't a bug, but is the first Fixes tag pointing at the right
commit?
c7aa12252f51 ("NFC: Take a reference on the LLCP local pointer when
creating a socket") did not create this teardown order. It moved the
existing sequence out of nfc_llcp_unregister_device() and into
local_release(). The order it moved was already
del_timer_sync(&local->link_timer), then
skb_queue_purge(&local->tx_queue), then destroy_workqueue() on tx_wq
and then on rx_wq.
That ordering, and the rx_work -> tx_work -> link_timer dependency
this patch fixes, go back to d646960f7986 ("NFC: Initial LLCP
support"). 474fee3db16c later replaced the destroy_workqueue() calls
with cancel_work_sync() and kept the same order.
The patch is Cc: stable, and the stable tooling uses the Fixes tag to
decide how far back to apply it. Should the first tag be:
Fixes: d646960f7986 ("NFC: Initial LLCP support")
The second tag, 40213fa8513c, looks correct for the sdreq_timer and
sdreq_timeout_work part.
[Severity: High]
This is a pre-existing issue, but is local protected from driver
data-exchange callbacks that are still in flight when it gets freed?
nfc_llcp_tx_work() passes the bare local pointer to the driver as the
callback context and does not take a kref:
net/nfc/llcp_core.c:nfc_llcp_tx_work() {
...
ret = nfc_data_exchange(local->dev, local->target_idx,
skb, nfc_llcp_recv, local);
...
}
When the exchange completes successfully, nfc_llcp_recv()->__nfc_llcp_recv()
writes into local:
local->rx_pending = skb;
timer_delete(&local->link_timer);
schedule_work(&local->rx_work);
pn533 unregisters the LLCP device before it drains its workqueue and before
it kills the URBs:
drivers/nfc/pn533/usb.c:pn533_usb_disconnect() {
...
pn53x_unregister_nfc(phy->priv);
pn53x_common_clean(phy->priv);
...
usb_kill_urb(phy->in_urb);
...
}
The free happens on this path:
pn53x_unregister_nfc()
nfc_unregister_device()
nfc_llcp_unregister_device()
local_cleanup()
nfc_llcp_local_put()
If no socket holds a reference, local_release_work() then runs
local_cleanup() again and calls kfree(local).
After that, destroy_workqueue(priv->wq) in pn53x_common_clean() can run a
pending cmd_complete_work. pn533_data_exchange_complete() then calls
arg->cb(arg->cb_context, skb, 0), where cb_context is the freed local.
On an active LLCP link a DATA_EXCHANGE is almost always in flight, because
tx_work keeps sending SYMM. Error completions are safe: nfc_llcp_recv()
returns before touching local when err < 0. A successful response is
needed, for example a driver unbind or rmmod while the peer is still in the
field.
The disable_work_sync(&local->rx_work) added here makes schedule_work() a
no-op while the memory is still valid. It does not stop the stores to
rx_pending and link_timer after kfree(local).
There is a second case. If the callback lands between kfree_skb(
local->rx_pending) in the second local_cleanup() and kfree(local), the skb
leaks.
The "nfc: digital: stop works and drain commands before
nfc_remove_device()" patch later in this series fixes the ordering only for
Digital-based drivers. Non-digital drivers like pn533 still have this
window.
> 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);
[Severity: High]
This isn't a bug introduced by this patch, but can this free race with a
concurrent NFC_CMD_LLC_SDREQ request?
local_cleanup() walks pending_sdreqs and frees it with hlist_del() and
kfree() without holding local->sdreq_lock. nfc_llcp_send_snl_sdreq() adds
entries to the same list under sdreq_lock:
net/nfc/llcp_commands.c:nfc_llcp_send_snl_sdreq() {
...
mutex_lock(&local->sdreq_lock);
...
hlist_add_head(&sdreq->node, &local->pending_sdreqs);
...
}
nfc_genl_llc_sdreq() takes device_lock but only checks dep_link_up. It
never checks dev->shutting_down:
device_lock(&dev->dev);
if (dev->dep_link_up == false) {
rc = -ENOLINK;
goto exit;
}
local = nfc_llcp_find_local(dev);
On the unregister side, nfc_unregister_device() calls
nfc_unregister_rfkill(), which sets shutting_down and drops device_lock.
It then reaches local_cleanup() without holding device_lock:
nfc_remove_device()
nfc_llcp_unregister_device()
local_cleanup()
So a request can take device_lock after nfc_unregister_rfkill() and call
nfc_llcp_find_local() before nfc_llcp_remove_local(). It then holds a valid
local reference while local_cleanup() runs. One interleaving:
nfc_llcp_send_snl_sdreq() local_cleanup()
hlist_add_head()
N->next = head->first (A)
nfc_llcp_free_sdp_tlv_list()
hlist_del(A); kfree(A)
head->first = N
N->next now points at freed memory.
When the netlink caller drops its reference, local_release_work() runs
local_cleanup() a second time. That walk reaches A, which is a
use-after-free, and frees it a second time.
The timer_shutdown_sync(&local->sdreq_timer) in this patch turns the
mod_timer() call in that path into a harmless no-op. It does not affect the
list race. NFC_CMD_LLC_SDREQ is GENL_ADMIN_PERM, so CAP_NET_ADMIN is
required.
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204060656.503-1-luob87709%40gmail.com
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH net v2 2/3] nfc: nfcsim: stop link and works before unregistering digital device
2026-12-04 6:06 ` Bin Luo (University of Electronic Science and Technology of China)
(?)
@ 2026-12-05 6:57 ` netdev-bot+sashiko
-1 siblings, 0 replies; 11+ messages in thread
From: netdev-bot+sashiko @ 2026-12-05 6:57 UTC (permalink / raw)
To: luob87709
Cc: david, davem, edumazet, kuba, pabeni, horms, krzk, oe-linux-nfc,
netdev, linux-kernel, stable
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 0 (1) · Medium: 2 · Low: 0
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] Clearing dev->up first in nfcsim_device_free() lets in-flight
digital commands be dropped silently, which leaks the caller's…
- [Medium] nfc: nfcsim: dev->up race before unregister stalls recv_work
teardown
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but
port100_disconnect() (drivers/nfc/port100.c:1615-1634) has the same…
--
nfc: nfcsim: stop link and works before unregistering digital device
This patch reorders nfcsim_device_free() so that dev->up is cleared,
link_in is shut down, and send_work/recv_work are disabled before
nfc_digital_unregister_device() drains the digital command queue. The goal
is to close a remaining use-after-free where nfcsim_recv_wq() calls
digital_send_cmd_complete() on a digital_cmd already freed by the drain
loop.
> diff --git a/drivers/nfc/nfcsim.c b/drivers/nfc/nfcsim.c
> index f8e5c4da3b13d..f3ddae6d41823 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;
[Severity: Medium]
If dev->up is cleared this early, can in-flight digital commands get dropped
without their completion callback being called?
The digital core is still running at this point. ddev->cmd_work,
cmd_complete_work and poll_work are only disabled later, inside
nfc_digital_unregister_device().
A new command can still be queued during that window. For example, say
nfcsim_recv_wq() passed the !dev->up check just before teardown and queued
cmd_complete_work. That work then runs after
disable_work_sync(&dev->recv_work) returns. Its cmd_cb can queue a DEP
ACK/RTOX via digital_in_send_cmd(). LLCP tx_work can also call
nfc_data_exchange()->digital_in_send(). shutting_down is not set yet at this
point.
digital_wq_cmd() then reaches nfcsim_send(), which now sees !dev->up:
drivers/nfc/nfcsim.c:nfcsim_send() {
...
if (!dev->up) {
NFCSIM_ERR(dev, "Device is down\n");
return -ENODEV;
}
...
}
The error path in digital_wq_cmd() then frees the cmd without calling
cmd->cmd_cb:
net/nfc/digital_core.c:digital_wq_cmd() {
...
mutex_lock(&ddev->cmd_lock);
list_del(&cmd->queue);
mutex_unlock(&ddev->cmd_lock);
kfree_skb(cmd->req);
kfree(cmd->mdaa_params);
kfree(cmd);
...
}
Would this leak the struct digital_data_exch allocated in digital_in_send()?
It only seems to be freed in digital_in_recv_dep_res().
ddev->saved_skb from digital_in_send_dep_req() also doesn't appear to be
freed, because nfc_digital_free_device() does not release it.
The upper layer would also never get an -ENODEV completion. That layer is
nfc_llcp_recv(), or rawsock_data_exchange_complete(), which drops a socket
reference.
Before this patch, dev->up stayed true until unregister had cancelled
cmd_work. nfcsim_send() then returned 0, and the command stayed queued until
the drain loop called cmd_cb(..., ERR_PTR(-ENODEV)).
Does the use-after-free fix need the early dev->up = false? It looks like
disable_work_sync(&dev->recv_work) before nfc_digital_unregister_device()
would be enough by itself. The commit message also doesn't mention this side
effect.
The window seems to remain after "nfc: digital: stop works and drain
commands before nfc_remove_device()" later in the series too. That change
disables cmd_work inside unregister, which is still after dev->up is
cleared.
>
> 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);
[Severity: Medium]
Is dev->up = false actually one-way at this point? dev->up is a plain
bool that tracks RF state. The digital device stays registered until
nfc_digital_unregister_device() runs below, so ddev->poll_work and the
netlink dev_up/start_poll paths are still live.
nfcsim_in_configure_hw(), nfcsim_tg_configure_hw() and
nfcsim_switch_rf(ddev, true) all set dev->up = true unconditionally:
drivers/nfc/nfcsim.c:nfcsim_in_configure_hw() {
...
case NFC_DIGITAL_CONFIG_RF_TECH:
dev->up = true;
dev->mode = NFCSIM_MODE_INITIATOR;
dev->rf_tech = param;
break;
...
}
Say an in-flight nfcsim_recv_wq() is woken by nfcsim_link_shutdown()
and gets an error skb back. If digital_wq_poll() reconfigures the RF
tech before the !dev->up check, the worker sees dev->up == true again
and calls dev->cb().
That callback can move the poll on and send the next frame through
digital_wq_cmd()->nfcsim_send(). nfcsim_send() also sees dev->up ==
true and calls schedule_work(&dev->recv_work). This can happen while
this function is still waiting in
disable_delayed_work_sync(&dev->send_work).
The newly queued nfcsim_recv_wq() then sleeps in
nfcsim_link_recv_skb():
rc = wait_event_interruptible_timeout(link->recv_wait,
link->cond,
msecs_to_jiffies(timeout));
The wait condition only tests link->cond, not link->shutdown. The
previous pass already reset link->cond = 0 (outside link->lock) after
nfcsim_link_shutdown() had set it. Nothing wakes the new worker, so
disable_work_sync(&dev->recv_work) waits for the full receive timeout.
For an NFC-DEP initiator that can be up to 5049 ms from
digital_rwt_map[], which stalls module unload.
There is a second variant. If dev->up flips back to true after both
disable calls but before unregister cancels ddev->cmd_work,
nfcsim_send() stores the skb in dev->link_out and returns 0. Its
schedule_work() and schedule_delayed_work() calls silently fail on the
disabled works.
This doesn't seem to reopen the digital_send_cmd_complete()
use-after-free, because recv_work is still disabled before the drain
loop. But the teardown now depends on dev->up staying false while the
digital core can still set it back to true.
Before this patch, unregister stopped poll_work and cmd_work before
dev->up was cleared, so this window did not exist. Patch 3/3 doesn't
touch nfcsim.c, so the window remains at the end of the series.
Would it make sense to use a separate one-way teardown flag that
nfcsim_send() and nfcsim_recv_wq() check, rather than reusing dev->up?
Alternatively, the digital side could be quiesced before the link is
shut down. Should nfcsim_link_recv_skb() also wake on link->shutdown
so a late worker cannot sleep for the whole timeout?
[Severity: High]
This isn't a bug introduced by this patch, but port100_disconnect() in
drivers/nfc/port100.c seems to have the same kind of teardown ordering
problem. It never cancels dev->cmd_complete_work:
drivers/nfc/port100.c:port100_disconnect() {
...
nfc_digital_unregister_device(dev->nfc_digital_dev);
nfc_digital_free_device(dev->nfc_digital_dev);
usb_kill_urb(dev->in_urb);
usb_kill_urb(dev->out_urb);
usb_free_urb(dev->in_urb);
usb_free_urb(dev->out_urb);
kfree(dev->cmd);
...
}
On unbind, usb_unbind_interface() calls usb_disable_interface() before
->disconnect, because port100 doesn't set soft_unbind. That kills in_urb.
The completion handler, port100_recv_response() or port100_recv_ack(), then
runs with -ESHUTDOWN or -ENOENT and does:
schedule_work(&dev->cmd_complete_work);
port100_disconnect() then frees state in three places:
- the drain loop in nfc_digital_unregister_device() frees every
digital_cmd
- nfc_digital_free_device() frees ddev
- kfree(dev->cmd) frees the port100 command
If port100_wq_cmd_complete() runs after that, port100_send_async_complete()
reads dev->cmd. By then dev->cmd may already be freed, or it may end up
freed twice.
It then calls port100_in_comm_rf_complete()->digital_send_cmd_complete().
Can that write cmd->resp into a digital_cmd that the drain loop already
freed, and call schedule_work() on a freed ddev->cmd_complete_work?
struct port100 is devm-allocated. If the work is delayed past devm cleanup,
it would also touch a freed struct port100.
Should port100_disconnect() cancel or disable dev->cmd_complete_work before
freeing this state?
>
> nfc_digital_free_device(dev->nfc_digital_dev);
>
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204060656.503-1-luob87709%40gmail.com
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH net v2 3/3] nfc: digital: stop works and drain commands before nfc_remove_device()
2026-12-04 6:06 ` Bin Luo (University of Electronic Science and Technology of China)
(?)
@ 2026-12-05 6:57 ` netdev-bot+sashiko
-1 siblings, 0 replies; 11+ messages in thread
From: netdev-bot+sashiko @ 2026-12-05 6:57 UTC (permalink / raw)
To: luob87709
Cc: david, davem, edumazet, kuba, pabeni, horms, krzk, oe-linux-nfc,
netdev, linux-kernel, stable
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 0 (2) · Medium: 1 · Low: 2
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] The drain loop in nfc_digital_unregister_device()
(net/nfc/digital_core.c:849-861) frees only cmd->mdaa_params and cmd.
- [Low] The commit message names two routes to a use-after-free of
nfc_llcp_local: a cmd_complete_work that is still running, and the…
- [Low] nfc: digital: Fixes trailer cites stub commit 4b10884eb428
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the pn533
driver has the same bug this patch fixes in the digital layer: a…
- [High] This problem wasn't introduced by this patch, but
nfc_digital_unregister_device() frees the head digital_cmd even when…
--
nfc: digital: stop works and drain commands before nfc_remove_device()
This patch reorders nfc_digital_unregister_device(). It now calls
nfc_unregister_rfkill() first, permanently disables poll_work, cmd_work
and cmd_complete_work, and drains cmd_queue while LLCP is still
registered. nfc_remove_device() is called last, mirroring
nci_unregister_device().
> 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.
[Severity: Low]
Is the drain loop really a use-after-free route? The drain in
nfc_digital_unregister_device() always passes an error:
cmd->cmd_cb(ddev, cmd->cb_context, ERR_PTR(-ENODEV));
digital_in_recv_dep_res() turns that into rc = -ENODEV and jumps to the
exit label. That calls the callback with a NULL resp:
exit:
data_exch->cb(data_exch->cb_context, resp, rc);
nfc_llcp_recv() then returns before it reaches __nfc_llcp_recv(local, ...):
if (err < 0) {
pr_err("LLCP PDU receive err %d\n", err);
return;
}
So on the drain path the stale local pointer is passed but never
dereferenced. The route that can actually dereference local is a
cmd_complete_work that is still running and delivers a valid resp.
Could the commit message name the in-flight cmd_complete_work as the
real trigger, and describe moving the drain earlier as defensive? That
would help stable backporters judge the fix.
> diff --git a/net/nfc/digital_core.c b/net/nfc/digital_core.c
> index 18236221d8988..7b8d72f917811 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);
[Severity: Low]
Is 4b10884eb428 the right commit for the first Fixes tag?
As far as I can tell, 4b10884eb428 only added the skeleton of the
digital stack. All of the digital_nfc_ops returned -EOPNOTSUPP, and
nfc_digital_unregister_device() was just a call to
nfc_unregister_device(ddev->nfc_dev). At that point there was no
poll_work, cmd_work, cmd_complete_work or ddev->cmd_queue, so the
teardown could not run in the wrong order.
Those pieces arrived with 59ee2361c924 ("NFC Digital: Implement driver
commands mechanism"). That commit added the work items and
ddev->cmd_queue. It also made digital_wq_cmd_complete() re-schedule
cmd_work. Its unregister sequence calls nfc_unregister_device() before
cancelling the works and draining the queue, which is the ordering this
patch changes.
The second tag, 82e57952869f, looks correct to me. It is the commit that
added the cmd->cmd_cb() call in the drain loop.
Stable and bisect tooling rely on these tags to decide which trees need
the fix. Could the first one be replaced with:
Fixes: 59ee2361c924 ("NFC Digital: Implement driver commands mechanism")
[Severity: Medium]
Can this ordering leak the req skb of a chained command?
cmd_work is disabled before cmd_complete_work. So
disable_work_sync(&ddev->cmd_complete_work) waits for a handler that may
still be running. That handler's cmd_cb can chain the next command, for
example digital_in_recv_sens_res() sending SDD_REQ, or a DEP ACK, RTOX
or next fragment. digital_send_cmd() then queues it:
cmd->req = skb;
...
list_add_tail(&cmd->queue, &ddev->cmd_queue);
mutex_unlock(&ddev->cmd_lock);
schedule_work(&ddev->cmd_work);
With cmd_work disabled, schedule_work() is now a no-op. The command stays
non-pending and ends up in the drain loop below, which only does:
kfree(cmd->mdaa_params);
kfree(cmd);
The drain never calls kfree_skb(cmd->req). The error path in
digital_wq_cmd() shows that the cmd owns req until the driver accepts
it:
kfree_skb(cmd->req);
kfree(cmd->mdaa_params);
kfree(cmd);
cmd->resp has the same problem. A driver completion can arrive after
cmd_complete_work has been disabled, or a pending cmd_complete_work can
be cancelled. In both cases cmd->resp holds a valid skb, but the drain
passes ERR_PTR(-ENODEV) to cmd_cb and nothing frees cmd->resp.
The missing kfree_skb() in the drain dates back to 82e57952869f. With
disable_work_sync(), though, these commands now always end up there.
Should the drain call kfree_skb(cmd->req) when !cmd->pending, and
kfree_skb(cmd->resp) when it holds a valid skb?
>
> 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);
> }
[Severity: High]
This isn't a bug introduced by this patch, but can a late driver
completion write into a digital_cmd that is freed here?
The head command may already have been handed to the driver
(cmd->pending == 1). Nothing aborts the driver's asynchronous completion
or waits for it, because the disable_work_sync() calls only wait for
digital's own work items. After the drain frees cmd, the driver can
still call:
net/nfc/digital_core.c:digital_send_cmd_complete() {
struct digital_cmd *cmd = arg;
cmd->resp = resp;
schedule_work(&ddev->cmd_complete_work);
}
That writes into the freed cmd. The schedule_work() may also act on a
ddev that the caller has already freed with nfc_digital_free_device().
port100 looks like it can reach this. port100 does not set soft_unbind,
so usb_unbind_interface() kills the URBs before ->disconnect:
if (!driver->soft_unbind || udev->state == USB_STATE_NOTATTACHED)
usb_disable_interface(udev, intf, false);
driver->disconnect(intf);
port100_recv_response() still queues the completion on system_wq for
killed URBs:
sched_wq:
schedule_work(&dev->cmd_complete_work);
port100_disconnect() never cancels that work:
nfc_digital_unregister_device(dev->nfc_digital_dev);
nfc_digital_free_device(dev->nfc_digital_dev);
usb_kill_urb(dev->in_urb);
So this chain can run after the drain has freed cmd:
port100_wq_cmd_complete()
port100_send_async_complete()
port100_in_comm_rf_complete()
digital_send_cmd_complete()
The same window existed with cancel_work_sync(). This patch narrows part
of it by preventing cmd_complete_work from being re-queued. As noted
above, a valid resp delivered that way is now leaked instead.
nci_unregister_device(), which the commit message says this mirrors,
stops the device before cleanup. There is no equivalent step here.
Would it make sense to abort the pending command through
ddev->ops->abort_cmd and wait for the driver? Another option would be to
require drivers to quiesce their I/O before calling
nfc_digital_unregister_device().
> +
> + nfc_remove_device(ddev->nfc_dev);
> }
> EXPORT_SYMBOL(nfc_digital_unregister_device);
[Severity: High]
This isn't a bug introduced by this patch, but does pn533 have the same
LLCP teardown problem that this patch fixes in the digital layer?
pn533_usb_disconnect() unregisters before it drains the driver
workqueue:
pn53x_unregister_nfc(phy->priv);
pn53x_common_clean(phy->priv);
pn53x_unregister_nfc() calls nfc_unregister_device(). That goes through
nfc_remove_device()->nfc_llcp_unregister_device(), which calls
local_cleanup() and nfc_llcp_local_put(). When the last reference is
dropped, local_release_work() kfree()s local. Only after that does
pn53x_common_clean() run:
destroy_workqueue(priv->wq);
LLCP sends via nfc_data_exchange(..., nfc_llcp_recv, local) without
taking a reference on local. Suppose a successful data exchange response
is already queued on priv->wq when unbind starts. Then
pn533_data_exchange_complete() calls:
arg->cb(arg->cb_context, skb, 0);
That is nfc_llcp_recv(local, skb, 0)->__nfc_llcp_recv(), which does:
local->rx_pending = skb;
timer_delete(&local->link_timer);
schedule_work(&local->rx_work);
Can this write into a freed nfc_llcp_local? Killed URBs complete with an
error, and nfc_llcp_recv() ignores errors, so only a successful
completion queued before unbind would hit this.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204060656.503-1-luob87709%40gmail.com
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2026-12-05 6:58 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 6:06 [PATCH net v2 0/3] nfc: fix LLCP, nfcsim, and digital device teardown ordering Bin Luo (University of Electronic Science and Technology of China)
2026-12-04 6:06 ` 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 (University of Electronic Science and Technology of China)
2026-12-05 6:57 ` netdev-bot+sashiko
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 (University of Electronic Science and Technology of China)
2026-12-05 6:57 ` netdev-bot+sashiko
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
2026-12-04 6:06 ` Bin Luo (University of Electronic Science and Technology of China)
2026-12-05 6:57 ` netdev-bot+sashiko
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.