Skip to content

Commit b7ea631

Browse files
author
Martin KaFai Lau
committed
Merge branch 'selftests/bpf: convert test_dev_cgroup to test_progs'
Alexis Lothoré (eBPF Foundation) says: ==================== Hello, this small series aims to integrate test_dev_cgroup in test_progs so it could be run automatically in CI. The new version brings a few differences with the current one: - test now uses directly syscalls instead of wrapping commandline tools into system() calls - test_progs manipulates /dev/null (eg: redirecting test logs into it), so disabling access to it in the bpf program confuses the tests. To fix this, the first commit modifies the bpf program to allow access to char devices 1:3 (/dev/null), and disable access to char devices 1:5 (/dev/zero) - once test is converted, add a small subtest to also check for device type interpretation (char or block) - paths used in mknod tests are now in /dev instead of /tmp: due to the CI runner organisation and mountpoints manipulations, trying to create nodes in /tmp leads to errors unrelated to the test (ie, mknod calls refused by kernel, not the bpf program). I don't understand exactly the root cause at the deepest point (all I see in CI is an -ENXIO error on mknod when trying to create the node in tmp, and I can not make sense out of it neither replicate it locally), so I would gladly take inputs from anyone more educated than me about this. The new test_progs part has been tested in a local qemu environment as well as in upstream CI: ./test_progs -a cgroup_dev 47/1 cgroup_dev/allow-mknod:OK 47/2 cgroup_dev/allow-read:OK 47/3 cgroup_dev/allow-write:OK 47/4 cgroup_dev/deny-mknod:OK 47/5 cgroup_dev/deny-read:OK 47/6 cgroup_dev/deny-write:OK 47/7 cgroup_dev/deny-mknod-wrong-type:OK 47 cgroup_dev:OK Summary: 1/7 PASSED, 0 SKIPPED, 0 FAILED --- Changes in v4: - Fix mixup between ret and errno by testing both - Properly apply ack tag from Stanislas - Link to v3: https://lore.kernel.org/r/[email protected] Changes in v3: - delete mknod file only if it has been created - use bpf_program__attach_cgroup() instead of bpf_prog_attach - reorganize subtests order - collect review/ack tags from Alan and Stanislas - Link to v2: https://lore.kernel.org/r/[email protected] Changes in v2: - directly pass expected ret code to subtests instead of boolean pass/not pass - fix faulty fd check in subtest expected to fail on open - fix wrong subtest name - pass test buffer and corresponding size to read/write subtests - use correct series prefix - Link to v1: https://lore.kernel.org/r/[email protected] ==================== Signed-off-by: Martin KaFai Lau <[email protected]>
2 parents 92cc245 + 84cdbff commit b7ea631

File tree

5 files changed

+127
-90
lines changed

5 files changed

+127
-90
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: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
if (test__start_subtest("deny-mknod-wrong-type"))
118+
test_mknod("/dev/test_dev_cgroup_block", S_IFBLK, 1, 3, -1,
119+
EPERM);
120+
121+
cleanup_progs:
122+
dev_cgroup__destroy(skel);
123+
cleanup_cgroup:
124+
cleanup_cgroup_environment();
125+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ int bpf_prog1(struct bpf_cgroup_dev_ctx *ctx)
4141
bpf_trace_printk(fmt, sizeof(fmt), ctx->major, ctx->minor);
4242
#endif
4343

44-
/* Allow access to /dev/zero and /dev/random.
44+
/* Allow access to /dev/null and /dev/urandom.
4545
* Forbid everything else.
4646
*/
4747
if (ctx->major != 1 || type != BPF_DEVCG_DEV_CHAR)
4848
return 0;
4949

5050
switch (ctx->minor) {
51-
case 5: /* 1:5 /dev/zero */
51+
case 3: /* 1:3 /dev/null */
5252
case 9: /* 1:9 /dev/urandom */
5353
return 1;
5454
}

tools/testing/selftests/bpf/test_dev_cgroup.c

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

0 commit comments

Comments
 (0)