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