ld: do not read ELF fields from a generic link hash table
The linker now checks if the link hash table is an ELF table before accessing ELF-specific fields, preventing heap buffer overflows.
This patch addresses an out-of-bounds read vulnerability in the linker (ld). When a linker script specifies an ELF flavor that uses a generic link hash table (e.g., OUTPUT_FORMAT(“elf64-nfp”)), the ldelf_after_open_output hook incorrectly attempts to read ELF-specific fields like hash_table_id. This patch adds a check for is_elf_hash_table to ensure that these fields are only accessed when the hash table is indeed an ELF type, thereby preventing heap buffer overflows. A related test case is also extended.
- proposer
Proposes a fix to prevent out-of-bounds reads in the linker by checking for ELF hash table type before accessing ELF-specific fields.
“ldelf_after_open_output assumes that an ELF output BFD implies an ELF link hash table. A linker script can select an ELF flavour that uses the generic hash table, for example OUTPUT_FORMAT("elf64-nfp"). The hook then reads hash_table_id, which is past the end of the generic table (AddressSanitizer: heap-buffer-overflow). Check is_elf_hash_table before reading the table, as most other uses of l…”
In Details
This patch addresses a heap buffer overflow in the ldelf_after_open_output function within the GNU linker (ld). The issue arises when a linker script specifies an ELF output format that utilizes a generic link hash table instead of an ELF-specific one (e.g., OUTPUT_FORMAT("elf64-nfp")). In such cases, the function incorrectly assumes an ELF hash table and attempts to read ELF-specific fields like hash_table_id, leading to an out-of-bounds read. The fix introduces a check is_elf_hash_table to ensure the hash table is of the expected ELF type before accessing these fields. This is par…
- ld
- The GNU linker, responsible for combining object files and libraries into an executable or shared library.
- link hash table
- A data structure used by the linker to efficiently store and retrieve information about symbols and object files during the linking process.
- ELF
- Executable and Linkable Format, a common standard file format for object files, executable files, shared libraries, and core dumps on Unix-like systems.
- generic link hash table
- A non-ELF specific version of the link hash table used by the linker, which might be employed when targeting non-ELF output formats or specific ELF configurations.
- heap-buffer-overflow
- A type of security vulnerability that occurs when a program writes data beyond the allocated buffer on the heap, potentially corrupting adjacent memory or control structures.