• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 U32_AT_SIZE = 4;
29 const std::string BUNDLE_TEMP_NAME = "temp_bundle_name";
30 const std::string BUNDLE_PATH = "test.hap";
31 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)32 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
33 {
34     std::shared_ptr<BundleMgrService> bundleMgrService_ = DelayedSingleton<BundleMgrService>::GetInstance();
35     bundleMgrService_->InitBmsParam();
36     bundleMgrService_->InitPreInstallExceptionMgr();
37 
38     auto preInstallExceptionMgr = bundleMgrService_->GetPreInstallExceptionMgr();
39     bundleMgrService_->GetBmsParam();
40     std::set<std::string> oldExceptionPaths;
41     oldExceptionPaths.insert(std::string(data, size));
42     std::set<std::string> oldExceptionBundleNames;
43     oldExceptionBundleNames.insert(std::string(data, size));
44     std::set<std::string> exceptionAppServicePaths;
45     std::set<std::string> exceptionAppServiceBundleNames;
46     preInstallExceptionMgr->GetAllPreInstallExceptionInfo(oldExceptionPaths, oldExceptionBundleNames,
47         exceptionAppServicePaths, exceptionAppServiceBundleNames);
48 
49     preInstallExceptionMgr->ClearAll();
50     std::set<std::string> exceptionPaths;
51     exceptionPaths.insert(std::string(data, size));
52     std::set<std::string> exceptionBundleNames;
53     exceptionBundleNames.insert(std::string(data, size));
54     preInstallExceptionMgr->GetAllPreInstallExceptionInfo(exceptionPaths, exceptionBundleNames,
55         exceptionAppServicePaths, exceptionAppServiceBundleNames);
56 
57     preInstallExceptionMgr->SavePreInstallExceptionBundleName(BUNDLE_TEMP_NAME);
58     preInstallExceptionMgr->SavePreInstallExceptionPath(BUNDLE_PATH);
59     preInstallExceptionMgr->GetAllPreInstallExceptionInfo(exceptionPaths, exceptionBundleNames,
60         exceptionAppServicePaths, exceptionAppServiceBundleNames);
61     preInstallExceptionMgr->DeletePreInstallExceptionBundleName(BUNDLE_TEMP_NAME);
62     preInstallExceptionMgr->DeletePreInstallExceptionPath(BUNDLE_PATH);
63     preInstallExceptionMgr->GetAllPreInstallExceptionInfo(exceptionPaths, exceptionBundleNames,
64         exceptionAppServicePaths, exceptionAppServiceBundleNames);
65 
66     if (oldExceptionPaths.size() > 0) {
67         for (const auto& pathIter : oldExceptionPaths) {
68             preInstallExceptionMgr->SavePreInstallExceptionPath(pathIter);
69         }
70     }
71 
72     if (oldExceptionBundleNames.size() > 0) {
73         for (const auto& bundleNameIter : oldExceptionBundleNames) {
74             preInstallExceptionMgr->SavePreInstallExceptionPath(bundleNameIter);
75         }
76     }
77     return true;
78 }
79 } // namespace OHOS
80 
81 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)82 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
83 {
84     /* Run your code on data */
85     if (data == nullptr) {
86         return 0;
87     }
88 
89     if (size < OHOS::U32_AT_SIZE) {
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 }