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 <linux/unistd.h> 26 #include <linux/filter.h> 27 #include <linux/bpf_perf_event.h> 28 #include <linux/bpf.h> 29 #include <linux/if_ether.h> 30 #include <linux/btf.h> 31 32 #include <bpf/btf.h> 33 #include <bpf/bpf.h> 34 #include <bpf/libbpf.h> 35 36 #ifdef HAVE_GENHDR 37 # include "autoconf.h" 38 #else 39 # if defined(__i386) || defined(__x86_64) || defined(__s390x__) || defined(__aarch64__) 40 # define CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 1 41 # endif 42 #endif 43 #include "cap_helpers.h" 44 #include "bpf_rand.h" 45 #include "bpf_util.h" 46 #include "test_btf.h" 47 #include "../../../include/linux/filter.h" 48 49 #ifndef ENOTSUPP 50 #define ENOTSUPP 524 51 #endif 52 53 #define MAX_INSNS BPF_MAXINSNS 54 #define MAX_EXPECTED_INSNS 32 55 #define MAX_UNEXPECTED_INSNS 32 56 #define MAX_TEST_INSNS 1000000 57 #define MAX_FIXUPS 8 58 #define MAX_NR_MAPS 23 59 #define MAX_TEST_RUNS 8 60 #define POINTER_VALUE 0xcafe4all 61 #define TEST_DATA_LEN 64 62 #define MAX_FUNC_INFOS 8 63 #define MAX_BTF_STRINGS 256 64 #define MAX_BTF_TYPES 256 65 66 #define INSN_OFF_MASK ((__s16)0xFFFF) 67 #define INSN_IMM_MASK ((__s32)0xFFFFFFFF) 68 #define SKIP_INSNS() BPF_RAW_INSN(0xde, 0xa, 0xd, 0xbeef, 0xdeadbeef) 69 70 #define DEFAULT_LIBBPF_LOG_LEVEL 4 71 #define VERBOSE_LIBBPF_LOG_LEVEL 1 72 73 #define F_NEEDS_EFFICIENT_UNALIGNED_ACCESS (1 << 0) 74 #define F_LOAD_WITH_STRICT_ALIGNMENT (1 << 1) 75 76 /* need CAP_BPF, CAP_NET_ADMIN, CAP_PERFMON to load progs */ 77 #define ADMIN_CAPS (1ULL << CAP_NET_ADMIN | \ 78 1ULL << CAP_PERFMON | \ 79 1ULL << CAP_BPF) 80 #define UNPRIV_SYSCTL "kernel/unprivileged_bpf_disabled" 81 static bool unpriv_disabled = false; 82 static int skips; 83 static bool verbose = false; 84 85 struct kfunc_btf_id_pair { 86 const char *kfunc; 87 int insn_idx; 88 }; 89 90 struct bpf_test { 91 const char *descr; 92 struct bpf_insn insns[MAX_INSNS]; 93 struct bpf_insn *fill_insns; 94 /* If specified, test engine looks for this sequence of 95 * instructions in the BPF program after loading. Allows to 96 * test rewrites applied by verifier. Use values 97 * INSN_OFF_MASK and INSN_IMM_MASK to mask `off` and `imm` 98 * fields if content does not matter. The test case fails if 99 * specified instructions are not found. 100 * 101 * The sequence could be split into sub-sequences by adding 102 * SKIP_INSNS instruction at the end of each sub-sequence. In 103 * such case sub-sequences are searched for one after another. 104 */ 105 struct bpf_insn expected_insns[MAX_EXPECTED_INSNS]; 106 /* If specified, test engine applies same pattern matching 107 * logic as for `expected_insns`. If the specified pattern is 108 * matched test case is marked as failed. 109 */ 110 struct bpf_insn unexpected_insns[MAX_UNEXPECTED_INSNS]; 111 int fixup_map_hash_8b[MAX_FIXUPS]; 112 int fixup_map_hash_48b[MAX_FIXUPS]; 113 int fixup_map_hash_16b[MAX_FIXUPS]; 114 int fixup_map_array_48b[MAX_FIXUPS]; 115 int fixup_map_sockmap[MAX_FIXUPS]; 116 int fixup_map_sockhash[MAX_FIXUPS]; 117 int fixup_map_xskmap[MAX_FIXUPS]; 118 int fixup_map_stacktrace[MAX_FIXUPS]; 119 int fixup_prog1[MAX_FIXUPS]; 120 int fixup_prog2[MAX_FIXUPS]; 121 int fixup_map_in_map[MAX_FIXUPS]; 122 int fixup_cgroup_storage[MAX_FIXUPS]; 123 int fixup_percpu_cgroup_storage[MAX_FIXUPS]; 124 int fixup_map_spin_lock[MAX_FIXUPS]; 125 int fixup_map_array_ro[MAX_FIXUPS]; 126 int fixup_map_array_wo[MAX_FIXUPS]; 127 int fixup_map_array_small[MAX_FIXUPS]; 128 int fixup_sk_storage_map[MAX_FIXUPS]; 129 int fixup_map_event_output[MAX_FIXUPS]; 130 int fixup_map_reuseport_array[MAX_FIXUPS]; 131 int fixup_map_ringbuf[MAX_FIXUPS]; 132 int fixup_map_timer[MAX_FIXUPS]; 133 int fixup_map_kptr[MAX_FIXUPS]; 134 struct kfunc_btf_id_pair fixup_kfunc_btf_id[MAX_FIXUPS]; 135 /* Expected verifier log output for result REJECT or VERBOSE_ACCEPT. 136 * Can be a tab-separated sequence of expected strings. An empty string 137 * means no log verification. 138 */ 139 const char *errstr; 140 const char *errstr_unpriv; 141 uint32_t insn_processed; 142 int prog_len; 143 enum { 144 UNDEF, 145 ACCEPT, 146 REJECT, 147 VERBOSE_ACCEPT, 148 } result, result_unpriv; 149 enum bpf_prog_type prog_type; 150 uint8_t flags; 151 void (*fill_helper)(struct bpf_test *self); 152 int runs; 153 #define bpf_testdata_struct_t \ 154 struct { \ 155 uint32_t retval, retval_unpriv; \ 156 union { \ 157 __u8 data[TEST_DATA_LEN]; \ 158 __u64 data64[TEST_DATA_LEN / 8]; \ 159 }; \ 160 } 161 union { 162 bpf_testdata_struct_t; 163 bpf_testdata_struct_t retvals[MAX_TEST_RUNS]; 164 }; 165 enum bpf_attach_type expected_attach_type; 166 const char *kfunc; 167 struct bpf_func_info func_info[MAX_FUNC_INFOS]; 168 int func_info_cnt; 169 char btf_strings[MAX_BTF_STRINGS]; 170 /* A set of BTF types to load when specified, 171 * use macro definitions from test_btf.h, 172 * must end with BTF_END_RAW 173 */ 174 __u32 btf_types[MAX_BTF_TYPES]; 175 }; 176 177 /* Note we want this to be 64 bit aligned so that the end of our array is 178 * actually the end of the structure. 179 */ 180 #define MAX_ENTRIES 11 181 182 struct test_val { 183 unsigned int index; 184 int foo[MAX_ENTRIES]; 185 }; 186 187 struct other_val { 188 long long foo; 189 long long bar; 190 }; 191 192 static void bpf_fill_ld_abs_vlan_push_pop(struct bpf_test *self) 193 { 194 /* test: {skb->data[0], vlan_push} x 51 + {skb->data[0], vlan_pop} x 51 */ 195 #define PUSH_CNT 51 196 /* jump range is limited to 16 bit. PUSH_CNT of ld_abs needs room */ 197 unsigned int len = (1 << 15) - PUSH_CNT * 2 * 5 * 6; 198 struct bpf_insn *insn = self->fill_insns; 199 int i = 0, j, k = 0; 200 201 insn[i++] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1); 202 loop: 203 for (j = 0; j < PUSH_CNT; j++) { 204 insn[i++] = BPF_LD_ABS(BPF_B, 0); 205 /* jump to error label */ 206 insn[i] = BPF_JMP32_IMM(BPF_JNE, BPF_REG_0, 0x34, len - i - 3); 207 i++; 208 insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6); 209 insn[i++] = BPF_MOV64_IMM(BPF_REG_2, 1); 210 insn[i++] = BPF_MOV64_IMM(BPF_REG_3, 2); 211 insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, 212 BPF_FUNC_skb_vlan_push), 213 insn[i] = BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, len - i - 3); 214 i++; 215 } 216 217 for (j = 0; j < PUSH_CNT; j++) { 218 insn[i++] = BPF_LD_ABS(BPF_B, 0); 219 insn[i] = BPF_JMP32_IMM(BPF_JNE, BPF_REG_0, 0x34, len - i - 3); 220 i++; 221 insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6); 222 insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, 223 BPF_FUNC_skb_vlan_pop), 224 insn[i] = BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, len - i - 3); 225 i++; 226 } 227 if (++k < 5) 228 goto loop; 229 230 for (; i < len - 3; i++) 231 insn[i] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 0xbef); 232 insn[len - 3] = BPF_JMP_A(1); 233 /* error label */ 234 insn[len - 2] = BPF_MOV32_IMM(BPF_REG_0, 0); 235 insn[len - 1] = BPF_EXIT_INSN(); 236 self->prog_len = len; 237 } 238 239 static void bpf_fill_jump_around_ld_abs(struct bpf_test *self) 240 { 241 struct bpf_insn *insn = self->fill_insns; 242 /* jump range is limited to 16 bit. every ld_abs is replaced by 6 insns, 243 * but on arches like arm, ppc etc, there will be one BPF_ZEXT inserted 244 * to extend the error value of the inlined ld_abs sequence which then 245 * contains 7 insns. so, set the dividend to 7 so the testcase could 246 * work on all arches. 247 */ 248 unsigned int len = (1 << 15) / 7; 249 int i = 0; 250 251 insn[i++] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1); 252 insn[i++] = BPF_LD_ABS(BPF_B, 0); 253 insn[i] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 10, len - i - 2); 254 i++; 255 while (i < len - 1) 256 insn[i++] = BPF_LD_ABS(BPF_B, 1); 257 insn[i] = BPF_EXIT_INSN(); 258 self->prog_len = i + 1; 259 } 260 261 static void bpf_fill_rand_ld_dw(struct bpf_test *self) 262 { 263 struct bpf_insn *insn = self->fill_insns; 264 uint64_t res = 0; 265 int i = 0; 266 267 insn[i++] = BPF_MOV32_IMM(BPF_REG_0, 0); 268 while (i < self->retval) { 269 uint64_t val = bpf_semi_rand_get(); 270 struct bpf_insn tmp[2] = { BPF_LD_IMM64(BPF_REG_1, val) }; 271 272 res ^= val; 273 insn[i++] = tmp[0]; 274 insn[i++] = tmp[1]; 275 insn[i++] = BPF_ALU64_REG(BPF_XOR, BPF_REG_0, BPF_REG_1); 276 } 277 insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_0); 278 insn[i++] = BPF_ALU64_IMM(BPF_RSH, BPF_REG_1, 32); 279 insn[i++] = BPF_ALU64_REG(BPF_XOR, BPF_REG_0, BPF_REG_1); 280 insn[i] = BPF_EXIT_INSN(); 281 self->prog_len = i + 1; 282 res ^= (res >> 32); 283 self->retval = (uint32_t)res; 284 } 285 286 #define MAX_JMP_SEQ 8192 287 288 /* test the sequence of 8k jumps */ 289 static void bpf_fill_scale1(struct bpf_test *self) 290 { 291 struct bpf_insn *insn = self->fill_insns; 292 int i = 0, k = 0; 293 294 insn[i++] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1); 295 /* test to check that the long sequence of jumps is acceptable */ 296 while (k++ < MAX_JMP_SEQ) { 297 insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, 298 BPF_FUNC_get_prandom_u32); 299 insn[i++] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, bpf_semi_rand_get(), 2); 300 insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_10); 301 insn[i++] = BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, 302 -8 * (k % 64 + 1)); 303 } 304 /* is_state_visited() doesn't allocate state for pruning for every jump. 305 * Hence multiply jmps by 4 to accommodate that heuristic 306 */ 307 while (i < MAX_TEST_INSNS - MAX_JMP_SEQ * 4) 308 insn[i++] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 42); 309 insn[i] = BPF_EXIT_INSN(); 310 self->prog_len = i + 1; 311 self->retval = 42; 312 } 313 314 /* test the sequence of 8k jumps in inner most function (function depth 8)*/ 315 static void bpf_fill_scale2(struct bpf_test *self) 316 { 317 struct bpf_insn *insn = self->fill_insns; 318 int i = 0, k = 0; 319 320 #define FUNC_NEST 7 321 for (k = 0; k < FUNC_NEST; k++) { 322 insn[i++] = BPF_CALL_REL(1); 323 insn[i++] = BPF_EXIT_INSN(); 324 } 325 insn[i++] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1); 326 /* test to check that the long sequence of jumps is acceptable */ 327 k = 0; 328 while (k++ < MAX_JMP_SEQ) { 329 insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, 330 BPF_FUNC_get_prandom_u32); 331 insn[i++] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, bpf_semi_rand_get(), 2); 332 insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_10); 333 insn[i++] = BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, 334 -8 * (k % (64 - 4 * FUNC_NEST) + 1)); 335 } 336 while (i < MAX_TEST_INSNS - MAX_JMP_SEQ * 4) 337 insn[i++] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 42); 338 insn[i] = BPF_EXIT_INSN(); 339 self->prog_len = i + 1; 340 self->retval = 42; 341 } 342 343 static void bpf_fill_scale(struct bpf_test *self) 344 { 345 switch (self->retval) { 346 case 1: 347 return bpf_fill_scale1(self); 348 case 2: 349 return bpf_fill_scale2(self); 350 default: 351 self->prog_len = 0; 352 break; 353 } 354 } 355 356 static int bpf_fill_torturous_jumps_insn_1(struct bpf_insn *insn) 357 { 358 unsigned int len = 259, hlen = 128; 359 int i; 360 361 insn[0] = BPF_EMIT_CALL(BPF_FUNC_get_prandom_u32); 362 for (i = 1; i <= hlen; i++) { 363 insn[i] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, i, hlen); 364 insn[i + hlen] = BPF_JMP_A(hlen - i); 365 } 366 insn[len - 2] = BPF_MOV64_IMM(BPF_REG_0, 1); 367 insn[len - 1] = BPF_EXIT_INSN(); 368 369 return len; 370 } 371 372 static int bpf_fill_torturous_jumps_insn_2(struct bpf_insn *insn) 373 { 374 unsigned int len = 4100, jmp_off = 2048; 375 int i, j; 376 377 insn[0] = BPF_EMIT_CALL(BPF_FUNC_get_prandom_u32); 378 for (i = 1; i <= jmp_off; i++) { 379 insn[i] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, i, jmp_off); 380 } 381 insn[i++] = BPF_JMP_A(jmp_off); 382 for (; i <= jmp_off * 2 + 1; i+=16) { 383 for (j = 0; j < 16; j++) { 384 insn[i + j] = BPF_JMP_A(16 - j - 1); 385 } 386 } 387 388 insn[len - 2] = BPF_MOV64_IMM(BPF_REG_0, 2); 389 insn[len - 1] = BPF_EXIT_INSN(); 390 391 return len; 392 } 393 394 static void bpf_fill_torturous_jumps(struct bpf_test *self) 395 { 396 struct bpf_insn *insn = self->fill_insns; 397 int i = 0; 398 399 switch (self->retval) { 400 case 1: 401 self->prog_len = bpf_fill_torturous_jumps_insn_1(insn); 402 return; 403 case 2: 404 self->prog_len = bpf_fill_torturous_jumps_insn_2(insn); 405 return; 406 case 3: 407 /* main */ 408 insn[i++] = BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 4); 409 insn[i++] = BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 262); 410 insn[i++] = BPF_ST_MEM(BPF_B, BPF_REG_10, -32, 0); 411 insn[i++] = BPF_MOV64_IMM(BPF_REG_0, 3); 412 insn[i++] = BPF_EXIT_INSN(); 413 414 /* subprog 1 */ 415 i += bpf_fill_torturous_jumps_insn_1(insn + i); 416 417 /* subprog 2 */ 418 i += bpf_fill_torturous_jumps_insn_2(insn + i); 419 420 self->prog_len = i; 421 return; 422 default: 423 self->prog_len = 0; 424 break; 425 } 426 } 427 428 /* BPF_SK_LOOKUP contains 13 instructions, if you need to fix up maps */ 429 #define BPF_SK_LOOKUP(func) \ 430 /* struct bpf_sock_tuple tuple = {} */ \ 431 BPF_MOV64_IMM(BPF_REG_2, 0), \ 432 BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_2, -8), \ 433 BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -16), \ 434 BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -24), \ 435 BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -32), \ 436 BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -40), \ 437 BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -48), \ 438 /* sk = func(ctx, &tuple, sizeof tuple, 0, 0) */ \ 439 BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), \ 440 BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -48), \ 441 BPF_MOV64_IMM(BPF_REG_3, sizeof(struct bpf_sock_tuple)), \ 442 BPF_MOV64_IMM(BPF_REG_4, 0), \ 443 BPF_MOV64_IMM(BPF_REG_5, 0), \ 444 BPF_EMIT_CALL(BPF_FUNC_ ## func) 445 446 /* BPF_DIRECT_PKT_R2 contains 7 instructions, it initializes default return 447 * value into 0 and does necessary preparation for direct packet access 448 * through r2. The allowed access range is 8 bytes. 449 */ 450 #define BPF_DIRECT_PKT_R2 \ 451 BPF_MOV64_IMM(BPF_REG_0, 0), \ 452 BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, \ 453 offsetof(struct __sk_buff, data)), \ 454 BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1, \ 455 offsetof(struct __sk_buff, data_end)), \ 456 BPF_MOV64_REG(BPF_REG_4, BPF_REG_2), \ 457 BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 8), \ 458 BPF_JMP_REG(BPF_JLE, BPF_REG_4, BPF_REG_3, 1), \ 459 BPF_EXIT_INSN() 460 461 /* BPF_RAND_UEXT_R7 contains 4 instructions, it initializes R7 into a random 462 * positive u32, and zero-extend it into 64-bit. 463 */ 464 #define BPF_RAND_UEXT_R7 \ 465 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, \ 466 BPF_FUNC_get_prandom_u32), \ 467 BPF_MOV64_REG(BPF_REG_7, BPF_REG_0), \ 468 BPF_ALU64_IMM(BPF_LSH, BPF_REG_7, 33), \ 469 BPF_ALU64_IMM(BPF_RSH, BPF_REG_7, 33) 470 471 /* BPF_RAND_SEXT_R7 contains 5 instructions, it initializes R7 into a random 472 * negative u32, and sign-extend it into 64-bit. 473 */ 474 #define BPF_RAND_SEXT_R7 \ 475 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, \ 476 BPF_FUNC_get_prandom_u32), \ 477 BPF_MOV64_REG(BPF_REG_7, BPF_REG_0), \ 478 BPF_ALU64_IMM(BPF_OR, BPF_REG_7, 0x80000000), \ 479 BPF_ALU64_IMM(BPF_LSH, BPF_REG_7, 32), \ 480 BPF_ALU64_IMM(BPF_ARSH, BPF_REG_7, 32) 481 482 static struct bpf_test tests[] = { 483 #define FILL_ARRAY 484 #include <verifier/tests.h> 485 #undef FILL_ARRAY 486 }; 487 488 static int probe_filter_length(const struct bpf_insn *fp) 489 { 490 int len; 491 492 for (len = MAX_INSNS - 1; len > 0; --len) 493 if (fp[len].code != 0 || fp[len].imm != 0) 494 break; 495 return len + 1; 496 } 497 498 static bool skip_unsupported_map(enum bpf_map_type map_type) 499 { 500 if (!libbpf_probe_bpf_map_type(map_type, NULL)) { 501 printf("SKIP (unsupported map type %d)\n", map_type); 502 skips++; 503 return true; 504 } 505 return false; 506 } 507 508 static int __create_map(uint32_t type, uint32_t size_key, 509 uint32_t size_value, uint32_t max_elem, 510 uint32_t extra_flags) 511 { 512 LIBBPF_OPTS(bpf_map_create_opts, opts); 513 int fd; 514 515 opts.map_flags = (type == BPF_MAP_TYPE_HASH ? BPF_F_NO_PREALLOC : 0) | extra_flags; 516 fd = bpf_map_create(type, NULL, size_key, size_value, max_elem, &opts); 517 if (fd < 0) { 518 if (skip_unsupported_map(type)) 519 return -1; 520 printf("Failed to create hash map '%s'!\n", strerror(errno)); 521 } 522 523 return fd; 524 } 525 526 static int create_map(uint32_t type, uint32_t size_key, 527 uint32_t size_value, uint32_t max_elem) 528 { 529 return __create_map(type, size_key, size_value, max_elem, 0); 530 } 531 532 static void update_map(int fd, int index) 533 { 534 struct test_val value = { 535 .index = (6 + 1) * sizeof(int), 536 .foo[6] = 0xabcdef12, 537 }; 538 539 assert(!bpf_map_update_elem(fd, &index, &value, 0)); 540 } 541 542 static int create_prog_dummy_simple(enum bpf_prog_type prog_type, int ret) 543 { 544 struct bpf_insn prog[] = { 545 BPF_MOV64_IMM(BPF_REG_0, ret), 546 BPF_EXIT_INSN(), 547 }; 548 549 return bpf_prog_load(prog_type, NULL, "GPL", prog, ARRAY_SIZE(prog), NULL); 550 } 551 552 static int create_prog_dummy_loop(enum bpf_prog_type prog_type, int mfd, 553 int idx, int ret) 554 { 555 struct bpf_insn prog[] = { 556 BPF_MOV64_IMM(BPF_REG_3, idx), 557 BPF_LD_MAP_FD(BPF_REG_2, mfd), 558 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, 559 BPF_FUNC_tail_call), 560 BPF_MOV64_IMM(BPF_REG_0, ret), 561 BPF_EXIT_INSN(), 562 }; 563 564 return bpf_prog_load(prog_type, NULL, "GPL", prog, ARRAY_SIZE(prog), NULL); 565 } 566 567 static int create_prog_array(enum bpf_prog_type prog_type, uint32_t max_elem, 568 int p1key, int p2key, int p3key) 569 { 570 int mfd, p1fd, p2fd, p3fd; 571 572 mfd = bpf_map_create(BPF_MAP_TYPE_PROG_ARRAY, NULL, sizeof(int), 573 sizeof(int), max_elem, NULL); 574 if (mfd < 0) { 575 if (skip_unsupported_map(BPF_MAP_TYPE_PROG_ARRAY)) 576 return -1; 577 printf("Failed to create prog array '%s'!\n", strerror(errno)); 578 return -1; 579 } 580 581 p1fd = create_prog_dummy_simple(prog_type, 42); 582 p2fd = create_prog_dummy_loop(prog_type, mfd, p2key, 41); 583 p3fd = create_prog_dummy_simple(prog_type, 24); 584 if (p1fd < 0 || p2fd < 0 || p3fd < 0) 585 goto err; 586 if (bpf_map_update_elem(mfd, &p1key, &p1fd, BPF_ANY) < 0) 587 goto err; 588 if (bpf_map_update_elem(mfd, &p2key, &p2fd, BPF_ANY) < 0) 589 goto err; 590 if (bpf_map_update_elem(mfd, &p3key, &p3fd, BPF_ANY) < 0) { 591 err: 592 close(mfd); 593 mfd = -1; 594 } 595 close(p3fd); 596 close(p2fd); 597 close(p1fd); 598 return mfd; 599 } 600 601 static int create_map_in_map(void) 602 { 603 LIBBPF_OPTS(bpf_map_create_opts, opts); 604 int inner_map_fd, outer_map_fd; 605 606 inner_map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(int), 607 sizeof(int), 1, NULL); 608 if (inner_map_fd < 0) { 609 if (skip_unsupported_map(BPF_MAP_TYPE_ARRAY)) 610 return -1; 611 printf("Failed to create array '%s'!\n", strerror(errno)); 612 return inner_map_fd; 613 } 614 615 opts.inner_map_fd = inner_map_fd; 616 outer_map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY_OF_MAPS, NULL, 617 sizeof(int), sizeof(int), 1, &opts); 618 if (outer_map_fd < 0) { 619 if (skip_unsupported_map(BPF_MAP_TYPE_ARRAY_OF_MAPS)) 620 return -1; 621 printf("Failed to create array of maps '%s'!\n", 622 strerror(errno)); 623 } 624 625 close(inner_map_fd); 626 627 return outer_map_fd; 628 } 629 630 static int create_cgroup_storage(bool percpu) 631 { 632 enum bpf_map_type type = percpu ? BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE : 633 BPF_MAP_TYPE_CGROUP_STORAGE; 634 int fd; 635 636 fd = bpf_map_create(type, NULL, sizeof(struct bpf_cgroup_storage_key), 637 TEST_DATA_LEN, 0, NULL); 638 if (fd < 0) { 639 if (skip_unsupported_map(type)) 640 return -1; 641 printf("Failed to create cgroup storage '%s'!\n", 642 strerror(errno)); 643 } 644 645 return fd; 646 } 647 648 /* struct bpf_spin_lock { 649 * int val; 650 * }; 651 * struct val { 652 * int cnt; 653 * struct bpf_spin_lock l; 654 * }; 655 * struct bpf_timer { 656 * __u64 :64; 657 * __u64 :64; 658 * } __attribute__((aligned(8))); 659 * struct timer { 660 * struct bpf_timer t; 661 * }; 662 * struct btf_ptr { 663 * struct prog_test_ref_kfunc __kptr *ptr; 664 * struct prog_test_ref_kfunc __kptr_ref *ptr; 665 * struct prog_test_member __kptr_ref *ptr; 666 * } 667 */ 668 static const char btf_str_sec[] = "\0bpf_spin_lock\0val\0cnt\0l\0bpf_timer\0timer\0t" 669 "\0btf_ptr\0prog_test_ref_kfunc\0ptr\0kptr\0kptr_ref" 670 "\0prog_test_member"; 671 static __u32 btf_raw_types[] = { 672 /* int */ 673 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ 674 /* struct bpf_spin_lock */ /* [2] */ 675 BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4), 676 BTF_MEMBER_ENC(15, 1, 0), /* int val; */ 677 /* struct val */ /* [3] */ 678 BTF_TYPE_ENC(15, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8), 679 BTF_MEMBER_ENC(19, 1, 0), /* int cnt; */ 680 BTF_MEMBER_ENC(23, 2, 32),/* struct bpf_spin_lock l; */ 681 /* struct bpf_timer */ /* [4] */ 682 BTF_TYPE_ENC(25, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 0), 16), 683 /* struct timer */ /* [5] */ 684 BTF_TYPE_ENC(35, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 16), 685 BTF_MEMBER_ENC(41, 4, 0), /* struct bpf_timer t; */ 686 /* struct prog_test_ref_kfunc */ /* [6] */ 687 BTF_STRUCT_ENC(51, 0, 0), 688 BTF_STRUCT_ENC(89, 0, 0), /* [7] */ 689 /* type tag "kptr" */ 690 BTF_TYPE_TAG_ENC(75, 6), /* [8] */ 691 /* type tag "kptr_ref" */ 692 BTF_TYPE_TAG_ENC(80, 6), /* [9] */ 693 BTF_TYPE_TAG_ENC(80, 7), /* [10] */ 694 BTF_PTR_ENC(8), /* [11] */ 695 BTF_PTR_ENC(9), /* [12] */ 696 BTF_PTR_ENC(10), /* [13] */ 697 /* struct btf_ptr */ /* [14] */ 698 BTF_STRUCT_ENC(43, 3, 24), 699 BTF_MEMBER_ENC(71, 11, 0), /* struct prog_test_ref_kfunc __kptr *ptr; */ 700 BTF_MEMBER_ENC(71, 12, 64), /* struct prog_test_ref_kfunc __kptr_ref *ptr; */ 701 BTF_MEMBER_ENC(71, 13, 128), /* struct prog_test_member __kptr_ref *ptr; */ 702 }; 703 704 static char bpf_vlog[UINT_MAX >> 8]; 705 706 static int load_btf_spec(__u32 *types, int types_len, 707 const char *strings, int strings_len) 708 { 709 struct btf_header hdr = { 710 .magic = BTF_MAGIC, 711 .version = BTF_VERSION, 712 .hdr_len = sizeof(struct btf_header), 713 .type_len = types_len, 714 .str_off = types_len, 715 .str_len = strings_len, 716 }; 717 void *ptr, *raw_btf; 718 int btf_fd; 719 LIBBPF_OPTS(bpf_btf_load_opts, opts, 720 .log_buf = bpf_vlog, 721 .log_size = sizeof(bpf_vlog), 722 .log_level = (verbose 723 ? VERBOSE_LIBBPF_LOG_LEVEL 724 : DEFAULT_LIBBPF_LOG_LEVEL), 725 ); 726 727 raw_btf = malloc(sizeof(hdr) + types_len + strings_len); 728 729 ptr = raw_btf; 730 memcpy(ptr, &hdr, sizeof(hdr)); 731 ptr += sizeof(hdr); 732 memcpy(ptr, types, hdr.type_len); 733 ptr += hdr.type_len; 734 memcpy(ptr, strings, hdr.str_len); 735 ptr += hdr.str_len; 736 737 btf_fd = bpf_btf_load(raw_btf, ptr - raw_btf, &opts); 738 if (btf_fd < 0) 739 printf("Failed to load BTF spec: '%s'\n", strerror(errno)); 740 741 free(raw_btf); 742 743 return btf_fd < 0 ? -1 : btf_fd; 744 } 745 746 static int load_btf(void) 747 { 748 return load_btf_spec(btf_raw_types, sizeof(btf_raw_types), 749 btf_str_sec, sizeof(btf_str_sec)); 750 } 751 752 static int load_btf_for_test(struct bpf_test *test) 753 { 754 int types_num = 0; 755 756 while (types_num < MAX_BTF_TYPES && 757 test->btf_types[types_num] != BTF_END_RAW) 758 ++types_num; 759 760 int types_len = types_num * sizeof(test->btf_types[0]); 761 762 return load_btf_spec(test->btf_types, types_len, 763 test->btf_strings, sizeof(test->btf_strings)); 764 } 765 766 static int create_map_spin_lock(void) 767 { 768 LIBBPF_OPTS(bpf_map_create_opts, opts, 769 .btf_key_type_id = 1, 770 .btf_value_type_id = 3, 771 ); 772 int fd, btf_fd; 773 774 btf_fd = load_btf(); 775 if (btf_fd < 0) 776 return -1; 777 opts.btf_fd = btf_fd; 778 fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "test_map", 4, 8, 1, &opts); 779 if (fd < 0) 780 printf("Failed to create map with spin_lock\n"); 781 return fd; 782 } 783 784 static int create_sk_storage_map(void) 785 { 786 LIBBPF_OPTS(bpf_map_create_opts, opts, 787 .map_flags = BPF_F_NO_PREALLOC, 788 .btf_key_type_id = 1, 789 .btf_value_type_id = 3, 790 ); 791 int fd, btf_fd; 792 793 btf_fd = load_btf(); 794 if (btf_fd < 0) 795 return -1; 796 opts.btf_fd = btf_fd; 797 fd = bpf_map_create(BPF_MAP_TYPE_SK_STORAGE, "test_map", 4, 8, 0, &opts); 798 close(opts.btf_fd); 799 if (fd < 0) 800 printf("Failed to create sk_storage_map\n"); 801 return fd; 802 } 803 804 static int create_map_timer(void) 805 { 806 LIBBPF_OPTS(bpf_map_create_opts, opts, 807 .btf_key_type_id = 1, 808 .btf_value_type_id = 5, 809 ); 810 int fd, btf_fd; 811 812 btf_fd = load_btf(); 813 if (btf_fd < 0) 814 return -1; 815 816 opts.btf_fd = btf_fd; 817 fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "test_map", 4, 16, 1, &opts); 818 if (fd < 0) 819 printf("Failed to create map with timer\n"); 820 return fd; 821 } 822 823 static int create_map_kptr(void) 824 { 825 LIBBPF_OPTS(bpf_map_create_opts, opts, 826 .btf_key_type_id = 1, 827 .btf_value_type_id = 14, 828 ); 829 int fd, btf_fd; 830 831 btf_fd = load_btf(); 832 if (btf_fd < 0) 833 return -1; 834 835 opts.btf_fd = btf_fd; 836 fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "test_map", 4, 24, 1, &opts); 837 if (fd < 0) 838 printf("Failed to create map with btf_id pointer\n"); 839 return fd; 840 } 841 842 static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type, 843 struct bpf_insn *prog, int *map_fds) 844 { 845 int *fixup_map_hash_8b = test->fixup_map_hash_8b; 846 int *fixup_map_hash_48b = test->fixup_map_hash_48b; 847 int *fixup_map_hash_16b = test->fixup_map_hash_16b; 848 int *fixup_map_array_48b = test->fixup_map_array_48b; 849 int *fixup_map_sockmap = test->fixup_map_sockmap; 850 int *fixup_map_sockhash = test->fixup_map_sockhash; 851 int *fixup_map_xskmap = test->fixup_map_xskmap; 852 int *fixup_map_stacktrace = test->fixup_map_stacktrace; 853 int *fixup_prog1 = test->fixup_prog1; 854 int *fixup_prog2 = test->fixup_prog2; 855 int *fixup_map_in_map = test->fixup_map_in_map; 856 int *fixup_cgroup_storage = test->fixup_cgroup_storage; 857 int *fixup_percpu_cgroup_storage = test->fixup_percpu_cgroup_storage; 858 int *fixup_map_spin_lock = test->fixup_map_spin_lock; 859 int *fixup_map_array_ro = test->fixup_map_array_ro; 860 int *fixup_map_array_wo = test->fixup_map_array_wo; 861 int *fixup_map_array_small = test->fixup_map_array_small; 862 int *fixup_sk_storage_map = test->fixup_sk_storage_map; 863 int *fixup_map_event_output = test->fixup_map_event_output; 864 int *fixup_map_reuseport_array = test->fixup_map_reuseport_array; 865 int *fixup_map_ringbuf = test->fixup_map_ringbuf; 866 int *fixup_map_timer = test->fixup_map_timer; 867 int *fixup_map_kptr = test->fixup_map_kptr; 868 struct kfunc_btf_id_pair *fixup_kfunc_btf_id = test->fixup_kfunc_btf_id; 869 870 if (test->fill_helper) { 871 test->fill_insns = calloc(MAX_TEST_INSNS, sizeof(struct bpf_insn)); 872 test->fill_helper(test); 873 } 874 875 /* Allocating HTs with 1 elem is fine here, since we only test 876 * for verifier and not do a runtime lookup, so the only thing 877 * that really matters is value size in this case. 878 */ 879 if (*fixup_map_hash_8b) { 880 map_fds[0] = create_map(BPF_MAP_TYPE_HASH, sizeof(long long), 881 sizeof(long long), 1); 882 do { 883 prog[*fixup_map_hash_8b].imm = map_fds[0]; 884 fixup_map_hash_8b++; 885 } while (*fixup_map_hash_8b); 886 } 887 888 if (*fixup_map_hash_48b) { 889 map_fds[1] = create_map(BPF_MAP_TYPE_HASH, sizeof(long long), 890 sizeof(struct test_val), 1); 891 do { 892 prog[*fixup_map_hash_48b].imm = map_fds[1]; 893 fixup_map_hash_48b++; 894 } while (*fixup_map_hash_48b); 895 } 896 897 if (*fixup_map_hash_16b) { 898 map_fds[2] = create_map(BPF_MAP_TYPE_HASH, sizeof(long long), 899 sizeof(struct other_val), 1); 900 do { 901 prog[*fixup_map_hash_16b].imm = map_fds[2]; 902 fixup_map_hash_16b++; 903 } while (*fixup_map_hash_16b); 904 } 905 906 if (*fixup_map_array_48b) { 907 map_fds[3] = create_map(BPF_MAP_TYPE_ARRAY, sizeof(int), 908 sizeof(struct test_val), 1); 909 update_map(map_fds[3], 0); 910 do { 911 prog[*fixup_map_array_48b].imm = map_fds[3]; 912 fixup_map_array_48b++; 913 } while (*fixup_map_array_48b); 914 } 915 916 if (*fixup_prog1) { 917 map_fds[4] = create_prog_array(prog_type, 4, 0, 1, 2); 918 do { 919 prog[*fixup_prog1].imm = map_fds[4]; 920 fixup_prog1++; 921 } while (*fixup_prog1); 922 } 923 924 if (*fixup_prog2) { 925 map_fds[5] = create_prog_array(prog_type, 8, 7, 1, 2); 926 do { 927 prog[*fixup_prog2].imm = map_fds[5]; 928 fixup_prog2++; 929 } while (*fixup_prog2); 930 } 931 932 if (*fixup_map_in_map) { 933 map_fds[6] = create_map_in_map(); 934 do { 935 prog[*fixup_map_in_map].imm = map_fds[6]; 936 fixup_map_in_map++; 937 } while (*fixup_map_in_map); 938 } 939 940 if (*fixup_cgroup_storage) { 941 map_fds[7] = create_cgroup_storage(false); 942 do { 943 prog[*fixup_cgroup_storage].imm = map_fds[7]; 944 fixup_cgroup_storage++; 945 } while (*fixup_cgroup_storage); 946 } 947 948 if (*fixup_percpu_cgroup_storage) { 949 map_fds[8] = create_cgroup_storage(true); 950 do { 951 prog[*fixup_percpu_cgroup_storage].imm = map_fds[8]; 952 fixup_percpu_cgroup_storage++; 953 } while (*fixup_percpu_cgroup_storage); 954 } 955 if (*fixup_map_sockmap) { 956 map_fds[9] = create_map(BPF_MAP_TYPE_SOCKMAP, sizeof(int), 957 sizeof(int), 1); 958 do { 959 prog[*fixup_map_sockmap].imm = map_fds[9]; 960 fixup_map_sockmap++; 961 } while (*fixup_map_sockmap); 962 } 963 if (*fixup_map_sockhash) { 964 map_fds[10] = create_map(BPF_MAP_TYPE_SOCKHASH, sizeof(int), 965 sizeof(int), 1); 966 do { 967 prog[*fixup_map_sockhash].imm = map_fds[10]; 968 fixup_map_sockhash++; 969 } while (*fixup_map_sockhash); 970 } 971 if (*fixup_map_xskmap) { 972 map_fds[11] = create_map(BPF_MAP_TYPE_XSKMAP, sizeof(int), 973 sizeof(int), 1); 974 do { 975 prog[*fixup_map_xskmap].imm = map_fds[11]; 976 fixup_map_xskmap++; 977 } while (*fixup_map_xskmap); 978 } 979 if (*fixup_map_stacktrace) { 980 map_fds[12] = create_map(BPF_MAP_TYPE_STACK_TRACE, sizeof(u32), 981 sizeof(u64), 1); 982 do { 983 prog[*fixup_map_stacktrace].imm = map_fds[12]; 984 fixup_map_stacktrace++; 985 } while (*fixup_map_stacktrace); 986 } 987 if (*fixup_map_spin_lock) { 988 map_fds[13] = create_map_spin_lock(); 989 do { 990 prog[*fixup_map_spin_lock].imm = map_fds[13]; 991 fixup_map_spin_lock++; 992 } while (*fixup_map_spin_lock); 993 } 994 if (*fixup_map_array_ro) { 995 map_fds[14] = __create_map(BPF_MAP_TYPE_ARRAY, sizeof(int), 996 sizeof(struct test_val), 1, 997 BPF_F_RDONLY_PROG); 998 update_map(map_fds[14], 0); 999 do { 1000 prog[*fixup_map_array_ro].imm = map_fds[14]; 1001 fixup_map_array_ro++; 1002 } while (*fixup_map_array_ro); 1003 } 1004 if (*fixup_map_array_wo) { 1005 map_fds[15] = __create_map(BPF_MAP_TYPE_ARRAY, sizeof(int), 1006 sizeof(struct test_val), 1, 1007 BPF_F_WRONLY_PROG); 1008 update_map(map_fds[15], 0); 1009 do { 1010 prog[*fixup_map_array_wo].imm = map_fds[15]; 1011 fixup_map_array_wo++; 1012 } while (*fixup_map_array_wo); 1013 } 1014 if (*fixup_map_array_small) { 1015 map_fds[16] = __create_map(BPF_MAP_TYPE_ARRAY, sizeof(int), 1016 1, 1, 0); 1017 update_map(map_fds[16], 0); 1018 do { 1019 prog[*fixup_map_array_small].imm = map_fds[16]; 1020 fixup_map_array_small++; 1021 } while (*fixup_map_array_small); 1022 } 1023 if (*fixup_sk_storage_map) { 1024 map_fds[17] = create_sk_storage_map(); 1025 do { 1026 prog[*fixup_sk_storage_map].imm = map_fds[17]; 1027 fixup_sk_storage_map++; 1028 } while (*fixup_sk_storage_map); 1029 } 1030 if (*fixup_map_event_output) { 1031 map_fds[18] = __create_map(BPF_MAP_TYPE_PERF_EVENT_ARRAY, 1032 sizeof(int), sizeof(int), 1, 0); 1033 do { 1034 prog[*fixup_map_event_output].imm = map_fds[18]; 1035 fixup_map_event_output++; 1036 } while (*fixup_map_event_output); 1037 } 1038 if (*fixup_map_reuseport_array) { 1039 map_fds[19] = __create_map(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, 1040 sizeof(u32), sizeof(u64), 1, 0); 1041 do { 1042 prog[*fixup_map_reuseport_array].imm = map_fds[19]; 1043 fixup_map_reuseport_array++; 1044 } while (*fixup_map_reuseport_array); 1045 } 1046 if (*fixup_map_ringbuf) { 1047 map_fds[20] = create_map(BPF_MAP_TYPE_RINGBUF, 0, 1048 0, 4096); 1049 do { 1050 prog[*fixup_map_ringbuf].imm = map_fds[20]; 1051 fixup_map_ringbuf++; 1052 } while (*fixup_map_ringbuf); 1053 } 1054 if (*fixup_map_timer) { 1055 map_fds[21] = create_map_timer(); 1056 do { 1057 prog[*fixup_map_timer].imm = map_fds[21]; 1058 fixup_map_timer++; 1059 } while (*fixup_map_timer); 1060 } 1061 if (*fixup_map_kptr) { 1062 map_fds[22] = create_map_kptr(); 1063 do { 1064 prog[*fixup_map_kptr].imm = map_fds[22]; 1065 fixup_map_kptr++; 1066 } while (*fixup_map_kptr); 1067 } 1068 1069 /* Patch in kfunc BTF IDs */ 1070 if (fixup_kfunc_btf_id->kfunc) { 1071 struct btf *btf; 1072 int btf_id; 1073 1074 do { 1075 btf_id = 0; 1076 btf = btf__load_vmlinux_btf(); 1077 if (btf) { 1078 btf_id = btf__find_by_name_kind(btf, 1079 fixup_kfunc_btf_id->kfunc, 1080 BTF_KIND_FUNC); 1081 btf_id = btf_id < 0 ? 0 : btf_id; 1082 } 1083 btf__free(btf); 1084 prog[fixup_kfunc_btf_id->insn_idx].imm = btf_id; 1085 fixup_kfunc_btf_id++; 1086 } while (fixup_kfunc_btf_id->kfunc); 1087 } 1088 } 1089 1090 struct libcap { 1091 struct __user_cap_header_struct hdr; 1092 struct __user_cap_data_struct data[2]; 1093 }; 1094 1095 static int set_admin(bool admin) 1096 { 1097 int err; 1098 1099 if (admin) { 1100 err = cap_enable_effective(ADMIN_CAPS, NULL); 1101 if (err) 1102 perror("cap_enable_effective(ADMIN_CAPS)"); 1103 } else { 1104 err = cap_disable_effective(ADMIN_CAPS, NULL); 1105 if (err) 1106 perror("cap_disable_effective(ADMIN_CAPS)"); 1107 } 1108 1109 return err; 1110 } 1111 1112 static int do_prog_test_run(int fd_prog, bool unpriv, uint32_t expected_val, 1113 void *data, size_t size_data) 1114 { 1115 __u8 tmp[TEST_DATA_LEN << 2]; 1116 __u32 size_tmp = sizeof(tmp); 1117 int err, saved_errno; 1118 LIBBPF_OPTS(bpf_test_run_opts, topts, 1119 .data_in = data, 1120 .data_size_in = size_data, 1121 .data_out = tmp, 1122 .data_size_out = size_tmp, 1123 .repeat = 1, 1124 ); 1125 1126 if (unpriv) 1127 set_admin(true); 1128 err = bpf_prog_test_run_opts(fd_prog, &topts); 1129 saved_errno = errno; 1130 1131 if (unpriv) 1132 set_admin(false); 1133 1134 if (err) { 1135 switch (saved_errno) { 1136 case ENOTSUPP: 1137 printf("Did not run the program (not supported) "); 1138 return 0; 1139 case EPERM: 1140 if (unpriv) { 1141 printf("Did not run the program (no permission) "); 1142 return 0; 1143 } 1144 /* fallthrough; */ 1145 default: 1146 printf("FAIL: Unexpected bpf_prog_test_run error (%s) ", 1147 strerror(saved_errno)); 1148 return err; 1149 } 1150 } 1151 1152 if (topts.retval != expected_val && expected_val != POINTER_VALUE) { 1153 printf("FAIL retval %d != %d ", topts.retval, expected_val); 1154 return 1; 1155 } 1156 1157 return 0; 1158 } 1159 1160 /* Returns true if every part of exp (tab-separated) appears in log, in order. 1161 * 1162 * If exp is an empty string, returns true. 1163 */ 1164 static bool cmp_str_seq(const char *log, const char *exp) 1165 { 1166 char needle[200]; 1167 const char *p, *q; 1168 int len; 1169 1170 do { 1171 if (!strlen(exp)) 1172 break; 1173 p = strchr(exp, '\t'); 1174 if (!p) 1175 p = exp + strlen(exp); 1176 1177 len = p - exp; 1178 if (len >= sizeof(needle) || !len) { 1179 printf("FAIL\nTestcase bug\n"); 1180 return false; 1181 } 1182 strncpy(needle, exp, len); 1183 needle[len] = 0; 1184 q = strstr(log, needle); 1185 if (!q) { 1186 printf("FAIL\nUnexpected verifier log!\n" 1187 "EXP: %s\nRES:\n", needle); 1188 return false; 1189 } 1190 log = q + len; 1191 exp = p + 1; 1192 } while (*p); 1193 return true; 1194 } 1195 1196 static int get_xlated_program(int fd_prog, struct bpf_insn **buf, int *cnt) 1197 { 1198 struct bpf_prog_info info = {}; 1199 __u32 info_len = sizeof(info); 1200 __u32 xlated_prog_len; 1201 __u32 buf_element_size = sizeof(struct bpf_insn); 1202 1203 if (bpf_obj_get_info_by_fd(fd_prog, &info, &info_len)) { 1204 perror("bpf_obj_get_info_by_fd failed"); 1205 return -1; 1206 } 1207 1208 xlated_prog_len = info.xlated_prog_len; 1209 if (xlated_prog_len % buf_element_size) { 1210 printf("Program length %d is not multiple of %d\n", 1211 xlated_prog_len, buf_element_size); 1212 return -1; 1213 } 1214 1215 *cnt = xlated_prog_len / buf_element_size; 1216 *buf = calloc(*cnt, buf_element_size); 1217 if (!buf) { 1218 perror("can't allocate xlated program buffer"); 1219 return -ENOMEM; 1220 } 1221 1222 bzero(&info, sizeof(info)); 1223 info.xlated_prog_len = xlated_prog_len; 1224 info.xlated_prog_insns = (__u64)*buf; 1225 if (bpf_obj_get_info_by_fd(fd_prog, &info, &info_len)) { 1226 perror("second bpf_obj_get_info_by_fd failed"); 1227 goto out_free_buf; 1228 } 1229 1230 return 0; 1231 1232 out_free_buf: 1233 free(*buf); 1234 return -1; 1235 } 1236 1237 static bool is_null_insn(struct bpf_insn *insn) 1238 { 1239 struct bpf_insn null_insn = {}; 1240 1241 return memcmp(insn, &null_insn, sizeof(null_insn)) == 0; 1242 } 1243 1244 static bool is_skip_insn(struct bpf_insn *insn) 1245 { 1246 struct bpf_insn skip_insn = SKIP_INSNS(); 1247 1248 return memcmp(insn, &skip_insn, sizeof(skip_insn)) == 0; 1249 } 1250 1251 static int null_terminated_insn_len(struct bpf_insn *seq, int max_len) 1252 { 1253 int i; 1254 1255 for (i = 0; i < max_len; ++i) { 1256 if (is_null_insn(&seq[i])) 1257 return i; 1258 } 1259 return max_len; 1260 } 1261 1262 static bool compare_masked_insn(struct bpf_insn *orig, struct bpf_insn *masked) 1263 { 1264 struct bpf_insn orig_masked; 1265 1266 memcpy(&orig_masked, orig, sizeof(orig_masked)); 1267 if (masked->imm == INSN_IMM_MASK) 1268 orig_masked.imm = INSN_IMM_MASK; 1269 if (masked->off == INSN_OFF_MASK) 1270 orig_masked.off = INSN_OFF_MASK; 1271 1272 return memcmp(&orig_masked, masked, sizeof(orig_masked)) == 0; 1273 } 1274 1275 static int find_insn_subseq(struct bpf_insn *seq, struct bpf_insn *subseq, 1276 int seq_len, int subseq_len) 1277 { 1278 int i, j; 1279 1280 if (subseq_len > seq_len) 1281 return -1; 1282 1283 for (i = 0; i < seq_len - subseq_len + 1; ++i) { 1284 bool found = true; 1285 1286 for (j = 0; j < subseq_len; ++j) { 1287 if (!compare_masked_insn(&seq[i + j], &subseq[j])) { 1288 found = false; 1289 break; 1290 } 1291 } 1292 if (found) 1293 return i; 1294 } 1295 1296 return -1; 1297 } 1298 1299 static int find_skip_insn_marker(struct bpf_insn *seq, int len) 1300 { 1301 int i; 1302 1303 for (i = 0; i < len; ++i) 1304 if (is_skip_insn(&seq[i])) 1305 return i; 1306 1307 return -1; 1308 } 1309 1310 /* Return true if all sub-sequences in `subseqs` could be found in 1311 * `seq` one after another. Sub-sequences are separated by a single 1312 * nil instruction. 1313 */ 1314 static bool find_all_insn_subseqs(struct bpf_insn *seq, struct bpf_insn *subseqs, 1315 int seq_len, int max_subseqs_len) 1316 { 1317 int subseqs_len = null_terminated_insn_len(subseqs, max_subseqs_len); 1318 1319 while (subseqs_len > 0) { 1320 int skip_idx = find_skip_insn_marker(subseqs, subseqs_len); 1321 int cur_subseq_len = skip_idx < 0 ? subseqs_len : skip_idx; 1322 int subseq_idx = find_insn_subseq(seq, subseqs, 1323 seq_len, cur_subseq_len); 1324 1325 if (subseq_idx < 0) 1326 return false; 1327 seq += subseq_idx + cur_subseq_len; 1328 seq_len -= subseq_idx + cur_subseq_len; 1329 subseqs += cur_subseq_len + 1; 1330 subseqs_len -= cur_subseq_len + 1; 1331 } 1332 1333 return true; 1334 } 1335 1336 static void print_insn(struct bpf_insn *buf, int cnt) 1337 { 1338 int i; 1339 1340 printf(" addr op d s off imm\n"); 1341 for (i = 0; i < cnt; ++i) { 1342 struct bpf_insn *insn = &buf[i]; 1343 1344 if (is_null_insn(insn)) 1345 break; 1346 1347 if (is_skip_insn(insn)) 1348 printf(" ...\n"); 1349 else 1350 printf(" %04x: %02x %1x %x %04hx %08x\n", 1351 i, insn->code, insn->dst_reg, 1352 insn->src_reg, insn->off, insn->imm); 1353 } 1354 } 1355 1356 static bool check_xlated_program(struct bpf_test *test, int fd_prog) 1357 { 1358 struct bpf_insn *buf; 1359 int cnt; 1360 bool result = true; 1361 bool check_expected = !is_null_insn(test->expected_insns); 1362 bool check_unexpected = !is_null_insn(test->unexpected_insns); 1363 1364 if (!check_expected && !check_unexpected) 1365 goto out; 1366 1367 if (get_xlated_program(fd_prog, &buf, &cnt)) { 1368 printf("FAIL: can't get xlated program\n"); 1369 result = false; 1370 goto out; 1371 } 1372 1373 if (check_expected && 1374 !find_all_insn_subseqs(buf, test->expected_insns, 1375 cnt, MAX_EXPECTED_INSNS)) { 1376 printf("FAIL: can't find expected subsequence of instructions\n"); 1377 result = false; 1378 if (verbose) { 1379 printf("Program:\n"); 1380 print_insn(buf, cnt); 1381 printf("Expected subsequence:\n"); 1382 print_insn(test->expected_insns, MAX_EXPECTED_INSNS); 1383 } 1384 } 1385 1386 if (check_unexpected && 1387 find_all_insn_subseqs(buf, test->unexpected_insns, 1388 cnt, MAX_UNEXPECTED_INSNS)) { 1389 printf("FAIL: found unexpected subsequence of instructions\n"); 1390 result = false; 1391 if (verbose) { 1392 printf("Program:\n"); 1393 print_insn(buf, cnt); 1394 printf("Un-expected subsequence:\n"); 1395 print_insn(test->unexpected_insns, MAX_UNEXPECTED_INSNS); 1396 } 1397 } 1398 1399 free(buf); 1400 out: 1401 return result; 1402 } 1403 1404 static void do_test_single(struct bpf_test *test, bool unpriv, 1405 int *passes, int *errors) 1406 { 1407 int fd_prog, btf_fd, expected_ret, alignment_prevented_execution; 1408 int prog_len, prog_type = test->prog_type; 1409 struct bpf_insn *prog = test->insns; 1410 LIBBPF_OPTS(bpf_prog_load_opts, opts); 1411 int run_errs, run_successes; 1412 int map_fds[MAX_NR_MAPS]; 1413 const char *expected_err; 1414 int saved_errno; 1415 int fixup_skips; 1416 __u32 pflags; 1417 int i, err; 1418 1419 fd_prog = -1; 1420 for (i = 0; i < MAX_NR_MAPS; i++) 1421 map_fds[i] = -1; 1422 btf_fd = -1; 1423 1424 if (!prog_type) 1425 prog_type = BPF_PROG_TYPE_SOCKET_FILTER; 1426 fixup_skips = skips; 1427 do_test_fixup(test, prog_type, prog, map_fds); 1428 if (test->fill_insns) { 1429 prog = test->fill_insns; 1430 prog_len = test->prog_len; 1431 } else { 1432 prog_len = probe_filter_length(prog); 1433 } 1434 /* If there were some map skips during fixup due to missing bpf 1435 * features, skip this test. 1436 */ 1437 if (fixup_skips != skips) 1438 return; 1439 1440 pflags = BPF_F_TEST_RND_HI32; 1441 if (test->flags & F_LOAD_WITH_STRICT_ALIGNMENT) 1442 pflags |= BPF_F_STRICT_ALIGNMENT; 1443 if (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS) 1444 pflags |= BPF_F_ANY_ALIGNMENT; 1445 if (test->flags & ~3) 1446 pflags |= test->flags; 1447 1448 expected_ret = unpriv && test->result_unpriv != UNDEF ? 1449 test->result_unpriv : test->result; 1450 expected_err = unpriv && test->errstr_unpriv ? 1451 test->errstr_unpriv : test->errstr; 1452 1453 opts.expected_attach_type = test->expected_attach_type; 1454 if (verbose) 1455 opts.log_level = VERBOSE_LIBBPF_LOG_LEVEL; 1456 else if (expected_ret == VERBOSE_ACCEPT) 1457 opts.log_level = 2; 1458 else 1459 opts.log_level = DEFAULT_LIBBPF_LOG_LEVEL; 1460 opts.prog_flags = pflags; 1461 1462 if (prog_type == BPF_PROG_TYPE_TRACING && test->kfunc) { 1463 int attach_btf_id; 1464 1465 attach_btf_id = libbpf_find_vmlinux_btf_id(test->kfunc, 1466 opts.expected_attach_type); 1467 if (attach_btf_id < 0) { 1468 printf("FAIL\nFailed to find BTF ID for '%s'!\n", 1469 test->kfunc); 1470 (*errors)++; 1471 return; 1472 } 1473 1474 opts.attach_btf_id = attach_btf_id; 1475 } 1476 1477 if (test->btf_types[0] != 0) { 1478 btf_fd = load_btf_for_test(test); 1479 if (btf_fd < 0) 1480 goto fail_log; 1481 opts.prog_btf_fd = btf_fd; 1482 } 1483 1484 if (test->func_info_cnt != 0) { 1485 opts.func_info = test->func_info; 1486 opts.func_info_cnt = test->func_info_cnt; 1487 opts.func_info_rec_size = sizeof(test->func_info[0]); 1488 } 1489 1490 opts.log_buf = bpf_vlog; 1491 opts.log_size = sizeof(bpf_vlog); 1492 fd_prog = bpf_prog_load(prog_type, NULL, "GPL", prog, prog_len, &opts); 1493 saved_errno = errno; 1494 1495 /* BPF_PROG_TYPE_TRACING requires more setup and 1496 * bpf_probe_prog_type won't give correct answer 1497 */ 1498 if (fd_prog < 0 && prog_type != BPF_PROG_TYPE_TRACING && 1499 !libbpf_probe_bpf_prog_type(prog_type, NULL)) { 1500 printf("SKIP (unsupported program type %d)\n", prog_type); 1501 skips++; 1502 goto close_fds; 1503 } 1504 1505 if (fd_prog < 0 && saved_errno == ENOTSUPP) { 1506 printf("SKIP (program uses an unsupported feature)\n"); 1507 skips++; 1508 goto close_fds; 1509 } 1510 1511 alignment_prevented_execution = 0; 1512 1513 if (expected_ret == ACCEPT || expected_ret == VERBOSE_ACCEPT) { 1514 if (fd_prog < 0) { 1515 printf("FAIL\nFailed to load prog '%s'!\n", 1516 strerror(saved_errno)); 1517 goto fail_log; 1518 } 1519 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 1520 if (fd_prog >= 0 && 1521 (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS)) 1522 alignment_prevented_execution = 1; 1523 #endif 1524 if (expected_ret == VERBOSE_ACCEPT && !cmp_str_seq(bpf_vlog, expected_err)) { 1525 goto fail_log; 1526 } 1527 } else { 1528 if (fd_prog >= 0) { 1529 printf("FAIL\nUnexpected success to load!\n"); 1530 goto fail_log; 1531 } 1532 if (!expected_err || !cmp_str_seq(bpf_vlog, expected_err)) { 1533 printf("FAIL\nUnexpected error message!\n\tEXP: %s\n\tRES: %s\n", 1534 expected_err, bpf_vlog); 1535 goto fail_log; 1536 } 1537 } 1538 1539 if (!unpriv && test->insn_processed) { 1540 uint32_t insn_processed; 1541 char *proc; 1542 1543 proc = strstr(bpf_vlog, "processed "); 1544 insn_processed = atoi(proc + 10); 1545 if (test->insn_processed != insn_processed) { 1546 printf("FAIL\nUnexpected insn_processed %u vs %u\n", 1547 insn_processed, test->insn_processed); 1548 goto fail_log; 1549 } 1550 } 1551 1552 if (verbose) 1553 printf(", verifier log:\n%s", bpf_vlog); 1554 1555 if (!check_xlated_program(test, fd_prog)) 1556 goto fail_log; 1557 1558 run_errs = 0; 1559 run_successes = 0; 1560 if (!alignment_prevented_execution && fd_prog >= 0 && test->runs >= 0) { 1561 uint32_t expected_val; 1562 int i; 1563 1564 if (!test->runs) 1565 test->runs = 1; 1566 1567 for (i = 0; i < test->runs; i++) { 1568 if (unpriv && test->retvals[i].retval_unpriv) 1569 expected_val = test->retvals[i].retval_unpriv; 1570 else 1571 expected_val = test->retvals[i].retval; 1572 1573 err = do_prog_test_run(fd_prog, unpriv, expected_val, 1574 test->retvals[i].data, 1575 sizeof(test->retvals[i].data)); 1576 if (err) { 1577 printf("(run %d/%d) ", i + 1, test->runs); 1578 run_errs++; 1579 } else { 1580 run_successes++; 1581 } 1582 } 1583 } 1584 1585 if (!run_errs) { 1586 (*passes)++; 1587 if (run_successes > 1) 1588 printf("%d cases ", run_successes); 1589 printf("OK"); 1590 if (alignment_prevented_execution) 1591 printf(" (NOTE: not executed due to unknown alignment)"); 1592 printf("\n"); 1593 } else { 1594 printf("\n"); 1595 goto fail_log; 1596 } 1597 close_fds: 1598 if (test->fill_insns) 1599 free(test->fill_insns); 1600 close(fd_prog); 1601 close(btf_fd); 1602 for (i = 0; i < MAX_NR_MAPS; i++) 1603 close(map_fds[i]); 1604 sched_yield(); 1605 return; 1606 fail_log: 1607 (*errors)++; 1608 printf("%s", bpf_vlog); 1609 goto close_fds; 1610 } 1611 1612 static bool is_admin(void) 1613 { 1614 __u64 caps; 1615 1616 /* The test checks for finer cap as CAP_NET_ADMIN, 1617 * CAP_PERFMON, and CAP_BPF instead of CAP_SYS_ADMIN. 1618 * Thus, disable CAP_SYS_ADMIN at the beginning. 1619 */ 1620 if (cap_disable_effective(1ULL << CAP_SYS_ADMIN, &caps)) { 1621 perror("cap_disable_effective(CAP_SYS_ADMIN)"); 1622 return false; 1623 } 1624 1625 return (caps & ADMIN_CAPS) == ADMIN_CAPS; 1626 } 1627 1628 static void get_unpriv_disabled() 1629 { 1630 char buf[2]; 1631 FILE *fd; 1632 1633 fd = fopen("/proc/sys/"UNPRIV_SYSCTL, "r"); 1634 if (!fd) { 1635 perror("fopen /proc/sys/"UNPRIV_SYSCTL); 1636 unpriv_disabled = true; 1637 return; 1638 } 1639 if (fgets(buf, 2, fd) == buf && atoi(buf)) 1640 unpriv_disabled = true; 1641 fclose(fd); 1642 } 1643 1644 static bool test_as_unpriv(struct bpf_test *test) 1645 { 1646 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 1647 /* Some architectures have strict alignment requirements. In 1648 * that case, the BPF verifier detects if a program has 1649 * unaligned accesses and rejects them. A user can pass 1650 * BPF_F_ANY_ALIGNMENT to a program to override this 1651 * check. That, however, will only work when a privileged user 1652 * loads a program. An unprivileged user loading a program 1653 * with this flag will be rejected prior entering the 1654 * verifier. 1655 */ 1656 if (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS) 1657 return false; 1658 #endif 1659 return !test->prog_type || 1660 test->prog_type == BPF_PROG_TYPE_SOCKET_FILTER || 1661 test->prog_type == BPF_PROG_TYPE_CGROUP_SKB; 1662 } 1663 1664 static int do_test(bool unpriv, unsigned int from, unsigned int to) 1665 { 1666 int i, passes = 0, errors = 0; 1667 1668 for (i = from; i < to; i++) { 1669 struct bpf_test *test = &tests[i]; 1670 1671 /* Program types that are not supported by non-root we 1672 * skip right away. 1673 */ 1674 if (test_as_unpriv(test) && unpriv_disabled) { 1675 printf("#%d/u %s SKIP\n", i, test->descr); 1676 skips++; 1677 } else if (test_as_unpriv(test)) { 1678 if (!unpriv) 1679 set_admin(false); 1680 printf("#%d/u %s ", i, test->descr); 1681 do_test_single(test, true, &passes, &errors); 1682 if (!unpriv) 1683 set_admin(true); 1684 } 1685 1686 if (unpriv) { 1687 printf("#%d/p %s SKIP\n", i, test->descr); 1688 skips++; 1689 } else { 1690 printf("#%d/p %s ", i, test->descr); 1691 do_test_single(test, false, &passes, &errors); 1692 } 1693 } 1694 1695 printf("Summary: %d PASSED, %d SKIPPED, %d FAILED\n", passes, 1696 skips, errors); 1697 return errors ? EXIT_FAILURE : EXIT_SUCCESS; 1698 } 1699 1700 int main(int argc, char **argv) 1701 { 1702 unsigned int from = 0, to = ARRAY_SIZE(tests); 1703 bool unpriv = !is_admin(); 1704 int arg = 1; 1705 1706 if (argc > 1 && strcmp(argv[1], "-v") == 0) { 1707 arg++; 1708 verbose = true; 1709 argc--; 1710 } 1711 1712 if (argc == 3) { 1713 unsigned int l = atoi(argv[arg]); 1714 unsigned int u = atoi(argv[arg + 1]); 1715 1716 if (l < to && u < to) { 1717 from = l; 1718 to = u + 1; 1719 } 1720 } else if (argc == 2) { 1721 unsigned int t = atoi(argv[arg]); 1722 1723 if (t < to) { 1724 from = t; 1725 to = t + 1; 1726 } 1727 } 1728 1729 get_unpriv_disabled(); 1730 if (unpriv && unpriv_disabled) { 1731 printf("Cannot run as unprivileged user with sysctl %s.\n", 1732 UNPRIV_SYSCTL); 1733 return EXIT_FAILURE; 1734 } 1735 1736 /* Use libbpf 1.0 API mode */ 1737 libbpf_set_strict_mode(LIBBPF_STRICT_ALL); 1738 1739 bpf_semi_rand_init(); 1740 return do_test(unpriv, from, to); 1741 } 1742