Re: [PATCH v2] pata_parport: fix possible memory leak

From: Ondrej Zary
Date: Sun Mar 12 2023 - 17:24:43 EST


On Sunday 12 March 2023 01:56:25 Damien Le Moal wrote:
> On 3/12/23 06:44, Ondrej Zary wrote:
> > When ida_alloc() fails, "pi" is not freed although the misleading
> > comment says otherwise.
> > Move the ida_alloc() call up so we really don't have to free it.
>
> Certainly you meant: "so we really do free it in case of error.", no ?

I meant "so we don't have to free pi in case of ida_alloc failure".

> >
> > Reported-by: kernel test robot <lkp@xxxxxxxxx>
> > Reported-by: Dan Carpenter <error27@xxxxxxxxx>
> > Link: https://lore.kernel.org/r/202303111822.IHNchbkp-lkp@xxxxxxxxx/
> > Signed-off-by: Ondrej Zary <linux@xxxxxxx>
> > ---
> > drivers/ata/pata_parport/pata_parport.c | 11 ++++++-----
> > 1 file changed, 6 insertions(+), 5 deletions(-)
> >
> > diff --git a/drivers/ata/pata_parport/pata_parport.c b/drivers/ata/pata_parport/pata_parport.c
> > index 6165ee9aa7da..a9eff6003098 100644
> > --- a/drivers/ata/pata_parport/pata_parport.c
> > +++ b/drivers/ata/pata_parport/pata_parport.c
> > @@ -503,18 +503,19 @@ static struct pi_adapter *pi_init_one(struct parport *parport,
> > if (bus_for_each_dev(&pata_parport_bus_type, NULL, &match, pi_find_dev))
> > return NULL;
> >
> > + id = ida_alloc(&pata_parport_bus_dev_ids, GFP_KERNEL);
> > + if (id < 0)
> > + return NULL;
> > +
> > pi = kzalloc(sizeof(struct pi_adapter), GFP_KERNEL);
> > if (!pi)
> > - return NULL;
> > + goto out_ida_free;
> >
> > /* set up pi->dev before pi_probe_unit() so it can use dev_printk() */
> > pi->dev.parent = &pata_parport_bus;
> > pi->dev.bus = &pata_parport_bus_type;
> > pi->dev.driver = &pr->driver;
> > pi->dev.release = pata_parport_dev_release;
> > - id = ida_alloc(&pata_parport_bus_dev_ids, GFP_KERNEL);
> > - if (id < 0)
> > - return NULL; /* pata_parport_dev_release will do kfree(pi) */
> > pi->dev.id = id;
> > dev_set_name(&pi->dev, "pata_parport.%u", pi->dev.id);
> > if (device_register(&pi->dev)) {
> > @@ -571,7 +572,7 @@ static struct pi_adapter *pi_init_one(struct parport *parport,
> > out_unreg_dev:
> > device_unregister(&pi->dev);
>
> Same comment as Sergey: isn't this going to do the ida free ? So shouldn't you
> return here ?

No. device_unregister() calls pata_parport_dev_release() which does only kfree(pi), not ida_free(). But it probably should do ida_free() too.

>
> > out_ida_free:
> > - ida_free(&pata_parport_bus_dev_ids, pi->dev.id);
> > + ida_free(&pata_parport_bus_dev_ids, id);
> > return NULL;
> > }
> >
>


--
Ondrej Zary