| 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/broker_services.h" | 5 #include "sandbox/src/broker_services.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/threading/platform_thread.h" | 9 #include "base/threading/platform_thread.h" |
| 10 #include "base/win/scoped_handle.h" |
| 11 #include "base/win/scoped_process_information.h" |
| 9 #include "sandbox/src/sandbox_policy_base.h" | 12 #include "sandbox/src/sandbox_policy_base.h" |
| 10 #include "sandbox/src/sandbox.h" | 13 #include "sandbox/src/sandbox.h" |
| 11 #include "sandbox/src/target_process.h" | 14 #include "sandbox/src/target_process.h" |
| 12 #include "sandbox/src/win2k_threadpool.h" | 15 #include "sandbox/src/win2k_threadpool.h" |
| 13 #include "sandbox/src/win_utils.h" | 16 #include "sandbox/src/win_utils.h" |
| 14 | 17 |
| 15 namespace { | 18 namespace { |
| 16 | 19 |
| 17 // Utility function to associate a completion port to a job object. | 20 // Utility function to associate a completion port to a job object. |
| 18 bool AssociateCompletionPort(HANDLE job, HANDLE port, void* key) { | 21 bool AssociateCompletionPort(HANDLE job, HANDLE port, void* key) { |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 static DWORD thread_id = ::GetCurrentThreadId(); | 290 static DWORD thread_id = ::GetCurrentThreadId(); |
| 288 DCHECK(thread_id == ::GetCurrentThreadId()); | 291 DCHECK(thread_id == ::GetCurrentThreadId()); |
| 289 | 292 |
| 290 AutoLock lock(&lock_); | 293 AutoLock lock(&lock_); |
| 291 | 294 |
| 292 // This downcast is safe as long as we control CreatePolicy() | 295 // This downcast is safe as long as we control CreatePolicy() |
| 293 PolicyBase* policy_base = static_cast<PolicyBase*>(policy); | 296 PolicyBase* policy_base = static_cast<PolicyBase*>(policy); |
| 294 | 297 |
| 295 // Construct the tokens and the job object that we are going to associate | 298 // Construct the tokens and the job object that we are going to associate |
| 296 // with the soon to be created target process. | 299 // with the soon to be created target process. |
| 297 HANDLE lockdown_token = NULL; | 300 base::win::ScopedHandle lockdown_token; |
| 298 HANDLE initial_token = NULL; | 301 base::win::ScopedHandle initial_token; |
| 299 DWORD win_result = policy_base->MakeTokens(&initial_token, &lockdown_token); | 302 DWORD win_result = policy_base->MakeTokens(initial_token.Receive(), |
| 303 lockdown_token.Receive()); |
| 300 if (ERROR_SUCCESS != win_result) | 304 if (ERROR_SUCCESS != win_result) |
| 301 return SBOX_ERROR_GENERIC; | 305 return SBOX_ERROR_GENERIC; |
| 302 | 306 |
| 303 HANDLE job = NULL; | 307 base::win::ScopedHandle job; |
| 304 win_result = policy_base->MakeJobObject(&job); | 308 win_result = policy_base->MakeJobObject(job.Receive()); |
| 305 if (ERROR_SUCCESS != win_result) | 309 if (ERROR_SUCCESS != win_result) |
| 306 return SBOX_ERROR_GENERIC; | 310 return SBOX_ERROR_GENERIC; |
| 307 | 311 |
| 308 if (ERROR_ALREADY_EXISTS == ::GetLastError()) | 312 if (ERROR_ALREADY_EXISTS == ::GetLastError()) |
| 309 return SBOX_ERROR_GENERIC; | 313 return SBOX_ERROR_GENERIC; |
| 310 | 314 |
| 311 // Construct the thread pool here in case it is expensive. | 315 // Construct the thread pool here in case it is expensive. |
| 312 // The thread pool is shared by all the targets | 316 // The thread pool is shared by all the targets |
| 313 if (NULL == thread_pool_) | 317 if (NULL == thread_pool_) |
| 314 thread_pool_ = new Win2kThreadPool(); | 318 thread_pool_ = new Win2kThreadPool(); |
| 315 | 319 |
| 316 // Create the TargetProces object and spawn the target suspended. Note that | 320 // Create the TargetProces object and spawn the target suspended. Note that |
| 317 // Brokerservices does not own the target object. It is owned by the Policy. | 321 // Brokerservices does not own the target object. It is owned by the Policy. |
| 318 PROCESS_INFORMATION process_info = {0}; | 322 base::win::ScopedProcessInformation process_info; |
| 319 TargetProcess* target = new TargetProcess(initial_token, lockdown_token, | 323 TargetProcess* target = new TargetProcess(initial_token.Take(), |
| 320 job, thread_pool_); | 324 lockdown_token.Take(), |
| 325 job, |
| 326 thread_pool_); |
| 321 | 327 |
| 322 std::wstring desktop = policy_base->GetAlternateDesktop(); | 328 std::wstring desktop = policy_base->GetAlternateDesktop(); |
| 323 | 329 |
| 324 win_result = target->Create(exe_path, command_line, | 330 win_result = target->Create(exe_path, command_line, |
| 325 desktop.empty() ? NULL : desktop.c_str(), | 331 desktop.empty() ? NULL : desktop.c_str(), |
| 326 &process_info); | 332 &process_info); |
| 327 if (ERROR_SUCCESS != win_result) | 333 if (ERROR_SUCCESS != win_result) |
| 328 return SpawnCleanup(target, win_result); | 334 return SpawnCleanup(target, win_result); |
| 329 | 335 |
| 330 if ((INVALID_HANDLE_VALUE == process_info.hProcess) || | |
| 331 (INVALID_HANDLE_VALUE == process_info.hThread)) | |
| 332 return SpawnCleanup(target, win_result); | |
| 333 | |
| 334 // Now the policy is the owner of the target. | 336 // Now the policy is the owner of the target. |
| 335 if (!policy_base->AddTarget(target)) { | 337 if (!policy_base->AddTarget(target)) { |
| 336 return SpawnCleanup(target, 0); | 338 return SpawnCleanup(target, 0); |
| 337 } | 339 } |
| 338 | 340 |
| 339 // We are going to keep a pointer to the policy because we'll call it when | 341 // We are going to keep a pointer to the policy because we'll call it when |
| 340 // the job object generates notifications using the completion port. | 342 // the job object generates notifications using the completion port. |
| 341 policy_base->AddRef(); | 343 policy_base->AddRef(); |
| 342 JobTracker* tracker = new JobTracker(job, policy_base); | 344 scoped_ptr<JobTracker> tracker(new JobTracker(job.Take(), policy_base)); |
| 343 if (!AssociateCompletionPort(job, job_port_, tracker)) | 345 if (!AssociateCompletionPort(tracker->job, job_port_, tracker.get())) |
| 344 return SpawnCleanup(target, 0); | 346 return SpawnCleanup(target, 0); |
| 345 // Save the tracker because in cleanup we might need to force closing | 347 // Save the tracker because in cleanup we might need to force closing |
| 346 // the Jobs. | 348 // the Jobs. |
| 347 tracker_list_.push_back(tracker); | 349 tracker_list_.push_back(tracker.release()); |
| 348 child_process_ids_.insert(process_info.dwProcessId); | 350 child_process_ids_.insert(process_info.process_id()); |
| 349 | 351 |
| 350 // We return the caller a duplicate of the process handle so they | 352 *target_info = process_info.Take(); |
| 351 // can close it at will. | |
| 352 HANDLE dup_process_handle = NULL; | |
| 353 if (!::DuplicateHandle(::GetCurrentProcess(), process_info.hProcess, | |
| 354 ::GetCurrentProcess(), &dup_process_handle, | |
| 355 0, FALSE, DUPLICATE_SAME_ACCESS)) | |
| 356 return SpawnCleanup(target, 0); | |
| 357 | |
| 358 *target_info = process_info; | |
| 359 target_info->hProcess = dup_process_handle; | |
| 360 return SBOX_ALL_OK; | 353 return SBOX_ALL_OK; |
| 361 } | 354 } |
| 362 | 355 |
| 363 | 356 |
| 364 ResultCode BrokerServicesBase::WaitForAllTargets() { | 357 ResultCode BrokerServicesBase::WaitForAllTargets() { |
| 365 ::WaitForSingleObject(no_targets_, INFINITE); | 358 ::WaitForSingleObject(no_targets_, INFINITE); |
| 366 return SBOX_ALL_OK; | 359 return SBOX_ALL_OK; |
| 367 } | 360 } |
| 368 | 361 |
| 369 bool BrokerServicesBase::IsActiveTarget(DWORD process_id) { | 362 bool BrokerServicesBase::IsActiveTarget(DWORD process_id) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 peer_map_.erase(peer->id); | 395 peer_map_.erase(peer->id); |
| 403 return SBOX_ERROR_GENERIC; | 396 return SBOX_ERROR_GENERIC; |
| 404 } | 397 } |
| 405 | 398 |
| 406 // Leak the pointer since it will be cleaned up by the callback. | 399 // Leak the pointer since it will be cleaned up by the callback. |
| 407 peer.release(); | 400 peer.release(); |
| 408 return SBOX_ALL_OK; | 401 return SBOX_ALL_OK; |
| 409 } | 402 } |
| 410 | 403 |
| 411 } // namespace sandbox | 404 } // namespace sandbox |
| OLD | NEW |