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