process_thread_dispatcher.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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
  3. // found in the LICENSE file.
  4. #include "sandbox/win/src/process_thread_dispatcher.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include "base/notreached.h"
  8. #include "sandbox/win/src/crosscall_client.h"
  9. #include "sandbox/win/src/interception.h"
  10. #include "sandbox/win/src/interceptors.h"
  11. #include "sandbox/win/src/ipc_tags.h"
  12. #include "sandbox/win/src/policy_broker.h"
  13. #include "sandbox/win/src/policy_params.h"
  14. #include "sandbox/win/src/process_thread_interception.h"
  15. #include "sandbox/win/src/process_thread_policy.h"
  16. #include "sandbox/win/src/sandbox.h"
  17. namespace sandbox {
  18. ThreadProcessDispatcher::ThreadProcessDispatcher() {
  19. static const IPCCall open_thread = {
  20. {IpcTag::NTOPENTHREAD, {UINT32_TYPE, UINT32_TYPE}},
  21. reinterpret_cast<CallbackGeneric>(
  22. &ThreadProcessDispatcher::NtOpenThread)};
  23. static const IPCCall open_process = {
  24. {IpcTag::NTOPENPROCESS, {UINT32_TYPE, UINT32_TYPE}},
  25. reinterpret_cast<CallbackGeneric>(
  26. &ThreadProcessDispatcher::NtOpenProcess)};
  27. static const IPCCall process_token = {
  28. {IpcTag::NTOPENPROCESSTOKEN, {VOIDPTR_TYPE, UINT32_TYPE}},
  29. reinterpret_cast<CallbackGeneric>(
  30. &ThreadProcessDispatcher::NtOpenProcessToken)};
  31. static const IPCCall process_tokenex = {
  32. {IpcTag::NTOPENPROCESSTOKENEX, {VOIDPTR_TYPE, UINT32_TYPE, UINT32_TYPE}},
  33. reinterpret_cast<CallbackGeneric>(
  34. &ThreadProcessDispatcher::NtOpenProcessTokenEx)};
  35. // NOTE(liamjm): 2nd param is size_t: Using VOIDPTR_TYPE as they are
  36. // the same size on windows.
  37. static_assert(sizeof(size_t) == sizeof(void*),
  38. "VOIDPTR_TYPE not same size as size_t");
  39. static const IPCCall create_thread_params = {
  40. {IpcTag::CREATETHREAD,
  41. {VOIDPTR_TYPE, VOIDPTR_TYPE, VOIDPTR_TYPE, UINT32_TYPE}},
  42. reinterpret_cast<CallbackGeneric>(
  43. &ThreadProcessDispatcher::CreateThread)};
  44. ipc_calls_.push_back(open_thread);
  45. ipc_calls_.push_back(open_process);
  46. ipc_calls_.push_back(process_token);
  47. ipc_calls_.push_back(process_tokenex);
  48. ipc_calls_.push_back(create_thread_params);
  49. }
  50. bool ThreadProcessDispatcher::SetupService(InterceptionManager* manager,
  51. IpcTag service) {
  52. switch (service) {
  53. case IpcTag::NTOPENTHREAD:
  54. case IpcTag::NTOPENPROCESS:
  55. case IpcTag::NTOPENPROCESSTOKEN:
  56. case IpcTag::NTOPENPROCESSTOKENEX:
  57. case IpcTag::CREATETHREAD:
  58. // There is no explicit policy for these services.
  59. NOTREACHED();
  60. return false;
  61. default:
  62. return false;
  63. }
  64. }
  65. bool ThreadProcessDispatcher::NtOpenThread(IPCInfo* ipc,
  66. uint32_t desired_access,
  67. uint32_t thread_id) {
  68. HANDLE handle;
  69. NTSTATUS ret = ProcessPolicy::OpenThreadAction(
  70. *ipc->client_info, desired_access, thread_id, &handle);
  71. ipc->return_info.nt_status = ret;
  72. ipc->return_info.handle = handle;
  73. return true;
  74. }
  75. bool ThreadProcessDispatcher::NtOpenProcess(IPCInfo* ipc,
  76. uint32_t desired_access,
  77. uint32_t process_id) {
  78. HANDLE handle;
  79. NTSTATUS ret = ProcessPolicy::OpenProcessAction(
  80. *ipc->client_info, desired_access, process_id, &handle);
  81. ipc->return_info.nt_status = ret;
  82. ipc->return_info.handle = handle;
  83. return true;
  84. }
  85. bool ThreadProcessDispatcher::NtOpenProcessToken(IPCInfo* ipc,
  86. HANDLE process,
  87. uint32_t desired_access) {
  88. HANDLE handle;
  89. NTSTATUS ret = ProcessPolicy::OpenProcessTokenAction(
  90. *ipc->client_info, process, desired_access, &handle);
  91. ipc->return_info.nt_status = ret;
  92. ipc->return_info.handle = handle;
  93. return true;
  94. }
  95. bool ThreadProcessDispatcher::NtOpenProcessTokenEx(IPCInfo* ipc,
  96. HANDLE process,
  97. uint32_t desired_access,
  98. uint32_t attributes) {
  99. HANDLE handle;
  100. NTSTATUS ret = ProcessPolicy::OpenProcessTokenExAction(
  101. *ipc->client_info, process, desired_access, attributes, &handle);
  102. ipc->return_info.nt_status = ret;
  103. ipc->return_info.handle = handle;
  104. return true;
  105. }
  106. bool ThreadProcessDispatcher::CreateThread(IPCInfo* ipc,
  107. SIZE_T stack_size,
  108. LPTHREAD_START_ROUTINE start_address,
  109. LPVOID parameter,
  110. DWORD creation_flags) {
  111. if (!start_address) {
  112. return false;
  113. }
  114. HANDLE handle;
  115. DWORD ret = ProcessPolicy::CreateThreadAction(
  116. *ipc->client_info, stack_size, start_address, parameter, creation_flags,
  117. nullptr, &handle);
  118. ipc->return_info.nt_status = ret;
  119. ipc->return_info.handle = handle;
  120. return true;
  121. }
  122. } // namespace sandbox