1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Testsuite for eBPF verifier 4 * 5 * Copyright (c) 2014 PLUMgrid, http://plumgrid.com 6 * Copyright (c) 2017 Facebook 7 * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io 8 */ 9 10 #include <endian.h> 11 #include <asm/types.h> 12 #include <linux/types.h> 13 #include <stdint.h> 14 #include <stdio.h> 15 #include <stdlib.h> 16 #include <unistd.h> 17 #include <errno.h> 18 #include <string.h> 19 #include <stddef.h> 20 #include <stdbool.h> 21 #include <sched.h> 22 #include <limits.h> 23 #include <assert.h> 24 25 #include <sys/capability.h> 26 27 #include <linux/unistd.h> 28 #include <linux/filter.h> 29 #include <linux/bpf_perf_event.h> 30 #include <linux/bpf.h> 31 #include <linux/if_ether.h> 32 #include <linux/btf.h> 33 34 #include <bpf/bpf.h> 35 #include <bpf/libbpf.h> 36 37 #ifdef HAVE_GENHDR 38 # include "autoconf.h" 39 #else 40 # if defined(__i386) || defined(__x86_64) || defined(__s390x__) || defined(__aarch64__) 41 # define CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 1 42 # endif 43 #endif 44 #include "bpf_rlimit.h" 45 #include "bpf_rand.h" 46 #include "bpf_util.h" 47 #include "test_btf.h" 48 #include "../../../include/linux/filter.h" 49 50 #ifndef ENOTSUPP 51 #define ENOTSUPP 524 52 #endif 53 54 #define MAX_INSNS BPF_MAXINSNS 55 #define MAX_TEST_INSNS 1000000 56 #define MAX_FIXUPS 8 57 #define MAX_NR_MAPS 21 58 #define MAX_TEST_RUNS 8 59 #define POINTER_VALUE 0xcafe4all 60 #define TEST_DATA_LEN 64 61 62 #define F_NEEDS_EFFICIENT_UNALIGNED_ACCESS (1 << 0) 63 #define F_LOAD_WITH_STRICT_ALIGNMENT (1 << 1) 64 65 #define UNPRIV_SYSCTL "kernel/unprivileged_bpf_disabled" 66 static bool unpriv_disabled = false; 67 static int skips; 68 static bool verbose = false; 69 70 struct bpf_test { 71 const char *descr; 72 struct bpf_insn insns[MAX_INSNS]; 73 struct bpf_insn *fill_insns; 74 int fixup_map_hash_8b[MAX_FIXUPS]; 75 int fixup_map_hash_48b[MAX_FIXUPS]; 76 int fixup_map_hash_16b[MAX_FIXUPS]; 77 int fixup_map_array_48b[MAX_FIXUPS]; 78 int fixup_map_sockmap[MAX_FIXUPS]; 79 int fixup_map_sockhash[MAX_FIXUPS]; 80 int fixup_map_xskmap[MAX_FIXUPS]; 81 int fixup_map_stacktrace[MAX_FIXUPS]; 82 int fixup_prog1[MAX_FIXUPS]; 83 int fixup_prog2[MAX_FIXUPS]; 84 int fixup_map_in_map[MAX_FIXUPS]; 85 int fixup_cgroup_storage[MAX_FIXUPS]; 86 int fixup_percpu_cgroup_storage[MAX_FIXUPS]; 87 int fixup_map_spin_lock[MAX_FIXUPS]; 88 int fixup_map_array_ro[MAX_FIXUPS]; 89 int fixup_map_array_wo[MAX_FIXUPS]; 90 int fixup_map_array_small[MAX_FIXUPS]; 91 int fixup_sk_storage_map[MAX_FIXUPS]; 92 int fixup_map_event_output[MAX_FIXUPS]; 93 int fixup_map_reuseport_array[MAX_FIXUPS]; 94 int fixup_map_ringbuf[MAX_FIXUPS]; 95 int fixup_map_timer[MAX_FIXUPS]; 96 /* Expected verifier log output for result REJECT or VERBOSE_ACCEPT. 97 * Can be a tab-separated sequence of expected strings. An empty string 98 * means no log verification. 99 */ 100 const char *errstr; 101 const char *errstr_unpriv; 102 uint32_t insn_processed; 103 int prog_len; 104 enum { 105 UNDEF, 106 ACCEPT, 107 REJECT, 108 VERBOSE_ACCEPT, 109 } result, result_unpriv; 110 enum bpf_prog_type prog_type; 111 uint8_t flags; 112 void (*fill_helper)(struct bpf_test *self); 113 int runs; 114 #define bpf_testdata_struct_t \ 115 struct { \ 116 uint32_t retval, retval_unpriv; \ 117 union { \ 118 __u8 data[TEST_DATA_LEN]; \ 119 __u64 data64[TEST_DATA_LEN / 8]; \ 120 }; \ 121 } 122 union { 123 bpf_testdata_struct_t; 124 bpf_testdata_struct_t retvals[MAX_TEST_RUNS]; 125 }; 126 enum bpf_attach_type expected_attach_type; 127 const char *kfunc; 128 }; 129 130 /* Note we want this to be 64 bit aligned so that the end of our array is 131 * actually the end of the structure. 132 */ 133 #define MAX_ENTRIES 11 134 135 struct test_val { 136 unsigned int index; 137 int foo[MAX_ENTRIES]; 138 }; 139 140 struct other_val { 141 long long foo; 142 long long bar; 143 }; 144 145 static void bpf_fill_ld_abs_vlan_push_pop(struct bpf_test *self) 146 { 147 /* test: {skb->data[0], vlan_push} x 51 + {skb->data[0], vlan_pop} x 51 */ 148 #define PUSH_CNT 51 149 /* jump range is limited to 16 bit. PUSH_CNT of ld_abs needs room */ 150 unsigned int len = (1 << 15) - PUSH_CNT * 2 * 5 * 6; 151 struct bpf_insn *insn = self->fill_insns; 152 int i = 0, j, k = 0; 153 154 insn[i++] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1); 155 loop: 156 for (j = 0; j < PUSH_CNT; j++) { 157 insn[i++] = BPF_LD_ABS(BPF_B, 0); 158 /* jump to error label */ 159 insn[i] = BPF_JMP32_IMM(BPF_JNE, BPF_REG_0, 0x34, len - i - 3); 160 i++; 161 insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6); 162 insn[i++] = BPF_MOV64_IMM(BPF_REG_2, 1); 163 insn[i++] = BPF_MOV64_IMM(BPF_REG_3, 2); 164 insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, 165 BPF_FUNC_skb_vlan_push), 166 insn[i] = BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, len - i - 3); 167 i++; 168 } 169 170 for (j = 0; j < PUSH_CNT; j++) { 171 insn[i++] = BPF_LD_ABS(BPF_B, 0); 172 insn[i] = BPF_JMP32_IMM(BPF_JNE, BPF_REG_0, 0x34, len - i - 3); 173 i++; 174 insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6); 175 insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, 176 BPF_FUNC_skb_vlan_pop), 177 insn[i] = BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, len - i - 3); 178 i++; 179 } 180 if (++k < 5) 181 goto loop; 182 183 for (; i < len - 3; i++) 184 insn[i] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 0xbef); 185 insn[len - 3] = BPF_JMP_A(1); 186 /* error label */ 187 insn[len - 2] = BPF_MOV32_IMM(BPF_REG_0, 0); 188 insn[len - 1] = BPF_EXIT_INSN(); 189 self->prog_len = len; 190 } 191 192 static void bpf_fill_jump_around_ld_abs(struct bpf_test *self) 193 { 194 struct bpf_insn *insn = self->fill_insns; 195 /* jump range is limited to 16 bit. every ld_abs is replaced by 6 insns, 196 * but on arches like arm, ppc etc, there will be one BPF_ZEXT inserted 197 * to extend the error value of the inlined ld_abs sequence which then 198 * contains 7 insns. so, set the dividend to 7 so the testcase could 199 * work on all arches. 200 */ 201 unsigned int len = (1 << 15) / 7; 202 int i = 0; 203 204 insn[i++] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1); 205 insn[i++] = BPF_LD_ABS(BPF_B, 0); 206 insn[i] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 10, len - i - 2); 207 i++; 208 while (i < len - 1) 209 insn[i++] = BPF_LD_ABS(BPF_B, 1); 210 insn[i] = BPF_EXIT_INSN(); 211 self->prog_len = i + 1; 212 } 213 214 static void bpf_fill_rand_ld_dw(struct bpf_test *self) 215 { 216 struct bpf_insn *insn = self->fill_insns; 217 uint64_t res = 0; 218 int i = 0; 219 220 insn[i++] = BPF_MOV32_IMM(BPF_REG_0, 0); 221 while (i < self->retval) { 222 uint64_t val = bpf_semi_rand_get(); 223 struct bpf_insn tmp[2] = { BPF_LD_IMM64(BPF_REG_1, val) }; 224 225 res ^= val; 226 insn[i++] = tmp[0]; 227 insn[i++] = tmp[1]; 228 insn[i++] = BPF_ALU64_REG(BPF_XOR, BPF_REG_0, BPF_REG_1); 229 } 230 insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_0); 231 insn[i++] = BPF_ALU64_IMM(BPF_RSH, BPF_REG_1, 32); 232 insn[i++] = BPF_ALU64_REG(BPF_XOR, BPF_REG_0, BPF_REG_1); 233 insn[i] = BPF_EXIT_INSN(); 234 self->prog_len = i + 1; 235 res ^= (res >> 32); 236 self->retval = (uint32_t)res; 237 } 238 239 #define MAX_JMP_SEQ 8192 240 241 /* test the sequence of 8k jumps */ 242 static void bpf_fill_scale1(struct bpf_test *self) 243 { 244 struct bpf_insn *insn = self->fill_insns; 245 int i = 0, k = 0; 246 247 insn[i++] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1); 248 /* test to check that the long sequence of jumps is acceptable */ 249 while (k++ < MAX_JMP_SEQ) { 250 insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, 251 BPF_FUNC_get_prandom_u32); 252 insn[i++] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, bpf_semi_rand_get(), 2); 253 insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_10); 254 insn[i++] = BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, 255 -8 * (k % 64 + 1)); 256 } 257 /* is_state_visited() doesn't allocate state for pruning for every jump. 258 * Hence multiply jmps by 4 to accommodate that heuristic 259 */ 260 while (i < MAX_TEST_INSNS - MAX_JMP_SEQ * 4) 261 insn[i++] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 42); 262 insn[i] = BPF_EXIT_INSN(); 263 self->prog_len = i + 1; 264 self->retval = 42; 265 } 266 267 /* test the sequence of 8k jumps in inner most function (function depth 8)*/ 268 static void bpf_fill_scale2(struct bpf_test *self) 269 { 270 struct bpf_insn *insn = self->fill_insns; 271 int i = 0, k = 0; 272 273 #define FUNC_NEST 7 274 for (k = 0; k < FUNC_NEST; k++) { 275 insn[i++] = BPF_CALL_REL(1); 276 insn[i++] = BPF_EXIT_INSN(); 277 } 278 insn[i++] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1); 279 /* test to check that the long sequence of jumps is acceptable */ 280 k = 0; 281 while (k++ < MAX_JMP_SEQ) { 282 insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, 283 BPF_FUNC_get_prandom_u32); 284 insn[i++] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, bpf_semi_rand_get(), 2); 285 insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_10); 286 insn[i++] = BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, 287 -8 * (k % (64 - 4 * FUNC_NEST) + 1)); 288 } 289 while (i < MAX_TEST_INSNS - MAX_JMP_SEQ * 4) 290 insn[i++] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 42); 291 insn[i] = BPF_EXIT_INSN(); 292 self->prog_len = i + 1; 293 self->retval = 42; 294 } 295 296 static void bpf_fill_scale(struct bpf_test *self) 297 { 298 switch (self->retval) { 299 case 1: 300 return bpf_fill_scale1(self); 301 case 2: 302 return bpf_fill_scale2(self); 303 default: 304 self->prog_len = 0; 305 break; 306 } 307 } 308 309 static int bpf_fill_torturous_jumps_insn_1(struct bpf_insn *insn) 310 { 311 unsigned int len = 259, hlen = 128; 312 int i; 313 314 insn[0] = BPF_EMIT_CALL(BPF_FUNC_get_prandom_u32); 315 for (i = 1; i <= hlen; i++) { 316 insn[i] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, i, hlen); 317 insn[i + hlen] = BPF_JMP_A(hlen - i); 318 } 319 insn[len - 2] = BPF_MOV64_IMM(BPF_REG_0, 1); 320 insn[len - 1] = BPF_EXIT_INSN(); 321 322 return len; 323 } 324 325 static int bpf_fill_torturous_jumps_insn_2(struct bpf_insn *insn) 326 { 327 unsigned int len = 4100, jmp_off = 2048; 328 int i, j; 329 330 insn[0] = BPF_EMIT_CALL(BPF_FUNC_get_prandom_u32); 331 for (i = 1; i <= jmp_off; i++) { 332 insn[i] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, i, jmp_off); 333 } 334 insn[i++] = BPF_JMP_A(jmp_off); 335 for (; i <= jmp_off * 2 + 1; i+=16) { 336 for (j = 0; j < 16; j++) { 337 insn[i + j] = BPF_JMP_A(16 - j - 1); 338 } 339 } 340 341 insn[len - 2] = BPF_MOV64_IMM(BPF_REG_0, 2); 342 insn[len - 1] = BPF_EXIT_INSN(); 343 344 return len; 345 } 346 347 static void bpf_fill_torturous_jumps(struct bpf_test *self) 348 { 349 struct bpf_insn *insn = self->fill_insns; 350 int i = 0; 351 352 switch (self->retval) { 353 case 1: 354 self->prog_len = bpf_fill_torturous_jumps_insn_1(insn); 355 return; 356 case 2: 357 self->prog_len = bpf_fill_torturous_jumps_insn_2(insn); 358 return; 359 case 3: 360 /* main */ 361 insn[i++] = BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 4); 362 insn[i++] = BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 262); 363 insn[i++] = BPF_ST_MEM(BPF_B, BPF_REG_10, -32, 0); 364 insn[i++] = BPF_MOV64_IMM(BPF_REG_0, 3); 365 insn[i++] = BPF_EXIT_INSN(); 366 367 /* subprog 1 */ 368 i += bpf_fill_torturous_jumps_insn_1(insn + i); 369 370 /* subprog 2 */ 371 i += bpf_fill_torturous_jumps_insn_2(insn + i); 372 373 self->prog_len = i; 374 return; 375 default: 376 self->prog_len = 0; 377 break; 378 } 379 } 380 381 /* BPF_SK_LOOKUP contains 13 instructions, if you need to fix up maps */ 382 #define BPF_SK_LOOKUP(func) \ 383 /* struct bpf_sock_tuple tuple = {} */ \ 384 BPF_MOV64_IMM(BPF_REG_2, 0), \ 385 BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_2, -8), \ 386 BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -16), \ 387 BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -24), \ 388 BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -32), \ 389 BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -40), \ 390 BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -48), \ 391 /* sk = func(ctx, &tuple, sizeof tuple, 0, 0) */ \ 392 BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), \ 393 BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -48), \ 394 BPF_MOV64_IMM(BPF_REG_3, sizeof(struct bpf_sock_tuple)), \ 395 BPF_MOV64_IMM(BPF_REG_4, 0), \ 396 BPF_MOV64_IMM(BPF_REG_5, 0), \ 397 BPF_EMIT_CALL(BPF_FUNC_ ## func) 398 399 /* BPF_DIRECT_PKT_R2 contains 7 instructions, it initializes default return 400 * value into 0 and does necessary preparation for direct packet access 401 * through r2. The allowed access range is 8 bytes. 402 */ 403 #define BPF_DIRECT_PKT_R2 \ 404 BPF_MOV64_IMM(BPF_REG_0, 0), \ 405 BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, \ 406 offsetof(struct __sk_buff, data)), \ 407 BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1, \ 408 offsetof(struct __sk_buff, data_end)), \ 409 BPF_MOV64_REG(BPF_REG_4, BPF_REG_2), \ 410 BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 8), \ 411 BPF_JMP_REG(BPF_JLE, BPF_REG_4, BPF_REG_3, 1), \ 412 BPF_EXIT_INSN() 413 414 /* BPF_RAND_UEXT_R7 contains 4 instructions, it initializes R7 into a random 415 * positive u32, and zero-extend it into 64-bit. 416 */ 417 #define BPF_RAND_UEXT_R7 \ 418 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, \ 419 BPF_FUNC_get_prandom_u32), \ 420 BPF_MOV64_REG(BPF_REG_7, BPF_REG_0), \ 421 BPF_ALU64_IMM(BPF_LSH, BPF_REG_7, 33), \ 422 BPF_ALU64_IMM(BPF_RSH, BPF_REG_7, 33) 423 424 /* BPF_RAND_SEXT_R7 contains 5 instructions, it initializes R7 into a random 425 * negative u32, and sign-extend it into 64-bit. 426 */ 427 #define BPF_RAND_SEXT_R7 \ 428 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, \ 429 BPF_FUNC_get_prandom_u32), \ 430 BPF_MOV64_REG(BPF_REG_7, BPF_REG_0), \ 431 BPF_ALU64_IMM(BPF_OR, BPF_REG_7, 0x80000000), \ 432 BPF_ALU64_IMM(BPF_LSH, BPF_REG_7, 32), \ 433 BPF_ALU64_IMM(BPF_ARSH, BPF_REG_7, 32) 434 435 static struct bpf_test tests[] = { 436 #define FILL_ARRAY 437 #include <verifier/tests.h> 438 #undef FILL_ARRAY 439 }; 440 441 static int probe_filter_length(const struct bpf_insn *fp) 442 { 443 int len; 444 445 for (len = MAX_INSNS - 1; len > 0; --len) 446 if (fp[len].code != 0 || fp[len].imm != 0) 447 break; 448 return len + 1; 449 } 450 451 static bool skip_unsupported_map(enum bpf_map_type map_type) 452 { 453 if (!bpf_probe_map_type(map_type, 0)) { 454 printf("SKIP (unsupported map type %d)\n", map_type); 455 skips++; 456 return true; 457 } 458 return false; 459 } 460 461 static int __create_map(uint32_t type, uint32_t size_key, 462 uint32_t size_value, uint32_t max_elem, 463 uint32_t extra_flags) 464 { 465 int fd; 466 467 fd = bpf_create_map(type, size_key, size_value, max_elem, 468 (type == BPF_MAP_TYPE_HASH ? 469 BPF_F_NO_PREALLOC : 0) | extra_flags); 470 if (fd < 0) { 471 if (skip_unsupported_map(type)) 472 return -1; 473 printf("Failed to create hash map '%s'!\n", strerror(errno)); 474 } 475 476 return fd; 477 } 478 479 static int create_map(uint32_t type, uint32_t size_key, 480 uint32_t size_value, uint32_t max_elem) 481 { 482 return __create_map(type, size_key, size_value, max_elem, 0); 483 } 484 485 static void update_map(int fd, int index) 486 { 487 struct test_val value = { 488 .index = (6 + 1) * sizeof(int), 489 .foo[6] = 0xabcdef12, 490 }; 491 492 assert(!bpf_map_update_elem(fd, &index, &value, 0)); 493 } 494 495 static int create_prog_dummy_simple(enum bpf_prog_type prog_type, int ret) 496 { 497 struct bpf_insn prog[] = { 498 BPF_MOV64_IMM(BPF_REG_0, ret), 499 BPF_EXIT_INSN(), 500 }; 501 502 return bpf_prog_load(prog_type, NULL, "GPL", prog, ARRAY_SIZE(prog), NULL); 503 } 504 505 static int create_prog_dummy_loop(enum bpf_prog_type prog_type, int mfd, 506 int idx, int ret) 507 { 508 struct bpf_insn prog[] = { 509 BPF_MOV64_IMM(BPF_REG_3, idx), 510 BPF_LD_MAP_FD(BPF_REG_2, mfd), 511 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, 512 BPF_FUNC_tail_call), 513 BPF_MOV64_IMM(BPF_REG_0, ret), 514 BPF_EXIT_INSN(), 515 }; 516 517 return bpf_prog_load(prog_type, NULL, "GPL", prog, ARRAY_SIZE(prog), NULL); 518 } 519 520 static int create_prog_array(enum bpf_prog_type prog_type, uint32_t max_elem, 521 int p1key, int p2key, int p3key) 522 { 523 int mfd, p1fd, p2fd, p3fd; 524 525 mfd = bpf_create_map(BPF_MAP_TYPE_PROG_ARRAY, sizeof(int), 526 sizeof(int), max_elem, 0); 527 if (mfd < 0) { 528 if (skip_unsupported_map(BPF_MAP_TYPE_PROG_ARRAY)) 529 return -1; 530 printf("Failed to create prog array '%s'!\n", strerror(errno)); 531 return -1; 532 } 533 534 p1fd = create_prog_dummy_simple(prog_type, 42); 535 p2fd = create_prog_dummy_loop(prog_type, mfd, p2key, 41); 536 p3fd = create_prog_dummy_simple(prog_type, 24); 537 if (p1fd < 0 || p2fd < 0 || p3fd < 0) 538 goto err; 539 if (bpf_map_update_elem(mfd, &p1key, &p1fd, BPF_ANY) < 0) 540 goto err; 541 if (bpf_map_update_elem(mfd, &p2key, &p2fd, BPF_ANY) < 0) 542 goto err; 543 if (bpf_map_update_elem(mfd, &p3key, &p3fd, BPF_ANY) < 0) { 544 err: 545 close(mfd); 546 mfd = -1; 547 } 548 close(p3fd); 549 close(p2fd); 550 close(p1fd); 551 return mfd; 552 } 553 554 static int create_map_in_map(void) 555 { 556 int inner_map_fd, outer_map_fd; 557 558 inner_map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(int), 559 sizeof(int), 1, 0); 560 if (inner_map_fd < 0) { 561 if (skip_unsupported_map(BPF_MAP_TYPE_ARRAY)) 562 return -1; 563 printf("Failed to create array '%s'!\n", strerror(errno)); 564 return inner_map_fd; 565 } 566 567 outer_map_fd = bpf_create_map_in_map(BPF_MAP_TYPE_ARRAY_OF_MAPS, NULL, 568 sizeof(int), inner_map_fd, 1, 0); 569 if (outer_map_fd < 0) { 570 if (skip_unsupported_map(BPF_MAP_TYPE_ARRAY_OF_MAPS)) 571 return -1; 572 printf("Failed to create array of maps '%s'!\n", 573 strerror(errno)); 574 } 575 576 close(inner_map_fd); 577 578 return outer_map_fd; 579 } 580 581 static int create_cgroup_storage(bool percpu) 582 { 583 enum bpf_map_type type = percpu ? BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE : 584 BPF_MAP_TYPE_CGROUP_STORAGE; 585 int fd; 586 587 fd = bpf_create_map(type, sizeof(struct bpf_cgroup_storage_key), 588 TEST_DATA_LEN, 0, 0); 589 if (fd < 0) { 590 if (skip_unsupported_map(type)) 591 return -1; 592 printf("Failed to create cgroup storage '%s'!\n", 593 strerror(errno)); 594 } 595 596 return fd; 597 } 598 599 /* struct bpf_spin_lock { 600 * int val; 601 * }; 602 * struct val { 603 * int cnt; 604 * struct bpf_spin_lock l; 605 * }; 606 * struct bpf_timer { 607 * __u64 :64; 608 * __u64 :64; 609 * } __attribute__((aligned(8))); 610 * struct timer { 611 * struct bpf_timer t; 612 * }; 613 */ 614 static const char btf_str_sec[] = "\0bpf_spin_lock\0val\0cnt\0l\0bpf_timer\0timer\0t"; 615 static __u32 btf_raw_types[] = { 616 /* int */ 617 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ 618 /* struct bpf_spin_lock */ /* [2] */ 619 BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4), 620 BTF_MEMBER_ENC(15, 1, 0), /* int val; */ 621 /* struct val */ /* [3] */ 622 BTF_TYPE_ENC(15, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8), 623 BTF_MEMBER_ENC(19, 1, 0), /* int cnt; */ 624 BTF_MEMBER_ENC(23, 2, 32),/* struct bpf_spin_lock l; */ 625 /* struct bpf_timer */ /* [4] */ 626 BTF_TYPE_ENC(25, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 0), 16), 627 /* struct timer */ /* [5] */ 628 BTF_TYPE_ENC(35, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 16), 629 BTF_MEMBER_ENC(41, 4, 0), /* struct bpf_timer t; */ 630 }; 631 632 static int load_btf(void) 633 { 634 struct btf_header hdr = { 635 .magic = BTF_MAGIC, 636 .version = BTF_VERSION, 637 .hdr_len = sizeof(struct btf_header), 638 .type_len = sizeof(btf_raw_types), 639 .str_off = sizeof(btf_raw_types), 640 .str_len = sizeof(btf_str_sec), 641 }; 642 void *ptr, *raw_btf; 643 int btf_fd; 644 645 ptr = raw_btf = malloc(sizeof(hdr) + sizeof(btf_raw_types) + 646 sizeof(btf_str_sec)); 647 648 memcpy(ptr, &hdr, sizeof(hdr)); 649 ptr += sizeof(hdr); 650 memcpy(ptr, btf_raw_types, hdr.type_len); 651 ptr += hdr.type_len; 652 memcpy(ptr, btf_str_sec, hdr.str_len); 653 ptr += hdr.str_len; 654 655 btf_fd = bpf_load_btf(raw_btf, ptr - raw_btf, 0, 0, 0); 656 free(raw_btf); 657 if (btf_fd < 0) 658 return -1; 659 return btf_fd; 660 } 661 662 static int create_map_spin_lock(void) 663 { 664 struct bpf_create_map_attr attr = { 665 .name = "test_map", 666 .map_type = BPF_MAP_TYPE_ARRAY, 667 .key_size = 4, 668 .value_size = 8, 669 .max_entries = 1, 670 .btf_key_type_id = 1, 671 .btf_value_type_id = 3, 672 }; 673 int fd, btf_fd; 674 675 btf_fd = load_btf(); 676 if (btf_fd < 0) 677 return -1; 678 attr.btf_fd = btf_fd; 679 fd = bpf_create_map_xattr(&attr); 680 if (fd < 0) 681 printf("Failed to create map with spin_lock\n"); 682 return fd; 683 } 684 685 static int create_sk_storage_map(void) 686 { 687 struct bpf_create_map_attr attr = { 688 .name = "test_map", 689 .map_type = BPF_MAP_TYPE_SK_STORAGE, 690 .key_size = 4, 691 .value_size = 8, 692 .max_entries = 0, 693 .map_flags = BPF_F_NO_PREALLOC, 694 .btf_key_type_id = 1, 695 .btf_value_type_id = 3, 696 }; 697 int fd, btf_fd; 698 699 btf_fd = load_btf(); 700 if (btf_fd < 0) 701 return -1; 702 attr.btf_fd = btf_fd; 703 fd = bpf_create_map_xattr(&attr); 704 close(attr.btf_fd); 705 if (fd < 0) 706 printf("Failed to create sk_storage_map\n"); 707 return fd; 708 } 709 710 static int create_map_timer(void) 711 { 712 struct bpf_create_map_attr attr = { 713 .name = "test_map", 714 .map_type = BPF_MAP_TYPE_ARRAY, 715 .key_size = 4, 716 .value_size = 16, 717 .max_entries = 1, 718 .btf_key_type_id = 1, 719 .btf_value_type_id = 5, 720 }; 721 int fd, btf_fd; 722 723 btf_fd = load_btf(); 724 if (btf_fd < 0) 725 return -1; 726 attr.btf_fd = btf_fd; 727 fd = bpf_create_map_xattr(&attr); 728 if (fd < 0) 729 printf("Failed to create map with timer\n"); 730 return fd; 731 } 732 733 static char bpf_vlog[UINT_MAX >> 8]; 734 735 static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type, 736 struct bpf_insn *prog, int *map_fds) 737 { 738 int *fixup_map_hash_8b = test->fixup_map_hash_8b; 739 int *fixup_map_hash_48b = test->fixup_map_hash_48b; 740 int *fixup_map_hash_16b = test->fixup_map_hash_16b; 741 int *fixup_map_array_48b = test->fixup_map_array_48b; 742 int *fixup_map_sockmap = test->fixup_map_sockmap; 743 int *fixup_map_sockhash = test->fixup_map_sockhash; 744 int *fixup_map_xskmap = test->fixup_map_xskmap; 745 int *fixup_map_stacktrace = test->fixup_map_stacktrace; 746 int *fixup_prog1 = test->fixup_prog1; 747 int *fixup_prog2 = test->fixup_prog2; 748 int *fixup_map_in_map = test->fixup_map_in_map; 749 int *fixup_cgroup_storage = test->fixup_cgroup_storage; 750 int *fixup_percpu_cgroup_storage = test->fixup_percpu_cgroup_storage; 751 int *fixup_map_spin_lock = test->fixup_map_spin_lock; 752 int *fixup_map_array_ro = test->fixup_map_array_ro; 753 int *fixup_map_array_wo = test->fixup_map_array_wo; 754 int *fixup_map_array_small = test->fixup_map_array_small; 755 int *fixup_sk_storage_map = test->fixup_sk_storage_map; 756 int *fixup_map_event_output = test->fixup_map_event_output; 757 int *fixup_map_reuseport_array = test->fixup_map_reuseport_array; 758 int *fixup_map_ringbuf = test->fixup_map_ringbuf; 759 int *fixup_map_timer = test->fixup_map_timer; 760 761 if (test->fill_helper) { 762 test->fill_insns = calloc(MAX_TEST_INSNS, sizeof(struct bpf_insn)); 763 test->fill_helper(test); 764 } 765 766 /* Allocating HTs with 1 elem is fine here, since we only test 767 * for verifier and not do a runtime lookup, so the only thing 768 * that really matters is value size in this case. 769 */ 770 if (*fixup_map_hash_8b) { 771 map_fds[0] = create_map(BPF_MAP_TYPE_HASH, sizeof(long long), 772 sizeof(long long), 1); 773 do { 774 prog[*fixup_map_hash_8b].imm = map_fds[0]; 775 fixup_map_hash_8b++; 776 } while (*fixup_map_hash_8b); 777 } 778 779 if (*fixup_map_hash_48b) { 780 map_fds[1] = create_map(BPF_MAP_TYPE_HASH, sizeof(long long), 781 sizeof(struct test_val), 1); 782 do { 783 prog[*fixup_map_hash_48b].imm = map_fds[1]; 784 fixup_map_hash_48b++; 785 } while (*fixup_map_hash_48b); 786 } 787 788 if (*fixup_map_hash_16b) { 789 map_fds[2] = create_map(BPF_MAP_TYPE_HASH, sizeof(long long), 790 sizeof(struct other_val), 1); 791 do { 792 prog[*fixup_map_hash_16b].imm = map_fds[2]; 793 fixup_map_hash_16b++; 794 } while (*fixup_map_hash_16b); 795 } 796 797 if (*fixup_map_array_48b) { 798 map_fds[3] = create_map(BPF_MAP_TYPE_ARRAY, sizeof(int), 799 sizeof(struct test_val), 1); 800 update_map(map_fds[3], 0); 801 do { 802 prog[*fixup_map_array_48b].imm = map_fds[3]; 803 fixup_map_array_48b++; 804 } while (*fixup_map_array_48b); 805 } 806 807 if (*fixup_prog1) { 808 map_fds[4] = create_prog_array(prog_type, 4, 0, 1, 2); 809 do { 810 prog[*fixup_prog1].imm = map_fds[4]; 811 fixup_prog1++; 812 } while (*fixup_prog1); 813 } 814 815 if (*fixup_prog2) { 816 map_fds[5] = create_prog_array(prog_type, 8, 7, 1, 2); 817 do { 818 prog[*fixup_prog2].imm = map_fds[5]; 819 fixup_prog2++; 820 } while (*fixup_prog2); 821 } 822 823 if (*fixup_map_in_map) { 824 map_fds[6] = create_map_in_map(); 825 do { 826 prog[*fixup_map_in_map].imm = map_fds[6]; 827 fixup_map_in_map++; 828 } while (*fixup_map_in_map); 829 } 830 831 if (*fixup_cgroup_storage) { 832 map_fds[7] = create_cgroup_storage(false); 833 do { 834 prog[*fixup_cgroup_storage].imm = map_fds[7]; 835 fixup_cgroup_storage++; 836 } while (*fixup_cgroup_storage); 837 } 838 839 if (*fixup_percpu_cgroup_storage) { 840 map_fds[8] = create_cgroup_storage(true); 841 do { 842 prog[*fixup_percpu_cgroup_storage].imm = map_fds[8]; 843 fixup_percpu_cgroup_storage++; 844 } while (*fixup_percpu_cgroup_storage); 845 } 846 if (*fixup_map_sockmap) { 847 map_fds[9] = create_map(BPF_MAP_TYPE_SOCKMAP, sizeof(int), 848 sizeof(int), 1); 849 do { 850 prog[*fixup_map_sockmap].imm = map_fds[9]; 851 fixup_map_sockmap++; 852 } while (*fixup_map_sockmap); 853 } 854 if (*fixup_map_sockhash) { 855 map_fds[10] = create_map(BPF_MAP_TYPE_SOCKHASH, sizeof(int), 856 sizeof(int), 1); 857 do { 858 prog[*fixup_map_sockhash].imm = map_fds[10]; 859 fixup_map_sockhash++; 860 } while (*fixup_map_sockhash); 861 } 862 if (*fixup_map_xskmap) { 863 map_fds[11] = create_map(BPF_MAP_TYPE_XSKMAP, sizeof(int), 864 sizeof(int), 1); 865 do { 866 prog[*fixup_map_xskmap].imm = map_fds[11]; 867 fixup_map_xskmap++; 868 } while (*fixup_map_xskmap); 869 } 870 if (*fixup_map_stacktrace) { 871 map_fds[12] = create_map(BPF_MAP_TYPE_STACK_TRACE, sizeof(u32), 872 sizeof(u64), 1); 873 do { 874 prog[*fixup_map_stacktrace].imm = map_fds[12]; 875 fixup_map_stacktrace++; 876 } while (*fixup_map_stacktrace); 877 } 878 if (*fixup_map_spin_lock) { 879 map_fds[13] = create_map_spin_lock(); 880 do { 881 prog[*fixup_map_spin_lock].imm = map_fds[13]; 882 fixup_map_spin_lock++; 883 } while (*fixup_map_spin_lock); 884 } 885 if (*fixup_map_array_ro) { 886 map_fds[14] = __create_map(BPF_MAP_TYPE_ARRAY, sizeof(int), 887 sizeof(struct test_val), 1, 888 BPF_F_RDONLY_PROG); 889 update_map(map_fds[14], 0); 890 do { 891 prog[*fixup_map_array_ro].imm = map_fds[14]; 892 fixup_map_array_ro++; 893 } while (*fixup_map_array_ro); 894 } 895 if (*fixup_map_array_wo) { 896 map_fds[15] = __create_map(BPF_MAP_TYPE_ARRAY, sizeof(int), 897 sizeof(struct test_val), 1, 898 BPF_F_WRONLY_PROG); 899 update_map(map_fds[15], 0); 900 do { 901 prog[*fixup_map_array_wo].imm = map_fds[15]; 902 fixup_map_array_wo++; 903 } while (*fixup_map_array_wo); 904 } 905 if (*fixup_map_array_small) { 906 map_fds[16] = __create_map(BPF_MAP_TYPE_ARRAY, sizeof(int), 907 1, 1, 0); 908 update_map(map_fds[16], 0); 909 do { 910 prog[*fixup_map_array_small].imm = map_fds[16]; 911 fixup_map_array_small++; 912 } while (*fixup_map_array_small); 913 } 914 if (*fixup_sk_storage_map) { 915 map_fds[17] = create_sk_storage_map(); 916 do { 917 prog[*fixup_sk_storage_map].imm = map_fds[17]; 918 fixup_sk_storage_map++; 919 } while (*fixup_sk_storage_map); 920 } 921 if (*fixup_map_event_output) { 922 map_fds[18] = __create_map(BPF_MAP_TYPE_PERF_EVENT_ARRAY, 923 sizeof(int), sizeof(int), 1, 0); 924 do { 925 prog[*fixup_map_event_output].imm = map_fds[18]; 926 fixup_map_event_output++; 927 } while (*fixup_map_event_output); 928 } 929 if (*fixup_map_reuseport_array) { 930 map_fds[19] = __create_map(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, 931 sizeof(u32), sizeof(u64), 1, 0); 932 do { 933 prog[*fixup_map_reuseport_array].imm = map_fds[19]; 934 fixup_map_reuseport_array++; 935 } while (*fixup_map_reuseport_array); 936 } 937 if (*fixup_map_ringbuf) { 938 map_fds[20] = create_map(BPF_MAP_TYPE_RINGBUF, 0, 939 0, 4096); 940 do { 941 prog[*fixup_map_ringbuf].imm = map_fds[20]; 942 fixup_map_ringbuf++; 943 } while (*fixup_map_ringbuf); 944 } 945 if (*fixup_map_timer) { 946 map_fds[21] = create_map_timer(); 947 do { 948 prog[*fixup_map_timer].imm = map_fds[21]; 949 fixup_map_timer++; 950 } while (*fixup_map_timer); 951 } 952 } 953 954 struct libcap { 955 struct __user_cap_header_struct hdr; 956 struct __user_cap_data_struct data[2]; 957 }; 958 959 static int set_admin(bool admin) 960 { 961 cap_t caps; 962 /* need CAP_BPF, CAP_NET_ADMIN, CAP_PERFMON to load progs */ 963 const cap_value_t cap_net_admin = CAP_NET_ADMIN; 964 const cap_value_t cap_sys_admin = CAP_SYS_ADMIN; 965 struct libcap *cap; 966 int ret = -1; 967 968 caps = cap_get_proc(); 969 if (!caps) { 970 perror("cap_get_proc"); 971 return -1; 972 } 973 cap = (struct libcap *)caps; 974 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, &cap_sys_admin, CAP_CLEAR)) { 975 perror("cap_set_flag clear admin"); 976 goto out; 977 } 978 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, &cap_net_admin, 979 admin ? CAP_SET : CAP_CLEAR)) { 980 perror("cap_set_flag set_or_clear net"); 981 goto out; 982 } 983 /* libcap is likely old and simply ignores CAP_BPF and CAP_PERFMON, 984 * so update effective bits manually 985 */ 986 if (admin) { 987 cap->data[1].effective |= 1 << (38 /* CAP_PERFMON */ - 32); 988 cap->data[1].effective |= 1 << (39 /* CAP_BPF */ - 32); 989 } else { 990 cap->data[1].effective &= ~(1 << (38 - 32)); 991 cap->data[1].effective &= ~(1 << (39 - 32)); 992 } 993 if (cap_set_proc(caps)) { 994 perror("cap_set_proc"); 995 goto out; 996 } 997 ret = 0; 998 out: 999 if (cap_free(caps)) 1000 perror("cap_free"); 1001 return ret; 1002 } 1003 1004 static int do_prog_test_run(int fd_prog, bool unpriv, uint32_t expected_val, 1005 void *data, size_t size_data) 1006 { 1007 __u8 tmp[TEST_DATA_LEN << 2]; 1008 __u32 size_tmp = sizeof(tmp); 1009 uint32_t retval; 1010 int err, saved_errno; 1011 1012 if (unpriv) 1013 set_admin(true); 1014 err = bpf_prog_test_run(fd_prog, 1, data, size_data, 1015 tmp, &size_tmp, &retval, NULL); 1016 saved_errno = errno; 1017 1018 if (unpriv) 1019 set_admin(false); 1020 1021 if (err) { 1022 switch (saved_errno) { 1023 case ENOTSUPP: 1024 printf("Did not run the program (not supported) "); 1025 return 0; 1026 case EPERM: 1027 if (unpriv) { 1028 printf("Did not run the program (no permission) "); 1029 return 0; 1030 } 1031 /* fallthrough; */ 1032 default: 1033 printf("FAIL: Unexpected bpf_prog_test_run error (%s) ", 1034 strerror(saved_errno)); 1035 return err; 1036 } 1037 } 1038 1039 if (retval != expected_val && 1040 expected_val != POINTER_VALUE) { 1041 printf("FAIL retval %d != %d ", retval, expected_val); 1042 return 1; 1043 } 1044 1045 return 0; 1046 } 1047 1048 /* Returns true if every part of exp (tab-separated) appears in log, in order. 1049 * 1050 * If exp is an empty string, returns true. 1051 */ 1052 static bool cmp_str_seq(const char *log, const char *exp) 1053 { 1054 char needle[200]; 1055 const char *p, *q; 1056 int len; 1057 1058 do { 1059 if (!strlen(exp)) 1060 break; 1061 p = strchr(exp, '\t'); 1062 if (!p) 1063 p = exp + strlen(exp); 1064 1065 len = p - exp; 1066 if (len >= sizeof(needle) || !len) { 1067 printf("FAIL\nTestcase bug\n"); 1068 return false; 1069 } 1070 strncpy(needle, exp, len); 1071 needle[len] = 0; 1072 q = strstr(log, needle); 1073 if (!q) { 1074 printf("FAIL\nUnexpected verifier log!\n" 1075 "EXP: %s\nRES:\n", needle); 1076 return false; 1077 } 1078 log = q + len; 1079 exp = p + 1; 1080 } while (*p); 1081 return true; 1082 } 1083 1084 static void do_test_single(struct bpf_test *test, bool unpriv, 1085 int *passes, int *errors) 1086 { 1087 int fd_prog, expected_ret, alignment_prevented_execution; 1088 int prog_len, prog_type = test->prog_type; 1089 struct bpf_insn *prog = test->insns; 1090 LIBBPF_OPTS(bpf_prog_load_opts, opts); 1091 int run_errs, run_successes; 1092 int map_fds[MAX_NR_MAPS]; 1093 const char *expected_err; 1094 int saved_errno; 1095 int fixup_skips; 1096 __u32 pflags; 1097 int i, err; 1098 1099 for (i = 0; i < MAX_NR_MAPS; i++) 1100 map_fds[i] = -1; 1101 1102 if (!prog_type) 1103 prog_type = BPF_PROG_TYPE_SOCKET_FILTER; 1104 fixup_skips = skips; 1105 do_test_fixup(test, prog_type, prog, map_fds); 1106 if (test->fill_insns) { 1107 prog = test->fill_insns; 1108 prog_len = test->prog_len; 1109 } else { 1110 prog_len = probe_filter_length(prog); 1111 } 1112 /* If there were some map skips during fixup due to missing bpf 1113 * features, skip this test. 1114 */ 1115 if (fixup_skips != skips) 1116 return; 1117 1118 pflags = BPF_F_TEST_RND_HI32; 1119 if (test->flags & F_LOAD_WITH_STRICT_ALIGNMENT) 1120 pflags |= BPF_F_STRICT_ALIGNMENT; 1121 if (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS) 1122 pflags |= BPF_F_ANY_ALIGNMENT; 1123 if (test->flags & ~3) 1124 pflags |= test->flags; 1125 1126 expected_ret = unpriv && test->result_unpriv != UNDEF ? 1127 test->result_unpriv : test->result; 1128 expected_err = unpriv && test->errstr_unpriv ? 1129 test->errstr_unpriv : test->errstr; 1130 1131 opts.expected_attach_type = test->expected_attach_type; 1132 if (verbose) 1133 opts.log_level = 1; 1134 else if (expected_ret == VERBOSE_ACCEPT) 1135 opts.log_level = 2; 1136 else 1137 opts.log_level = 4; 1138 opts.prog_flags = pflags; 1139 1140 if (prog_type == BPF_PROG_TYPE_TRACING && test->kfunc) { 1141 int attach_btf_id; 1142 1143 attach_btf_id = libbpf_find_vmlinux_btf_id(test->kfunc, 1144 opts.expected_attach_type); 1145 if (attach_btf_id < 0) { 1146 printf("FAIL\nFailed to find BTF ID for '%s'!\n", 1147 test->kfunc); 1148 (*errors)++; 1149 return; 1150 } 1151 1152 opts.attach_btf_id = attach_btf_id; 1153 } 1154 1155 opts.log_buf = bpf_vlog; 1156 opts.log_size = sizeof(bpf_vlog); 1157 fd_prog = bpf_prog_load(prog_type, NULL, "GPL", prog, prog_len, &opts); 1158 saved_errno = errno; 1159 1160 /* BPF_PROG_TYPE_TRACING requires more setup and 1161 * bpf_probe_prog_type won't give correct answer 1162 */ 1163 if (fd_prog < 0 && prog_type != BPF_PROG_TYPE_TRACING && 1164 !bpf_probe_prog_type(prog_type, 0)) { 1165 printf("SKIP (unsupported program type %d)\n", prog_type); 1166 skips++; 1167 goto close_fds; 1168 } 1169 1170 if (fd_prog < 0 && saved_errno == ENOTSUPP) { 1171 printf("SKIP (program uses an unsupported feature)\n"); 1172 skips++; 1173 goto close_fds; 1174 } 1175 1176 alignment_prevented_execution = 0; 1177 1178 if (expected_ret == ACCEPT || expected_ret == VERBOSE_ACCEPT) { 1179 if (fd_prog < 0) { 1180 printf("FAIL\nFailed to load prog '%s'!\n", 1181 strerror(saved_errno)); 1182 goto fail_log; 1183 } 1184 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 1185 if (fd_prog >= 0 && 1186 (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS)) 1187 alignment_prevented_execution = 1; 1188 #endif 1189 if (expected_ret == VERBOSE_ACCEPT && !cmp_str_seq(bpf_vlog, expected_err)) { 1190 goto fail_log; 1191 } 1192 } else { 1193 if (fd_prog >= 0) { 1194 printf("FAIL\nUnexpected success to load!\n"); 1195 goto fail_log; 1196 } 1197 if (!expected_err || !cmp_str_seq(bpf_vlog, expected_err)) { 1198 printf("FAIL\nUnexpected error message!\n\tEXP: %s\n\tRES: %s\n", 1199 expected_err, bpf_vlog); 1200 goto fail_log; 1201 } 1202 } 1203 1204 if (!unpriv && test->insn_processed) { 1205 uint32_t insn_processed; 1206 char *proc; 1207 1208 proc = strstr(bpf_vlog, "processed "); 1209 insn_processed = atoi(proc + 10); 1210 if (test->insn_processed != insn_processed) { 1211 printf("FAIL\nUnexpected insn_processed %u vs %u\n", 1212 insn_processed, test->insn_processed); 1213 goto fail_log; 1214 } 1215 } 1216 1217 if (verbose) 1218 printf(", verifier log:\n%s", bpf_vlog); 1219 1220 run_errs = 0; 1221 run_successes = 0; 1222 if (!alignment_prevented_execution && fd_prog >= 0 && test->runs >= 0) { 1223 uint32_t expected_val; 1224 int i; 1225 1226 if (!test->runs) 1227 test->runs = 1; 1228 1229 for (i = 0; i < test->runs; i++) { 1230 if (unpriv && test->retvals[i].retval_unpriv) 1231 expected_val = test->retvals[i].retval_unpriv; 1232 else 1233 expected_val = test->retvals[i].retval; 1234 1235 err = do_prog_test_run(fd_prog, unpriv, expected_val, 1236 test->retvals[i].data, 1237 sizeof(test->retvals[i].data)); 1238 if (err) { 1239 printf("(run %d/%d) ", i + 1, test->runs); 1240 run_errs++; 1241 } else { 1242 run_successes++; 1243 } 1244 } 1245 } 1246 1247 if (!run_errs) { 1248 (*passes)++; 1249 if (run_successes > 1) 1250 printf("%d cases ", run_successes); 1251 printf("OK"); 1252 if (alignment_prevented_execution) 1253 printf(" (NOTE: not executed due to unknown alignment)"); 1254 printf("\n"); 1255 } else { 1256 printf("\n"); 1257 goto fail_log; 1258 } 1259 close_fds: 1260 if (test->fill_insns) 1261 free(test->fill_insns); 1262 close(fd_prog); 1263 for (i = 0; i < MAX_NR_MAPS; i++) 1264 close(map_fds[i]); 1265 sched_yield(); 1266 return; 1267 fail_log: 1268 (*errors)++; 1269 printf("%s", bpf_vlog); 1270 goto close_fds; 1271 } 1272 1273 static bool is_admin(void) 1274 { 1275 cap_flag_value_t net_priv = CAP_CLEAR; 1276 bool perfmon_priv = false; 1277 bool bpf_priv = false; 1278 struct libcap *cap; 1279 cap_t caps; 1280 1281 #ifdef CAP_IS_SUPPORTED 1282 if (!CAP_IS_SUPPORTED(CAP_SETFCAP)) { 1283 perror("cap_get_flag"); 1284 return false; 1285 } 1286 #endif 1287 caps = cap_get_proc(); 1288 if (!caps) { 1289 perror("cap_get_proc"); 1290 return false; 1291 } 1292 cap = (struct libcap *)caps; 1293 bpf_priv = cap->data[1].effective & (1 << (39/* CAP_BPF */ - 32)); 1294 perfmon_priv = cap->data[1].effective & (1 << (38/* CAP_PERFMON */ - 32)); 1295 if (cap_get_flag(caps, CAP_NET_ADMIN, CAP_EFFECTIVE, &net_priv)) 1296 perror("cap_get_flag NET"); 1297 if (cap_free(caps)) 1298 perror("cap_free"); 1299 return bpf_priv && perfmon_priv && net_priv == CAP_SET; 1300 } 1301 1302 static void get_unpriv_disabled() 1303 { 1304 char buf[2]; 1305 FILE *fd; 1306 1307 fd = fopen("/proc/sys/"UNPRIV_SYSCTL, "r"); 1308 if (!fd) { 1309 perror("fopen /proc/sys/"UNPRIV_SYSCTL); 1310 unpriv_disabled = true; 1311 return; 1312 } 1313 if (fgets(buf, 2, fd) == buf && atoi(buf)) 1314 unpriv_disabled = true; 1315 fclose(fd); 1316 } 1317 1318 static bool test_as_unpriv(struct bpf_test *test) 1319 { 1320 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 1321 /* Some architectures have strict alignment requirements. In 1322 * that case, the BPF verifier detects if a program has 1323 * unaligned accesses and rejects them. A user can pass 1324 * BPF_F_ANY_ALIGNMENT to a program to override this 1325 * check. That, however, will only work when a privileged user 1326 * loads a program. An unprivileged user loading a program 1327 * with this flag will be rejected prior entering the 1328 * verifier. 1329 */ 1330 if (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS) 1331 return false; 1332 #endif 1333 return !test->prog_type || 1334 test->prog_type == BPF_PROG_TYPE_SOCKET_FILTER || 1335 test->prog_type == BPF_PROG_TYPE_CGROUP_SKB; 1336 } 1337 1338 static int do_test(bool unpriv, unsigned int from, unsigned int to) 1339 { 1340 int i, passes = 0, errors = 0; 1341 1342 for (i = from; i < to; i++) { 1343 struct bpf_test *test = &tests[i]; 1344 1345 /* Program types that are not supported by non-root we 1346 * skip right away. 1347 */ 1348 if (test_as_unpriv(test) && unpriv_disabled) { 1349 printf("#%d/u %s SKIP\n", i, test->descr); 1350 skips++; 1351 } else if (test_as_unpriv(test)) { 1352 if (!unpriv) 1353 set_admin(false); 1354 printf("#%d/u %s ", i, test->descr); 1355 do_test_single(test, true, &passes, &errors); 1356 if (!unpriv) 1357 set_admin(true); 1358 } 1359 1360 if (unpriv) { 1361 printf("#%d/p %s SKIP\n", i, test->descr); 1362 skips++; 1363 } else { 1364 printf("#%d/p %s ", i, test->descr); 1365 do_test_single(test, false, &passes, &errors); 1366 } 1367 } 1368 1369 printf("Summary: %d PASSED, %d SKIPPED, %d FAILED\n", passes, 1370 skips, errors); 1371 return errors ? EXIT_FAILURE : EXIT_SUCCESS; 1372 } 1373 1374 int main(int argc, char **argv) 1375 { 1376 unsigned int from = 0, to = ARRAY_SIZE(tests); 1377 bool unpriv = !is_admin(); 1378 int arg = 1; 1379 1380 if (argc > 1 && strcmp(argv[1], "-v") == 0) { 1381 arg++; 1382 verbose = true; 1383 argc--; 1384 } 1385 1386 if (argc == 3) { 1387 unsigned int l = atoi(argv[arg]); 1388 unsigned int u = atoi(argv[arg + 1]); 1389 1390 if (l < to && u < to) { 1391 from = l; 1392 to = u + 1; 1393 } 1394 } else if (argc == 2) { 1395 unsigned int t = atoi(argv[arg]); 1396 1397 if (t < to) { 1398 from = t; 1399 to = t + 1; 1400 } 1401 } 1402 1403 get_unpriv_disabled(); 1404 if (unpriv && unpriv_disabled) { 1405 printf("Cannot run as unprivileged user with sysctl %s.\n", 1406 UNPRIV_SYSCTL); 1407 return EXIT_FAILURE; 1408 } 1409 1410 bpf_semi_rand_init(); 1411 return do_test(unpriv, from, to); 1412 } 1413