bfd: Validate common symbol alignment before bfd_log2
Fixes undefined behavior in bfd by validating common symbol alignment before log2 calculation.
This patch adds validation guards to the bfd_log2 function call in elf_link_add_object_symbols. It prevents undefined behavior that occurs when a malformed ELF object has a common symbol whose st_value is not a power of two. The fix rejects invalid st_value values and ensures proper error handling and memory cleanup.
- proposer
Adds validation guards to elf_link_add_object_symbols to prevent undefined behavior caused by malformed common symbols.
“A malformed ELF object with a common symbol whose st_value is not a power of two (e.g., 0xffffff000000ffff) causes bfd_log2 to return 64, which then triggers undefined behavior at the 1 << alignment_power shift (elflink.c:10992).”
In Details
This change addresses an undefined behavior in the BFD library's ELF linking logic. Specifically, it validates the st_value of common symbols before passing it to bfd_log2. The st_value is used to determine alignment, and non-power-of-two values can lead to bfd_log2 returning 64, causing a shift overflow in elf_link_add_object_symbols.
- BFD
- The Binary File Descriptor library, a low-level library used by GNU Binutils to parse and manipulate object files.
- ELF
- Executable and Linkable Format, a common standard for object file formats on Unix-like systems.
- st_value
- A field in the ELF symbol table entry that, for common symbols, indicates the symbol's alignment requirement.
- undefined behavior
- Program behavior that results from an error, such as memory corruption or an invalid operation, for which the C and C++ standards impose no requirements.