• 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 "bundlemgrhelper_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21 #define private public
22 #define protected public
23 #include "bundle_mgr_helper.h"
24 #include "ability_keep_alive_service.h"
25 #include "bundle_info.h"
26 #include "main_thread.h"
27 #include "connection_observer.h"
28 #undef protected
29 #undef private
30 #include "ability_fuzz_util.h"
31 #include "ability_record.h"
32 #include "app_mgr_client.h"
33 #include "continuous_task_callback_info.h"
34 #include "keep_alive_process_manager.h"
35 #include "parameters.h"
36 #include "permission_verification.h"
37 
38 
39 using namespace OHOS::AAFwk;
40 using namespace OHOS::AppExecFwk;
41 using namespace OHOS::AbilityRuntime;
42 
43 namespace OHOS {
44 namespace {
45 constexpr int INPUT_ZERO = 0;
46 constexpr int INPUT_ONE = 1;
47 constexpr int INPUT_THREE = 3;
48 constexpr size_t U32_AT_SIZE = 4;
49 constexpr uint8_t ENABLE = 2;
50 constexpr size_t OFFSET_ZERO = 24;
51 constexpr size_t OFFSET_ONE = 16;
52 constexpr size_t OFFSET_TWO = 8;
53 constexpr size_t STRING_MAX_LENGTH = 128;
54 }
55 
GetU32Data(const char * ptr)56 uint32_t GetU32Data(const char* ptr)
57 {
58     // convert fuzz input data to an integer
59     return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[ENABLE] << OFFSET_TWO) |
60         ptr[INPUT_THREE];
61 }
62 
GetFuzzAbilityToken()63 sptr<Token> GetFuzzAbilityToken()
64 {
65     sptr<Token> token = nullptr;
66     AbilityRequest abilityRequest;
67     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
68     abilityRequest.abilityInfo.name = "MainAbility";
69     abilityRequest.abilityInfo.type = AbilityType::DATA;
70     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
71     if (abilityRecord) {
72         token = abilityRecord->GetToken();
73     }
74     return token;
75 }
76 
BundleMgrHelperFuzztest1(bool boolParam,std::string & stringParam,int32_t int32Param)77 void BundleMgrHelperFuzztest1(bool boolParam, std::string &stringParam, int32_t int32Param)
78 {
79     std::shared_ptr<BundleMgrHelper> bmHelper = std::make_shared<BundleMgrHelper>(); // branch constructor
80     bmHelper->GetNameForUid(int32Param, stringParam); // branch
81     BundleInfo bundleInfo;
82     bmHelper->GetBundleInfo(stringParam, boolParam, bundleInfo, int32Param); // branch
83     bmHelper->InstallSandboxApp(stringParam, int32Param, int32Param, int32Param); // branch
84     bmHelper->UninstallSandboxApp(stringParam, int32Param, int32Param); // branch
85     bmHelper->GetUninstalledBundleInfo(stringParam, bundleInfo); // branch
86     bmHelper->GetSandboxBundleInfo(stringParam, int32Param, int32Param, bundleInfo); // branch
87     Want want;
88     AbilityInfo abilityInfo;
89     bmHelper->GetSandboxAbilityInfo(want, int32Param, int32Param, int32Param, abilityInfo);
90     std::vector<ExtensionAbilityInfo> extensionInfos;
91     bmHelper->GetSandboxExtAbilityInfos(want, int32Param, int32Param, int32Param, extensionInfos);
92     HapModuleInfo hapModuleInfo;
93     bmHelper->GetSandboxHapModuleInfo(abilityInfo, int32Param, int32Param, hapModuleInfo);
94     bmHelper->Connect();
95     bmHelper->ConnectBundleInstaller();
96     bmHelper->OnDeath();
97     bmHelper->GetBundleInfo(stringParam, int32Param, bundleInfo, int32Param);
98     bmHelper->GetHapModuleInfo(abilityInfo, hapModuleInfo);
99     bmHelper->GetAbilityLabel(stringParam, stringParam);
100     bmHelper->GetAppType(stringParam);
101     std::vector<BaseSharedBundleInfo> baseSharedBundleInfos;
102     bmHelper->GetBaseSharedBundleInfos(
103         stringParam, baseSharedBundleInfos, static_cast<GetDependentBundleInfoFlag>(int32Param));
104     bmHelper->GetBundleInfoForSelf(int32Param, bundleInfo);
105     bmHelper->GetDependentBundleInfo(stringParam, bundleInfo, static_cast<GetDependentBundleInfoFlag>(int32Param));
106     bmHelper->GetGroupDir(stringParam, stringParam);
107     bmHelper->GetOverlayManagerProxy();
108     bmHelper->QueryAbilityInfo(want, abilityInfo);
109     bmHelper->QueryAbilityInfo(want, int32Param, int32Param, abilityInfo);
110     std::vector<BundleInfo> bundleInfos;
111     bmHelper->GetBundleInfos(int32Param, bundleInfos, int32Param);
112     bmHelper->GetBundleInfos(static_cast<BundleFlag>(int32Param), bundleInfos, int32Param);
113     bmHelper->GetQuickFixManagerProxy();
114     bmHelper->ProcessPreload(want);
115     bmHelper->GetAppControlProxy();
116     bmHelper->QueryExtensionAbilityInfos(want, int32Param, int32Param, extensionInfos);
117     bmHelper->GetBundleInfoV9(stringParam, int32Param, bundleInfo, int32Param);
118 }
119 
BundleMgrHelperFuzztest2(bool boolParam,std::string & stringParam,int32_t int32Param)120 void BundleMgrHelperFuzztest2(bool boolParam, std::string &stringParam, int32_t int32Param)
121 {
122     std::shared_ptr<BundleMgrHelper> bmHelper = std::make_shared<BundleMgrHelper>(); // branch constructor
123     ApplicationInfo appInfo;
124     Want want;
125     bmHelper->GetApplicationInfo(stringParam, static_cast<ApplicationFlag>(int32Param), int32Param, appInfo);
126     bmHelper->GetApplicationInfo(stringParam, int32Param, int32Param, appInfo);
127     bmHelper->GetApplicationInfoWithAppIndex(stringParam, int32Param, int32Param, appInfo);
128     bmHelper->UnregisterBundleEventCallback(nullptr); // branch null
129     ExtensionAbilityInfo extensionAbilityInfo;
130     bmHelper->QueryExtensionAbilityInfoByUri(stringParam, int32Param, extensionAbilityInfo);
131     AbilityInfo abilityInfo;
132     bmHelper->ImplicitQueryInfoByPriority(want, int32Param, int32Param, abilityInfo, extensionAbilityInfo);
133     bmHelper->QueryAbilityInfoByUri(stringParam, int32Param, abilityInfo);
134     bmHelper->QueryAbilityInfo(want, int32Param, int32Param, abilityInfo, nullptr);
135     bmHelper->UpgradeAtomicService(want, int32Param);
136     std::vector<AbilityInfo> abilityInfos;
137     std::vector<ExtensionAbilityInfo> extensionInfos;
138     bmHelper->ImplicitQueryInfos(want, int32Param, int32Param, boolParam, abilityInfos, extensionInfos, boolParam);
139     bmHelper->CleanBundleDataFiles(stringParam, int32Param, int32Param, int32Param);
140     std::vector<DataGroupInfo> infos;
141     bmHelper->QueryDataGroupInfos(stringParam, int32Param, infos);
142     bmHelper->RegisterBundleEventCallback(nullptr);
143     HapModuleInfo hapModuleInfo;
144     bmHelper->GetHapModuleInfo(abilityInfo, int32Param, hapModuleInfo);
145     bmHelper->QueryAppGalleryBundleName(stringParam);
146     bmHelper->GetUidByBundleName(stringParam, int32Param, int32Param);
147     bmHelper->QueryExtensionAbilityInfosOnlyWithTypeName(stringParam, int32Param, int32Param, extensionInfos);
148     bmHelper->GetDefaultAppProxy();
149     bmHelper->GetJsonProfile(static_cast<ProfileType>(int32Param), stringParam, stringParam, stringParam, int32Param);
150     bmHelper->GetLaunchWantForBundle(stringParam, want, int32Param);
151     ElementName element;
152     bmHelper->QueryCloneAbilityInfo(element, int32Param, int32Param, abilityInfo, int32Param);
153     BundleInfo bundleInfo;
154     bmHelper->GetCloneBundleInfo(stringParam, int32Param, int32Param, bundleInfo, int32Param);
155     ExtensionAbilityInfo extensionInfo;
156     bmHelper->QueryCloneExtensionAbilityInfoWithAppIndex(element, int32Param, int32Param, extensionInfo, int32Param);
157 }
158 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)159 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
160 {
161     bool boolParam = *data % ENABLE;
162     std::string stringParam(data, size);
163     int32_t int32Param = static_cast<int32_t>(GetU32Data(data));
164     BundleMgrHelperFuzztest1(boolParam, stringParam, int32Param);
165     BundleMgrHelperFuzztest2(boolParam, stringParam, int32Param);
166     return true;
167 }
168 
169 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)170 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
171 {
172     std::shared_ptr<BundleMgrHelper> bmHelper = std::make_shared<BundleMgrHelper>();
173     std::string bundleName;
174     int32_t userId;
175     bool bmsReady;
176     std::string hostBundleName;
177     std::string pluginBundleName;
178     std::string pluginModuleName;
179     BundleInfo bundleInfo;
180     int32_t flags;
181     std::vector<int32_t> appIndexes;
182     SignatureInfo signatureInfo;
183     HapModuleInfo hapModuleInfo;
184     std::string moduleName;
185     int32_t resId;
186     int32_t appIndex;
187     sptr<IBundleEventCallback> pluginEventCallback;
188     std::vector<PluginBundleInfo> pluginBundleInfos;
189     FuzzedDataProvider fdp(data, size);
190     bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
191     hostBundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
192     pluginBundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
193     moduleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
194     pluginModuleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
195     userId = fdp.ConsumeIntegral<int32_t>();
196     flags = fdp.ConsumeIntegral<int32_t>();
197     resId = fdp.ConsumeIntegral<int32_t>();
198     appIndex = fdp.ConsumeIntegral<int32_t>();
199     bmsReady = fdp.ConsumeBool();
200     AbilityFuzzUtil::GetRandomBundleInfo(fdp, bundleInfo);
201     AbilityFuzzUtil::GenerateSignatureInfo(fdp, signatureInfo);
202     bmHelper->PreConnect();
203     bmHelper->GetAppIdByBundleName(bundleName, userId);
204     bmHelper->ConnectTillSuccess();
205     bmHelper->SetBmsReady(bmsReady);
206     bmHelper->GetPluginHapModuleInfo(hostBundleName, pluginBundleName, pluginModuleName, userId, hapModuleInfo);
207     bmHelper->GetBundleInfoForSelfWithOutCache (flags, bundleInfo);
208     bmHelper->GetCloneAppIndexes(bundleName, appIndexes, userId);
209     bmHelper->GetSignatureInfoByBundleName(bundleName, signatureInfo);
210     bmHelper->GetStringById(bundleName, moduleName, resId, userId);
211     bmHelper->GetDataDir(bundleName, appIndex);
212     bmHelper->GetPluginInfosForSelf(pluginBundleInfos);
213     bmHelper->RegisterPluginEventCallback(pluginEventCallback);
214     bmHelper->UnregisterPluginEventCallback(pluginEventCallback);
215     return true;
216 }
217 }
218 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)219 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
220 {
221     /* Run your code on data */
222     if (data == nullptr) {
223         return 0;
224     }
225 
226     /* Validate the length of size */
227     if (size < OHOS::U32_AT_SIZE) {
228         return 0;
229     }
230 
231     char* ch = static_cast<char*>(malloc(size + 1));
232     if (ch == nullptr) {
233         std::cout << "malloc failed." << std::endl;
234         return 0;
235     }
236 
237     (void)memset_s(ch, size + 1, 0x00, size + 1);
238     if (memcpy_s(ch, size, data, size) != EOK) {
239         std::cout << "copy failed." << std::endl;
240         free(ch);
241         ch = nullptr;
242         return 0;
243     }
244 
245     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
246     free(ch);
247     ch = nullptr;
248     return 0;
249 }
250 
251