• 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 };
GetFuzzAbilityToken(AbilityType type)161 sptr<Token> GetFuzzAbilityToken(AbilityType type)
162 {
163     sptr<Token> token = nullptr;
164 
165     AbilityRequest abilityRequest;
166     abilityRequest.uid = UID_TEST;
167     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
168     abilityRequest.abilityInfo.name = "MainAbility";
169     abilityRequest.abilityInfo.type = type;
170     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
171     if (abilityRecord) {
172         token = abilityRecord->GetToken();
173     }
174 
175     return token;
176 }
GetU32Data(const char * ptr)177 uint32_t GetU32Data(const char* ptr)
178 {
179     // convert fuzz input data to an integer
180     return (ptr[0] << OFFSET_ZERO) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
181 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)182 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
183 {
184     auto abilitymgr = AbilityManagerClient::GetInstance();
185     int userId = static_cast<int>(GetU32Data(data));
186     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(userId);
187     if (!connectManager) {
188         return false;
189     }
190     std::shared_ptr<DataAbilityManager> dataManager = std::make_shared<DataAbilityManager>();
191     if (!dataManager) {
192         return false;
193     }
194     sptr<IAbilityScheduler> scheduler = new AbilitySchedulerFuzzTest();
195     if (!abilitymgr) {
196         return false;
197     }
198 
199     // get token
200     sptr<IRemoteObject> token = GetFuzzAbilityToken(AbilityType::PAGE);
201     if (!token) {
202         std::cout << "Get ability token failed." << std::endl;
203         return false;
204     }
205 
206     // get serviceToken
207     sptr<IRemoteObject> serviceToken = GetFuzzAbilityToken(AbilityType::SERVICE);
208     if (!serviceToken) {
209         std::cout << "Get service ability token failed." << std::endl;
210         return false;
211     }
212 
213     // get dataToken
214     sptr<IRemoteObject> dataToken = GetFuzzAbilityToken(AbilityType::DATA);
215     if (!dataToken) {
216         std::cout << "Get data ability token failed." << std::endl;
217         return false;
218     }
219 
220     if (connectManager) {
221         connectManager->AttachAbilityThreadLocked(scheduler, serviceToken);
222     }
223 
224     if (dataManager) {
225         dataManager->AttachAbilityThread(scheduler, dataToken);
226     }
227 
228     if (abilitymgr->AttachAbilityThread(scheduler, token) != 0) {
229         return false;
230     }
231 
232     return true;
233 }
234 }
235 
236 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)237 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
238 {
239     /* Run your code on data */
240     if (data == nullptr) {
241         std::cout << "invalid data" << std::endl;
242         return 0;
243     }
244 
245     /* Validate the length of size */
246     if (size < OHOS::U32_AT_SIZE) {
247         return 0;
248     }
249 
250     char* ch = static_cast<char*>(malloc(size + 1));
251     if (ch == nullptr) {
252         std::cout << "malloc failed." << std::endl;
253         return 0;
254     }
255 
256     (void)memset_s(ch, size + 1, 0x00, size + 1);
257     if (memcpy_s(ch, size, data, size) != EOK) {
258         std::cout << "copy failed." << std::endl;
259         free(ch);
260         ch = nullptr;
261         return 0;
262     }
263 
264     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
265     free(ch);
266     ch = nullptr;
267     return 0;
268 }
269 
270