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