1/* 2 * Copyright (C) 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 <cstddef> 17#include <cstdint> 18#include <unistd.h> 19#include "serverstub_fuzzer.h" 20#include "message_parcel.h" 21#include "securec.h" 22#include "dhcp_server_service_impl.h" 23#include "dhcp_server_stub.h" 24#include "dhcp_manager_service_ipc_interface_code.h" 25#include "dhcp_fuzz_common_func.h" 26 27namespace OHOS { 28namespace DHCP { 29constexpr size_t U32_AT_SIZE_ZERO = 4; 30constexpr size_t MAP_SCAN_NUMS = 12; 31constexpr size_t DHCP_SLEEP_1 = 2; 32const std::u16string FORMMGR_INTERFACE_TOKEN = u"ohos.wifi.IDhcpServer"; 33sptr<DhcpServerStub> pDhcpServerStub = DhcpServerServiceImpl::GetInstance(); 34 35void OnGetSupportedFeaturesTest(const uint8_t* data, size_t size) 36{ 37 uint32_t code = U32_AT(data) % MAP_SCAN_NUMS + static_cast<uint32_t> 38 (DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REG_CALL_BACK); 39 if (code == static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_GET_SERVER_STATUS) || 40 code == static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_GET_IP_LIST)) { 41 return; 42 } 43 MessageParcel datas; 44 datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN); 45 datas.WriteInt32(0); 46 datas.WriteBuffer(data, size); 47 datas.RewindRead(0); 48 MessageParcel reply; 49 MessageOption option; 50 pDhcpServerStub->OnRemoteRequest(code, datas, reply, option); 51} 52 53void OnRegisterCallBackTest(const uint8_t* data, size_t size) 54{ 55 MessageParcel datas; 56 datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN); 57 datas.WriteInt32(0); 58 datas.WriteBuffer(data, size); 59 datas.RewindRead(0); 60 MessageParcel reply; 61 MessageOption option; 62 pDhcpServerStub->OnRemoteRequest(static_cast<uint32_t> 63 (DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REG_CALL_BACK), datas, reply, option); 64} 65 66void OnStopDhcpServerTest(const uint8_t* data, size_t size) 67{ 68 MessageParcel datas; 69 datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN); 70 datas.WriteInt32(0); 71 datas.WriteBuffer(data, size); 72 datas.RewindRead(0); 73 MessageParcel reply; 74 MessageOption option; 75 pDhcpServerStub->OnRemoteRequest(static_cast<uint32_t> 76 (DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_STOP_DHCP_SERVER), datas, reply, option); 77} 78 79void OnSetDhcpNameTest(const uint8_t* data, size_t size) 80{ 81 MessageParcel datas; 82 datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN); 83 datas.WriteInt32(0); 84 datas.WriteBuffer(data, size); 85 datas.RewindRead(0); 86 MessageParcel reply; 87 MessageOption option; 88 pDhcpServerStub->OnRemoteRequest(static_cast<uint32_t> 89 (DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_SET_DHCP_NAME), datas, reply, option); 90} 91 92void OnSetDhcpRangeTest(const uint8_t* data, size_t size) 93{ 94 MessageParcel datas; 95 datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN); 96 datas.WriteInt32(0); 97 datas.WriteBuffer(data, size); 98 datas.RewindRead(0); 99 MessageParcel reply; 100 MessageOption option; 101 pDhcpServerStub->OnRemoteRequest(static_cast<uint32_t> 102 (DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_SET_DHCP_RANGE), datas, reply, option); 103} 104 105void OnRemoveAllDhcpRangeTest(const uint8_t* data, size_t size) 106{ 107 MessageParcel datas; 108 datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN); 109 datas.WriteInt32(0); 110 datas.WriteBuffer(data, size); 111 datas.RewindRead(0); 112 MessageParcel reply; 113 MessageOption option; 114 pDhcpServerStub->OnRemoteRequest(static_cast<uint32_t> 115 (DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REMOVE_ALL_DHCP_RANGE), datas, reply, option); 116} 117 118void OnRemoveDhcpRangeTest(const uint8_t* data, size_t size) 119{ 120 MessageParcel datas; 121 datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN); 122 datas.WriteInt32(0); 123 datas.WriteBuffer(data, size); 124 datas.RewindRead(0); 125 MessageParcel reply; 126 MessageOption option; 127 pDhcpServerStub->OnRemoteRequest(static_cast<uint32_t> 128 (DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REMOVE_DHCP_RANGE), datas, reply, option); 129} 130 131void OnGetDhcpClientInfosTest(const uint8_t* data, size_t size) 132{ 133 MessageParcel datas; 134 datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN); 135 datas.WriteInt32(0); 136 datas.WriteBuffer(data, size); 137 datas.RewindRead(0); 138 MessageParcel reply; 139 MessageOption option; 140 pDhcpServerStub->OnRemoteRequest(static_cast<uint32_t> 141 (DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_GET_DHCP_CLIENT_INFO), datas, reply, option); 142} 143 144void OnUpdateLeasesTimeTest(const uint8_t* data, size_t size) 145{ 146 MessageParcel datas; 147 datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN); 148 datas.WriteInt32(0); 149 datas.WriteBuffer(data, size); 150 datas.RewindRead(0); 151 MessageParcel reply; 152 MessageOption option; 153 pDhcpServerStub->OnRemoteRequest(static_cast<uint32_t> 154 (DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_UPDATE_RENEW_TIME), datas, reply, option); 155} 156 157void OnPutDhcpRangeTest(const uint8_t* data, size_t size) 158{ 159 MessageParcel datas; 160 datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN); 161 datas.WriteInt32(0); 162 datas.WriteBuffer(data, size); 163 datas.RewindRead(0); 164 MessageParcel reply; 165 MessageOption option; 166 pDhcpServerStub->OnRemoteRequest(static_cast<uint32_t> 167 (DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_PUT_DHCP_RANGE), datas, reply, option); 168} 169 170/* Fuzzer entry point */ 171extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 172{ 173 if ((data == nullptr) || (size <= OHOS::DHCP::U32_AT_SIZE_ZERO)) { 174 return 0; 175 } 176 sleep(DHCP_SLEEP_1); 177 OHOS::DHCP::OnGetSupportedFeaturesTest(data, size); 178 OHOS::DHCP::OnRegisterCallBackTest(data, size); 179 OHOS::DHCP::OnStopDhcpServerTest(data, size); 180 OHOS::DHCP::OnSetDhcpNameTest(data, size); 181 OHOS::DHCP::OnSetDhcpRangeTest(data, size); 182 OHOS::DHCP::OnRemoveAllDhcpRangeTest(data, size); 183 OHOS::DHCP::OnRemoveDhcpRangeTest(data, size); 184 OHOS::DHCP::OnGetDhcpClientInfosTest(data, size); 185 OHOS::DHCP::OnUpdateLeasesTimeTest(data, size); 186 OHOS::DHCP::OnPutDhcpRangeTest(data, size); 187 return 0; 188} 189} // namespace DHCP 190} // namespace OHOS 191