blob: d3ff1a0103ac4602b1c5d8f65791606ec1a8ea7d [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"
thestigf84f17f2015-03-11 20:41:5510#include "base/strings/string16.h"
11#include "base/strings/utf_string_conversions.h"
[email protected]f214f8792011-01-01 02:17:0812#include "base/threading/platform_thread.h"
[email protected]0cb7d8c82013-01-11 15:13:3713#include "ipc/ipc_test_base.h"
initial.commit09911bf2008-07-26 23:55:2914#include "testing/gtest/include/gtest/gtest.h"
15
[email protected]2a3aa7b52013-01-11 20:56:2216// IPC messages for testing ----------------------------------------------------
[email protected]1d4ecf42011-08-26 21:27:3017
18#define IPC_MESSAGE_IMPL
19#include "ipc/ipc_message_macros.h"
20
21#define IPC_MESSAGE_START TestMsgStart
22
thestigf84f17f2015-03-11 20:41:5523// Generic message class that is an int followed by a string16.
24IPC_MESSAGE_CONTROL2(MsgClassIS, int, base::string16)
[email protected]1d4ecf42011-08-26 21:27:3025
thestigf84f17f2015-03-11 20:41:5526// Generic message class that is a string16 followed by an int.
27IPC_MESSAGE_CONTROL2(MsgClassSI, base::string16, int)
[email protected]1d4ecf42011-08-26 21:27:3028
29// Message to create a mutex in the IPC server, using the received name.
thestigf84f17f2015-03-11 20:41:5530IPC_MESSAGE_CONTROL2(MsgDoMutex, base::string16, int)
[email protected]1d4ecf42011-08-26 21:27:3031
32// Used to generate an ID for a message that should not exist.
33IPC_MESSAGE_CONTROL0(MsgUnhandled)
34
[email protected]2a3aa7b52013-01-11 20:56:2235// -----------------------------------------------------------------------------
36
37namespace {
[email protected]1d4ecf42011-08-26 21:27:3038
initial.commit09911bf2008-07-26 23:55:2939TEST(IPCMessageIntegrity, ReadBeyondBufferStr) {
thestigf84f17f2015-03-11 20:41:5540 // This was BUG 984408.
initial.commit09911bf2008-07-26 23:55:2941 uint32 v1 = kuint32max - 1;
42 int v2 = 666;
[email protected]753bb252013-11-04 22:28:1243 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:2944 EXPECT_TRUE(m.WriteInt(v1));
45 EXPECT_TRUE(m.WriteInt(v2));
46
brettwbd4d7112015-06-03 04:29:2547 base::PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2948 std::string vs;
avi48fc13b2014-12-28 23:31:4849 EXPECT_FALSE(iter.ReadString(&vs));
initial.commit09911bf2008-07-26 23:55:2950}
51
thestigf84f17f2015-03-11 20:41:5552TEST(IPCMessageIntegrity, ReadBeyondBufferStr16) {
53 // This was BUG 984408.
initial.commit09911bf2008-07-26 23:55:2954 uint32 v1 = kuint32max - 1;
55 int v2 = 777;
[email protected]753bb252013-11-04 22:28:1256 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:2957 EXPECT_TRUE(m.WriteInt(v1));
58 EXPECT_TRUE(m.WriteInt(v2));
59
brettwbd4d7112015-06-03 04:29:2560 base::PickleIterator iter(m);
thestigf84f17f2015-03-11 20:41:5561 base::string16 vs;
62 EXPECT_FALSE(iter.ReadString16(&vs));
initial.commit09911bf2008-07-26 23:55:2963}
64
65TEST(IPCMessageIntegrity, ReadBytesBadIterator) {
66 // This was BUG 1035467.
[email protected]753bb252013-11-04 22:28:1267 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:2968 EXPECT_TRUE(m.WriteInt(1));
69 EXPECT_TRUE(m.WriteInt(2));
70
brettwbd4d7112015-06-03 04:29:2571 base::PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2972 const char* data = NULL;
avi48fc13b2014-12-28 23:31:4873 EXPECT_TRUE(iter.ReadBytes(&data, sizeof(int)));
initial.commit09911bf2008-07-26 23:55:2974}
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>.
[email protected]753bb252013-11-04 22:28:1280 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:2981 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;
brettwbd4d7112015-06-03 04:29:2587 base::PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2988 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>.
[email protected]753bb252013-11-04 22:28:1294 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:2995 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;
brettwbd4d7112015-06-03 04:29:25100 base::PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:29101 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>.
[email protected]753bb252013-11-04 22:28:12108 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:29109 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;
brettwbd4d7112015-06-03 04:29:25114 base::PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:29115 EXPECT_FALSE(ReadParam(&m, &iter, &vec));
116}
117
[email protected]57319ce2012-06-11 22:35:26118class SimpleListener : public IPC::Listener {
initial.commit09911bf2008-07-26 23:55:29119 public:
120 SimpleListener() : other_(NULL) {
121 }
[email protected]57319ce2012-06-11 22:35:26122 void Init(IPC::Sender* s) {
initial.commit09911bf2008-07-26 23:55:29123 other_ = s;
124 }
125 protected:
[email protected]57319ce2012-06-11 22:35:26126 IPC::Sender* other_;
initial.commit09911bf2008-07-26 23:55:29127};
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 }
dchengfe61fca2014-10-22 02:29:52140 bool OnMessageReceived(const IPC::Message& msg) override {
initial.commit09911bf2008-07-26 23:55:29141 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 }
[email protected]a95986a82010-12-24 06:19:28152 return true;
initial.commit09911bf2008-07-26 23:55:29153 }
154
155 private:
thestigf84f17f2015-03-11 20:41:55156 void OnMsgClassISMessage(int value, const base::string16& text) {
initial.commit09911bf2008-07-26 23:55:29157 UseData(MsgClassIS::ID, value, text);
158 RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassIS::ID, value);
159 Cleanup();
160 }
161
thestigf84f17f2015-03-11 20:41:55162 void OnMsgClassSIMessage(const base::string16& text, int value) {
initial.commit09911bf2008-07-26 23:55:29163 UseData(MsgClassSI::ID, value, text);
164 RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassSI::ID, value);
165 Cleanup();
166 }
167
[email protected]7ee1a44c2010-07-23 14:18:59168 bool RoundtripAckReply(int routing, uint32 type_id, int reply) {
[email protected]753bb252013-11-04 22:28:12169 IPC::Message* message = new IPC::Message(routing, type_id,
170 IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:29171 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_)
[email protected]fd0a773a2013-04-30 20:55:03180 base::MessageLoop::current()->Quit();
initial.commit09911bf2008-07-26 23:55:29181 }
182
[email protected]7ee1a44c2010-07-23 14:18:59183 void ReplyMsgNotHandled(uint32 type_id) {
[email protected]1d4ecf42011-08-26 21:27:30184 RoundtripAckReply(FUZZER_ROUTING_ID, MsgUnhandled::ID, type_id);
initial.commit09911bf2008-07-26 23:55:29185 Cleanup();
186 }
187
thestigf84f17f2015-03-11 20:41:55188 void UseData(int caller, int value, const base::string16& text) {
189 std::ostringstream os;
190 os << "IPC fuzzer:" << caller << " [" << value << " "
191 << base::UTF16ToUTF8(text) << "]\n";
192 std::string output = os.str();
193 LOG(WARNING) << output;
194 }
initial.commit09911bf2008-07-26 23:55:29195
196 int message_count_;
197 int pending_messages_;
198};
199
200class FuzzerClientListener : public SimpleListener {
201 public:
202 FuzzerClientListener() : last_msg_(NULL) {
203 }
204
dchengfe61fca2014-10-22 02:29:52205 bool OnMessageReceived(const IPC::Message& msg) override {
initial.commit09911bf2008-07-26 23:55:29206 last_msg_ = new IPC::Message(msg);
[email protected]fd0a773a2013-04-30 20:55:03207 base::MessageLoop::current()->Quit();
[email protected]a95986a82010-12-24 06:19:28208 return true;
initial.commit09911bf2008-07-26 23:55:29209 }
210
[email protected]7ee1a44c2010-07-23 14:18:59211 bool ExpectMessage(int value, uint32 type_id) {
initial.commit09911bf2008-07-26 23:55:29212 if (!MsgHandlerInternal(type_id))
213 return false;
214 int msg_value1 = 0;
215 int msg_value2 = 0;
brettwbd4d7112015-06-03 04:29:25216 base::PickleIterator iter(*last_msg_);
avi48fc13b2014-12-28 23:31:48217 if (!iter.ReadInt(&msg_value1))
initial.commit09911bf2008-07-26 23:55:29218 return false;
avi48fc13b2014-12-28 23:31:48219 if (!iter.ReadInt(&msg_value2))
initial.commit09911bf2008-07-26 23:55:29220 return false;
221 if ((msg_value2 + 1) != msg_value1)
222 return false;
223 if (msg_value2 != value)
224 return false;
225
226 delete last_msg_;
227 last_msg_ = NULL;
228 return true;
229 }
230
[email protected]7ee1a44c2010-07-23 14:18:59231 bool ExpectMsgNotHandled(uint32 type_id) {
[email protected]1d4ecf42011-08-26 21:27:30232 return ExpectMessage(type_id, MsgUnhandled::ID);
initial.commit09911bf2008-07-26 23:55:29233 }
234
235 private:
[email protected]7ee1a44c2010-07-23 14:18:59236 bool MsgHandlerInternal(uint32 type_id) {
[email protected]fd0a773a2013-04-30 20:55:03237 base::MessageLoop::current()->Run();
initial.commit09911bf2008-07-26 23:55:29238 if (NULL == last_msg_)
239 return false;
240 if (FUZZER_ROUTING_ID != last_msg_->routing_id())
241 return false;
242 return (type_id == last_msg_->type());
thestigf84f17f2015-03-11 20:41:55243 }
initial.commit09911bf2008-07-26 23:55:29244
245 IPC::Message* last_msg_;
246};
247
[email protected]3c788582013-01-25 21:51:35248// Runs the fuzzing server child mode. Returns when the preset number of
249// messages have been received.
250MULTIPROCESS_IPC_TEST_CLIENT_MAIN(FuzzServerClient) {
[email protected]fd0a773a2013-04-30 20:55:03251 base::MessageLoopForIO main_message_loop;
initial.commit09911bf2008-07-26 23:55:29252 FuzzerServerListener listener;
[email protected]e482111a82014-05-30 03:58:59253 scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient(
erikchen27aa7d82015-06-16 21:21:04254 IPCTestBase::GetChannelName("FuzzServerClient"), &listener, nullptr));
[email protected]e482111a82014-05-30 03:58:59255 CHECK(channel->Connect());
256 listener.Init(channel.get());
[email protected]fd0a773a2013-04-30 20:55:03257 base::MessageLoop::current()->Run();
[email protected]95cb7fb92008-12-09 22:00:47258 return 0;
initial.commit09911bf2008-07-26 23:55:29259}
260
[email protected]0cb7d8c82013-01-11 15:13:37261class IPCFuzzingTest : public IPCTestBase {
[email protected]95cb7fb92008-12-09 22:00:47262};
263
initial.commit09911bf2008-07-26 23:55:29264// This test makes sure that the FuzzerClientListener and FuzzerServerListener
265// are working properly by generating two well formed IPC calls.
[email protected]95cb7fb92008-12-09 22:00:47266TEST_F(IPCFuzzingTest, SanityTest) {
[email protected]3c788582013-01-25 21:51:35267 Init("FuzzServerClient");
268
[email protected]df3c1ca12008-12-19 21:37:01269 FuzzerClientListener listener;
[email protected]3c788582013-01-25 21:51:35270 CreateChannel(&listener);
271 listener.Init(channel());
272 ASSERT_TRUE(ConnectChannel());
273 ASSERT_TRUE(StartClient());
initial.commit09911bf2008-07-26 23:55:29274
275 IPC::Message* msg = NULL;
276 int value = 43;
thestigf84f17f2015-03-11 20:41:55277 msg = new MsgClassIS(value, base::ASCIIToUTF16("expect 43"));
[email protected]3c788582013-01-25 21:51:35278 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29279 EXPECT_TRUE(listener.ExpectMessage(value, MsgClassIS::ID));
280
thestigf84f17f2015-03-11 20:41:55281 msg = new MsgClassSI(base::ASCIIToUTF16("expect 44"), ++value);
[email protected]3c788582013-01-25 21:51:35282 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29283 EXPECT_TRUE(listener.ExpectMessage(value, MsgClassSI::ID));
284
[email protected]3c788582013-01-25 21:51:35285 EXPECT_TRUE(WaitForClientShutdown());
286 DestroyChannel();
initial.commit09911bf2008-07-26 23:55:29287}
288
[email protected]3c788582013-01-25 21:51:35289// This test uses a payload that is smaller than expected. This generates an
290// error while unpacking the IPC buffer which in debug trigger an assertion and
291// in release is ignored (!). Right after we generate another valid IPC to make
292// sure framing is working properly.
[email protected]20960e072011-09-20 20:59:01293#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
[email protected]95cb7fb92008-12-09 22:00:47294TEST_F(IPCFuzzingTest, MsgBadPayloadShort) {
[email protected]3c788582013-01-25 21:51:35295 Init("FuzzServerClient");
296
[email protected]df3c1ca12008-12-19 21:37:01297 FuzzerClientListener listener;
[email protected]3c788582013-01-25 21:51:35298 CreateChannel(&listener);
299 listener.Init(channel());
300 ASSERT_TRUE(ConnectChannel());
301 ASSERT_TRUE(StartClient());
initial.commit09911bf2008-07-26 23:55:29302
[email protected]753bb252013-11-04 22:28:12303 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassIS::ID,
304 IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:29305 msg->WriteInt(666);
[email protected]3c788582013-01-25 21:51:35306 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29307 EXPECT_TRUE(listener.ExpectMsgNotHandled(MsgClassIS::ID));
308
thestigf84f17f2015-03-11 20:41:55309 msg = new MsgClassSI(base::ASCIIToUTF16("expect one"), 1);
[email protected]3c788582013-01-25 21:51:35310 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29311 EXPECT_TRUE(listener.ExpectMessage(1, MsgClassSI::ID));
312
[email protected]3c788582013-01-25 21:51:35313 EXPECT_TRUE(WaitForClientShutdown());
314 DestroyChannel();
initial.commit09911bf2008-07-26 23:55:29315}
[email protected]20960e072011-09-20 20:59:01316#endif
initial.commit09911bf2008-07-26 23:55:29317
[email protected]3c788582013-01-25 21:51:35318// This test uses a payload that has too many arguments, but so the payload size
319// is big enough so the unpacking routine does not generate an error as in the
320// case of MsgBadPayloadShort test. This test does not pinpoint a flaw (per se)
321// as by design we don't carry type information on the IPC message.
[email protected]95cb7fb92008-12-09 22:00:47322TEST_F(IPCFuzzingTest, MsgBadPayloadArgs) {
[email protected]3c788582013-01-25 21:51:35323 Init("FuzzServerClient");
324
[email protected]df3c1ca12008-12-19 21:37:01325 FuzzerClientListener listener;
[email protected]3c788582013-01-25 21:51:35326 CreateChannel(&listener);
327 listener.Init(channel());
328 ASSERT_TRUE(ConnectChannel());
329 ASSERT_TRUE(StartClient());
initial.commit09911bf2008-07-26 23:55:29330
[email protected]753bb252013-11-04 22:28:12331 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassSI::ID,
332 IPC::Message::PRIORITY_NORMAL);
thestigf84f17f2015-03-11 20:41:55333 msg->WriteString16(base::ASCIIToUTF16("d"));
initial.commit09911bf2008-07-26 23:55:29334 msg->WriteInt(0);
[email protected]95cb7fb92008-12-09 22:00:47335 msg->WriteInt(0x65); // Extra argument.
336
[email protected]3c788582013-01-25 21:51:35337 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29338 EXPECT_TRUE(listener.ExpectMessage(0, MsgClassSI::ID));
339
[email protected]95cb7fb92008-12-09 22:00:47340 // Now send a well formed message to make sure the receiver wasn't
341 // thrown out of sync by the extra argument.
thestigf84f17f2015-03-11 20:41:55342 msg = new MsgClassIS(3, base::ASCIIToUTF16("expect three"));
[email protected]3c788582013-01-25 21:51:35343 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29344 EXPECT_TRUE(listener.ExpectMessage(3, MsgClassIS::ID));
345
[email protected]3c788582013-01-25 21:51:35346 EXPECT_TRUE(WaitForClientShutdown());
347 DestroyChannel();
initial.commit09911bf2008-07-26 23:55:29348}
349
[email protected]2a3aa7b52013-01-11 20:56:22350} // namespace