Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Wandun Chen" <chenwandun1@gmail.com>
Cc: robh@kernel.org, conor+dt@kernel.org, devicetree@vger.kernel.org
Subject: Re: [PATCH v5 7/8] crash_core: replace for_each_mem_range() with for_each_mem_region()
Date: Thu, 06 Aug 2026 10:07:21 +0000	[thread overview]
Message-ID: <20260806100721.AA4191F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260806093519.2907771-8-chenwandun1@gmail.com>

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

New issues:
- [High] Use-after-free and wild pointer iteration in `for_each_mem_region` due to concurrent memory hotplug.

Pre-existing issues:
- [High] TOCTOU race between counting and populating memblock regions leading to an out-of-bounds write.
--

--- Patch [7]: [PATCH v5 7/8] crash_core: replace for_each_mem_range() with for_each_mem_region() ---
commit 50852c55d127f622d99b3ad7a91bdd709c53a6eb
Author: Wandun Chen <chenwandun@lixiang.com>

crash_core: replace for_each_mem_range() with for_each_mem_region()

This patch replaces the use of for_each_mem_range() with
for_each_mem_region() in the weak defaults for arch_get_system_nr_ranges()
and arch_crash_populate_cmem(). It also introduces a
crash_should_skip_region() helper to explicitly filter out MEMBLOCK_NOMAP
regions.

> diff --git a/kernel/crash_core.c b/kernel/crash_core.c
> index 77285ae3ce600..fb78b7814f188 100644
> --- a/kernel/crash_core.c
> +++ b/kernel/crash_core.c
[ ... ]
> @@ -284,22 +290,25 @@ static struct crash_mem *alloc_cmem(unsigned int nr_ranges)
>  unsigned int __weak arch_get_system_nr_ranges(void)
>  {
>  	unsigned int nr_ranges = 2 + crashk_cma_cnt; /* crashk_res + crashk_low_res, +CMA splits */
> -	phys_addr_t start, end;
> -	u64 i;
> +	struct memblock_region *reg;
>  
> -	for_each_mem_range(i, &start, &end)
> +	for_each_mem_region(reg) {
[Severity: High]
Is it possible for a concurrent memory hotplug to cause a use-after-free and
unbounded iteration here?

If sys_kexec_file_load() calls this path without holding mem_hotplug_lock, a
concurrent memory hotplug (e.g., ACPI memory add) could add a new region via
memblock_add_node().

This could trigger memblock_double_array(), which frees the old
memblock.memory.regions array.

Because the for_each_mem_region(reg) macro initialized the reg pointer once,
would it now point to freed memory? Also, if the loop condition compares
against the new array's bounds, and the new array is at a higher address,
could the loop run out of bounds?

> +		if (crash_should_skip_region(reg))
> +			continue;
>  		nr_ranges++;
> +	}
>  	return nr_ranges;
>  }
>  
>  int __weak arch_crash_populate_cmem(struct crash_mem *cmem)
>  {
> -	phys_addr_t start, end;
> -	u64 i;
> +	struct memblock_region *reg;
>  
> -	for_each_mem_range(i, &start, &end) {
> -		cmem->ranges[cmem->nr_ranges].start = start;
> -		cmem->ranges[cmem->nr_ranges].end = end - 1;
> +	for_each_mem_region(reg) {
> +		if (crash_should_skip_region(reg))
> +			continue;
> +		cmem->ranges[cmem->nr_ranges].start = reg->base;
> +		cmem->ranges[cmem->nr_ranges].end = reg->base + reg->size - 1;
>  		cmem->nr_ranges++;
[Severity: High]
This is a pre-existing issue, but can this out-of-bounds write corrupt the
heap if a region is added concurrently?

crash_prepare_headers() performs two lockless passes over the regions:

	max_nr_ranges = arch_get_system_nr_ranges();
	if (!max_nr_ranges)
		return -ENOMEM;

	cmem = alloc_cmem(max_nr_ranges);
	if (!cmem)
		return -ENOMEM;

	ret = arch_crash_populate_cmem(cmem);

If memory hotplug adds a new region concurrently between these two passes,
the region count increases. Since the array assignment here lacks bounds
checking against cmem->max_nr_ranges, would the second pass iterate more
times than the allocated capacity and overwrite adjacent memory?

>  	}
>  	return 0;

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260806093519.2907771-1-chenwandun1@gmail.com?part=7

  reply	other threads:[~2026-08-06 10:07 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20260806093541eucas1p169d6fcb31de09012bdfca326867e86d6@eucas1p1.samsung.com>
2026-08-06  9:35 ` [PATCH v5 0/8] kdump: reduce vmcore size and capture time Wandun Chen
2026-08-06  9:35   ` [PATCH v5 1/8] memblock: introduce MEMBLOCK_NODUMP flag Wandun Chen
2026-08-06  9:51     ` sashiko-bot
2026-08-06 11:43       ` Wandun
2026-08-06  9:35   ` [PATCH v5 2/8] of: reserved_mem: add dumpable flag to opt-in vmcore Wandun Chen
2026-08-06  9:47     ` sashiko-bot
2026-08-06 11:48       ` Wandun
2026-08-06  9:35   ` [PATCH v5 3/8] of: reserved_mem: mark /reserved-memory entries with MEMBLOCK_NODUMP Wandun Chen
2026-08-06 10:06     ` sashiko-bot
2026-08-06  9:35   ` [PATCH v5 4/8] of: reserved_mem: mark /memreserve/ entries as MEMBLOCK_NODUMP Wandun Chen
2026-08-06  9:57     ` sashiko-bot
2026-08-06  9:35   ` [PATCH v5 5/8] riscv: build crash_mem ranges from memblock instead of resource tree Wandun Chen
2026-08-06 10:10     ` sashiko-bot
2026-08-06  9:35   ` [PATCH v5 6/8] crash_core: fold duplicated memblock arch hooks into the weak default Wandun Chen
2026-08-06  9:56     ` sashiko-bot
2026-08-06  9:35   ` [PATCH v5 7/8] crash_core: replace for_each_mem_range() with for_each_mem_region() Wandun Chen
2026-08-06 10:07     ` sashiko-bot [this message]
2026-08-06  9:35   ` [PATCH v5 8/8] crash_core: skip MEMBLOCK_NODUMP regions when building vmcore ELF header Wandun Chen
2026-08-06 10:24     ` sashiko-bot
2026-08-06 10:11   ` [PATCH v5 0/8] kdump: reduce vmcore size and capture time Marek Szyprowski

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=20260806100721.AA4191F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=chenwandun1@gmail.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=robh@kernel.org \
    --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