linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] arm64: kexec: Initialize kexec_buf struct in image_load()
@ 2025-08-26 12:08 Breno Leitao
  2025-08-26 12:54 ` Pratyush Yadav
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Breno Leitao @ 2025-08-26 12:08 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon, kexec
  Cc: linux-arm-kernel, linux-kernel, bhe, linux-arm-kernel, oxu,
	berrange, kernel-team, Andrew Morton, Breno Leitao

The kexec_buf structure was previously declared without initialization
in image_load(). This led to a UBSAN warning when the structure was
expanded and uninitialized fields were accessed [1].

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

Andrew Morton suggested that this function is only called 3x a week[2],
thus, the memset() cost is inexpressive.

Link: https://lore.kernel.org/all/oninomspajhxp4omtdapxnckxydbk2nzmrix7rggmpukpnzadw@c67o7njgdgm3/ [1]
Link: https://lore.kernel.org/all/20250825180531.94bfb86a26a43127c0a1296f@linux-foundation.org/ [2]
Suggested-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Breno Leitao <leitao@debian.org>
---
 arch/arm64/kernel/kexec_image.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/arm64/kernel/kexec_image.c b/arch/arm64/kernel/kexec_image.c
index 532d72ea42ee8..b70f4df15a1ae 100644
--- a/arch/arm64/kernel/kexec_image.c
+++ b/arch/arm64/kernel/kexec_image.c
@@ -41,7 +41,7 @@ static void *image_load(struct kimage *image,
 	struct arm64_image_header *h;
 	u64 flags, value;
 	bool be_image, be_kernel;
-	struct kexec_buf kbuf;
+	struct kexec_buf kbuf = {};
 	unsigned long text_offset, kernel_segment_number;
 	struct kexec_segment *kernel_segment;
 	int ret;

---
base-commit: 7a77c6b5ce68a71b9102760a988a4564ff6d4106
change-id: 20250826-akpm-18a57e3a39fd

Best regards,
--  
Breno Leitao <leitao@debian.org>


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] arm64: kexec: Initialize kexec_buf struct in image_load()
  2025-08-26 12:08 [PATCH] arm64: kexec: Initialize kexec_buf struct in image_load() Breno Leitao
@ 2025-08-26 12:54 ` Pratyush Yadav
  2025-08-26 14:40 ` Mark Rutland
  2025-08-27  1:07 ` Andrew Morton
  2 siblings, 0 replies; 5+ messages in thread
From: Pratyush Yadav @ 2025-08-26 12:54 UTC (permalink / raw)
  To: Breno Leitao
  Cc: Catalin Marinas, Will Deacon, kexec, linux-arm-kernel,
	linux-kernel, bhe, oxu, berrange, kernel-team, Andrew Morton

On Tue, Aug 26 2025, Breno Leitao wrote:

> The kexec_buf structure was previously declared without initialization
> in image_load(). This led to a UBSAN warning when the structure was
> expanded and uninitialized fields were accessed [1].
>
> Zero-initializing kexec_buf at declaration ensures all fields are
> cleanly set, preventing future instances of uninitialized memory being
> used.
>
> Andrew Morton suggested that this function is only called 3x a week[2],
> thus, the memset() cost is inexpressive.

Yep, and it is only 80 bytes on an already very expensive kexec load
call.

>
> Link: https://lore.kernel.org/all/oninomspajhxp4omtdapxnckxydbk2nzmrix7rggmpukpnzadw@c67o7njgdgm3/ [1]
> Link: https://lore.kernel.org/all/20250825180531.94bfb86a26a43127c0a1296f@linux-foundation.org/ [2]
> Suggested-by: Andrew Morton <akpm@linux-foundation.org>
> Signed-off-by: Breno Leitao <leitao@debian.org>

Reviewed-by: Pratyush Yadav <pratyush@kernel.org>

[...]

-- 
Regards,
Pratyush Yadav

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] arm64: kexec: Initialize kexec_buf struct in image_load()
  2025-08-26 12:08 [PATCH] arm64: kexec: Initialize kexec_buf struct in image_load() Breno Leitao
  2025-08-26 12:54 ` Pratyush Yadav
@ 2025-08-26 14:40 ` Mark Rutland
  2025-08-26 15:26   ` Breno Leitao
  2025-08-27  1:07 ` Andrew Morton
  2 siblings, 1 reply; 5+ messages in thread
From: Mark Rutland @ 2025-08-26 14:40 UTC (permalink / raw)
  To: Breno Leitao
  Cc: Catalin Marinas, Will Deacon, kexec, linux-arm-kernel,
	linux-kernel, bhe, oxu, berrange, kernel-team, Andrew Morton

On Tue, Aug 26, 2025 at 05:08:51AM -0700, Breno Leitao wrote:
> The kexec_buf structure was previously declared without initialization
> in image_load(). This led to a UBSAN warning when the structure was
> expanded and uninitialized fields were accessed [1].

Just to check my understanding, is that only a problem for new fields
(e.g. the 'random' field added in [1]), or do we have UBSAN warnigns for
any existing fields? I assume there's no problem with existing fields
today.

