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