OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include <map> | |
6 | |
7 #include "sandbox/src/policy_broker.h" | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/win/pe_image.h" | |
11 #include "base/win/windows_version.h" | |
12 #include "sandbox/src/interception.h" | |
13 #include "sandbox/src/interceptors.h" | |
14 #include "sandbox/src/policy_target.h" | |
15 #include "sandbox/src/process_thread_interception.h" | |
16 #include "sandbox/src/sandbox.h" | |
17 #include "sandbox/src/sandbox_nt_types.h" | |
18 #include "sandbox/src/sandbox_types.h" | |
19 #include "sandbox/src/sandbox_utils.h" | |
20 #include "sandbox/src/target_process.h" | |
21 | |
22 // This code executes on the broker side, as a callback from the policy on the | |
23 // target side (the child). | |
24 | |
25 namespace sandbox { | |
26 | |
27 // This is the list of all imported symbols from ntdll.dll. | |
28 SANDBOX_INTERCEPT NtExports g_nt; | |
29 | |
30 #define INIT_GLOBAL_NT(member) \ | |
31 g_nt.member = reinterpret_cast<Nt##member##Function>( \ | |
32 ntdll_image.GetProcAddress("Nt" #member)); \ | |
33 if (NULL == g_nt.member) \ | |
34 return false | |
35 | |
36 #define INIT_GLOBAL_RTL(member) \ | |
37 g_nt.member = reinterpret_cast<member##Function>( \ | |
38 ntdll_image.GetProcAddress(#member)); \ | |
39 if (NULL == g_nt.member) \ | |
40 return false | |
41 | |
42 bool SetupNtdllImports(TargetProcess *child) { | |
43 HMODULE ntdll = ::GetModuleHandle(kNtdllName); | |
44 base::win::PEImage ntdll_image(ntdll); | |
45 | |
46 // Bypass purify's interception. | |
47 wchar_t* loader_get = reinterpret_cast<wchar_t*>( | |
48 ntdll_image.GetProcAddress("LdrGetDllHandle")); | |
49 if (loader_get) { | |
50 GetModuleHandleHelper(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | | |
51 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, | |
52 loader_get, &ntdll); | |
53 } | |
54 | |
55 INIT_GLOBAL_NT(AllocateVirtualMemory); | |
56 INIT_GLOBAL_NT(Close); | |
57 INIT_GLOBAL_NT(DuplicateObject); | |
58 INIT_GLOBAL_NT(FreeVirtualMemory); | |
59 INIT_GLOBAL_NT(MapViewOfSection); | |
60 INIT_GLOBAL_NT(ProtectVirtualMemory); | |
61 INIT_GLOBAL_NT(QueryInformationProcess); | |
62 INIT_GLOBAL_NT(QueryObject); | |
63 INIT_GLOBAL_NT(QuerySection); | |
64 INIT_GLOBAL_NT(QueryVirtualMemory); | |
65 INIT_GLOBAL_NT(UnmapViewOfSection); | |
66 | |
67 INIT_GLOBAL_RTL(RtlAllocateHeap); | |
68 INIT_GLOBAL_RTL(RtlAnsiStringToUnicodeString); | |
69 INIT_GLOBAL_RTL(RtlCompareUnicodeString); | |
70 INIT_GLOBAL_RTL(RtlCreateHeap); | |
71 INIT_GLOBAL_RTL(RtlCreateUserThread); | |
72 INIT_GLOBAL_RTL(RtlDestroyHeap); | |
73 INIT_GLOBAL_RTL(RtlFreeHeap); | |
74 INIT_GLOBAL_RTL(_strnicmp); | |
75 INIT_GLOBAL_RTL(strlen); | |
76 INIT_GLOBAL_RTL(wcslen); | |
77 | |
78 #ifndef NDEBUG | |
79 // Verify that the structure is fully initialized. | |
80 for (size_t i = 0; i < sizeof(g_nt)/sizeof(void*); i++) | |
81 DCHECK(reinterpret_cast<char**>(&g_nt)[i]); | |
82 #endif | |
83 ResultCode ret = child->TransferVariable("g_nt", &g_nt, sizeof(g_nt)); | |
84 | |
85 return SBOX_ALL_OK == ret ? true : false; | |
86 } | |
87 | |
88 #undef INIT_GLOBAL_NT | |
89 #undef INIT_GLOBAL_RTL | |
90 | |
91 bool SetupBasicInterceptions(InterceptionManager* manager) { | |
92 // Interceptions provided by process_thread_policy, without actual policy. | |
93 if (!INTERCEPT_NT(manager, NtOpenThread, OPEN_TREAD_ID, 20) || | |
94 !INTERCEPT_NT(manager, NtOpenProcess, OPEN_PROCESS_ID, 20) || | |
95 !INTERCEPT_NT(manager, NtOpenProcessToken, OPEN_PROCESS_TOKEN_ID, 16)) | |
96 return false; | |
97 | |
98 // Interceptions with neither policy nor IPC. | |
99 if (!INTERCEPT_NT(manager, NtSetInformationThread, SET_INFORMATION_THREAD_ID, | |
100 20) || | |
101 !INTERCEPT_NT(manager, NtOpenThreadToken, OPEN_THREAD_TOKEN_ID, 20)) | |
102 return false; | |
103 | |
104 if (base::win::GetVersion() >= base::win::VERSION_XP) { | |
105 // Bug 27218: We don't have dispatch for some x64 syscalls. | |
106 // This one is also provided by process_thread_policy. | |
107 if (!INTERCEPT_NT(manager, NtOpenProcessTokenEx, OPEN_PROCESS_TOKEN_EX_ID, | |
108 20)) | |
109 return false; | |
110 | |
111 return INTERCEPT_NT(manager, NtOpenThreadTokenEx, OPEN_THREAD_TOKEN_EX_ID, | |
112 24); | |
113 } | |
114 | |
115 return true; | |
116 } | |
117 | |
118 } // namespace sandbox | |
OLD | NEW |