Skip to contentSkip to conversation

kexec: Fix invalid field access

PATCH Applied Breno Leitao 4 participants 9 messages Aug 27, 2025 – Sep 17, 2025

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

cover kexec: Fix invalid field access

The kexec_buf structure was previously declared without initialization. commit bf454ec31add ("kexec_file: allow to place kexec_buf randomly") added a field that is always read but not consistently populated by all architectures. This un-initialized field will contain garbage.

This is also triggering a UBSAN warning when the uninitialized data was accessed:

	------------[ cut here ]------------
	UBSAN: invalid-load in ./include/linux/kexec.h:210:10
	load of value 252 is not a valid value for type '_Bool'

Zero-initializing kexec_buf at declaration ensures all fields are cleanly set, preventing future instances of uninitialized memory being used.

An initial fix was already landed for arm64[0], and this patchset fixes the problem on the remaining arm64 code and on riscv, as raised by Mark.

Discussions about this problem could be found at[1][2].

---
Breno Leitao (3):
arm64: kexec: Initialize kexec_buf struct in load_other_segments()
riscv: kexec: Initialize kexec_buf struct
s390: kexec: Initialize kexec_buf struct

 arch/arm64/kernel/machine_kexec_file.c | 2 +-
 arch/riscv/kernel/kexec_elf.c          | 4 ++--
 arch/riscv/kernel/kexec_image.c        | 2 +-
 arch/riscv/kernel/machine_kexec_file.c | 2 +-
 arch/s390/kernel/kexec_elf.c           | 2 +-
 arch/s390/kernel/kexec_image.c         | 2 +-
 arch/s390/kernel/machine_kexec_file.c  | 6 +++---
 7 files changed, 10 insertions(+), 10 deletions(-)

---
base-commit: 3c642997252eef4449cb6b6e02af3dc22515d817
change-id: 20250827-kbuf_all-b9d55c9291eb

Best regards,

Signature · 1 line
Breno Leitao <[email protected]>
#1 lore raw

PATCH 1/3 arm64: kexec: Initialize kexec_buf struct in load_other_segments()

The kexec_buf structure was previously declared without initialization. commit bf454ec31add ("kexec_file: allow to place kexec_buf randomly") added a field that is always read but not consistently populated by all architectures. This un-initialized field will contain garbage.

This is also triggering a UBSAN warning when the uninitialized data was accessed:

	------------[ cut here ]------------
	UBSAN: invalid-load in ./include/linux/kexec.h:210:10
	load of value 252 is not a valid value for type '_Bool'

Zero-initializing kexec_buf at declaration ensures all fields are cleanly set, preventing future instances of uninitialized memory being used.

Fixes bf454ec31add ("kexec_file: allow to place kexec_buf randomly")
---
 arch/arm64/kernel/machine_kexec_file.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kernel/machine_kexec_file.c +1 -1
