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 "preinstallexceptionmgr_fuzzer.h"
18 
19 #include <cstddef>
20 #include <cstdint>
21 
22 #include "bundle_mgr_service.h"
23 #include "pre_install_exception_mgr.h"
24 #include "securec.h"
25 
26 using namespace OHOS::AppExecFwk;
27 namespace OHOS {
28 constexpr size_t FOO_MAX_LEN = 1024;
29 constexpr size_t U32_AT_SIZE = 4;
30 const std::string BUNDLE_TEMP_NAME = "temp_bundle_name";
31 const std::string BUNDLE_PATH = "test.hap";
32 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)33 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
34 {
35     std::shared_ptr<BundleMgrService> bundleMgrService_ = DelayedSingleton<BundleMgrService>::GetInstance();
36     bundleMgrService_->InitBmsParam();
37     bundleMgrService_->InitPreInstallExceptionMgr();
38 
39     auto preInstallExceptionMgr = bundleMgrService_->GetPreInstallExceptionMgr();
40     bundleMgrService_->GetBmsParam();
41     std::set<std::string> oldExceptionPaths;
42     oldExceptionPaths.insert(std::string(data, size));
43     std::set<std::string> oldExceptionBundleNames;
44     oldExceptionBundleNames.insert(std::string(data, size));
45     preInstallExceptionMgr->GetAllPreInstallExceptionInfo(oldExceptionPaths, oldExceptionBundleNames);
46 
47     preInstallExceptionMgr->ClearAll();
48     std::set<std::string> exceptionPaths;
49     exceptionPaths.insert(std::string(data, size));
50     std::set<std::string> exceptionBundleNames;
51     exceptionBundleNames.insert(std::string(data, size));
52     preInstallExceptionMgr->GetAllPreInstallExceptionInfo(exceptionPaths, exceptionBundleNames);
53 
54     preInstallExceptionMgr->SavePreInstallExceptionBundleName(BUNDLE_TEMP_NAME);
55     preInstallExceptionMgr->SavePreInstallExceptionPath(BUNDLE_PATH);
56     preInstallExceptionMgr->GetAllPreInstallExceptionInfo(exceptionPaths, exceptionBundleNames);
57     preInstallExceptionMgr->DeletePreInstallExceptionBundleName(BUNDLE_TEMP_NAME);
58     preInstallExceptionMgr->DeletePreInstallExceptionPath(BUNDLE_PATH);
59     preInstallExceptionMgr->GetAllPreInstallExceptionInfo(exceptionPaths, exceptionBundleNames);
60 
61     if (oldExceptionPaths.size() > 0) {
62         for (const auto& pathIter : oldExceptionPaths) {
63             preInstallExceptionMgr->SavePreInstallExceptionPath(pathIter);
64         }
65     }
66 
67     if (oldExceptionBundleNames.size() > 0) {
68         for (const auto& bundleNameIter : oldExceptionBundleNames) {
69             preInstallExceptionMgr->SavePreInstallExceptionPath(bundleNameIter);
70         }
71     }
72     return true;
73 }
74 } // namespace OHOS
75 
76 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)77 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
78 {
79     /* Run your code on data */
80     if (data == nullptr) {
81         return 0;
82     }
83 
84     if (size < OHOS::U32_AT_SIZE) {
85         return 0;
86     }
87 
88     /* Validate the length of size */
89     if (size > OHOS::FOO_MAX_LEN) {
90         return 0;
91     }
92 
93     char* ch = static_cast<char*>(malloc(size + 1));
94     if (ch == nullptr) {
95         return 0;
96     }
97 
98     (void)memset_s(ch, size + 1, 0x00, size + 1);
99     if (memcpy_s(ch, size, data, size) != EOK) {
100         free(ch);
101         ch = nullptr;
102         return 0;
103     }
104     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
105     free(ch);
106     ch = nullptr;
107     return 0;
108 }