• 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 (memset_s(socketName.get(), bufSize, 0, bufSize) != EOK) {
38         return;
39     }
40 
41     if (memcpy_s(socketName.get(), bufSize, data, size) != EOK) {
42         return;
43     }
44 
45     SocketInfo info = {
46         .name = socketName.get(),
47         .peerName = const_cast<char *>(DEFAULT_SOCKET_PEER_NAME.c_str()),
48         .peerNetworkId = const_cast<char *>(DEFAULT_SOCKET_PEER_NETWORKID.c_str()),
49         .pkgName = const_cast<char *>(DEFAULT_SOCKET_PKG_NAME.c_str()),
50         .dataType = DATA_TYPE_MESSAGE,
51     };
52 
53     (void)Socket(info);
54 }
55 
SocketTestWithPeerName(const uint8_t * data,size_t size)56 void SocketTestWithPeerName(const uint8_t *data, size_t size)
57 {
58     if ((data == nullptr) || (size == 0)) {
59         return;
60     }
61 
62     const size_t bufSize = size + 1;
63     std::unique_ptr<char[]> socketPeerName = std::make_unique<char[]>(bufSize);
64     if (memset_s(socketPeerName.get(), bufSize, 0, bufSize) != EOK) {
65         return;
66     }
67 
68     if (memcpy_s(socketPeerName.get(), bufSize, data, size) != EOK) {
69         return;
70     }
71 
72     SocketInfo info = {
73         .name = const_cast<char *>(DEFAULT_SOCKET_NAME.c_str()),
74         .peerName = socketPeerName.get(),
75         .peerNetworkId = const_cast<char *>(DEFAULT_SOCKET_PEER_NETWORKID.c_str()),
76         .pkgName = const_cast<char *>(DEFAULT_SOCKET_PKG_NAME.c_str()),
77         .dataType = DATA_TYPE_MESSAGE,
78     };
79 
80     (void)Socket(info);
81 }
82 
SocketTestWithNetworkId(const uint8_t * data,size_t size)83 void SocketTestWithNetworkId(const uint8_t *data, size_t size)
84 {
85     if ((data == nullptr) || (size == 0)) {
86         return;
87     }
88 
89     const size_t bufSize = size + 1;
90     std::unique_ptr<char[]> socketNetworkId = std::make_unique<char[]>(bufSize);
91     if (memset_s(socketNetworkId.get(), bufSize, 0, bufSize) != EOK) {
92         return;
93     }
94 
95     if (memcpy_s(socketNetworkId.get(), bufSize, data, size) != EOK) {
96         return;
97     }
98 
99     SocketInfo info = {
100         .name = const_cast<char *>(DEFAULT_SOCKET_NAME.c_str()),
101         .peerName = const_cast<char *>(DEFAULT_SOCKET_PEER_NAME.c_str()),
102         .peerNetworkId = socketNetworkId.get(),
103         .pkgName = const_cast<char *>(DEFAULT_SOCKET_PKG_NAME.c_str()),
104         .dataType = DATA_TYPE_MESSAGE,
105     };
106 
107     (void)Socket(info);
108 }
109 
SocketTestWithPkgName(const uint8_t * data,size_t size)110 void SocketTestWithPkgName(const uint8_t *data, size_t size)
111 {
112     if ((data == nullptr) || (size == 0)) {
113         return;
114     }
115 
116     const size_t bufSize = size + 1;
117     std::unique_ptr<char[]> socketPkgName = std::make_unique<char[]>(bufSize);
118     if (memset_s(socketPkgName.get(), bufSize, 0, bufSize) != EOK) {
119         return;
120     }
121 
122     if (memcpy_s(socketPkgName.get(), bufSize, data, size) != EOK) {
123         return;
124     }
125 
126     SocketInfo info = {
127         .name = const_cast<char *>(DEFAULT_SOCKET_NAME.c_str()),
128         .peerName = const_cast<char *>(DEFAULT_SOCKET_PEER_NAME.c_str()),
129         .peerNetworkId = const_cast<char *>(DEFAULT_SOCKET_PKG_NAME.c_str()),
130         .pkgName = socketPkgName.get(),
131         .dataType = DATA_TYPE_MESSAGE,
132     };
133 
134     (void)Socket(info);
135 }
136 
SocketTestWithDataType(const uint8_t * data,size_t size)137 void SocketTestWithDataType(const uint8_t *data, size_t size)
138 {
139     if ((data == nullptr) || (size < sizeof(TransDataType))) {
140         return;
141     }
142 
143     TransDataType socketDataType = DATA_TYPE_BUTT;
144     if (memcpy_s(&socketDataType, sizeof(TransDataType), data, sizeof(TransDataType)) != EOK) {
145         return;
146     }
147 
148     SocketInfo info = {
149         .name = const_cast<char *>(DEFAULT_SOCKET_NAME.c_str()),
150         .peerName = const_cast<char *>(DEFAULT_SOCKET_PEER_NAME.c_str()),
151         .peerNetworkId = const_cast<char *>(DEFAULT_SOCKET_PKG_NAME.c_str()),
152         .pkgName = const_cast<char *>(DEFAULT_SOCKET_PKG_NAME.c_str()),
153         .dataType = socketDataType,
154     };
155 
156     (void)Socket(info);
157 }
158 } // namespace OHOS
159 
160 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)161 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
162 {
163     OHOS::SocketTestWithName(data, size);
164     OHOS::SocketTestWithPeerName(data, size);
165     OHOS::SocketTestWithNetworkId(data, size);
166     OHOS::SocketTestWithPkgName(data, size);
167     OHOS::SocketTestWithDataType(data, size);
168     return 0;
169 }
170