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