Skip to contentSkip to conversation

vbe: bound FIT external-data reads against the firmware area

PATCH Superseded Aristo Chen 3 participants 17 messages Jun 21 – Jul 16

lore
Share & tools
lore
#0 lore raw

cover vbe: bound FIT external-data reads against the firmware area

vbe_read_fit() loads a firmware-phase FIT from a fixed firmware area on a block device and then issues a follow-up blk_read() to pull in the image, and optionally an FDT, referenced by the FIT's image node. The source offset on the device and the read length both come from the FIT itself, via data-position or data-offset and data-size. Those properties live on mutable boot media and can be controlled by an attacker with write access to the firmware area. On the TPL or VPL path, and on the bootmeth bootflow path reached via abrec_read_bootflow_fw() and vbe_simple_read_bootflow_fw(), the follow-up blk_read() runs before any signature or hash check on the loaded phase.

Patch 1 is a sandbox test-tree preparation. The firmware1 node in arch/sandbox/dts/test.dts declared area-size = 0xe00000 (14 MiB), but the binman fw-update section in sandbox_vpl.dtsi is 32 MiB and the FIT inside it carries ~16 MiB of external data, so the FIT already extended past the declared area. The mismatch was tolerated because no caller bounded the external-data load against area_size. Patch 1 raises area-size to match the binman section size so test_vbe_vpl keeps passing once the bound is enforced. The patches are ordered so the test is never broken in the middle of the series.

Patch 2 adds the missing range check, confining the FIT-supplied [load_addr, load_addr + len) window to [addr, addr + area_size] before block numbers and lengths are computed, and applying the same constraint to fdt_load_addr and fdt_size. The check is written in subtraction-only form against the trusted area_size so the comparison cannot itself overflow.

One open question: should the external-data blk_read() in vbe_read_fit() also be deferred until after the phase has been signature-verified, rather than just bounded? Patch 2 hardens the load step, but moving verification earlier would be a stronger structural fix. Happy to follow up with a separate change if that is preferred.

Aristo Chen (2):
sandbox: vbe: size firmware1 area to fit the binman fw-update section
vbe: bound FIT external-data offset and size before blk_read

 arch/sandbox/dts/test.dts |  2 +-
 boot/vbe_common.c         | 16 ++++++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 sandbox: vbe: size firmware1 area to fit the binman fw-update section

The firmware1 node in test.dts declares area-size = 0xe00000 (14 MiB) but the binman fw-update section in sandbox_vpl.dtsi is 0x2000000 (32 MiB) and the FIT inside it carries ~16 MiB of external data (spl + u-boot subimages). The FIT therefore extends past the declared firmware area, contradicting the documented contract of vbe_read_fit() that the FIT must fit within @area_size.

The mismatch was tolerated because no caller actually bounded the external-data load against area_size. Bring the devicetree in line with the binman section size so the FIT extent stays within the trusted firmware area, in preparation for vbe_read_fit() enforcing that bound.

state-offset and version-offset are left as-is; they were already inside the FIT data region and are not exercised by test_vbe_vpl.

---
 arch/sandbox/dts/test.dts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/sandbox/dts/test.dts +1 -1
index 0887de4333b..37a36705b36 100644
@@ -176,7 +176,7 @@
 			storage = "mmc3";
 			skip-offset = <0x800000>;
 			area-start = <0>;
-			area-size = <0xe00000>;
+			area-size = <0x2000000>;
 			state-offset = <0xdffc00>;
 			state-size = <0x40>;
 			version-offset = <0xdffe00>;
Signature · 1 line
2.43.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH v1 1/2] sandbox: vbe: size firmware1 area to fit the binman fw-update section

Quoted 15 lines · Aristo Chen

sandbox: vbe: size firmware1 area to fit the binman fw-update section

The firmware1 node in test.dts declares area-size = 0xe00000 (14 MiB) but the binman fw-update section in sandbox_vpl.dtsi is 0x2000000 (32 MiB) and the FIT inside it carries ~16 MiB of external data (spl + u-boot subimages). The FIT therefore extends past the declared firmware area, contradicting the documented contract of vbe_read_fit() that the FIT must fit within @area_size.

