[PATCH v2 2/2] test: boot: imagemap: check the data-size bound on a direct read
Aristo Chen
aristo.chen at canonical.com
Fri Nov 20 05:11:08 PST 2026
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 at 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
More information about the openwrt-devel
mailing list