| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
|
| 5 |
#include "bus-util.h" |
| 6 |
#include "env-util.h" |
| 7 |
#include "fd-util.h" |
| 8 |
#include "fuzz.h" |
| 9 |
#include "nulstr-util.h" |
| 10 |
#include "pager.h" |
| 11 |
#include "selinux-util.h" |
| 12 |
#include "static-destruct.h" |
| 13 |
#include "strv.h" |
| 14 |
#include "systemctl.h" |
| 15 |
#include "systemctl-util.h" |
| 16 |
|
| 17 |
static int verb_noop(int argc, char *argv[], uintptr_t _data, void *userdata) { return 0; } |
| 18 |
_alias_(verb_noop) |
| 19 |
verb_add_dependency, |
| 20 |
verb_bind, |
| 21 |
verb_cancel, |
| 22 |
verb_cat, |
| 23 |
verb_clean_or_freeze, |
| 24 |
verb_edit, |
| 25 |
verb_enable, |
| 26 |
verb_get_default, |
| 27 |
verb_import_environment, |
| 28 |
verb_is_active, |
| 29 |
verb_is_enabled, |
| 30 |
verb_is_failed, |
| 31 |
verb_is_system_running, |
| 32 |
verb_kill, |
| 33 |
verb_list_automounts, |
| 34 |
verb_list_dependencies, |
| 35 |
verb_list_jobs, |
| 36 |
verb_list_machines, |
| 37 |
verb_list_paths, |
| 38 |
verb_list_sockets, |
| 39 |
verb_list_timers, |
| 40 |
verb_list_unit_files, |
| 41 |
verb_list_units, |
| 42 |
verb_log_setting, |
| 43 |
verb_mount_image, |
| 44 |
verb_preset_all, |
| 45 |
verb_reset_failed, |
| 46 |
verb_service_log_setting, |
| 47 |
verb_service_watchdogs, |
| 48 |
verb_set_default, |
| 49 |
verb_set_environment, |
| 50 |
verb_set_property, |
| 51 |
verb_show, |
| 52 |
verb_show_environment, |
| 53 |
verb_start_special, |
| 54 |
verb_start_system_special, |
| 55 |
verb_switch_root, |
| 56 |
verb_trivial_method, |
| 57 |
verb_whoami; |
| 58 |
|
| 59 |
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
| 60 |
_cleanup_strv_free_ char **argv = NULL; |
| 61 |
_cleanup_close_ int orig_stdout_fd = -EBADF; |
| 62 |
int r; |
| 63 |
|
| 64 |
if (size > 16*1024) |
| 65 |
return 0; /* See the comment below about the limit for strv_length(). */ |
| 66 |
|
| 67 |
fuzz_setup_logging(); |
| 68 |
|
| 69 |
arg_pager_flags = PAGER_DISABLE; /* We shouldn't execute the pager */ |
| 70 |
|
| 71 |
argv = strv_parse_nulstr((const char *)data, size); |
| 72 |
if (!argv) |
| 73 |
return log_oom(); |
| 74 |
|
| 75 |
if (!argv[0]) |
| 76 |
return 0; /* argv[0] should always be present, but may be zero-length. */ |
| 77 |
if (strv_length(argv) > 1024) |
| 78 |
return 0; /* oss-fuzz reports timeouts which are caused by appending to a very long strv. |
| 79 |
* The code is indeed not very efficient, but it's designed for normal command-line |
| 80 |
* use, where we don't expect more than a dozen of entries. The fact that it is |
| 81 |
* slow with ~100k entries is not particularly interesting. Let's just refuse such |
| 82 |
* long command lines. */ |
| 83 |
|
| 84 |
if (getenv_bool("SYSTEMD_FUZZ_OUTPUT") <= 0) { |
| 85 |
orig_stdout_fd = fcntl(fileno(stdout), F_DUPFD_CLOEXEC, 3); |
| 86 |
if (orig_stdout_fd < 0) |
| 87 |
log_warning_errno(orig_stdout_fd, "Failed to duplicate fd 1: %m"); |
| 88 |
else |
| 89 |
assert_se(freopen("/dev/null", "w", stdout)); |
| 90 |
} |
| 91 |
|
| 92 |
/* We need to reset some global state manually here since libfuzzer feeds a single process with |
| 93 |
* multiple inputs, so we might carry over state from previous invocations that can trigger |
| 94 |
* certain asserts. */ |
| 95 |
arg_transport = BUS_TRANSPORT_LOCAL; |
| 96 |
|
| 97 |
r = systemctl_dispatch_parse_argv(strv_length(argv), argv, |
| 98 |
/* log_level_shift= */ LOG_DEBUG - LOG_ERR, |
| 99 |
/* remaining_args= */ NULL); |
| 100 |
if (r < 0) |
| 101 |
log_error_errno(r, "Failed to parse args: %m"); |
| 102 |
else |
| 103 |
log_info(r == 0 ? "Done!" : "Action!"); |
| 104 |
|
| 105 |
if (orig_stdout_fd >= 0) |
| 106 |
assert_se(freopen(FORMAT_PROC_FD_PATH(orig_stdout_fd), "w", stdout)); |
| 107 |
|
| 108 |
release_busses(); /* We open the bus for communication with logind. |
| 109 |
* It needs to be closed to avoid apparent leaks. */ |
| 110 |
|
| 111 |
mac_selinux_finish(); |
| 112 |
|
| 113 |
/* Call static destructors to do global state cleanup. We do it here, and not in fuzz-main.c so that |
| 114 |
* any global state is destroyed between fuzzer runs. */ |
| 115 |
static_destruct(); |
| 116 |
|
| 117 |
return 0; |
| 118 |
} |