1 //===-- interception_linux.cc -----------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of AddressSanitizer, an address sanity checker.
9 //
10 // Windows-specific interception methods.
11 //
12 // This file is implementing several hooking techniques to intercept calls
13 // to functions. The hooks are dynamically installed by modifying the assembly
14 // code.
15 //
16 // The hooking techniques are making assumptions on the way the code is
17 // generated and are safe under these assumptions.
18 //
19 // On 64-bit architecture, there is no direct 64-bit jump instruction. To allow
20 // arbitrary branching on the whole memory space, the notion of trampoline
21 // region is used. A trampoline region is a memory space withing 2G boundary
22 // where it is safe to add custom assembly code to build 64-bit jumps.
23 //
24 // Hooking techniques
25 // ==================
26 //
27 // 1) Detour
28 //
29 // The Detour hooking technique is assuming the presence of an header with
30 // padding and an overridable 2-bytes nop instruction (mov edi, edi). The
31 // nop instruction can safely be replaced by a 2-bytes jump without any need
32 // to save the instruction. A jump to the target is encoded in the function
33 // header and the nop instruction is replaced by a short jump to the header.
34 //
35 // head: 5 x nop head: jmp <hook>
36 // func: mov edi, edi --> func: jmp short <head>
37 // [...] real: [...]
38 //
39 // This technique is only implemented on 32-bit architecture.
40 // Most of the time, Windows API are hookable with the detour technique.
41 //
42 // 2) Redirect Jump
43 //
44 // The redirect jump is applicable when the first instruction is a direct
45 // jump. The instruction is replaced by jump to the hook.
46 //
47 // func: jmp <label> --> func: jmp <hook>
48 //
49 // On an 64-bit architecture, a trampoline is inserted.
50 //
51 // func: jmp <label> --> func: jmp <tramp>
52 // [...]
53 //
54 // [trampoline]
55 // tramp: jmp QWORD [addr]
56 // addr: .bytes <hook>
57 //
58 // Note: <real> is equilavent to <label>.
59 //
60 // 3) HotPatch
61 //
62 // The HotPatch hooking is assuming the presence of an header with padding
63 // and a first instruction with at least 2-bytes.
64 //
65 // The reason to enforce the 2-bytes limitation is to provide the minimal
66 // space to encode a short jump. HotPatch technique is only rewriting one
67 // instruction to avoid breaking a sequence of instructions containing a
68 // branching target.
69 //
70 // Assumptions are enforced by MSVC compiler by using the /HOTPATCH flag.
71 // see: https://msdn.microsoft.com/en-us/library/ms173507.aspx
72 // Default padding length is 5 bytes in 32-bits and 6 bytes in 64-bits.
73 //
74 // head: 5 x nop head: jmp <hook>
75 // func: <instr> --> func: jmp short <head>
76 // [...] body: [...]
77 //
78 // [trampoline]
79 // real: <instr>
80 // jmp <body>
81 //
82 // On an 64-bit architecture:
83 //
84 // head: 6 x nop head: jmp QWORD [addr1]
85 // func: <instr> --> func: jmp short <head>
86 // [...] body: [...]
87 //
88 // [trampoline]
89 // addr1: .bytes <hook>
90 // real: <instr>
91 // jmp QWORD [addr2]
92 // addr2: .bytes <body>
93 //
94 // 4) Trampoline
95 //
96 // The Trampoline hooking technique is the most aggressive one. It is
97 // assuming that there is a sequence of instructions that can be safely
98 // replaced by a jump (enough room and no incoming branches).
99 //
100 // Unfortunately, these assumptions can't be safely presumed and code may
101 // be broken after hooking.
102 //
103 // func: <instr> --> func: jmp <hook>
104 // <instr>
105 // [...] body: [...]
106 //
107 // [trampoline]
108 // real: <instr>
109 // <instr>
110 // jmp <body>
111 //
112 // On an 64-bit architecture:
113 //
114 // func: <instr> --> func: jmp QWORD [addr1]
115 // <instr>
116 // [...] body: [...]
117 //
118 // [trampoline]
119 // addr1: .bytes <hook>
120 // real: <instr>
121 // <instr>
122 // jmp QWORD [addr2]
123 // addr2: .bytes <body>
124 //===----------------------------------------------------------------------===//
125
126 #include "interception.h"
127
128 #if SANITIZER_WINDOWS
129 #include "sanitizer_common/sanitizer_platform.h"
130 #define WIN32_LEAN_AND_MEAN
131 #include <windows.h>
132
133 namespace __interception {
134
135 static const int kAddressLength = FIRST_32_SECOND_64(4, 8);
136 static const int kJumpInstructionLength = 5;
137 static const int kShortJumpInstructionLength = 2;
138 static const int kIndirectJumpInstructionLength = 6;
139 static const int kBranchLength =
140 FIRST_32_SECOND_64(kJumpInstructionLength, kIndirectJumpInstructionLength);
141 static const int kDirectBranchLength = kBranchLength + kAddressLength;
142
InterceptionFailed()143 static void InterceptionFailed() {
144 // Do we have a good way to abort with an error message here?
145 __debugbreak();
146 }
147
DistanceIsWithin2Gig(uptr from,uptr target)148 static bool DistanceIsWithin2Gig(uptr from, uptr target) {
149 #if SANITIZER_WINDOWS64
150 if (from < target)
151 return target - from <= (uptr)0x7FFFFFFFU;
152 else
153 return from - target <= (uptr)0x80000000U;
154 #else
155 // In a 32-bit address space, the address calculation will wrap, so this check
156 // is unnecessary.
157 return true;
158 #endif
159 }
160
GetMmapGranularity()161 static uptr GetMmapGranularity() {
162 SYSTEM_INFO si;
163 GetSystemInfo(&si);
164 return si.dwAllocationGranularity;
165 }
166
RoundUpTo(uptr size,uptr boundary)167 static uptr RoundUpTo(uptr size, uptr boundary) {
168 return (size + boundary - 1) & ~(boundary - 1);
169 }
170
171 // FIXME: internal_str* and internal_mem* functions should be moved from the
172 // ASan sources into interception/.
173
_strlen(const char * str)174 static size_t _strlen(const char *str) {
175 const char* p = str;
176 while (*p != '\0') ++p;
177 return p - str;
178 }
179
_strchr(char * str,char c)180 static char* _strchr(char* str, char c) {
181 while (*str) {
182 if (*str == c)
183 return str;
184 ++str;
185 }
186 return nullptr;
187 }
188
_memset(void * p,int value,size_t sz)189 static void _memset(void *p, int value, size_t sz) {
190 for (size_t i = 0; i < sz; ++i)
191 ((char*)p)[i] = (char)value;
192 }
193
_memcpy(void * dst,void * src,size_t sz)194 static void _memcpy(void *dst, void *src, size_t sz) {
195 char *dst_c = (char*)dst,
196 *src_c = (char*)src;
197 for (size_t i = 0; i < sz; ++i)
198 dst_c[i] = src_c[i];
199 }
200
ChangeMemoryProtection(uptr address,uptr size,DWORD * old_protection)201 static bool ChangeMemoryProtection(
202 uptr address, uptr size, DWORD *old_protection) {
203 return ::VirtualProtect((void*)address, size,
204 PAGE_EXECUTE_READWRITE,
205 old_protection) != FALSE;
206 }
207
RestoreMemoryProtection(uptr address,uptr size,DWORD old_protection)208 static bool RestoreMemoryProtection(
209 uptr address, uptr size, DWORD old_protection) {
210 DWORD unused;
211 return ::VirtualProtect((void*)address, size,
212 old_protection,
213 &unused) != FALSE;
214 }
215
IsMemoryPadding(uptr address,uptr size)216 static bool IsMemoryPadding(uptr address, uptr size) {
217 u8* function = (u8*)address;
218 for (size_t i = 0; i < size; ++i)
219 if (function[i] != 0x90 && function[i] != 0xCC)
220 return false;
221 return true;
222 }
223
224 static const u8 kHintNop8Bytes[] = {
225 0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00
226 };
227
228 template<class T>
FunctionHasPrefix(uptr address,const T & pattern)229 static bool FunctionHasPrefix(uptr address, const T &pattern) {
230 u8* function = (u8*)address - sizeof(pattern);
231 for (size_t i = 0; i < sizeof(pattern); ++i)
232 if (function[i] != pattern[i])
233 return false;
234 return true;
235 }
236
FunctionHasPadding(uptr address,uptr size)237 static bool FunctionHasPadding(uptr address, uptr size) {
238 if (IsMemoryPadding(address - size, size))
239 return true;
240 if (size <= sizeof(kHintNop8Bytes) &&
241 FunctionHasPrefix(address, kHintNop8Bytes))
242 return true;
243 return false;
244 }
245
WritePadding(uptr from,uptr size)246 static void WritePadding(uptr from, uptr size) {
247 _memset((void*)from, 0xCC, (size_t)size);
248 }
249
WriteJumpInstruction(uptr from,uptr target)250 static void WriteJumpInstruction(uptr from, uptr target) {
251 if (!DistanceIsWithin2Gig(from + kJumpInstructionLength, target))
252 InterceptionFailed();
253 ptrdiff_t offset = target - from - kJumpInstructionLength;
254 *(u8*)from = 0xE9;
255 *(u32*)(from + 1) = offset;
256 }
257
WriteShortJumpInstruction(uptr from,uptr target)258 static void WriteShortJumpInstruction(uptr from, uptr target) {
259 sptr offset = target - from - kShortJumpInstructionLength;
260 if (offset < -128 || offset > 127)
261 InterceptionFailed();
262 *(u8*)from = 0xEB;
263 *(u8*)(from + 1) = (u8)offset;
264 }
265
266 #if SANITIZER_WINDOWS64
WriteIndirectJumpInstruction(uptr from,uptr indirect_target)267 static void WriteIndirectJumpInstruction(uptr from, uptr indirect_target) {
268 // jmp [rip + <offset>] = FF 25 <offset> where <offset> is a relative
269 // offset.
270 // The offset is the distance from then end of the jump instruction to the
271 // memory location containing the targeted address. The displacement is still
272 // 32-bit in x64, so indirect_target must be located within +/- 2GB range.
273 int offset = indirect_target - from - kIndirectJumpInstructionLength;
274 if (!DistanceIsWithin2Gig(from + kIndirectJumpInstructionLength,
275 indirect_target)) {
276 InterceptionFailed();
277 }
278 *(u16*)from = 0x25FF;
279 *(u32*)(from + 2) = offset;
280 }
281 #endif
282
WriteBranch(uptr from,uptr indirect_target,uptr target)283 static void WriteBranch(
284 uptr from, uptr indirect_target, uptr target) {
285 #if SANITIZER_WINDOWS64
286 WriteIndirectJumpInstruction(from, indirect_target);
287 *(u64*)indirect_target = target;
288 #else
289 (void)indirect_target;
290 WriteJumpInstruction(from, target);
291 #endif
292 }
293
WriteDirectBranch(uptr from,uptr target)294 static void WriteDirectBranch(uptr from, uptr target) {
295 #if SANITIZER_WINDOWS64
296 // Emit an indirect jump through immediately following bytes:
297 // jmp [rip + kBranchLength]
298 // .quad <target>
299 WriteBranch(from, from + kBranchLength, target);
300 #else
301 WriteJumpInstruction(from, target);
302 #endif
303 }
304
305 struct TrampolineMemoryRegion {
306 uptr content;
307 uptr allocated_size;
308 uptr max_size;
309 };
310
311 static const uptr kTrampolineScanLimitRange = 1 << 31; // 2 gig
312 static const int kMaxTrampolineRegion = 1024;
313 static TrampolineMemoryRegion TrampolineRegions[kMaxTrampolineRegion];
314
AllocateTrampolineRegion(uptr image_address,size_t granularity)315 static void *AllocateTrampolineRegion(uptr image_address, size_t granularity) {
316 #if SANITIZER_WINDOWS64
317 uptr address = image_address;
318 uptr scanned = 0;
319 while (scanned < kTrampolineScanLimitRange) {
320 MEMORY_BASIC_INFORMATION info;
321 if (!::VirtualQuery((void*)address, &info, sizeof(info)))
322 return nullptr;
323
324 // Check whether a region can be allocated at |address|.
325 if (info.State == MEM_FREE && info.RegionSize >= granularity) {
326 void *page = ::VirtualAlloc((void*)RoundUpTo(address, granularity),
327 granularity,
328 MEM_RESERVE | MEM_COMMIT,
329 PAGE_EXECUTE_READWRITE);
330 return page;
331 }
332
333 // Move to the next region.
334 address = (uptr)info.BaseAddress + info.RegionSize;
335 scanned += info.RegionSize;
336 }
337 return nullptr;
338 #else
339 return ::VirtualAlloc(nullptr,
340 granularity,
341 MEM_RESERVE | MEM_COMMIT,
342 PAGE_EXECUTE_READWRITE);
343 #endif
344 }
345
346 // Used by unittests to release mapped memory space.
TestOnlyReleaseTrampolineRegions()347 void TestOnlyReleaseTrampolineRegions() {
348 for (size_t bucket = 0; bucket < kMaxTrampolineRegion; ++bucket) {
349 TrampolineMemoryRegion *current = &TrampolineRegions[bucket];
350 if (current->content == 0)
351 return;
352 ::VirtualFree((void*)current->content, 0, MEM_RELEASE);
353 current->content = 0;
354 }
355 }
356
AllocateMemoryForTrampoline(uptr image_address,size_t size)357 static uptr AllocateMemoryForTrampoline(uptr image_address, size_t size) {
358 // Find a region within 2G with enough space to allocate |size| bytes.
359 TrampolineMemoryRegion *region = nullptr;
360 for (size_t bucket = 0; bucket < kMaxTrampolineRegion; ++bucket) {
361 TrampolineMemoryRegion* current = &TrampolineRegions[bucket];
362 if (current->content == 0) {
363 // No valid region found, allocate a new region.
364 size_t bucket_size = GetMmapGranularity();
365 void *content = AllocateTrampolineRegion(image_address, bucket_size);
366 if (content == nullptr)
367 return 0U;
368
369 current->content = (uptr)content;
370 current->allocated_size = 0;
371 current->max_size = bucket_size;
372 region = current;
373 break;
374 } else if (current->max_size - current->allocated_size > size) {
375 #if SANITIZER_WINDOWS64
376 // In 64-bits, the memory space must be allocated within 2G boundary.
377 uptr next_address = current->content + current->allocated_size;
378 if (next_address < image_address ||
379 next_address - image_address >= 0x7FFF0000)
380 continue;
381 #endif
382 // The space can be allocated in the current region.
383 region = current;
384 break;
385 }
386 }
387
388 // Failed to find a region.
389 if (region == nullptr)
390 return 0U;
391
392 // Allocate the space in the current region.
393 uptr allocated_space = region->content + region->allocated_size;
394 region->allocated_size += size;
395 WritePadding(allocated_space, size);
396
397 return allocated_space;
398 }
399
400 // Returns 0 on error.
GetInstructionSize(uptr address,size_t * rel_offset=nullptr)401 static size_t GetInstructionSize(uptr address, size_t* rel_offset = nullptr) {
402 switch (*(u64*)address) {
403 case 0x90909090909006EB: // stub: jmp over 6 x nop.
404 return 8;
405 }
406
407 switch (*(u8*)address) {
408 case 0x90: // 90 : nop
409 return 1;
410
411 case 0x50: // push eax / rax
412 case 0x51: // push ecx / rcx
413 case 0x52: // push edx / rdx
414 case 0x53: // push ebx / rbx
415 case 0x54: // push esp / rsp
416 case 0x55: // push ebp / rbp
417 case 0x56: // push esi / rsi
418 case 0x57: // push edi / rdi
419 case 0x5D: // pop ebp / rbp
420 return 1;
421
422 case 0x6A: // 6A XX = push XX
423 return 2;
424
425 case 0xb8: // b8 XX XX XX XX : mov eax, XX XX XX XX
426 case 0xB9: // b9 XX XX XX XX : mov ecx, XX XX XX XX
427 return 5;
428
429 // Cannot overwrite control-instruction. Return 0 to indicate failure.
430 case 0xE9: // E9 XX XX XX XX : jmp <label>
431 case 0xE8: // E8 XX XX XX XX : call <func>
432 case 0xC3: // C3 : ret
433 case 0xEB: // EB XX : jmp XX (short jump)
434 case 0x70: // 7Y YY : jy XX (short conditional jump)
435 case 0x71:
436 case 0x72:
437 case 0x73:
438 case 0x74:
439 case 0x75:
440 case 0x76:
441 case 0x77:
442 case 0x78:
443 case 0x79:
444 case 0x7A:
445 case 0x7B:
446 case 0x7C:
447 case 0x7D:
448 case 0x7E:
449 case 0x7F:
450 return 0;
451 }
452
453 switch (*(u16*)(address)) {
454 case 0x018A: // 8A 01 : mov al, byte ptr [ecx]
455 case 0xFF8B: // 8B FF : mov edi, edi
456 case 0xEC8B: // 8B EC : mov ebp, esp
457 case 0xc889: // 89 C8 : mov eax, ecx
458 case 0xC18B: // 8B C1 : mov eax, ecx
459 case 0xC033: // 33 C0 : xor eax, eax
460 case 0xC933: // 33 C9 : xor ecx, ecx
461 case 0xD233: // 33 D2 : xor edx, edx
462 return 2;
463
464 // Cannot overwrite control-instruction. Return 0 to indicate failure.
465 case 0x25FF: // FF 25 XX XX XX XX : jmp [XXXXXXXX]
466 return 0;
467 }
468
469 switch (0x00FFFFFF & *(u32*)address) {
470 case 0x24A48D: // 8D A4 24 XX XX XX XX : lea esp, [esp + XX XX XX XX]
471 return 7;
472 }
473
474 #if SANITIZER_WINDOWS64
475 switch (*(u8*)address) {
476 case 0xA1: // A1 XX XX XX XX XX XX XX XX :
477 // movabs eax, dword ptr ds:[XXXXXXXX]
478 return 9;
479 }
480
481 switch (*(u16*)address) {
482 case 0x5040: // push rax
483 case 0x5140: // push rcx
484 case 0x5240: // push rdx
485 case 0x5340: // push rbx
486 case 0x5440: // push rsp
487 case 0x5540: // push rbp
488 case 0x5640: // push rsi
489 case 0x5740: // push rdi
490 case 0x5441: // push r12
491 case 0x5541: // push r13
492 case 0x5641: // push r14
493 case 0x5741: // push r15
494 case 0x9066: // Two-byte NOP
495 return 2;
496
497 case 0x058B: // 8B 05 XX XX XX XX : mov eax, dword ptr [XX XX XX XX]
498 if (rel_offset)
499 *rel_offset = 2;
500 return 6;
501 }
502
503 switch (0x00FFFFFF & *(u32*)address) {
504 case 0xe58948: // 48 8b c4 : mov rbp, rsp
505 case 0xc18b48: // 48 8b c1 : mov rax, rcx
506 case 0xc48b48: // 48 8b c4 : mov rax, rsp
507 case 0xd9f748: // 48 f7 d9 : neg rcx
508 case 0xd12b48: // 48 2b d1 : sub rdx, rcx
509 case 0x07c1f6: // f6 c1 07 : test cl, 0x7
510 case 0xc98548: // 48 85 C9 : test rcx, rcx
511 case 0xc0854d: // 4d 85 c0 : test r8, r8
512 case 0xc2b60f: // 0f b6 c2 : movzx eax, dl
513 case 0xc03345: // 45 33 c0 : xor r8d, r8d
514 case 0xdb3345: // 45 33 DB : xor r11d, r11d
515 case 0xd98b4c: // 4c 8b d9 : mov r11, rcx
516 case 0xd28b4c: // 4c 8b d2 : mov r10, rdx
517 case 0xc98b4c: // 4C 8B C9 : mov r9, rcx
518 case 0xd2b60f: // 0f b6 d2 : movzx edx, dl
519 case 0xca2b48: // 48 2b ca : sub rcx, rdx
520 case 0x10b70f: // 0f b7 10 : movzx edx, WORD PTR [rax]
521 case 0xc00b4d: // 3d 0b c0 : or r8, r8
522 case 0xd18b48: // 48 8b d1 : mov rdx, rcx
523 case 0xdc8b4c: // 4c 8b dc : mov r11, rsp
524 case 0xd18b4c: // 4c 8b d1 : mov r10, rcx
525 return 3;
526
527 case 0xec8348: // 48 83 ec XX : sub rsp, XX
528 case 0xf88349: // 49 83 f8 XX : cmp r8, XX
529 case 0x588948: // 48 89 58 XX : mov QWORD PTR[rax + XX], rbx
530 return 4;
531
532 case 0xec8148: // 48 81 EC XX XX XX XX : sub rsp, XXXXXXXX
533 return 7;
534
535 case 0x058b48: // 48 8b 05 XX XX XX XX :
536 // mov rax, QWORD PTR [rip + XXXXXXXX]
537 case 0x25ff48: // 48 ff 25 XX XX XX XX :
538 // rex.W jmp QWORD PTR [rip + XXXXXXXX]
539
540 // Instructions having offset relative to 'rip' need offset adjustment.
541 if (rel_offset)
542 *rel_offset = 3;
543 return 7;
544
545 case 0x2444c7: // C7 44 24 XX YY YY YY YY
546 // mov dword ptr [rsp + XX], YYYYYYYY
547 return 8;
548 }
549
550 switch (*(u32*)(address)) {
551 case 0x24448b48: // 48 8b 44 24 XX : mov rax, QWORD ptr [rsp + XX]
552 case 0x246c8948: // 48 89 6C 24 XX : mov QWORD ptr [rsp + XX], rbp
553 case 0x245c8948: // 48 89 5c 24 XX : mov QWORD PTR [rsp + XX], rbx
554 case 0x24748948: // 48 89 74 24 XX : mov QWORD PTR [rsp + XX], rsi
555 case 0x244C8948: // 48 89 4C 24 XX : mov QWORD PTR [rsp + XX], rcx
556 return 5;
557 case 0x24648348: // 48 83 64 24 XX : and QWORD PTR [rsp + XX], YY
558 return 6;
559 }
560
561 #else
562
563 switch (*(u8*)address) {
564 case 0xA1: // A1 XX XX XX XX : mov eax, dword ptr ds:[XXXXXXXX]
565 return 5;
566 }
567 switch (*(u16*)address) {
568 case 0x458B: // 8B 45 XX : mov eax, dword ptr [ebp + XX]
569 case 0x5D8B: // 8B 5D XX : mov ebx, dword ptr [ebp + XX]
570 case 0x7D8B: // 8B 7D XX : mov edi, dword ptr [ebp + XX]
571 case 0xEC83: // 83 EC XX : sub esp, XX
572 case 0x75FF: // FF 75 XX : push dword ptr [ebp + XX]
573 return 3;
574 case 0xC1F7: // F7 C1 XX YY ZZ WW : test ecx, WWZZYYXX
575 case 0x25FF: // FF 25 XX YY ZZ WW : jmp dword ptr ds:[WWZZYYXX]
576 return 6;
577 case 0x3D83: // 83 3D XX YY ZZ WW TT : cmp TT, WWZZYYXX
578 return 7;
579 case 0x7D83: // 83 7D XX YY : cmp dword ptr [ebp + XX], YY
580 return 4;
581 }
582
583 switch (0x00FFFFFF & *(u32*)address) {
584 case 0x24448A: // 8A 44 24 XX : mov eal, dword ptr [esp + XX]
585 case 0x24448B: // 8B 44 24 XX : mov eax, dword ptr [esp + XX]
586 case 0x244C8B: // 8B 4C 24 XX : mov ecx, dword ptr [esp + XX]
587 case 0x24548B: // 8B 54 24 XX : mov edx, dword ptr [esp + XX]
588 case 0x24748B: // 8B 74 24 XX : mov esi, dword ptr [esp + XX]
589 case 0x247C8B: // 8B 7C 24 XX : mov edi, dword ptr [esp + XX]
590 return 4;
591 }
592
593 switch (*(u32*)address) {
594 case 0x2444B60F: // 0F B6 44 24 XX : movzx eax, byte ptr [esp + XX]
595 return 5;
596 }
597 #endif
598
599 // Unknown instruction!
600 // FIXME: Unknown instruction failures might happen when we add a new
601 // interceptor or a new compiler version. In either case, they should result
602 // in visible and readable error messages. However, merely calling abort()
603 // leads to an infinite recursion in CheckFailed.
604 InterceptionFailed();
605 return 0;
606 }
607
608 // Returns 0 on error.
RoundUpToInstrBoundary(size_t size,uptr address)609 static size_t RoundUpToInstrBoundary(size_t size, uptr address) {
610 size_t cursor = 0;
611 while (cursor < size) {
612 size_t instruction_size = GetInstructionSize(address + cursor);
613 if (!instruction_size)
614 return 0;
615 cursor += instruction_size;
616 }
617 return cursor;
618 }
619
CopyInstructions(uptr to,uptr from,size_t size)620 static bool CopyInstructions(uptr to, uptr from, size_t size) {
621 size_t cursor = 0;
622 while (cursor != size) {
623 size_t rel_offset = 0;
624 size_t instruction_size = GetInstructionSize(from + cursor, &rel_offset);
625 _memcpy((void*)(to + cursor), (void*)(from + cursor),
626 (size_t)instruction_size);
627 if (rel_offset) {
628 uptr delta = to - from;
629 uptr relocated_offset = *(u32*)(to + cursor + rel_offset) - delta;
630 #if SANITIZER_WINDOWS64
631 if (relocated_offset + 0x80000000U >= 0xFFFFFFFFU)
632 return false;
633 #endif
634 *(u32*)(to + cursor + rel_offset) = relocated_offset;
635 }
636 cursor += instruction_size;
637 }
638 return true;
639 }
640
641
642 #if !SANITIZER_WINDOWS64
OverrideFunctionWithDetour(uptr old_func,uptr new_func,uptr * orig_old_func)643 bool OverrideFunctionWithDetour(
644 uptr old_func, uptr new_func, uptr *orig_old_func) {
645 const int kDetourHeaderLen = 5;
646 const u16 kDetourInstruction = 0xFF8B;
647
648 uptr header = (uptr)old_func - kDetourHeaderLen;
649 uptr patch_length = kDetourHeaderLen + kShortJumpInstructionLength;
650
651 // Validate that the function is hookable.
652 if (*(u16*)old_func != kDetourInstruction ||
653 !IsMemoryPadding(header, kDetourHeaderLen))
654 return false;
655
656 // Change memory protection to writable.
657 DWORD protection = 0;
658 if (!ChangeMemoryProtection(header, patch_length, &protection))
659 return false;
660
661 // Write a relative jump to the redirected function.
662 WriteJumpInstruction(header, new_func);
663
664 // Write the short jump to the function prefix.
665 WriteShortJumpInstruction(old_func, header);
666
667 // Restore previous memory protection.
668 if (!RestoreMemoryProtection(header, patch_length, protection))
669 return false;
670
671 if (orig_old_func)
672 *orig_old_func = old_func + kShortJumpInstructionLength;
673
674 return true;
675 }
676 #endif
677
OverrideFunctionWithRedirectJump(uptr old_func,uptr new_func,uptr * orig_old_func)678 bool OverrideFunctionWithRedirectJump(
679 uptr old_func, uptr new_func, uptr *orig_old_func) {
680 // Check whether the first instruction is a relative jump.
681 if (*(u8*)old_func != 0xE9)
682 return false;
683
684 if (orig_old_func) {
685 uptr relative_offset = *(u32*)(old_func + 1);
686 uptr absolute_target = old_func + relative_offset + kJumpInstructionLength;
687 *orig_old_func = absolute_target;
688 }
689
690 #if SANITIZER_WINDOWS64
691 // If needed, get memory space for a trampoline jump.
692 uptr trampoline = AllocateMemoryForTrampoline(old_func, kDirectBranchLength);
693 if (!trampoline)
694 return false;
695 WriteDirectBranch(trampoline, new_func);
696 #endif
697
698 // Change memory protection to writable.
699 DWORD protection = 0;
700 if (!ChangeMemoryProtection(old_func, kJumpInstructionLength, &protection))
701 return false;
702
703 // Write a relative jump to the redirected function.
704 WriteJumpInstruction(old_func, FIRST_32_SECOND_64(new_func, trampoline));
705
706 // Restore previous memory protection.
707 if (!RestoreMemoryProtection(old_func, kJumpInstructionLength, protection))
708 return false;
709
710 return true;
711 }
712
OverrideFunctionWithHotPatch(uptr old_func,uptr new_func,uptr * orig_old_func)713 bool OverrideFunctionWithHotPatch(
714 uptr old_func, uptr new_func, uptr *orig_old_func) {
715 const int kHotPatchHeaderLen = kBranchLength;
716
717 uptr header = (uptr)old_func - kHotPatchHeaderLen;
718 uptr patch_length = kHotPatchHeaderLen + kShortJumpInstructionLength;
719
720 // Validate that the function is hot patchable.
721 size_t instruction_size = GetInstructionSize(old_func);
722 if (instruction_size < kShortJumpInstructionLength ||
723 !FunctionHasPadding(old_func, kHotPatchHeaderLen))
724 return false;
725
726 if (orig_old_func) {
727 // Put the needed instructions into the trampoline bytes.
728 uptr trampoline_length = instruction_size + kDirectBranchLength;
729 uptr trampoline = AllocateMemoryForTrampoline(old_func, trampoline_length);
730 if (!trampoline)
731 return false;
732 if (!CopyInstructions(trampoline, old_func, instruction_size))
733 return false;
734 WriteDirectBranch(trampoline + instruction_size,
735 old_func + instruction_size);
736 *orig_old_func = trampoline;
737 }
738
739 // If needed, get memory space for indirect address.
740 uptr indirect_address = 0;
741 #if SANITIZER_WINDOWS64
742 indirect_address = AllocateMemoryForTrampoline(old_func, kAddressLength);
743 if (!indirect_address)
744 return false;
745 #endif
746
747 // Change memory protection to writable.
748 DWORD protection = 0;
749 if (!ChangeMemoryProtection(header, patch_length, &protection))
750 return false;
751
752 // Write jumps to the redirected function.
753 WriteBranch(header, indirect_address, new_func);
754 WriteShortJumpInstruction(old_func, header);
755
756 // Restore previous memory protection.
757 if (!RestoreMemoryProtection(header, patch_length, protection))
758 return false;
759
760 return true;
761 }
762
OverrideFunctionWithTrampoline(uptr old_func,uptr new_func,uptr * orig_old_func)763 bool OverrideFunctionWithTrampoline(
764 uptr old_func, uptr new_func, uptr *orig_old_func) {
765
766 size_t instructions_length = kBranchLength;
767 size_t padding_length = 0;
768 uptr indirect_address = 0;
769
770 if (orig_old_func) {
771 // Find out the number of bytes of the instructions we need to copy
772 // to the trampoline.
773 instructions_length = RoundUpToInstrBoundary(kBranchLength, old_func);
774 if (!instructions_length)
775 return false;
776
777 // Put the needed instructions into the trampoline bytes.
778 uptr trampoline_length = instructions_length + kDirectBranchLength;
779 uptr trampoline = AllocateMemoryForTrampoline(old_func, trampoline_length);
780 if (!trampoline)
781 return false;
782 if (!CopyInstructions(trampoline, old_func, instructions_length))
783 return false;
784 WriteDirectBranch(trampoline + instructions_length,
785 old_func + instructions_length);
786 *orig_old_func = trampoline;
787 }
788
789 #if SANITIZER_WINDOWS64
790 // Check if the targeted address can be encoded in the function padding.
791 // Otherwise, allocate it in the trampoline region.
792 if (IsMemoryPadding(old_func - kAddressLength, kAddressLength)) {
793 indirect_address = old_func - kAddressLength;
794 padding_length = kAddressLength;
795 } else {
796 indirect_address = AllocateMemoryForTrampoline(old_func, kAddressLength);
797 if (!indirect_address)
798 return false;
799 }
800 #endif
801
802 // Change memory protection to writable.
803 uptr patch_address = old_func - padding_length;
804 uptr patch_length = instructions_length + padding_length;
805 DWORD protection = 0;
806 if (!ChangeMemoryProtection(patch_address, patch_length, &protection))
807 return false;
808
809 // Patch the original function.
810 WriteBranch(old_func, indirect_address, new_func);
811
812 // Restore previous memory protection.
813 if (!RestoreMemoryProtection(patch_address, patch_length, protection))
814 return false;
815
816 return true;
817 }
818
OverrideFunction(uptr old_func,uptr new_func,uptr * orig_old_func)819 bool OverrideFunction(
820 uptr old_func, uptr new_func, uptr *orig_old_func) {
821 #if !SANITIZER_WINDOWS64
822 if (OverrideFunctionWithDetour(old_func, new_func, orig_old_func))
823 return true;
824 #endif
825 if (OverrideFunctionWithRedirectJump(old_func, new_func, orig_old_func))
826 return true;
827 if (OverrideFunctionWithHotPatch(old_func, new_func, orig_old_func))
828 return true;
829 if (OverrideFunctionWithTrampoline(old_func, new_func, orig_old_func))
830 return true;
831 return false;
832 }
833
InterestingDLLsAvailable()834 static void **InterestingDLLsAvailable() {
835 static const char *InterestingDLLs[] = {
836 "kernel32.dll",
837 "msvcr100.dll", // VS2010
838 "msvcr110.dll", // VS2012
839 "msvcr120.dll", // VS2013
840 "vcruntime140.dll", // VS2015
841 "ucrtbase.dll", // Universal CRT
842 // NTDLL should go last as it exports some functions that we should
843 // override in the CRT [presumably only used internally].
844 "ntdll.dll", NULL};
845 static void *result[ARRAY_SIZE(InterestingDLLs)] = { 0 };
846 if (!result[0]) {
847 for (size_t i = 0, j = 0; InterestingDLLs[i]; ++i) {
848 if (HMODULE h = GetModuleHandleA(InterestingDLLs[i]))
849 result[j++] = (void *)h;
850 }
851 }
852 return &result[0];
853 }
854
855 namespace {
856 // Utility for reading loaded PE images.
857 template <typename T> class RVAPtr {
858 public:
RVAPtr(void * module,uptr rva)859 RVAPtr(void *module, uptr rva)
860 : ptr_(reinterpret_cast<T *>(reinterpret_cast<char *>(module) + rva)) {}
operator T*()861 operator T *() { return ptr_; }
operator ->()862 T *operator->() { return ptr_; }
operator ++()863 T *operator++() { return ++ptr_; }
864
865 private:
866 T *ptr_;
867 };
868 } // namespace
869
870 // Internal implementation of GetProcAddress. At least since Windows 8,
871 // GetProcAddress appears to initialize DLLs before returning function pointers
872 // into them. This is problematic for the sanitizers, because they typically
873 // want to intercept malloc *before* MSVCRT initializes. Our internal
874 // implementation walks the export list manually without doing initialization.
InternalGetProcAddress(void * module,const char * func_name)875 uptr InternalGetProcAddress(void *module, const char *func_name) {
876 // Check that the module header is full and present.
877 RVAPtr<IMAGE_DOS_HEADER> dos_stub(module, 0);
878 RVAPtr<IMAGE_NT_HEADERS> headers(module, dos_stub->e_lfanew);
879 if (!module || dos_stub->e_magic != IMAGE_DOS_SIGNATURE || // "MZ"
880 headers->Signature != IMAGE_NT_SIGNATURE || // "PE\0\0"
881 headers->FileHeader.SizeOfOptionalHeader <
882 sizeof(IMAGE_OPTIONAL_HEADER)) {
883 return 0;
884 }
885
886 IMAGE_DATA_DIRECTORY *export_directory =
887 &headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
888 if (export_directory->Size == 0)
889 return 0;
890 RVAPtr<IMAGE_EXPORT_DIRECTORY> exports(module,
891 export_directory->VirtualAddress);
892 RVAPtr<DWORD> functions(module, exports->AddressOfFunctions);
893 RVAPtr<DWORD> names(module, exports->AddressOfNames);
894 RVAPtr<WORD> ordinals(module, exports->AddressOfNameOrdinals);
895
896 for (DWORD i = 0; i < exports->NumberOfNames; i++) {
897 RVAPtr<char> name(module, names[i]);
898 if (!strcmp(func_name, name)) {
899 DWORD index = ordinals[i];
900 RVAPtr<char> func(module, functions[index]);
901
902 // Handle forwarded functions.
903 DWORD offset = functions[index];
904 if (offset >= export_directory->VirtualAddress &&
905 offset < export_directory->VirtualAddress + export_directory->Size) {
906 // An entry for a forwarded function is a string with the following
907 // format: "<module> . <function_name>" that is stored into the
908 // exported directory.
909 char function_name[256];
910 size_t funtion_name_length = _strlen(func);
911 if (funtion_name_length >= sizeof(function_name) - 1)
912 InterceptionFailed();
913
914 _memcpy(function_name, func, funtion_name_length);
915 function_name[funtion_name_length] = '\0';
916 char* separator = _strchr(function_name, '.');
917 if (!separator)
918 InterceptionFailed();
919 *separator = '\0';
920
921 void* redirected_module = GetModuleHandleA(function_name);
922 if (!redirected_module)
923 InterceptionFailed();
924 return InternalGetProcAddress(redirected_module, separator + 1);
925 }
926
927 return (uptr)(char *)func;
928 }
929 }
930
931 return 0;
932 }
933
OverrideFunction(const char * func_name,uptr new_func,uptr * orig_old_func)934 bool OverrideFunction(
935 const char *func_name, uptr new_func, uptr *orig_old_func) {
936 bool hooked = false;
937 void **DLLs = InterestingDLLsAvailable();
938 for (size_t i = 0; DLLs[i]; ++i) {
939 uptr func_addr = InternalGetProcAddress(DLLs[i], func_name);
940 if (func_addr &&
941 OverrideFunction(func_addr, new_func, orig_old_func)) {
942 hooked = true;
943 }
944 }
945 return hooked;
946 }
947
OverrideImportedFunction(const char * module_to_patch,const char * imported_module,const char * function_name,uptr new_function,uptr * orig_old_func)948 bool OverrideImportedFunction(const char *module_to_patch,
949 const char *imported_module,
950 const char *function_name, uptr new_function,
951 uptr *orig_old_func) {
952 HMODULE module = GetModuleHandleA(module_to_patch);
953 if (!module)
954 return false;
955
956 // Check that the module header is full and present.
957 RVAPtr<IMAGE_DOS_HEADER> dos_stub(module, 0);
958 RVAPtr<IMAGE_NT_HEADERS> headers(module, dos_stub->e_lfanew);
959 if (!module || dos_stub->e_magic != IMAGE_DOS_SIGNATURE || // "MZ"
960 headers->Signature != IMAGE_NT_SIGNATURE || // "PE\0\0"
961 headers->FileHeader.SizeOfOptionalHeader <
962 sizeof(IMAGE_OPTIONAL_HEADER)) {
963 return false;
964 }
965
966 IMAGE_DATA_DIRECTORY *import_directory =
967 &headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
968
969 // Iterate the list of imported DLLs. FirstThunk will be null for the last
970 // entry.
971 RVAPtr<IMAGE_IMPORT_DESCRIPTOR> imports(module,
972 import_directory->VirtualAddress);
973 for (; imports->FirstThunk != 0; ++imports) {
974 RVAPtr<const char> modname(module, imports->Name);
975 if (_stricmp(&*modname, imported_module) == 0)
976 break;
977 }
978 if (imports->FirstThunk == 0)
979 return false;
980
981 // We have two parallel arrays: the import address table (IAT) and the table
982 // of names. They start out containing the same data, but the loader rewrites
983 // the IAT to hold imported addresses and leaves the name table in
984 // OriginalFirstThunk alone.
985 RVAPtr<IMAGE_THUNK_DATA> name_table(module, imports->OriginalFirstThunk);
986 RVAPtr<IMAGE_THUNK_DATA> iat(module, imports->FirstThunk);
987 for (; name_table->u1.Ordinal != 0; ++name_table, ++iat) {
988 if (!IMAGE_SNAP_BY_ORDINAL(name_table->u1.Ordinal)) {
989 RVAPtr<IMAGE_IMPORT_BY_NAME> import_by_name(
990 module, name_table->u1.ForwarderString);
991 const char *funcname = &import_by_name->Name[0];
992 if (strcmp(funcname, function_name) == 0)
993 break;
994 }
995 }
996 if (name_table->u1.Ordinal == 0)
997 return false;
998
999 // Now we have the correct IAT entry. Do the swap. We have to make the page
1000 // read/write first.
1001 if (orig_old_func)
1002 *orig_old_func = iat->u1.AddressOfData;
1003 DWORD old_prot, unused_prot;
1004 if (!VirtualProtect(&iat->u1.AddressOfData, 4, PAGE_EXECUTE_READWRITE,
1005 &old_prot))
1006 return false;
1007 iat->u1.AddressOfData = new_function;
1008 if (!VirtualProtect(&iat->u1.AddressOfData, 4, old_prot, &unused_prot))
1009 return false; // Not clear if this failure bothers us.
1010 return true;
1011 }
1012
1013 } // namespace __interception
1014
1015 #endif // SANITIZER_MAC
1016