Skip to contentSkip to conversation

bootm: bound noload kernel decompression to the allocated buffer

PATCH Superseded Aristo Chen 3 participants 24 messages May 20 – Jun 18

lore
Share & tools
lore
#0 lore raw

cover bootm: bound noload kernel decompression to the allocated buffer

For a compressed kernel_noload image, bootm_load_os() allocates a decompression buffer of ALIGN(image_len * 4, SZ_1M) and then passes CONFIG_SYS_BOOTM_LEN (typically 128 MiB on arm64) to image_decomp() as the output limit. The decompressors honour whatever limit they are given, so a kernel that decompresses to more than four times its compressed size runs past the end of the allocated buffer and silently corrupts adjacent memory.

A 4x compression ratio is at the edge of what modern compressors (zstd, xz) achieve on real kernels, and is trivially exceeded by crafted, highly compressible payloads, so this is reachable both accidentally and intentionally. The overflow can land on already-loaded boot artefacts (FDT, ramdisk, loadables), U-Boot's own data, or memory-mapped device registers; the existing post-decompression overlap check in bootm_load_os() only catches overlap with the FIT itself.

Patch 1 plumbs the actual allocation size through to image_decomp() and handle_decomp_error() via a single decomp_len variable, so decompression stops at the buffer boundary and fails cleanly when the image is too large. The non-noload code path is unchanged and continues to use CONFIG_SYS_BOOTM_LEN.

Patch 2 adds a sandbox py-test that builds a FIT with a compressed kernel_noload image whose decompressed size exceeds the per-image buffer, and asserts that 'bootm loados' reports the failure instead of overflowing.

Tested on sandbox: the new test passes; the existing test_fit_compressed_images_load (which covers the load-address path) and the other tests in test/py/tests/test_fit.py continue to pass.

Aristo Chen (2):
bootm: fix overflow of the noload kernel decompression buffer test/py: test kernel_noload decompression buffer overflow

 boot/bootm.c              | 11 ++---
 test/py/tests/test_fit.py | 84 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 90 insertions(+), 5 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 bootm: fix overflow of the noload kernel decompression buffer

For a compressed kernel_noload image, bootm_load_os() allocates a decompression buffer sized to ALIGN(image_len * 4, SZ_1M), assuming the kernel compresses by no more than a factor of four. It then passes CONFIG_SYS_BOOTM_LEN, rather than the size of that buffer, to image_decomp() as the output limit. The decompressors honour the limit they are given, so a kernel that decompresses to more than four times its compressed size is written past the end of the allocated buffer and corrupts adjacent memory.

Pass the allocation size to image_decomp() and handle_decomp_error() so decompression stops at the buffer boundary and fails cleanly when the image is too large, instead of overflowing. The regular non-noload paths are unchanged and continue to use CONFIG_SYS_BOOTM_LEN.

Fixes 69544c4fd8b1 ("bootm: Support kernel_noload with compression")
---
 boot/bootm.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
boot/bootm.c +6 -5
index 4836d6b2d41..98226b208ee 100644
@@ -610,6 +610,7 @@ static int bootm_load_os(struct bootm_headers *images, int boot_progress)
 	ulong blob_end = os.end;
 	ulong image_start = os.image_start;
 	ulong image_len = os.image_len;
+	ulong decomp_len = CONFIG_SYS_BOOTM_LEN;
 	ulong flush_start = ALIGN_DOWN(load, ARCH_DMA_MINALIGN);
 	bool no_overlap;
 	void *load_buf, *image_buf;
@@ -623,11 +624,11 @@ static int bootm_load_os(struct bootm_headers *images, int boot_progress)
 	 * Use an alignment of 2MB since this might help arm64
 	 */
 	if (os.type == IH_TYPE_KERNEL_NOLOAD && os.comp != IH_COMP_NONE) {
-		ulong req_size = ALIGN(image_len * 4, SZ_1M);
 		phys_addr_t addr;
 
+		decomp_len = ALIGN(image_len * 4, SZ_1M);
 		err = lmb_alloc_mem(LMB_MEM_ALLOC_ANY, SZ_2M, &addr,
-				    req_size, LMB_NONE);
+				    decomp_len, LMB_NONE);
 		if (err)
 			return 1;
 
@@ -635,17 +636,17 @@ static int bootm_load_os(struct bootm_headers *images, int boot_progress)
 		images->os.load = (ulong)addr;
 		images->ep = (ulong)addr;
 		debug("Allocated %lx bytes at %lx for kernel (size %lx) decompression\n",
-		      req_size, load, image_len);
+		      decomp_len, load, image_len);
 	}
 
 	load_buf = map_sysmem(load, 0);
 	image_buf = map_sysmem(os.image_start, image_len);
 	err = image_decomp(os.comp, load, os.image_start, os.type,
 			   load_buf, image_buf, image_len,
-			   CONFIG_SYS_BOOTM_LEN, &load_end);
+			   decomp_len, &load_end);
 	if (err) {
 		err = handle_decomp_error(os.comp, load_end - load,
-					  CONFIG_SYS_BOOTM_LEN, err);
+					  decomp_len, err);
 		bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
 		return err;
 	}
Signature · 1 line
2.43.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH v1 1/2] bootm: fix overflow of the noload kernel decompression buffer

Quoted 9 lines · Aristo Chen

For a compressed kernel_noload image, bootm_load_os() allocates a decompression buffer sized to ALIGN(image_len * 4, SZ_1M), assuming the kernel compresses by no more than a factor of four. It then passes CONFIG_SYS_BOOTM_LEN, rather than the size of that buffer, to image_decomp() as the output limit. The decompressors honour the limit they are given, so a kernel that decompresses to more than four times its compressed size is written past the end of the allocated buffer and corrupts adjacent memory.

Quoted 7 lines

Pass the allocation size to image_decomp() and handle_decomp_error() so decompression stops at the buffer boundary and fails cleanly when the image is too large, instead of overflowing. The regular non-noload paths are unchanged and continue to use CONFIG_SYS_BOOTM_LEN.

Fixes 69544c4fd8b1 ("bootm: Support kernel_noload with compression")
Signature · 1 line
Tom
Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH v1 1/2] bootm: fix overflow of the noload kernel decompression buffer

In reply to Aristo Chen

Hi Aristo,

Quoted 24 lines · Aristo Chen

bootm: fix overflow of the noload kernel decompression buffer

For a compressed kernel_noload image, bootm_load_os() allocates a decompression buffer sized to ALIGN(image_len * 4, SZ_1M), assuming the kernel compresses by no more than a factor of four. It then passes CONFIG_SYS_BOOTM_LEN, rather than the size of that buffer, to image_decomp() as the output limit. The decompressors honour the limit they are given, so a kernel that decompresses to more than four times its compressed size is written past the end of the allocated buffer and corrupts adjacent memory.

Pass the allocation size to image_decomp() and handle_decomp_error() so decompression stops at the buffer boundary and fails cleanly when the image is too large, instead of overflowing. The regular non-noload paths are unchanged and continue to use CONFIG_SYS_BOOTM_LEN.

Fixes 69544c4fd8b1 ("bootm: Support kernel_noload with compression")

boot/bootm.c | 11 ++++++-----

 1 file changed, 6 insertions(+), 5 deletions(-)

boot/bootm.c static int bootm_load_os(struct bootm_headers *images, int boot_progress) · line 643

Quoted 10 lines
boot/bootm.c +2 -2
@@ -635,17 +636,17 @@ static int bootm_load_os(struct bootm_headers *images, int boot_progress)
      load_buf = map_sysmem(load, 0);
      image_buf = map_sysmem(os.image_start, image_len);
      err = image_decomp(os.comp, load, os.image_start, os.type,
                         load_buf, image_buf, image_len,
-                        CONFIG_SYS_BOOTM_LEN, &load_end);
+                        decomp_len, &load_end);
      if (err) {
              err = handle_decomp_error(os.comp, load_end - load,
-                                       CONFIG_SYS_BOOTM_LEN, err);
+                                       decomp_len, err);

handle_decomp_error() prints

Image too large: increase CONFIG_SYS_BOOTM_LEN

when uncomp_size >= buf_size, which is the path the new test exercises. In the noload case that advice is misleading - the limit is the per-image ALIGN(image_len * 4, SZ_1M) buffer, not CONFIG_SYS_BOOTM_LEN, and bumping CONFIG_SYS_BOOTM_LEN will not help.

Worth either tweaking the message to mention the 4x noload buffer, or printing a separate note from bootm_load_os() when decomp_len != CONFIG_SYS_BOOTM_LEN. What do you think?

