1 /*
2  * Copyright (c) 2022-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 "transserverproxyextern_fuzzer.h"
17 
18 #include <chrono>
19 #include <thread>
20 #include "securec.h"
21 
22 #include "softbus_adapter_mem.h"
23 #include "trans_server_proxy.h"
24 
25 #define LOOP_SLEEP_MILLS 100
26 
27 namespace OHOS {
28 class TransServerProxyExternTestEnv {
29 public:
TransServerProxyExternTestEnv()30     TransServerProxyExternTestEnv()
31     {
32         isInited_ = false;
33         (void)TransServerProxyInit();
34         isInited_ = true;
35     }
36 
~TransServerProxyExternTestEnv()37     ~TransServerProxyExternTestEnv()
38     {
39         isInited_ = false;
40         TransServerProxyDeInit();
41     }
42 
IsInited(void) const43     bool IsInited(void) const
44     {
45         return isInited_;
46     }
47 
48 private:
49     volatile bool isInited_;
50 };
51 
TransServerProxyDeInitTest(const uint8_t * data,size_t size)52 void TransServerProxyDeInitTest(const uint8_t *data, size_t size)
53 {
54     (void)data;
55     (void)size;
56     TransServerProxyDeInit();
57 }
58 
ServerIpcCreateSessionServerTest(const uint8_t * data,size_t size)59 void ServerIpcCreateSessionServerTest(const uint8_t *data, size_t size)
60 {
61     char *pkgName = const_cast<char *>(reinterpret_cast<const char *>(data));
62     char *sessionName = const_cast<char *>(reinterpret_cast<const char *>(data));
63 
64     (void)ServerIpcCreateSessionServer(pkgName, sessionName);
65     (void)ServerIpcCreateSessionServer(nullptr, sessionName);
66     (void)ServerIpcCreateSessionServer(pkgName, nullptr);
67     (void)ServerIpcCreateSessionServer(nullptr, nullptr);
68 }
69 
ServerIpcRemoveSessionServerTest(const uint8_t * data,size_t size)70 void ServerIpcRemoveSessionServerTest(const uint8_t *data, size_t size)
71 {
72     char *pkgName = const_cast<char *>(reinterpret_cast<const char *>(data));
73     char *sessionName = const_cast<char *>(reinterpret_cast<const char *>(data));
74 
75     (void)ServerIpcRemoveSessionServer(pkgName, sessionName);
76     (void)ServerIpcRemoveSessionServer(nullptr, sessionName);
77     (void)ServerIpcRemoveSessionServer(pkgName, nullptr);
78     (void)ServerIpcRemoveSessionServer(nullptr, nullptr);
79 }
80 
InitSessionAttribute(const uint8_t * data,size_t size,SessionAttribute * sessionAttr)81 static void InitSessionAttribute(const uint8_t *data, size_t size, SessionAttribute *sessionAttr)
82 {
83     sessionAttr->dataType = *(reinterpret_cast<const int32_t *>(data));
84     sessionAttr->attr.streamAttr.streamType = *(reinterpret_cast<const int32_t *>(data));
85 }
86 
InitSessionParam(const uint8_t * data,size_t size,SessionParam * sessionParam,SessionAttribute * sessionAttr)87 static void InitSessionParam(
88     const uint8_t *data, size_t size, SessionParam *sessionParam, SessionAttribute *sessionAttr)
89 {
90     bool boolParam = (size % 2 == 0) ? true : false;
91     char *charParam = boolParam ? const_cast<char *>(reinterpret_cast<const char *>(data)) : nullptr;
92 
93     sessionParam->sessionName = charParam;
94     sessionParam->peerSessionName = charParam;
95     sessionParam->peerDeviceId = charParam;
96     sessionParam->groupId = charParam;
97     sessionParam->attr = sessionAttr;
98     sessionParam->sessionId = *(reinterpret_cast<const int32_t *>(data));
99     sessionParam->isQosLane = boolParam;
100     sessionParam->isAsync = boolParam;
101 }
102 
ServerIpcOpenSessionTest(const uint8_t * data,size_t size)103 void ServerIpcOpenSessionTest(const uint8_t *data, size_t size)
104 {
105     TransInfo transInfo = {
106         .channelId = *(reinterpret_cast<const int32_t *>(data)),
107         .channelType = *(reinterpret_cast<const int32_t *>(data)),
108     };
109 
110     SessionAttribute sessionAttr = { 0 };
111     InitSessionAttribute(data, size, &sessionAttr);
112 
113     SessionParam sessionParam = { 0 };
114     InitSessionParam(data, size, &sessionParam, &sessionAttr);
115 
116     (void)ServerIpcOpenSession(&sessionParam, &transInfo);
117 }
118 
ServerIpcOpenAuthSessionTest(const uint8_t * data,size_t size)119 void ServerIpcOpenAuthSessionTest(const uint8_t *data, size_t size)
120 {
121     char *sessionName = const_cast<char *>(reinterpret_cast<const char *>(data));
122     ConnectionAddr connectionAddr;
123     connectionAddr.type = CONNECTION_ADDR_SESSION;
124     connectionAddr.info.session.sessionId = *(reinterpret_cast<const int32_t *>(data));
125     connectionAddr.info.session.channelId = *(reinterpret_cast<const int32_t *>(data));
126     connectionAddr.info.session.type = *(reinterpret_cast<const int32_t *>(data));
127     (void)ServerIpcOpenAuthSession(sessionName, &connectionAddr);
128     (void)ServerIpcOpenAuthSession(nullptr, &connectionAddr);
129     (void)ServerIpcOpenAuthSession(sessionName, nullptr);
130     (void)ServerIpcOpenAuthSession(nullptr, nullptr);
131 }
132 
ServerIpcNotifyAuthSuccessTest(const uint8_t * data,size_t size)133 void ServerIpcNotifyAuthSuccessTest(const uint8_t *data, size_t size)
134 {
135     int32_t channelId = *(reinterpret_cast<const int32_t *>(data));
136     int32_t channelType = *(reinterpret_cast<const int32_t *>(data));
137 
138     (void)ServerIpcNotifyAuthSuccess(channelId, channelType);
139 }
140 
ServerIpcCloseChannelTest(const uint8_t * data,size_t size)141 void ServerIpcCloseChannelTest(const uint8_t *data, size_t size)
142 {
143     char *sessionName = const_cast<char *>(reinterpret_cast<const char *>(data));
144     int32_t channelId = *(reinterpret_cast<const int32_t *>(data));
145     int32_t channelType = *(reinterpret_cast<const int32_t *>(data));
146 
147     (void)ServerIpcCloseChannel(sessionName, channelId, channelType);
148 }
149 
ServerIpcCloseChannelWithStatisticsTest(const uint8_t * data,size_t size)150 void ServerIpcCloseChannelWithStatisticsTest(const uint8_t *data, size_t size)
151 {
152     if (size < sizeof(uint64_t)) {
153         return;
154     }
155 
156     int32_t channelId = *(reinterpret_cast<const int32_t *>(data));
157     int32_t channelType = *(reinterpret_cast<const int32_t *>(data));
158     uint64_t laneId = *(reinterpret_cast<const uint64_t *>(data));
159 
160     (void)ServerIpcCloseChannelWithStatistics(channelId, channelType, laneId, data, size);
161 }
162 
ServerIpcReleaseResourcesTest(const uint8_t * data,size_t size)163 void ServerIpcReleaseResourcesTest(const uint8_t *data, size_t size)
164 {
165     int32_t channelId = *(reinterpret_cast<const int32_t *>(data));
166 
167     (void)ServerIpcReleaseResources(channelId);
168 }
169 
ServerIpcSendMessageTest(const uint8_t * data,size_t size)170 void ServerIpcSendMessageTest(const uint8_t *data, size_t size)
171 {
172     int32_t channelId = *(reinterpret_cast<const int32_t *>(data));
173     int32_t channelType = *(reinterpret_cast<const int32_t *>(data));
174     int32_t msgType = *(reinterpret_cast<const int32_t *>(data));
175 
176     (void)ServerIpcSendMessage(channelId, channelType, data, size, msgType);
177 }
178 
ServerIpcQosReportTest(const uint8_t * data,size_t size)179 void ServerIpcQosReportTest(const uint8_t *data, size_t size)
180 {
181     int32_t channelId = *(reinterpret_cast<const int32_t *>(data));
182     int32_t channelType = *(reinterpret_cast<const int32_t *>(data));
183     int32_t appType = *(reinterpret_cast<const int32_t *>(data));
184     int32_t quality = *(reinterpret_cast<const int32_t *>(data));
185 
186     (void)ServerIpcQosReport(channelId, channelType, appType, quality);
187 }
188 
ServerIpcStreamStatsTest(const uint8_t * data,size_t size)189 void ServerIpcStreamStatsTest(const uint8_t *data, size_t size)
190 {
191     int32_t channelId = *(reinterpret_cast<const int32_t *>(data));
192     int32_t channelType = *(reinterpret_cast<const int32_t *>(data));
193     StreamSendStats streamSendStats;
194     streamSendStats.costTimeStatsCnt[FRAME_COST_LT10MS] = *(reinterpret_cast<const uint32_t *>(data));
195     streamSendStats.sendBitRateStatsCnt[FRAME_BIT_RATE_LT3M] = *(reinterpret_cast<const uint32_t *>(data));
196     (void)ServerIpcStreamStats(channelId, channelType, &streamSendStats);
197 }
198 
ServerIpcRippleStatsTest(const uint8_t * data,size_t size)199 void ServerIpcRippleStatsTest(const uint8_t *data, size_t size)
200 {
201     int32_t channelId = *(reinterpret_cast<const int32_t *>(data));
202     int32_t channelType = *(reinterpret_cast<const int32_t *>(data));
203     TrafficStats trafficStats;
204     trafficStats.stats[0] = 't';
205     trafficStats.stats[1] = 'e';
206     (void)ServerIpcRippleStats(channelId, channelType, &trafficStats);
207 }
208 
ServerIpcGrantPermissionTest(const uint8_t * data,size_t size)209 void ServerIpcGrantPermissionTest(const uint8_t *data, size_t size)
210 {
211     int uid = *(reinterpret_cast<const int *>(data));
212     int pid = *(reinterpret_cast<const int *>(data));
213     char *sessionName = const_cast<char *>(reinterpret_cast<const char *>(data));
214 
215     (void)ServerIpcGrantPermission(uid, pid, sessionName);
216     (void)ServerIpcGrantPermission(uid, pid, nullptr);
217 }
218 
ServerIpcRemovePermissionTest(const uint8_t * data,size_t size)219 void ServerIpcRemovePermissionTest(const uint8_t *data, size_t size)
220 {
221     char *sessionName = const_cast<char *>(reinterpret_cast<const char *>(data));
222 
223     (void)ServerIpcRemovePermission(sessionName);
224     (void)ServerIpcRemovePermission(nullptr);
225 }
226 
ServerIpcEvaluateQosTest(const uint8_t * data,size_t size)227 void ServerIpcEvaluateQosTest(const uint8_t *data, size_t size)
228 {
229     char *peerNetworkId = const_cast<char *>(reinterpret_cast<const char *>(data));
230     TransDataType dataType = *(reinterpret_cast<const TransDataType *>(data));
231     QosTV qosTv = {
232         .qos = *(reinterpret_cast<const QosType *>(data)),
233         .value = *(reinterpret_cast<const int32_t *>(data)),
234     };
235     uint32_t qosCount = 1;
236 
237     (void)ServerIpcEvaluateQos(peerNetworkId, dataType, &qosTv, qosCount);
238     (void)ServerIpcEvaluateQos(nullptr, dataType, &qosTv, qosCount);
239 }
240 } // namespace OHOS
241 
242 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)243 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
244 {
245     if (data == nullptr || size < sizeof(int32_t)) {
246         return 0;
247     }
248 
249     static OHOS::TransServerProxyExternTestEnv env;
250     if (!env.IsInited()) {
251         return 0;
252     }
253 
254     uint8_t *dataWithEndCharacter = static_cast<uint8_t *>(SoftBusCalloc(size + 1));
255     if (dataWithEndCharacter == nullptr) {
256         return 0;
257     }
258 
259     if (memcpy_s(dataWithEndCharacter, size, data, size) != EOK) {
260         SoftBusFree(dataWithEndCharacter);
261         return 0;
262     }
263 
264     /* Run your code on data */
265     OHOS::TransServerProxyDeInitTest(data, size);
266     OHOS::ServerIpcCreateSessionServerTest(dataWithEndCharacter, size);
267     OHOS::ServerIpcRemoveSessionServerTest(dataWithEndCharacter, size);
268     OHOS::ServerIpcOpenSessionTest(dataWithEndCharacter, size);
269     OHOS::ServerIpcOpenAuthSessionTest(dataWithEndCharacter, size);
270     OHOS::ServerIpcCloseChannelTest(dataWithEndCharacter, size);
271     OHOS::ServerIpcCloseChannelWithStatisticsTest(data, size);
272     OHOS::ServerIpcReleaseResourcesTest(data, size);
273     OHOS::ServerIpcSendMessageTest(data, size);
274     OHOS::ServerIpcQosReportTest(data, size);
275     OHOS::ServerIpcStreamStatsTest(data, size);
276     OHOS::ServerIpcRippleStatsTest(data, size);
277     OHOS::ServerIpcGrantPermissionTest(dataWithEndCharacter, size);
278     OHOS::ServerIpcRemovePermissionTest(dataWithEndCharacter, size);
279     OHOS::ServerIpcEvaluateQosTest(dataWithEndCharacter, size);
280     std::this_thread::sleep_for(std::chrono::milliseconds(LOOP_SLEEP_MILLS));
281     SoftBusFree(dataWithEndCharacter);
282     return 0;
283 }
284