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