boot/bootm.c static int bootm_load_os(struct bootm_headers *images, int boot_progress) · line 629

Quoted 9 lines
boot/bootm.c +1 -1
@@ -623,11 +624,11 @@ static int bootm_load_os(struct bootm_headers *images, int boot_progress)
       * Use an alignment of 2MB since this might help arm64
       */
      if (os.type == IH_TYPE_KERNEL_NOLOAD && os.comp != IH_COMP_NONE) {
-             ulong req_size = ALIGN(image_len * 4, SZ_1M);
              phys_addr_t addr;
 
+             decomp_len = ALIGN(image_len * 4, SZ_1M);

It might be worth updating image_decomp() to take a ulong size, since now a read per-image size is flowing through (perhaps a follow-up?).

Regards,
Simon

#4 lore raw

Re: [PATCH v1 1/2] bootm: fix overflow of the noload kernel decompression buffer

Hi Simon,

Quoted 43 lines · Simon Glass

Hi Aristo,

Quoted 24 lines · Aristo Chen

bootm: fix overflow of the noload kernel decompression buffer

For a compressed kernel_noload image, bootm_load_os() allocates a decompression buffer sized to ALIGN(image_len * 4, SZ_1M), assuming the kernel compresses by no more than a factor of four. It then passes CONFIG_SYS_BOOTM_LEN, rather than the size of that buffer, to image_decomp() as the output limit. The decompressors honour the limit they are given, so a kernel that decompresses to more than four times its compressed size is written past the end of the allocated buffer and corrupts adjacent memory.

Pass the allocation size to image_decomp() and handle_decomp_error() so decompression stops at the buffer boundary and fails cleanly when the image is too large, instead of overflowing. The regular non-noload paths are unchanged and continue to use CONFIG_SYS_BOOTM_LEN.

Fixes 69544c4fd8b1 ("bootm: Support kernel_noload with compression")

boot/bootm.c | 11 ++++++-----

 1 file changed, 6 insertions(+), 5 deletions(-)

boot/bootm.c static int bootm_load_os(struct bootm_headers *images, int boot_progress) · line 643

Quoted 10 lines
boot/bootm.c +2 -2
@@ -635,17 +636,17 @@ static int bootm_load_os(struct bootm_headers *images, int boot_progress)
      load_buf = map_sysmem(load, 0);
      image_buf = map_sysmem(os.image_start, image_len);
      err = image_decomp(os.comp, load, os.image_start, os.type,
                         load_buf, image_buf, image_len,
-                        CONFIG_SYS_BOOTM_LEN, &load_end);
+                        decomp_len, &load_end);
      if (err) {
              err = handle_decomp_error(os.comp, load_end - load,
-                                       CONFIG_SYS_BOOTM_LEN, err);
+                                       decomp_len, err);

handle_decomp_error() prints

Image too large: increase CONFIG_SYS_BOOTM_LEN

Quoted 8 lines

when uncomp_size >= buf_size, which is the path the new test exercises. In the noload case that advice is misleading - the limit is the per-image ALIGN(image_len * 4, SZ_1M) buffer, not CONFIG_SYS_BOOTM_LEN, and bumping CONFIG_SYS_BOOTM_LEN will not help.

Worth either tweaking the message to mention the 4x noload buffer, or printing a separate note from bootm_load_os() when decomp_len != CONFIG_SYS_BOOTM_LEN. What do you think?

I will prepare a v2 to tweak the message

