Skip to contentSkip to conversation

x86/kexec: Fix double-free of elf header buffer

PATCH Applied Takashi Iwai 4 participants 4 messages Nov 22, 2022 – Jan 2, 2023

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

PATCH 1/1 x86/kexec: Fix double-free of elf header buffer

The recent fix for memory leaks forgot to clear the error path that already does vfree() for the elf headers. This may result in a double-free.

Drop the superfluous vfree() call at the error path of crash_load_segments().

Fixes b3e34a47f989 ("x86/kexec: fix memory leak of elf header buffer")
---
 arch/x86/kernel/crash.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
arch/x86/kernel/crash.c +1 -3
index 9730c88530fc..305514431f26 100644
@@ -401,10 +401,8 @@ int crash_load_segments(struct kimage *image)
 	kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
 	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 	ret = kexec_add_buffer(&kbuf);
-	if (ret) {
-		vfree((void *)image->elf_headers);
+	if (ret)
 		return ret;
-	}
 	image->elf_load_addr = kbuf.mem;
 	pr_debug("Loaded ELF headers at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
 		 image->elf_load_addr, kbuf.bufsz, kbuf.memsz);
Signature · 1 line
2.35.3
Acked-by on v1 1/1
#1 lore raw

CC [email protected]

Quoted 3 lines · Takashi Iwai

The recent fix for memory leaks forgot to clear the error path that already does vfree() for the elf headers. This may result in a double-free.

Good catch.

The log can be improved to make the issue more obvious. How about this:
======
With commit b3e34a47f989 ("x86/kexec: fix memory leak of elf header buffer"), freeing up image->elf_headers in error path of crash_load_segments() is not needed, because later kimage_file_post_load_cleanup() will cover it. And not clearing it could result in a double-free.

Drop the superfluous vfree() call at the error path of crash_load_segments().
========

Other than the log part, LGTM. Thanks.

Quoted 29 lines

Drop the superfluous vfree() call at the error path of crash_load_segments().

Fixes b3e34a47f989 ("x86/kexec: fix memory leak of elf header buffer")
---
 arch/x86/kernel/crash.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
arch/x86/kernel/crash.c +1 -3
index 9730c88530fc..305514431f26 100644
@@ -401,10 +401,8 @@ int crash_load_segments(struct kimage *image)
 	kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
 	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 	ret = kexec_add_buffer(&kbuf);
-	if (ret) {
-		vfree((void *)image->elf_headers);
+	if (ret)
 		return ret;
-	}
 	image->elf_load_addr = kbuf.mem;
 	pr_debug("Loaded ELF headers at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
 		 image->elf_load_addr, kbuf.bufsz, kbuf.memsz);
Signature · 1 line
2.35.3

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

Acked-by on v1 1/1
#2 lore raw
In reply to Takashi Iwai
Quoted 9 lines · Takashi Iwai

The recent fix for memory leaks forgot to clear the error path that already does vfree() for the elf headers. This may result in a double-free.

Drop the superfluous vfree() call at the error path of crash_load_segments().

Fixes b3e34a47f989 ("x86/kexec: fix memory leak of elf header buffer")
Quoted 20 lines
---
 arch/x86/kernel/crash.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
arch/x86/kernel/crash.c +1 -3
index 9730c88530fc..305514431f26 100644
@@ -401,10 +401,8 @@ int crash_load_segments(struct kimage *image)
 	kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
 	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 	ret = kexec_add_buffer(&kbuf);
-	if (ret) {
-		vfree((void *)image->elf_headers);
+	if (ret)
 		return ret;
-	}
 	image->elf_load_addr = kbuf.mem;
 	pr_debug("Loaded ELF headers at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
 		 image->elf_load_addr, kbuf.bufsz, kbuf.memsz);
merge bot Applied
#3 lore raw
In reply to Takashi Iwai
The following commit has been merged into the x86/urgent branch of tip:

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     d00dd2f2645dca04cf399d8fc692f3f69b6dd996
Gitweb:        https://git.kernel.org/tip/d00dd2f2645dca04cf399d8fc692f3f69b6dd996
Author:        Takashi Iwai <[email protected]>
AuthorDate:    Tue, 22 Nov 2022 12:51:22 +01:00
Committer:     Borislav Petkov (AMD) <[email protected]>
CommitterDate: Mon, 02 Jan 2023 18:56:21 +01:00

x86/kexec: Fix double-free of elf header buffer

After

b3e34a47f989 ("x86/kexec: fix memory leak of elf header buffer"),

freeing image->elf_headers in the error path of crash_load_segments() is not needed because kimage_file_post_load_cleanup() will take care of that later. And not clearing it could result in a double-free.

Drop the superfluous vfree() call at the error path of crash_load_segments().

---
 arch/x86/kernel/crash.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
arch/x86/kernel/crash.c +1 -3
index 9730c88..3055144 100644
@@ -401,10 +401,8 @@ int crash_load_segments(struct kimage *image)
 	kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
 	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 	ret = kexec_add_buffer(&kbuf);
-	if (ret) {
-		vfree((void *)image->elf_headers);
+	if (ret)
 		return ret;
-	}
 	image->elf_load_addr = kbuf.mem;
 	pr_debug("Loaded ELF headers at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
 		 image->elf_load_addr, kbuf.bufsz, kbuf.memsz);