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 #include "formbundleforbidmgr_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #define private public
22 #define protected public
23 #include "feature/bundle_forbidden/form_bundle_forbid_mgr.h"
24 #undef private
25 #undef protected
26 #include "message_parcel.h"
27 #include "securec.h"
28
29 using namespace OHOS::AppExecFwk;
30
31 namespace OHOS {
32 constexpr size_t U32_AT_SIZE = 4;
33 constexpr uint8_t ENABLE = 2;
GetU32Data(const char * ptr)34 uint32_t GetU32Data(const char* ptr)
35 {
36 // convert fuzz input data to an integer
37 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
38 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)39 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
40 {
41 FormBundleForbidMgr& formbundleforbidmgr = FormBundleForbidMgr::GetInstance();
42 FormBundleForbidMgr bundleForbidMgr;
43 formbundleforbidmgr.Init();
44 std::string bundleName(data, size);
45 formbundleforbidmgr.IsBundleForbidden(bundleName);
46 bool isForbidden = *data % ENABLE;
47 formbundleforbidmgr.SetBundleForbiddenStatus(bundleName, isForbidden);
48 return true;
49 }
50
DoSomethingInterestingWithMyAPI1(const char * data,size_t size)51 bool DoSomethingInterestingWithMyAPI1(const char* data, size_t size)
52 {
53 FormBundleForbidMgr& formbundleforbidmgr = FormBundleForbidMgr::GetInstance();
54 FormBundleForbidMgr bundleForbidMgr;
55 formbundleforbidmgr.Init();
56 std::string bundleName(data, size);
57 formbundleforbidmgr.IsBundleForbidden(bundleName);
58 bool isForbidden = *data % ENABLE;
59 formbundleforbidmgr.SetBundleForbiddenStatus(bundleName, isForbidden);
60 return true;
61 }
62
DoSomethingInterestingWithMyAPI2(const char * data,size_t size)63 bool DoSomethingInterestingWithMyAPI2(const char* data, size_t size)
64 {
65 FormBundleForbidMgr& formbundleforbidmgr = FormBundleForbidMgr::GetInstance();
66 FormBundleForbidMgr bundleForbidMgr;
67 formbundleforbidmgr.Init();
68 std::string bundleName(data, size);
69 formbundleforbidmgr.IsBundleForbidden(bundleName);
70 bool isForbidden = *data % ENABLE;
71 formbundleforbidmgr.SetBundleForbiddenStatus(bundleName, isForbidden);
72 return true;
73 }
74
DoSomethingInterestingWithMyAPI3(const char * data,size_t size)75 bool DoSomethingInterestingWithMyAPI3(const char* data, size_t size)
76 {
77 FormBundleForbidMgr& formbundleforbidmgr = FormBundleForbidMgr::GetInstance();
78 FormBundleForbidMgr bundleForbidMgr;
79 formbundleforbidmgr.Init();
80 std::string bundleName(data, size);
81 formbundleforbidmgr.IsBundleForbidden(bundleName);
82 bool isForbidden = *data % ENABLE;
83 formbundleforbidmgr.SetBundleForbiddenStatus(bundleName, isForbidden);
84 return true;
85 }
86
DoSomethingInterestingWithMyAPI4(const char * data,size_t size)87 bool DoSomethingInterestingWithMyAPI4(const char* data, size_t size)
88 {
89 FormBundleForbidMgr& formbundleforbidmgr = FormBundleForbidMgr::GetInstance();
90 FormBundleForbidMgr bundleForbidMgr;
91 formbundleforbidmgr.Init();
92 std::string bundleName(data, size);
93 formbundleforbidmgr.IsBundleForbidden(bundleName);
94 bool isForbidden = *data % ENABLE;
95 formbundleforbidmgr.SetBundleForbiddenStatus(bundleName, isForbidden);
96 return true;
97 }
98
DoSomethingInterestingWithMyAPI5(const char * data,size_t size)99 bool DoSomethingInterestingWithMyAPI5(const char* data, size_t size)
100 {
101 FormBundleForbidMgr& formbundleforbidmgr = FormBundleForbidMgr::GetInstance();
102 FormBundleForbidMgr bundleForbidMgr;
103 formbundleforbidmgr.Init();
104 std::string bundleName(data, size);
105 formbundleforbidmgr.IsBundleForbidden(bundleName);
106 bool isForbidden = *data % ENABLE;
107 formbundleforbidmgr.SetBundleForbiddenStatus(bundleName, isForbidden);
108 return true;
109 }
110
DoSomethingInterestingWithMyAPI6(const char * data,size_t size)111 bool DoSomethingInterestingWithMyAPI6(const char* data, size_t size)
112 {
113 FormBundleForbidMgr& formbundleforbidmgr = FormBundleForbidMgr::GetInstance();
114 FormBundleForbidMgr bundleForbidMgr;
115 formbundleforbidmgr.Init();
116 std::string bundleName(data, size);
117 formbundleforbidmgr.IsBundleForbidden(bundleName);
118 bool isForbidden = *data % ENABLE;
119 formbundleforbidmgr.SetBundleForbiddenStatus(bundleName, isForbidden);
120 return true;
121 }
122
DoSomethingInterestingWithMyAPI7(const char * data,size_t size)123 bool DoSomethingInterestingWithMyAPI7(const char* data, size_t size)
124 {
125 FormBundleForbidMgr& formbundleforbidmgr = FormBundleForbidMgr::GetInstance();
126 FormBundleForbidMgr bundleForbidMgr;
127 formbundleforbidmgr.Init();
128 std::string bundleName(data, size);
129 formbundleforbidmgr.IsBundleForbidden(bundleName);
130 bool isForbidden = *data % ENABLE;
131 formbundleforbidmgr.SetBundleForbiddenStatus(bundleName, isForbidden);
132 return true;
133 }
134 }
135
136 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)137 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
138 {
139 /* Run your code on data */
140 if (data == nullptr) {
141 return 0;
142 }
143
144 if (size < OHOS::U32_AT_SIZE) {
145 return 0;
146 }
147
148 char* ch = static_cast<char*>(malloc(size + 1));
149 if (ch == nullptr) {
150 return 0;
151 }
152
153 (void)memset_s(ch, size + 1, 0x00, size + 1);
154 if (memcpy_s(ch, size + 1, data, size) != EOK) {
155 free(ch);
156 ch = nullptr;
157 return 0;
158 }
159
160 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
161 free(ch);
162 ch = nullptr;
163 return 0;
164 }
165
166