| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/mac/bootstrap_sandbox.h" | 5 #include "sandbox/mac/bootstrap_sandbox.h" |
| 6 | 6 |
| 7 #include <servers/bootstrap.h> |
| 8 #include <unistd.h> |
| 9 |
| 7 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/mac/foundation_util.h" |
| 8 #include "base/mac/mach_logging.h" | 12 #include "base/mac/mach_logging.h" |
| 9 | 13 #include "base/strings/stringprintf.h" |
| 10 #include "sandbox/mac/launchd_interception_server.h" | 14 #include "sandbox/mac/launchd_interception_server.h" |
| 11 | 15 |
| 12 namespace sandbox { | 16 namespace sandbox { |
| 13 | 17 |
| 14 const int kNotAPolicy = -1; | 18 const int kNotAPolicy = -1; |
| 15 | 19 |
| 16 // static | 20 // static |
| 17 scoped_ptr<BootstrapSandbox> BootstrapSandbox::Create() { | 21 scoped_ptr<BootstrapSandbox> BootstrapSandbox::Create() { |
| 18 scoped_ptr<BootstrapSandbox> null; // Used for early returns. | 22 scoped_ptr<BootstrapSandbox> null; // Used for early returns. |
| 19 scoped_ptr<BootstrapSandbox> sandbox(new BootstrapSandbox()); | 23 scoped_ptr<BootstrapSandbox> sandbox(new BootstrapSandbox()); |
| 20 sandbox->server_.reset(new LaunchdInterceptionServer(sandbox.get())); | 24 sandbox->server_.reset(new LaunchdInterceptionServer(sandbox.get())); |
| 21 | 25 |
| 22 if (!sandbox->server_->Initialize()) | 26 // Check in with launchd to get the receive right for the server that is |
| 23 return null.Pass(); | 27 // published in the bootstrap namespace. |
| 24 | 28 mach_port_t port = MACH_PORT_NULL; |
| 25 mach_port_t port = sandbox->server_->server_port(); | 29 kern_return_t kr = bootstrap_check_in(bootstrap_port, |
| 26 kern_return_t kr = mach_port_insert_right(mach_task_self(), port, port, | 30 sandbox->server_bootstrap_name().c_str(), &port); |
| 27 MACH_MSG_TYPE_MAKE_SEND); | |
| 28 if (kr != KERN_SUCCESS) { | 31 if (kr != KERN_SUCCESS) { |
| 29 MACH_LOG(ERROR, kr) << "Failed to insert send right on bootstrap port."; | 32 BOOTSTRAP_LOG(ERROR, kr) |
| 33 << "Failed to bootstrap_check_in the sandbox server."; |
| 30 return null.Pass(); | 34 return null.Pass(); |
| 31 } | 35 } |
| 32 base::mac::ScopedMachSendRight scoped_right(port); | 36 base::mac::ScopedMachReceiveRight scoped_port(port); |
| 33 | 37 |
| 34 // Note that the extern global bootstrap_port (in bootstrap.h) will not | 38 // Start the sandbox server. |
| 35 // be changed here. The parent only has its bootstrap port replaced | 39 if (sandbox->server_->Initialize(scoped_port.get())) |
| 36 // permanently because changing it repeatedly in a multi-threaded program | 40 ignore_result(scoped_port.release()); // Transferred to server_. |
| 37 // could lead to unsafe access patterns. In a single-threaded program, | 41 else |
| 38 // the port would be restored after fork(). See the design document for | |
| 39 // a larger discussion. | |
| 40 // | |
| 41 // By not changing the global bootstrap_port, users of the bootstrap port | |
| 42 // in the parent can potentially skip an unnecessary indirection through | |
| 43 // the sandbox server. | |
| 44 kr = task_set_special_port(mach_task_self(), TASK_BOOTSTRAP_PORT, port); | |
| 45 if (kr != KERN_SUCCESS) { | |
| 46 MACH_LOG(ERROR, kr) << "Failed to set new bootstrap port."; | |
| 47 return null.Pass(); | 42 return null.Pass(); |
| 48 } | |
| 49 | 43 |
| 50 return sandbox.Pass(); | 44 return sandbox.Pass(); |
| 51 } | 45 } |
| 52 | 46 |
| 53 BootstrapSandbox::~BootstrapSandbox() { | 47 BootstrapSandbox::~BootstrapSandbox() { |
| 54 kern_return_t kr = task_set_special_port(mach_task_self(), | |
| 55 TASK_BOOTSTRAP_PORT, real_bootstrap_port_); | |
| 56 MACH_CHECK(kr == KERN_SUCCESS, kr); | |
| 57 } | 48 } |
| 58 | 49 |
| 59 void BootstrapSandbox::RegisterSandboxPolicy( | 50 void BootstrapSandbox::RegisterSandboxPolicy( |
| 60 int sandbox_policy_id, | 51 int sandbox_policy_id, |
| 61 const BootstrapSandboxPolicy& policy) { | 52 const BootstrapSandboxPolicy& policy) { |
| 62 CHECK(IsPolicyValid(policy)); | 53 CHECK(IsPolicyValid(policy)); |
| 63 CHECK_GT(sandbox_policy_id, kNotAPolicy); | 54 CHECK_GT(sandbox_policy_id, kNotAPolicy); |
| 64 base::AutoLock lock(lock_); | 55 base::AutoLock lock(lock_); |
| 65 DCHECK(policies_.find(sandbox_policy_id) == policies_.end()); | 56 DCHECK(policies_.find(sandbox_policy_id) == policies_.end()); |
| 66 policies_.insert(std::make_pair(sandbox_policy_id, policy)); | 57 policies_.insert(std::make_pair(sandbox_policy_id, policy)); |
| 67 } | 58 } |
| 68 | 59 |
| 69 void BootstrapSandbox::PrepareToForkWithPolicy(int sandbox_policy_id) { | 60 void BootstrapSandbox::PrepareToForkWithPolicy(int sandbox_policy_id) { |
| 70 base::AutoLock lock(lock_); | 61 base::AutoLock lock(lock_); |
| 71 | 62 |
| 63 // Verify that this is a real policy. |
| 72 CHECK(policies_.find(sandbox_policy_id) != policies_.end()); | 64 CHECK(policies_.find(sandbox_policy_id) != policies_.end()); |
| 73 CHECK_EQ(kNotAPolicy, effective_policy_id_) | 65 CHECK_EQ(kNotAPolicy, effective_policy_id_) |
| 74 << "Cannot nest calls to PrepareToForkWithPolicy()"; | 66 << "Cannot nest calls to PrepareToForkWithPolicy()"; |
| 75 | 67 |
| 76 // Store the policy for the process we're about to create. | 68 // Store the policy for the process we're about to create. |
| 77 effective_policy_id_ = sandbox_policy_id; | 69 effective_policy_id_ = sandbox_policy_id; |
| 78 } | 70 } |
| 79 | 71 |
| 80 // TODO(rsesek): The |lock_| needs to be taken twice because | 72 // TODO(rsesek): The |lock_| needs to be taken twice because |
| 81 // base::LaunchProcess handles both fork+exec, and holding the lock for the | 73 // base::LaunchProcess handles both fork+exec, and holding the lock for the |
| 82 // duration would block servicing of other bootstrap messages. If a better | 74 // duration would block servicing of other bootstrap messages. If a better |
| 83 // LaunchProcess existed (do arbitrary work without layering violations), this | 75 // LaunchProcess existed (do arbitrary work without layering violations), this |
| 84 // could be avoided. | 76 // could be avoided. |
| 85 | 77 |
| 86 void BootstrapSandbox::FinishedFork(base::ProcessHandle handle) { | 78 void BootstrapSandbox::FinishedFork(base::ProcessHandle handle) { |
| 87 base::AutoLock lock(lock_); | 79 base::AutoLock lock(lock_); |
| 88 | 80 |
| 89 CHECK_NE(kNotAPolicy, effective_policy_id_) | 81 CHECK_NE(kNotAPolicy, effective_policy_id_) |
| 90 << "Must PrepareToForkWithPolicy() before FinishedFork()"; | 82 << "Must PrepareToForkWithPolicy() before FinishedFork()"; |
| 91 | 83 |
| 84 // Apply the policy to the new process. |
| 92 if (handle != base::kNullProcessHandle) { | 85 if (handle != base::kNullProcessHandle) { |
| 93 const auto& existing_process = sandboxed_processes_.find(handle); | 86 const auto& existing_process = sandboxed_processes_.find(handle); |
| 94 CHECK(existing_process == sandboxed_processes_.end()); | 87 CHECK(existing_process == sandboxed_processes_.end()); |
| 95 sandboxed_processes_.insert(std::make_pair(handle, effective_policy_id_)); | 88 sandboxed_processes_.insert(std::make_pair(handle, effective_policy_id_)); |
| 96 VLOG(3) << "Bootstrap sandbox enforced for pid " << handle; | 89 VLOG(3) << "Bootstrap sandbox enforced for pid " << handle; |
| 97 } | 90 } |
| 98 | 91 |
| 99 effective_policy_id_ = kNotAPolicy; | 92 effective_policy_id_ = kNotAPolicy; |
| 100 } | 93 } |
| 101 | 94 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 118 policy_id = process->second; | 111 policy_id = process->second; |
| 119 } | 112 } |
| 120 | 113 |
| 121 if (policy_id == kNotAPolicy) | 114 if (policy_id == kNotAPolicy) |
| 122 return NULL; | 115 return NULL; |
| 123 | 116 |
| 124 return &policies_.find(policy_id)->second; | 117 return &policies_.find(policy_id)->second; |
| 125 } | 118 } |
| 126 | 119 |
| 127 BootstrapSandbox::BootstrapSandbox() | 120 BootstrapSandbox::BootstrapSandbox() |
| 128 : real_bootstrap_port_(MACH_PORT_NULL), | 121 : server_bootstrap_name_( |
| 122 base::StringPrintf("%s.sandbox.%d", base::mac::BaseBundleID(), |
| 123 getpid())), |
| 124 real_bootstrap_port_(MACH_PORT_NULL), |
| 129 effective_policy_id_(kNotAPolicy) { | 125 effective_policy_id_(kNotAPolicy) { |
| 130 mach_port_t port = MACH_PORT_NULL; | 126 mach_port_t port = MACH_PORT_NULL; |
| 131 kern_return_t kr = task_get_special_port( | 127 kern_return_t kr = task_get_special_port( |
| 132 mach_task_self(), TASK_BOOTSTRAP_PORT, &port); | 128 mach_task_self(), TASK_BOOTSTRAP_PORT, &port); |
| 133 MACH_CHECK(kr == KERN_SUCCESS, kr); | 129 MACH_CHECK(kr == KERN_SUCCESS, kr); |
| 134 real_bootstrap_port_.reset(port); | 130 real_bootstrap_port_.reset(port); |
| 135 } | 131 } |
| 136 | 132 |
| 137 } // namespace sandbox | 133 } // namespace sandbox |
| OLD | NEW |