Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Side by Side Diff: sandbox/win/src/sandbox_utils.cc

Issue 84063004: Restructure sandbox code to reduce dependencies pulled in by intercept code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove sandbox re-libification Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 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/sandbox_utils.h" 5 #include "sandbox/win/src/sandbox_utils.h"
6 6
7 #include <windows.h>
8
9 #include "base/logging.h" 7 #include "base/logging.h"
10 #include "base/win/windows_version.h" 8 #include "base/win/windows_version.h"
11 #include "sandbox/win/src/internal_types.h" 9 #include "sandbox/win/src/internal_types.h"
10 #include "sandbox/win/src/sandbox_factory.h"
11 #include "sandbox/win/src/sandbox_nt_types.h"
12 #include "sandbox/win/src/sandbox_nt_util.h"
13 #include "sandbox/win/src/target_services.h"
12 14
13 namespace sandbox { 15 namespace sandbox {
14 16
17 // This is the list of all imported symbols from ntdll.dll.
18 SANDBOX_INTERCEPT NtExports g_nt;
19
20 SANDBOX_INTERCEPT HANDLE g_shared_section;
21 SANDBOX_INTERCEPT size_t g_shared_IPC_size = 0;
22 SANDBOX_INTERCEPT size_t g_shared_policy_size = 0;
23
24 void* volatile g_shared_policy_memory = NULL;
25 void* volatile g_shared_IPC_memory = NULL;
26
27 // Both the IPC and the policy share a single region of memory in which the IPC
28 // memory is first and the policy memory is last.
29 bool MapGlobalMemory() {
30 if (NULL == g_shared_IPC_memory) {
31 void* memory = NULL;
32 SIZE_T size = 0;
33 // Map the entire shared section from the start.
34 NTSTATUS ret = g_nt.MapViewOfSection(g_shared_section, NtCurrentProcess,
35 &memory, 0, 0, NULL, &size, ViewUnmap,
36 0, PAGE_READWRITE);
37
38 if (!NT_SUCCESS(ret) || NULL == memory) {
39 NOTREACHED_NT();
40 return false;
41 }
42
43 if (NULL != _InterlockedCompareExchangePointer(&g_shared_IPC_memory,
44 memory, NULL)) {
45 // Somebody beat us to the memory setup.
46 ret = g_nt.UnmapViewOfSection(NtCurrentProcess, memory);
47 VERIFY_SUCCESS(ret);
48 }
49 DCHECK_NT(g_shared_IPC_size > 0);
50 g_shared_policy_memory = reinterpret_cast<char*>(g_shared_IPC_memory)
51 + g_shared_IPC_size;
52 }
53 DCHECK_NT(g_shared_policy_memory);
54 DCHECK_NT(g_shared_policy_size > 0);
55 return true;
56 }
57
58 void* GetGlobalIPCMemory() {
59 if (!MapGlobalMemory())
60 return NULL;
61 return g_shared_IPC_memory;
62 }
63
64 void* GetGlobalPolicyMemory() {
65 if (!MapGlobalMemory())
66 return NULL;
67 return g_shared_policy_memory;
68 }
69
70 NTSTATUS CopyData(void* destination, const void* source, size_t bytes) {
71 NTSTATUS ret = STATUS_SUCCESS;
72 __try {
73 if (SandboxFactory::GetTargetServices()->GetState()->InitCalled()) {
74 memcpy(destination, source, bytes);
75 } else {
76 const char* from = reinterpret_cast<const char*>(source);
77 char* to = reinterpret_cast<char*>(destination);
78 for (size_t i = 0; i < bytes; i++) {
79 to[i] = from[i];
80 }
81 }
82 } __except(EXCEPTION_EXECUTE_HANDLER) {
83 ret = GetExceptionCode();
84 }
85 return ret;
86 }
87
88
89 // Hacky code... replace with AllocAndCopyObjectAttributes.
90 NTSTATUS AllocAndCopyName(const OBJECT_ATTRIBUTES* in_object,
91 wchar_t** out_name, uint32* attributes,
92 HANDLE* root) {
93 if (!InitHeap())
94 return STATUS_NO_MEMORY;
95
96 DCHECK_NT(out_name);
97 *out_name = NULL;
98 NTSTATUS ret = STATUS_UNSUCCESSFUL;
99 __try {
100 do {
101 if (in_object->RootDirectory != static_cast<HANDLE>(0) && !root)
102 break;
103 if (NULL == in_object->ObjectName)
104 break;
105 if (NULL == in_object->ObjectName->Buffer)
106 break;
107
108 size_t size = in_object->ObjectName->Length + sizeof(wchar_t);
109 *out_name = new(NT_ALLOC) wchar_t[size/sizeof(wchar_t)];
110 if (NULL == *out_name)
111 break;
112
113 ret = CopyData(*out_name, in_object->ObjectName->Buffer,
114 size - sizeof(wchar_t));
115 if (!NT_SUCCESS(ret))
116 break;
117
118 (*out_name)[size / sizeof(wchar_t) - 1] = L'\0';
119
120 if (attributes)
121 *attributes = in_object->Attributes;
122
123 if (root)
124 *root = in_object->RootDirectory;
125 ret = STATUS_SUCCESS;
126 } while (false);
127 } __except(EXCEPTION_EXECUTE_HANDLER) {
128 ret = GetExceptionCode();
129 }
130
131 if (!NT_SUCCESS(ret) && *out_name) {
132 operator delete(*out_name, NT_ALLOC);
133 *out_name = NULL;
134 }
135
136 return ret;
137 }
138
139
140 UNICODE_STRING* ExtractModuleName(const UNICODE_STRING* module_path) {
141 if ((!module_path) || (!module_path->Buffer))
142 return NULL;
143
144 wchar_t* sep = NULL;
145 int start_pos = module_path->Length / sizeof(wchar_t) - 1;
146 int ix = start_pos;
147
148 for (; ix >= 0; --ix) {
149 if (module_path->Buffer[ix] == L'\\') {
150 sep = &module_path->Buffer[ix];
151 break;
152 }
153 }
154
155 // Ends with path separator. Not a valid module name.
156 if ((ix == start_pos) && sep)
157 return NULL;
158
159 // No path separator found. Use the entire name.
160 if (!sep) {
161 sep = &module_path->Buffer[-1];
162 }
163
164 // Add one to the size so we can null terminate the string.
165 size_t size_bytes = (start_pos - ix + 1) * sizeof(wchar_t);
166
167 // Based on the code above, size_bytes should always be small enough
168 // to make the static_cast below safe.
169 DCHECK_NT(kuint16max > size_bytes);
170 char* str_buffer = new(NT_ALLOC) char[size_bytes + sizeof(UNICODE_STRING)];
171 if (!str_buffer)
172 return NULL;
173
174 UNICODE_STRING* out_string = reinterpret_cast<UNICODE_STRING*>(str_buffer);
175 out_string->Buffer = reinterpret_cast<wchar_t*>(&out_string[1]);
176 out_string->Length = static_cast<USHORT>(size_bytes - sizeof(wchar_t));
177 out_string->MaximumLength = static_cast<USHORT>(size_bytes);
178
179 NTSTATUS ret = CopyData(out_string->Buffer, &sep[1], out_string->Length);
180 if (!NT_SUCCESS(ret)) {
181 operator delete(out_string, NT_ALLOC);
182 return NULL;
183 }
184
185 out_string->Buffer[out_string->Length / sizeof(wchar_t)] = L'\0';
186 return out_string;
187 }
188
15 bool IsXPSP2OrLater() { 189 bool IsXPSP2OrLater() {
16 base::win::Version version = base::win::GetVersion(); 190 base::win::Version version = base::win::GetVersion();
17 return (version > base::win::VERSION_XP) || 191 return (version > base::win::VERSION_XP) ||
18 ((version == base::win::VERSION_XP) && 192 ((version == base::win::VERSION_XP) &&
19 (base::win::OSInfo::GetInstance()->service_pack().major >= 2)); 193 (base::win::OSInfo::GetInstance()->service_pack().major >= 2));
20 } 194 }
21 195
22 void InitObjectAttribs(const std::wstring& name, 196 void InitObjectAttribs(const string16& name,
23 ULONG attributes, 197 ULONG attributes,
24 HANDLE root, 198 HANDLE root,
25 OBJECT_ATTRIBUTES* obj_attr, 199 OBJECT_ATTRIBUTES* obj_attr,
26 UNICODE_STRING* uni_name) { 200 UNICODE_STRING* uni_name) {
27 static RtlInitUnicodeStringFunction RtlInitUnicodeString; 201 static RtlInitUnicodeStringFunction RtlInitUnicodeString;
28 if (!RtlInitUnicodeString) { 202 if (!RtlInitUnicodeString) {
29 HMODULE ntdll = ::GetModuleHandle(kNtdllName); 203 HMODULE ntdll = ::GetModuleHandle(kNtdllName);
30 RtlInitUnicodeString = reinterpret_cast<RtlInitUnicodeStringFunction>( 204 RtlInitUnicodeString = reinterpret_cast<RtlInitUnicodeStringFunction>(
31 GetProcAddress(ntdll, "RtlInitUnicodeString")); 205 GetProcAddress(ntdll, "RtlInitUnicodeString"));
32 DCHECK(RtlInitUnicodeString); 206 DCHECK(RtlInitUnicodeString);
33 } 207 }
34 RtlInitUnicodeString(uni_name, name.c_str()); 208 RtlInitUnicodeString(uni_name, name.c_str());
35 InitializeObjectAttributes(obj_attr, uni_name, attributes, root, NULL); 209 InitializeObjectAttributes(obj_attr, uni_name, attributes, root, NULL);
36 } 210 }
37 211
38 }; // namespace sandbox 212 } // namespace sandbox
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698