1 /*
2 * Copyright (c) 2021 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 "UpdaterStartUpdaterProc_fuzzer.h"
17
18 #include <array>
19 #include <cstddef>
20 #include <cstdint>
21 #include <iostream>
22 #include <string>
23 #include <vector>
24 #include "pkg_algorithm.h"
25 #include "pkg_algo_digest.h"
26 #include "pkg_utils.h"
27 #include "updater.h"
28 #include "updater_ui.h"
29
30 using namespace Updater;
31 using namespace Hpackage;
32
33 const static std::string TEST_PATH_TO = "/data/fuzz/test/";
34
GetTestCertName()35 static inline std::string GetTestCertName()
36 {
37 std::string name = TEST_PATH_TO;
38 name += "signing_cert.crt";
39 return name;
40 }
41
GetTestPrivateKeyName()42 static inline std::string GetTestPrivateKeyName()
43 {
44 std::string name = TEST_PATH_TO;
45 name += "rsa_private_key2048.pem";
46 return name;
47 }
48
BuildFileDigest(uint8_t & digest,size_t size,const std::string & packagePath)49 static int32_t BuildFileDigest(uint8_t &digest, size_t size, const std::string &packagePath)
50 {
51 PkgManager::StreamPtr stream = nullptr;
52 PkgManager::PkgManagerPtr packageManager = PkgManager::GetPackageInstance();
53
54 int32_t ret = packageManager->CreatePkgStream(stream, packagePath, 0, PkgStream::PkgStreamType_Read);
55 PKG_CHECK(ret == PKG_SUCCESS, packageManager->ClosePkgStream(stream);
56 return ret, "Create input stream fail %s", packagePath.c_str());
57 size_t fileLen = stream->GetFileLength();
58 PKG_CHECK(fileLen > 0, packageManager->ClosePkgStream(stream); return PKG_INVALID_FILE, "invalid file to load");
59 PKG_CHECK(fileLen <= SIZE_MAX, packageManager->ClosePkgStream(stream); return PKG_INVALID_FILE,
60 "Invalid file len %zu to load %s", fileLen, stream->GetFileName().c_str());
61
62 size_t buffSize = 4096;
63 PkgBuffer buff(buffSize);
64 // 整包检查
65 DigestAlgorithm::DigestAlgorithmPtr algorithm = PkgAlgorithmFactory::GetDigestAlgorithm(PKG_DIGEST_TYPE_SHA256);
66 PKG_CHECK(algorithm != nullptr, packageManager->ClosePkgStream(stream); return PKG_NOT_EXIST_ALGORITHM,
67 "Invalid file %s", stream->GetFileName().c_str());
68 algorithm->Init();
69
70 size_t offset = 0;
71 size_t readLen = 0;
72 while (offset < fileLen) {
73 ret = stream->Read(buff, offset, buffSize, readLen);
74 PKG_CHECK(ret == PKG_SUCCESS,
75 packageManager->ClosePkgStream(stream); return ret,
76 "read buffer fail %s", stream->GetFileName().c_str());
77 algorithm->Update(buff, readLen);
78
79 offset += readLen;
80 readLen = 0;
81 }
82 PkgBuffer signBuffer(&digest, size);
83 algorithm->Final(signBuffer);
84 packageManager->ClosePkgStream(stream);
85 return PKG_SUCCESS;
86 }
87
CreatePackageZip(const std::vector<std::string> & fstabFile)88 static int CreatePackageZip(const std::vector<std::string> &fstabFile)
89 {
90 int32_t ret = PKG_SUCCESS;
91 uint32_t updateFileVersion = 1000;
92 uint32_t componentInfoIdBase = 100;
93 uint8_t componentInfoFlags = 22;
94 std::string testPackageName = "test_package.zip";
95
96 PKG_LOGI("\n\n ************* CreatePackageZip %s \r\n", testPackageName.c_str());
97 UpgradePkgInfoExt pkgInfo;
98 pkgInfo.softwareVersion = strdup("100.100.100.100");
99 pkgInfo.date = strdup("2021-07-16");
100 pkgInfo.time = strdup("13:14:00");
101 pkgInfo.productUpdateId = strdup("555.555.100.555");
102 pkgInfo.entryCount = fstabFile.size();
103 pkgInfo.updateFileVersion = updateFileVersion;
104 pkgInfo.digestMethod = PKG_DIGEST_TYPE_SHA256;
105 pkgInfo.signMethod = PKG_SIGN_METHOD_RSA;
106 pkgInfo.pkgType = PKG_PACK_TYPE_ZIP;
107 std::string filePath;
108 ComponentInfoExt comp[fstabFile.size()];
109 for (size_t i = 0; i < fstabFile.size(); i++) {
110 comp[i].componentAddr = strdup(fstabFile[i].c_str());
111 filePath = TEST_PATH_TO;
112 filePath += fstabFile[i].c_str();
113 comp[i].filePath = strdup(filePath.c_str());
114 comp[i].version = strdup("55555555");
115
116 ret = BuildFileDigest(*comp[i].digest, sizeof(comp[i].digest), filePath);
117 comp[i].size = GetFileSize(filePath);
118 comp[i].originalSize = comp[i].size;
119 comp[i].id = componentInfoIdBase;
120 comp[i].resType = 1;
121 comp[i].type = 1;
122 comp[i].flags = componentInfoFlags;
123 filePath.clear();
124 }
125
126 std::string packagePath = TEST_PATH_TO;
127 packagePath += testPackageName;
128 ret = CreatePackage(&pkgInfo, comp, packagePath.c_str(), GetTestPrivateKeyName().c_str());
129 if (ret == 0) {
130 PKG_LOGI("CreatePackage success offset");
131 }
132 for (size_t i = 0; i < fstabFile.size(); i++) {
133 free(comp[i].componentAddr);
134 free(comp[i].filePath);
135 free(comp[i].version);
136 }
137 free(pkgInfo.productUpdateId);
138 free(pkgInfo.softwareVersion);
139 free(pkgInfo.date);
140 free(pkgInfo.time);
141 return ret;
142 }
143
StartUpdaterProcFun(const std::string & patch)144 static int StartUpdaterProcFun(const std::string &patch)
145 {
146 UpdaterStatus status;
147 std::vector<std::string> components;
148 int maxTemperature;
149 PkgManager::PkgManagerPtr pkgManager = PkgManager::GetPackageInstance();
150
151 pkgManager->LoadPackage(patch, GetTestCertName(), components);
152 status = StartUpdaterProc(pkgManager, patch, 0, maxTemperature);
153 LOG(INFO) << "[fuzz] status " << status;
154
155 return status;
156 }
157
158 namespace OHOS {
FuzzStartUpdaterProc(const uint8_t * data,size_t size)159 bool FuzzStartUpdaterProc(const uint8_t* data, size_t size)
160 {
161 FILE *pFile;
162 std::vector<std::string> fstabFile = {
163 "build_tools.zip",
164 "updater.txt"
165 };
166
167 pFile = fopen("updater.txt", "w+");
168 if (pFile == nullptr) {
169 LOG(ERROR) << "[fuzz]open file failed";
170 return -1;
171 }
172
173 fwrite(data, 1, size, pFile);
174 fclose(pFile);
175
176 CreatePackageZip(fstabFile);
177 StartUpdaterProcFun(TEST_PATH_TO + "test_package.zip");
178
179 remove("updater.txt");
180 remove("test_package.zip");
181
182 return 0;
183 }
184 }
185
186 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)187 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
188 {
189 /* Run your code on data */
190 OHOS::FuzzStartUpdaterProc(data, size);
191 return 0;
192 }
193
194