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

Jan Beulich jbeulich@suse.com
Fri Nov 27 09:30:17 GMT 2026


On 24.11.2026 03:10, jelly wrote:
> Here's what I've changed in v2:

As you can see your reply is completely unreadable. Please send plain-text
mails only. And please start a new thread for submitting new versions of a
patch.

Jan

> A common symbol (SHN_COMMON) with a very large or non-power-of-two
> st_value can cause bfd_log2 to return 64, which then triggers
> undefined behaviour when used as a shift count:
> 
> 
>   sym.st_value = 1 << h->root.u.c.p->alignment_power;  /* elflink.c */
> 
> 
> bfd_log2 (st_value) returns 64 for any st_value > 2^63, and
> 1 << 64 (or even 1 << 31 on 32-bit int) is undefined.
> 
> 
> Cap the alignment result of bfd_log2 at 63 (the maximum useful
> alignment for a 64-bit address space) so 64-bit shifts are safe,
> and cast the left shift operands at the use sites to bfd_vma so
> the cap is effective everywhere the alignment is consumed.
> Guard the shifts in the alignment comparison warning code similarly.
> 
> 
> A st_value of 0 is valid (means "no alignment requirement", per
> sh_addralign semantics) and is not rejected --- bfd_log2 (0)
> returns 0, which is correct.  Large powers of two are also valid
> and are not rejected.
> 
> 
>         PR binutils/34996
>         * elflink.c (elf_link_add_object_symbols): Cap alignment
>         from bfd_log2 to avoid undefined behaviour.  Guard shifts
>         in the alignment comparison warning code.
>         (elf_link_output_extsym): Cast left shift operand to bfd_vma.
>         (bfd_elf_discard_info): Likewise.
>         * linker.c (bfd_generic_define_common_symbol): Cast left
>         shift operand to bfd_vma.
> 
> 
> Signed-off-by: Haolin Xue <xuehaolin@uniontech.com>
> ---
>  bfd/elflink.c | 19 +++++++++++++++----
>  bfd/linker.c  |  3 ++-
>  2 files changed, 17 insertions(+), 5 deletions(-)
> 
> 
> diff --git a/bfd/elflink.c b/bfd/elflink.c
> index 1eef868be49..e1a98c8962d 100644
> --- a/bfd/elflink.c
> +++ b/bfd/elflink.c
> @@ -5469,6 +5469,13 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
>                  We need to get the alignment from the section.  */
>               align = new_sec->alignment_power;
>             }
> +         /* PR binutils/34996: A common symbol with a very large or
> +            non-power-of-two st_value can cause bfd_log2 to return 64,
> +            which would be undefined behaviour when later used as a
> +            shift count.  Cap at the maximum useful alignment for a
> +            64-bit address space.  */
> +         if (align >= 8 * sizeof (bfd_vma))
> +           align = 8 * sizeof (bfd_vma) - 1;
>           if (align > old_alignment)
>             h->root.u.c.p->alignment_power = align;
>           else
> @@ -5598,8 +5605,12 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
>               if (normal_align < common_align)
>                 {
>                   /* PR binutils/2735 */
> -                 uint64_t c_align = UINT64_C (1) << common_align;
> -                 uint64_t n_align = UINT64_C (1) << normal_align;
> +                 /* PR binutils/34996: Guard against bfd_log2 returning 64
> +                    for a common symbol with a very large st_value.  */
> +                 uint64_t c_align = common_align < 8 * sizeof (c_align)
> +                   ? UINT64_C (1) << common_align : 0;
> +                 uint64_t n_align = normal_align < 8 * sizeof (n_align)
> +                   ? UINT64_C (1) << normal_align : 0;
>                   if (normal_bfd == NULL)
>                     _bfd_error_handler
>                       /* xgettext:c-format */
> @@ -10989,7 +11000,7 @@ elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
>      case bfd_link_hash_common:
>        input_sec = h->root.u.c.p->section;
>        sym.st_shndx = bed->common_section_index (input_sec);
> -      sym.st_value = 1 << h->root.u.c.p->alignment_power;
> +      sym.st_value = (bfd_vma) 1 << h->root.u.c.p->alignment_power;
>        break;
>  
>      case bfd_link_hash_indirect:
> @@ -15281,7 +15292,7 @@ bfd_elf_discard_info (struct bfd_link_info *info)
>           fini_reloc_cookie_for_section (&cookie, i);
>         }
>  
> -      eh_alignment = ((1 << o->alignment_power)
> +      eh_alignment = (((bfd_vma) 1 << o->alignment_power)
>                       * bfd_octets_per_byte (info->output_bfd, o));
>        /* Skip over zero terminator, and prevent empty sections from
>          adding alignment padding at the end.  */
> diff --git a/bfd/linker.c b/bfd/linker.c
> index c3dbe667cc9..7b4a04d693f 100644
> --- a/bfd/linker.c
> +++ b/bfd/linker.c
> @@ -3169,7 +3169,8 @@ bfd_generic_define_common_symbol (bfd *output_bfd,
>       not have any alignment requirement then do not increase the
>       alignment unnecessarily.  */
>    if (power_of_two)
> -    alignment = bfd_octets_per_byte (output_bfd, section) << power_of_two;
> +    alignment = (bfd_vma) bfd_octets_per_byte (output_bfd, section)
> +      << power_of_two;
>    else
>      alignment = 1;
>    BFD_ASSERT (alignment != 0 && (alignment & -alignment) == alignment);
> -- 
> 2.43.0
> 
> 
> 
> 
> 
> 原始邮件
> 发件人:Jan Beulich <jbeulich@suse.com>
> 发件时间:2026年11月20日 21:46
> 收件人:Haolin Xue <762233433@qq.com>
> 抄送:binutils <binutils@sourceware.org>
> 主题:Re: [PATCH] bfd: Validate common symbol alignment before bfd_log2 to avoid UB [PR binutils/34996]
> 
> 
> 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