pe: bound RVA reads and TLS slots by Misc.VirtualSize in intake_exe.py #753

Merged
Ammmae0x merged 3 commits from upstream-pr/759 into main 2026-11-23 03:15:29 +00:00
Ammmae0x commented 2026-11-23 01:06:20 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-23T01:06:20Z.


Fixes #744

Change

inspect_executable() in tools/intake_exe.py previously bounded section_for(), read_rva(), and the TLS callback slots calculation by SizeOfRawData alone and required pe.get_data(rva, length) to return length file-backed bytes. This rejected valid RVAs in zero-filled section virtual tails (Misc_VirtualSize > SizeOfRawData) and failed to clamp reads when Misc_VirtualSize < SizeOfRawData.

  • Added section_span(s) inside inspect_executable() to compute min(s.Misc_VirtualSize or s.SizeOfRawData, max(0, hdr.SizeOfImage - s.VirtualAddress)), matching the Windows PE loader and tools/pe_map.cpp.
  • Updated section_for() and the TLS callback slot bound to use section_span(...).
  • Updated read_rva() to read the file-backed prefix via section.get_data(rva, backed) (still raising Truncated data if short) and zero-pad the remaining length - backed uninitialized virtual-tail bytes.
  • Added check_tls_virtual_tail() in tests/intake/check.py covering positive cases (TLS directory inside a virtual tail, TLS directory straddling SizeOfRawData, callback table straddling SizeOfRawData with a zero terminator in the virtual tail, and Misc_VirtualSize == 0 fallback) and four negative controls (exit 2: unterminated callback table hitting Misc_VirtualSize, TLS directory read past Misc_VirtualSize, non-executable callback in a straddling table, and Misc_VirtualSize exceeding SizeOfImage).

Verification

Tested on commit 401dea0 (base 356a7be), Windows x64 (MSVC 19.50.35727, CMake 4.2.3-msvc3, Ninja 1.12.1, Python 3.12.8):

  • ctest --preset dev -N: 328 tests registered (matches baseline).
  • cmake --build --preset dev --target intake_provider intake_delay intake_candidate & release: 0 errors, 0 warnings.
  • ctest --preset dev --no-tests=error -R "static_exe_intake|conditional_import_paths|pe_inventory|forwarders|dependency_paths" -V: 4/4 passed, 0 skipped, 0 failed (4.24s).
  • ctest --preset release --no-tests=error -R "static_exe_intake|conditional_import_paths|pe_inventory|forwarders|dependency_paths" -V: 4/4 passed, 0 skipped, 0 failed (3.99s).
  • .\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py: exit 0 (form coverage checks passed).
  • .\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py: exit 0 (Checked 11 Markdown files and 137 links.).
  • .\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py: exit 0 (Checked 618 tracked source, script and build/CI files: no bidirectional controls.).

Evidence and follow-up

