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 "driver_installer.h"
22
23 #include "bmsdriverinstaller_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 DriverInstaller installer;
32 FuzzedDataProvider fdp(data, size);
33
34 std::unordered_map<std::string, InnerBundleInfo> newInfos;
35 InnerBundleInfo newInfo1;
36 newInfo1.baseApplicationInfo_->bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
37 newInfo1.SetCurrentModulePackage(fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH));
38 newInfos.emplace(newInfo1.GetBundleName(), newInfo1);
39 InnerBundleInfo newInfo2;
40 newInfo2.baseApplicationInfo_->bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
41 newInfo2.SetCurrentModulePackage(fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH));
42 newInfos.emplace(newInfo2.GetBundleName(), newInfo2);
43 InnerBundleInfo oldInfo1;
44 oldInfo1.baseApplicationInfo_->bundleName = newInfo1.GetBundleName();
45 oldInfo1.SetCurrentModulePackage(newInfo1.GetCurrentModulePackage());
46 installer.CopyAllDriverFile(newInfos, oldInfo1);
47 installer.RemoveAndReNameDriverFile(newInfos, oldInfo1);
48 InnerBundleInfo oldInfo2;
49 oldInfo2.baseApplicationInfo_->bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
50 oldInfo2.SetCurrentModulePackage(fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH));
51 installer.CopyAllDriverFile(newInfos, oldInfo2);
52 installer.RemoveAndReNameDriverFile(newInfos, oldInfo2);
53
54 Metadata metaData;
55 metaData.name = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
56 metaData.value = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
57 metaData.resource = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
58 std::unordered_multimap<std::string, std::string> dirMap;
59 bool isModuleExisted = fdp.ConsumeBool();
60 installer.FilterDriverSoFile(newInfo1, metaData, dirMap, isModuleExisted);
61 metaData.name = "cupsFilter";
62 installer.FilterDriverSoFile(newInfo1, metaData, dirMap, isModuleExisted);
63 metaData.value = "../invalid_path";
64 installer.FilterDriverSoFile(newInfo1, metaData, dirMap, isModuleExisted);
65 metaData.resource = "../invalid_path";
66 installer.FilterDriverSoFile(newInfo1, metaData, dirMap, isModuleExisted);
67 metaData.value = "valid_path";
68 metaData.resource = "valid_path";
69 installer.FilterDriverSoFile(newInfo1, metaData, dirMap, isModuleExisted);
70
71 installer.RemoveDriverSoFile(newInfo1, newInfo1.GetCurrentModulePackage(), isModuleExisted);
72 InnerExtensionInfo innerExtensionInfo;
73 innerExtensionInfo.moduleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
74 innerExtensionInfo.bundleName = newInfo1.GetBundleName();
75 std::string extensionKey = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
76 newInfo1.InsertExtensionInfo(extensionKey, innerExtensionInfo);
77 installer.RemoveDriverSoFile(newInfo1, innerExtensionInfo.moduleName, isModuleExisted);
78 innerExtensionInfo.type = ExtensionAbilityType::DRIVER;
79 newInfo1.InsertExtensionInfo(extensionKey, innerExtensionInfo);
80 installer.RemoveDriverSoFile(newInfo1, innerExtensionInfo.moduleName, isModuleExisted);
81 innerExtensionInfo.metadata.push_back(metaData);
82 newInfo1.InsertExtensionInfo(extensionKey, innerExtensionInfo);
83 installer.RemoveDriverSoFile(newInfo1, innerExtensionInfo.moduleName, isModuleExisted);
84
85 installer.CreateDriverSoDestinedDir("", "", "", "", isModuleExisted);
86 std::string bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
87 std::string moduleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
88 std::string fileName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
89 std::string destinedDir = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
90 isModuleExisted = fdp.ConsumeBool();
91 installer.CreateDriverSoDestinedDir(bundleName, moduleName, fileName, destinedDir, isModuleExisted);
92 destinedDir = "../invalid_path";
93 installer.CreateDriverSoDestinedDir(bundleName, moduleName, fileName, destinedDir, isModuleExisted);
94
95 std::string srcPath = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
96 isModuleExisted = fdp.ConsumeBool();
97 installer.CopyDriverSoFile(newInfo1, fileName, isModuleExisted);
98 installer.CopyDriverSoFile(newInfo2, fileName, isModuleExisted);
99
100 installer.RenameDriverFile(newInfo1);
101 installer.RenameDriverFile(newInfo2);
102
103 return true;
104 }
105 }
106
107 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)108 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
109 {
110 /* Run your code on data */
111 OHOS::DoSomethingInterestingWithMyAPI(data, size);
112 return 0;
113 }
114