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