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