efi/esrt: Fix reference count leak in esre_create_sysfs_entry.
PATCH 1/1 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.
--- drivers/firmware/efi/esrt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/efi/esrt.c +1 -1
@@ -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; } }
Signature · 1 line
2.17.1
Ard Biesheuvel
maintainerQuoted 19 lines · 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(-)drivers/firmware/efi/esrt.c +0
-0
drivers/firmware/efi/esrt.c static int esre_create_sysfs_entry(void *esre, int entry_num) · line 184
Quoted 4 lines
drivers/firmware/efi/esrt.c +0
-1@@ -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?
Quoted 7 lines
drivers/firmware/efi/esrt.c +1
-1+kobject_put (&entry->kobj);return rc; } } --2.17.1
A message that is not in the archive (sent off-list or not yet received): CAMV6ehETfqcZS=e4Fk82neOwNJH8Qm+AXXihRQYAPfS4aqjVGg@mail.gmail.com
Ard Biesheuvel
maintainerQuoted 11 lines · Qiushi Wu
Thanks for your reply!
Quoted 1 line
Why are you removing this
kfree()call?Because
kobject_put(&entry->kobj)will callkobject_release(), which will callkobject_cleanup(), which will dynamically call get_ktype(kobj)->release(kobj); . In this case, the "release" function is defined as:
static structkobj_typeesre1_ktype= {
.release =esre_release.
...
};
Quoted 9 lines
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()andkfree(), a double-free will be introduced.
Thanks for the explanation
Queued in efi/urgent.
Quoted 36 lines · Ard Biesheuvel
Quoted 35 lines · On Thu, 28 May 2020 at 20:38, <[email protected]> wrote:
Quoted 19 lines
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(-)drivers/firmware/efi/esrt.c +0
-0drivers/firmware/efi/esrt.c static int esre_create_sysfs_entry(void *esre, int entry_num) · line 184
Quoted 4 lines
drivers/firmware/efi/esrt.c +0
-1@@ -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?Quoted 7 lines
drivers/firmware/efi/esrt.c +1
-1+kobject_put (&entry->kobj);return rc; } } --2.17.1
The following commit has been merged into the efi/urgent branch of tip:
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.
--- drivers/firmware/efi/esrt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/efi/esrt.c +1 -1
@@ -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; } }