[PATCH v1 2/2] test: boot: imagemap: reject an oversized data-size

Aristo Chen aristo.chen at canonical.com
Sun Nov 15 06:31:14 PST 2026


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 at 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




More information about the openwrt-devel mailing list