pe: reject TLS images with an unwritable AddressOfIndex #537

Merged
yummancito merged 1 commit from upstream-pr/543 into main 2026-11-22 16:32:19 +00:00
yummancito commented 2026-11-22 10:19:52 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by yummancito on 2026-11-22T10:19:52Z.


Bug

pe_map never validated the TLS directory's AddressOfIndex. Any image with a bogus index slot mapped successfully, while the Windows loader fails it: the loader writes the TLS index through AddressOfIndex, so the slot must be writable.

Measured on Windows 11 (source-built cpu_tls_original fixture, patched copies loaded with LoadLibraryExW):

  • index past the image / null / garbage VA -> fails with ERROR_NOACCESS (998)
  • index in headers, .text, .rdata or .tlscb (in-image but not writable) -> fails with 998
  • index at another DWORD in writable .data (even unaligned, .data+4) -> loads, TLS callback and entry point run

pe_map mapped all of the failing cases (no read of the field anywhere; only comment was "pe_map leaves AddressOfIndex as linked").

Fix (tools/pe_map.cpp)

  • New writable_section(nt, rva, size) helper, mirroring executable(): the full [rva, rva+size) must lie in one IMAGE_SCN_MEM_WRITE section (DWORD-safe bounds, no overflow: size <= span guards span - size, 64-bit RVA arithmetic).
  • In tls_callbacks(), whenever a TLS directory is present (even with a null callback list, since the loader assigns the index even for an empty template): reject with MAP_FORMAT/"TLS index outside image" if the slot is not a DWORD inside the image, or "TLS index outside writable section" if it is not in a writable section. Checked after section protections, so it holds on map, release and thread notify paths like the other TLS validations.

Tests (tests/cpu/native_tls_check.py, no proprietary binaries)

  • Two rejection controls (no notifications, imports bound first like the other late checks): index past the image, index in read-only .rdata.
  • One acceptance control guarding against false rejections: alternate writable index (.data+4, unaligned) loads under both loaders with TLS callback + entry point attach events.

