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