Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: sandbox/src/broker_services.cc

Issue 9959018: Use ScopedProcessInformation and other RAII types in sandbox. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update copyright years. Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sandbox/src/Wow64.cc ('k') | sandbox/src/interception_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 static DWORD thread_id = ::GetCurrentThreadId(); 241 static DWORD thread_id = ::GetCurrentThreadId();
239 DCHECK(thread_id == ::GetCurrentThreadId()); 242 DCHECK(thread_id == ::GetCurrentThreadId());
240 243
241 AutoLock lock(&lock_); 244 AutoLock lock(&lock_);
242 245
243 // This downcast is safe as long as we control CreatePolicy() 246 // This downcast is safe as long as we control CreatePolicy()
244 PolicyBase* policy_base = static_cast<PolicyBase*>(policy); 247 PolicyBase* policy_base = static_cast<PolicyBase*>(policy);
245 248
246 // Construct the tokens and the job object that we are going to associate 249 // Construct the tokens and the job object that we are going to associate
247 // with the soon to be created target process. 250 // with the soon to be created target process.
248 HANDLE lockdown_token = NULL; 251 base::win::ScopedHandle lockdown_token;
249 HANDLE initial_token = NULL; 252 base::win::ScopedHandle initial_token;
250 DWORD win_result = policy_base->MakeTokens(&initial_token, &lockdown_token); 253 DWORD win_result = policy_base->MakeTokens(initial_token.Receive(),
254 lockdown_token.Receive());
251 if (ERROR_SUCCESS != win_result) 255 if (ERROR_SUCCESS != win_result)
252 return SBOX_ERROR_GENERIC; 256 return SBOX_ERROR_GENERIC;
253 257
254 HANDLE job = NULL; 258 base::win::ScopedHandle job;
255 win_result = policy_base->MakeJobObject(&job); 259 win_result = policy_base->MakeJobObject(job.Receive());
256 if (ERROR_SUCCESS != win_result) 260 if (ERROR_SUCCESS != win_result)
257 return SBOX_ERROR_GENERIC; 261 return SBOX_ERROR_GENERIC;
258 262
259 if (ERROR_ALREADY_EXISTS == ::GetLastError()) 263 if (ERROR_ALREADY_EXISTS == ::GetLastError())
260 return SBOX_ERROR_GENERIC; 264 return SBOX_ERROR_GENERIC;
261 265
262 // Construct the thread pool here in case it is expensive. 266 // Construct the thread pool here in case it is expensive.
263 // The thread pool is shared by all the targets 267 // The thread pool is shared by all the targets
264 if (NULL == thread_pool_) 268 if (NULL == thread_pool_)
265 thread_pool_ = new Win2kThreadPool(); 269 thread_pool_ = new Win2kThreadPool();
266 270
267 // Create the TargetProces object and spawn the target suspended. Note that 271 // Create the TargetProces object and spawn the target suspended. Note that
268 // Brokerservices does not own the target object. It is owned by the Policy. 272 // Brokerservices does not own the target object. It is owned by the Policy.
269 PROCESS_INFORMATION process_info = {0}; 273 base::win::ScopedProcessInformation process_info;
270 TargetProcess* target = new TargetProcess(initial_token, lockdown_token, 274 TargetProcess* target = new TargetProcess(initial_token.Take(),
271 job, thread_pool_); 275 lockdown_token.Take(),
276 job,
277 thread_pool_);
272 278
273 std::wstring desktop = policy_base->GetAlternateDesktop(); 279 std::wstring desktop = policy_base->GetAlternateDesktop();
274 280
275 win_result = target->Create(exe_path, command_line, 281 win_result = target->Create(exe_path, command_line,
276 desktop.empty() ? NULL : desktop.c_str(), 282 desktop.empty() ? NULL : desktop.c_str(),
277 &process_info); 283 &process_info);
278 if (ERROR_SUCCESS != win_result) 284 if (ERROR_SUCCESS != win_result)
279 return SpawnCleanup(target, win_result); 285 return SpawnCleanup(target, win_result);
280 286
281 if ((INVALID_HANDLE_VALUE == process_info.hProcess) ||
282 (INVALID_HANDLE_VALUE == process_info.hThread))
283 return SpawnCleanup(target, win_result);
284
285 // Now the policy is the owner of the target. 287 // Now the policy is the owner of the target.
286 if (!policy_base->AddTarget(target)) { 288 if (!policy_base->AddTarget(target)) {
287 return SpawnCleanup(target, 0); 289 return SpawnCleanup(target, 0);
288 } 290 }
289 291
290 // We are going to keep a pointer to the policy because we'll call it when 292 // We are going to keep a pointer to the policy because we'll call it when
291 // the job object generates notifications using the completion port. 293 // the job object generates notifications using the completion port.
292 policy_base->AddRef(); 294 policy_base->AddRef();
293 JobTracker* tracker = new JobTracker(job, policy_base); 295 scoped_ptr<JobTracker> tracker(new JobTracker(job.Take(), policy_base));
294 if (!AssociateCompletionPort(job, job_port_, tracker)) 296 if (!AssociateCompletionPort(tracker->job, job_port_, tracker.get()))
295 return SpawnCleanup(target, 0); 297 return SpawnCleanup(target, 0);
296 // Save the tracker because in cleanup we might need to force closing 298 // Save the tracker because in cleanup we might need to force closing
297 // the Jobs. 299 // the Jobs.
298 tracker_list_.push_back(tracker); 300 tracker_list_.push_back(tracker.release());
299 child_process_ids_.insert(process_info.dwProcessId); 301 child_process_ids_.insert(process_info.process_id());
300 302
301 // We return the caller a duplicate of the process handle so they 303 *target_info = process_info.Take();
302 // can close it at will.
303 HANDLE dup_process_handle = NULL;
304 if (!::DuplicateHandle(::GetCurrentProcess(), process_info.hProcess,
305 ::GetCurrentProcess(), &dup_process_handle,
306 0, FALSE, DUPLICATE_SAME_ACCESS))
307 return SpawnCleanup(target, 0);
308
309 *target_info = process_info;
310 target_info->hProcess = dup_process_handle;
311 return SBOX_ALL_OK; 304 return SBOX_ALL_OK;
312 } 305 }
313 306
314 307
315 ResultCode BrokerServicesBase::WaitForAllTargets() { 308 ResultCode BrokerServicesBase::WaitForAllTargets() {
316 ::WaitForSingleObject(no_targets_, INFINITE); 309 ::WaitForSingleObject(no_targets_, INFINITE);
317 return SBOX_ALL_OK; 310 return SBOX_ALL_OK;
318 } 311 }
319 312
320 bool BrokerServicesBase::IsActiveTarget(DWORD process_id) { 313 bool BrokerServicesBase::IsActiveTarget(DWORD process_id) {
321 AutoLock lock(&lock_); 314 AutoLock lock(&lock_);
322 return child_process_ids_.find(process_id) != child_process_ids_.end(); 315 return child_process_ids_.find(process_id) != child_process_ids_.end();
323 } 316 }
324 317
325 } // namespace sandbox 318 } // namespace sandbox
OLDNEW
« no previous file with comments | « sandbox/src/Wow64.cc ('k') | sandbox/src/interception_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698