blob: 00e91f3a25ecee071163e7dc6587b99bc8843bf7 [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>
danakj03de39b22016-04-23 04:21:099#include <memory>
initial.commit09911bf2008-07-26 23:55:2910#include <sstream>
tfarina10a5c062015-09-04 18:47:5711#include <string>
initial.commit09911bf2008-07-26 23:55:2912
[email protected]2a9ec0e2013-07-17 23:00:3013#include "base/message_loop/message_loop.h"
thestigf84f17f2015-03-11 20:41:5514#include "base/strings/string16.h"
15#include "base/strings/utf_string_conversions.h"
[email protected]f214f8792011-01-01 02:17:0816#include "base/threading/platform_thread.h"
avi246998d82015-12-22 02:39:0417#include "build/build_config.h"
[email protected]0cb7d8c82013-01-11 15:13:3718#include "ipc/ipc_test_base.h"
initial.commit09911bf2008-07-26 23:55:2919#include "testing/gtest/include/gtest/gtest.h"
20
[email protected]2a3aa7b52013-01-11 20:56:2221// IPC messages for testing ----------------------------------------------------
[email protected]1d4ecf42011-08-26 21:27:3022
23#define IPC_MESSAGE_IMPL
24#include "ipc/ipc_message_macros.h"
25
26#define IPC_MESSAGE_START TestMsgStart
27
thestigf84f17f2015-03-11 20:41:5528// Generic message class that is an int followed by a string16.
29IPC_MESSAGE_CONTROL2(MsgClassIS, int, base::string16)
[email protected]1d4ecf42011-08-26 21:27:3030
thestigf84f17f2015-03-11 20:41:5531// Generic message class that is a string16 followed by an int.
32IPC_MESSAGE_CONTROL2(MsgClassSI, base::string16, int)
[email protected]1d4ecf42011-08-26 21:27:3033
34// Message to create a mutex in the IPC server, using the received name.
thestigf84f17f2015-03-11 20:41:5535IPC_MESSAGE_CONTROL2(MsgDoMutex, base::string16, int)
[email protected]1d4ecf42011-08-26 21:27:3036
37// Used to generate an ID for a message that should not exist.
38IPC_MESSAGE_CONTROL0(MsgUnhandled)
39
[email protected]2a3aa7b52013-01-11 20:56:2240// -----------------------------------------------------------------------------
41
42namespace {
[email protected]1d4ecf42011-08-26 21:27:3043
initial.commit09911bf2008-07-26 23:55:2944TEST(IPCMessageIntegrity, ReadBeyondBufferStr) {
thestigf84f17f2015-03-11 20:41:5545 // This was BUG 984408.
tfarina7023f522015-09-11 19:58:4846 uint32_t v1 = std::numeric_limits<uint32_t>::max() - 1;
initial.commit09911bf2008-07-26 23:55:2947 int v2 = 666;
[email protected]753bb252013-11-04 22:28:1248 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:2949 EXPECT_TRUE(m.WriteInt(v1));
50 EXPECT_TRUE(m.WriteInt(v2));
51
brettwbd4d7112015-06-03 04:29:2552 base::PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2953 std::string vs;
avi48fc13b2014-12-28 23:31:4854 EXPECT_FALSE(iter.ReadString(&vs));
initial.commit09911bf2008-07-26 23:55:2955}
56
thestigf84f17f2015-03-11 20:41:5557TEST(IPCMessageIntegrity, ReadBeyondBufferStr16) {
58 // This was BUG 984408.
tfarina7023f522015-09-11 19:58:4859 uint32_t v1 = std::numeric_limits<uint32_t>::max() - 1;
initial.commit09911bf2008-07-26 23:55:2960 int v2 = 777;
[email protected]753bb252013-11-04 22:28:1261 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:2962 EXPECT_TRUE(m.WriteInt(v1));
63 EXPECT_TRUE(m.WriteInt(v2));
64
brettwbd4d7112015-06-03 04:29:2565 base::PickleIterator iter(m);
thestigf84f17f2015-03-11 20:41:5566 base::string16 vs;
67 EXPECT_FALSE(iter.ReadString16(&vs));
initial.commit09911bf2008-07-26 23:55:2968}
69
70TEST(IPCMessageIntegrity, ReadBytesBadIterator) {
71 // This was BUG 1035467.
[email protected]753bb252013-11-04 22:28:1272 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:2973 EXPECT_TRUE(m.WriteInt(1));
74 EXPECT_TRUE(m.WriteInt(2));
75
brettwbd4d7112015-06-03 04:29:2576 base::PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2977 const char* data = NULL;
avi48fc13b2014-12-28 23:31:4878 EXPECT_TRUE(iter.ReadBytes(&data, sizeof(int)));
initial.commit09911bf2008-07-26 23:55:2979}
80
81TEST(IPCMessageIntegrity, ReadVectorNegativeSize) {
82 // A slight variation of BUG 984408. Note that the pickling of vector<char>
83 // has a specialized template which is not vulnerable to this bug. So here
84 // try to hit the non-specialized case vector<P>.
[email protected]753bb252013-11-04 22:28:1285 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:2986 EXPECT_TRUE(m.WriteInt(-1)); // This is the count of elements.
87 EXPECT_TRUE(m.WriteInt(1));
88 EXPECT_TRUE(m.WriteInt(2));
89 EXPECT_TRUE(m.WriteInt(3));
90
91 std::vector<double> vec;
brettwbd4d7112015-06-03 04:29:2592 base::PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:2993 EXPECT_FALSE(ReadParam(&m, &iter, &vec));
94}
95
tfarina8514f0d2015-07-28 14:41:4796#if defined(OS_ANDROID)
97#define MAYBE_ReadVectorTooLarge1 DISABLED_ReadVectorTooLarge1
98#else
99#define MAYBE_ReadVectorTooLarge1 ReadVectorTooLarge1
100#endif
101TEST(IPCMessageIntegrity, MAYBE_ReadVectorTooLarge1) {
initial.commit09911bf2008-07-26 23:55:29102 // This was BUG 1006367. This is the large but positive length case. Again
103 // we try to hit the non-specialized case vector<P>.
[email protected]753bb252013-11-04 22:28:12104 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:29105 EXPECT_TRUE(m.WriteInt(0x21000003)); // This is the count of elements.
106 EXPECT_TRUE(m.WriteInt64(1));
107 EXPECT_TRUE(m.WriteInt64(2));
108
tfarina10a5c062015-09-04 18:47:57109 std::vector<int64_t> vec;
brettwbd4d7112015-06-03 04:29:25110 base::PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:29111 EXPECT_FALSE(ReadParam(&m, &iter, &vec));
112}
113
114TEST(IPCMessageIntegrity, ReadVectorTooLarge2) {
115 // This was BUG 1006367. This is the large but positive with an additional
116 // integer overflow when computing the actual byte size. Again we try to hit
117 // the non-specialized case vector<P>.
[email protected]753bb252013-11-04 22:28:12118 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:29119 EXPECT_TRUE(m.WriteInt(0x71000000)); // This is the count of elements.
120 EXPECT_TRUE(m.WriteInt64(1));
121 EXPECT_TRUE(m.WriteInt64(2));
122
tfarina10a5c062015-09-04 18:47:57123 std::vector<int64_t> vec;
brettwbd4d7112015-06-03 04:29:25124 base::PickleIterator iter(m);
initial.commit09911bf2008-07-26 23:55:29125 EXPECT_FALSE(ReadParam(&m, &iter, &vec));
126}
127
[email protected]57319ce2012-06-11 22:35:26128class SimpleListener : public IPC::Listener {
initial.commit09911bf2008-07-26 23:55:29129 public:
130 SimpleListener() : other_(NULL) {
131 }
[email protected]57319ce2012-06-11 22:35:26132 void Init(IPC::Sender* s) {
initial.commit09911bf2008-07-26 23:55:29133 other_ = s;
134 }
135 protected:
[email protected]57319ce2012-06-11 22:35:26136 IPC::Sender* other_;
initial.commit09911bf2008-07-26 23:55:29137};
138
139enum {
140 FUZZER_ROUTING_ID = 5
141};
142
143// The fuzzer server class. It runs in a child process and expects
144// only two IPC calls; after that it exits the message loop which
145// terminates the child process.
146class FuzzerServerListener : public SimpleListener {
147 public:
148 FuzzerServerListener() : message_count_(2), pending_messages_(0) {
149 }
dchengfe61fca2014-10-22 02:29:52150 bool OnMessageReceived(const IPC::Message& msg) override {
initial.commit09911bf2008-07-26 23:55:29151 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
152 ++pending_messages_;
153 IPC_BEGIN_MESSAGE_MAP(FuzzerServerListener, msg)
154 IPC_MESSAGE_HANDLER(MsgClassIS, OnMsgClassISMessage)
155 IPC_MESSAGE_HANDLER(MsgClassSI, OnMsgClassSIMessage)
156 IPC_END_MESSAGE_MAP()
157 if (pending_messages_) {
158 // Probably a problem de-serializing the message.
159 ReplyMsgNotHandled(msg.type());
160 }
161 }
[email protected]a95986a82010-12-24 06:19:28162 return true;
initial.commit09911bf2008-07-26 23:55:29163 }
164
165 private:
thestigf84f17f2015-03-11 20:41:55166 void OnMsgClassISMessage(int value, const base::string16& text) {
initial.commit09911bf2008-07-26 23:55:29167 UseData(MsgClassIS::ID, value, text);
168 RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassIS::ID, value);
169 Cleanup();
170 }
171
thestigf84f17f2015-03-11 20:41:55172 void OnMsgClassSIMessage(const base::string16& text, int value) {
initial.commit09911bf2008-07-26 23:55:29173 UseData(MsgClassSI::ID, value, text);
174 RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassSI::ID, value);
175 Cleanup();
176 }
177
tfarina10a5c062015-09-04 18:47:57178 bool RoundtripAckReply(int routing, uint32_t type_id, int reply) {
[email protected]753bb252013-11-04 22:28:12179 IPC::Message* message = new IPC::Message(routing, type_id,
180 IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:29181 message->WriteInt(reply + 1);
182 message->WriteInt(reply);
183 return other_->Send(message);
184 }
185
186 void Cleanup() {
187 --message_count_;
188 --pending_messages_;
189 if (0 == message_count_)
ki.stfua21ed8c2015-10-12 17:26:00190 base::MessageLoop::current()->QuitWhenIdle();
initial.commit09911bf2008-07-26 23:55:29191 }
192
tfarina10a5c062015-09-04 18:47:57193 void ReplyMsgNotHandled(uint32_t type_id) {
[email protected]1d4ecf42011-08-26 21:27:30194 RoundtripAckReply(FUZZER_ROUTING_ID, MsgUnhandled::ID, type_id);
initial.commit09911bf2008-07-26 23:55:29195 Cleanup();
196 }
197
thestigf84f17f2015-03-11 20:41:55198 void UseData(int caller, int value, const base::string16& text) {
199 std::ostringstream os;
200 os << "IPC fuzzer:" << caller << " [" << value << " "
201 << base::UTF16ToUTF8(text) << "]\n";
202 std::string output = os.str();
203 LOG(WARNING) << output;
204 }
initial.commit09911bf2008-07-26 23:55:29205
206 int message_count_;
207 int pending_messages_;
208};
209
210class FuzzerClientListener : public SimpleListener {
211 public:
212 FuzzerClientListener() : last_msg_(NULL) {
213 }
214
dchengfe61fca2014-10-22 02:29:52215 bool OnMessageReceived(const IPC::Message& msg) override {
initial.commit09911bf2008-07-26 23:55:29216 last_msg_ = new IPC::Message(msg);
ki.stfua21ed8c2015-10-12 17:26:00217 base::MessageLoop::current()->QuitWhenIdle();
[email protected]a95986a82010-12-24 06:19:28218 return true;
initial.commit09911bf2008-07-26 23:55:29219 }
220
tfarina10a5c062015-09-04 18:47:57221 bool ExpectMessage(int value, uint32_t type_id) {
initial.commit09911bf2008-07-26 23:55:29222 if (!MsgHandlerInternal(type_id))
223 return false;
224 int msg_value1 = 0;
225 int msg_value2 = 0;
brettwbd4d7112015-06-03 04:29:25226 base::PickleIterator iter(*last_msg_);
avi48fc13b2014-12-28 23:31:48227 if (!iter.ReadInt(&msg_value1))
initial.commit09911bf2008-07-26 23:55:29228 return false;
avi48fc13b2014-12-28 23:31:48229 if (!iter.ReadInt(&msg_value2))
initial.commit09911bf2008-07-26 23:55:29230 return false;
231 if ((msg_value2 + 1) != msg_value1)
232 return false;
233 if (msg_value2 != value)
234 return false;
235
236 delete last_msg_;
237 last_msg_ = NULL;
238 return true;
239 }
240
tfarina10a5c062015-09-04 18:47:57241 bool ExpectMsgNotHandled(uint32_t type_id) {
[email protected]1d4ecf42011-08-26 21:27:30242 return ExpectMessage(type_id, MsgUnhandled::ID);
initial.commit09911bf2008-07-26 23:55:29243 }
244
245 private:
tfarina10a5c062015-09-04 18:47:57246 bool MsgHandlerInternal(uint32_t type_id) {
[email protected]fd0a773a2013-04-30 20:55:03247 base::MessageLoop::current()->Run();
initial.commit09911bf2008-07-26 23:55:29248 if (NULL == last_msg_)
249 return false;
250 if (FUZZER_ROUTING_ID != last_msg_->routing_id())
251 return false;
252 return (type_id == last_msg_->type());
thestigf84f17f2015-03-11 20:41:55253 }
initial.commit09911bf2008-07-26 23:55:29254
255 IPC::Message* last_msg_;
256};
257
[email protected]3c788582013-01-25 21:51:35258// Runs the fuzzing server child mode. Returns when the preset number of
259// messages have been received.
260MULTIPROCESS_IPC_TEST_CLIENT_MAIN(FuzzServerClient) {
[email protected]fd0a773a2013-04-30 20:55:03261 base::MessageLoopForIO main_message_loop;
initial.commit09911bf2008-07-26 23:55:29262 FuzzerServerListener listener;
danakj03de39b22016-04-23 04:21:09263 std::unique_ptr<IPC::Channel> channel(IPC::Channel::CreateClient(
erikchen30dc2812015-09-24 03:26:38264 IPCTestBase::GetChannelName("FuzzServerClient"), &listener));
[email protected]e482111a82014-05-30 03:58:59265 CHECK(channel->Connect());
266 listener.Init(channel.get());
[email protected]fd0a773a2013-04-30 20:55:03267 base::MessageLoop::current()->Run();
[email protected]95cb7fb92008-12-09 22:00:47268 return 0;
initial.commit09911bf2008-07-26 23:55:29269}
270
[email protected]0cb7d8c82013-01-11 15:13:37271class IPCFuzzingTest : public IPCTestBase {
[email protected]95cb7fb92008-12-09 22:00:47272};
273
tfarina8514f0d2015-07-28 14:41:47274#if defined(OS_ANDROID)
275#define MAYBE_SanityTest DISABLED_SanityTest
276#else
277#define MAYBE_SanityTest SanityTest
278#endif
initial.commit09911bf2008-07-26 23:55:29279// This test makes sure that the FuzzerClientListener and FuzzerServerListener
280// are working properly by generating two well formed IPC calls.
tfarina8514f0d2015-07-28 14:41:47281TEST_F(IPCFuzzingTest, MAYBE_SanityTest) {
[email protected]3c788582013-01-25 21:51:35282 Init("FuzzServerClient");
283
[email protected]df3c1ca12008-12-19 21:37:01284 FuzzerClientListener listener;
[email protected]3c788582013-01-25 21:51:35285 CreateChannel(&listener);
286 listener.Init(channel());
287 ASSERT_TRUE(ConnectChannel());
288 ASSERT_TRUE(StartClient());
initial.commit09911bf2008-07-26 23:55:29289
290 IPC::Message* msg = NULL;
291 int value = 43;
thestigf84f17f2015-03-11 20:41:55292 msg = new MsgClassIS(value, base::ASCIIToUTF16("expect 43"));
[email protected]3c788582013-01-25 21:51:35293 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29294 EXPECT_TRUE(listener.ExpectMessage(value, MsgClassIS::ID));
295
thestigf84f17f2015-03-11 20:41:55296 msg = new MsgClassSI(base::ASCIIToUTF16("expect 44"), ++value);
[email protected]3c788582013-01-25 21:51:35297 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29298 EXPECT_TRUE(listener.ExpectMessage(value, MsgClassSI::ID));
299
[email protected]3c788582013-01-25 21:51:35300 EXPECT_TRUE(WaitForClientShutdown());
301 DestroyChannel();
initial.commit09911bf2008-07-26 23:55:29302}
303
tfarina8514f0d2015-07-28 14:41:47304#if defined(OS_ANDROID)
305#define MAYBE_MsgBadPayloadShort DISABLED_MsgBadPayloadShort
306#else
307#define MAYBE_MsgBadPayloadShort MsgBadPayloadShort
308#endif
[email protected]3c788582013-01-25 21:51:35309// This test uses a payload that is smaller than expected. This generates an
310// error while unpacking the IPC buffer which in debug trigger an assertion and
311// in release is ignored (!). Right after we generate another valid IPC to make
312// sure framing is working properly.
[email protected]20960e072011-09-20 20:59:01313#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
tfarina8514f0d2015-07-28 14:41:47314TEST_F(IPCFuzzingTest, MAYBE_MsgBadPayloadShort) {
[email protected]3c788582013-01-25 21:51:35315 Init("FuzzServerClient");
316
[email protected]df3c1ca12008-12-19 21:37:01317 FuzzerClientListener listener;
[email protected]3c788582013-01-25 21:51:35318 CreateChannel(&listener);
319 listener.Init(channel());
320 ASSERT_TRUE(ConnectChannel());
321 ASSERT_TRUE(StartClient());
initial.commit09911bf2008-07-26 23:55:29322
[email protected]753bb252013-11-04 22:28:12323 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassIS::ID,
324 IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:29325 msg->WriteInt(666);
[email protected]3c788582013-01-25 21:51:35326 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29327 EXPECT_TRUE(listener.ExpectMsgNotHandled(MsgClassIS::ID));
328
thestigf84f17f2015-03-11 20:41:55329 msg = new MsgClassSI(base::ASCIIToUTF16("expect one"), 1);
[email protected]3c788582013-01-25 21:51:35330 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29331 EXPECT_TRUE(listener.ExpectMessage(1, MsgClassSI::ID));
332
[email protected]3c788582013-01-25 21:51:35333 EXPECT_TRUE(WaitForClientShutdown());
334 DestroyChannel();
initial.commit09911bf2008-07-26 23:55:29335}
[email protected]20960e072011-09-20 20:59:01336#endif
initial.commit09911bf2008-07-26 23:55:29337
tfarina8514f0d2015-07-28 14:41:47338#if defined(OS_ANDROID)
339#define MAYBE_MsgBadPayloadArgs DISABLED_MsgBadPayloadArgs
340#else
341#define MAYBE_MsgBadPayloadArgs MsgBadPayloadArgs
342#endif
[email protected]3c788582013-01-25 21:51:35343// This test uses a payload that has too many arguments, but so the payload size
344// is big enough so the unpacking routine does not generate an error as in the
345// case of MsgBadPayloadShort test. This test does not pinpoint a flaw (per se)
346// as by design we don't carry type information on the IPC message.
tfarina8514f0d2015-07-28 14:41:47347TEST_F(IPCFuzzingTest, MAYBE_MsgBadPayloadArgs) {
[email protected]3c788582013-01-25 21:51:35348 Init("FuzzServerClient");
349
[email protected]df3c1ca12008-12-19 21:37:01350 FuzzerClientListener listener;
[email protected]3c788582013-01-25 21:51:35351 CreateChannel(&listener);
352 listener.Init(channel());
353 ASSERT_TRUE(ConnectChannel());
354 ASSERT_TRUE(StartClient());
initial.commit09911bf2008-07-26 23:55:29355
[email protected]753bb252013-11-04 22:28:12356 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassSI::ID,
357 IPC::Message::PRIORITY_NORMAL);
thestigf84f17f2015-03-11 20:41:55358 msg->WriteString16(base::ASCIIToUTF16("d"));
initial.commit09911bf2008-07-26 23:55:29359 msg->WriteInt(0);
[email protected]95cb7fb92008-12-09 22:00:47360 msg->WriteInt(0x65); // Extra argument.
361
[email protected]3c788582013-01-25 21:51:35362 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29363 EXPECT_TRUE(listener.ExpectMessage(0, MsgClassSI::ID));
364
[email protected]95cb7fb92008-12-09 22:00:47365 // Now send a well formed message to make sure the receiver wasn't
366 // thrown out of sync by the extra argument.
thestigf84f17f2015-03-11 20:41:55367 msg = new MsgClassIS(3, base::ASCIIToUTF16("expect three"));
[email protected]3c788582013-01-25 21:51:35368 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29369 EXPECT_TRUE(listener.ExpectMessage(3, MsgClassIS::ID));
370
[email protected]3c788582013-01-25 21:51:35371 EXPECT_TRUE(WaitForClientShutdown());
372 DestroyChannel();
initial.commit09911bf2008-07-26 23:55:29373}
374
[email protected]2a3aa7b52013-01-11 20:56:22375} // namespace