[PATCH v3 0/8] bfd: harden in-place relaxation byte deletion against size underflow
Harshit Kumar
harshit7kr@gmail.com
Sat Nov 7 07:50:55 GMT 2026
This series hardens 18 ELF backends in BFD against buffer underflow and
heap corruption during linker relaxation byte deletion.
This v3 series supersedes v2:
https://inbox.sourceware.org/binutils/20261016123644.1429854-1-harshit7kr@gmail.com/
and addresses review feedback from Jan Beulich on patches 1/8, 2/8, and 6/8.
Problem Description
===================
Linker relaxation backends that shrink code sections slide remaining
section bytes downward over deleted space via memmove:
memmove (contents + addr, contents + addr + count, toaddr - addr - count);
When input relocations or section headers specify an out-of-range offset
or a deletion count exceeding remaining section size, the unsigned
calculation (toaddr - addr - count) wraps around near SIZE_MAX, leading
to out-of-bounds heap corruption or glibc aborts.
Scope of Series and Follow-up Work
==================================
This series hardens all 18 ELF backends that perform in-place memmove
deletion: AVR, CR16, CRX, FT32, H8/300, IP2K, M10200, M10300, M32C,
M68HC11, MIPS, MSP430, PRU, RISC-V, RL78, RX, SH, and V850.
All 56 call sites across these 18 backends were audited: 100% of call
sites either already check return status or are updated in this series
(AVR, M68HC11, RISC-V, RL78, RX) to propagate failure cleanly.
Pre-deletion opcode bounds issues (RL78/RX), COFF targets (coff-sh.c),
and deferred playback underflows (MicroBlaze) will be tracked in
follow-up PRs.
Testing Summary
===============
- DejaGnu relaxation tests across 5 targets (CRX, H8/300, RISC-V, RL78,
RX): 11/11 tests pass. Malformed underflow cases trigger glibc abort
or SIGSEGV on unpatched ld, and are cleanly rejected with exit status 1
on patched ld (clean under ASan).
- Cross-target regression testing (gas + ld built for --target=<t>):
avr-elf (51 pass / 1 fail), crx-elf (16/0), h8300-elf (8/0),
m68hc11-elf (14/0), mips-elf (278/0), mn10300-elf (10/0), msp430-elf (55/0),
pru-elf (16/0), riscv64-elf (201 baseline; 203 with 2 new tests),
sh-elf (8/1), and v850-elf (1/0).
Results identical to stock apart from the new tests; real RV32/RV64
links with --relax produce byte-for-byte identical output.
Verified section shrinkage across AVR, SH, MN10300, and V850 matches
stock byte-for-byte.
- x86_64 ld suite (--enable-targets=all): 3367 passes, 59 expected
failures, 1 untested, 38 unsupported, 0 unexpected failures (+11 passes
from target tests).
Changes in v3
=============
- Patch 1/8:
* Renamed parameters to 'offs' and 'limit', making limit clearly an
upper bound, and made abfd and sec pointer-to-const.
* Kept the limit <= sec->size check (RX alignment relocs can place
limit past sec->size on crafted input).
* Diagnostic reworded to log offs, count, limit, and section size.
- Patch 2/8:
* Dropped redundant (size_t) cast in elf32-v850.c.
* Added shared ld_relax_underflow_test in ld-lib.exp taking args for
clean single-test and multi-test invocation.
* Updated CRX and H8/300 tests to use ld_relax_underflow_test.
- Patches 3/8, 4/8, 6/8: Unchanged from v2.
- Patches 5/8, 7/8:
* Documented intentional goto error_return in SNIP/SNIPNR macros.
* Converted RX and RL78 tests to use ld_relax_underflow_test.
- Patch 8/8:
* Converted RISC-V test to use ld_relax_underflow_test.
- Series rebased onto current master
(1b10c36dfd459a485c7744f8d26670b8369585a0).
Changes in v2
=============
- Split monolithic patch into 8 subsystem patches; separated RL78
error_return fix into prerequisite patch (6/8).
- Added DejaGnu tests (expanding to 5 targets: CRX, H8/300, RISC-V,
RL78, RX) and shared helpers in ld-lib.exp.
- Dropped redundant delete_total wrap guard in RISC-V.
Base commit: 1b10c36dfd459a485c7744f8d26670b8369585a0
Harshit Kumar (8):
bfd: add _bfd_elf_relax_delete_range_ok
bfd: check relaxation delete ranges in simple ELF backends
avr: check relaxation delete range and propagate failure
m68hc11: return bool from relax delete helpers
rx: check relaxation delete range, propagate failures
rl78: return false from relax_section error path
rl78: check relaxation delete range, propagate failures
riscv: check relaxation delete range, propagate failures
bfd/elf-bfd.h | 2 +
bfd/elf-m10200.c | 3 +
bfd/elf-m10300.c | 3 +
bfd/elf.c | 30 +++++
bfd/elf32-avr.c | 9 +-
bfd/elf32-cr16.c | 3 +
bfd/elf32-crx.c | 3 +
bfd/elf32-ft32.c | 3 +
bfd/elf32-h8300.c | 3 +
bfd/elf32-ip2k.c | 3 +
bfd/elf32-m32c.c | 3 +
bfd/elf32-m68hc11.c | 52 +++++----
bfd/elf32-msp430.c | 3 +
bfd/elf32-pru.c | 3 +
bfd/elf32-rl78.c | 33 ++++--
bfd/elf32-rx.c | 38 +++++--
bfd/elf32-sh.c | 3 +
bfd/elf32-v850.c | 4 +
bfd/elfnn-riscv.c | 23 ++--
bfd/elfxx-mips.c | 3 +
ld/testsuite/ld-crx/crx-relax-underflow.exp | 67 +++++++++++
.../ld-h8300/h8300-relax-underflow.exp | 93 ++++++++++++++++
.../ld-riscv-elf/riscv-relax-underflow.exp | 69 ++++++++++++
ld/testsuite/ld-rl78/rl78-relax-underflow.exp | 83 ++++++++++++++
ld/testsuite/ld-rx/rx-relax-underflow.exp | 71 ++++++++++++
ld/testsuite/lib/ld-lib.exp | 105 ++++++++++++++++++
26 files changed, 670 insertions(+), 45 deletions(-)
create mode 100644 ld/testsuite/ld-crx/crx-relax-underflow.exp
create mode 100644 ld/testsuite/ld-h8300/h8300-relax-underflow.exp
create mode 100644 ld/testsuite/ld-riscv-elf/riscv-relax-underflow.exp
create mode 100644 ld/testsuite/ld-rl78/rl78-relax-underflow.exp
create mode 100644 ld/testsuite/ld-rx/rx-relax-underflow.exp
--
2.55.0
More information about the Binutils
mailing list