]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/bpf: add tests for attaching invalid fd
authorKohei Enju <enjuk@amazon.com>
Mon, 8 Dec 2025 13:14:32 +0000 (22:14 +0900)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 10 Dec 2025 07:53:27 +0000 (23:53 -0800)
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 <enjuk@amazon.com>
Reviewed-by: Toke Høiland-Jørgensen <toke@redhat.com>
Link: https://lore.kernel.org/r/20251208131449.73036-3-enjuk@amazon.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/prog_tests/xdp_cpumap_attach.c

index df27535995af8d5fb9823f114b6ed5ac3847d826..ad56e4370ce38cb09ea83be6f1f51f0b35a73d59 100644 (file)
@@ -18,7 +18,7 @@ static void test_xdp_with_cpumap_helpers(void)
        struct bpf_cpumap_val val = {
                .qsize = 192,
        };
-       int err, prog_fd, prog_redir_fd, map_fd;
+       int err, prog_fd, prog_redir_fd, map_fd, bad_fd;
        struct nstoken *nstoken = NULL;
        __u32 idx = 0;
 
@@ -79,7 +79,22 @@ static void test_xdp_with_cpumap_helpers(void)
        val.qsize = 192;
        val.bpf_prog.fd = bpf_program__fd(skel->progs.xdp_dummy_prog);
        err = bpf_map_update_elem(map_fd, &idx, &val, 0);
-       ASSERT_NEQ(err, 0, "Add non-BPF_XDP_CPUMAP program to cpumap entry");
+       ASSERT_EQ(err, -EINVAL, "Add non-BPF_XDP_CPUMAP program to cpumap entry");
+
+       /* Try to attach non-BPF file descriptor */
+       bad_fd = open("/dev/null", O_RDONLY);
+       ASSERT_GE(bad_fd, 0, "Open /dev/null for non-BPF fd");
+
+       val.bpf_prog.fd = bad_fd;
+       err = bpf_map_update_elem(map_fd, &idx, &val, 0);
+       ASSERT_EQ(err, -EINVAL, "Add non-BPF fd to cpumap entry");
+
+       /* Try to attach nonexistent file descriptor */
+       err = close(bad_fd);
+       ASSERT_EQ(err, 0, "Close non-BPF fd for nonexistent fd");
+
+       err = bpf_map_update_elem(map_fd, &idx, &val, 0);
+       ASSERT_EQ(err, -EBADF, "Add nonexistent fd to cpumap entry");
 
        /* Try to attach BPF_XDP program with frags to cpumap when we have
         * already loaded a BPF_XDP program on the map