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_ZERO
| gfp_extra_flags
;
80 struct bpf_prog_aux
*aux
;
83 size
= round_up(size
, PAGE_SIZE
);
84 fp
= __vmalloc(size
, gfp_flags
, PAGE_KERNEL
);
88 aux
= kzalloc(sizeof(*aux
), GFP_KERNEL
| gfp_extra_flags
);
94 fp
->pages
= size
/ PAGE_SIZE
;
97 fp
->jit_requested
= ebpf_jit_enabled();
99 INIT_LIST_HEAD_RCU(&fp
->aux
->ksym_lnode
);
103 EXPORT_SYMBOL_GPL(bpf_prog_alloc
);
105 struct bpf_prog
*bpf_prog_realloc(struct bpf_prog
*fp_old
, unsigned int size
,
106 gfp_t gfp_extra_flags
)
108 gfp_t gfp_flags
= GFP_KERNEL
| __GFP_ZERO
| gfp_extra_flags
;
113 BUG_ON(fp_old
== NULL
);
115 size
= round_up(size
, PAGE_SIZE
);
116 pages
= size
/ PAGE_SIZE
;
117 if (pages
<= fp_old
->pages
)
120 delta
= pages
- fp_old
->pages
;
121 ret
= __bpf_prog_charge(fp_old
->aux
->user
, delta
);
125 fp
= __vmalloc(size
, gfp_flags
, PAGE_KERNEL
);
127 __bpf_prog_uncharge(fp_old
->aux
->user
, delta
);
129 memcpy(fp
, fp_old
, fp_old
->pages
* PAGE_SIZE
);
133 /* We keep fp->aux from fp_old around in the new
134 * reallocated structure.
137 __bpf_prog_free(fp_old
);
143 void __bpf_prog_free(struct bpf_prog
*fp
)
149 int bpf_prog_calc_tag(struct bpf_prog
*fp
)
151 const u32 bits_offset
= SHA_MESSAGE_BYTES
- sizeof(__be64
);
152 u32 raw_size
= bpf_prog_tag_scratch_size(fp
);
153 u32 digest
[SHA_DIGEST_WORDS
];
154 u32 ws
[SHA_WORKSPACE_WORDS
];
155 u32 i
, bsize
, psize
, blocks
;
156 struct bpf_insn
*dst
;
162 raw
= vmalloc(raw_size
);
167 memset(ws
, 0, sizeof(ws
));
169 /* We need to take out the map fd for the digest calculation
170 * since they are unstable from user space side.
173 for (i
= 0, was_ld_map
= false; i
< fp
->len
; i
++) {
174 dst
[i
] = fp
->insnsi
[i
];
176 dst
[i
].code
== (BPF_LD
| BPF_IMM
| BPF_DW
) &&
177 dst
[i
].src_reg
== BPF_PSEUDO_MAP_FD
) {
180 } else if (was_ld_map
&&
182 dst
[i
].dst_reg
== 0 &&
183 dst
[i
].src_reg
== 0 &&
192 psize
= bpf_prog_insn_size(fp
);
193 memset(&raw
[psize
], 0, raw_size
- psize
);
196 bsize
= round_up(psize
, SHA_MESSAGE_BYTES
);
197 blocks
= bsize
/ SHA_MESSAGE_BYTES
;
199 if (bsize
- psize
>= sizeof(__be64
)) {
200 bits
= (__be64
*)(todo
+ bsize
- sizeof(__be64
));
202 bits
= (__be64
*)(todo
+ bsize
+ bits_offset
);
205 *bits
= cpu_to_be64((psize
- 1) << 3);
208 sha_transform(digest
, todo
, ws
);
209 todo
+= SHA_MESSAGE_BYTES
;
212 result
= (__force __be32
*)digest
;
213 for (i
= 0; i
< SHA_DIGEST_WORDS
; i
++)
214 result
[i
] = cpu_to_be32(digest
[i
]);
215 memcpy(fp
->tag
, result
, sizeof(fp
->tag
));
221 static void bpf_adj_branches(struct bpf_prog
*prog
, u32 pos
, u32 delta
)
223 struct bpf_insn
*insn
= prog
->insnsi
;
224 u32 i
, insn_cnt
= prog
->len
;
229 for (i
= 0; i
< insn_cnt
; i
++, insn
++) {
231 if (BPF_CLASS(code
) != BPF_JMP
)
233 if (BPF_OP(code
) == BPF_EXIT
)
235 if (BPF_OP(code
) == BPF_CALL
) {
236 if (insn
->src_reg
== BPF_PSEUDO_CALL
)
243 off
= pseudo_call
? insn
->imm
: insn
->off
;
245 /* Adjust offset of jmps if we cross boundaries. */
246 if (i
< pos
&& i
+ off
+ 1 > pos
)
248 else if (i
> pos
+ delta
&& i
+ off
+ 1 <= pos
+ delta
)
258 struct bpf_prog
*bpf_patch_insn_single(struct bpf_prog
*prog
, u32 off
,
259 const struct bpf_insn
*patch
, u32 len
)
261 u32 insn_adj_cnt
, insn_rest
, insn_delta
= len
- 1;
262 struct bpf_prog
*prog_adj
;
264 /* Since our patchlet doesn't expand the image, we're done. */
265 if (insn_delta
== 0) {
266 memcpy(prog
->insnsi
+ off
, patch
, sizeof(*patch
));
270 insn_adj_cnt
= prog
->len
+ insn_delta
;
272 /* Several new instructions need to be inserted. Make room
273 * for them. Likely, there's no need for a new allocation as
274 * last page could have large enough tailroom.
276 prog_adj
= bpf_prog_realloc(prog
, bpf_prog_size(insn_adj_cnt
),
281 prog_adj
->len
= insn_adj_cnt
;
283 /* Patching happens in 3 steps:
285 * 1) Move over tail of insnsi from next instruction onwards,
286 * so we can patch the single target insn with one or more
287 * new ones (patching is always from 1 to n insns, n > 0).
288 * 2) Inject new instructions at the target location.
289 * 3) Adjust branch offsets if necessary.
291 insn_rest
= insn_adj_cnt
- off
- len
;
293 memmove(prog_adj
->insnsi
+ off
+ len
, prog_adj
->insnsi
+ off
+ 1,
294 sizeof(*patch
) * insn_rest
);
295 memcpy(prog_adj
->insnsi
+ off
, patch
, sizeof(*patch
) * len
);
297 bpf_adj_branches(prog_adj
, off
, insn_delta
);
302 #ifdef CONFIG_BPF_JIT
303 /* All BPF JIT sysctl knobs here. */
304 int bpf_jit_enable __read_mostly
= IS_BUILTIN(CONFIG_BPF_JIT_ALWAYS_ON
);
305 int bpf_jit_harden __read_mostly
;
306 int bpf_jit_kallsyms __read_mostly
;
308 static __always_inline
void
309 bpf_get_prog_addr_region(const struct bpf_prog
*prog
,
310 unsigned long *symbol_start
,
311 unsigned long *symbol_end
)
313 const struct bpf_binary_header
*hdr
= bpf_jit_binary_hdr(prog
);
314 unsigned long addr
= (unsigned long)hdr
;
316 WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog
));
318 *symbol_start
= addr
;
319 *symbol_end
= addr
+ hdr
->pages
* PAGE_SIZE
;
322 static void bpf_get_prog_name(const struct bpf_prog
*prog
, char *sym
)
324 const char *end
= sym
+ KSYM_NAME_LEN
;
326 BUILD_BUG_ON(sizeof("bpf_prog_") +
327 sizeof(prog
->tag
) * 2 +
328 /* name has been null terminated.
329 * We should need +1 for the '_' preceding
330 * the name. However, the null character
331 * is double counted between the name and the
332 * sizeof("bpf_prog_") above, so we omit
335 sizeof(prog
->aux
->name
) > KSYM_NAME_LEN
);
337 sym
+= snprintf(sym
, KSYM_NAME_LEN
, "bpf_prog_");
338 sym
= bin2hex(sym
, prog
->tag
, sizeof(prog
->tag
));
339 if (prog
->aux
->name
[0])
340 snprintf(sym
, (size_t)(end
- sym
), "_%s", prog
->aux
->name
);
345 static __always_inline
unsigned long
346 bpf_get_prog_addr_start(struct latch_tree_node
*n
)
348 unsigned long symbol_start
, symbol_end
;
349 const struct bpf_prog_aux
*aux
;
351 aux
= container_of(n
, struct bpf_prog_aux
, ksym_tnode
);
352 bpf_get_prog_addr_region(aux
->prog
, &symbol_start
, &symbol_end
);
357 static __always_inline
bool bpf_tree_less(struct latch_tree_node
*a
,
358 struct latch_tree_node
*b
)
360 return bpf_get_prog_addr_start(a
) < bpf_get_prog_addr_start(b
);
363 static __always_inline
int bpf_tree_comp(void *key
, struct latch_tree_node
*n
)
365 unsigned long val
= (unsigned long)key
;
366 unsigned long symbol_start
, symbol_end
;
367 const struct bpf_prog_aux
*aux
;
369 aux
= container_of(n
, struct bpf_prog_aux
, ksym_tnode
);
370 bpf_get_prog_addr_region(aux
->prog
, &symbol_start
, &symbol_end
);
372 if (val
< symbol_start
)
374 if (val
>= symbol_end
)
380 static const struct latch_tree_ops bpf_tree_ops
= {
381 .less
= bpf_tree_less
,
382 .comp
= bpf_tree_comp
,
385 static DEFINE_SPINLOCK(bpf_lock
);
386 static LIST_HEAD(bpf_kallsyms
);
387 static struct latch_tree_root bpf_tree __cacheline_aligned
;
389 static void bpf_prog_ksym_node_add(struct bpf_prog_aux
*aux
)
391 WARN_ON_ONCE(!list_empty(&aux
->ksym_lnode
));
392 list_add_tail_rcu(&aux
->ksym_lnode
, &bpf_kallsyms
);
393 latch_tree_insert(&aux
->ksym_tnode
, &bpf_tree
, &bpf_tree_ops
);
396 static void bpf_prog_ksym_node_del(struct bpf_prog_aux
*aux
)
398 if (list_empty(&aux
->ksym_lnode
))
401 latch_tree_erase(&aux
->ksym_tnode
, &bpf_tree
, &bpf_tree_ops
);
402 list_del_rcu(&aux
->ksym_lnode
);
405 static bool bpf_prog_kallsyms_candidate(const struct bpf_prog
*fp
)
407 return fp
->jited
&& !bpf_prog_was_classic(fp
);
410 static bool bpf_prog_kallsyms_verify_off(const struct bpf_prog
*fp
)
412 return list_empty(&fp
->aux
->ksym_lnode
) ||
413 fp
->aux
->ksym_lnode
.prev
== LIST_POISON2
;
416 void bpf_prog_kallsyms_add(struct bpf_prog
*fp
)
418 if (!bpf_prog_kallsyms_candidate(fp
) ||
419 !capable(CAP_SYS_ADMIN
))
422 spin_lock_bh(&bpf_lock
);
423 bpf_prog_ksym_node_add(fp
->aux
);
424 spin_unlock_bh(&bpf_lock
);
427 void bpf_prog_kallsyms_del(struct bpf_prog
*fp
)
429 if (!bpf_prog_kallsyms_candidate(fp
))
432 spin_lock_bh(&bpf_lock
);
433 bpf_prog_ksym_node_del(fp
->aux
);
434 spin_unlock_bh(&bpf_lock
);
437 static struct bpf_prog
*bpf_prog_kallsyms_find(unsigned long addr
)
439 struct latch_tree_node
*n
;
441 if (!bpf_jit_kallsyms_enabled())
444 n
= latch_tree_find((void *)addr
, &bpf_tree
, &bpf_tree_ops
);
446 container_of(n
, struct bpf_prog_aux
, ksym_tnode
)->prog
:
450 const char *__bpf_address_lookup(unsigned long addr
, unsigned long *size
,
451 unsigned long *off
, char *sym
)
453 unsigned long symbol_start
, symbol_end
;
454 struct bpf_prog
*prog
;
458 prog
= bpf_prog_kallsyms_find(addr
);
460 bpf_get_prog_addr_region(prog
, &symbol_start
, &symbol_end
);
461 bpf_get_prog_name(prog
, sym
);
465 *size
= symbol_end
- symbol_start
;
467 *off
= addr
- symbol_start
;
474 bool is_bpf_text_address(unsigned long addr
)
479 ret
= bpf_prog_kallsyms_find(addr
) != NULL
;
485 int bpf_get_kallsym(unsigned int symnum
, unsigned long *value
, char *type
,
488 unsigned long symbol_start
, symbol_end
;
489 struct bpf_prog_aux
*aux
;
493 if (!bpf_jit_kallsyms_enabled())
497 list_for_each_entry_rcu(aux
, &bpf_kallsyms
, ksym_lnode
) {
501 bpf_get_prog_addr_region(aux
->prog
, &symbol_start
, &symbol_end
);
502 bpf_get_prog_name(aux
->prog
, sym
);
504 *value
= symbol_start
;
505 *type
= BPF_SYM_ELF_TYPE
;
515 struct bpf_binary_header
*
516 bpf_jit_binary_alloc(unsigned int proglen
, u8
**image_ptr
,
517 unsigned int alignment
,
518 bpf_jit_fill_hole_t bpf_fill_ill_insns
)
520 struct bpf_binary_header
*hdr
;
521 unsigned int size
, hole
, start
;
523 /* Most of BPF filters are really small, but if some of them
524 * fill a page, allow at least 128 extra bytes to insert a
525 * random section of illegal instructions.
527 size
= round_up(proglen
+ sizeof(*hdr
) + 128, PAGE_SIZE
);
528 hdr
= module_alloc(size
);
532 /* Fill space with illegal/arch-dep instructions. */
533 bpf_fill_ill_insns(hdr
, size
);
535 hdr
->pages
= size
/ PAGE_SIZE
;
536 hole
= min_t(unsigned int, size
- (proglen
+ sizeof(*hdr
)),
537 PAGE_SIZE
- sizeof(*hdr
));
538 start
= (get_random_int() % hole
) & ~(alignment
- 1);
540 /* Leave a random number of instructions before BPF code. */
541 *image_ptr
= &hdr
->image
[start
];
546 void bpf_jit_binary_free(struct bpf_binary_header
*hdr
)
551 /* This symbol is only overridden by archs that have different
552 * requirements than the usual eBPF JITs, f.e. when they only
553 * implement cBPF JIT, do not set images read-only, etc.
555 void __weak
bpf_jit_free(struct bpf_prog
*fp
)
558 struct bpf_binary_header
*hdr
= bpf_jit_binary_hdr(fp
);
560 bpf_jit_binary_unlock_ro(hdr
);
561 bpf_jit_binary_free(hdr
);
563 WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp
));
566 bpf_prog_unlock_free(fp
);
569 static int bpf_jit_blind_insn(const struct bpf_insn
*from
,
570 const struct bpf_insn
*aux
,
571 struct bpf_insn
*to_buff
)
573 struct bpf_insn
*to
= to_buff
;
574 u32 imm_rnd
= get_random_int();
577 BUILD_BUG_ON(BPF_REG_AX
+ 1 != MAX_BPF_JIT_REG
);
578 BUILD_BUG_ON(MAX_BPF_REG
+ 1 != MAX_BPF_JIT_REG
);
580 if (from
->imm
== 0 &&
581 (from
->code
== (BPF_ALU
| BPF_MOV
| BPF_K
) ||
582 from
->code
== (BPF_ALU64
| BPF_MOV
| BPF_K
))) {
583 *to
++ = BPF_ALU64_REG(BPF_XOR
, from
->dst_reg
, from
->dst_reg
);
587 switch (from
->code
) {
588 case BPF_ALU
| BPF_ADD
| BPF_K
:
589 case BPF_ALU
| BPF_SUB
| BPF_K
:
590 case BPF_ALU
| BPF_AND
| BPF_K
:
591 case BPF_ALU
| BPF_OR
| BPF_K
:
592 case BPF_ALU
| BPF_XOR
| BPF_K
:
593 case BPF_ALU
| BPF_MUL
| BPF_K
:
594 case BPF_ALU
| BPF_MOV
| BPF_K
:
595 case BPF_ALU
| BPF_DIV
| BPF_K
:
596 case BPF_ALU
| BPF_MOD
| BPF_K
:
597 *to
++ = BPF_ALU32_IMM(BPF_MOV
, BPF_REG_AX
, imm_rnd
^ from
->imm
);
598 *to
++ = BPF_ALU32_IMM(BPF_XOR
, BPF_REG_AX
, imm_rnd
);
599 *to
++ = BPF_ALU32_REG(from
->code
, from
->dst_reg
, BPF_REG_AX
);
602 case BPF_ALU64
| BPF_ADD
| BPF_K
:
603 case BPF_ALU64
| BPF_SUB
| BPF_K
:
604 case BPF_ALU64
| BPF_AND
| BPF_K
:
605 case BPF_ALU64
| BPF_OR
| BPF_K
:
606 case BPF_ALU64
| BPF_XOR
| BPF_K
:
607 case BPF_ALU64
| BPF_MUL
| BPF_K
:
608 case BPF_ALU64
| BPF_MOV
| BPF_K
:
609 case BPF_ALU64
| BPF_DIV
| BPF_K
:
610 case BPF_ALU64
| BPF_MOD
| BPF_K
:
611 *to
++ = BPF_ALU64_IMM(BPF_MOV
, BPF_REG_AX
, imm_rnd
^ from
->imm
);
612 *to
++ = BPF_ALU64_IMM(BPF_XOR
, BPF_REG_AX
, imm_rnd
);
613 *to
++ = BPF_ALU64_REG(from
->code
, from
->dst_reg
, BPF_REG_AX
);
616 case BPF_JMP
| BPF_JEQ
| BPF_K
:
617 case BPF_JMP
| BPF_JNE
| BPF_K
:
618 case BPF_JMP
| BPF_JGT
| BPF_K
:
619 case BPF_JMP
| BPF_JLT
| BPF_K
:
620 case BPF_JMP
| BPF_JGE
| BPF_K
:
621 case BPF_JMP
| BPF_JLE
| BPF_K
:
622 case BPF_JMP
| BPF_JSGT
| BPF_K
:
623 case BPF_JMP
| BPF_JSLT
| BPF_K
:
624 case BPF_JMP
| BPF_JSGE
| BPF_K
:
625 case BPF_JMP
| BPF_JSLE
| BPF_K
:
626 case BPF_JMP
| BPF_JSET
| BPF_K
:
627 /* Accommodate for extra offset in case of a backjump. */
631 *to
++ = BPF_ALU64_IMM(BPF_MOV
, BPF_REG_AX
, imm_rnd
^ from
->imm
);
632 *to
++ = BPF_ALU64_IMM(BPF_XOR
, BPF_REG_AX
, imm_rnd
);
633 *to
++ = BPF_JMP_REG(from
->code
, from
->dst_reg
, BPF_REG_AX
, off
);
636 case BPF_LD
| BPF_ABS
| BPF_W
:
637 case BPF_LD
| BPF_ABS
| BPF_H
:
638 case BPF_LD
| BPF_ABS
| BPF_B
:
639 *to
++ = BPF_ALU64_IMM(BPF_MOV
, BPF_REG_AX
, imm_rnd
^ from
->imm
);
640 *to
++ = BPF_ALU64_IMM(BPF_XOR
, BPF_REG_AX
, imm_rnd
);
641 *to
++ = BPF_LD_IND(from
->code
, BPF_REG_AX
, 0);
644 case BPF_LD
| BPF_IND
| BPF_W
:
645 case BPF_LD
| BPF_IND
| BPF_H
:
646 case BPF_LD
| BPF_IND
| BPF_B
:
647 *to
++ = BPF_ALU64_IMM(BPF_MOV
, BPF_REG_AX
, imm_rnd
^ from
->imm
);
648 *to
++ = BPF_ALU64_IMM(BPF_XOR
, BPF_REG_AX
, imm_rnd
);
649 *to
++ = BPF_ALU32_REG(BPF_ADD
, BPF_REG_AX
, from
->src_reg
);
650 *to
++ = BPF_LD_IND(from
->code
, BPF_REG_AX
, 0);
653 case BPF_LD
| BPF_IMM
| BPF_DW
:
654 *to
++ = BPF_ALU64_IMM(BPF_MOV
, BPF_REG_AX
, imm_rnd
^ aux
[1].imm
);
655 *to
++ = BPF_ALU64_IMM(BPF_XOR
, BPF_REG_AX
, imm_rnd
);
656 *to
++ = BPF_ALU64_IMM(BPF_LSH
, BPF_REG_AX
, 32);
657 *to
++ = BPF_ALU64_REG(BPF_MOV
, aux
[0].dst_reg
, BPF_REG_AX
);
659 case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */
660 *to
++ = BPF_ALU32_IMM(BPF_MOV
, BPF_REG_AX
, imm_rnd
^ aux
[0].imm
);
661 *to
++ = BPF_ALU32_IMM(BPF_XOR
, BPF_REG_AX
, imm_rnd
);
662 *to
++ = BPF_ALU64_REG(BPF_OR
, aux
[0].dst_reg
, BPF_REG_AX
);
665 case BPF_ST
| BPF_MEM
| BPF_DW
:
666 case BPF_ST
| BPF_MEM
| BPF_W
:
667 case BPF_ST
| BPF_MEM
| BPF_H
:
668 case BPF_ST
| BPF_MEM
| BPF_B
:
669 *to
++ = BPF_ALU64_IMM(BPF_MOV
, BPF_REG_AX
, imm_rnd
^ from
->imm
);
670 *to
++ = BPF_ALU64_IMM(BPF_XOR
, BPF_REG_AX
, imm_rnd
);
671 *to
++ = BPF_STX_MEM(from
->code
, from
->dst_reg
, BPF_REG_AX
, from
->off
);
678 static struct bpf_prog
*bpf_prog_clone_create(struct bpf_prog
*fp_other
,
679 gfp_t gfp_extra_flags
)
681 gfp_t gfp_flags
= GFP_KERNEL
| __GFP_ZERO
| gfp_extra_flags
;
684 fp
= __vmalloc(fp_other
->pages
* PAGE_SIZE
, gfp_flags
, PAGE_KERNEL
);
686 /* aux->prog still points to the fp_other one, so
687 * when promoting the clone to the real program,
688 * this still needs to be adapted.
690 memcpy(fp
, fp_other
, fp_other
->pages
* PAGE_SIZE
);
696 static void bpf_prog_clone_free(struct bpf_prog
*fp
)
698 /* aux was stolen by the other clone, so we cannot free
699 * it from this path! It will be freed eventually by the
700 * other program on release.
702 * At this point, we don't need a deferred release since
703 * clone is guaranteed to not be locked.
709 void bpf_jit_prog_release_other(struct bpf_prog
*fp
, struct bpf_prog
*fp_other
)
711 /* We have to repoint aux->prog to self, as we don't
712 * know whether fp here is the clone or the original.
715 bpf_prog_clone_free(fp_other
);
718 struct bpf_prog
*bpf_jit_blind_constants(struct bpf_prog
*prog
)
720 struct bpf_insn insn_buff
[16], aux
[2];
721 struct bpf_prog
*clone
, *tmp
;
722 int insn_delta
, insn_cnt
;
723 struct bpf_insn
*insn
;
726 if (!bpf_jit_blinding_enabled(prog
) || prog
->blinded
)
729 clone
= bpf_prog_clone_create(prog
, GFP_USER
);
731 return ERR_PTR(-ENOMEM
);
733 insn_cnt
= clone
->len
;
734 insn
= clone
->insnsi
;
736 for (i
= 0; i
< insn_cnt
; i
++, insn
++) {
737 /* We temporarily need to hold the original ld64 insn
738 * so that we can still access the first part in the
739 * second blinding run.
741 if (insn
[0].code
== (BPF_LD
| BPF_IMM
| BPF_DW
) &&
743 memcpy(aux
, insn
, sizeof(aux
));
745 rewritten
= bpf_jit_blind_insn(insn
, aux
, insn_buff
);
749 tmp
= bpf_patch_insn_single(clone
, i
, insn_buff
, rewritten
);
751 /* Patching may have repointed aux->prog during
752 * realloc from the original one, so we need to
753 * fix it up here on error.
755 bpf_jit_prog_release_other(prog
, clone
);
756 return ERR_PTR(-ENOMEM
);
760 insn_delta
= rewritten
- 1;
762 /* Walk new program and skip insns we just inserted. */
763 insn
= clone
->insnsi
+ i
+ insn_delta
;
764 insn_cnt
+= insn_delta
;
771 #endif /* CONFIG_BPF_JIT */
773 /* Base function for offset calculation. Needs to go into .text section,
774 * therefore keeping it non-static as well; will also be used by JITs
775 * anyway later on, so do not let the compiler omit it. This also needs
776 * to go into kallsyms for correlation from e.g. bpftool, so naming
779 noinline u64
__bpf_call_base(u64 r1
, u64 r2
, u64 r3
, u64 r4
, u64 r5
)
783 EXPORT_SYMBOL_GPL(__bpf_call_base
);
785 /* All UAPI available opcodes. */
786 #define BPF_INSN_MAP(INSN_2, INSN_3) \
787 /* 32 bit ALU operations. */ \
788 /* Register based. */ \
789 INSN_3(ALU, ADD, X), \
790 INSN_3(ALU, SUB, X), \
791 INSN_3(ALU, AND, X), \
792 INSN_3(ALU, OR, X), \
793 INSN_3(ALU, LSH, X), \
794 INSN_3(ALU, RSH, X), \
795 INSN_3(ALU, XOR, X), \
796 INSN_3(ALU, MUL, X), \
797 INSN_3(ALU, MOV, X), \
798 INSN_3(ALU, DIV, X), \
799 INSN_3(ALU, MOD, X), \
801 INSN_3(ALU, END, TO_BE), \
802 INSN_3(ALU, END, TO_LE), \
803 /* Immediate based. */ \
804 INSN_3(ALU, ADD, K), \
805 INSN_3(ALU, SUB, K), \
806 INSN_3(ALU, AND, K), \
807 INSN_3(ALU, OR, K), \
808 INSN_3(ALU, LSH, K), \
809 INSN_3(ALU, RSH, K), \
810 INSN_3(ALU, XOR, K), \
811 INSN_3(ALU, MUL, K), \
812 INSN_3(ALU, MOV, K), \
813 INSN_3(ALU, DIV, K), \
814 INSN_3(ALU, MOD, K), \
815 /* 64 bit ALU operations. */ \
816 /* Register based. */ \
817 INSN_3(ALU64, ADD, X), \
818 INSN_3(ALU64, SUB, X), \
819 INSN_3(ALU64, AND, X), \
820 INSN_3(ALU64, OR, X), \
821 INSN_3(ALU64, LSH, X), \
822 INSN_3(ALU64, RSH, X), \
823 INSN_3(ALU64, XOR, X), \
824 INSN_3(ALU64, MUL, X), \
825 INSN_3(ALU64, MOV, X), \
826 INSN_3(ALU64, ARSH, X), \
827 INSN_3(ALU64, DIV, X), \
828 INSN_3(ALU64, MOD, X), \
829 INSN_2(ALU64, NEG), \
830 /* Immediate based. */ \
831 INSN_3(ALU64, ADD, K), \
832 INSN_3(ALU64, SUB, K), \
833 INSN_3(ALU64, AND, K), \
834 INSN_3(ALU64, OR, K), \
835 INSN_3(ALU64, LSH, K), \
836 INSN_3(ALU64, RSH, K), \
837 INSN_3(ALU64, XOR, K), \
838 INSN_3(ALU64, MUL, K), \
839 INSN_3(ALU64, MOV, K), \
840 INSN_3(ALU64, ARSH, K), \
841 INSN_3(ALU64, DIV, K), \
842 INSN_3(ALU64, MOD, K), \
843 /* Call instruction. */ \
845 /* Exit instruction. */ \
847 /* Jump instructions. */ \
848 /* Register based. */ \
849 INSN_3(JMP, JEQ, X), \
850 INSN_3(JMP, JNE, X), \
851 INSN_3(JMP, JGT, X), \
852 INSN_3(JMP, JLT, X), \
853 INSN_3(JMP, JGE, X), \
854 INSN_3(JMP, JLE, X), \
855 INSN_3(JMP, JSGT, X), \
856 INSN_3(JMP, JSLT, X), \
857 INSN_3(JMP, JSGE, X), \
858 INSN_3(JMP, JSLE, X), \
859 INSN_3(JMP, JSET, X), \
860 /* Immediate based. */ \
861 INSN_3(JMP, JEQ, K), \
862 INSN_3(JMP, JNE, K), \
863 INSN_3(JMP, JGT, K), \
864 INSN_3(JMP, JLT, K), \
865 INSN_3(JMP, JGE, K), \
866 INSN_3(JMP, JLE, K), \
867 INSN_3(JMP, JSGT, K), \
868 INSN_3(JMP, JSLT, K), \
869 INSN_3(JMP, JSGE, K), \
870 INSN_3(JMP, JSLE, K), \
871 INSN_3(JMP, JSET, K), \
873 /* Store instructions. */ \
874 /* Register based. */ \
875 INSN_3(STX, MEM, B), \
876 INSN_3(STX, MEM, H), \
877 INSN_3(STX, MEM, W), \
878 INSN_3(STX, MEM, DW), \
879 INSN_3(STX, XADD, W), \
880 INSN_3(STX, XADD, DW), \
881 /* Immediate based. */ \
882 INSN_3(ST, MEM, B), \
883 INSN_3(ST, MEM, H), \
884 INSN_3(ST, MEM, W), \
885 INSN_3(ST, MEM, DW), \
886 /* Load instructions. */ \
887 /* Register based. */ \
888 INSN_3(LDX, MEM, B), \
889 INSN_3(LDX, MEM, H), \
890 INSN_3(LDX, MEM, W), \
891 INSN_3(LDX, MEM, DW), \
892 /* Immediate based. */ \
893 INSN_3(LD, IMM, DW), \
894 /* Misc (old cBPF carry-over). */ \
895 INSN_3(LD, ABS, B), \
896 INSN_3(LD, ABS, H), \
897 INSN_3(LD, ABS, W), \
898 INSN_3(LD, IND, B), \
899 INSN_3(LD, IND, H), \
902 bool bpf_opcode_in_insntable(u8 code
)
904 #define BPF_INSN_2_TBL(x, y) [BPF_##x | BPF_##y] = true
905 #define BPF_INSN_3_TBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = true
906 static const bool public_insntable
[256] = {
908 /* Now overwrite non-defaults ... */
909 BPF_INSN_MAP(BPF_INSN_2_TBL
, BPF_INSN_3_TBL
),
911 #undef BPF_INSN_3_TBL
912 #undef BPF_INSN_2_TBL
913 return public_insntable
[code
];
916 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
918 * __bpf_prog_run - run eBPF program on a given context
919 * @ctx: is the data we are operating on
920 * @insn: is the array of eBPF instructions
922 * Decode and execute eBPF instructions.
924 static u64
___bpf_prog_run(u64
*regs
, const struct bpf_insn
*insn
, u64
*stack
)
927 #define BPF_INSN_2_LBL(x, y) [BPF_##x | BPF_##y] = &&x##_##y
928 #define BPF_INSN_3_LBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = &&x##_##y##_##z
929 static const void *jumptable
[256] = {
930 [0 ... 255] = &&default_label
,
931 /* Now overwrite non-defaults ... */
932 BPF_INSN_MAP(BPF_INSN_2_LBL
, BPF_INSN_3_LBL
),
933 /* Non-UAPI available opcodes. */
934 [BPF_JMP
| BPF_CALL_ARGS
] = &&JMP_CALL_ARGS
,
935 [BPF_JMP
| BPF_TAIL_CALL
] = &&JMP_TAIL_CALL
,
937 #undef BPF_INSN_3_LBL
938 #undef BPF_INSN_2_LBL
939 u32 tail_call_cnt
= 0;
943 #define CONT ({ insn++; goto select_insn; })
944 #define CONT_JMP ({ insn++; goto select_insn; })
947 goto *jumptable
[insn
->code
];
950 #define ALU(OPCODE, OP) \
951 ALU64_##OPCODE##_X: \
955 DST = (u32) DST OP (u32) SRC; \
957 ALU64_##OPCODE##_K: \
961 DST = (u32) DST OP (u32) IMM; \
992 DST
= (u64
) (u32
) insn
[0].imm
| ((u64
) (u32
) insn
[1].imm
) << 32;
996 (*(s64
*) &DST
) >>= SRC
;
999 (*(s64
*) &DST
) >>= IMM
;
1002 div64_u64_rem(DST
, SRC
, &tmp
);
1007 DST
= do_div(tmp
, (u32
) SRC
);
1010 div64_u64_rem(DST
, IMM
, &tmp
);
1015 DST
= do_div(tmp
, (u32
) IMM
);
1018 DST
= div64_u64(DST
, SRC
);
1022 do_div(tmp
, (u32
) SRC
);
1026 DST
= div64_u64(DST
, IMM
);
1030 do_div(tmp
, (u32
) IMM
);
1036 DST
= (__force u16
) cpu_to_be16(DST
);
1039 DST
= (__force u32
) cpu_to_be32(DST
);
1042 DST
= (__force u64
) cpu_to_be64(DST
);
1049 DST
= (__force u16
) cpu_to_le16(DST
);
1052 DST
= (__force u32
) cpu_to_le32(DST
);
1055 DST
= (__force u64
) cpu_to_le64(DST
);
1062 /* Function call scratches BPF_R1-BPF_R5 registers,
1063 * preserves BPF_R6-BPF_R9, and stores return value
1066 BPF_R0
= (__bpf_call_base
+ insn
->imm
)(BPF_R1
, BPF_R2
, BPF_R3
,
1071 BPF_R0
= (__bpf_call_base_args
+ insn
->imm
)(BPF_R1
, BPF_R2
,
1074 insn
+ insn
->off
+ 1);
1078 struct bpf_map
*map
= (struct bpf_map
*) (unsigned long) BPF_R2
;
1079 struct bpf_array
*array
= container_of(map
, struct bpf_array
, map
);
1080 struct bpf_prog
*prog
;
1083 if (unlikely(index
>= array
->map
.max_entries
))
1085 if (unlikely(tail_call_cnt
> MAX_TAIL_CALL_CNT
))
1090 prog
= READ_ONCE(array
->ptrs
[index
]);
1094 /* ARG1 at this point is guaranteed to point to CTX from
1095 * the verifier side due to the fact that the tail call is
1096 * handeled like a helper, that is, bpf_tail_call_proto,
1097 * where arg1_type is ARG_PTR_TO_CTX.
1099 insn
= prog
->insnsi
;
1181 if (((s64
) DST
) > ((s64
) SRC
)) {
1187 if (((s64
) DST
) > ((s64
) IMM
)) {
1193 if (((s64
) DST
) < ((s64
) SRC
)) {
1199 if (((s64
) DST
) < ((s64
) IMM
)) {
1205 if (((s64
) DST
) >= ((s64
) SRC
)) {
1211 if (((s64
) DST
) >= ((s64
) IMM
)) {
1217 if (((s64
) DST
) <= ((s64
) SRC
)) {
1223 if (((s64
) DST
) <= ((s64
) IMM
)) {
1243 /* STX and ST and LDX*/
1244 #define LDST(SIZEOP, SIZE) \
1246 *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \
1249 *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \
1252 DST = *(SIZE *)(unsigned long) (SRC + insn->off); \
1260 STX_XADD_W
: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
1261 atomic_add((u32
) SRC
, (atomic_t
*)(unsigned long)
1264 STX_XADD_DW
: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
1265 atomic64_add((u64
) SRC
, (atomic64_t
*)(unsigned long)
1268 LD_ABS_W
: /* BPF_R0 = ntohl(*(u32 *) (skb->data + imm32)) */
1271 /* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are only
1272 * appearing in the programs where ctx == skb
1273 * (see may_access_skb() in the verifier). All programs
1274 * keep 'ctx' in regs[BPF_REG_CTX] == BPF_R6,
1275 * bpf_convert_filter() saves it in BPF_R6, internal BPF
1276 * verifier will check that BPF_R6 == ctx.
1278 * BPF_ABS and BPF_IND are wrappers of function calls,
1279 * so they scratch BPF_R1-BPF_R5 registers, preserve
1280 * BPF_R6-BPF_R9, and store return value into BPF_R0.
1283 * ctx == skb == BPF_R6 == CTX
1286 * SRC == any register
1287 * IMM == 32-bit immediate
1290 * BPF_R0 - 8/16/32-bit skb data converted to cpu endianness
1293 ptr
= bpf_load_pointer((struct sk_buff
*) (unsigned long) CTX
, off
, 4, &tmp
);
1294 if (likely(ptr
!= NULL
)) {
1295 BPF_R0
= get_unaligned_be32(ptr
);
1300 LD_ABS_H
: /* BPF_R0 = ntohs(*(u16 *) (skb->data + imm32)) */
1303 ptr
= bpf_load_pointer((struct sk_buff
*) (unsigned long) CTX
, off
, 2, &tmp
);
1304 if (likely(ptr
!= NULL
)) {
1305 BPF_R0
= get_unaligned_be16(ptr
);
1310 LD_ABS_B
: /* BPF_R0 = *(u8 *) (skb->data + imm32) */
1313 ptr
= bpf_load_pointer((struct sk_buff
*) (unsigned long) CTX
, off
, 1, &tmp
);
1314 if (likely(ptr
!= NULL
)) {
1315 BPF_R0
= *(u8
*)ptr
;
1320 LD_IND_W
: /* BPF_R0 = ntohl(*(u32 *) (skb->data + src_reg + imm32)) */
1323 LD_IND_H
: /* BPF_R0 = ntohs(*(u16 *) (skb->data + src_reg + imm32)) */
1326 LD_IND_B
: /* BPF_R0 = *(u8 *) (skb->data + src_reg + imm32) */
1331 /* If we ever reach this, we have a bug somewhere. Die hard here
1332 * instead of just returning 0; we could be somewhere in a subprog,
1333 * so execution could continue otherwise which we do /not/ want.
1335 * Note, verifier whitelists all opcodes in bpf_opcode_in_insntable().
1337 pr_warn("BPF interpreter: unknown opcode %02x\n", insn
->code
);
1341 STACK_FRAME_NON_STANDARD(___bpf_prog_run
); /* jump table */
1343 #define PROG_NAME(stack_size) __bpf_prog_run##stack_size
1344 #define DEFINE_BPF_PROG_RUN(stack_size) \
1345 static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \
1347 u64 stack[stack_size / sizeof(u64)]; \
1348 u64 regs[MAX_BPF_REG]; \
1350 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1351 ARG1 = (u64) (unsigned long) ctx; \
1352 return ___bpf_prog_run(regs, insn, stack); \
1355 #define PROG_NAME_ARGS(stack_size) __bpf_prog_run_args##stack_size
1356 #define DEFINE_BPF_PROG_RUN_ARGS(stack_size) \
1357 static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \
1358 const struct bpf_insn *insn) \
1360 u64 stack[stack_size / sizeof(u64)]; \
1361 u64 regs[MAX_BPF_REG]; \
1363 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1369 return ___bpf_prog_run(regs, insn, stack); \
1372 #define EVAL1(FN, X) FN(X)
1373 #define EVAL2(FN, X, Y...) FN(X) EVAL1(FN, Y)
1374 #define EVAL3(FN, X, Y...) FN(X) EVAL2(FN, Y)
1375 #define EVAL4(FN, X, Y...) FN(X) EVAL3(FN, Y)
1376 #define EVAL5(FN, X, Y...) FN(X) EVAL4(FN, Y)
1377 #define EVAL6(FN, X, Y...) FN(X) EVAL5(FN, Y)
1379 EVAL6(DEFINE_BPF_PROG_RUN
, 32, 64, 96, 128, 160, 192);
1380 EVAL6(DEFINE_BPF_PROG_RUN
, 224, 256, 288, 320, 352, 384);
1381 EVAL4(DEFINE_BPF_PROG_RUN
, 416, 448, 480, 512);
1383 EVAL6(DEFINE_BPF_PROG_RUN_ARGS
, 32, 64, 96, 128, 160, 192);
1384 EVAL6(DEFINE_BPF_PROG_RUN_ARGS
, 224, 256, 288, 320, 352, 384);
1385 EVAL4(DEFINE_BPF_PROG_RUN_ARGS
, 416, 448, 480, 512);
1387 #define PROG_NAME_LIST(stack_size) PROG_NAME(stack_size),
1389 static unsigned int (*interpreters
[])(const void *ctx
,
1390 const struct bpf_insn
*insn
) = {
1391 EVAL6(PROG_NAME_LIST
, 32, 64, 96, 128, 160, 192)
1392 EVAL6(PROG_NAME_LIST
, 224, 256, 288, 320, 352, 384)
1393 EVAL4(PROG_NAME_LIST
, 416, 448, 480, 512)
1395 #undef PROG_NAME_LIST
1396 #define PROG_NAME_LIST(stack_size) PROG_NAME_ARGS(stack_size),
1397 static u64 (*interpreters_args
[])(u64 r1
, u64 r2
, u64 r3
, u64 r4
, u64 r5
,
1398 const struct bpf_insn
*insn
) = {
1399 EVAL6(PROG_NAME_LIST
, 32, 64, 96, 128, 160, 192)
1400 EVAL6(PROG_NAME_LIST
, 224, 256, 288, 320, 352, 384)
1401 EVAL4(PROG_NAME_LIST
, 416, 448, 480, 512)
1403 #undef PROG_NAME_LIST
1405 void bpf_patch_call_args(struct bpf_insn
*insn
, u32 stack_depth
)
1407 stack_depth
= max_t(u32
, stack_depth
, 1);
1408 insn
->off
= (s16
) insn
->imm
;
1409 insn
->imm
= interpreters_args
[(round_up(stack_depth
, 32) / 32) - 1] -
1410 __bpf_call_base_args
;
1411 insn
->code
= BPF_JMP
| BPF_CALL_ARGS
;
1415 static unsigned int __bpf_prog_ret0_warn(const void *ctx
,
1416 const struct bpf_insn
*insn
)
1418 /* If this handler ever gets executed, then BPF_JIT_ALWAYS_ON
1419 * is not working properly, so warn about it!
1426 bool bpf_prog_array_compatible(struct bpf_array
*array
,
1427 const struct bpf_prog
*fp
)
1429 if (fp
->kprobe_override
)
1432 if (!array
->owner_prog_type
) {
1433 /* There's no owner yet where we could check for
1436 array
->owner_prog_type
= fp
->type
;
1437 array
->owner_jited
= fp
->jited
;
1442 return array
->owner_prog_type
== fp
->type
&&
1443 array
->owner_jited
== fp
->jited
;
1446 static int bpf_check_tail_call(const struct bpf_prog
*fp
)
1448 struct bpf_prog_aux
*aux
= fp
->aux
;
1451 for (i
= 0; i
< aux
->used_map_cnt
; i
++) {
1452 struct bpf_map
*map
= aux
->used_maps
[i
];
1453 struct bpf_array
*array
;
1455 if (map
->map_type
!= BPF_MAP_TYPE_PROG_ARRAY
)
1458 array
= container_of(map
, struct bpf_array
, map
);
1459 if (!bpf_prog_array_compatible(array
, fp
))
1467 * bpf_prog_select_runtime - select exec runtime for BPF program
1468 * @fp: bpf_prog populated with internal BPF program
1469 * @err: pointer to error variable
1471 * Try to JIT eBPF program, if JIT is not available, use interpreter.
1472 * The BPF program will be executed via BPF_PROG_RUN() macro.
1474 struct bpf_prog
*bpf_prog_select_runtime(struct bpf_prog
*fp
, int *err
)
1476 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
1477 u32 stack_depth
= max_t(u32
, fp
->aux
->stack_depth
, 1);
1479 fp
->bpf_func
= interpreters
[(round_up(stack_depth
, 32) / 32) - 1];
1481 fp
->bpf_func
= __bpf_prog_ret0_warn
;
1484 /* eBPF JITs can rewrite the program in case constant
1485 * blinding is active. However, in case of error during
1486 * blinding, bpf_int_jit_compile() must always return a
1487 * valid program, which in this case would simply not
1488 * be JITed, but falls back to the interpreter.
1490 if (!bpf_prog_is_dev_bound(fp
->aux
)) {
1491 fp
= bpf_int_jit_compile(fp
);
1492 #ifdef CONFIG_BPF_JIT_ALWAYS_ON
1499 *err
= bpf_prog_offload_compile(fp
);
1503 bpf_prog_lock_ro(fp
);
1505 /* The tail call compatibility check can only be done at
1506 * this late stage as we need to determine, if we deal
1507 * with JITed or non JITed program concatenations and not
1508 * all eBPF JITs might immediately support all features.
1510 *err
= bpf_check_tail_call(fp
);
1514 EXPORT_SYMBOL_GPL(bpf_prog_select_runtime
);
1516 static unsigned int __bpf_prog_ret1(const void *ctx
,
1517 const struct bpf_insn
*insn
)
1522 static struct bpf_prog_dummy
{
1523 struct bpf_prog prog
;
1524 } dummy_bpf_prog
= {
1526 .bpf_func
= __bpf_prog_ret1
,
1530 /* to avoid allocating empty bpf_prog_array for cgroups that
1531 * don't have bpf program attached use one global 'empty_prog_array'
1532 * It will not be modified the caller of bpf_prog_array_alloc()
1533 * (since caller requested prog_cnt == 0)
1534 * that pointer should be 'freed' by bpf_prog_array_free()
1537 struct bpf_prog_array hdr
;
1538 struct bpf_prog
*null_prog
;
1539 } empty_prog_array
= {
1543 struct bpf_prog_array __rcu
*bpf_prog_array_alloc(u32 prog_cnt
, gfp_t flags
)
1546 return kzalloc(sizeof(struct bpf_prog_array
) +
1547 sizeof(struct bpf_prog
*) * (prog_cnt
+ 1),
1550 return &empty_prog_array
.hdr
;
1553 void bpf_prog_array_free(struct bpf_prog_array __rcu
*progs
)
1556 progs
== (struct bpf_prog_array __rcu
*)&empty_prog_array
.hdr
)
1558 kfree_rcu(progs
, rcu
);
1561 int bpf_prog_array_length(struct bpf_prog_array __rcu
*progs
)
1563 struct bpf_prog
**prog
;
1567 prog
= rcu_dereference(progs
)->progs
;
1568 for (; *prog
; prog
++)
1569 if (*prog
!= &dummy_bpf_prog
.prog
)
1575 int bpf_prog_array_copy_to_user(struct bpf_prog_array __rcu
*progs
,
1576 __u32 __user
*prog_ids
, u32 cnt
)
1578 struct bpf_prog
**prog
;
1579 unsigned long err
= 0;
1583 /* users of this function are doing:
1584 * cnt = bpf_prog_array_length();
1586 * bpf_prog_array_copy_to_user(..., cnt);
1587 * so below kcalloc doesn't need extra cnt > 0 check, but
1588 * bpf_prog_array_length() releases rcu lock and
1589 * prog array could have been swapped with empty or larger array,
1590 * so always copy 'cnt' prog_ids to the user.
1591 * In a rare race the user will see zero prog_ids
1593 ids
= kcalloc(cnt
, sizeof(u32
), GFP_USER
| __GFP_NOWARN
);
1597 prog
= rcu_dereference(progs
)->progs
;
1598 for (; *prog
; prog
++) {
1599 if (*prog
== &dummy_bpf_prog
.prog
)
1601 ids
[i
] = (*prog
)->aux
->id
;
1609 err
= copy_to_user(prog_ids
, ids
, cnt
* sizeof(u32
));
1618 void bpf_prog_array_delete_safe(struct bpf_prog_array __rcu
*progs
,
1619 struct bpf_prog
*old_prog
)
1621 struct bpf_prog
**prog
= progs
->progs
;
1623 for (; *prog
; prog
++)
1624 if (*prog
== old_prog
) {
1625 WRITE_ONCE(*prog
, &dummy_bpf_prog
.prog
);
1630 int bpf_prog_array_copy(struct bpf_prog_array __rcu
*old_array
,
1631 struct bpf_prog
*exclude_prog
,
1632 struct bpf_prog
*include_prog
,
1633 struct bpf_prog_array
**new_array
)
1635 int new_prog_cnt
, carry_prog_cnt
= 0;
1636 struct bpf_prog
**existing_prog
;
1637 struct bpf_prog_array
*array
;
1638 int new_prog_idx
= 0;
1640 /* Figure out how many existing progs we need to carry over to
1644 existing_prog
= old_array
->progs
;
1645 for (; *existing_prog
; existing_prog
++) {
1646 if (*existing_prog
!= exclude_prog
&&
1647 *existing_prog
!= &dummy_bpf_prog
.prog
)
1649 if (*existing_prog
== include_prog
)
1654 /* How many progs (not NULL) will be in the new array? */
1655 new_prog_cnt
= carry_prog_cnt
;
1659 /* Do we have any prog (not NULL) in the new array? */
1660 if (!new_prog_cnt
) {
1665 /* +1 as the end of prog_array is marked with NULL */
1666 array
= bpf_prog_array_alloc(new_prog_cnt
+ 1, GFP_KERNEL
);
1670 /* Fill in the new prog array */
1671 if (carry_prog_cnt
) {
1672 existing_prog
= old_array
->progs
;
1673 for (; *existing_prog
; existing_prog
++)
1674 if (*existing_prog
!= exclude_prog
&&
1675 *existing_prog
!= &dummy_bpf_prog
.prog
)
1676 array
->progs
[new_prog_idx
++] = *existing_prog
;
1679 array
->progs
[new_prog_idx
++] = include_prog
;
1680 array
->progs
[new_prog_idx
] = NULL
;
1685 int bpf_prog_array_copy_info(struct bpf_prog_array __rcu
*array
,
1686 __u32 __user
*prog_ids
, u32 request_cnt
,
1687 __u32 __user
*prog_cnt
)
1692 cnt
= bpf_prog_array_length(array
);
1694 if (copy_to_user(prog_cnt
, &cnt
, sizeof(cnt
)))
1697 /* return early if user requested only program count or nothing to copy */
1698 if (!request_cnt
|| !cnt
)
1701 return bpf_prog_array_copy_to_user(array
, prog_ids
, request_cnt
);
1704 static void bpf_prog_free_deferred(struct work_struct
*work
)
1706 struct bpf_prog_aux
*aux
;
1709 aux
= container_of(work
, struct bpf_prog_aux
, work
);
1710 if (bpf_prog_is_dev_bound(aux
))
1711 bpf_prog_offload_destroy(aux
->prog
);
1712 for (i
= 0; i
< aux
->func_cnt
; i
++)
1713 bpf_jit_free(aux
->func
[i
]);
1714 if (aux
->func_cnt
) {
1716 bpf_prog_unlock_free(aux
->prog
);
1718 bpf_jit_free(aux
->prog
);
1722 /* Free internal BPF program */
1723 void bpf_prog_free(struct bpf_prog
*fp
)
1725 struct bpf_prog_aux
*aux
= fp
->aux
;
1727 INIT_WORK(&aux
->work
, bpf_prog_free_deferred
);
1728 schedule_work(&aux
->work
);
1730 EXPORT_SYMBOL_GPL(bpf_prog_free
);
1732 /* RNG for unpriviledged user space with separated state from prandom_u32(). */
1733 static DEFINE_PER_CPU(struct rnd_state
, bpf_user_rnd_state
);
1735 void bpf_user_rnd_init_once(void)
1737 prandom_init_once(&bpf_user_rnd_state
);
1740 BPF_CALL_0(bpf_user_rnd_u32
)
1742 /* Should someone ever have the rather unwise idea to use some
1743 * of the registers passed into this function, then note that
1744 * this function is called from native eBPF and classic-to-eBPF
1745 * transformations. Register assignments from both sides are
1746 * different, f.e. classic always sets fn(ctx, A, X) here.
1748 struct rnd_state
*state
;
1751 state
= &get_cpu_var(bpf_user_rnd_state
);
1752 res
= prandom_u32_state(state
);
1753 put_cpu_var(bpf_user_rnd_state
);
1758 /* Weak definitions of helper functions in case we don't have bpf syscall. */
1759 const struct bpf_func_proto bpf_map_lookup_elem_proto __weak
;
1760 const struct bpf_func_proto bpf_map_update_elem_proto __weak
;
1761 const struct bpf_func_proto bpf_map_delete_elem_proto __weak
;
1763 const struct bpf_func_proto bpf_get_prandom_u32_proto __weak
;
1764 const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak
;
1765 const struct bpf_func_proto bpf_get_numa_node_id_proto __weak
;
1766 const struct bpf_func_proto bpf_ktime_get_ns_proto __weak
;
1768 const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak
;
1769 const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak
;
1770 const struct bpf_func_proto bpf_get_current_comm_proto __weak
;
1771 const struct bpf_func_proto bpf_sock_map_update_proto __weak
;
1773 const struct bpf_func_proto
* __weak
bpf_get_trace_printk_proto(void)
1779 bpf_event_output(struct bpf_map
*map
, u64 flags
, void *meta
, u64 meta_size
,
1780 void *ctx
, u64 ctx_size
, bpf_ctx_copy_t ctx_copy
)
1785 /* Always built-in helper functions. */
1786 const struct bpf_func_proto bpf_tail_call_proto
= {
1789 .ret_type
= RET_VOID
,
1790 .arg1_type
= ARG_PTR_TO_CTX
,
1791 .arg2_type
= ARG_CONST_MAP_PTR
,
1792 .arg3_type
= ARG_ANYTHING
,
1795 /* Stub for JITs that only support cBPF. eBPF programs are interpreted.
1796 * It is encouraged to implement bpf_int_jit_compile() instead, so that
1797 * eBPF and implicitly also cBPF can get JITed!
1799 struct bpf_prog
* __weak
bpf_int_jit_compile(struct bpf_prog
*prog
)
1804 /* Stub for JITs that support eBPF. All cBPF code gets transformed into
1805 * eBPF by the kernel and is later compiled by bpf_int_jit_compile().
1807 void __weak
bpf_jit_compile(struct bpf_prog
*prog
)
1811 bool __weak
bpf_helper_changes_pkt_data(void *func
)
1816 /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
1817 * skb_copy_bits(), so provide a weak definition of it for NET-less config.
1819 int __weak
skb_copy_bits(const struct sk_buff
*skb
, int offset
, void *to
,
1825 /* All definitions of tracepoints related to BPF. */
1826 #define CREATE_TRACE_POINTS
1827 #include <linux/bpf_trace.h>
1829 EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception
);
1831 /* These are only used within the BPF_SYSCALL code */
1832 #ifdef CONFIG_BPF_SYSCALL
1833 EXPORT_TRACEPOINT_SYMBOL_GPL(bpf_prog_get_type
);
1834 EXPORT_TRACEPOINT_SYMBOL_GPL(bpf_prog_put_rcu
);