OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/ipc_channel_win.h" | 5 #include "ipc/ipc_channel_win.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 | 8 |
9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/pickle.h" | 13 #include "base/pickle.h" |
14 #include "base/process/process_handle.h" | 14 #include "base/process/process_handle.h" |
15 #include "base/rand_util.h" | 15 #include "base/rand_util.h" |
16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
18 #include "base/threading/thread_checker.h" | 18 #include "base/threading/thread_checker.h" |
19 #include "base/win/scoped_handle.h" | 19 #include "base/win/scoped_handle.h" |
| 20 #include "ipc/attachment_broker.h" |
20 #include "ipc/ipc_listener.h" | 21 #include "ipc/ipc_listener.h" |
21 #include "ipc/ipc_logging.h" | 22 #include "ipc/ipc_logging.h" |
22 #include "ipc/ipc_message_attachment_set.h" | 23 #include "ipc/ipc_message_attachment_set.h" |
23 #include "ipc/ipc_message_utils.h" | 24 #include "ipc/ipc_message_utils.h" |
24 | 25 |
25 namespace IPC { | 26 namespace IPC { |
26 | 27 |
27 ChannelWin::State::State(ChannelWin* channel) : is_pending(false) { | 28 ChannelWin::State::State(ChannelWin* channel) : is_pending(false) { |
28 memset(&context.overlapped, 0, sizeof(context.overlapped)); | 29 memset(&context.overlapped, 0, sizeof(context.overlapped)); |
29 context.handler = channel; | 30 context.handler = channel; |
30 } | 31 } |
31 | 32 |
32 ChannelWin::State::~State() { | 33 ChannelWin::State::~State() { |
33 static_assert(offsetof(ChannelWin::State, context) == 0, | 34 static_assert(offsetof(ChannelWin::State, context) == 0, |
34 "ChannelWin::State should have context as its first data" | 35 "ChannelWin::State should have context as its first data" |
35 "member."); | 36 "member."); |
36 } | 37 } |
37 | 38 |
38 ChannelWin::ChannelWin(const IPC::ChannelHandle& channel_handle, | 39 ChannelWin::ChannelWin(const IPC::ChannelHandle& channel_handle, |
39 Mode mode, | 40 Mode mode, |
40 Listener* listener, | 41 Listener* listener) |
41 AttachmentBroker* broker) | |
42 : ChannelReader(listener), | 42 : ChannelReader(listener), |
43 input_state_(this), | 43 input_state_(this), |
44 output_state_(this), | 44 output_state_(this), |
45 peer_pid_(base::kNullProcessId), | 45 peer_pid_(base::kNullProcessId), |
46 waiting_connect_(mode & MODE_SERVER_FLAG), | 46 waiting_connect_(mode & MODE_SERVER_FLAG), |
47 processing_incoming_(false), | 47 processing_incoming_(false), |
48 validate_client_(false), | 48 validate_client_(false), |
49 client_secret_(0), | 49 client_secret_(0), |
50 broker_(broker), | |
51 weak_factory_(this) { | 50 weak_factory_(this) { |
52 CreatePipe(channel_handle, mode); | 51 CreatePipe(channel_handle, mode); |
53 } | 52 } |
54 | 53 |
55 ChannelWin::~ChannelWin() { | 54 ChannelWin::~ChannelWin() { |
56 CleanUp(); | 55 CleanUp(); |
57 Close(); | 56 Close(); |
58 } | 57 } |
59 | 58 |
60 void ChannelWin::Close() { | 59 void ChannelWin::Close() { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 } | 98 } |
100 | 99 |
101 return ProcessMessageForDelivery(message); | 100 return ProcessMessageForDelivery(message); |
102 } | 101 } |
103 | 102 |
104 bool ChannelWin::ProcessMessageForDelivery(Message* message) { | 103 bool ChannelWin::ProcessMessageForDelivery(Message* message) { |
105 // Sending a brokerable attachment requires a call to Channel::Send(), so | 104 // Sending a brokerable attachment requires a call to Channel::Send(), so |
106 // both Send() and ProcessMessageForDelivery() may be re-entrant. Brokered | 105 // both Send() and ProcessMessageForDelivery() may be re-entrant. Brokered |
107 // attachments must be sent before the Message itself. | 106 // attachments must be sent before the Message itself. |
108 if (message->HasBrokerableAttachments()) { | 107 if (message->HasBrokerableAttachments()) { |
109 DCHECK(broker_); | 108 DCHECK(GetAttachmentBroker()); |
110 DCHECK(peer_pid_ != base::kNullProcessId); | 109 DCHECK(peer_pid_ != base::kNullProcessId); |
111 for (const BrokerableAttachment* attachment : | 110 for (const BrokerableAttachment* attachment : |
112 message->attachment_set()->PeekBrokerableAttachments()) { | 111 message->attachment_set()->PeekBrokerableAttachments()) { |
113 if (!broker_->SendAttachmentToProcess(attachment, peer_pid_)) { | 112 if (!GetAttachmentBroker()->SendAttachmentToProcess(attachment, |
| 113 peer_pid_)) { |
114 delete message; | 114 delete message; |
115 return false; | 115 return false; |
116 } | 116 } |
117 } | 117 } |
118 } | 118 } |
119 | 119 |
120 #ifdef IPC_MESSAGE_LOG_ENABLED | 120 #ifdef IPC_MESSAGE_LOG_ENABLED |
121 Logging::GetInstance()->OnSendMessage(message, ""); | 121 Logging::GetInstance()->OnSendMessage(message, ""); |
122 #endif | 122 #endif |
123 | 123 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 prelim_queue_.swap(prelim_queue); | 160 prelim_queue_.swap(prelim_queue); |
161 | 161 |
162 while (!prelim_queue.empty()) { | 162 while (!prelim_queue.empty()) { |
163 Message* m = prelim_queue.front(); | 163 Message* m = prelim_queue.front(); |
164 ProcessMessageForDelivery(m); | 164 ProcessMessageForDelivery(m); |
165 prelim_queue.pop(); | 165 prelim_queue.pop(); |
166 } | 166 } |
167 } | 167 } |
168 | 168 |
169 AttachmentBroker* ChannelWin::GetAttachmentBroker() { | 169 AttachmentBroker* ChannelWin::GetAttachmentBroker() { |
170 return broker_; | 170 return AttachmentBroker::GetGlobal(); |
171 } | 171 } |
172 | 172 |
173 base::ProcessId ChannelWin::GetPeerPID() const { | 173 base::ProcessId ChannelWin::GetPeerPID() const { |
174 return peer_pid_; | 174 return peer_pid_; |
175 } | 175 } |
176 | 176 |
177 base::ProcessId ChannelWin::GetSelfPID() const { | 177 base::ProcessId ChannelWin::GetSelfPID() const { |
178 return GetCurrentProcessId(); | 178 return GetCurrentProcessId(); |
179 } | 179 } |
180 | 180 |
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
564 | 564 |
565 //------------------------------------------------------------------------------ | 565 //------------------------------------------------------------------------------ |
566 // Channel's methods | 566 // Channel's methods |
567 | 567 |
568 // static | 568 // static |
569 scoped_ptr<Channel> Channel::Create(const IPC::ChannelHandle& channel_handle, | 569 scoped_ptr<Channel> Channel::Create(const IPC::ChannelHandle& channel_handle, |
570 Mode mode, | 570 Mode mode, |
571 Listener* listener, | 571 Listener* listener, |
572 AttachmentBroker* broker) { | 572 AttachmentBroker* broker) { |
573 return scoped_ptr<Channel>( | 573 return scoped_ptr<Channel>( |
574 new ChannelWin(channel_handle, mode, listener, broker)); | 574 new ChannelWin(channel_handle, mode, listener)); |
575 } | 575 } |
576 | 576 |
577 // static | 577 // static |
578 bool Channel::IsNamedServerInitialized(const std::string& channel_id) { | 578 bool Channel::IsNamedServerInitialized(const std::string& channel_id) { |
579 return ChannelWin::IsNamedServerInitialized(channel_id); | 579 return ChannelWin::IsNamedServerInitialized(channel_id); |
580 } | 580 } |
581 | 581 |
582 // static | 582 // static |
583 std::string Channel::GenerateVerifiedChannelID(const std::string& prefix) { | 583 std::string Channel::GenerateVerifiedChannelID(const std::string& prefix) { |
584 // Windows pipes can be enumerated by low-privileged processes. So, we | 584 // Windows pipes can be enumerated by low-privileged processes. So, we |
585 // append a strong random value after the \ character. This value is not | 585 // append a strong random value after the \ character. This value is not |
586 // included in the pipe name, but sent as part of the client hello, to | 586 // included in the pipe name, but sent as part of the client hello, to |
587 // hijacking the pipe name to spoof the client. | 587 // hijacking the pipe name to spoof the client. |
588 | 588 |
589 std::string id = prefix; | 589 std::string id = prefix; |
590 if (!id.empty()) | 590 if (!id.empty()) |
591 id.append("."); | 591 id.append("."); |
592 | 592 |
593 int secret; | 593 int secret; |
594 do { // Guarantee we get a non-zero value. | 594 do { // Guarantee we get a non-zero value. |
595 secret = base::RandInt(0, std::numeric_limits<int>::max()); | 595 secret = base::RandInt(0, std::numeric_limits<int>::max()); |
596 } while (secret == 0); | 596 } while (secret == 0); |
597 | 597 |
598 id.append(GenerateUniqueRandomChannelID()); | 598 id.append(GenerateUniqueRandomChannelID()); |
599 return id.append(base::StringPrintf("\\%d", secret)); | 599 return id.append(base::StringPrintf("\\%d", secret)); |
600 } | 600 } |
601 | 601 |
602 } // namespace IPC | 602 } // namespace IPC |
OLD | NEW |