• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "passiveability_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_PASSIVE_SUPPORT
30 #include "passive_ability.h"
31 #endif
32 #include "permission_manager.h"
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 const int32_t LOCATION_PERM_NUM = 4;
39 
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 = "GnssAbility_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_PASSIVE_SUPPORT
PassiveAbility001FuzzTest(const char * data,size_t size)87 bool PassiveAbility001FuzzTest(const char* data, size_t size)
88 {
89     MessageParcel requestParcel;
90     requestParcel.WriteInterfaceToken(u"location.IPassiveAbility");
91     requestParcel.WriteBuffer(data, size);
92     requestParcel.RewindRead(0);
93 
94     MessageParcel reply;
95     MessageOption option;
96 
97     auto ability = sptr<PassiveAbility>(new (std::nothrow) PassiveAbility());
98     ability->OnRemoteRequest(static_cast<uint32_t>(PassiveInterfaceCode::SEND_LOCATION_REQUEST),
99         requestParcel, reply, option);
100     std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
101     return true;
102 }
103 
PassiveAbility002FuzzTest(const char * data,size_t size)104 bool PassiveAbility002FuzzTest(const char* data, size_t size)
105 {
106     MessageParcel requestParcel;
107     requestParcel.WriteInterfaceToken(u"location.IPassiveAbility");
108     requestParcel.WriteBuffer(data, size);
109     requestParcel.RewindRead(0);
110 
111     MessageParcel reply;
112     MessageOption option;
113 
114     auto ability = sptr<PassiveAbility>(new (std::nothrow) PassiveAbility());
115     ability->OnRemoteRequest(static_cast<uint32_t>(PassiveInterfaceCode::SET_ENABLE),
116         requestParcel, reply, option);
117     std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
118     return true;
119 }
120 
PassiveAbility003FuzzTest(const char * data,size_t size)121 bool PassiveAbility003FuzzTest(const char* data, size_t size)
122 {
123     MessageParcel requestParcel;
124     requestParcel.WriteInterfaceToken(u"location.IPassiveAbility");
125     requestParcel.WriteBuffer(data, size);
126     requestParcel.RewindRead(0);
127 
128     MessageParcel reply;
129     MessageOption option;
130 
131     auto ability = sptr<PassiveAbility>(new (std::nothrow) PassiveAbility());
132     ability->OnRemoteRequest(static_cast<uint32_t>(PassiveInterfaceCode::ENABLE_LOCATION_MOCK),
133         requestParcel, reply, option);
134     std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
135     return true;
136 }
137 
PassiveAbility004FuzzTest(const char * data,size_t size)138 bool PassiveAbility004FuzzTest(const char* data, size_t size)
139 {
140     MessageParcel requestParcel;
141     requestParcel.WriteInterfaceToken(u"location.IPassiveAbility");
142     requestParcel.WriteBuffer(data, size);
143     requestParcel.RewindRead(0);
144 
145     MessageParcel reply;
146     MessageOption option;
147 
148     auto ability = sptr<PassiveAbility>(new (std::nothrow) PassiveAbility());
149     ability->OnRemoteRequest(static_cast<uint32_t>(PassiveInterfaceCode::DISABLE_LOCATION_MOCK),
150         requestParcel, reply, option);
151     std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
152     return true;
153 }
154 
PassiveAbility005FuzzTest(const char * data,size_t size)155 bool PassiveAbility005FuzzTest(const char* data, size_t size)
156 {
157     MessageParcel requestParcel;
158     requestParcel.WriteInterfaceToken(u"location.IPassiveAbility");
159     requestParcel.WriteBuffer(data, size);
160     requestParcel.RewindRead(0);
161 
162     MessageParcel reply;
163     MessageOption option;
164 
165     auto ability = sptr<PassiveAbility>(new (std::nothrow) PassiveAbility());
166     ability->OnRemoteRequest(static_cast<uint32_t>(PassiveInterfaceCode::SET_MOCKED_LOCATIONS),
167         requestParcel, reply, option);
168     std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
169     return true;
170 }
171 #endif
172 } // namespace OHOS
173 
174 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)175 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
176 {
177     OHOS::MockNativePermission();
178     char* ch = OHOS::ParseData(data, size);
179     if (ch != nullptr) {
180 #ifdef FEATURE_PASSIVE_SUPPORT
181         OHOS::PassiveAbility001FuzzTest(ch, size);
182         OHOS::PassiveAbility002FuzzTest(ch, size);
183         OHOS::PassiveAbility003FuzzTest(ch, size);
184         OHOS::PassiveAbility004FuzzTest(ch, size);
185         OHOS::PassiveAbility005FuzzTest(ch, size);
186 #endif
187         free(ch);
188         ch = nullptr;
189     }
190     return 0;
191 }
192 
193