U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1 0/2] boot: fit: bound the external data size on the imagemap storage path
@ 2026-11-15 14:31 Aristo Chen
  2026-11-15 14:31 ` [PATCH v1 1/2] " Aristo Chen
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Aristo Chen @ 2026-11-15 14:31 UTC (permalink / raw)
  To: u-boot; +Cc: Tom Rini, Simon Glass, Daniel Golle, Aristo Chen

This series bounds a read that the FIT loader sizes from unsigned
metadata on the imagemap storage path. It addresses Coverity CID 661899
from the scan report in [1]. It is partial hardening rather than a
complete fix; what it leaves open is described under patch 1.

fit_image_load_storage() in boot/image-fit.c reads an external-data
sub-image from the storage device with a transfer sized from the FIT
data-size property, and fit_image_load() only checks the image hash
after that, in fit_image_select(). data-size is in the exc_prop[]
exclusion list in boot/image-fit-sig.c, so it is not covered by the
configuration signature and stays under the control of anyone able to
modify the boot medium even when FIT_SIGNATURE is enabled. For an
uncompressed image with a load address that the caller asks to have
loaded (a ramdisk or a loadable, for example, but not the kernel, which
bootm moves later) the read goes straight to that address, so an
inflated value writes past the end of the image before the hash can
reject it. This is the same problem that
commit 8f71d7170f5c ("spl: fit: bound the external data size before
reading it") dealt with in load_simple_fit() for SPL.

Patch 1 rejects a data-size larger than CONFIG_SYS_BOOTM_LEN with
-EFBIG before the read is issued, the ceiling the SPL commit uses for
loads to a memory region, and prints a message that names the option.
Two consequences of that choice are spelled out in the commit message.

The ceiling is not a tight bound on the destination. On such a direct
read, a size inflated to anything at or below it is still written to
the load address, and the hash only rejects the image after that. What
goes away is the much larger overrun, limited until now only by the
32-bit data-size and the end of the partition. Removing the write
before verification altogether would mean not reading to the load
address at all (read to scratch memory, verify, then copy); this series
keeps the direct read and matches the SPL path instead.

The ceiling is also a new limit. On this path CONFIG_SYS_BOOTM_LEN now
applies to every external-data sub-image that is read into RAM, ramdisks
and loadables included, where so far it has only been the default limit
bootm applies when it decompresses or moves the OS image. A filesystem
image that is left on storage for dm-verity is not read and is not
affected.

Patch 1 also makes CONFIG_SYS_BOOTM_LEN available when IMAGEMAP is
enabled, since an IMAGEMAP build with no boot command and no legacy
image format enabled would otherwise not define it, and mentions the
new use in the help text of the option.

Patch 2 adds a sandbox unit test to test/boot/imagemap.c. It builds a
FIT with one external-data ramdisk whose data-size is far beyond the
ceiling, loads it through a mock imagemap device with verification on,
and checks that fit_image_load() returns -EFBIG and that the mock
reader was never entered. Without patch 1 the read is attempted and the
test fails with -EIO. The test is skipped when FIT is disabled, since
IMAGEMAP does not depend on it.

No in-tree boot flow hands an imagemap device to bootm yet:
imagemap_create() is only called from the unit tests and
bootm_info_set_imagemap() has no caller, so the new limit does not
change any in-tree boot flow.

Verified on sandbox: each commit builds, and the imagemap and bootm unit
test suites pass with the series applied (15 and 7 tests). The series
also builds with FIT disabled, where the new test is reported as
skipped. With the boot commands and the legacy image formats disabled,
boot/image-fit.c compiles with the Kconfig change of patch 1 and fails
on the undefined CONFIG_SYS_BOOTM_LEN without it.

The series is based on current main and does not conflict with the
imagemap series in [2]; that series applies on top of this one, and the
two test suites pass with both applied.

Link: https://lore.kernel.org/u-boot/20261109221650.GA3741582@bill-the-cat/ [1]
Link: https://lore.kernel.org/u-boot/cover.1794614777.git.daniel@makrotopia.org/ [2]

Aristo Chen (2):
  boot: fit: bound the external data size on the imagemap storage path
  test: boot: imagemap: reject an oversized data-size

 boot/Kconfig         |   5 +-
 boot/image-fit.c     |  20 +++++++
 test/boot/imagemap.c | 133 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 157 insertions(+), 1 deletion(-)

-- 
2.43.0


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

* [PATCH v1 1/2] boot: fit: bound the external data size on the imagemap storage path
  2026-11-15 14:31 [PATCH v1 0/2] boot: fit: bound the external data size on the imagemap storage path Aristo Chen
@ 2026-11-15 14:31 ` Aristo Chen
  2026-11-15 21:39   ` Daniel Golle
  2026-11-15 14:31 ` [PATCH v1 2/2] test: boot: imagemap: reject an oversized data-size Aristo Chen
  2026-11-20 13:11 ` [PATCH v2 0/2] boot: fit: bound the external data size on the imagemap storage path Aristo Chen
  2 siblings, 1 reply; 10+ messages in thread
From: Aristo Chen @ 2026-11-15 14:31 UTC (permalink / raw)
  To: u-boot
  Cc: Tom Rini, Simon Glass, Daniel Golle, Aristo Chen,
	Frieder Schrempf, Alexey Charkov, Johan Jonker

fit_image_load_storage() loads an external-data sub-image by reading it
from the storage device with a transfer sized from the FIT data-size
property. That property is listed in exc_prop[] in boot/image-fit-sig.c,
so it is excluded from the configuration signature and stays under the
control of anyone able to modify the boot medium even when FIT_SIGNATURE
is enabled. The read happens before fit_image_select() checks the image
hash, and for an uncompressed image it goes straight to the load address,
so an inflated data-size overruns the destination before the corruption
can be detected.

Reject an image whose data-size exceeds CONFIG_SYS_BOOTM_LEN before the
read is issued, the same bound load_simple_fit() applies on the SPL path
in commit 8f71d7170f5c ("spl: fit: bound the external data size before
reading it"), and print a message that names the option so that a real
image which trips the limit can be diagnosed.

This is partial hardening, not a complete fix. CONFIG_SYS_BOOTM_LEN is a
conservative ceiling rather than a tight bound on the destination: an
inflated size at or below it is still written to the load address, and
the image hash only rejects the image after that. What the ceiling
removes is the much larger overrun that was possible until now, when the
only limits were the 32-bit data-size itself and the end of the
partition.

The ceiling is also a new limit. In full U-Boot CONFIG_SYS_BOOTM_LEN has
so far only been the default limit bootm applies when it decompresses or
moves the OS image. On the imagemap storage path it now applies to every
external-data sub-image that is read into RAM, ramdisks and loadables
included, so one larger than that can no longer be loaded this way. A
filesystem image that is left on storage for dm-verity is not read and
is not affected. Mention the new use in the help text of the option.

CONFIG_SYS_BOOTM_LEN is only offered when a boot command or a legacy
image format is enabled, none of which IMAGEMAP selects, so make it
depend on IMAGEMAP as well. Without that an IMAGEMAP build that enables
none of them would reference an undefined macro here.

Fixes: 46d32e38ee4e ("boot: fit: support on-demand loading in fit_image_load()")
Addresses-Coverity-ID: CID 661899: Insecure data handling (TAINTED_SCALAR)
Signed-off-by: Aristo Chen <aristo.chen@canonical.com>
---
 boot/Kconfig     |  5 ++++-
 boot/image-fit.c | 20 ++++++++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/boot/Kconfig b/boot/Kconfig
index 8c9ddc2ef1d..e2fe399a51b 100644
--- a/boot/Kconfig
+++ b/boot/Kconfig
@@ -1178,7 +1178,7 @@ endif # MEASURED_BOOT
 config SYS_BOOTM_LEN
 	hex "Maximum size of a decompressed OS image"
 	depends on CMD_BOOTM || CMD_BOOTI || CMD_BOOTZ || \
-		LEGACY_IMAGE_FORMAT || SPL_LEGACY_IMAGE_FORMAT
+		LEGACY_IMAGE_FORMAT || SPL_LEGACY_IMAGE_FORMAT || IMAGEMAP
 	default 0x8000000 if PPC || ARM64 || RISCV
 	default 0x1000000 if X86 || ARCH_MX6 || ARCH_MX7
 	default 0x800000
@@ -1186,6 +1186,9 @@ config SYS_BOOTM_LEN
 	  This is the maximum size of the buffer that is used to decompress the OS
 	  image in to if attempting to boot a compressed image.
 
+	  With IMAGEMAP it is also the maximum size of an external-data FIT
+	  sub-image that can be read from storage.
+
 config SUPPORT_RAW_INITRD
 	bool "Enable raw initrd images"
 	help
diff --git a/boot/image-fit.c b/boot/image-fit.c
index 75d0f3f72d8..228d5be49c2 100644
--- a/boot/image-fit.c
+++ b/boot/image-fit.c
@@ -2305,6 +2305,26 @@ static int fit_image_load_storage(struct bootm_headers *images, const void *fit,
 	if (fit_image_get_data_size(fit, noffset, &data_sz))
 		return 0;
 
+	/*
+	 * data-size is excluded from the configuration signature (it is in
+	 * exc_prop[] in boot/image-fit-sig.c), so it stays attacker controlled
+	 * even after fit_config_verify() succeeds.  The payload is read below,
+	 * for an uncompressed image straight to its load address, before
+	 * fit_image_select() checks the hash, so an inflated data-size overruns
+	 * the destination before the corruption can be detected.  Reject an
+	 * implausibly large size here, as load_simple_fit() does on the SPL
+	 * path.
+	 *
+	 * CONFIG_SYS_BOOTM_LEN is a conservative ceiling, not a tight bound on
+	 * the destination: an inflated size at or below it is still written
+	 * before the hash check rejects the image.
+	 */
+	if (data_sz > CONFIG_SYS_BOOTM_LEN) {
+		printf("FIT external data too large (%u bytes): increase CONFIG_SYS_BOOTM_LEN\n",
+		       data_sz);
+		return -EFBIG;
+	}
+
 	fit_image_get_comp(fit, noffset, &img_comp);
 
 	if (img_comp == IH_COMP_NONE && load_op != FIT_LOAD_IGNORED &&
-- 
2.43.0


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

* [PATCH v1 2/2] test: boot: imagemap: reject an oversized data-size
  2026-11-15 14:31 [PATCH v1 0/2] boot: fit: bound the external data size on the imagemap storage path Aristo Chen
  2026-11-15 14:31 ` [PATCH v1 1/2] " Aristo Chen
