1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Linux Socket Filter - Kernel level socket filtering 4 * 5 * Based on the design of the Berkeley Packet Filter. The new 6 * internal format has been designed by PLUMgrid: 7 * 8 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com 9 * 10 * Authors: 11 * 12 * Jay Schulist <jschlst@samba.org> 13 * Alexei Starovoitov <ast@plumgrid.com> 14 * Daniel Borkmann <dborkman@redhat.com> 15 * 16 * Andi Kleen - Fix a few bad bugs and races. 17 * Kris Katterjohn - Added many additional checks in bpf_check_classic() 18 */ 19 20 #include <uapi/linux/btf.h> 21 #include <linux/filter.h> 22 #include <linux/skbuff.h> 23 #include <linux/vmalloc.h> 24 #include <linux/random.h> 25 #include <linux/moduleloader.h> 26 #include <linux/bpf.h> 27 #include <linux/btf.h> 28 #include <linux/objtool.h> 29 #include <linux/rbtree_latch.h> 30 #include <linux/kallsyms.h> 31 #include <linux/rcupdate.h> 32 #include <linux/perf_event.h> 33 #include <linux/extable.h> 34 #include <linux/log2.h> 35 #include <linux/bpf_verifier.h> 36 #include <linux/nodemask.h> 37 38 #include <asm/barrier.h> 39 #include <asm/unaligned.h> 40 41 /* Registers */ 42 #define BPF_R0 regs[BPF_REG_0] 43 #define BPF_R1 regs[BPF_REG_1] 44 #define BPF_R2 regs[BPF_REG_2] 45 #define BPF_R3 regs[BPF_REG_3] 46 #define BPF_R4 regs[BPF_REG_4] 47 #define BPF_R5 regs[BPF_REG_5] 48 #define BPF_R6 regs[BPF_REG_6] 49 #define BPF_R7 regs[BPF_REG_7] 50 #define BPF_R8 regs[BPF_REG_8] 51 #define BPF_R9 regs[BPF_REG_9] 52 #define BPF_R10 regs[BPF_REG_10] 53 54 /* Named registers */ 55 #define DST regs[insn->dst_reg] 56 #define SRC regs[insn->src_reg] 57 #define FP regs[BPF_REG_FP] 58 #define AX regs[BPF_REG_AX] 59 #define ARG1 regs[BPF_REG_ARG1] 60 #define CTX regs[BPF_REG_CTX] 61 #define IMM insn->imm 62 63 /* No hurry in this branch 64 * 65 * Exported for the bpf jit load helper. 66 */ 67 void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size) 68 { 69 u8 *ptr = NULL; 70 71 if (k >= SKF_NET_OFF) 72 ptr = skb_network_header(skb) + k - SKF_NET_OFF; 73 else if (k >= SKF_LL_OFF) 74 ptr = skb_mac_header(skb) + k - SKF_LL_OFF; 75 76 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb)) 77 return ptr; 78 79 return NULL; 80 } 81 82 struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flags) 83 { 84 gfp_t gfp_flags = GFP_KERNEL_ACCOUNT | __GFP_ZERO | gfp_extra_flags; 85 struct bpf_prog_aux *aux; 86 struct bpf_prog *fp; 87 88 size = round_up(size, PAGE_SIZE); 89 fp = __vmalloc(size, gfp_flags); 90 if (fp == NULL) 91 return NULL; 92 93 aux = kzalloc(sizeof(*aux), GFP_KERNEL_ACCOUNT | gfp_extra_flags); 94 if (aux == NULL) { 95 vfree(fp); 96 return NULL; 97 } 98 fp->active = alloc_percpu_gfp(int, GFP_KERNEL_ACCOUNT | gfp_extra_flags); 99 if (!fp->active) { 100 vfree(fp); 101 kfree(aux); 102 return NULL; 103 } 104 105 fp->pages = size / PAGE_SIZE; 106 fp->aux = aux; 107 fp->aux->prog = fp; 108 fp->jit_requested = ebpf_jit_enabled(); 109 fp->blinding_requested = bpf_jit_blinding_enabled(fp); 110 #ifdef CONFIG_CGROUP_BPF 111 aux->cgroup_atype = CGROUP_BPF_ATTACH_TYPE_INVALID; 112 #endif 113 114 INIT_LIST_HEAD_RCU(&fp->aux->ksym.lnode); 115 mutex_init(&fp->aux->used_maps_mutex); 116 mutex_init(&fp->aux->dst_mutex); 117 118 return fp; 119 } 120 121 struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags) 122 { 123 gfp_t gfp_flags = GFP_KERNEL_ACCOUNT | __GFP_ZERO | gfp_extra_flags; 124 struct bpf_prog *prog; 125 int cpu; 126 127 prog = bpf_prog_alloc_no_stats(size, gfp_extra_flags); 128 if (!prog) 129 return NULL; 130 131 prog->stats = alloc_percpu_gfp(struct bpf_prog_stats, gfp_flags); 132 if (!prog->stats) { 133 free_percpu(prog->active); 134 kfree(prog->aux); 135 vfree(prog); 136 return NULL; 137 } 138 139 for_each_possible_cpu(cpu) { 140 struct bpf_prog_stats *pstats; 141 142 pstats = per_cpu_ptr(prog->stats, cpu); 143 u64_stats_init(&pstats->syncp); 144 } 145 return prog; 146 } 147 EXPORT_SYMBOL_GPL(bpf_prog_alloc); 148 149 int bpf_prog_alloc_jited_linfo(struct bpf_prog *prog) 150 { 151 if (!prog->aux->nr_linfo || !prog->jit_requested) 152 return 0; 153 154 prog->aux->jited_linfo = kvcalloc(prog->aux->nr_linfo, 155 sizeof(*prog->aux->jited_linfo), 156 GFP_KERNEL_ACCOUNT | __GFP_NOWARN); 157 if (!prog->aux->jited_linfo) 158 return -ENOMEM; 159 160 return 0; 161 } 162 163 void bpf_prog_jit_attempt_done(struct bpf_prog *prog) 164 { 165 if (prog->aux->jited_linfo && 166 (!prog->jited || !prog->aux->jited_linfo[0])) { 167 kvfree(prog->aux->jited_linfo); 168 prog->aux->jited_linfo = NULL; 169 } 170 171 kfree(prog->aux->kfunc_tab); 172 prog->aux->kfunc_tab = NULL; 173 } 174 175 /* The jit engine is responsible to provide an array 176 * for insn_off to the jited_off mapping (insn_to_jit_off). 177 * 178 * The idx to this array is the insn_off. Hence, the insn_off 179 * here is relative to the prog itself instead of the main prog. 180 * This array has one entry for each xlated bpf insn. 181 * 182 * jited_off is the byte off to the end of the jited insn. 183 * 184 * Hence, with 185 * insn_start: 186 * The first bpf insn off of the prog. The insn off 187 * here is relative to the main prog. 188 * e.g. if prog is a subprog, insn_start > 0 189 * linfo_idx: 190 * The prog's idx to prog->aux->linfo and jited_linfo 191 * 192 * jited_linfo[linfo_idx] = prog->bpf_func 193 * 194 * For i > linfo_idx, 195 * 196 * jited_linfo[i] = prog->bpf_func + 197 * insn_to_jit_off[linfo[i].insn_off - insn_start - 1] 198 */ 199 void bpf_prog_fill_jited_linfo(struct bpf_prog *prog, 200 const u32 *insn_to_jit_off) 201 { 202 u32 linfo_idx, insn_start, insn_end, nr_linfo, i; 203 const struct bpf_line_info *linfo; 204 void **jited_linfo; 205 206 if (!prog->aux->jited_linfo) 207 /* Userspace did not provide linfo */ 208 return; 209 210 linfo_idx = prog->aux->linfo_idx; 211 linfo = &prog->aux->linfo[linfo_idx]; 212 insn_start = linfo[0].insn_off; 213 insn_end = insn_start + prog->len; 214 215 jited_linfo = &prog->aux->jited_linfo[linfo_idx]; 216 jited_linfo[0] = prog->bpf_func; 217 218 nr_linfo = prog->aux->nr_linfo - linfo_idx; 219 220 for (i = 1; i < nr_linfo && linfo[i].insn_off < insn_end; i++) 221 /* The verifier ensures that linfo[i].insn_off is 222 * strictly increasing 223 */ 224 jited_linfo[i] = prog->bpf_func + 225 insn_to_jit_off[linfo[i].insn_off - insn_start - 1]; 226 } 227 228 struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size, 229 gfp_t gfp_extra_flags) 230 { 231 gfp_t gfp_flags = GFP_KERNEL_ACCOUNT | __GFP_ZERO | gfp_extra_flags; 232 struct bpf_prog *fp; 233 u32 pages; 234 235 size = round_up(size, PAGE_SIZE); 236 pages = size / PAGE_SIZE; 237 if (pages <= fp_old->pages) 238 return fp_old; 239 240 fp = __vmalloc(size, gfp_flags); 241 if (fp) { 242 memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE); 243 fp->pages = pages; 244 fp->aux->prog = fp; 245 246 /* We keep fp->aux from fp_old around in the new 247 * reallocated structure. 248 */ 249 fp_old->aux = NULL; 250 fp_old->stats = NULL; 251 fp_old->active = NULL; 252 __bpf_prog_free(fp_old); 253 } 254 255 return fp; 256 } 257 258 void __bpf_prog_free(struct bpf_prog *fp) 259 { 260 if (fp->aux) { 261 mutex_destroy(&fp->aux->used_maps_mutex); 262 mutex_destroy(&fp->aux->dst_mutex); 263 kfree(fp->aux->poke_tab); 264 kfree(fp->aux); 265 } 266 free_percpu(fp->stats); 267 free_percpu(fp->active); 268 vfree(fp); 269 } 270 271 int bpf_prog_calc_tag(struct bpf_prog *fp) 272 { 273 const u32 bits_offset = SHA1_BLOCK_SIZE - sizeof(__be64); 274 u32 raw_size = bpf_prog_tag_scratch_size(fp); 275 u32 digest[SHA1_DIGEST_WORDS]; 276 u32 ws[SHA1_WORKSPACE_WORDS]; 277 u32 i, bsize, psize, blocks; 278 struct bpf_insn *dst; 279 bool was_ld_map; 280 u8 *raw, *todo; 281 __be32 *result; 282 __be64 *bits; 283 284 raw = vmalloc(raw_size); 285 if (!raw) 286 return -ENOMEM; 287 288 sha1_init(digest); 289 memset(ws, 0, sizeof(ws)); 290 291 /* We need to take out the map fd for the digest calculation 292 * since they are unstable from user space side. 293 */ 294 dst = (void *)raw; 295 for (i = 0, was_ld_map = false; i < fp->len; i++) { 296 dst[i] = fp->insnsi[i]; 297 if (!was_ld_map && 298 dst[i].code == (BPF_LD | BPF_IMM | BPF_DW) && 299 (dst[i].src_reg == BPF_PSEUDO_MAP_FD || 300 dst[i].src_reg == BPF_PSEUDO_MAP_VALUE)) { 301 was_ld_map = true; 302 dst[i].imm = 0; 303 } else if (was_ld_map && 304 dst[i].code == 0 && 305 dst[i].dst_reg == 0 && 306 dst[i].src_reg == 0 && 307 dst[i].off == 0) { 308 was_ld_map = false; 309 dst[i].imm = 0; 310 } else { 311 was_ld_map = false; 312 } 313 } 314 315 psize = bpf_prog_insn_size(fp); 316 memset(&raw[psize], 0, raw_size - psize); 317 raw[psize++] = 0x80; 318 319 bsize = round_up(psize, SHA1_BLOCK_SIZE); 320 blocks = bsize / SHA1_BLOCK_SIZE; 321 todo = raw; 322 if (bsize - psize >= sizeof(__be64)) { 323 bits = (__be64 *)(todo + bsize - sizeof(__be64)); 324 } else { 325 bits = (__be64 *)(todo + bsize + bits_offset); 326 blocks++; 327 } 328 *bits = cpu_to_be64((psize - 1) << 3); 329 330 while (blocks--) { 331 sha1_transform(digest, todo, ws); 332 todo += SHA1_BLOCK_SIZE; 333 } 334 335 result = (__force __be32 *)digest; 336 for (i = 0; i < SHA1_DIGEST_WORDS; i++) 337 result[i] = cpu_to_be32(digest[i]); 338 memcpy(fp->tag, result, sizeof(fp->tag)); 339 340 vfree(raw); 341 return 0; 342 } 343 344 static int bpf_adj_delta_to_imm(struct bpf_insn *insn, u32 pos, s32 end_old, 345 s32 end_new, s32 curr, const bool probe_pass) 346 { 347 const s64 imm_min = S32_MIN, imm_max = S32_MAX; 348 s32 delta = end_new - end_old; 349 s64 imm = insn->imm; 350 351 if (curr < pos && curr + imm + 1 >= end_old) 352 imm += delta; 353 else if (curr >= end_new && curr + imm + 1 < end_new) 354 imm -= delta; 355 if (imm < imm_min || imm > imm_max) 356 return -ERANGE; 357 if (!probe_pass) 358 insn->imm = imm; 359 return 0; 360 } 361 362 static int bpf_adj_delta_to_off(struct bpf_insn *insn, u32 pos, s32 end_old, 363 s32 end_new, s32 curr, const bool probe_pass) 364 { 365 const s32 off_min = S16_MIN, off_max = S16_MAX; 366 s32 delta = end_new - end_old; 367 s32 off = insn->off; 368 369 if (curr < pos && curr + off + 1 >= end_old) 370 off += delta; 371 else if (curr >= end_new && curr + off + 1 < end_new) 372 off -= delta; 373 if (off < off_min || off > off_max) 374 return -ERANGE; 375 if (!probe_pass) 376 insn->off = off; 377 return 0; 378 } 379 380 static int bpf_adj_branches(struct bpf_prog *prog, u32 pos, s32 end_old, 381 s32 end_new, const bool probe_pass) 382 { 383 u32 i, insn_cnt = prog->len + (probe_pass ? end_new - end_old : 0); 384 struct bpf_insn *insn = prog->insnsi; 385 int ret = 0; 386 387 for (i = 0; i < insn_cnt; i++, insn++) { 388 u8 code; 389 390 /* In the probing pass we still operate on the original, 391 * unpatched image in order to check overflows before we 392 * do any other adjustments. Therefore skip the patchlet. 393 */ 394 if (probe_pass && i == pos) { 395 i = end_new; 396 insn = prog->insnsi + end_old; 397 } 398 if (bpf_pseudo_func(insn)) { 399 ret = bpf_adj_delta_to_imm(insn, pos, end_old, 400 end_new, i, probe_pass); 401 if (ret) 402 return ret; 403 continue; 404 } 405 code = insn->code; 406 if ((BPF_CLASS(code) != BPF_JMP && 407 BPF_CLASS(code) != BPF_JMP32) || 408 BPF_OP(code) == BPF_EXIT) 409 continue; 410 /* Adjust offset of jmps if we cross patch boundaries. */ 411 if (BPF_OP(code) == BPF_CALL) { 412 if (insn->src_reg != BPF_PSEUDO_CALL) 413 continue; 414 ret = bpf_adj_delta_to_imm(insn, pos, end_old, 415 end_new, i, probe_pass); 416 } else { 417 ret = bpf_adj_delta_to_off(insn, pos, end_old, 418 end_new, i, probe_pass); 419 } 420 if (ret) 421 break; 422 } 423 424 return ret; 425 } 426 427 static void bpf_adj_linfo(struct bpf_prog *prog, u32 off, u32 delta) 428 { 429 struct bpf_line_info *linfo; 430 u32 i, nr_linfo; 431 432 nr_linfo = prog->aux->nr_linfo; 433 if (!nr_linfo || !delta) 434 return; 435 436 linfo = prog->aux->linfo; 437 438 for (i = 0; i < nr_linfo; i++) 439 if (off < linfo[i].insn_off) 440 break; 441 442 /* Push all off < linfo[i].insn_off by delta */ 443 for (; i < nr_linfo; i++) 444 linfo[i].insn_off += delta; 445 } 446 447 struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off, 448 const struct bpf_insn *patch, u32 len) 449 { 450 u32 insn_adj_cnt, insn_rest, insn_delta = len - 1; 451 const u32 cnt_max = S16_MAX; 452 struct bpf_prog *prog_adj; 453 int err; 454 455 /* Since our patchlet doesn't expand the image, we're done. */ 456 if (insn_delta == 0) { 457 memcpy(prog->insnsi + off, patch, sizeof(*patch)); 458 return prog; 459 } 460 461 insn_adj_cnt = prog->len + insn_delta; 462 463 /* Reject anything that would potentially let the insn->off 464 * target overflow when we have excessive program expansions. 465 * We need to probe here before we do any reallocation where 466 * we afterwards may not fail anymore. 467 */ 468 if (insn_adj_cnt > cnt_max && 469 (err = bpf_adj_branches(prog, off, off + 1, off + len, true))) 470 return ERR_PTR(err); 471 472 /* Several new instructions need to be inserted. Make room 473 * for them. Likely, there's no need for a new allocation as 474 * last page could have large enough tailroom. 475 */ 476 prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt), 477 GFP_USER); 478 if (!prog_adj) 479 return ERR_PTR(-ENOMEM); 480 481 prog_adj->len = insn_adj_cnt; 482 483 /* Patching happens in 3 steps: 484 * 485 * 1) Move over tail of insnsi from next instruction onwards, 486 * so we can patch the single target insn with one or more 487 * new ones (patching is always from 1 to n insns, n > 0). 488 * 2) Inject new instructions at the target location. 489 * 3) Adjust branch offsets if necessary. 490 */ 491 insn_rest = insn_adj_cnt - off - len; 492 493 memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1, 494 sizeof(*patch) * insn_rest); 495 memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len); 496 497 /* We are guaranteed to not fail at this point, otherwise 498 * the ship has sailed to reverse to the original state. An 499 * overflow cannot happen at this point. 500 */ 501 BUG_ON(bpf_adj_branches(prog_adj, off, off + 1, off + len, false)); 502 503 bpf_adj_linfo(prog_adj, off, insn_delta); 504 505 return prog_adj; 506 } 507 508 int bpf_remove_insns(struct bpf_prog *prog, u32 off, u32 cnt) 509 { 510 /* Branch offsets can't overflow when program is shrinking, no need 511 * to call bpf_adj_branches(..., true) here 512 */ 513 memmove(prog->insnsi + off, prog->insnsi + off + cnt, 514 sizeof(struct bpf_insn) * (prog->len - off - cnt)); 515 prog->len -= cnt; 516 517 return WARN_ON_ONCE(bpf_adj_branches(prog, off, off + cnt, off, false)); 518 } 519 520 static void bpf_prog_kallsyms_del_subprogs(struct bpf_prog *fp) 521 { 522 int i; 523 524 for (i = 0; i < fp->aux->func_cnt; i++) 525 bpf_prog_kallsyms_del(fp->aux->func[i]); 526 } 527 528 void bpf_prog_kallsyms_del_all(struct bpf_prog *fp) 529 { 530 bpf_prog_kallsyms_del_subprogs(fp); 531 bpf_prog_kallsyms_del(fp); 532 } 533 534 #ifdef CONFIG_BPF_JIT 535 /* All BPF JIT sysctl knobs here. */ 536 int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_DEFAULT_ON); 537 int bpf_jit_kallsyms __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_DEFAULT_ON); 538 int bpf_jit_harden __read_mostly; 539 long bpf_jit_limit __read_mostly; 540 long bpf_jit_limit_max __read_mostly; 541 542 static void 543 bpf_prog_ksym_set_addr(struct bpf_prog *prog) 544 { 545 WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog)); 546 547 prog->aux->ksym.start = (unsigned long) prog->bpf_func; 548 prog->aux->ksym.end = prog->aux->ksym.start + prog->jited_len; 549 } 550 551 static void 552 bpf_prog_ksym_set_name(struct bpf_prog *prog) 553 { 554 char *sym = prog->aux->ksym.name; 555 const char *end = sym + KSYM_NAME_LEN; 556 const struct btf_type *type; 557 const char *func_name; 558 559 BUILD_BUG_ON(sizeof("bpf_prog_") + 560 sizeof(prog->tag) * 2 + 561 /* name has been null terminated. 562 * We should need +1 for the '_' preceding 563 * the name. However, the null character 564 * is double counted between the name and the 565 * sizeof("bpf_prog_") above, so we omit 566 * the +1 here. 567 */ 568 sizeof(prog->aux->name) > KSYM_NAME_LEN); 569 570 sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_"); 571 sym = bin2hex(sym, prog->tag, sizeof(prog->tag)); 572 573 /* prog->aux->name will be ignored if full btf name is available */ 574 if (prog->aux->func_info_cnt) { 575 type = btf_type_by_id(prog->aux->btf, 576 prog->aux->func_info[prog->aux->func_idx].type_id); 577 func_name = btf_name_by_offset(prog->aux->btf, type->name_off); 578 snprintf(sym, (size_t)(end - sym), "_%s", func_name); 579 return; 580 } 581 582 if (prog->aux->name[0]) 583 snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name); 584 else 585 *sym = 0; 586 } 587 588 static unsigned long bpf_get_ksym_start(struct latch_tree_node *n) 589 { 590 return container_of(n, struct bpf_ksym, tnode)->start; 591 } 592 593 static __always_inline bool bpf_tree_less(struct latch_tree_node *a, 594 struct latch_tree_node *b) 595 { 596 return bpf_get_ksym_start(a) < bpf_get_ksym_start(b); 597 } 598 599 static __always_inline int bpf_tree_comp(void *key, struct latch_tree_node *n) 600 { 601 unsigned long val = (unsigned long)key; 602 const struct bpf_ksym *ksym; 603 604 ksym = container_of(n, struct bpf_ksym, tnode); 605 606 if (val < ksym->start) 607 return -1; 608 if (val >= ksym->end) 609 return 1; 610 611 return 0; 612 } 613 614 static const struct latch_tree_ops bpf_tree_ops = { 615 .less = bpf_tree_less, 616 .comp = bpf_tree_comp, 617 }; 618 619 static DEFINE_SPINLOCK(bpf_lock); 620 static LIST_HEAD(bpf_kallsyms); 621 static struct latch_tree_root bpf_tree __cacheline_aligned; 622 623 void bpf_ksym_add(struct bpf_ksym *ksym) 624 { 625 spin_lock_bh(&bpf_lock); 626 WARN_ON_ONCE(!list_empty(&ksym->lnode)); 627 list_add_tail_rcu(&ksym->lnode, &bpf_kallsyms); 628 latch_tree_insert(&ksym->tnode, &bpf_tree, &bpf_tree_ops); 629 spin_unlock_bh(&bpf_lock); 630 } 631 632 static void __bpf_ksym_del(struct bpf_ksym *ksym) 633 { 634 if (list_empty(&ksym->lnode)) 635 return; 636 637 latch_tree_erase(&ksym->tnode, &bpf_tree, &bpf_tree_ops); 638 list_del_rcu(&ksym->lnode); 639 } 640 641 void bpf_ksym_del(struct bpf_ksym *ksym) 642 { 643 spin_lock_bh(&bpf_lock); 644 __bpf_ksym_del(ksym); 645 spin_unlock_bh(&bpf_lock); 646 } 647 648 static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp) 649 { 650 return fp->jited && !bpf_prog_was_classic(fp); 651 } 652 653 static bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp) 654 { 655 return list_empty(&fp->aux->ksym.lnode) || 656 fp->aux->ksym.lnode.prev == LIST_POISON2; 657 } 658 659 void bpf_prog_kallsyms_add(struct bpf_prog *fp) 660 { 661 if (!bpf_prog_kallsyms_candidate(fp) || 662 !bpf_capable()) 663 return; 664 665 bpf_prog_ksym_set_addr(fp); 666 bpf_prog_ksym_set_name(fp); 667 fp->aux->ksym.prog = true; 668 669 bpf_ksym_add(&fp->aux->ksym); 670 } 671 672 void bpf_prog_kallsyms_del(struct bpf_prog *fp) 673 { 674 if (!bpf_prog_kallsyms_candidate(fp)) 675 return; 676 677 bpf_ksym_del(&fp->aux->ksym); 678 } 679 680 static struct bpf_ksym *bpf_ksym_find(unsigned long addr) 681 { 682 struct latch_tree_node *n; 683 684 n = latch_tree_find((void *)addr, &bpf_tree, &bpf_tree_ops); 685 return n ? container_of(n, struct bpf_ksym, tnode) : NULL; 686 } 687 688 const char *__bpf_address_lookup(unsigned long addr, unsigned long *size, 689 unsigned long *off, char *sym) 690 { 691 struct bpf_ksym *ksym; 692 char *ret = NULL; 693 694 rcu_read_lock(); 695 ksym = bpf_ksym_find(addr); 696 if (ksym) { 697 unsigned long symbol_start = ksym->start; 698 unsigned long symbol_end = ksym->end; 699 700 strncpy(sym, ksym->name, KSYM_NAME_LEN); 701 702 ret = sym; 703 if (size) 704 *size = symbol_end - symbol_start; 705 if (off) 706 *off = addr - symbol_start; 707 } 708 rcu_read_unlock(); 709 710 return ret; 711 } 712 713 bool is_bpf_text_address(unsigned long addr) 714 { 715 bool ret; 716 717 rcu_read_lock(); 718 ret = bpf_ksym_find(addr) != NULL; 719 rcu_read_unlock(); 720 721 return ret; 722 } 723 724 static struct bpf_prog *bpf_prog_ksym_find(unsigned long addr) 725 { 726 struct bpf_ksym *ksym = bpf_ksym_find(addr); 727 728 return ksym && ksym->prog ? 729 container_of(ksym, struct bpf_prog_aux, ksym)->prog : 730 NULL; 731 } 732 733 const struct exception_table_entry *search_bpf_extables(unsigned long addr) 734 { 735 const struct exception_table_entry *e = NULL; 736 struct bpf_prog *prog; 737 738 rcu_read_lock(); 739 prog = bpf_prog_ksym_find(addr); 740 if (!prog) 741 goto out; 742 if (!prog->aux->num_exentries) 743 goto out; 744 745 e = search_extable(prog->aux->extable, prog->aux->num_exentries, addr); 746 out: 747 rcu_read_unlock(); 748 return e; 749 } 750 751 int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type, 752 char *sym) 753 { 754 struct bpf_ksym *ksym; 755 unsigned int it = 0; 756 int ret = -ERANGE; 757 758 if (!bpf_jit_kallsyms_enabled()) 759 return ret; 760 761 rcu_read_lock(); 762 list_for_each_entry_rcu(ksym, &bpf_kallsyms, lnode) { 763 if (it++ != symnum) 764 continue; 765 766 strncpy(sym, ksym->name, KSYM_NAME_LEN); 767 768 *value = ksym->start; 769 *type = BPF_SYM_ELF_TYPE; 770 771 ret = 0; 772 break; 773 } 774 rcu_read_unlock(); 775 776 return ret; 777 } 778 779 int bpf_jit_add_poke_descriptor(struct bpf_prog *prog, 780 struct bpf_jit_poke_descriptor *poke) 781 { 782 struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab; 783 static const u32 poke_tab_max = 1024; 784 u32 slot = prog->aux->size_poke_tab; 785 u32 size = slot + 1; 786 787 if (size > poke_tab_max) 788 return -ENOSPC; 789 if (poke->tailcall_target || poke->tailcall_target_stable || 790 poke->tailcall_bypass || poke->adj_off || poke->bypass_addr) 791 return -EINVAL; 792 793 switch (poke->reason) { 794 case BPF_POKE_REASON_TAIL_CALL: 795 if (!poke->tail_call.map) 796 return -EINVAL; 797 break; 798 default: 799 return -EINVAL; 800 } 801 802 tab = krealloc(tab, size * sizeof(*poke), GFP_KERNEL); 803 if (!tab) 804 return -ENOMEM; 805 806 memcpy(&tab[slot], poke, sizeof(*poke)); 807 prog->aux->size_poke_tab = size; 808 prog->aux->poke_tab = tab; 809 810 return slot; 811 } 812 813 /* 814 * BPF program pack allocator. 815 * 816 * Most BPF programs are pretty small. Allocating a hole page for each 817 * program is sometime a waste. Many small bpf program also adds pressure 818 * to instruction TLB. To solve this issue, we introduce a BPF program pack 819 * allocator. The prog_pack allocator uses HPAGE_PMD_SIZE page (2MB on x86) 820 * to host BPF programs. 821 */ 822 #define BPF_PROG_CHUNK_SHIFT 6 823 #define BPF_PROG_CHUNK_SIZE (1 << BPF_PROG_CHUNK_SHIFT) 824 #define BPF_PROG_CHUNK_MASK (~(BPF_PROG_CHUNK_SIZE - 1)) 825 826 struct bpf_prog_pack { 827 struct list_head list; 828 void *ptr; 829 unsigned long bitmap[]; 830 }; 831 832 #define BPF_PROG_SIZE_TO_NBITS(size) (round_up(size, BPF_PROG_CHUNK_SIZE) / BPF_PROG_CHUNK_SIZE) 833 834 static size_t bpf_prog_pack_size = -1; 835 static size_t bpf_prog_pack_mask = -1; 836 837 static int bpf_prog_chunk_count(void) 838 { 839 WARN_ON_ONCE(bpf_prog_pack_size == -1); 840 return bpf_prog_pack_size / BPF_PROG_CHUNK_SIZE; 841 } 842 843 static DEFINE_MUTEX(pack_mutex); 844 static LIST_HEAD(pack_list); 845 846 /* PMD_SIZE is not available in some special config, e.g. ARCH=arm with 847 * CONFIG_MMU=n. Use PAGE_SIZE in these cases. 848 */ 849 #ifdef PMD_SIZE 850 #define BPF_HPAGE_SIZE PMD_SIZE 851 #define BPF_HPAGE_MASK PMD_MASK 852 #else 853 #define BPF_HPAGE_SIZE PAGE_SIZE 854 #define BPF_HPAGE_MASK PAGE_MASK 855 #endif 856 857 static size_t select_bpf_prog_pack_size(void) 858 { 859 size_t size; 860 void *ptr; 861 862 size = BPF_HPAGE_SIZE * num_online_nodes(); 863 ptr = module_alloc(size); 864 865 /* Test whether we can get huge pages. If not just use PAGE_SIZE 866 * packs. 867 */ 868 if (!ptr || !is_vm_area_hugepages(ptr)) { 869 size = PAGE_SIZE; 870 bpf_prog_pack_mask = PAGE_MASK; 871 } else { 872 bpf_prog_pack_mask = BPF_HPAGE_MASK; 873 } 874 875 vfree(ptr); 876 return size; 877 } 878 879 static struct bpf_prog_pack *alloc_new_pack(bpf_jit_fill_hole_t bpf_fill_ill_insns) 880 { 881 struct bpf_prog_pack *pack; 882 883 pack = kzalloc(struct_size(pack, bitmap, BITS_TO_LONGS(bpf_prog_chunk_count())), 884 GFP_KERNEL); 885 if (!pack) 886 return NULL; 887 pack->ptr = module_alloc(bpf_prog_pack_size); 888 if (!pack->ptr) { 889 kfree(pack); 890 return NULL; 891 } 892 bpf_fill_ill_insns(pack->ptr, bpf_prog_pack_size); 893 bitmap_zero(pack->bitmap, bpf_prog_pack_size / BPF_PROG_CHUNK_SIZE); 894 list_add_tail(&pack->list, &pack_list); 895 896 set_vm_flush_reset_perms(pack->ptr); 897 set_memory_ro((unsigned long)pack->ptr, bpf_prog_pack_size / PAGE_SIZE); 898 set_memory_x((unsigned long)pack->ptr, bpf_prog_pack_size / PAGE_SIZE); 899 return pack; 900 } 901 902 static void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns) 903 { 904 unsigned int nbits = BPF_PROG_SIZE_TO_NBITS(size); 905 struct bpf_prog_pack *pack; 906 unsigned long pos; 907 void *ptr = NULL; 908 909 mutex_lock(&pack_mutex); 910 if (bpf_prog_pack_size == -1) 911 bpf_prog_pack_size = select_bpf_prog_pack_size(); 912 913 if (size > bpf_prog_pack_size) { 914 size = round_up(size, PAGE_SIZE); 915 ptr = module_alloc(size); 916 if (ptr) { 917 bpf_fill_ill_insns(ptr, size); 918 set_vm_flush_reset_perms(ptr); 919 set_memory_ro((unsigned long)ptr, size / PAGE_SIZE); 920 set_memory_x((unsigned long)ptr, size / PAGE_SIZE); 921 } 922 goto out; 923 } 924 list_for_each_entry(pack, &pack_list, list) { 925 pos = bitmap_find_next_zero_area(pack->bitmap, bpf_prog_chunk_count(), 0, 926 nbits, 0); 927 if (pos < bpf_prog_chunk_count()) 928 goto found_free_area; 929 } 930 931 pack = alloc_new_pack(bpf_fill_ill_insns); 932 if (!pack) 933 goto out; 934 935 pos = 0; 936 937 found_free_area: 938 bitmap_set(pack->bitmap, pos, nbits); 939 ptr = (void *)(pack->ptr) + (pos << BPF_PROG_CHUNK_SHIFT); 940 941 out: 942 mutex_unlock(&pack_mutex); 943 return ptr; 944 } 945 946 static void bpf_prog_pack_free(struct bpf_binary_header *hdr) 947 { 948 struct bpf_prog_pack *pack = NULL, *tmp; 949 unsigned int nbits; 950 unsigned long pos; 951 void *pack_ptr; 952 953 mutex_lock(&pack_mutex); 954 if (hdr->size > bpf_prog_pack_size) { 955 module_memfree(hdr); 956 goto out; 957 } 958 959 pack_ptr = (void *)((unsigned long)hdr & bpf_prog_pack_mask); 960 961 list_for_each_entry(tmp, &pack_list, list) { 962 if (tmp->ptr == pack_ptr) { 963 pack = tmp; 964 break; 965 } 966 } 967 968 if (WARN_ONCE(!pack, "bpf_prog_pack bug\n")) 969 goto out; 970 971 nbits = BPF_PROG_SIZE_TO_NBITS(hdr->size); 972 pos = ((unsigned long)hdr - (unsigned long)pack_ptr) >> BPF_PROG_CHUNK_SHIFT; 973 974 WARN_ONCE(bpf_arch_text_invalidate(hdr, hdr->size), 975 "bpf_prog_pack bug: missing bpf_arch_text_invalidate?\n"); 976 977 bitmap_clear(pack->bitmap, pos, nbits); 978 if (bitmap_find_next_zero_area(pack->bitmap, bpf_prog_chunk_count(), 0, 979 bpf_prog_chunk_count(), 0) == 0) { 980 list_del(&pack->list); 981 module_memfree(pack->ptr); 982 kfree(pack); 983 } 984 out: 985 mutex_unlock(&pack_mutex); 986 } 987 988 static atomic_long_t bpf_jit_current; 989 990 /* Can be overridden by an arch's JIT compiler if it has a custom, 991 * dedicated BPF backend memory area, or if neither of the two 992 * below apply. 993 */ 994 u64 __weak bpf_jit_alloc_exec_limit(void) 995 { 996 #if defined(MODULES_VADDR) 997 return MODULES_END - MODULES_VADDR; 998 #else 999 return VMALLOC_END - VMALLOC_START; 1000 #endif 1001 } 1002 1003 static int __init bpf_jit_charge_init(void) 1004 { 1005 /* Only used as heuristic here to derive limit. */ 1006 bpf_jit_limit_max = bpf_jit_alloc_exec_limit(); 1007 bpf_jit_limit = min_t(u64, round_up(bpf_jit_limit_max >> 2, 1008 PAGE_SIZE), LONG_MAX); 1009 return 0; 1010 } 1011 pure_initcall(bpf_jit_charge_init); 1012 1013 int bpf_jit_charge_modmem(u32 size) 1014 { 1015 if (atomic_long_add_return(size, &bpf_jit_current) > bpf_jit_limit) { 1016 if (!bpf_capable()) { 1017 atomic_long_sub(size, &bpf_jit_current); 1018 return -EPERM; 1019 } 1020 } 1021 1022 return 0; 1023 } 1024 1025 void bpf_jit_uncharge_modmem(u32 size) 1026 { 1027 atomic_long_sub(size, &bpf_jit_current); 1028 } 1029 1030 void *__weak bpf_jit_alloc_exec(unsigned long size) 1031 { 1032 return module_alloc(size); 1033 } 1034 1035 void __weak bpf_jit_free_exec(void *addr) 1036 { 1037 module_memfree(addr); 1038 } 1039 1040 struct bpf_binary_header * 1041 bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr, 1042 unsigned int alignment, 1043 bpf_jit_fill_hole_t bpf_fill_ill_insns) 1044 { 1045 struct bpf_binary_header *hdr; 1046 u32 size, hole, start; 1047 1048 WARN_ON_ONCE(!is_power_of_2(alignment) || 1049 alignment > BPF_IMAGE_ALIGNMENT); 1050 1051 /* Most of BPF filters are really small, but if some of them 1052 * fill a page, allow at least 128 extra bytes to insert a 1053 * random section of illegal instructions. 1054 */ 1055 size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE); 1056 1057 if (bpf_jit_charge_modmem(size)) 1058 return NULL; 1059 hdr = bpf_jit_alloc_exec(size); 1060 if (!hdr) { 1061 bpf_jit_uncharge_modmem(size); 1062 return NULL; 1063 } 1064 1065 /* Fill space with illegal/arch-dep instructions. */ 1066 bpf_fill_ill_insns(hdr, size); 1067 1068 hdr->size = size; 1069 hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)), 1070 PAGE_SIZE - sizeof(*hdr)); 1071 start = (get_random_int() % hole) & ~(alignment - 1); 1072 1073 /* Leave a random number of instructions before BPF code. */ 1074 *image_ptr = &hdr->image[start]; 1075 1076 return hdr; 1077 } 1078 1079 void bpf_jit_binary_free(struct bpf_binary_header *hdr) 1080 { 1081 u32 size = hdr->size; 1082 1083 bpf_jit_free_exec(hdr); 1084 bpf_jit_uncharge_modmem(size); 1085 } 1086 1087 /* Allocate jit binary from bpf_prog_pack allocator. 1088 * Since the allocated memory is RO+X, the JIT engine cannot write directly 1089 * to the memory. To solve this problem, a RW buffer is also allocated at 1090 * as the same time. The JIT engine should calculate offsets based on the 1091 * RO memory address, but write JITed program to the RW buffer. Once the 1092 * JIT engine finishes, it calls bpf_jit_binary_pack_finalize, which copies 1093 * the JITed program to the RO memory. 1094 */ 1095 struct bpf_binary_header * 1096 bpf_jit_binary_pack_alloc(unsigned int proglen, u8 **image_ptr, 1097 unsigned int alignment, 1098 struct bpf_binary_header **rw_header, 1099 u8 **rw_image, 1100 bpf_jit_fill_hole_t bpf_fill_ill_insns) 1101 { 1102 struct bpf_binary_header *ro_header; 1103 u32 size, hole, start; 1104 1105 WARN_ON_ONCE(!is_power_of_2(alignment) || 1106 alignment > BPF_IMAGE_ALIGNMENT); 1107 1108 /* add 16 bytes for a random section of illegal instructions */ 1109 size = round_up(proglen + sizeof(*ro_header) + 16, BPF_PROG_CHUNK_SIZE); 1110 1111 if (bpf_jit_charge_modmem(size)) 1112 return NULL; 1113 ro_header = bpf_prog_pack_alloc(size, bpf_fill_ill_insns); 1114 if (!ro_header) { 1115 bpf_jit_uncharge_modmem(size); 1116 return NULL; 1117 } 1118 1119 *rw_header = kvmalloc(size, GFP_KERNEL); 1120 if (!*rw_header) { 1121 bpf_arch_text_copy(&ro_header->size, &size, sizeof(size)); 1122 bpf_prog_pack_free(ro_header); 1123 bpf_jit_uncharge_modmem(size); 1124 return NULL; 1125 } 1126 1127 /* Fill space with illegal/arch-dep instructions. */ 1128 bpf_fill_ill_insns(*rw_header, size); 1129 (*rw_header)->size = size; 1130 1131 hole = min_t(unsigned int, size - (proglen + sizeof(*ro_header)), 1132 BPF_PROG_CHUNK_SIZE - sizeof(*ro_header)); 1133 start = (get_random_int() % hole) & ~(alignment - 1); 1134 1135 *image_ptr = &ro_header->image[start]; 1136 *rw_image = &(*rw_header)->image[start]; 1137 1138 return ro_header; 1139 } 1140 1141 /* Copy JITed text from rw_header to its final location, the ro_header. */ 1142 int bpf_jit_binary_pack_finalize(struct bpf_prog *prog, 1143 struct bpf_binary_header *ro_header, 1144 struct bpf_binary_header *rw_header) 1145 { 1146 void *ptr; 1147 1148 ptr = bpf_arch_text_copy(ro_header, rw_header, rw_header->size); 1149 1150 kvfree(rw_header); 1151 1152 if (IS_ERR(ptr)) { 1153 bpf_prog_pack_free(ro_header); 1154 return PTR_ERR(ptr); 1155 } 1156 prog->aux->use_bpf_prog_pack = true; 1157 return 0; 1158 } 1159 1160 /* bpf_jit_binary_pack_free is called in two different scenarios: 1161 * 1) when the program is freed after; 1162 * 2) when the JIT engine fails (before bpf_jit_binary_pack_finalize). 1163 * For case 2), we need to free both the RO memory and the RW buffer. 1164 * 1165 * bpf_jit_binary_pack_free requires proper ro_header->size. However, 1166 * bpf_jit_binary_pack_alloc does not set it. Therefore, ro_header->size 1167 * must be set with either bpf_jit_binary_pack_finalize (normal path) or 1168 * bpf_arch_text_copy (when jit fails). 1169 */ 1170 void bpf_jit_binary_pack_free(struct bpf_binary_header *ro_header, 1171 struct bpf_binary_header *rw_header) 1172 { 1173 u32 size = ro_header->size; 1174 1175 bpf_prog_pack_free(ro_header); 1176 kvfree(rw_header); 1177 bpf_jit_uncharge_modmem(size); 1178 } 1179 1180 static inline struct bpf_binary_header * 1181 bpf_jit_binary_hdr(const struct bpf_prog *fp) 1182 { 1183 unsigned long real_start = (unsigned long)fp->bpf_func; 1184 unsigned long addr; 1185 1186 if (fp->aux->use_bpf_prog_pack) 1187 addr = real_start & BPF_PROG_CHUNK_MASK; 1188 else 1189 addr = real_start & PAGE_MASK; 1190 1191 return (void *)addr; 1192 } 1193 1194 /* This symbol is only overridden by archs that have different 1195 * requirements than the usual eBPF JITs, f.e. when they only 1196 * implement cBPF JIT, do not set images read-only, etc. 1197 */ 1198 void __weak bpf_jit_free(struct bpf_prog *fp) 1199 { 1200 if (fp->jited) { 1201 struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp); 1202 1203 if (fp->aux->use_bpf_prog_pack) 1204 bpf_jit_binary_pack_free(hdr, NULL /* rw_buffer */); 1205 else 1206 bpf_jit_binary_free(hdr); 1207 1208 WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp)); 1209 } 1210 1211 bpf_prog_unlock_free(fp); 1212 } 1213 1214 int bpf_jit_get_func_addr(const struct bpf_prog *prog, 1215 const struct bpf_insn *insn, bool extra_pass, 1216 u64 *func_addr, bool *func_addr_fixed) 1217 { 1218 s16 off = insn->off; 1219 s32 imm = insn->imm; 1220 u8 *addr; 1221 1222 *func_addr_fixed = insn->src_reg != BPF_PSEUDO_CALL; 1223 if (!*func_addr_fixed) { 1224 /* Place-holder address till the last pass has collected 1225 * all addresses for JITed subprograms in which case we 1226 * can pick them up from prog->aux. 1227 */ 1228 if (!extra_pass) 1229 addr = NULL; 1230 else if (prog->aux->func && 1231 off >= 0 && off < prog->aux->func_cnt) 1232 addr = (u8 *)prog->aux->func[off]->bpf_func; 1233 else 1234 return -EINVAL; 1235 } else { 1236 /* Address of a BPF helper call. Since part of the core 1237 * kernel, it's always at a fixed location. __bpf_call_base 1238 * and the helper with imm relative to it are both in core 1239 * kernel. 1240 */ 1241 addr = (u8 *)__bpf_call_base + imm; 1242 } 1243 1244 *func_addr = (unsigned long)addr; 1245 return 0; 1246 } 1247 1248 static int bpf_jit_blind_insn(const struct bpf_insn *from, 1249 const struct bpf_insn *aux, 1250 struct bpf_insn *to_buff, 1251 bool emit_zext) 1252 { 1253 struct bpf_insn *to = to_buff; 1254 u32 imm_rnd = get_random_int(); 1255 s16 off; 1256 1257 BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG); 1258 BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG); 1259 1260 /* Constraints on AX register: 1261 * 1262 * AX register is inaccessible from user space. It is mapped in 1263 * all JITs, and used here for constant blinding rewrites. It is 1264 * typically "stateless" meaning its contents are only valid within 1265 * the executed instruction, but not across several instructions. 1266 * There are a few exceptions however which are further detailed 1267 * below. 1268 * 1269 * Constant blinding is only used by JITs, not in the interpreter. 1270 * The interpreter uses AX in some occasions as a local temporary 1271 * register e.g. in DIV or MOD instructions. 1272 * 1273 * In restricted circumstances, the verifier can also use the AX 1274 * register for rewrites as long as they do not interfere with 1275 * the above cases! 1276 */ 1277 if (from->dst_reg == BPF_REG_AX || from->src_reg == BPF_REG_AX) 1278 goto out; 1279 1280 if (from->imm == 0 && 1281 (from->code == (BPF_ALU | BPF_MOV | BPF_K) || 1282 from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) { 1283 *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg); 1284 goto out; 1285 } 1286 1287 switch (from->code) { 1288 case BPF_ALU | BPF_ADD | BPF_K: 1289 case BPF_ALU | BPF_SUB | BPF_K: 1290 case BPF_ALU | BPF_AND | BPF_K: 1291 case BPF_ALU | BPF_OR | BPF_K: 1292 case BPF_ALU | BPF_XOR | BPF_K: 1293 case BPF_ALU | BPF_MUL | BPF_K: 1294 case BPF_ALU | BPF_MOV | BPF_K: 1295 case BPF_ALU | BPF_DIV | BPF_K: 1296 case BPF_ALU | BPF_MOD | BPF_K: 1297 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); 1298 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); 1299 *to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX); 1300 break; 1301 1302 case BPF_ALU64 | BPF_ADD | BPF_K: 1303 case BPF_ALU64 | BPF_SUB | BPF_K: 1304 case BPF_ALU64 | BPF_AND | BPF_K: 1305 case BPF_ALU64 | BPF_OR | BPF_K: 1306 case BPF_ALU64 | BPF_XOR | BPF_K: 1307 case BPF_ALU64 | BPF_MUL | BPF_K: 1308 case BPF_ALU64 | BPF_MOV | BPF_K: 1309 case BPF_ALU64 | BPF_DIV | BPF_K: 1310 case BPF_ALU64 | BPF_MOD | BPF_K: 1311 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); 1312 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); 1313 *to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX); 1314 break; 1315 1316 case BPF_JMP | BPF_JEQ | BPF_K: 1317 case BPF_JMP | BPF_JNE | BPF_K: 1318 case BPF_JMP | BPF_JGT | BPF_K: 1319 case BPF_JMP | BPF_JLT | BPF_K: 1320 case BPF_JMP | BPF_JGE | BPF_K: 1321 case BPF_JMP | BPF_JLE | BPF_K: 1322 case BPF_JMP | BPF_JSGT | BPF_K: 1323 case BPF_JMP | BPF_JSLT | BPF_K: 1324 case BPF_JMP | BPF_JSGE | BPF_K: 1325 case BPF_JMP | BPF_JSLE | BPF_K: 1326 case BPF_JMP | BPF_JSET | BPF_K: 1327 /* Accommodate for extra offset in case of a backjump. */ 1328 off = from->off; 1329 if (off < 0) 1330 off -= 2; 1331 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); 1332 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); 1333 *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off); 1334 break; 1335 1336 case BPF_JMP32 | BPF_JEQ | BPF_K: 1337 case BPF_JMP32 | BPF_JNE | BPF_K: 1338 case BPF_JMP32 | BPF_JGT | BPF_K: 1339 case BPF_JMP32 | BPF_JLT | BPF_K: 1340 case BPF_JMP32 | BPF_JGE | BPF_K: 1341 case BPF_JMP32 | BPF_JLE | BPF_K: 1342 case BPF_JMP32 | BPF_JSGT | BPF_K: 1343 case BPF_JMP32 | BPF_JSLT | BPF_K: 1344 case BPF_JMP32 | BPF_JSGE | BPF_K: 1345 case BPF_JMP32 | BPF_JSLE | BPF_K: 1346 case BPF_JMP32 | BPF_JSET | BPF_K: 1347 /* Accommodate for extra offset in case of a backjump. */ 1348 off = from->off; 1349 if (off < 0) 1350 off -= 2; 1351 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); 1352 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); 1353 *to++ = BPF_JMP32_REG(from->code, from->dst_reg, BPF_REG_AX, 1354 off); 1355 break; 1356 1357 case BPF_LD | BPF_IMM | BPF_DW: 1358 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm); 1359 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); 1360 *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); 1361 *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX); 1362 break; 1363 case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */ 1364 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm); 1365 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); 1366 if (emit_zext) 1367 *to++ = BPF_ZEXT_REG(BPF_REG_AX); 1368 *to++ = BPF_ALU64_REG(BPF_OR, aux[0].dst_reg, BPF_REG_AX); 1369 break; 1370 1371 case BPF_ST | BPF_MEM | BPF_DW: 1372 case BPF_ST | BPF_MEM | BPF_W: 1373 case BPF_ST | BPF_MEM | BPF_H: 1374 case BPF_ST | BPF_MEM | BPF_B: 1375 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); 1376 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); 1377 *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off); 1378 break; 1379 } 1380 out: 1381 return to - to_buff; 1382 } 1383 1384 static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other, 1385 gfp_t gfp_extra_flags) 1386 { 1387 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags; 1388 struct bpf_prog *fp; 1389 1390 fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags); 1391 if (fp != NULL) { 1392 /* aux->prog still points to the fp_other one, so 1393 * when promoting the clone to the real program, 1394 * this still needs to be adapted. 1395 */ 1396 memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE); 1397 } 1398 1399 return fp; 1400 } 1401 1402 static void bpf_prog_clone_free(struct bpf_prog *fp) 1403 { 1404 /* aux was stolen by the other clone, so we cannot free 1405 * it from this path! It will be freed eventually by the 1406 * other program on release. 1407 * 1408 * At this point, we don't need a deferred release since 1409 * clone is guaranteed to not be locked. 1410 */ 1411 fp->aux = NULL; 1412 fp->stats = NULL; 1413 fp->active = NULL; 1414 __bpf_prog_free(fp); 1415 } 1416 1417 void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other) 1418 { 1419 /* We have to repoint aux->prog to self, as we don't 1420 * know whether fp here is the clone or the original. 1421 */ 1422 fp->aux->prog = fp; 1423 bpf_prog_clone_free(fp_other); 1424 } 1425 1426 struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog) 1427 { 1428 struct bpf_insn insn_buff[16], aux[2]; 1429 struct bpf_prog *clone, *tmp; 1430 int insn_delta, insn_cnt; 1431 struct bpf_insn *insn; 1432 int i, rewritten; 1433 1434 if (!prog->blinding_requested || prog->blinded) 1435 return prog; 1436 1437 clone = bpf_prog_clone_create(prog, GFP_USER); 1438 if (!clone) 1439 return ERR_PTR(-ENOMEM); 1440 1441 insn_cnt = clone->len; 1442 insn = clone->insnsi; 1443 1444 for (i = 0; i < insn_cnt; i++, insn++) { 1445 if (bpf_pseudo_func(insn)) { 1446 /* ld_imm64 with an address of bpf subprog is not 1447 * a user controlled constant. Don't randomize it, 1448 * since it will conflict with jit_subprogs() logic. 1449 */ 1450 insn++; 1451 i++; 1452 continue; 1453 } 1454 1455 /* We temporarily need to hold the original ld64 insn 1456 * so that we can still access the first part in the 1457 * second blinding run. 1458 */ 1459 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) && 1460 insn[1].code == 0) 1461 memcpy(aux, insn, sizeof(aux)); 1462 1463 rewritten = bpf_jit_blind_insn(insn, aux, insn_buff, 1464 clone->aux->verifier_zext); 1465 if (!rewritten) 1466 continue; 1467 1468 tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten); 1469 if (IS_ERR(tmp)) { 1470 /* Patching may have repointed aux->prog during 1471 * realloc from the original one, so we need to 1472 * fix it up here on error. 1473 */ 1474 bpf_jit_prog_release_other(prog, clone); 1475 return tmp; 1476 } 1477 1478 clone = tmp; 1479 insn_delta = rewritten - 1; 1480 1481 /* Walk new program and skip insns we just inserted. */ 1482 insn = clone->insnsi + i + insn_delta; 1483 insn_cnt += insn_delta; 1484 i += insn_delta; 1485 } 1486 1487 clone->blinded = 1; 1488 return clone; 1489 } 1490 #endif /* CONFIG_BPF_JIT */ 1491 1492 /* Base function for offset calculation. Needs to go into .text section, 1493 * therefore keeping it non-static as well; will also be used by JITs 1494 * anyway later on, so do not let the compiler omit it. This also needs 1495 * to go into kallsyms for correlation from e.g. bpftool, so naming 1496 * must not change. 1497 */ 1498 noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) 1499 { 1500 return 0; 1501 } 1502 EXPORT_SYMBOL_GPL(__bpf_call_base); 1503 1504 /* All UAPI available opcodes. */ 1505 #define BPF_INSN_MAP(INSN_2, INSN_3) \ 1506 /* 32 bit ALU operations. */ \ 1507 /* Register based. */ \ 1508 INSN_3(ALU, ADD, X), \ 1509 INSN_3(ALU, SUB, X), \ 1510 INSN_3(ALU, AND, X), \ 1511 INSN_3(ALU, OR, X), \ 1512 INSN_3(ALU, LSH, X), \ 1513 INSN_3(ALU, RSH, X), \ 1514 INSN_3(ALU, XOR, X), \ 1515 INSN_3(ALU, MUL, X), \ 1516 INSN_3(ALU, MOV, X), \ 1517 INSN_3(ALU, ARSH, X), \ 1518 INSN_3(ALU, DIV, X), \ 1519 INSN_3(ALU, MOD, X), \ 1520 INSN_2(ALU, NEG), \ 1521 INSN_3(ALU, END, TO_BE), \ 1522 INSN_3(ALU, END, TO_LE), \ 1523 /* Immediate based. */ \ 1524 INSN_3(ALU, ADD, K), \ 1525 INSN_3(ALU, SUB, K), \ 1526 INSN_3(ALU, AND, K), \ 1527 INSN_3(ALU, OR, K), \ 1528 INSN_3(ALU, LSH, K), \ 1529 INSN_3(ALU, RSH, K), \ 1530 INSN_3(ALU, XOR, K), \ 1531 INSN_3(ALU, MUL, K), \ 1532 INSN_3(ALU, MOV, K), \ 1533 INSN_3(ALU, ARSH, K), \ 1534 INSN_3(ALU, DIV, K), \ 1535 INSN_3(ALU, MOD, K), \ 1536 /* 64 bit ALU operations. */ \ 1537 /* Register based. */ \ 1538 INSN_3(ALU64, ADD, X), \ 1539 INSN_3(ALU64, SUB, X), \ 1540 INSN_3(ALU64, AND, X), \ 1541 INSN_3(ALU64, OR, X), \ 1542 INSN_3(ALU64, LSH, X), \ 1543 INSN_3(ALU64, RSH, X), \ 1544 INSN_3(ALU64, XOR, X), \ 1545 INSN_3(ALU64, MUL, X), \ 1546 INSN_3(ALU64, MOV, X), \ 1547 INSN_3(ALU64, ARSH, X), \ 1548 INSN_3(ALU64, DIV, X), \ 1549 INSN_3(ALU64, MOD, X), \ 1550 INSN_2(ALU64, NEG), \ 1551 /* Immediate based. */ \ 1552 INSN_3(ALU64, ADD, K), \ 1553 INSN_3(ALU64, SUB, K), \ 1554 INSN_3(ALU64, AND, K), \ 1555 INSN_3(ALU64, OR, K), \ 1556 INSN_3(ALU64, LSH, K), \ 1557 INSN_3(ALU64, RSH, K), \ 1558 INSN_3(ALU64, XOR, K), \ 1559 INSN_3(ALU64, MUL, K), \ 1560 INSN_3(ALU64, MOV, K), \ 1561 INSN_3(ALU64, ARSH, K), \ 1562 INSN_3(ALU64, DIV, K), \ 1563 INSN_3(ALU64, MOD, K), \ 1564 /* Call instruction. */ \ 1565 INSN_2(JMP, CALL), \ 1566 /* Exit instruction. */ \ 1567 INSN_2(JMP, EXIT), \ 1568 /* 32-bit Jump instructions. */ \ 1569 /* Register based. */ \ 1570 INSN_3(JMP32, JEQ, X), \ 1571 INSN_3(JMP32, JNE, X), \ 1572 INSN_3(JMP32, JGT, X), \ 1573 INSN_3(JMP32, JLT, X), \ 1574 INSN_3(JMP32, JGE, X), \ 1575 INSN_3(JMP32, JLE, X), \ 1576 INSN_3(JMP32, JSGT, X), \ 1577 INSN_3(JMP32, JSLT, X), \ 1578 INSN_3(JMP32, JSGE, X), \ 1579 INSN_3(JMP32, JSLE, X), \ 1580 INSN_3(JMP32, JSET, X), \ 1581 /* Immediate based. */ \ 1582 INSN_3(JMP32, JEQ, K), \ 1583 INSN_3(JMP32, JNE, K), \ 1584 INSN_3(JMP32, JGT, K), \ 1585 INSN_3(JMP32, JLT, K), \ 1586 INSN_3(JMP32, JGE, K), \ 1587 INSN_3(JMP32, JLE, K), \ 1588 INSN_3(JMP32, JSGT, K), \ 1589 INSN_3(JMP32, JSLT, K), \ 1590 INSN_3(JMP32, JSGE, K), \ 1591 INSN_3(JMP32, JSLE, K), \ 1592 INSN_3(JMP32, JSET, K), \ 1593 /* Jump instructions. */ \ 1594 /* Register based. */ \ 1595 INSN_3(JMP, JEQ, X), \ 1596 INSN_3(JMP, JNE, X), \ 1597 INSN_3(JMP, JGT, X), \ 1598 INSN_3(JMP, JLT, X), \ 1599 INSN_3(JMP, JGE, X), \ 1600 INSN_3(JMP, JLE, X), \ 1601 INSN_3(JMP, JSGT, X), \ 1602 INSN_3(JMP, JSLT, X), \ 1603 INSN_3(JMP, JSGE, X), \ 1604 INSN_3(JMP, JSLE, X), \ 1605 INSN_3(JMP, JSET, X), \ 1606 /* Immediate based. */ \ 1607 INSN_3(JMP, JEQ, K), \ 1608 INSN_3(JMP, JNE, K), \ 1609 INSN_3(JMP, JGT, K), \ 1610 INSN_3(JMP, JLT, K), \ 1611 INSN_3(JMP, JGE, K), \ 1612 INSN_3(JMP, JLE, K), \ 1613 INSN_3(JMP, JSGT, K), \ 1614 INSN_3(JMP, JSLT, K), \ 1615 INSN_3(JMP, JSGE, K), \ 1616 INSN_3(JMP, JSLE, K), \ 1617 INSN_3(JMP, JSET, K), \ 1618 INSN_2(JMP, JA), \ 1619 /* Store instructions. */ \ 1620 /* Register based. */ \ 1621 INSN_3(STX, MEM, B), \ 1622 INSN_3(STX, MEM, H), \ 1623 INSN_3(STX, MEM, W), \ 1624 INSN_3(STX, MEM, DW), \ 1625 INSN_3(STX, ATOMIC, W), \ 1626 INSN_3(STX, ATOMIC, DW), \ 1627 /* Immediate based. */ \ 1628 INSN_3(ST, MEM, B), \ 1629 INSN_3(ST, MEM, H), \ 1630 INSN_3(ST, MEM, W), \ 1631 INSN_3(ST, MEM, DW), \ 1632 /* Load instructions. */ \ 1633 /* Register based. */ \ 1634 INSN_3(LDX, MEM, B), \ 1635 INSN_3(LDX, MEM, H), \ 1636 INSN_3(LDX, MEM, W), \ 1637 INSN_3(LDX, MEM, DW), \ 1638 /* Immediate based. */ \ 1639 INSN_3(LD, IMM, DW) 1640 1641 bool bpf_opcode_in_insntable(u8 code) 1642 { 1643 #define BPF_INSN_2_TBL(x, y) [BPF_##x | BPF_##y] = true 1644 #define BPF_INSN_3_TBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = true 1645 static const bool public_insntable[256] = { 1646 [0 ... 255] = false, 1647 /* Now overwrite non-defaults ... */ 1648 BPF_INSN_MAP(BPF_INSN_2_TBL, BPF_INSN_3_TBL), 1649 /* UAPI exposed, but rewritten opcodes. cBPF carry-over. */ 1650 [BPF_LD | BPF_ABS | BPF_B] = true, 1651 [BPF_LD | BPF_ABS | BPF_H] = true, 1652 [BPF_LD | BPF_ABS | BPF_W] = true, 1653 [BPF_LD | BPF_IND | BPF_B] = true, 1654 [BPF_LD | BPF_IND | BPF_H] = true, 1655 [BPF_LD | BPF_IND | BPF_W] = true, 1656 }; 1657 #undef BPF_INSN_3_TBL 1658 #undef BPF_INSN_2_TBL 1659 return public_insntable[code]; 1660 } 1661 1662 #ifndef CONFIG_BPF_JIT_ALWAYS_ON 1663 u64 __weak bpf_probe_read_kernel(void *dst, u32 size, const void *unsafe_ptr) 1664 { 1665 memset(dst, 0, size); 1666 return -EFAULT; 1667 } 1668 1669 /** 1670 * ___bpf_prog_run - run eBPF program on a given context 1671 * @regs: is the array of MAX_BPF_EXT_REG eBPF pseudo-registers 1672 * @insn: is the array of eBPF instructions 1673 * 1674 * Decode and execute eBPF instructions. 1675 * 1676 * Return: whatever value is in %BPF_R0 at program exit 1677 */ 1678 static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn) 1679 { 1680 #define BPF_INSN_2_LBL(x, y) [BPF_##x | BPF_##y] = &&x##_##y 1681 #define BPF_INSN_3_LBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = &&x##_##y##_##z 1682 static const void * const jumptable[256] __annotate_jump_table = { 1683 [0 ... 255] = &&default_label, 1684 /* Now overwrite non-defaults ... */ 1685 BPF_INSN_MAP(BPF_INSN_2_LBL, BPF_INSN_3_LBL), 1686 /* Non-UAPI available opcodes. */ 1687 [BPF_JMP | BPF_CALL_ARGS] = &&JMP_CALL_ARGS, 1688 [BPF_JMP | BPF_TAIL_CALL] = &&JMP_TAIL_CALL, 1689 [BPF_ST | BPF_NOSPEC] = &&ST_NOSPEC, 1690 [BPF_LDX | BPF_PROBE_MEM | BPF_B] = &&LDX_PROBE_MEM_B, 1691 [BPF_LDX | BPF_PROBE_MEM | BPF_H] = &&LDX_PROBE_MEM_H, 1692 [BPF_LDX | BPF_PROBE_MEM | BPF_W] = &&LDX_PROBE_MEM_W, 1693 [BPF_LDX | BPF_PROBE_MEM | BPF_DW] = &&LDX_PROBE_MEM_DW, 1694 }; 1695 #undef BPF_INSN_3_LBL 1696 #undef BPF_INSN_2_LBL 1697 u32 tail_call_cnt = 0; 1698 1699 #define CONT ({ insn++; goto select_insn; }) 1700 #define CONT_JMP ({ insn++; goto select_insn; }) 1701 1702 select_insn: 1703 goto *jumptable[insn->code]; 1704 1705 /* Explicitly mask the register-based shift amounts with 63 or 31 1706 * to avoid undefined behavior. Normally this won't affect the 1707 * generated code, for example, in case of native 64 bit archs such 1708 * as x86-64 or arm64, the compiler is optimizing the AND away for 1709 * the interpreter. In case of JITs, each of the JIT backends compiles 1710 * the BPF shift operations to machine instructions which produce 1711 * implementation-defined results in such a case; the resulting 1712 * contents of the register may be arbitrary, but program behaviour 1713 * as a whole remains defined. In other words, in case of JIT backends, 1714 * the AND must /not/ be added to the emitted LSH/RSH/ARSH translation. 1715 */ 1716 /* ALU (shifts) */ 1717 #define SHT(OPCODE, OP) \ 1718 ALU64_##OPCODE##_X: \ 1719 DST = DST OP (SRC & 63); \ 1720 CONT; \ 1721 ALU_##OPCODE##_X: \ 1722 DST = (u32) DST OP ((u32) SRC & 31); \ 1723 CONT; \ 1724 ALU64_##OPCODE##_K: \ 1725 DST = DST OP IMM; \ 1726 CONT; \ 1727 ALU_##OPCODE##_K: \ 1728 DST = (u32) DST OP (u32) IMM; \ 1729 CONT; 1730 /* ALU (rest) */ 1731 #define ALU(OPCODE, OP) \ 1732 ALU64_##OPCODE##_X: \ 1733 DST = DST OP SRC; \ 1734 CONT; \ 1735 ALU_##OPCODE##_X: \ 1736 DST = (u32) DST OP (u32) SRC; \ 1737 CONT; \ 1738 ALU64_##OPCODE##_K: \ 1739 DST = DST OP IMM; \ 1740 CONT; \ 1741 ALU_##OPCODE##_K: \ 1742 DST = (u32) DST OP (u32) IMM; \ 1743 CONT; 1744 ALU(ADD, +) 1745 ALU(SUB, -) 1746 ALU(AND, &) 1747 ALU(OR, |) 1748 ALU(XOR, ^) 1749 ALU(MUL, *) 1750 SHT(LSH, <<) 1751 SHT(RSH, >>) 1752 #undef SHT 1753 #undef ALU 1754 ALU_NEG: 1755 DST = (u32) -DST; 1756 CONT; 1757 ALU64_NEG: 1758 DST = -DST; 1759 CONT; 1760 ALU_MOV_X: 1761 DST = (u32) SRC; 1762 CONT; 1763 ALU_MOV_K: 1764 DST = (u32) IMM; 1765 CONT; 1766 ALU64_MOV_X: 1767 DST = SRC; 1768 CONT; 1769 ALU64_MOV_K: 1770 DST = IMM; 1771 CONT; 1772 LD_IMM_DW: 1773 DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32; 1774 insn++; 1775 CONT; 1776 ALU_ARSH_X: 1777 DST = (u64) (u32) (((s32) DST) >> (SRC & 31)); 1778 CONT; 1779 ALU_ARSH_K: 1780 DST = (u64) (u32) (((s32) DST) >> IMM); 1781 CONT; 1782 ALU64_ARSH_X: 1783 (*(s64 *) &DST) >>= (SRC & 63); 1784 CONT; 1785 ALU64_ARSH_K: 1786 (*(s64 *) &DST) >>= IMM; 1787 CONT; 1788 ALU64_MOD_X: 1789 div64_u64_rem(DST, SRC, &AX); 1790 DST = AX; 1791 CONT; 1792 ALU_MOD_X: 1793 AX = (u32) DST; 1794 DST = do_div(AX, (u32) SRC); 1795 CONT; 1796 ALU64_MOD_K: 1797 div64_u64_rem(DST, IMM, &AX); 1798 DST = AX; 1799 CONT; 1800 ALU_MOD_K: 1801 AX = (u32) DST; 1802 DST = do_div(AX, (u32) IMM); 1803 CONT; 1804 ALU64_DIV_X: 1805 DST = div64_u64(DST, SRC); 1806 CONT; 1807 ALU_DIV_X: 1808 AX = (u32) DST; 1809 do_div(AX, (u32) SRC); 1810 DST = (u32) AX; 1811 CONT; 1812 ALU64_DIV_K: 1813 DST = div64_u64(DST, IMM); 1814 CONT; 1815 ALU_DIV_K: 1816 AX = (u32) DST; 1817 do_div(AX, (u32) IMM); 1818 DST = (u32) AX; 1819 CONT; 1820 ALU_END_TO_BE: 1821 switch (IMM) { 1822 case 16: 1823 DST = (__force u16) cpu_to_be16(DST); 1824 break; 1825 case 32: 1826 DST = (__force u32) cpu_to_be32(DST); 1827 break; 1828 case 64: 1829 DST = (__force u64) cpu_to_be64(DST); 1830 break; 1831 } 1832 CONT; 1833 ALU_END_TO_LE: 1834 switch (IMM) { 1835 case 16: 1836 DST = (__force u16) cpu_to_le16(DST); 1837 break; 1838 case 32: 1839 DST = (__force u32) cpu_to_le32(DST); 1840 break; 1841 case 64: 1842 DST = (__force u64) cpu_to_le64(DST); 1843 break; 1844 } 1845 CONT; 1846 1847 /* CALL */ 1848 JMP_CALL: 1849 /* Function call scratches BPF_R1-BPF_R5 registers, 1850 * preserves BPF_R6-BPF_R9, and stores return value 1851 * into BPF_R0. 1852 */ 1853 BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3, 1854 BPF_R4, BPF_R5); 1855 CONT; 1856 1857 JMP_CALL_ARGS: 1858 BPF_R0 = (__bpf_call_base_args + insn->imm)(BPF_R1, BPF_R2, 1859 BPF_R3, BPF_R4, 1860 BPF_R5, 1861 insn + insn->off + 1); 1862 CONT; 1863 1864 JMP_TAIL_CALL: { 1865 struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2; 1866 struct bpf_array *array = container_of(map, struct bpf_array, map); 1867 struct bpf_prog *prog; 1868 u32 index = BPF_R3; 1869 1870 if (unlikely(index >= array->map.max_entries)) 1871 goto out; 1872 1873 if (unlikely(tail_call_cnt >= MAX_TAIL_CALL_CNT)) 1874 goto out; 1875 1876 tail_call_cnt++; 1877 1878 prog = READ_ONCE(array->ptrs[index]); 1879 if (!prog) 1880 goto out; 1881 1882 /* ARG1 at this point is guaranteed to point to CTX from 1883 * the verifier side due to the fact that the tail call is 1884 * handled like a helper, that is, bpf_tail_call_proto, 1885 * where arg1_type is ARG_PTR_TO_CTX. 1886 */ 1887 insn = prog->insnsi; 1888 goto select_insn; 1889 out: 1890 CONT; 1891 } 1892 JMP_JA: 1893 insn += insn->off; 1894 CONT; 1895 JMP_EXIT: 1896 return BPF_R0; 1897 /* JMP */ 1898 #define COND_JMP(SIGN, OPCODE, CMP_OP) \ 1899 JMP_##OPCODE##_X: \ 1900 if ((SIGN##64) DST CMP_OP (SIGN##64) SRC) { \ 1901 insn += insn->off; \ 1902 CONT_JMP; \ 1903 } \ 1904 CONT; \ 1905 JMP32_##OPCODE##_X: \ 1906 if ((SIGN##32) DST CMP_OP (SIGN##32) SRC) { \ 1907 insn += insn->off; \ 1908 CONT_JMP; \ 1909 } \ 1910 CONT; \ 1911 JMP_##OPCODE##_K: \ 1912 if ((SIGN##64) DST CMP_OP (SIGN##64) IMM) { \ 1913 insn += insn->off; \ 1914 CONT_JMP; \ 1915 } \ 1916 CONT; \ 1917 JMP32_##OPCODE##_K: \ 1918 if ((SIGN##32) DST CMP_OP (SIGN##32) IMM) { \ 1919 insn += insn->off; \ 1920 CONT_JMP; \ 1921 } \ 1922 CONT; 1923 COND_JMP(u, JEQ, ==) 1924 COND_JMP(u, JNE, !=) 1925 COND_JMP(u, JGT, >) 1926 COND_JMP(u, JLT, <) 1927 COND_JMP(u, JGE, >=) 1928 COND_JMP(u, JLE, <=) 1929 COND_JMP(u, JSET, &) 1930 COND_JMP(s, JSGT, >) 1931 COND_JMP(s, JSLT, <) 1932 COND_JMP(s, JSGE, >=) 1933 COND_JMP(s, JSLE, <=) 1934 #undef COND_JMP 1935 /* ST, STX and LDX*/ 1936 ST_NOSPEC: 1937 /* Speculation barrier for mitigating Speculative Store Bypass. 1938 * In case of arm64, we rely on the firmware mitigation as 1939 * controlled via the ssbd kernel parameter. Whenever the 1940 * mitigation is enabled, it works for all of the kernel code 1941 * with no need to provide any additional instructions here. 1942 * In case of x86, we use 'lfence' insn for mitigation. We 1943 * reuse preexisting logic from Spectre v1 mitigation that 1944 * happens to produce the required code on x86 for v4 as well. 1945 */ 1946 #ifdef CONFIG_X86 1947 barrier_nospec(); 1948 #endif 1949 CONT; 1950 #define LDST(SIZEOP, SIZE) \ 1951 STX_MEM_##SIZEOP: \ 1952 *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \ 1953 CONT; \ 1954 ST_MEM_##SIZEOP: \ 1955 *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \ 1956 CONT; \ 1957 LDX_MEM_##SIZEOP: \ 1958 DST = *(SIZE *)(unsigned long) (SRC + insn->off); \ 1959 CONT; \ 1960 LDX_PROBE_MEM_##SIZEOP: \ 1961 bpf_probe_read_kernel(&DST, sizeof(SIZE), \ 1962 (const void *)(long) (SRC + insn->off)); \ 1963 DST = *((SIZE *)&DST); \ 1964 CONT; 1965 1966 LDST(B, u8) 1967 LDST(H, u16) 1968 LDST(W, u32) 1969 LDST(DW, u64) 1970 #undef LDST 1971 1972 #define ATOMIC_ALU_OP(BOP, KOP) \ 1973 case BOP: \ 1974 if (BPF_SIZE(insn->code) == BPF_W) \ 1975 atomic_##KOP((u32) SRC, (atomic_t *)(unsigned long) \ 1976 (DST + insn->off)); \ 1977 else \ 1978 atomic64_##KOP((u64) SRC, (atomic64_t *)(unsigned long) \ 1979 (DST + insn->off)); \ 1980 break; \ 1981 case BOP | BPF_FETCH: \ 1982 if (BPF_SIZE(insn->code) == BPF_W) \ 1983 SRC = (u32) atomic_fetch_##KOP( \ 1984 (u32) SRC, \ 1985 (atomic_t *)(unsigned long) (DST + insn->off)); \ 1986 else \ 1987 SRC = (u64) atomic64_fetch_##KOP( \ 1988 (u64) SRC, \ 1989 (atomic64_t *)(unsigned long) (DST + insn->off)); \ 1990 break; 1991 1992 STX_ATOMIC_DW: 1993 STX_ATOMIC_W: 1994 switch (IMM) { 1995 ATOMIC_ALU_OP(BPF_ADD, add) 1996 ATOMIC_ALU_OP(BPF_AND, and) 1997 ATOMIC_ALU_OP(BPF_OR, or) 1998 ATOMIC_ALU_OP(BPF_XOR, xor) 1999 #undef ATOMIC_ALU_OP 2000 2001 case BPF_XCHG: 2002 if (BPF_SIZE(insn->code) == BPF_W) 2003 SRC = (u32) atomic_xchg( 2004 (atomic_t *)(unsigned long) (DST + insn->off), 2005 (u32) SRC); 2006 else 2007 SRC = (u64) atomic64_xchg( 2008 (atomic64_t *)(unsigned long) (DST + insn->off), 2009 (u64) SRC); 2010 break; 2011 case BPF_CMPXCHG: 2012 if (BPF_SIZE(insn->code) == BPF_W) 2013 BPF_R0 = (u32) atomic_cmpxchg( 2014 (atomic_t *)(unsigned long) (DST + insn->off), 2015 (u32) BPF_R0, (u32) SRC); 2016 else 2017 BPF_R0 = (u64) atomic64_cmpxchg( 2018 (atomic64_t *)(unsigned long) (DST + insn->off), 2019 (u64) BPF_R0, (u64) SRC); 2020 break; 2021 2022 default: 2023 goto default_label; 2024 } 2025 CONT; 2026 2027 default_label: 2028 /* If we ever reach this, we have a bug somewhere. Die hard here 2029 * instead of just returning 0; we could be somewhere in a subprog, 2030 * so execution could continue otherwise which we do /not/ want. 2031 * 2032 * Note, verifier whitelists all opcodes in bpf_opcode_in_insntable(). 2033 */ 2034 pr_warn("BPF interpreter: unknown opcode %02x (imm: 0x%x)\n", 2035 insn->code, insn->imm); 2036 BUG_ON(1); 2037 return 0; 2038 } 2039 2040 #define PROG_NAME(stack_size) __bpf_prog_run##stack_size 2041 #define DEFINE_BPF_PROG_RUN(stack_size) \ 2042 static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \ 2043 { \ 2044 u64 stack[stack_size / sizeof(u64)]; \ 2045 u64 regs[MAX_BPF_EXT_REG]; \ 2046 \ 2047 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \ 2048 ARG1 = (u64) (unsigned long) ctx; \ 2049 return ___bpf_prog_run(regs, insn); \ 2050 } 2051 2052 #define PROG_NAME_ARGS(stack_size) __bpf_prog_run_args##stack_size 2053 #define DEFINE_BPF_PROG_RUN_ARGS(stack_size) \ 2054 static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \ 2055 const struct bpf_insn *insn) \ 2056 { \ 2057 u64 stack[stack_size / sizeof(u64)]; \ 2058 u64 regs[MAX_BPF_EXT_REG]; \ 2059 \ 2060 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \ 2061 BPF_R1 = r1; \ 2062 BPF_R2 = r2; \ 2063 BPF_R3 = r3; \ 2064 BPF_R4 = r4; \ 2065 BPF_R5 = r5; \ 2066 return ___bpf_prog_run(regs, insn); \ 2067 } 2068 2069 #define EVAL1(FN, X) FN(X) 2070 #define EVAL2(FN, X, Y...) FN(X) EVAL1(FN, Y) 2071 #define EVAL3(FN, X, Y...) FN(X) EVAL2(FN, Y) 2072 #define EVAL4(FN, X, Y...) FN(X) EVAL3(FN, Y) 2073 #define EVAL5(FN, X, Y...) FN(X) EVAL4(FN, Y) 2074 #define EVAL6(FN, X, Y...) FN(X) EVAL5(FN, Y) 2075 2076 EVAL6(DEFINE_BPF_PROG_RUN, 32, 64, 96, 128, 160, 192); 2077 EVAL6(DEFINE_BPF_PROG_RUN, 224, 256, 288, 320, 352, 384); 2078 EVAL4(DEFINE_BPF_PROG_RUN, 416, 448, 480, 512); 2079 2080 EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 32, 64, 96, 128, 160, 192); 2081 EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 224, 256, 288, 320, 352, 384); 2082 EVAL4(DEFINE_BPF_PROG_RUN_ARGS, 416, 448, 480, 512); 2083 2084 #define PROG_NAME_LIST(stack_size) PROG_NAME(stack_size), 2085 2086 static unsigned int (*interpreters[])(const void *ctx, 2087 const struct bpf_insn *insn) = { 2088 EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192) 2089 EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384) 2090 EVAL4(PROG_NAME_LIST, 416, 448, 480, 512) 2091 }; 2092 #undef PROG_NAME_LIST 2093 #define PROG_NAME_LIST(stack_size) PROG_NAME_ARGS(stack_size), 2094 static u64 (*interpreters_args[])(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, 2095 const struct bpf_insn *insn) = { 2096 EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192) 2097 EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384) 2098 EVAL4(PROG_NAME_LIST, 416, 448, 480, 512) 2099 }; 2100 #undef PROG_NAME_LIST 2101 2102 void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth) 2103 { 2104 stack_depth = max_t(u32, stack_depth, 1); 2105 insn->off = (s16) insn->imm; 2106 insn->imm = interpreters_args[(round_up(stack_depth, 32) / 32) - 1] - 2107 __bpf_call_base_args; 2108 insn->code = BPF_JMP | BPF_CALL_ARGS; 2109 } 2110 2111 #else 2112 static unsigned int __bpf_prog_ret0_warn(const void *ctx, 2113 const struct bpf_insn *insn) 2114 { 2115 /* If this handler ever gets executed, then BPF_JIT_ALWAYS_ON 2116 * is not working properly, so warn about it! 2117 */ 2118 WARN_ON_ONCE(1); 2119 return 0; 2120 } 2121 #endif 2122 2123 bool bpf_prog_map_compatible(struct bpf_map *map, 2124 const struct bpf_prog *fp) 2125 { 2126 bool ret; 2127 2128 if (fp->kprobe_override) 2129 return false; 2130 2131 spin_lock(&map->owner.lock); 2132 if (!map->owner.type) { 2133 /* There's no owner yet where we could check for 2134 * compatibility. 2135 */ 2136 map->owner.type = fp->type; 2137 map->owner.jited = fp->jited; 2138 map->owner.xdp_has_frags = fp->aux->xdp_has_frags; 2139 ret = true; 2140 } else { 2141 ret = map->owner.type == fp->type && 2142 map->owner.jited == fp->jited && 2143 map->owner.xdp_has_frags == fp->aux->xdp_has_frags; 2144 } 2145 spin_unlock(&map->owner.lock); 2146 2147 return ret; 2148 } 2149 2150 static int bpf_check_tail_call(const struct bpf_prog *fp) 2151 { 2152 struct bpf_prog_aux *aux = fp->aux; 2153 int i, ret = 0; 2154 2155 mutex_lock(&aux->used_maps_mutex); 2156 for (i = 0; i < aux->used_map_cnt; i++) { 2157 struct bpf_map *map = aux->used_maps[i]; 2158 2159 if (!map_type_contains_progs(map)) 2160 continue; 2161 2162 if (!bpf_prog_map_compatible(map, fp)) { 2163 ret = -EINVAL; 2164 goto out; 2165 } 2166 } 2167 2168 out: 2169 mutex_unlock(&aux->used_maps_mutex); 2170 return ret; 2171 } 2172 2173 static void bpf_prog_select_func(struct bpf_prog *fp) 2174 { 2175 #ifndef CONFIG_BPF_JIT_ALWAYS_ON 2176 u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1); 2177 2178 fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1]; 2179 #else 2180 fp->bpf_func = __bpf_prog_ret0_warn; 2181 #endif 2182 } 2183 2184 /** 2185 * bpf_prog_select_runtime - select exec runtime for BPF program 2186 * @fp: bpf_prog populated with BPF program 2187 * @err: pointer to error variable 2188 * 2189 * Try to JIT eBPF program, if JIT is not available, use interpreter. 2190 * The BPF program will be executed via bpf_prog_run() function. 2191 * 2192 * Return: the &fp argument along with &err set to 0 for success or 2193 * a negative errno code on failure 2194 */ 2195 struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err) 2196 { 2197 /* In case of BPF to BPF calls, verifier did all the prep 2198 * work with regards to JITing, etc. 2199 */ 2200 bool jit_needed = false; 2201 2202 if (fp->bpf_func) 2203 goto finalize; 2204 2205 if (IS_ENABLED(CONFIG_BPF_JIT_ALWAYS_ON) || 2206 bpf_prog_has_kfunc_call(fp)) 2207 jit_needed = true; 2208 2209 bpf_prog_select_func(fp); 2210 2211 /* eBPF JITs can rewrite the program in case constant 2212 * blinding is active. However, in case of error during 2213 * blinding, bpf_int_jit_compile() must always return a 2214 * valid program, which in this case would simply not 2215 * be JITed, but falls back to the interpreter. 2216 */ 2217 if (!bpf_prog_is_dev_bound(fp->aux)) { 2218 *err = bpf_prog_alloc_jited_linfo(fp); 2219 if (*err) 2220 return fp; 2221 2222 fp = bpf_int_jit_compile(fp); 2223 bpf_prog_jit_attempt_done(fp); 2224 if (!fp->jited && jit_needed) { 2225 *err = -ENOTSUPP; 2226 return fp; 2227 } 2228 } else { 2229 *err = bpf_prog_offload_compile(fp); 2230 if (*err) 2231 return fp; 2232 } 2233 2234 finalize: 2235 bpf_prog_lock_ro(fp); 2236 2237 /* The tail call compatibility check can only be done at 2238 * this late stage as we need to determine, if we deal 2239 * with JITed or non JITed program concatenations and not 2240 * all eBPF JITs might immediately support all features. 2241 */ 2242 *err = bpf_check_tail_call(fp); 2243 2244 return fp; 2245 } 2246 EXPORT_SYMBOL_GPL(bpf_prog_select_runtime); 2247 2248 static unsigned int __bpf_prog_ret1(const void *ctx, 2249 const struct bpf_insn *insn) 2250 { 2251 return 1; 2252 } 2253 2254 static struct bpf_prog_dummy { 2255 struct bpf_prog prog; 2256 } dummy_bpf_prog = { 2257 .prog = { 2258 .bpf_func = __bpf_prog_ret1, 2259 }, 2260 }; 2261 2262 struct bpf_empty_prog_array bpf_empty_prog_array = { 2263 .null_prog = NULL, 2264 }; 2265 EXPORT_SYMBOL(bpf_empty_prog_array); 2266 2267 struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags) 2268 { 2269 if (prog_cnt) 2270 return kzalloc(sizeof(struct bpf_prog_array) + 2271 sizeof(struct bpf_prog_array_item) * 2272 (prog_cnt + 1), 2273 flags); 2274 2275 return &bpf_empty_prog_array.hdr; 2276 } 2277 2278 void bpf_prog_array_free(struct bpf_prog_array *progs) 2279 { 2280 if (!progs || progs == &bpf_empty_prog_array.hdr) 2281 return; 2282 kfree_rcu(progs, rcu); 2283 } 2284 2285 static void __bpf_prog_array_free_sleepable_cb(struct rcu_head *rcu) 2286 { 2287 struct bpf_prog_array *progs; 2288 2289 progs = container_of(rcu, struct bpf_prog_array, rcu); 2290 kfree_rcu(progs, rcu); 2291 } 2292 2293 void bpf_prog_array_free_sleepable(struct bpf_prog_array *progs) 2294 { 2295 if (!progs || progs == &bpf_empty_prog_array.hdr) 2296 return; 2297 call_rcu_tasks_trace(&progs->rcu, __bpf_prog_array_free_sleepable_cb); 2298 } 2299 2300 int bpf_prog_array_length(struct bpf_prog_array *array) 2301 { 2302 struct bpf_prog_array_item *item; 2303 u32 cnt = 0; 2304 2305 for (item = array->items; item->prog; item++) 2306 if (item->prog != &dummy_bpf_prog.prog) 2307 cnt++; 2308 return cnt; 2309 } 2310 2311 bool bpf_prog_array_is_empty(struct bpf_prog_array *array) 2312 { 2313 struct bpf_prog_array_item *item; 2314 2315 for (item = array->items; item->prog; item++) 2316 if (item->prog != &dummy_bpf_prog.prog) 2317 return false; 2318 return true; 2319 } 2320 2321 static bool bpf_prog_array_copy_core(struct bpf_prog_array *array, 2322 u32 *prog_ids, 2323 u32 request_cnt) 2324 { 2325 struct bpf_prog_array_item *item; 2326 int i = 0; 2327 2328 for (item = array->items; item->prog; item++) { 2329 if (item->prog == &dummy_bpf_prog.prog) 2330 continue; 2331 prog_ids[i] = item->prog->aux->id; 2332 if (++i == request_cnt) { 2333 item++; 2334 break; 2335 } 2336 } 2337 2338 return !!(item->prog); 2339 } 2340 2341 int bpf_prog_array_copy_to_user(struct bpf_prog_array *array, 2342 __u32 __user *prog_ids, u32 cnt) 2343 { 2344 unsigned long err = 0; 2345 bool nospc; 2346 u32 *ids; 2347 2348 /* users of this function are doing: 2349 * cnt = bpf_prog_array_length(); 2350 * if (cnt > 0) 2351 * bpf_prog_array_copy_to_user(..., cnt); 2352 * so below kcalloc doesn't need extra cnt > 0 check. 2353 */ 2354 ids = kcalloc(cnt, sizeof(u32), GFP_USER | __GFP_NOWARN); 2355 if (!ids) 2356 return -ENOMEM; 2357 nospc = bpf_prog_array_copy_core(array, ids, cnt); 2358 err = copy_to_user(prog_ids, ids, cnt * sizeof(u32)); 2359 kfree(ids); 2360 if (err) 2361 return -EFAULT; 2362 if (nospc) 2363 return -ENOSPC; 2364 return 0; 2365 } 2366 2367 void bpf_prog_array_delete_safe(struct bpf_prog_array *array, 2368 struct bpf_prog *old_prog) 2369 { 2370 struct bpf_prog_array_item *item; 2371 2372 for (item = array->items; item->prog; item++) 2373 if (item->prog == old_prog) { 2374 WRITE_ONCE(item->prog, &dummy_bpf_prog.prog); 2375 break; 2376 } 2377 } 2378 2379 /** 2380 * bpf_prog_array_delete_safe_at() - Replaces the program at the given 2381 * index into the program array with 2382 * a dummy no-op program. 2383 * @array: a bpf_prog_array 2384 * @index: the index of the program to replace 2385 * 2386 * Skips over dummy programs, by not counting them, when calculating 2387 * the position of the program to replace. 2388 * 2389 * Return: 2390 * * 0 - Success 2391 * * -EINVAL - Invalid index value. Must be a non-negative integer. 2392 * * -ENOENT - Index out of range 2393 */ 2394 int bpf_prog_array_delete_safe_at(struct bpf_prog_array *array, int index) 2395 { 2396 return bpf_prog_array_update_at(array, index, &dummy_bpf_prog.prog); 2397 } 2398 2399 /** 2400 * bpf_prog_array_update_at() - Updates the program at the given index 2401 * into the program array. 2402 * @array: a bpf_prog_array 2403 * @index: the index of the program to update 2404 * @prog: the program to insert into the array 2405 * 2406 * Skips over dummy programs, by not counting them, when calculating 2407 * the position of the program to update. 2408 * 2409 * Return: 2410 * * 0 - Success 2411 * * -EINVAL - Invalid index value. Must be a non-negative integer. 2412 * * -ENOENT - Index out of range 2413 */ 2414 int bpf_prog_array_update_at(struct bpf_prog_array *array, int index, 2415 struct bpf_prog *prog) 2416 { 2417 struct bpf_prog_array_item *item; 2418 2419 if (unlikely(index < 0)) 2420 return -EINVAL; 2421 2422 for (item = array->items; item->prog; item++) { 2423 if (item->prog == &dummy_bpf_prog.prog) 2424 continue; 2425 if (!index) { 2426 WRITE_ONCE(item->prog, prog); 2427 return 0; 2428 } 2429 index--; 2430 } 2431 return -ENOENT; 2432 } 2433 2434 int bpf_prog_array_copy(struct bpf_prog_array *old_array, 2435 struct bpf_prog *exclude_prog, 2436 struct bpf_prog *include_prog, 2437 u64 bpf_cookie, 2438 struct bpf_prog_array **new_array) 2439 { 2440 int new_prog_cnt, carry_prog_cnt = 0; 2441 struct bpf_prog_array_item *existing, *new; 2442 struct bpf_prog_array *array; 2443 bool found_exclude = false; 2444 2445 /* Figure out how many existing progs we need to carry over to 2446 * the new array. 2447 */ 2448 if (old_array) { 2449 existing = old_array->items; 2450 for (; existing->prog; existing++) { 2451 if (existing->prog == exclude_prog) { 2452 found_exclude = true; 2453 continue; 2454 } 2455 if (existing->prog != &dummy_bpf_prog.prog) 2456 carry_prog_cnt++; 2457 if (existing->prog == include_prog) 2458 return -EEXIST; 2459 } 2460 } 2461 2462 if (exclude_prog && !found_exclude) 2463 return -ENOENT; 2464 2465 /* How many progs (not NULL) will be in the new array? */ 2466 new_prog_cnt = carry_prog_cnt; 2467 if (include_prog) 2468 new_prog_cnt += 1; 2469 2470 /* Do we have any prog (not NULL) in the new array? */ 2471 if (!new_prog_cnt) { 2472 *new_array = NULL; 2473 return 0; 2474 } 2475 2476 /* +1 as the end of prog_array is marked with NULL */ 2477 array = bpf_prog_array_alloc(new_prog_cnt + 1, GFP_KERNEL); 2478 if (!array) 2479 return -ENOMEM; 2480 new = array->items; 2481 2482 /* Fill in the new prog array */ 2483 if (carry_prog_cnt) { 2484 existing = old_array->items; 2485 for (; existing->prog; existing++) { 2486 if (existing->prog == exclude_prog || 2487 existing->prog == &dummy_bpf_prog.prog) 2488 continue; 2489 2490 new->prog = existing->prog; 2491 new->bpf_cookie = existing->bpf_cookie; 2492 new++; 2493 } 2494 } 2495 if (include_prog) { 2496 new->prog = include_prog; 2497 new->bpf_cookie = bpf_cookie; 2498 new++; 2499 } 2500 new->prog = NULL; 2501 *new_array = array; 2502 return 0; 2503 } 2504 2505 int bpf_prog_array_copy_info(struct bpf_prog_array *array, 2506 u32 *prog_ids, u32 request_cnt, 2507 u32 *prog_cnt) 2508 { 2509 u32 cnt = 0; 2510 2511 if (array) 2512 cnt = bpf_prog_array_length(array); 2513 2514 *prog_cnt = cnt; 2515 2516 /* return early if user requested only program count or nothing to copy */ 2517 if (!request_cnt || !cnt) 2518 return 0; 2519 2520 /* this function is called under trace/bpf_trace.c: bpf_event_mutex */ 2521 return bpf_prog_array_copy_core(array, prog_ids, request_cnt) ? -ENOSPC 2522 : 0; 2523 } 2524 2525 void __bpf_free_used_maps(struct bpf_prog_aux *aux, 2526 struct bpf_map **used_maps, u32 len) 2527 { 2528 struct bpf_map *map; 2529 u32 i; 2530 2531 for (i = 0; i < len; i++) { 2532 map = used_maps[i]; 2533 if (map->ops->map_poke_untrack) 2534 map->ops->map_poke_untrack(map, aux); 2535 bpf_map_put(map); 2536 } 2537 } 2538 2539 static void bpf_free_used_maps(struct bpf_prog_aux *aux) 2540 { 2541 __bpf_free_used_maps(aux, aux->used_maps, aux->used_map_cnt); 2542 kfree(aux->used_maps); 2543 } 2544 2545 void __bpf_free_used_btfs(struct bpf_prog_aux *aux, 2546 struct btf_mod_pair *used_btfs, u32 len) 2547 { 2548 #ifdef CONFIG_BPF_SYSCALL 2549 struct btf_mod_pair *btf_mod; 2550 u32 i; 2551 2552 for (i = 0; i < len; i++) { 2553 btf_mod = &used_btfs[i]; 2554 if (btf_mod->module) 2555 module_put(btf_mod->module); 2556 btf_put(btf_mod->btf); 2557 } 2558 #endif 2559 } 2560 2561 static void bpf_free_used_btfs(struct bpf_prog_aux *aux) 2562 { 2563 __bpf_free_used_btfs(aux, aux->used_btfs, aux->used_btf_cnt); 2564 kfree(aux->used_btfs); 2565 } 2566 2567 static void bpf_prog_free_deferred(struct work_struct *work) 2568 { 2569 struct bpf_prog_aux *aux; 2570 int i; 2571 2572 aux = container_of(work, struct bpf_prog_aux, work); 2573 #ifdef CONFIG_BPF_SYSCALL 2574 bpf_free_kfunc_btf_tab(aux->kfunc_btf_tab); 2575 #endif 2576 #ifdef CONFIG_CGROUP_BPF 2577 if (aux->cgroup_atype != CGROUP_BPF_ATTACH_TYPE_INVALID) 2578 bpf_cgroup_atype_put(aux->cgroup_atype); 2579 #endif 2580 bpf_free_used_maps(aux); 2581 bpf_free_used_btfs(aux); 2582 if (bpf_prog_is_dev_bound(aux)) 2583 bpf_prog_offload_destroy(aux->prog); 2584 #ifdef CONFIG_PERF_EVENTS 2585 if (aux->prog->has_callchain_buf) 2586 put_callchain_buffers(); 2587 #endif 2588 if (aux->dst_trampoline) 2589 bpf_trampoline_put(aux->dst_trampoline); 2590 for (i = 0; i < aux->func_cnt; i++) { 2591 /* We can just unlink the subprog poke descriptor table as 2592 * it was originally linked to the main program and is also 2593 * released along with it. 2594 */ 2595 aux->func[i]->aux->poke_tab = NULL; 2596 bpf_jit_free(aux->func[i]); 2597 } 2598 if (aux->func_cnt) { 2599 kfree(aux->func); 2600 bpf_prog_unlock_free(aux->prog); 2601 } else { 2602 bpf_jit_free(aux->prog); 2603 } 2604 } 2605 2606 void bpf_prog_free(struct bpf_prog *fp) 2607 { 2608 struct bpf_prog_aux *aux = fp->aux; 2609 2610 if (aux->dst_prog) 2611 bpf_prog_put(aux->dst_prog); 2612 INIT_WORK(&aux->work, bpf_prog_free_deferred); 2613 schedule_work(&aux->work); 2614 } 2615 EXPORT_SYMBOL_GPL(bpf_prog_free); 2616 2617 /* RNG for unpriviledged user space with separated state from prandom_u32(). */ 2618 static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state); 2619 2620 void bpf_user_rnd_init_once(void) 2621 { 2622 prandom_init_once(&bpf_user_rnd_state); 2623 } 2624 2625 BPF_CALL_0(bpf_user_rnd_u32) 2626 { 2627 /* Should someone ever have the rather unwise idea to use some 2628 * of the registers passed into this function, then note that 2629 * this function is called from native eBPF and classic-to-eBPF 2630 * transformations. Register assignments from both sides are 2631 * different, f.e. classic always sets fn(ctx, A, X) here. 2632 */ 2633 struct rnd_state *state; 2634 u32 res; 2635 2636 state = &get_cpu_var(bpf_user_rnd_state); 2637 res = prandom_u32_state(state); 2638 put_cpu_var(bpf_user_rnd_state); 2639 2640 return res; 2641 } 2642 2643 BPF_CALL_0(bpf_get_raw_cpu_id) 2644 { 2645 return raw_smp_processor_id(); 2646 } 2647 2648 /* Weak definitions of helper functions in case we don't have bpf syscall. */ 2649 const struct bpf_func_proto bpf_map_lookup_elem_proto __weak; 2650 const struct bpf_func_proto bpf_map_update_elem_proto __weak; 2651 const struct bpf_func_proto bpf_map_delete_elem_proto __weak; 2652 const struct bpf_func_proto bpf_map_push_elem_proto __weak; 2653 const struct bpf_func_proto bpf_map_pop_elem_proto __weak; 2654 const struct bpf_func_proto bpf_map_peek_elem_proto __weak; 2655 const struct bpf_func_proto bpf_map_lookup_percpu_elem_proto __weak; 2656 const struct bpf_func_proto bpf_spin_lock_proto __weak; 2657 const struct bpf_func_proto bpf_spin_unlock_proto __weak; 2658 const struct bpf_func_proto bpf_jiffies64_proto __weak; 2659 2660 const struct bpf_func_proto bpf_get_prandom_u32_proto __weak; 2661 const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak; 2662 const struct bpf_func_proto bpf_get_numa_node_id_proto __weak; 2663 const struct bpf_func_proto bpf_ktime_get_ns_proto __weak; 2664 const struct bpf_func_proto bpf_ktime_get_boot_ns_proto __weak; 2665 const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto __weak; 2666 2667 const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak; 2668 const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak; 2669 const struct bpf_func_proto bpf_get_current_comm_proto __weak; 2670 const struct bpf_func_proto bpf_get_current_cgroup_id_proto __weak; 2671 const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto __weak; 2672 const struct bpf_func_proto bpf_get_local_storage_proto __weak; 2673 const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto __weak; 2674 const struct bpf_func_proto bpf_snprintf_btf_proto __weak; 2675 const struct bpf_func_proto bpf_seq_printf_btf_proto __weak; 2676 const struct bpf_func_proto bpf_set_retval_proto __weak; 2677 const struct bpf_func_proto bpf_get_retval_proto __weak; 2678 2679 const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void) 2680 { 2681 return NULL; 2682 } 2683 2684 const struct bpf_func_proto * __weak bpf_get_trace_vprintk_proto(void) 2685 { 2686 return NULL; 2687 } 2688 2689 u64 __weak 2690 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size, 2691 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy) 2692 { 2693 return -ENOTSUPP; 2694 } 2695 EXPORT_SYMBOL_GPL(bpf_event_output); 2696 2697 /* Always built-in helper functions. */ 2698 const struct bpf_func_proto bpf_tail_call_proto = { 2699 .func = NULL, 2700 .gpl_only = false, 2701 .ret_type = RET_VOID, 2702 .arg1_type = ARG_PTR_TO_CTX, 2703 .arg2_type = ARG_CONST_MAP_PTR, 2704 .arg3_type = ARG_ANYTHING, 2705 }; 2706 2707 /* Stub for JITs that only support cBPF. eBPF programs are interpreted. 2708 * It is encouraged to implement bpf_int_jit_compile() instead, so that 2709 * eBPF and implicitly also cBPF can get JITed! 2710 */ 2711 struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog) 2712 { 2713 return prog; 2714 } 2715 2716 /* Stub for JITs that support eBPF. All cBPF code gets transformed into 2717 * eBPF by the kernel and is later compiled by bpf_int_jit_compile(). 2718 */ 2719 void __weak bpf_jit_compile(struct bpf_prog *prog) 2720 { 2721 } 2722 2723 bool __weak bpf_helper_changes_pkt_data(void *func) 2724 { 2725 return false; 2726 } 2727 2728 /* Return TRUE if the JIT backend wants verifier to enable sub-register usage 2729 * analysis code and wants explicit zero extension inserted by verifier. 2730 * Otherwise, return FALSE. 2731 * 2732 * The verifier inserts an explicit zero extension after BPF_CMPXCHGs even if 2733 * you don't override this. JITs that don't want these extra insns can detect 2734 * them using insn_is_zext. 2735 */ 2736 bool __weak bpf_jit_needs_zext(void) 2737 { 2738 return false; 2739 } 2740 2741 /* Return TRUE if the JIT backend supports mixing bpf2bpf and tailcalls. */ 2742 bool __weak bpf_jit_supports_subprog_tailcalls(void) 2743 { 2744 return false; 2745 } 2746 2747 bool __weak bpf_jit_supports_kfunc_call(void) 2748 { 2749 return false; 2750 } 2751 2752 /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call 2753 * skb_copy_bits(), so provide a weak definition of it for NET-less config. 2754 */ 2755 int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to, 2756 int len) 2757 { 2758 return -EFAULT; 2759 } 2760 2761 int __weak bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t, 2762 void *addr1, void *addr2) 2763 { 2764 return -ENOTSUPP; 2765 } 2766 2767 void * __weak bpf_arch_text_copy(void *dst, void *src, size_t len) 2768 { 2769 return ERR_PTR(-ENOTSUPP); 2770 } 2771 2772 int __weak bpf_arch_text_invalidate(void *dst, size_t len) 2773 { 2774 return -ENOTSUPP; 2775 } 2776 2777 DEFINE_STATIC_KEY_FALSE(bpf_stats_enabled_key); 2778 EXPORT_SYMBOL(bpf_stats_enabled_key); 2779 2780 /* All definitions of tracepoints related to BPF. */ 2781 #define CREATE_TRACE_POINTS 2782 #include <linux/bpf_trace.h> 2783 2784 EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception); 2785 EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_bulk_tx); 2786