netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v0] nfc: pn533: Fix buggy cleanup order
@ 2022-05-18 10:53 Lin Ma
  2022-05-18 13:00 ` patchwork-bot+netdevbpf
  0 siblings, 1 reply; 2+ messages in thread
From: Lin Ma @ 2022-05-18 10:53 UTC (permalink / raw)
  To: krzysztof.kozlowski, dan.carpenter, cyeaa, rikard.falkeborn,
	netdev, linux-kernel
  Cc: Lin Ma

When removing the pn533 device (i2c or USB), there is a logic error. The
original code first cancels the worker (flush_delayed_work) and then
destroys the workqueue (destroy_workqueue), leaving the timer the last
one to be deleted (del_timer). This result in a possible race condition
in a multi-core preempt-able kernel. That is, if the cleanup
(pn53x_common_clean) is concurrently run with the timer handler
(pn533_listen_mode_timer), the timer can queue the poll_work to the
already destroyed workqueue, causing use-after-free.

This patch reorder the cleanup: it uses the del_timer_sync to make sure
the handler is finished before the routine will destroy the workqueue.
Note that the timer cannot be activated by the worker again.

static void pn533_wq_poll(struct work_struct *work)
...
 rc = pn533_send_poll_frame(dev);
 if (rc)
   return;

 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
   mod_timer(&dev->listen_timer, ...);

That is, the mod_timer can be called only when pn533_send_poll_frame()
returns no error, which is impossible because the device is detaching
and the lower driver should return ENODEV code.

Signed-off-by: Lin Ma <linma@zju.edu.cn>
---
 drivers/nfc/pn533/pn533.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/nfc/pn533/pn533.c b/drivers/nfc/pn533/pn533.c
index a491db46e3bd..a0532647b040 100644
--- a/drivers/nfc/pn533/pn533.c
+++ b/drivers/nfc/pn533/pn533.c
@@ -2787,13 +2787,14 @@ void pn53x_common_clean(struct pn533 *priv)
 {
 	struct pn533_cmd *cmd, *n;
 
+	/* delete the timer before cleanup the worker */
+	del_timer_sync(&priv->listen_timer);
+
 	flush_delayed_work(&priv->poll_work);
 	destroy_workqueue(priv->wq);
 
 	skb_queue_purge(&priv->resp_q);
 
-	del_timer(&priv->listen_timer);
-
 	list_for_each_entry_safe(cmd, n, &priv->cmd_queue, queue) {
 		list_del(&cmd->queue);
 		kfree(cmd);
-- 
2.35.1


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

* Re: [PATCH v0] nfc: pn533: Fix buggy cleanup order
  2022-05-18 10:53 [PATCH v0] nfc: pn533: Fix buggy cleanup order Lin Ma
@ 2022-05-18 13:00 ` patchwork-bot+netdevbpf
  0 siblings, 0 replies; 2+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-05-18 13:00 UTC (permalink / raw)
  To: Lin Ma
  Cc: krzysztof.kozlowski, dan.carpenter, cyeaa, rikard.falkeborn,
	netdev, linux-kernel

Hello:

This patch was applied to netdev/net.git (master)
by David S. Miller <davem@davemloft.net>:

On Wed, 18 May 2022 18:53:21 +0800 you wrote:
> When removing the pn533 device (i2c or USB), there is a logic error. The
> original code first cancels the worker (flush_delayed_work) and then
> destroys the workqueue (destroy_workqueue), leaving the timer the last
> one to be deleted (del_timer). This result in a possible race condition
> in a multi-core preempt-able kernel. That is, if the cleanup
> (pn53x_common_clean) is concurrently run with the timer handler
> (pn533_listen_mode_timer), the timer can queue the poll_work to the
> already destroyed workqueue, causing use-after-free.
> 
> [...]

Here is the summary with links:
  - [v0] nfc: pn533: Fix buggy cleanup order
    https://git.kernel.org/netdev/net/c/b8cedb7093b2

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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

end of thread, other threads:[~2022-05-18 13:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-18 10:53 [PATCH v0] nfc: pn533: Fix buggy cleanup order Lin Ma
2022-05-18 13:00 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).