@@ -74,3 +74,4 @@ tmp.*
/truncated_memrsv
/utilfdt_test
/value-labels
+/get_next_tag_invalid_prop_len
@@ -4,7 +4,7 @@ LIB_TESTS_L = get_mem_rsv \
get_path supernode_atdepth_offset parent_offset \
node_offset_by_prop_value node_offset_by_phandle \
node_check_compatible node_offset_by_compatible \
- get_alias \
+ get_alias get_next_tag_invalid_prop_len \
char_literal \
sized_cells \
notfound \
new file mode 100644
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * libfdt - Flat Device Tree manipulation
+ * Testcase for fdt_next_tag()
+ */
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+
+#include <libfdt.h>
+#include "tests.h"
+#include "testdata.h"
+
+int main(int argc, char *argv[])
+{
+ struct fdt_header *hdr;
+ struct fdt_property *prp;
+ void *fdt;
+ int size, nextoff = 0;
+ uint32_t tag;
+
+ test_init(argc, argv);
+ size = sizeof(*hdr) + sizeof(*prp) + 256;
+ fdt = calloc(1, size);
+ if (!fdt)
+ FAIL("Can't allocate memory");
+
+ hdr = fdt;
+ prp = (struct fdt_property *)(((char *) fdt) + sizeof(*hdr));
+ fdt_set_magic(fdt, FDT_MAGIC);
+ fdt_set_totalsize(fdt, size);
+ fdt_set_version(fdt, 0x10);
+ prp->tag = cpu_to_fdt32(FDT_PROP);
+ prp->len = cpu_to_fdt32(256);
+ prp->nameoff = 0;
+
+ tag = fdt_next_tag(fdt, sizeof(*hdr), &nextoff);
+ if (tag != FDT_PROP)
+ FAIL("Invalid tag %X", tag);
+
+ if (nextoff != size)
+ FAIL("Invalid next_offset");
+
+ /* int overflow case */
+ prp->len = cpu_to_fdt32(0xFFFFFFFA);
+ tag = fdt_next_tag(fdt, sizeof(*hdr), &nextoff);
+ if (tag != FDT_END)
+ FAIL("Invalid tag, expected premature end");
+
+ if (nextoff != -FDT_ERR_BADSTRUCTURE)
+ FAIL("Invalid nextoff, expected error FDT_ERR_BADSTRUCTURE");
+
+ /* negative offset case */
+ prp->len = cpu_to_fdt32(0x7FFFFFFA);
+ tag = fdt_next_tag(fdt, sizeof(*hdr), &nextoff);
+ if (tag != FDT_END)
+ FAIL("Invalid tag, expected premature end");
+
+ if (nextoff != -FDT_ERR_BADSTRUCTURE)
+ FAIL("Invalid nextoff, expected error FDT_ERR_BADSTRUCTURE");
+
+ free(fdt);
+ PASS();
+}
@@ -47,6 +47,7 @@ tests = [
'get_path',
'get_phandle',
'get_prop_offset',
+ 'get_next_tag_invalid_prop_len',
'getprop',
'incbin',
'integer-expressions',
@@ -346,6 +346,7 @@ tree1_tests () {
run_test get_prop_offset $TREE
run_test get_phandle $TREE
run_test get_path $TREE
+ run_test get_next_tag_invalid_prop_len $TREE #TREE not really needed
run_test supernode_atdepth_offset $TREE
run_test parent_offset $TREE
run_test node_offset_by_prop_value $TREE
Add a new test get_next_tag_invalid_prop_len, which covers fdt_next_tag(), when it is passed an corrupted blob, with invalid property len values. Signed-off-by: Tadeusz Struk <tadeusz.struk@linaro.org> --- tests/.gitignore | 1 + tests/Makefile.tests | 2 +- tests/get_next_tag_invalid_prop_len.c | 65 +++++++++++++++++++++++++++ tests/meson.build | 1 + tests/run_tests.sh | 1 + 5 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 tests/get_next_tag_invalid_prop_len.c