linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] dma: Fix a double free in dma_async_device_register
@ 2021-03-31  1:44 Lv Yunlong
  2021-03-31  2:48 ` Dave Jiang
  2021-04-12  9:36 ` Vinod Koul
  0 siblings, 2 replies; 3+ messages in thread
From: Lv Yunlong @ 2021-03-31  1:44 UTC (permalink / raw)
  To: vkoul, dave.jiang; +Cc: dmaengine, linux-kernel, Lv Yunlong

In the first list_for_each_entry() macro of dma_async_device_register,
it gets the chan from list and calls __dma_async_device_channel_register
(..,chan). We can see that chan->local is allocated by alloc_percpu() and
it is freed chan->local by free_percpu(chan->local) when
__dma_async_device_channel_register() failed.

But after __dma_async_device_channel_register() failed, the caller will
goto err_out and freed the chan->local in the second time by free_percpu().

The cause of this problem is forget to set chan->local to NULL when
chan->local was freed in __dma_async_device_channel_register(). My
patch sets chan->local to NULL when the callee failed to avoid double free.

Fixes: d2fb0a0438384 ("dmaengine: break out channel registration")
Signed-off-by: Lv Yunlong <lyl2019@mail.ustc.edu.cn>
---
 drivers/dma/dmaengine.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/dma/dmaengine.c b/drivers/dma/dmaengine.c
index fe6a460c4373..af3ee288bc11 100644
--- a/drivers/dma/dmaengine.c
+++ b/drivers/dma/dmaengine.c
@@ -1086,6 +1086,7 @@ static int __dma_async_device_channel_register(struct dma_device *device,
 	kfree(chan->dev);
  err_free_local:
 	free_percpu(chan->local);
+	chan->local = NULL;
 	return rc;
 }
 
-- 
2.25.1



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

* Re: [PATCH v2] dma: Fix a double free in dma_async_device_register
  2021-03-31  1:44 [PATCH v2] dma: Fix a double free in dma_async_device_register Lv Yunlong
@ 2021-03-31  2:48 ` Dave Jiang
  2021-04-12  9:36 ` Vinod Koul
  1 sibling, 0 replies; 3+ messages in thread
From: Dave Jiang @ 2021-03-31  2:48 UTC (permalink / raw)
  To: Lv Yunlong, vkoul; +Cc: dmaengine, linux-kernel


On 3/30/2021 6:44 PM, Lv Yunlong wrote:
> In the first list_for_each_entry() macro of dma_async_device_register,
> it gets the chan from list and calls __dma_async_device_channel_register
> (..,chan). We can see that chan->local is allocated by alloc_percpu() and
> it is freed chan->local by free_percpu(chan->local) when
> __dma_async_device_channel_register() failed.
>
> But after __dma_async_device_channel_register() failed, the caller will
> goto err_out and freed the chan->local in the second time by free_percpu().
>
> The cause of this problem is forget to set chan->local to NULL when
> chan->local was freed in __dma_async_device_channel_register(). My
> patch sets chan->local to NULL when the callee failed to avoid double free.
>
> Fixes: d2fb0a0438384 ("dmaengine: break out channel registration")
> Signed-off-by: Lv Yunlong <lyl2019@mail.ustc.edu.cn>

Reviewed-by: Dave Jiang <dave.jiang@intel.com>


> ---
>   drivers/dma/dmaengine.c | 1 +
>   1 file changed, 1 insertion(+)
>
> diff --git a/drivers/dma/dmaengine.c b/drivers/dma/dmaengine.c
> index fe6a460c4373..af3ee288bc11 100644
> --- a/drivers/dma/dmaengine.c
> +++ b/drivers/dma/dmaengine.c
> @@ -1086,6 +1086,7 @@ static int __dma_async_device_channel_register(struct dma_device *device,
>   	kfree(chan->dev);
>    err_free_local:
>   	free_percpu(chan->local);
> +	chan->local = NULL;
>   	return rc;
>   }
>   

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

* Re: [PATCH v2] dma: Fix a double free in dma_async_device_register
  2021-03-31  1:44 [PATCH v2] dma: Fix a double free in dma_async_device_register Lv Yunlong
  2021-03-31  2:48 ` Dave Jiang
@ 2021-04-12  9:36 ` Vinod Koul
  1 sibling, 0 replies; 3+ messages in thread
From: Vinod Koul @ 2021-04-12  9:36 UTC (permalink / raw)
  To: Lv Yunlong; +Cc: dave.jiang, dmaengine, linux-kernel

On 30-03-21, 18:44, Lv Yunlong wrote:
> In the first list_for_each_entry() macro of dma_async_device_register,
> it gets the chan from list and calls __dma_async_device_channel_register
> (..,chan). We can see that chan->local is allocated by alloc_percpu() and
> it is freed chan->local by free_percpu(chan->local) when
> __dma_async_device_channel_register() failed.
> 
> But after __dma_async_device_channel_register() failed, the caller will
> goto err_out and freed the chan->local in the second time by free_percpu().
> 
> The cause of this problem is forget to set chan->local to NULL when
> chan->local was freed in __dma_async_device_channel_register(). My
> patch sets chan->local to NULL when the callee failed to avoid double free.

Applied after fixing subsystem name, thanks

-- 
~Vinod

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

end of thread, other threads:[~2021-04-12  9:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-31  1:44 [PATCH v2] dma: Fix a double free in dma_async_device_register Lv Yunlong
2021-03-31  2:48 ` Dave Jiang
2021-04-12  9:36 ` Vinod Koul

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