• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 <cstddef>
19 #include <cstdint>
20 #include <map>
21 
22 #include "copy_uri_handler.h"
23 #include "i_pasteboard_service.h"
24 #include "message_parcel.h"
25 #include "paste_data.h"
26 #include "pasteboard_service.h"
27 #include "pasteboard_serv_ipc_interface_code.h"
28 
29 using namespace OHOS::Security::PasteboardServ;
30 using namespace OHOS::MiscServices;
31 
32 namespace OHOS {
33 constexpr size_t THRESHOLD = 10;
34 constexpr int32_t OFFSET = 4;
35 constexpr size_t RANDNUM_ZERO = 0;
36 constexpr size_t RANDNUM_ONE = 1;
37 const std::u16string PASTEBOARDSERVICE_INTERFACE_TOKEN = u"ohos.miscservices.pasteboard.IPasteboardService";
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 
FuzzPasteboardService(const uint8_t * rawData,size_t size)48 bool FuzzPasteboardService(const uint8_t *rawData, size_t size)
49 {
50     uint32_t code = ConvertToUint32(rawData);
51     rawData = rawData + OFFSET;
52     size = size - OFFSET;
53 
54     MessageParcel data;
55     data.WriteInterfaceToken(PASTEBOARDSERVICE_INTERFACE_TOKEN);
56     data.WriteBuffer(rawData, size);
57     data.RewindRead(0);
58     MessageParcel reply;
59     MessageOption option;
60 
61     std::make_shared<PasteboardService>()->OnRemoteRequest(code, data, reply, option);
62 
63     return true;
64 }
65 
FuzzPasteboardServiceOnSetPasteData(const uint8_t * rawData,size_t size)66 bool FuzzPasteboardServiceOnSetPasteData(const uint8_t *rawData, size_t size)
67 {
68     PasteData pasteData;
69     uint32_t code = ConvertToUint32(rawData);
70     rawData = rawData + OFFSET;
71     size = size - OFFSET;
72     std::string str(reinterpret_cast<const char *>(rawData), size);
73     switch (code) {
74         case RANDNUM_ZERO:
75             pasteData.AddTextRecord(str);
76             break;
77         case RANDNUM_ONE:
78             pasteData.AddHtmlRecord(str);
79             break;
80         default:
81             pasteData.AddUriRecord(Uri(str));
82             break;
83     }
84 
85     MessageParcel data;
86     MessageParcel reply;
87     MessageOption option;
88     data.WriteInterfaceToken(PASTEBOARDSERVICE_INTERFACE_TOKEN);
89     std::vector<uint8_t> pasteDataTlv(0);
90     pasteData.Encode(pasteDataTlv);
91     data.WriteInt32(pasteDataTlv.size());
92     data.WriteRawData(pasteDataTlv.data(), pasteDataTlv.size());
93 
94     CopyUriHandler copyHandler;
95     pasteData.WriteUriFd(data, copyHandler);
96 
97     std::make_shared<PasteboardService>()->OnRemoteRequest(
98         static_cast<uint32_t>(PasteboardServiceInterfaceCode::SET_PASTE_DATA), data, reply, option);
99 
100     return true;
101 }
102 } // namespace OHOS
103 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)104 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
105 {
106     if (size < OHOS::THRESHOLD) {
107         return 0;
108     }
109     /* Run your code on data */
110     OHOS::FuzzPasteboardService(data, size);
111     OHOS::FuzzPasteboardServiceOnSetPasteData(data, size);
112     return 0;
113 }
114