/* * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "sys_installer_fuzzer.h" #include #include #include #include #include #include #include "isys_installer.h" #include "isys_installer_callback_func.h" #include "sys_installer_kits_impl.h" namespace OHOS { using namespace SysInstaller; constexpr int DATA_IDX_NEED_REBOOT = 0; constexpr int DATA_IDX_INSTALL_MODE = 1; constexpr int DATA_IDX_REBOOT = 2; constexpr int DATA_FEATURE_MIN_LEN = 3; constexpr int DATA_FEATURE_CLEAR_LEN = 2; FeatureInfo ParseFeatureInfo(const uint8_t* data, size_t size) { FeatureInfo featureInfo {}; size_t len = size / DATA_FEATURE_MIN_LEN; size_t offset = 0; std::string featureName = std::string(reinterpret_cast(data + offset), len); offset += len; std::string version = std::string(reinterpret_cast(data + offset), len); offset += len; std::string path = std::string(reinterpret_cast(data + offset), size - offset); return {(data[DATA_IDX_NEED_REBOOT] & 1) == 0, featureName, version, path}; } void InstallCloudRomFuzzTest(const std::string &taskId, const uint8_t* data, size_t size) { if (size < DATA_FEATURE_MIN_LEN) { return; } FeatureInfo featureInfo = ParseFeatureInfo(data, size); std::vector featureInfos = {featureInfo}; InstallMode installMode = (data[DATA_IDX_INSTALL_MODE] & 1) == 0 ? InstallMode::FEATURE_INSTALL : InstallMode::REGULAR_OTA; RebootStatus rebootStatus = (data[DATA_IDX_REBOOT] & 1) == 0 ? RebootStatus::NOT_REBOOT : RebootStatus::REBOOTED; SysInstallerKitsImpl::GetInstance().InstallCloudRom(taskId, installMode, featureInfos, rebootStatus); } void UninstallCloudRomFuzzTest(const std::string &taskId, const uint8_t* data, size_t size) { if (size < DATA_FEATURE_MIN_LEN) { return; } FeatureInfo featureInfo = ParseFeatureInfo(data, size); std::vector featureInfos = {featureInfo}; RebootStatus rebootStatus = (data[DATA_IDX_REBOOT] & 1) == 0 ? RebootStatus::NOT_REBOOT : RebootStatus::REBOOTED; SysInstallerKitsImpl::GetInstance().UninstallCloudRom(taskId, featureInfos, rebootStatus); } void GetFeatureStatusFuzzTest(const uint8_t* data, size_t size) { if (size < DATA_FEATURE_MIN_LEN) { return; } FeatureInfo featureInfo = ParseFeatureInfo(data, size); std::vector featureInfos = {featureInfo}; std::vector statusInfos = {}; SysInstallerKitsImpl::GetInstance().GetFeatureStatus(featureInfos, statusInfos); } void GetAllFeatureStatusFuzzTest(const uint8_t* data, size_t size) { std::vector statusInfos = {}; SysInstallerKitsImpl::GetInstance().GetAllFeatureStatus( std::string(reinterpret_cast(data), size), statusInfos); } void ClearCloudRomFuzzTest(const uint8_t* data, size_t size) { if (size < 0) { return; } std::string baseVersion = ""; std::string featureName = ""; if ((data[0] & 1) == 0) { baseVersion = std::string(reinterpret_cast(data), size); } else { size_t len = size / DATA_FEATURE_CLEAR_LEN; baseVersion = std::string(reinterpret_cast(data), len); featureName = std::string(reinterpret_cast(data + len), size - len); } SysInstallerKitsImpl::GetInstance().ClearCloudRom(baseVersion, featureName); } void FuzzSysInstaller(const uint8_t* data, size_t size) { std::string taskId = "fuzz_test"; SysInstallerKitsImpl::GetInstance().SysInstallerInit(taskId); SysInstallerKitsImpl::GetInstance().SetUpdateCallback(taskId, nullptr); SysInstallerKitsImpl::GetInstance().StartUpdatePackageZip(taskId, std::string(reinterpret_cast(data), size)); const std::string pkgPath = "/data/updater/fuzz/updater.zip"; const std::string location = "location"; SysInstallerKitsImpl::GetInstance().GetUpdateStatus(taskId); SysInstallerKitsImpl::GetInstance().StartUpdateParaZip(taskId, pkgPath, location, std::string(reinterpret_cast(data), size)); SysInstallerKitsImpl::GetInstance().AccDecompressAndVerifyPkg(taskId, pkgPath, std::string(reinterpret_cast(data), size), 1); SysInstallerKitsImpl::GetInstance().AccDeleteDir(taskId, std::string(reinterpret_cast(data), size)); SysInstallerKitsImpl::GetInstance().ClearVabMetadataAndCow(); SysInstallerKitsImpl::GetInstance().VabUpdateActive(); SysInstallerKitsImpl::GetInstance().StartVabMerge(taskId); const std::string action = "needMerge"; bool result = false; SysInstallerKitsImpl::GetInstance().GetMetadataResult(action, result); uint32_t reservedCores; std::copy(data, data + std::min(sizeof(uint32_t), size), reinterpret_cast(&reservedCores)); SysInstallerKitsImpl::GetInstance().SetCpuAffinity(taskId, reservedCores); } void FuzzSysInstallerCloudRom(const uint8_t* data, size_t size) { std::string taskId = "fuzz_test"; InstallCloudRomFuzzTest(taskId, data, size); UninstallCloudRomFuzzTest(taskId, data, size); GetFeatureStatusFuzzTest(data, size); GetAllFeatureStatusFuzzTest(data, size); ClearCloudRomFuzzTest(data, size); } } /* Fuzzer entry point */ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { /* Run your code on data */ OHOS::FuzzSysInstaller(data, size); OHOS::FuzzSysInstallerCloudRom(data, size); return 0; }