The mismatch was tolerated because no caller actually bounded the external-data load against area_size. Bring the devicetree in line with the binman section size so the FIT extent stays within the trusted firmware area, in preparation for vbe_read_fit() enforcing that bound.

Quoted 7 lines

state-offset and version-offset are left as-is; they were already inside the FIT data region and are not exercised by test_vbe_vpl.

arch/sandbox/dts/test.dts | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
#3 lore raw

PATCH 2/2 vbe: bound FIT external-data offset and size before blk_read

In reply to Aristo Chen

vbe_read_fit() loads a firmware-phase FIT from the trusted firmware area and then issues a blk_read() to pull in the image, and optionally an FDT, referenced by the FIT image node. The source offset on the device and the read length both come from the FIT's data-position or data-offset property and its data-size property, which live on mutable boot media and can be controlled by an attacker with prior write access to the firmware area.

Without a range check the resulting blk_read() can read past the firmware area on the device and, on the non-SPL path, write an attacker-chosen number of blocks past the malloc(aligned_size) FIT buffer into adjacent memory. Only the SPL branch routes through spl_load_simple_fit(), which hashes the data. The external-data block reached from TPL or VPL, and from the bootflow path via abrec_read_bootflow_fw() and vbe_simple_read_bootflow_fw(), runs before any signature or hash check on the loaded phase.

Confine the FIT-supplied [load_addr, load_addr + len) window to [addr, addr + area_size] before computing block numbers and lengths, and apply the same constraint to fdt_load_addr and fdt_size. The checks are written in subtraction-only form against the trusted area_size so the comparison itself cannot overflow.

---
 boot/vbe_common.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
