• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ipasteboard_service.h"
21 #include "loader.h"
22 #include "message_parcel.h"
23 #include "pasteboard_service.h"
24 
25 namespace {
26 using namespace OHOS;
27 using namespace OHOS::MiscServices;
28 
29 const std::u16string PASTEBOARDSERVICE_INTERFACE_TOKEN = u"OHOS.MiscServices.IPasteboardService";
30 const std::vector<IPasteboardServiceIpcCode> CODE_LIST = {
31     IPasteboardServiceIpcCode::COMMAND_GET_PASTE_DATA,
32     IPasteboardServiceIpcCode::COMMAND_HAS_PASTE_DATA,
33     IPasteboardServiceIpcCode::COMMAND_SET_PASTE_DATA,
34     IPasteboardServiceIpcCode::COMMAND_SET_PASTE_DATA_ONLY,
35     IPasteboardServiceIpcCode::COMMAND_SET_PASTE_DATA_DELAY_DATA,
36     IPasteboardServiceIpcCode::COMMAND_SET_PASTE_DATA_ENTRY_DATA,
37     IPasteboardServiceIpcCode::COMMAND_CLEAR,
38     IPasteboardServiceIpcCode::COMMAND_SUBSCRIBE_OBSERVER,
39     IPasteboardServiceIpcCode::COMMAND_RESUBSCRIBE_OBSERVER,
40     IPasteboardServiceIpcCode::COMMAND_UNSUBSCRIBE_OBSERVER,
41     IPasteboardServiceIpcCode::COMMAND_UNSUBSCRIBE_ALL_OBSERVER,
42     IPasteboardServiceIpcCode::COMMAND_SUBSCRIBE_DISPOSABLE_OBSERVER,
43     IPasteboardServiceIpcCode::COMMAND_IS_REMOTE_DATA,
44     IPasteboardServiceIpcCode::COMMAND_GET_DATA_SOURCE,
45     IPasteboardServiceIpcCode::COMMAND_HAS_DATA_TYPE,
46     IPasteboardServiceIpcCode::COMMAND_SET_GLOBAL_SHARE_OPTION,
47     IPasteboardServiceIpcCode::COMMAND_REMOVE_GLOBAL_SHARE_OPTION,
48     IPasteboardServiceIpcCode::COMMAND_GET_GLOBAL_SHARE_OPTION,
49     IPasteboardServiceIpcCode::COMMAND_SET_APP_SHARE_OPTIONS,
50     IPasteboardServiceIpcCode::COMMAND_REMOVE_APP_SHARE_OPTIONS,
51     IPasteboardServiceIpcCode::COMMAND_PASTE_START,
52     IPasteboardServiceIpcCode::COMMAND_PASTE_COMPLETE,
53     IPasteboardServiceIpcCode::COMMAND_REGISTER_CLIENT_DEATH_OBSERVER,
54     IPasteboardServiceIpcCode::COMMAND_DETECT_PATTERNS,
55     IPasteboardServiceIpcCode::COMMAND_GET_RECORD_VALUE_BY_TYPE,
56     IPasteboardServiceIpcCode::COMMAND_GET_MIME_TYPES,
57     IPasteboardServiceIpcCode::COMMAND_SHOW_PROGRESS,
58     IPasteboardServiceIpcCode::COMMAND_GET_CHANGE_COUNT,
59     IPasteboardServiceIpcCode::COMMAND_SUBSCRIBE_ENTITY_OBSERVER,
60     IPasteboardServiceIpcCode::COMMAND_UNSUBSCRIBE_ENTITY_OBSERVER,
61 };
62 
63 class PasteboardServiceMock : public PasteboardServiceStub {
64 public:
CallbackEnter(uint32_t code)65     int32_t CallbackEnter(uint32_t code) override
66     {
67         (void)code;
68         return 0;
69     }
70 
CallbackExit(uint32_t code,int32_t result)71     int32_t CallbackExit(uint32_t code, int32_t result) override
72     {
73         (void)code;
74         (void)result;
75         return 0;
76     }
77 
SetPasteData(int fd,int64_t memSize,const std::vector<uint8_t> & buffer,const sptr<IPasteboardDelayGetter> & delayGetter,const sptr<IPasteboardEntryGetter> & entryGetter)78     int32_t SetPasteData(int fd, int64_t memSize, const std::vector<uint8_t> &buffer,
79         const sptr<IPasteboardDelayGetter> &delayGetter, const sptr<IPasteboardEntryGetter> &entryGetter) override
80     {
81         (void)fd;
82         (void)memSize;
83         (void)buffer;
84         (void)delayGetter;
85         (void)entryGetter;
86         return 0;
87     }
88 
SetPasteDataDelayData(int fd,int64_t memSize,const std::vector<uint8_t> & buffer,const sptr<IPasteboardDelayGetter> & delayGetter)89     int32_t SetPasteDataDelayData(int fd, int64_t memSize, const std::vector<uint8_t> &buffer,
90         const sptr<IPasteboardDelayGetter> &delayGetter) override
91     {
92         (void)fd;
93         (void)memSize;
94         (void)buffer;
95         (void)delayGetter;
96         return 0;
97     }
98 
SetPasteDataEntryData(int fd,int64_t memSize,const std::vector<uint8_t> & buffer,const sptr<IPasteboardEntryGetter> & entryGetter)99     int32_t SetPasteDataEntryData(int fd, int64_t memSize, const std::vector<uint8_t> &buffer,
100         const sptr<IPasteboardEntryGetter> &entryGetter) override
101     {
102         (void)fd;
103         (void)memSize;
104         (void)buffer;
105         (void)entryGetter;
106         return 0;
107     }
108 
SetPasteDataOnly(int fd,int64_t memSize,const std::vector<uint8_t> & buffer)109     int32_t SetPasteDataOnly(int fd, int64_t memSize, const std::vector<uint8_t> &buffer) override
110     {
111         (void)fd;
112         (void)memSize;
113         (void)buffer;
114         return 0;
115     }
116 
GetPasteData(int & fd,int64_t & memSize,std::vector<uint8_t> & buffer,const std::string & pasteId,int32_t & syncTime)117     int32_t GetPasteData(int &fd, int64_t &memSize, std::vector<uint8_t> &buffer,
118         const std::string &pasteId, int32_t &syncTime) override
119     {
120         (void)fd;
121         (void)memSize;
122         (void)buffer;
123         (void)pasteId;
124         (void)syncTime;
125         return 0;
126     }
127 
GetRecordValueByType(uint32_t dataId,uint32_t recordId,int64_t & rawDataSize,std::vector<uint8_t> & buffer,int & fd)128     int32_t GetRecordValueByType(uint32_t dataId, uint32_t recordId, int64_t &rawDataSize,
129         std::vector<uint8_t> &buffer, int &fd) override
130     {
131         (void)dataId;
132         (void)recordId;
133         (void)rawDataSize;
134         (void)buffer;
135         (void)fd;
136         return 0;
137     }
138 
GetDataSource(std::string & bundleName)139     int32_t GetDataSource(std::string &bundleName) override
140     {
141         (void)bundleName;
142         return 0;
143     }
144 
GetGlobalShareOption(const std::vector<uint32_t> & tokenIds,std::unordered_map<uint32_t,int32_t> & funcResult)145     int32_t GetGlobalShareOption(const std::vector<uint32_t> &tokenIds,
146         std::unordered_map<uint32_t, int32_t>& funcResult) override
147     {
148         (void)tokenIds;
149         (void)funcResult;
150         return 0;
151     }
152 
SetGlobalShareOption(const std::unordered_map<uint32_t,int32_t> & globalShareOptions)153     int32_t SetGlobalShareOption(const std::unordered_map<uint32_t, int32_t> &globalShareOptions) override
154     {
155         (void)globalShareOptions;
156         return 0;
157     }
158 
RemoveGlobalShareOption(const std::vector<uint32_t> & tokenIds)159     int32_t RemoveGlobalShareOption(const std::vector<uint32_t> &tokenIds) override
160     {
161         (void)tokenIds;
162         return 0;
163     }
164 
SetAppShareOptions(int32_t shareOptions)165     int32_t SetAppShareOptions(int32_t shareOptions) override
166     {
167         (void)shareOptions;
168         return 0;
169     }
170 
RemoveAppShareOptions()171     int32_t RemoveAppShareOptions() override
172     {
173         return 0;
174     }
175 
RegisterClientDeathObserver(const sptr<IRemoteObject> & observer)176     int32_t RegisterClientDeathObserver(const sptr<IRemoteObject> &observer) override
177     {
178         (void)observer;
179         return 0;
180     }
181 
HasPasteData(bool & funcResult)182     int32_t HasPasteData(bool& funcResult) override
183     {
184         (void)funcResult;
185         return 0;
186     }
187 
HasDataType(const std::string & mimeType,bool & funcResult)188     int32_t HasDataType(const std::string &mimeType, bool& funcResult) override
189     {
190         (void)mimeType;
191         (void)funcResult;
192         return 0;
193     }
194 
GetMimeTypes(std::vector<std::string> & funcResult)195     int32_t GetMimeTypes(std::vector<std::string>& funcResult) override
196     {
197         (void)funcResult;
198         return 0;
199     }
200 
GetChangeCount(uint32_t & changeCount)201     int32_t GetChangeCount(uint32_t &changeCount) override
202     {
203         return 0;
204     }
205 
SubscribeEntityObserver(EntityType entityType,uint32_t expectedDataLength,const sptr<IEntityRecognitionObserver> & observer)206     int32_t SubscribeEntityObserver(
207         EntityType entityType, uint32_t expectedDataLength, const sptr<IEntityRecognitionObserver> &observer) override
208     {
209         (void)entityType;
210         (void)expectedDataLength;
211         (void)observer;
212         return 0;
213     }
214 
UnsubscribeEntityObserver(EntityType entityType,uint32_t expectedDataLength,const sptr<IEntityRecognitionObserver> & observer)215     int32_t UnsubscribeEntityObserver(
216         EntityType entityType, uint32_t expectedDataLength,  const sptr<IEntityRecognitionObserver> &observer) override
217     {
218         (void)entityType;
219         (void)expectedDataLength;
220         (void)observer;
221         return 0;
222     }
223 
IsRemoteData(bool & funcResult)224     int32_t IsRemoteData(bool& funcResult) override
225     {
226         (void)funcResult;
227         return 0;
228     }
229 
DetectPatterns(const std::vector<Pattern> & patternsToCheck,std::vector<Pattern> & funcResult)230     int32_t DetectPatterns(const std::vector<Pattern> &patternsToCheck,
231         std::vector<Pattern>& funcResult) override
232     {
233         (void)patternsToCheck;
234         (void)funcResult;
235         return 0;
236     }
237 
SubscribeObserver(PasteboardObserverType type,const sptr<IPasteboardChangedObserver> & observer)238     int32_t SubscribeObserver(PasteboardObserverType type, const sptr<IPasteboardChangedObserver> &observer) override
239     {
240         (void)type;
241         (void)observer;
242         return 0;
243     }
244 
ResubscribeObserver(PasteboardObserverType type,const sptr<IPasteboardChangedObserver> & observer)245     int32_t ResubscribeObserver(PasteboardObserverType type, const sptr<IPasteboardChangedObserver> &observer) override
246     {
247         (void)type;
248         (void)observer;
249         return 0;
250     }
251 
UnsubscribeObserver(PasteboardObserverType type,const sptr<IPasteboardChangedObserver> & observer)252     int32_t UnsubscribeObserver(PasteboardObserverType type, const sptr<IPasteboardChangedObserver> &observer) override
253     {
254         (void)type;
255         (void)observer;
256         return 0;
257     }
258 
UnsubscribeAllObserver(PasteboardObserverType type)259     int32_t UnsubscribeAllObserver(PasteboardObserverType type) override
260     {
261         (void)type;
262         return 0;
263     }
264 
PasteStart(const std::string & pasteId)265     int32_t PasteStart(const std::string &pasteId) override
266     {
267         (void)pasteId;
268         return 0;
269     }
270 
PasteComplete(const std::string & deviceId,const std::string & pasteId)271     int32_t PasteComplete(const std::string &deviceId, const std::string &pasteId) override
272     {
273         (void)deviceId;
274         (void)pasteId;
275         return 0;
276     }
277 
Clear()278     int32_t Clear() override
279     {
280         return 0;
281     }
282 
ShowProgress(const std::string & progressKey,const sptr<IRemoteObject> & observer)283     int32_t ShowProgress(const std::string &progressKey, const sptr<IRemoteObject> &observer) override
284     {
285         (void)progressKey;
286         (void)observer;
287         return 0;
288     }
289 
SubscribeDisposableObserver(const sptr<IPasteboardDisposableObserver> & observer,const std::string & targetBundleName,DisposableType type,uint32_t maxLength)290     int32_t SubscribeDisposableObserver(const sptr<IPasteboardDisposableObserver> &observer,
291         const std::string &targetBundleName, DisposableType type, uint32_t maxLength) override
292     {
293         (void)observer;
294         (void)targetBundleName;
295         (void)type;
296         (void)maxLength;
297         return 0;
298     }
299 };
300 
DoSleep(void)301 static inline void DoSleep(void)
302 {
303     constexpr uint32_t SLEEP_MS = 10;
304     std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_MS));
305 }
306 
307 class TestEnv {
308 public:
TestEnv()309     TestEnv()
310     {
311         isInited_ = false;
312         stub_ = new PasteboardServiceMock();
313         if (stub_ == nullptr) {
314             return;
315         }
316         isInited_ = true;
317     }
318 
~TestEnv()319     ~TestEnv()
320     {
321         isInited_ = false;
322         stub_ = nullptr;
323     }
324 
IsInited() const325     bool IsInited() const noexcept
326     {
327         return isInited_;
328     }
329 
DoRemoteRequest(IPasteboardServiceIpcCode code,MessageParcel & data)330     void DoRemoteRequest(IPasteboardServiceIpcCode code, MessageParcel &data)
331     {
332         MessageParcel reply;
333         MessageOption option;
334         if (stub_ != nullptr) {
335             stub_->OnRemoteRequest(static_cast<uint32_t>(code), data, reply, option);
336         }
337     }
338 
339 private:
340     volatile bool isInited_;
341     sptr<PasteboardServiceMock> stub_;
342 };
343 } // anonymous namespace
344 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)345 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
346 {
347     static TestEnv env;
348     if (!env.IsInited()) {
349         return 0;
350     }
351 
352     if (data == nullptr || size == 0) {
353         return 0;
354     }
355     IPasteboardServiceIpcCode code = CODE_LIST[data[0] % CODE_LIST.size()];
356 
357     OHOS::MessageParcel parcel;
358     parcel.WriteInterfaceToken(PASTEBOARDSERVICE_INTERFACE_TOKEN);
359     parcel.WriteBuffer(data + 1, size - 1);
360 
361     env.DoRemoteRequest(code, parcel);
362     DoSleep();
363     return 0;
364 }
365