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