blob: 7f893084b1f8c8761d34f1984b50484a64033538 [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
tfarina10a5c062015-09-04 18:47:575#include <stdint.h>
initial.commit09911bf2008-07-26 23:55:296#include <stdio.h>
tfarina10a5c062015-09-04 18:47:577
tfarina7023f522015-09-11 19:58:488#include <limits>
initial.commit09911bf2008-07-26 23:55:299#include <sstream>
tfarina10a5c062015-09-04 18:47:5710#include <string>
initial.commit09911bf2008-07-26 23:55:2911
[email protected]2a9ec0e2013-07-17 23:00:3012#include "base/message_loop/message_loop.h"
thestigf84f17f2015-03-11 20:41:5513#include "base/strings/string16.h"
14#include "base/strings/utf_string_conversions.h"
[email protected]f214f8792011-01-01 02:17:0815#include "base/threading/platform_thread.h"
[email protected]0cb7d8c82013-01-11 15:13:3716#include "ipc/ipc_test_base.h"
initial.commit09911bf2008-07-26 23:55:2917#include "testing/gtest/include/gtest/gtest.h"
18
[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
thestigf84f17f2015-03-11 20:41:5526// Generic message class that is an int followed by a string16.
27IPC_MESSAGE_CONTROL2(MsgClassIS, int, base::string16)
[email protected]1d4ecf42011-08-26 21:27:3028
thestigf84f17f2015-03-11 20:41:5529// Generic message class that is a string16 followed by an int.
30IPC_MESSAGE_CONTROL2(MsgClassSI, base::string16, int)
[email protected]1d4ecf42011-08-26 21:27:3031
32// Message to create a mutex in the IPC server, using the received name.
thestigf84f17f2015-03-11 20:41:5533IPC_MESSAGE_CONTROL2(MsgDoMutex, base::string16, int)
[email protected]1d4ecf42011-08-26 21:27:3034
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) {
thestigf84f17f2015-03-11 20:41:5543 // This was BUG 984408.
tfarina7023f522015-09-11 19:58:4844 uint32_t v1 = std::numeric_limits<uint32_t>::max() - 1;
initial.commit09911bf2008-07-26 23:55:2945 int v2 = 666;
[email protected]753bb252013-11-04 22:28:1246 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:2947 EXPECT_TRUE(m.WriteInt(v1));
48 EXPECT_TRUE(m.WriteInt(v2));
49
brettwbd4d7112015-06-03 04:29:2550 base::PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2951 std::string vs;
avi48fc13b2014-12-28 23:31:4852 EXPECT_FALSE(iter.ReadString(&vs));
initial.commit09911bf2008-07-26 23:55:2953}
54
thestigf84f17f2015-03-11 20:41:5555TEST(IPCMessageIntegrity, ReadBeyondBufferStr16) {
56 // This was BUG 984408.
tfarina7023f522015-09-11 19:58:4857 uint32_t v1 = std::numeric_limits<uint32_t>::max() - 1;
initial.commit09911bf2008-07-26 23:55:2958 int v2 = 777;
[email protected]753bb252013-11-04 22:28:1259 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:2960 EXPECT_TRUE(m.WriteInt(v1));
61 EXPECT_TRUE(m.WriteInt(v2));
62
brettwbd4d7112015-06-03 04:29:2563 base::PickleIterator iter(m);
thestigf84f17f2015-03-11 20:41:5564 base::string16 vs;
65 EXPECT_FALSE(iter.ReadString16(&vs));
initial.commit09911bf2008-07-26 23:55:2966}
67
68TEST(IPCMessageIntegrity, ReadBytesBadIterator) {
69 // This was BUG 1035467.
[email protected]753bb252013-11-04 22:28:1270 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:2971 EXPECT_TRUE(m.WriteInt(1));
72 EXPECT_TRUE(m.WriteInt(2));
73
brettwbd4d7112015-06-03 04:29:2574 base::PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2975 const char* data = NULL;
avi48fc13b2014-12-28 23:31:4876 EXPECT_TRUE(iter.ReadBytes(&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>.
[email protected]753bb252013-11-04 22:28:1283 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:2984 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;
brettwbd4d7112015-06-03 04:29:2590 base::PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2991 EXPECT_FALSE(ReadParam(&m, &iter, &vec));
92}
93
tfarina8514f0d2015-07-28 14:41:4794#if defined(OS_ANDROID)
95#define MAYBE_ReadVectorTooLarge1 DISABLED_ReadVectorTooLarge1
96#else
97#define MAYBE_ReadVectorTooLarge1 ReadVectorTooLarge1
98#endif
99TEST(IPCMessageIntegrity, MAYBE_ReadVectorTooLarge1) {
initial.commit09911bf2008-07-26 23:55:29100 // This was BUG 1006367. This is the large but positive length case. Again
101 // we try to hit the non-specialized case vector<P>.
[email protected]753bb252013-11-04 22:28:12102 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:29103 EXPECT_TRUE(m.WriteInt(0x21000003)); // This is the count of elements.
104 EXPECT_TRUE(m.WriteInt64(1));
105 EXPECT_TRUE(m.WriteInt64(2));
106
tfarina10a5c062015-09-04 18:47:57107 std::vector<int64_t> vec;
brettwbd4d7112015-06-03 04:29:25108 base::PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:29109 EXPECT_FALSE(ReadParam(&m, &iter, &vec));
110}
111
112TEST(IPCMessageIntegrity, ReadVectorTooLarge2) {
113 // This was BUG 1006367. This is the large but positive with an additional
114 // integer overflow when computing the actual byte size. Again we try to hit
115 // the non-specialized case vector<P>.
[email protected]753bb252013-11-04 22:28:12116 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:29117 EXPECT_TRUE(m.WriteInt(0x71000000)); // This is the count of elements.
118 EXPECT_TRUE(m.WriteInt64(1));
119 EXPECT_TRUE(m.WriteInt64(2));
120
tfarina10a5c062015-09-04 18:47:57121 std::vector<int64_t> vec;
brettwbd4d7112015-06-03 04:29:25122 base::PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:29123 EXPECT_FALSE(ReadParam(&m, &iter, &vec));
124}
125
[email protected]57319ce2012-06-11 22:35:26126class SimpleListener : public IPC::Listener {
initial.commit09911bf2008-07-26 23:55:29127 public:
128 SimpleListener() : other_(NULL) {
129 }
[email protected]57319ce2012-06-11 22:35:26130 void Init(IPC::Sender* s) {
initial.commit09911bf2008-07-26 23:55:29131 other_ = s;
132 }
133 protected:
[email protected]57319ce2012-06-11 22:35:26134 IPC::Sender* other_;
initial.commit09911bf2008-07-26 23:55:29135};
136
137enum {
138 FUZZER_ROUTING_ID = 5
139};
140
141// The fuzzer server class. It runs in a child process and expects
142// only two IPC calls; after that it exits the message loop which
143// terminates the child process.
144class FuzzerServerListener : public SimpleListener {
145 public:
146 FuzzerServerListener() : message_count_(2), pending_messages_(0) {
147 }
dchengfe61fca2014-10-22 02:29:52148 bool OnMessageReceived(const IPC::Message& msg) override {
initial.commit09911bf2008-07-26 23:55:29149 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
150 ++pending_messages_;
151 IPC_BEGIN_MESSAGE_MAP(FuzzerServerListener, msg)
152 IPC_MESSAGE_HANDLER(MsgClassIS, OnMsgClassISMessage)
153 IPC_MESSAGE_HANDLER(MsgClassSI, OnMsgClassSIMessage)
154 IPC_END_MESSAGE_MAP()
155 if (pending_messages_) {
156 // Probably a problem de-serializing the message.
157 ReplyMsgNotHandled(msg.type());
158 }
159 }
[email protected]a95986a82010-12-24 06:19:28160 return true;
initial.commit09911bf2008-07-26 23:55:29161 }
162
163 private:
thestigf84f17f2015-03-11 20:41:55164 void OnMsgClassISMessage(int value, const base::string16& text) {
initial.commit09911bf2008-07-26 23:55:29165 UseData(MsgClassIS::ID, value, text);
166 RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassIS::ID, value);
167 Cleanup();
168 }
169
thestigf84f17f2015-03-11 20:41:55170 void OnMsgClassSIMessage(const base::string16& text, int value) {
initial.commit09911bf2008-07-26 23:55:29171 UseData(MsgClassSI::ID, value, text);
172 RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassSI::ID, value);
173 Cleanup();
174 }
175
tfarina10a5c062015-09-04 18:47:57176 bool RoundtripAckReply(int routing, uint32_t type_id, int reply) {
[email protected]753bb252013-11-04 22:28:12177 IPC::Message* message = new IPC::Message(routing, type_id,
178 IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:29179 message->WriteInt(reply + 1);
180 message->WriteInt(reply);
181 return other_->Send(message);
182 }
183
184 void Cleanup() {
185 --message_count_;
186 --pending_messages_;
187 if (0 == message_count_)
ki.stfua21ed8c2015-10-12 17:26:00188 base::MessageLoop::current()->QuitWhenIdle();
initial.commit09911bf2008-07-26 23:55:29189 }
190
tfarina10a5c062015-09-04 18:47:57191 void ReplyMsgNotHandled(uint32_t type_id) {
[email protected]1d4ecf42011-08-26 21:27:30192 RoundtripAckReply(FUZZER_ROUTING_ID, MsgUnhandled::ID, type_id);
initial.commit09911bf2008-07-26 23:55:29193 Cleanup();
194 }
195
thestigf84f17f2015-03-11 20:41:55196 void UseData(int caller, int value, const base::string16& text) {
197 std::ostringstream os;
198 os << "IPC fuzzer:" << caller << " [" << value << " "
199 << base::UTF16ToUTF8(text) << "]\n";
200 std::string output = os.str();
201 LOG(WARNING) << output;
202 }
initial.commit09911bf2008-07-26 23:55:29203
204 int message_count_;
205 int pending_messages_;
206};
207
208class FuzzerClientListener : public SimpleListener {
209 public:
210 FuzzerClientListener() : last_msg_(NULL) {
211 }
212
dchengfe61fca2014-10-22 02:29:52213 bool OnMessageReceived(const IPC::Message& msg) override {
initial.commit09911bf2008-07-26 23:55:29214 last_msg_ = new IPC::Message(msg);
ki.stfua21ed8c2015-10-12 17:26:00215 base::MessageLoop::current()->QuitWhenIdle();
[email protected]a95986a82010-12-24 06:19:28216 return true;
initial.commit09911bf2008-07-26 23:55:29217 }
218
tfarina10a5c062015-09-04 18:47:57219 bool ExpectMessage(int value, uint32_t type_id) {
initial.commit09911bf2008-07-26 23:55:29220 if (!MsgHandlerInternal(type_id))
221 return false;
222 int msg_value1 = 0;
223 int msg_value2 = 0;
brettwbd4d7112015-06-03 04:29:25224 base::PickleIterator iter(*last_msg_);
avi48fc13b2014-12-28 23:31:48225 if (!iter.ReadInt(&msg_value1))
initial.commit09911bf2008-07-26 23:55:29226 return false;
avi48fc13b2014-12-28 23:31:48227 if (!iter.ReadInt(&msg_value2))
initial.commit09911bf2008-07-26 23:55:29228 return false;
229 if ((msg_value2 + 1) != msg_value1)
230 return false;
231 if (msg_value2 != value)
232 return false;
233
234 delete last_msg_;
235 last_msg_ = NULL;
236 return true;
237 }
238
tfarina10a5c062015-09-04 18:47:57239 bool ExpectMsgNotHandled(uint32_t type_id) {
[email protected]1d4ecf42011-08-26 21:27:30240 return ExpectMessage(type_id, MsgUnhandled::ID);
initial.commit09911bf2008-07-26 23:55:29241 }
242
243 private:
tfarina10a5c062015-09-04 18:47:57244 bool MsgHandlerInternal(uint32_t type_id) {
[email protected]fd0a773a2013-04-30 20:55:03245 base::MessageLoop::current()->Run();
initial.commit09911bf2008-07-26 23:55:29246 if (NULL == last_msg_)
247 return false;
248 if (FUZZER_ROUTING_ID != last_msg_->routing_id())
249 return false;
250 return (type_id == last_msg_->type());
thestigf84f17f2015-03-11 20:41:55251 }
initial.commit09911bf2008-07-26 23:55:29252
253 IPC::Message* last_msg_;
254};
255
[email protected]3c788582013-01-25 21:51:35256// Runs the fuzzing server child mode. Returns when the preset number of
257// messages have been received.
258MULTIPROCESS_IPC_TEST_CLIENT_MAIN(FuzzServerClient) {
[email protected]fd0a773a2013-04-30 20:55:03259 base::MessageLoopForIO main_message_loop;
initial.commit09911bf2008-07-26 23:55:29260 FuzzerServerListener listener;
[email protected]e482111a82014-05-30 03:58:59261 scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient(
erikchen30dc2812015-09-24 03:26:38262 IPCTestBase::GetChannelName("FuzzServerClient"), &listener));
[email protected]e482111a82014-05-30 03:58:59263 CHECK(channel->Connect());
264 listener.Init(channel.get());
[email protected]fd0a773a2013-04-30 20:55:03265 base::MessageLoop::current()->Run();
[email protected]95cb7fb92008-12-09 22:00:47266 return 0;
initial.commit09911bf2008-07-26 23:55:29267}
268
[email protected]0cb7d8c82013-01-11 15:13:37269class IPCFuzzingTest : public IPCTestBase {
[email protected]95cb7fb92008-12-09 22:00:47270};
271
tfarina8514f0d2015-07-28 14:41:47272#if defined(OS_ANDROID)
273#define MAYBE_SanityTest DISABLED_SanityTest
274#else
275#define MAYBE_SanityTest SanityTest
276#endif
initial.commit09911bf2008-07-26 23:55:29277// This test makes sure that the FuzzerClientListener and FuzzerServerListener
278// are working properly by generating two well formed IPC calls.
tfarina8514f0d2015-07-28 14:41:47279TEST_F(IPCFuzzingTest, MAYBE_SanityTest) {
[email protected]3c788582013-01-25 21:51:35280 Init("FuzzServerClient");
281
[email protected]df3c1ca12008-12-19 21:37:01282 FuzzerClientListener listener;
[email protected]3c788582013-01-25 21:51:35283 CreateChannel(&listener);
284 listener.Init(channel());
285 ASSERT_TRUE(ConnectChannel());
286 ASSERT_TRUE(StartClient());
initial.commit09911bf2008-07-26 23:55:29287
288 IPC::Message* msg = NULL;
289 int value = 43;
thestigf84f17f2015-03-11 20:41:55290 msg = new MsgClassIS(value, base::ASCIIToUTF16("expect 43"));
[email protected]3c788582013-01-25 21:51:35291 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29292 EXPECT_TRUE(listener.ExpectMessage(value, MsgClassIS::ID));
293
thestigf84f17f2015-03-11 20:41:55294 msg = new MsgClassSI(base::ASCIIToUTF16("expect 44"), ++value);
[email protected]3c788582013-01-25 21:51:35295 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29296 EXPECT_TRUE(listener.ExpectMessage(value, MsgClassSI::ID));
297
[email protected]3c788582013-01-25 21:51:35298 EXPECT_TRUE(WaitForClientShutdown());
299 DestroyChannel();
initial.commit09911bf2008-07-26 23:55:29300}
301
tfarina8514f0d2015-07-28 14:41:47302#if defined(OS_ANDROID)
303#define MAYBE_MsgBadPayloadShort DISABLED_MsgBadPayloadShort
304#else
305#define MAYBE_MsgBadPayloadShort MsgBadPayloadShort
306#endif
[email protected]3c788582013-01-25 21:51:35307// This test uses a payload that is smaller than expected. This generates an
308// error while unpacking the IPC buffer which in debug trigger an assertion and
309// in release is ignored (!). Right after we generate another valid IPC to make
310// sure framing is working properly.
[email protected]20960e072011-09-20 20:59:01311#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
tfarina8514f0d2015-07-28 14:41:47312TEST_F(IPCFuzzingTest, MAYBE_MsgBadPayloadShort) {
[email protected]3c788582013-01-25 21:51:35313 Init("FuzzServerClient");
314
[email protected]df3c1ca12008-12-19 21:37:01315 FuzzerClientListener listener;
[email protected]3c788582013-01-25 21:51:35316 CreateChannel(&listener);
317 listener.Init(channel());
318 ASSERT_TRUE(ConnectChannel());
319 ASSERT_TRUE(StartClient());
initial.commit09911bf2008-07-26 23:55:29320
[email protected]753bb252013-11-04 22:28:12321 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassIS::ID,
322 IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:29323 msg->WriteInt(666);
[email protected]3c788582013-01-25 21:51:35324 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29325 EXPECT_TRUE(listener.ExpectMsgNotHandled(MsgClassIS::ID));
326
thestigf84f17f2015-03-11 20:41:55327 msg = new MsgClassSI(base::ASCIIToUTF16("expect one"), 1);
[email protected]3c788582013-01-25 21:51:35328 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29329 EXPECT_TRUE(listener.ExpectMessage(1, MsgClassSI::ID));
330
[email protected]3c788582013-01-25 21:51:35331 EXPECT_TRUE(WaitForClientShutdown());
332 DestroyChannel();
initial.commit09911bf2008-07-26 23:55:29333}
[email protected]20960e072011-09-20 20:59:01334#endif
initial.commit09911bf2008-07-26 23:55:29335
tfarina8514f0d2015-07-28 14:41:47336#if defined(OS_ANDROID)
337#define MAYBE_MsgBadPayloadArgs DISABLED_MsgBadPayloadArgs
338#else
339#define MAYBE_MsgBadPayloadArgs MsgBadPayloadArgs
340#endif
[email protected]3c788582013-01-25 21:51:35341// This test uses a payload that has too many arguments, but so the payload size
342// is big enough so the unpacking routine does not generate an error as in the
343// case of MsgBadPayloadShort test. This test does not pinpoint a flaw (per se)
344// as by design we don't carry type information on the IPC message.
tfarina8514f0d2015-07-28 14:41:47345TEST_F(IPCFuzzingTest, MAYBE_MsgBadPayloadArgs) {
[email protected]3c788582013-01-25 21:51:35346 Init("FuzzServerClient");
347
[email protected]df3c1ca12008-12-19 21:37:01348 FuzzerClientListener listener;
[email protected]3c788582013-01-25 21:51:35349 CreateChannel(&listener);
350 listener.Init(channel());
351 ASSERT_TRUE(ConnectChannel());
352 ASSERT_TRUE(StartClient());
initial.commit09911bf2008-07-26 23:55:29353
[email protected]753bb252013-11-04 22:28:12354 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassSI::ID,
355 IPC::Message::PRIORITY_NORMAL);
thestigf84f17f2015-03-11 20:41:55356 msg->WriteString16(base::ASCIIToUTF16("d"));
initial.commit09911bf2008-07-26 23:55:29357 msg->WriteInt(0);
[email protected]95cb7fb92008-12-09 22:00:47358 msg->WriteInt(0x65); // Extra argument.
359
[email protected]3c788582013-01-25 21:51:35360 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29361 EXPECT_TRUE(listener.ExpectMessage(0, MsgClassSI::ID));
362
[email protected]95cb7fb92008-12-09 22:00:47363 // Now send a well formed message to make sure the receiver wasn't
364 // thrown out of sync by the extra argument.
thestigf84f17f2015-03-11 20:41:55365 msg = new MsgClassIS(3, base::ASCIIToUTF16("expect three"));
[email protected]3c788582013-01-25 21:51:35366 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29367 EXPECT_TRUE(listener.ExpectMessage(3, MsgClassIS::ID));
368
[email protected]3c788582013-01-25 21:51:35369 EXPECT_TRUE(WaitForClientShutdown());
370 DestroyChannel();
initial.commit09911bf2008-07-26 23:55:29371}
372
[email protected]2a3aa7b52013-01-11 20:56:22373} // namespace