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