Reject trailing relocation bytes, odd block sizes and unaligned .pdata directories in pe_map #121
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/122"
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 PrivacyOnion on 2026-11-21T16:38:49Z.
Change
pe_map_imagewalked base-relocation blocks only whileoffset + sizeof(IMAGE_BASE_RELOCATION) <= relocs.Sizeand accepted anySizeOfBlock >= sizeof(IMAGE_BASE_RELOCATION)within the remaining directory bytes. As a result:sizeof(IMAGE_BASE_RELOCATION)(or aSizeof 1..7 bytes) stopped the loop early without failing;SizeOfBlockwas not a multiple ofsizeof(WORD)silently ignored the trailing odd byte.For
.pdataregistration,pe_map_imagecheckedpdata.Sizeonly and passedpdata.Size / sizeof(RUNTIME_FUNCTION)toimage.atandRtlAddFunctionTable. A non-zeroVirtualAddresswithSize == 0, or aSizenot divisible bysizeof(RUNTIME_FUNCTION)(12bytes, including1..11bytes where integer division yields0entries), was not rejected.pe_map_imagenow requireslength % sizeof(WORD) == 0for each relocation block, requiresoffset == relocs.Sizeafter walking the relocation directory, and rejects any present.pdatadirectory (VirtualAddress || Size) whoseSizeis zero or not a multiple ofsizeof(RUNTIME_FUNCTION).Verification
Verified on Windows 11 with VS Build Tools 2022 17.14 (
MSVC 19.44.35228.0), Windows SDK10.0.26100.0, Python3.12:cpu_native_map: passed (0.95s), including negative controls for a 4-byte truncated relocation directory, an oddSizeOfBlock(orig_block_len - 1), trailing bytes after a valid relocation block (relocs.Size + 4), a.pdatadirectory withVirtualAddress != 0andSize == 0, and a.pdatadirectory withSize == pdata.Size + 4.cpu_native_seh: passed (0.57s).Evidence and follow-up
All negative controls are in-memory byte patches applied to temporary copies of the source-built
cpu_import_original,cpu_ordinal_originalandcpu_reloc_originalfixtures at test time.Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T17:16:21Z.
Please sync these mapper checks on current main and compare the proposed malformed-directory rejections with the Windows loader, documenting any intentionally stricter rule. Keep the relocation-free fixture in #115 working.
Imported from R0Y00/Xbox2Windows. Originally posted by PrivacyOnion on 2026-11-21T17:44:15Z.
Rebased onto
mainand added a direct side-by-side comparison (check_reloc_and_pdata_bounds) againstLoadLibraryExWandRtlLookupFunctionEntry, documenting both shared behavior and intentionally stricter rules intools/pe_map.cpp:VirtualAddress == 0 || Size == 0):pe_maptreats an empty relocation directory as a no-op whenIMAGE_FILE_RELOCS_STRIPPEDis clear, keeping relocation-free fixtures (cpu_import_originalin #115) working.1..7bytes remaining after all blocks, e.g.Size = 4orSize = built + 4, orSizeOfBlock = built - 1withSize = built):LoadLibraryExW(ERROR_NOACCESS = 998) andpe_map(MAP_RELOCATION,"bad relocation block") reject the image.SizeOfBlockmatchingrelocs.Size(SizeOfBlock = built - 1,Size = built - 1):LoadLibraryExWloads the image by truncating(SizeOfBlock - 8) / 2;pe_mapis intentionally stricter and requires 2-byteWORDalignment (length % sizeof(WORD) == 0) for every relocation block..pdatasizes (Size = 0with non-zero RVA,Size = 11,Size = 20):LoadLibraryExWmaps the image without callingRtlAddFunctionTableand dividespdata.Sizebysizeof(RUNTIME_FUNCTION)(12) at lookup/unwind time (RtlLookupFunctionEntryfinds 0 entries forSize = 0and11, and 1 entry forSize = 20). Becausepe_mapregisters.pdataexplicitly viaRtlAddFunctionTable, it is intentionally stricter and rejects any non-empty exception directory whoseSizeis zero or not a multiple ofsizeof(RUNTIME_FUNCTION)(MAP_FUNCTION_TABLE,"cannot register .pdata").Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T17:57:53Z.
Thanks for adding the Windows loader comparison. The required Windows job still fails in cpu_native_map on the reloc Size=4 case. LoadLibraryExW returned a module and err=0 on the runner. Please check whether the image actually needed relocation during this comparison and make the test distinguish the two conditions. Please rerun cpu_native_map and cpu_native_reloc_free on Windows. Failing job
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T06:32:43Z.
I integrated current main and the agreed directory rules at
01e2539. Empty .pdata registers nothing, and a nonempty table must consist of complete in-image RUNTIME_FUNCTION entries. The comparison records native loader observations with the Windows build; the mapper assertions independently enforce the bounds. cpu_native_map and cpu_native_reloc_free both passed locally on Windows. CI is now testing this head.