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