OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sandbox/win/src/service_resolver.h" | 5 #include "sandbox/win/src/service_resolver.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "sandbox/win/src/sandbox_nt_util.h" | 8 #include "sandbox/win/src/sandbox_nt_util.h" |
9 #include "sandbox/win/src/win_utils.h" | 9 #include "sandbox/win/src/win_utils.h" |
10 | 10 |
11 namespace { | 11 namespace { |
12 #pragma pack(push, 1) | 12 #pragma pack(push, 1) |
13 | 13 |
14 const ULONG kMmovR10EcxMovEax = 0xB8D18B4C; | 14 const ULONG kMmovR10EcxMovEax = 0xB8D18B4C; |
15 const USHORT kSyscall = 0x050F; | 15 const USHORT kSyscall = 0x050F; |
16 const BYTE kRetNp = 0xC3; | 16 const BYTE kRetNp = 0xC3; |
17 const ULONG64 kMov1 = 0x54894808244C8948; | 17 const ULONG64 kMov1 = 0x54894808244C8948; |
18 const ULONG64 kMov2 = 0x4C182444894C1024; | 18 const ULONG64 kMov2 = 0x4C182444894C1024; |
19 const ULONG kMov3 = 0x20244C89; | 19 const ULONG kMov3 = 0x20244C89; |
| 20 const USHORT kTestByte = 0x04F6; |
| 21 const BYTE kPtr = 0x25; |
| 22 const BYTE kRet = 0xC3; |
| 23 const USHORT kJne = 0x0375; |
20 | 24 |
21 // Service code for 64 bit systems. | 25 // Service code for 64 bit systems. |
22 struct ServiceEntry { | 26 struct ServiceEntry { |
23 // This struct contains roughly the following code: | 27 // This struct contains roughly the following code: |
24 // 00 mov r10,rcx | 28 // 00 mov r10,rcx |
25 // 03 mov eax,52h | 29 // 03 mov eax,52h |
26 // 08 syscall | 30 // 08 syscall |
27 // 0a ret | 31 // 0a ret |
28 // 0b xchg ax,ax | 32 // 0b xchg ax,ax |
29 // 0e xchg ax,ax | 33 // 0e xchg ax,ax |
(...skipping 23 matching lines...) Expand all Loading... |
53 ULONG64 mov_1; // = 48 89 4C 24 08 48 89 54 | 57 ULONG64 mov_1; // = 48 89 4C 24 08 48 89 54 |
54 ULONG64 mov_2; // = 24 10 4C 89 44 24 18 4C | 58 ULONG64 mov_2; // = 24 10 4C 89 44 24 18 4C |
55 ULONG mov_3; // = 89 4C 24 20 | 59 ULONG mov_3; // = 89 4C 24 20 |
56 ULONG mov_r10_rcx_mov_eax; // = 4C 8B D1 B8 | 60 ULONG mov_r10_rcx_mov_eax; // = 4C 8B D1 B8 |
57 ULONG service_id; | 61 ULONG service_id; |
58 USHORT syscall; // = 0F 05 | 62 USHORT syscall; // = 0F 05 |
59 BYTE ret; // = C3 | 63 BYTE ret; // = C3 |
60 BYTE nop; // = 90 | 64 BYTE nop; // = 90 |
61 }; | 65 }; |
62 | 66 |
| 67 // Service code for 64 bit systems with int 2e fallback. |
| 68 struct ServiceEntryWithInt2E { |
| 69 // This struct contains roughly the following code: |
| 70 // 00 4c8bd1 mov r10,rcx |
| 71 // 03 b855000000 mov eax,52h |
| 72 // 08 f604250803fe7f01 test byte ptr SharedUserData!308, 1 |
| 73 // 10 7503 jne [over syscall] |
| 74 // 12 0f05 syscall |
| 75 // 14 c3 ret |
| 76 // 15 cd2e int 2e |
| 77 // 17 c3 ret |
| 78 |
| 79 ULONG mov_r10_rcx_mov_eax; // = 4C 8B D1 B8 |
| 80 ULONG service_id; |
| 81 USHORT test_byte; // = F6 04 |
| 82 BYTE ptr; // = 25 |
| 83 ULONG user_shared_data_ptr; |
| 84 BYTE one; // = 01 |
| 85 USHORT jne_over_syscall; // = 75 03 |
| 86 USHORT syscall; // = 0F 05 |
| 87 BYTE ret; // = C3 |
| 88 USHORT int2e; // = CD 2E |
| 89 BYTE ret2; // = C3 |
| 90 }; |
| 91 |
63 // We don't have an internal thunk for x64. | 92 // We don't have an internal thunk for x64. |
64 struct ServiceFullThunk { | 93 struct ServiceFullThunk { |
65 union { | 94 union { |
66 ServiceEntry original; | 95 ServiceEntry original; |
67 ServiceEntryW8 original_w8; | 96 ServiceEntryW8 original_w8; |
| 97 ServiceEntryWithInt2E original_int2e_fallback; |
68 }; | 98 }; |
69 }; | 99 }; |
70 | 100 |
71 #pragma pack(pop) | 101 #pragma pack(pop) |
72 | 102 |
73 bool IsService(const void* source) { | 103 bool IsService(const void* source) { |
74 const ServiceEntry* service = | 104 const ServiceEntry* service = |
75 reinterpret_cast<const ServiceEntry*>(source); | 105 reinterpret_cast<const ServiceEntry*>(source); |
76 | 106 |
77 return (kMmovR10EcxMovEax == service->mov_r10_rcx_mov_eax && | 107 return (kMmovR10EcxMovEax == service->mov_r10_rcx_mov_eax && |
78 kSyscall == service->syscall && kRetNp == service->ret); | 108 kSyscall == service->syscall && kRetNp == service->ret); |
79 } | 109 } |
80 | 110 |
| 111 bool IsServiceW8(const void* source) { |
| 112 const ServiceEntryW8* service = |
| 113 reinterpret_cast<const ServiceEntryW8*>(source); |
| 114 |
| 115 return (kMmovR10EcxMovEax == service->mov_r10_rcx_mov_eax && |
| 116 kMov1 == service->mov_1 && kMov2 == service->mov_2 && |
| 117 kMov3 == service->mov_3); |
| 118 } |
| 119 |
| 120 bool IsServiceWithInt2E(const void* source) { |
| 121 const ServiceEntryWithInt2E* service = |
| 122 reinterpret_cast<const ServiceEntryWithInt2E*>(source); |
| 123 |
| 124 return (kMmovR10EcxMovEax == service->mov_r10_rcx_mov_eax && |
| 125 kTestByte == service->test_byte && kPtr == service->ptr && |
| 126 kJne == service->jne_over_syscall && kSyscall == service->syscall && |
| 127 kRet == service->ret && kRet == service->ret2); |
| 128 } |
| 129 |
81 }; // namespace | 130 }; // namespace |
82 | 131 |
83 namespace sandbox { | 132 namespace sandbox { |
84 | 133 |
85 NTSTATUS ServiceResolverThunk::Setup(const void* target_module, | 134 NTSTATUS ServiceResolverThunk::Setup(const void* target_module, |
86 const void* interceptor_module, | 135 const void* interceptor_module, |
87 const char* target_name, | 136 const char* target_name, |
88 const char* interceptor_name, | 137 const char* interceptor_name, |
89 const void* interceptor_entry_point, | 138 const void* interceptor_entry_point, |
90 void* thunk_storage, | 139 void* thunk_storage, |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 bool ServiceResolverThunk::IsFunctionAService(void* local_thunk) const { | 192 bool ServiceResolverThunk::IsFunctionAService(void* local_thunk) const { |
144 ServiceFullThunk function_code; | 193 ServiceFullThunk function_code; |
145 SIZE_T read; | 194 SIZE_T read; |
146 if (!::ReadProcessMemory(process_, target_, &function_code, | 195 if (!::ReadProcessMemory(process_, target_, &function_code, |
147 sizeof(function_code), &read)) | 196 sizeof(function_code), &read)) |
148 return false; | 197 return false; |
149 | 198 |
150 if (sizeof(function_code) != read) | 199 if (sizeof(function_code) != read) |
151 return false; | 200 return false; |
152 | 201 |
153 if (!IsService(&function_code)) { | 202 if (!IsService(&function_code) && !IsServiceW8(&function_code) && |
154 // See if it's the Win8 signature. | 203 !IsServiceWithInt2E(&function_code)) |
155 ServiceEntryW8* w8_service = &function_code.original_w8; | 204 return false; |
156 if (!IsService(&w8_service->mov_r10_rcx_mov_eax) || | |
157 w8_service->mov_1 != kMov1 || w8_service->mov_2 != kMov2 || | |
158 w8_service->mov_3 != kMov3) { | |
159 return false; | |
160 } | |
161 } | |
162 | 205 |
163 // Save the verified code. | 206 // Save the verified code. |
164 memcpy(local_thunk, &function_code, sizeof(function_code)); | 207 memcpy(local_thunk, &function_code, sizeof(function_code)); |
165 | 208 |
166 return true; | 209 return true; |
167 } | 210 } |
168 | 211 |
169 NTSTATUS ServiceResolverThunk::PerformPatch(void* local_thunk, | 212 NTSTATUS ServiceResolverThunk::PerformPatch(void* local_thunk, |
170 void* remote_thunk) { | 213 void* remote_thunk) { |
171 // Patch the original code. | 214 // Patch the original code. |
(...skipping 26 matching lines...) Expand all Loading... |
198 | 241 |
199 return STATUS_SUCCESS; | 242 return STATUS_SUCCESS; |
200 } | 243 } |
201 | 244 |
202 bool Wow64ResolverThunk::IsFunctionAService(void* local_thunk) const { | 245 bool Wow64ResolverThunk::IsFunctionAService(void* local_thunk) const { |
203 NOTREACHED_NT(); | 246 NOTREACHED_NT(); |
204 return false; | 247 return false; |
205 } | 248 } |
206 | 249 |
207 } // namespace sandbox | 250 } // namespace sandbox |
OLD | NEW |