diff options
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 112 |
1 files changed, 90 insertions, 22 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index f27060b94..e6a9501ab 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -27,6 +27,21 @@ std::vector<uint8_t> read_file(const std::filesystem::path& p) { | |||
| 27 | template <class T> | 27 | template <class T> |
| 28 | T* at(uint8_t* base, uint32_t rva) { return reinterpret_cast<T*>(base + rva); } | 28 | T* at(uint8_t* base, uint32_t rva) { return reinterpret_cast<T*>(base + rva); } |
| 29 | 29 | ||
| 30 | // Every RVA followed below comes out of the image itself, so each one is checked first. A truncated or corrupt | ||
| 31 | // image then fails with a message instead of reading, writing or protecting memory outside its allocation. | ||
| 32 | void require_range(const std::string& name, uint64_t rva, uint64_t len, uint32_t image_size, const char* what) { | ||
| 33 | if (rva > image_size || len > image_size - rva) | ||
| 34 | throw std::runtime_error(name + ": " + what + " runs past the image"); | ||
| 35 | } | ||
| 36 | |||
| 37 | std::string read_string(const std::string& name, uint8_t* base, uint32_t image_size, uint32_t rva, const char* what) { | ||
| 38 | if (rva >= image_size) throw std::runtime_error(name + ": " + what + " is outside the image"); | ||
| 39 | auto* s = reinterpret_cast<const char*>(base + rva); | ||
| 40 | auto* end = static_cast<const char*>(std::memchr(s, 0, image_size - rva)); | ||
| 41 | if (!end) throw std::runtime_error(name + ": " + what + " isn't terminated inside the image"); | ||
| 42 | return {s, end}; | ||
| 43 | } | ||
| 44 | |||
| 30 | // The Xbox builds of the MSVC runtime import the same CRT the desktop does, so the host's own copies are used. | 45 | // The Xbox builds of the MSVC runtime import the same CRT the desktop does, so the host's own copies are used. |
| 31 | bool prefer_host_copy(const std::string& dll) { | 46 | bool prefer_host_copy(const std::string& dll) { |
| 32 | static const char* names[] = {"vcruntime140.dll", "vcruntime140_1.dll", "msvcp140.dll", "msvcp140_1.dll", | 47 | static const char* names[] = {"vcruntime140.dll", "vcruntime140_1.dll", "msvcp140.dll", "msvcp140_1.dll", |
| @@ -57,16 +72,28 @@ Module& Loader::load(const std::filesystem::path& image) { | |||
| 57 | 72 | ||
| 58 | Module& Loader::map_image(const std::filesystem::path& path) { | 73 | Module& Loader::map_image(const std::filesystem::path& path) { |
| 59 | auto file = read_file(path); | 74 | auto file = read_file(path); |
| 75 | const std::string name = path.filename().string(); | ||
| 60 | auto* dos = reinterpret_cast<IMAGE_DOS_HEADER*>(file.data()); | 76 | auto* dos = reinterpret_cast<IMAGE_DOS_HEADER*>(file.data()); |
| 61 | if (file.size() < sizeof(IMAGE_DOS_HEADER) || dos->e_magic != IMAGE_DOS_SIGNATURE) | 77 | if (file.size() < sizeof(IMAGE_DOS_HEADER) || dos->e_magic != IMAGE_DOS_SIGNATURE) |
| 62 | throw std::runtime_error(path.string() + ": not an MZ image"); | 78 | throw std::runtime_error(path.string() + ": not an MZ image"); |
| 79 | if (dos->e_lfanew < 0 || static_cast<uint64_t>(dos->e_lfanew) + sizeof(IMAGE_NT_HEADERS64) > file.size()) | ||
| 80 | throw std::runtime_error(name + ": the NT headers are outside the file"); | ||
| 63 | auto* nt = reinterpret_cast<IMAGE_NT_HEADERS64*>(file.data() + dos->e_lfanew); | 81 | auto* nt = reinterpret_cast<IMAGE_NT_HEADERS64*>(file.data() + dos->e_lfanew); |
| 64 | if (nt->Signature != IMAGE_NT_SIGNATURE || nt->FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64) | 82 | if (nt->Signature != IMAGE_NT_SIGNATURE || nt->FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64) |
| 65 | throw std::runtime_error(path.string() + ": not an x86-64 PE"); | 83 | throw std::runtime_error(name + ": not an x86-64 PE"); |
| 84 | if (nt->FileHeader.SizeOfOptionalHeader < sizeof(IMAGE_OPTIONAL_HEADER64) || | ||
| 85 | nt->OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC) | ||
| 86 | throw std::runtime_error(name + ": not a PE32+ image"); | ||
| 66 | auto& oh = nt->OptionalHeader; | 87 | auto& oh = nt->OptionalHeader; |
| 88 | uint64_t sections = static_cast<uint64_t>(dos->e_lfanew) + 4 + sizeof(IMAGE_FILE_HEADER) + | ||
| 89 | nt->FileHeader.SizeOfOptionalHeader; | ||
| 90 | if (sections + static_cast<uint64_t>(nt->FileHeader.NumberOfSections) * sizeof(IMAGE_SECTION_HEADER) > file.size()) | ||
| 91 | throw std::runtime_error(name + ": the section table runs past the end of the file"); | ||
| 92 | if (!oh.SizeOfImage || oh.SizeOfHeaders > oh.SizeOfImage || oh.SizeOfHeaders > file.size()) | ||
| 93 | throw std::runtime_error(name + ": SizeOfHeaders and SizeOfImage don't fit the file"); | ||
| 67 | 94 | ||
| 68 | auto* m = new Module; | 95 | auto* m = new Module; |
| 69 | m->name = path.filename().string(); | 96 | m->name = name; |
| 70 | m->path = path; | 97 | m->path = path; |
| 71 | m->preferred_base = oh.ImageBase; | 98 | m->preferred_base = oh.ImageBase; |
| 72 | m->size = oh.SizeOfImage; | 99 | m->size = oh.SizeOfImage; |
| @@ -88,25 +115,36 @@ Module& Loader::map_image(const std::filesystem::path& path) { | |||
| 88 | auto* sec = IMAGE_FIRST_SECTION(nt); | 115 | auto* sec = IMAGE_FIRST_SECTION(nt); |
| 89 | for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i, ++sec) { | 116 | for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i, ++sec) { |
| 90 | uint32_t n = std::min(sec->SizeOfRawData, sec->Misc.VirtualSize ? sec->Misc.VirtualSize : sec->SizeOfRawData); | 117 | uint32_t n = std::min(sec->SizeOfRawData, sec->Misc.VirtualSize ? sec->Misc.VirtualSize : sec->SizeOfRawData); |
| 91 | if (n && sec->PointerToRawData + n <= file.size()) | 118 | if (!n) continue; |
| 92 | std::memcpy(m->base + sec->VirtualAddress, file.data() + sec->PointerToRawData, n); | 119 | if (sec->PointerToRawData > file.size() || n > file.size() - sec->PointerToRawData) |
| 120 | throw std::runtime_error(m->name + ": section " + std::to_string(i) + " reads past the end of the file"); | ||
| 121 | require_range(m->name, sec->VirtualAddress, n, m->size, "a section's data"); | ||
| 122 | std::memcpy(m->base + sec->VirtualAddress, file.data() + sec->PointerToRawData, n); | ||
| 93 | } | 123 | } |
| 94 | 124 | ||
| 95 | int64_t delta = reinterpret_cast<int64_t>(m->base) - static_cast<int64_t>(oh.ImageBase); | 125 | int64_t delta = reinterpret_cast<int64_t>(m->base) - static_cast<int64_t>(oh.ImageBase); |
| 96 | if (delta) { | 126 | if (delta) { |
| 97 | m->relocated = true; | 127 | m->relocated = true; |
| 98 | auto& dir = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC]; | 128 | auto& dir = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC]; |
| 129 | if (!dir.Size) throw std::runtime_error(m->name + ": the image has no relocations and isn't at its base"); | ||
| 130 | require_range(m->name, dir.VirtualAddress, dir.Size, m->size, "the relocation table"); | ||
| 99 | auto* block = at<IMAGE_BASE_RELOCATION>(m->base, dir.VirtualAddress); | 131 | auto* block = at<IMAGE_BASE_RELOCATION>(m->base, dir.VirtualAddress); |
| 100 | auto* end = reinterpret_cast<uint8_t*>(block) + dir.Size; | 132 | auto* end = reinterpret_cast<uint8_t*>(block) + dir.Size; |
| 101 | while (reinterpret_cast<uint8_t*>(block) < end && block->SizeOfBlock) { | 133 | while (reinterpret_cast<uint8_t*>(block) < end) { |
| 134 | if (block->SizeOfBlock < sizeof(IMAGE_BASE_RELOCATION) || | ||
| 135 | reinterpret_cast<uint8_t*>(block) + block->SizeOfBlock > end) | ||
| 136 | throw std::runtime_error(m->name + ": a relocation block doesn't fit the relocation table"); | ||
| 102 | auto* entries = reinterpret_cast<uint16_t*>(block + 1); | 137 | auto* entries = reinterpret_cast<uint16_t*>(block + 1); |
| 103 | size_t count = (block->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / 2; | 138 | size_t count = (block->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / 2; |
| 104 | for (size_t i = 0; i < count; ++i) { | 139 | for (size_t i = 0; i < count; ++i) { |
| 105 | uint16_t type = entries[i] >> 12, off = entries[i] & 0xFFF; | 140 | uint16_t type = entries[i] >> 12, off = entries[i] & 0xFFF; |
| 106 | if (type == IMAGE_REL_BASED_DIR64) | 141 | if (type == IMAGE_REL_BASED_DIR64) { |
| 142 | require_range(m->name, static_cast<uint64_t>(block->VirtualAddress) + off, sizeof(uint64_t), m->size, | ||
| 143 | "a relocation"); | ||
| 107 | *at<uint64_t>(m->base, block->VirtualAddress + off) += delta; | 144 | *at<uint64_t>(m->base, block->VirtualAddress + off) += delta; |
| 108 | else if (type != IMAGE_REL_BASED_ABSOLUTE) | 145 | } else if (type != IMAGE_REL_BASED_ABSOLUTE) { |
| 109 | throw std::runtime_error(m->name + ": unsupported relocation type " + std::to_string(type)); | 146 | throw std::runtime_error(m->name + ": unsupported relocation type " + std::to_string(type)); |
| 147 | } | ||
| 110 | } | 148 | } |
| 111 | block = reinterpret_cast<IMAGE_BASE_RELOCATION*>(reinterpret_cast<uint8_t*>(block) + block->SizeOfBlock); | 149 | block = reinterpret_cast<IMAGE_BASE_RELOCATION*>(reinterpret_cast<uint8_t*>(block) + block->SizeOfBlock); |
| 112 | } | 150 | } |
| @@ -119,14 +157,24 @@ void Loader::parse_exports(Module& m) { | |||
| 119 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); | 157 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); |
| 120 | auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]; | 158 | auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]; |
| 121 | if (!dir.Size) return; | 159 | if (!dir.Size) return; |
| 160 | require_range(m.name, dir.VirtualAddress, sizeof(IMAGE_EXPORT_DIRECTORY), m.size, "the export directory"); | ||
| 122 | auto* ed = at<IMAGE_EXPORT_DIRECTORY>(m.base, dir.VirtualAddress); | 161 | auto* ed = at<IMAGE_EXPORT_DIRECTORY>(m.base, dir.VirtualAddress); |
| 162 | require_range(m.name, ed->AddressOfFunctions, static_cast<uint64_t>(ed->NumberOfFunctions) * sizeof(uint32_t), m.size, | ||
| 163 | "the export address table"); | ||
| 164 | require_range(m.name, ed->AddressOfNames, static_cast<uint64_t>(ed->NumberOfNames) * sizeof(uint32_t), m.size, | ||
| 165 | "the export name table"); | ||
| 166 | require_range(m.name, ed->AddressOfNameOrdinals, static_cast<uint64_t>(ed->NumberOfNames) * sizeof(uint16_t), m.size, | ||
| 167 | "the export ordinal table"); | ||
| 123 | auto* funcs = at<uint32_t>(m.base, ed->AddressOfFunctions); | 168 | auto* funcs = at<uint32_t>(m.base, ed->AddressOfFunctions); |
| 124 | auto* names = at<uint32_t>(m.base, ed->AddressOfNames); | 169 | auto* names = at<uint32_t>(m.base, ed->AddressOfNames); |
| 125 | auto* ords = at<uint16_t>(m.base, ed->AddressOfNameOrdinals); | 170 | auto* ords = at<uint16_t>(m.base, ed->AddressOfNameOrdinals); |
| 126 | for (uint32_t i = 0; i < ed->NumberOfFunctions; ++i) | 171 | for (uint32_t i = 0; i < ed->NumberOfFunctions; ++i) |
| 127 | if (funcs[i]) m.exports["#" + std::to_string(ed->Base + i)] = m.base + funcs[i]; | 172 | if (funcs[i]) m.exports["#" + std::to_string(ed->Base + i)] = m.base + funcs[i]; |
| 128 | for (uint32_t i = 0; i < ed->NumberOfNames; ++i) | 173 | for (uint32_t i = 0; i < ed->NumberOfNames; ++i) { |
| 129 | m.exports[at<char>(m.base, names[i])] = m.base + funcs[ords[i]]; | 174 | if (ords[i] >= ed->NumberOfFunctions) |
| 175 | throw std::runtime_error(m.name + ": an export name points past the address table"); | ||
| 176 | m.exports[read_string(m.name, m.base, m.size, names[i], "an export name")] = m.base + funcs[ords[i]]; | ||
| 177 | } | ||
| 130 | // Forwarded exports (RVA inside the export directory) aren't followed yet; none of the bundled DLLs use them. | 178 | // Forwarded exports (RVA inside the export directory) aren't followed yet; none of the bundled DLLs use them. |
| 131 | } | 179 | } |
| 132 | 180 | ||
| @@ -162,30 +210,48 @@ void* Loader::resolve(Module& m, const std::string& dll, const std::string& name | |||
| 162 | void Loader::bind_imports(Module& m) { | 210 | void Loader::bind_imports(Module& m) { |
| 163 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); | 211 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); |
| 164 | 212 | ||
| 165 | auto bind_thunks = [&](const std::string& dll, IMAGE_THUNK_DATA64* lookup, IMAGE_THUNK_DATA64* iat) { | 213 | // Thunk tables have no length field: they end at a zero entry, so each step is checked against the image. |
| 166 | for (; lookup->u1.AddressOfData; ++lookup, ++iat) { | 214 | auto bind_thunks = [&](const std::string& dll, uint64_t lookup_rva, uint64_t iat_rva) { |
| 215 | for (;; lookup_rva += sizeof(IMAGE_THUNK_DATA64), iat_rva += sizeof(IMAGE_THUNK_DATA64)) { | ||
| 216 | require_range(m.name, lookup_rva, sizeof(IMAGE_THUNK_DATA64), m.size, "an import lookup table"); | ||
| 217 | require_range(m.name, iat_rva, sizeof(IMAGE_THUNK_DATA64), m.size, "an import address table"); | ||
| 218 | auto* lookup = at<IMAGE_THUNK_DATA64>(m.base, static_cast<uint32_t>(lookup_rva)); | ||
| 219 | if (!lookup->u1.AddressOfData) return; | ||
| 167 | bool by_ord = IMAGE_SNAP_BY_ORDINAL64(lookup->u1.Ordinal); | 220 | bool by_ord = IMAGE_SNAP_BY_ORDINAL64(lookup->u1.Ordinal); |
| 168 | uint16_t ord = by_ord ? (uint16_t)IMAGE_ORDINAL64(lookup->u1.Ordinal) : 0; | 221 | uint16_t ord = by_ord ? (uint16_t)IMAGE_ORDINAL64(lookup->u1.Ordinal) : 0; |
| 169 | std::string name = by_ord ? "" : at<IMAGE_IMPORT_BY_NAME>(m.base, (uint32_t)lookup->u1.AddressOfData)->Name; | 222 | std::string name; |
| 170 | iat->u1.Function = reinterpret_cast<uint64_t>(resolve(m, dll, name, ord, by_ord)); | 223 | if (!by_ord) { |
| 224 | uint32_t hint = static_cast<uint32_t>(lookup->u1.AddressOfData); | ||
| 225 | require_range(m.name, hint, sizeof(uint16_t), m.size, "an import name"); | ||
| 226 | name = read_string(m.name, m.base, m.size, hint + sizeof(uint16_t), "an import name"); | ||
| 227 | } | ||
| 228 | at<IMAGE_THUNK_DATA64>(m.base, static_cast<uint32_t>(iat_rva))->u1.Function = | ||
| 229 | reinterpret_cast<uint64_t>(resolve(m, dll, name, ord, by_ord)); | ||
| 171 | } | 230 | } |
| 172 | }; | 231 | }; |
| 173 | 232 | ||
| 174 | auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]; | 233 | auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]; |
| 175 | if (dir.Size) { | 234 | if (dir.Size) { |
| 176 | for (auto* d = at<IMAGE_IMPORT_DESCRIPTOR>(m.base, dir.VirtualAddress); d->Name; ++d) { | 235 | for (uint64_t i = 0;; ++i) { |
| 177 | std::string dll = at<char>(m.base, d->Name); | 236 | uint64_t rva = dir.VirtualAddress + i * sizeof(IMAGE_IMPORT_DESCRIPTOR); |
| 237 | require_range(m.name, rva, sizeof(IMAGE_IMPORT_DESCRIPTOR), m.size, "the import table"); | ||
| 238 | auto* d = at<IMAGE_IMPORT_DESCRIPTOR>(m.base, static_cast<uint32_t>(rva)); | ||
| 239 | if (!d->Name) break; | ||
| 240 | std::string dll = read_string(m.name, m.base, m.size, d->Name, "an imported DLL name"); | ||
| 178 | uint32_t lookup = d->OriginalFirstThunk ? d->OriginalFirstThunk : d->FirstThunk; | 241 | uint32_t lookup = d->OriginalFirstThunk ? d->OriginalFirstThunk : d->FirstThunk; |
| 179 | bind_thunks(dll, at<IMAGE_THUNK_DATA64>(m.base, lookup), at<IMAGE_THUNK_DATA64>(m.base, d->FirstThunk)); | 242 | bind_thunks(dll, lookup, d->FirstThunk); |
| 180 | } | 243 | } |
| 181 | } | 244 | } |
| 182 | // Delay imports are bound eagerly: simpler, and the full import surface shows up in --dry-run. | 245 | // Delay imports are bound eagerly: simpler, and the full import surface shows up in --dry-run. |
| 183 | auto& ddir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT]; | 246 | auto& ddir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT]; |
| 184 | if (ddir.Size) { | 247 | if (ddir.Size) { |
| 185 | for (auto* d = at<IMAGE_DELAYLOAD_DESCRIPTOR>(m.base, ddir.VirtualAddress); d->DllNameRVA; ++d) { | 248 | for (uint64_t i = 0;; ++i) { |
| 186 | std::string dll = at<char>(m.base, d->DllNameRVA); | 249 | uint64_t rva = ddir.VirtualAddress + i * sizeof(IMAGE_DELAYLOAD_DESCRIPTOR); |
| 187 | bind_thunks(dll, at<IMAGE_THUNK_DATA64>(m.base, d->ImportNameTableRVA), | 250 | require_range(m.name, rva, sizeof(IMAGE_DELAYLOAD_DESCRIPTOR), m.size, "the delay-import table"); |
| 188 | at<IMAGE_THUNK_DATA64>(m.base, d->ImportAddressTableRVA)); | 251 | auto* d = at<IMAGE_DELAYLOAD_DESCRIPTOR>(m.base, static_cast<uint32_t>(rva)); |
| 252 | if (!d->DllNameRVA) break; | ||
| 253 | std::string dll = read_string(m.name, m.base, m.size, d->DllNameRVA, "a delay-imported DLL name"); | ||
| 254 | bind_thunks(dll, d->ImportNameTableRVA, d->ImportAddressTableRVA); | ||
| 189 | } | 255 | } |
| 190 | } | 256 | } |
| 191 | } | 257 | } |
| @@ -198,8 +264,10 @@ void Loader::protect_sections(Module& m) { | |||
| 198 | for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i, ++sec) { | 264 | for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i, ++sec) { |
| 199 | bool x = sec->Characteristics & IMAGE_SCN_MEM_EXECUTE, w = sec->Characteristics & IMAGE_SCN_MEM_WRITE; | 265 | bool x = sec->Characteristics & IMAGE_SCN_MEM_EXECUTE, w = sec->Characteristics & IMAGE_SCN_MEM_WRITE; |
| 200 | DWORD prot = x ? (w ? PAGE_EXECUTE_READWRITE : PAGE_EXECUTE_READ) : (w ? PAGE_READWRITE : PAGE_READONLY); | 266 | DWORD prot = x ? (w ? PAGE_EXECUTE_READWRITE : PAGE_EXECUTE_READ) : (w ? PAGE_READWRITE : PAGE_READONLY); |
| 201 | SIZE_T size = sec->Misc.VirtualSize ? sec->Misc.VirtualSize : sec->SizeOfRawData; | 267 | uint64_t size = sec->Misc.VirtualSize ? sec->Misc.VirtualSize : sec->SizeOfRawData; |
| 202 | if (size) VirtualProtect(m.base + sec->VirtualAddress, size, prot, &old); | 268 | if (sec->VirtualAddress >= m.size) continue; |
| 269 | size = std::min<uint64_t>(size, m.size - sec->VirtualAddress); | ||
| 270 | if (size) VirtualProtect(m.base + sec->VirtualAddress, static_cast<SIZE_T>(size), prot, &old); | ||
| 203 | } | 271 | } |
| 204 | FlushInstructionCache(GetCurrentProcess(), m.base, m.size); | 272 | FlushInstructionCache(GetCurrentProcess(), m.base, m.size); |
| 205 | } | 273 | } |