linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] staging: comedi: drivers: Fix memory leak in gsc_hpdi_auto_attach
@ 2019-12-15  1:33 Navid Emamdoost
  2019-12-15 15:07 ` Markus Elfring
  2019-12-16 10:36 ` Ian Abbott
  0 siblings, 2 replies; 4+ messages in thread
From: Navid Emamdoost @ 2019-12-15  1:33 UTC (permalink / raw)
  To: Ian Abbott, H Hartley Sweeten, Greg Kroah-Hartman,
	Navid Emamdoost, devel, linux-kernel
  Cc: emamd001

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

Signed-off-by: Navid Emamdoost <navid.emamdoost@gmail.com>
---
 drivers/staging/comedi/drivers/gsc_hpdi.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/drivers/staging/comedi/drivers/gsc_hpdi.c b/drivers/staging/comedi/drivers/gsc_hpdi.c
index 4bdf44d82879..c0c7047a6d1b 100644
--- a/drivers/staging/comedi/drivers/gsc_hpdi.c
+++ b/drivers/staging/comedi/drivers/gsc_hpdi.c
@@ -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)
-- 
2.17.1


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

* Re: [PATCH] staging: comedi: drivers: Fix memory leak in gsc_hpdi_auto_attach
  2019-12-15  1:33 [PATCH] staging: comedi: drivers: Fix memory leak in gsc_hpdi_auto_attach Navid Emamdoost
@ 2019-12-15 15:07 ` Markus Elfring
  2019-12-16 10:36 ` Ian Abbott
  1 sibling, 0 replies; 4+ messages in thread
From: Markus Elfring @ 2019-12-15 15:07 UTC (permalink / raw)
  To: Navid Emamdoost, devel
  Cc: linux-kernel, kernel-janitors, Greg Kroah-Hartman,
	H Hartley Sweeten, Ian Abbott, Navid Emamdoost

> 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
…
> @@ -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)

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

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

* Re: [PATCH] staging: comedi: drivers: Fix memory leak in gsc_hpdi_auto_attach
  2019-12-15  1:33 [PATCH] staging: comedi: drivers: Fix memory leak in gsc_hpdi_auto_attach Navid Emamdoost
  2019-12-15 15:07 ` Markus Elfring
@ 2019-12-16 10:36 ` Ian Abbott
  2019-12-16 20:18   ` Navid Emamdoost
  1 sibling, 1 reply; 4+ messages in thread
From: Ian Abbott @ 2019-12-16 10:36 UTC (permalink / raw)
  To: Navid Emamdoost, H Hartley Sweeten, Greg Kroah-Hartman, devel,
	linux-kernel
  Cc: emamd001

On 15/12/2019 01:33, Navid Emamdoost wrote:
> 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().
> 
> Signed-off-by: Navid Emamdoost <navid.emamdoost@gmail.com>

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.

> ---
>   drivers/staging/comedi/drivers/gsc_hpdi.c | 16 +++++++++++++---
>   1 file changed, 13 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/staging/comedi/drivers/gsc_hpdi.c b/drivers/staging/comedi/drivers/gsc_hpdi.c
> index 4bdf44d82879..c0c7047a6d1b 100644
> --- a/drivers/staging/comedi/drivers/gsc_hpdi.c
> +++ b/drivers/staging/comedi/drivers/gsc_hpdi.c
> @@ -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)
> 

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.

-- 
-=( Ian Abbott <abbotti@mev.co.uk> || 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. )=-

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

* Re: [PATCH] staging: comedi: drivers: Fix memory leak in gsc_hpdi_auto_attach
  2019-12-16 10:36 ` Ian Abbott
@ 2019-12-16 20:18   ` Navid Emamdoost
  0 siblings, 0 replies; 4+ messages in thread
From: Navid Emamdoost @ 2019-12-16 20:18 UTC (permalink / raw)
  To: Ian Abbott
  Cc: H Hartley Sweeten, Greg Kroah-Hartman, devel, LKML, Navid Emamdoost

Ian, thanks for your feedback.

On Mon, Dec 16, 2019 at 4:36 AM Ian Abbott <abbotti@mev.co.uk> wrote:
>
> On 15/12/2019 01:33, Navid Emamdoost wrote:
> > 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().
> >
> > Signed-off-by: Navid Emamdoost <navid.emamdoost@gmail.com>
>
> 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.

> > ---
> >   drivers/staging/comedi/drivers/gsc_hpdi.c | 16 +++++++++++++---
> >   1 file changed, 13 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/staging/comedi/drivers/gsc_hpdi.c b/drivers/staging/comedi/drivers/gsc_hpdi.c
> > index 4bdf44d82879..c0c7047a6d1b 100644
> > --- a/drivers/staging/comedi/drivers/gsc_hpdi.c
> > +++ b/drivers/staging/comedi/drivers/gsc_hpdi.c
> > @@ -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)
> >
>
> 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.
>
Yes, this potential null deref is another type of bug, which I will
send a patch for separately.

> --
> -=( Ian Abbott <abbotti@mev.co.uk> || 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. )=-



-- 
Navid.

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

end of thread, other threads:[~2019-12-16 20:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-15  1:33 [PATCH] staging: comedi: drivers: Fix memory leak in gsc_hpdi_auto_attach Navid Emamdoost
2019-12-15 15:07 ` Markus Elfring
2019-12-16 10:36 ` Ian Abbott
2019-12-16 20:18   ` Navid Emamdoost

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