fdt_support: validate property lengths in chosen and dma-range fixups
v2 cover fdt_support: validate property lengths in chosen and dma-range fixups
boot/fdt_support.c contains a number of helpers that fix up the kernel devicetree handed to the OS during bootm/booti. Several of those helpers consume fdt_getprop() results without validating the returned length against the per-entry size implied by the surrounding cell-count arithmetic. When the OS devicetree is not signature-verified, for example an unsigned FIT, a DT loaded from $fdtaddr or $fdtcontroladdr, or a DT supplied over a network boot, the property is attacker-influenced and the missing checks turn into out-of-bounds reads or writes on the FDT blob and on stack buffers.
The first patch targets fdt_fixup_stdout(). The function copies the value of /aliases/serialN into a fixed 256-byte stack buffer before publishing it as /chosen/linux,stdout-path, but does not check that the property fits. The patch rejects an oversized property with a debug-only message and -FDT_ERR_NOSPACE so the unbounded memcpy cannot run.
The second patch addresses fdt_get_dma_range(). The function reads one full dma-ranges entry of (na + pna + ns) * sizeof(u32) bytes after checking only that the returned length is non-zero. A dma-ranges property shorter than one entry causes the subsequent fdt_read_number() and fdt_translate_dma_address() calls to read past the property within the FDT blob. The patch validates the length against one full entry and returns -EINVAL when the property is too short, matching the existing failure paths in this function.
Both rejection paths use debug() rather than printf() so production builds do not pay any .text or .rodata growth for the new diagnostic text. Measured against master on real cross-compiled targets, the v1 printf form added 88 bytes of .text on CMPCPRO_defconfig (which links the fdt_fixup_stdout check) and 119 bytes on rpi_arm64_defconfig (which links fdt_get_dma_range). The v2 debug form adds 0 bytes on CMPCPRO and 20 bytes on rpi_arm64; the 20-byte residual is the length-check branch itself, not the diagnostic.
Build tested with kontron_sl28_defconfig (aarch64), CMPCPRO_defconfig (powerpc, which enables both CONFIG_OF_STDOUT_VIA_ALIAS and CONFIG_CONS_INDEX and therefore links the new bounds check in fdt_fixup_stdout), rpi_arm64_defconfig (aarch64, links fdt_get_dma_range) and sandbox_defconfig. All builds are clean and scripts/checkpatch.pl reports no errors, warnings, or checks on either patch.
Changes in v2:
The two rejection paths are now debug() instead of printf() so the diagnostic text disappears from production builds. v1 added 88-119 bytes per warning per board that linked it; v2 adds 0 bytes for the diagnostic and only the length-check branch remains.
The __FUNCTION__ to __func__ cleanup that was patch 3 in v1 has been moved out into a separate treewide series.
Aristo Chen (2):
fdt_support: bound serialN alias length before copying to stack
fdt_support: validate dma-ranges length in fdt_get_dma_range
boot/fdt_support.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
Signature · 1 line
2.43.0
PATCH v2 1/2 fdt_support: bound serialN alias length before copying to stack
fdt_fixup_stdout() reads the path stored in /aliases/serialN with fdt_getprop() and then memcpys it into a fixed 256-byte stack buffer. The length returned by libfdt is the raw on-disk property size and is not bounded by any console-path convention, so an oversized property in a malformed or untrusted devicetree overflows the buffer with attacker-controlled length and contents. The "/* long enough */" comment next to tmp[] codifies an unchecked assumption.
Reject lengths that exceed sizeof(tmp) with a debug-only message and return -FDT_ERR_NOSPACE. The fixup runs during fdt_chosen() on every booted kernel when CONFIG_OF_STDOUT_VIA_ALIAS is enabled, and when the OS devicetree is not signature-verified the property is reachable from an attacker-influenced blob. Using debug() rather than printf() keeps the rejection text out of production builds so there is no .text or .rodata growth on space-constrained targets.
--- boot/fdt_support.c | 6 ++++++ 1 file changed, 6 insertions(+)
boot/fdt_support.c +6 -0
@@ -160,6 +160,12 @@ static int fdt_fixup_stdout(void *fdt, int chosenoff) goto noalias; } +if (len > (int )sizeof (tmp)) { +debug ("%s: %s alias path too long (%d bytes)\n" , + __func__, sername, len); +return -FDT_ERR_NOSPACE ; + } +/* fdt_setprop may break "path" so we copy it to tmp buffer */ memcpy (tmp, path, len);
Signature · 1 line
2.43.0
Re: [PATCH v2 1/2] fdt_support: bound serialN alias length before copying to stack
Hi Aristo,
Quoted 25 lines · Aristo Chen
fdt_support: validate dma-ranges length infdt_get_dma_range
fdt_get_dma_range()fetches the dma-ranges property withfdt_getprop()and checks only that the length is non-zero before reading one full entry from it. The entry size depends on na, pna and ns cells returned bycount_cells, which come from the parent buses in the devicetree. A dma-ranges property shorter than (na + pna + ns) * sizeof(u32) bytes causesfdt_read_number()andfdt_translate_dma_address()to read past the end of the property within the FDT blob, an out-of-bounds read of attacker-influenced data when the OS devicetree is not signature verified.Reject the property when its length is smaller than one full entry and return
-EINVAL, matching the existing failure paths in this function. Usedebug()rather thanprintf()for the rejection text so that production builds do not pay any .text or .rodata growth for the new diagnostic.Signed-off-by Aristo Chen <[email protected]>
boot/fdt_support.c| 7 +++++++1 file changed, 7 insertions(+)
boot/fdt_support.c int fdt_get_dma_range(const void *blob, int node, phys_addr_t *cpu, · line 1645
Quoted 6 lines
boot/fdt_support.c +6
-0@@ -1640,6 +1640,13 @@ int fdt_get_dma_range(const void *blob, int node, phys_addr_t *cpu, +if (len < (int )((na + pna + ns) *sizeof (*ranges))) { +debug ("%s: dma-ranges too short for %s\n" , __func__, +fdt_get_name (blob, node,NULL )); + ret = -EINVAL ; + goto out; + }
The bound looks correct: fdt_read_number() consumes na cells, the translation reads pna cells from ranges + na and the size read covers ns cells, so one full entry is na + pna + ns cells.
As a follow-up, dm_test_dma_ranges in test/dm/read.c already exercises dev_get_dma_range() and the -ENOENT path. Adding a node with a truncated dma-ranges to the sandbox DT and checking for -EINVAL would cover the new path. What do you think?
Regards,
Simon
Re: [PATCH v2 1/2] fdt_support: bound serialN alias length before copying to stack
Hi Simon,
Quoted 40 lines · Simon Glass
Hi Aristo,
Quoted 25 lines · Aristo Chen
fdt_support: validate dma-ranges length infdt_get_dma_range
fdt_get_dma_range()fetches the dma-ranges property withfdt_getprop()and checks only that the length is non-zero before reading one full entry from it. The entry size depends on na, pna and ns cells returned bycount_cells, which come from the parent buses in the devicetree. A dma-ranges property shorter than (na + pna + ns) * sizeof(u32) bytes causesfdt_read_number()andfdt_translate_dma_address()to read past the end of the property within the FDT blob, an out-of-bounds read of attacker-influenced data when the OS devicetree is not signature verified.Reject the property when its length is smaller than one full entry and return
-EINVAL, matching the existing failure paths in this function. Usedebug()rather thanprintf()for the rejection text so that production builds do not pay any .text or .rodata growth for the new diagnostic.Signed-off-by Aristo Chen <[email protected]>
boot/fdt_support.c| 7 +++++++1 file changed, 7 insertions(+)boot/fdt_support.c int fdt_get_dma_range(const void *blob, int node, phys_addr_t *cpu, · line 1645
Quoted 6 lines
boot/fdt_support.c +6
-0@@ -1640,6 +1640,13 @@ int fdt_get_dma_range(const void *blob, int node, phys_addr_t *cpu, +if (len < (int )((na + pna + ns) *sizeof (*ranges))) { +debug ("%s: dma-ranges too short for %s\n" , __func__, +fdt_get_name (blob, node,NULL )); + ret = -EINVAL ; + goto out; + }The bound looks correct:
fdt_read_number()consumes na cells, the translation reads pna cells from ranges + na and the size read covers ns cells, so one full entry is na + pna + ns cells.
Quoted 4 lines
As a follow-up,
dm_test_dma_rangesintest/dm/read.calready exercisesdev_get_dma_range()and the-ENOENTpath. Adding a node with a truncated dma-ranges to the sandbox DT and checking for-EINVALwould cover the new path. What do you think?
Sounds good to me, I will look into adding that as a follow-up.
Quoted 5 lines
Reviewed-by Simon Glass <[email protected]>Regards,
Simon
Best regards,
Aristo
PATCH v2 2/2 fdt_support: validate dma-ranges length in fdt_get_dma_range
In reply to Aristo Chenfdt_get_dma_range() fetches the dma-ranges property with fdt_getprop() and checks only that the length is non-zero before reading one full entry from it. The entry size depends on na, pna and ns cells returned by count_cells, which come from the parent buses in the devicetree. A dma-ranges property shorter than (na + pna + ns) * sizeof(u32) bytes causes fdt_read_number() and fdt_translate_dma_address() to read past the end of the property within the FDT blob, an out-of-bounds read of attacker-influenced data when the OS devicetree is not signature verified.
Reject the property when its length is smaller than one full entry and return -EINVAL, matching the existing failure paths in this function. Use debug() rather than printf() for the rejection text so that production builds do not pay any .text or .rodata growth for the new diagnostic.
--- boot/fdt_support.c | 7 +++++++ 1 file changed, 7 insertions(+)
boot/fdt_support.c +7 -0
@@ -1633,6 +1633,13 @@ int fdt_get_dma_range(const void *blob, int node, phys_addr_t *cpu, goto out; } +if (len < (int )((na + pna + ns) *sizeof (*ranges))) { +debug ("%s: dma-ranges too short for %s\n" , __func__, +fdt_get_name (blob, node,NULL )); + ret = -EINVAL ; + goto out; + } + *bus =fdt_read_number (ranges, na); *cpu =fdt_translate_dma_address (blob, node, ranges + na); *size =fdt_read_number (ranges + na + pna, ns);
Signature · 1 line
2.43.0
Quoted 11 lines · Aristo Chen
boot/fdt_support.ccontains a number of helpers that fix up the kernel devicetree handed to the OS during bootm/booti. Several of those helpers consumefdt_getprop()results without validating the returned length against the per-entry size implied by the surrounding cell-count arithmetic. When the OS devicetree is not signature-verified, for example an unsigned FIT, a DT loaded from $fdtaddr or $fdtcontroladdr, or a DT supplied over a network boot, the property is attacker-influenced and the missing checks turn into out-of-bounds reads or writes on the FDT blob and on stack buffers.[...]
Applied to u-boot/next, thanks!
[1/2] fdt_support: bound serialN alias length before copying to stack
commit: ca774b94d66332b6bd033369227ac487ad07d5e8
[2/2] fdt_support: validate dma-ranges length in fdt_get_dma_range
commit: 84e250c0a85a615620a461e0710bb970801fb276Signature · 1 line
Tom