Skip to contentSkip to conversation

nfc: llcp: use disable_work_sync and timer_shutdown_sync in local_cleanup

PATCH In review Tristan Madani 3 participants 4 messages Oct 29 – Oct 31

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 nfc: llcp: use disable_work_sync and timer_shutdown_sync in local_cleanup

From: Tristan Madani <[email protected]>

local_cleanup() tears down timers and work items before local_release_work() frees the nfc_llcp_local structure. The current code has two ordering problems:

1) timer_delete_sync() does not prevent timer callbacks from re-arming the timer via mod_timer(). nfc_llcp_tx_work() unconditionally re-arms link_timer, and nfc_llcp_sdreq_timeout_work() conditionally re-arms sdreq_timer. A work callback that runs during cancel_work_sync() and calls mod_timer() causes the timer to fire after the structure is freed.

2) cancel_work_sync() does not prevent work items from being re-queued. nfc_llcp_rx_work() queues tx_work via schedule_work(), and the timer callbacks queue their associated work items. Since tx_work is cancelled before rx_work, a running rx_work instance can re-queue tx_work after its cancel_work_sync() has returned. The re-queued tx_work then runs on the freed structure.

Switch the timers to timer_shutdown_sync() which rejects subsequent mod_timer() calls. Switch the work items to disable_work_sync() which rejects subsequent schedule_work() calls.

Reorder the teardown sequence so that both timers are shut down first (their callbacks queue work items), rx_work is disabled before tx_work (rx_work queues tx_work), and data structures are freed only after all asynchronous callbacks are quiesced.

Fixes d646960f7986 ("NFC: Initial LLCP support")

---
v2:
- Switch work items from cancel_work_sync() to disable_work_sync() to prevent re-queuing after cancellation, as identified by the automated review and confirmed by Simon Horman
- Reorder teardown: shut down both timers first (their callbacks queue work items), then disable rx_work before tx_work (rx_work queues tx_work), then disable remaining work items
- Move data structure cleanup (skb_queue_purge, kfree_skb) after all asynchronous callbacks are quiesced

 net/nfc/llcp_core.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
net/nfc/llcp_core.c +6 -6
index 74bf817007cf2..984e7088288f0 100644
@@ -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);
+	timer_shutdown_sync(&local->link_timer);
+	timer_shutdown_sync(&local->sdreq_timer);
+	disable_work_sync(&local->rx_work);
+	disable_work_sync(&local->tx_work);
+	disable_work_sync(&local->timeout_work);
+	disable_work_sync(&local->sdreq_timeout_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);
 	kfree_skb(local->rx_pending);
 	local->rx_pending = NULL;
-	timer_delete_sync(&local->sdreq_timer);
-	cancel_work_sync(&local->sdreq_timeout_work);
 	nfc_llcp_free_sdp_tlv_list(&local->pending_sdreqs);
 }
 
Signature · 1 line
2.47.3
#1 lore raw
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

Hi!

This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.

- Whether the issue was actually triggered, or is only theoretical (e.g. found by code inspection). If it was triggered please include the symptoms, like the stack trace or error messages.

Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then.

The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.

#2 lore raw

The issue was found by manual code inspection of the NFC LLCP teardown path in local_cleanup(). cancel_work_sync() does not prevent the work from being re-queued, and timer_delete_sync() does not prevent the timer from being re-armed -- both can race with an incoming LLCP frame that schedules them again after cancellation.

The issue is theoretical (found by code review, not triggered), but the race window is real: any LLCP frame arriving between the cancel/delete calls and kfree(local) can re-queue the work or re-arm the timer, leading to the callback running after the local structure is freed.

reviewer Reviewed-by on v2 1/1
#3 lore raw
In reply to Tristan Madani
Quoted 33 lines · Tristan Madani

From: Tristan Madani <[email protected]>

local_cleanup() tears down timers and work items before local_release_work() frees the nfc_llcp_local structure. The current code has two ordering problems:

1) timer_delete_sync() does not prevent timer callbacks from re-arming the timer via mod_timer(). nfc_llcp_tx_work() unconditionally re-arms link_timer, and nfc_llcp_sdreq_timeout_work() conditionally re-arms sdreq_timer. A work callback that runs during cancel_work_sync() and calls mod_timer() causes the timer to fire after the structure is freed.

2) cancel_work_sync() does not prevent work items from being re-queued. nfc_llcp_rx_work() queues tx_work via schedule_work(), and the timer callbacks queue their associated work items. Since tx_work is cancelled before rx_work, a running rx_work instance can re-queue tx_work after its cancel_work_sync() has returned. The re-queued tx_work then runs on the freed structure.

Switch the timers to timer_shutdown_sync() which rejects subsequent mod_timer() calls. Switch the work items to disable_work_sync() which rejects subsequent schedule_work() calls.

Reorder the teardown sequence so that both timers are shut down first (their callbacks queue work items), rx_work is disabled before tx_work (rx_work queues tx_work), and data structures are freed only after all asynchronous callbacks are quiesced.

Fixes d646960f7986 ("NFC: Initial LLCP support")
Quoted 10 lines

---
v2:
- Switch work items from cancel_work_sync() to disable_work_sync() to prevent re-queuing after cancellation, as identified by the automated review and confirmed by Simon Horman
- Reorder teardown: shut down both timers first (their callbacks queue work items), then disable rx_work before tx_work (rx_work queues tx_work), then disable remaining work items
- Move data structure cleanup (skb_queue_purge, kfree_skb) after all asynchronous callbacks are quiesced

Thanks for the updates.