• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #define private public
17 #include <cstddef>
18 #include <cstdint>
19 #include <fuzzer/FuzzedDataProvider.h>
20 
21 #include "app_service_fwk_installer.h"
22 
23 #include "bmsappservicefwkinstaller_fuzzer.h"
24 #include "bms_fuzztest_util.h"
25 #include "bundle_mgr_service.h"
26 #include "securec.h"
27 
28 using namespace OHOS::AppExecFwk;
29 using namespace OHOS::AppExecFwk::BMSFuzzTestUtil;
30 namespace OHOS {
31 namespace AppExecFwk {
32     std::string ObtainTempSoPath(const std::string &moduleName, const std::string &nativeLibPath);
33 }
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)34 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
35 {
36     AppServiceFwkInstaller appServiceFwkInstaller;
37     appServiceFwkInstaller.MarkInstallFinish();
38 
39     FuzzedDataProvider fdp(data, size);
40     std::vector<std::string> hspPaths = GenerateStringArray(fdp);
41     hspPaths.emplace_back("/system/app/appServiceFwk/test");
42     InstallParam installParam;
43     GenerateInstallParam(fdp, installParam);
44     appServiceFwkInstaller.BeforeInstall(hspPaths, installParam);
45     appServiceFwkInstaller.Install(hspPaths, installParam);
46 
47     std::string moduleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
48     std::string nativeLibPath;
49     ObtainTempSoPath(moduleName, nativeLibPath);
50 
51     nativeLibPath = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
52     ObtainTempSoPath(moduleName, nativeLibPath);
53 
54     std::string bundleName;
55     bool isKeepData = fdp.ConsumeBool();
56     appServiceFwkInstaller.UnInstall(bundleName, isKeepData);
57     appServiceFwkInstaller.UnInstall(bundleName, moduleName);
58 
59     bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
60 
61     InnerBundleInfo emptyInfo;
62     appServiceFwkInstaller.CheckNeedUninstallBundle(moduleName, emptyInfo);
63 
64     InnerBundleInfo innerBundleInfo;
65     InnerModuleInfo innerModuleInfo2;
66     innerModuleInfo2.moduleName = "entry";
67     innerModuleInfo2.distro.moduleType = "shared";
68     innerBundleInfo.innerModuleInfos_["entry"] = innerModuleInfo2;
69     appServiceFwkInstaller.CheckNeedUninstallBundle("entry", innerBundleInfo);
70     appServiceFwkInstaller.CheckNeedUninstallBundle(moduleName, innerBundleInfo);
71     appServiceFwkInstaller.RemoveModuleDataDir(bundleName, moduleName, innerBundleInfo);
72     appServiceFwkInstaller.ProcessInstall(hspPaths, installParam);
73 
74     std::unordered_map<std::string, InnerBundleInfo> infos;
75     appServiceFwkInstaller.CheckNeedInstall(infos, emptyInfo, isKeepData);
76     std::vector<Security::Verify::HapVerifyResult> hapVerifyRes;
77     hapVerifyRes.emplace_back(Security::Verify::HapVerifyResult());
78     infos["test"] = InnerBundleInfo();
79     appServiceFwkInstaller.GenerateOdid(infos, hapVerifyRes);
80     appServiceFwkInstaller.CheckAppLabelInfo(infos);
81     appServiceFwkInstaller.InnerProcessInstall(infos, installParam);
82     appServiceFwkInstaller.CheckNeedInstall(infos, emptyInfo, isKeepData);
83     std::string realHspPath = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
84     std::string realSoPath = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
85     appServiceFwkInstaller.VerifyCodeSignatureForHsp(realHspPath, realSoPath);
86     std::string dir = "data/test";
87     appServiceFwkInstaller.MkdirIfNotExist(dir);
88     dir = "data/test/test";
89     appServiceFwkInstaller.MkdirIfNotExist(dir);
90 
91     std::string bundlePath = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
92     std::string moduleDir = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
93     std::string versionDir = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
94     InnerBundleInfo newInfo;
95     bool copyHapToInstallPath = fdp.ConsumeBool();
96     appServiceFwkInstaller.ProcessNativeLibrary(
97         bundlePath, moduleDir, moduleName, versionDir, newInfo, copyHapToInstallPath);
98     moduleName = "entry";
99     InnerModuleInfo innerModuleInfo;
100     innerModuleInfo.moduleName = moduleName;
101     innerModuleInfo.compressNativeLibs = false;
102     innerModuleInfo.nativeLibraryPath = "x86";
103     newInfo.innerModuleInfos_[moduleName] = innerModuleInfo;
104     appServiceFwkInstaller.ProcessNativeLibrary(
105         bundlePath, moduleDir, moduleName, versionDir, newInfo, copyHapToInstallPath);
106 
107     newInfo.baseApplicationInfo_->bundleName = bundleName;
108     appServiceFwkInstaller.newInnerBundleInfo_ = newInfo;
109     appServiceFwkInstaller.MergeBundleInfos(newInfo);
110 
111     appServiceFwkInstaller.newInnerBundleInfo_.baseBundleInfo_->isPreInstallApp = true;
112     appServiceFwkInstaller.RollBack();
113     appServiceFwkInstaller.ProcessNewModuleInstall(newInfo, innerBundleInfo, bundlePath, installParam);
114     bool isAppExist = false;
115     appServiceFwkInstaller.GetInnerBundleInfoWithDisable(emptyInfo, isAppExist);
116 
117     uint32_t versionCode = fdp.ConsumeIntegral<uint32_t>();
118     appServiceFwkInstaller.versionUpgrade_ = true;
119     appServiceFwkInstaller.RemoveLowerVersionSoDir(versionCode);
120     appServiceFwkInstaller.MarkInstallFinish();
121     appServiceFwkInstaller.UnInstall(bundleName, isKeepData);
122     appServiceFwkInstaller.UnInstall(bundleName, moduleName);
123 
124     appServiceFwkInstaller.ResetProperties();
125     return true;
126 }
127 }
128 
129 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)130 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
131 {
132     /* Run your code on data */
133     OHOS::DoSomethingInterestingWithMyAPI(data, size);
134     return 0;
135 }
136