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

selftests/bpf: verifier/map_in_map converted to inline assembly

Test verifier/map_in_map automatically converted to use inline assembly.

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

authored by

Eduard Zingerman and committed by
Alexei Starovoitov
4a400ef9 b427ca57

+144 -96
+2
tools/testing/selftests/bpf/prog_tests/verifier.c
··· 34 34 #include "verifier_leak_ptr.skel.h" 35 35 #include "verifier_loops1.skel.h" 36 36 #include "verifier_lwt.skel.h" 37 + #include "verifier_map_in_map.skel.h" 37 38 #include "verifier_map_ptr.skel.h" 38 39 #include "verifier_map_ret_val.skel.h" 39 40 #include "verifier_masking.skel.h" ··· 120 119 void test_verifier_leak_ptr(void) { RUN(verifier_leak_ptr); } 121 120 void test_verifier_loops1(void) { RUN(verifier_loops1); } 122 121 void test_verifier_lwt(void) { RUN(verifier_lwt); } 122 + void test_verifier_map_in_map(void) { RUN(verifier_map_in_map); } 123 123 void test_verifier_map_ptr(void) { RUN(verifier_map_ptr); } 124 124 void test_verifier_map_ret_val(void) { RUN(verifier_map_ret_val); } 125 125 void test_verifier_masking(void) { RUN(verifier_masking); }
+142
tools/testing/selftests/bpf/progs/verifier_map_in_map.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Converted from tools/testing/selftests/bpf/verifier/map_in_map.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_ARRAY_OF_MAPS); 10 + __uint(max_entries, 1); 11 + __type(key, int); 12 + __type(value, int); 13 + __array(values, struct { 14 + __uint(type, BPF_MAP_TYPE_ARRAY); 15 + __uint(max_entries, 1); 16 + __type(key, int); 17 + __type(value, int); 18 + }); 19 + } map_in_map SEC(".maps"); 20 + 21 + SEC("socket") 22 + __description("map in map access") 23 + __success __success_unpriv __retval(0) 24 + __naked void map_in_map_access(void) 25 + { 26 + asm volatile (" \ 27 + r1 = 0; \ 28 + *(u32*)(r10 - 4) = r1; \ 29 + r2 = r10; \ 30 + r2 += -4; \ 31 + r1 = %[map_in_map] ll; \ 32 + call %[bpf_map_lookup_elem]; \ 33 + if r0 == 0 goto l0_%=; \ 34 + r1 = 0; \ 35 + *(u32*)(r10 - 4) = r1; \ 36 + r2 = r10; \ 37 + r2 += -4; \ 38 + r1 = r0; \ 39 + call %[bpf_map_lookup_elem]; \ 40 + l0_%=: r0 = 0; \ 41 + exit; \ 42 + " : 43 + : __imm(bpf_map_lookup_elem), 44 + __imm_addr(map_in_map) 45 + : __clobber_all); 46 + } 47 + 48 + SEC("xdp") 49 + __description("map in map state pruning") 50 + __success __msg("processed 26 insns") 51 + __log_level(2) __retval(0) __flag(BPF_F_TEST_STATE_FREQ) 52 + __naked void map_in_map_state_pruning(void) 53 + { 54 + asm volatile (" \ 55 + r1 = 0; \ 56 + *(u32*)(r10 - 4) = r1; \ 57 + r6 = r10; \ 58 + r6 += -4; \ 59 + r2 = r6; \ 60 + r1 = %[map_in_map] ll; \ 61 + call %[bpf_map_lookup_elem]; \ 62 + if r0 != 0 goto l0_%=; \ 63 + exit; \ 64 + l0_%=: r2 = r6; \ 65 + r1 = r0; \ 66 + call %[bpf_map_lookup_elem]; \ 67 + if r0 != 0 goto l1_%=; \ 68 + r2 = r6; \ 69 + r1 = %[map_in_map] ll; \ 70 + call %[bpf_map_lookup_elem]; \ 71 + if r0 != 0 goto l2_%=; \ 72 + exit; \ 73 + l2_%=: r2 = r6; \ 74 + r1 = r0; \ 75 + call %[bpf_map_lookup_elem]; \ 76 + if r0 != 0 goto l1_%=; \ 77 + exit; \ 78 + l1_%=: r0 = *(u32*)(r0 + 0); \ 79 + exit; \ 80 + " : 81 + : __imm(bpf_map_lookup_elem), 82 + __imm_addr(map_in_map) 83 + : __clobber_all); 84 + } 85 + 86 + SEC("socket") 87 + __description("invalid inner map pointer") 88 + __failure __msg("R1 pointer arithmetic on map_ptr prohibited") 89 + __failure_unpriv 90 + __naked void invalid_inner_map_pointer(void) 91 + { 92 + asm volatile (" \ 93 + r1 = 0; \ 94 + *(u32*)(r10 - 4) = r1; \ 95 + r2 = r10; \ 96 + r2 += -4; \ 97 + r1 = %[map_in_map] ll; \ 98 + call %[bpf_map_lookup_elem]; \ 99 + if r0 == 0 goto l0_%=; \ 100 + r1 = 0; \ 101 + *(u32*)(r10 - 4) = r1; \ 102 + r2 = r10; \ 103 + r2 += -4; \ 104 + r1 = r0; \ 105 + r1 += 8; \ 106 + call %[bpf_map_lookup_elem]; \ 107 + l0_%=: r0 = 0; \ 108 + exit; \ 109 + " : 110 + : __imm(bpf_map_lookup_elem), 111 + __imm_addr(map_in_map) 112 + : __clobber_all); 113 + } 114 + 115 + SEC("socket") 116 + __description("forgot null checking on the inner map pointer") 117 + __failure __msg("R1 type=map_value_or_null expected=map_ptr") 118 + __failure_unpriv 119 + __naked void on_the_inner_map_pointer(void) 120 + { 121 + asm volatile (" \ 122 + r1 = 0; \ 123 + *(u32*)(r10 - 4) = r1; \ 124 + r2 = r10; \ 125 + r2 += -4; \ 126 + r1 = %[map_in_map] ll; \ 127 + call %[bpf_map_lookup_elem]; \ 128 + r1 = 0; \ 129 + *(u32*)(r10 - 4) = r1; \ 130 + r2 = r10; \ 131 + r2 += -4; \ 132 + r1 = r0; \ 133 + call %[bpf_map_lookup_elem]; \ 134 + r0 = 0; \ 135 + exit; \ 136 + " : 137 + : __imm(bpf_map_lookup_elem), 138 + __imm_addr(map_in_map) 139 + : __clobber_all); 140 + } 141 + 142 + char _license[] SEC("license") = "GPL";
-96
tools/testing/selftests/bpf/verifier/map_in_map.c
··· 1 - { 2 - "map in map access", 3 - .insns = { 4 - BPF_ST_MEM(0, BPF_REG_10, -4, 0), 5 - BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), 6 - BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), 7 - BPF_LD_MAP_FD(BPF_REG_1, 0), 8 - BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), 9 - BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 5), 10 - BPF_ST_MEM(0, BPF_REG_10, -4, 0), 11 - BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), 12 - BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), 13 - BPF_MOV64_REG(BPF_REG_1, BPF_REG_0), 14 - BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), 15 - BPF_MOV64_IMM(BPF_REG_0, 0), 16 - BPF_EXIT_INSN(), 17 - }, 18 - .fixup_map_in_map = { 3 }, 19 - .result = ACCEPT, 20 - }, 21 - { 22 - "map in map state pruning", 23 - .insns = { 24 - BPF_ST_MEM(0, BPF_REG_10, -4, 0), 25 - BPF_MOV64_REG(BPF_REG_6, BPF_REG_10), 26 - BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -4), 27 - BPF_MOV64_REG(BPF_REG_2, BPF_REG_6), 28 - BPF_LD_MAP_FD(BPF_REG_1, 0), 29 - BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), 30 - BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1), 31 - BPF_EXIT_INSN(), 32 - BPF_MOV64_REG(BPF_REG_2, BPF_REG_6), 33 - BPF_MOV64_REG(BPF_REG_1, BPF_REG_0), 34 - BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), 35 - BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 11), 36 - BPF_MOV64_REG(BPF_REG_2, BPF_REG_6), 37 - BPF_LD_MAP_FD(BPF_REG_1, 0), 38 - BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), 39 - BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1), 40 - BPF_EXIT_INSN(), 41 - BPF_MOV64_REG(BPF_REG_2, BPF_REG_6), 42 - BPF_MOV64_REG(BPF_REG_1, BPF_REG_0), 43 - BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), 44 - BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1), 45 - BPF_EXIT_INSN(), 46 - BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_0, 0), 47 - BPF_EXIT_INSN(), 48 - }, 49 - .fixup_map_in_map = { 4, 14 }, 50 - .flags = BPF_F_TEST_STATE_FREQ, 51 - .result = VERBOSE_ACCEPT, 52 - .errstr = "processed 25 insns", 53 - .prog_type = BPF_PROG_TYPE_XDP, 54 - }, 55 - { 56 - "invalid inner map pointer", 57 - .insns = { 58 - BPF_ST_MEM(0, BPF_REG_10, -4, 0), 59 - BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), 60 - BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), 61 - BPF_LD_MAP_FD(BPF_REG_1, 0), 62 - BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), 63 - BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6), 64 - BPF_ST_MEM(0, BPF_REG_10, -4, 0), 65 - BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), 66 - BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), 67 - BPF_MOV64_REG(BPF_REG_1, BPF_REG_0), 68 - BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8), 69 - BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), 70 - BPF_MOV64_IMM(BPF_REG_0, 0), 71 - BPF_EXIT_INSN(), 72 - }, 73 - .fixup_map_in_map = { 3 }, 74 - .errstr = "R1 pointer arithmetic on map_ptr prohibited", 75 - .result = REJECT, 76 - }, 77 - { 78 - "forgot null checking on the inner map pointer", 79 - .insns = { 80 - BPF_ST_MEM(0, BPF_REG_10, -4, 0), 81 - BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), 82 - BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), 83 - BPF_LD_MAP_FD(BPF_REG_1, 0), 84 - BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), 85 - BPF_ST_MEM(0, BPF_REG_10, -4, 0), 86 - BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), 87 - BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), 88 - BPF_MOV64_REG(BPF_REG_1, BPF_REG_0), 89 - BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), 90 - BPF_MOV64_IMM(BPF_REG_0, 0), 91 - BPF_EXIT_INSN(), 92 - }, 93 - .fixup_map_in_map = { 3 }, 94 - .errstr = "R1 type=map_value_or_null expected=map_ptr", 95 - .result = REJECT, 96 - },