Linux 4.19.133
[linux/fpc-iii.git] / tools / testing / selftests / bpf / test_sock_addr.c
blobe38f1cb7089d39b6a8d8fb246fd2b797d59a6ea8
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018 Facebook
4 #define _GNU_SOURCE
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
10 #include <arpa/inet.h>
11 #include <netinet/in.h>
12 #include <sys/types.h>
13 #include <sys/select.h>
14 #include <sys/socket.h>
16 #include <linux/filter.h>
18 #include <bpf/bpf.h>
19 #include <bpf/libbpf.h>
21 #include "cgroup_helpers.h"
22 #include "bpf_rlimit.h"
23 #include "bpf_util.h"
25 #ifndef ENOTSUPP
26 # define ENOTSUPP 524
27 #endif
29 #define CG_PATH "/foo"
30 #define CONNECT4_PROG_PATH "./connect4_prog.o"
31 #define CONNECT6_PROG_PATH "./connect6_prog.o"
32 #define SENDMSG4_PROG_PATH "./sendmsg4_prog.o"
33 #define SENDMSG6_PROG_PATH "./sendmsg6_prog.o"
35 #define SERV4_IP "192.168.1.254"
36 #define SERV4_REWRITE_IP "127.0.0.1"
37 #define SRC4_IP "172.16.0.1"
38 #define SRC4_REWRITE_IP "127.0.0.4"
39 #define SERV4_PORT 4040
40 #define SERV4_REWRITE_PORT 4444
42 #define SERV6_IP "face:b00c:1234:5678::abcd"
43 #define SERV6_REWRITE_IP "::1"
44 #define SERV6_V4MAPPED_IP "::ffff:192.168.0.4"
45 #define SRC6_IP "::1"
46 #define SRC6_REWRITE_IP "::6"
47 #define WILDCARD6_IP "::"
48 #define SERV6_PORT 6060
49 #define SERV6_REWRITE_PORT 6666
51 #define INET_NTOP_BUF 40
53 struct sock_addr_test;
55 typedef int (*load_fn)(const struct sock_addr_test *test);
56 typedef int (*info_fn)(int, struct sockaddr *, socklen_t *);
58 char bpf_log_buf[BPF_LOG_BUF_SIZE];
60 struct sock_addr_test {
61 const char *descr;
62 /* BPF prog properties */
63 load_fn loadfn;
64 enum bpf_attach_type expected_attach_type;
65 enum bpf_attach_type attach_type;
66 /* Socket properties */
67 int domain;
68 int type;
69 /* IP:port pairs for BPF prog to override */
70 const char *requested_ip;
71 unsigned short requested_port;
72 const char *expected_ip;
73 unsigned short expected_port;
74 const char *expected_src_ip;
75 /* Expected test result */
76 enum {
77 LOAD_REJECT,
78 ATTACH_REJECT,
79 SYSCALL_EPERM,
80 SYSCALL_ENOTSUPP,
81 SUCCESS,
82 } expected_result;
85 static int bind4_prog_load(const struct sock_addr_test *test);
86 static int bind6_prog_load(const struct sock_addr_test *test);
87 static int connect4_prog_load(const struct sock_addr_test *test);
88 static int connect6_prog_load(const struct sock_addr_test *test);
89 static int sendmsg_allow_prog_load(const struct sock_addr_test *test);
90 static int sendmsg_deny_prog_load(const struct sock_addr_test *test);
91 static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test);
92 static int sendmsg4_rw_c_prog_load(const struct sock_addr_test *test);
93 static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test);
94 static int sendmsg6_rw_c_prog_load(const struct sock_addr_test *test);
95 static int sendmsg6_rw_v4mapped_prog_load(const struct sock_addr_test *test);
96 static int sendmsg6_rw_wildcard_prog_load(const struct sock_addr_test *test);
98 static struct sock_addr_test tests[] = {
99 /* bind */
101 "bind4: load prog with wrong expected attach type",
102 bind4_prog_load,
103 BPF_CGROUP_INET6_BIND,
104 BPF_CGROUP_INET4_BIND,
105 AF_INET,
106 SOCK_STREAM,
107 NULL,
109 NULL,
111 NULL,
112 LOAD_REJECT,
115 "bind4: attach prog with wrong attach type",
116 bind4_prog_load,
117 BPF_CGROUP_INET4_BIND,
118 BPF_CGROUP_INET6_BIND,
119 AF_INET,
120 SOCK_STREAM,
121 NULL,
123 NULL,
125 NULL,
126 ATTACH_REJECT,
129 "bind4: rewrite IP & TCP port in",
130 bind4_prog_load,
131 BPF_CGROUP_INET4_BIND,
132 BPF_CGROUP_INET4_BIND,
133 AF_INET,
134 SOCK_STREAM,
135 SERV4_IP,
136 SERV4_PORT,
137 SERV4_REWRITE_IP,
138 SERV4_REWRITE_PORT,
139 NULL,
140 SUCCESS,
143 "bind4: rewrite IP & UDP port in",
144 bind4_prog_load,
145 BPF_CGROUP_INET4_BIND,
146 BPF_CGROUP_INET4_BIND,
147 AF_INET,
148 SOCK_DGRAM,
149 SERV4_IP,
150 SERV4_PORT,
151 SERV4_REWRITE_IP,
152 SERV4_REWRITE_PORT,
153 NULL,
154 SUCCESS,
157 "bind6: load prog with wrong expected attach type",
158 bind6_prog_load,
159 BPF_CGROUP_INET4_BIND,
160 BPF_CGROUP_INET6_BIND,
161 AF_INET6,
162 SOCK_STREAM,
163 NULL,
165 NULL,
167 NULL,
168 LOAD_REJECT,
171 "bind6: attach prog with wrong attach type",
172 bind6_prog_load,
173 BPF_CGROUP_INET6_BIND,
174 BPF_CGROUP_INET4_BIND,
175 AF_INET,
176 SOCK_STREAM,
177 NULL,
179 NULL,
181 NULL,
182 ATTACH_REJECT,
185 "bind6: rewrite IP & TCP port in",
186 bind6_prog_load,
187 BPF_CGROUP_INET6_BIND,
188 BPF_CGROUP_INET6_BIND,
189 AF_INET6,
190 SOCK_STREAM,
191 SERV6_IP,
192 SERV6_PORT,
193 SERV6_REWRITE_IP,
194 SERV6_REWRITE_PORT,
195 NULL,
196 SUCCESS,
199 "bind6: rewrite IP & UDP port in",
200 bind6_prog_load,
201 BPF_CGROUP_INET6_BIND,
202 BPF_CGROUP_INET6_BIND,
203 AF_INET6,
204 SOCK_DGRAM,
205 SERV6_IP,
206 SERV6_PORT,
207 SERV6_REWRITE_IP,
208 SERV6_REWRITE_PORT,
209 NULL,
210 SUCCESS,
213 /* connect */
215 "connect4: load prog with wrong expected attach type",
216 connect4_prog_load,
217 BPF_CGROUP_INET6_CONNECT,
218 BPF_CGROUP_INET4_CONNECT,
219 AF_INET,
220 SOCK_STREAM,
221 NULL,
223 NULL,
225 NULL,
226 LOAD_REJECT,
229 "connect4: attach prog with wrong attach type",
230 connect4_prog_load,
231 BPF_CGROUP_INET4_CONNECT,
232 BPF_CGROUP_INET6_CONNECT,
233 AF_INET,
234 SOCK_STREAM,
235 NULL,
237 NULL,
239 NULL,
240 ATTACH_REJECT,
243 "connect4: rewrite IP & TCP port",
244 connect4_prog_load,
245 BPF_CGROUP_INET4_CONNECT,
246 BPF_CGROUP_INET4_CONNECT,
247 AF_INET,
248 SOCK_STREAM,
249 SERV4_IP,
250 SERV4_PORT,
251 SERV4_REWRITE_IP,
252 SERV4_REWRITE_PORT,
253 SRC4_REWRITE_IP,
254 SUCCESS,
257 "connect4: rewrite IP & UDP port",
258 connect4_prog_load,
259 BPF_CGROUP_INET4_CONNECT,
260 BPF_CGROUP_INET4_CONNECT,
261 AF_INET,
262 SOCK_DGRAM,
263 SERV4_IP,
264 SERV4_PORT,
265 SERV4_REWRITE_IP,
266 SERV4_REWRITE_PORT,
267 SRC4_REWRITE_IP,
268 SUCCESS,
271 "connect6: load prog with wrong expected attach type",
272 connect6_prog_load,
273 BPF_CGROUP_INET4_CONNECT,
274 BPF_CGROUP_INET6_CONNECT,
275 AF_INET6,
276 SOCK_STREAM,
277 NULL,
279 NULL,
281 NULL,
282 LOAD_REJECT,
285 "connect6: attach prog with wrong attach type",
286 connect6_prog_load,
287 BPF_CGROUP_INET6_CONNECT,
288 BPF_CGROUP_INET4_CONNECT,
289 AF_INET,
290 SOCK_STREAM,
291 NULL,
293 NULL,
295 NULL,
296 ATTACH_REJECT,
299 "connect6: rewrite IP & TCP port",
300 connect6_prog_load,
301 BPF_CGROUP_INET6_CONNECT,
302 BPF_CGROUP_INET6_CONNECT,
303 AF_INET6,
304 SOCK_STREAM,
305 SERV6_IP,
306 SERV6_PORT,
307 SERV6_REWRITE_IP,
308 SERV6_REWRITE_PORT,
309 SRC6_REWRITE_IP,
310 SUCCESS,
313 "connect6: rewrite IP & UDP port",
314 connect6_prog_load,
315 BPF_CGROUP_INET6_CONNECT,
316 BPF_CGROUP_INET6_CONNECT,
317 AF_INET6,
318 SOCK_DGRAM,
319 SERV6_IP,
320 SERV6_PORT,
321 SERV6_REWRITE_IP,
322 SERV6_REWRITE_PORT,
323 SRC6_REWRITE_IP,
324 SUCCESS,
327 /* sendmsg */
329 "sendmsg4: load prog with wrong expected attach type",
330 sendmsg4_rw_asm_prog_load,
331 BPF_CGROUP_UDP6_SENDMSG,
332 BPF_CGROUP_UDP4_SENDMSG,
333 AF_INET,
334 SOCK_DGRAM,
335 NULL,
337 NULL,
339 NULL,
340 LOAD_REJECT,
343 "sendmsg4: attach prog with wrong attach type",
344 sendmsg4_rw_asm_prog_load,
345 BPF_CGROUP_UDP4_SENDMSG,
346 BPF_CGROUP_UDP6_SENDMSG,
347 AF_INET,
348 SOCK_DGRAM,
349 NULL,
351 NULL,
353 NULL,
354 ATTACH_REJECT,
357 "sendmsg4: rewrite IP & port (asm)",
358 sendmsg4_rw_asm_prog_load,
359 BPF_CGROUP_UDP4_SENDMSG,
360 BPF_CGROUP_UDP4_SENDMSG,
361 AF_INET,
362 SOCK_DGRAM,
363 SERV4_IP,
364 SERV4_PORT,
365 SERV4_REWRITE_IP,
366 SERV4_REWRITE_PORT,
367 SRC4_REWRITE_IP,
368 SUCCESS,
371 "sendmsg4: rewrite IP & port (C)",
372 sendmsg4_rw_c_prog_load,
373 BPF_CGROUP_UDP4_SENDMSG,
374 BPF_CGROUP_UDP4_SENDMSG,
375 AF_INET,
376 SOCK_DGRAM,
377 SERV4_IP,
378 SERV4_PORT,
379 SERV4_REWRITE_IP,
380 SERV4_REWRITE_PORT,
381 SRC4_REWRITE_IP,
382 SUCCESS,
385 "sendmsg4: deny call",
386 sendmsg_deny_prog_load,
387 BPF_CGROUP_UDP4_SENDMSG,
388 BPF_CGROUP_UDP4_SENDMSG,
389 AF_INET,
390 SOCK_DGRAM,
391 SERV4_IP,
392 SERV4_PORT,
393 SERV4_REWRITE_IP,
394 SERV4_REWRITE_PORT,
395 SRC4_REWRITE_IP,
396 SYSCALL_EPERM,
399 "sendmsg6: load prog with wrong expected attach type",
400 sendmsg6_rw_asm_prog_load,
401 BPF_CGROUP_UDP4_SENDMSG,
402 BPF_CGROUP_UDP6_SENDMSG,
403 AF_INET6,
404 SOCK_DGRAM,
405 NULL,
407 NULL,
409 NULL,
410 LOAD_REJECT,
413 "sendmsg6: attach prog with wrong attach type",
414 sendmsg6_rw_asm_prog_load,
415 BPF_CGROUP_UDP6_SENDMSG,
416 BPF_CGROUP_UDP4_SENDMSG,
417 AF_INET6,
418 SOCK_DGRAM,
419 NULL,
421 NULL,
423 NULL,
424 ATTACH_REJECT,
427 "sendmsg6: rewrite IP & port (asm)",
428 sendmsg6_rw_asm_prog_load,
429 BPF_CGROUP_UDP6_SENDMSG,
430 BPF_CGROUP_UDP6_SENDMSG,
431 AF_INET6,
432 SOCK_DGRAM,
433 SERV6_IP,
434 SERV6_PORT,
435 SERV6_REWRITE_IP,
436 SERV6_REWRITE_PORT,
437 SRC6_REWRITE_IP,
438 SUCCESS,
441 "sendmsg6: rewrite IP & port (C)",
442 sendmsg6_rw_c_prog_load,
443 BPF_CGROUP_UDP6_SENDMSG,
444 BPF_CGROUP_UDP6_SENDMSG,
445 AF_INET6,
446 SOCK_DGRAM,
447 SERV6_IP,
448 SERV6_PORT,
449 SERV6_REWRITE_IP,
450 SERV6_REWRITE_PORT,
451 SRC6_REWRITE_IP,
452 SUCCESS,
455 "sendmsg6: IPv4-mapped IPv6",
456 sendmsg6_rw_v4mapped_prog_load,
457 BPF_CGROUP_UDP6_SENDMSG,
458 BPF_CGROUP_UDP6_SENDMSG,
459 AF_INET6,
460 SOCK_DGRAM,
461 SERV6_IP,
462 SERV6_PORT,
463 SERV6_REWRITE_IP,
464 SERV6_REWRITE_PORT,
465 SRC6_REWRITE_IP,
466 SYSCALL_ENOTSUPP,
469 "sendmsg6: set dst IP = [::] (BSD'ism)",
470 sendmsg6_rw_wildcard_prog_load,
471 BPF_CGROUP_UDP6_SENDMSG,
472 BPF_CGROUP_UDP6_SENDMSG,
473 AF_INET6,
474 SOCK_DGRAM,
475 SERV6_IP,
476 SERV6_PORT,
477 SERV6_REWRITE_IP,
478 SERV6_REWRITE_PORT,
479 SRC6_REWRITE_IP,
480 SUCCESS,
483 "sendmsg6: preserve dst IP = [::] (BSD'ism)",
484 sendmsg_allow_prog_load,
485 BPF_CGROUP_UDP6_SENDMSG,
486 BPF_CGROUP_UDP6_SENDMSG,
487 AF_INET6,
488 SOCK_DGRAM,
489 WILDCARD6_IP,
490 SERV6_PORT,
491 SERV6_REWRITE_IP,
492 SERV6_PORT,
493 SRC6_IP,
494 SUCCESS,
497 "sendmsg6: deny call",
498 sendmsg_deny_prog_load,
499 BPF_CGROUP_UDP6_SENDMSG,
500 BPF_CGROUP_UDP6_SENDMSG,
501 AF_INET6,
502 SOCK_DGRAM,
503 SERV6_IP,
504 SERV6_PORT,
505 SERV6_REWRITE_IP,
506 SERV6_REWRITE_PORT,
507 SRC6_REWRITE_IP,
508 SYSCALL_EPERM,
512 static int mk_sockaddr(int domain, const char *ip, unsigned short port,
513 struct sockaddr *addr, socklen_t addr_len)
515 struct sockaddr_in6 *addr6;
516 struct sockaddr_in *addr4;
518 if (domain != AF_INET && domain != AF_INET6) {
519 log_err("Unsupported address family");
520 return -1;
523 memset(addr, 0, addr_len);
525 if (domain == AF_INET) {
526 if (addr_len < sizeof(struct sockaddr_in))
527 return -1;
528 addr4 = (struct sockaddr_in *)addr;
529 addr4->sin_family = domain;
530 addr4->sin_port = htons(port);
531 if (inet_pton(domain, ip, (void *)&addr4->sin_addr) != 1) {
532 log_err("Invalid IPv4: %s", ip);
533 return -1;
535 } else if (domain == AF_INET6) {
536 if (addr_len < sizeof(struct sockaddr_in6))
537 return -1;
538 addr6 = (struct sockaddr_in6 *)addr;
539 addr6->sin6_family = domain;
540 addr6->sin6_port = htons(port);
541 if (inet_pton(domain, ip, (void *)&addr6->sin6_addr) != 1) {
542 log_err("Invalid IPv6: %s", ip);
543 return -1;
547 return 0;
550 static int load_insns(const struct sock_addr_test *test,
551 const struct bpf_insn *insns, size_t insns_cnt)
553 struct bpf_load_program_attr load_attr;
554 int ret;
556 memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
557 load_attr.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
558 load_attr.expected_attach_type = test->expected_attach_type;
559 load_attr.insns = insns;
560 load_attr.insns_cnt = insns_cnt;
561 load_attr.license = "GPL";
563 ret = bpf_load_program_xattr(&load_attr, bpf_log_buf, BPF_LOG_BUF_SIZE);
564 if (ret < 0 && test->expected_result != LOAD_REJECT) {
565 log_err(">>> Loading program error.\n"
566 ">>> Verifier output:\n%s\n-------\n", bpf_log_buf);
569 return ret;
572 /* [1] These testing programs try to read different context fields, including
573 * narrow loads of different sizes from user_ip4 and user_ip6, and write to
574 * those allowed to be overridden.
576 * [2] BPF_LD_IMM64 & BPF_JMP_REG are used below whenever there is a need to
577 * compare a register with unsigned 32bit integer. BPF_JMP_IMM can't be used
578 * in such cases since it accepts only _signed_ 32bit integer as IMM
579 * argument. Also note that BPF_LD_IMM64 contains 2 instructions what matters
580 * to count jumps properly.
583 static int bind4_prog_load(const struct sock_addr_test *test)
585 union {
586 uint8_t u4_addr8[4];
587 uint16_t u4_addr16[2];
588 uint32_t u4_addr32;
589 } ip4;
590 struct sockaddr_in addr4_rw;
592 if (inet_pton(AF_INET, SERV4_IP, (void *)&ip4) != 1) {
593 log_err("Invalid IPv4: %s", SERV4_IP);
594 return -1;
597 if (mk_sockaddr(AF_INET, SERV4_REWRITE_IP, SERV4_REWRITE_PORT,
598 (struct sockaddr *)&addr4_rw, sizeof(addr4_rw)) == -1)
599 return -1;
601 /* See [1]. */
602 struct bpf_insn insns[] = {
603 BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
605 /* if (sk.family == AF_INET && */
606 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
607 offsetof(struct bpf_sock_addr, family)),
608 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, AF_INET, 16),
610 /* (sk.type == SOCK_DGRAM || sk.type == SOCK_STREAM) && */
611 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
612 offsetof(struct bpf_sock_addr, type)),
613 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, SOCK_DGRAM, 1),
614 BPF_JMP_A(1),
615 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, SOCK_STREAM, 12),
617 /* 1st_byte_of_user_ip4 == expected && */
618 BPF_LDX_MEM(BPF_B, BPF_REG_7, BPF_REG_6,
619 offsetof(struct bpf_sock_addr, user_ip4)),
620 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip4.u4_addr8[0], 10),
622 /* 1st_half_of_user_ip4 == expected && */
623 BPF_LDX_MEM(BPF_H, BPF_REG_7, BPF_REG_6,
624 offsetof(struct bpf_sock_addr, user_ip4)),
625 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip4.u4_addr16[0], 8),
627 /* whole_user_ip4 == expected) { */
628 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
629 offsetof(struct bpf_sock_addr, user_ip4)),
630 BPF_LD_IMM64(BPF_REG_8, ip4.u4_addr32), /* See [2]. */
631 BPF_JMP_REG(BPF_JNE, BPF_REG_7, BPF_REG_8, 4),
633 /* user_ip4 = addr4_rw.sin_addr */
634 BPF_MOV32_IMM(BPF_REG_7, addr4_rw.sin_addr.s_addr),
635 BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7,
636 offsetof(struct bpf_sock_addr, user_ip4)),
638 /* user_port = addr4_rw.sin_port */
639 BPF_MOV32_IMM(BPF_REG_7, addr4_rw.sin_port),
640 BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7,
641 offsetof(struct bpf_sock_addr, user_port)),
642 /* } */
644 /* return 1 */
645 BPF_MOV64_IMM(BPF_REG_0, 1),
646 BPF_EXIT_INSN(),
649 return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
652 static int bind6_prog_load(const struct sock_addr_test *test)
654 struct sockaddr_in6 addr6_rw;
655 struct in6_addr ip6;
657 if (inet_pton(AF_INET6, SERV6_IP, (void *)&ip6) != 1) {
658 log_err("Invalid IPv6: %s", SERV6_IP);
659 return -1;
662 if (mk_sockaddr(AF_INET6, SERV6_REWRITE_IP, SERV6_REWRITE_PORT,
663 (struct sockaddr *)&addr6_rw, sizeof(addr6_rw)) == -1)
664 return -1;
666 /* See [1]. */
667 struct bpf_insn insns[] = {
668 BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
670 /* if (sk.family == AF_INET6 && */
671 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
672 offsetof(struct bpf_sock_addr, family)),
673 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, AF_INET6, 18),
675 /* 5th_byte_of_user_ip6 == expected && */
676 BPF_LDX_MEM(BPF_B, BPF_REG_7, BPF_REG_6,
677 offsetof(struct bpf_sock_addr, user_ip6[1])),
678 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip6.s6_addr[4], 16),
680 /* 3rd_half_of_user_ip6 == expected && */
681 BPF_LDX_MEM(BPF_H, BPF_REG_7, BPF_REG_6,
682 offsetof(struct bpf_sock_addr, user_ip6[1])),
683 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip6.s6_addr16[2], 14),
685 /* last_word_of_user_ip6 == expected) { */
686 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
687 offsetof(struct bpf_sock_addr, user_ip6[3])),
688 BPF_LD_IMM64(BPF_REG_8, ip6.s6_addr32[3]), /* See [2]. */
689 BPF_JMP_REG(BPF_JNE, BPF_REG_7, BPF_REG_8, 10),
692 #define STORE_IPV6_WORD(N) \
693 BPF_MOV32_IMM(BPF_REG_7, addr6_rw.sin6_addr.s6_addr32[N]), \
694 BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7, \
695 offsetof(struct bpf_sock_addr, user_ip6[N]))
697 /* user_ip6 = addr6_rw.sin6_addr */
698 STORE_IPV6_WORD(0),
699 STORE_IPV6_WORD(1),
700 STORE_IPV6_WORD(2),
701 STORE_IPV6_WORD(3),
703 /* user_port = addr6_rw.sin6_port */
704 BPF_MOV32_IMM(BPF_REG_7, addr6_rw.sin6_port),
705 BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7,
706 offsetof(struct bpf_sock_addr, user_port)),
708 /* } */
710 /* return 1 */
711 BPF_MOV64_IMM(BPF_REG_0, 1),
712 BPF_EXIT_INSN(),
715 return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
718 static int load_path(const struct sock_addr_test *test, const char *path)
720 struct bpf_prog_load_attr attr;
721 struct bpf_object *obj;
722 int prog_fd;
724 memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
725 attr.file = path;
726 attr.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
727 attr.expected_attach_type = test->expected_attach_type;
729 if (bpf_prog_load_xattr(&attr, &obj, &prog_fd)) {
730 if (test->expected_result != LOAD_REJECT)
731 log_err(">>> Loading program (%s) error.\n", path);
732 return -1;
735 return prog_fd;
738 static int connect4_prog_load(const struct sock_addr_test *test)
740 return load_path(test, CONNECT4_PROG_PATH);
743 static int connect6_prog_load(const struct sock_addr_test *test)
745 return load_path(test, CONNECT6_PROG_PATH);
748 static int sendmsg_ret_only_prog_load(const struct sock_addr_test *test,
749 int32_t rc)
751 struct bpf_insn insns[] = {
752 /* return rc */
753 BPF_MOV64_IMM(BPF_REG_0, rc),
754 BPF_EXIT_INSN(),
756 return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
759 static int sendmsg_allow_prog_load(const struct sock_addr_test *test)
761 return sendmsg_ret_only_prog_load(test, /*rc*/ 1);
764 static int sendmsg_deny_prog_load(const struct sock_addr_test *test)
766 return sendmsg_ret_only_prog_load(test, /*rc*/ 0);
769 static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test)
771 struct sockaddr_in dst4_rw_addr;
772 struct in_addr src4_rw_ip;
774 if (inet_pton(AF_INET, SRC4_REWRITE_IP, (void *)&src4_rw_ip) != 1) {
775 log_err("Invalid IPv4: %s", SRC4_REWRITE_IP);
776 return -1;
779 if (mk_sockaddr(AF_INET, SERV4_REWRITE_IP, SERV4_REWRITE_PORT,
780 (struct sockaddr *)&dst4_rw_addr,
781 sizeof(dst4_rw_addr)) == -1)
782 return -1;
784 struct bpf_insn insns[] = {
785 BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
787 /* if (sk.family == AF_INET && */
788 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
789 offsetof(struct bpf_sock_addr, family)),
790 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, AF_INET, 8),
792 /* sk.type == SOCK_DGRAM) { */
793 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
794 offsetof(struct bpf_sock_addr, type)),
795 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, SOCK_DGRAM, 6),
797 /* msg_src_ip4 = src4_rw_ip */
798 BPF_MOV32_IMM(BPF_REG_7, src4_rw_ip.s_addr),
799 BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7,
800 offsetof(struct bpf_sock_addr, msg_src_ip4)),
802 /* user_ip4 = dst4_rw_addr.sin_addr */
803 BPF_MOV32_IMM(BPF_REG_7, dst4_rw_addr.sin_addr.s_addr),
804 BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7,
805 offsetof(struct bpf_sock_addr, user_ip4)),
807 /* user_port = dst4_rw_addr.sin_port */
808 BPF_MOV32_IMM(BPF_REG_7, dst4_rw_addr.sin_port),
809 BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7,
810 offsetof(struct bpf_sock_addr, user_port)),
811 /* } */
813 /* return 1 */
814 BPF_MOV64_IMM(BPF_REG_0, 1),
815 BPF_EXIT_INSN(),
818 return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
821 static int sendmsg4_rw_c_prog_load(const struct sock_addr_test *test)
823 return load_path(test, SENDMSG4_PROG_PATH);
826 static int sendmsg6_rw_dst_asm_prog_load(const struct sock_addr_test *test,
827 const char *rw_dst_ip)
829 struct sockaddr_in6 dst6_rw_addr;
830 struct in6_addr src6_rw_ip;
832 if (inet_pton(AF_INET6, SRC6_REWRITE_IP, (void *)&src6_rw_ip) != 1) {
833 log_err("Invalid IPv6: %s", SRC6_REWRITE_IP);
834 return -1;
837 if (mk_sockaddr(AF_INET6, rw_dst_ip, SERV6_REWRITE_PORT,
838 (struct sockaddr *)&dst6_rw_addr,
839 sizeof(dst6_rw_addr)) == -1)
840 return -1;
842 struct bpf_insn insns[] = {
843 BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
845 /* if (sk.family == AF_INET6) { */
846 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
847 offsetof(struct bpf_sock_addr, family)),
848 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, AF_INET6, 18),
850 #define STORE_IPV6_WORD_N(DST, SRC, N) \
851 BPF_MOV32_IMM(BPF_REG_7, SRC[N]), \
852 BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7, \
853 offsetof(struct bpf_sock_addr, DST[N]))
855 #define STORE_IPV6(DST, SRC) \
856 STORE_IPV6_WORD_N(DST, SRC, 0), \
857 STORE_IPV6_WORD_N(DST, SRC, 1), \
858 STORE_IPV6_WORD_N(DST, SRC, 2), \
859 STORE_IPV6_WORD_N(DST, SRC, 3)
861 STORE_IPV6(msg_src_ip6, src6_rw_ip.s6_addr32),
862 STORE_IPV6(user_ip6, dst6_rw_addr.sin6_addr.s6_addr32),
864 /* user_port = dst6_rw_addr.sin6_port */
865 BPF_MOV32_IMM(BPF_REG_7, dst6_rw_addr.sin6_port),
866 BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7,
867 offsetof(struct bpf_sock_addr, user_port)),
869 /* } */
871 /* return 1 */
872 BPF_MOV64_IMM(BPF_REG_0, 1),
873 BPF_EXIT_INSN(),
876 return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
879 static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test)
881 return sendmsg6_rw_dst_asm_prog_load(test, SERV6_REWRITE_IP);
884 static int sendmsg6_rw_v4mapped_prog_load(const struct sock_addr_test *test)
886 return sendmsg6_rw_dst_asm_prog_load(test, SERV6_V4MAPPED_IP);
889 static int sendmsg6_rw_wildcard_prog_load(const struct sock_addr_test *test)
891 return sendmsg6_rw_dst_asm_prog_load(test, WILDCARD6_IP);
894 static int sendmsg6_rw_c_prog_load(const struct sock_addr_test *test)
896 return load_path(test, SENDMSG6_PROG_PATH);
899 static int cmp_addr(const struct sockaddr_storage *addr1,
900 const struct sockaddr_storage *addr2, int cmp_port)
902 const struct sockaddr_in *four1, *four2;
903 const struct sockaddr_in6 *six1, *six2;
905 if (addr1->ss_family != addr2->ss_family)
906 return -1;
908 if (addr1->ss_family == AF_INET) {
909 four1 = (const struct sockaddr_in *)addr1;
910 four2 = (const struct sockaddr_in *)addr2;
911 return !((four1->sin_port == four2->sin_port || !cmp_port) &&
912 four1->sin_addr.s_addr == four2->sin_addr.s_addr);
913 } else if (addr1->ss_family == AF_INET6) {
914 six1 = (const struct sockaddr_in6 *)addr1;
915 six2 = (const struct sockaddr_in6 *)addr2;
916 return !((six1->sin6_port == six2->sin6_port || !cmp_port) &&
917 !memcmp(&six1->sin6_addr, &six2->sin6_addr,
918 sizeof(struct in6_addr)));
921 return -1;
924 static int cmp_sock_addr(info_fn fn, int sock1,
925 const struct sockaddr_storage *addr2, int cmp_port)
927 struct sockaddr_storage addr1;
928 socklen_t len1 = sizeof(addr1);
930 memset(&addr1, 0, len1);
931 if (fn(sock1, (struct sockaddr *)&addr1, (socklen_t *)&len1) != 0)
932 return -1;
934 return cmp_addr(&addr1, addr2, cmp_port);
937 static int cmp_local_ip(int sock1, const struct sockaddr_storage *addr2)
939 return cmp_sock_addr(getsockname, sock1, addr2, /*cmp_port*/ 0);
942 static int cmp_local_addr(int sock1, const struct sockaddr_storage *addr2)
944 return cmp_sock_addr(getsockname, sock1, addr2, /*cmp_port*/ 1);
947 static int cmp_peer_addr(int sock1, const struct sockaddr_storage *addr2)
949 return cmp_sock_addr(getpeername, sock1, addr2, /*cmp_port*/ 1);
952 static int start_server(int type, const struct sockaddr_storage *addr,
953 socklen_t addr_len)
955 int fd;
957 fd = socket(addr->ss_family, type, 0);
958 if (fd == -1) {
959 log_err("Failed to create server socket");
960 goto out;
963 if (bind(fd, (const struct sockaddr *)addr, addr_len) == -1) {
964 log_err("Failed to bind server socket");
965 goto close_out;
968 if (type == SOCK_STREAM) {
969 if (listen(fd, 128) == -1) {
970 log_err("Failed to listen on server socket");
971 goto close_out;
975 goto out;
976 close_out:
977 close(fd);
978 fd = -1;
979 out:
980 return fd;
983 static int connect_to_server(int type, const struct sockaddr_storage *addr,
984 socklen_t addr_len)
986 int domain;
987 int fd = -1;
989 domain = addr->ss_family;
991 if (domain != AF_INET && domain != AF_INET6) {
992 log_err("Unsupported address family");
993 goto err;
996 fd = socket(domain, type, 0);
997 if (fd == -1) {
998 log_err("Failed to create client socket");
999 goto err;
1002 if (connect(fd, (const struct sockaddr *)addr, addr_len) == -1) {
1003 log_err("Fail to connect to server");
1004 goto err;
1007 goto out;
1008 err:
1009 close(fd);
1010 fd = -1;
1011 out:
1012 return fd;
1015 int init_pktinfo(int domain, struct cmsghdr *cmsg)
1017 struct in6_pktinfo *pktinfo6;
1018 struct in_pktinfo *pktinfo4;
1020 if (domain == AF_INET) {
1021 cmsg->cmsg_level = SOL_IP;
1022 cmsg->cmsg_type = IP_PKTINFO;
1023 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
1024 pktinfo4 = (struct in_pktinfo *)CMSG_DATA(cmsg);
1025 memset(pktinfo4, 0, sizeof(struct in_pktinfo));
1026 if (inet_pton(domain, SRC4_IP,
1027 (void *)&pktinfo4->ipi_spec_dst) != 1)
1028 return -1;
1029 } else if (domain == AF_INET6) {
1030 cmsg->cmsg_level = SOL_IPV6;
1031 cmsg->cmsg_type = IPV6_PKTINFO;
1032 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1033 pktinfo6 = (struct in6_pktinfo *)CMSG_DATA(cmsg);
1034 memset(pktinfo6, 0, sizeof(struct in6_pktinfo));
1035 if (inet_pton(domain, SRC6_IP,
1036 (void *)&pktinfo6->ipi6_addr) != 1)
1037 return -1;
1038 } else {
1039 return -1;
1042 return 0;
1045 static int sendmsg_to_server(int type, const struct sockaddr_storage *addr,
1046 socklen_t addr_len, int set_cmsg, int flags,
1047 int *syscall_err)
1049 union {
1050 char buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
1051 struct cmsghdr align;
1052 } control6;
1053 union {
1054 char buf[CMSG_SPACE(sizeof(struct in_pktinfo))];
1055 struct cmsghdr align;
1056 } control4;
1057 struct msghdr hdr;
1058 struct iovec iov;
1059 char data = 'a';
1060 int domain;
1061 int fd = -1;
1063 domain = addr->ss_family;
1065 if (domain != AF_INET && domain != AF_INET6) {
1066 log_err("Unsupported address family");
1067 goto err;
1070 fd = socket(domain, type, 0);
1071 if (fd == -1) {
1072 log_err("Failed to create client socket");
1073 goto err;
1076 memset(&iov, 0, sizeof(iov));
1077 iov.iov_base = &data;
1078 iov.iov_len = sizeof(data);
1080 memset(&hdr, 0, sizeof(hdr));
1081 hdr.msg_name = (void *)addr;
1082 hdr.msg_namelen = addr_len;
1083 hdr.msg_iov = &iov;
1084 hdr.msg_iovlen = 1;
1086 if (set_cmsg) {
1087 if (domain == AF_INET) {
1088 hdr.msg_control = &control4;
1089 hdr.msg_controllen = sizeof(control4.buf);
1090 } else if (domain == AF_INET6) {
1091 hdr.msg_control = &control6;
1092 hdr.msg_controllen = sizeof(control6.buf);
1094 if (init_pktinfo(domain, CMSG_FIRSTHDR(&hdr))) {
1095 log_err("Fail to init pktinfo");
1096 goto err;
1100 if (sendmsg(fd, &hdr, flags) != sizeof(data)) {
1101 log_err("Fail to send message to server");
1102 *syscall_err = errno;
1103 goto err;
1106 goto out;
1107 err:
1108 close(fd);
1109 fd = -1;
1110 out:
1111 return fd;
1114 static int fastconnect_to_server(const struct sockaddr_storage *addr,
1115 socklen_t addr_len)
1117 int sendmsg_err;
1119 return sendmsg_to_server(SOCK_STREAM, addr, addr_len, /*set_cmsg*/0,
1120 MSG_FASTOPEN, &sendmsg_err);
1123 static int recvmsg_from_client(int sockfd, struct sockaddr_storage *src_addr)
1125 struct timeval tv;
1126 struct msghdr hdr;
1127 struct iovec iov;
1128 char data[64];
1129 fd_set rfds;
1131 FD_ZERO(&rfds);
1132 FD_SET(sockfd, &rfds);
1134 tv.tv_sec = 2;
1135 tv.tv_usec = 0;
1137 if (select(sockfd + 1, &rfds, NULL, NULL, &tv) <= 0 ||
1138 !FD_ISSET(sockfd, &rfds))
1139 return -1;
1141 memset(&iov, 0, sizeof(iov));
1142 iov.iov_base = data;
1143 iov.iov_len = sizeof(data);
1145 memset(&hdr, 0, sizeof(hdr));
1146 hdr.msg_name = src_addr;
1147 hdr.msg_namelen = sizeof(struct sockaddr_storage);
1148 hdr.msg_iov = &iov;
1149 hdr.msg_iovlen = 1;
1151 return recvmsg(sockfd, &hdr, 0);
1154 static int init_addrs(const struct sock_addr_test *test,
1155 struct sockaddr_storage *requested_addr,
1156 struct sockaddr_storage *expected_addr,
1157 struct sockaddr_storage *expected_src_addr)
1159 socklen_t addr_len = sizeof(struct sockaddr_storage);
1161 if (mk_sockaddr(test->domain, test->expected_ip, test->expected_port,
1162 (struct sockaddr *)expected_addr, addr_len) == -1)
1163 goto err;
1165 if (mk_sockaddr(test->domain, test->requested_ip, test->requested_port,
1166 (struct sockaddr *)requested_addr, addr_len) == -1)
1167 goto err;
1169 if (test->expected_src_ip &&
1170 mk_sockaddr(test->domain, test->expected_src_ip, 0,
1171 (struct sockaddr *)expected_src_addr, addr_len) == -1)
1172 goto err;
1174 return 0;
1175 err:
1176 return -1;
1179 static int run_bind_test_case(const struct sock_addr_test *test)
1181 socklen_t addr_len = sizeof(struct sockaddr_storage);
1182 struct sockaddr_storage requested_addr;
1183 struct sockaddr_storage expected_addr;
1184 int clientfd = -1;
1185 int servfd = -1;
1186 int err = 0;
1188 if (init_addrs(test, &requested_addr, &expected_addr, NULL))
1189 goto err;
1191 servfd = start_server(test->type, &requested_addr, addr_len);
1192 if (servfd == -1)
1193 goto err;
1195 if (cmp_local_addr(servfd, &expected_addr))
1196 goto err;
1198 /* Try to connect to server just in case */
1199 clientfd = connect_to_server(test->type, &expected_addr, addr_len);
1200 if (clientfd == -1)
1201 goto err;
1203 goto out;
1204 err:
1205 err = -1;
1206 out:
1207 close(clientfd);
1208 close(servfd);
1209 return err;
1212 static int run_connect_test_case(const struct sock_addr_test *test)
1214 socklen_t addr_len = sizeof(struct sockaddr_storage);
1215 struct sockaddr_storage expected_src_addr;
1216 struct sockaddr_storage requested_addr;
1217 struct sockaddr_storage expected_addr;
1218 int clientfd = -1;
1219 int servfd = -1;
1220 int err = 0;
1222 if (init_addrs(test, &requested_addr, &expected_addr,
1223 &expected_src_addr))
1224 goto err;
1226 /* Prepare server to connect to */
1227 servfd = start_server(test->type, &expected_addr, addr_len);
1228 if (servfd == -1)
1229 goto err;
1231 clientfd = connect_to_server(test->type, &requested_addr, addr_len);
1232 if (clientfd == -1)
1233 goto err;
1235 /* Make sure src and dst addrs were overridden properly */
1236 if (cmp_peer_addr(clientfd, &expected_addr))
1237 goto err;
1239 if (cmp_local_ip(clientfd, &expected_src_addr))
1240 goto err;
1242 if (test->type == SOCK_STREAM) {
1243 /* Test TCP Fast Open scenario */
1244 clientfd = fastconnect_to_server(&requested_addr, addr_len);
1245 if (clientfd == -1)
1246 goto err;
1248 /* Make sure src and dst addrs were overridden properly */
1249 if (cmp_peer_addr(clientfd, &expected_addr))
1250 goto err;
1252 if (cmp_local_ip(clientfd, &expected_src_addr))
1253 goto err;
1256 goto out;
1257 err:
1258 err = -1;
1259 out:
1260 close(clientfd);
1261 close(servfd);
1262 return err;
1265 static int run_sendmsg_test_case(const struct sock_addr_test *test)
1267 socklen_t addr_len = sizeof(struct sockaddr_storage);
1268 struct sockaddr_storage expected_src_addr;
1269 struct sockaddr_storage requested_addr;
1270 struct sockaddr_storage expected_addr;
1271 struct sockaddr_storage real_src_addr;
1272 int clientfd = -1;
1273 int servfd = -1;
1274 int set_cmsg;
1275 int err = 0;
1277 if (test->type != SOCK_DGRAM)
1278 goto err;
1280 if (init_addrs(test, &requested_addr, &expected_addr,
1281 &expected_src_addr))
1282 goto err;
1284 /* Prepare server to sendmsg to */
1285 servfd = start_server(test->type, &expected_addr, addr_len);
1286 if (servfd == -1)
1287 goto err;
1289 for (set_cmsg = 0; set_cmsg <= 1; ++set_cmsg) {
1290 if (clientfd >= 0)
1291 close(clientfd);
1293 clientfd = sendmsg_to_server(test->type, &requested_addr,
1294 addr_len, set_cmsg, /*flags*/0,
1295 &err);
1296 if (err)
1297 goto out;
1298 else if (clientfd == -1)
1299 goto err;
1301 /* Try to receive message on server instead of using
1302 * getpeername(2) on client socket, to check that client's
1303 * destination address was rewritten properly, since
1304 * getpeername(2) doesn't work with unconnected datagram
1305 * sockets.
1307 * Get source address from recvmsg(2) as well to make sure
1308 * source was rewritten properly: getsockname(2) can't be used
1309 * since socket is unconnected and source defined for one
1310 * specific packet may differ from the one used by default and
1311 * returned by getsockname(2).
1313 if (recvmsg_from_client(servfd, &real_src_addr) == -1)
1314 goto err;
1316 if (cmp_addr(&real_src_addr, &expected_src_addr, /*cmp_port*/0))
1317 goto err;
1320 goto out;
1321 err:
1322 err = -1;
1323 out:
1324 close(clientfd);
1325 close(servfd);
1326 return err;
1329 static int run_test_case(int cgfd, const struct sock_addr_test *test)
1331 int progfd = -1;
1332 int err = 0;
1334 printf("Test case: %s .. ", test->descr);
1336 progfd = test->loadfn(test);
1337 if (test->expected_result == LOAD_REJECT && progfd < 0)
1338 goto out;
1339 else if (test->expected_result == LOAD_REJECT || progfd < 0)
1340 goto err;
1342 err = bpf_prog_attach(progfd, cgfd, test->attach_type,
1343 BPF_F_ALLOW_OVERRIDE);
1344 if (test->expected_result == ATTACH_REJECT && err) {
1345 err = 0; /* error was expected, reset it */
1346 goto out;
1347 } else if (test->expected_result == ATTACH_REJECT || err) {
1348 goto err;
1351 switch (test->attach_type) {
1352 case BPF_CGROUP_INET4_BIND:
1353 case BPF_CGROUP_INET6_BIND:
1354 err = run_bind_test_case(test);
1355 break;
1356 case BPF_CGROUP_INET4_CONNECT:
1357 case BPF_CGROUP_INET6_CONNECT:
1358 err = run_connect_test_case(test);
1359 break;
1360 case BPF_CGROUP_UDP4_SENDMSG:
1361 case BPF_CGROUP_UDP6_SENDMSG:
1362 err = run_sendmsg_test_case(test);
1363 break;
1364 default:
1365 goto err;
1368 if (test->expected_result == SYSCALL_EPERM && err == EPERM) {
1369 err = 0; /* error was expected, reset it */
1370 goto out;
1373 if (test->expected_result == SYSCALL_ENOTSUPP && err == ENOTSUPP) {
1374 err = 0; /* error was expected, reset it */
1375 goto out;
1378 if (err || test->expected_result != SUCCESS)
1379 goto err;
1381 goto out;
1382 err:
1383 err = -1;
1384 out:
1385 /* Detaching w/o checking return code: best effort attempt. */
1386 if (progfd != -1)
1387 bpf_prog_detach(cgfd, test->attach_type);
1388 close(progfd);
1389 printf("[%s]\n", err ? "FAIL" : "PASS");
1390 return err;
1393 static int run_tests(int cgfd)
1395 int passes = 0;
1396 int fails = 0;
1397 int i;
1399 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
1400 if (run_test_case(cgfd, &tests[i]))
1401 ++fails;
1402 else
1403 ++passes;
1405 printf("Summary: %d PASSED, %d FAILED\n", passes, fails);
1406 return fails ? -1 : 0;
1409 int main(int argc, char **argv)
1411 int cgfd = -1;
1412 int err = 0;
1414 if (argc < 2) {
1415 fprintf(stderr,
1416 "%s has to be run via %s.sh. Skip direct run.\n",
1417 argv[0], argv[0]);
1418 exit(err);
1421 if (setup_cgroup_environment())
1422 goto err;
1424 cgfd = create_and_get_cgroup(CG_PATH);
1425 if (!cgfd)
1426 goto err;
1428 if (join_cgroup(CG_PATH))
1429 goto err;
1431 if (run_tests(cgfd))
1432 goto err;
1434 goto out;
1435 err:
1436 err = -1;
1437 out:
1438 close(cgfd);
1439 cleanup_cgroup_environment();
1440 return err;