OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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. |
| 4 |
| 5 #include "base/mac/mach_port_util.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 |
| 9 namespace base { |
| 10 |
| 11 namespace { |
| 12 |
| 13 // Struct for sending a complex Mach message. |
| 14 struct MachSendComplexMessage { |
| 15 mach_msg_header_t header; |
| 16 mach_msg_body_t body; |
| 17 mach_msg_port_descriptor_t data; |
| 18 }; |
| 19 |
| 20 // Struct for receiving a complex message. |
| 21 struct MachReceiveComplexMessage { |
| 22 mach_msg_header_t header; |
| 23 mach_msg_body_t body; |
| 24 mach_msg_port_descriptor_t data; |
| 25 mach_msg_trailer_t trailer; |
| 26 }; |
| 27 |
| 28 // These reflect the values in IPC::AttachmentBrokerPrivileged::UMAError. |
| 29 // Do NOT change them. |
| 30 const uint32_t kErrorMakeReceivePort = 6; |
| 31 const uint32_t kErrorSetAttributes = 7; |
| 32 const uint32_t kErrorExtractDestRight = 8; |
| 33 const uint32_t kErrorSendMachPort = 9; |
| 34 |
| 35 } // namespace |
| 36 |
| 37 kern_return_t SendMachPort(mach_port_t endpoint, |
| 38 mach_port_t port_to_send, |
| 39 int disposition) { |
| 40 MachSendComplexMessage send_msg; |
| 41 send_msg.header.msgh_bits = |
| 42 MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE, 0) | MACH_MSGH_BITS_COMPLEX; |
| 43 send_msg.header.msgh_size = sizeof(send_msg); |
| 44 send_msg.header.msgh_remote_port = endpoint; |
| 45 send_msg.header.msgh_local_port = MACH_PORT_NULL; |
| 46 send_msg.header.msgh_reserved = 0; |
| 47 send_msg.header.msgh_id = 0; |
| 48 send_msg.body.msgh_descriptor_count = 1; |
| 49 send_msg.data.name = port_to_send; |
| 50 send_msg.data.disposition = disposition; |
| 51 send_msg.data.type = MACH_MSG_PORT_DESCRIPTOR; |
| 52 |
| 53 kern_return_t kr = |
| 54 mach_msg(&send_msg.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT, |
| 55 send_msg.header.msgh_size, |
| 56 0, // receive limit |
| 57 MACH_PORT_NULL, // receive name |
| 58 0, // timeout |
| 59 MACH_PORT_NULL); // notification port |
| 60 |
| 61 if (kr != KERN_SUCCESS) |
| 62 mach_port_deallocate(mach_task_self(), endpoint); |
| 63 |
| 64 return kr; |
| 65 } |
| 66 |
| 67 base::mac::ScopedMachSendRight ReceiveMachPort(mach_port_t port_to_listen_on) { |
| 68 MachReceiveComplexMessage recv_msg; |
| 69 mach_msg_header_t* recv_hdr = &recv_msg.header; |
| 70 recv_hdr->msgh_local_port = port_to_listen_on; |
| 71 recv_hdr->msgh_size = sizeof(recv_msg); |
| 72 |
| 73 kern_return_t kr = |
| 74 mach_msg(recv_hdr, MACH_RCV_MSG | MACH_RCV_TIMEOUT, 0, |
| 75 recv_hdr->msgh_size, port_to_listen_on, 0, MACH_PORT_NULL); |
| 76 if (kr != KERN_SUCCESS) |
| 77 return base::mac::ScopedMachSendRight(MACH_PORT_NULL); |
| 78 if (recv_msg.header.msgh_id != 0) |
| 79 return base::mac::ScopedMachSendRight(MACH_PORT_NULL); |
| 80 return base::mac::ScopedMachSendRight(recv_msg.data.name); |
| 81 } |
| 82 |
| 83 mach_port_name_t CreateIntermediateMachPort( |
| 84 mach_port_t task_port, |
| 85 base::mac::ScopedMachSendRight port_to_insert, |
| 86 uint32_t* error_code) { |
| 87 DCHECK_NE(mach_task_self(), task_port); |
| 88 DCHECK_NE(static_cast<mach_port_name_t>(MACH_PORT_NULL), task_port); |
| 89 |
| 90 // Make a port with receive rights in the destination task. |
| 91 mach_port_name_t endpoint; |
| 92 kern_return_t kr = |
| 93 mach_port_allocate(task_port, MACH_PORT_RIGHT_RECEIVE, &endpoint); |
| 94 if (kr != KERN_SUCCESS) { |
| 95 if (error_code) |
| 96 *error_code = kErrorMakeReceivePort; |
| 97 return MACH_PORT_NULL; |
| 98 } |
| 99 |
| 100 // Change its message queue limit so that it accepts one message. |
| 101 mach_port_limits limits = {}; |
| 102 limits.mpl_qlimit = 1; |
| 103 kr = mach_port_set_attributes(task_port, endpoint, MACH_PORT_LIMITS_INFO, |
| 104 reinterpret_cast<mach_port_info_t>(&limits), |
| 105 MACH_PORT_LIMITS_INFO_COUNT); |
| 106 if (kr != KERN_SUCCESS) { |
| 107 if (error_code) |
| 108 *error_code = kErrorSetAttributes; |
| 109 mach_port_deallocate(task_port, endpoint); |
| 110 return MACH_PORT_NULL; |
| 111 } |
| 112 |
| 113 // Get a send right. |
| 114 mach_port_t send_once_right; |
| 115 mach_msg_type_name_t send_right_type; |
| 116 kr = |
| 117 mach_port_extract_right(task_port, endpoint, MACH_MSG_TYPE_MAKE_SEND_ONCE, |
| 118 &send_once_right, &send_right_type); |
| 119 if (kr != KERN_SUCCESS) { |
| 120 if (error_code) |
| 121 *error_code = kErrorExtractDestRight; |
| 122 mach_port_deallocate(task_port, endpoint); |
| 123 return MACH_PORT_NULL; |
| 124 } |
| 125 DCHECK_EQ(static_cast<mach_msg_type_name_t>(MACH_MSG_TYPE_PORT_SEND_ONCE), |
| 126 send_right_type); |
| 127 |
| 128 // This call takes ownership of |send_once_right|. |
| 129 kr = base::SendMachPort( |
| 130 send_once_right, port_to_insert.get(), MACH_MSG_TYPE_COPY_SEND); |
| 131 if (kr != KERN_SUCCESS) { |
| 132 if (error_code) |
| 133 *error_code = kErrorSendMachPort; |
| 134 mach_port_deallocate(task_port, endpoint); |
| 135 return MACH_PORT_NULL; |
| 136 } |
| 137 |
| 138 // Endpoint is intentionally leaked into the destination task. An IPC must be |
| 139 // sent to the destination task so that it can clean up this port. |
| 140 return endpoint; |
| 141 } |
| 142 |
| 143 } // namespace base |
OLD | NEW |