boot/vbe_common.c +16 -0
index a86986d86e9..844100b7ee6 100644
@@ -278,6 +278,17 @@ int vbe_read_fit(struct udevice *blk, ulong area_offset, ulong area_size,
 		ulong fdt_offset;
 		void *base_buf, *fdt_base_buf;
 
+		/*
+		 * load_addr and len describe the external data location in
+		 * the FIT, which lives on mutable boot media. Bound them
+		 * against the trusted area_size so the blk_read() below
+		 * cannot read past the firmware area or write an
+		 * attacker-chosen number of blocks past the FIT allocation.
+		 */
+		if (load_addr < addr || load_addr - addr > area_size ||
+		    len > area_size - (load_addr - addr))
+			return log_msg_ret("rng", -E2BIG);
+
 		/* Find the start address to load from */
 		base = ALIGN_DOWN(load_addr, desc->blksz);
 
@@ -321,6 +332,11 @@ int vbe_read_fit(struct udevice *blk, ulong area_offset, ulong area_size,
 
 		/* now the FDT */
 		if (fdt_size) {
+			/* Same constraint as above for the FDT region */
+			if (fdt_load_addr < addr ||
+			    fdt_load_addr - addr > area_size ||
+			    fdt_size > area_size - (fdt_load_addr - addr))
+				return log_msg_ret("rng", -E2BIG);
 			fdt_offset = area_offset + fdt_load_addr - addr;
 			blknum = fdt_offset / desc->blksz;
 			extra = fdt_offset % desc->blksz;
Signature · 1 line
2.43.0
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH v1 2/2] vbe: bound FIT external-data offset and size before blk_read

Quoted 25 lines · Aristo Chen

vbe: bound FIT external-data offset and size before blk_read

vbe_read_fit() loads a firmware-phase FIT from the trusted firmware area and then issues a blk_read() to pull in the image, and optionally an FDT, referenced by the FIT image node. The source offset on the device and the read length both come from the FIT's data-position or data-offset property and its data-size property, which live on mutable boot media and can be controlled by an attacker with prior write access to the firmware area.

Without a range check the resulting blk_read() can read past the firmware area on the device and, on the non-SPL path, write an attacker-chosen number of blocks past the malloc(aligned_size) FIT buffer into adjacent memory. Only the SPL branch routes through spl_load_simple_fit(), which hashes the data. The external-data block reached from TPL or VPL, and from the bootflow path via abrec_read_bootflow_fw() and vbe_simple_read_bootflow_fw(), runs before any signature or hash check on the loaded phase.

Confine the FIT-supplied [load_addr, load_addr + len) window to [addr, addr + area_size] before computing block numbers and lengths, and apply the same constraint to fdt_load_addr and fdt_size. The checks are written in subtraction-only form against the trusted area_size so the comparison itself cannot overflow.

Quoted 4 lines

boot/vbe_common.c | 16 ++++++++++++++++

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

Hi Aristo,

Quoted 3 lines · Aristo Chen

One open question: should the external-data blk_read() in vbe_read_fit() also be deferred until after the phase has been signature-verified, rather than just bounded?

The bounded read is the right first step. Deferring until after verification touches the SPL/TPL/VPL call paths and the bootmeth flow, so that could be done as a separate series.

Quoted 1 line

Patch 2 adds the missing range check ...

This could use a sandbox test that gives vbe_read_fit() a FIT with an out-of-range data-position or oversized data-size and checks it returns -E2BIG

Regards,
Simon

#6 lore raw

Hi Simon

Quoted 5 lines · Simon Glass

Hi Aristo,

On 2026-06-21T14:39:32, Aristo Chen <[email protected]> wrote:

Quoted 7 lines
Quoted 3 lines

One open question: should the external-data blk_read() in vbe_read_fit() also be deferred until after the phase has been signature-verified, rather than just bounded?

The bounded read is the right first step. Deferring until after verification touches the SPL/TPL/VPL call paths and the bootmeth flow, so that could be done as a separate series.

Noted, I will prepare a separate patch set after this is merged

Quoted 6 lines
Quoted 2 lines

Patch 2 adds the missing range check ...

This could use a sandbox test that gives vbe_read_fit() a FIT with an out-of-range data-position or oversized data-size and checks it returns -E2BIG

I will figure it out and send a V2

Quoted 3 lines

Regards,
Simon

Best Regards,
Aristo

#7 lore raw

v2 cover vbe: bound FIT external-data reads against the firmware area

In reply to Aristo Chen

vbe_read_fit() loads a firmware-phase FIT from a fixed firmware area on a block device and then issues a follow-up blk_read() to pull in the image, and optionally an FDT, referenced by the FIT's image node. The source offset on the device and the read length both come from the FIT itself, via data-position or data-offset and data-size. Those properties live on mutable boot media and can be controlled by an attacker with write access to the firmware area. On the TPL or VPL path, and on the bootmeth bootflow path reached via abrec_read_bootflow_fw() and vbe_simple_read_bootflow_fw(), the follow-up blk_read() runs before any signature or hash check on the loaded phase.

Patch 1 is a sandbox test-tree preparation. The firmware1 node in arch/sandbox/dts/test.dts declared area-size = 0xe00000 (14 MiB), but the binman fw-update section in sandbox_vpl.dtsi is 32 MiB and the FIT inside it carries ~16 MiB of external data, so the FIT already extended past the declared area. The mismatch was tolerated because no caller bounded the external-data load against area_size. Patch 1 raises area-size to match the binman section size so test_vbe_vpl keeps passing once the bound is enforced. The patches are ordered so the test is never broken in the middle of the series.

Patch 2 adds the missing range check, confining the FIT-supplied [load_addr, load_addr + len) window to [addr, addr + area_size] before block numbers and lengths are computed, and applying the same constraint to fdt_load_addr and fdt_size. The check is written in subtraction-only form against the trusted area_size so the comparison cannot itself overflow.

Patch 3 adds two sandbox unit tests under test/boot/ that construct synthetic FITs with out-of-range data-position and oversized data-size, write them to mmc1, and confirm vbe_read_fit() returns -E2BIG for each before issuing the follow-up blk_read().

Deferring the external-data blk_read() until after the phase has been signature-verified would be a stronger structural fix and was discussed on the v1 thread. Simon confirmed the bounded read is the right first step and that the verify-then-load change should be a separate series, so this v2 stays scoped to the bound.

Changes since v1:
- Collected Simon Glass's Reviewed-by on patches 1 and 2.
- Added patch 3: sandbox bounds-check tests for vbe_read_fit() (Suggested-by Simon Glass).

Aristo Chen (3):
sandbox: vbe: size firmware1 area to fit the binman fw-update section
vbe: bound FIT external-data offset and size before blk_read
test: vbe: cover vbe_read_fit() external-data bounds checks

 arch/sandbox/dts/test.dts |   2 +-
 boot/vbe_common.c         |  16 +++
 test/boot/Makefile        |   2 +-
 test/boot/vbe_read_fit.c  | 225 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 243 insertions(+), 2 deletions(-)
 create mode 100644 test/boot/vbe_read_fit.c
Signature · 1 line
2.43.0
#8 lore raw

PATCH v2 1/3 sandbox: vbe: size firmware1 area to fit the binman fw-update section

The firmware1 node in test.dts declares area-size = 0xe00000 (14 MiB) but the binman fw-update section in sandbox_vpl.dtsi is 0x2000000 (32 MiB) and the FIT inside it carries ~16 MiB of external data (spl + u-boot subimages). The FIT therefore extends past the declared firmware area, contradicting the documented contract of vbe_read_fit() that the FIT must fit within @area_size.

The mismatch was tolerated because no caller actually bounded the external-data load against area_size. Bring the devicetree in line with the binman section size so the FIT extent stays within the trusted firmware area, in preparation for vbe_read_fit() enforcing that bound.

state-offset and version-offset are left as-is; they were already inside the FIT data region and are not exercised by test_vbe_vpl.

---
 arch/sandbox/dts/test.dts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/sandbox/dts/test.dts +1 -1
index 0887de4333b..37a36705b36 100644
@@ -176,7 +176,7 @@
 			storage = "mmc3";
 			skip-offset = <0x800000>;
 			area-start = <0>;
-			area-size = <0xe00000>;
+			area-size = <0x2000000>;
 			state-offset = <0xdffc00>;
 			state-size = <0x40>;
 			version-offset = <0xdffe00>;
Signature · 1 line
2.43.0
#9 lore raw

PATCH v2 2/3 vbe: bound FIT external-data offset and size before blk_read

In reply to Aristo Chen

vbe_read_fit() loads a firmware-phase FIT from the trusted firmware area and then issues a blk_read() to pull in the image, and optionally an FDT, referenced by the FIT image node. The source offset on the device and the read length both come from the FIT's data-position or data-offset property and its data-size property, which live on mutable boot media and can be controlled by an attacker with prior write access to the firmware area.

Without a range check the resulting blk_read() can read past the firmware area on the device and, on the non-SPL path, write an attacker-chosen number of blocks past the malloc(aligned_size) FIT buffer into adjacent memory. Only the SPL branch routes through spl_load_simple_fit(), which hashes the data. The external-data block reached from TPL or VPL, and from the bootflow path via abrec_read_bootflow_fw() and vbe_simple_read_bootflow_fw(), runs before any signature or hash check on the loaded phase.

Confine the FIT-supplied [load_addr, load_addr + len) window to [addr, addr + area_size] before computing block numbers and lengths, and apply the same constraint to fdt_load_addr and fdt_size. The checks are written in subtraction-only form against the trusted area_size so the comparison itself cannot overflow.

---
 boot/vbe_common.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
boot/vbe_common.c +16 -0
index a86986d86e9..844100b7ee6 100644
@@ -278,6 +278,17 @@ int vbe_read_fit(struct udevice *blk, ulong area_offset, ulong area_size,
 		ulong fdt_offset;
 		void *base_buf, *fdt_base_buf;
 
+		/*
+		 * load_addr and len describe the external data location in
+		 * the FIT, which lives on mutable boot media. Bound them
+		 * against the trusted area_size so the blk_read() below
+		 * cannot read past the firmware area or write an
+		 * attacker-chosen number of blocks past the FIT allocation.
+		 */
+		if (load_addr < addr || load_addr - addr > area_size ||
+		    len > area_size - (load_addr - addr))
+			return log_msg_ret("rng", -E2BIG);
+
 		/* Find the start address to load from */
 		base = ALIGN_DOWN(load_addr, desc->blksz);
 
@@ -321,6 +332,11 @@ int vbe_read_fit(struct udevice *blk, ulong area_offset, ulong area_size,
 
 		/* now the FDT */
 		if (fdt_size) {
+			/* Same constraint as above for the FDT region */
+			if (fdt_load_addr < addr ||
+			    fdt_load_addr - addr > area_size ||
+			    fdt_size > area_size - (fdt_load_addr - addr))
+				return log_msg_ret("rng", -E2BIG);
 			fdt_offset = area_offset + fdt_load_addr - addr;
 			blknum = fdt_offset / desc->blksz;
 			extra = fdt_offset % desc->blksz;
Signature · 1 line
2.43.0
#10 lore raw

PATCH v2 3/3 test: vbe: cover vbe_read_fit() external-data bounds checks

In reply to Aristo Chen

vbe_read_fit() rejects FITs whose external-data window extends past the trusted firmware area on disk by returning -E2BIG. Add two sandbox unit tests that construct synthetic FITs with attacker-controlled data-position and data-size values, write them to mmc1, and assert vbe_read_fit() catches each one before issuing the follow-up blk_read().

vbe_read_fit_oob_position uses a data-position past area_size, which trips the load_addr - addr > area_size clause. vbe_read_fit_oversize_data keeps data-position inside the area but picks a data-size that overruns area_size - (load_addr - addr), tripping the third clause.

The new file follows the existing bootstd VBE test layout and writes the FIT at block 16, past the version and nvdata blocks already used by bootstd_setup_for_tests().

---
 test/boot/Makefile       |   2 +-
 test/boot/vbe_read_fit.c | 225 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 226 insertions(+), 1 deletion(-)
 create mode 100644 test/boot/vbe_read_fit.c
test/boot/Makefile +1 -1
index 89538d4f0a6..b8e5f64ac82 100644
@@ -18,7 +18,7 @@ endif
 obj-$(CONFIG_MEASURED_BOOT) += measurement.o
 
 ifdef CONFIG_OF_LIVE
-obj-$(CONFIG_BOOTMETH_VBE_SIMPLE) += vbe_simple.o
+obj-$(CONFIG_BOOTMETH_VBE_SIMPLE) += vbe_simple.o vbe_read_fit.o
 endif
 obj-$(CONFIG_BOOTMETH_VBE) += vbe_fixup.o
 
test/boot/vbe_read_fit.c new file +225 -0

225 lines not on this page · raw message

Signature · 1 line
2.43.0
Reviewed-by on v2 3/3
#11 lore raw

Re: [PATCH v2 3/3] test: vbe: cover vbe_read_fit() external-data bounds checks

Hi Aristo,

Quoted 18 lines · Aristo Chen

test: vbe: cover vbe_read_fit() external-data bounds checks

vbe_read_fit() rejects FITs whose external-data window extends past the trusted firmware area on disk by returning -E2BIG. Add two sandbox unit tests that construct synthetic FITs with attacker-controlled data-position and data-size values, write them to mmc1, and assert vbe_read_fit() catches each one before issuing the follow-up blk_read().

vbe_read_fit_oob_position uses a data-position past area_size, which trips the load_addr - addr > area_size clause. vbe_read_fit_oversize_data keeps data-position inside the area but picks a data-size that overruns area_size - (load_addr - addr), tripping the third clause.

The new file follows the existing bootstd VBE test layout and writes the FIT at block 16, past the version and nvdata blocks already used by bootstd_setup_for_tests().

Quoted 6 lines

test/boot/Makefile | 2 +-

 test/boot/vbe_read_fit.c | 225 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 226 insertions(+), 1 deletion(-)

Thanks for doing the test!

test/boot/vbe_read_fit.c line 6

Quoted 8 lines
test/boot/vbe_read_fit.c +6 -0
@@ -0,0 +1,225 @@
+#include <blk.h>
+#include <dm.h>
+#include <image.h>
+#include <mapmem.h>
+#include <memalign.h>
+#include <mmc.h>

Nothing here uses map_sysmem() or friends, so <mapmem.h> can go.

test/boot/vbe_read_fit.c line 6

Quoted 8 lines
test/boot/vbe_read_fit.c +6 -0
@@ -0,0 +1,225 @@
+/*
+ * The synthetic FIT is written to mmc1 starting at block TEST_FIT_BLK.
+ * bootstd_setup_for_tests() uses blocks 4 and 6 (see bootstd_common.h);
+ * block 16 leaves a comfortable gap.
+ */
+#define TEST_FIT_BLK 16

Just to check, the two clauses that stay unreachable in sandbox (load_addr < addr while TEXT_BASE is 0, and the FDT bound behind !CONFIG_SANDBOX in vbe_read_fit()) - is that why only data-position and data-size are exercised? A one-line note in the commit message would help future readers.

Regards,
Simon

#12 lore raw

v3 cover vbe: bound FIT external-data reads against the firmware area

In reply to Aristo Chen

vbe_read_fit() loads a firmware-phase FIT from a fixed firmware area on a block device and then issues a follow-up blk_read() to pull in the image, and optionally an FDT, referenced by the FIT's image node. The source offset on the device and the read length both come from the FIT itself, via data-position or data-offset and data-size. Those properties live on mutable boot media and can be controlled by an attacker with write access to the firmware area. On the TPL or VPL path, and on the bootmeth bootflow path reached via abrec_read_bootflow_fw() and vbe_simple_read_bootflow_fw(), the follow-up blk_read() runs before any signature or hash check on the loaded phase.

Patch 1 is a sandbox test-tree preparation. The firmware1 node in arch/sandbox/dts/test.dts declared area-size = 0xe00000 (14 MiB), but the binman fw-update section in sandbox_vpl.dtsi is 32 MiB and the FIT inside it carries ~16 MiB of external data, so the FIT already extended past the declared area. The mismatch was tolerated because no caller bounded the external-data load against area_size. Patch 1 raises area-size to match the binman section size so test_vbe_vpl keeps passing once the bound is enforced. The patches are ordered so the test is never broken in the middle of the series.

Patch 2 adds the missing range check, confining the FIT-supplied [load_addr, load_addr + len) window to [addr, addr + area_size] before block numbers and lengths are computed, and applying the same constraint to fdt_load_addr and fdt_size. The check is written in subtraction-only form against the trusted area_size so the comparison cannot itself overflow.

Patch 3 adds two sandbox unit tests under test/boot/ that construct synthetic FITs with out-of-range data-position and oversized data-size, write them to mmc1, and confirm vbe_read_fit() returns -E2BIG for each before issuing the follow-up blk_read().

Deferring the external-data blk_read() until after the phase has been signature-verified would be a stronger structural fix and was discussed on the v1 thread. Simon confirmed the bounded read is the right first step and that the verify-then-load change should be a separate series, so this v3 stays scoped to the bound.

Changes since v2:
- Patch 3: dropped an unused <mapmem.h> include and added a note in the commit body explaining why the load_addr < addr and FDT-region clauses are not exercised by the new tests (both are unreachable from a sandbox test). Both nits from Simon's v2 review.
- Carried forward Simon Glass's Reviewed-by on all three patches.

Changes since v1:
- Collected Simon Glass's Reviewed-by on patches 1 and 2.
- Added patch 3: sandbox bounds-check tests for vbe_read_fit() (Suggested-by Simon Glass).

Aristo Chen (3):
sandbox: vbe: size firmware1 area to fit the binman fw-update section
vbe: bound FIT external-data offset and size before blk_read
test: vbe: cover vbe_read_fit() external-data bounds checks

 arch/sandbox/dts/test.dts |   2 +-
 boot/vbe_common.c         |  16 +++
 test/boot/Makefile        |   2 +-
 test/boot/vbe_read_fit.c  | 224 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 242 insertions(+), 2 deletions(-)
 create mode 100644 test/boot/vbe_read_fit.c
Signature · 1 line
2.43.0
#13 lore raw

PATCH v3 1/3 sandbox: vbe: size firmware1 area to fit the binman fw-update section

The firmware1 node in test.dts declares area-size = 0xe00000 (14 MiB) but the binman fw-update section in sandbox_vpl.dtsi is 0x2000000 (32 MiB) and the FIT inside it carries ~16 MiB of external data (spl + u-boot subimages). The FIT therefore extends past the declared firmware area, contradicting the documented contract of vbe_read_fit() that the FIT must fit within @area_size.

The mismatch was tolerated because no caller actually bounded the external-data load against area_size. Bring the devicetree in line with the binman section size so the FIT extent stays within the trusted firmware area, in preparation for vbe_read_fit() enforcing that bound.

state-offset and version-offset are left as-is; they were already inside the FIT data region and are not exercised by test_vbe_vpl.

---
 arch/sandbox/dts/test.dts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/sandbox/dts/test.dts +1 -1
index 0887de4333b..37a36705b36 100644
@@ -176,7 +176,7 @@
 			storage = "mmc3";
 			skip-offset = <0x800000>;
 			area-start = <0>;
-			area-size = <0xe00000>;
+			area-size = <0x2000000>;
 			state-offset = <0xdffc00>;
 			state-size = <0x40>;
 			version-offset = <0xdffe00>;
Signature · 1 line
2.43.0
#14 lore raw

PATCH v3 2/3 vbe: bound FIT external-data offset and size before blk_read

In reply to Aristo Chen

vbe_read_fit() loads a firmware-phase FIT from the trusted firmware area and then issues a blk_read() to pull in the image, and optionally an FDT, referenced by the FIT image node. The source offset on the device and the read length both come from the FIT's data-position or data-offset property and its data-size property, which live on mutable boot media and can be controlled by an attacker with prior write access to the firmware area.

Without a range check the resulting blk_read() can read past the firmware area on the device and, on the non-SPL path, write an attacker-chosen number of blocks past the malloc(aligned_size) FIT buffer into adjacent memory. Only the SPL branch routes through spl_load_simple_fit(), which hashes the data. The external-data block reached from TPL or VPL, and from the bootflow path via abrec_read_bootflow_fw() and vbe_simple_read_bootflow_fw(), runs before any signature or hash check on the loaded phase.

Confine the FIT-supplied [load_addr, load_addr + len) window to [addr, addr + area_size] before computing block numbers and lengths, and apply the same constraint to fdt_load_addr and fdt_size. The checks are written in subtraction-only form against the trusted area_size so the comparison itself cannot overflow.

---
 boot/vbe_common.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
boot/vbe_common.c +16 -0
index a86986d86e9..844100b7ee6 100644
@@ -278,6 +278,17 @@ int vbe_read_fit(struct udevice *blk, ulong area_offset, ulong area_size,
 		ulong fdt_offset;
 		void *base_buf, *fdt_base_buf;
 
+		/*
+		 * load_addr and len describe the external data location in
+		 * the FIT, which lives on mutable boot media. Bound them
+		 * against the trusted area_size so the blk_read() below
+		 * cannot read past the firmware area or write an
+		 * attacker-chosen number of blocks past the FIT allocation.
+		 */
+		if (load_addr < addr || load_addr - addr > area_size ||
+		    len > area_size - (load_addr - addr))
+			return log_msg_ret("rng", -E2BIG);
+
 		/* Find the start address to load from */
 		base = ALIGN_DOWN(load_addr, desc->blksz);
 
@@ -321,6 +332,11 @@ int vbe_read_fit(struct udevice *blk, ulong area_offset, ulong area_size,
 
 		/* now the FDT */
 		if (fdt_size) {
+			/* Same constraint as above for the FDT region */
+			if (fdt_load_addr < addr ||
+			    fdt_load_addr - addr > area_size ||
+			    fdt_size > area_size - (fdt_load_addr - addr))
+				return log_msg_ret("rng", -E2BIG);
 			fdt_offset = area_offset + fdt_load_addr - addr;
 			blknum = fdt_offset / desc->blksz;
 			extra = fdt_offset % desc->blksz;
Signature · 1 line
2.43.0
#15 lore raw

PATCH v3 3/3 test: vbe: cover vbe_read_fit() external-data bounds checks

In reply to Aristo Chen

vbe_read_fit() rejects FITs whose external-data window extends past the trusted firmware area on disk by returning -E2BIG. Add two sandbox unit tests that construct synthetic FITs with attacker-controlled data-position and data-size values, write them to mmc1, and assert vbe_read_fit() catches each one before issuing the follow-up blk_read().

vbe_read_fit_oob_position uses a data-position past area_size, which trips the load_addr - addr > area_size clause. vbe_read_fit_oversize_data keeps data-position inside the area but picks a data-size that overruns area_size - (load_addr - addr), tripping the third clause.

The two remaining bound clauses stay unreachable from a sandbox test. The load_addr < addr guard trivially holds when addr comes from CONFIG_VAL(TEXT_BASE), which is 0 on sandbox, and the FDT-region bound sits behind a !CONFIG_SANDBOX guard in vbe_read_fit(), so fdt_size stays 0 and that block is skipped in this test environment.

The new file follows the existing bootstd VBE test layout and writes the FIT at block 16, past the version and nvdata blocks already used by bootstd_setup_for_tests().

---
 test/boot/Makefile       |   2 +-
 test/boot/vbe_read_fit.c | 224 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 225 insertions(+), 1 deletion(-)
 create mode 100644 test/boot/vbe_read_fit.c
test/boot/Makefile +1 -1
index 89538d4f0a6..b8e5f64ac82 100644
@@ -18,7 +18,7 @@ endif
 obj-$(CONFIG_MEASURED_BOOT) += measurement.o
 
 ifdef CONFIG_OF_LIVE
-obj-$(CONFIG_BOOTMETH_VBE_SIMPLE) += vbe_simple.o
+obj-$(CONFIG_BOOTMETH_VBE_SIMPLE) += vbe_simple.o vbe_read_fit.o
 endif
 obj-$(CONFIG_BOOTMETH_VBE) += vbe_fixup.o
 
test/boot/vbe_read_fit.c new file +224 -0

224 lines not on this page · raw message

Signature · 1 line
2.43.0
#16 lore raw
In reply to Aristo Chen
Quoted 11 lines · Aristo Chen

vbe_read_fit() loads a firmware-phase FIT from a fixed firmware area on a block device and then issues a follow-up blk_read() to pull in the image, and optionally an FDT, referenced by the FIT's image node. The source offset on the device and the read length both come from the FIT itself, via data-position or data-offset and data-size. Those properties live on mutable boot media and can be controlled by an attacker with write access to the firmware area. On the TPL or VPL path, and on the bootmeth bootflow path reached via abrec_read_bootflow_fw() and vbe_simple_read_bootflow_fw(), the follow-up blk_read() runs before any signature or hash check on the loaded phase.

Quoted 1 line

[...]

Applied to u-boot/main, thanks!

[1/3] sandbox: vbe: size firmware1 area to fit the binman fw-update section

      commit: a8fc408cdfcbc3d8a04b493510a54d852f60550d
[2/3] vbe: bound FIT external-data offset and size before blk_read
      commit: a84c7670a698c1d42a6da7e713afdeaaa51eae46
[3/3] test: vbe: cover vbe_read_fit() external-data bounds checks
      commit: 9fa00f563bea7ac4a4fda3220e05b21ebd4a242f
Signature · 1 line
Tom