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 <cstdio>
20 #include <cstdlib>
21 #include <fcntl.h>
22 #include <sys/time.h>
23 #include <cerrno>
24 #include <csignal>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/socket.h>
28 #include <sys/file.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 #include <netinet/if_ether.h>
32 #include <net/if_arp.h>
33 #include <string>
34 #include "clientstub_fuzzer.h"
35 #include "message_parcel.h"
36 #include "securec.h"
37 #include "dhcp_client_service_impl.h"
38 #include "dhcp_client_stub.h"
39 #include "dhcp_manager_service_ipc_interface_code.h"
40 #include "dhcp_fuzz_common_func.h"
41 #include "dhcp_ipv6_client.h"
42 #include "dhcp_client_callback_stub.h"
43 #include "dhcp_logger.h"
44
45 namespace OHOS {
46 namespace DHCP {
47 constexpr size_t U32_AT_SIZE_ZERO = 4;
48 constexpr int THREE = 2;
49 const std::u16string FORMMGR_INTERFACE_TOKEN = u"ohos.wifi.IDhcpClient";
50 sptr<DhcpClientStub> pDhcpClientStub = DhcpClientServiceImpl::GetInstance();
51 static sptr<DhcpClientCallBackStub> g_dhcpClientCallBackStub =
52 sptr<DhcpClientCallBackStub>(new (std::nothrow)DhcpClientCallBackStub());
53 std::shared_ptr<DhcpClientServiceImpl> pDhcpClientServiceImpl = std::make_shared<DhcpClientServiceImpl>();
54 static std::unique_ptr<DhcpIpv6Client> ipv6Client = std::make_unique<DhcpIpv6Client>("wlan0");
55
OnRegisterCallBackTest(const std::string & ifname,size_t size)56 void OnRegisterCallBackTest(const std::string& ifname, size_t size)
57 {
58 uint32_t code = static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_REG_CALL_BACK);
59 MessageParcel datas;
60 datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
61 datas.WriteInt32(0);
62 if (!datas.WriteRemoteObject(g_dhcpClientCallBackStub->AsObject())) {
63 return;
64 }
65 datas.WriteString(ifname);
66 datas.RewindRead(0);
67 MessageParcel reply;
68 MessageOption option;
69 pDhcpClientStub->OnRemoteRequest(code, datas, reply, option);
70 }
71
OnStartDhcpClientTest(const std::string & ifname,size_t size,bool ipv6,const uint8_t * data)72 void OnStartDhcpClientTest(const std::string& ifname, size_t size, bool ipv6, const uint8_t* data)
73 {
74 uint32_t code = static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_START_DHCP_CLIENT);
75 MessageParcel datas;
76 datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
77 datas.WriteInt32(0);
78 datas.WriteString(ifname);
79 datas.WriteString(std::string(reinterpret_cast<const char*>(data), size));
80 datas.WriteBool(ipv6);
81 datas.WriteBool(ipv6);
82 datas.WriteBool(static_cast<int>(data[0]) % THREE);
83 datas.RewindRead(0);
84 ipv6Client->runFlag_ = true;
85 MessageParcel reply;
86 MessageOption option;
87 pDhcpClientStub->OnRemoteRequest(code, datas, reply, option);
88 }
89
OnStopDhcpClientTest(const std::string & ifname,size_t size,bool ipv6)90 void OnStopDhcpClientTest(const std::string& ifname, size_t size, bool ipv6)
91 {
92 uint32_t code = static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_DHCP_CLIENT);
93 MessageParcel datas;
94 datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
95 datas.WriteInt32(0);
96 datas.WriteString(ifname);
97 datas.WriteBool(ipv6);
98 MessageParcel reply;
99 MessageOption option;
100 pDhcpClientStub->OnRemoteRequest(code, datas, reply, option);
101 }
102
OnStopClientSaTest(const uint8_t * data,size_t size)103 void OnStopClientSaTest(const uint8_t* data, size_t size)
104 {
105 uint32_t code = static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_SA);
106 MessageParcel datas;
107 datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
108 datas.WriteInt32(0);
109 datas.WriteBuffer(data, size);
110 datas.RewindRead(0);
111 MessageParcel reply;
112 MessageOption option;
113 pDhcpClientStub->OnRemoteRequest(code, datas, reply, option);
114 }
115
OnDealWifiDhcpCacheTest(const uint8_t * data,size_t size)116 void OnDealWifiDhcpCacheTest(const uint8_t* data, size_t size)
117 {
118 uint32_t code = static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_DEAL_DHCP_CACHE);
119 MessageParcel datas;
120 datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
121 datas.WriteInt32(0);
122 datas.WriteBuffer(data, size);
123 datas.RewindRead(0);
124 MessageParcel reply;
125 MessageOption option;
126 pDhcpClientStub->OnRemoteRequest(code, datas, reply, option);
127 }
128
IsGlobalIPv6AddressTest(const uint8_t * data,size_t size)129 void IsGlobalIPv6AddressTest(const uint8_t* data, size_t size)
130 {
131 std::string ipAddress = std::string(reinterpret_cast<const char*>(data), size);
132 pDhcpClientServiceImpl->IsGlobalIPv6Address(ipAddress);
133 }
134
DhcpOfferResultSuccessTest(const uint8_t * data,size_t size)135 void DhcpOfferResultSuccessTest(const uint8_t* data, size_t size)
136 {
137 std::string ipAddress = std::string(reinterpret_cast<const char*>(data), size);
138 struct DhcpIpResult ipResult;
139 struct DhcpIpv6Info info;
140 ipResult.ifname = ipAddress;
141 ipResult.uAddTime = static_cast<uint32_t>(data[0]);
142 pDhcpClientServiceImpl->DhcpOfferResultSuccess(ipResult);
143 pDhcpClientServiceImpl->DhcpIpv4ResultSuccess(ipResult);
144 pDhcpClientServiceImpl->DhcpIpv4ResultFail(ipResult);
145 pDhcpClientServiceImpl->DhcpIpv4ResultExpired(ipAddress);
146 pDhcpClientServiceImpl->DhcpIpv6ResulCallback(ipAddress, info);
147 }
148
149 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)150 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
151 {
152 if ((data == nullptr) || (size <= OHOS::DHCP::U32_AT_SIZE_ZERO)) {
153 return 0;
154 }
155 std::string ifname = "wlan0";
156 OnRegisterCallBackTest(ifname, size);
157 OnStartDhcpClientTest(ifname, size, false, data);
158 OnStopDhcpClientTest(ifname, size, false);
159 OnDealWifiDhcpCacheTest(data, size);
160 DhcpOfferResultSuccessTest(data, size);
161 OnStopClientSaTest(data, size);
162 return 0;
163 }
164 } // namespace DHCP
165 } // namespace OHOS