1 /*
2 Copyright (c) 2020 Contributors as noted in the AUTHORS file
3
4 This file is part of libzmq, the ZeroMQ core engine in C++.
5
6 libzmq is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License (LGPL) as published
8 by the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 As a special exception, the Contributors give you permission to link
12 this library with independent modules to produce an executable,
13 regardless of the license terms of these independent modules, and to
14 copy and distribute the resulting executable under terms of your choice,
15 provided that you also meet, for each linked independent module, the
16 terms and conditions of the license of that module. An independent
17 module is a module which is not derived from or based on this library.
18 If you modify this library, you must extend this exception to your
19 version of the library.
20
21 libzmq is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24 License for more details.
25
26 You should have received a copy of the GNU Lesser General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29
30 #ifdef ZMQ_USE_FUZZING_ENGINE
31 #include <fuzzer/FuzzedDataProvider.h>
32 #endif
33
34 #include "testutil.hpp"
35 #include "testutil_unity.hpp"
36
37 // Test that the ZMTP WebSocket engine handles invalid handshake when connecting
38 // https://rfc.zeromq.org/spec/45/
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)39 extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size)
40 {
41 setup_test_context ();
42 char my_endpoint[MAX_SOCKET_STRING];
43 size_t my_endpoint_size = sizeof (my_endpoint);
44 void *server = test_context_socket (ZMQ_DEALER);
45 // As per API by default there's no limit to the size of a message,
46 // but the sanitizer allocator will barf over a gig or so
47 int64_t max_msg_size = 64 * 1024 * 1024;
48 TEST_ASSERT_SUCCESS_ERRNO (
49 zmq_setsockopt (server, ZMQ_MAXMSGSIZE, &max_msg_size, sizeof (int64_t)));
50 TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (server, "ws://127.0.0.1:*"));
51 TEST_ASSERT_SUCCESS_ERRNO (zmq_getsockopt (server, ZMQ_LAST_ENDPOINT,
52 my_endpoint, &my_endpoint_size));
53 // Remove trailing /
54 my_endpoint[my_endpoint_size - 2] = '\0';
55 fd_t client = connect_socket (my_endpoint, AF_INET, IPPROTO_WS);
56
57 void *client_good = test_context_socket (ZMQ_DEALER);
58 my_endpoint[my_endpoint_size - 2] = '/';
59 TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client_good, my_endpoint));
60
61 // If there is not enough data for a full handshake, just send what we can
62 // Otherwise send websocket handshake first, as expected by the protocol
63 uint8_t buf[256];
64 if (size >= 192) {
65 send (client, (void *) data, 192, MSG_NOSIGNAL);
66 data += 192;
67 size -= 192;
68 }
69 recv (client, buf, 256, MSG_DONTWAIT);
70 msleep (250);
71 for (ssize_t sent = 0; size > 0 && (sent != -1 || errno == EINTR);
72 size -= sent > 0 ? sent : 0, data += sent > 0 ? sent : 0)
73 sent = send (client, (const char *) data, size, MSG_NOSIGNAL);
74 msleep (250);
75 recv (client, buf, 256, MSG_DONTWAIT);
76
77 zmq_msg_t msg;
78 zmq_msg_init (&msg);
79 while (-1 != zmq_msg_recv (&msg, server, ZMQ_DONTWAIT)) {
80 zmq_msg_close (&msg);
81 zmq_msg_init (&msg);
82 }
83
84 send_string_expect_success (client_good, "abc", 0);
85 recv_string_expect_success (server, "abc", 0);
86
87 close (client);
88 test_context_socket_close_zero_linger (client_good);
89 test_context_socket_close_zero_linger (server);
90 teardown_test_context ();
91
92 return 0;
93 }
94
95 #ifndef ZMQ_USE_FUZZING_ENGINE
test_bind_ws_fuzzer()96 void test_bind_ws_fuzzer ()
97 {
98 uint8_t **data;
99 size_t *len, num_cases = 0;
100 if (fuzzer_corpus_encode (
101 "tests/libzmq-fuzz-corpora/test_bind_ws_fuzzer_seed_corpus", &data,
102 &len, &num_cases)
103 != 0)
104 exit (77);
105
106 while (num_cases-- > 0) {
107 TEST_ASSERT_SUCCESS_ERRNO (
108 LLVMFuzzerTestOneInput (data[num_cases], len[num_cases]));
109 free (data[num_cases]);
110 }
111
112 free (data);
113 free (len);
114 }
115
main(int argc,char ** argv)116 int main (int argc, char **argv)
117 {
118 setup_test_environment ();
119
120 UNITY_BEGIN ();
121 RUN_TEST (test_bind_ws_fuzzer);
122
123 return UNITY_END ();
124 }
125 #endif
126