1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
11 #include "base/message_loop/message_loop.h"
12 #include "base/strings/string16.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/threading/platform_thread.h"
15 #include "ipc/ipc_test_base.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 // IPC messages for testing ----------------------------------------------------
20 #define IPC_MESSAGE_IMPL
21 #include "ipc/ipc_message_macros.h"
23 #define IPC_MESSAGE_START TestMsgStart
25 // Generic message class that is an int followed by a string16.
26 IPC_MESSAGE_CONTROL2(MsgClassIS
, int, base::string16
)
28 // Generic message class that is a string16 followed by an int.
29 IPC_MESSAGE_CONTROL2(MsgClassSI
, base::string16
, int)
31 // Message to create a mutex in the IPC server, using the received name.
32 IPC_MESSAGE_CONTROL2(MsgDoMutex
, base::string16
, int)
34 // Used to generate an ID for a message that should not exist.
35 IPC_MESSAGE_CONTROL0(MsgUnhandled
)
37 // -----------------------------------------------------------------------------
41 TEST(IPCMessageIntegrity
, ReadBeyondBufferStr
) {
42 // This was BUG 984408.
43 uint32_t v1
= kuint32max
- 1;
45 IPC::Message
m(0, 1, IPC::Message::PRIORITY_NORMAL
);
46 EXPECT_TRUE(m
.WriteInt(v1
));
47 EXPECT_TRUE(m
.WriteInt(v2
));
49 base::PickleIterator
iter(m
);
51 EXPECT_FALSE(iter
.ReadString(&vs
));
54 TEST(IPCMessageIntegrity
, ReadBeyondBufferStr16
) {
55 // This was BUG 984408.
56 uint32_t v1
= kuint32max
- 1;
58 IPC::Message
m(0, 1, IPC::Message::PRIORITY_NORMAL
);
59 EXPECT_TRUE(m
.WriteInt(v1
));
60 EXPECT_TRUE(m
.WriteInt(v2
));
62 base::PickleIterator
iter(m
);
64 EXPECT_FALSE(iter
.ReadString16(&vs
));
67 TEST(IPCMessageIntegrity
, ReadBytesBadIterator
) {
68 // This was BUG 1035467.
69 IPC::Message
m(0, 1, IPC::Message::PRIORITY_NORMAL
);
70 EXPECT_TRUE(m
.WriteInt(1));
71 EXPECT_TRUE(m
.WriteInt(2));
73 base::PickleIterator
iter(m
);
74 const char* data
= NULL
;
75 EXPECT_TRUE(iter
.ReadBytes(&data
, sizeof(int)));
78 TEST(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>.
82 IPC::Message
m(0, 1, IPC::Message::PRIORITY_NORMAL
);
83 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));
88 std::vector
<double> vec
;
89 base::PickleIterator
iter(m
);
90 EXPECT_FALSE(ReadParam(&m
, &iter
, &vec
));
93 #if defined(OS_ANDROID)
94 #define MAYBE_ReadVectorTooLarge1 DISABLED_ReadVectorTooLarge1
96 #define MAYBE_ReadVectorTooLarge1 ReadVectorTooLarge1
98 TEST(IPCMessageIntegrity
, MAYBE_ReadVectorTooLarge1
) {
99 // This was BUG 1006367. This is the large but positive length case. Again
100 // we try to hit the non-specialized case vector<P>.
101 IPC::Message
m(0, 1, IPC::Message::PRIORITY_NORMAL
);
102 EXPECT_TRUE(m
.WriteInt(0x21000003)); // This is the count of elements.
103 EXPECT_TRUE(m
.WriteInt64(1));
104 EXPECT_TRUE(m
.WriteInt64(2));
106 std::vector
<int64_t> vec
;
107 base::PickleIterator
iter(m
);
108 EXPECT_FALSE(ReadParam(&m
, &iter
, &vec
));
111 TEST(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>.
115 IPC::Message
m(0, 1, IPC::Message::PRIORITY_NORMAL
);
116 EXPECT_TRUE(m
.WriteInt(0x71000000)); // This is the count of elements.
117 EXPECT_TRUE(m
.WriteInt64(1));
118 EXPECT_TRUE(m
.WriteInt64(2));
120 std::vector
<int64_t> vec
;
121 base::PickleIterator
iter(m
);
122 EXPECT_FALSE(ReadParam(&m
, &iter
, &vec
));
125 class SimpleListener
: public IPC::Listener
{
127 SimpleListener() : other_(NULL
) {
129 void Init(IPC::Sender
* s
) {
137 FUZZER_ROUTING_ID
= 5
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.
143 class FuzzerServerListener
: public SimpleListener
{
145 FuzzerServerListener() : message_count_(2), pending_messages_(0) {
147 bool OnMessageReceived(const IPC::Message
& msg
) override
{
148 if (msg
.routing_id() == MSG_ROUTING_CONTROL
) {
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());
163 void OnMsgClassISMessage(int value
, const base::string16
& text
) {
164 UseData(MsgClassIS::ID
, value
, text
);
165 RoundtripAckReply(FUZZER_ROUTING_ID
, MsgClassIS::ID
, value
);
169 void OnMsgClassSIMessage(const base::string16
& text
, int value
) {
170 UseData(MsgClassSI::ID
, value
, text
);
171 RoundtripAckReply(FUZZER_ROUTING_ID
, MsgClassSI::ID
, value
);
175 bool RoundtripAckReply(int routing
, uint32_t type_id
, int reply
) {
176 IPC::Message
* message
= new IPC::Message(routing
, type_id
,
177 IPC::Message::PRIORITY_NORMAL
);
178 message
->WriteInt(reply
+ 1);
179 message
->WriteInt(reply
);
180 return other_
->Send(message
);
186 if (0 == message_count_
)
187 base::MessageLoop::current()->Quit();
190 void ReplyMsgNotHandled(uint32_t type_id
) {
191 RoundtripAckReply(FUZZER_ROUTING_ID
, MsgUnhandled::ID
, type_id
);
195 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
;
204 int pending_messages_
;
207 class FuzzerClientListener
: public SimpleListener
{
209 FuzzerClientListener() : last_msg_(NULL
) {
212 bool OnMessageReceived(const IPC::Message
& msg
) override
{
213 last_msg_
= new IPC::Message(msg
);
214 base::MessageLoop::current()->Quit();
218 bool ExpectMessage(int value
, uint32_t type_id
) {
219 if (!MsgHandlerInternal(type_id
))
223 base::PickleIterator
iter(*last_msg_
);
224 if (!iter
.ReadInt(&msg_value1
))
226 if (!iter
.ReadInt(&msg_value2
))
228 if ((msg_value2
+ 1) != msg_value1
)
230 if (msg_value2
!= value
)
238 bool ExpectMsgNotHandled(uint32_t type_id
) {
239 return ExpectMessage(type_id
, MsgUnhandled::ID
);
243 bool MsgHandlerInternal(uint32_t type_id
) {
244 base::MessageLoop::current()->Run();
245 if (NULL
== last_msg_
)
247 if (FUZZER_ROUTING_ID
!= last_msg_
->routing_id())
249 return (type_id
== last_msg_
->type());
252 IPC::Message
* last_msg_
;
255 // Runs the fuzzing server child mode. Returns when the preset number of
256 // messages have been received.
257 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(FuzzServerClient
) {
258 base::MessageLoopForIO main_message_loop
;
259 FuzzerServerListener listener
;
260 scoped_ptr
<IPC::Channel
> channel(IPC::Channel::CreateClient(
261 IPCTestBase::GetChannelName("FuzzServerClient"), &listener
, nullptr));
262 CHECK(channel
->Connect());
263 listener
.Init(channel
.get());
264 base::MessageLoop::current()->Run();
268 class IPCFuzzingTest
: public IPCTestBase
{
271 #if defined(OS_ANDROID)
272 #define MAYBE_SanityTest DISABLED_SanityTest
274 #define MAYBE_SanityTest SanityTest
276 // This test makes sure that the FuzzerClientListener and FuzzerServerListener
277 // are working properly by generating two well formed IPC calls.
278 TEST_F(IPCFuzzingTest
, MAYBE_SanityTest
) {
279 Init("FuzzServerClient");
281 FuzzerClientListener listener
;
282 CreateChannel(&listener
);
283 listener
.Init(channel());
284 ASSERT_TRUE(ConnectChannel());
285 ASSERT_TRUE(StartClient());
287 IPC::Message
* msg
= NULL
;
289 msg
= new MsgClassIS(value
, base::ASCIIToUTF16("expect 43"));
291 EXPECT_TRUE(listener
.ExpectMessage(value
, MsgClassIS::ID
));
293 msg
= new MsgClassSI(base::ASCIIToUTF16("expect 44"), ++value
);
295 EXPECT_TRUE(listener
.ExpectMessage(value
, MsgClassSI::ID
));
297 EXPECT_TRUE(WaitForClientShutdown());
301 #if defined(OS_ANDROID)
302 #define MAYBE_MsgBadPayloadShort DISABLED_MsgBadPayloadShort
304 #define MAYBE_MsgBadPayloadShort MsgBadPayloadShort
306 // 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.
310 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
311 TEST_F(IPCFuzzingTest
, MAYBE_MsgBadPayloadShort
) {
312 Init("FuzzServerClient");
314 FuzzerClientListener listener
;
315 CreateChannel(&listener
);
316 listener
.Init(channel());
317 ASSERT_TRUE(ConnectChannel());
318 ASSERT_TRUE(StartClient());
320 IPC::Message
* msg
= new IPC::Message(MSG_ROUTING_CONTROL
, MsgClassIS::ID
,
321 IPC::Message::PRIORITY_NORMAL
);
324 EXPECT_TRUE(listener
.ExpectMsgNotHandled(MsgClassIS::ID
));
326 msg
= new MsgClassSI(base::ASCIIToUTF16("expect one"), 1);
328 EXPECT_TRUE(listener
.ExpectMessage(1, MsgClassSI::ID
));
330 EXPECT_TRUE(WaitForClientShutdown());
335 #if defined(OS_ANDROID)
336 #define MAYBE_MsgBadPayloadArgs DISABLED_MsgBadPayloadArgs
338 #define MAYBE_MsgBadPayloadArgs MsgBadPayloadArgs
340 // 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.
344 TEST_F(IPCFuzzingTest
, MAYBE_MsgBadPayloadArgs
) {
345 Init("FuzzServerClient");
347 FuzzerClientListener listener
;
348 CreateChannel(&listener
);
349 listener
.Init(channel());
350 ASSERT_TRUE(ConnectChannel());
351 ASSERT_TRUE(StartClient());
353 IPC::Message
* msg
= new IPC::Message(MSG_ROUTING_CONTROL
, MsgClassSI::ID
,
354 IPC::Message::PRIORITY_NORMAL
);
355 msg
->WriteString16(base::ASCIIToUTF16("d"));
357 msg
->WriteInt(0x65); // Extra argument.
360 EXPECT_TRUE(listener
.ExpectMessage(0, MsgClassSI::ID
));
362 // Now send a well formed message to make sure the receiver wasn't
363 // thrown out of sync by the extra argument.
364 msg
= new MsgClassIS(3, base::ASCIIToUTF16("expect three"));
366 EXPECT_TRUE(listener
.ExpectMessage(3, MsgClassIS::ID
));
368 EXPECT_TRUE(WaitForClientShutdown());