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 v16 06/10] riscv: kexec_file: Use crash_prepare_headers() helper to simplify code
Date: Mon, 08 Jun 2026 07:54:20 +0000	[thread overview]
Message-ID: <20260608075420.8C5871F00893@smtp.kernel.org> (raw)
In-Reply-To: <20260608073459.3119290-7-ruanjinjie@huawei.com>

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

Pre-existing issues:
- [High] A Time-of-Check to Time-of-Use (TOCTOU) race condition in kexec segment preparation can cause a heap buffer overflow if memory hotplug occurs.
--

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

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

This patch simplifies the kexec_file code for RISC-V by using the newly
introduced crash_prepare_headers() helper. It replaces the architecture
specific prepare_elf_headers() with arch_get_system_nr_ranges() and
arch_crash_populate_cmem() to reduce code duplication.

> 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
[ ... ]
> @@ -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)
>  {
> -	struct crash_mem *cmem;
> -	unsigned int nr_ranges;
> -	int ret;
> -
> -	nr_ranges = 2; /* For exclusion of crashkernel region */
> -	walk_system_ram_res(0, -1, &nr_ranges, get_nr_ram_ranges_callback);
> -
> -	cmem = kmalloc_flex(*cmem, ranges, nr_ranges);
> -	if (!cmem)
> -		return -ENOMEM;
> -
> -	cmem->max_nr_ranges = nr_ranges;
> -	cmem->nr_ranges = 0;
> -	ret = walk_system_ram_res(0, -1, cmem, prepare_elf64_ram_headers_callback);
> -	if (ret)
> -		goto out;
> -
[ ... ]
> +	return walk_system_ram_res(0, -1, cmem, prepare_elf64_ram_headers_callback);
>  }

[Severity: High]
This is a pre-existing issue, but can this code suffer from a Time-of-Check
to Time-of-Use race condition during memory hotplug?

The crash_prepare_headers() helper in kernel/crash_core.c uses a lockless
two-pass sequence:

kernel/crash_core.c:crash_prepare_headers() {
    ...
    max_nr_ranges = arch_get_system_nr_ranges();
    ...
    cmem = alloc_cmem(max_nr_ranges);
    ...
    ret = arch_crash_populate_cmem(cmem);
    ...
}

Since the kexec syscall does not hold mem_hotplug_lock or use
get_online_mems(), could a concurrent memory hotplug event call add_memory()
and insert a new resource into the system RAM resource tree between the two
passes?

If that happens, the second walk_system_ram_res() pass in
arch_crash_populate_cmem() would encounter the newly inserted memory range.
This would cause it to execute prepare_elf64_ram_headers_callback() more
times than initially counted.

Because prepare_elf64_ram_headers_callback() unconditionally writes to
cmem->ranges[cmem->nr_ranges] without verifying that
cmem->nr_ranges < cmem->max_nr_ranges, would this result in an out-of-bounds
write that corrupts adjacent heap memory?

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

  reply	other threads:[~2026-06-08  7:54 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-08  7:34 [PATCH v16 00/10] arm64/riscv: Add support for crashkernel CMA reservation Jinjie Ruan
2026-06-08  7:34 ` [PATCH v16 01/10] riscv: kexec_file: Fix crashk_low_res not exclude bug Jinjie Ruan
2026-06-08  7:48   ` sashiko-bot
2026-06-08  8:05   ` Jinjie Ruan
2026-06-08  7:34 ` [PATCH v16 02/10] powerpc/crash: sort crash memory ranges before preparing elfcorehdr Jinjie Ruan
2026-06-08  7:34 ` [PATCH v16 03/10] crash: Add crash_prepare_headers() to exclude crash kernel memory Jinjie Ruan
2026-06-08  7:34 ` [PATCH v16 04/10] arm64: kexec_file: Use crash_prepare_headers() helper to simplify code Jinjie Ruan
2026-06-08  7:56   ` sashiko-bot
2026-06-08  8:07   ` Jinjie Ruan
2026-06-08  7:34 ` [PATCH v16 05/10] x86: " Jinjie Ruan
2026-06-08  7:47   ` sashiko-bot
2026-06-08  8:21   ` Jinjie Ruan
2026-06-08  7:34 ` [PATCH v16 06/10] riscv: " Jinjie Ruan
2026-06-08  7:54   ` sashiko-bot [this message]
2026-06-08  8:08   ` Jinjie Ruan
2026-06-08  7:34 ` [PATCH v16 07/10] LoongArch: " Jinjie Ruan
2026-06-08  7:52   ` sashiko-bot
2026-06-08  8:23   ` Jinjie Ruan
2026-06-08  7:34 ` [PATCH v16 08/10] powerpc/kexec_file: Use crash_exclude_core_ranges() helper Jinjie Ruan
2026-06-08  8:48   ` Breno Leitao
2026-06-08  7:34 ` [PATCH v16 09/10] arm64: kexec_file: Add support for crashkernel CMA reservation Jinjie Ruan
2026-06-08  7:34 ` [PATCH v16 10/10] riscv: " Jinjie Ruan

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=20260608075420.8C5871F00893@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