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 "attachabilitythread_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "ability_manager_client.h"
22 #include "ability_connect_manager.h"
23 #include "ability_record.h"
24 #include "ability_scheduler_stub.h"
25 #include "data_ability_manager.h"
26
27 using namespace OHOS::AAFwk;
28 using namespace OHOS::AppExecFwk;
29
30 namespace OHOS {
31 namespace {
32 constexpr size_t FOO_MAX_LEN = 1024;
33 constexpr size_t U32_AT_SIZE = 4;
34 constexpr int32_t UID_TEST = 100;
35 constexpr int OFFSET_ZERO = 24;
36 }
37 class AbilitySchedulerFuzzTest : public AbilitySchedulerStub {
38 public:
39 AbilitySchedulerFuzzTest() = default;
~AbilitySchedulerFuzzTest()40 virtual ~AbilitySchedulerFuzzTest()
41 {};
ScheduleAbilityTransaction(const Want & want,const LifeCycleStateInfo & targetState,sptr<SessionInfo> sessionInfo=nullptr)42 bool ScheduleAbilityTransaction(const Want& want, const LifeCycleStateInfo& targetState,
43 sptr<SessionInfo> sessionInfo = nullptr) override
44 {
45 return true;
46 }
ScheduleShareData(const int32_t & uniqueId)47 void ScheduleShareData(const int32_t &uniqueId) override
48 {}
SendResult(int requestCode,int resultCode,const Want & resultWant)49 void SendResult(int requestCode, int resultCode, const Want& resultWant) override
50 {}
ScheduleConnectAbility(const Want & want)51 void ScheduleConnectAbility(const Want& want) override
52 {}
ScheduleDisconnectAbility(const Want & want)53 void ScheduleDisconnectAbility(const Want& want) override
54 {}
ScheduleCommandAbility(const Want & want,bool restart,int startId)55 void ScheduleCommandAbility(const Want& want, bool restart, int startId) override
56 {}
ScheduleCommandAbilityWindow(const Want & want,const sptr<SessionInfo> & sessionInfo,WindowCommand winCmd)57 void ScheduleCommandAbilityWindow(const Want &want, const sptr<SessionInfo> &sessionInfo,
58 WindowCommand winCmd) override
59 {}
SchedulePrepareTerminateAbility()60 bool SchedulePrepareTerminateAbility() override
61 {
62 return false;
63 }
ScheduleSaveAbilityState()64 void ScheduleSaveAbilityState() override
65 {}
ScheduleRestoreAbilityState(const PacMap & inState)66 void ScheduleRestoreAbilityState(const PacMap& inState) override
67 {}
GetFileTypes(const Uri & uri,const std::string & mimeTypeFilter)68 std::vector<std::string> GetFileTypes(const Uri& uri, const std::string& mimeTypeFilter) override
69 {
70 return {};
71 }
OpenFile(const Uri & uri,const std::string & mode)72 int OpenFile(const Uri& uri, const std::string& mode) override
73 {
74 return 0;
75 }
OpenRawFile(const Uri & uri,const std::string & mode)76 int OpenRawFile(const Uri& uri, const std::string& mode) override
77 {
78 return 0;
79 }
Insert(const Uri & uri,const NativeRdb::ValuesBucket & value)80 int Insert(const Uri& uri, const NativeRdb::ValuesBucket& value) override
81 {
82 return 0;
83 }
Update(const Uri & uri,const NativeRdb::ValuesBucket & value,const NativeRdb::DataAbilityPredicates & predicates)84 int Update(const Uri& uri, const NativeRdb::ValuesBucket& value,
85 const NativeRdb::DataAbilityPredicates& predicates) override
86 {
87 return 0;
88 }
Delete(const Uri & uri,const NativeRdb::DataAbilityPredicates & predicates)89 int Delete(const Uri& uri, const NativeRdb::DataAbilityPredicates& predicates) override
90 {
91 return 0;
92 }
Call(const Uri & uri,const std::string & method,const std::string & arg,const AppExecFwk::PacMap & pacMap)93 std::shared_ptr<AppExecFwk::PacMap> Call(
94 const Uri& uri, const std::string& method, const std::string& arg, const AppExecFwk::PacMap& pacMap) override
95 {
96 return {};
97 }
Query(const Uri & uri,std::vector<std::string> & columns,const NativeRdb::DataAbilityPredicates & predicates)98 std::shared_ptr<NativeRdb::AbsSharedResultSet> Query(const Uri& uri,
99 std::vector<std::string>& columns, const NativeRdb::DataAbilityPredicates& predicates) override
100 {
101 return {};
102 }
GetType(const Uri & uri)103 std::string GetType(const Uri& uri) override
104 {
105 return {};
106 }
Reload(const Uri & uri,const PacMap & extras)107 bool Reload(const Uri& uri, const PacMap& extras) override
108 {
109 return true;
110 }
BatchInsert(const Uri & uri,const std::vector<NativeRdb::ValuesBucket> & values)111 int BatchInsert(const Uri& uri, const std::vector<NativeRdb::ValuesBucket>& values) override
112 {
113 return 0;
114 }
ScheduleRegisterObserver(const Uri & uri,const sptr<IDataAbilityObserver> & dataObserver)115 bool ScheduleRegisterObserver(const Uri& uri, const sptr<IDataAbilityObserver>& dataObserver) override
116 {
117 return true;
118 }
ScheduleUnregisterObserver(const Uri & uri,const sptr<IDataAbilityObserver> & dataObserver)119 bool ScheduleUnregisterObserver(const Uri& uri, const sptr<IDataAbilityObserver>& dataObserver) override
120 {
121 return true;
122 }
ScheduleNotifyChange(const Uri & uri)123 bool ScheduleNotifyChange(const Uri& uri) override
124 {
125 return true;
126 }
NormalizeUri(const Uri & uri)127 Uri NormalizeUri(const Uri& uri) override
128 {
129 return Uri{ "abilityschedulerstub" };
130 }
131
DenormalizeUri(const Uri & uri)132 Uri DenormalizeUri(const Uri& uri) override
133 {
134 return Uri{ "abilityschedulerstub" };
135 }
ExecuteBatch(const std::vector<std::shared_ptr<AppExecFwk::DataAbilityOperation>> & operations)136 std::vector<std::shared_ptr<AppExecFwk::DataAbilityResult>> ExecuteBatch(
137 const std::vector<std::shared_ptr<AppExecFwk::DataAbilityOperation>>& operations) override
138 {
139 return {};
140 }
ContinueAbility(const std::string & deviceId,uint32_t versionCode)141 void ContinueAbility(const std::string& deviceId, uint32_t versionCode) override
142 {}
NotifyContinuationResult(int32_t result)143 void NotifyContinuationResult(int32_t result) override
144 {}
DumpAbilityInfo(const std::vector<std::string> & params,std::vector<std::string> & info)145 void DumpAbilityInfo(const std::vector<std::string>& params, std::vector<std::string>& info) override
146 {}
UpdateSessionToken(sptr<IRemoteObject> sessionToken)147 void UpdateSessionToken(sptr<IRemoteObject> sessionToken) override
148 {}
OnExecuteIntent(const Want & want)149 void OnExecuteIntent(const Want &want) override
150 {}
CreateModalUIExtension(const Want & want)151 int CreateModalUIExtension(const Want &want) override
152 {
153 return 0;
154 }
CallRequest()155 void CallRequest() override
156 {
157 return;
158 }
159 };
GetFuzzAbilityToken(AbilityType type)160 sptr<Token> GetFuzzAbilityToken(AbilityType type)
161 {
162 sptr<Token> token = nullptr;
163
164 AbilityRequest abilityRequest;
165 abilityRequest.uid = UID_TEST;
166 abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
167 abilityRequest.abilityInfo.name = "MainAbility";
168 abilityRequest.abilityInfo.type = type;
169 std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
170 if (abilityRecord) {
171 token = abilityRecord->GetToken();
172 }
173
174 return token;
175 }
GetU32Data(const char * ptr)176 uint32_t GetU32Data(const char* ptr)
177 {
178 // convert fuzz input data to an integer
179 return (ptr[0] << OFFSET_ZERO) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
180 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)181 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
182 {
183 auto abilitymgr = AbilityManagerClient::GetInstance();
184 int userId = static_cast<int>(GetU32Data(data));
185 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(userId);
186 if (!connectManager) {
187 return false;
188 }
189 std::shared_ptr<DataAbilityManager> dataManager = std::make_shared<DataAbilityManager>();
190 if (!dataManager) {
191 return false;
192 }
193 sptr<IAbilityScheduler> scheduler = new AbilitySchedulerFuzzTest();
194 if (!abilitymgr) {
195 return false;
196 }
197
198 // get token
199 sptr<IRemoteObject> token = GetFuzzAbilityToken(AbilityType::PAGE);
200 if (!token) {
201 std::cout << "Get ability token failed." << std::endl;
202 return false;
203 }
204
205 // get serviceToken
206 sptr<IRemoteObject> serviceToken = GetFuzzAbilityToken(AbilityType::SERVICE);
207 if (!serviceToken) {
208 std::cout << "Get service ability token failed." << std::endl;
209 return false;
210 }
211
212 // get dataToken
213 sptr<IRemoteObject> dataToken = GetFuzzAbilityToken(AbilityType::DATA);
214 if (!dataToken) {
215 std::cout << "Get data ability token failed." << std::endl;
216 return false;
217 }
218
219 if (connectManager) {
220 connectManager->AttachAbilityThreadLocked(scheduler, serviceToken);
221 }
222
223 if (dataManager) {
224 dataManager->AttachAbilityThread(scheduler, dataToken);
225 }
226
227 if (abilitymgr->AttachAbilityThread(scheduler, token) != 0) {
228 return false;
229 }
230
231 return true;
232 }
233 }
234
235 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)236 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
237 {
238 /* Run your code on data */
239 if (data == nullptr) {
240 std::cout << "invalid data" << std::endl;
241 return 0;
242 }
243
244 /* Validate the length of size */
245 if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
246 return 0;
247 }
248
249 char* ch = static_cast<char*>(malloc(size + 1));
250 if (ch == nullptr) {
251 std::cout << "malloc failed." << std::endl;
252 return 0;
253 }
254
255 (void)memset_s(ch, size + 1, 0x00, size + 1);
256 if (memcpy_s(ch, size, data, size) != EOK) {
257 std::cout << "copy failed." << std::endl;
258 free(ch);
259 ch = nullptr;
260 return 0;
261 }
262
263 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
264 free(ch);
265 ch = nullptr;
266 return 0;
267 }
268
269