1 // SPDX-License-Identifier: GPL-2.0
2 /* Converted from tools/testing/selftests/bpf/verifier/map_ptr.c */
5 #include <bpf/bpf_helpers.h>
16 __uint(type, BPF_MAP_TYPE_ARRAY);
17 __uint(max_entries, 1);
19 __type(value, struct test_val);
20 } map_array_48b SEC(".maps");
28 __uint(type, BPF_MAP_TYPE_HASH);
29 __uint(max_entries, 1);
30 __type(key, long long);
31 __type(value, struct other_val);
32 } map_hash_16b SEC(".maps");
35 __description("bpf_map_ptr: read with negative offset rejected")
36 __failure __msg("R1 is bpf_array invalid negative access: off=-8")
38 __msg_unpriv("access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN")
39 __naked void read_with_negative_offset_rejected(void)
43 r1 = %[map_array_48b] ll; \
44 r6 = *(u64*)(r1 - 8); \
48 : __imm_addr(map_array_48b)
53 __description("bpf_map_ptr: write rejected")
54 __failure __msg("only read from bpf_array is supported")
56 __msg_unpriv("access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN")
57 __naked void bpf_map_ptr_write_rejected(void)
61 *(u64*)(r10 - 8) = r0; \
64 r1 = %[map_array_48b] ll; \
65 *(u64*)(r1 + 0) = r2; \
69 : __imm_addr(map_array_48b)
74 __description("bpf_map_ptr: read non-existent field rejected")
76 __msg("cannot access ptr member ops with moff 0 in struct bpf_map with off 1 size 4")
78 __msg_unpriv("access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN")
79 __flag(BPF_F_ANY_ALIGNMENT)
80 __naked void read_non_existent_field_rejected(void)
84 r1 = %[map_array_48b] ll; \
85 r6 = *(u32*)(r1 + 1); \
89 : __imm_addr(map_array_48b)
94 __description("bpf_map_ptr: read ops field accepted")
95 __success __failure_unpriv
96 __msg_unpriv("access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN")
98 __naked void ptr_read_ops_field_accepted(void)
102 r1 = %[map_array_48b] ll; \
103 r6 = *(u64*)(r1 + 0); \
107 : __imm_addr(map_array_48b)
112 __description("bpf_map_ptr: r = 0, map_ptr = map_ptr + r")
113 __success __failure_unpriv
114 __msg_unpriv("R1 has pointer with unsupported alu operation")
116 __naked void map_ptr_map_ptr_r(void)
120 *(u64*)(r10 - 8) = r0; \
124 r1 = %[map_hash_16b] ll; \
126 call %[bpf_map_lookup_elem]; \
130 : __imm(bpf_map_lookup_elem),
131 __imm_addr(map_hash_16b)
136 __description("bpf_map_ptr: r = 0, r = r + map_ptr")
137 __success __failure_unpriv
138 __msg_unpriv("R0 has pointer with unsupported alu operation")
140 __naked void _0_r_r_map_ptr(void)
144 *(u64*)(r10 - 8) = r0; \
148 r0 = %[map_hash_16b] ll; \
150 call %[bpf_map_lookup_elem]; \
154 : __imm(bpf_map_lookup_elem),
155 __imm_addr(map_hash_16b)
159 char _license[] SEC("license") = "GPL";