• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "sta_fuzzer.h"
16 #include "wlan_common_fuzzer.h"
17 
18 namespace OHOS {
19 namespace WIFI {
20 constexpr size_t THRESHOLD = 10;
21 const char *g_wlanServiceName = "wlan_interface_service";
22 const int32_t wlanType = PROTOCOL_80211_IFTYPE_STATION;
23 struct IWlanInterface *g_wlanObj = nullptr;
24 
FuzzStartScan(struct IWlanInterface * interface,const uint8_t * rawData)25 static void FuzzStartScan(struct IWlanInterface *interface, const uint8_t *rawData)
26 {
27     struct HdfWifiScan scan = {0};
28     struct HdfFeatureInfo feature;
29     feature.ifName = const_cast<char *>(reinterpret_cast<const char *>(rawData));
30     feature.type = *const_cast<int32_t *>(reinterpret_cast<const int32_t *>(rawData));
31 
32     interface->StartScan(interface, &feature, &scan);
33     HDF_LOGI("%{public}s: success", __FUNCTION__);
34 }
35 
FuzzSetScanningMacAddress(struct IWlanInterface * interface,const uint8_t * rawData)36 static void FuzzSetScanningMacAddress(struct IWlanInterface *interface, const uint8_t *rawData)
37 {
38     struct HdfFeatureInfo feature;
39     feature.ifName = const_cast<char *>(reinterpret_cast<const char *>(rawData));
40     feature.type = *const_cast<int32_t *>(reinterpret_cast<const int32_t *>(rawData));
41     const uint8_t *scanMac = rawData;
42     uint32_t macLen = 0;
43 
44     if (GetWlanDataSize(&macLen) != HDF_SUCCESS) {
45         HDF_LOGE("%{public}s: get data size failed!", __FUNCTION__);
46     }
47 
48     interface->SetScanningMacAddress(interface, &feature, scanMac, macLen);
49     HDF_LOGI("%{public}s: success", __FUNCTION__);
50 }
51 
52 static FuzzWlanFuncs g_fuzzWlanFuncs[] = {
53     FuzzStartScan,
54     FuzzGetChipId,
55     FuzzGetDeviceMacAddress,
56     FuzzGetFeatureType,
57     FuzzGetFreqsWithBand,
58     FuzzGetNetworkIfaceName,
59     FuzzSetMacAddress,
60     FuzzSetTxPower,
61     FuzzGetPowerMode,
62     FuzzSetPowerMode,
63     FuzzGetIfNamesByChipId,
64     FuzzResetDriver,
65     FuzzStartChannelMeas,
66     FuzzSetProjectionScreenParam,
67     FuzzWifiSendCmdIoctl,
68     FuzzGetFeatureByIfName,
69     FuzzGetStaInfo,
70     FuzzGetChannelMeasResult,
71     FuzzSetScanningMacAddress,
72     FuzzResetToFactoryMacAddress,
73 };
74 
FuncToOptimal(struct IWlanInterface * interface,uint32_t cmdId,const uint8_t * data)75 static void FuncToOptimal(struct IWlanInterface *interface, uint32_t cmdId, const uint8_t *data)
76 {
77     FuzzWlanFuncs fuzzWlanFunc = g_fuzzWlanFuncs[cmdId];
78     if (fuzzWlanFunc != nullptr) {
79         fuzzWlanFunc(interface, data);
80     }
81     return;
82 }
83 
DoSomethingInterestingWithMyAPI(const uint8_t * rawData,size_t size)84 bool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size)
85 {
86     struct HdfFeatureInfo ifeature;
87     bool result = false;
88 
89     if (rawData == nullptr || size == 0) {
90         return false;
91     }
92     uint32_t cmdId = Convert2Uint32(rawData) % ((sizeof(g_fuzzWlanFuncs) / sizeof(g_fuzzWlanFuncs[0])));
93     g_wlanObj = IWlanInterfaceGetInstance(g_wlanServiceName, false);
94     if (g_wlanObj == nullptr) {
95         HDF_LOGE("%{public}s: g_wlanObj is null", __FUNCTION__);
96         return result;
97     }
98     uint32_t dataSize = size - OFFSET;
99     uint8_t *tmpRawData = reinterpret_cast<uint8_t *>(OsalMemCalloc(dataSize + 1));
100     if (tmpRawData == nullptr) {
101         HDF_LOGE("%{public}s: OsalMemCalloc failed!", __FUNCTION__);
102         return result;
103     }
104     int32_t ret = g_wlanObj->Start(g_wlanObj);
105     if (ret != HDF_SUCCESS) {
106         HDF_LOGE("%{public}s: Start failed! ret=%{public}d", __FUNCTION__, ret);
107         OsalMemFree(tmpRawData);
108         return result;
109     }
110     do {
111         if (PreProcessRawData(rawData, size, tmpRawData, dataSize + 1) != true) {
112             break;
113         }
114         ret = g_wlanObj->CreateFeature(g_wlanObj, wlanType, &ifeature);
115         if (ret != HDF_SUCCESS) {
116             HDF_LOGE("%{public}s: CreateFeature failed! ret=%{public}d", __FUNCTION__, ret);
117             break;
118         }
119         FuncToOptimal(g_wlanObj, cmdId, tmpRawData);
120         ret = g_wlanObj->DestroyFeature(g_wlanObj, &ifeature);
121         if (ret != HDF_SUCCESS) {
122             HDF_LOGE("%{public}s: DestroyFeature failed! ret=%{public}d", __FUNCTION__, ret);
123             break;
124         }
125         result = true;
126     } while (false);
127     ret = g_wlanObj->Stop(g_wlanObj);
128     if (ret != HDF_SUCCESS) {
129         HDF_LOGE("%{public}s: Stop failed! ret=%{public}d", __FUNCTION__, ret);
130         result = false;
131     }
132     IWlanInterfaceReleaseInstance(g_wlanServiceName, g_wlanObj, false);
133     OsalMemFree(tmpRawData);
134     return result;
135 }
136 } // namespace WIFI
137 } // namespace OHOS
138 
139 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)140 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
141 {
142     if (size < OHOS::WIFI::THRESHOLD) {
143         return 0;
144     }
145 
146     /* Run your code on data */
147     OHOS::WIFI::DoSomethingInterestingWithMyAPI(data, size);
148     return 0;
149 }
150