Skip to contentSkip to conversation

(patch) segfault when calling fit_check_format() on corrupt FIT images

U-Boot Jon Nalley 3 participants 5 messages Mar 5, 2010 – Mar 10, 2010

lore
Share & tools
lore

Jon Nalley

author
#0 lore raw

All,

I found that fit_check_format() was causing a segfault when run on a corrupt FIT image. I tracked the problem down to line 92 in libfdt/fdt_ro.c in _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.

libfdt/fdt_ro.c Fri Mar 05 11:10:21 2010 -0600 renamed from libfdt/fdt_ro.c Fri Mar 05 06:52:52 2010 -0600 +1 -1
@@ -89,7 +89,7 @@
 {
        const char *p = fdt_string(fdt, stroffset);
 
-       return (strlen(p) == len) && (memcmp(p, s, len) == 0);
+       return (strnlen(p, len) == len) && (memcmp(p, s, len) == 0);
 }
 
 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
#1 lore raw

Hi Jon,

Quoted 8 lines

I found that fit_check_format() was causing a segfault when run on a corrupt FIT image. I tracked the problem down to line 92 in libfdt/fdt_ro.c in _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.

We are a bit reluctant to accept changes here as this is shared code with the 'dtc' device tree compiler[1].

Also when glancing over the code, it seems like there may be more places where a corrupt fdt may backfire so this makes me also sceptic if this single fix is a useful thing.

Stepping back a little bit, I don't even know why we should trap such a problem at all - after all while developing we have quite a few possibilities to shoot ourselves in the foot. In a production system such a thing should not happen and if it does, it will be caught by a sensible infrastructure and e.g. a hardware watchdog.

libfdt/fdt_ro.c Fri Mar 05 11:10:21 2010 -0600 line 95

Quoted 11 lines
libfdt/fdt_ro.c Fri Mar 05 11:10:21 2010 -0600 renamed from libfdt/fdt_ro.c Fri Mar 05 06:52:52 2010 -0600 +1 -1
@@ -89,7 +89,7 @@
 {
        const char *p = fdt_string(fdt, stroffset);
 
-       return (strlen(p) == len) && (memcmp(p, s, len) == 0);
+       return (strnlen(p, len) == len) && (memcmp(p, s, len) == 0);
 }
 
 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)

On the other hand if you do insist on your change, then pleas send git patches as written in the documentation[2].

Cheers
Detlev

[1] http://jdl.com/software/
[2] http://www.denx.de/wiki/U-Boot/Patches

Signature · 8 lines
[Linux] USB consoles was a  bad hack written on a drunken dare.   I'm still
constantly amazed that the thing even works at all, let alone the fact that
people are actually using it :) 
                            -- Greg KH <[email protected]>
--
DENX Software Engineering GmbH,      MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich,  Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-40 Fax: (+49)-8142-66989-80 Email: dzu at denx.de
author
#2 lore raw
Quoted 2 lines · Detlev Zundel

We are a bit reluctant to accept changes here as this is shared code with the 'dtc' device tree compiler[1].

Understandable.

Quoted 3 lines

Also when glancing over the code, it seems like there may be more places where a corrupt fdt may backfire so this makes me also sceptic if this single fix is a useful thing.

I agree. It looks like there might be more than one case where a non null terminated string could cause problems.

Quoted 5 lines

Stepping back a little bit, I don't even know why we should trap such a problem at all - after all while developing we have quite a few possibilities to shoot ourselves in the foot. ?In a production system such a thing should not happen and if it does, it will be caught by a sensible infrastructure and e.g. a hardware watchdog.

In our environment we have redundant FIT images in flash. This issue was caught by one of our testers who was purposely powering off the device during a firmware upgrade (in order to test the redundancy). The segfault is observed after rebooting to the remaining "good" image and invoking a command that calls fit_check_format(). The issue occurs after the flash has been erased, since in this case the erased flash is all 1's no terminating null character is ever found.

My goal is to be able to determine (while in Linux) if one of the two firmware images in flash is corrupt or not. Although our platform supports a hardware watchdog, I am unclear how it could solve this particular problem? Granted, this is a fairly obscure case and it is not likely that users will be powering off hardware during firmware upgrades, but I wanted to at least describe the scenario to see what others might think. I would appreciate any suggestions for other methods of determining if a FIT image in flash is valid or not.

Quoted 2 lines

On the other hand if you do insist on your change, then pleas send git patches as written in the documentation[2].

Sorry about that. I will submit any further patches as per the documentation.

Regards,

Jon Nalley

#3 lore raw

Dear Jon Nalley,

Quoted 9 lines · In message <[email protected]> you…

My goal is to be able to determine (while in Linux) if one of the two firmware images in flash is corrupt or not. Although our platform supports a hardware watchdog, I am unclear how it could solve this particular problem? Granted, this is a fairly obscure case and it is not likely that users will be powering off hardware during firmware upgrades, but I wanted to at least describe the scenario to see what others might think. I would appreciate any suggestions for other methods of determining if a FIT image in flash is valid or not.

Corruption should be detected by the checksum tests we're doing.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
It seems intuitively obvious to me, which  means  that  it  might  be
wrong.                                                 -- Chris Torek
author
#4 lore raw
Quoted 1 line · Wolfgang Denk

Corruption should be detected by the checksum tests we're doing.

It is possible to have a FIT image that is corrupted such that when fit_check_format() is called it segfaults (due to strlen() being called on data that is not null terminated). It is then not possible to read the checksums from the FIT image for comparison.

Is there another checksum I should be looking at?

My target platform is powerpc 405 and uses uclibc 0.9.30.2. Again, I am trying to determine the status of a FIT image from Linux, not from u-boot. I noticed that the issue does not occur on x86 Linux with glibc, perhaps there is a difference in in the strlen() implementation.

Since the strlen() bug is in libfdt, it is probably better for me to communicate this issue to the libfdt developers.

Regards,

Jon Nalley