readelf: Update dynamic tag processing and validate section info
Improves readelf's handling of dynamic tags and section information, preventing crashes and providing better warnings for invalid data.
This patch enhances readelf’s robustness by updating its dynamic tag processing and introducing validation for section information. It now correctly counts DT_RELR relocations using dynamic tags when section headers are not present, and crucially, it ignores section sh_offset and sh_size values if they exceed the file size. This prevents segfaults and introduces warnings for out-of-range section data, improving diagnostic capabilities.
- proposer
Introduces changes to `readelf` to handle dynamic tags more robustly and validate section information, preventing crashes and adding warnings for invalid data.
“Count DT_RELR relocations using dynamic tags when section header isn't used. Ignore section sh_offset and sh_size if they are larger than file size. One side effect is that we now display readelf: Warning: Ignore the out of range sh_offset value of 2199023261148 for section 17 readelf: Warning: Ignore the out of range sh_size value of 13 for section 17 with sh_offset value of 2199023261148 ...…”
- proposer
Provides updated information about the changes, emphasizing the handling of out-of-range section offset and size values.
“Count DT_RELR relocations using dynamic tags when section header isn't used. Ignore section sh_offset and sh_size if they are larger than file size. One side effect is that we now display ... and segfault.”
Technical Tradeoffs
- Improves robustness against malformed files by ignoring invalid data, potentially hiding underlying issues that might be relevant for debugging.
- Adds warnings for previously unhandled out-of-range values, providing clearer diagnostics.
In Details
This change to readelf improves its resilience to malformed ELF files by better handling dynamic tag processing, particularly for DT_RELR relocations, and by validating section header fields (sh_offset, sh_size) against the overall file size. Previously, excessively large values in these fields could lead to segfaults or incorrect output; this patch introduces checks to ignore such out-of-range values and provides informative warnings, improving the tool's diagnostic capabilities.
- readelf
- A utility that displays information about ELF files, including headers, section details, symbol tables, and relocation entries.
- ELF
- Executable and Linkable Format, a standard file format for executables, object code, shared libraries, and core dumps on many Unix-like systems.
- dynamic tag
- Entries in the ELF dynamic section (e.g., DT_RELR) that provide information necessary for dynamic linking and runtime loading of executables and libraries.
- DT_RELR
- An ELF dynamic tag that indicates the presence and location of the relocation array, often used in position-independent code (PIC) or for specific relocation types.
- section header
- Metadata describing each section within an ELF file, including its name, size, offset in the file, and attributes.
- sh_offset
- The file offset of the section's content.
- sh_size
- The size of the section's content in bytes.