1 // SPDX-License-Identifier: GPL-2.0
3 #include <test_progs.h>
5 #include "cgroup_helpers.h"
8 #define BAR "/foo/bar/"
9 #define PING_CMD "ping -q -c1 -w1 127.0.0.1 > /dev/null"
11 static char bpf_log_buf
[BPF_LOG_BUF_SIZE
];
13 static int prog_load(int verdict
)
15 struct bpf_insn prog
[] = {
16 BPF_MOV64_IMM(BPF_REG_0
, verdict
), /* r0 = verdict */
19 size_t insns_cnt
= sizeof(prog
) / sizeof(struct bpf_insn
);
21 return bpf_load_program(BPF_PROG_TYPE_CGROUP_SKB
,
22 prog
, insns_cnt
, "GPL", 0,
23 bpf_log_buf
, BPF_LOG_BUF_SIZE
);
26 void test_cgroup_attach_override(void)
28 int drop_prog
= -1, allow_prog
= -1, foo
= -1, bar
= -1;
31 allow_prog
= prog_load(1);
32 if (CHECK(allow_prog
< 0, "prog_load_allow",
33 "verifier output:\n%s\n-------\n", bpf_log_buf
))
36 drop_prog
= prog_load(0);
37 if (CHECK(drop_prog
< 0, "prog_load_drop",
38 "verifier output:\n%s\n-------\n", bpf_log_buf
))
41 foo
= test__join_cgroup(FOO
);
42 if (CHECK(foo
< 0, "cgroup_join_foo", "cgroup setup failed\n"))
45 if (CHECK(bpf_prog_attach(drop_prog
, foo
, BPF_CGROUP_INET_EGRESS
,
46 BPF_F_ALLOW_OVERRIDE
),
47 "prog_attach_drop_foo_override",
48 "attach prog to %s failed, errno=%d\n", FOO
, errno
))
51 if (CHECK(!system(PING_CMD
), "ping_fail",
52 "ping unexpectedly succeeded\n"))
55 bar
= test__join_cgroup(BAR
);
56 if (CHECK(bar
< 0, "cgroup_join_bar", "cgroup setup failed\n"))
59 if (CHECK(!system(PING_CMD
), "ping_fail",
60 "ping unexpectedly succeeded\n"))
63 if (CHECK(bpf_prog_attach(allow_prog
, bar
, BPF_CGROUP_INET_EGRESS
,
64 BPF_F_ALLOW_OVERRIDE
),
65 "prog_attach_allow_bar_override",
66 "attach prog to %s failed, errno=%d\n", BAR
, errno
))
69 if (CHECK(system(PING_CMD
), "ping_ok", "ping failed\n"))
72 if (CHECK(bpf_prog_detach(bar
, BPF_CGROUP_INET_EGRESS
),
74 "detach prog from %s failed, errno=%d\n", BAR
, errno
))
77 if (CHECK(!system(PING_CMD
), "ping_fail",
78 "ping unexpectedly succeeded\n"))
81 if (CHECK(bpf_prog_attach(allow_prog
, bar
, BPF_CGROUP_INET_EGRESS
,
82 BPF_F_ALLOW_OVERRIDE
),
83 "prog_attach_allow_bar_override",
84 "attach prog to %s failed, errno=%d\n", BAR
, errno
))
87 if (CHECK(bpf_prog_detach(foo
, BPF_CGROUP_INET_EGRESS
),
89 "detach prog from %s failed, errno=%d\n", FOO
, errno
))
92 if (CHECK(system(PING_CMD
), "ping_ok", "ping failed\n"))
95 if (CHECK(bpf_prog_attach(allow_prog
, bar
, BPF_CGROUP_INET_EGRESS
,
96 BPF_F_ALLOW_OVERRIDE
),
97 "prog_attach_allow_bar_override",
98 "attach prog to %s failed, errno=%d\n", BAR
, errno
))
101 if (CHECK(!bpf_prog_attach(allow_prog
, bar
, BPF_CGROUP_INET_EGRESS
, 0),
102 "fail_prog_attach_allow_bar_none",
103 "attach prog to %s unexpectedly succeeded\n", BAR
))
106 if (CHECK(bpf_prog_detach(bar
, BPF_CGROUP_INET_EGRESS
),
108 "detach prog from %s failed, errno=%d\n", BAR
, errno
))
111 if (CHECK(!bpf_prog_detach(foo
, BPF_CGROUP_INET_EGRESS
),
112 "fail_prog_detach_foo",
113 "double detach from %s unexpectedly succeeded\n", FOO
))
116 if (CHECK(bpf_prog_attach(allow_prog
, foo
, BPF_CGROUP_INET_EGRESS
, 0),
117 "prog_attach_allow_foo_none",
118 "attach prog to %s failed, errno=%d\n", FOO
, errno
))
121 if (CHECK(!bpf_prog_attach(allow_prog
, bar
, BPF_CGROUP_INET_EGRESS
, 0),
122 "fail_prog_attach_allow_bar_none",
123 "attach prog to %s unexpectedly succeeded\n", BAR
))
126 if (CHECK(!bpf_prog_attach(allow_prog
, bar
, BPF_CGROUP_INET_EGRESS
,
127 BPF_F_ALLOW_OVERRIDE
),
128 "fail_prog_attach_allow_bar_override",
129 "attach prog to %s unexpectedly succeeded\n", BAR
))
132 if (CHECK(!bpf_prog_attach(allow_prog
, foo
, BPF_CGROUP_INET_EGRESS
,
133 BPF_F_ALLOW_OVERRIDE
),
134 "fail_prog_attach_allow_foo_override",
135 "attach prog to %s unexpectedly succeeded\n", FOO
))
138 if (CHECK(bpf_prog_attach(drop_prog
, foo
, BPF_CGROUP_INET_EGRESS
, 0),
139 "prog_attach_drop_foo_none",
140 "attach prog to %s failed, errno=%d\n", FOO
, errno
))