Repository navigation
fix(ghidra): prevent loader relocations from constant folding - #10483
Conversation
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]>
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
[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
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.SummaryThe PR protects non-writable blocks containing unapplied loader relocations before Ghidra opens the decompiler.
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
Reviews (7) · Last reviewed commit: "fix(ghidra): protect only relocations Gh..." · Reviewed by Greptile |
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]>
- 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
left a comment
There was a problem hiding this comment.
Reviewed the final diff, the real-Ghidra lane result and automated review; approving.
| Relocation.Status status = relocation.getStatus(); | ||
| if (status == Relocation.Status.APPLIED || status == Relocation.Status.APPLIED_OTHER) | ||
| continue; |
There was a problem hiding this comment.
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!
…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]>
Summary
Ghidra pseudocode showed musl stdio streams as NULL. The copy-relocated
stderrlives 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 thanAPPLIED/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 sourceFileBytesare 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
.gotvolatile, and ordinary external calls decompiled as(*pcVar2)(param_1)throughPTR_rea_external_...instead ofrea_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-relocatedstderrin RELRO is folded to its on-file zero #4177 case, is imported asUNSUPPORTEDwith 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_onlyrule). Read-only sessions still add the limitation. The analysis profile versions both cases:volatile-unapplied-relocation-blocks-v1, andread-only-relocation-limitations-v1on 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 providerconstobject, which GCC and ld copy into.data.rel.rowith an unapplied copy relocation, the shape of [Bug] Ghidra pseudocode shows musl stdio streams as NULL: copy-relocatedstderrin RELRO is folded to its on-file zero #4177. The probe checks:FileBytesand permissions;-fno-pltGOT 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_constantand compares before and after only when the baseline folded.Verifier plumbing changes made against current main:
inspectVerifierGhidraInstallation.withVerifierCleanupand keeps the workspace if owned Ghidra cleanup is incomplete.scripts/verify-ghidra-loader-relocation.mjs, which keeps it out of the manual-verifier glob.Docs:
docs/providers/ghidra-provider.mdanddocs/testing-lanes.md.Contribution scope: existing-provider bug fix. The bridge already owns ephemeral-database edits for writable sessions; target bytes are untouched.
Evidence and regression coverage
npm run verify:ghidra:loader-relocationpassed withrelocation_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-sourcescompiled the bridge against the same SDK.full_relro_got_caller_pseudocode_is_stable; protected output is quoted above.usigncase from [Bug] Ghidra pseudocode shows musl stdio streams as NULL: copy-relocatedstderrin 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 reachesstderrthrough 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.GhidraProvider.behavior.test.ts(Linux, Windows and existing-project profile semantics) andciChangeScope.test.ts(the verifier, probe and fixtures select theghidra_bridgelane).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
.data.rel.roobjects) 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.