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 socketConnection.OnReadable(fd);
83 socketConnection.OnShutdown(fd);
84 socketConnection.OnException(fd);
85 Msdp::DeviceStatus::SocketConnection::Connect(socket, recv, onDisconnected);
86 return true;
87 }
88
SocketParamsFuzzTest(const uint8_t * data,size_t size)89 bool SocketParamsFuzzTest(const uint8_t* data, size_t size)
90 {
91 MessageParcel datas;
92 if (!datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN) ||
93 !datas.WriteBuffer(data, size) || !datas.RewindRead(0)) {
94 return false;
95 }
96 AllocSocketPairParam allocSocketPairParam("testProgramName", 1);
97 AllocSocketPairReply allocSocketPairReply(1, 1);
98
99 allocSocketPairParam.Marshalling(datas);
100 allocSocketPairParam.Unmarshalling(datas);
101 allocSocketPairReply.Marshalling(datas);
102 allocSocketPairReply.Unmarshalling(datas);
103 return true;
104 }
105
106
SocketSessionFuzzTest(const uint8_t * data,size_t size)107 bool SocketSessionFuzzTest(const uint8_t* data, size_t size)
108 {
109 NetPacket packet(MessageId::COORDINATION_ADD_LISTENER);
110 struct epoll_event ev{};
111
112 SocketSession socketSession("testProgramName", 1, 1, 1, 1, 1);
113 socketSession.SendMsg(packet);
114 socketSession.ToString();
115 socketSession.Dispatch(ev);
116 return true;
117 }
118
119 } // namespace OHOS
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)120 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
121 {
122 /* Run your code on data */
123 if (data == nullptr) {
124 return 0;
125 }
126
127 OHOS::SocketConnectionFuzzTest(data, size);
128 OHOS::SocketParamsFuzzTest(data, size);
129 OHOS::SocketSessionFuzzTest(data, size);
130 return 0;
131 }
132 } // namespace DeviceStatus
133 } // namespace Msdp
134 } // namespace OHOS