1 /*
2 * Copyright (c) 2021-2022 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 * miscservices under the License is miscservices 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 "screenlockmanager_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "message_parcel.h"
22 #include "screenlock_callback.h"
23 #include "screenlock_manager.h"
24 #include "screenlock_manager_interface.h"
25 #include "screenlock_system_ability.h"
26 #include "screenlock_system_ability_callback.h"
27
28 using namespace OHOS::ScreenLock;
29
30 namespace OHOS {
31 constexpr size_t THRESHOLD = 10;
32 constexpr int32_t OFFSET = 4;
33 constexpr size_t LENGTH = 1;
34 constexpr size_t RANDNUM_ZERO = 0;
35 constexpr size_t RANDNUM_ONE = 1;
36 constexpr size_t RANDNUM_TWO = 2;
37
ConvertToUint32(const uint8_t * ptr)38 uint32_t ConvertToUint32(const uint8_t *ptr)
39 {
40 if (ptr == nullptr) {
41 return 0;
42 }
43 uint32_t bigvar = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
44 return bigvar;
45 }
46
FuzzScreenlockManager(const uint8_t * rawData,size_t size)47 bool FuzzScreenlockManager(const uint8_t *rawData, size_t size)
48 {
49 uint32_t code = ConvertToUint32(rawData);
50 EventListener eventListener;
51 if (code == RANDNUM_ZERO) {
52 return ScreenLockManager::GetInstance()->IsScreenLocked();
53 }
54 if (code == RANDNUM_ONE) {
55 return ScreenLockManager::GetInstance()->GetSecure();
56 }
57 if (code == RANDNUM_TWO) {
58 sptr<ScreenlockCallback> listener_ = new ScreenlockCallback(eventListener);
59 int32_t ret = ScreenLockManager::GetInstance()->Lock(listener_);
60 return ret == E_SCREENLOCK_OK;
61 }
62 return true;
63 }
64
UnlockFuzzTest(const uint8_t * rawData,size_t size)65 bool UnlockFuzzTest(const uint8_t *rawData, size_t size)
66 {
67 EventListener eventListener;
68 sptr<ScreenlockCallback> listener_ = new ScreenlockCallback(eventListener);
69 if (size < LENGTH) {
70 return true;
71 }
72 int32_t ret = ScreenLockManager::GetInstance()->Unlock(static_cast<Action>(rawData[0] % 3), listener_);
73 return ret == E_SCREENLOCK_OK;
74 }
75
IsLockedFuzzTest(const uint8_t * rawData,size_t size)76 bool IsLockedFuzzTest(const uint8_t *rawData, size_t size)
77 {
78 if (size < LENGTH) {
79 return true;
80 }
81 bool isLocked = static_cast<bool>(rawData[0] % 2);
82 int32_t ret = ScreenLockManager::GetInstance()->IsLocked(isLocked);
83 return ret == E_SCREENLOCK_OK;
84 }
85
FuzzScreenlockAppManager(const uint8_t * rawData,size_t size)86 bool FuzzScreenlockAppManager(const uint8_t *rawData, size_t size)
87 {
88 uint32_t code = ConvertToUint32(rawData);
89 rawData = rawData + OFFSET;
90 size = size - OFFSET;
91 EventListener eventListener;
92 if (code == RANDNUM_ZERO) {
93 sptr<ScreenlockSystemAbilityCallback> listener_ = new ScreenlockSystemAbilityCallback(eventListener);
94 int32_t ret = ScreenLockManager::GetInstance()->OnSystemEvent(listener_);
95 return ret == E_SCREENLOCK_OK;
96 }
97 if (code == RANDNUM_ONE) {
98 int param = 0;
99 std::string event(reinterpret_cast<const char *>(rawData), size);
100 int32_t ret = ScreenLockManager::GetInstance()->SendScreenLockEvent(event, param);
101 return ret == E_SCREENLOCK_OK;
102 }
103 return true;
104 }
105
FuzzScreenlockIsDisabled(const uint8_t * rawData,size_t size)106 bool FuzzScreenlockIsDisabled(const uint8_t *rawData, size_t size)
107 {
108 if (size < LENGTH) {
109 return true;
110 }
111 int32_t userId = 100;
112 bool isDisabled = static_cast<bool>(rawData[0] % 2);
113 int32_t ret = ScreenLockManager::GetInstance()->IsScreenLockDisabled(userId, isDisabled);
114 return ret == E_SCREENLOCK_OK;
115 }
116
FuzzScreenlockSetDisabled(const uint8_t * rawData,size_t size)117 bool FuzzScreenlockSetDisabled(const uint8_t *rawData, size_t size)
118 {
119 if (size < LENGTH) {
120 return true;
121 }
122 int32_t userId = 100;
123 bool isDisabled = static_cast<bool>(rawData[0] % 2);
124 int32_t ret = ScreenLockManager::GetInstance()->SetScreenLockDisabled(isDisabled, userId);
125 return ret == E_SCREENLOCK_OK;
126 }
127
FuzzScreenlockSetAuthState(const uint8_t * rawData,size_t size)128 bool FuzzScreenlockSetAuthState(const uint8_t *rawData, size_t size)
129 {
130 if (size < LENGTH) {
131 return true;
132 }
133 int32_t userId = 100;
134 int32_t authState = 2;
135 std::string authToken = "test";
136 int32_t ret = ScreenLockManager::GetInstance()->SetScreenLockAuthState(authState, userId, authToken);
137 return ret == E_SCREENLOCK_OK;
138 }
139
FuzzScreenlockGetAuthState(const uint8_t * rawData,size_t size)140 bool FuzzScreenlockGetAuthState(const uint8_t *rawData, size_t size)
141 {
142 if (size < LENGTH) {
143 return true;
144 }
145 int32_t userId = 100;
146 int32_t authState = 2;
147 int32_t ret = ScreenLockManager::GetInstance()->GetScreenLockAuthState(userId, authState);
148 return ret == E_SCREENLOCK_OK;
149 }
150
FuzzScreenlockRequestStrongAuth(const uint8_t * rawData,size_t size)151 bool FuzzScreenlockRequestStrongAuth(const uint8_t *rawData, size_t size)
152 {
153 if (size < LENGTH) {
154 return true;
155 }
156 int32_t userId = 100;
157 int reasonFlag = 1;
158 int32_t ret = ScreenLockManager::GetInstance()->RequestStrongAuth(reasonFlag, userId);
159 return ret == E_SCREENLOCK_OK;
160 }
161
FuzzScreenlockGetStrongAuth(const uint8_t * rawData,size_t size)162 bool FuzzScreenlockGetStrongAuth(const uint8_t *rawData, size_t size)
163 {
164 if (size < LENGTH) {
165 return true;
166 }
167 int32_t userId = 100;
168 int reasonFlag = 1;
169 int32_t ret = ScreenLockManager::GetInstance()->GetStrongAuth(userId, reasonFlag);
170 return ret == E_SCREENLOCK_OK;
171 }
172
173 } // namespace OHOS
174
175 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)176 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
177 {
178 if (size < OHOS::THRESHOLD) {
179 return 0;
180 }
181
182 /* Run your code on data */
183 OHOS::FuzzScreenlockManager(data, size);
184 OHOS::UnlockFuzzTest(data, size);
185 OHOS::IsLockedFuzzTest(data, size);
186 OHOS::FuzzScreenlockAppManager(data, size);
187 OHOS::FuzzScreenlockIsDisabled(data, size);
188 OHOS::FuzzScreenlockSetDisabled(data, size);
189 OHOS::FuzzScreenlockSetAuthState(data, size);
190 OHOS::FuzzScreenlockGetAuthState(data, size);
191 OHOS::FuzzScreenlockRequestStrongAuth(data, size);
192 OHOS::FuzzScreenlockGetStrongAuth(data, size);
193 return 0;
194 }