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 "plugin_installer.h"
22
23 #include "bmsplugininstaller_fuzzer.h"
24 #include "bms_fuzztest_util.h"
25
26 using namespace OHOS::AppExecFwk;
27 using namespace OHOS::AppExecFwk::BMSFuzzTestUtil;
28 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)29 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
30 {
31 PluginInstaller installer;
32 InstallPluginParam installPluginParam;
33
34 FuzzedDataProvider fdp(data, size);
35 std::string hostBundleName;
36 std::vector<std::string> pluginFilePaths = GenerateStringArray(fdp);
37 std::string pluginBundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
38
39 installPluginParam.userId = Constants::UNSPECIFIED_USERID;
40 installer.InstallPlugin(hostBundleName, pluginFilePaths, installPluginParam);
41 installer.UninstallPlugin(hostBundleName, pluginBundleName, installPluginParam);
42 std::vector<std::string> pluginFilePaths2;
43 installer.ParseFiles(pluginFilePaths2, installPluginParam);
44
45 installPluginParam.userId = Constants::START_USERID;
46 installer.InstallPlugin(hostBundleName, pluginFilePaths, installPluginParam);
47 installer.UninstallPlugin(hostBundleName, pluginBundleName, installPluginParam);
48 installer.ParseFiles(pluginFilePaths2, installPluginParam);
49
50 std::string dir = "data/test";
51 installer.MkdirIfNotExist(dir);
52
53 dir = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
54 installer.MkdirIfNotExist(dir);
55
56 std::vector<std::string> inBundlePaths;
57 std::vector<std::string> parsedPaths;
58 installer.ParseHapPaths(installPluginParam, inBundlePaths, parsedPaths);
59 installer.ParseHapPaths(installPluginParam, pluginFilePaths, parsedPaths);
60 std::string signatureFilePath = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
61 installer.ObtainHspFileAndSignatureFilePath(inBundlePaths, parsedPaths, signatureFilePath);
62 installer.CopyHspToSecurityDir(inBundlePaths, installPluginParam);
63 std::vector<Security::Verify::HapVerifyResult> hapVerifyResults;
64 installer.DeliveryProfileToCodeSign(hapVerifyResults);
65
66 inBundlePaths.emplace_back(fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH));
67 installer.ObtainHspFileAndSignatureFilePath(inBundlePaths, parsedPaths, signatureFilePath);
68 installer.ObtainHspFileAndSignatureFilePath(pluginFilePaths, parsedPaths, signatureFilePath);
69
70 installer.CopyHspToSecurityDir(pluginFilePaths, installPluginParam);
71
72 std::string bundlePath = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
73 std::string moduleDir = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
74 std::string moduleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
75 std::string pluginBundleDir = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
76 InnerBundleInfo newInfo;
77 installer.ProcessNativeLibrary(bundlePath, moduleDir, moduleName, pluginBundleDir, newInfo);
78 moduleName = "entry";
79 InnerModuleInfo innerModuleInfo;
80 innerModuleInfo.moduleName = moduleName;
81 innerModuleInfo.compressNativeLibs = false;
82 innerModuleInfo.nativeLibraryPath = "x86";
83 newInfo.innerModuleInfos_[moduleName] = innerModuleInfo;
84 installer.ProcessNativeLibrary(bundlePath, moduleDir, moduleName, pluginBundleDir, newInfo);
85 std::string cpuAbi = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
86 bool isPreInstalledBundle = fdp.ConsumeBool();
87 installer.VerifyCodeSignatureForNativeFiles(bundlePath, cpuAbi,
88 bundlePath, signatureFilePath, isPreInstalledBundle);
89 std::string appIdentifier = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
90 bool isEnterpriseBundle = fdp.ConsumeBool();
91 bool isCompileSdkOpenHarmony = fdp.ConsumeBool();
92 installer.VerifyCodeSignatureForHsp(bundlePath, appIdentifier, isEnterpriseBundle, isCompileSdkOpenHarmony);
93
94 installer.CheckPluginId(hostBundleName);
95 installer.pluginIds_ = pluginFilePaths;
96 installer.CheckPluginId(hostBundleName);
97
98 std::string appServiceCapabilities;
99 std::vector<std::string> pluginIds;
100 installer.ParsePluginId(appServiceCapabilities, pluginIds);
101 appServiceCapabilities = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
102 installer.ParsePluginId(appServiceCapabilities, pluginIds);
103 installer.CheckSupportPluginPermission(hostBundleName);
104 installer.CheckPluginAppLabelInfo();
105 installer.ProcessPluginInstall(newInfo);
106 installer.parsedBundles_[hostBundleName] = InnerBundleInfo();
107 installer.ProcessPluginInstall(newInfo);
108 installer.CheckPluginAppLabelInfo();
109 installer.CreatePluginDir(hostBundleName, moduleDir);
110 installer.CheckAppIdentifier();
111 installer.CheckVersionCodeForUpdate();
112 installer.ExtractPluginBundles(bundlePath, newInfo, moduleDir);
113 installer.MergePluginBundleInfo(newInfo);
114 InnerBundleInfo hostBundleInfo;
115 installer.SavePluginInfoToStorage(newInfo, hostBundleInfo);
116 installer.PluginRollBack(hostBundleName);
117 installer.RemovePluginDir(hostBundleInfo);
118 installer.SaveHspToInstallDir(bundlePath, moduleDir, moduleName, newInfo);
119 installer.RemoveEmptyDirs(moduleDir);
120 installer.RemoveDir(moduleDir);
121 installer.ProcessPluginUninstall(hostBundleInfo);
122 installer.RemoveOldInstallDir();
123 installer.isPluginExist_ = true;
124 installer.RemoveOldInstallDir();
125 installer.UninstallRollBack(hostBundleName);
126 installer.GetModuleNames();
127 NotifyType type = static_cast<NotifyType>(fdp.ConsumeIntegralInRange<uint8_t>(0, ORIENTATION_MAX));
128 int32_t uid = fdp.ConsumeIntegral<int32_t>();
129 installer.NotifyPluginEvents(type, uid);
130
131 return true;
132 }
133 }
134
135 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)136 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
137 {
138 /* Run your code on data */
139 OHOS::DoSomethingInterestingWithMyAPI(data, size);
140 return 0;
141 }
142