From 05dec6d1827dc7016cad11c4ddfe8f965bceddb7 Mon Sep 17 00:00:00 2001 From: Mike McTernan Date: Fri, 22 Jul 2022 11:44:33 +0100 Subject: [PATCH] Fix integer wrap sanitisation. Test: make check Test: afl-clang with new corpus data Bug: 239630493 Change-Id: I232155e7f7a54271a6a3e3a7cd91ed6bbabc051f --- libfdt/fdt.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/libfdt/fdt.c b/libfdt/fdt.c index 9fe7cf4..c17cad5 100644 --- a/libfdt/fdt.c +++ b/libfdt/fdt.c @@ -188,12 +188,20 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset) break; case FDT_PROP: - lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp)); + lenp = fdt_offset_ptr(fdt, offset, sizeof(struct fdt_property) - FDT_TAGSIZE); if (!can_assume(VALID_DTB) && !lenp) return FDT_END; /* premature end */ - /* skip-name offset, length and value */ - offset += sizeof(struct fdt_property) - FDT_TAGSIZE - + fdt32_to_cpu(*lenp); + + /* skip name offset, length */ + offset += sizeof(struct fdt_property) - FDT_TAGSIZE; + + if (!can_assume(VALID_DTB) + && !fdt_offset_ptr(fdt, offset, fdt32_to_cpu(*lenp))) + return FDT_END; /* premature end */ + + /* skip value */ + offset += fdt32_to_cpu(*lenp); + if (!can_assume(LATEST) && fdt_version(fdt) < 0x10 && fdt32_to_cpu(*lenp) >= 8 && ((offset - fdt32_to_cpu(*lenp)) % 8) != 0) @@ -209,7 +217,8 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset) return FDT_END; } - if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset)) + if (!can_assume(VALID_DTB) && (offset <= startoffset + || !fdt_offset_ptr(fdt, startoffset, offset - startoffset))) return FDT_END; /* premature end */ *nextoffset = FDT_TAGALIGN(offset);