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