• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "bundleinstallerhost_fuzzer.h"
17 
18 #define private public
19 
20 #include <cstddef>
21 #include <cstdint>
22 #include "bundle_installer_host.h"
23 #include "bundle_mgr_service.h"
24 #include "message_parcel.h"
25 #include "securec.h"
26 
27 using namespace OHOS::AppExecFwk;
28 namespace OHOS {
29 constexpr size_t U32_AT_SIZE = 4;
30 constexpr size_t MESSAGE_SIZE = 10;
31 constexpr size_t CODE_MAX = 13;
32 
GetU32Data(const char * ptr)33 uint32_t GetU32Data(const char* ptr)
34 {
35     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
36 }
37 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)38 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
39 {
40     auto bundleInstallerHost = std::make_unique<BundleInstallerHost>();
41 #ifdef ON_64BIT_SYSTEM
42     for (uint32_t code = 0; code <= CODE_MAX; code++) {
43         MessageParcel datas;
44         std::u16string descriptor = BundleInstallerHost::GetDescriptor();
45         datas.WriteInterfaceToken(descriptor);
46         datas.WriteBuffer(data, size);
47         datas.RewindRead(0);
48         MessageParcel reply;
49         MessageOption option;
50         DelayedSingleton<BundleMgrService>::GetInstance()->OnStop();
51         bundleInstallerHost->OnRemoteRequest(code, datas, reply, option);
52     }
53 #endif
54     int32_t userId = 0;
55     int32_t appIndex = 0;
56     int32_t dplType = 0;
57     int32_t streamInstallerId = 0;
58     std::string bundleFilePath(std::string(data, size));
59     std::string bundleName(std::string(data, size));
60     std::string modulePackage(std::string(data, size));
61     std::vector<std::string> bundleFilePaths;
62     InstallParam installParam;
63     UninstallParam uninstallParam;
64     sptr<IStatusReceiver> statusReceiver;
65     std::vector<std::string> originHapPaths;
66     bundleInstallerHost->Init();
67     bundleInstallerHost->Install(bundleFilePath, installParam, statusReceiver);
68     bundleInstallerHost->Recover(bundleName, installParam, statusReceiver);
69     bundleInstallerHost->Install(bundleFilePaths, installParam, statusReceiver);
70     bundleInstallerHost->Uninstall(bundleName, installParam, statusReceiver);
71     bundleInstallerHost->Uninstall(bundleName, modulePackage, installParam, statusReceiver);
72     bundleInstallerHost->Uninstall(uninstallParam, statusReceiver);
73     bundleInstallerHost->InstallByBundleName(bundleName, installParam, statusReceiver);
74     bundleInstallerHost->InstallSandboxApp(bundleName, dplType, userId, appIndex);
75     bundleInstallerHost->UninstallSandboxApp(bundleName, appIndex, userId);
76     bundleInstallerHost->CreateStreamInstaller(installParam, statusReceiver, originHapPaths);
77     bundleInstallerHost->DestoryBundleStreamInstaller(streamInstallerId);
78     bundleInstallerHost->StreamInstall(bundleFilePaths, installParam, statusReceiver);
79     bundleInstallerHost->UpdateBundleForSelf(bundleFilePaths, installParam, statusReceiver);
80     bundleInstallerHost->UninstallAndRecover(bundleName, installParam, statusReceiver);
81     bundleInstallerHost->GetCurTaskNum();
82     bundleInstallerHost->GetThreadsNum();
83     bundleInstallerHost->InstallCloneApp(bundleName, userId, appIndex);
84     bundleInstallerHost->UninstallCloneApp(bundleName, userId, appIndex, DestroyAppCloneParam());
85     bundleInstallerHost->InstallExisted(bundleName, userId);
86     bundleInstallerHost->CheckInstallParam(installParam);
87     InstallParam installParam2;
88     bundleInstallerHost->IsPermissionVaild(installParam, installParam2);
89     return true;
90 }
91 }
92 
93 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)94 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
95 {
96     /* Run your code on data */
97     if (data == nullptr) {
98         return 0;
99     }
100 
101     if (size < OHOS::U32_AT_SIZE) {
102         return 0;
103     }
104 
105     char* ch = static_cast<char*>(malloc(size + 1));
106     if (ch == nullptr) {
107         return 0;
108     }
109 
110     (void)memset_s(ch, size + 1, 0x00, size + 1);
111     if (memcpy_s(ch, size, data, size) != EOK) {
112         free(ch);
113         ch = nullptr;
114         return 0;
115     }
116 
117     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
118     free(ch);
119     ch = nullptr;
120     return 0;
121 }