1 /*
2 * Copyright (c) 2023 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 "geoconvertservice_fuzzer.h"
17
18 #include "accesstoken_kit.h"
19 #include "if_system_ability_manager.h"
20 #include "iservice_registry.h"
21 #include "message_option.h"
22 #include "message_parcel.h"
23 #include "nativetoken_kit.h"
24 #include "system_ability_definition.h"
25 #include "token_setproc.h"
26 #include "locationhub_ipc_interface_code.h"
27
28 #include "location.h"
29 #include "locator_ability.h"
30 #ifdef FEATURE_GEOCODE_SUPPORT
31 #include "geo_convert_service.h"
32 #include "geo_convert_request.h"
33 #endif
34 #include "permission_manager.h"
35
36 namespace OHOS {
37 using namespace OHOS::Location;
38 const int32_t MAX_MEM_SIZE = 4 * 1024 * 1024;
39 const int32_t LOCATION_PERM_NUM = 4;
MockNativePermission()40 void MockNativePermission()
41 {
42 const char *perms[] = {
43 ACCESS_LOCATION.c_str(), ACCESS_APPROXIMATELY_LOCATION.c_str(),
44 ACCESS_BACKGROUND_LOCATION.c_str(), MANAGE_SECURE_SETTINGS.c_str(),
45 };
46 NativeTokenInfoParams infoInstance = {
47 .dcapsNum = 0,
48 .permsNum = LOCATION_PERM_NUM,
49 .aclsNum = 0,
50 .dcaps = nullptr,
51 .perms = perms,
52 .acls = nullptr,
53 .processName = "Geocode_FuzzTest",
54 .aplStr = "system_basic",
55 };
56 auto tokenId = GetAccessTokenId(&infoInstance);
57 SetSelfTokenID(tokenId);
58 Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
59 LocatorAbility::GetInstance()->EnableAbility(true);
60 }
61
ParseData(const uint8_t * data,size_t size)62 char* ParseData(const uint8_t* data, size_t size)
63 {
64 if (data == nullptr) {
65 return nullptr;
66 }
67
68 if (size > MAX_MEM_SIZE) {
69 return nullptr;
70 }
71
72 char* ch = (char *)malloc(size + 1);
73 if (ch == nullptr) {
74 return nullptr;
75 }
76
77 (void)memset_s(ch, size + 1, 0x00, size + 1);
78 if (memcpy_s(ch, size, data, size) != EOK) {
79 free(ch);
80 ch = nullptr;
81 return nullptr;
82 }
83 return ch;
84 }
85
86 #ifdef FEATURE_GEOCODE_SUPPORT
GeoConvertServiceFuzzTest001(const char * data,size_t size)87 bool GeoConvertServiceFuzzTest001(const char* data, size_t size)
88 {
89 MessageParcel requestParcel;
90 requestParcel.WriteInterfaceToken(u"location.IGeoConvert");
91 requestParcel.WriteBuffer(data, size);
92 requestParcel.RewindRead(0);
93
94 MessageParcel reply;
95 MessageOption option;
96 auto service1 = sptr<GeoConvertService>(new (std::nothrow) GeoConvertService());
97 service1->OnRemoteRequest(static_cast<uint32_t>(GeoConvertInterfaceCode::IS_AVAILABLE),
98 requestParcel, reply, option);
99
100 MessageParcel request1;
101 MessageParcel request2;
102 std::unique_ptr<GeoConvertRequest> geoConvertRequest = std::make_unique<GeoConvertRequest>();
103 geoConvertRequest->GetLocale();
104 geoConvertRequest->SetLocale("zh");
105 geoConvertRequest->GetLatitude();
106 geoConvertRequest->SetLatitude(0.0);
107 geoConvertRequest->GetLongitude();
108 geoConvertRequest->SetLongitude(0.0);
109 geoConvertRequest->GetMaxItems();
110 geoConvertRequest->SetMaxItems(0.0);
111 geoConvertRequest->GetDescription();
112 geoConvertRequest->SetDescription("zh");
113 geoConvertRequest->GetMaxLatitude();
114 geoConvertRequest->SetMaxLatitude(0.0);
115 geoConvertRequest->GetMaxLongitude();
116 geoConvertRequest->SetMaxLongitude(0.0);
117 geoConvertRequest->GetMinLatitude();
118 geoConvertRequest->SetMinLatitude(0.0);
119 geoConvertRequest->GetMinLongitude();
120 geoConvertRequest->SetMinLongitude(0.0);
121 geoConvertRequest->GetBundleName();
122 geoConvertRequest->SetBundleName("zh");
123 geoConvertRequest->SetCallback(geoConvertRequest->GetCallback());
124 geoConvertRequest->GetTransId();
125 geoConvertRequest->SetTransId("zh");
126 geoConvertRequest->GetCountry();
127 geoConvertRequest->SetCountry("zh");
128 geoConvertRequest->GetRequestType();
129 geoConvertRequest->SetRequestType(GeoCodeType::REQUEST_GEOCODE);
130 geoConvertRequest->Marshalling(request1);
131 geoConvertRequest->SetRequestType(GeoCodeType::REQUEST_REVERSE_GEOCODE);
132 geoConvertRequest->Marshalling(request2);
133 return true;
134 }
135
GeoConvertServiceFuzzTest002(const char * data,size_t size)136 bool GeoConvertServiceFuzzTest002(const char* data, size_t size)
137 {
138 MessageParcel requestParcel;
139 requestParcel.WriteInterfaceToken(u"location.IGeoConvert");
140 requestParcel.WriteBuffer(data, size);
141 requestParcel.RewindRead(0);
142
143 MessageParcel reply;
144 MessageOption option;
145
146 auto service2 = sptr<GeoConvertService>(new (std::nothrow) GeoConvertService());
147 service2->OnRemoteRequest(static_cast<uint32_t>(GeoConvertInterfaceCode::GET_FROM_COORDINATE),
148 requestParcel, reply, option);
149
150 return true;
151 }
152
GeoConvertServiceFuzzTest003(const char * data,size_t size)153 bool GeoConvertServiceFuzzTest003(const char* data, size_t size)
154 {
155 MessageParcel requestParcel;
156 requestParcel.WriteInterfaceToken(u"location.IGeoConvert");
157 requestParcel.WriteBuffer(data, size);
158 requestParcel.RewindRead(0);
159
160 MessageParcel reply;
161 MessageOption option;
162
163 auto service3 = sptr<GeoConvertService>(new (std::nothrow) GeoConvertService());
164 service3->OnRemoteRequest(static_cast<uint32_t>(GeoConvertInterfaceCode::GET_FROM_LOCATION_NAME_BY_BOUNDARY),
165 requestParcel, reply, option);
166
167 return true;
168 }
169
GeoConvertServiceFuzzTest004(const char * data,size_t size)170 bool GeoConvertServiceFuzzTest004(const char* data, size_t size)
171 {
172 MessageParcel requestParcel;
173 requestParcel.WriteInterfaceToken(u"location.IGeoConvert");
174 requestParcel.WriteBuffer(data, size);
175 requestParcel.RewindRead(0);
176
177 MessageParcel reply;
178 MessageOption option;
179
180 auto service4 = sptr<GeoConvertService>(new (std::nothrow) GeoConvertService());
181 service4->OnRemoteRequest(static_cast<uint32_t>(GeoConvertInterfaceCode::ENABLE_REVERSE_GEOCODE_MOCK),
182 requestParcel, reply, option);
183
184 return true;
185 }
186
GeoConvertServiceFuzzTest005(const char * data,size_t size)187 bool GeoConvertServiceFuzzTest005(const char* data, size_t size)
188 {
189 MessageParcel requestParcel;
190 requestParcel.WriteInterfaceToken(u"location.IGeoConvert");
191 requestParcel.WriteBuffer(data, size);
192 requestParcel.RewindRead(0);
193
194 MessageParcel reply;
195 MessageOption option;
196
197 auto service5 = sptr<GeoConvertService>(new (std::nothrow) GeoConvertService());
198 service5->OnRemoteRequest(static_cast<uint32_t>(GeoConvertInterfaceCode::DISABLE_REVERSE_GEOCODE_MOCK),
199 requestParcel, reply, option);
200
201 return true;
202 }
203
GeoConvertServiceFuzzTest006(const char * data,size_t size)204 bool GeoConvertServiceFuzzTest006(const char* data, size_t size)
205 {
206 MessageParcel requestParcel;
207 requestParcel.WriteInterfaceToken(u"location.IGeoConvert");
208 requestParcel.WriteBuffer(data, size);
209 requestParcel.RewindRead(0);
210
211 MessageParcel reply;
212 MessageOption option;
213
214 auto service6 = sptr<GeoConvertService>(new (std::nothrow) GeoConvertService());
215 service6->OnRemoteRequest(static_cast<uint32_t>(GeoConvertInterfaceCode::SET_REVERSE_GEOCODE_MOCKINFO),
216 requestParcel, reply, option);
217
218 return true;
219 }
220 #endif
221 } // namespace OHOS
222
223 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)224 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
225 {
226 OHOS::MockNativePermission();
227 char* ch = OHOS::ParseData(data, size);
228 if (ch != nullptr) {
229 #ifdef FEATURE_GEOCODE_SUPPORT
230 OHOS::GeoConvertServiceFuzzTest001(ch, size);
231 OHOS::GeoConvertServiceFuzzTest002(ch, size);
232 OHOS::GeoConvertServiceFuzzTest003(ch, size);
233 OHOS::GeoConvertServiceFuzzTest004(ch, size);
234 OHOS::GeoConvertServiceFuzzTest005(ch, size);
235 OHOS::GeoConvertServiceFuzzTest006(ch, size);
236 #endif
237 free(ch);
238 ch = nullptr;
239 }
240 return 0;
241 }
242
243