index af1ca875c52ce..410060ebd86df 100644
@@ -94,7 +94,7 @@ int load_other_segments(struct kimage *image,
 			char *initrd, unsigned long initrd_len,
 			char *cmdline)
 {
-	struct kexec_buf kbuf;
+	struct kexec_buf kbuf = {};
 	void *dtb = NULL;
 	unsigned long initrd_load_addr = 0, dtb_len,
 		      orig_segments = image->nr_segments;
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/3 riscv: kexec: Initialize kexec_buf struct

In reply to Breno Leitao

The kexec_buf structure was previously declared without initialization. commit bf454ec31add ("kexec_file: allow to place kexec_buf randomly") added a field that is always read but not consistently populated by all architectures. This un-initialized field will contain garbage.

This is also triggering a UBSAN warning when the uninitialized data was accessed:

	------------[ cut here ]------------
	UBSAN: invalid-load in ./include/linux/kexec.h:210:10
	load of value 252 is not a valid value for type '_Bool'

Zero-initializing kexec_buf at declaration ensures all fields are cleanly set, preventing future instances of uninitialized memory being used.

Fixes bf454ec31add ("kexec_file: allow to place kexec_buf randomly")
---
 arch/riscv/kernel/kexec_elf.c          | 4 ++--
 arch/riscv/kernel/kexec_image.c        | 2 +-
 arch/riscv/kernel/machine_kexec_file.c | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
arch/riscv/kernel/kexec_elf.c +2 -2
index 56444c7bd34eb..531d348db84d7 100644
@@ -28,7 +28,7 @@ static int riscv_kexec_elf_load(struct kimage *image, struct elfhdr *ehdr,
 	int i;
 	int ret = 0;
 	size_t size;
-	struct kexec_buf kbuf;
+	struct kexec_buf kbuf = {};
 	const struct elf_phdr *phdr;
 
 	kbuf.image = image;
@@ -66,7 +66,7 @@ static int elf_find_pbase(struct kimage *image, unsigned long kernel_len,
 {
 	int i;
 	int ret;
-	struct kexec_buf kbuf;
+	struct kexec_buf kbuf = {};
 	const struct elf_phdr *phdr;
 	unsigned long lowest_paddr = ULONG_MAX;
 	unsigned long lowest_vaddr = ULONG_MAX;
arch/riscv/kernel/kexec_image.c +1 -1
index 26a81774a78a3..8f2eb900910b1 100644
@@ -41,7 +41,7 @@ static void *image_load(struct kimage *image,
 	struct riscv_image_header *h;
 	u64 flags;
 	bool be_image, be_kernel;
-	struct kexec_buf kbuf;
+	struct kexec_buf kbuf = {};
 	int ret;
 
 	/* Check Image header */
arch/riscv/kernel/machine_kexec_file.c +1 -1
index e36104af2e247..b9eb41b0a9751 100644
@@ -261,7 +261,7 @@ int load_extra_segments(struct kimage *image, unsigned long kernel_start,
 	int ret;
 	void *fdt;
 	unsigned long initrd_pbase = 0UL;
-	struct kexec_buf kbuf;
+	struct kexec_buf kbuf = {};
 	char *modified_cmdline = NULL;
 
 	kbuf.image = image;
Signature · 1 line
2.47.3
#3 lore raw

PATCH 3/3 s390: kexec: Initialize kexec_buf struct

In reply to Breno Leitao

The kexec_buf structure was previously declared without initialization. commit bf454ec31add ("kexec_file: allow to place kexec_buf randomly") added a field that is always read but not consistently populated by all architectures. This un-initialized field will contain garbage.

This is also triggering a UBSAN warning when the uninitialized data was accessed:

	------------[ cut here ]------------
	UBSAN: invalid-load in ./include/linux/kexec.h:210:10
	load of value 252 is not a valid value for type '_Bool'

Zero-initializing kexec_buf at declaration ensures all fields are cleanly set, preventing future instances of uninitialized memory being used.

Fixes bf454ec31add ("kexec_file: allow to place kexec_buf randomly")
---
 arch/s390/kernel/kexec_elf.c          | 2 +-
 arch/s390/kernel/kexec_image.c        | 2 +-
 arch/s390/kernel/machine_kexec_file.c | 6 +++---
 3 files changed, 5 insertions(+), 5 deletions(-)
arch/s390/kernel/kexec_elf.c +1 -1
index 4d364de437992..143e34a4eca57 100644
@@ -16,7 +16,7 @@
 static int kexec_file_add_kernel_elf(struct kimage *image,
 				     struct s390_load_data *data)
 {
-	struct kexec_buf buf;
+	struct kexec_buf buf = {};
 	const Elf_Ehdr *ehdr;
 	const Elf_Phdr *phdr;
 	Elf_Addr entry;
arch/s390/kernel/kexec_image.c +1 -1
index a32ce8bea745c..9a439175723ca 100644
@@ -16,7 +16,7 @@
 static int kexec_file_add_kernel_image(struct kimage *image,
 				       struct s390_load_data *data)
 {
-	struct kexec_buf buf;
+	struct kexec_buf buf = {};
 
 	buf.image = image;
 
arch/s390/kernel/machine_kexec_file.c +3 -3
index c2bac14dd668a..a36d7311c6683 100644
@@ -129,7 +129,7 @@ static int kexec_file_update_purgatory(struct kimage *image,
 static int kexec_file_add_purgatory(struct kimage *image,
 				    struct s390_load_data *data)
 {
-	struct kexec_buf buf;
+	struct kexec_buf buf = {};
 	int ret;
 
 	buf.image = image;
@@ -152,7 +152,7 @@ static int kexec_file_add_purgatory(struct kimage *image,
 static int kexec_file_add_initrd(struct kimage *image,
 				 struct s390_load_data *data)
 {
-	struct kexec_buf buf;
+	struct kexec_buf buf = {};
 	int ret;
 
 	buf.image = image;
@@ -184,7 +184,7 @@ static int kexec_file_add_ipl_report(struct kimage *image,
 {
 	__u32 *lc_ipl_parmblock_ptr;
 	unsigned int len, ncerts;
-	struct kexec_buf buf;
+	struct kexec_buf buf = {};
 	unsigned long addr;
 	void *ptr, *end;
 	int ret;
Signature · 1 line
2.47.3
Acked-by on the v1 series
#4 lore raw
In reply to Breno Leitao
Quoted 25 lines · Breno Leitao

The kexec_buf structure was previously declared without initialization. commit bf454ec31add ("kexec_file: allow to place kexec_buf randomly") added a field that is always read but not consistently populated by all architectures. This un-initialized field will contain garbage.

This is also triggering a UBSAN warning when the uninitialized data was accessed:

	------------[ cut here ]------------
	UBSAN: invalid-load in ./include/linux/kexec.h:210:10
	load of value 252 is not a valid value for type '_Bool'

Zero-initializing kexec_buf at declaration ensures all fields are cleanly set, preventing future instances of uninitialized memory being used.

An initial fix was already landed for arm64[0], and this patchset fixes the problem on the remaining arm64 code and on riscv, as raised by Mark.

Discussions about this problem could be found at[1][2].

Quoted 6 lines

---
Breno Leitao (3):
arm64: kexec: Initialize kexec_buf struct in load_other_segments()
riscv: kexec: Initialize kexec_buf struct
s390: kexec: Initialize kexec_buf struct

Thanks for the fix, all looks good to me.

#5 lore raw
In reply to Breno Leitao

Hi Breno,

Quoted 40 lines · Breno Leitao

The kexec_buf structure was previously declared without initialization. commit bf454ec31add ("kexec_file: allow to place kexec_buf randomly") added a field that is always read but not consistently populated by all architectures. This un-initialized field will contain garbage.

This is also triggering a UBSAN warning when the uninitialized data was accessed:

	------------[ cut here ]------------
	UBSAN: invalid-load in ./include/linux/kexec.h:210:10
	load of value 252 is not a valid value for type '_Bool'

Zero-initializing kexec_buf at declaration ensures all fields are cleanly set, preventing future instances of uninitialized memory being used.

An initial fix was already landed for arm64[0], and this patchset fixes the problem on the remaining arm64 code and on riscv, as raised by Mark.

Discussions about this problem could be found at[1][2].

---
Breno Leitao (3):
arm64: kexec: Initialize kexec_buf struct in load_other_segments()
riscv: kexec: Initialize kexec_buf struct
s390: kexec: Initialize kexec_buf struct

  arch/arm64/kernel/machine_kexec_file.c | 2 +-
  arch/riscv/kernel/kexec_elf.c          | 4 ++--
  arch/riscv/kernel/kexec_image.c        | 2 +-
  arch/riscv/kernel/machine_kexec_file.c | 2 +-
  arch/s390/kernel/kexec_elf.c           | 2 +-
  arch/s390/kernel/kexec_image.c         | 2 +-
  arch/s390/kernel/machine_kexec_file.c  | 6 +++---
  7 files changed, 10 insertions(+), 10 deletions(-)
Quoted 7 lines

---
base-commit: 3c642997252eef4449cb6b6e02af3dc22515d817
change-id: 20250827-kbuf_all-b9d55c9291eb

Best regards,
--
Breno Leitao <[email protected]>

I see that the commit those patches fix is in 6.16 so we should add cc: stable.

And who should merge those patches? Should we do it on a per-arch basis?

Thanks,

Alex

Quoted 5 lines

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

#6 lore raw
Quoted 3 lines · Alexandre Ghiti

Hi Breno,

On 8/27/25 12:42, Breno Leitao wrote:

.....snip...

Quoted 5 lines

I see that the commit those patches fix is in 6.16 so we should add cc:
stable.

And who should merge those patches? Should we do it on a per-arch basis?

It's been in Andrew's akpm-mm/mm-hotfixes-unstable.

#7 lore raw

Hi Baoquan,

Quoted 10 lines · Baoquan He
Quoted 3 lines · Alexandre Ghiti

Hi Breno,

On 8/27/25 12:42, Breno Leitao wrote:

.....snip...

Quoted 4 lines

I see that the commit those patches fix is in 6.16 so we should add cc:
stable.

And who should merge those patches? Should we do it on a per-arch basis?

It's been in Andrew's akpm-mm/mm-hotfixes-unstable.

Great, thanks for letting me know.

Alex

Quoted 1 line
#8 lore raw
In reply to Breno Leitao
This series was applied to riscv/linux.git (for-next) by Paul Walmsley <[email protected]>:

Hello:

This series was applied to riscv/linux.git (for-next) by Paul Walmsley <[email protected]>:

Quoted 9 lines · On Wed, 27 Aug 2025 03:42:20 -0700 you wrote:

The kexec_buf structure was previously declared without initialization. commit bf454ec31add ("kexec_file: allow to place kexec_buf randomly") added a field that is always read but not consistently populated by all architectures. This un-initialized field will contain garbage.

This is also triggering a UBSAN warning when the uninitialized data was accessed:

[...]

Here is the summary with links:
- [1/3] arm64: kexec: Initialize kexec_buf struct in load_other_segments() (no matching commit)
- [2/3] riscv: kexec: Initialize kexec_buf struct

    https://git.kernel.org/riscv/c/95c54cd9c769

- [3/3] s390: kexec: Initialize kexec_buf struct (no matching commit)

You are awesome, thank you!

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