blob: f9475a1adb0015fdf7f80b0dc5e7dae7487fec86 [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]2a9ec0e2013-07-17 23:00:309#include "base/message_loop/message_loop.h"
[email protected]f214f8792011-01-01 02:17:0810#include "base/threading/platform_thread.h"
[email protected]0cb7d8c82013-01-11 15:13:3711#include "ipc/ipc_test_base.h"
initial.commit09911bf2008-07-26 23:55:2912#include "testing/gtest/include/gtest/gtest.h"
13
[email protected]2a3aa7b52013-01-11 20:56:2214// IPC messages for testing ----------------------------------------------------
[email protected]1d4ecf42011-08-26 21:27:3015
16#define IPC_MESSAGE_IMPL
17#include "ipc/ipc_message_macros.h"
18
19#define IPC_MESSAGE_START TestMsgStart
20
21// Generic message class that is an int followed by a wstring.
22IPC_MESSAGE_CONTROL2(MsgClassIS, int, std::wstring)
23
24// Generic message class that is a wstring followed by an int.
25IPC_MESSAGE_CONTROL2(MsgClassSI, std::wstring, int)
26
27// Message to create a mutex in the IPC server, using the received name.
28IPC_MESSAGE_CONTROL2(MsgDoMutex, std::wstring, int)
29
30// Used to generate an ID for a message that should not exist.
31IPC_MESSAGE_CONTROL0(MsgUnhandled)
32
[email protected]2a3aa7b52013-01-11 20:56:2233// -----------------------------------------------------------------------------
34
35namespace {
[email protected]1d4ecf42011-08-26 21:27:3036
initial.commit09911bf2008-07-26 23:55:2937TEST(IPCMessageIntegrity, ReadBeyondBufferStr) {
38 //This was BUG 984408.
39 uint32 v1 = kuint32max - 1;
40 int v2 = 666;
[email protected]753bb252013-11-04 22:28:1241 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:2942 EXPECT_TRUE(m.WriteInt(v1));
43 EXPECT_TRUE(m.WriteInt(v2));
44
[email protected]ce208f872012-03-07 20:42:5645 PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2946 std::string vs;
47 EXPECT_FALSE(m.ReadString(&iter, &vs));
48}
49
50TEST(IPCMessageIntegrity, ReadBeyondBufferWStr) {
51 //This was BUG 984408.
52 uint32 v1 = kuint32max - 1;
53 int v2 = 777;
[email protected]753bb252013-11-04 22:28:1254 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:2955 EXPECT_TRUE(m.WriteInt(v1));
56 EXPECT_TRUE(m.WriteInt(v2));
57
[email protected]ce208f872012-03-07 20:42:5658 PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2959 std::wstring vs;
60 EXPECT_FALSE(m.ReadWString(&iter, &vs));
61}
62
63TEST(IPCMessageIntegrity, ReadBytesBadIterator) {
64 // This was BUG 1035467.
[email protected]753bb252013-11-04 22:28:1265 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:2966 EXPECT_TRUE(m.WriteInt(1));
67 EXPECT_TRUE(m.WriteInt(2));
68
[email protected]ce208f872012-03-07 20:42:5669 PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2970 const char* data = NULL;
[email protected]26d2f472010-03-30 23:52:2471 EXPECT_TRUE(m.ReadBytes(&iter, &data, sizeof(int)));
initial.commit09911bf2008-07-26 23:55:2972}
73
74TEST(IPCMessageIntegrity, ReadVectorNegativeSize) {
75 // A slight variation of BUG 984408. Note that the pickling of vector<char>
76 // has a specialized template which is not vulnerable to this bug. So here
77 // try to hit the non-specialized case vector<P>.
[email protected]753bb252013-11-04 22:28:1278 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:2979 EXPECT_TRUE(m.WriteInt(-1)); // This is the count of elements.
80 EXPECT_TRUE(m.WriteInt(1));
81 EXPECT_TRUE(m.WriteInt(2));
82 EXPECT_TRUE(m.WriteInt(3));
83
84 std::vector<double> vec;
[email protected]ce208f872012-03-07 20:42:5685 PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2986 EXPECT_FALSE(ReadParam(&m, &iter, &vec));
87}
88
89TEST(IPCMessageIntegrity, ReadVectorTooLarge1) {
90 // This was BUG 1006367. This is the large but positive length case. Again
91 // we try to hit the non-specialized case vector<P>.
[email protected]753bb252013-11-04 22:28:1292 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:2993 EXPECT_TRUE(m.WriteInt(0x21000003)); // This is the count of elements.
94 EXPECT_TRUE(m.WriteInt64(1));
95 EXPECT_TRUE(m.WriteInt64(2));
96
97 std::vector<int64> vec;
[email protected]ce208f872012-03-07 20:42:5698 PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2999 EXPECT_FALSE(ReadParam(&m, &iter, &vec));
100}
101
102TEST(IPCMessageIntegrity, ReadVectorTooLarge2) {
103 // This was BUG 1006367. This is the large but positive with an additional
104 // integer overflow when computing the actual byte size. Again we try to hit
105 // the non-specialized case vector<P>.
[email protected]753bb252013-11-04 22:28:12106 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:29107 EXPECT_TRUE(m.WriteInt(0x71000000)); // This is the count of elements.
108 EXPECT_TRUE(m.WriteInt64(1));
109 EXPECT_TRUE(m.WriteInt64(2));
110
111 std::vector<int64> vec;
[email protected]ce208f872012-03-07 20:42:56112 PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:29113 EXPECT_FALSE(ReadParam(&m, &iter, &vec));
114}
115
[email protected]57319ce2012-06-11 22:35:26116class SimpleListener : public IPC::Listener {
initial.commit09911bf2008-07-26 23:55:29117 public:
118 SimpleListener() : other_(NULL) {
119 }
[email protected]57319ce2012-06-11 22:35:26120 void Init(IPC::Sender* s) {
initial.commit09911bf2008-07-26 23:55:29121 other_ = s;
122 }
123 protected:
[email protected]57319ce2012-06-11 22:35:26124 IPC::Sender* other_;
initial.commit09911bf2008-07-26 23:55:29125};
126
127enum {
128 FUZZER_ROUTING_ID = 5
129};
130
131// The fuzzer server class. It runs in a child process and expects
132// only two IPC calls; after that it exits the message loop which
133// terminates the child process.
134class FuzzerServerListener : public SimpleListener {
135 public:
136 FuzzerServerListener() : message_count_(2), pending_messages_(0) {
137 }
dchengfe61fca2014-10-22 02:29:52138 bool OnMessageReceived(const IPC::Message& msg) override {
initial.commit09911bf2008-07-26 23:55:29139 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
140 ++pending_messages_;
141 IPC_BEGIN_MESSAGE_MAP(FuzzerServerListener, msg)
142 IPC_MESSAGE_HANDLER(MsgClassIS, OnMsgClassISMessage)
143 IPC_MESSAGE_HANDLER(MsgClassSI, OnMsgClassSIMessage)
144 IPC_END_MESSAGE_MAP()
145 if (pending_messages_) {
146 // Probably a problem de-serializing the message.
147 ReplyMsgNotHandled(msg.type());
148 }
149 }
[email protected]a95986a82010-12-24 06:19:28150 return true;
initial.commit09911bf2008-07-26 23:55:29151 }
152
153 private:
154 void OnMsgClassISMessage(int value, const std::wstring& text) {
155 UseData(MsgClassIS::ID, value, text);
156 RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassIS::ID, value);
157 Cleanup();
158 }
159
160 void OnMsgClassSIMessage(const std::wstring& text, int value) {
161 UseData(MsgClassSI::ID, value, text);
162 RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassSI::ID, value);
163 Cleanup();
164 }
165
[email protected]7ee1a44c2010-07-23 14:18:59166 bool RoundtripAckReply(int routing, uint32 type_id, int reply) {
[email protected]753bb252013-11-04 22:28:12167 IPC::Message* message = new IPC::Message(routing, type_id,
168 IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:29169 message->WriteInt(reply + 1);
170 message->WriteInt(reply);
171 return other_->Send(message);
172 }
173
174 void Cleanup() {
175 --message_count_;
176 --pending_messages_;
177 if (0 == message_count_)
[email protected]fd0a773a2013-04-30 20:55:03178 base::MessageLoop::current()->Quit();
initial.commit09911bf2008-07-26 23:55:29179 }
180
[email protected]7ee1a44c2010-07-23 14:18:59181 void ReplyMsgNotHandled(uint32 type_id) {
[email protected]1d4ecf42011-08-26 21:27:30182 RoundtripAckReply(FUZZER_ROUTING_ID, MsgUnhandled::ID, type_id);
initial.commit09911bf2008-07-26 23:55:29183 Cleanup();
184 }
185
186 void UseData(int caller, int value, const std::wstring& text) {
187 std::wostringstream wos;
188 wos << L"IPC fuzzer:" << caller << " [" << value << L" " << text << L"]\n";
189 std::wstring output = wos.str();
[email protected]d4651ff2008-12-02 16:51:58190 LOG(WARNING) << output.c_str();
initial.commit09911bf2008-07-26 23:55:29191 };
192
193 int message_count_;
194 int pending_messages_;
195};
196
197class FuzzerClientListener : public SimpleListener {
198 public:
199 FuzzerClientListener() : last_msg_(NULL) {
200 }
201
dchengfe61fca2014-10-22 02:29:52202 bool OnMessageReceived(const IPC::Message& msg) override {
initial.commit09911bf2008-07-26 23:55:29203 last_msg_ = new IPC::Message(msg);
[email protected]fd0a773a2013-04-30 20:55:03204 base::MessageLoop::current()->Quit();
[email protected]a95986a82010-12-24 06:19:28205 return true;
initial.commit09911bf2008-07-26 23:55:29206 }
207
[email protected]7ee1a44c2010-07-23 14:18:59208 bool ExpectMessage(int value, uint32 type_id) {
initial.commit09911bf2008-07-26 23:55:29209 if (!MsgHandlerInternal(type_id))
210 return false;
211 int msg_value1 = 0;
212 int msg_value2 = 0;
[email protected]ce208f872012-03-07 20:42:56213 PickleIterator iter(*last_msg_);
initial.commit09911bf2008-07-26 23:55:29214 if (!last_msg_->ReadInt(&iter, &msg_value1))
215 return false;
216 if (!last_msg_->ReadInt(&iter, &msg_value2))
217 return false;
218 if ((msg_value2 + 1) != msg_value1)
219 return false;
220 if (msg_value2 != value)
221 return false;
222
223 delete last_msg_;
224 last_msg_ = NULL;
225 return true;
226 }
227
[email protected]7ee1a44c2010-07-23 14:18:59228 bool ExpectMsgNotHandled(uint32 type_id) {
[email protected]1d4ecf42011-08-26 21:27:30229 return ExpectMessage(type_id, MsgUnhandled::ID);
initial.commit09911bf2008-07-26 23:55:29230 }
231
232 private:
[email protected]7ee1a44c2010-07-23 14:18:59233 bool MsgHandlerInternal(uint32 type_id) {
[email protected]fd0a773a2013-04-30 20:55:03234 base::MessageLoop::current()->Run();
initial.commit09911bf2008-07-26 23:55:29235 if (NULL == last_msg_)
236 return false;
237 if (FUZZER_ROUTING_ID != last_msg_->routing_id())
238 return false;
239 return (type_id == last_msg_->type());
240 };
241
242 IPC::Message* last_msg_;
243};
244
[email protected]3c788582013-01-25 21:51:35245// Runs the fuzzing server child mode. Returns when the preset number of
246// messages have been received.
247MULTIPROCESS_IPC_TEST_CLIENT_MAIN(FuzzServerClient) {
[email protected]fd0a773a2013-04-30 20:55:03248 base::MessageLoopForIO main_message_loop;
initial.commit09911bf2008-07-26 23:55:29249 FuzzerServerListener listener;
[email protected]e482111a82014-05-30 03:58:59250 scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient(
251 IPCTestBase::GetChannelName("FuzzServerClient"),
252 &listener));
253 CHECK(channel->Connect());
254 listener.Init(channel.get());
[email protected]fd0a773a2013-04-30 20:55:03255 base::MessageLoop::current()->Run();
[email protected]95cb7fb92008-12-09 22:00:47256 return 0;
initial.commit09911bf2008-07-26 23:55:29257}
258
[email protected]0cb7d8c82013-01-11 15:13:37259class IPCFuzzingTest : public IPCTestBase {
[email protected]95cb7fb92008-12-09 22:00:47260};
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]3c788582013-01-25 21:51:35265 Init("FuzzServerClient");
266
[email protected]df3c1ca12008-12-19 21:37:01267 FuzzerClientListener listener;
[email protected]3c788582013-01-25 21:51:35268 CreateChannel(&listener);
269 listener.Init(channel());
270 ASSERT_TRUE(ConnectChannel());
271 ASSERT_TRUE(StartClient());
initial.commit09911bf2008-07-26 23:55:29272
273 IPC::Message* msg = NULL;
274 int value = 43;
275 msg = new MsgClassIS(value, L"expect 43");
[email protected]3c788582013-01-25 21:51:35276 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29277 EXPECT_TRUE(listener.ExpectMessage(value, MsgClassIS::ID));
278
279 msg = new MsgClassSI(L"expect 44", ++value);
[email protected]3c788582013-01-25 21:51:35280 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29281 EXPECT_TRUE(listener.ExpectMessage(value, MsgClassSI::ID));
282
[email protected]3c788582013-01-25 21:51:35283 EXPECT_TRUE(WaitForClientShutdown());
284 DestroyChannel();
initial.commit09911bf2008-07-26 23:55:29285}
286
[email protected]3c788582013-01-25 21:51:35287// This test uses a payload that is smaller than expected. This generates an
288// error while unpacking the IPC buffer which in debug trigger an assertion and
289// in release is ignored (!). Right after we generate another valid IPC to make
290// sure framing is working properly.
[email protected]20960e072011-09-20 20:59:01291#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
[email protected]95cb7fb92008-12-09 22:00:47292TEST_F(IPCFuzzingTest, MsgBadPayloadShort) {
[email protected]3c788582013-01-25 21:51:35293 Init("FuzzServerClient");
294
[email protected]df3c1ca12008-12-19 21:37:01295 FuzzerClientListener listener;
[email protected]3c788582013-01-25 21:51:35296 CreateChannel(&listener);
297 listener.Init(channel());
298 ASSERT_TRUE(ConnectChannel());
299 ASSERT_TRUE(StartClient());
initial.commit09911bf2008-07-26 23:55:29300
[email protected]753bb252013-11-04 22:28:12301 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassIS::ID,
302 IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:29303 msg->WriteInt(666);
[email protected]3c788582013-01-25 21:51:35304 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29305 EXPECT_TRUE(listener.ExpectMsgNotHandled(MsgClassIS::ID));
306
307 msg = new MsgClassSI(L"expect one", 1);
[email protected]3c788582013-01-25 21:51:35308 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29309 EXPECT_TRUE(listener.ExpectMessage(1, MsgClassSI::ID));
310
[email protected]3c788582013-01-25 21:51:35311 EXPECT_TRUE(WaitForClientShutdown());
312 DestroyChannel();
initial.commit09911bf2008-07-26 23:55:29313}
[email protected]20960e072011-09-20 20:59:01314#endif
initial.commit09911bf2008-07-26 23:55:29315
[email protected]3c788582013-01-25 21:51:35316// This test uses a payload that has too many arguments, but so the payload size
317// is big enough so the unpacking routine does not generate an error as in the
318// case of MsgBadPayloadShort test. This test does not pinpoint a flaw (per se)
319// as by design we don't carry type information on the IPC message.
[email protected]95cb7fb92008-12-09 22:00:47320TEST_F(IPCFuzzingTest, MsgBadPayloadArgs) {
[email protected]3c788582013-01-25 21:51:35321 Init("FuzzServerClient");
322
[email protected]df3c1ca12008-12-19 21:37:01323 FuzzerClientListener listener;
[email protected]3c788582013-01-25 21:51:35324 CreateChannel(&listener);
325 listener.Init(channel());
326 ASSERT_TRUE(ConnectChannel());
327 ASSERT_TRUE(StartClient());
initial.commit09911bf2008-07-26 23:55:29328
[email protected]753bb252013-11-04 22:28:12329 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassSI::ID,
330 IPC::Message::PRIORITY_NORMAL);
[email protected]95cb7fb92008-12-09 22:00:47331 msg->WriteWString(L"d");
initial.commit09911bf2008-07-26 23:55:29332 msg->WriteInt(0);
[email protected]95cb7fb92008-12-09 22:00:47333 msg->WriteInt(0x65); // Extra argument.
334
[email protected]3c788582013-01-25 21:51:35335 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29336 EXPECT_TRUE(listener.ExpectMessage(0, MsgClassSI::ID));
337
[email protected]95cb7fb92008-12-09 22:00:47338 // Now send a well formed message to make sure the receiver wasn't
339 // thrown out of sync by the extra argument.
initial.commit09911bf2008-07-26 23:55:29340 msg = new MsgClassIS(3, L"expect three");
[email protected]3c788582013-01-25 21:51:35341 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29342 EXPECT_TRUE(listener.ExpectMessage(3, MsgClassIS::ID));
343
[email protected]3c788582013-01-25 21:51:35344 EXPECT_TRUE(WaitForClientShutdown());
345 DestroyChannel();
initial.commit09911bf2008-07-26 23:55:29346}
347
[email protected]2a3aa7b52013-01-11 20:56:22348} // namespace