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