Quoted 9 lines
Quoted 9 lines
boot/bootm.c +0 -1
@@ -623,11 +624,11 @@ static int bootm_load_os(struct bootm_headers *images, int boot_progress)
       * Use an alignment of 2MB since this might help arm64
       */
      if (os.type == IH_TYPE_KERNEL_NOLOAD && os.comp != IH_COMP_NONE) {
-             ulong req_size = ALIGN(image_len * 4, SZ_1M);
              phys_addr_t addr;
 
Quoted 4 lines

boot/bootm.c static int bootm_load_os(struct bootm_headers *images, int boot_progress) · line 629

Quoted 1 line
boot/bootm.c +1 -0
+             decomp_len = ALIGN(image_len * 4, SZ_1M);

It might be worth updating image_decomp() to take a ulong size, since now a read per-image size is flowing through (perhaps a follow-up?).

I've written it down in my notes; I will see how to tackle this with a follow-up patch set

Quoted 5 lines

Regards,
Simon

Best regards,
Aristo

#5 lore raw

PATCH 2/2 test/py: test kernel_noload decompression buffer overflow

In reply to Aristo Chen

Add a sandbox test that builds a FIT containing a compressed kernel_noload kernel whose uncompressed size far exceeds four times its compressed size. bootm_load_os() sizes the decompression buffer to ALIGN(image_len * 4, SZ_1M), so such an image must be rejected with a decompression error rather than overflowing the buffer.

The test verifies that 'bootm loados' reports the failure instead of decompressing past the end of the buffer.

---
 test/py/tests/test_fit.py | 84 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 84 insertions(+)
test/py/tests/test_fit.py +84 -0
index 4f56a1421e1..04e10c33046 100755
@@ -117,6 +117,36 @@ host save hostfs 0 %(loadables1_addr)x %(loadables1_out)s %(loadables1_size)x
 host save hostfs 0 %(loadables2_addr)x %(loadables2_out)s %(loadables2_size)x
 '''
 
+# A minimal ITS for a compressed 'kernel_noload' kernel. bootm allocates its
+# own decompression buffer for this image type, sized to 4x the compressed
+# length; see test_fit_kernel_noload_decomp_overflow().
+NOLOAD_ITS = '''
+/dts-v1/;
+
+/ {
+        description = "FIT with a compressed kernel_noload image";
+        #address-cells = <1>;
+
+        images {
+                kernel-1 {
+                        data = /incbin/("%(kernel)s");
+                        type = "kernel_noload";
+                        arch = "sandbox";
+                        os = "linux";
+                        compression = "gzip";
+                        load = <0>;
+                        entry = <0>;
+                };
+        };
+        configurations {
+                default = "conf-1";
+                conf-1 {
+                        kernel = "kernel-1";
+                };
+        };
+};
+'''
+
 @pytest.mark.boardspec('sandbox')
 @pytest.mark.buildconfigspec('fit')
 @pytest.mark.requiredtool('dtc')
@@ -426,3 +456,57 @@ class TestFitImage:
 
         output = ubman.run_command_list(cmds)
         assert "can't get kernel image!" in '\n'.join(output)
+
+    def test_fit_kernel_noload_decomp_overflow(self, ubman, fsetup):
+        """Test that an over-large compressed kernel_noload image is rejected
+
+        For a compressed 'kernel_noload' kernel, bootm_load_os() allocates a
+        decompression buffer of ALIGN(image_len * 4, SZ_1M) and must bound the
+        decompressor by that buffer. A kernel that decompresses to far more
+        than four times its compressed size must therefore fail with a
+        decompression error instead of overflowing the buffer.
+        """
+        sz_1m = 1 << 20
+
+        # CONFIG_SYS_BOOTM_LEN is the global decompression limit. Keep the
+        # uncompressed size below it, so the failure is forced by the smaller
+        # per-image kernel_noload buffer rather than by that global limit.
+        bootm_len = int(ubman.config.buildconfig['config_sys_bootm_len'], 0)
+
+        # 4MB of zeros compresses to a few KB, so the decompression buffer
+        # (ALIGN(image_len * 4, SZ_1M), i.e. 1MB here) ends up far smaller
+        # than the uncompressed image.
+        decomp_size = 4 * sz_1m
+        kernel = fit_util.make_fname(ubman, 'test-noload-kernel.bin')
+        with open(kernel, 'wb') as fd:
+            fd.write(b'\0' * decomp_size)
+        kernel_gz = self.make_compressed(ubman, kernel)
+
+        image_len = self.filesize(kernel_gz)
+        req_size = (image_len * 4 + sz_1m - 1) // sz_1m * sz_1m
+        assert req_size < decomp_size <= bootm_len, (
+            'Test setup error: need decomp buffer (%#x) < image (%#x) <= '
+            'CONFIG_SYS_BOOTM_LEN (%#x)' % (req_size, decomp_size, bootm_len))
+
+        fit = fit_util.make_fit(ubman, fsetup['mkimage'], NOLOAD_ITS,
+                                {'kernel': kernel_gz})
+        fit_addr = fsetup['fit_addr']
+
+        ubman.run_command_list([
+            'host load hostfs 0 %x %s' % (fit_addr, fit),
+            'bootm start %x' % fit_addr,
+        ])
+
+        # 'bootm loados' decompresses the kernel. Decompression must stop at
+        # the buffer boundary and report 'Image too large', which resets the
+        # board; it must not run past the buffer and return to the prompt.
+        ubman.run_command('bootm loados', wait_for_prompt=False)
+        matched = ubman.p.expect(['Image too large', ubman.prompt_compiled])
+
+        # The decompression failure resets the board; bring up a fresh
+        # instance so later tests start from a clean console.
+        ubman.restart_uboot()
+
+        assert matched == 0, \
+            "'bootm loados' did not reject a kernel_noload image whose " \
+            'decompressed size overflows its decompression buffer'
Signature · 1 line
2.43.0
Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH v1 2/2] test/py: test kernel_noload decompression buffer overflow

Quoted 8 lines · Aristo Chen

test/py: test kernel_noload decompression buffer overflow

Add a sandbox test that builds a FIT containing a compressed kernel_noload kernel whose uncompressed size far exceeds four times its compressed size. bootm_load_os() sizes the decompression buffer to ALIGN(image_len * 4, SZ_1M), so such an image must be rejected with a decompression error rather than overflowing the buffer.

Quoted 7 lines

The test verifies that 'bootm loados' reports the failure instead of decompressing past the end of the buffer.

test/py/tests/test_fit.py | 84 +++++++++++++++++++++++++++++++++++++++++++++++

 1 file changed, 84 insertions(+)
#7 lore raw
In reply to Aristo Chen

Hi Aristo,

Quoted 4 lines · Aristo Chen

A 4x compression ratio is at the edge of what modern compressors (zstd, xz) achieve on real kernels, and is trivially exceeded by crafted, highly compressible payloads, so this is reachable both accidentally and intentionally.

In that case, perhaps we should have a third patch that either bumps the multiplier (8x feels safer and still bounded), or makes the headroom CONFIG-tunable, so users with well-compressed kernels do not have to bisect to find out why their board stopped booting?

I believe in each case it is also possible to find out the decomp size by looking at the header.

Quoted 3 lines

Patch 2 adds a sandbox py-test that builds a FIT with a compressed kernel_noload image whose decompressed size exceeds the per-image buffer

Just an idea...you could also perhaps add a test for the boundary - i.e. an image exactly at the buffer limit.

Regards,
Simon

#8 lore raw

v2 cover bootm: bound noload kernel decompression to the allocated buffer

In reply to Aristo Chen

For a compressed kernel_noload image, bootm_load_os() allocates a decompression buffer of ALIGN(image_len * 4, SZ_1M) and then passes CONFIG_SYS_BOOTM_LEN (typically 128 MiB on arm64) to image_decomp() as the output limit. The decompressors honour whatever limit they are given, so a kernel that decompresses to more than four times its compressed size runs past the end of the allocated buffer and silently corrupts adjacent memory.

A 4x compression ratio is at the edge of what modern compressors (zstd, xz) achieve on real kernels, and is trivially exceeded by crafted, highly compressible payloads, so this is reachable both accidentally and intentionally. The overflow can land on already-loaded boot artefacts (FDT, ramdisk, loadables), U-Boot's own data, or memory-mapped device registers; the existing post-decompression overlap check in bootm_load_os() only catches overlap with the FIT itself.

Patch 1 plumbs the actual allocation size through to image_decomp() and handle_decomp_error() via a single decomp_len variable, so decompression stops at the buffer boundary and fails cleanly when the image is too large. The non-noload code path is unchanged and continues to use CONFIG_SYS_BOOTM_LEN. A clarifying note is printed when the failure is gated by the per-image buffer, so the generic "increase CONFIG_SYS_BOOTM_LEN" advice does not mislead.

Patch 2 adds two sandbox py-tests against the per-image buffer: one that exceeds the buffer and must be rejected, and one that matches the buffer exactly and must succeed (guarding the boundary).

Patch 3 raises the noload-decompression headroom from 4x to 8x. The 4x factor is at the edge of what zstd and xz achieve on real kernels, so well-compressed vendor kernels can fail to boot at runtime once the bound is enforced. 8x covers them comfortably while remaining bounded. The matching references in patch 2's regression tests are updated to the new bound in the same commit so every patch in the series is internally consistent.

Tested on sandbox: both new tests pass; the existing test_fit_compressed_images_load (which covers the load-address path) and the other tests in test/py/tests/test_fit.py continue to pass.

Changes in v2:
- Patch 1: also print the per-image buffer size from bootm_load_os() when the failure is gated by the noload buffer, so handle_decomp_error()'s generic "increase CONFIG_SYS_BOOTM_LEN" advice does not mislead.
- Patch 2: add a second sandbox test covering the buffer-limit boundary, in addition to the existing overflow test.
- New patch 3: raise the noload-decompression headroom from 4x to 8x so well-compressed kernels do not have to bisect to find out why their board stopped booting; the same commit also updates the regression test references so each patch stays self-consistent.

Aristo Chen (3):
bootm: fix overflow of the noload kernel decompression buffer test/py: test kernel_noload decompression buffer overflow
bootm: increase kernel_noload decompression headroom from 4x to 8x

 boot/bootm.c              |  18 +++---
 test/py/tests/test_fit.py | 126 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 137 insertions(+), 7 deletions(-)
Signature · 1 line
2.43.0
#9 lore raw

PATCH v2 1/3 bootm: fix overflow of the noload kernel decompression buffer

For a compressed kernel_noload image, bootm_load_os() allocates a decompression buffer sized to ALIGN(image_len * 4, SZ_1M), assuming the kernel compresses by no more than a factor of four. It then passes CONFIG_SYS_BOOTM_LEN, rather than the size of that buffer, to image_decomp() as the output limit. The decompressors honour the limit they are given, so a kernel that decompresses to more than four times its compressed size is written past the end of the allocated buffer and corrupts adjacent memory.

Pass the allocation size to image_decomp() and handle_decomp_error() so decompression stops at the buffer boundary and fails cleanly when the image is too large, instead of overflowing. The regular non-noload paths are unchanged and continue to use CONFIG_SYS_BOOTM_LEN. When the failure is triggered by the smaller per-image buffer, print a note so that handle_decomp_error()'s generic advice to increase CONFIG_SYS_BOOTM_LEN does not mislead the reader.

Fixes 69544c4fd8b1 ("bootm: Support kernel_noload with compression")
---
 boot/bootm.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
boot/bootm.c +9 -5
index 4836d6b2d41..f9b81bbf794 100644
@@ -610,6 +610,7 @@ static int bootm_load_os(struct bootm_headers *images, int boot_progress)
 	ulong blob_end = os.end;
 	ulong image_start = os.image_start;
 	ulong image_len = os.image_len;
+	ulong decomp_len = CONFIG_SYS_BOOTM_LEN;
 	ulong flush_start = ALIGN_DOWN(load, ARCH_DMA_MINALIGN);
 	bool no_overlap;
 	void *load_buf, *image_buf;
@@ -623,11 +624,11 @@ static int bootm_load_os(struct bootm_headers *images, int boot_progress)
 	 * Use an alignment of 2MB since this might help arm64
 	 */
 	if (os.type == IH_TYPE_KERNEL_NOLOAD && os.comp != IH_COMP_NONE) {
-		ulong req_size = ALIGN(image_len * 4, SZ_1M);
 		phys_addr_t addr;
 
+		decomp_len = ALIGN(image_len * 4, SZ_1M);
 		err = lmb_alloc_mem(LMB_MEM_ALLOC_ANY, SZ_2M, &addr,
-				    req_size, LMB_NONE);
+				    decomp_len, LMB_NONE);
 		if (err)
 			return 1;
 
@@ -635,17 +636,20 @@ static int bootm_load_os(struct bootm_headers *images, int boot_progress)
 		images->os.load = (ulong)addr;
 		images->ep = (ulong)addr;
 		debug("Allocated %lx bytes at %lx for kernel (size %lx) decompression\n",
-		      req_size, load, image_len);
+		      decomp_len, load, image_len);
 	}
 
 	load_buf = map_sysmem(load, 0);
 	image_buf = map_sysmem(os.image_start, image_len);
 	err = image_decomp(os.comp, load, os.image_start, os.type,
 			   load_buf, image_buf, image_len,
-			   CONFIG_SYS_BOOTM_LEN, &load_end);
+			   decomp_len, &load_end);
 	if (err) {
 		err = handle_decomp_error(os.comp, load_end - load,
-					  CONFIG_SYS_BOOTM_LEN, err);
+					  decomp_len, err);
+		if (decomp_len != CONFIG_SYS_BOOTM_LEN)
+			printf("Note: noload decompression buffer is %#lx bytes (not CONFIG_SYS_BOOTM_LEN)\n",
+			       decomp_len);
 		bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
 		return err;
 	}
Signature · 1 line
2.43.0
Reviewed-by on v2 1/3
#10 lore raw

Re: [PATCH v2 1/3] bootm: fix overflow of the noload kernel decompression buffer

Hi Aristo,

Quoted 28 lines · Aristo Chen

bootm: fix overflow of the noload kernel decompression buffer

For a compressed kernel_noload image, bootm_load_os() allocates a decompression buffer sized to ALIGN(image_len * 4, SZ_1M), assuming the kernel compresses by no more than a factor of four. It then passes CONFIG_SYS_BOOTM_LEN, rather than the size of that buffer, to image_decomp() as the output limit. The decompressors honour the limit they are given, so a kernel that decompresses to more than four times its compressed size is written past the end of the allocated buffer and corrupts adjacent memory.

Pass the allocation size to image_decomp() and handle_decomp_error() so decompression stops at the buffer boundary and fails cleanly when the image is too large, instead of overflowing. The regular non-noload paths are unchanged and continue to use CONFIG_SYS_BOOTM_LEN. When the failure is triggered by the smaller per-image buffer, print a note so that handle_decomp_error()'s generic advice to increase CONFIG_SYS_BOOTM_LEN does not mislead the reader.

Fixes 69544c4fd8b1 ("bootm: Support kernel_noload with compression")
[...]

boot/bootm.c | 14 +++++++++-----

 1 file changed, 9 insertions(+), 5 deletions(-)
boot/bootm.c +0 -0
@@ -635,17 +636,20 @@ static int bootm_load_os(struct bootm_headers *images, int boot_progress)
      err = image_decomp(os.comp, load, os.image_start, os.type,

boot/bootm.c static int bootm_load_os(struct bootm_headers *images, int boot_progress) · line 644

Quoted 10 lines
boot/bootm.c +5 -2
                         load_buf, image_buf, image_len,
-                        CONFIG_SYS_BOOTM_LEN, &load_end);
+                        decomp_len, &load_end);
      if (err) {
              err = handle_decomp_error(os.comp, load_end - load,
-                                       CONFIG_SYS_BOOTM_LEN, err);
+                                       decomp_len, err);
+             if (decomp_len != CONFIG_SYS_BOOTM_LEN)
+                     printf("Note: noload decompression buffer is %#lx bytes (not CONFIG_SYS_BOOTM_LEN)\n",
+                            decomp_len);

The note is printed after handle_decomp_error() has already produced:

Image too large: increase CONFIG_SYS_BOOTM_LEN Must RESET board to recover

So I think this is worth a follow-up, too...see also below:

Using decomp_len != CONFIG_SYS_BOOTM_LEN as the gate is indirect - it relies on the alignment never coincidentally matching the global limit. The condition you actually want is the one that allocated the per-image buffer (IH_TYPE_KERNEL_NOLOAD and os.comp != IH_COMP_NONE), so testing that directly (or a bool set inside the allocation block) reads more obviously and stays correct regardless of how CONFIG_SYS_BOOTM_LEN is configured.

The cleaner fix is to teach handle_decomp_error() itself about the noload buffer (e.g. an extra flag or a separate label for the limit), so the 'Image too large: increase CONFIG_SYS_BOOTM_LEN' line never gets printed when the noload buffer is the true bound. The trailing 'Note:' is essentially patching over a message handle_decomp_error() should not have emitted. What do you think?

Regards,
Simon

#11 lore raw

Re: [PATCH v2 1/3] bootm: fix overflow of the noload kernel decompression buffer

Hi Simon,

Quoted 45 lines · Simon Glass

Hi Aristo,

Quoted 28 lines · Aristo Chen

bootm: fix overflow of the noload kernel decompression buffer

For a compressed kernel_noload image, bootm_load_os() allocates a decompression buffer sized to ALIGN(image_len * 4, SZ_1M), assuming the kernel compresses by no more than a factor of four. It then passes CONFIG_SYS_BOOTM_LEN, rather than the size of that buffer, to image_decomp() as the output limit. The decompressors honour the limit they are given, so a kernel that decompresses to more than four times its compressed size is written past the end of the allocated buffer and corrupts adjacent memory.

Pass the allocation size to image_decomp() and handle_decomp_error() so decompression stops at the buffer boundary and fails cleanly when the image is too large, instead of overflowing. The regular non-noload paths are unchanged and continue to use CONFIG_SYS_BOOTM_LEN. When the failure is triggered by the smaller per-image buffer, print a note so that handle_decomp_error()'s generic advice to increase CONFIG_SYS_BOOTM_LEN does not mislead the reader.

Fixes 69544c4fd8b1 ("bootm: Support kernel_noload with compression")
[...]

boot/bootm.c | 14 +++++++++-----

 1 file changed, 9 insertions(+), 5 deletions(-)
boot/bootm.c +0 -0
@@ -635,17 +636,20 @@ static int bootm_load_os(struct bootm_headers *images, int boot_progress)
      err = image_decomp(os.comp, load, os.image_start, os.type,

boot/bootm.c static int bootm_load_os(struct bootm_headers *images, int boot_progress) · line 644

Quoted 10 lines
boot/bootm.c +5 -2
                         load_buf, image_buf, image_len,
-                        CONFIG_SYS_BOOTM_LEN, &load_end);
+                        decomp_len, &load_end);
      if (err) {
              err = handle_decomp_error(os.comp, load_end - load,
-                                       CONFIG_SYS_BOOTM_LEN, err);
+                                       decomp_len, err);
+             if (decomp_len != CONFIG_SYS_BOOTM_LEN)
+                     printf("Note: noload decompression buffer is %#lx bytes (not CONFIG_SYS_BOOTM_LEN)\n",
+                            decomp_len);
Quoted 6 lines

The note is printed after handle_decomp_error() has already produced:

Image too large: increase CONFIG_SYS_BOOTM_LEN Must RESET board to recover

So I think this is worth a follow-up, too...see also below:

Noted, I will find time to see how to fix this

Quoted 16 lines

Using decomp_len != CONFIG_SYS_BOOTM_LEN as the gate is indirect - it relies on the alignment never coincidentally matching the global limit. The condition you actually want is the one that allocated the per-image buffer (IH_TYPE_KERNEL_NOLOAD and os.comp != IH_COMP_NONE), so testing that directly (or a bool set inside the allocation block) reads more obviously and stays correct regardless of how CONFIG_SYS_BOOTM_LEN is configured.

The cleaner fix is to teach handle_decomp_error() itself about the noload buffer (e.g. an extra flag or a separate label for the limit), so the 'Image too large: increase CONFIG_SYS_BOOTM_LEN' line never gets printed when the noload buffer is the true bound. The trailing 'Note:' is essentially patching over a message handle_decomp_error() should not have emitted. What do you think?

Quoted 2 lines

Regards,
Simon

Regards,
Aristo

#12 lore raw

PATCH v2 2/3 test/py: test kernel_noload decompression buffer overflow

In reply to Aristo Chen

Add sandbox tests that exercise the per-image decompression buffer that bootm_load_os() allocates for a compressed kernel_noload image (ALIGN(image_len * 4, SZ_1M)).

The overflow test builds a FIT whose decompressed size far exceeds the per-image buffer and asserts that 'bootm loados' rejects it with a decompression error rather than overflowing.

The boundary test builds a FIT whose decompressed size equals the per-image buffer exactly and asserts that 'bootm loados' succeeds, guarding against an off-by-one rejection at the buffer limit.

---
 test/py/tests/test_fit.py | 126 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 126 insertions(+)
test/py/tests/test_fit.py +126 -0
index 4f56a1421e1..560b7513e67 100755
@@ -117,6 +117,36 @@ host save hostfs 0 %(loadables1_addr)x %(loadables1_out)s %(loadables1_size)x
 host save hostfs 0 %(loadables2_addr)x %(loadables2_out)s %(loadables2_size)x
 '''
 
+# A minimal ITS for a compressed 'kernel_noload' kernel. bootm allocates a
+# per-image decompression buffer for this image type, sized as a multiple of
+# the compressed length; see the test_fit_kernel_noload_decomp_* tests.
+NOLOAD_ITS = '''
+/dts-v1/;
+
+/ {
+        description = "FIT with a compressed kernel_noload image";
+        #address-cells = <1>;
+
+        images {
+                kernel-1 {
+                        data = /incbin/("%(kernel)s");
+                        type = "kernel_noload";
+                        arch = "sandbox";
+                        os = "linux";
+                        compression = "gzip";
+                        load = <0>;
+                        entry = <0>;
+                };
+        };
+        configurations {
+                default = "conf-1";
+                conf-1 {
+                        kernel = "kernel-1";
+                };
+        };
+};
+'''
+
 @pytest.mark.boardspec('sandbox')
 @pytest.mark.buildconfigspec('fit')
 @pytest.mark.requiredtool('dtc')
@@ -426,3 +456,99 @@ class TestFitImage:
 
         output = ubman.run_command_list(cmds)
         assert "can't get kernel image!" in '\n'.join(output)
+
+    def test_fit_kernel_noload_decomp_overflow(self, ubman, fsetup):
+        """Test that an over-large compressed kernel_noload image is rejected
+
+        For a compressed 'kernel_noload' kernel, bootm_load_os() allocates a
+        decompression buffer of ALIGN(image_len * 4, SZ_1M) and must bound the
+        decompressor by that buffer. A kernel that decompresses to far more
+        than four times its compressed size must therefore fail with a
+        decompression error instead of overflowing the buffer.
+        """
+        sz_1m = 1 << 20
+
+        # CONFIG_SYS_BOOTM_LEN is the global decompression limit. Keep the
+        # uncompressed size below it, so the failure is forced by the smaller
+        # per-image kernel_noload buffer rather than by that global limit.
+        bootm_len = int(ubman.config.buildconfig['config_sys_bootm_len'], 0)
+
+        # 4MB of zeros compresses to a few KB, so the decompression buffer
+        # (ALIGN(image_len * 4, SZ_1M), i.e. 1MB here) ends up far smaller
+        # than the uncompressed image.
+        decomp_size = 4 * sz_1m
+        kernel = fit_util.make_fname(ubman, 'test-noload-kernel.bin')
+        with open(kernel, 'wb') as fd:
+            fd.write(b'\0' * decomp_size)
+        kernel_gz = self.make_compressed(ubman, kernel)
+
+        image_len = self.filesize(kernel_gz)
+        req_size = (image_len * 4 + sz_1m - 1) // sz_1m * sz_1m
+        assert req_size < decomp_size <= bootm_len, (
+            'Test setup error: need decomp buffer (%#x) < image (%#x) <= '
+            'CONFIG_SYS_BOOTM_LEN (%#x)' % (req_size, decomp_size, bootm_len))
+
+        fit = fit_util.make_fit(ubman, fsetup['mkimage'], NOLOAD_ITS,
+                                {'kernel': kernel_gz})
+        fit_addr = fsetup['fit_addr']
+
+        ubman.run_command_list([
+            'host load hostfs 0 %x %s' % (fit_addr, fit),
+            'bootm start %x' % fit_addr,
+        ])
+
+        # 'bootm loados' decompresses the kernel. Decompression must stop at
+        # the buffer boundary and report 'Image too large', which resets the
+        # board; it must not run past the buffer and return to the prompt.
+        ubman.run_command('bootm loados', wait_for_prompt=False)
+        matched = ubman.p.expect(['Image too large', ubman.prompt_compiled])
+
+        # The decompression failure resets the board; bring up a fresh
+        # instance so later tests start from a clean console.
+        ubman.restart_uboot()
+
+        assert matched == 0, \
+            "'bootm loados' did not reject a kernel_noload image whose " \
+            'decompressed size overflows its decompression buffer'
+
+    def test_fit_kernel_noload_decomp_boundary(self, ubman, fsetup):
+        """Test that decompression succeeds exactly at the buffer limit
+
+        For a compressed 'kernel_noload' kernel, bootm_load_os() allocates a
+        decompression buffer of ALIGN(image_len * 4, SZ_1M). A kernel whose
+        decompressed size equals that buffer exactly must succeed, guarding
+        against an off-by-one rejection at the buffer limit.
+        """
+        sz_1m = 1 << 20
+
+        # 1MiB of zeros compresses to a few KB, so image_len * 4 rounds up to
+        # exactly 1MiB. Picking decomp_size = 1MiB makes the decompressed size
+        # match the buffer exactly.
+        decomp_size = sz_1m
+        kernel = fit_util.make_fname(ubman, 'test-noload-kernel-boundary.bin')
+        with open(kernel, 'wb') as fd:
+            fd.write(b'\0' * decomp_size)
+        kernel_gz = self.make_compressed(ubman, kernel)
+
+        image_len = self.filesize(kernel_gz)
+        req_size = (image_len * 4 + sz_1m - 1) // sz_1m * sz_1m
+        assert decomp_size == req_size, (
+            'Test setup error: need decomp_size (%#x) == req_size (%#x)'
+            % (decomp_size, req_size))
+
+        fit = fit_util.make_fit(ubman, fsetup['mkimage'], NOLOAD_ITS,
+                                {'kernel': kernel_gz},
+                                basename='test-noload-boundary.fit')
+        fit_addr = fsetup['fit_addr']
+
+        # Decompression at the buffer limit must succeed, returning to the
+        # prompt cleanly and never printing 'Image too large'.
+        output = ubman.run_command_list([
+            'host load hostfs 0 %x %s' % (fit_addr, fit),
+            'bootm start %x' % fit_addr,
+            'bootm loados',
+        ])
+        text = '\n'.join(output)
+        assert 'Image too large' not in text, (
+            "'bootm loados' rejected a kernel_noload image whose decompressed "
+            'size matches its buffer exactly: %s' % text)
Signature · 1 line
2.43.0
#13 lore raw

Re: [PATCH v2 2/3] test/py: test kernel_noload decompression buffer overflow

Hi Aristo,

Quoted 10 lines · Aristo Chen

test/py: test kernel_noload decompression buffer overflow

Add sandbox tests that exercise the per-image decompression buffer that bootm_load_os() allocates for a compressed kernel_noload image (ALIGN(image_len * 4, SZ_1M)).

The overflow test builds a FIT whose decompressed size far exceeds the per-image buffer and asserts that 'bootm loados' rejects it with a decompression error rather than overflowing.

Quoted 8 lines

The boundary test builds a FIT whose decompressed size equals the per-image buffer exactly and asserts that 'bootm loados' succeeds, guarding against an off-by-one rejection at the buffer limit.

test/py/tests/test_fit.py | 126 ++++++++++++++++++++++++++++++++++++++++++++++

 1 file changed, 126 insertions(+)

You didn't pick up my review tag so I hope you don't mind a few more nits

test/py/tests/test_fit.py class TestFitImage: · line 457

Quoted 4 lines
test/py/tests/test_fit.py +2 -0
@@ -426,3 +456,99 @@ class TestFitImage:
+        ubman.run_command('bootm loados', wait_for_prompt=False)
+        matched = ubman.p.expect(['Image too large', ubman.prompt_compiled])

Please use ubman.expect() rather than reaching into ubman.p directly

test/py/tests/test_fit.py host save hostfs 0 %(loadables1_addr)x %(loadables1_out)s %(loadables1_size)x · line 120

Quoted 6 lines
test/py/tests/test_fit.py +4 -0
@@ -117,6 +117,36 @@ host save hostfs 0 %(loadables1_addr)x %(loadables1_out)s %(loadables1_size)x
+# A minimal ITS for a compressed 'kernel_noload' kernel. bootm allocates a
+# per-image decompression buffer for this image type, sized as a multiple of
+# the compressed length; see the test_fit_kernel_noload_decomp_* tests.
+NOLOAD_ITS = '''

The test could use a buildconfigspec('gzip')

Regards,
Simon

#14 lore raw

Re: [PATCH v2 2/3] test/py: test kernel_noload decompression buffer overflow

Hi Simon,

Quoted 18 lines · Simon Glass

Hi Aristo,

Quoted 14 lines · Aristo Chen

test/py: test kernel_noload decompression buffer overflow

Add sandbox tests that exercise the per-image decompression buffer that bootm_load_os() allocates for a compressed kernel_noload image (ALIGN(image_len * 4, SZ_1M)).

The overflow test builds a FIT whose decompressed size far exceeds the per-image buffer and asserts that 'bootm loados' rejects it with a decompression error rather than overflowing.

The boundary test builds a FIT whose decompressed size equals the per-image buffer exactly and asserts that 'bootm loados' succeeds, guarding against an off-by-one rejection at the buffer limit.

Quoted 6 lines
Quoted 4 lines

test/py/tests/test_fit.py | 126 ++++++++++++++++++++++++++++++++++++++++++++++

 1 file changed, 126 insertions(+)

You didn't pick up my review tag so I hope you don't mind a few more nits

Sure, I don't mind, I will fix them :)

Quoted 17 lines

test/py/tests/test_fit.py class TestFitImage: · line 457

Quoted 5 lines
test/py/tests/test_fit.py +2 -0
@@ -426,3 +456,99 @@ class TestFitImage:
+        ubman.run_command('bootm loados', wait_for_prompt=False)
+        matched = ubman.p.expect(['Image too large', ubman.prompt_compiled])

Please use ubman.expect() rather than reaching into ubman.p directly

test/py/tests/test_fit.py host save hostfs 0 %(loadables1_addr)x %(loadables1_out)s %(loadables1_size)x · line 120

Quoted 6 lines
test/py/tests/test_fit.py +4 -0
@@ -117,6 +117,36 @@ host save hostfs 0 %(loadables1_addr)x %(loadables1_out)s %(loadables1_size)x
+# A minimal ITS for a compressed 'kernel_noload' kernel. bootm allocates a
+# per-image decompression buffer for this image type, sized as a multiple of
+# the compressed length; see the test_fit_kernel_noload_decomp_* tests.
+NOLOAD_ITS = '''

The test could use a buildconfigspec('gzip')

Quoted 2 lines

Regards,
Simon

Regards,
Aristo

#15 lore raw

PATCH v2 3/3 bootm: increase kernel_noload decompression headroom from 4x to 8x

In reply to Aristo Chen

For a compressed kernel_noload image, bootm_load_os() allocates a buffer of ALIGN(image_len * 4, SZ_1M). The 4x factor is at the edge of what modern compressors (zstd, xz) achieve on real kernels, so a well-compressed vendor kernel can fail to boot at runtime with no intervening warning.

Bump the headroom to 8x. The buffer is still bounded by the compressed image size, and the SZ_1M alignment keeps the overhead below 1 MiB on small kernels. Update the matching references in the kernel_noload sandbox tests so they stay in sync with the bound bootm_load_os() enforces.

---
 boot/bootm.c              |  6 +++---
 test/py/tests/test_fit.py | 14 +++++++-------
 2 files changed, 10 insertions(+), 10 deletions(-)
boot/bootm.c +3 -3
index f9b81bbf794..1d8bd30217f 100644
@@ -619,14 +619,14 @@ static int bootm_load_os(struct bootm_headers *images, int boot_progress)
 	/*
 	 * For a "noload" compressed kernel we need to allocate a buffer large
 	 * enough to decompress in to and use that as the load address now.
-	 * Assume that the kernel compression is at most a factor of 4 since
-	 * zstd almost achieves that.
+	 * Allow up to 8x compression: this comfortably covers what zstd and xz
+	 * achieve on real kernels, with headroom for well-compressed payloads.
 	 * Use an alignment of 2MB since this might help arm64
 	 */
 	if (os.type == IH_TYPE_KERNEL_NOLOAD && os.comp != IH_COMP_NONE) {
 		phys_addr_t addr;
 
-		decomp_len = ALIGN(image_len * 4, SZ_1M);
+		decomp_len = ALIGN(image_len * 8, SZ_1M);
 		err = lmb_alloc_mem(LMB_MEM_ALLOC_ANY, SZ_2M, &addr,
 				    decomp_len, LMB_NONE);
 		if (err)
test/py/tests/test_fit.py +7 -7
index 560b7513e67..deffadc8b3e 100755
@@ -461,9 +461,9 @@ class TestFitImage:
         """Test that an over-large compressed kernel_noload image is rejected
 
         For a compressed 'kernel_noload' kernel, bootm_load_os() allocates a
-        decompression buffer of ALIGN(image_len * 4, SZ_1M) and must bound the
+        decompression buffer of ALIGN(image_len * 8, SZ_1M) and must bound the
         decompressor by that buffer. A kernel that decompresses to far more
-        than four times its compressed size must therefore fail with a
+        than eight times its compressed size must therefore fail with a
         decompression error instead of overflowing the buffer.
         """
         sz_1m = 1 << 20
@@ -474,7 +474,7 @@ class TestFitImage:
         bootm_len = int(ubman.config.buildconfig['config_sys_bootm_len'], 0)
 
         # 4MB of zeros compresses to a few KB, so the decompression buffer
-        # (ALIGN(image_len * 4, SZ_1M), i.e. 1MB here) ends up far smaller
+        # (ALIGN(image_len * 8, SZ_1M), i.e. 1MB here) ends up far smaller
         # than the uncompressed image.
         decomp_size = 4 * sz_1m
         kernel = fit_util.make_fname(ubman, 'test-noload-kernel.bin')
@@ -483,7 +483,7 @@ class TestFitImage:
         kernel_gz = self.make_compressed(ubman, kernel)
 
         image_len = self.filesize(kernel_gz)
-        req_size = (image_len * 4 + sz_1m - 1) // sz_1m * sz_1m
+        req_size = (image_len * 8 + sz_1m - 1) // sz_1m * sz_1m
         assert req_size < decomp_size <= bootm_len, (
             'Test setup error: need decomp buffer (%#x) < image (%#x) <= '
             'CONFIG_SYS_BOOTM_LEN (%#x)' % (req_size, decomp_size, bootm_len))
@@ -515,13 +515,13 @@ class TestFitImage:
         """Test that decompression succeeds exactly at the buffer limit
 
         For a compressed 'kernel_noload' kernel, bootm_load_os() allocates a
-        decompression buffer of ALIGN(image_len * 4, SZ_1M). A kernel whose
+        decompression buffer of ALIGN(image_len * 8, SZ_1M). A kernel whose
         decompressed size equals that buffer exactly must succeed, guarding
         against an off-by-one rejection at the buffer limit.
         """
         sz_1m = 1 << 20
 
-        # 1MiB of zeros compresses to a few KB, so image_len * 4 rounds up to
+        # 1MiB of zeros compresses to a few KB, so image_len * 8 rounds up to
         # exactly 1MiB. Picking decomp_size = 1MiB makes the decompressed size
         # match the buffer exactly.
         decomp_size = sz_1m
@@ -531,7 +531,7 @@ class TestFitImage:
         kernel_gz = self.make_compressed(ubman, kernel)
 
         image_len = self.filesize(kernel_gz)
-        req_size = (image_len * 4 + sz_1m - 1) // sz_1m * sz_1m
+        req_size = (image_len * 8 + sz_1m - 1) // sz_1m * sz_1m
         assert decomp_size == req_size, (
             'Test setup error: need decomp_size (%#x) == req_size (%#x)'
             % (decomp_size, req_size))
Signature · 1 line
2.43.0
Reviewed-by on v2 3/3
#16 lore raw

Re: [PATCH v2 3/3] bootm: increase kernel_noload decompression headroom from 4x to 8x

Quoted 14 lines · Aristo Chen

bootm: increase kernel_noload decompression headroom from 4x to 8x

For a compressed kernel_noload image, bootm_load_os() allocates a buffer of ALIGN(image_len * 4, SZ_1M). The 4x factor is at the edge of what modern compressors (zstd, xz) achieve on real kernels, so a well-compressed vendor kernel can fail to boot at runtime with no intervening warning.

Bump the headroom to 8x. The buffer is still bounded by the compressed image size, and the SZ_1M alignment keeps the overhead below 1 MiB on small kernels. Update the matching references in the kernel_noload sandbox tests so they stay in sync with the bound bootm_load_os() enforces.

Quoted 6 lines

boot/bootm.c | 6 +++---

 test/py/tests/test_fit.py | 14 +++++++-------
 2 files changed, 10 insertions(+), 10 deletions(-)
#17 lore raw
In reply to Aristo Chen

Hi Aristo,

Quoted 4 lines · Aristo Chen

Patch 2 adds two sandbox py-tests against the per-image buffer [...] New patch 3: raise the noload-decompression headroom from 4x to 8x [...] the same commit also updates the regression test references so each patch stays self-consistent.

I suggest swapping patches 2 and 3: bump the multiplier first, then add the tests at the final 8x value. That way the test references are introduced once, instead of being written at 4x in patch 2 and rewritten at 8x in patch 3. Patch 1 stays first as the self-contained security fix for backporting.

Regards,
Simon

#18 lore raw

v3 cover bootm: bound noload kernel decompression to the allocated buffer

In reply to Aristo Chen

For a compressed kernel_noload image, bootm_load_os() allocates a decompression buffer of ALIGN(image_len * 4, SZ_1M) and then passes CONFIG_SYS_BOOTM_LEN (typically 128 MiB on arm64) to image_decomp() as the output limit. The decompressors honour whatever limit they are given, so a kernel that decompresses to more than four times its compressed size runs past the end of the allocated buffer and silently corrupts adjacent memory.

A 4x compression ratio is at the edge of what modern compressors (zstd, xz) achieve on real kernels, and is trivially exceeded by crafted, highly compressible payloads, so this is reachable both accidentally and intentionally. The overflow can land on already-loaded boot artefacts (FDT, ramdisk, loadables), U-Boot's own data, or memory-mapped device registers; the existing post-decompression overlap check in bootm_load_os() only catches overlap with the FIT itself.

Patch 1 plumbs the actual allocation size through to image_decomp() and handle_decomp_error() via a single decomp_len variable, so decompression stops at the buffer boundary and fails cleanly when the image is too large. The non-noload code path is unchanged and continues to use CONFIG_SYS_BOOTM_LEN. A clarifying note is printed when the failure is gated by the per-image buffer, so the generic "increase CONFIG_SYS_BOOTM_LEN" advice does not mislead.

Patch 2 raises the noload-decompression headroom from 4x to 8x. The 4x factor is at the edge of what zstd and xz achieve on real kernels, so well-compressed vendor kernels can fail to boot at runtime once the bound is enforced. 8x covers them comfortably while remaining bounded.

Patch 3 adds two sandbox py-tests against the per-image buffer at the final 8x value: one that exceeds the buffer and must be rejected, and one that matches the buffer exactly and must succeed (guarding the boundary).

Tested on sandbox: both new tests pass; the existing test_fit_compressed_images_load (which covers the load-address path) and the other tests in test/py/tests/test_fit.py continue to pass.

Changes in v3:
- Swap patch 2 (tests) and patch 3 (bump) so the multiplier is bumped first and the tests are introduced once at the final 8x value, rather than written at 4x and rewritten at 8x. Patch 1 stays first as the self-contained fix for backporting.
- Patch 1: use the direct IH_TYPE_KERNEL_NOLOAD && comp != NONE condition to gate the "noload buffer" note, instead of 'decomp_len != CONFIG_SYS_BOOTM_LEN' which relied on alignment never coincidentally matching the global limit.
- Patch 3 (tests): drive the overflow test with ubman.wait_for() and ubman.restart_uboot(), instead of reaching into ubman.p.expect().
- Patch 3 (tests): add @pytest.mark.buildconfigspec('gzip') to both test methods.

Changes in v2:
- Patch 1: also print the per-image buffer size from bootm_load_os() when the failure is gated by the noload buffer, so handle_decomp_error()'s generic "increase CONFIG_SYS_BOOTM_LEN" advice does not mislead.
- Patch 2: add a second sandbox test covering the buffer-limit boundary, in addition to the existing overflow test.
- New patch 3: raise the noload-decompression headroom from 4x to 8x so well-compressed kernels do not have to bisect to find out why their board stopped booting.

Aristo Chen (3):
bootm: fix overflow of the noload kernel decompression buffer
bootm: increase kernel_noload decompression headroom from 4x to 8x test/py: test kernel_noload decompression buffer overflow

 boot/bootm.c              |  18 +++---
 test/py/tests/test_fit.py | 125 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 136 insertions(+), 7 deletions(-)
Signature · 1 line
2.43.0
#19 lore raw

PATCH v3 1/3 bootm: fix overflow of the noload kernel decompression buffer

For a compressed kernel_noload image, bootm_load_os() allocates a decompression buffer sized to ALIGN(image_len * 4, SZ_1M), assuming the kernel compresses by no more than a factor of four. It then passes CONFIG_SYS_BOOTM_LEN, rather than the size of that buffer, to image_decomp() as the output limit. The decompressors honour the limit they are given, so a kernel that decompresses to more than four times its compressed size is written past the end of the allocated buffer and corrupts adjacent memory.

Pass the allocation size to image_decomp() and handle_decomp_error() so decompression stops at the buffer boundary and fails cleanly when the image is too large, instead of overflowing. The regular non-noload paths are unchanged and continue to use CONFIG_SYS_BOOTM_LEN. When the failure is triggered by the smaller per-image buffer, print a note so that handle_decomp_error()'s generic advice to increase CONFIG_SYS_BOOTM_LEN does not mislead the reader.

Fixes 69544c4fd8b1 ("bootm: Support kernel_noload with compression")
---
 boot/bootm.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
boot/bootm.c +9 -5
index 4836d6b2d41..b55c41f30b7 100644
@@ -610,6 +610,7 @@ static int bootm_load_os(struct bootm_headers *images, int boot_progress)
 	ulong blob_end = os.end;
 	ulong image_start = os.image_start;
 	ulong image_len = os.image_len;
+	ulong decomp_len = CONFIG_SYS_BOOTM_LEN;
 	ulong flush_start = ALIGN_DOWN(load, ARCH_DMA_MINALIGN);
 	bool no_overlap;
 	void *load_buf, *image_buf;
@@ -623,11 +624,11 @@ static int bootm_load_os(struct bootm_headers *images, int boot_progress)
 	 * Use an alignment of 2MB since this might help arm64
 	 */
 	if (os.type == IH_TYPE_KERNEL_NOLOAD && os.comp != IH_COMP_NONE) {
-		ulong req_size = ALIGN(image_len * 4, SZ_1M);
 		phys_addr_t addr;
 
+		decomp_len = ALIGN(image_len * 4, SZ_1M);
 		err = lmb_alloc_mem(LMB_MEM_ALLOC_ANY, SZ_2M, &addr,
-				    req_size, LMB_NONE);
+				    decomp_len, LMB_NONE);
 		if (err)
 			return 1;
 
@@ -635,17 +636,20 @@ static int bootm_load_os(struct bootm_headers *images, int boot_progress)
 		images->os.load = (ulong)addr;
 		images->ep = (ulong)addr;
 		debug("Allocated %lx bytes at %lx for kernel (size %lx) decompression\n",
-		      req_size, load, image_len);
+		      decomp_len, load, image_len);
 	}
 
 	load_buf = map_sysmem(load, 0);
 	image_buf = map_sysmem(os.image_start, image_len);
 	err = image_decomp(os.comp, load, os.image_start, os.type,
 			   load_buf, image_buf, image_len,
-			   CONFIG_SYS_BOOTM_LEN, &load_end);
+			   decomp_len, &load_end);
 	if (err) {
 		err = handle_decomp_error(os.comp, load_end - load,
-					  CONFIG_SYS_BOOTM_LEN, err);
+					  decomp_len, err);
+		if (os.type == IH_TYPE_KERNEL_NOLOAD && os.comp != IH_COMP_NONE)
+			printf("Note: noload decompression buffer is %#lx bytes (not CONFIG_SYS_BOOTM_LEN)\n",
+			       decomp_len);
 		bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
 		return err;
 	}
