interception_win.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #ifdef _WIN32
  13. #include "interception.h"
  14. #include <windows.h>
  15. namespace __interception {
  16. // FIXME: internal_str* and internal_mem* functions should be moved from the
  17. // ASan sources into interception/.
  18. static void _memset(void *p, int value, size_t sz) {
  19. for (size_t i = 0; i < sz; ++i)
  20. ((char*)p)[i] = (char)value;
  21. }
  22. static void _memcpy(void *dst, void *src, size_t sz) {
  23. char *dst_c = (char*)dst,
  24. *src_c = (char*)src;
  25. for (size_t i = 0; i < sz; ++i)
  26. dst_c[i] = src_c[i];
  27. }
  28. static void WriteJumpInstruction(char *jmp_from, char *to) {
  29. // jmp XXYYZZWW = E9 WW ZZ YY XX, where XXYYZZWW is an offset fromt jmp_from
  30. // to the next instruction to the destination.
  31. ptrdiff_t offset = to - jmp_from - 5;
  32. *jmp_from = '\xE9';
  33. *(ptrdiff_t*)(jmp_from + 1) = offset;
  34. }
  35. static char *GetMemoryForTrampoline(size_t size) {
  36. // Trampolines are allocated from a common pool.
  37. const int POOL_SIZE = 1024;
  38. static char *pool = NULL;
  39. static size_t pool_used = 0;
  40. if (!pool) {
  41. pool = (char *)VirtualAlloc(NULL, POOL_SIZE, MEM_RESERVE | MEM_COMMIT,
  42. PAGE_EXECUTE_READWRITE);
  43. // FIXME: Might want to apply PAGE_EXECUTE_READ access after all the
  44. // interceptors are in place.
  45. if (!pool)
  46. return NULL;
  47. _memset(pool, 0xCC /* int 3 */, POOL_SIZE);
  48. }
  49. if (pool_used + size > POOL_SIZE)
  50. return NULL;
  51. char *ret = pool + pool_used;
  52. pool_used += size;
  53. return ret;
  54. }
  55. // Returns 0 on error.
  56. static size_t RoundUpToInstrBoundary(size_t size, char *code) {
  57. size_t cursor = 0;
  58. while (cursor < size) {
  59. switch (code[cursor]) {
  60. case '\x51': // push ecx
  61. case '\x52': // push edx
  62. case '\x53': // push ebx
  63. case '\x54': // push esp
  64. case '\x55': // push ebp
  65. case '\x56': // push esi
  66. case '\x57': // push edi
  67. case '\x5D': // pop ebp
  68. cursor++;
  69. continue;
  70. case '\x6A': // 6A XX = push XX
  71. cursor += 2;
  72. continue;
  73. case '\xE9': // E9 XX YY ZZ WW = jmp WWZZYYXX
  74. cursor += 5;
  75. continue;
  76. }
  77. switch (*(unsigned short*)(code + cursor)) { // NOLINT
  78. case 0xFF8B: // 8B FF = mov edi, edi
  79. case 0xEC8B: // 8B EC = mov ebp, esp
  80. case 0xC033: // 33 C0 = xor eax, eax
  81. cursor += 2;
  82. continue;
  83. case 0x458B: // 8B 45 XX = mov eax, dword ptr [ebp+XXh]
  84. case 0x5D8B: // 8B 5D XX = mov ebx, dword ptr [ebp+XXh]
  85. case 0xEC83: // 83 EC XX = sub esp, XX
  86. case 0x75FF: // FF 75 XX = push dword ptr [ebp+XXh]
  87. cursor += 3;
  88. continue;
  89. case 0xC1F7: // F7 C1 XX YY ZZ WW = test ecx, WWZZYYXX
  90. case 0x25FF: // FF 25 XX YY ZZ WW = jmp dword ptr ds:[WWZZYYXX]
  91. cursor += 6;
  92. continue;
  93. case 0x3D83: // 83 3D XX YY ZZ WW TT = cmp TT, WWZZYYXX
  94. cursor += 7;
  95. continue;
  96. }
  97. switch (0x00FFFFFF & *(unsigned int*)(code + cursor)) {
  98. case 0x24448A: // 8A 44 24 XX = mov eal, dword ptr [esp+XXh]
  99. case 0x24448B: // 8B 44 24 XX = mov eax, dword ptr [esp+XXh]
  100. case 0x244C8B: // 8B 4C 24 XX = mov ecx, dword ptr [esp+XXh]
  101. case 0x24548B: // 8B 54 24 XX = mov edx, dword ptr [esp+XXh]
  102. case 0x24748B: // 8B 74 24 XX = mov esi, dword ptr [esp+XXh]
  103. case 0x247C8B: // 8B 7C 24 XX = mov edi, dword ptr [esp+XXh]
  104. cursor += 4;
  105. continue;
  106. }
  107. // Unknown instruction!
  108. // FIXME: Unknown instruction failures might happen when we add a new
  109. // interceptor or a new compiler version. In either case, they should result
  110. // in visible and readable error messages. However, merely calling abort()
  111. // leads to an infinite recursion in CheckFailed.
  112. // Do we have a good way to abort with an error message here?
  113. __debugbreak();
  114. return 0;
  115. }
  116. return cursor;
  117. }
  118. bool OverrideFunction(uptr old_func, uptr new_func, uptr *orig_old_func) {
  119. #ifdef _WIN64
  120. #error OverrideFunction is not yet supported on x64
  121. #endif
  122. // Function overriding works basically like this:
  123. // We write "jmp <new_func>" (5 bytes) at the beginning of the 'old_func'
  124. // to override it.
  125. // We might want to be able to execute the original 'old_func' from the
  126. // wrapper, in this case we need to keep the leading 5+ bytes ('head')
  127. // of the original code somewhere with a "jmp <old_func+head>".
  128. // We call these 'head'+5 bytes of instructions a "trampoline".
  129. char *old_bytes = (char *)old_func;
  130. // We'll need at least 5 bytes for a 'jmp'.
  131. size_t head = 5;
  132. if (orig_old_func) {
  133. // Find out the number of bytes of the instructions we need to copy
  134. // to the trampoline and store it in 'head'.
  135. head = RoundUpToInstrBoundary(head, old_bytes);
  136. if (!head)
  137. return false;
  138. // Put the needed instructions into the trampoline bytes.
  139. char *trampoline = GetMemoryForTrampoline(head + 5);
  140. if (!trampoline)
  141. return false;
  142. _memcpy(trampoline, old_bytes, head);
  143. WriteJumpInstruction(trampoline + head, old_bytes + head);
  144. *orig_old_func = (uptr)trampoline;
  145. }
  146. // Now put the "jmp <new_func>" instruction at the original code location.
  147. // We should preserve the EXECUTE flag as some of our own code might be
  148. // located in the same page (sic!). FIXME: might consider putting the
  149. // __interception code into a separate section or something?
  150. DWORD old_prot, unused_prot;
  151. if (!VirtualProtect((void *)old_bytes, head, PAGE_EXECUTE_READWRITE,
  152. &old_prot))
  153. return false;
  154. WriteJumpInstruction(old_bytes, (char *)new_func);
  155. _memset(old_bytes + 5, 0xCC /* int 3 */, head - 5);
  156. // Restore the original permissions.
  157. if (!VirtualProtect((void *)old_bytes, head, old_prot, &unused_prot))
  158. return false; // not clear if this failure bothers us.
  159. return true;
  160. }
  161. static const void **InterestingDLLsAvailable() {
  162. const char *InterestingDLLs[] = {"kernel32.dll",
  163. "msvcr110.dll", // VS2012
  164. "msvcr120.dll", // VS2013
  165. NULL};
  166. static void *result[ARRAY_SIZE(InterestingDLLs)] = { 0 };
  167. if (!result[0]) {
  168. for (size_t i = 0, j = 0; InterestingDLLs[i]; ++i) {
  169. if (HMODULE h = GetModuleHandleA(InterestingDLLs[i]))
  170. result[j++] = (void *)h;
  171. }
  172. }
  173. return (const void **)&result[0];
  174. }
  175. static bool GetFunctionAddressInDLLs(const char *func_name, uptr *func_addr) {
  176. *func_addr = 0;
  177. const void **DLLs = InterestingDLLsAvailable();
  178. for (size_t i = 0; *func_addr == 0 && DLLs[i]; ++i)
  179. *func_addr = (uptr)GetProcAddress((HMODULE)DLLs[i], func_name);
  180. return (*func_addr != 0);
  181. }
  182. bool OverrideFunction(const char *name, uptr new_func, uptr *orig_old_func) {
  183. uptr orig_func;
  184. if (!GetFunctionAddressInDLLs(name, &orig_func))
  185. return false;
  186. return OverrideFunction(orig_func, new_func, orig_old_func);
  187. }
  188. } // namespace __interception
  189. #endif // _WIN32