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