libfdt: Check that there is only one root node
PATCH 1/2 libfdt: Check that there is only one root node
At present it is possible to have two root nodes and even access nodes in the 'second' root. Such trees should not be considered valid. This was discovered as part of a security investigation into U-Boot verified boot.
Add a check for this to fdt_check_full().
---
libfdt/fdt_check.c | 7 +++++++ tests/Makefile.tests | 4 +++- tests/dumptrees.c | 1 + tests/run_tests.sh | 2 +- tests/testdata.h | 1 + tests/trees.S | 19 +++++++++++++++++++ 6 files changed, 32 insertions(+), 2 deletions(-)
libfdt/fdt_check.c +7 -0
@@ -19,6 +19,7 @@ int fdt_check_full(const void *fdt, size_t bufsize)unsigned int depth =0 ;const void *prop;const char *propname; +bool expect_end = false;if (bufsize <FDT_V1_SIZE )return -FDT_ERR_TRUNCATED ;
@@ -41,6 +42,10 @@ int fdt_check_full(const void *fdt, size_t bufsize)if (nextoffset <0 )return nextoffset; +/* If we see two root nodes, something is wrong */ +if (expect_end && tag !=FDT_END ) +return -FDT_ERR_BADLAYOUT ; +switch (tag) {case FDT_NOP :break ;
@@ -60,6 +65,8 @@ int fdt_check_full(const void *fdt, size_t bufsize)if (depth ==0 )return -FDT_ERR_BADSTRUCTURE ; depth--; +if (depth ==0 ) + expect_end = true;break ;case FDT_PROP :
tests/Makefile.tests +3 -1
@@ -32,7 +32,9 @@ LIB_TESTS_L = get_mem_rsv \ fs_tree1 LIB_TESTS = $(LIB_TESTS_L:%=$(TESTS_PREFIX)%) -LIBTREE_TESTS_L = truncated_property truncated_string truncated_memrsv +LIBTREE_TESTS_L = truncated_property truncated_string truncated_memrsv \ + two_roots + LIBTREE_TESTS = $(LIBTREE_TESTS_L:%=$(TESTS_PREFIX)%) DL_LIB_TESTS_L = asm_tree_dump value-labels
tests/dumptrees.c +1 -0
@@ -24,6 +24,7 @@ static struct {TREE (ovf_size_strings),TREE (truncated_property),TREE (truncated_string),TREE (truncated_memrsv), + TREE(two_roots) };#define NUM_TREES (sizeof(trees) / sizeof(trees[0]))
tests/run_tests.sh +1 -1
@@ -518,7 +518,7 @@ libfdt_tests () { run_test check_full $good done for bad in truncated_property.dtb truncated_string.dtb \ - truncated_memrsv.dtb; do + truncated_memrsv.dtb two_roots.dtb; do run_test check_full -n $bad done }
tests/testdata.h +1 -0
@@ -55,4 +55,5 @@ extern struct fdt_header bad_prop_char;extern struct fdt_header ovf_size_strings;extern struct fdt_header truncated_string;extern struct fdt_header truncated_memrsv; +extern struct fdt_header two_roots;#endif /* ! __ASSEMBLY */
tests/trees.S +19 -0
@@ -279,3 +279,22 @@ truncated_memrsv_rsvmap: truncated_memrsv_rsvmap_end: truncated_memrsv_end: + + + /* two root nodes */ + TREE_HDR(two_roots) + EMPTY_RSVMAP(two_roots) + +two_roots_struct: + BEGIN_NODE("") + END_NODE + BEGIN_NODE("") + END_NODE + FDTLONG(FDT_END) +two_roots_struct_end: + +two_roots_strings: +two_roots_strings_end: + +two_roots_end: +
Signature · 1 line
2.31.0.rc2.261.g7f71774620-goog
A message that is not in the archive (sent off-list or not yet received): 20210322231941.3202986-1-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org
PATCH 2/2 libfdt: Check that the root-node name is empty
The root node is supposed to have an empty name, but at present this is not checked. The behaviour of such a tree is not well defined. Most software rightly assumes that the root node is at offset 0 and does not check the name. This oddity was discovered as part of a security investigation into U-Boot verified boot.
Add a check for this to fdt_check_full().
---
libfdt/fdt_check.c | 10 ++++++++++ tests/Makefile.tests | 2 +- tests/dumptrees.c | 3 ++- tests/testdata.h | 1 + tests/trees.S | 15 +++++++++++++++ 5 files changed, 29 insertions(+), 2 deletions(-)
libfdt/fdt_check.c +10 -0
@@ -59,6 +59,16 @@ int fdt_check_full(const void *fdt, size_t bufsize) depth++;if (depth >INT_MAX )return -FDT_ERR_BADSTRUCTURE ; + +/* The root node must have an empty name */ +if (depth ==1 ) { +const char *name; +int len; + + name =fdt_get_name (fdt, offset, &len); +if (*name || len) +return -FDT_ERR_BADLAYOUT ; + }break ;case FDT_END_NODE :
tests/Makefile.tests +1 -1
@@ -33,7 +33,7 @@ LIB_TESTS_L = get_mem_rsv \ LIB_TESTS = $(LIB_TESTS_L:%=$(TESTS_PREFIX)%) LIBTREE_TESTS_L = truncated_property truncated_string truncated_memrsv \ - two_roots + two_roots named_root LIBTREE_TESTS = $(LIBTREE_TESTS_L:%=$(TESTS_PREFIX)%)
tests/dumptrees.c +2 -1
@@ -24,7 +24,8 @@ static struct {TREE (ovf_size_strings),TREE (truncated_property),TREE (truncated_string),TREE (truncated_memrsv), - TREE(two_roots) +TREE (two_roots), + TREE(named_root) };#define NUM_TREES (sizeof(trees) / sizeof(trees[0]))
tests/testdata.h +1 -0
@@ -56,4 +56,5 @@ extern struct fdt_header ovf_size_strings;extern struct fdt_header truncated_string;extern struct fdt_header truncated_memrsv;extern struct fdt_header two_roots; +extern struct fdt_header named_root;#endif /* ! __ASSEMBLY */
tests/trees.S +15 -0
@@ -298,3 +298,18 @@ two_roots_strings_end: two_roots_end: + + /* root node with a non-empty name */ + TREE_HDR(named_root) + EMPTY_RSVMAP(named_root) + +named_root_struct: + BEGIN_NODE("fake") + END_NODE + FDTLONG(FDT_END) +named_root_struct_end: + +named_root_strings: +named_root_strings_end: + +named_root_end:
Signature · 1 line
2.31.0.rc2.261.g7f71774620-goog
A message that is not in the archive (sent off-list or not yet received): 20210322231941.3202986-2-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org
Re: [PATCH 2/2] libfdt: Check that the root-node name is empty
Quoted 27 lines · Simon Glass
The root node is supposed to have an empty name, but at present this is not checked. The behaviour of such a tree is not well defined. Most software rightly assumes that the root node is at offset 0 and does not check the name. This oddity was discovered as part of a security investigation into U-Boot verified boot.
Add a check for this to
fdt_check_full().Signed-off-by Simon Glass <[email protected]>Reported-by Arie Haenel <[email protected]>Reported-by Julien Lenoir <[email protected]>---
libfdt/fdt_check.c | 10 ++++++++++ tests/Makefile.tests | 2 +- tests/dumptrees.c | 3 ++- tests/testdata.h | 1 + tests/trees.S | 15 +++++++++++++++ 5 files changed, 29 insertions(+), 2 deletions(-)libfdt/fdt_check.c +0
-0@@ -59,6 +59,16 @@ int fdt_check_full(const void *fdt, size_t bufsize) depth++;if (depth >INT_MAX )
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 70
Quoted 10 lines
libfdt/fdt_check.c +9
-0return -FDT_ERR_BADSTRUCTURE ; + +/* The root node must have an empty name */ +if (depth ==1 ) { +const char *name; +int len; + + name =fdt_get_name (fdt, offset, &len); +if (*name || len) +return -FDT_ERR_BADLAYOUT ;
Again, this should be BADSTRUCTURE, not BADLAYOUT. Otherwise LGTM.
Quoted 64 lines
libfdt/fdt_check.c +1
-0+ }break ;case FDT_END_NODE :tests/Makefile.tests +1
-1@@ -33,7 +33,7 @@ LIB_TESTS_L = get_mem_rsv \ LIB_TESTS = $(LIB_TESTS_L:%=$(TESTS_PREFIX)%) LIBTREE_TESTS_L = truncated_property truncated_string truncated_memrsv \ - two_roots + two_roots named_root LIBTREE_TESTS = $(LIBTREE_TESTS_L:%=$(TESTS_PREFIX)%)tests/dumptrees.c +2
-1@@ -24,7 +24,8 @@ static struct {TREE (ovf_size_strings),TREE (truncated_property),TREE (truncated_string),TREE (truncated_memrsv), - TREE(two_roots) +TREE (two_roots), + TREE(named_root) };#define NUM_TREES (sizeof(trees) / sizeof(trees[0]))tests/testdata.h +1
-0@@ -56,4 +56,5 @@ extern struct fdt_header ovf_size_strings;extern struct fdt_header truncated_string;extern struct fdt_header truncated_memrsv;extern struct fdt_header two_roots; +extern struct fdt_header named_root;#endif /* ! __ASSEMBLY */ tests/trees.S +15
-0@@ -298,3 +298,18 @@ two_roots_strings_end: two_roots_end: + + /* root node with a non-empty name */ + TREE_HDR(named_root) + EMPTY_RSVMAP(named_root) + +named_root_struct: + BEGIN_NODE("fake") + END_NODE + FDTLONG(FDT_END) +named_root_struct_end: + +named_root_strings: +named_root_strings_end: + +named_root_end:
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Quoted 33 lines · Simon Glass
At present it is possible to have two root nodes and even access nodes in the 'second' root. Such trees should not be considered valid. This was discovered as part of a security investigation into U-Boot verified boot.
Add a check for this to
fdt_check_full().Signed-off-by Simon Glass <[email protected]>Reported-by Arie Haenel <[email protected]>Reported-by Julien Lenoir <[email protected]>---
libfdt/fdt_check.c | 7 +++++++ tests/Makefile.tests | 4 +++- tests/dumptrees.c | 1 + tests/run_tests.sh | 2 +- tests/testdata.h | 1 + tests/trees.S | 19 +++++++++++++++++++ 6 files changed, 32 insertions(+), 2 deletions(-)libfdt/fdt_check.c +1
-0@@ -19,6 +19,7 @@ int fdt_check_full(const void *fdt, size_t bufsize)unsigned int depth =0 ;const void *prop;const char *propname; +bool expect_end = false;if (bufsize <FDT_V1_SIZE )return -FDT_ERR_TRUNCATED ;
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 47
Quoted 6 lines
libfdt/fdt_check.c +3
-0@@ -41,6 +42,10 @@ int fdt_check_full(const void *fdt, size_t bufsize)if (nextoffset <0 )return nextoffset; +/* If we see two root nodes, something is wrong */ +if (expect_end && tag !=FDT_END ) +return -FDT_ERR_BADLAYOUT ;
This should be -FDT_ERR_BADSTRUCTURE (since it's an error in the format of the structure block), but otherwise this patch LGTM.
Quoted 90 lines
libfdt/fdt_check.c +3
-0+switch (tag) {case FDT_NOP :break ;@@ -60,6 +65,8 @@ int fdt_check_full(const void *fdt, size_t bufsize)if (depth ==0 )return -FDT_ERR_BADSTRUCTURE ; depth--; +if (depth ==0 ) + expect_end = true;break ;case FDT_PROP :tests/Makefile.tests +3
-1@@ -32,7 +32,9 @@ LIB_TESTS_L = get_mem_rsv \ fs_tree1 LIB_TESTS = $(LIB_TESTS_L:%=$(TESTS_PREFIX)%) -LIBTREE_TESTS_L = truncated_property truncated_string truncated_memrsv +LIBTREE_TESTS_L = truncated_property truncated_string truncated_memrsv \ + two_roots + LIBTREE_TESTS = $(LIBTREE_TESTS_L:%=$(TESTS_PREFIX)%) DL_LIB_TESTS_L = asm_tree_dump value-labelstests/dumptrees.c +1
-0@@ -24,6 +24,7 @@ static struct {TREE (ovf_size_strings),TREE (truncated_property),TREE (truncated_string),TREE (truncated_memrsv), + TREE(two_roots) };#define NUM_TREES (sizeof(trees) / sizeof(trees[0]))tests/run_tests.sh +1
-1@@ -518,7 +518,7 @@ libfdt_tests () { run_test check_full $good done for bad in truncated_property.dtb truncated_string.dtb \ - truncated_memrsv.dtb; do + truncated_memrsv.dtb two_roots.dtb; do run_test check_full -n $bad done }tests/testdata.h +1
-0@@ -55,4 +55,5 @@ extern struct fdt_header bad_prop_char;extern struct fdt_header ovf_size_strings;extern struct fdt_header truncated_string;extern struct fdt_header truncated_memrsv; +extern struct fdt_header two_roots;#endif /* ! __ASSEMBLY */ tests/trees.S +19
-0@@ -279,3 +279,22 @@ truncated_memrsv_rsvmap: truncated_memrsv_rsvmap_end: truncated_memrsv_end: + + + /* two root nodes */ + TREE_HDR(two_roots) + EMPTY_RSVMAP(two_roots) + +two_roots_struct: + BEGIN_NODE("") + END_NODE + BEGIN_NODE("") + END_NODE + FDTLONG(FDT_END) +two_roots_struct_end: + +two_roots_strings: +two_roots_strings_end: + +two_roots_end: +
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson