From: Petr Štetiar Date: Tue, 13 Oct 2020 12:59:38 +0000 (+0200) Subject: tests: add dns_handle_packet_file tool X-Git-Url: http://git.openwrt.org/?p=project%2Fmdnsd.git;a=commitdiff_plain;h=bf01f2dd0089fd4a77a792ecf9f0fa45dbd66b50 tests: add dns_handle_packet_file tool So it can read packets from file, which is handy when using for example AFL or hongfuzz fuzzers. Signed-off-by: Petr Štetiar --- diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 02b121c..99c2482 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,3 +1,13 @@ IF(CMAKE_C_COMPILER_ID STREQUAL "Clang") ADD_SUBDIRECTORY(fuzz) + + ADD_EXECUTABLE(dhpf-san dns_handle_packet_file.c) + TARGET_INCLUDE_DIRECTORIES(dhpf-san PRIVATE ${PROJECT_SOURCE_DIR}) + TARGET_COMPILE_OPTIONS(dhpf-san PRIVATE -g -fno-omit-frame-pointer -fsanitize=undefined,address,leak -fno-sanitize-recover=all) + TARGET_LINK_OPTIONS(dhpf-san PRIVATE -fsanitize=undefined,address,leak) + TARGET_LINK_LIBRARIES(dhpf-san umdns-lib-san) ENDIF() + +ADD_EXECUTABLE(dhpf dns_handle_packet_file.c) +TARGET_INCLUDE_DIRECTORIES(dhpf PRIVATE ${PROJECT_SOURCE_DIR}) +TARGET_LINK_LIBRARIES(dhpf umdns-lib) diff --git a/tests/dns_handle_packet_file.c b/tests/dns_handle_packet_file.c new file mode 100644 index 0000000..cbcea08 --- /dev/null +++ b/tests/dns_handle_packet_file.c @@ -0,0 +1,64 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "dns.h" +#include "cache.c" +#include "interface.h" + +int cfg_proto = 0; +int cfg_no_subnet = 0; + +static void fuzz_dns_handle_packet(uint8_t *input, size_t size) +{ + struct sockaddr from; + struct interface iface; + struct cache_service *s, *t; + + memset(&from, 0, sizeof(from)); + memset(&iface, 0, sizeof(iface)); + + cache_init(); + dns_handle_packet(&iface, &from, 1922, input, size); + + avl_for_each_element_safe(&services, s, avl, t) + cache_service_free(s); +} + +int main(int argc, char *argv[]) +{ + size_t len = 0; + FILE *fd = NULL; + uint8_t *buf = NULL; + + if (argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return -1; + } + + fd = fopen(argv[1], "r"); + if (!fd) { + perror("unable to open input file\n"); + return -1; + } + + buf = calloc(1, MDNS_BUF_LEN+1); + if (!buf) + return -1; + + len = fread(buf, 1, MDNS_BUF_LEN, fd); + + fuzz_dns_handle_packet(buf, len); + + fclose(fd); + free(buf); +}