• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 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 * 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 
17 #include "screenlockmanager_fuzzer.h"
18 
19 #include <cstddef>
20 #include <cstdint>
21 
22 #include "message_parcel.h"
23 #include "screenlock_app_manager.h"
24 #include "screenlock_callback.h"
25 #include "screenlock_manager.h"
26 #include "screenlock_manager_interface.h"
27 #include "screenlock_system_ability.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 RANDNUM_ZERO = 0;
35 constexpr size_t RANDNUM_ONE = 1;
36 constexpr size_t RANDNUM_TWO = 2;
37 constexpr size_t RANDNUM_THREE = 3;
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     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<ScreenLockSystemAbilityInterface> listener_ = new ScreenLockSystemAbilityStub();
59         int32_t ret = ScreenLockManager::GetInstance()->Unlock(Action::UNLOCK, listener_);
60         return ret == E_SCREENLOCK_OK;
61     }
62     if (code == RANDNUM_THREE) {
63         sptr<ScreenLockSystemAbilityInterface> listener_ = new ScreenLockSystemAbilityStub();
64         int32_t ret = ScreenLockManager::GetInstance()->Lock(listener_);
65         return ret == E_SCREENLOCK_OK;
66     }
67     return true;
68 }
69 
FuzzScreenlockAppManager(const uint8_t * rawData,size_t size)70 bool FuzzScreenlockAppManager(const uint8_t *rawData, size_t size)
71 {
72     uint32_t code = ConvertToUint32(rawData);
73     rawData = rawData + OFFSET;
74     size = size - OFFSET;
75     if (code == RANDNUM_ZERO) {
76         sptr<ScreenLockSystemAbilityInterface> listener_ = new ScreenLockSystemAbilityStub();
77         int32_t ret = ScreenLockAppManager::GetInstance()->OnSystemEvent(listener_);
78         return ret == E_SCREENLOCK_OK;
79     }
80     if (code == RANDNUM_ONE) {
81         int param = 0;
82         std::string event(reinterpret_cast<const char *>(rawData), size);
83         int32_t ret = ScreenLockAppManager::GetInstance()->SendScreenLockEvent(event, param);
84         return ret == E_SCREENLOCK_OK;
85     }
86     return true;
87 }
88 } // namespace OHOS
89 
90 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)91 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
92 {
93     if (size < OHOS::THRESHOLD) {
94         return 0;
95     }
96 
97     /* Run your code on data */
98     OHOS::FuzzScreenlockManager(data, size);
99     OHOS::FuzzScreenlockAppManager(data, size);
100     return 0;
101 }