diff mbox series

[v0] ieee802154/adf7242: defer destroy_workqueue call

Message ID 20220808034224.12642-1-linma@zju.edu.cn (mailing list archive)
State Awaiting Upstream
Delegated to: Netdev Maintainers
Headers show
Series [v0] ieee802154/adf7242: defer destroy_workqueue call | expand

Checks

Context Check Description
netdev/tree_selection success Guessed tree name to be net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers fail 1 blamed authors not CCed: alexandru.ardelean@analog.com; 1 maintainers not CCed: alexandru.ardelean@analog.com
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes fail Problems with Fixes tag: 2
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 12 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Lin Ma Aug. 8, 2022, 3:42 a.m. UTC
There is a possible race condition (use-after-free) like below

  (FREE)                     |  (USE)
  adf7242_remove             |  adf7242_channel
   cancel_delayed_work_sync  |
    destroy_workqueue (1)    |   adf7242_cmd_rx
                             |    mod_delayed_work (2)
                             |

The root cause for this race is that the upper layer (ieee802154) is
unaware of this detaching event and the function adf7242_channel can
be called without any checks.

To fix this, we can add a flag write at the beginning of adf7242_remove
and add flag check in adf7242_channel. Or we can just defer the
destructive operation like other commit 3e0588c291d6 ("hamradio: defer
ax25 kfree after unregister_netdev") which let the
ieee802154_unregister_hw() to handle the synchronization. This patch
takes the second option.

Fixes: 58e9683d1475 ("net: ieee802154: adf7242: Fix OCL calibration
runs")
Signed-off-by: Lin Ma <linma@zju.edu.cn>
---
 drivers/net/ieee802154/adf7242.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Comments

Hennerich, Michael Aug. 8, 2022, 7:43 a.m. UTC | #1
> -----Original Message-----
> From: Lin Ma <linma@zju.edu.cn>
> Sent: Montag, 8. August 2022 05:42
> To: Hennerich, Michael <Michael.Hennerich@analog.com>;
> alex.aring@gmail.com; stefan@datenfreihafen.org; davem@davemloft.net;
> edumazet@google.com; kuba@kernel.org; pabeni@redhat.com; linux-
> wpan@vger.kernel.org; netdev@vger.kernel.org; linux-
> kernel@vger.kernel.org
> Cc: Lin Ma <linma@zju.edu.cn>
> Subject: [PATCH v0] ieee802154/adf7242: defer destroy_workqueue call
> 
> 
> There is a possible race condition (use-after-free) like below
> 
>   (FREE)                     |  (USE)
>   adf7242_remove             |  adf7242_channel
>    cancel_delayed_work_sync  |
>     destroy_workqueue (1)    |   adf7242_cmd_rx
>                              |    mod_delayed_work (2)
>                              |
> 
> The root cause for this race is that the upper layer (ieee802154) is unaware of
> this detaching event and the function adf7242_channel can be called without
> any checks.
> 
> To fix this, we can add a flag write at the beginning of adf7242_remove and
> add flag check in adf7242_channel. Or we can just defer the destructive
> operation like other commit 3e0588c291d6 ("hamradio: defer
> ax25 kfree after unregister_netdev") which let the
> ieee802154_unregister_hw() to handle the synchronization. This patch takes
> the second option.
> 
> Fixes: 58e9683d1475 ("net: ieee802154: adf7242: Fix OCL calibration
> runs")
> Signed-off-by: Lin Ma <linma@zju.edu.cn>

Acked-by: Michael Hennerich <michael.hennerich@analog.com>

> ---
>  drivers/net/ieee802154/adf7242.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/net/ieee802154/adf7242.c
> b/drivers/net/ieee802154/adf7242.c
> index 6afdf1622944..efcc45aef508 100644
> --- a/drivers/net/ieee802154/adf7242.c
> +++ b/drivers/net/ieee802154/adf7242.c
> @@ -1310,10 +1310,11 @@ static void adf7242_remove(struct spi_device
> *spi)
> 
>  	debugfs_remove_recursive(lp->debugfs_root);
> 
> +	ieee802154_unregister_hw(lp->hw);
> +
>  	cancel_delayed_work_sync(&lp->work);
>  	destroy_workqueue(lp->wqueue);
> -
> -	ieee802154_unregister_hw(lp->hw);
> +
>  	mutex_destroy(&lp->bmux);
>  	ieee802154_free_hw(lp->hw);
>  }
> --
> 2.36.1
Stefan Schmidt Aug. 23, 2022, 8:03 a.m. UTC | #2
Hello.

On 08.08.22 05:42, Lin Ma wrote:
> There is a possible race condition (use-after-free) like below
> 
>    (FREE)                     |  (USE)
>    adf7242_remove             |  adf7242_channel
>     cancel_delayed_work_sync  |
>      destroy_workqueue (1)    |   adf7242_cmd_rx
>                               |    mod_delayed_work (2)
>                               |
> 
> The root cause for this race is that the upper layer (ieee802154) is
> unaware of this detaching event and the function adf7242_channel can
> be called without any checks.
> 
> To fix this, we can add a flag write at the beginning of adf7242_remove
> and add flag check in adf7242_channel. Or we can just defer the
> destructive operation like other commit 3e0588c291d6 ("hamradio: defer
> ax25 kfree after unregister_netdev") which let the
> ieee802154_unregister_hw() to handle the synchronization. This patch
> takes the second option.
> 
> Fixes: 58e9683d1475 ("net: ieee802154: adf7242: Fix OCL calibration
> runs")
> Signed-off-by: Lin Ma <linma@zju.edu.cn>


This patch has been applied to the wpan tree and will be
part of the next pull request to net. Thanks!

regards
Stefan Schmidt
diff mbox series

Patch

diff --git a/drivers/net/ieee802154/adf7242.c b/drivers/net/ieee802154/adf7242.c
index 6afdf1622944..efcc45aef508 100644
--- a/drivers/net/ieee802154/adf7242.c
+++ b/drivers/net/ieee802154/adf7242.c
@@ -1310,10 +1310,11 @@  static void adf7242_remove(struct spi_device *spi)
 
 	debugfs_remove_recursive(lp->debugfs_root);
 
+	ieee802154_unregister_hw(lp->hw);
+
 	cancel_delayed_work_sync(&lp->work);
 	destroy_workqueue(lp->wqueue);
-
-	ieee802154_unregister_hw(lp->hw);
+
 	mutex_destroy(&lp->bmux);
 	ieee802154_free_hw(lp->hw);
 }