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