xref: /openbmc/linux/kernel/bpf/core.c (revision eb3fcf00)
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 EXPORT_SYMBOL_GPL(__bpf_call_base);
181 
182 /**
183  *	__bpf_prog_run - run eBPF program on a given context
184  *	@ctx: is the data we are operating on
185  *	@insn: is the array of eBPF instructions
186  *
187  * Decode and execute eBPF instructions.
188  */
189 static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
190 {
191 	u64 stack[MAX_BPF_STACK / sizeof(u64)];
192 	u64 regs[MAX_BPF_REG], tmp;
193 	static const void *jumptable[256] = {
194 		[0 ... 255] = &&default_label,
195 		/* Now overwrite non-defaults ... */
196 		/* 32 bit ALU operations */
197 		[BPF_ALU | BPF_ADD | BPF_X] = &&ALU_ADD_X,
198 		[BPF_ALU | BPF_ADD | BPF_K] = &&ALU_ADD_K,
199 		[BPF_ALU | BPF_SUB | BPF_X] = &&ALU_SUB_X,
200 		[BPF_ALU | BPF_SUB | BPF_K] = &&ALU_SUB_K,
201 		[BPF_ALU | BPF_AND | BPF_X] = &&ALU_AND_X,
202 		[BPF_ALU | BPF_AND | BPF_K] = &&ALU_AND_K,
203 		[BPF_ALU | BPF_OR | BPF_X]  = &&ALU_OR_X,
204 		[BPF_ALU | BPF_OR | BPF_K]  = &&ALU_OR_K,
205 		[BPF_ALU | BPF_LSH | BPF_X] = &&ALU_LSH_X,
206 		[BPF_ALU | BPF_LSH | BPF_K] = &&ALU_LSH_K,
207 		[BPF_ALU | BPF_RSH | BPF_X] = &&ALU_RSH_X,
208 		[BPF_ALU | BPF_RSH | BPF_K] = &&ALU_RSH_K,
209 		[BPF_ALU | BPF_XOR | BPF_X] = &&ALU_XOR_X,
210 		[BPF_ALU | BPF_XOR | BPF_K] = &&ALU_XOR_K,
211 		[BPF_ALU | BPF_MUL | BPF_X] = &&ALU_MUL_X,
212 		[BPF_ALU | BPF_MUL | BPF_K] = &&ALU_MUL_K,
213 		[BPF_ALU | BPF_MOV | BPF_X] = &&ALU_MOV_X,
214 		[BPF_ALU | BPF_MOV | BPF_K] = &&ALU_MOV_K,
215 		[BPF_ALU | BPF_DIV | BPF_X] = &&ALU_DIV_X,
216 		[BPF_ALU | BPF_DIV | BPF_K] = &&ALU_DIV_K,
217 		[BPF_ALU | BPF_MOD | BPF_X] = &&ALU_MOD_X,
218 		[BPF_ALU | BPF_MOD | BPF_K] = &&ALU_MOD_K,
219 		[BPF_ALU | BPF_NEG] = &&ALU_NEG,
220 		[BPF_ALU | BPF_END | BPF_TO_BE] = &&ALU_END_TO_BE,
221 		[BPF_ALU | BPF_END | BPF_TO_LE] = &&ALU_END_TO_LE,
222 		/* 64 bit ALU operations */
223 		[BPF_ALU64 | BPF_ADD | BPF_X] = &&ALU64_ADD_X,
224 		[BPF_ALU64 | BPF_ADD | BPF_K] = &&ALU64_ADD_K,
225 		[BPF_ALU64 | BPF_SUB | BPF_X] = &&ALU64_SUB_X,
226 		[BPF_ALU64 | BPF_SUB | BPF_K] = &&ALU64_SUB_K,
227 		[BPF_ALU64 | BPF_AND | BPF_X] = &&ALU64_AND_X,
228 		[BPF_ALU64 | BPF_AND | BPF_K] = &&ALU64_AND_K,
229 		[BPF_ALU64 | BPF_OR | BPF_X] = &&ALU64_OR_X,
230 		[BPF_ALU64 | BPF_OR | BPF_K] = &&ALU64_OR_K,
231 		[BPF_ALU64 | BPF_LSH | BPF_X] = &&ALU64_LSH_X,
232 		[BPF_ALU64 | BPF_LSH | BPF_K] = &&ALU64_LSH_K,
233 		[BPF_ALU64 | BPF_RSH | BPF_X] = &&ALU64_RSH_X,
234 		[BPF_ALU64 | BPF_RSH | BPF_K] = &&ALU64_RSH_K,
235 		[BPF_ALU64 | BPF_XOR | BPF_X] = &&ALU64_XOR_X,
236 		[BPF_ALU64 | BPF_XOR | BPF_K] = &&ALU64_XOR_K,
237 		[BPF_ALU64 | BPF_MUL | BPF_X] = &&ALU64_MUL_X,
238 		[BPF_ALU64 | BPF_MUL | BPF_K] = &&ALU64_MUL_K,
239 		[BPF_ALU64 | BPF_MOV | BPF_X] = &&ALU64_MOV_X,
240 		[BPF_ALU64 | BPF_MOV | BPF_K] = &&ALU64_MOV_K,
241 		[BPF_ALU64 | BPF_ARSH | BPF_X] = &&ALU64_ARSH_X,
242 		[BPF_ALU64 | BPF_ARSH | BPF_K] = &&ALU64_ARSH_K,
243 		[BPF_ALU64 | BPF_DIV | BPF_X] = &&ALU64_DIV_X,
244 		[BPF_ALU64 | BPF_DIV | BPF_K] = &&ALU64_DIV_K,
245 		[BPF_ALU64 | BPF_MOD | BPF_X] = &&ALU64_MOD_X,
246 		[BPF_ALU64 | BPF_MOD | BPF_K] = &&ALU64_MOD_K,
247 		[BPF_ALU64 | BPF_NEG] = &&ALU64_NEG,
248 		/* Call instruction */
249 		[BPF_JMP | BPF_CALL] = &&JMP_CALL,
250 		[BPF_JMP | BPF_CALL | BPF_X] = &&JMP_TAIL_CALL,
251 		/* Jumps */
252 		[BPF_JMP | BPF_JA] = &&JMP_JA,
253 		[BPF_JMP | BPF_JEQ | BPF_X] = &&JMP_JEQ_X,
254 		[BPF_JMP | BPF_JEQ | BPF_K] = &&JMP_JEQ_K,
255 		[BPF_JMP | BPF_JNE | BPF_X] = &&JMP_JNE_X,
256 		[BPF_JMP | BPF_JNE | BPF_K] = &&JMP_JNE_K,
257 		[BPF_JMP | BPF_JGT | BPF_X] = &&JMP_JGT_X,
258 		[BPF_JMP | BPF_JGT | BPF_K] = &&JMP_JGT_K,
259 		[BPF_JMP | BPF_JGE | BPF_X] = &&JMP_JGE_X,
260 		[BPF_JMP | BPF_JGE | BPF_K] = &&JMP_JGE_K,
261 		[BPF_JMP | BPF_JSGT | BPF_X] = &&JMP_JSGT_X,
262 		[BPF_JMP | BPF_JSGT | BPF_K] = &&JMP_JSGT_K,
263 		[BPF_JMP | BPF_JSGE | BPF_X] = &&JMP_JSGE_X,
264 		[BPF_JMP | BPF_JSGE | BPF_K] = &&JMP_JSGE_K,
265 		[BPF_JMP | BPF_JSET | BPF_X] = &&JMP_JSET_X,
266 		[BPF_JMP | BPF_JSET | BPF_K] = &&JMP_JSET_K,
267 		/* Program return */
268 		[BPF_JMP | BPF_EXIT] = &&JMP_EXIT,
269 		/* Store instructions */
270 		[BPF_STX | BPF_MEM | BPF_B] = &&STX_MEM_B,
271 		[BPF_STX | BPF_MEM | BPF_H] = &&STX_MEM_H,
272 		[BPF_STX | BPF_MEM | BPF_W] = &&STX_MEM_W,
273 		[BPF_STX | BPF_MEM | BPF_DW] = &&STX_MEM_DW,
274 		[BPF_STX | BPF_XADD | BPF_W] = &&STX_XADD_W,
275 		[BPF_STX | BPF_XADD | BPF_DW] = &&STX_XADD_DW,
276 		[BPF_ST | BPF_MEM | BPF_B] = &&ST_MEM_B,
277 		[BPF_ST | BPF_MEM | BPF_H] = &&ST_MEM_H,
278 		[BPF_ST | BPF_MEM | BPF_W] = &&ST_MEM_W,
279 		[BPF_ST | BPF_MEM | BPF_DW] = &&ST_MEM_DW,
280 		/* Load instructions */
281 		[BPF_LDX | BPF_MEM | BPF_B] = &&LDX_MEM_B,
282 		[BPF_LDX | BPF_MEM | BPF_H] = &&LDX_MEM_H,
283 		[BPF_LDX | BPF_MEM | BPF_W] = &&LDX_MEM_W,
284 		[BPF_LDX | BPF_MEM | BPF_DW] = &&LDX_MEM_DW,
285 		[BPF_LD | BPF_ABS | BPF_W] = &&LD_ABS_W,
286 		[BPF_LD | BPF_ABS | BPF_H] = &&LD_ABS_H,
287 		[BPF_LD | BPF_ABS | BPF_B] = &&LD_ABS_B,
288 		[BPF_LD | BPF_IND | BPF_W] = &&LD_IND_W,
289 		[BPF_LD | BPF_IND | BPF_H] = &&LD_IND_H,
290 		[BPF_LD | BPF_IND | BPF_B] = &&LD_IND_B,
291 		[BPF_LD | BPF_IMM | BPF_DW] = &&LD_IMM_DW,
292 	};
293 	u32 tail_call_cnt = 0;
294 	void *ptr;
295 	int off;
296 
297 #define CONT	 ({ insn++; goto select_insn; })
298 #define CONT_JMP ({ insn++; goto select_insn; })
299 
300 	FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)];
301 	ARG1 = (u64) (unsigned long) ctx;
302 
303 	/* Registers used in classic BPF programs need to be reset first. */
304 	regs[BPF_REG_A] = 0;
305 	regs[BPF_REG_X] = 0;
306 
307 select_insn:
308 	goto *jumptable[insn->code];
309 
310 	/* ALU */
311 #define ALU(OPCODE, OP)			\
312 	ALU64_##OPCODE##_X:		\
313 		DST = DST OP SRC;	\
314 		CONT;			\
315 	ALU_##OPCODE##_X:		\
316 		DST = (u32) DST OP (u32) SRC;	\
317 		CONT;			\
318 	ALU64_##OPCODE##_K:		\
319 		DST = DST OP IMM;		\
320 		CONT;			\
321 	ALU_##OPCODE##_K:		\
322 		DST = (u32) DST OP (u32) IMM;	\
323 		CONT;
324 
325 	ALU(ADD,  +)
326 	ALU(SUB,  -)
327 	ALU(AND,  &)
328 	ALU(OR,   |)
329 	ALU(LSH, <<)
330 	ALU(RSH, >>)
331 	ALU(XOR,  ^)
332 	ALU(MUL,  *)
333 #undef ALU
334 	ALU_NEG:
335 		DST = (u32) -DST;
336 		CONT;
337 	ALU64_NEG:
338 		DST = -DST;
339 		CONT;
340 	ALU_MOV_X:
341 		DST = (u32) SRC;
342 		CONT;
343 	ALU_MOV_K:
344 		DST = (u32) IMM;
345 		CONT;
346 	ALU64_MOV_X:
347 		DST = SRC;
348 		CONT;
349 	ALU64_MOV_K:
350 		DST = IMM;
351 		CONT;
352 	LD_IMM_DW:
353 		DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
354 		insn++;
355 		CONT;
356 	ALU64_ARSH_X:
357 		(*(s64 *) &DST) >>= SRC;
358 		CONT;
359 	ALU64_ARSH_K:
360 		(*(s64 *) &DST) >>= IMM;
361 		CONT;
362 	ALU64_MOD_X:
363 		if (unlikely(SRC == 0))
364 			return 0;
365 		div64_u64_rem(DST, SRC, &tmp);
366 		DST = tmp;
367 		CONT;
368 	ALU_MOD_X:
369 		if (unlikely(SRC == 0))
370 			return 0;
371 		tmp = (u32) DST;
372 		DST = do_div(tmp, (u32) SRC);
373 		CONT;
374 	ALU64_MOD_K:
375 		div64_u64_rem(DST, IMM, &tmp);
376 		DST = tmp;
377 		CONT;
378 	ALU_MOD_K:
379 		tmp = (u32) DST;
380 		DST = do_div(tmp, (u32) IMM);
381 		CONT;
382 	ALU64_DIV_X:
383 		if (unlikely(SRC == 0))
384 			return 0;
385 		DST = div64_u64(DST, SRC);
386 		CONT;
387 	ALU_DIV_X:
388 		if (unlikely(SRC == 0))
389 			return 0;
390 		tmp = (u32) DST;
391 		do_div(tmp, (u32) SRC);
392 		DST = (u32) tmp;
393 		CONT;
394 	ALU64_DIV_K:
395 		DST = div64_u64(DST, IMM);
396 		CONT;
397 	ALU_DIV_K:
398 		tmp = (u32) DST;
399 		do_div(tmp, (u32) IMM);
400 		DST = (u32) tmp;
401 		CONT;
402 	ALU_END_TO_BE:
403 		switch (IMM) {
404 		case 16:
405 			DST = (__force u16) cpu_to_be16(DST);
406 			break;
407 		case 32:
408 			DST = (__force u32) cpu_to_be32(DST);
409 			break;
410 		case 64:
411 			DST = (__force u64) cpu_to_be64(DST);
412 			break;
413 		}
414 		CONT;
415 	ALU_END_TO_LE:
416 		switch (IMM) {
417 		case 16:
418 			DST = (__force u16) cpu_to_le16(DST);
419 			break;
420 		case 32:
421 			DST = (__force u32) cpu_to_le32(DST);
422 			break;
423 		case 64:
424 			DST = (__force u64) cpu_to_le64(DST);
425 			break;
426 		}
427 		CONT;
428 
429 	/* CALL */
430 	JMP_CALL:
431 		/* Function call scratches BPF_R1-BPF_R5 registers,
432 		 * preserves BPF_R6-BPF_R9, and stores return value
433 		 * into BPF_R0.
434 		 */
435 		BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
436 						       BPF_R4, BPF_R5);
437 		CONT;
438 
439 	JMP_TAIL_CALL: {
440 		struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
441 		struct bpf_array *array = container_of(map, struct bpf_array, map);
442 		struct bpf_prog *prog;
443 		u64 index = BPF_R3;
444 
445 		if (unlikely(index >= array->map.max_entries))
446 			goto out;
447 
448 		if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
449 			goto out;
450 
451 		tail_call_cnt++;
452 
453 		prog = READ_ONCE(array->ptrs[index]);
454 		if (unlikely(!prog))
455 			goto out;
456 
457 		/* ARG1 at this point is guaranteed to point to CTX from
458 		 * the verifier side due to the fact that the tail call is
459 		 * handeled like a helper, that is, bpf_tail_call_proto,
460 		 * where arg1_type is ARG_PTR_TO_CTX.
461 		 */
462 		insn = prog->insnsi;
463 		goto select_insn;
464 out:
465 		CONT;
466 	}
467 	/* JMP */
468 	JMP_JA:
469 		insn += insn->off;
470 		CONT;
471 	JMP_JEQ_X:
472 		if (DST == SRC) {
473 			insn += insn->off;
474 			CONT_JMP;
475 		}
476 		CONT;
477 	JMP_JEQ_K:
478 		if (DST == IMM) {
479 			insn += insn->off;
480 			CONT_JMP;
481 		}
482 		CONT;
483 	JMP_JNE_X:
484 		if (DST != SRC) {
485 			insn += insn->off;
486 			CONT_JMP;
487 		}
488 		CONT;
489 	JMP_JNE_K:
490 		if (DST != IMM) {
491 			insn += insn->off;
492 			CONT_JMP;
493 		}
494 		CONT;
495 	JMP_JGT_X:
496 		if (DST > SRC) {
497 			insn += insn->off;
498 			CONT_JMP;
499 		}
500 		CONT;
501 	JMP_JGT_K:
502 		if (DST > IMM) {
503 			insn += insn->off;
504 			CONT_JMP;
505 		}
506 		CONT;
507 	JMP_JGE_X:
508 		if (DST >= SRC) {
509 			insn += insn->off;
510 			CONT_JMP;
511 		}
512 		CONT;
513 	JMP_JGE_K:
514 		if (DST >= IMM) {
515 			insn += insn->off;
516 			CONT_JMP;
517 		}
518 		CONT;
519 	JMP_JSGT_X:
520 		if (((s64) DST) > ((s64) SRC)) {
521 			insn += insn->off;
522 			CONT_JMP;
523 		}
524 		CONT;
525 	JMP_JSGT_K:
526 		if (((s64) DST) > ((s64) IMM)) {
527 			insn += insn->off;
528 			CONT_JMP;
529 		}
530 		CONT;
531 	JMP_JSGE_X:
532 		if (((s64) DST) >= ((s64) SRC)) {
533 			insn += insn->off;
534 			CONT_JMP;
535 		}
536 		CONT;
537 	JMP_JSGE_K:
538 		if (((s64) DST) >= ((s64) IMM)) {
539 			insn += insn->off;
540 			CONT_JMP;
541 		}
542 		CONT;
543 	JMP_JSET_X:
544 		if (DST & SRC) {
545 			insn += insn->off;
546 			CONT_JMP;
547 		}
548 		CONT;
549 	JMP_JSET_K:
550 		if (DST & IMM) {
551 			insn += insn->off;
552 			CONT_JMP;
553 		}
554 		CONT;
555 	JMP_EXIT:
556 		return BPF_R0;
557 
558 	/* STX and ST and LDX*/
559 #define LDST(SIZEOP, SIZE)						\
560 	STX_MEM_##SIZEOP:						\
561 		*(SIZE *)(unsigned long) (DST + insn->off) = SRC;	\
562 		CONT;							\
563 	ST_MEM_##SIZEOP:						\
564 		*(SIZE *)(unsigned long) (DST + insn->off) = IMM;	\
565 		CONT;							\
566 	LDX_MEM_##SIZEOP:						\
567 		DST = *(SIZE *)(unsigned long) (SRC + insn->off);	\
568 		CONT;
569 
570 	LDST(B,   u8)
571 	LDST(H,  u16)
572 	LDST(W,  u32)
573 	LDST(DW, u64)
574 #undef LDST
575 	STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
576 		atomic_add((u32) SRC, (atomic_t *)(unsigned long)
577 			   (DST + insn->off));
578 		CONT;
579 	STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
580 		atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
581 			     (DST + insn->off));
582 		CONT;
583 	LD_ABS_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + imm32)) */
584 		off = IMM;
585 load_word:
586 		/* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are
587 		 * only appearing in the programs where ctx ==
588 		 * skb. All programs keep 'ctx' in regs[BPF_REG_CTX]
589 		 * == BPF_R6, bpf_convert_filter() saves it in BPF_R6,
590 		 * internal BPF verifier will check that BPF_R6 ==
591 		 * ctx.
592 		 *
593 		 * BPF_ABS and BPF_IND are wrappers of function calls,
594 		 * so they scratch BPF_R1-BPF_R5 registers, preserve
595 		 * BPF_R6-BPF_R9, and store return value into BPF_R0.
596 		 *
597 		 * Implicit input:
598 		 *   ctx == skb == BPF_R6 == CTX
599 		 *
600 		 * Explicit input:
601 		 *   SRC == any register
602 		 *   IMM == 32-bit immediate
603 		 *
604 		 * Output:
605 		 *   BPF_R0 - 8/16/32-bit skb data converted to cpu endianness
606 		 */
607 
608 		ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 4, &tmp);
609 		if (likely(ptr != NULL)) {
610 			BPF_R0 = get_unaligned_be32(ptr);
611 			CONT;
612 		}
613 
614 		return 0;
615 	LD_ABS_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + imm32)) */
616 		off = IMM;
617 load_half:
618 		ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 2, &tmp);
619 		if (likely(ptr != NULL)) {
620 			BPF_R0 = get_unaligned_be16(ptr);
621 			CONT;
622 		}
623 
624 		return 0;
625 	LD_ABS_B: /* BPF_R0 = *(u8 *) (skb->data + imm32) */
626 		off = IMM;
627 load_byte:
628 		ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 1, &tmp);
629 		if (likely(ptr != NULL)) {
630 			BPF_R0 = *(u8 *)ptr;
631 			CONT;
632 		}
633 
634 		return 0;
635 	LD_IND_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + src_reg + imm32)) */
636 		off = IMM + SRC;
637 		goto load_word;
638 	LD_IND_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + src_reg + imm32)) */
639 		off = IMM + SRC;
640 		goto load_half;
641 	LD_IND_B: /* BPF_R0 = *(u8 *) (skb->data + src_reg + imm32) */
642 		off = IMM + SRC;
643 		goto load_byte;
644 
645 	default_label:
646 		/* If we ever reach this, we have a bug somewhere. */
647 		WARN_RATELIMIT(1, "unknown opcode %02x\n", insn->code);
648 		return 0;
649 }
650 
651 bool bpf_prog_array_compatible(struct bpf_array *array,
652 			       const struct bpf_prog *fp)
653 {
654 	if (!array->owner_prog_type) {
655 		/* There's no owner yet where we could check for
656 		 * compatibility.
657 		 */
658 		array->owner_prog_type = fp->type;
659 		array->owner_jited = fp->jited;
660 
661 		return true;
662 	}
663 
664 	return array->owner_prog_type == fp->type &&
665 	       array->owner_jited == fp->jited;
666 }
667 
668 static int bpf_check_tail_call(const struct bpf_prog *fp)
669 {
670 	struct bpf_prog_aux *aux = fp->aux;
671 	int i;
672 
673 	for (i = 0; i < aux->used_map_cnt; i++) {
674 		struct bpf_map *map = aux->used_maps[i];
675 		struct bpf_array *array;
676 
677 		if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
678 			continue;
679 
680 		array = container_of(map, struct bpf_array, map);
681 		if (!bpf_prog_array_compatible(array, fp))
682 			return -EINVAL;
683 	}
684 
685 	return 0;
686 }
687 
688 /**
689  *	bpf_prog_select_runtime - select exec runtime for BPF program
690  *	@fp: bpf_prog populated with internal BPF program
691  *
692  * Try to JIT eBPF program, if JIT is not available, use interpreter.
693  * The BPF program will be executed via BPF_PROG_RUN() macro.
694  */
695 int bpf_prog_select_runtime(struct bpf_prog *fp)
696 {
697 	fp->bpf_func = (void *) __bpf_prog_run;
698 
699 	bpf_int_jit_compile(fp);
700 	bpf_prog_lock_ro(fp);
701 
702 	/* The tail call compatibility check can only be done at
703 	 * this late stage as we need to determine, if we deal
704 	 * with JITed or non JITed program concatenations and not
705 	 * all eBPF JITs might immediately support all features.
706 	 */
707 	return bpf_check_tail_call(fp);
708 }
709 EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
710 
711 static void bpf_prog_free_deferred(struct work_struct *work)
712 {
713 	struct bpf_prog_aux *aux;
714 
715 	aux = container_of(work, struct bpf_prog_aux, work);
716 	bpf_jit_free(aux->prog);
717 }
718 
719 /* Free internal BPF program */
720 void bpf_prog_free(struct bpf_prog *fp)
721 {
722 	struct bpf_prog_aux *aux = fp->aux;
723 
724 	INIT_WORK(&aux->work, bpf_prog_free_deferred);
725 	aux->prog = fp;
726 	schedule_work(&aux->work);
727 }
728 EXPORT_SYMBOL_GPL(bpf_prog_free);
729 
730 /* Weak definitions of helper functions in case we don't have bpf syscall. */
731 const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
732 const struct bpf_func_proto bpf_map_update_elem_proto __weak;
733 const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
734 
735 const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
736 const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
737 const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
738 const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
739 const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
740 const struct bpf_func_proto bpf_get_current_comm_proto __weak;
741 const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
742 {
743 	return NULL;
744 }
745 
746 /* Always built-in helper functions. */
747 const struct bpf_func_proto bpf_tail_call_proto = {
748 	.func		= NULL,
749 	.gpl_only	= false,
750 	.ret_type	= RET_VOID,
751 	.arg1_type	= ARG_PTR_TO_CTX,
752 	.arg2_type	= ARG_CONST_MAP_PTR,
753 	.arg3_type	= ARG_ANYTHING,
754 };
755 
756 /* For classic BPF JITs that don't implement bpf_int_jit_compile(). */
757 void __weak bpf_int_jit_compile(struct bpf_prog *prog)
758 {
759 }
760 
761 /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
762  * skb_copy_bits(), so provide a weak definition of it for NET-less config.
763  */
764 int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
765 			 int len)
766 {
767 	return -EFAULT;
768 }
769