All new checks mutate temporary copies of the existing source-built intake_candidate fixture in tests/intake/check.py; no binary fixtures or external inputs were added.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6550177774 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/759). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-23T01:06:20Z. --- Fixes #744 ## Change `inspect_executable()` in `tools/intake_exe.py` previously bounded `section_for()`, `read_rva()`, and the TLS callback `slots` calculation by `SizeOfRawData` alone and required `pe.get_data(rva, length)` to return `length` file-backed bytes. This rejected valid RVAs in zero-filled section virtual tails (`Misc_VirtualSize > SizeOfRawData`) and failed to clamp reads when `Misc_VirtualSize < SizeOfRawData`. - Added `section_span(s)` inside `inspect_executable()` to compute `min(s.Misc_VirtualSize or s.SizeOfRawData, max(0, hdr.SizeOfImage - s.VirtualAddress))`, matching the Windows PE loader and `tools/pe_map.cpp`. - Updated `section_for()` and the TLS callback slot bound to use `section_span(...)`. - Updated `read_rva()` to read the file-backed prefix via `section.get_data(rva, backed)` (still raising `Truncated data` if short) and zero-pad the remaining `length - backed` uninitialized virtual-tail bytes. - Added `check_tls_virtual_tail()` in `tests/intake/check.py` covering positive cases (TLS directory inside a virtual tail, TLS directory straddling `SizeOfRawData`, callback table straddling `SizeOfRawData` with a zero terminator in the virtual tail, and `Misc_VirtualSize == 0` fallback) and four negative controls (exit `2`: unterminated callback table hitting `Misc_VirtualSize`, TLS directory read past `Misc_VirtualSize`, non-executable callback in a straddling table, and `Misc_VirtualSize` exceeding `SizeOfImage`). ## Verification Tested on commit `401dea0` (base `356a7be`), Windows x64 (`MSVC 19.50.35727`, `CMake 4.2.3-msvc3`, `Ninja 1.12.1`, `Python 3.12.8`): - `ctest --preset dev -N`: `328` tests registered (matches baseline). - `cmake --build --preset dev --target intake_provider intake_delay intake_candidate` & `release`: 0 errors, 0 warnings. - `ctest --preset dev --no-tests=error -R "static_exe_intake|conditional_import_paths|pe_inventory|forwarders|dependency_paths" -V`: `4/4 passed`, `0 skipped`, `0 failed` (`4.24s`). - `ctest --preset release --no-tests=error -R "static_exe_intake|conditional_import_paths|pe_inventory|forwarders|dependency_paths" -V`: `4/4 passed`, `0 skipped`, `0 failed` (`3.99s`). - `.\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py`: exit `0` (`form coverage checks passed`). - `.\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py`: exit `0` (`Checked 11 Markdown files and 137 links.`). - `.\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py`: exit `0` (`Checked 618 tracked source, script and build/CI files: no bidirectional controls.`). ## Evidence and follow-up All new checks mutate temporary copies of the existing source-built `intake_candidate` fixture in `tests/intake/check.py`; no binary fixtures or external inputs were added. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted.
upstream-sync closed this pull request 2026-11-24 06:44:39 +00:00
Ammmae0x commented 2026-11-24 06:48:56 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-23T01:57:07Z.


Updated in d9c1f23 to match the #744 feedback:

  • Separated virtual section membership (section_span() / section_for()) from file-backed bytes (raw_span()).
  • Updated read_rva() to prove the entire [rva, rva + length) span is file-backed before unpacking rather than zero-padding unbacked virtual tails (Unbacked or truncated data at RVA ...).
  • Added explicit ValueError diagnostics for TLS callback slots/tables truncated at either raw_span (straddling or inside the zero-filled tail) or section_span (Truncated or unbacked TLS callback slot ..., Truncated TLS callback slot ..., Unterminated TLS callback table).
  • Updated tests/intake/candidate.cpp and tests/intake/check.py so .data is source-built with a raw prefix and zero-filled virtual tail (Misc_VirtualSize > SizeOfRawData) in both Debug and Release, covering raw-prefix and Misc_VirtualSize == 0 positive cases plus 9 exit-2 negative controls.
  • Verified locally in dev (Debug) and release (/O2) (3/3 intake/forwarder CTests, 0 warnings/errors, pure-Python checks passing), and all 4 required CI checks passed on d9c1f23.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7089542182 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/759#issuecomment-7089542182). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-23T01:57:07Z. --- Updated in d9c1f23 to match the #744 feedback: - Separated virtual section membership (`section_span()` / `section_for()`) from file-backed bytes (`raw_span()`). - Updated `read_rva()` to prove the entire `[rva, rva + length)` span is file-backed before unpacking rather than zero-padding unbacked virtual tails (`Unbacked or truncated data at RVA ...`). - Added explicit `ValueError` diagnostics for TLS callback slots/tables truncated at either `raw_span` (straddling or inside the zero-filled tail) or `section_span` (`Truncated or unbacked TLS callback slot ...`, `Truncated TLS callback slot ...`, `Unterminated TLS callback table`). - Updated `tests/intake/candidate.cpp` and `tests/intake/check.py` so `.data` is source-built with a raw prefix and zero-filled virtual tail (`Misc_VirtualSize > SizeOfRawData`) in both Debug and Release, covering `raw-prefix` and `Misc_VirtualSize == 0` positive cases plus 9 exit-2 negative controls. - Verified locally in `dev` (`Debug`) and `release` (`/O2`) (`3/3` intake/forwarder CTests, `0` warnings/errors, pure-Python checks passing), and all 4 required CI checks passed on `d9c1f23`. AI-assisted.
