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