1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018 Facebook
9 #include <sys/socket.h>
11 #include <linux/filter.h>
15 #include "cgroup_helpers.h"
16 #include "bpf_rlimit.h"
19 # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
22 #define CG_PATH "/foo"
25 char bpf_log_buf[BPF_LOG_BUF_SIZE];
29 /* BPF prog properties */
30 struct bpf_insn insns[MAX_INSNS];
31 enum bpf_attach_type expected_attach_type;
32 enum bpf_attach_type attach_type;
33 /* Socket properties */
36 /* Endpoint to bind() to */
39 /* Expected test result */
48 static struct sock_test tests[] = {
50 "bind4 load with invalid access: src_ip6",
52 BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
53 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
54 offsetof(struct bpf_sock, src_ip6[0])),
55 BPF_MOV64_IMM(BPF_REG_0, 1),
58 BPF_CGROUP_INET4_POST_BIND,
59 BPF_CGROUP_INET4_POST_BIND,
67 "bind4 load with invalid access: mark",
69 BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
70 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
71 offsetof(struct bpf_sock, mark)),
72 BPF_MOV64_IMM(BPF_REG_0, 1),
75 BPF_CGROUP_INET4_POST_BIND,
76 BPF_CGROUP_INET4_POST_BIND,
84 "bind6 load with invalid access: src_ip4",
86 BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
87 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
88 offsetof(struct bpf_sock, src_ip4)),
89 BPF_MOV64_IMM(BPF_REG_0, 1),
92 BPF_CGROUP_INET6_POST_BIND,
93 BPF_CGROUP_INET6_POST_BIND,
101 "sock_create load with invalid access: src_port",
103 BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
104 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
105 offsetof(struct bpf_sock, src_port)),
106 BPF_MOV64_IMM(BPF_REG_0, 1),
109 BPF_CGROUP_INET_SOCK_CREATE,
110 BPF_CGROUP_INET_SOCK_CREATE,
118 "sock_create load w/o expected_attach_type (compat mode)",
120 BPF_MOV64_IMM(BPF_REG_0, 1),
124 BPF_CGROUP_INET_SOCK_CREATE,
132 "sock_create load w/ expected_attach_type",
134 BPF_MOV64_IMM(BPF_REG_0, 1),
137 BPF_CGROUP_INET_SOCK_CREATE,
138 BPF_CGROUP_INET_SOCK_CREATE,
146 "attach type mismatch bind4 vs bind6",
148 BPF_MOV64_IMM(BPF_REG_0, 1),
151 BPF_CGROUP_INET4_POST_BIND,
152 BPF_CGROUP_INET6_POST_BIND,
160 "attach type mismatch bind6 vs bind4",
162 BPF_MOV64_IMM(BPF_REG_0, 1),
165 BPF_CGROUP_INET6_POST_BIND,
166 BPF_CGROUP_INET4_POST_BIND,
174 "attach type mismatch default vs bind4",
176 BPF_MOV64_IMM(BPF_REG_0, 1),
180 BPF_CGROUP_INET4_POST_BIND,
188 "attach type mismatch bind6 vs sock_create",
190 BPF_MOV64_IMM(BPF_REG_0, 1),
193 BPF_CGROUP_INET6_POST_BIND,
194 BPF_CGROUP_INET_SOCK_CREATE,
204 BPF_MOV64_IMM(BPF_REG_0, 0),
207 BPF_CGROUP_INET4_POST_BIND,
208 BPF_CGROUP_INET4_POST_BIND,
218 BPF_MOV64_IMM(BPF_REG_0, 0),
221 BPF_CGROUP_INET6_POST_BIND,
222 BPF_CGROUP_INET6_POST_BIND,
230 "bind6 deny specific IP & port",
232 BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
234 /* if (ip == expected && port == expected) */
235 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
236 offsetof(struct bpf_sock, src_ip6[3])),
237 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, 0x01000000, 4),
238 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
239 offsetof(struct bpf_sock, src_port)),
240 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, 0x2001, 2),
243 BPF_MOV64_IMM(BPF_REG_0, 0),
246 /* else return ALLOW; */
247 BPF_MOV64_IMM(BPF_REG_0, 1),
250 BPF_CGROUP_INET6_POST_BIND,
251 BPF_CGROUP_INET6_POST_BIND,
259 "bind4 allow specific IP & port",
261 BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
263 /* if (ip == expected && port == expected) */
264 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
265 offsetof(struct bpf_sock, src_ip4)),
266 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, 0x0100007F, 4),
267 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
268 offsetof(struct bpf_sock, src_port)),
269 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, 0x1002, 2),
272 BPF_MOV64_IMM(BPF_REG_0, 1),
275 /* else return DENY; */
276 BPF_MOV64_IMM(BPF_REG_0, 0),
279 BPF_CGROUP_INET4_POST_BIND,
280 BPF_CGROUP_INET4_POST_BIND,
290 BPF_MOV64_IMM(BPF_REG_0, 1),
293 BPF_CGROUP_INET4_POST_BIND,
294 BPF_CGROUP_INET4_POST_BIND,
304 BPF_MOV64_IMM(BPF_REG_0, 1),
307 BPF_CGROUP_INET6_POST_BIND,
308 BPF_CGROUP_INET6_POST_BIND,
317 static size_t probe_prog_length(const struct bpf_insn *fp)
321 for (len = MAX_INSNS - 1; len > 0; --len)
322 if (fp[len].code != 0 || fp[len].imm != 0)
327 static int load_sock_prog(const struct bpf_insn *prog,
328 enum bpf_attach_type attach_type)
330 struct bpf_load_program_attr attr;
332 memset(&attr, 0, sizeof(struct bpf_load_program_attr));
333 attr.prog_type = BPF_PROG_TYPE_CGROUP_SOCK;
334 attr.expected_attach_type = attach_type;
336 attr.insns_cnt = probe_prog_length(attr.insns);
337 attr.license = "GPL";
339 return bpf_load_program_xattr(&attr, bpf_log_buf, BPF_LOG_BUF_SIZE);
342 static int attach_sock_prog(int cgfd, int progfd,
343 enum bpf_attach_type attach_type)
345 return bpf_prog_attach(progfd, cgfd, attach_type, BPF_F_ALLOW_OVERRIDE);
348 static int bind_sock(int domain, int type, const char *ip, unsigned short port)
350 struct sockaddr_storage addr;
351 struct sockaddr_in6 *addr6;
352 struct sockaddr_in *addr4;
357 sockfd = socket(domain, type, 0);
361 memset(&addr, 0, sizeof(addr));
363 if (domain == AF_INET) {
364 len = sizeof(struct sockaddr_in);
365 addr4 = (struct sockaddr_in *)&addr;
366 addr4->sin_family = domain;
367 addr4->sin_port = htons(port);
368 if (inet_pton(domain, ip, (void *)&addr4->sin_addr) != 1)
370 } else if (domain == AF_INET6) {
371 len = sizeof(struct sockaddr_in6);
372 addr6 = (struct sockaddr_in6 *)&addr;
373 addr6->sin6_family = domain;
374 addr6->sin6_port = htons(port);
375 if (inet_pton(domain, ip, (void *)&addr6->sin6_addr) != 1)
381 if (bind(sockfd, (const struct sockaddr *)&addr, len) == -1)
392 static int run_test_case(int cgfd, const struct sock_test *test)
397 printf("Test case: %s .. ", test->descr);
398 progfd = load_sock_prog(test->insns, test->expected_attach_type);
400 if (test->result == LOAD_REJECT)
406 if (attach_sock_prog(cgfd, progfd, test->attach_type) == -1) {
407 if (test->result == ATTACH_REJECT)
413 if (bind_sock(test->domain, test->type, test->ip, test->port) == -1) {
414 /* sys_bind() may fail for different reasons, errno has to be
415 * checked to confirm that BPF program rejected it.
417 if (test->result == BIND_REJECT && errno == EPERM)
424 if (test->result != SUCCESS)
431 /* Detaching w/o checking return code: best effort attempt. */
433 bpf_prog_detach(cgfd, test->attach_type);
435 printf("[%s]\n", err ? "FAIL" : "PASS");
439 static int run_tests(int cgfd)
445 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
446 if (run_test_case(cgfd, &tests[i]))
451 printf("Summary: %d PASSED, %d FAILED\n", passes, fails);
452 return fails ? -1 : 0;
455 int main(int argc, char **argv)
460 if (setup_cgroup_environment())
463 cgfd = create_and_get_cgroup(CG_PATH);
467 if (join_cgroup(CG_PATH))
478 cleanup_cgroup_environment();