blob: 89d52b053ebdfc8a69c28f9f681ec9e54d9a18b9 [file] [log] [blame]
Timur Iskhodzhanov36d297d2012-02-22 13:59:491//===-- 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 Samsonov9d7429502012-08-02 11:29:1417#include "interception.h"
Saleem Abdulrasoold006c932015-10-29 20:36:5518#define WIN32_LEAN_AND_MEAN
Timur Iskhodzhanov36d297d2012-02-22 13:59:4919#include <windows.h>
20
21namespace __interception {
22
Timur Iskhodzhanov2f48b872012-03-12 11:45:0923// FIXME: internal_str* and internal_mem* functions should be moved from the
24// ASan sources into interception/.
25
26static 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
31static 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
38static 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 Iskhodzhanov37c00b42014-05-16 14:04:5746static char *GetMemoryForTrampoline(size_t size) {
Timur Iskhodzhanov2f48b872012-03-12 11:45:0947 // 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 Iskhodzhanov37c00b42014-05-16 14:04:5751 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 Iskhodzhanov2f48b872012-03-12 11:45:0958 _memset(pool, 0xCC /* int 3 */, POOL_SIZE);
59 }
60
Timur Iskhodzhanov37c00b42014-05-16 14:04:5761 if (pool_used + size > POOL_SIZE)
62 return NULL;
Timur Iskhodzhanov2f48b872012-03-12 11:45:0963
Timur Iskhodzhanov37c00b42014-05-16 14:04:5764 char *ret = pool + pool_used;
65 pool_used += size;
66 return ret;
67}
68
69// Returns 0 on error.
70static size_t RoundUpToInstrBoundary(size_t size, char *code) {
71 size_t cursor = 0;
72 while (cursor < size) {
73 switch (code[cursor]) {
Timur Iskhodzhanov50672ac2014-01-29 02:00:5874 case '\x51': // push ecx
75 case '\x52': // push edx
76 case '\x53': // push ebx
77 case '\x54': // push esp
Timur Iskhodzhanov2f48b872012-03-12 11:45:0978 case '\x55': // push ebp
79 case '\x56': // push esi
80 case '\x57': // push edi
Timur Iskhodzhanov50672ac2014-01-29 02:00:5881 case '\x5D': // pop ebp
Timur Iskhodzhanov37c00b42014-05-16 14:04:5782 cursor++;
Timur Iskhodzhanov2f48b872012-03-12 11:45:0983 continue;
Timur Iskhodzhanov50672ac2014-01-29 02:00:5884 case '\x6A': // 6A XX = push XX
Timur Iskhodzhanov37c00b42014-05-16 14:04:5785 cursor += 2;
Timur Iskhodzhanov50672ac2014-01-29 02:00:5886 continue;
87 case '\xE9': // E9 XX YY ZZ WW = jmp WWZZYYXX
Timur Iskhodzhanovd58230b2015-03-17 16:50:5988 case '\xB8': // B8 XX YY ZZ WW = mov eax, WWZZYYXX
Timur Iskhodzhanov37c00b42014-05-16 14:04:5789 cursor += 5;
Timur Iskhodzhanov50672ac2014-01-29 02:00:5890 continue;
Timur Iskhodzhanov2f48b872012-03-12 11:45:0991 }
Timur Iskhodzhanov37c00b42014-05-16 14:04:5792 switch (*(unsigned short*)(code + cursor)) { // NOLINT
Timur Iskhodzhanov2f48b872012-03-12 11:45:0993 case 0xFF8B: // 8B FF = mov edi, edi
94 case 0xEC8B: // 8B EC = mov ebp, esp
95 case 0xC033: // 33 C0 = xor eax, eax
Timur Iskhodzhanov37c00b42014-05-16 14:04:5796 cursor += 2;
Timur Iskhodzhanov2f48b872012-03-12 11:45:0997 continue;
Timur Iskhodzhanov50672ac2014-01-29 02:00:5898 case 0x458B: // 8B 45 XX = mov eax, dword ptr [ebp+XXh]
99 case 0x5D8B: // 8B 5D XX = mov ebx, dword ptr [ebp+XXh]
Reid Kleckner2310c652016-03-22 15:46:43100 case 0x7D8B: // 8B 7D XX = mov edi, dword ptr [ebp+XXh]
Timur Iskhodzhanov2f48b872012-03-12 11:45:09101 case 0xEC83: // 83 EC XX = sub esp, XX
Timur Iskhodzhanov220ddac2014-08-22 12:38:07102 case 0x75FF: // FF 75 XX = push dword ptr [ebp+XXh]
Timur Iskhodzhanov37c00b42014-05-16 14:04:57103 cursor += 3;
Timur Iskhodzhanov2f48b872012-03-12 11:45:09104 continue;
105 case 0xC1F7: // F7 C1 XX YY ZZ WW = test ecx, WWZZYYXX
Ehsan Akhgari911ea4e2014-07-14 20:28:21106 case 0x25FF: // FF 25 XX YY ZZ WW = jmp dword ptr ds:[WWZZYYXX]
Timur Iskhodzhanov37c00b42014-05-16 14:04:57107 cursor += 6;
Timur Iskhodzhanov2f48b872012-03-12 11:45:09108 continue;
Timur Iskhodzhanov50672ac2014-01-29 02:00:58109 case 0x3D83: // 83 3D XX YY ZZ WW TT = cmp TT, WWZZYYXX
Timur Iskhodzhanov37c00b42014-05-16 14:04:57110 cursor += 7;
Timur Iskhodzhanov50672ac2014-01-29 02:00:58111 continue;
Reid Kleckner23d0fde2016-03-22 00:52:47112 case 0x7D83: // 83 7D XX YY = cmp dword ptr [ebp+XXh], YY
113 cursor += 4;
114 continue;
Timur Iskhodzhanov2f48b872012-03-12 11:45:09115 }
Timur Iskhodzhanov37c00b42014-05-16 14:04:57116 switch (0x00FFFFFF & *(unsigned int*)(code + cursor)) {
Timur Iskhodzhanov2f48b872012-03-12 11:45:09117 case 0x24448A: // 8A 44 24 XX = mov eal, dword ptr [esp+XXh]
Timur Iskhodzhanov51fadc32014-06-02 13:23:42118 case 0x24448B: // 8B 44 24 XX = mov eax, dword ptr [esp+XXh]
Timur Iskhodzhanov2f48b872012-03-12 11:45:09119 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 Iskhodzhanov50672ac2014-01-29 02:00:58121 case 0x24748B: // 8B 74 24 XX = mov esi, dword ptr [esp+XXh]
Timur Iskhodzhanov2f48b872012-03-12 11:45:09122 case 0x247C8B: // 8B 7C 24 XX = mov edi, dword ptr [esp+XXh]
Timur Iskhodzhanov37c00b42014-05-16 14:04:57123 cursor += 4;
Timur Iskhodzhanov2f48b872012-03-12 11:45:09124 continue;
125 }
Reid Klecknerd2f05f52016-03-21 18:23:07126 switch (*(unsigned int *)(code + cursor)) {
Reid Kleckner23d0fde2016-03-22 00:52:47127 case 0x2444B60F: // 0F B6 44 24 XX = movzx eax, byte ptr [esp+XXh]
Reid Klecknerd2f05f52016-03-21 18:23:07128 cursor += 5;
129 continue;
130 }
Timur Iskhodzhanov2f48b872012-03-12 11:45:09131
132 // Unknown instruction!
Timur Iskhodzhanov50672ac2014-01-29 02:00:58133 // 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 Iskhodzhanovcbee13e2014-06-02 13:40:41136 // leads to an infinite recursion in CheckFailed.
Timur Iskhodzhanov50672ac2014-01-29 02:00:58137 // Do we have a good way to abort with an error message here?
Timur Iskhodzhanovcbee13e2014-06-02 13:40:41138 __debugbreak();
Timur Iskhodzhanov37c00b42014-05-16 14:04:57139 return 0;
Timur Iskhodzhanov2f48b872012-03-12 11:45:09140 }
141
Timur Iskhodzhanov37c00b42014-05-16 14:04:57142 return cursor;
143}
Timur Iskhodzhanov2f48b872012-03-12 11:45:09144
Timur Iskhodzhanov37c00b42014-05-16 14:04:57145bool 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 Iskhodzhanov2f48b872012-03-12 11:45:09180 DWORD old_prot, unused_prot;
Timur Iskhodzhanov37c00b42014-05-16 14:04:57181 if (!VirtualProtect((void *)old_bytes, head, PAGE_EXECUTE_READWRITE,
Alexey Samsonov9d7429502012-08-02 11:29:14182 &old_prot))
Timur Iskhodzhanov2f48b872012-03-12 11:45:09183 return false;
184
Timur Iskhodzhanov37c00b42014-05-16 14:04:57185 WriteJumpInstruction(old_bytes, (char *)new_func);
Timur Iskhodzhanov2f48b872012-03-12 11:45:09186 _memset(old_bytes + 5, 0xCC /* int 3 */, head - 5);
187
Timur Iskhodzhanov37c00b42014-05-16 14:04:57188 // Restore the original permissions.
189 if (!VirtualProtect((void *)old_bytes, head, old_prot, &unused_prot))
Timur Iskhodzhanov2f48b872012-03-12 11:45:09190 return false; // not clear if this failure bothers us.
191
192 return true;
193}
194
Reid Klecknerd85f7012015-08-18 22:38:27195static void **InterestingDLLsAvailable() {
Timur Iskhodzhanovd58230b2015-03-17 16:50:59196 const char *InterestingDLLs[] = {
Reid Kleckner3b029052016-03-24 20:19:48197 "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 Iskhodzhanov0a88b252014-08-25 13:19:05205 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 Klecknerd85f7012015-08-18 22:38:27212 return &result[0];
213}
214
215namespace {
216// Utility for reading loaded PE images.
217template <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.
235uptr 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 Iskhodzhanov0a88b252014-08-25 13:19:05264}
265
266static bool GetFunctionAddressInDLLs(const char *func_name, uptr *func_addr) {
267 *func_addr = 0;
Reid Klecknerd85f7012015-08-18 22:38:27268 void **DLLs = InterestingDLLsAvailable();
Timur Iskhodzhanov0a88b252014-08-25 13:19:05269 for (size_t i = 0; *func_addr == 0 && DLLs[i]; ++i)
Reid Klecknerd85f7012015-08-18 22:38:27270 *func_addr = InternalGetProcAddress(DLLs[i], func_name);
Timur Iskhodzhanov0a88b252014-08-25 13:19:05271 return (*func_addr != 0);
272}
273
274bool 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 Kleckner3b029052016-03-24 20:19:48281bool 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 Iskhodzhanov36d297d2012-02-22 13:59:49346} // namespace __interception
347
348#endif // _WIN32