devicetree-compiler.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] dtc: fdtdump: check fdt if not in scanning mode
@ 2016-12-20 22:32 Heinrich Schuchardt
       [not found] ` <20161220223216.28686-1-xypron.glpk-Mmb7MZpHnFY@public.gmane.org>
  0 siblings, 1 reply; 6+ messages in thread
From: Heinrich Schuchardt @ 2016-12-20 22:32 UTC (permalink / raw)
  To: David Gibson, Jon Loeliger
  Cc: devicetree-compiler-u79uwXL29TY76Z2rM5mHXA, Heinrich Schuchardt

Running fdtdump without scan mode for an invalid file often
results in a segmentation fault because the fdt header is
not checked.

With the patch the header is checked both in scanning as
well as in non-scanning mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk-Mmb7MZpHnFY@public.gmane.org>
---
 fdtdump.c | 57 +++++++++++++++++++++++++++++----------------------------
 1 file changed, 29 insertions(+), 28 deletions(-)

diff --git a/fdtdump.c b/fdtdump.c
index a9a2484..717fef5 100644
--- a/fdtdump.c
+++ b/fdtdump.c
@@ -189,39 +189,40 @@ int main(int argc, char *argv[])
 		die("could not read: %s\n", file);
 
 	/* try and locate an embedded fdt in a bigger blob */
-	if (scan) {
-		unsigned char smagic[FDT_MAGIC_SIZE];
-		char *p = buf;
-		char *endp = buf + len;
+	unsigned char smagic[FDT_MAGIC_SIZE];
+	char *p = buf;
+	char *endp = buf + len;
 
-		fdt_set_magic(smagic, FDT_MAGIC);
+	fdt_set_magic(smagic, FDT_MAGIC);
 
-		/* poor man's memmem */
-		while ((endp - p) >= FDT_MAGIC_SIZE) {
-			p = memchr(p, smagic[0], endp - p - FDT_MAGIC_SIZE);
-			if (!p)
+	/* poor man's memmem */
+	while ((endp - p) >= FDT_MAGIC_SIZE) {
+		p = memchr(p, smagic[0], endp - p - FDT_MAGIC_SIZE);
+		if (!p)
+			break;
+		if (fdt_magic(p) == FDT_MAGIC) {
+			/* try and validate the main struct */
+			off_t this_len = endp - p;
+			fdt32_t max_version = 17;
+			if (fdt_version(p) <= max_version &&
+			    fdt_last_comp_version(p) < max_version &&
+			    fdt_totalsize(p) < this_len &&
+			    fdt_off_dt_struct(p) < this_len &&
+				fdt_off_dt_strings(p) < this_len)
 				break;
-			if (fdt_magic(p) == FDT_MAGIC) {
-				/* try and validate the main struct */
-				off_t this_len = endp - p;
-				fdt32_t max_version = 17;
-				if (fdt_version(p) <= max_version &&
-				    fdt_last_comp_version(p) < max_version &&
-				    fdt_totalsize(p) < this_len &&
-				    fdt_off_dt_struct(p) < this_len &&
-					fdt_off_dt_strings(p) < this_len)
-					break;
-				if (debug)
-					printf("%s: skipping fdt magic at offset %#zx\n",
-						file, p - buf);
-			}
-			++p;
+			if (debug)
+				printf("%s: skipping fdt magic at offset %#zx\n",
+					file, p - buf);
 		}
-		if (!p || ((endp - p) < FDT_MAGIC_SIZE))
-			die("%s: could not locate fdt magic\n", file);
-		printf("%s: found fdt at offset %#zx\n", file, p - buf);
-		buf = p;
+		if (!scan)
+			die("%s: fdt missing\n", file);
+		++p;
 	}
+	if (!p || ((endp - p) < FDT_MAGIC_SIZE))
+		die("%s: could not locate fdt magic\n", file);
+	if (scan)
+		printf("%s: found fdt at offset %#zx\n", file, p - buf);
+	buf = p;
 
 	dump_blob(buf, debug);
 
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-12-26 22:47 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-12-20 22:32 [PATCH 1/1] dtc: fdtdump: check fdt if not in scanning mode Heinrich Schuchardt
     [not found] ` <20161220223216.28686-1-xypron.glpk-Mmb7MZpHnFY@public.gmane.org>
2016-12-21 22:41   ` David Gibson
     [not found]     ` <20161221224138.GA14282-K0bRW+63XPQe6aEkudXLsA@public.gmane.org>
2016-12-21 23:59       ` [PATCH 1/1 v2] " Heinrich Schuchardt
     [not found]         ` <20161221235906.24281-1-xypron.glpk-Mmb7MZpHnFY@public.gmane.org>
2016-12-26  5:24           ` Simon Glass
     [not found]             ` <CAPnjgZ0Tu-UdEg0Yx1aX+1exnXsR7ukN8d7hCDuxMCDRzboMRQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-12-26 22:42               ` David Gibson
2016-12-26 22:47           ` David Gibson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).