Infobar MD refresh: close button.
[chromium-blink-merge.git] / sandbox / mac / mach_message_server.cc
blob7cfcecc6cc2ea4d1e4a358632d38f9e5d4ea84b5
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.
5 #include "sandbox/mac/mach_message_server.h"
7 #include <bsm/libbsm.h>
8 #include <servers/bootstrap.h>
10 #include <string>
12 #include "base/logging.h"
13 #include "base/mac/mach_logging.h"
14 #include "base/strings/stringprintf.h"
16 namespace sandbox {
18 MachMessageServer::MachMessageServer(
19 MessageDemuxer* demuxer,
20 mach_port_t server_receive_right,
21 mach_msg_size_t buffer_size)
22 : demuxer_(demuxer),
23 server_port_(server_receive_right),
24 buffer_size_(
25 mach_vm_round_page(buffer_size + sizeof(mach_msg_audit_trailer_t))),
26 did_forward_message_(false) {
27 DCHECK(demuxer_);
30 MachMessageServer::~MachMessageServer() {
33 bool MachMessageServer::Initialize() {
34 mach_port_t task = mach_task_self();
35 kern_return_t kr;
37 // Allocate a port for use as a new server port if one was not passed to the
38 // constructor.
39 if (!server_port_.is_valid()) {
40 mach_port_t port;
41 if ((kr = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &port)) !=
42 KERN_SUCCESS) {
43 MACH_LOG(ERROR, kr) << "Failed to allocate new server port.";
44 return false;
46 server_port_.reset(port);
49 // Allocate the message request and reply buffers.
50 const int kMachMsgMemoryFlags = VM_MAKE_TAG(VM_MEMORY_MACH_MSG) |
51 VM_FLAGS_ANYWHERE;
52 vm_address_t buffer = 0;
54 kr = vm_allocate(task, &buffer, buffer_size_, kMachMsgMemoryFlags);
55 if (kr != KERN_SUCCESS) {
56 MACH_LOG(ERROR, kr) << "Failed to allocate request buffer.";
57 return false;
59 request_buffer_.reset(buffer, buffer_size_);
61 kr = vm_allocate(task, &buffer, buffer_size_, kMachMsgMemoryFlags);
62 if (kr != KERN_SUCCESS) {
63 MACH_LOG(ERROR, kr) << "Failed to allocate reply buffer.";
64 return false;
66 reply_buffer_.reset(buffer, buffer_size_);
68 // Set up the dispatch queue to service the bootstrap port.
69 std::string label = base::StringPrintf(
70 "org.chromium.sandbox.MachMessageServer.%p", demuxer_);
71 dispatch_source_.reset(new base::DispatchSourceMach(
72 label.c_str(), server_port_.get(), ^{ ReceiveMessage(); }));
73 dispatch_source_->Resume();
75 return true;
78 pid_t MachMessageServer::GetMessageSenderPID(IPCMessage request) {
79 // Get the PID of the task that sent this request. This requires getting at
80 // the trailer of the message, from the header.
81 mach_msg_audit_trailer_t* trailer =
82 reinterpret_cast<mach_msg_audit_trailer_t*>(
83 reinterpret_cast<vm_address_t>(request.mach) +
84 round_msg(request.mach->msgh_size));
85 // TODO(rsesek): In the 10.7 SDK, there's audit_token_to_pid().
86 pid_t sender_pid;
87 audit_token_to_au32(trailer->msgh_audit,
88 NULL, NULL, NULL, NULL, NULL, &sender_pid, NULL, NULL);
89 return sender_pid;
92 IPCMessage MachMessageServer::CreateReply(IPCMessage request_message) {
93 mach_msg_header_t* request = request_message.mach;
95 IPCMessage reply_message;
96 mach_msg_header_t* reply = reply_message.mach =
97 reinterpret_cast<mach_msg_header_t*>(reply_buffer_.address());
98 bzero(reply, buffer_size_);
100 reply->msgh_bits = MACH_MSGH_BITS_REMOTE(reply->msgh_bits);
101 // Since mach_msg will automatically swap the request and reply ports,
102 // undo that.
103 reply->msgh_remote_port = request->msgh_remote_port;
104 reply->msgh_local_port = MACH_PORT_NULL;
105 // MIG servers simply add 100 to the request ID to generate the reply ID.
106 reply->msgh_id = request->msgh_id + 100;
108 return reply_message;
111 bool MachMessageServer::SendReply(IPCMessage reply) {
112 kern_return_t kr = mach_msg(reply.mach, MACH_SEND_MSG,
113 reply.mach->msgh_size, 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE,
114 MACH_PORT_NULL);
115 MACH_LOG_IF(ERROR, kr != KERN_SUCCESS, kr)
116 << "Unable to send intercepted reply message.";
117 return kr == KERN_SUCCESS;
120 void MachMessageServer::ForwardMessage(IPCMessage message,
121 mach_port_t destination) {
122 mach_msg_header_t* request = message.mach;
123 request->msgh_local_port = request->msgh_remote_port;
124 request->msgh_remote_port = destination;
125 // Preserve the msgh_bits that do not deal with the local and remote ports.
126 request->msgh_bits = (request->msgh_bits & ~MACH_MSGH_BITS_PORTS_MASK) |
127 MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MOVE_SEND_ONCE);
128 kern_return_t kr = mach_msg_send(request);
129 if (kr == KERN_SUCCESS) {
130 did_forward_message_ = true;
131 } else {
132 MACH_LOG(ERROR, kr) << "Unable to forward message to the real launchd.";
136 void MachMessageServer::RejectMessage(IPCMessage request, int error_code) {
137 IPCMessage reply = CreateReply(request);
138 mig_reply_error_t* error_reply =
139 reinterpret_cast<mig_reply_error_t*>(reply.mach);
140 error_reply->Head.msgh_size = sizeof(mig_reply_error_t);
141 error_reply->Head.msgh_bits =
142 MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_MOVE_SEND_ONCE);
143 error_reply->NDR = NDR_record;
144 error_reply->RetCode = error_code;
145 SendReply(reply);
148 mach_port_t MachMessageServer::GetServerPort() const {
149 return server_port_.get();
152 void MachMessageServer::ReceiveMessage() {
153 const mach_msg_options_t kRcvOptions = MACH_RCV_MSG |
154 MACH_RCV_TRAILER_TYPE(MACH_MSG_TRAILER_FORMAT_0) |
155 MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AUDIT);
157 mach_msg_header_t* request =
158 reinterpret_cast<mach_msg_header_t*>(request_buffer_.address());
159 mach_msg_header_t* reply =
160 reinterpret_cast<mach_msg_header_t*>(reply_buffer_.address());
162 // Zero out the buffers from handling any previous message.
163 bzero(request, buffer_size_);
164 bzero(reply, buffer_size_);
165 did_forward_message_ = false;
167 // A Mach message server-once. The system library to run a message server
168 // cannot be used here, because some requests are conditionally forwarded
169 // to another server.
170 kern_return_t kr = mach_msg(request, kRcvOptions, 0, buffer_size_,
171 server_port_.get(), MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
172 if (kr != KERN_SUCCESS) {
173 MACH_LOG(ERROR, kr) << "Unable to receive message.";
174 return;
177 // Process the message.
178 IPCMessage request_message = { request };
179 demuxer_->DemuxMessage(request_message);
181 // Free any descriptors in the message body. If the message was forwarded,
182 // any descriptors would have been moved out of the process on send. If the
183 // forwarded message was sent from the process hosting this sandbox server,
184 // destroying the message could also destroy rights held outside the scope of
185 // this message server.
186 if (!did_forward_message_) {
187 mach_msg_destroy(request);
188 mach_msg_destroy(reply);
192 } // namespace sandbox