Skip to contentSkip to conversation

bootstd: cros: Validate keyblock_size before locating the preamble

PATCH In review Aristo Chen 2 participants 4 messages Sep 7 – Sep 29

lore
Share & tools
lore
#0 lore raw

cover bootstd: cros: Validate keyblock_size before locating the preamble

The ChromiumOS bootmeth reads the first 4KB of a kernel partition and, after checking only the keyblock magic, uses the on-disk keyblock_size field to locate the kernel preamble within that buffer. Nothing bounds the value, so a corrupt or malicious partition can place the preamble pointer far outside the allocation, and every subsequent preamble field access is an out-of-bounds read. The values fetched this way then drive allocation sizes and disk offsets. On sandbox, scanning such a partition crashes U-Boot with SIGSEGV.

Patch 1 rejects the partition if the keyblock claims to be smaller than its own header, if the preamble does not lie entirely within the probed area, or if the computed kernel body offset falls inside the probed area. The last condition matches what cros_read_kernel() already requires before reading the kernel body, and also catches a preamble_size that wraps the 32-bit sum.

Patch 2 adds four malformed kernel partitions to the ChromiumOS test disk, one for each rejected condition: an oversized keyblock_size, an undersized keyblock_size, an undersized kernel body offset and a preamble_size that wraps the 32-bit sum. Without patch 1, the first crashes the bootflow_cros test and the other three scan to spurious ready bootflows; with it, all four partitions are rejected during scanning and all bootstd sandbox tests pass.

Aristo Chen (2):
bootstd: cros: Validate keyblock_size before locating the preamble
test: bootstd: Cover invalid ChromiumOS keyblock and preamble metadata

 boot/bootmeth_cros.c     | 29 ++++++++++++++++++++++++--
 test/boot/bootflow.c     |  6 ++++++
 test/py/tests/test_ut.py | 44 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 77 insertions(+), 2 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 bootstd: cros: Validate keyblock_size before locating the preamble

cros_read_bootflow() locates the kernel preamble with

	preamble = (void *)hdr + hdr->keyblock_size;

where hdr is a PROBE_SIZE (4KB) buffer holding the start of the kernel partition and keyblock_size is a 32-bit value read from the disk. The only field checked before this is the keyblock magic, so a corrupt or malicious partition can carry an arbitrary keyblock_size and make every subsequent preamble access read far outside the buffer, crashing U-Boot or fetching garbage that then drives allocation sizes and disk offsets.

Reject the partition if the keyblock claims to be smaller than its own header, or if the preamble does not lie entirely within the probed area.

Also require the computed kernel body offset (keyblock_size plus preamble_size) to lie beyond the probed area, as cros_read_kernel() already demands just before reading the body. Rejecting such invalid metadata during scanning means no bootflow is created for it, and the single comparison also catches a preamble_size that wraps the 32-bit sum, since a wrapped value is necessarily smaller than keyblock_size and therefore below PROBE_SIZE.

Fixes 3257835e5640 ("bootstd: cros: Decode some kernel preamble fields")
---
 boot/bootmeth_cros.c | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)
boot/bootmeth_cros.c +27 -2
index f894ed78e3a..685d5335e86 100644
@@ -369,6 +369,7 @@ static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow)
 	struct vb2_keyblock *hdr;
 	const char *uuid = NULL;
 	struct cros_priv *priv;
+	ulong body_offset;
 	int ret;
 
 	log_debug("starting, part=%x\n", bflow->part);
@@ -380,6 +381,31 @@ static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow)
 		return log_msg_ret("scan", ret);
 	}
 
