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