]>
Commit | Line | Data |
---|---|---|
a38f7fec ZJS |
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 | ||
6dbef305 | 9 | #include "env-util.h" |
a38f7fec ZJS |
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) { | |
6dbef305 | 19 | _cleanup_fclose_ FILE *dev_null = NULL; |
a38f7fec ZJS |
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; | |
bbdad085 | 26 | OutputMode mode; |
a38f7fec ZJS |
27 | int r; |
28 | ||
29 | if (size <= 2) | |
30 | return 0; | |
31 | ||
95ccf1aa EV |
32 | if (!getenv("SYSTEMD_LOG_LEVEL")) |
33 | log_set_max_level(LOG_CRIT); | |
34 | ||
a38f7fec ZJS |
35 | assert_se((fdin = memfd_new_and_map("fuzz-journal-remote", size, &mem)) >= 0); |
36 | memcpy(mem, data, size); | |
37 | assert_se(munmap(mem, size) == 0); | |
38 | ||
39 | fdout = mkostemps(name, STRLEN(".journal"), O_CLOEXEC); | |
40 | assert_se(fdout >= 0); | |
41 | ||
42 | /* In */ | |
43 | ||
44 | assert_se(journal_remote_server_init(&s, name, JOURNAL_WRITE_SPLIT_NONE, false, false) >= 0); | |
45 | ||
46 | assert_se(journal_remote_add_source(&s, fdin, (char*) "fuzz-data", false) > 0); | |
47 | ||
48 | while (s.active) { | |
49 | r = journal_remote_handle_raw_source(NULL, fdin, 0, &s); | |
50 | assert_se(r >= 0); | |
51 | } | |
52 | ||
53 | journal_remote_server_destroy(&s); | |
54 | assert_se(close(fdin) < 0 && errno == EBADF); /* Check that the fd is closed already */ | |
55 | ||
56 | /* Out */ | |
57 | ||
58 | r = sd_journal_open_files(&j, (const char**) STRV_MAKE(name), 0); | |
59 | assert_se(r >= 0); | |
60 | ||
6dbef305 ZJS |
61 | if (getenv_bool("SYSTEMD_FUZZ_OUTPUT") <= 0) |
62 | assert_se(dev_null = fopen("/dev/null", "we")); | |
63 | ||
bbdad085 | 64 | for (mode = 0; mode < _OUTPUT_MODE_MAX; mode++) { |
6dbef305 ZJS |
65 | if (!dev_null) |
66 | log_info("/* %s */", output_mode_to_string(mode)); | |
67 | r = show_journal(dev_null ?: stdout, j, mode, 0, 0, -1, 0, NULL); | |
bbdad085 ZJS |
68 | assert_se(r >= 0); |
69 | ||
70 | r = sd_journal_seek_head(j); | |
71 | assert_se(r >= 0); | |
72 | } | |
a38f7fec ZJS |
73 | |
74 | sd_journal_close(j); | |
75 | unlink(name); | |
76 | ||
77 | return 0; | |
78 | } |