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 #include "permission_manager.h"
33
34
35 namespace OHOS {
36 using namespace OHOS::Location;
37 const int32_t MAX_MEM_SIZE = 4 * 1024 * 1024;
38 const int32_t SLEEP_TIMES = 1000;
39 const int32_t LOCATION_PERM_NUM = 4;
40
MockNativePermission()41 void MockNativePermission()
42 {
43 const char *perms[] = {
44 ACCESS_LOCATION.c_str(), ACCESS_APPROXIMATELY_LOCATION.c_str(),
45 ACCESS_BACKGROUND_LOCATION.c_str(), MANAGE_SECURE_SETTINGS.c_str(),
46 };
47 NativeTokenInfoParams infoInstance = {
48 .dcapsNum = 0,
49 .permsNum = LOCATION_PERM_NUM,
50 .aclsNum = 0,
51 .dcaps = nullptr,
52 .perms = perms,
53 .acls = nullptr,
54 .processName = "NetworkAbility_FuzzTest",
55 .aplStr = "system_basic",
56 };
57 auto tokenId = GetAccessTokenId(&infoInstance);
58 SetSelfTokenID(tokenId);
59 Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
60 LocatorAbility::GetInstance()->EnableAbility(true);
61 }
62
ParseData(const uint8_t * data,size_t size)63 char* ParseData(const uint8_t* data, size_t size)
64 {
65 if (data == nullptr) {
66 return nullptr;
67 }
68
69 if (size > MAX_MEM_SIZE) {
70 return nullptr;
71 }
72
73 char* ch = (char *)malloc(size + 1);
74 if (ch == nullptr) {
75 return nullptr;
76 }
77
78 (void)memset_s(ch, size + 1, 0x00, size + 1);
79 if (memcpy_s(ch, size, data, size) != EOK) {
80 free(ch);
81 ch = nullptr;
82 return nullptr;
83 }
84 return ch;
85 }
86
87 #ifdef FEATURE_NETWORK_SUPPORT
NetworkAbility001FuzzTest(const char * data,size_t size)88 bool NetworkAbility001FuzzTest(const char* data, size_t size)
89 {
90 MessageParcel requestParcel;
91 requestParcel.WriteInterfaceToken(u"location.INetworkAbility");
92 requestParcel.WriteBuffer(data, size);
93 requestParcel.RewindRead(0);
94
95 MessageParcel reply;
96 MessageOption option;
97 auto ability = sptr<NetworkAbility>(new (std::nothrow) NetworkAbility());
98 ability->OnRemoteRequest(static_cast<uint32_t>(NetworkInterfaceCode::SEND_LOCATION_REQUEST),
99 requestParcel, reply, option);
100 return true;
101 }
102
NetworkAbility002FuzzTest(const char * data,size_t size)103 bool NetworkAbility002FuzzTest(const char* data, size_t size)
104 {
105 MessageParcel requestParcel;
106 requestParcel.WriteInterfaceToken(u"location.INetworkAbility");
107 requestParcel.WriteBuffer(data, size);
108 requestParcel.RewindRead(0);
109
110 MessageParcel reply;
111 MessageOption option;
112 auto ability = sptr<NetworkAbility>(new (std::nothrow) NetworkAbility());
113 ability->OnRemoteRequest(static_cast<uint32_t>(NetworkInterfaceCode::SET_MOCKED_LOCATIONS),
114 requestParcel, reply, option);
115 return true;
116 }
117
NetworkAbility003FuzzTest(const char * data,size_t size)118 bool NetworkAbility003FuzzTest(const char* data, size_t size)
119 {
120 MessageParcel requestParcel;
121 requestParcel.WriteInterfaceToken(u"location.INetworkAbility");
122 requestParcel.WriteBuffer(data, size);
123 requestParcel.RewindRead(0);
124
125 MessageParcel reply;
126 MessageOption option;
127 auto ability = sptr<NetworkAbility>(new (std::nothrow) NetworkAbility());
128 ability->OnRemoteRequest(static_cast<uint32_t>(NetworkInterfaceCode::SELF_REQUEST),
129 requestParcel, reply, option);
130 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
131 return true;
132 }
133
NetworkAbility004FuzzTest(const char * data,size_t size)134 bool NetworkAbility004FuzzTest(const char* data, size_t size)
135 {
136 MessageParcel requestParcel;
137 requestParcel.WriteInterfaceToken(u"location.INetworkAbility");
138 requestParcel.WriteBuffer(data, size);
139 requestParcel.RewindRead(0);
140
141 MessageParcel reply;
142 MessageOption option;
143 auto ability = sptr<NetworkAbility>(new (std::nothrow) NetworkAbility());
144 ability->OnRemoteRequest(static_cast<uint32_t>(NetworkInterfaceCode::SET_ENABLE),
145 requestParcel, reply, option);
146 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
147 return true;
148 }
149
NetworkAbility005FuzzTest(const char * data,size_t size)150 bool NetworkAbility005FuzzTest(const char* data, size_t size)
151 {
152 MessageParcel requestParcel;
153 requestParcel.WriteInterfaceToken(u"location.INetworkAbility");
154 requestParcel.WriteBuffer(data, size);
155 requestParcel.RewindRead(0);
156
157 MessageParcel reply;
158 MessageOption option;
159 auto ability = sptr<NetworkAbility>(new (std::nothrow) NetworkAbility());
160 ability->OnRemoteRequest(static_cast<uint32_t>(NetworkInterfaceCode::ENABLE_LOCATION_MOCK),
161 requestParcel, reply, option);
162 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
163 return true;
164 }
165
NetworkAbility006FuzzTest(const char * data,size_t size)166 bool NetworkAbility006FuzzTest(const char* data, size_t size)
167 {
168 MessageParcel requestParcel;
169 requestParcel.WriteInterfaceToken(u"location.INetworkAbility");
170 requestParcel.WriteBuffer(data, size);
171 requestParcel.RewindRead(0);
172
173 MessageParcel reply;
174 MessageOption option;
175 auto ability = sptr<NetworkAbility>(new (std::nothrow) NetworkAbility());
176 ability->OnRemoteRequest(static_cast<uint32_t>(NetworkInterfaceCode::DISABLE_LOCATION_MOCK),
177 requestParcel, reply, option);
178 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
179 return true;
180 }
181 #endif
182 } // namespace OHOS
183
184 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)185 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
186 {
187 OHOS::MockNativePermission();
188 char* ch = OHOS::ParseData(data, size);
189 if (ch != nullptr) {
190 #ifdef FEATURE_NETWORK_SUPPORT
191 OHOS::NetworkAbility001FuzzTest(ch, size);
192 OHOS::NetworkAbility002FuzzTest(ch, size);
193 OHOS::NetworkAbility003FuzzTest(ch, size);
194 OHOS::NetworkAbility004FuzzTest(ch, size);
195 OHOS::NetworkAbility005FuzzTest(ch, size);
196 OHOS::NetworkAbility006FuzzTest(ch, size);
197 #endif
198 free(ch);
199 ch = nullptr;
200 }
201 return 0;
202 }
203
204