+	/*
+	 * Make sure that the preamble, which follows the keyblock, lies within
+	 * the data read by scan_part(), since keyblock_size comes from the
+	 * disk and cannot be trusted
+	 */
+	if (hdr->keyblock_size < sizeof(*hdr) ||
+	    hdr->keyblock_size > PROBE_SIZE - sizeof(*preamble)) {
+		log_debug("- invalid keyblock size %x\n", hdr->keyblock_size);
+		free(hdr);
+		return log_msg_ret("kblk", -ERANGE);
+	}
+	preamble = (void *)hdr + hdr->keyblock_size;
+
+	/*
+	 * The kernel body must start beyond the probed area, as
+	 * cros_read_kernel() requires; this also rejects a preamble_size
+	 * that wraps the 32-bit sum
+	 */
+	body_offset = hdr->keyblock_size + preamble->preamble_size;
+	if (body_offset < PROBE_SIZE) {
+		log_debug("- invalid body offset %lx\n", body_offset);
+		free(hdr);
+		return log_msg_ret("bod", -ERANGE);
+	}
+
 	priv = malloc(sizeof(struct cros_priv));
 	if (!priv) {
 		free(hdr);
@@ -391,8 +417,7 @@ static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow)
 		  (ulong)map_to_sysmem(hdr));
 
 	/* Grab a few things from the preamble */
-	preamble = (void *)hdr + hdr->keyblock_size;
-	priv->body_offset = hdr->keyblock_size + preamble->preamble_size;
+	priv->body_offset = body_offset;
 	priv->part_start = info.start;
 
 	/* Now read everything we can learn about kernel */
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/2 test: bootstd: Cover invalid ChromiumOS keyblock and preamble metadata

In reply to Aristo Chen

Add four malformed kernel partitions to the ChromiumOS test disk:

- partition 6 (KERN_C): a keyblock with a valid magic but an out-of-range keyblock_size, placing the preamble far outside the probed area
- partition 13 (KERN_D): an accepted keyblock_size with a preamble_size that puts the kernel body offset inside the probed area
- partition 14 (KERN_E): a preamble_size that wraps the 32-bit sum forming the kernel body offset
- partition 15 (KERN_F): a keyblock_size smaller than the keyblock header itself, placing the claimed preamble inside the header

All four must be rejected while scanning, so the existing bootflow_cros and bootstd_images assertions (bootflow rows and counts) are unchanged. Without the previous fix, partition 6 crashes sandbox with SIGSEGV, and the other three scan successfully and surface as spurious ready bootflows, so each check is covered by an observable failure.

---
 test/boot/bootflow.c     |  6 ++++++
 test/py/tests/test_ut.py | 44 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)
test/boot/bootflow.c +6 -0
index ef4638ec700..b7a8df293ad 100644
@@ -1255,6 +1255,12 @@ BOOTSTD_TEST(bootflow_cmdline_special, 0);
 /* Test ChromiumOS bootmeth */
 static int bootflow_cros(struct unit_test_state *uts)
 {
+	/*
+	 * mmc5 has valid kernels in partitions 2 and 4 and corrupt kernel
+	 * metadata in partitions 6, 13, 14 and 15 (oversized keyblock,
+	 * undersized body offset, wrapped body offset, undersized keyblock),
+	 * none of which may produce a bootflow
+	 */
 	ut_assertok(scan_mmc_bootdev(uts, "mmc5", true));
 	ut_assertok(run_command("bootflow list", 0));
 
test/py/tests/test_ut.py +44 -0

62 lines not on this page · raw message

Signature · 1 line
2.43.0
#3 lore raw
In reply to Aristo Chen
Quoted 10 lines · Aristo Chen

The ChromiumOS bootmeth reads the first 4KB of a kernel partition and, after checking only the keyblock magic, uses the on-disk keyblock_size field to locate the kernel preamble within that buffer. Nothing bounds the value, so a corrupt or malicious partition can place the preamble pointer far outside the allocation, and every subsequent preamble field access is an out-of-bounds read. The values fetched this way then drive allocation sizes and disk offsets. On sandbox, scanning such a partition crashes U-Boot with SIGSEGV.

[...]

Applied to u-boot/next, thanks!

[1/2] bootstd: cros: Validate keyblock_size before locating the preamble

      commit: f82b14174f71cc3667ebb86bbb9a7e1345c6ee17
[2/2] test: bootstd: Cover invalid ChromiumOS keyblock and preamble metadata
      commit: a127ac8c99354bcb3415a365677eb626ddc6a289
Signature · 1 line
Tom