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 #include "quickfixchecker_fuzzer.h"
17 #define private public
18 #include "quick_fix_checker.h"
19 #include "securec.h"
20 
21 using namespace OHOS::AppExecFwk;
22 namespace OHOS {
23 constexpr size_t FOO_MAX_LEN = 1024;
24 constexpr size_t U32_AT_SIZE = 4;
25 constexpr size_t MESSAGE_SIZE = 4;
26 constexpr size_t DCAMERA_SHIFT_24 = 24;
27 constexpr size_t DCAMERA_SHIFT_16 = 16;
28 constexpr size_t DCAMERA_SHIFT_8 = 8;
29 const std::string BUNDLE_NAME = "com.example.bmsaccesstoken1";
30 const uint32_t QUICK_FIX_VERSION_CODE = 1;
31 const uint32_t BUNDLE_VERSION_CODE = 1;
32 const std::string QUICK_FIX_VERSION_NAME = "1.0";
33 const std::string BUNDLE_VERSION_NAME = "1.0";
34 
GetU32Data(const char * ptr)35 uint32_t GetU32Data(const char* ptr)
36 {
37     return (ptr[0] << DCAMERA_SHIFT_24) | (ptr[1] << DCAMERA_SHIFT_16) | (ptr[2] << DCAMERA_SHIFT_8) | (ptr[3]);
38 }
39 
CreateAppQuickFix()40 AppQuickFix CreateAppQuickFix()
41 {
42     AppqfInfo appInfo;
43     appInfo.versionCode = QUICK_FIX_VERSION_CODE;
44     appInfo.versionName = QUICK_FIX_VERSION_NAME;
45     appInfo.type = QuickFixType::PATCH;
46     HqfInfo hqfInfo;
47     hqfInfo.moduleName = "entry";
48     hqfInfo.type = QuickFixType::PATCH;
49     appInfo.hqfInfos.push_back(hqfInfo);
50     AppQuickFix appQuickFix;
51     appQuickFix.bundleName = BUNDLE_NAME;
52     appQuickFix.versionCode = BUNDLE_VERSION_CODE;
53     appQuickFix.versionName = BUNDLE_VERSION_NAME;
54     appQuickFix.deployingAppqfInfo = appInfo;
55     return appQuickFix;
56 }
57 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)58 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
59 {
60     QuickFixChecker quickFixChecker;
61     std::vector<std::string> bundlePaths;
62     std::vector<Security::Verify::HapVerifyResult> hapVerifyRes;
63     quickFixChecker.CheckMultipleHqfsSignInfo(bundlePaths, hapVerifyRes);
64     std::unordered_map<std::string, AppQuickFix> infos;
65     AppQuickFix appQuickFix = CreateAppQuickFix();
66     infos.emplace("appQuickFix_1", appQuickFix);
67     quickFixChecker.CheckAppQuickFixInfos(infos);
68     quickFixChecker.CheckMultiNativeSo(infos);
69     Security::Verify::ProvisionInfo provisionInfo;
70     BundleInfo bundleInfo;
71     quickFixChecker.CheckPatchWithInstalledBundle(appQuickFix, bundleInfo, provisionInfo);
72     quickFixChecker.CheckHotReloadWithInstalledBundle(appQuickFix, bundleInfo);
73     quickFixChecker.CheckSignatureInfo(bundleInfo, provisionInfo);
74     quickFixChecker.GetAppDistributionType(Security::Verify::AppDistType::APP_GALLERY);
75     quickFixChecker.CheckCommonWithInstalledBundle(appQuickFix, bundleInfo);
76     quickFixChecker.CheckModuleNameExist(bundleInfo, infos);
77     quickFixChecker.GetAppProvisionType(Security::Verify::ProvisionType::DEBUG);
78     AppqfInfo qfInfo;
79     qfInfo.cpuAbi = "arm";
80     quickFixChecker.CheckPatchNativeSoWithInstalledBundle(bundleInfo, qfInfo);
81     return true;
82 }
83 }
84 
85 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)86 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
87 {
88     /* Run your code on data */
89     if (data == nullptr) {
90         return 0;
91     }
92 
93     if (size < OHOS::U32_AT_SIZE) {
94         return 0;
95     }
96 
97     /* Validate the length of size */
98     if (size > OHOS::FOO_MAX_LEN) {
99         return 0;
100     }
101 
102     char* ch = static_cast<char*>(malloc(size + 1));
103     if (ch == nullptr) {
104         return 0;
105     }
106 
107     (void)memset_s(ch, size + 1, 0x00, size + 1);
108     if (memcpy_s(ch, size, data, size) != EOK) {
109         free(ch);
110         ch = nullptr;
111         return 0;
112     }
113     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
114     free(ch);
115     ch = nullptr;
116     return 0;
117 }