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 <linux/filter.h>
22 #include <linux/skbuff.h>
23 #include <linux/vmalloc.h>
24 #include <linux/random.h>
25 #include <linux/moduleloader.h>
26 #include <linux/bpf.h>
27 #include <linux/btf.h>
28 #include <linux/objtool.h>
29 #include <linux/rbtree_latch.h>
30 #include <linux/kallsyms.h>
31 #include <linux/rcupdate.h>
32 #include <linux/perf_event.h>
33 #include <linux/extable.h>
34 #include <linux/log2.h>
35 #include <linux/nospec.h>
36
37 #include <asm/barrier.h>
38 #include <asm/unaligned.h>
39
40 /* Registers */
41 #define BPF_R0 regs[BPF_REG_0]
42 #define BPF_R1 regs[BPF_REG_1]
43 #define BPF_R2 regs[BPF_REG_2]
44 #define BPF_R3 regs[BPF_REG_3]
45 #define BPF_R4 regs[BPF_REG_4]
46 #define BPF_R5 regs[BPF_REG_5]
47 #define BPF_R6 regs[BPF_REG_6]
48 #define BPF_R7 regs[BPF_REG_7]
49 #define BPF_R8 regs[BPF_REG_8]
50 #define BPF_R9 regs[BPF_REG_9]
51 #define BPF_R10 regs[BPF_REG_10]
52
53 /* Named registers */
54 #define DST regs[insn->dst_reg]
55 #define SRC regs[insn->src_reg]
56 #define FP regs[BPF_REG_FP]
57 #define AX regs[BPF_REG_AX]
58 #define ARG1 regs[BPF_REG_ARG1]
59 #define CTX regs[BPF_REG_CTX]
60 #define IMM insn->imm
61
62 /* No hurry in this branch
63 *
64 * Exported for the bpf jit load helper.
65 */
bpf_internal_load_pointer_neg_helper(const struct sk_buff * skb,int k,unsigned int size)66 void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
67 {
68 u8 *ptr = NULL;
69
70 if (k >= SKF_NET_OFF) {
71 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
72 } else if (k >= SKF_LL_OFF) {
73 if (unlikely(!skb_mac_header_was_set(skb)))
74 return NULL;
75 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
76 }
77 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
78 return ptr;
79
80 return NULL;
81 }
82
bpf_prog_alloc_no_stats(unsigned int size,gfp_t gfp_extra_flags)83 struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flags)
84 {
85 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
86 struct bpf_prog_aux *aux;
87 struct bpf_prog *fp;
88
89 size = round_up(size, PAGE_SIZE);
90 fp = __vmalloc(size, gfp_flags);
91 if (fp == NULL)
92 return NULL;
93
94 aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);
95 if (aux == NULL) {
96 vfree(fp);
97 return NULL;
98 }
99
100 fp->pages = size / PAGE_SIZE;
101 fp->aux = aux;
102 fp->aux->prog = fp;
103 fp->jit_requested = ebpf_jit_enabled();
104
105 INIT_LIST_HEAD_RCU(&fp->aux->ksym.lnode);
106 mutex_init(&fp->aux->used_maps_mutex);
107 mutex_init(&fp->aux->dst_mutex);
108
109 return fp;
110 }
111
bpf_prog_alloc(unsigned int size,gfp_t gfp_extra_flags)112 struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
113 {
114 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
115 struct bpf_prog *prog;
116 int cpu;
117
118 prog = bpf_prog_alloc_no_stats(size, gfp_extra_flags);
119 if (!prog)
120 return NULL;
121
122 prog->aux->stats = alloc_percpu_gfp(struct bpf_prog_stats, gfp_flags);
123 if (!prog->aux->stats) {
124 kfree(prog->aux);
125 vfree(prog);
126 return NULL;
127 }
128
129 for_each_possible_cpu(cpu) {
130 struct bpf_prog_stats *pstats;
131
132 pstats = per_cpu_ptr(prog->aux->stats, cpu);
133 u64_stats_init(&pstats->syncp);
134 }
135 return prog;
136 }
137 EXPORT_SYMBOL_GPL(bpf_prog_alloc);
138
bpf_prog_alloc_jited_linfo(struct bpf_prog * prog)139 int bpf_prog_alloc_jited_linfo(struct bpf_prog *prog)
140 {
141 if (!prog->aux->nr_linfo || !prog->jit_requested)
142 return 0;
143
144 prog->aux->jited_linfo = kcalloc(prog->aux->nr_linfo,
145 sizeof(*prog->aux->jited_linfo),
146 GFP_KERNEL | __GFP_NOWARN);
147 if (!prog->aux->jited_linfo)
148 return -ENOMEM;
149
150 return 0;
151 }
152
bpf_prog_free_jited_linfo(struct bpf_prog * prog)153 void bpf_prog_free_jited_linfo(struct bpf_prog *prog)
154 {
155 kfree(prog->aux->jited_linfo);
156 prog->aux->jited_linfo = NULL;
157 }
158
bpf_prog_free_unused_jited_linfo(struct bpf_prog * prog)159 void bpf_prog_free_unused_jited_linfo(struct bpf_prog *prog)
160 {
161 if (prog->aux->jited_linfo && !prog->aux->jited_linfo[0])
162 bpf_prog_free_jited_linfo(prog);
163 }
164
165 /* The jit engine is responsible to provide an array
166 * for insn_off to the jited_off mapping (insn_to_jit_off).
167 *
168 * The idx to this array is the insn_off. Hence, the insn_off
169 * here is relative to the prog itself instead of the main prog.
170 * This array has one entry for each xlated bpf insn.
171 *
172 * jited_off is the byte off to the last byte of the jited insn.
173 *
174 * Hence, with
175 * insn_start:
176 * The first bpf insn off of the prog. The insn off
177 * here is relative to the main prog.
178 * e.g. if prog is a subprog, insn_start > 0
179 * linfo_idx:
180 * The prog's idx to prog->aux->linfo and jited_linfo
181 *
182 * jited_linfo[linfo_idx] = prog->bpf_func
183 *
184 * For i > linfo_idx,
185 *
186 * jited_linfo[i] = prog->bpf_func +
187 * insn_to_jit_off[linfo[i].insn_off - insn_start - 1]
188 */
bpf_prog_fill_jited_linfo(struct bpf_prog * prog,const u32 * insn_to_jit_off)189 void bpf_prog_fill_jited_linfo(struct bpf_prog *prog,
190 const u32 *insn_to_jit_off)
191 {
192 u32 linfo_idx, insn_start, insn_end, nr_linfo, i;
193 const struct bpf_line_info *linfo;
194 void **jited_linfo;
195
196 if (!prog->aux->jited_linfo)
197 /* Userspace did not provide linfo */
198 return;
199
200 linfo_idx = prog->aux->linfo_idx;
201 linfo = &prog->aux->linfo[linfo_idx];
202 insn_start = linfo[0].insn_off;
203 insn_end = insn_start + prog->len;
204
205 jited_linfo = &prog->aux->jited_linfo[linfo_idx];
206 jited_linfo[0] = prog->bpf_func;
207
208 nr_linfo = prog->aux->nr_linfo - linfo_idx;
209
210 for (i = 1; i < nr_linfo && linfo[i].insn_off < insn_end; i++)
211 /* The verifier ensures that linfo[i].insn_off is
212 * strictly increasing
213 */
214 jited_linfo[i] = prog->bpf_func +
215 insn_to_jit_off[linfo[i].insn_off - insn_start - 1];
216 }
217
bpf_prog_free_linfo(struct bpf_prog * prog)218 void bpf_prog_free_linfo(struct bpf_prog *prog)
219 {
220 bpf_prog_free_jited_linfo(prog);
221 kvfree(prog->aux->linfo);
222 }
223
bpf_prog_realloc(struct bpf_prog * fp_old,unsigned int size,gfp_t gfp_extra_flags)224 struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
225 gfp_t gfp_extra_flags)
226 {
227 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
228 struct bpf_prog *fp;
229 u32 pages, delta;
230 int ret;
231
232 size = round_up(size, PAGE_SIZE);
233 pages = size / PAGE_SIZE;
234 if (pages <= fp_old->pages)
235 return fp_old;
236
237 delta = pages - fp_old->pages;
238 ret = __bpf_prog_charge(fp_old->aux->user, delta);
239 if (ret)
240 return NULL;
241
242 fp = __vmalloc(size, gfp_flags);
243 if (fp == NULL) {
244 __bpf_prog_uncharge(fp_old->aux->user, delta);
245 } else {
246 memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
247 fp->pages = pages;
248 fp->aux->prog = fp;
249
250 /* We keep fp->aux from fp_old around in the new
251 * reallocated structure.
252 */
253 fp_old->aux = NULL;
254 __bpf_prog_free(fp_old);
255 }
256
257 return fp;
258 }
259
__bpf_prog_free(struct bpf_prog * fp)260 void __bpf_prog_free(struct bpf_prog *fp)
261 {
262 if (fp->aux) {
263 mutex_destroy(&fp->aux->used_maps_mutex);
264 mutex_destroy(&fp->aux->dst_mutex);
265 free_percpu(fp->aux->stats);
266 kfree(fp->aux->poke_tab);
267 kfree(fp->aux);
268 }
269 vfree(fp);
270 }
271
bpf_prog_calc_tag(struct bpf_prog * fp)272 int bpf_prog_calc_tag(struct bpf_prog *fp)
273 {
274 const u32 bits_offset = SHA1_BLOCK_SIZE - sizeof(__be64);
275 u32 raw_size = bpf_prog_tag_scratch_size(fp);
276 u32 digest[SHA1_DIGEST_WORDS];
277 u32 ws[SHA1_WORKSPACE_WORDS];
278 u32 i, bsize, psize, blocks;
279 struct bpf_insn *dst;
280 bool was_ld_map;
281 u8 *raw, *todo;
282 __be32 *result;
283 __be64 *bits;
284
285 raw = vmalloc(raw_size);
286 if (!raw)
287 return -ENOMEM;
288
289 sha1_init(digest);
290 memset(ws, 0, sizeof(ws));
291
292 /* We need to take out the map fd for the digest calculation
293 * since they are unstable from user space side.
294 */
295 dst = (void *)raw;
296 for (i = 0, was_ld_map = false; i < fp->len; i++) {
297 dst[i] = fp->insnsi[i];
298 if (!was_ld_map &&
299 dst[i].code == (BPF_LD | BPF_IMM | BPF_DW) &&
300 (dst[i].src_reg == BPF_PSEUDO_MAP_FD ||
301 dst[i].src_reg == BPF_PSEUDO_MAP_VALUE)) {
302 was_ld_map = true;
303 dst[i].imm = 0;
304 } else if (was_ld_map &&
305 dst[i].code == 0 &&
306 dst[i].dst_reg == 0 &&
307 dst[i].src_reg == 0 &&
308 dst[i].off == 0) {
309 was_ld_map = false;
310 dst[i].imm = 0;
311 } else {
312 was_ld_map = false;
313 }
314 }
315
316 psize = bpf_prog_insn_size(fp);
317 memset(&raw[psize], 0, raw_size - psize);
318 raw[psize++] = 0x80;
319
320 bsize = round_up(psize, SHA1_BLOCK_SIZE);
321 blocks = bsize / SHA1_BLOCK_SIZE;
322 todo = raw;
323 if (bsize - psize >= sizeof(__be64)) {
324 bits = (__be64 *)(todo + bsize - sizeof(__be64));
325 } else {
326 bits = (__be64 *)(todo + bsize + bits_offset);
327 blocks++;
328 }
329 *bits = cpu_to_be64((psize - 1) << 3);
330
331 while (blocks--) {
332 sha1_transform(digest, todo, ws);
333 todo += SHA1_BLOCK_SIZE;
334 }
335
336 result = (__force __be32 *)digest;
337 for (i = 0; i < SHA1_DIGEST_WORDS; i++)
338 result[i] = cpu_to_be32(digest[i]);
339 memcpy(fp->tag, result, sizeof(fp->tag));
340
341 vfree(raw);
342 return 0;
343 }
344
bpf_adj_delta_to_imm(struct bpf_insn * insn,u32 pos,s32 end_old,s32 end_new,s32 curr,const bool probe_pass)345 static int bpf_adj_delta_to_imm(struct bpf_insn *insn, u32 pos, s32 end_old,
346 s32 end_new, s32 curr, const bool probe_pass)
347 {
348 const s64 imm_min = S32_MIN, imm_max = S32_MAX;
349 s32 delta = end_new - end_old;
350 s64 imm = insn->imm;
351
352 if (curr < pos && curr + imm + 1 >= end_old)
353 imm += delta;
354 else if (curr >= end_new && curr + imm + 1 < end_new)
355 imm -= delta;
356 if (imm < imm_min || imm > imm_max)
357 return -ERANGE;
358 if (!probe_pass)
359 insn->imm = imm;
360 return 0;
361 }
362
bpf_adj_delta_to_off(struct bpf_insn * insn,u32 pos,s32 end_old,s32 end_new,s32 curr,const bool probe_pass)363 static int bpf_adj_delta_to_off(struct bpf_insn *insn, u32 pos, s32 end_old,
364 s32 end_new, s32 curr, const bool probe_pass)
365 {
366 const s32 off_min = S16_MIN, off_max = S16_MAX;
367 s32 delta = end_new - end_old;
368 s32 off = insn->off;
369
370 if (curr < pos && curr + off + 1 >= end_old)
371 off += delta;
372 else if (curr >= end_new && curr + off + 1 < end_new)
373 off -= delta;
374 if (off < off_min || off > off_max)
375 return -ERANGE;
376 if (!probe_pass)
377 insn->off = off;
378 return 0;
379 }
380
bpf_adj_branches(struct bpf_prog * prog,u32 pos,s32 end_old,s32 end_new,const bool probe_pass)381 static int bpf_adj_branches(struct bpf_prog *prog, u32 pos, s32 end_old,
382 s32 end_new, const bool probe_pass)
383 {
384 u32 i, insn_cnt = prog->len + (probe_pass ? end_new - end_old : 0);
385 struct bpf_insn *insn = prog->insnsi;
386 int ret = 0;
387
388 for (i = 0; i < insn_cnt; i++, insn++) {
389 u8 code;
390
391 /* In the probing pass we still operate on the original,
392 * unpatched image in order to check overflows before we
393 * do any other adjustments. Therefore skip the patchlet.
394 */
395 if (probe_pass && i == pos) {
396 i = end_new;
397 insn = prog->insnsi + end_old;
398 }
399 code = insn->code;
400 if ((BPF_CLASS(code) != BPF_JMP &&
401 BPF_CLASS(code) != BPF_JMP32) ||
402 BPF_OP(code) == BPF_EXIT)
403 continue;
404 /* Adjust offset of jmps if we cross patch boundaries. */
405 if (BPF_OP(code) == BPF_CALL) {
406 if (insn->src_reg != BPF_PSEUDO_CALL)
407 continue;
408 ret = bpf_adj_delta_to_imm(insn, pos, end_old,
409 end_new, i, probe_pass);
410 } else {
411 ret = bpf_adj_delta_to_off(insn, pos, end_old,
412 end_new, i, probe_pass);
413 }
414 if (ret)
415 break;
416 }
417
418 return ret;
419 }
420
bpf_adj_linfo(struct bpf_prog * prog,u32 off,u32 delta)421 static void bpf_adj_linfo(struct bpf_prog *prog, u32 off, u32 delta)
422 {
423 struct bpf_line_info *linfo;
424 u32 i, nr_linfo;
425
426 nr_linfo = prog->aux->nr_linfo;
427 if (!nr_linfo || !delta)
428 return;
429
430 linfo = prog->aux->linfo;
431
432 for (i = 0; i < nr_linfo; i++)
433 if (off < linfo[i].insn_off)
434 break;
435
436 /* Push all off < linfo[i].insn_off by delta */
437 for (; i < nr_linfo; i++)
438 linfo[i].insn_off += delta;
439 }
440
bpf_patch_insn_single(struct bpf_prog * prog,u32 off,const struct bpf_insn * patch,u32 len)441 struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
442 const struct bpf_insn *patch, u32 len)
443 {
444 u32 insn_adj_cnt, insn_rest, insn_delta = len - 1;
445 const u32 cnt_max = S16_MAX;
446 struct bpf_prog *prog_adj;
447 int err;
448
449 /* Since our patchlet doesn't expand the image, we're done. */
450 if (insn_delta == 0) {
451 memcpy(prog->insnsi + off, patch, sizeof(*patch));
452 return prog;
453 }
454
455 insn_adj_cnt = prog->len + insn_delta;
456
457 /* Reject anything that would potentially let the insn->off
458 * target overflow when we have excessive program expansions.
459 * We need to probe here before we do any reallocation where
460 * we afterwards may not fail anymore.
461 */
462 if (insn_adj_cnt > cnt_max &&
463 (err = bpf_adj_branches(prog, off, off + 1, off + len, true)))
464 return ERR_PTR(err);
465
466 /* Several new instructions need to be inserted. Make room
467 * for them. Likely, there's no need for a new allocation as
468 * last page could have large enough tailroom.
469 */
470 prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),
471 GFP_USER);
472 if (!prog_adj)
473 return ERR_PTR(-ENOMEM);
474
475 prog_adj->len = insn_adj_cnt;
476
477 /* Patching happens in 3 steps:
478 *
479 * 1) Move over tail of insnsi from next instruction onwards,
480 * so we can patch the single target insn with one or more
481 * new ones (patching is always from 1 to n insns, n > 0).
482 * 2) Inject new instructions at the target location.
483 * 3) Adjust branch offsets if necessary.
484 */
485 insn_rest = insn_adj_cnt - off - len;
486
487 memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1,
488 sizeof(*patch) * insn_rest);
489 memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len);
490
491 /* We are guaranteed to not fail at this point, otherwise
492 * the ship has sailed to reverse to the original state. An
493 * overflow cannot happen at this point.
494 */
495 BUG_ON(bpf_adj_branches(prog_adj, off, off + 1, off + len, false));
496
497 bpf_adj_linfo(prog_adj, off, insn_delta);
498
499 return prog_adj;
500 }
501
bpf_remove_insns(struct bpf_prog * prog,u32 off,u32 cnt)502 int bpf_remove_insns(struct bpf_prog *prog, u32 off, u32 cnt)
503 {
504 /* Branch offsets can't overflow when program is shrinking, no need
505 * to call bpf_adj_branches(..., true) here
506 */
507 memmove(prog->insnsi + off, prog->insnsi + off + cnt,
508 sizeof(struct bpf_insn) * (prog->len - off - cnt));
509 prog->len -= cnt;
510
511 return WARN_ON_ONCE(bpf_adj_branches(prog, off, off + cnt, off, false));
512 }
513
bpf_prog_kallsyms_del_subprogs(struct bpf_prog * fp)514 static void bpf_prog_kallsyms_del_subprogs(struct bpf_prog *fp)
515 {
516 int i;
517
518 for (i = 0; i < fp->aux->func_cnt; i++)
519 bpf_prog_kallsyms_del(fp->aux->func[i]);
520 }
521
bpf_prog_kallsyms_del_all(struct bpf_prog * fp)522 void bpf_prog_kallsyms_del_all(struct bpf_prog *fp)
523 {
524 bpf_prog_kallsyms_del_subprogs(fp);
525 bpf_prog_kallsyms_del(fp);
526 }
527
528 #ifdef CONFIG_BPF_JIT
529 /* All BPF JIT sysctl knobs here. */
530 int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_DEFAULT_ON);
531 int bpf_jit_kallsyms __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_DEFAULT_ON);
532 int bpf_jit_harden __read_mostly;
533 long bpf_jit_limit __read_mostly;
534 long bpf_jit_limit_max __read_mostly;
535
536 static void
bpf_prog_ksym_set_addr(struct bpf_prog * prog)537 bpf_prog_ksym_set_addr(struct bpf_prog *prog)
538 {
539 const struct bpf_binary_header *hdr = bpf_jit_binary_hdr(prog);
540 unsigned long addr = (unsigned long)hdr;
541
542 WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog));
543
544 prog->aux->ksym.start = (unsigned long) prog->bpf_func;
545 prog->aux->ksym.end = addr + hdr->pages * PAGE_SIZE;
546 }
547
548 static void
bpf_prog_ksym_set_name(struct bpf_prog * prog)549 bpf_prog_ksym_set_name(struct bpf_prog *prog)
550 {
551 char *sym = prog->aux->ksym.name;
552 const char *end = sym + KSYM_NAME_LEN;
553 const struct btf_type *type;
554 const char *func_name;
555
556 BUILD_BUG_ON(sizeof("bpf_prog_") +
557 sizeof(prog->tag) * 2 +
558 /* name has been null terminated.
559 * We should need +1 for the '_' preceding
560 * the name. However, the null character
561 * is double counted between the name and the
562 * sizeof("bpf_prog_") above, so we omit
563 * the +1 here.
564 */
565 sizeof(prog->aux->name) > KSYM_NAME_LEN);
566
567 sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_");
568 sym = bin2hex(sym, prog->tag, sizeof(prog->tag));
569
570 /* prog->aux->name will be ignored if full btf name is available */
571 if (prog->aux->func_info_cnt) {
572 type = btf_type_by_id(prog->aux->btf,
573 prog->aux->func_info[prog->aux->func_idx].type_id);
574 func_name = btf_name_by_offset(prog->aux->btf, type->name_off);
575 snprintf(sym, (size_t)(end - sym), "_%s", func_name);
576 return;
577 }
578
579 if (prog->aux->name[0])
580 snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name);
581 else
582 *sym = 0;
583 }
584
bpf_get_ksym_start(struct latch_tree_node * n)585 static unsigned long bpf_get_ksym_start(struct latch_tree_node *n)
586 {
587 return container_of(n, struct bpf_ksym, tnode)->start;
588 }
589
bpf_tree_less(struct latch_tree_node * a,struct latch_tree_node * b)590 static __always_inline bool bpf_tree_less(struct latch_tree_node *a,
591 struct latch_tree_node *b)
592 {
593 return bpf_get_ksym_start(a) < bpf_get_ksym_start(b);
594 }
595
bpf_tree_comp(void * key,struct latch_tree_node * n)596 static __always_inline int bpf_tree_comp(void *key, struct latch_tree_node *n)
597 {
598 unsigned long val = (unsigned long)key;
599 const struct bpf_ksym *ksym;
600
601 ksym = container_of(n, struct bpf_ksym, tnode);
602
603 if (val < ksym->start)
604 return -1;
605 if (val >= ksym->end)
606 return 1;
607
608 return 0;
609 }
610
611 static const struct latch_tree_ops bpf_tree_ops = {
612 .less = bpf_tree_less,
613 .comp = bpf_tree_comp,
614 };
615
616 static DEFINE_SPINLOCK(bpf_lock);
617 static LIST_HEAD(bpf_kallsyms);
618 static struct latch_tree_root bpf_tree __cacheline_aligned;
619
bpf_ksym_add(struct bpf_ksym * ksym)620 void bpf_ksym_add(struct bpf_ksym *ksym)
621 {
622 spin_lock_bh(&bpf_lock);
623 WARN_ON_ONCE(!list_empty(&ksym->lnode));
624 list_add_tail_rcu(&ksym->lnode, &bpf_kallsyms);
625 latch_tree_insert(&ksym->tnode, &bpf_tree, &bpf_tree_ops);
626 spin_unlock_bh(&bpf_lock);
627 }
628
__bpf_ksym_del(struct bpf_ksym * ksym)629 static void __bpf_ksym_del(struct bpf_ksym *ksym)
630 {
631 if (list_empty(&ksym->lnode))
632 return;
633
634 latch_tree_erase(&ksym->tnode, &bpf_tree, &bpf_tree_ops);
635 list_del_rcu(&ksym->lnode);
636 }
637
bpf_ksym_del(struct bpf_ksym * ksym)638 void bpf_ksym_del(struct bpf_ksym *ksym)
639 {
640 spin_lock_bh(&bpf_lock);
641 __bpf_ksym_del(ksym);
642 spin_unlock_bh(&bpf_lock);
643 }
644
bpf_prog_kallsyms_candidate(const struct bpf_prog * fp)645 static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp)
646 {
647 return fp->jited && !bpf_prog_was_classic(fp);
648 }
649
bpf_prog_kallsyms_verify_off(const struct bpf_prog * fp)650 static bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp)
651 {
652 return list_empty(&fp->aux->ksym.lnode) ||
653 fp->aux->ksym.lnode.prev == LIST_POISON2;
654 }
655
bpf_prog_kallsyms_add(struct bpf_prog * fp)656 void bpf_prog_kallsyms_add(struct bpf_prog *fp)
657 {
658 if (!bpf_prog_kallsyms_candidate(fp) ||
659 !bpf_capable())
660 return;
661
662 bpf_prog_ksym_set_addr(fp);
663 bpf_prog_ksym_set_name(fp);
664 fp->aux->ksym.prog = true;
665
666 bpf_ksym_add(&fp->aux->ksym);
667 }
668
bpf_prog_kallsyms_del(struct bpf_prog * fp)669 void bpf_prog_kallsyms_del(struct bpf_prog *fp)
670 {
671 if (!bpf_prog_kallsyms_candidate(fp))
672 return;
673
674 bpf_ksym_del(&fp->aux->ksym);
675 }
676
bpf_ksym_find(unsigned long addr)677 static struct bpf_ksym *bpf_ksym_find(unsigned long addr)
678 {
679 struct latch_tree_node *n;
680
681 n = latch_tree_find((void *)addr, &bpf_tree, &bpf_tree_ops);
682 return n ? container_of(n, struct bpf_ksym, tnode) : NULL;
683 }
684
__bpf_address_lookup(unsigned long addr,unsigned long * size,unsigned long * off,char * sym)685 const char *__bpf_address_lookup(unsigned long addr, unsigned long *size,
686 unsigned long *off, char *sym)
687 {
688 struct bpf_ksym *ksym;
689 char *ret = NULL;
690
691 rcu_read_lock();
692 ksym = bpf_ksym_find(addr);
693 if (ksym) {
694 unsigned long symbol_start = ksym->start;
695 unsigned long symbol_end = ksym->end;
696
697 strncpy(sym, ksym->name, KSYM_NAME_LEN);
698
699 ret = sym;
700 if (size)
701 *size = symbol_end - symbol_start;
702 if (off)
703 *off = addr - symbol_start;
704 }
705 rcu_read_unlock();
706
707 return ret;
708 }
709
is_bpf_text_address(unsigned long addr)710 bool is_bpf_text_address(unsigned long addr)
711 {
712 bool ret;
713
714 rcu_read_lock();
715 ret = bpf_ksym_find(addr) != NULL;
716 rcu_read_unlock();
717
718 return ret;
719 }
720
bpf_prog_ksym_find(unsigned long addr)721 static struct bpf_prog *bpf_prog_ksym_find(unsigned long addr)
722 {
723 struct bpf_ksym *ksym = bpf_ksym_find(addr);
724
725 return ksym && ksym->prog ?
726 container_of(ksym, struct bpf_prog_aux, ksym)->prog :
727 NULL;
728 }
729
search_bpf_extables(unsigned long addr)730 const struct exception_table_entry *search_bpf_extables(unsigned long addr)
731 {
732 const struct exception_table_entry *e = NULL;
733 struct bpf_prog *prog;
734
735 rcu_read_lock();
736 prog = bpf_prog_ksym_find(addr);
737 if (!prog)
738 goto out;
739 if (!prog->aux->num_exentries)
740 goto out;
741
742 e = search_extable(prog->aux->extable, prog->aux->num_exentries, addr);
743 out:
744 rcu_read_unlock();
745 return e;
746 }
747
bpf_get_kallsym(unsigned int symnum,unsigned long * value,char * type,char * sym)748 int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
749 char *sym)
750 {
751 struct bpf_ksym *ksym;
752 unsigned int it = 0;
753 int ret = -ERANGE;
754
755 if (!bpf_jit_kallsyms_enabled())
756 return ret;
757
758 rcu_read_lock();
759 list_for_each_entry_rcu(ksym, &bpf_kallsyms, lnode) {
760 if (it++ != symnum)
761 continue;
762
763 strncpy(sym, ksym->name, KSYM_NAME_LEN);
764
765 *value = ksym->start;
766 *type = BPF_SYM_ELF_TYPE;
767
768 ret = 0;
769 break;
770 }
771 rcu_read_unlock();
772
773 return ret;
774 }
775
bpf_jit_add_poke_descriptor(struct bpf_prog * prog,struct bpf_jit_poke_descriptor * poke)776 int bpf_jit_add_poke_descriptor(struct bpf_prog *prog,
777 struct bpf_jit_poke_descriptor *poke)
778 {
779 struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab;
780 static const u32 poke_tab_max = 1024;
781 u32 slot = prog->aux->size_poke_tab;
782 u32 size = slot + 1;
783
784 if (size > poke_tab_max)
785 return -ENOSPC;
786 if (poke->tailcall_target || poke->tailcall_target_stable ||
787 poke->tailcall_bypass || poke->adj_off || poke->bypass_addr)
788 return -EINVAL;
789
790 switch (poke->reason) {
791 case BPF_POKE_REASON_TAIL_CALL:
792 if (!poke->tail_call.map)
793 return -EINVAL;
794 break;
795 default:
796 return -EINVAL;
797 }
798
799 tab = krealloc(tab, size * sizeof(*poke), GFP_KERNEL);
800 if (!tab)
801 return -ENOMEM;
802
803 memcpy(&tab[slot], poke, sizeof(*poke));
804 prog->aux->size_poke_tab = size;
805 prog->aux->poke_tab = tab;
806
807 return slot;
808 }
809
810 static atomic_long_t bpf_jit_current;
811
812 /* Can be overridden by an arch's JIT compiler if it has a custom,
813 * dedicated BPF backend memory area, or if neither of the two
814 * below apply.
815 */
bpf_jit_alloc_exec_limit(void)816 u64 __weak bpf_jit_alloc_exec_limit(void)
817 {
818 #if defined(MODULES_VADDR)
819 return MODULES_END - MODULES_VADDR;
820 #else
821 return VMALLOC_END - VMALLOC_START;
822 #endif
823 }
824
bpf_jit_charge_init(void)825 static int __init bpf_jit_charge_init(void)
826 {
827 /* Only used as heuristic here to derive limit. */
828 bpf_jit_limit_max = bpf_jit_alloc_exec_limit();
829 bpf_jit_limit = min_t(u64, round_up(bpf_jit_limit_max >> 2,
830 PAGE_SIZE), LONG_MAX);
831 return 0;
832 }
833 pure_initcall(bpf_jit_charge_init);
834
bpf_jit_charge_modmem(u32 pages)835 int bpf_jit_charge_modmem(u32 pages)
836 {
837 if (atomic_long_add_return(pages, &bpf_jit_current) >
838 (bpf_jit_limit >> PAGE_SHIFT)) {
839 if (!bpf_capable()) {
840 atomic_long_sub(pages, &bpf_jit_current);
841 return -EPERM;
842 }
843 }
844
845 return 0;
846 }
847
bpf_jit_uncharge_modmem(u32 pages)848 void bpf_jit_uncharge_modmem(u32 pages)
849 {
850 atomic_long_sub(pages, &bpf_jit_current);
851 }
852
bpf_jit_alloc_exec(unsigned long size)853 void *__weak bpf_jit_alloc_exec(unsigned long size)
854 {
855 return module_alloc(size);
856 }
857
bpf_jit_free_exec(void * addr)858 void __weak bpf_jit_free_exec(void *addr)
859 {
860 module_memfree(addr);
861 }
862
863 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)864 bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
865 unsigned int alignment,
866 bpf_jit_fill_hole_t bpf_fill_ill_insns)
867 {
868 struct bpf_binary_header *hdr;
869 u32 size, hole, start, pages;
870
871 WARN_ON_ONCE(!is_power_of_2(alignment) ||
872 alignment > BPF_IMAGE_ALIGNMENT);
873
874 /* Most of BPF filters are really small, but if some of them
875 * fill a page, allow at least 128 extra bytes to insert a
876 * random section of illegal instructions.
877 */
878 size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
879 pages = size / PAGE_SIZE;
880
881 if (bpf_jit_charge_modmem(pages))
882 return NULL;
883 hdr = bpf_jit_alloc_exec(size);
884 if (!hdr) {
885 bpf_jit_uncharge_modmem(pages);
886 return NULL;
887 }
888
889 /* Fill space with illegal/arch-dep instructions. */
890 bpf_fill_ill_insns(hdr, size);
891
892 hdr->pages = pages;
893 hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
894 PAGE_SIZE - sizeof(*hdr));
895 start = (get_random_int() % hole) & ~(alignment - 1);
896
897 /* Leave a random number of instructions before BPF code. */
898 *image_ptr = &hdr->image[start];
899
900 return hdr;
901 }
902
bpf_jit_binary_free(struct bpf_binary_header * hdr)903 void bpf_jit_binary_free(struct bpf_binary_header *hdr)
904 {
905 u32 pages = hdr->pages;
906
907 bpf_jit_free_exec(hdr);
908 bpf_jit_uncharge_modmem(pages);
909 }
910
911 /* This symbol is only overridden by archs that have different
912 * requirements than the usual eBPF JITs, f.e. when they only
913 * implement cBPF JIT, do not set images read-only, etc.
914 */
bpf_jit_free(struct bpf_prog * fp)915 void __weak bpf_jit_free(struct bpf_prog *fp)
916 {
917 if (fp->jited) {
918 struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp);
919
920 bpf_jit_binary_free(hdr);
921
922 WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp));
923 }
924
925 bpf_prog_unlock_free(fp);
926 }
927
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)928 int bpf_jit_get_func_addr(const struct bpf_prog *prog,
929 const struct bpf_insn *insn, bool extra_pass,
930 u64 *func_addr, bool *func_addr_fixed)
931 {
932 s16 off = insn->off;
933 s32 imm = insn->imm;
934 u8 *addr;
935
936 *func_addr_fixed = insn->src_reg != BPF_PSEUDO_CALL;
937 if (!*func_addr_fixed) {
938 /* Place-holder address till the last pass has collected
939 * all addresses for JITed subprograms in which case we
940 * can pick them up from prog->aux.
941 */
942 if (!extra_pass)
943 addr = NULL;
944 else if (prog->aux->func &&
945 off >= 0 && off < prog->aux->func_cnt)
946 addr = (u8 *)prog->aux->func[off]->bpf_func;
947 else
948 return -EINVAL;
949 } else {
950 /* Address of a BPF helper call. Since part of the core
951 * kernel, it's always at a fixed location. __bpf_call_base
952 * and the helper with imm relative to it are both in core
953 * kernel.
954 */
955 addr = (u8 *)__bpf_call_base + imm;
956 }
957
958 *func_addr = (unsigned long)addr;
959 return 0;
960 }
961
bpf_jit_blind_insn(const struct bpf_insn * from,const struct bpf_insn * aux,struct bpf_insn * to_buff,bool emit_zext)962 static int bpf_jit_blind_insn(const struct bpf_insn *from,
963 const struct bpf_insn *aux,
964 struct bpf_insn *to_buff,
965 bool emit_zext)
966 {
967 struct bpf_insn *to = to_buff;
968 u32 imm_rnd = get_random_int();
969 s16 off;
970
971 BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG);
972 BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG);
973
974 /* Constraints on AX register:
975 *
976 * AX register is inaccessible from user space. It is mapped in
977 * all JITs, and used here for constant blinding rewrites. It is
978 * typically "stateless" meaning its contents are only valid within
979 * the executed instruction, but not across several instructions.
980 * There are a few exceptions however which are further detailed
981 * below.
982 *
983 * Constant blinding is only used by JITs, not in the interpreter.
984 * The interpreter uses AX in some occasions as a local temporary
985 * register e.g. in DIV or MOD instructions.
986 *
987 * In restricted circumstances, the verifier can also use the AX
988 * register for rewrites as long as they do not interfere with
989 * the above cases!
990 */
991 if (from->dst_reg == BPF_REG_AX || from->src_reg == BPF_REG_AX)
992 goto out;
993
994 if (from->imm == 0 &&
995 (from->code == (BPF_ALU | BPF_MOV | BPF_K) ||
996 from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) {
997 *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg);
998 goto out;
999 }
1000
1001 switch (from->code) {
1002 case BPF_ALU | BPF_ADD | BPF_K:
1003 case BPF_ALU | BPF_SUB | BPF_K:
1004 case BPF_ALU | BPF_AND | BPF_K:
1005 case BPF_ALU | BPF_OR | BPF_K:
1006 case BPF_ALU | BPF_XOR | BPF_K:
1007 case BPF_ALU | BPF_MUL | BPF_K:
1008 case BPF_ALU | BPF_MOV | BPF_K:
1009 case BPF_ALU | BPF_DIV | BPF_K:
1010 case BPF_ALU | BPF_MOD | BPF_K:
1011 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
1012 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
1013 *to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX);
1014 break;
1015
1016 case BPF_ALU64 | BPF_ADD | BPF_K:
1017 case BPF_ALU64 | BPF_SUB | BPF_K:
1018 case BPF_ALU64 | BPF_AND | BPF_K:
1019 case BPF_ALU64 | BPF_OR | BPF_K:
1020 case BPF_ALU64 | BPF_XOR | BPF_K:
1021 case BPF_ALU64 | BPF_MUL | BPF_K:
1022 case BPF_ALU64 | BPF_MOV | BPF_K:
1023 case BPF_ALU64 | BPF_DIV | BPF_K:
1024 case BPF_ALU64 | BPF_MOD | BPF_K:
1025 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
1026 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
1027 *to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX);
1028 break;
1029
1030 case BPF_JMP | BPF_JEQ | BPF_K:
1031 case BPF_JMP | BPF_JNE | BPF_K:
1032 case BPF_JMP | BPF_JGT | BPF_K:
1033 case BPF_JMP | BPF_JLT | BPF_K:
1034 case BPF_JMP | BPF_JGE | BPF_K:
1035 case BPF_JMP | BPF_JLE | BPF_K:
1036 case BPF_JMP | BPF_JSGT | BPF_K:
1037 case BPF_JMP | BPF_JSLT | BPF_K:
1038 case BPF_JMP | BPF_JSGE | BPF_K:
1039 case BPF_JMP | BPF_JSLE | BPF_K:
1040 case BPF_JMP | BPF_JSET | BPF_K:
1041 /* Accommodate for extra offset in case of a backjump. */
1042 off = from->off;
1043 if (off < 0)
1044 off -= 2;
1045 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
1046 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
1047 *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off);
1048 break;
1049
1050 case BPF_JMP32 | BPF_JEQ | BPF_K:
1051 case BPF_JMP32 | BPF_JNE | BPF_K:
1052 case BPF_JMP32 | BPF_JGT | BPF_K:
1053 case BPF_JMP32 | BPF_JLT | BPF_K:
1054 case BPF_JMP32 | BPF_JGE | BPF_K:
1055 case BPF_JMP32 | BPF_JLE | BPF_K:
1056 case BPF_JMP32 | BPF_JSGT | BPF_K:
1057 case BPF_JMP32 | BPF_JSLT | BPF_K:
1058 case BPF_JMP32 | BPF_JSGE | BPF_K:
1059 case BPF_JMP32 | BPF_JSLE | BPF_K:
1060 case BPF_JMP32 | BPF_JSET | BPF_K:
1061 /* Accommodate for extra offset in case of a backjump. */
1062 off = from->off;
1063 if (off < 0)
1064 off -= 2;
1065 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
1066 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
1067 *to++ = BPF_JMP32_REG(from->code, from->dst_reg, BPF_REG_AX,
1068 off);
1069 break;
1070
1071 case BPF_LD | BPF_IMM | BPF_DW:
1072 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm);
1073 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
1074 *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
1075 *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX);
1076 break;
1077 case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */
1078 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm);
1079 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
1080 if (emit_zext)
1081 *to++ = BPF_ZEXT_REG(BPF_REG_AX);
1082 *to++ = BPF_ALU64_REG(BPF_OR, aux[0].dst_reg, BPF_REG_AX);
1083 break;
1084
1085 case BPF_ST | BPF_MEM | BPF_DW:
1086 case BPF_ST | BPF_MEM | BPF_W:
1087 case BPF_ST | BPF_MEM | BPF_H:
1088 case BPF_ST | BPF_MEM | BPF_B:
1089 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
1090 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
1091 *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off);
1092 break;
1093 }
1094 out:
1095 return to - to_buff;
1096 }
1097
bpf_prog_clone_create(struct bpf_prog * fp_other,gfp_t gfp_extra_flags)1098 static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other,
1099 gfp_t gfp_extra_flags)
1100 {
1101 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
1102 struct bpf_prog *fp;
1103
1104 fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags);
1105 if (fp != NULL) {
1106 /* aux->prog still points to the fp_other one, so
1107 * when promoting the clone to the real program,
1108 * this still needs to be adapted.
1109 */
1110 memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE);
1111 }
1112
1113 return fp;
1114 }
1115
bpf_prog_clone_free(struct bpf_prog * fp)1116 static void bpf_prog_clone_free(struct bpf_prog *fp)
1117 {
1118 /* aux was stolen by the other clone, so we cannot free
1119 * it from this path! It will be freed eventually by the
1120 * other program on release.
1121 *
1122 * At this point, we don't need a deferred release since
1123 * clone is guaranteed to not be locked.
1124 */
1125 fp->aux = NULL;
1126 __bpf_prog_free(fp);
1127 }
1128
bpf_jit_prog_release_other(struct bpf_prog * fp,struct bpf_prog * fp_other)1129 void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other)
1130 {
1131 /* We have to repoint aux->prog to self, as we don't
1132 * know whether fp here is the clone or the original.
1133 */
1134 fp->aux->prog = fp;
1135 bpf_prog_clone_free(fp_other);
1136 }
1137
bpf_jit_blind_constants(struct bpf_prog * prog)1138 struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
1139 {
1140 struct bpf_insn insn_buff[16], aux[2];
1141 struct bpf_prog *clone, *tmp;
1142 int insn_delta, insn_cnt;
1143 struct bpf_insn *insn;
1144 int i, rewritten;
1145
1146 if (!bpf_jit_blinding_enabled(prog) || prog->blinded)
1147 return prog;
1148
1149 clone = bpf_prog_clone_create(prog, GFP_USER);
1150 if (!clone)
1151 return ERR_PTR(-ENOMEM);
1152
1153 insn_cnt = clone->len;
1154 insn = clone->insnsi;
1155
1156 for (i = 0; i < insn_cnt; i++, insn++) {
1157 /* We temporarily need to hold the original ld64 insn
1158 * so that we can still access the first part in the
1159 * second blinding run.
1160 */
1161 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) &&
1162 insn[1].code == 0)
1163 memcpy(aux, insn, sizeof(aux));
1164
1165 rewritten = bpf_jit_blind_insn(insn, aux, insn_buff,
1166 clone->aux->verifier_zext);
1167 if (!rewritten)
1168 continue;
1169
1170 tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);
1171 if (IS_ERR(tmp)) {
1172 /* Patching may have repointed aux->prog during
1173 * realloc from the original one, so we need to
1174 * fix it up here on error.
1175 */
1176 bpf_jit_prog_release_other(prog, clone);
1177 return tmp;
1178 }
1179
1180 clone = tmp;
1181 insn_delta = rewritten - 1;
1182
1183 /* Walk new program and skip insns we just inserted. */
1184 insn = clone->insnsi + i + insn_delta;
1185 insn_cnt += insn_delta;
1186 i += insn_delta;
1187 }
1188
1189 clone->blinded = 1;
1190 return clone;
1191 }
1192 #endif /* CONFIG_BPF_JIT */
1193
1194 /* Base function for offset calculation. Needs to go into .text section,
1195 * therefore keeping it non-static as well; will also be used by JITs
1196 * anyway later on, so do not let the compiler omit it. This also needs
1197 * to go into kallsyms for correlation from e.g. bpftool, so naming
1198 * must not change.
1199 */
__bpf_call_base(u64 r1,u64 r2,u64 r3,u64 r4,u64 r5)1200 noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1201 {
1202 return 0;
1203 }
1204 EXPORT_SYMBOL_GPL(__bpf_call_base);
1205
1206 /* All UAPI available opcodes. */
1207 #define BPF_INSN_MAP(INSN_2, INSN_3) \
1208 /* 32 bit ALU operations. */ \
1209 /* Register based. */ \
1210 INSN_3(ALU, ADD, X), \
1211 INSN_3(ALU, SUB, X), \
1212 INSN_3(ALU, AND, X), \
1213 INSN_3(ALU, OR, X), \
1214 INSN_3(ALU, LSH, X), \
1215 INSN_3(ALU, RSH, X), \
1216 INSN_3(ALU, XOR, X), \
1217 INSN_3(ALU, MUL, X), \
1218 INSN_3(ALU, MOV, X), \
1219 INSN_3(ALU, ARSH, X), \
1220 INSN_3(ALU, DIV, X), \
1221 INSN_3(ALU, MOD, X), \
1222 INSN_2(ALU, NEG), \
1223 INSN_3(ALU, END, TO_BE), \
1224 INSN_3(ALU, END, TO_LE), \
1225 /* Immediate based. */ \
1226 INSN_3(ALU, ADD, K), \
1227 INSN_3(ALU, SUB, K), \
1228 INSN_3(ALU, AND, K), \
1229 INSN_3(ALU, OR, K), \
1230 INSN_3(ALU, LSH, K), \
1231 INSN_3(ALU, RSH, K), \
1232 INSN_3(ALU, XOR, K), \
1233 INSN_3(ALU, MUL, K), \
1234 INSN_3(ALU, MOV, K), \
1235 INSN_3(ALU, ARSH, K), \
1236 INSN_3(ALU, DIV, K), \
1237 INSN_3(ALU, MOD, K), \
1238 /* 64 bit ALU operations. */ \
1239 /* Register based. */ \
1240 INSN_3(ALU64, ADD, X), \
1241 INSN_3(ALU64, SUB, X), \
1242 INSN_3(ALU64, AND, X), \
1243 INSN_3(ALU64, OR, X), \
1244 INSN_3(ALU64, LSH, X), \
1245 INSN_3(ALU64, RSH, X), \
1246 INSN_3(ALU64, XOR, X), \
1247 INSN_3(ALU64, MUL, X), \
1248 INSN_3(ALU64, MOV, X), \
1249 INSN_3(ALU64, ARSH, X), \
1250 INSN_3(ALU64, DIV, X), \
1251 INSN_3(ALU64, MOD, X), \
1252 INSN_2(ALU64, NEG), \
1253 /* Immediate based. */ \
1254 INSN_3(ALU64, ADD, K), \
1255 INSN_3(ALU64, SUB, K), \
1256 INSN_3(ALU64, AND, K), \
1257 INSN_3(ALU64, OR, K), \
1258 INSN_3(ALU64, LSH, K), \
1259 INSN_3(ALU64, RSH, K), \
1260 INSN_3(ALU64, XOR, K), \
1261 INSN_3(ALU64, MUL, K), \
1262 INSN_3(ALU64, MOV, K), \
1263 INSN_3(ALU64, ARSH, K), \
1264 INSN_3(ALU64, DIV, K), \
1265 INSN_3(ALU64, MOD, K), \
1266 /* Call instruction. */ \
1267 INSN_2(JMP, CALL), \
1268 /* Exit instruction. */ \
1269 INSN_2(JMP, EXIT), \
1270 /* 32-bit Jump instructions. */ \
1271 /* Register based. */ \
1272 INSN_3(JMP32, JEQ, X), \
1273 INSN_3(JMP32, JNE, X), \
1274 INSN_3(JMP32, JGT, X), \
1275 INSN_3(JMP32, JLT, X), \
1276 INSN_3(JMP32, JGE, X), \
1277 INSN_3(JMP32, JLE, X), \
1278 INSN_3(JMP32, JSGT, X), \
1279 INSN_3(JMP32, JSLT, X), \
1280 INSN_3(JMP32, JSGE, X), \
1281 INSN_3(JMP32, JSLE, X), \
1282 INSN_3(JMP32, JSET, X), \
1283 /* Immediate based. */ \
1284 INSN_3(JMP32, JEQ, K), \
1285 INSN_3(JMP32, JNE, K), \
1286 INSN_3(JMP32, JGT, K), \
1287 INSN_3(JMP32, JLT, K), \
1288 INSN_3(JMP32, JGE, K), \
1289 INSN_3(JMP32, JLE, K), \
1290 INSN_3(JMP32, JSGT, K), \
1291 INSN_3(JMP32, JSLT, K), \
1292 INSN_3(JMP32, JSGE, K), \
1293 INSN_3(JMP32, JSLE, K), \
1294 INSN_3(JMP32, JSET, K), \
1295 /* Jump instructions. */ \
1296 /* Register based. */ \
1297 INSN_3(JMP, JEQ, X), \
1298 INSN_3(JMP, JNE, X), \
1299 INSN_3(JMP, JGT, X), \
1300 INSN_3(JMP, JLT, X), \
1301 INSN_3(JMP, JGE, X), \
1302 INSN_3(JMP, JLE, X), \
1303 INSN_3(JMP, JSGT, X), \
1304 INSN_3(JMP, JSLT, X), \
1305 INSN_3(JMP, JSGE, X), \
1306 INSN_3(JMP, JSLE, X), \
1307 INSN_3(JMP, JSET, X), \
1308 /* Immediate based. */ \
1309 INSN_3(JMP, JEQ, K), \
1310 INSN_3(JMP, JNE, K), \
1311 INSN_3(JMP, JGT, K), \
1312 INSN_3(JMP, JLT, K), \
1313 INSN_3(JMP, JGE, K), \
1314 INSN_3(JMP, JLE, K), \
1315 INSN_3(JMP, JSGT, K), \
1316 INSN_3(JMP, JSLT, K), \
1317 INSN_3(JMP, JSGE, K), \
1318 INSN_3(JMP, JSLE, K), \
1319 INSN_3(JMP, JSET, K), \
1320 INSN_2(JMP, JA), \
1321 /* Store instructions. */ \
1322 /* Register based. */ \
1323 INSN_3(STX, MEM, B), \
1324 INSN_3(STX, MEM, H), \
1325 INSN_3(STX, MEM, W), \
1326 INSN_3(STX, MEM, DW), \
1327 INSN_3(STX, XADD, W), \
1328 INSN_3(STX, XADD, DW), \
1329 /* Immediate based. */ \
1330 INSN_3(ST, MEM, B), \
1331 INSN_3(ST, MEM, H), \
1332 INSN_3(ST, MEM, W), \
1333 INSN_3(ST, MEM, DW), \
1334 /* Load instructions. */ \
1335 /* Register based. */ \
1336 INSN_3(LDX, MEM, B), \
1337 INSN_3(LDX, MEM, H), \
1338 INSN_3(LDX, MEM, W), \
1339 INSN_3(LDX, MEM, DW), \
1340 /* Immediate based. */ \
1341 INSN_3(LD, IMM, DW)
1342
bpf_opcode_in_insntable(u8 code)1343 bool bpf_opcode_in_insntable(u8 code)
1344 {
1345 #define BPF_INSN_2_TBL(x, y) [BPF_##x | BPF_##y] = true
1346 #define BPF_INSN_3_TBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = true
1347 static const bool public_insntable[256] = {
1348 [0 ... 255] = false,
1349 /* Now overwrite non-defaults ... */
1350 BPF_INSN_MAP(BPF_INSN_2_TBL, BPF_INSN_3_TBL),
1351 /* UAPI exposed, but rewritten opcodes. cBPF carry-over. */
1352 [BPF_LD | BPF_ABS | BPF_B] = true,
1353 [BPF_LD | BPF_ABS | BPF_H] = true,
1354 [BPF_LD | BPF_ABS | BPF_W] = true,
1355 [BPF_LD | BPF_IND | BPF_B] = true,
1356 [BPF_LD | BPF_IND | BPF_H] = true,
1357 [BPF_LD | BPF_IND | BPF_W] = true,
1358 };
1359 #undef BPF_INSN_3_TBL
1360 #undef BPF_INSN_2_TBL
1361 return public_insntable[code];
1362 }
1363
1364 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
bpf_probe_read_kernel(void * dst,u32 size,const void * unsafe_ptr)1365 u64 __weak bpf_probe_read_kernel(void *dst, u32 size, const void *unsafe_ptr)
1366 {
1367 memset(dst, 0, size);
1368 return -EFAULT;
1369 }
1370
1371 /**
1372 * __bpf_prog_run - run eBPF program on a given context
1373 * @regs: is the array of MAX_BPF_EXT_REG eBPF pseudo-registers
1374 * @insn: is the array of eBPF instructions
1375 * @stack: is the eBPF storage stack
1376 *
1377 * Decode and execute eBPF instructions.
1378 */
___bpf_prog_run(u64 * regs,const struct bpf_insn * insn,u64 * stack)1379 static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack)
1380 {
1381 #define BPF_INSN_2_LBL(x, y) [BPF_##x | BPF_##y] = &&x##_##y
1382 #define BPF_INSN_3_LBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = &&x##_##y##_##z
1383 static const void * const jumptable[256] __annotate_jump_table = {
1384 [0 ... 255] = &&default_label,
1385 /* Now overwrite non-defaults ... */
1386 BPF_INSN_MAP(BPF_INSN_2_LBL, BPF_INSN_3_LBL),
1387 /* Non-UAPI available opcodes. */
1388 [BPF_JMP | BPF_CALL_ARGS] = &&JMP_CALL_ARGS,
1389 [BPF_JMP | BPF_TAIL_CALL] = &&JMP_TAIL_CALL,
1390 [BPF_ST | BPF_NOSPEC] = &&ST_NOSPEC,
1391 [BPF_LDX | BPF_PROBE_MEM | BPF_B] = &&LDX_PROBE_MEM_B,
1392 [BPF_LDX | BPF_PROBE_MEM | BPF_H] = &&LDX_PROBE_MEM_H,
1393 [BPF_LDX | BPF_PROBE_MEM | BPF_W] = &&LDX_PROBE_MEM_W,
1394 [BPF_LDX | BPF_PROBE_MEM | BPF_DW] = &&LDX_PROBE_MEM_DW,
1395 };
1396 #undef BPF_INSN_3_LBL
1397 #undef BPF_INSN_2_LBL
1398 u32 tail_call_cnt = 0;
1399
1400 #define CONT ({ insn++; goto select_insn; })
1401 #define CONT_JMP ({ insn++; goto select_insn; })
1402
1403 select_insn:
1404 goto *jumptable[insn->code];
1405
1406 /* Explicitly mask the register-based shift amounts with 63 or 31
1407 * to avoid undefined behavior. Normally this won't affect the
1408 * generated code, for example, in case of native 64 bit archs such
1409 * as x86-64 or arm64, the compiler is optimizing the AND away for
1410 * the interpreter. In case of JITs, each of the JIT backends compiles
1411 * the BPF shift operations to machine instructions which produce
1412 * implementation-defined results in such a case; the resulting
1413 * contents of the register may be arbitrary, but program behaviour
1414 * as a whole remains defined. In other words, in case of JIT backends,
1415 * the AND must /not/ be added to the emitted LSH/RSH/ARSH translation.
1416 */
1417 /* ALU (shifts) */
1418 #define SHT(OPCODE, OP) \
1419 ALU64_##OPCODE##_X: \
1420 DST = DST OP (SRC & 63); \
1421 CONT; \
1422 ALU_##OPCODE##_X: \
1423 DST = (u32) DST OP ((u32) SRC & 31); \
1424 CONT; \
1425 ALU64_##OPCODE##_K: \
1426 DST = DST OP IMM; \
1427 CONT; \
1428 ALU_##OPCODE##_K: \
1429 DST = (u32) DST OP (u32) IMM; \
1430 CONT;
1431 /* ALU (rest) */
1432 #define ALU(OPCODE, OP) \
1433 ALU64_##OPCODE##_X: \
1434 DST = DST OP SRC; \
1435 CONT; \
1436 ALU_##OPCODE##_X: \
1437 DST = (u32) DST OP (u32) SRC; \
1438 CONT; \
1439 ALU64_##OPCODE##_K: \
1440 DST = DST OP IMM; \
1441 CONT; \
1442 ALU_##OPCODE##_K: \
1443 DST = (u32) DST OP (u32) IMM; \
1444 CONT;
1445 ALU(ADD, +)
1446 ALU(SUB, -)
1447 ALU(AND, &)
1448 ALU(OR, |)
1449 ALU(XOR, ^)
1450 ALU(MUL, *)
1451 SHT(LSH, <<)
1452 SHT(RSH, >>)
1453 #undef SHT
1454 #undef ALU
1455 ALU_NEG:
1456 DST = (u32) -DST;
1457 CONT;
1458 ALU64_NEG:
1459 DST = -DST;
1460 CONT;
1461 ALU_MOV_X:
1462 DST = (u32) SRC;
1463 CONT;
1464 ALU_MOV_K:
1465 DST = (u32) IMM;
1466 CONT;
1467 ALU64_MOV_X:
1468 DST = SRC;
1469 CONT;
1470 ALU64_MOV_K:
1471 DST = IMM;
1472 CONT;
1473 LD_IMM_DW:
1474 DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
1475 insn++;
1476 CONT;
1477 ALU_ARSH_X:
1478 DST = (u64) (u32) (((s32) DST) >> (SRC & 31));
1479 CONT;
1480 ALU_ARSH_K:
1481 DST = (u64) (u32) (((s32) DST) >> IMM);
1482 CONT;
1483 ALU64_ARSH_X:
1484 (*(s64 *) &DST) >>= (SRC & 63);
1485 CONT;
1486 ALU64_ARSH_K:
1487 (*(s64 *) &DST) >>= IMM;
1488 CONT;
1489 ALU64_MOD_X:
1490 div64_u64_rem(DST, SRC, &AX);
1491 DST = AX;
1492 CONT;
1493 ALU_MOD_X:
1494 AX = (u32) DST;
1495 DST = do_div(AX, (u32) SRC);
1496 CONT;
1497 ALU64_MOD_K:
1498 div64_u64_rem(DST, IMM, &AX);
1499 DST = AX;
1500 CONT;
1501 ALU_MOD_K:
1502 AX = (u32) DST;
1503 DST = do_div(AX, (u32) IMM);
1504 CONT;
1505 ALU64_DIV_X:
1506 DST = div64_u64(DST, SRC);
1507 CONT;
1508 ALU_DIV_X:
1509 AX = (u32) DST;
1510 do_div(AX, (u32) SRC);
1511 DST = (u32) AX;
1512 CONT;
1513 ALU64_DIV_K:
1514 DST = div64_u64(DST, IMM);
1515 CONT;
1516 ALU_DIV_K:
1517 AX = (u32) DST;
1518 do_div(AX, (u32) IMM);
1519 DST = (u32) AX;
1520 CONT;
1521 ALU_END_TO_BE:
1522 switch (IMM) {
1523 case 16:
1524 DST = (__force u16) cpu_to_be16(DST);
1525 break;
1526 case 32:
1527 DST = (__force u32) cpu_to_be32(DST);
1528 break;
1529 case 64:
1530 DST = (__force u64) cpu_to_be64(DST);
1531 break;
1532 }
1533 CONT;
1534 ALU_END_TO_LE:
1535 switch (IMM) {
1536 case 16:
1537 DST = (__force u16) cpu_to_le16(DST);
1538 break;
1539 case 32:
1540 DST = (__force u32) cpu_to_le32(DST);
1541 break;
1542 case 64:
1543 DST = (__force u64) cpu_to_le64(DST);
1544 break;
1545 }
1546 CONT;
1547
1548 /* CALL */
1549 JMP_CALL:
1550 /* Function call scratches BPF_R1-BPF_R5 registers,
1551 * preserves BPF_R6-BPF_R9, and stores return value
1552 * into BPF_R0.
1553 */
1554 BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
1555 BPF_R4, BPF_R5);
1556 CONT;
1557
1558 JMP_CALL_ARGS:
1559 BPF_R0 = (__bpf_call_base_args + insn->imm)(BPF_R1, BPF_R2,
1560 BPF_R3, BPF_R4,
1561 BPF_R5,
1562 insn + insn->off + 1);
1563 CONT;
1564
1565 JMP_TAIL_CALL: {
1566 struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
1567 struct bpf_array *array = container_of(map, struct bpf_array, map);
1568 struct bpf_prog *prog;
1569 u32 index = BPF_R3;
1570
1571 if (unlikely(index >= array->map.max_entries))
1572 goto out;
1573 if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
1574 goto out;
1575
1576 tail_call_cnt++;
1577
1578 prog = READ_ONCE(array->ptrs[index]);
1579 if (!prog)
1580 goto out;
1581
1582 /* ARG1 at this point is guaranteed to point to CTX from
1583 * the verifier side due to the fact that the tail call is
1584 * handled like a helper, that is, bpf_tail_call_proto,
1585 * where arg1_type is ARG_PTR_TO_CTX.
1586 */
1587 insn = prog->insnsi;
1588 goto select_insn;
1589 out:
1590 CONT;
1591 }
1592 JMP_JA:
1593 insn += insn->off;
1594 CONT;
1595 JMP_EXIT:
1596 return BPF_R0;
1597 /* JMP */
1598 #define COND_JMP(SIGN, OPCODE, CMP_OP) \
1599 JMP_##OPCODE##_X: \
1600 if ((SIGN##64) DST CMP_OP (SIGN##64) SRC) { \
1601 insn += insn->off; \
1602 CONT_JMP; \
1603 } \
1604 CONT; \
1605 JMP32_##OPCODE##_X: \
1606 if ((SIGN##32) DST CMP_OP (SIGN##32) SRC) { \
1607 insn += insn->off; \
1608 CONT_JMP; \
1609 } \
1610 CONT; \
1611 JMP_##OPCODE##_K: \
1612 if ((SIGN##64) DST CMP_OP (SIGN##64) IMM) { \
1613 insn += insn->off; \
1614 CONT_JMP; \
1615 } \
1616 CONT; \
1617 JMP32_##OPCODE##_K: \
1618 if ((SIGN##32) DST CMP_OP (SIGN##32) IMM) { \
1619 insn += insn->off; \
1620 CONT_JMP; \
1621 } \
1622 CONT;
1623 COND_JMP(u, JEQ, ==)
1624 COND_JMP(u, JNE, !=)
1625 COND_JMP(u, JGT, >)
1626 COND_JMP(u, JLT, <)
1627 COND_JMP(u, JGE, >=)
1628 COND_JMP(u, JLE, <=)
1629 COND_JMP(u, JSET, &)
1630 COND_JMP(s, JSGT, >)
1631 COND_JMP(s, JSLT, <)
1632 COND_JMP(s, JSGE, >=)
1633 COND_JMP(s, JSLE, <=)
1634 #undef COND_JMP
1635 /* ST, STX and LDX*/
1636 ST_NOSPEC:
1637 /* Speculation barrier for mitigating Speculative Store Bypass.
1638 * In case of arm64, we rely on the firmware mitigation as
1639 * controlled via the ssbd kernel parameter. Whenever the
1640 * mitigation is enabled, it works for all of the kernel code
1641 * with no need to provide any additional instructions here.
1642 * In case of x86, we use 'lfence' insn for mitigation. We
1643 * reuse preexisting logic from Spectre v1 mitigation that
1644 * happens to produce the required code on x86 for v4 as well.
1645 */
1646 barrier_nospec();
1647 CONT;
1648 #define LDST(SIZEOP, SIZE) \
1649 STX_MEM_##SIZEOP: \
1650 *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \
1651 CONT; \
1652 ST_MEM_##SIZEOP: \
1653 *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \
1654 CONT; \
1655 LDX_MEM_##SIZEOP: \
1656 DST = *(SIZE *)(unsigned long) (SRC + insn->off); \
1657 CONT; \
1658 LDX_PROBE_MEM_##SIZEOP: \
1659 bpf_probe_read_kernel(&DST, sizeof(SIZE), \
1660 (const void *)(long) (SRC + insn->off)); \
1661 DST = *((SIZE *)&DST); \
1662 CONT;
1663
1664 LDST(B, u8)
1665 LDST(H, u16)
1666 LDST(W, u32)
1667 LDST(DW, u64)
1668 #undef LDST
1669
1670 STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
1671 atomic_add((u32) SRC, (atomic_t *)(unsigned long)
1672 (DST + insn->off));
1673 CONT;
1674 STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
1675 atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
1676 (DST + insn->off));
1677 CONT;
1678
1679 default_label:
1680 /* If we ever reach this, we have a bug somewhere. Die hard here
1681 * instead of just returning 0; we could be somewhere in a subprog,
1682 * so execution could continue otherwise which we do /not/ want.
1683 *
1684 * Note, verifier whitelists all opcodes in bpf_opcode_in_insntable().
1685 */
1686 pr_warn("BPF interpreter: unknown opcode %02x\n", insn->code);
1687 BUG_ON(1);
1688 return 0;
1689 }
1690
1691 #define PROG_NAME(stack_size) __bpf_prog_run##stack_size
1692 #define DEFINE_BPF_PROG_RUN(stack_size) \
1693 static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \
1694 { \
1695 u64 stack[stack_size / sizeof(u64)]; \
1696 u64 regs[MAX_BPF_EXT_REG]; \
1697 \
1698 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1699 ARG1 = (u64) (unsigned long) ctx; \
1700 return ___bpf_prog_run(regs, insn, stack); \
1701 }
1702
1703 #define PROG_NAME_ARGS(stack_size) __bpf_prog_run_args##stack_size
1704 #define DEFINE_BPF_PROG_RUN_ARGS(stack_size) \
1705 static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \
1706 const struct bpf_insn *insn) \
1707 { \
1708 u64 stack[stack_size / sizeof(u64)]; \
1709 u64 regs[MAX_BPF_EXT_REG]; \
1710 \
1711 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1712 BPF_R1 = r1; \
1713 BPF_R2 = r2; \
1714 BPF_R3 = r3; \
1715 BPF_R4 = r4; \
1716 BPF_R5 = r5; \
1717 return ___bpf_prog_run(regs, insn, stack); \
1718 }
1719
1720 #define EVAL1(FN, X) FN(X)
1721 #define EVAL2(FN, X, Y...) FN(X) EVAL1(FN, Y)
1722 #define EVAL3(FN, X, Y...) FN(X) EVAL2(FN, Y)
1723 #define EVAL4(FN, X, Y...) FN(X) EVAL3(FN, Y)
1724 #define EVAL5(FN, X, Y...) FN(X) EVAL4(FN, Y)
1725 #define EVAL6(FN, X, Y...) FN(X) EVAL5(FN, Y)
1726
1727 EVAL6(DEFINE_BPF_PROG_RUN, 32, 64, 96, 128, 160, 192);
1728 EVAL6(DEFINE_BPF_PROG_RUN, 224, 256, 288, 320, 352, 384);
1729 EVAL4(DEFINE_BPF_PROG_RUN, 416, 448, 480, 512);
1730
1731 EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 32, 64, 96, 128, 160, 192);
1732 EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 224, 256, 288, 320, 352, 384);
1733 EVAL4(DEFINE_BPF_PROG_RUN_ARGS, 416, 448, 480, 512);
1734
1735 #define PROG_NAME_LIST(stack_size) PROG_NAME(stack_size),
1736
1737 static unsigned int (*interpreters[])(const void *ctx,
1738 const struct bpf_insn *insn) = {
1739 EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1740 EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1741 EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1742 };
1743 #undef PROG_NAME_LIST
1744 #define PROG_NAME_LIST(stack_size) PROG_NAME_ARGS(stack_size),
1745 static u64 (*interpreters_args[])(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5,
1746 const struct bpf_insn *insn) = {
1747 EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1748 EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1749 EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1750 };
1751 #undef PROG_NAME_LIST
1752
bpf_patch_call_args(struct bpf_insn * insn,u32 stack_depth)1753 void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth)
1754 {
1755 stack_depth = max_t(u32, stack_depth, 1);
1756 insn->off = (s16) insn->imm;
1757 insn->imm = interpreters_args[(round_up(stack_depth, 32) / 32) - 1] -
1758 __bpf_call_base_args;
1759 insn->code = BPF_JMP | BPF_CALL_ARGS;
1760 }
1761
1762 #else
__bpf_prog_ret0_warn(const void * ctx,const struct bpf_insn * insn)1763 static unsigned int __bpf_prog_ret0_warn(const void *ctx,
1764 const struct bpf_insn *insn)
1765 {
1766 /* If this handler ever gets executed, then BPF_JIT_ALWAYS_ON
1767 * is not working properly, so warn about it!
1768 */
1769 WARN_ON_ONCE(1);
1770 return 0;
1771 }
1772 #endif
1773
bpf_prog_array_compatible(struct bpf_array * array,const struct bpf_prog * fp)1774 bool bpf_prog_array_compatible(struct bpf_array *array,
1775 const struct bpf_prog *fp)
1776 {
1777 bool ret;
1778
1779 if (fp->kprobe_override)
1780 return false;
1781
1782 spin_lock(&array->aux->owner.lock);
1783
1784 if (!array->aux->owner.type) {
1785 /* There's no owner yet where we could check for
1786 * compatibility.
1787 */
1788 array->aux->owner.type = fp->type;
1789 array->aux->owner.jited = fp->jited;
1790 ret = true;
1791 } else {
1792 ret = array->aux->owner.type == fp->type &&
1793 array->aux->owner.jited == fp->jited;
1794 }
1795 spin_unlock(&array->aux->owner.lock);
1796 return ret;
1797 }
1798
bpf_check_tail_call(const struct bpf_prog * fp)1799 static int bpf_check_tail_call(const struct bpf_prog *fp)
1800 {
1801 struct bpf_prog_aux *aux = fp->aux;
1802 int i, ret = 0;
1803
1804 mutex_lock(&aux->used_maps_mutex);
1805 for (i = 0; i < aux->used_map_cnt; i++) {
1806 struct bpf_map *map = aux->used_maps[i];
1807 struct bpf_array *array;
1808
1809 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1810 continue;
1811
1812 array = container_of(map, struct bpf_array, map);
1813 if (!bpf_prog_array_compatible(array, fp)) {
1814 ret = -EINVAL;
1815 goto out;
1816 }
1817 }
1818
1819 out:
1820 mutex_unlock(&aux->used_maps_mutex);
1821 return ret;
1822 }
1823
bpf_prog_select_func(struct bpf_prog * fp)1824 static void bpf_prog_select_func(struct bpf_prog *fp)
1825 {
1826 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
1827 u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1);
1828
1829 fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1];
1830 #else
1831 fp->bpf_func = __bpf_prog_ret0_warn;
1832 #endif
1833 }
1834
1835 /**
1836 * bpf_prog_select_runtime - select exec runtime for BPF program
1837 * @fp: bpf_prog populated with internal BPF program
1838 * @err: pointer to error variable
1839 *
1840 * Try to JIT eBPF program, if JIT is not available, use interpreter.
1841 * The BPF program will be executed via BPF_PROG_RUN() macro.
1842 */
bpf_prog_select_runtime(struct bpf_prog * fp,int * err)1843 struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
1844 {
1845 /* In case of BPF to BPF calls, verifier did all the prep
1846 * work with regards to JITing, etc.
1847 */
1848 if (fp->bpf_func)
1849 goto finalize;
1850
1851 bpf_prog_select_func(fp);
1852
1853 /* eBPF JITs can rewrite the program in case constant
1854 * blinding is active. However, in case of error during
1855 * blinding, bpf_int_jit_compile() must always return a
1856 * valid program, which in this case would simply not
1857 * be JITed, but falls back to the interpreter.
1858 */
1859 if (!bpf_prog_is_dev_bound(fp->aux)) {
1860 *err = bpf_prog_alloc_jited_linfo(fp);
1861 if (*err)
1862 return fp;
1863
1864 fp = bpf_int_jit_compile(fp);
1865 if (!fp->jited) {
1866 bpf_prog_free_jited_linfo(fp);
1867 #ifdef CONFIG_BPF_JIT_ALWAYS_ON
1868 *err = -ENOTSUPP;
1869 return fp;
1870 #endif
1871 } else {
1872 bpf_prog_free_unused_jited_linfo(fp);
1873 }
1874 } else {
1875 *err = bpf_prog_offload_compile(fp);
1876 if (*err)
1877 return fp;
1878 }
1879
1880 finalize:
1881 bpf_prog_lock_ro(fp);
1882
1883 /* The tail call compatibility check can only be done at
1884 * this late stage as we need to determine, if we deal
1885 * with JITed or non JITed program concatenations and not
1886 * all eBPF JITs might immediately support all features.
1887 */
1888 *err = bpf_check_tail_call(fp);
1889
1890 return fp;
1891 }
1892 EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
1893
__bpf_prog_ret1(const void * ctx,const struct bpf_insn * insn)1894 static unsigned int __bpf_prog_ret1(const void *ctx,
1895 const struct bpf_insn *insn)
1896 {
1897 return 1;
1898 }
1899
1900 static struct bpf_prog_dummy {
1901 struct bpf_prog prog;
1902 } dummy_bpf_prog = {
1903 .prog = {
1904 .bpf_func = __bpf_prog_ret1,
1905 },
1906 };
1907
1908 /* to avoid allocating empty bpf_prog_array for cgroups that
1909 * don't have bpf program attached use one global 'empty_prog_array'
1910 * It will not be modified the caller of bpf_prog_array_alloc()
1911 * (since caller requested prog_cnt == 0)
1912 * that pointer should be 'freed' by bpf_prog_array_free()
1913 */
1914 static struct {
1915 struct bpf_prog_array hdr;
1916 struct bpf_prog *null_prog;
1917 } empty_prog_array = {
1918 .null_prog = NULL,
1919 };
1920
bpf_prog_array_alloc(u32 prog_cnt,gfp_t flags)1921 struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags)
1922 {
1923 if (prog_cnt)
1924 return kzalloc(sizeof(struct bpf_prog_array) +
1925 sizeof(struct bpf_prog_array_item) *
1926 (prog_cnt + 1),
1927 flags);
1928
1929 return &empty_prog_array.hdr;
1930 }
1931
bpf_prog_array_free(struct bpf_prog_array * progs)1932 void bpf_prog_array_free(struct bpf_prog_array *progs)
1933 {
1934 if (!progs || progs == &empty_prog_array.hdr)
1935 return;
1936 kfree_rcu(progs, rcu);
1937 }
1938
bpf_prog_array_length(struct bpf_prog_array * array)1939 int bpf_prog_array_length(struct bpf_prog_array *array)
1940 {
1941 struct bpf_prog_array_item *item;
1942 u32 cnt = 0;
1943
1944 for (item = array->items; item->prog; item++)
1945 if (item->prog != &dummy_bpf_prog.prog)
1946 cnt++;
1947 return cnt;
1948 }
1949
bpf_prog_array_is_empty(struct bpf_prog_array * array)1950 bool bpf_prog_array_is_empty(struct bpf_prog_array *array)
1951 {
1952 struct bpf_prog_array_item *item;
1953
1954 for (item = array->items; item->prog; item++)
1955 if (item->prog != &dummy_bpf_prog.prog)
1956 return false;
1957 return true;
1958 }
1959
bpf_prog_array_copy_core(struct bpf_prog_array * array,u32 * prog_ids,u32 request_cnt)1960 static bool bpf_prog_array_copy_core(struct bpf_prog_array *array,
1961 u32 *prog_ids,
1962 u32 request_cnt)
1963 {
1964 struct bpf_prog_array_item *item;
1965 int i = 0;
1966
1967 for (item = array->items; item->prog; item++) {
1968 if (item->prog == &dummy_bpf_prog.prog)
1969 continue;
1970 prog_ids[i] = item->prog->aux->id;
1971 if (++i == request_cnt) {
1972 item++;
1973 break;
1974 }
1975 }
1976
1977 return !!(item->prog);
1978 }
1979
bpf_prog_array_copy_to_user(struct bpf_prog_array * array,__u32 __user * prog_ids,u32 cnt)1980 int bpf_prog_array_copy_to_user(struct bpf_prog_array *array,
1981 __u32 __user *prog_ids, u32 cnt)
1982 {
1983 unsigned long err = 0;
1984 bool nospc;
1985 u32 *ids;
1986
1987 /* users of this function are doing:
1988 * cnt = bpf_prog_array_length();
1989 * if (cnt > 0)
1990 * bpf_prog_array_copy_to_user(..., cnt);
1991 * so below kcalloc doesn't need extra cnt > 0 check.
1992 */
1993 ids = kcalloc(cnt, sizeof(u32), GFP_USER | __GFP_NOWARN);
1994 if (!ids)
1995 return -ENOMEM;
1996 nospc = bpf_prog_array_copy_core(array, ids, cnt);
1997 err = copy_to_user(prog_ids, ids, cnt * sizeof(u32));
1998 kfree(ids);
1999 if (err)
2000 return -EFAULT;
2001 if (nospc)
2002 return -ENOSPC;
2003 return 0;
2004 }
2005
bpf_prog_array_delete_safe(struct bpf_prog_array * array,struct bpf_prog * old_prog)2006 void bpf_prog_array_delete_safe(struct bpf_prog_array *array,
2007 struct bpf_prog *old_prog)
2008 {
2009 struct bpf_prog_array_item *item;
2010
2011 for (item = array->items; item->prog; item++)
2012 if (item->prog == old_prog) {
2013 WRITE_ONCE(item->prog, &dummy_bpf_prog.prog);
2014 break;
2015 }
2016 }
2017
2018 /**
2019 * bpf_prog_array_delete_safe_at() - Replaces the program at the given
2020 * index into the program array with
2021 * a dummy no-op program.
2022 * @array: a bpf_prog_array
2023 * @index: the index of the program to replace
2024 *
2025 * Skips over dummy programs, by not counting them, when calculating
2026 * the position of the program to replace.
2027 *
2028 * Return:
2029 * * 0 - Success
2030 * * -EINVAL - Invalid index value. Must be a non-negative integer.
2031 * * -ENOENT - Index out of range
2032 */
bpf_prog_array_delete_safe_at(struct bpf_prog_array * array,int index)2033 int bpf_prog_array_delete_safe_at(struct bpf_prog_array *array, int index)
2034 {
2035 return bpf_prog_array_update_at(array, index, &dummy_bpf_prog.prog);
2036 }
2037
2038 /**
2039 * bpf_prog_array_update_at() - Updates the program at the given index
2040 * into the program array.
2041 * @array: a bpf_prog_array
2042 * @index: the index of the program to update
2043 * @prog: the program to insert into the array
2044 *
2045 * Skips over dummy programs, by not counting them, when calculating
2046 * the position of the program to update.
2047 *
2048 * Return:
2049 * * 0 - Success
2050 * * -EINVAL - Invalid index value. Must be a non-negative integer.
2051 * * -ENOENT - Index out of range
2052 */
bpf_prog_array_update_at(struct bpf_prog_array * array,int index,struct bpf_prog * prog)2053 int bpf_prog_array_update_at(struct bpf_prog_array *array, int index,
2054 struct bpf_prog *prog)
2055 {
2056 struct bpf_prog_array_item *item;
2057
2058 if (unlikely(index < 0))
2059 return -EINVAL;
2060
2061 for (item = array->items; item->prog; item++) {
2062 if (item->prog == &dummy_bpf_prog.prog)
2063 continue;
2064 if (!index) {
2065 WRITE_ONCE(item->prog, prog);
2066 return 0;
2067 }
2068 index--;
2069 }
2070 return -ENOENT;
2071 }
2072
bpf_prog_array_copy(struct bpf_prog_array * old_array,struct bpf_prog * exclude_prog,struct bpf_prog * include_prog,struct bpf_prog_array ** new_array)2073 int bpf_prog_array_copy(struct bpf_prog_array *old_array,
2074 struct bpf_prog *exclude_prog,
2075 struct bpf_prog *include_prog,
2076 struct bpf_prog_array **new_array)
2077 {
2078 int new_prog_cnt, carry_prog_cnt = 0;
2079 struct bpf_prog_array_item *existing;
2080 struct bpf_prog_array *array;
2081 bool found_exclude = false;
2082 int new_prog_idx = 0;
2083
2084 /* Figure out how many existing progs we need to carry over to
2085 * the new array.
2086 */
2087 if (old_array) {
2088 existing = old_array->items;
2089 for (; existing->prog; existing++) {
2090 if (existing->prog == exclude_prog) {
2091 found_exclude = true;
2092 continue;
2093 }
2094 if (existing->prog != &dummy_bpf_prog.prog)
2095 carry_prog_cnt++;
2096 if (existing->prog == include_prog)
2097 return -EEXIST;
2098 }
2099 }
2100
2101 if (exclude_prog && !found_exclude)
2102 return -ENOENT;
2103
2104 /* How many progs (not NULL) will be in the new array? */
2105 new_prog_cnt = carry_prog_cnt;
2106 if (include_prog)
2107 new_prog_cnt += 1;
2108
2109 /* Do we have any prog (not NULL) in the new array? */
2110 if (!new_prog_cnt) {
2111 *new_array = NULL;
2112 return 0;
2113 }
2114
2115 /* +1 as the end of prog_array is marked with NULL */
2116 array = bpf_prog_array_alloc(new_prog_cnt + 1, GFP_KERNEL);
2117 if (!array)
2118 return -ENOMEM;
2119
2120 /* Fill in the new prog array */
2121 if (carry_prog_cnt) {
2122 existing = old_array->items;
2123 for (; existing->prog; existing++)
2124 if (existing->prog != exclude_prog &&
2125 existing->prog != &dummy_bpf_prog.prog) {
2126 array->items[new_prog_idx++].prog =
2127 existing->prog;
2128 }
2129 }
2130 if (include_prog)
2131 array->items[new_prog_idx++].prog = include_prog;
2132 array->items[new_prog_idx].prog = NULL;
2133 *new_array = array;
2134 return 0;
2135 }
2136
bpf_prog_array_copy_info(struct bpf_prog_array * array,u32 * prog_ids,u32 request_cnt,u32 * prog_cnt)2137 int bpf_prog_array_copy_info(struct bpf_prog_array *array,
2138 u32 *prog_ids, u32 request_cnt,
2139 u32 *prog_cnt)
2140 {
2141 u32 cnt = 0;
2142
2143 if (array)
2144 cnt = bpf_prog_array_length(array);
2145
2146 *prog_cnt = cnt;
2147
2148 /* return early if user requested only program count or nothing to copy */
2149 if (!request_cnt || !cnt)
2150 return 0;
2151
2152 /* this function is called under trace/bpf_trace.c: bpf_event_mutex */
2153 return bpf_prog_array_copy_core(array, prog_ids, request_cnt) ? -ENOSPC
2154 : 0;
2155 }
2156
__bpf_free_used_maps(struct bpf_prog_aux * aux,struct bpf_map ** used_maps,u32 len)2157 void __bpf_free_used_maps(struct bpf_prog_aux *aux,
2158 struct bpf_map **used_maps, u32 len)
2159 {
2160 struct bpf_map *map;
2161 u32 i;
2162
2163 for (i = 0; i < len; i++) {
2164 map = used_maps[i];
2165 if (map->ops->map_poke_untrack)
2166 map->ops->map_poke_untrack(map, aux);
2167 bpf_map_put(map);
2168 }
2169 }
2170
bpf_free_used_maps(struct bpf_prog_aux * aux)2171 static void bpf_free_used_maps(struct bpf_prog_aux *aux)
2172 {
2173 __bpf_free_used_maps(aux, aux->used_maps, aux->used_map_cnt);
2174 kfree(aux->used_maps);
2175 }
2176
bpf_prog_free_deferred(struct work_struct * work)2177 static void bpf_prog_free_deferred(struct work_struct *work)
2178 {
2179 struct bpf_prog_aux *aux;
2180 int i;
2181
2182 aux = container_of(work, struct bpf_prog_aux, work);
2183 bpf_free_used_maps(aux);
2184 if (bpf_prog_is_dev_bound(aux))
2185 bpf_prog_offload_destroy(aux->prog);
2186 #ifdef CONFIG_PERF_EVENTS
2187 if (aux->prog->has_callchain_buf)
2188 put_callchain_buffers();
2189 #endif
2190 if (aux->dst_trampoline)
2191 bpf_trampoline_put(aux->dst_trampoline);
2192 for (i = 0; i < aux->func_cnt; i++) {
2193 /* We can just unlink the subprog poke descriptor table as
2194 * it was originally linked to the main program and is also
2195 * released along with it.
2196 */
2197 aux->func[i]->aux->poke_tab = NULL;
2198 bpf_jit_free(aux->func[i]);
2199 }
2200 if (aux->func_cnt) {
2201 kfree(aux->func);
2202 bpf_prog_unlock_free(aux->prog);
2203 } else {
2204 bpf_jit_free(aux->prog);
2205 }
2206 }
2207
2208 /* Free internal BPF program */
bpf_prog_free(struct bpf_prog * fp)2209 void bpf_prog_free(struct bpf_prog *fp)
2210 {
2211 struct bpf_prog_aux *aux = fp->aux;
2212
2213 if (aux->dst_prog)
2214 bpf_prog_put(aux->dst_prog);
2215 INIT_WORK(&aux->work, bpf_prog_free_deferred);
2216 schedule_work(&aux->work);
2217 }
2218 EXPORT_SYMBOL_GPL(bpf_prog_free);
2219
2220 /* RNG for unpriviledged user space with separated state from prandom_u32(). */
2221 static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
2222
bpf_user_rnd_init_once(void)2223 void bpf_user_rnd_init_once(void)
2224 {
2225 prandom_init_once(&bpf_user_rnd_state);
2226 }
2227
BPF_CALL_0(bpf_user_rnd_u32)2228 BPF_CALL_0(bpf_user_rnd_u32)
2229 {
2230 /* Should someone ever have the rather unwise idea to use some
2231 * of the registers passed into this function, then note that
2232 * this function is called from native eBPF and classic-to-eBPF
2233 * transformations. Register assignments from both sides are
2234 * different, f.e. classic always sets fn(ctx, A, X) here.
2235 */
2236 struct rnd_state *state;
2237 u32 res;
2238
2239 state = &get_cpu_var(bpf_user_rnd_state);
2240 res = prandom_u32_state(state);
2241 put_cpu_var(bpf_user_rnd_state);
2242
2243 return res;
2244 }
2245
BPF_CALL_0(bpf_get_raw_cpu_id)2246 BPF_CALL_0(bpf_get_raw_cpu_id)
2247 {
2248 return raw_smp_processor_id();
2249 }
2250
2251 /* Weak definitions of helper functions in case we don't have bpf syscall. */
2252 const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
2253 const struct bpf_func_proto bpf_map_update_elem_proto __weak;
2254 const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
2255 const struct bpf_func_proto bpf_map_push_elem_proto __weak;
2256 const struct bpf_func_proto bpf_map_pop_elem_proto __weak;
2257 const struct bpf_func_proto bpf_map_peek_elem_proto __weak;
2258 const struct bpf_func_proto bpf_spin_lock_proto __weak;
2259 const struct bpf_func_proto bpf_spin_unlock_proto __weak;
2260 const struct bpf_func_proto bpf_jiffies64_proto __weak;
2261
2262 const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
2263 const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
2264 const struct bpf_func_proto bpf_get_numa_node_id_proto __weak;
2265 const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
2266 const struct bpf_func_proto bpf_ktime_get_boot_ns_proto __weak;
2267
2268 const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
2269 const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
2270 const struct bpf_func_proto bpf_get_current_comm_proto __weak;
2271 const struct bpf_func_proto bpf_get_current_cgroup_id_proto __weak;
2272 const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto __weak;
2273 const struct bpf_func_proto bpf_get_local_storage_proto __weak;
2274 const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto __weak;
2275 const struct bpf_func_proto bpf_snprintf_btf_proto __weak;
2276 const struct bpf_func_proto bpf_seq_printf_btf_proto __weak;
2277
bpf_get_trace_printk_proto(void)2278 const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
2279 {
2280 return NULL;
2281 }
2282
2283 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)2284 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
2285 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
2286 {
2287 return -ENOTSUPP;
2288 }
2289 EXPORT_SYMBOL_GPL(bpf_event_output);
2290
2291 /* Always built-in helper functions. */
2292 const struct bpf_func_proto bpf_tail_call_proto = {
2293 .func = NULL,
2294 .gpl_only = false,
2295 .ret_type = RET_VOID,
2296 .arg1_type = ARG_PTR_TO_CTX,
2297 .arg2_type = ARG_CONST_MAP_PTR,
2298 .arg3_type = ARG_ANYTHING,
2299 };
2300
2301 /* Stub for JITs that only support cBPF. eBPF programs are interpreted.
2302 * It is encouraged to implement bpf_int_jit_compile() instead, so that
2303 * eBPF and implicitly also cBPF can get JITed!
2304 */
bpf_int_jit_compile(struct bpf_prog * prog)2305 struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
2306 {
2307 return prog;
2308 }
2309
2310 /* Stub for JITs that support eBPF. All cBPF code gets transformed into
2311 * eBPF by the kernel and is later compiled by bpf_int_jit_compile().
2312 */
bpf_jit_compile(struct bpf_prog * prog)2313 void __weak bpf_jit_compile(struct bpf_prog *prog)
2314 {
2315 }
2316
bpf_helper_changes_pkt_data(void * func)2317 bool __weak bpf_helper_changes_pkt_data(void *func)
2318 {
2319 return false;
2320 }
2321
2322 /* Return TRUE if the JIT backend wants verifier to enable sub-register usage
2323 * analysis code and wants explicit zero extension inserted by verifier.
2324 * Otherwise, return FALSE.
2325 */
bpf_jit_needs_zext(void)2326 bool __weak bpf_jit_needs_zext(void)
2327 {
2328 return false;
2329 }
2330
2331 /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
2332 * skb_copy_bits(), so provide a weak definition of it for NET-less config.
2333 */
skb_copy_bits(const struct sk_buff * skb,int offset,void * to,int len)2334 int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
2335 int len)
2336 {
2337 return -EFAULT;
2338 }
2339
bpf_arch_text_poke(void * ip,enum bpf_text_poke_type t,void * addr1,void * addr2)2340 int __weak bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
2341 void *addr1, void *addr2)
2342 {
2343 return -ENOTSUPP;
2344 }
2345
2346 DEFINE_STATIC_KEY_FALSE(bpf_stats_enabled_key);
2347 EXPORT_SYMBOL(bpf_stats_enabled_key);
2348
2349 /* All definitions of tracepoints related to BPF. */
2350 #define CREATE_TRACE_POINTS
2351 #include <linux/bpf_trace.h>
2352
2353 EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception);
2354 EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_bulk_tx);
2355