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