blob: ca8827b12b706c5774d63d1a1f9d853c391407db [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
fdoray8e32586852016-06-22 19:56:1613#include "base/run_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);
Daniel Cheng0d89f9222017-09-22 05:05:0749 m.WriteInt(v1);
50 m.WriteInt(v2);
initial.commit09911bf2008-07-26 23:55:2951
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);
Daniel Cheng0d89f9222017-09-22 05:05:0762 m.WriteInt(v1);
63 m.WriteInt(v2);
initial.commit09911bf2008-07-26 23:55:2964
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);
Daniel Cheng0d89f9222017-09-22 05:05:0773 m.WriteInt(1);
74 m.WriteInt(2);
initial.commit09911bf2008-07-26 23:55:2975
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);
Daniel Cheng0d89f9222017-09-22 05:05:0786 m.WriteInt(-1); // This is the count of elements.
87 m.WriteInt(1);
88 m.WriteInt(2);
89 m.WriteInt(3);
initial.commit09911bf2008-07-26 23:55:2990
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);
Daniel Cheng0d89f9222017-09-22 05:05:07105 m.WriteInt(0x21000003); // This is the count of elements.
106 m.WriteInt64(1);
107 m.WriteInt64(2);
initial.commit09911bf2008-07-26 23:55:29108
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);
Daniel Cheng0d89f9222017-09-22 05:05:07119 m.WriteInt(0x71000000); // This is the count of elements.
120 m.WriteInt64(1);
121 m.WriteInt64(2);
initial.commit09911bf2008-07-26 23:55:29122
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
zijiehe84eef4fa2017-07-18 20:39:50128// This test needs ~20 seconds in Debug mode, or ~4 seconds in Release mode.
129// See http://crbug.com/741866 for details.
130TEST(IPCMessageIntegrity, DISABLED_ReadVectorTooLarge3) {
131 base::Pickle pickle;
132 IPC::WriteParam(&pickle, 256 * 1024 * 1024);
133 IPC::WriteParam(&pickle, 0);
134 IPC::WriteParam(&pickle, 1);
135 IPC::WriteParam(&pickle, 2);
136
137 base::PickleIterator iter(pickle);
138 std::vector<int> vec;
139 EXPECT_FALSE(IPC::ReadParam(&pickle, &iter, &vec));
140}
141
[email protected]57319ce2012-06-11 22:35:26142class SimpleListener : public IPC::Listener {
initial.commit09911bf2008-07-26 23:55:29143 public:
144 SimpleListener() : other_(NULL) {
145 }
[email protected]57319ce2012-06-11 22:35:26146 void Init(IPC::Sender* s) {
initial.commit09911bf2008-07-26 23:55:29147 other_ = s;
148 }
149 protected:
[email protected]57319ce2012-06-11 22:35:26150 IPC::Sender* other_;
initial.commit09911bf2008-07-26 23:55:29151};
152
153enum {
154 FUZZER_ROUTING_ID = 5
155};
156
157// The fuzzer server class. It runs in a child process and expects
158// only two IPC calls; after that it exits the message loop which
159// terminates the child process.
160class FuzzerServerListener : public SimpleListener {
161 public:
162 FuzzerServerListener() : message_count_(2), pending_messages_(0) {
163 }
dchengfe61fca2014-10-22 02:29:52164 bool OnMessageReceived(const IPC::Message& msg) override {
initial.commit09911bf2008-07-26 23:55:29165 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
166 ++pending_messages_;
167 IPC_BEGIN_MESSAGE_MAP(FuzzerServerListener, msg)
168 IPC_MESSAGE_HANDLER(MsgClassIS, OnMsgClassISMessage)
169 IPC_MESSAGE_HANDLER(MsgClassSI, OnMsgClassSIMessage)
170 IPC_END_MESSAGE_MAP()
171 if (pending_messages_) {
172 // Probably a problem de-serializing the message.
173 ReplyMsgNotHandled(msg.type());
174 }
175 }
[email protected]a95986a82010-12-24 06:19:28176 return true;
initial.commit09911bf2008-07-26 23:55:29177 }
178
179 private:
thestigf84f17f2015-03-11 20:41:55180 void OnMsgClassISMessage(int value, const base::string16& text) {
initial.commit09911bf2008-07-26 23:55:29181 UseData(MsgClassIS::ID, value, text);
182 RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassIS::ID, value);
183 Cleanup();
184 }
185
thestigf84f17f2015-03-11 20:41:55186 void OnMsgClassSIMessage(const base::string16& text, int value) {
initial.commit09911bf2008-07-26 23:55:29187 UseData(MsgClassSI::ID, value, text);
188 RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassSI::ID, value);
189 Cleanup();
190 }
191
tfarina10a5c062015-09-04 18:47:57192 bool RoundtripAckReply(int routing, uint32_t type_id, int reply) {
[email protected]753bb252013-11-04 22:28:12193 IPC::Message* message = new IPC::Message(routing, type_id,
194 IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:29195 message->WriteInt(reply + 1);
196 message->WriteInt(reply);
197 return other_->Send(message);
198 }
199
200 void Cleanup() {
201 --message_count_;
202 --pending_messages_;
203 if (0 == message_count_)
Gabriel Charette53a9ef812017-07-26 12:36:23204 base::RunLoop::QuitCurrentWhenIdleDeprecated();
initial.commit09911bf2008-07-26 23:55:29205 }
206
tfarina10a5c062015-09-04 18:47:57207 void ReplyMsgNotHandled(uint32_t type_id) {
[email protected]1d4ecf42011-08-26 21:27:30208 RoundtripAckReply(FUZZER_ROUTING_ID, MsgUnhandled::ID, type_id);
initial.commit09911bf2008-07-26 23:55:29209 Cleanup();
210 }
211
thestigf84f17f2015-03-11 20:41:55212 void UseData(int caller, int value, const base::string16& text) {
213 std::ostringstream os;
214 os << "IPC fuzzer:" << caller << " [" << value << " "
215 << base::UTF16ToUTF8(text) << "]\n";
216 std::string output = os.str();
217 LOG(WARNING) << output;
218 }
initial.commit09911bf2008-07-26 23:55:29219
220 int message_count_;
221 int pending_messages_;
222};
223
224class FuzzerClientListener : public SimpleListener {
225 public:
226 FuzzerClientListener() : last_msg_(NULL) {
227 }
228
dchengfe61fca2014-10-22 02:29:52229 bool OnMessageReceived(const IPC::Message& msg) override {
initial.commit09911bf2008-07-26 23:55:29230 last_msg_ = new IPC::Message(msg);
Gabriel Charette53a9ef812017-07-26 12:36:23231 base::RunLoop::QuitCurrentWhenIdleDeprecated();
[email protected]a95986a82010-12-24 06:19:28232 return true;
initial.commit09911bf2008-07-26 23:55:29233 }
234
tfarina10a5c062015-09-04 18:47:57235 bool ExpectMessage(int value, uint32_t type_id) {
initial.commit09911bf2008-07-26 23:55:29236 if (!MsgHandlerInternal(type_id))
237 return false;
238 int msg_value1 = 0;
239 int msg_value2 = 0;
brettwbd4d7112015-06-03 04:29:25240 base::PickleIterator iter(*last_msg_);
avi48fc13b2014-12-28 23:31:48241 if (!iter.ReadInt(&msg_value1))
initial.commit09911bf2008-07-26 23:55:29242 return false;
avi48fc13b2014-12-28 23:31:48243 if (!iter.ReadInt(&msg_value2))
initial.commit09911bf2008-07-26 23:55:29244 return false;
245 if ((msg_value2 + 1) != msg_value1)
246 return false;
247 if (msg_value2 != value)
248 return false;
249
250 delete last_msg_;
251 last_msg_ = NULL;
252 return true;
253 }
254
tfarina10a5c062015-09-04 18:47:57255 bool ExpectMsgNotHandled(uint32_t type_id) {
[email protected]1d4ecf42011-08-26 21:27:30256 return ExpectMessage(type_id, MsgUnhandled::ID);
initial.commit09911bf2008-07-26 23:55:29257 }
258
259 private:
tfarina10a5c062015-09-04 18:47:57260 bool MsgHandlerInternal(uint32_t type_id) {
fdoray8e32586852016-06-22 19:56:16261 base::RunLoop().Run();
initial.commit09911bf2008-07-26 23:55:29262 if (NULL == last_msg_)
263 return false;
264 if (FUZZER_ROUTING_ID != last_msg_->routing_id())
265 return false;
266 return (type_id == last_msg_->type());
thestigf84f17f2015-03-11 20:41:55267 }
initial.commit09911bf2008-07-26 23:55:29268
269 IPC::Message* last_msg_;
270};
271
[email protected]3c788582013-01-25 21:51:35272// Runs the fuzzing server child mode. Returns when the preset number of
273// messages have been received.
sammc4bcc4ed62016-10-27 10:13:59274DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(FuzzServerClient) {
initial.commit09911bf2008-07-26 23:55:29275 FuzzerServerListener listener;
sammc4bcc4ed62016-10-27 10:13:59276 Connect(&listener);
277 listener.Init(channel());
fdoray8e32586852016-06-22 19:56:16278 base::RunLoop().Run();
sammc4bcc4ed62016-10-27 10:13:59279 Close();
initial.commit09911bf2008-07-26 23:55:29280}
281
sammc4bcc4ed62016-10-27 10:13:59282using IPCFuzzingTest = IPCChannelMojoTestBase;
[email protected]95cb7fb92008-12-09 22:00:47283
initial.commit09911bf2008-07-26 23:55:29284// This test makes sure that the FuzzerClientListener and FuzzerServerListener
285// are working properly by generating two well formed IPC calls.
amistry6de2ee4f2016-05-05 05:12:09286TEST_F(IPCFuzzingTest, SanityTest) {
[email protected]3c788582013-01-25 21:51:35287 Init("FuzzServerClient");
288
[email protected]df3c1ca12008-12-19 21:37:01289 FuzzerClientListener listener;
[email protected]3c788582013-01-25 21:51:35290 CreateChannel(&listener);
291 listener.Init(channel());
292 ASSERT_TRUE(ConnectChannel());
initial.commit09911bf2008-07-26 23:55:29293
294 IPC::Message* msg = NULL;
295 int value = 43;
thestigf84f17f2015-03-11 20:41:55296 msg = new MsgClassIS(value, base::ASCIIToUTF16("expect 43"));
[email protected]3c788582013-01-25 21:51:35297 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29298 EXPECT_TRUE(listener.ExpectMessage(value, MsgClassIS::ID));
299
thestigf84f17f2015-03-11 20:41:55300 msg = new MsgClassSI(base::ASCIIToUTF16("expect 44"), ++value);
[email protected]3c788582013-01-25 21:51:35301 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29302 EXPECT_TRUE(listener.ExpectMessage(value, MsgClassSI::ID));
303
[email protected]3c788582013-01-25 21:51:35304 EXPECT_TRUE(WaitForClientShutdown());
305 DestroyChannel();
initial.commit09911bf2008-07-26 23:55:29306}
307
[email protected]3c788582013-01-25 21:51:35308// This test uses a payload that is smaller than expected. This generates an
sammc4bcc4ed62016-10-27 10:13:59309// error while unpacking the IPC buffer. Right after we generate another valid
310// IPC to make sure framing is working properly.
amistry6de2ee4f2016-05-05 05:12:09311TEST_F(IPCFuzzingTest, 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());
initial.commit09911bf2008-07-26 23:55:29318
[email protected]753bb252013-11-04 22:28:12319 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassIS::ID,
320 IPC::Message::PRIORITY_NORMAL);
initial.commit09911bf2008-07-26 23:55:29321 msg->WriteInt(666);
[email protected]3c788582013-01-25 21:51:35322 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29323 EXPECT_TRUE(listener.ExpectMsgNotHandled(MsgClassIS::ID));
324
thestigf84f17f2015-03-11 20:41:55325 msg = new MsgClassSI(base::ASCIIToUTF16("expect one"), 1);
[email protected]3c788582013-01-25 21:51:35326 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29327 EXPECT_TRUE(listener.ExpectMessage(1, MsgClassSI::ID));
328
[email protected]3c788582013-01-25 21:51:35329 EXPECT_TRUE(WaitForClientShutdown());
330 DestroyChannel();
initial.commit09911bf2008-07-26 23:55:29331}
initial.commit09911bf2008-07-26 23:55:29332
[email protected]3c788582013-01-25 21:51:35333// This test uses a payload that has too many arguments, but so the payload size
334// is big enough so the unpacking routine does not generate an error as in the
335// case of MsgBadPayloadShort test. This test does not pinpoint a flaw (per se)
336// as by design we don't carry type information on the IPC message.
amistry6de2ee4f2016-05-05 05:12:09337TEST_F(IPCFuzzingTest, MsgBadPayloadArgs) {
[email protected]3c788582013-01-25 21:51:35338 Init("FuzzServerClient");
339
[email protected]df3c1ca12008-12-19 21:37:01340 FuzzerClientListener listener;
[email protected]3c788582013-01-25 21:51:35341 CreateChannel(&listener);
342 listener.Init(channel());
343 ASSERT_TRUE(ConnectChannel());
initial.commit09911bf2008-07-26 23:55:29344
[email protected]753bb252013-11-04 22:28:12345 IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassSI::ID,
346 IPC::Message::PRIORITY_NORMAL);
thestigf84f17f2015-03-11 20:41:55347 msg->WriteString16(base::ASCIIToUTF16("d"));
initial.commit09911bf2008-07-26 23:55:29348 msg->WriteInt(0);
[email protected]95cb7fb92008-12-09 22:00:47349 msg->WriteInt(0x65); // Extra argument.
350
[email protected]3c788582013-01-25 21:51:35351 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29352 EXPECT_TRUE(listener.ExpectMessage(0, MsgClassSI::ID));
353
[email protected]95cb7fb92008-12-09 22:00:47354 // Now send a well formed message to make sure the receiver wasn't
355 // thrown out of sync by the extra argument.
thestigf84f17f2015-03-11 20:41:55356 msg = new MsgClassIS(3, base::ASCIIToUTF16("expect three"));
[email protected]3c788582013-01-25 21:51:35357 sender()->Send(msg);
initial.commit09911bf2008-07-26 23:55:29358 EXPECT_TRUE(listener.ExpectMessage(3, MsgClassIS::ID));
359
[email protected]3c788582013-01-25 21:51:35360 EXPECT_TRUE(WaitForClientShutdown());
361 DestroyChannel();
initial.commit09911bf2008-07-26 23:55:29362}
363
[email protected]2a3aa7b52013-01-11 20:56:22364} // namespace