readelf: Improve section header handling and error reporting
Readelf now preserves and displays original section header values, fixes a buffer overflow risk, and improves error messages.
This commit enhances the readelf utility by preserving and dumping original section header values, aiding in debugging. It also addresses a potential buffer overflow in orig_section_headers that could occur if memory allocation failed during file processing before the data was freed. Additionally, warning messages have been refined for better clarity and reduced translator effort, and specific malloc failure details are no longer reported for small buffer allocations.
In Details
The readelf utility's section header processing is improved. Specifically, the save_original_section_header_values function now uses xmalloc for reliability. The validate_section_info function has its parameters reduced and warning messages rewritten. Crucially, the patch fixes a potential overflow in orig_section_headers by correcting how it's allocated and accessed across get_32bit_section_headers, get_64bit_section_headers, and free_filedata, especially in probing scenarios. process_relocs is also updated for relr handling.
- readelf
- A utility in GNU Binutils that displays information about ELF (Executable and Linkable Format) files, including section headers, symbol tables, and relocation entries.
- Section Header
- Metadata within an ELF file that describes each section. It includes information like the section's name, type, size, and memory address.
- ELF relocation
- Information within an ELF object file that specifies how to modify addresses in the code or data when the object is linked or loaded into memory. This allows code to be position-independent or to correctly reference external symbols.
- Buffer overflow
- A condition where a program writes data beyond the allocated buffer boundaries, potentially corrupting adjacent memory and leading to crashes or security vulnerabilities.
- xmalloc
- A memory allocation function that automatically exits the program if allocation fails, simplifying error handling compared to standard
malloc. - relr
- Relative relocation entries. A compact representation of relocations used in some ELF variants, particularly for Position-Independent Code (PIC).