Skip to contentSkip to conversation

efi/esrt: Fix reference count leak in esre_create_sysfs_entry.

PATCH Applied [email protected] 3 participants 4 messages May 28, 2020 – Jun 19, 2020 1 message not in the archive EFI

lore
Share & tools
lore
#0 lore raw

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.

Fixes 0bb549052d33 ("efi: Add esrt support")
---
 drivers/firmware/efi/esrt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/efi/esrt.c +1 -1
index e3d692696583..d5915272141f 100644
@@ -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
maintainer
#1 lore raw
Quoted 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")
---
 drivers/firmware/efi/esrt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/efi/esrt.c +0 -0
index e3d692696583..d5915272141f 100644

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

maintainer
#3 lore raw
Quoted 11 lines · Qiushi Wu

Thanks for your reply!

Quoted 1 line

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.
...
};

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() and kfree(), 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")
---
 drivers/firmware/efi/esrt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/efi/esrt.c +0 -0
index e3d692696583..d5915272141f 100644

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

merge bot Applied
#4 lore raw
In reply to [email protected]
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
index e3d6926..d591527 100644
@@ -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;
 		}
 	}