> Zero-initializing kexec_buf at declaration ensures all fields are
> cleanly set, preventing future instances of uninitialized memory being
> used.
> 
> Andrew Morton suggested that this function is only called 3x a week[2],
> thus, the memset() cost is inexpressive.
> 
> Link: https://lore.kernel.org/all/oninomspajhxp4omtdapxnckxydbk2nzmrix7rggmpukpnzadw@c67o7njgdgm3/ [1]
> Link: https://lore.kernel.org/all/20250825180531.94bfb86a26a43127c0a1296f@linux-foundation.org/ [2]
> Suggested-by: Andrew Morton <akpm@linux-foundation.org>
> Signed-off-by: Breno Leitao <leitao@debian.org>

This looks fine to me, but I reckon it should be added to the series
which extends kexec_buf, unless there's some reason to avoid that?

> ---
>  arch/arm64/kernel/kexec_image.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

IIUC arch/arm64/kernel/machine_kexec_file.c would need the same
treatment in load_other_segments().

If other architectures need this, it'd probably make sense to clean that
up treewide in one go. It looks like at least riscv and s390 need that
from a quick grep:

| [mark@lakrids:~/src/linux]% git grep -w 'struct kexec_buf .*;'
| arch/arm64/kernel/kexec_image.c:        struct kexec_buf kbuf;
| arch/arm64/kernel/machine_kexec_file.c: struct kexec_buf kbuf;
| arch/powerpc/include/asm/kexec.h:int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf);
| arch/powerpc/include/asm/kexec.h:                                 struct kexec_buf *kbuf);
| arch/riscv/kernel/elf_kexec.c:  struct kexec_buf kbuf;
| arch/riscv/kernel/elf_kexec.c:  struct kexec_buf kbuf;
| arch/riscv/kernel/elf_kexec.c:  struct kexec_buf kbuf;
| arch/s390/kernel/kexec_elf.c:   struct kexec_buf buf;
| arch/s390/kernel/kexec_image.c: struct kexec_buf buf;
| arch/s390/kernel/machine_kexec_file.c:  struct kexec_buf buf;
| arch/s390/kernel/machine_kexec_file.c:  struct kexec_buf buf;
| arch/s390/kernel/machine_kexec_file.c:  struct kexec_buf buf;
| include/linux/kexec.h:int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf);
| include/linux/kexec.h:extern int kexec_add_buffer(struct kexec_buf *kbuf);
| include/linux/kexec.h:int kexec_locate_mem_hole(struct kexec_buf *kbuf);
| kernel/kexec_file.c:    struct kexec_buf *kbuf = (struct kexec_buf *)arg;

Mark.

> 
> diff --git a/arch/arm64/kernel/kexec_image.c b/arch/arm64/kernel/kexec_image.c
> index 532d72ea42ee8..b70f4df15a1ae 100644
> --- a/arch/arm64/kernel/kexec_image.c
> +++ b/arch/arm64/kernel/kexec_image.c
> @@ -41,7 +41,7 @@ static void *image_load(struct kimage *image,
>  	struct arm64_image_header *h;
>  	u64 flags, value;
>  	bool be_image, be_kernel;
> -	struct kexec_buf kbuf;
> +	struct kexec_buf kbuf = {};
>  	unsigned long text_offset, kernel_segment_number;
>  	struct kexec_segment *kernel_segment;
>  	int ret;
> 
> ---
> base-commit: 7a77c6b5ce68a71b9102760a988a4564ff6d4106
> change-id: 20250826-akpm-18a57e3a39fd
> 
> Best regards,
> --  
> Breno Leitao <leitao@debian.org>
> 
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] arm64: kexec: Initialize kexec_buf struct in image_load()
  2025-08-26 14:40 ` Mark Rutland
@ 2025-08-26 15:26   ` Breno Leitao
  0 siblings, 0 replies; 5+ messages in thread
From: Breno Leitao @ 2025-08-26 15:26 UTC (permalink / raw)
  To: Mark Rutland
  Cc: Catalin Marinas, Will Deacon, kexec, linux-arm-kernel,
	linux-kernel, bhe, oxu, berrange, kernel-team, Andrew Morton

On Tue, Aug 26, 2025 at 03:40:22PM +0100, Mark Rutland wrote:
> On Tue, Aug 26, 2025 at 05:08:51AM -0700, Breno Leitao wrote:
> > The kexec_buf structure was previously declared without initialization
> > in image_load(). This led to a UBSAN warning when the structure was
> > expanded and uninitialized fields were accessed [1].
> 
> Just to check my understanding, is that only a problem for new fields
> (e.g. the 'random' field added in [1]), or do we have UBSAN warnigns for
> any existing fields? I assume there's no problem with existing fields
> today.