R0Y00 commented 2026-11-24 06:48:56 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T02:13:30Z.


Thanks for revising this around my earlier file-backed-only feedback. The landed #760 uses mapped-image semantics for TLS startup inspection. It validates and reads the file-backed prefix, then supplies the zero bytes the mapper provides within the virtual section. That differs from the earlier review guidance, so this revision deserves a separate decision. I’ll leave it open. The source-built raw-prefix, zero VirtualSize fallback and image-boundary checks are useful candidates for keeping with the landed behavior. The strict rejection of valid zero-filled TLS data would change that behavior.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7089764026 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/759#issuecomment-7089764026). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T02:13:30Z. --- Thanks for revising this around my earlier file-backed-only feedback. The landed #760 uses mapped-image semantics for TLS startup inspection. It validates and reads the file-backed prefix, then supplies the zero bytes the mapper provides within the virtual section. That differs from the earlier review guidance, so this revision deserves a separate decision. I’ll leave it open. The source-built raw-prefix, zero VirtualSize fallback and image-boundary checks are useful candidates for keeping with the landed behavior. The strict rejection of valid zero-filled TLS data would change that behavior.
Ammmae0x commented 2026-11-24 06:48:57 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-23T02:28:56Z.


Merged upstream/main (including #760) in 39d989c and updated the branch to keep the landed mapped-image semantics:

  • Kept tools/intake_exe.py identical to upstream/main (#760), preserving file-backed prefix reads + zero-filled virtual tails within section_span, Misc_VirtualSize or SizeOfRawData fallback, and SizeOfImage clamping.
  • Removed the strict rejection of valid zero-filled TLS directories and tail-terminated callback tables.
  • Kept the source-built .data raw-prefix + zero-tail fixture in tests/intake/candidate.cpp (intake_raw_prefix + intake_zero_tail[4096]).
  • Integrated the additive checks into check_virtual_tail_tls() in tests/intake/check.py: source-built .data raw-prefix TLS (tls-raw-prefix), populated TLS directory crossing into the zero tail (tls-dir-crossing-populated), Misc_VirtualSize == 0 fallback (tls-zero-vsize), and negative controls for Misc_VirtualSize bounds, partial slots, invalid straddling slots, non-executable callbacks, and SizeOfImage clamping (tls-unterminated-vsize, tls-partial-vsize-slot, tls-partial-vsize-start, tls-slot-straddle, tls-past-vsize, tls-nonexec-tail-cb, tls-past-image).

Verification on 39d989c (Windows 11, x64 MSVC):

  • dev (Debug): cmake --build --preset dev --target intake_candidate intake_provider intake_delay intake_forward intake_forward_caller (0 errors, 0 warnings); ctest --preset dev --no-tests=error -R 'static_exe_intake|conditional_import_paths|bundled_export_forwarders' -V -> 3/3 passed.
  • release (/O2): cmake --build --preset release --target intake_candidate intake_provider intake_delay intake_forward intake_forward_caller (0 errors, 0 warnings); ctest --preset release --no-tests=error -R 'static_exe_intake|conditional_import_paths|bundled_export_forwarders' -V -> 3/3 passed.
  • Pure-Python checks (tests/intake/paths_check.py, tests/form_coverage_check.py, tests/docs_links_check.py, tests/bidi_controls_check.py, tests/dependency_paths_check.py, tests/layout_chunks_check.py) -> all exited 0.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7089971737 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/759#issuecomment-7089971737). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-23T02:28:56Z. --- Merged `upstream/main` (including #760) in `39d989c` and updated the branch to keep the landed mapped-image semantics: - Kept `tools/intake_exe.py` identical to `upstream/main` (#760), preserving file-backed prefix reads + zero-filled virtual tails within `section_span`, `Misc_VirtualSize or SizeOfRawData` fallback, and `SizeOfImage` clamping. - Removed the strict rejection of valid zero-filled TLS directories and tail-terminated callback tables. - Kept the source-built `.data` raw-prefix + zero-tail fixture in `tests/intake/candidate.cpp` (`intake_raw_prefix` + `intake_zero_tail[4096]`). - Integrated the additive checks into `check_virtual_tail_tls()` in `tests/intake/check.py`: source-built `.data` raw-prefix TLS (`tls-raw-prefix`), populated TLS directory crossing into the zero tail (`tls-dir-crossing-populated`), `Misc_VirtualSize == 0` fallback (`tls-zero-vsize`), and negative controls for `Misc_VirtualSize` bounds, partial slots, invalid straddling slots, non-executable callbacks, and `SizeOfImage` clamping (`tls-unterminated-vsize`, `tls-partial-vsize-slot`, `tls-partial-vsize-start`, `tls-slot-straddle`, `tls-past-vsize`, `tls-nonexec-tail-cb`, `tls-past-image`). Verification on `39d989c` (Windows 11, x64 MSVC): - `dev` (`Debug`): `cmake --build --preset dev --target intake_candidate intake_provider intake_delay intake_forward intake_forward_caller` (0 errors, 0 warnings); `ctest --preset dev --no-tests=error -R 'static_exe_intake|conditional_import_paths|bundled_export_forwarders' -V` -> 3/3 passed. - `release` (`/O2`): `cmake --build --preset release --target intake_candidate intake_provider intake_delay intake_forward intake_forward_caller` (0 errors, 0 warnings); `ctest --preset release --no-tests=error -R 'static_exe_intake|conditional_import_paths|bundled_export_forwarders' -V` -> 3/3 passed. - Pure-Python checks (`tests/intake/paths_check.py`, `tests/form_coverage_check.py`, `tests/docs_links_check.py`, `tests/bidi_controls_check.py`, `tests/dependency_paths_check.py`, `tests/layout_chunks_check.py`) -> all exited 0. AI-assisted.
R0Y00 commented 2026-11-24 06:48:58 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T03:15:21Z.


I reviewed 39d989c after the policy change. tools/intake_exe.py is byte-identical to merged #760. The PR now changes only the source-built intake fixture and its checks. I rebuilt both intake fixtures in an isolated Windows Release tree and ran ctest --preset release --no-tests=error -R 'static_exe_intake|conditional_import_paths|bundled_export_forwarders': 3/3 passed. The added checks distinguish the mapped zero tail, raw prefix, VirtualSize fallback and the section/image bounds. Thanks for keeping those controls with the merged behavior.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7090592158 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/759#issuecomment-7090592158). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T03:15:21Z. --- I reviewed `39d989c` after the policy change. `tools/intake_exe.py` is byte-identical to merged #760. The PR now changes only the source-built intake fixture and its checks. I rebuilt both intake fixtures in an isolated Windows Release tree and ran `ctest --preset release --no-tests=error -R 'static_exe_intake|conditional_import_paths|bundled_export_forwarders'`: 3/3 passed. The added checks distinguish the mapped zero tail, raw prefix, VirtualSize fallback and the section/image bounds. Thanks for keeping those controls with the merged behavior.
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
R0Y00/Xbox2Windows!753
No description provided.