• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "socket_fuzzer.h"
17 #include <memory>
18 #include <string>
19 #include <securec.h>
20 #include "socket.h"
21 
22 namespace OHOS {
23 static std::string DEFAULT_SOCKET_NAME = "com.communication.fuzz.socketName";
24 static std::string DEFAULT_SOCKET_PEER_NAME = "com.communication.fuzz.peerName";
25 static std::string DEFAULT_SOCKET_PEER_NETWORKID =
26     "a8ynvpdaihw1f6nknjd2hkfhxljxypkr6kvjsbhnhpp16974uo4fvsrpfa6t50fm";
27 static std::string DEFAULT_SOCKET_PKG_NAME = "com.communication.fuzz.pkgName";
28 
SocketTestWithName(const uint8_t * data,size_t size)29 void SocketTestWithName(const uint8_t *data, size_t size)
30 {
31     if ((data == nullptr) || (size == 0)) {
32         return;
33     }
34 
35     const size_t bufSize = size + 1;
36     std::unique_ptr<char[]> socketName = std::make_unique<char[]>(bufSize);
37     if (socketName == nullptr) {
38         return;
39     }
40 
41     if (memset_s(socketName.get(), bufSize, 0, bufSize) != EOK) {
42         return;
43     }
44 
45     if (memcpy_s(socketName.get(), bufSize, data, size) != EOK) {
46         return;
47     }
48 
49     SocketInfo info = {
50         .name = socketName.get(),
51         .peerName = const_cast<char *>(DEFAULT_SOCKET_PEER_NAME.c_str()),
52         .peerNetworkId = const_cast<char *>(DEFAULT_SOCKET_PEER_NETWORKID.c_str()),
53         .pkgName = const_cast<char *>(DEFAULT_SOCKET_PKG_NAME.c_str()),
54         .dataType = DATA_TYPE_MESSAGE,
55     };
56 
57     (void)Socket(info);
58 }
59 
SocketTestWithPeerName(const uint8_t * data,size_t size)60 void SocketTestWithPeerName(const uint8_t *data, size_t size)
61 {
62     if ((data == nullptr) || (size == 0)) {
63         return;
64     }
65 
66     const size_t bufSize = size + 1;
67     std::unique_ptr<char[]> socketPeerName = std::make_unique<char[]>(bufSize);
68     if (socketPeerName == nullptr) {
69         return;
70     }
71 
72     if (memset_s(socketPeerName.get(), bufSize, 0, bufSize) != EOK) {
73         return;
74     }
75 
76     if (memcpy_s(socketPeerName.get(), bufSize, data, size) != EOK) {
77         return;
78     }
79 
80     SocketInfo info = {
81         .name = const_cast<char *>(DEFAULT_SOCKET_NAME.c_str()),
82         .peerName = socketPeerName.get(),
83         .peerNetworkId = const_cast<char *>(DEFAULT_SOCKET_PEER_NETWORKID.c_str()),
84         .pkgName = const_cast<char *>(DEFAULT_SOCKET_PKG_NAME.c_str()),
85         .dataType = DATA_TYPE_MESSAGE,
86     };
87 
88     (void)Socket(info);
89 }
90 
SocketTestWithNetworkId(const uint8_t * data,size_t size)91 void SocketTestWithNetworkId(const uint8_t *data, size_t size)
92 {
93     if ((data == nullptr) || (size == 0)) {
94         return;
95     }
96 
97     const size_t bufSize = size + 1;
98     std::unique_ptr<char[]> socketNetworkId = std::make_unique<char[]>(bufSize);
99     if (socketNetworkId == nullptr) {
100         return;
101     }
102 
103     if (memset_s(socketNetworkId.get(), bufSize, 0, bufSize) != EOK) {
104         return;
105     }
106 
107     if (memcpy_s(socketNetworkId.get(), bufSize, data, size) != EOK) {
108         return;
109     }
110 
111     SocketInfo info = {
112         .name = const_cast<char *>(DEFAULT_SOCKET_NAME.c_str()),
113         .peerName = const_cast<char *>(DEFAULT_SOCKET_PEER_NAME.c_str()),
114         .peerNetworkId = socketNetworkId.get(),
115         .pkgName = const_cast<char *>(DEFAULT_SOCKET_PKG_NAME.c_str()),
116         .dataType = DATA_TYPE_MESSAGE,
117     };
118 
119     (void)Socket(info);
120 }
121 
SocketTestWithPkgName(const uint8_t * data,size_t size)122 void SocketTestWithPkgName(const uint8_t *data, size_t size)
123 {
124     if ((data == nullptr) || (size == 0)) {
125         return;
126     }
127 
128     const size_t bufSize = size + 1;
129     std::unique_ptr<char[]> socketPkgName = std::make_unique<char[]>(bufSize);
130     if (socketPkgName == nullptr) {
131         return;
132     }
133 
134     if (memset_s(socketPkgName.get(), bufSize, 0, bufSize) != EOK) {
135         return;
136     }
137 
138     if (memcpy_s(socketPkgName.get(), bufSize, data, size) != EOK) {
139         return;
140     }
141 
142     SocketInfo info = {
143         .name = const_cast<char *>(DEFAULT_SOCKET_NAME.c_str()),
144         .peerName = const_cast<char *>(DEFAULT_SOCKET_PEER_NAME.c_str()),
145         .peerNetworkId = const_cast<char *>(DEFAULT_SOCKET_PKG_NAME.c_str()),
146         .pkgName = socketPkgName.get(),
147         .dataType = DATA_TYPE_MESSAGE,
148     };
149 
150     (void)Socket(info);
151 }
152 
SocketTestWithDataType(const uint8_t * data,size_t size)153 void SocketTestWithDataType(const uint8_t *data, size_t size)
154 {
155     if ((data == nullptr) || (size < sizeof(TransDataType))) {
156         return;
157     }
158 
159     TransDataType socketDataType = DATA_TYPE_BUTT;
160     if (memcpy_s(&socketDataType, sizeof(TransDataType), data, sizeof(TransDataType)) != EOK) {
161         return;
162     }
163 
164     SocketInfo info = {
165         .name = const_cast<char *>(DEFAULT_SOCKET_NAME.c_str()),
166         .peerName = const_cast<char *>(DEFAULT_SOCKET_PEER_NAME.c_str()),
167         .peerNetworkId = const_cast<char *>(DEFAULT_SOCKET_PKG_NAME.c_str()),
168         .pkgName = const_cast<char *>(DEFAULT_SOCKET_PKG_NAME.c_str()),
169         .dataType = socketDataType,
170     };
171 
172     (void)Socket(info);
173 }
174 } // namespace OHOS
175 
176 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)177 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
178 {
179     OHOS::SocketTestWithName(data, size);
180     OHOS::SocketTestWithPeerName(data, size);
181     OHOS::SocketTestWithNetworkId(data, size);
182     OHOS::SocketTestWithPkgName(data, size);
183     OHOS::SocketTestWithDataType(data, size);
184     return 0;
185 }
186