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 #define private public
17 #include <cstddef>
18 #include <cstdint>
19
20 #include "app_service_fwk/app_service_fwk_installer.h"
21
22 #include "appservicefwkinstallerupdateappservice_fuzzer.h"
23 #include "securec.h"
24
25 using namespace OHOS::AppExecFwk;
26 namespace OHOS {
27 constexpr size_t FOO_MAX_LEN = 1024;
28 constexpr size_t U32_AT_SIZE = 4;
29 const int32_t VERSION_LOW = 0;
30 const std::string MODULE_NAME_TEST = "moduleName";
31 const std::string TEST_CREATE_FILE_PATH = "/data/test/resource/bms/app_service_test/test_create_dir/test.hap";
32 const std::string VERSION_ONE_LIBRARY_ONE_PATH =
33 "/data/test/resource/bms/app_service_test/test_create_dir/test.hap";
34
DoSomethingInterestingWithMyAPI(const char * data,size_t size)35 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
36 {
37 AppServiceFwkInstaller appServiceFwk;
38 InstallParam installParam;
39 installParam.copyHapToInstallPath = false;
40 std::unordered_map<std::string, InnerBundleInfo> infos;
41 InnerBundleInfo innerBundleInfo;
42 innerBundleInfo.currentPackage_ = MODULE_NAME_TEST;
43 infos.emplace(TEST_CREATE_FILE_PATH, innerBundleInfo);
44 appServiceFwk.UpdateAppService(innerBundleInfo, infos, installParam);
45 InnerBundleInfo oldInfo;
46 InnerBundleInfo newInfo;
47 appServiceFwk.CheckNeedUpdate(newInfo, oldInfo);
48 std::string hspPath = ", path: ";
49 appServiceFwk.ProcessBundleUpdateStatus(oldInfo, newInfo, VERSION_ONE_LIBRARY_ONE_PATH, installParam);
50 bool isReplace = true;
51 bool killProcess = false;
52 appServiceFwk.ProcessModuleUpdate(innerBundleInfo, oldInfo, hspPath, installParam);
53 appServiceFwk.RemoveLowerVersionSoDir(VERSION_LOW);
54 std::string bundlePath(data, size);
55 std::string cpuAbi(data, size);
56 std::string targetSoPath(data, size);
57 bool isPreInstalledBundle = false;
58 appServiceFwk.VerifyCodeSignatureForNativeFiles(bundlePath, cpuAbi, targetSoPath);
59 std::vector<Security::Verify::HapVerifyResult> hapVerifyResults;
60 appServiceFwk.DeliveryProfileToCodeSign(hapVerifyResults);
61 appServiceFwk.GenerateOdid(infos, hapVerifyResults);
62 return true;
63 }
64 }
65
66 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)67 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
68 {
69 /* Run your code on data */
70 if (data == nullptr) {
71 return 0;
72 }
73
74 if (size < OHOS::U32_AT_SIZE) {
75 return 0;
76 }
77
78 /* Validate the length of size */
79 if (size > OHOS::FOO_MAX_LEN) {
80 return 0;
81 }
82
83 char* ch = static_cast<char*>(malloc(size + 1));
84 if (ch == nullptr) {
85 return 0;
86 }
87
88 (void)memset_s(ch, size + 1, 0x00, size + 1);
89 if (memcpy_s(ch, size, data, size) != EOK) {
90 free(ch);
91 ch = nullptr;
92 return 0;
93 }
94 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
95 free(ch);
96 ch = nullptr;
97 return 0;
98 }