]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/fuzz/fuzz-unit-file.c
fuzz-unit-file: adjust check for ListenNetlink yet again
[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 long offset;
22
23 if (size == 0)
24 return 0;
25
26 f = fmemopen((char*) data, size, "re");
27 assert_se(f);
28
29 if (read_line(f, LINE_MAX, &p) < 0)
30 return 0;
31
32 t = unit_type_from_string(p);
33 if (t < 0)
34 return 0;
35
36 if (!unit_vtable[t]->load)
37 return 0;
38
39 offset = ftell(f);
40 assert_se(offset >= 0);
41
42 for (;;) {
43 _cleanup_free_ char *l = NULL;
44 const char *ll;
45
46 if (read_line(f, LONG_LINE_MAX, &l) <= 0)
47 break;
48
49 ll = l + strspn(l, WHITESPACE);
50
51 if (startswith(ll, "ListenNetlink=")) {
52 /* ListenNetlink causes a false positive in msan,
53 * let's skip this for now. */
54 log_notice("Skipping test because ListenNetlink= is present");
55 return 0;
56 }
57 }
58
59 assert_se(fseek(f, offset, SEEK_SET) == 0);
60
61 /* We don't want to fill the logs with messages about parse errors.
62 * Disable most logging if not running standalone */
63 if (!getenv("SYSTEMD_LOG_LEVEL"))
64 log_set_max_level(LOG_CRIT);
65
66 assert_se(manager_new(UNIT_FILE_SYSTEM, MANAGER_TEST_RUN_MINIMAL, &m) >= 0);
67
68 name = strjoina("a.", unit_type_to_string(t));
69 assert_se(unit_new_for_name(m, unit_vtable[t]->object_size, name, &u) >= 0);
70
71 (void) config_parse(name, name, f,
72 UNIT_VTABLE(u)->sections,
73 config_item_perf_lookup, load_fragment_gperf_lookup,
74 CONFIG_PARSE_ALLOW_INCLUDE, u);
75
76 g = open_memstream(&out, &out_size);
77 assert_se(g);
78
79 unit_dump(u, g, "");
80
81 return 0;
82 }