linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH V2] drivers: nfc: nfcmrvl: fix double free bug in nfcmrvl_nci_unregister_dev()
@ 2022-04-10 13:52 Duoming Zhou
  2022-04-11  6:51 ` Krzysztof Kozlowski
  2022-04-12  7:28 ` Paolo Abeni
  0 siblings, 2 replies; 4+ messages in thread
From: Duoming Zhou @ 2022-04-10 13:52 UTC (permalink / raw)
  To: krzk, linux-kernel
  Cc: netdev, akpm, davem, gregkh, alexander.deucher, broonie, Duoming Zhou

There is a potential double bug in nfcmrvl usb driver between
unregister and resume operation.

The race that cause that double free bug can be shown as below:

   (FREE)                   |      (USE)
                            | nfcmrvl_resume
                            |  nfcmrvl_submit_bulk_urb
                            |   nfcmrvl_bulk_complete
                            |    nfcmrvl_nci_recv_frame
                            |     nfcmrvl_fw_dnld_recv_frame
                            |      queue_work
                            |       fw_dnld_rx_work
                            |        fw_dnld_over
                            |         release_firmware
                            |          kfree(fw); //(1)
nfcmrvl_disconnect          |
 nfcmrvl_nci_unregister_dev |
  nfcmrvl_fw_dnld_abort     |
   fw_dnld_over             |         ...
    if (priv->fw_dnld.fw)   |
    release_firmware        |
     kfree(fw); //(2)       |
     ...                    |         priv->fw_dnld.fw = NULL;

When nfcmrvl usb driver is resuming, we detach the device.
The release_firmware() will deallocate firmware in position (1),
but firmware will be deallocated again in position (2), which
leads to double free.

This patch reorders nfcmrvl_fw_dnld_deinit() before nfcmrvl_fw_dnld_abort()
in order to prevent double free bug. Because destroy_workqueue() will
not return until all work items are finished. The priv->fw_dnld.fw will
be set to NULL after work items are finished and fw_dnld_over() called by
nfcmrvl_nci_unregister_dev() will check whether priv->fw_dnld.fw is NULL.
So the double free bug could be prevented.

Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
---
Changes in V2:
  - Make commit message more clearer.

 drivers/nfc/nfcmrvl/main.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/nfc/nfcmrvl/main.c b/drivers/nfc/nfcmrvl/main.c
