Skip to content

Commit 16c9ccc

Browse files
kkdwivediKernel Patches Daemon
authored andcommitted
selftests/bpf: Add a test for bpf_cgroup_from_id lookup in non-root cgns
Make sure that we only switch the cgroup namespace and enter a new cgroup in a child process separate from test_progs, to not mess up the environment for subsequent tests. Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]>
1 parent f726d3e commit 16c9ccc

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

tools/testing/selftests/bpf/prog_tests/cgrp_kfunc.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#define _GNU_SOURCE
55
#include <cgroup_helpers.h>
66
#include <test_progs.h>
7+
#include <sched.h>
78

89
#include "cgrp_kfunc_failure.skel.h"
910
#include "cgrp_kfunc_success.skel.h"
@@ -87,6 +88,78 @@ static const char * const success_tests[] = {
8788
"test_cgrp_from_id",
8889
};
8990

91+
static void test_cgrp_from_id_ns(void)
92+
{
93+
LIBBPF_OPTS(bpf_test_run_opts, opts);
94+
struct cgrp_kfunc_success *skel;
95+
struct bpf_program *prog;
96+
int fd, pid, pipe_fd[2];
97+
98+
skel = open_load_cgrp_kfunc_skel();
99+
if (!ASSERT_OK_PTR(skel, "open_load_skel"))
100+
return;
101+
102+
if (!ASSERT_OK(skel->bss->err, "pre_mkdir_err"))
103+
goto cleanup;
104+
105+
prog = bpf_object__find_program_by_name(skel->obj, "test_cgrp_from_id_ns");
106+
if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
107+
goto cleanup;
108+
109+
if (!ASSERT_OK(pipe(pipe_fd), "pipe"))
110+
goto cleanup;
111+
112+
pid = fork();
113+
if (!ASSERT_GE(pid, 0, "fork result"))
114+
goto pipe_cleanup;
115+
116+
if (pid == 0) {
117+
int ret = 1;
118+
119+
close(pipe_fd[0]);
120+
fd = create_and_get_cgroup("cgrp_from_id_ns");
121+
if (!ASSERT_GE(fd, 0, "cgrp_fd"))
122+
_exit(1);
123+
124+
if (!ASSERT_OK(join_cgroup("cgrp_from_id_ns"), "join cgrp"))
125+
goto fail;
126+
127+
if (!ASSERT_OK(unshare(CLONE_NEWCGROUP), "unshare cgns"))
128+
goto fail;
129+
130+
ret = bpf_prog_test_run_opts(bpf_program__fd(prog), &opts);
131+
if (!ASSERT_OK(ret, "test run ret"))
132+
goto fail;
133+
134+
remove_cgroup("cgrp_from_id_ns");
135+
136+
if (!ASSERT_OK(opts.retval, "test run retval"))
137+
_exit(1);
138+
ret = 0;
139+
close(fd);
140+
if (!ASSERT_EQ(write(pipe_fd[1], &ret, sizeof(ret)), sizeof(ret), "write pipe"))
141+
_exit(1);
142+
143+
_exit(0);
144+
fail:
145+
remove_cgroup("cgrp_from_id_ns");
146+
_exit(1);
147+
} else {
148+
int res;
149+
150+
close(pipe_fd[1]);
151+
if (!ASSERT_EQ(read(pipe_fd[0], &res, sizeof(res)), sizeof(res), "read res"))
152+
goto pipe_cleanup;
153+
if (!ASSERT_OK(res, "result from run"))
154+
goto pipe_cleanup;
155+
}
156+
157+
pipe_cleanup:
158+
close(pipe_fd[1]);
159+
cleanup:
160+
cgrp_kfunc_success__destroy(skel);
161+
}
162+
90163
void test_cgrp_kfunc(void)
91164
{
92165
int i, err;
@@ -102,6 +175,9 @@ void test_cgrp_kfunc(void)
102175
run_success_test(success_tests[i]);
103176
}
104177

178+
if (test__start_subtest("test_cgrp_from_id_ns"))
179+
test_cgrp_from_id_ns();
180+
105181
RUN_TESTS(cgrp_kfunc_failure);
106182

107183
cleanup:

tools/testing/selftests/bpf/progs/cgrp_kfunc_success.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,15 @@ int BPF_PROG(test_cgrp_from_id, struct cgroup *cgrp, const char *path)
221221

222222
return 0;
223223
}
224+
225+
SEC("syscall")
226+
int test_cgrp_from_id_ns(void *ctx)
227+
{
228+
struct cgroup *cg;
229+
230+
cg = bpf_cgroup_from_id(1);
231+
if (!cg)
232+
return 42;
233+
bpf_cgroup_release(cg);
234+
return 0;
235+
}

0 commit comments

Comments
 (0)