blob: 32fe71caf37db46b75faa3164a84f114a7c5fe89 [file] [log] [blame]
tedvessenes@gmail.com30dbbaa2012-01-13 09:11:01 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
agl@chromium.org1c6dcf22009-07-23 08:57:21 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <stdio.h>
agl@chromium.org1c6dcf22009-07-23 08:57:21 +09006#include <string>
7#include <sstream>
8
9#include "base/message_loop.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090010#include "base/process_util.h"
brettw@google.com7c5cc672011-01-01 11:17:08 +090011#include "base/threading/platform_thread.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090012#include "ipc/ipc_channel.h"
13#include "ipc/ipc_channel_proxy.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090014#include "ipc/ipc_tests.h"
15#include "testing/gtest/include/gtest/gtest.h"
16#include "testing/multiprocess_func_list.h"
17
darin@chromium.orgb4587d52011-08-27 06:27:30 +090018// 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
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090039TEST(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
jbates@chromium.org0fc87362012-03-08 05:42:56 +090047 PickleIterator iter(m);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090048 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
jbates@chromium.org0fc87362012-03-08 05:42:56 +090060 PickleIterator iter(m);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090061 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
jbates@chromium.org0fc87362012-03-08 05:42:56 +090071 PickleIterator iter(m);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090072 const char* data = NULL;
mpcomplete@chromium.org0409ecb2010-03-31 08:52:24 +090073 EXPECT_TRUE(m.ReadBytes(&iter, &data, sizeof(int)));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090074}
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;
jbates@chromium.org0fc87362012-03-08 05:42:56 +090087 PickleIterator iter(m);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090088 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;
jbates@chromium.org0fc87362012-03-08 05:42:56 +0900100 PickleIterator iter(m);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900101 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;
jbates@chromium.org0fc87362012-03-08 05:42:56 +0900114 PickleIterator iter(m);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900115 EXPECT_FALSE(ReadParam(&m, &iter, &vec));
116}
117
brettw@chromium.orgf947ed02012-06-12 07:35:26 +0900118class SimpleListener : public IPC::Listener {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900119 public:
120 SimpleListener() : other_(NULL) {
121 }
brettw@chromium.orgf947ed02012-06-12 07:35:26 +0900122 void Init(IPC::Sender* s) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900123 other_ = s;
124 }
125 protected:
brettw@chromium.orgf947ed02012-06-12 07:35:26 +0900126 IPC::Sender* other_;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900127};
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 }
jam@chromium.org8a2c7842010-12-24 15:19:28 +0900140 virtual bool OnMessageReceived(const IPC::Message& msg) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900141 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 }
jam@chromium.org8a2c7842010-12-24 15:19:28 +0900152 return true;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900153 }
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
thomasvl@google.com9a242072010-07-23 23:18:59 +0900168 bool RoundtripAckReply(int routing, uint32 type_id, int reply) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900169 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
thomasvl@google.com9a242072010-07-23 23:18:59 +0900183 void ReplyMsgNotHandled(uint32 type_id) {
darin@chromium.orgb4587d52011-08-27 06:27:30 +0900184 RoundtripAckReply(FUZZER_ROUTING_ID, MsgUnhandled::ID, type_id);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900185 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();
192 LOG(WARNING) << output.c_str();
193 };
194
195 int message_count_;
196 int pending_messages_;
197};
198
199class FuzzerClientListener : public SimpleListener {
200 public:
201 FuzzerClientListener() : last_msg_(NULL) {
202 }
203
jam@chromium.org8a2c7842010-12-24 15:19:28 +0900204 virtual bool OnMessageReceived(const IPC::Message& msg) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900205 last_msg_ = new IPC::Message(msg);
206 MessageLoop::current()->Quit();
jam@chromium.org8a2c7842010-12-24 15:19:28 +0900207 return true;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900208 }
209
thomasvl@google.com9a242072010-07-23 23:18:59 +0900210 bool ExpectMessage(int value, uint32 type_id) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900211 if (!MsgHandlerInternal(type_id))
212 return false;
213 int msg_value1 = 0;
214 int msg_value2 = 0;
jbates@chromium.org0fc87362012-03-08 05:42:56 +0900215 PickleIterator iter(*last_msg_);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900216 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
thomasvl@google.com9a242072010-07-23 23:18:59 +0900230 bool ExpectMsgNotHandled(uint32 type_id) {
darin@chromium.orgb4587d52011-08-27 06:27:30 +0900231 return ExpectMessage(type_id, MsgUnhandled::ID);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900232 }
233
234 private:
thomasvl@google.com9a242072010-07-23 23:18:59 +0900235 bool MsgHandlerInternal(uint32 type_id) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900236 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
247// 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;
251 FuzzerServerListener listener;
252 IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_CLIENT, &listener);
evan@chromium.org1f3e46b2010-10-20 04:11:15 +0900253 CHECK(chan.Connect());
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900254 listener.Init(&chan);
255 MessageLoop::current()->Run();
256 return 0;
257}
258
259class IPCFuzzingTest : public IPCChannelTest {
260};
261
262// This test makes sure that the FuzzerClientListener and FuzzerServerListener
263// are working properly by generating two well formed IPC calls.
264TEST_F(IPCFuzzingTest, SanityTest) {
265 FuzzerClientListener listener;
266 IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_SERVER,
267 &listener);
268 base::ProcessHandle server_process = SpawnChild(FUZZER_SERVER, &chan);
269 ASSERT_TRUE(server_process);
tedvessenes@gmail.com30dbbaa2012-01-13 09:11:01 +0900270 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900271 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
284 EXPECT_TRUE(base::WaitForSingleProcess(server_process, 5000));
285 base::CloseProcessHandle(server_process);
286}
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.
nsylvain@chromium.org675aad32011-09-21 05:59:01 +0900293#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900294TEST_F(IPCFuzzingTest, MsgBadPayloadShort) {
295 FuzzerClientListener listener;
296 IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_SERVER,
297 &listener);
298 base::ProcessHandle server_process = SpawnChild(FUZZER_SERVER, &chan);
299 ASSERT_TRUE(server_process);
tedvessenes@gmail.com30dbbaa2012-01-13 09:11:01 +0900300 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900301 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
314 EXPECT_TRUE(base::WaitForSingleProcess(server_process, 5000));
315 base::CloseProcessHandle(server_process);
316}
nsylvain@chromium.org675aad32011-09-21 05:59:01 +0900317#endif
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900318
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.
324TEST_F(IPCFuzzingTest, MsgBadPayloadArgs) {
325 FuzzerClientListener listener;
326 IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_SERVER,
327 &listener);
328 base::ProcessHandle server_process = SpawnChild(FUZZER_SERVER, &chan);
329 ASSERT_TRUE(server_process);
tedvessenes@gmail.com30dbbaa2012-01-13 09:11:01 +0900330 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900331 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);
336 msg->WriteWString(L"d");
337 msg->WriteInt(0);
338 msg->WriteInt(0x65); // Extra argument.
339
340 chan.Send(msg);
341 EXPECT_TRUE(listener.ExpectMessage(0, MsgClassSI::ID));
342
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");
346 chan.Send(msg);
347 EXPECT_TRUE(listener.ExpectMessage(3, MsgClassIS::ID));
348
349 EXPECT_TRUE(base::WaitForSingleProcess(server_process, 5000));
350 base::CloseProcessHandle(server_process);
351}
352
353// This class is for testing the IPC_BEGIN_MESSAGE_MAP_EX macros.
354class ServerMacroExTest {
355 public:
356 ServerMacroExTest() : unhandled_msgs_(0) {
357 }
jrg@chromium.org2eb192e2011-11-04 09:14:16 +0900358
ziadh@chromium.org7ebaeea2010-08-03 01:34:16 +0900359 virtual ~ServerMacroExTest() {
360 }
jrg@chromium.org2eb192e2011-11-04 09:14:16 +0900361
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900362 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_;
jrg@chromium.org2eb192e2011-11-04 09:14:16 +0900383
384 DISALLOW_COPY_AND_ASSIGN(ServerMacroExTest);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900385};
386
387TEST_F(IPCFuzzingTest, MsgMapExMacro) {
388 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
nsylvain@chromium.org675aad32011-09-21 05:59:01 +0900399#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900400 // 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}