[PATCH kexec-tools 2/3] uImage: add bounds checks when parsing gzip headers in uImage_gz_load()
Simon Horman
horms at kernel.org
Tue Nov 10 03:35:17 PST 2026
When unpacking a gzip-compressed uImage payload in uImage_gz_load(),
optional header fields (EXTRA_FIELD, ORIG_NAME, COMMENT, HEAD_CRC)
are parsed using offset arithmetic and unbounded NUL-terminator scans.
If a corrupted or truncated uImage is supplied, skip can exceed the
buffer length len, or the string scanner can read past the end of the
buffer searching for '\0', ultimately leading to out-of-bounds reads
or an underflow of strm.avail_in and a crash in inflate().
Add explicit bounds checking for each optional gzip header field to
ensure skip never exceeds len.
Patch written with LLM assistance.
Reported-by: Tristan <TristanInSec at gmail.com>
Signed-off-by: Simon Horman <horms at kernel.org>
---
kexec/kexec-uImage.c | 36 +++++++++++++++++++++++++++++-------
1 file changed, 29 insertions(+), 7 deletions(-)
diff --git a/kexec/kexec-uImage.c b/kexec/kexec-uImage.c
index 76b8c73a3494..50893e7318ee 100644
--- a/kexec/kexec-uImage.c
+++ b/kexec/kexec-uImage.c
@@ -153,6 +153,9 @@ static int uImage_gz_load(const char *buf, off_t len,
memset(&strm, 0, sizeof(strm));
+ if (len < 10)
+ goto err_free;
+
/* Skip magic, method, time, flags, os code ... */
skip = 10;
@@ -167,20 +170,39 @@ static int uImage_gz_load(const char *buf, off_t len,
}
if (flags & EXTRA_FIELD) {
+ unsigned int extra_len;
+
+ if (len - skip < 2)
+ goto err_free;
+ extra_len = (unsigned char)buf[skip] |
+ (unsigned char)buf[skip + 1] << 8;
skip += 2;
- skip += buf[10];
- skip += buf[11] << 8;
+ if (len - skip < extra_len)
+ goto err_free;
+ skip += extra_len;
}
if (flags & ORIG_NAME) {
- while (buf[skip++])
- ;
+ while (skip < len && buf[skip] != '\0')
+ skip++;
+ if (skip >= len)
+ goto err_free;
+ skip++; /* skip the '\0' */
}
if (flags & COMMENT) {
- while (buf[skip++])
- ;
+ while (skip < len && buf[skip] != '\0')
+ skip++;
+ if (skip >= len)
+ goto err_free;
+ skip++; /* skip the '\0' */
}
- if (flags & HEAD_CRC)
+ if (flags & HEAD_CRC) {
+ if (len - skip < 2)
+ goto err_free;
skip += 2;
+ }
+
+ if (skip >= len)
+ goto err_free;
strm.avail_in = len - skip;
strm.next_in = (void *)buf + skip;
--
2.56.0
More information about the kexec
mailing list