Scripts: map PE export RVAs only into file-backed bytes #2652

Open
luckyteagirl wants to merge 2 commits from upstream-pr/2662 into main
luckyteagirl commented 2026-11-26 05:29:41 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by luckyteagirl on 2026-11-26T05:29:41Z.


The PE export inspector can read unrelated file bytes when export metadata extends beyond the file-backed part of a section. Bound RVA mapping, fixed-width reads, and string scanning to the section's raw bytes and the end of the file. PE section format

What is in it

  • Map export RVAs using SizeOfRawData. Check the entire export directory and each name, ordinal, and address table entry before reading it.
  • Require export strings to terminate within their section's file-backed bytes. Preserve the original two-section regression and add cases for a name in the file overlay, a terminator beyond the raw section, and a table entry across the raw section boundary.

Tested

  • On upstream c2ee10e4, the original two-section fixture read a name from the next section. Public head 0841c094 rejected that RVA but still accepted a string and a table entry that crossed the raw boundary. The updated head 649d3856 rejects each with an RVA-specific diagnostic.
  • On Windows 11 build 26200 and Python 3.11.9, python -B -m unittest discover -s scripts -p 'test_*.py' passed 28 tests with 0 failures. The PE32 and PE32+ alias and forwarder regressions passed.
  • Native and WOW64 kernel32.dll output matched both upstream and the prior PR head byte for byte, across 1,700 and 1,672 output lines respectively. git diff --check passed.

Not tested

Retail title PE files, Rust or GPU tests, and a retail title run. This changes only the Python export inspector and its synthetic fixtures.

AI use

OpenAI Codex (GPT-6.2 Sol) implemented the original RVA fix and regression. OpenAI Codex (GPT-6, exact variant unavailable) implemented and integrated the wider read bounds, ran the Python suite and DLL comparisons, and drafted this description.

