Repository navigation
fix(ghidra): prevent loader relocations from constant folding - #4198
arslanbasharat-o-o wants to merge 13 commits into
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
[Medium impact] The reviewed changes appear safe to merge; the previous CI selection issue is fixed. SummaryPrevents Ghidra from treating loader-filled values in non-writable memory as known constants.
No new actionable issues were found in the changes since the previous review. Native Ghidra execution was not verified during this review. Whether another PR supersedes this one could not be checked because GitHub access was unavailable. Diagram%%{init: {'theme': 'neutral'}}%%
flowchart LR
A[Import target] --> B[Find eligible relocation destinations]
B --> C[Mark containing blocks volatile]
C --> D[Initialize decompiler]
D --> E[Return pseudocode and read-specific uncertainty]
Reviews (10) · Last reviewed commit: "test(ci): select loader relocation Ghidr..." · Reviewed by Greptile |
|
Ran this on Apple Silicon (macOS, Ghidra 12.1.4 with the native decompiler built), which should cover the decompiler gap mentioned above.
One concern with that decompiler check, though. The pseudocode it produced is I couldn't test ELF here; I can't link a normal ELF program on macOS. So whether whole-block volatility affects GOT calls in RELRO on ELF is still open. Separately, the Dockerfile / Makefile / README container workflow looks unrelated to #4177. Might be easier to review as its own PR. |
|
@morluto @RishTheFish21 Thanks for catching the flaw in the Mach-O probe. I pushed |
|
@morluto I pushed The new fork workflows currently report The earlier run also failed in browser cancellation and packet/signal cleanup tests, outside this PR's changed files. Please review those remaining CI results before considering a merge into |
There was a problem hiding this comment.
TL;DR: Main still lacks this fix, and the Java change reads sensibly. No GitHub checks have run on this head, and the author says the new Linux Ghidra check has not been run. The PR also carries unrelated container files, and the body does not follow the template.
Edit (24 Oct): I re-read the new commits on 5ad9987. The follow-up is here.
Edit (22 Oct): I was wrong about line 930. It skips PIE files and shared objects too, not just object files. So the protection never runs on the PR's own fixture. See the follow-up.
Findings
Should fix
- No workflow has run on this head. The
CI,PR titleand three "Real ..." runs all sayaction_required, so a maintainer has to approve them. The author asked for that on 19 Oct. OnlyPR readinessand Greptile report, and both pass. The last head that did run (0f3bbfb) failedDocs Generation,Source tests,Test shard (3/5)andPortable tests. The log showsdocumentationFacts.test.tsfailing. The test name is"keeps translated readmes linked to canonical setup and tool details".Portable testsalso failsPlaywrightBrowserScenarioProvider.test.ts("cancels an active action and still closes the session"). The second file is not in this PR, and I did not check whether main fails it too. The head commit 8ecee31 changes the README link to fix the first one, but that is unproven until CI runs. I did not read theSource testslog or thetsharkandsigrokfailures. - The new regression has never run on Linux. The author says here that the macOS host could not run the ELF lane. So two claims are still unproven. The baseline pseudocode folds
0x11111111. The protected pseudocode does not. RishTheFish21 asked one more thing. Does marking a whole block volatile hide GOT calls in RELRO on ELF? Nobody has answered that. Someone with Linux and Ghidra 12.1.4 should runnpm run verify:ghidra:loader-relocation. A before and after pseudocode of a GOT-indirect call in the same block would settle the second question. This part is unverified. - The body does not keep the template as written. All eight "Expected effects" boxes are ticked. That includes "Documentation or usability only" and "Not sure yet". The body itself says no target process is launched or attached. The three "For evidence-bearing changes" boxes are gone. The "Related issue is linked" line lost its ending about the reason for not linking one. "Tests added or updated" names only the profile string test. But the PR now also adds a real-Ghidra script, a Java probe and a C fixture. The "Validation performed" list (540 test files) reads as one state. The "Follow-up validation" section reads as a different state. Please bring the body up to date.
- The PR carries a Dockerfile, a Makefile and a
.dockerignore. It adds a README section too, titled"Reproducible development". And it adds amake deploytarget that pushes images. None of that touches #4177. RishTheFish21 raised this on 15 Oct. The author answered that a Buildathon submission needs them. The author also said commits 9670a68 and d48fc9d can be split out. The head still has them, and the "one focused outcome" box is ticked. I would split them into their own PR so the Ghidra fix can be reviewed alone. This is the maintainers' call.
Nit
- The new note says "This procedure reads ...". But the loop at lines 3538-3543 adds it for any reference from the instruction. It has no
isRead()check. The probe's ownreaderReferencedoes checkisRead(). A write or an address-of reference would also say "reads". - The lookup in that loop matches only the relocation's first address. A reference to a later byte of the same relocated object would not add the note. The block is volatile either way, so the pseudocode stays protected. I did not check how Ghidra reports such references, so this part is unverified.
Looks good
- Main does not have the fix. A search of main for
setVolatile,loader_relocationandprotectLoaderInitializedRelocationsfinds nothing, and #4177 is still open. protectLoaderInitializedRelocations()runs at line 239, right beforeinitializeDecompiler(). It skips relocatable object files (line 930). It only touches loaded blocks. They must be initialized and non-writable. It leaves permissions and source bytes alone. The Javadoc says plainly that volatility is per block and conservative.- Old snapshots cannot replay. The new
loader_relocation_semanticskey sits in the profile parameters.analysisProfilesEqualcompares the profile digest. The digest covers those parameters. I read this in the code and did not run it. - The three Greptile threads are resolved and fixed on this head. They were about a mocked test, a root user in the Dockerfile, and missing
.dockerignoreentries. The fixes areUSER node, the website and results excludes, and a real ELF probe. The probe now needs three things. They are an importer-made relocation, a real read reference to it and a non-writable loaded block. That is what RishTheFish21 asked for. - The Dockerfile's
node:24.18.0matches.nvmrc(24.18.0). - It merges cleanly with main (bea6b98). I extracted base, main and head for the seven files that both sides touch and ran a three-way merge on each. There were no conflicts. The files are
ReaGhidraBridge.java,GhidraAnalysisProfile.ts,GhidraProvider.behavior.test.ts,package.json,README.md,docs/testing.mdandtests/conformance/ghidra/README.md.
Not run
- No build and no tests. This is a read-only review of the diff against main.
- The Ghidra lane (
verify:ghidra:loader-relocation), the Docker build and the Java compile were not run. - I did not read the
Source tests,tsharkorsigrokfailure logs from the earlier head.
Thanks, arslanbasharat-o-o, for the fix, and for swapping the mocked test for a real ELF check after review. Thanks, RishTheFish21, for running the first version on macOS.
Test setup
🤖 Written with Claude Code (Sonnet 5.5, verified by Opus 5.5), posted with my OK 🦊
|
I tried to answer my own RELRO/GOT question on macOS by cross-linking Linux ELFs with clang and lld (no Linux host needed, since the fixture is Repro (macOS arm64, Ghidra 12.1.4, Homebrew LLD 23.1.4, this PR checked out and built with The layout is the one the lane expects. No "non-writable block" limitation is emitted. Likely cause: the first line of if (currentProgram.getRelocationTable().isRelocatable()) return;I think this is meant to skip relocatable object files. But On my earlier question (GOT calls in RELRO): this still needs answering once the early return is fixed. Ghidra applies relocations first and then marks the whole Build it |
|
TL;DR: I read Ghidra 12.1.4's source and this PR's code. All of it agrees with RishTheFish21. The early return skips PIE files and shared objects. So the protection does not run for the PR's own PIE fixture. I ran nothing, and my first review was wrong to say that line only skips object files. Thanks, RishTheFish21, for the repro and for naming the exact line. What the source says
What the PR's own files say
Not run
🤖 Written with Claude Code (Sonnet 5.5, verified by Opus 5.5), posted with my OK 🦊 |
|
Want your agent to iterate on Greptile's feedback? Try greploops. |
|
TL;DR: I read the new commits on 5ad9987, and most of my findings are now fixed in the code. The check now lets PIE and shared-object files through, and the container files are gone. GitHub has still run no workflow on this head, so the new Linux Ghidra check has no result yet.
All 7 review threads are resolved. A three-way merge of the 9 files that both this PR and main (d24ad4f) changed has no conflicts. Not run
Thanks, arslanbasharat-o-o, for the quick fixes, and for the move of the container files to a PR of their own. Thanks, RishTheFish21, for the GOT question. What I read
Test setup🤖 Written with Claude Code (Sonnet 5.5, verified by Opus 5.5), posted with my OK 🦊 |
|
TL;DR: Fixes the Build: this PR's head Target:
So the copy-relocated |
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]>
* fix(ghidra): prevent loader relocations from constant folding 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]> * fix(ghidra): await installation inspection in the relocation verifier 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]> * fix(ghidra): address relocation review findings - 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]> * test(ghidra): reach the external data through the GOT with a weak extern 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]> * fix(ghidra): protect only relocations Ghidra could not apply 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]> --------- Co-authored-by: Arslan Basharat <[email protected]> Co-authored-by: gensokyo-dev <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
|
Thanks, arslanbasharat-o-o. This landed in #10483, squashed with your authorship kept and rebased onto main. Running the real-Ghidra lane showed that protecting every relocation also made full-RELRO |
Summary
Fixes #4177.
Ghidra now treats loader-initialized relocation destinations in non-writable loaded memory as uncertain during decompilation. This prevents RELRO copy-relocation values such as
stderrfrom being presented as trustworthy file-backed constants.Problem and expected behavior
OpenWrt's non-PIE MIPS
usigncontains a copy-relocatedstderrin RELRO. The file stores zero at the destination while the runtime loader supplies the stream pointer. Ghidra could constant-propagate the on-disk zero, makingfprintf(stderr, ...)appear to write through a null stream.A loader-populated destination must not be presented as a known runtime constant. Evidence reports uncertainty only when an instruction reads an address covered by a loader relocation.
Change and scope
FileBytes.ET_DYNPIEs and shared objects even though Ghidra also marks their relocation table relocatable. Keep relocatable object files and core files outside this mitigation.ET_DYNcase, read/write reference handling, the affected-byte range, and baseline/protected pseudocode. A full-RELRO-fno-pltcaller checks that external GOT calls retain the same pseudocode after relocation blocks become volatile.Contract and boundary impact
ET_DYNand executable relocations are eligible;ET_RELobject files and other Ghidra programs still marked relocatable are skipped.FileBytesremain unchanged.Expected effects:
Evidence and regression coverage
ET_DYNrelocation tables relocatable. The probe now requires an ELF shared-object file type and a relocatable table, then verifies the production bridge makes the destination volatile.CIandPR titleareaction_requiredand need maintainer approval; Greptile passed on the latest head after the test-path selection fix.For evidence-bearing changes:
Validation performed
npm ciwith Node 24.22.1 and npm 11.16.0 — passed.npm run check:fast— passed.npm run format:check— passed.vitest run src/ghidra/GhidraProvider.behavior.test.ts -t "commits exact provider, isolation, and resource semantics"— 1 passed.npm run test:local -- tests/conformance/readmeConsistency.test.ts— 9 passed.npm run test:focused -- tests/boundary/filesystem/ciChangeScope.test.ts— 279 passed, including verifier/fixture lane selection.npm run verify:test-discovery— 2,178 test files discovered exactly once.npm run build:cached— passed.node --check scripts/verify-real-ghidra-loader-relocation.mjsandclang -fsyntax-only -Wall -Wextra tests/conformance/ghidra/loader-relocation.c tests/conformance/ghidra/loader-relocation-provider.c— passed.git diff --check— passed.npm run verify:ghidra:bridge-sources— unavailable becauseGHIDRA_INSTALL_DIRis not set; the host has no Ghidra installation.Compatibility, safety, and release
Author readiness
Human owner: @arslanbasharat-o-o
Before merge
type(scope): outcome.