resolver_64.cc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "sandbox/win/src/resolver.h"
  5. #include <stddef.h>
  6. // For placement new. This file must not depend on the CRT at runtime, but
  7. // placement operator new is inline.
  8. #include <new>
  9. #include "sandbox/win/src/sandbox_nt_util.h"
  10. namespace {
  11. #if defined(_M_X64)
  12. const USHORT kMovRax = 0xB848;
  13. const USHORT kJmpRax = 0xe0ff;
  14. #pragma pack(push, 1)
  15. struct InternalThunk {
  16. // This struct contains roughly the following code:
  17. // 01 48b8f0debc9a78563412 mov rax,123456789ABCDEF0h
  18. // ff e0 jmp rax
  19. //
  20. // The code modifies rax, but that's fine for x64 ABI.
  21. InternalThunk() {
  22. mov_rax = kMovRax;
  23. jmp_rax = kJmpRax;
  24. interceptor_function = 0;
  25. }
  26. USHORT mov_rax; // = 48 B8
  27. ULONG_PTR interceptor_function;
  28. USHORT jmp_rax; // = ff e0
  29. };
  30. #pragma pack(pop)
  31. #elif defined(_M_ARM64)
  32. const ULONG kLdrX16Pc4 = 0x58000050;
  33. const ULONG kBrX16 = 0xD61F0200;
  34. #pragma pack(push, 4)
  35. struct InternalThunk {
  36. // This struct contains roughly the following code:
  37. // 00 58000050 ldr x16, pc+4
  38. // 04 D61F0200 br x16
  39. // 08 123456789ABCDEF0H
  40. InternalThunk() {
  41. ldr_x16_pc4 = kLdrX16Pc4;
  42. br_x16 = kBrX16;
  43. interceptor_function = 0;
  44. }
  45. ULONG ldr_x16_pc4;
  46. ULONG br_x16;
  47. ULONG_PTR interceptor_function;
  48. };
  49. #pragma pack(pop)
  50. #else
  51. #error "Unsupported Windows 64-bit Arch"
  52. #endif
  53. } // namespace.
  54. namespace sandbox {
  55. size_t ResolverThunk::GetInternalThunkSize() const {
  56. return sizeof(InternalThunk);
  57. }
  58. bool ResolverThunk::SetInternalThunk(void* storage,
  59. size_t storage_bytes,
  60. const void* original_function,
  61. const void* interceptor) {
  62. if (storage_bytes < sizeof(InternalThunk))
  63. return false;
  64. InternalThunk* thunk = new (storage) InternalThunk;
  65. thunk->interceptor_function = reinterpret_cast<ULONG_PTR>(interceptor);
  66. return true;
  67. }
  68. NTSTATUS ResolverThunk::ResolveTarget(const void* module,
  69. const char* function_name,
  70. void** address) {
  71. // We don't support sidestep & co.
  72. return STATUS_NOT_IMPLEMENTED;
  73. }
  74. } // namespace sandbox