|
| 1 | +/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 | + |
| 3 | +#include "bpf-lsm.h" |
| 4 | +#include "load-fragment.h" |
| 5 | +#include "manager.h" |
| 6 | +#include "process-util.h" |
| 7 | +#include "rlimit-util.h" |
| 8 | +#include "rm-rf.h" |
| 9 | +#include "service.h" |
| 10 | +#include "strv.h" |
| 11 | +#include "tests.h" |
| 12 | +#include "unit.h" |
| 13 | +#include "virt.h" |
| 14 | + |
| 15 | +static int test_restrict_filesystems(Manager *m, const char *unit_name, const char *file_path, char **allowed_filesystems) { |
| 16 | + _cleanup_free_ char *exec_start = NULL; |
| 17 | + _cleanup_(unit_freep) Unit *u = NULL; |
| 18 | + ExecContext *ec = NULL; |
| 19 | + char **allow_filesystem; |
| 20 | + int cld_code, r; |
| 21 | + |
| 22 | + assert_se(u = unit_new(m, sizeof(Service))); |
| 23 | + assert_se(unit_add_name(u, unit_name) == 0); |
| 24 | + assert_se(ec = unit_get_exec_context(u)); |
| 25 | + |
| 26 | + STRV_FOREACH(allow_filesystem, allowed_filesystems) { |
| 27 | + r = config_parse_restrict_filesystems( |
| 28 | + u->id, "filename", 1, "Service", 1, "RestrictFileSystems", 0, |
| 29 | + *allow_filesystem, ec, u); |
| 30 | + if (r < 0) |
| 31 | + return log_unit_error_errno(u, r, "Failed to parse RestrictFileSystems: %m"); |
| 32 | + } |
| 33 | + |
| 34 | + assert_se(exec_start = strjoin("cat ", file_path)); |
| 35 | + r = config_parse_exec(u->id, "filename", 1, "Service", 1, "ExecStart", |
| 36 | + SERVICE_EXEC_START, exec_start, SERVICE(u)->exec_command, u); |
| 37 | + if (r < 0) |
| 38 | + return log_error_errno(r, "Failed to parse ExecStart"); |
| 39 | + |
| 40 | + SERVICE(u)->type = SERVICE_ONESHOT; |
| 41 | + u->load_state = UNIT_LOADED; |
| 42 | + |
| 43 | + r = unit_start(u); |
| 44 | + if (r < 0) |
| 45 | + return log_error_errno(r, "Unit start failed %m"); |
| 46 | + |
| 47 | + while (!IN_SET(SERVICE(u)->state, SERVICE_DEAD, SERVICE_FAILED)) { |
| 48 | + r = sd_event_run(m->event, UINT64_MAX); |
| 49 | + if (r < 0) |
| 50 | + return log_error_errno(errno, "Event run failed %m"); |
| 51 | + } |
| 52 | + |
| 53 | + cld_code = SERVICE(u)->exec_command[SERVICE_EXEC_START]->exec_status.code; |
| 54 | + if (cld_code != CLD_EXITED) { |
| 55 | + return log_error_errno(-SYNTHETIC_ERRNO(EBUSY), "ExecStart didn't exited, code='%s'", sigchld_code_to_string(cld_code)); |
| 56 | + } |
| 57 | + |
| 58 | + if (SERVICE(u)->state != SERVICE_DEAD) { |
| 59 | + return log_error_errno(-SYNTHETIC_ERRNO(EBUSY), "Service is not dead"); |
| 60 | + } |
| 61 | + |
| 62 | + return 0; |
| 63 | +} |
| 64 | + |
| 65 | +int main(int argc, char *argv[]) { |
| 66 | + _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL; |
| 67 | + _cleanup_(manager_freep) Manager *m = NULL; |
| 68 | + _cleanup_free_ char *unit_dir = NULL; |
| 69 | + struct rlimit rl; |
| 70 | + int r; |
| 71 | + |
| 72 | + test_setup_logging(LOG_DEBUG); |
| 73 | + |
| 74 | + if (getuid() != 0) |
| 75 | + return log_tests_skipped("not running as root"); |
| 76 | + |
| 77 | + assert_se(getrlimit(RLIMIT_MEMLOCK, &rl) >= 0); |
| 78 | + rl.rlim_cur = rl.rlim_max = MAX(rl.rlim_max, CAN_MEMLOCK_SIZE); |
| 79 | + (void) setrlimit_closest(RLIMIT_MEMLOCK, &rl); |
| 80 | + |
| 81 | + if (!can_memlock()) |
| 82 | + return log_tests_skipped("Can't use mlock(), skipping."); |
| 83 | + |
| 84 | + r = lsm_bpf_supported(); |
| 85 | + if (r <= 0) |
| 86 | + return log_tests_skipped("LSM BPF hooks are not supported"); |
| 87 | + |
| 88 | + r = enter_cgroup_subroot(NULL); |
| 89 | + if (r == -ENOMEDIUM) |
| 90 | + return log_tests_skipped("cgroupfs not available"); |
| 91 | + |
| 92 | + assert_se(get_testdata_dir("units", &unit_dir) >= 0); |
| 93 | + assert_se(set_unit_path(unit_dir) >= 0); |
| 94 | + assert_se(runtime_dir = setup_fake_runtime_dir()); |
| 95 | + |
| 96 | + assert_se(manager_new(UNIT_FILE_SYSTEM, MANAGER_TEST_RUN_BASIC, &m) >= 0); |
| 97 | + assert_se(manager_startup(m, NULL, NULL, NULL) >= 0); |
| 98 | + |
| 99 | + /* We need to enable access to the filesystem where the binary is so we |
| 100 | + * add @common-block */ |
| 101 | + assert_se(test_restrict_filesystems(m, "restrict_filesystems_test.service", "/sys/kernel/tracing/printk_formats", STRV_MAKE("@common-block")) < 0); |
| 102 | + assert_se(test_restrict_filesystems(m, "restrict_filesystems_test.service", "/sys/kernel/tracing/printk_formats", STRV_MAKE("tracefs", "@common-block")) >= 0); |
| 103 | + assert_se(test_restrict_filesystems(m, "restrict_filesystems_test.service", "/sys/kernel/tracing/printk_formats", STRV_MAKE("tracefs", "@common-block", "~tracefs")) < 0); |
| 104 | + assert_se(test_restrict_filesystems(m, "restrict_filesystems_test.service", "/sys/kernel/debug/sleep_time", STRV_MAKE("@common-block")) < 0); |
| 105 | + assert_se(test_restrict_filesystems(m, "restrict_filesystems_test.service", "/sys/kernel/debug/sleep_time", STRV_MAKE("debugfs", "@common-block")) >= 0); |
| 106 | + assert_se(test_restrict_filesystems(m, "restrict_filesystems_test.service", "/sys/kernel/debug/sleep_time", STRV_MAKE("~debugfs")) < 0); |
| 107 | + |
| 108 | + return 0; |
| 109 | +} |
0 commit comments