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.
5 #include "sandbox/win/src/resolver.h"
7 #include "sandbox/win/src/sandbox_nt_util.h"
12 struct InternalThunk
{
13 // This struct contains roughly the following code:
14 // sub esp, 8 // Create working space
15 // push edx // Save register
16 // mov edx, [esp + 0xc] // Get return adddress
17 // mov [esp + 8], edx // Store return address
18 // mov dword ptr [esp + 0xc], 0x7c401200 // Store extra argument
19 // mov dword ptr [esp + 4], 0x40010203 // Store address to jump to
20 // pop edx // Restore register
21 // ret // Jump to interceptor
23 // This code only modifies esp and eip so it must work with to normal calling
24 // convention. It is assembled as:
26 // 00 83ec08 sub esp,8
28 // 04 8b54240c mov edx,dword ptr [esp + 0Ch]
29 // 08 89542408 mov dword ptr [esp + 8], edx
30 // 0c c744240c0012407c mov dword ptr [esp + 0Ch], 7C401200h
31 // 14 c744240403020140 mov dword ptr [esp + 4], 40010203h
35 opcodes_1
= 0x5208ec83;
36 opcodes_2
= 0x0c24548b;
37 opcodes_3
= 0x08245489;
38 opcodes_4
= 0x0c2444c7;
39 opcodes_5
= 0x042444c7;
42 interceptor_function
= 0;
44 ULONG opcodes_1
; // = 0x5208ec83
45 ULONG opcodes_2
; // = 0x0c24548b
46 ULONG opcodes_3
; // = 0x08245489
47 ULONG opcodes_4
; // = 0x0c2444c7
49 ULONG opcodes_5
; // = 0x042444c7
50 ULONG interceptor_function
;
51 USHORT opcodes_6
; // = 0xc35a
59 bool ResolverThunk::SetInternalThunk(void* storage
, size_t storage_bytes
,
60 const void* original_function
,
61 const void* interceptor
) {
62 if (storage_bytes
< sizeof(InternalThunk
))
65 InternalThunk
* thunk
= new(storage
, NT_PLACE
) InternalThunk
;
68 #pragma warning(disable: 4311)
69 // These casts generate warnings because they are 32 bit specific.
70 thunk
->interceptor_function
= reinterpret_cast<ULONG
>(interceptor
);
71 thunk
->extra_argument
= reinterpret_cast<ULONG
>(original_function
);
77 size_t ResolverThunk::GetInternalThunkSize() const {
78 return sizeof(InternalThunk
);
81 NTSTATUS
ResolverThunk::ResolveTarget(const void* module
,
82 const char* function_name
,
84 const void** casted
= const_cast<const void**>(address
);
85 return ResolverThunk::ResolveInterceptor(module
, function_name
, casted
);
88 } // namespace sandbox