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_win.h" | 5 #include "ipc/attachment_broker_privileged_win.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 | 8 |
9 #include "base/process/process.h" | 9 #include "base/process/process.h" |
10 #include "ipc/attachment_broker_messages.h" | 10 #include "ipc/attachment_broker_messages.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 bool AttachmentBrokerPrivilegedWin::OnMessageReceived(const Message& msg) { | 40 bool AttachmentBrokerPrivilegedWin::OnMessageReceived(const Message& msg) { |
41 bool handled = true; | 41 bool handled = true; |
42 switch (msg.type()) { | 42 switch (msg.type()) { |
43 IPC_MESSAGE_HANDLER_GENERIC(AttachmentBrokerMsg_DuplicateWinHandle, | 43 IPC_MESSAGE_HANDLER_GENERIC(AttachmentBrokerMsg_DuplicateWinHandle, |
44 OnDuplicateWinHandle(msg)) | 44 OnDuplicateWinHandle(msg)) |
45 IPC_MESSAGE_UNHANDLED(handled = false) | 45 IPC_MESSAGE_UNHANDLED(handled = false) |
46 } | 46 } |
47 return handled; | 47 return handled; |
48 } | 48 } |
49 | 49 |
50 void AttachmentBrokerPrivilegedWin::RegisterCommunicationChannel( | |
51 Channel* channel) { | |
52 auto it = std::find(channels_.begin(), channels_.end(), channel); | |
53 DCHECK(channels_.end() == it); | |
54 channels_.push_back(channel); | |
55 } | |
56 | |
57 void AttachmentBrokerPrivilegedWin::DeregisterCommunicationChannel( | |
58 Channel* channel) { | |
59 auto it = std::find(channels_.begin(), channels_.end(), channel); | |
60 DCHECK(it != channels_.end()); | |
61 channels_.erase(it); | |
62 } | |
63 | |
64 void AttachmentBrokerPrivilegedWin::OnDuplicateWinHandle( | 50 void AttachmentBrokerPrivilegedWin::OnDuplicateWinHandle( |
65 const IPC::Message& message) { | 51 const IPC::Message& message) { |
66 AttachmentBrokerMsg_DuplicateWinHandle::Param param; | 52 AttachmentBrokerMsg_DuplicateWinHandle::Param param; |
67 if (!AttachmentBrokerMsg_DuplicateWinHandle::Read(&message, ¶m)) | 53 if (!AttachmentBrokerMsg_DuplicateWinHandle::Read(&message, ¶m)) |
68 return; | 54 return; |
69 IPC::internal::HandleAttachmentWin::WireFormat wire_format = | 55 IPC::internal::HandleAttachmentWin::WireFormat wire_format = |
70 base::get<0>(param); | 56 base::get<0>(param); |
71 | 57 |
72 if (wire_format.destination_process == base::kNullProcessId) | 58 if (wire_format.destination_process == base::kNullProcessId) |
73 return; | 59 return; |
74 | 60 |
75 HandleWireFormat new_wire_format = | 61 HandleWireFormat new_wire_format = |
76 DuplicateWinHandle(wire_format, message.get_sender_pid()); | 62 DuplicateWinHandle(wire_format, message.get_sender_pid()); |
77 RouteDuplicatedHandle(new_wire_format); | 63 RouteDuplicatedHandle(new_wire_format); |
78 } | 64 } |
79 | 65 |
80 void AttachmentBrokerPrivilegedWin::RouteDuplicatedHandle( | 66 void AttachmentBrokerPrivilegedWin::RouteDuplicatedHandle( |
81 const HandleWireFormat& wire_format) { | 67 const HandleWireFormat& wire_format) { |
82 // This process is the destination. | 68 // This process is the destination. |
83 if (wire_format.destination_process == base::Process::Current().Pid()) { | 69 if (wire_format.destination_process == base::Process::Current().Pid()) { |
84 scoped_refptr<BrokerableAttachment> attachment( | 70 scoped_refptr<BrokerableAttachment> attachment( |
85 new internal::HandleAttachmentWin(wire_format)); | 71 new internal::HandleAttachmentWin(wire_format)); |
86 HandleReceivedAttachment(attachment); | 72 HandleReceivedAttachment(attachment); |
87 return; | 73 return; |
88 } | 74 } |
89 | 75 |
90 // Another process is the destination. | 76 // Another process is the destination. |
91 base::ProcessId dest = wire_format.destination_process; | 77 base::ProcessId dest = wire_format.destination_process; |
92 auto it = | 78 Channel* channel = GetChannelWithProcessId(dest); |
93 std::find_if(channels_.begin(), channels_.end(), | 79 if (!channel) { |
94 [dest](Channel* c) { return c->GetPeerPID() == dest; }); | |
95 if (it == channels_.end()) { | |
96 // Assuming that this message was not sent from a malicious process, the | 80 // Assuming that this message was not sent from a malicious process, the |
97 // channel endpoint that would have received this message will block | 81 // channel endpoint that would have received this message will block |
98 // forever. | 82 // forever. |
99 LOG(ERROR) << "Failed to deliver brokerable attachment to process with id: " | 83 LOG(ERROR) << "Failed to deliver brokerable attachment to process with id: " |
100 << dest; | 84 << dest; |
101 return; | 85 return; |
102 } | 86 } |
103 | 87 |
104 (*it)->Send(new AttachmentBrokerMsg_WinHandleHasBeenDuplicated(wire_format)); | 88 channel->Send( |
| 89 new AttachmentBrokerMsg_WinHandleHasBeenDuplicated(wire_format)); |
105 } | 90 } |
106 | 91 |
107 AttachmentBrokerPrivilegedWin::HandleWireFormat | 92 AttachmentBrokerPrivilegedWin::HandleWireFormat |
108 AttachmentBrokerPrivilegedWin::DuplicateWinHandle( | 93 AttachmentBrokerPrivilegedWin::DuplicateWinHandle( |
109 const HandleWireFormat& wire_format, | 94 const HandleWireFormat& wire_format, |
110 base::ProcessId source_pid) { | 95 base::ProcessId source_pid) { |
111 HandleWireFormat new_wire_format; | 96 HandleWireFormat new_wire_format; |
112 new_wire_format.destination_process = wire_format.destination_process; | 97 new_wire_format.destination_process = wire_format.destination_process; |
113 new_wire_format.attachment_id = wire_format.attachment_id; | 98 new_wire_format.attachment_id = wire_format.attachment_id; |
114 | 99 |
(...skipping 10 matching lines...) Expand all Loading... |
125 FALSE, DUPLICATE_SAME_ACCESS); | 110 FALSE, DUPLICATE_SAME_ACCESS); |
126 | 111 |
127 new_wire_format.handle = (result != 0) ? HandleToLong(new_handle) : 0; | 112 new_wire_format.handle = (result != 0) ? HandleToLong(new_handle) : 0; |
128 } else { | 113 } else { |
129 new_wire_format.handle = 0; | 114 new_wire_format.handle = 0; |
130 } | 115 } |
131 return new_wire_format; | 116 return new_wire_format; |
132 } | 117 } |
133 | 118 |
134 } // namespace IPC | 119 } // namespace IPC |
OLD | NEW |