]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/fuzz/fuzz-unit-file.c
Merge pull request #8423 from keszybz/unit-file-fuzzer
[thirdparty/systemd.git] / src / fuzz / fuzz-unit-file.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "conf-parser.h"
4 #include "fd-util.h"
5 #include "fileio.h"
6 #include "fuzz.h"
7 #include "install.h"
8 #include "load-fragment.h"
9 #include "string-util.h"
10 #include "unit.h"
11
12 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
13 _cleanup_free_ char *out = NULL; /* out should be freed after g */
14 size_t out_size;
15 _cleanup_fclose_ FILE *f = NULL, *g = NULL;
16 _cleanup_free_ char *p = NULL;
17 UnitType t;
18 _cleanup_(manager_freep) Manager *m = NULL;
19 Unit *u;
20 const char *name;
21
22 if (size == 0)
23 return 0;
24
25 f = fmemopen((char*) data, size, "re");
26 assert_se(f);
27
28 if (read_line(f, LINE_MAX, &p) < 0)
29 return 0;
30
31 t = unit_type_from_string(p);
32 if (t < 0)
33 return 0;
34
35 if (!unit_vtable[t]->load)
36 return 0;
37
38 /* We don't want to fill the logs with messages about parse errors.
39 * Disable most logging if not running standalone */
40 if (!getenv("SYSTEMD_LOG_LEVEL"))
41 log_set_max_level(LOG_CRIT);
42
43 assert_se(manager_new(UNIT_FILE_SYSTEM, MANAGER_TEST_RUN_MINIMAL, &m) >= 0);
44
45 name = strjoina("a.", unit_type_to_string(t));
46 assert_se(unit_new_for_name(m, unit_vtable[t]->object_size, name, &u) >= 0);
47
48 (void) config_parse(name, name, f,
49 UNIT_VTABLE(u)->sections,
50 config_item_perf_lookup, load_fragment_gperf_lookup,
51 CONFIG_PARSE_ALLOW_INCLUDE, u);
52
53 g = open_memstream(&out, &out_size);
54 assert_se(g);
55
56 unit_dump(u, g, "");
57
58 return 0;
59 }