diff options
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index ff0693a15..c29a288a9 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -1527,6 +1527,11 @@ void Loader::initialize_tls(Module& m) { | |||
| 1527 | if (tls->AddressOfIndex) { | 1527 | if (tls->AddressOfIndex) { |
| 1528 | index_rva = rva_in(m, tls->AddressOfIndex, "the TLS index"); | 1528 | index_rva = rva_in(m, tls->AddressOfIndex, "the TLS index"); |
| 1529 | require_range(m.name, index_rva, sizeof(uint32_t), m.size, "the TLS index"); | 1529 | require_range(m.name, index_rva, sizeof(uint32_t), m.size, "the TLS index"); |
| 1530 | // The index is written below, and every later step reads the headers back from the mapping. An index at | ||
| 1531 | // RVA 0 would not be written at all (index_rva doubles as "no index"). | ||
| 1532 | const auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); | ||
| 1533 | if (index_rva < nt->OptionalHeader.SizeOfHeaders) | ||
| 1534 | throw std::runtime_error(m.name + ": the TLS index lands in the headers"); | ||
| 1530 | } | 1535 | } |
| 1531 | 1536 | ||
| 1532 | if (!module_tls_slots()) throw std::runtime_error(m.name + ": this thread has no TLS array"); | 1537 | if (!module_tls_slots()) throw std::runtime_error(m.name + ": this thread has no TLS array"); |
| @@ -1786,6 +1791,9 @@ void* Loader::resolve(Module& m, const std::string& dll, const std::string& name | |||
| 1786 | 1791 | ||
| 1787 | void Loader::bind_imports(Module& m) { | 1792 | void Loader::bind_imports(Module& m) { |
| 1788 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); | 1793 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); |
| 1794 | // Read before the first address is written: an entry in the headers would replace what the steps after this | ||
| 1795 | // one read back from the mapping (e_lfanew, the section table), so it is refused like a section there. | ||
| 1796 | const uint64_t headers_end = nt->OptionalHeader.SizeOfHeaders; | ||
| 1789 | 1797 | ||
| 1790 | // Thunk tables have no length field: they end at a zero entry, so each step is checked against the image. | 1798 | // Thunk tables have no length field: they end at a zero entry, so each step is checked against the image. |
| 1791 | auto bind_thunks = [&](const std::string& dll, uint64_t lookup_rva, uint64_t iat_rva) { | 1799 | auto bind_thunks = [&](const std::string& dll, uint64_t lookup_rva, uint64_t iat_rva) { |
| @@ -1802,6 +1810,8 @@ void Loader::bind_imports(Module& m) { | |||
| 1802 | uint32_t hint = static_cast<uint32_t>(lookup->u1.AddressOfData); | 1810 | uint32_t hint = static_cast<uint32_t>(lookup->u1.AddressOfData); |
| 1803 | name = read_string(m.name, m.base, m.size, hint + sizeof(uint16_t), "an import name"); | 1811 | name = read_string(m.name, m.base, m.size, hint + sizeof(uint16_t), "an import name"); |
| 1804 | } | 1812 | } |
| 1813 | if (iat_rva < headers_end) | ||
| 1814 | throw std::runtime_error(m.name + ": an import address table lands in the headers"); | ||
| 1805 | at<IMAGE_THUNK_DATA64>(m.base, static_cast<uint32_t>(iat_rva))->u1.Function = | 1815 | at<IMAGE_THUNK_DATA64>(m.base, static_cast<uint32_t>(iat_rva))->u1.Function = |
| 1806 | reinterpret_cast<uint64_t>(resolve(m, dll, name, ord, by_ord)); | 1816 | reinterpret_cast<uint64_t>(resolve(m, dll, name, ord, by_ord)); |
| 1807 | } | 1817 | } |