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/unaligned.h>
39 #define BPF_R0 regs[BPF_REG_0]
40 #define BPF_R1 regs[BPF_REG_1]
41 #define BPF_R2 regs[BPF_REG_2]
42 #define BPF_R3 regs[BPF_REG_3]
43 #define BPF_R4 regs[BPF_REG_4]
44 #define BPF_R5 regs[BPF_REG_5]
45 #define BPF_R6 regs[BPF_REG_6]
46 #define BPF_R7 regs[BPF_REG_7]
47 #define BPF_R8 regs[BPF_REG_8]
48 #define BPF_R9 regs[BPF_REG_9]
49 #define BPF_R10 regs[BPF_REG_10]
52 #define DST regs[insn->dst_reg]
53 #define SRC regs[insn->src_reg]
54 #define FP regs[BPF_REG_FP]
55 #define ARG1 regs[BPF_REG_ARG1]
56 #define CTX regs[BPF_REG_CTX]
59 /* No hurry in this branch
61 * Exported for the bpf jit load helper.
63 void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
68 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
69 else if (k >= SKF_LL_OFF)
70 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
72 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
78 struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
80 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
81 struct bpf_prog_aux *aux;
84 size = round_up(size, PAGE_SIZE);
85 fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
89 aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);
95 fp->pages = size / PAGE_SIZE;
98 fp->jit_requested = ebpf_jit_enabled();
100 INIT_LIST_HEAD_RCU(&fp->aux->ksym_lnode);
104 EXPORT_SYMBOL_GPL(bpf_prog_alloc);
106 struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
107 gfp_t gfp_extra_flags)
109 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
114 BUG_ON(fp_old == NULL);
116 size = round_up(size, PAGE_SIZE);
117 pages = size / PAGE_SIZE;
118 if (pages <= fp_old->pages)
121 delta = pages - fp_old->pages;
122 ret = __bpf_prog_charge(fp_old->aux->user, delta);
126 fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
128 __bpf_prog_uncharge(fp_old->aux->user, delta);
130 memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
134 /* We keep fp->aux from fp_old around in the new
135 * reallocated structure.
138 __bpf_prog_free(fp_old);
144 void __bpf_prog_free(struct bpf_prog *fp)
150 int bpf_prog_calc_tag(struct bpf_prog *fp)
152 const u32 bits_offset = SHA_MESSAGE_BYTES - sizeof(__be64);
153 u32 raw_size = bpf_prog_tag_scratch_size(fp);
154 u32 digest[SHA_DIGEST_WORDS];
155 u32 ws[SHA_WORKSPACE_WORDS];
156 u32 i, bsize, psize, blocks;
157 struct bpf_insn *dst;
163 raw = vmalloc(raw_size);
168 memset(ws, 0, sizeof(ws));
170 /* We need to take out the map fd for the digest calculation
171 * since they are unstable from user space side.
174 for (i = 0, was_ld_map = false; i < fp->len; i++) {
175 dst[i] = fp->insnsi[i];
177 dst[i].code == (BPF_LD | BPF_IMM | BPF_DW) &&
178 dst[i].src_reg == BPF_PSEUDO_MAP_FD) {
181 } else if (was_ld_map &&
183 dst[i].dst_reg == 0 &&
184 dst[i].src_reg == 0 &&
193 psize = bpf_prog_insn_size(fp);
194 memset(&raw[psize], 0, raw_size - psize);
197 bsize = round_up(psize, SHA_MESSAGE_BYTES);
198 blocks = bsize / SHA_MESSAGE_BYTES;
200 if (bsize - psize >= sizeof(__be64)) {
201 bits = (__be64 *)(todo + bsize - sizeof(__be64));
203 bits = (__be64 *)(todo + bsize + bits_offset);
206 *bits = cpu_to_be64((psize - 1) << 3);
209 sha_transform(digest, todo, ws);
210 todo += SHA_MESSAGE_BYTES;
213 result = (__force __be32 *)digest;
214 for (i = 0; i < SHA_DIGEST_WORDS; i++)
215 result[i] = cpu_to_be32(digest[i]);
216 memcpy(fp->tag, result, sizeof(fp->tag));
222 static void bpf_adj_branches(struct bpf_prog *prog, u32 pos, u32 delta)
224 struct bpf_insn *insn = prog->insnsi;
225 u32 i, insn_cnt = prog->len;
230 for (i = 0; i < insn_cnt; i++, insn++) {
232 if (BPF_CLASS(code) != BPF_JMP)
234 if (BPF_OP(code) == BPF_EXIT)
236 if (BPF_OP(code) == BPF_CALL) {
237 if (insn->src_reg == BPF_PSEUDO_CALL)
244 off = pseudo_call ? insn->imm : insn->off;
246 /* Adjust offset of jmps if we cross boundaries. */
247 if (i < pos && i + off + 1 > pos)
249 else if (i > pos + delta && i + off + 1 <= pos + delta)
259 struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
260 const struct bpf_insn *patch, u32 len)
262 u32 insn_adj_cnt, insn_rest, insn_delta = len - 1;
263 struct bpf_prog *prog_adj;
265 /* Since our patchlet doesn't expand the image, we're done. */
266 if (insn_delta == 0) {
267 memcpy(prog->insnsi + off, patch, sizeof(*patch));
271 insn_adj_cnt = prog->len + insn_delta;
273 /* Several new instructions need to be inserted. Make room
274 * for them. Likely, there's no need for a new allocation as
275 * last page could have large enough tailroom.
277 prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),
282 prog_adj->len = insn_adj_cnt;
284 /* Patching happens in 3 steps:
286 * 1) Move over tail of insnsi from next instruction onwards,
287 * so we can patch the single target insn with one or more
288 * new ones (patching is always from 1 to n insns, n > 0).
289 * 2) Inject new instructions at the target location.
290 * 3) Adjust branch offsets if necessary.
292 insn_rest = insn_adj_cnt - off - len;
294 memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1,
295 sizeof(*patch) * insn_rest);
296 memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len);
298 bpf_adj_branches(prog_adj, off, insn_delta);
303 #ifdef CONFIG_BPF_JIT
304 /* All BPF JIT sysctl knobs here. */
305 int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_ALWAYS_ON);
306 int bpf_jit_harden __read_mostly;
307 int bpf_jit_kallsyms __read_mostly;
309 static __always_inline void
310 bpf_get_prog_addr_region(const struct bpf_prog *prog,
311 unsigned long *symbol_start,
312 unsigned long *symbol_end)
314 const struct bpf_binary_header *hdr = bpf_jit_binary_hdr(prog);
315 unsigned long addr = (unsigned long)hdr;
317 WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog));
319 *symbol_start = addr;
320 *symbol_end = addr + hdr->pages * PAGE_SIZE;
323 static void bpf_get_prog_name(const struct bpf_prog *prog, char *sym)
325 const char *end = sym + KSYM_NAME_LEN;
327 BUILD_BUG_ON(sizeof("bpf_prog_") +
328 sizeof(prog->tag) * 2 +
329 /* name has been null terminated.
330 * We should need +1 for the '_' preceding
331 * the name. However, the null character
332 * is double counted between the name and the
333 * sizeof("bpf_prog_") above, so we omit
336 sizeof(prog->aux->name) > KSYM_NAME_LEN);
338 sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_");
339 sym = bin2hex(sym, prog->tag, sizeof(prog->tag));
340 if (prog->aux->name[0])
341 snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name);
346 static __always_inline unsigned long
347 bpf_get_prog_addr_start(struct latch_tree_node *n)
349 unsigned long symbol_start, symbol_end;
350 const struct bpf_prog_aux *aux;
352 aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
353 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
358 static __always_inline bool bpf_tree_less(struct latch_tree_node *a,
359 struct latch_tree_node *b)
361 return bpf_get_prog_addr_start(a) < bpf_get_prog_addr_start(b);
364 static __always_inline int bpf_tree_comp(void *key, struct latch_tree_node *n)
366 unsigned long val = (unsigned long)key;
367 unsigned long symbol_start, symbol_end;
368 const struct bpf_prog_aux *aux;
370 aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
371 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
373 if (val < symbol_start)
375 if (val >= symbol_end)
381 static const struct latch_tree_ops bpf_tree_ops = {
382 .less = bpf_tree_less,
383 .comp = bpf_tree_comp,
386 static DEFINE_SPINLOCK(bpf_lock);
387 static LIST_HEAD(bpf_kallsyms);
388 static struct latch_tree_root bpf_tree __cacheline_aligned;
390 static void bpf_prog_ksym_node_add(struct bpf_prog_aux *aux)
392 WARN_ON_ONCE(!list_empty(&aux->ksym_lnode));
393 list_add_tail_rcu(&aux->ksym_lnode, &bpf_kallsyms);
394 latch_tree_insert(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
397 static void bpf_prog_ksym_node_del(struct bpf_prog_aux *aux)
399 if (list_empty(&aux->ksym_lnode))
402 latch_tree_erase(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
403 list_del_rcu(&aux->ksym_lnode);
406 static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp)
408 return fp->jited && !bpf_prog_was_classic(fp);
411 static bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp)
413 return list_empty(&fp->aux->ksym_lnode) ||
414 fp->aux->ksym_lnode.prev == LIST_POISON2;
417 void bpf_prog_kallsyms_add(struct bpf_prog *fp)
419 if (!bpf_prog_kallsyms_candidate(fp) ||
420 !capable(CAP_SYS_ADMIN))
423 spin_lock_bh(&bpf_lock);
424 bpf_prog_ksym_node_add(fp->aux);
425 spin_unlock_bh(&bpf_lock);
428 void bpf_prog_kallsyms_del(struct bpf_prog *fp)
430 if (!bpf_prog_kallsyms_candidate(fp))
433 spin_lock_bh(&bpf_lock);
434 bpf_prog_ksym_node_del(fp->aux);
435 spin_unlock_bh(&bpf_lock);
438 static struct bpf_prog *bpf_prog_kallsyms_find(unsigned long addr)
440 struct latch_tree_node *n;
442 if (!bpf_jit_kallsyms_enabled())
445 n = latch_tree_find((void *)addr, &bpf_tree, &bpf_tree_ops);
447 container_of(n, struct bpf_prog_aux, ksym_tnode)->prog :
451 const char *__bpf_address_lookup(unsigned long addr, unsigned long *size,
452 unsigned long *off, char *sym)
454 unsigned long symbol_start, symbol_end;
455 struct bpf_prog *prog;
459 prog = bpf_prog_kallsyms_find(addr);
461 bpf_get_prog_addr_region(prog, &symbol_start, &symbol_end);
462 bpf_get_prog_name(prog, sym);
466 *size = symbol_end - symbol_start;
468 *off = addr - symbol_start;
475 bool is_bpf_text_address(unsigned long addr)
480 ret = bpf_prog_kallsyms_find(addr) != NULL;
486 int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
489 unsigned long symbol_start, symbol_end;
490 struct bpf_prog_aux *aux;
494 if (!bpf_jit_kallsyms_enabled())
498 list_for_each_entry_rcu(aux, &bpf_kallsyms, ksym_lnode) {
502 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
503 bpf_get_prog_name(aux->prog, sym);
505 *value = symbol_start;
506 *type = BPF_SYM_ELF_TYPE;
516 struct bpf_binary_header *
517 bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
518 unsigned int alignment,
519 bpf_jit_fill_hole_t bpf_fill_ill_insns)
521 struct bpf_binary_header *hdr;
522 unsigned int size, hole, start;
524 /* Most of BPF filters are really small, but if some of them
525 * fill a page, allow at least 128 extra bytes to insert a
526 * random section of illegal instructions.
528 size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
529 hdr = module_alloc(size);
533 /* Fill space with illegal/arch-dep instructions. */
534 bpf_fill_ill_insns(hdr, size);
536 hdr->pages = size / PAGE_SIZE;
537 hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
538 PAGE_SIZE - sizeof(*hdr));
539 start = (get_random_int() % hole) & ~(alignment - 1);
541 /* Leave a random number of instructions before BPF code. */
542 *image_ptr = &hdr->image[start];
547 void bpf_jit_binary_free(struct bpf_binary_header *hdr)
552 /* This symbol is only overridden by archs that have different
553 * requirements than the usual eBPF JITs, f.e. when they only
554 * implement cBPF JIT, do not set images read-only, etc.
556 void __weak bpf_jit_free(struct bpf_prog *fp)
559 struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp);
561 bpf_jit_binary_unlock_ro(hdr);
562 bpf_jit_binary_free(hdr);
564 WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp));
567 bpf_prog_unlock_free(fp);
570 static int bpf_jit_blind_insn(const struct bpf_insn *from,
571 const struct bpf_insn *aux,
572 struct bpf_insn *to_buff)
574 struct bpf_insn *to = to_buff;
575 u32 imm_rnd = get_random_int();
578 BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG);
579 BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG);
581 if (from->imm == 0 &&
582 (from->code == (BPF_ALU | BPF_MOV | BPF_K) ||
583 from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) {
584 *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg);
588 switch (from->code) {
589 case BPF_ALU | BPF_ADD | BPF_K:
590 case BPF_ALU | BPF_SUB | BPF_K:
591 case BPF_ALU | BPF_AND | BPF_K:
592 case BPF_ALU | BPF_OR | BPF_K:
593 case BPF_ALU | BPF_XOR | BPF_K:
594 case BPF_ALU | BPF_MUL | BPF_K:
595 case BPF_ALU | BPF_MOV | BPF_K:
596 case BPF_ALU | BPF_DIV | BPF_K:
597 case BPF_ALU | BPF_MOD | BPF_K:
598 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
599 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
600 *to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX);
603 case BPF_ALU64 | BPF_ADD | BPF_K:
604 case BPF_ALU64 | BPF_SUB | BPF_K:
605 case BPF_ALU64 | BPF_AND | BPF_K:
606 case BPF_ALU64 | BPF_OR | BPF_K:
607 case BPF_ALU64 | BPF_XOR | BPF_K:
608 case BPF_ALU64 | BPF_MUL | BPF_K:
609 case BPF_ALU64 | BPF_MOV | BPF_K:
610 case BPF_ALU64 | BPF_DIV | BPF_K:
611 case BPF_ALU64 | BPF_MOD | BPF_K:
612 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
613 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
614 *to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX);
617 case BPF_JMP | BPF_JEQ | BPF_K:
618 case BPF_JMP | BPF_JNE | BPF_K:
619 case BPF_JMP | BPF_JGT | BPF_K:
620 case BPF_JMP | BPF_JLT | BPF_K:
621 case BPF_JMP | BPF_JGE | BPF_K:
622 case BPF_JMP | BPF_JLE | BPF_K:
623 case BPF_JMP | BPF_JSGT | BPF_K:
624 case BPF_JMP | BPF_JSLT | BPF_K:
625 case BPF_JMP | BPF_JSGE | BPF_K:
626 case BPF_JMP | BPF_JSLE | BPF_K:
627 case BPF_JMP | BPF_JSET | BPF_K:
628 /* Accommodate for extra offset in case of a backjump. */
632 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
633 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
634 *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off);
637 case BPF_LD | BPF_ABS | BPF_W:
638 case BPF_LD | BPF_ABS | BPF_H:
639 case BPF_LD | BPF_ABS | BPF_B:
640 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
641 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
642 *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
645 case BPF_LD | BPF_IND | BPF_W:
646 case BPF_LD | BPF_IND | BPF_H:
647 case BPF_LD | BPF_IND | BPF_B:
648 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
649 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
650 *to++ = BPF_ALU32_REG(BPF_ADD, BPF_REG_AX, from->src_reg);
651 *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
654 case BPF_LD | BPF_IMM | BPF_DW:
655 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm);
656 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
657 *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
658 *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX);
660 case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */
661 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm);
662 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
663 *to++ = BPF_ALU64_REG(BPF_OR, aux[0].dst_reg, BPF_REG_AX);
666 case BPF_ST | BPF_MEM | BPF_DW:
667 case BPF_ST | BPF_MEM | BPF_W:
668 case BPF_ST | BPF_MEM | BPF_H:
669 case BPF_ST | BPF_MEM | BPF_B:
670 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
671 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
672 *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off);
679 static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other,
680 gfp_t gfp_extra_flags)
682 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
685 fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags, PAGE_KERNEL);
687 /* aux->prog still points to the fp_other one, so
688 * when promoting the clone to the real program,
689 * this still needs to be adapted.
691 memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE);
697 static void bpf_prog_clone_free(struct bpf_prog *fp)
699 /* aux was stolen by the other clone, so we cannot free
700 * it from this path! It will be freed eventually by the
701 * other program on release.
703 * At this point, we don't need a deferred release since
704 * clone is guaranteed to not be locked.
710 void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other)
712 /* We have to repoint aux->prog to self, as we don't
713 * know whether fp here is the clone or the original.
716 bpf_prog_clone_free(fp_other);
719 struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
721 struct bpf_insn insn_buff[16], aux[2];
722 struct bpf_prog *clone, *tmp;
723 int insn_delta, insn_cnt;
724 struct bpf_insn *insn;
727 if (!bpf_jit_blinding_enabled(prog) || prog->blinded)
730 clone = bpf_prog_clone_create(prog, GFP_USER);
732 return ERR_PTR(-ENOMEM);
734 insn_cnt = clone->len;
735 insn = clone->insnsi;
737 for (i = 0; i < insn_cnt; i++, insn++) {
738 /* We temporarily need to hold the original ld64 insn
739 * so that we can still access the first part in the
740 * second blinding run.
742 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) &&
744 memcpy(aux, insn, sizeof(aux));
746 rewritten = bpf_jit_blind_insn(insn, aux, insn_buff);
750 tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);
752 /* Patching may have repointed aux->prog during
753 * realloc from the original one, so we need to
754 * fix it up here on error.
756 bpf_jit_prog_release_other(prog, clone);
757 return ERR_PTR(-ENOMEM);
761 insn_delta = rewritten - 1;
763 /* Walk new program and skip insns we just inserted. */
764 insn = clone->insnsi + i + insn_delta;
765 insn_cnt += insn_delta;
772 #endif /* CONFIG_BPF_JIT */
774 /* Base function for offset calculation. Needs to go into .text section,
775 * therefore keeping it non-static as well; will also be used by JITs
776 * anyway later on, so do not let the compiler omit it. This also needs
777 * to go into kallsyms for correlation from e.g. bpftool, so naming
780 noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
784 EXPORT_SYMBOL_GPL(__bpf_call_base);
786 /* All UAPI available opcodes. */
787 #define BPF_INSN_MAP(INSN_2, INSN_3) \
788 /* 32 bit ALU operations. */ \
789 /* Register based. */ \
790 INSN_3(ALU, ADD, X), \
791 INSN_3(ALU, SUB, X), \
792 INSN_3(ALU, AND, X), \
793 INSN_3(ALU, OR, X), \
794 INSN_3(ALU, LSH, X), \
795 INSN_3(ALU, RSH, X), \
796 INSN_3(ALU, XOR, X), \
797 INSN_3(ALU, MUL, X), \
798 INSN_3(ALU, MOV, X), \
799 INSN_3(ALU, DIV, X), \
800 INSN_3(ALU, MOD, X), \
802 INSN_3(ALU, END, TO_BE), \
803 INSN_3(ALU, END, TO_LE), \
804 /* Immediate based. */ \
805 INSN_3(ALU, ADD, K), \
806 INSN_3(ALU, SUB, K), \
807 INSN_3(ALU, AND, K), \
808 INSN_3(ALU, OR, K), \
809 INSN_3(ALU, LSH, K), \
810 INSN_3(ALU, RSH, K), \
811 INSN_3(ALU, XOR, K), \
812 INSN_3(ALU, MUL, K), \
813 INSN_3(ALU, MOV, K), \
814 INSN_3(ALU, DIV, K), \
815 INSN_3(ALU, MOD, K), \
816 /* 64 bit ALU operations. */ \
817 /* Register based. */ \
818 INSN_3(ALU64, ADD, X), \
819 INSN_3(ALU64, SUB, X), \
820 INSN_3(ALU64, AND, X), \
821 INSN_3(ALU64, OR, X), \
822 INSN_3(ALU64, LSH, X), \
823 INSN_3(ALU64, RSH, X), \
824 INSN_3(ALU64, XOR, X), \
825 INSN_3(ALU64, MUL, X), \
826 INSN_3(ALU64, MOV, X), \
827 INSN_3(ALU64, ARSH, X), \
828 INSN_3(ALU64, DIV, X), \
829 INSN_3(ALU64, MOD, X), \
830 INSN_2(ALU64, NEG), \
831 /* Immediate based. */ \
832 INSN_3(ALU64, ADD, K), \
833 INSN_3(ALU64, SUB, K), \
834 INSN_3(ALU64, AND, K), \
835 INSN_3(ALU64, OR, K), \
836 INSN_3(ALU64, LSH, K), \
837 INSN_3(ALU64, RSH, K), \
838 INSN_3(ALU64, XOR, K), \
839 INSN_3(ALU64, MUL, K), \
840 INSN_3(ALU64, MOV, K), \
841 INSN_3(ALU64, ARSH, K), \
842 INSN_3(ALU64, DIV, K), \
843 INSN_3(ALU64, MOD, K), \
844 /* Call instruction. */ \
846 /* Exit instruction. */ \
848 /* Jump instructions. */ \
849 /* Register based. */ \
850 INSN_3(JMP, JEQ, X), \
851 INSN_3(JMP, JNE, X), \
852 INSN_3(JMP, JGT, X), \
853 INSN_3(JMP, JLT, X), \
854 INSN_3(JMP, JGE, X), \
855 INSN_3(JMP, JLE, X), \
856 INSN_3(JMP, JSGT, X), \
857 INSN_3(JMP, JSLT, X), \
858 INSN_3(JMP, JSGE, X), \
859 INSN_3(JMP, JSLE, X), \
860 INSN_3(JMP, JSET, X), \
861 /* Immediate based. */ \
862 INSN_3(JMP, JEQ, K), \
863 INSN_3(JMP, JNE, K), \
864 INSN_3(JMP, JGT, K), \
865 INSN_3(JMP, JLT, K), \
866 INSN_3(JMP, JGE, K), \
867 INSN_3(JMP, JLE, K), \
868 INSN_3(JMP, JSGT, K), \
869 INSN_3(JMP, JSLT, K), \
870 INSN_3(JMP, JSGE, K), \
871 INSN_3(JMP, JSLE, K), \
872 INSN_3(JMP, JSET, K), \
874 /* Store instructions. */ \
875 /* Register based. */ \
876 INSN_3(STX, MEM, B), \
877 INSN_3(STX, MEM, H), \
878 INSN_3(STX, MEM, W), \
879 INSN_3(STX, MEM, DW), \
880 INSN_3(STX, XADD, W), \
881 INSN_3(STX, XADD, DW), \
882 /* Immediate based. */ \
883 INSN_3(ST, MEM, B), \
884 INSN_3(ST, MEM, H), \
885 INSN_3(ST, MEM, W), \
886 INSN_3(ST, MEM, DW), \
887 /* Load instructions. */ \
888 /* Register based. */ \
889 INSN_3(LDX, MEM, B), \
890 INSN_3(LDX, MEM, H), \
891 INSN_3(LDX, MEM, W), \
892 INSN_3(LDX, MEM, DW), \
893 /* Immediate based. */ \
894 INSN_3(LD, IMM, DW), \
895 /* Misc (old cBPF carry-over). */ \
896 INSN_3(LD, ABS, B), \
897 INSN_3(LD, ABS, H), \
898 INSN_3(LD, ABS, W), \
899 INSN_3(LD, IND, B), \
900 INSN_3(LD, IND, H), \
903 bool bpf_opcode_in_insntable(u8 code)
905 #define BPF_INSN_2_TBL(x, y) [BPF_##x | BPF_##y] = true
906 #define BPF_INSN_3_TBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = true
907 static const bool public_insntable[256] = {
909 /* Now overwrite non-defaults ... */
910 BPF_INSN_MAP(BPF_INSN_2_TBL, BPF_INSN_3_TBL),
912 #undef BPF_INSN_3_TBL
913 #undef BPF_INSN_2_TBL
914 return public_insntable[code];
917 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
919 * __bpf_prog_run - run eBPF program on a given context
920 * @ctx: is the data we are operating on
921 * @insn: is the array of eBPF instructions
923 * Decode and execute eBPF instructions.
925 static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack)
928 #define BPF_INSN_2_LBL(x, y) [BPF_##x | BPF_##y] = &&x##_##y
929 #define BPF_INSN_3_LBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = &&x##_##y##_##z
930 static const void *jumptable[256] = {
931 [0 ... 255] = &&default_label,
932 /* Now overwrite non-defaults ... */
933 BPF_INSN_MAP(BPF_INSN_2_LBL, BPF_INSN_3_LBL),
934 /* Non-UAPI available opcodes. */
935 [BPF_JMP | BPF_CALL_ARGS] = &&JMP_CALL_ARGS,
936 [BPF_JMP | BPF_TAIL_CALL] = &&JMP_TAIL_CALL,
938 #undef BPF_INSN_3_LBL
939 #undef BPF_INSN_2_LBL
940 u32 tail_call_cnt = 0;
944 #define CONT ({ insn++; goto select_insn; })
945 #define CONT_JMP ({ insn++; goto select_insn; })
948 goto *jumptable[insn->code];
951 #define ALU(OPCODE, OP) \
952 ALU64_##OPCODE##_X: \
956 DST = (u32) DST OP (u32) SRC; \
958 ALU64_##OPCODE##_K: \
962 DST = (u32) DST OP (u32) IMM; \
993 DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
997 (*(s64 *) &DST) >>= SRC;
1000 (*(s64 *) &DST) >>= IMM;
1003 div64_u64_rem(DST, SRC, &tmp);
1008 DST = do_div(tmp, (u32) SRC);
1011 div64_u64_rem(DST, IMM, &tmp);
1016 DST = do_div(tmp, (u32) IMM);
1019 DST = div64_u64(DST, SRC);
1023 do_div(tmp, (u32) SRC);
1027 DST = div64_u64(DST, IMM);
1031 do_div(tmp, (u32) IMM);
1037 DST = (__force u16) cpu_to_be16(DST);
1040 DST = (__force u32) cpu_to_be32(DST);
1043 DST = (__force u64) cpu_to_be64(DST);
1050 DST = (__force u16) cpu_to_le16(DST);
1053 DST = (__force u32) cpu_to_le32(DST);
1056 DST = (__force u64) cpu_to_le64(DST);
1063 /* Function call scratches BPF_R1-BPF_R5 registers,
1064 * preserves BPF_R6-BPF_R9, and stores return value
1067 BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
1072 BPF_R0 = (__bpf_call_base_args + insn->imm)(BPF_R1, BPF_R2,
1075 insn + insn->off + 1);
1079 struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
1080 struct bpf_array *array = container_of(map, struct bpf_array, map);
1081 struct bpf_prog *prog;
1084 if (unlikely(index >= array->map.max_entries))
1086 if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
1091 prog = READ_ONCE(array->ptrs[index]);
1095 /* ARG1 at this point is guaranteed to point to CTX from
1096 * the verifier side due to the fact that the tail call is
1097 * handeled like a helper, that is, bpf_tail_call_proto,
1098 * where arg1_type is ARG_PTR_TO_CTX.
1100 insn = prog->insnsi;
1182 if (((s64) DST) > ((s64) SRC)) {
1188 if (((s64) DST) > ((s64) IMM)) {
1194 if (((s64) DST) < ((s64) SRC)) {
1200 if (((s64) DST) < ((s64) IMM)) {
1206 if (((s64) DST) >= ((s64) SRC)) {
1212 if (((s64) DST) >= ((s64) IMM)) {
1218 if (((s64) DST) <= ((s64) SRC)) {
1224 if (((s64) DST) <= ((s64) IMM)) {
1244 /* STX and ST and LDX*/
1245 #define LDST(SIZEOP, SIZE) \
1247 *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \
1250 *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \
1253 DST = *(SIZE *)(unsigned long) (SRC + insn->off); \
1261 STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
1262 atomic_add((u32) SRC, (atomic_t *)(unsigned long)
1265 STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
1266 atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
1269 LD_ABS_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + imm32)) */
1272 /* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are only
1273 * appearing in the programs where ctx == skb
1274 * (see may_access_skb() in the verifier). All programs
1275 * keep 'ctx' in regs[BPF_REG_CTX] == BPF_R6,
1276 * bpf_convert_filter() saves it in BPF_R6, internal BPF
1277 * verifier will check that BPF_R6 == ctx.
1279 * BPF_ABS and BPF_IND are wrappers of function calls,
1280 * so they scratch BPF_R1-BPF_R5 registers, preserve
1281 * BPF_R6-BPF_R9, and store return value into BPF_R0.
1284 * ctx == skb == BPF_R6 == CTX
1287 * SRC == any register
1288 * IMM == 32-bit immediate
1291 * BPF_R0 - 8/16/32-bit skb data converted to cpu endianness
1294 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 4, &tmp);
1295 if (likely(ptr != NULL)) {
1296 BPF_R0 = get_unaligned_be32(ptr);
1301 LD_ABS_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + imm32)) */
1304 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 2, &tmp);
1305 if (likely(ptr != NULL)) {
1306 BPF_R0 = get_unaligned_be16(ptr);
1311 LD_ABS_B: /* BPF_R0 = *(u8 *) (skb->data + imm32) */
1314 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 1, &tmp);
1315 if (likely(ptr != NULL)) {
1316 BPF_R0 = *(u8 *)ptr;
1321 LD_IND_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + src_reg + imm32)) */
1324 LD_IND_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + src_reg + imm32)) */
1327 LD_IND_B: /* BPF_R0 = *(u8 *) (skb->data + src_reg + imm32) */
1332 /* If we ever reach this, we have a bug somewhere. Die hard here
1333 * instead of just returning 0; we could be somewhere in a subprog,
1334 * so execution could continue otherwise which we do /not/ want.
1336 * Note, verifier whitelists all opcodes in bpf_opcode_in_insntable().
1338 pr_warn("BPF interpreter: unknown opcode %02x\n", insn->code);
1342 STACK_FRAME_NON_STANDARD(___bpf_prog_run); /* jump table */
1344 #define PROG_NAME(stack_size) __bpf_prog_run##stack_size
1345 #define DEFINE_BPF_PROG_RUN(stack_size) \
1346 static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \
1348 u64 stack[stack_size / sizeof(u64)]; \
1349 u64 regs[MAX_BPF_REG]; \
1351 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1352 ARG1 = (u64) (unsigned long) ctx; \
1353 return ___bpf_prog_run(regs, insn, stack); \
1356 #define PROG_NAME_ARGS(stack_size) __bpf_prog_run_args##stack_size
1357 #define DEFINE_BPF_PROG_RUN_ARGS(stack_size) \
1358 static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \
1359 const struct bpf_insn *insn) \
1361 u64 stack[stack_size / sizeof(u64)]; \
1362 u64 regs[MAX_BPF_REG]; \
1364 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1370 return ___bpf_prog_run(regs, insn, stack); \
1373 #define EVAL1(FN, X) FN(X)
1374 #define EVAL2(FN, X, Y...) FN(X) EVAL1(FN, Y)
1375 #define EVAL3(FN, X, Y...) FN(X) EVAL2(FN, Y)
1376 #define EVAL4(FN, X, Y...) FN(X) EVAL3(FN, Y)
1377 #define EVAL5(FN, X, Y...) FN(X) EVAL4(FN, Y)
1378 #define EVAL6(FN, X, Y...) FN(X) EVAL5(FN, Y)
1380 EVAL6(DEFINE_BPF_PROG_RUN, 32, 64, 96, 128, 160, 192);
1381 EVAL6(DEFINE_BPF_PROG_RUN, 224, 256, 288, 320, 352, 384);
1382 EVAL4(DEFINE_BPF_PROG_RUN, 416, 448, 480, 512);
1384 EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 32, 64, 96, 128, 160, 192);
1385 EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 224, 256, 288, 320, 352, 384);
1386 EVAL4(DEFINE_BPF_PROG_RUN_ARGS, 416, 448, 480, 512);
1388 #define PROG_NAME_LIST(stack_size) PROG_NAME(stack_size),
1390 static unsigned int (*interpreters[])(const void *ctx,
1391 const struct bpf_insn *insn) = {
1392 EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1393 EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1394 EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1396 #undef PROG_NAME_LIST
1397 #define PROG_NAME_LIST(stack_size) PROG_NAME_ARGS(stack_size),
1398 static u64 (*interpreters_args[])(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5,
1399 const struct bpf_insn *insn) = {
1400 EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1401 EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1402 EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1404 #undef PROG_NAME_LIST
1406 void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth)
1408 stack_depth = max_t(u32, stack_depth, 1);
1409 insn->off = (s16) insn->imm;
1410 insn->imm = interpreters_args[(round_up(stack_depth, 32) / 32) - 1] -
1411 __bpf_call_base_args;
1412 insn->code = BPF_JMP | BPF_CALL_ARGS;
1416 static unsigned int __bpf_prog_ret0_warn(const void *ctx,
1417 const struct bpf_insn *insn)
1419 /* If this handler ever gets executed, then BPF_JIT_ALWAYS_ON
1420 * is not working properly, so warn about it!
1427 bool bpf_prog_array_compatible(struct bpf_array *array,
1428 const struct bpf_prog *fp)
1430 if (fp->kprobe_override)
1433 if (!array->owner_prog_type) {
1434 /* There's no owner yet where we could check for
1437 array->owner_prog_type = fp->type;
1438 array->owner_jited = fp->jited;
1443 return array->owner_prog_type == fp->type &&
1444 array->owner_jited == fp->jited;
1447 static int bpf_check_tail_call(const struct bpf_prog *fp)
1449 struct bpf_prog_aux *aux = fp->aux;
1452 for (i = 0; i < aux->used_map_cnt; i++) {
1453 struct bpf_map *map = aux->used_maps[i];
1454 struct bpf_array *array;
1456 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1459 array = container_of(map, struct bpf_array, map);
1460 if (!bpf_prog_array_compatible(array, fp))
1468 * bpf_prog_select_runtime - select exec runtime for BPF program
1469 * @fp: bpf_prog populated with internal BPF program
1470 * @err: pointer to error variable
1472 * Try to JIT eBPF program, if JIT is not available, use interpreter.
1473 * The BPF program will be executed via BPF_PROG_RUN() macro.
1475 struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
1477 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
1478 u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1);
1480 fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1];
1482 fp->bpf_func = __bpf_prog_ret0_warn;
1485 /* eBPF JITs can rewrite the program in case constant
1486 * blinding is active. However, in case of error during
1487 * blinding, bpf_int_jit_compile() must always return a
1488 * valid program, which in this case would simply not
1489 * be JITed, but falls back to the interpreter.
1491 if (!bpf_prog_is_dev_bound(fp->aux)) {
1492 fp = bpf_int_jit_compile(fp);
1493 #ifdef CONFIG_BPF_JIT_ALWAYS_ON
1500 *err = bpf_prog_offload_compile(fp);
1504 bpf_prog_lock_ro(fp);
1506 /* The tail call compatibility check can only be done at
1507 * this late stage as we need to determine, if we deal
1508 * with JITed or non JITed program concatenations and not
1509 * all eBPF JITs might immediately support all features.
1511 *err = bpf_check_tail_call(fp);
1515 EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
1517 static unsigned int __bpf_prog_ret1(const void *ctx,
1518 const struct bpf_insn *insn)
1523 static struct bpf_prog_dummy {
1524 struct bpf_prog prog;
1525 } dummy_bpf_prog = {
1527 .bpf_func = __bpf_prog_ret1,
1531 /* to avoid allocating empty bpf_prog_array for cgroups that
1532 * don't have bpf program attached use one global 'empty_prog_array'
1533 * It will not be modified the caller of bpf_prog_array_alloc()
1534 * (since caller requested prog_cnt == 0)
1535 * that pointer should be 'freed' by bpf_prog_array_free()
1538 struct bpf_prog_array hdr;
1539 struct bpf_prog *null_prog;
1540 } empty_prog_array = {
1544 struct bpf_prog_array __rcu *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags)
1547 return kzalloc(sizeof(struct bpf_prog_array) +
1548 sizeof(struct bpf_prog *) * (prog_cnt + 1),
1551 return &empty_prog_array.hdr;
1554 void bpf_prog_array_free(struct bpf_prog_array __rcu *progs)
1557 progs == (struct bpf_prog_array __rcu *)&empty_prog_array.hdr)
1559 kfree_rcu(progs, rcu);
1562 int bpf_prog_array_length(struct bpf_prog_array __rcu *progs)
1564 struct bpf_prog **prog;
1568 prog = rcu_dereference(progs)->progs;
1569 for (; *prog; prog++)
1570 if (*prog != &dummy_bpf_prog.prog)
1576 static bool bpf_prog_array_copy_core(struct bpf_prog **prog,
1582 for (; *prog; prog++) {
1583 if (*prog == &dummy_bpf_prog.prog)
1585 prog_ids[i] = (*prog)->aux->id;
1586 if (++i == request_cnt) {
1595 int bpf_prog_array_copy_to_user(struct bpf_prog_array __rcu *progs,
1596 __u32 __user *prog_ids, u32 cnt)
1598 struct bpf_prog **prog;
1599 unsigned long err = 0;
1603 /* users of this function are doing:
1604 * cnt = bpf_prog_array_length();
1606 * bpf_prog_array_copy_to_user(..., cnt);
1607 * so below kcalloc doesn't need extra cnt > 0 check, but
1608 * bpf_prog_array_length() releases rcu lock and
1609 * prog array could have been swapped with empty or larger array,
1610 * so always copy 'cnt' prog_ids to the user.
1611 * In a rare race the user will see zero prog_ids
1613 ids = kcalloc(cnt, sizeof(u32), GFP_USER | __GFP_NOWARN);
1617 prog = rcu_dereference(progs)->progs;
1618 nospc = bpf_prog_array_copy_core(prog, ids, cnt);
1620 err = copy_to_user(prog_ids, ids, cnt * sizeof(u32));
1629 void bpf_prog_array_delete_safe(struct bpf_prog_array __rcu *progs,
1630 struct bpf_prog *old_prog)
1632 struct bpf_prog **prog = progs->progs;
1634 for (; *prog; prog++)
1635 if (*prog == old_prog) {
1636 WRITE_ONCE(*prog, &dummy_bpf_prog.prog);
1641 int bpf_prog_array_copy(struct bpf_prog_array __rcu *old_array,
1642 struct bpf_prog *exclude_prog,
1643 struct bpf_prog *include_prog,
1644 struct bpf_prog_array **new_array)
1646 int new_prog_cnt, carry_prog_cnt = 0;
1647 struct bpf_prog **existing_prog;
1648 struct bpf_prog_array *array;
1649 int new_prog_idx = 0;
1651 /* Figure out how many existing progs we need to carry over to
1655 existing_prog = old_array->progs;
1656 for (; *existing_prog; existing_prog++) {
1657 if (*existing_prog != exclude_prog &&
1658 *existing_prog != &dummy_bpf_prog.prog)
1660 if (*existing_prog == include_prog)
1665 /* How many progs (not NULL) will be in the new array? */
1666 new_prog_cnt = carry_prog_cnt;
1670 /* Do we have any prog (not NULL) in the new array? */
1671 if (!new_prog_cnt) {
1676 /* +1 as the end of prog_array is marked with NULL */
1677 array = bpf_prog_array_alloc(new_prog_cnt + 1, GFP_KERNEL);
1681 /* Fill in the new prog array */
1682 if (carry_prog_cnt) {
1683 existing_prog = old_array->progs;
1684 for (; *existing_prog; existing_prog++)
1685 if (*existing_prog != exclude_prog &&
1686 *existing_prog != &dummy_bpf_prog.prog)
1687 array->progs[new_prog_idx++] = *existing_prog;
1690 array->progs[new_prog_idx++] = include_prog;
1691 array->progs[new_prog_idx] = NULL;
1696 int bpf_prog_array_copy_info(struct bpf_prog_array __rcu *array,
1697 u32 *prog_ids, u32 request_cnt,
1700 struct bpf_prog **prog;
1704 cnt = bpf_prog_array_length(array);
1708 /* return early if user requested only program count or nothing to copy */
1709 if (!request_cnt || !cnt)
1712 /* this function is called under trace/bpf_trace.c: bpf_event_mutex */
1713 prog = rcu_dereference_check(array, 1)->progs;
1714 return bpf_prog_array_copy_core(prog, prog_ids, request_cnt) ? -ENOSPC
1718 static void bpf_prog_free_deferred(struct work_struct *work)
1720 struct bpf_prog_aux *aux;
1723 aux = container_of(work, struct bpf_prog_aux, work);
1724 if (bpf_prog_is_dev_bound(aux))
1725 bpf_prog_offload_destroy(aux->prog);
1726 #ifdef CONFIG_PERF_EVENTS
1727 if (aux->prog->has_callchain_buf)
1728 put_callchain_buffers();
1730 for (i = 0; i < aux->func_cnt; i++)
1731 bpf_jit_free(aux->func[i]);
1732 if (aux->func_cnt) {
1734 bpf_prog_unlock_free(aux->prog);
1736 bpf_jit_free(aux->prog);
1740 /* Free internal BPF program */
1741 void bpf_prog_free(struct bpf_prog *fp)
1743 struct bpf_prog_aux *aux = fp->aux;
1745 INIT_WORK(&aux->work, bpf_prog_free_deferred);
1746 schedule_work(&aux->work);
1748 EXPORT_SYMBOL_GPL(bpf_prog_free);
1750 /* RNG for unpriviledged user space with separated state from prandom_u32(). */
1751 static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
1753 void bpf_user_rnd_init_once(void)
1755 prandom_init_once(&bpf_user_rnd_state);
1758 BPF_CALL_0(bpf_user_rnd_u32)
1760 /* Should someone ever have the rather unwise idea to use some
1761 * of the registers passed into this function, then note that
1762 * this function is called from native eBPF and classic-to-eBPF
1763 * transformations. Register assignments from both sides are
1764 * different, f.e. classic always sets fn(ctx, A, X) here.
1766 struct rnd_state *state;
1769 state = &get_cpu_var(bpf_user_rnd_state);
1770 res = prandom_u32_state(state);
1771 put_cpu_var(bpf_user_rnd_state);
1776 /* Weak definitions of helper functions in case we don't have bpf syscall. */
1777 const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
1778 const struct bpf_func_proto bpf_map_update_elem_proto __weak;
1779 const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
1781 const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
1782 const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
1783 const struct bpf_func_proto bpf_get_numa_node_id_proto __weak;
1784 const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
1786 const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
1787 const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
1788 const struct bpf_func_proto bpf_get_current_comm_proto __weak;
1789 const struct bpf_func_proto bpf_sock_map_update_proto __weak;
1791 const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
1797 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1798 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
1803 /* Always built-in helper functions. */
1804 const struct bpf_func_proto bpf_tail_call_proto = {
1807 .ret_type = RET_VOID,
1808 .arg1_type = ARG_PTR_TO_CTX,
1809 .arg2_type = ARG_CONST_MAP_PTR,
1810 .arg3_type = ARG_ANYTHING,
1813 /* Stub for JITs that only support cBPF. eBPF programs are interpreted.
1814 * It is encouraged to implement bpf_int_jit_compile() instead, so that
1815 * eBPF and implicitly also cBPF can get JITed!
1817 struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
1822 /* Stub for JITs that support eBPF. All cBPF code gets transformed into
1823 * eBPF by the kernel and is later compiled by bpf_int_jit_compile().
1825 void __weak bpf_jit_compile(struct bpf_prog *prog)
1829 bool __weak bpf_helper_changes_pkt_data(void *func)
1834 /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
1835 * skb_copy_bits(), so provide a weak definition of it for NET-less config.
1837 int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
1843 /* All definitions of tracepoints related to BPF. */
1844 #define CREATE_TRACE_POINTS
1845 #include <linux/bpf_trace.h>
1847 EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception);