bpf: add bpf_jit_limit knob to restrict unpriv allocations
[linux-2.6-microblaze.git] / kernel / bpf / core.c
1 /*
2  * Linux Socket Filter - Kernel level socket filtering
3  *
4  * Based on the design of the Berkeley Packet Filter. The new
5  * internal format has been designed by PLUMgrid:
6  *
7  *      Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
8  *
9  * Authors:
10  *
11  *      Jay Schulist <jschlst@samba.org>
12  *      Alexei Starovoitov <ast@plumgrid.com>
13  *      Daniel Borkmann <dborkman@redhat.com>
14  *
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.
19  *
20  * Andi Kleen - Fix a few bad bugs and races.
21  * Kris Katterjohn - Added many additional checks in bpf_check_classic()
22  */
23
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>
35
36 #include <asm/unaligned.h>
37
38 /* Registers */
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]
50
51 /* Named registers */
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]
57 #define IMM     insn->imm
58
59 /* No hurry in this branch
60  *
61  * Exported for the bpf jit load helper.
62  */
63 void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
64 {
65         u8 *ptr = NULL;
66
67         if (k >= SKF_NET_OFF)
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;
71
72         if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
73                 return ptr;
74
75         return NULL;
76 }
77
78 struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
79 {
80         gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
81         struct bpf_prog_aux *aux;
82         struct bpf_prog *fp;
83
84         size = round_up(size, PAGE_SIZE);
85         fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
86         if (fp == NULL)
87                 return NULL;
88
89         aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);
90         if (aux == NULL) {
91                 vfree(fp);
92                 return NULL;
93         }
94
95         fp->pages = size / PAGE_SIZE;
96         fp->aux = aux;
97         fp->aux->prog = fp;
98         fp->jit_requested = ebpf_jit_enabled();
99
100         INIT_LIST_HEAD_RCU(&fp->aux->ksym_lnode);
101
102         return fp;
103 }
104 EXPORT_SYMBOL_GPL(bpf_prog_alloc);
105
106 struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
107                                   gfp_t gfp_extra_flags)
108 {
109         gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
110         struct bpf_prog *fp;
111         u32 pages, delta;
112         int ret;
113
114         BUG_ON(fp_old == NULL);
115
116         size = round_up(size, PAGE_SIZE);
117         pages = size / PAGE_SIZE;
118         if (pages <= fp_old->pages)
119                 return fp_old;
120
121         delta = pages - fp_old->pages;
122         ret = __bpf_prog_charge(fp_old->aux->user, delta);
123         if (ret)
124                 return NULL;
125
126         fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
127         if (fp == NULL) {
128                 __bpf_prog_uncharge(fp_old->aux->user, delta);
129         } else {
130                 memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
131                 fp->pages = pages;
132                 fp->aux->prog = fp;
133
134                 /* We keep fp->aux from fp_old around in the new
135                  * reallocated structure.
136                  */
137                 fp_old->aux = NULL;
138                 __bpf_prog_free(fp_old);
139         }
140
141         return fp;
142 }
143
144 void __bpf_prog_free(struct bpf_prog *fp)
145 {
146         kfree(fp->aux);
147         vfree(fp);
148 }
149
150 int bpf_prog_calc_tag(struct bpf_prog *fp)
151 {
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;
158         bool was_ld_map;
159         u8 *raw, *todo;
160         __be32 *result;
161         __be64 *bits;
162
163         raw = vmalloc(raw_size);
164         if (!raw)
165                 return -ENOMEM;
166
167         sha_init(digest);
168         memset(ws, 0, sizeof(ws));
169
170         /* We need to take out the map fd for the digest calculation
171          * since they are unstable from user space side.
172          */
173         dst = (void *)raw;
174         for (i = 0, was_ld_map = false; i < fp->len; i++) {
175                 dst[i] = fp->insnsi[i];
176                 if (!was_ld_map &&
177                     dst[i].code == (BPF_LD | BPF_IMM | BPF_DW) &&
178                     dst[i].src_reg == BPF_PSEUDO_MAP_FD) {
179                         was_ld_map = true;
180                         dst[i].imm = 0;
181                 } else if (was_ld_map &&
182                            dst[i].code == 0 &&
183                            dst[i].dst_reg == 0 &&
184                            dst[i].src_reg == 0 &&
185                            dst[i].off == 0) {
186                         was_ld_map = false;
187                         dst[i].imm = 0;
188                 } else {
189                         was_ld_map = false;
190                 }
191         }
192
193         psize = bpf_prog_insn_size(fp);
194         memset(&raw[psize], 0, raw_size - psize);
195         raw[psize++] = 0x80;
196
197         bsize  = round_up(psize, SHA_MESSAGE_BYTES);
198         blocks = bsize / SHA_MESSAGE_BYTES;
199         todo   = raw;
200         if (bsize - psize >= sizeof(__be64)) {
201                 bits = (__be64 *)(todo + bsize - sizeof(__be64));
202         } else {
203                 bits = (__be64 *)(todo + bsize + bits_offset);
204                 blocks++;
205         }
206         *bits = cpu_to_be64((psize - 1) << 3);
207
208         while (blocks--) {
209                 sha_transform(digest, todo, ws);
210                 todo += SHA_MESSAGE_BYTES;
211         }
212
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));
217
218         vfree(raw);
219         return 0;
220 }
221
222 static int bpf_adj_delta_to_imm(struct bpf_insn *insn, u32 pos, u32 delta,
223                                 u32 curr, const bool probe_pass)
224 {
225         const s64 imm_min = S32_MIN, imm_max = S32_MAX;
226         s64 imm = insn->imm;
227
228         if (curr < pos && curr + imm + 1 > pos)
229                 imm += delta;
230         else if (curr > pos + delta && curr + imm + 1 <= pos + delta)
231                 imm -= delta;
232         if (imm < imm_min || imm > imm_max)
233                 return -ERANGE;
234         if (!probe_pass)
235                 insn->imm = imm;
236         return 0;
237 }
238
239 static int bpf_adj_delta_to_off(struct bpf_insn *insn, u32 pos, u32 delta,
240                                 u32 curr, const bool probe_pass)
241 {
242         const s32 off_min = S16_MIN, off_max = S16_MAX;
243         s32 off = insn->off;
244
245         if (curr < pos && curr + off + 1 > pos)
246                 off += delta;
247         else if (curr > pos + delta && curr + off + 1 <= pos + delta)
248                 off -= delta;
249         if (off < off_min || off > off_max)
250                 return -ERANGE;
251         if (!probe_pass)
252                 insn->off = off;
253         return 0;
254 }
255
256 static int bpf_adj_branches(struct bpf_prog *prog, u32 pos, u32 delta,
257                             const bool probe_pass)
258 {
259         u32 i, insn_cnt = prog->len + (probe_pass ? delta : 0);
260         struct bpf_insn *insn = prog->insnsi;
261         int ret = 0;
262
263         for (i = 0; i < insn_cnt; i++, insn++) {
264                 u8 code;
265
266                 /* In the probing pass we still operate on the original,
267                  * unpatched image in order to check overflows before we
268                  * do any other adjustments. Therefore skip the patchlet.
269                  */
270                 if (probe_pass && i == pos) {
271                         i += delta + 1;
272                         insn++;
273                 }
274                 code = insn->code;
275                 if (BPF_CLASS(code) != BPF_JMP ||
276                     BPF_OP(code) == BPF_EXIT)
277                         continue;
278                 /* Adjust offset of jmps if we cross patch boundaries. */
279                 if (BPF_OP(code) == BPF_CALL) {
280                         if (insn->src_reg != BPF_PSEUDO_CALL)
281                                 continue;
282                         ret = bpf_adj_delta_to_imm(insn, pos, delta, i,
283                                                    probe_pass);
284                 } else {
285                         ret = bpf_adj_delta_to_off(insn, pos, delta, i,
286                                                    probe_pass);
287                 }
288                 if (ret)
289                         break;
290         }
291
292         return ret;
293 }
294
295 struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
296                                        const struct bpf_insn *patch, u32 len)
297 {
298         u32 insn_adj_cnt, insn_rest, insn_delta = len - 1;
299         const u32 cnt_max = S16_MAX;
300         struct bpf_prog *prog_adj;
301
302         /* Since our patchlet doesn't expand the image, we're done. */
303         if (insn_delta == 0) {
304                 memcpy(prog->insnsi + off, patch, sizeof(*patch));
305                 return prog;
306         }
307
308         insn_adj_cnt = prog->len + insn_delta;
309
310         /* Reject anything that would potentially let the insn->off
311          * target overflow when we have excessive program expansions.
312          * We need to probe here before we do any reallocation where
313          * we afterwards may not fail anymore.
314          */
315         if (insn_adj_cnt > cnt_max &&
316             bpf_adj_branches(prog, off, insn_delta, true))
317                 return NULL;
318
319         /* Several new instructions need to be inserted. Make room
320          * for them. Likely, there's no need for a new allocation as
321          * last page could have large enough tailroom.
322          */
323         prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),
324                                     GFP_USER);
325         if (!prog_adj)
326                 return NULL;
327
328         prog_adj->len = insn_adj_cnt;
329
330         /* Patching happens in 3 steps:
331          *
332          * 1) Move over tail of insnsi from next instruction onwards,
333          *    so we can patch the single target insn with one or more
334          *    new ones (patching is always from 1 to n insns, n > 0).
335          * 2) Inject new instructions at the target location.
336          * 3) Adjust branch offsets if necessary.
337          */
338         insn_rest = insn_adj_cnt - off - len;
339
340         memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1,
341                 sizeof(*patch) * insn_rest);
342         memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len);
343
344         /* We are guaranteed to not fail at this point, otherwise
345          * the ship has sailed to reverse to the original state. An
346          * overflow cannot happen at this point.
347          */
348         BUG_ON(bpf_adj_branches(prog_adj, off, insn_delta, false));
349
350         return prog_adj;
351 }
352
353 void bpf_prog_kallsyms_del_subprogs(struct bpf_prog *fp)
354 {
355         int i;
356
357         for (i = 0; i < fp->aux->func_cnt; i++)
358                 bpf_prog_kallsyms_del(fp->aux->func[i]);
359 }
360
361 void bpf_prog_kallsyms_del_all(struct bpf_prog *fp)
362 {
363         bpf_prog_kallsyms_del_subprogs(fp);
364         bpf_prog_kallsyms_del(fp);
365 }
366
367 #ifdef CONFIG_BPF_JIT
368 # define BPF_JIT_LIMIT_DEFAULT  (PAGE_SIZE * 40000)
369
370 /* All BPF JIT sysctl knobs here. */
371 int bpf_jit_enable   __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_ALWAYS_ON);
372 int bpf_jit_harden   __read_mostly;
373 int bpf_jit_kallsyms __read_mostly;
374 int bpf_jit_limit    __read_mostly = BPF_JIT_LIMIT_DEFAULT;
375
376 static __always_inline void
377 bpf_get_prog_addr_region(const struct bpf_prog *prog,
378                          unsigned long *symbol_start,
379                          unsigned long *symbol_end)
380 {
381         const struct bpf_binary_header *hdr = bpf_jit_binary_hdr(prog);
382         unsigned long addr = (unsigned long)hdr;
383
384         WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog));
385
386         *symbol_start = addr;
387         *symbol_end   = addr + hdr->pages * PAGE_SIZE;
388 }
389
390 static void bpf_get_prog_name(const struct bpf_prog *prog, char *sym)
391 {
392         const char *end = sym + KSYM_NAME_LEN;
393
394         BUILD_BUG_ON(sizeof("bpf_prog_") +
395                      sizeof(prog->tag) * 2 +
396                      /* name has been null terminated.
397                       * We should need +1 for the '_' preceding
398                       * the name.  However, the null character
399                       * is double counted between the name and the
400                       * sizeof("bpf_prog_") above, so we omit
401                       * the +1 here.
402                       */
403                      sizeof(prog->aux->name) > KSYM_NAME_LEN);
404
405         sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_");
406         sym  = bin2hex(sym, prog->tag, sizeof(prog->tag));
407         if (prog->aux->name[0])
408                 snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name);
409         else
410                 *sym = 0;
411 }
412
413 static __always_inline unsigned long
414 bpf_get_prog_addr_start(struct latch_tree_node *n)
415 {
416         unsigned long symbol_start, symbol_end;
417         const struct bpf_prog_aux *aux;
418
419         aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
420         bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
421
422         return symbol_start;
423 }
424
425 static __always_inline bool bpf_tree_less(struct latch_tree_node *a,
426                                           struct latch_tree_node *b)
427 {
428         return bpf_get_prog_addr_start(a) < bpf_get_prog_addr_start(b);
429 }
430
431 static __always_inline int bpf_tree_comp(void *key, struct latch_tree_node *n)
432 {
433         unsigned long val = (unsigned long)key;
434         unsigned long symbol_start, symbol_end;
435         const struct bpf_prog_aux *aux;
436
437         aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
438         bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
439
440         if (val < symbol_start)
441                 return -1;
442         if (val >= symbol_end)
443                 return  1;
444
445         return 0;
446 }
447
448 static const struct latch_tree_ops bpf_tree_ops = {
449         .less   = bpf_tree_less,
450         .comp   = bpf_tree_comp,
451 };
452
453 static DEFINE_SPINLOCK(bpf_lock);
454 static LIST_HEAD(bpf_kallsyms);
455 static struct latch_tree_root bpf_tree __cacheline_aligned;
456
457 static void bpf_prog_ksym_node_add(struct bpf_prog_aux *aux)
458 {
459         WARN_ON_ONCE(!list_empty(&aux->ksym_lnode));
460         list_add_tail_rcu(&aux->ksym_lnode, &bpf_kallsyms);
461         latch_tree_insert(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
462 }
463
464 static void bpf_prog_ksym_node_del(struct bpf_prog_aux *aux)
465 {
466         if (list_empty(&aux->ksym_lnode))
467                 return;
468
469         latch_tree_erase(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
470         list_del_rcu(&aux->ksym_lnode);
471 }
472
473 static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp)
474 {
475         return fp->jited && !bpf_prog_was_classic(fp);
476 }
477
478 static bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp)
479 {
480         return list_empty(&fp->aux->ksym_lnode) ||
481                fp->aux->ksym_lnode.prev == LIST_POISON2;
482 }
483
484 void bpf_prog_kallsyms_add(struct bpf_prog *fp)
485 {
486         if (!bpf_prog_kallsyms_candidate(fp) ||
487             !capable(CAP_SYS_ADMIN))
488                 return;
489
490         spin_lock_bh(&bpf_lock);
491         bpf_prog_ksym_node_add(fp->aux);
492         spin_unlock_bh(&bpf_lock);
493 }
494
495 void bpf_prog_kallsyms_del(struct bpf_prog *fp)
496 {
497         if (!bpf_prog_kallsyms_candidate(fp))
498                 return;
499
500         spin_lock_bh(&bpf_lock);
501         bpf_prog_ksym_node_del(fp->aux);
502         spin_unlock_bh(&bpf_lock);
503 }
504
505 static struct bpf_prog *bpf_prog_kallsyms_find(unsigned long addr)
506 {
507         struct latch_tree_node *n;
508
509         if (!bpf_jit_kallsyms_enabled())
510                 return NULL;
511
512         n = latch_tree_find((void *)addr, &bpf_tree, &bpf_tree_ops);
513         return n ?
514                container_of(n, struct bpf_prog_aux, ksym_tnode)->prog :
515                NULL;
516 }
517
518 const char *__bpf_address_lookup(unsigned long addr, unsigned long *size,
519                                  unsigned long *off, char *sym)
520 {
521         unsigned long symbol_start, symbol_end;
522         struct bpf_prog *prog;
523         char *ret = NULL;
524
525         rcu_read_lock();
526         prog = bpf_prog_kallsyms_find(addr);
527         if (prog) {
528                 bpf_get_prog_addr_region(prog, &symbol_start, &symbol_end);
529                 bpf_get_prog_name(prog, sym);
530
531                 ret = sym;
532                 if (size)
533                         *size = symbol_end - symbol_start;
534                 if (off)
535                         *off  = addr - symbol_start;
536         }
537         rcu_read_unlock();
538
539         return ret;
540 }
541
542 bool is_bpf_text_address(unsigned long addr)
543 {
544         bool ret;
545
546         rcu_read_lock();
547         ret = bpf_prog_kallsyms_find(addr) != NULL;
548         rcu_read_unlock();
549
550         return ret;
551 }
552
553 int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
554                     char *sym)
555 {
556         unsigned long symbol_start, symbol_end;
557         struct bpf_prog_aux *aux;
558         unsigned int it = 0;
559         int ret = -ERANGE;
560
561         if (!bpf_jit_kallsyms_enabled())
562                 return ret;
563
564         rcu_read_lock();
565         list_for_each_entry_rcu(aux, &bpf_kallsyms, ksym_lnode) {
566                 if (it++ != symnum)
567                         continue;
568
569                 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
570                 bpf_get_prog_name(aux->prog, sym);
571
572                 *value = symbol_start;
573                 *type  = BPF_SYM_ELF_TYPE;
574
575                 ret = 0;
576                 break;
577         }
578         rcu_read_unlock();
579
580         return ret;
581 }
582
583 static atomic_long_t bpf_jit_current;
584
585 #if defined(MODULES_VADDR)
586 static int __init bpf_jit_charge_init(void)
587 {
588         /* Only used as heuristic here to derive limit. */
589         bpf_jit_limit = min_t(u64, round_up((MODULES_END - MODULES_VADDR) >> 2,
590                                             PAGE_SIZE), INT_MAX);
591         return 0;
592 }
593 pure_initcall(bpf_jit_charge_init);
594 #endif
595
596 static int bpf_jit_charge_modmem(u32 pages)
597 {
598         if (atomic_long_add_return(pages, &bpf_jit_current) >
599             (bpf_jit_limit >> PAGE_SHIFT)) {
600                 if (!capable(CAP_SYS_ADMIN)) {
601                         atomic_long_sub(pages, &bpf_jit_current);
602                         return -EPERM;
603                 }
604         }
605
606         return 0;
607 }
608
609 static void bpf_jit_uncharge_modmem(u32 pages)
610 {
611         atomic_long_sub(pages, &bpf_jit_current);
612 }
613
614 struct bpf_binary_header *
615 bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
616                      unsigned int alignment,
617                      bpf_jit_fill_hole_t bpf_fill_ill_insns)
618 {
619         struct bpf_binary_header *hdr;
620         u32 size, hole, start, pages;
621
622         /* Most of BPF filters are really small, but if some of them
623          * fill a page, allow at least 128 extra bytes to insert a
624          * random section of illegal instructions.
625          */
626         size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
627         pages = size / PAGE_SIZE;
628
629         if (bpf_jit_charge_modmem(pages))
630                 return NULL;
631         hdr = module_alloc(size);
632         if (!hdr) {
633                 bpf_jit_uncharge_modmem(pages);
634                 return NULL;
635         }
636
637         /* Fill space with illegal/arch-dep instructions. */
638         bpf_fill_ill_insns(hdr, size);
639
640         hdr->pages = pages;
641         hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
642                      PAGE_SIZE - sizeof(*hdr));
643         start = (get_random_int() % hole) & ~(alignment - 1);
644
645         /* Leave a random number of instructions before BPF code. */
646         *image_ptr = &hdr->image[start];
647
648         return hdr;
649 }
650
651 void bpf_jit_binary_free(struct bpf_binary_header *hdr)
652 {
653         u32 pages = hdr->pages;
654
655         module_memfree(hdr);
656         bpf_jit_uncharge_modmem(pages);
657 }
658
659 /* This symbol is only overridden by archs that have different
660  * requirements than the usual eBPF JITs, f.e. when they only
661  * implement cBPF JIT, do not set images read-only, etc.
662  */
663 void __weak bpf_jit_free(struct bpf_prog *fp)
664 {
665         if (fp->jited) {
666                 struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp);
667
668                 bpf_jit_binary_unlock_ro(hdr);
669                 bpf_jit_binary_free(hdr);
670
671                 WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp));
672         }
673
674         bpf_prog_unlock_free(fp);
675 }
676
677 static int bpf_jit_blind_insn(const struct bpf_insn *from,
678                               const struct bpf_insn *aux,
679                               struct bpf_insn *to_buff)
680 {
681         struct bpf_insn *to = to_buff;
682         u32 imm_rnd = get_random_int();
683         s16 off;
684
685         BUILD_BUG_ON(BPF_REG_AX  + 1 != MAX_BPF_JIT_REG);
686         BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG);
687
688         if (from->imm == 0 &&
689             (from->code == (BPF_ALU   | BPF_MOV | BPF_K) ||
690              from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) {
691                 *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg);
692                 goto out;
693         }
694
695         switch (from->code) {
696         case BPF_ALU | BPF_ADD | BPF_K:
697         case BPF_ALU | BPF_SUB | BPF_K:
698         case BPF_ALU | BPF_AND | BPF_K:
699         case BPF_ALU | BPF_OR  | BPF_K:
700         case BPF_ALU | BPF_XOR | BPF_K:
701         case BPF_ALU | BPF_MUL | BPF_K:
702         case BPF_ALU | BPF_MOV | BPF_K:
703         case BPF_ALU | BPF_DIV | BPF_K:
704         case BPF_ALU | BPF_MOD | BPF_K:
705                 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
706                 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
707                 *to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX);
708                 break;
709
710         case BPF_ALU64 | BPF_ADD | BPF_K:
711         case BPF_ALU64 | BPF_SUB | BPF_K:
712         case BPF_ALU64 | BPF_AND | BPF_K:
713         case BPF_ALU64 | BPF_OR  | BPF_K:
714         case BPF_ALU64 | BPF_XOR | BPF_K:
715         case BPF_ALU64 | BPF_MUL | BPF_K:
716         case BPF_ALU64 | BPF_MOV | BPF_K:
717         case BPF_ALU64 | BPF_DIV | BPF_K:
718         case BPF_ALU64 | BPF_MOD | BPF_K:
719                 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
720                 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
721                 *to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX);
722                 break;
723
724         case BPF_JMP | BPF_JEQ  | BPF_K:
725         case BPF_JMP | BPF_JNE  | BPF_K:
726         case BPF_JMP | BPF_JGT  | BPF_K:
727         case BPF_JMP | BPF_JLT  | BPF_K:
728         case BPF_JMP | BPF_JGE  | BPF_K:
729         case BPF_JMP | BPF_JLE  | BPF_K:
730         case BPF_JMP | BPF_JSGT | BPF_K:
731         case BPF_JMP | BPF_JSLT | BPF_K:
732         case BPF_JMP | BPF_JSGE | BPF_K:
733         case BPF_JMP | BPF_JSLE | BPF_K:
734         case BPF_JMP | BPF_JSET | BPF_K:
735                 /* Accommodate for extra offset in case of a backjump. */
736                 off = from->off;
737                 if (off < 0)
738                         off -= 2;
739                 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
740                 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
741                 *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off);
742                 break;
743
744         case BPF_LD | BPF_IMM | BPF_DW:
745                 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm);
746                 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
747                 *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
748                 *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX);
749                 break;
750         case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */
751                 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm);
752                 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
753                 *to++ = BPF_ALU64_REG(BPF_OR,  aux[0].dst_reg, BPF_REG_AX);
754                 break;
755
756         case BPF_ST | BPF_MEM | BPF_DW:
757         case BPF_ST | BPF_MEM | BPF_W:
758         case BPF_ST | BPF_MEM | BPF_H:
759         case BPF_ST | BPF_MEM | BPF_B:
760                 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
761                 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
762                 *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off);
763                 break;
764         }
765 out:
766         return to - to_buff;
767 }
768
769 static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other,
770                                               gfp_t gfp_extra_flags)
771 {
772         gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
773         struct bpf_prog *fp;
774
775         fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags, PAGE_KERNEL);
776         if (fp != NULL) {
777                 /* aux->prog still points to the fp_other one, so
778                  * when promoting the clone to the real program,
779                  * this still needs to be adapted.
780                  */
781                 memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE);
782         }
783
784         return fp;
785 }
786
787 static void bpf_prog_clone_free(struct bpf_prog *fp)
788 {
789         /* aux was stolen by the other clone, so we cannot free
790          * it from this path! It will be freed eventually by the
791          * other program on release.
792          *
793          * At this point, we don't need a deferred release since
794          * clone is guaranteed to not be locked.
795          */
796         fp->aux = NULL;
797         __bpf_prog_free(fp);
798 }
799
800 void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other)
801 {
802         /* We have to repoint aux->prog to self, as we don't
803          * know whether fp here is the clone or the original.
804          */
805         fp->aux->prog = fp;
806         bpf_prog_clone_free(fp_other);
807 }
808
809 struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
810 {
811         struct bpf_insn insn_buff[16], aux[2];
812         struct bpf_prog *clone, *tmp;
813         int insn_delta, insn_cnt;
814         struct bpf_insn *insn;
815         int i, rewritten;
816
817         if (!bpf_jit_blinding_enabled(prog) || prog->blinded)
818                 return prog;
819
820         clone = bpf_prog_clone_create(prog, GFP_USER);
821         if (!clone)
822                 return ERR_PTR(-ENOMEM);
823
824         insn_cnt = clone->len;
825         insn = clone->insnsi;
826
827         for (i = 0; i < insn_cnt; i++, insn++) {
828                 /* We temporarily need to hold the original ld64 insn
829                  * so that we can still access the first part in the
830                  * second blinding run.
831                  */
832                 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) &&
833                     insn[1].code == 0)
834                         memcpy(aux, insn, sizeof(aux));
835
836                 rewritten = bpf_jit_blind_insn(insn, aux, insn_buff);
837                 if (!rewritten)
838                         continue;
839
840                 tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);
841                 if (!tmp) {
842                         /* Patching may have repointed aux->prog during
843                          * realloc from the original one, so we need to
844                          * fix it up here on error.
845                          */
846                         bpf_jit_prog_release_other(prog, clone);
847                         return ERR_PTR(-ENOMEM);
848                 }
849
850                 clone = tmp;
851                 insn_delta = rewritten - 1;
852
853                 /* Walk new program and skip insns we just inserted. */
854                 insn = clone->insnsi + i + insn_delta;
855                 insn_cnt += insn_delta;
856                 i        += insn_delta;
857         }
858
859         clone->blinded = 1;
860         return clone;
861 }
862 #endif /* CONFIG_BPF_JIT */
863
864 /* Base function for offset calculation. Needs to go into .text section,
865  * therefore keeping it non-static as well; will also be used by JITs
866  * anyway later on, so do not let the compiler omit it. This also needs
867  * to go into kallsyms for correlation from e.g. bpftool, so naming
868  * must not change.
869  */
870 noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
871 {
872         return 0;
873 }
874 EXPORT_SYMBOL_GPL(__bpf_call_base);
875
876 /* All UAPI available opcodes. */
877 #define BPF_INSN_MAP(INSN_2, INSN_3)            \
878         /* 32 bit ALU operations. */            \
879         /*   Register based. */                 \
880         INSN_3(ALU, ADD, X),                    \
881         INSN_3(ALU, SUB, X),                    \
882         INSN_3(ALU, AND, X),                    \
883         INSN_3(ALU, OR,  X),                    \
884         INSN_3(ALU, LSH, X),                    \
885         INSN_3(ALU, RSH, X),                    \
886         INSN_3(ALU, XOR, X),                    \
887         INSN_3(ALU, MUL, X),                    \
888         INSN_3(ALU, MOV, X),                    \
889         INSN_3(ALU, DIV, X),                    \
890         INSN_3(ALU, MOD, X),                    \
891         INSN_2(ALU, NEG),                       \
892         INSN_3(ALU, END, TO_BE),                \
893         INSN_3(ALU, END, TO_LE),                \
894         /*   Immediate based. */                \
895         INSN_3(ALU, ADD, K),                    \
896         INSN_3(ALU, SUB, K),                    \
897         INSN_3(ALU, AND, K),                    \
898         INSN_3(ALU, OR,  K),                    \
899         INSN_3(ALU, LSH, K),                    \
900         INSN_3(ALU, RSH, K),                    \
901         INSN_3(ALU, XOR, K),                    \
902         INSN_3(ALU, MUL, K),                    \
903         INSN_3(ALU, MOV, K),                    \
904         INSN_3(ALU, DIV, K),                    \
905         INSN_3(ALU, MOD, K),                    \
906         /* 64 bit ALU operations. */            \
907         /*   Register based. */                 \
908         INSN_3(ALU64, ADD,  X),                 \
909         INSN_3(ALU64, SUB,  X),                 \
910         INSN_3(ALU64, AND,  X),                 \
911         INSN_3(ALU64, OR,   X),                 \
912         INSN_3(ALU64, LSH,  X),                 \
913         INSN_3(ALU64, RSH,  X),                 \
914         INSN_3(ALU64, XOR,  X),                 \
915         INSN_3(ALU64, MUL,  X),                 \
916         INSN_3(ALU64, MOV,  X),                 \
917         INSN_3(ALU64, ARSH, X),                 \
918         INSN_3(ALU64, DIV,  X),                 \
919         INSN_3(ALU64, MOD,  X),                 \
920         INSN_2(ALU64, NEG),                     \
921         /*   Immediate based. */                \
922         INSN_3(ALU64, ADD,  K),                 \
923         INSN_3(ALU64, SUB,  K),                 \
924         INSN_3(ALU64, AND,  K),                 \
925         INSN_3(ALU64, OR,   K),                 \
926         INSN_3(ALU64, LSH,  K),                 \
927         INSN_3(ALU64, RSH,  K),                 \
928         INSN_3(ALU64, XOR,  K),                 \
929         INSN_3(ALU64, MUL,  K),                 \
930         INSN_3(ALU64, MOV,  K),                 \
931         INSN_3(ALU64, ARSH, K),                 \
932         INSN_3(ALU64, DIV,  K),                 \
933         INSN_3(ALU64, MOD,  K),                 \
934         /* Call instruction. */                 \
935         INSN_2(JMP, CALL),                      \
936         /* Exit instruction. */                 \
937         INSN_2(JMP, EXIT),                      \
938         /* Jump instructions. */                \
939         /*   Register based. */                 \
940         INSN_3(JMP, JEQ,  X),                   \
941         INSN_3(JMP, JNE,  X),                   \
942         INSN_3(JMP, JGT,  X),                   \
943         INSN_3(JMP, JLT,  X),                   \
944         INSN_3(JMP, JGE,  X),                   \
945         INSN_3(JMP, JLE,  X),                   \
946         INSN_3(JMP, JSGT, X),                   \
947         INSN_3(JMP, JSLT, X),                   \
948         INSN_3(JMP, JSGE, X),                   \
949         INSN_3(JMP, JSLE, X),                   \
950         INSN_3(JMP, JSET, X),                   \
951         /*   Immediate based. */                \
952         INSN_3(JMP, JEQ,  K),                   \
953         INSN_3(JMP, JNE,  K),                   \
954         INSN_3(JMP, JGT,  K),                   \
955         INSN_3(JMP, JLT,  K),                   \
956         INSN_3(JMP, JGE,  K),                   \
957         INSN_3(JMP, JLE,  K),                   \
958         INSN_3(JMP, JSGT, K),                   \
959         INSN_3(JMP, JSLT, K),                   \
960         INSN_3(JMP, JSGE, K),                   \
961         INSN_3(JMP, JSLE, K),                   \
962         INSN_3(JMP, JSET, K),                   \
963         INSN_2(JMP, JA),                        \
964         /* Store instructions. */               \
965         /*   Register based. */                 \
966         INSN_3(STX, MEM,  B),                   \
967         INSN_3(STX, MEM,  H),                   \
968         INSN_3(STX, MEM,  W),                   \
969         INSN_3(STX, MEM,  DW),                  \
970         INSN_3(STX, XADD, W),                   \
971         INSN_3(STX, XADD, DW),                  \
972         /*   Immediate based. */                \
973         INSN_3(ST, MEM, B),                     \
974         INSN_3(ST, MEM, H),                     \
975         INSN_3(ST, MEM, W),                     \
976         INSN_3(ST, MEM, DW),                    \
977         /* Load instructions. */                \
978         /*   Register based. */                 \
979         INSN_3(LDX, MEM, B),                    \
980         INSN_3(LDX, MEM, H),                    \
981         INSN_3(LDX, MEM, W),                    \
982         INSN_3(LDX, MEM, DW),                   \
983         /*   Immediate based. */                \
984         INSN_3(LD, IMM, DW)
985
986 bool bpf_opcode_in_insntable(u8 code)
987 {
988 #define BPF_INSN_2_TBL(x, y)    [BPF_##x | BPF_##y] = true
989 #define BPF_INSN_3_TBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = true
990         static const bool public_insntable[256] = {
991                 [0 ... 255] = false,
992                 /* Now overwrite non-defaults ... */
993                 BPF_INSN_MAP(BPF_INSN_2_TBL, BPF_INSN_3_TBL),
994                 /* UAPI exposed, but rewritten opcodes. cBPF carry-over. */
995                 [BPF_LD | BPF_ABS | BPF_B] = true,
996                 [BPF_LD | BPF_ABS | BPF_H] = true,
997                 [BPF_LD | BPF_ABS | BPF_W] = true,
998                 [BPF_LD | BPF_IND | BPF_B] = true,
999                 [BPF_LD | BPF_IND | BPF_H] = true,
1000                 [BPF_LD | BPF_IND | BPF_W] = true,
1001         };
1002 #undef BPF_INSN_3_TBL
1003 #undef BPF_INSN_2_TBL
1004         return public_insntable[code];
1005 }
1006
1007 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
1008 /**
1009  *      __bpf_prog_run - run eBPF program on a given context
1010  *      @ctx: is the data we are operating on
1011  *      @insn: is the array of eBPF instructions
1012  *
1013  * Decode and execute eBPF instructions.
1014  */
1015 static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack)
1016 {
1017         u64 tmp;
1018 #define BPF_INSN_2_LBL(x, y)    [BPF_##x | BPF_##y] = &&x##_##y
1019 #define BPF_INSN_3_LBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = &&x##_##y##_##z
1020         static const void *jumptable[256] = {
1021                 [0 ... 255] = &&default_label,
1022                 /* Now overwrite non-defaults ... */
1023                 BPF_INSN_MAP(BPF_INSN_2_LBL, BPF_INSN_3_LBL),
1024                 /* Non-UAPI available opcodes. */
1025                 [BPF_JMP | BPF_CALL_ARGS] = &&JMP_CALL_ARGS,
1026                 [BPF_JMP | BPF_TAIL_CALL] = &&JMP_TAIL_CALL,
1027         };
1028 #undef BPF_INSN_3_LBL
1029 #undef BPF_INSN_2_LBL
1030         u32 tail_call_cnt = 0;
1031
1032 #define CONT     ({ insn++; goto select_insn; })
1033 #define CONT_JMP ({ insn++; goto select_insn; })
1034
1035 select_insn:
1036         goto *jumptable[insn->code];
1037
1038         /* ALU */
1039 #define ALU(OPCODE, OP)                 \
1040         ALU64_##OPCODE##_X:             \
1041                 DST = DST OP SRC;       \
1042                 CONT;                   \
1043         ALU_##OPCODE##_X:               \
1044                 DST = (u32) DST OP (u32) SRC;   \
1045                 CONT;                   \
1046         ALU64_##OPCODE##_K:             \
1047                 DST = DST OP IMM;               \
1048                 CONT;                   \
1049         ALU_##OPCODE##_K:               \
1050                 DST = (u32) DST OP (u32) IMM;   \
1051                 CONT;
1052
1053         ALU(ADD,  +)
1054         ALU(SUB,  -)
1055         ALU(AND,  &)
1056         ALU(OR,   |)
1057         ALU(LSH, <<)
1058         ALU(RSH, >>)
1059         ALU(XOR,  ^)
1060         ALU(MUL,  *)
1061 #undef ALU
1062         ALU_NEG:
1063                 DST = (u32) -DST;
1064                 CONT;
1065         ALU64_NEG:
1066                 DST = -DST;
1067                 CONT;
1068         ALU_MOV_X:
1069                 DST = (u32) SRC;
1070                 CONT;
1071         ALU_MOV_K:
1072                 DST = (u32) IMM;
1073                 CONT;
1074         ALU64_MOV_X:
1075                 DST = SRC;
1076                 CONT;
1077         ALU64_MOV_K:
1078                 DST = IMM;
1079                 CONT;
1080         LD_IMM_DW:
1081                 DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
1082                 insn++;
1083                 CONT;
1084         ALU64_ARSH_X:
1085                 (*(s64 *) &DST) >>= SRC;
1086                 CONT;
1087         ALU64_ARSH_K:
1088                 (*(s64 *) &DST) >>= IMM;
1089                 CONT;
1090         ALU64_MOD_X:
1091                 div64_u64_rem(DST, SRC, &tmp);
1092                 DST = tmp;
1093                 CONT;
1094         ALU_MOD_X:
1095                 tmp = (u32) DST;
1096                 DST = do_div(tmp, (u32) SRC);
1097                 CONT;
1098         ALU64_MOD_K:
1099                 div64_u64_rem(DST, IMM, &tmp);
1100                 DST = tmp;
1101                 CONT;
1102         ALU_MOD_K:
1103                 tmp = (u32) DST;
1104                 DST = do_div(tmp, (u32) IMM);
1105                 CONT;
1106         ALU64_DIV_X:
1107                 DST = div64_u64(DST, SRC);
1108                 CONT;
1109         ALU_DIV_X:
1110                 tmp = (u32) DST;
1111                 do_div(tmp, (u32) SRC);
1112                 DST = (u32) tmp;
1113                 CONT;
1114         ALU64_DIV_K:
1115                 DST = div64_u64(DST, IMM);
1116                 CONT;
1117         ALU_DIV_K:
1118                 tmp = (u32) DST;
1119                 do_div(tmp, (u32) IMM);
1120                 DST = (u32) tmp;
1121                 CONT;
1122         ALU_END_TO_BE:
1123                 switch (IMM) {
1124                 case 16:
1125                         DST = (__force u16) cpu_to_be16(DST);
1126                         break;
1127                 case 32:
1128                         DST = (__force u32) cpu_to_be32(DST);
1129                         break;
1130                 case 64:
1131                         DST = (__force u64) cpu_to_be64(DST);
1132                         break;
1133                 }
1134                 CONT;
1135         ALU_END_TO_LE:
1136                 switch (IMM) {
1137                 case 16:
1138                         DST = (__force u16) cpu_to_le16(DST);
1139                         break;
1140                 case 32:
1141                         DST = (__force u32) cpu_to_le32(DST);
1142                         break;
1143                 case 64:
1144                         DST = (__force u64) cpu_to_le64(DST);
1145                         break;
1146                 }
1147                 CONT;
1148
1149         /* CALL */
1150         JMP_CALL:
1151                 /* Function call scratches BPF_R1-BPF_R5 registers,
1152                  * preserves BPF_R6-BPF_R9, and stores return value
1153                  * into BPF_R0.
1154                  */
1155                 BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
1156                                                        BPF_R4, BPF_R5);
1157                 CONT;
1158
1159         JMP_CALL_ARGS:
1160                 BPF_R0 = (__bpf_call_base_args + insn->imm)(BPF_R1, BPF_R2,
1161                                                             BPF_R3, BPF_R4,
1162                                                             BPF_R5,
1163                                                             insn + insn->off + 1);
1164                 CONT;
1165
1166         JMP_TAIL_CALL: {
1167                 struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
1168                 struct bpf_array *array = container_of(map, struct bpf_array, map);
1169                 struct bpf_prog *prog;
1170                 u32 index = BPF_R3;
1171
1172                 if (unlikely(index >= array->map.max_entries))
1173                         goto out;
1174                 if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
1175                         goto out;
1176
1177                 tail_call_cnt++;
1178
1179                 prog = READ_ONCE(array->ptrs[index]);
1180                 if (!prog)
1181                         goto out;
1182
1183                 /* ARG1 at this point is guaranteed to point to CTX from
1184                  * the verifier side due to the fact that the tail call is
1185                  * handeled like a helper, that is, bpf_tail_call_proto,
1186                  * where arg1_type is ARG_PTR_TO_CTX.
1187                  */
1188                 insn = prog->insnsi;
1189                 goto select_insn;
1190 out:
1191                 CONT;
1192         }
1193         /* JMP */
1194         JMP_JA:
1195                 insn += insn->off;
1196                 CONT;
1197         JMP_JEQ_X:
1198                 if (DST == SRC) {
1199                         insn += insn->off;
1200                         CONT_JMP;
1201                 }
1202                 CONT;
1203         JMP_JEQ_K:
1204                 if (DST == IMM) {
1205                         insn += insn->off;
1206                         CONT_JMP;
1207                 }
1208                 CONT;
1209         JMP_JNE_X:
1210                 if (DST != SRC) {
1211                         insn += insn->off;
1212                         CONT_JMP;
1213                 }
1214                 CONT;
1215         JMP_JNE_K:
1216                 if (DST != IMM) {
1217                         insn += insn->off;
1218                         CONT_JMP;
1219                 }
1220                 CONT;
1221         JMP_JGT_X:
1222                 if (DST > SRC) {
1223                         insn += insn->off;
1224                         CONT_JMP;
1225                 }
1226                 CONT;
1227         JMP_JGT_K:
1228                 if (DST > IMM) {
1229                         insn += insn->off;
1230                         CONT_JMP;
1231                 }
1232                 CONT;
1233         JMP_JLT_X:
1234                 if (DST < SRC) {
1235                         insn += insn->off;
1236                         CONT_JMP;
1237                 }
1238                 CONT;
1239         JMP_JLT_K:
1240                 if (DST < IMM) {
1241                         insn += insn->off;
1242                         CONT_JMP;
1243                 }
1244                 CONT;
1245         JMP_JGE_X:
1246                 if (DST >= SRC) {
1247                         insn += insn->off;
1248                         CONT_JMP;
1249                 }
1250                 CONT;
1251         JMP_JGE_K:
1252                 if (DST >= IMM) {
1253                         insn += insn->off;
1254                         CONT_JMP;
1255                 }
1256                 CONT;
1257         JMP_JLE_X:
1258                 if (DST <= SRC) {
1259                         insn += insn->off;
1260                         CONT_JMP;
1261                 }
1262                 CONT;
1263         JMP_JLE_K:
1264                 if (DST <= IMM) {
1265                         insn += insn->off;
1266                         CONT_JMP;
1267                 }
1268                 CONT;
1269         JMP_JSGT_X:
1270                 if (((s64) DST) > ((s64) SRC)) {
1271                         insn += insn->off;
1272                         CONT_JMP;
1273                 }
1274                 CONT;
1275         JMP_JSGT_K:
1276                 if (((s64) DST) > ((s64) IMM)) {
1277                         insn += insn->off;
1278                         CONT_JMP;
1279                 }
1280                 CONT;
1281         JMP_JSLT_X:
1282                 if (((s64) DST) < ((s64) SRC)) {
1283                         insn += insn->off;
1284                         CONT_JMP;
1285                 }
1286                 CONT;
1287         JMP_JSLT_K:
1288                 if (((s64) DST) < ((s64) IMM)) {
1289                         insn += insn->off;
1290                         CONT_JMP;
1291                 }
1292                 CONT;
1293         JMP_JSGE_X:
1294                 if (((s64) DST) >= ((s64) SRC)) {
1295                         insn += insn->off;
1296                         CONT_JMP;
1297                 }
1298                 CONT;
1299         JMP_JSGE_K:
1300                 if (((s64) DST) >= ((s64) IMM)) {
1301                         insn += insn->off;
1302                         CONT_JMP;
1303                 }
1304                 CONT;
1305         JMP_JSLE_X:
1306                 if (((s64) DST) <= ((s64) SRC)) {
1307                         insn += insn->off;
1308                         CONT_JMP;
1309                 }
1310                 CONT;
1311         JMP_JSLE_K:
1312                 if (((s64) DST) <= ((s64) IMM)) {
1313                         insn += insn->off;
1314                         CONT_JMP;
1315                 }
1316                 CONT;
1317         JMP_JSET_X:
1318                 if (DST & SRC) {
1319                         insn += insn->off;
1320                         CONT_JMP;
1321                 }
1322                 CONT;
1323         JMP_JSET_K:
1324                 if (DST & IMM) {
1325                         insn += insn->off;
1326                         CONT_JMP;
1327                 }
1328                 CONT;
1329         JMP_EXIT:
1330                 return BPF_R0;
1331
1332         /* STX and ST and LDX*/
1333 #define LDST(SIZEOP, SIZE)                                              \
1334         STX_MEM_##SIZEOP:                                               \
1335                 *(SIZE *)(unsigned long) (DST + insn->off) = SRC;       \
1336                 CONT;                                                   \
1337         ST_MEM_##SIZEOP:                                                \
1338                 *(SIZE *)(unsigned long) (DST + insn->off) = IMM;       \
1339                 CONT;                                                   \
1340         LDX_MEM_##SIZEOP:                                               \
1341                 DST = *(SIZE *)(unsigned long) (SRC + insn->off);       \
1342                 CONT;
1343
1344         LDST(B,   u8)
1345         LDST(H,  u16)
1346         LDST(W,  u32)
1347         LDST(DW, u64)
1348 #undef LDST
1349         STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
1350                 atomic_add((u32) SRC, (atomic_t *)(unsigned long)
1351                            (DST + insn->off));
1352                 CONT;
1353         STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
1354                 atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
1355                              (DST + insn->off));
1356                 CONT;
1357
1358         default_label:
1359                 /* If we ever reach this, we have a bug somewhere. Die hard here
1360                  * instead of just returning 0; we could be somewhere in a subprog,
1361                  * so execution could continue otherwise which we do /not/ want.
1362                  *
1363                  * Note, verifier whitelists all opcodes in bpf_opcode_in_insntable().
1364                  */
1365                 pr_warn("BPF interpreter: unknown opcode %02x\n", insn->code);
1366                 BUG_ON(1);
1367                 return 0;
1368 }
1369 STACK_FRAME_NON_STANDARD(___bpf_prog_run); /* jump table */
1370
1371 #define PROG_NAME(stack_size) __bpf_prog_run##stack_size
1372 #define DEFINE_BPF_PROG_RUN(stack_size) \
1373 static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \
1374 { \
1375         u64 stack[stack_size / sizeof(u64)]; \
1376         u64 regs[MAX_BPF_REG]; \
1377 \
1378         FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1379         ARG1 = (u64) (unsigned long) ctx; \
1380         return ___bpf_prog_run(regs, insn, stack); \
1381 }
1382
1383 #define PROG_NAME_ARGS(stack_size) __bpf_prog_run_args##stack_size
1384 #define DEFINE_BPF_PROG_RUN_ARGS(stack_size) \
1385 static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \
1386                                       const struct bpf_insn *insn) \
1387 { \
1388         u64 stack[stack_size / sizeof(u64)]; \
1389         u64 regs[MAX_BPF_REG]; \
1390 \
1391         FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1392         BPF_R1 = r1; \
1393         BPF_R2 = r2; \
1394         BPF_R3 = r3; \
1395         BPF_R4 = r4; \
1396         BPF_R5 = r5; \
1397         return ___bpf_prog_run(regs, insn, stack); \
1398 }
1399
1400 #define EVAL1(FN, X) FN(X)
1401 #define EVAL2(FN, X, Y...) FN(X) EVAL1(FN, Y)
1402 #define EVAL3(FN, X, Y...) FN(X) EVAL2(FN, Y)
1403 #define EVAL4(FN, X, Y...) FN(X) EVAL3(FN, Y)
1404 #define EVAL5(FN, X, Y...) FN(X) EVAL4(FN, Y)
1405 #define EVAL6(FN, X, Y...) FN(X) EVAL5(FN, Y)
1406
1407 EVAL6(DEFINE_BPF_PROG_RUN, 32, 64, 96, 128, 160, 192);
1408 EVAL6(DEFINE_BPF_PROG_RUN, 224, 256, 288, 320, 352, 384);
1409 EVAL4(DEFINE_BPF_PROG_RUN, 416, 448, 480, 512);
1410
1411 EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 32, 64, 96, 128, 160, 192);
1412 EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 224, 256, 288, 320, 352, 384);
1413 EVAL4(DEFINE_BPF_PROG_RUN_ARGS, 416, 448, 480, 512);
1414
1415 #define PROG_NAME_LIST(stack_size) PROG_NAME(stack_size),
1416
1417 static unsigned int (*interpreters[])(const void *ctx,
1418                                       const struct bpf_insn *insn) = {
1419 EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1420 EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1421 EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1422 };
1423 #undef PROG_NAME_LIST
1424 #define PROG_NAME_LIST(stack_size) PROG_NAME_ARGS(stack_size),
1425 static u64 (*interpreters_args[])(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5,
1426                                   const struct bpf_insn *insn) = {
1427 EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1428 EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1429 EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1430 };
1431 #undef PROG_NAME_LIST
1432
1433 void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth)
1434 {
1435         stack_depth = max_t(u32, stack_depth, 1);
1436         insn->off = (s16) insn->imm;
1437         insn->imm = interpreters_args[(round_up(stack_depth, 32) / 32) - 1] -
1438                 __bpf_call_base_args;
1439         insn->code = BPF_JMP | BPF_CALL_ARGS;
1440 }
1441
1442 #else
1443 static unsigned int __bpf_prog_ret0_warn(const void *ctx,
1444                                          const struct bpf_insn *insn)
1445 {
1446         /* If this handler ever gets executed, then BPF_JIT_ALWAYS_ON
1447          * is not working properly, so warn about it!
1448          */
1449         WARN_ON_ONCE(1);
1450         return 0;
1451 }
1452 #endif
1453
1454 bool bpf_prog_array_compatible(struct bpf_array *array,
1455                                const struct bpf_prog *fp)
1456 {
1457         if (fp->kprobe_override)
1458                 return false;
1459
1460         if (!array->owner_prog_type) {
1461                 /* There's no owner yet where we could check for
1462                  * compatibility.
1463                  */
1464                 array->owner_prog_type = fp->type;
1465                 array->owner_jited = fp->jited;
1466
1467                 return true;
1468         }
1469
1470         return array->owner_prog_type == fp->type &&
1471                array->owner_jited == fp->jited;
1472 }
1473
1474 static int bpf_check_tail_call(const struct bpf_prog *fp)
1475 {
1476         struct bpf_prog_aux *aux = fp->aux;
1477         int i;
1478
1479         for (i = 0; i < aux->used_map_cnt; i++) {
1480                 struct bpf_map *map = aux->used_maps[i];
1481                 struct bpf_array *array;
1482
1483                 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1484                         continue;
1485
1486                 array = container_of(map, struct bpf_array, map);
1487                 if (!bpf_prog_array_compatible(array, fp))
1488                         return -EINVAL;
1489         }
1490
1491         return 0;
1492 }
1493
1494 static void bpf_prog_select_func(struct bpf_prog *fp)
1495 {
1496 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
1497         u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1);
1498
1499         fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1];
1500 #else
1501         fp->bpf_func = __bpf_prog_ret0_warn;
1502 #endif
1503 }
1504
1505 /**
1506  *      bpf_prog_select_runtime - select exec runtime for BPF program
1507  *      @fp: bpf_prog populated with internal BPF program
1508  *      @err: pointer to error variable
1509  *
1510  * Try to JIT eBPF program, if JIT is not available, use interpreter.
1511  * The BPF program will be executed via BPF_PROG_RUN() macro.
1512  */
1513 struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
1514 {
1515         /* In case of BPF to BPF calls, verifier did all the prep
1516          * work with regards to JITing, etc.
1517          */
1518         if (fp->bpf_func)
1519                 goto finalize;
1520
1521         bpf_prog_select_func(fp);
1522
1523         /* eBPF JITs can rewrite the program in case constant
1524          * blinding is active. However, in case of error during
1525          * blinding, bpf_int_jit_compile() must always return a
1526          * valid program, which in this case would simply not
1527          * be JITed, but falls back to the interpreter.
1528          */
1529         if (!bpf_prog_is_dev_bound(fp->aux)) {
1530                 fp = bpf_int_jit_compile(fp);
1531 #ifdef CONFIG_BPF_JIT_ALWAYS_ON
1532                 if (!fp->jited) {
1533                         *err = -ENOTSUPP;
1534                         return fp;
1535                 }
1536 #endif
1537         } else {
1538                 *err = bpf_prog_offload_compile(fp);
1539                 if (*err)
1540                         return fp;
1541         }
1542
1543 finalize:
1544         bpf_prog_lock_ro(fp);
1545
1546         /* The tail call compatibility check can only be done at
1547          * this late stage as we need to determine, if we deal
1548          * with JITed or non JITed program concatenations and not
1549          * all eBPF JITs might immediately support all features.
1550          */
1551         *err = bpf_check_tail_call(fp);
1552
1553         return fp;
1554 }
1555 EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
1556
1557 static unsigned int __bpf_prog_ret1(const void *ctx,
1558                                     const struct bpf_insn *insn)
1559 {
1560         return 1;
1561 }
1562
1563 static struct bpf_prog_dummy {
1564         struct bpf_prog prog;
1565 } dummy_bpf_prog = {
1566         .prog = {
1567                 .bpf_func = __bpf_prog_ret1,
1568         },
1569 };
1570
1571 /* to avoid allocating empty bpf_prog_array for cgroups that
1572  * don't have bpf program attached use one global 'empty_prog_array'
1573  * It will not be modified the caller of bpf_prog_array_alloc()
1574  * (since caller requested prog_cnt == 0)
1575  * that pointer should be 'freed' by bpf_prog_array_free()
1576  */
1577 static struct {
1578         struct bpf_prog_array hdr;
1579         struct bpf_prog *null_prog;
1580 } empty_prog_array = {
1581         .null_prog = NULL,
1582 };
1583
1584 struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags)
1585 {
1586         if (prog_cnt)
1587                 return kzalloc(sizeof(struct bpf_prog_array) +
1588                                sizeof(struct bpf_prog_array_item) *
1589                                (prog_cnt + 1),
1590                                flags);
1591
1592         return &empty_prog_array.hdr;
1593 }
1594
1595 void bpf_prog_array_free(struct bpf_prog_array __rcu *progs)
1596 {
1597         if (!progs ||
1598             progs == (struct bpf_prog_array __rcu *)&empty_prog_array.hdr)
1599                 return;
1600         kfree_rcu(progs, rcu);
1601 }
1602
1603 int bpf_prog_array_length(struct bpf_prog_array __rcu *array)
1604 {
1605         struct bpf_prog_array_item *item;
1606         u32 cnt = 0;
1607
1608         rcu_read_lock();
1609         item = rcu_dereference(array)->items;
1610         for (; item->prog; item++)
1611                 if (item->prog != &dummy_bpf_prog.prog)
1612                         cnt++;
1613         rcu_read_unlock();
1614         return cnt;
1615 }
1616
1617
1618 static bool bpf_prog_array_copy_core(struct bpf_prog_array __rcu *array,
1619                                      u32 *prog_ids,
1620                                      u32 request_cnt)
1621 {
1622         struct bpf_prog_array_item *item;
1623         int i = 0;
1624
1625         item = rcu_dereference_check(array, 1)->items;
1626         for (; item->prog; item++) {
1627                 if (item->prog == &dummy_bpf_prog.prog)
1628                         continue;
1629                 prog_ids[i] = item->prog->aux->id;
1630                 if (++i == request_cnt) {
1631                         item++;
1632                         break;
1633                 }
1634         }
1635
1636         return !!(item->prog);
1637 }
1638
1639 int bpf_prog_array_copy_to_user(struct bpf_prog_array __rcu *array,
1640                                 __u32 __user *prog_ids, u32 cnt)
1641 {
1642         unsigned long err = 0;
1643         bool nospc;
1644         u32 *ids;
1645
1646         /* users of this function are doing:
1647          * cnt = bpf_prog_array_length();
1648          * if (cnt > 0)
1649          *     bpf_prog_array_copy_to_user(..., cnt);
1650          * so below kcalloc doesn't need extra cnt > 0 check, but
1651          * bpf_prog_array_length() releases rcu lock and
1652          * prog array could have been swapped with empty or larger array,
1653          * so always copy 'cnt' prog_ids to the user.
1654          * In a rare race the user will see zero prog_ids
1655          */
1656         ids = kcalloc(cnt, sizeof(u32), GFP_USER | __GFP_NOWARN);
1657         if (!ids)
1658                 return -ENOMEM;
1659         rcu_read_lock();
1660         nospc = bpf_prog_array_copy_core(array, ids, cnt);
1661         rcu_read_unlock();
1662         err = copy_to_user(prog_ids, ids, cnt * sizeof(u32));
1663         kfree(ids);
1664         if (err)
1665                 return -EFAULT;
1666         if (nospc)
1667                 return -ENOSPC;
1668         return 0;
1669 }
1670
1671 void bpf_prog_array_delete_safe(struct bpf_prog_array __rcu *array,
1672                                 struct bpf_prog *old_prog)
1673 {
1674         struct bpf_prog_array_item *item = array->items;
1675
1676         for (; item->prog; item++)
1677                 if (item->prog == old_prog) {
1678                         WRITE_ONCE(item->prog, &dummy_bpf_prog.prog);
1679                         break;
1680                 }
1681 }
1682
1683 int bpf_prog_array_copy(struct bpf_prog_array __rcu *old_array,
1684                         struct bpf_prog *exclude_prog,
1685                         struct bpf_prog *include_prog,
1686                         struct bpf_prog_array **new_array)
1687 {
1688         int new_prog_cnt, carry_prog_cnt = 0;
1689         struct bpf_prog_array_item *existing;
1690         struct bpf_prog_array *array;
1691         bool found_exclude = false;
1692         int new_prog_idx = 0;
1693
1694         /* Figure out how many existing progs we need to carry over to
1695          * the new array.
1696          */
1697         if (old_array) {
1698                 existing = old_array->items;
1699                 for (; existing->prog; existing++) {
1700                         if (existing->prog == exclude_prog) {
1701                                 found_exclude = true;
1702                                 continue;
1703                         }
1704                         if (existing->prog != &dummy_bpf_prog.prog)
1705                                 carry_prog_cnt++;
1706                         if (existing->prog == include_prog)
1707                                 return -EEXIST;
1708                 }
1709         }
1710
1711         if (exclude_prog && !found_exclude)
1712                 return -ENOENT;
1713
1714         /* How many progs (not NULL) will be in the new array? */
1715         new_prog_cnt = carry_prog_cnt;
1716         if (include_prog)
1717                 new_prog_cnt += 1;
1718
1719         /* Do we have any prog (not NULL) in the new array? */
1720         if (!new_prog_cnt) {
1721                 *new_array = NULL;
1722                 return 0;
1723         }
1724
1725         /* +1 as the end of prog_array is marked with NULL */
1726         array = bpf_prog_array_alloc(new_prog_cnt + 1, GFP_KERNEL);
1727         if (!array)
1728                 return -ENOMEM;
1729
1730         /* Fill in the new prog array */
1731         if (carry_prog_cnt) {
1732                 existing = old_array->items;
1733                 for (; existing->prog; existing++)
1734                         if (existing->prog != exclude_prog &&
1735                             existing->prog != &dummy_bpf_prog.prog) {
1736                                 array->items[new_prog_idx++].prog =
1737                                         existing->prog;
1738                         }
1739         }
1740         if (include_prog)
1741                 array->items[new_prog_idx++].prog = include_prog;
1742         array->items[new_prog_idx].prog = NULL;
1743         *new_array = array;
1744         return 0;
1745 }
1746
1747 int bpf_prog_array_copy_info(struct bpf_prog_array __rcu *array,
1748                              u32 *prog_ids, u32 request_cnt,
1749                              u32 *prog_cnt)
1750 {
1751         u32 cnt = 0;
1752
1753         if (array)
1754                 cnt = bpf_prog_array_length(array);
1755
1756         *prog_cnt = cnt;
1757
1758         /* return early if user requested only program count or nothing to copy */
1759         if (!request_cnt || !cnt)
1760                 return 0;
1761
1762         /* this function is called under trace/bpf_trace.c: bpf_event_mutex */
1763         return bpf_prog_array_copy_core(array, prog_ids, request_cnt) ? -ENOSPC
1764                                                                      : 0;
1765 }
1766
1767 static void bpf_prog_free_deferred(struct work_struct *work)
1768 {
1769         struct bpf_prog_aux *aux;
1770         int i;
1771
1772         aux = container_of(work, struct bpf_prog_aux, work);
1773         if (bpf_prog_is_dev_bound(aux))
1774                 bpf_prog_offload_destroy(aux->prog);
1775 #ifdef CONFIG_PERF_EVENTS
1776         if (aux->prog->has_callchain_buf)
1777                 put_callchain_buffers();
1778 #endif
1779         for (i = 0; i < aux->func_cnt; i++)
1780                 bpf_jit_free(aux->func[i]);
1781         if (aux->func_cnt) {
1782                 kfree(aux->func);
1783                 bpf_prog_unlock_free(aux->prog);
1784         } else {
1785                 bpf_jit_free(aux->prog);
1786         }
1787 }
1788
1789 /* Free internal BPF program */
1790 void bpf_prog_free(struct bpf_prog *fp)
1791 {
1792         struct bpf_prog_aux *aux = fp->aux;
1793
1794         INIT_WORK(&aux->work, bpf_prog_free_deferred);
1795         schedule_work(&aux->work);
1796 }
1797 EXPORT_SYMBOL_GPL(bpf_prog_free);
1798
1799 /* RNG for unpriviledged user space with separated state from prandom_u32(). */
1800 static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
1801
1802 void bpf_user_rnd_init_once(void)
1803 {
1804         prandom_init_once(&bpf_user_rnd_state);
1805 }
1806
1807 BPF_CALL_0(bpf_user_rnd_u32)
1808 {
1809         /* Should someone ever have the rather unwise idea to use some
1810          * of the registers passed into this function, then note that
1811          * this function is called from native eBPF and classic-to-eBPF
1812          * transformations. Register assignments from both sides are
1813          * different, f.e. classic always sets fn(ctx, A, X) here.
1814          */
1815         struct rnd_state *state;
1816         u32 res;
1817
1818         state = &get_cpu_var(bpf_user_rnd_state);
1819         res = prandom_u32_state(state);
1820         put_cpu_var(bpf_user_rnd_state);
1821
1822         return res;
1823 }
1824
1825 /* Weak definitions of helper functions in case we don't have bpf syscall. */
1826 const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
1827 const struct bpf_func_proto bpf_map_update_elem_proto __weak;
1828 const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
1829 const struct bpf_func_proto bpf_map_push_elem_proto __weak;
1830 const struct bpf_func_proto bpf_map_pop_elem_proto __weak;
1831 const struct bpf_func_proto bpf_map_peek_elem_proto __weak;
1832
1833 const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
1834 const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
1835 const struct bpf_func_proto bpf_get_numa_node_id_proto __weak;
1836 const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
1837
1838 const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
1839 const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
1840 const struct bpf_func_proto bpf_get_current_comm_proto __weak;
1841 const struct bpf_func_proto bpf_get_current_cgroup_id_proto __weak;
1842 const struct bpf_func_proto bpf_get_local_storage_proto __weak;
1843
1844 const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
1845 {
1846         return NULL;
1847 }
1848
1849 u64 __weak
1850 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1851                  void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
1852 {
1853         return -ENOTSUPP;
1854 }
1855 EXPORT_SYMBOL_GPL(bpf_event_output);
1856
1857 /* Always built-in helper functions. */
1858 const struct bpf_func_proto bpf_tail_call_proto = {
1859         .func           = NULL,
1860         .gpl_only       = false,
1861         .ret_type       = RET_VOID,
1862         .arg1_type      = ARG_PTR_TO_CTX,
1863         .arg2_type      = ARG_CONST_MAP_PTR,
1864         .arg3_type      = ARG_ANYTHING,
1865 };
1866
1867 /* Stub for JITs that only support cBPF. eBPF programs are interpreted.
1868  * It is encouraged to implement bpf_int_jit_compile() instead, so that
1869  * eBPF and implicitly also cBPF can get JITed!
1870  */
1871 struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
1872 {
1873         return prog;
1874 }
1875
1876 /* Stub for JITs that support eBPF. All cBPF code gets transformed into
1877  * eBPF by the kernel and is later compiled by bpf_int_jit_compile().
1878  */
1879 void __weak bpf_jit_compile(struct bpf_prog *prog)
1880 {
1881 }
1882
1883 bool __weak bpf_helper_changes_pkt_data(void *func)
1884 {
1885         return false;
1886 }
1887
1888 /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
1889  * skb_copy_bits(), so provide a weak definition of it for NET-less config.
1890  */
1891 int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
1892                          int len)
1893 {
1894         return -EFAULT;
1895 }
1896
1897 /* All definitions of tracepoints related to BPF. */
1898 #define CREATE_TRACE_POINTS
1899 #include <linux/bpf_trace.h>
1900
1901 EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception);