Skip to contentSkip to conversation

riscv: Fix memory leak in module_frob_arch_sections()

PATCH Applied Miaoqian Lin 2 participants 2 messages Oct 26, 2025 – Oct 28, 2025

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

PATCH 1/1 riscv: Fix memory leak in module_frob_arch_sections()

The current code directly overwrites the scratch pointer with the return value of kvrealloc(). If kvrealloc() fails and returns NULL, the original buffer becomes unreachable, causing a memory leak.

Fix this by using a temporary variable to store kvrealloc()'s return value and only update the scratch pointer on success.

Found via static anlaysis and this is similar to commit 42378a9ca553 ("bpf, verifier: Fix memory leak in array reallocation for stack state")

Fixes be17c0df6795 ("riscv: module: Optimize PLT/GOT entry counting")
---
 arch/riscv/kernel/module-sections.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/riscv/kernel/module-sections.c +6 -2
index 75551ac6504c..1675cbad8619 100644
@@ -119,6 +119,7 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
 	unsigned int num_plts = 0;
 	unsigned int num_gots = 0;
 	Elf_Rela *scratch = NULL;
+	Elf_Rela *new_scratch;
 	size_t scratch_size = 0;
 	int i;
 
@@ -168,9 +169,12 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
 		scratch_size_needed = (num_scratch_relas + num_relas) * sizeof(*scratch);
 		if (scratch_size_needed > scratch_size) {
 			scratch_size = scratch_size_needed;
-			scratch = kvrealloc(scratch, scratch_size, GFP_KERNEL);
-			if (!scratch)
+			new_scratch = kvrealloc(scratch, scratch_size, GFP_KERNEL);
+			if (!new_scratch) {
+				kvfree(scratch);
 				return -ENOMEM;
+			}
+			scratch = new_scratch;
 		}
 
 		for (size_t j = 0; j < num_relas; j++)
Signature · 1 line
2.39.5 (Apple Git-154)
#1 lore raw
This patch was applied to riscv/linux.git (fixes) by Paul Walmsley <[email protected]>:

Hello:

This patch was applied to riscv/linux.git (fixes)
by Paul Walmsley <[email protected]>:

Quoted 8 lines · On Sun, 26 Oct 2025 17:19:08 +0800 you wrote:

The current code directly overwrites the scratch pointer with the return value of kvrealloc(). If kvrealloc() fails and returns NULL, the original buffer becomes unreachable, causing a memory leak.

Fix this by using a temporary variable to store kvrealloc()'s return value and only update the scratch pointer on success.

[...]

Here is the summary with links:
- riscv: Fix memory leak in module_frob_arch_sections()

    https://git.kernel.org/riscv/c/c42458fcf54b

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html