fuzz: add initial fuzzing infrastructure

The fuzzers will be used by oss-fuzz to automatically and
continuously fuzz systemd.

This commit includes the build tooling necessary to build fuzz
targets, and a fuzzer for the DNS packet parser.
This commit is contained in:
Jonathan Rudenberg
2018-01-17 13:57:06 -05:00
parent 52ffb3d2e9
commit 7db7d5b733
9 changed files with 232 additions and 2 deletions
+43
View File
@@ -0,0 +1,43 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
/***
Copyright 2018 Jonathan Rudenberg
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "fuzz.h"
#include "resolved-dns-packet.h"
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
_cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
int r;
if (size > DNS_PACKET_SIZE_MAX)
return 0;
r = dns_packet_new(&p, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX);
if (r < 0)
return 0;
p->size = 0; /* by default append starts after the header, undo that */
dns_packet_append_blob(p, data, size, NULL);
if (size < DNS_PACKET_HEADER_SIZE) {
/* make sure we pad the packet back up to the minimum header size */
assert(p->allocated >= DNS_PACKET_HEADER_SIZE);
memzero(DNS_PACKET_DATA(p) + size, DNS_PACKET_HEADER_SIZE - size);
p->size = DNS_PACKET_HEADER_SIZE;
}
dns_packet_extract(p);
return 0;
}
+2
View File
@@ -0,0 +1,2 @@
[libfuzzer]
max_len = 65535
+51
View File
@@ -0,0 +1,51 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
/***
Copyright 2018 Jonathan Rudenberg
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "alloc-util.h"
#include "log.h"
#include "fileio.h"
#include "fuzz.h"
/* This is a test driver for the systemd fuzzers that provides main function
* for regression testing outside of oss-fuzz (https://github.com/google/oss-fuzz)
*
* It reads files named on the command line and passes them one by one into the
* fuzzer that it is compiled into. */
int main(int argc, char **argv) {
int i, r;
size_t size;
char *name;
log_set_max_level(LOG_DEBUG);
for (i = 1; i < argc; i++) {
_cleanup_free_ char *buf = NULL;
name = argv[i];
r = read_full_file(name, &buf, &size);
if (r < 0) {
log_error_errno(r, "Failed to open '%s': %m", name);
return EXIT_FAILURE;
}
printf("%s... ", name);
fflush(stdout);
(void)LLVMFuzzerTestOneInput((uint8_t*)buf, size);
printf("ok\n");
}
return EXIT_SUCCESS;
}
+23
View File
@@ -0,0 +1,23 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
/***
Copyright 2018 Jonathan Rudenberg
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stddef.h>
#include <stdint.h>
/* The entry point into the fuzzer */
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
+25
View File
@@ -0,0 +1,25 @@
# SPDX-License-Identifier: LGPL-2.1+
# Copyright 2018 Jonathan Rudenberg
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# systemd 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with systemd; If not, see <http://www.gnu.org/licenses/>.
fuzzers += [
[['src/fuzz/fuzz-dns-packet.c',
dns_type_headers],
[libsystemd_resolve_core,
libshared],
[libgcrypt,
libgpg_error,
libm]],
]