blob: 32fe71caf37db46b75faa3164a84f114a7c5fe89 [file] [log] [blame]
[email protected]b5393332012-01-13 00:11:011// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
initial.commit09911bf2008-07-26 23:55:295#include <stdio.h>
initial.commit09911bf2008-07-26 23:55:296#include <string>
7#include <sstream>
8
[email protected]514411fc2008-12-10 22:28:119#include "base/message_loop.h"
[email protected]d4651ff2008-12-02 16:51:5810#include "base/process_util.h"
[email protected]f214f8792011-01-01 02:17:0811#include "base/threading/platform_thread.h"
[email protected]946d1b22009-07-22 23:57:2112#include "ipc/ipc_channel.h"
13#include "ipc/ipc_channel_proxy.h"
[email protected]946d1b22009-07-22 23:57:2114#include "ipc/ipc_tests.h"
initial.commit09911bf2008-07-26 23:55:2915#include "testing/gtest/include/gtest/gtest.h"
[email protected]95cb7fb92008-12-09 22:00:4716#include "testing/multiprocess_func_list.h"
initial.commit09911bf2008-07-26 23:55:2917
[email protected]1d4ecf42011-08-26 21:27:3018// IPC messages for testing ---------------------------------------------------
19
20#define IPC_MESSAGE_IMPL
21#include "ipc/ipc_message_macros.h"
22
23#define IPC_MESSAGE_START TestMsgStart
24
25// Generic message class that is an int followed by a wstring.
26IPC_MESSAGE_CONTROL2(MsgClassIS, int, std::wstring)
27
28// Generic message class that is a wstring followed by an int.
29IPC_MESSAGE_CONTROL2(MsgClassSI, std::wstring, int)
30
31// Message to create a mutex in the IPC server, using the received name.
32IPC_MESSAGE_CONTROL2(MsgDoMutex, std::wstring, int)
33
34// Used to generate an ID for a message that should not exist.
35IPC_MESSAGE_CONTROL0(MsgUnhandled)
36
37// ----------------------------------------------------------------------------
38
initial.commit09911bf2008-07-26 23:55:2939TEST(IPCMessageIntegrity, ReadBeyondBufferStr) {
40 //This was BUG 984408.
41 uint32 v1 = kuint32max - 1;
42 int v2 = 666;
43 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
44 EXPECT_TRUE(m.WriteInt(v1));
45 EXPECT_TRUE(m.WriteInt(v2));
46
[email protected]ce208f872012-03-07 20:42:5647 PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2948 std::string vs;
49 EXPECT_FALSE(m.ReadString(&iter, &vs));
50}
51
52TEST(IPCMessageIntegrity, ReadBeyondBufferWStr) {
53 //This was BUG 984408.
54 uint32 v1 = kuint32max - 1;
55 int v2 = 777;
56 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
57 EXPECT_TRUE(m.WriteInt(v1));
58 EXPECT_TRUE(m.WriteInt(v2));
59
[email protected]ce208f872012-03-07 20:42:5660 PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2961 std::wstring vs;
62 EXPECT_FALSE(m.ReadWString(&iter, &vs));
63}
64
65TEST(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));
70
[email protected]ce208f872012-03-07 20:42:5671 PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2972 const char* data = NULL;
[email protected]26d2f472010-03-30 23:52:2473 EXPECT_TRUE(m.ReadBytes(&iter, &data, sizeof(int)));
initial.commit09911bf2008-07-26 23:55:2974}
75
76TEST(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));
85
86 std::vector<double> vec;
[email protected]ce208f872012-03-07 20:42:5687 PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2988 EXPECT_FALSE(ReadParam(&m, &iter, &vec));
89}
90
91TEST(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));
98
99 std::vector<int64> vec;
[email protected]ce208f872012-03-07 20:42:56100 PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:29101 EXPECT_FALSE(ReadParam(&m, &iter, &vec));
102}
103
104TEST(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));
112
113 std::vector<int64> vec;
[email protected]ce208f872012-03-07 20:42:56114 PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:29115 EXPECT_FALSE(ReadParam(&m, &iter, &vec));
116}
117
[email protected]57319ce2012-06-11 22:35:26118class SimpleListener : public IPC::Listener {
initial.commit09911bf2008-07-26 23:55:29119 public:
120 SimpleListener() : other_(NULL) {
121 }
[email protected]57319ce2012-06-11 22:35:26122 void Init(IPC::Sender* s) {
initial.commit09911bf2008-07-26 23:55:29123 other_ = s;
124 }
125 protected:
[email protected]57319ce2012-06-11 22:35:26126 IPC::Sender* other_;
initial.commit09911bf2008-07-26 23:55:29127};
128
129enum {
130 FUZZER_ROUTING_ID = 5
131};
132
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.
136class FuzzerServerListener : public SimpleListener {
137 public:
138 FuzzerServerListener() : message_count_(2), pending_messages_(0) {
139 }
[email protected]a95986a82010-12-24 06:19:28140 virtual bool OnMessageReceived(const IPC::Message& msg) {
initial.commit09911bf2008-07-26 23:55:29141 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
142 ++pending_messages_;
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());
150 }
151 }
[email protected]a95986a82010-12-24 06:19:28152 return true;
initial.commit09911bf2008-07-26 23:55:29153 }
154
155 private:
156 void OnMsgClassISMessage(int value, const std::wstring& text) {
157 UseData(MsgClassIS::ID, value, text);
158 RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassIS::ID, value);
159 Cleanup();
160 }
161
162 void OnMsgClassSIMessage(const std::wstring& text, int value) {
163 UseData(MsgClassSI::ID, value, text);
164 RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassSI::ID, value);
165 Cleanup();
166 }
167
[email protected]7ee1a44c2010-07-23 14:18:59168 bool RoundtripAckReply(int routing, uint32 type_id, int reply) {
initial.commit09911bf2008-07-26 23:55:29169 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);
174 }
175
176 void Cleanup() {
177 --message_count_;
178 --pending_messages_;
179 if (0 == message_count_)
180 MessageLoop::current()->Quit();
181 }
182
[email protected]7ee1a44c2010-07-23 14:18:59183 void ReplyMsgNotHandled(uint32 type_id) {
[email protected]1d4ecf42011-08-26 21:27:30184 RoundtripAckReply(FUZZER_ROUTING_ID, MsgUnhandled::ID, type_id);
initial.commit09911bf2008-07-26 23:55:29185 Cleanup();
186 }
187
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();
[email protected]d4651ff2008-12-02 16:51:58192 LOG(WARNING) << output.c_str();
initial.commit09911bf2008-07-26 23:55:29193 };
194
195 int message_count_;
196 int pending_messages_;
197};
198
199class FuzzerClientListener : public SimpleListener {
200 public:
201 FuzzerClientListener() : last_msg_(NULL) {
202 }
203
[email protected]a95986a82010-12-24 06:19:28204 virtual bool OnMessageReceived(const IPC::Message& msg) {
initial.commit09911bf2008-07-26 23:55:29205 last_msg_ = new IPC::Message(msg);
206 MessageLoop::current()->Quit();
[email protected]a95986a82010-12-24 06:19:28207 return true;
initial.commit09911bf2008-07-26 23:55:29208 }
209
[email protected]7ee1a44c2010-07-23 14:18:59210 bool ExpectMessage(int value, uint32 type_id) {
initial.commit09911bf2008-07-26 23:55:29211 if (!MsgHandlerInternal(type_id))
212 return false;
213 int msg_value1 = 0;
214 int msg_value2 = 0;
[email protected]ce208f872012-03-07 20:42:56215 PickleIterator iter(*last_msg_);
initial.commit09911bf2008-07-26 23:55:29216 if (!last_msg_->ReadInt(&iter, &msg_value1))
217 return false;
218 if (!last_msg_->ReadInt(&iter, &msg_value2))
219 return false;
220 if ((msg_value2 + 1) != msg_value1)
221 return false;
222 if (msg_value2 != value)
223 return false;
224
225 delete last_msg_;
226 last_msg_ = NULL;
227 return true;
228 }
229
[email protected]7ee1a44c2010-07-23 14:18:59230 bool ExpectMsgNotHandled(uint32 type_id) {
[email protected]1d4ecf42011-08-26 21:27:30231 return ExpectMessage(type_id, MsgUnhandled::ID);
initial.commit09911bf2008-07-26 23:55:29232 }
233
234 private:
[email protected]7ee1a44c2010-07-23 14:18:59235 bool MsgHandlerInternal(uint32 type_id) {
initial.commit09911bf2008-07-26 23:55:29236 MessageLoop::current()->Run();
237 if (NULL == last_msg_)
238 return false;
239 if (FUZZER_ROUTING_ID != last_msg_->routing_id())
240 return false;
241 return (type_id == last_msg_->type());
242 };
243
244 IPC::Message* last_msg_;
245};
246
[email protected]95cb7fb92008-12-09 22:00:47247// Runs the fuzzing server child mode. Returns when the preset number
248// of messages have been received.
249MULTIPROCESS_TEST_MAIN(RunFuzzServer) {
250 MessageLoopForIO main_message_loop;
initial.commit09911bf2008-07-26 23:55:29251 FuzzerServerListener listener;
[email protected]df3c1ca12008-12-19 21:37:01252 IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_CLIENT, &listener);
[email protected]39703fb2010-10-19 19:11:15253 CHECK(chan.Connect());
initial.commit09911bf2008-07-26 23:55:29254 listener.Init(&chan);
255 MessageLoop::current()->Run();
[email protected]95cb7fb92008-12-09 22:00:47256 return 0;
initial.commit09911bf2008-07-26 23:55:29257}
258
[email protected]95cb7fb92008-12-09 22:00:47259class IPCFuzzingTest : public IPCChannelTest {
260};
261
initial.commit09911bf2008-07-26 23:55:29262// This test makes sure that the FuzzerClientListener and FuzzerServerListener
263// are working properly by generating two well formed IPC calls.
[email protected]95cb7fb92008-12-09 22:00:47264TEST_F(IPCFuzzingTest, SanityTest) {
[email protected]df3c1ca12008-12-19 21:37:01265 FuzzerClientListener listener;
266 IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_SERVER,
267 &listener);
268 base::ProcessHandle server_process = SpawnChild(FUZZER_SERVER, &chan);
initial.commit09911bf2008-07-26 23:55:29269 ASSERT_TRUE(server_process);
[email protected]b5393332012-01-13 00:11:01270 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1));
initial.commit09911bf2008-07-26 23:55:29271 ASSERT_TRUE(chan.Connect());
272 listener.Init(&chan);
273
274 IPC::Message* msg = NULL;
275 int value = 43;
276 msg = new MsgClassIS(value, L"expect 43");
277 chan.Send(msg);
278 EXPECT_TRUE(listener.ExpectMessage(value, MsgClassIS::ID));
279
280 msg = new MsgClassSI(L"expect 44", ++value);
281 chan.Send(msg);
282 EXPECT_TRUE(listener.ExpectMessage(value, MsgClassSI::ID));
283
[email protected]d4651ff2008-12-02 16:51:58284 EXPECT_TRUE(base::WaitForSingleProcess(server_process, 5000));
[email protected]cd4fd152009-02-09 19:28:41285 base::CloseProcessHandle(server_process);
initial.commit09911bf2008-07-26 23:55:29286}
287
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
292// properly.
[email protected]20960e072011-09-20 20:59:01293#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
[email protected]95cb7fb92008-12-09 22:00:47294TEST_F(IPCFuzzingTest, MsgBadPayloadShort) {
[email protected]df3c1ca12008-12-19 21:37:01295 FuzzerClientListener listener;
296 IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_SERVER,
297 &listener);
298 base::ProcessHandle server_process = SpawnChild(FUZZER_SERVER, &chan);
initial.commit09911bf2008-07-26 23:55:29299 ASSERT_TRUE(server_process);
[email protected]b5393332012-01-13 00:11:01300 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1));
initial.commit09911bf2008-07-26 23:55:29301 ASSERT_TRUE(chan.Connect());
302 listener.Init(&chan);
303
304 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassIS::ID,
305 IPC::Message::PRIORITY_NORMAL);
306 msg->WriteInt(666);
307 chan.Send(msg);
308 EXPECT_TRUE(listener.ExpectMsgNotHandled(MsgClassIS::ID));
309
310 msg = new MsgClassSI(L"expect one", 1);
311 chan.Send(msg);
312 EXPECT_TRUE(listener.ExpectMessage(1, MsgClassSI::ID));
313
[email protected]a1b399f2008-12-10 17:16:22314 EXPECT_TRUE(base::WaitForSingleProcess(server_process, 5000));
[email protected]cd4fd152009-02-09 19:28:41315 base::CloseProcessHandle(server_process);
initial.commit09911bf2008-07-26 23:55:29316}
[email protected]20960e072011-09-20 20:59:01317#endif
initial.commit09911bf2008-07-26 23:55:29318
[email protected]95cb7fb92008-12-09 22:00:47319// This test uses a payload that has too many arguments, but so the payload
initial.commit09911bf2008-07-26 23:55:29320// 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.
[email protected]95cb7fb92008-12-09 22:00:47324TEST_F(IPCFuzzingTest, MsgBadPayloadArgs) {
[email protected]df3c1ca12008-12-19 21:37:01325 FuzzerClientListener listener;
326 IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_SERVER,
327 &listener);
328 base::ProcessHandle server_process = SpawnChild(FUZZER_SERVER, &chan);
initial.commit09911bf2008-07-26 23:55:29329 ASSERT_TRUE(server_process);
[email protected]b5393332012-01-13 00:11:01330 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1));
initial.commit09911bf2008-07-26 23:55:29331 ASSERT_TRUE(chan.Connect());
332 listener.Init(&chan);
333
334 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassSI::ID,
335 IPC::Message::PRIORITY_NORMAL);
[email protected]95cb7fb92008-12-09 22:00:47336 msg->WriteWString(L"d");
initial.commit09911bf2008-07-26 23:55:29337 msg->WriteInt(0);
[email protected]95cb7fb92008-12-09 22:00:47338 msg->WriteInt(0x65); // Extra argument.
339
initial.commit09911bf2008-07-26 23:55:29340 chan.Send(msg);
341 EXPECT_TRUE(listener.ExpectMessage(0, MsgClassSI::ID));
342
[email protected]95cb7fb92008-12-09 22:00:47343 // Now send a well formed message to make sure the receiver wasn't
344 // thrown out of sync by the extra argument.
initial.commit09911bf2008-07-26 23:55:29345 msg = new MsgClassIS(3, L"expect three");
346 chan.Send(msg);
347 EXPECT_TRUE(listener.ExpectMessage(3, MsgClassIS::ID));
348
[email protected]d4651ff2008-12-02 16:51:58349 EXPECT_TRUE(base::WaitForSingleProcess(server_process, 5000));
[email protected]cd4fd152009-02-09 19:28:41350 base::CloseProcessHandle(server_process);
initial.commit09911bf2008-07-26 23:55:29351}
352
353// This class is for testing the IPC_BEGIN_MESSAGE_MAP_EX macros.
354class ServerMacroExTest {
355 public:
356 ServerMacroExTest() : unhandled_msgs_(0) {
357 }
[email protected]6fa21d02011-11-04 00:14:16358
[email protected]695092f2010-08-02 16:34:16359 virtual ~ServerMacroExTest() {
360 }
[email protected]6fa21d02011-11-04 00:14:16361
initial.commit09911bf2008-07-26 23:55:29362 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()
369 return msg_is_ok;
370 }
371
372 int unhandled_msgs() const {
373 return unhandled_msgs_;
374 }
375
376 private:
377 void OnMsgClassISMessage(int value, const std::wstring& text) {
378 }
379 void OnMsgClassSIMessage(const std::wstring& text, int value) {
380 }
381
382 int unhandled_msgs_;
[email protected]6fa21d02011-11-04 00:14:16383
384 DISALLOW_COPY_AND_ASSIGN(ServerMacroExTest);
initial.commit09911bf2008-07-26 23:55:29385};
386
[email protected]95cb7fb92008-12-09 22:00:47387TEST_F(IPCFuzzingTest, MsgMapExMacro) {
initial.commit09911bf2008-07-26 23:55:29388 IPC::Message* msg = NULL;
389 ServerMacroExTest server;
390
391 // Test the regular messages.
392 msg = new MsgClassIS(3, L"text3");
393 EXPECT_TRUE(server.OnMessageReceived(*msg));
394 delete msg;
395 msg = new MsgClassSI(L"text2", 2);
396 EXPECT_TRUE(server.OnMessageReceived(*msg));
397 delete msg;
398
[email protected]20960e072011-09-20 20:59:01399#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
initial.commit09911bf2008-07-26 23:55:29400 // Test a bad message.
401 msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassSI::ID,
402 IPC::Message::PRIORITY_NORMAL);
403 msg->WriteInt(2);
404 EXPECT_FALSE(server.OnMessageReceived(*msg));
405 delete msg;
406
407 msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassIS::ID,
408 IPC::Message::PRIORITY_NORMAL);
409 msg->WriteInt(0x64);
410 msg->WriteInt(0x32);
411 EXPECT_FALSE(server.OnMessageReceived(*msg));
412 delete msg;
413
414 EXPECT_EQ(0, server.unhandled_msgs());
415#endif
416}