/src/systemd/src/fuzz/fuzz-journal-remote.c
Line | Count | Source |
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 | 4.30k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
19 | 4.30k | _cleanup_fclose_ FILE *dev_null = NULL; |
20 | 4.30k | RemoteServer s = {}; |
21 | 4.30k | char name[] = "/tmp/fuzz-journal-remote.XXXXXX.journal"; |
22 | 4.30k | void *mem; |
23 | 4.30k | int fdin; /* will be closed by journal_remote handler after EOF */ |
24 | 4.30k | _cleanup_close_ int fdout = -1; |
25 | 4.30k | sd_journal *j; |
26 | 4.30k | OutputMode mode; |
27 | 4.30k | int r; |
28 | 4.30k | |
29 | 4.30k | if (size <= 2) |
30 | 2 | return 0; |
31 | 4.30k | |
32 | 4.30k | if (!getenv("SYSTEMD_LOG_LEVEL")) |
33 | 4.30k | log_set_max_level(LOG_CRIT); |
34 | 4.30k | |
35 | 4.30k | assert_se((fdin = memfd_new_and_map("fuzz-journal-remote", size, &mem)) >= 0); |
36 | 4.30k | memcpy(mem, data, size); |
37 | 4.30k | assert_se(munmap(mem, size) == 0); |
38 | 4.30k | |
39 | 4.30k | fdout = mkostemps(name, STRLEN(".journal"), O_CLOEXEC); |
40 | 4.30k | assert_se(fdout >= 0); |
41 | 4.30k | |
42 | 4.30k | /* In */ |
43 | 4.30k | |
44 | 4.30k | assert_se(journal_remote_server_init(&s, name, JOURNAL_WRITE_SPLIT_NONE, false, false) >= 0); |
45 | 4.30k | |
46 | 4.30k | assert_se(journal_remote_add_source(&s, fdin, (char*) "fuzz-data", false) > 0); |
47 | 4.30k | |
48 | 1.05M | while (s.active) { |
49 | 1.04M | r = journal_remote_handle_raw_source(NULL, fdin, 0, &s); |
50 | 1.04M | assert_se(r >= 0); |
51 | 1.04M | } |
52 | 4.30k | |
53 | 4.30k | journal_remote_server_destroy(&s); |
54 | 4.30k | assert_se(close(fdin) < 0 && errno == EBADF); /* Check that the fd is closed already */ |
55 | 4.30k | |
56 | 4.30k | /* Out */ |
57 | 4.30k | |
58 | 4.30k | r = sd_journal_open_files(&j, (const char**) STRV_MAKE(name), 0); |
59 | 4.30k | assert_se(r >= 0); |
60 | 4.30k | |
61 | 4.30k | if (getenv_bool("SYSTEMD_FUZZ_OUTPUT") <= 0) |
62 | 4.30k | assert_se(dev_null = fopen("/dev/null", "we")); |
63 | 4.30k | |
64 | 68.8k | for (mode = 0; mode < _OUTPUT_MODE_MAX; mode++) { |
65 | 64.5k | if (!dev_null) |
66 | 64.5k | log_info("/* %s */", output_mode_to_string(mode)); |
67 | 64.5k | r = show_journal(dev_null ?: stdout, j, mode, 0, 0, -1, 0, NULL); |
68 | 64.5k | assert_se(r >= 0); |
69 | 64.5k | |
70 | 64.5k | r = sd_journal_seek_head(j); |
71 | 64.5k | assert_se(r >= 0); |
72 | 64.5k | } |
73 | 4.30k | |
74 | 4.30k | sd_journal_close(j); |
75 | 4.30k | unlink(name); |
76 | 4.30k | |
77 | 4.30k | return 0; |
78 | 4.30k | } |