GNU Linux-libre 4.9.333-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
32 #include <asm/unaligned.h>
33
34 /* Registers */
35 #define BPF_R0  regs[BPF_REG_0]
36 #define BPF_R1  regs[BPF_REG_1]
37 #define BPF_R2  regs[BPF_REG_2]
38 #define BPF_R3  regs[BPF_REG_3]
39 #define BPF_R4  regs[BPF_REG_4]
40 #define BPF_R5  regs[BPF_REG_5]
41 #define BPF_R6  regs[BPF_REG_6]
42 #define BPF_R7  regs[BPF_REG_7]
43 #define BPF_R8  regs[BPF_REG_8]
44 #define BPF_R9  regs[BPF_REG_9]
45 #define BPF_R10 regs[BPF_REG_10]
46
47 /* Named registers */
48 #define DST     regs[insn->dst_reg]
49 #define SRC     regs[insn->src_reg]
50 #define FP      regs[BPF_REG_FP]
51 #define ARG1    regs[BPF_REG_ARG1]
52 #define CTX     regs[BPF_REG_CTX]
53 #define IMM     insn->imm
54
55 /* No hurry in this branch
56  *
57  * Exported for the bpf jit load helper.
58  */
59 void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
60 {
61         u8 *ptr = NULL;
62
63         if (k >= SKF_NET_OFF) {
64                 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
65         } else if (k >= SKF_LL_OFF) {
66                 if (unlikely(!skb_mac_header_was_set(skb)))
67                         return NULL;
68                 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
69         }
70         if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
71                 return ptr;
72
73         return NULL;
74 }
75
76 struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
77 {
78         gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
79                           gfp_extra_flags;
80         struct bpf_prog_aux *aux;
81         struct bpf_prog *fp;
82
83         size = round_up(size, PAGE_SIZE);
84         fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
85         if (fp == NULL)
86                 return NULL;
87
88         kmemcheck_annotate_bitfield(fp, meta);
89
90         aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);
91         if (aux == NULL) {
92                 vfree(fp);
93                 return NULL;
94         }
95
96         fp->pages = size / PAGE_SIZE;
97         fp->aux = aux;
98         fp->aux->prog = fp;
99
100         return fp;
101 }
102 EXPORT_SYMBOL_GPL(bpf_prog_alloc);
103
104 struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
105                                   gfp_t gfp_extra_flags)
106 {
107         gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
108                           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                 kmemcheck_annotate_bitfield(fp, meta);
130
131                 memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
132                 fp->pages = pages;
133                 fp->aux->prog = fp;
134
135                 /* We keep fp->aux from fp_old around in the new
136                  * reallocated structure.
137                  */
138                 fp_old->aux = NULL;
139                 __bpf_prog_free(fp_old);
140         }
141
142         return fp;
143 }
144
145 void __bpf_prog_free(struct bpf_prog *fp)
146 {
147         kfree(fp->aux);
148         vfree(fp);
149 }
150
151 static bool bpf_is_jmp_and_has_target(const struct bpf_insn *insn)
152 {
153         return BPF_CLASS(insn->code) == BPF_JMP  &&
154                /* Call and Exit are both special jumps with no
155                 * target inside the BPF instruction image.
156                 */
157                BPF_OP(insn->code) != BPF_CALL &&
158                BPF_OP(insn->code) != BPF_EXIT;
159 }
160
161 static void bpf_adj_branches(struct bpf_prog *prog, u32 pos, u32 delta)
162 {
163         struct bpf_insn *insn = prog->insnsi;
164         u32 i, insn_cnt = prog->len;
165
166         for (i = 0; i < insn_cnt; i++, insn++) {
167                 if (!bpf_is_jmp_and_has_target(insn))
168                         continue;
169
170                 /* Adjust offset of jmps if we cross boundaries. */
171                 if (i < pos && i + insn->off + 1 > pos)
172                         insn->off += delta;
173                 else if (i > pos + delta && i + insn->off + 1 <= pos + delta)
174                         insn->off -= delta;
175         }
176 }
177
178 struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
179                                        const struct bpf_insn *patch, u32 len)
180 {
181         u32 insn_adj_cnt, insn_rest, insn_delta = len - 1;
182         struct bpf_prog *prog_adj;
183
184         /* Since our patchlet doesn't expand the image, we're done. */
185         if (insn_delta == 0) {
186                 memcpy(prog->insnsi + off, patch, sizeof(*patch));
187                 return prog;
188         }
189
190         insn_adj_cnt = prog->len + insn_delta;
191
192         /* Several new instructions need to be inserted. Make room
193          * for them. Likely, there's no need for a new allocation as
194          * last page could have large enough tailroom.
195          */
196         prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),
197                                     GFP_USER);
198         if (!prog_adj)
199                 return NULL;
200
201         prog_adj->len = insn_adj_cnt;
202
203         /* Patching happens in 3 steps:
204          *
205          * 1) Move over tail of insnsi from next instruction onwards,
206          *    so we can patch the single target insn with one or more
207          *    new ones (patching is always from 1 to n insns, n > 0).
208          * 2) Inject new instructions at the target location.
209          * 3) Adjust branch offsets if necessary.
210          */
211         insn_rest = insn_adj_cnt - off - len;
212
213         memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1,
214                 sizeof(*patch) * insn_rest);
215         memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len);
216
217         bpf_adj_branches(prog_adj, off, insn_delta);
218
219         return prog_adj;
220 }
221
222 #ifdef CONFIG_BPF_JIT
223 /* All BPF JIT sysctl knobs here. */
224 int bpf_jit_enable   __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_ALWAYS_ON);
225 int bpf_jit_harden   __read_mostly;
226 long bpf_jit_limit   __read_mostly;
227 long bpf_jit_limit_max __read_mostly;
228
229 static atomic_long_t bpf_jit_current;
230
231 /* Can be overridden by an arch's JIT compiler if it has a custom,
232  * dedicated BPF backend memory area, or if neither of the two
233  * below apply.
234  */
235 u64 __weak bpf_jit_alloc_exec_limit(void)
236 {
237 #if defined(MODULES_VADDR)
238         return MODULES_END - MODULES_VADDR;
239 #else
240         return VMALLOC_END - VMALLOC_START;
241 #endif
242 }
243
244 static int __init bpf_jit_charge_init(void)
245 {
246         /* Only used as heuristic here to derive limit. */
247         bpf_jit_limit_max = bpf_jit_alloc_exec_limit();
248         bpf_jit_limit = min_t(u64, round_up(bpf_jit_limit_max >> 2,
249                                             PAGE_SIZE), LONG_MAX);
250         return 0;
251 }
252 pure_initcall(bpf_jit_charge_init);
253
254 static int bpf_jit_charge_modmem(u32 pages)
255 {
256         if (atomic_long_add_return(pages, &bpf_jit_current) >
257             (bpf_jit_limit >> PAGE_SHIFT)) {
258                 if (!capable(CAP_SYS_ADMIN)) {
259                         atomic_long_sub(pages, &bpf_jit_current);
260                         return -EPERM;
261                 }
262         }
263
264         return 0;
265 }
266
267 static void bpf_jit_uncharge_modmem(u32 pages)
268 {
269         atomic_long_sub(pages, &bpf_jit_current);
270 }
271
272 struct bpf_binary_header *
273 bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
274                      unsigned int alignment,
275                      bpf_jit_fill_hole_t bpf_fill_ill_insns)
276 {
277         struct bpf_binary_header *hdr;
278         u32 size, hole, start, pages;
279
280         /* Most of BPF filters are really small, but if some of them
281          * fill a page, allow at least 128 extra bytes to insert a
282          * random section of illegal instructions.
283          */
284         size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
285         pages = size / PAGE_SIZE;
286
287         if (bpf_jit_charge_modmem(pages))
288                 return NULL;
289         hdr = module_alloc(size);
290         if (!hdr) {
291                 bpf_jit_uncharge_modmem(pages);
292                 return NULL;
293         }
294
295         /* Fill space with illegal/arch-dep instructions. */
296         bpf_fill_ill_insns(hdr, size);
297
298         hdr->pages = pages;
299         hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
300                      PAGE_SIZE - sizeof(*hdr));
301         start = (get_random_int() % hole) & ~(alignment - 1);
302
303         /* Leave a random number of instructions before BPF code. */
304         *image_ptr = &hdr->image[start];
305
306         return hdr;
307 }
308
309 void bpf_jit_binary_free(struct bpf_binary_header *hdr)
310 {
311         u32 pages = hdr->pages;
312
313         module_memfree(hdr);
314         bpf_jit_uncharge_modmem(pages);
315 }
316
317 static int bpf_jit_blind_insn(const struct bpf_insn *from,
318                               const struct bpf_insn *aux,
319                               struct bpf_insn *to_buff)
320 {
321         struct bpf_insn *to = to_buff;
322         u32 imm_rnd = get_random_int();
323         s16 off;
324
325         BUILD_BUG_ON(BPF_REG_AX  + 1 != MAX_BPF_JIT_REG);
326         BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG);
327
328         if (from->imm == 0 &&
329             (from->code == (BPF_ALU   | BPF_MOV | BPF_K) ||
330              from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) {
331                 *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg);
332                 goto out;
333         }
334
335         switch (from->code) {
336         case BPF_ALU | BPF_ADD | BPF_K:
337         case BPF_ALU | BPF_SUB | BPF_K:
338         case BPF_ALU | BPF_AND | BPF_K:
339         case BPF_ALU | BPF_OR  | BPF_K:
340         case BPF_ALU | BPF_XOR | BPF_K:
341         case BPF_ALU | BPF_MUL | BPF_K:
342         case BPF_ALU | BPF_MOV | BPF_K:
343         case BPF_ALU | BPF_DIV | BPF_K:
344         case BPF_ALU | BPF_MOD | BPF_K:
345                 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
346                 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
347                 *to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX);
348                 break;
349
350         case BPF_ALU64 | BPF_ADD | BPF_K:
351         case BPF_ALU64 | BPF_SUB | BPF_K:
352         case BPF_ALU64 | BPF_AND | BPF_K:
353         case BPF_ALU64 | BPF_OR  | BPF_K:
354         case BPF_ALU64 | BPF_XOR | BPF_K:
355         case BPF_ALU64 | BPF_MUL | BPF_K:
356         case BPF_ALU64 | BPF_MOV | BPF_K:
357         case BPF_ALU64 | BPF_DIV | BPF_K:
358         case BPF_ALU64 | BPF_MOD | BPF_K:
359                 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
360                 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
361                 *to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX);
362                 break;
363
364         case BPF_JMP | BPF_JEQ  | BPF_K:
365         case BPF_JMP | BPF_JNE  | BPF_K:
366         case BPF_JMP | BPF_JGT  | BPF_K:
367         case BPF_JMP | BPF_JGE  | BPF_K:
368         case BPF_JMP | BPF_JSGT | BPF_K:
369         case BPF_JMP | BPF_JSGE | BPF_K:
370         case BPF_JMP | BPF_JSET | BPF_K:
371                 /* Accommodate for extra offset in case of a backjump. */
372                 off = from->off;
373                 if (off < 0)
374                         off -= 2;
375                 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
376                 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
377                 *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off);
378                 break;
379
380         case BPF_LD | BPF_ABS | BPF_W:
381         case BPF_LD | BPF_ABS | BPF_H:
382         case BPF_LD | BPF_ABS | BPF_B:
383                 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
384                 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
385                 *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
386                 break;
387
388         case BPF_LD | BPF_IND | BPF_W:
389         case BPF_LD | BPF_IND | BPF_H:
390         case BPF_LD | BPF_IND | BPF_B:
391                 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
392                 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
393                 *to++ = BPF_ALU32_REG(BPF_ADD, BPF_REG_AX, from->src_reg);
394                 *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
395                 break;
396
397         case BPF_LD | BPF_IMM | BPF_DW:
398                 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm);
399                 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
400                 *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
401                 *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX);
402                 break;
403         case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */
404                 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm);
405                 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
406                 *to++ = BPF_ALU64_REG(BPF_OR,  aux[0].dst_reg, BPF_REG_AX);
407                 break;
408
409         case BPF_ST | BPF_MEM | BPF_DW:
410         case BPF_ST | BPF_MEM | BPF_W:
411         case BPF_ST | BPF_MEM | BPF_H:
412         case BPF_ST | BPF_MEM | BPF_B:
413                 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
414                 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
415                 *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off);
416                 break;
417         }
418 out:
419         return to - to_buff;
420 }
421
422 static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other,
423                                               gfp_t gfp_extra_flags)
424 {
425         gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
426                           gfp_extra_flags;
427         struct bpf_prog *fp;
428
429         fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags, PAGE_KERNEL);
430         if (fp != NULL) {
431                 kmemcheck_annotate_bitfield(fp, meta);
432
433                 /* aux->prog still points to the fp_other one, so
434                  * when promoting the clone to the real program,
435                  * this still needs to be adapted.
436                  */
437                 memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE);
438         }
439
440         return fp;
441 }
442
443 static void bpf_prog_clone_free(struct bpf_prog *fp)
444 {
445         /* aux was stolen by the other clone, so we cannot free
446          * it from this path! It will be freed eventually by the
447          * other program on release.
448          *
449          * At this point, we don't need a deferred release since
450          * clone is guaranteed to not be locked.
451          */
452         fp->aux = NULL;
453         __bpf_prog_free(fp);
454 }
455
456 void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other)
457 {
458         /* We have to repoint aux->prog to self, as we don't
459          * know whether fp here is the clone or the original.
460          */
461         fp->aux->prog = fp;
462         bpf_prog_clone_free(fp_other);
463 }
464
465 struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
466 {
467         struct bpf_insn insn_buff[16], aux[2];
468         struct bpf_prog *clone, *tmp;
469         int insn_delta, insn_cnt;
470         struct bpf_insn *insn;
471         int i, rewritten;
472
473         if (!bpf_jit_blinding_enabled())
474                 return prog;
475
476         clone = bpf_prog_clone_create(prog, GFP_USER);
477         if (!clone)
478                 return ERR_PTR(-ENOMEM);
479
480         insn_cnt = clone->len;
481         insn = clone->insnsi;
482
483         for (i = 0; i < insn_cnt; i++, insn++) {
484                 /* We temporarily need to hold the original ld64 insn
485                  * so that we can still access the first part in the
486                  * second blinding run.
487                  */
488                 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) &&
489                     insn[1].code == 0)
490                         memcpy(aux, insn, sizeof(aux));
491
492                 rewritten = bpf_jit_blind_insn(insn, aux, insn_buff);
493                 if (!rewritten)
494                         continue;
495
496                 tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);
497                 if (!tmp) {
498                         /* Patching may have repointed aux->prog during
499                          * realloc from the original one, so we need to
500                          * fix it up here on error.
501                          */
502                         bpf_jit_prog_release_other(prog, clone);
503                         return ERR_PTR(-ENOMEM);
504                 }
505
506                 clone = tmp;
507                 insn_delta = rewritten - 1;
508
509                 /* Walk new program and skip insns we just inserted. */
510                 insn = clone->insnsi + i + insn_delta;
511                 insn_cnt += insn_delta;
512                 i        += insn_delta;
513         }
514
515         return clone;
516 }
517 #endif /* CONFIG_BPF_JIT */
518
519 /* Base function for offset calculation. Needs to go into .text section,
520  * therefore keeping it non-static as well; will also be used by JITs
521  * anyway later on, so do not let the compiler omit it.
522  */
523 noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
524 {
525         return 0;
526 }
527 EXPORT_SYMBOL_GPL(__bpf_call_base);
528
529 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
530 /**
531  *      __bpf_prog_run - run eBPF program on a given context
532  *      @ctx: is the data we are operating on
533  *      @insn: is the array of eBPF instructions
534  *
535  * Decode and execute eBPF instructions.
536  */
537 static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
538 {
539         u64 stack[MAX_BPF_STACK / sizeof(u64)];
540         u64 regs[MAX_BPF_REG], tmp;
541         static const void *jumptable[256] = {
542                 [0 ... 255] = &&default_label,
543                 /* Now overwrite non-defaults ... */
544                 /* 32 bit ALU operations */
545                 [BPF_ALU | BPF_ADD | BPF_X] = &&ALU_ADD_X,
546                 [BPF_ALU | BPF_ADD | BPF_K] = &&ALU_ADD_K,
547                 [BPF_ALU | BPF_SUB | BPF_X] = &&ALU_SUB_X,
548                 [BPF_ALU | BPF_SUB | BPF_K] = &&ALU_SUB_K,
549                 [BPF_ALU | BPF_AND | BPF_X] = &&ALU_AND_X,
550                 [BPF_ALU | BPF_AND | BPF_K] = &&ALU_AND_K,
551                 [BPF_ALU | BPF_OR | BPF_X]  = &&ALU_OR_X,
552                 [BPF_ALU | BPF_OR | BPF_K]  = &&ALU_OR_K,
553                 [BPF_ALU | BPF_LSH | BPF_X] = &&ALU_LSH_X,
554                 [BPF_ALU | BPF_LSH | BPF_K] = &&ALU_LSH_K,
555                 [BPF_ALU | BPF_RSH | BPF_X] = &&ALU_RSH_X,
556                 [BPF_ALU | BPF_RSH | BPF_K] = &&ALU_RSH_K,
557                 [BPF_ALU | BPF_XOR | BPF_X] = &&ALU_XOR_X,
558                 [BPF_ALU | BPF_XOR | BPF_K] = &&ALU_XOR_K,
559                 [BPF_ALU | BPF_MUL | BPF_X] = &&ALU_MUL_X,
560                 [BPF_ALU | BPF_MUL | BPF_K] = &&ALU_MUL_K,
561                 [BPF_ALU | BPF_MOV | BPF_X] = &&ALU_MOV_X,
562                 [BPF_ALU | BPF_MOV | BPF_K] = &&ALU_MOV_K,
563                 [BPF_ALU | BPF_DIV | BPF_X] = &&ALU_DIV_X,
564                 [BPF_ALU | BPF_DIV | BPF_K] = &&ALU_DIV_K,
565                 [BPF_ALU | BPF_MOD | BPF_X] = &&ALU_MOD_X,
566                 [BPF_ALU | BPF_MOD | BPF_K] = &&ALU_MOD_K,
567                 [BPF_ALU | BPF_NEG] = &&ALU_NEG,
568                 [BPF_ALU | BPF_END | BPF_TO_BE] = &&ALU_END_TO_BE,
569                 [BPF_ALU | BPF_END | BPF_TO_LE] = &&ALU_END_TO_LE,
570                 /* 64 bit ALU operations */
571                 [BPF_ALU64 | BPF_ADD | BPF_X] = &&ALU64_ADD_X,
572                 [BPF_ALU64 | BPF_ADD | BPF_K] = &&ALU64_ADD_K,
573                 [BPF_ALU64 | BPF_SUB | BPF_X] = &&ALU64_SUB_X,
574                 [BPF_ALU64 | BPF_SUB | BPF_K] = &&ALU64_SUB_K,
575                 [BPF_ALU64 | BPF_AND | BPF_X] = &&ALU64_AND_X,
576                 [BPF_ALU64 | BPF_AND | BPF_K] = &&ALU64_AND_K,
577                 [BPF_ALU64 | BPF_OR | BPF_X] = &&ALU64_OR_X,
578                 [BPF_ALU64 | BPF_OR | BPF_K] = &&ALU64_OR_K,
579                 [BPF_ALU64 | BPF_LSH | BPF_X] = &&ALU64_LSH_X,
580                 [BPF_ALU64 | BPF_LSH | BPF_K] = &&ALU64_LSH_K,
581                 [BPF_ALU64 | BPF_RSH | BPF_X] = &&ALU64_RSH_X,
582                 [BPF_ALU64 | BPF_RSH | BPF_K] = &&ALU64_RSH_K,
583                 [BPF_ALU64 | BPF_XOR | BPF_X] = &&ALU64_XOR_X,
584                 [BPF_ALU64 | BPF_XOR | BPF_K] = &&ALU64_XOR_K,
585                 [BPF_ALU64 | BPF_MUL | BPF_X] = &&ALU64_MUL_X,
586                 [BPF_ALU64 | BPF_MUL | BPF_K] = &&ALU64_MUL_K,
587                 [BPF_ALU64 | BPF_MOV | BPF_X] = &&ALU64_MOV_X,
588                 [BPF_ALU64 | BPF_MOV | BPF_K] = &&ALU64_MOV_K,
589                 [BPF_ALU64 | BPF_ARSH | BPF_X] = &&ALU64_ARSH_X,
590                 [BPF_ALU64 | BPF_ARSH | BPF_K] = &&ALU64_ARSH_K,
591                 [BPF_ALU64 | BPF_DIV | BPF_X] = &&ALU64_DIV_X,
592                 [BPF_ALU64 | BPF_DIV | BPF_K] = &&ALU64_DIV_K,
593                 [BPF_ALU64 | BPF_MOD | BPF_X] = &&ALU64_MOD_X,
594                 [BPF_ALU64 | BPF_MOD | BPF_K] = &&ALU64_MOD_K,
595                 [BPF_ALU64 | BPF_NEG] = &&ALU64_NEG,
596                 /* Call instruction */
597                 [BPF_JMP | BPF_CALL] = &&JMP_CALL,
598                 [BPF_JMP | BPF_CALL | BPF_X] = &&JMP_TAIL_CALL,
599                 /* Jumps */
600                 [BPF_JMP | BPF_JA] = &&JMP_JA,
601                 [BPF_JMP | BPF_JEQ | BPF_X] = &&JMP_JEQ_X,
602                 [BPF_JMP | BPF_JEQ | BPF_K] = &&JMP_JEQ_K,
603                 [BPF_JMP | BPF_JNE | BPF_X] = &&JMP_JNE_X,
604                 [BPF_JMP | BPF_JNE | BPF_K] = &&JMP_JNE_K,
605                 [BPF_JMP | BPF_JGT | BPF_X] = &&JMP_JGT_X,
606                 [BPF_JMP | BPF_JGT | BPF_K] = &&JMP_JGT_K,
607                 [BPF_JMP | BPF_JGE | BPF_X] = &&JMP_JGE_X,
608                 [BPF_JMP | BPF_JGE | BPF_K] = &&JMP_JGE_K,
609                 [BPF_JMP | BPF_JSGT | BPF_X] = &&JMP_JSGT_X,
610                 [BPF_JMP | BPF_JSGT | BPF_K] = &&JMP_JSGT_K,
611                 [BPF_JMP | BPF_JSGE | BPF_X] = &&JMP_JSGE_X,
612                 [BPF_JMP | BPF_JSGE | BPF_K] = &&JMP_JSGE_K,
613                 [BPF_JMP | BPF_JSET | BPF_X] = &&JMP_JSET_X,
614                 [BPF_JMP | BPF_JSET | BPF_K] = &&JMP_JSET_K,
615                 /* Program return */
616                 [BPF_JMP | BPF_EXIT] = &&JMP_EXIT,
617                 /* Store instructions */
618                 [BPF_STX | BPF_MEM | BPF_B] = &&STX_MEM_B,
619                 [BPF_STX | BPF_MEM | BPF_H] = &&STX_MEM_H,
620                 [BPF_STX | BPF_MEM | BPF_W] = &&STX_MEM_W,
621                 [BPF_STX | BPF_MEM | BPF_DW] = &&STX_MEM_DW,
622                 [BPF_STX | BPF_XADD | BPF_W] = &&STX_XADD_W,
623                 [BPF_STX | BPF_XADD | BPF_DW] = &&STX_XADD_DW,
624                 [BPF_ST | BPF_MEM | BPF_B] = &&ST_MEM_B,
625                 [BPF_ST | BPF_MEM | BPF_H] = &&ST_MEM_H,
626                 [BPF_ST | BPF_MEM | BPF_W] = &&ST_MEM_W,
627                 [BPF_ST | BPF_MEM | BPF_DW] = &&ST_MEM_DW,
628                 /* Load instructions */
629                 [BPF_LDX | BPF_MEM | BPF_B] = &&LDX_MEM_B,
630                 [BPF_LDX | BPF_MEM | BPF_H] = &&LDX_MEM_H,
631                 [BPF_LDX | BPF_MEM | BPF_W] = &&LDX_MEM_W,
632                 [BPF_LDX | BPF_MEM | BPF_DW] = &&LDX_MEM_DW,
633                 [BPF_LD | BPF_ABS | BPF_W] = &&LD_ABS_W,
634                 [BPF_LD | BPF_ABS | BPF_H] = &&LD_ABS_H,
635                 [BPF_LD | BPF_ABS | BPF_B] = &&LD_ABS_B,
636                 [BPF_LD | BPF_IND | BPF_W] = &&LD_IND_W,
637                 [BPF_LD | BPF_IND | BPF_H] = &&LD_IND_H,
638                 [BPF_LD | BPF_IND | BPF_B] = &&LD_IND_B,
639                 [BPF_LD | BPF_IMM | BPF_DW] = &&LD_IMM_DW,
640         };
641         u32 tail_call_cnt = 0;
642         void *ptr;
643         int off;
644
645 #define CONT     ({ insn++; goto select_insn; })
646 #define CONT_JMP ({ insn++; goto select_insn; })
647
648         FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)];
649         ARG1 = (u64) (unsigned long) ctx;
650
651 select_insn:
652         goto *jumptable[insn->code];
653
654         /* ALU */
655 #define ALU(OPCODE, OP)                 \
656         ALU64_##OPCODE##_X:             \
657                 DST = DST OP SRC;       \
658                 CONT;                   \
659         ALU_##OPCODE##_X:               \
660                 DST = (u32) DST OP (u32) SRC;   \
661                 CONT;                   \
662         ALU64_##OPCODE##_K:             \
663                 DST = DST OP IMM;               \
664                 CONT;                   \
665         ALU_##OPCODE##_K:               \
666                 DST = (u32) DST OP (u32) IMM;   \
667                 CONT;
668
669         ALU(ADD,  +)
670         ALU(SUB,  -)
671         ALU(AND,  &)
672         ALU(OR,   |)
673         ALU(LSH, <<)
674         ALU(RSH, >>)
675         ALU(XOR,  ^)
676         ALU(MUL,  *)
677 #undef ALU
678         ALU_NEG:
679                 DST = (u32) -DST;
680                 CONT;
681         ALU64_NEG:
682                 DST = -DST;
683                 CONT;
684         ALU_MOV_X:
685                 DST = (u32) SRC;
686                 CONT;
687         ALU_MOV_K:
688                 DST = (u32) IMM;
689                 CONT;
690         ALU64_MOV_X:
691                 DST = SRC;
692                 CONT;
693         ALU64_MOV_K:
694                 DST = IMM;
695                 CONT;
696         LD_IMM_DW:
697                 DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
698                 insn++;
699                 CONT;
700         ALU64_ARSH_X:
701                 (*(s64 *) &DST) >>= SRC;
702                 CONT;
703         ALU64_ARSH_K:
704                 (*(s64 *) &DST) >>= IMM;
705                 CONT;
706         ALU64_MOD_X:
707                 if (unlikely(SRC == 0))
708                         return 0;
709                 div64_u64_rem(DST, SRC, &tmp);
710                 DST = tmp;
711                 CONT;
712         ALU_MOD_X:
713                 if (unlikely((u32)SRC == 0))
714                         return 0;
715                 tmp = (u32) DST;
716                 DST = do_div(tmp, (u32) SRC);
717                 CONT;
718         ALU64_MOD_K:
719                 div64_u64_rem(DST, IMM, &tmp);
720                 DST = tmp;
721                 CONT;
722         ALU_MOD_K:
723                 tmp = (u32) DST;
724                 DST = do_div(tmp, (u32) IMM);
725                 CONT;
726         ALU64_DIV_X:
727                 if (unlikely(SRC == 0))
728                         return 0;
729                 DST = div64_u64(DST, SRC);
730                 CONT;
731         ALU_DIV_X:
732                 if (unlikely((u32)SRC == 0))
733                         return 0;
734                 tmp = (u32) DST;
735                 do_div(tmp, (u32) SRC);
736                 DST = (u32) tmp;
737                 CONT;
738         ALU64_DIV_K:
739                 DST = div64_u64(DST, IMM);
740                 CONT;
741         ALU_DIV_K:
742                 tmp = (u32) DST;
743                 do_div(tmp, (u32) IMM);
744                 DST = (u32) tmp;
745                 CONT;
746         ALU_END_TO_BE:
747                 switch (IMM) {
748                 case 16:
749                         DST = (__force u16) cpu_to_be16(DST);
750                         break;
751                 case 32:
752                         DST = (__force u32) cpu_to_be32(DST);
753                         break;
754                 case 64:
755                         DST = (__force u64) cpu_to_be64(DST);
756                         break;
757                 }
758                 CONT;
759         ALU_END_TO_LE:
760                 switch (IMM) {
761                 case 16:
762                         DST = (__force u16) cpu_to_le16(DST);
763                         break;
764                 case 32:
765                         DST = (__force u32) cpu_to_le32(DST);
766                         break;
767                 case 64:
768                         DST = (__force u64) cpu_to_le64(DST);
769                         break;
770                 }
771                 CONT;
772
773         /* CALL */
774         JMP_CALL:
775                 /* Function call scratches BPF_R1-BPF_R5 registers,
776                  * preserves BPF_R6-BPF_R9, and stores return value
777                  * into BPF_R0.
778                  */
779                 BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
780                                                        BPF_R4, BPF_R5);
781                 CONT;
782
783         JMP_TAIL_CALL: {
784                 struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
785                 struct bpf_array *array = container_of(map, struct bpf_array, map);
786                 struct bpf_prog *prog;
787                 u32 index = BPF_R3;
788
789                 if (unlikely(index >= array->map.max_entries))
790                         goto out;
791                 if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
792                         goto out;
793
794                 tail_call_cnt++;
795
796                 prog = READ_ONCE(array->ptrs[index]);
797                 if (!prog)
798                         goto out;
799
800                 /* ARG1 at this point is guaranteed to point to CTX from
801                  * the verifier side due to the fact that the tail call is
802                  * handeled like a helper, that is, bpf_tail_call_proto,
803                  * where arg1_type is ARG_PTR_TO_CTX.
804                  */
805                 insn = prog->insnsi;
806                 goto select_insn;
807 out:
808                 CONT;
809         }
810         /* JMP */
811         JMP_JA:
812                 insn += insn->off;
813                 CONT;
814         JMP_JEQ_X:
815                 if (DST == SRC) {
816                         insn += insn->off;
817                         CONT_JMP;
818                 }
819                 CONT;
820         JMP_JEQ_K:
821                 if (DST == IMM) {
822                         insn += insn->off;
823                         CONT_JMP;
824                 }
825                 CONT;
826         JMP_JNE_X:
827                 if (DST != SRC) {
828                         insn += insn->off;
829                         CONT_JMP;
830                 }
831                 CONT;
832         JMP_JNE_K:
833                 if (DST != IMM) {
834                         insn += insn->off;
835                         CONT_JMP;
836                 }
837                 CONT;
838         JMP_JGT_X:
839                 if (DST > SRC) {
840                         insn += insn->off;
841                         CONT_JMP;
842                 }
843                 CONT;
844         JMP_JGT_K:
845                 if (DST > IMM) {
846                         insn += insn->off;
847                         CONT_JMP;
848                 }
849                 CONT;
850         JMP_JGE_X:
851                 if (DST >= SRC) {
852                         insn += insn->off;
853                         CONT_JMP;
854                 }
855                 CONT;
856         JMP_JGE_K:
857                 if (DST >= IMM) {
858                         insn += insn->off;
859                         CONT_JMP;
860                 }
861                 CONT;
862         JMP_JSGT_X:
863                 if (((s64) DST) > ((s64) SRC)) {
864                         insn += insn->off;
865                         CONT_JMP;
866                 }
867                 CONT;
868         JMP_JSGT_K:
869                 if (((s64) DST) > ((s64) IMM)) {
870                         insn += insn->off;
871                         CONT_JMP;
872                 }
873                 CONT;
874         JMP_JSGE_X:
875                 if (((s64) DST) >= ((s64) SRC)) {
876                         insn += insn->off;
877                         CONT_JMP;
878                 }
879                 CONT;
880         JMP_JSGE_K:
881                 if (((s64) DST) >= ((s64) IMM)) {
882                         insn += insn->off;
883                         CONT_JMP;
884                 }
885                 CONT;
886         JMP_JSET_X:
887                 if (DST & SRC) {
888                         insn += insn->off;
889                         CONT_JMP;
890                 }
891                 CONT;
892         JMP_JSET_K:
893                 if (DST & IMM) {
894                         insn += insn->off;
895                         CONT_JMP;
896                 }
897                 CONT;
898         JMP_EXIT:
899                 return BPF_R0;
900
901         /* STX and ST and LDX*/
902 #define LDST(SIZEOP, SIZE)                                              \
903         STX_MEM_##SIZEOP:                                               \
904                 *(SIZE *)(unsigned long) (DST + insn->off) = SRC;       \
905                 CONT;                                                   \
906         ST_MEM_##SIZEOP:                                                \
907                 *(SIZE *)(unsigned long) (DST + insn->off) = IMM;       \
908                 CONT;                                                   \
909         LDX_MEM_##SIZEOP:                                               \
910                 DST = *(SIZE *)(unsigned long) (SRC + insn->off);       \
911                 CONT;
912
913         LDST(B,   u8)
914         LDST(H,  u16)
915         LDST(W,  u32)
916         LDST(DW, u64)
917 #undef LDST
918         STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
919                 atomic_add((u32) SRC, (atomic_t *)(unsigned long)
920                            (DST + insn->off));
921                 CONT;
922         STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
923                 atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
924                              (DST + insn->off));
925                 CONT;
926         LD_ABS_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + imm32)) */
927                 off = IMM;
928 load_word:
929                 /* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are
930                  * only appearing in the programs where ctx ==
931                  * skb. All programs keep 'ctx' in regs[BPF_REG_CTX]
932                  * == BPF_R6, bpf_convert_filter() saves it in BPF_R6,
933                  * internal BPF verifier will check that BPF_R6 ==
934                  * ctx.
935                  *
936                  * BPF_ABS and BPF_IND are wrappers of function calls,
937                  * so they scratch BPF_R1-BPF_R5 registers, preserve
938                  * BPF_R6-BPF_R9, and store return value into BPF_R0.
939                  *
940                  * Implicit input:
941                  *   ctx == skb == BPF_R6 == CTX
942                  *
943                  * Explicit input:
944                  *   SRC == any register
945                  *   IMM == 32-bit immediate
946                  *
947                  * Output:
948                  *   BPF_R0 - 8/16/32-bit skb data converted to cpu endianness
949                  */
950
951                 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 4, &tmp);
952                 if (likely(ptr != NULL)) {
953                         BPF_R0 = get_unaligned_be32(ptr);
954                         CONT;
955                 }
956
957                 return 0;
958         LD_ABS_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + imm32)) */
959                 off = IMM;
960 load_half:
961                 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 2, &tmp);
962                 if (likely(ptr != NULL)) {
963                         BPF_R0 = get_unaligned_be16(ptr);
964                         CONT;
965                 }
966
967                 return 0;
968         LD_ABS_B: /* BPF_R0 = *(u8 *) (skb->data + imm32) */
969                 off = IMM;
970 load_byte:
971                 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 1, &tmp);
972                 if (likely(ptr != NULL)) {
973                         BPF_R0 = *(u8 *)ptr;
974                         CONT;
975                 }
976
977                 return 0;
978         LD_IND_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + src_reg + imm32)) */
979                 off = IMM + SRC;
980                 goto load_word;
981         LD_IND_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + src_reg + imm32)) */
982                 off = IMM + SRC;
983                 goto load_half;
984         LD_IND_B: /* BPF_R0 = *(u8 *) (skb->data + src_reg + imm32) */
985                 off = IMM + SRC;
986                 goto load_byte;
987
988         default_label:
989                 /* If we ever reach this, we have a bug somewhere. */
990                 WARN_RATELIMIT(1, "unknown opcode %02x\n", insn->code);
991                 return 0;
992 }
993 STACK_FRAME_NON_STANDARD(__bpf_prog_run); /* jump table */
994
995 #else
996 static unsigned int __bpf_prog_ret0_warn(void *ctx,
997                                          const struct bpf_insn *insn)
998 {
999         /* If this handler ever gets executed, then BPF_JIT_ALWAYS_ON
1000          * is not working properly, so warn about it!
1001          */
1002         WARN_ON_ONCE(1);
1003         return 0;
1004 }
1005 #endif
1006
1007 bool bpf_prog_array_compatible(struct bpf_array *array,
1008                                const struct bpf_prog *fp)
1009 {
1010         if (!array->owner_prog_type) {
1011                 /* There's no owner yet where we could check for
1012                  * compatibility.
1013                  */
1014                 array->owner_prog_type = fp->type;
1015                 array->owner_jited = fp->jited;
1016
1017                 return true;
1018         }
1019
1020         return array->owner_prog_type == fp->type &&
1021                array->owner_jited == fp->jited;
1022 }
1023
1024 static int bpf_check_tail_call(const struct bpf_prog *fp)
1025 {
1026         struct bpf_prog_aux *aux = fp->aux;
1027         int i;
1028
1029         for (i = 0; i < aux->used_map_cnt; i++) {
1030                 struct bpf_map *map = aux->used_maps[i];
1031                 struct bpf_array *array;
1032
1033                 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1034                         continue;
1035
1036                 array = container_of(map, struct bpf_array, map);
1037                 if (!bpf_prog_array_compatible(array, fp))
1038                         return -EINVAL;
1039         }
1040
1041         return 0;
1042 }
1043
1044 /**
1045  *      bpf_prog_select_runtime - select exec runtime for BPF program
1046  *      @fp: bpf_prog populated with internal BPF program
1047  *      @err: pointer to error variable
1048  *
1049  * Try to JIT eBPF program, if JIT is not available, use interpreter.
1050  * The BPF program will be executed via BPF_PROG_RUN() macro.
1051  */
1052 struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
1053 {
1054 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
1055         fp->bpf_func = (void *) __bpf_prog_run;
1056 #else
1057         fp->bpf_func = (void *) __bpf_prog_ret0_warn;
1058 #endif
1059
1060         /* eBPF JITs can rewrite the program in case constant
1061          * blinding is active. However, in case of error during
1062          * blinding, bpf_int_jit_compile() must always return a
1063          * valid program, which in this case would simply not
1064          * be JITed, but falls back to the interpreter.
1065          */
1066         fp = bpf_int_jit_compile(fp);
1067 #ifdef CONFIG_BPF_JIT_ALWAYS_ON
1068         if (!fp->jited) {
1069                 *err = -ENOTSUPP;
1070                 return fp;
1071         }
1072 #endif
1073         bpf_prog_lock_ro(fp);
1074
1075         /* The tail call compatibility check can only be done at
1076          * this late stage as we need to determine, if we deal
1077          * with JITed or non JITed program concatenations and not
1078          * all eBPF JITs might immediately support all features.
1079          */
1080         *err = bpf_check_tail_call(fp);
1081
1082         return fp;
1083 }
1084 EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
1085
1086 static void bpf_prog_free_deferred(struct work_struct *work)
1087 {
1088         struct bpf_prog_aux *aux;
1089
1090         aux = container_of(work, struct bpf_prog_aux, work);
1091         bpf_jit_free(aux->prog);
1092 }
1093
1094 /* Free internal BPF program */
1095 void bpf_prog_free(struct bpf_prog *fp)
1096 {
1097         struct bpf_prog_aux *aux = fp->aux;
1098
1099         INIT_WORK(&aux->work, bpf_prog_free_deferred);
1100         schedule_work(&aux->work);
1101 }
1102 EXPORT_SYMBOL_GPL(bpf_prog_free);
1103
1104 /* RNG for unpriviledged user space with separated state from prandom_u32(). */
1105 static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
1106
1107 void bpf_user_rnd_init_once(void)
1108 {
1109         prandom_init_once(&bpf_user_rnd_state);
1110 }
1111
1112 BPF_CALL_0(bpf_user_rnd_u32)
1113 {
1114         /* Should someone ever have the rather unwise idea to use some
1115          * of the registers passed into this function, then note that
1116          * this function is called from native eBPF and classic-to-eBPF
1117          * transformations. Register assignments from both sides are
1118          * different, f.e. classic always sets fn(ctx, A, X) here.
1119          */
1120         struct rnd_state *state;
1121         u32 res;
1122
1123         state = &get_cpu_var(bpf_user_rnd_state);
1124         res = prandom_u32_state(state);
1125         put_cpu_var(bpf_user_rnd_state);
1126
1127         return res;
1128 }
1129
1130 /* Weak definitions of helper functions in case we don't have bpf syscall. */
1131 const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
1132 const struct bpf_func_proto bpf_map_update_elem_proto __weak;
1133 const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
1134
1135 const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
1136 const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
1137 const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
1138
1139 const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
1140 const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
1141 const struct bpf_func_proto bpf_get_current_comm_proto __weak;
1142
1143 const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
1144 {
1145         return NULL;
1146 }
1147
1148 u64 __weak
1149 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1150                  void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
1151 {
1152         return -ENOTSUPP;
1153 }
1154
1155 /* Always built-in helper functions. */
1156 const struct bpf_func_proto bpf_tail_call_proto = {
1157         .func           = NULL,
1158         .gpl_only       = false,
1159         .ret_type       = RET_VOID,
1160         .arg1_type      = ARG_PTR_TO_CTX,
1161         .arg2_type      = ARG_CONST_MAP_PTR,
1162         .arg3_type      = ARG_ANYTHING,
1163 };
1164
1165 /* For classic BPF JITs that don't implement bpf_int_jit_compile(). */
1166 struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
1167 {
1168         return prog;
1169 }
1170
1171 bool __weak bpf_helper_changes_skb_data(void *func)
1172 {
1173         return false;
1174 }
1175
1176 /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
1177  * skb_copy_bits(), so provide a weak definition of it for NET-less config.
1178  */
1179 int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
1180                          int len)
1181 {
1182         return -EFAULT;
1183 }