1 /*
2 * Copyright (c) 2022 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 "networkability_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 "locator_ability.h"
27 #include "locationhub_ipc_interface_code.h"
28
29 #ifdef FEATURE_NETWORK_SUPPORT
30 #include "network_ability.h"
31 #endif
32
33
34 namespace OHOS {
35 using namespace OHOS::Location;
36 const int32_t MAX_MEM_SIZE = 4 * 1024 * 1024;
37 const int32_t SLEEP_TIMES = 1000;
38
ParseData(const uint8_t * data,size_t size)39 char* ParseData(const uint8_t* data, size_t size)
40 {
41 if (data == nullptr) {
42 return nullptr;
43 }
44
45 if (size > MAX_MEM_SIZE) {
46 return nullptr;
47 }
48
49 char* ch = (char *)malloc(size + 1);
50 if (ch == nullptr) {
51 return nullptr;
52 }
53
54 (void)memset_s(ch, size + 1, 0x00, size + 1);
55 if (memcpy_s(ch, size, data, size) != EOK) {
56 free(ch);
57 ch = nullptr;
58 return nullptr;
59 }
60 return ch;
61 }
62
63 #ifdef FEATURE_NETWORK_SUPPORT
NetworkAbilityFuzzTest(const char * data,size_t size)64 bool NetworkAbilityFuzzTest(const char* data, size_t size)
65 {
66 MessageParcel requestParcel;
67 requestParcel.WriteInterfaceToken(u"location.INetworkAbility");
68 requestParcel.WriteBuffer(data, size);
69 requestParcel.RewindRead(0);
70
71 MessageParcel reply;
72 MessageOption option;
73 auto ability1 = sptr<NetworkAbility>(new (std::nothrow) NetworkAbility());
74 ability1->OnRemoteRequest(static_cast<uint32_t>(NetworkInterfaceCode::SEND_LOCATION_REQUEST),
75 requestParcel, reply, option);
76 auto ability2 = sptr<NetworkAbility>(new (std::nothrow) NetworkAbility());
77 ability2->OnRemoteRequest(static_cast<uint32_t>(NetworkInterfaceCode::SET_MOCKED_LOCATIONS),
78 requestParcel, reply, option);
79 auto ability3 = sptr<NetworkAbility>(new (std::nothrow) NetworkAbility());
80 ability3->OnRemoteRequest(static_cast<uint32_t>(NetworkInterfaceCode::SELF_REQUEST),
81 requestParcel, reply, option);
82 auto ability4 = sptr<NetworkAbility>(new (std::nothrow) NetworkAbility());
83 ability4->OnRemoteRequest(static_cast<uint32_t>(NetworkInterfaceCode::SET_ENABLE),
84 requestParcel, reply, option);
85 auto ability5 = sptr<NetworkAbility>(new (std::nothrow) NetworkAbility());
86 ability5->OnRemoteRequest(static_cast<uint32_t>(NetworkInterfaceCode::ENABLE_LOCATION_MOCK),
87 requestParcel, reply, option);
88 auto ability6 = sptr<NetworkAbility>(new (std::nothrow) NetworkAbility());
89 ability6->OnRemoteRequest(static_cast<uint32_t>(NetworkInterfaceCode::DISABLE_LOCATION_MOCK),
90 requestParcel, reply, option);
91 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
92
93 return true;
94 }
95 #endif
96 } // namespace OHOS
97
98 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)99 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
100 {
101 char* ch = OHOS::ParseData(data, size);
102 if (ch != nullptr) {
103 #ifdef FEATURE_NETWORK_SUPPORT
104 OHOS::NetworkAbilityFuzzTest(ch, size);
105 #endif
106 free(ch);
107 ch = nullptr;
108 }
109 return 0;
110 }
111
112