Skip to contentSkip to conversation

mkimage: fix option parsing segfault

PATCH In review Carlos López 2 participants 2 messages Apr 24, 2025 – Apr 28, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mkimage: fix option parsing segfault

getopt_long() expects a NULL-terminated list of structures. The current list in mkimage does not have a zero-filled structure at the end, which can cause getopt_long() to walk past the end of the array when passing an unknown option, causing a segmentation fault.

As a reproducer, the following command causes a segmentation fault (tested in Debian 12):

    mkimage --foobar
---
 tools/mkimage.c | 1 +
 1 file changed, 1 insertion(+)
tools/mkimage.c +1 -0
index ac62ebbde9b..2954626a283 100644
@@ -196,6 +196,7 @@ static const struct option longopts[] = {
 	{ "verbose", no_argument, NULL, 'v' },
 	{ "version", no_argument, NULL, 'V' },
 	{ "xip", no_argument, NULL, 'x' },
+	{ /* sentinel */ },
 };
 
 static void process_args(int argc, char **argv)
Signature · 1 line
2.39.5
#1 lore raw
Quoted 9 lines · Carlos López

getopt_long() expects a NULL-terminated list of structures. The current list in mkimage does not have a zero-filled structure at the end, which can cause getopt_long() to walk past the end of the array when passing an unknown option, causing a segmentation fault.

As a reproducer, the following command causes a segmentation fault (tested in Debian 12):

[...]

Applied to u-boot/master, thanks!

[1/1] mkimage: fix option parsing segfault

      commit: 2c529839a93d886253c596449545f62bec21a4ac
Signature · 1 line
Tom