Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: verifier/raw_tp_writable.c converted to inline assembly

Test verifier/raw_tp_writable.c automatically converted to use inline assembly.

Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20230325025524.144043-33-eddyz87@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Eduard Zingerman and committed by
Alexei Starovoitov
18cdc2b5 5a77a01f

+52 -35
+2
tools/testing/selftests/bpf/prog_tests/verifier.c
··· 29 29 #include "verifier_masking.skel.h" 30 30 #include "verifier_meta_access.skel.h" 31 31 #include "verifier_raw_stack.skel.h" 32 + #include "verifier_raw_tp_writable.skel.h" 32 33 33 34 __maybe_unused 34 35 static void run_tests_aux(const char *skel_name, skel_elf_bytes_fn elf_bytes_factory) ··· 81 80 void test_verifier_masking(void) { RUN(verifier_masking); } 82 81 void test_verifier_meta_access(void) { RUN(verifier_meta_access); } 83 82 void test_verifier_raw_stack(void) { RUN(verifier_raw_stack); } 83 + void test_verifier_raw_tp_writable(void) { RUN(verifier_raw_tp_writable); }
+50
tools/testing/selftests/bpf/progs/verifier_raw_tp_writable.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Converted from tools/testing/selftests/bpf/verifier/raw_tp_writable.c */ 3 + 4 + #include <linux/bpf.h> 5 + #include <bpf/bpf_helpers.h> 6 + #include "bpf_misc.h" 7 + 8 + struct { 9 + __uint(type, BPF_MAP_TYPE_HASH); 10 + __uint(max_entries, 1); 11 + __type(key, long long); 12 + __type(value, long long); 13 + } map_hash_8b SEC(".maps"); 14 + 15 + SEC("raw_tracepoint.w") 16 + __description("raw_tracepoint_writable: reject variable offset") 17 + __failure 18 + __msg("R6 invalid variable buffer offset: off=0, var_off=(0x0; 0xffffffff)") 19 + __flag(BPF_F_ANY_ALIGNMENT) 20 + __naked void tracepoint_writable_reject_variable_offset(void) 21 + { 22 + asm volatile (" \ 23 + /* r6 is our tp buffer */ \ 24 + r6 = *(u64*)(r1 + 0); \ 25 + r1 = %[map_hash_8b] ll; \ 26 + /* move the key (== 0) to r10-8 */ \ 27 + w0 = 0; \ 28 + r2 = r10; \ 29 + r2 += -8; \ 30 + *(u64*)(r2 + 0) = r0; \ 31 + /* lookup in the map */ \ 32 + call %[bpf_map_lookup_elem]; \ 33 + /* exit clean if null */ \ 34 + if r0 != 0 goto l0_%=; \ 35 + exit; \ 36 + l0_%=: /* shift the buffer pointer to a variable location */\ 37 + r0 = *(u32*)(r0 + 0); \ 38 + r6 += r0; \ 39 + /* clobber whatever's there */ \ 40 + r7 = 4242; \ 41 + *(u64*)(r6 + 0) = r7; \ 42 + r0 = 0; \ 43 + exit; \ 44 + " : 45 + : __imm(bpf_map_lookup_elem), 46 + __imm_addr(map_hash_8b) 47 + : __clobber_all); 48 + } 49 + 50 + char _license[] SEC("license") = "GPL";
-35
tools/testing/selftests/bpf/verifier/raw_tp_writable.c
··· 1 - { 2 - "raw_tracepoint_writable: reject variable offset", 3 - .insns = { 4 - /* r6 is our tp buffer */ 5 - BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0), 6 - 7 - BPF_LD_MAP_FD(BPF_REG_1, 0), 8 - /* move the key (== 0) to r10-8 */ 9 - BPF_MOV32_IMM(BPF_REG_0, 0), 10 - BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), 11 - BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8), 12 - BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_0, 0), 13 - /* lookup in the map */ 14 - BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, 15 - BPF_FUNC_map_lookup_elem), 16 - 17 - /* exit clean if null */ 18 - BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1), 19 - BPF_EXIT_INSN(), 20 - 21 - /* shift the buffer pointer to a variable location */ 22 - BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_0, 0), 23 - BPF_ALU64_REG(BPF_ADD, BPF_REG_6, BPF_REG_0), 24 - /* clobber whatever's there */ 25 - BPF_MOV64_IMM(BPF_REG_7, 4242), 26 - BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_7, 0), 27 - 28 - BPF_MOV64_IMM(BPF_REG_0, 0), 29 - BPF_EXIT_INSN(), 30 - }, 31 - .fixup_map_hash_8b = { 1, }, 32 - .prog_type = BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, 33 - .errstr = "R6 invalid variable buffer offset: off=0, var_off=(0x0; 0xffffffff)", 34 - .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS, 35 - },