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