pe: reject TLS images with an unwritable AddressOfIndex #537
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/543"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by yummancito on 2026-11-22T10:19:52Z.
Bug
pe_mapnever validated the TLS directory'sAddressOfIndex. Any image with a bogus index slot mapped successfully, while the Windows loader fails it: the loader writes the TLS index throughAddressOfIndex, so the slot must be writable.Measured on Windows 11 (source-built
cpu_tls_originalfixture, patched copies loaded withLoadLibraryExW):ERROR_NOACCESS(998).text,.rdataor.tlscb(in-image but not writable) -> fails with 998.data(even unaligned,.data+4) -> loads, TLS callback and entry point runpe_mapmapped 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)writable_section(nt, rva, size)helper, mirroringexecutable(): the full[rva, rva+size)must lie in oneIMAGE_SCN_MEM_WRITEsection (DWORD-safe bounds, no overflow:size <= spanguardsspan - size, 64-bit RVA arithmetic).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 withMAP_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).rdata..data+4, unaligned) loads under both loaders with TLS callback + entry point attach events.Evidence (measured, not inspection)
cpu_native_tlsfails in CI withTLS index outside image control: base=<mapped> status=0(old code maps the malformed image). Throwaway branch, deleted.exp/tls-dir-validation@d39095d(rebased onupstream/main@aba0555, includes #523): run 45011225426 -> success, 205/205 in Development and 205/205 at /O2,cpu_native_tlsPassed both..data+4-> loads with[(TLS,ATTACH),(ENTRY,ATTACH)]+ matching detaches.Limitations
Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-22T15:55:38Z.
Verification of
d39095don Windows 11 build 26100.9168 (x64, Intel Skylake), MSVC 19.52.37029,devpreset. It merges cleanly withmainatf8d11ad.cpu_native_tlsprinted the new controls: "a TLS index past the image and a TLS index in read-only .rdata", and the.data+4alternate "Windows and pe_map run the TLS callback and the entry point".The test asserts that Windows refuses only the
.data+4positive 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:AddressOfIndexpatched in the builtcpu_tls_original.dll.LoadLibraryExW(..., LOAD_WITH_ALTERED_SEARCH_PATH), one child process per copy.pe_map.dllthrough the test's ownMapper.AddressOfIndexpe_mapatd39095d0x123456780000base+0x40).text.rdata.tlscb.data+4(unaligned)998 is
ERROR_NOACCESS. In every rejected casepe_mapsent 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.