Skip to contentSkip to conversation

staging: comedi: drivers: Fix memory leak in gsc_hpdi_auto_attach

PATCH In review Navid Emamdoost 3 participants 4 messages Dec 15, 2019 – Dec 16, 2019

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

PATCH 1/1 staging: comedi: drivers: Fix memory leak in gsc_hpdi_auto_attach

In the implementation of gsc_hpdi_auto_attach(), the allocated dma description is leaks in case of alignment error, or failure of gsc_hpdi_setup_dma_descriptors() or comedi_alloc_subdevices(). Release devpriv->dma_desc via dma_free_coherent().

---
 drivers/staging/comedi/drivers/gsc_hpdi.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/staging/comedi/drivers/gsc_hpdi.c +13 -3
index 4bdf44d82879..c0c7047a6d1b 100644
@@ -633,16 +633,17 @@ static int gsc_hpdi_auto_attach(struct comedi_device *dev,
 	if (devpriv->dma_desc_phys_addr & 0xf) {
 		dev_warn(dev->class_dev,
 			 " dma descriptors not quad-word aligned (bug)\n");
-		return -EIO;
+		retval = -EIO;
+		goto release_dma_desc;
 	}
 
 	retval = gsc_hpdi_setup_dma_descriptors(dev, 0x1000);
 	if (retval < 0)
-		return retval;
+		goto release_dma_desc;
 
 	retval = comedi_alloc_subdevices(dev, 1);
 	if (retval)
-		return retval;
+		goto release_dma_desc;
 
 	/* Digital I/O subdevice */
 	s = &dev->subdevices[0];
@@ -660,6 +661,15 @@ static int gsc_hpdi_auto_attach(struct comedi_device *dev,
 	s->cancel	= gsc_hpdi_cancel;
 
 	return gsc_hpdi_init(dev);
+
+release_dma_desc:
+	if (devpriv->dma_desc)
+		dma_free_coherent(&pcidev->dev,
+				  sizeof(struct plx_dma_desc) *
+				NUM_DMA_DESCRIPTORS,
+				devpriv->dma_desc,
+				devpriv->dma_desc_phys_addr);
+	return retval;
 }
 
 static void gsc_hpdi_detach(struct comedi_device *dev)
Signature · 1 line
2.17.1
#1 lore raw
Quoted 2 lines

In the implementation of gsc_hpdi_auto_attach(), the allocated dma description is leaks in case of alignment error, …

Please avoid a typo in this change message.

…
+++ b/drivers/staging/comedi/drivers/gsc_hpdi.c
…

Quoted 6 lines
drivers/staging/comedi/drivers/gsc_hpdi.c +2 -0
@@ -660,6 +661,15 @@ static int gsc_hpdi_auto_attach(struct comedi_device *dev,
 	s->cancel	= gsc_hpdi_cancel;
 
 	return gsc_hpdi_init(dev);
+
+release_dma_desc:

drivers/staging/comedi/drivers/gsc_hpdi.c static int gsc_hpdi_auto_attach(struct comedi_device *dev, · line 675

Quoted 10 lines
drivers/staging/comedi/drivers/gsc_hpdi.c +7 -0
+	if (devpriv->dma_desc)
+		dma_free_coherent(&pcidev->dev,
+				  sizeof(struct plx_dma_desc) *
+				NUM_DMA_DESCRIPTORS,
+				devpriv->dma_desc,
+				devpriv->dma_desc_phys_addr);
+	return retval;
 }
 
 static void gsc_hpdi_detach(struct comedi_device *dev)

I got the impression that return values from calls of the function “dma_alloc_coherent” should be checked before.
* Would you like to add null pointer checks at other source code places?
* Should the jump targets be accordingly adjusted then for the completion of the desired exception handling?

Regards,
Markus

#2 lore raw
In reply to Navid Emamdoost
Quoted 6 lines · Navid Emamdoost

In the implementation of gsc_hpdi_auto_attach(), the allocated dma description is leaks in case of alignment error, or failure of gsc_hpdi_setup_dma_descriptors() or comedi_alloc_subdevices(). Release devpriv->dma_desc via dma_free_coherent().

Actually, there is no memory leak (although there is another problem that I'll mention below). If the "auto_attach" handler gsc_hpdi_auto_attach() returns an error, then the "detach" handler gsc_hpdi_detach() will be called automatically to clean up. (This is true for all comedi drivers). gsc_hpdi_detach() calls gsc_hpdi_free_dma() to free the DMA buffers and DMA descriptors.

Quoted 36 lines
---
  drivers/staging/comedi/drivers/gsc_hpdi.c | 16 +++++++++++++---
  1 file changed, 13 insertions(+), 3 deletions(-)
drivers/staging/comedi/drivers/gsc_hpdi.c +6 -3
index 4bdf44d82879..c0c7047a6d1b 100644
@@ -633,16 +633,17 @@ static int gsc_hpdi_auto_attach(struct comedi_device *dev,
  	if (devpriv->dma_desc_phys_addr & 0xf) {
  		dev_warn(dev->class_dev,
  			 " dma descriptors not quad-word aligned (bug)\n");
-		return -EIO;
+		retval = -EIO;
+		goto release_dma_desc;
  	}
 
  	retval = gsc_hpdi_setup_dma_descriptors(dev, 0x1000);
  	if (retval < 0)
-		return retval;
+		goto release_dma_desc;
 
  	retval = comedi_alloc_subdevices(dev, 1);
  	if (retval)
-		return retval;
+		goto release_dma_desc;
 
  	/* Digital I/O subdevice */
  	s = &dev->subdevices[0];
@@ -660,6 +661,15 @@ static int gsc_hpdi_auto_attach(struct comedi_device *dev,
  	s->cancel	= gsc_hpdi_cancel;
 
  	return gsc_hpdi_init(dev);
+
+release_dma_desc:

drivers/staging/comedi/drivers/gsc_hpdi.c static int gsc_hpdi_auto_attach(struct comedi_device *dev, · line 675

Quoted 10 lines
drivers/staging/comedi/drivers/gsc_hpdi.c +7 -0
+	if (devpriv->dma_desc)
+		dma_free_coherent(&pcidev->dev,
+				  sizeof(struct plx_dma_desc) *
+				NUM_DMA_DESCRIPTORS,
+				devpriv->dma_desc,
+				devpriv->dma_desc_phys_addr);
+	return retval;
  }
 
  static void gsc_hpdi_detach(struct comedi_device *dev)

This patch could actually result in devpriv->dma_desc being freed twice - once in the 'release_dma_desc:' code and again when gsc_hpdi_detach() is called externally as part of the clean-up.

The real bug in the original code is that it does not check whether any of the calls to dma_alloc_coherent() returned NULL. If any of the calls to dma_alloc_coherent() returns NULL, gsc_hpdi_auto_attach() needs to return an error (-ENOMEM). The subsequent call to gsc_hpdi_detach() will then free whatever DMA coherent buffers where allocated.

Signature · 4 lines
-=( Ian Abbott <[email protected]> || Web: www.mev.co.uk )=-
-=( MEV Ltd. is a company registered in England & Wales. )=-
-=( Registered number: 02862268.  Registered address:    )=-
-=( 15 West Park Road, Bramhall, STOCKPORT, SK7 3JZ, UK. )=-
#3 lore raw

Ian, thanks for your feedback.

Quoted 7 lines · Ian Abbott
Quoted 5 lines · Navid Emamdoost

In the implementation of gsc_hpdi_auto_attach(), the allocated dma description is leaks in case of alignment error, or failure of gsc_hpdi_setup_dma_descriptors() or comedi_alloc_subdevices(). Release devpriv->dma_desc via dma_free_coherent().

Quoted 9 lines
Quoted 1 line

Actually, there is no memory leak (although there is another problem that I'll mention below). If the "auto_attach" handler gsc_hpdi_auto_attach() returns an error, then the "detach" handler gsc_hpdi_detach() will be called automatically to clean up. (This is true for all comedi drivers). gsc_hpdi_detach() calls gsc_hpdi_free_dma() to free the DMA buffers and DMA descriptors.

I was aware that comedi_alloc_devpriv() is a resource managed allocation, but was not sure how subsequent dma_desc allocation will be handled when device detach.

Quoted 51 lines
Quoted 36 lines
---
  drivers/staging/comedi/drivers/gsc_hpdi.c | 16 +++++++++++++---
  1 file changed, 13 insertions(+), 3 deletions(-)
drivers/staging/comedi/drivers/gsc_hpdi.c +6 -3
index 4bdf44d82879..c0c7047a6d1b 100644
@@ -633,16 +633,17 @@ static int gsc_hpdi_auto_attach(struct comedi_device *dev,
      if (devpriv->dma_desc_phys_addr & 0xf) {
              dev_warn(dev->class_dev,
                       " dma descriptors not quad-word aligned (bug)\n");
-             return -EIO;
+             retval = -EIO;
+             goto release_dma_desc;
      }
 
      retval = gsc_hpdi_setup_dma_descriptors(dev, 0x1000);
      if (retval < 0)
-             return retval;
+             goto release_dma_desc;
 
      retval = comedi_alloc_subdevices(dev, 1);
      if (retval)
-             return retval;
+             goto release_dma_desc;
 
      /* Digital I/O subdevice */
      s = &dev->subdevices[0];
@@ -660,6 +661,15 @@ static int gsc_hpdi_auto_attach(struct comedi_device *dev,
      s->cancel       = gsc_hpdi_cancel;
 
      return gsc_hpdi_init(dev);
+
+release_dma_desc:

drivers/staging/comedi/drivers/gsc_hpdi.c static int gsc_hpdi_auto_attach(struct comedi_device *dev, · line 675

Quoted 10 lines
drivers/staging/comedi/drivers/gsc_hpdi.c +7 -0
+     if (devpriv->dma_desc)
+             dma_free_coherent(&pcidev->dev,
+                               sizeof(struct plx_dma_desc) *
+                             NUM_DMA_DESCRIPTORS,
+                             devpriv->dma_desc,
+                             devpriv->dma_desc_phys_addr);
+     return retval;
  }
 
  static void gsc_hpdi_detach(struct comedi_device *dev)

This patch could actually result in devpriv->dma_desc being freed twice
- once in the 'release_dma_desc:' code and again when gsc_hpdi_detach() is called externally as part of the clean-up.

Quoted 6 lines

The real bug in the original code is that it does not check whether any of the calls to dma_alloc_coherent() returned NULL. If any of the calls to dma_alloc_coherent() returns NULL, gsc_hpdi_auto_attach() needs to return an error (-ENOMEM). The subsequent call to gsc_hpdi_detach() will then free whatever DMA coherent buffers where allocated.

Yes, this potential null deref is another type of bug, which I will send a patch for separately.

Quoted 5 lines

--
-=( Ian Abbott <[email protected]> || Web: www.mev.co.uk )=- -=( MEV Ltd. is a company registered in England & Wales. )=-
-=( Registered number: 02862268. Registered address: )=-
-=( 15 West Park Road, Bramhall, STOCKPORT, SK7 3JZ, UK. )=-

Signature · 1 line
Navid.