* [PATCH] dmaengine: plx_dma: Fix potential double free in plx_dma_create
@ 2022-12-20 6:17 Miaoqian Lin
2022-12-20 16:35 ` Logan Gunthorpe
0 siblings, 1 reply; 4+ messages in thread
From: Miaoqian Lin @ 2022-12-20 6:17 UTC (permalink / raw)
To: Logan Gunthorpe, Vinod Koul, Dan Carpenter, dmaengine, linux-kernel
Cc: linmq006
When all references are dropped, callback function plx_dma_release()
for put_device() will call kfree(plxdev) to release memory.
Fix the error path to fix the possible double free.
Fixes: 07503e6aefe4 ("dmaengine: plx_dma: add a missing put_device() on error path")
Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
---
Please correct me if I make mistakes, thanks for your review.
---
drivers/dma/plx_dma.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/dma/plx_dma.c b/drivers/dma/plx_dma.c
index 12725fa1655f..bce724ff4e16 100644
--- a/drivers/dma/plx_dma.c
+++ b/drivers/dma/plx_dma.c
@@ -546,8 +546,9 @@ static int plx_dma_create(struct pci_dev *pdev)
return 0;
put_device:
- put_device(&pdev->dev);
free_irq(pci_irq_vector(pdev, 0), plxdev);
+ put_device(&pdev->dev);
+ return rc;
free_plx:
kfree(plxdev);
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] dmaengine: plx_dma: Fix potential double free in plx_dma_create
2022-12-20 6:17 [PATCH] dmaengine: plx_dma: Fix potential double free in plx_dma_create Miaoqian Lin
@ 2022-12-20 16:35 ` Logan Gunthorpe
2022-12-23 10:42 ` Dan Carpenter
0 siblings, 1 reply; 4+ messages in thread
From: Logan Gunthorpe @ 2022-12-20 16:35 UTC (permalink / raw)
To: Miaoqian Lin, Vinod Koul, Dan Carpenter, dmaengine, linux-kernel
On 2022-12-19 23:17, Miaoqian Lin wrote:
> When all references are dropped, callback function plx_dma_release()
> for put_device() will call kfree(plxdev) to release memory.
> Fix the error path to fix the possible double free.
>
> Fixes: 07503e6aefe4 ("dmaengine: plx_dma: add a missing put_device() on error path")
> Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
> ---
> Please correct me if I make mistakes, thanks for your review.
> ---
> drivers/dma/plx_dma.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/dma/plx_dma.c b/drivers/dma/plx_dma.c
> index 12725fa1655f..bce724ff4e16 100644
> --- a/drivers/dma/plx_dma.c
> +++ b/drivers/dma/plx_dma.c
> @@ -546,8 +546,9 @@ static int plx_dma_create(struct pci_dev *pdev)
> return 0;
>
> put_device:
> - put_device(&pdev->dev);
> free_irq(pci_irq_vector(pdev, 0), plxdev);
> + put_device(&pdev->dev);
> + return rc;
> free_plx:
> kfree(plxdev);
>
Sorry, after reviewing, I don't think this patch is correct.
plx_dma_release() is called from dma_async_device_unregister() which
won't be called if dma_async_device_register() fails. It does not get
freed when the pci device is put. So I don't think this is a possible
double free.
Logan
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] dmaengine: plx_dma: Fix potential double free in plx_dma_create
2022-12-20 16:35 ` Logan Gunthorpe
@ 2022-12-23 10:42 ` Dan Carpenter
2022-12-23 18:17 ` Logan Gunthorpe
0 siblings, 1 reply; 4+ messages in thread
From: Dan Carpenter @ 2022-12-23 10:42 UTC (permalink / raw)
To: Logan Gunthorpe; +Cc: Miaoqian Lin, Vinod Koul, dmaengine, linux-kernel
On Tue, Dec 20, 2022 at 09:35:38AM -0700, Logan Gunthorpe wrote:
>
>
> On 2022-12-19 23:17, Miaoqian Lin wrote:
> > When all references are dropped, callback function plx_dma_release()
> > for put_device() will call kfree(plxdev) to release memory.
> > Fix the error path to fix the possible double free.
> >
> > Fixes: 07503e6aefe4 ("dmaengine: plx_dma: add a missing put_device() on error path")
> > Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
> > ---
> > Please correct me if I make mistakes, thanks for your review.
> > ---
> > drivers/dma/plx_dma.c | 3 ++-
> > 1 file changed, 2 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/dma/plx_dma.c b/drivers/dma/plx_dma.c
> > index 12725fa1655f..bce724ff4e16 100644
> > --- a/drivers/dma/plx_dma.c
> > +++ b/drivers/dma/plx_dma.c
> > @@ -546,8 +546,9 @@ static int plx_dma_create(struct pci_dev *pdev)
> > return 0;
> >
> > put_device:
> > - put_device(&pdev->dev);
> > free_irq(pci_irq_vector(pdev, 0), plxdev);
> > + put_device(&pdev->dev);
> > + return rc;
> > free_plx:
> > kfree(plxdev);
> >
>
>
> Sorry, after reviewing, I don't think this patch is correct.
>
> plx_dma_release() is called from dma_async_device_unregister() which
> won't be called if dma_async_device_register() fails. It does not get
> freed when the pci device is put. So I don't think this is a possible
> double free.
I think instead of "double free" it Miaoqian meant "use after free". It
does look like a use after free to me. Certainly there is no harm from
applying the patch and it makes the code more obviously correct.
regards,
dan carpenter
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] dmaengine: plx_dma: Fix potential double free in plx_dma_create
2022-12-23 10:42 ` Dan Carpenter
@ 2022-12-23 18:17 ` Logan Gunthorpe
0 siblings, 0 replies; 4+ messages in thread
From: Logan Gunthorpe @ 2022-12-23 18:17 UTC (permalink / raw)
To: Dan Carpenter; +Cc: Miaoqian Lin, Vinod Koul, dmaengine, linux-kernel
On 2022-12-23 03:42, Dan Carpenter wrote:
> On Tue, Dec 20, 2022 at 09:35:38AM -0700, Logan Gunthorpe wrote:
>>
>>
>> On 2022-12-19 23:17, Miaoqian Lin wrote:
>>> When all references are dropped, callback function plx_dma_release()
>>> for put_device() will call kfree(plxdev) to release memory.
>>> Fix the error path to fix the possible double free.
>>>
>>> Fixes: 07503e6aefe4 ("dmaengine: plx_dma: add a missing put_device() on error path")
>>> Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
>>> ---
>>> Please correct me if I make mistakes, thanks for your review.
>>> ---
>>> drivers/dma/plx_dma.c | 3 ++-
>>> 1 file changed, 2 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/drivers/dma/plx_dma.c b/drivers/dma/plx_dma.c
>>> index 12725fa1655f..bce724ff4e16 100644
>>> --- a/drivers/dma/plx_dma.c
>>> +++ b/drivers/dma/plx_dma.c
>>> @@ -546,8 +546,9 @@ static int plx_dma_create(struct pci_dev *pdev)
>>> return 0;
>>>
>>> put_device:
>>> - put_device(&pdev->dev);
>>> free_irq(pci_irq_vector(pdev, 0), plxdev);
>>> + put_device(&pdev->dev);
>>> + return rc;
>>> free_plx:
>>> kfree(plxdev);
>>>
>>
>>
>> Sorry, after reviewing, I don't think this patch is correct.
>>
>> plx_dma_release() is called from dma_async_device_unregister() which
>> won't be called if dma_async_device_register() fails. It does not get
>> freed when the pci device is put. So I don't think this is a possible
>> double free.
>
> I think instead of "double free" it Miaoqian meant "use after free". It
> does look like a use after free to me. Certainly there is no harm from
> applying the patch and it makes the code more obviously correct.
Yeah, moving the put_device() up would certainly look more correct.
But I think it's actually not a bug either because plx_dma_create() is
called from the pci probe function, so the pci device cannot go away.
Logan
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-12-23 18:17 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-20 6:17 [PATCH] dmaengine: plx_dma: Fix potential double free in plx_dma_create Miaoqian Lin
2022-12-20 16:35 ` Logan Gunthorpe
2022-12-23 10:42 ` Dan Carpenter
2022-12-23 18:17 ` Logan Gunthorpe
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.