2020-03-25 09:03:54

by butt3rflyh4ck

[permalink] [raw]
Subject: [PATCH] media: media/pci: prevent memory leak in bttv_probe

In bttv_probe if some functions such as pci_enable_device,
pci_set_dma_mask and request_mem_region fails the allocated
memory for btv should be released.

Signed-off-by: Xiaolong Huang <[email protected]>
---
drivers/media/pci/bt8xx/bttv-driver.c | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/drivers/media/pci/bt8xx/bttv-driver.c b/drivers/media/pci/bt8xx/bttv-driver.c
index a359da7773a9..37ac59110383 100644
--- a/drivers/media/pci/bt8xx/bttv-driver.c
+++ b/drivers/media/pci/bt8xx/bttv-driver.c
@@ -4013,10 +4013,14 @@ static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
btv->id = dev->device;
if (pci_enable_device(dev)) {
pr_warn("%d: Can't enable device\n", btv->c.nr);
+ bttvs[btv->c.nr] = NULL;
+ kfree(btv);
return -EIO;
}
if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
pr_warn("%d: No suitable DMA available\n", btv->c.nr);
+ bttvs[btv->c.nr] = NULL;
+ kfree(btv);
return -EIO;
}
if (!request_mem_region(pci_resource_start(dev,0),
@@ -4025,6 +4029,8 @@ static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
pr_warn("%d: can't request iomem (0x%llx)\n",
btv->c.nr,
(unsigned long long)pci_resource_start(dev, 0));
+ bttvs[btv->c.nr] = NULL;
+ kfree(btv);
return -EBUSY;
}
pci_set_master(dev);
@@ -4211,6 +4217,8 @@ static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
release_mem_region(pci_resource_start(btv->c.pci,0),
pci_resource_len(btv->c.pci,0));
pci_disable_device(btv->c.pci);
+ bttvs[btv->c.nr] = NULL;
+ kfree(btv);
return result;
}

--
2.17.1


2020-04-17 07:28:29

by Hans Verkuil

[permalink] [raw]
Subject: Re: [PATCH] media: media/pci: prevent memory leak in bttv_probe

On 25/03/2020 10:01, Xiaolong Huang wrote:
> In bttv_probe if some functions such as pci_enable_device,
> pci_set_dma_mask and request_mem_region fails the allocated
> memory for btv should be released.
>
> Signed-off-by: Xiaolong Huang <[email protected]>
> ---
> drivers/media/pci/bt8xx/bttv-driver.c | 8 ++++++++
> 1 file changed, 8 insertions(+)
>
> diff --git a/drivers/media/pci/bt8xx/bttv-driver.c b/drivers/media/pci/bt8xx/bttv-driver.c
> index a359da7773a9..37ac59110383 100644
> --- a/drivers/media/pci/bt8xx/bttv-driver.c
> +++ b/drivers/media/pci/bt8xx/bttv-driver.c
> @@ -4013,10 +4013,14 @@ static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
> btv->id = dev->device;
> if (pci_enable_device(dev)) {
> pr_warn("%d: Can't enable device\n", btv->c.nr);
> + bttvs[btv->c.nr] = NULL;
> + kfree(btv);
> return -EIO;

Let's change this...

> }
> if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
> pr_warn("%d: No suitable DMA available\n", btv->c.nr);
> + bttvs[btv->c.nr] = NULL;
> + kfree(btv);
> return -EIO;

this...

> }
> if (!request_mem_region(pci_resource_start(dev,0),
> @@ -4025,6 +4029,8 @@ static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
> pr_warn("%d: can't request iomem (0x%llx)\n",
> btv->c.nr,
> (unsigned long long)pci_resource_start(dev, 0));
> + bttvs[btv->c.nr] = NULL;
> + kfree(btv);
> return -EBUSY;

and this to a goto free_mem.

> }
> pci_set_master(dev);
> @@ -4211,6 +4217,8 @@ static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
> release_mem_region(pci_resource_start(btv->c.pci,0),
> pci_resource_len(btv->c.pci,0));
> pci_disable_device(btv->c.pci);

And add the free_mem: label here.

> + bttvs[btv->c.nr] = NULL;
> + kfree(btv);
> return result;
> }
>
>

Regards,

Hans

2020-04-17 09:47:15

by butt3rflyh4ck

[permalink] [raw]
Subject: Re: [PATCH] media: media/pci: prevent memory leak in bttv_probe

On Fri, Apr 17, 2020 at 3:26 PM Hans Verkuil <[email protected]> wrote:
>
> On 25/03/2020 10:01, Xiaolong Huang wrote:
> > In bttv_probe if some functions such as pci_enable_device,
> > pci_set_dma_mask and request_mem_region fails the allocated
> > memory for btv should be released.
> >
> > Signed-off-by: Xiaolong Huang <[email protected]>
> > ---
> > drivers/media/pci/bt8xx/bttv-driver.c | 8 ++++++++
> > 1 file changed, 8 insertions(+)
> >
> > diff --git a/drivers/media/pci/bt8xx/bttv-driver.c b/drivers/media/pci/bt8xx/bttv-driver.c
> > index a359da7773a9..37ac59110383 100644
> > --- a/drivers/media/pci/bt8xx/bttv-driver.c
> > +++ b/drivers/media/pci/bt8xx/bttv-driver.c
> > @@ -4013,10 +4013,14 @@ static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
> > btv->id = dev->device;
> > if (pci_enable_device(dev)) {
> > pr_warn("%d: Can't enable device\n", btv->c.nr);
> > + bttvs[btv->c.nr] = NULL;
> > + kfree(btv);
> > return -EIO;
>
> Let's change this...
>
> > }
> > if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
> > pr_warn("%d: No suitable DMA available\n", btv->c.nr);
> > + bttvs[btv->c.nr] = NULL;
> > + kfree(btv);
> > return -EIO;
>
> this...
>
> > }
> > if (!request_mem_region(pci_resource_start(dev,0),
> > @@ -4025,6 +4029,8 @@ static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
> > pr_warn("%d: can't request iomem (0x%llx)\n",
> > btv->c.nr,
> > (unsigned long long)pci_resource_start(dev, 0));
> > + bttvs[btv->c.nr] = NULL;
> > + kfree(btv);
> > return -EBUSY;
>
> and this to a goto free_mem.
>
> > }
> > pci_set_master(dev);
> > @@ -4211,6 +4217,8 @@ static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
> > release_mem_region(pci_resource_start(btv->c.pci,0),
> > pci_resource_len(btv->c.pci,0));
> > pci_disable_device(btv->c.pci);
>
> And add the free_mem: label here.
>
> > + bttvs[btv->c.nr] = NULL;
> > + kfree(btv);
> > return result;
> > }
> >
> >
>
> Regards,
>
> Hans

Good, I will modify the patch and resend a new one.

Thanks,

Butt3flyh4ck