@ 2026-11-15 14:31 ` Aristo Chen
  2026-11-20 13:11 ` [PATCH v2 0/2] boot: fit: bound the external data size on the imagemap storage path Aristo Chen
  2 siblings, 0 replies; 10+ messages in thread
From: Aristo Chen @ 2026-11-15 14:31 UTC (permalink / raw)
  To: u-boot; +Cc: Tom Rini, Simon Glass, Daniel Golle, Aristo Chen, openwrt-devel

Build a FIT with an external-data ramdisk whose data-size is far beyond
CONFIG_SYS_BOOTM_LEN and any plausible load region, load it through the
imagemap storage path with verification on, and confirm fit_image_load()
returns -EFBIG. Count how often the mock reader is entered and assert it
is zero, so the oversized image is rejected before any read reaches the
device rather than after a failed transfer. Without the preceding bound
the read is attempted and fails with -EIO.

The test needs the FIT loader, which IMAGEMAP does not depend on, so it
is skipped when FIT is disabled.

Signed-off-by: Aristo Chen <aristo.chen@canonical.com>
---
 test/boot/imagemap.c | 133 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 133 insertions(+)

diff --git a/test/boot/imagemap.c b/test/boot/imagemap.c
index 2c6f23fb4f0..f8de8e6e357 100644
--- a/test/boot/imagemap.c
+++ b/test/boot/imagemap.c
@@ -7,6 +7,8 @@
 
 #include <dm.h>
 #include <blk.h>
+#include <bootstage.h>
+#include <image.h>
 #include <imagemap.h>
 #include <mtd.h>
 #include <part.h>
@@ -21,6 +23,7 @@
 #include <dm/lists.h>
 #include <dm/root.h>
 #include <dm/uclass-internal.h>
+#include <linux/libfdt.h>
 #include <test/test.h>
 #include <test/ut.h>
 
@@ -35,6 +38,7 @@
  *
  * @image:	pointer to synthetic image data in RAM
  * @image_size:	size of the synthetic image
+ * @attempts:	number of times .read() was entered, including rejected ones
  * @read_count:	number of times .read() was called
  * @last_off:	offset from the most recent .read() call
  * @last_size:	size from the most recent .read() call
