2 * Linux Socket Filter - Kernel level socket filtering
4 * Based on the design of the Berkeley Packet Filter. The new
5 * internal format has been designed by PLUMgrid:
7 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
11 * Jay Schulist <jschlst@samba.org>
12 * Alexei Starovoitov <ast@plumgrid.com>
13 * Daniel Borkmann <dborkman@redhat.com>
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
20 * Andi Kleen - Fix a few bad bugs and races.
21 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
24 #include <linux/filter.h>
25 #include <linux/skbuff.h>
26 #include <linux/vmalloc.h>
27 #include <linux/random.h>
28 #include <linux/moduleloader.h>
29 #include <linux/bpf.h>
30 #include <linux/frame.h>
31 #include <linux/rbtree_latch.h>
32 #include <linux/kallsyms.h>
33 #include <linux/rcupdate.h>
34 #include <linux/perf_event.h>
36 #include <asm/barrier.h>
37 #include <asm/unaligned.h>
40 #define BPF_R0 regs[BPF_REG_0]
41 #define BPF_R1 regs[BPF_REG_1]
42 #define BPF_R2 regs[BPF_REG_2]
43 #define BPF_R3 regs[BPF_REG_3]
44 #define BPF_R4 regs[BPF_REG_4]
45 #define BPF_R5 regs[BPF_REG_5]
46 #define BPF_R6 regs[BPF_REG_6]
47 #define BPF_R7 regs[BPF_REG_7]
48 #define BPF_R8 regs[BPF_REG_8]
49 #define BPF_R9 regs[BPF_REG_9]
50 #define BPF_R10 regs[BPF_REG_10]
53 #define DST regs[insn->dst_reg]
54 #define SRC regs[insn->src_reg]
55 #define FP regs[BPF_REG_FP]
56 #define AX regs[BPF_REG_AX]
57 #define ARG1 regs[BPF_REG_ARG1]
58 #define CTX regs[BPF_REG_CTX]
61 /* No hurry in this branch
63 * Exported for the bpf jit load helper.
65 void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
70 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
71 else if (k >= SKF_LL_OFF)
72 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
74 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
80 struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
82 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
83 struct bpf_prog_aux *aux;
86 size = round_up(size, PAGE_SIZE);
87 fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
91 aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);
97 fp->pages = size / PAGE_SIZE;
100 fp->jit_requested = ebpf_jit_enabled();
102 INIT_LIST_HEAD_RCU(&fp->aux->ksym_lnode);
106 EXPORT_SYMBOL_GPL(bpf_prog_alloc);
108 struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
109 gfp_t gfp_extra_flags)
111 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
116 BUG_ON(fp_old == NULL);
118 size = round_up(size, PAGE_SIZE);
119 pages = size / PAGE_SIZE;
120 if (pages <= fp_old->pages)
123 delta = pages - fp_old->pages;
124 ret = __bpf_prog_charge(fp_old->aux->user, delta);
128 fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
130 __bpf_prog_uncharge(fp_old->aux->user, delta);
132 memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
136 /* We keep fp->aux from fp_old around in the new
137 * reallocated structure.
140 __bpf_prog_free(fp_old);
146 void __bpf_prog_free(struct bpf_prog *fp)
152 int bpf_prog_calc_tag(struct bpf_prog *fp)
154 const u32 bits_offset = SHA_MESSAGE_BYTES - sizeof(__be64);
155 u32 raw_size = bpf_prog_tag_scratch_size(fp);
156 u32 digest[SHA_DIGEST_WORDS];
157 u32 ws[SHA_WORKSPACE_WORDS];
158 u32 i, bsize, psize, blocks;
159 struct bpf_insn *dst;
165 raw = vmalloc(raw_size);
170 memset(ws, 0, sizeof(ws));
172 /* We need to take out the map fd for the digest calculation
173 * since they are unstable from user space side.
176 for (i = 0, was_ld_map = false; i < fp->len; i++) {
177 dst[i] = fp->insnsi[i];
179 dst[i].code == (BPF_LD | BPF_IMM | BPF_DW) &&
180 dst[i].src_reg == BPF_PSEUDO_MAP_FD) {
183 } else if (was_ld_map &&
185 dst[i].dst_reg == 0 &&
186 dst[i].src_reg == 0 &&
195 psize = bpf_prog_insn_size(fp);
196 memset(&raw[psize], 0, raw_size - psize);
199 bsize = round_up(psize, SHA_MESSAGE_BYTES);
200 blocks = bsize / SHA_MESSAGE_BYTES;
202 if (bsize - psize >= sizeof(__be64)) {
203 bits = (__be64 *)(todo + bsize - sizeof(__be64));
205 bits = (__be64 *)(todo + bsize + bits_offset);
208 *bits = cpu_to_be64((psize - 1) << 3);
211 sha_transform(digest, todo, ws);
212 todo += SHA_MESSAGE_BYTES;
215 result = (__force __be32 *)digest;
216 for (i = 0; i < SHA_DIGEST_WORDS; i++)
217 result[i] = cpu_to_be32(digest[i]);
218 memcpy(fp->tag, result, sizeof(fp->tag));
224 static int bpf_adj_delta_to_imm(struct bpf_insn *insn, u32 pos, u32 delta,
225 u32 curr, const bool probe_pass)
227 const s64 imm_min = S32_MIN, imm_max = S32_MAX;
230 if (curr < pos && curr + imm + 1 > pos)
232 else if (curr > pos + delta && curr + imm + 1 <= pos + delta)
234 if (imm < imm_min || imm > imm_max)
241 static int bpf_adj_delta_to_off(struct bpf_insn *insn, u32 pos, u32 delta,
242 u32 curr, const bool probe_pass)
244 const s32 off_min = S16_MIN, off_max = S16_MAX;
247 if (curr < pos && curr + off + 1 > pos)
249 else if (curr > pos + delta && curr + off + 1 <= pos + delta)
251 if (off < off_min || off > off_max)
258 static int bpf_adj_branches(struct bpf_prog *prog, u32 pos, u32 delta,
259 const bool probe_pass)
261 u32 i, insn_cnt = prog->len + (probe_pass ? delta : 0);
262 struct bpf_insn *insn = prog->insnsi;
265 for (i = 0; i < insn_cnt; i++, insn++) {
268 /* In the probing pass we still operate on the original,
269 * unpatched image in order to check overflows before we
270 * do any other adjustments. Therefore skip the patchlet.
272 if (probe_pass && i == pos) {
277 if (BPF_CLASS(code) != BPF_JMP ||
278 BPF_OP(code) == BPF_EXIT)
280 /* Adjust offset of jmps if we cross patch boundaries. */
281 if (BPF_OP(code) == BPF_CALL) {
282 if (insn->src_reg != BPF_PSEUDO_CALL)
284 ret = bpf_adj_delta_to_imm(insn, pos, delta, i,
287 ret = bpf_adj_delta_to_off(insn, pos, delta, i,
297 struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
298 const struct bpf_insn *patch, u32 len)
300 u32 insn_adj_cnt, insn_rest, insn_delta = len - 1;
301 const u32 cnt_max = S16_MAX;
302 struct bpf_prog *prog_adj;
304 /* Since our patchlet doesn't expand the image, we're done. */
305 if (insn_delta == 0) {
306 memcpy(prog->insnsi + off, patch, sizeof(*patch));
310 insn_adj_cnt = prog->len + insn_delta;
312 /* Reject anything that would potentially let the insn->off
313 * target overflow when we have excessive program expansions.
314 * We need to probe here before we do any reallocation where
315 * we afterwards may not fail anymore.
317 if (insn_adj_cnt > cnt_max &&
318 bpf_adj_branches(prog, off, insn_delta, true))
321 /* Several new instructions need to be inserted. Make room
322 * for them. Likely, there's no need for a new allocation as
323 * last page could have large enough tailroom.
325 prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),
330 prog_adj->len = insn_adj_cnt;
332 /* Patching happens in 3 steps:
334 * 1) Move over tail of insnsi from next instruction onwards,
335 * so we can patch the single target insn with one or more
336 * new ones (patching is always from 1 to n insns, n > 0).
337 * 2) Inject new instructions at the target location.
338 * 3) Adjust branch offsets if necessary.
340 insn_rest = insn_adj_cnt - off - len;
342 memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1,
343 sizeof(*patch) * insn_rest);
344 memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len);
346 /* We are guaranteed to not fail at this point, otherwise
347 * the ship has sailed to reverse to the original state. An
348 * overflow cannot happen at this point.
350 BUG_ON(bpf_adj_branches(prog_adj, off, insn_delta, false));
355 void bpf_prog_kallsyms_del_subprogs(struct bpf_prog *fp)
359 for (i = 0; i < fp->aux->func_cnt; i++)
360 bpf_prog_kallsyms_del(fp->aux->func[i]);
363 void bpf_prog_kallsyms_del_all(struct bpf_prog *fp)
365 bpf_prog_kallsyms_del_subprogs(fp);
366 bpf_prog_kallsyms_del(fp);
369 #ifdef CONFIG_BPF_JIT
370 /* All BPF JIT sysctl knobs here. */
371 int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_ALWAYS_ON);
372 int bpf_jit_harden __read_mostly;
373 int bpf_jit_kallsyms __read_mostly;
374 long bpf_jit_limit __read_mostly;
376 static __always_inline void
377 bpf_get_prog_addr_region(const struct bpf_prog *prog,
378 unsigned long *symbol_start,
379 unsigned long *symbol_end)
381 const struct bpf_binary_header *hdr = bpf_jit_binary_hdr(prog);
382 unsigned long addr = (unsigned long)hdr;
384 WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog));
386 *symbol_start = addr;
387 *symbol_end = addr + hdr->pages * PAGE_SIZE;
390 static void bpf_get_prog_name(const struct bpf_prog *prog, char *sym)
392 const char *end = sym + KSYM_NAME_LEN;
394 BUILD_BUG_ON(sizeof("bpf_prog_") +
395 sizeof(prog->tag) * 2 +
396 /* name has been null terminated.
397 * We should need +1 for the '_' preceding
398 * the name. However, the null character
399 * is double counted between the name and the
400 * sizeof("bpf_prog_") above, so we omit
403 sizeof(prog->aux->name) > KSYM_NAME_LEN);
405 sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_");
406 sym = bin2hex(sym, prog->tag, sizeof(prog->tag));
407 if (prog->aux->name[0])
408 snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name);
413 static __always_inline unsigned long
414 bpf_get_prog_addr_start(struct latch_tree_node *n)
416 unsigned long symbol_start, symbol_end;
417 const struct bpf_prog_aux *aux;
419 aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
420 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
425 static __always_inline bool bpf_tree_less(struct latch_tree_node *a,
426 struct latch_tree_node *b)
428 return bpf_get_prog_addr_start(a) < bpf_get_prog_addr_start(b);
431 static __always_inline int bpf_tree_comp(void *key, struct latch_tree_node *n)
433 unsigned long val = (unsigned long)key;
434 unsigned long symbol_start, symbol_end;
435 const struct bpf_prog_aux *aux;
437 aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
438 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
440 if (val < symbol_start)
442 if (val >= symbol_end)
448 static const struct latch_tree_ops bpf_tree_ops = {
449 .less = bpf_tree_less,
450 .comp = bpf_tree_comp,
453 static DEFINE_SPINLOCK(bpf_lock);
454 static LIST_HEAD(bpf_kallsyms);
455 static struct latch_tree_root bpf_tree __cacheline_aligned;
457 static void bpf_prog_ksym_node_add(struct bpf_prog_aux *aux)
459 WARN_ON_ONCE(!list_empty(&aux->ksym_lnode));
460 list_add_tail_rcu(&aux->ksym_lnode, &bpf_kallsyms);
461 latch_tree_insert(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
464 static void bpf_prog_ksym_node_del(struct bpf_prog_aux *aux)
466 if (list_empty(&aux->ksym_lnode))
469 latch_tree_erase(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
470 list_del_rcu(&aux->ksym_lnode);
473 static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp)
475 return fp->jited && !bpf_prog_was_classic(fp);
478 static bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp)
480 return list_empty(&fp->aux->ksym_lnode) ||
481 fp->aux->ksym_lnode.prev == LIST_POISON2;
484 void bpf_prog_kallsyms_add(struct bpf_prog *fp)
486 if (!bpf_prog_kallsyms_candidate(fp) ||
487 !capable(CAP_SYS_ADMIN))
490 spin_lock_bh(&bpf_lock);
491 bpf_prog_ksym_node_add(fp->aux);
492 spin_unlock_bh(&bpf_lock);
495 void bpf_prog_kallsyms_del(struct bpf_prog *fp)
497 if (!bpf_prog_kallsyms_candidate(fp))
500 spin_lock_bh(&bpf_lock);
501 bpf_prog_ksym_node_del(fp->aux);
502 spin_unlock_bh(&bpf_lock);
505 static struct bpf_prog *bpf_prog_kallsyms_find(unsigned long addr)
507 struct latch_tree_node *n;
509 if (!bpf_jit_kallsyms_enabled())
512 n = latch_tree_find((void *)addr, &bpf_tree, &bpf_tree_ops);
514 container_of(n, struct bpf_prog_aux, ksym_tnode)->prog :
518 const char *__bpf_address_lookup(unsigned long addr, unsigned long *size,
519 unsigned long *off, char *sym)
521 unsigned long symbol_start, symbol_end;
522 struct bpf_prog *prog;
526 prog = bpf_prog_kallsyms_find(addr);
528 bpf_get_prog_addr_region(prog, &symbol_start, &symbol_end);
529 bpf_get_prog_name(prog, sym);
533 *size = symbol_end - symbol_start;
535 *off = addr - symbol_start;
542 bool is_bpf_text_address(unsigned long addr)
547 ret = bpf_prog_kallsyms_find(addr) != NULL;
553 int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
556 unsigned long symbol_start, symbol_end;
557 struct bpf_prog_aux *aux;
561 if (!bpf_jit_kallsyms_enabled())
565 list_for_each_entry_rcu(aux, &bpf_kallsyms, ksym_lnode) {
569 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
570 bpf_get_prog_name(aux->prog, sym);
572 *value = symbol_start;
573 *type = BPF_SYM_ELF_TYPE;
583 static atomic_long_t bpf_jit_current;
585 /* Can be overridden by an arch's JIT compiler if it has a custom,
586 * dedicated BPF backend memory area, or if neither of the two
589 u64 __weak bpf_jit_alloc_exec_limit(void)
591 #if defined(MODULES_VADDR)
592 return MODULES_END - MODULES_VADDR;
594 return VMALLOC_END - VMALLOC_START;
598 static int __init bpf_jit_charge_init(void)
600 /* Only used as heuristic here to derive limit. */
601 bpf_jit_limit = min_t(u64, round_up(bpf_jit_alloc_exec_limit() >> 2,
602 PAGE_SIZE), LONG_MAX);
605 pure_initcall(bpf_jit_charge_init);
607 static int bpf_jit_charge_modmem(u32 pages)
609 if (atomic_long_add_return(pages, &bpf_jit_current) >
610 (bpf_jit_limit >> PAGE_SHIFT)) {
611 if (!capable(CAP_SYS_ADMIN)) {
612 atomic_long_sub(pages, &bpf_jit_current);
620 static void bpf_jit_uncharge_modmem(u32 pages)
622 atomic_long_sub(pages, &bpf_jit_current);
625 struct bpf_binary_header *
626 bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
627 unsigned int alignment,
628 bpf_jit_fill_hole_t bpf_fill_ill_insns)
630 struct bpf_binary_header *hdr;
631 u32 size, hole, start, pages;
633 /* Most of BPF filters are really small, but if some of them
634 * fill a page, allow at least 128 extra bytes to insert a
635 * random section of illegal instructions.
637 size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
638 pages = size / PAGE_SIZE;
640 if (bpf_jit_charge_modmem(pages))
642 hdr = module_alloc(size);
644 bpf_jit_uncharge_modmem(pages);
648 /* Fill space with illegal/arch-dep instructions. */
649 bpf_fill_ill_insns(hdr, size);
652 hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
653 PAGE_SIZE - sizeof(*hdr));
654 start = (get_random_int() % hole) & ~(alignment - 1);
656 /* Leave a random number of instructions before BPF code. */
657 *image_ptr = &hdr->image[start];
662 void bpf_jit_binary_free(struct bpf_binary_header *hdr)
664 u32 pages = hdr->pages;
667 bpf_jit_uncharge_modmem(pages);
670 /* This symbol is only overridden by archs that have different
671 * requirements than the usual eBPF JITs, f.e. when they only
672 * implement cBPF JIT, do not set images read-only, etc.
674 void __weak bpf_jit_free(struct bpf_prog *fp)
677 struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp);
679 bpf_jit_binary_unlock_ro(hdr);
680 bpf_jit_binary_free(hdr);
682 WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp));
685 bpf_prog_unlock_free(fp);
688 static int bpf_jit_blind_insn(const struct bpf_insn *from,
689 const struct bpf_insn *aux,
690 struct bpf_insn *to_buff)
692 struct bpf_insn *to = to_buff;
693 u32 imm_rnd = get_random_int();
696 BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG);
697 BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG);
699 /* Constraints on AX register:
701 * AX register is inaccessible from user space. It is mapped in
702 * all JITs, and used here for constant blinding rewrites. It is
703 * typically "stateless" meaning its contents are only valid within
704 * the executed instruction, but not across several instructions.
705 * There are a few exceptions however which are further detailed
708 * Constant blinding is only used by JITs, not in the interpreter.
709 * In restricted circumstances, the verifier can also use the AX
710 * register for rewrites as long as they do not interfere with
713 if (from->dst_reg == BPF_REG_AX || from->src_reg == BPF_REG_AX)
716 if (from->imm == 0 &&
717 (from->code == (BPF_ALU | BPF_MOV | BPF_K) ||
718 from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) {
719 *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg);
723 switch (from->code) {
724 case BPF_ALU | BPF_ADD | BPF_K:
725 case BPF_ALU | BPF_SUB | BPF_K:
726 case BPF_ALU | BPF_AND | BPF_K:
727 case BPF_ALU | BPF_OR | BPF_K:
728 case BPF_ALU | BPF_XOR | BPF_K:
729 case BPF_ALU | BPF_MUL | BPF_K:
730 case BPF_ALU | BPF_MOV | BPF_K:
731 case BPF_ALU | BPF_DIV | BPF_K:
732 case BPF_ALU | BPF_MOD | BPF_K:
733 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
734 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
735 *to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX);
738 case BPF_ALU64 | BPF_ADD | BPF_K:
739 case BPF_ALU64 | BPF_SUB | BPF_K:
740 case BPF_ALU64 | BPF_AND | BPF_K:
741 case BPF_ALU64 | BPF_OR | BPF_K:
742 case BPF_ALU64 | BPF_XOR | BPF_K:
743 case BPF_ALU64 | BPF_MUL | BPF_K:
744 case BPF_ALU64 | BPF_MOV | BPF_K:
745 case BPF_ALU64 | BPF_DIV | BPF_K:
746 case BPF_ALU64 | BPF_MOD | BPF_K:
747 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
748 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
749 *to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX);
752 case BPF_JMP | BPF_JEQ | BPF_K:
753 case BPF_JMP | BPF_JNE | BPF_K:
754 case BPF_JMP | BPF_JGT | BPF_K:
755 case BPF_JMP | BPF_JLT | BPF_K:
756 case BPF_JMP | BPF_JGE | BPF_K:
757 case BPF_JMP | BPF_JLE | BPF_K:
758 case BPF_JMP | BPF_JSGT | BPF_K:
759 case BPF_JMP | BPF_JSLT | BPF_K:
760 case BPF_JMP | BPF_JSGE | BPF_K:
761 case BPF_JMP | BPF_JSLE | BPF_K:
762 case BPF_JMP | BPF_JSET | BPF_K:
763 /* Accommodate for extra offset in case of a backjump. */
767 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
768 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
769 *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off);
772 case BPF_LD | BPF_IMM | BPF_DW:
773 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm);
774 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
775 *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
776 *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX);
778 case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */
779 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm);
780 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
781 *to++ = BPF_ALU64_REG(BPF_OR, aux[0].dst_reg, BPF_REG_AX);
784 case BPF_ST | BPF_MEM | BPF_DW:
785 case BPF_ST | BPF_MEM | BPF_W:
786 case BPF_ST | BPF_MEM | BPF_H:
787 case BPF_ST | BPF_MEM | BPF_B:
788 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
789 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
790 *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off);
797 static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other,
798 gfp_t gfp_extra_flags)
800 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
803 fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags, PAGE_KERNEL);
805 /* aux->prog still points to the fp_other one, so
806 * when promoting the clone to the real program,
807 * this still needs to be adapted.
809 memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE);
815 static void bpf_prog_clone_free(struct bpf_prog *fp)
817 /* aux was stolen by the other clone, so we cannot free
818 * it from this path! It will be freed eventually by the
819 * other program on release.
821 * At this point, we don't need a deferred release since
822 * clone is guaranteed to not be locked.
828 void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other)
830 /* We have to repoint aux->prog to self, as we don't
831 * know whether fp here is the clone or the original.
834 bpf_prog_clone_free(fp_other);
837 struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
839 struct bpf_insn insn_buff[16], aux[2];
840 struct bpf_prog *clone, *tmp;
841 int insn_delta, insn_cnt;
842 struct bpf_insn *insn;
845 if (!bpf_jit_blinding_enabled(prog) || prog->blinded)
848 clone = bpf_prog_clone_create(prog, GFP_USER);
850 return ERR_PTR(-ENOMEM);
852 insn_cnt = clone->len;
853 insn = clone->insnsi;
855 for (i = 0; i < insn_cnt; i++, insn++) {
856 /* We temporarily need to hold the original ld64 insn
857 * so that we can still access the first part in the
858 * second blinding run.
860 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) &&
862 memcpy(aux, insn, sizeof(aux));
864 rewritten = bpf_jit_blind_insn(insn, aux, insn_buff);
868 tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);
870 /* Patching may have repointed aux->prog during
871 * realloc from the original one, so we need to
872 * fix it up here on error.
874 bpf_jit_prog_release_other(prog, clone);
875 return ERR_PTR(-ENOMEM);
879 insn_delta = rewritten - 1;
881 /* Walk new program and skip insns we just inserted. */
882 insn = clone->insnsi + i + insn_delta;
883 insn_cnt += insn_delta;
890 #endif /* CONFIG_BPF_JIT */
892 /* Base function for offset calculation. Needs to go into .text section,
893 * therefore keeping it non-static as well; will also be used by JITs
894 * anyway later on, so do not let the compiler omit it. This also needs
895 * to go into kallsyms for correlation from e.g. bpftool, so naming
898 noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
902 EXPORT_SYMBOL_GPL(__bpf_call_base);
904 /* All UAPI available opcodes. */
905 #define BPF_INSN_MAP(INSN_2, INSN_3) \
906 /* 32 bit ALU operations. */ \
907 /* Register based. */ \
908 INSN_3(ALU, ADD, X), \
909 INSN_3(ALU, SUB, X), \
910 INSN_3(ALU, AND, X), \
911 INSN_3(ALU, OR, X), \
912 INSN_3(ALU, LSH, X), \
913 INSN_3(ALU, RSH, X), \
914 INSN_3(ALU, XOR, X), \
915 INSN_3(ALU, MUL, X), \
916 INSN_3(ALU, MOV, X), \
917 INSN_3(ALU, DIV, X), \
918 INSN_3(ALU, MOD, X), \
920 INSN_3(ALU, END, TO_BE), \
921 INSN_3(ALU, END, TO_LE), \
922 /* Immediate based. */ \
923 INSN_3(ALU, ADD, K), \
924 INSN_3(ALU, SUB, K), \
925 INSN_3(ALU, AND, K), \
926 INSN_3(ALU, OR, K), \
927 INSN_3(ALU, LSH, K), \
928 INSN_3(ALU, RSH, K), \
929 INSN_3(ALU, XOR, K), \
930 INSN_3(ALU, MUL, K), \
931 INSN_3(ALU, MOV, K), \
932 INSN_3(ALU, DIV, K), \
933 INSN_3(ALU, MOD, K), \
934 /* 64 bit ALU operations. */ \
935 /* Register based. */ \
936 INSN_3(ALU64, ADD, X), \
937 INSN_3(ALU64, SUB, X), \
938 INSN_3(ALU64, AND, X), \
939 INSN_3(ALU64, OR, X), \
940 INSN_3(ALU64, LSH, X), \
941 INSN_3(ALU64, RSH, X), \
942 INSN_3(ALU64, XOR, X), \
943 INSN_3(ALU64, MUL, X), \
944 INSN_3(ALU64, MOV, X), \
945 INSN_3(ALU64, ARSH, X), \
946 INSN_3(ALU64, DIV, X), \
947 INSN_3(ALU64, MOD, X), \
948 INSN_2(ALU64, NEG), \
949 /* Immediate based. */ \
950 INSN_3(ALU64, ADD, K), \
951 INSN_3(ALU64, SUB, K), \
952 INSN_3(ALU64, AND, K), \
953 INSN_3(ALU64, OR, K), \
954 INSN_3(ALU64, LSH, K), \
955 INSN_3(ALU64, RSH, K), \
956 INSN_3(ALU64, XOR, K), \
957 INSN_3(ALU64, MUL, K), \
958 INSN_3(ALU64, MOV, K), \
959 INSN_3(ALU64, ARSH, K), \
960 INSN_3(ALU64, DIV, K), \
961 INSN_3(ALU64, MOD, K), \
962 /* Call instruction. */ \
964 /* Exit instruction. */ \
966 /* Jump instructions. */ \
967 /* Register based. */ \
968 INSN_3(JMP, JEQ, X), \
969 INSN_3(JMP, JNE, X), \
970 INSN_3(JMP, JGT, X), \
971 INSN_3(JMP, JLT, X), \
972 INSN_3(JMP, JGE, X), \
973 INSN_3(JMP, JLE, X), \
974 INSN_3(JMP, JSGT, X), \
975 INSN_3(JMP, JSLT, X), \
976 INSN_3(JMP, JSGE, X), \
977 INSN_3(JMP, JSLE, X), \
978 INSN_3(JMP, JSET, X), \
979 /* Immediate based. */ \
980 INSN_3(JMP, JEQ, K), \
981 INSN_3(JMP, JNE, K), \
982 INSN_3(JMP, JGT, K), \
983 INSN_3(JMP, JLT, K), \
984 INSN_3(JMP, JGE, K), \
985 INSN_3(JMP, JLE, K), \
986 INSN_3(JMP, JSGT, K), \
987 INSN_3(JMP, JSLT, K), \
988 INSN_3(JMP, JSGE, K), \
989 INSN_3(JMP, JSLE, K), \
990 INSN_3(JMP, JSET, K), \
992 /* Store instructions. */ \
993 /* Register based. */ \
994 INSN_3(STX, MEM, B), \
995 INSN_3(STX, MEM, H), \
996 INSN_3(STX, MEM, W), \
997 INSN_3(STX, MEM, DW), \
998 INSN_3(STX, XADD, W), \
999 INSN_3(STX, XADD, DW), \
1000 /* Immediate based. */ \
1001 INSN_3(ST, MEM, B), \
1002 INSN_3(ST, MEM, H), \
1003 INSN_3(ST, MEM, W), \
1004 INSN_3(ST, MEM, DW), \
1005 /* Load instructions. */ \
1006 /* Register based. */ \
1007 INSN_3(LDX, MEM, B), \
1008 INSN_3(LDX, MEM, H), \
1009 INSN_3(LDX, MEM, W), \
1010 INSN_3(LDX, MEM, DW), \
1011 /* Immediate based. */ \
1014 bool bpf_opcode_in_insntable(u8 code)
1016 #define BPF_INSN_2_TBL(x, y) [BPF_##x | BPF_##y] = true
1017 #define BPF_INSN_3_TBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = true
1018 static const bool public_insntable[256] = {
1019 [0 ... 255] = false,
1020 /* Now overwrite non-defaults ... */
1021 BPF_INSN_MAP(BPF_INSN_2_TBL, BPF_INSN_3_TBL),
1022 /* UAPI exposed, but rewritten opcodes. cBPF carry-over. */
1023 [BPF_LD | BPF_ABS | BPF_B] = true,
1024 [BPF_LD | BPF_ABS | BPF_H] = true,
1025 [BPF_LD | BPF_ABS | BPF_W] = true,
1026 [BPF_LD | BPF_IND | BPF_B] = true,
1027 [BPF_LD | BPF_IND | BPF_H] = true,
1028 [BPF_LD | BPF_IND | BPF_W] = true,
1030 #undef BPF_INSN_3_TBL
1031 #undef BPF_INSN_2_TBL
1032 return public_insntable[code];
1035 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
1037 * __bpf_prog_run - run eBPF program on a given context
1038 * @ctx: is the data we are operating on
1039 * @insn: is the array of eBPF instructions
1041 * Decode and execute eBPF instructions.
1043 static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack)
1045 #define BPF_INSN_2_LBL(x, y) [BPF_##x | BPF_##y] = &&x##_##y
1046 #define BPF_INSN_3_LBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = &&x##_##y##_##z
1047 static const void *jumptable[256] = {
1048 [0 ... 255] = &&default_label,
1049 /* Now overwrite non-defaults ... */
1050 BPF_INSN_MAP(BPF_INSN_2_LBL, BPF_INSN_3_LBL),
1051 /* Non-UAPI available opcodes. */
1052 [BPF_JMP | BPF_CALL_ARGS] = &&JMP_CALL_ARGS,
1053 [BPF_JMP | BPF_TAIL_CALL] = &&JMP_TAIL_CALL,
1054 [BPF_ST | BPF_NOSPEC] = &&ST_NOSPEC,
1056 #undef BPF_INSN_3_LBL
1057 #undef BPF_INSN_2_LBL
1058 u32 tail_call_cnt = 0;
1061 #define CONT ({ insn++; goto select_insn; })
1062 #define CONT_JMP ({ insn++; goto select_insn; })
1065 goto *jumptable[insn->code];
1068 #define ALU(OPCODE, OP) \
1069 ALU64_##OPCODE##_X: \
1073 DST = (u32) DST OP (u32) SRC; \
1075 ALU64_##OPCODE##_K: \
1079 DST = (u32) DST OP (u32) IMM; \
1110 DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
1114 (*(s64 *) &DST) >>= SRC;
1117 (*(s64 *) &DST) >>= IMM;
1120 div64_u64_rem(DST, SRC, &tmp);
1125 DST = do_div(tmp, (u32) SRC);
1128 div64_u64_rem(DST, IMM, &tmp);
1133 DST = do_div(tmp, (u32) IMM);
1136 DST = div64_u64(DST, SRC);
1140 do_div(tmp, (u32) SRC);
1144 DST = div64_u64(DST, IMM);
1148 do_div(tmp, (u32) IMM);
1154 DST = (__force u16) cpu_to_be16(DST);
1157 DST = (__force u32) cpu_to_be32(DST);
1160 DST = (__force u64) cpu_to_be64(DST);
1167 DST = (__force u16) cpu_to_le16(DST);
1170 DST = (__force u32) cpu_to_le32(DST);
1173 DST = (__force u64) cpu_to_le64(DST);
1180 /* Function call scratches BPF_R1-BPF_R5 registers,
1181 * preserves BPF_R6-BPF_R9, and stores return value
1184 BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
1189 BPF_R0 = (__bpf_call_base_args + insn->imm)(BPF_R1, BPF_R2,
1192 insn + insn->off + 1);
1196 struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
1197 struct bpf_array *array = container_of(map, struct bpf_array, map);
1198 struct bpf_prog *prog;
1201 if (unlikely(index >= array->map.max_entries))
1203 if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
1208 prog = READ_ONCE(array->ptrs[index]);
1212 /* ARG1 at this point is guaranteed to point to CTX from
1213 * the verifier side due to the fact that the tail call is
1214 * handeled like a helper, that is, bpf_tail_call_proto,
1215 * where arg1_type is ARG_PTR_TO_CTX.
1217 insn = prog->insnsi;
1299 if (((s64) DST) > ((s64) SRC)) {
1305 if (((s64) DST) > ((s64) IMM)) {
1311 if (((s64) DST) < ((s64) SRC)) {
1317 if (((s64) DST) < ((s64) IMM)) {
1323 if (((s64) DST) >= ((s64) SRC)) {
1329 if (((s64) DST) >= ((s64) IMM)) {
1335 if (((s64) DST) <= ((s64) SRC)) {
1341 if (((s64) DST) <= ((s64) IMM)) {
1361 /* ST, STX and LDX*/
1363 /* Speculation barrier for mitigating Speculative Store Bypass.
1364 * In case of arm64, we rely on the firmware mitigation as
1365 * controlled via the ssbd kernel parameter. Whenever the
1366 * mitigation is enabled, it works for all of the kernel code
1367 * with no need to provide any additional instructions here.
1368 * In case of x86, we use 'lfence' insn for mitigation. We
1369 * reuse preexisting logic from Spectre v1 mitigation that
1370 * happens to produce the required code on x86 for v4 as well.
1376 #define LDST(SIZEOP, SIZE) \
1378 *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \
1381 *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \
1384 DST = *(SIZE *)(unsigned long) (SRC + insn->off); \
1392 STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
1393 atomic_add((u32) SRC, (atomic_t *)(unsigned long)
1396 STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
1397 atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
1402 /* If we ever reach this, we have a bug somewhere. Die hard here
1403 * instead of just returning 0; we could be somewhere in a subprog,
1404 * so execution could continue otherwise which we do /not/ want.
1406 * Note, verifier whitelists all opcodes in bpf_opcode_in_insntable().
1408 pr_warn("BPF interpreter: unknown opcode %02x\n", insn->code);
1412 STACK_FRAME_NON_STANDARD(___bpf_prog_run); /* jump table */
1414 #define PROG_NAME(stack_size) __bpf_prog_run##stack_size
1415 #define DEFINE_BPF_PROG_RUN(stack_size) \
1416 static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \
1418 u64 stack[stack_size / sizeof(u64)]; \
1419 u64 regs[MAX_BPF_EXT_REG]; \
1421 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1422 ARG1 = (u64) (unsigned long) ctx; \
1423 return ___bpf_prog_run(regs, insn, stack); \
1426 #define PROG_NAME_ARGS(stack_size) __bpf_prog_run_args##stack_size
1427 #define DEFINE_BPF_PROG_RUN_ARGS(stack_size) \
1428 static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \
1429 const struct bpf_insn *insn) \
1431 u64 stack[stack_size / sizeof(u64)]; \
1432 u64 regs[MAX_BPF_EXT_REG]; \
1434 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1440 return ___bpf_prog_run(regs, insn, stack); \
1443 #define EVAL1(FN, X) FN(X)
1444 #define EVAL2(FN, X, Y...) FN(X) EVAL1(FN, Y)
1445 #define EVAL3(FN, X, Y...) FN(X) EVAL2(FN, Y)
1446 #define EVAL4(FN, X, Y...) FN(X) EVAL3(FN, Y)
1447 #define EVAL5(FN, X, Y...) FN(X) EVAL4(FN, Y)
1448 #define EVAL6(FN, X, Y...) FN(X) EVAL5(FN, Y)
1450 EVAL6(DEFINE_BPF_PROG_RUN, 32, 64, 96, 128, 160, 192);
1451 EVAL6(DEFINE_BPF_PROG_RUN, 224, 256, 288, 320, 352, 384);
1452 EVAL4(DEFINE_BPF_PROG_RUN, 416, 448, 480, 512);
1454 EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 32, 64, 96, 128, 160, 192);
1455 EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 224, 256, 288, 320, 352, 384);
1456 EVAL4(DEFINE_BPF_PROG_RUN_ARGS, 416, 448, 480, 512);
1458 #define PROG_NAME_LIST(stack_size) PROG_NAME(stack_size),
1460 static unsigned int (*interpreters[])(const void *ctx,
1461 const struct bpf_insn *insn) = {
1462 EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1463 EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1464 EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1466 #undef PROG_NAME_LIST
1467 #define PROG_NAME_LIST(stack_size) PROG_NAME_ARGS(stack_size),
1468 static u64 (*interpreters_args[])(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5,
1469 const struct bpf_insn *insn) = {
1470 EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1471 EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1472 EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1474 #undef PROG_NAME_LIST
1476 void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth)
1478 stack_depth = max_t(u32, stack_depth, 1);
1479 insn->off = (s16) insn->imm;
1480 insn->imm = interpreters_args[(round_up(stack_depth, 32) / 32) - 1] -
1481 __bpf_call_base_args;
1482 insn->code = BPF_JMP | BPF_CALL_ARGS;
1486 static unsigned int __bpf_prog_ret0_warn(const void *ctx,
1487 const struct bpf_insn *insn)
1489 /* If this handler ever gets executed, then BPF_JIT_ALWAYS_ON
1490 * is not working properly, so warn about it!
1497 bool bpf_prog_array_compatible(struct bpf_array *array,
1498 const struct bpf_prog *fp)
1500 if (fp->kprobe_override)
1503 if (!array->owner_prog_type) {
1504 /* There's no owner yet where we could check for
1507 array->owner_prog_type = fp->type;
1508 array->owner_jited = fp->jited;
1513 return array->owner_prog_type == fp->type &&
1514 array->owner_jited == fp->jited;
1517 static int bpf_check_tail_call(const struct bpf_prog *fp)
1519 struct bpf_prog_aux *aux = fp->aux;
1522 for (i = 0; i < aux->used_map_cnt; i++) {
1523 struct bpf_map *map = aux->used_maps[i];
1524 struct bpf_array *array;
1526 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1529 array = container_of(map, struct bpf_array, map);
1530 if (!bpf_prog_array_compatible(array, fp))
1537 static void bpf_prog_select_func(struct bpf_prog *fp)
1539 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
1540 u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1);
1542 fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1];
1544 fp->bpf_func = __bpf_prog_ret0_warn;
1549 * bpf_prog_select_runtime - select exec runtime for BPF program
1550 * @fp: bpf_prog populated with internal BPF program
1551 * @err: pointer to error variable
1553 * Try to JIT eBPF program, if JIT is not available, use interpreter.
1554 * The BPF program will be executed via BPF_PROG_RUN() macro.
1556 struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
1558 /* In case of BPF to BPF calls, verifier did all the prep
1559 * work with regards to JITing, etc.
1564 bpf_prog_select_func(fp);
1566 /* eBPF JITs can rewrite the program in case constant
1567 * blinding is active. However, in case of error during
1568 * blinding, bpf_int_jit_compile() must always return a
1569 * valid program, which in this case would simply not
1570 * be JITed, but falls back to the interpreter.
1572 if (!bpf_prog_is_dev_bound(fp->aux)) {
1573 fp = bpf_int_jit_compile(fp);
1574 #ifdef CONFIG_BPF_JIT_ALWAYS_ON
1581 *err = bpf_prog_offload_compile(fp);
1587 bpf_prog_lock_ro(fp);
1589 /* The tail call compatibility check can only be done at
1590 * this late stage as we need to determine, if we deal
1591 * with JITed or non JITed program concatenations and not
1592 * all eBPF JITs might immediately support all features.
1594 *err = bpf_check_tail_call(fp);
1598 EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
1600 static unsigned int __bpf_prog_ret1(const void *ctx,
1601 const struct bpf_insn *insn)
1606 static struct bpf_prog_dummy {
1607 struct bpf_prog prog;
1608 } dummy_bpf_prog = {
1610 .bpf_func = __bpf_prog_ret1,
1614 /* to avoid allocating empty bpf_prog_array for cgroups that
1615 * don't have bpf program attached use one global 'empty_prog_array'
1616 * It will not be modified the caller of bpf_prog_array_alloc()
1617 * (since caller requested prog_cnt == 0)
1618 * that pointer should be 'freed' by bpf_prog_array_free()
1621 struct bpf_prog_array hdr;
1622 struct bpf_prog *null_prog;
1623 } empty_prog_array = {
1627 struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags)
1630 return kzalloc(sizeof(struct bpf_prog_array) +
1631 sizeof(struct bpf_prog_array_item) *
1635 return &empty_prog_array.hdr;
1638 void bpf_prog_array_free(struct bpf_prog_array __rcu *progs)
1641 progs == (struct bpf_prog_array __rcu *)&empty_prog_array.hdr)
1643 kfree_rcu(progs, rcu);
1646 int bpf_prog_array_length(struct bpf_prog_array __rcu *array)
1648 struct bpf_prog_array_item *item;
1652 item = rcu_dereference(array)->items;
1653 for (; item->prog; item++)
1654 if (item->prog != &dummy_bpf_prog.prog)
1661 static bool bpf_prog_array_copy_core(struct bpf_prog_array __rcu *array,
1665 struct bpf_prog_array_item *item;
1668 item = rcu_dereference_check(array, 1)->items;
1669 for (; item->prog; item++) {
1670 if (item->prog == &dummy_bpf_prog.prog)
1672 prog_ids[i] = item->prog->aux->id;
1673 if (++i == request_cnt) {
1679 return !!(item->prog);
1682 int bpf_prog_array_copy_to_user(struct bpf_prog_array __rcu *array,
1683 __u32 __user *prog_ids, u32 cnt)
1685 unsigned long err = 0;
1689 /* users of this function are doing:
1690 * cnt = bpf_prog_array_length();
1692 * bpf_prog_array_copy_to_user(..., cnt);
1693 * so below kcalloc doesn't need extra cnt > 0 check, but
1694 * bpf_prog_array_length() releases rcu lock and
1695 * prog array could have been swapped with empty or larger array,
1696 * so always copy 'cnt' prog_ids to the user.
1697 * In a rare race the user will see zero prog_ids
1699 ids = kcalloc(cnt, sizeof(u32), GFP_USER | __GFP_NOWARN);
1703 nospc = bpf_prog_array_copy_core(array, ids, cnt);
1705 err = copy_to_user(prog_ids, ids, cnt * sizeof(u32));
1714 void bpf_prog_array_delete_safe(struct bpf_prog_array __rcu *array,
1715 struct bpf_prog *old_prog)
1717 struct bpf_prog_array_item *item = array->items;
1719 for (; item->prog; item++)
1720 if (item->prog == old_prog) {
1721 WRITE_ONCE(item->prog, &dummy_bpf_prog.prog);
1726 int bpf_prog_array_copy(struct bpf_prog_array __rcu *old_array,
1727 struct bpf_prog *exclude_prog,
1728 struct bpf_prog *include_prog,
1729 struct bpf_prog_array **new_array)
1731 int new_prog_cnt, carry_prog_cnt = 0;
1732 struct bpf_prog_array_item *existing;
1733 struct bpf_prog_array *array;
1734 bool found_exclude = false;
1735 int new_prog_idx = 0;
1737 /* Figure out how many existing progs we need to carry over to
1741 existing = old_array->items;
1742 for (; existing->prog; existing++) {
1743 if (existing->prog == exclude_prog) {
1744 found_exclude = true;
1747 if (existing->prog != &dummy_bpf_prog.prog)
1749 if (existing->prog == include_prog)
1754 if (exclude_prog && !found_exclude)
1757 /* How many progs (not NULL) will be in the new array? */
1758 new_prog_cnt = carry_prog_cnt;
1762 /* Do we have any prog (not NULL) in the new array? */
1763 if (!new_prog_cnt) {
1768 /* +1 as the end of prog_array is marked with NULL */
1769 array = bpf_prog_array_alloc(new_prog_cnt + 1, GFP_KERNEL);
1773 /* Fill in the new prog array */
1774 if (carry_prog_cnt) {
1775 existing = old_array->items;
1776 for (; existing->prog; existing++)
1777 if (existing->prog != exclude_prog &&
1778 existing->prog != &dummy_bpf_prog.prog) {
1779 array->items[new_prog_idx++].prog =
1784 array->items[new_prog_idx++].prog = include_prog;
1785 array->items[new_prog_idx].prog = NULL;
1790 int bpf_prog_array_copy_info(struct bpf_prog_array __rcu *array,
1791 u32 *prog_ids, u32 request_cnt,
1797 cnt = bpf_prog_array_length(array);
1801 /* return early if user requested only program count or nothing to copy */
1802 if (!request_cnt || !cnt)
1805 /* this function is called under trace/bpf_trace.c: bpf_event_mutex */
1806 return bpf_prog_array_copy_core(array, prog_ids, request_cnt) ? -ENOSPC
1810 static void bpf_prog_free_deferred(struct work_struct *work)
1812 struct bpf_prog_aux *aux;
1815 aux = container_of(work, struct bpf_prog_aux, work);
1816 if (bpf_prog_is_dev_bound(aux))
1817 bpf_prog_offload_destroy(aux->prog);
1818 #ifdef CONFIG_PERF_EVENTS
1819 if (aux->prog->has_callchain_buf)
1820 put_callchain_buffers();
1822 for (i = 0; i < aux->func_cnt; i++)
1823 bpf_jit_free(aux->func[i]);
1824 if (aux->func_cnt) {
1826 bpf_prog_unlock_free(aux->prog);
1828 bpf_jit_free(aux->prog);
1832 /* Free internal BPF program */
1833 void bpf_prog_free(struct bpf_prog *fp)
1835 struct bpf_prog_aux *aux = fp->aux;
1837 INIT_WORK(&aux->work, bpf_prog_free_deferred);
1838 schedule_work(&aux->work);
1840 EXPORT_SYMBOL_GPL(bpf_prog_free);
1842 /* RNG for unpriviledged user space with separated state from prandom_u32(). */
1843 static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
1845 void bpf_user_rnd_init_once(void)
1847 prandom_init_once(&bpf_user_rnd_state);
1850 BPF_CALL_0(bpf_user_rnd_u32)
1852 /* Should someone ever have the rather unwise idea to use some
1853 * of the registers passed into this function, then note that
1854 * this function is called from native eBPF and classic-to-eBPF
1855 * transformations. Register assignments from both sides are
1856 * different, f.e. classic always sets fn(ctx, A, X) here.
1858 struct rnd_state *state;
1861 state = &get_cpu_var(bpf_user_rnd_state);
1862 res = prandom_u32_state(state);
1863 put_cpu_var(bpf_user_rnd_state);
1868 /* Weak definitions of helper functions in case we don't have bpf syscall. */
1869 const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
1870 const struct bpf_func_proto bpf_map_update_elem_proto __weak;
1871 const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
1873 const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
1874 const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
1875 const struct bpf_func_proto bpf_get_numa_node_id_proto __weak;
1876 const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
1878 const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
1879 const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
1880 const struct bpf_func_proto bpf_get_current_comm_proto __weak;
1881 const struct bpf_func_proto bpf_sock_map_update_proto __weak;
1882 const struct bpf_func_proto bpf_sock_hash_update_proto __weak;
1883 const struct bpf_func_proto bpf_get_current_cgroup_id_proto __weak;
1884 const struct bpf_func_proto bpf_get_local_storage_proto __weak;
1886 const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
1892 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1893 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
1897 EXPORT_SYMBOL_GPL(bpf_event_output);
1899 /* Always built-in helper functions. */
1900 const struct bpf_func_proto bpf_tail_call_proto = {
1903 .ret_type = RET_VOID,
1904 .arg1_type = ARG_PTR_TO_CTX,
1905 .arg2_type = ARG_CONST_MAP_PTR,
1906 .arg3_type = ARG_ANYTHING,
1909 /* Stub for JITs that only support cBPF. eBPF programs are interpreted.
1910 * It is encouraged to implement bpf_int_jit_compile() instead, so that
1911 * eBPF and implicitly also cBPF can get JITed!
1913 struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
1918 /* Stub for JITs that support eBPF. All cBPF code gets transformed into
1919 * eBPF by the kernel and is later compiled by bpf_int_jit_compile().
1921 void __weak bpf_jit_compile(struct bpf_prog *prog)
1925 bool __weak bpf_helper_changes_pkt_data(void *func)
1930 /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
1931 * skb_copy_bits(), so provide a weak definition of it for NET-less config.
1933 int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
1939 /* All definitions of tracepoints related to BPF. */
1940 #define CREATE_TRACE_POINTS
1941 #include <linux/bpf_trace.h>
1943 EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception);