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