1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include "fd-util.h"
4 #include "fs-util.h"
5 #include "fuzz-journald.h"
6 #include "fuzz.h"
7 #include "journald-native.h"
8 #include "memfd-util.h"
9 #include "process-util.h"
10 #include "tmpfile-util.h"
11 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)12 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
13         Server s;
14         _cleanup_close_ int sealed_fd = -1, unsealed_fd = -1;
15         _cleanup_(unlink_tempfilep) char name[] = "/tmp/fuzz-journald-native-fd.XXXXXX";
16         char *label = NULL;
17         size_t label_len = 0;
18         struct ucred ucred;
19         struct timeval *tv = NULL;
20 
21         if (!getenv("SYSTEMD_LOG_LEVEL"))
22                 log_set_max_level(LOG_CRIT);
23 
24         dummy_server_init(&s, NULL, 0);
25 
26         sealed_fd = memfd_new(NULL);
27         assert_se(sealed_fd >= 0);
28         assert_se(write(sealed_fd, data, size) == (ssize_t) size);
29         assert_se(memfd_set_sealed(sealed_fd) >= 0);
30         assert_se(lseek(sealed_fd, 0, SEEK_SET) == 0);
31         ucred = (struct ucred) {
32                 .pid = getpid_cached(),
33                 .uid = geteuid(),
34                 .gid = getegid(),
35         };
36         server_process_native_file(&s, sealed_fd, &ucred, tv, label, label_len);
37 
38         unsealed_fd = mkostemp_safe(name);
39         assert_se(unsealed_fd >= 0);
40         assert_se(write(unsealed_fd, data, size) == (ssize_t) size);
41         assert_se(lseek(unsealed_fd, 0, SEEK_SET) == 0);
42         server_process_native_file(&s, unsealed_fd, &ucred, tv, label, label_len);
43 
44         server_done(&s);
45 
46         return 0;
47 }
48