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 "deeplinkreserveconfig_fuzzer.h" 17 18#include <cstddef> 19#include <cstdint> 20#include <iostream> 21 22#include "securec.h" 23 24#define private public 25#include "deeplink_reserve_config.h" 26#undef private 27 28using json = nlohmann::json; 29using namespace OHOS::AAFwk; 30namespace OHOS { 31namespace { 32constexpr int INPUT_ZERO = 0; 33constexpr int INPUT_ONE = 1; 34constexpr int INPUT_TWO = 2; 35constexpr int INPUT_THREE = 3; 36constexpr size_t FOO_MAX_LEN = 1024; 37constexpr size_t U32_AT_SIZE = 4; 38constexpr size_t OFFSET_ZERO = 24; 39constexpr size_t OFFSET_ONE = 16; 40constexpr size_t OFFSET_TWO = 8; 41const std::string CONFIG_PATH = "/etc/ability_runtime/deeplink_reserve_config.json"; 42const std::string DEFAULT_RESERVE_CONFIG_PATH = "/system/etc/deeplink_reserve_config.json"; 43const std::string DEEPLINK_RESERVED_URI_NAME = "deepLinkReservedUri"; 44const std::string BUNDLE_NAME = "bundleName"; 45const std::string URIS_NAME = "uris"; 46const std::string SCHEME_NAME = "scheme"; 47const std::string HOST_NAME = "host"; 48const std::string PORT_NAME = "port"; 49const std::string PATH_NAME = "path"; 50const std::string PATH_START_WITH_NAME = "pathStartWith"; 51const std::string PATH_REGEX_NAME = "pathRegex"; 52const std::string TYPE_NAME = "type"; 53const std::string UTD_NAME = "utd"; 54const std::string PORT_SEPARATOR = ":"; 55const std::string SCHEME_SEPARATOR = "://"; 56const std::string PATH_SEPARATOR = "/"; 57const std::string PARAM_SEPARATOR = "?"; 58} 59uint32_t GetU32Data(const char* ptr) 60{ 61 // convert fuzz input data to an integer 62 return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[INPUT_TWO] << OFFSET_TWO) | 63 ptr[INPUT_THREE]; 64} 65bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) 66{ 67 auto &deepLinkReserveConfig = DeepLinkReserveConfig::GetInstance(); 68 deepLinkReserveConfig.GetConfigPath(); 69 deepLinkReserveConfig.LoadConfiguration(); 70 std::string linkString(data, size); 71 std::string bundleName(data, size); 72 deepLinkReserveConfig.isLinkReserved(linkString, bundleName); 73 ReserveUri reservedUri; 74 std::string link(data, size); 75 std::string strParam(data, size); 76 deepLinkReserveConfig.isUriMatched(reservedUri, link); 77 reservedUri.scheme = strParam; 78 deepLinkReserveConfig.isUriMatched(reservedUri, link); 79 reservedUri.host = strParam; 80 deepLinkReserveConfig.isUriMatched(reservedUri, link); 81 reservedUri.port = strParam; 82 deepLinkReserveConfig.isUriMatched(reservedUri, link); 83 reservedUri.path = strParam; 84 deepLinkReserveConfig.isUriMatched(reservedUri, link); 85 reservedUri.pathStartWith = strParam; 86 deepLinkReserveConfig.isUriMatched(reservedUri, link); 87 reservedUri.pathRegex = strParam; 88 deepLinkReserveConfig.isUriMatched(reservedUri, link); 89 std::vector<ReserveUri> uriList; 90 json jsonUriObject; 91 jsonUriObject["SCHEME_NAME"] = SCHEME_NAME; 92 deepLinkReserveConfig.LoadReservedUrilItem(jsonUriObject, uriList); 93 jsonUriObject["HOST_NAME"] = HOST_NAME; 94 deepLinkReserveConfig.LoadReservedUrilItem(jsonUriObject, uriList); 95 jsonUriObject["PORT_NAME"] = PORT_NAME; 96 deepLinkReserveConfig.LoadReservedUrilItem(jsonUriObject, uriList); 97 jsonUriObject["PATH_NAME"] = PATH_NAME; 98 deepLinkReserveConfig.LoadReservedUrilItem(jsonUriObject, uriList); 99 jsonUriObject["PATH_START_WITH_NAME"] = PATH_START_WITH_NAME; 100 deepLinkReserveConfig.LoadReservedUrilItem(jsonUriObject, uriList); 101 jsonUriObject["PATH_REGEX_NAME"] = PATH_REGEX_NAME; 102 deepLinkReserveConfig.LoadReservedUrilItem(jsonUriObject, uriList); 103 jsonUriObject["TYPE_NAME"] = TYPE_NAME; 104 deepLinkReserveConfig.LoadReservedUrilItem(jsonUriObject, uriList); 105 jsonUriObject["UTD_NAME"] = UTD_NAME; 106 deepLinkReserveConfig.LoadReservedUrilItem(jsonUriObject, uriList); 107 return true; 108} 109 110bool DoSomethingInterestingWithMyAPIOne(const char* data, size_t size) 111{ 112 auto &deepLinkReserveConfig1 = DeepLinkReserveConfig::GetInstance(); 113 std::string filePath(data, size); 114 json jsonBuf; 115 deepLinkReserveConfig1.ReadFileInfoJson(filePath, jsonBuf); 116 json object; 117 deepLinkReserveConfig1.LoadReservedUriList(object); 118 object["DEEPLINK_RESERVED_URI_NAME"] = DEEPLINK_RESERVED_URI_NAME; 119 deepLinkReserveConfig1.LoadReservedUriList(object); 120 int32_t userId = static_cast<int32_t>(GetU32Data(data)); 121 object["BUNDLE_NAME"] = userId; 122 deepLinkReserveConfig1.LoadReservedUriList(object); 123 object["BUNDLE_NAME"] = BUNDLE_NAME; 124 deepLinkReserveConfig1.LoadReservedUriList(object); 125 json uriArray = { "uri1", "uri2", "uri3" }; 126 object["URIS_NAME"] = uriArray; 127 deepLinkReserveConfig1.LoadReservedUriList(object); 128 object["URIS_NAME"] = URIS_NAME; 129 deepLinkReserveConfig1.LoadReservedUriList(object); 130 return true; 131} 132} 133 134/* Fuzzer entry point */ 135extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 136{ 137 /* Run your code on data */ 138 if (data == nullptr) { 139 std::cout << "invalid data" << std::endl; 140 return 0; 141 } 142 143 /* Validate the length of size */ 144 if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) { 145 return 0; 146 } 147 148 char* ch = (char*)malloc(size + 1); 149 if (ch == nullptr) { 150 std::cout << "malloc failed." << std::endl; 151 return 0; 152 } 153 154 (void)memset_s(ch, size + 1, 0x00, size + 1); 155 if (memcpy_s(ch, size + 1, data, size) != EOK) { 156 std::cout << "copy failed." << std::endl; 157 free(ch); 158 ch = nullptr; 159 return 0; 160 } 161 162 OHOS::DoSomethingInterestingWithMyAPI(ch, size); 163 OHOS::DoSomethingInterestingWithMyAPIOne(ch, size); 164 free(ch); 165 ch = nullptr; 166 return 0; 167} 168 169