OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/process_thread_interception.h" | 5 #include "sandbox/win/src/process_thread_interception.h" |
6 | 6 |
| 7 #include "base/win/windows_version.h" |
7 #include "sandbox/win/src/crosscall_client.h" | 8 #include "sandbox/win/src/crosscall_client.h" |
8 #include "sandbox/win/src/ipc_tags.h" | 9 #include "sandbox/win/src/ipc_tags.h" |
9 #include "sandbox/win/src/policy_params.h" | 10 #include "sandbox/win/src/policy_params.h" |
10 #include "sandbox/win/src/policy_target.h" | 11 #include "sandbox/win/src/policy_target.h" |
11 #include "sandbox/win/src/sandbox_factory.h" | 12 #include "sandbox/win/src/sandbox_factory.h" |
12 #include "sandbox/win/src/sandbox_nt_util.h" | 13 #include "sandbox/win/src/sandbox_nt_util.h" |
13 #include "sandbox/win/src/sharedmem_ipc_client.h" | 14 #include "sandbox/win/src/sharedmem_ipc_client.h" |
14 #include "sandbox/win/src/target_services.h" | 15 #include "sandbox/win/src/target_services.h" |
15 | 16 |
16 namespace sandbox { | 17 namespace sandbox { |
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 if (ERROR_SUCCESS != answer.win32_result) | 395 if (ERROR_SUCCESS != answer.win32_result) |
395 return FALSE; | 396 return FALSE; |
396 | 397 |
397 return TRUE; | 398 return TRUE; |
398 } while (false); | 399 } while (false); |
399 | 400 |
400 ::SetLastError(original_error); | 401 ::SetLastError(original_error); |
401 return FALSE; | 402 return FALSE; |
402 } | 403 } |
403 | 404 |
| 405 // GetThreadId is not available on WINXP. So we'll |
| 406 // load it on-the-fly. |
| 407 const wchar_t kKernel32DllName[] = L"Kernel32.dll"; |
| 408 typedef DWORD(WINAPI* GetThreadIdFunc)(HANDLE Thread); |
| 409 |
| 410 HANDLE WINAPI TargetCreateThread(CreateThreadFunction orig_CreateThread, |
| 411 LPSECURITY_ATTRIBUTES thread_attributes, |
| 412 SIZE_T stack_size, |
| 413 LPTHREAD_START_ROUTINE start_address, |
| 414 PVOID parameter, |
| 415 DWORD creation_flags, |
| 416 LPDWORD thread_id) { |
| 417 HANDLE hThread = NULL; |
| 418 |
| 419 TargetServices* target_services = SandboxFactory::GetTargetServices(); |
| 420 if (NULL == target_services || |
| 421 target_services->GetState()->IsCsrssConnected()) { |
| 422 hThread = orig_CreateThread(thread_attributes, stack_size, start_address, |
| 423 parameter, creation_flags, thread_id); |
| 424 if (hThread) { |
| 425 return hThread; |
| 426 } |
| 427 } |
| 428 static GetThreadIdFunc GetThreadId_func = NULL; |
| 429 |
| 430 if (NULL == target_services) |
| 431 return NULL; |
| 432 |
| 433 // We don't trust that the IPC can work this early. |
| 434 if (!target_services->GetState()->InitCalled()) |
| 435 return NULL; |
| 436 |
| 437 DWORD original_error = ::GetLastError(); |
| 438 |
| 439 do { |
| 440 if (NULL != thread_id && |
| 441 !ValidParameter(thread_id, sizeof(*thread_id), WRITE)) |
| 442 break; |
| 443 |
| 444 void* memory = GetGlobalIPCMemory(); |
| 445 if (NULL == memory) |
| 446 break; |
| 447 |
| 448 SharedMemIPCClient ipc(memory); |
| 449 CrossCallReturn answer = {0}; |
| 450 |
| 451 ResultCode code = |
| 452 CrossCall(ipc, IPC_CREATETHREAD_TAG, (LPVOID)thread_attributes, |
| 453 (LPVOID)stack_size, (LPVOID)start_address, (LPVOID)parameter, |
| 454 (DWORD)creation_flags, &answer); |
| 455 |
| 456 if (SBOX_ALL_OK != code) |
| 457 break; |
| 458 |
| 459 ::SetLastError(answer.win32_result); |
| 460 if (ERROR_SUCCESS != answer.win32_result) { |
| 461 return NULL; |
| 462 } |
| 463 |
| 464 if (thread_id != NULL) { |
| 465 // GetThreadId is not available on WINXP. Set thread_id to 0, as this is |
| 466 // what is returned in the event of a failure. |
| 467 *thread_id = 0; |
| 468 if (base::win::GetVersion() >= base::win::VERSION_VISTA) { |
| 469 if (!GetThreadId_func) { |
| 470 HMODULE kernel32_dll = ::GetModuleHandle(kKernel32DllName); |
| 471 if (!kernel32_dll) |
| 472 break; |
| 473 GetThreadId_func = reinterpret_cast<GetThreadIdFunc>( |
| 474 GetProcAddress(kernel32_dll, "GetThreadId")); |
| 475 if (!GetThreadId_func) |
| 476 break; |
| 477 } |
| 478 *thread_id = GetThreadId_func(answer.handle); |
| 479 } |
| 480 } |
| 481 return answer.handle; |
| 482 } while (false); |
| 483 |
| 484 ::SetLastError(original_error); |
| 485 return NULL; |
| 486 } |
| 487 |
404 } // namespace sandbox | 488 } // namespace sandbox |
OLD | NEW |