spl: fit: bound external data-size in load_simple_fit()
cover spl: fit: bound external data-size in load_simple_fit()
This series closes a pre-authentication memory corruption window in the SPL FIT loader. load_simple_fit() in common/spl/spl_fit.c reads external image data from the boot device with a transfer sized from the FIT data-size property, and only verifies the image hash after the read has already happened. 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 CONFIG_SPL_FIT_SIGNATURE is on. An inflated value drives info->read() into the destination before fit_image_verify_with_data() can reject the image. The device-tree overlay path is the sharpest case, because there the destination is a fixed CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ heap buffer rather than a sized load region.
Patch 1 adds a max_size argument to load_simple_fit() and rejects both the raw data-size and the block-aligned transfer length with -EFBIG before the device read is issued. The data-size property is read into an int, so a value with bit 31 set arrives as a negative number, and comparing it as an unsigned quantity rejects that case too. Callers that load into a memory region pass CONFIG_SYS_BOOTM_LEN, the same limit spl_parse_legacy_validate() already applies to legacy images. The overlay path passes the size of its temporary buffer. The two guards share one diagnostic printf via a goto to a common label so the bound check stays under the SPL SRAM budget on size constrained boards such as am335x_hs_evm (CONFIG_SPL_MAX_SIZE = 0xb0b0). Measured cost on that defconfig is 57 bytes of .text, leaving 114 bytes of headroom against the limit.
Patch 2 adds a sandbox regression test in test/image/spl_load.c. It builds a FIT with external data, then rewrites the data-size property in place to a value far beyond both the image buffer and any plausible load region, and asserts that spl_load_simple_fit() returns -EFBIG instead of overrunning the destination. Without the fix the test crashes inside the test-harness read callback when memcpy() walks past the small calloc'd image; with the fix the load is rejected cleanly.
Verified on sandbox_spl_defconfig: the new test passes, and the full SPL unit test suite reports 20 run, 1 skipped, 0 failures. Also cross-built am335x_hs_evm_defconfig and am335x_hs_evm_spi_defconfig with arm-linux-gnueabihf-, both within the SRAM budget.
Aristo Chen (2):
spl: fit: bound the external data size before reading it
test: spl: check load_simple_fit() rejects an oversized data-size
common/spl/spl_fit.c | 41 ++++++++++++++++++++++++++++++------ test/image/spl_load.c | 49 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/2 spl: fit: bound the external data size before reading it
load_simple_fit() loads an image stored as external data by reading it from the boot device with a transfer sized from the FIT data-size property. That property is listed in exc_prop[] in 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 CONFIG_SPL_FIT_SIGNATURE is enabled. The read happens before fit_image_verify_with_data() checks the image hash, so an inflated data-size overruns the destination before the corruption can be detected. The device-tree overlay path is the sharpest case, because there the destination is a fixed CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ heap buffer.
Pass the size of the destination into load_simple_fit() and reject an image whose data does not fit before the read is issued. The data-size property is read into an int, so a value with bit 31 set arrives as a negative number, and comparing it as an unsigned quantity rejects that case as well. The block-aligned transfer length is checked too, because info->read() moves data-size rounded up to the device block length.
Callers that load into a memory region pass CONFIG_SYS_BOOTM_LEN, the limit spl_parse_legacy_validate() already applies to legacy images. The overlay path passes the size of its temporary buffer.
--- common/spl/spl_fit.c | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-)
common/spl/spl_fit.c +35 -6
@@ -204,6 +204,9 @@ static int get_aligned_image_size(struct spl_load_info *info, int data_size,* If the FIT node does not contain a "load" (address) property, * the image gets loaded to the address pointed to by the * load_addr member in this struct, if load_addr is not 0 +* @max_size: maximum number of bytes that may be written to the +* destination; an image whose data exceeds this is rejected +* before it is read from the device * * Return: 0 on success, -EBADSLT if this image is not the correct phase * (for CONFIG_BOOTMETH_VBE_SIMPLE_FW), or another negative error number on
@@ -211,7 +214,7 @@ static int get_aligned_image_size(struct spl_load_info *info, int data_size,*/ static int load_simple_fit (struct spl_load_info *info,ulong fit_offset,const struct spl_fit_info *ctx,int node, -struct spl_image_info *image_info) +struct spl_image_info *image_info,ulong max_size) {int offset;size_t length;
@@ -291,6 +294,18 @@ static int load_simple_fit(struct spl_load_info *info, ulong fit_offset,return 0 ; } +/* +* data-size is excluded from the configuration signature (it +* is in the exc_prop[] list in image-fit-sig.c), so it remains +* attacker-controlled even after fit_config_verify() succeeds. +* The image hash is only verified after the device read below, +* so an oversized data-size has to be rejected here. The value +* is stored into an int, so one with bit 31 set arrives as a +* negative number; the unsigned compare rejects that too. +*/ +if ((ulong )len > max_size) + goto too_big; +if (spl_decompression_enabled () && (image_comp ==IH_COMP_GZIP || image_comp ==IH_COMP_LZMA )) src_ptr =map_sysmem (ALIGN (CONFIG_SYS_LOAD_ADDR ,ARCH_DMA_MINALIGN ), len);
@@ -302,6 +317,15 @@ static int load_simple_fit(struct spl_load_info *info, ulong fit_offset, size =get_aligned_image_size (info, length, offset); read_offset = fit_offset +get_aligned_image_offset (info, offset); + +/* The block-aligned read size must also fit the destination */ +if (size > max_size) { +too_big: +printf ("%s: FIT image too large (max %lu)\n" , + __func__, max_size); +return -EFBIG ; + } +log_debug ("reading from offset %x / %lx size %lx to %p: " , offset, read_offset, size, src_ptr);
@@ -427,7 +451,8 @@ static int spl_fit_append_fdt(struct spl_image_info *spl_image, spl_image->fdt_addr =map_sysmem (image_info.load_addr, size);memcpy (spl_image->fdt_addr, gd->fdt_blob, size); }else { - ret =load_simple_fit (info, offset, ctx, node, &image_info); + ret =load_simple_fit (info, offset, ctx, node, &image_info, +CONFIG_SYS_BOOTM_LEN );if (ret <0 )return ret;
@@ -479,7 +504,8 @@ static int spl_fit_append_fdt(struct spl_image_info *spl_image, } image_info.load_addr = (ulong )tmpbuffer; ret =load_simple_fit (info, offset, ctx, node, - &image_info); + &image_info, +CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ );if (ret == -EBADSLT )continue ;else if (ret <0 )
@@ -687,7 +713,8 @@ static int spl_fit_load_fpga(struct spl_fit_info *ctx,warn_deprecated ("'fpga' property in config node. Use 'loadables'" );/* Load the image and set up the fpga_image structure */ - ret =load_simple_fit (info, offset, ctx, node, &fpga_image); + ret =load_simple_fit (info, offset, ctx, node, &fpga_image, +CONFIG_SYS_BOOTM_LEN );if (ret) {printf ("%s: Cannot load the FPGA: %i\n" , __func__, ret);return ret;
@@ -849,7 +876,8 @@ int spl_load_simple_fit(struct spl_image_info *spl_image, }/* Load the image and set up the spl_image structure */ - ret =load_simple_fit (info, offset, &ctx, node, spl_image); + ret =load_simple_fit (info, offset, &ctx, node, spl_image, +CONFIG_SYS_BOOTM_LEN );if (ret)return ret;
@@ -890,7 +918,8 @@ int spl_load_simple_fit(struct spl_image_info *spl_image,continue ; image_info.load_addr =0 ; - ret =load_simple_fit (info, offset, &ctx, node, &image_info); + ret =load_simple_fit (info, offset, &ctx, node, &image_info, +CONFIG_SYS_BOOTM_LEN );if (ret <0 && ret != -EBADSLT ) {printf ("%s: can't load image loadables index %d (ret = %d)\n" , __func__, index, ret);
Signature · 1 line
2.43.0
Re: [PATCH v1 1/2] spl: fit: bound the external data size before reading it
Hi Aristo,
Quoted 32 lines · Aristo Chen
spl: fit: bound the external data size before reading it
load_simple_fit()loads an image stored as external data by reading it from the boot device with a transfer sized from the FIT data-size property. That property is listed in exc_prop[] inimage-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 whenCONFIG_SPL_FIT_SIGNATUREis enabled. The read happens beforefit_image_verify_with_data()checks the image hash, so an inflated data-size overruns the destination before the corruption can be detected. The device-tree overlay path is the sharpest case, because there the destination is a fixedCONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZheap buffer.Pass the size of the destination into
load_simple_fit()and reject an image whose data does not fit before the read is issued. The data-size property is read into an int, so a value with bit 31 set arrives as a negative number, and comparing it as an unsigned quantity rejects that case as well. The block-aligned transfer length is checked too, becauseinfo->read()moves data-size rounded up to the device block length. [...]
common/spl/spl_fit.c| 41 +++++++++++++++++++++++++++++++++++------1 file changed, 35 insertions(+), 6 deletions(-)common/spl/spl_fit.c +3
-0@@ -291,6 +294,18 @@ static int load_simple_fit(struct spl_load_info *info, ulong fit_offset, +if ((ulong )len > max_size) + goto too_big; +
common/spl/spl_fit.c static int load_simple_fit(struct spl_load_info *info, ulong fit_offset, · line 323
Quoted 7 lines
common/spl/spl_fit.c +7
-0@@ -302,6 +317,15 @@ static int load_simple_fit(struct spl_load_info *info, ulong fit_offset, +/* The block-aligned read size must also fit the destination */ +if (size > max_size) { +too_big: +printf ("%s: FIT image too large (max %lu)\n" , + __func__, max_size); +return -EFBIG ; + }
Please can you avoid jumping into the body of an if-statement. A goto landing inside a conditional block and falling through the printf and return is hard to follow, and will likely trip up static analysers. Put the shared label at the end of the function:
if ((ulong)len > max_size)
goto too_big;
...
if (size > max_size)
goto too_big;
...
return 0;
too_big:
printf(...);
return -EFBIG;The cover letter quotes 57 bytes for the current shape; what does the cleaner form cost on am335x_hs_evm? If it really does push past CONFIG_SPL_MAX_SIZE then we'll have to use your code, but please say so in the commit message.
common/spl/spl_fit.c static int load_simple_fit(struct spl_load_info *info, ulong fit_offset, · line 295
Quoted 4 lines
common/spl/spl_fit.c +2
-0@@ -291,6 +294,18 @@ static int load_simple_fit(struct spl_load_info *info, ulong fit_offset, +if ((ulong )len > max_size) + goto too_big;
Since size is the block-aligned length and size >= len, the second check already covers everything this one catches, including the negative-len case. The value of the early check is that it bails before get_aligned_image_size() runs on a hostile value - please mention that in the comment rather than the bit-31 reasoning, which applies equally to the later check.
Also, the diagnostic only prints max_size. Including len (or size) would make a real failure much easier to triage from a board log.
common/spl/spl_fit.c static int spl_fit_append_fdt(struct spl_image_info *spl_image, · line 453
Quoted 6 lines
common/spl/spl_fit.c +2
-1@@ -427,7 +451,8 @@ static int spl_fit_append_fdt(struct spl_image_info *spl_image, }else { - ret =load_simple_fit (info, offset, ctx, node, &image_info); + ret =load_simple_fit (info, offset, ctx, node, &image_info, +CONFIG_SYS_BOOTM_LEN );
Just to check - the destination here is image_info.load_addr, set a few lines up to ALIGN(spl_image->load_addr + spl_image->size, 8) -- CONFIG_SYS_BOOTM_LEN is a sensible upper bound for the overall image, but it isn't the space available at that address; the FDT is just placed after the loaded image. Please note in the commit message that for the non-overlay paths this is a conservative ceiling rather than the true destination size, so it isn't mistaken for a tight bound later. The overlay caller is the one actually sized to the buffer.
Regards,
Simon
PATCH 2/2 test: spl: check load_simple_fit() rejects an oversized data-size
In reply to Aristo ChenAdd a regression test that builds a FIT with external data, inflates the data-size property far beyond the image and any plausible load region, and confirms that spl_load_simple_fit() returns -EFBIG instead of reading the declared size off the device. Without the bounds check in load_simple_fit() this test overruns memory and crashes; with it the load is rejected cleanly.
--- test/image/spl_load.c | 49 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+)
test/image/spl_load.c +49 -0
@@ -367,6 +367,55 @@ SPL_IMG_TEST(spl_test_image, IMX8, 0);SPL_IMG_TEST (spl_test_image,FIT_INTERNAL ,0 );SPL_IMG_TEST (spl_test_image,FIT_EXTERNAL ,0 ); +/* +* A FIT image's data-size property is not covered by the configuration +* signature, so it is untrusted input. load_simple_fit() must reject a +* data-size larger than the destination rather than overrun it, because the +* device read happens before the image hash is verified. +*/ +static int spl_test_fit_external_oversize (struct unit_test_state *uts) +{ +size_t img_size, img_data, data_size =SPL_TEST_DATA_SIZE ; +struct spl_image_info info_write = { + .name ="oversize" , + .size = data_size, + }, info_read = { }; +struct spl_load_info load; +void *img; +int node; + +if (!image_supported (FIT_EXTERNAL )) +return -EAGAIN ; + + img_size =create_image (NULL ,FIT_EXTERNAL , &info_write, &img_data); +ut_assert (img_size); + img =calloc (img_size,1 ); +ut_assertnonnull (img); + +generate_data (img + img_data, data_size,"oversize" ); +ut_asserteq (img_size,create_image (img,FIT_EXTERNAL , &info_write, +NULL )); + +/* +* Inflate data-size far beyond the image buffer and any plausible +* load region. Without a bounds check, load_simple_fit() reads this +* many bytes off the "device" before the hash is checked. +*/ + node =fdt_path_offset (img,FIT_IMAGES_PATH ); +ut_assert (node >=0 ); + node =fdt_first_subnode (img, node); +ut_assert (node >=0 ); +ut_assertok (fdt_setprop_inplace_u32 (img, node,FIT_DATA_SIZE_PROP , +0x40000000 )); + +spl_load_init (&load, spl_test_read, img,1 ); +ut_asserteq (-EFBIG ,spl_load_simple_fit (&info_read, &load,0 , img)); + +free (img); +return 0 ; +} +SPL_TEST (spl_test_fit_external_oversize,0 ); +/* * LZMA is too complex to generate on the fly, so let's use some data I put in * the oven^H^H^H^H compressed earlier
Signature · 1 line
2.43.0
Re: [PATCH v1 2/2] test: spl: check load_simple_fit() rejects an oversized data-size
Quoted 9 lines · Aristo Chen
test: spl: check
load_simple_fit()rejects an oversized data-sizeAdd a regression test that builds a FIT with external data, inflates the data-size property far beyond the image and any plausible load region, and confirms that
spl_load_simple_fit()returns-EFBIGinstead of reading the declared size off the device. Without the bounds check inload_simple_fit()this test overruns memory and crashes; with it the load is rejected cleanly.
Quoted 4 lines
Signed-off-by Aristo Chen <[email protected]>
test/image/spl_load.c| 49 +++++++++++++++++++++++++++++++++++++++++++++++++1 file changed, 49 insertions(+)
v2 cover spl: fit: bound external data-size in load_simple_fit()
In reply to Aristo ChenLink to v1:
https://lore.kernel.org/u-boot/[email protected]/
Changes from v1 (all from Simon's review of patch 1):
- Move the shared error label to the end of the function so no goto jumps into an if-body. Both bound checks now goto too_big and the printf/return sits after the final return 0.
- Rewrite the comment on the early len > max_size check. The real reason it exists is to keep get_aligned_image_size() from running its int arithmetic on a hostile value; the block-aligned size check further down is the mathematically binding one because size is len rounded up.
- Include the data-size value in the diagnostic printf so a real failure is triageable from a board log.
- Note in the fix commit message that CONFIG_SYS_BOOTM_LEN is a conservative ceiling for the firmware/loadables/FDT/FPGA callers rather than the exact capacity at the destination. The overlay caller remains the one that is actually tight against its buffer.
---
This series closes a pre-authentication memory corruption window in the SPL FIT loader. load_simple_fit() in common/spl/spl_fit.c reads external image data from the boot device with a transfer sized from the FIT data-size property, and only verifies the image hash after the read has already happened. 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 CONFIG_SPL_FIT_SIGNATURE is on. An inflated value drives info->read() into the destination before fit_image_verify_with_data() can reject the image. The device-tree overlay path is the sharpest case, because there the destination is a fixed CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ heap buffer rather than a sized load region.
Patch 1 adds a max_size argument to load_simple_fit() and rejects both the raw data-size and the block-aligned transfer length with -EFBIG before the device read is issued. The size check is the mathematically binding one because size is len rounded up to the device block length; the early len check exists so get_aligned_image_size() never runs on a hostile value, where its int arithmetic would invoke signed-integer overflow. Callers that load into a memory region pass CONFIG_SYS_BOOTM_LEN, the same limit spl_parse_legacy_validate() already applies to legacy images. The overlay path passes the size of its temporary buffer.
Patch 2 adds a sandbox regression test in test/image/spl_load.c. It builds a FIT with external data, then rewrites the data-size property in place to a value far beyond both the image buffer and any plausible load region, and asserts that spl_load_simple_fit() returns -EFBIG instead of overrunning the destination. Without the fix the test crashes inside the test-harness read callback when memcpy() walks past the small calloc'd image; with the fix the load is rejected cleanly.
Verified on sandbox_spl_defconfig: the new test passes, and the full SPL unit test suite reports 20 run, 1 skipped, 0 failures. Also cross-built am335x_hs_evm_defconfig and am335x_hs_evm_spi_defconfig with arm-linux-gnueabihf-, both within the SRAM budget.
On the size question from v1 review: the cleaner shape (single too_big label at end of function) plus the expanded printf now costs 71 bytes text on am335x_hs_evm (vs the 57 bytes v1 quoted for the goto-into-if shape). text+data lands at 45132 out of the CONFIG_SPL_MAX_SIZE = 0xb0b0 = 45232 budget, so 100 bytes of headroom.
Aristo Chen (2):
spl: fit: bound the external data size before reading it
test: spl: check load_simple_fit() rejects an oversized data-size
common/spl/spl_fit.c | 51 ++++++++++++++++++++++++++++++++++++++----- test/image/spl_load.c | 49 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/2 spl: fit: bound the external data size before reading it
load_simple_fit() loads an image stored as external data by reading it from the boot device with a transfer sized from the FIT data-size property. That property is listed in exc_prop[] in 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 CONFIG_SPL_FIT_SIGNATURE is enabled. The read happens before fit_image_verify_with_data() checks the image hash, so an inflated data-size overruns the destination before the corruption can be detected. The device-tree overlay path is the sharpest case, because there the destination is a fixed CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ heap buffer.
Pass the size of the destination into load_simple_fit() and reject an image whose data does not fit before the read is issued. The check is done in two places: an early bail on len > max_size, then a bail on the block-aligned size > max_size. The size check is the mathematically binding one because size is len rounded up to the device block length. The early bail exists so that get_aligned_image_size() never runs on a hostile len, where its int arithmetic would invoke signed-integer overflow.
For the overlay path the bound is exact: the caller passes the size of its temporary buffer. For the firmware, loadables, FDT and FPGA call sites the destination is wherever the load_addr field points, with no defined upper limit at the call site. Those callers pass CONFIG_SYS_BOOTM_LEN as a conservative ceiling, matching the same limit spl_parse_legacy_validate() already applies to legacy images. It is not a tight bound on the actual capacity at the destination, just a cap that rejects implausibly-sized data.
--- common/spl/spl_fit.c | 51 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 6 deletions(-)
common/spl/spl_fit.c +45 -6
@@ -204,6 +204,9 @@ static int get_aligned_image_size(struct spl_load_info *info, int data_size,* If the FIT node does not contain a "load" (address) property, * the image gets loaded to the address pointed to by the * load_addr member in this struct, if load_addr is not 0 +* @max_size: maximum number of bytes that may be written to the +* destination; an image whose data exceeds this is rejected +* before it is read from the device * * Return: 0 on success, -EBADSLT if this image is not the correct phase * (for CONFIG_BOOTMETH_VBE_SIMPLE_FW), or another negative error number on
@@ -211,7 +214,7 @@ static int get_aligned_image_size(struct spl_load_info *info, int data_size,*/ static int load_simple_fit (struct spl_load_info *info,ulong fit_offset,const struct spl_fit_info *ctx,int node, -struct spl_image_info *image_info) +struct spl_image_info *image_info,ulong max_size) {int offset;size_t length;
@@ -291,6 +294,23 @@ static int load_simple_fit(struct spl_load_info *info, ulong fit_offset,return 0 ; } +/* +* data-size is excluded from the configuration signature (it +* is in exc_prop[] in image-fit-sig.c), so it stays attacker +* controlled even after fit_config_verify() succeeds. The +* image hash is only verified after the device read below, so +* an oversized value has to be rejected here. +* +* Bail out before get_aligned_image_size() runs on a hostile +* len: that helper does its arithmetic in int and would +* invoke signed-integer overflow on a value close to or above +* INT_MAX. The block-aligned check further down is the +* mathematically binding one, since size is len rounded up to +* the device block length. +*/ +if ((ulong )len > max_size) + goto too_big; +if (spl_decompression_enabled () && (image_comp ==IH_COMP_GZIP || image_comp ==IH_COMP_LZMA )) src_ptr =map_sysmem (ALIGN (CONFIG_SYS_LOAD_ADDR ,ARCH_DMA_MINALIGN ), len);
@@ -302,6 +322,15 @@ static int load_simple_fit(struct spl_load_info *info, ulong fit_offset, size =get_aligned_image_size (info, length, offset); read_offset = fit_offset +get_aligned_image_offset (info, offset); + +/* +* info->read() transfers the block-aligned size into the +* destination, so this is the bound that actually matters; +* len was rejected above only to keep this computation safe. +*/ +if (size > max_size) + goto too_big; +log_debug ("reading from offset %x / %lx size %lx to %p: " , offset, read_offset, size, src_ptr);
@@ -372,6 +401,11 @@ static int load_simple_fit(struct spl_load_info *info, ulong fit_offset,upl_add_image (fit, node, load_addr, length);return 0 ; + +too_big: +printf ("%s: FIT image too large (data-size %u, max %lu)\n" , + __func__, (u32 )len, max_size); +return -EFBIG ; }static bool os_takes_devicetree (uint8_t os)
@@ -427,7 +461,8 @@ static int spl_fit_append_fdt(struct spl_image_info *spl_image, spl_image->fdt_addr =map_sysmem (image_info.load_addr, size);memcpy (spl_image->fdt_addr, gd->fdt_blob, size); }else { - ret =load_simple_fit (info, offset, ctx, node, &image_info); + ret =load_simple_fit (info, offset, ctx, node, &image_info, +CONFIG_SYS_BOOTM_LEN );if (ret <0 )return ret;
@@ -479,7 +514,8 @@ static int spl_fit_append_fdt(struct spl_image_info *spl_image, } image_info.load_addr = (ulong )tmpbuffer; ret =load_simple_fit (info, offset, ctx, node, - &image_info); + &image_info, +CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ );if (ret == -EBADSLT )continue ;else if (ret <0 )
@@ -687,7 +723,8 @@ static int spl_fit_load_fpga(struct spl_fit_info *ctx,warn_deprecated ("'fpga' property in config node. Use 'loadables'" );/* Load the image and set up the fpga_image structure */ - ret =load_simple_fit (info, offset, ctx, node, &fpga_image); + ret =load_simple_fit (info, offset, ctx, node, &fpga_image, +CONFIG_SYS_BOOTM_LEN );if (ret) {printf ("%s: Cannot load the FPGA: %i\n" , __func__, ret);return ret;
@@ -849,7 +886,8 @@ int spl_load_simple_fit(struct spl_image_info *spl_image, }/* Load the image and set up the spl_image structure */ - ret =load_simple_fit (info, offset, &ctx, node, spl_image); + ret =load_simple_fit (info, offset, &ctx, node, spl_image, +CONFIG_SYS_BOOTM_LEN );if (ret)return ret;
@@ -890,7 +928,8 @@ int spl_load_simple_fit(struct spl_image_info *spl_image,continue ; image_info.load_addr =0 ; - ret =load_simple_fit (info, offset, &ctx, node, &image_info); + ret =load_simple_fit (info, offset, &ctx, node, &image_info, +CONFIG_SYS_BOOTM_LEN );if (ret <0 && ret != -EBADSLT ) {printf ("%s: can't load image loadables index %d (ret = %d)\n" , __func__, index, ret);
Signature · 1 line
2.43.0
Re: [PATCH v2 1/2] spl: fit: bound the external data size before reading it
Quoted 32 lines · Aristo Chen
spl: fit: bound the external data size before reading it
load_simple_fit()loads an image stored as external data by reading it from the boot device with a transfer sized from the FIT data-size property. That property is listed in exc_prop[] inimage-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 whenCONFIG_SPL_FIT_SIGNATUREis enabled. The read happens beforefit_image_verify_with_data()checks the image hash, so an inflated data-size overruns the destination before the corruption can be detected. The device-tree overlay path is the sharpest case, because there the destination is a fixedCONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZheap buffer.Pass the size of the destination into
load_simple_fit()and reject an image whose data does not fit before the read is issued. The check is done in two places: an early bail on len >max_size, then a bail on the block-aligned size >max_size. The size check is the mathematically binding one because size is len rounded up to the device block length. The early bail exists so thatget_aligned_image_size()never runs on a hostile len, where its int arithmetic would invoke signed-integer overflow.For the overlay path the bound is exact: the caller passes the size of its temporary buffer. For the firmware, loadables, FDT and FPGA call sites the destination is wherever the
load_addrfield points, with no defined upper limit at the call site. Those callers passCONFIG_SYS_BOOTM_LENas a conservative ceiling, matching the same limitspl_parse_legacy_validate()already applies to legacy images. It is not a tight bound on the actual capacity at the destination, just a cap that rejects implausibly-sized data.
Quoted 4 lines
Signed-off-by Aristo Chen <[email protected]>
common/spl/spl_fit.c| 51 +++++++++++++++++++++++++++++++++++++++++++++------1 file changed, 45 insertions(+), 6 deletions(-)
PATCH v2 2/2 test: spl: check load_simple_fit() rejects an oversized data-size
In reply to Aristo ChenAdd a regression test that builds a FIT with external data, inflates the data-size property far beyond the image and any plausible load region, and confirms that spl_load_simple_fit() returns -EFBIG instead of reading the declared size off the device. Without the bounds check in load_simple_fit() this test overruns memory and crashes; with it the load is rejected cleanly.
--- test/image/spl_load.c | 49 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+)
test/image/spl_load.c +49 -0
@@ -367,6 +367,55 @@ SPL_IMG_TEST(spl_test_image, IMX8, 0);SPL_IMG_TEST (spl_test_image,FIT_INTERNAL ,0 );SPL_IMG_TEST (spl_test_image,FIT_EXTERNAL ,0 ); +/* +* A FIT image's data-size property is not covered by the configuration +* signature, so it is untrusted input. load_simple_fit() must reject a +* data-size larger than the destination rather than overrun it, because the +* device read happens before the image hash is verified. +*/ +static int spl_test_fit_external_oversize (struct unit_test_state *uts) +{ +size_t img_size, img_data, data_size =SPL_TEST_DATA_SIZE ; +struct spl_image_info info_write = { + .name ="oversize" , + .size = data_size, + }, info_read = { }; +struct spl_load_info load; +void *img; +int node; + +if (!image_supported (FIT_EXTERNAL )) +return -EAGAIN ; + + img_size =create_image (NULL ,FIT_EXTERNAL , &info_write, &img_data); +ut_assert (img_size); + img =calloc (img_size,1 ); +ut_assertnonnull (img); + +generate_data (img + img_data, data_size,"oversize" ); +ut_asserteq (img_size,create_image (img,FIT_EXTERNAL , &info_write, +NULL )); + +/* +* Inflate data-size far beyond the image buffer and any plausible +* load region. Without a bounds check, load_simple_fit() reads this +* many bytes off the "device" before the hash is checked. +*/ + node =fdt_path_offset (img,FIT_IMAGES_PATH ); +ut_assert (node >=0 ); + node =fdt_first_subnode (img, node); +ut_assert (node >=0 ); +ut_assertok (fdt_setprop_inplace_u32 (img, node,FIT_DATA_SIZE_PROP , +0x40000000 )); + +spl_load_init (&load, spl_test_read, img,1 ); +ut_asserteq (-EFBIG ,spl_load_simple_fit (&info_read, &load,0 , img)); + +free (img); +return 0 ; +} +SPL_TEST (spl_test_fit_external_oversize,0 ); +/* * LZMA is too complex to generate on the fly, so let's use some data I put in * the oven^H^H^H^H compressed earlier
Signature · 1 line
2.43.0
Re: [PATCH v2 2/2] test: spl: check load_simple_fit() rejects an oversized data-size
Quoted 9 lines · Aristo Chen
test: spl: check
load_simple_fit()rejects an oversized data-sizeAdd a regression test that builds a FIT with external data, inflates the data-size property far beyond the image and any plausible load region, and confirms that
spl_load_simple_fit()returns-EFBIGinstead of reading the declared size off the device. Without the bounds check inload_simple_fit()this test overruns memory and crashes; with it the load is rejected cleanly.
Quoted 4 lines
Signed-off-by Aristo Chen <[email protected]>
test/image/spl_load.c| 49 +++++++++++++++++++++++++++++++++++++++++++++++++1 file changed, 49 insertions(+)
Quoted 20 lines · Aristo Chen
Link to v1:
https://lore.kernel.org/u-boot/[email protected]/Changes from v1 (all from Simon's review of patch 1):
- Move the shared error label to the end of the function so no goto jumps into an if-body. Both bound checks now goto
too_bigand the printf/return sits after the final return 0.
- Rewrite the comment on the early len >max_sizecheck. The real reason it exists is to keepget_aligned_image_size()from running its int arithmetic on a hostile value; the block-aligned size check further down is the mathematically binding one because size is len rounded up.
- Include the data-size value in the diagnostic printf so a real failure is triageable from a board log.
- Note in the fix commit message thatCONFIG_SYS_BOOTM_LENis a conservative ceiling for thefirmware/loadables/FDT/FPGAcallers rather than the exact capacity at the destination. The overlay caller remains the one that is actually tight against its buffer.
Quoted 1 line
[...]
Applied to u-boot/main, thanks!
[1/2] spl: fit: bound the external data size before reading it
commit: 8f71d7170f5c3abadee45683e35ae6d925b6d417
[2/2] test: spl: check load_simple_fit() rejects an oversized data-size
commit: 85667122fb5fa215315a633335efc27ecd195b21Signature · 1 line
Tom