[PATCH] bfd: Validate common symbol alignment before bfd_log2 to avoid UB [PR binutils/34996]

Jan Beulich jbeulich@suse.com
Fri Nov 20 13:46:30 GMT 2026


On 20.11.2026 04:35, Haolin Xue wrote:
> 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).
> 
> Add validation guards at both bfd_log2(isym->st_value) call sites in
> elf_link_add_object_symbols:
>   - elflink.c:5465 (initial common symbol alignment)
>   - elflink.c:5593 (common alignment when overridden by normal definition)
> 
> Reject st_value that is zero, not a power of two, or >= 2^31,
> reporting the offending object and symbol name.

A value of 0 means "no alignment requirement", just like a value of 1. The
spec I'm looking at is explicit about this, be referring to sh_addralign.
bfd_log2() also properly deals with an input of 0.

Limiting to below 2^31 also doesn't feel right. There's nothing conceptually
wrong with bigger values. Bad calculations like ...

> The >= 2^31 bound avoids a second UB at elflink.c:10992 where
> '1 << alignment_power' with alignment_power=31 would overflow int.

... this one need adjusting. There's no good reason to use plain 1 there.
Anything that fits in sym.st_value can be used, i.e. the 1 wants casting to
sym.st_value's type (or maybe bfd_vma).

Jan


More information about the Binutils mailing list