Signature · 1 line
2.43.0
#20 lore raw

PATCH v3 2/3 bootm: increase kernel_noload decompression headroom from 4x to 8x

In reply to Aristo Chen

For a compressed kernel_noload image, bootm_load_os() allocates a buffer of ALIGN(image_len * 4, SZ_1M). The 4x factor is at the edge of what modern compressors (zstd, xz) achieve on real kernels, so a well-compressed vendor kernel can fail to boot at runtime with no intervening warning.

Bump the headroom to 8x. The buffer is still bounded by the compressed image size, and the SZ_1M alignment keeps the overhead below 1 MiB on small kernels.

---
 boot/bootm.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
boot/bootm.c +3 -3
index b55c41f30b7..4eee35d7e62 100644
@@ -619,14 +619,14 @@ static int bootm_load_os(struct bootm_headers *images, int boot_progress)
 	/*
 	 * For a "noload" compressed kernel we need to allocate a buffer large
 	 * enough to decompress in to and use that as the load address now.
-	 * Assume that the kernel compression is at most a factor of 4 since
-	 * zstd almost achieves that.
+	 * Allow up to 8x compression: this comfortably covers what zstd and xz
+	 * achieve on real kernels, with headroom for well-compressed payloads.
 	 * Use an alignment of 2MB since this might help arm64
 	 */
 	if (os.type == IH_TYPE_KERNEL_NOLOAD && os.comp != IH_COMP_NONE) {
 		phys_addr_t addr;
 
-		decomp_len = ALIGN(image_len * 4, SZ_1M);
+		decomp_len = ALIGN(image_len * 8, SZ_1M);
 		err = lmb_alloc_mem(LMB_MEM_ALLOC_ANY, SZ_2M, &addr,
 				    decomp_len, LMB_NONE);
 		if (err)
Signature · 1 line
2.43.0
#21 lore raw

PATCH v3 3/3 test/py: test kernel_noload decompression buffer overflow

In reply to Aristo Chen

Add sandbox tests that exercise the per-image decompression buffer that bootm_load_os() allocates for a compressed kernel_noload image (ALIGN(image_len * 8, SZ_1M)).

The overflow test builds a FIT whose decompressed size far exceeds the per-image buffer and asserts that 'bootm loados' rejects it with a decompression error rather than overflowing.

The boundary test builds a FIT whose decompressed size equals the per-image buffer exactly and asserts that 'bootm loados' succeeds, guarding against an off-by-one rejection at the buffer limit.

---
 test/py/tests/test_fit.py | 125 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 125 insertions(+)
test/py/tests/test_fit.py +125 -0
index 4f56a1421e1..d63fdd80528 100755
@@ -117,6 +117,36 @@ host save hostfs 0 %(loadables1_addr)x %(loadables1_out)s %(loadables1_size)x
 host save hostfs 0 %(loadables2_addr)x %(loadables2_out)s %(loadables2_size)x
 '''
 
+# A minimal ITS for a compressed 'kernel_noload' kernel. bootm allocates a
+# per-image decompression buffer for this image type, sized as a multiple of
+# the compressed length; see the test_fit_kernel_noload_decomp_* tests.
+NOLOAD_ITS = '''
+/dts-v1/;
+
+/ {
+        description = "FIT with a compressed kernel_noload image";
+        #address-cells = <1>;
+
+        images {
+                kernel-1 {
+                        data = /incbin/("%(kernel)s");
+                        type = "kernel_noload";
+                        arch = "sandbox";
+                        os = "linux";
+                        compression = "gzip";
+                        load = <0>;
+                        entry = <0>;
+                };
+        };
+        configurations {
+                default = "conf-1";
+                conf-1 {
+                        kernel = "kernel-1";
+                };
+        };
+};
+'''
+
 @pytest.mark.boardspec('sandbox')
 @pytest.mark.buildconfigspec('fit')
 @pytest.mark.requiredtool('dtc')
@@ -426,3 +456,98 @@ class TestFitImage:
 
         output = ubman.run_command_list(cmds)
         assert "can't get kernel image!" in '\n'.join(output)
+
+    @pytest.mark.buildconfigspec('gzip')
+    def test_fit_kernel_noload_decomp_overflow(self, ubman, fsetup):
+        """Test that an over-large compressed kernel_noload image is rejected
+
+        For a compressed 'kernel_noload' kernel, bootm_load_os() allocates a
+        decompression buffer of ALIGN(image_len * 8, SZ_1M) and must bound the
+        decompressor by that buffer. A kernel that decompresses to far more
+        than eight times its compressed size must therefore fail with a
+        decompression error instead of overflowing the buffer.
+        """
+        sz_1m = 1 << 20
+
+        # CONFIG_SYS_BOOTM_LEN is the global decompression limit. Keep the
+        # uncompressed size below it, so the failure is forced by the smaller
+        # per-image kernel_noload buffer rather than by that global limit.
+        bootm_len = int(ubman.config.buildconfig['config_sys_bootm_len'], 0)
+
+        # 4MB of zeros compresses to a few KB, so the decompression buffer
+        # (ALIGN(image_len * 8, SZ_1M), i.e. 1MB here) ends up far smaller
+        # than the uncompressed image.
+        decomp_size = 4 * sz_1m
+        kernel = fit_util.make_fname(ubman, 'test-noload-kernel.bin')
+        with open(kernel, 'wb') as fd:
+            fd.write(b'\0' * decomp_size)
+        kernel_gz = self.make_compressed(ubman, kernel)
+
+        image_len = self.filesize(kernel_gz)
+        req_size = (image_len * 8 + sz_1m - 1) // sz_1m * sz_1m
+        assert req_size < decomp_size <= bootm_len, (
+            'Test setup error: need decomp buffer (%#x) < image (%#x) <= '
+            'CONFIG_SYS_BOOTM_LEN (%#x)' % (req_size, decomp_size, bootm_len))
+
+        fit = fit_util.make_fit(ubman, fsetup['mkimage'], NOLOAD_ITS,
+                                {'kernel': kernel_gz})
+        fit_addr = fsetup['fit_addr']
+
+        ubman.run_command_list([
+            'host load hostfs 0 %x %s' % (fit_addr, fit),
+            'bootm start %x' % fit_addr,
+        ])
+
+        # 'bootm loados' decompresses the kernel. Decompression must stop at
+        # the buffer boundary and report 'Image too large'; it must not run
+        # past the buffer and return to the prompt.
+        ubman.run_command('bootm loados', wait_for_prompt=False)
+        try:
+            ubman.wait_for('Image too large')
+        finally:
+            # The decompression failure resets the board; bring up a fresh
+            # instance so later tests start from a clean console.
+            ubman.restart_uboot()
+
+    @pytest.mark.buildconfigspec('gzip')
+    def test_fit_kernel_noload_decomp_boundary(self, ubman, fsetup):
+        """Test that decompression succeeds exactly at the buffer limit
+
+        For a compressed 'kernel_noload' kernel, bootm_load_os() allocates a
+        decompression buffer of ALIGN(image_len * 8, SZ_1M). A kernel whose
+        decompressed size equals that buffer exactly must succeed, guarding
+        against an off-by-one rejection at the buffer limit.
+        """
+        sz_1m = 1 << 20
+
+        # 1MiB of zeros compresses to a few KB, so image_len * 8 rounds up to
+        # exactly 1MiB. Picking decomp_size = 1MiB makes the decompressed size
+        # match the buffer exactly.
+        decomp_size = sz_1m
+        kernel = fit_util.make_fname(ubman, 'test-noload-kernel-boundary.bin')
+        with open(kernel, 'wb') as fd:
+            fd.write(b'\0' * decomp_size)
+        kernel_gz = self.make_compressed(ubman, kernel)
+
+        image_len = self.filesize(kernel_gz)
+        req_size = (image_len * 8 + sz_1m - 1) // sz_1m * sz_1m
+        assert decomp_size == req_size, (
+            'Test setup error: need decomp_size (%#x) == req_size (%#x)'
+            % (decomp_size, req_size))
+
+        fit = fit_util.make_fit(ubman, fsetup['mkimage'], NOLOAD_ITS,
+                                {'kernel': kernel_gz},
+                                basename='test-noload-boundary.fit')
+        fit_addr = fsetup['fit_addr']
+
+        # Decompression at the buffer limit must succeed, returning to the
+        # prompt cleanly and never printing 'Image too large'.
+        output = ubman.run_command_list([
+            'host load hostfs 0 %x %s' % (fit_addr, fit),
+            'bootm start %x' % fit_addr,
+            'bootm loados',
+        ])
+        text = '\n'.join(output)
+        assert 'Image too large' not in text, (
+            "'bootm loados' rejected a kernel_noload image whose decompressed "
+            'size matches its buffer exactly: %s' % text)
Signature · 1 line
2.43.0
Reviewed-by on v3 3/3
#22 lore raw

Re: [PATCH v3 3/3] test/py: test kernel_noload decompression buffer overflow

Quoted 9 lines · Aristo Chen

Add sandbox tests that exercise the per-image decompression buffer that bootm_load_os() allocates for a compressed kernel_noload image (ALIGN(image_len * 8, SZ_1M)).

The overflow test builds a FIT whose decompressed size far exceeds the per-image buffer and asserts that 'bootm loados' rejects it with a decompression error rather than overflowing.

Quoted 9 lines

The boundary test builds a FIT whose decompressed size equals the per-image buffer exactly and asserts that 'bootm loados' succeeds, guarding against an off-by-one rejection at the buffer limit.

---
 test/py/tests/test_fit.py | 125 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 125 insertions(+)
#23 lore raw
In reply to Aristo Chen
Quoted 9 lines · Aristo Chen

For a compressed kernel_noload image, bootm_load_os() allocates a decompression buffer of ALIGN(image_len * 4, SZ_1M) and then passes CONFIG_SYS_BOOTM_LEN (typically 128 MiB on arm64) to image_decomp() as the output limit. The decompressors honour whatever limit they are given, so a kernel that decompresses to more than four times its compressed size runs past the end of the allocated buffer and silently corrupts adjacent memory.

[...]

Applied to u-boot/next, thanks!

[1/3] bootm: fix overflow of the noload kernel decompression buffer

      commit: 2ff26c1e37858ba0b2fd12c82e114a3cedcb317a
[2/3] bootm: increase kernel_noload decompression headroom from 4x to 8x
      commit: 4956f108539135afb1afdec2b509f62291087d16
[3/3] test/py: test kernel_noload decompression buffer overflow
      commit: a7ea33e3a35860326aeb5792f337bd9082d40ecf
Signature · 1 line
Tom