All of lore.kernel.org
 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 v2 3/3] of: reserved_mem: only support one <base size> entry in reg property
Date: Tue, 19 May 2026 09:24:03 +0000	[thread overview]
Message-ID: <20260519092404.0969FC2BCC9@smtp.kernel.org> (raw)
In-Reply-To: <20260519082427.4181476-4-chenwandun@lixiang.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Unconditional read of the first `reg` property entry without verifying that `len > 0` leads to an out-of-bounds FDT read for empty `reg` properties.
--

commit def0743ccda5f4a97f9b1275e989c8ea559363b4
Author: Wandun Chen <chenwandun1@gmail.com>

of: reserved_mem: only support one <base size> entry in reg property

This commit simplifies reserved memory parsing by restricting support
to a single base/size entry in the reg property. It removes the loop
in the early pass and warns if multiple entries are present,
unconditionally reading only the first entry.

Link: https://lore.kernel.org/all/20260506014752.GA280279-robh@kernel.org/

> diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c
> index 462e7c3078a3a..0e91c4a71d89d 100644
> --- a/drivers/of/of_reserved_mem.c
> +++ b/drivers/of/of_reserved_mem.c
> @@ -134,37 +134,38 @@ static int __init __reserved_mem_reserve_reg(unsigned long node,
[ ... ]
>  	prop = of_flat_dt_get_addr_size_prop(node, "reg", &len);
>  	if (!prop)
>  		return -ENOENT;
>  
> +	if (len > 1)
> +		pr_warn("Reserved memory: node '%s' has %d <base size> entries, only the first is used\n",
> +			uname, len);
> +
>  	nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
>  
>  	err = fdt_validate_reserved_mem_node(node, NULL);
>  	if (err && err != -ENODEV)
>  		return err;
>  
> -	for (i = 0; i < len; i++) {
> -		u64 b, s;
> -
> -		of_flat_dt_read_addr_size(prop, i, &b, &s);
> -
> -		base = b;
> -		size = s;
> +	of_flat_dt_read_addr_size(prop, 0, &b, &s);

If a device tree node contains an empty reg property,
of_flat_dt_get_addr_size_prop() returns a valid pointer to the empty
property data and sets len to 0.

Since the loop was removed, is there a risk of an out-of-bounds read here
because the code unconditionally calls of_flat_dt_read_addr_size()?

This could fetch garbage values for the physical base and size. If the
parsed garbage size happens to be non-zero, could the kernel attempt
to reserve unintended physical memory regions via
early_init_dt_reserve_memory()?

Would it be safer to verify len > 0 before reading the first entry?

> +	base = b;
> +	size = s;
[ ... ]
> @@ -277,20 +278,24 @@ void __init fdt_scan_reserved_mem_late(void)
>  	alloc_reserved_mem_array();
>  
>  	fdt_for_each_subnode(child, fdt, node) {
> +		const __be32 *prop;
[ ... ]
> -		if (!of_flat_dt_get_addr_size(child, "reg", &b, &s))
> +		prop = of_flat_dt_get_addr_size_prop(child, "reg", &len);
> +		if (!prop)
>  			continue;
>  
>  		ret = fdt_validate_reserved_mem_node(child, NULL);
>  		if (ret && ret != -ENODEV)
>  			continue;
>  
> +		of_flat_dt_read_addr_size(prop, 0, &b, &s);

Does this have the same out-of-bounds read potential when len == 0?

The previous of_flat_dt_get_addr_size() wrapper explicitly checked that
entries != 1, which inherently skipped empty reg properties.

>  		base = b;
>  		size = s;
>

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260519082427.4181476-1-chenwandun@lixiang.com?part=3

  reply	other threads:[~2026-05-19  9:24 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-19  8:24 [PATCH v2 0/3] of: reserved_mem: fixes and cleanups Wandun Chen
2026-05-19  8:24 ` [PATCH v2 1/3] of: reserved_mem: avoid unnecessary memory allocation when __reserved_mem_check_root() fails Wandun Chen
2026-05-19  8:46   ` sashiko-bot
2026-05-20  7:44   ` Krzysztof Kozlowski
2026-05-20  8:16     ` Wandun
2026-06-01 23:06       ` Rob Herring
2026-06-02  1:37         ` Wandun
2026-05-19  8:24 ` [PATCH v2 2/3] of: reserved_mem: clean up redundant alloc_reserved_mem_array() call Wandun Chen
2026-05-19  9:09   ` sashiko-bot
2026-05-19  8:24 ` [PATCH v2 3/3] of: reserved_mem: only support one <base size> entry in reg property Wandun Chen
2026-05-19  9:24   ` sashiko-bot [this message]
2026-05-22  7:13     ` Wandun

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=20260519092404.0969FC2BCC9@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.