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 CG_PATH "/foo"
22 char bpf_log_buf
[BPF_LOG_BUF_SIZE
];
23 static bool verbose
= false;
27 /* BPF prog properties */
28 struct bpf_insn insns
[MAX_INSNS
];
29 enum bpf_attach_type expected_attach_type
;
30 enum bpf_attach_type attach_type
;
31 /* Socket properties */
34 /* Endpoint to bind() to */
37 /* Expected test result */
46 static struct sock_test tests
[] = {
48 "bind4 load with invalid access: src_ip6",
50 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
),
51 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
52 offsetof(struct bpf_sock
, src_ip6
[0])),
53 BPF_MOV64_IMM(BPF_REG_0
, 1),
56 BPF_CGROUP_INET4_POST_BIND
,
57 BPF_CGROUP_INET4_POST_BIND
,
65 "bind4 load with invalid access: mark",
67 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
),
68 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
69 offsetof(struct bpf_sock
, mark
)),
70 BPF_MOV64_IMM(BPF_REG_0
, 1),
73 BPF_CGROUP_INET4_POST_BIND
,
74 BPF_CGROUP_INET4_POST_BIND
,
82 "bind6 load with invalid access: src_ip4",
84 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
),
85 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
86 offsetof(struct bpf_sock
, src_ip4
)),
87 BPF_MOV64_IMM(BPF_REG_0
, 1),
90 BPF_CGROUP_INET6_POST_BIND
,
91 BPF_CGROUP_INET6_POST_BIND
,
99 "sock_create load with invalid access: src_port",
101 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
),
102 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
103 offsetof(struct bpf_sock
, src_port
)),
104 BPF_MOV64_IMM(BPF_REG_0
, 1),
107 BPF_CGROUP_INET_SOCK_CREATE
,
108 BPF_CGROUP_INET_SOCK_CREATE
,
116 "sock_create load w/o expected_attach_type (compat mode)",
118 BPF_MOV64_IMM(BPF_REG_0
, 1),
122 BPF_CGROUP_INET_SOCK_CREATE
,
130 "sock_create load w/ expected_attach_type",
132 BPF_MOV64_IMM(BPF_REG_0
, 1),
135 BPF_CGROUP_INET_SOCK_CREATE
,
136 BPF_CGROUP_INET_SOCK_CREATE
,
144 "attach type mismatch bind4 vs bind6",
146 BPF_MOV64_IMM(BPF_REG_0
, 1),
149 BPF_CGROUP_INET4_POST_BIND
,
150 BPF_CGROUP_INET6_POST_BIND
,
158 "attach type mismatch bind6 vs bind4",
160 BPF_MOV64_IMM(BPF_REG_0
, 1),
163 BPF_CGROUP_INET6_POST_BIND
,
164 BPF_CGROUP_INET4_POST_BIND
,
172 "attach type mismatch default vs bind4",
174 BPF_MOV64_IMM(BPF_REG_0
, 1),
178 BPF_CGROUP_INET4_POST_BIND
,
186 "attach type mismatch bind6 vs sock_create",
188 BPF_MOV64_IMM(BPF_REG_0
, 1),
191 BPF_CGROUP_INET6_POST_BIND
,
192 BPF_CGROUP_INET_SOCK_CREATE
,
202 BPF_MOV64_IMM(BPF_REG_0
, 0),
205 BPF_CGROUP_INET4_POST_BIND
,
206 BPF_CGROUP_INET4_POST_BIND
,
216 BPF_MOV64_IMM(BPF_REG_0
, 0),
219 BPF_CGROUP_INET6_POST_BIND
,
220 BPF_CGROUP_INET6_POST_BIND
,
228 "bind6 deny specific IP & port",
230 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
),
232 /* if (ip == expected && port == expected) */
233 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
234 offsetof(struct bpf_sock
, src_ip6
[3])),
235 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, 0x01000000, 4),
236 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
237 offsetof(struct bpf_sock
, src_port
)),
238 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, 0x2001, 2),
241 BPF_MOV64_IMM(BPF_REG_0
, 0),
244 /* else return ALLOW; */
245 BPF_MOV64_IMM(BPF_REG_0
, 1),
248 BPF_CGROUP_INET6_POST_BIND
,
249 BPF_CGROUP_INET6_POST_BIND
,
257 "bind4 allow specific IP & port",
259 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
),
261 /* if (ip == expected && port == expected) */
262 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
263 offsetof(struct bpf_sock
, src_ip4
)),
264 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, 0x0100007F, 4),
265 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
266 offsetof(struct bpf_sock
, src_port
)),
267 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, 0x1002, 2),
270 BPF_MOV64_IMM(BPF_REG_0
, 1),
273 /* else return DENY; */
274 BPF_MOV64_IMM(BPF_REG_0
, 0),
277 BPF_CGROUP_INET4_POST_BIND
,
278 BPF_CGROUP_INET4_POST_BIND
,
288 BPF_MOV64_IMM(BPF_REG_0
, 1),
291 BPF_CGROUP_INET4_POST_BIND
,
292 BPF_CGROUP_INET4_POST_BIND
,
302 BPF_MOV64_IMM(BPF_REG_0
, 1),
305 BPF_CGROUP_INET6_POST_BIND
,
306 BPF_CGROUP_INET6_POST_BIND
,
315 static size_t probe_prog_length(const struct bpf_insn
*fp
)
319 for (len
= MAX_INSNS
- 1; len
> 0; --len
)
320 if (fp
[len
].code
!= 0 || fp
[len
].imm
!= 0)
325 static int load_sock_prog(const struct bpf_insn
*prog
,
326 enum bpf_attach_type attach_type
)
328 struct bpf_load_program_attr attr
;
331 memset(&attr
, 0, sizeof(struct bpf_load_program_attr
));
332 attr
.prog_type
= BPF_PROG_TYPE_CGROUP_SOCK
;
333 attr
.expected_attach_type
= attach_type
;
335 attr
.insns_cnt
= probe_prog_length(attr
.insns
);
336 attr
.license
= "GPL";
339 ret
= bpf_load_program_xattr(&attr
, bpf_log_buf
, BPF_LOG_BUF_SIZE
);
340 if (verbose
&& ret
< 0)
341 fprintf(stderr
, "%s\n", bpf_log_buf
);
346 static int attach_sock_prog(int cgfd
, int progfd
,
347 enum bpf_attach_type attach_type
)
349 return bpf_prog_attach(progfd
, cgfd
, attach_type
, BPF_F_ALLOW_OVERRIDE
);
352 static int bind_sock(int domain
, int type
, const char *ip
, unsigned short port
)
354 struct sockaddr_storage addr
;
355 struct sockaddr_in6
*addr6
;
356 struct sockaddr_in
*addr4
;
361 sockfd
= socket(domain
, type
, 0);
365 memset(&addr
, 0, sizeof(addr
));
367 if (domain
== AF_INET
) {
368 len
= sizeof(struct sockaddr_in
);
369 addr4
= (struct sockaddr_in
*)&addr
;
370 addr4
->sin_family
= domain
;
371 addr4
->sin_port
= htons(port
);
372 if (inet_pton(domain
, ip
, (void *)&addr4
->sin_addr
) != 1)
374 } else if (domain
== AF_INET6
) {
375 len
= sizeof(struct sockaddr_in6
);
376 addr6
= (struct sockaddr_in6
*)&addr
;
377 addr6
->sin6_family
= domain
;
378 addr6
->sin6_port
= htons(port
);
379 if (inet_pton(domain
, ip
, (void *)&addr6
->sin6_addr
) != 1)
385 if (bind(sockfd
, (const struct sockaddr
*)&addr
, len
) == -1)
396 static int run_test_case(int cgfd
, const struct sock_test
*test
)
401 printf("Test case: %s .. ", test
->descr
);
402 progfd
= load_sock_prog(test
->insns
, test
->expected_attach_type
);
404 if (test
->result
== LOAD_REJECT
)
410 if (attach_sock_prog(cgfd
, progfd
, test
->attach_type
) == -1) {
411 if (test
->result
== ATTACH_REJECT
)
417 if (bind_sock(test
->domain
, test
->type
, test
->ip
, test
->port
) == -1) {
418 /* sys_bind() may fail for different reasons, errno has to be
419 * checked to confirm that BPF program rejected it.
421 if (test
->result
== BIND_REJECT
&& errno
== EPERM
)
428 if (test
->result
!= SUCCESS
)
435 /* Detaching w/o checking return code: best effort attempt. */
437 bpf_prog_detach(cgfd
, test
->attach_type
);
439 printf("[%s]\n", err
? "FAIL" : "PASS");
443 static int run_tests(int cgfd
)
449 for (i
= 0; i
< ARRAY_SIZE(tests
); ++i
) {
450 if (run_test_case(cgfd
, &tests
[i
]))
455 printf("Summary: %d PASSED, %d FAILED\n", passes
, fails
);
456 return fails
? -1 : 0;
459 int main(int argc
, char **argv
)
464 if (setup_cgroup_environment())
467 cgfd
= create_and_get_cgroup(CG_PATH
);
471 if (join_cgroup(CG_PATH
))
482 cleanup_cgroup_environment();