2018-06-20 08:13:28 -04:00
|
|
|
/* Copyright (c) 2016-2018, The Tor Project, Inc. */
|
2016-12-13 19:16:03 -05:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
#include "orconfig.h"
|
|
|
|
|
|
|
|
#define BUFFERS_PRIVATE
|
2016-12-13 19:41:03 -05:00
|
|
|
#define DIRECTORY_PRIVATE
|
2016-12-13 19:16:03 -05:00
|
|
|
|
2018-06-20 09:35:05 -04:00
|
|
|
#include "or/or.h"
|
2018-06-21 09:54:04 -04:00
|
|
|
#include "lib/err/backtrace.h"
|
2018-06-28 16:29:35 -04:00
|
|
|
#include "lib/container/buffers.h"
|
2018-06-20 09:35:05 -04:00
|
|
|
#include "or/config.h"
|
|
|
|
#include "or/connection.h"
|
|
|
|
#include "or/directory.h"
|
2018-06-22 10:32:10 -04:00
|
|
|
#include "lib/log/torlog.h"
|
2016-12-13 19:16:03 -05:00
|
|
|
|
2018-06-20 09:35:05 -04:00
|
|
|
#include "or/dir_connection_st.h"
|
2018-06-15 10:31:21 -04:00
|
|
|
|
2018-06-20 09:35:05 -04:00
|
|
|
#include "test/fuzz/fuzzing.h"
|
2016-12-13 19:16:03 -05:00
|
|
|
|
|
|
|
static void
|
2016-12-14 08:05:22 -05:00
|
|
|
mock_connection_write_to_buf_impl_(const char *string, size_t len,
|
2017-04-27 03:36:02 +02:00
|
|
|
connection_t *conn, int compressed)
|
2016-12-13 19:16:03 -05:00
|
|
|
{
|
2017-01-30 09:09:42 -05:00
|
|
|
log_debug(LD_GENERAL, "%sResponse:\n%u\nConnection: %p\n%s\n",
|
2017-04-27 03:36:02 +02:00
|
|
|
compressed ? "Compressed " : "", (unsigned)len, conn, string);
|
2016-12-13 19:16:03 -05:00
|
|
|
}
|
|
|
|
|
2016-12-14 08:05:22 -05:00
|
|
|
static int
|
|
|
|
mock_directory_handle_command_get(dir_connection_t *conn,
|
|
|
|
const char *headers,
|
|
|
|
const char *body,
|
|
|
|
size_t body_len)
|
2016-12-13 19:16:03 -05:00
|
|
|
{
|
2016-12-14 08:05:22 -05:00
|
|
|
(void)conn;
|
|
|
|
|
|
|
|
log_debug(LD_GENERAL, "Method:\nGET\n");
|
|
|
|
|
|
|
|
if (headers) {
|
2017-01-30 09:09:42 -05:00
|
|
|
log_debug(LD_GENERAL, "Header-Length:\n%u\n", (unsigned)strlen(headers));
|
2016-12-14 08:05:22 -05:00
|
|
|
log_debug(LD_GENERAL, "Headers:\n%s\n", headers);
|
|
|
|
}
|
|
|
|
|
2017-01-30 09:09:42 -05:00
|
|
|
log_debug(LD_GENERAL, "Body-Length:\n%u\n", (unsigned)body_len);
|
2016-12-14 08:05:22 -05:00
|
|
|
if (body) {
|
|
|
|
log_debug(LD_GENERAL, "Body:\n%s\n", body);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Always tell the caller we succeeded */
|
|
|
|
return 0;
|
2016-12-13 19:16:03 -05:00
|
|
|
}
|
|
|
|
|
2016-12-14 08:05:22 -05:00
|
|
|
static int
|
|
|
|
mock_directory_handle_command_post(dir_connection_t *conn,
|
|
|
|
const char *headers,
|
|
|
|
const char *body,
|
|
|
|
size_t body_len)
|
2016-12-13 19:16:03 -05:00
|
|
|
{
|
2016-12-14 08:05:22 -05:00
|
|
|
(void)conn;
|
|
|
|
|
|
|
|
log_debug(LD_GENERAL, "Method:\nPOST\n");
|
|
|
|
|
|
|
|
if (headers) {
|
2017-01-30 09:09:42 -05:00
|
|
|
log_debug(LD_GENERAL, "Header-Length:\n%u\n", (unsigned)strlen(headers));
|
2016-12-14 08:05:22 -05:00
|
|
|
log_debug(LD_GENERAL, "Headers:\n%s\n", headers);
|
|
|
|
}
|
|
|
|
|
2017-01-30 09:09:42 -05:00
|
|
|
log_debug(LD_GENERAL, "Body-Length:\n%u\n", (unsigned)body_len);
|
2016-12-14 08:05:22 -05:00
|
|
|
if (body) {
|
|
|
|
log_debug(LD_GENERAL, "Body:\n%s\n", body);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Always tell the caller we succeeded */
|
|
|
|
return 0;
|
2016-12-13 19:16:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2016-12-13 19:41:03 -05:00
|
|
|
fuzz_init(void)
|
2016-12-13 19:16:03 -05:00
|
|
|
{
|
|
|
|
/* Set up fake response handler */
|
|
|
|
MOCK(connection_write_to_buf_impl_, mock_connection_write_to_buf_impl_);
|
2016-12-14 08:05:22 -05:00
|
|
|
/* Set up the fake handler functions */
|
|
|
|
MOCK(directory_handle_command_get, mock_directory_handle_command_get);
|
|
|
|
MOCK(directory_handle_command_post, mock_directory_handle_command_post);
|
|
|
|
|
2016-12-13 19:41:03 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2016-12-13 19:16:03 -05:00
|
|
|
|
2016-12-13 19:41:03 -05:00
|
|
|
int
|
|
|
|
fuzz_cleanup(void)
|
|
|
|
{
|
|
|
|
UNMOCK(connection_write_to_buf_impl_);
|
2016-12-14 08:05:22 -05:00
|
|
|
UNMOCK(directory_handle_command_get);
|
|
|
|
UNMOCK(directory_handle_command_post);
|
2016-12-13 19:41:03 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2016-12-13 19:16:03 -05:00
|
|
|
|
2016-12-13 19:41:03 -05:00
|
|
|
int
|
|
|
|
fuzz_main(const uint8_t *stdin_buf, size_t data_size)
|
|
|
|
{
|
|
|
|
dir_connection_t dir_conn;
|
2016-12-13 19:16:03 -05:00
|
|
|
|
2016-12-13 19:41:03 -05:00
|
|
|
/* Set up the fake connection */
|
|
|
|
memset(&dir_conn, 0, sizeof(dir_connection_t));
|
|
|
|
dir_conn.base_.type = CONN_TYPE_DIR;
|
2016-12-13 19:16:03 -05:00
|
|
|
/* Apparently tor sets this before directory_handle_command() is called. */
|
|
|
|
dir_conn.base_.address = tor_strdup("replace-this-address.example.com");
|
|
|
|
|
2016-12-13 19:41:03 -05:00
|
|
|
dir_conn.base_.inbuf = buf_new_with_data((char*)stdin_buf, data_size);
|
2016-12-13 19:16:03 -05:00
|
|
|
if (!dir_conn.base_.inbuf) {
|
|
|
|
log_debug(LD_GENERAL, "Zero-Length-Input\n");
|
2017-01-11 13:24:48 -05:00
|
|
|
goto done;
|
2016-12-13 19:16:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the headers */
|
2016-12-13 19:41:03 -05:00
|
|
|
int rv = directory_handle_command(&dir_conn);
|
2016-12-13 19:16:03 -05:00
|
|
|
|
|
|
|
/* TODO: check the output is correctly parsed based on the input */
|
|
|
|
|
|
|
|
/* Report the parsed origin address */
|
|
|
|
if (dir_conn.base_.address) {
|
|
|
|
log_debug(LD_GENERAL, "Address:\n%s\n", dir_conn.base_.address);
|
|
|
|
}
|
|
|
|
|
|
|
|
log_debug(LD_GENERAL, "Result:\n%d\n", rv);
|
|
|
|
|
2017-01-11 13:24:48 -05:00
|
|
|
done:
|
2016-12-13 19:41:03 -05:00
|
|
|
/* Reset. */
|
2016-12-13 19:16:03 -05:00
|
|
|
tor_free(dir_conn.base_.address);
|
|
|
|
buf_free(dir_conn.base_.inbuf);
|
|
|
|
dir_conn.base_.inbuf = NULL;
|
|
|
|
|
2016-12-13 19:41:03 -05:00
|
|
|
return 0;
|
2016-12-13 19:16:03 -05:00
|
|
|
}
|
2016-12-14 17:11:26 -05:00
|
|
|
|