]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/systemctl/fuzz-systemctl-parse-argv.c
Merge pull request #23351 from keszybz/logind-message
[thirdparty/systemd.git] / src / systemctl / fuzz-systemctl-parse-argv.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4 #include <unistd.h>
5
6 #include "env-util.h"
7 #include "fd-util.h"
8 #include "fuzz.h"
9 #include "selinux-util.h"
10 #include "static-destruct.h"
11 #include "stdio-util.h"
12 #include "strv.h"
13 #include "systemctl.h"
14 #include "systemctl-util.h"
15
16 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
17 _cleanup_strv_free_ char **argv = NULL;
18 _cleanup_close_ int orig_stdout_fd = -1;
19 int r;
20
21 /* We don't want to fill the logs with messages about parse errors.
22 * Disable most logging if not running standalone */
23 if (!getenv("SYSTEMD_LOG_LEVEL"))
24 log_set_max_level(LOG_CRIT);
25
26 arg_pager_flags = PAGER_DISABLE; /* We shouldn't execute the pager */
27
28 argv = strv_parse_nulstr((const char *)data, size);
29 if (!argv)
30 return log_oom();
31
32 if (!argv[0])
33 return 0; /* argv[0] should always be present, but may be zero-length. */
34 if (strv_length(argv) > 1024)
35 return 0; /* oss-fuzz reports timeouts which are caused by appending to a very long strv.
36 * The code is indeed not very efficient, but it's designed for normal command-line
37 * use, where we don't expect more than a dozen of entries. The fact that it is
38 * slow with ~100k entries is not particularly interesting. Let's just refuse such
39 * long command lines. */
40
41 if (getenv_bool("SYSTEMD_FUZZ_OUTPUT") <= 0) {
42 orig_stdout_fd = fcntl(fileno(stdout), F_DUPFD_CLOEXEC, 3);
43 if (orig_stdout_fd < 0)
44 log_warning_errno(orig_stdout_fd, "Failed to duplicate fd 1: %m");
45 else
46 assert_se(freopen("/dev/null", "w", stdout));
47
48 opterr = 0; /* do not print errors */
49 }
50
51 optind = 0; /* this tells the getopt machinery to reinitialize */
52
53 r = systemctl_dispatch_parse_argv(strv_length(argv), argv);
54 if (r < 0)
55 log_error_errno(r, "Failed to parse args: %m");
56 else
57 log_info(r == 0 ? "Done!" : "Action!");
58
59 if (orig_stdout_fd >= 0)
60 assert_se(freopen(FORMAT_PROC_FD_PATH(orig_stdout_fd), "w", stdout));
61
62 release_busses(); /* We open the bus for communication with logind.
63 * It needs to be closed to avoid apparent leaks. */
64
65 mac_selinux_finish();
66
67 /* Call static destructors to do global state cleanup. We do it here, and not in fuzz-main.c so that
68 * any global state is destroyed between fuzzer runs. */
69 static_destruct();
70
71 return 0;
72 }