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