binutils Newspaper
NOVEMBER 30, 2026
risc-v — Proposed

RISC-V: reject relocation offsets outside the section

Adds checks to reject RISC-V relocations targeting memory outside the section bounds, preventing memory corruption and crashes.

This patch introduces riscv_reloc_field_in_range to validate relocation field offsets against the section’s limits before applying them in riscv_elf_relocate_section. This prevents operations like bfd_putl32 from writing past the end of the section, which could lead to heap-buffer-overflows, as seen with R_RISCV_TLSDESC_CALL. It also correctly skips R_RISCV_NONE relocations and ensures that relocations used during relaxation are also checked.

In the Thread 1 participant
  1. Harshit Kumar <harshit7kr@gmail.com> proposer

    Implements checks in `riscv_elf_relocate_section` to ensure all relocation fields, including those for `R_RISCV_TLSDESC_CALL`, are within the section bounds, using a new helper function `riscv_reloc_field_in_range`.

    “riscv_elf_relocate_section applies relocations without checking that the relocated field lies inside the section, so a relocation with a bad r_offset reads and writes past the section contents. Add riscv_reloc_field_in_range and check each relocation field against bfd_get_section_limit_octets before the relocation is applied.”

In Details

This patch addresses a critical safety issue in RISC-V relocation handling within bfd/elfnn-riscv.c. It introduces riscv_reloc_field_in_range to perform explicit bounds checking on relocation targets, preventing writes or reads beyond the section's boundaries. This is particularly important for relocations like R_RISCV_TLSDESC_CALL which, despite having a zero HOWTO size, involve instruction rewriting that can extend beyond the section end if not checked. The patch also correctly handles R_RISCV_NONE and ensures checks are applied even during relaxation, improving overall linker robus…

For Context
relocation
The process of updating symbolic references in object code to their final memory addresses after linking.
R_RISCV_TLSDESC_CALL
A RISC-V relocation type specifically for TLS (Thread-Local Storage) descriptor calls, which may involve instruction rewriting.
bfd_putl32
A BFD library function to write a 32-bit value to memory, typically used for patching instruction fields.
AddressSanitizer
A fast memory error detector for C/C++ that finds use-after-free, buffer overflows, and other memory bugs.
HOWTO size
In the context of BFD relocations, this refers to the size of the field being modified by the relocation, as defined in the relocation type's HOWTO structure.
Filed Under: risc-vrelocationbounds checkingsecuritylinker