HomeFreeBSD

link_elf: fix SysV hash function overflow

Description

link_elf: fix SysV hash function overflow

Quoting from https://maskray.me/blog/2023-04-12-elf-hash-function:

The System V Application Binary Interface (generic ABI) specifies the
ELF object file format. When producing an output executable or shared
object needing a dynamic symbol table (.dynsym), a linker generates a
.hash section with type SHT_HASH to hold a symbol hash table. A DT_HASH
tag is produced to hold the address of .hash.

The function is supposed to return a value no larger than 0x0fffffff.
Unfortunately, there is a bug. When unsigned long consists of more than
32 bits, the return value may be larger than UINT32_MAX. For instance,
elf_hash((const unsigned char *)"\xff\x0f\x0f\x0f\x0f\x0f\x12") returns
0x100000002, which is clearly unintended, as the function should behave
the same way regardless of whether long represents a 32-bit integer or
a 64-bit integer.

Reviewed by: kib, Fangrui Song
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D39517

(cherry picked from commit 2ef2c26f3f132af33f6f12cd7b27d4dbbd7fa435)

Details

Provenance
emasteAuthored on Apr 12 2023, 2:04 PM
Reviewer
kib
Differential Revision
D39517: rtld: fix SysV hash function overflow
Parents
rG26cc23c84e0e: rtld: fix SysV hash function overflow
Branches
Unknown
Tags
Unknown