• 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 "verifycallingpermission_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <iostream>
21 
22 #include "bundle_mgr_interface.h"
23 #include "if_system_ability_manager.h"
24 #include "iservice_registry.h"
25 #include "system_ability_definition.h"
26 
27 using namespace OHOS::AppExecFwk;
28 
29 namespace OHOS {
GetBundleMgr()30     sptr<OHOS::AppExecFwk::IBundleMgr> GetBundleMgr()
31     {
32         auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
33         if (systemAbilityManager == nullptr) {
34             std::cout << "GetBundleMgr GetSystemAbilityManager is null";
35             return nullptr;
36         }
37 
38         auto bundleMgrSa = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
39         if (bundleMgrSa == nullptr) {
40             std::cout << "[fuzz] GetBundleMgr GetSystemAbility is null";
41             return nullptr;
42         }
43 
44         return iface_cast<IBundleMgr>(bundleMgrSa);
45     }
46 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)47     bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
48     {
49         if (size != 1) {
50             return false;
51         }
52 
53         auto bundleMgr = GetBundleMgr();
54         if (bundleMgr == nullptr) {
55             return false;
56         }
57 
58         std::string info (reinterpret_cast<const char*>(data), size);
59         bundleMgr->VerifyCallingPermission(info);
60         return true;
61     }
62 }
63 
64 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)65 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
66 {
67     /* Run your code on data */
68     OHOS::DoSomethingInterestingWithMyAPI(data, size);
69     return 0;
70 }
71