1 /*
2 * Copyright (c) 2022-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 "pasteboardservice_fuzzer.h"
17
18 #include <thread>
19
20 #include "loader.h"
21 #include "message_parcel.h"
22 #include "pasteboard_service.h"
23 #include "pasteboard_serv_ipc_interface_code.h"
24
25 namespace {
26 using namespace OHOS;
27 using namespace OHOS::MiscServices;
28 using namespace OHOS::Security::PasteboardServ;
29
30 const std::u16string PASTEBOARDSERVICE_INTERFACE_TOKEN = u"ohos.miscservices.pasteboard.IPasteboardService";
31 const std::vector<PasteboardServiceInterfaceCode> CODE_LIST = {
32 GET_PASTE_DATA,
33 HAS_PASTE_DATA,
34 SET_PASTE_DATA,
35 CLEAR_ALL,
36 SUBSCRIBE_OBSERVER,
37 UNSUBSCRIBE_OBSERVER,
38 UNSUBSCRIBE_ALL_OBSERVER,
39 IS_REMOTE_DATA,
40 GET_DATA_SOURCE,
41 HAS_DATA_TYPE,
42 SET_GLOBAL_SHARE_OPTION,
43 REMOVE_GLOBAL_SHARE_OPTION,
44 GET_GLOBAL_SHARE_OPTION,
45 SET_APP_SHARE_OPTIONS,
46 REMOVE_APP_SHARE_OPTIONS,
47 PASTE_START,
48 PASTE_COMPLETE,
49 REGISTER_CLIENT_DEATH_OBSERVER,
50 DETECT_PATTERNS,
51 GET_RECORD_VALUE,
52 GET_MIME_TYPES,
53 GET_REMOTE_DEVICE_NAME,
54 SHOW_PROGRESS,
55 GET_CHANGE_COUNT,
56 SUBSCRIBE_ENTITY_OBSERVER,
57 UNSUBSCRIBE_ENTITY_OBSERVER,
58 };
59
60 class PasteboardServiceMock : public PasteboardServiceStub {
61 public:
SetPasteData(PasteData & pasteData,const sptr<IPasteboardDelayGetter> delayGetter=nullptr,const sptr<IPasteboardEntryGetter> entryGetter=nullptr)62 int32_t SetPasteData(PasteData &pasteData, const sptr<IPasteboardDelayGetter> delayGetter = nullptr,
63 const sptr<IPasteboardEntryGetter> entryGetter = nullptr) override
64 {
65 (void)pasteData;
66 (void)delayGetter;
67 (void)entryGetter;
68 return 0;
69 }
70
GetPasteData(PasteData & data,int32_t & syncTime)71 int32_t GetPasteData(PasteData &data, int32_t &syncTime) override
72 {
73 (void)data;
74 (void)syncTime;
75 return 0;
76 }
77
GetRecordValueByType(uint32_t dataId,uint32_t recordId,PasteDataEntry & value)78 int32_t GetRecordValueByType(uint32_t dataId, uint32_t recordId, PasteDataEntry &value) override
79 {
80 (void)dataId;
81 (void)recordId;
82 (void)value;
83 return 0;
84 }
85
GetDataSource(std::string & bundleName)86 int32_t GetDataSource(std::string &bundleName) override
87 {
88 (void)bundleName;
89 return 0;
90 }
91
GetGlobalShareOption(const std::vector<uint32_t> & tokenIds)92 std::map<uint32_t, ShareOption> GetGlobalShareOption(const std::vector<uint32_t> &tokenIds) override
93 {
94 (void)tokenIds;
95 return {};
96 }
97
SetGlobalShareOption(const std::map<uint32_t,ShareOption> & globalShareOptions)98 int32_t SetGlobalShareOption(const std::map<uint32_t, ShareOption> &globalShareOptions) override
99 {
100 (void)globalShareOptions;
101 return 0;
102 }
103
RemoveGlobalShareOption(const std::vector<uint32_t> & tokenIds)104 int32_t RemoveGlobalShareOption(const std::vector<uint32_t> &tokenIds) override
105 {
106 (void)tokenIds;
107 return 0;
108 }
109
SetAppShareOptions(const ShareOption & shareOptions)110 int32_t SetAppShareOptions(const ShareOption &shareOptions) override
111 {
112 (void)shareOptions;
113 return 0;
114 }
115
RemoveAppShareOptions()116 int32_t RemoveAppShareOptions() override
117 {
118 return 0;
119 }
120
RegisterClientDeathObserver(sptr<IRemoteObject> observer)121 int32_t RegisterClientDeathObserver(sptr<IRemoteObject> observer) override
122 {
123 (void)observer;
124 return 0;
125 }
126
HasPasteData()127 bool HasPasteData() override
128 {
129 return true;
130 }
131
HasDataType(const std::string & mimeType)132 bool HasDataType(const std::string &mimeType) override
133 {
134 (void)mimeType;
135 return true;
136 }
137
GetMimeTypes()138 std::vector<std::string> GetMimeTypes() override
139 {
140 return {};
141 }
142
GetChangeCount(uint32_t & changeCount)143 int32_t GetChangeCount(uint32_t &changeCount) override
144 {
145 return 0;
146 }
147
SubscribeEntityObserver(EntityType entityType,uint32_t expectedDataLength,const sptr<IEntityRecognitionObserver> & observer)148 int32_t SubscribeEntityObserver(
149 EntityType entityType, uint32_t expectedDataLength, const sptr<IEntityRecognitionObserver> &observer) override
150 {
151 (void)entityType;
152 (void)expectedDataLength;
153 (void)observer;
154 return 0;
155 }
156
UnsubscribeEntityObserver(EntityType entityType,uint32_t expectedDataLength,const sptr<IEntityRecognitionObserver> & observer)157 int32_t UnsubscribeEntityObserver(
158 EntityType entityType, uint32_t expectedDataLength, const sptr<IEntityRecognitionObserver> &observer) override
159 {
160 (void)entityType;
161 (void)expectedDataLength;
162 (void)observer;
163 return 0;
164 }
165
IsRemoteData()166 bool IsRemoteData() override
167 {
168 return true;
169 }
170
DetectPatterns(const std::set<Pattern> & patternsToCheck)171 std::set<Pattern> DetectPatterns(const std::set<Pattern> &patternsToCheck) override
172 {
173 (void)patternsToCheck;
174 return {};
175 }
176
SubscribeObserver(PasteboardObserverType type,const sptr<IPasteboardChangedObserver> & observer)177 void SubscribeObserver(PasteboardObserverType type, const sptr<IPasteboardChangedObserver> &observer) override
178 {
179 (void)type;
180 (void)observer;
181 }
182
UnsubscribeObserver(PasteboardObserverType type,const sptr<IPasteboardChangedObserver> & observer)183 void UnsubscribeObserver(PasteboardObserverType type, const sptr<IPasteboardChangedObserver> &observer) override
184 {
185 (void)type;
186 (void)observer;
187 }
188
UnsubscribeAllObserver(PasteboardObserverType type)189 void UnsubscribeAllObserver(PasteboardObserverType type) override
190 {
191 (void)type;
192 }
193
PasteStart(const std::string & pasteId)194 void PasteStart(const std::string &pasteId) override
195 {
196 (void)pasteId;
197 }
198
PasteComplete(const std::string & deviceId,const std::string & pasteId)199 void PasteComplete(const std::string &deviceId, const std::string &pasteId) override
200 {
201 (void)deviceId;
202 (void)pasteId;
203 }
204
Clear()205 void Clear() override
206 {
207 }
208
GetRemoteDeviceName(std::string & deviceName,bool & isRemote)209 int32_t GetRemoteDeviceName(std::string &deviceName, bool &isRemote) override
210 {
211 (void)deviceName;
212 (void)isRemote;
213 return 0;
214 }
215
ShowProgress(const std::string & progressKey,const sptr<IRemoteObject> & observer)216 void ShowProgress(const std::string &progressKey, const sptr<IRemoteObject> &observer) override
217 {
218 (void)progressKey;
219 (void)observer;
220 }
221 };
222
DoSleep(void)223 static inline void DoSleep(void)
224 {
225 constexpr uint32_t SLEEP_MS = 10;
226 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_MS));
227 }
228
229 class TestEnv {
230 public:
TestEnv()231 TestEnv()
232 {
233 isInited_ = false;
234 stub_ = new PasteboardServiceMock();
235 if (stub_ == nullptr) {
236 return;
237 }
238 isInited_ = true;
239 }
240
~TestEnv()241 ~TestEnv()
242 {
243 isInited_ = false;
244 stub_ = nullptr;
245 }
246
IsInited() const247 bool IsInited() const noexcept
248 {
249 return isInited_;
250 }
251
DoRemoteRequest(PasteboardServiceInterfaceCode code,MessageParcel & data)252 void DoRemoteRequest(PasteboardServiceInterfaceCode code, MessageParcel &data)
253 {
254 MessageParcel reply;
255 MessageOption option;
256 if (stub_ != nullptr) {
257 stub_->OnRemoteRequest(static_cast<uint32_t>(code), data, reply, option);
258 }
259 }
260
261 private:
262 volatile bool isInited_;
263 sptr<PasteboardServiceMock> stub_;
264 };
265 } // anonymous namespace
266
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)267 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
268 {
269 static TestEnv env;
270 if (!env.IsInited()) {
271 return 0;
272 }
273
274 if (data == nullptr || size == 0) {
275 return 0;
276 }
277 PasteboardServiceInterfaceCode code = CODE_LIST[data[0] % CODE_LIST.size()];
278
279 OHOS::MessageParcel parcel;
280 parcel.WriteInterfaceToken(PASTEBOARDSERVICE_INTERFACE_TOKEN);
281 parcel.WriteBuffer(data + 1, size - 1);
282
283 env.DoRemoteRequest(code, parcel);
284 DoSleep();
285 return 0;
286 }
287