Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jinjie Ruan" <ruanjinjie@huawei.com>
Cc: robh@kernel.org, conor+dt@kernel.org, devicetree@vger.kernel.org
Subject: Re: [PATCH v17 06/10] riscv: kexec_file: Use crash_prepare_headers() helper to simplify code
Date: Mon, 29 Jun 2026 10:53:58 +0000	[thread overview]
Message-ID: <20260629105358.72D6D1F00A3A@smtp.kernel.org> (raw)
In-Reply-To: <20260629094746.191843-7-ruanjinjie@huawei.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Concurrent memory hotplug during kexec file load causes an out-of-bounds array write in `prepare_elf64_ram_headers_callback` due to a TOCTOU mismatch.
--

commit a0fbb79c71ece58936999721df807081917d41c0
Author: Jinjie Ruan <ruanjinjie@huawei.com>

riscv: kexec_file: Use crash_prepare_headers() helper to simplify code

This commit refactors the crash core code to use crash_prepare_headers() to
reduce code duplication. It introduces arch_get_system_nr_ranges() to count
the max memory ranges and arch_crash_populate_cmem() to collect and fill
them into cmem.

> diff --git a/arch/riscv/kernel/machine_kexec_file.c b/arch/riscv/kernel/machine_kexec_file.c
> index 3f7766057cac3..439cbc50dfa69 100644
> --- a/arch/riscv/kernel/machine_kexec_file.c
> +++ b/arch/riscv/kernel/machine_kexec_file.c
> @@ -44,6 +44,15 @@ static int get_nr_ram_ranges_callback(struct resource *res, void *arg)
>  	return 0;
>  }
>  
> +unsigned int arch_get_system_nr_ranges(void)
> +{
> +	unsigned int nr_ranges = 2; /* For exclusion of crashkernel region */
> +
> +	walk_system_ram_res(0, -1, &nr_ranges, get_nr_ram_ranges_callback);
> +
> +	return nr_ranges;
> +}
> +
[ ... ]
> @@ -55,41 +64,9 @@ static int prepare_elf64_ram_headers_callback(struct resource *res, void *arg)
>  	return 0;
>  }
>  
> -static int prepare_elf_headers(void **addr, unsigned long *sz)
> +int arch_crash_populate_cmem(struct crash_mem *cmem)
>  {
> +	return walk_system_ram_res(0, -1, cmem, prepare_elf64_ram_headers_callback);
>  }

[Severity: High]
This isn't a bug introduced by this patch, but is there a pre-existing race 
condition with concurrent memory hotplug here?

When kexec_file_load runs, arch_get_system_nr_ranges() counts the ranges to
allocate the cmem array in crash_prepare_headers(), and then
arch_crash_populate_cmem() fills it. Since this two-pass iteration is not
synchronized against memory hot-add events, could a new memory range be
added between the two passes?

If that happens, the number of discovered ranges will exceed the capacity
allocated for cmem. Looking at prepare_elf64_ram_headers_callback(), it
unconditionally writes to the array:

	cmem->ranges[cmem->nr_ranges].start = res->start;
	cmem->ranges[cmem->nr_ranges].end = res->end;
	cmem->nr_ranges++;

Could this result in an out-of-bounds write since it doesn't check against
cmem->max_nr_ranges during the second pass?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260629094746.191843-1-ruanjinjie@huawei.com?part=6

  reply	other threads:[~2026-06-29 10:53 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-29  9:47 [PATCH v17 00/10] arm64/riscv: Add support for crashkernel CMA reservation Jinjie Ruan
2026-06-29  9:47 ` [PATCH v17 01/10] riscv: kexec_file: Fix crashk_low_res not exclude bug Jinjie Ruan
2026-06-29  9:58   ` sashiko-bot
2026-06-29  9:47 ` [PATCH v17 02/10] powerpc/crash: sort crash memory ranges before preparing elfcorehdr Jinjie Ruan
2026-06-29  9:47 ` [PATCH v17 03/10] crash: Add crash_prepare_headers() to exclude crash kernel memory Jinjie Ruan
2026-06-29 10:19   ` sashiko-bot
2026-06-29  9:47 ` [PATCH v17 04/10] arm64: kexec_file: Use crash_prepare_headers() helper to simplify code Jinjie Ruan
2026-06-29 10:31   ` sashiko-bot
2026-06-29  9:47 ` [PATCH v17 05/10] x86/crash: " Jinjie Ruan
2026-06-29 10:41   ` sashiko-bot
2026-06-29  9:47 ` [PATCH v17 06/10] riscv: kexec_file: " Jinjie Ruan
2026-06-29 10:53   ` sashiko-bot [this message]
2026-06-29 11:28   ` Guo Ren
2026-06-29  9:47 ` [PATCH v17 07/10] LoongArch: " Jinjie Ruan
2026-06-29 11:06   ` sashiko-bot
2026-06-29  9:47 ` [PATCH v17 08/10] powerpc/kexec_file: Use crash_exclude_core_ranges() helper Jinjie Ruan
2026-06-29  9:47 ` [PATCH v17 09/10] arm64: kexec_file: Add support for crashkernel CMA reservation Jinjie Ruan
2026-06-29  9:47 ` [PATCH v17 10/10] riscv: " Jinjie Ruan
2026-06-29 11:33   ` sashiko-bot
2026-06-30 15:49 ` [PATCH v17 00/10] arm64/riscv: " Mike Rapoport

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260629105358.72D6D1F00A3A@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=ruanjinjie@huawei.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox