Repository navigation
pe_loader: honour NumberOfRvaAndSizes, and refuse a forwarder ordinal past 65535 - #1126
Merged
Merged
Conversation
… past 65535 Data directories past NumberOfRvaAndSizes were read anyway (only the exception directory checked the count), so stale bytes there were parsed as import, delay-import, export or relocation tables. And resolve_forwarder range-checked the ordinal before adding each digit, so '#65537' passed and was cast to ordinal 1. The hand-built test images now declare their 16 directories.
Contributor
|
Tested 236b0ac on Windows 11, MSVC 14.51: pass. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two header-validation gaps in
runtime/src/pe_loader.cpp:NumberOfRvaAndSizes: PE/COFF defines it as the number of data-directory entries in the rest of the optional header, so entries past it are not directories. Onlyregister_unwind(the exception directory) checked it. The export, import, delay-import and base-relocation directories were read fromDataDirectory[i]whatever the count, so stale bytes there were parsed as tables (or applied as relocations). A newdirectory(nt, index)helper returns an empty entry past the count and is used at those sites, replacing the special case inregister_unwind.tls_directory()is left alone because RT-25: loader TLS on every thread and MSVC C++ exceptions in mapped images #965 rewrites it.resolve_forwardercheckedordinal > 0xFFFFbefore adding each digit, so the last digit was never checked.kernel32.#65537passed the check,static_cast<WORD>turned it into 1, and the import was silently bound to kernel32's ordinal 1. The check now runs after each digit. Ordinals are 16-bit, so such a forwarder names nothing and stays unimplemented.mainNumberOfRvaAndSizes= 1, junk in the import/delay slotskernel32.#65537Host)UnimplementedTests:
test_directory_countandtest_forwarder_ordinal_rangeinruntime/test/test_pe_loader_bounds.cpp. Withmain'spe_loader.cppboth fail:FAILED (44 checks, 2 failures). With this change:ALL 44 CHECKS PASSED. The hand-built images inloader_test.cppandunimplemented_policy_test.cppleft the count at 0 and only worked because of gap 1, so they now setIMAGE_NUMBEROF_DIRECTORY_ENTRIES.make_base_imagein the bounds test already did, and the other tests read linker-built images. Fullcteston Windows 11, RTX 5070, VS 2022 Build Tools, withLEONIDA_VK_VALIDATION=1andLEONIDA_D3D12=vulkan: 54/54.AI-assisted.