新增Fuzz接口

Signed-off-by: sunjingqiang <17319862417@163.com>
This commit is contained in:
sunjingqiang 2023-06-20 11:27:44 +08:00
parent 237351e768
commit c43dac1129
6 changed files with 358 additions and 3 deletions

View File

@ -0,0 +1,56 @@
# Copyright (C) 2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#####################hydra-fuzz###################
import("//build/config/features.gni")
import("//build/test.gni")
import("//foundation/communication/wifi/wifi/wifi.gni")
module_output_path = "wifi/wifi_sta"
##############################fuzztest##########################################
ohos_fuzztest("WifiHotSpotStubFuzzTest") {
module_out_path = module_output_path
fuzz_config_file = "$WIFI_ROOT_DIR/test/fuzztest/wifi_sta/wifihotspotstub_fuzzer"
include_dirs = [
"$WIFI_ROOT_DIR/interfaces/inner_api",
"$WIFI_ROOT_DIR/frameworks/native/include",
"$WIFI_ROOT_DIR/frameworks/native/interfaces",
"$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage",
"$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_toolkit/net_helper",
"$WIFI_ROOT_DIR/test/fuzztest/fuzz_common_func",
]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "wifip2pstub_fuzzer.cpp" ]
deps = [
"$WIFI_ROOT_DIR/frameworks/native:wifi_sdk",
"$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage:wifi_manager_service",
"$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage:wifi_hotspot_service_impl",
"$WIFI_ROOT_DIR/utils:wifi_utils",
]
external_deps = [
"c_utils:utils",
"ipc:ipc_single",
]
part_name = "wifi"
subsystem_name = "communication"
}

View File

@ -0,0 +1,16 @@
/*
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
FUZZ

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2022 Huawei Device Co., Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<fuzz_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>300</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>4096</rss_limit_mb>
</fuzztest>
</fuzz_config>

View File

@ -0,0 +1,216 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "wifip2pstub_fuzzer.h"
#include <cstddef>
#include <cstdint>
#include "wifi_p2p_stub.h"
#include "message_parcel.h"
#include "securec.h"
#include "define.h"
namespace OHOS {
namespace Wifi {
constexpr size_t FOO_MAX_LEN = 1024;
constexpr size_t U32_AT_SIZE = 4;
constexpr size_t MAP_NUMS = 21;
const std::u16string FORMMGR_INTERFACE_TOKEN = u"ohos.wifi.IWifiP2pService";
class WifiHotSpotStubTest : public WifiP2pStub {
public:
WifiP2pStubTest() = default;
virtual ~WifiP2pStubTest() = default;
ErrCode IsHotspotActive(bool &bActive) override
{
return WIFI_OPT_SUCCESS;
}
ErrCode IsHotspotDualBandSupported(bool &isSupported) override
{
return WIFI_OPT_SUCCESS;
}
ErrCode GetHotspotConfig(HotspotConfig &config) override
{
return WIFI_OPT_SUCCESS;
}
ErrCode GetHotspotState(int &state) override
{
return WIFI_OPT_SUCCESS;
}
ErrCode SetHotspotConfig(const HotspotConfig &config) override
{
return WIFI_OPT_SUCCESS;
}
ErrCode SetHotspotIdleTimeout(int time) override
{
return WIFI_OPT_SUCCESS;
}
ErrCode GetStationList(std::vector<StationInfo> &result) override
{
return WIFI_OPT_SUCCESS;
}
ErrCode DisassociateSta(const StationInfo &info) override
{
return WIFI_OPT_SUCCESS;
}
ErrCode EnableHotspot(const ServiceType type = ServiceType::DEFAULT) override
{
return WIFI_OPT_SUCCESS;
}
ErrCode DisableHotspot(const ServiceType type = ServiceType::DEFAULT) override
{
return WIFI_OPT_SUCCESS;
}
ErrCode GetBlockLists(std::vector<StationInfo> &infos) override
{
return WIFI_OPT_SUCCESS;
}
ErrCode AddBlockList(const StationInfo &info) override
{
return WIFI_OPT_SUCCESS;
}
ErrCode DelBlockList(const StationInfo &info) override
{
return WIFI_OPT_SUCCESS;
}
ErrCode GetValidBands(std::vector<BandType> &bands) override
{
return WIFI_OPT_SUCCESS;
}
ErrCode GetValidChannels(BandType band, std::vector<int32_t> &validchannels) override
{
return WIFI_OPT_SUCCESS;
}
ErrCode RegisterCallBack(const sptr<IWifiHotspotCallback> &callback,
const std::vector<std::string> &event) override
{
return WIFI_OPT_SUCCESS;
}
ErrCode GetSupportedFeatures(long &features) override
{
return WIFI_OPT_SUCCESS;
}
ErrCode GetSupportedPowerModel(std::set<PowerModel>& setPowerModelList) override
{
return WIFI_OPT_SUCCESS;
}
ErrCode GetPowerModel(PowerModel& model) override
{
return WIFI_OPT_SUCCESS;
}
ErrCode SetPowerModel(const PowerModel& model) override
{
return WIFI_OPT_SUCCESS;
}
bool IsRemoteDied()
{
return true;
}
};
uint32_t GetU32Data(const char* ptr)
{
return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
}
void OnGetSupportedFeaturesTest(const char* data, size_t size)
{
uint32_t code = GetU32Data(data) % MAP_NUMS + WIFI_SVR_CMD_ENABLE_WIFI_AP;
MessageParcel datas;
datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
datas.WriteBuffer(data, size);
datas.RewindRead(0);
MessageParcel reply;
MessageOption option;
std::shared_ptr<WifiHotspotStub> pWifiHotspotStub = std::make_shared<WifiHotSpotStubTest>();
pWifiHotspotStub->OnRemoteRequest(WIFI_SVR_CMD_P2P_ENABLE, datas, reply, option);
}
bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
{
uint32_t code = GetU32Data(data) % MAP_NUMS + WIFI_SVR_CMD_ENABLE_WIFI_AP;
MessageParcel datas;
datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
datas.WriteInt32(0);
datas.WriteBuffer(data, size);
datas.RewindRead(0);
MessageParcel reply;
MessageOption option;
std::shared_ptr<WifiHotspotStub> pWifiHotspotStub = std::make_shared<WifiP2pStubTest>();
OnGetSupportedFeaturesTest(data, size);
pWifiHotspotStub->OnRemoteRequest(code, datas, reply, option);
return true;
}
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
/* Run your code on data */
if (data == nullptr) {
return 0;
}
if (size < OHOS::Wifi::U32_AT_SIZE) {
return 0;
}
/* Validate the length of size */
if (size == 0 || size > OHOS::Wifi::FOO_MAX_LEN) {
return 0;
}
char* ch = (char *)malloc(size + 1);
if (ch == nullptr) {
return 0;
}
(void)memset_s(ch, size + 1, 0x00, size + 1);
if (memcpy_s(ch, size, data, size) != EOK) {
free(ch);
ch = nullptr;
return 0;
}
OHOS::Wifi::DoSomethingInterestingWithMyAPI(ch, size);
free(ch);
ch = nullptr;
return 0;
}
}
}

