]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-bpf.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / test / test-bpf.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <linux/libbpf.h>
4 #include <string.h>
5 #include <unistd.h>
6
7 #include "bpf-firewall.h"
8 #include "bpf-program.h"
9 #include "load-fragment.h"
10 #include "manager.h"
11 #include "rm-rf.h"
12 #include "service.h"
13 #include "test-helper.h"
14 #include "tests.h"
15 #include "unit.h"
16
17 int main(int argc, char *argv[]) {
18 struct bpf_insn exit_insn[] = {
19 BPF_MOV64_IMM(BPF_REG_0, 1),
20 BPF_EXIT_INSN()
21 };
22
23 _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
24 CGroupContext *cc = NULL;
25 _cleanup_(bpf_program_unrefp) BPFProgram *p = NULL;
26 _cleanup_(manager_freep) Manager *m = NULL;
27 Unit *u;
28 char log_buf[65535];
29 int r;
30
31 test_setup_logging(LOG_DEBUG);
32
33 r = enter_cgroup_subroot();
34 if (r == -ENOMEDIUM)
35 return log_tests_skipped("cgroupfs not available");
36
37 assert_se(set_unit_path(get_testdata_dir()) >= 0);
38 assert_se(runtime_dir = setup_fake_runtime_dir());
39
40 r = bpf_program_new(BPF_PROG_TYPE_CGROUP_SKB, &p);
41 assert(r == 0);
42
43 r = bpf_program_add_instructions(p, exit_insn, ELEMENTSOF(exit_insn));
44 assert(r == 0);
45
46 if (getuid() != 0)
47 return log_tests_skipped("not running as root");
48
49 r = bpf_firewall_supported();
50 if (r == BPF_FIREWALL_UNSUPPORTED)
51 return log_tests_skipped("BPF firewalling not supported");
52 assert_se(r > 0);
53
54 if (r == BPF_FIREWALL_SUPPORTED_WITH_MULTI)
55 log_notice("BPF firewalling with BPF_F_ALLOW_MULTI supported. Yay!");
56 else
57 log_notice("BPF firewalling (though without BPF_F_ALLOW_MULTI) supported. Good.");
58
59 r = bpf_program_load_kernel(p, log_buf, ELEMENTSOF(log_buf));
60 assert(r >= 0);
61
62 p = bpf_program_unref(p);
63
64 /* The simple tests suceeded. Now let's try full unit-based use-case. */
65
66 assert_se(manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_BASIC, &m) >= 0);
67 assert_se(manager_startup(m, NULL, NULL) >= 0);
68
69 assert_se(u = unit_new(m, sizeof(Service)));
70 assert_se(unit_add_name(u, "foo.service") == 0);
71 assert_se(cc = unit_get_cgroup_context(u));
72 u->perpetual = true;
73
74 cc->ip_accounting = true;
75
76 assert_se(config_parse_ip_address_access(u->id, "filename", 1, "Service", 1, "IPAddressAllow", 0, "10.0.1.0/24", &cc->ip_address_allow, NULL) == 0);
77 assert_se(config_parse_ip_address_access(u->id, "filename", 1, "Service", 1, "IPAddressAllow", 0, "127.0.0.2", &cc->ip_address_allow, NULL) == 0);
78 assert_se(config_parse_ip_address_access(u->id, "filename", 1, "Service", 1, "IPAddressDeny", 0, "127.0.0.3", &cc->ip_address_deny, NULL) == 0);
79 assert_se(config_parse_ip_address_access(u->id, "filename", 1, "Service", 1, "IPAddressDeny", 0, "10.0.3.2/24", &cc->ip_address_deny, NULL) == 0);
80 assert_se(config_parse_ip_address_access(u->id, "filename", 1, "Service", 1, "IPAddressDeny", 0, "127.0.0.1/25", &cc->ip_address_deny, NULL) == 0);
81 assert_se(config_parse_ip_address_access(u->id, "filename", 1, "Service", 1, "IPAddressDeny", 0, "127.0.0.4", &cc->ip_address_deny, NULL) == 0);
82
83 assert(cc->ip_address_allow);
84 assert(cc->ip_address_allow->items_next);
85 assert(!cc->ip_address_allow->items_next->items_next);
86
87 /* The deny list is defined redundantly, let's ensure it got properly reduced */
88 assert(cc->ip_address_deny);
89 assert(cc->ip_address_deny->items_next);
90 assert(!cc->ip_address_deny->items_next->items_next);
91
92 assert_se(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) == 0);
93 assert_se(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) == 0);
94
95 assert_se(SERVICE(u)->exec_command[SERVICE_EXEC_START]);
96 assert_se(SERVICE(u)->exec_command[SERVICE_EXEC_START]->command_next);
97 assert_se(!SERVICE(u)->exec_command[SERVICE_EXEC_START]->command_next->command_next);
98
99 SERVICE(u)->type = SERVICE_ONESHOT;
100 u->load_state = UNIT_LOADED;
101
102 unit_dump(u, stdout, NULL);
103
104 r = bpf_firewall_compile(u);
105 if (IN_SET(r, -ENOTTY, -ENOSYS, -EPERM))
106 return log_tests_skipped("Kernel doesn't support the necessary bpf bits (masked out via seccomp?)");
107 assert_se(r >= 0);
108
109 assert(u->ip_bpf_ingress);
110 assert(u->ip_bpf_egress);
111
112 r = bpf_program_load_kernel(u->ip_bpf_ingress, log_buf, ELEMENTSOF(log_buf));
113
114 log_notice("log:");
115 log_notice("-------");
116 log_notice("%s", log_buf);
117 log_notice("-------");
118
119 assert(r >= 0);
120
121 r = bpf_program_load_kernel(u->ip_bpf_egress, log_buf, ELEMENTSOF(log_buf));
122
123 log_notice("log:");
124 log_notice("-------");
125 log_notice("%s", log_buf);
126 log_notice("-------");
127
128 assert(r >= 0);
129
130 assert_se(unit_start(u) >= 0);
131
132 while (!IN_SET(SERVICE(u)->state, SERVICE_DEAD, SERVICE_FAILED))
133 assert_se(sd_event_run(m->event, UINT64_MAX) >= 0);
134
135 assert_se(SERVICE(u)->exec_command[SERVICE_EXEC_START]->exec_status.code == CLD_EXITED &&
136 SERVICE(u)->exec_command[SERVICE_EXEC_START]->exec_status.status == EXIT_SUCCESS);
137
138 assert_se(SERVICE(u)->exec_command[SERVICE_EXEC_START]->command_next->exec_status.code != CLD_EXITED ||
139 SERVICE(u)->exec_command[SERVICE_EXEC_START]->command_next->exec_status.status != EXIT_SUCCESS);
140
141 return 0;
142 }