ebpf: remove self-assignment in interpreter's tail call
[linux-block.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
31 #include <asm/unaligned.h>
32
33 /* Registers */
34 #define BPF_R0  regs[BPF_REG_0]
35 #define BPF_R1  regs[BPF_REG_1]
36 #define BPF_R2  regs[BPF_REG_2]
37 #define BPF_R3  regs[BPF_REG_3]
38 #define BPF_R4  regs[BPF_REG_4]
39 #define BPF_R5  regs[BPF_REG_5]
40 #define BPF_R6  regs[BPF_REG_6]
41 #define BPF_R7  regs[BPF_REG_7]
42 #define BPF_R8  regs[BPF_REG_8]
43 #define BPF_R9  regs[BPF_REG_9]
44 #define BPF_R10 regs[BPF_REG_10]
45
46 /* Named registers */
47 #define DST     regs[insn->dst_reg]
48 #define SRC     regs[insn->src_reg]
49 #define FP      regs[BPF_REG_FP]
50 #define ARG1    regs[BPF_REG_ARG1]
51 #define CTX     regs[BPF_REG_CTX]
52 #define IMM     insn->imm
53
54 /* No hurry in this branch
55  *
56  * Exported for the bpf jit load helper.
57  */
58 void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
59 {
60         u8 *ptr = NULL;
61
62         if (k >= SKF_NET_OFF)
63                 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
64         else if (k >= SKF_LL_OFF)
65                 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
66
67         if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
68                 return ptr;
69
70         return NULL;
71 }
72
73 struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
74 {
75         gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
76                           gfp_extra_flags;
77         struct bpf_prog_aux *aux;
78         struct bpf_prog *fp;
79
80         size = round_up(size, PAGE_SIZE);
81         fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
82         if (fp == NULL)
83                 return NULL;
84
85         aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);
86         if (aux == NULL) {
87                 vfree(fp);
88                 return NULL;
89         }
90
91         fp->pages = size / PAGE_SIZE;
92         fp->aux = aux;
93
94         return fp;
95 }
96 EXPORT_SYMBOL_GPL(bpf_prog_alloc);
97
98 struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
99                                   gfp_t gfp_extra_flags)
100 {
101         gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
102                           gfp_extra_flags;
103         struct bpf_prog *fp;
104
105         BUG_ON(fp_old == NULL);
106
107         size = round_up(size, PAGE_SIZE);
108         if (size <= fp_old->pages * PAGE_SIZE)
109                 return fp_old;
110
111         fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
112         if (fp != NULL) {
113                 memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
114                 fp->pages = size / PAGE_SIZE;
115
116                 /* We keep fp->aux from fp_old around in the new
117                  * reallocated structure.
118                  */
119                 fp_old->aux = NULL;
120                 __bpf_prog_free(fp_old);
121         }
122
123         return fp;
124 }
125 EXPORT_SYMBOL_GPL(bpf_prog_realloc);
126
127 void __bpf_prog_free(struct bpf_prog *fp)
128 {
129         kfree(fp->aux);
130         vfree(fp);
131 }
132 EXPORT_SYMBOL_GPL(__bpf_prog_free);
133
134 #ifdef CONFIG_BPF_JIT
135 struct bpf_binary_header *
136 bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
137                      unsigned int alignment,
138                      bpf_jit_fill_hole_t bpf_fill_ill_insns)
139 {
140         struct bpf_binary_header *hdr;
141         unsigned int size, hole, start;
142
143         /* Most of BPF filters are really small, but if some of them
144          * fill a page, allow at least 128 extra bytes to insert a
145          * random section of illegal instructions.
146          */
147         size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
148         hdr = module_alloc(size);
149         if (hdr == NULL)
150                 return NULL;
151
152         /* Fill space with illegal/arch-dep instructions. */
153         bpf_fill_ill_insns(hdr, size);
154
155         hdr->pages = size / PAGE_SIZE;
156         hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
157                      PAGE_SIZE - sizeof(*hdr));
158         start = (prandom_u32() % hole) & ~(alignment - 1);
159
160         /* Leave a random number of instructions before BPF code. */
161         *image_ptr = &hdr->image[start];
162
163         return hdr;
164 }
165
166 void bpf_jit_binary_free(struct bpf_binary_header *hdr)
167 {
168         module_memfree(hdr);
169 }
170 #endif /* CONFIG_BPF_JIT */
171
172 /* Base function for offset calculation. Needs to go into .text section,
173  * therefore keeping it non-static as well; will also be used by JITs
174  * anyway later on, so do not let the compiler omit it.
175  */
176 noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
177 {
178         return 0;
179 }
180
181 /**
182  *      __bpf_prog_run - run eBPF program on a given context
183  *      @ctx: is the data we are operating on
184  *      @insn: is the array of eBPF instructions
185  *
186  * Decode and execute eBPF instructions.
187  */
188 static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
189 {
190         u64 stack[MAX_BPF_STACK / sizeof(u64)];
191         u64 regs[MAX_BPF_REG], tmp;
192         static const void *jumptable[256] = {
193                 [0 ... 255] = &&default_label,
194                 /* Now overwrite non-defaults ... */
195                 /* 32 bit ALU operations */
196                 [BPF_ALU | BPF_ADD | BPF_X] = &&ALU_ADD_X,
197                 [BPF_ALU | BPF_ADD | BPF_K] = &&ALU_ADD_K,
198                 [BPF_ALU | BPF_SUB | BPF_X] = &&ALU_SUB_X,
199                 [BPF_ALU | BPF_SUB | BPF_K] = &&ALU_SUB_K,
200                 [BPF_ALU | BPF_AND | BPF_X] = &&ALU_AND_X,
201                 [BPF_ALU | BPF_AND | BPF_K] = &&ALU_AND_K,
202                 [BPF_ALU | BPF_OR | BPF_X]  = &&ALU_OR_X,
203                 [BPF_ALU | BPF_OR | BPF_K]  = &&ALU_OR_K,
204                 [BPF_ALU | BPF_LSH | BPF_X] = &&ALU_LSH_X,
205                 [BPF_ALU | BPF_LSH | BPF_K] = &&ALU_LSH_K,
206                 [BPF_ALU | BPF_RSH | BPF_X] = &&ALU_RSH_X,
207                 [BPF_ALU | BPF_RSH | BPF_K] = &&ALU_RSH_K,
208                 [BPF_ALU | BPF_XOR | BPF_X] = &&ALU_XOR_X,
209                 [BPF_ALU | BPF_XOR | BPF_K] = &&ALU_XOR_K,
210                 [BPF_ALU | BPF_MUL | BPF_X] = &&ALU_MUL_X,
211                 [BPF_ALU | BPF_MUL | BPF_K] = &&ALU_MUL_K,
212                 [BPF_ALU | BPF_MOV | BPF_X] = &&ALU_MOV_X,
213                 [BPF_ALU | BPF_MOV | BPF_K] = &&ALU_MOV_K,
214                 [BPF_ALU | BPF_DIV | BPF_X] = &&ALU_DIV_X,
215                 [BPF_ALU | BPF_DIV | BPF_K] = &&ALU_DIV_K,
216                 [BPF_ALU | BPF_MOD | BPF_X] = &&ALU_MOD_X,
217                 [BPF_ALU | BPF_MOD | BPF_K] = &&ALU_MOD_K,
218                 [BPF_ALU | BPF_NEG] = &&ALU_NEG,
219                 [BPF_ALU | BPF_END | BPF_TO_BE] = &&ALU_END_TO_BE,
220                 [BPF_ALU | BPF_END | BPF_TO_LE] = &&ALU_END_TO_LE,
221                 /* 64 bit ALU operations */
222                 [BPF_ALU64 | BPF_ADD | BPF_X] = &&ALU64_ADD_X,
223                 [BPF_ALU64 | BPF_ADD | BPF_K] = &&ALU64_ADD_K,
224                 [BPF_ALU64 | BPF_SUB | BPF_X] = &&ALU64_SUB_X,
225                 [BPF_ALU64 | BPF_SUB | BPF_K] = &&ALU64_SUB_K,
226                 [BPF_ALU64 | BPF_AND | BPF_X] = &&ALU64_AND_X,
227                 [BPF_ALU64 | BPF_AND | BPF_K] = &&ALU64_AND_K,
228                 [BPF_ALU64 | BPF_OR | BPF_X] = &&ALU64_OR_X,
229                 [BPF_ALU64 | BPF_OR | BPF_K] = &&ALU64_OR_K,
230                 [BPF_ALU64 | BPF_LSH | BPF_X] = &&ALU64_LSH_X,
231                 [BPF_ALU64 | BPF_LSH | BPF_K] = &&ALU64_LSH_K,
232                 [BPF_ALU64 | BPF_RSH | BPF_X] = &&ALU64_RSH_X,
233                 [BPF_ALU64 | BPF_RSH | BPF_K] = &&ALU64_RSH_K,
234                 [BPF_ALU64 | BPF_XOR | BPF_X] = &&ALU64_XOR_X,
235                 [BPF_ALU64 | BPF_XOR | BPF_K] = &&ALU64_XOR_K,
236                 [BPF_ALU64 | BPF_MUL | BPF_X] = &&ALU64_MUL_X,
237                 [BPF_ALU64 | BPF_MUL | BPF_K] = &&ALU64_MUL_K,
238                 [BPF_ALU64 | BPF_MOV | BPF_X] = &&ALU64_MOV_X,
239                 [BPF_ALU64 | BPF_MOV | BPF_K] = &&ALU64_MOV_K,
240                 [BPF_ALU64 | BPF_ARSH | BPF_X] = &&ALU64_ARSH_X,
241                 [BPF_ALU64 | BPF_ARSH | BPF_K] = &&ALU64_ARSH_K,
242                 [BPF_ALU64 | BPF_DIV | BPF_X] = &&ALU64_DIV_X,
243                 [BPF_ALU64 | BPF_DIV | BPF_K] = &&ALU64_DIV_K,
244                 [BPF_ALU64 | BPF_MOD | BPF_X] = &&ALU64_MOD_X,
245                 [BPF_ALU64 | BPF_MOD | BPF_K] = &&ALU64_MOD_K,
246                 [BPF_ALU64 | BPF_NEG] = &&ALU64_NEG,
247                 /* Call instruction */
248                 [BPF_JMP | BPF_CALL] = &&JMP_CALL,
249                 [BPF_JMP | BPF_CALL | BPF_X] = &&JMP_TAIL_CALL,
250                 /* Jumps */
251                 [BPF_JMP | BPF_JA] = &&JMP_JA,
252                 [BPF_JMP | BPF_JEQ | BPF_X] = &&JMP_JEQ_X,
253                 [BPF_JMP | BPF_JEQ | BPF_K] = &&JMP_JEQ_K,
254                 [BPF_JMP | BPF_JNE | BPF_X] = &&JMP_JNE_X,
255                 [BPF_JMP | BPF_JNE | BPF_K] = &&JMP_JNE_K,
256                 [BPF_JMP | BPF_JGT | BPF_X] = &&JMP_JGT_X,
257                 [BPF_JMP | BPF_JGT | BPF_K] = &&JMP_JGT_K,
258                 [BPF_JMP | BPF_JGE | BPF_X] = &&JMP_JGE_X,
259                 [BPF_JMP | BPF_JGE | BPF_K] = &&JMP_JGE_K,
260                 [BPF_JMP | BPF_JSGT | BPF_X] = &&JMP_JSGT_X,
261                 [BPF_JMP | BPF_JSGT | BPF_K] = &&JMP_JSGT_K,
262                 [BPF_JMP | BPF_JSGE | BPF_X] = &&JMP_JSGE_X,
263                 [BPF_JMP | BPF_JSGE | BPF_K] = &&JMP_JSGE_K,
264                 [BPF_JMP | BPF_JSET | BPF_X] = &&JMP_JSET_X,
265                 [BPF_JMP | BPF_JSET | BPF_K] = &&JMP_JSET_K,
266                 /* Program return */
267                 [BPF_JMP | BPF_EXIT] = &&JMP_EXIT,
268                 /* Store instructions */
269                 [BPF_STX | BPF_MEM | BPF_B] = &&STX_MEM_B,
270                 [BPF_STX | BPF_MEM | BPF_H] = &&STX_MEM_H,
271                 [BPF_STX | BPF_MEM | BPF_W] = &&STX_MEM_W,
272                 [BPF_STX | BPF_MEM | BPF_DW] = &&STX_MEM_DW,
273                 [BPF_STX | BPF_XADD | BPF_W] = &&STX_XADD_W,
274                 [BPF_STX | BPF_XADD | BPF_DW] = &&STX_XADD_DW,
275                 [BPF_ST | BPF_MEM | BPF_B] = &&ST_MEM_B,
276                 [BPF_ST | BPF_MEM | BPF_H] = &&ST_MEM_H,
277                 [BPF_ST | BPF_MEM | BPF_W] = &&ST_MEM_W,
278                 [BPF_ST | BPF_MEM | BPF_DW] = &&ST_MEM_DW,
279                 /* Load instructions */
280                 [BPF_LDX | BPF_MEM | BPF_B] = &&LDX_MEM_B,
281                 [BPF_LDX | BPF_MEM | BPF_H] = &&LDX_MEM_H,
282                 [BPF_LDX | BPF_MEM | BPF_W] = &&LDX_MEM_W,
283                 [BPF_LDX | BPF_MEM | BPF_DW] = &&LDX_MEM_DW,
284                 [BPF_LD | BPF_ABS | BPF_W] = &&LD_ABS_W,
285                 [BPF_LD | BPF_ABS | BPF_H] = &&LD_ABS_H,
286                 [BPF_LD | BPF_ABS | BPF_B] = &&LD_ABS_B,
287                 [BPF_LD | BPF_IND | BPF_W] = &&LD_IND_W,
288                 [BPF_LD | BPF_IND | BPF_H] = &&LD_IND_H,
289                 [BPF_LD | BPF_IND | BPF_B] = &&LD_IND_B,
290                 [BPF_LD | BPF_IMM | BPF_DW] = &&LD_IMM_DW,
291         };
292         u32 tail_call_cnt = 0;
293         void *ptr;
294         int off;
295
296 #define CONT     ({ insn++; goto select_insn; })
297 #define CONT_JMP ({ insn++; goto select_insn; })
298
299         FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)];
300         ARG1 = (u64) (unsigned long) ctx;
301
302         /* Registers used in classic BPF programs need to be reset first. */
303         regs[BPF_REG_A] = 0;
304         regs[BPF_REG_X] = 0;
305
306 select_insn:
307         goto *jumptable[insn->code];
308
309         /* ALU */
310 #define ALU(OPCODE, OP)                 \
311         ALU64_##OPCODE##_X:             \
312                 DST = DST OP SRC;       \
313                 CONT;                   \
314         ALU_##OPCODE##_X:               \
315                 DST = (u32) DST OP (u32) SRC;   \
316                 CONT;                   \
317         ALU64_##OPCODE##_K:             \
318                 DST = DST OP IMM;               \
319                 CONT;                   \
320         ALU_##OPCODE##_K:               \
321                 DST = (u32) DST OP (u32) IMM;   \
322                 CONT;
323
324         ALU(ADD,  +)
325         ALU(SUB,  -)
326         ALU(AND,  &)
327         ALU(OR,   |)
328         ALU(LSH, <<)
329         ALU(RSH, >>)
330         ALU(XOR,  ^)
331         ALU(MUL,  *)
332 #undef ALU
333         ALU_NEG:
334                 DST = (u32) -DST;
335                 CONT;
336         ALU64_NEG:
337                 DST = -DST;
338                 CONT;
339         ALU_MOV_X:
340                 DST = (u32) SRC;
341                 CONT;
342         ALU_MOV_K:
343                 DST = (u32) IMM;
344                 CONT;
345         ALU64_MOV_X:
346                 DST = SRC;
347                 CONT;
348         ALU64_MOV_K:
349                 DST = IMM;
350                 CONT;
351         LD_IMM_DW:
352                 DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
353                 insn++;
354                 CONT;
355         ALU64_ARSH_X:
356                 (*(s64 *) &DST) >>= SRC;
357                 CONT;
358         ALU64_ARSH_K:
359                 (*(s64 *) &DST) >>= IMM;
360                 CONT;
361         ALU64_MOD_X:
362                 if (unlikely(SRC == 0))
363                         return 0;
364                 div64_u64_rem(DST, SRC, &tmp);
365                 DST = tmp;
366                 CONT;
367         ALU_MOD_X:
368                 if (unlikely(SRC == 0))
369                         return 0;
370                 tmp = (u32) DST;
371                 DST = do_div(tmp, (u32) SRC);
372                 CONT;
373         ALU64_MOD_K:
374                 div64_u64_rem(DST, IMM, &tmp);
375                 DST = tmp;
376                 CONT;
377         ALU_MOD_K:
378                 tmp = (u32) DST;
379                 DST = do_div(tmp, (u32) IMM);
380                 CONT;
381         ALU64_DIV_X:
382                 if (unlikely(SRC == 0))
383                         return 0;
384                 DST = div64_u64(DST, SRC);
385                 CONT;
386         ALU_DIV_X:
387                 if (unlikely(SRC == 0))
388                         return 0;
389                 tmp = (u32) DST;
390                 do_div(tmp, (u32) SRC);
391                 DST = (u32) tmp;
392                 CONT;
393         ALU64_DIV_K:
394                 DST = div64_u64(DST, IMM);
395                 CONT;
396         ALU_DIV_K:
397                 tmp = (u32) DST;
398                 do_div(tmp, (u32) IMM);
399                 DST = (u32) tmp;
400                 CONT;
401         ALU_END_TO_BE:
402                 switch (IMM) {
403                 case 16:
404                         DST = (__force u16) cpu_to_be16(DST);
405                         break;
406                 case 32:
407                         DST = (__force u32) cpu_to_be32(DST);
408                         break;
409                 case 64:
410                         DST = (__force u64) cpu_to_be64(DST);
411                         break;
412                 }
413                 CONT;
414         ALU_END_TO_LE:
415                 switch (IMM) {
416                 case 16:
417                         DST = (__force u16) cpu_to_le16(DST);
418                         break;
419                 case 32:
420                         DST = (__force u32) cpu_to_le32(DST);
421                         break;
422                 case 64:
423                         DST = (__force u64) cpu_to_le64(DST);
424                         break;
425                 }
426                 CONT;
427
428         /* CALL */
429         JMP_CALL:
430                 /* Function call scratches BPF_R1-BPF_R5 registers,
431                  * preserves BPF_R6-BPF_R9, and stores return value
432                  * into BPF_R0.
433                  */
434                 BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
435                                                        BPF_R4, BPF_R5);
436                 CONT;
437
438         JMP_TAIL_CALL: {
439                 struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
440                 struct bpf_array *array = container_of(map, struct bpf_array, map);
441                 struct bpf_prog *prog;
442                 u64 index = BPF_R3;
443
444                 if (unlikely(index >= array->map.max_entries))
445                         goto out;
446
447                 if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
448                         goto out;
449
450                 tail_call_cnt++;
451
452                 prog = READ_ONCE(array->prog[index]);
453                 if (unlikely(!prog))
454                         goto out;
455
456                 /* ARG1 at this point is guaranteed to point to CTX from
457                  * the verifier side due to the fact that the tail call is
458                  * handeled like a helper, that is, bpf_tail_call_proto,
459                  * where arg1_type is ARG_PTR_TO_CTX.
460                  */
461                 insn = prog->insnsi;
462                 goto select_insn;
463 out:
464                 CONT;
465         }
466         /* JMP */
467         JMP_JA:
468                 insn += insn->off;
469                 CONT;
470         JMP_JEQ_X:
471                 if (DST == SRC) {
472                         insn += insn->off;
473                         CONT_JMP;
474                 }
475                 CONT;
476         JMP_JEQ_K:
477                 if (DST == IMM) {
478                         insn += insn->off;
479                         CONT_JMP;
480                 }
481                 CONT;
482         JMP_JNE_X:
483                 if (DST != SRC) {
484                         insn += insn->off;
485                         CONT_JMP;
486                 }
487                 CONT;
488         JMP_JNE_K:
489                 if (DST != IMM) {
490                         insn += insn->off;
491                         CONT_JMP;
492                 }
493                 CONT;
494         JMP_JGT_X:
495                 if (DST > SRC) {
496                         insn += insn->off;
497                         CONT_JMP;
498                 }
499                 CONT;
500         JMP_JGT_K:
501                 if (DST > IMM) {
502                         insn += insn->off;
503                         CONT_JMP;
504                 }
505                 CONT;
506         JMP_JGE_X:
507                 if (DST >= SRC) {
508                         insn += insn->off;
509                         CONT_JMP;
510                 }
511                 CONT;
512         JMP_JGE_K:
513                 if (DST >= IMM) {
514                         insn += insn->off;
515                         CONT_JMP;
516                 }
517                 CONT;
518         JMP_JSGT_X:
519                 if (((s64) DST) > ((s64) SRC)) {
520                         insn += insn->off;
521                         CONT_JMP;
522                 }
523                 CONT;
524         JMP_JSGT_K:
525                 if (((s64) DST) > ((s64) IMM)) {
526                         insn += insn->off;
527                         CONT_JMP;
528                 }
529                 CONT;
530         JMP_JSGE_X:
531                 if (((s64) DST) >= ((s64) SRC)) {
532                         insn += insn->off;
533                         CONT_JMP;
534                 }
535                 CONT;
536         JMP_JSGE_K:
537                 if (((s64) DST) >= ((s64) IMM)) {
538                         insn += insn->off;
539                         CONT_JMP;
540                 }
541                 CONT;
542         JMP_JSET_X:
543                 if (DST & SRC) {
544                         insn += insn->off;
545                         CONT_JMP;
546                 }
547                 CONT;
548         JMP_JSET_K:
549                 if (DST & IMM) {
550                         insn += insn->off;
551                         CONT_JMP;
552                 }
553                 CONT;
554         JMP_EXIT:
555                 return BPF_R0;
556
557         /* STX and ST and LDX*/
558 #define LDST(SIZEOP, SIZE)                                              \
559         STX_MEM_##SIZEOP:                                               \
560                 *(SIZE *)(unsigned long) (DST + insn->off) = SRC;       \
561                 CONT;                                                   \
562         ST_MEM_##SIZEOP:                                                \
563                 *(SIZE *)(unsigned long) (DST + insn->off) = IMM;       \
564                 CONT;                                                   \
565         LDX_MEM_##SIZEOP:                                               \
566                 DST = *(SIZE *)(unsigned long) (SRC + insn->off);       \
567                 CONT;
568
569         LDST(B,   u8)
570         LDST(H,  u16)
571         LDST(W,  u32)
572         LDST(DW, u64)
573 #undef LDST
574         STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
575                 atomic_add((u32) SRC, (atomic_t *)(unsigned long)
576                            (DST + insn->off));
577                 CONT;
578         STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
579                 atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
580                              (DST + insn->off));
581                 CONT;
582         LD_ABS_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + imm32)) */
583                 off = IMM;
584 load_word:
585                 /* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are
586                  * only appearing in the programs where ctx ==
587                  * skb. All programs keep 'ctx' in regs[BPF_REG_CTX]
588                  * == BPF_R6, bpf_convert_filter() saves it in BPF_R6,
589                  * internal BPF verifier will check that BPF_R6 ==
590                  * ctx.
591                  *
592                  * BPF_ABS and BPF_IND are wrappers of function calls,
593                  * so they scratch BPF_R1-BPF_R5 registers, preserve
594                  * BPF_R6-BPF_R9, and store return value into BPF_R0.
595                  *
596                  * Implicit input:
597                  *   ctx == skb == BPF_R6 == CTX
598                  *
599                  * Explicit input:
600                  *   SRC == any register
601                  *   IMM == 32-bit immediate
602                  *
603                  * Output:
604                  *   BPF_R0 - 8/16/32-bit skb data converted to cpu endianness
605                  */
606
607                 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 4, &tmp);
608                 if (likely(ptr != NULL)) {
609                         BPF_R0 = get_unaligned_be32(ptr);
610                         CONT;
611                 }
612
613                 return 0;
614         LD_ABS_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + imm32)) */
615                 off = IMM;
616 load_half:
617                 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 2, &tmp);
618                 if (likely(ptr != NULL)) {
619                         BPF_R0 = get_unaligned_be16(ptr);
620                         CONT;
621                 }
622
623                 return 0;
624         LD_ABS_B: /* BPF_R0 = *(u8 *) (skb->data + imm32) */
625                 off = IMM;
626 load_byte:
627                 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 1, &tmp);
628                 if (likely(ptr != NULL)) {
629                         BPF_R0 = *(u8 *)ptr;
630                         CONT;
631                 }
632
633                 return 0;
634         LD_IND_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + src_reg + imm32)) */
635                 off = IMM + SRC;
636                 goto load_word;
637         LD_IND_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + src_reg + imm32)) */
638                 off = IMM + SRC;
639                 goto load_half;
640         LD_IND_B: /* BPF_R0 = *(u8 *) (skb->data + src_reg + imm32) */
641                 off = IMM + SRC;
642                 goto load_byte;
643
644         default_label:
645                 /* If we ever reach this, we have a bug somewhere. */
646                 WARN_RATELIMIT(1, "unknown opcode %02x\n", insn->code);
647                 return 0;
648 }
649
650 bool bpf_prog_array_compatible(struct bpf_array *array,
651                                const struct bpf_prog *fp)
652 {
653         if (!array->owner_prog_type) {
654                 /* There's no owner yet where we could check for
655                  * compatibility.
656                  */
657                 array->owner_prog_type = fp->type;
658                 array->owner_jited = fp->jited;
659
660                 return true;
661         }
662
663         return array->owner_prog_type == fp->type &&
664                array->owner_jited == fp->jited;
665 }
666
667 static int bpf_check_tail_call(const struct bpf_prog *fp)
668 {
669         struct bpf_prog_aux *aux = fp->aux;
670         int i;
671
672         for (i = 0; i < aux->used_map_cnt; i++) {
673                 struct bpf_map *map = aux->used_maps[i];
674                 struct bpf_array *array;
675
676                 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
677                         continue;
678
679                 array = container_of(map, struct bpf_array, map);
680                 if (!bpf_prog_array_compatible(array, fp))
681                         return -EINVAL;
682         }
683
684         return 0;
685 }
686
687 /**
688  *      bpf_prog_select_runtime - select exec runtime for BPF program
689  *      @fp: bpf_prog populated with internal BPF program
690  *
691  * Try to JIT eBPF program, if JIT is not available, use interpreter.
692  * The BPF program will be executed via BPF_PROG_RUN() macro.
693  */
694 int bpf_prog_select_runtime(struct bpf_prog *fp)
695 {
696         fp->bpf_func = (void *) __bpf_prog_run;
697
698         bpf_int_jit_compile(fp);
699         bpf_prog_lock_ro(fp);
700
701         /* The tail call compatibility check can only be done at
702          * this late stage as we need to determine, if we deal
703          * with JITed or non JITed program concatenations and not
704          * all eBPF JITs might immediately support all features.
705          */
706         return bpf_check_tail_call(fp);
707 }
708 EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
709
710 static void bpf_prog_free_deferred(struct work_struct *work)
711 {
712         struct bpf_prog_aux *aux;
713
714         aux = container_of(work, struct bpf_prog_aux, work);
715         bpf_jit_free(aux->prog);
716 }
717
718 /* Free internal BPF program */
719 void bpf_prog_free(struct bpf_prog *fp)
720 {
721         struct bpf_prog_aux *aux = fp->aux;
722
723         INIT_WORK(&aux->work, bpf_prog_free_deferred);
724         aux->prog = fp;
725         schedule_work(&aux->work);
726 }
727 EXPORT_SYMBOL_GPL(bpf_prog_free);
728
729 /* Weak definitions of helper functions in case we don't have bpf syscall. */
730 const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
731 const struct bpf_func_proto bpf_map_update_elem_proto __weak;
732 const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
733
734 const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
735 const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
736 const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
737 const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
738 const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
739 const struct bpf_func_proto bpf_get_current_comm_proto __weak;
740 const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
741 {
742         return NULL;
743 }
744
745 /* Always built-in helper functions. */
746 const struct bpf_func_proto bpf_tail_call_proto = {
747         .func           = NULL,
748         .gpl_only       = false,
749         .ret_type       = RET_VOID,
750         .arg1_type      = ARG_PTR_TO_CTX,
751         .arg2_type      = ARG_CONST_MAP_PTR,
752         .arg3_type      = ARG_ANYTHING,
753 };
754
755 /* For classic BPF JITs that don't implement bpf_int_jit_compile(). */
756 void __weak bpf_int_jit_compile(struct bpf_prog *prog)
757 {
758 }
759
760 /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
761  * skb_copy_bits(), so provide a weak definition of it for NET-less config.
762  */
763 int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
764                          int len)
765 {
766         return -EFAULT;
767 }