1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ipcsocket_fuzzer.h"
17
18 #include "singleton.h"
19
20 #include "devicestatus_define.h"
21 #include "i_tunnel_client.h"
22 #include "i_plugin.h"
23 #include "socket_client.h"
24 #include "socket_session_manager.h"
25 #include "socket_session.h"
26 #include "socket_connection.h"
27 #include "socket_params.h"
28 #include "stream_client.h"
29 #include "tunnel_client.h"
30
31 #include "message_parcel.h"
32
33 #undef LOG_TAG
34 #define LOG_TAG "IpcSocketFuzzTest"
35 namespace OHOS {
36 namespace Msdp {
37 namespace DeviceStatus {
38 const std::u16string FORMMGR_INTERFACE_TOKEN { u"ohos.msdp.Idevicestatus" };
39 inline constexpr int32_t MAX_EVENT_SIZE { 100 };
40 const uint8_t *g_baseFuzzData = nullptr;
41 size_t g_baseFuzzSize = 0;
42 size_t g_baseFuzzPos = 0;
43
44 namespace OHOS {
45
GetData()46 template <class T> T GetData()
47 {
48 T objetct{};
49 size_t objetctSize = sizeof(objetct);
50 if (g_baseFuzzData == nullptr || objetctSize > g_baseFuzzSize - g_baseFuzzPos) {
51 return objetct;
52 }
53 errno_t ret = memcpy_s(&objetct, objetctSize, g_baseFuzzData + g_baseFuzzPos, objetctSize);
54 if (ret != EOK) {
55 return {};
56 }
57 g_baseFuzzPos += objetctSize;
58 return objetct;
59 }
60
SocketConnectionFuzzTest(const uint8_t * data,size_t size)61 bool SocketConnectionFuzzTest(const uint8_t* data, size_t size)
62 {
63 if ((data == nullptr) || (size < 1)) {
64 return false;
65 }
66
67 auto recv = [](const NetPacket &pkt) {
68 return;
69 };
70 auto onDisconnected = []() {
71 return;
72 };
73 g_baseFuzzData = data;
74 g_baseFuzzSize = size;
75 g_baseFuzzPos = 0;
76 int32_t fd = GetData<int32_t>();
77 SocketConnection socketConnection(1, recv, onDisconnected);
78
79 auto socket = []() {
80 return 0;
81 };
82 NetPacket packet(MessageId::COORDINATION_ADD_LISTENER);
83 struct epoll_event ev{};
84 auto tunnel = std::make_shared<TunnelClient>();
85 auto client = std::make_unique<SocketClient>(tunnel);
86 client->Connect();
87 client->Socket();
88 MessageId msgId { MessageId::INVALID };
89 NetPacket pkt(msgId);
90 client->OnPacket(pkt);
91 client->Reconnect();
92 client->Stop();
93 client->OnDisconnected();
94 SocketSession socketSession("testProgramName", 1, 1, 1, 1, 1);
95 socketSession.SendMsg(packet);
96 socketSession.ToString();
97 socketSession.Dispatch(ev);
98 socketConnection.OnReadable(fd);
99 socketConnection.OnShutdown(fd);
100 socketConnection.OnException(fd);
101 Msdp::DeviceStatus::SocketConnection::Connect(socket, recv, onDisconnected);
102 tunnel = nullptr;
103 client = nullptr;
104 return true;
105 }
106
SocketParamsFuzzTest(const uint8_t * data,size_t size)107 bool SocketParamsFuzzTest(const uint8_t* data, size_t size)
108 {
109 MessageParcel datas;
110 if (!datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN) ||
111 !datas.WriteBuffer(data, size) || !datas.RewindRead(0)) {
112 return false;
113 }
114 AllocSocketPairParam allocSocketPairParam("testProgramName", 1);
115 AllocSocketPairReply allocSocketPairReply(1, 1);
116
117 allocSocketPairParam.Marshalling(datas);
118 allocSocketPairParam.Unmarshalling(datas);
119 allocSocketPairReply.Marshalling(datas);
120 allocSocketPairReply.Unmarshalling(datas);
121 return true;
122 }
123
124 } // namespace OHOS
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)125 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
126 {
127 /* Run your code on data */
128 if (data == nullptr) {
129 return 0;
130 }
131
132 OHOS::SocketConnectionFuzzTest(data, size);
133 OHOS::SocketParamsFuzzTest(data, size);
134 return 0;
135 }
136 } // namespace DeviceStatus
137 } // namespace Msdp
138 } // namespace OHOS