2020-05-28 18:42:05

by Qiushi Wu

[permalink] [raw]
Subject: [PATCH] efi/esrt: Fix reference count leak in esre_create_sysfs_entry.

From: Qiushi Wu <[email protected]>

kobject_init_and_add() takes reference even when it fails.
If this function returns an error, kobject_put() must be called to
properly clean up the memory associated with the object. Previous
commit "b8eb718348b8" fixed a similar problem.

Fixes: 0bb549052d33 ("efi: Add esrt support")
Signed-off-by: Qiushi Wu <[email protected]>
---
drivers/firmware/efi/esrt.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/firmware/efi/esrt.c b/drivers/firmware/efi/esrt.c
index e3d692696583..d5915272141f 100644
--- a/drivers/firmware/efi/esrt.c
+++ b/drivers/firmware/efi/esrt.c
@@ -181,7 +181,7 @@ static int esre_create_sysfs_entry(void *esre, int entry_num)
rc = kobject_init_and_add(&entry->kobj, &esre1_ktype, NULL,
"entry%d", entry_num);
if (rc) {
- kfree(entry);
+ kobject_put(&entry->kobj);
return rc;
}
}
--
2.17.1


2020-05-29 17:05:17

by Ard Biesheuvel

[permalink] [raw]
Subject: Re: [PATCH] efi/esrt: Fix reference count leak in esre_create_sysfs_entry.

On Thu, 28 May 2020 at 20:38, <[email protected]> wrote:
>
> From: Qiushi Wu <[email protected]>
>
> kobject_init_and_add() takes reference even when it fails.
> If this function returns an error, kobject_put() must be called to
> properly clean up the memory associated with the object. Previous
> commit "b8eb718348b8" fixed a similar problem.
>
> Fixes: 0bb549052d33 ("efi: Add esrt support")
> Signed-off-by: Qiushi Wu <[email protected]>
> ---
> drivers/firmware/efi/esrt.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/firmware/efi/esrt.c b/drivers/firmware/efi/esrt.c
> index e3d692696583..d5915272141f 100644
> --- a/drivers/firmware/efi/esrt.c
> +++ b/drivers/firmware/efi/esrt.c
> @@ -181,7 +181,7 @@ static int esre_create_sysfs_entry(void *esre, int entry_num)
> rc = kobject_init_and_add(&entry->kobj, &esre1_ktype, NULL,
> "entry%d", entry_num);
> if (rc) {
> - kfree(entry);

Why are you removing this kfree() call?

> + kobject_put(&entry->kobj);
> return rc;
> }
> }
> --
> 2.17.1
>

2020-06-15 09:53:11

by Ard Biesheuvel

[permalink] [raw]
Subject: Re: [PATCH] efi/esrt: Fix reference count leak in esre_create_sysfs_entry.

On Sat, 30 May 2020 at 06:23, Qiushi Wu <[email protected]> wrote:
>
> Thanks for your reply!
> > Why are you removing this kfree() call?
>
> Because kobject_put(&entry->kobj) will call kobject_release(), which will call kobject_cleanup(), which will dynamically call get_ktype(kobj)->release(kobj); .
> In this case, the "release" function is defined as:
> static struct kobj_type esre1_ktype = {
> .release = esre_release.
> ...
> };
>
> and esre_release() is defined as :
> static void esre_release(struct kobject *kobj) {
> struct esre_entry *entry = to_entry(kobj);
> list_del(&entry->list);
> kfree(entry);
> }
>
> In this case, if we call both kobject_put() and kfree(), a double-free will be introduced.
>

Thanks for the explanation

Queued in efi/urgent.

> On Fri, May 29, 2020 at 12:00 PM Ard Biesheuvel <[email protected]> wrote:
>>
>> On Thu, 28 May 2020 at 20:38, <[email protected]> wrote:
>> >
>> > From: Qiushi Wu <[email protected]>
>> >
>> > kobject_init_and_add() takes reference even when it fails.
>> > If this function returns an error, kobject_put() must be called to
>> > properly clean up the memory associated with the object. Previous
>> > commit "b8eb718348b8" fixed a similar problem.
>> >
>> > Fixes: 0bb549052d33 ("efi: Add esrt support")
>> > Signed-off-by: Qiushi Wu <[email protected]>
>> > ---
>> > drivers/firmware/efi/esrt.c | 2 +-
>> > 1 file changed, 1 insertion(+), 1 deletion(-)
>> >
>> > diff --git a/drivers/firmware/efi/esrt.c b/drivers/firmware/efi/esrt.c
>> > index e3d692696583..d5915272141f 100644
>> > --- a/drivers/firmware/efi/esrt.c
>> > +++ b/drivers/firmware/efi/esrt.c
>> > @@ -181,7 +181,7 @@ static int esre_create_sysfs_entry(void *esre, int entry_num)
>> > rc = kobject_init_and_add(&entry->kobj, &esre1_ktype, NULL,
>> > "entry%d", entry_num);
>> > if (rc) {
>> > - kfree(entry);
>>
>> Why are you removing this kfree() call?
>>
>> > + kobject_put(&entry->kobj);
>> > return rc;
>> > }
>> > }
>> > --
>> > 2.17.1
>> >

2020-06-19 16:51:43

by tip-bot2 for Jacob Pan

[permalink] [raw]
Subject: [tip: efi/urgent] efi/esrt: Fix reference count leak in esre_create_sysfs_entry.

The following commit has been merged into the efi/urgent branch of tip:

Commit-ID: 4ddf4739be6e375116c375f0a68bf3893ffcee21
Gitweb: https://git.kernel.org/tip/4ddf4739be6e375116c375f0a68bf3893ffcee21
Author: Qiushi Wu <[email protected]>
AuthorDate: Thu, 28 May 2020 13:38:04 -05:00
Committer: Ard Biesheuvel <[email protected]>
CommitterDate: Mon, 15 Jun 2020 14:38:56 +02:00

efi/esrt: Fix reference count leak in esre_create_sysfs_entry.

kobject_init_and_add() takes reference even when it fails.
If this function returns an error, kobject_put() must be called to
properly clean up the memory associated with the object. Previous
commit "b8eb718348b8" fixed a similar problem.

Fixes: 0bb549052d33 ("efi: Add esrt support")
Signed-off-by: Qiushi Wu <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Ard Biesheuvel <[email protected]>
---
drivers/firmware/efi/esrt.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/firmware/efi/esrt.c b/drivers/firmware/efi/esrt.c
index e3d6926..d591527 100644
--- a/drivers/firmware/efi/esrt.c
+++ b/drivers/firmware/efi/esrt.c
@@ -181,7 +181,7 @@ static int esre_create_sysfs_entry(void *esre, int entry_num)
rc = kobject_init_and_add(&entry->kobj, &esre1_ktype, NULL,
"entry%d", entry_num);
if (rc) {
- kfree(entry);
+ kobject_put(&entry->kobj);
return rc;
}
}