1fb299fa2Sopenharmony_ci/* 2fb299fa2Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License. 5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at 6fb299fa2Sopenharmony_ci * 7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fb299fa2Sopenharmony_ci * 9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and 13fb299fa2Sopenharmony_ci * limitations under the License. 14fb299fa2Sopenharmony_ci */ 15fb299fa2Sopenharmony_ci 16fb299fa2Sopenharmony_ci#include "UpdaterStartUpdaterProc_fuzzer.h" 17fb299fa2Sopenharmony_ci 18fb299fa2Sopenharmony_ci#include <array> 19fb299fa2Sopenharmony_ci#include <cstddef> 20fb299fa2Sopenharmony_ci#include <cstdint> 21fb299fa2Sopenharmony_ci#include <iostream> 22fb299fa2Sopenharmony_ci#include <string> 23fb299fa2Sopenharmony_ci#include <vector> 24fb299fa2Sopenharmony_ci#include "pkg_algorithm.h" 25fb299fa2Sopenharmony_ci#include "pkg_algo_digest.h" 26fb299fa2Sopenharmony_ci#include "pkg_utils.h" 27fb299fa2Sopenharmony_ci#include "updater.h" 28fb299fa2Sopenharmony_ci#include "updater_ui.h" 29fb299fa2Sopenharmony_ci 30fb299fa2Sopenharmony_ciusing namespace Updater; 31fb299fa2Sopenharmony_ciusing namespace Hpackage; 32fb299fa2Sopenharmony_ci 33fb299fa2Sopenharmony_ciconst static std::string TEST_PATH_TO = "/data/fuzz/test/"; 34fb299fa2Sopenharmony_ci 35fb299fa2Sopenharmony_cistatic inline std::string GetTestCertName() 36fb299fa2Sopenharmony_ci{ 37fb299fa2Sopenharmony_ci std::string name = TEST_PATH_TO; 38fb299fa2Sopenharmony_ci name += "signing_cert.crt"; 39fb299fa2Sopenharmony_ci return name; 40fb299fa2Sopenharmony_ci} 41fb299fa2Sopenharmony_ci 42fb299fa2Sopenharmony_cistatic inline std::string GetTestPrivateKeyName() 43fb299fa2Sopenharmony_ci{ 44fb299fa2Sopenharmony_ci std::string name = TEST_PATH_TO; 45fb299fa2Sopenharmony_ci name += "rsa_private_key2048.pem"; 46fb299fa2Sopenharmony_ci return name; 47fb299fa2Sopenharmony_ci} 48fb299fa2Sopenharmony_ci 49fb299fa2Sopenharmony_cistatic int32_t BuildFileDigest(uint8_t &digest, size_t size, const std::string &packagePath) 50fb299fa2Sopenharmony_ci{ 51fb299fa2Sopenharmony_ci PkgManager::StreamPtr stream = nullptr; 52fb299fa2Sopenharmony_ci PkgManager::PkgManagerPtr packageManager = PkgManager::CreatePackageInstance(); 53fb299fa2Sopenharmony_ci 54fb299fa2Sopenharmony_ci int32_t ret = packageManager->CreatePkgStream(stream, packagePath, 0, PkgStream::PkgStreamType_Read); 55fb299fa2Sopenharmony_ci if (ret != PKG_SUCCESS) { 56fb299fa2Sopenharmony_ci PKG_LOGE("Create input stream fail %s", packagePath.c_str()); 57fb299fa2Sopenharmony_ci packageManager->ClosePkgStream(stream); 58fb299fa2Sopenharmony_ci PkgManager::ReleasePackageInstance(packageManager); 59fb299fa2Sopenharmony_ci return ret; 60fb299fa2Sopenharmony_ci } 61fb299fa2Sopenharmony_ci size_t fileLen = stream->GetFileLength(); 62fb299fa2Sopenharmony_ci if (fileLen <= 0 || fileLen > SIZE_MAX) { 63fb299fa2Sopenharmony_ci PKG_LOGE("Invalid file len %zu to load %s", fileLen, stream->GetFileName().c_str()); 64fb299fa2Sopenharmony_ci packageManager->ClosePkgStream(stream); 65fb299fa2Sopenharmony_ci PkgManager::ReleasePackageInstance(packageManager); 66fb299fa2Sopenharmony_ci return PKG_INVALID_FILE; 67fb299fa2Sopenharmony_ci } 68fb299fa2Sopenharmony_ci 69fb299fa2Sopenharmony_ci size_t buffSize = 4096; 70fb299fa2Sopenharmony_ci PkgBuffer buff(buffSize); 71fb299fa2Sopenharmony_ci // 整包检查 72fb299fa2Sopenharmony_ci DigestAlgorithm::DigestAlgorithmPtr algorithm = PkgAlgorithmFactory::GetDigestAlgorithm(PKG_DIGEST_TYPE_SHA256); 73fb299fa2Sopenharmony_ci if (algorithm == nullptr) { 74fb299fa2Sopenharmony_ci PKG_LOGE("Invalid file %s", stream->GetFileName().c_str()); 75fb299fa2Sopenharmony_ci packageManager->ClosePkgStream(stream); 76fb299fa2Sopenharmony_ci PkgManager::ReleasePackageInstance(packageManager); 77fb299fa2Sopenharmony_ci return PKG_NOT_EXIST_ALGORITHM; 78fb299fa2Sopenharmony_ci } 79fb299fa2Sopenharmony_ci algorithm->Init(); 80fb299fa2Sopenharmony_ci 81fb299fa2Sopenharmony_ci size_t offset = 0; 82fb299fa2Sopenharmony_ci size_t readLen = 0; 83fb299fa2Sopenharmony_ci while (offset < fileLen) { 84fb299fa2Sopenharmony_ci ret = stream->Read(buff, offset, buffSize, readLen); 85fb299fa2Sopenharmony_ci if (ret != PKG_SUCCESS) { 86fb299fa2Sopenharmony_ci PKG_LOGE("read buffer fail %s", stream->GetFileName().c_str()); 87fb299fa2Sopenharmony_ci packageManager->ClosePkgStream(stream); 88fb299fa2Sopenharmony_ci PkgManager::ReleasePackageInstance(packageManager); 89fb299fa2Sopenharmony_ci return ret; 90fb299fa2Sopenharmony_ci } 91fb299fa2Sopenharmony_ci algorithm->Update(buff, readLen); 92fb299fa2Sopenharmony_ci 93fb299fa2Sopenharmony_ci offset += readLen; 94fb299fa2Sopenharmony_ci readLen = 0; 95fb299fa2Sopenharmony_ci } 96fb299fa2Sopenharmony_ci PkgBuffer signBuffer(&digest, size); 97fb299fa2Sopenharmony_ci algorithm->Final(signBuffer); 98fb299fa2Sopenharmony_ci packageManager->ClosePkgStream(stream); 99fb299fa2Sopenharmony_ci PkgManager::ReleasePackageInstance(packageManager); 100fb299fa2Sopenharmony_ci return PKG_SUCCESS; 101fb299fa2Sopenharmony_ci} 102fb299fa2Sopenharmony_ci 103fb299fa2Sopenharmony_cistatic int CreatePackageZip(const std::vector<std::string> &fstabFile) 104fb299fa2Sopenharmony_ci{ 105fb299fa2Sopenharmony_ci int32_t ret = PKG_SUCCESS; 106fb299fa2Sopenharmony_ci uint32_t updateFileVersion = 1000; 107fb299fa2Sopenharmony_ci uint32_t componentInfoIdBase = 100; 108fb299fa2Sopenharmony_ci uint8_t componentInfoFlags = 22; 109fb299fa2Sopenharmony_ci std::string testPackageName = "test_package.zip"; 110fb299fa2Sopenharmony_ci 111fb299fa2Sopenharmony_ci PKG_LOGI("\n\n ************* CreatePackageZip %s \r\n", testPackageName.c_str()); 112fb299fa2Sopenharmony_ci UpgradePkgInfoExt pkgInfo; 113fb299fa2Sopenharmony_ci pkgInfo.softwareVersion = strdup("100.100.100.100"); 114fb299fa2Sopenharmony_ci pkgInfo.date = strdup("2021-07-16"); 115fb299fa2Sopenharmony_ci pkgInfo.time = strdup("13:14:00"); 116fb299fa2Sopenharmony_ci pkgInfo.productUpdateId = strdup("555.555.100.555"); 117fb299fa2Sopenharmony_ci pkgInfo.entryCount = fstabFile.size(); 118fb299fa2Sopenharmony_ci pkgInfo.updateFileVersion = updateFileVersion; 119fb299fa2Sopenharmony_ci pkgInfo.digestMethod = PKG_DIGEST_TYPE_SHA256; 120fb299fa2Sopenharmony_ci pkgInfo.signMethod = PKG_SIGN_METHOD_RSA; 121fb299fa2Sopenharmony_ci pkgInfo.pkgType = PKG_PACK_TYPE_ZIP; 122fb299fa2Sopenharmony_ci std::string filePath; 123fb299fa2Sopenharmony_ci std::vector<ComponentInfoExt> comp(fstabFile.size()); 124fb299fa2Sopenharmony_ci for (size_t i = 0; i < fstabFile.size(); i++) { 125fb299fa2Sopenharmony_ci comp[i].componentAddr = strdup(fstabFile[i].c_str()); 126fb299fa2Sopenharmony_ci filePath = TEST_PATH_TO; 127fb299fa2Sopenharmony_ci filePath += fstabFile[i].c_str(); 128fb299fa2Sopenharmony_ci comp[i].filePath = strdup(filePath.c_str()); 129fb299fa2Sopenharmony_ci comp[i].version = strdup("55555555"); 130fb299fa2Sopenharmony_ci 131fb299fa2Sopenharmony_ci ret = BuildFileDigest(*comp[i].digest, sizeof(comp[i].digest), filePath); 132fb299fa2Sopenharmony_ci comp[i].size = GetFileSize(filePath); 133fb299fa2Sopenharmony_ci comp[i].originalSize = comp[i].size; 134fb299fa2Sopenharmony_ci comp[i].id = componentInfoIdBase; 135fb299fa2Sopenharmony_ci comp[i].resType = 1; 136fb299fa2Sopenharmony_ci comp[i].type = 1; 137fb299fa2Sopenharmony_ci comp[i].flags = componentInfoFlags; 138fb299fa2Sopenharmony_ci filePath.clear(); 139fb299fa2Sopenharmony_ci } 140fb299fa2Sopenharmony_ci 141fb299fa2Sopenharmony_ci std::string packagePath = TEST_PATH_TO; 142fb299fa2Sopenharmony_ci packagePath += testPackageName; 143fb299fa2Sopenharmony_ci ret = CreatePackage(&pkgInfo, comp, packagePath.c_str(), GetTestPrivateKeyName().c_str()); 144fb299fa2Sopenharmony_ci if (ret == 0) { 145fb299fa2Sopenharmony_ci PKG_LOGI("CreatePackage success offset"); 146fb299fa2Sopenharmony_ci } 147fb299fa2Sopenharmony_ci for (size_t i = 0; i < fstabFile.size(); i++) { 148fb299fa2Sopenharmony_ci free(comp[i].componentAddr); 149fb299fa2Sopenharmony_ci free(comp[i].filePath); 150fb299fa2Sopenharmony_ci free(comp[i].version); 151fb299fa2Sopenharmony_ci } 152fb299fa2Sopenharmony_ci free(pkgInfo.productUpdateId); 153fb299fa2Sopenharmony_ci free(pkgInfo.softwareVersion); 154fb299fa2Sopenharmony_ci free(pkgInfo.date); 155fb299fa2Sopenharmony_ci free(pkgInfo.time); 156fb299fa2Sopenharmony_ci return ret; 157fb299fa2Sopenharmony_ci} 158fb299fa2Sopenharmony_ci 159fb299fa2Sopenharmony_cistatic int StartUpdaterProcFun(const std::string &patch) 160fb299fa2Sopenharmony_ci{ 161fb299fa2Sopenharmony_ci UpdaterStatus status; 162fb299fa2Sopenharmony_ci std::vector<std::string> components; 163fb299fa2Sopenharmony_ci PkgManager::PkgManagerPtr pkgManager = PkgManager::CreatePackageInstance(); 164fb299fa2Sopenharmony_ci 165fb299fa2Sopenharmony_ci pkgManager->LoadPackage(patch, GetTestCertName(), components); 166fb299fa2Sopenharmony_ci UpdaterParams upParams; 167fb299fa2Sopenharmony_ci upParams.updatePackage.push_back(patch); 168fb299fa2Sopenharmony_ci upParams.retryCount = 0; 169fb299fa2Sopenharmony_ci status = StartUpdaterProc(pkgManager, upParams); 170fb299fa2Sopenharmony_ci LOG(INFO) << "[fuzz] status " << status; 171fb299fa2Sopenharmony_ci PkgManager::ReleasePackageInstance(pkgManager); 172fb299fa2Sopenharmony_ci return status; 173fb299fa2Sopenharmony_ci} 174fb299fa2Sopenharmony_ci 175fb299fa2Sopenharmony_cinamespace OHOS { 176fb299fa2Sopenharmony_ci void FuzzStartUpdaterProc(const uint8_t* data, size_t size) 177fb299fa2Sopenharmony_ci { 178fb299fa2Sopenharmony_ci FILE *pFile; 179fb299fa2Sopenharmony_ci std::vector<std::string> fstabFile = { 180fb299fa2Sopenharmony_ci "build_tools.zip", 181fb299fa2Sopenharmony_ci "updater.txt" 182fb299fa2Sopenharmony_ci }; 183fb299fa2Sopenharmony_ci 184fb299fa2Sopenharmony_ci pFile = fopen("updater.txt", "w+"); 185fb299fa2Sopenharmony_ci if (pFile == nullptr) { 186fb299fa2Sopenharmony_ci LOG(ERROR) << "[fuzz]open file failed"; 187fb299fa2Sopenharmony_ci return; 188fb299fa2Sopenharmony_ci } 189fb299fa2Sopenharmony_ci 190fb299fa2Sopenharmony_ci fwrite(data, 1, size, pFile); 191fb299fa2Sopenharmony_ci fclose(pFile); 192fb299fa2Sopenharmony_ci 193fb299fa2Sopenharmony_ci CreatePackageZip(fstabFile); 194fb299fa2Sopenharmony_ci StartUpdaterProcFun(TEST_PATH_TO + "test_package.zip"); 195fb299fa2Sopenharmony_ci 196fb299fa2Sopenharmony_ci remove("updater.txt"); 197fb299fa2Sopenharmony_ci remove("test_package.zip"); 198fb299fa2Sopenharmony_ci } 199fb299fa2Sopenharmony_ci} 200fb299fa2Sopenharmony_ci 201fb299fa2Sopenharmony_ci/* Fuzzer entry point */ 202fb299fa2Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 203fb299fa2Sopenharmony_ci{ 204fb299fa2Sopenharmony_ci /* Run your code on data */ 205fb299fa2Sopenharmony_ci OHOS::FuzzStartUpdaterProc(data, size); 206fb299fa2Sopenharmony_ci return 0; 207fb299fa2Sopenharmony_ci} 208fb299fa2Sopenharmony_ci 209