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,bool bIpv4)90 void OnStopDhcpClientTest(const std::string& ifname, size_t size, bool ipv6, bool bIpv4)
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 datas.WriteBool(bIpv4);
99 MessageParcel reply;
100 MessageOption option;
101 pDhcpClientStub->OnRemoteRequest(code, datas, reply, option);
102 }
103
OnStopClientSaTest(const uint8_t * data,size_t size)104 void OnStopClientSaTest(const uint8_t* data, size_t size)
105 {
106 uint32_t code = static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_SA);
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 pDhcpClientStub->OnRemoteRequest(code, datas, reply, option);
115 }
116
OnDealWifiDhcpCacheTest(const uint8_t * data,size_t size)117 void OnDealWifiDhcpCacheTest(const uint8_t* data, size_t size)
118 {
119 uint32_t code = static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_DEAL_DHCP_CACHE);
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 pDhcpClientStub->OnRemoteRequest(code, datas, reply, option);
128 }
129
IsGlobalIPv6AddressTest(const uint8_t * data,size_t size)130 void IsGlobalIPv6AddressTest(const uint8_t* data, size_t size)
131 {
132 std::string ipAddress = std::string(reinterpret_cast<const char*>(data), size);
133 pDhcpClientServiceImpl->IsGlobalIPv6Address(ipAddress);
134 }
135
DhcpOfferResultSuccessTest(const uint8_t * data,size_t size)136 void DhcpOfferResultSuccessTest(const uint8_t* data, size_t size)
137 {
138 std::string ipAddress = std::string(reinterpret_cast<const char*>(data), size);
139 struct DhcpIpResult ipResult;
140 struct DhcpIpv6Info info;
141 ipResult.ifname = ipAddress;
142 ipResult.uAddTime = static_cast<uint32_t>(data[0]);
143 pDhcpClientServiceImpl->DhcpOfferResultSuccess(ipResult);
144 pDhcpClientServiceImpl->DhcpIpv4ResultSuccess(ipResult);
145 pDhcpClientServiceImpl->DhcpIpv4ResultFail(ipResult);
146 pDhcpClientServiceImpl->DhcpIpv4ResultExpired(ipAddress);
147 pDhcpClientServiceImpl->DhcpIpv6ResulCallback(ipAddress, info);
148 }
149
150 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)151 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
152 {
153 if ((data == nullptr) || (size <= OHOS::DHCP::U32_AT_SIZE_ZERO)) {
154 return 0;
155 }
156 std::string ifname = "wlan0";
157 OnRegisterCallBackTest(ifname, size);
158 OnStartDhcpClientTest(ifname, size, false, data);
159 OnStopDhcpClientTest(ifname, size, false, true);
160 OnDealWifiDhcpCacheTest(data, size);
161 DhcpOfferResultSuccessTest(data, size);
162 OnStopClientSaTest(data, size);
163 sleep(1);
164 return 0;
165 }
166 } // namespace DHCP
167 } // namespace OHOS