| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "bpf-socket-bind.h" |
| 4 |
#include "load-fragment.h" |
| 5 |
#include "manager.h" |
| 6 |
#include "path-util.h" |
| 7 |
#include "process-util.h" |
| 8 |
#include "rlimit-util.h" |
| 9 |
#include "rm-rf.h" |
| 10 |
#include "service.h" |
| 11 |
#include "strv.h" |
| 12 |
#include "tests.h" |
| 13 |
#include "unit.h" |
| 14 |
#include "virt.h" |
| 15 |
|
| 16 |
static int find_netcat_executable(char **ret_path) { |
| 17 |
char **candidates = STRV_MAKE("ncat", "nc", "netcat"); |
| 18 |
int r = 0; |
| 19 |
|
| 20 |
STRV_FOREACH(c, candidates) { |
| 21 |
r = find_executable(*c, ret_path); |
| 22 |
if (r == 0) |
| 23 |
break; |
| 24 |
} |
| 25 |
|
| 26 |
return r; |
| 27 |
} |
| 28 |
|
| 29 |
static int test_socket_bind( |
| 30 |
Manager *m, |
| 31 |
const char *unit_name, |
| 32 |
const char *netcat_path, |
| 33 |
const char *port, |
| 34 |
char **allow_rules, |
| 35 |
char **deny_rules) { |
| 36 |
_cleanup_free_ char *exec_start = NULL; |
| 37 |
_cleanup_(unit_freep) Unit *u = NULL; |
| 38 |
CGroupContext *cc = NULL; |
| 39 |
int cld_code, r; |
| 40 |
|
| 41 |
assert_se(u = unit_new(m, sizeof(Service))); |
| 42 |
assert_se(unit_add_name(u, unit_name) == 0); |
| 43 |
assert_se(cc = unit_get_cgroup_context(u)); |
| 44 |
|
| 45 |
STRV_FOREACH(rule, allow_rules) { |
| 46 |
r = config_parse_cgroup_socket_bind( |
| 47 |
u->id, "filename", 1, "Service", 1, "SocketBindAllow", 0, |
| 48 |
*rule, &cc->socket_bind_allow, u); |
| 49 |
if (r < 0) |
| 50 |
return log_unit_error_errno(u, r, "Failed to parse SocketBindAllow: %m"); |
| 51 |
} |
| 52 |
|
| 53 |
fprintf(stderr, "SocketBindAllow: "); |
| 54 |
cgroup_context_dump_socket_bind_items(cc->socket_bind_allow, stderr); |
| 55 |
fputc('\n', stderr); |
| 56 |
|
| 57 |
STRV_FOREACH(rule, deny_rules) { |
| 58 |
r = config_parse_cgroup_socket_bind( |
| 59 |
u->id, "filename", 1, "Service", 1, "SocketBindDeny", 0, |
| 60 |
*rule, &cc->socket_bind_deny, u); |
| 61 |
if (r < 0) |
| 62 |
return log_unit_error_errno(u, r, "Failed to parse SocketBindDeny: %m"); |
| 63 |
} |
| 64 |
|
| 65 |
fprintf(stderr, "SocketBindDeny: "); |
| 66 |
cgroup_context_dump_socket_bind_items(cc->socket_bind_deny, stderr); |
| 67 |
fputc('\n', stderr); |
| 68 |
|
| 69 |
exec_start = strjoin("-timeout --preserve-status -sSIGTERM 1s ", netcat_path, " -l ", port, " -vv"); |
| 70 |
ASSERT_NOT_NULL(exec_start); |
| 71 |
|
| 72 |
r = config_parse_exec(u->id, "filename", 1, "Service", 1, "ExecStart", |
| 73 |
SERVICE_EXEC_START, exec_start, SERVICE(u)->exec_command, u); |
| 74 |
if (r < 0) |
| 75 |
return log_error_errno(r, "Failed to parse ExecStart"); |
| 76 |
|
| 77 |
SERVICE(u)->type = SERVICE_ONESHOT; |
| 78 |
u->load_state = UNIT_LOADED; |
| 79 |
|
| 80 |
ASSERT_OK(unit_patch_contexts(u)); |
| 81 |
r = unit_start(u, NULL); |
| 82 |
if (r < 0) |
| 83 |
return log_error_errno(r, "Unit start failed: %m"); |
| 84 |
|
| 85 |
while (!IN_SET(SERVICE(u)->state, SERVICE_DEAD, SERVICE_FAILED)) { |
| 86 |
r = sd_event_run(m->event, UINT64_MAX); |
| 87 |
if (r < 0) |
| 88 |
return log_error_errno(r, "Event run failed: %m"); |
| 89 |
} |
| 90 |
|
| 91 |
cld_code = SERVICE(u)->exec_command[SERVICE_EXEC_START]->exec_status.code; |
| 92 |
if (cld_code != CLD_EXITED) |
| 93 |
return log_error_errno(SYNTHETIC_ERRNO(EBUSY), "ExecStart didn't exited, code='%s'", sigchld_code_to_string(cld_code)); |
| 94 |
|
| 95 |
if (SERVICE(u)->state != SERVICE_DEAD) |
| 96 |
return log_error_errno(SYNTHETIC_ERRNO(EBUSY), "Service is not dead"); |
| 97 |
|
| 98 |
return 0; |
| 99 |
} |
| 100 |
|
| 101 |
int main(int argc, char *argv[]) { |
| 102 |
_cleanup_free_ char *unit_dir = NULL, *netcat_path = NULL; |
| 103 |
_cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL; |
| 104 |
_cleanup_(manager_freep) Manager *m = NULL; |
| 105 |
struct rlimit rl; |
| 106 |
int r; |
| 107 |
|
| 108 |
test_setup_logging(LOG_DEBUG); |
| 109 |
|
| 110 |
if (detect_container() > 0) |
| 111 |
return log_tests_skipped("test-socket-bind fails inside LXC and Docker containers: https://github.com/systemd/systemd/issues/9666"); |
| 112 |
|
| 113 |
assert_se(getrlimit(RLIMIT_MEMLOCK, &rl) >= 0); |
| 114 |
rl.rlim_cur = rl.rlim_max = MAX(rl.rlim_max, CAN_MEMLOCK_SIZE); |
| 115 |
(void) setrlimit_closest(RLIMIT_MEMLOCK, &rl); |
| 116 |
|
| 117 |
if (!can_memlock()) |
| 118 |
return log_tests_skipped("Can't use mlock()"); |
| 119 |
|
| 120 |
r = bpf_socket_bind_supported(); |
| 121 |
if (r <= 0) |
| 122 |
return log_tests_skipped("socket-bind is not supported"); |
| 123 |
|
| 124 |
if (find_netcat_executable(&netcat_path) != 0) |
| 125 |
return log_tests_skipped("Cannot find netcat executable"); |
| 126 |
|
| 127 |
r = enter_cgroup_subroot(NULL); |
| 128 |
if (r == -ENOMEDIUM) |
| 129 |
return log_tests_skipped("cgroupfs not available"); |
| 130 |
|
| 131 |
ASSERT_OK(get_testdata_dir("units", &unit_dir)); |
| 132 |
ASSERT_OK(setenv_unit_path(unit_dir)); |
| 133 |
assert_se(runtime_dir = setup_fake_runtime_dir()); |
| 134 |
|
| 135 |
assert_se(manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_BASIC, &m) >= 0); |
| 136 |
assert_se(manager_startup(m, NULL, NULL, NULL, NULL) >= 0); |
| 137 |
|
| 138 |
assert_se(test_socket_bind(m, "socket_bind_test.service", netcat_path, "2000", STRV_MAKE("2000"), STRV_MAKE("any")) >= 0); |
| 139 |
assert_se(test_socket_bind(m, "socket_bind_test.service", netcat_path, "2000", STRV_MAKE("ipv6:2001-2002"), STRV_MAKE("any")) >= 0); |
| 140 |
assert_se(test_socket_bind(m, "socket_bind_test.service", netcat_path, "6666", STRV_MAKE("ipv4:6666", "6667"), STRV_MAKE("any")) >= 0); |
| 141 |
assert_se(test_socket_bind(m, "socket_bind_test.service", netcat_path, "6666", STRV_MAKE("6667", "6668", ""), STRV_MAKE("any")) >= 0); |
| 142 |
assert_se(test_socket_bind(m, "socket_bind_test.service", netcat_path, "7777", STRV_EMPTY, STRV_EMPTY) >= 0); |
| 143 |
assert_se(test_socket_bind(m, "socket_bind_test.service", netcat_path, "8888", STRV_MAKE("any"), STRV_MAKE("any")) >= 0); |
| 144 |
assert_se(test_socket_bind(m, "socket_bind_test.service", netcat_path, "8888", STRV_MAKE("ipv6:tcp:8888-8889"), STRV_MAKE("any")) >= 0); |
| 145 |
assert_se(test_socket_bind(m, "socket_bind_test.service", netcat_path, "10000", STRV_MAKE("ipv6:udp:9999-10000"), STRV_MAKE("any")) >= 0); |
| 146 |
assert_se(test_socket_bind(m, "socket_bind_test.service", netcat_path, "6666", STRV_MAKE("ipv4:tcp:6666"), STRV_MAKE("any")) >= 0); |
| 147 |
assert_se(test_socket_bind(m, "socket_bind_test.service", netcat_path, "6666", STRV_MAKE("ipv4:udp:6666"), STRV_MAKE("any")) >= 0); |
| 148 |
assert_se(test_socket_bind(m, "socket_bind_test.service", netcat_path, "6666", STRV_MAKE("tcp:6666"), STRV_MAKE("any")) >= 0); |
| 149 |
|
| 150 |
return 0; |
| 151 |
} |