Skip to contentSkip to conversation

dmaengine: plx_dma: Fix potential double free in plx_dma_create

PATCH In review Miaoqian Lin 3 participants 4 messages Dec 20, 2022 – Dec 23, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dmaengine: plx_dma: Fix potential double free in plx_dma_create

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

---
Please correct me if I make mistakes, thanks for your review.

---
 drivers/dma/plx_dma.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/dma/plx_dma.c +2 -1
index 12725fa1655f..bce724ff4e16 100644
@@ -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);
 
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 17 lines · Miaoqian Lin

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

---
Please correct me if I make mistakes, thanks for your review.

---
 drivers/dma/plx_dma.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/dma/plx_dma.c +0 -0
index 12725fa1655f..bce724ff4e16 100644

drivers/dma/plx_dma.c static int plx_dma_create(struct pci_dev *pdev) · line 554

Quoted 10 lines
drivers/dma/plx_dma.c +2 -1
@@ -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

#2 lore raw
Quoted 34 lines · Logan Gunthorpe
Quoted 17 lines · Miaoqian Lin

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

---
Please correct me if I make mistakes, thanks for your review.

---
 drivers/dma/plx_dma.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/dma/plx_dma.c +0 -0
index 12725fa1655f..bce724ff4e16 100644

drivers/dma/plx_dma.c static int plx_dma_create(struct pci_dev *pdev) · line 554

Quoted 10 lines
drivers/dma/plx_dma.c +2 -1
@@ -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.

Quoted 4 lines

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

maintainer
#3 lore raw
Quoted 35 lines · Dan Carpenter
Quoted 34 lines · Logan Gunthorpe
Quoted 17 lines · Miaoqian Lin

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

---
Please correct me if I make mistakes, thanks for your review.

---
 drivers/dma/plx_dma.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/dma/plx_dma.c +0 -0
index 12725fa1655f..bce724ff4e16 100644

drivers/dma/plx_dma.c static int plx_dma_create(struct pci_dev *pdev) · line 554

Quoted 10 lines
drivers/dma/plx_dma.c +2 -1
@@ -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.

Quoted 8 lines
Quoted 4 lines

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