]> git.proxmox.com Git - mirror_ubuntu-kernels.git/commitdiff
selftests/bpf: Migrate recvmsg* return code tests to verifier_sock_addr.c
authorJordan Rife <jrife@google.com>
Fri, 10 May 2024 19:02:18 +0000 (14:02 -0500)
committerAlexei Starovoitov <ast@kernel.org>
Mon, 13 May 2024 00:10:40 +0000 (17:10 -0700)
This set of tests check that the BPF verifier rejects programs with
invalid return codes (recvmsg4 and recvmsg6 hooks can only return 1).
This patch replaces the tests in test_sock_addr.c with
verifier_sock_addr.c, a new verifier prog_tests for sockaddr hooks, in a
step towards fully retiring test_sock_addr.c.

Signed-off-by: Jordan Rife <jrife@google.com>
Link: https://lore.kernel.org/r/20240510190246.3247730-2-jrife@google.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/prog_tests/verifier.c
tools/testing/selftests/bpf/progs/verifier_sock_addr.c [new file with mode: 0644]
tools/testing/selftests/bpf/test_sock_addr.c

index c4f9f306646ed3e7918ae484c78bde4aa56d342e..c60db8beeb734238f4cdb197292dcf1e58578290 100644 (file)
@@ -66,6 +66,7 @@
 #include "verifier_sdiv.skel.h"
 #include "verifier_search_pruning.skel.h"
 #include "verifier_sock.skel.h"
+#include "verifier_sock_addr.skel.h"
 #include "verifier_spill_fill.skel.h"
 #include "verifier_spin_lock.skel.h"
 #include "verifier_stack_ptr.skel.h"
@@ -181,6 +182,7 @@ void test_verifier_scalar_ids(void)           { RUN(verifier_scalar_ids); }
 void test_verifier_sdiv(void)                 { RUN(verifier_sdiv); }
 void test_verifier_search_pruning(void)       { RUN(verifier_search_pruning); }
 void test_verifier_sock(void)                 { RUN(verifier_sock); }
+void test_verifier_sock_addr(void)            { RUN(verifier_sock_addr); }
 void test_verifier_spill_fill(void)           { RUN(verifier_spill_fill); }
 void test_verifier_spin_lock(void)            { RUN(verifier_spin_lock); }
 void test_verifier_stack_ptr(void)            { RUN(verifier_stack_ptr); }
diff --git a/tools/testing/selftests/bpf/progs/verifier_sock_addr.c b/tools/testing/selftests/bpf/progs/verifier_sock_addr.c
new file mode 100644 (file)
index 0000000..5081fa7
--- /dev/null
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2024 Google LLC */
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf_sockopt_helpers.h>
+#include "bpf_misc.h"
+
+SEC("cgroup/recvmsg4")
+__success
+int recvmsg4_good_return_code(struct bpf_sock_addr *ctx)
+{
+       return 1;
+}
+
+SEC("cgroup/recvmsg4")
+__failure __msg("At program exit the register R0 has smin=0 smax=0 should have been in [1, 1]")
+int recvmsg4_bad_return_code(struct bpf_sock_addr *ctx)
+{
+       return 0;
+}
+
+SEC("cgroup/recvmsg6")
+__success
+int recvmsg6_good_return_code(struct bpf_sock_addr *ctx)
+{
+       return 1;
+}
+
+SEC("cgroup/recvmsg6")
+__failure __msg("At program exit the register R0 has smin=0 smax=0 should have been in [1, 1]")
+int recvmsg6_bad_return_code(struct bpf_sock_addr *ctx)
+{
+       return 0;
+}
+
+char _license[] SEC("license") = "GPL";
index aa2198a0f24dd2e36931f441cd3beccbd7c81b27..40e33167bec209b4d1663d26db230a344b20d010 100644 (file)
@@ -94,8 +94,6 @@ static int connect4_prog_load(const struct sock_addr_test *test);
 static int connect6_prog_load(const struct sock_addr_test *test);
 static int sendmsg_allow_prog_load(const struct sock_addr_test *test);
 static int sendmsg_deny_prog_load(const struct sock_addr_test *test);
-static int recvmsg_allow_prog_load(const struct sock_addr_test *test);
-static int recvmsg_deny_prog_load(const struct sock_addr_test *test);
 static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test);
 static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test);
 static int sendmsg6_rw_v4mapped_prog_load(const struct sock_addr_test *test);
@@ -373,64 +371,6 @@ static struct sock_addr_test tests[] = {
                SRC6_REWRITE_IP,
                SYSCALL_EPERM,
        },
-
-       /* recvmsg */
-       {
-               "recvmsg4: return code ok",
-               recvmsg_allow_prog_load,
-               BPF_CGROUP_UDP4_RECVMSG,
-               BPF_CGROUP_UDP4_RECVMSG,
-               AF_INET,
-               SOCK_DGRAM,
-               NULL,
-               0,
-               NULL,
-               0,
-               NULL,
-               ATTACH_OKAY,
-       },
-       {
-               "recvmsg4: return code !ok",
-               recvmsg_deny_prog_load,
-               BPF_CGROUP_UDP4_RECVMSG,
-               BPF_CGROUP_UDP4_RECVMSG,
-               AF_INET,
-               SOCK_DGRAM,
-               NULL,
-               0,
-               NULL,
-               0,
-               NULL,
-               LOAD_REJECT,
-       },
-       {
-               "recvmsg6: return code ok",
-               recvmsg_allow_prog_load,
-               BPF_CGROUP_UDP6_RECVMSG,
-               BPF_CGROUP_UDP6_RECVMSG,
-               AF_INET6,
-               SOCK_DGRAM,
-               NULL,
-               0,
-               NULL,
-               0,
-               NULL,
-               ATTACH_OKAY,
-       },
-       {
-               "recvmsg6: return code !ok",
-               recvmsg_deny_prog_load,
-               BPF_CGROUP_UDP6_RECVMSG,
-               BPF_CGROUP_UDP6_RECVMSG,
-               AF_INET6,
-               SOCK_DGRAM,
-               NULL,
-               0,
-               NULL,
-               0,
-               NULL,
-               LOAD_REJECT,
-       },
 };
 
 static int load_insns(const struct sock_addr_test *test,
@@ -527,16 +467,6 @@ static int sendmsg_deny_prog_load(const struct sock_addr_test *test)
        return xmsg_ret_only_prog_load(test, /*rc*/ 0);
 }
 
-static int recvmsg_allow_prog_load(const struct sock_addr_test *test)
-{
-       return xmsg_ret_only_prog_load(test, /*rc*/ 1);
-}
-
-static int recvmsg_deny_prog_load(const struct sock_addr_test *test)
-{
-       return xmsg_ret_only_prog_load(test, /*rc*/ 0);
-}
-
 static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test)
 {
        struct sockaddr_in dst4_rw_addr;