Skip to content

fix(ghidra): prevent loader relocations from constant folding - #10483

Merged
N0zoM1z0 merged 5 commits into
mainfrom
fix/4177-ghidra-relocation-volatile
Nov 14, 2026
Merged

N0zoM1z0 merged 5 commits into
mainfrom
fix/4177-ghidra-relocation-volatile

Conversation

@N0zoM1z0

@N0zoM1z0 N0zoM1z0 commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Ghidra pseudocode showed musl stdio streams as NULL. The copy-relocated stderr lives in RELRO, the file stores zero there, and the decompiler folded that on-disk zero into a constant (fprintf(NULL, ...)). The runtime loader supplies the real pointer, so this was wrong pseudocode Evidence.

Fixes #4177

Change and scope

This builds on #4198 by arslanbasharat-o-o, squashed and rebased onto current main with authorship kept. It then narrows the rule after real-Ghidra runs.

  • bridge/ghidra/ReaGhidraBridge.java: before initializing the decompiler, find relocation destinations that Ghidra did not apply (status other than APPLIED/APPLIED_OTHER) in loaded, initialized, non-writable blocks. Mark those blocks volatile in the ephemeral database. Procedures that read such a destination get a limitation naming the relocation. ELF PIEs and shared objects are included; relocatable objects and core files are not. Permissions and source FileBytes are unchanged.

  • Why only unapplied relocations. fix(ghidra): prevent loader relocations from constant folding #4198 protected every relocation destination. Run against real Ghidra, that also marked full-RELRO .got volatile, and ordinary external calls decompiled as (*pcVar2)(param_1) through PTR_rea_external_... instead of rea_external(param_1). The PR's own GOT-stability check failed on that. Ghidra applies GLOB_DAT and RELATIVE relocations, so those bytes already hold the loader's value. A copy relocation, the [Bug] Ghidra pseudocode shows musl stdio streams as NULL: copy-relocated stderr in RELRO is folded to its on-file zero #4177 case, is imported as UNSUPPORTED with the file's zero in place. Whole-block volatility still applies to the block holding an unapplied destination, because Ghidra sets volatility per block.

  • Read-only authority gate. AGENTS.md says Windows Ghidra P0 has no mutation authority. So blocks are marked only in writable sessions (unix-socket transport and not an existing caller project, the bridge's read_only rule). Read-only sessions still add the limitation. The analysis profile versions both cases: volatile-unapplied-relocation-blocks-v1, and read-only-relocation-limitations-v1 on Windows and for existing projects.

  • Real-Ghidra probe (ReaLoaderRelocationProbe.java, npm run verify:ghidra:loader-relocation, Ghidra bridge CI lane). The source-owned PIE fixture reads a provider const object, which GCC and ld copy into .data.rel.ro with an unapplied copy relocation, the shape of [Bug] Ghidra pseudocode shows musl stdio streams as NULL: copy-relocated stderr in RELRO is folded to its on-file zero #4177. The probe checks:

    • the unapplied relocation and the reader's read reference to it;
    • a non-writable, file-backed block, marked volatile after protection;
    • the limitation;
    • unchanged FileBytes and permissions;
    • applied GOT relocations stay non-volatile;
    • a full-RELRO -fno-plt GOT caller decompiles identically before and after.

    Ghidra 12.1.4 already shows this x86-64 read by name without protection, so the probe records baseline_folds_file_constant and compares before and after only when the baseline folded.

  • Verifier plumbing changes made against current main:

    • It uses inspectVerifierGhidraInstallation.
    • Cleanup runs under withVerifierCleanup and keeps the workspace if owned Ghidra cleanup is incomplete.
    • The script is renamed scripts/verify-ghidra-loader-relocation.mjs, which keeps it out of the manual-verifier glob.
  • Docs: docs/providers/ghidra-provider.md and docs/testing-lanes.md.

Contribution scope: existing-provider bug fix. The bridge already owns ephemeral-database edits for writable sessions; target bytes are untouched.

  • Read existing files or captured data.

Evidence and regression coverage

  • Executed locally with real Ghidra 12.1.4 (checksum-verified release, Temurin JDK 21.0.13, GCC 12.2, Linux x64): npm run verify:ghidra:loader-relocation passed with relocation_status: UNSUPPORTED, block: .data.rel.ro, the limitation present, and the GOT caller unchanged (iVar1 = rea_external(param_1); return iVar1 + _rea_external_value;). npm run verify:ghidra:bridge-sources compiled the bridge against the same SDK.
  • The same lane before narrowing failed on full_relro_got_caller_pseudocode_is_stable; protected output is quoted above.
  • Observed for the original rule (not rerun by me): on fix(ghidra): prevent loader relocations from constant folding #4198, Rinkia reported the usign case from [Bug] Ghidra pseudocode shows musl stdio streams as NULL: copy-relocated stderr in RELRO is folded to its on-file zero #4177 no longer folded with real Ghidra 12.1.4 on Linux. That binary is MIPS, where code reaches stderr through a $gp-relative GOT load. No MIPS linker is available here, so the x86-64 lane checks the protection mechanics, not the MIPS folding itself. Real-provider verification of the original MIPS binary under the narrowed rule is outstanding. Its copy relocation is unapplied, so it should still be protected, but I have not run it.
  • TS tests: GhidraProvider.behavior.test.ts (Linux, Windows and existing-project profile semantics) and ciChangeScope.test.ts (the verifier, probe and fixtures select the ghidra_bridge lane).

Validation performed

  • npm run verify:ghidra:loader-relocation (real Ghidra 12.1.4, local): passed.
  • npm run verify:ghidra:bridge-sources: passed.
  • npm run test:local -- src/ghidra/GhidraProvider.behavior.test.ts: 289 passed.
  • npm run test:focused -- tests/boundary/filesystem/ciChangeScope.test.ts: 370 passed.
  • npm run docs:check: passed.
  • npm run check:fast (pre-push): passed.

Compatibility, safety, and release

  • Breaking changes: none. The analysis profile digest changes on purpose, so existing Ghidra snapshots are re-analyzed.
  • Remaining conservative effect: other constants in a block that holds an unapplied relocation destination (for example other .data.rel.ro objects) also stop folding.

Author readiness

Human owner: N0zoM1z0 (PR author, exempt from the declaration per CONTRIBUTING.md).

Supersedes #4198 (arslanbasharat-o-o), squashed here with authorship preserved.

Ghidra imports loader relocation destinations with their file bytes. A
destination in a non-writable loaded block can be populated by the
runtime loader instead, for example a copy-relocated musl `stderr` in
RELRO, so the decompiler folded the on-disk zero and showed
`fprintf(NULL, ...)`.

Before initializing the decompiler, mark each loaded, initialized,
non-writable block containing a relocation destination volatile in the
ephemeral database, and add a limitation to procedures that read such a
destination. ELF shared objects and PIEs are included; relocatable
objects and core files are not. Permissions and source FileBytes are
unchanged.

Sessions without database-mutation authority (Windows P0 and existing
caller projects) record the limitation but do not mark blocks volatile,
and the analysis profile versions the two behaviors separately.

A real Linux Ghidra ELF probe runs in the Ghidra bridge CI lane.

Fixes #4177

Squashed from #4198 and rebased onto main. Rebase adaptations: the
read-only gate, the verifier renamed to verify-ghidra-loader-relocation
so it stays out of the manual-verifier glob, docs moved to
docs/testing-lanes.md and docs/providers/ghidra-provider.md.

Co-Authored-By: gensokyo-dev <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@greptile-apps

greptile-apps Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change affects analysis results within the Ghidra provider workflow.

The read-only profile version must be updated to satisfy the repository’s cache-key requirement before merging.

Findings

  1. P2 Saved warnings stay outdated ▶
Fix with agent prompt
### Issue 1
bridge/ghidra/ReaGhidraBridge.java:1089-1091
This filter changes warnings in read-only sessions, but `readOnlyLoaderRelocationSemantics` stays at `read-only-relocation-limitations-v1`. Saved existing-project results still match the profile and replay warnings for applied relocations that the bridge now excludes.

The repository requires cache keys to cover changes that affect results. Bump the read-only version too and test that older snapshots are refused. This requirement must be satisfied before merging.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

The PR protects non-writable blocks containing unapplied loader relocations before Ghidra opens the decompiler.

  • Ghidra pseudocode no longer folds unapplied loader relocations into constants.

No new security issue was established. Live supersession status could not be checked because GitHub authentication was unavailable; the supplied description says this PR supersedes #4198.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Loader relocation] --> B{Applied?}
  B -->|Yes| C[Leave block foldable]
  B -->|No| D{Loaded, initialized, non-writable?}
  D -->|No| E[Skip]
  D -->|Yes| F[Record relocation warning]
  F --> G{Writable session?}
  G -->|Yes| H[Mark block volatile]
  G -->|No| I[Keep block unchanged]
  H --> J[Open decompiler]
  I --> J
Loading

Reviews (7) · Last reviewed commit: "fix(ghidra): protect only relocations Gh..." · Reviewed by Greptile

Comment thread src/ghidra/GhidraAnalysisProfile.ts Resolved
Comment thread docs/testing-lanes.md Resolved
inspectGhidraInstallation became async on main after #4198's base, so the
verifier asserted on a pending Promise. Also wait for provider output to
close before reading it, like the sibling Ghidra verifiers, and keep the
workflow reference inside the docs site.

Co-Authored-By: gensokyo-dev <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
gensokyo-dev and others added 3 commits November 14, 2026 07:37
- Version the existing-project analysis profile with the read-only
  relocation semantics, since those sessions now add the limitation.
- Inspect the installation through the verifier helper that owns the
  file-reader resources.
- Build the fixture with -fPIC: with -fPIE, GCC 12 emits a copy
  relocation of rea_external_value into writable .bss, so the probe's
  GOT check could not hold.
- Run probe cleanup through withVerifierCleanup so a cleanup failure
  does not hide the probe failure, and keep the workspace when owned
  Ghidra cleanup is incomplete.
- Link the workflow with a full GitHub URL, per the docs guide.

Co-Authored-By: gensokyo-dev <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Building the whole fixture with -fPIC also turned the reader's direct
load of rea_loader_pointer into a GOT-relaxed lea plus two loads, and
the real-Ghidra lane then reported that the baseline no longer folds
the file constant. Keep -fPIE and declare only rea_external_value weak:
GCC and clang must reach a possibly absent symbol through a GOT entry,
which lands as R_X86_64_GLOB_DAT inside GNU_RELRO (checked with readelf
for GCC 12.2 and clang/lld).

Co-Authored-By: gensokyo-dev <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Running the real-Ghidra lane locally (Ghidra 12.1.4, JDK 21, GCC 12)
showed that marking every non-writable block that holds a relocation
volatile also marks full-RELRO .got. External calls then decompile as
opaque pointer calls (`(*pcVar2)(param_1)` through
`PTR_rea_external_...` instead of `rea_external(param_1)`), which the
probe's GOT-stability check caught.

Ghidra applies GLOB_DAT and RELATIVE relocations, so those bytes already
hold the loader's value and folding them is correct. The #4177 case is
a copy relocation, which Ghidra imports as UNSUPPORTED with the file's
zero in place. Protect only destinations whose relocation status is not
APPLIED or APPLIED_OTHER.

The fixture now reads a provider `const` object that the linker copies
into .data.rel.ro, the #4177 shape. The probe requires that relocation
to be unapplied and requires applied GOT relocations to stay
non-volatile. Ghidra 12.1.4 already shows this x86-64 read by name
without protection, so the probe records whether the baseline folds and
compares before and after only when it does. The profile value becomes
volatile-unapplied-relocation-blocks-v1.

Co-Authored-By: gensokyo-dev <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>

@gensokyo-dev gensokyo-dev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the final diff, the real-Ghidra lane result and automated review; approving.

@N0zoM1z0
N0zoM1z0 merged commit 8e2853d into main Nov 14, 2026
54 of 56 checks passed
@N0zoM1z0
N0zoM1z0 deleted the fix/4177-ghidra-relocation-volatile branch November 14, 2026 08:32
Comment on lines +1089 to +1091
Relocation.Status status = relocation.getStatus();
if (status == Relocation.Status.APPLIED || status == Relocation.Status.APPLIED_OTHER)
continue;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Saved warnings stay outdated

This filter changes warnings in read-only sessions, but readOnlyLoaderRelocationSemantics stays at read-only-relocation-limitations-v1. Saved existing-project results still match the profile and replay warnings for applied relocations that the bridge now excludes.

The repository requires cache keys to cover changes that affect results. Bump the read-only version too and test that older snapshots are refused. This requirement must be satisfied before merging.

Context Used: AGENTS.md (source)

Knowledge Base Used: Ghidra integration

Prompt To Fix With AI
This is a comment left during a code review.
Path: bridge/ghidra/ReaGhidraBridge.java
Line: 1089-1091

Comment:
**Saved warnings stay outdated**

This filter changes warnings in read-only sessions, but `readOnlyLoaderRelocationSemantics` stays at `read-only-relocation-limitations-v1`. Saved existing-project results still match the profile and replay warnings for applied relocations that the bridge now excludes.

The repository requires cache keys to cover changes that affect results. Bump the read-only version too and test that older snapshots are refused. This requirement must be satisfied before merging.

**Context Used:** AGENTS.md ([source](https://github.com/morluto/rea/blob/main/AGENTS.md))

**Knowledge Base Used:** [Ghidra integration](https://app.greptile.com/-/switch-tenant?tenant=pre&next=%2Fcustom-context%2Fknowledge-base%2Fmorluto%2Frea%2F-%2Fdocs%2Fghidra-integration.md)

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

morluto added a commit that referenced this pull request Nov 19, 2026
…r whole block (#11996)

Since #10483, an unapplied loader relocation in a non-writable block made
that whole block volatile. TLS slots are such relocations and sit in .got
beside the entries Ghidra did apply, so in any shared object or PIE with
initial-exec or dynamic TLS every GOT-indirect call decompiled as pointer
arithmetic instead of a named call.

Mark each unapplied destination volatile through its data item's Mutability
setting, creating an undefined item of the relocation's length where nothing
is defined. Fall back to the whole block only when an instruction or a data
item starting before the destination occupies it. Eligibility, limitations,
the read-only path, permissions and source FileBytes are unchanged.

The loader-relocation fixture now reads an initial-exec TLS variable whose
R_X86_64_TPOFF64 slot shares .got with the applied entries. The probe
requires that block and its applied entries to stay non-volatile and both
unapplied destinations to have volatile data items; on the previous bridge
it fails at applied_got_relocations_not_volatile.

Rename loader_relocation_semantics to
volatile-unapplied-relocation-destinations to state the new behavior.

Co-authored-by: morluto <[email protected]>
@morluto morluto added bug Something isn't working provider: ghidra Analysis provider / engine: ghidra labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working provider: ghidra Analysis provider / engine: ghidra

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Ghidra pseudocode shows musl stdio streams as NULL: copy-relocated stderr in RELRO is folded to its on-file zero

5 participants