<!-- greyfinch-sync:ivall/xcl:issue:5504567126 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2662). Originally posted by [luckyteagirl](https://github.com/luckyteagirl) on 2026-11-26T05:29:41Z. --- The PE export inspector can read unrelated file bytes when export metadata extends beyond the file-backed part of a section. Bound RVA mapping, fixed-width reads, and string scanning to the section's raw bytes and the end of the file. [PE section format](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#section-table-section-headers) **What is in it** - Map export RVAs using `SizeOfRawData`. Check the entire export directory and each name, ordinal, and address table entry before reading it. - Require export strings to terminate within their section's file-backed bytes. Preserve the original two-section regression and add cases for a name in the file overlay, a terminator beyond the raw section, and a table entry across the raw section boundary. **Tested** - On upstream `c2ee10e4`, the original two-section fixture read a name from the next section. Public head `0841c094` rejected that RVA but still accepted a string and a table entry that crossed the raw boundary. The updated head `649d3856` rejects each with an RVA-specific diagnostic. - On Windows 11 build 26200 and Python 3.11.9, `python -B -m unittest discover -s scripts -p 'test_*.py'` passed 28 tests with 0 failures. The PE32 and PE32+ alias and forwarder regressions passed. - Native and WOW64 `kernel32.dll` output matched both upstream and the prior PR head byte for byte, across 1,700 and 1,672 output lines respectively. `git diff --check` passed. **Not tested** Retail title PE files, Rust or GPU tests, and a retail title run. This changes only the Python export inspector and its synthetic fixtures. **AI use** OpenAI Codex (GPT-6.2 Sol) implemented the original RVA fix and regression. OpenAI Codex (GPT-6, exact variant unavailable) implemented and integrated the wider read bounds, ran the Python suite and DLL comparisons, and drafted this description.
Foniass commented 2026-12-01 08:37:18 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T08:37:18Z.


Triage (#1607) – head 649d385

No open PR limits pe_exports.py reads to a section's file-backed bytes. #1814 adds bounds checks against the file size but keeps max(VirtualSize, SizeOfRawData). #1685 maps RVAs in the image headers and #1494 checks NumberOfRvaAndSizes; both are different changes, but #1685 will conflict with raw_span. Most of the lines already on main come from the test helper make_export_image, which was moved out of the existing test.
An RVA in the zero-filled tail, or a table entry that crosses the end of a section's raw data, now raises instead of reading the next section's file bytes, and the tests cover both cases. No problems found in the diff.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7305561202 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2662#issuecomment-7305561202). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T08:37:18Z. --- **Triage (#1607)** – head 649d385 No open PR limits pe_exports.py reads to a section's file-backed bytes. #1814 adds bounds checks against the file size but keeps `max(VirtualSize, SizeOfRawData)`. #1685 maps RVAs in the image headers and #1494 checks NumberOfRvaAndSizes; both are different changes, but #1685 will conflict with `raw_span`. Most of the lines already on main come from the test helper `make_export_image`, which was moved out of the existing test. An RVA in the zero-filled tail, or a table entry that crosses the end of a section's raw data, now raises instead of reading the next section's file bytes, and the tests cover both cases. No problems found in the diff. Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
Decodere commented 2026-12-02 09:09:45 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Decodere on 2026-12-02T09:09:45Z.


merge-check
base: 1c8b551475
head: 649d3856e7
merge: clean (git merge --no-edit onto 1c8b5514, git diff --check clean; scripts/re/pe_exports.py and scripts/test_pe_exports.py, +119/-39)
build: not run (Tier 0 Python script diff; no Rust code changed)
tests: python -m unittest discover -s scripts -p "test_pe_exports.py" -v -> 4 passed, 0 failed (base 1c8b5514: 1 passed; 3 new boundary tests passed); python -m unittest discover -s scripts -p "test_*.py" -> 68 passed, 1 error (test_discord_mirror.py on Windows, same failure on base 1c8b5514). Rust workspace tests not run (Tier 0).
review: Read the full diff (+119/-39). Tracks each section's SizeOfRawData instead of max(VirtualSize, SizeOfRawData), validates every export directory/table entry and NUL-terminated string lies wholly within file-backed section bytes (scripts/re/pe_exports.py:15-33,39-48), and rejects RVAs landing in zero-filled virtual tails or crossing section ends. The 3 new tests cover zero-filled tails, overlay reads, unterminated strings, and straddling table entries. No problems found.
performance: none (offline PE inspection script in scripts/re/; zero runtime impact)
verdict: safe to merge
hardware / tool and model: NVIDIA GeForce RTX 4060 8 GB (driver 621.40), Intel Core i5-11400F, 32 GB RAM, Windows 11 Build 26200, Python 3.15.2, Rust 1.100.1 MSVC, DXC 1.9.2602.17 / Antigravity (Gemini 4 Neon)

<!-- greyfinch-sync:ivall/xcl:comment:7340469784 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2662#issuecomment-7340469784). Originally posted by [Decodere](https://github.com/Decodere) on 2026-12-02T09:09:45Z. --- merge-check base: 1c8b551475e1e7bfe48f94448492a4c3544a2e0f head: 649d3856e7985f8afb5db9af64d40d1dec5e6d6d merge: clean (`git merge --no-edit` onto `1c8b5514`, `git diff --check` clean; `scripts/re/pe_exports.py` and `scripts/test_pe_exports.py`, +119/-39) build: not run (Tier 0 Python script diff; no Rust code changed) tests: `python -m unittest discover -s scripts -p "test_pe_exports.py" -v` -> 4 passed, 0 failed (base `1c8b5514`: 1 passed; 3 new boundary tests passed); `python -m unittest discover -s scripts -p "test_*.py"` -> 68 passed, 1 error (`test_discord_mirror.py` on Windows, same failure on base `1c8b5514`). Rust workspace tests not run (Tier 0). review: Read the full diff (+119/-39). Tracks each section's `SizeOfRawData` instead of `max(VirtualSize, SizeOfRawData)`, validates every export directory/table entry and NUL-terminated string lies wholly within file-backed section bytes (`scripts/re/pe_exports.py:15-33,39-48`), and rejects RVAs landing in zero-filled virtual tails or crossing section ends. The 3 new tests cover zero-filled tails, overlay reads, unterminated strings, and straddling table entries. No problems found. performance: none (offline PE inspection script in `scripts/re/`; zero runtime impact) verdict: safe to merge hardware / tool and model: NVIDIA GeForce RTX 4060 8 GB (driver 621.40), Intel Core i5-11400F, 32 GB RAM, Windows 11 Build 26200, Python 3.15.2, Rust 1.100.1 MSVC, DXC 1.9.2602.17 / Antigravity (Gemini 4 Neon)
gut0to commented 2026-12-02 14:21:15 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by gut0to on 2026-12-02T14:21:15Z.

Code review location: scripts/test_pe_exports.py; line 93

@@ -49,6 +86,42 @@ def test_all_names_for_an_ordinal_are_printed(self):
                 self.assertRegex(result.stdout, r"(?m)^  #8\s+\(ordinal only\) @0x1110$")
                 self.assertRegex(result.stdout, r"(?m)^  #9\s+Forwarded @0x10c0 -> other.Target$")
 
+    def test_export_names_cannot_read_outside_section_raw_data(self):
+        for name_rva, offset, raw_name, error in (
+            (0x1200, 0x400, b"OverlayName\0", "is outside file-backed section data"),
+            (0x11FC, 0x3FC, b"FakeName\0", "has no terminator in its section"),
+        ):

Could these raw-boundary fixtures also run against a PE32 (0x10B) image? make_export_image supports both formats, and the existing alias/forwarder test is parameterized for 0x10B and 0x20B, but these new zero-filled-tail, string-termination, and table-entry boundary cases all use PE32+. The export data-directory offset differs between optional-header formats, so running the boundary cases against both would cover the PE32 parsing path too. AI-assisted review (model: GPT-6; tool name unavailable in this session).

<!-- greyfinch-sync:ivall/xcl:review-comment:4770993622 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2662#discussion_r4770993622). Originally posted by [gut0to](https://github.com/gut0to) on 2026-12-02T14:21:15Z. **Code review location:** `scripts/test_pe_exports.py`; line 93 ~~~~diff @@ -49,6 +86,42 @@ def test_all_names_for_an_ordinal_are_printed(self): self.assertRegex(result.stdout, r"(?m)^ #8\s+\(ordinal only\) @0x1110$") self.assertRegex(result.stdout, r"(?m)^ #9\s+Forwarded @0x10c0 -> other.Target$") + def test_export_names_cannot_read_outside_section_raw_data(self): + for name_rva, offset, raw_name, error in ( + (0x1200, 0x400, b"OverlayName\0", "is outside file-backed section data"), + (0x11FC, 0x3FC, b"FakeName\0", "has no terminator in its section"), + ): ~~~~ --- Could these raw-boundary fixtures also run against a PE32 (0x10B) image? make_export_image supports both formats, and the existing alias/forwarder test is parameterized for 0x10B and 0x20B, but these new zero-filled-tail, string-termination, and table-entry boundary cases all use PE32+. The export data-directory offset differs between optional-header formats, so running the boundary cases against both would cover the PE32 parsing path too. AI-assisted review (model: GPT-6; tool name unavailable in this session).
gut0to commented 2026-12-02 14:21:15 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by gut0to on 2026-12-02T14:21:15Z.

Review: COMMENTED


AI-assisted review (model: GPT-6; tool name unavailable in this session).

<!-- greyfinch-sync:ivall/xcl:review:6152628423 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2662#pullrequestreview-6152628423). Originally posted by [gut0to](https://github.com/gut0to) on 2026-12-02T14:21:15Z. **Review:** COMMENTED --- AI-assisted review (model: GPT-6; tool name unavailable in this session).
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2662:upstream-pr/2662'
git switch 'upstream-pr/2662'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2662'
git switch 'upstream-pr/2662'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2662'
git switch 'upstream-pr/2662'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2662'
git switch 'main'
git merge --squash 'upstream-pr/2662'
git switch 'main'
git merge --ff-only 'upstream-pr/2662'
git switch 'main'
git merge 'upstream-pr/2662'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!2652
No description provided.