Skip to content

fix(pe)!: bound static readers by the bytes they read, not file size - #14262

Merged
morluto merged 16 commits into
mainfrom
fix/pe-bytes-read-admission
Nov 24, 2026
Merged

morluto merged 16 commits into
mainfrom
fix/pe-bytes-read-admission

Conversation

@morluto

@morluto morluto commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

#14253 has merged and this PR now targets main. #14263 (the image_base and iat_va address vocabulary, reviewed there) was squash-merged into this branch, so this PR also carries that change. Its description, validation and threads are on #14263.

Root cause

inspect-pe-imports, -resources, -load-config, -certificates, inspect-binary-hardening and inspect-native-exports passed max_file_bytes to withStableArtifactHandle as a whole-file size gate. These readers already read by position, through a 4096-byte header prefix and readRange. The only stage that touches every byte is the identity pass, which streams the file once in fixed chunks to compute its SHA-256 and retains only the prefix. The gate therefore tracked no resource the readers use. A 181 MB PE was refused by default, even though its tables are a few hundred KiB.

Fix

  • withStableArtifactHandle gains an optional readBudget (ArtifactReadBudget, from src/domain/artifactReadBudget.ts). budgetArtifactReads (src/resources/artifactReadBudget.ts) counts the parsed header prefix and every positional read against it, and refuses before the read that would exceed it. The six readers pass max_file_bytes as that allowance. The parameter keeps its name, default (64 MiB, or 256 MiB for exports), floor and maximum. Its description now says it bounds the bytes read, not the file size.
  • The identity pass has a fixed whole-file ceiling. It is 4 GiB minus one byte for the four PE-only readers, because PE file offsets are 32-bit and artifact.bytes is a u32. It is 512 MiB, the existing schema ceiling, for hardening and exports, which also read ELF and Mach-O. A file above it gets a file-size refusal naming that fixed allowance (artifactFileCeilingConstraint).
  • An exhausted allowance gets a capacity refusal (artifactReadBudgetConstraint). It carries a typed details.limit (control_kind: "parameter", parameter: "max_file_bytes", effective value, bounds, default and applies_at: "next_call"), and reported_limits.required_read_bytes holds the reads needed so far. The remediation says to raise the parameter, or names the ceiling when that is not possible.
  • The services no longer reject a report whose artifact.bytes exceeds max_file_bytes.
  • Refusal sites changed only where the fix requires it. These refusals reuse the existing ArtifactReaderFailure("limit") / AnalysisResourceConstraintError, and [Design] One owned target-failure model with derived CLI/MCP projections and a declarative reader conformance registry #14239 migrates them to TargetFailure's resource kind. The resource-constraint constructor baseline nets 3 removed and 2 shared added, in src/resources/artifactReadBudget.ts.
  • Docs: a new "Static reader read allowance" section in docs/reference/limits-and-recovery.md, linked from the tool pages. rea-numeric-limits stays green. The limit inventory has no entries for these per-call parameters.

Breaking behaviour: max_file_bytes no longer refuses a large file whose reads fit. An exhausted allowance now reports capacity instead of file-size. Only a file above the fixed ceiling reports file-size.

Large public PE

The test file is Godot_v4.7.2-stable_win64.exe (180,858,888 bytes, SHA-256 ab1824f8…2424, from the official release zip). It was not executed. Each run was in-process, from source through tsx, with default inputs. Peak RSS is from /usr/bin/time -l and includes the tsx loader.

Reader Before (main) After Positional reads Peak RSS
imports refused, file-size 64 MiB ok, 175 ms 557,056 B 135 MB
exports ok (256 MiB gate) ok, 124 ms 4,166 B 133 MB
load config refused ok, 109 ms 560 B 132 MB
resources refused ok, 111 ms 144,711 B 132 MB
certificates refused ok, 107 ms 8,712 B 133 MB
hardening refused ok, 107 ms 592 B 137 MB

Read counts come from a temporary counter that was removed before commit. Each count excludes the 4096-byte prefix, which also counts toward the allowance. These are single cold runs, not benchmarks.

corkami/pocs PE/bin (230 MZ files) gives the same per-reader counts as #14253, because every file fits the default allowance. imports 206/24, exports 202/28, load config 204/26, resources 203/27, certificates 204/26, hardening 207/23 (ok/refused).

