Debian Codegraph @

src/test/test-bpf-restrict-fs.c

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