1 // SPDX-License-Identifier: GPL-2.0
2 /* Converted from tools/testing/selftests/bpf/verifier/cgroup_storage.c */
5 #include <bpf/bpf_helpers.h>
6 #include "../../../include/linux/filter.h"
10 __uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
11 __uint(max_entries, 0);
12 __type(key, struct bpf_cgroup_storage_key);
13 __type(value, char[TEST_DATA_LEN]);
14 } cgroup_storage SEC(".maps");
17 __uint(type, BPF_MAP_TYPE_HASH);
18 __uint(max_entries, 1);
19 __type(key, long long);
20 __type(value, long long);
21 } map_hash_8b SEC(".maps");
24 __uint(type, BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE);
25 __uint(max_entries, 0);
26 __type(key, struct bpf_cgroup_storage_key);
27 __type(value, char[64]);
28 } percpu_cgroup_storage SEC(".maps");
31 __description("valid cgroup storage access")
32 __success __success_unpriv __retval(0)
33 __naked void valid_cgroup_storage_access(void)
37 r1 = %[cgroup_storage] ll; \
38 call %[bpf_get_local_storage]; \
39 r1 = *(u32*)(r0 + 0); \
44 : __imm(bpf_get_local_storage),
45 __imm_addr(cgroup_storage)
50 __description("invalid cgroup storage access 1")
51 __failure __msg("cannot pass map_type 1 into func bpf_get_local_storage")
53 __naked void invalid_cgroup_storage_access_1(void)
57 r1 = %[map_hash_8b] ll; \
58 call %[bpf_get_local_storage]; \
59 r1 = *(u32*)(r0 + 0); \
64 : __imm(bpf_get_local_storage),
65 __imm_addr(map_hash_8b)
70 __description("invalid cgroup storage access 2")
71 __failure __msg("fd 1 is not pointing to valid bpf_map")
73 __naked void invalid_cgroup_storage_access_2(void)
77 .8byte %[ld_map_fd]; \
79 call %[bpf_get_local_storage]; \
83 : __imm(bpf_get_local_storage),
84 __imm_insn(ld_map_fd, BPF_RAW_INSN(BPF_LD | BPF_DW | BPF_IMM, BPF_REG_1, BPF_PSEUDO_MAP_FD, 0, 1))
89 __description("invalid cgroup storage access 3")
90 __failure __msg("invalid access to map value, value_size=64 off=256 size=4")
92 __naked void invalid_cgroup_storage_access_3(void)
96 r1 = %[cgroup_storage] ll; \
97 call %[bpf_get_local_storage]; \
98 r1 = *(u32*)(r0 + 256); \
103 : __imm(bpf_get_local_storage),
104 __imm_addr(cgroup_storage)
109 __description("invalid cgroup storage access 4")
110 __failure __msg("invalid access to map value, value_size=64 off=-2 size=4")
112 __flag(BPF_F_ANY_ALIGNMENT)
113 __naked void invalid_cgroup_storage_access_4(void)
117 r1 = %[cgroup_storage] ll; \
118 call %[bpf_get_local_storage]; \
119 r1 = *(u32*)(r0 - 2); \
124 : __imm(bpf_get_local_storage),
125 __imm_addr(cgroup_storage)
130 __description("invalid cgroup storage access 5")
131 __failure __msg("get_local_storage() doesn't support non-zero flags")
133 __naked void invalid_cgroup_storage_access_5(void)
137 r1 = %[cgroup_storage] ll; \
138 call %[bpf_get_local_storage]; \
139 r1 = *(u32*)(r0 + 0); \
144 : __imm(bpf_get_local_storage),
145 __imm_addr(cgroup_storage)
150 __description("invalid cgroup storage access 6")
151 __failure __msg("get_local_storage() doesn't support non-zero flags")
152 __msg_unpriv("R2 leaks addr into helper function")
153 __naked void invalid_cgroup_storage_access_6(void)
157 r1 = %[cgroup_storage] ll; \
158 call %[bpf_get_local_storage]; \
159 r1 = *(u32*)(r0 + 0); \
164 : __imm(bpf_get_local_storage),
165 __imm_addr(cgroup_storage)
170 __description("valid per-cpu cgroup storage access")
171 __success __success_unpriv __retval(0)
172 __naked void per_cpu_cgroup_storage_access(void)
176 r1 = %[percpu_cgroup_storage] ll; \
177 call %[bpf_get_local_storage]; \
178 r1 = *(u32*)(r0 + 0); \
183 : __imm(bpf_get_local_storage),
184 __imm_addr(percpu_cgroup_storage)
189 __description("invalid per-cpu cgroup storage access 1")
190 __failure __msg("cannot pass map_type 1 into func bpf_get_local_storage")
192 __naked void cpu_cgroup_storage_access_1(void)
196 r1 = %[map_hash_8b] ll; \
197 call %[bpf_get_local_storage]; \
198 r1 = *(u32*)(r0 + 0); \
203 : __imm(bpf_get_local_storage),
204 __imm_addr(map_hash_8b)
209 __description("invalid per-cpu cgroup storage access 2")
210 __failure __msg("fd 1 is not pointing to valid bpf_map")
212 __naked void cpu_cgroup_storage_access_2(void)
216 .8byte %[ld_map_fd]; \
218 call %[bpf_get_local_storage]; \
222 : __imm(bpf_get_local_storage),
223 __imm_insn(ld_map_fd, BPF_RAW_INSN(BPF_LD | BPF_DW | BPF_IMM, BPF_REG_1, BPF_PSEUDO_MAP_FD, 0, 1))
228 __description("invalid per-cpu cgroup storage access 3")
229 __failure __msg("invalid access to map value, value_size=64 off=256 size=4")
231 __naked void cpu_cgroup_storage_access_3(void)
235 r1 = %[percpu_cgroup_storage] ll; \
236 call %[bpf_get_local_storage]; \
237 r1 = *(u32*)(r0 + 256); \
242 : __imm(bpf_get_local_storage),
243 __imm_addr(percpu_cgroup_storage)
248 __description("invalid per-cpu cgroup storage access 4")
249 __failure __msg("invalid access to map value, value_size=64 off=-2 size=4")
251 __flag(BPF_F_ANY_ALIGNMENT)
252 __naked void cpu_cgroup_storage_access_4(void)
256 r1 = %[cgroup_storage] ll; \
257 call %[bpf_get_local_storage]; \
258 r1 = *(u32*)(r0 - 2); \
263 : __imm(bpf_get_local_storage),
264 __imm_addr(cgroup_storage)
269 __description("invalid per-cpu cgroup storage access 5")
270 __failure __msg("get_local_storage() doesn't support non-zero flags")
272 __naked void cpu_cgroup_storage_access_5(void)
276 r1 = %[percpu_cgroup_storage] ll; \
277 call %[bpf_get_local_storage]; \
278 r1 = *(u32*)(r0 + 0); \
283 : __imm(bpf_get_local_storage),
284 __imm_addr(percpu_cgroup_storage)
289 __description("invalid per-cpu cgroup storage access 6")
290 __failure __msg("get_local_storage() doesn't support non-zero flags")
291 __msg_unpriv("R2 leaks addr into helper function")
292 __naked void cpu_cgroup_storage_access_6(void)
296 r1 = %[percpu_cgroup_storage] ll; \
297 call %[bpf_get_local_storage]; \
298 r1 = *(u32*)(r0 + 0); \
303 : __imm(bpf_get_local_storage),
304 __imm_addr(percpu_cgroup_storage)
308 char _license[] SEC("license") = "GPL";