blob: efaed2cdeeaf7aff9a592945a13e81046e1c9d25 [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
initial.commit09911bf2008-07-26 23:55:298#include <sstream>
tfarina10a5c062015-09-04 18:47:579#include <string>
initial.commit09911bf2008-07-26 23:55:2910
[email protected]2a9ec0e2013-07-17 23:00:3011#include "base/message_loop/message_loop.h"
thestigf84f17f2015-03-11 20:41:5512#include "base/strings/string16.h"
13#include "base/strings/utf_string_conversions.h"
[email protected]f214f8792011-01-01 02:17:0814#include "base/threading/platform_thread.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"
17
[email protected]2a3aa7b52013-01-11 20:56:2218// IPC messages for testing ----------------------------------------------------
[email protected]1d4ecf42011-08-26 21:27:3019
20#define IPC_MESSAGE_IMPL
21#include "ipc/ipc_message_macros.h"
22
23#define IPC_MESSAGE_START TestMsgStart
24
thestigf84f17f2015-03-11 20:41:5525// Generic message class that is an int followed by a string16.
26IPC_MESSAGE_CONTROL2(MsgClassIS, int, base::string16)
[email protected]1d4ecf42011-08-26 21:27:3027
thestigf84f17f2015-03-11 20:41:5528// Generic message class that is a string16 followed by an int.
29IPC_MESSAGE_CONTROL2(MsgClassSI, base::string16, int)
[email protected]1d4ecf42011-08-26 21:27:3030
31// Message to create a mutex in the IPC server, using the received name.
thestigf84f17f2015-03-11 20:41:5532IPC_MESSAGE_CONTROL2(MsgDoMutex, base::string16, int)
[email protected]1d4ecf42011-08-26 21:27:3033
34// Used to generate an ID for a message that should not exist.
35IPC_MESSAGE_CONTROL0(MsgUnhandled)
36
[email protected]2a3aa7b52013-01-11 20:56:2237// -----------------------------------------------------------------------------
38
39namespace {
[email protected]1d4ecf42011-08-26 21:27:3040
initial.commit09911bf2008-07-26 23:55:2941TEST(IPCMessageIntegrity, ReadBeyondBufferStr) {
thestigf84f17f2015-03-11 20:41:5542 // This was BUG 984408.
tfarina10a5c062015-09-04 18:47:5743 uint32_t v1 = kuint32max - 1;
initial.commit09911bf2008-07-26 23:55:2944 int v2 = 666;
[email protected]753bb252013-11-04 22:28:1245 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:2946 EXPECT_TRUE(m.WriteInt(v1));
47 EXPECT_TRUE(m.WriteInt(v2));
48
brettwbd4d7112015-06-03 04:29:2549 base::PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2950 std::string vs;
avi48fc13b2014-12-28 23:31:4851 EXPECT_FALSE(iter.ReadString(&vs));
initial.commit09911bf2008-07-26 23:55:2952}
53
thestigf84f17f2015-03-11 20:41:5554TEST(IPCMessageIntegrity, ReadBeyondBufferStr16) {
55 // This was BUG 984408.
tfarina10a5c062015-09-04 18:47:5756 uint32_t v1 = kuint32max - 1;
initial.commit09911bf2008-07-26 23:55:2957 int v2 = 777;
[email protected]753bb252013-11-04 22:28:1258 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:2959 EXPECT_TRUE(m.WriteInt(v1));
60 EXPECT_TRUE(m.WriteInt(v2));
61
brettwbd4d7112015-06-03 04:29:2562 base::PickleIterator iter(m);
thestigf84f17f2015-03-11 20:41:5563 base::string16 vs;
64 EXPECT_FALSE(iter.ReadString16(&vs));
initial.commit09911bf2008-07-26 23:55:2965}
66
67TEST(IPCMessageIntegrity, ReadBytesBadIterator) {
68 // This was BUG 1035467.
[email protected]753bb252013-11-04 22:28:1269 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:2970 EXPECT_TRUE(m.WriteInt(1));
71 EXPECT_TRUE(m.WriteInt(2));
72
brettwbd4d7112015-06-03 04:29:2573 base::PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2974 const char* data = NULL;
avi48fc13b2014-12-28 23:31:4875 EXPECT_TRUE(iter.ReadBytes(&data, sizeof(int)));
initial.commit09911bf2008-07-26 23:55:2976}
77
78TEST(IPCMessageIntegrity, ReadVectorNegativeSize) {
79 // A slight variation of BUG 984408. Note that the pickling of vector<char>
80 // has a specialized template which is not vulnerable to this bug. So here
81 // try to hit the non-specialized case vector<P>.
[email protected]753bb252013-11-04 22:28:1282 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:2983 EXPECT_TRUE(m.WriteInt(-1)); // This is the count of elements.
84 EXPECT_TRUE(m.WriteInt(1));
85 EXPECT_TRUE(m.WriteInt(2));
86 EXPECT_TRUE(m.WriteInt(3));
87
88 std::vector<double> vec;
brettwbd4d7112015-06-03 04:29:2589 base::PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2990 EXPECT_FALSE(ReadParam(&m, &iter, &vec));
91}
92
tfarina8514f0d2015-07-28 14:41:4793#if defined(OS_ANDROID)
94#define MAYBE_ReadVectorTooLarge1 DISABLED_ReadVectorTooLarge1
95#else
96#define MAYBE_ReadVectorTooLarge1 ReadVectorTooLarge1
97#endif
98TEST(IPCMessageIntegrity, MAYBE_ReadVectorTooLarge1) {
initial.commit09911bf2008-07-26 23:55:2999 // This was BUG 1006367. This is the large but positive length case. Again
100 // we try to hit the non-specialized case vector<P>.
[email protected]753bb252013-11-04 22:28:12101 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:29102 EXPECT_TRUE(m.WriteInt(0x21000003)); // This is the count of elements.
103 EXPECT_TRUE(m.WriteInt64(1));
104 EXPECT_TRUE(m.WriteInt64(2));
105
tfarina10a5c062015-09-04 18:47:57106 std::vector<int64_t> vec;
brettwbd4d7112015-06-03 04:29:25107 base::PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:29108 EXPECT_FALSE(ReadParam(&m, &iter, &vec));
109}
110
111TEST(IPCMessageIntegrity, ReadVectorTooLarge2) {
112 // This was BUG 1006367. This is the large but positive with an additional
113 // integer overflow when computing the actual byte size. Again we try to hit
114 // the non-specialized case vector<P>.
[email protected]753bb252013-11-04 22:28:12115 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:29116 EXPECT_TRUE(m.WriteInt(0x71000000)); // This is the count of elements.
117 EXPECT_TRUE(m.WriteInt64(1));
118 EXPECT_TRUE(m.WriteInt64(2));
119
tfarina10a5c062015-09-04 18:47:57120 std::vector<int64_t> vec;
brettwbd4d7112015-06-03 04:29:25121 base::PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:29122 EXPECT_FALSE(ReadParam(&m, &iter, &vec));
123}
124
[email protected]57319ce2012-06-11 22:35:26125class SimpleListener : public IPC::Listener {
initial.commit09911bf2008-07-26 23:55:29126 public:
127 SimpleListener() : other_(NULL) {
128 }
[email protected]57319ce2012-06-11 22:35:26129 void Init(IPC::Sender* s) {
initial.commit09911bf2008-07-26 23:55:29130 other_ = s;
131 }
132 protected:
[email protected]57319ce2012-06-11 22:35:26133 IPC::Sender* other_;
initial.commit09911bf2008-07-26 23:55:29134};
135
136enum {
137 FUZZER_ROUTING_ID = 5
138};
139
140// The fuzzer server class. It runs in a child process and expects
141// only two IPC calls; after that it exits the message loop which
142// terminates the child process.
143class FuzzerServerListener : public SimpleListener {
144 public:
145 FuzzerServerListener() : message_count_(2), pending_messages_(0) {
146 }
dchengfe61fca2014-10-22 02:29:52147 bool OnMessageReceived(const IPC::Message& msg) override {
initial.commit09911bf2008-07-26 23:55:29148 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
149 ++pending_messages_;
150 IPC_BEGIN_MESSAGE_MAP(FuzzerServerListener, msg)
151 IPC_MESSAGE_HANDLER(MsgClassIS, OnMsgClassISMessage)
152 IPC_MESSAGE_HANDLER(MsgClassSI, OnMsgClassSIMessage)
153 IPC_END_MESSAGE_MAP()
154 if (pending_messages_) {
155 // Probably a problem de-serializing the message.
156 ReplyMsgNotHandled(msg.type());
157 }
158 }
[email protected]a95986a82010-12-24 06:19:28159 return true;
initial.commit09911bf2008-07-26 23:55:29160 }
161
162 private:
thestigf84f17f2015-03-11 20:41:55163 void OnMsgClassISMessage(int value, const base::string16& text) {
initial.commit09911bf2008-07-26 23:55:29164 UseData(MsgClassIS::ID, value, text);
165 RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassIS::ID, value);
166 Cleanup();
167 }
168
thestigf84f17f2015-03-11 20:41:55169 void OnMsgClassSIMessage(const base::string16& text, int value) {
initial.commit09911bf2008-07-26 23:55:29170 UseData(MsgClassSI::ID, value, text);
171 RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassSI::ID, value);
172 Cleanup();
173 }
174
tfarina10a5c062015-09-04 18:47:57175 bool RoundtripAckReply(int routing, uint32_t type_id, int reply) {
[email protected]753bb252013-11-04 22:28:12176 IPC::Message* message = new IPC::Message(routing, type_id,
177 IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:29178 message->WriteInt(reply + 1);
179 message->WriteInt(reply);
180 return other_->Send(message);
181 }
182
183 void Cleanup() {
184 --message_count_;
185 --pending_messages_;
186 if (0 == message_count_)
[email protected]fd0a773a2013-04-30 20:55:03187 base::MessageLoop::current()->Quit();
initial.commit09911bf2008-07-26 23:55:29188 }
189
tfarina10a5c062015-09-04 18:47:57190 void ReplyMsgNotHandled(uint32_t type_id) {
[email protected]1d4ecf42011-08-26 21:27:30191 RoundtripAckReply(FUZZER_ROUTING_ID, MsgUnhandled::ID, type_id);
initial.commit09911bf2008-07-26 23:55:29192 Cleanup();
193 }
194
thestigf84f17f2015-03-11 20:41:55195 void UseData(int caller, int value, const base::string16& text) {
196 std::ostringstream os;
197 os << "IPC fuzzer:" << caller << " [" << value << " "
198 << base::UTF16ToUTF8(text) << "]\n";
199 std::string output = os.str();
200 LOG(WARNING) << output;
201 }
initial.commit09911bf2008-07-26 23:55:29202
203 int message_count_;
204 int pending_messages_;
205};
206
207class FuzzerClientListener : public SimpleListener {
208 public:
209 FuzzerClientListener() : last_msg_(NULL) {
210 }
211
dchengfe61fca2014-10-22 02:29:52212 bool OnMessageReceived(const IPC::Message& msg) override {
initial.commit09911bf2008-07-26 23:55:29213 last_msg_ = new IPC::Message(msg);
[email protected]fd0a773a2013-04-30 20:55:03214 base::MessageLoop::current()->Quit();
[email protected]a95986a82010-12-24 06:19:28215 return true;
initial.commit09911bf2008-07-26 23:55:29216 }
217
tfarina10a5c062015-09-04 18:47:57218 bool ExpectMessage(int value, uint32_t type_id) {
initial.commit09911bf2008-07-26 23:55:29219 if (!MsgHandlerInternal(type_id))
220 return false;
221 int msg_value1 = 0;
222 int msg_value2 = 0;
brettwbd4d7112015-06-03 04:29:25223 base::PickleIterator iter(*last_msg_);
avi48fc13b2014-12-28 23:31:48224 if (!iter.ReadInt(&msg_value1))
initial.commit09911bf2008-07-26 23:55:29225 return false;
avi48fc13b2014-12-28 23:31:48226 if (!iter.ReadInt(&msg_value2))
initial.commit09911bf2008-07-26 23:55:29227 return false;
228 if ((msg_value2 + 1) != msg_value1)
229 return false;
230 if (msg_value2 != value)
231 return false;
232
233 delete last_msg_;
234 last_msg_ = NULL;
235 return true;
236 }
237
tfarina10a5c062015-09-04 18:47:57238 bool ExpectMsgNotHandled(uint32_t type_id) {
[email protected]1d4ecf42011-08-26 21:27:30239 return ExpectMessage(type_id, MsgUnhandled::ID);
initial.commit09911bf2008-07-26 23:55:29240 }
241
242 private:
tfarina10a5c062015-09-04 18:47:57243 bool MsgHandlerInternal(uint32_t type_id) {
[email protected]fd0a773a2013-04-30 20:55:03244 base::MessageLoop::current()->Run();
initial.commit09911bf2008-07-26 23:55:29245 if (NULL == last_msg_)
246 return false;
247 if (FUZZER_ROUTING_ID != last_msg_->routing_id())
248 return false;
249 return (type_id == last_msg_->type());
thestigf84f17f2015-03-11 20:41:55250 }
initial.commit09911bf2008-07-26 23:55:29251
252 IPC::Message* last_msg_;
253};
254
[email protected]3c788582013-01-25 21:51:35255// Runs the fuzzing server child mode. Returns when the preset number of
256// messages have been received.
257MULTIPROCESS_IPC_TEST_CLIENT_MAIN(FuzzServerClient) {
[email protected]fd0a773a2013-04-30 20:55:03258 base::MessageLoopForIO main_message_loop;
initial.commit09911bf2008-07-26 23:55:29259 FuzzerServerListener listener;
[email protected]e482111a82014-05-30 03:58:59260 scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient(
erikchen27aa7d82015-06-16 21:21:04261 IPCTestBase::GetChannelName("FuzzServerClient"), &listener, nullptr));
[email protected]e482111a82014-05-30 03:58:59262 CHECK(channel->Connect());
263 listener.Init(channel.get());
[email protected]fd0a773a2013-04-30 20:55:03264 base::MessageLoop::current()->Run();
[email protected]95cb7fb92008-12-09 22:00:47265 return 0;
initial.commit09911bf2008-07-26 23:55:29266}
267
[email protected]0cb7d8c82013-01-11 15:13:37268class IPCFuzzingTest : public IPCTestBase {
[email protected]95cb7fb92008-12-09 22:00:47269};
270
tfarina8514f0d2015-07-28 14:41:47271#if defined(OS_ANDROID)
272#define MAYBE_SanityTest DISABLED_SanityTest
273#else
274#define MAYBE_SanityTest SanityTest
275#endif
initial.commit09911bf2008-07-26 23:55:29276// This test makes sure that the FuzzerClientListener and FuzzerServerListener
277// are working properly by generating two well formed IPC calls.
tfarina8514f0d2015-07-28 14:41:47278TEST_F(IPCFuzzingTest, MAYBE_SanityTest) {
[email protected]3c788582013-01-25 21:51:35279 Init("FuzzServerClient");
280
[email protected]df3c1ca12008-12-19 21:37:01281 FuzzerClientListener listener;
[email protected]3c788582013-01-25 21:51:35282 CreateChannel(&listener);
283 listener.Init(channel());
284 ASSERT_TRUE(ConnectChannel());
285 ASSERT_TRUE(StartClient());
initial.commit09911bf2008-07-26 23:55:29286
287 IPC::Message* msg = NULL;
288 int value = 43;
thestigf84f17f2015-03-11 20:41:55289 msg = new MsgClassIS(value, base::ASCIIToUTF16("expect 43"));
[email protected]3c788582013-01-25 21:51:35290 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29291 EXPECT_TRUE(listener.ExpectMessage(value, MsgClassIS::ID));
292
thestigf84f17f2015-03-11 20:41:55293 msg = new MsgClassSI(base::ASCIIToUTF16("expect 44"), ++value);
[email protected]3c788582013-01-25 21:51:35294 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29295 EXPECT_TRUE(listener.ExpectMessage(value, MsgClassSI::ID));
296
[email protected]3c788582013-01-25 21:51:35297 EXPECT_TRUE(WaitForClientShutdown());
298 DestroyChannel();
initial.commit09911bf2008-07-26 23:55:29299}
300
tfarina8514f0d2015-07-28 14:41:47301#if defined(OS_ANDROID)
302#define MAYBE_MsgBadPayloadShort DISABLED_MsgBadPayloadShort
303#else
304#define MAYBE_MsgBadPayloadShort MsgBadPayloadShort
305#endif
[email protected]3c788582013-01-25 21:51:35306// This test uses a payload that is smaller than expected. This generates an
307// error while unpacking the IPC buffer which in debug trigger an assertion and
308// in release is ignored (!). Right after we generate another valid IPC to make
309// sure framing is working properly.
[email protected]20960e072011-09-20 20:59:01310#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
tfarina8514f0d2015-07-28 14:41:47311TEST_F(IPCFuzzingTest, MAYBE_MsgBadPayloadShort) {
[email protected]3c788582013-01-25 21:51:35312 Init("FuzzServerClient");
313
[email protected]df3c1ca12008-12-19 21:37:01314 FuzzerClientListener listener;
[email protected]3c788582013-01-25 21:51:35315 CreateChannel(&listener);
316 listener.Init(channel());
317 ASSERT_TRUE(ConnectChannel());
318 ASSERT_TRUE(StartClient());
initial.commit09911bf2008-07-26 23:55:29319
[email protected]753bb252013-11-04 22:28:12320 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassIS::ID,
321 IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:29322 msg->WriteInt(666);
[email protected]3c788582013-01-25 21:51:35323 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29324 EXPECT_TRUE(listener.ExpectMsgNotHandled(MsgClassIS::ID));
325
thestigf84f17f2015-03-11 20:41:55326 msg = new MsgClassSI(base::ASCIIToUTF16("expect one"), 1);
[email protected]3c788582013-01-25 21:51:35327 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29328 EXPECT_TRUE(listener.ExpectMessage(1, MsgClassSI::ID));
329
[email protected]3c788582013-01-25 21:51:35330 EXPECT_TRUE(WaitForClientShutdown());
331 DestroyChannel();
initial.commit09911bf2008-07-26 23:55:29332}
[email protected]20960e072011-09-20 20:59:01333#endif
initial.commit09911bf2008-07-26 23:55:29334
tfarina8514f0d2015-07-28 14:41:47335#if defined(OS_ANDROID)
336#define MAYBE_MsgBadPayloadArgs DISABLED_MsgBadPayloadArgs
337#else
338#define MAYBE_MsgBadPayloadArgs MsgBadPayloadArgs
339#endif
[email protected]3c788582013-01-25 21:51:35340// This test uses a payload that has too many arguments, but so the payload size
341// is big enough so the unpacking routine does not generate an error as in the
342// case of MsgBadPayloadShort test. This test does not pinpoint a flaw (per se)
343// as by design we don't carry type information on the IPC message.
tfarina8514f0d2015-07-28 14:41:47344TEST_F(IPCFuzzingTest, MAYBE_MsgBadPayloadArgs) {
[email protected]3c788582013-01-25 21:51:35345 Init("FuzzServerClient");
346
[email protected]df3c1ca12008-12-19 21:37:01347 FuzzerClientListener listener;
[email protected]3c788582013-01-25 21:51:35348 CreateChannel(&listener);
349 listener.Init(channel());
350 ASSERT_TRUE(ConnectChannel());
351 ASSERT_TRUE(StartClient());
initial.commit09911bf2008-07-26 23:55:29352
[email protected]753bb252013-11-04 22:28:12353 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassSI::ID,
354 IPC::Message::PRIORITY_NORMAL);
thestigf84f17f2015-03-11 20:41:55355 msg->WriteString16(base::ASCIIToUTF16("d"));
initial.commit09911bf2008-07-26 23:55:29356 msg->WriteInt(0);
[email protected]95cb7fb92008-12-09 22:00:47357 msg->WriteInt(0x65); // Extra argument.
358
[email protected]3c788582013-01-25 21:51:35359 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29360 EXPECT_TRUE(listener.ExpectMessage(0, MsgClassSI::ID));
361
[email protected]95cb7fb92008-12-09 22:00:47362 // Now send a well formed message to make sure the receiver wasn't
363 // thrown out of sync by the extra argument.
thestigf84f17f2015-03-11 20:41:55364 msg = new MsgClassIS(3, base::ASCIIToUTF16("expect three"));
[email protected]3c788582013-01-25 21:51:35365 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29366 EXPECT_TRUE(listener.ExpectMessage(3, MsgClassIS::ID));
367
[email protected]3c788582013-01-25 21:51:35368 EXPECT_TRUE(WaitForClientShutdown());
369 DestroyChannel();
initial.commit09911bf2008-07-26 23:55:29370}
371
[email protected]2a3aa7b52013-01-11 20:56:22372} // namespace