Timur Iskhodzhanov | 36d297d | 2012-02-22 13:59:49 | [diff] [blame] | 1 | //===-- interception_linux.cc -----------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is a part of AddressSanitizer, an address sanity checker. |
| 11 | // |
| 12 | // Windows-specific interception methods. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifdef _WIN32 |
| 16 | |
Alexey Samsonov | 9d742950 | 2012-08-02 11:29:14 | [diff] [blame] | 17 | #include "interception.h" |
Saleem Abdulrasool | d006c93 | 2015-10-29 20:36:55 | [diff] [blame] | 18 | #define WIN32_LEAN_AND_MEAN |
Timur Iskhodzhanov | 36d297d | 2012-02-22 13:59:49 | [diff] [blame] | 19 | #include <windows.h> |
| 20 | |
| 21 | namespace __interception { |
| 22 | |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 23 | // FIXME: internal_str* and internal_mem* functions should be moved from the |
| 24 | // ASan sources into interception/. |
| 25 | |
| 26 | static void _memset(void *p, int value, size_t sz) { |
| 27 | for (size_t i = 0; i < sz; ++i) |
| 28 | ((char*)p)[i] = (char)value; |
| 29 | } |
| 30 | |
| 31 | static void _memcpy(void *dst, void *src, size_t sz) { |
| 32 | char *dst_c = (char*)dst, |
| 33 | *src_c = (char*)src; |
| 34 | for (size_t i = 0; i < sz; ++i) |
| 35 | dst_c[i] = src_c[i]; |
| 36 | } |
| 37 | |
| 38 | static void WriteJumpInstruction(char *jmp_from, char *to) { |
| 39 | // jmp XXYYZZWW = E9 WW ZZ YY XX, where XXYYZZWW is an offset fromt jmp_from |
| 40 | // to the next instruction to the destination. |
| 41 | ptrdiff_t offset = to - jmp_from - 5; |
| 42 | *jmp_from = '\xE9'; |
| 43 | *(ptrdiff_t*)(jmp_from + 1) = offset; |
| 44 | } |
| 45 | |
Timur Iskhodzhanov | 37c00b4 | 2014-05-16 14:04:57 | [diff] [blame] | 46 | static char *GetMemoryForTrampoline(size_t size) { |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 47 | // Trampolines are allocated from a common pool. |
| 48 | const int POOL_SIZE = 1024; |
| 49 | static char *pool = NULL; |
| 50 | static size_t pool_used = 0; |
Timur Iskhodzhanov | 37c00b4 | 2014-05-16 14:04:57 | [diff] [blame] | 51 | if (!pool) { |
| 52 | pool = (char *)VirtualAlloc(NULL, POOL_SIZE, MEM_RESERVE | MEM_COMMIT, |
| 53 | PAGE_EXECUTE_READWRITE); |
| 54 | // FIXME: Might want to apply PAGE_EXECUTE_READ access after all the |
| 55 | // interceptors are in place. |
| 56 | if (!pool) |
| 57 | return NULL; |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 58 | _memset(pool, 0xCC /* int 3 */, POOL_SIZE); |
| 59 | } |
| 60 | |
Timur Iskhodzhanov | 37c00b4 | 2014-05-16 14:04:57 | [diff] [blame] | 61 | if (pool_used + size > POOL_SIZE) |
| 62 | return NULL; |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 63 | |
Timur Iskhodzhanov | 37c00b4 | 2014-05-16 14:04:57 | [diff] [blame] | 64 | char *ret = pool + pool_used; |
| 65 | pool_used += size; |
| 66 | return ret; |
| 67 | } |
| 68 | |
| 69 | // Returns 0 on error. |
| 70 | static size_t RoundUpToInstrBoundary(size_t size, char *code) { |
| 71 | size_t cursor = 0; |
| 72 | while (cursor < size) { |
| 73 | switch (code[cursor]) { |
Timur Iskhodzhanov | 50672ac | 2014-01-29 02:00:58 | [diff] [blame] | 74 | case '\x51': // push ecx |
| 75 | case '\x52': // push edx |
| 76 | case '\x53': // push ebx |
| 77 | case '\x54': // push esp |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 78 | case '\x55': // push ebp |
| 79 | case '\x56': // push esi |
| 80 | case '\x57': // push edi |
Timur Iskhodzhanov | 50672ac | 2014-01-29 02:00:58 | [diff] [blame] | 81 | case '\x5D': // pop ebp |
Timur Iskhodzhanov | 37c00b4 | 2014-05-16 14:04:57 | [diff] [blame] | 82 | cursor++; |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 83 | continue; |
Timur Iskhodzhanov | 50672ac | 2014-01-29 02:00:58 | [diff] [blame] | 84 | case '\x6A': // 6A XX = push XX |
Timur Iskhodzhanov | 37c00b4 | 2014-05-16 14:04:57 | [diff] [blame] | 85 | cursor += 2; |
Timur Iskhodzhanov | 50672ac | 2014-01-29 02:00:58 | [diff] [blame] | 86 | continue; |
| 87 | case '\xE9': // E9 XX YY ZZ WW = jmp WWZZYYXX |
Timur Iskhodzhanov | d58230b | 2015-03-17 16:50:59 | [diff] [blame] | 88 | case '\xB8': // B8 XX YY ZZ WW = mov eax, WWZZYYXX |
Timur Iskhodzhanov | 37c00b4 | 2014-05-16 14:04:57 | [diff] [blame] | 89 | cursor += 5; |
Timur Iskhodzhanov | 50672ac | 2014-01-29 02:00:58 | [diff] [blame] | 90 | continue; |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 91 | } |
Timur Iskhodzhanov | 37c00b4 | 2014-05-16 14:04:57 | [diff] [blame] | 92 | switch (*(unsigned short*)(code + cursor)) { // NOLINT |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 93 | case 0xFF8B: // 8B FF = mov edi, edi |
| 94 | case 0xEC8B: // 8B EC = mov ebp, esp |
| 95 | case 0xC033: // 33 C0 = xor eax, eax |
Timur Iskhodzhanov | 37c00b4 | 2014-05-16 14:04:57 | [diff] [blame] | 96 | cursor += 2; |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 97 | continue; |
Timur Iskhodzhanov | 50672ac | 2014-01-29 02:00:58 | [diff] [blame] | 98 | case 0x458B: // 8B 45 XX = mov eax, dword ptr [ebp+XXh] |
| 99 | case 0x5D8B: // 8B 5D XX = mov ebx, dword ptr [ebp+XXh] |
Reid Kleckner | 2310c65 | 2016-03-22 15:46:43 | [diff] [blame] | 100 | case 0x7D8B: // 8B 7D XX = mov edi, dword ptr [ebp+XXh] |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 101 | case 0xEC83: // 83 EC XX = sub esp, XX |
Timur Iskhodzhanov | 220ddac | 2014-08-22 12:38:07 | [diff] [blame] | 102 | case 0x75FF: // FF 75 XX = push dword ptr [ebp+XXh] |
Timur Iskhodzhanov | 37c00b4 | 2014-05-16 14:04:57 | [diff] [blame] | 103 | cursor += 3; |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 104 | continue; |
| 105 | case 0xC1F7: // F7 C1 XX YY ZZ WW = test ecx, WWZZYYXX |
Ehsan Akhgari | 911ea4e | 2014-07-14 20:28:21 | [diff] [blame] | 106 | case 0x25FF: // FF 25 XX YY ZZ WW = jmp dword ptr ds:[WWZZYYXX] |
Timur Iskhodzhanov | 37c00b4 | 2014-05-16 14:04:57 | [diff] [blame] | 107 | cursor += 6; |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 108 | continue; |
Timur Iskhodzhanov | 50672ac | 2014-01-29 02:00:58 | [diff] [blame] | 109 | case 0x3D83: // 83 3D XX YY ZZ WW TT = cmp TT, WWZZYYXX |
Timur Iskhodzhanov | 37c00b4 | 2014-05-16 14:04:57 | [diff] [blame] | 110 | cursor += 7; |
Timur Iskhodzhanov | 50672ac | 2014-01-29 02:00:58 | [diff] [blame] | 111 | continue; |
Reid Kleckner | 23d0fde | 2016-03-22 00:52:47 | [diff] [blame] | 112 | case 0x7D83: // 83 7D XX YY = cmp dword ptr [ebp+XXh], YY |
| 113 | cursor += 4; |
| 114 | continue; |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 115 | } |
Timur Iskhodzhanov | 37c00b4 | 2014-05-16 14:04:57 | [diff] [blame] | 116 | switch (0x00FFFFFF & *(unsigned int*)(code + cursor)) { |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 117 | case 0x24448A: // 8A 44 24 XX = mov eal, dword ptr [esp+XXh] |
Timur Iskhodzhanov | 51fadc3 | 2014-06-02 13:23:42 | [diff] [blame] | 118 | case 0x24448B: // 8B 44 24 XX = mov eax, dword ptr [esp+XXh] |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 119 | case 0x244C8B: // 8B 4C 24 XX = mov ecx, dword ptr [esp+XXh] |
| 120 | case 0x24548B: // 8B 54 24 XX = mov edx, dword ptr [esp+XXh] |
Timur Iskhodzhanov | 50672ac | 2014-01-29 02:00:58 | [diff] [blame] | 121 | case 0x24748B: // 8B 74 24 XX = mov esi, dword ptr [esp+XXh] |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 122 | case 0x247C8B: // 8B 7C 24 XX = mov edi, dword ptr [esp+XXh] |
Timur Iskhodzhanov | 37c00b4 | 2014-05-16 14:04:57 | [diff] [blame] | 123 | cursor += 4; |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 124 | continue; |
| 125 | } |
Reid Kleckner | d2f05f5 | 2016-03-21 18:23:07 | [diff] [blame] | 126 | switch (*(unsigned int *)(code + cursor)) { |
Reid Kleckner | 23d0fde | 2016-03-22 00:52:47 | [diff] [blame] | 127 | case 0x2444B60F: // 0F B6 44 24 XX = movzx eax, byte ptr [esp+XXh] |
Reid Kleckner | d2f05f5 | 2016-03-21 18:23:07 | [diff] [blame] | 128 | cursor += 5; |
| 129 | continue; |
| 130 | } |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 131 | |
| 132 | // Unknown instruction! |
Timur Iskhodzhanov | 50672ac | 2014-01-29 02:00:58 | [diff] [blame] | 133 | // FIXME: Unknown instruction failures might happen when we add a new |
| 134 | // interceptor or a new compiler version. In either case, they should result |
| 135 | // in visible and readable error messages. However, merely calling abort() |
Timur Iskhodzhanov | cbee13e | 2014-06-02 13:40:41 | [diff] [blame] | 136 | // leads to an infinite recursion in CheckFailed. |
Timur Iskhodzhanov | 50672ac | 2014-01-29 02:00:58 | [diff] [blame] | 137 | // Do we have a good way to abort with an error message here? |
Timur Iskhodzhanov | cbee13e | 2014-06-02 13:40:41 | [diff] [blame] | 138 | __debugbreak(); |
Timur Iskhodzhanov | 37c00b4 | 2014-05-16 14:04:57 | [diff] [blame] | 139 | return 0; |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 140 | } |
| 141 | |
Timur Iskhodzhanov | 37c00b4 | 2014-05-16 14:04:57 | [diff] [blame] | 142 | return cursor; |
| 143 | } |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 144 | |
Timur Iskhodzhanov | 37c00b4 | 2014-05-16 14:04:57 | [diff] [blame] | 145 | bool OverrideFunction(uptr old_func, uptr new_func, uptr *orig_old_func) { |
| 146 | #ifdef _WIN64 |
| 147 | #error OverrideFunction is not yet supported on x64 |
| 148 | #endif |
| 149 | // Function overriding works basically like this: |
| 150 | // We write "jmp <new_func>" (5 bytes) at the beginning of the 'old_func' |
| 151 | // to override it. |
| 152 | // We might want to be able to execute the original 'old_func' from the |
| 153 | // wrapper, in this case we need to keep the leading 5+ bytes ('head') |
| 154 | // of the original code somewhere with a "jmp <old_func+head>". |
| 155 | // We call these 'head'+5 bytes of instructions a "trampoline". |
| 156 | char *old_bytes = (char *)old_func; |
| 157 | |
| 158 | // We'll need at least 5 bytes for a 'jmp'. |
| 159 | size_t head = 5; |
| 160 | if (orig_old_func) { |
| 161 | // Find out the number of bytes of the instructions we need to copy |
| 162 | // to the trampoline and store it in 'head'. |
| 163 | head = RoundUpToInstrBoundary(head, old_bytes); |
| 164 | if (!head) |
| 165 | return false; |
| 166 | |
| 167 | // Put the needed instructions into the trampoline bytes. |
| 168 | char *trampoline = GetMemoryForTrampoline(head + 5); |
| 169 | if (!trampoline) |
| 170 | return false; |
| 171 | _memcpy(trampoline, old_bytes, head); |
| 172 | WriteJumpInstruction(trampoline + head, old_bytes + head); |
| 173 | *orig_old_func = (uptr)trampoline; |
| 174 | } |
| 175 | |
| 176 | // Now put the "jmp <new_func>" instruction at the original code location. |
| 177 | // We should preserve the EXECUTE flag as some of our own code might be |
| 178 | // located in the same page (sic!). FIXME: might consider putting the |
| 179 | // __interception code into a separate section or something? |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 180 | DWORD old_prot, unused_prot; |
Timur Iskhodzhanov | 37c00b4 | 2014-05-16 14:04:57 | [diff] [blame] | 181 | if (!VirtualProtect((void *)old_bytes, head, PAGE_EXECUTE_READWRITE, |
Alexey Samsonov | 9d742950 | 2012-08-02 11:29:14 | [diff] [blame] | 182 | &old_prot)) |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 183 | return false; |
| 184 | |
Timur Iskhodzhanov | 37c00b4 | 2014-05-16 14:04:57 | [diff] [blame] | 185 | WriteJumpInstruction(old_bytes, (char *)new_func); |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 186 | _memset(old_bytes + 5, 0xCC /* int 3 */, head - 5); |
| 187 | |
Timur Iskhodzhanov | 37c00b4 | 2014-05-16 14:04:57 | [diff] [blame] | 188 | // Restore the original permissions. |
| 189 | if (!VirtualProtect((void *)old_bytes, head, old_prot, &unused_prot)) |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 | [diff] [blame] | 190 | return false; // not clear if this failure bothers us. |
| 191 | |
| 192 | return true; |
| 193 | } |
| 194 | |
Reid Kleckner | d85f701 | 2015-08-18 22:38:27 | [diff] [blame] | 195 | static void **InterestingDLLsAvailable() { |
Timur Iskhodzhanov | d58230b | 2015-03-17 16:50:59 | [diff] [blame] | 196 | const char *InterestingDLLs[] = { |
Reid Kleckner | 3b02905 | 2016-03-24 20:19:48 | [diff] [blame^] | 197 | "kernel32.dll", |
| 198 | "msvcr110.dll", // VS2012 |
| 199 | "msvcr120.dll", // VS2013 |
| 200 | "vcruntime140.dll", // VS2015 |
| 201 | "ucrtbase.dll", // Universal CRT |
| 202 | // NTDLL should go last as it exports some functions that we should |
| 203 | // override in the CRT [presumably only used internally]. |
| 204 | "ntdll.dll", NULL}; |
Timur Iskhodzhanov | 0a88b25 | 2014-08-25 13:19:05 | [diff] [blame] | 205 | static void *result[ARRAY_SIZE(InterestingDLLs)] = { 0 }; |
| 206 | if (!result[0]) { |
| 207 | for (size_t i = 0, j = 0; InterestingDLLs[i]; ++i) { |
| 208 | if (HMODULE h = GetModuleHandleA(InterestingDLLs[i])) |
| 209 | result[j++] = (void *)h; |
| 210 | } |
| 211 | } |
Reid Kleckner | d85f701 | 2015-08-18 22:38:27 | [diff] [blame] | 212 | return &result[0]; |
| 213 | } |
| 214 | |
| 215 | namespace { |
| 216 | // Utility for reading loaded PE images. |
| 217 | template <typename T> class RVAPtr { |
| 218 | public: |
| 219 | RVAPtr(void *module, uptr rva) |
| 220 | : ptr_(reinterpret_cast<T *>(reinterpret_cast<char *>(module) + rva)) {} |
| 221 | operator T *() { return ptr_; } |
| 222 | T *operator->() { return ptr_; } |
| 223 | T *operator++() { return ++ptr_; } |
| 224 | |
| 225 | private: |
| 226 | T *ptr_; |
| 227 | }; |
| 228 | } // namespace |
| 229 | |
| 230 | // Internal implementation of GetProcAddress. At least since Windows 8, |
| 231 | // GetProcAddress appears to initialize DLLs before returning function pointers |
| 232 | // into them. This is problematic for the sanitizers, because they typically |
| 233 | // want to intercept malloc *before* MSVCRT initializes. Our internal |
| 234 | // implementation walks the export list manually without doing initialization. |
| 235 | uptr InternalGetProcAddress(void *module, const char *func_name) { |
| 236 | // Check that the module header is full and present. |
| 237 | RVAPtr<IMAGE_DOS_HEADER> dos_stub(module, 0); |
| 238 | RVAPtr<IMAGE_NT_HEADERS> headers(module, dos_stub->e_lfanew); |
| 239 | if (!module || dos_stub->e_magic != IMAGE_DOS_SIGNATURE || // "MZ" |
| 240 | headers->Signature != IMAGE_NT_SIGNATURE || // "PE\0\0" |
| 241 | headers->FileHeader.SizeOfOptionalHeader < |
| 242 | sizeof(IMAGE_OPTIONAL_HEADER)) { |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | IMAGE_DATA_DIRECTORY *export_directory = |
| 247 | &headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]; |
| 248 | RVAPtr<IMAGE_EXPORT_DIRECTORY> exports(module, |
| 249 | export_directory->VirtualAddress); |
| 250 | RVAPtr<DWORD> functions(module, exports->AddressOfFunctions); |
| 251 | RVAPtr<DWORD> names(module, exports->AddressOfNames); |
| 252 | RVAPtr<WORD> ordinals(module, exports->AddressOfNameOrdinals); |
| 253 | |
| 254 | for (DWORD i = 0; i < exports->NumberOfNames; i++) { |
| 255 | RVAPtr<char> name(module, names[i]); |
| 256 | if (!strcmp(func_name, name)) { |
| 257 | DWORD index = ordinals[i]; |
| 258 | RVAPtr<char> func(module, functions[index]); |
| 259 | return (uptr)(char *)func; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | return 0; |
Timur Iskhodzhanov | 0a88b25 | 2014-08-25 13:19:05 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static bool GetFunctionAddressInDLLs(const char *func_name, uptr *func_addr) { |
| 267 | *func_addr = 0; |
Reid Kleckner | d85f701 | 2015-08-18 22:38:27 | [diff] [blame] | 268 | void **DLLs = InterestingDLLsAvailable(); |
Timur Iskhodzhanov | 0a88b25 | 2014-08-25 13:19:05 | [diff] [blame] | 269 | for (size_t i = 0; *func_addr == 0 && DLLs[i]; ++i) |
Reid Kleckner | d85f701 | 2015-08-18 22:38:27 | [diff] [blame] | 270 | *func_addr = InternalGetProcAddress(DLLs[i], func_name); |
Timur Iskhodzhanov | 0a88b25 | 2014-08-25 13:19:05 | [diff] [blame] | 271 | return (*func_addr != 0); |
| 272 | } |
| 273 | |
| 274 | bool OverrideFunction(const char *name, uptr new_func, uptr *orig_old_func) { |
| 275 | uptr orig_func; |
| 276 | if (!GetFunctionAddressInDLLs(name, &orig_func)) |
| 277 | return false; |
| 278 | return OverrideFunction(orig_func, new_func, orig_old_func); |
| 279 | } |
| 280 | |
Reid Kleckner | 3b02905 | 2016-03-24 20:19:48 | [diff] [blame^] | 281 | bool OverrideImportedFunction(const char *module_to_patch, |
| 282 | const char *imported_module, |
| 283 | const char *function_name, uptr new_function, |
| 284 | uptr *orig_old_func) { |
| 285 | HMODULE module = GetModuleHandleA(module_to_patch); |
| 286 | if (!module) |
| 287 | return false; |
| 288 | |
| 289 | // Check that the module header is full and present. |
| 290 | RVAPtr<IMAGE_DOS_HEADER> dos_stub(module, 0); |
| 291 | RVAPtr<IMAGE_NT_HEADERS> headers(module, dos_stub->e_lfanew); |
| 292 | if (!module || dos_stub->e_magic != IMAGE_DOS_SIGNATURE || // "MZ" |
| 293 | headers->Signature != IMAGE_NT_SIGNATURE || // "PE\0\0" |
| 294 | headers->FileHeader.SizeOfOptionalHeader < |
| 295 | sizeof(IMAGE_OPTIONAL_HEADER)) { |
| 296 | return false; |
| 297 | } |
| 298 | |
| 299 | IMAGE_DATA_DIRECTORY *import_directory = |
| 300 | &headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]; |
| 301 | |
| 302 | // Iterate the list of imported DLLs. FirstThunk will be null for the last |
| 303 | // entry. |
| 304 | RVAPtr<IMAGE_IMPORT_DESCRIPTOR> imports(module, |
| 305 | import_directory->VirtualAddress); |
| 306 | for (; imports->FirstThunk != 0; ++imports) { |
| 307 | RVAPtr<const char> modname(module, imports->Name); |
| 308 | if (_stricmp(&*modname, imported_module) == 0) |
| 309 | break; |
| 310 | } |
| 311 | if (imports->FirstThunk == 0) |
| 312 | return false; |
| 313 | |
| 314 | // We have two parallel arrays: the import address table (IAT) and the table |
| 315 | // of names. They start out containing the same data, but the loader rewrites |
| 316 | // the IAT to hold imported addresses and leaves the name table in |
| 317 | // OriginalFirstThunk alone. |
| 318 | RVAPtr<IMAGE_THUNK_DATA> name_table(module, imports->OriginalFirstThunk); |
| 319 | RVAPtr<IMAGE_THUNK_DATA> iat(module, imports->FirstThunk); |
| 320 | for (; name_table->u1.Ordinal != 0; ++name_table, ++iat) { |
| 321 | if (!IMAGE_SNAP_BY_ORDINAL(name_table->u1.Ordinal)) { |
| 322 | RVAPtr<IMAGE_IMPORT_BY_NAME> import_by_name( |
| 323 | module, name_table->u1.ForwarderString); |
| 324 | const char *funcname = &import_by_name->Name[0]; |
| 325 | if (strcmp(funcname, function_name) == 0) |
| 326 | break; |
| 327 | } |
| 328 | } |
| 329 | if (name_table->u1.Ordinal == 0) |
| 330 | return false; |
| 331 | |
| 332 | // Now we have the correct IAT entry. Do the swap. We have to make the page |
| 333 | // read/write first. |
| 334 | if (orig_old_func) |
| 335 | *orig_old_func = iat->u1.AddressOfData; |
| 336 | DWORD old_prot, unused_prot; |
| 337 | if (!VirtualProtect(&iat->u1.AddressOfData, 4, PAGE_EXECUTE_READWRITE, |
| 338 | &old_prot)) |
| 339 | return false; |
| 340 | iat->u1.AddressOfData = new_function; |
| 341 | if (!VirtualProtect(&iat->u1.AddressOfData, 4, old_prot, &unused_prot)) |
| 342 | return false; // Not clear if this failure bothers us. |
| 343 | return true; |
| 344 | } |
| 345 | |
Timur Iskhodzhanov | 36d297d | 2012-02-22 13:59:49 | [diff] [blame] | 346 | } // namespace __interception |
| 347 | |
| 348 | #endif // _WIN32 |