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