| 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/mach_message_server.h" | 5 #include "sandbox/mac/mach_message_server.h" |
| 6 | 6 |
| 7 #include <bsm/libbsm.h> | 7 #include <bsm/libbsm.h> |
| 8 #include <servers/bootstrap.h> | 8 #include <servers/bootstrap.h> |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/mac/mach_logging.h" | 13 #include "base/mac/mach_logging.h" |
| 14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "sandbox/mac/dispatch_source_mach.h" |
| 15 | 16 |
| 16 namespace sandbox { | 17 namespace sandbox { |
| 17 | 18 |
| 18 MachMessageServer::MachMessageServer( | 19 MachMessageServer::MachMessageServer( |
| 19 MessageDemuxer* demuxer, | 20 MessageDemuxer* demuxer, |
| 20 mach_port_t server_receive_right, | 21 mach_port_t server_receive_right, |
| 21 mach_msg_size_t buffer_size) | 22 mach_msg_size_t buffer_size) |
| 22 : demuxer_(demuxer), | 23 : demuxer_(demuxer), |
| 23 server_port_(server_receive_right), | 24 server_port_(server_receive_right), |
| 24 server_queue_(NULL), | |
| 25 server_source_(NULL), | |
| 26 source_canceled_(dispatch_semaphore_create(0)), | |
| 27 buffer_size_( | 25 buffer_size_( |
| 28 mach_vm_round_page(buffer_size + sizeof(mach_msg_audit_trailer_t))), | 26 mach_vm_round_page(buffer_size + sizeof(mach_msg_audit_trailer_t))), |
| 29 did_forward_message_(false) { | 27 did_forward_message_(false) { |
| 30 DCHECK(demuxer_); | 28 DCHECK(demuxer_); |
| 31 } | 29 } |
| 32 | 30 |
| 33 MachMessageServer::~MachMessageServer() { | 31 MachMessageServer::~MachMessageServer() { |
| 34 if (server_source_) { | |
| 35 dispatch_source_cancel(server_source_); | |
| 36 dispatch_release(server_source_); | |
| 37 | |
| 38 dispatch_semaphore_wait(source_canceled_, DISPATCH_TIME_FOREVER); | |
| 39 dispatch_release(source_canceled_); | |
| 40 } | |
| 41 if (server_queue_) | |
| 42 dispatch_release(server_queue_); | |
| 43 } | 32 } |
| 44 | 33 |
| 45 bool MachMessageServer::Initialize() { | 34 bool MachMessageServer::Initialize() { |
| 46 mach_port_t task = mach_task_self(); | 35 mach_port_t task = mach_task_self(); |
| 47 kern_return_t kr; | 36 kern_return_t kr; |
| 48 | 37 |
| 49 // Allocate a port for use as a new server port if one was not passed to the | 38 // Allocate a port for use as a new server port if one was not passed to the |
| 50 // constructor. | 39 // constructor. |
| 51 if (!server_port_.is_valid()) { | 40 if (!server_port_.is_valid()) { |
| 52 mach_port_t port; | 41 mach_port_t port; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 71 request_buffer_.reset(buffer, buffer_size_); | 60 request_buffer_.reset(buffer, buffer_size_); |
| 72 | 61 |
| 73 kr = vm_allocate(task, &buffer, buffer_size_, kMachMsgMemoryFlags); | 62 kr = vm_allocate(task, &buffer, buffer_size_, kMachMsgMemoryFlags); |
| 74 if (kr != KERN_SUCCESS) { | 63 if (kr != KERN_SUCCESS) { |
| 75 MACH_LOG(ERROR, kr) << "Failed to allocate reply buffer."; | 64 MACH_LOG(ERROR, kr) << "Failed to allocate reply buffer."; |
| 76 return false; | 65 return false; |
| 77 } | 66 } |
| 78 reply_buffer_.reset(buffer, buffer_size_); | 67 reply_buffer_.reset(buffer, buffer_size_); |
| 79 | 68 |
| 80 // Set up the dispatch queue to service the bootstrap port. | 69 // Set up the dispatch queue to service the bootstrap port. |
| 81 // TODO(rsesek): Specify DISPATCH_QUEUE_SERIAL, in the 10.7 SDK. NULL means | |
| 82 // the same thing but is not symbolically clear. | |
| 83 std::string label = base::StringPrintf( | 70 std::string label = base::StringPrintf( |
| 84 "org.chromium.sandbox.MachMessageServer.%p", demuxer_); | 71 "org.chromium.sandbox.MachMessageServer.%p", demuxer_); |
| 85 server_queue_ = dispatch_queue_create(label.c_str(), NULL); | 72 dispatch_source_.reset(new DispatchSourceMach( |
| 86 server_source_ = dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_RECV, | 73 label.c_str(), server_port_.get(), ^{ ReceiveMessage(); })); |
| 87 server_port_.get(), 0, server_queue_); | 74 dispatch_source_->Resume(); |
| 88 dispatch_source_set_event_handler(server_source_, ^{ ReceiveMessage(); }); | |
| 89 dispatch_source_set_cancel_handler(server_source_, ^{ | |
| 90 dispatch_semaphore_signal(source_canceled_); | |
| 91 }); | |
| 92 dispatch_resume(server_source_); | |
| 93 | 75 |
| 94 return true; | 76 return true; |
| 95 } | 77 } |
| 96 | 78 |
| 97 pid_t MachMessageServer::GetMessageSenderPID(IPCMessage request) { | 79 pid_t MachMessageServer::GetMessageSenderPID(IPCMessage request) { |
| 98 // Get the PID of the task that sent this request. This requires getting at | 80 // Get the PID of the task that sent this request. This requires getting at |
| 99 // the trailer of the message, from the header. | 81 // the trailer of the message, from the header. |
| 100 mach_msg_audit_trailer_t* trailer = | 82 mach_msg_audit_trailer_t* trailer = |
| 101 reinterpret_cast<mach_msg_audit_trailer_t*>( | 83 reinterpret_cast<mach_msg_audit_trailer_t*>( |
| 102 reinterpret_cast<vm_address_t>(request.mach) + | 84 reinterpret_cast<vm_address_t>(request.mach) + |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 // forwarded message was sent from the process hosting this sandbox server, | 184 // forwarded message was sent from the process hosting this sandbox server, |
| 203 // destroying the message could also destroy rights held outside the scope of | 185 // destroying the message could also destroy rights held outside the scope of |
| 204 // this message server. | 186 // this message server. |
| 205 if (!did_forward_message_) { | 187 if (!did_forward_message_) { |
| 206 mach_msg_destroy(request); | 188 mach_msg_destroy(request); |
| 207 mach_msg_destroy(reply); | 189 mach_msg_destroy(reply); |
| 208 } | 190 } |
| 209 } | 191 } |
| 210 | 192 |
| 211 } // namespace sandbox | 193 } // namespace sandbox |
| OLD | NEW |