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 | 15 |
16 namespace sandbox { | 16 namespace sandbox { |
17 | 17 |
18 MachMessageServer::MachMessageServer( | 18 MachMessageServer::MachMessageServer( |
19 MessageDemuxer* demuxer, | 19 MessageDemuxer* demuxer, |
| 20 mach_port_t server_receive_right, |
20 mach_msg_size_t buffer_size) | 21 mach_msg_size_t buffer_size) |
21 : demuxer_(demuxer), | 22 : demuxer_(demuxer), |
22 server_port_(MACH_PORT_NULL), | 23 server_port_(server_receive_right), |
23 server_queue_(NULL), | 24 server_queue_(NULL), |
24 server_source_(NULL), | 25 server_source_(NULL), |
25 buffer_size_( | 26 buffer_size_( |
26 mach_vm_round_page(buffer_size + sizeof(mach_msg_audit_trailer_t))), | 27 mach_vm_round_page(buffer_size + sizeof(mach_msg_audit_trailer_t))), |
27 did_forward_message_(false) { | 28 did_forward_message_(false) { |
28 DCHECK(demuxer_); | 29 DCHECK(demuxer_); |
29 } | 30 } |
30 | 31 |
31 MachMessageServer::~MachMessageServer() { | 32 MachMessageServer::~MachMessageServer() { |
32 if (server_source_) | 33 if (server_source_) |
33 dispatch_release(server_source_); | 34 dispatch_release(server_source_); |
34 if (server_queue_) | 35 if (server_queue_) |
35 dispatch_release(server_queue_); | 36 dispatch_release(server_queue_); |
36 } | 37 } |
37 | 38 |
38 bool MachMessageServer::Initialize() { | 39 bool MachMessageServer::Initialize() { |
39 mach_port_t task = mach_task_self(); | 40 mach_port_t task = mach_task_self(); |
40 kern_return_t kr; | 41 kern_return_t kr; |
41 | 42 |
42 // Allocate a port for use as a new server port. | 43 // Allocate a port for use as a new server port if one was not passed to the |
43 mach_port_t port; | 44 // constructor. |
44 if ((kr = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &port)) != | 45 if (!server_port_.is_valid()) { |
45 KERN_SUCCESS) { | 46 mach_port_t port; |
46 MACH_LOG(ERROR, kr) << "Failed to allocate new server port."; | 47 if ((kr = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &port)) != |
47 return false; | 48 KERN_SUCCESS) { |
| 49 MACH_LOG(ERROR, kr) << "Failed to allocate new server port."; |
| 50 return false; |
| 51 } |
| 52 server_port_.reset(port); |
48 } | 53 } |
49 server_port_.reset(port); | |
50 | 54 |
51 // Allocate the message request and reply buffers. | 55 // Allocate the message request and reply buffers. |
52 const int kMachMsgMemoryFlags = VM_MAKE_TAG(VM_MEMORY_MACH_MSG) | | 56 const int kMachMsgMemoryFlags = VM_MAKE_TAG(VM_MEMORY_MACH_MSG) | |
53 VM_FLAGS_ANYWHERE; | 57 VM_FLAGS_ANYWHERE; |
54 vm_address_t buffer = 0; | 58 vm_address_t buffer = 0; |
55 | 59 |
56 kr = vm_allocate(task, &buffer, buffer_size_, kMachMsgMemoryFlags); | 60 kr = vm_allocate(task, &buffer, buffer_size_, kMachMsgMemoryFlags); |
57 if (kr != KERN_SUCCESS) { | 61 if (kr != KERN_SUCCESS) { |
58 MACH_LOG(ERROR, kr) << "Failed to allocate request buffer."; | 62 MACH_LOG(ERROR, kr) << "Failed to allocate request buffer."; |
59 return false; | 63 return false; |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 // forwarded message was sent from the process hosting this sandbox server, | 175 // forwarded message was sent from the process hosting this sandbox server, |
172 // destroying the message could also destroy rights held outside the scope of | 176 // destroying the message could also destroy rights held outside the scope of |
173 // this message server. | 177 // this message server. |
174 if (!did_forward_message_) { | 178 if (!did_forward_message_) { |
175 mach_msg_destroy(request); | 179 mach_msg_destroy(request); |
176 mach_msg_destroy(reply); | 180 mach_msg_destroy(reply); |
177 } | 181 } |
178 } | 182 } |
179 | 183 |
180 } // namespace sandbox | 184 } // namespace sandbox |
OLD | NEW |