2021-11-16 11:43:01

by Miaoqian Lin

[permalink] [raw]
Subject: [PATCH] fw_cfg: Fix memory leak in fw_cfg_register_file

When kobject_init_and_add() fails, entry should be freed just like
when sysfs_create_bin_file() fails.

Signed-off-by: Miaoqian Lin <[email protected]>
---
drivers/firmware/qemu_fw_cfg.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/drivers/firmware/qemu_fw_cfg.c b/drivers/firmware/qemu_fw_cfg.c
index 172c751a4f6c..0f404777f016 100644
--- a/drivers/firmware/qemu_fw_cfg.c
+++ b/drivers/firmware/qemu_fw_cfg.c
@@ -608,6 +608,7 @@ static int fw_cfg_register_file(const struct fw_cfg_file *f)
fw_cfg_sel_ko, "%d", entry->select);
if (err) {
kobject_put(&entry->kobj);
+ kfree(entry);
return err;
}

--
2.17.1



2021-11-16 15:28:57

by Philippe Mathieu-Daudé

[permalink] [raw]
Subject: Re: [PATCH] fw_cfg: Fix memory leak in fw_cfg_register_file

On 11/16/21 12:42, Miaoqian Lin wrote:
> When kobject_init_and_add() fails, entry should be freed just like
> when sysfs_create_bin_file() fails.
>

Fixes: fe3c60684377 ("firmware: Fix a reference count leak.")
Reviewed-by: Philippe Mathieu-Daudé <[email protected]>

> Signed-off-by: Miaoqian Lin <[email protected]>
> ---
> drivers/firmware/qemu_fw_cfg.c | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/drivers/firmware/qemu_fw_cfg.c b/drivers/firmware/qemu_fw_cfg.c
> index 172c751a4f6c..0f404777f016 100644
> --- a/drivers/firmware/qemu_fw_cfg.c
> +++ b/drivers/firmware/qemu_fw_cfg.c
> @@ -608,6 +608,7 @@ static int fw_cfg_register_file(const struct fw_cfg_file *f)
> fw_cfg_sel_ko, "%d", entry->select);
> if (err) {
> kobject_put(&entry->kobj);
> + kfree(entry);
> return err;
> }
>
>


2021-11-30 17:18:55

by Johan Hovold

[permalink] [raw]
Subject: Re: [PATCH] fw_cfg: Fix memory leak in fw_cfg_register_file

On Tue, Nov 16, 2021 at 04:28:34PM +0100, Philippe Mathieu-Daudé wrote:
> On 11/16/21 12:42, Miaoqian Lin wrote:
> > When kobject_init_and_add() fails, entry should be freed just like
> > when sysfs_create_bin_file() fails.
> >
>
> Fixes: fe3c60684377 ("firmware: Fix a reference count leak.")
> Reviewed-by: Philippe Mathieu-Daudé <[email protected]>

No, no. This patch is completely bogus and would introduce a double
free.

> > Signed-off-by: Miaoqian Lin <[email protected]>
> > ---
> > drivers/firmware/qemu_fw_cfg.c | 1 +
> > 1 file changed, 1 insertion(+)
> >
> > diff --git a/drivers/firmware/qemu_fw_cfg.c b/drivers/firmware/qemu_fw_cfg.c
> > index 172c751a4f6c..0f404777f016 100644
> > --- a/drivers/firmware/qemu_fw_cfg.c
> > +++ b/drivers/firmware/qemu_fw_cfg.c
> > @@ -608,6 +608,7 @@ static int fw_cfg_register_file(const struct fw_cfg_file *f)
> > fw_cfg_sel_ko, "%d", entry->select);
> > if (err) {
> > kobject_put(&entry->kobj);
> > + kfree(entry);

entry would already have been freed by kobject_put() and
fw_cfg_sysfs_release_entry() here.

> > return err;
> > }
> >
> >

Doesn't look like this patch has been picked up yet, so:

NAK.

Johan