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.
9 #include "base/message_loop.h"
10 #include "base/process_util.h"
11 #include "base/threading/platform_thread.h"
12 #include "ipc/ipc_channel.h"
13 #include "ipc/ipc_channel_proxy.h"
14 #include "ipc/ipc_tests.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "testing/multiprocess_func_list.h"
18 // IPC messages for testing ---------------------------------------------------
20 #define IPC_MESSAGE_IMPL
21 #include "ipc/ipc_message_macros.h"
23 #define IPC_MESSAGE_START TestMsgStart
25 // Generic message class that is an int followed by a wstring.
26 IPC_MESSAGE_CONTROL2(MsgClassIS
, int, std::wstring
)
28 // Generic message class that is a wstring followed by an int.
29 IPC_MESSAGE_CONTROL2(MsgClassSI
, std::wstring
, int)
31 // Message to create a mutex in the IPC server, using the received name.
32 IPC_MESSAGE_CONTROL2(MsgDoMutex
, std::wstring
, int)
34 // Used to generate an ID for a message that should not exist.
35 IPC_MESSAGE_CONTROL0(MsgUnhandled
)
37 // ----------------------------------------------------------------------------
39 TEST(IPCMessageIntegrity
, ReadBeyondBufferStr
) {
40 //This was BUG 984408.
41 uint32 v1
= kuint32max
- 1;
43 IPC::Message
m(0, 1, IPC::Message::PRIORITY_NORMAL
);
44 EXPECT_TRUE(m
.WriteInt(v1
));
45 EXPECT_TRUE(m
.WriteInt(v2
));
47 PickleIterator
iter(m
);
49 EXPECT_FALSE(m
.ReadString(&iter
, &vs
));
52 TEST(IPCMessageIntegrity
, ReadBeyondBufferWStr
) {
53 //This was BUG 984408.
54 uint32 v1
= kuint32max
- 1;
56 IPC::Message
m(0, 1, IPC::Message::PRIORITY_NORMAL
);
57 EXPECT_TRUE(m
.WriteInt(v1
));
58 EXPECT_TRUE(m
.WriteInt(v2
));
60 PickleIterator
iter(m
);
62 EXPECT_FALSE(m
.ReadWString(&iter
, &vs
));
65 TEST(IPCMessageIntegrity
, ReadBytesBadIterator
) {
66 // This was BUG 1035467.
67 IPC::Message
m(0, 1, IPC::Message::PRIORITY_NORMAL
);
68 EXPECT_TRUE(m
.WriteInt(1));
69 EXPECT_TRUE(m
.WriteInt(2));
71 PickleIterator
iter(m
);
72 const char* data
= NULL
;
73 EXPECT_TRUE(m
.ReadBytes(&iter
, &data
, sizeof(int)));
76 TEST(IPCMessageIntegrity
, ReadVectorNegativeSize
) {
77 // A slight variation of BUG 984408. Note that the pickling of vector<char>
78 // has a specialized template which is not vulnerable to this bug. So here
79 // try to hit the non-specialized case vector<P>.
80 IPC::Message
m(0, 1, IPC::Message::PRIORITY_NORMAL
);
81 EXPECT_TRUE(m
.WriteInt(-1)); // This is the count of elements.
82 EXPECT_TRUE(m
.WriteInt(1));
83 EXPECT_TRUE(m
.WriteInt(2));
84 EXPECT_TRUE(m
.WriteInt(3));
86 std::vector
<double> vec
;
87 PickleIterator
iter(m
);
88 EXPECT_FALSE(ReadParam(&m
, &iter
, &vec
));
91 TEST(IPCMessageIntegrity
, ReadVectorTooLarge1
) {
92 // This was BUG 1006367. This is the large but positive length case. Again
93 // we try to hit the non-specialized case vector<P>.
94 IPC::Message
m(0, 1, IPC::Message::PRIORITY_NORMAL
);
95 EXPECT_TRUE(m
.WriteInt(0x21000003)); // This is the count of elements.
96 EXPECT_TRUE(m
.WriteInt64(1));
97 EXPECT_TRUE(m
.WriteInt64(2));
99 std::vector
<int64
> vec
;
100 PickleIterator
iter(m
);
101 EXPECT_FALSE(ReadParam(&m
, &iter
, &vec
));
104 TEST(IPCMessageIntegrity
, ReadVectorTooLarge2
) {
105 // This was BUG 1006367. This is the large but positive with an additional
106 // integer overflow when computing the actual byte size. Again we try to hit
107 // the non-specialized case vector<P>.
108 IPC::Message
m(0, 1, IPC::Message::PRIORITY_NORMAL
);
109 EXPECT_TRUE(m
.WriteInt(0x71000000)); // This is the count of elements.
110 EXPECT_TRUE(m
.WriteInt64(1));
111 EXPECT_TRUE(m
.WriteInt64(2));
113 std::vector
<int64
> vec
;
114 PickleIterator
iter(m
);
115 EXPECT_FALSE(ReadParam(&m
, &iter
, &vec
));
118 class SimpleListener
: public IPC::Listener
{
120 SimpleListener() : other_(NULL
) {
122 void Init(IPC::Sender
* s
) {
130 FUZZER_ROUTING_ID
= 5
133 // The fuzzer server class. It runs in a child process and expects
134 // only two IPC calls; after that it exits the message loop which
135 // terminates the child process.
136 class FuzzerServerListener
: public SimpleListener
{
138 FuzzerServerListener() : message_count_(2), pending_messages_(0) {
140 virtual bool OnMessageReceived(const IPC::Message
& msg
) {
141 if (msg
.routing_id() == MSG_ROUTING_CONTROL
) {
143 IPC_BEGIN_MESSAGE_MAP(FuzzerServerListener
, msg
)
144 IPC_MESSAGE_HANDLER(MsgClassIS
, OnMsgClassISMessage
)
145 IPC_MESSAGE_HANDLER(MsgClassSI
, OnMsgClassSIMessage
)
146 IPC_END_MESSAGE_MAP()
147 if (pending_messages_
) {
148 // Probably a problem de-serializing the message.
149 ReplyMsgNotHandled(msg
.type());
156 void OnMsgClassISMessage(int value
, const std::wstring
& text
) {
157 UseData(MsgClassIS::ID
, value
, text
);
158 RoundtripAckReply(FUZZER_ROUTING_ID
, MsgClassIS::ID
, value
);
162 void OnMsgClassSIMessage(const std::wstring
& text
, int value
) {
163 UseData(MsgClassSI::ID
, value
, text
);
164 RoundtripAckReply(FUZZER_ROUTING_ID
, MsgClassSI::ID
, value
);
168 bool RoundtripAckReply(int routing
, uint32 type_id
, int reply
) {
169 IPC::Message
* message
= new IPC::Message(routing
, type_id
,
170 IPC::Message::PRIORITY_NORMAL
);
171 message
->WriteInt(reply
+ 1);
172 message
->WriteInt(reply
);
173 return other_
->Send(message
);
179 if (0 == message_count_
)
180 MessageLoop::current()->Quit();
183 void ReplyMsgNotHandled(uint32 type_id
) {
184 RoundtripAckReply(FUZZER_ROUTING_ID
, MsgUnhandled::ID
, type_id
);
188 void UseData(int caller
, int value
, const std::wstring
& text
) {
189 std::wostringstream wos
;
190 wos
<< L
"IPC fuzzer:" << caller
<< " [" << value
<< L
" " << text
<< L
"]\n";
191 std::wstring output
= wos
.str();
192 LOG(WARNING
) << output
.c_str();
196 int pending_messages_
;
199 class FuzzerClientListener
: public SimpleListener
{
201 FuzzerClientListener() : last_msg_(NULL
) {
204 virtual bool OnMessageReceived(const IPC::Message
& msg
) {
205 last_msg_
= new IPC::Message(msg
);
206 MessageLoop::current()->Quit();
210 bool ExpectMessage(int value
, uint32 type_id
) {
211 if (!MsgHandlerInternal(type_id
))
215 PickleIterator
iter(*last_msg_
);
216 if (!last_msg_
->ReadInt(&iter
, &msg_value1
))
218 if (!last_msg_
->ReadInt(&iter
, &msg_value2
))
220 if ((msg_value2
+ 1) != msg_value1
)
222 if (msg_value2
!= value
)
230 bool ExpectMsgNotHandled(uint32 type_id
) {
231 return ExpectMessage(type_id
, MsgUnhandled::ID
);
235 bool MsgHandlerInternal(uint32 type_id
) {
236 MessageLoop::current()->Run();
237 if (NULL
== last_msg_
)
239 if (FUZZER_ROUTING_ID
!= last_msg_
->routing_id())
241 return (type_id
== last_msg_
->type());
244 IPC::Message
* last_msg_
;
247 // Runs the fuzzing server child mode. Returns when the preset number
248 // of messages have been received.
249 MULTIPROCESS_TEST_MAIN(RunFuzzServer
) {
250 MessageLoopForIO main_message_loop
;
251 FuzzerServerListener listener
;
252 IPC::Channel
chan(kFuzzerChannel
, IPC::Channel::MODE_CLIENT
, &listener
);
253 CHECK(chan
.Connect());
254 listener
.Init(&chan
);
255 MessageLoop::current()->Run();
259 class IPCFuzzingTest
: public IPCChannelTest
{
262 // This test makes sure that the FuzzerClientListener and FuzzerServerListener
263 // are working properly by generating two well formed IPC calls.
264 TEST_F(IPCFuzzingTest
, SanityTest
) {
265 FuzzerClientListener listener
;
266 IPC::Channel
chan(kFuzzerChannel
, IPC::Channel::MODE_SERVER
,
268 base::ProcessHandle server_process
= SpawnChild(FUZZER_SERVER
, &chan
);
269 ASSERT_TRUE(server_process
);
270 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1));
271 ASSERT_TRUE(chan
.Connect());
272 listener
.Init(&chan
);
274 IPC::Message
* msg
= NULL
;
276 msg
= new MsgClassIS(value
, L
"expect 43");
278 EXPECT_TRUE(listener
.ExpectMessage(value
, MsgClassIS::ID
));
280 msg
= new MsgClassSI(L
"expect 44", ++value
);
282 EXPECT_TRUE(listener
.ExpectMessage(value
, MsgClassSI::ID
));
284 EXPECT_TRUE(base::WaitForSingleProcess(server_process
, 5000));
285 base::CloseProcessHandle(server_process
);
288 // This test uses a payload that is smaller than expected.
289 // This generates an error while unpacking the IPC buffer which in
290 // In debug this triggers an assertion and in release it is ignored(!!). Right
291 // after we generate another valid IPC to make sure framing is working
293 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
294 TEST_F(IPCFuzzingTest
, MsgBadPayloadShort
) {
295 FuzzerClientListener listener
;
296 IPC::Channel
chan(kFuzzerChannel
, IPC::Channel::MODE_SERVER
,
298 base::ProcessHandle server_process
= SpawnChild(FUZZER_SERVER
, &chan
);
299 ASSERT_TRUE(server_process
);
300 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1));
301 ASSERT_TRUE(chan
.Connect());
302 listener
.Init(&chan
);
304 IPC::Message
* msg
= new IPC::Message(MSG_ROUTING_CONTROL
, MsgClassIS::ID
,
305 IPC::Message::PRIORITY_NORMAL
);
308 EXPECT_TRUE(listener
.ExpectMsgNotHandled(MsgClassIS::ID
));
310 msg
= new MsgClassSI(L
"expect one", 1);
312 EXPECT_TRUE(listener
.ExpectMessage(1, MsgClassSI::ID
));
314 EXPECT_TRUE(base::WaitForSingleProcess(server_process
, 5000));
315 base::CloseProcessHandle(server_process
);
319 // This test uses a payload that has too many arguments, but so the payload
320 // size is big enough so the unpacking routine does not generate an error as
321 // in the case of MsgBadPayloadShort test.
322 // This test does not pinpoint a flaw (per se) as by design we don't carry
323 // type information on the IPC message.
324 TEST_F(IPCFuzzingTest
, MsgBadPayloadArgs
) {
325 FuzzerClientListener listener
;
326 IPC::Channel
chan(kFuzzerChannel
, IPC::Channel::MODE_SERVER
,
328 base::ProcessHandle server_process
= SpawnChild(FUZZER_SERVER
, &chan
);
329 ASSERT_TRUE(server_process
);
330 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1));
331 ASSERT_TRUE(chan
.Connect());
332 listener
.Init(&chan
);
334 IPC::Message
* msg
= new IPC::Message(MSG_ROUTING_CONTROL
, MsgClassSI::ID
,
335 IPC::Message::PRIORITY_NORMAL
);
336 msg
->WriteWString(L
"d");
338 msg
->WriteInt(0x65); // Extra argument.
341 EXPECT_TRUE(listener
.ExpectMessage(0, MsgClassSI::ID
));
343 // Now send a well formed message to make sure the receiver wasn't
344 // thrown out of sync by the extra argument.
345 msg
= new MsgClassIS(3, L
"expect three");
347 EXPECT_TRUE(listener
.ExpectMessage(3, MsgClassIS::ID
));
349 EXPECT_TRUE(base::WaitForSingleProcess(server_process
, 5000));
350 base::CloseProcessHandle(server_process
);
353 // This class is for testing the IPC_BEGIN_MESSAGE_MAP_EX macros.
354 class ServerMacroExTest
{
356 ServerMacroExTest() : unhandled_msgs_(0) {
359 virtual ~ServerMacroExTest() {
362 virtual bool OnMessageReceived(const IPC::Message
& msg
) {
363 bool msg_is_ok
= false;
364 IPC_BEGIN_MESSAGE_MAP_EX(ServerMacroExTest
, msg
, msg_is_ok
)
365 IPC_MESSAGE_HANDLER(MsgClassIS
, OnMsgClassISMessage
)
366 IPC_MESSAGE_HANDLER(MsgClassSI
, OnMsgClassSIMessage
)
367 IPC_MESSAGE_UNHANDLED(++unhandled_msgs_
)
368 IPC_END_MESSAGE_MAP_EX()
372 int unhandled_msgs() const {
373 return unhandled_msgs_
;
377 void OnMsgClassISMessage(int value
, const std::wstring
& text
) {
379 void OnMsgClassSIMessage(const std::wstring
& text
, int value
) {
384 DISALLOW_COPY_AND_ASSIGN(ServerMacroExTest
);
387 TEST_F(IPCFuzzingTest
, MsgMapExMacro
) {
388 IPC::Message
* msg
= NULL
;
389 ServerMacroExTest server
;
391 // Test the regular messages.
392 msg
= new MsgClassIS(3, L
"text3");
393 EXPECT_TRUE(server
.OnMessageReceived(*msg
));
395 msg
= new MsgClassSI(L
"text2", 2);
396 EXPECT_TRUE(server
.OnMessageReceived(*msg
));
399 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
400 // Test a bad message.
401 msg
= new IPC::Message(MSG_ROUTING_CONTROL
, MsgClassSI::ID
,
402 IPC::Message::PRIORITY_NORMAL
);
404 EXPECT_FALSE(server
.OnMessageReceived(*msg
));
407 msg
= new IPC::Message(MSG_ROUTING_CONTROL
, MsgClassIS::ID
,
408 IPC::Message::PRIORITY_NORMAL
);
411 EXPECT_FALSE(server
.OnMessageReceived(*msg
));
414 EXPECT_EQ(0, server
.unhandled_msgs());