@@ -42,6 +46,7 @@
 struct mock_priv {
 	const void *image;
 	size_t image_size;
+	int attempts;
 	int read_count;
 	loff_t last_off;
 	ulong last_size;
@@ -52,6 +57,8 @@ static ulong mock_reader(struct spl_load_info *info, ulong sector,
 {
 	struct mock_priv *p = info->priv;
 
+	p->attempts++;
+
 	if (sector + count > p->image_size)
 		return 0;
 
@@ -611,6 +618,132 @@ static int imagemap_test_blocklen(struct unit_test_state *uts)
 
 IMAGEMAP_TEST(imagemap_test_blocklen, UTF_DM);
 
+/*
+ * build_fit_ext_datasz() - build a FIT with one external-data ramdisk image
+ *
+ * The ramdisk uses external data (data-offset 0, so the payload would start at
+ * the aligned end of the FDT) and declares @data_size for it. No hash is
+ * needed: the bound check under test runs before any verification.
+ *
+ * @buf:	output buffer for the FIT
+ * @bufsz:	size of @buf
+ * @load:	sub-image load address
+ * @data_size:	the data-size property value
+ * Return: 0 on success, negative FDT error otherwise
+ */
+static int build_fit_ext_datasz(void *buf, size_t bufsz, u32 load,
+				u32 data_size)
+{
+	int images, img, confs, conf, ret;
+	fdt32_t v;
+
+	ret = fdt_create_empty_tree(buf, bufsz);
+	if (ret)
+		return ret;
+
+	/* fit_check_format() requires description and timestamp. */
+	ret = fdt_setprop_string(buf, 0, FIT_DESC_PROP, "data-size bound");
+	if (ret)
+		return ret;
+	v = cpu_to_fdt32(0);
+	ret = fdt_setprop(buf, 0, FIT_TIMESTAMP_PROP, &v, sizeof(v));
+	if (ret)
+		return ret;
+
+	images = fdt_add_subnode(buf, 0, "images");
+	if (images < 0)
+		return images;
+	img = fdt_add_subnode(buf, images, "ramdisk");
+	if (img < 0)
+		return img;
+	ret = fdt_setprop_string(buf, img, FIT_TYPE_PROP, "ramdisk");
+	if (ret)
+		return ret;
+	ret = fdt_setprop_string(buf, img, FIT_ARCH_PROP, "sandbox");
+	if (ret)
+		return ret;
+	ret = fdt_setprop_string(buf, img, FIT_OS_PROP, "linux");
+	if (ret)
+		return ret;
+	ret = fdt_setprop_string(buf, img, FIT_COMP_PROP, "none");
+	if (ret)
+		return ret;
+	v = cpu_to_fdt32(load);
+	ret = fdt_setprop(buf, img, FIT_LOAD_PROP, &v, sizeof(v));
+	if (ret)
+		return ret;
+	v = cpu_to_fdt32(0);
+	ret = fdt_setprop(buf, img, FIT_DATA_OFFSET_PROP, &v, sizeof(v));
+	if (ret)
+		return ret;
+	v = cpu_to_fdt32(data_size);
+	ret = fdt_setprop(buf, img, FIT_DATA_SIZE_PROP, &v, sizeof(v));
+	if (ret)
+		return ret;
+
+	confs = fdt_add_subnode(buf, 0, "configurations");
+	if (confs < 0)
+		return confs;
+	ret = fdt_setprop_string(buf, confs, FIT_DEFAULT_PROP, "conf-1");
+	if (ret)
+		return ret;
+	conf = fdt_add_subnode(buf, confs, "conf-1");
+	if (conf < 0)
+		return conf;
+	ret = fdt_setprop_string(buf, conf, FIT_RAMDISK_PROP, "ramdisk");
+	if (ret)
+		return ret;
+
+	return fdt_pack(buf);
+}
+
+/*
+ * A FIT's data-size is not covered by the configuration signature, so it is
+ * untrusted input. On the imagemap storage path the payload is read to the
+ * load address before fit_image_select() verifies it, so fit_image_load()
+ * must reject a data-size larger than CONFIG_SYS_BOOTM_LEN rather than read
+ * that many bytes off the device.
+ */
+static int imagemap_test_fit_oversize_datasz(struct unit_test_state *uts)
+{
+	struct udevice *dev, *saved_im;
+	u8 fitbuf[1024];
+	ulong load = 0x02000000, data = 0, len = 0;
+	int saved_verify, ret, attempts;
+
+	/* fit_image_load() is only built with FIT, which IMAGEMAP does not need */
+	if (!CONFIG_IS_ENABLED(FIT))
+		return -EAGAIN;
+
+	/* data-size far beyond CONFIG_SYS_BOOTM_LEN and any load region. */
+	ut_assertok(build_fit_ext_datasz(fitbuf, sizeof(fitbuf), load,
+					 0x40000000));
+	ut_assertok(create_mock_loader(fitbuf, sizeof(fitbuf), &dev));
+
+	saved_im = bootm_imagemap(&images);
+	saved_verify = images.verify;
+	bootm_set_imagemap(&images, dev);
+	images.verify = 1;
+	ret = fit_image_load(&images, map_to_sysmem(fitbuf), NULL, NULL,
+			     IH_ARCH_SANDBOX, IH_TYPE_RAMDISK,
+			     BOOTSTAGE_ID_FIT_RD_START,
+			     FIT_LOAD_OPTIONAL_NON_ZERO, &data, &len);
+	bootm_set_imagemap(&images, saved_im);
+	images.verify = saved_verify;
+
+	attempts = get_mock_priv(dev)->attempts;
+	imagemap_cleanup(dev);
+
+	/* Rejected before the device read, not after a hash failure. */
+	ut_asserteq(-EFBIG, ret);
+	/* and the reject happened before any read reached the device. */
+	ut_asserteq(0, attempts);
+
+	return 0;
+}
+
+IMAGEMAP_TEST(imagemap_test_fit_oversize_datasz, UTF_DM);
+
 #if CONFIG_IS_ENABLED(MTD_BLOCK)
 /*
  * End-to-end: read a known pattern back from a bare (non-NAND) MTD
-- 
2.43.0


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

* Re: [PATCH v1 1/2] boot: fit: bound the external data size on the imagemap storage path
  2026-11-15 14:31 ` [PATCH v1 1/2] " Aristo Chen
@ 2026-11-15 21:39   ` Daniel Golle
  2026-11-17  9:53     ` Aristo Chen
  0 siblings, 1 reply; 10+ messages in thread
From: Daniel Golle @ 2026-11-15 21:39 UTC (permalink / raw)
  To: Aristo Chen
  Cc: u-boot, Tom Rini, Simon Glass, Frieder Schrempf, Alexey Charkov,
	Johan Jonker

Hi Aristo,

On Sun, Nov 15, 2026 at 02:31:13PM +0000, Aristo Chen wrote:
> fit_image_load_storage() loads an external-data sub-image by reading it
> from the storage device with a transfer sized from the FIT data-size
> property. That property is listed in exc_prop[] in boot/image-fit-sig.c,
> so it is excluded from the configuration signature and stays under the
> control of anyone able to modify the boot medium even when FIT_SIGNATURE
> is enabled. The read happens before fit_image_select() checks the image
> hash, and for an uncompressed image it goes straight to the load address,
> so an inflated data-size overruns the destination before the corruption
> can be detected.

Agreed, that is a real hole in the zero-copy path and it makes sense
to close it.

> Reject an image whose data-size exceeds CONFIG_SYS_BOOTM_LEN before the
> read is issued, the same bound load_simple_fit() applies on the SPL path
> in commit 8f71d7170f5c ("spl: fit: bound the external data size before
> reading it"), and print a message that names the option so that a real
> image which trips the limit can be diagnosed.

I would rather not use CONFIG_SYS_BOOTM_LEN here, for two reasons.

First, only the imagemap_map_to() branch can overrun anything. In the
other branch, imagemap_map() allocates the buffer from LMB at the
requested size and reads into that, so an inflated data-size simply
fails the allocation. Without your patch, a ramdisk without a load
address and with a data-size of 1 GiB gives

  imagemap: LMB alloc failed (0x40000000 bytes)

and -ENOMEM, and the reader is never called. On that branch the check
adds a size limit but no protection.

Second, the limit has nothing to do with the destination. On most
32-bit ARM and on MIPS, SYS_BOOTM_LEN defaults to 8 MiB, so a ramdisk
or loadable larger than that could no longer be loaded through imagemap
there, while on arm64 an inflated size of up to 128 MiB is still written
before the hash check.

What the direct read needs is to stay within the free memory at the
load address, and LMB already knows where that ends. IMAGEMAP depends
on LMB, so something like this in the direct-read branch would do it:

	if (img_comp == IH_COMP_NONE && load_op != FIT_LOAD_IGNORED &&
	    !fit_image_get_load(fit, noffset, &img_load)) {
		if (data_sz > lmb_get_free_size(img_load)) {
			printf("FIT external data (%u bytes) does not fit at 0x%08lx\n",
			       data_sz, img_load);
			return -EFBIG;
		}
		dst = map_sysmem(img_load, data_sz);
		...

lmb_get_free_size() returns the number of free bytes from an address up
to the next reserved region or the end of RAM. That keeps the write away
from U-Boot itself (code, stack, heap and the relocated FDT), from
memreserve and reserved-memory regions, and from anything imagemap_map()
has already put in LMB, such as the kernel. It does not limit how large
an image may be, and the Kconfig change is no longer needed. I tried it
on sandbox: 2/2 passes unmodified, as do the rest of the imagemap and
bootm suites, and a valid ramdisk with a load address is still read
straight to its destination.

Like your version, it is not a complete fix: free memory past the real
end of the image can still be written before the hash check. If we want
complete protection two more things would need a look:

 - Destinations written by imagemap_map_to() are not reserved in LMB,
   so a sub-image read directly earlier (an FDT, say) is not protected
   from a later one. Reserving the range after a successful read would
   cover that.

 - We need to be sure that a legitimate load address cannot already be
   reserved at that point, for instance after an earlier boot attempt
   failed.

Separately from this patch, imagemap_map() should not depend on a check
in image-fit.c: on 32-bit, a data-size close to 4 GiB makes its size
arithmetic wrap, which your check currently happens to prevent. I will
send a fix for that in imagemap_map() itself. If Coverity still flags
the imagemap_map() branch once the direct read is bounded, I would
rather deal with it there.

> CONFIG_SYS_BOOTM_LEN is only offered when a boot command or a legacy
> image format is enabled, none of which IMAGEMAP selects, so make it
> depend on IMAGEMAP as well. Without that an IMAGEMAP build that enables
> none of them would reference an undefined macro here.

True, but with a bound from LMB this hunk can go.


Cheers


Daniel

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

* Re: [PATCH v1 1/2] boot: fit: bound the external data size on the imagemap storage path
  2026-11-15 21:39   ` Daniel Golle
@ 2026-11-17  9:53     ` Aristo Chen
  0 siblings, 0 replies; 10+ messages in thread
From: Aristo Chen @ 2026-11-17  9:53 UTC (permalink / raw)
  To: Daniel Golle
  Cc: u-boot, Tom Rini, Simon Glass, Frieder Schrempf, Alexey Charkov,
	Johan Jonker

Hi Daniel,

On Mon, Nov 16, 2026 at 5:39 AM Daniel Golle <daniel@makrotopia.org> wrote:
>
> Hi Aristo,
>
> On Sun, Nov 15, 2026 at 02:31:13PM +0000, Aristo Chen wrote:
> > fit_image_load_storage() loads an external-data sub-image by reading it
> > from the storage device with a transfer sized from the FIT data-size
> > property. That property is listed in exc_prop[] in boot/image-fit-sig.c,
> > so it is excluded from the configuration signature and stays under the
> > control of anyone able to modify the boot medium even when FIT_SIGNATURE
> > is enabled. The read happens before fit_image_select() checks the image
> > hash, and for an uncompressed image it goes straight to the load address,
> > so an inflated data-size overruns the destination before the corruption
> > can be detected.
>
> Agreed, that is a real hole in the zero-copy path and it makes sense
> to close it.
>
> > Reject an image whose data-size exceeds CONFIG_SYS_BOOTM_LEN before the
> > read is issued, the same bound load_simple_fit() applies on the SPL path
> > in commit 8f71d7170f5c ("spl: fit: bound the external data size before
> > reading it"), and print a message that names the option so that a real
> > image which trips the limit can be diagnosed.
>
> I would rather not use CONFIG_SYS_BOOTM_LEN here, for two reasons.
>
> First, only the imagemap_map_to() branch can overrun anything. In the
> other branch, imagemap_map() allocates the buffer from LMB at the
> requested size and reads into that, so an inflated data-size simply
> fails the allocation. Without your patch, a ramdisk without a load
> address and with a data-size of 1 GiB gives
>
>   imagemap: LMB alloc failed (0x40000000 bytes)
>
> and -ENOMEM, and the reader is never called. On that branch the check
> adds a size limit but no protection.
>
> Second, the limit has nothing to do with the destination. On most
> 32-bit ARM and on MIPS, SYS_BOOTM_LEN defaults to 8 MiB, so a ramdisk
> or loadable larger than that could no longer be loaded through imagemap
> there, while on arm64 an inflated size of up to 128 MiB is still written
> before the hash check.
>
> What the direct read needs is to stay within the free memory at the
> load address, and LMB already knows where that ends. IMAGEMAP depends
> on LMB, so something like this in the direct-read branch would do it:
>
>         if (img_comp == IH_COMP_NONE && load_op != FIT_LOAD_IGNORED &&
>             !fit_image_get_load(fit, noffset, &img_load)) {
>                 if (data_sz > lmb_get_free_size(img_load)) {

Thanks for the review. Agreed on both points: a bound taken from the
destination is the better one, and only the direct read needs it.

I tried your check on sandbox: 2/2 passes unmodified, the imagemap and
bootm suites pass, and a valid ramdisk is still read straight to its
load address. It also stops a case that the v1 ceiling lets through:
8 KiB whose end lies inside a no-overwrite LMB reservation is far below
CONFIG_SYS_BOOTM_LEN.

So v2 will have your check in the direct-read branch, no Kconfig change,
and your Suggested-by unless you object. It stays partial hardening, as
you say: free memory behind the real payload can still be written before
the hash check, and direct-read destinations are still not reserved. The
commit message will say so. I would also add a test that a ramdisk with
a load address is still read straight to it, since the check can refuse
a valid image.

While testing I found three limits of lmb_get_free_size(). I would
document them in the commit message rather than solve them in v2.
Would you prefer a different scope for v2?

>                         printf("FIT external data (%u bytes) does not fit at 0x%08lx\n",
>                                data_sz, img_load);
>                         return -EFBIG;
>                 }
>                 dst = map_sysmem(img_load, data_sz);
>                 ...
>
> lmb_get_free_size() returns the number of free bytes from an address up
> to the next reserved region or the end of RAM. That keeps the write away
> from U-Boot itself (code, stack, heap and the relocated FDT), from
> memreserve and reserved-memory regions, and from anything imagemap_map()
> has already put in LMB, such as the kernel. It does not limit how large
> an image may be, and the Kconfig change is no longer needed. I tried it
> on sandbox: 2/2 passes unmodified, as do the rest of the imagemap and
> bootm suites, and a valid ramdisk with a load address is still read
> straight to its destination.
>
> Like your version, it is not a complete fix: free memory past the real
> end of the image can still be written before the hash check. If we want
> complete protection two more things would need a look:
>
>  - Destinations written by imagemap_map_to() are not reserved in LMB,
>    so a sub-image read directly earlier (an FDT, say) is not protected
>    from a later one. Reserving the range after a successful read would
>    cover that.
>
>  - We need to be sure that a legitimate load address cannot already be
>    reserved at that point, for instance after an earlier boot attempt
>    failed.

1. Reused addresses.
The helper stops at every used region, LMB_NONE ones included, and
returns 0 from inside one (item 3 below is the exception).
fs_read_lmb_check() ("load"), lmb_read_check() (tftp, wget, "sf read")
and bootm (in-place ramdisk, in-place FDT, the OS region) all create
such regions, and there is no general rollback when an attempt fails.
So a valid FIT is refused when the load address of a sub-image was used
by an earlier load in the same session, successful or not. For example,
with in-place initrd, an attempt that gets as far as reserving a
directly loaded ramdisk and then fails leaves that address unavailable
to the next attempt. On sandbox, a valid 64-byte ramdisk whose load
address lies inside an LMB_NONE reservation, made with the call
fs_read_lmb_check() uses, gets -EFBIG.

tftp and wget stopped using the helper for this reason, in
commit af45c84871e4 ("tftp: rework the logic to validate the load
address") and commit 51ebd514ec78 ("wget: rework the logic to validate
the load address"): "This call fails if the same region of memory has
been used earlier." Only its unit test calls it today. It also means
that reserving direct-read destinations, your first point, needs
lifetime handling: a reservation that is not cleared makes the next
check refuse that destination.

2. More than one RAM bank. The span it returns can run across the hole
   to the next bank: with two banks of 256 MiB at 0x40000000 and
   0x60000000 and nothing reserved, it returns 0x28000000 for
   0x48000000.

3. With a 32-bit phys_addr_t, a reservation that ends at 4 GiB is not
   seen from inside it: with RAM from 0xc0000000 to 4 GiB and the last
   16 MiB reserved, it returns 0x800000 for 0xff800000. From below the
   reservation the result is correct, so this only matters for a load
   address that is itself reserved.

I looked at lmb_read_check() as a replacement, since tftp and wget use
it now. It accepts a reused address and keeps the range inside one bank.
But on sandbox_defconfig, where phys_addr_t is 32 bits, it accepts
0xff000000 bytes at 0x02000000 because the end address wraps; it lets a
direct read overlap LMB_NONE regions such as the scratch buffers of
imagemap_map(); and it leaves a reservation behind. So I would stay with
your comparison.

>
> Separately from this patch, imagemap_map() should not depend on a check
> in image-fit.c: on 32-bit, a data-size close to 4 GiB makes its size
> arithmetic wrap, which your check currently happens to prevent. I will
> send a fix for that in imagemap_map() itself. If Coverity still flags
> the imagemap_map() branch once the direct read is bounded, I would
> rather deal with it there.
>
> > CONFIG_SYS_BOOTM_LEN is only offered when a boot command or a legacy
> > image format is enabled, none of which IMAGEMAP selects, so make it
> > depend on IMAGEMAP as well. Without that an IMAGEMAP build that enables
> > none of them would reference an undefined macro here.
>
> True, but with a bound from LMB this hunk can go.

I will drop that hunk in v2.

>
>
> Cheers
>
>
> Daniel

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

* [PATCH v2 0/2] boot: fit: bound the external data size on the imagemap storage path
  2026-11-15 14:31 [PATCH v1 0/2] boot: fit: bound the external data size on the imagemap storage path Aristo Chen
  2026-11-15 14:31 ` [PATCH v1 1/2] " Aristo Chen
  2026-11-15 14:31 ` [PATCH v1 2/2] test: boot: imagemap: reject an oversized data-size Aristo Chen
@ 2026-11-20 13:11 ` Aristo Chen
  2026-11-20 13:11   ` [PATCH v2 1/2] " Aristo Chen
  2026-11-20 13:11   ` [PATCH v2 2/2] test: boot: imagemap: check the data-size bound on a direct read Aristo Chen
  2 siblings, 2 replies; 10+ messages in thread
From: Aristo Chen @ 2026-11-20 13:11 UTC (permalink / raw)
  To: u-boot; +Cc: Tom Rini, Daniel Golle, Aristo Chen

This series bounds a read that the FIT loader sizes from unsigned
metadata on the imagemap storage path. It addresses Coverity CID 661899
from the scan report in [1]. It is partial hardening rather than a
complete fix; what it leaves open is described under patch 1.

fit_image_load_storage() in boot/image-fit.c reads an external-data
sub-image from the storage device with a transfer sized from the FIT
data-size property, and fit_image_load() only checks the image hash
after that, in fit_image_select(). data-size is in the exc_prop[]
exclusion list in boot/image-fit-sig.c, so it is not covered by the
configuration signature and stays under the control of anyone able to
modify the boot medium even when FIT_SIGNATURE is enabled. For an
uncompressed image with a load address that the caller asks to have
loaded (a ramdisk or a loadable, for example, but not the kernel, which
bootm moves later) the read goes straight to that address, so an
inflated value writes past the end of the image before the hash can
reject it.

Patch 1 checks the size before such a direct read: an image whose
data-size is larger than the memory that LMB reports as free at the
load address is rejected with -EFBIG and a message. This is the check
that Daniel proposed in his review of v1 [2]. It replaces the
CONFIG_SYS_BOOTM_LEN ceiling of v1, which had nothing to do with the
destination and limited the size of every sub-image on this path. The
other branch, where imagemap_map() allocates the buffer from LMB at the
requested size, is no longer touched, and neither is Kconfig.

The commit message of patch 1 spells out what stays open:

- Free memory behind the real payload can still be written before the
  hash check rejects the image.
- The destination of a direct read is not reserved in LMB, so a
  sub-image that was read directly is not protected from a later one.
- lmb_get_free_size() stops at every used region. A valid image is
  refused when the load address of a sub-image is still recorded as
  used after an earlier load in the same session.
- The helper has two more edge cases, with more than one RAM bank and
  with a reservation that ends at 4 GiB on a 32-bit phys_addr_t.

The numbers behind the last two points are in [3].

Patch 2 adds two sandbox unit tests to test/boot/imagemap.c. Both build
a FIT with one external-data ramdisk that has a load address and load it
through a mock imagemap device with verification on. In the first the
data-size is 1 GiB; it checks that fit_image_load() returns -EFBIG and
that the mock reader was never entered. Without patch 1 the read is
attempted and the test fails with -EIO. The second checks that a valid
ramdisk is still read straight to its load address. The tests are
skipped when FIT is disabled, since IMAGEMAP does not depend on it.

No in-tree boot flow hands an imagemap device to bootm yet:
imagemap_create() is only called from the unit tests and
bootm_info_set_imagemap() has no caller, so the new check does not
change any in-tree boot flow.

This version no longer covers one case that v1 happened to reject: a
data-size close to 4 GiB makes the size arithmetic in imagemap_map()
wrap when ulong or phys_size_t is 32 bits wide. For that case the
series leaves the behaviour of current main unchanged. Daniel has said
in [2] that he will fix it in imagemap_map() itself.

Verified on sandbox: each commit builds, and the imagemap and bootm unit
test suites pass with the series applied (16 and 7 tests). The series
also builds with FIT disabled, where the new tests are reported as
skipped, and boot/image-fit.c compiles with the boot commands and the
legacy image formats disabled, the configuration that needed the
Kconfig change in v1.

The series is based on current main and does not conflict with the
imagemap series in [4]: the two apply in either order, and the test
suites pass with both applied.

Link: https://lore.kernel.org/u-boot/20261109221650.GA3741582@bill-the-cat/ [1]
Link: https://lore.kernel.org/u-boot/20261115205059.62cd55fdb132@makrotopia.org/ [2]
Link: https://lore.kernel.org/u-boot/CAA=k7=keU0TbGea8vV7Ht9ziL50YZ=tufUrnXDcQOYahJ7tV3Q@mail.gmail.com/ [3]
Link: https://lore.kernel.org/u-boot/cover.1794614777.git.daniel@makrotopia.org/ [4]

Link to v1:
https://lore.kernel.org/r/20261115143134.25791-1-aristo.chen@canonical.com

Changes in v2:
- Patch 1: bound only the direct read, by the free memory that LMB
  reports at the load address, instead of bounding every read by
  CONFIG_SYS_BOOTM_LEN; no Kconfig change any more (Daniel)
- Patch 1: describe the limits of lmb_get_free_size() in the commit
  message
- Patch 2: add a test that a valid ramdisk with a load address is still
  read straight to it; the comments of the existing test no longer
  refer to CONFIG_SYS_BOOTM_LEN; new subject
- Rebased on current main

Aristo Chen (2):
  boot: fit: bound the external data size on the imagemap storage path
  test: boot: imagemap: check the data-size bound on a direct read

 boot/image-fit.c     |  17 ++++
 test/boot/imagemap.c | 205 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 222 insertions(+)

-- 
2.43.0


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

* [PATCH v2 1/2] boot: fit: bound the external data size on the imagemap storage path
  2026-11-20 13:11 ` [PATCH v2 0/2] boot: fit: bound the external data size on the imagemap storage path Aristo Chen
@ 2026-11-20 13:11   ` Aristo Chen
  2026-11-29  2:03     ` Daniel Golle
  2026-11-20 13:11   ` [PATCH v2 2/2] test: boot: imagemap: check the data-size bound on a direct read Aristo Chen
  1 sibling, 1 reply; 10+ messages in thread
From: Aristo Chen @ 2026-11-20 13:11 UTC (permalink / raw)
  To: u-boot; +Cc: Tom Rini, Daniel Golle, Aristo Chen

fit_image_load_storage() loads an external-data sub-image by reading it
from the storage device with a transfer sized from the FIT data-size
property. That property is listed in exc_prop[] in boot/image-fit-sig.c,
so it is excluded from the configuration signature and stays under the
control of anyone able to modify the boot medium even when FIT_SIGNATURE
is enabled. The read happens before fit_image_select() checks the image
hash, and for an uncompressed image with a load address it goes straight
to that address, so an inflated data-size overruns the destination
before the corruption can be detected.

The other branch is not changed. There imagemap_map() reads into a
buffer that it allocates from LMB at the requested size. Its size
arithmetic can wrap for a data-size close to 4 GiB when ulong or
phys_size_t is 32 bits wide; that needs a fix in imagemap_map() itself
and is not part of this patch.

Before the direct read, reject an image whose data-size is larger than
the memory that lmb_get_free_size() reports as free at the load address,
and print a message that gives both. Within the limits of that helper,
which are listed below, this keeps the read away from U-Boot itself,
from reserved memory and from the buffers imagemap_map() has allocated.
It does not limit how large an image may be.

This is partial hardening, not a complete fix. Free memory behind the
real payload can still be written before the hash check rejects the
image, and the destination of a direct read is not reserved in LMB, so
a sub-image that was read directly is not protected from a later one.

lmb_get_free_size() has three limits that are left as they are:

- It stops at every used region and returns 0 from inside one. That
  includes the LMB_NONE regions that the load commands, tftp, wget,
  "sf read" and bootm leave behind, so a valid image is refused when
  the load address of a sub-image is still recorded as used after an
  earlier load in the same session. tftp and wget stopped using the
  helper for that reason, in commit af45c84871e4 ("tftp: rework the
  logic to validate the load address") and commit 51ebd514ec78 ("wget:
  rework the logic to validate the load address").
- With more than one RAM bank, the size it returns can run across the
  hole to the next bank.
- With a 32-bit phys_addr_t, it does not see a reservation that ends at
  4 GiB from an address inside that reservation.

Fixes: 46d32e38ee4e ("boot: fit: support on-demand loading in fit_image_load()")
Addresses-Coverity-ID: CID 661899: Insecure data handling (TAINTED_SCALAR)
Suggested-by: Daniel Golle <daniel@makrotopia.org>
Signed-off-by: Aristo Chen <aristo.chen@canonical.com>
---
v2: bound only the direct read, by the free memory that LMB reports at
the load address, as suggested by Daniel. The CONFIG_SYS_BOOTM_LEN
ceiling and the Kconfig change are gone. Commit message rewritten; it
now lists the limits of lmb_get_free_size().

 boot/image-fit.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/boot/image-fit.c b/boot/image-fit.c
index 75d0f3f72d8..bb4bbdb04c8 100644
--- a/boot/image-fit.c
+++ b/boot/image-fit.c
@@ -2309,6 +2309,23 @@ static int fit_image_load_storage(struct bootm_headers *images, const void *fit,
 
 	if (img_comp == IH_COMP_NONE && load_op != FIT_LOAD_IGNORED &&
 	    !fit_image_get_load(fit, noffset, &img_load)) {
+		/*
+		 * data-size is excluded from the configuration signature (it
+		 * is in exc_prop[] in boot/image-fit-sig.c), so it stays
+		 * attacker controlled even after fit_config_verify() succeeds,
+		 * and this read goes to the load address before
+		 * fit_image_select() checks the hash.  Keep it within the
+		 * memory that LMB reports as free at that address.
+		 *
+		 * This is not a tight bound: free memory behind the real
+		 * payload can still be written before the hash check rejects
+		 * the image.
+		 */
+		if (data_sz > lmb_get_free_size(img_load)) {
+			printf("FIT external data (%u bytes) does not fit at 0x%08lx\n",
+			       data_sz, img_load);
+			return -EFBIG;
+		}
 		dst = map_sysmem(img_load, data_sz);
 
 		mapped = imagemap_map_to(imagemap, data_off, data_sz,
-- 
2.43.0


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

* [PATCH v2 2/2] test: boot: imagemap: check the data-size bound on a direct read
  2026-11-20 13:11 ` [PATCH v2 0/2] boot: fit: bound the external data size on the imagemap storage path Aristo Chen
  2026-11-20 13:11   ` [PATCH v2 1/2] " Aristo Chen
@ 2026-11-20 13:11   ` Aristo Chen
  2026-11-29  2:05     ` Daniel Golle
  1 sibling, 1 reply; 10+ messages in thread
From: Aristo Chen @ 2026-11-20 13:11 UTC (permalink / raw)
  To: u-boot; +Cc: Tom Rini, Daniel Golle, Aristo Chen, openwrt-devel

Build a FIT with an external-data ramdisk that has a load address and
load it through the imagemap storage path with verification on.

The first test declares a data-size of 1 GiB, far more than the free
memory at the load address, and confirms that fit_image_load() returns
-EFBIG. It counts how often the mock reader is entered and asserts that
this is zero, so the oversized image is rejected before any read reaches
the device rather than after a failed transfer. Without the preceding
bound the read is attempted and fails with -EIO.

The second test makes sure that the bound does not get in the way of a
valid image: a ramdisk whose data-size matches its payload is still read
straight to its load address. The bound refuses a load address inside a
used LMB region, so the test takes an address that LMB reports as free.

The tests need the FIT loader, which IMAGEMAP does not depend on, so
they are skipped when FIT is disabled.

Signed-off-by: Aristo Chen <aristo.chen@canonical.com>
---
v2: add imagemap_test_fit_direct_read(), which checks that a valid
ramdisk with a load address is still read straight to it. The comments
of imagemap_test_fit_oversize_datasz() no longer refer to
CONFIG_SYS_BOOTM_LEN; the test itself is unchanged. New subject, was
"test: boot: imagemap: reject an oversized data-size".

 test/boot/imagemap.c | 205 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 205 insertions(+)

diff --git a/test/boot/imagemap.c b/test/boot/imagemap.c
index 2c6f23fb4f0..8546794ecfc 100644
--- a/test/boot/imagemap.c
+++ b/test/boot/imagemap.c
@@ -7,6 +7,8 @@
 
 #include <dm.h>
 #include <blk.h>
+#include <bootstage.h>
+#include <image.h>
 #include <imagemap.h>
 #include <mtd.h>
 #include <part.h>
@@ -21,6 +23,7 @@
 #include <dm/lists.h>
 #include <dm/root.h>
 #include <dm/uclass-internal.h>
+#include <linux/libfdt.h>
 #include <test/test.h>
 #include <test/ut.h>
 
@@ -35,6 +38,7 @@
  *
  * @image:	pointer to synthetic image data in RAM
  * @image_size:	size of the synthetic image
+ * @attempts:	number of times .read() was entered, including rejected ones
  * @read_count:	number of times .read() was called
  * @last_off:	offset from the most recent .read() call
  * @last_size:	size from the most recent .read() call
@@ -42,6 +46,7 @@
 struct mock_priv {
 	const void *image;
 	size_t image_size;
+	int attempts;
 	int read_count;
 	loff_t last_off;
 	ulong last_size;
@@ -52,6 +57,8 @@ static ulong mock_reader(struct spl_load_info *info, ulong sector,
 {
 	struct mock_priv *p = info->priv;
 
+	p->attempts++;
+
 	if (sector + count > p->image_size)
 		return 0;
 
@@ -611,6 +618,204 @@ static int imagemap_test_blocklen(struct unit_test_state *uts)
 
 IMAGEMAP_TEST(imagemap_test_blocklen, UTF_DM);
 
+/*
+ * build_fit_ext_datasz() - build a FIT with one external-data ramdisk image
+ *
+ * The ramdisk uses external data (data-offset 0, so the payload starts at the
+ * aligned end of the FDT) and declares @data_size for it. No hash is needed:
+ * the read under test happens before any verification.
+ *
+ * @buf:	output buffer for the FIT
+ * @bufsz:	size of @buf
+ * @load:	sub-image load address
+ * @data_size:	the data-size property value
+ * Return: 0 on success, negative FDT error otherwise
+ */
+static int build_fit_ext_datasz(void *buf, size_t bufsz, u32 load,
+				u32 data_size)
+{
+	int images, img, confs, conf, ret;
+	fdt32_t v;
+
+	ret = fdt_create_empty_tree(buf, bufsz);
+	if (ret)
+		return ret;
+
+	/* fit_check_format() requires description and timestamp. */
+	ret = fdt_setprop_string(buf, 0, FIT_DESC_PROP, "data-size bound");
+	if (ret)
+		return ret;
+	v = cpu_to_fdt32(0);
+	ret = fdt_setprop(buf, 0, FIT_TIMESTAMP_PROP, &v, sizeof(v));
+	if (ret)
+		return ret;
+
+	images = fdt_add_subnode(buf, 0, "images");
+	if (images < 0)
+		return images;
+	img = fdt_add_subnode(buf, images, "ramdisk");
+	if (img < 0)
+		return img;
+	ret = fdt_setprop_string(buf, img, FIT_TYPE_PROP, "ramdisk");
+	if (ret)
+		return ret;
+	ret = fdt_setprop_string(buf, img, FIT_ARCH_PROP, "sandbox");
+	if (ret)
+		return ret;
+	ret = fdt_setprop_string(buf, img, FIT_OS_PROP, "linux");
+	if (ret)
+		return ret;
+	ret = fdt_setprop_string(buf, img, FIT_COMP_PROP, "none");
+	if (ret)
+		return ret;
+	v = cpu_to_fdt32(load);
+	ret = fdt_setprop(buf, img, FIT_LOAD_PROP, &v, sizeof(v));
+	if (ret)
+		return ret;
+	v = cpu_to_fdt32(0);
+	ret = fdt_setprop(buf, img, FIT_DATA_OFFSET_PROP, &v, sizeof(v));
+	if (ret)
+		return ret;
+	v = cpu_to_fdt32(data_size);
+	ret = fdt_setprop(buf, img, FIT_DATA_SIZE_PROP, &v, sizeof(v));
+	if (ret)
+		return ret;
+
+	confs = fdt_add_subnode(buf, 0, "configurations");
+	if (confs < 0)
+		return confs;
+	ret = fdt_setprop_string(buf, confs, FIT_DEFAULT_PROP, "conf-1");
+	if (ret)
+		return ret;
+	conf = fdt_add_subnode(buf, confs, "conf-1");
+	if (conf < 0)
+		return conf;
+	ret = fdt_setprop_string(buf, conf, FIT_RAMDISK_PROP, "ramdisk");
+	if (ret)
+		return ret;
+
+	return fdt_pack(buf);
+}
+
+/*
+ * A FIT's data-size is not covered by the configuration signature, so it is
+ * untrusted input. On the imagemap storage path an uncompressed image with a
+ * load address is read to that address before fit_image_select() verifies it,
+ * so fit_image_load() must reject a data-size that does not fit in the free
+ * memory there rather than read that many bytes off the device.
+ */
+static int imagemap_test_fit_oversize_datasz(struct unit_test_state *uts)
+{
+	struct udevice *dev, *saved_im;
+	u8 fitbuf[1024];
+	ulong load = 0x02000000, data = 0, len = 0;
+	int saved_verify, ret, attempts;
+
+	/* fit_image_load() is only built with FIT, which IMAGEMAP does not need */
+	if (!CONFIG_IS_ENABLED(FIT))
+		return -EAGAIN;
+
+	/* data-size far beyond the free memory at the load address. */
+	ut_assertok(build_fit_ext_datasz(fitbuf, sizeof(fitbuf), load,
+					 0x40000000));
+	ut_assertok(create_mock_loader(fitbuf, sizeof(fitbuf), &dev));
+
+	saved_im = bootm_imagemap(&images);
+	saved_verify = images.verify;
+	bootm_set_imagemap(&images, dev);
+	images.verify = 1;
+	ret = fit_image_load(&images, map_to_sysmem(fitbuf), NULL, NULL,
+			     IH_ARCH_SANDBOX, IH_TYPE_RAMDISK,
+			     BOOTSTAGE_ID_FIT_RD_START,
+			     FIT_LOAD_OPTIONAL_NON_ZERO, &data, &len);
+	bootm_set_imagemap(&images, saved_im);
+	images.verify = saved_verify;
+
+	attempts = get_mock_priv(dev)->attempts;
+	imagemap_cleanup(dev);
+
+	/* Rejected before the device read, not after a hash failure. */
+	ut_asserteq(-EFBIG, ret);
+	/* and the reject happened before any read reached the device. */
+	ut_asserteq(0, attempts);
+
+	return 0;
+}
+
+IMAGEMAP_TEST(imagemap_test_fit_oversize_datasz, UTF_DM);
+
+/*
+ * The bound on the direct read must not get in the way of a valid image: an
+ * uncompressed ramdisk with a load address is still read straight to it.
+ */
+static int imagemap_test_fit_direct_read(struct unit_test_state *uts)
+{
+	struct udevice *dev, *saved_im;
+	struct imagemap_priv *priv;
+	const struct imagemap_region *r;
+	u8 fitbuf[1024];
+	const int size = 64;
+	phys_addr_t load;
+	ulong data = 0, len = 0;
+	int saved_verify, ret, read_count;
+	bool direct;
+	void *dst;
+	u32 off;
+
+	/* fit_image_load() is only built with FIT, which IMAGEMAP does not need */
+	if (!CONFIG_IS_ENABLED(FIT))
+		return -EAGAIN;
+
+	/*
+	 * A load address inside a used LMB region is refused, so take one
+	 * that LMB reports as free.
+	 */
+	ut_assertok(lmb_alloc_mem(LMB_MEM_ALLOC_ANY, ARCH_DMA_MINALIGN, &load,
+				  size, LMB_NONE));
+	ut_assertok(lmb_free(load, size, LMB_NONE));
+	dst = map_sysmem(load, size);
+	memset(dst, 0xff, size);
+
+	ut_assertok(build_fit_ext_datasz(fitbuf, sizeof(fitbuf), load, size));
+
+	/* data-offset is 0, so the payload follows the FDT */
+	off = ALIGN(fdt_totalsize(fitbuf), 4);
+	ut_assert(off + size <= sizeof(fitbuf));
+	for (int i = 0; i < size; i++)
+		fitbuf[off + i] = i;
+
+	ut_assertok(create_mock_loader(fitbuf, sizeof(fitbuf), &dev));
+
+	saved_im = bootm_imagemap(&images);
+	saved_verify = images.verify;
+	bootm_set_imagemap(&images, dev);
+	images.verify = 1;
+	ret = fit_image_load(&images, map_to_sysmem(fitbuf), NULL, NULL,
+			     IH_ARCH_SANDBOX, IH_TYPE_RAMDISK,
+			     BOOTSTAGE_ID_FIT_RD_START,
+			     FIT_LOAD_OPTIONAL_NON_ZERO, &data, &len);
+	bootm_set_imagemap(&images, saved_im);
+	images.verify = saved_verify;
+
+	/* A direct read is recorded at the load address, without scratch RAM */
+	priv = dev_get_uclass_priv(dev);
+	r = alist_get(&priv->regions, 0, struct imagemap_region);
+	direct = priv->regions.count == 1 && r->ram == dst && !r->lmb_reserved;
+	read_count = get_mock_priv(dev)->read_count;
+	imagemap_cleanup(dev);
+
+	ut_assert(ret >= 0);
+	ut_asserteq(load, data);
+	ut_asserteq(size, len);
+	ut_asserteq(1, read_count);
+	ut_assert(direct);
+	ut_asserteq_mem(fitbuf + off, dst, size);
+
+	return 0;
+}
+
+IMAGEMAP_TEST(imagemap_test_fit_direct_read, UTF_DM);
+
 #if CONFIG_IS_ENABLED(MTD_BLOCK)
 /*
  * End-to-end: read a known pattern back from a bare (non-NAND) MTD
-- 
2.43.0


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

* Re: [PATCH v2 1/2] boot: fit: bound the external data size on the imagemap storage path
  2026-11-20 13:11   ` [PATCH v2 1/2] " Aristo Chen
@ 2026-11-29  2:03     ` Daniel Golle
  0 siblings, 0 replies; 10+ messages in thread
From: Daniel Golle @ 2026-11-29  2:03 UTC (permalink / raw)
  To: Aristo Chen; +Cc: u-boot, Tom Rini

On Fri, Nov 20, 2026 at 01:11:07PM +0000, Aristo Chen wrote:
> fit_image_load_storage() loads an external-data sub-image by reading it
> from the storage device with a transfer sized from the FIT data-size
> property. That property is listed in exc_prop[] in boot/image-fit-sig.c,
> so it is excluded from the configuration signature and stays under the
> control of anyone able to modify the boot medium even when FIT_SIGNATURE
> is enabled. The read happens before fit_image_select() checks the image
> hash, and for an uncompressed image with a load address it goes straight
> to that address, so an inflated data-size overruns the destination
> before the corruption can be detected.
> 
> The other branch is not changed. There imagemap_map() reads into a
> buffer that it allocates from LMB at the requested size. Its size
> arithmetic can wrap for a data-size close to 4 GiB when ulong or
> phys_size_t is 32 bits wide; that needs a fix in imagemap_map() itself
> and is not part of this patch.
> 
> Before the direct read, reject an image whose data-size is larger than
> the memory that lmb_get_free_size() reports as free at the load address,
> and print a message that gives both. Within the limits of that helper,
> which are listed below, this keeps the read away from U-Boot itself,
> from reserved memory and from the buffers imagemap_map() has allocated.
> It does not limit how large an image may be.
> 
> This is partial hardening, not a complete fix. Free memory behind the
> real payload can still be written before the hash check rejects the
> image, and the destination of a direct read is not reserved in LMB, so
> a sub-image that was read directly is not protected from a later one.
> 
> lmb_get_free_size() has three limits that are left as they are:
> 
> - It stops at every used region and returns 0 from inside one. That
>   includes the LMB_NONE regions that the load commands, tftp, wget,
>   "sf read" and bootm leave behind, so a valid image is refused when
>   the load address of a sub-image is still recorded as used after an
>   earlier load in the same session. tftp and wget stopped using the
>   helper for that reason, in commit af45c84871e4 ("tftp: rework the
>   logic to validate the load address") and commit 51ebd514ec78 ("wget:
>   rework the logic to validate the load address").
> - With more than one RAM bank, the size it returns can run across the
>   hole to the next bank.
> - With a 32-bit phys_addr_t, it does not see a reservation that ends at
>   4 GiB from an address inside that reservation.
> 
> Fixes: 46d32e38ee4e ("boot: fit: support on-demand loading in fit_image_load()")
> Addresses-Coverity-ID: CID 661899: Insecure data handling (TAINTED_SCALAR)
> Suggested-by: Daniel Golle <daniel@makrotopia.org>
> Signed-off-by: Aristo Chen <aristo.chen@canonical.com>
> ---
> v2: bound only the direct read, by the free memory that LMB reports at
> the load address, as suggested by Daniel. The CONFIG_SYS_BOOTM_LEN
> ceiling and the Kconfig change are gone. Commit message rewritten; it
> now lists the limits of lmb_get_free_size().

Reviewed-by: Daniel Golle <daniel@makrotopia.org>

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

* Re: [PATCH v2 2/2] test: boot: imagemap: check the data-size bound on a direct read
  2026-11-20 13:11   ` [PATCH v2 2/2] test: boot: imagemap: check the data-size bound on a direct read Aristo Chen
@ 2026-11-29  2:05     ` Daniel Golle
  0 siblings, 0 replies; 10+ messages in thread
From: Daniel Golle @ 2026-11-29  2:05 UTC (permalink / raw)
  To: Aristo Chen; +Cc: u-boot, Tom Rini, openwrt-devel

On Fri, Nov 20, 2026 at 01:11:08PM +0000, Aristo Chen wrote:
> Build a FIT with an external-data ramdisk that has a load address and
> load it through the imagemap storage path with verification on.
> 
> The first test declares a data-size of 1 GiB, far more than the free
> memory at the load address, and confirms that fit_image_load() returns
> -EFBIG. It counts how often the mock reader is entered and asserts that
> this is zero, so the oversized image is rejected before any read reaches
> the device rather than after a failed transfer. Without the preceding
> bound the read is attempted and fails with -EIO.
> 
> The second test makes sure that the bound does not get in the way of a
> valid image: a ramdisk whose data-size matches its payload is still read
> straight to its load address. The bound refuses a load address inside a
> used LMB region, so the test takes an address that LMB reports as free.
> 
> The tests need the FIT loader, which IMAGEMAP does not depend on, so
> they are skipped when FIT is disabled.
> 
> Signed-off-by: Aristo Chen <aristo.chen@canonical.com>
> ---
> v2: add imagemap_test_fit_direct_read(), which checks that a valid
> ramdisk with a load address is still read straight to it. The comments
> of imagemap_test_fit_oversize_datasz() no longer refer to
> CONFIG_SYS_BOOTM_LEN; the test itself is unchanged. New subject, was
> "test: boot: imagemap: reject an oversized data-size".

Reviewed-by: Daniel Golle <daniel@makrotopia.org>

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

end of thread, other threads:[~2026-11-29  2:05 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-15 14:31 [PATCH v1 0/2] boot: fit: bound the external data size on the imagemap storage path Aristo Chen
2026-11-15 14:31 ` [PATCH v1 1/2] " Aristo Chen
2026-11-15 21:39   ` Daniel Golle
2026-11-17  9:53     ` Aristo Chen
2026-11-15 14:31 ` [PATCH v1 2/2] test: boot: imagemap: reject an oversized data-size Aristo Chen
2026-11-20 13:11 ` [PATCH v2 0/2] boot: fit: bound the external data size on the imagemap storage path Aristo Chen
2026-11-20 13:11   ` [PATCH v2 1/2] " Aristo Chen
2026-11-29  2:03     ` Daniel Golle
2026-11-20 13:11   ` [PATCH v2 2/2] test: boot: imagemap: check the data-size bound on a direct read Aristo Chen
2026-11-29  2:05     ` Daniel Golle

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox