x86 linker rejects foreign ELF backends, fixing a heap overflow
elf_x86_hash_table now accepts only x86 target ids, so a script that retargets output to a non-x86 ELF format can't corrupt the heap.
A linker script such as OUTPUT_FORMAT(“elf64-bpfle”) can retarget the output BFD while the x86 emulation is still active. bfd_elf_linker_x86_set_options then takes the target id from the foreign backend. The id check in elf_x86_hash_table passed because the id and the hash table came from the same foreign backend. The x86 code then treated that table as struct elf_x86_link_hash_table. Writing htab->params overflowed the heap block, an 8-byte write 424 bytes past a 1848-byte allocation under AddressSanitizer. The fix checks the id after is_elf_hash_table and accepts only x86 ids. Non-ELF outputs such as “binary” still short-circuit before bed->target_id is read, because those formats have no ELF backend data and reading the id would segfault. New tests arch-confusion.exp and arch-confusion.s cover both cases (PR ld/34874).
In Details
elf_x86_hash_table in bfd/elfxx-x86.h is the checked accessor for struct elf_x86_link_hash_table. It compared bed->target_id against the caller's expected id. bfd_elf_linker_x86_set_options takes that expected id from the output BFD's own backend data. After a script retargets the output, the id and the table both come from the foreign backend, so the comparison is a tautology. Check ordering matters. is_elf_hash_table must run first, because a non-ELF output like "binary" has no elf_backend_data.
- elf_x86_hash_table
- An accessor macro in bfd/elfxx-x86.h. It returns the x86-specific extension of the ELF link hash table, or NULL if the table isn't an x86 one.
- target_id
- A field in
elf_backend_datathat identifies which architecture backend created an ELF link hash table. Code uses it to check that a table cast to a backend-specific struct is the right type. - OUTPUT_FORMAT
- A linker-script command that sets the output BFD target, for example elf64-bpfle. It can change the output backend after the emulation has been chosen.
- elf_backend_data
- A per-target structure of BFD ELF parameters and hooks. Linker code reaches it through the output BFD, and non-ELF targets such as "binary" have none.
- AddressSanitizer
- A compiler-instrumented runtime that detects out-of-bounds memory access and similar errors. The foreign-ELF case in the new test only shows the overflow under it.