Skip to content

Commit 43d2b88

Browse files
borkmannAlexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf, selftests: Add test case for mixed cgroup v1/v2
Minimal selftest which implements a small BPF policy program to the connect(2) hook which rejects TCP connection requests to port 60123 with EPERM. This is being attached to a non-root cgroup v2 path. The test asserts that this works under cgroup v2-only and under a mixed cgroup v1/v2 environment where net_classid is set in the former case. Before fix: # ./test_progs -t cgroup_v1v2 test_cgroup_v1v2:PASS:server_fd 0 nsec test_cgroup_v1v2:PASS:client_fd 0 nsec test_cgroup_v1v2:PASS:cgroup_fd 0 nsec test_cgroup_v1v2:PASS:server_fd 0 nsec run_test:PASS:skel_open 0 nsec run_test:PASS:prog_attach 0 nsec test_cgroup_v1v2:PASS:cgroup-v2-only 0 nsec run_test:PASS:skel_open 0 nsec run_test:PASS:prog_attach 0 nsec run_test:PASS:join_classid 0 nsec (network_helpers.c:219: errno: None) Unexpected success to connect to server test_cgroup_v1v2:FAIL:cgroup-v1v2 unexpected error: -1 (errno 0) Rust-for-Linux#27 cgroup_v1v2:FAIL Summary: 0/0 PASSED, 0 SKIPPED, 1 FAILED After fix: # ./test_progs -t cgroup_v1v2 Rust-for-Linux#27 cgroup_v1v2:OK Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Daniel Borkmann <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent d8079d8 commit 43d2b88

File tree

4 files changed

+127
-6
lines changed

4 files changed

+127
-6
lines changed

tools/testing/selftests/bpf/network_helpers.c

+21-6
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,26 @@ int fastopen_connect(int server_fd, const char *data, unsigned int data_len,
208208

209209
static int connect_fd_to_addr(int fd,
210210
const struct sockaddr_storage *addr,
211-
socklen_t addrlen)
211+
socklen_t addrlen, const bool must_fail)
212212
{
213-
if (connect(fd, (const struct sockaddr *)addr, addrlen)) {
214-
log_err("Failed to connect to server");
215-
return -1;
213+
int ret;
214+
215+
errno = 0;
216+
ret = connect(fd, (const struct sockaddr *)addr, addrlen);
217+
if (must_fail) {
218+
if (!ret) {
219+
log_err("Unexpected success to connect to server");
220+
return -1;
221+
}
222+
if (errno != EPERM) {
223+
log_err("Unexpected error from connect to server");
224+
return -1;
225+
}
226+
} else {
227+
if (ret) {
228+
log_err("Failed to connect to server");
229+
return -1;
230+
}
216231
}
217232

218233
return 0;
@@ -257,7 +272,7 @@ int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts)
257272
strlen(opts->cc) + 1))
258273
goto error_close;
259274

260-
if (connect_fd_to_addr(fd, &addr, addrlen))
275+
if (connect_fd_to_addr(fd, &addr, addrlen, opts->must_fail))
261276
goto error_close;
262277

263278
return fd;
@@ -289,7 +304,7 @@ int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms)
289304
return -1;
290305
}
291306

292-
if (connect_fd_to_addr(client_fd, &addr, len))
307+
if (connect_fd_to_addr(client_fd, &addr, len, false))
293308
return -1;
294309

295310
return 0;

tools/testing/selftests/bpf/network_helpers.h

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ typedef __u16 __sum16;
2020
struct network_helper_opts {
2121
const char *cc;
2222
int timeout_ms;
23+
bool must_fail;
2324
};
2425

2526
/* ipv4 test vector */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <test_progs.h>
4+
5+
#include "connect4_dropper.skel.h"
6+
7+
#include "cgroup_helpers.h"
8+
#include "network_helpers.h"
9+
10+
static int run_test(int cgroup_fd, int server_fd, bool classid)
11+
{
12+
struct network_helper_opts opts = {
13+
.must_fail = true,
14+
};
15+
struct connect4_dropper *skel;
16+
int fd, err = 0;
17+
18+
skel = connect4_dropper__open_and_load();
19+
if (!ASSERT_OK_PTR(skel, "skel_open"))
20+
return -1;
21+
22+
skel->links.connect_v4_dropper =
23+
bpf_program__attach_cgroup(skel->progs.connect_v4_dropper,
24+
cgroup_fd);
25+
if (!ASSERT_OK_PTR(skel->links.connect_v4_dropper, "prog_attach")) {
26+
err = -1;
27+
goto out;
28+
}
29+
30+
if (classid && !ASSERT_OK(join_classid(), "join_classid")) {
31+
err = -1;
32+
goto out;
33+
}
34+
35+
fd = connect_to_fd_opts(server_fd, &opts);
36+
if (fd < 0)
37+
err = -1;
38+
else
39+
close(fd);
40+
out:
41+
connect4_dropper__destroy(skel);
42+
return err;
43+
}
44+
45+
void test_cgroup_v1v2(void)
46+
{
47+
struct network_helper_opts opts = {};
48+
int server_fd, client_fd, cgroup_fd;
49+
static const int port = 60123;
50+
51+
/* Step 1: Check base connectivity works without any BPF. */
52+
server_fd = start_server(AF_INET, SOCK_STREAM, NULL, port, 0);
53+
if (!ASSERT_GE(server_fd, 0, "server_fd"))
54+
return;
55+
client_fd = connect_to_fd_opts(server_fd, &opts);
56+
if (!ASSERT_GE(client_fd, 0, "client_fd")) {
57+
close(server_fd);
58+
return;
59+
}
60+
close(client_fd);
61+
close(server_fd);
62+
63+
/* Step 2: Check BPF policy prog attached to cgroups drops connectivity. */
64+
cgroup_fd = test__join_cgroup("/connect_dropper");
65+
if (!ASSERT_GE(cgroup_fd, 0, "cgroup_fd"))
66+
return;
67+
server_fd = start_server(AF_INET, SOCK_STREAM, NULL, port, 0);
68+
if (!ASSERT_GE(server_fd, 0, "server_fd")) {
69+
close(cgroup_fd);
70+
return;
71+
}
72+
ASSERT_OK(run_test(cgroup_fd, server_fd, false), "cgroup-v2-only");
73+
setup_classid_environment();
74+
set_classid(42);
75+
ASSERT_OK(run_test(cgroup_fd, server_fd, true), "cgroup-v1v2");
76+
cleanup_classid_environment();
77+
close(server_fd);
78+
close(cgroup_fd);
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <string.h>
4+
5+
#include <linux/stddef.h>
6+
#include <linux/bpf.h>
7+
8+
#include <sys/socket.h>
9+
10+
#include <bpf/bpf_helpers.h>
11+
#include <bpf/bpf_endian.h>
12+
13+
#define VERDICT_REJECT 0
14+
#define VERDICT_PROCEED 1
15+
16+
SEC("cgroup/connect4")
17+
int connect_v4_dropper(struct bpf_sock_addr *ctx)
18+
{
19+
if (ctx->type != SOCK_STREAM)
20+
return VERDICT_PROCEED;
21+
if (ctx->user_port == bpf_htons(60123))
22+
return VERDICT_REJECT;
23+
return VERDICT_PROCEED;
24+
}
25+
26+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)