[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 5 | #include "ipc/ipc_channel_win.h" |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 6 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | #include <windows.h> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | |
[email protected] | 5be7da24 | 2009-11-20 23:16:26 | [diff] [blame] | 9 | #include "base/auto_reset.h" |
[email protected] | 72b6f8e2 | 2011-11-12 21:16:41 | [diff] [blame] | 10 | #include "base/bind.h" |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 12 | #include "base/logging.h" |
[email protected] | 7edae3d0 | 2012-12-17 20:23:47 | [diff] [blame] | 13 | #include "base/pickle.h" |
[email protected] | e66ef60 | 2013-07-24 05:15:24 | [diff] [blame] | 14 | #include "base/process/process_handle.h" |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 15 | #include "base/rand_util.h" |
[email protected] | 4aa794a1 | 2013-06-11 06:32:18 | [diff] [blame] | 16 | #include "base/strings/string_number_conversions.h" |
[email protected] | 90626587 | 2013-06-07 22:40:45 | [diff] [blame] | 17 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 327e52b | 2012-06-25 21:11:36 | [diff] [blame] | 18 | #include "base/threading/thread_checker.h" |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 19 | #include "base/win/scoped_handle.h" |
[email protected] | 0ee99068 | 2012-11-17 19:20:05 | [diff] [blame] | 20 | #include "ipc/ipc_listener.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 21 | #include "ipc/ipc_logging.h" |
| 22 | #include "ipc/ipc_message_utils.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 23 | |
[email protected] | 2d0faf1 | 2014-06-27 04:50:43 | [diff] [blame] | 24 | namespace { |
| 25 | |
| 26 | enum DebugFlags { |
| 27 | INIT_DONE = 1 << 0, |
| 28 | CALLED_CONNECT = 1 << 1, |
| 29 | PENDING_CONNECT = 1 << 2, |
| 30 | CONNECT_COMPLETED = 1 << 3, |
| 31 | PIPE_CONNECTED = 1 << 4, |
| 32 | WRITE_MSG = 1 << 5, |
| 33 | READ_MSG = 1 << 6, |
| 34 | WRITE_COMPLETED = 1 << 7, |
| 35 | READ_COMPLETED = 1 << 8, |
| 36 | CLOSED = 1 << 9, |
| 37 | WAIT_FOR_READ = 1 << 10, |
| 38 | WAIT_FOR_WRITE = 1 << 11, |
| 39 | WAIT_FOR_READ_COMPLETE = 1 << 12, |
| 40 | WAIT_FOR_WRITE_COMPLETE = 1 << 13 |
| 41 | }; |
| 42 | |
| 43 | } // namespace |
| 44 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | namespace IPC { |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 46 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 47 | ChannelWin::State::State(ChannelWin* channel) : is_pending(false) { |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 48 | memset(&context.overlapped, 0, sizeof(context.overlapped)); |
| 49 | context.handler = channel; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | } |
| 51 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 52 | ChannelWin::State::~State() { |
| 53 | COMPILE_ASSERT(!offsetof(ChannelWin::State, context), |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 54 | starts_with_io_context); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 55 | } |
| 56 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 57 | ChannelWin::ChannelWin(const IPC::ChannelHandle &channel_handle, |
[email protected] | 2d0faf1 | 2014-06-27 04:50:43 | [diff] [blame] | 58 | Mode mode, Listener* listener) |
[email protected] | d805c6a | 2012-03-08 12:30:28 | [diff] [blame] | 59 | : ChannelReader(listener), |
[email protected] | ba2194d | 2013-04-26 19:51:30 | [diff] [blame] | 60 | input_state_(this), |
| 61 | output_state_(this), |
[email protected] | 0a6fc4b | 2012-04-05 02:38:34 | [diff] [blame] | 62 | peer_pid_(base::kNullProcessId), |
[email protected] | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 63 | waiting_connect_(mode & MODE_SERVER_FLAG), |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 64 | processing_incoming_(false), |
[email protected] | 2d0faf1 | 2014-06-27 04:50:43 | [diff] [blame] | 65 | validate_client_(false), |
[email protected] | 65727eb | 2014-07-31 12:24:29 | [diff] [blame] | 66 | writing_(false), |
[email protected] | 2d0faf1 | 2014-06-27 04:50:43 | [diff] [blame] | 67 | debug_flags_(0), |
rvargas | 431ce73 | 2014-09-16 02:44:15 | [diff] [blame] | 68 | write_error_(0), |
| 69 | last_write_error_(0), |
| 70 | write_size_(0), |
[email protected] | 65727eb | 2014-07-31 12:24:29 | [diff] [blame] | 71 | client_secret_(0), |
| 72 | weak_factory_(this) { |
[email protected] | c2391b8 | 2011-05-06 17:39:07 | [diff] [blame] | 73 | CreatePipe(channel_handle, mode); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 74 | } |
| 75 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 76 | ChannelWin::~ChannelWin() { |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 77 | Close(); |
| 78 | } |
| 79 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 80 | void ChannelWin::Close() { |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 81 | if (thread_check_.get()) { |
| 82 | DCHECK(thread_check_->CalledOnValidThread()); |
| 83 | } |
[email protected] | 2d0faf1 | 2014-06-27 04:50:43 | [diff] [blame] | 84 | debug_flags_ |= CLOSED; |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 85 | |
[email protected] | 74f87acf2 | 2009-10-14 22:10:40 | [diff] [blame] | 86 | if (input_state_.is_pending || output_state_.is_pending) |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 87 | CancelIo(pipe_.Get()); |
[email protected] | ee78622d | 2008-10-13 21:25:50 | [diff] [blame] | 88 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 89 | // Closing the handle at this point prevents us from issuing more requests |
| 90 | // form OnIOCompleted(). |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 91 | if (pipe_.IsValid()) |
| 92 | pipe_.Close(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 93 | |
[email protected] | 2d0faf1 | 2014-06-27 04:50:43 | [diff] [blame] | 94 | if (input_state_.is_pending) |
| 95 | debug_flags_ |= WAIT_FOR_READ; |
| 96 | |
| 97 | if (output_state_.is_pending) |
| 98 | debug_flags_ |= WAIT_FOR_WRITE; |
| 99 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 100 | // Make sure all IO has completed. |
| 101 | base::Time start = base::Time::Now(); |
| 102 | while (input_state_.is_pending || output_state_.is_pending) { |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 103 | base::MessageLoopForIO::current()->WaitForIOCompletion(INFINITE, this); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 104 | } |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 105 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 106 | while (!output_queue_.empty()) { |
| 107 | Message* m = output_queue_.front(); |
| 108 | output_queue_.pop(); |
| 109 | delete m; |
| 110 | } |
| 111 | } |
| 112 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 113 | bool ChannelWin::Send(Message* message) { |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 114 | DCHECK(thread_check_->CalledOnValidThread()); |
[email protected] | 2a9d601b | 2010-10-19 23:50:00 | [diff] [blame] | 115 | DVLOG(2) << "sending message @" << message << " on channel @" << this |
| 116 | << " with type " << message->type() |
| 117 | << " (" << output_queue_.size() << " in queue)"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 118 | |
| 119 | #ifdef IPC_MESSAGE_LOG_ENABLED |
[email protected] | 8e8bb6d | 2010-12-13 08:18:55 | [diff] [blame] | 120 | Logging::GetInstance()->OnSendMessage(message, ""); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 121 | #endif |
| 122 | |
[email protected] | 2c391df | 2012-09-18 03:41:29 | [diff] [blame] | 123 | message->TraceMessageBegin(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | output_queue_.push(message); |
| 125 | // ensure waiting to write |
| 126 | if (!waiting_connect_) { |
| 127 | if (!output_state_.is_pending) { |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 128 | if (!ProcessOutgoingMessages(NULL, 0)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 129 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | return true; |
| 134 | } |
| 135 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 136 | base::ProcessId ChannelWin::GetPeerPID() const { |
| 137 | return peer_pid_; |
| 138 | } |
| 139 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 140 | base::ProcessId ChannelWin::GetSelfPID() const { |
| 141 | return GetCurrentProcessId(); |
| 142 | } |
| 143 | |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 144 | // static |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 145 | bool ChannelWin::IsNamedServerInitialized( |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 146 | const std::string& channel_id) { |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 147 | if (WaitNamedPipe(PipeName(channel_id, NULL).c_str(), 1)) |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 148 | return true; |
| 149 | // If ERROR_SEM_TIMEOUT occurred, the pipe exists but is handling another |
| 150 | // connection. |
| 151 | return GetLastError() == ERROR_SEM_TIMEOUT; |
| 152 | } |
| 153 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 154 | ChannelWin::ReadState ChannelWin::ReadData( |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 155 | char* buffer, |
| 156 | int buffer_len, |
| 157 | int* /* bytes_read */) { |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 158 | if (!pipe_.IsValid()) |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 159 | return READ_FAILED; |
| 160 | |
[email protected] | 2d0faf1 | 2014-06-27 04:50:43 | [diff] [blame] | 161 | debug_flags_ |= READ_MSG; |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 162 | DWORD bytes_read = 0; |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 163 | BOOL ok = ReadFile(pipe_.Get(), buffer, buffer_len, |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 164 | &bytes_read, &input_state_.context.overlapped); |
| 165 | if (!ok) { |
| 166 | DWORD err = GetLastError(); |
| 167 | if (err == ERROR_IO_PENDING) { |
| 168 | input_state_.is_pending = true; |
| 169 | return READ_PENDING; |
| 170 | } |
| 171 | LOG(ERROR) << "pipe error: " << err; |
| 172 | return READ_FAILED; |
| 173 | } |
| 174 | |
| 175 | // We could return READ_SUCCEEDED here. But the way that this code is |
| 176 | // structured we instead go back to the message loop. Our completion port |
| 177 | // will be signalled even in the "synchronously completed" state. |
| 178 | // |
| 179 | // This allows us to potentially process some outgoing messages and |
| 180 | // interleave other work on this thread when we're getting hammered with |
| 181 | // input messages. Potentially, this could be tuned to be more efficient |
| 182 | // with some testing. |
| 183 | input_state_.is_pending = true; |
| 184 | return READ_PENDING; |
| 185 | } |
| 186 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 187 | bool ChannelWin::WillDispatchInputMessage(Message* msg) { |
[email protected] | 0590878 | 2012-04-03 08:49:43 | [diff] [blame] | 188 | // Make sure we get a hello when client validation is required. |
| 189 | if (validate_client_) |
| 190 | return IsHelloMessage(*msg); |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 191 | return true; |
| 192 | } |
| 193 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 194 | void ChannelWin::HandleInternalMessage(const Message& msg) { |
[email protected] | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 195 | DCHECK_EQ(msg.type(), static_cast<unsigned>(Channel::HELLO_MESSAGE_TYPE)); |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 196 | // The hello message contains one parameter containing the PID. |
[email protected] | 7edae3d0 | 2012-12-17 20:23:47 | [diff] [blame] | 197 | PickleIterator it(msg); |
| 198 | int32 claimed_pid; |
| 199 | bool failed = !it.ReadInt(&claimed_pid); |
| 200 | |
| 201 | if (!failed && validate_client_) { |
| 202 | int32 secret; |
| 203 | failed = it.ReadInt(&secret) ? (secret != client_secret_) : true; |
| 204 | } |
| 205 | |
| 206 | if (failed) { |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 207 | NOTREACHED(); |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 208 | Close(); |
| 209 | listener()->OnChannelError(); |
| 210 | return; |
| 211 | } |
[email protected] | 7edae3d0 | 2012-12-17 20:23:47 | [diff] [blame] | 212 | |
[email protected] | 0a6fc4b | 2012-04-05 02:38:34 | [diff] [blame] | 213 | peer_pid_ = claimed_pid; |
[email protected] | 7edae3d0 | 2012-12-17 20:23:47 | [diff] [blame] | 214 | // Validation completed. |
[email protected] | 0590878 | 2012-04-03 08:49:43 | [diff] [blame] | 215 | validate_client_ = false; |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 216 | listener()->OnChannelConnected(claimed_pid); |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 217 | } |
| 218 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 219 | bool ChannelWin::DidEmptyInputBuffers() { |
[email protected] | d805c6a | 2012-03-08 12:30:28 | [diff] [blame] | 220 | // We don't need to do anything here. |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 221 | return true; |
| 222 | } |
| 223 | |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 224 | // static |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 225 | const base::string16 ChannelWin::PipeName( |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 226 | const std::string& channel_id, int32* secret) { |
[email protected] | c2391b8 | 2011-05-06 17:39:07 | [diff] [blame] | 227 | std::string name("\\\\.\\pipe\\chrome."); |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 228 | |
| 229 | // Prevent the shared secret from ending up in the pipe name. |
| 230 | size_t index = channel_id.find_first_of('\\'); |
| 231 | if (index != std::string::npos) { |
| 232 | if (secret) // Retrieve the secret if asked for. |
| 233 | base::StringToInt(channel_id.substr(index + 1), secret); |
thestig | e5c64d9 | 2014-11-07 01:19:24 | [diff] [blame^] | 234 | return base::ASCIIToUTF16(name.append(channel_id.substr(0, index - 1))); |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | // This case is here to support predictable named pipes in tests. |
| 238 | if (secret) |
| 239 | *secret = 0; |
thestig | e5c64d9 | 2014-11-07 01:19:24 | [diff] [blame^] | 240 | return base::ASCIIToUTF16(name.append(channel_id)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 241 | } |
| 242 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 243 | bool ChannelWin::CreatePipe(const IPC::ChannelHandle &channel_handle, |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 244 | Mode mode) { |
| 245 | DCHECK(!pipe_.IsValid()); |
[email protected] | 905cda81 | 2013-12-20 09:04:55 | [diff] [blame] | 246 | base::string16 pipe_name; |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 247 | // If we already have a valid pipe for channel just copy it. |
| 248 | if (channel_handle.pipe.handle) { |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 249 | // TODO(rvargas) crbug.com/415294: ChannelHandle should either go away in |
| 250 | // favor of two independent entities (name/file), or it should be a move- |
| 251 | // only type with a base::File member. In any case, this code should not |
| 252 | // call DuplicateHandle. |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 253 | DCHECK(channel_handle.name.empty()); |
| 254 | pipe_name = L"Not Available"; // Just used for LOG |
| 255 | // Check that the given pipe confirms to the specified mode. We can |
| 256 | // only check for PIPE_TYPE_MESSAGE & PIPE_SERVER_END flags since the |
| 257 | // other flags (PIPE_TYPE_BYTE, and PIPE_CLIENT_END) are defined as 0. |
| 258 | DWORD flags = 0; |
| 259 | GetNamedPipeInfo(channel_handle.pipe.handle, &flags, NULL, NULL, NULL); |
| 260 | DCHECK(!(flags & PIPE_TYPE_MESSAGE)); |
| 261 | if (((mode & MODE_SERVER_FLAG) && !(flags & PIPE_SERVER_END)) || |
| 262 | ((mode & MODE_CLIENT_FLAG) && (flags & PIPE_SERVER_END))) { |
| 263 | LOG(WARNING) << "Inconsistent open mode. Mode :" << mode; |
| 264 | return false; |
| 265 | } |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 266 | HANDLE local_handle; |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 267 | if (!DuplicateHandle(GetCurrentProcess(), |
| 268 | channel_handle.pipe.handle, |
| 269 | GetCurrentProcess(), |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 270 | &local_handle, |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 271 | 0, |
| 272 | FALSE, |
| 273 | DUPLICATE_SAME_ACCESS)) { |
| 274 | LOG(WARNING) << "DuplicateHandle failed. Error :" << GetLastError(); |
| 275 | return false; |
| 276 | } |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 277 | pipe_.Set(local_handle); |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 278 | } else if (mode & MODE_SERVER_FLAG) { |
| 279 | DCHECK(!channel_handle.pipe.handle); |
| 280 | const DWORD open_mode = PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | |
| 281 | FILE_FLAG_FIRST_PIPE_INSTANCE; |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 282 | pipe_name = PipeName(channel_handle.name, &client_secret_); |
| 283 | validate_client_ = !!client_secret_; |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 284 | pipe_.Set(CreateNamedPipeW(pipe_name.c_str(), |
| 285 | open_mode, |
| 286 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, |
| 287 | 1, |
| 288 | Channel::kReadBufferSize, |
| 289 | Channel::kReadBufferSize, |
| 290 | 5000, |
| 291 | NULL)); |
[email protected] | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 292 | } else if (mode & MODE_CLIENT_FLAG) { |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 293 | DCHECK(!channel_handle.pipe.handle); |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 294 | pipe_name = PipeName(channel_handle.name, &client_secret_); |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 295 | pipe_.Set(CreateFileW(pipe_name.c_str(), |
| 296 | GENERIC_READ | GENERIC_WRITE, |
| 297 | 0, |
| 298 | NULL, |
| 299 | OPEN_EXISTING, |
| 300 | SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION | |
| 301 | FILE_FLAG_OVERLAPPED, |
| 302 | NULL)); |
[email protected] | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 303 | } else { |
| 304 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 305 | } |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 306 | |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 307 | if (!pipe_.IsValid()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 308 | // If this process is being closed, the pipe may be gone already. |
[email protected] | 8398bf1 | 2014-06-26 21:11:34 | [diff] [blame] | 309 | PLOG(WARNING) << "Unable to create pipe \"" << pipe_name << "\" in " |
| 310 | << (mode & MODE_SERVER_FLAG ? "server" : "client") << " mode"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 311 | return false; |
| 312 | } |
| 313 | |
| 314 | // Create the Hello message to be sent when Connect is called |
| 315 | scoped_ptr<Message> m(new Message(MSG_ROUTING_NONE, |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 316 | HELLO_MESSAGE_TYPE, |
| 317 | IPC::Message::PRIORITY_NORMAL)); |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 318 | |
| 319 | // Don't send the secret to the untrusted process, and don't send a secret |
| 320 | // if the value is zero (for IPC backwards compatability). |
| 321 | int32 secret = validate_client_ ? 0 : client_secret_; |
| 322 | if (!m->WriteInt(GetCurrentProcessId()) || |
| 323 | (secret && !m->WriteUInt32(secret))) { |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 324 | pipe_.Close(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 325 | return false; |
| 326 | } |
| 327 | |
[email protected] | 2d0faf1 | 2014-06-27 04:50:43 | [diff] [blame] | 328 | debug_flags_ |= INIT_DONE; |
| 329 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 330 | output_queue_.push(m.release()); |
| 331 | return true; |
| 332 | } |
| 333 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 334 | bool ChannelWin::Connect() { |
[email protected] | 1934072 | 2009-08-17 19:53:25 | [diff] [blame] | 335 | DLOG_IF(WARNING, thread_check_.get()) << "Connect called more than once"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 336 | |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 337 | if (!thread_check_.get()) |
[email protected] | 327e52b | 2012-06-25 21:11:36 | [diff] [blame] | 338 | thread_check_.reset(new base::ThreadChecker()); |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 339 | |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 340 | if (!pipe_.IsValid()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 341 | return false; |
| 342 | |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 343 | base::MessageLoopForIO::current()->RegisterIOHandler(pipe_.Get(), this); |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 344 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 345 | // Check to see if there is a client connected to our pipe... |
| 346 | if (waiting_connect_) |
| 347 | ProcessConnection(); |
| 348 | |
| 349 | if (!input_state_.is_pending) { |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 350 | // Complete setup asynchronously. By not setting input_state_.is_pending |
| 351 | // to true, we indicate to OnIOCompleted that this is the special |
| 352 | // initialization signal. |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 353 | base::MessageLoopForIO::current()->PostTask( |
| 354 | FROM_HERE, |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 355 | base::Bind(&ChannelWin::OnIOCompleted, |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 356 | weak_factory_.GetWeakPtr(), |
| 357 | &input_state_.context, |
| 358 | 0, |
| 359 | 0)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | if (!waiting_connect_) |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 363 | ProcessOutgoingMessages(NULL, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 364 | return true; |
| 365 | } |
| 366 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 367 | bool ChannelWin::ProcessConnection() { |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 368 | DCHECK(thread_check_->CalledOnValidThread()); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 369 | if (input_state_.is_pending) |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 370 | input_state_.is_pending = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 371 | |
| 372 | // Do we have a client connected to our pipe? |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 373 | if (!pipe_.IsValid()) |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 374 | return false; |
| 375 | |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 376 | BOOL ok = ConnectNamedPipe(pipe_.Get(), &input_state_.context.overlapped); |
[email protected] | 2d0faf1 | 2014-06-27 04:50:43 | [diff] [blame] | 377 | debug_flags_ |= CALLED_CONNECT; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 378 | |
| 379 | DWORD err = GetLastError(); |
| 380 | if (ok) { |
| 381 | // Uhm, the API documentation says that this function should never |
| 382 | // return success when used in overlapped mode. |
| 383 | NOTREACHED(); |
| 384 | return false; |
| 385 | } |
| 386 | |
| 387 | switch (err) { |
| 388 | case ERROR_IO_PENDING: |
| 389 | input_state_.is_pending = true; |
[email protected] | 2d0faf1 | 2014-06-27 04:50:43 | [diff] [blame] | 390 | debug_flags_ |= PENDING_CONNECT; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 391 | break; |
| 392 | case ERROR_PIPE_CONNECTED: |
[email protected] | 2d0faf1 | 2014-06-27 04:50:43 | [diff] [blame] | 393 | debug_flags_ |= PIPE_CONNECTED; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 394 | waiting_connect_ = false; |
| 395 | break; |
[email protected] | 20aa32c | 2009-07-14 22:25:49 | [diff] [blame] | 396 | case ERROR_NO_DATA: |
| 397 | // The pipe is being closed. |
| 398 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 399 | default: |
| 400 | NOTREACHED(); |
| 401 | return false; |
| 402 | } |
| 403 | |
| 404 | return true; |
| 405 | } |
| 406 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 407 | bool ChannelWin::ProcessOutgoingMessages( |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 408 | base::MessageLoopForIO::IOContext* context, |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 409 | DWORD bytes_written) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 410 | DCHECK(!waiting_connect_); // Why are we trying to send messages if there's |
| 411 | // no connection? |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 412 | DCHECK(thread_check_->CalledOnValidThread()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 413 | |
| 414 | if (output_state_.is_pending) { |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 415 | DCHECK(context); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 416 | output_state_.is_pending = false; |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 417 | if (!context || bytes_written == 0) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 418 | DWORD err = GetLastError(); |
| 419 | LOG(ERROR) << "pipe error: " << err; |
| 420 | return false; |
| 421 | } |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 422 | // Message was sent. |
[email protected] | 88ecb5f5 | 2014-01-21 14:29:36 | [diff] [blame] | 423 | CHECK(!output_queue_.empty()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 424 | Message* m = output_queue_.front(); |
| 425 | output_queue_.pop(); |
| 426 | delete m; |
| 427 | } |
| 428 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 429 | if (output_queue_.empty()) |
| 430 | return true; |
| 431 | |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 432 | if (!pipe_.IsValid()) |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 433 | return false; |
| 434 | |
| 435 | // Write to pipe... |
| 436 | Message* m = output_queue_.front(); |
[email protected] | 8a86140 | 2011-01-28 19:59:11 | [diff] [blame] | 437 | DCHECK(m->size() <= INT_MAX); |
[email protected] | 2d0faf1 | 2014-06-27 04:50:43 | [diff] [blame] | 438 | debug_flags_ |= WRITE_MSG; |
[email protected] | 65727eb | 2014-07-31 12:24:29 | [diff] [blame] | 439 | CHECK(!writing_); |
| 440 | writing_ = true; |
rvargas | 431ce73 | 2014-09-16 02:44:15 | [diff] [blame] | 441 | write_size_ = static_cast<uint32>(m->size()); |
| 442 | write_error_ = 0; |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 443 | BOOL ok = WriteFile(pipe_.Get(), |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 444 | m->data(), |
rvargas | 431ce73 | 2014-09-16 02:44:15 | [diff] [blame] | 445 | write_size_, |
| 446 | NULL, |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 447 | &output_state_.context.overlapped); |
| 448 | if (!ok) { |
rvargas | 431ce73 | 2014-09-16 02:44:15 | [diff] [blame] | 449 | write_error_ = GetLastError(); |
| 450 | if (write_error_ == ERROR_IO_PENDING) { |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 451 | output_state_.is_pending = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 452 | |
[email protected] | 2a9d601b | 2010-10-19 23:50:00 | [diff] [blame] | 453 | DVLOG(2) << "sent pending message @" << m << " on channel @" << this |
| 454 | << " with type " << m->type(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 455 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 456 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 457 | } |
[email protected] | 65727eb | 2014-07-31 12:24:29 | [diff] [blame] | 458 | writing_ = false; |
rvargas | 431ce73 | 2014-09-16 02:44:15 | [diff] [blame] | 459 | last_write_error_ = write_error_; |
| 460 | LOG(ERROR) << "pipe error: " << write_error_; |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 461 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 462 | } |
| 463 | |
[email protected] | 2a9d601b | 2010-10-19 23:50:00 | [diff] [blame] | 464 | DVLOG(2) << "sent message @" << m << " on channel @" << this |
| 465 | << " with type " << m->type(); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 466 | |
| 467 | output_state_.is_pending = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 468 | return true; |
| 469 | } |
| 470 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 471 | void ChannelWin::OnIOCompleted( |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 472 | base::MessageLoopForIO::IOContext* context, |
| 473 | DWORD bytes_transfered, |
| 474 | DWORD error) { |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 475 | bool ok = true; |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 476 | DCHECK(thread_check_->CalledOnValidThread()); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 477 | if (context == &input_state_.context) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 478 | if (waiting_connect_) { |
[email protected] | 2d0faf1 | 2014-06-27 04:50:43 | [diff] [blame] | 479 | debug_flags_ |= CONNECT_COMPLETED; |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 480 | if (!ProcessConnection()) |
| 481 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 482 | // We may have some messages queued up to send... |
| 483 | if (!output_queue_.empty() && !output_state_.is_pending) |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 484 | ProcessOutgoingMessages(NULL, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 485 | if (input_state_.is_pending) |
| 486 | return; |
| 487 | // else, fall-through and look for incoming messages... |
| 488 | } |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 489 | |
| 490 | // We don't support recursion through OnMessageReceived yet! |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 491 | DCHECK(!processing_incoming_); |
[email protected] | 997ec9f | 2012-11-21 04:44:14 | [diff] [blame] | 492 | base::AutoReset<bool> auto_reset_processing_incoming( |
| 493 | &processing_incoming_, true); |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 494 | |
| 495 | // Process the new data. |
| 496 | if (input_state_.is_pending) { |
| 497 | // This is the normal case for everything except the initialization step. |
[email protected] | 2d0faf1 | 2014-06-27 04:50:43 | [diff] [blame] | 498 | debug_flags_ |= READ_COMPLETED; |
| 499 | if (debug_flags_ & WAIT_FOR_READ) { |
| 500 | CHECK(!(debug_flags_ & WAIT_FOR_READ_COMPLETE)); |
| 501 | debug_flags_ |= WAIT_FOR_READ_COMPLETE; |
| 502 | } |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 503 | input_state_.is_pending = false; |
| 504 | if (!bytes_transfered) |
| 505 | ok = false; |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 506 | else if (pipe_.IsValid()) |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 507 | ok = AsyncReadComplete(bytes_transfered); |
| 508 | } else { |
| 509 | DCHECK(!bytes_transfered); |
| 510 | } |
| 511 | |
| 512 | // Request more data. |
| 513 | if (ok) |
| 514 | ok = ProcessIncomingMessages(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 515 | } else { |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 516 | DCHECK(context == &output_state_.context); |
[email protected] | 65727eb | 2014-07-31 12:24:29 | [diff] [blame] | 517 | CHECK(writing_); |
| 518 | CHECK(output_state_.is_pending); |
| 519 | writing_ = false; |
[email protected] | 2d0faf1 | 2014-06-27 04:50:43 | [diff] [blame] | 520 | debug_flags_ |= WRITE_COMPLETED; |
| 521 | if (debug_flags_ & WAIT_FOR_WRITE) { |
| 522 | CHECK(!(debug_flags_ & WAIT_FOR_WRITE_COMPLETE)); |
| 523 | debug_flags_ |= WAIT_FOR_WRITE_COMPLETE; |
| 524 | } |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 525 | ok = ProcessOutgoingMessages(context, bytes_transfered); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 526 | } |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 527 | if (!ok && pipe_.IsValid()) { |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 528 | // We don't want to re-enter Close(). |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 529 | Close(); |
[email protected] | d805c6a | 2012-03-08 12:30:28 | [diff] [blame] | 530 | listener()->OnChannelError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 534 | //------------------------------------------------------------------------------ |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 535 | // Channel's methods |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 536 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 537 | // static |
| 538 | scoped_ptr<Channel> Channel::Create( |
| 539 | const IPC::ChannelHandle &channel_handle, Mode mode, Listener* listener) { |
| 540 | return scoped_ptr<Channel>( |
| 541 | new ChannelWin(channel_handle, mode, listener)); |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 542 | } |
| 543 | |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 544 | // static |
| 545 | bool Channel::IsNamedServerInitialized(const std::string& channel_id) { |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 546 | return ChannelWin::IsNamedServerInitialized(channel_id); |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 547 | } |
| 548 | |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 549 | // static |
| 550 | std::string Channel::GenerateVerifiedChannelID(const std::string& prefix) { |
| 551 | // Windows pipes can be enumerated by low-privileged processes. So, we |
| 552 | // append a strong random value after the \ character. This value is not |
| 553 | // included in the pipe name, but sent as part of the client hello, to |
| 554 | // hijacking the pipe name to spoof the client. |
| 555 | |
| 556 | std::string id = prefix; |
| 557 | if (!id.empty()) |
| 558 | id.append("."); |
| 559 | |
| 560 | int secret; |
| 561 | do { // Guarantee we get a non-zero value. |
| 562 | secret = base::RandInt(0, std::numeric_limits<int>::max()); |
| 563 | } while (secret == 0); |
| 564 | |
| 565 | id.append(GenerateUniqueRandomChannelID()); |
| 566 | return id.append(base::StringPrintf("\\%d", secret)); |
| 567 | } |
| 568 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 569 | } // namespace IPC |