Skip to contentSkip to conversation

pstore/ram: fix resource leak when ioremap() fails

PATCH In review Cole Leavitt 2 participants 2 messages Feb 25 – Feb 26 Pstore filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pstore/ram: fix resource leak when ioremap() fails

In persistent_ram_iomap(), ioremap() or ioremap_wc() may return NULL on failure. Currently, if this happens, the function returns NULL without releasing the memory region acquired by request_mem_region().

This leads to a resource leak where the memory region remains reserved but unusable.

Additionally, the caller persistent_ram_buffer_map() handles NULL correctly by returning -ENOMEM, but without this check, a NULL return combined with request_mem_region() succeeding leaves resources in an inconsistent state.

This is the ioremap() counterpart to commit 05363abc7625 ("pstore:
ram_core: fix incorrect success return when vmap() fails") which fixed a similar issue in the vmap() path.

Fixes 404a6043385d ("staging: android: persistent_ram: handle reserving and mapping memory")
---
 fs/pstore/ram_core.c | 9 +++++++++
 1 file changed, 9 insertions(+)
fs/pstore/ram_core.c +9 -0
index ed97494abf60..0cc971f70eb0 100644
@@ -493,6 +493,15 @@ static void *persistent_ram_iomap(phys_addr_t start, size_t size,
 	 * there is no need handle anything special like we do when the
 	 * vmap() case in persistent_ram_vmap() above.
 	 */
+	/*
+	 * ioremap() may fail. Release the mem region and return NULL
+	 * to let the caller handle the error.
+	 */
+	if (!va) {
+		release_mem_region(start, size);
+		return NULL;
+	}
+
 	return va;
 }
 
Signature · 1 line
2.52.0
maintainer
#1 lore raw
Quoted 8 lines · Cole Leavitt

In persistent_ram_iomap(), ioremap() or ioremap_wc() may return NULL on failure. Currently, if this happens, the function returns NULL without releasing the memory region acquired by request_mem_region().

This leads to a resource leak where the memory region remains reserved but unusable.

[...]

Thanks! I tweaked the patch a bit and applied to for-next/pstore, thanks!

[1/1] pstore/ram: fix resource leak when ioremap() fails

      https://git.kernel.org/kees/c/bb5278a89dad

Take care,

Signature · 1 line
Kees Cook