Chromium Code Reviews| 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/src/target_process.h" | 5 #include "sandbox/src/target_process.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/win/pe_image.h" | 9 #include "base/win/pe_image.h" |
| 10 #include "base/win/windows_version.h" | 10 #include "base/win/windows_version.h" |
| 11 #include "sandbox/src/crosscall_server.h" | 11 #include "sandbox/src/crosscall_server.h" |
| 12 #include "sandbox/src/crosscall_client.h" | 12 #include "sandbox/src/crosscall_client.h" |
| 13 #include "sandbox/src/internal_types.h" | |
| 13 #include "sandbox/src/policy_low_level.h" | 14 #include "sandbox/src/policy_low_level.h" |
| 14 #include "sandbox/src/sandbox_types.h" | 15 #include "sandbox/src/sandbox_types.h" |
| 15 #include "sandbox/src/sharedmem_ipc_server.h" | 16 #include "sandbox/src/sharedmem_ipc_server.h" |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 void CopyPolicyToTarget(const void* source, size_t size, void* dest) { | 20 void CopyPolicyToTarget(const void* source, size_t size, void* dest) { |
| 20 if (!source || !size) | 21 if (!source || !size) |
| 21 return; | 22 return; |
| 22 memcpy(dest, source, size); | 23 memcpy(dest, source, size); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 48 if (!::VirtualQueryEx(process, ptr, &memory_info, sizeof(memory_info))) | 49 if (!::VirtualQueryEx(process, ptr, &memory_info, sizeof(memory_info))) |
| 49 break; | 50 break; |
| 50 size_t size = std::min((memory_info.RegionSize + kMask64k) & ~kMask64k, | 51 size_t size = std::min((memory_info.RegionSize + kMask64k) & ~kMask64k, |
| 51 static_cast<SIZE_T>(end - ptr)); | 52 static_cast<SIZE_T>(end - ptr)); |
| 52 if (ptr && memory_info.State == MEM_FREE) | 53 if (ptr && memory_info.State == MEM_FREE) |
| 53 ::VirtualAllocEx(process, ptr, size, MEM_RESERVE, PAGE_NOACCESS); | 54 ::VirtualAllocEx(process, ptr, size, MEM_RESERVE, PAGE_NOACCESS); |
| 54 ptr += size; | 55 ptr += size; |
| 55 } | 56 } |
| 56 } | 57 } |
| 57 | 58 |
| 58 } | 59 // This helper class wraps the STARTUPINFO structure and handles the |
| 60 // additional attributes in STARTUPINFOEX. | |
| 61 class ScopedStartupInfo { | |
| 62 public: | |
| 63 ScopedStartupInfo() { | |
| 64 memset(&startup_info_, 0, sizeof(startup_info_)); | |
| 65 | |
| 66 // Pre Windows Vista doesn't support STARTUPINFOEX. | |
| 67 if (base::win::GetVersion() < base::win::VERSION_VISTA) { | |
| 68 startup_info_.StartupInfo.cb = sizeof(STARTUPINFO); | |
| 69 return; | |
| 70 } | |
| 71 | |
| 72 startup_info_.StartupInfo.cb = sizeof(STARTUPINFOEX); | |
|
cpu_(ooo_6.6-7.5)
2012/07/02 21:42:32
sizeof(startup_info_)
| |
| 73 | |
| 74 // Load the attribute API functions. | |
| 75 if (!s_InitializeProcThreadAttributeList) { | |
| 76 HMODULE module = ::GetModuleHandleW(sandbox::kKerneldllName); | |
| 77 s_InitializeProcThreadAttributeList = | |
| 78 reinterpret_cast<InitializeProcThreadAttributeListFunction>( | |
| 79 ::GetProcAddress(module, "InitializeProcThreadAttributeList")); | |
| 80 s_UpdateProcThreadAttribute = | |
| 81 reinterpret_cast<UpdateProcThreadAttributeFunction>( | |
| 82 ::GetProcAddress(module, "UpdateProcThreadAttribute")); | |
| 83 s_DeleteProcThreadAttributeList = | |
| 84 reinterpret_cast<DeleteProcThreadAttributeListFunction>( | |
| 85 ::GetProcAddress(module, "DeleteProcThreadAttributeList")); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 ~ScopedStartupInfo() { | |
| 90 if (startup_info_.lpAttributeList) { | |
| 91 s_DeleteProcThreadAttributeList(startup_info_.lpAttributeList); | |
| 92 delete [] reinterpret_cast<BYTE*>(startup_info_.lpAttributeList); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 // Initialize the attribute list to the specified number of entries. | |
| 97 bool InitializeProcThreadAttributeList(DWORD attribute_count) { | |
| 98 if (startup_info_.lpAttributeList) | |
| 99 return false; | |
| 100 | |
| 101 SIZE_T size = 0; | |
| 102 s_InitializeProcThreadAttributeList(NULL, attribute_count, 0, &size); | |
| 103 if (size == 0) | |
| 104 return false; | |
| 105 | |
| 106 startup_info_.lpAttributeList = | |
| 107 reinterpret_cast<LPPROC_THREAD_ATTRIBUTE_LIST>(new BYTE[size]); | |
| 108 if (!s_InitializeProcThreadAttributeList(startup_info_.lpAttributeList, | |
| 109 attribute_count, 0, &size)) { | |
| 110 delete [] reinterpret_cast<BYTE*>(startup_info_.lpAttributeList); | |
| 111 return false; | |
| 112 } | |
| 113 | |
| 114 return true; | |
| 115 } | |
| 116 | |
| 117 // Sets one entry in the initialized attribute list. | |
| 118 bool UpdateProcThreadAttribute(DWORD_PTR attribute, | |
| 119 PVOID value, | |
|
cpu_(ooo_6.6-7.5)
2012/07/02 21:42:32
PVOID --> void*
Interestingly we also mix SIZE_T
| |
| 120 SIZE_T size) { | |
| 121 if (!startup_info_.lpAttributeList) | |
| 122 return false; | |
| 123 return s_UpdateProcThreadAttribute(startup_info_.lpAttributeList, 0, | |
| 124 attribute, value, size, NULL, NULL); | |
| 125 } | |
| 126 | |
| 127 STARTUPINFO* data() { return &startup_info_.StartupInfo; } | |
| 128 | |
| 129 private: | |
| 130 STARTUPINFOEX startup_info_; | |
| 131 | |
| 132 typedef BOOL (WINAPI *InitializeProcThreadAttributeListFunction)( | |
| 133 LPPROC_THREAD_ATTRIBUTE_LIST attribute_list, | |
| 134 DWORD attribute_count, | |
| 135 DWORD flags, | |
| 136 PSIZE_T size); | |
| 137 static InitializeProcThreadAttributeListFunction | |
| 138 s_InitializeProcThreadAttributeList; | |
| 139 | |
| 140 typedef BOOL (WINAPI *UpdateProcThreadAttributeFunction)( | |
| 141 LPPROC_THREAD_ATTRIBUTE_LIST attribute_list, | |
| 142 DWORD flags, | |
| 143 DWORD_PTR attribute, | |
| 144 PVOID value, | |
| 145 SIZE_T size, | |
| 146 PVOID previous_value, | |
| 147 PSIZE_T return_size); | |
| 148 static UpdateProcThreadAttributeFunction s_UpdateProcThreadAttribute; | |
| 149 | |
| 150 typedef VOID (WINAPI *DeleteProcThreadAttributeListFunction)( | |
| 151 LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList); | |
| 152 static DeleteProcThreadAttributeListFunction s_DeleteProcThreadAttributeList; | |
| 153 | |
| 154 DISALLOW_COPY_AND_ASSIGN(ScopedStartupInfo); | |
| 155 }; | |
| 156 | |
| 157 ScopedStartupInfo::InitializeProcThreadAttributeListFunction | |
| 158 ScopedStartupInfo::s_InitializeProcThreadAttributeList; | |
| 159 ScopedStartupInfo::UpdateProcThreadAttributeFunction | |
| 160 ScopedStartupInfo::s_UpdateProcThreadAttribute; | |
| 161 ScopedStartupInfo::DeleteProcThreadAttributeListFunction | |
| 162 ScopedStartupInfo::s_DeleteProcThreadAttributeList; | |
| 163 | |
| 164 } // namespace | |
| 59 | 165 |
| 60 namespace sandbox { | 166 namespace sandbox { |
| 61 | 167 |
| 62 SANDBOX_INTERCEPT HANDLE g_shared_section; | 168 SANDBOX_INTERCEPT HANDLE g_shared_section; |
| 63 SANDBOX_INTERCEPT size_t g_shared_IPC_size; | 169 SANDBOX_INTERCEPT size_t g_shared_IPC_size; |
| 64 SANDBOX_INTERCEPT size_t g_shared_policy_size; | 170 SANDBOX_INTERCEPT size_t g_shared_policy_size; |
| 65 | 171 |
| 66 // Returns the address of the main exe module in memory taking in account | 172 // Returns the address of the main exe module in memory taking in account |
| 67 // address space layout randomization. | 173 // address space layout randomization. |
| 68 void* GetBaseAddress(const wchar_t* exe_name, void* entry_point) { | 174 void* GetBaseAddress(const wchar_t* exe_name, void* entry_point) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 138 // Start the target process suspended. | 244 // Start the target process suspended. |
| 139 DWORD flags = | 245 DWORD flags = |
| 140 CREATE_SUSPENDED | CREATE_UNICODE_ENVIRONMENT | DETACHED_PROCESS; | 246 CREATE_SUSPENDED | CREATE_UNICODE_ENVIRONMENT | DETACHED_PROCESS; |
| 141 | 247 |
| 142 if (base::win::GetVersion() < base::win::VERSION_WIN8) { | 248 if (base::win::GetVersion() < base::win::VERSION_WIN8) { |
| 143 // Windows 8 implements nested jobs, but for older systems we need to | 249 // Windows 8 implements nested jobs, but for older systems we need to |
| 144 // break out of any job we're in to enforce our restrictions. | 250 // break out of any job we're in to enforce our restrictions. |
| 145 flags |= CREATE_BREAKAWAY_FROM_JOB; | 251 flags |= CREATE_BREAKAWAY_FROM_JOB; |
| 146 } | 252 } |
| 147 | 253 |
| 148 STARTUPINFO startup_info = {sizeof(STARTUPINFO)}; | 254 ScopedStartupInfo startup_info; |
| 149 if (desktop) { | 255 if (desktop) |
| 150 startup_info.lpDesktop = desktop_name.get(); | 256 startup_info.data()->lpDesktop = desktop_name.get(); |
| 257 if (base::win::GetVersion() >= base::win::VERSION_WIN7) { | |
|
cpu_(ooo_6.6-7.5)
2012/07/02 21:42:32
space after line 256
| |
| 258 flags |= EXTENDED_STARTUPINFO_PRESENT; | |
| 259 DWORD_PTR mitigation_flags = | |
| 260 PROCESS_CREATION_MITIGATION_POLICY_DEP_ENABLE | | |
| 261 PROCESS_CREATION_MITIGATION_POLICY_DEP_ATL_THUNK_ENABLE | | |
|
cpu_(ooo_6.6-7.5)
2012/07/02 21:42:32
I think these flags need to be passed as an argume
| |
| 262 PROCESS_CREATION_MITIGATION_POLICY_SEHOP_ENABLE; | |
| 263 | |
| 264 // TODO(jschuh): Remove this #ifdef after we resolve the SDK versioning. | |
| 265 #if defined(_WIN32_WINNT_WIN8) && _WIN32_WINNT >= _WIN32_WINNT_WIN8 | |
| 266 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { | |
| 267 mitigation_flags |= | |
| 268 PROCESS_CREATION_MITIGATION_POLICY_HEAP_TERMINATE_ALWAYS_ON | | |
| 269 PROCESS_CREATION_MITIGATION_POLICY_BOTTOM_UP_ASLR_ALWAYS_ON | | |
| 270 PROCESS_CREATION_MITIGATION_POLICY_HIGH_ENTROPY_ASLR_ALWAYS_ON | | |
| 271 PROCESS_CREATION_MITIGATION_POLICY_FORCE_RELOCATE_IMAGES_ALWAYS_ON_REQ_R ELOCS; | |
| 272 } | |
| 273 #endif | |
| 274 | |
| 275 if (startup_info.InitializeProcThreadAttributeList(1)) { | |
| 276 startup_info.UpdateProcThreadAttribute( | |
| 277 PROC_THREAD_ATTRIBUTE_MITIGATION_POLICY, &mitigation_flags, | |
| 278 sizeof(mitigation_flags)); | |
| 279 } | |
| 151 } | 280 } |
| 152 | 281 |
| 153 base::win::ScopedProcessInformation process_info; | 282 base::win::ScopedProcessInformation process_info; |
| 154 | 283 |
| 155 if (!::CreateProcessAsUserW(lockdown_token_, | 284 if (!::CreateProcessAsUserW(lockdown_token_, |
| 156 exe_path, | 285 exe_path, |
| 157 cmd_line.get(), | 286 cmd_line.get(), |
| 158 NULL, // No security attribute. | 287 NULL, // No security attribute. |
| 159 NULL, // No thread attribute. | 288 NULL, // No thread attribute. |
| 160 FALSE, // Do not inherit handles. | 289 FALSE, // Do not inherit handles. |
| 161 flags, | 290 flags, |
| 162 NULL, // Use the environment of the caller. | 291 NULL, // Use the environment of the caller. |
| 163 NULL, // Use current directory of the caller. | 292 NULL, // Use current directory of the caller. |
| 164 &startup_info, | 293 startup_info.data(), |
| 165 process_info.Receive())) { | 294 process_info.Receive())) { |
| 166 return ::GetLastError(); | 295 return ::GetLastError(); |
| 167 } | 296 } |
| 168 lockdown_token_.Close(); | 297 lockdown_token_.Close(); |
| 169 | 298 |
| 170 PoisonLowerAddressRange(process_info.process_handle()); | 299 PoisonLowerAddressRange(process_info.process_handle()); |
| 171 | 300 |
| 172 DWORD win_result = ERROR_SUCCESS; | 301 DWORD win_result = ERROR_SUCCESS; |
| 173 | 302 |
| 174 // Assign the suspended target to the windows job object | 303 // Assign the suspended target to the windows job object |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 346 | 475 |
| 347 | 476 |
| 348 TargetProcess* MakeTestTargetProcess(HANDLE process, HMODULE base_address) { | 477 TargetProcess* MakeTestTargetProcess(HANDLE process, HMODULE base_address) { |
| 349 TargetProcess* target = new TargetProcess(NULL, NULL, NULL, NULL); | 478 TargetProcess* target = new TargetProcess(NULL, NULL, NULL, NULL); |
| 350 target->sandbox_process_info_.Receive()->hProcess = process; | 479 target->sandbox_process_info_.Receive()->hProcess = process; |
| 351 target->base_address_ = base_address; | 480 target->base_address_ = base_address; |
| 352 return target; | 481 return target; |
| 353 } | 482 } |
| 354 | 483 |
| 355 } // namespace sandbox | 484 } // namespace sandbox |
| OLD | NEW |