kexec: bounds and validation fixes for uImage and PE zboot
Subsystems (2)
cover kexec: bounds and validation fixes for uImage and PE zboot
Hi,
This series addresses two bounds validation issues reported by Tristan when processing untrusted or corrupted kernel images in kexec-tools:
1. In uImage_gz_load(), optional gzip header fields (EXTRA_FIELD, ORIG_NAME, COMMENT, HEAD_CRC) were parsed using unbounded scans and offset arithmetic without verifying whether the computed offset stayed within the input buffer. This could lead to out-of-bounds heap reads or buffer length underflow, crashing kexec.
Patch 1 first refactors uImage_gz_load() to use a common goto unwind path for freeing the decompression buffer on error.
Patch 2 adds the necessary bounds and length checks.
2. In pez_prepare(), payload_offset and payload_size are 32-bit values read from untrusted PE zboot headers. Adding them directly could wrap around, bypassing the bounds check and causing an out-of-bounds read when staging the payload.
Patch 3 checks that the buffer is large enough for the header and avoids wrap-around by using defensive subtraction.
---
Simon Horman (3):
uImage: use goto for unwind in uImage_gz_load
uImage: add bounds checks when parsing gzip headers in uImage_gz_load()
pe-zboot: fix integer overflow in payload bounds check in pez_prepare()
kexec/kexec-pe-zboot.c | 6 ++++- kexec/kexec-uImage.c | 61 +++++++++++++++++++++++++++++++++----------------- 2 files changed, 45 insertions(+), 22 deletions(-)
base-commit: 0fd0d8e9ea975696fdc05ad267a65f3efd415612
PATCH 1/3 uImage: use goto for unwind in uImage_gz_load
In the case where an error requires uncmp_buf to be freed, which is many cases, use a goto label rather than repeatedly open-coding this clean-up.
This is in preparation for adding more such cases.
--- kexec/kexec-uImage.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)
kexec/kexec-uImage.c +11 -14
@@ -157,16 +157,13 @@ static int uImage_gz_load(const char *buf, off_t len, skip =10 ;/* check GZ magic */ -if (buf[0 ] !=0x1f || buf[1 ] !=0x8b ) { -free (uncomp_buf); -return -1 ; - } +if (buf[0 ] !=0x1f || buf[1 ] !=0x8b ) + goto err_free; flags = buf[3 ];if (buf[2 ] !=Z_DEFLATED || (flags &RESERVED ) !=0 ) {puts ("Error: Bad gzipped data\n" ); -free (uncomp_buf); -return -1 ; + goto err_free; }if (flags &EXTRA_FIELD ) {
@@ -190,10 +187,8 @@ static int uImage_gz_load(const char *buf, off_t len,/* - activates parsing gz headers */ ret =inflateInit2 (&strm, -MAX_WBITS ); -if (ret !=Z_OK ) { -free (uncomp_buf); -return -1 ; - } +if (ret !=Z_OK ) + goto err_free; strm.next_out = uncomp_buf; strm.avail_out = mem_alloc;
@@ -211,17 +206,15 @@ static int uImage_gz_load(const char *buf, off_t len, new_buf =realloc (uncomp_buf, mem_alloc);if (!new_buf) {inflateEnd (&strm); -free (uncomp_buf); -return -1 ; + goto err_free; } uncomp_buf = new_buf; strm.next_out = uncomp_buf + mem_alloc - inc_buf; strm.avail_out = inc_buf; }else { -free (uncomp_buf);printf ("Error during decompression %d\n" , ret); -return -1 ; + goto err_free; } }while (1 );
@@ -229,6 +222,10 @@ static int uImage_gz_load(const char *buf, off_t len, image->buf = (char *)uncomp_buf; image->len = mem_alloc - strm.avail_out;return 0 ; + +err_free: +free (uncomp_buf); +return -1 ; }#else static int uImage_gz_load(const char *UNUSED(buf), off_t UNUSED(len),
Signature · 1 line
2.56.0
PATCH 2/3 uImage: add bounds checks when parsing gzip headers in uImage_gz_load()
In reply to Simon HormanWhen 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.
--- kexec/kexec-uImage.c | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-)
kexec/kexec-uImage.c +29 -7
@@ -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;
Signature · 1 line
2.56.0
PATCH 3/3 pe-zboot: fix integer overflow in payload bounds check in pez_prepare()
In reply to Simon HormanIn pez_prepare(), z->payload_offset and z->payload_size are uint32_t values read from the untrusted file header. The validation check:
if (buf_sz < z->payload_offset + z->payload_size)
performs 32-bit unsigned addition. If the sum wraps around, the bounds check succeeds, causing a subsequent out-of-bounds heap access when writing the payload to the temporary decompression file.
Also, ensure buf_sz is large enough to contain the zboot header before dereferencing its fields.
Prevent the overflow by checking that z->payload_offset does not exceed buf_sz and that the remaining buffer (buf_sz - z->payload_offset) is at least z->payload_size.
Patch written with LLM assistance.
--- kexec/kexec-pe-zboot.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
kexec/kexec-pe-zboot.c +5 -1
@@ -51,6 +51,9 @@ int pez_prepare(const char *crude_buf, off_t buf_sz, int *kernel_fd,struct pe32plus_opt_hdr *opt_hdr;struct data_directory *dir; +if (buf_sz < (off_t )sizeof (*z)) +return -1 ; + z = (const struct linux_pe_zboot_header *)(crude_buf);if (memcmp (&z->image_type,"zimg" ,sizeof (z->image_type))) {
@@ -69,7 +72,8 @@ int pez_prepare(const char *crude_buf, off_t buf_sz, int *kernel_fd,return -1 ; } -if (buf_sz < z->payload_offset + z->payload_size) { +if ((uint64_t )buf_sz < z->payload_offset || + (uint64_t )buf_sz - z->payload_offset < z->payload_size) {dbgprintf ("%s: PE too small to contain complete payload.\n" , __func__);return -1 ; }
Signature · 1 line
2.56.0
Simon Horman
AppliedQuoted 20 lines · Simon Horman
Hi,
This series addresses two bounds validation issues reported by Tristan when processing untrusted or corrupted kernel images in kexec-tools:
1. In
uImage_gz_load(), optional gzip header fields (EXTRA_FIELD,ORIG_NAME, COMMENT,HEAD_CRC) were parsed using unbounded scans and offset arithmetic without verifying whether the computed offset stayed within the input buffer. This could lead to out-of-bounds heap reads or buffer length underflow, crashing kexec.Patch 1 first refactors
uImage_gz_load()to use a common goto unwind path for freeing the decompression buffer on error.
Patch 2 adds the necessary bounds and length checks.2. In
pez_prepare(),payload_offsetandpayload_sizeare 32-bit values read from untrusted PE zboot headers. Adding them directly could wrap around, bypassing the bounds check and causing an out-of-bounds read when staging the payload.
Quoted 8 lines
Patch 3 checks that the buffer is large enough for the header and avoids wrap-around by using defensive subtraction.
---
Simon Horman (3):
uImage: use goto for unwind inuImage_gz_load
uImage: add bounds checks when parsing gzip headers inuImage_gz_load()
pe-zboot: fix integer overflow in payload bounds check inpez_prepare()
Applied:
- pe-zboot: fix integer overflow in payload bounds check in pez_prepare()
https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/commit/?id=0052381b29f3
- uImage: add bounds checks when parsing gzip headers in uImage_gz_load()
https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/commit/?id=ea20da8a3db8
- uImage: use goto for unwind in uImage_gz_load
https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/commit/?id=a315e6248d5f