Skip to content

Commit d83d823

Browse files
TropicaoMartin KaFai Lau
authored andcommitted
selftests/bpf: convert test_dev_cgroup to test_progs
test_dev_cgroup is defined as a standalone test program, and so is not executed in CI. Convert it to test_progs framework so it is tested automatically in CI, and remove the old test. In order to be able to run it in test_progs, /dev/null must remain usable, so change the new test to test operations on devices 1:3 as valid, and operations on devices 1:5 (/dev/zero) as invalid. Reviewed-by: Alan Maguire <[email protected]> Acked-by: Stanislav Fomichev <[email protected]> Signed-off-by: Alexis Lothoré (eBPF Foundation) <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]>
1 parent ba6a901 commit d83d823

File tree

4 files changed

+121
-88
lines changed

4 files changed

+121
-88
lines changed

tools/testing/selftests/bpf/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ test_lpm_map
99
test_tag
1010
FEATURE-DUMP.libbpf
1111
fixdep
12-
test_dev_cgroup
1312
/test_progs
1413
/test_progs-no_alu32
1514
/test_progs-bpf_gcc

tools/testing/selftests/bpf/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ endif
6767

6868
# Order correspond to 'make run_tests' order
6969
TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
70-
test_dev_cgroup \
7170
test_sock test_sockmap get_cgroup_id_user \
7271
test_cgroup_storage \
7372
test_tcpnotify_user test_sysctl \
@@ -292,7 +291,6 @@ JSON_WRITER := $(OUTPUT)/json_writer.o
292291
CAP_HELPERS := $(OUTPUT)/cap_helpers.o
293292
NETWORK_HELPERS := $(OUTPUT)/network_helpers.o
294293

295-
$(OUTPUT)/test_dev_cgroup: $(CGROUP_HELPERS) $(TESTING_HELPERS)
296294
$(OUTPUT)/test_skb_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS)
297295
$(OUTPUT)/test_sock: $(CGROUP_HELPERS) $(TESTING_HELPERS)
298296
$(OUTPUT)/test_sockmap: $(CGROUP_HELPERS) $(TESTING_HELPERS)
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <sys/stat.h>
4+
#include <sys/sysmacros.h>
5+
#include <errno.h>
6+
#include "test_progs.h"
7+
#include "cgroup_helpers.h"
8+
#include "dev_cgroup.skel.h"
9+
10+
#define TEST_CGROUP "/test-bpf-based-device-cgroup/"
11+
#define TEST_BUFFER_SIZE 64
12+
13+
static void test_mknod(const char *path, mode_t mode, int dev_major,
14+
int dev_minor, int expected_ret, int expected_errno)
15+
{
16+
int ret;
17+
18+
unlink(path);
19+
ret = mknod(path, mode, makedev(dev_major, dev_minor));
20+
ASSERT_EQ(ret, expected_ret, "mknod");
21+
if (expected_ret)
22+
ASSERT_EQ(errno, expected_errno, "mknod errno");
23+
else
24+
unlink(path);
25+
}
26+
27+
static void test_read(const char *path, char *buf, int buf_size,
28+
int expected_ret, int expected_errno)
29+
{
30+
int ret, fd;
31+
32+
fd = open(path, O_RDONLY);
33+
34+
/* A bare open on unauthorized device should fail */
35+
if (expected_ret < 0) {
36+
ASSERT_EQ(fd, expected_ret, "open ret for read");
37+
ASSERT_EQ(errno, expected_errno, "open errno for read");
38+
if (fd >= 0)
39+
close(fd);
40+
return;
41+
}
42+
43+
if (!ASSERT_OK_FD(fd, "open ret for read"))
44+
return;
45+
46+
ret = read(fd, buf, buf_size);
47+
ASSERT_EQ(ret, expected_ret, "read");
48+
49+
close(fd);
50+
}
51+
52+
static void test_write(const char *path, char *buf, int buf_size,
53+
int expected_ret, int expected_errno)
54+
{
55+
int ret, fd;
56+
57+
fd = open(path, O_WRONLY);
58+
59+
/* A bare open on unauthorized device should fail */
60+
if (expected_ret < 0) {
61+
ASSERT_EQ(fd, expected_ret, "open ret for write");
62+
ASSERT_EQ(errno, expected_errno, "open errno for write");
63+
if (fd >= 0)
64+
close(fd);
65+
return;
66+
}
67+
68+
if (!ASSERT_OK_FD(fd, "open ret for write"))
69+
return;
70+
71+
ret = write(fd, buf, buf_size);
72+
ASSERT_EQ(ret, expected_ret, "write");
73+
74+
close(fd);
75+
}
76+
77+
void test_cgroup_dev(void)
78+
{
79+
char buf[TEST_BUFFER_SIZE] = "some random test data";
80+
struct dev_cgroup *skel;
81+
int cgroup_fd;
82+
83+
cgroup_fd = cgroup_setup_and_join(TEST_CGROUP);
84+
if (!ASSERT_OK_FD(cgroup_fd, "cgroup switch"))
85+
return;
86+
87+
skel = dev_cgroup__open_and_load();
88+
if (!ASSERT_OK_PTR(skel, "load program"))
89+
goto cleanup_cgroup;
90+
91+
skel->links.bpf_prog1 =
92+
bpf_program__attach_cgroup(skel->progs.bpf_prog1, cgroup_fd);
93+
if (!ASSERT_OK_PTR(skel->links.bpf_prog1, "attach_program"))
94+
goto cleanup_progs;
95+
96+
if (test__start_subtest("allow-mknod"))
97+
test_mknod("/dev/test_dev_cgroup_null", S_IFCHR, 1, 3, 0, 0);
98+
99+
if (test__start_subtest("allow-read"))
100+
test_read("/dev/urandom", buf, TEST_BUFFER_SIZE,
101+
TEST_BUFFER_SIZE, 0);
102+
103+
if (test__start_subtest("allow-write"))
104+
test_write("/dev/null", buf, TEST_BUFFER_SIZE,
105+
TEST_BUFFER_SIZE, 0);
106+
107+
if (test__start_subtest("deny-mknod"))
108+
test_mknod("/dev/test_dev_cgroup_zero", S_IFCHR, 1, 5, -1,
109+
EPERM);
110+
111+
if (test__start_subtest("deny-read"))
112+
test_read("/dev/random", buf, TEST_BUFFER_SIZE, -1, EPERM);
113+
114+
if (test__start_subtest("deny-write"))
115+
test_write("/dev/zero", buf, TEST_BUFFER_SIZE, -1, EPERM);
116+
117+
cleanup_progs:
118+
dev_cgroup__destroy(skel);
119+
cleanup_cgroup:
120+
cleanup_cgroup_environment();
121+
}

tools/testing/selftests/bpf/test_dev_cgroup.c

Lines changed: 0 additions & 85 deletions
This file was deleted.

0 commit comments

Comments
 (0)