blob: f5014491f804d06721d648630d1a903ecc765ee0 [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]97c652b2012-06-27 01:12:2414#include "ipc/ipc_multiprocess_test.h"
[email protected]0cb7d8c82013-01-11 15:13:3715#include "ipc/ipc_test_base.h"
initial.commit09911bf2008-07-26 23:55:2916#include "testing/gtest/include/gtest/gtest.h"
[email protected]95cb7fb92008-12-09 22:00:4717#include "testing/multiprocess_func_list.h"
initial.commit09911bf2008-07-26 23:55:2918
[email protected]2a3aa7b52013-01-11 20:56:2219// IPC messages for testing ----------------------------------------------------
[email protected]1d4ecf42011-08-26 21:27:3020
21#define IPC_MESSAGE_IMPL
22#include "ipc/ipc_message_macros.h"
23
24#define IPC_MESSAGE_START TestMsgStart
25
26// Generic message class that is an int followed by a wstring.
27IPC_MESSAGE_CONTROL2(MsgClassIS, int, std::wstring)
28
29// Generic message class that is a wstring followed by an int.
30IPC_MESSAGE_CONTROL2(MsgClassSI, std::wstring, int)
31
32// Message to create a mutex in the IPC server, using the received name.
33IPC_MESSAGE_CONTROL2(MsgDoMutex, std::wstring, int)
34
35// Used to generate an ID for a message that should not exist.
36IPC_MESSAGE_CONTROL0(MsgUnhandled)
37
[email protected]2a3aa7b52013-01-11 20:56:2238// -----------------------------------------------------------------------------
39
40namespace {
[email protected]1d4ecf42011-08-26 21:27:3041
initial.commit09911bf2008-07-26 23:55:2942TEST(IPCMessageIntegrity, ReadBeyondBufferStr) {
43 //This was BUG 984408.
44 uint32 v1 = kuint32max - 1;
45 int v2 = 666;
46 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
47 EXPECT_TRUE(m.WriteInt(v1));
48 EXPECT_TRUE(m.WriteInt(v2));
49
[email protected]ce208f872012-03-07 20:42:5650 PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2951 std::string vs;
52 EXPECT_FALSE(m.ReadString(&iter, &vs));
53}
54
55TEST(IPCMessageIntegrity, ReadBeyondBufferWStr) {
56 //This was BUG 984408.
57 uint32 v1 = kuint32max - 1;
58 int v2 = 777;
59 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
60 EXPECT_TRUE(m.WriteInt(v1));
61 EXPECT_TRUE(m.WriteInt(v2));
62
[email protected]ce208f872012-03-07 20:42:5663 PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2964 std::wstring vs;
65 EXPECT_FALSE(m.ReadWString(&iter, &vs));
66}
67
68TEST(IPCMessageIntegrity, ReadBytesBadIterator) {
69 // This was BUG 1035467.
70 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
71 EXPECT_TRUE(m.WriteInt(1));
72 EXPECT_TRUE(m.WriteInt(2));
73
[email protected]ce208f872012-03-07 20:42:5674 PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2975 const char* data = NULL;
[email protected]26d2f472010-03-30 23:52:2476 EXPECT_TRUE(m.ReadBytes(&iter, &data, sizeof(int)));
initial.commit09911bf2008-07-26 23:55:2977}
78
79TEST(IPCMessageIntegrity, ReadVectorNegativeSize) {
80 // A slight variation of BUG 984408. Note that the pickling of vector<char>
81 // has a specialized template which is not vulnerable to this bug. So here
82 // try to hit the non-specialized case vector<P>.
83 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
84 EXPECT_TRUE(m.WriteInt(-1)); // This is the count of elements.
85 EXPECT_TRUE(m.WriteInt(1));
86 EXPECT_TRUE(m.WriteInt(2));
87 EXPECT_TRUE(m.WriteInt(3));
88
89 std::vector<double> vec;
[email protected]ce208f872012-03-07 20:42:5690 PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2991 EXPECT_FALSE(ReadParam(&m, &iter, &vec));
92}
93
94TEST(IPCMessageIntegrity, ReadVectorTooLarge1) {
95 // This was BUG 1006367. This is the large but positive length case. Again
96 // we try to hit the non-specialized case vector<P>.
97 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
98 EXPECT_TRUE(m.WriteInt(0x21000003)); // This is the count of elements.
99 EXPECT_TRUE(m.WriteInt64(1));
100 EXPECT_TRUE(m.WriteInt64(2));
101
102 std::vector<int64> vec;
[email protected]ce208f872012-03-07 20:42:56103 PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:29104 EXPECT_FALSE(ReadParam(&m, &iter, &vec));
105}
106
107TEST(IPCMessageIntegrity, ReadVectorTooLarge2) {
108 // This was BUG 1006367. This is the large but positive with an additional
109 // integer overflow when computing the actual byte size. Again we try to hit
110 // the non-specialized case vector<P>.
111 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
112 EXPECT_TRUE(m.WriteInt(0x71000000)); // This is the count of elements.
113 EXPECT_TRUE(m.WriteInt64(1));
114 EXPECT_TRUE(m.WriteInt64(2));
115
116 std::vector<int64> vec;
[email protected]ce208f872012-03-07 20:42:56117 PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:29118 EXPECT_FALSE(ReadParam(&m, &iter, &vec));
119}
120
[email protected]57319ce2012-06-11 22:35:26121class SimpleListener : public IPC::Listener {
initial.commit09911bf2008-07-26 23:55:29122 public:
123 SimpleListener() : other_(NULL) {
124 }
[email protected]57319ce2012-06-11 22:35:26125 void Init(IPC::Sender* s) {
initial.commit09911bf2008-07-26 23:55:29126 other_ = s;
127 }
128 protected:
[email protected]57319ce2012-06-11 22:35:26129 IPC::Sender* other_;
initial.commit09911bf2008-07-26 23:55:29130};
131
132enum {
133 FUZZER_ROUTING_ID = 5
134};
135
136// The fuzzer server class. It runs in a child process and expects
137// only two IPC calls; after that it exits the message loop which
138// terminates the child process.
139class FuzzerServerListener : public SimpleListener {
140 public:
141 FuzzerServerListener() : message_count_(2), pending_messages_(0) {
142 }
[email protected]a95986a82010-12-24 06:19:28143 virtual bool OnMessageReceived(const IPC::Message& msg) {
initial.commit09911bf2008-07-26 23:55:29144 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
145 ++pending_messages_;
146 IPC_BEGIN_MESSAGE_MAP(FuzzerServerListener, msg)
147 IPC_MESSAGE_HANDLER(MsgClassIS, OnMsgClassISMessage)
148 IPC_MESSAGE_HANDLER(MsgClassSI, OnMsgClassSIMessage)
149 IPC_END_MESSAGE_MAP()
150 if (pending_messages_) {
151 // Probably a problem de-serializing the message.
152 ReplyMsgNotHandled(msg.type());
153 }
154 }
[email protected]a95986a82010-12-24 06:19:28155 return true;
initial.commit09911bf2008-07-26 23:55:29156 }
157
158 private:
159 void OnMsgClassISMessage(int value, const std::wstring& text) {
160 UseData(MsgClassIS::ID, value, text);
161 RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassIS::ID, value);
162 Cleanup();
163 }
164
165 void OnMsgClassSIMessage(const std::wstring& text, int value) {
166 UseData(MsgClassSI::ID, value, text);
167 RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassSI::ID, value);
168 Cleanup();
169 }
170
[email protected]7ee1a44c2010-07-23 14:18:59171 bool RoundtripAckReply(int routing, uint32 type_id, int reply) {
initial.commit09911bf2008-07-26 23:55:29172 IPC::Message* message = new IPC::Message(routing, type_id,
173 IPC::Message::PRIORITY_NORMAL);
174 message->WriteInt(reply + 1);
175 message->WriteInt(reply);
176 return other_->Send(message);
177 }
178
179 void Cleanup() {
180 --message_count_;
181 --pending_messages_;
182 if (0 == message_count_)
183 MessageLoop::current()->Quit();
184 }
185
[email protected]7ee1a44c2010-07-23 14:18:59186 void ReplyMsgNotHandled(uint32 type_id) {
[email protected]1d4ecf42011-08-26 21:27:30187 RoundtripAckReply(FUZZER_ROUTING_ID, MsgUnhandled::ID, type_id);
initial.commit09911bf2008-07-26 23:55:29188 Cleanup();
189 }
190
191 void UseData(int caller, int value, const std::wstring& text) {
192 std::wostringstream wos;
193 wos << L"IPC fuzzer:" << caller << " [" << value << L" " << text << L"]\n";
194 std::wstring output = wos.str();
[email protected]d4651ff2008-12-02 16:51:58195 LOG(WARNING) << output.c_str();
initial.commit09911bf2008-07-26 23:55:29196 };
197
198 int message_count_;
199 int pending_messages_;
200};
201
202class FuzzerClientListener : public SimpleListener {
203 public:
204 FuzzerClientListener() : last_msg_(NULL) {
205 }
206
[email protected]a95986a82010-12-24 06:19:28207 virtual bool OnMessageReceived(const IPC::Message& msg) {
initial.commit09911bf2008-07-26 23:55:29208 last_msg_ = new IPC::Message(msg);
209 MessageLoop::current()->Quit();
[email protected]a95986a82010-12-24 06:19:28210 return true;
initial.commit09911bf2008-07-26 23:55:29211 }
212
[email protected]7ee1a44c2010-07-23 14:18:59213 bool ExpectMessage(int value, uint32 type_id) {
initial.commit09911bf2008-07-26 23:55:29214 if (!MsgHandlerInternal(type_id))
215 return false;
216 int msg_value1 = 0;
217 int msg_value2 = 0;
[email protected]ce208f872012-03-07 20:42:56218 PickleIterator iter(*last_msg_);
initial.commit09911bf2008-07-26 23:55:29219 if (!last_msg_->ReadInt(&iter, &msg_value1))
220 return false;
221 if (!last_msg_->ReadInt(&iter, &msg_value2))
222 return false;
223 if ((msg_value2 + 1) != msg_value1)
224 return false;
225 if (msg_value2 != value)
226 return false;
227
228 delete last_msg_;
229 last_msg_ = NULL;
230 return true;
231 }
232
[email protected]7ee1a44c2010-07-23 14:18:59233 bool ExpectMsgNotHandled(uint32 type_id) {
[email protected]1d4ecf42011-08-26 21:27:30234 return ExpectMessage(type_id, MsgUnhandled::ID);
initial.commit09911bf2008-07-26 23:55:29235 }
236
237 private:
[email protected]7ee1a44c2010-07-23 14:18:59238 bool MsgHandlerInternal(uint32 type_id) {
initial.commit09911bf2008-07-26 23:55:29239 MessageLoop::current()->Run();
240 if (NULL == last_msg_)
241 return false;
242 if (FUZZER_ROUTING_ID != last_msg_->routing_id())
243 return false;
244 return (type_id == last_msg_->type());
245 };
246
247 IPC::Message* last_msg_;
248};
249
[email protected]95cb7fb92008-12-09 22:00:47250// Runs the fuzzing server child mode. Returns when the preset number
251// of messages have been received.
[email protected]97c652b2012-06-27 01:12:24252MULTIPROCESS_IPC_TEST_MAIN(RunFuzzServer) {
[email protected]95cb7fb92008-12-09 22:00:47253 MessageLoopForIO main_message_loop;
initial.commit09911bf2008-07-26 23:55:29254 FuzzerServerListener listener;
[email protected]df3c1ca12008-12-19 21:37:01255 IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_CLIENT, &listener);
[email protected]39703fb2010-10-19 19:11:15256 CHECK(chan.Connect());
initial.commit09911bf2008-07-26 23:55:29257 listener.Init(&chan);
258 MessageLoop::current()->Run();
[email protected]95cb7fb92008-12-09 22:00:47259 return 0;
initial.commit09911bf2008-07-26 23:55:29260}
261
[email protected]0cb7d8c82013-01-11 15:13:37262class IPCFuzzingTest : public IPCTestBase {
[email protected]95cb7fb92008-12-09 22:00:47263};
264
initial.commit09911bf2008-07-26 23:55:29265// This test makes sure that the FuzzerClientListener and FuzzerServerListener
266// are working properly by generating two well formed IPC calls.
[email protected]95cb7fb92008-12-09 22:00:47267TEST_F(IPCFuzzingTest, SanityTest) {
[email protected]df3c1ca12008-12-19 21:37:01268 FuzzerClientListener listener;
269 IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_SERVER,
270 &listener);
271 base::ProcessHandle server_process = SpawnChild(FUZZER_SERVER, &chan);
initial.commit09911bf2008-07-26 23:55:29272 ASSERT_TRUE(server_process);
[email protected]b5393332012-01-13 00:11:01273 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1));
initial.commit09911bf2008-07-26 23:55:29274 ASSERT_TRUE(chan.Connect());
275 listener.Init(&chan);
276
277 IPC::Message* msg = NULL;
278 int value = 43;
279 msg = new MsgClassIS(value, L"expect 43");
280 chan.Send(msg);
281 EXPECT_TRUE(listener.ExpectMessage(value, MsgClassIS::ID));
282
283 msg = new MsgClassSI(L"expect 44", ++value);
284 chan.Send(msg);
285 EXPECT_TRUE(listener.ExpectMessage(value, MsgClassSI::ID));
286
[email protected]0f2e45ec2012-07-11 15:41:43287 EXPECT_TRUE(base::WaitForSingleProcess(
288 server_process, base::TimeDelta::FromSeconds(5)));
[email protected]cd4fd152009-02-09 19:28:41289 base::CloseProcessHandle(server_process);
initial.commit09911bf2008-07-26 23:55:29290}
291
292// This test uses a payload that is smaller than expected.
293// This generates an error while unpacking the IPC buffer which in
294// In debug this triggers an assertion and in release it is ignored(!!). Right
295// after we generate another valid IPC to make sure framing is working
296// properly.
[email protected]20960e072011-09-20 20:59:01297#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
[email protected]95cb7fb92008-12-09 22:00:47298TEST_F(IPCFuzzingTest, MsgBadPayloadShort) {
[email protected]df3c1ca12008-12-19 21:37:01299 FuzzerClientListener listener;
300 IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_SERVER,
301 &listener);
302 base::ProcessHandle server_process = SpawnChild(FUZZER_SERVER, &chan);
initial.commit09911bf2008-07-26 23:55:29303 ASSERT_TRUE(server_process);
[email protected]b5393332012-01-13 00:11:01304 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1));
initial.commit09911bf2008-07-26 23:55:29305 ASSERT_TRUE(chan.Connect());
306 listener.Init(&chan);
307
308 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassIS::ID,
309 IPC::Message::PRIORITY_NORMAL);
310 msg->WriteInt(666);
311 chan.Send(msg);
312 EXPECT_TRUE(listener.ExpectMsgNotHandled(MsgClassIS::ID));
313
314 msg = new MsgClassSI(L"expect one", 1);
315 chan.Send(msg);
316 EXPECT_TRUE(listener.ExpectMessage(1, MsgClassSI::ID));
317
[email protected]0f2e45ec2012-07-11 15:41:43318 EXPECT_TRUE(base::WaitForSingleProcess(
319 server_process, base::TimeDelta::FromSeconds(5)));
[email protected]cd4fd152009-02-09 19:28:41320 base::CloseProcessHandle(server_process);
initial.commit09911bf2008-07-26 23:55:29321}
[email protected]20960e072011-09-20 20:59:01322#endif
initial.commit09911bf2008-07-26 23:55:29323
[email protected]95cb7fb92008-12-09 22:00:47324// This test uses a payload that has too many arguments, but so the payload
initial.commit09911bf2008-07-26 23:55:29325// size is big enough so the unpacking routine does not generate an error as
326// in the case of MsgBadPayloadShort test.
327// This test does not pinpoint a flaw (per se) as by design we don't carry
328// type information on the IPC message.
[email protected]95cb7fb92008-12-09 22:00:47329TEST_F(IPCFuzzingTest, MsgBadPayloadArgs) {
[email protected]df3c1ca12008-12-19 21:37:01330 FuzzerClientListener listener;
331 IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_SERVER,
332 &listener);
333 base::ProcessHandle server_process = SpawnChild(FUZZER_SERVER, &chan);
initial.commit09911bf2008-07-26 23:55:29334 ASSERT_TRUE(server_process);
[email protected]b5393332012-01-13 00:11:01335 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1));
initial.commit09911bf2008-07-26 23:55:29336 ASSERT_TRUE(chan.Connect());
337 listener.Init(&chan);
338
339 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassSI::ID,
340 IPC::Message::PRIORITY_NORMAL);
[email protected]95cb7fb92008-12-09 22:00:47341 msg->WriteWString(L"d");
initial.commit09911bf2008-07-26 23:55:29342 msg->WriteInt(0);
[email protected]95cb7fb92008-12-09 22:00:47343 msg->WriteInt(0x65); // Extra argument.
344
initial.commit09911bf2008-07-26 23:55:29345 chan.Send(msg);
346 EXPECT_TRUE(listener.ExpectMessage(0, MsgClassSI::ID));
347
[email protected]95cb7fb92008-12-09 22:00:47348 // Now send a well formed message to make sure the receiver wasn't
349 // thrown out of sync by the extra argument.
initial.commit09911bf2008-07-26 23:55:29350 msg = new MsgClassIS(3, L"expect three");
351 chan.Send(msg);
352 EXPECT_TRUE(listener.ExpectMessage(3, MsgClassIS::ID));
353
[email protected]0f2e45ec2012-07-11 15:41:43354 EXPECT_TRUE(base::WaitForSingleProcess(
355 server_process, base::TimeDelta::FromSeconds(5)));
[email protected]cd4fd152009-02-09 19:28:41356 base::CloseProcessHandle(server_process);
initial.commit09911bf2008-07-26 23:55:29357}
358
359// This class is for testing the IPC_BEGIN_MESSAGE_MAP_EX macros.
360class ServerMacroExTest {
361 public:
362 ServerMacroExTest() : unhandled_msgs_(0) {
363 }
[email protected]6fa21d02011-11-04 00:14:16364
[email protected]695092f2010-08-02 16:34:16365 virtual ~ServerMacroExTest() {
366 }
[email protected]6fa21d02011-11-04 00:14:16367
initial.commit09911bf2008-07-26 23:55:29368 virtual bool OnMessageReceived(const IPC::Message& msg) {
369 bool msg_is_ok = false;
370 IPC_BEGIN_MESSAGE_MAP_EX(ServerMacroExTest, msg, msg_is_ok)
371 IPC_MESSAGE_HANDLER(MsgClassIS, OnMsgClassISMessage)
372 IPC_MESSAGE_HANDLER(MsgClassSI, OnMsgClassSIMessage)
373 IPC_MESSAGE_UNHANDLED(++unhandled_msgs_)
374 IPC_END_MESSAGE_MAP_EX()
375 return msg_is_ok;
376 }
377
378 int unhandled_msgs() const {
379 return unhandled_msgs_;
380 }
381
382 private:
383 void OnMsgClassISMessage(int value, const std::wstring& text) {
384 }
385 void OnMsgClassSIMessage(const std::wstring& text, int value) {
386 }
387
388 int unhandled_msgs_;
[email protected]6fa21d02011-11-04 00:14:16389
390 DISALLOW_COPY_AND_ASSIGN(ServerMacroExTest);
initial.commit09911bf2008-07-26 23:55:29391};
392
[email protected]95cb7fb92008-12-09 22:00:47393TEST_F(IPCFuzzingTest, MsgMapExMacro) {
initial.commit09911bf2008-07-26 23:55:29394 IPC::Message* msg = NULL;
395 ServerMacroExTest server;
396
397 // Test the regular messages.
398 msg = new MsgClassIS(3, L"text3");
399 EXPECT_TRUE(server.OnMessageReceived(*msg));
400 delete msg;
401 msg = new MsgClassSI(L"text2", 2);
402 EXPECT_TRUE(server.OnMessageReceived(*msg));
403 delete msg;
404
[email protected]20960e072011-09-20 20:59:01405#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
initial.commit09911bf2008-07-26 23:55:29406 // Test a bad message.
407 msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassSI::ID,
408 IPC::Message::PRIORITY_NORMAL);
409 msg->WriteInt(2);
410 EXPECT_FALSE(server.OnMessageReceived(*msg));
411 delete msg;
412
413 msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassIS::ID,
414 IPC::Message::PRIORITY_NORMAL);
415 msg->WriteInt(0x64);
416 msg->WriteInt(0x32);
417 EXPECT_FALSE(server.OnMessageReceived(*msg));
418 delete msg;
419
420 EXPECT_EQ(0, server.unhandled_msgs());
421#endif
422}
[email protected]2a3aa7b52013-01-11 20:56:22423
424} // namespace