UBSAN is only complaning for this new field that was added to the
struct, but only populated in x86, later it is read in in common code,
causing UBSAN to complain (and even wrong code to be executed depending
on the garabe that is in the memory during kbuf instantiation.

> > Zero-initializing kexec_buf at declaration ensures all fields are
> > cleanly set, preventing future instances of uninitialized memory being
> > used.
> > 
> > Andrew Morton suggested that this function is only called 3x a week[2],
> > thus, the memset() cost is inexpressive.
> > 
> > Link: https://lore.kernel.org/all/oninomspajhxp4omtdapxnckxydbk2nzmrix7rggmpukpnzadw@c67o7njgdgm3/ [1]
> > Link: https://lore.kernel.org/all/20250825180531.94bfb86a26a43127c0a1296f@linux-foundation.org/ [2]
> > Suggested-by: Andrew Morton <akpm@linux-foundation.org>
> > Signed-off-by: Breno Leitao <leitao@debian.org>
> 
> This looks fine to me, but I reckon it should be added to the series
> which extends kexec_buf, unless there's some reason to avoid that?
> 
> > ---
> >  arch/arm64/kernel/kexec_image.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> IIUC arch/arm64/kernel/machine_kexec_file.c would need the same
> treatment in load_other_segments().
> 
> If other architectures need this, it'd probably make sense to clean that
> up treewide in one go. It looks like at least riscv and s390 need that
> from a quick grep:

Agree. Let me send a v2 addressing it on all archicterures.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] arm64: kexec: Initialize kexec_buf struct in image_load()
  2025-08-26 12:08 [PATCH] arm64: kexec: Initialize kexec_buf struct in image_load() Breno Leitao
  2025-08-26 12:54 ` Pratyush Yadav
  2025-08-26 14:40 ` Mark Rutland
@ 2025-08-27  1:07 ` Andrew Morton
  2 siblings, 0 replies; 5+ messages in thread
From: Andrew Morton @ 2025-08-27  1:07 UTC (permalink / raw)
  To: Breno Leitao
  Cc: Catalin Marinas, Will Deacon, kexec, linux-arm-kernel,
	linux-kernel, bhe, oxu, berrange, kernel-team, Mark Rutland

On Tue, 26 Aug 2025 05:08:51 -0700 Breno Leitao <leitao@debian.org> wrote:

> The kexec_buf structure was previously declared without initialization
> in image_load(). This led to a UBSAN warning when the structure was
> expanded and uninitialized fields were accessed [1].
> 
> Zero-initializing kexec_buf at declaration ensures all fields are
> cleanly set, preventing future instances of uninitialized memory being
> used.
> 
> Andrew Morton suggested that this function is only called 3x a week[2],
> thus, the memset() cost is inexpressive.
> 

Thanks, I queued this as a replacement for your eralier "kexec/arm64:
initialize the random field of kbuf to zero in the image loader".  I
added cc:stable, a mention of the UBSAN failure and I added thie Fixes:



From: Breno Leitao <leitao@debian.org>
Subject: arm64: kexec: Initialize kexec_buf struct in image_load()
Date: Tue, 26 Aug 2025 05:08:51 -0700

The kexec_buf structure was previously declared without initialization in
image_load().  This led to a UBSAN warning when the structure was expanded
and uninitialized fields were accessed [1].

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

Fixes this UBSAN warning:

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

Andrew Morton suggested that this function is only called 3x a week[2],
thus, the memset() cost is inexpensive.

Link: https://lore.kernel.org/all/oninomspajhxp4omtdapxnckxydbk2nzmrix7rggmpukpnzadw@c67o7njgdgm3/ [1]
Link: https://lore.kernel.org/all/20250825180531.94bfb86a26a43127c0a1296f@linux-foundation.org/ [2]
Link: https://lkml.kernel.org/r/20250826-akpm-v1-1-3c831f0e3799@debian.org
Fixes: bf454ec31add ("kexec_file: allow to place kexec_buf randomly")
Signed-off-by: Breno Leitao <leitao@debian.org>
Suggested-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Baoquan He <bhe@redhat.com>
Cc: Coiby Xu <coxu@redhat.com>
Cc: "Daniel P. Berrange" <berrange@redhat.com>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Dave Young <dyoung@redhat.com>
Cc: Kairui Song <ryncsn@gmail.com>
Cc: Liu Pingfan <kernelfans@gmail.com>
Cc: Milan Broz <gmazyland@gmail.com>
Cc: Ondrej Kozina <okozina@redhat.com>
Cc: Vitaly Kuznetsov <vkuznets@redhat.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
---

 arch/arm64/kernel/kexec_image.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

--- a/arch/arm64/kernel/kexec_image.c~arm64-kexec-initialize-kexec_buf-struct-in-image_load
+++ a/arch/arm64/kernel/kexec_image.c
@@ -41,7 +41,7 @@ static void *image_load(struct kimage *i
 	struct arm64_image_header *h;
 	u64 flags, value;
 	bool be_image, be_kernel;
-	struct kexec_buf kbuf;
+	struct kexec_buf kbuf = {};
 	unsigned long text_offset, kernel_segment_number;
 	struct kexec_segment *kernel_segment;
 	int ret;
_


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-08-27  1:07 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-26 12:08 [PATCH] arm64: kexec: Initialize kexec_buf struct in image_load() Breno Leitao
2025-08-26 12:54 ` Pratyush Yadav
2025-08-26 14:40 ` Mark Rutland
2025-08-26 15:26   ` Breno Leitao
2025-08-27  1:07 ` Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).