Commit c01f62cb authored by Jan Včelák's avatar Jan Včelák 🚀
Browse files

Merge branch 'afl-persistent' into 'master'

Add support for AFL Persistent Fuzzing Mode.
parents be23fecd ca49426e
......@@ -131,6 +131,7 @@ src/dnssec/tests/kasp_dir_file.c
src/dnssec/tests/kasp_store.c
src/dnssec/tests/key.c
src/dnssec/tests/key_algorithm.c
src/dnssec/tests/key_ds.c
src/dnssec/tests/keyid.c
src/dnssec/tests/keystore_pkcs8.c
src/dnssec/tests/keystore_pkcs8_dir.c
......@@ -487,6 +488,9 @@ src/zscanner/tests/tests.h
src/zscanner/tests/zscanner-tool.c
tests-fuzz/Makefile.am
tests-fuzz/packet.c
tests-fuzz/wrap/server.c
tests-fuzz/wrap/tcp-handler.c
tests-fuzz/wrap/udp-handler.c
tests/Makefile.am
tests/acl.c
tests/base32hex.c
......
/Makefile.in
/Makefile
/knotd_stdio
/packet
AM_CPPFLAGS = \
-include $(top_builddir)/src/config.h \
-I$(top_srcdir)/src
# -I$(top_srcdir)/src/dnssec/lib
-I$(top_srcdir)/src \
-I$(top_srcdir)/src/dnssec/lib
LDADD = \
$(top_builddir)/src/libknot.la
noinst_PROGRAMS = \
check_PROGRAMS = \
knotd_stdio \
packet
knotd_stdio_SOURCES = wrap/server.c wrap/tcp-handler.c wrap/udp-handler.c
knotd_stdio_CPPFLAGS = $(AM_CPPFLAGS) $(liburcu_CFLAGS)
knotd_stdio_LDADD = \
$(top_builddir)/src/knot/knotd-main.o \
$(top_builddir)/src/libknotd.la $(liburcu_LIBS)
# Fuzzing
Knot DNS 2.0 includes two fuzzing tests in `tests-fuzz/`: a) a simple
test harness that exercises the packet parsing logic in
`packet.c` and more through test that replaces UDP handler with reads
from stdin in `knotd_stdio.c`. This compiles into a test harness that
is designed to be used with lcamtuf's [American Fuzzy Lop (AFL)
fuzzer](http://lcamtuf.coredump.cx/afl/). We will use knotd_stdio in
the following examples.
## How it works
AFL 1.83b includes an experimental feature called ["persistent
mode"](http://lcamtuf.blogspot.com/2015/06/new-in-afl-persistent-mode.html)
that can be used to control AFL's fork server to fuzz inputs and
exercise the program without restarting it. You can use this new
feature along with the included Knot DNS test harness.
## Using the AFL persistent harness
### Gathering seed inputs
Gathering DNS packets for use in fuzzing is left to the tester, but
note that the fuzzing shim includes an environment variable to support
test cases minimization with `afl-cmin`:
```
$ cat > knot-afl.conf << EOF
server:
listen: 0.0.0.0@5353
log:
- target: stderr
any: error
control:
listen: /tmp/knot.sock
EOF
$ afl-cmin -i ~/knot-seeds -o ~/knot-seeds-cmin -m 1000000 -t 400000 -- tests-fuzz/knotd_stdio -c knot-afl.conf
```
You might want to configure some sample zones and have a test set of
fuzzing data that would end up querying those zones.
### Compiling the test harness.
See the AFL [blog post](http://lcamtuf.blogspot.com/2015/06/new-in-afl-persistent-mode.html)
and README for details on how to use LLVM mode and compile binaries
for use with persistent mode. For reference, you can use these
commands to build Knot with the fuzzing harness:
```
$ CC=afl-clang-fast ./configure --disable-shared
$ make check
```
### Fuzz
A basic AFL run can then be kicked off as follows:
```
AFL_PERSISTENT=1 afl-fuzz -i my_seeds -o my_output_dir -t 10000 -m 100000 -- tests-fuzz/knotd_stdio -c knot-afl.conf
```
Note that AFL can be scaled up by supplying the `-M` flag and starting
multiple instances of the fuzzer.
......@@ -17,27 +17,25 @@
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <signal.h>
#include "libknot/errcode.h"
#include "libknot/packet/pkt.h"
#define log(fmt, ...) fprintf(stderr, "# " fmt "\n", ## __VA_ARGS__)
#include "libknot/libknot.h"
int main(void)
{
log("reading packet data from stdin");
uint8_t buffer[UINT16_MAX + 1] = { 0 };
size_t len = fread(buffer, 1, sizeof(buffer), stdin);
log("parsing packet of size %zu", len);
knot_pkt_t *pkt = knot_pkt_new(buffer, len, NULL);
assert(pkt);
int r = knot_pkt_parse(pkt, 0);
knot_pkt_free(&pkt);
log("result %d (%s)", r, r == KNOT_EOK ? "success" : "failure");
return (r == KNOT_EOK ? 0 : 1);
for(;;) {
uint8_t buffer[UINT16_MAX + 1] = { 0 };
size_t len = fread(buffer, 1, sizeof(buffer), stdin);
knot_pkt_t *pkt = knot_pkt_new(buffer, len, NULL);
assert(pkt);
int r = knot_pkt_parse(pkt, 0);
knot_pkt_free(&pkt);
if (getenv("AFL_PERSISTENT")) {
raise(SIGSTOP);
} else {
return (r == KNOT_EOK ? 0 : 1);
}
}
}
/* Copyright (C) 2015 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Wrap function server_reconfigure to initialize udp_master to stdin
*/
#define server_reconfigure _orig_server_reconfigure
#include "knot/server/server.c"
#undef server_reconfigure
extern void udp_master_init_stdio(server_t *server);
int server_reconfigure(conf_t *conf, void *data)
{
log_info("AFL, Wrap server_reconfigure()");
int ret = _orig_server_reconfigure(conf, data);
udp_master_init_stdio(data);
return ret;
}
/* Copyright (C) 2015 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define tcp_master _orig_tcp_master
#include "knot/server/tcp-handler.c"
#undef tcp_master
int tcp_master(dthread_t *thread)
{
log_info("AFL, tcp_master out of order");
return 0;
}
/* Copyright (C) 2015 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Udp handler listen on stdin and send to stdout.
* To use this handler initialize it with udp_master_init_stdin.
*/
#include <stdbool.h>
#include "knot/server/udp-handler.c"
#include "knot/common/debug.h"
struct udp_stdin {
struct iovec iov[NBUFS];
uint8_t buf[NBUFS][KNOT_WIRE_MAX_PKTSIZE];
struct sockaddr_storage addr;
bool afl_persistent;
};
static inline void next(struct udp_stdin *rq)
{
if (rq->afl_persistent) {
raise(SIGSTOP);
} else {
exit(0);
}
}
static void *udp_stdin_init(void)
{
struct udp_stdin *rq = malloc(sizeof(struct udp_stdin));
memset(rq, 0, sizeof(struct udp_stdin));
for (unsigned i = 0; i < NBUFS; ++i) {
rq->iov[i].iov_base = rq->buf[i];
rq->iov[i].iov_len = KNOT_WIRE_MAX_PKTSIZE;
}
struct sockaddr_in *a = (struct sockaddr_in *)&rq->addr;
a->sin_family = AF_INET;
a->sin_addr.s_addr = IN_LOOPBACKNET;
a->sin_port = 42;
rq->afl_persistent = getenv("AFL_PERSISTENT") != NULL;
return rq;
}
static int udp_stdin_deinit(void *d)
{
free(d);
return 0;
}
static int udp_stdin_recv(int fd, void *d)
{
struct udp_stdin *rq = (struct udp_stdin *)d;
rq->iov[RX].iov_len = fread(rq->iov[RX].iov_base,
1, KNOT_WIRE_MAX_PKTSIZE, stdin);
if (rq->iov[RX].iov_len == 0) {
next(rq);
}
return rq->iov[RX].iov_len;
}
static int udp_stdin_handle(udp_context_t *ctx, void *d)
{
struct udp_stdin *rq = (struct udp_stdin *)d;
udp_handle(ctx, STDIN_FILENO, &rq->addr, &rq->iov[RX], &rq->iov[TX]);
return 0;
}
static int udp_stdin_send(void *d)
{
struct udp_stdin *rq = (struct udp_stdin *)d;
next(rq);
return 0;
}
/*!
* \brief Initialize udp_handler with stdio
*/
void udp_master_init_stdio(server_t *server) {
log_info("AFL, UDP handler listen on stdin");
// register our dummy interface to server
iface_t *ifc = malloc(sizeof(iface_t));
ifc->fd[RX] = STDIN_FILENO;
ifc->fd[TX] = STDOUT_FILENO;
add_tail(&server->ifaces->l, (node_t *)ifc);
_udp_init = udp_stdin_init;
_udp_recv = udp_stdin_recv;
_udp_handle = udp_stdin_handle;
_udp_send = udp_stdin_send;
_udp_deinit = udp_stdin_deinit;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment