erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ipc/attachment_broker_privileged_win.h" |
| 6 | |
| 7 | #include <windows.h> |
| 8 | |
| 9 | #include "base/process/process.h" |
| 10 | #include "ipc/attachment_broker_messages.h" |
| 11 | #include "ipc/brokerable_attachment.h" |
| 12 | #include "ipc/handle_attachment_win.h" |
| 13 | #include "ipc/ipc_channel.h" |
| 14 | |
| 15 | namespace IPC { |
| 16 | |
| 17 | AttachmentBrokerPrivilegedWin::AttachmentBrokerPrivilegedWin() {} |
| 18 | |
| 19 | AttachmentBrokerPrivilegedWin::~AttachmentBrokerPrivilegedWin() {} |
| 20 | |
| 21 | bool AttachmentBrokerPrivilegedWin::SendAttachmentToProcess( |
| 22 | const BrokerableAttachment* attachment, |
| 23 | base::ProcessId destination_process) { |
| 24 | switch (attachment->GetBrokerableType()) { |
erikchen | 87351da | 2015-09-15 19:11:09 | [diff] [blame] | 25 | case BrokerableAttachment::WIN_HANDLE: { |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 26 | const internal::HandleAttachmentWin* handle_attachment = |
| 27 | static_cast<const internal::HandleAttachmentWin*>(attachment); |
| 28 | HandleWireFormat wire_format = |
| 29 | handle_attachment->GetWireFormat(destination_process); |
| 30 | HandleWireFormat new_wire_format = |
| 31 | DuplicateWinHandle(wire_format, base::Process::Current().Pid()); |
| 32 | if (new_wire_format.handle == 0) |
| 33 | return false; |
| 34 | RouteDuplicatedHandle(new_wire_format); |
| 35 | return true; |
erikchen | 87351da | 2015-09-15 19:11:09 | [diff] [blame] | 36 | } |
| 37 | case BrokerableAttachment::PLACEHOLDER: |
| 38 | NOTREACHED(); |
| 39 | return false; |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 40 | } |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | bool AttachmentBrokerPrivilegedWin::OnMessageReceived(const Message& msg) { |
| 45 | bool handled = true; |
erikchen | 3c175a3 | 2015-07-28 23:16:48 | [diff] [blame] | 46 | switch (msg.type()) { |
| 47 | IPC_MESSAGE_HANDLER_GENERIC(AttachmentBrokerMsg_DuplicateWinHandle, |
| 48 | OnDuplicateWinHandle(msg)) |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 49 | IPC_MESSAGE_UNHANDLED(handled = false) |
erikchen | 3c175a3 | 2015-07-28 23:16:48 | [diff] [blame] | 50 | } |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 51 | return handled; |
| 52 | } |
| 53 | |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 54 | void AttachmentBrokerPrivilegedWin::OnDuplicateWinHandle( |
erikchen | 3c175a3 | 2015-07-28 23:16:48 | [diff] [blame] | 55 | const IPC::Message& message) { |
| 56 | AttachmentBrokerMsg_DuplicateWinHandle::Param param; |
| 57 | if (!AttachmentBrokerMsg_DuplicateWinHandle::Read(&message, ¶m)) |
| 58 | return; |
| 59 | IPC::internal::HandleAttachmentWin::WireFormat wire_format = |
| 60 | base::get<0>(param); |
| 61 | |
erikchen | 16f9112d | 2015-09-23 06:05:20 | [diff] [blame] | 62 | if (wire_format.destination_process == base::kNullProcessId) { |
| 63 | LogError(NO_DESTINATION); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 64 | return; |
erikchen | 16f9112d | 2015-09-23 06:05:20 | [diff] [blame] | 65 | } |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 66 | |
| 67 | HandleWireFormat new_wire_format = |
erikchen | 3c175a3 | 2015-07-28 23:16:48 | [diff] [blame] | 68 | DuplicateWinHandle(wire_format, message.get_sender_pid()); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 69 | RouteDuplicatedHandle(new_wire_format); |
| 70 | } |
| 71 | |
| 72 | void AttachmentBrokerPrivilegedWin::RouteDuplicatedHandle( |
| 73 | const HandleWireFormat& wire_format) { |
| 74 | // This process is the destination. |
| 75 | if (wire_format.destination_process == base::Process::Current().Pid()) { |
| 76 | scoped_refptr<BrokerableAttachment> attachment( |
| 77 | new internal::HandleAttachmentWin(wire_format)); |
| 78 | HandleReceivedAttachment(attachment); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | // Another process is the destination. |
| 83 | base::ProcessId dest = wire_format.destination_process; |
erikchen | a09b9be7 | 2015-08-10 19:22:33 | [diff] [blame] | 84 | Sender* sender = GetSenderWithProcessId(dest); |
| 85 | if (!sender) { |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 86 | // Assuming that this message was not sent from a malicious process, the |
| 87 | // channel endpoint that would have received this message will block |
| 88 | // forever. |
| 89 | LOG(ERROR) << "Failed to deliver brokerable attachment to process with id: " |
| 90 | << dest; |
erikchen | 16f9112d | 2015-09-23 06:05:20 | [diff] [blame] | 91 | LogError(DESTINATION_NOT_FOUND); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 92 | return; |
| 93 | } |
| 94 | |
erikchen | 16f9112d | 2015-09-23 06:05:20 | [diff] [blame] | 95 | LogError(DESTINATION_FOUND); |
erikchen | a09b9be7 | 2015-08-10 19:22:33 | [diff] [blame] | 96 | sender->Send(new AttachmentBrokerMsg_WinHandleHasBeenDuplicated(wire_format)); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | AttachmentBrokerPrivilegedWin::HandleWireFormat |
| 100 | AttachmentBrokerPrivilegedWin::DuplicateWinHandle( |
| 101 | const HandleWireFormat& wire_format, |
| 102 | base::ProcessId source_pid) { |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 103 | base::Process source_process = |
| 104 | base::Process::OpenWithExtraPrivileges(source_pid); |
| 105 | base::Process dest_process = |
| 106 | base::Process::OpenWithExtraPrivileges(wire_format.destination_process); |
erikchen | 28299a1 | 2015-09-24 00:10:27 | [diff] [blame] | 107 | int new_wire_format_handle = 0; |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 108 | if (source_process.Handle() && dest_process.Handle()) { |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 109 | DWORD desired_access = 0; |
| 110 | DWORD options = 0; |
| 111 | switch (wire_format.permissions) { |
| 112 | case HandleWin::INVALID: |
| 113 | LOG(ERROR) << "Received invalid permissions for duplication."; |
erikchen | 28299a1 | 2015-09-24 00:10:27 | [diff] [blame] | 114 | return CopyWireFormat(wire_format, 0); |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 115 | case HandleWin::DUPLICATE: |
| 116 | options = DUPLICATE_SAME_ACCESS; |
| 117 | break; |
| 118 | case HandleWin::FILE_READ_WRITE: |
| 119 | desired_access = FILE_GENERIC_READ | FILE_GENERIC_WRITE; |
| 120 | break; |
| 121 | } |
| 122 | |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 123 | HANDLE new_handle; |
erikchen | 28299a1 | 2015-09-24 00:10:27 | [diff] [blame] | 124 | HANDLE original_handle = LongToHandle(wire_format.handle); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 125 | DWORD result = ::DuplicateHandle(source_process.Handle(), original_handle, |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 126 | dest_process.Handle(), &new_handle, |
| 127 | desired_access, FALSE, options); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 128 | |
erikchen | 28299a1 | 2015-09-24 00:10:27 | [diff] [blame] | 129 | new_wire_format_handle = (result != 0) ? HandleToLong(new_handle) : 0; |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 130 | } |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 131 | |
erikchen | 28299a1 | 2015-09-24 00:10:27 | [diff] [blame] | 132 | return CopyWireFormat(wire_format, new_wire_format_handle); |
| 133 | } |
| 134 | |
| 135 | AttachmentBrokerPrivilegedWin::HandleWireFormat |
| 136 | AttachmentBrokerPrivilegedWin::CopyWireFormat( |
| 137 | const HandleWireFormat& wire_format, |
| 138 | int handle) { |
| 139 | return HandleWireFormat(handle, wire_format.destination_process, |
| 140 | wire_format.permissions, wire_format.attachment_id); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | } // namespace IPC |