1 // SPDX-License-Identifier: GPL-2.0-only
2
3 #include <test_progs.h>
4
5 #include "cap_helpers.h"
6 #include "verifier_and.skel.h"
7 #include "verifier_arena.skel.h"
8 #include "verifier_arena_large.skel.h"
9 #include "verifier_array_access.skel.h"
10 #include "verifier_basic_stack.skel.h"
11 #include "verifier_bitfield_write.skel.h"
12 #include "verifier_bounds.skel.h"
13 #include "verifier_bounds_deduction.skel.h"
14 #include "verifier_bounds_deduction_non_const.skel.h"
15 #include "verifier_bounds_mix_sign_unsign.skel.h"
16 #include "verifier_bpf_get_stack.skel.h"
17 #include "verifier_bswap.skel.h"
18 #include "verifier_btf_ctx_access.skel.h"
19 #include "verifier_btf_unreliable_prog.skel.h"
20 #include "verifier_cfg.skel.h"
21 #include "verifier_cgroup_inv_retcode.skel.h"
22 #include "verifier_cgroup_skb.skel.h"
23 #include "verifier_cgroup_storage.skel.h"
24 #include "verifier_const.skel.h"
25 #include "verifier_const_or.skel.h"
26 #include "verifier_ctx.skel.h"
27 #include "verifier_ctx_sk_msg.skel.h"
28 #include "verifier_d_path.skel.h"
29 #include "verifier_direct_packet_access.skel.h"
30 #include "verifier_direct_stack_access_wraparound.skel.h"
31 #include "verifier_div0.skel.h"
32 #include "verifier_div_overflow.skel.h"
33 #include "verifier_global_subprogs.skel.h"
34 #include "verifier_global_ptr_args.skel.h"
35 #include "verifier_gotol.skel.h"
36 #include "verifier_helper_access_var_len.skel.h"
37 #include "verifier_helper_packet_access.skel.h"
38 #include "verifier_helper_restricted.skel.h"
39 #include "verifier_helper_value_access.skel.h"
40 #include "verifier_int_ptr.skel.h"
41 #include "verifier_iterating_callbacks.skel.h"
42 #include "verifier_jeq_infer_not_null.skel.h"
43 #include "verifier_jit_convergence.skel.h"
44 #include "verifier_ld_ind.skel.h"
45 #include "verifier_ldsx.skel.h"
46 #include "verifier_leak_ptr.skel.h"
47 #include "verifier_linked_scalars.skel.h"
48 #include "verifier_loops1.skel.h"
49 #include "verifier_lwt.skel.h"
50 #include "verifier_map_in_map.skel.h"
51 #include "verifier_map_ptr.skel.h"
52 #include "verifier_map_ptr_mixing.skel.h"
53 #include "verifier_map_ret_val.skel.h"
54 #include "verifier_masking.skel.h"
55 #include "verifier_meta_access.skel.h"
56 #include "verifier_movsx.skel.h"
57 #include "verifier_mtu.skel.h"
58 #include "verifier_netfilter_ctx.skel.h"
59 #include "verifier_netfilter_retcode.skel.h"
60 #include "verifier_bpf_fastcall.skel.h"
61 #include "verifier_or_jmp32_k.skel.h"
62 #include "verifier_precision.skel.h"
63 #include "verifier_prevent_map_lookup.skel.h"
64 #include "verifier_raw_stack.skel.h"
65 #include "verifier_raw_tp_writable.skel.h"
66 #include "verifier_reg_equal.skel.h"
67 #include "verifier_ref_tracking.skel.h"
68 #include "verifier_regalloc.skel.h"
69 #include "verifier_ringbuf.skel.h"
70 #include "verifier_runtime_jit.skel.h"
71 #include "verifier_scalar_ids.skel.h"
72 #include "verifier_sdiv.skel.h"
73 #include "verifier_search_pruning.skel.h"
74 #include "verifier_sock.skel.h"
75 #include "verifier_sock_addr.skel.h"
76 #include "verifier_sockmap_mutate.skel.h"
77 #include "verifier_spill_fill.skel.h"
78 #include "verifier_spin_lock.skel.h"
79 #include "verifier_stack_ptr.skel.h"
80 #include "verifier_subprog_precision.skel.h"
81 #include "verifier_subreg.skel.h"
82 #include "verifier_tailcall_jit.skel.h"
83 #include "verifier_typedef.skel.h"
84 #include "verifier_uninit.skel.h"
85 #include "verifier_unpriv.skel.h"
86 #include "verifier_unpriv_perf.skel.h"
87 #include "verifier_value_adj_spill.skel.h"
88 #include "verifier_value.skel.h"
89 #include "verifier_value_illegal_alu.skel.h"
90 #include "verifier_value_or_null.skel.h"
91 #include "verifier_value_ptr_arith.skel.h"
92 #include "verifier_var_off.skel.h"
93 #include "verifier_vfs_accept.skel.h"
94 #include "verifier_vfs_reject.skel.h"
95 #include "verifier_xadd.skel.h"
96 #include "verifier_xdp.skel.h"
97 #include "verifier_xdp_direct_packet_access.skel.h"
98 #include "verifier_bits_iter.skel.h"
99 #include "verifier_lsm.skel.h"
100
101 #define MAX_ENTRIES 11
102
103 struct test_val {
104 unsigned int index;
105 int foo[MAX_ENTRIES];
106 };
107
108 __maybe_unused
run_tests_aux(const char * skel_name,skel_elf_bytes_fn elf_bytes_factory,pre_execution_cb pre_execution_cb)109 static void run_tests_aux(const char *skel_name,
110 skel_elf_bytes_fn elf_bytes_factory,
111 pre_execution_cb pre_execution_cb)
112 {
113 struct test_loader tester = {};
114 __u64 old_caps;
115 int err;
116
117 /* test_verifier tests are executed w/o CAP_SYS_ADMIN, do the same here */
118 err = cap_disable_effective(1ULL << CAP_SYS_ADMIN, &old_caps);
119 if (err) {
120 PRINT_FAIL("failed to drop CAP_SYS_ADMIN: %i, %s\n", err, strerror(err));
121 return;
122 }
123
124 test_loader__set_pre_execution_cb(&tester, pre_execution_cb);
125 test_loader__run_subtests(&tester, skel_name, elf_bytes_factory);
126 test_loader_fini(&tester);
127
128 err = cap_enable_effective(old_caps, NULL);
129 if (err)
130 PRINT_FAIL("failed to restore CAP_SYS_ADMIN: %i, %s\n", err, strerror(err));
131 }
132
133 #define RUN(skel) run_tests_aux(#skel, skel##__elf_bytes, NULL)
134
test_verifier_and(void)135 void test_verifier_and(void) { RUN(verifier_and); }
test_verifier_arena(void)136 void test_verifier_arena(void) { RUN(verifier_arena); }
test_verifier_arena_large(void)137 void test_verifier_arena_large(void) { RUN(verifier_arena_large); }
test_verifier_basic_stack(void)138 void test_verifier_basic_stack(void) { RUN(verifier_basic_stack); }
test_verifier_bitfield_write(void)139 void test_verifier_bitfield_write(void) { RUN(verifier_bitfield_write); }
test_verifier_bounds(void)140 void test_verifier_bounds(void) { RUN(verifier_bounds); }
test_verifier_bounds_deduction(void)141 void test_verifier_bounds_deduction(void) { RUN(verifier_bounds_deduction); }
test_verifier_bounds_deduction_non_const(void)142 void test_verifier_bounds_deduction_non_const(void) { RUN(verifier_bounds_deduction_non_const); }
test_verifier_bounds_mix_sign_unsign(void)143 void test_verifier_bounds_mix_sign_unsign(void) { RUN(verifier_bounds_mix_sign_unsign); }
test_verifier_bpf_get_stack(void)144 void test_verifier_bpf_get_stack(void) { RUN(verifier_bpf_get_stack); }
test_verifier_bswap(void)145 void test_verifier_bswap(void) { RUN(verifier_bswap); }
test_verifier_btf_ctx_access(void)146 void test_verifier_btf_ctx_access(void) { RUN(verifier_btf_ctx_access); }
test_verifier_btf_unreliable_prog(void)147 void test_verifier_btf_unreliable_prog(void) { RUN(verifier_btf_unreliable_prog); }
test_verifier_cfg(void)148 void test_verifier_cfg(void) { RUN(verifier_cfg); }
test_verifier_cgroup_inv_retcode(void)149 void test_verifier_cgroup_inv_retcode(void) { RUN(verifier_cgroup_inv_retcode); }
test_verifier_cgroup_skb(void)150 void test_verifier_cgroup_skb(void) { RUN(verifier_cgroup_skb); }
test_verifier_cgroup_storage(void)151 void test_verifier_cgroup_storage(void) { RUN(verifier_cgroup_storage); }
test_verifier_const(void)152 void test_verifier_const(void) { RUN(verifier_const); }
test_verifier_const_or(void)153 void test_verifier_const_or(void) { RUN(verifier_const_or); }
test_verifier_ctx(void)154 void test_verifier_ctx(void) { RUN(verifier_ctx); }
test_verifier_ctx_sk_msg(void)155 void test_verifier_ctx_sk_msg(void) { RUN(verifier_ctx_sk_msg); }
test_verifier_d_path(void)156 void test_verifier_d_path(void) { RUN(verifier_d_path); }
test_verifier_direct_packet_access(void)157 void test_verifier_direct_packet_access(void) { RUN(verifier_direct_packet_access); }
test_verifier_direct_stack_access_wraparound(void)158 void test_verifier_direct_stack_access_wraparound(void) { RUN(verifier_direct_stack_access_wraparound); }
test_verifier_div0(void)159 void test_verifier_div0(void) { RUN(verifier_div0); }
test_verifier_div_overflow(void)160 void test_verifier_div_overflow(void) { RUN(verifier_div_overflow); }
test_verifier_global_subprogs(void)161 void test_verifier_global_subprogs(void) { RUN(verifier_global_subprogs); }
test_verifier_global_ptr_args(void)162 void test_verifier_global_ptr_args(void) { RUN(verifier_global_ptr_args); }
test_verifier_gotol(void)163 void test_verifier_gotol(void) { RUN(verifier_gotol); }
test_verifier_helper_access_var_len(void)164 void test_verifier_helper_access_var_len(void) { RUN(verifier_helper_access_var_len); }
test_verifier_helper_packet_access(void)165 void test_verifier_helper_packet_access(void) { RUN(verifier_helper_packet_access); }
test_verifier_helper_restricted(void)166 void test_verifier_helper_restricted(void) { RUN(verifier_helper_restricted); }
test_verifier_helper_value_access(void)167 void test_verifier_helper_value_access(void) { RUN(verifier_helper_value_access); }
test_verifier_int_ptr(void)168 void test_verifier_int_ptr(void) { RUN(verifier_int_ptr); }
test_verifier_iterating_callbacks(void)169 void test_verifier_iterating_callbacks(void) { RUN(verifier_iterating_callbacks); }
test_verifier_jeq_infer_not_null(void)170 void test_verifier_jeq_infer_not_null(void) { RUN(verifier_jeq_infer_not_null); }
test_verifier_jit_convergence(void)171 void test_verifier_jit_convergence(void) { RUN(verifier_jit_convergence); }
test_verifier_ld_ind(void)172 void test_verifier_ld_ind(void) { RUN(verifier_ld_ind); }
test_verifier_ldsx(void)173 void test_verifier_ldsx(void) { RUN(verifier_ldsx); }
test_verifier_leak_ptr(void)174 void test_verifier_leak_ptr(void) { RUN(verifier_leak_ptr); }
test_verifier_linked_scalars(void)175 void test_verifier_linked_scalars(void) { RUN(verifier_linked_scalars); }
test_verifier_loops1(void)176 void test_verifier_loops1(void) { RUN(verifier_loops1); }
test_verifier_lwt(void)177 void test_verifier_lwt(void) { RUN(verifier_lwt); }
test_verifier_map_in_map(void)178 void test_verifier_map_in_map(void) { RUN(verifier_map_in_map); }
test_verifier_map_ptr(void)179 void test_verifier_map_ptr(void) { RUN(verifier_map_ptr); }
test_verifier_map_ptr_mixing(void)180 void test_verifier_map_ptr_mixing(void) { RUN(verifier_map_ptr_mixing); }
test_verifier_map_ret_val(void)181 void test_verifier_map_ret_val(void) { RUN(verifier_map_ret_val); }
test_verifier_masking(void)182 void test_verifier_masking(void) { RUN(verifier_masking); }
test_verifier_meta_access(void)183 void test_verifier_meta_access(void) { RUN(verifier_meta_access); }
test_verifier_movsx(void)184 void test_verifier_movsx(void) { RUN(verifier_movsx); }
test_verifier_netfilter_ctx(void)185 void test_verifier_netfilter_ctx(void) { RUN(verifier_netfilter_ctx); }
test_verifier_netfilter_retcode(void)186 void test_verifier_netfilter_retcode(void) { RUN(verifier_netfilter_retcode); }
test_verifier_bpf_fastcall(void)187 void test_verifier_bpf_fastcall(void) { RUN(verifier_bpf_fastcall); }
test_verifier_or_jmp32_k(void)188 void test_verifier_or_jmp32_k(void) { RUN(verifier_or_jmp32_k); }
test_verifier_precision(void)189 void test_verifier_precision(void) { RUN(verifier_precision); }
test_verifier_prevent_map_lookup(void)190 void test_verifier_prevent_map_lookup(void) { RUN(verifier_prevent_map_lookup); }
test_verifier_raw_stack(void)191 void test_verifier_raw_stack(void) { RUN(verifier_raw_stack); }
test_verifier_raw_tp_writable(void)192 void test_verifier_raw_tp_writable(void) { RUN(verifier_raw_tp_writable); }
test_verifier_reg_equal(void)193 void test_verifier_reg_equal(void) { RUN(verifier_reg_equal); }
test_verifier_ref_tracking(void)194 void test_verifier_ref_tracking(void) { RUN(verifier_ref_tracking); }
test_verifier_regalloc(void)195 void test_verifier_regalloc(void) { RUN(verifier_regalloc); }
test_verifier_ringbuf(void)196 void test_verifier_ringbuf(void) { RUN(verifier_ringbuf); }
test_verifier_runtime_jit(void)197 void test_verifier_runtime_jit(void) { RUN(verifier_runtime_jit); }
test_verifier_scalar_ids(void)198 void test_verifier_scalar_ids(void) { RUN(verifier_scalar_ids); }
test_verifier_sdiv(void)199 void test_verifier_sdiv(void) { RUN(verifier_sdiv); }
test_verifier_search_pruning(void)200 void test_verifier_search_pruning(void) { RUN(verifier_search_pruning); }
test_verifier_sock(void)201 void test_verifier_sock(void) { RUN(verifier_sock); }
test_verifier_sock_addr(void)202 void test_verifier_sock_addr(void) { RUN(verifier_sock_addr); }
test_verifier_sockmap_mutate(void)203 void test_verifier_sockmap_mutate(void) { RUN(verifier_sockmap_mutate); }
test_verifier_spill_fill(void)204 void test_verifier_spill_fill(void) { RUN(verifier_spill_fill); }
test_verifier_spin_lock(void)205 void test_verifier_spin_lock(void) { RUN(verifier_spin_lock); }
test_verifier_stack_ptr(void)206 void test_verifier_stack_ptr(void) { RUN(verifier_stack_ptr); }
test_verifier_subprog_precision(void)207 void test_verifier_subprog_precision(void) { RUN(verifier_subprog_precision); }
test_verifier_subreg(void)208 void test_verifier_subreg(void) { RUN(verifier_subreg); }
test_verifier_tailcall_jit(void)209 void test_verifier_tailcall_jit(void) { RUN(verifier_tailcall_jit); }
test_verifier_typedef(void)210 void test_verifier_typedef(void) { RUN(verifier_typedef); }
test_verifier_uninit(void)211 void test_verifier_uninit(void) { RUN(verifier_uninit); }
test_verifier_unpriv(void)212 void test_verifier_unpriv(void) { RUN(verifier_unpriv); }
test_verifier_unpriv_perf(void)213 void test_verifier_unpriv_perf(void) { RUN(verifier_unpriv_perf); }
test_verifier_value_adj_spill(void)214 void test_verifier_value_adj_spill(void) { RUN(verifier_value_adj_spill); }
test_verifier_value(void)215 void test_verifier_value(void) { RUN(verifier_value); }
test_verifier_value_illegal_alu(void)216 void test_verifier_value_illegal_alu(void) { RUN(verifier_value_illegal_alu); }
test_verifier_value_or_null(void)217 void test_verifier_value_or_null(void) { RUN(verifier_value_or_null); }
test_verifier_var_off(void)218 void test_verifier_var_off(void) { RUN(verifier_var_off); }
test_verifier_vfs_accept(void)219 void test_verifier_vfs_accept(void) { RUN(verifier_vfs_accept); }
test_verifier_vfs_reject(void)220 void test_verifier_vfs_reject(void) { RUN(verifier_vfs_reject); }
test_verifier_xadd(void)221 void test_verifier_xadd(void) { RUN(verifier_xadd); }
test_verifier_xdp(void)222 void test_verifier_xdp(void) { RUN(verifier_xdp); }
test_verifier_xdp_direct_packet_access(void)223 void test_verifier_xdp_direct_packet_access(void) { RUN(verifier_xdp_direct_packet_access); }
test_verifier_bits_iter(void)224 void test_verifier_bits_iter(void) { RUN(verifier_bits_iter); }
test_verifier_lsm(void)225 void test_verifier_lsm(void) { RUN(verifier_lsm); }
226
test_verifier_mtu(void)227 void test_verifier_mtu(void)
228 {
229 __u64 caps = 0;
230 int ret;
231
232 /* In case CAP_BPF and CAP_PERFMON is not set */
233 ret = cap_enable_effective(1ULL << CAP_BPF | 1ULL << CAP_NET_ADMIN, &caps);
234 if (!ASSERT_OK(ret, "set_cap_bpf_cap_net_admin"))
235 return;
236 ret = cap_disable_effective(1ULL << CAP_SYS_ADMIN | 1ULL << CAP_PERFMON, NULL);
237 if (!ASSERT_OK(ret, "disable_cap_sys_admin"))
238 goto restore_cap;
239 RUN(verifier_mtu);
240 restore_cap:
241 if (caps)
242 cap_enable_effective(caps, NULL);
243 }
244
init_test_val_map(struct bpf_object * obj,char * map_name)245 static int init_test_val_map(struct bpf_object *obj, char *map_name)
246 {
247 struct test_val value = {
248 .index = (6 + 1) * sizeof(int),
249 .foo[6] = 0xabcdef12,
250 };
251 struct bpf_map *map;
252 int err, key = 0;
253
254 map = bpf_object__find_map_by_name(obj, map_name);
255 if (!map) {
256 PRINT_FAIL("Can't find map '%s'\n", map_name);
257 return -EINVAL;
258 }
259
260 err = bpf_map_update_elem(bpf_map__fd(map), &key, &value, 0);
261 if (err) {
262 PRINT_FAIL("Error while updating map '%s': %d\n", map_name, err);
263 return err;
264 }
265
266 return 0;
267 }
268
init_array_access_maps(struct bpf_object * obj)269 static int init_array_access_maps(struct bpf_object *obj)
270 {
271 return init_test_val_map(obj, "map_array_ro");
272 }
273
test_verifier_array_access(void)274 void test_verifier_array_access(void)
275 {
276 run_tests_aux("verifier_array_access",
277 verifier_array_access__elf_bytes,
278 init_array_access_maps);
279 }
280
init_value_ptr_arith_maps(struct bpf_object * obj)281 static int init_value_ptr_arith_maps(struct bpf_object *obj)
282 {
283 return init_test_val_map(obj, "map_array_48b");
284 }
285
test_verifier_value_ptr_arith(void)286 void test_verifier_value_ptr_arith(void)
287 {
288 run_tests_aux("verifier_value_ptr_arith",
289 verifier_value_ptr_arith__elf_bytes,
290 init_value_ptr_arith_maps);
291 }
292