1 // SPDX-License-Identifier: GPL-2.0-only
3 * Testsuite for eBPF verifier
5 * Copyright (c) 2014 PLUMgrid, http://plumgrid.com
6 * Copyright (c) 2017 Facebook
7 * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io
11 #include <asm/types.h>
12 #include <linux/types.h>
25 #include <sys/capability.h>
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>
35 #include <bpf/libbpf.h>
38 # include "autoconf.h"
40 # if defined(__i386) || defined(__x86_64) || defined(__s390x__) || defined(__aarch64__)
41 # define CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 1
44 #include "bpf_rlimit.h"
48 #include "../../../include/linux/filter.h"
50 #define MAX_INSNS BPF_MAXINSNS
51 #define MAX_TEST_INSNS 1000000
53 #define MAX_NR_MAPS 21
54 #define MAX_TEST_RUNS 8
55 #define POINTER_VALUE 0xcafe4all
56 #define TEST_DATA_LEN 64
58 #define F_NEEDS_EFFICIENT_UNALIGNED_ACCESS (1 << 0)
59 #define F_LOAD_WITH_STRICT_ALIGNMENT (1 << 1)
61 #define UNPRIV_SYSCTL "kernel/unprivileged_bpf_disabled"
62 static bool unpriv_disabled = false;
64 static bool verbose = false;
68 struct bpf_insn insns[MAX_INSNS];
69 struct bpf_insn *fill_insns;
70 int fixup_map_hash_8b[MAX_FIXUPS];
71 int fixup_map_hash_48b[MAX_FIXUPS];
72 int fixup_map_hash_16b[MAX_FIXUPS];
73 int fixup_map_array_48b[MAX_FIXUPS];
74 int fixup_map_sockmap[MAX_FIXUPS];
75 int fixup_map_sockhash[MAX_FIXUPS];
76 int fixup_map_xskmap[MAX_FIXUPS];
77 int fixup_map_stacktrace[MAX_FIXUPS];
78 int fixup_prog1[MAX_FIXUPS];
79 int fixup_prog2[MAX_FIXUPS];
80 int fixup_map_in_map[MAX_FIXUPS];
81 int fixup_cgroup_storage[MAX_FIXUPS];
82 int fixup_percpu_cgroup_storage[MAX_FIXUPS];
83 int fixup_map_spin_lock[MAX_FIXUPS];
84 int fixup_map_array_ro[MAX_FIXUPS];
85 int fixup_map_array_wo[MAX_FIXUPS];
86 int fixup_map_array_small[MAX_FIXUPS];
87 int fixup_sk_storage_map[MAX_FIXUPS];
88 int fixup_map_event_output[MAX_FIXUPS];
89 int fixup_map_reuseport_array[MAX_FIXUPS];
90 int fixup_map_ringbuf[MAX_FIXUPS];
91 /* Expected verifier log output for result REJECT or VERBOSE_ACCEPT.
92 * Can be a tab-separated sequence of expected strings. An empty string
93 * means no log verification.
96 const char *errstr_unpriv;
97 uint32_t insn_processed;
104 } result, result_unpriv;
105 enum bpf_prog_type prog_type;
107 void (*fill_helper)(struct bpf_test *self);
109 #define bpf_testdata_struct_t \
111 uint32_t retval, retval_unpriv; \
113 __u8 data[TEST_DATA_LEN]; \
114 __u64 data64[TEST_DATA_LEN / 8]; \
118 bpf_testdata_struct_t;
119 bpf_testdata_struct_t retvals[MAX_TEST_RUNS];
121 enum bpf_attach_type expected_attach_type;
125 /* Note we want this to be 64 bit aligned so that the end of our array is
126 * actually the end of the structure.
128 #define MAX_ENTRIES 11
132 int foo[MAX_ENTRIES];
140 static void bpf_fill_ld_abs_vlan_push_pop(struct bpf_test *self)
142 /* test: {skb->data[0], vlan_push} x 51 + {skb->data[0], vlan_pop} x 51 */
144 /* jump range is limited to 16 bit. PUSH_CNT of ld_abs needs room */
145 unsigned int len = (1 << 15) - PUSH_CNT * 2 * 5 * 6;
146 struct bpf_insn *insn = self->fill_insns;
149 insn[i++] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
151 for (j = 0; j < PUSH_CNT; j++) {
152 insn[i++] = BPF_LD_ABS(BPF_B, 0);
153 /* jump to error label */
154 insn[i] = BPF_JMP32_IMM(BPF_JNE, BPF_REG_0, 0x34, len - i - 3);
156 insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
157 insn[i++] = BPF_MOV64_IMM(BPF_REG_2, 1);
158 insn[i++] = BPF_MOV64_IMM(BPF_REG_3, 2);
159 insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
160 BPF_FUNC_skb_vlan_push),
161 insn[i] = BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, len - i - 3);
165 for (j = 0; j < PUSH_CNT; j++) {
166 insn[i++] = BPF_LD_ABS(BPF_B, 0);
167 insn[i] = BPF_JMP32_IMM(BPF_JNE, BPF_REG_0, 0x34, len - i - 3);
169 insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
170 insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
171 BPF_FUNC_skb_vlan_pop),
172 insn[i] = BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, len - i - 3);
178 for (; i < len - 3; i++)
179 insn[i] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 0xbef);
180 insn[len - 3] = BPF_JMP_A(1);
182 insn[len - 2] = BPF_MOV32_IMM(BPF_REG_0, 0);
183 insn[len - 1] = BPF_EXIT_INSN();
184 self->prog_len = len;
187 static void bpf_fill_jump_around_ld_abs(struct bpf_test *self)
189 struct bpf_insn *insn = self->fill_insns;
190 /* jump range is limited to 16 bit. every ld_abs is replaced by 6 insns,
191 * but on arches like arm, ppc etc, there will be one BPF_ZEXT inserted
192 * to extend the error value of the inlined ld_abs sequence which then
193 * contains 7 insns. so, set the dividend to 7 so the testcase could
194 * work on all arches.
196 unsigned int len = (1 << 15) / 7;
199 insn[i++] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
200 insn[i++] = BPF_LD_ABS(BPF_B, 0);
201 insn[i] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 10, len - i - 2);
204 insn[i++] = BPF_LD_ABS(BPF_B, 1);
205 insn[i] = BPF_EXIT_INSN();
206 self->prog_len = i + 1;
209 static void bpf_fill_rand_ld_dw(struct bpf_test *self)
211 struct bpf_insn *insn = self->fill_insns;
215 insn[i++] = BPF_MOV32_IMM(BPF_REG_0, 0);
216 while (i < self->retval) {
217 uint64_t val = bpf_semi_rand_get();
218 struct bpf_insn tmp[2] = { BPF_LD_IMM64(BPF_REG_1, val) };
223 insn[i++] = BPF_ALU64_REG(BPF_XOR, BPF_REG_0, BPF_REG_1);
225 insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_0);
226 insn[i++] = BPF_ALU64_IMM(BPF_RSH, BPF_REG_1, 32);
227 insn[i++] = BPF_ALU64_REG(BPF_XOR, BPF_REG_0, BPF_REG_1);
228 insn[i] = BPF_EXIT_INSN();
229 self->prog_len = i + 1;
231 self->retval = (uint32_t)res;
234 #define MAX_JMP_SEQ 8192
236 /* test the sequence of 8k jumps */
237 static void bpf_fill_scale1(struct bpf_test *self)
239 struct bpf_insn *insn = self->fill_insns;
242 insn[i++] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
243 /* test to check that the long sequence of jumps is acceptable */
244 while (k++ < MAX_JMP_SEQ) {
245 insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
246 BPF_FUNC_get_prandom_u32);
247 insn[i++] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, bpf_semi_rand_get(), 2);
248 insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_10);
249 insn[i++] = BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6,
252 /* is_state_visited() doesn't allocate state for pruning for every jump.
253 * Hence multiply jmps by 4 to accommodate that heuristic
255 while (i < MAX_TEST_INSNS - MAX_JMP_SEQ * 4)
256 insn[i++] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 42);
257 insn[i] = BPF_EXIT_INSN();
258 self->prog_len = i + 1;
262 /* test the sequence of 8k jumps in inner most function (function depth 8)*/
263 static void bpf_fill_scale2(struct bpf_test *self)
265 struct bpf_insn *insn = self->fill_insns;
269 for (k = 0; k < FUNC_NEST; k++) {
270 insn[i++] = BPF_CALL_REL(1);
271 insn[i++] = BPF_EXIT_INSN();
273 insn[i++] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
274 /* test to check that the long sequence of jumps is acceptable */
276 while (k++ < MAX_JMP_SEQ) {
277 insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
278 BPF_FUNC_get_prandom_u32);
279 insn[i++] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, bpf_semi_rand_get(), 2);
280 insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_10);
281 insn[i++] = BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6,
282 -8 * (k % (64 - 4 * FUNC_NEST) + 1));
284 while (i < MAX_TEST_INSNS - MAX_JMP_SEQ * 4)
285 insn[i++] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 42);
286 insn[i] = BPF_EXIT_INSN();
287 self->prog_len = i + 1;
291 static void bpf_fill_scale(struct bpf_test *self)
293 switch (self->retval) {
295 return bpf_fill_scale1(self);
297 return bpf_fill_scale2(self);
304 static int bpf_fill_torturous_jumps_insn_1(struct bpf_insn *insn)
306 unsigned int len = 259, hlen = 128;
309 insn[0] = BPF_EMIT_CALL(BPF_FUNC_get_prandom_u32);
310 for (i = 1; i <= hlen; i++) {
311 insn[i] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, i, hlen);
312 insn[i + hlen] = BPF_JMP_A(hlen - i);
314 insn[len - 2] = BPF_MOV64_IMM(BPF_REG_0, 1);
315 insn[len - 1] = BPF_EXIT_INSN();
320 static int bpf_fill_torturous_jumps_insn_2(struct bpf_insn *insn)
322 unsigned int len = 4100, jmp_off = 2048;
325 insn[0] = BPF_EMIT_CALL(BPF_FUNC_get_prandom_u32);
326 for (i = 1; i <= jmp_off; i++) {
327 insn[i] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, i, jmp_off);
329 insn[i++] = BPF_JMP_A(jmp_off);
330 for (; i <= jmp_off * 2 + 1; i+=16) {
331 for (j = 0; j < 16; j++) {
332 insn[i + j] = BPF_JMP_A(16 - j - 1);
336 insn[len - 2] = BPF_MOV64_IMM(BPF_REG_0, 2);
337 insn[len - 1] = BPF_EXIT_INSN();
342 static void bpf_fill_torturous_jumps(struct bpf_test *self)
344 struct bpf_insn *insn = self->fill_insns;
347 switch (self->retval) {
349 self->prog_len = bpf_fill_torturous_jumps_insn_1(insn);
352 self->prog_len = bpf_fill_torturous_jumps_insn_2(insn);
356 insn[i++] = BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 4);
357 insn[i++] = BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 262);
358 insn[i++] = BPF_ST_MEM(BPF_B, BPF_REG_10, -32, 0);
359 insn[i++] = BPF_MOV64_IMM(BPF_REG_0, 3);
360 insn[i++] = BPF_EXIT_INSN();
363 i += bpf_fill_torturous_jumps_insn_1(insn + i);
366 i += bpf_fill_torturous_jumps_insn_2(insn + i);
376 /* BPF_SK_LOOKUP contains 13 instructions, if you need to fix up maps */
377 #define BPF_SK_LOOKUP(func) \
378 /* struct bpf_sock_tuple tuple = {} */ \
379 BPF_MOV64_IMM(BPF_REG_2, 0), \
380 BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_2, -8), \
381 BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -16), \
382 BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -24), \
383 BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -32), \
384 BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -40), \
385 BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -48), \
386 /* sk = func(ctx, &tuple, sizeof tuple, 0, 0) */ \
387 BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), \
388 BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -48), \
389 BPF_MOV64_IMM(BPF_REG_3, sizeof(struct bpf_sock_tuple)), \
390 BPF_MOV64_IMM(BPF_REG_4, 0), \
391 BPF_MOV64_IMM(BPF_REG_5, 0), \
392 BPF_EMIT_CALL(BPF_FUNC_ ## func)
394 /* BPF_DIRECT_PKT_R2 contains 7 instructions, it initializes default return
395 * value into 0 and does necessary preparation for direct packet access
396 * through r2. The allowed access range is 8 bytes.
398 #define BPF_DIRECT_PKT_R2 \
399 BPF_MOV64_IMM(BPF_REG_0, 0), \
400 BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, \
401 offsetof(struct __sk_buff, data)), \
402 BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1, \
403 offsetof(struct __sk_buff, data_end)), \
404 BPF_MOV64_REG(BPF_REG_4, BPF_REG_2), \
405 BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 8), \
406 BPF_JMP_REG(BPF_JLE, BPF_REG_4, BPF_REG_3, 1), \
409 /* BPF_RAND_UEXT_R7 contains 4 instructions, it initializes R7 into a random
410 * positive u32, and zero-extend it into 64-bit.
412 #define BPF_RAND_UEXT_R7 \
413 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, \
414 BPF_FUNC_get_prandom_u32), \
415 BPF_MOV64_REG(BPF_REG_7, BPF_REG_0), \
416 BPF_ALU64_IMM(BPF_LSH, BPF_REG_7, 33), \
417 BPF_ALU64_IMM(BPF_RSH, BPF_REG_7, 33)
419 /* BPF_RAND_SEXT_R7 contains 5 instructions, it initializes R7 into a random
420 * negative u32, and sign-extend it into 64-bit.
422 #define BPF_RAND_SEXT_R7 \
423 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, \
424 BPF_FUNC_get_prandom_u32), \
425 BPF_MOV64_REG(BPF_REG_7, BPF_REG_0), \
426 BPF_ALU64_IMM(BPF_OR, BPF_REG_7, 0x80000000), \
427 BPF_ALU64_IMM(BPF_LSH, BPF_REG_7, 32), \
428 BPF_ALU64_IMM(BPF_ARSH, BPF_REG_7, 32)
430 static struct bpf_test tests[] = {
432 #include <verifier/tests.h>
436 static int probe_filter_length(const struct bpf_insn *fp)
440 for (len = MAX_INSNS - 1; len > 0; --len)
441 if (fp[len].code != 0 || fp[len].imm != 0)
446 static bool skip_unsupported_map(enum bpf_map_type map_type)
448 if (!bpf_probe_map_type(map_type, 0)) {
449 printf("SKIP (unsupported map type %d)\n", map_type);
456 static int __create_map(uint32_t type, uint32_t size_key,
457 uint32_t size_value, uint32_t max_elem,
458 uint32_t extra_flags)
462 fd = bpf_create_map(type, size_key, size_value, max_elem,
463 (type == BPF_MAP_TYPE_HASH ?
464 BPF_F_NO_PREALLOC : 0) | extra_flags);
466 if (skip_unsupported_map(type))
468 printf("Failed to create hash map '%s'!\n", strerror(errno));
474 static int create_map(uint32_t type, uint32_t size_key,
475 uint32_t size_value, uint32_t max_elem)
477 return __create_map(type, size_key, size_value, max_elem, 0);
480 static void update_map(int fd, int index)
482 struct test_val value = {
483 .index = (6 + 1) * sizeof(int),
484 .foo[6] = 0xabcdef12,
487 assert(!bpf_map_update_elem(fd, &index, &value, 0));
490 static int create_prog_dummy_simple(enum bpf_prog_type prog_type, int ret)
492 struct bpf_insn prog[] = {
493 BPF_MOV64_IMM(BPF_REG_0, ret),
497 return bpf_load_program(prog_type, prog,
498 ARRAY_SIZE(prog), "GPL", 0, NULL, 0);
501 static int create_prog_dummy_loop(enum bpf_prog_type prog_type, int mfd,
504 struct bpf_insn prog[] = {
505 BPF_MOV64_IMM(BPF_REG_3, idx),
506 BPF_LD_MAP_FD(BPF_REG_2, mfd),
507 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
509 BPF_MOV64_IMM(BPF_REG_0, ret),
513 return bpf_load_program(prog_type, prog,
514 ARRAY_SIZE(prog), "GPL", 0, NULL, 0);
517 static int create_prog_array(enum bpf_prog_type prog_type, uint32_t max_elem,
518 int p1key, int p2key, int p3key)
520 int mfd, p1fd, p2fd, p3fd;
522 mfd = bpf_create_map(BPF_MAP_TYPE_PROG_ARRAY, sizeof(int),
523 sizeof(int), max_elem, 0);
525 if (skip_unsupported_map(BPF_MAP_TYPE_PROG_ARRAY))
527 printf("Failed to create prog array '%s'!\n", strerror(errno));
531 p1fd = create_prog_dummy_simple(prog_type, 42);
532 p2fd = create_prog_dummy_loop(prog_type, mfd, p2key, 41);
533 p3fd = create_prog_dummy_simple(prog_type, 24);
534 if (p1fd < 0 || p2fd < 0 || p3fd < 0)
536 if (bpf_map_update_elem(mfd, &p1key, &p1fd, BPF_ANY) < 0)
538 if (bpf_map_update_elem(mfd, &p2key, &p2fd, BPF_ANY) < 0)
540 if (bpf_map_update_elem(mfd, &p3key, &p3fd, BPF_ANY) < 0) {
551 static int create_map_in_map(void)
553 int inner_map_fd, outer_map_fd;
555 inner_map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(int),
557 if (inner_map_fd < 0) {
558 if (skip_unsupported_map(BPF_MAP_TYPE_ARRAY))
560 printf("Failed to create array '%s'!\n", strerror(errno));
564 outer_map_fd = bpf_create_map_in_map(BPF_MAP_TYPE_ARRAY_OF_MAPS, NULL,
565 sizeof(int), inner_map_fd, 1, 0);
566 if (outer_map_fd < 0) {
567 if (skip_unsupported_map(BPF_MAP_TYPE_ARRAY_OF_MAPS))
569 printf("Failed to create array of maps '%s'!\n",
578 static int create_cgroup_storage(bool percpu)
580 enum bpf_map_type type = percpu ? BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE :
581 BPF_MAP_TYPE_CGROUP_STORAGE;
584 fd = bpf_create_map(type, sizeof(struct bpf_cgroup_storage_key),
585 TEST_DATA_LEN, 0, 0);
587 if (skip_unsupported_map(type))
589 printf("Failed to create cgroup storage '%s'!\n",
596 /* struct bpf_spin_lock {
601 * struct bpf_spin_lock l;
604 static const char btf_str_sec[] = "\0bpf_spin_lock\0val\0cnt\0l";
605 static __u32 btf_raw_types[] = {
607 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
608 /* struct bpf_spin_lock */ /* [2] */
609 BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4),
610 BTF_MEMBER_ENC(15, 1, 0), /* int val; */
611 /* struct val */ /* [3] */
612 BTF_TYPE_ENC(15, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
613 BTF_MEMBER_ENC(19, 1, 0), /* int cnt; */
614 BTF_MEMBER_ENC(23, 2, 32),/* struct bpf_spin_lock l; */
617 static int load_btf(void)
619 struct btf_header hdr = {
621 .version = BTF_VERSION,
622 .hdr_len = sizeof(struct btf_header),
623 .type_len = sizeof(btf_raw_types),
624 .str_off = sizeof(btf_raw_types),
625 .str_len = sizeof(btf_str_sec),
630 ptr = raw_btf = malloc(sizeof(hdr) + sizeof(btf_raw_types) +
631 sizeof(btf_str_sec));
633 memcpy(ptr, &hdr, sizeof(hdr));
635 memcpy(ptr, btf_raw_types, hdr.type_len);
637 memcpy(ptr, btf_str_sec, hdr.str_len);
640 btf_fd = bpf_load_btf(raw_btf, ptr - raw_btf, 0, 0, 0);
647 static int create_map_spin_lock(void)
649 struct bpf_create_map_attr attr = {
651 .map_type = BPF_MAP_TYPE_ARRAY,
655 .btf_key_type_id = 1,
656 .btf_value_type_id = 3,
663 attr.btf_fd = btf_fd;
664 fd = bpf_create_map_xattr(&attr);
666 printf("Failed to create map with spin_lock\n");
670 static int create_sk_storage_map(void)
672 struct bpf_create_map_attr attr = {
674 .map_type = BPF_MAP_TYPE_SK_STORAGE,
678 .map_flags = BPF_F_NO_PREALLOC,
679 .btf_key_type_id = 1,
680 .btf_value_type_id = 3,
687 attr.btf_fd = btf_fd;
688 fd = bpf_create_map_xattr(&attr);
691 printf("Failed to create sk_storage_map\n");
695 static char bpf_vlog[UINT_MAX >> 8];
697 static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type,
698 struct bpf_insn *prog, int *map_fds)
700 int *fixup_map_hash_8b = test->fixup_map_hash_8b;
701 int *fixup_map_hash_48b = test->fixup_map_hash_48b;
702 int *fixup_map_hash_16b = test->fixup_map_hash_16b;
703 int *fixup_map_array_48b = test->fixup_map_array_48b;
704 int *fixup_map_sockmap = test->fixup_map_sockmap;
705 int *fixup_map_sockhash = test->fixup_map_sockhash;
706 int *fixup_map_xskmap = test->fixup_map_xskmap;
707 int *fixup_map_stacktrace = test->fixup_map_stacktrace;
708 int *fixup_prog1 = test->fixup_prog1;
709 int *fixup_prog2 = test->fixup_prog2;
710 int *fixup_map_in_map = test->fixup_map_in_map;
711 int *fixup_cgroup_storage = test->fixup_cgroup_storage;
712 int *fixup_percpu_cgroup_storage = test->fixup_percpu_cgroup_storage;
713 int *fixup_map_spin_lock = test->fixup_map_spin_lock;
714 int *fixup_map_array_ro = test->fixup_map_array_ro;
715 int *fixup_map_array_wo = test->fixup_map_array_wo;
716 int *fixup_map_array_small = test->fixup_map_array_small;
717 int *fixup_sk_storage_map = test->fixup_sk_storage_map;
718 int *fixup_map_event_output = test->fixup_map_event_output;
719 int *fixup_map_reuseport_array = test->fixup_map_reuseport_array;
720 int *fixup_map_ringbuf = test->fixup_map_ringbuf;
722 if (test->fill_helper) {
723 test->fill_insns = calloc(MAX_TEST_INSNS, sizeof(struct bpf_insn));
724 test->fill_helper(test);
727 /* Allocating HTs with 1 elem is fine here, since we only test
728 * for verifier and not do a runtime lookup, so the only thing
729 * that really matters is value size in this case.
731 if (*fixup_map_hash_8b) {
732 map_fds[0] = create_map(BPF_MAP_TYPE_HASH, sizeof(long long),
733 sizeof(long long), 1);
735 prog[*fixup_map_hash_8b].imm = map_fds[0];
737 } while (*fixup_map_hash_8b);
740 if (*fixup_map_hash_48b) {
741 map_fds[1] = create_map(BPF_MAP_TYPE_HASH, sizeof(long long),
742 sizeof(struct test_val), 1);
744 prog[*fixup_map_hash_48b].imm = map_fds[1];
745 fixup_map_hash_48b++;
746 } while (*fixup_map_hash_48b);
749 if (*fixup_map_hash_16b) {
750 map_fds[2] = create_map(BPF_MAP_TYPE_HASH, sizeof(long long),
751 sizeof(struct other_val), 1);
753 prog[*fixup_map_hash_16b].imm = map_fds[2];
754 fixup_map_hash_16b++;
755 } while (*fixup_map_hash_16b);
758 if (*fixup_map_array_48b) {
759 map_fds[3] = create_map(BPF_MAP_TYPE_ARRAY, sizeof(int),
760 sizeof(struct test_val), 1);
761 update_map(map_fds[3], 0);
763 prog[*fixup_map_array_48b].imm = map_fds[3];
764 fixup_map_array_48b++;
765 } while (*fixup_map_array_48b);
769 map_fds[4] = create_prog_array(prog_type, 4, 0, 1, 2);
771 prog[*fixup_prog1].imm = map_fds[4];
773 } while (*fixup_prog1);
777 map_fds[5] = create_prog_array(prog_type, 8, 7, 1, 2);
779 prog[*fixup_prog2].imm = map_fds[5];
781 } while (*fixup_prog2);
784 if (*fixup_map_in_map) {
785 map_fds[6] = create_map_in_map();
787 prog[*fixup_map_in_map].imm = map_fds[6];
789 } while (*fixup_map_in_map);
792 if (*fixup_cgroup_storage) {
793 map_fds[7] = create_cgroup_storage(false);
795 prog[*fixup_cgroup_storage].imm = map_fds[7];
796 fixup_cgroup_storage++;
797 } while (*fixup_cgroup_storage);
800 if (*fixup_percpu_cgroup_storage) {
801 map_fds[8] = create_cgroup_storage(true);
803 prog[*fixup_percpu_cgroup_storage].imm = map_fds[8];
804 fixup_percpu_cgroup_storage++;
805 } while (*fixup_percpu_cgroup_storage);
807 if (*fixup_map_sockmap) {
808 map_fds[9] = create_map(BPF_MAP_TYPE_SOCKMAP, sizeof(int),
811 prog[*fixup_map_sockmap].imm = map_fds[9];
813 } while (*fixup_map_sockmap);
815 if (*fixup_map_sockhash) {
816 map_fds[10] = create_map(BPF_MAP_TYPE_SOCKHASH, sizeof(int),
819 prog[*fixup_map_sockhash].imm = map_fds[10];
820 fixup_map_sockhash++;
821 } while (*fixup_map_sockhash);
823 if (*fixup_map_xskmap) {
824 map_fds[11] = create_map(BPF_MAP_TYPE_XSKMAP, sizeof(int),
827 prog[*fixup_map_xskmap].imm = map_fds[11];
829 } while (*fixup_map_xskmap);
831 if (*fixup_map_stacktrace) {
832 map_fds[12] = create_map(BPF_MAP_TYPE_STACK_TRACE, sizeof(u32),
835 prog[*fixup_map_stacktrace].imm = map_fds[12];
836 fixup_map_stacktrace++;
837 } while (*fixup_map_stacktrace);
839 if (*fixup_map_spin_lock) {
840 map_fds[13] = create_map_spin_lock();
842 prog[*fixup_map_spin_lock].imm = map_fds[13];
843 fixup_map_spin_lock++;
844 } while (*fixup_map_spin_lock);
846 if (*fixup_map_array_ro) {
847 map_fds[14] = __create_map(BPF_MAP_TYPE_ARRAY, sizeof(int),
848 sizeof(struct test_val), 1,
850 update_map(map_fds[14], 0);
852 prog[*fixup_map_array_ro].imm = map_fds[14];
853 fixup_map_array_ro++;
854 } while (*fixup_map_array_ro);
856 if (*fixup_map_array_wo) {
857 map_fds[15] = __create_map(BPF_MAP_TYPE_ARRAY, sizeof(int),
858 sizeof(struct test_val), 1,
860 update_map(map_fds[15], 0);
862 prog[*fixup_map_array_wo].imm = map_fds[15];
863 fixup_map_array_wo++;
864 } while (*fixup_map_array_wo);
866 if (*fixup_map_array_small) {
867 map_fds[16] = __create_map(BPF_MAP_TYPE_ARRAY, sizeof(int),
869 update_map(map_fds[16], 0);
871 prog[*fixup_map_array_small].imm = map_fds[16];
872 fixup_map_array_small++;
873 } while (*fixup_map_array_small);
875 if (*fixup_sk_storage_map) {
876 map_fds[17] = create_sk_storage_map();
878 prog[*fixup_sk_storage_map].imm = map_fds[17];
879 fixup_sk_storage_map++;
880 } while (*fixup_sk_storage_map);
882 if (*fixup_map_event_output) {
883 map_fds[18] = __create_map(BPF_MAP_TYPE_PERF_EVENT_ARRAY,
884 sizeof(int), sizeof(int), 1, 0);
886 prog[*fixup_map_event_output].imm = map_fds[18];
887 fixup_map_event_output++;
888 } while (*fixup_map_event_output);
890 if (*fixup_map_reuseport_array) {
891 map_fds[19] = __create_map(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY,
892 sizeof(u32), sizeof(u64), 1, 0);
894 prog[*fixup_map_reuseport_array].imm = map_fds[19];
895 fixup_map_reuseport_array++;
896 } while (*fixup_map_reuseport_array);
898 if (*fixup_map_ringbuf) {
899 map_fds[20] = create_map(BPF_MAP_TYPE_RINGBUF, 0,
902 prog[*fixup_map_ringbuf].imm = map_fds[20];
904 } while (*fixup_map_ringbuf);
909 struct __user_cap_header_struct hdr;
910 struct __user_cap_data_struct data[2];
913 static int set_admin(bool admin)
916 /* need CAP_BPF, CAP_NET_ADMIN, CAP_PERFMON to load progs */
917 const cap_value_t cap_net_admin = CAP_NET_ADMIN;
918 const cap_value_t cap_sys_admin = CAP_SYS_ADMIN;
922 caps = cap_get_proc();
924 perror("cap_get_proc");
927 cap = (struct libcap *)caps;
928 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, &cap_sys_admin, CAP_CLEAR)) {
929 perror("cap_set_flag clear admin");
932 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, &cap_net_admin,
933 admin ? CAP_SET : CAP_CLEAR)) {
934 perror("cap_set_flag set_or_clear net");
937 /* libcap is likely old and simply ignores CAP_BPF and CAP_PERFMON,
938 * so update effective bits manually
941 cap->data[1].effective |= 1 << (38 /* CAP_PERFMON */ - 32);
942 cap->data[1].effective |= 1 << (39 /* CAP_BPF */ - 32);
944 cap->data[1].effective &= ~(1 << (38 - 32));
945 cap->data[1].effective &= ~(1 << (39 - 32));
947 if (cap_set_proc(caps)) {
948 perror("cap_set_proc");
958 static int do_prog_test_run(int fd_prog, bool unpriv, uint32_t expected_val,
959 void *data, size_t size_data)
961 __u8 tmp[TEST_DATA_LEN << 2];
962 __u32 size_tmp = sizeof(tmp);
964 int err, saved_errno;
968 err = bpf_prog_test_run(fd_prog, 1, data, size_data,
969 tmp, &size_tmp, &retval, NULL);
976 switch (saved_errno) {
977 case 524/*ENOTSUPP*/:
978 printf("Did not run the program (not supported) ");
982 printf("Did not run the program (no permission) ");
987 printf("FAIL: Unexpected bpf_prog_test_run error (%s) ",
988 strerror(saved_errno));
993 if (retval != expected_val &&
994 expected_val != POINTER_VALUE) {
995 printf("FAIL retval %d != %d ", retval, expected_val);
1002 /* Returns true if every part of exp (tab-separated) appears in log, in order.
1004 * If exp is an empty string, returns true.
1006 static bool cmp_str_seq(const char *log, const char *exp)
1015 p = strchr(exp, '\t');
1017 p = exp + strlen(exp);
1020 if (len >= sizeof(needle) || !len) {
1021 printf("FAIL\nTestcase bug\n");
1024 strncpy(needle, exp, len);
1026 q = strstr(log, needle);
1028 printf("FAIL\nUnexpected verifier log!\n"
1029 "EXP: %s\nRES:\n", needle);
1038 static void do_test_single(struct bpf_test *test, bool unpriv,
1039 int *passes, int *errors)
1041 int fd_prog, expected_ret, alignment_prevented_execution;
1042 int prog_len, prog_type = test->prog_type;
1043 struct bpf_insn *prog = test->insns;
1044 struct bpf_load_program_attr attr;
1045 int run_errs, run_successes;
1046 int map_fds[MAX_NR_MAPS];
1047 const char *expected_err;
1053 for (i = 0; i < MAX_NR_MAPS; i++)
1057 prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
1058 fixup_skips = skips;
1059 do_test_fixup(test, prog_type, prog, map_fds);
1060 if (test->fill_insns) {
1061 prog = test->fill_insns;
1062 prog_len = test->prog_len;
1064 prog_len = probe_filter_length(prog);
1066 /* If there were some map skips during fixup due to missing bpf
1067 * features, skip this test.
1069 if (fixup_skips != skips)
1072 pflags = BPF_F_TEST_RND_HI32;
1073 if (test->flags & F_LOAD_WITH_STRICT_ALIGNMENT)
1074 pflags |= BPF_F_STRICT_ALIGNMENT;
1075 if (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS)
1076 pflags |= BPF_F_ANY_ALIGNMENT;
1077 if (test->flags & ~3)
1078 pflags |= test->flags;
1080 expected_ret = unpriv && test->result_unpriv != UNDEF ?
1081 test->result_unpriv : test->result;
1082 expected_err = unpriv && test->errstr_unpriv ?
1083 test->errstr_unpriv : test->errstr;
1084 memset(&attr, 0, sizeof(attr));
1085 attr.prog_type = prog_type;
1086 attr.expected_attach_type = test->expected_attach_type;
1088 attr.insns_cnt = prog_len;
1089 attr.license = "GPL";
1092 else if (expected_ret == VERBOSE_ACCEPT)
1096 attr.prog_flags = pflags;
1098 if (prog_type == BPF_PROG_TYPE_TRACING && test->kfunc) {
1099 attr.attach_btf_id = libbpf_find_vmlinux_btf_id(test->kfunc,
1100 attr.expected_attach_type);
1101 if (attr.attach_btf_id < 0) {
1102 printf("FAIL\nFailed to find BTF ID for '%s'!\n",
1109 fd_prog = bpf_load_program_xattr(&attr, bpf_vlog, sizeof(bpf_vlog));
1110 saved_errno = errno;
1112 /* BPF_PROG_TYPE_TRACING requires more setup and
1113 * bpf_probe_prog_type won't give correct answer
1115 if (fd_prog < 0 && prog_type != BPF_PROG_TYPE_TRACING &&
1116 !bpf_probe_prog_type(prog_type, 0)) {
1117 printf("SKIP (unsupported program type %d)\n", prog_type);
1122 alignment_prevented_execution = 0;
1124 if (expected_ret == ACCEPT || expected_ret == VERBOSE_ACCEPT) {
1126 printf("FAIL\nFailed to load prog '%s'!\n",
1127 strerror(saved_errno));
1130 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1132 (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS))
1133 alignment_prevented_execution = 1;
1135 if (expected_ret == VERBOSE_ACCEPT && !cmp_str_seq(bpf_vlog, expected_err)) {
1140 printf("FAIL\nUnexpected success to load!\n");
1143 if (!expected_err || !cmp_str_seq(bpf_vlog, expected_err)) {
1144 printf("FAIL\nUnexpected error message!\n\tEXP: %s\n\tRES: %s\n",
1145 expected_err, bpf_vlog);
1150 if (!unpriv && test->insn_processed) {
1151 uint32_t insn_processed;
1154 proc = strstr(bpf_vlog, "processed ");
1155 insn_processed = atoi(proc + 10);
1156 if (test->insn_processed != insn_processed) {
1157 printf("FAIL\nUnexpected insn_processed %u vs %u\n",
1158 insn_processed, test->insn_processed);
1164 printf(", verifier log:\n%s", bpf_vlog);
1168 if (!alignment_prevented_execution && fd_prog >= 0 && test->runs >= 0) {
1169 uint32_t expected_val;
1175 for (i = 0; i < test->runs; i++) {
1176 if (unpriv && test->retvals[i].retval_unpriv)
1177 expected_val = test->retvals[i].retval_unpriv;
1179 expected_val = test->retvals[i].retval;
1181 err = do_prog_test_run(fd_prog, unpriv, expected_val,
1182 test->retvals[i].data,
1183 sizeof(test->retvals[i].data));
1185 printf("(run %d/%d) ", i + 1, test->runs);
1195 if (run_successes > 1)
1196 printf("%d cases ", run_successes);
1198 if (alignment_prevented_execution)
1199 printf(" (NOTE: not executed due to unknown alignment)");
1206 if (test->fill_insns)
1207 free(test->fill_insns);
1209 for (i = 0; i < MAX_NR_MAPS; i++)
1215 printf("%s", bpf_vlog);
1219 static bool is_admin(void)
1221 cap_flag_value_t net_priv = CAP_CLEAR;
1222 bool perfmon_priv = false;
1223 bool bpf_priv = false;
1227 #ifdef CAP_IS_SUPPORTED
1228 if (!CAP_IS_SUPPORTED(CAP_SETFCAP)) {
1229 perror("cap_get_flag");
1233 caps = cap_get_proc();
1235 perror("cap_get_proc");
1238 cap = (struct libcap *)caps;
1239 bpf_priv = cap->data[1].effective & (1 << (39/* CAP_BPF */ - 32));
1240 perfmon_priv = cap->data[1].effective & (1 << (38/* CAP_PERFMON */ - 32));
1241 if (cap_get_flag(caps, CAP_NET_ADMIN, CAP_EFFECTIVE, &net_priv))
1242 perror("cap_get_flag NET");
1245 return bpf_priv && perfmon_priv && net_priv == CAP_SET;
1248 static void get_unpriv_disabled()
1253 fd = fopen("/proc/sys/"UNPRIV_SYSCTL, "r");
1255 perror("fopen /proc/sys/"UNPRIV_SYSCTL);
1256 unpriv_disabled = true;
1259 if (fgets(buf, 2, fd) == buf && atoi(buf))
1260 unpriv_disabled = true;
1264 static bool test_as_unpriv(struct bpf_test *test)
1266 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1267 /* Some architectures have strict alignment requirements. In
1268 * that case, the BPF verifier detects if a program has
1269 * unaligned accesses and rejects them. A user can pass
1270 * BPF_F_ANY_ALIGNMENT to a program to override this
1271 * check. That, however, will only work when a privileged user
1272 * loads a program. An unprivileged user loading a program
1273 * with this flag will be rejected prior entering the
1276 if (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS)
1279 return !test->prog_type ||
1280 test->prog_type == BPF_PROG_TYPE_SOCKET_FILTER ||
1281 test->prog_type == BPF_PROG_TYPE_CGROUP_SKB;
1284 static int do_test(bool unpriv, unsigned int from, unsigned int to)
1286 int i, passes = 0, errors = 0;
1288 for (i = from; i < to; i++) {
1289 struct bpf_test *test = &tests[i];
1291 /* Program types that are not supported by non-root we
1294 if (test_as_unpriv(test) && unpriv_disabled) {
1295 printf("#%d/u %s SKIP\n", i, test->descr);
1297 } else if (test_as_unpriv(test)) {
1300 printf("#%d/u %s ", i, test->descr);
1301 do_test_single(test, true, &passes, &errors);
1307 printf("#%d/p %s SKIP\n", i, test->descr);
1310 printf("#%d/p %s ", i, test->descr);
1311 do_test_single(test, false, &passes, &errors);
1315 printf("Summary: %d PASSED, %d SKIPPED, %d FAILED\n", passes,
1317 return errors ? EXIT_FAILURE : EXIT_SUCCESS;
1320 int main(int argc, char **argv)
1322 unsigned int from = 0, to = ARRAY_SIZE(tests);
1323 bool unpriv = !is_admin();
1326 if (argc > 1 && strcmp(argv[1], "-v") == 0) {
1333 unsigned int l = atoi(argv[arg]);
1334 unsigned int u = atoi(argv[arg + 1]);
1336 if (l < to && u < to) {
1340 } else if (argc == 2) {
1341 unsigned int t = atoi(argv[arg]);
1349 get_unpriv_disabled();
1350 if (unpriv && unpriv_disabled) {
1351 printf("Cannot run as unprivileged user with sysctl %s.\n",
1353 return EXIT_FAILURE;
1356 bpf_semi_rand_init();
1357 return do_test(unpriv, from, to);