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>
35 #include <asm/unaligned.h>
38 #define BPF_R0 regs[BPF_REG_0]
39 #define BPF_R1 regs[BPF_REG_1]
40 #define BPF_R2 regs[BPF_REG_2]
41 #define BPF_R3 regs[BPF_REG_3]
42 #define BPF_R4 regs[BPF_REG_4]
43 #define BPF_R5 regs[BPF_REG_5]
44 #define BPF_R6 regs[BPF_REG_6]
45 #define BPF_R7 regs[BPF_REG_7]
46 #define BPF_R8 regs[BPF_REG_8]
47 #define BPF_R9 regs[BPF_REG_9]
48 #define BPF_R10 regs[BPF_REG_10]
51 #define DST regs[insn->dst_reg]
52 #define SRC regs[insn->src_reg]
53 #define FP regs[BPF_REG_FP]
54 #define ARG1 regs[BPF_REG_ARG1]
55 #define CTX regs[BPF_REG_CTX]
58 /* No hurry in this branch
60 * Exported for the bpf jit load helper.
62 void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
67 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
68 else if (k >= SKF_LL_OFF)
69 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
71 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
77 struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
79 gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
81 struct bpf_prog_aux *aux;
84 size = round_up(size, PAGE_SIZE);
85 fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
89 kmemcheck_annotate_bitfield(fp, meta);
91 aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);
97 fp->pages = size / PAGE_SIZE;
101 INIT_LIST_HEAD_RCU(&fp->aux->ksym_lnode);
105 EXPORT_SYMBOL_GPL(bpf_prog_alloc);
107 struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
108 gfp_t gfp_extra_flags)
110 gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
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 kmemcheck_annotate_bitfield(fp, meta);
134 memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
138 /* We keep fp->aux from fp_old around in the new
139 * reallocated structure.
142 __bpf_prog_free(fp_old);
148 void __bpf_prog_free(struct bpf_prog *fp)
154 int bpf_prog_calc_tag(struct bpf_prog *fp)
156 const u32 bits_offset = SHA_MESSAGE_BYTES - sizeof(__be64);
157 u32 raw_size = bpf_prog_tag_scratch_size(fp);
158 u32 digest[SHA_DIGEST_WORDS];
159 u32 ws[SHA_WORKSPACE_WORDS];
160 u32 i, bsize, psize, blocks;
161 struct bpf_insn *dst;
167 raw = vmalloc(raw_size);
172 memset(ws, 0, sizeof(ws));
174 /* We need to take out the map fd for the digest calculation
175 * since they are unstable from user space side.
178 for (i = 0, was_ld_map = false; i < fp->len; i++) {
179 dst[i] = fp->insnsi[i];
181 dst[i].code == (BPF_LD | BPF_IMM | BPF_DW) &&
182 dst[i].src_reg == BPF_PSEUDO_MAP_FD) {
185 } else if (was_ld_map &&
187 dst[i].dst_reg == 0 &&
188 dst[i].src_reg == 0 &&
197 psize = bpf_prog_insn_size(fp);
198 memset(&raw[psize], 0, raw_size - psize);
201 bsize = round_up(psize, SHA_MESSAGE_BYTES);
202 blocks = bsize / SHA_MESSAGE_BYTES;
204 if (bsize - psize >= sizeof(__be64)) {
205 bits = (__be64 *)(todo + bsize - sizeof(__be64));
207 bits = (__be64 *)(todo + bsize + bits_offset);
210 *bits = cpu_to_be64((psize - 1) << 3);
213 sha_transform(digest, todo, ws);
214 todo += SHA_MESSAGE_BYTES;
217 result = (__force __be32 *)digest;
218 for (i = 0; i < SHA_DIGEST_WORDS; i++)
219 result[i] = cpu_to_be32(digest[i]);
220 memcpy(fp->tag, result, sizeof(fp->tag));
226 static bool bpf_is_jmp_and_has_target(const struct bpf_insn *insn)
228 return BPF_CLASS(insn->code) == BPF_JMP &&
229 /* Call and Exit are both special jumps with no
230 * target inside the BPF instruction image.
232 BPF_OP(insn->code) != BPF_CALL &&
233 BPF_OP(insn->code) != BPF_EXIT;
236 static void bpf_adj_branches(struct bpf_prog *prog, u32 pos, u32 delta)
238 struct bpf_insn *insn = prog->insnsi;
239 u32 i, insn_cnt = prog->len;
241 for (i = 0; i < insn_cnt; i++, insn++) {
242 if (!bpf_is_jmp_and_has_target(insn))
245 /* Adjust offset of jmps if we cross boundaries. */
246 if (i < pos && i + insn->off + 1 > pos)
248 else if (i > pos + delta && i + insn->off + 1 <= pos + delta)
253 struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
254 const struct bpf_insn *patch, u32 len)
256 u32 insn_adj_cnt, insn_rest, insn_delta = len - 1;
257 struct bpf_prog *prog_adj;
259 /* Since our patchlet doesn't expand the image, we're done. */
260 if (insn_delta == 0) {
261 memcpy(prog->insnsi + off, patch, sizeof(*patch));
265 insn_adj_cnt = prog->len + insn_delta;
267 /* Several new instructions need to be inserted. Make room
268 * for them. Likely, there's no need for a new allocation as
269 * last page could have large enough tailroom.
271 prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),
276 prog_adj->len = insn_adj_cnt;
278 /* Patching happens in 3 steps:
280 * 1) Move over tail of insnsi from next instruction onwards,
281 * so we can patch the single target insn with one or more
282 * new ones (patching is always from 1 to n insns, n > 0).
283 * 2) Inject new instructions at the target location.
284 * 3) Adjust branch offsets if necessary.
286 insn_rest = insn_adj_cnt - off - len;
288 memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1,
289 sizeof(*patch) * insn_rest);
290 memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len);
292 bpf_adj_branches(prog_adj, off, insn_delta);
297 #ifdef CONFIG_BPF_JIT
298 static __always_inline void
299 bpf_get_prog_addr_region(const struct bpf_prog *prog,
300 unsigned long *symbol_start,
301 unsigned long *symbol_end)
303 const struct bpf_binary_header *hdr = bpf_jit_binary_hdr(prog);
304 unsigned long addr = (unsigned long)hdr;
306 WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog));
308 *symbol_start = addr;
309 *symbol_end = addr + hdr->pages * PAGE_SIZE;
312 static void bpf_get_prog_name(const struct bpf_prog *prog, char *sym)
314 BUILD_BUG_ON(sizeof("bpf_prog_") +
315 sizeof(prog->tag) * 2 + 1 > KSYM_NAME_LEN);
317 sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_");
318 sym = bin2hex(sym, prog->tag, sizeof(prog->tag));
322 static __always_inline unsigned long
323 bpf_get_prog_addr_start(struct latch_tree_node *n)
325 unsigned long symbol_start, symbol_end;
326 const struct bpf_prog_aux *aux;
328 aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
329 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
334 static __always_inline bool bpf_tree_less(struct latch_tree_node *a,
335 struct latch_tree_node *b)
337 return bpf_get_prog_addr_start(a) < bpf_get_prog_addr_start(b);
340 static __always_inline int bpf_tree_comp(void *key, struct latch_tree_node *n)
342 unsigned long val = (unsigned long)key;
343 unsigned long symbol_start, symbol_end;
344 const struct bpf_prog_aux *aux;
346 aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
347 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
349 if (val < symbol_start)
351 if (val >= symbol_end)
357 static const struct latch_tree_ops bpf_tree_ops = {
358 .less = bpf_tree_less,
359 .comp = bpf_tree_comp,
362 static DEFINE_SPINLOCK(bpf_lock);
363 static LIST_HEAD(bpf_kallsyms);
364 static struct latch_tree_root bpf_tree __cacheline_aligned;
366 int bpf_jit_kallsyms __read_mostly;
368 static void bpf_prog_ksym_node_add(struct bpf_prog_aux *aux)
370 WARN_ON_ONCE(!list_empty(&aux->ksym_lnode));
371 list_add_tail_rcu(&aux->ksym_lnode, &bpf_kallsyms);
372 latch_tree_insert(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
375 static void bpf_prog_ksym_node_del(struct bpf_prog_aux *aux)
377 if (list_empty(&aux->ksym_lnode))
380 latch_tree_erase(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
381 list_del_rcu(&aux->ksym_lnode);
384 static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp)
386 return fp->jited && !bpf_prog_was_classic(fp);
389 static bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp)
391 return list_empty(&fp->aux->ksym_lnode) ||
392 fp->aux->ksym_lnode.prev == LIST_POISON2;
395 void bpf_prog_kallsyms_add(struct bpf_prog *fp)
399 if (!bpf_prog_kallsyms_candidate(fp) ||
400 !capable(CAP_SYS_ADMIN))
403 spin_lock_irqsave(&bpf_lock, flags);
404 bpf_prog_ksym_node_add(fp->aux);
405 spin_unlock_irqrestore(&bpf_lock, flags);
408 void bpf_prog_kallsyms_del(struct bpf_prog *fp)
412 if (!bpf_prog_kallsyms_candidate(fp))
415 spin_lock_irqsave(&bpf_lock, flags);
416 bpf_prog_ksym_node_del(fp->aux);
417 spin_unlock_irqrestore(&bpf_lock, flags);
420 static struct bpf_prog *bpf_prog_kallsyms_find(unsigned long addr)
422 struct latch_tree_node *n;
424 if (!bpf_jit_kallsyms_enabled())
427 n = latch_tree_find((void *)addr, &bpf_tree, &bpf_tree_ops);
429 container_of(n, struct bpf_prog_aux, ksym_tnode)->prog :
433 const char *__bpf_address_lookup(unsigned long addr, unsigned long *size,
434 unsigned long *off, char *sym)
436 unsigned long symbol_start, symbol_end;
437 struct bpf_prog *prog;
441 prog = bpf_prog_kallsyms_find(addr);
443 bpf_get_prog_addr_region(prog, &symbol_start, &symbol_end);
444 bpf_get_prog_name(prog, sym);
448 *size = symbol_end - symbol_start;
450 *off = addr - symbol_start;
457 bool is_bpf_text_address(unsigned long addr)
462 ret = bpf_prog_kallsyms_find(addr) != NULL;
468 int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
471 unsigned long symbol_start, symbol_end;
472 struct bpf_prog_aux *aux;
476 if (!bpf_jit_kallsyms_enabled())
480 list_for_each_entry_rcu(aux, &bpf_kallsyms, ksym_lnode) {
484 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
485 bpf_get_prog_name(aux->prog, sym);
487 *value = symbol_start;
488 *type = BPF_SYM_ELF_TYPE;
498 struct bpf_binary_header *
499 bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
500 unsigned int alignment,
501 bpf_jit_fill_hole_t bpf_fill_ill_insns)
503 struct bpf_binary_header *hdr;
504 unsigned int size, hole, start;
506 /* Most of BPF filters are really small, but if some of them
507 * fill a page, allow at least 128 extra bytes to insert a
508 * random section of illegal instructions.
510 size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
511 hdr = module_alloc(size);
515 /* Fill space with illegal/arch-dep instructions. */
516 bpf_fill_ill_insns(hdr, size);
518 hdr->pages = size / PAGE_SIZE;
519 hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
520 PAGE_SIZE - sizeof(*hdr));
521 start = (get_random_int() % hole) & ~(alignment - 1);
523 /* Leave a random number of instructions before BPF code. */
524 *image_ptr = &hdr->image[start];
529 void bpf_jit_binary_free(struct bpf_binary_header *hdr)
534 /* This symbol is only overridden by archs that have different
535 * requirements than the usual eBPF JITs, f.e. when they only
536 * implement cBPF JIT, do not set images read-only, etc.
538 void __weak bpf_jit_free(struct bpf_prog *fp)
541 struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp);
543 bpf_jit_binary_unlock_ro(hdr);
544 bpf_jit_binary_free(hdr);
546 WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp));
549 bpf_prog_unlock_free(fp);
552 int bpf_jit_harden __read_mostly;
554 static int bpf_jit_blind_insn(const struct bpf_insn *from,
555 const struct bpf_insn *aux,
556 struct bpf_insn *to_buff)
558 struct bpf_insn *to = to_buff;
559 u32 imm_rnd = get_random_int();
562 BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG);
563 BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG);
565 if (from->imm == 0 &&
566 (from->code == (BPF_ALU | BPF_MOV | BPF_K) ||
567 from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) {
568 *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg);
572 switch (from->code) {
573 case BPF_ALU | BPF_ADD | BPF_K:
574 case BPF_ALU | BPF_SUB | BPF_K:
575 case BPF_ALU | BPF_AND | BPF_K:
576 case BPF_ALU | BPF_OR | BPF_K:
577 case BPF_ALU | BPF_XOR | BPF_K:
578 case BPF_ALU | BPF_MUL | BPF_K:
579 case BPF_ALU | BPF_MOV | BPF_K:
580 case BPF_ALU | BPF_DIV | BPF_K:
581 case BPF_ALU | BPF_MOD | BPF_K:
582 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
583 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
584 *to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX);
587 case BPF_ALU64 | BPF_ADD | BPF_K:
588 case BPF_ALU64 | BPF_SUB | BPF_K:
589 case BPF_ALU64 | BPF_AND | BPF_K:
590 case BPF_ALU64 | BPF_OR | BPF_K:
591 case BPF_ALU64 | BPF_XOR | BPF_K:
592 case BPF_ALU64 | BPF_MUL | BPF_K:
593 case BPF_ALU64 | BPF_MOV | BPF_K:
594 case BPF_ALU64 | BPF_DIV | BPF_K:
595 case BPF_ALU64 | BPF_MOD | BPF_K:
596 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
597 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
598 *to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX);
601 case BPF_JMP | BPF_JEQ | BPF_K:
602 case BPF_JMP | BPF_JNE | BPF_K:
603 case BPF_JMP | BPF_JGT | BPF_K:
604 case BPF_JMP | BPF_JGE | BPF_K:
605 case BPF_JMP | BPF_JSGT | BPF_K:
606 case BPF_JMP | BPF_JSGE | BPF_K:
607 case BPF_JMP | BPF_JSET | BPF_K:
608 /* Accommodate for extra offset in case of a backjump. */
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_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off);
617 case BPF_LD | BPF_ABS | BPF_W:
618 case BPF_LD | BPF_ABS | BPF_H:
619 case BPF_LD | BPF_ABS | BPF_B:
620 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
621 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
622 *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
625 case BPF_LD | BPF_IND | BPF_W:
626 case BPF_LD | BPF_IND | BPF_H:
627 case BPF_LD | BPF_IND | BPF_B:
628 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
629 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
630 *to++ = BPF_ALU32_REG(BPF_ADD, BPF_REG_AX, from->src_reg);
631 *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
634 case BPF_LD | BPF_IMM | BPF_DW:
635 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm);
636 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
637 *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
638 *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX);
640 case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */
641 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm);
642 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
643 *to++ = BPF_ALU64_REG(BPF_OR, aux[0].dst_reg, BPF_REG_AX);
646 case BPF_ST | BPF_MEM | BPF_DW:
647 case BPF_ST | BPF_MEM | BPF_W:
648 case BPF_ST | BPF_MEM | BPF_H:
649 case BPF_ST | BPF_MEM | BPF_B:
650 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
651 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
652 *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off);
659 static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other,
660 gfp_t gfp_extra_flags)
662 gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
666 fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags, PAGE_KERNEL);
668 kmemcheck_annotate_bitfield(fp, meta);
670 /* aux->prog still points to the fp_other one, so
671 * when promoting the clone to the real program,
672 * this still needs to be adapted.
674 memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE);
680 static void bpf_prog_clone_free(struct bpf_prog *fp)
682 /* aux was stolen by the other clone, so we cannot free
683 * it from this path! It will be freed eventually by the
684 * other program on release.
686 * At this point, we don't need a deferred release since
687 * clone is guaranteed to not be locked.
693 void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other)
695 /* We have to repoint aux->prog to self, as we don't
696 * know whether fp here is the clone or the original.
699 bpf_prog_clone_free(fp_other);
702 struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
704 struct bpf_insn insn_buff[16], aux[2];
705 struct bpf_prog *clone, *tmp;
706 int insn_delta, insn_cnt;
707 struct bpf_insn *insn;
710 if (!bpf_jit_blinding_enabled())
713 clone = bpf_prog_clone_create(prog, GFP_USER);
715 return ERR_PTR(-ENOMEM);
717 insn_cnt = clone->len;
718 insn = clone->insnsi;
720 for (i = 0; i < insn_cnt; i++, insn++) {
721 /* We temporarily need to hold the original ld64 insn
722 * so that we can still access the first part in the
723 * second blinding run.
725 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) &&
727 memcpy(aux, insn, sizeof(aux));
729 rewritten = bpf_jit_blind_insn(insn, aux, insn_buff);
733 tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);
735 /* Patching may have repointed aux->prog during
736 * realloc from the original one, so we need to
737 * fix it up here on error.
739 bpf_jit_prog_release_other(prog, clone);
740 return ERR_PTR(-ENOMEM);
744 insn_delta = rewritten - 1;
746 /* Walk new program and skip insns we just inserted. */
747 insn = clone->insnsi + i + insn_delta;
748 insn_cnt += insn_delta;
754 #endif /* CONFIG_BPF_JIT */
756 /* Base function for offset calculation. Needs to go into .text section,
757 * therefore keeping it non-static as well; will also be used by JITs
758 * anyway later on, so do not let the compiler omit it.
760 noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
764 EXPORT_SYMBOL_GPL(__bpf_call_base);
767 * __bpf_prog_run - run eBPF program on a given context
768 * @ctx: is the data we are operating on
769 * @insn: is the array of eBPF instructions
771 * Decode and execute eBPF instructions.
773 static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
775 u64 stack[MAX_BPF_STACK / sizeof(u64)];
776 u64 regs[MAX_BPF_REG], tmp;
777 static const void *jumptable[256] = {
778 [0 ... 255] = &&default_label,
779 /* Now overwrite non-defaults ... */
780 /* 32 bit ALU operations */
781 [BPF_ALU | BPF_ADD | BPF_X] = &&ALU_ADD_X,
782 [BPF_ALU | BPF_ADD | BPF_K] = &&ALU_ADD_K,
783 [BPF_ALU | BPF_SUB | BPF_X] = &&ALU_SUB_X,
784 [BPF_ALU | BPF_SUB | BPF_K] = &&ALU_SUB_K,
785 [BPF_ALU | BPF_AND | BPF_X] = &&ALU_AND_X,
786 [BPF_ALU | BPF_AND | BPF_K] = &&ALU_AND_K,
787 [BPF_ALU | BPF_OR | BPF_X] = &&ALU_OR_X,
788 [BPF_ALU | BPF_OR | BPF_K] = &&ALU_OR_K,
789 [BPF_ALU | BPF_LSH | BPF_X] = &&ALU_LSH_X,
790 [BPF_ALU | BPF_LSH | BPF_K] = &&ALU_LSH_K,
791 [BPF_ALU | BPF_RSH | BPF_X] = &&ALU_RSH_X,
792 [BPF_ALU | BPF_RSH | BPF_K] = &&ALU_RSH_K,
793 [BPF_ALU | BPF_XOR | BPF_X] = &&ALU_XOR_X,
794 [BPF_ALU | BPF_XOR | BPF_K] = &&ALU_XOR_K,
795 [BPF_ALU | BPF_MUL | BPF_X] = &&ALU_MUL_X,
796 [BPF_ALU | BPF_MUL | BPF_K] = &&ALU_MUL_K,
797 [BPF_ALU | BPF_MOV | BPF_X] = &&ALU_MOV_X,
798 [BPF_ALU | BPF_MOV | BPF_K] = &&ALU_MOV_K,
799 [BPF_ALU | BPF_DIV | BPF_X] = &&ALU_DIV_X,
800 [BPF_ALU | BPF_DIV | BPF_K] = &&ALU_DIV_K,
801 [BPF_ALU | BPF_MOD | BPF_X] = &&ALU_MOD_X,
802 [BPF_ALU | BPF_MOD | BPF_K] = &&ALU_MOD_K,
803 [BPF_ALU | BPF_NEG] = &&ALU_NEG,
804 [BPF_ALU | BPF_END | BPF_TO_BE] = &&ALU_END_TO_BE,
805 [BPF_ALU | BPF_END | BPF_TO_LE] = &&ALU_END_TO_LE,
806 /* 64 bit ALU operations */
807 [BPF_ALU64 | BPF_ADD | BPF_X] = &&ALU64_ADD_X,
808 [BPF_ALU64 | BPF_ADD | BPF_K] = &&ALU64_ADD_K,
809 [BPF_ALU64 | BPF_SUB | BPF_X] = &&ALU64_SUB_X,
810 [BPF_ALU64 | BPF_SUB | BPF_K] = &&ALU64_SUB_K,
811 [BPF_ALU64 | BPF_AND | BPF_X] = &&ALU64_AND_X,
812 [BPF_ALU64 | BPF_AND | BPF_K] = &&ALU64_AND_K,
813 [BPF_ALU64 | BPF_OR | BPF_X] = &&ALU64_OR_X,
814 [BPF_ALU64 | BPF_OR | BPF_K] = &&ALU64_OR_K,
815 [BPF_ALU64 | BPF_LSH | BPF_X] = &&ALU64_LSH_X,
816 [BPF_ALU64 | BPF_LSH | BPF_K] = &&ALU64_LSH_K,
817 [BPF_ALU64 | BPF_RSH | BPF_X] = &&ALU64_RSH_X,
818 [BPF_ALU64 | BPF_RSH | BPF_K] = &&ALU64_RSH_K,
819 [BPF_ALU64 | BPF_XOR | BPF_X] = &&ALU64_XOR_X,
820 [BPF_ALU64 | BPF_XOR | BPF_K] = &&ALU64_XOR_K,
821 [BPF_ALU64 | BPF_MUL | BPF_X] = &&ALU64_MUL_X,
822 [BPF_ALU64 | BPF_MUL | BPF_K] = &&ALU64_MUL_K,
823 [BPF_ALU64 | BPF_MOV | BPF_X] = &&ALU64_MOV_X,
824 [BPF_ALU64 | BPF_MOV | BPF_K] = &&ALU64_MOV_K,
825 [BPF_ALU64 | BPF_ARSH | BPF_X] = &&ALU64_ARSH_X,
826 [BPF_ALU64 | BPF_ARSH | BPF_K] = &&ALU64_ARSH_K,
827 [BPF_ALU64 | BPF_DIV | BPF_X] = &&ALU64_DIV_X,
828 [BPF_ALU64 | BPF_DIV | BPF_K] = &&ALU64_DIV_K,
829 [BPF_ALU64 | BPF_MOD | BPF_X] = &&ALU64_MOD_X,
830 [BPF_ALU64 | BPF_MOD | BPF_K] = &&ALU64_MOD_K,
831 [BPF_ALU64 | BPF_NEG] = &&ALU64_NEG,
832 /* Call instruction */
833 [BPF_JMP | BPF_CALL] = &&JMP_CALL,
834 [BPF_JMP | BPF_CALL | BPF_X] = &&JMP_TAIL_CALL,
836 [BPF_JMP | BPF_JA] = &&JMP_JA,
837 [BPF_JMP | BPF_JEQ | BPF_X] = &&JMP_JEQ_X,
838 [BPF_JMP | BPF_JEQ | BPF_K] = &&JMP_JEQ_K,
839 [BPF_JMP | BPF_JNE | BPF_X] = &&JMP_JNE_X,
840 [BPF_JMP | BPF_JNE | BPF_K] = &&JMP_JNE_K,
841 [BPF_JMP | BPF_JGT | BPF_X] = &&JMP_JGT_X,
842 [BPF_JMP | BPF_JGT | BPF_K] = &&JMP_JGT_K,
843 [BPF_JMP | BPF_JGE | BPF_X] = &&JMP_JGE_X,
844 [BPF_JMP | BPF_JGE | BPF_K] = &&JMP_JGE_K,
845 [BPF_JMP | BPF_JSGT | BPF_X] = &&JMP_JSGT_X,
846 [BPF_JMP | BPF_JSGT | BPF_K] = &&JMP_JSGT_K,
847 [BPF_JMP | BPF_JSGE | BPF_X] = &&JMP_JSGE_X,
848 [BPF_JMP | BPF_JSGE | BPF_K] = &&JMP_JSGE_K,
849 [BPF_JMP | BPF_JSET | BPF_X] = &&JMP_JSET_X,
850 [BPF_JMP | BPF_JSET | BPF_K] = &&JMP_JSET_K,
852 [BPF_JMP | BPF_EXIT] = &&JMP_EXIT,
853 /* Store instructions */
854 [BPF_STX | BPF_MEM | BPF_B] = &&STX_MEM_B,
855 [BPF_STX | BPF_MEM | BPF_H] = &&STX_MEM_H,
856 [BPF_STX | BPF_MEM | BPF_W] = &&STX_MEM_W,
857 [BPF_STX | BPF_MEM | BPF_DW] = &&STX_MEM_DW,
858 [BPF_STX | BPF_XADD | BPF_W] = &&STX_XADD_W,
859 [BPF_STX | BPF_XADD | BPF_DW] = &&STX_XADD_DW,
860 [BPF_ST | BPF_MEM | BPF_B] = &&ST_MEM_B,
861 [BPF_ST | BPF_MEM | BPF_H] = &&ST_MEM_H,
862 [BPF_ST | BPF_MEM | BPF_W] = &&ST_MEM_W,
863 [BPF_ST | BPF_MEM | BPF_DW] = &&ST_MEM_DW,
864 /* Load instructions */
865 [BPF_LDX | BPF_MEM | BPF_B] = &&LDX_MEM_B,
866 [BPF_LDX | BPF_MEM | BPF_H] = &&LDX_MEM_H,
867 [BPF_LDX | BPF_MEM | BPF_W] = &&LDX_MEM_W,
868 [BPF_LDX | BPF_MEM | BPF_DW] = &&LDX_MEM_DW,
869 [BPF_LD | BPF_ABS | BPF_W] = &&LD_ABS_W,
870 [BPF_LD | BPF_ABS | BPF_H] = &&LD_ABS_H,
871 [BPF_LD | BPF_ABS | BPF_B] = &&LD_ABS_B,
872 [BPF_LD | BPF_IND | BPF_W] = &&LD_IND_W,
873 [BPF_LD | BPF_IND | BPF_H] = &&LD_IND_H,
874 [BPF_LD | BPF_IND | BPF_B] = &&LD_IND_B,
875 [BPF_LD | BPF_IMM | BPF_DW] = &&LD_IMM_DW,
877 u32 tail_call_cnt = 0;
881 #define CONT ({ insn++; goto select_insn; })
882 #define CONT_JMP ({ insn++; goto select_insn; })
884 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)];
885 ARG1 = (u64) (unsigned long) ctx;
888 goto *jumptable[insn->code];
891 #define ALU(OPCODE, OP) \
892 ALU64_##OPCODE##_X: \
896 DST = (u32) DST OP (u32) SRC; \
898 ALU64_##OPCODE##_K: \
902 DST = (u32) DST OP (u32) IMM; \
933 DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
937 (*(s64 *) &DST) >>= SRC;
940 (*(s64 *) &DST) >>= IMM;
943 if (unlikely(SRC == 0))
945 div64_u64_rem(DST, SRC, &tmp);
949 if (unlikely(SRC == 0))
952 DST = do_div(tmp, (u32) SRC);
955 div64_u64_rem(DST, IMM, &tmp);
960 DST = do_div(tmp, (u32) IMM);
963 if (unlikely(SRC == 0))
965 DST = div64_u64(DST, SRC);
968 if (unlikely(SRC == 0))
971 do_div(tmp, (u32) SRC);
975 DST = div64_u64(DST, IMM);
979 do_div(tmp, (u32) IMM);
985 DST = (__force u16) cpu_to_be16(DST);
988 DST = (__force u32) cpu_to_be32(DST);
991 DST = (__force u64) cpu_to_be64(DST);
998 DST = (__force u16) cpu_to_le16(DST);
1001 DST = (__force u32) cpu_to_le32(DST);
1004 DST = (__force u64) cpu_to_le64(DST);
1011 /* Function call scratches BPF_R1-BPF_R5 registers,
1012 * preserves BPF_R6-BPF_R9, and stores return value
1015 BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
1020 struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
1021 struct bpf_array *array = container_of(map, struct bpf_array, map);
1022 struct bpf_prog *prog;
1025 if (unlikely(index >= array->map.max_entries))
1027 if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
1032 prog = READ_ONCE(array->ptrs[index]);
1036 /* ARG1 at this point is guaranteed to point to CTX from
1037 * the verifier side due to the fact that the tail call is
1038 * handeled like a helper, that is, bpf_tail_call_proto,
1039 * where arg1_type is ARG_PTR_TO_CTX.
1041 insn = prog->insnsi;
1099 if (((s64) DST) > ((s64) SRC)) {
1105 if (((s64) DST) > ((s64) IMM)) {
1111 if (((s64) DST) >= ((s64) SRC)) {
1117 if (((s64) DST) >= ((s64) IMM)) {
1137 /* STX and ST and LDX*/
1138 #define LDST(SIZEOP, SIZE) \
1140 *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \
1143 *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \
1146 DST = *(SIZE *)(unsigned long) (SRC + insn->off); \
1154 STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
1155 atomic_add((u32) SRC, (atomic_t *)(unsigned long)
1158 STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
1159 atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
1162 LD_ABS_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + imm32)) */
1165 /* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are only
1166 * appearing in the programs where ctx == skb
1167 * (see may_access_skb() in the verifier). All programs
1168 * keep 'ctx' in regs[BPF_REG_CTX] == BPF_R6,
1169 * bpf_convert_filter() saves it in BPF_R6, internal BPF
1170 * verifier will check that BPF_R6 == ctx.
1172 * BPF_ABS and BPF_IND are wrappers of function calls,
1173 * so they scratch BPF_R1-BPF_R5 registers, preserve
1174 * BPF_R6-BPF_R9, and store return value into BPF_R0.
1177 * ctx == skb == BPF_R6 == CTX
1180 * SRC == any register
1181 * IMM == 32-bit immediate
1184 * BPF_R0 - 8/16/32-bit skb data converted to cpu endianness
1187 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 4, &tmp);
1188 if (likely(ptr != NULL)) {
1189 BPF_R0 = get_unaligned_be32(ptr);
1194 LD_ABS_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + imm32)) */
1197 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 2, &tmp);
1198 if (likely(ptr != NULL)) {
1199 BPF_R0 = get_unaligned_be16(ptr);
1204 LD_ABS_B: /* BPF_R0 = *(u8 *) (skb->data + imm32) */
1207 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 1, &tmp);
1208 if (likely(ptr != NULL)) {
1209 BPF_R0 = *(u8 *)ptr;
1214 LD_IND_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + src_reg + imm32)) */
1217 LD_IND_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + src_reg + imm32)) */
1220 LD_IND_B: /* BPF_R0 = *(u8 *) (skb->data + src_reg + imm32) */
1225 /* If we ever reach this, we have a bug somewhere. */
1226 WARN_RATELIMIT(1, "unknown opcode %02x\n", insn->code);
1229 STACK_FRAME_NON_STANDARD(__bpf_prog_run); /* jump table */
1231 bool bpf_prog_array_compatible(struct bpf_array *array,
1232 const struct bpf_prog *fp)
1234 if (!array->owner_prog_type) {
1235 /* There's no owner yet where we could check for
1238 array->owner_prog_type = fp->type;
1239 array->owner_jited = fp->jited;
1244 return array->owner_prog_type == fp->type &&
1245 array->owner_jited == fp->jited;
1248 static int bpf_check_tail_call(const struct bpf_prog *fp)
1250 struct bpf_prog_aux *aux = fp->aux;
1253 for (i = 0; i < aux->used_map_cnt; i++) {
1254 struct bpf_map *map = aux->used_maps[i];
1255 struct bpf_array *array;
1257 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1260 array = container_of(map, struct bpf_array, map);
1261 if (!bpf_prog_array_compatible(array, fp))
1269 * bpf_prog_select_runtime - select exec runtime for BPF program
1270 * @fp: bpf_prog populated with internal BPF program
1271 * @err: pointer to error variable
1273 * Try to JIT eBPF program, if JIT is not available, use interpreter.
1274 * The BPF program will be executed via BPF_PROG_RUN() macro.
1276 struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
1278 fp->bpf_func = (void *) __bpf_prog_run;
1280 /* eBPF JITs can rewrite the program in case constant
1281 * blinding is active. However, in case of error during
1282 * blinding, bpf_int_jit_compile() must always return a
1283 * valid program, which in this case would simply not
1284 * be JITed, but falls back to the interpreter.
1286 fp = bpf_int_jit_compile(fp);
1287 bpf_prog_lock_ro(fp);
1289 /* The tail call compatibility check can only be done at
1290 * this late stage as we need to determine, if we deal
1291 * with JITed or non JITed program concatenations and not
1292 * all eBPF JITs might immediately support all features.
1294 *err = bpf_check_tail_call(fp);
1298 EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
1300 static void bpf_prog_free_deferred(struct work_struct *work)
1302 struct bpf_prog_aux *aux;
1304 aux = container_of(work, struct bpf_prog_aux, work);
1305 bpf_jit_free(aux->prog);
1308 /* Free internal BPF program */
1309 void bpf_prog_free(struct bpf_prog *fp)
1311 struct bpf_prog_aux *aux = fp->aux;
1313 INIT_WORK(&aux->work, bpf_prog_free_deferred);
1314 schedule_work(&aux->work);
1316 EXPORT_SYMBOL_GPL(bpf_prog_free);
1318 /* RNG for unpriviledged user space with separated state from prandom_u32(). */
1319 static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
1321 void bpf_user_rnd_init_once(void)
1323 prandom_init_once(&bpf_user_rnd_state);
1326 BPF_CALL_0(bpf_user_rnd_u32)
1328 /* Should someone ever have the rather unwise idea to use some
1329 * of the registers passed into this function, then note that
1330 * this function is called from native eBPF and classic-to-eBPF
1331 * transformations. Register assignments from both sides are
1332 * different, f.e. classic always sets fn(ctx, A, X) here.
1334 struct rnd_state *state;
1337 state = &get_cpu_var(bpf_user_rnd_state);
1338 res = prandom_u32_state(state);
1339 put_cpu_var(bpf_user_rnd_state);
1344 /* Weak definitions of helper functions in case we don't have bpf syscall. */
1345 const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
1346 const struct bpf_func_proto bpf_map_update_elem_proto __weak;
1347 const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
1349 const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
1350 const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
1351 const struct bpf_func_proto bpf_get_numa_node_id_proto __weak;
1352 const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
1354 const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
1355 const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
1356 const struct bpf_func_proto bpf_get_current_comm_proto __weak;
1358 const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
1364 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1365 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
1370 /* Always built-in helper functions. */
1371 const struct bpf_func_proto bpf_tail_call_proto = {
1374 .ret_type = RET_VOID,
1375 .arg1_type = ARG_PTR_TO_CTX,
1376 .arg2_type = ARG_CONST_MAP_PTR,
1377 .arg3_type = ARG_ANYTHING,
1380 /* Stub for JITs that only support cBPF. eBPF programs are interpreted.
1381 * It is encouraged to implement bpf_int_jit_compile() instead, so that
1382 * eBPF and implicitly also cBPF can get JITed!
1384 struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
1389 /* Stub for JITs that support eBPF. All cBPF code gets transformed into
1390 * eBPF by the kernel and is later compiled by bpf_int_jit_compile().
1392 void __weak bpf_jit_compile(struct bpf_prog *prog)
1396 bool __weak bpf_helper_changes_pkt_data(void *func)
1401 /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
1402 * skb_copy_bits(), so provide a weak definition of it for NET-less config.
1404 int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
1410 /* All definitions of tracepoints related to BPF. */
1411 #define CREATE_TRACE_POINTS
1412 #include <linux/bpf_trace.h>
1414 EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception);
1416 EXPORT_TRACEPOINT_SYMBOL_GPL(bpf_prog_get_type);
1417 EXPORT_TRACEPOINT_SYMBOL_GPL(bpf_prog_put_rcu);