OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "sandbox/mac/bootstrap_sandbox.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/mac/mach_logging.h" |
| 9 |
| 10 #include "sandbox/mac/launchd_interception_server.h" |
| 11 |
| 12 namespace sandbox { |
| 13 |
| 14 const int kNotAPolicy = -1; |
| 15 |
| 16 // static |
| 17 scoped_ptr<BootstrapSandbox> BootstrapSandbox::Create() { |
| 18 scoped_ptr<BootstrapSandbox> sandbox(new BootstrapSandbox()); |
| 19 sandbox->server_.reset(new LaunchdInterceptionServer(sandbox.get())); |
| 20 |
| 21 if (!sandbox->server_->Initialize()) { |
| 22 sandbox.reset(); |
| 23 } else { |
| 24 kern_return_t kr = task_set_special_port(mach_task_self(), |
| 25 TASK_BOOTSTRAP_PORT, sandbox->server_->server_port()); |
| 26 if (kr != KERN_SUCCESS) |
| 27 sandbox.reset(); |
| 28 } |
| 29 |
| 30 return sandbox.Pass(); |
| 31 } |
| 32 |
| 33 BootstrapSandbox::~BootstrapSandbox() { |
| 34 kern_return_t kr = task_set_special_port(mach_task_self(), |
| 35 TASK_BOOTSTRAP_PORT, real_bootstrap_port_); |
| 36 MACH_CHECK(kr == KERN_SUCCESS, kr); |
| 37 } |
| 38 |
| 39 void BootstrapSandbox::RegisterSandboxPolicy( |
| 40 int sandbox_policy_id, |
| 41 const BootstrapSandboxPolicy& policy) { |
| 42 CHECK(IsPolicyValid(policy)); |
| 43 CHECK_GT(sandbox_policy_id, 0); |
| 44 base::AutoLock lock(lock_); |
| 45 DCHECK(policies_.find(sandbox_policy_id) == policies_.end()); |
| 46 policies_.insert(std::make_pair(sandbox_policy_id, policy)); |
| 47 } |
| 48 |
| 49 void BootstrapSandbox::PrepareToForkWithPolicy(int sandbox_policy_id) { |
| 50 base::AutoLock lock(lock_); |
| 51 |
| 52 CHECK(policies_.find(sandbox_policy_id) != policies_.end()); |
| 53 CHECK_EQ(kNotAPolicy, effective_policy_id_) |
| 54 << "Cannot nest calls to PrepareToForkWithPolicy()"; |
| 55 |
| 56 // Store the policy for the process we're about to create. |
| 57 effective_policy_id_ = sandbox_policy_id; |
| 58 } |
| 59 |
| 60 void BootstrapSandbox::FinishedFork(base::ProcessHandle handle) { |
| 61 base::AutoLock lock(lock_); |
| 62 |
| 63 CHECK_NE(kNotAPolicy, effective_policy_id_) |
| 64 << "Must PrepareToForkWithPolicy() before FinishedFork()"; |
| 65 |
| 66 if (handle != base::kNullProcessHandle) { |
| 67 const auto& existing_process = sandboxed_processes_.find(handle); |
| 68 CHECK(existing_process == sandboxed_processes_.end()); |
| 69 sandboxed_processes_.insert(std::make_pair(handle, effective_policy_id_)); |
| 70 VLOG(3) << "Bootstrap sandbox enforced for pid " << handle; |
| 71 } |
| 72 |
| 73 effective_policy_id_ = kNotAPolicy; |
| 74 } |
| 75 |
| 76 void BootstrapSandbox::ChildDied(base::ProcessHandle handle) { |
| 77 base::AutoLock lock(lock_); |
| 78 const auto& it = sandboxed_processes_.find(handle); |
| 79 CHECK(it != sandboxed_processes_.end()); |
| 80 sandboxed_processes_.erase(it); |
| 81 } |
| 82 |
| 83 const BootstrapSandboxPolicy* BootstrapSandbox::PolicyForProcess( |
| 84 pid_t pid) const { |
| 85 base::AutoLock lock(lock_); |
| 86 const auto& process = sandboxed_processes_.find(pid); |
| 87 |
| 88 // The new child could send bootstrap requests before the parent calls |
| 89 // FinishedFork(). |
| 90 int policy_id = effective_policy_id_; |
| 91 if (process != sandboxed_processes_.end()) { |
| 92 policy_id = process->second; |
| 93 } |
| 94 |
| 95 if (policy_id == kNotAPolicy) |
| 96 return NULL; |
| 97 |
| 98 return &policies_.find(policy_id)->second; |
| 99 } |
| 100 |
| 101 BootstrapSandbox::BootstrapSandbox() |
| 102 : real_bootstrap_port_(MACH_PORT_NULL), |
| 103 effective_policy_id_(kNotAPolicy) { |
| 104 mach_port_t port = MACH_PORT_NULL; |
| 105 kern_return_t kr = task_get_special_port( |
| 106 mach_task_self(), TASK_BOOTSTRAP_PORT, &port); |
| 107 MACH_CHECK(kr == KERN_SUCCESS, kr); |
| 108 real_bootstrap_port_.reset(port); |
| 109 } |
| 110 |
| 111 } // namespace sandbox |
OLD | NEW |