Skip to content

fix(elf): keep copy relocations of read-only symbols inside RELRO - #155

Merged
zackees merged 2 commits into
mainfrom
fix/123-copyrel-relro
Sep 17, 2026
Merged

zackees merged 2 commits into
mainfrom
fix/123-copyrel-relro

Conversation

@zackees

@zackees zackees commented Sep 17, 2026

Copy link
Copy Markdown
Collaborator

Refs #123 (Phase 2, the copyrel_readonly_is_relro checklist line). Part of the #153 burn-down.

The bug

A copy relocation of a symbol that the shared object kept read-only was allocated in .bss, so a const imported by a non-PIE executable stayed writable for the life of the process. Both reference linkers keep it inside PT_GNU_RELRO so the loader can re-protect it after relocation.

Reproduced with a DSO exporting const int ro_value and int rw_value, referenced from a -no-pie main:

Linker ro_value lands in Inside PT_GNU_RELRO
ld.bfd 2.46 .data.rel.ro yes
ld.lld 21.1.8 .bss.rel.ro yes
reld, before .bss no
reld, after .data.rel.ro yes

It is a silent hardening regression, not a link failure, which is why nothing caught it.

The fix

reld uses .data.rel.ro, matching GNU ld. Writable symbols keep their .bss home.

Four places picked the destination independently: the size-reservation pass, the address-assignment pass, and both the dynsym and symtab writers. They now share one copy_relocation_section helper. That matters — fixing only the first two produced an output whose symbol address was in .data.rel.ro while its st_shndx still named .bss, and the new fixture is what caught that inconsistency.

Evidence

  • New acceptance fixture elf/copy-relocation-relro asserts the section of both the read-only and writable symbol, and executes the linked binary so the copy relocations are proven to still work at runtime.
  • Full acceptance suite: the failure set is identical to main. Both runs fail the same 11 tests, all environmental on this host (no static libm/libc, missing libstdc++.so.6); my branch adds +1 pass and +3 arch-ignored, which is exactly the new fixture.
  • 12-seed reld-difftest against ld.bfd: 0 differential failures.
  • cargo fmt --check, clippy --workspace --all-targets -D warnings, reld-core lib tests (209), acceptance-policy, external-runner, and the host-platform Dylint are all green.

🤖 Generated with Claude Code

zackees and others added 2 commits September 17, 2026 13:37
…ld#123)

A copy relocation of a symbol that the shared object kept read-only was
allocated in `.bss`, so a `const` imported by a non-PIE executable stayed
writable for the life of the process. Both reference linkers put it inside
PT_GNU_RELRO instead: GNU ld uses `.data.rel.ro`, lld uses `.bss.rel.ro`.
reld now uses `.data.rel.ro`, matching GNU ld.

Reproduced with a DSO exporting `const int ro_value` and `int rw_value`
referenced from a `-no-pie` main: ld.bfd placed ro_value in `.data.rel.ro`,
ld.lld in `.bss.rel.ro`, reld in `.bss`.

Four sites chose the destination independently — the size-reservation pass,
the address-assignment pass, and both symbol-table writers — so they now
share `copy_relocation_section`. Fixing only the first two produced an
output whose symbol address was in `.data.rel.ro` while its `st_shndx`
still named `.bss`, which is how the new acceptance fixture caught it.

Writable symbols keep their `.bss` home, so `rw_value`-style imports are
unchanged.

Evidence: new `elf/copy-relocation-relro` acceptance fixture asserts the
section of both symbols and executes the result. Acceptance failure set is
byte-identical to main (11 pre-existing environmental failures, +1 pass and
+3 arch-ignored from this fixture); 12-seed differential run against ld.bfd
is clean.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The RELRO copy-relocation fix makes mold's own copyrel-relro.sh pass, and
the ratchet correctly failed the build with "is in skip list but now
passes". That is independent third-party confirmation of the fix.

The other four copyrel entries still fail, per the same CI run, so they
stay; their shared comment no longer described reality, since read-only
copies now land in `.data.rel.ro` and only writable ones stay in `.bss`.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@zackees

zackees commented Sep 17, 2026

Copy link
Copy Markdown
Collaborator Author

CI caught something my local run could not: the mold corpus ratchet failed with copyrel-relro.sh is in skip list but now passes.

That is independent third-party confirmation of the fix — mold's own RELRO copy-relocation test now passes against reld. Removed that entry from mold_skip_tests.toml, which also burns down one line of #14.

The same CI run shows the other four copyrel-* entries still failing, so they stay; I corrected their shared comment, which claimed "We put copy relocation info to .bss" and is no longer true for read-only symbols.

Acceptance in CI: 234 passed, 0 failed.

@zackees
zackees merged commit a20a9e9 into main Sep 17, 2026
18 checks passed
@zackees
zackees deleted the fix/123-copyrel-relro branch September 17, 2026 20:53
zackees added a commit that referenced this pull request Sep 17, 2026
…3) (#160)

A caller-supplied ignore is ratcheted per fixture: the fixture that declares it
must still observe it. The ~90 inherited `--reld-defaults` patterns had no such
check, because each one suppresses differences across every fixture and no
single fixture can say whether it is still needed. Nothing removed them, so a
default outlived the divergence it described — `.data.rel.ro` is listed as a
section reld does not support, which stopped being true in #155.

The run now takes the union instead. Every report contributes the defaults it
declared and the ignores it observed, and at the end of a full acceptance run
the harness prints the defaults no fixture observed. Those are removal
candidates for reld#13, which is how the inherited list shrinks.

It prints rather than fails, because which defaults get exercised depends on
which fixtures the host can run. `RELD_RATCHET_DEFAULT_IGNORES=1` turns the
report into a failure, for a run that covers the matrix.

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant