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/sandbox_nt_util.h" | 5 #include "sandbox/win/src/sandbox_nt_util.h" |
6 | 6 |
7 #include "base/win/pe_image.h" | 7 #include "base/win/pe_image.h" |
8 #include "sandbox/win/src/sandbox_factory.h" | 8 #include "sandbox/win/src/sandbox_factory.h" |
9 #include "sandbox/win/src/target_services.h" | 9 #include "sandbox/win/src/target_services.h" |
10 | 10 |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 NTSTATUS CopyData(void* destination, const void* source, size_t bytes) { | 208 NTSTATUS CopyData(void* destination, const void* source, size_t bytes) { |
209 NTSTATUS ret = STATUS_SUCCESS; | 209 NTSTATUS ret = STATUS_SUCCESS; |
210 __try { | 210 __try { |
211 g_nt.memcpy(destination, source, bytes); | 211 g_nt.memcpy(destination, source, bytes); |
212 } __except(EXCEPTION_EXECUTE_HANDLER) { | 212 } __except(EXCEPTION_EXECUTE_HANDLER) { |
213 ret = GetExceptionCode(); | 213 ret = GetExceptionCode(); |
214 } | 214 } |
215 return ret; | 215 return ret; |
216 } | 216 } |
217 | 217 |
| 218 NTSTATUS AllocAndGetFullPath(HANDLE root, |
| 219 wchar_t* path, |
| 220 wchar_t** full_path) { |
| 221 if (!InitHeap()) |
| 222 return STATUS_NO_MEMORY; |
| 223 |
| 224 DCHECK_NT(full_path); |
| 225 DCHECK_NT(path); |
| 226 *full_path = NULL; |
| 227 OBJECT_NAME_INFORMATION* handle_name = NULL; |
| 228 NTSTATUS ret = STATUS_UNSUCCESSFUL; |
| 229 __try { |
| 230 do { |
| 231 static NtQueryObjectFunction NtQueryObject = NULL; |
| 232 if (!NtQueryObject) |
| 233 ResolveNTFunctionPtr("NtQueryObject", &NtQueryObject); |
| 234 |
| 235 ULONG size = 0; |
| 236 // Query the name information a first time to get the size of the name. |
| 237 ret = NtQueryObject(root, ObjectNameInformation, NULL, 0, &size); |
| 238 |
| 239 if (size) { |
| 240 handle_name = reinterpret_cast<OBJECT_NAME_INFORMATION*>( |
| 241 new(NT_ALLOC) BYTE[size]); |
| 242 |
| 243 // Query the name information a second time to get the name of the |
| 244 // object referenced by the handle. |
| 245 ret = NtQueryObject(root, ObjectNameInformation, handle_name, size, |
| 246 &size); |
| 247 } |
| 248 |
| 249 if (STATUS_SUCCESS != ret) |
| 250 break; |
| 251 |
| 252 // Space for path + '\' + name + '\0'. |
| 253 size_t name_length = handle_name->ObjectName.Length + |
| 254 (wcslen(path) + 2) * sizeof(wchar_t); |
| 255 *full_path = new(NT_ALLOC) wchar_t[name_length/sizeof(wchar_t)]; |
| 256 if (NULL == *full_path) |
| 257 break; |
| 258 wchar_t* off = *full_path; |
| 259 ret = CopyData(off, handle_name->ObjectName.Buffer, |
| 260 handle_name->ObjectName.Length); |
| 261 if (!NT_SUCCESS(ret)) |
| 262 break; |
| 263 off += handle_name->ObjectName.Length / sizeof(wchar_t); |
| 264 *off = L'\\'; |
| 265 off += 1; |
| 266 ret = CopyData(off, path, wcslen(path) * sizeof(wchar_t)); |
| 267 if (!NT_SUCCESS(ret)) |
| 268 break; |
| 269 off += wcslen(path); |
| 270 *off = L'\0'; |
| 271 } while (false); |
| 272 } __except(EXCEPTION_EXECUTE_HANDLER) { |
| 273 ret = GetExceptionCode(); |
| 274 } |
| 275 |
| 276 if (!NT_SUCCESS(ret)) { |
| 277 if (*full_path) { |
| 278 operator delete(*full_path, NT_ALLOC); |
| 279 *full_path = NULL; |
| 280 } |
| 281 if (handle_name) { |
| 282 operator delete(handle_name, NT_ALLOC); |
| 283 handle_name = NULL; |
| 284 } |
| 285 } |
| 286 |
| 287 return ret; |
| 288 } |
| 289 |
218 // Hacky code... replace with AllocAndCopyObjectAttributes. | 290 // Hacky code... replace with AllocAndCopyObjectAttributes. |
219 NTSTATUS AllocAndCopyName(const OBJECT_ATTRIBUTES* in_object, | 291 NTSTATUS AllocAndCopyName(const OBJECT_ATTRIBUTES* in_object, |
220 wchar_t** out_name, uint32* attributes, | 292 wchar_t** out_name, uint32* attributes, |
221 HANDLE* root) { | 293 HANDLE* root) { |
222 if (!InitHeap()) | 294 if (!InitHeap()) |
223 return STATUS_NO_MEMORY; | 295 return STATUS_NO_MEMORY; |
224 | 296 |
225 DCHECK_NT(out_name); | 297 DCHECK_NT(out_name); |
226 *out_name = NULL; | 298 *out_name = NULL; |
227 NTSTATUS ret = STATUS_UNSUCCESSFUL; | 299 NTSTATUS ret = STATUS_UNSUCCESSFUL; |
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
598 UNREFERENCED_PARAMETER(type); | 670 UNREFERENCED_PARAMETER(type); |
599 return buffer; | 671 return buffer; |
600 } | 672 } |
601 | 673 |
602 void __cdecl operator delete(void* memory, void* buffer, | 674 void __cdecl operator delete(void* memory, void* buffer, |
603 sandbox::AllocationType type) { | 675 sandbox::AllocationType type) { |
604 UNREFERENCED_PARAMETER(memory); | 676 UNREFERENCED_PARAMETER(memory); |
605 UNREFERENCED_PARAMETER(buffer); | 677 UNREFERENCED_PARAMETER(buffer); |
606 UNREFERENCED_PARAMETER(type); | 678 UNREFERENCED_PARAMETER(type); |
607 } | 679 } |
OLD | NEW |