| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/bpf.h> |
| 4 |
#include <linux/bpf_insn.h> |
| 5 |
#include <unistd.h> |
| 6 |
|
| 7 |
#include "bpf-firewall.h" |
| 8 |
#include "bpf-program.h" |
| 9 |
#include "in-addr-prefix-util.h" |
| 10 |
#include "load-fragment.h" |
| 11 |
#include "manager.h" |
| 12 |
#include "path-util.h" |
| 13 |
#include "rm-rf.h" |
| 14 |
#include "service.h" |
| 15 |
#include "set.h" |
| 16 |
#include "tests.h" |
| 17 |
#include "unit-serialize.h" |
| 18 |
#include "virt.h" |
| 19 |
|
| 20 |
int main(int argc, char *argv[]) { |
| 21 |
const struct bpf_insn exit_insn[] = { |
| 22 |
BPF_MOV64_IMM(BPF_REG_0, 0), /* drop */ |
| 23 |
BPF_EXIT_INSN() |
| 24 |
}; |
| 25 |
|
| 26 |
_cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL; |
| 27 |
CGroupContext *cc = NULL; |
| 28 |
_cleanup_(bpf_program_freep) BPFProgram *p = NULL; |
| 29 |
_cleanup_(manager_freep) Manager *m = NULL; |
| 30 |
Unit *u; |
| 31 |
char log_buf[65535]; |
| 32 |
struct rlimit rl; |
| 33 |
int r; |
| 34 |
|
| 35 |
test_setup_logging(LOG_DEBUG); |
| 36 |
|
| 37 |
if (detect_container() > 0) |
| 38 |
return log_tests_skipped("test-bpf-firewall fails inside LXC and Docker containers: https://github.com/systemd/systemd/issues/9666"); |
| 39 |
|
| 40 |
r = bpf_program_supported(); |
| 41 |
if (r < 0) |
| 42 |
return log_tests_skipped_errno(r, "BPF firewalling not supported"); |
| 43 |
ASSERT_TRUE(r); |
| 44 |
|
| 45 |
ASSERT_OK(getrlimit(RLIMIT_MEMLOCK, &rl)); |
| 46 |
rl.rlim_cur = rl.rlim_max = MAX(rl.rlim_max, CAN_MEMLOCK_SIZE); |
| 47 |
(void) setrlimit(RLIMIT_MEMLOCK, &rl); |
| 48 |
|
| 49 |
if (!can_memlock()) |
| 50 |
return log_tests_skipped("Can't use mlock()"); |
| 51 |
|
| 52 |
r = enter_cgroup_subroot(NULL); |
| 53 |
if (r == -ENOMEDIUM) |
| 54 |
return log_tests_skipped("cgroupfs v2 is not mounted"); |
| 55 |
|
| 56 |
r = find_executable("ping", NULL); |
| 57 |
if (r < 0) |
| 58 |
return log_tests_skipped_errno(r, "Can't find ping binary: %m"); |
| 59 |
|
| 60 |
_cleanup_free_ char *unit_dir = NULL; |
| 61 |
ASSERT_OK(get_testdata_dir("units", &unit_dir)); |
| 62 |
ASSERT_OK(setenv_unit_path(unit_dir)); |
| 63 |
ASSERT_NOT_NULL(runtime_dir = setup_fake_runtime_dir()); |
| 64 |
|
| 65 |
ASSERT_OK(bpf_program_new(BPF_PROG_TYPE_CGROUP_SKB, "sd_trivial", &p)); |
| 66 |
ASSERT_OK(bpf_program_add_instructions(p, exit_insn, ELEMENTSOF(exit_insn))); |
| 67 |
ASSERT_OK(bpf_program_load_kernel(p, log_buf, ELEMENTSOF(log_buf))); |
| 68 |
|
| 69 |
const char *test_prog = "/sys/fs/bpf/test-dropper"; |
| 70 |
(void) unlink(test_prog); |
| 71 |
ASSERT_OK(bpf_program_pin(p->kernel_fd, test_prog)); |
| 72 |
|
| 73 |
p = bpf_program_free(p); |
| 74 |
|
| 75 |
/* The simple tests succeeded. Now let's try full unit-based use-case. */ |
| 76 |
|
| 77 |
ASSERT_OK(manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_BASIC, &m)); |
| 78 |
ASSERT_OK(manager_startup(m, NULL, NULL, NULL, NULL)); |
| 79 |
|
| 80 |
ASSERT_NOT_NULL(u = unit_new(m, sizeof(Service))); |
| 81 |
ASSERT_EQ(unit_add_name(u, "foo.service"), 0); |
| 82 |
ASSERT_NOT_NULL(cc = unit_get_cgroup_context(u)); |
| 83 |
u->perpetual = true; |
| 84 |
|
| 85 |
cc->ip_accounting = true; |
| 86 |
|
| 87 |
ASSERT_EQ(config_parse_in_addr_prefixes(u->id, "filename", 1, "Service", 1, "IPAddressAllow", 0, "10.0.1.0/24", &cc->ip_address_allow, NULL), 0); |
| 88 |
ASSERT_EQ(config_parse_in_addr_prefixes(u->id, "filename", 1, "Service", 1, "IPAddressAllow", 0, "127.0.0.2", &cc->ip_address_allow, NULL), 0); |
| 89 |
ASSERT_EQ(config_parse_in_addr_prefixes(u->id, "filename", 1, "Service", 1, "IPAddressDeny", 0, "127.0.0.3", &cc->ip_address_deny, NULL), 0); |
| 90 |
ASSERT_EQ(config_parse_in_addr_prefixes(u->id, "filename", 1, "Service", 1, "IPAddressDeny", 0, "10.0.3.2/24", &cc->ip_address_deny, NULL), 0); |
| 91 |
ASSERT_EQ(config_parse_in_addr_prefixes(u->id, "filename", 1, "Service", 1, "IPAddressDeny", 0, "127.0.0.1/25", &cc->ip_address_deny, NULL), 0); |
| 92 |
ASSERT_EQ(config_parse_in_addr_prefixes(u->id, "filename", 1, "Service", 1, "IPAddressDeny", 0, "127.0.0.4", &cc->ip_address_deny, NULL), 0); |
| 93 |
|
| 94 |
ASSERT_EQ(set_size(cc->ip_address_allow), 2u); |
| 95 |
ASSERT_EQ(set_size(cc->ip_address_deny), 4u); |
| 96 |
|
| 97 |
/* The deny list is defined redundantly, let's ensure it will be properly reduced */ |
| 98 |
ASSERT_OK(in_addr_prefixes_reduce(cc->ip_address_allow)); |
| 99 |
ASSERT_OK(in_addr_prefixes_reduce(cc->ip_address_deny)); |
| 100 |
|
| 101 |
ASSERT_EQ(set_size(cc->ip_address_allow), 2u); |
| 102 |
ASSERT_EQ(set_size(cc->ip_address_deny), 2u); |
| 103 |
|
| 104 |
ASSERT_TRUE(set_contains(cc->ip_address_allow, &(struct in_addr_prefix) { |
| 105 |
.family = AF_INET, |
| 106 |
.address.in.s_addr = htobe32((UINT32_C(10) << 24) | (UINT32_C(1) << 8)), |
| 107 |
.prefixlen = 24 })); |
| 108 |
ASSERT_TRUE(set_contains(cc->ip_address_allow, &(struct in_addr_prefix) { |
| 109 |
.family = AF_INET, |
| 110 |
.address.in.s_addr = htobe32(0x7f000002), |
| 111 |
.prefixlen = 32 })); |
| 112 |
ASSERT_TRUE(set_contains(cc->ip_address_deny, &(struct in_addr_prefix) { |
| 113 |
.family = AF_INET, |
| 114 |
.address.in.s_addr = htobe32(0x7f000000), |
| 115 |
.prefixlen = 25 })); |
| 116 |
ASSERT_TRUE(set_contains(cc->ip_address_deny, &(struct in_addr_prefix) { |
| 117 |
.family = AF_INET, |
| 118 |
.address.in.s_addr = htobe32((UINT32_C(10) << 24) | (UINT32_C(3) << 8)), |
| 119 |
.prefixlen = 24 })); |
| 120 |
|
| 121 |
ASSERT_OK(config_parse_exec(u->id, "filename", 1, "Service", 1, "ExecStart", SERVICE_EXEC_START, "/bin/ping -c 1 127.0.0.2 -W 5", SERVICE(u)->exec_command, u)); |
| 122 |
ASSERT_OK(config_parse_exec(u->id, "filename", 1, "Service", 1, "ExecStart", SERVICE_EXEC_START, "/bin/ping -c 1 127.0.0.3 -W 5", SERVICE(u)->exec_command, u)); |
| 123 |
|
| 124 |
ASSERT_NOT_NULL(SERVICE(u)->exec_command[SERVICE_EXEC_START]); |
| 125 |
ASSERT_NOT_NULL(SERVICE(u)->exec_command[SERVICE_EXEC_START]->command_next); |
| 126 |
ASSERT_NULL(SERVICE(u)->exec_command[SERVICE_EXEC_START]->command_next->command_next); |
| 127 |
|
| 128 |
SERVICE(u)->type = SERVICE_ONESHOT; |
| 129 |
u->load_state = UNIT_LOADED; |
| 130 |
|
| 131 |
CGroupRuntime *crt = ASSERT_PTR(unit_setup_cgroup_runtime(u)); |
| 132 |
|
| 133 |
unit_dump(u, stdout, NULL); |
| 134 |
|
| 135 |
r = bpf_firewall_compile(u); |
| 136 |
if (IN_SET(r, -ENOTTY, -ENOSYS, -EPERM)) |
| 137 |
return log_tests_skipped("Kernel doesn't support the necessary bpf bits (masked out via seccomp?)"); |
| 138 |
ASSERT_OK(r); |
| 139 |
|
| 140 |
ASSERT_NOT_NULL(crt->ip_bpf_ingress); |
| 141 |
ASSERT_NOT_NULL(crt->ip_bpf_egress); |
| 142 |
|
| 143 |
r = bpf_program_load_kernel(crt->ip_bpf_ingress, log_buf, ELEMENTSOF(log_buf)); |
| 144 |
|
| 145 |
log_notice("log:"); |
| 146 |
log_notice("-------"); |
| 147 |
log_notice("%s", log_buf); |
| 148 |
log_notice("-------"); |
| 149 |
|
| 150 |
ASSERT_OK(r); |
| 151 |
|
| 152 |
r = bpf_program_load_kernel(crt->ip_bpf_egress, log_buf, ELEMENTSOF(log_buf)); |
| 153 |
|
| 154 |
log_notice("log:"); |
| 155 |
log_notice("-------"); |
| 156 |
log_notice("%s", log_buf); |
| 157 |
log_notice("-------"); |
| 158 |
|
| 159 |
ASSERT_OK(r); |
| 160 |
|
| 161 |
ASSERT_OK(unit_patch_contexts(u)); |
| 162 |
ASSERT_OK(unit_start(u, NULL)); |
| 163 |
|
| 164 |
while (!IN_SET(SERVICE(u)->state, SERVICE_DEAD, SERVICE_FAILED)) |
| 165 |
ASSERT_OK(sd_event_run(m->event, UINT64_MAX)); |
| 166 |
|
| 167 |
ASSERT_EQ(SERVICE(u)->exec_command[SERVICE_EXEC_START]->exec_status.code, CLD_EXITED); |
| 168 |
ASSERT_EQ(SERVICE(u)->exec_command[SERVICE_EXEC_START]->exec_status.status, EXIT_SUCCESS); |
| 169 |
|
| 170 |
ASSERT_TRUE(SERVICE(u)->exec_command[SERVICE_EXEC_START]->command_next->exec_status.code != CLD_EXITED || |
| 171 |
SERVICE(u)->exec_command[SERVICE_EXEC_START]->command_next->exec_status.status != EXIT_SUCCESS); |
| 172 |
|
| 173 |
/* testing custom filter */ |
| 174 |
ASSERT_NOT_NULL(u = unit_new(m, sizeof(Service))); |
| 175 |
ASSERT_OK(unit_add_name(u, "custom-filter.service")); |
| 176 |
ASSERT_NOT_NULL(cc = unit_get_cgroup_context(u)); |
| 177 |
u->perpetual = true; |
| 178 |
|
| 179 |
cc->ip_accounting = true; |
| 180 |
|
| 181 |
ASSERT_OK(config_parse_ip_filter_bpf_progs(u->id, "filename", 1, "Service", 1, "IPIngressFilterPath", 0, test_prog, &cc->ip_filters_ingress, u)); |
| 182 |
ASSERT_OK(config_parse_exec(u->id, "filename", 1, "Service", 1, "ExecStart", SERVICE_EXEC_START, "-/bin/ping -c 1 127.0.0.1 -W 5", SERVICE(u)->exec_command, u)); |
| 183 |
|
| 184 |
SERVICE(u)->type = SERVICE_ONESHOT; |
| 185 |
u->load_state = UNIT_LOADED; |
| 186 |
|
| 187 |
ASSERT_OK(unit_patch_contexts(u)); |
| 188 |
ASSERT_OK(unit_start(u, NULL)); |
| 189 |
|
| 190 |
while (!IN_SET(SERVICE(u)->state, SERVICE_DEAD, SERVICE_FAILED)) |
| 191 |
ASSERT_OK(sd_event_run(m->event, UINT64_MAX)); |
| 192 |
|
| 193 |
ASSERT_TRUE(SERVICE(u)->exec_command[SERVICE_EXEC_START]->exec_status.code != CLD_EXITED || |
| 194 |
SERVICE(u)->exec_command[SERVICE_EXEC_START]->exec_status.status != EXIT_SUCCESS); |
| 195 |
|
| 196 |
(void) unlink(test_prog); |
| 197 |
ASSERT_EQ(SERVICE(u)->state, SERVICE_DEAD); |
| 198 |
|
| 199 |
return 0; |
| 200 |
} |