diff options
| author | Dominik <55330408+Dom-in@users.noreply.github.com> | 2026-11-22 15:19:53 +0000 |
|---|---|---|
| committer | Dominik <55330408+Dom-in@users.noreply.github.com> | 2026-11-22 15:26:21 +0000 |
| commit | b805f191da65088ca3831fd5f4360e603670aef8 (patch) | |
| tree | 73f3de5103a17c7b8acd439a23cf7ea16403e144 /runtime/test/loader_test.cpp | |
| parent | 4eb6155d3ac4f9d3fadb0d095c4ce13e76783615 (diff) | |
pe_loader: refuse an export whose address is outside the image
parse_exports range-checks the export directory, its three tables and every
name, but stored each code export as base + rva without checking rva. An
address-table entry at or past SizeOfImage (and outside the export
directory, so not a forwarder) became a pointer past the mapping, which an
import or GetProcAddress through the shims would hand out and a caller would
jump to. The load now fails with the export's name, like every other RVA the
loader follows.
loader_test: an export at SizeOfImage is rejected (it loaded before this
change); one at the image's last byte still loads.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/loader_test.cpp')
| -rw-r--r-- | runtime/test/loader_test.cpp | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp index fd38434c6..2e4a32aa8 100644 --- a/runtime/test/loader_test.cpp +++ b/runtime/test/loader_test.cpp | |||
| @@ -977,6 +977,19 @@ void test_unwind_registration() { | |||
| 977 | expect_rejects("unwind data past the image", make_unwind_image({{0x1000, 0x1010, 0x1FFE}})); | 977 | expect_rejects("unwind data past the image", make_unwind_image({{0x1000, 0x1010, 0x1FFE}})); |
| 978 | } | 978 | } |
| 979 | 979 | ||
| 980 | // An export whose address-table entry is past SizeOfImage (not a forwarder: outside the export directory) is | ||
| 981 | // refused instead of being stored as base + rva, a pointer outside the mapping. | ||
| 982 | void test_export_bounds() { | ||
| 983 | auto outside = make_forwarder_image("KERNEL32.GetCurrentProcessId", 0x180000000); | ||
| 984 | nt_of(outside)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size = sizeof(IMAGE_EXPORT_DIRECTORY); | ||
| 985 | *reinterpret_cast<uint32_t*>(outside.data() + 0x300) = 0x2000; // funcs[0] == SizeOfImage | ||
| 986 | expect_rejects("an export whose address is past the image", outside); | ||
| 987 | auto last = make_forwarder_image("KERNEL32.GetCurrentProcessId", 0x180000000); | ||
| 988 | nt_of(last)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size = sizeof(IMAGE_EXPORT_DIRECTORY); | ||
| 989 | *reinterpret_cast<uint32_t*>(last.data() + 0x300) = 0x1FFF; // the image's last byte: still inside | ||
| 990 | expect_loads("an export at the image's last byte still loads", last); | ||
| 991 | } | ||
| 992 | |||
| 980 | // The folder the images go to: leonida_loader_test\<process id> in LEONIDA_TEST_DIR when that is set, otherwise in | 993 | // The folder the images go to: leonida_loader_test\<process id> in LEONIDA_TEST_DIR when that is set, otherwise in |
| 981 | // the temporary directory. A folder per run, because security software can hold the previous run's images for a | 994 | // the temporary directory. A folder per run, because security software can hold the previous run's images for a |
| 982 | // while after they are written (so they can be neither deleted nor overwritten); the fixed parent name is what an | 995 | // while after they are written (so they can be neither deleted nor overwritten); the fixed parent name is what an |
| @@ -1066,6 +1079,7 @@ int main() { | |||
| 1066 | test_process_detach_dependencies(); | 1079 | test_process_detach_dependencies(); |
| 1067 | test_delay_descriptor_fields(); | 1080 | test_delay_descriptor_fields(); |
| 1068 | test_forwarded_exports(); | 1081 | test_forwarded_exports(); |
| 1082 | test_export_bounds(); | ||
| 1069 | test_unwind_registration(); | 1083 | test_unwind_registration(); |
| 1070 | } catch (const std::exception& e) { | 1084 | } catch (const std::exception& e) { |
| 1071 | // A write that fails (write_image throws) ends the run with the reason, instead of terminating the process. | 1085 | // A write that fails (write_image throws) ends the run with the reason, instead of terminating the process. |