1 /* 2 * Linux Socket Filter - Kernel level socket filtering 3 * 4 * Based on the design of the Berkeley Packet Filter. The new 5 * internal format has been designed by PLUMgrid: 6 * 7 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com 8 * 9 * Authors: 10 * 11 * Jay Schulist <jschlst@samba.org> 12 * Alexei Starovoitov <ast@plumgrid.com> 13 * Daniel Borkmann <dborkman@redhat.com> 14 * 15 * This program is free software; you can redistribute it and/or 16 * modify it under the terms of the GNU General Public License 17 * as published by the Free Software Foundation; either version 18 * 2 of the License, or (at your option) any later version. 19 * 20 * Andi Kleen - Fix a few bad bugs and races. 21 * Kris Katterjohn - Added many additional checks in bpf_check_classic() 22 */ 23 24 #include <linux/filter.h> 25 #include <linux/skbuff.h> 26 #include <linux/vmalloc.h> 27 #include <linux/random.h> 28 #include <linux/moduleloader.h> 29 #include <linux/bpf.h> 30 #include <linux/frame.h> 31 #include <linux/rbtree_latch.h> 32 #include <linux/kallsyms.h> 33 #include <linux/rcupdate.h> 34 #include <linux/perf_event.h> 35 36 #include <asm/unaligned.h> 37 38 /* Registers */ 39 #define BPF_R0 regs[BPF_REG_0] 40 #define BPF_R1 regs[BPF_REG_1] 41 #define BPF_R2 regs[BPF_REG_2] 42 #define BPF_R3 regs[BPF_REG_3] 43 #define BPF_R4 regs[BPF_REG_4] 44 #define BPF_R5 regs[BPF_REG_5] 45 #define BPF_R6 regs[BPF_REG_6] 46 #define BPF_R7 regs[BPF_REG_7] 47 #define BPF_R8 regs[BPF_REG_8] 48 #define BPF_R9 regs[BPF_REG_9] 49 #define BPF_R10 regs[BPF_REG_10] 50 51 /* Named registers */ 52 #define DST regs[insn->dst_reg] 53 #define SRC regs[insn->src_reg] 54 #define FP regs[BPF_REG_FP] 55 #define ARG1 regs[BPF_REG_ARG1] 56 #define CTX regs[BPF_REG_CTX] 57 #define IMM insn->imm 58 59 /* No hurry in this branch 60 * 61 * Exported for the bpf jit load helper. 62 */ 63 void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size) 64 { 65 u8 *ptr = NULL; 66 67 if (k >= SKF_NET_OFF) 68 ptr = skb_network_header(skb) + k - SKF_NET_OFF; 69 else if (k >= SKF_LL_OFF) 70 ptr = skb_mac_header(skb) + k - SKF_LL_OFF; 71 72 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb)) 73 return ptr; 74 75 return NULL; 76 } 77 78 struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags) 79 { 80 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags; 81 struct bpf_prog_aux *aux; 82 struct bpf_prog *fp; 83 84 size = round_up(size, PAGE_SIZE); 85 fp = __vmalloc(size, gfp_flags, PAGE_KERNEL); 86 if (fp == NULL) 87 return NULL; 88 89 aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags); 90 if (aux == NULL) { 91 vfree(fp); 92 return NULL; 93 } 94 95 fp->pages = size / PAGE_SIZE; 96 fp->aux = aux; 97 fp->aux->prog = fp; 98 fp->jit_requested = ebpf_jit_enabled(); 99 100 INIT_LIST_HEAD_RCU(&fp->aux->ksym_lnode); 101 102 return fp; 103 } 104 EXPORT_SYMBOL_GPL(bpf_prog_alloc); 105 106 struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size, 107 gfp_t gfp_extra_flags) 108 { 109 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags; 110 struct bpf_prog *fp; 111 u32 pages, delta; 112 int ret; 113 114 BUG_ON(fp_old == NULL); 115 116 size = round_up(size, PAGE_SIZE); 117 pages = size / PAGE_SIZE; 118 if (pages <= fp_old->pages) 119 return fp_old; 120 121 delta = pages - fp_old->pages; 122 ret = __bpf_prog_charge(fp_old->aux->user, delta); 123 if (ret) 124 return NULL; 125 126 fp = __vmalloc(size, gfp_flags, PAGE_KERNEL); 127 if (fp == NULL) { 128 __bpf_prog_uncharge(fp_old->aux->user, delta); 129 } else { 130 memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE); 131 fp->pages = pages; 132 fp->aux->prog = fp; 133 134 /* We keep fp->aux from fp_old around in the new 135 * reallocated structure. 136 */ 137 fp_old->aux = NULL; 138 __bpf_prog_free(fp_old); 139 } 140 141 return fp; 142 } 143 144 void __bpf_prog_free(struct bpf_prog *fp) 145 { 146 kfree(fp->aux); 147 vfree(fp); 148 } 149 150 int bpf_prog_calc_tag(struct bpf_prog *fp) 151 { 152 const u32 bits_offset = SHA_MESSAGE_BYTES - sizeof(__be64); 153 u32 raw_size = bpf_prog_tag_scratch_size(fp); 154 u32 digest[SHA_DIGEST_WORDS]; 155 u32 ws[SHA_WORKSPACE_WORDS]; 156 u32 i, bsize, psize, blocks; 157 struct bpf_insn *dst; 158 bool was_ld_map; 159 u8 *raw, *todo; 160 __be32 *result; 161 __be64 *bits; 162 163 raw = vmalloc(raw_size); 164 if (!raw) 165 return -ENOMEM; 166 167 sha_init(digest); 168 memset(ws, 0, sizeof(ws)); 169 170 /* We need to take out the map fd for the digest calculation 171 * since they are unstable from user space side. 172 */ 173 dst = (void *)raw; 174 for (i = 0, was_ld_map = false; i < fp->len; i++) { 175 dst[i] = fp->insnsi[i]; 176 if (!was_ld_map && 177 dst[i].code == (BPF_LD | BPF_IMM | BPF_DW) && 178 dst[i].src_reg == BPF_PSEUDO_MAP_FD) { 179 was_ld_map = true; 180 dst[i].imm = 0; 181 } else if (was_ld_map && 182 dst[i].code == 0 && 183 dst[i].dst_reg == 0 && 184 dst[i].src_reg == 0 && 185 dst[i].off == 0) { 186 was_ld_map = false; 187 dst[i].imm = 0; 188 } else { 189 was_ld_map = false; 190 } 191 } 192 193 psize = bpf_prog_insn_size(fp); 194 memset(&raw[psize], 0, raw_size - psize); 195 raw[psize++] = 0x80; 196 197 bsize = round_up(psize, SHA_MESSAGE_BYTES); 198 blocks = bsize / SHA_MESSAGE_BYTES; 199 todo = raw; 200 if (bsize - psize >= sizeof(__be64)) { 201 bits = (__be64 *)(todo + bsize - sizeof(__be64)); 202 } else { 203 bits = (__be64 *)(todo + bsize + bits_offset); 204 blocks++; 205 } 206 *bits = cpu_to_be64((psize - 1) << 3); 207 208 while (blocks--) { 209 sha_transform(digest, todo, ws); 210 todo += SHA_MESSAGE_BYTES; 211 } 212 213 result = (__force __be32 *)digest; 214 for (i = 0; i < SHA_DIGEST_WORDS; i++) 215 result[i] = cpu_to_be32(digest[i]); 216 memcpy(fp->tag, result, sizeof(fp->tag)); 217 218 vfree(raw); 219 return 0; 220 } 221 222 static int bpf_adj_delta_to_imm(struct bpf_insn *insn, u32 pos, u32 delta, 223 u32 curr, const bool probe_pass) 224 { 225 const s64 imm_min = S32_MIN, imm_max = S32_MAX; 226 s64 imm = insn->imm; 227 228 if (curr < pos && curr + imm + 1 > pos) 229 imm += delta; 230 else if (curr > pos + delta && curr + imm + 1 <= pos + delta) 231 imm -= delta; 232 if (imm < imm_min || imm > imm_max) 233 return -ERANGE; 234 if (!probe_pass) 235 insn->imm = imm; 236 return 0; 237 } 238 239 static int bpf_adj_delta_to_off(struct bpf_insn *insn, u32 pos, u32 delta, 240 u32 curr, const bool probe_pass) 241 { 242 const s32 off_min = S16_MIN, off_max = S16_MAX; 243 s32 off = insn->off; 244 245 if (curr < pos && curr + off + 1 > pos) 246 off += delta; 247 else if (curr > pos + delta && curr + off + 1 <= pos + delta) 248 off -= delta; 249 if (off < off_min || off > off_max) 250 return -ERANGE; 251 if (!probe_pass) 252 insn->off = off; 253 return 0; 254 } 255 256 static int bpf_adj_branches(struct bpf_prog *prog, u32 pos, u32 delta, 257 const bool probe_pass) 258 { 259 u32 i, insn_cnt = prog->len + (probe_pass ? delta : 0); 260 struct bpf_insn *insn = prog->insnsi; 261 int ret = 0; 262 263 for (i = 0; i < insn_cnt; i++, insn++) { 264 u8 code; 265 266 /* In the probing pass we still operate on the original, 267 * unpatched image in order to check overflows before we 268 * do any other adjustments. Therefore skip the patchlet. 269 */ 270 if (probe_pass && i == pos) { 271 i += delta + 1; 272 insn++; 273 } 274 code = insn->code; 275 if (BPF_CLASS(code) != BPF_JMP || 276 BPF_OP(code) == BPF_EXIT) 277 continue; 278 /* Adjust offset of jmps if we cross patch boundaries. */ 279 if (BPF_OP(code) == BPF_CALL) { 280 if (insn->src_reg != BPF_PSEUDO_CALL) 281 continue; 282 ret = bpf_adj_delta_to_imm(insn, pos, delta, i, 283 probe_pass); 284 } else { 285 ret = bpf_adj_delta_to_off(insn, pos, delta, i, 286 probe_pass); 287 } 288 if (ret) 289 break; 290 } 291 292 return ret; 293 } 294 295 struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off, 296 const struct bpf_insn *patch, u32 len) 297 { 298 u32 insn_adj_cnt, insn_rest, insn_delta = len - 1; 299 const u32 cnt_max = S16_MAX; 300 struct bpf_prog *prog_adj; 301 302 /* Since our patchlet doesn't expand the image, we're done. */ 303 if (insn_delta == 0) { 304 memcpy(prog->insnsi + off, patch, sizeof(*patch)); 305 return prog; 306 } 307 308 insn_adj_cnt = prog->len + insn_delta; 309 310 /* Reject anything that would potentially let the insn->off 311 * target overflow when we have excessive program expansions. 312 * We need to probe here before we do any reallocation where 313 * we afterwards may not fail anymore. 314 */ 315 if (insn_adj_cnt > cnt_max && 316 bpf_adj_branches(prog, off, insn_delta, true)) 317 return NULL; 318 319 /* Several new instructions need to be inserted. Make room 320 * for them. Likely, there's no need for a new allocation as 321 * last page could have large enough tailroom. 322 */ 323 prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt), 324 GFP_USER); 325 if (!prog_adj) 326 return NULL; 327 328 prog_adj->len = insn_adj_cnt; 329 330 /* Patching happens in 3 steps: 331 * 332 * 1) Move over tail of insnsi from next instruction onwards, 333 * so we can patch the single target insn with one or more 334 * new ones (patching is always from 1 to n insns, n > 0). 335 * 2) Inject new instructions at the target location. 336 * 3) Adjust branch offsets if necessary. 337 */ 338 insn_rest = insn_adj_cnt - off - len; 339 340 memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1, 341 sizeof(*patch) * insn_rest); 342 memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len); 343 344 /* We are guaranteed to not fail at this point, otherwise 345 * the ship has sailed to reverse to the original state. An 346 * overflow cannot happen at this point. 347 */ 348 BUG_ON(bpf_adj_branches(prog_adj, off, insn_delta, false)); 349 350 return prog_adj; 351 } 352 353 void bpf_prog_kallsyms_del_subprogs(struct bpf_prog *fp) 354 { 355 int i; 356 357 for (i = 0; i < fp->aux->func_cnt; i++) 358 bpf_prog_kallsyms_del(fp->aux->func[i]); 359 } 360 361 void bpf_prog_kallsyms_del_all(struct bpf_prog *fp) 362 { 363 bpf_prog_kallsyms_del_subprogs(fp); 364 bpf_prog_kallsyms_del(fp); 365 } 366 367 #ifdef CONFIG_BPF_JIT 368 /* All BPF JIT sysctl knobs here. */ 369 int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_ALWAYS_ON); 370 int bpf_jit_harden __read_mostly; 371 int bpf_jit_kallsyms __read_mostly; 372 373 static __always_inline void 374 bpf_get_prog_addr_region(const struct bpf_prog *prog, 375 unsigned long *symbol_start, 376 unsigned long *symbol_end) 377 { 378 const struct bpf_binary_header *hdr = bpf_jit_binary_hdr(prog); 379 unsigned long addr = (unsigned long)hdr; 380 381 WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog)); 382 383 *symbol_start = addr; 384 *symbol_end = addr + hdr->pages * PAGE_SIZE; 385 } 386 387 static void bpf_get_prog_name(const struct bpf_prog *prog, char *sym) 388 { 389 const char *end = sym + KSYM_NAME_LEN; 390 391 BUILD_BUG_ON(sizeof("bpf_prog_") + 392 sizeof(prog->tag) * 2 + 393 /* name has been null terminated. 394 * We should need +1 for the '_' preceding 395 * the name. However, the null character 396 * is double counted between the name and the 397 * sizeof("bpf_prog_") above, so we omit 398 * the +1 here. 399 */ 400 sizeof(prog->aux->name) > KSYM_NAME_LEN); 401 402 sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_"); 403 sym = bin2hex(sym, prog->tag, sizeof(prog->tag)); 404 if (prog->aux->name[0]) 405 snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name); 406 else 407 *sym = 0; 408 } 409 410 static __always_inline unsigned long 411 bpf_get_prog_addr_start(struct latch_tree_node *n) 412 { 413 unsigned long symbol_start, symbol_end; 414 const struct bpf_prog_aux *aux; 415 416 aux = container_of(n, struct bpf_prog_aux, ksym_tnode); 417 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end); 418 419 return symbol_start; 420 } 421 422 static __always_inline bool bpf_tree_less(struct latch_tree_node *a, 423 struct latch_tree_node *b) 424 { 425 return bpf_get_prog_addr_start(a) < bpf_get_prog_addr_start(b); 426 } 427 428 static __always_inline int bpf_tree_comp(void *key, struct latch_tree_node *n) 429 { 430 unsigned long val = (unsigned long)key; 431 unsigned long symbol_start, symbol_end; 432 const struct bpf_prog_aux *aux; 433 434 aux = container_of(n, struct bpf_prog_aux, ksym_tnode); 435 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end); 436 437 if (val < symbol_start) 438 return -1; 439 if (val >= symbol_end) 440 return 1; 441 442 return 0; 443 } 444 445 static const struct latch_tree_ops bpf_tree_ops = { 446 .less = bpf_tree_less, 447 .comp = bpf_tree_comp, 448 }; 449 450 static DEFINE_SPINLOCK(bpf_lock); 451 static LIST_HEAD(bpf_kallsyms); 452 static struct latch_tree_root bpf_tree __cacheline_aligned; 453 454 static void bpf_prog_ksym_node_add(struct bpf_prog_aux *aux) 455 { 456 WARN_ON_ONCE(!list_empty(&aux->ksym_lnode)); 457 list_add_tail_rcu(&aux->ksym_lnode, &bpf_kallsyms); 458 latch_tree_insert(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops); 459 } 460 461 static void bpf_prog_ksym_node_del(struct bpf_prog_aux *aux) 462 { 463 if (list_empty(&aux->ksym_lnode)) 464 return; 465 466 latch_tree_erase(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops); 467 list_del_rcu(&aux->ksym_lnode); 468 } 469 470 static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp) 471 { 472 return fp->jited && !bpf_prog_was_classic(fp); 473 } 474 475 static bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp) 476 { 477 return list_empty(&fp->aux->ksym_lnode) || 478 fp->aux->ksym_lnode.prev == LIST_POISON2; 479 } 480 481 void bpf_prog_kallsyms_add(struct bpf_prog *fp) 482 { 483 if (!bpf_prog_kallsyms_candidate(fp) || 484 !capable(CAP_SYS_ADMIN)) 485 return; 486 487 spin_lock_bh(&bpf_lock); 488 bpf_prog_ksym_node_add(fp->aux); 489 spin_unlock_bh(&bpf_lock); 490 } 491 492 void bpf_prog_kallsyms_del(struct bpf_prog *fp) 493 { 494 if (!bpf_prog_kallsyms_candidate(fp)) 495 return; 496 497 spin_lock_bh(&bpf_lock); 498 bpf_prog_ksym_node_del(fp->aux); 499 spin_unlock_bh(&bpf_lock); 500 } 501 502 static struct bpf_prog *bpf_prog_kallsyms_find(unsigned long addr) 503 { 504 struct latch_tree_node *n; 505 506 if (!bpf_jit_kallsyms_enabled()) 507 return NULL; 508 509 n = latch_tree_find((void *)addr, &bpf_tree, &bpf_tree_ops); 510 return n ? 511 container_of(n, struct bpf_prog_aux, ksym_tnode)->prog : 512 NULL; 513 } 514 515 const char *__bpf_address_lookup(unsigned long addr, unsigned long *size, 516 unsigned long *off, char *sym) 517 { 518 unsigned long symbol_start, symbol_end; 519 struct bpf_prog *prog; 520 char *ret = NULL; 521 522 rcu_read_lock(); 523 prog = bpf_prog_kallsyms_find(addr); 524 if (prog) { 525 bpf_get_prog_addr_region(prog, &symbol_start, &symbol_end); 526 bpf_get_prog_name(prog, sym); 527 528 ret = sym; 529 if (size) 530 *size = symbol_end - symbol_start; 531 if (off) 532 *off = addr - symbol_start; 533 } 534 rcu_read_unlock(); 535 536 return ret; 537 } 538 539 bool is_bpf_text_address(unsigned long addr) 540 { 541 bool ret; 542 543 rcu_read_lock(); 544 ret = bpf_prog_kallsyms_find(addr) != NULL; 545 rcu_read_unlock(); 546 547 return ret; 548 } 549 550 int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type, 551 char *sym) 552 { 553 unsigned long symbol_start, symbol_end; 554 struct bpf_prog_aux *aux; 555 unsigned int it = 0; 556 int ret = -ERANGE; 557 558 if (!bpf_jit_kallsyms_enabled()) 559 return ret; 560 561 rcu_read_lock(); 562 list_for_each_entry_rcu(aux, &bpf_kallsyms, ksym_lnode) { 563 if (it++ != symnum) 564 continue; 565 566 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end); 567 bpf_get_prog_name(aux->prog, sym); 568 569 *value = symbol_start; 570 *type = BPF_SYM_ELF_TYPE; 571 572 ret = 0; 573 break; 574 } 575 rcu_read_unlock(); 576 577 return ret; 578 } 579 580 struct bpf_binary_header * 581 bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr, 582 unsigned int alignment, 583 bpf_jit_fill_hole_t bpf_fill_ill_insns) 584 { 585 struct bpf_binary_header *hdr; 586 unsigned int size, hole, start; 587 588 /* Most of BPF filters are really small, but if some of them 589 * fill a page, allow at least 128 extra bytes to insert a 590 * random section of illegal instructions. 591 */ 592 size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE); 593 hdr = module_alloc(size); 594 if (hdr == NULL) 595 return NULL; 596 597 /* Fill space with illegal/arch-dep instructions. */ 598 bpf_fill_ill_insns(hdr, size); 599 600 hdr->pages = size / PAGE_SIZE; 601 hdr->locked = 0; 602 603 hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)), 604 PAGE_SIZE - sizeof(*hdr)); 605 start = (get_random_int() % hole) & ~(alignment - 1); 606 607 /* Leave a random number of instructions before BPF code. */ 608 *image_ptr = &hdr->image[start]; 609 610 return hdr; 611 } 612 613 void bpf_jit_binary_free(struct bpf_binary_header *hdr) 614 { 615 module_memfree(hdr); 616 } 617 618 /* This symbol is only overridden by archs that have different 619 * requirements than the usual eBPF JITs, f.e. when they only 620 * implement cBPF JIT, do not set images read-only, etc. 621 */ 622 void __weak bpf_jit_free(struct bpf_prog *fp) 623 { 624 if (fp->jited) { 625 struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp); 626 627 bpf_jit_binary_unlock_ro(hdr); 628 bpf_jit_binary_free(hdr); 629 630 WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp)); 631 } 632 633 bpf_prog_unlock_free(fp); 634 } 635 636 static int bpf_jit_blind_insn(const struct bpf_insn *from, 637 const struct bpf_insn *aux, 638 struct bpf_insn *to_buff) 639 { 640 struct bpf_insn *to = to_buff; 641 u32 imm_rnd = get_random_int(); 642 s16 off; 643 644 BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG); 645 BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG); 646 647 if (from->imm == 0 && 648 (from->code == (BPF_ALU | BPF_MOV | BPF_K) || 649 from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) { 650 *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg); 651 goto out; 652 } 653 654 switch (from->code) { 655 case BPF_ALU | BPF_ADD | BPF_K: 656 case BPF_ALU | BPF_SUB | BPF_K: 657 case BPF_ALU | BPF_AND | BPF_K: 658 case BPF_ALU | BPF_OR | BPF_K: 659 case BPF_ALU | BPF_XOR | BPF_K: 660 case BPF_ALU | BPF_MUL | BPF_K: 661 case BPF_ALU | BPF_MOV | BPF_K: 662 case BPF_ALU | BPF_DIV | BPF_K: 663 case BPF_ALU | BPF_MOD | BPF_K: 664 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); 665 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); 666 *to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX); 667 break; 668 669 case BPF_ALU64 | BPF_ADD | BPF_K: 670 case BPF_ALU64 | BPF_SUB | BPF_K: 671 case BPF_ALU64 | BPF_AND | BPF_K: 672 case BPF_ALU64 | BPF_OR | BPF_K: 673 case BPF_ALU64 | BPF_XOR | BPF_K: 674 case BPF_ALU64 | BPF_MUL | BPF_K: 675 case BPF_ALU64 | BPF_MOV | BPF_K: 676 case BPF_ALU64 | BPF_DIV | BPF_K: 677 case BPF_ALU64 | BPF_MOD | BPF_K: 678 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); 679 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); 680 *to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX); 681 break; 682 683 case BPF_JMP | BPF_JEQ | BPF_K: 684 case BPF_JMP | BPF_JNE | BPF_K: 685 case BPF_JMP | BPF_JGT | BPF_K: 686 case BPF_JMP | BPF_JLT | BPF_K: 687 case BPF_JMP | BPF_JGE | BPF_K: 688 case BPF_JMP | BPF_JLE | BPF_K: 689 case BPF_JMP | BPF_JSGT | BPF_K: 690 case BPF_JMP | BPF_JSLT | BPF_K: 691 case BPF_JMP | BPF_JSGE | BPF_K: 692 case BPF_JMP | BPF_JSLE | BPF_K: 693 case BPF_JMP | BPF_JSET | BPF_K: 694 /* Accommodate for extra offset in case of a backjump. */ 695 off = from->off; 696 if (off < 0) 697 off -= 2; 698 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); 699 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); 700 *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off); 701 break; 702 703 case BPF_LD | BPF_IMM | BPF_DW: 704 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm); 705 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); 706 *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); 707 *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX); 708 break; 709 case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */ 710 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm); 711 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); 712 *to++ = BPF_ALU64_REG(BPF_OR, aux[0].dst_reg, BPF_REG_AX); 713 break; 714 715 case BPF_ST | BPF_MEM | BPF_DW: 716 case BPF_ST | BPF_MEM | BPF_W: 717 case BPF_ST | BPF_MEM | BPF_H: 718 case BPF_ST | BPF_MEM | BPF_B: 719 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); 720 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); 721 *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off); 722 break; 723 } 724 out: 725 return to - to_buff; 726 } 727 728 static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other, 729 gfp_t gfp_extra_flags) 730 { 731 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags; 732 struct bpf_prog *fp; 733 734 fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags, PAGE_KERNEL); 735 if (fp != NULL) { 736 /* aux->prog still points to the fp_other one, so 737 * when promoting the clone to the real program, 738 * this still needs to be adapted. 739 */ 740 memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE); 741 } 742 743 return fp; 744 } 745 746 static void bpf_prog_clone_free(struct bpf_prog *fp) 747 { 748 /* aux was stolen by the other clone, so we cannot free 749 * it from this path! It will be freed eventually by the 750 * other program on release. 751 * 752 * At this point, we don't need a deferred release since 753 * clone is guaranteed to not be locked. 754 */ 755 fp->aux = NULL; 756 __bpf_prog_free(fp); 757 } 758 759 void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other) 760 { 761 /* We have to repoint aux->prog to self, as we don't 762 * know whether fp here is the clone or the original. 763 */ 764 fp->aux->prog = fp; 765 bpf_prog_clone_free(fp_other); 766 } 767 768 struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog) 769 { 770 struct bpf_insn insn_buff[16], aux[2]; 771 struct bpf_prog *clone, *tmp; 772 int insn_delta, insn_cnt; 773 struct bpf_insn *insn; 774 int i, rewritten; 775 776 if (!bpf_jit_blinding_enabled(prog) || prog->blinded) 777 return prog; 778 779 clone = bpf_prog_clone_create(prog, GFP_USER); 780 if (!clone) 781 return ERR_PTR(-ENOMEM); 782 783 insn_cnt = clone->len; 784 insn = clone->insnsi; 785 786 for (i = 0; i < insn_cnt; i++, insn++) { 787 /* We temporarily need to hold the original ld64 insn 788 * so that we can still access the first part in the 789 * second blinding run. 790 */ 791 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) && 792 insn[1].code == 0) 793 memcpy(aux, insn, sizeof(aux)); 794 795 rewritten = bpf_jit_blind_insn(insn, aux, insn_buff); 796 if (!rewritten) 797 continue; 798 799 tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten); 800 if (!tmp) { 801 /* Patching may have repointed aux->prog during 802 * realloc from the original one, so we need to 803 * fix it up here on error. 804 */ 805 bpf_jit_prog_release_other(prog, clone); 806 return ERR_PTR(-ENOMEM); 807 } 808 809 clone = tmp; 810 insn_delta = rewritten - 1; 811 812 /* Walk new program and skip insns we just inserted. */ 813 insn = clone->insnsi + i + insn_delta; 814 insn_cnt += insn_delta; 815 i += insn_delta; 816 } 817 818 clone->blinded = 1; 819 return clone; 820 } 821 #endif /* CONFIG_BPF_JIT */ 822 823 /* Base function for offset calculation. Needs to go into .text section, 824 * therefore keeping it non-static as well; will also be used by JITs 825 * anyway later on, so do not let the compiler omit it. This also needs 826 * to go into kallsyms for correlation from e.g. bpftool, so naming 827 * must not change. 828 */ 829 noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) 830 { 831 return 0; 832 } 833 EXPORT_SYMBOL_GPL(__bpf_call_base); 834 835 /* All UAPI available opcodes. */ 836 #define BPF_INSN_MAP(INSN_2, INSN_3) \ 837 /* 32 bit ALU operations. */ \ 838 /* Register based. */ \ 839 INSN_3(ALU, ADD, X), \ 840 INSN_3(ALU, SUB, X), \ 841 INSN_3(ALU, AND, X), \ 842 INSN_3(ALU, OR, X), \ 843 INSN_3(ALU, LSH, X), \ 844 INSN_3(ALU, RSH, X), \ 845 INSN_3(ALU, XOR, X), \ 846 INSN_3(ALU, MUL, X), \ 847 INSN_3(ALU, MOV, X), \ 848 INSN_3(ALU, DIV, X), \ 849 INSN_3(ALU, MOD, X), \ 850 INSN_2(ALU, NEG), \ 851 INSN_3(ALU, END, TO_BE), \ 852 INSN_3(ALU, END, TO_LE), \ 853 /* Immediate based. */ \ 854 INSN_3(ALU, ADD, K), \ 855 INSN_3(ALU, SUB, K), \ 856 INSN_3(ALU, AND, K), \ 857 INSN_3(ALU, OR, K), \ 858 INSN_3(ALU, LSH, K), \ 859 INSN_3(ALU, RSH, K), \ 860 INSN_3(ALU, XOR, K), \ 861 INSN_3(ALU, MUL, K), \ 862 INSN_3(ALU, MOV, K), \ 863 INSN_3(ALU, DIV, K), \ 864 INSN_3(ALU, MOD, K), \ 865 /* 64 bit ALU operations. */ \ 866 /* Register based. */ \ 867 INSN_3(ALU64, ADD, X), \ 868 INSN_3(ALU64, SUB, X), \ 869 INSN_3(ALU64, AND, X), \ 870 INSN_3(ALU64, OR, X), \ 871 INSN_3(ALU64, LSH, X), \ 872 INSN_3(ALU64, RSH, X), \ 873 INSN_3(ALU64, XOR, X), \ 874 INSN_3(ALU64, MUL, X), \ 875 INSN_3(ALU64, MOV, X), \ 876 INSN_3(ALU64, ARSH, X), \ 877 INSN_3(ALU64, DIV, X), \ 878 INSN_3(ALU64, MOD, X), \ 879 INSN_2(ALU64, NEG), \ 880 /* Immediate based. */ \ 881 INSN_3(ALU64, ADD, K), \ 882 INSN_3(ALU64, SUB, K), \ 883 INSN_3(ALU64, AND, K), \ 884 INSN_3(ALU64, OR, K), \ 885 INSN_3(ALU64, LSH, K), \ 886 INSN_3(ALU64, RSH, K), \ 887 INSN_3(ALU64, XOR, K), \ 888 INSN_3(ALU64, MUL, K), \ 889 INSN_3(ALU64, MOV, K), \ 890 INSN_3(ALU64, ARSH, K), \ 891 INSN_3(ALU64, DIV, K), \ 892 INSN_3(ALU64, MOD, K), \ 893 /* Call instruction. */ \ 894 INSN_2(JMP, CALL), \ 895 /* Exit instruction. */ \ 896 INSN_2(JMP, EXIT), \ 897 /* Jump instructions. */ \ 898 /* Register based. */ \ 899 INSN_3(JMP, JEQ, X), \ 900 INSN_3(JMP, JNE, X), \ 901 INSN_3(JMP, JGT, X), \ 902 INSN_3(JMP, JLT, X), \ 903 INSN_3(JMP, JGE, X), \ 904 INSN_3(JMP, JLE, X), \ 905 INSN_3(JMP, JSGT, X), \ 906 INSN_3(JMP, JSLT, X), \ 907 INSN_3(JMP, JSGE, X), \ 908 INSN_3(JMP, JSLE, X), \ 909 INSN_3(JMP, JSET, X), \ 910 /* Immediate based. */ \ 911 INSN_3(JMP, JEQ, K), \ 912 INSN_3(JMP, JNE, K), \ 913 INSN_3(JMP, JGT, K), \ 914 INSN_3(JMP, JLT, K), \ 915 INSN_3(JMP, JGE, K), \ 916 INSN_3(JMP, JLE, K), \ 917 INSN_3(JMP, JSGT, K), \ 918 INSN_3(JMP, JSLT, K), \ 919 INSN_3(JMP, JSGE, K), \ 920 INSN_3(JMP, JSLE, K), \ 921 INSN_3(JMP, JSET, K), \ 922 INSN_2(JMP, JA), \ 923 /* Store instructions. */ \ 924 /* Register based. */ \ 925 INSN_3(STX, MEM, B), \ 926 INSN_3(STX, MEM, H), \ 927 INSN_3(STX, MEM, W), \ 928 INSN_3(STX, MEM, DW), \ 929 INSN_3(STX, XADD, W), \ 930 INSN_3(STX, XADD, DW), \ 931 /* Immediate based. */ \ 932 INSN_3(ST, MEM, B), \ 933 INSN_3(ST, MEM, H), \ 934 INSN_3(ST, MEM, W), \ 935 INSN_3(ST, MEM, DW), \ 936 /* Load instructions. */ \ 937 /* Register based. */ \ 938 INSN_3(LDX, MEM, B), \ 939 INSN_3(LDX, MEM, H), \ 940 INSN_3(LDX, MEM, W), \ 941 INSN_3(LDX, MEM, DW), \ 942 /* Immediate based. */ \ 943 INSN_3(LD, IMM, DW) 944 945 bool bpf_opcode_in_insntable(u8 code) 946 { 947 #define BPF_INSN_2_TBL(x, y) [BPF_##x | BPF_##y] = true 948 #define BPF_INSN_3_TBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = true 949 static const bool public_insntable[256] = { 950 [0 ... 255] = false, 951 /* Now overwrite non-defaults ... */ 952 BPF_INSN_MAP(BPF_INSN_2_TBL, BPF_INSN_3_TBL), 953 /* UAPI exposed, but rewritten opcodes. cBPF carry-over. */ 954 [BPF_LD | BPF_ABS | BPF_B] = true, 955 [BPF_LD | BPF_ABS | BPF_H] = true, 956 [BPF_LD | BPF_ABS | BPF_W] = true, 957 [BPF_LD | BPF_IND | BPF_B] = true, 958 [BPF_LD | BPF_IND | BPF_H] = true, 959 [BPF_LD | BPF_IND | BPF_W] = true, 960 }; 961 #undef BPF_INSN_3_TBL 962 #undef BPF_INSN_2_TBL 963 return public_insntable[code]; 964 } 965 966 #ifndef CONFIG_BPF_JIT_ALWAYS_ON 967 /** 968 * __bpf_prog_run - run eBPF program on a given context 969 * @ctx: is the data we are operating on 970 * @insn: is the array of eBPF instructions 971 * 972 * Decode and execute eBPF instructions. 973 */ 974 static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack) 975 { 976 u64 tmp; 977 #define BPF_INSN_2_LBL(x, y) [BPF_##x | BPF_##y] = &&x##_##y 978 #define BPF_INSN_3_LBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = &&x##_##y##_##z 979 static const void *jumptable[256] = { 980 [0 ... 255] = &&default_label, 981 /* Now overwrite non-defaults ... */ 982 BPF_INSN_MAP(BPF_INSN_2_LBL, BPF_INSN_3_LBL), 983 /* Non-UAPI available opcodes. */ 984 [BPF_JMP | BPF_CALL_ARGS] = &&JMP_CALL_ARGS, 985 [BPF_JMP | BPF_TAIL_CALL] = &&JMP_TAIL_CALL, 986 }; 987 #undef BPF_INSN_3_LBL 988 #undef BPF_INSN_2_LBL 989 u32 tail_call_cnt = 0; 990 991 #define CONT ({ insn++; goto select_insn; }) 992 #define CONT_JMP ({ insn++; goto select_insn; }) 993 994 select_insn: 995 goto *jumptable[insn->code]; 996 997 /* ALU */ 998 #define ALU(OPCODE, OP) \ 999 ALU64_##OPCODE##_X: \ 1000 DST = DST OP SRC; \ 1001 CONT; \ 1002 ALU_##OPCODE##_X: \ 1003 DST = (u32) DST OP (u32) SRC; \ 1004 CONT; \ 1005 ALU64_##OPCODE##_K: \ 1006 DST = DST OP IMM; \ 1007 CONT; \ 1008 ALU_##OPCODE##_K: \ 1009 DST = (u32) DST OP (u32) IMM; \ 1010 CONT; 1011 1012 ALU(ADD, +) 1013 ALU(SUB, -) 1014 ALU(AND, &) 1015 ALU(OR, |) 1016 ALU(LSH, <<) 1017 ALU(RSH, >>) 1018 ALU(XOR, ^) 1019 ALU(MUL, *) 1020 #undef ALU 1021 ALU_NEG: 1022 DST = (u32) -DST; 1023 CONT; 1024 ALU64_NEG: 1025 DST = -DST; 1026 CONT; 1027 ALU_MOV_X: 1028 DST = (u32) SRC; 1029 CONT; 1030 ALU_MOV_K: 1031 DST = (u32) IMM; 1032 CONT; 1033 ALU64_MOV_X: 1034 DST = SRC; 1035 CONT; 1036 ALU64_MOV_K: 1037 DST = IMM; 1038 CONT; 1039 LD_IMM_DW: 1040 DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32; 1041 insn++; 1042 CONT; 1043 ALU64_ARSH_X: 1044 (*(s64 *) &DST) >>= SRC; 1045 CONT; 1046 ALU64_ARSH_K: 1047 (*(s64 *) &DST) >>= IMM; 1048 CONT; 1049 ALU64_MOD_X: 1050 div64_u64_rem(DST, SRC, &tmp); 1051 DST = tmp; 1052 CONT; 1053 ALU_MOD_X: 1054 tmp = (u32) DST; 1055 DST = do_div(tmp, (u32) SRC); 1056 CONT; 1057 ALU64_MOD_K: 1058 div64_u64_rem(DST, IMM, &tmp); 1059 DST = tmp; 1060 CONT; 1061 ALU_MOD_K: 1062 tmp = (u32) DST; 1063 DST = do_div(tmp, (u32) IMM); 1064 CONT; 1065 ALU64_DIV_X: 1066 DST = div64_u64(DST, SRC); 1067 CONT; 1068 ALU_DIV_X: 1069 tmp = (u32) DST; 1070 do_div(tmp, (u32) SRC); 1071 DST = (u32) tmp; 1072 CONT; 1073 ALU64_DIV_K: 1074 DST = div64_u64(DST, IMM); 1075 CONT; 1076 ALU_DIV_K: 1077 tmp = (u32) DST; 1078 do_div(tmp, (u32) IMM); 1079 DST = (u32) tmp; 1080 CONT; 1081 ALU_END_TO_BE: 1082 switch (IMM) { 1083 case 16: 1084 DST = (__force u16) cpu_to_be16(DST); 1085 break; 1086 case 32: 1087 DST = (__force u32) cpu_to_be32(DST); 1088 break; 1089 case 64: 1090 DST = (__force u64) cpu_to_be64(DST); 1091 break; 1092 } 1093 CONT; 1094 ALU_END_TO_LE: 1095 switch (IMM) { 1096 case 16: 1097 DST = (__force u16) cpu_to_le16(DST); 1098 break; 1099 case 32: 1100 DST = (__force u32) cpu_to_le32(DST); 1101 break; 1102 case 64: 1103 DST = (__force u64) cpu_to_le64(DST); 1104 break; 1105 } 1106 CONT; 1107 1108 /* CALL */ 1109 JMP_CALL: 1110 /* Function call scratches BPF_R1-BPF_R5 registers, 1111 * preserves BPF_R6-BPF_R9, and stores return value 1112 * into BPF_R0. 1113 */ 1114 BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3, 1115 BPF_R4, BPF_R5); 1116 CONT; 1117 1118 JMP_CALL_ARGS: 1119 BPF_R0 = (__bpf_call_base_args + insn->imm)(BPF_R1, BPF_R2, 1120 BPF_R3, BPF_R4, 1121 BPF_R5, 1122 insn + insn->off + 1); 1123 CONT; 1124 1125 JMP_TAIL_CALL: { 1126 struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2; 1127 struct bpf_array *array = container_of(map, struct bpf_array, map); 1128 struct bpf_prog *prog; 1129 u32 index = BPF_R3; 1130 1131 if (unlikely(index >= array->map.max_entries)) 1132 goto out; 1133 if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT)) 1134 goto out; 1135 1136 tail_call_cnt++; 1137 1138 prog = READ_ONCE(array->ptrs[index]); 1139 if (!prog) 1140 goto out; 1141 1142 /* ARG1 at this point is guaranteed to point to CTX from 1143 * the verifier side due to the fact that the tail call is 1144 * handeled like a helper, that is, bpf_tail_call_proto, 1145 * where arg1_type is ARG_PTR_TO_CTX. 1146 */ 1147 insn = prog->insnsi; 1148 goto select_insn; 1149 out: 1150 CONT; 1151 } 1152 /* JMP */ 1153 JMP_JA: 1154 insn += insn->off; 1155 CONT; 1156 JMP_JEQ_X: 1157 if (DST == SRC) { 1158 insn += insn->off; 1159 CONT_JMP; 1160 } 1161 CONT; 1162 JMP_JEQ_K: 1163 if (DST == IMM) { 1164 insn += insn->off; 1165 CONT_JMP; 1166 } 1167 CONT; 1168 JMP_JNE_X: 1169 if (DST != SRC) { 1170 insn += insn->off; 1171 CONT_JMP; 1172 } 1173 CONT; 1174 JMP_JNE_K: 1175 if (DST != IMM) { 1176 insn += insn->off; 1177 CONT_JMP; 1178 } 1179 CONT; 1180 JMP_JGT_X: 1181 if (DST > SRC) { 1182 insn += insn->off; 1183 CONT_JMP; 1184 } 1185 CONT; 1186 JMP_JGT_K: 1187 if (DST > IMM) { 1188 insn += insn->off; 1189 CONT_JMP; 1190 } 1191 CONT; 1192 JMP_JLT_X: 1193 if (DST < SRC) { 1194 insn += insn->off; 1195 CONT_JMP; 1196 } 1197 CONT; 1198 JMP_JLT_K: 1199 if (DST < IMM) { 1200 insn += insn->off; 1201 CONT_JMP; 1202 } 1203 CONT; 1204 JMP_JGE_X: 1205 if (DST >= SRC) { 1206 insn += insn->off; 1207 CONT_JMP; 1208 } 1209 CONT; 1210 JMP_JGE_K: 1211 if (DST >= IMM) { 1212 insn += insn->off; 1213 CONT_JMP; 1214 } 1215 CONT; 1216 JMP_JLE_X: 1217 if (DST <= SRC) { 1218 insn += insn->off; 1219 CONT_JMP; 1220 } 1221 CONT; 1222 JMP_JLE_K: 1223 if (DST <= IMM) { 1224 insn += insn->off; 1225 CONT_JMP; 1226 } 1227 CONT; 1228 JMP_JSGT_X: 1229 if (((s64) DST) > ((s64) SRC)) { 1230 insn += insn->off; 1231 CONT_JMP; 1232 } 1233 CONT; 1234 JMP_JSGT_K: 1235 if (((s64) DST) > ((s64) IMM)) { 1236 insn += insn->off; 1237 CONT_JMP; 1238 } 1239 CONT; 1240 JMP_JSLT_X: 1241 if (((s64) DST) < ((s64) SRC)) { 1242 insn += insn->off; 1243 CONT_JMP; 1244 } 1245 CONT; 1246 JMP_JSLT_K: 1247 if (((s64) DST) < ((s64) IMM)) { 1248 insn += insn->off; 1249 CONT_JMP; 1250 } 1251 CONT; 1252 JMP_JSGE_X: 1253 if (((s64) DST) >= ((s64) SRC)) { 1254 insn += insn->off; 1255 CONT_JMP; 1256 } 1257 CONT; 1258 JMP_JSGE_K: 1259 if (((s64) DST) >= ((s64) IMM)) { 1260 insn += insn->off; 1261 CONT_JMP; 1262 } 1263 CONT; 1264 JMP_JSLE_X: 1265 if (((s64) DST) <= ((s64) SRC)) { 1266 insn += insn->off; 1267 CONT_JMP; 1268 } 1269 CONT; 1270 JMP_JSLE_K: 1271 if (((s64) DST) <= ((s64) IMM)) { 1272 insn += insn->off; 1273 CONT_JMP; 1274 } 1275 CONT; 1276 JMP_JSET_X: 1277 if (DST & SRC) { 1278 insn += insn->off; 1279 CONT_JMP; 1280 } 1281 CONT; 1282 JMP_JSET_K: 1283 if (DST & IMM) { 1284 insn += insn->off; 1285 CONT_JMP; 1286 } 1287 CONT; 1288 JMP_EXIT: 1289 return BPF_R0; 1290 1291 /* STX and ST and LDX*/ 1292 #define LDST(SIZEOP, SIZE) \ 1293 STX_MEM_##SIZEOP: \ 1294 *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \ 1295 CONT; \ 1296 ST_MEM_##SIZEOP: \ 1297 *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \ 1298 CONT; \ 1299 LDX_MEM_##SIZEOP: \ 1300 DST = *(SIZE *)(unsigned long) (SRC + insn->off); \ 1301 CONT; 1302 1303 LDST(B, u8) 1304 LDST(H, u16) 1305 LDST(W, u32) 1306 LDST(DW, u64) 1307 #undef LDST 1308 STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */ 1309 atomic_add((u32) SRC, (atomic_t *)(unsigned long) 1310 (DST + insn->off)); 1311 CONT; 1312 STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */ 1313 atomic64_add((u64) SRC, (atomic64_t *)(unsigned long) 1314 (DST + insn->off)); 1315 CONT; 1316 1317 default_label: 1318 /* If we ever reach this, we have a bug somewhere. Die hard here 1319 * instead of just returning 0; we could be somewhere in a subprog, 1320 * so execution could continue otherwise which we do /not/ want. 1321 * 1322 * Note, verifier whitelists all opcodes in bpf_opcode_in_insntable(). 1323 */ 1324 pr_warn("BPF interpreter: unknown opcode %02x\n", insn->code); 1325 BUG_ON(1); 1326 return 0; 1327 } 1328 STACK_FRAME_NON_STANDARD(___bpf_prog_run); /* jump table */ 1329 1330 #define PROG_NAME(stack_size) __bpf_prog_run##stack_size 1331 #define DEFINE_BPF_PROG_RUN(stack_size) \ 1332 static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \ 1333 { \ 1334 u64 stack[stack_size / sizeof(u64)]; \ 1335 u64 regs[MAX_BPF_REG]; \ 1336 \ 1337 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \ 1338 ARG1 = (u64) (unsigned long) ctx; \ 1339 return ___bpf_prog_run(regs, insn, stack); \ 1340 } 1341 1342 #define PROG_NAME_ARGS(stack_size) __bpf_prog_run_args##stack_size 1343 #define DEFINE_BPF_PROG_RUN_ARGS(stack_size) \ 1344 static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \ 1345 const struct bpf_insn *insn) \ 1346 { \ 1347 u64 stack[stack_size / sizeof(u64)]; \ 1348 u64 regs[MAX_BPF_REG]; \ 1349 \ 1350 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \ 1351 BPF_R1 = r1; \ 1352 BPF_R2 = r2; \ 1353 BPF_R3 = r3; \ 1354 BPF_R4 = r4; \ 1355 BPF_R5 = r5; \ 1356 return ___bpf_prog_run(regs, insn, stack); \ 1357 } 1358 1359 #define EVAL1(FN, X) FN(X) 1360 #define EVAL2(FN, X, Y...) FN(X) EVAL1(FN, Y) 1361 #define EVAL3(FN, X, Y...) FN(X) EVAL2(FN, Y) 1362 #define EVAL4(FN, X, Y...) FN(X) EVAL3(FN, Y) 1363 #define EVAL5(FN, X, Y...) FN(X) EVAL4(FN, Y) 1364 #define EVAL6(FN, X, Y...) FN(X) EVAL5(FN, Y) 1365 1366 EVAL6(DEFINE_BPF_PROG_RUN, 32, 64, 96, 128, 160, 192); 1367 EVAL6(DEFINE_BPF_PROG_RUN, 224, 256, 288, 320, 352, 384); 1368 EVAL4(DEFINE_BPF_PROG_RUN, 416, 448, 480, 512); 1369 1370 EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 32, 64, 96, 128, 160, 192); 1371 EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 224, 256, 288, 320, 352, 384); 1372 EVAL4(DEFINE_BPF_PROG_RUN_ARGS, 416, 448, 480, 512); 1373 1374 #define PROG_NAME_LIST(stack_size) PROG_NAME(stack_size), 1375 1376 static unsigned int (*interpreters[])(const void *ctx, 1377 const struct bpf_insn *insn) = { 1378 EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192) 1379 EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384) 1380 EVAL4(PROG_NAME_LIST, 416, 448, 480, 512) 1381 }; 1382 #undef PROG_NAME_LIST 1383 #define PROG_NAME_LIST(stack_size) PROG_NAME_ARGS(stack_size), 1384 static u64 (*interpreters_args[])(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, 1385 const struct bpf_insn *insn) = { 1386 EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192) 1387 EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384) 1388 EVAL4(PROG_NAME_LIST, 416, 448, 480, 512) 1389 }; 1390 #undef PROG_NAME_LIST 1391 1392 void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth) 1393 { 1394 stack_depth = max_t(u32, stack_depth, 1); 1395 insn->off = (s16) insn->imm; 1396 insn->imm = interpreters_args[(round_up(stack_depth, 32) / 32) - 1] - 1397 __bpf_call_base_args; 1398 insn->code = BPF_JMP | BPF_CALL_ARGS; 1399 } 1400 1401 #else 1402 static unsigned int __bpf_prog_ret0_warn(const void *ctx, 1403 const struct bpf_insn *insn) 1404 { 1405 /* If this handler ever gets executed, then BPF_JIT_ALWAYS_ON 1406 * is not working properly, so warn about it! 1407 */ 1408 WARN_ON_ONCE(1); 1409 return 0; 1410 } 1411 #endif 1412 1413 bool bpf_prog_array_compatible(struct bpf_array *array, 1414 const struct bpf_prog *fp) 1415 { 1416 if (fp->kprobe_override) 1417 return false; 1418 1419 if (!array->owner_prog_type) { 1420 /* There's no owner yet where we could check for 1421 * compatibility. 1422 */ 1423 array->owner_prog_type = fp->type; 1424 array->owner_jited = fp->jited; 1425 1426 return true; 1427 } 1428 1429 return array->owner_prog_type == fp->type && 1430 array->owner_jited == fp->jited; 1431 } 1432 1433 static int bpf_check_tail_call(const struct bpf_prog *fp) 1434 { 1435 struct bpf_prog_aux *aux = fp->aux; 1436 int i; 1437 1438 for (i = 0; i < aux->used_map_cnt; i++) { 1439 struct bpf_map *map = aux->used_maps[i]; 1440 struct bpf_array *array; 1441 1442 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) 1443 continue; 1444 1445 array = container_of(map, struct bpf_array, map); 1446 if (!bpf_prog_array_compatible(array, fp)) 1447 return -EINVAL; 1448 } 1449 1450 return 0; 1451 } 1452 1453 static int bpf_prog_check_pages_ro_locked(const struct bpf_prog *fp) 1454 { 1455 #ifdef CONFIG_ARCH_HAS_SET_MEMORY 1456 int i, err; 1457 1458 for (i = 0; i < fp->aux->func_cnt; i++) { 1459 err = bpf_prog_check_pages_ro_single(fp->aux->func[i]); 1460 if (err) 1461 return err; 1462 } 1463 1464 return bpf_prog_check_pages_ro_single(fp); 1465 #endif 1466 return 0; 1467 } 1468 1469 static void bpf_prog_select_func(struct bpf_prog *fp) 1470 { 1471 #ifndef CONFIG_BPF_JIT_ALWAYS_ON 1472 u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1); 1473 1474 fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1]; 1475 #else 1476 fp->bpf_func = __bpf_prog_ret0_warn; 1477 #endif 1478 } 1479 1480 /** 1481 * bpf_prog_select_runtime - select exec runtime for BPF program 1482 * @fp: bpf_prog populated with internal BPF program 1483 * @err: pointer to error variable 1484 * 1485 * Try to JIT eBPF program, if JIT is not available, use interpreter. 1486 * The BPF program will be executed via BPF_PROG_RUN() macro. 1487 */ 1488 struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err) 1489 { 1490 /* In case of BPF to BPF calls, verifier did all the prep 1491 * work with regards to JITing, etc. 1492 */ 1493 if (fp->bpf_func) 1494 goto finalize; 1495 1496 bpf_prog_select_func(fp); 1497 1498 /* eBPF JITs can rewrite the program in case constant 1499 * blinding is active. However, in case of error during 1500 * blinding, bpf_int_jit_compile() must always return a 1501 * valid program, which in this case would simply not 1502 * be JITed, but falls back to the interpreter. 1503 */ 1504 if (!bpf_prog_is_dev_bound(fp->aux)) { 1505 fp = bpf_int_jit_compile(fp); 1506 #ifdef CONFIG_BPF_JIT_ALWAYS_ON 1507 if (!fp->jited) { 1508 *err = -ENOTSUPP; 1509 return fp; 1510 } 1511 #endif 1512 } else { 1513 *err = bpf_prog_offload_compile(fp); 1514 if (*err) 1515 return fp; 1516 } 1517 1518 finalize: 1519 bpf_prog_lock_ro(fp); 1520 1521 /* The tail call compatibility check can only be done at 1522 * this late stage as we need to determine, if we deal 1523 * with JITed or non JITed program concatenations and not 1524 * all eBPF JITs might immediately support all features. 1525 */ 1526 *err = bpf_check_tail_call(fp); 1527 if (*err) 1528 return fp; 1529 1530 /* Checkpoint: at this point onwards any cBPF -> eBPF or 1531 * native eBPF program is read-only. If we failed to change 1532 * the page attributes (e.g. allocation failure from 1533 * splitting large pages), then reject the whole program 1534 * in order to guarantee not ending up with any W+X pages 1535 * from BPF side in kernel. 1536 */ 1537 *err = bpf_prog_check_pages_ro_locked(fp); 1538 return fp; 1539 } 1540 EXPORT_SYMBOL_GPL(bpf_prog_select_runtime); 1541 1542 static unsigned int __bpf_prog_ret1(const void *ctx, 1543 const struct bpf_insn *insn) 1544 { 1545 return 1; 1546 } 1547 1548 static struct bpf_prog_dummy { 1549 struct bpf_prog prog; 1550 } dummy_bpf_prog = { 1551 .prog = { 1552 .bpf_func = __bpf_prog_ret1, 1553 }, 1554 }; 1555 1556 /* to avoid allocating empty bpf_prog_array for cgroups that 1557 * don't have bpf program attached use one global 'empty_prog_array' 1558 * It will not be modified the caller of bpf_prog_array_alloc() 1559 * (since caller requested prog_cnt == 0) 1560 * that pointer should be 'freed' by bpf_prog_array_free() 1561 */ 1562 static struct { 1563 struct bpf_prog_array hdr; 1564 struct bpf_prog *null_prog; 1565 } empty_prog_array = { 1566 .null_prog = NULL, 1567 }; 1568 1569 struct bpf_prog_array __rcu *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags) 1570 { 1571 if (prog_cnt) 1572 return kzalloc(sizeof(struct bpf_prog_array) + 1573 sizeof(struct bpf_prog *) * (prog_cnt + 1), 1574 flags); 1575 1576 return &empty_prog_array.hdr; 1577 } 1578 1579 void bpf_prog_array_free(struct bpf_prog_array __rcu *progs) 1580 { 1581 if (!progs || 1582 progs == (struct bpf_prog_array __rcu *)&empty_prog_array.hdr) 1583 return; 1584 kfree_rcu(progs, rcu); 1585 } 1586 1587 int bpf_prog_array_length(struct bpf_prog_array __rcu *progs) 1588 { 1589 struct bpf_prog **prog; 1590 u32 cnt = 0; 1591 1592 rcu_read_lock(); 1593 prog = rcu_dereference(progs)->progs; 1594 for (; *prog; prog++) 1595 if (*prog != &dummy_bpf_prog.prog) 1596 cnt++; 1597 rcu_read_unlock(); 1598 return cnt; 1599 } 1600 1601 static bool bpf_prog_array_copy_core(struct bpf_prog **prog, 1602 u32 *prog_ids, 1603 u32 request_cnt) 1604 { 1605 int i = 0; 1606 1607 for (; *prog; prog++) { 1608 if (*prog == &dummy_bpf_prog.prog) 1609 continue; 1610 prog_ids[i] = (*prog)->aux->id; 1611 if (++i == request_cnt) { 1612 prog++; 1613 break; 1614 } 1615 } 1616 1617 return !!(*prog); 1618 } 1619 1620 int bpf_prog_array_copy_to_user(struct bpf_prog_array __rcu *progs, 1621 __u32 __user *prog_ids, u32 cnt) 1622 { 1623 struct bpf_prog **prog; 1624 unsigned long err = 0; 1625 bool nospc; 1626 u32 *ids; 1627 1628 /* users of this function are doing: 1629 * cnt = bpf_prog_array_length(); 1630 * if (cnt > 0) 1631 * bpf_prog_array_copy_to_user(..., cnt); 1632 * so below kcalloc doesn't need extra cnt > 0 check, but 1633 * bpf_prog_array_length() releases rcu lock and 1634 * prog array could have been swapped with empty or larger array, 1635 * so always copy 'cnt' prog_ids to the user. 1636 * In a rare race the user will see zero prog_ids 1637 */ 1638 ids = kcalloc(cnt, sizeof(u32), GFP_USER | __GFP_NOWARN); 1639 if (!ids) 1640 return -ENOMEM; 1641 rcu_read_lock(); 1642 prog = rcu_dereference(progs)->progs; 1643 nospc = bpf_prog_array_copy_core(prog, ids, cnt); 1644 rcu_read_unlock(); 1645 err = copy_to_user(prog_ids, ids, cnt * sizeof(u32)); 1646 kfree(ids); 1647 if (err) 1648 return -EFAULT; 1649 if (nospc) 1650 return -ENOSPC; 1651 return 0; 1652 } 1653 1654 void bpf_prog_array_delete_safe(struct bpf_prog_array __rcu *progs, 1655 struct bpf_prog *old_prog) 1656 { 1657 struct bpf_prog **prog = progs->progs; 1658 1659 for (; *prog; prog++) 1660 if (*prog == old_prog) { 1661 WRITE_ONCE(*prog, &dummy_bpf_prog.prog); 1662 break; 1663 } 1664 } 1665 1666 int bpf_prog_array_copy(struct bpf_prog_array __rcu *old_array, 1667 struct bpf_prog *exclude_prog, 1668 struct bpf_prog *include_prog, 1669 struct bpf_prog_array **new_array) 1670 { 1671 int new_prog_cnt, carry_prog_cnt = 0; 1672 struct bpf_prog **existing_prog; 1673 struct bpf_prog_array *array; 1674 bool found_exclude = false; 1675 int new_prog_idx = 0; 1676 1677 /* Figure out how many existing progs we need to carry over to 1678 * the new array. 1679 */ 1680 if (old_array) { 1681 existing_prog = old_array->progs; 1682 for (; *existing_prog; existing_prog++) { 1683 if (*existing_prog == exclude_prog) { 1684 found_exclude = true; 1685 continue; 1686 } 1687 if (*existing_prog != &dummy_bpf_prog.prog) 1688 carry_prog_cnt++; 1689 if (*existing_prog == include_prog) 1690 return -EEXIST; 1691 } 1692 } 1693 1694 if (exclude_prog && !found_exclude) 1695 return -ENOENT; 1696 1697 /* How many progs (not NULL) will be in the new array? */ 1698 new_prog_cnt = carry_prog_cnt; 1699 if (include_prog) 1700 new_prog_cnt += 1; 1701 1702 /* Do we have any prog (not NULL) in the new array? */ 1703 if (!new_prog_cnt) { 1704 *new_array = NULL; 1705 return 0; 1706 } 1707 1708 /* +1 as the end of prog_array is marked with NULL */ 1709 array = bpf_prog_array_alloc(new_prog_cnt + 1, GFP_KERNEL); 1710 if (!array) 1711 return -ENOMEM; 1712 1713 /* Fill in the new prog array */ 1714 if (carry_prog_cnt) { 1715 existing_prog = old_array->progs; 1716 for (; *existing_prog; existing_prog++) 1717 if (*existing_prog != exclude_prog && 1718 *existing_prog != &dummy_bpf_prog.prog) 1719 array->progs[new_prog_idx++] = *existing_prog; 1720 } 1721 if (include_prog) 1722 array->progs[new_prog_idx++] = include_prog; 1723 array->progs[new_prog_idx] = NULL; 1724 *new_array = array; 1725 return 0; 1726 } 1727 1728 int bpf_prog_array_copy_info(struct bpf_prog_array __rcu *array, 1729 u32 *prog_ids, u32 request_cnt, 1730 u32 *prog_cnt) 1731 { 1732 struct bpf_prog **prog; 1733 u32 cnt = 0; 1734 1735 if (array) 1736 cnt = bpf_prog_array_length(array); 1737 1738 *prog_cnt = cnt; 1739 1740 /* return early if user requested only program count or nothing to copy */ 1741 if (!request_cnt || !cnt) 1742 return 0; 1743 1744 /* this function is called under trace/bpf_trace.c: bpf_event_mutex */ 1745 prog = rcu_dereference_check(array, 1)->progs; 1746 return bpf_prog_array_copy_core(prog, prog_ids, request_cnt) ? -ENOSPC 1747 : 0; 1748 } 1749 1750 static void bpf_prog_free_deferred(struct work_struct *work) 1751 { 1752 struct bpf_prog_aux *aux; 1753 int i; 1754 1755 aux = container_of(work, struct bpf_prog_aux, work); 1756 if (bpf_prog_is_dev_bound(aux)) 1757 bpf_prog_offload_destroy(aux->prog); 1758 #ifdef CONFIG_PERF_EVENTS 1759 if (aux->prog->has_callchain_buf) 1760 put_callchain_buffers(); 1761 #endif 1762 for (i = 0; i < aux->func_cnt; i++) 1763 bpf_jit_free(aux->func[i]); 1764 if (aux->func_cnt) { 1765 kfree(aux->func); 1766 bpf_prog_unlock_free(aux->prog); 1767 } else { 1768 bpf_jit_free(aux->prog); 1769 } 1770 } 1771 1772 /* Free internal BPF program */ 1773 void bpf_prog_free(struct bpf_prog *fp) 1774 { 1775 struct bpf_prog_aux *aux = fp->aux; 1776 1777 INIT_WORK(&aux->work, bpf_prog_free_deferred); 1778 schedule_work(&aux->work); 1779 } 1780 EXPORT_SYMBOL_GPL(bpf_prog_free); 1781 1782 /* RNG for unpriviledged user space with separated state from prandom_u32(). */ 1783 static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state); 1784 1785 void bpf_user_rnd_init_once(void) 1786 { 1787 prandom_init_once(&bpf_user_rnd_state); 1788 } 1789 1790 BPF_CALL_0(bpf_user_rnd_u32) 1791 { 1792 /* Should someone ever have the rather unwise idea to use some 1793 * of the registers passed into this function, then note that 1794 * this function is called from native eBPF and classic-to-eBPF 1795 * transformations. Register assignments from both sides are 1796 * different, f.e. classic always sets fn(ctx, A, X) here. 1797 */ 1798 struct rnd_state *state; 1799 u32 res; 1800 1801 state = &get_cpu_var(bpf_user_rnd_state); 1802 res = prandom_u32_state(state); 1803 put_cpu_var(bpf_user_rnd_state); 1804 1805 return res; 1806 } 1807 1808 /* Weak definitions of helper functions in case we don't have bpf syscall. */ 1809 const struct bpf_func_proto bpf_map_lookup_elem_proto __weak; 1810 const struct bpf_func_proto bpf_map_update_elem_proto __weak; 1811 const struct bpf_func_proto bpf_map_delete_elem_proto __weak; 1812 1813 const struct bpf_func_proto bpf_get_prandom_u32_proto __weak; 1814 const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak; 1815 const struct bpf_func_proto bpf_get_numa_node_id_proto __weak; 1816 const struct bpf_func_proto bpf_ktime_get_ns_proto __weak; 1817 1818 const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak; 1819 const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak; 1820 const struct bpf_func_proto bpf_get_current_comm_proto __weak; 1821 const struct bpf_func_proto bpf_sock_map_update_proto __weak; 1822 const struct bpf_func_proto bpf_sock_hash_update_proto __weak; 1823 const struct bpf_func_proto bpf_get_current_cgroup_id_proto __weak; 1824 1825 const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void) 1826 { 1827 return NULL; 1828 } 1829 1830 u64 __weak 1831 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size, 1832 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy) 1833 { 1834 return -ENOTSUPP; 1835 } 1836 EXPORT_SYMBOL_GPL(bpf_event_output); 1837 1838 /* Always built-in helper functions. */ 1839 const struct bpf_func_proto bpf_tail_call_proto = { 1840 .func = NULL, 1841 .gpl_only = false, 1842 .ret_type = RET_VOID, 1843 .arg1_type = ARG_PTR_TO_CTX, 1844 .arg2_type = ARG_CONST_MAP_PTR, 1845 .arg3_type = ARG_ANYTHING, 1846 }; 1847 1848 /* Stub for JITs that only support cBPF. eBPF programs are interpreted. 1849 * It is encouraged to implement bpf_int_jit_compile() instead, so that 1850 * eBPF and implicitly also cBPF can get JITed! 1851 */ 1852 struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog) 1853 { 1854 return prog; 1855 } 1856 1857 /* Stub for JITs that support eBPF. All cBPF code gets transformed into 1858 * eBPF by the kernel and is later compiled by bpf_int_jit_compile(). 1859 */ 1860 void __weak bpf_jit_compile(struct bpf_prog *prog) 1861 { 1862 } 1863 1864 bool __weak bpf_helper_changes_pkt_data(void *func) 1865 { 1866 return false; 1867 } 1868 1869 /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call 1870 * skb_copy_bits(), so provide a weak definition of it for NET-less config. 1871 */ 1872 int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to, 1873 int len) 1874 { 1875 return -EFAULT; 1876 } 1877 1878 /* All definitions of tracepoints related to BPF. */ 1879 #define CREATE_TRACE_POINTS 1880 #include <linux/bpf_trace.h> 1881 1882 EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception); 1883