View File

@ -0,0 +1,21 @@
/*
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef WIFI_FUZZ_P2P_STUB_H
#define WIFI_FUZZ_P2P_STUB_H
#define FUZZ_PROJECT_NAME "wifip2pstub_fuzzer"
#endif

View File

@ -27,6 +27,7 @@ namespace OHOS {
namespace Wifi {
constexpr size_t FOO_MAX_LEN = 1024;
constexpr size_t U32_AT_SIZE = 4;
constexpr size_t MAP_P2P_NUMS = 41;
const std::u16string FORMMGR_INTERFACE_TOKEN = u"ohos.wifi.IWifiP2pService";
class WifiP2pStubTest : public WifiP2pStub {
@ -257,20 +258,40 @@ public:
}
};
bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
uint32_t GetU32Data(const char* ptr)
{
return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
}
void OnGetSupportedFeaturesTest(const char* data, size_t size)
{
MessageParcel datas;
datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
datas.WriteInt32(0);
datas.WriteBuffer(data, size);
datas.RewindRead(0);
MessageParcel reply;
MessageOption option;
std::shared_ptr<WifiHotspotStub> pWifiP2pStub = std::make_shared<WifiHotSpotStubTest>();
pWifiP2pStub->OnRemoteRequest(WIFI_SVR_CMD_GET_SUPPORTED_FEATURES, datas, reply, option);
}
bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
{
uint32_t code = GetU32Data(data) % MAP_P2P_NUMS + WIFI_SVR_CMD_P2P_ENABLE;
MessageParcel datas;
datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
datas.WriteInt32(0);
datas.WriteBuffer(data, size);
datas.RewindRead(0);
MessageParcel reply;
MessageOption option;
std::shared_ptr<WifiP2pStub> pWifiP2pStub = std::make_shared<WifiP2pStubTest>();
pWifiP2pStub->OnRemoteRequest(WIFI_SVR_CMD_P2P_ENABLE, datas, reply, option);
OnGetSupportedFeaturesTest(data, size);
pWifiP2pStub->OnRemoteRequest(code, datas, reply, option);
return true;
}
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{