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 "dhcpaddresspool_fuzzer.h" 17 18#include <cstddef> 19#include <cstdint> 20#include <unistd.h> 21#include "securec.h" 22#include "address_utils.h" 23#include "dhcp_address_pool.h" 24#include "dhcp_s_define.h" 25 26namespace OHOS { 27namespace Wifi { 28constexpr size_t DHCP_SLEEP_1 = 2; 29constexpr size_t U32_AT_SIZE_ZERO = 4; 30 31void DhcpAddressPoolFuzzTest(const uint8_t* data, size_t size) 32{ 33 AddressBinding binding; 34 memcpy_s(binding.chaddr, PID_MAX_LEN, "*", PID_MAX_LEN); 35 binding.ipAddress = static_cast<uint32_t>(data[0]); 36 binding.clientId = static_cast<uint32_t>(data[0]); 37 binding.bindingTime = static_cast<uint64_t>(data[0]); 38 binding.pendingTime = static_cast<uint64_t>(data[0]); 39 binding.expireIn = static_cast<uint64_t>(data[0]); 40 binding.leaseTime = static_cast<uint64_t>(data[0]); 41 binding.pendingInterval = static_cast<uint64_t>(data[0]); 42 DhcpAddressPool pool; 43 strncpy_s(pool.ifname, IFACE_NAME_SIZE, "*", IFACE_NAME_SIZE - 1); 44 pool.netmask = static_cast<uint32_t>(data[0]); 45 pool.serverId = static_cast<uint32_t>(data[0]); 46 pool.gateway = static_cast<uint32_t>(data[0]); 47 pool.leaseTime = static_cast<uint32_t>(data[0]); 48 pool.renewalTime = static_cast<uint32_t>(data[0]); 49 DhcpOptionList options; 50 options.size = static_cast<uint32_t>(data[0]); 51 const char *ifname = "wlan0"; 52 uint32_t ipAdd = 0; 53 memcpy_s(&ipAdd, sizeof(uint32_t), "192.168.100.1", sizeof(uint32_t)); 54 int force = static_cast<int>(data[0]); 55 int mode = static_cast<int>(data[0]); 56 uint8_t macAddr[DHCP_HWADDR_LENGTH]; 57 InitAddressPool(&pool, ifname, &options); 58 FreeAddressPool(&pool); 59 IsReservedIp(&pool, ipAdd); 60 AddReservedBinding(&macAddr[0]); 61 RemoveReservedBinding(&macAddr[0]); 62 RemoveBinding(&macAddr[0]); 63 AddBinding(&binding); 64 AddLease(&pool, &binding); 65 GetLease(&pool, ipAdd); 66 UpdateLease(&pool, &binding); 67 RemoveLease(&pool, &binding); 68 LoadBindingRecoders(&pool); 69 SaveBindingRecoders(&pool, force); 70 QueryBinding(macAddr, &options); 71 SetDistributeMode(mode); 72 GetDistributeMode(); 73 DeleteMacInLease(&pool, &binding); 74 DeleteMacInLease(nullptr, &binding); 75 DeleteMacInLease(&pool, nullptr); 76} 77 78void FindBindingByIpFuzzTest(const uint8_t* data, size_t size) 79{ 80 uint32_t ipAddress1 = ParseIpAddr("192.168.100.1"); 81 uint32_t ipAddress2 = ParseIpAddr("192.168.100.2"); 82 FindBindingByIp(ipAddress1); 83 FindBindingByIp(ipAddress2); 84} 85 86void DhcpMacAddrFuzzTest(const uint8_t* data, size_t size) 87{ 88 uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 89 uint8_t testMac2[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x0b, 0}; 90 uint8_t testMac3[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x0a, 0}; 91 ReleaseBinding((testMac1)); 92 ReleaseBinding((testMac2)); 93 IsReserved((testMac1)); 94 IsReserved((testMac3)); 95} 96 97/* Fuzzer entry point */ 98extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 99{ 100 if ((data == nullptr) || (size <= OHOS::Wifi::U32_AT_SIZE_ZERO)) { 101 return 0; 102 } 103 sleep(DHCP_SLEEP_1); 104 OHOS::Wifi::DhcpAddressPoolFuzzTest(data, size); 105 OHOS::Wifi::FindBindingByIpFuzzTest(data, size); 106 OHOS::Wifi::DhcpMacAddrFuzzTest(data, size); 107 return 0; 108} 109} // namespace Wifi 110} // namespace OHOS 111