Browse Source

libfdt: tests: add get_next_tag_invalid_prop_len

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. The test runs twice, on a blob
in sw and finished state.

Signed-off-by: Tadeusz Struk <tadeusz.struk@linaro.org>
Message-Id: <20221011182611.116011-2-tadeusz.struk@linaro.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
main
Tadeusz Struk 2 years ago committed by David Gibson
parent
commit
55778a03df
  1. 1
      tests/.gitignore
  2. 2
      tests/Makefile.tests
  3. 100
      tests/get_next_tag_invalid_prop_len.c
  4. 1
      tests/meson.build
  5. 1
      tests/run_tests.sh

1
tests/.gitignore vendored

@ -74,3 +74,4 @@ tmp.*
/truncated_memrsv /truncated_memrsv
/utilfdt_test /utilfdt_test
/value-labels /value-labels
/get_next_tag_invalid_prop_len

2
tests/Makefile.tests

@ -4,7 +4,7 @@ LIB_TESTS_L = get_mem_rsv \
get_path supernode_atdepth_offset parent_offset \ get_path supernode_atdepth_offset parent_offset \
node_offset_by_prop_value node_offset_by_phandle \ node_offset_by_prop_value node_offset_by_phandle \
node_check_compatible node_offset_by_compatible \ node_check_compatible node_offset_by_compatible \
get_alias \ get_alias get_next_tag_invalid_prop_len \
char_literal \ char_literal \
sized_cells \ sized_cells \
notfound \ notfound \

100
tests/get_next_tag_invalid_prop_len.c

@ -0,0 +1,100 @@
// 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"

#define FDT_SIZE 65536
#define CHECK_ERR(err) \
({ if (err) \
FAIL("%s: %d: %s", __FILE__, __LINE__, fdt_strerror(err)); \
})

static void fdt_next_tag_test(bool fdt_finished)
{
struct fdt_property *prp;
void *fdt;
int nextoff = 0, offset, err;
uint32_t tag;

fdt = malloc(FDT_SIZE);
if (!fdt)
FAIL("Can't allocate memory");
err = fdt_create(fdt, FDT_SIZE);
CHECK_ERR(err);
err = fdt_finish_reservemap(fdt);
CHECK_ERR(err);
/* Create a root node and add two properties */
err = fdt_begin_node(fdt, "");
CHECK_ERR(err);
err = fdt_property_u32(fdt, "prop-int-32", 0x1234);
CHECK_ERR(err);
err = fdt_property_u32(fdt, "prop2-int-32", 0x4321);
CHECK_ERR(err);
err = fdt_end_node(fdt);
CHECK_ERR(err);
if (fdt_finished) {
/* Call fdt_finish to set the correct fdt state. */
err = fdt_finish(fdt);
CHECK_ERR(err);
}

offset = fdt_first_property_offset(fdt, 0);
if (offset <= 0)
FAIL("Invalid offset %x, expected value greater than 0, finished=%d\n",
offset, fdt_finished);

/* Normal case */
tag = fdt_next_tag(fdt, offset, &nextoff);
if (tag != FDT_PROP)
FAIL("Invalid tag %x, expected FDT_PROP, finished=%d\n",
tag, fdt_finished);
if (nextoff <= 0)
FAIL("Invalid nextoff %d, expected value greater than 0, finished=%d",
nextoff, fdt_finished);

/* Get a writable ptr to the first property and corrupt the length */
prp = fdt_get_property_by_offset_w(fdt, offset, NULL);
if (!prp)
FAIL("Bad property pointer, finished=%d", fdt_finished);

/* int overflow case */
prp->len = cpu_to_fdt32(0xFFFFFFFA);
tag = fdt_next_tag(fdt, offset, &nextoff);
if (tag != FDT_END)
FAIL("Invalid tag %x, expected premature FDT_END, finished=%d",
tag, fdt_finished);
if (nextoff != -FDT_ERR_BADSTRUCTURE)
FAIL("Invalid nextoff, expected error -FDT_ERR_BADSTRUCTURE, finished=%d",
fdt_finished);

/* negative offset case */
prp->len = cpu_to_fdt32(0x7FFFFFFA);
tag = fdt_next_tag(fdt, offset, &nextoff);
if (tag != FDT_END)
FAIL("Invalid tag %x, expected premature FDT_END, finished=%d",
tag, fdt_finished);
if (nextoff != -FDT_ERR_BADSTRUCTURE)
FAIL("Invalid nextoff, expected -FDT_ERR_BADSTRUCTURE, finished=%d",
fdt_finished);

free(fdt);
}

int main(int argc, char *argv[])
{
test_init(argc, argv);

fdt_next_tag_test(false);
fdt_next_tag_test(true);

PASS();
}

1
tests/meson.build

@ -47,6 +47,7 @@ tests = [
'get_path', 'get_path',
'get_phandle', 'get_phandle',
'get_prop_offset', 'get_prop_offset',
'get_next_tag_invalid_prop_len',
'getprop', 'getprop',
'incbin', 'incbin',
'integer-expressions', 'integer-expressions',

1
tests/run_tests.sh

@ -513,6 +513,7 @@ libfdt_tests () {
run_dtc_test -I fs -O dts -o fs.test_tree1.test.dts $FSBASE/test_tree1 run_dtc_test -I fs -O dts -o fs.test_tree1.test.dts $FSBASE/test_tree1
run_dtc_test -I fs -O dtb -o fs.test_tree1.test.dtb $FSBASE/test_tree1 run_dtc_test -I fs -O dtb -o fs.test_tree1.test.dtb $FSBASE/test_tree1
run_test dtbs_equal_unordered -m fs.test_tree1.test.dtb test_tree1.dtb run_test dtbs_equal_unordered -m fs.test_tree1.test.dtb test_tree1.dtb
run_test get_next_tag_invalid_prop_len


## https://github.com/dgibson/dtc/issues/64 ## https://github.com/dgibson/dtc/issues/64
check_tests "$SRCDIR/phandle-args-overflow.dts" clocks_property check_tests "$SRCDIR/phandle-args-overflow.dts" clocks_property

Loading…
Cancel
Save