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