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