Skip to contentSkip to conversation

libfdt: Fix segfault when calling fit_check_format() on corrupt FIT images

PATCH New Tom Rini 1 participant 2 messages Feb 26, 2014 – Jun 19, 2014

lore
Share & tools
lore

Tom Rini

author
#0 lore raw

PATCH v2 1/1 libfdt: Fix segfault when calling fit_check_format() on corrupt FIT images

From: Jon Nalley <[email protected]>

It has been observed that fit_check_format() will fail when passed a corrupt FIT image. This was tracked down to _fdt_string_eq():
return (strlen(p) == len) && (memcmp(p, s, len) == 0);

In the case of a corrupt FIT image one can't depend on 'p' being NULL terminated. I changed it to use strnlen() to fix the issue.

---
Changes in v2:
- Pass len + 1, not len to strnlen as that's the best practice for strnlen.

---
 lib/libfdt/fdt_ro.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/libfdt/fdt_ro.c +1 -1
index f2154e8..36af043 100644
@@ -44,7 +44,7 @@ static int _fdt_string_eq(const void *fdt, int stroffset,
 {
 	const char *p = fdt_string(fdt, stroffset);
 
-	return (strlen(p) == len) && (memcmp(p, s, len) == 0);
+	return (strnlen(p, len + 1) == len) && (memcmp(p, s, len) == 0);
 }
 
 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
Signature · 1 line
1.7.9.5
author
#1 lore raw

PATCH v2 1/1 libfdt: Fix segfault when calling fit_check_format() on corrupt FIT images

Quoted 10 lines · Tom Rini

From: Jon Nalley <[email protected]>

It has been observed that fit_check_format() will fail when passed a corrupt FIT image. This was tracked down to _fdt_string_eq():
return (strlen(p) == len) && (memcmp(p, s, len) == 0);

In the case of a corrupt FIT image one can't depend on 'p' being NULL terminated. I changed it to use strnlen() to fix the issue.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140619/be91ee53/attachment.pgp>