linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] net: alx: fix race condition in alx_remove
@ 2020-06-14 16:59 Zekun Shen
  2020-06-15 15:50 ` [PATCH v2] " Zekun Shen
  0 siblings, 1 reply; 3+ messages in thread
From: Zekun Shen @ 2020-06-14 16:59 UTC (permalink / raw)
  Cc: Zekun Shen, Jay Cliburn, Chris Snook, David S. Miller,
	Jakub Kicinski, netdev, linux-kernel

There is a race condition exist during termination. The path is
alx_stop and then alx_remove. An alx_schedule_link_check could be called
before alx_stop and invoke alx_link_check later. Alx_stop frees the
napis, and alx_remove cancels any pending works. If any of the work is
scheduled before termination and invoked before alx_remove, a
null-ptr-deref occurs because both expect alx->napis[i].

This patch fix the race condition by moving cancel_work_sync functions
before alx_free_napis inside alx_stop.

Signed-off-by: Zekun Shen <bruceshenzk@gmail.com>
---
 drivers/net/ethernet/atheros/alx/main.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/net/ethernet/atheros/alx/main.c b/drivers/net/ethernet/atheros/alx/main.c
index b9b4edb91..1487d6765 100644
--- a/drivers/net/ethernet/atheros/alx/main.c
+++ b/drivers/net/ethernet/atheros/alx/main.c
@@ -1249,6 +1249,9 @@ static int __alx_open(struct alx_priv *alx, bool resume)
 
 static void __alx_stop(struct alx_priv *alx)
 {
+	cancel_work_sync(&alx->link_check_wk);
+	cancel_work_sync(&alx->reset_wk);
+
 	alx_halt(alx);
 	alx_free_irq(alx);
 	alx_free_rings(alx);
@@ -1855,9 +1858,6 @@ static void alx_remove(struct pci_dev *pdev)
 	struct alx_priv *alx = pci_get_drvdata(pdev);
 	struct alx_hw *hw = &alx->hw;
 
-	cancel_work_sync(&alx->link_check_wk);
-	cancel_work_sync(&alx->reset_wk);
-
 	/* restore permanent mac address */
 	alx_set_macaddr(hw, hw->perm_addr);
 
-- 
2.17.1


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

* [PATCH v2] net: alx: fix race condition in alx_remove
  2020-06-14 16:59 [PATCH] net: alx: fix race condition in alx_remove Zekun Shen
@ 2020-06-15 15:50 ` Zekun Shen
  2020-06-15 20:20   ` David Miller
  0 siblings, 1 reply; 3+ messages in thread
From: Zekun Shen @ 2020-06-15 15:50 UTC (permalink / raw)
  Cc: Zekun Shen, Jay Cliburn, Chris Snook, David S. Miller,
	Jakub Kicinski, netdev, linux-kernel

There is a race condition exist during termination. The path is
alx_stop and then alx_remove. An alx_schedule_link_check could be called
before alx_stop by interrupt handler and invoke alx_link_check later.
Alx_stop frees the napis, and alx_remove cancels any pending works.
If any of the work is scheduled before termination and invoked before
alx_remove, a null-ptr-deref occurs because both expect alx->napis[i].

This patch fix the race condition by moving cancel_work_sync functions
before alx_free_napis inside alx_stop. Because interrupt handler can call
alx_schedule_link_check again, alx_free_irq is moved before
cancel_work_sync calls too.

Signed-off-by: Zekun Shen <bruceshenzk@gmail.com>
---
Changes in v2:
- move alx_free_irq before cancel_work_sync calls
- prevent another interrupt to cause race condition

 drivers/net/ethernet/atheros/alx/main.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/net/ethernet/atheros/alx/main.c b/drivers/net/ethernet/atheros/alx/main.c
index b9b4edb91..9b7f1af5f 100644
--- a/drivers/net/ethernet/atheros/alx/main.c
+++ b/drivers/net/ethernet/atheros/alx/main.c
@@ -1249,8 +1249,12 @@ static int __alx_open(struct alx_priv *alx, bool resume)
 
 static void __alx_stop(struct alx_priv *alx)
 {
-	alx_halt(alx);
 	alx_free_irq(alx);
+
+	cancel_work_sync(&alx->link_check_wk);
+	cancel_work_sync(&alx->reset_wk);
+
+	alx_halt(alx);
 	alx_free_rings(alx);
 	alx_free_napis(alx);
 }
@@ -1855,9 +1859,6 @@ static void alx_remove(struct pci_dev *pdev)
 	struct alx_priv *alx = pci_get_drvdata(pdev);
 	struct alx_hw *hw = &alx->hw;
 
-	cancel_work_sync(&alx->link_check_wk);
-	cancel_work_sync(&alx->reset_wk);
-
 	/* restore permanent mac address */
 	alx_set_macaddr(hw, hw->perm_addr);
 
-- 
2.17.1


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

* Re: [PATCH v2] net: alx: fix race condition in alx_remove
  2020-06-15 15:50 ` [PATCH v2] " Zekun Shen
@ 2020-06-15 20:20   ` David Miller
  0 siblings, 0 replies; 3+ messages in thread
From: David Miller @ 2020-06-15 20:20 UTC (permalink / raw)
  To: bruceshenzk; +Cc: jcliburn, chris.snook, kuba, netdev, linux-kernel

From: Zekun Shen <bruceshenzk@gmail.com>
Date: Mon, 15 Jun 2020 11:50:29 -0400

> There is a race condition exist during termination. The path is
> alx_stop and then alx_remove. An alx_schedule_link_check could be called
> before alx_stop by interrupt handler and invoke alx_link_check later.
> Alx_stop frees the napis, and alx_remove cancels any pending works.
> If any of the work is scheduled before termination and invoked before
> alx_remove, a null-ptr-deref occurs because both expect alx->napis[i].
> 
> This patch fix the race condition by moving cancel_work_sync functions
> before alx_free_napis inside alx_stop. Because interrupt handler can call
> alx_schedule_link_check again, alx_free_irq is moved before
> cancel_work_sync calls too.
> 
> Signed-off-by: Zekun Shen <bruceshenzk@gmail.com>

Applied, thank you.

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

end of thread, other threads:[~2020-06-15 20:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-14 16:59 [PATCH] net: alx: fix race condition in alx_remove Zekun Shen
2020-06-15 15:50 ` [PATCH v2] " Zekun Shen
2020-06-15 20:20   ` David Miller

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).