Skip to content

Commit 228d364

Browse files
hyperenjuKernel Patches Daemon
authored andcommitted
selftests/bpf: add tests for attaching invalid fd
Add test cases for situations where adding the following types of file descriptors to a cpumap entry should fail: - Non-BPF file descriptor (expect -EINVAL) - Nonexistent file descriptor (expect -EBADF) Also tighten the assertion for the expected error when adding a non-BPF_XDP_CPUMAP program to a cpumap entry. Signed-off-by: Kohei Enju <[email protected]>
1 parent 26b213c commit 228d364

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static void test_xdp_with_cpumap_helpers(void)
1818
struct bpf_cpumap_val val = {
1919
.qsize = 192,
2020
};
21-
int err, prog_fd, prog_redir_fd, map_fd;
21+
int err, prog_fd, prog_redir_fd, map_fd, bad_fd;
2222
struct nstoken *nstoken = NULL;
2323
__u32 idx = 0;
2424

@@ -79,7 +79,22 @@ static void test_xdp_with_cpumap_helpers(void)
7979
val.qsize = 192;
8080
val.bpf_prog.fd = bpf_program__fd(skel->progs.xdp_dummy_prog);
8181
err = bpf_map_update_elem(map_fd, &idx, &val, 0);
82-
ASSERT_NEQ(err, 0, "Add non-BPF_XDP_CPUMAP program to cpumap entry");
82+
ASSERT_EQ(err, -EINVAL, "Add non-BPF_XDP_CPUMAP program to cpumap entry");
83+
84+
/* Try to attach non-BPF file descriptor */
85+
bad_fd = open("/dev/null", O_RDONLY);
86+
ASSERT_GE(bad_fd, 0, "Open /dev/null for non-BPF fd");
87+
88+
val.bpf_prog.fd = bad_fd;
89+
err = bpf_map_update_elem(map_fd, &idx, &val, 0);
90+
ASSERT_EQ(err, -EINVAL, "Add non-BPF fd to cpumap entry");
91+
92+
/* Try to attach nonexistent file descriptor */
93+
err = close(bad_fd);
94+
ASSERT_EQ(err, 0, "Close non-BPF fd for nonexistent fd");
95+
96+
err = bpf_map_update_elem(map_fd, &idx, &val, 0);
97+
ASSERT_EQ(err, -EBADF, "Add nonexistent fd to cpumap entry");
8398

8499
/* Try to attach BPF_XDP program with frags to cpumap when we have
85100
* already loaded a BPF_XDP program on the map

0 commit comments

Comments
 (0)