1 /*
2 * Copyright (c) 2023 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 <cerrno>
17 #include <cstdlib>
18 #include "securec.h"
19 #include "v1_0/ihostapd_interface.h"
20 #include "hostapd_fuzzer.h"
21 #include "hostapd_common_fuzzer.h"
22
23 namespace OHOS {
24 namespace WIFI {
25 constexpr size_t THRESHOLD = 10;
26 const char *g_wpaServiceName = "hostapd_interface_service";
27 struct IHostapdInterface *g_wpaObj = nullptr;
28
DoSomethingInterestingWithMyAPI(const uint8_t * rawData,size_t size)29 bool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size)
30 {
31 bool result = false;
32
33 if (rawData == nullptr || size == 0) {
34 return false;
35 }
36 g_wpaObj = IHostapdInterfaceGetInstance(g_wpaServiceName, true);
37 if (g_wpaObj == nullptr) {
38 HDF_LOGE("%{public}s : g_wpaObj is null", __FUNCTION__);
39 return result;
40 }
41 uint32_t dataSize = size - OFFSET;
42 uint8_t *tmpRawData = reinterpret_cast<uint8_t *>(OsalMemCalloc(dataSize + 1));
43 if (tmpRawData == nullptr) {
44 HDF_LOGE("%{public}s : OsalMemCalloc failed!", __FUNCTION__);
45 return result;
46 }
47 int32_t ret = g_wpaObj->StartAp(g_wpaObj);
48 if (ret != HDF_SUCCESS) {
49 HDF_LOGE("%{public}s : StartAp failed!", __FUNCTION__);
50 OsalMemFree(tmpRawData);
51 return result;
52 }
53
54 FuzzHostapdInterfaceStartAp(g_wpaObj, tmpRawData);
55 FuzzHostapdInterfaceStopAp(g_wpaObj, tmpRawData);
56 FuzzHostapdInterfaceEnableAp(g_wpaObj, tmpRawData);
57 FuzzHostapdInterfaceDisableAp(g_wpaObj, tmpRawData);
58 FuzzHostapdInterfaceSetApPasswd(g_wpaObj, tmpRawData);
59 FuzzHostapdInterfaceSetApName(g_wpaObj, tmpRawData);
60 FuzzHostapdInterfaceSetApBand(g_wpaObj, tmpRawData);
61 FuzzHostapdInterfaceSetAp80211n(g_wpaObj, tmpRawData);
62 FuzzHostapdInterfaceSetApWmm(g_wpaObj, tmpRawData);
63 FuzzHostapdInterfaceSetApChannel(g_wpaObj, tmpRawData);
64 FuzzHostapdInterfaceSetApMaxConn(g_wpaObj, tmpRawData);
65 FuzzHostapdInterfaceSetMacFilter(g_wpaObj, tmpRawData);
66 FuzzHostapdInterfaceDelMacFilter(g_wpaObj, tmpRawData);
67 FuzzHostapdInterfaceGetStaInfos(g_wpaObj, tmpRawData);
68 FuzzHostapdInterfaceDisassociateSta(g_wpaObj, tmpRawData);
69 FuzzHostapdInterfaceRegisterEventCallback(g_wpaObj, tmpRawData);
70 FuzzHostapdInterfaceUnregisterEventCallback(g_wpaObj, tmpRawData);
71
72 ret = g_wpaObj->StopAp(g_wpaObj);
73 if (ret != HDF_SUCCESS) {
74 HDF_LOGE("%{public}s : StopAp failed!", __FUNCTION__);
75 result = false;
76 }
77 IHostapdInterfaceReleaseInstance(g_wpaServiceName, g_wpaObj, true);
78 OsalMemFree(tmpRawData);
79 return result;
80 }
81 } // namespace WIFI
82 } // namespace OHOS
83
84 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)85 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
86 {
87 if (size < OHOS::WIFI::THRESHOLD) {
88 return 0;
89 }
90
91 /* Run your code on data */
92 OHOS::WIFI::DoSomethingInterestingWithMyAPI(data, size);
93 return 0;
94 }
95