test_connect_ws_fuzzer.cpp 4.2 KB

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