1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)18 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
19         int fdin;
20         void *mem;
21         _cleanup_(unlink_tempfilep) char name[] = "/tmp/fuzz-journal-remote.XXXXXX.journal";
22         _cleanup_close_ int fdout = -1;
23         _cleanup_(sd_journal_closep) sd_journal *j = NULL;
24         _cleanup_(journal_remote_server_destroy) RemoteServer s = {};
25         int r;
26 
27         if (outside_size_range(size, 3, 65536))
28                 return 0;
29 
30         if (!getenv("SYSTEMD_LOG_LEVEL"))
31                 log_set_max_level(LOG_ERR);
32 
33         fdin = memfd_new_and_map("fuzz-journal-remote", size, &mem);
34         if (fdin < 0)
35                 return log_error_errno(fdin, "memfd_new_and_map() failed: %m");
36 
37         memcpy(mem, data, size);
38         assert_se(munmap(mem, size) == 0);
39 
40         fdout = mkostemps(name, STRLEN(".journal"), O_CLOEXEC);
41         if (fdout < 0)
42                 return log_error_errno(errno, "mkostemps() failed: %m");
43 
44         /* In */
45 
46         r = journal_remote_server_init(&s, name, JOURNAL_WRITE_SPLIT_NONE, 0);
47         if (r < 0) {
48                 assert_se(IN_SET(r, -ENOMEM, -EMFILE, -ENFILE));
49                 return r;
50         }
51 
52         r = journal_remote_add_source(&s, fdin, (char*) "fuzz-data", false);
53         if (r < 0) {
54                 safe_close(fdin);
55                 return r;
56         }
57         assert(r > 0);
58 
59         while (s.active)
60                 assert_se(journal_remote_handle_raw_source(NULL, fdin, 0, &s) >= 0);
61 
62         assert_se(close(fdin) < 0 && errno == EBADF); /* Check that the fd is closed already */
63 
64         /* Out */
65 
66         r = sd_journal_open_files(&j, (const char**) STRV_MAKE(name), 0);
67         if (r < 0) {
68                 log_error_errno(r, "sd_journal_open_files([\"%s\"]) failed: %m", name);
69                 assert_se(IN_SET(r, -ENOMEM, -EMFILE, -ENFILE, -ENODATA));
70                 return r;
71         }
72 
73         _cleanup_fclose_ FILE *dev_null = NULL;
74         if (getenv_bool("SYSTEMD_FUZZ_OUTPUT") <= 0) {
75                 dev_null = fopen("/dev/null", "we");
76                 if (!dev_null)
77                         return log_error_errno(errno, "fopen(\"/dev/null\") failed: %m");
78         }
79 
80         for (OutputMode mode = 0; mode < _OUTPUT_MODE_MAX; mode++) {
81                 if (!dev_null)
82                         log_info("/* %s */", output_mode_to_string(mode));
83                 r = show_journal(dev_null ?: stdout, j, mode, 0, 0, -1, 0, NULL);
84                 assert_se(r >= 0);
85 
86                 r = sd_journal_seek_head(j);
87                 assert_se(r >= 0);
88         }
89 
90         return 0;
91 }
92