| 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 struct PeerTracker { | 60 struct PeerTracker { |
| 58 HANDLE wait_object; | 61 HANDLE wait_object; |
| 59 base::win::ScopedHandle process; | 62 base::win::ScopedHandle process; |
| 60 DWORD id; | 63 DWORD id; |
| 61 HANDLE job_port; | 64 HANDLE job_port; |
| 62 PeerTracker(DWORD process_id, HANDLE broker_job_port) | 65 PeerTracker(DWORD process_id, HANDLE broker_job_port) |
| 63 : wait_object(NULL), id(process_id), job_port(broker_job_port) { | 66 : wait_object(NULL), id(process_id), job_port(broker_job_port) { |
| 64 } | 67 } |
| 65 }; | 68 }; |
| 66 | 69 |
| 70 void DeregisterPeerTracker(PeerTracker* peer) { |
| 71 // Deregistration shouldn't fail, but we leak rather than crash if it does. |
| 72 if (::UnregisterWaitEx(peer->wait_object, INVALID_HANDLE_VALUE)) { |
| 73 delete peer; |
| 74 } else { |
| 75 NOTREACHED(); |
| 76 } |
| 77 } |
| 78 |
| 67 } // namespace | 79 } // namespace |
| 68 | 80 |
| 69 namespace sandbox { | 81 namespace sandbox { |
| 70 | 82 |
| 71 BrokerServicesBase::BrokerServicesBase() | 83 BrokerServicesBase::BrokerServicesBase() |
| 72 : thread_pool_(NULL), job_port_(NULL), no_targets_(NULL), | 84 : thread_pool_(NULL), job_port_(NULL), no_targets_(NULL), |
| 73 job_thread_(NULL) { | 85 job_thread_(NULL) { |
| 74 } | 86 } |
| 75 | 87 |
| 76 // The broker uses a dedicated worker thread that services the job completion | 88 // The broker uses a dedicated worker thread that services the job completion |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 FreeResources(tracker); | 136 FreeResources(tracker); |
| 125 delete tracker; | 137 delete tracker; |
| 126 } | 138 } |
| 127 ::CloseHandle(job_thread_); | 139 ::CloseHandle(job_thread_); |
| 128 delete thread_pool_; | 140 delete thread_pool_; |
| 129 ::CloseHandle(no_targets_); | 141 ::CloseHandle(no_targets_); |
| 130 | 142 |
| 131 // Cancel the wait events and delete remaining peer trackers. | 143 // Cancel the wait events and delete remaining peer trackers. |
| 132 for (PeerTrackerMap::iterator it = peer_map_.begin(); | 144 for (PeerTrackerMap::iterator it = peer_map_.begin(); |
| 133 it != peer_map_.end(); ++it) { | 145 it != peer_map_.end(); ++it) { |
| 134 // Deregistration shouldn't fail, but we leak rather than crash if it does. | 146 DeregisterPeerTracker(it->second); |
| 135 if (::UnregisterWaitEx(it->second->wait_object, INVALID_HANDLE_VALUE)) { | |
| 136 delete it->second; | |
| 137 } else { | |
| 138 NOTREACHED(); | |
| 139 } | |
| 140 } | 147 } |
| 141 | 148 |
| 142 // If job_port_ isn't NULL, assumes that the lock has been initialized. | 149 // If job_port_ isn't NULL, assumes that the lock has been initialized. |
| 143 if (job_port_) | 150 if (job_port_) |
| 144 ::DeleteCriticalSection(&lock_); | 151 ::DeleteCriticalSection(&lock_); |
| 145 } | 152 } |
| 146 | 153 |
| 147 TargetPolicy* BrokerServicesBase::CreatePolicy() { | 154 TargetPolicy* BrokerServicesBase::CreatePolicy() { |
| 148 // If you change the type of the object being created here you must also | 155 // If you change the type of the object being created here you must also |
| 149 // change the downcast to it in SpawnTarget(). | 156 // change the downcast to it in SpawnTarget(). |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 | 240 |
| 234 case JOB_OBJECT_MSG_ACTIVE_PROCESS_LIMIT: { | 241 case JOB_OBJECT_MSG_ACTIVE_PROCESS_LIMIT: { |
| 235 break; | 242 break; |
| 236 } | 243 } |
| 237 | 244 |
| 238 default: { | 245 default: { |
| 239 NOTREACHED(); | 246 NOTREACHED(); |
| 240 break; | 247 break; |
| 241 } | 248 } |
| 242 } | 249 } |
| 243 | |
| 244 } else if (THREAD_CTRL_REMOVE_PEER == key) { | 250 } else if (THREAD_CTRL_REMOVE_PEER == key) { |
| 245 // Remove a process from our list of peers. | 251 // Remove a process from our list of peers. |
| 246 AutoLock lock(&broker->lock_); | 252 AutoLock lock(&broker->lock_); |
| 247 PeerTrackerMap::iterator it = | 253 PeerTrackerMap::iterator it = |
| 248 broker->peer_map_.find(reinterpret_cast<DWORD>(ovl)); | 254 broker->peer_map_.find(reinterpret_cast<DWORD>(ovl)); |
| 249 // This shouldn't fail, but if it does leak the memory rather than crash. | 255 DeregisterPeerTracker(it->second); |
| 250 if (::UnregisterWaitEx(it->second->wait_object, INVALID_HANDLE_VALUE)) { | 256 broker->peer_map_.erase(it); |
| 251 delete it->second; | |
| 252 broker->peer_map_.erase(it); | |
| 253 } else { | |
| 254 NOTREACHED(); | |
| 255 } | |
| 256 | |
| 257 } else if (THREAD_CTRL_QUIT == key) { | 257 } else if (THREAD_CTRL_QUIT == key) { |
| 258 // The broker object is being destroyed so the thread needs to exit. | 258 // The broker object is being destroyed so the thread needs to exit. |
| 259 return 0; | 259 return 0; |
| 260 | |
| 261 } else { | 260 } else { |
| 262 // We have not implemented more commands. | 261 // We have not implemented more commands. |
| 263 NOTREACHED(); | 262 NOTREACHED(); |
| 264 } | 263 } |
| 265 } | 264 } |
| 266 | 265 |
| 267 NOTREACHED(); | 266 NOTREACHED(); |
| 268 return 0; | 267 return 0; |
| 269 } | 268 } |
| 270 | 269 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 287 static DWORD thread_id = ::GetCurrentThreadId(); | 286 static DWORD thread_id = ::GetCurrentThreadId(); |
| 288 DCHECK(thread_id == ::GetCurrentThreadId()); | 287 DCHECK(thread_id == ::GetCurrentThreadId()); |
| 289 | 288 |
| 290 AutoLock lock(&lock_); | 289 AutoLock lock(&lock_); |
| 291 | 290 |
| 292 // This downcast is safe as long as we control CreatePolicy() | 291 // This downcast is safe as long as we control CreatePolicy() |
| 293 PolicyBase* policy_base = static_cast<PolicyBase*>(policy); | 292 PolicyBase* policy_base = static_cast<PolicyBase*>(policy); |
| 294 | 293 |
| 295 // Construct the tokens and the job object that we are going to associate | 294 // Construct the tokens and the job object that we are going to associate |
| 296 // with the soon to be created target process. | 295 // with the soon to be created target process. |
| 297 HANDLE lockdown_token = NULL; | 296 HANDLE initial_token_temp; |
| 298 HANDLE initial_token = NULL; | 297 HANDLE lockdown_token_temp; |
| 299 DWORD win_result = policy_base->MakeTokens(&initial_token, &lockdown_token); | 298 DWORD win_result = policy_base->MakeTokens(&initial_token_temp, |
| 299 &lockdown_token_temp); |
| 300 base::win::ScopedHandle initial_token(initial_token_temp); |
| 301 base::win::ScopedHandle lockdown_token(lockdown_token_temp); |
| 302 |
| 300 if (ERROR_SUCCESS != win_result) | 303 if (ERROR_SUCCESS != win_result) |
| 301 return SBOX_ERROR_GENERIC; | 304 return SBOX_ERROR_GENERIC; |
| 302 | 305 |
| 303 HANDLE job = NULL; | 306 HANDLE job_temp; |
| 304 win_result = policy_base->MakeJobObject(&job); | 307 win_result = policy_base->MakeJobObject(&job_temp); |
| 308 base::win::ScopedHandle job(job_temp); |
| 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) { |
| 370 AutoLock lock(&lock_); | 363 AutoLock lock(&lock_); |
| 371 return child_process_ids_.find(process_id) != child_process_ids_.end() || | 364 return child_process_ids_.find(process_id) != child_process_ids_.end() || |
| 372 peer_map_.find(process_id) != peer_map_.end(); | 365 peer_map_.find(process_id) != peer_map_.end(); |
| 373 } | 366 } |
| 374 | 367 |
| 375 VOID CALLBACK BrokerServicesBase::RemovePeer(PVOID parameter, BOOLEAN) { | 368 VOID CALLBACK BrokerServicesBase::RemovePeer(PVOID parameter, BOOLEAN timeout) { |
| 376 PeerTracker* peer = reinterpret_cast<PeerTracker*>(parameter); | 369 PeerTracker* peer = reinterpret_cast<PeerTracker*>(parameter); |
| 377 // Don't check the return code because we this may fail (safely) at shutdown. | 370 // Don't check the return code because we this may fail (safely) at shutdown. |
| 378 ::PostQueuedCompletionStatus(peer->job_port, 0, THREAD_CTRL_REMOVE_PEER, | 371 ::PostQueuedCompletionStatus(peer->job_port, 0, THREAD_CTRL_REMOVE_PEER, |
| 379 reinterpret_cast<LPOVERLAPPED>(peer->id)); | 372 reinterpret_cast<LPOVERLAPPED>(peer->id)); |
| 380 } | 373 } |
| 381 | 374 |
| 382 ResultCode BrokerServicesBase::AddTargetPeer(HANDLE peer_process) { | 375 ResultCode BrokerServicesBase::AddTargetPeer(HANDLE peer_process) { |
| 383 scoped_ptr<PeerTracker> peer(new PeerTracker(::GetProcessId(peer_process), | 376 scoped_ptr<PeerTracker> peer(new PeerTracker(::GetProcessId(peer_process), |
| 384 job_port_)); | 377 job_port_)); |
| 385 if (!peer->id) | 378 if (!peer->id) |
| 386 return SBOX_ERROR_GENERIC; | 379 return SBOX_ERROR_GENERIC; |
| 387 | 380 |
| 381 HANDLE process_handle; |
| 388 if (!::DuplicateHandle(::GetCurrentProcess(), peer_process, | 382 if (!::DuplicateHandle(::GetCurrentProcess(), peer_process, |
| 389 ::GetCurrentProcess(), peer->process.Receive(), | 383 ::GetCurrentProcess(), &process_handle, |
| 390 SYNCHRONIZE, FALSE, 0)) { | 384 SYNCHRONIZE, FALSE, 0)) { |
| 391 return SBOX_ERROR_GENERIC; | 385 return SBOX_ERROR_GENERIC; |
| 392 } | 386 } |
| 387 peer->process.Set(process_handle); |
| 393 | 388 |
| 394 AutoLock lock(&lock_); | 389 AutoLock lock(&lock_); |
| 395 if (!peer_map_.insert(std::make_pair(peer->id, peer.get())).second) | 390 if (!peer_map_.insert(std::make_pair(peer->id, peer.get())).second) |
| 396 return SBOX_ERROR_BAD_PARAMS; | 391 return SBOX_ERROR_BAD_PARAMS; |
| 397 | 392 |
| 398 if (!::RegisterWaitForSingleObject(&peer->wait_object, | 393 if (!::RegisterWaitForSingleObject( |
| 399 peer->process, RemovePeer, | 394 &peer->wait_object, peer->process, RemovePeer, peer.get(), INFINITE, |
| 400 peer.get(), INFINITE, WT_EXECUTEONLYONCE | | 395 WT_EXECUTEONLYONCE | WT_EXECUTEINWAITTHREAD)) { |
| 401 WT_EXECUTEINWAITTHREAD)) { | |
| 402 peer_map_.erase(peer->id); | 396 peer_map_.erase(peer->id); |
| 403 return SBOX_ERROR_GENERIC; | 397 return SBOX_ERROR_GENERIC; |
| 404 } | 398 } |
| 405 | 399 |
| 406 // Leak the pointer since it will be cleaned up by the callback. | 400 // Release the pointer since it will be cleaned up by the callback. |
| 407 peer.release(); | 401 peer.release(); |
| 408 return SBOX_ALL_OK; | 402 return SBOX_ALL_OK; |
| 409 } | 403 } |
| 410 | 404 |
| 411 } // namespace sandbox | 405 } // namespace sandbox |
| OLD | NEW |