test_bind_curve_fuzzer.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 <string.h>
  28. #include "testutil.hpp"
  29. #include "testutil_security.hpp"
  30. // Test that the ZMTP engine handles invalid handshake when binding
  31. // https://rfc.zeromq.org/spec/37/
  32. extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size)
  33. {
  34. const char *fixed_client_public =
  35. "{{k*81)yMWEF{/BxdMd[5RL^qRFxBgoL<8m.D^KD";
  36. const char *fixed_client_secret =
  37. "N?Gmik8R[2ACw{b7*[-$S6[4}aO#?DB?#=<OQPc7";
  38. const char *fixed_server_public =
  39. "3.9-xXwy{g*w72TP*3iB9IJJRxlBH<ufTAvPd2>C";
  40. const char *fixed_server_secret =
  41. "T}t5GLq%&Qm1)y3ywu-}pY3KEA//{^Ut!M1ut+B4";
  42. void *handler;
  43. void *zap_thread;
  44. void *server;
  45. void *server_mon;
  46. char my_endpoint[MAX_SOCKET_STRING];
  47. setup_test_context ();
  48. memcpy (valid_client_public, fixed_client_public, 41);
  49. setup_context_and_server_side (
  50. &handler, &zap_thread, &server, &server_mon, my_endpoint, &zap_handler,
  51. &socket_config_curve_server, (void *) fixed_server_secret);
  52. fd_t client = connect_socket (my_endpoint);
  53. // If there is not enough data for a full greeting, just send what we can
  54. // Otherwise send greeting first, as expected by the protocol
  55. uint8_t buf[512];
  56. if (size >= 64) {
  57. send (client, (void *) data, 64, MSG_NOSIGNAL);
  58. data += 64;
  59. size -= 64;
  60. }
  61. recv (client, buf, 64, 0);
  62. // Then send HELLO and expect WELCOME if there's enough data
  63. if (size >= 202) {
  64. send (client, (void *) data, 202, MSG_NOSIGNAL);
  65. data += 202;
  66. size -= 202;
  67. recv (client, buf, 170, MSG_DONTWAIT);
  68. }
  69. // Then send READY and expect INITIATE if there's enough data
  70. if (size >= 301) {
  71. send (client, (void *) data, 301, MSG_NOSIGNAL);
  72. data += 301;
  73. size -= 301;
  74. recv (client, buf, 512, MSG_DONTWAIT);
  75. }
  76. msleep (250);
  77. for (ssize_t sent = 0; size > 0 && (sent != -1 || errno == EINTR);
  78. size -= sent > 0 ? sent : 0, data += sent > 0 ? sent : 0)
  79. sent = send (client, (const char *) data, size, MSG_NOSIGNAL);
  80. msleep (250);
  81. // Drain the queue, if any
  82. zmq_msg_t msg;
  83. zmq_msg_init (&msg);
  84. while (-1 != zmq_msg_recv (&msg, server, ZMQ_DONTWAIT)) {
  85. zmq_msg_close (&msg);
  86. zmq_msg_init (&msg);
  87. }
  88. // A well-behaved client should work while the malformed data from the other
  89. // is being received
  90. curve_client_data_t curve_client_data = {
  91. fixed_server_public, fixed_client_public, fixed_client_secret};
  92. void *client_mon;
  93. void *client_good = create_and_connect_client (
  94. my_endpoint, socket_config_curve_client, &curve_client_data, &client_mon);
  95. bounce (server, client_good);
  96. close (client);
  97. test_context_socket_close_zero_linger (client_good);
  98. test_context_socket_close_zero_linger (client_mon);
  99. shutdown_context_and_server_side (zap_thread, server, server_mon, handler);
  100. teardown_test_context ();
  101. return 0;
  102. }
  103. #ifndef ZMQ_USE_FUZZING_ENGINE
  104. void test_bind_curve_fuzzer ()
  105. {
  106. uint8_t **data;
  107. size_t *len, num_cases = 0;
  108. if (fuzzer_corpus_encode (
  109. "tests/libzmq-fuzz-corpora/test_bind_curve_fuzzer_seed_corpus", &data,
  110. &len, &num_cases)
  111. != 0)
  112. exit (77);
  113. while (num_cases-- > 0) {
  114. TEST_ASSERT_SUCCESS_ERRNO (
  115. LLVMFuzzerTestOneInput (data[num_cases], len[num_cases]));
  116. free (data[num_cases]);
  117. }
  118. free (data);
  119. free (len);
  120. }
  121. int main (int argc, char **argv)
  122. {
  123. setup_test_environment ();
  124. UNITY_BEGIN ();
  125. RUN_TEST (test_bind_curve_fuzzer);
  126. return UNITY_END ();
  127. }
  128. #endif