fuzz_http_connect.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Copyright (c) 2016-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #define BUFFERS_PRIVATE
  5. #define CONNECTION_EDGE_PRIVATE
  6. #include "or.h"
  7. #include "backtrace.h"
  8. #include "buffers.h"
  9. #include "config.h"
  10. #include "connection.h"
  11. #include "connection_edge.h"
  12. #include "proto_socks.h"
  13. #include "torlog.h"
  14. #include "entry_connection_st.h"
  15. #include "fuzzing.h"
  16. static void
  17. mock_connection_write_to_buf_impl_(const char *string, size_t len,
  18. connection_t *conn, int compressed)
  19. {
  20. log_debug(LD_GENERAL, "%sResponse:\n%u\nConnection: %p\n%s\n",
  21. compressed ? "Compressed " : "", (unsigned)len, conn, string);
  22. }
  23. static void
  24. mock_connection_mark_unattached_ap_(entry_connection_t *conn, int endreason,
  25. int line, const char *file)
  26. {
  27. (void)conn;
  28. (void)endreason;
  29. (void)line;
  30. (void)file;
  31. }
  32. static int
  33. mock_connection_ap_rewrite_and_attach_if_allowed(entry_connection_t *conn,
  34. origin_circuit_t *circ,
  35. crypt_path_t *cpath)
  36. {
  37. (void)conn;
  38. (void)circ;
  39. (void)cpath;
  40. return 0;
  41. }
  42. int
  43. fuzz_init(void)
  44. {
  45. /* Set up fake response handler */
  46. MOCK(connection_write_to_buf_impl_, mock_connection_write_to_buf_impl_);
  47. /* Set up the fake handler functions */
  48. MOCK(connection_mark_unattached_ap_, mock_connection_mark_unattached_ap_);
  49. MOCK(connection_ap_rewrite_and_attach_if_allowed,
  50. mock_connection_ap_rewrite_and_attach_if_allowed);
  51. return 0;
  52. }
  53. int
  54. fuzz_cleanup(void)
  55. {
  56. UNMOCK(connection_write_to_buf_impl_);
  57. UNMOCK(connection_mark_unattached_ap_);
  58. UNMOCK(connection_ap_rewrite_and_attach_if_allowed);
  59. return 0;
  60. }
  61. int
  62. fuzz_main(const uint8_t *stdin_buf, size_t data_size)
  63. {
  64. entry_connection_t conn;
  65. /* Set up the fake connection */
  66. memset(&conn, 0, sizeof(conn));
  67. conn.edge_.base_.type = CONN_TYPE_AP;
  68. conn.edge_.base_.state = AP_CONN_STATE_HTTP_CONNECT_WAIT;
  69. conn.socks_request = tor_malloc_zero(sizeof(socks_request_t));
  70. conn.socks_request->listener_type = CONN_TYPE_AP_HTTP_CONNECT_LISTENER;
  71. conn.edge_.base_.inbuf = buf_new_with_data((char*)stdin_buf, data_size);
  72. if (!conn.edge_.base_.inbuf) {
  73. log_debug(LD_GENERAL, "Zero-Length-Input\n");
  74. goto done;
  75. }
  76. /* Parse the headers */
  77. int rv = connection_ap_process_http_connect(&conn);
  78. /* TODO: check the output is correctly parsed based on the input */
  79. log_debug(LD_GENERAL, "Result:\n%d\n", rv);
  80. goto done;
  81. done:
  82. /* Reset. */
  83. socks_request_free(conn.socks_request);
  84. buf_free(conn.edge_.base_.inbuf);
  85. conn.edge_.base_.inbuf = NULL;
  86. return 0;
  87. }