index 2fcf545012b..d8342271f50 100644
--- a/drivers/nfc/nfcmrvl/main.c
+++ b/drivers/nfc/nfcmrvl/main.c
@@ -183,11 +183,10 @@ void nfcmrvl_nci_unregister_dev(struct nfcmrvl_private *priv)
 {
 	struct nci_dev *ndev = priv->ndev;
 
+	nfcmrvl_fw_dnld_deinit(priv);
 	if (priv->ndev->nfc_dev->fw_download_in_progress)
 		nfcmrvl_fw_dnld_abort(priv);
 
-	nfcmrvl_fw_dnld_deinit(priv);
-
 	if (gpio_is_valid(priv->config.reset_n_io))
 		gpio_free(priv->config.reset_n_io);
 
-- 
2.17.1


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

* Re: [PATCH V2] drivers: nfc: nfcmrvl: fix double free bug in nfcmrvl_nci_unregister_dev()
  2022-04-10 13:52 [PATCH V2] drivers: nfc: nfcmrvl: fix double free bug in nfcmrvl_nci_unregister_dev() Duoming Zhou
@ 2022-04-11  6:51 ` Krzysztof Kozlowski
  2022-04-12  7:28 ` Paolo Abeni
  1 sibling, 0 replies; 4+ messages in thread
From: Krzysztof Kozlowski @ 2022-04-11  6:51 UTC (permalink / raw)
  To: Duoming Zhou, linux-kernel
  Cc: netdev, akpm, davem, gregkh, alexander.deucher, broonie

On 10/04/2022 15:52, Duoming Zhou wrote:
> There is a potential double bug in nfcmrvl usb driver between
> unregister and resume operation.
> 
> The race that cause that double free bug can be shown as below:
> 
>    (FREE)                   |      (USE)
>                             | nfcmrvl_resume
>                             |  nfcmrvl_submit_bulk_urb
>                             |   nfcmrvl_bulk_complete
>                             |    nfcmrvl_nci_recv_frame
>                             |     nfcmrvl_fw_dnld_recv_frame
>                             |      queue_work
>                             |       fw_dnld_rx_work
>                             |        fw_dnld_over
>                             |         release_firmware
>                             |          kfree(fw); //(1)
> nfcmrvl_disconnect          |
>  nfcmrvl_nci_unregister_dev |
>   nfcmrvl_fw_dnld_abort     |
>    fw_dnld_over             |         ...
>     if (priv->fw_dnld.fw)   |
>     release_firmware        |
>      kfree(fw); //(2)       |
>      ...                    |         priv->fw_dnld.fw = NULL;
> 


Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>


Best regards,
Krzysztof

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

* Re: [PATCH V2] drivers: nfc: nfcmrvl: fix double free bug in nfcmrvl_nci_unregister_dev()
  2022-04-10 13:52 [PATCH V2] drivers: nfc: nfcmrvl: fix double free bug in nfcmrvl_nci_unregister_dev() Duoming Zhou
  2022-04-11  6:51 ` Krzysztof Kozlowski
@ 2022-04-12  7:28 ` Paolo Abeni
  2022-04-12 10:59   ` duoming
  1 sibling, 1 reply; 4+ messages in thread
From: Paolo Abeni @ 2022-04-12  7:28 UTC (permalink / raw)
  To: Duoming Zhou, krzk, linux-kernel
  Cc: netdev, akpm, davem, gregkh, alexander.deucher, broonie

Hello,

On Sun, 2022-04-10 at 21:52 +0800, Duoming Zhou wrote:
> There is a potential double bug in nfcmrvl usb driver between
> unregister and resume operation.
> 
> The race that cause that double free bug can be shown as below:
> 
>    (FREE)                   |      (USE)
>                             | nfcmrvl_resume
>                             |  nfcmrvl_submit_bulk_urb
>                             |   nfcmrvl_bulk_complete
>                             |    nfcmrvl_nci_recv_frame
>                             |     nfcmrvl_fw_dnld_recv_frame
>                             |      queue_work
>                             |       fw_dnld_rx_work
>                             |        fw_dnld_over
>                             |         release_firmware
>                             |          kfree(fw); //(1)
> nfcmrvl_disconnect          |
>  nfcmrvl_nci_unregister_dev |
>   nfcmrvl_fw_dnld_abort     |
>    fw_dnld_over             |         ...
>     if (priv->fw_dnld.fw)   |
>     release_firmware        |
>      kfree(fw); //(2)       |
>      ...                    |         priv->fw_dnld.fw = NULL;
> 
> When nfcmrvl usb driver is resuming, we detach the device.
> The release_firmware() will deallocate firmware in position (1),
> but firmware will be deallocated again in position (2), which
> leads to double free.
> 
> This patch reorders nfcmrvl_fw_dnld_deinit() before nfcmrvl_fw_dnld_abort()
> in order to prevent double free bug. Because destroy_workqueue() will
> not return until all work items are finished. The priv->fw_dnld.fw will
> be set to NULL after work items are finished and fw_dnld_over() called by
> nfcmrvl_nci_unregister_dev() will check whether priv->fw_dnld.fw is NULL.
> So the double free bug could be prevented.
> 
> Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>

This looks like a -net candidates, could you please add a suitable
fixes tag?

Thanks!

Paolo


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

* Re: Re: [PATCH V2] drivers: nfc: nfcmrvl: fix double free bug in nfcmrvl_nci_unregister_dev()
  2022-04-12  7:28 ` Paolo Abeni
@ 2022-04-12 10:59   ` duoming
  0 siblings, 0 replies; 4+ messages in thread
From: duoming @ 2022-04-12 10:59 UTC (permalink / raw)
  To: Paolo Abeni
  Cc: krzk, linux-kernel, netdev, akpm, davem, gregkh,
	alexander.deucher, broonie

Hello,
On Tue, 12 Apr 2022 00:28:16 -0700 Paolo Abeni wrote:

> > There is a potential double bug in nfcmrvl usb driver between
> > unregister and resume operation.
> > 
> > The race that cause that double free bug can be shown as below:
> > 
> >    (FREE)                   |      (USE)
> >                             | nfcmrvl_resume
> >                             |  nfcmrvl_submit_bulk_urb
> >                             |   nfcmrvl_bulk_complete
> >                             |    nfcmrvl_nci_recv_frame
> >                             |     nfcmrvl_fw_dnld_recv_frame
> >                             |      queue_work
> >                             |       fw_dnld_rx_work
> >                             |        fw_dnld_over
> >                             |         release_firmware
> >                             |          kfree(fw); //(1)
> > nfcmrvl_disconnect          |
> >  nfcmrvl_nci_unregister_dev |
> >   nfcmrvl_fw_dnld_abort     |
> >    fw_dnld_over             |         ...
> >     if (priv->fw_dnld.fw)   |
> >     release_firmware        |
> >      kfree(fw); //(2)       |
> >      ...                    |         priv->fw_dnld.fw = NULL;
> > 
> > When nfcmrvl usb driver is resuming, we detach the device.
> > The release_firmware() will deallocate firmware in position (1),
> > but firmware will be deallocated again in position (2), which
> > leads to double free.
> > 
> > This patch reorders nfcmrvl_fw_dnld_deinit() before nfcmrvl_fw_dnld_abort()
> > in order to prevent double free bug. Because destroy_workqueue() will
> > not return until all work items are finished. The priv->fw_dnld.fw will
> > be set to NULL after work items are finished and fw_dnld_over() called by
> > nfcmrvl_nci_unregister_dev() will check whether priv->fw_dnld.fw is NULL.
> > So the double free bug could be prevented.
> > 
> > Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
> 
> This looks like a -net candidates, could you please add a suitable
> fixes tag?

I found my patch is not a comprehensive fix, because the fw_dnld_timeout()
could also reach fw_dnld_over(), which leads to double free bugs among 
fw_dnld_rx_work(), fw_dnld_timeout() and nfcmrvl_nci_unregister_dev().

I sent a patch set "Fix double free bugs in nfcmrvl module" just now, and add
a fixes tag in it.

Best regards,
Duoming Zhou

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

end of thread, other threads:[~2022-04-12 12:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-10 13:52 [PATCH V2] drivers: nfc: nfcmrvl: fix double free bug in nfcmrvl_nci_unregister_dev() Duoming Zhou
2022-04-11  6:51 ` Krzysztof Kozlowski
2022-04-12  7:28 ` Paolo Abeni
2022-04-12 10:59   ` duoming

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