pe: intake_exe.py bounds-checks RVA reads and TLS callback slots by SizeOfRawData instead of Misc.VirtualSize #738

Closed
opened 2026-11-23 00:26:10 +00:00 by Ammmae0x · 2 comments
Ammmae0x commented 2026-11-23 00:26:10 +00:00 (Migrated from github.com)
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-23T00:26:10Z.


Commit or build

fd2486f (upstream/main)

Reproduction

Run against a temporary copy of the source-built fixture out/build/dev/intake_forward_caller.exe (whose .data section has VirtualAddress=0xc000, Misc_VirtualSize=0x2b9, SizeOfRawData=0x200):

out\pe-env\Scripts\python.exe -B -c '
import sys, tempfile, pathlib, pefile; sys.path.insert(0, "tools")
import intake_exe

with pefile.PE("out/build/dev/intake_forward_caller.exe", fast_load=True) as pe:
    s = next(sec for sec in pe.sections if sec.Misc_VirtualSize > sec.SizeOfRawData)
    tail_rva = s.VirtualAddress + s.SizeOfRawData
    print("Section:", s.Name.rstrip(b"\x00").decode(),
          "VA=", hex(s.VirtualAddress),
          "VSize=", hex(s.Misc_VirtualSize),
          "RawSize=", hex(s.SizeOfRawData))
    print("pe.get_data(tail_rva, 40) len:", len(pe.get_data(tail_rva, 40)))
    pe.OPTIONAL_HEADER.DATA_DIRECTORY[9].VirtualAddress = tail_rva
    pe.OPTIONAL_HEADER.DATA_DIRECTORY[9].Size = 40
    td = pathlib.Path(tempfile.mkdtemp())
    p = td / "tls_tail.exe"
    pe.write(str(p))
    intake_exe.inspect_executable(p)
'

Expected behavior

Consistent with the Windows PE loader and tools/pe_map.cpp:95 (const DWORD span = s.Misc.VirtualSize ? s.Misc.VirtualSize : s.SizeOfRawData;):

  1. section_for() (tools/intake_exe.py:81-88) and TLS callback slot counting (tools/intake_exe.py:109) should bound section spans by Misc_VirtualSize (falling back to SizeOfRawData when Misc_VirtualSize == 0, and clamped to SizeOfImage), not SizeOfRawData alone.
  2. read_rva() (tools/intake_exe.py:90-95) should zero-pad bytes in the uninitialized virtual tail [SizeOfRawData, Misc_VirtualSize) where pe.get_data() returns fewer bytes (or b""), while continuing to reject truncated file-backed bytes within SizeOfRawData.

Acceptance criteria:

  • Update section_for(), read_rva(), and the TLS callback slot bound in tools/intake_exe.py:81-111 to honor Misc_VirtualSize and zero-fill uninitialized BSS tail reads.
  • Add a regression test in tests/intake/check.py placing a TLS directory / zero-terminated callback table in a section's virtual tail (Misc_VirtualSize > SizeOfRawData) and verifying intake_exe.inspect_executable() parses it cleanly.

Observed behavior

section_for() (tools/intake_exe.py:84) checks offset + length <= s.SizeOfRawData, and inspect_executable() (tools/intake_exe.py:109) computes slots = (section.VirtualAddress + section.SizeOfRawData - table) // 8. Additionally, read_rva() (tools/intake_exe.py:92-94) requires len(pe.get_data(rva, length)) == length, which fails for virtual-tail RVAs because pefile returns b"" past SizeOfRawData.

Running the reproduction prints:

Section: .data VA= 0xc000 VSize= 0x2b9 RawSize= 0x200
pe.get_data(tail_rva, 40) len: 0
Traceback (most recent call last):
  ...
ValueError: Unmapped or unsuitable RVA 0xc200

Environment

Windows 10 Pro 24H2 (build 26100.7623), Python 3.12.8 (out\pe-env\Scripts\python.exe).

