dmaengine.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] dma: Fix a double free in dma_async_device_register
@ 2021-03-30  9:01 Lv Yunlong
  2021-03-30 16:05 ` Dave Jiang
  0 siblings, 1 reply; 4+ messages in thread
From: Lv Yunlong @ 2021-03-30  9:01 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 | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/dma/dmaengine.c b/drivers/dma/dmaengine.c
index fe6a460c4373..fef64b198c95 100644
--- a/drivers/dma/dmaengine.c
+++ b/drivers/dma/dmaengine.c
@@ -1249,8 +1249,10 @@ int dma_async_device_register(struct dma_device *device)
 	/* represent channels in sysfs. Probably want devs too */
 	list_for_each_entry(chan, &device->channels, device_node) {
 		rc = __dma_async_device_channel_register(device, chan);
-		if (rc < 0)
+		if (rc < 0) {
+			chan->local = NULL;
 			goto err_out;
+		}
 	}
 
 	mutex_lock(&dma_list_mutex);
-- 
2.25.1



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

* Re: [PATCH] dma: Fix a double free in dma_async_device_register
  2021-03-30  9:01 [PATCH] dma: Fix a double free in dma_async_device_register Lv Yunlong
@ 2021-03-30 16:05 ` Dave Jiang
  2021-03-31  1:46   ` lyl2019
  0 siblings, 1 reply; 4+ messages in thread
From: Dave Jiang @ 2021-03-30 16:05 UTC (permalink / raw)
  To: Lv Yunlong, vkoul; +Cc: dmaengine, linux-kernel


On 3/30/2021 2:01 AM, 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.

Thanks for the fix. I think it would make sense to set it to NULL in 
__dma_async_device_channel_register() cleanup path after it calls 
free_percpu(chan->local) right? That would address any other instances 
of this issue happening else where.


>
> Fixes: d2fb0a0438384 ("dmaengine: break out channel registration")
> Signed-off-by: Lv Yunlong <lyl2019@mail.ustc.edu.cn>
> ---
>   drivers/dma/dmaengine.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/dma/dmaengine.c b/drivers/dma/dmaengine.c
> index fe6a460c4373..fef64b198c95 100644
> --- a/drivers/dma/dmaengine.c
> +++ b/drivers/dma/dmaengine.c
> @@ -1249,8 +1249,10 @@ int dma_async_device_register(struct dma_device *device)
>   	/* represent channels in sysfs. Probably want devs too */
>   	list_for_each_entry(chan, &device->channels, device_node) {
>   		rc = __dma_async_device_channel_register(device, chan);
> -		if (rc < 0)
> +		if (rc < 0) {
> +			chan->local = NULL;
>   			goto err_out;
> +		}
>   	}
>   
>   	mutex_lock(&dma_list_mutex);

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

* Re: Re: [PATCH] dma: Fix a double free in dma_async_device_register
  2021-03-30 16:05 ` Dave Jiang
@ 2021-03-31  1:46   ` lyl2019
  0 siblings, 0 replies; 4+ messages in thread
From: lyl2019 @ 2021-03-31  1:46 UTC (permalink / raw)
  To: Dave Jiang; +Cc: vkoul, dmaengine, linux-kernel




> -----原始邮件-----
> 发件人: "Dave Jiang" <dave.jiang@intel.com>
> 发送时间: 2021-03-31 00:05:15 (星期三)
> 收件人: "Lv Yunlong" <lyl2019@mail.ustc.edu.cn>, vkoul@kernel.org
> 抄送: dmaengine@vger.kernel.org, linux-kernel@vger.kernel.org
> 主题: Re: [PATCH] dma: Fix a double free in dma_async_device_register
> 
> 
> On 3/30/2021 2:01 AM, 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.
> 
> Thanks for the fix. I think it would make sense to set it to NULL in 
> __dma_async_device_channel_register() cleanup path after it calls 
> free_percpu(chan->local) right? That would address any other instances 
> of this issue happening else where.
> 
> 
> >
> > Fixes: d2fb0a0438384 ("dmaengine: break out channel registration")
> > Signed-off-by: Lv Yunlong <lyl2019@mail.ustc.edu.cn>
> > ---
> >   drivers/dma/dmaengine.c | 4 +++-
> >   1 file changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/dma/dmaengine.c b/drivers/dma/dmaengine.c
> > index fe6a460c4373..fef64b198c95 100644
> > --- a/drivers/dma/dmaengine.c
> > +++ b/drivers/dma/dmaengine.c
> > @@ -1249,8 +1249,10 @@ int dma_async_device_register(struct dma_device *device)
> >   	/* represent channels in sysfs. Probably want devs too */
> >   	list_for_each_entry(chan, &device->channels, device_node) {
> >   		rc = __dma_async_device_channel_register(device, chan);
> > -		if (rc < 0)
> > +		if (rc < 0) {
> > +			chan->local = NULL;
> >   			goto err_out;
> > +		}
> >   	}
> >   
> >   	mutex_lock(&dma_list_mutex);

Ok, that is a good idea. I have submitted the PATCH v2.

Thanks.

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

* [PATCH] dma: Fix a double free in dma_async_device_register
@ 2021-03-22 13:08 Lv Yunlong
  0 siblings, 0 replies; 4+ messages in thread
From: Lv Yunlong @ 2021-03-22 13:08 UTC (permalink / raw)
  To: vkoul; +Cc: dmaengine, linux-kernel, Lv Yunlong

In dma_async_device_register, in the loop
list_for_each_entry(chan, &device->channels, device_node).
If __dma_async_device_channel_register(device, chan) failed
and it colud free chan->local and return err.

But in the err_out branch, it will free chan->local again.
My patch sets chan->local to NULL after it is freed in
__dma_async_device_channel_register().

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] 4+ messages in thread

end of thread, other threads:[~2021-03-31  1:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-30  9:01 [PATCH] dma: Fix a double free in dma_async_device_register Lv Yunlong
2021-03-30 16:05 ` Dave Jiang
2021-03-31  1:46   ` lyl2019
  -- strict thread matches above, loose matches on Subject: below --
2021-03-22 13:08 Lv Yunlong

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