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