]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/fuzz/fuzz-journal-remote.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / fuzz / fuzz-journal-remote.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "fuzz.h"
4
5 #include <sys/mman.h>
6
7 #include "sd-journal.h"
8
9 #include "env-util.h"
10 #include "fd-util.h"
11 #include "fileio.h"
12 #include "fs-util.h"
13 #include "journal-remote.h"
14 #include "logs-show.h"
15 #include "memfd-util.h"
16 #include "strv.h"
17
18 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
19 _cleanup_fclose_ FILE *dev_null = NULL;
20 RemoteServer s = {};
21 char name[] = "/tmp/fuzz-journal-remote.XXXXXX.journal";
22 void *mem;
23 int fdin; /* will be closed by journal_remote handler after EOF */
24 _cleanup_close_ int fdout = -1;
25 sd_journal *j;
26 OutputMode mode;
27 int r;
28
29 if (size <= 2)
30 return 0;
31
32 assert_se((fdin = memfd_new_and_map("fuzz-journal-remote", size, &mem)) >= 0);
33 memcpy(mem, data, size);
34 assert_se(munmap(mem, size) == 0);
35
36 fdout = mkostemps(name, STRLEN(".journal"), O_CLOEXEC);
37 assert_se(fdout >= 0);
38
39 /* In */
40
41 assert_se(journal_remote_server_init(&s, name, JOURNAL_WRITE_SPLIT_NONE, false, false) >= 0);
42
43 assert_se(journal_remote_add_source(&s, fdin, (char*) "fuzz-data", false) > 0);
44
45 while (s.active) {
46 r = journal_remote_handle_raw_source(NULL, fdin, 0, &s);
47 assert_se(r >= 0);
48 }
49
50 journal_remote_server_destroy(&s);
51 assert_se(close(fdin) < 0 && errno == EBADF); /* Check that the fd is closed already */
52
53 /* Out */
54
55 r = sd_journal_open_files(&j, (const char**) STRV_MAKE(name), 0);
56 assert_se(r >= 0);
57
58 if (getenv_bool("SYSTEMD_FUZZ_OUTPUT") <= 0)
59 assert_se(dev_null = fopen("/dev/null", "we"));
60
61 for (mode = 0; mode < _OUTPUT_MODE_MAX; mode++) {
62 if (!dev_null)
63 log_info("/* %s */", output_mode_to_string(mode));
64 r = show_journal(dev_null ?: stdout, j, mode, 0, 0, -1, 0, NULL);
65 assert_se(r >= 0);
66
67 r = sd_journal_seek_head(j);
68 assert_se(r >= 0);
69 }
70
71 sd_journal_close(j);
72 unlink(name);
73
74 return 0;
75 }