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 "quickfixdeployer_fuzzer.h"
17 #define private public
18 #include "quick_fix_deployer.h"
19 #include "securec.h"
20 #include "inner_bundle_info.h"
21
22 using namespace OHOS::AppExecFwk;
23 namespace OHOS {
24 constexpr size_t FOO_MAX_LEN = 1024;
25 constexpr size_t U32_AT_SIZE = 4;
26 constexpr size_t MESSAGE_SIZE = 4;
27 constexpr size_t DCAMERA_SHIFT_24 = 24;
28 constexpr size_t DCAMERA_SHIFT_16 = 16;
29 constexpr size_t DCAMERA_SHIFT_8 = 8;
30 const std::string BUNDLE_NAME = "com.example.bmsaccesstoken1";
31 const uint32_t QUICK_FIX_VERSION_CODE = 1;
32 const uint32_t BUNDLE_VERSION_CODE = 1;
33 const std::string QUICK_FIX_VERSION_NAME = "1.0";
34 const std::string BUNDLE_VERSION_NAME = "1.0";
35
DoSomething2(const char * data,size_t size)36 void DoSomething2(const char* data, size_t size) {}
37
GetU32Data(const char * ptr)38 uint32_t GetU32Data(const char* ptr)
39 {
40 return (ptr[0] << DCAMERA_SHIFT_24) | (ptr[1] << DCAMERA_SHIFT_16) | (ptr[2] << DCAMERA_SHIFT_8) | (ptr[3]);
41 }
42
CreateAppQuickFix()43 AppQuickFix CreateAppQuickFix()
44 {
45 AppqfInfo appInfo;
46 appInfo.versionCode = QUICK_FIX_VERSION_CODE;
47 appInfo.versionName = QUICK_FIX_VERSION_NAME;
48 appInfo.type = QuickFixType::PATCH;
49 HqfInfo hqfInfo;
50 hqfInfo.moduleName = "entry";
51 hqfInfo.type = QuickFixType::PATCH;
52 appInfo.hqfInfos.push_back(hqfInfo);
53 AppQuickFix appQuickFix;
54 appQuickFix.bundleName = BUNDLE_NAME;
55 appQuickFix.versionCode = BUNDLE_VERSION_CODE;
56 appQuickFix.versionName = BUNDLE_VERSION_NAME;
57 appQuickFix.deployingAppqfInfo = appInfo;
58 return appQuickFix;
59 }
60
DoSomethingInterestingWithMyAPI(const char * data,size_t size)61 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
62 {
63 std::string targetPath(data, size);
64 nlohmann::json jsonObject;
65 std::vector<std::string> bundlePaths;
66 QuickFixDeployer quickFixDeployer(bundlePaths, false, targetPath);
67 std::unordered_map<std::string, AppQuickFix> infos;
68 InnerAppQuickFix oldInnerAppQuickFix;
69 InnerAppQuickFix newInnerAppQuickFix;
70 std::vector<std::string> bundleFilePaths;
71 bundleFilePaths.push_back(targetPath);
72 quickFixDeployer.GetDeployQuickFixResult();
73 quickFixDeployer.DeployQuickFix();
74 quickFixDeployer.GetQuickFixDataMgr();
75 quickFixDeployer.SaveToInnerBundleInfo(newInnerAppQuickFix);
76 quickFixDeployer.ToDeployEndStatus(newInnerAppQuickFix, oldInnerAppQuickFix);
77 quickFixDeployer.ToDeployStartStatus(bundleFilePaths, newInnerAppQuickFix, oldInnerAppQuickFix);
78 quickFixDeployer.ParseAndCheckAppQuickFixInfos(bundleFilePaths, infos);
79 quickFixDeployer.ToInnerAppQuickFix(infos, oldInnerAppQuickFix, newInnerAppQuickFix);
80 BundleInfo bundleInfo;
81 std::string bundleName(data, size);
82 quickFixDeployer.GetBundleInfo(bundleName, bundleInfo);
83 quickFixDeployer.ProcessPatchDeployStart(bundleFilePaths, bundleInfo, infos);
84 std::unordered_map<std::string, AppQuickFix> infos1;
85 const AppQuickFix appQuickFix = CreateAppQuickFix();
86 quickFixDeployer.ProcessHotReloadDeployStart(bundleInfo, appQuickFix);
87 quickFixDeployer.ProcessPatchDeployEnd(appQuickFix, targetPath);
88 quickFixDeployer.ProcessHotReloadDeployEnd(appQuickFix, targetPath);
89 AppQuickFix newAppQuickFix = CreateAppQuickFix();
90 AppQuickFix oldAppQuickFix = CreateAppQuickFix();
91 quickFixDeployer.CheckPatchVersionCode(newAppQuickFix, oldAppQuickFix);
92 QuickFixMark mark;
93 mark.bundleName = appQuickFix.bundleName;
94 mark.status = QuickFixStatus::DEPLOY_START;
95 InnerAppQuickFix innerAppQuickFix(appQuickFix, mark);
96 quickFixDeployer.SaveAppQuickFix(innerAppQuickFix);
97 quickFixDeployer.MoveHqfFiles(innerAppQuickFix, targetPath);
98 std::vector<std::string> realPaths;
99 quickFixDeployer.ProcessBundleFilePaths(bundleFilePaths, realPaths);
100 quickFixDeployer.ToDeployQuickFixResult(appQuickFix);
101 quickFixDeployer.ProcessNativeLibraryPath(targetPath, innerAppQuickFix);
102 quickFixDeployer.ResetNativeSoAttrs(infos1);
103 DoSomething2(data, size);
104 return true;
105 }
106 }
DoSomething2(const char * data,size_t size)107 void DoSomething2(const char* data, size_t size)
108 {
109 std::string targetPath(data, size);
110 std::vector<std::string> bundlePaths;
111 std::string hqfSoPath(data, size);
112 QuickFixDeployer quickFixDeployer(bundlePaths, false, hqfSoPath);
113 BundleInfo bundleInfo;
114 HapModuleInfo info;
115 info.moduleName = "entry";
116 bundleInfo.hapModuleInfos.emplace_back(info);
117 std::unordered_map<std::string, AppQuickFix> infos1;
118 const AppQuickFix &appQuickFix = infos1.begin()->second;
119 quickFixDeployer.ExtractQuickFixSoFile(appQuickFix, hqfSoPath, bundleInfo);
120 std::string bundleName(data, size);
121 std::string moduleName(data, size);
122 InnerBundleInfo innerBundleInfo;
123 quickFixDeployer.FetchInnerBundleInfo(bundleName, innerBundleInfo);
124 AppqfInfo appqfInfo;
125 HqfInfo hqfInfo;
126 bool isLibIsolated = false;
127 std::string nativeLibraryPath(data, size);
128 std::string cpuAbi(data, size);
129 quickFixDeployer.FetchPatchNativeSoAttrs(appqfInfo, hqfInfo, isLibIsolated,
130 nativeLibraryPath, cpuAbi);
131 quickFixDeployer.HasNativeSoInBundle(appQuickFix);
132 quickFixDeployer.SendQuickFixSystemEvent(innerBundleInfo);
133 quickFixDeployer.ExtractSoAndApplyDiff(appQuickFix, bundleInfo, hqfSoPath);
134 quickFixDeployer.ExtractSoFiles(bundleInfo, moduleName, hqfSoPath);
135 std::string oldSoPath(data, size);
136 quickFixDeployer.ProcessApplyDiffPatch(appQuickFix,
137 appQuickFix.deployingAppqfInfo.hqfInfos[0], oldSoPath, hqfSoPath, reinterpret_cast<uintptr_t>(data));
138 quickFixDeployer.ExtractEncryptedSoFiles(bundleInfo, moduleName, reinterpret_cast<uintptr_t>(data), oldSoPath);
139 bundleInfo.applicationInfo.compileSdkType = "";
140 CodeSignatureParam codeSignatureParam;
141 quickFixDeployer.PrepareCodeSignatureParam(appQuickFix, hqfInfo, bundleInfo, hqfSoPath, codeSignatureParam);
142 InnerAppQuickFix innerAppQuickFix;
143 innerAppQuickFix.SetAppQuickFix(appQuickFix);
144 quickFixDeployer.VerifyCodeSignatureForHqf(innerAppQuickFix, oldSoPath);
145 std::vector<std::string> bundleFilePaths;
146 bundleFilePaths.push_back(targetPath);
147 quickFixDeployer.CheckHqfResourceIsValid(bundleFilePaths, bundleInfo);
148 quickFixDeployer.ExtractQuickFixResFile(appQuickFix, bundleInfo);
149 }
150 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)151 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
152 {
153 /* Run your code on data */
154 if (data == nullptr) {
155 return 0;
156 }
157
158 if (size < OHOS::U32_AT_SIZE) {
159 return 0;
160 }
161
162 /* Validate the length of size */
163 if (size > OHOS::FOO_MAX_LEN) {
164 return 0;
165 }
166
167 char* ch = static_cast<char*>(malloc(size + 1));
168 if (ch == nullptr) {
169 return 0;
170 }
171
172 (void)memset_s(ch, size + 1, 0x00, size + 1);
173 if (memcpy_s(ch, size, data, size) != EOK) {
174 free(ch);
175 ch = nullptr;
176 return 0;
177 }
178 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
179 free(ch);
180 ch = nullptr;
181 return 0;
182 }