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