Evidence (measured, not inspection)

  • Negative proof: branch with the new tests but the fix reverted -> cpu_native_tls fails in CI with TLS index outside image control: base=<mapped> status=0 (old code maps the malformed image). Throwaway branch, deleted.
  • Fix branch exp/tls-dir-validation @ d39095d (rebased on upstream/main@aba0555, includes #523): run 45011225426 -> success, 205/205 in Development and 205/205 at /O2, cpu_native_tls Passed both.
  • Local native-loader measurements: bogus index -> 998; .data+4 -> loads with [(TLS,ATTACH),(ENTRY,ATTACH)] + matching detaches.

Limitations

  • No alignment requirement imposed (Windows accepts unaligned slots).
  • A DWORD straddling a writable-section boundary is rejected; linkers never emit that for the index slot.
  • The null-callback-list + bogus-index combination is rejected by the same check but has no dedicated fixture (reasoning: the loader writes the index regardless of callbacks).
  • Refs #165.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6541212853 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/543). Originally posted by [yummancito](https://github.com/yummancito) on 2026-11-22T10:19:52Z. --- ## Bug `pe_map` never validated the TLS directory's `AddressOfIndex`. Any image with a bogus index slot mapped successfully, while the Windows loader fails it: the loader writes the TLS index through `AddressOfIndex`, so the slot must be writable. Measured on Windows 11 (source-built `cpu_tls_original` fixture, patched copies loaded with `LoadLibraryExW`): - index past the image / null / garbage VA -> fails with `ERROR_NOACCESS` (998) - index in headers, `.text`, `.rdata` or `.tlscb` (in-image but not writable) -> fails with 998 - index at another DWORD in writable `.data` (even unaligned, `.data+4`) -> loads, TLS callback and entry point run `pe_map` mapped all of the failing cases (no read of the field anywhere; only comment was "pe_map leaves AddressOfIndex as linked"). ## Fix (`tools/pe_map.cpp`) - New `writable_section(nt, rva, size)` helper, mirroring `executable()`: the full `[rva, rva+size)` must lie in one `IMAGE_SCN_MEM_WRITE` section (DWORD-safe bounds, no overflow: `size <= span` guards `span - size`, 64-bit RVA arithmetic). - In `tls_callbacks()`, whenever a TLS directory is present (even with a null callback list, since the loader assigns the index even for an empty template): reject with `MAP_FORMAT`/`"TLS index outside image"` if the slot is not a DWORD inside the image, or `"TLS index outside writable section"` if it is not in a writable section. Checked after section protections, so it holds on map, release and thread notify paths like the other TLS validations. ## Tests (`tests/cpu/native_tls_check.py`, no proprietary binaries) - Two rejection controls (no notifications, imports bound first like the other late checks): index past the image, index in read-only `.rdata`. - One acceptance control guarding against false rejections: alternate writable index (`.data+4`, unaligned) loads under **both** loaders with TLS callback + entry point attach events. ## Evidence (measured, not inspection) - Negative proof: branch with the new tests but the fix reverted -> `cpu_native_tls` **fails** in CI with `TLS index outside image control: base=<mapped> status=0` (old code maps the malformed image). Throwaway branch, deleted. - Fix branch `exp/tls-dir-validation` @ `d39095d` (rebased on `upstream/main@aba0555`, includes #523): run **45011225426** -> **success**, **205/205** in Development and **205/205** at /O2, `cpu_native_tls` Passed both. - Local native-loader measurements: bogus index -> 998; `.data+4` -> loads with `[(TLS,ATTACH),(ENTRY,ATTACH)]` + matching detaches. ## Limitations - No alignment requirement imposed (Windows accepts unaligned slots). - A DWORD straddling a writable-section boundary is rejected; linkers never emit that for the index slot. - The null-callback-list + bogus-index combination is rejected by the same check but has no dedicated fixture (reasoning: the loader writes the index regardless of callbacks). - Refs #165.
upstream-sync closed this pull request 2026-11-24 06:43:36 +00:00
legacy-code-archaeologist commented 2026-11-24 06:48:35 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-22T15:55:38Z.


Verification of d39095d on Windows 11 build 26100.9168 (x64, Intel Skylake), MSVC 19.52.37029, dev preset. It merges cleanly with main at f8d11ad.

ctest --preset dev --no-tests=error -R "^cpu_native_" -j 4
100% tests passed, 0 tests failed out of 25

cpu_native_tls printed the new controls: "a TLS index past the image and a TLS index in read-only .rdata", and the .data+4 alternate "Windows and pe_map run the TLS callback and the entry point".

The test asserts that Windows refuses only the .data+4 positive case. The PR text gives all the other cases from a measurement on a Windows 11 build. I measured all nine on 26100.9168 against both loaders:

  • Copies: AddressOfIndex patched in the built cpu_tls_original.dll.
  • Native: LoadLibraryExW(..., LOAD_WITH_ALTERED_SEARCH_PATH), one child process per copy.
  • pe_map: pe_map.dll through the test's own Mapper.
AddressOfIndex Windows 26100.9168 pe_map at d39095d
unchanged (as linked) loads maps, notifications sent
null fails, 998 rejected, "TLS index outside image"
past the image fails, 998 rejected, "TLS index outside image"
garbage VA 0x123456780000 fails, 998 rejected, "TLS index outside image"
headers (base+0x40) fails, 998 rejected, "TLS index outside writable section"
.text fails, 998 rejected, "TLS index outside writable section"
.rdata fails, 998 rejected, "TLS index outside writable section"
.tlscb fails, 998 rejected, "TLS index outside writable section"
.data+4 (unaligned) loads maps, notifications sent

998 is ERROR_NOACCESS. In every rejected case pe_map sent no notifications. The two loaders agree on all nine cases, which matches the PR's own measurement on this Windows build. Five of the seven failing cases (null, garbage VA, headers, .text, .tlscb) are not yet among the test's controls, if you want them pinned.

AI-assisted: run and summarized with Claude Code; outputs copied from the local runs.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7081679346 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/543#issuecomment-7081679346). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-22T15:55:38Z. --- Verification of `d39095d` on Windows 11 build 26100.9168 (x64, Intel Skylake), MSVC 19.52.37029, `dev` preset. It merges cleanly with `main` at `f8d11ad`. ```text ctest --preset dev --no-tests=error -R "^cpu_native_" -j 4 100% tests passed, 0 tests failed out of 25 ``` `cpu_native_tls` printed the new controls: "a TLS index past the image and a TLS index in read-only .rdata", and the `.data+4` alternate "Windows and pe_map run the TLS callback and the entry point". The test asserts that Windows refuses only the `.data+4` positive case. The PR text gives all the other cases from a measurement on a Windows 11 build. I measured all nine on 26100.9168 against both loaders: - **Copies:** `AddressOfIndex` patched in the built `cpu_tls_original.dll`. - **Native:** `LoadLibraryExW(..., LOAD_WITH_ALTERED_SEARCH_PATH)`, one child process per copy. - **pe_map:** `pe_map.dll` through the test's own `Mapper`. | `AddressOfIndex` | Windows 26100.9168 | `pe_map` at `d39095d` | |---|---|---| | unchanged (as linked) | loads | maps, notifications sent | | null | fails, 998 | rejected, "TLS index outside image" | | past the image | fails, 998 | rejected, "TLS index outside image" | | garbage VA `0x123456780000` | fails, 998 | rejected, "TLS index outside image" | | headers (`base+0x40`) | fails, 998 | rejected, "TLS index outside writable section" | | `.text` | fails, 998 | rejected, "TLS index outside writable section" | | `.rdata` | fails, 998 | rejected, "TLS index outside writable section" | | `.tlscb` | fails, 998 | rejected, "TLS index outside writable section" | | `.data+4` (unaligned) | loads | maps, notifications sent | 998 is `ERROR_NOACCESS`. In every rejected case `pe_map` sent no notifications. The two loaders agree on all nine cases, which matches the PR's own measurement on this Windows build. Five of the seven failing cases (null, garbage VA, headers, `.text`, `.tlscb`) are not yet among the test's controls, if you want them pinned. AI-assisted: run and summarized with Claude Code; outputs copied from the local runs.
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!537
No description provided.