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/launchd_interception_server.h" | 5 #include "sandbox/mac/launchd_interception_server.h" |
6 | 6 |
7 #include <servers/bootstrap.h> | 7 #include <servers/bootstrap.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/mac/mach_logging.h" | 10 #include "base/mac/mach_logging.h" |
11 #include "sandbox/mac/bootstrap_sandbox.h" | 11 #include "sandbox/mac/bootstrap_sandbox.h" |
12 | 12 |
13 namespace sandbox { | 13 namespace sandbox { |
14 | 14 |
15 // The buffer size for all launchd messages. This comes from | 15 // The buffer size for all launchd messages. This comes from |
16 // sizeof(union __RequestUnion__vproc_mig_job_subsystem) in launchd, and it | 16 // sizeof(union __RequestUnion__vproc_mig_job_subsystem) in launchd, and it |
17 // is larger than the __ReplyUnion. | 17 // is larger than the __ReplyUnion. |
18 const mach_msg_size_t kBufferSize = 2096; | 18 const mach_msg_size_t kBufferSize = 2096; |
19 | 19 |
20 LaunchdInterceptionServer::LaunchdInterceptionServer( | 20 LaunchdInterceptionServer::LaunchdInterceptionServer( |
21 const BootstrapSandbox* sandbox) | 21 const BootstrapSandbox* sandbox) |
22 : sandbox_(sandbox), | 22 : sandbox_(sandbox), |
23 sandbox_port_(MACH_PORT_NULL), | 23 sandbox_port_(MACH_PORT_NULL), |
24 compat_shim_(GetLaunchdCompatibilityShim()) { | 24 compat_shim_(GetLaunchdCompatibilityShim()) { |
25 } | 25 } |
26 | 26 |
27 LaunchdInterceptionServer::~LaunchdInterceptionServer() { | 27 LaunchdInterceptionServer::~LaunchdInterceptionServer() { |
28 } | 28 } |
29 | 29 |
30 bool LaunchdInterceptionServer::Initialize() { | 30 bool LaunchdInterceptionServer::Initialize(mach_port_t server_receive_right) { |
31 mach_port_t task = mach_task_self(); | 31 mach_port_t task = mach_task_self(); |
32 kern_return_t kr; | 32 kern_return_t kr; |
33 | 33 |
34 // Allocate the dummy sandbox port. | 34 // Allocate the dummy sandbox port. |
35 mach_port_t port; | 35 mach_port_t port; |
36 if ((kr = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &port)) != | 36 if ((kr = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &port)) != |
37 KERN_SUCCESS) { | 37 KERN_SUCCESS) { |
38 MACH_LOG(ERROR, kr) << "Failed to allocate dummy sandbox port."; | 38 MACH_LOG(ERROR, kr) << "Failed to allocate dummy sandbox port."; |
39 return false; | 39 return false; |
40 } | 40 } |
41 sandbox_port_.reset(port); | 41 sandbox_port_.reset(port); |
42 if ((kr = mach_port_insert_right(task, sandbox_port_, sandbox_port_, | 42 if ((kr = mach_port_insert_right(task, sandbox_port_, sandbox_port_, |
43 MACH_MSG_TYPE_MAKE_SEND) != KERN_SUCCESS)) { | 43 MACH_MSG_TYPE_MAKE_SEND) != KERN_SUCCESS)) { |
44 MACH_LOG(ERROR, kr) << "Failed to allocate dummy sandbox port send right."; | 44 MACH_LOG(ERROR, kr) << "Failed to allocate dummy sandbox port send right."; |
45 return false; | 45 return false; |
46 } | 46 } |
47 sandbox_send_port_.reset(sandbox_port_); | 47 sandbox_send_port_.reset(sandbox_port_); |
48 | 48 |
49 message_server_.reset(new MachMessageServer(this, kBufferSize)); | 49 message_server_.reset( |
| 50 new MachMessageServer(this, server_receive_right, kBufferSize)); |
50 return message_server_->Initialize(); | 51 return message_server_->Initialize(); |
51 } | 52 } |
52 | 53 |
53 void LaunchdInterceptionServer::DemuxMessage(mach_msg_header_t* request, | 54 void LaunchdInterceptionServer::DemuxMessage(mach_msg_header_t* request, |
54 mach_msg_header_t* reply) { | 55 mach_msg_header_t* reply) { |
55 VLOG(3) << "Incoming message #" << request->msgh_id; | 56 VLOG(3) << "Incoming message #" << request->msgh_id; |
56 | 57 |
57 pid_t sender_pid = message_server_->GetMessageSenderPID(request); | 58 pid_t sender_pid = message_server_->GetMessageSenderPID(request); |
58 const BootstrapSandboxPolicy* policy = | 59 const BootstrapSandboxPolicy* policy = |
59 sandbox_->PolicyForProcess(sender_pid); | 60 sandbox_->PolicyForProcess(sender_pid); |
60 if (policy == NULL) { | 61 if (policy == NULL) { |
61 // No sandbox policy is in place for the sender of this message, which | 62 // No sandbox policy is in place for the sender of this message, which |
62 // means it is from the sandbox host process or an unsandboxed child. | 63 // means it came from the unknown. Reject it. |
63 VLOG(3) << "Message from pid " << sender_pid << " forwarded to launchd"; | 64 VLOG(3) << "Message from unknown pid " << sender_pid << " rejected."; |
64 ForwardMessage(request); | 65 message_server_->RejectMessage(request, MIG_REMOTE_ERROR); |
65 return; | 66 return; |
66 } | 67 } |
67 | 68 |
68 if (request->msgh_id == compat_shim_.msg_id_look_up2) { | 69 if (request->msgh_id == compat_shim_.msg_id_look_up2) { |
69 // Filter messages sent via bootstrap_look_up to enforce the sandbox policy | 70 // Filter messages sent via bootstrap_look_up to enforce the sandbox policy |
70 // over the bootstrap namespace. | 71 // over the bootstrap namespace. |
71 HandleLookUp(request, reply, policy); | 72 HandleLookUp(request, reply, policy); |
72 } else if (request->msgh_id == compat_shim_.msg_id_swap_integer) { | 73 } else if (request->msgh_id == compat_shim_.msg_id_swap_integer) { |
73 // Ensure that any vproc_swap_integer requests are safe. | 74 // Ensure that any vproc_swap_integer requests are safe. |
74 HandleSwapInteger(request, reply); | 75 HandleSwapInteger(request, reply); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 } else { | 144 } else { |
144 VLOG(2) << "Rejecting non-read-only swap_integer message."; | 145 VLOG(2) << "Rejecting non-read-only swap_integer message."; |
145 message_server_->RejectMessage(reply, BOOTSTRAP_NOT_PRIVILEGED); | 146 message_server_->RejectMessage(reply, BOOTSTRAP_NOT_PRIVILEGED); |
146 } | 147 } |
147 } | 148 } |
148 void LaunchdInterceptionServer::ForwardMessage(mach_msg_header_t* request) { | 149 void LaunchdInterceptionServer::ForwardMessage(mach_msg_header_t* request) { |
149 message_server_->ForwardMessage(request, sandbox_->real_bootstrap_port()); | 150 message_server_->ForwardMessage(request, sandbox_->real_bootstrap_port()); |
150 } | 151 } |
151 | 152 |
152 } // namespace sandbox | 153 } // namespace sandbox |
OLD | NEW |