devicetree-compiler.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] fdtdump.c: make sure size_t argument to memchr is always unsigned.
@ 2016-07-13  0:31 Jean-Christophe Dubois
       [not found] ` <1468369873-3244-1-git-send-email-jcd-WBS85hRCVJbxB9160cZjhg@public.gmane.org>
  0 siblings, 1 reply; 2+ messages in thread
From: Jean-Christophe Dubois @ 2016-07-13  0:31 UTC (permalink / raw)
  To: devicetree-compiler-u79uwXL29TY76Z2rM5mHXA,
	david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+, jdl-CYoMK+44s/E
  Cc: Jean-Christophe Dubois

CID 132817 (#1 of 1): Integer overflowed argument (INTEGER_OVERFLOW)
15. overflow_sink: Overflowed or truncated value (or a value computed from an overflowed or truncated value) endp - p - 4L used as critical argument to function.

Signed-off-by: Jean-Christophe Dubois <jcd-WBS85hRCVJbxB9160cZjhg@public.gmane.org>
---

Changes since v1:
 * fix the post loop test.

 fdtdump.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/fdtdump.c b/fdtdump.c
index 95a6a20..a9a2484 100644
--- a/fdtdump.c
+++ b/fdtdump.c
@@ -15,6 +15,8 @@
 
 #include "util.h"
 
+#define FDT_MAGIC_SIZE	4
+
 #define ALIGN(x, a)	(((x) + ((a) - 1)) & ~((a) - 1))
 #define PALIGN(p, a)	((void *)(ALIGN((unsigned long)(p), (a))))
 #define GET_CELL(p)	(p += 4, *((const uint32_t *)(p-4)))
@@ -188,15 +190,15 @@ int main(int argc, char *argv[])
 
 	/* try and locate an embedded fdt in a bigger blob */
 	if (scan) {
-		unsigned char smagic[4];
+		unsigned char smagic[FDT_MAGIC_SIZE];
 		char *p = buf;
 		char *endp = buf + len;
 
 		fdt_set_magic(smagic, FDT_MAGIC);
 
 		/* poor man's memmem */
-		while (true) {
-			p = memchr(p, smagic[0], endp - p - 4);
+		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) {
@@ -215,7 +217,7 @@ int main(int argc, char *argv[])
 			}
 			++p;
 		}
-		if (!p)
+		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;
-- 
2.7.4

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

* Re: [PATCH v2] fdtdump.c: make sure size_t argument to memchr is always unsigned.
       [not found] ` <1468369873-3244-1-git-send-email-jcd-WBS85hRCVJbxB9160cZjhg@public.gmane.org>
@ 2016-07-23 14:41   ` David Gibson
  0 siblings, 0 replies; 2+ messages in thread
From: David Gibson @ 2016-07-23 14:41 UTC (permalink / raw)
  To: Jean-Christophe Dubois
  Cc: devicetree-compiler-u79uwXL29TY76Z2rM5mHXA, jdl-CYoMK+44s/E

[-- Attachment #1: Type: text/plain, Size: 2146 bytes --]

On Wed, Jul 13, 2016 at 02:31:13AM +0200, Jean-Christophe Dubois wrote:
> CID 132817 (#1 of 1): Integer overflowed argument (INTEGER_OVERFLOW)
> 15. overflow_sink: Overflowed or truncated value (or a value computed from an overflowed or truncated value) endp - p - 4L used as critical argument to function.
> 
> Signed-off-by: Jean-Christophe Dubois <jcd-WBS85hRCVJbxB9160cZjhg@public.gmane.org>

Applied.

fdtdump is a hacky tool, so I don't particularly care about bugs in
it, but I guess we might as well shut up the semantic checker.

> ---
> 
> Changes since v1:
>  * fix the post loop test.
> 
>  fdtdump.c | 10 ++++++----
>  1 file changed, 6 insertions(+), 4 deletions(-)
> 
> diff --git a/fdtdump.c b/fdtdump.c
> index 95a6a20..a9a2484 100644
> --- a/fdtdump.c
> +++ b/fdtdump.c
> @@ -15,6 +15,8 @@
>  
>  #include "util.h"
>  
> +#define FDT_MAGIC_SIZE	4
> +
>  #define ALIGN(x, a)	(((x) + ((a) - 1)) & ~((a) - 1))
>  #define PALIGN(p, a)	((void *)(ALIGN((unsigned long)(p), (a))))
>  #define GET_CELL(p)	(p += 4, *((const uint32_t *)(p-4)))
> @@ -188,15 +190,15 @@ int main(int argc, char *argv[])
>  
>  	/* try and locate an embedded fdt in a bigger blob */
>  	if (scan) {
> -		unsigned char smagic[4];
> +		unsigned char smagic[FDT_MAGIC_SIZE];
>  		char *p = buf;
>  		char *endp = buf + len;
>  
>  		fdt_set_magic(smagic, FDT_MAGIC);
>  
>  		/* poor man's memmem */
> -		while (true) {
> -			p = memchr(p, smagic[0], endp - p - 4);
> +		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) {
> @@ -215,7 +217,7 @@ int main(int argc, char *argv[])
>  			}
>  			++p;
>  		}
> -		if (!p)
> +		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;

-- 
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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

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

end of thread, other threads:[~2016-07-23 14:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-07-13  0:31 [PATCH v2] fdtdump.c: make sure size_t argument to memchr is always unsigned Jean-Christophe Dubois
     [not found] ` <1468369873-3244-1-git-send-email-jcd-WBS85hRCVJbxB9160cZjhg@public.gmane.org>
2016-07-23 14:41   ` 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).