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