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 "bundlemgrhelper_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #define private public
22 #define protected public
23 #include "bundle_mgr_helper.h"
24 #undef protected
25 #undef private
26
27 #include "ability_record.h"
28
29 using namespace OHOS::AAFwk;
30 using namespace OHOS::AppExecFwk;
31 using namespace OHOS::AbilityRuntime;
32
33 namespace OHOS {
34 namespace {
35 constexpr int INPUT_ZERO = 0;
36 constexpr int INPUT_ONE = 1;
37 constexpr int INPUT_THREE = 3;
38 constexpr size_t FOO_MAX_LEN = 1024;
39 constexpr size_t U32_AT_SIZE = 4;
40 constexpr uint8_t ENABLE = 2;
41 constexpr size_t OFFSET_ZERO = 24;
42 constexpr size_t OFFSET_ONE = 16;
43 constexpr size_t OFFSET_TWO = 8;
44 }
45
GetU32Data(const char * ptr)46 uint32_t GetU32Data(const char* ptr)
47 {
48 // convert fuzz input data to an integer
49 return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[ENABLE] << OFFSET_TWO) |
50 ptr[INPUT_THREE];
51 }
52
GetFuzzAbilityToken()53 sptr<Token> GetFuzzAbilityToken()
54 {
55 sptr<Token> token = nullptr;
56 AbilityRequest abilityRequest;
57 abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
58 abilityRequest.abilityInfo.name = "MainAbility";
59 abilityRequest.abilityInfo.type = AbilityType::DATA;
60 std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
61 if (abilityRecord) {
62 token = abilityRecord->GetToken();
63 }
64 return token;
65 }
66
BundleMgrHelperFuzztest1(bool boolParam,std::string & stringParam,int32_t int32Param)67 void BundleMgrHelperFuzztest1(bool boolParam, std::string &stringParam, int32_t int32Param)
68 {
69 std::shared_ptr<BundleMgrHelper> bmHelper = std::make_shared<BundleMgrHelper>(); // branch constructor
70 bmHelper->GetNameForUid(int32Param, stringParam); // branch
71 BundleInfo bundleInfo;
72 bmHelper->GetBundleInfo(stringParam, boolParam, bundleInfo, int32Param); // branch
73 bmHelper->InstallSandboxApp(stringParam, int32Param, int32Param, int32Param); // branch
74 bmHelper->UninstallSandboxApp(stringParam, int32Param, int32Param); // branch
75 bmHelper->GetUninstalledBundleInfo(stringParam, bundleInfo); // branch
76 bmHelper->GetSandboxBundleInfo(stringParam, int32Param, int32Param, bundleInfo); // branch
77 Want want;
78 AbilityInfo abilityInfo;
79 bmHelper->GetSandboxAbilityInfo(want, int32Param, int32Param, int32Param, abilityInfo);
80 std::vector<ExtensionAbilityInfo> extensionInfos;
81 bmHelper->GetSandboxExtAbilityInfos(want, int32Param, int32Param, int32Param, extensionInfos);
82 HapModuleInfo hapModuleInfo;
83 bmHelper->GetSandboxHapModuleInfo(abilityInfo, int32Param, int32Param, hapModuleInfo);
84 bmHelper->Connect();
85 bmHelper->ConnectBundleInstaller();
86 bmHelper->OnDeath();
87 bmHelper->GetBundleInfo(stringParam, int32Param, bundleInfo, int32Param);
88 bmHelper->GetHapModuleInfo(abilityInfo, hapModuleInfo);
89 bmHelper->GetAbilityLabel(stringParam, stringParam);
90 bmHelper->GetAppType(stringParam);
91 std::vector<BaseSharedBundleInfo> baseSharedBundleInfos;
92 bmHelper->GetBaseSharedBundleInfos(
93 stringParam, baseSharedBundleInfos, static_cast<GetDependentBundleInfoFlag>(int32Param));
94 bmHelper->GetBundleInfoForSelf(int32Param, bundleInfo);
95 bmHelper->GetDependentBundleInfo(stringParam, bundleInfo, static_cast<GetDependentBundleInfoFlag>(int32Param));
96 bmHelper->GetGroupDir(stringParam, stringParam);
97 bmHelper->GetOverlayManagerProxy();
98 bmHelper->QueryAbilityInfo(want, abilityInfo);
99 bmHelper->QueryAbilityInfo(want, int32Param, int32Param, abilityInfo);
100 std::vector<BundleInfo> bundleInfos;
101 bmHelper->GetBundleInfos(int32Param, bundleInfos, int32Param);
102 bmHelper->GetBundleInfos(static_cast<BundleFlag>(int32Param), bundleInfos, int32Param);
103 bmHelper->GetQuickFixManagerProxy();
104 bmHelper->ProcessPreload(want);
105 bmHelper->GetAppControlProxy();
106 bmHelper->QueryExtensionAbilityInfos(want, int32Param, int32Param, extensionInfos);
107 bmHelper->GetBundleInfoV9(stringParam, int32Param, bundleInfo, int32Param);
108 }
109
BundleMgrHelperFuzztest2(bool boolParam,std::string & stringParam,int32_t int32Param)110 void BundleMgrHelperFuzztest2(bool boolParam, std::string &stringParam, int32_t int32Param)
111 {
112 std::shared_ptr<BundleMgrHelper> bmHelper = std::make_shared<BundleMgrHelper>(); // branch constructor
113 ApplicationInfo appInfo;
114 Want want;
115 bmHelper->GetApplicationInfo(stringParam, static_cast<ApplicationFlag>(int32Param), int32Param, appInfo);
116 bmHelper->GetApplicationInfo(stringParam, int32Param, int32Param, appInfo);
117 bmHelper->GetApplicationInfoWithAppIndex(stringParam, int32Param, int32Param, appInfo);
118 bmHelper->UnregisterBundleEventCallback(nullptr); // branch null
119 ExtensionAbilityInfo extensionAbilityInfo;
120 bmHelper->QueryExtensionAbilityInfoByUri(stringParam, int32Param, extensionAbilityInfo);
121 AbilityInfo abilityInfo;
122 bmHelper->ImplicitQueryInfoByPriority(want, int32Param, int32Param, abilityInfo, extensionAbilityInfo);
123 bmHelper->QueryAbilityInfoByUri(stringParam, int32Param, abilityInfo);
124 bmHelper->QueryAbilityInfo(want, int32Param, int32Param, abilityInfo, nullptr);
125 bmHelper->UpgradeAtomicService(want, int32Param);
126 std::vector<AbilityInfo> abilityInfos;
127 std::vector<ExtensionAbilityInfo> extensionInfos;
128 bmHelper->ImplicitQueryInfos(want, int32Param, int32Param, boolParam, abilityInfos, extensionInfos);
129 bmHelper->CleanBundleDataFiles(stringParam, int32Param, int32Param);
130 std::vector<DataGroupInfo> infos;
131 bmHelper->QueryDataGroupInfos(stringParam, int32Param, infos);
132 bmHelper->RegisterBundleEventCallback(nullptr);
133 HapModuleInfo hapModuleInfo;
134 bmHelper->GetHapModuleInfo(abilityInfo, int32Param, hapModuleInfo);
135 bmHelper->QueryAppGalleryBundleName(stringParam);
136 bmHelper->GetUidByBundleName(stringParam, int32Param, int32Param);
137 bmHelper->QueryExtensionAbilityInfosOnlyWithTypeName(stringParam, int32Param, int32Param, extensionInfos);
138 bmHelper->GetDefaultAppProxy();
139 bmHelper->GetJsonProfile(static_cast<ProfileType>(int32Param), stringParam, stringParam, stringParam, int32Param);
140 bmHelper->GetLaunchWantForBundle(stringParam, want, int32Param);
141 ElementName element;
142 bmHelper->QueryCloneAbilityInfo(element, int32Param, int32Param, abilityInfo, int32Param);
143 BundleInfo bundleInfo;
144 bmHelper->GetCloneBundleInfo(stringParam, int32Param, int32Param, bundleInfo, int32Param);
145 ExtensionAbilityInfo extensionInfo;
146 bmHelper->QueryCloneExtensionAbilityInfoWithAppIndex(element, int32Param, int32Param, extensionInfo, int32Param);
147 }
148
DoSomethingInterestingWithMyAPI(const char * data,size_t size)149 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
150 {
151 bool boolParam = *data % ENABLE;
152 std::string stringParam(data, size);
153 int32_t int32Param = static_cast<int32_t>(GetU32Data(data));
154 BundleMgrHelperFuzztest1(boolParam, stringParam, int32Param);
155 BundleMgrHelperFuzztest2(boolParam, stringParam, int32Param);
156 return true;
157 }
158 }
159
160 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)161 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
162 {
163 /* Run your code on data */
164 if (data == nullptr) {
165 return 0;
166 }
167
168 /* Validate the length of size */
169 if (size < OHOS::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) {
170 return 0;
171 }
172
173 char* ch = static_cast<char*>(malloc(size + 1));
174 if (ch == nullptr) {
175 std::cout << "malloc failed." << std::endl;
176 return 0;
177 }
178
179 (void)memset_s(ch, size + 1, 0x00, size + 1);
180 if (memcpy_s(ch, size, data, size) != EOK) {
181 std::cout << "copy failed." << std::endl;
182 free(ch);
183 ch = nullptr;
184 return 0;
185 }
186
187 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
188 free(ch);
189 ch = nullptr;
190 return 0;
191 }
192
193