| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "ipc/attachment_broker_privileged_mac.h" | 5 #include "ipc/attachment_broker_privileged_mac.h" |
| 6 | 6 |
| 7 #include "base/mac/scoped_mach_port.h" | 7 #include "base/mac/scoped_mach_port.h" |
| 8 #include "base/memory/shared_memory.h" | 8 #include "base/memory/shared_memory.h" |
| 9 #include "base/process/port_provider_mac.h" |
| 9 #include "base/process/process.h" | 10 #include "base/process/process.h" |
| 10 #include "ipc/attachment_broker_messages.h" | 11 #include "ipc/attachment_broker_messages.h" |
| 11 #include "ipc/brokerable_attachment.h" | 12 #include "ipc/brokerable_attachment.h" |
| 12 #include "ipc/ipc_channel.h" | 13 #include "ipc/ipc_channel.h" |
| 13 #include "ipc/mach_port_attachment_mac.h" | 14 #include "ipc/mach_port_attachment_mac.h" |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 // Struct for sending a complex Mach message. | 18 // Struct for sending a complex Mach message. |
| 18 struct MachSendComplexMessage { | 19 struct MachSendComplexMessage { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 45 0, // receive limit | 46 0, // receive limit |
| 46 MACH_PORT_NULL, // receive name | 47 MACH_PORT_NULL, // receive name |
| 47 0, // timeout | 48 0, // timeout |
| 48 MACH_PORT_NULL); // notification port | 49 MACH_PORT_NULL); // notification port |
| 49 } | 50 } |
| 50 | 51 |
| 51 } // namespace | 52 } // namespace |
| 52 | 53 |
| 53 namespace IPC { | 54 namespace IPC { |
| 54 | 55 |
| 55 AttachmentBrokerPrivilegedMac::AttachmentBrokerPrivilegedMac() | 56 AttachmentBrokerPrivilegedMac::AttachmentBrokerPrivilegedMac() {} |
| 56 : port_provider_(nullptr) {} | |
| 57 | |
| 58 AttachmentBrokerPrivilegedMac::~AttachmentBrokerPrivilegedMac() {} | 57 AttachmentBrokerPrivilegedMac::~AttachmentBrokerPrivilegedMac() {} |
| 59 | 58 |
| 60 void AttachmentBrokerPrivilegedMac::SetPortProvider( | |
| 61 base::PortProvider* port_provider) { | |
| 62 CHECK(!port_provider_); | |
| 63 port_provider_ = port_provider; | |
| 64 } | |
| 65 | |
| 66 bool AttachmentBrokerPrivilegedMac::SendAttachmentToProcess( | 59 bool AttachmentBrokerPrivilegedMac::SendAttachmentToProcess( |
| 67 BrokerableAttachment* attachment, | 60 BrokerableAttachment* attachment, |
| 68 base::ProcessId destination_process) { | 61 base::ProcessId destination_process) { |
| 69 switch (attachment->GetBrokerableType()) { | 62 switch (attachment->GetBrokerableType()) { |
| 70 case BrokerableAttachment::MACH_PORT: { | 63 case BrokerableAttachment::MACH_PORT: { |
| 71 internal::MachPortAttachmentMac* mach_port_attachment = | 64 internal::MachPortAttachmentMac* mach_port_attachment = |
| 72 static_cast<internal::MachPortAttachmentMac*>(attachment); | 65 static_cast<internal::MachPortAttachmentMac*>(attachment); |
| 73 MachPortWireFormat wire_format = | 66 MachPortWireFormat wire_format = |
| 74 mach_port_attachment->GetWireFormat(destination_process); | 67 mach_port_attachment->GetWireFormat(destination_process); |
| 75 | 68 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 } | 166 } |
| 174 | 167 |
| 175 LogError(DESTINATION_FOUND); | 168 LogError(DESTINATION_FOUND); |
| 176 sender->Send(new AttachmentBrokerMsg_MachPortHasBeenDuplicated(wire_format)); | 169 sender->Send(new AttachmentBrokerMsg_MachPortHasBeenDuplicated(wire_format)); |
| 177 } | 170 } |
| 178 | 171 |
| 179 mach_port_name_t AttachmentBrokerPrivilegedMac::CreateIntermediateMachPort( | 172 mach_port_name_t AttachmentBrokerPrivilegedMac::CreateIntermediateMachPort( |
| 180 base::ProcessId pid, | 173 base::ProcessId pid, |
| 181 base::mac::ScopedMachSendRight port_to_insert) { | 174 base::mac::ScopedMachSendRight port_to_insert) { |
| 182 DCHECK_NE(pid, base::GetCurrentProcId()); | 175 DCHECK_NE(pid, base::GetCurrentProcId()); |
| 183 mach_port_t task_port = port_provider_->TaskForPid(pid); | 176 mach_port_t task_port = get_port_provider()->TaskForPid(pid); |
| 184 if (task_port == MACH_PORT_NULL) { | 177 if (task_port == MACH_PORT_NULL) { |
| 185 // TODO(erikchen): UMA metric. | 178 // TODO(erikchen): UMA metric. |
| 186 return MACH_PORT_NULL; | 179 return MACH_PORT_NULL; |
| 187 } | 180 } |
| 188 return CreateIntermediateMachPort( | 181 return CreateIntermediateMachPort( |
| 189 task_port, base::mac::ScopedMachSendRight(port_to_insert.release())); | 182 task_port, base::mac::ScopedMachSendRight(port_to_insert.release())); |
| 190 } | 183 } |
| 191 | 184 |
| 192 mach_port_name_t AttachmentBrokerPrivilegedMac::CreateIntermediateMachPort( | 185 mach_port_name_t AttachmentBrokerPrivilegedMac::CreateIntermediateMachPort( |
| 193 mach_port_t task_port, | 186 mach_port_t task_port, |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 base::ProcessId pid, | 240 base::ProcessId pid, |
| 248 mach_port_name_t named_right) { | 241 mach_port_name_t named_right) { |
| 249 if (pid == base::GetCurrentProcId()) { | 242 if (pid == base::GetCurrentProcId()) { |
| 250 kern_return_t kr = mach_port_mod_refs(mach_task_self(), named_right, | 243 kern_return_t kr = mach_port_mod_refs(mach_task_self(), named_right, |
| 251 MACH_PORT_RIGHT_SEND, 1); | 244 MACH_PORT_RIGHT_SEND, 1); |
| 252 if (kr != KERN_SUCCESS) | 245 if (kr != KERN_SUCCESS) |
| 253 return base::mac::ScopedMachSendRight(MACH_PORT_NULL); | 246 return base::mac::ScopedMachSendRight(MACH_PORT_NULL); |
| 254 return base::mac::ScopedMachSendRight(named_right); | 247 return base::mac::ScopedMachSendRight(named_right); |
| 255 } | 248 } |
| 256 | 249 |
| 257 mach_port_t task_port = port_provider_->TaskForPid(pid); | 250 mach_port_t task_port = get_port_provider()->TaskForPid(pid); |
| 258 return ExtractNamedRight(task_port, named_right); | 251 return ExtractNamedRight(task_port, named_right); |
| 259 } | 252 } |
| 260 | 253 |
| 261 base::mac::ScopedMachSendRight AttachmentBrokerPrivilegedMac::ExtractNamedRight( | 254 base::mac::ScopedMachSendRight AttachmentBrokerPrivilegedMac::ExtractNamedRight( |
| 262 mach_port_t task_port, | 255 mach_port_t task_port, |
| 263 mach_port_name_t named_right) { | 256 mach_port_name_t named_right) { |
| 264 mach_port_t extracted_right = MACH_PORT_NULL; | 257 mach_port_t extracted_right = MACH_PORT_NULL; |
| 265 mach_msg_type_name_t extracted_right_type; | 258 mach_msg_type_name_t extracted_right_type; |
| 266 kern_return_t kr = | 259 kern_return_t kr = |
| 267 mach_port_extract_right(task_port, named_right, MACH_MSG_TYPE_COPY_SEND, | 260 mach_port_extract_right(task_port, named_right, MACH_MSG_TYPE_COPY_SEND, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 285 | 278 |
| 286 AttachmentBrokerPrivilegedMac::MachPortWireFormat | 279 AttachmentBrokerPrivilegedMac::MachPortWireFormat |
| 287 AttachmentBrokerPrivilegedMac::CopyWireFormat( | 280 AttachmentBrokerPrivilegedMac::CopyWireFormat( |
| 288 const MachPortWireFormat& wire_format, | 281 const MachPortWireFormat& wire_format, |
| 289 uint32_t mach_port) { | 282 uint32_t mach_port) { |
| 290 return MachPortWireFormat(mach_port, wire_format.destination_process, | 283 return MachPortWireFormat(mach_port, wire_format.destination_process, |
| 291 wire_format.attachment_id); | 284 wire_format.attachment_id); |
| 292 } | 285 } |
| 293 | 286 |
| 294 } // namespace IPC | 287 } // namespace IPC |
| OLD | NEW |