1/* 2 * Copyright (c) 2022-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 <thread> 17 18#include <securec.h> 19 20#include "singleton.h" 21 22#include "i_net_policy_service.h" 23#include "net_conn_service_iface.h" 24#include "net_mgr_log_wrapper.h" 25#include "net_policy_constants.h" 26#include "net_policy_service_common.h" 27#include "net_stats_info.h" 28#define private public 29#include "net_manager_center.h" 30 31namespace OHOS { 32namespace NetManagerStandard { 33namespace { 34const uint8_t *g_baseFuzzData = nullptr; 35static constexpr uint32_t CREATE_NET_TYPE_VALUE = 7; 36static constexpr uint32_t CONVERT_NUMBER_TO_BOOL = 2; 37size_t g_baseFuzzSize = 0; 38size_t g_baseFuzzPos; 39constexpr size_t STR_LEN = 10; 40} // namespace 41 42template <class T> T NetCommonGetData() 43{ 44 T object{}; 45 size_t commonSize = sizeof(object); 46 if (g_baseFuzzData == nullptr || commonSize > g_baseFuzzSize - g_baseFuzzPos) { 47 return object; 48 } 49 errno_t ret = memcpy_s(&object, commonSize, g_baseFuzzData + g_baseFuzzPos, commonSize); 50 if (ret != EOK) { 51 return {}; 52 } 53 g_baseFuzzPos += commonSize; 54 return object; 55} 56 57std::string NetCommonGetString(int strlen) 58{ 59 char cstr[strlen]; 60 cstr[strlen - 1] = '\0'; 61 for (int i = 0; i < strlen - 1; i++) { 62 cstr[i] = NetCommonGetData<char>(); 63 } 64 std::string str(cstr); 65 return str; 66} 67 68bool IsCommonFuzzValidData(const uint8_t *data, size_t size) 69{ 70 if ((data == nullptr) || (size == 0)) { 71 return false; 72 } 73 g_baseFuzzData = data; 74 g_baseFuzzSize = size; 75 g_baseFuzzPos = 0; 76 return true; 77} 78 79static auto g_netManagerCenter = DelayedSingleton<NetManagerCenter>::GetInstance(); 80 81void GetIfaceNamesFuzzTest(const uint8_t *data, size_t size) 82{ 83 if (!IsCommonFuzzValidData(data, size)) { 84 return; 85 } 86 87 uint32_t netType = NetCommonGetData<uint32_t>() % CREATE_NET_TYPE_VALUE; 88 std::list<std::string> ifaceNames; 89 g_netManagerCenter->GetIfaceNames(static_cast<NetBearType>(netType), ifaceNames); 90 return; 91} 92 93void GetIfaceNameByTypeFuzzTest(const uint8_t *data, size_t size) 94{ 95 if (!IsCommonFuzzValidData(data, size)) { 96 return; 97 } 98 99 uint32_t bearerType = NetCommonGetData<uint32_t>() % CREATE_NET_TYPE_VALUE; 100 std::string ident = NetCommonGetString(STR_LEN); 101 std::string ifaceName = NetCommonGetString(STR_LEN); 102 g_netManagerCenter->GetIfaceNameByType(static_cast<NetBearType>(bearerType), ident, ifaceName); 103} 104 105void UnregisterNetSupplierFuzzTest(const uint8_t *data, size_t size) 106{ 107 if (!IsCommonFuzzValidData(data, size)) { 108 return; 109 } 110 uint32_t supplierId = NetCommonGetData<uint32_t>(); 111 g_netManagerCenter->UnregisterNetSupplier(supplierId); 112} 113 114void UpdateNetLinkInfoFuzzTest(const uint8_t *data, size_t size) 115{ 116 if (!IsCommonFuzzValidData(data, size)) { 117 return; 118 } 119 uint32_t supplierId = NetCommonGetData<uint32_t>(); 120 sptr<NetLinkInfo> netLinkInfo = new (std::nothrow) NetLinkInfo(); 121 if (netLinkInfo == nullptr) { 122 return; 123 } 124 125 g_netManagerCenter->UpdateNetLinkInfo(supplierId, netLinkInfo); 126} 127 128void UpdateNetSupplierInfoFuzzTest(const uint8_t *data, size_t size) 129{ 130 if (!IsCommonFuzzValidData(data, size)) { 131 return; 132 } 133 uint32_t supplierId = NetCommonGetData<uint32_t>(); 134 sptr<NetSupplierInfo> netSupplierInfo = new (std::nothrow) NetSupplierInfo(); 135 if (netSupplierInfo == nullptr) { 136 return; 137 } 138 g_netManagerCenter->UpdateNetSupplierInfo(supplierId, netSupplierInfo); 139} 140 141__attribute__((no_sanitize("cfi"))) void RegisterConnServiceFuzzTest(const uint8_t *data, size_t size) 142{ 143 if (!IsCommonFuzzValidData(data, size)) { 144 return; 145 } 146 sptr<NetConnServiceIface> serviceIface = new (std::nothrow) NetConnServiceIface(); 147 if (serviceIface == nullptr) { 148 return; 149 } 150 g_netManagerCenter->RegisterConnService(serviceIface); 151} 152 153void GetIfaceStatsDetailFuzzTest(const uint8_t *data, size_t size) 154{ 155 if (!IsCommonFuzzValidData(data, size)) { 156 return; 157 } 158 std::string iface = NetCommonGetString(STR_LEN); 159 uint32_t start = NetCommonGetData<uint32_t>(); 160 uint32_t end = NetCommonGetData<uint32_t>() + start; 161 NetStatsInfo info; 162 g_netManagerCenter->GetIfaceStatsDetail(iface, start, end, info); 163} 164 165void ResetStatsFactoryFuzzTest(const uint8_t *data, size_t size) 166{ 167 if (!IsCommonFuzzValidData(data, size)) { 168 return; 169 } 170 171 g_netManagerCenter->ResetStatsFactory(); 172} 173 174void RegisterStatsServiceFuzzTest(const uint8_t *data, size_t size) 175{ 176 if (!IsCommonFuzzValidData(data, size)) { 177 return; 178 } 179 sptr<NetStatsBaseService> service = nullptr; 180 181 g_netManagerCenter->RegisterStatsService(service); 182} 183 184void ResetPolicyFactoryFuzzTest(const uint8_t *data, size_t size) 185{ 186 if (!IsCommonFuzzValidData(data, size)) { 187 return; 188 } 189 190 g_netManagerCenter->ResetPolicyFactory(); 191} 192 193void ResetPoliciesFuzzTest(const uint8_t *data, size_t size) 194{ 195 if (!IsCommonFuzzValidData(data, size)) { 196 return; 197 } 198 g_netManagerCenter->ResetPolicies(); 199} 200 201void RegisterPolicyServiceFuzzTest(const uint8_t *data, size_t size) 202{ 203 if (!IsCommonFuzzValidData(data, size)) { 204 return; 205 } 206 sptr<NetPolicyBaseService> service = new (std::nothrow) NetPolicyServiceCommon(); 207 if (service == nullptr) { 208 return; 209 } 210 211 g_netManagerCenter->RegisterPolicyService(service); 212} 213 214void ResetEthernetFactoryFuzzTest(const uint8_t *data, size_t size) 215{ 216 if (!IsCommonFuzzValidData(data, size)) { 217 return; 218 } 219 220 g_netManagerCenter->ResetEthernetFactory(); 221} 222 223void RegisterEthernetServiceFuzzTest(const uint8_t *data, size_t size) 224{ 225 if (!IsCommonFuzzValidData(data, size)) { 226 return; 227 } 228 sptr<NetEthernetBaseService> service = nullptr; 229 g_netManagerCenter->RegisterEthernetService(service); 230} 231 232void GetAddressesByNameFuzzTest(const uint8_t *data, size_t size) 233{ 234 if (!IsCommonFuzzValidData(data, size)) { 235 return; 236 } 237 std::string hostName = NetCommonGetString(STR_LEN); 238 int32_t netId = NetCommonGetData<int32_t>(); 239 std::vector<INetAddr> addrInfo; 240 g_netManagerCenter->GetAddressesByName(hostName, netId, addrInfo); 241} 242 243void RegisterDnsServiceFuzzTest(const uint8_t *data, size_t size) 244{ 245 if (!IsCommonFuzzValidData(data, size)) { 246 return; 247 } 248 sptr<DnsBaseService> service = nullptr; 249 g_netManagerCenter->RegisterDnsService(service); 250} 251 252void RestrictBackgroundChangedFuzzTest(const uint8_t *data, size_t size) 253{ 254 if (!IsCommonFuzzValidData(data, size)) { 255 return; 256 } 257 bool isRestrictBackground = NetCommonGetData<uint32_t>() % CONVERT_NUMBER_TO_BOOL == 0; 258 g_netManagerCenter->RestrictBackgroundChanged(isRestrictBackground); 259} 260 261void IsUidNetAccessFuzzTest(const uint8_t *data, size_t size) 262{ 263 if (!IsCommonFuzzValidData(data, size)) { 264 return; 265 } 266 uint32_t uid = NetCommonGetData<uint32_t>(); 267 bool metered = NetCommonGetData<uint32_t>() % CONVERT_NUMBER_TO_BOOL == 0; 268 g_netManagerCenter->IsUidNetAccess(uid, metered); 269} 270 271void IsUidNetAllowedFuzzTest(const uint8_t *data, size_t size) 272{ 273 if (!IsCommonFuzzValidData(data, size)) { 274 return; 275 } 276 uint32_t uid = NetCommonGetData<uint32_t>(); 277 bool metered = NetCommonGetData<uint32_t>() % CONVERT_NUMBER_TO_BOOL == 0; 278 g_netManagerCenter->IsUidNetAllowed(uid, metered); 279} 280 281} // namespace NetManagerStandard 282} // namespace OHOS 283 284extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) 285{ 286 /* Run your code on data */ 287 OHOS::NetManagerStandard::RegisterConnServiceFuzzTest(data, size); 288 OHOS::NetManagerStandard::RegisterStatsServiceFuzzTest(data, size); 289 OHOS::NetManagerStandard::RegisterDnsServiceFuzzTest(data, size); 290 OHOS::NetManagerStandard::RegisterEthernetServiceFuzzTest(data, size); 291 OHOS::NetManagerStandard::RegisterPolicyServiceFuzzTest(data, size); 292 OHOS::NetManagerStandard::GetIfaceNamesFuzzTest(data, size); 293 OHOS::NetManagerStandard::GetIfaceNameByTypeFuzzTest(data, size); 294 OHOS::NetManagerStandard::UpdateNetLinkInfoFuzzTest(data, size); 295 OHOS::NetManagerStandard::UpdateNetSupplierInfoFuzzTest(data, size); 296 OHOS::NetManagerStandard::GetIfaceStatsDetailFuzzTest(data, size); 297 OHOS::NetManagerStandard::ResetStatsFactoryFuzzTest(data, size); 298 OHOS::NetManagerStandard::ResetPolicyFactoryFuzzTest(data, size); 299 OHOS::NetManagerStandard::ResetPoliciesFuzzTest(data, size); 300 OHOS::NetManagerStandard::ResetEthernetFactoryFuzzTest(data, size); 301 OHOS::NetManagerStandard::GetAddressesByNameFuzzTest(data, size); 302 OHOS::NetManagerStandard::RestrictBackgroundChangedFuzzTest(data, size); 303 OHOS::NetManagerStandard::IsUidNetAccessFuzzTest(data, size); 304 OHOS::NetManagerStandard::IsUidNetAllowedFuzzTest(data, size); 305 OHOS::NetManagerStandard::UnregisterNetSupplierFuzzTest(data, size); 306 307 return 0; 308}