Validation

  • src/resources/artifactReadBudget.test.ts covers prefix and positional accounting, the refusal before the over-budget read, the typed details.limit, and that other reader limits are left alone.
  • PeResourcesProvider.test.ts covers a sparse file 4 KiB over 64 MiB with small directories, which is accepted at the default allowance. It also covers the typed refusal at max_file_bytes: 64.
  • The boundary budget tests for imports, load config, hardening and exports still refuse at 64 bytes.
  • Mutation checks: not counting reads fails the unit test, and not wiring readBudget into the handle fails the provider test.
  • npx vitest run passed on src/native, src/resources, src/application/binaryDiagnostics, src/domain/pe and the PE filesystem and MCP boundary suites. Also clean: scoped tsc, the push hook's full typecheck, oxlint, oxfmt, the architecture guards and rea-numeric-limits.
  • Not covered: an end-to-end file above 4 GiB, because hashing one in a test is too costly. The ceiling projection is shared code exercised by the 512 MiB readers' existing handler. tests/boundary/filesystem/stableArtifact.test.ts has 7 failures that predate this branch (descriptor byte counts and chunkBytes); they reproduce with this change reverted.
  • inspect-dwarf-units (listed in [Bug] Static PE readers refuse PEs over 64 MiB by default although they read only directories #14167) is not a PE reader and still gates on file size. It can adopt readBudget in a follow-up.

Fixes #14167

…eaders

Resources and certificates now admit headers through readPeImage, and
resources map RVAs through the shared file-backed mapping. The owner reads
the optional header at fixed offsets, caps data directories at 16, and lets
a PE signature at e_lfanew override a DOS relocation table. Export, import,
TLS and resource directories are read at their RVA whatever Size declares.
Disregarded declarations are reported in header_anomalies.

BREAKING CHANGE: import and delay-import walks continue to the null
descriptor past a short directory Size instead of stopping with a
malformed_entry issue, and inspect-pe-resources reports an unsupported
optional-header magic as unsupported_target.
Only a relocation table that describes no DOS load module yields e_lfanew
to a PE signature; update MZ/PE conformance to the shared owner.
max_file_bytes now counts the header prefix and every positional read of
headers, directory tables and their referenced ranges. The identity pass
that hashes the whole file is bounded only by a fixed whole-file ceiling:
4 GiB minus one byte for the PE-only readers, whose file offsets are 32-bit,
and 512 MiB for hardening and exports. An exhausted allowance is a capacity
refusal with a typed parameter details.limit.

BREAKING CHANGE: max_file_bytes no longer refuses a large file whose reads
fit; an exhausted read allowance reports resource capacity instead of
file-size, and only a file above the fixed ceiling reports file-size.
The resource walk is bounded by the file-backed run from the directory RVA
rather than its Size, and a Size that ends before the bytes walked is a
directory_size_below_structure anomaly. The TLS result carries
structure_location for the structure read past a short Size, and Evidence
cites it.
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-24T02:40:30.430222Z
We couldn’t complete this review. Comment @codex review to try again.
7ca0985 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@greptile-apps

greptile-apps Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new blocking issue was found.

Summary

The six static readers now count header-prefix and positional reads against max_file_bytes, rather than rejecting files based on their total size.

  • Static readers now spend max_file_bytes on parser reads, not total file size.
  • PE reports now show their image base and import slots as usable addresses.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Selected file] --> B{Within fixed whole-file ceiling?}
  B -- No --> C[file-size refusal]
  B -- Yes --> D[Stream SHA-256 and retain header prefix]
  D --> E[Charge prefix and positional reads]
  E --> F{Within max_file_bytes?}
  F -- No --> G[capacity refusal with parameter details]
  F -- Yes --> H[Parse tables]
  H --> I[Check file stability and close]
  I --> J[Return report]
Loading

Reviews (8) · Last reviewed commit: "Merge remote-tracking branch 'origin/mai..." · Reviewed by Greptile

…MZ headers malformed

An oversized resource Size is reported without requiring its unused bytes
to map. A Windows-header candidate is malformed when the DOS module is also
invalid, and the guide states that a valid DOS relocation table owns
e_lfanew.
Base automatically changed from fix/pe-shared-header-admission to main November 24, 2026 04:27
…4263)

* feat(pe): report image_base and IAT slot VAs in static PE results

Every static PE result reports the optional-header image_base once as a hex
VA, and import rows add iat_va beside iat_rva so a slot goes straight to
xrefs. Fields name their space as rva, va or file_offset; the new fields are
optional so saved Evidence still parses, and the imports service rejects an
iat_va that disagrees with image_base plus iat_rva.

* fix(pe): document iat_va for rebased databases and test VA rejection

iat_va is relative to the file's ImageBase; a rebased provider database
needs its own base plus iat_rva. Cover the service rejection of a slot VA
that disagrees with image_base plus iat_rva.
…ission

# Conflicts:
#	docs/guides/native/binary-diagnostics.md
#	src/domain/native/nativeExportParser.ts
#	src/domain/native/nativeExports.ts
#	src/domain/native/peCertificates.ts
#	src/domain/native/peImports.ts
#	src/domain/native/peLoadConfig.ts
#	src/domain/native/peResources.ts
#	src/domain/pe/peResourceLayout.ts
#	src/native/pe/PeCertificateParser.ts
#	src/native/pe/PeImportParser.ts
…ission

# Conflicts:
#	docs/reference/limits-and-recovery.md
@morluto
morluto enabled auto-merge (squash) November 24, 2026 14:57
@morluto
morluto merged commit 789ca04 into main Nov 24, 2026
59 checks passed
@morluto
morluto deleted the fix/pe-bytes-read-admission branch November 24, 2026 15:12
@morluto morluto added area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior bug Something isn't working labels Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Static PE readers refuse PEs over 64 MiB by default although they read only directories

1 participant