1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Testsuite for eBPF verifier
4  *
5  * Copyright (c) 2014 PLUMgrid, http://plumgrid.com
6  * Copyright (c) 2017 Facebook
7  * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io
8  */
9 
10 #include <endian.h>
11 #include <asm/types.h>
12 #include <linux/types.h>
13 #include <stdint.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <errno.h>
18 #include <string.h>
19 #include <stddef.h>
20 #include <stdbool.h>
21 #include <sched.h>
22 #include <limits.h>
23 #include <assert.h>
24 
25 #include <sys/capability.h>
26 
27 #include <linux/unistd.h>
28 #include <linux/filter.h>
29 #include <linux/bpf_perf_event.h>
30 #include <linux/bpf.h>
31 #include <linux/if_ether.h>
32 #include <linux/btf.h>
33 
34 #include <bpf/bpf.h>
35 #include <bpf/libbpf.h>
36 
37 #ifdef HAVE_GENHDR
38 # include "autoconf.h"
39 #else
40 # if defined(__i386) || defined(__x86_64) || defined(__s390x__) || defined(__aarch64__)
41 #  define CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 1
42 # endif
43 #endif
44 #include "bpf_rlimit.h"
45 #include "bpf_rand.h"
46 #include "bpf_util.h"
47 #include "test_btf.h"
48 #include "../../../include/linux/filter.h"
49 
50 #define MAX_INSNS	BPF_MAXINSNS
51 #define MAX_TEST_INSNS	1000000
52 #define MAX_FIXUPS	8
53 #define MAX_NR_MAPS	18
54 #define MAX_TEST_RUNS	8
55 #define POINTER_VALUE	0xcafe4all
56 #define TEST_DATA_LEN	64
57 
58 #define F_NEEDS_EFFICIENT_UNALIGNED_ACCESS	(1 << 0)
59 #define F_LOAD_WITH_STRICT_ALIGNMENT		(1 << 1)
60 
61 #define UNPRIV_SYSCTL "kernel/unprivileged_bpf_disabled"
62 static bool unpriv_disabled = false;
63 static int skips;
64 
65 struct bpf_test {
66 	const char *descr;
67 	struct bpf_insn	insns[MAX_INSNS];
68 	struct bpf_insn	*fill_insns;
69 	int fixup_map_hash_8b[MAX_FIXUPS];
70 	int fixup_map_hash_48b[MAX_FIXUPS];
71 	int fixup_map_hash_16b[MAX_FIXUPS];
72 	int fixup_map_array_48b[MAX_FIXUPS];
73 	int fixup_map_sockmap[MAX_FIXUPS];
74 	int fixup_map_sockhash[MAX_FIXUPS];
75 	int fixup_map_xskmap[MAX_FIXUPS];
76 	int fixup_map_stacktrace[MAX_FIXUPS];
77 	int fixup_prog1[MAX_FIXUPS];
78 	int fixup_prog2[MAX_FIXUPS];
79 	int fixup_map_in_map[MAX_FIXUPS];
80 	int fixup_cgroup_storage[MAX_FIXUPS];
81 	int fixup_percpu_cgroup_storage[MAX_FIXUPS];
82 	int fixup_map_spin_lock[MAX_FIXUPS];
83 	int fixup_map_array_ro[MAX_FIXUPS];
84 	int fixup_map_array_wo[MAX_FIXUPS];
85 	int fixup_map_array_small[MAX_FIXUPS];
86 	int fixup_sk_storage_map[MAX_FIXUPS];
87 	const char *errstr;
88 	const char *errstr_unpriv;
89 	uint32_t retval, retval_unpriv, insn_processed;
90 	int prog_len;
91 	enum {
92 		UNDEF,
93 		ACCEPT,
94 		REJECT
95 	} result, result_unpriv;
96 	enum bpf_prog_type prog_type;
97 	uint8_t flags;
98 	__u8 data[TEST_DATA_LEN];
99 	void (*fill_helper)(struct bpf_test *self);
100 	uint8_t runs;
101 	struct {
102 		uint32_t retval, retval_unpriv;
103 		union {
104 			__u8 data[TEST_DATA_LEN];
105 			__u64 data64[TEST_DATA_LEN / 8];
106 		};
107 	} retvals[MAX_TEST_RUNS];
108 	enum bpf_attach_type expected_attach_type;
109 };
110 
111 /* Note we want this to be 64 bit aligned so that the end of our array is
112  * actually the end of the structure.
113  */
114 #define MAX_ENTRIES 11
115 
116 struct test_val {
117 	unsigned int index;
118 	int foo[MAX_ENTRIES];
119 };
120 
121 struct other_val {
122 	long long foo;
123 	long long bar;
124 };
125 
126 static void bpf_fill_ld_abs_vlan_push_pop(struct bpf_test *self)
127 {
128 	/* test: {skb->data[0], vlan_push} x 51 + {skb->data[0], vlan_pop} x 51 */
129 #define PUSH_CNT 51
130 	/* jump range is limited to 16 bit. PUSH_CNT of ld_abs needs room */
131 	unsigned int len = (1 << 15) - PUSH_CNT * 2 * 5 * 6;
132 	struct bpf_insn *insn = self->fill_insns;
133 	int i = 0, j, k = 0;
134 
135 	insn[i++] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
136 loop:
137 	for (j = 0; j < PUSH_CNT; j++) {
138 		insn[i++] = BPF_LD_ABS(BPF_B, 0);
139 		/* jump to error label */
140 		insn[i] = BPF_JMP32_IMM(BPF_JNE, BPF_REG_0, 0x34, len - i - 3);
141 		i++;
142 		insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
143 		insn[i++] = BPF_MOV64_IMM(BPF_REG_2, 1);
144 		insn[i++] = BPF_MOV64_IMM(BPF_REG_3, 2);
145 		insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
146 					 BPF_FUNC_skb_vlan_push),
147 		insn[i] = BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, len - i - 3);
148 		i++;
149 	}
150 
151 	for (j = 0; j < PUSH_CNT; j++) {
152 		insn[i++] = BPF_LD_ABS(BPF_B, 0);
153 		insn[i] = BPF_JMP32_IMM(BPF_JNE, BPF_REG_0, 0x34, len - i - 3);
154 		i++;
155 		insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
156 		insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
157 					 BPF_FUNC_skb_vlan_pop),
158 		insn[i] = BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, len - i - 3);
159 		i++;
160 	}
161 	if (++k < 5)
162 		goto loop;
163 
164 	for (; i < len - 3; i++)
165 		insn[i] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 0xbef);
166 	insn[len - 3] = BPF_JMP_A(1);
167 	/* error label */
168 	insn[len - 2] = BPF_MOV32_IMM(BPF_REG_0, 0);
169 	insn[len - 1] = BPF_EXIT_INSN();
170 	self->prog_len = len;
171 }
172 
173 static void bpf_fill_jump_around_ld_abs(struct bpf_test *self)
174 {
175 	struct bpf_insn *insn = self->fill_insns;
176 	/* jump range is limited to 16 bit. every ld_abs is replaced by 6 insns,
177 	 * but on arches like arm, ppc etc, there will be one BPF_ZEXT inserted
178 	 * to extend the error value of the inlined ld_abs sequence which then
179 	 * contains 7 insns. so, set the dividend to 7 so the testcase could
180 	 * work on all arches.
181 	 */
182 	unsigned int len = (1 << 15) / 7;
183 	int i = 0;
184 
185 	insn[i++] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
186 	insn[i++] = BPF_LD_ABS(BPF_B, 0);
187 	insn[i] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 10, len - i - 2);
188 	i++;
189 	while (i < len - 1)
190 		insn[i++] = BPF_LD_ABS(BPF_B, 1);
191 	insn[i] = BPF_EXIT_INSN();
192 	self->prog_len = i + 1;
193 }
194 
195 static void bpf_fill_rand_ld_dw(struct bpf_test *self)
196 {
197 	struct bpf_insn *insn = self->fill_insns;
198 	uint64_t res = 0;
199 	int i = 0;
200 
201 	insn[i++] = BPF_MOV32_IMM(BPF_REG_0, 0);
202 	while (i < self->retval) {
203 		uint64_t val = bpf_semi_rand_get();
204 		struct bpf_insn tmp[2] = { BPF_LD_IMM64(BPF_REG_1, val) };
205 
206 		res ^= val;
207 		insn[i++] = tmp[0];
208 		insn[i++] = tmp[1];
209 		insn[i++] = BPF_ALU64_REG(BPF_XOR, BPF_REG_0, BPF_REG_1);
210 	}
211 	insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_0);
212 	insn[i++] = BPF_ALU64_IMM(BPF_RSH, BPF_REG_1, 32);
213 	insn[i++] = BPF_ALU64_REG(BPF_XOR, BPF_REG_0, BPF_REG_1);
214 	insn[i] = BPF_EXIT_INSN();
215 	self->prog_len = i + 1;
216 	res ^= (res >> 32);
217 	self->retval = (uint32_t)res;
218 }
219 
220 #define MAX_JMP_SEQ 8192
221 
222 /* test the sequence of 8k jumps */
223 static void bpf_fill_scale1(struct bpf_test *self)
224 {
225 	struct bpf_insn *insn = self->fill_insns;
226 	int i = 0, k = 0;
227 
228 	insn[i++] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
229 	/* test to check that the long sequence of jumps is acceptable */
230 	while (k++ < MAX_JMP_SEQ) {
231 		insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
232 					 BPF_FUNC_get_prandom_u32);
233 		insn[i++] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, bpf_semi_rand_get(), 2);
234 		insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_10);
235 		insn[i++] = BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6,
236 					-8 * (k % 64 + 1));
237 	}
238 	/* is_state_visited() doesn't allocate state for pruning for every jump.
239 	 * Hence multiply jmps by 4 to accommodate that heuristic
240 	 */
241 	while (i < MAX_TEST_INSNS - MAX_JMP_SEQ * 4)
242 		insn[i++] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 42);
243 	insn[i] = BPF_EXIT_INSN();
244 	self->prog_len = i + 1;
245 	self->retval = 42;
246 }
247 
248 /* test the sequence of 8k jumps in inner most function (function depth 8)*/
249 static void bpf_fill_scale2(struct bpf_test *self)
250 {
251 	struct bpf_insn *insn = self->fill_insns;
252 	int i = 0, k = 0;
253 
254 #define FUNC_NEST 7
255 	for (k = 0; k < FUNC_NEST; k++) {
256 		insn[i++] = BPF_CALL_REL(1);
257 		insn[i++] = BPF_EXIT_INSN();
258 	}
259 	insn[i++] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
260 	/* test to check that the long sequence of jumps is acceptable */
261 	k = 0;
262 	while (k++ < MAX_JMP_SEQ) {
263 		insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
264 					 BPF_FUNC_get_prandom_u32);
265 		insn[i++] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, bpf_semi_rand_get(), 2);
266 		insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_10);
267 		insn[i++] = BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6,
268 					-8 * (k % (64 - 4 * FUNC_NEST) + 1));
269 	}
270 	while (i < MAX_TEST_INSNS - MAX_JMP_SEQ * 4)
271 		insn[i++] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 42);
272 	insn[i] = BPF_EXIT_INSN();
273 	self->prog_len = i + 1;
274 	self->retval = 42;
275 }
276 
277 static void bpf_fill_scale(struct bpf_test *self)
278 {
279 	switch (self->retval) {
280 	case 1:
281 		return bpf_fill_scale1(self);
282 	case 2:
283 		return bpf_fill_scale2(self);
284 	default:
285 		self->prog_len = 0;
286 		break;
287 	}
288 }
289 
290 /* BPF_SK_LOOKUP contains 13 instructions, if you need to fix up maps */
291 #define BPF_SK_LOOKUP(func)						\
292 	/* struct bpf_sock_tuple tuple = {} */				\
293 	BPF_MOV64_IMM(BPF_REG_2, 0),					\
294 	BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_2, -8),			\
295 	BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -16),		\
296 	BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -24),		\
297 	BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -32),		\
298 	BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -40),		\
299 	BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -48),		\
300 	/* sk = func(ctx, &tuple, sizeof tuple, 0, 0) */		\
301 	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),				\
302 	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -48),				\
303 	BPF_MOV64_IMM(BPF_REG_3, sizeof(struct bpf_sock_tuple)),	\
304 	BPF_MOV64_IMM(BPF_REG_4, 0),					\
305 	BPF_MOV64_IMM(BPF_REG_5, 0),					\
306 	BPF_EMIT_CALL(BPF_FUNC_ ## func)
307 
308 /* BPF_DIRECT_PKT_R2 contains 7 instructions, it initializes default return
309  * value into 0 and does necessary preparation for direct packet access
310  * through r2. The allowed access range is 8 bytes.
311  */
312 #define BPF_DIRECT_PKT_R2						\
313 	BPF_MOV64_IMM(BPF_REG_0, 0),					\
314 	BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,			\
315 		    offsetof(struct __sk_buff, data)),			\
316 	BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,			\
317 		    offsetof(struct __sk_buff, data_end)),		\
318 	BPF_MOV64_REG(BPF_REG_4, BPF_REG_2),				\
319 	BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 8),				\
320 	BPF_JMP_REG(BPF_JLE, BPF_REG_4, BPF_REG_3, 1),			\
321 	BPF_EXIT_INSN()
322 
323 /* BPF_RAND_UEXT_R7 contains 4 instructions, it initializes R7 into a random
324  * positive u32, and zero-extend it into 64-bit.
325  */
326 #define BPF_RAND_UEXT_R7						\
327 	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,			\
328 		     BPF_FUNC_get_prandom_u32),				\
329 	BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),				\
330 	BPF_ALU64_IMM(BPF_LSH, BPF_REG_7, 33),				\
331 	BPF_ALU64_IMM(BPF_RSH, BPF_REG_7, 33)
332 
333 /* BPF_RAND_SEXT_R7 contains 5 instructions, it initializes R7 into a random
334  * negative u32, and sign-extend it into 64-bit.
335  */
336 #define BPF_RAND_SEXT_R7						\
337 	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,			\
338 		     BPF_FUNC_get_prandom_u32),				\
339 	BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),				\
340 	BPF_ALU64_IMM(BPF_OR, BPF_REG_7, 0x80000000),			\
341 	BPF_ALU64_IMM(BPF_LSH, BPF_REG_7, 32),				\
342 	BPF_ALU64_IMM(BPF_ARSH, BPF_REG_7, 32)
343 
344 static struct bpf_test tests[] = {
345 #define FILL_ARRAY
346 #include <verifier/tests.h>
347 #undef FILL_ARRAY
348 };
349 
350 static int probe_filter_length(const struct bpf_insn *fp)
351 {
352 	int len;
353 
354 	for (len = MAX_INSNS - 1; len > 0; --len)
355 		if (fp[len].code != 0 || fp[len].imm != 0)
356 			break;
357 	return len + 1;
358 }
359 
360 static bool skip_unsupported_map(enum bpf_map_type map_type)
361 {
362 	if (!bpf_probe_map_type(map_type, 0)) {
363 		printf("SKIP (unsupported map type %d)\n", map_type);
364 		skips++;
365 		return true;
366 	}
367 	return false;
368 }
369 
370 static int __create_map(uint32_t type, uint32_t size_key,
371 			uint32_t size_value, uint32_t max_elem,
372 			uint32_t extra_flags)
373 {
374 	int fd;
375 
376 	fd = bpf_create_map(type, size_key, size_value, max_elem,
377 			    (type == BPF_MAP_TYPE_HASH ?
378 			     BPF_F_NO_PREALLOC : 0) | extra_flags);
379 	if (fd < 0) {
380 		if (skip_unsupported_map(type))
381 			return -1;
382 		printf("Failed to create hash map '%s'!\n", strerror(errno));
383 	}
384 
385 	return fd;
386 }
387 
388 static int create_map(uint32_t type, uint32_t size_key,
389 		      uint32_t size_value, uint32_t max_elem)
390 {
391 	return __create_map(type, size_key, size_value, max_elem, 0);
392 }
393 
394 static void update_map(int fd, int index)
395 {
396 	struct test_val value = {
397 		.index = (6 + 1) * sizeof(int),
398 		.foo[6] = 0xabcdef12,
399 	};
400 
401 	assert(!bpf_map_update_elem(fd, &index, &value, 0));
402 }
403 
404 static int create_prog_dummy1(enum bpf_prog_type prog_type)
405 {
406 	struct bpf_insn prog[] = {
407 		BPF_MOV64_IMM(BPF_REG_0, 42),
408 		BPF_EXIT_INSN(),
409 	};
410 
411 	return bpf_load_program(prog_type, prog,
412 				ARRAY_SIZE(prog), "GPL", 0, NULL, 0);
413 }
414 
415 static int create_prog_dummy2(enum bpf_prog_type prog_type, int mfd, int idx)
416 {
417 	struct bpf_insn prog[] = {
418 		BPF_MOV64_IMM(BPF_REG_3, idx),
419 		BPF_LD_MAP_FD(BPF_REG_2, mfd),
420 		BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
421 			     BPF_FUNC_tail_call),
422 		BPF_MOV64_IMM(BPF_REG_0, 41),
423 		BPF_EXIT_INSN(),
424 	};
425 
426 	return bpf_load_program(prog_type, prog,
427 				ARRAY_SIZE(prog), "GPL", 0, NULL, 0);
428 }
429 
430 static int create_prog_array(enum bpf_prog_type prog_type, uint32_t max_elem,
431 			     int p1key)
432 {
433 	int p2key = 1;
434 	int mfd, p1fd, p2fd;
435 
436 	mfd = bpf_create_map(BPF_MAP_TYPE_PROG_ARRAY, sizeof(int),
437 			     sizeof(int), max_elem, 0);
438 	if (mfd < 0) {
439 		if (skip_unsupported_map(BPF_MAP_TYPE_PROG_ARRAY))
440 			return -1;
441 		printf("Failed to create prog array '%s'!\n", strerror(errno));
442 		return -1;
443 	}
444 
445 	p1fd = create_prog_dummy1(prog_type);
446 	p2fd = create_prog_dummy2(prog_type, mfd, p2key);
447 	if (p1fd < 0 || p2fd < 0)
448 		goto out;
449 	if (bpf_map_update_elem(mfd, &p1key, &p1fd, BPF_ANY) < 0)
450 		goto out;
451 	if (bpf_map_update_elem(mfd, &p2key, &p2fd, BPF_ANY) < 0)
452 		goto out;
453 	close(p2fd);
454 	close(p1fd);
455 
456 	return mfd;
457 out:
458 	close(p2fd);
459 	close(p1fd);
460 	close(mfd);
461 	return -1;
462 }
463 
464 static int create_map_in_map(void)
465 {
466 	int inner_map_fd, outer_map_fd;
467 
468 	inner_map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(int),
469 				      sizeof(int), 1, 0);
470 	if (inner_map_fd < 0) {
471 		if (skip_unsupported_map(BPF_MAP_TYPE_ARRAY))
472 			return -1;
473 		printf("Failed to create array '%s'!\n", strerror(errno));
474 		return inner_map_fd;
475 	}
476 
477 	outer_map_fd = bpf_create_map_in_map(BPF_MAP_TYPE_ARRAY_OF_MAPS, NULL,
478 					     sizeof(int), inner_map_fd, 1, 0);
479 	if (outer_map_fd < 0) {
480 		if (skip_unsupported_map(BPF_MAP_TYPE_ARRAY_OF_MAPS))
481 			return -1;
482 		printf("Failed to create array of maps '%s'!\n",
483 		       strerror(errno));
484 	}
485 
486 	close(inner_map_fd);
487 
488 	return outer_map_fd;
489 }
490 
491 static int create_cgroup_storage(bool percpu)
492 {
493 	enum bpf_map_type type = percpu ? BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE :
494 		BPF_MAP_TYPE_CGROUP_STORAGE;
495 	int fd;
496 
497 	fd = bpf_create_map(type, sizeof(struct bpf_cgroup_storage_key),
498 			    TEST_DATA_LEN, 0, 0);
499 	if (fd < 0) {
500 		if (skip_unsupported_map(type))
501 			return -1;
502 		printf("Failed to create cgroup storage '%s'!\n",
503 		       strerror(errno));
504 	}
505 
506 	return fd;
507 }
508 
509 /* struct bpf_spin_lock {
510  *   int val;
511  * };
512  * struct val {
513  *   int cnt;
514  *   struct bpf_spin_lock l;
515  * };
516  */
517 static const char btf_str_sec[] = "\0bpf_spin_lock\0val\0cnt\0l";
518 static __u32 btf_raw_types[] = {
519 	/* int */
520 	BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
521 	/* struct bpf_spin_lock */                      /* [2] */
522 	BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4),
523 	BTF_MEMBER_ENC(15, 1, 0), /* int val; */
524 	/* struct val */                                /* [3] */
525 	BTF_TYPE_ENC(15, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
526 	BTF_MEMBER_ENC(19, 1, 0), /* int cnt; */
527 	BTF_MEMBER_ENC(23, 2, 32),/* struct bpf_spin_lock l; */
528 };
529 
530 static int load_btf(void)
531 {
532 	struct btf_header hdr = {
533 		.magic = BTF_MAGIC,
534 		.version = BTF_VERSION,
535 		.hdr_len = sizeof(struct btf_header),
536 		.type_len = sizeof(btf_raw_types),
537 		.str_off = sizeof(btf_raw_types),
538 		.str_len = sizeof(btf_str_sec),
539 	};
540 	void *ptr, *raw_btf;
541 	int btf_fd;
542 
543 	ptr = raw_btf = malloc(sizeof(hdr) + sizeof(btf_raw_types) +
544 			       sizeof(btf_str_sec));
545 
546 	memcpy(ptr, &hdr, sizeof(hdr));
547 	ptr += sizeof(hdr);
548 	memcpy(ptr, btf_raw_types, hdr.type_len);
549 	ptr += hdr.type_len;
550 	memcpy(ptr, btf_str_sec, hdr.str_len);
551 	ptr += hdr.str_len;
552 
553 	btf_fd = bpf_load_btf(raw_btf, ptr - raw_btf, 0, 0, 0);
554 	free(raw_btf);
555 	if (btf_fd < 0)
556 		return -1;
557 	return btf_fd;
558 }
559 
560 static int create_map_spin_lock(void)
561 {
562 	struct bpf_create_map_attr attr = {
563 		.name = "test_map",
564 		.map_type = BPF_MAP_TYPE_ARRAY,
565 		.key_size = 4,
566 		.value_size = 8,
567 		.max_entries = 1,
568 		.btf_key_type_id = 1,
569 		.btf_value_type_id = 3,
570 	};
571 	int fd, btf_fd;
572 
573 	btf_fd = load_btf();
574 	if (btf_fd < 0)
575 		return -1;
576 	attr.btf_fd = btf_fd;
577 	fd = bpf_create_map_xattr(&attr);
578 	if (fd < 0)
579 		printf("Failed to create map with spin_lock\n");
580 	return fd;
581 }
582 
583 static int create_sk_storage_map(void)
584 {
585 	struct bpf_create_map_attr attr = {
586 		.name = "test_map",
587 		.map_type = BPF_MAP_TYPE_SK_STORAGE,
588 		.key_size = 4,
589 		.value_size = 8,
590 		.max_entries = 0,
591 		.map_flags = BPF_F_NO_PREALLOC,
592 		.btf_key_type_id = 1,
593 		.btf_value_type_id = 3,
594 	};
595 	int fd, btf_fd;
596 
597 	btf_fd = load_btf();
598 	if (btf_fd < 0)
599 		return -1;
600 	attr.btf_fd = btf_fd;
601 	fd = bpf_create_map_xattr(&attr);
602 	close(attr.btf_fd);
603 	if (fd < 0)
604 		printf("Failed to create sk_storage_map\n");
605 	return fd;
606 }
607 
608 static char bpf_vlog[UINT_MAX >> 8];
609 
610 static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type,
611 			  struct bpf_insn *prog, int *map_fds)
612 {
613 	int *fixup_map_hash_8b = test->fixup_map_hash_8b;
614 	int *fixup_map_hash_48b = test->fixup_map_hash_48b;
615 	int *fixup_map_hash_16b = test->fixup_map_hash_16b;
616 	int *fixup_map_array_48b = test->fixup_map_array_48b;
617 	int *fixup_map_sockmap = test->fixup_map_sockmap;
618 	int *fixup_map_sockhash = test->fixup_map_sockhash;
619 	int *fixup_map_xskmap = test->fixup_map_xskmap;
620 	int *fixup_map_stacktrace = test->fixup_map_stacktrace;
621 	int *fixup_prog1 = test->fixup_prog1;
622 	int *fixup_prog2 = test->fixup_prog2;
623 	int *fixup_map_in_map = test->fixup_map_in_map;
624 	int *fixup_cgroup_storage = test->fixup_cgroup_storage;
625 	int *fixup_percpu_cgroup_storage = test->fixup_percpu_cgroup_storage;
626 	int *fixup_map_spin_lock = test->fixup_map_spin_lock;
627 	int *fixup_map_array_ro = test->fixup_map_array_ro;
628 	int *fixup_map_array_wo = test->fixup_map_array_wo;
629 	int *fixup_map_array_small = test->fixup_map_array_small;
630 	int *fixup_sk_storage_map = test->fixup_sk_storage_map;
631 
632 	if (test->fill_helper) {
633 		test->fill_insns = calloc(MAX_TEST_INSNS, sizeof(struct bpf_insn));
634 		test->fill_helper(test);
635 	}
636 
637 	/* Allocating HTs with 1 elem is fine here, since we only test
638 	 * for verifier and not do a runtime lookup, so the only thing
639 	 * that really matters is value size in this case.
640 	 */
641 	if (*fixup_map_hash_8b) {
642 		map_fds[0] = create_map(BPF_MAP_TYPE_HASH, sizeof(long long),
643 					sizeof(long long), 1);
644 		do {
645 			prog[*fixup_map_hash_8b].imm = map_fds[0];
646 			fixup_map_hash_8b++;
647 		} while (*fixup_map_hash_8b);
648 	}
649 
650 	if (*fixup_map_hash_48b) {
651 		map_fds[1] = create_map(BPF_MAP_TYPE_HASH, sizeof(long long),
652 					sizeof(struct test_val), 1);
653 		do {
654 			prog[*fixup_map_hash_48b].imm = map_fds[1];
655 			fixup_map_hash_48b++;
656 		} while (*fixup_map_hash_48b);
657 	}
658 
659 	if (*fixup_map_hash_16b) {
660 		map_fds[2] = create_map(BPF_MAP_TYPE_HASH, sizeof(long long),
661 					sizeof(struct other_val), 1);
662 		do {
663 			prog[*fixup_map_hash_16b].imm = map_fds[2];
664 			fixup_map_hash_16b++;
665 		} while (*fixup_map_hash_16b);
666 	}
667 
668 	if (*fixup_map_array_48b) {
669 		map_fds[3] = create_map(BPF_MAP_TYPE_ARRAY, sizeof(int),
670 					sizeof(struct test_val), 1);
671 		update_map(map_fds[3], 0);
672 		do {
673 			prog[*fixup_map_array_48b].imm = map_fds[3];
674 			fixup_map_array_48b++;
675 		} while (*fixup_map_array_48b);
676 	}
677 
678 	if (*fixup_prog1) {
679 		map_fds[4] = create_prog_array(prog_type, 4, 0);
680 		do {
681 			prog[*fixup_prog1].imm = map_fds[4];
682 			fixup_prog1++;
683 		} while (*fixup_prog1);
684 	}
685 
686 	if (*fixup_prog2) {
687 		map_fds[5] = create_prog_array(prog_type, 8, 7);
688 		do {
689 			prog[*fixup_prog2].imm = map_fds[5];
690 			fixup_prog2++;
691 		} while (*fixup_prog2);
692 	}
693 
694 	if (*fixup_map_in_map) {
695 		map_fds[6] = create_map_in_map();
696 		do {
697 			prog[*fixup_map_in_map].imm = map_fds[6];
698 			fixup_map_in_map++;
699 		} while (*fixup_map_in_map);
700 	}
701 
702 	if (*fixup_cgroup_storage) {
703 		map_fds[7] = create_cgroup_storage(false);
704 		do {
705 			prog[*fixup_cgroup_storage].imm = map_fds[7];
706 			fixup_cgroup_storage++;
707 		} while (*fixup_cgroup_storage);
708 	}
709 
710 	if (*fixup_percpu_cgroup_storage) {
711 		map_fds[8] = create_cgroup_storage(true);
712 		do {
713 			prog[*fixup_percpu_cgroup_storage].imm = map_fds[8];
714 			fixup_percpu_cgroup_storage++;
715 		} while (*fixup_percpu_cgroup_storage);
716 	}
717 	if (*fixup_map_sockmap) {
718 		map_fds[9] = create_map(BPF_MAP_TYPE_SOCKMAP, sizeof(int),
719 					sizeof(int), 1);
720 		do {
721 			prog[*fixup_map_sockmap].imm = map_fds[9];
722 			fixup_map_sockmap++;
723 		} while (*fixup_map_sockmap);
724 	}
725 	if (*fixup_map_sockhash) {
726 		map_fds[10] = create_map(BPF_MAP_TYPE_SOCKHASH, sizeof(int),
727 					sizeof(int), 1);
728 		do {
729 			prog[*fixup_map_sockhash].imm = map_fds[10];
730 			fixup_map_sockhash++;
731 		} while (*fixup_map_sockhash);
732 	}
733 	if (*fixup_map_xskmap) {
734 		map_fds[11] = create_map(BPF_MAP_TYPE_XSKMAP, sizeof(int),
735 					sizeof(int), 1);
736 		do {
737 			prog[*fixup_map_xskmap].imm = map_fds[11];
738 			fixup_map_xskmap++;
739 		} while (*fixup_map_xskmap);
740 	}
741 	if (*fixup_map_stacktrace) {
742 		map_fds[12] = create_map(BPF_MAP_TYPE_STACK_TRACE, sizeof(u32),
743 					 sizeof(u64), 1);
744 		do {
745 			prog[*fixup_map_stacktrace].imm = map_fds[12];
746 			fixup_map_stacktrace++;
747 		} while (*fixup_map_stacktrace);
748 	}
749 	if (*fixup_map_spin_lock) {
750 		map_fds[13] = create_map_spin_lock();
751 		do {
752 			prog[*fixup_map_spin_lock].imm = map_fds[13];
753 			fixup_map_spin_lock++;
754 		} while (*fixup_map_spin_lock);
755 	}
756 	if (*fixup_map_array_ro) {
757 		map_fds[14] = __create_map(BPF_MAP_TYPE_ARRAY, sizeof(int),
758 					   sizeof(struct test_val), 1,
759 					   BPF_F_RDONLY_PROG);
760 		update_map(map_fds[14], 0);
761 		do {
762 			prog[*fixup_map_array_ro].imm = map_fds[14];
763 			fixup_map_array_ro++;
764 		} while (*fixup_map_array_ro);
765 	}
766 	if (*fixup_map_array_wo) {
767 		map_fds[15] = __create_map(BPF_MAP_TYPE_ARRAY, sizeof(int),
768 					   sizeof(struct test_val), 1,
769 					   BPF_F_WRONLY_PROG);
770 		update_map(map_fds[15], 0);
771 		do {
772 			prog[*fixup_map_array_wo].imm = map_fds[15];
773 			fixup_map_array_wo++;
774 		} while (*fixup_map_array_wo);
775 	}
776 	if (*fixup_map_array_small) {
777 		map_fds[16] = __create_map(BPF_MAP_TYPE_ARRAY, sizeof(int),
778 					   1, 1, 0);
779 		update_map(map_fds[16], 0);
780 		do {
781 			prog[*fixup_map_array_small].imm = map_fds[16];
782 			fixup_map_array_small++;
783 		} while (*fixup_map_array_small);
784 	}
785 	if (*fixup_sk_storage_map) {
786 		map_fds[17] = create_sk_storage_map();
787 		do {
788 			prog[*fixup_sk_storage_map].imm = map_fds[17];
789 			fixup_sk_storage_map++;
790 		} while (*fixup_sk_storage_map);
791 	}
792 }
793 
794 static int set_admin(bool admin)
795 {
796 	cap_t caps;
797 	const cap_value_t cap_val = CAP_SYS_ADMIN;
798 	int ret = -1;
799 
800 	caps = cap_get_proc();
801 	if (!caps) {
802 		perror("cap_get_proc");
803 		return -1;
804 	}
805 	if (cap_set_flag(caps, CAP_EFFECTIVE, 1, &cap_val,
806 				admin ? CAP_SET : CAP_CLEAR)) {
807 		perror("cap_set_flag");
808 		goto out;
809 	}
810 	if (cap_set_proc(caps)) {
811 		perror("cap_set_proc");
812 		goto out;
813 	}
814 	ret = 0;
815 out:
816 	if (cap_free(caps))
817 		perror("cap_free");
818 	return ret;
819 }
820 
821 static int do_prog_test_run(int fd_prog, bool unpriv, uint32_t expected_val,
822 			    void *data, size_t size_data)
823 {
824 	__u8 tmp[TEST_DATA_LEN << 2];
825 	__u32 size_tmp = sizeof(tmp);
826 	uint32_t retval;
827 	int err;
828 
829 	if (unpriv)
830 		set_admin(true);
831 	err = bpf_prog_test_run(fd_prog, 1, data, size_data,
832 				tmp, &size_tmp, &retval, NULL);
833 	if (unpriv)
834 		set_admin(false);
835 	if (err && errno != 524/*ENOTSUPP*/ && errno != EPERM) {
836 		printf("Unexpected bpf_prog_test_run error ");
837 		return err;
838 	}
839 	if (!err && retval != expected_val &&
840 	    expected_val != POINTER_VALUE) {
841 		printf("FAIL retval %d != %d ", retval, expected_val);
842 		return 1;
843 	}
844 
845 	return 0;
846 }
847 
848 static void do_test_single(struct bpf_test *test, bool unpriv,
849 			   int *passes, int *errors)
850 {
851 	int fd_prog, expected_ret, alignment_prevented_execution;
852 	int prog_len, prog_type = test->prog_type;
853 	struct bpf_insn *prog = test->insns;
854 	struct bpf_load_program_attr attr;
855 	int run_errs, run_successes;
856 	int map_fds[MAX_NR_MAPS];
857 	const char *expected_err;
858 	int fixup_skips;
859 	__u32 pflags;
860 	int i, err;
861 
862 	for (i = 0; i < MAX_NR_MAPS; i++)
863 		map_fds[i] = -1;
864 
865 	if (!prog_type)
866 		prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
867 	fixup_skips = skips;
868 	do_test_fixup(test, prog_type, prog, map_fds);
869 	if (test->fill_insns) {
870 		prog = test->fill_insns;
871 		prog_len = test->prog_len;
872 	} else {
873 		prog_len = probe_filter_length(prog);
874 	}
875 	/* If there were some map skips during fixup due to missing bpf
876 	 * features, skip this test.
877 	 */
878 	if (fixup_skips != skips)
879 		return;
880 
881 	pflags = BPF_F_TEST_RND_HI32;
882 	if (test->flags & F_LOAD_WITH_STRICT_ALIGNMENT)
883 		pflags |= BPF_F_STRICT_ALIGNMENT;
884 	if (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS)
885 		pflags |= BPF_F_ANY_ALIGNMENT;
886 
887 	memset(&attr, 0, sizeof(attr));
888 	attr.prog_type = prog_type;
889 	attr.expected_attach_type = test->expected_attach_type;
890 	attr.insns = prog;
891 	attr.insns_cnt = prog_len;
892 	attr.license = "GPL";
893 	attr.log_level = 4;
894 	attr.prog_flags = pflags;
895 
896 	fd_prog = bpf_load_program_xattr(&attr, bpf_vlog, sizeof(bpf_vlog));
897 	if (fd_prog < 0 && !bpf_probe_prog_type(prog_type, 0)) {
898 		printf("SKIP (unsupported program type %d)\n", prog_type);
899 		skips++;
900 		goto close_fds;
901 	}
902 
903 	expected_ret = unpriv && test->result_unpriv != UNDEF ?
904 		       test->result_unpriv : test->result;
905 	expected_err = unpriv && test->errstr_unpriv ?
906 		       test->errstr_unpriv : test->errstr;
907 
908 	alignment_prevented_execution = 0;
909 
910 	if (expected_ret == ACCEPT) {
911 		if (fd_prog < 0) {
912 			printf("FAIL\nFailed to load prog '%s'!\n",
913 			       strerror(errno));
914 			goto fail_log;
915 		}
916 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
917 		if (fd_prog >= 0 &&
918 		    (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS))
919 			alignment_prevented_execution = 1;
920 #endif
921 	} else {
922 		if (fd_prog >= 0) {
923 			printf("FAIL\nUnexpected success to load!\n");
924 			goto fail_log;
925 		}
926 		if (!expected_err || !strstr(bpf_vlog, expected_err)) {
927 			printf("FAIL\nUnexpected error message!\n\tEXP: %s\n\tRES: %s\n",
928 			      expected_err, bpf_vlog);
929 			goto fail_log;
930 		}
931 	}
932 
933 	if (test->insn_processed) {
934 		uint32_t insn_processed;
935 		char *proc;
936 
937 		proc = strstr(bpf_vlog, "processed ");
938 		insn_processed = atoi(proc + 10);
939 		if (test->insn_processed != insn_processed) {
940 			printf("FAIL\nUnexpected insn_processed %u vs %u\n",
941 			       insn_processed, test->insn_processed);
942 			goto fail_log;
943 		}
944 	}
945 
946 	run_errs = 0;
947 	run_successes = 0;
948 	if (!alignment_prevented_execution && fd_prog >= 0) {
949 		uint32_t expected_val;
950 		int i;
951 
952 		if (!test->runs) {
953 			expected_val = unpriv && test->retval_unpriv ?
954 				test->retval_unpriv : test->retval;
955 
956 			err = do_prog_test_run(fd_prog, unpriv, expected_val,
957 					       test->data, sizeof(test->data));
958 			if (err)
959 				run_errs++;
960 			else
961 				run_successes++;
962 		}
963 
964 		for (i = 0; i < test->runs; i++) {
965 			if (unpriv && test->retvals[i].retval_unpriv)
966 				expected_val = test->retvals[i].retval_unpriv;
967 			else
968 				expected_val = test->retvals[i].retval;
969 
970 			err = do_prog_test_run(fd_prog, unpriv, expected_val,
971 					       test->retvals[i].data,
972 					       sizeof(test->retvals[i].data));
973 			if (err) {
974 				printf("(run %d/%d) ", i + 1, test->runs);
975 				run_errs++;
976 			} else {
977 				run_successes++;
978 			}
979 		}
980 	}
981 
982 	if (!run_errs) {
983 		(*passes)++;
984 		if (run_successes > 1)
985 			printf("%d cases ", run_successes);
986 		printf("OK");
987 		if (alignment_prevented_execution)
988 			printf(" (NOTE: not executed due to unknown alignment)");
989 		printf("\n");
990 	} else {
991 		printf("\n");
992 		goto fail_log;
993 	}
994 close_fds:
995 	if (test->fill_insns)
996 		free(test->fill_insns);
997 	close(fd_prog);
998 	for (i = 0; i < MAX_NR_MAPS; i++)
999 		close(map_fds[i]);
1000 	sched_yield();
1001 	return;
1002 fail_log:
1003 	(*errors)++;
1004 	printf("%s", bpf_vlog);
1005 	goto close_fds;
1006 }
1007 
1008 static bool is_admin(void)
1009 {
1010 	cap_t caps;
1011 	cap_flag_value_t sysadmin = CAP_CLEAR;
1012 	const cap_value_t cap_val = CAP_SYS_ADMIN;
1013 
1014 #ifdef CAP_IS_SUPPORTED
1015 	if (!CAP_IS_SUPPORTED(CAP_SETFCAP)) {
1016 		perror("cap_get_flag");
1017 		return false;
1018 	}
1019 #endif
1020 	caps = cap_get_proc();
1021 	if (!caps) {
1022 		perror("cap_get_proc");
1023 		return false;
1024 	}
1025 	if (cap_get_flag(caps, cap_val, CAP_EFFECTIVE, &sysadmin))
1026 		perror("cap_get_flag");
1027 	if (cap_free(caps))
1028 		perror("cap_free");
1029 	return (sysadmin == CAP_SET);
1030 }
1031 
1032 static void get_unpriv_disabled()
1033 {
1034 	char buf[2];
1035 	FILE *fd;
1036 
1037 	fd = fopen("/proc/sys/"UNPRIV_SYSCTL, "r");
1038 	if (!fd) {
1039 		perror("fopen /proc/sys/"UNPRIV_SYSCTL);
1040 		unpriv_disabled = true;
1041 		return;
1042 	}
1043 	if (fgets(buf, 2, fd) == buf && atoi(buf))
1044 		unpriv_disabled = true;
1045 	fclose(fd);
1046 }
1047 
1048 static bool test_as_unpriv(struct bpf_test *test)
1049 {
1050 	return !test->prog_type ||
1051 	       test->prog_type == BPF_PROG_TYPE_SOCKET_FILTER ||
1052 	       test->prog_type == BPF_PROG_TYPE_CGROUP_SKB;
1053 }
1054 
1055 static int do_test(bool unpriv, unsigned int from, unsigned int to)
1056 {
1057 	int i, passes = 0, errors = 0;
1058 
1059 	for (i = from; i < to; i++) {
1060 		struct bpf_test *test = &tests[i];
1061 
1062 		/* Program types that are not supported by non-root we
1063 		 * skip right away.
1064 		 */
1065 		if (test_as_unpriv(test) && unpriv_disabled) {
1066 			printf("#%d/u %s SKIP\n", i, test->descr);
1067 			skips++;
1068 		} else if (test_as_unpriv(test)) {
1069 			if (!unpriv)
1070 				set_admin(false);
1071 			printf("#%d/u %s ", i, test->descr);
1072 			do_test_single(test, true, &passes, &errors);
1073 			if (!unpriv)
1074 				set_admin(true);
1075 		}
1076 
1077 		if (unpriv) {
1078 			printf("#%d/p %s SKIP\n", i, test->descr);
1079 			skips++;
1080 		} else {
1081 			printf("#%d/p %s ", i, test->descr);
1082 			do_test_single(test, false, &passes, &errors);
1083 		}
1084 	}
1085 
1086 	printf("Summary: %d PASSED, %d SKIPPED, %d FAILED\n", passes,
1087 	       skips, errors);
1088 	return errors ? EXIT_FAILURE : EXIT_SUCCESS;
1089 }
1090 
1091 int main(int argc, char **argv)
1092 {
1093 	unsigned int from = 0, to = ARRAY_SIZE(tests);
1094 	bool unpriv = !is_admin();
1095 
1096 	if (argc == 3) {
1097 		unsigned int l = atoi(argv[argc - 2]);
1098 		unsigned int u = atoi(argv[argc - 1]);
1099 
1100 		if (l < to && u < to) {
1101 			from = l;
1102 			to   = u + 1;
1103 		}
1104 	} else if (argc == 2) {
1105 		unsigned int t = atoi(argv[argc - 1]);
1106 
1107 		if (t < to) {
1108 			from = t;
1109 			to   = t + 1;
1110 		}
1111 	}
1112 
1113 	get_unpriv_disabled();
1114 	if (unpriv && unpriv_disabled) {
1115 		printf("Cannot run as unprivileged user with sysctl %s.\n",
1116 		       UNPRIV_SYSCTL);
1117 		return EXIT_FAILURE;
1118 	}
1119 
1120 	bpf_semi_rand_init();
1121 	return do_test(unpriv, from, to);
1122 }
1123