[PATCH] readelf: reject incomplete Zstandard decompression

Alhuda Khan alhuda@research.digiscrypt.com
Fri Nov 20 18:20:06 GMT 2026


Dear Reviewer,

uncompress_section_contents() decompresses a Zstandard-compressed section into a buffer sized from the uncompressed size declared in the ELF compression header. ZSTD_decompress() can return fewer bytes than requested, and the code only checked ZSTD_isError(), so a section whose header overstates the uncompressed size was accepted. The unwritten tail of the buffer kept its previous (uninitialized) heap contents, which readelf then printed when dumping the section.

This was reproduced with a malformed ELF whose compressed .debug_info header declares a size 4096 bytes larger than the real content: before the change readelf prints the uninitialized bytes, after it the section is rejected with "Unable to decompress section".

Reject the result when the number of decompressed bytes does not match the declared uncompressed size.

Signed-off-by: Alhuda Khan <alhuda@research.digiscrypt.com>
---
 binutils/readelf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/binutils/readelf.c b/binutils/readelf.c
index 42734f621c5..9aa76e1e5d7 100644
--- a/binutils/readelf.c
+++ b/binutils/readelf.c
@@ -16634,7 +16634,7 @@ uncompress_section_contents (bool              is_zstd,
 #ifdef HAVE_ZSTD
       size_t ret = ZSTD_decompress (uncompressed_buffer, uncompressed_size,
 				    compressed_buffer, compressed_size);
-      if (ZSTD_isError (ret))
+      if (ZSTD_isError (ret) || ret != uncompressed_size)
 	goto fail;
 #endif
     }


More information about the Binutils mailing list