diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2018-03-09 22:02:02 +0100 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2018-03-11 16:33:59 +0100 |
commit | ec7a02ea542c21ca1f734dd5766213426b26153a (patch) | |
tree | 8069cf776760353312ab016b0e403a7465a2bb96 /src/fuzz | |
parent | e8112e67e4a5dadf57b5f7a29061f63dfc9e0635 (diff) | |
download | systemd-ec7a02ea542c21ca1f734dd5766213426b26153a.tar.gz |
Add fuzzer for unit file parser
Diffstat (limited to 'src/fuzz')
-rw-r--r-- | src/fuzz/fuzz-unit-file.c | 54 | ||||
-rw-r--r-- | src/fuzz/meson.build | 11 |
2 files changed, 62 insertions, 3 deletions
diff --git a/src/fuzz/fuzz-unit-file.c b/src/fuzz/fuzz-unit-file.c new file mode 100644 index 0000000000..87e0b10f00 --- /dev/null +++ b/src/fuzz/fuzz-unit-file.c @@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ + +#include "conf-parser.h" +#include "fd-util.h" +#include "fileio.h" +#include "fuzz.h" +#include "install.h" +#include "load-fragment.h" +#include "string-util.h" +#include "unit.h" + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + _cleanup_free_ char *out = NULL; /* out should be freed after g */ + size_t out_size; + _cleanup_fclose_ FILE *f = NULL, *g = NULL; + _cleanup_free_ char *p = NULL; + UnitType t; + _cleanup_(manager_freep) Manager *m = NULL; + Unit *u; + const char *name; + + if (size == 0) + return 0; + + f = fmemopen((char*) data, size, "re"); + assert_se(f); + + if (read_line(f, LINE_MAX, &p) < 0) + return 0; + + t = unit_type_from_string(p); + if (t < 0) + return 0; + + if (!unit_vtable[t]->load) + return 0; + + assert_se(manager_new(UNIT_FILE_SYSTEM, MANAGER_TEST_RUN_MINIMAL, &m) >= 0); + + name = strjoina("a.", unit_type_to_string(t)); + assert_se(unit_new_for_name(m, unit_vtable[t]->object_size, name, &u) >= 0); + + (void) config_parse(name, name, f, + UNIT_VTABLE(u)->sections, + config_item_perf_lookup, load_fragment_gperf_lookup, + CONFIG_PARSE_ALLOW_INCLUDE, u); + + g = open_memstream(&out, &out_size); + assert_se(g); + + unit_dump(u, g, ""); + + return 0; +} diff --git a/src/fuzz/meson.build b/src/fuzz/meson.build index 09a8c8a11d..796c28e429 100644 --- a/src/fuzz/meson.build +++ b/src/fuzz/meson.build @@ -22,9 +22,14 @@ fuzzers += [ [libgcrypt, libgpg_error, libm]], - [['src/fuzz/fuzz-dhcp-server.c', - ], + + [['src/fuzz/fuzz-dhcp-server.c'], [libsystemd_network, libshared], - []] + []], + + [['src/fuzz/fuzz-unit-file.c'], + [libcore, + libshared], + [libmount]], ] |