Checklist

  • I reproduced this on a source-built input.
  • I did not include game files, dumps, distribution links or credentials.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6549772817 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/744). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-23T00:26:10Z. --- ### Commit or build `fd2486f` (`upstream/main`) ### Reproduction Run against a temporary copy of the source-built fixture `out/build/dev/intake_forward_caller.exe` (whose `.data` section has `VirtualAddress=0xc000`, `Misc_VirtualSize=0x2b9`, `SizeOfRawData=0x200`): ```powershell out\pe-env\Scripts\python.exe -B -c ' import sys, tempfile, pathlib, pefile; sys.path.insert(0, "tools") import intake_exe with pefile.PE("out/build/dev/intake_forward_caller.exe", fast_load=True) as pe: s = next(sec for sec in pe.sections if sec.Misc_VirtualSize > sec.SizeOfRawData) tail_rva = s.VirtualAddress + s.SizeOfRawData print("Section:", s.Name.rstrip(b"\x00").decode(), "VA=", hex(s.VirtualAddress), "VSize=", hex(s.Misc_VirtualSize), "RawSize=", hex(s.SizeOfRawData)) print("pe.get_data(tail_rva, 40) len:", len(pe.get_data(tail_rva, 40))) pe.OPTIONAL_HEADER.DATA_DIRECTORY[9].VirtualAddress = tail_rva pe.OPTIONAL_HEADER.DATA_DIRECTORY[9].Size = 40 td = pathlib.Path(tempfile.mkdtemp()) p = td / "tls_tail.exe" pe.write(str(p)) intake_exe.inspect_executable(p) ' ``` ### Expected behavior Consistent with the Windows PE loader and `tools/pe_map.cpp:95` (`const DWORD span = s.Misc.VirtualSize ? s.Misc.VirtualSize : s.SizeOfRawData;`): 1. `section_for()` (`tools/intake_exe.py:81-88`) and TLS callback slot counting (`tools/intake_exe.py:109`) should bound section spans by `Misc_VirtualSize` (falling back to `SizeOfRawData` when `Misc_VirtualSize == 0`, and clamped to `SizeOfImage`), not `SizeOfRawData` alone. 2. `read_rva()` (`tools/intake_exe.py:90-95`) should zero-pad bytes in the uninitialized virtual tail `[SizeOfRawData, Misc_VirtualSize)` where `pe.get_data()` returns fewer bytes (or `b""`), while continuing to reject truncated file-backed bytes within `SizeOfRawData`. **Acceptance criteria:** - Update `section_for()`, `read_rva()`, and the TLS callback slot bound in `tools/intake_exe.py:81-111` to honor `Misc_VirtualSize` and zero-fill uninitialized BSS tail reads. - Add a regression test in `tests/intake/check.py` placing a TLS directory / zero-terminated callback table in a section's virtual tail (`Misc_VirtualSize > SizeOfRawData`) and verifying `intake_exe.inspect_executable()` parses it cleanly. ### Observed behavior `section_for()` (`tools/intake_exe.py:84`) checks `offset + length <= s.SizeOfRawData`, and `inspect_executable()` (`tools/intake_exe.py:109`) computes `slots = (section.VirtualAddress + section.SizeOfRawData - table) // 8`. Additionally, `read_rva()` (`tools/intake_exe.py:92-94`) requires `len(pe.get_data(rva, length)) == length`, which fails for virtual-tail RVAs because `pefile` returns `b""` past `SizeOfRawData`. Running the reproduction prints: ```text Section: .data VA= 0xc000 VSize= 0x2b9 RawSize= 0x200 pe.get_data(tail_rva, 40) len: 0 Traceback (most recent call last): ... ValueError: Unmapped or unsuitable RVA 0xc200 ``` ### Environment Windows 10 Pro 24H2 (build 26100.7623), Python 3.12.8 (`out\pe-env\Scripts\python.exe`). ### Checklist - [x] I reproduced this on a source-built input. - [x] I did not include game files, dumps, distribution links or credentials. AI-assisted.
R0Y00 commented 2026-11-24 06:48:54 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T01:08:43Z.


Please keep section membership separate from file-backed bytes. An RVA can be inside the virtual section and still have no payload in the file. Prove that the entire requested field is file-backed before unpacking it, and give a clear error for a truncated TLS slot or table. A source-built section with a raw prefix and zero-filled tail can cover both cases.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7088909813 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/744#issuecomment-7088909813). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T01:08:43Z. --- Please keep section membership separate from file-backed bytes. An RVA can be inside the virtual section and still have no payload in the file. Prove that the entire requested field is file-backed before unpacking it, and give a clear error for a truncated TLS slot or table. A source-built section with a raw prefix and zero-filled tail can cover both cases.
R0Y00 commented 2026-11-24 06:48:56 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T01:52:01Z.


#760 is the merge candidate for this issue. I pushed 5b7b7b5 to match pe_map when a section has no raw-data pointer. The targeted source-built TLS check passed locally, including the virtual tail, the missing raw pointer, the section boundary and the unterminated table. The new Windows CI runs are still required.

The inspector checks the file-backed prefix separately and only supplies zeroes where the mapped section has no file backing. Thanks to both authors. #759 independently covered the same behavior and caught the missing raw-pointer case. Please hold further duplicate changes while #760 is verified.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7089477433 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/744#issuecomment-7089477433). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T01:52:01Z. --- #760 is the merge candidate for this issue. I pushed 5b7b7b5 to match pe_map when a section has no raw-data pointer. The targeted source-built TLS check passed locally, including the virtual tail, the missing raw pointer, the section boundary and the unterminated table. The new Windows CI runs are still required. The inspector checks the file-backed prefix separately and only supplies zeroes where the mapped section has no file backing. Thanks to both authors. #759 independently covered the same behavior and caught the missing raw-pointer case. Please hold further duplicate changes while #760 is verified.
Sign in to join this conversation.
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#738
No description provided.