]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
selftests/bpf: verifier/map_ret_val.c converted to inline assembly
authorEduard Zingerman <eddyz87@gmail.com>
Sat, 25 Mar 2023 02:55:09 +0000 (04:55 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Sun, 26 Mar 2023 00:02:05 +0000 (17:02 -0700)
Test verifier/map_ret_val.c automatically converted to use inline assembly.

Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20230325025524.144043-29-eddyz87@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/prog_tests/verifier.c
tools/testing/selftests/bpf/progs/verifier_map_ret_val.c [new file with mode: 0644]
tools/testing/selftests/bpf/verifier/map_ret_val.c [deleted file]

index d2f3bff0e9425b31c513a9c5d1ee7b61c6842a0a..5131a73fd22589fa7601476b042b6173a74c18c0 100644 (file)
@@ -25,6 +25,7 @@
 #include "verifier_ld_ind.skel.h"
 #include "verifier_leak_ptr.skel.h"
 #include "verifier_map_ptr.skel.h"
+#include "verifier_map_ret_val.skel.h"
 
 __maybe_unused
 static void run_tests_aux(const char *skel_name, skel_elf_bytes_fn elf_bytes_factory)
@@ -72,3 +73,4 @@ void test_verifier_int_ptr(void)              { RUN(verifier_int_ptr); }
 void test_verifier_ld_ind(void)               { RUN(verifier_ld_ind); }
 void test_verifier_leak_ptr(void)             { RUN(verifier_leak_ptr); }
 void test_verifier_map_ptr(void)              { RUN(verifier_map_ptr); }
+void test_verifier_map_ret_val(void)          { RUN(verifier_map_ret_val); }
diff --git a/tools/testing/selftests/bpf/progs/verifier_map_ret_val.c b/tools/testing/selftests/bpf/progs/verifier_map_ret_val.c
new file mode 100644 (file)
index 0000000..1639628
--- /dev/null
@@ -0,0 +1,110 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Converted from tools/testing/selftests/bpf/verifier/map_ret_val.c */
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include "../../../include/linux/filter.h"
+#include "bpf_misc.h"
+
+struct {
+       __uint(type, BPF_MAP_TYPE_HASH);
+       __uint(max_entries, 1);
+       __type(key, long long);
+       __type(value, long long);
+} map_hash_8b SEC(".maps");
+
+SEC("socket")
+__description("invalid map_fd for function call")
+__failure __msg("fd 0 is not pointing to valid bpf_map")
+__failure_unpriv
+__naked void map_fd_for_function_call(void)
+{
+       asm volatile ("                                 \
+       r2 = 0;                                         \
+       *(u64*)(r10 - 8) = r2;                          \
+       r2 = r10;                                       \
+       r2 += -8;                                       \
+       .8byte %[ld_map_fd];                            \
+       .8byte 0;                                       \
+       call %[bpf_map_delete_elem];                    \
+       exit;                                           \
+"      :
+       : __imm(bpf_map_delete_elem),
+         __imm_insn(ld_map_fd, BPF_RAW_INSN(BPF_LD | BPF_DW | BPF_IMM, BPF_REG_1, BPF_PSEUDO_MAP_FD, 0, 0))
+       : __clobber_all);
+}
+
+SEC("socket")
+__description("don't check return value before access")
+__failure __msg("R0 invalid mem access 'map_value_or_null'")
+__failure_unpriv
+__naked void check_return_value_before_access(void)
+{
+       asm volatile ("                                 \
+       r1 = 0;                                         \
+       *(u64*)(r10 - 8) = r1;                          \
+       r2 = r10;                                       \
+       r2 += -8;                                       \
+       r1 = %[map_hash_8b] ll;                         \
+       call %[bpf_map_lookup_elem];                    \
+       r1 = 0;                                         \
+       *(u64*)(r0 + 0) = r1;                           \
+       exit;                                           \
+"      :
+       : __imm(bpf_map_lookup_elem),
+         __imm_addr(map_hash_8b)
+       : __clobber_all);
+}
+
+SEC("socket")
+__description("access memory with incorrect alignment")
+__failure __msg("misaligned value access")
+__failure_unpriv
+__flag(BPF_F_STRICT_ALIGNMENT)
+__naked void access_memory_with_incorrect_alignment_1(void)
+{
+       asm volatile ("                                 \
+       r1 = 0;                                         \
+       *(u64*)(r10 - 8) = r1;                          \
+       r2 = r10;                                       \
+       r2 += -8;                                       \
+       r1 = %[map_hash_8b] ll;                         \
+       call %[bpf_map_lookup_elem];                    \
+       if r0 == 0 goto l0_%=;                          \
+       r1 = 0;                                         \
+       *(u64*)(r0 + 4) = r1;                           \
+l0_%=: exit;                                           \
+"      :
+       : __imm(bpf_map_lookup_elem),
+         __imm_addr(map_hash_8b)
+       : __clobber_all);
+}
+
+SEC("socket")
+__description("sometimes access memory with incorrect alignment")
+__failure __msg("R0 invalid mem access")
+__msg_unpriv("R0 leaks addr")
+__flag(BPF_F_STRICT_ALIGNMENT)
+__naked void access_memory_with_incorrect_alignment_2(void)
+{
+       asm volatile ("                                 \
+       r1 = 0;                                         \
+       *(u64*)(r10 - 8) = r1;                          \
+       r2 = r10;                                       \
+       r2 += -8;                                       \
+       r1 = %[map_hash_8b] ll;                         \
+       call %[bpf_map_lookup_elem];                    \
+       if r0 == 0 goto l0_%=;                          \
+       r1 = 0;                                         \
+       *(u64*)(r0 + 0) = r1;                           \
+       exit;                                           \
+l0_%=: r1 = 1;                                         \
+       *(u64*)(r0 + 0) = r1;                           \
+       exit;                                           \
+"      :
+       : __imm(bpf_map_lookup_elem),
+         __imm_addr(map_hash_8b)
+       : __clobber_all);
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/verifier/map_ret_val.c b/tools/testing/selftests/bpf/verifier/map_ret_val.c
deleted file mode 100644 (file)
index bdd0e8d..0000000
+++ /dev/null
@@ -1,65 +0,0 @@
-{
-       "invalid map_fd for function call",
-       .insns = {
-       BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
-       BPF_ALU64_REG(BPF_MOV, BPF_REG_2, BPF_REG_10),
-       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
-       BPF_LD_MAP_FD(BPF_REG_1, 0),
-       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_delete_elem),
-       BPF_EXIT_INSN(),
-       },
-       .errstr = "fd 0 is not pointing to valid bpf_map",
-       .result = REJECT,
-},
-{
-       "don't check return value before access",
-       .insns = {
-       BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
-       BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
-       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
-       BPF_LD_MAP_FD(BPF_REG_1, 0),
-       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
-       BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 0),
-       BPF_EXIT_INSN(),
-       },
-       .fixup_map_hash_8b = { 3 },
-       .errstr = "R0 invalid mem access 'map_value_or_null'",
-       .result = REJECT,
-},
-{
-       "access memory with incorrect alignment",
-       .insns = {
-       BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
-       BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
-       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
-       BPF_LD_MAP_FD(BPF_REG_1, 0),
-       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
-       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
-       BPF_ST_MEM(BPF_DW, BPF_REG_0, 4, 0),
-       BPF_EXIT_INSN(),
-       },
-       .fixup_map_hash_8b = { 3 },
-       .errstr = "misaligned value access",
-       .result = REJECT,
-       .flags = F_LOAD_WITH_STRICT_ALIGNMENT,
-},
-{
-       "sometimes access memory with incorrect alignment",
-       .insns = {
-       BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
-       BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
-       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
-       BPF_LD_MAP_FD(BPF_REG_1, 0),
-       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
-       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
-       BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 0),
-       BPF_EXIT_INSN(),
-       BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 1),
-       BPF_EXIT_INSN(),
-       },
-       .fixup_map_hash_8b = { 3 },
-       .errstr = "R0 invalid mem access",
-       .errstr_unpriv = "R0 leaks addr",
-       .result = REJECT,
-       .flags = F_LOAD_WITH_STRICT_ALIGNMENT,
-},