diff options
| author | Dominik <55330408+Dom-in@users.noreply.github.com> | 2026-11-22 15:22:13 +0000 |
|---|---|---|
| committer | Dominik <55330408+Dom-in@users.noreply.github.com> | 2026-11-22 16:43:32 +0000 |
| commit | 83d973a424afa3a5c21a52683154b770e4702065 (patch) | |
| tree | 2c4142b2485b169b46063c5a0562cc705110e830 /runtime/src/pe_loader.cpp | |
| parent | 49475796c226c8511b9470bf97a4daf57b8af323 (diff) | |
pe_loader: read the TLS directory through directory(), honouring NumberOfRvaAndSizes
Every data directory the loader reads goes through directory(), which
returns an empty entry past OptionalHeader.NumberOfRvaAndSizes (PE/COFF: those
slots are not directories, whatever bytes they hold) - except the TLS one,
which read DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] directly. An image
declaring fewer than 10 directories with leftover bytes in slot 9 then failed
to load ("the TLS directory runs past the image") or, with plausible bytes,
had a TLS directory the Windows loader would ignore.
loader_test: with NumberOfRvaAndSizes = 9 and junk in the TLS slot the image
loads (it was rejected before); with all 16 the same slot is still checked
and refused.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index bdb3df6f5..b78053d01 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -64,7 +64,8 @@ uint64_t rva_in(const Module& m, uint64_t va, const char* what) { | |||
| 64 | 64 | ||
| 65 | IMAGE_TLS_DIRECTORY64* tls_directory(Module& m) { | 65 | IMAGE_TLS_DIRECTORY64* tls_directory(Module& m) { |
| 66 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); | 66 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); |
| 67 | const auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS]; | 67 | // Through directory(), as every other directory: an entry past NumberOfRvaAndSizes is not a TLS directory. |
| 68 | const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_TLS); | ||
| 68 | if (!dir.Size || !dir.VirtualAddress) return nullptr; | 69 | if (!dir.Size || !dir.VirtualAddress) return nullptr; |
| 69 | require_range(m.name, dir.VirtualAddress, sizeof(IMAGE_TLS_DIRECTORY64), m.size, "the TLS directory"); | 70 | require_range(m.name, dir.VirtualAddress, sizeof(IMAGE_TLS_DIRECTORY64), m.size, "the TLS directory"); |
| 70 | return at<IMAGE_TLS_DIRECTORY64>(m.base, dir.VirtualAddress); | 71 | return at<IMAGE_TLS_DIRECTORY64>(m.base, dir.VirtualAddress); |