• 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 "screenlockservice_fuzzer.h"
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <string_ex.h>
22 
23 #include "message_parcel.h"
24 #include "screenlock_callback.h"
25 #include "screenlock_manager_interface.h"
26 #include "screenlock_system_ability.h"
27 
28 using namespace OHOS::ScreenLock;
29 using namespace OHOS::Rosen;
30 
31 namespace OHOS {
32 constexpr size_t THRESHOLD = 14;
33 constexpr int32_t OFFSET = 4;
34 const std::u16string SCREENLOCK_SYSTEMABILITY_INTERFACE_TOKEN = u"OHOS.ScreenLock.ScreenLockSystemAbilityInterface";
35 const std::u16string SCREENLOCK_MANAGER_INTERFACE_TOKEN = u"ohos.screenlock.ScreenLockManagerInterface";
36 
ConvertToUint32(const uint8_t * ptr)37 uint32_t ConvertToUint32(const uint8_t *ptr)
38 {
39     if (ptr == nullptr) {
40         return 0;
41     }
42     uint32_t bigvar = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
43     return bigvar;
44 }
45 
FuzzScreenlockCallback(const uint8_t * rawData,size_t size)46 bool FuzzScreenlockCallback(const uint8_t *rawData, size_t size)
47 {
48     uint32_t code = ConvertToUint32(rawData);
49     rawData = rawData + OFFSET;
50     size = size - OFFSET;
51 
52     EventListener mEventListener;
53     MessageParcel data;
54     data.WriteInterfaceToken(SCREENLOCK_SYSTEMABILITY_INTERFACE_TOKEN);
55     data.WriteBuffer(rawData, size);
56     data.RewindRead(0);
57     MessageParcel reply;
58     MessageOption option;
59 
60     sptr<ScreenlockCallback> mScreenlock = new ScreenlockCallback(mEventListener);
61     mScreenlock->OnRemoteRequest(code, data, reply, option);
62 
63     return true;
64 }
65 
FuzzScreenlockService(const uint8_t * rawData,size_t size)66 bool FuzzScreenlockService(const uint8_t *rawData, size_t size)
67 {
68     uint32_t code = ConvertToUint32(rawData);
69     rawData = rawData + OFFSET;
70     size = size - OFFSET;
71 
72     MessageParcel data;
73     data.WriteInterfaceToken(SCREENLOCK_MANAGER_INTERFACE_TOKEN);
74     data.WriteBuffer(rawData, size);
75     data.RewindRead(0);
76     MessageParcel reply;
77     MessageOption option;
78 
79     ScreenLockSystemAbility::GetInstance()->OnRemoteRequest(code, data, reply, option);
80 
81     return true;
82 }
83 
FuzzScreenlockDisplayPowerEvent(const uint8_t * rawData,size_t size)84 bool FuzzScreenlockDisplayPowerEvent(const uint8_t *rawData, size_t size)
85 {
86     uint32_t event = ConvertToUint32(rawData);
87     rawData = rawData + OFFSET;
88     uint32_t status = ConvertToUint32(rawData);
89 
90     sptr<ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener> displayPowerEventListener_;
91     displayPowerEventListener_ = new ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener();
92     displayPowerEventListener_->OnDisplayPowerEvent(
93         static_cast<DisplayPowerEvent>(event), static_cast<EventStatus>(status));
94     return true;
95 }
96 
FuzzScreenlockDump(const uint8_t * rawData,size_t size)97 bool FuzzScreenlockDump(const uint8_t *rawData, size_t size)
98 {
99     std::vector<std::u16string> args;
100     std::string str(reinterpret_cast<const char *>(rawData), size);
101     args.push_back(Str8ToStr16(str));
102     int fd = 0;
103     ScreenLockSystemAbility::GetInstance()->Dump(fd, args);
104     return true;
105 }
106 } // namespace OHOS
107 
108 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)109 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
110 {
111     if (size < OHOS::THRESHOLD) {
112         return 0;
113     }
114 
115     /* Run your code on data */
116     OHOS::FuzzScreenlockCallback(data, size);
117     OHOS::FuzzScreenlockService(data, size);
118     OHOS::FuzzScreenlockDisplayPowerEvent(data, size);
119     OHOS::FuzzScreenlockDump(data, size);
120     return 0;
121 }