readelf: reject incomplete Zstandard decompression
Fix readelf to reject Zstandard-compressed sections where the decompressed size does not match the declared size, preventing the display of uninitialized data.
This patch addresses an issue in readelf where Zstandard-compressed sections could be accepted even if the decompression process returned fewer bytes than declared in the ELF header. This led to readelf printing uninitialized heap contents from the unwritten tail of the buffer. The fix ensures that the number of decompressed bytes must match the declared uncompressed size.
- proposer
Submits a patch to `readelf` to reject Zstandard-compressed sections if the decompressed output size does not match the declared uncompressed size, preventing the display of uninitialized data.
“Reject the result when the number of decompressed bytes does not match the declared uncompressed size.”
In Details
The uncompress_section_contents function in readelf handles Zstandard decompression. Previously, it only checked ZSTD_isError() and not the returned decompressed size against the uncompressed_size specified in the ELF compression header. This could lead to reading uninitialized memory if the actual decompressed data was smaller than expected. This patch adds a check for ret != uncompressed_size.
- readelf
- A utility that displays information about ELF (Executable and Linkable Format) files.
- Zstandard
- A fast lossless compression algorithm.
- ELF
- Executable and Linkable Format, a common standard file format for executables, object code, shared libraries, and core dumps.
- AddressSanitizer
- A memory error detector for C/C++ that finds use-after-free, buffer overflows, and use-after-return.