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 #include <cstddef>
16 #include <cstdint>
17 #include <unistd.h>
18 #include <string>
19 #include "dhcpclientfun_fuzzer.h"
20 #include "dhcp_client_state_machine.h"
21 #include "dhcp_ipv6_client.h"
22 #include "dhcp_socket.h"
23 #include "securec.h"
24 #include <linux/rtnetlink.h>
25 #include <netinet/icmp6.h>
26
27 namespace OHOS {
28 namespace DHCP {
29 std::string g_ifname = "wlan0";
30 std::unique_ptr<OHOS::DHCP::DhcpIpv6Client> ipv6Client = std::make_unique<OHOS::DHCP::DhcpIpv6Client>("wlan0");
31
32
DhcpIpv6FunFuzzerTest(const uint8_t * data,size_t size)33 bool DhcpIpv6FunFuzzerTest(const uint8_t *data, size_t size)
34 {
35 if (ipv6Client == nullptr) {
36 return false;
37 }
38 if (data == nullptr) {
39 return false;
40 }
41 if (size <= 0) {
42 return false;
43 }
44 ipv6Client->handleKernelEvent(data, static_cast<int>(size));
45 return true;
46 }
47
48 /* Dhcp Ipv6 Event */
SetSocketFilterFuzzerTest(const uint8_t * data,size_t size)49 void SetSocketFilterFuzzerTest(const uint8_t *data, size_t size)
50 {
51 ipv6Client->setSocketFilter(nullptr);
52
53 struct sockaddr_nl addr;
54 ipv6Client->setSocketFilter(&addr);
55 }
56
57 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)58 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
59 {
60 if (data == nullptr) {
61 return 0;
62 }
63 DhcpIpv6FunFuzzerTest(data, size);
64 SetSocketFilterFuzzerTest(data, size);
65 return 0;
66 }
67 } // namespace DHCP
68 } // namespace OHOS