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 #define private public
17 #include <cstddef>
18 #include <cstdint>
19 #include <iostream>
20 #include "driver_installer.h"
21
22 #include "driverinstaller_fuzzer.h"
23 #include "securec.h"
24
25 using namespace OHOS::AppExecFwk;
26 using namespace OHOS::AAFwk;
27 namespace OHOS {
28 constexpr size_t U32_AT_SIZE = 4;
29 constexpr uint8_t ENABLE = 2;
DoSomethingInterestingWithMyAPI(const char * data,size_t size)30 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
31 {
32 std::shared_ptr driverInstaller = std::make_shared<DriverInstaller>();
33 InnerBundleInfo info;
34 std::string bundleName(reinterpret_cast<const char*>(data), size);
35 std::string moduleName(reinterpret_cast<const char*>(data), size);
36 std::string fileName(reinterpret_cast<const char*>(data), size);
37 std::string destinedDir(reinterpret_cast<const char*>(data), size);
38 bool isModuleExisted = *data % ENABLE;
39 auto res = driverInstaller->CreateDriverSoDestinedDir(bundleName,
40 moduleName, fileName, destinedDir, isModuleExisted);
41
42 driverInstaller->RemoveDriverSoFile(info, moduleName, isModuleExisted);
43
44 Metadata meta;
45 std::unordered_multimap<std::string, std::string> dirMap;
46 driverInstaller->FilterDriverSoFile(info, meta, dirMap, isModuleExisted);
47
48 std::unordered_map<std::string, InnerBundleInfo> newInfos;
49 InnerBundleInfo oldInfo;
50 driverInstaller->CopyAllDriverFile(newInfos, oldInfo);
51
52 std::string srcPath(reinterpret_cast<const char*>(data), size);
53 driverInstaller->CopyDriverSoFile(info, srcPath, isModuleExisted);
54
55 driverInstaller->RemoveAndReNameDriverFile(newInfos, oldInfo);
56
57 driverInstaller->RenameDriverFile(info);
58
59 return true;
60 }
61 }
62
63 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)64 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
65 {
66 /* Run your code on data */
67 if (data == nullptr) {
68 return 0;
69 }
70
71 /* Validate the length of size */
72 if (size < OHOS::U32_AT_SIZE) {
73 return 0;
74 }
75
76 char* ch = (char*)malloc(size + 1);
77 if (ch == nullptr) {
78 std::cout << "malloc failed." << std::endl;
79 return 0;
80 }
81
82 (void)memset_s(ch, size + 1, 0x00, size + 1);
83 if (memcpy_s(ch, size + 1, data, size) != EOK) {
84 std::cout << "copy failed." << std::endl;
85 free(ch);
86 ch = nullptr;
87 return 0;
88 }
89
90 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
91 free(ch);
92 ch = nullptr;
93 return 0;
94 }