1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Linux Socket Filter - Kernel level socket filtering
4 *
5 * Based on the design of the Berkeley Packet Filter. The new
6 * internal format has been designed by PLUMgrid:
7 *
8 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
9 *
10 * Authors:
11 *
12 * Jay Schulist <jschlst@samba.org>
13 * Alexei Starovoitov <ast@plumgrid.com>
14 * Daniel Borkmann <dborkman@redhat.com>
15 *
16 * Andi Kleen - Fix a few bad bugs and races.
17 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
18 */
19
20 #include <uapi/linux/btf.h>
21 #include <crypto/sha1.h>
22 #include <linux/filter.h>
23 #include <linux/skbuff.h>
24 #include <linux/vmalloc.h>
25 #include <linux/prandom.h>
26 #include <linux/bpf.h>
27 #include <linux/btf.h>
28 #include <linux/hex.h>
29 #include <linux/objtool.h>
30 #include <linux/overflow.h>
31 #include <linux/rbtree_latch.h>
32 #include <linux/kallsyms.h>
33 #include <linux/rcupdate.h>
34 #include <linux/perf_event.h>
35 #include <linux/extable.h>
36 #include <linux/log2.h>
37 #include <linux/bpf_verifier.h>
38 #include <linux/nodemask.h>
39 #include <linux/nospec.h>
40 #include <linux/bpf_mem_alloc.h>
41 #include <linux/memcontrol.h>
42 #include <linux/execmem.h>
43 #include <crypto/sha2.h>
44
45 #include <asm/barrier.h>
46 #include <linux/unaligned.h>
47
48 /* Registers */
49 #define BPF_R0 regs[BPF_REG_0]
50 #define BPF_R1 regs[BPF_REG_1]
51 #define BPF_R2 regs[BPF_REG_2]
52 #define BPF_R3 regs[BPF_REG_3]
53 #define BPF_R4 regs[BPF_REG_4]
54 #define BPF_R5 regs[BPF_REG_5]
55 #define BPF_R6 regs[BPF_REG_6]
56 #define BPF_R7 regs[BPF_REG_7]
57 #define BPF_R8 regs[BPF_REG_8]
58 #define BPF_R9 regs[BPF_REG_9]
59 #define BPF_R10 regs[BPF_REG_10]
60
61 /* Named registers */
62 #define DST regs[insn->dst_reg]
63 #define SRC regs[insn->src_reg]
64 #define FP regs[BPF_REG_FP]
65 #define AX regs[BPF_REG_AX]
66 #define ARG1 regs[BPF_REG_ARG1]
67 #define CTX regs[BPF_REG_CTX]
68 #define OFF insn->off
69 #define IMM insn->imm
70
71 struct bpf_mem_alloc bpf_global_ma;
72 bool bpf_global_ma_set;
73
74 /* No hurry in this branch
75 *
76 * Exported for the bpf jit load helper.
77 */
bpf_internal_load_pointer_neg_helper(const struct sk_buff * skb,int k,unsigned int size)78 void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
79 {
80 u8 *ptr = NULL;
81
82 if (k >= SKF_NET_OFF) {
83 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
84 } else if (k >= SKF_LL_OFF) {
85 if (unlikely(!skb_mac_header_was_set(skb)))
86 return NULL;
87 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
88 }
89 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
90 return ptr;
91
92 return NULL;
93 }
94
95 /* tell bpf programs that include vmlinux.h kernel's PAGE_SIZE */
96 enum page_size_enum {
97 __PAGE_SIZE = PAGE_SIZE
98 };
99
bpf_prog_alloc_no_stats(unsigned int size,gfp_t gfp_extra_flags)100 struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flags)
101 {
102 gfp_t gfp_flags = bpf_memcg_flags(GFP_KERNEL | __GFP_ZERO | gfp_extra_flags);
103 struct bpf_prog_aux *aux;
104 struct bpf_prog *fp;
105
106 size = round_up(size, __PAGE_SIZE);
107 fp = __vmalloc(size, gfp_flags);
108 if (fp == NULL)
109 return NULL;
110
111 aux = kzalloc_obj(*aux, bpf_memcg_flags(GFP_KERNEL | gfp_extra_flags));
112 if (aux == NULL) {
113 vfree(fp);
114 return NULL;
115 }
116 fp->active = __alloc_percpu_gfp(sizeof(u8[BPF_NR_CONTEXTS]), 4,
117 bpf_memcg_flags(GFP_KERNEL | gfp_extra_flags));
118 if (!fp->active) {
119 vfree(fp);
120 kfree(aux);
121 return NULL;
122 }
123
124 fp->pages = size / PAGE_SIZE;
125 fp->aux = aux;
126 fp->aux->main_prog_aux = aux;
127 fp->aux->prog = fp;
128 fp->jit_requested = ebpf_jit_enabled();
129 fp->blinding_requested = bpf_jit_blinding_enabled(fp);
130 #ifdef CONFIG_CGROUP_BPF
131 aux->cgroup_atype = CGROUP_BPF_ATTACH_TYPE_INVALID;
132 #endif
133
134 INIT_LIST_HEAD_RCU(&fp->aux->ksym.lnode);
135 #ifdef CONFIG_FINEIBT
136 INIT_LIST_HEAD_RCU(&fp->aux->ksym_prefix.lnode);
137 #endif
138 mutex_init(&fp->aux->used_maps_mutex);
139 mutex_init(&fp->aux->ext_mutex);
140 mutex_init(&fp->aux->dst_mutex);
141 mutex_init(&fp->aux->st_ops_assoc_mutex);
142
143 #ifdef CONFIG_BPF_SYSCALL
144 bpf_prog_stream_init(fp);
145 #endif
146
147 return fp;
148 }
149
bpf_prog_alloc(unsigned int size,gfp_t gfp_extra_flags)150 struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
151 {
152 gfp_t gfp_flags = bpf_memcg_flags(GFP_KERNEL | __GFP_ZERO | gfp_extra_flags);
153 struct bpf_prog *prog;
154 int cpu;
155
156 prog = bpf_prog_alloc_no_stats(size, gfp_extra_flags);
157 if (!prog)
158 return NULL;
159
160 prog->stats = alloc_percpu_gfp(struct bpf_prog_stats, gfp_flags);
161 if (!prog->stats) {
162 free_percpu(prog->active);
163 kfree(prog->aux);
164 vfree(prog);
165 return NULL;
166 }
167
168 for_each_possible_cpu(cpu) {
169 struct bpf_prog_stats *pstats;
170
171 pstats = per_cpu_ptr(prog->stats, cpu);
172 u64_stats_init(&pstats->syncp);
173 }
174 return prog;
175 }
176 EXPORT_SYMBOL_GPL(bpf_prog_alloc);
177
bpf_prog_alloc_jited_linfo(struct bpf_prog * prog)178 int bpf_prog_alloc_jited_linfo(struct bpf_prog *prog)
179 {
180 if (!prog->aux->nr_linfo || !prog->jit_requested)
181 return 0;
182
183 prog->aux->jited_linfo = kvzalloc_objs(*prog->aux->jited_linfo,
184 prog->aux->nr_linfo,
185 bpf_memcg_flags(GFP_KERNEL | __GFP_NOWARN));
186 if (!prog->aux->jited_linfo)
187 return -ENOMEM;
188
189 return 0;
190 }
191
bpf_prog_jit_attempt_done(struct bpf_prog * prog)192 void bpf_prog_jit_attempt_done(struct bpf_prog *prog)
193 {
194 if (prog->aux->jited_linfo &&
195 (!prog->jited || !prog->aux->jited_linfo[0])) {
196 kvfree(prog->aux->jited_linfo);
197 prog->aux->jited_linfo = NULL;
198 }
199
200 kfree(prog->aux->kfunc_tab);
201 prog->aux->kfunc_tab = NULL;
202 }
203
204 /* The jit engine is responsible to provide an array
205 * for insn_off to the jited_off mapping (insn_to_jit_off).
206 *
207 * The idx to this array is the insn_off. Hence, the insn_off
208 * here is relative to the prog itself instead of the main prog.
209 * This array has one entry for each xlated bpf insn.
210 *
211 * jited_off is the byte off to the end of the jited insn.
212 *
213 * Hence, with
214 * insn_start:
215 * The first bpf insn off of the prog. The insn off
216 * here is relative to the main prog.
217 * e.g. if prog is a subprog, insn_start > 0
218 * linfo_idx:
219 * The prog's idx to prog->aux->linfo and jited_linfo
220 *
221 * jited_linfo[linfo_idx] = prog->bpf_func
222 *
223 * For i > linfo_idx,
224 *
225 * jited_linfo[i] = prog->bpf_func +
226 * insn_to_jit_off[linfo[i].insn_off - insn_start - 1]
227 */
bpf_prog_fill_jited_linfo(struct bpf_prog * prog,const u32 * insn_to_jit_off)228 void bpf_prog_fill_jited_linfo(struct bpf_prog *prog,
229 const u32 *insn_to_jit_off)
230 {
231 u32 linfo_idx, insn_start, insn_end, nr_linfo, i;
232 const struct bpf_line_info *linfo;
233 void **jited_linfo;
234
235 if (!prog->aux->jited_linfo || prog->aux->func_idx > prog->aux->func_cnt)
236 /* Userspace did not provide linfo */
237 return;
238
239 linfo_idx = prog->aux->linfo_idx;
240 linfo = &prog->aux->linfo[linfo_idx];
241 insn_start = linfo[0].insn_off;
242 insn_end = insn_start + prog->len;
243
244 jited_linfo = &prog->aux->jited_linfo[linfo_idx];
245 jited_linfo[0] = prog->bpf_func;
246
247 nr_linfo = prog->aux->nr_linfo - linfo_idx;
248
249 for (i = 1; i < nr_linfo && linfo[i].insn_off < insn_end; i++)
250 /* The verifier ensures that linfo[i].insn_off is
251 * strictly increasing
252 */
253 jited_linfo[i] = prog->bpf_func +
254 insn_to_jit_off[linfo[i].insn_off - insn_start - 1];
255 }
256
bpf_prog_realloc(struct bpf_prog * fp_old,unsigned int size,gfp_t gfp_extra_flags)257 struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
258 gfp_t gfp_extra_flags)
259 {
260 gfp_t gfp_flags = bpf_memcg_flags(GFP_KERNEL | __GFP_ZERO | gfp_extra_flags);
261 struct bpf_prog *fp;
262 u32 pages;
263
264 size = round_up(size, PAGE_SIZE);
265 pages = size / PAGE_SIZE;
266 if (pages <= fp_old->pages)
267 return fp_old;
268
269 fp = __vmalloc(size, gfp_flags);
270 if (fp) {
271 memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
272 fp->pages = pages;
273 fp->aux->prog = fp;
274
275 /* We keep fp->aux from fp_old around in the new
276 * reallocated structure.
277 */
278 fp_old->aux = NULL;
279 fp_old->stats = NULL;
280 fp_old->active = NULL;
281 __bpf_prog_free(fp_old);
282 }
283
284 return fp;
285 }
286
__bpf_prog_free(struct bpf_prog * fp)287 void __bpf_prog_free(struct bpf_prog *fp)
288 {
289 if (fp->aux) {
290 mutex_destroy(&fp->aux->used_maps_mutex);
291 mutex_destroy(&fp->aux->dst_mutex);
292 mutex_destroy(&fp->aux->st_ops_assoc_mutex);
293 kfree(fp->aux->poke_tab);
294 kfree(fp->aux);
295 }
296 free_percpu(fp->stats);
297 free_percpu(fp->active);
298 vfree(fp);
299 }
300
bpf_prog_calc_tag(struct bpf_prog * fp)301 int bpf_prog_calc_tag(struct bpf_prog *fp)
302 {
303 size_t size = bpf_prog_insn_size(fp);
304 struct bpf_insn *dst;
305 bool was_ld_map;
306 u32 i;
307
308 dst = vmalloc(size);
309 if (!dst)
310 return -ENOMEM;
311
312 /* We need to take out the map fd for the digest calculation
313 * since they are unstable from user space side.
314 */
315 for (i = 0, was_ld_map = false; i < fp->len; i++) {
316 dst[i] = fp->insnsi[i];
317 if (!was_ld_map &&
318 dst[i].code == (BPF_LD | BPF_IMM | BPF_DW) &&
319 (dst[i].src_reg == BPF_PSEUDO_MAP_FD ||
320 dst[i].src_reg == BPF_PSEUDO_MAP_VALUE)) {
321 was_ld_map = true;
322 dst[i].imm = 0;
323 } else if (was_ld_map &&
324 dst[i].code == 0 &&
325 dst[i].dst_reg == 0 &&
326 dst[i].src_reg == 0 &&
327 dst[i].off == 0) {
328 was_ld_map = false;
329 dst[i].imm = 0;
330 } else {
331 was_ld_map = false;
332 }
333 }
334 sha256((u8 *)dst, size, fp->digest);
335 vfree(dst);
336 return 0;
337 }
338
bpf_adj_delta_to_imm(struct bpf_insn * insn,u32 pos,s32 end_old,s32 end_new,s32 curr,const bool probe_pass)339 static int bpf_adj_delta_to_imm(struct bpf_insn *insn, u32 pos, s32 end_old,
340 s32 end_new, s32 curr, const bool probe_pass)
341 {
342 const s64 imm_min = S32_MIN, imm_max = S32_MAX;
343 s32 delta = end_new - end_old;
344 s64 imm = insn->imm;
345
346 if (curr < pos && curr + imm + 1 >= end_old)
347 imm += delta;
348 else if (curr >= end_new && curr + imm + 1 < end_new)
349 imm -= delta;
350 if (imm < imm_min || imm > imm_max)
351 return -ERANGE;
352 if (!probe_pass)
353 insn->imm = imm;
354 return 0;
355 }
356
bpf_adj_delta_to_off(struct bpf_insn * insn,u32 pos,s32 end_old,s32 end_new,s32 curr,const bool probe_pass)357 static int bpf_adj_delta_to_off(struct bpf_insn *insn, u32 pos, s32 end_old,
358 s32 end_new, s32 curr, const bool probe_pass)
359 {
360 s64 off_min, off_max, off;
361 s32 delta = end_new - end_old;
362
363 if (insn->code == (BPF_JMP32 | BPF_JA)) {
364 off = insn->imm;
365 off_min = S32_MIN;
366 off_max = S32_MAX;
367 } else {
368 off = insn->off;
369 off_min = S16_MIN;
370 off_max = S16_MAX;
371 }
372
373 if (curr < pos && curr + off + 1 >= end_old)
374 off += delta;
375 else if (curr >= end_new && curr + off + 1 < end_new)
376 off -= delta;
377 if (off < off_min || off > off_max)
378 return -ERANGE;
379 if (!probe_pass) {
380 if (insn->code == (BPF_JMP32 | BPF_JA))
381 insn->imm = off;
382 else
383 insn->off = off;
384 }
385 return 0;
386 }
387
bpf_adj_branches(struct bpf_prog * prog,u32 pos,s32 end_old,s32 end_new,const bool probe_pass)388 static int bpf_adj_branches(struct bpf_prog *prog, u32 pos, s32 end_old,
389 s32 end_new, const bool probe_pass)
390 {
391 u32 i, insn_cnt = prog->len + (probe_pass ? end_new - end_old : 0);
392 struct bpf_insn *insn = prog->insnsi;
393 int ret = 0;
394
395 for (i = 0; i < insn_cnt; i++, insn++) {
396 u8 code;
397
398 /* In the probing pass we still operate on the original,
399 * unpatched image in order to check overflows before we
400 * do any other adjustments. Therefore skip the patchlet.
401 */
402 if (probe_pass && i == pos) {
403 i = end_new;
404 insn = prog->insnsi + end_old;
405 }
406 if (bpf_pseudo_func(insn)) {
407 ret = bpf_adj_delta_to_imm(insn, pos, end_old,
408 end_new, i, probe_pass);
409 if (ret)
410 return ret;
411 continue;
412 }
413 code = insn->code;
414 if ((BPF_CLASS(code) != BPF_JMP &&
415 BPF_CLASS(code) != BPF_JMP32) ||
416 BPF_OP(code) == BPF_EXIT)
417 continue;
418 /* Adjust offset of jmps if we cross patch boundaries. */
419 if (BPF_OP(code) == BPF_CALL) {
420 if (insn->src_reg != BPF_PSEUDO_CALL)
421 continue;
422 ret = bpf_adj_delta_to_imm(insn, pos, end_old,
423 end_new, i, probe_pass);
424 } else {
425 ret = bpf_adj_delta_to_off(insn, pos, end_old,
426 end_new, i, probe_pass);
427 }
428 if (ret)
429 break;
430 }
431
432 return ret;
433 }
434
bpf_adj_linfo(struct bpf_prog * prog,u32 off,u32 delta)435 static void bpf_adj_linfo(struct bpf_prog *prog, u32 off, u32 delta)
436 {
437 struct bpf_line_info *linfo;
438 u32 i, nr_linfo;
439
440 nr_linfo = prog->aux->nr_linfo;
441 if (!nr_linfo || !delta)
442 return;
443
444 linfo = prog->aux->linfo;
445
446 for (i = 0; i < nr_linfo; i++)
447 if (off < linfo[i].insn_off)
448 break;
449
450 /* Push all off < linfo[i].insn_off by delta */
451 for (; i < nr_linfo; i++)
452 linfo[i].insn_off += delta;
453 }
454
bpf_patch_insn_single(struct bpf_prog * prog,u32 off,const struct bpf_insn * patch,u32 len)455 struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
456 const struct bpf_insn *patch, u32 len)
457 {
458 u32 insn_adj_cnt, insn_rest, insn_delta = len - 1;
459 const u32 cnt_max = S16_MAX;
460 struct bpf_prog *prog_adj;
461 int err;
462
463 /* Since our patchlet doesn't expand the image, we're done. */
464 if (insn_delta == 0) {
465 memcpy(prog->insnsi + off, patch, sizeof(*patch));
466 return prog;
467 }
468
469 insn_adj_cnt = prog->len + insn_delta;
470
471 /* Reject anything that would potentially let the insn->off
472 * target overflow when we have excessive program expansions.
473 * We need to probe here before we do any reallocation where
474 * we afterwards may not fail anymore.
475 */
476 if (insn_adj_cnt > cnt_max &&
477 (err = bpf_adj_branches(prog, off, off + 1, off + len, true)))
478 return ERR_PTR(err);
479
480 /* Several new instructions need to be inserted. Make room
481 * for them. Likely, there's no need for a new allocation as
482 * last page could have large enough tailroom.
483 */
484 prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),
485 GFP_USER);
486 if (!prog_adj)
487 return ERR_PTR(-ENOMEM);
488
489 prog_adj->len = insn_adj_cnt;
490
491 /* Patching happens in 3 steps:
492 *
493 * 1) Move over tail of insnsi from next instruction onwards,
494 * so we can patch the single target insn with one or more
495 * new ones (patching is always from 1 to n insns, n > 0).
496 * 2) Inject new instructions at the target location.
497 * 3) Adjust branch offsets if necessary.
498 */
499 insn_rest = insn_adj_cnt - off - len;
500
501 memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1,
502 sizeof(*patch) * insn_rest);
503 memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len);
504
505 /* We are guaranteed to not fail at this point, otherwise
506 * the ship has sailed to reverse to the original state. An
507 * overflow cannot happen at this point.
508 */
509 BUG_ON(bpf_adj_branches(prog_adj, off, off + 1, off + len, false));
510
511 bpf_adj_linfo(prog_adj, off, insn_delta);
512
513 return prog_adj;
514 }
515
bpf_remove_insns(struct bpf_prog * prog,u32 off,u32 cnt)516 int bpf_remove_insns(struct bpf_prog *prog, u32 off, u32 cnt)
517 {
518 int err;
519
520 /* Branch offsets can't overflow when program is shrinking, no need
521 * to call bpf_adj_branches(..., true) here
522 */
523 memmove(prog->insnsi + off, prog->insnsi + off + cnt,
524 sizeof(struct bpf_insn) * (prog->len - off - cnt));
525 prog->len -= cnt;
526
527 err = bpf_adj_branches(prog, off, off + cnt, off, false);
528 WARN_ON_ONCE(err);
529 return err;
530 }
531
bpf_prog_kallsyms_del_subprogs(struct bpf_prog * fp)532 static void bpf_prog_kallsyms_del_subprogs(struct bpf_prog *fp)
533 {
534 int i;
535
536 for (i = 0; i < fp->aux->real_func_cnt; i++)
537 bpf_prog_kallsyms_del(fp->aux->func[i]);
538 }
539
bpf_prog_kallsyms_del_all(struct bpf_prog * fp)540 void bpf_prog_kallsyms_del_all(struct bpf_prog *fp)
541 {
542 bpf_prog_kallsyms_del_subprogs(fp);
543 bpf_prog_kallsyms_del(fp);
544 }
545
546 #ifdef CONFIG_BPF_JIT
547 /* All BPF JIT sysctl knobs here. */
548 int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_DEFAULT_ON);
549 int bpf_jit_kallsyms __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_DEFAULT_ON);
550 int bpf_jit_harden __read_mostly;
551 long bpf_jit_limit __read_mostly;
552 long bpf_jit_limit_max __read_mostly;
553
554 static void
bpf_prog_ksym_set_addr(struct bpf_prog * prog)555 bpf_prog_ksym_set_addr(struct bpf_prog *prog)
556 {
557 WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog));
558
559 prog->aux->ksym.start = (unsigned long) prog->bpf_func;
560 prog->aux->ksym.end = prog->aux->ksym.start + prog->jited_len;
561 }
562
563 static void
bpf_prog_ksym_set_name(struct bpf_prog * prog)564 bpf_prog_ksym_set_name(struct bpf_prog *prog)
565 {
566 char *sym = prog->aux->ksym.name;
567 const char *end = sym + KSYM_NAME_LEN;
568 const struct btf_type *type;
569 const char *func_name;
570
571 BUILD_BUG_ON(sizeof("bpf_prog_") +
572 sizeof(prog->tag) * 2 +
573 /* name has been null terminated.
574 * We should need +1 for the '_' preceding
575 * the name. However, the null character
576 * is double counted between the name and the
577 * sizeof("bpf_prog_") above, so we omit
578 * the +1 here.
579 */
580 sizeof(prog->aux->name) > KSYM_NAME_LEN);
581
582 sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_");
583 sym = bin2hex(sym, prog->tag, sizeof(prog->tag));
584
585 /* prog->aux->name will be ignored if full btf name is available */
586 if (prog->aux->func_info_cnt && prog->aux->func_idx < prog->aux->func_info_cnt) {
587 type = btf_type_by_id(prog->aux->btf,
588 prog->aux->func_info[prog->aux->func_idx].type_id);
589 func_name = btf_name_by_offset(prog->aux->btf, type->name_off);
590 snprintf(sym, (size_t)(end - sym), "_%s", func_name);
591 return;
592 }
593
594 if (prog->aux->name[0])
595 snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name);
596 else
597 *sym = 0;
598 }
599
bpf_get_ksym_start(struct latch_tree_node * n)600 static unsigned long bpf_get_ksym_start(struct latch_tree_node *n)
601 {
602 return container_of(n, struct bpf_ksym, tnode)->start;
603 }
604
bpf_tree_less(struct latch_tree_node * a,struct latch_tree_node * b)605 static __always_inline bool bpf_tree_less(struct latch_tree_node *a,
606 struct latch_tree_node *b)
607 {
608 return bpf_get_ksym_start(a) < bpf_get_ksym_start(b);
609 }
610
bpf_tree_comp(void * key,struct latch_tree_node * n)611 static __always_inline int bpf_tree_comp(void *key, struct latch_tree_node *n)
612 {
613 unsigned long val = (unsigned long)key;
614 const struct bpf_ksym *ksym;
615
616 ksym = container_of(n, struct bpf_ksym, tnode);
617
618 if (val < ksym->start)
619 return -1;
620 /* Ensure that we detect return addresses as part of the program, when
621 * the final instruction is a call for a program part of the stack
622 * trace. Therefore, do val > ksym->end instead of val >= ksym->end.
623 */
624 if (val > ksym->end)
625 return 1;
626
627 return 0;
628 }
629
630 static const struct latch_tree_ops bpf_tree_ops = {
631 .less = bpf_tree_less,
632 .comp = bpf_tree_comp,
633 };
634
635 static DEFINE_SPINLOCK(bpf_lock);
636 static LIST_HEAD(bpf_kallsyms);
637 static struct latch_tree_root bpf_tree __cacheline_aligned;
638
bpf_ksym_add(struct bpf_ksym * ksym)639 void bpf_ksym_add(struct bpf_ksym *ksym)
640 {
641 spin_lock_bh(&bpf_lock);
642 WARN_ON_ONCE(!list_empty(&ksym->lnode));
643 list_add_tail_rcu(&ksym->lnode, &bpf_kallsyms);
644 latch_tree_insert(&ksym->tnode, &bpf_tree, &bpf_tree_ops);
645 spin_unlock_bh(&bpf_lock);
646 }
647
__bpf_ksym_del(struct bpf_ksym * ksym)648 static void __bpf_ksym_del(struct bpf_ksym *ksym)
649 {
650 if (list_empty(&ksym->lnode))
651 return;
652
653 latch_tree_erase(&ksym->tnode, &bpf_tree, &bpf_tree_ops);
654 list_del_rcu(&ksym->lnode);
655 }
656
bpf_ksym_del(struct bpf_ksym * ksym)657 void bpf_ksym_del(struct bpf_ksym *ksym)
658 {
659 spin_lock_bh(&bpf_lock);
660 __bpf_ksym_del(ksym);
661 spin_unlock_bh(&bpf_lock);
662 }
663
bpf_prog_kallsyms_candidate(const struct bpf_prog * fp)664 static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp)
665 {
666 return fp->jited && !bpf_prog_was_classic(fp);
667 }
668
bpf_prog_kallsyms_add(struct bpf_prog * fp)669 void bpf_prog_kallsyms_add(struct bpf_prog *fp)
670 {
671 if (!bpf_prog_kallsyms_candidate(fp) ||
672 !bpf_token_capable(fp->aux->token, CAP_BPF))
673 return;
674
675 bpf_prog_ksym_set_addr(fp);
676 bpf_prog_ksym_set_name(fp);
677 fp->aux->ksym.prog = true;
678
679 bpf_ksym_add(&fp->aux->ksym);
680
681 #ifdef CONFIG_FINEIBT
682 /*
683 * When FineIBT, code in the __cfi_foo() symbols can get executed
684 * and hence unwinder needs help.
685 */
686 if (cfi_mode != CFI_FINEIBT)
687 return;
688
689 snprintf(fp->aux->ksym_prefix.name, KSYM_NAME_LEN,
690 "__cfi_%s", fp->aux->ksym.name);
691
692 fp->aux->ksym_prefix.start = (unsigned long) fp->bpf_func - 16;
693 fp->aux->ksym_prefix.end = (unsigned long) fp->bpf_func;
694
695 bpf_ksym_add(&fp->aux->ksym_prefix);
696 #endif
697 }
698
bpf_prog_kallsyms_del(struct bpf_prog * fp)699 void bpf_prog_kallsyms_del(struct bpf_prog *fp)
700 {
701 if (!bpf_prog_kallsyms_candidate(fp))
702 return;
703
704 bpf_ksym_del(&fp->aux->ksym);
705 #ifdef CONFIG_FINEIBT
706 if (cfi_mode != CFI_FINEIBT)
707 return;
708 bpf_ksym_del(&fp->aux->ksym_prefix);
709 #endif
710 }
711
bpf_ksym_find(unsigned long addr)712 static struct bpf_ksym *bpf_ksym_find(unsigned long addr)
713 {
714 struct latch_tree_node *n;
715
716 n = latch_tree_find((void *)addr, &bpf_tree, &bpf_tree_ops);
717 return n ? container_of(n, struct bpf_ksym, tnode) : NULL;
718 }
719
bpf_address_lookup(unsigned long addr,unsigned long * size,unsigned long * off,char * sym)720 int bpf_address_lookup(unsigned long addr, unsigned long *size,
721 unsigned long *off, char *sym)
722 {
723 struct bpf_ksym *ksym;
724 int ret = 0;
725
726 rcu_read_lock();
727 ksym = bpf_ksym_find(addr);
728 if (ksym) {
729 unsigned long symbol_start = ksym->start;
730 unsigned long symbol_end = ksym->end;
731
732 ret = strscpy(sym, ksym->name, KSYM_NAME_LEN);
733
734 if (size)
735 *size = symbol_end - symbol_start;
736 if (off)
737 *off = addr - symbol_start;
738 }
739 rcu_read_unlock();
740
741 return ret;
742 }
743
is_bpf_text_address(unsigned long addr)744 bool is_bpf_text_address(unsigned long addr)
745 {
746 bool ret;
747
748 rcu_read_lock();
749 ret = bpf_ksym_find(addr) != NULL;
750 rcu_read_unlock();
751
752 return ret;
753 }
754
bpf_prog_ksym_find(unsigned long addr)755 struct bpf_prog *bpf_prog_ksym_find(unsigned long addr)
756 {
757 struct bpf_ksym *ksym;
758
759 WARN_ON_ONCE(!rcu_read_lock_held());
760 ksym = bpf_ksym_find(addr);
761
762 return ksym && ksym->prog ?
763 container_of(ksym, struct bpf_prog_aux, ksym)->prog :
764 NULL;
765 }
766
bpf_has_frame_pointer(unsigned long ip)767 bool bpf_has_frame_pointer(unsigned long ip)
768 {
769 struct bpf_ksym *ksym;
770 unsigned long offset;
771
772 guard(rcu)();
773
774 ksym = bpf_ksym_find(ip);
775 if (!ksym || !ksym->fp_start || !ksym->fp_end)
776 return false;
777
778 offset = ip - ksym->start;
779
780 return offset >= ksym->fp_start && offset < ksym->fp_end;
781 }
782
search_bpf_extables(unsigned long addr)783 const struct exception_table_entry *search_bpf_extables(unsigned long addr)
784 {
785 const struct exception_table_entry *e = NULL;
786 struct bpf_prog *prog;
787
788 rcu_read_lock();
789 prog = bpf_prog_ksym_find(addr);
790 if (!prog)
791 goto out;
792 if (!prog->aux->num_exentries)
793 goto out;
794
795 e = search_extable(prog->aux->extable, prog->aux->num_exentries, addr);
796 out:
797 rcu_read_unlock();
798 return e;
799 }
800
bpf_get_kallsym(unsigned int symnum,unsigned long * value,char * type,char * sym)801 int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
802 char *sym)
803 {
804 struct bpf_ksym *ksym;
805 unsigned int it = 0;
806 int ret = -ERANGE;
807
808 if (!bpf_jit_kallsyms_enabled())
809 return ret;
810
811 rcu_read_lock();
812 list_for_each_entry_rcu(ksym, &bpf_kallsyms, lnode) {
813 if (it++ != symnum)
814 continue;
815
816 strscpy(sym, ksym->name, KSYM_NAME_LEN);
817
818 *value = ksym->start;
819 *type = BPF_SYM_ELF_TYPE;
820
821 ret = 0;
822 break;
823 }
824 rcu_read_unlock();
825
826 return ret;
827 }
828
bpf_jit_add_poke_descriptor(struct bpf_prog * prog,struct bpf_jit_poke_descriptor * poke)829 int bpf_jit_add_poke_descriptor(struct bpf_prog *prog,
830 struct bpf_jit_poke_descriptor *poke)
831 {
832 struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab;
833 static const u32 poke_tab_max = 1024;
834 u32 slot = prog->aux->size_poke_tab;
835 u32 size = slot + 1;
836
837 if (size > poke_tab_max)
838 return -ENOSPC;
839 if (poke->tailcall_target || poke->tailcall_target_stable ||
840 poke->tailcall_bypass || poke->adj_off || poke->bypass_addr)
841 return -EINVAL;
842
843 switch (poke->reason) {
844 case BPF_POKE_REASON_TAIL_CALL:
845 if (!poke->tail_call.map)
846 return -EINVAL;
847 break;
848 default:
849 return -EINVAL;
850 }
851
852 tab = krealloc_array(tab, size, sizeof(*poke), GFP_KERNEL);
853 if (!tab)
854 return -ENOMEM;
855
856 memcpy(&tab[slot], poke, sizeof(*poke));
857 prog->aux->size_poke_tab = size;
858 prog->aux->poke_tab = tab;
859
860 return slot;
861 }
862
863 /*
864 * BPF program pack allocator.
865 *
866 * Most BPF programs are pretty small. Allocating a hole page for each
867 * program is sometime a waste. Many small bpf program also adds pressure
868 * to instruction TLB. To solve this issue, we introduce a BPF program pack
869 * allocator. The prog_pack allocator uses HPAGE_PMD_SIZE page (2MB on x86)
870 * to host BPF programs.
871 */
872 #define BPF_PROG_CHUNK_SHIFT 6
873 #define BPF_PROG_CHUNK_SIZE (1 << BPF_PROG_CHUNK_SHIFT)
874 #define BPF_PROG_CHUNK_MASK (~(BPF_PROG_CHUNK_SIZE - 1))
875
876 struct bpf_prog_pack {
877 struct list_head list;
878 void *ptr;
879 unsigned long bitmap[];
880 };
881
bpf_jit_fill_hole_with_zero(void * area,unsigned int size)882 void bpf_jit_fill_hole_with_zero(void *area, unsigned int size)
883 {
884 memset(area, 0, size);
885 }
886
887 #define BPF_PROG_SIZE_TO_NBITS(size) (round_up(size, BPF_PROG_CHUNK_SIZE) / BPF_PROG_CHUNK_SIZE)
888
889 static DEFINE_MUTEX(pack_mutex);
890 static LIST_HEAD(pack_list);
891
892 /* PMD_SIZE is not available in some special config, e.g. ARCH=arm with
893 * CONFIG_MMU=n. Use PAGE_SIZE in these cases.
894 */
895 #ifdef PMD_SIZE
896 /* PMD_SIZE is really big for some archs. It doesn't make sense to
897 * reserve too much memory in one allocation. Hardcode BPF_PROG_PACK_SIZE to
898 * 2MiB * num_possible_nodes(). On most architectures PMD_SIZE will be
899 * greater than or equal to 2MB.
900 */
901 #define BPF_PROG_PACK_SIZE (SZ_2M * num_possible_nodes())
902 #else
903 #define BPF_PROG_PACK_SIZE PAGE_SIZE
904 #endif
905
906 #define BPF_PROG_CHUNK_COUNT (BPF_PROG_PACK_SIZE / BPF_PROG_CHUNK_SIZE)
907
alloc_new_pack(bpf_jit_fill_hole_t bpf_fill_ill_insns)908 static struct bpf_prog_pack *alloc_new_pack(bpf_jit_fill_hole_t bpf_fill_ill_insns)
909 {
910 struct bpf_prog_pack *pack;
911 int err;
912
913 pack = kzalloc_flex(*pack, bitmap, BITS_TO_LONGS(BPF_PROG_CHUNK_COUNT));
914 if (!pack)
915 return NULL;
916 pack->ptr = bpf_jit_alloc_exec(BPF_PROG_PACK_SIZE);
917 if (!pack->ptr)
918 goto out;
919 bpf_fill_ill_insns(pack->ptr, BPF_PROG_PACK_SIZE);
920 bitmap_zero(pack->bitmap, BPF_PROG_PACK_SIZE / BPF_PROG_CHUNK_SIZE);
921
922 set_vm_flush_reset_perms(pack->ptr);
923 err = set_memory_rox((unsigned long)pack->ptr,
924 BPF_PROG_PACK_SIZE / PAGE_SIZE);
925 if (err)
926 goto out;
927 list_add_tail(&pack->list, &pack_list);
928 return pack;
929
930 out:
931 bpf_jit_free_exec(pack->ptr);
932 kfree(pack);
933 return NULL;
934 }
935
bpf_prog_pack_alloc(u32 size,bpf_jit_fill_hole_t bpf_fill_ill_insns)936 void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns)
937 {
938 unsigned int nbits = BPF_PROG_SIZE_TO_NBITS(size);
939 struct bpf_prog_pack *pack;
940 unsigned long pos;
941 void *ptr = NULL;
942
943 mutex_lock(&pack_mutex);
944 if (size > BPF_PROG_PACK_SIZE) {
945 size = round_up(size, PAGE_SIZE);
946 ptr = bpf_jit_alloc_exec(size);
947 if (ptr) {
948 int err;
949
950 bpf_fill_ill_insns(ptr, size);
951 set_vm_flush_reset_perms(ptr);
952 err = set_memory_rox((unsigned long)ptr,
953 size / PAGE_SIZE);
954 if (err) {
955 bpf_jit_free_exec(ptr);
956 ptr = NULL;
957 }
958 }
959 goto out;
960 }
961 list_for_each_entry(pack, &pack_list, list) {
962 pos = bitmap_find_next_zero_area(pack->bitmap, BPF_PROG_CHUNK_COUNT, 0,
963 nbits, 0);
964 if (pos < BPF_PROG_CHUNK_COUNT)
965 goto found_free_area;
966 }
967
968 pack = alloc_new_pack(bpf_fill_ill_insns);
969 if (!pack)
970 goto out;
971
972 pos = 0;
973
974 found_free_area:
975 bitmap_set(pack->bitmap, pos, nbits);
976 ptr = (void *)(pack->ptr) + (pos << BPF_PROG_CHUNK_SHIFT);
977
978 out:
979 mutex_unlock(&pack_mutex);
980 return ptr;
981 }
982
bpf_prog_pack_free(void * ptr,u32 size)983 void bpf_prog_pack_free(void *ptr, u32 size)
984 {
985 struct bpf_prog_pack *pack = NULL, *tmp;
986 unsigned int nbits;
987 unsigned long pos;
988
989 mutex_lock(&pack_mutex);
990 if (size > BPF_PROG_PACK_SIZE) {
991 bpf_jit_free_exec(ptr);
992 goto out;
993 }
994
995 list_for_each_entry(tmp, &pack_list, list) {
996 if (ptr >= tmp->ptr && (tmp->ptr + BPF_PROG_PACK_SIZE) > ptr) {
997 pack = tmp;
998 break;
999 }
1000 }
1001
1002 if (WARN_ONCE(!pack, "bpf_prog_pack bug\n"))
1003 goto out;
1004
1005 nbits = BPF_PROG_SIZE_TO_NBITS(size);
1006 pos = ((unsigned long)ptr - (unsigned long)pack->ptr) >> BPF_PROG_CHUNK_SHIFT;
1007
1008 WARN_ONCE(bpf_arch_text_invalidate(ptr, size),
1009 "bpf_prog_pack bug: missing bpf_arch_text_invalidate?\n");
1010
1011 bitmap_clear(pack->bitmap, pos, nbits);
1012 if (bitmap_find_next_zero_area(pack->bitmap, BPF_PROG_CHUNK_COUNT, 0,
1013 BPF_PROG_CHUNK_COUNT, 0) == 0) {
1014 list_del(&pack->list);
1015 bpf_jit_free_exec(pack->ptr);
1016 kfree(pack);
1017 }
1018 out:
1019 mutex_unlock(&pack_mutex);
1020 }
1021
1022 static atomic_long_t bpf_jit_current;
1023
1024 /* Can be overridden by an arch's JIT compiler if it has a custom,
1025 * dedicated BPF backend memory area, or if neither of the two
1026 * below apply.
1027 */
bpf_jit_alloc_exec_limit(void)1028 u64 __weak bpf_jit_alloc_exec_limit(void)
1029 {
1030 #if defined(MODULES_VADDR)
1031 return MODULES_END - MODULES_VADDR;
1032 #else
1033 return VMALLOC_END - VMALLOC_START;
1034 #endif
1035 }
1036
bpf_jit_charge_init(void)1037 static int __init bpf_jit_charge_init(void)
1038 {
1039 /* Only used as heuristic here to derive limit. */
1040 bpf_jit_limit_max = bpf_jit_alloc_exec_limit();
1041 bpf_jit_limit = min_t(u64, round_up(bpf_jit_limit_max >> 1,
1042 PAGE_SIZE), LONG_MAX);
1043 return 0;
1044 }
1045 pure_initcall(bpf_jit_charge_init);
1046
bpf_jit_charge_modmem(u32 size)1047 int bpf_jit_charge_modmem(u32 size)
1048 {
1049 if (atomic_long_add_return(size, &bpf_jit_current) > READ_ONCE(bpf_jit_limit)) {
1050 if (!bpf_capable()) {
1051 atomic_long_sub(size, &bpf_jit_current);
1052 return -EPERM;
1053 }
1054 }
1055
1056 return 0;
1057 }
1058
bpf_jit_uncharge_modmem(u32 size)1059 void bpf_jit_uncharge_modmem(u32 size)
1060 {
1061 atomic_long_sub(size, &bpf_jit_current);
1062 }
1063
bpf_jit_alloc_exec(unsigned long size)1064 void *__weak bpf_jit_alloc_exec(unsigned long size)
1065 {
1066 return execmem_alloc(EXECMEM_BPF, size);
1067 }
1068
bpf_jit_free_exec(void * addr)1069 void __weak bpf_jit_free_exec(void *addr)
1070 {
1071 execmem_free(addr);
1072 }
1073
1074 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)1075 bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
1076 unsigned int alignment,
1077 bpf_jit_fill_hole_t bpf_fill_ill_insns)
1078 {
1079 struct bpf_binary_header *hdr;
1080 u32 size, hole, start;
1081
1082 WARN_ON_ONCE(!is_power_of_2(alignment) ||
1083 alignment > BPF_IMAGE_ALIGNMENT);
1084
1085 /* Most of BPF filters are really small, but if some of them
1086 * fill a page, allow at least 128 extra bytes to insert a
1087 * random section of illegal instructions.
1088 */
1089 size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
1090
1091 if (bpf_jit_charge_modmem(size))
1092 return NULL;
1093 hdr = bpf_jit_alloc_exec(size);
1094 if (!hdr) {
1095 bpf_jit_uncharge_modmem(size);
1096 return NULL;
1097 }
1098
1099 /* Fill space with illegal/arch-dep instructions. */
1100 bpf_fill_ill_insns(hdr, size);
1101
1102 hdr->size = size;
1103 hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
1104 PAGE_SIZE - sizeof(*hdr));
1105 start = get_random_u32_below(hole) & ~(alignment - 1);
1106
1107 /* Leave a random number of instructions before BPF code. */
1108 *image_ptr = &hdr->image[start];
1109
1110 return hdr;
1111 }
1112
bpf_jit_binary_free(struct bpf_binary_header * hdr)1113 void bpf_jit_binary_free(struct bpf_binary_header *hdr)
1114 {
1115 u32 size = hdr->size;
1116
1117 bpf_jit_free_exec(hdr);
1118 bpf_jit_uncharge_modmem(size);
1119 }
1120
1121 /* Allocate jit binary from bpf_prog_pack allocator.
1122 * Since the allocated memory is RO+X, the JIT engine cannot write directly
1123 * to the memory. To solve this problem, a RW buffer is also allocated at
1124 * as the same time. The JIT engine should calculate offsets based on the
1125 * RO memory address, but write JITed program to the RW buffer. Once the
1126 * JIT engine finishes, it calls bpf_jit_binary_pack_finalize, which copies
1127 * the JITed program to the RO memory.
1128 */
1129 struct bpf_binary_header *
bpf_jit_binary_pack_alloc(unsigned int proglen,u8 ** image_ptr,unsigned int alignment,struct bpf_binary_header ** rw_header,u8 ** rw_image,bpf_jit_fill_hole_t bpf_fill_ill_insns)1130 bpf_jit_binary_pack_alloc(unsigned int proglen, u8 **image_ptr,
1131 unsigned int alignment,
1132 struct bpf_binary_header **rw_header,
1133 u8 **rw_image,
1134 bpf_jit_fill_hole_t bpf_fill_ill_insns)
1135 {
1136 struct bpf_binary_header *ro_header;
1137 u32 size, hole, start;
1138
1139 WARN_ON_ONCE(!is_power_of_2(alignment) ||
1140 alignment > BPF_IMAGE_ALIGNMENT);
1141
1142 /* add 16 bytes for a random section of illegal instructions */
1143 size = round_up(proglen + sizeof(*ro_header) + 16, BPF_PROG_CHUNK_SIZE);
1144
1145 if (bpf_jit_charge_modmem(size))
1146 return NULL;
1147 ro_header = bpf_prog_pack_alloc(size, bpf_fill_ill_insns);
1148 if (!ro_header) {
1149 bpf_jit_uncharge_modmem(size);
1150 return NULL;
1151 }
1152
1153 *rw_header = kvmalloc(size, GFP_KERNEL);
1154 if (!*rw_header) {
1155 bpf_prog_pack_free(ro_header, size);
1156 bpf_jit_uncharge_modmem(size);
1157 return NULL;
1158 }
1159
1160 /* Fill space with illegal/arch-dep instructions. */
1161 bpf_fill_ill_insns(*rw_header, size);
1162 (*rw_header)->size = size;
1163
1164 hole = min_t(unsigned int, size - (proglen + sizeof(*ro_header)),
1165 BPF_PROG_CHUNK_SIZE - sizeof(*ro_header));
1166 start = get_random_u32_below(hole) & ~(alignment - 1);
1167
1168 *image_ptr = &ro_header->image[start];
1169 *rw_image = &(*rw_header)->image[start];
1170
1171 return ro_header;
1172 }
1173
1174 /* Copy JITed text from rw_header to its final location, the ro_header. */
bpf_jit_binary_pack_finalize(struct bpf_binary_header * ro_header,struct bpf_binary_header * rw_header)1175 int bpf_jit_binary_pack_finalize(struct bpf_binary_header *ro_header,
1176 struct bpf_binary_header *rw_header)
1177 {
1178 void *ptr;
1179
1180 ptr = bpf_arch_text_copy(ro_header, rw_header, rw_header->size);
1181
1182 kvfree(rw_header);
1183
1184 if (IS_ERR(ptr)) {
1185 bpf_prog_pack_free(ro_header, ro_header->size);
1186 return PTR_ERR(ptr);
1187 }
1188 return 0;
1189 }
1190
1191 /* bpf_jit_binary_pack_free is called in two different scenarios:
1192 * 1) when the program is freed after;
1193 * 2) when the JIT engine fails (before bpf_jit_binary_pack_finalize).
1194 * For case 2), we need to free both the RO memory and the RW buffer.
1195 *
1196 * bpf_jit_binary_pack_free requires proper ro_header->size. However,
1197 * bpf_jit_binary_pack_alloc does not set it. Therefore, ro_header->size
1198 * must be set with either bpf_jit_binary_pack_finalize (normal path) or
1199 * bpf_arch_text_copy (when jit fails).
1200 */
bpf_jit_binary_pack_free(struct bpf_binary_header * ro_header,struct bpf_binary_header * rw_header)1201 void bpf_jit_binary_pack_free(struct bpf_binary_header *ro_header,
1202 struct bpf_binary_header *rw_header)
1203 {
1204 u32 size = ro_header->size;
1205
1206 bpf_prog_pack_free(ro_header, size);
1207 kvfree(rw_header);
1208 bpf_jit_uncharge_modmem(size);
1209 }
1210
1211 struct bpf_binary_header *
bpf_jit_binary_pack_hdr(const struct bpf_prog * fp)1212 bpf_jit_binary_pack_hdr(const struct bpf_prog *fp)
1213 {
1214 unsigned long real_start = (unsigned long)fp->bpf_func;
1215 unsigned long addr;
1216
1217 addr = real_start & BPF_PROG_CHUNK_MASK;
1218 return (void *)addr;
1219 }
1220
1221 static inline struct bpf_binary_header *
bpf_jit_binary_hdr(const struct bpf_prog * fp)1222 bpf_jit_binary_hdr(const struct bpf_prog *fp)
1223 {
1224 unsigned long real_start = (unsigned long)fp->bpf_func;
1225 unsigned long addr;
1226
1227 addr = real_start & PAGE_MASK;
1228 return (void *)addr;
1229 }
1230
1231 /* This symbol is only overridden by archs that have different
1232 * requirements than the usual eBPF JITs, f.e. when they only
1233 * implement cBPF JIT, do not set images read-only, etc.
1234 */
bpf_jit_free(struct bpf_prog * fp)1235 void __weak bpf_jit_free(struct bpf_prog *fp)
1236 {
1237 if (fp->jited) {
1238 struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp);
1239
1240 bpf_jit_binary_free(hdr);
1241 WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp));
1242 }
1243
1244 bpf_prog_unlock_free(fp);
1245 }
1246
bpf_jit_get_func_addr(const struct bpf_prog * prog,const struct bpf_insn * insn,bool extra_pass,u64 * func_addr,bool * func_addr_fixed)1247 int bpf_jit_get_func_addr(const struct bpf_prog *prog,
1248 const struct bpf_insn *insn, bool extra_pass,
1249 u64 *func_addr, bool *func_addr_fixed)
1250 {
1251 s16 off = insn->off;
1252 s32 imm = insn->imm;
1253 u8 *addr;
1254 int err;
1255
1256 *func_addr_fixed = insn->src_reg != BPF_PSEUDO_CALL;
1257 if (!*func_addr_fixed) {
1258 /* Place-holder address till the last pass has collected
1259 * all addresses for JITed subprograms in which case we
1260 * can pick them up from prog->aux.
1261 */
1262 if (!extra_pass)
1263 addr = NULL;
1264 else if (prog->aux->func &&
1265 off >= 0 && off < prog->aux->real_func_cnt)
1266 addr = (u8 *)prog->aux->func[off]->bpf_func;
1267 else
1268 return -EINVAL;
1269 } else if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL &&
1270 bpf_jit_supports_far_kfunc_call()) {
1271 err = bpf_get_kfunc_addr(prog, insn->imm, insn->off, &addr);
1272 if (err)
1273 return err;
1274 } else {
1275 /* Address of a BPF helper call. Since part of the core
1276 * kernel, it's always at a fixed location. __bpf_call_base
1277 * and the helper with imm relative to it are both in core
1278 * kernel.
1279 */
1280 addr = (u8 *)__bpf_call_base + imm;
1281 }
1282
1283 *func_addr = (unsigned long)addr;
1284 return 0;
1285 }
1286
bpf_jit_get_prog_name(struct bpf_prog * prog)1287 const char *bpf_jit_get_prog_name(struct bpf_prog *prog)
1288 {
1289 if (prog->aux->ksym.prog)
1290 return prog->aux->ksym.name;
1291 return prog->aux->name;
1292 }
1293
bpf_jit_blind_insn(const struct bpf_insn * from,const struct bpf_insn * aux,struct bpf_insn * to_buff,bool emit_zext)1294 static int bpf_jit_blind_insn(const struct bpf_insn *from,
1295 const struct bpf_insn *aux,
1296 struct bpf_insn *to_buff,
1297 bool emit_zext)
1298 {
1299 struct bpf_insn *to = to_buff;
1300 u32 imm_rnd = get_random_u32();
1301 s16 off;
1302
1303 BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG);
1304 BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG);
1305
1306 /* Constraints on AX register:
1307 *
1308 * AX register is inaccessible from user space. It is mapped in
1309 * all JITs, and used here for constant blinding rewrites. It is
1310 * typically "stateless" meaning its contents are only valid within
1311 * the executed instruction, but not across several instructions.
1312 * There are a few exceptions however which are further detailed
1313 * below.
1314 *
1315 * Constant blinding is only used by JITs, not in the interpreter.
1316 * The interpreter uses AX in some occasions as a local temporary
1317 * register e.g. in DIV or MOD instructions.
1318 *
1319 * In restricted circumstances, the verifier can also use the AX
1320 * register for rewrites as long as they do not interfere with
1321 * the above cases!
1322 */
1323 if (from->dst_reg == BPF_REG_AX || from->src_reg == BPF_REG_AX)
1324 goto out;
1325
1326 if (from->imm == 0 &&
1327 (from->code == (BPF_ALU | BPF_MOV | BPF_K) ||
1328 from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) {
1329 *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg);
1330 goto out;
1331 }
1332
1333 switch (from->code) {
1334 case BPF_ALU | BPF_ADD | BPF_K:
1335 case BPF_ALU | BPF_SUB | BPF_K:
1336 case BPF_ALU | BPF_AND | BPF_K:
1337 case BPF_ALU | BPF_OR | BPF_K:
1338 case BPF_ALU | BPF_XOR | BPF_K:
1339 case BPF_ALU | BPF_MUL | BPF_K:
1340 case BPF_ALU | BPF_MOV | BPF_K:
1341 case BPF_ALU | BPF_DIV | BPF_K:
1342 case BPF_ALU | BPF_MOD | BPF_K:
1343 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
1344 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
1345 *to++ = BPF_ALU32_REG_OFF(from->code, from->dst_reg, BPF_REG_AX, from->off);
1346 break;
1347
1348 case BPF_ALU64 | BPF_ADD | BPF_K:
1349 case BPF_ALU64 | BPF_SUB | BPF_K:
1350 case BPF_ALU64 | BPF_AND | BPF_K:
1351 case BPF_ALU64 | BPF_OR | BPF_K:
1352 case BPF_ALU64 | BPF_XOR | BPF_K:
1353 case BPF_ALU64 | BPF_MUL | BPF_K:
1354 case BPF_ALU64 | BPF_MOV | BPF_K:
1355 case BPF_ALU64 | BPF_DIV | BPF_K:
1356 case BPF_ALU64 | BPF_MOD | BPF_K:
1357 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
1358 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
1359 *to++ = BPF_ALU64_REG_OFF(from->code, from->dst_reg, BPF_REG_AX, from->off);
1360 break;
1361
1362 case BPF_JMP | BPF_JEQ | BPF_K:
1363 case BPF_JMP | BPF_JNE | BPF_K:
1364 case BPF_JMP | BPF_JGT | BPF_K:
1365 case BPF_JMP | BPF_JLT | BPF_K:
1366 case BPF_JMP | BPF_JGE | BPF_K:
1367 case BPF_JMP | BPF_JLE | BPF_K:
1368 case BPF_JMP | BPF_JSGT | BPF_K:
1369 case BPF_JMP | BPF_JSLT | BPF_K:
1370 case BPF_JMP | BPF_JSGE | BPF_K:
1371 case BPF_JMP | BPF_JSLE | BPF_K:
1372 case BPF_JMP | BPF_JSET | BPF_K:
1373 /* Accommodate for extra offset in case of a backjump. */
1374 off = from->off;
1375 if (off < 0)
1376 off -= 2;
1377 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
1378 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
1379 *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off);
1380 break;
1381
1382 case BPF_JMP32 | BPF_JEQ | BPF_K:
1383 case BPF_JMP32 | BPF_JNE | BPF_K:
1384 case BPF_JMP32 | BPF_JGT | BPF_K:
1385 case BPF_JMP32 | BPF_JLT | BPF_K:
1386 case BPF_JMP32 | BPF_JGE | BPF_K:
1387 case BPF_JMP32 | BPF_JLE | BPF_K:
1388 case BPF_JMP32 | BPF_JSGT | BPF_K:
1389 case BPF_JMP32 | BPF_JSLT | BPF_K:
1390 case BPF_JMP32 | BPF_JSGE | BPF_K:
1391 case BPF_JMP32 | BPF_JSLE | BPF_K:
1392 case BPF_JMP32 | BPF_JSET | BPF_K:
1393 /* Accommodate for extra offset in case of a backjump. */
1394 off = from->off;
1395 if (off < 0)
1396 off -= 2;
1397 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
1398 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
1399 *to++ = BPF_JMP32_REG(from->code, from->dst_reg, BPF_REG_AX,
1400 off);
1401 break;
1402
1403 case BPF_LD | BPF_IMM | BPF_DW:
1404 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm);
1405 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
1406 *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
1407 *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX);
1408 break;
1409 case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */
1410 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm);
1411 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
1412 if (emit_zext)
1413 *to++ = BPF_ZEXT_REG(BPF_REG_AX);
1414 *to++ = BPF_ALU64_REG(BPF_OR, aux[0].dst_reg, BPF_REG_AX);
1415 break;
1416
1417 case BPF_ST | BPF_MEM | BPF_DW:
1418 case BPF_ST | BPF_MEM | BPF_W:
1419 case BPF_ST | BPF_MEM | BPF_H:
1420 case BPF_ST | BPF_MEM | BPF_B:
1421 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
1422 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
1423 *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off);
1424 break;
1425 }
1426 out:
1427 return to - to_buff;
1428 }
1429
bpf_prog_clone_create(struct bpf_prog * fp_other,gfp_t gfp_extra_flags)1430 static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other,
1431 gfp_t gfp_extra_flags)
1432 {
1433 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
1434 struct bpf_prog *fp;
1435
1436 fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags);
1437 if (fp != NULL) {
1438 /* aux->prog still points to the fp_other one, so
1439 * when promoting the clone to the real program,
1440 * this still needs to be adapted.
1441 */
1442 memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE);
1443 }
1444
1445 return fp;
1446 }
1447
bpf_prog_clone_free(struct bpf_prog * fp)1448 static void bpf_prog_clone_free(struct bpf_prog *fp)
1449 {
1450 /* aux was stolen by the other clone, so we cannot free
1451 * it from this path! It will be freed eventually by the
1452 * other program on release.
1453 *
1454 * At this point, we don't need a deferred release since
1455 * clone is guaranteed to not be locked.
1456 */
1457 fp->aux = NULL;
1458 fp->stats = NULL;
1459 fp->active = NULL;
1460 __bpf_prog_free(fp);
1461 }
1462
bpf_jit_prog_release_other(struct bpf_prog * fp,struct bpf_prog * fp_other)1463 void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other)
1464 {
1465 /* We have to repoint aux->prog to self, as we don't
1466 * know whether fp here is the clone or the original.
1467 */
1468 fp->aux->prog = fp;
1469 bpf_prog_clone_free(fp_other);
1470 }
1471
adjust_insn_arrays(struct bpf_prog * prog,u32 off,u32 len)1472 static void adjust_insn_arrays(struct bpf_prog *prog, u32 off, u32 len)
1473 {
1474 #ifdef CONFIG_BPF_SYSCALL
1475 struct bpf_map *map;
1476 int i;
1477
1478 if (len <= 1)
1479 return;
1480
1481 for (i = 0; i < prog->aux->used_map_cnt; i++) {
1482 map = prog->aux->used_maps[i];
1483 if (map->map_type == BPF_MAP_TYPE_INSN_ARRAY)
1484 bpf_insn_array_adjust(map, off, len);
1485 }
1486 #endif
1487 }
1488
bpf_jit_blind_constants(struct bpf_prog * prog)1489 struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
1490 {
1491 struct bpf_insn insn_buff[16], aux[2];
1492 struct bpf_prog *clone, *tmp;
1493 int insn_delta, insn_cnt;
1494 struct bpf_insn *insn;
1495 int i, rewritten;
1496
1497 if (!prog->blinding_requested || prog->blinded)
1498 return prog;
1499
1500 clone = bpf_prog_clone_create(prog, GFP_USER);
1501 if (!clone)
1502 return ERR_PTR(-ENOMEM);
1503
1504 insn_cnt = clone->len;
1505 insn = clone->insnsi;
1506
1507 for (i = 0; i < insn_cnt; i++, insn++) {
1508 if (bpf_pseudo_func(insn)) {
1509 /* ld_imm64 with an address of bpf subprog is not
1510 * a user controlled constant. Don't randomize it,
1511 * since it will conflict with jit_subprogs() logic.
1512 */
1513 insn++;
1514 i++;
1515 continue;
1516 }
1517
1518 /* We temporarily need to hold the original ld64 insn
1519 * so that we can still access the first part in the
1520 * second blinding run.
1521 */
1522 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) &&
1523 insn[1].code == 0)
1524 memcpy(aux, insn, sizeof(aux));
1525
1526 rewritten = bpf_jit_blind_insn(insn, aux, insn_buff,
1527 clone->aux->verifier_zext);
1528 if (!rewritten)
1529 continue;
1530
1531 tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);
1532 if (IS_ERR(tmp)) {
1533 /* Patching may have repointed aux->prog during
1534 * realloc from the original one, so we need to
1535 * fix it up here on error.
1536 */
1537 bpf_jit_prog_release_other(prog, clone);
1538 return tmp;
1539 }
1540
1541 clone = tmp;
1542 insn_delta = rewritten - 1;
1543
1544 /* Instructions arrays must be updated using absolute xlated offsets */
1545 adjust_insn_arrays(clone, prog->aux->subprog_start + i, rewritten);
1546
1547 /* Walk new program and skip insns we just inserted. */
1548 insn = clone->insnsi + i + insn_delta;
1549 insn_cnt += insn_delta;
1550 i += insn_delta;
1551 }
1552
1553 clone->blinded = 1;
1554 return clone;
1555 }
1556 #endif /* CONFIG_BPF_JIT */
1557
1558 /* Base function for offset calculation. Needs to go into .text section,
1559 * therefore keeping it non-static as well; will also be used by JITs
1560 * anyway later on, so do not let the compiler omit it. This also needs
1561 * to go into kallsyms for correlation from e.g. bpftool, so naming
1562 * must not change.
1563 */
__bpf_call_base(u64 r1,u64 r2,u64 r3,u64 r4,u64 r5)1564 noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1565 {
1566 return 0;
1567 }
1568 EXPORT_SYMBOL_GPL(__bpf_call_base);
1569
1570 /* All UAPI available opcodes. */
1571 #define BPF_INSN_MAP(INSN_2, INSN_3) \
1572 /* 32 bit ALU operations. */ \
1573 /* Register based. */ \
1574 INSN_3(ALU, ADD, X), \
1575 INSN_3(ALU, SUB, X), \
1576 INSN_3(ALU, AND, X), \
1577 INSN_3(ALU, OR, X), \
1578 INSN_3(ALU, LSH, X), \
1579 INSN_3(ALU, RSH, X), \
1580 INSN_3(ALU, XOR, X), \
1581 INSN_3(ALU, MUL, X), \
1582 INSN_3(ALU, MOV, X), \
1583 INSN_3(ALU, ARSH, X), \
1584 INSN_3(ALU, DIV, X), \
1585 INSN_3(ALU, MOD, X), \
1586 INSN_2(ALU, NEG), \
1587 INSN_3(ALU, END, TO_BE), \
1588 INSN_3(ALU, END, TO_LE), \
1589 /* Immediate based. */ \
1590 INSN_3(ALU, ADD, K), \
1591 INSN_3(ALU, SUB, K), \
1592 INSN_3(ALU, AND, K), \
1593 INSN_3(ALU, OR, K), \
1594 INSN_3(ALU, LSH, K), \
1595 INSN_3(ALU, RSH, K), \
1596 INSN_3(ALU, XOR, K), \
1597 INSN_3(ALU, MUL, K), \
1598 INSN_3(ALU, MOV, K), \
1599 INSN_3(ALU, ARSH, K), \
1600 INSN_3(ALU, DIV, K), \
1601 INSN_3(ALU, MOD, K), \
1602 /* 64 bit ALU operations. */ \
1603 /* Register based. */ \
1604 INSN_3(ALU64, ADD, X), \
1605 INSN_3(ALU64, SUB, X), \
1606 INSN_3(ALU64, AND, X), \
1607 INSN_3(ALU64, OR, X), \
1608 INSN_3(ALU64, LSH, X), \
1609 INSN_3(ALU64, RSH, X), \
1610 INSN_3(ALU64, XOR, X), \
1611 INSN_3(ALU64, MUL, X), \
1612 INSN_3(ALU64, MOV, X), \
1613 INSN_3(ALU64, ARSH, X), \
1614 INSN_3(ALU64, DIV, X), \
1615 INSN_3(ALU64, MOD, X), \
1616 INSN_2(ALU64, NEG), \
1617 INSN_3(ALU64, END, TO_LE), \
1618 /* Immediate based. */ \
1619 INSN_3(ALU64, ADD, K), \
1620 INSN_3(ALU64, SUB, K), \
1621 INSN_3(ALU64, AND, K), \
1622 INSN_3(ALU64, OR, K), \
1623 INSN_3(ALU64, LSH, K), \
1624 INSN_3(ALU64, RSH, K), \
1625 INSN_3(ALU64, XOR, K), \
1626 INSN_3(ALU64, MUL, K), \
1627 INSN_3(ALU64, MOV, K), \
1628 INSN_3(ALU64, ARSH, K), \
1629 INSN_3(ALU64, DIV, K), \
1630 INSN_3(ALU64, MOD, K), \
1631 /* Call instruction. */ \
1632 INSN_2(JMP, CALL), \
1633 /* Exit instruction. */ \
1634 INSN_2(JMP, EXIT), \
1635 /* 32-bit Jump instructions. */ \
1636 /* Register based. */ \
1637 INSN_3(JMP32, JEQ, X), \
1638 INSN_3(JMP32, JNE, X), \
1639 INSN_3(JMP32, JGT, X), \
1640 INSN_3(JMP32, JLT, X), \
1641 INSN_3(JMP32, JGE, X), \
1642 INSN_3(JMP32, JLE, X), \
1643 INSN_3(JMP32, JSGT, X), \
1644 INSN_3(JMP32, JSLT, X), \
1645 INSN_3(JMP32, JSGE, X), \
1646 INSN_3(JMP32, JSLE, X), \
1647 INSN_3(JMP32, JSET, X), \
1648 /* Immediate based. */ \
1649 INSN_3(JMP32, JEQ, K), \
1650 INSN_3(JMP32, JNE, K), \
1651 INSN_3(JMP32, JGT, K), \
1652 INSN_3(JMP32, JLT, K), \
1653 INSN_3(JMP32, JGE, K), \
1654 INSN_3(JMP32, JLE, K), \
1655 INSN_3(JMP32, JSGT, K), \
1656 INSN_3(JMP32, JSLT, K), \
1657 INSN_3(JMP32, JSGE, K), \
1658 INSN_3(JMP32, JSLE, K), \
1659 INSN_3(JMP32, JSET, K), \
1660 /* Jump instructions. */ \
1661 /* Register based. */ \
1662 INSN_3(JMP, JEQ, X), \
1663 INSN_3(JMP, JNE, X), \
1664 INSN_3(JMP, JGT, X), \
1665 INSN_3(JMP, JLT, X), \
1666 INSN_3(JMP, JGE, X), \
1667 INSN_3(JMP, JLE, X), \
1668 INSN_3(JMP, JSGT, X), \
1669 INSN_3(JMP, JSLT, X), \
1670 INSN_3(JMP, JSGE, X), \
1671 INSN_3(JMP, JSLE, X), \
1672 INSN_3(JMP, JSET, X), \
1673 /* Immediate based. */ \
1674 INSN_3(JMP, JEQ, K), \
1675 INSN_3(JMP, JNE, K), \
1676 INSN_3(JMP, JGT, K), \
1677 INSN_3(JMP, JLT, K), \
1678 INSN_3(JMP, JGE, K), \
1679 INSN_3(JMP, JLE, K), \
1680 INSN_3(JMP, JSGT, K), \
1681 INSN_3(JMP, JSLT, K), \
1682 INSN_3(JMP, JSGE, K), \
1683 INSN_3(JMP, JSLE, K), \
1684 INSN_3(JMP, JSET, K), \
1685 INSN_2(JMP, JA), \
1686 INSN_2(JMP32, JA), \
1687 /* Atomic operations. */ \
1688 INSN_3(STX, ATOMIC, B), \
1689 INSN_3(STX, ATOMIC, H), \
1690 INSN_3(STX, ATOMIC, W), \
1691 INSN_3(STX, ATOMIC, DW), \
1692 /* Store instructions. */ \
1693 /* Register based. */ \
1694 INSN_3(STX, MEM, B), \
1695 INSN_3(STX, MEM, H), \
1696 INSN_3(STX, MEM, W), \
1697 INSN_3(STX, MEM, DW), \
1698 /* Immediate based. */ \
1699 INSN_3(ST, MEM, B), \
1700 INSN_3(ST, MEM, H), \
1701 INSN_3(ST, MEM, W), \
1702 INSN_3(ST, MEM, DW), \
1703 /* Load instructions. */ \
1704 /* Register based. */ \
1705 INSN_3(LDX, MEM, B), \
1706 INSN_3(LDX, MEM, H), \
1707 INSN_3(LDX, MEM, W), \
1708 INSN_3(LDX, MEM, DW), \
1709 INSN_3(LDX, MEMSX, B), \
1710 INSN_3(LDX, MEMSX, H), \
1711 INSN_3(LDX, MEMSX, W), \
1712 /* Immediate based. */ \
1713 INSN_3(LD, IMM, DW)
1714
bpf_opcode_in_insntable(u8 code)1715 bool bpf_opcode_in_insntable(u8 code)
1716 {
1717 #define BPF_INSN_2_TBL(x, y) [BPF_##x | BPF_##y] = true
1718 #define BPF_INSN_3_TBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = true
1719 static const bool public_insntable[256] = {
1720 [0 ... 255] = false,
1721 /* Now overwrite non-defaults ... */
1722 BPF_INSN_MAP(BPF_INSN_2_TBL, BPF_INSN_3_TBL),
1723 /* UAPI exposed, but rewritten opcodes. cBPF carry-over. */
1724 [BPF_LD | BPF_ABS | BPF_B] = true,
1725 [BPF_LD | BPF_ABS | BPF_H] = true,
1726 [BPF_LD | BPF_ABS | BPF_W] = true,
1727 [BPF_LD | BPF_IND | BPF_B] = true,
1728 [BPF_LD | BPF_IND | BPF_H] = true,
1729 [BPF_LD | BPF_IND | BPF_W] = true,
1730 [BPF_JMP | BPF_JA | BPF_X] = true,
1731 [BPF_JMP | BPF_JCOND] = true,
1732 };
1733 #undef BPF_INSN_3_TBL
1734 #undef BPF_INSN_2_TBL
1735 return public_insntable[code];
1736 }
1737
1738 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
1739 /**
1740 * ___bpf_prog_run - run eBPF program on a given context
1741 * @regs: is the array of MAX_BPF_EXT_REG eBPF pseudo-registers
1742 * @insn: is the array of eBPF instructions
1743 *
1744 * Decode and execute eBPF instructions.
1745 *
1746 * Return: whatever value is in %BPF_R0 at program exit
1747 */
___bpf_prog_run(u64 * regs,const struct bpf_insn * insn)1748 static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn)
1749 {
1750 #define BPF_INSN_2_LBL(x, y) [BPF_##x | BPF_##y] = &&x##_##y
1751 #define BPF_INSN_3_LBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = &&x##_##y##_##z
1752 static const void * const jumptable[256] __annotate_jump_table = {
1753 [0 ... 255] = &&default_label,
1754 /* Now overwrite non-defaults ... */
1755 BPF_INSN_MAP(BPF_INSN_2_LBL, BPF_INSN_3_LBL),
1756 /* Non-UAPI available opcodes. */
1757 [BPF_JMP | BPF_CALL_ARGS] = &&JMP_CALL_ARGS,
1758 [BPF_JMP | BPF_TAIL_CALL] = &&JMP_TAIL_CALL,
1759 [BPF_ST | BPF_NOSPEC] = &&ST_NOSPEC,
1760 [BPF_LDX | BPF_PROBE_MEM | BPF_B] = &&LDX_PROBE_MEM_B,
1761 [BPF_LDX | BPF_PROBE_MEM | BPF_H] = &&LDX_PROBE_MEM_H,
1762 [BPF_LDX | BPF_PROBE_MEM | BPF_W] = &&LDX_PROBE_MEM_W,
1763 [BPF_LDX | BPF_PROBE_MEM | BPF_DW] = &&LDX_PROBE_MEM_DW,
1764 [BPF_LDX | BPF_PROBE_MEMSX | BPF_B] = &&LDX_PROBE_MEMSX_B,
1765 [BPF_LDX | BPF_PROBE_MEMSX | BPF_H] = &&LDX_PROBE_MEMSX_H,
1766 [BPF_LDX | BPF_PROBE_MEMSX | BPF_W] = &&LDX_PROBE_MEMSX_W,
1767 };
1768 #undef BPF_INSN_3_LBL
1769 #undef BPF_INSN_2_LBL
1770 u32 tail_call_cnt = 0;
1771
1772 #define CONT ({ insn++; goto select_insn; })
1773 #define CONT_JMP ({ insn++; goto select_insn; })
1774
1775 select_insn:
1776 goto *jumptable[insn->code];
1777
1778 /* Explicitly mask the register-based shift amounts with 63 or 31
1779 * to avoid undefined behavior. Normally this won't affect the
1780 * generated code, for example, in case of native 64 bit archs such
1781 * as x86-64 or arm64, the compiler is optimizing the AND away for
1782 * the interpreter. In case of JITs, each of the JIT backends compiles
1783 * the BPF shift operations to machine instructions which produce
1784 * implementation-defined results in such a case; the resulting
1785 * contents of the register may be arbitrary, but program behaviour
1786 * as a whole remains defined. In other words, in case of JIT backends,
1787 * the AND must /not/ be added to the emitted LSH/RSH/ARSH translation.
1788 */
1789 /* ALU (shifts) */
1790 #define SHT(OPCODE, OP) \
1791 ALU64_##OPCODE##_X: \
1792 DST = DST OP (SRC & 63); \
1793 CONT; \
1794 ALU_##OPCODE##_X: \
1795 DST = (u32) DST OP ((u32) SRC & 31); \
1796 CONT; \
1797 ALU64_##OPCODE##_K: \
1798 DST = DST OP IMM; \
1799 CONT; \
1800 ALU_##OPCODE##_K: \
1801 DST = (u32) DST OP (u32) IMM; \
1802 CONT;
1803 /* ALU (rest) */
1804 #define ALU(OPCODE, OP) \
1805 ALU64_##OPCODE##_X: \
1806 DST = DST OP SRC; \
1807 CONT; \
1808 ALU_##OPCODE##_X: \
1809 DST = (u32) DST OP (u32) SRC; \
1810 CONT; \
1811 ALU64_##OPCODE##_K: \
1812 DST = DST OP IMM; \
1813 CONT; \
1814 ALU_##OPCODE##_K: \
1815 DST = (u32) DST OP (u32) IMM; \
1816 CONT;
1817 ALU(ADD, +)
1818 ALU(SUB, -)
1819 ALU(AND, &)
1820 ALU(OR, |)
1821 ALU(XOR, ^)
1822 ALU(MUL, *)
1823 SHT(LSH, <<)
1824 SHT(RSH, >>)
1825 #undef SHT
1826 #undef ALU
1827 ALU_NEG:
1828 DST = (u32) -DST;
1829 CONT;
1830 ALU64_NEG:
1831 DST = -DST;
1832 CONT;
1833 ALU_MOV_X:
1834 switch (OFF) {
1835 case 0:
1836 DST = (u32) SRC;
1837 break;
1838 case 8:
1839 DST = (u32)(s8) SRC;
1840 break;
1841 case 16:
1842 DST = (u32)(s16) SRC;
1843 break;
1844 }
1845 CONT;
1846 ALU_MOV_K:
1847 DST = (u32) IMM;
1848 CONT;
1849 ALU64_MOV_X:
1850 switch (OFF) {
1851 case 0:
1852 DST = SRC;
1853 break;
1854 case 8:
1855 DST = (s8) SRC;
1856 break;
1857 case 16:
1858 DST = (s16) SRC;
1859 break;
1860 case 32:
1861 DST = (s32) SRC;
1862 break;
1863 }
1864 CONT;
1865 ALU64_MOV_K:
1866 DST = IMM;
1867 CONT;
1868 LD_IMM_DW:
1869 DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
1870 insn++;
1871 CONT;
1872 ALU_ARSH_X:
1873 DST = (u64) (u32) (((s32) DST) >> (SRC & 31));
1874 CONT;
1875 ALU_ARSH_K:
1876 DST = (u64) (u32) (((s32) DST) >> IMM);
1877 CONT;
1878 ALU64_ARSH_X:
1879 (*(s64 *) &DST) >>= (SRC & 63);
1880 CONT;
1881 ALU64_ARSH_K:
1882 (*(s64 *) &DST) >>= IMM;
1883 CONT;
1884 ALU64_MOD_X:
1885 switch (OFF) {
1886 case 0:
1887 div64_u64_rem(DST, SRC, &AX);
1888 DST = AX;
1889 break;
1890 case 1:
1891 AX = div64_s64(DST, SRC);
1892 DST = DST - AX * SRC;
1893 break;
1894 }
1895 CONT;
1896 ALU_MOD_X:
1897 switch (OFF) {
1898 case 0:
1899 AX = (u32) DST;
1900 DST = do_div(AX, (u32) SRC);
1901 break;
1902 case 1:
1903 AX = abs((s32)DST);
1904 AX = do_div(AX, abs((s32)SRC));
1905 if ((s32)DST < 0)
1906 DST = (u32)-AX;
1907 else
1908 DST = (u32)AX;
1909 break;
1910 }
1911 CONT;
1912 ALU64_MOD_K:
1913 switch (OFF) {
1914 case 0:
1915 div64_u64_rem(DST, IMM, &AX);
1916 DST = AX;
1917 break;
1918 case 1:
1919 AX = div64_s64(DST, IMM);
1920 DST = DST - AX * IMM;
1921 break;
1922 }
1923 CONT;
1924 ALU_MOD_K:
1925 switch (OFF) {
1926 case 0:
1927 AX = (u32) DST;
1928 DST = do_div(AX, (u32) IMM);
1929 break;
1930 case 1:
1931 AX = abs((s32)DST);
1932 AX = do_div(AX, abs((s32)IMM));
1933 if ((s32)DST < 0)
1934 DST = (u32)-AX;
1935 else
1936 DST = (u32)AX;
1937 break;
1938 }
1939 CONT;
1940 ALU64_DIV_X:
1941 switch (OFF) {
1942 case 0:
1943 DST = div64_u64(DST, SRC);
1944 break;
1945 case 1:
1946 DST = div64_s64(DST, SRC);
1947 break;
1948 }
1949 CONT;
1950 ALU_DIV_X:
1951 switch (OFF) {
1952 case 0:
1953 AX = (u32) DST;
1954 do_div(AX, (u32) SRC);
1955 DST = (u32) AX;
1956 break;
1957 case 1:
1958 AX = abs((s32)DST);
1959 do_div(AX, abs((s32)SRC));
1960 if (((s32)DST < 0) == ((s32)SRC < 0))
1961 DST = (u32)AX;
1962 else
1963 DST = (u32)-AX;
1964 break;
1965 }
1966 CONT;
1967 ALU64_DIV_K:
1968 switch (OFF) {
1969 case 0:
1970 DST = div64_u64(DST, IMM);
1971 break;
1972 case 1:
1973 DST = div64_s64(DST, IMM);
1974 break;
1975 }
1976 CONT;
1977 ALU_DIV_K:
1978 switch (OFF) {
1979 case 0:
1980 AX = (u32) DST;
1981 do_div(AX, (u32) IMM);
1982 DST = (u32) AX;
1983 break;
1984 case 1:
1985 AX = abs((s32)DST);
1986 do_div(AX, abs((s32)IMM));
1987 if (((s32)DST < 0) == ((s32)IMM < 0))
1988 DST = (u32)AX;
1989 else
1990 DST = (u32)-AX;
1991 break;
1992 }
1993 CONT;
1994 ALU_END_TO_BE:
1995 switch (IMM) {
1996 case 16:
1997 DST = (__force u16) cpu_to_be16(DST);
1998 break;
1999 case 32:
2000 DST = (__force u32) cpu_to_be32(DST);
2001 break;
2002 case 64:
2003 DST = (__force u64) cpu_to_be64(DST);
2004 break;
2005 }
2006 CONT;
2007 ALU_END_TO_LE:
2008 switch (IMM) {
2009 case 16:
2010 DST = (__force u16) cpu_to_le16(DST);
2011 break;
2012 case 32:
2013 DST = (__force u32) cpu_to_le32(DST);
2014 break;
2015 case 64:
2016 DST = (__force u64) cpu_to_le64(DST);
2017 break;
2018 }
2019 CONT;
2020 ALU64_END_TO_LE:
2021 switch (IMM) {
2022 case 16:
2023 DST = (__force u16) __swab16(DST);
2024 break;
2025 case 32:
2026 DST = (__force u32) __swab32(DST);
2027 break;
2028 case 64:
2029 DST = (__force u64) __swab64(DST);
2030 break;
2031 }
2032 CONT;
2033
2034 /* CALL */
2035 JMP_CALL:
2036 /* Function call scratches BPF_R1-BPF_R5 registers,
2037 * preserves BPF_R6-BPF_R9, and stores return value
2038 * into BPF_R0.
2039 */
2040 BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
2041 BPF_R4, BPF_R5);
2042 CONT;
2043
2044 JMP_CALL_ARGS:
2045 BPF_R0 = (__bpf_call_base_args + insn->imm)(BPF_R1, BPF_R2,
2046 BPF_R3, BPF_R4,
2047 BPF_R5,
2048 insn + insn->off + 1);
2049 CONT;
2050
2051 JMP_TAIL_CALL: {
2052 struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
2053 struct bpf_array *array = container_of(map, struct bpf_array, map);
2054 struct bpf_prog *prog;
2055 u32 index = BPF_R3;
2056
2057 if (unlikely(index >= array->map.max_entries))
2058 goto out;
2059
2060 if (unlikely(tail_call_cnt >= MAX_TAIL_CALL_CNT))
2061 goto out;
2062
2063 tail_call_cnt++;
2064
2065 prog = READ_ONCE(array->ptrs[index]);
2066 if (!prog)
2067 goto out;
2068
2069 /* ARG1 at this point is guaranteed to point to CTX from
2070 * the verifier side due to the fact that the tail call is
2071 * handled like a helper, that is, bpf_tail_call_proto,
2072 * where arg1_type is ARG_PTR_TO_CTX.
2073 */
2074 insn = prog->insnsi;
2075 goto select_insn;
2076 out:
2077 CONT;
2078 }
2079 JMP_JA:
2080 insn += insn->off;
2081 CONT;
2082 JMP32_JA:
2083 insn += insn->imm;
2084 CONT;
2085 JMP_EXIT:
2086 return BPF_R0;
2087 /* JMP */
2088 #define COND_JMP(SIGN, OPCODE, CMP_OP) \
2089 JMP_##OPCODE##_X: \
2090 if ((SIGN##64) DST CMP_OP (SIGN##64) SRC) { \
2091 insn += insn->off; \
2092 CONT_JMP; \
2093 } \
2094 CONT; \
2095 JMP32_##OPCODE##_X: \
2096 if ((SIGN##32) DST CMP_OP (SIGN##32) SRC) { \
2097 insn += insn->off; \
2098 CONT_JMP; \
2099 } \
2100 CONT; \
2101 JMP_##OPCODE##_K: \
2102 if ((SIGN##64) DST CMP_OP (SIGN##64) IMM) { \
2103 insn += insn->off; \
2104 CONT_JMP; \
2105 } \
2106 CONT; \
2107 JMP32_##OPCODE##_K: \
2108 if ((SIGN##32) DST CMP_OP (SIGN##32) IMM) { \
2109 insn += insn->off; \
2110 CONT_JMP; \
2111 } \
2112 CONT;
2113 COND_JMP(u, JEQ, ==)
2114 COND_JMP(u, JNE, !=)
2115 COND_JMP(u, JGT, >)
2116 COND_JMP(u, JLT, <)
2117 COND_JMP(u, JGE, >=)
2118 COND_JMP(u, JLE, <=)
2119 COND_JMP(u, JSET, &)
2120 COND_JMP(s, JSGT, >)
2121 COND_JMP(s, JSLT, <)
2122 COND_JMP(s, JSGE, >=)
2123 COND_JMP(s, JSLE, <=)
2124 #undef COND_JMP
2125 /* ST, STX and LDX*/
2126 ST_NOSPEC:
2127 /* Speculation barrier for mitigating Speculative Store Bypass,
2128 * Bounds-Check Bypass and Type Confusion. In case of arm64, we
2129 * rely on the firmware mitigation as controlled via the ssbd
2130 * kernel parameter. Whenever the mitigation is enabled, it
2131 * works for all of the kernel code with no need to provide any
2132 * additional instructions here. In case of x86, we use 'lfence'
2133 * insn for mitigation. We reuse preexisting logic from Spectre
2134 * v1 mitigation that happens to produce the required code on
2135 * x86 for v4 as well.
2136 */
2137 barrier_nospec();
2138 CONT;
2139 #define LDST(SIZEOP, SIZE) \
2140 STX_MEM_##SIZEOP: \
2141 *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \
2142 CONT; \
2143 ST_MEM_##SIZEOP: \
2144 *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \
2145 CONT; \
2146 LDX_MEM_##SIZEOP: \
2147 DST = *(SIZE *)(unsigned long) (SRC + insn->off); \
2148 CONT; \
2149 LDX_PROBE_MEM_##SIZEOP: \
2150 bpf_probe_read_kernel_common(&DST, sizeof(SIZE), \
2151 (const void *)(long) (SRC + insn->off)); \
2152 DST = *((SIZE *)&DST); \
2153 CONT;
2154
2155 LDST(B, u8)
2156 LDST(H, u16)
2157 LDST(W, u32)
2158 LDST(DW, u64)
2159 #undef LDST
2160
2161 #define LDSX(SIZEOP, SIZE) \
2162 LDX_MEMSX_##SIZEOP: \
2163 DST = *(SIZE *)(unsigned long) (SRC + insn->off); \
2164 CONT; \
2165 LDX_PROBE_MEMSX_##SIZEOP: \
2166 bpf_probe_read_kernel_common(&DST, sizeof(SIZE), \
2167 (const void *)(long) (SRC + insn->off)); \
2168 DST = *((SIZE *)&DST); \
2169 CONT;
2170
2171 LDSX(B, s8)
2172 LDSX(H, s16)
2173 LDSX(W, s32)
2174 #undef LDSX
2175
2176 #define ATOMIC_ALU_OP(BOP, KOP) \
2177 case BOP: \
2178 if (BPF_SIZE(insn->code) == BPF_W) \
2179 atomic_##KOP((u32) SRC, (atomic_t *)(unsigned long) \
2180 (DST + insn->off)); \
2181 else if (BPF_SIZE(insn->code) == BPF_DW) \
2182 atomic64_##KOP((u64) SRC, (atomic64_t *)(unsigned long) \
2183 (DST + insn->off)); \
2184 else \
2185 goto default_label; \
2186 break; \
2187 case BOP | BPF_FETCH: \
2188 if (BPF_SIZE(insn->code) == BPF_W) \
2189 SRC = (u32) atomic_fetch_##KOP( \
2190 (u32) SRC, \
2191 (atomic_t *)(unsigned long) (DST + insn->off)); \
2192 else if (BPF_SIZE(insn->code) == BPF_DW) \
2193 SRC = (u64) atomic64_fetch_##KOP( \
2194 (u64) SRC, \
2195 (atomic64_t *)(unsigned long) (DST + insn->off)); \
2196 else \
2197 goto default_label; \
2198 break;
2199
2200 STX_ATOMIC_DW:
2201 STX_ATOMIC_W:
2202 STX_ATOMIC_H:
2203 STX_ATOMIC_B:
2204 switch (IMM) {
2205 /* Atomic read-modify-write instructions support only W and DW
2206 * size modifiers.
2207 */
2208 ATOMIC_ALU_OP(BPF_ADD, add)
2209 ATOMIC_ALU_OP(BPF_AND, and)
2210 ATOMIC_ALU_OP(BPF_OR, or)
2211 ATOMIC_ALU_OP(BPF_XOR, xor)
2212 #undef ATOMIC_ALU_OP
2213
2214 case BPF_XCHG:
2215 if (BPF_SIZE(insn->code) == BPF_W)
2216 SRC = (u32) atomic_xchg(
2217 (atomic_t *)(unsigned long) (DST + insn->off),
2218 (u32) SRC);
2219 else if (BPF_SIZE(insn->code) == BPF_DW)
2220 SRC = (u64) atomic64_xchg(
2221 (atomic64_t *)(unsigned long) (DST + insn->off),
2222 (u64) SRC);
2223 else
2224 goto default_label;
2225 break;
2226 case BPF_CMPXCHG:
2227 if (BPF_SIZE(insn->code) == BPF_W)
2228 BPF_R0 = (u32) atomic_cmpxchg(
2229 (atomic_t *)(unsigned long) (DST + insn->off),
2230 (u32) BPF_R0, (u32) SRC);
2231 else if (BPF_SIZE(insn->code) == BPF_DW)
2232 BPF_R0 = (u64) atomic64_cmpxchg(
2233 (atomic64_t *)(unsigned long) (DST + insn->off),
2234 (u64) BPF_R0, (u64) SRC);
2235 else
2236 goto default_label;
2237 break;
2238 /* Atomic load and store instructions support all size
2239 * modifiers.
2240 */
2241 case BPF_LOAD_ACQ:
2242 switch (BPF_SIZE(insn->code)) {
2243 #define LOAD_ACQUIRE(SIZEOP, SIZE) \
2244 case BPF_##SIZEOP: \
2245 DST = (SIZE)smp_load_acquire( \
2246 (SIZE *)(unsigned long)(SRC + insn->off)); \
2247 break;
2248 LOAD_ACQUIRE(B, u8)
2249 LOAD_ACQUIRE(H, u16)
2250 LOAD_ACQUIRE(W, u32)
2251 #ifdef CONFIG_64BIT
2252 LOAD_ACQUIRE(DW, u64)
2253 #endif
2254 #undef LOAD_ACQUIRE
2255 default:
2256 goto default_label;
2257 }
2258 break;
2259 case BPF_STORE_REL:
2260 switch (BPF_SIZE(insn->code)) {
2261 #define STORE_RELEASE(SIZEOP, SIZE) \
2262 case BPF_##SIZEOP: \
2263 smp_store_release( \
2264 (SIZE *)(unsigned long)(DST + insn->off), (SIZE)SRC); \
2265 break;
2266 STORE_RELEASE(B, u8)
2267 STORE_RELEASE(H, u16)
2268 STORE_RELEASE(W, u32)
2269 #ifdef CONFIG_64BIT
2270 STORE_RELEASE(DW, u64)
2271 #endif
2272 #undef STORE_RELEASE
2273 default:
2274 goto default_label;
2275 }
2276 break;
2277
2278 default:
2279 goto default_label;
2280 }
2281 CONT;
2282
2283 default_label:
2284 /* If we ever reach this, we have a bug somewhere. Die hard here
2285 * instead of just returning 0; we could be somewhere in a subprog,
2286 * so execution could continue otherwise which we do /not/ want.
2287 *
2288 * Note, verifier whitelists all opcodes in bpf_opcode_in_insntable().
2289 */
2290 pr_warn("BPF interpreter: unknown opcode %02x (imm: 0x%x)\n",
2291 insn->code, insn->imm);
2292 BUG_ON(1);
2293 return 0;
2294 }
2295
2296 #define PROG_NAME(stack_size) __bpf_prog_run##stack_size
2297 #define DEFINE_BPF_PROG_RUN(stack_size) \
2298 static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \
2299 { \
2300 u64 stack[stack_size / sizeof(u64)]; \
2301 u64 regs[MAX_BPF_EXT_REG] = {}; \
2302 \
2303 kmsan_unpoison_memory(stack, sizeof(stack)); \
2304 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
2305 ARG1 = (u64) (unsigned long) ctx; \
2306 return ___bpf_prog_run(regs, insn); \
2307 }
2308
2309 #define PROG_NAME_ARGS(stack_size) __bpf_prog_run_args##stack_size
2310 #define DEFINE_BPF_PROG_RUN_ARGS(stack_size) \
2311 static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \
2312 const struct bpf_insn *insn) \
2313 { \
2314 u64 stack[stack_size / sizeof(u64)]; \
2315 u64 regs[MAX_BPF_EXT_REG]; \
2316 \
2317 kmsan_unpoison_memory(stack, sizeof(stack)); \
2318 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
2319 BPF_R1 = r1; \
2320 BPF_R2 = r2; \
2321 BPF_R3 = r3; \
2322 BPF_R4 = r4; \
2323 BPF_R5 = r5; \
2324 return ___bpf_prog_run(regs, insn); \
2325 }
2326
2327 #define EVAL1(FN, X) FN(X)
2328 #define EVAL2(FN, X, Y...) FN(X) EVAL1(FN, Y)
2329 #define EVAL3(FN, X, Y...) FN(X) EVAL2(FN, Y)
2330 #define EVAL4(FN, X, Y...) FN(X) EVAL3(FN, Y)
2331 #define EVAL5(FN, X, Y...) FN(X) EVAL4(FN, Y)
2332 #define EVAL6(FN, X, Y...) FN(X) EVAL5(FN, Y)
2333
2334 EVAL6(DEFINE_BPF_PROG_RUN, 32, 64, 96, 128, 160, 192);
2335 EVAL6(DEFINE_BPF_PROG_RUN, 224, 256, 288, 320, 352, 384);
2336 EVAL4(DEFINE_BPF_PROG_RUN, 416, 448, 480, 512);
2337
2338 EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 32, 64, 96, 128, 160, 192);
2339 EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 224, 256, 288, 320, 352, 384);
2340 EVAL4(DEFINE_BPF_PROG_RUN_ARGS, 416, 448, 480, 512);
2341
2342 #define PROG_NAME_LIST(stack_size) PROG_NAME(stack_size),
2343
2344 static unsigned int (*interpreters[])(const void *ctx,
2345 const struct bpf_insn *insn) = {
2346 EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
2347 EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
2348 EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
2349 };
2350 #undef PROG_NAME_LIST
2351 #define PROG_NAME_LIST(stack_size) PROG_NAME_ARGS(stack_size),
2352 static __maybe_unused
2353 u64 (*interpreters_args[])(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5,
2354 const struct bpf_insn *insn) = {
2355 EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
2356 EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
2357 EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
2358 };
2359 #undef PROG_NAME_LIST
2360
2361 #ifdef CONFIG_BPF_SYSCALL
bpf_patch_call_args(struct bpf_insn * insn,u32 stack_depth)2362 void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth)
2363 {
2364 stack_depth = max_t(u32, stack_depth, 1);
2365 insn->off = (s16) insn->imm;
2366 insn->imm = interpreters_args[(round_up(stack_depth, 32) / 32) - 1] -
2367 __bpf_call_base_args;
2368 insn->code = BPF_JMP | BPF_CALL_ARGS;
2369 }
2370 #endif
2371 #endif
2372
__bpf_prog_ret0_warn(const void * ctx,const struct bpf_insn * insn)2373 static unsigned int __bpf_prog_ret0_warn(const void *ctx,
2374 const struct bpf_insn *insn)
2375 {
2376 /* If this handler ever gets executed, then BPF_JIT_ALWAYS_ON
2377 * is not working properly, so warn about it!
2378 */
2379 WARN_ON_ONCE(1);
2380 return 0;
2381 }
2382
__bpf_prog_map_compatible(struct bpf_map * map,const struct bpf_prog * fp)2383 static bool __bpf_prog_map_compatible(struct bpf_map *map,
2384 const struct bpf_prog *fp)
2385 {
2386 enum bpf_prog_type prog_type = resolve_prog_type(fp);
2387 struct bpf_prog_aux *aux = fp->aux;
2388 enum bpf_cgroup_storage_type i;
2389 bool ret = false;
2390 u64 cookie;
2391
2392 if (fp->kprobe_override)
2393 return ret;
2394
2395 spin_lock(&map->owner_lock);
2396 /* There's no owner yet where we could check for compatibility. */
2397 if (!map->owner) {
2398 map->owner = bpf_map_owner_alloc(map);
2399 if (!map->owner)
2400 goto err;
2401 map->owner->type = prog_type;
2402 map->owner->jited = fp->jited;
2403 map->owner->xdp_has_frags = aux->xdp_has_frags;
2404 map->owner->sleepable = fp->sleepable;
2405 map->owner->expected_attach_type = fp->expected_attach_type;
2406 map->owner->attach_func_proto = aux->attach_func_proto;
2407 for_each_cgroup_storage_type(i) {
2408 map->owner->storage_cookie[i] =
2409 aux->cgroup_storage[i] ?
2410 aux->cgroup_storage[i]->cookie : 0;
2411 }
2412 ret = true;
2413 } else {
2414 ret = map->owner->type == prog_type &&
2415 map->owner->jited == fp->jited &&
2416 map->owner->xdp_has_frags == aux->xdp_has_frags &&
2417 map->owner->sleepable == fp->sleepable;
2418 if (ret &&
2419 map->map_type == BPF_MAP_TYPE_PROG_ARRAY &&
2420 map->owner->expected_attach_type != fp->expected_attach_type)
2421 ret = false;
2422 for_each_cgroup_storage_type(i) {
2423 if (!ret)
2424 break;
2425 cookie = aux->cgroup_storage[i] ?
2426 aux->cgroup_storage[i]->cookie : 0;
2427 ret = map->owner->storage_cookie[i] == cookie ||
2428 !cookie;
2429 }
2430 if (ret &&
2431 map->owner->attach_func_proto != aux->attach_func_proto) {
2432 switch (prog_type) {
2433 case BPF_PROG_TYPE_TRACING:
2434 case BPF_PROG_TYPE_LSM:
2435 case BPF_PROG_TYPE_EXT:
2436 case BPF_PROG_TYPE_STRUCT_OPS:
2437 ret = false;
2438 break;
2439 default:
2440 break;
2441 }
2442 }
2443 }
2444 err:
2445 spin_unlock(&map->owner_lock);
2446 return ret;
2447 }
2448
bpf_prog_map_compatible(struct bpf_map * map,const struct bpf_prog * fp)2449 bool bpf_prog_map_compatible(struct bpf_map *map, const struct bpf_prog *fp)
2450 {
2451 /* XDP programs inserted into maps are not guaranteed to run on
2452 * a particular netdev (and can run outside driver context entirely
2453 * in the case of devmap and cpumap). Until device checks
2454 * are implemented, prohibit adding dev-bound programs to program maps.
2455 */
2456 if (bpf_prog_is_dev_bound(fp->aux))
2457 return false;
2458
2459 return __bpf_prog_map_compatible(map, fp);
2460 }
2461
bpf_check_tail_call(const struct bpf_prog * fp)2462 static int bpf_check_tail_call(const struct bpf_prog *fp)
2463 {
2464 struct bpf_prog_aux *aux = fp->aux;
2465 int i, ret = 0;
2466
2467 mutex_lock(&aux->used_maps_mutex);
2468 for (i = 0; i < aux->used_map_cnt; i++) {
2469 struct bpf_map *map = aux->used_maps[i];
2470
2471 if (!map_type_contains_progs(map))
2472 continue;
2473
2474 if (!__bpf_prog_map_compatible(map, fp)) {
2475 ret = -EINVAL;
2476 goto out;
2477 }
2478 }
2479
2480 out:
2481 mutex_unlock(&aux->used_maps_mutex);
2482 return ret;
2483 }
2484
bpf_prog_select_interpreter(struct bpf_prog * fp)2485 static bool bpf_prog_select_interpreter(struct bpf_prog *fp)
2486 {
2487 bool select_interpreter = false;
2488 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
2489 u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1);
2490 u32 idx = (round_up(stack_depth, 32) / 32) - 1;
2491
2492 /* may_goto may cause stack size > 512, leading to idx out-of-bounds.
2493 * But for non-JITed programs, we don't need bpf_func, so no bounds
2494 * check needed.
2495 */
2496 if (idx < ARRAY_SIZE(interpreters)) {
2497 fp->bpf_func = interpreters[idx];
2498 select_interpreter = true;
2499 } else {
2500 fp->bpf_func = __bpf_prog_ret0_warn;
2501 }
2502 #else
2503 fp->bpf_func = __bpf_prog_ret0_warn;
2504 #endif
2505 return select_interpreter;
2506 }
2507
2508 /**
2509 * bpf_prog_select_runtime - select exec runtime for BPF program
2510 * @fp: bpf_prog populated with BPF program
2511 * @err: pointer to error variable
2512 *
2513 * Try to JIT eBPF program, if JIT is not available, use interpreter.
2514 * The BPF program will be executed via bpf_prog_run() function.
2515 *
2516 * Return: the &fp argument along with &err set to 0 for success or
2517 * a negative errno code on failure
2518 */
bpf_prog_select_runtime(struct bpf_prog * fp,int * err)2519 struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
2520 {
2521 /* In case of BPF to BPF calls, verifier did all the prep
2522 * work with regards to JITing, etc.
2523 */
2524 bool jit_needed = false;
2525
2526 if (fp->bpf_func)
2527 goto finalize;
2528
2529 if (IS_ENABLED(CONFIG_BPF_JIT_ALWAYS_ON) ||
2530 bpf_prog_has_kfunc_call(fp))
2531 jit_needed = true;
2532
2533 if (!bpf_prog_select_interpreter(fp))
2534 jit_needed = true;
2535
2536 /* eBPF JITs can rewrite the program in case constant
2537 * blinding is active. However, in case of error during
2538 * blinding, bpf_int_jit_compile() must always return a
2539 * valid program, which in this case would simply not
2540 * be JITed, but falls back to the interpreter.
2541 */
2542 if (!bpf_prog_is_offloaded(fp->aux)) {
2543 *err = bpf_prog_alloc_jited_linfo(fp);
2544 if (*err)
2545 return fp;
2546
2547 fp = bpf_int_jit_compile(fp);
2548 bpf_prog_jit_attempt_done(fp);
2549 if (!fp->jited && jit_needed) {
2550 *err = -ENOTSUPP;
2551 return fp;
2552 }
2553 } else {
2554 *err = bpf_prog_offload_compile(fp);
2555 if (*err)
2556 return fp;
2557 }
2558
2559 finalize:
2560 *err = bpf_prog_lock_ro(fp);
2561 if (*err)
2562 return fp;
2563
2564 /* The tail call compatibility check can only be done at
2565 * this late stage as we need to determine, if we deal
2566 * with JITed or non JITed program concatenations and not
2567 * all eBPF JITs might immediately support all features.
2568 */
2569 *err = bpf_check_tail_call(fp);
2570
2571 return fp;
2572 }
2573 EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
2574
__bpf_prog_ret1(const void * ctx,const struct bpf_insn * insn)2575 static unsigned int __bpf_prog_ret1(const void *ctx,
2576 const struct bpf_insn *insn)
2577 {
2578 return 1;
2579 }
2580
2581 static struct bpf_prog_dummy {
2582 struct bpf_prog prog;
2583 } dummy_bpf_prog = {
2584 .prog = {
2585 .bpf_func = __bpf_prog_ret1,
2586 },
2587 };
2588
2589 struct bpf_empty_prog_array bpf_empty_prog_array = {
2590 .null_prog = NULL,
2591 };
2592 EXPORT_SYMBOL(bpf_empty_prog_array);
2593
bpf_prog_array_alloc(u32 prog_cnt,gfp_t flags)2594 struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags)
2595 {
2596 struct bpf_prog_array *p;
2597
2598 if (prog_cnt)
2599 p = kzalloc_flex(*p, items, prog_cnt + 1, flags);
2600 else
2601 p = &bpf_empty_prog_array.hdr;
2602
2603 return p;
2604 }
2605
bpf_prog_array_free(struct bpf_prog_array * progs)2606 void bpf_prog_array_free(struct bpf_prog_array *progs)
2607 {
2608 if (!progs || progs == &bpf_empty_prog_array.hdr)
2609 return;
2610 kfree_rcu(progs, rcu);
2611 }
2612
__bpf_prog_array_free_sleepable_cb(struct rcu_head * rcu)2613 static void __bpf_prog_array_free_sleepable_cb(struct rcu_head *rcu)
2614 {
2615 struct bpf_prog_array *progs;
2616
2617 /* If RCU Tasks Trace grace period implies RCU grace period, there is
2618 * no need to call kfree_rcu(), just call kfree() directly.
2619 */
2620 progs = container_of(rcu, struct bpf_prog_array, rcu);
2621 if (rcu_trace_implies_rcu_gp())
2622 kfree(progs);
2623 else
2624 kfree_rcu(progs, rcu);
2625 }
2626
bpf_prog_array_free_sleepable(struct bpf_prog_array * progs)2627 void bpf_prog_array_free_sleepable(struct bpf_prog_array *progs)
2628 {
2629 if (!progs || progs == &bpf_empty_prog_array.hdr)
2630 return;
2631 call_rcu_tasks_trace(&progs->rcu, __bpf_prog_array_free_sleepable_cb);
2632 }
2633
bpf_prog_array_length(struct bpf_prog_array * array)2634 int bpf_prog_array_length(struct bpf_prog_array *array)
2635 {
2636 struct bpf_prog_array_item *item;
2637 u32 cnt = 0;
2638
2639 for (item = array->items; item->prog; item++)
2640 if (item->prog != &dummy_bpf_prog.prog)
2641 cnt++;
2642 return cnt;
2643 }
2644
bpf_prog_array_is_empty(struct bpf_prog_array * array)2645 bool bpf_prog_array_is_empty(struct bpf_prog_array *array)
2646 {
2647 struct bpf_prog_array_item *item;
2648
2649 for (item = array->items; item->prog; item++)
2650 if (item->prog != &dummy_bpf_prog.prog)
2651 return false;
2652 return true;
2653 }
2654
bpf_prog_array_copy_core(struct bpf_prog_array * array,u32 * prog_ids,u32 request_cnt)2655 static bool bpf_prog_array_copy_core(struct bpf_prog_array *array,
2656 u32 *prog_ids,
2657 u32 request_cnt)
2658 {
2659 struct bpf_prog_array_item *item;
2660 int i = 0;
2661
2662 for (item = array->items; item->prog; item++) {
2663 if (item->prog == &dummy_bpf_prog.prog)
2664 continue;
2665 prog_ids[i] = item->prog->aux->id;
2666 if (++i == request_cnt) {
2667 item++;
2668 break;
2669 }
2670 }
2671
2672 return !!(item->prog);
2673 }
2674
bpf_prog_array_copy_to_user(struct bpf_prog_array * array,__u32 __user * prog_ids,u32 cnt)2675 int bpf_prog_array_copy_to_user(struct bpf_prog_array *array,
2676 __u32 __user *prog_ids, u32 cnt)
2677 {
2678 unsigned long err = 0;
2679 bool nospc;
2680 u32 *ids;
2681
2682 /* users of this function are doing:
2683 * cnt = bpf_prog_array_length();
2684 * if (cnt > 0)
2685 * bpf_prog_array_copy_to_user(..., cnt);
2686 * so below kcalloc doesn't need extra cnt > 0 check.
2687 */
2688 ids = kcalloc(cnt, sizeof(u32), GFP_USER | __GFP_NOWARN);
2689 if (!ids)
2690 return -ENOMEM;
2691 nospc = bpf_prog_array_copy_core(array, ids, cnt);
2692 err = copy_to_user(prog_ids, ids, cnt * sizeof(u32));
2693 kfree(ids);
2694 if (err)
2695 return -EFAULT;
2696 if (nospc)
2697 return -ENOSPC;
2698 return 0;
2699 }
2700
bpf_prog_array_delete_safe(struct bpf_prog_array * array,struct bpf_prog * old_prog)2701 void bpf_prog_array_delete_safe(struct bpf_prog_array *array,
2702 struct bpf_prog *old_prog)
2703 {
2704 struct bpf_prog_array_item *item;
2705
2706 for (item = array->items; item->prog; item++)
2707 if (item->prog == old_prog) {
2708 WRITE_ONCE(item->prog, &dummy_bpf_prog.prog);
2709 break;
2710 }
2711 }
2712
2713 /**
2714 * bpf_prog_array_delete_safe_at() - Replaces the program at the given
2715 * index into the program array with
2716 * a dummy no-op program.
2717 * @array: a bpf_prog_array
2718 * @index: the index of the program to replace
2719 *
2720 * Skips over dummy programs, by not counting them, when calculating
2721 * the position of the program to replace.
2722 *
2723 * Return:
2724 * * 0 - Success
2725 * * -EINVAL - Invalid index value. Must be a non-negative integer.
2726 * * -ENOENT - Index out of range
2727 */
bpf_prog_array_delete_safe_at(struct bpf_prog_array * array,int index)2728 int bpf_prog_array_delete_safe_at(struct bpf_prog_array *array, int index)
2729 {
2730 return bpf_prog_array_update_at(array, index, &dummy_bpf_prog.prog);
2731 }
2732
2733 /**
2734 * bpf_prog_array_update_at() - Updates the program at the given index
2735 * into the program array.
2736 * @array: a bpf_prog_array
2737 * @index: the index of the program to update
2738 * @prog: the program to insert into the array
2739 *
2740 * Skips over dummy programs, by not counting them, when calculating
2741 * the position of the program to update.
2742 *
2743 * Return:
2744 * * 0 - Success
2745 * * -EINVAL - Invalid index value. Must be a non-negative integer.
2746 * * -ENOENT - Index out of range
2747 */
bpf_prog_array_update_at(struct bpf_prog_array * array,int index,struct bpf_prog * prog)2748 int bpf_prog_array_update_at(struct bpf_prog_array *array, int index,
2749 struct bpf_prog *prog)
2750 {
2751 struct bpf_prog_array_item *item;
2752
2753 if (unlikely(index < 0))
2754 return -EINVAL;
2755
2756 for (item = array->items; item->prog; item++) {
2757 if (item->prog == &dummy_bpf_prog.prog)
2758 continue;
2759 if (!index) {
2760 WRITE_ONCE(item->prog, prog);
2761 return 0;
2762 }
2763 index--;
2764 }
2765 return -ENOENT;
2766 }
2767
bpf_prog_array_copy(struct bpf_prog_array * old_array,struct bpf_prog * exclude_prog,struct bpf_prog * include_prog,u64 bpf_cookie,struct bpf_prog_array ** new_array)2768 int bpf_prog_array_copy(struct bpf_prog_array *old_array,
2769 struct bpf_prog *exclude_prog,
2770 struct bpf_prog *include_prog,
2771 u64 bpf_cookie,
2772 struct bpf_prog_array **new_array)
2773 {
2774 int new_prog_cnt, carry_prog_cnt = 0;
2775 struct bpf_prog_array_item *existing, *new;
2776 struct bpf_prog_array *array;
2777 bool found_exclude = false;
2778
2779 /* Figure out how many existing progs we need to carry over to
2780 * the new array.
2781 */
2782 if (old_array) {
2783 existing = old_array->items;
2784 for (; existing->prog; existing++) {
2785 if (existing->prog == exclude_prog) {
2786 found_exclude = true;
2787 continue;
2788 }
2789 if (existing->prog != &dummy_bpf_prog.prog)
2790 carry_prog_cnt++;
2791 if (existing->prog == include_prog)
2792 return -EEXIST;
2793 }
2794 }
2795
2796 if (exclude_prog && !found_exclude)
2797 return -ENOENT;
2798
2799 /* How many progs (not NULL) will be in the new array? */
2800 new_prog_cnt = carry_prog_cnt;
2801 if (include_prog)
2802 new_prog_cnt += 1;
2803
2804 /* Do we have any prog (not NULL) in the new array? */
2805 if (!new_prog_cnt) {
2806 *new_array = NULL;
2807 return 0;
2808 }
2809
2810 /* +1 as the end of prog_array is marked with NULL */
2811 array = bpf_prog_array_alloc(new_prog_cnt + 1, GFP_KERNEL);
2812 if (!array)
2813 return -ENOMEM;
2814 new = array->items;
2815
2816 /* Fill in the new prog array */
2817 if (carry_prog_cnt) {
2818 existing = old_array->items;
2819 for (; existing->prog; existing++) {
2820 if (existing->prog == exclude_prog ||
2821 existing->prog == &dummy_bpf_prog.prog)
2822 continue;
2823
2824 new->prog = existing->prog;
2825 new->bpf_cookie = existing->bpf_cookie;
2826 new++;
2827 }
2828 }
2829 if (include_prog) {
2830 new->prog = include_prog;
2831 new->bpf_cookie = bpf_cookie;
2832 new++;
2833 }
2834 new->prog = NULL;
2835 *new_array = array;
2836 return 0;
2837 }
2838
bpf_prog_array_copy_info(struct bpf_prog_array * array,u32 * prog_ids,u32 request_cnt,u32 * prog_cnt)2839 int bpf_prog_array_copy_info(struct bpf_prog_array *array,
2840 u32 *prog_ids, u32 request_cnt,
2841 u32 *prog_cnt)
2842 {
2843 u32 cnt = 0;
2844
2845 if (array)
2846 cnt = bpf_prog_array_length(array);
2847
2848 *prog_cnt = cnt;
2849
2850 /* return early if user requested only program count or nothing to copy */
2851 if (!request_cnt || !cnt)
2852 return 0;
2853
2854 /* this function is called under trace/bpf_trace.c: bpf_event_mutex */
2855 return bpf_prog_array_copy_core(array, prog_ids, request_cnt) ? -ENOSPC
2856 : 0;
2857 }
2858
__bpf_free_used_maps(struct bpf_prog_aux * aux,struct bpf_map ** used_maps,u32 len)2859 void __bpf_free_used_maps(struct bpf_prog_aux *aux,
2860 struct bpf_map **used_maps, u32 len)
2861 {
2862 struct bpf_map *map;
2863 bool sleepable;
2864 u32 i;
2865
2866 sleepable = aux->prog->sleepable;
2867 for (i = 0; i < len; i++) {
2868 map = used_maps[i];
2869 if (map->ops->map_poke_untrack)
2870 map->ops->map_poke_untrack(map, aux);
2871 if (sleepable)
2872 atomic64_dec(&map->sleepable_refcnt);
2873 bpf_map_put(map);
2874 }
2875 }
2876
bpf_free_used_maps(struct bpf_prog_aux * aux)2877 static void bpf_free_used_maps(struct bpf_prog_aux *aux)
2878 {
2879 __bpf_free_used_maps(aux, aux->used_maps, aux->used_map_cnt);
2880 kfree(aux->used_maps);
2881 }
2882
__bpf_free_used_btfs(struct btf_mod_pair * used_btfs,u32 len)2883 void __bpf_free_used_btfs(struct btf_mod_pair *used_btfs, u32 len)
2884 {
2885 #ifdef CONFIG_BPF_SYSCALL
2886 struct btf_mod_pair *btf_mod;
2887 u32 i;
2888
2889 for (i = 0; i < len; i++) {
2890 btf_mod = &used_btfs[i];
2891 if (btf_mod->module)
2892 module_put(btf_mod->module);
2893 btf_put(btf_mod->btf);
2894 }
2895 #endif
2896 }
2897
bpf_free_used_btfs(struct bpf_prog_aux * aux)2898 static void bpf_free_used_btfs(struct bpf_prog_aux *aux)
2899 {
2900 __bpf_free_used_btfs(aux->used_btfs, aux->used_btf_cnt);
2901 kfree(aux->used_btfs);
2902 }
2903
bpf_prog_free_deferred(struct work_struct * work)2904 static void bpf_prog_free_deferred(struct work_struct *work)
2905 {
2906 struct bpf_prog_aux *aux;
2907 int i;
2908
2909 aux = container_of(work, struct bpf_prog_aux, work);
2910 #ifdef CONFIG_BPF_SYSCALL
2911 bpf_free_kfunc_btf_tab(aux->kfunc_btf_tab);
2912 bpf_prog_stream_free(aux->prog);
2913 #endif
2914 #ifdef CONFIG_CGROUP_BPF
2915 if (aux->cgroup_atype != CGROUP_BPF_ATTACH_TYPE_INVALID)
2916 bpf_cgroup_atype_put(aux->cgroup_atype);
2917 #endif
2918 bpf_free_used_maps(aux);
2919 bpf_free_used_btfs(aux);
2920 bpf_prog_disassoc_struct_ops(aux->prog);
2921 if (bpf_prog_is_dev_bound(aux))
2922 bpf_prog_dev_bound_destroy(aux->prog);
2923 #ifdef CONFIG_PERF_EVENTS
2924 if (aux->prog->has_callchain_buf)
2925 put_callchain_buffers();
2926 #endif
2927 if (aux->dst_trampoline)
2928 bpf_trampoline_put(aux->dst_trampoline);
2929 for (i = 0; i < aux->real_func_cnt; i++) {
2930 /* We can just unlink the subprog poke descriptor table as
2931 * it was originally linked to the main program and is also
2932 * released along with it.
2933 */
2934 aux->func[i]->aux->poke_tab = NULL;
2935 bpf_jit_free(aux->func[i]);
2936 }
2937 if (aux->real_func_cnt) {
2938 kfree(aux->func);
2939 bpf_prog_unlock_free(aux->prog);
2940 } else {
2941 bpf_jit_free(aux->prog);
2942 }
2943 }
2944
bpf_prog_free(struct bpf_prog * fp)2945 void bpf_prog_free(struct bpf_prog *fp)
2946 {
2947 struct bpf_prog_aux *aux = fp->aux;
2948
2949 if (aux->dst_prog)
2950 bpf_prog_put(aux->dst_prog);
2951 bpf_token_put(aux->token);
2952 INIT_WORK(&aux->work, bpf_prog_free_deferred);
2953 schedule_work(&aux->work);
2954 }
2955 EXPORT_SYMBOL_GPL(bpf_prog_free);
2956
2957 /* RNG for unprivileged user space with separated state from prandom_u32(). */
2958 static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
2959
bpf_user_rnd_init_once(void)2960 void bpf_user_rnd_init_once(void)
2961 {
2962 prandom_init_once(&bpf_user_rnd_state);
2963 }
2964
BPF_CALL_0(bpf_user_rnd_u32)2965 BPF_CALL_0(bpf_user_rnd_u32)
2966 {
2967 /* Should someone ever have the rather unwise idea to use some
2968 * of the registers passed into this function, then note that
2969 * this function is called from native eBPF and classic-to-eBPF
2970 * transformations. Register assignments from both sides are
2971 * different, f.e. classic always sets fn(ctx, A, X) here.
2972 */
2973 struct rnd_state *state;
2974 u32 res;
2975
2976 state = &get_cpu_var(bpf_user_rnd_state);
2977 res = prandom_u32_state(state);
2978 put_cpu_var(bpf_user_rnd_state);
2979
2980 return res;
2981 }
2982
BPF_CALL_0(bpf_get_raw_cpu_id)2983 BPF_CALL_0(bpf_get_raw_cpu_id)
2984 {
2985 return raw_smp_processor_id();
2986 }
2987
2988 /* Weak definitions of helper functions in case we don't have bpf syscall. */
2989 const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
2990 const struct bpf_func_proto bpf_map_update_elem_proto __weak;
2991 const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
2992 const struct bpf_func_proto bpf_map_push_elem_proto __weak;
2993 const struct bpf_func_proto bpf_map_pop_elem_proto __weak;
2994 const struct bpf_func_proto bpf_map_peek_elem_proto __weak;
2995 const struct bpf_func_proto bpf_map_lookup_percpu_elem_proto __weak;
2996 const struct bpf_func_proto bpf_spin_lock_proto __weak;
2997 const struct bpf_func_proto bpf_spin_unlock_proto __weak;
2998 const struct bpf_func_proto bpf_jiffies64_proto __weak;
2999
3000 const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
3001 const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
3002 const struct bpf_func_proto bpf_get_numa_node_id_proto __weak;
3003 const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
3004 const struct bpf_func_proto bpf_ktime_get_boot_ns_proto __weak;
3005 const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto __weak;
3006 const struct bpf_func_proto bpf_ktime_get_tai_ns_proto __weak;
3007
3008 const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
3009 const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
3010 const struct bpf_func_proto bpf_get_current_comm_proto __weak;
3011 const struct bpf_func_proto bpf_get_current_cgroup_id_proto __weak;
3012 const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto __weak;
3013 const struct bpf_func_proto bpf_get_local_storage_proto __weak;
3014 const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto __weak;
3015 const struct bpf_func_proto bpf_snprintf_btf_proto __weak;
3016 const struct bpf_func_proto bpf_seq_printf_btf_proto __weak;
3017 const struct bpf_func_proto bpf_set_retval_proto __weak;
3018 const struct bpf_func_proto bpf_get_retval_proto __weak;
3019
bpf_get_trace_printk_proto(void)3020 const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
3021 {
3022 return NULL;
3023 }
3024
bpf_get_trace_vprintk_proto(void)3025 const struct bpf_func_proto * __weak bpf_get_trace_vprintk_proto(void)
3026 {
3027 return NULL;
3028 }
3029
bpf_get_perf_event_read_value_proto(void)3030 const struct bpf_func_proto * __weak bpf_get_perf_event_read_value_proto(void)
3031 {
3032 return NULL;
3033 }
3034
3035 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)3036 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
3037 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
3038 {
3039 return -ENOTSUPP;
3040 }
3041 EXPORT_SYMBOL_GPL(bpf_event_output);
3042
3043 /* Always built-in helper functions. */
3044 const struct bpf_func_proto bpf_tail_call_proto = {
3045 /* func is unused for tail_call, we set it to pass the
3046 * get_helper_proto check
3047 */
3048 .func = BPF_PTR_POISON,
3049 .gpl_only = false,
3050 .ret_type = RET_VOID,
3051 .arg1_type = ARG_PTR_TO_CTX,
3052 .arg2_type = ARG_CONST_MAP_PTR,
3053 .arg3_type = ARG_ANYTHING,
3054 };
3055
3056 /* Stub for JITs that only support cBPF. eBPF programs are interpreted.
3057 * It is encouraged to implement bpf_int_jit_compile() instead, so that
3058 * eBPF and implicitly also cBPF can get JITed!
3059 */
bpf_int_jit_compile(struct bpf_prog * prog)3060 struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
3061 {
3062 return prog;
3063 }
3064
3065 /* Stub for JITs that support eBPF. All cBPF code gets transformed into
3066 * eBPF by the kernel and is later compiled by bpf_int_jit_compile().
3067 */
bpf_jit_compile(struct bpf_prog * prog)3068 void __weak bpf_jit_compile(struct bpf_prog *prog)
3069 {
3070 }
3071
bpf_helper_changes_pkt_data(enum bpf_func_id func_id)3072 bool __weak bpf_helper_changes_pkt_data(enum bpf_func_id func_id)
3073 {
3074 return false;
3075 }
3076
3077 /* Return TRUE if the JIT backend wants verifier to enable sub-register usage
3078 * analysis code and wants explicit zero extension inserted by verifier.
3079 * Otherwise, return FALSE.
3080 *
3081 * The verifier inserts an explicit zero extension after BPF_CMPXCHGs even if
3082 * you don't override this. JITs that don't want these extra insns can detect
3083 * them using insn_is_zext.
3084 */
bpf_jit_needs_zext(void)3085 bool __weak bpf_jit_needs_zext(void)
3086 {
3087 return false;
3088 }
3089
3090 /* By default, enable the verifier's mitigations against Spectre v1 and v4 for
3091 * all archs. The value returned must not change at runtime as there is
3092 * currently no support for reloading programs that were loaded without
3093 * mitigations.
3094 */
bpf_jit_bypass_spec_v1(void)3095 bool __weak bpf_jit_bypass_spec_v1(void)
3096 {
3097 return false;
3098 }
3099
bpf_jit_bypass_spec_v4(void)3100 bool __weak bpf_jit_bypass_spec_v4(void)
3101 {
3102 return false;
3103 }
3104
3105 /* Return true if the JIT inlines the call to the helper corresponding to
3106 * the imm.
3107 *
3108 * The verifier will not patch the insn->imm for the call to the helper if
3109 * this returns true.
3110 */
bpf_jit_inlines_helper_call(s32 imm)3111 bool __weak bpf_jit_inlines_helper_call(s32 imm)
3112 {
3113 return false;
3114 }
3115
3116 /* Return TRUE if the JIT backend supports mixing bpf2bpf and tailcalls. */
bpf_jit_supports_subprog_tailcalls(void)3117 bool __weak bpf_jit_supports_subprog_tailcalls(void)
3118 {
3119 return false;
3120 }
3121
bpf_jit_supports_percpu_insn(void)3122 bool __weak bpf_jit_supports_percpu_insn(void)
3123 {
3124 return false;
3125 }
3126
bpf_jit_supports_kfunc_call(void)3127 bool __weak bpf_jit_supports_kfunc_call(void)
3128 {
3129 return false;
3130 }
3131
bpf_jit_supports_far_kfunc_call(void)3132 bool __weak bpf_jit_supports_far_kfunc_call(void)
3133 {
3134 return false;
3135 }
3136
bpf_jit_supports_arena(void)3137 bool __weak bpf_jit_supports_arena(void)
3138 {
3139 return false;
3140 }
3141
bpf_jit_supports_insn(struct bpf_insn * insn,bool in_arena)3142 bool __weak bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)
3143 {
3144 return false;
3145 }
3146
bpf_jit_supports_fsession(void)3147 bool __weak bpf_jit_supports_fsession(void)
3148 {
3149 return false;
3150 }
3151
bpf_arch_uaddress_limit(void)3152 u64 __weak bpf_arch_uaddress_limit(void)
3153 {
3154 #if defined(CONFIG_64BIT) && defined(CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE)
3155 return TASK_SIZE;
3156 #else
3157 return 0;
3158 #endif
3159 }
3160
3161 /* Return TRUE if the JIT backend satisfies the following two conditions:
3162 * 1) JIT backend supports atomic_xchg() on pointer-sized words.
3163 * 2) Under the specific arch, the implementation of xchg() is the same
3164 * as atomic_xchg() on pointer-sized words.
3165 */
bpf_jit_supports_ptr_xchg(void)3166 bool __weak bpf_jit_supports_ptr_xchg(void)
3167 {
3168 return false;
3169 }
3170
3171 /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
3172 * skb_copy_bits(), so provide a weak definition of it for NET-less config.
3173 */
skb_copy_bits(const struct sk_buff * skb,int offset,void * to,int len)3174 int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
3175 int len)
3176 {
3177 return -EFAULT;
3178 }
3179
bpf_arch_text_poke(void * ip,enum bpf_text_poke_type old_t,enum bpf_text_poke_type new_t,void * old_addr,void * new_addr)3180 int __weak bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,
3181 enum bpf_text_poke_type new_t, void *old_addr,
3182 void *new_addr)
3183 {
3184 return -ENOTSUPP;
3185 }
3186
bpf_arch_text_copy(void * dst,void * src,size_t len)3187 void * __weak bpf_arch_text_copy(void *dst, void *src, size_t len)
3188 {
3189 return ERR_PTR(-ENOTSUPP);
3190 }
3191
bpf_arch_text_invalidate(void * dst,size_t len)3192 int __weak bpf_arch_text_invalidate(void *dst, size_t len)
3193 {
3194 return -ENOTSUPP;
3195 }
3196
bpf_jit_supports_exceptions(void)3197 bool __weak bpf_jit_supports_exceptions(void)
3198 {
3199 return false;
3200 }
3201
bpf_jit_supports_private_stack(void)3202 bool __weak bpf_jit_supports_private_stack(void)
3203 {
3204 return false;
3205 }
3206
arch_bpf_stack_walk(bool (* consume_fn)(void * cookie,u64 ip,u64 sp,u64 bp),void * cookie)3207 void __weak arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp), void *cookie)
3208 {
3209 }
3210
bpf_jit_supports_timed_may_goto(void)3211 bool __weak bpf_jit_supports_timed_may_goto(void)
3212 {
3213 return false;
3214 }
3215
arch_bpf_timed_may_goto(void)3216 u64 __weak arch_bpf_timed_may_goto(void)
3217 {
3218 return 0;
3219 }
3220
bpf_prog_report_may_goto_violation(void)3221 static noinline void bpf_prog_report_may_goto_violation(void)
3222 {
3223 #ifdef CONFIG_BPF_SYSCALL
3224 struct bpf_stream_stage ss;
3225 struct bpf_prog *prog;
3226
3227 prog = bpf_prog_find_from_stack();
3228 if (!prog)
3229 return;
3230 bpf_stream_stage(ss, prog, BPF_STDERR, ({
3231 bpf_stream_printk(ss, "ERROR: Timeout detected for may_goto instruction\n");
3232 bpf_stream_dump_stack(ss);
3233 }));
3234 #endif
3235 }
3236
bpf_check_timed_may_goto(struct bpf_timed_may_goto * p)3237 u64 bpf_check_timed_may_goto(struct bpf_timed_may_goto *p)
3238 {
3239 u64 time = ktime_get_mono_fast_ns();
3240
3241 /* Populate the timestamp for this stack frame, and refresh count. */
3242 if (!p->timestamp) {
3243 p->timestamp = time;
3244 return BPF_MAX_TIMED_LOOPS;
3245 }
3246 /* Check if we've exhausted our time slice, and zero count. */
3247 if (unlikely(time - p->timestamp >= (NSEC_PER_SEC / 4))) {
3248 bpf_prog_report_may_goto_violation();
3249 return 0;
3250 }
3251 /* Refresh the count for the stack frame. */
3252 return BPF_MAX_TIMED_LOOPS;
3253 }
3254
3255 /* for configs without MMU or 32-bit */
3256 __weak const struct bpf_map_ops arena_map_ops;
bpf_arena_get_user_vm_start(struct bpf_arena * arena)3257 __weak u64 bpf_arena_get_user_vm_start(struct bpf_arena *arena)
3258 {
3259 return 0;
3260 }
bpf_arena_get_kern_vm_start(struct bpf_arena * arena)3261 __weak u64 bpf_arena_get_kern_vm_start(struct bpf_arena *arena)
3262 {
3263 return 0;
3264 }
3265
3266 #ifdef CONFIG_BPF_SYSCALL
bpf_global_ma_init(void)3267 static int __init bpf_global_ma_init(void)
3268 {
3269 int ret;
3270
3271 ret = bpf_mem_alloc_init(&bpf_global_ma, 0, false);
3272 bpf_global_ma_set = !ret;
3273 return ret;
3274 }
3275 late_initcall(bpf_global_ma_init);
3276 #endif
3277
3278 DEFINE_STATIC_KEY_FALSE(bpf_stats_enabled_key);
3279 EXPORT_SYMBOL(bpf_stats_enabled_key);
3280
3281 /* All definitions of tracepoints related to BPF. */
3282 #define CREATE_TRACE_POINTS
3283 #include <linux/bpf_trace.h>
3284
3285 EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception);
3286 EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_bulk_tx);
3287
3288 #ifdef CONFIG_BPF_SYSCALL
3289
bpf_prog_get_file_line(struct bpf_prog * prog,unsigned long ip,const char ** filep,const char ** linep,int * nump)3290 int bpf_prog_get_file_line(struct bpf_prog *prog, unsigned long ip, const char **filep,
3291 const char **linep, int *nump)
3292 {
3293 int idx = -1, insn_start, insn_end, len;
3294 struct bpf_line_info *linfo;
3295 void **jited_linfo;
3296 struct btf *btf;
3297 int nr_linfo;
3298
3299 btf = prog->aux->btf;
3300 linfo = prog->aux->linfo;
3301 jited_linfo = prog->aux->jited_linfo;
3302
3303 if (!btf || !linfo || !jited_linfo)
3304 return -EINVAL;
3305 len = prog->aux->func ? prog->aux->func[prog->aux->func_idx]->len : prog->len;
3306
3307 linfo = &prog->aux->linfo[prog->aux->linfo_idx];
3308 jited_linfo = &prog->aux->jited_linfo[prog->aux->linfo_idx];
3309
3310 insn_start = linfo[0].insn_off;
3311 insn_end = insn_start + len;
3312 nr_linfo = prog->aux->nr_linfo - prog->aux->linfo_idx;
3313
3314 for (int i = 0; i < nr_linfo &&
3315 linfo[i].insn_off >= insn_start && linfo[i].insn_off < insn_end; i++) {
3316 if (jited_linfo[i] >= (void *)ip)
3317 break;
3318 idx = i;
3319 }
3320
3321 if (idx == -1)
3322 return -ENOENT;
3323
3324 /* Get base component of the file path. */
3325 *filep = btf_name_by_offset(btf, linfo[idx].file_name_off);
3326 *filep = kbasename(*filep);
3327 /* Obtain the source line, and strip whitespace in prefix. */
3328 *linep = btf_name_by_offset(btf, linfo[idx].line_off);
3329 while (isspace(**linep))
3330 *linep += 1;
3331 *nump = BPF_LINE_INFO_LINE_NUM(linfo[idx].line_col);
3332 return 0;
3333 }
3334
3335 struct walk_stack_ctx {
3336 struct bpf_prog *prog;
3337 };
3338
find_from_stack_cb(void * cookie,u64 ip,u64 sp,u64 bp)3339 static bool find_from_stack_cb(void *cookie, u64 ip, u64 sp, u64 bp)
3340 {
3341 struct walk_stack_ctx *ctxp = cookie;
3342 struct bpf_prog *prog;
3343
3344 /*
3345 * The RCU read lock is held to safely traverse the latch tree, but we
3346 * don't need its protection when accessing the prog, since it has an
3347 * active stack frame on the current stack trace, and won't disappear.
3348 */
3349 rcu_read_lock();
3350 prog = bpf_prog_ksym_find(ip);
3351 rcu_read_unlock();
3352 if (!prog)
3353 return true;
3354 /* Make sure we return the main prog if we found a subprog */
3355 ctxp->prog = prog->aux->main_prog_aux->prog;
3356 return false;
3357 }
3358
bpf_prog_find_from_stack(void)3359 struct bpf_prog *bpf_prog_find_from_stack(void)
3360 {
3361 struct walk_stack_ctx ctx = {};
3362
3363 arch_bpf_stack_walk(find_from_stack_cb, &ctx);
3364 return ctx.prog;
3365 }
3366
3367 #endif
3368