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
3 // found in the LICENSE file.
5 #include "sandbox/win/src/service_resolver.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "sandbox/win/src/win_utils.h"
13 const BYTE kMovEax
= 0xB8;
14 const BYTE kMovEdx
= 0xBA;
15 const USHORT kMovEdxEsp
= 0xD48B;
16 const USHORT kCallPtrEdx
= 0x12FF;
17 const USHORT kCallEdx
= 0xD2FF;
18 const BYTE kCallEip
= 0xE8;
19 const BYTE kRet
= 0xC2;
20 const BYTE kRet2
= 0xC3;
21 const BYTE kNop
= 0x90;
22 const USHORT kJmpEdx
= 0xE2FF;
23 const USHORT kXorEcx
= 0xC933;
24 const ULONG kLeaEdx
= 0x0424548D;
25 const ULONG kCallFs1
= 0xC015FF64;
26 const USHORT kCallFs2
= 0;
27 const BYTE kCallFs3
= 0;
28 const BYTE kAddEsp1
= 0x83;
29 const USHORT kAddEsp2
= 0x4C4;
30 const BYTE kJmp32
= 0xE9;
31 const USHORT kSysenter
= 0x340F;
33 const int kMaxService
= 1000;
35 // Service code for 32 bit systems.
36 // NOTE: on win2003 "call dword ptr [edx]" is "call edx".
38 // This struct contains roughly the following code:
40 // 05 mov edx,offset SharedUserData!SystemCallStub (7ffe0300)
41 // 0a call dword ptr [edx]
48 USHORT call_ptr_edx
; // = FF 12
54 // Service code for 32 bit Windows 8.
55 struct ServiceEntryW8
{
56 // This struct contains the following code:
57 // 00 b825000000 mov eax,25h
58 // 05 e803000000 call eip+3
60 // 0d 8bd4 mov edx,esp
63 // 12 8bff mov edi,edi
66 BYTE call_eip
; // = E8
70 USHORT mov_edx_esp
; // = BD D4
71 USHORT sysenter
; // = 0F 34
76 // Service code for a 32 bit process running on a 64 bit os.
78 // This struct may contain one of two versions of code:
79 // 1. For XP, Vista and 2K3:
80 // 00 b825000000 mov eax, 25h
81 // 05 33c9 xor ecx, ecx
82 // 07 8d542404 lea edx, [esp + 4]
83 // 0b 64ff15c0000000 call dword ptr fs:[0C0h]
87 // 00 b825000000 mov eax, 25h
88 // 05 33c9 xor ecx, ecx
89 // 07 8d542404 lea edx, [esp + 4]
90 // 0b 64ff15c0000000 call dword ptr fs:[0C0h]
91 // 12 83c404 add esp, 4
94 // So we base the structure on the bigger one:
97 USHORT xor_ecx
; // = 33 C9
98 ULONG lea_edx
; // = 8D 54 24 04
99 ULONG call_fs1
; // = 64 FF 15 C0
100 USHORT call_fs2
; // = 00 00
101 BYTE call_fs3
; // = 00
102 BYTE add_esp1
; // = 83 or ret
103 USHORT add_esp2
; // = C4 04 or num_params
108 // Service code for a 32 bit process running on 64 bit Windows 8.
109 struct Wow64EntryW8
{
110 // 00 b825000000 mov eax, 25h
111 // 05 64ff15c0000000 call dword ptr fs:[0C0h]
114 BYTE mov_eax
; // = B8
116 ULONG call_fs1
; // = 64 FF 15 C0
117 USHORT call_fs2
; // = 00 00
118 BYTE call_fs3
; // = 00
124 // Make sure that relaxed patching works as expected.
125 const size_t kMinServiceSize
= offsetof(ServiceEntry
, ret
);
126 static_assert(sizeof(ServiceEntryW8
) >= kMinServiceSize
,
127 "wrong service length");
128 static_assert(sizeof(Wow64Entry
) >= kMinServiceSize
, "wrong service length");
129 static_assert(sizeof(Wow64EntryW8
) >= kMinServiceSize
, "wrong service length");
131 struct ServiceFullThunk
{
133 ServiceEntry original
;
134 ServiceEntryW8 original_w8
;
136 Wow64EntryW8 wow_64_w8
;
138 int internal_thunk
; // Dummy member to the beginning of the internal thunk.
147 NTSTATUS
ServiceResolverThunk::Setup(const void* target_module
,
148 const void* interceptor_module
,
149 const char* target_name
,
150 const char* interceptor_name
,
151 const void* interceptor_entry_point
,
153 size_t storage_bytes
,
154 size_t* storage_used
) {
155 NTSTATUS ret
= Init(target_module
, interceptor_module
, target_name
,
156 interceptor_name
, interceptor_entry_point
,
157 thunk_storage
, storage_bytes
);
158 if (!NT_SUCCESS(ret
))
162 size_t thunk_bytes
= GetThunkSize();
163 scoped_ptr
<char[]> thunk_buffer(new char[thunk_bytes
]);
164 ServiceFullThunk
* thunk
= reinterpret_cast<ServiceFullThunk
*>(
167 if (!IsFunctionAService(&thunk
->original
) &&
168 (!relaxed_
|| !SaveOriginalFunction(&thunk
->original
, thunk_storage
)))
169 return STATUS_UNSUCCESSFUL
;
171 ret
= PerformPatch(thunk
, thunk_storage
);
173 if (NULL
!= storage_used
)
174 *storage_used
= thunk_bytes
;
179 size_t ServiceResolverThunk::GetThunkSize() const {
180 return offsetof(ServiceFullThunk
, internal_thunk
) + GetInternalThunkSize();
183 NTSTATUS
ServiceResolverThunk::CopyThunk(const void* target_module
,
184 const char* target_name
,
186 size_t storage_bytes
,
187 size_t* storage_used
) {
188 NTSTATUS ret
= ResolveTarget(target_module
, target_name
, &target_
);
189 if (!NT_SUCCESS(ret
))
192 size_t thunk_bytes
= GetThunkSize();
193 if (storage_bytes
< thunk_bytes
)
194 return STATUS_UNSUCCESSFUL
;
196 ServiceFullThunk
* thunk
= reinterpret_cast<ServiceFullThunk
*>(thunk_storage
);
198 if (!IsFunctionAService(&thunk
->original
) &&
199 (!relaxed_
|| !SaveOriginalFunction(&thunk
->original
, thunk_storage
))) {
200 return STATUS_UNSUCCESSFUL
;
203 if (NULL
!= storage_used
)
204 *storage_used
= thunk_bytes
;
209 bool ServiceResolverThunk::IsFunctionAService(void* local_thunk
) const {
210 ServiceEntry function_code
;
212 if (!::ReadProcessMemory(process_
, target_
, &function_code
,
213 sizeof(function_code
), &read
))
216 if (sizeof(function_code
) != read
)
219 if (kMovEax
!= function_code
.mov_eax
||
220 kMovEdx
!= function_code
.mov_edx
||
221 (kCallPtrEdx
!= function_code
.call_ptr_edx
&&
222 kCallEdx
!= function_code
.call_ptr_edx
) ||
223 kRet
!= function_code
.ret
)
226 // Find the system call pointer if we don't already have it.
227 if (kCallEdx
!= function_code
.call_ptr_edx
) {
228 DWORD ki_system_call
;
229 if (!::ReadProcessMemory(process_
,
230 bit_cast
<const void*>(function_code
.stub
),
231 &ki_system_call
, sizeof(ki_system_call
), &read
))
234 if (sizeof(ki_system_call
) != read
)
237 HMODULE module_1
, module_2
;
238 // last check, call_stub should point to a KiXXSystemCall function on ntdll
239 if (!GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
|
240 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
,
241 bit_cast
<const wchar_t*>(ki_system_call
), &module_1
))
244 if (NULL
!= ntdll_base_
) {
245 // This path is only taken when running the unit tests. We want to be
246 // able to patch a buffer in memory, so target_ is not inside ntdll.
247 module_2
= ntdll_base_
;
249 if (!GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
|
250 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
,
251 reinterpret_cast<const wchar_t*>(target_
),
256 if (module_1
!= module_2
)
260 // Save the verified code
261 memcpy(local_thunk
, &function_code
, sizeof(function_code
));
266 NTSTATUS
ServiceResolverThunk::PerformPatch(void* local_thunk
,
267 void* remote_thunk
) {
268 ServiceEntry intercepted_code
;
269 size_t bytes_to_write
= sizeof(intercepted_code
);
270 ServiceFullThunk
*full_local_thunk
= reinterpret_cast<ServiceFullThunk
*>(
272 ServiceFullThunk
*full_remote_thunk
= reinterpret_cast<ServiceFullThunk
*>(
275 // patch the original code
276 memcpy(&intercepted_code
, &full_local_thunk
->original
,
277 sizeof(intercepted_code
));
278 intercepted_code
.mov_eax
= kMovEax
;
279 intercepted_code
.service_id
= full_local_thunk
->original
.service_id
;
280 intercepted_code
.mov_edx
= kMovEdx
;
281 intercepted_code
.stub
= bit_cast
<ULONG
>(&full_remote_thunk
->internal_thunk
);
282 intercepted_code
.call_ptr_edx
= kJmpEdx
;
283 bytes_to_write
= kMinServiceSize
;
285 if (relative_jump_
) {
286 intercepted_code
.mov_eax
= kJmp32
;
287 intercepted_code
.service_id
= relative_jump_
;
288 bytes_to_write
= offsetof(ServiceEntry
, mov_edx
);
292 SetInternalThunk(&full_local_thunk
->internal_thunk
, GetInternalThunkSize(),
293 remote_thunk
, interceptor_
);
295 size_t thunk_size
= GetThunkSize();
297 // copy the local thunk buffer to the child
299 if (!::WriteProcessMemory(process_
, remote_thunk
, local_thunk
,
300 thunk_size
, &written
))
301 return STATUS_UNSUCCESSFUL
;
303 if (thunk_size
!= written
)
304 return STATUS_UNSUCCESSFUL
;
306 // and now change the function to intercept, on the child
307 if (NULL
!= ntdll_base_
) {
308 // running a unit test
309 if (!::WriteProcessMemory(process_
, target_
, &intercepted_code
,
310 bytes_to_write
, &written
))
311 return STATUS_UNSUCCESSFUL
;
313 if (!WriteProtectedChildMemory(process_
, target_
, &intercepted_code
,
315 return STATUS_UNSUCCESSFUL
;
318 return STATUS_SUCCESS
;
321 bool ServiceResolverThunk::SaveOriginalFunction(void* local_thunk
,
322 void* remote_thunk
) {
323 ServiceEntry function_code
;
325 if (!::ReadProcessMemory(process_
, target_
, &function_code
,
326 sizeof(function_code
), &read
))
329 if (sizeof(function_code
) != read
)
332 if (kJmp32
== function_code
.mov_eax
) {
333 // Plain old entry point patch. The relative jump address follows it.
334 ULONG relative
= function_code
.service_id
;
336 // First, fix our copy of their patch.
337 relative
+= bit_cast
<ULONG
>(target_
) - bit_cast
<ULONG
>(remote_thunk
);
339 function_code
.service_id
= relative
;
341 // And now, remember how to re-patch it.
342 ServiceFullThunk
*full_thunk
=
343 reinterpret_cast<ServiceFullThunk
*>(remote_thunk
);
345 const ULONG kJmp32Size
= 5;
347 relative_jump_
= bit_cast
<ULONG
>(&full_thunk
->internal_thunk
) -
348 bit_cast
<ULONG
>(target_
) - kJmp32Size
;
351 // Save the verified code
352 memcpy(local_thunk
, &function_code
, sizeof(function_code
));
357 bool Wow64ResolverThunk::IsFunctionAService(void* local_thunk
) const {
358 Wow64Entry function_code
;
360 if (!::ReadProcessMemory(process_
, target_
, &function_code
,
361 sizeof(function_code
), &read
))
364 if (sizeof(function_code
) != read
)
367 if (kMovEax
!= function_code
.mov_eax
|| kXorEcx
!= function_code
.xor_ecx
||
368 kLeaEdx
!= function_code
.lea_edx
|| kCallFs1
!= function_code
.call_fs1
||
369 kCallFs2
!= function_code
.call_fs2
|| kCallFs3
!= function_code
.call_fs3
)
372 if ((kAddEsp1
== function_code
.add_esp1
&&
373 kAddEsp2
== function_code
.add_esp2
&&
374 kRet
== function_code
.ret
) || kRet
== function_code
.add_esp1
) {
375 // Save the verified code
376 memcpy(local_thunk
, &function_code
, sizeof(function_code
));
383 bool Wow64W8ResolverThunk::IsFunctionAService(void* local_thunk
) const {
384 Wow64EntryW8 function_code
;
386 if (!::ReadProcessMemory(process_
, target_
, &function_code
,
387 sizeof(function_code
), &read
))
390 if (sizeof(function_code
) != read
)
393 if (kMovEax
!= function_code
.mov_eax
|| kCallFs1
!= function_code
.call_fs1
||
394 kCallFs2
!= function_code
.call_fs2
||
395 kCallFs3
!= function_code
.call_fs3
|| kRet
!= function_code
.ret
) {
399 // Save the verified code
400 memcpy(local_thunk
, &function_code
, sizeof(function_code
));
404 bool Win8ResolverThunk::IsFunctionAService(void* local_thunk
) const {
405 ServiceEntryW8 function_code
;
407 if (!::ReadProcessMemory(process_
, target_
, &function_code
,
408 sizeof(function_code
), &read
))
411 if (sizeof(function_code
) != read
)
414 if (kMovEax
!= function_code
.mov_eax
|| kCallEip
!= function_code
.call_eip
||
415 function_code
.call_offset
!= 3 || kRet
!= function_code
.ret_p
||
416 kMovEdxEsp
!= function_code
.mov_edx_esp
||
417 kSysenter
!= function_code
.sysenter
|| kRet2
!= function_code
.ret
) {
421 // Save the verified code
422 memcpy(local_thunk
, &function_code
, sizeof(function_code
));
427 } // namespace sandbox