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
3 // found in the LICENSE file.
5 #include "ipc/ipc_channel_win.h"
9 #include "base/auto_reset.h"
10 #include "base/bind.h"
11 #include "base/compiler_specific.h"
12 #include "base/logging.h"
13 #include "base/pickle.h"
14 #include "base/process/process_handle.h"
15 #include "base/rand_util.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/threading/thread_checker.h"
19 #include "base/win/scoped_handle.h"
20 #include "ipc/ipc_listener.h"
21 #include "ipc/ipc_logging.h"
22 #include "ipc/ipc_message_utils.h"
28 CALLED_CONNECT
= 1 << 1,
29 PENDING_CONNECT
= 1 << 2,
30 CONNECT_COMPLETED
= 1 << 3,
31 PIPE_CONNECTED
= 1 << 4,
34 WRITE_COMPLETED
= 1 << 7,
35 READ_COMPLETED
= 1 << 8,
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
47 ChannelWin::State::State(ChannelWin
* channel
) : is_pending(false) {
48 memset(&context
.overlapped
, 0, sizeof(context
.overlapped
));
49 context
.handler
= channel
;
52 ChannelWin::State::~State() {
53 COMPILE_ASSERT(!offsetof(ChannelWin::State
, context
),
54 starts_with_io_context
);
57 ChannelWin::ChannelWin(const IPC::ChannelHandle
&channel_handle
,
58 Mode mode
, Listener
* listener
)
59 : ChannelReader(listener
),
62 peer_pid_(base::kNullProcessId
),
63 waiting_connect_(mode
& MODE_SERVER_FLAG
),
64 processing_incoming_(false),
65 validate_client_(false),
73 CreatePipe(channel_handle
, mode
);
76 ChannelWin::~ChannelWin() {
80 void ChannelWin::Close() {
81 if (thread_check_
.get()) {
82 DCHECK(thread_check_
->CalledOnValidThread());
84 debug_flags_
|= CLOSED
;
86 if (input_state_
.is_pending
|| output_state_
.is_pending
)
87 CancelIo(pipe_
.Get());
89 // Closing the handle at this point prevents us from issuing more requests
90 // form OnIOCompleted().
94 if (input_state_
.is_pending
)
95 debug_flags_
|= WAIT_FOR_READ
;
97 if (output_state_
.is_pending
)
98 debug_flags_
|= WAIT_FOR_WRITE
;
100 // Make sure all IO has completed.
101 base::Time start
= base::Time::Now();
102 while (input_state_
.is_pending
|| output_state_
.is_pending
) {
103 base::MessageLoopForIO::current()->WaitForIOCompletion(INFINITE
, this);
106 while (!output_queue_
.empty()) {
107 Message
* m
= output_queue_
.front();
113 bool ChannelWin::Send(Message
* message
) {
114 DCHECK(thread_check_
->CalledOnValidThread());
115 DVLOG(2) << "sending message @" << message
<< " on channel @" << this
116 << " with type " << message
->type()
117 << " (" << output_queue_
.size() << " in queue)";
119 #ifdef IPC_MESSAGE_LOG_ENABLED
120 Logging::GetInstance()->OnSendMessage(message
, "");
123 message
->TraceMessageBegin();
124 output_queue_
.push(message
);
125 // ensure waiting to write
126 if (!waiting_connect_
) {
127 if (!output_state_
.is_pending
) {
128 if (!ProcessOutgoingMessages(NULL
, 0))
136 base::ProcessId
ChannelWin::GetPeerPID() const {
140 base::ProcessId
ChannelWin::GetSelfPID() const {
141 return GetCurrentProcessId();
145 bool ChannelWin::IsNamedServerInitialized(
146 const std::string
& channel_id
) {
147 if (WaitNamedPipe(PipeName(channel_id
, NULL
).c_str(), 1))
149 // If ERROR_SEM_TIMEOUT occurred, the pipe exists but is handling another
151 return GetLastError() == ERROR_SEM_TIMEOUT
;
154 ChannelWin::ReadState
ChannelWin::ReadData(
157 int* /* bytes_read */) {
158 if (!pipe_
.IsValid())
161 debug_flags_
|= READ_MSG
;
162 DWORD bytes_read
= 0;
163 BOOL ok
= ReadFile(pipe_
.Get(), buffer
, buffer_len
,
164 &bytes_read
, &input_state_
.context
.overlapped
);
166 DWORD err
= GetLastError();
167 if (err
== ERROR_IO_PENDING
) {
168 input_state_
.is_pending
= true;
171 LOG(ERROR
) << "pipe error: " << err
;
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.
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;
187 bool ChannelWin::WillDispatchInputMessage(Message
* msg
) {
188 // Make sure we get a hello when client validation is required.
189 if (validate_client_
)
190 return IsHelloMessage(*msg
);
194 void ChannelWin::HandleInternalMessage(const Message
& msg
) {
195 DCHECK_EQ(msg
.type(), static_cast<unsigned>(Channel::HELLO_MESSAGE_TYPE
));
196 // The hello message contains one parameter containing the PID.
197 PickleIterator
it(msg
);
199 bool failed
= !it
.ReadInt(&claimed_pid
);
201 if (!failed
&& validate_client_
) {
203 failed
= it
.ReadInt(&secret
) ? (secret
!= client_secret_
) : true;
209 listener()->OnChannelError();
213 peer_pid_
= claimed_pid
;
214 // Validation completed.
215 validate_client_
= false;
216 listener()->OnChannelConnected(claimed_pid
);
219 bool ChannelWin::DidEmptyInputBuffers() {
220 // We don't need to do anything here.
225 const base::string16
ChannelWin::PipeName(
226 const std::string
& channel_id
, int32
* secret
) {
227 std::string
name("\\\\.\\pipe\\chrome.");
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
);
234 return base::ASCIIToUTF16(name
.append(channel_id
.substr(0, index
- 1)));
237 // This case is here to support predictable named pipes in tests.
240 return base::ASCIIToUTF16(name
.append(channel_id
));
243 bool ChannelWin::CreatePipe(const IPC::ChannelHandle
&channel_handle
,
245 DCHECK(!pipe_
.IsValid());
246 base::string16 pipe_name
;
247 // If we already have a valid pipe for channel just copy it.
248 if (channel_handle
.pipe
.handle
) {
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.
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.
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
;
267 if (!DuplicateHandle(GetCurrentProcess(),
268 channel_handle
.pipe
.handle
,
273 DUPLICATE_SAME_ACCESS
)) {
274 LOG(WARNING
) << "DuplicateHandle failed. Error :" << GetLastError();
277 pipe_
.Set(local_handle
);
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
;
282 pipe_name
= PipeName(channel_handle
.name
, &client_secret_
);
283 validate_client_
= !!client_secret_
;
284 pipe_
.Set(CreateNamedPipeW(pipe_name
.c_str(),
286 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
,
288 Channel::kReadBufferSize
,
289 Channel::kReadBufferSize
,
292 } else if (mode
& MODE_CLIENT_FLAG
) {
293 DCHECK(!channel_handle
.pipe
.handle
);
294 pipe_name
= PipeName(channel_handle
.name
, &client_secret_
);
295 pipe_
.Set(CreateFileW(pipe_name
.c_str(),
296 GENERIC_READ
| GENERIC_WRITE
,
300 SECURITY_SQOS_PRESENT
| SECURITY_IDENTIFICATION
|
301 FILE_FLAG_OVERLAPPED
,
307 if (!pipe_
.IsValid()) {
308 // If this process is being closed, the pipe may be gone already.
309 PLOG(WARNING
) << "Unable to create pipe \"" << pipe_name
<< "\" in "
310 << (mode
& MODE_SERVER_FLAG
? "server" : "client") << " mode";
314 // Create the Hello message to be sent when Connect is called
315 scoped_ptr
<Message
> m(new Message(MSG_ROUTING_NONE
,
317 IPC::Message::PRIORITY_NORMAL
));
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
))) {
328 debug_flags_
|= INIT_DONE
;
330 output_queue_
.push(m
.release());
334 bool ChannelWin::Connect() {
335 DLOG_IF(WARNING
, thread_check_
.get()) << "Connect called more than once";
337 if (!thread_check_
.get())
338 thread_check_
.reset(new base::ThreadChecker());
340 if (!pipe_
.IsValid())
343 base::MessageLoopForIO::current()->RegisterIOHandler(pipe_
.Get(), this);
345 // Check to see if there is a client connected to our pipe...
346 if (waiting_connect_
)
349 if (!input_state_
.is_pending
) {
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.
353 base::MessageLoopForIO::current()->PostTask(
355 base::Bind(&ChannelWin::OnIOCompleted
,
356 weak_factory_
.GetWeakPtr(),
357 &input_state_
.context
,
362 if (!waiting_connect_
)
363 ProcessOutgoingMessages(NULL
, 0);
367 bool ChannelWin::ProcessConnection() {
368 DCHECK(thread_check_
->CalledOnValidThread());
369 if (input_state_
.is_pending
)
370 input_state_
.is_pending
= false;
372 // Do we have a client connected to our pipe?
373 if (!pipe_
.IsValid())
376 BOOL ok
= ConnectNamedPipe(pipe_
.Get(), &input_state_
.context
.overlapped
);
377 debug_flags_
|= CALLED_CONNECT
;
379 DWORD err
= GetLastError();
381 // Uhm, the API documentation says that this function should never
382 // return success when used in overlapped mode.
388 case ERROR_IO_PENDING
:
389 input_state_
.is_pending
= true;
390 debug_flags_
|= PENDING_CONNECT
;
392 case ERROR_PIPE_CONNECTED
:
393 debug_flags_
|= PIPE_CONNECTED
;
394 waiting_connect_
= false;
397 // The pipe is being closed.
407 bool ChannelWin::ProcessOutgoingMessages(
408 base::MessageLoopForIO::IOContext
* context
,
409 DWORD bytes_written
) {
410 DCHECK(!waiting_connect_
); // Why are we trying to send messages if there's
412 DCHECK(thread_check_
->CalledOnValidThread());
414 if (output_state_
.is_pending
) {
416 output_state_
.is_pending
= false;
417 if (!context
|| bytes_written
== 0) {
418 DWORD err
= GetLastError();
419 LOG(ERROR
) << "pipe error: " << err
;
423 CHECK(!output_queue_
.empty());
424 Message
* m
= output_queue_
.front();
429 if (output_queue_
.empty())
432 if (!pipe_
.IsValid())
436 Message
* m
= output_queue_
.front();
437 DCHECK(m
->size() <= INT_MAX
);
438 debug_flags_
|= WRITE_MSG
;
441 write_size_
= static_cast<uint32
>(m
->size());
443 BOOL ok
= WriteFile(pipe_
.Get(),
447 &output_state_
.context
.overlapped
);
449 write_error_
= GetLastError();
450 if (write_error_
== ERROR_IO_PENDING
) {
451 output_state_
.is_pending
= true;
453 DVLOG(2) << "sent pending message @" << m
<< " on channel @" << this
454 << " with type " << m
->type();
459 last_write_error_
= write_error_
;
460 LOG(ERROR
) << "pipe error: " << write_error_
;
464 DVLOG(2) << "sent message @" << m
<< " on channel @" << this
465 << " with type " << m
->type();
467 output_state_
.is_pending
= true;
471 void ChannelWin::OnIOCompleted(
472 base::MessageLoopForIO::IOContext
* context
,
473 DWORD bytes_transfered
,
476 DCHECK(thread_check_
->CalledOnValidThread());
477 if (context
== &input_state_
.context
) {
478 if (waiting_connect_
) {
479 debug_flags_
|= CONNECT_COMPLETED
;
480 if (!ProcessConnection())
482 // We may have some messages queued up to send...
483 if (!output_queue_
.empty() && !output_state_
.is_pending
)
484 ProcessOutgoingMessages(NULL
, 0);
485 if (input_state_
.is_pending
)
487 // else, fall-through and look for incoming messages...
490 // We don't support recursion through OnMessageReceived yet!
491 DCHECK(!processing_incoming_
);
492 base::AutoReset
<bool> auto_reset_processing_incoming(
493 &processing_incoming_
, true);
495 // Process the new data.
496 if (input_state_
.is_pending
) {
497 // This is the normal case for everything except the initialization step.
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
;
503 input_state_
.is_pending
= false;
504 if (!bytes_transfered
)
506 else if (pipe_
.IsValid())
507 ok
= AsyncReadComplete(bytes_transfered
);
509 DCHECK(!bytes_transfered
);
512 // Request more data.
514 ok
= ProcessIncomingMessages();
516 DCHECK(context
== &output_state_
.context
);
518 CHECK(output_state_
.is_pending
);
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
;
525 ok
= ProcessOutgoingMessages(context
, bytes_transfered
);
527 if (!ok
&& pipe_
.IsValid()) {
528 // We don't want to re-enter Close().
530 listener()->OnChannelError();
534 //------------------------------------------------------------------------------
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
));
545 bool Channel::IsNamedServerInitialized(const std::string
& channel_id
) {
546 return ChannelWin::IsNamedServerInitialized(channel_id
);
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.
556 std::string id
= prefix
;
561 do { // Guarantee we get a non-zero value.
562 secret
= base::RandInt(0, std::numeric_limits
<int>::max());
563 } while (secret
== 0);
565 id
.append(GenerateUniqueRandomChannelID());
566 return id
.append(base::StringPrintf("\\%d", secret
));