Skip to contentSkip to conversation

pstore/ram: Fix memory leak in persistent_ram_new

PATCH In review Navid Emamdoost 3 participants 3 messages Dec 11, 2019 – Dec 30, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pstore/ram: Fix memory leak in persistent_ram_new

In the implementation of persistent_ram_new(), if the allocation for prz fails, "label" should be released as part of error handling. Release the label via kfree().

Fixes 8cf5aff89e59 ("staging: android: persistent_ram: Introduce persistent_ram_new()")
---
 fs/pstore/ram_core.c | 1 +
 1 file changed, 1 insertion(+)
fs/pstore/ram_core.c +1 -0
index 8823f65888f0..7d2d86999211 100644
@@ -568,6 +568,7 @@ struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size,
 	prz = kzalloc(sizeof(struct persistent_ram_zone), GFP_KERNEL);
 	if (!prz) {
 		pr_err("failed to allocate persistent ram zone\n");
+		kfree(label);
 		goto err;
 	}
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 2 lines

In the implementation of persistent_ram_new(), if the allocation for prz fails, "label" should be released as part of error handling.

How did you come to this conclusion?

I suggest to reconsider this patch because the mentioned string is passed by a function parameter.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/pstore/ram_core.c?id7d4e84f765bb3038ddfeebdc5d1cfd7e1ef688f#n561
https://elixir.bootlin.com/linux/v5.5-rc1/source/fs/pstore/ram_core.c#L561

I recognise other implementation details which could be improved here.
https://lore.kernel.org/lkml/[email protected]/
https://lkml.org/lkml/2017/8/16/645

Regards,
Markus

#2 lore raw
In reply to Navid Emamdoost
Quoted 6 lines · Navid Emamdoost

In the implementation of persistent_ram_new(), if the allocation for prz fails, "label" should be released as part of error handling. Release the label via kfree().

Fixes 8cf5aff89e59 ("staging: android: persistent_ram: Introduce persistent_ram_new()")

You're right about the need to clean up the allocation, but I think it's on the caller to do this, not persistent_ram_new(), in case a const char string is used in the future.

I think the "Fixes" should also be:

Fixes 1227daa43bce ("pstore/ram: Clarify resource reservation labels")

-Kees

Quoted 19 lines
---
 fs/pstore/ram_core.c | 1 +
 1 file changed, 1 insertion(+)
fs/pstore/ram_core.c +1 -0
index 8823f65888f0..7d2d86999211 100644
@@ -568,6 +568,7 @@ struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size,
 	prz = kzalloc(sizeof(struct persistent_ram_zone), GFP_KERNEL);
 	if (!prz) {
 		pr_err("failed to allocate persistent ram zone\n");
+		kfree(label);
 		goto err;
 	}
 
Signature · 1 line
2.17.1
Signature · 1 line
Kees Cook