]>
Commit | Line | Data |
---|---|---|
f5bffecd AS |
1 | /* |
2 | * Linux Socket Filter - Kernel level socket filtering | |
3 | * | |
4 | * Based on the design of the Berkeley Packet Filter. The new | |
5 | * internal format has been designed by PLUMgrid: | |
6 | * | |
7 | * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com | |
8 | * | |
9 | * Authors: | |
10 | * | |
11 | * Jay Schulist <jschlst@samba.org> | |
12 | * Alexei Starovoitov <ast@plumgrid.com> | |
13 | * Daniel Borkmann <dborkman@redhat.com> | |
14 | * | |
15 | * This program is free software; you can redistribute it and/or | |
16 | * modify it under the terms of the GNU General Public License | |
17 | * as published by the Free Software Foundation; either version | |
18 | * 2 of the License, or (at your option) any later version. | |
19 | * | |
20 | * Andi Kleen - Fix a few bad bugs and races. | |
4df95ff4 | 21 | * Kris Katterjohn - Added many additional checks in bpf_check_classic() |
f5bffecd | 22 | */ |
738cbe72 | 23 | |
f5bffecd AS |
24 | #include <linux/filter.h> |
25 | #include <linux/skbuff.h> | |
60a3b225 | 26 | #include <linux/vmalloc.h> |
738cbe72 DB |
27 | #include <linux/random.h> |
28 | #include <linux/moduleloader.h> | |
09756af4 | 29 | #include <linux/bpf.h> |
39853cc0 | 30 | #include <linux/frame.h> |
74451e66 DB |
31 | #include <linux/rbtree_latch.h> |
32 | #include <linux/kallsyms.h> | |
33 | #include <linux/rcupdate.h> | |
c195651e | 34 | #include <linux/perf_event.h> |
f5bffecd | 35 | |
3324b584 DB |
36 | #include <asm/unaligned.h> |
37 | ||
f5bffecd AS |
38 | /* Registers */ |
39 | #define BPF_R0 regs[BPF_REG_0] | |
40 | #define BPF_R1 regs[BPF_REG_1] | |
41 | #define BPF_R2 regs[BPF_REG_2] | |
42 | #define BPF_R3 regs[BPF_REG_3] | |
43 | #define BPF_R4 regs[BPF_REG_4] | |
44 | #define BPF_R5 regs[BPF_REG_5] | |
45 | #define BPF_R6 regs[BPF_REG_6] | |
46 | #define BPF_R7 regs[BPF_REG_7] | |
47 | #define BPF_R8 regs[BPF_REG_8] | |
48 | #define BPF_R9 regs[BPF_REG_9] | |
49 | #define BPF_R10 regs[BPF_REG_10] | |
50 | ||
51 | /* Named registers */ | |
52 | #define DST regs[insn->dst_reg] | |
53 | #define SRC regs[insn->src_reg] | |
54 | #define FP regs[BPF_REG_FP] | |
55 | #define ARG1 regs[BPF_REG_ARG1] | |
56 | #define CTX regs[BPF_REG_CTX] | |
57 | #define IMM insn->imm | |
58 | ||
59 | /* No hurry in this branch | |
60 | * | |
61 | * Exported for the bpf jit load helper. | |
62 | */ | |
63 | void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size) | |
64 | { | |
65 | u8 *ptr = NULL; | |
66 | ||
67 | if (k >= SKF_NET_OFF) | |
68 | ptr = skb_network_header(skb) + k - SKF_NET_OFF; | |
69 | else if (k >= SKF_LL_OFF) | |
70 | ptr = skb_mac_header(skb) + k - SKF_LL_OFF; | |
3324b584 | 71 | |
f5bffecd AS |
72 | if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb)) |
73 | return ptr; | |
74 | ||
75 | return NULL; | |
76 | } | |
77 | ||
60a3b225 DB |
78 | struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags) |
79 | { | |
19809c2d | 80 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags; |
09756af4 | 81 | struct bpf_prog_aux *aux; |
60a3b225 DB |
82 | struct bpf_prog *fp; |
83 | ||
84 | size = round_up(size, PAGE_SIZE); | |
85 | fp = __vmalloc(size, gfp_flags, PAGE_KERNEL); | |
86 | if (fp == NULL) | |
87 | return NULL; | |
88 | ||
09756af4 AS |
89 | aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags); |
90 | if (aux == NULL) { | |
60a3b225 DB |
91 | vfree(fp); |
92 | return NULL; | |
93 | } | |
94 | ||
95 | fp->pages = size / PAGE_SIZE; | |
09756af4 | 96 | fp->aux = aux; |
e9d8afa9 | 97 | fp->aux->prog = fp; |
60b58afc | 98 | fp->jit_requested = ebpf_jit_enabled(); |
60a3b225 | 99 | |
74451e66 DB |
100 | INIT_LIST_HEAD_RCU(&fp->aux->ksym_lnode); |
101 | ||
60a3b225 DB |
102 | return fp; |
103 | } | |
104 | EXPORT_SYMBOL_GPL(bpf_prog_alloc); | |
105 | ||
106 | struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size, | |
107 | gfp_t gfp_extra_flags) | |
108 | { | |
19809c2d | 109 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags; |
60a3b225 | 110 | struct bpf_prog *fp; |
5ccb071e DB |
111 | u32 pages, delta; |
112 | int ret; | |
60a3b225 DB |
113 | |
114 | BUG_ON(fp_old == NULL); | |
115 | ||
116 | size = round_up(size, PAGE_SIZE); | |
5ccb071e DB |
117 | pages = size / PAGE_SIZE; |
118 | if (pages <= fp_old->pages) | |
60a3b225 DB |
119 | return fp_old; |
120 | ||
5ccb071e DB |
121 | delta = pages - fp_old->pages; |
122 | ret = __bpf_prog_charge(fp_old->aux->user, delta); | |
123 | if (ret) | |
124 | return NULL; | |
125 | ||
60a3b225 | 126 | fp = __vmalloc(size, gfp_flags, PAGE_KERNEL); |
5ccb071e DB |
127 | if (fp == NULL) { |
128 | __bpf_prog_uncharge(fp_old->aux->user, delta); | |
129 | } else { | |
60a3b225 | 130 | memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE); |
5ccb071e | 131 | fp->pages = pages; |
e9d8afa9 | 132 | fp->aux->prog = fp; |
60a3b225 | 133 | |
09756af4 | 134 | /* We keep fp->aux from fp_old around in the new |
60a3b225 DB |
135 | * reallocated structure. |
136 | */ | |
09756af4 | 137 | fp_old->aux = NULL; |
60a3b225 DB |
138 | __bpf_prog_free(fp_old); |
139 | } | |
140 | ||
141 | return fp; | |
142 | } | |
60a3b225 DB |
143 | |
144 | void __bpf_prog_free(struct bpf_prog *fp) | |
145 | { | |
09756af4 | 146 | kfree(fp->aux); |
60a3b225 DB |
147 | vfree(fp); |
148 | } | |
60a3b225 | 149 | |
f1f7714e | 150 | int bpf_prog_calc_tag(struct bpf_prog *fp) |
7bd509e3 DB |
151 | { |
152 | const u32 bits_offset = SHA_MESSAGE_BYTES - sizeof(__be64); | |
f1f7714e DB |
153 | u32 raw_size = bpf_prog_tag_scratch_size(fp); |
154 | u32 digest[SHA_DIGEST_WORDS]; | |
aafe6ae9 | 155 | u32 ws[SHA_WORKSPACE_WORDS]; |
7bd509e3 | 156 | u32 i, bsize, psize, blocks; |
aafe6ae9 | 157 | struct bpf_insn *dst; |
7bd509e3 | 158 | bool was_ld_map; |
aafe6ae9 | 159 | u8 *raw, *todo; |
7bd509e3 DB |
160 | __be32 *result; |
161 | __be64 *bits; | |
162 | ||
aafe6ae9 DB |
163 | raw = vmalloc(raw_size); |
164 | if (!raw) | |
165 | return -ENOMEM; | |
166 | ||
f1f7714e | 167 | sha_init(digest); |
7bd509e3 DB |
168 | memset(ws, 0, sizeof(ws)); |
169 | ||
170 | /* We need to take out the map fd for the digest calculation | |
171 | * since they are unstable from user space side. | |
172 | */ | |
aafe6ae9 | 173 | dst = (void *)raw; |
7bd509e3 DB |
174 | for (i = 0, was_ld_map = false; i < fp->len; i++) { |
175 | dst[i] = fp->insnsi[i]; | |
176 | if (!was_ld_map && | |
177 | dst[i].code == (BPF_LD | BPF_IMM | BPF_DW) && | |
178 | dst[i].src_reg == BPF_PSEUDO_MAP_FD) { | |
179 | was_ld_map = true; | |
180 | dst[i].imm = 0; | |
181 | } else if (was_ld_map && | |
182 | dst[i].code == 0 && | |
183 | dst[i].dst_reg == 0 && | |
184 | dst[i].src_reg == 0 && | |
185 | dst[i].off == 0) { | |
186 | was_ld_map = false; | |
187 | dst[i].imm = 0; | |
188 | } else { | |
189 | was_ld_map = false; | |
190 | } | |
191 | } | |
192 | ||
aafe6ae9 DB |
193 | psize = bpf_prog_insn_size(fp); |
194 | memset(&raw[psize], 0, raw_size - psize); | |
7bd509e3 DB |
195 | raw[psize++] = 0x80; |
196 | ||
197 | bsize = round_up(psize, SHA_MESSAGE_BYTES); | |
198 | blocks = bsize / SHA_MESSAGE_BYTES; | |
aafe6ae9 | 199 | todo = raw; |
7bd509e3 DB |
200 | if (bsize - psize >= sizeof(__be64)) { |
201 | bits = (__be64 *)(todo + bsize - sizeof(__be64)); | |
202 | } else { | |
203 | bits = (__be64 *)(todo + bsize + bits_offset); | |
204 | blocks++; | |
205 | } | |
206 | *bits = cpu_to_be64((psize - 1) << 3); | |
207 | ||
208 | while (blocks--) { | |
f1f7714e | 209 | sha_transform(digest, todo, ws); |
7bd509e3 DB |
210 | todo += SHA_MESSAGE_BYTES; |
211 | } | |
212 | ||
f1f7714e | 213 | result = (__force __be32 *)digest; |
7bd509e3 | 214 | for (i = 0; i < SHA_DIGEST_WORDS; i++) |
f1f7714e DB |
215 | result[i] = cpu_to_be32(digest[i]); |
216 | memcpy(fp->tag, result, sizeof(fp->tag)); | |
aafe6ae9 DB |
217 | |
218 | vfree(raw); | |
219 | return 0; | |
7bd509e3 DB |
220 | } |
221 | ||
050fad7c DB |
222 | static int bpf_adj_delta_to_imm(struct bpf_insn *insn, u32 pos, u32 delta, |
223 | u32 curr, const bool probe_pass) | |
c237ee5e | 224 | { |
050fad7c DB |
225 | const s64 imm_min = S32_MIN, imm_max = S32_MAX; |
226 | s64 imm = insn->imm; | |
227 | ||
228 | if (curr < pos && curr + imm + 1 > pos) | |
229 | imm += delta; | |
230 | else if (curr > pos + delta && curr + imm + 1 <= pos + delta) | |
231 | imm -= delta; | |
232 | if (imm < imm_min || imm > imm_max) | |
233 | return -ERANGE; | |
234 | if (!probe_pass) | |
235 | insn->imm = imm; | |
236 | return 0; | |
237 | } | |
238 | ||
239 | static int bpf_adj_delta_to_off(struct bpf_insn *insn, u32 pos, u32 delta, | |
240 | u32 curr, const bool probe_pass) | |
241 | { | |
242 | const s32 off_min = S16_MIN, off_max = S16_MAX; | |
243 | s32 off = insn->off; | |
244 | ||
245 | if (curr < pos && curr + off + 1 > pos) | |
246 | off += delta; | |
247 | else if (curr > pos + delta && curr + off + 1 <= pos + delta) | |
248 | off -= delta; | |
249 | if (off < off_min || off > off_max) | |
250 | return -ERANGE; | |
251 | if (!probe_pass) | |
252 | insn->off = off; | |
253 | return 0; | |
254 | } | |
255 | ||
256 | static int bpf_adj_branches(struct bpf_prog *prog, u32 pos, u32 delta, | |
257 | const bool probe_pass) | |
258 | { | |
259 | u32 i, insn_cnt = prog->len + (probe_pass ? delta : 0); | |
c237ee5e | 260 | struct bpf_insn *insn = prog->insnsi; |
050fad7c | 261 | int ret = 0; |
c237ee5e DB |
262 | |
263 | for (i = 0; i < insn_cnt; i++, insn++) { | |
050fad7c DB |
264 | u8 code; |
265 | ||
266 | /* In the probing pass we still operate on the original, | |
267 | * unpatched image in order to check overflows before we | |
268 | * do any other adjustments. Therefore skip the patchlet. | |
269 | */ | |
270 | if (probe_pass && i == pos) { | |
271 | i += delta + 1; | |
272 | insn++; | |
273 | } | |
1ea47e01 | 274 | code = insn->code; |
050fad7c DB |
275 | if (BPF_CLASS(code) != BPF_JMP || |
276 | BPF_OP(code) == BPF_EXIT) | |
1ea47e01 | 277 | continue; |
050fad7c | 278 | /* Adjust offset of jmps if we cross patch boundaries. */ |
1ea47e01 | 279 | if (BPF_OP(code) == BPF_CALL) { |
050fad7c | 280 | if (insn->src_reg != BPF_PSEUDO_CALL) |
1ea47e01 | 281 | continue; |
050fad7c DB |
282 | ret = bpf_adj_delta_to_imm(insn, pos, delta, i, |
283 | probe_pass); | |
1ea47e01 | 284 | } else { |
050fad7c DB |
285 | ret = bpf_adj_delta_to_off(insn, pos, delta, i, |
286 | probe_pass); | |
1ea47e01 | 287 | } |
050fad7c DB |
288 | if (ret) |
289 | break; | |
c237ee5e | 290 | } |
050fad7c DB |
291 | |
292 | return ret; | |
c237ee5e DB |
293 | } |
294 | ||
295 | struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off, | |
296 | const struct bpf_insn *patch, u32 len) | |
297 | { | |
298 | u32 insn_adj_cnt, insn_rest, insn_delta = len - 1; | |
050fad7c | 299 | const u32 cnt_max = S16_MAX; |
c237ee5e DB |
300 | struct bpf_prog *prog_adj; |
301 | ||
302 | /* Since our patchlet doesn't expand the image, we're done. */ | |
303 | if (insn_delta == 0) { | |
304 | memcpy(prog->insnsi + off, patch, sizeof(*patch)); | |
305 | return prog; | |
306 | } | |
307 | ||
308 | insn_adj_cnt = prog->len + insn_delta; | |
309 | ||
050fad7c DB |
310 | /* Reject anything that would potentially let the insn->off |
311 | * target overflow when we have excessive program expansions. | |
312 | * We need to probe here before we do any reallocation where | |
313 | * we afterwards may not fail anymore. | |
314 | */ | |
315 | if (insn_adj_cnt > cnt_max && | |
316 | bpf_adj_branches(prog, off, insn_delta, true)) | |
317 | return NULL; | |
318 | ||
c237ee5e DB |
319 | /* Several new instructions need to be inserted. Make room |
320 | * for them. Likely, there's no need for a new allocation as | |
321 | * last page could have large enough tailroom. | |
322 | */ | |
323 | prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt), | |
324 | GFP_USER); | |
325 | if (!prog_adj) | |
326 | return NULL; | |
327 | ||
328 | prog_adj->len = insn_adj_cnt; | |
329 | ||
330 | /* Patching happens in 3 steps: | |
331 | * | |
332 | * 1) Move over tail of insnsi from next instruction onwards, | |
333 | * so we can patch the single target insn with one or more | |
334 | * new ones (patching is always from 1 to n insns, n > 0). | |
335 | * 2) Inject new instructions at the target location. | |
336 | * 3) Adjust branch offsets if necessary. | |
337 | */ | |
338 | insn_rest = insn_adj_cnt - off - len; | |
339 | ||
340 | memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1, | |
341 | sizeof(*patch) * insn_rest); | |
342 | memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len); | |
343 | ||
050fad7c DB |
344 | /* We are guaranteed to not fail at this point, otherwise |
345 | * the ship has sailed to reverse to the original state. An | |
346 | * overflow cannot happen at this point. | |
347 | */ | |
348 | BUG_ON(bpf_adj_branches(prog_adj, off, insn_delta, false)); | |
c237ee5e DB |
349 | |
350 | return prog_adj; | |
351 | } | |
352 | ||
b954d834 | 353 | #ifdef CONFIG_BPF_JIT |
fa9dd599 DB |
354 | /* All BPF JIT sysctl knobs here. */ |
355 | int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_ALWAYS_ON); | |
356 | int bpf_jit_harden __read_mostly; | |
357 | int bpf_jit_kallsyms __read_mostly; | |
358 | ||
74451e66 DB |
359 | static __always_inline void |
360 | bpf_get_prog_addr_region(const struct bpf_prog *prog, | |
361 | unsigned long *symbol_start, | |
362 | unsigned long *symbol_end) | |
363 | { | |
364 | const struct bpf_binary_header *hdr = bpf_jit_binary_hdr(prog); | |
365 | unsigned long addr = (unsigned long)hdr; | |
366 | ||
367 | WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog)); | |
368 | ||
369 | *symbol_start = addr; | |
370 | *symbol_end = addr + hdr->pages * PAGE_SIZE; | |
371 | } | |
372 | ||
373 | static void bpf_get_prog_name(const struct bpf_prog *prog, char *sym) | |
374 | { | |
368211fb MKL |
375 | const char *end = sym + KSYM_NAME_LEN; |
376 | ||
74451e66 | 377 | BUILD_BUG_ON(sizeof("bpf_prog_") + |
368211fb MKL |
378 | sizeof(prog->tag) * 2 + |
379 | /* name has been null terminated. | |
380 | * We should need +1 for the '_' preceding | |
381 | * the name. However, the null character | |
382 | * is double counted between the name and the | |
383 | * sizeof("bpf_prog_") above, so we omit | |
384 | * the +1 here. | |
385 | */ | |
386 | sizeof(prog->aux->name) > KSYM_NAME_LEN); | |
74451e66 DB |
387 | |
388 | sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_"); | |
389 | sym = bin2hex(sym, prog->tag, sizeof(prog->tag)); | |
368211fb MKL |
390 | if (prog->aux->name[0]) |
391 | snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name); | |
392 | else | |
393 | *sym = 0; | |
74451e66 DB |
394 | } |
395 | ||
396 | static __always_inline unsigned long | |
397 | bpf_get_prog_addr_start(struct latch_tree_node *n) | |
398 | { | |
399 | unsigned long symbol_start, symbol_end; | |
400 | const struct bpf_prog_aux *aux; | |
401 | ||
402 | aux = container_of(n, struct bpf_prog_aux, ksym_tnode); | |
403 | bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end); | |
404 | ||
405 | return symbol_start; | |
406 | } | |
407 | ||
408 | static __always_inline bool bpf_tree_less(struct latch_tree_node *a, | |
409 | struct latch_tree_node *b) | |
410 | { | |
411 | return bpf_get_prog_addr_start(a) < bpf_get_prog_addr_start(b); | |
412 | } | |
413 | ||
414 | static __always_inline int bpf_tree_comp(void *key, struct latch_tree_node *n) | |
415 | { | |
416 | unsigned long val = (unsigned long)key; | |
417 | unsigned long symbol_start, symbol_end; | |
418 | const struct bpf_prog_aux *aux; | |
419 | ||
420 | aux = container_of(n, struct bpf_prog_aux, ksym_tnode); | |
421 | bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end); | |
422 | ||
423 | if (val < symbol_start) | |
424 | return -1; | |
425 | if (val >= symbol_end) | |
426 | return 1; | |
427 | ||
428 | return 0; | |
429 | } | |
430 | ||
431 | static const struct latch_tree_ops bpf_tree_ops = { | |
432 | .less = bpf_tree_less, | |
433 | .comp = bpf_tree_comp, | |
434 | }; | |
435 | ||
436 | static DEFINE_SPINLOCK(bpf_lock); | |
437 | static LIST_HEAD(bpf_kallsyms); | |
438 | static struct latch_tree_root bpf_tree __cacheline_aligned; | |
439 | ||
74451e66 DB |
440 | static void bpf_prog_ksym_node_add(struct bpf_prog_aux *aux) |
441 | { | |
442 | WARN_ON_ONCE(!list_empty(&aux->ksym_lnode)); | |
443 | list_add_tail_rcu(&aux->ksym_lnode, &bpf_kallsyms); | |
444 | latch_tree_insert(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops); | |
445 | } | |
446 | ||
447 | static void bpf_prog_ksym_node_del(struct bpf_prog_aux *aux) | |
448 | { | |
449 | if (list_empty(&aux->ksym_lnode)) | |
450 | return; | |
451 | ||
452 | latch_tree_erase(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops); | |
453 | list_del_rcu(&aux->ksym_lnode); | |
454 | } | |
455 | ||
456 | static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp) | |
457 | { | |
458 | return fp->jited && !bpf_prog_was_classic(fp); | |
459 | } | |
460 | ||
461 | static bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp) | |
462 | { | |
463 | return list_empty(&fp->aux->ksym_lnode) || | |
464 | fp->aux->ksym_lnode.prev == LIST_POISON2; | |
465 | } | |
466 | ||
467 | void bpf_prog_kallsyms_add(struct bpf_prog *fp) | |
468 | { | |
74451e66 DB |
469 | if (!bpf_prog_kallsyms_candidate(fp) || |
470 | !capable(CAP_SYS_ADMIN)) | |
471 | return; | |
472 | ||
d24f7c7f | 473 | spin_lock_bh(&bpf_lock); |
74451e66 | 474 | bpf_prog_ksym_node_add(fp->aux); |
d24f7c7f | 475 | spin_unlock_bh(&bpf_lock); |
74451e66 DB |
476 | } |
477 | ||
478 | void bpf_prog_kallsyms_del(struct bpf_prog *fp) | |
479 | { | |
74451e66 DB |
480 | if (!bpf_prog_kallsyms_candidate(fp)) |
481 | return; | |
482 | ||
d24f7c7f | 483 | spin_lock_bh(&bpf_lock); |
74451e66 | 484 | bpf_prog_ksym_node_del(fp->aux); |
d24f7c7f | 485 | spin_unlock_bh(&bpf_lock); |
74451e66 DB |
486 | } |
487 | ||
488 | static struct bpf_prog *bpf_prog_kallsyms_find(unsigned long addr) | |
489 | { | |
490 | struct latch_tree_node *n; | |
491 | ||
492 | if (!bpf_jit_kallsyms_enabled()) | |
493 | return NULL; | |
494 | ||
495 | n = latch_tree_find((void *)addr, &bpf_tree, &bpf_tree_ops); | |
496 | return n ? | |
497 | container_of(n, struct bpf_prog_aux, ksym_tnode)->prog : | |
498 | NULL; | |
499 | } | |
500 | ||
501 | const char *__bpf_address_lookup(unsigned long addr, unsigned long *size, | |
502 | unsigned long *off, char *sym) | |
503 | { | |
504 | unsigned long symbol_start, symbol_end; | |
505 | struct bpf_prog *prog; | |
506 | char *ret = NULL; | |
507 | ||
508 | rcu_read_lock(); | |
509 | prog = bpf_prog_kallsyms_find(addr); | |
510 | if (prog) { | |
511 | bpf_get_prog_addr_region(prog, &symbol_start, &symbol_end); | |
512 | bpf_get_prog_name(prog, sym); | |
513 | ||
514 | ret = sym; | |
515 | if (size) | |
516 | *size = symbol_end - symbol_start; | |
517 | if (off) | |
518 | *off = addr - symbol_start; | |
519 | } | |
520 | rcu_read_unlock(); | |
521 | ||
522 | return ret; | |
523 | } | |
524 | ||
525 | bool is_bpf_text_address(unsigned long addr) | |
526 | { | |
527 | bool ret; | |
528 | ||
529 | rcu_read_lock(); | |
530 | ret = bpf_prog_kallsyms_find(addr) != NULL; | |
531 | rcu_read_unlock(); | |
532 | ||
533 | return ret; | |
534 | } | |
535 | ||
536 | int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type, | |
537 | char *sym) | |
538 | { | |
539 | unsigned long symbol_start, symbol_end; | |
540 | struct bpf_prog_aux *aux; | |
541 | unsigned int it = 0; | |
542 | int ret = -ERANGE; | |
543 | ||
544 | if (!bpf_jit_kallsyms_enabled()) | |
545 | return ret; | |
546 | ||
547 | rcu_read_lock(); | |
548 | list_for_each_entry_rcu(aux, &bpf_kallsyms, ksym_lnode) { | |
549 | if (it++ != symnum) | |
550 | continue; | |
551 | ||
552 | bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end); | |
553 | bpf_get_prog_name(aux->prog, sym); | |
554 | ||
555 | *value = symbol_start; | |
556 | *type = BPF_SYM_ELF_TYPE; | |
557 | ||
558 | ret = 0; | |
559 | break; | |
560 | } | |
561 | rcu_read_unlock(); | |
562 | ||
563 | return ret; | |
564 | } | |
565 | ||
738cbe72 DB |
566 | struct bpf_binary_header * |
567 | bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr, | |
568 | unsigned int alignment, | |
569 | bpf_jit_fill_hole_t bpf_fill_ill_insns) | |
570 | { | |
571 | struct bpf_binary_header *hdr; | |
572 | unsigned int size, hole, start; | |
573 | ||
574 | /* Most of BPF filters are really small, but if some of them | |
575 | * fill a page, allow at least 128 extra bytes to insert a | |
576 | * random section of illegal instructions. | |
577 | */ | |
578 | size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE); | |
579 | hdr = module_alloc(size); | |
580 | if (hdr == NULL) | |
581 | return NULL; | |
582 | ||
583 | /* Fill space with illegal/arch-dep instructions. */ | |
584 | bpf_fill_ill_insns(hdr, size); | |
585 | ||
586 | hdr->pages = size / PAGE_SIZE; | |
587 | hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)), | |
588 | PAGE_SIZE - sizeof(*hdr)); | |
b7552e1b | 589 | start = (get_random_int() % hole) & ~(alignment - 1); |
738cbe72 DB |
590 | |
591 | /* Leave a random number of instructions before BPF code. */ | |
592 | *image_ptr = &hdr->image[start]; | |
593 | ||
594 | return hdr; | |
595 | } | |
596 | ||
597 | void bpf_jit_binary_free(struct bpf_binary_header *hdr) | |
598 | { | |
be1f221c | 599 | module_memfree(hdr); |
738cbe72 | 600 | } |
4f3446bb | 601 | |
74451e66 DB |
602 | /* This symbol is only overridden by archs that have different |
603 | * requirements than the usual eBPF JITs, f.e. when they only | |
604 | * implement cBPF JIT, do not set images read-only, etc. | |
605 | */ | |
606 | void __weak bpf_jit_free(struct bpf_prog *fp) | |
607 | { | |
608 | if (fp->jited) { | |
609 | struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp); | |
610 | ||
611 | bpf_jit_binary_unlock_ro(hdr); | |
612 | bpf_jit_binary_free(hdr); | |
613 | ||
614 | WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp)); | |
615 | } | |
616 | ||
617 | bpf_prog_unlock_free(fp); | |
618 | } | |
619 | ||
4f3446bb DB |
620 | static int bpf_jit_blind_insn(const struct bpf_insn *from, |
621 | const struct bpf_insn *aux, | |
622 | struct bpf_insn *to_buff) | |
623 | { | |
624 | struct bpf_insn *to = to_buff; | |
b7552e1b | 625 | u32 imm_rnd = get_random_int(); |
4f3446bb DB |
626 | s16 off; |
627 | ||
628 | BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG); | |
629 | BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG); | |
630 | ||
631 | if (from->imm == 0 && | |
632 | (from->code == (BPF_ALU | BPF_MOV | BPF_K) || | |
633 | from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) { | |
634 | *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg); | |
635 | goto out; | |
636 | } | |
637 | ||
638 | switch (from->code) { | |
639 | case BPF_ALU | BPF_ADD | BPF_K: | |
640 | case BPF_ALU | BPF_SUB | BPF_K: | |
641 | case BPF_ALU | BPF_AND | BPF_K: | |
642 | case BPF_ALU | BPF_OR | BPF_K: | |
643 | case BPF_ALU | BPF_XOR | BPF_K: | |
644 | case BPF_ALU | BPF_MUL | BPF_K: | |
645 | case BPF_ALU | BPF_MOV | BPF_K: | |
646 | case BPF_ALU | BPF_DIV | BPF_K: | |
647 | case BPF_ALU | BPF_MOD | BPF_K: | |
648 | *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); | |
649 | *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); | |
650 | *to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX); | |
651 | break; | |
652 | ||
653 | case BPF_ALU64 | BPF_ADD | BPF_K: | |
654 | case BPF_ALU64 | BPF_SUB | BPF_K: | |
655 | case BPF_ALU64 | BPF_AND | BPF_K: | |
656 | case BPF_ALU64 | BPF_OR | BPF_K: | |
657 | case BPF_ALU64 | BPF_XOR | BPF_K: | |
658 | case BPF_ALU64 | BPF_MUL | BPF_K: | |
659 | case BPF_ALU64 | BPF_MOV | BPF_K: | |
660 | case BPF_ALU64 | BPF_DIV | BPF_K: | |
661 | case BPF_ALU64 | BPF_MOD | BPF_K: | |
662 | *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); | |
663 | *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); | |
664 | *to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX); | |
665 | break; | |
666 | ||
667 | case BPF_JMP | BPF_JEQ | BPF_K: | |
668 | case BPF_JMP | BPF_JNE | BPF_K: | |
669 | case BPF_JMP | BPF_JGT | BPF_K: | |
92b31a9a | 670 | case BPF_JMP | BPF_JLT | BPF_K: |
4f3446bb | 671 | case BPF_JMP | BPF_JGE | BPF_K: |
92b31a9a | 672 | case BPF_JMP | BPF_JLE | BPF_K: |
4f3446bb | 673 | case BPF_JMP | BPF_JSGT | BPF_K: |
92b31a9a | 674 | case BPF_JMP | BPF_JSLT | BPF_K: |
4f3446bb | 675 | case BPF_JMP | BPF_JSGE | BPF_K: |
92b31a9a | 676 | case BPF_JMP | BPF_JSLE | BPF_K: |
4f3446bb DB |
677 | case BPF_JMP | BPF_JSET | BPF_K: |
678 | /* Accommodate for extra offset in case of a backjump. */ | |
679 | off = from->off; | |
680 | if (off < 0) | |
681 | off -= 2; | |
682 | *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); | |
683 | *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); | |
684 | *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off); | |
685 | break; | |
686 | ||
4f3446bb DB |
687 | case BPF_LD | BPF_IMM | BPF_DW: |
688 | *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm); | |
689 | *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); | |
690 | *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); | |
691 | *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX); | |
692 | break; | |
693 | case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */ | |
694 | *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm); | |
695 | *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); | |
696 | *to++ = BPF_ALU64_REG(BPF_OR, aux[0].dst_reg, BPF_REG_AX); | |
697 | break; | |
698 | ||
699 | case BPF_ST | BPF_MEM | BPF_DW: | |
700 | case BPF_ST | BPF_MEM | BPF_W: | |
701 | case BPF_ST | BPF_MEM | BPF_H: | |
702 | case BPF_ST | BPF_MEM | BPF_B: | |
703 | *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); | |
704 | *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); | |
705 | *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off); | |
706 | break; | |
707 | } | |
708 | out: | |
709 | return to - to_buff; | |
710 | } | |
711 | ||
712 | static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other, | |
713 | gfp_t gfp_extra_flags) | |
714 | { | |
19809c2d | 715 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags; |
4f3446bb DB |
716 | struct bpf_prog *fp; |
717 | ||
718 | fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags, PAGE_KERNEL); | |
719 | if (fp != NULL) { | |
4f3446bb DB |
720 | /* aux->prog still points to the fp_other one, so |
721 | * when promoting the clone to the real program, | |
722 | * this still needs to be adapted. | |
723 | */ | |
724 | memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE); | |
725 | } | |
726 | ||
727 | return fp; | |
728 | } | |
729 | ||
730 | static void bpf_prog_clone_free(struct bpf_prog *fp) | |
731 | { | |
732 | /* aux was stolen by the other clone, so we cannot free | |
733 | * it from this path! It will be freed eventually by the | |
734 | * other program on release. | |
735 | * | |
736 | * At this point, we don't need a deferred release since | |
737 | * clone is guaranteed to not be locked. | |
738 | */ | |
739 | fp->aux = NULL; | |
740 | __bpf_prog_free(fp); | |
741 | } | |
742 | ||
743 | void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other) | |
744 | { | |
745 | /* We have to repoint aux->prog to self, as we don't | |
746 | * know whether fp here is the clone or the original. | |
747 | */ | |
748 | fp->aux->prog = fp; | |
749 | bpf_prog_clone_free(fp_other); | |
750 | } | |
751 | ||
752 | struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog) | |
753 | { | |
754 | struct bpf_insn insn_buff[16], aux[2]; | |
755 | struct bpf_prog *clone, *tmp; | |
756 | int insn_delta, insn_cnt; | |
757 | struct bpf_insn *insn; | |
758 | int i, rewritten; | |
759 | ||
1c2a088a | 760 | if (!bpf_jit_blinding_enabled(prog) || prog->blinded) |
4f3446bb DB |
761 | return prog; |
762 | ||
763 | clone = bpf_prog_clone_create(prog, GFP_USER); | |
764 | if (!clone) | |
765 | return ERR_PTR(-ENOMEM); | |
766 | ||
767 | insn_cnt = clone->len; | |
768 | insn = clone->insnsi; | |
769 | ||
770 | for (i = 0; i < insn_cnt; i++, insn++) { | |
771 | /* We temporarily need to hold the original ld64 insn | |
772 | * so that we can still access the first part in the | |
773 | * second blinding run. | |
774 | */ | |
775 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) && | |
776 | insn[1].code == 0) | |
777 | memcpy(aux, insn, sizeof(aux)); | |
778 | ||
779 | rewritten = bpf_jit_blind_insn(insn, aux, insn_buff); | |
780 | if (!rewritten) | |
781 | continue; | |
782 | ||
783 | tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten); | |
784 | if (!tmp) { | |
785 | /* Patching may have repointed aux->prog during | |
786 | * realloc from the original one, so we need to | |
787 | * fix it up here on error. | |
788 | */ | |
789 | bpf_jit_prog_release_other(prog, clone); | |
790 | return ERR_PTR(-ENOMEM); | |
791 | } | |
792 | ||
793 | clone = tmp; | |
794 | insn_delta = rewritten - 1; | |
795 | ||
796 | /* Walk new program and skip insns we just inserted. */ | |
797 | insn = clone->insnsi + i + insn_delta; | |
798 | insn_cnt += insn_delta; | |
799 | i += insn_delta; | |
800 | } | |
801 | ||
1c2a088a | 802 | clone->blinded = 1; |
4f3446bb DB |
803 | return clone; |
804 | } | |
b954d834 | 805 | #endif /* CONFIG_BPF_JIT */ |
738cbe72 | 806 | |
f5bffecd AS |
807 | /* Base function for offset calculation. Needs to go into .text section, |
808 | * therefore keeping it non-static as well; will also be used by JITs | |
7105e828 DB |
809 | * anyway later on, so do not let the compiler omit it. This also needs |
810 | * to go into kallsyms for correlation from e.g. bpftool, so naming | |
811 | * must not change. | |
f5bffecd AS |
812 | */ |
813 | noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
814 | { | |
815 | return 0; | |
816 | } | |
4d9c5c53 | 817 | EXPORT_SYMBOL_GPL(__bpf_call_base); |
f5bffecd | 818 | |
5e581dad DB |
819 | /* All UAPI available opcodes. */ |
820 | #define BPF_INSN_MAP(INSN_2, INSN_3) \ | |
821 | /* 32 bit ALU operations. */ \ | |
822 | /* Register based. */ \ | |
823 | INSN_3(ALU, ADD, X), \ | |
824 | INSN_3(ALU, SUB, X), \ | |
825 | INSN_3(ALU, AND, X), \ | |
826 | INSN_3(ALU, OR, X), \ | |
827 | INSN_3(ALU, LSH, X), \ | |
828 | INSN_3(ALU, RSH, X), \ | |
829 | INSN_3(ALU, XOR, X), \ | |
830 | INSN_3(ALU, MUL, X), \ | |
831 | INSN_3(ALU, MOV, X), \ | |
832 | INSN_3(ALU, DIV, X), \ | |
833 | INSN_3(ALU, MOD, X), \ | |
834 | INSN_2(ALU, NEG), \ | |
835 | INSN_3(ALU, END, TO_BE), \ | |
836 | INSN_3(ALU, END, TO_LE), \ | |
837 | /* Immediate based. */ \ | |
838 | INSN_3(ALU, ADD, K), \ | |
839 | INSN_3(ALU, SUB, K), \ | |
840 | INSN_3(ALU, AND, K), \ | |
841 | INSN_3(ALU, OR, K), \ | |
842 | INSN_3(ALU, LSH, K), \ | |
843 | INSN_3(ALU, RSH, K), \ | |
844 | INSN_3(ALU, XOR, K), \ | |
845 | INSN_3(ALU, MUL, K), \ | |
846 | INSN_3(ALU, MOV, K), \ | |
847 | INSN_3(ALU, DIV, K), \ | |
848 | INSN_3(ALU, MOD, K), \ | |
849 | /* 64 bit ALU operations. */ \ | |
850 | /* Register based. */ \ | |
851 | INSN_3(ALU64, ADD, X), \ | |
852 | INSN_3(ALU64, SUB, X), \ | |
853 | INSN_3(ALU64, AND, X), \ | |
854 | INSN_3(ALU64, OR, X), \ | |
855 | INSN_3(ALU64, LSH, X), \ | |
856 | INSN_3(ALU64, RSH, X), \ | |
857 | INSN_3(ALU64, XOR, X), \ | |
858 | INSN_3(ALU64, MUL, X), \ | |
859 | INSN_3(ALU64, MOV, X), \ | |
860 | INSN_3(ALU64, ARSH, X), \ | |
861 | INSN_3(ALU64, DIV, X), \ | |
862 | INSN_3(ALU64, MOD, X), \ | |
863 | INSN_2(ALU64, NEG), \ | |
864 | /* Immediate based. */ \ | |
865 | INSN_3(ALU64, ADD, K), \ | |
866 | INSN_3(ALU64, SUB, K), \ | |
867 | INSN_3(ALU64, AND, K), \ | |
868 | INSN_3(ALU64, OR, K), \ | |
869 | INSN_3(ALU64, LSH, K), \ | |
870 | INSN_3(ALU64, RSH, K), \ | |
871 | INSN_3(ALU64, XOR, K), \ | |
872 | INSN_3(ALU64, MUL, K), \ | |
873 | INSN_3(ALU64, MOV, K), \ | |
874 | INSN_3(ALU64, ARSH, K), \ | |
875 | INSN_3(ALU64, DIV, K), \ | |
876 | INSN_3(ALU64, MOD, K), \ | |
877 | /* Call instruction. */ \ | |
878 | INSN_2(JMP, CALL), \ | |
879 | /* Exit instruction. */ \ | |
880 | INSN_2(JMP, EXIT), \ | |
881 | /* Jump instructions. */ \ | |
882 | /* Register based. */ \ | |
883 | INSN_3(JMP, JEQ, X), \ | |
884 | INSN_3(JMP, JNE, X), \ | |
885 | INSN_3(JMP, JGT, X), \ | |
886 | INSN_3(JMP, JLT, X), \ | |
887 | INSN_3(JMP, JGE, X), \ | |
888 | INSN_3(JMP, JLE, X), \ | |
889 | INSN_3(JMP, JSGT, X), \ | |
890 | INSN_3(JMP, JSLT, X), \ | |
891 | INSN_3(JMP, JSGE, X), \ | |
892 | INSN_3(JMP, JSLE, X), \ | |
893 | INSN_3(JMP, JSET, X), \ | |
894 | /* Immediate based. */ \ | |
895 | INSN_3(JMP, JEQ, K), \ | |
896 | INSN_3(JMP, JNE, K), \ | |
897 | INSN_3(JMP, JGT, K), \ | |
898 | INSN_3(JMP, JLT, K), \ | |
899 | INSN_3(JMP, JGE, K), \ | |
900 | INSN_3(JMP, JLE, K), \ | |
901 | INSN_3(JMP, JSGT, K), \ | |
902 | INSN_3(JMP, JSLT, K), \ | |
903 | INSN_3(JMP, JSGE, K), \ | |
904 | INSN_3(JMP, JSLE, K), \ | |
905 | INSN_3(JMP, JSET, K), \ | |
906 | INSN_2(JMP, JA), \ | |
907 | /* Store instructions. */ \ | |
908 | /* Register based. */ \ | |
909 | INSN_3(STX, MEM, B), \ | |
910 | INSN_3(STX, MEM, H), \ | |
911 | INSN_3(STX, MEM, W), \ | |
912 | INSN_3(STX, MEM, DW), \ | |
913 | INSN_3(STX, XADD, W), \ | |
914 | INSN_3(STX, XADD, DW), \ | |
915 | /* Immediate based. */ \ | |
916 | INSN_3(ST, MEM, B), \ | |
917 | INSN_3(ST, MEM, H), \ | |
918 | INSN_3(ST, MEM, W), \ | |
919 | INSN_3(ST, MEM, DW), \ | |
920 | /* Load instructions. */ \ | |
921 | /* Register based. */ \ | |
922 | INSN_3(LDX, MEM, B), \ | |
923 | INSN_3(LDX, MEM, H), \ | |
924 | INSN_3(LDX, MEM, W), \ | |
925 | INSN_3(LDX, MEM, DW), \ | |
926 | /* Immediate based. */ \ | |
e0cea7ce | 927 | INSN_3(LD, IMM, DW) |
5e581dad DB |
928 | |
929 | bool bpf_opcode_in_insntable(u8 code) | |
930 | { | |
931 | #define BPF_INSN_2_TBL(x, y) [BPF_##x | BPF_##y] = true | |
932 | #define BPF_INSN_3_TBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = true | |
933 | static const bool public_insntable[256] = { | |
934 | [0 ... 255] = false, | |
935 | /* Now overwrite non-defaults ... */ | |
936 | BPF_INSN_MAP(BPF_INSN_2_TBL, BPF_INSN_3_TBL), | |
e0cea7ce DB |
937 | /* UAPI exposed, but rewritten opcodes. cBPF carry-over. */ |
938 | [BPF_LD | BPF_ABS | BPF_B] = true, | |
939 | [BPF_LD | BPF_ABS | BPF_H] = true, | |
940 | [BPF_LD | BPF_ABS | BPF_W] = true, | |
941 | [BPF_LD | BPF_IND | BPF_B] = true, | |
942 | [BPF_LD | BPF_IND | BPF_H] = true, | |
943 | [BPF_LD | BPF_IND | BPF_W] = true, | |
5e581dad DB |
944 | }; |
945 | #undef BPF_INSN_3_TBL | |
946 | #undef BPF_INSN_2_TBL | |
947 | return public_insntable[code]; | |
948 | } | |
949 | ||
290af866 | 950 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
f5bffecd | 951 | /** |
7ae457c1 AS |
952 | * __bpf_prog_run - run eBPF program on a given context |
953 | * @ctx: is the data we are operating on | |
954 | * @insn: is the array of eBPF instructions | |
f5bffecd | 955 | * |
7ae457c1 | 956 | * Decode and execute eBPF instructions. |
f5bffecd | 957 | */ |
1ea47e01 | 958 | static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack) |
f5bffecd | 959 | { |
f696b8f4 | 960 | u64 tmp; |
5e581dad DB |
961 | #define BPF_INSN_2_LBL(x, y) [BPF_##x | BPF_##y] = &&x##_##y |
962 | #define BPF_INSN_3_LBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = &&x##_##y##_##z | |
f5bffecd AS |
963 | static const void *jumptable[256] = { |
964 | [0 ... 255] = &&default_label, | |
965 | /* Now overwrite non-defaults ... */ | |
5e581dad DB |
966 | BPF_INSN_MAP(BPF_INSN_2_LBL, BPF_INSN_3_LBL), |
967 | /* Non-UAPI available opcodes. */ | |
1ea47e01 | 968 | [BPF_JMP | BPF_CALL_ARGS] = &&JMP_CALL_ARGS, |
71189fa9 | 969 | [BPF_JMP | BPF_TAIL_CALL] = &&JMP_TAIL_CALL, |
f5bffecd | 970 | }; |
5e581dad DB |
971 | #undef BPF_INSN_3_LBL |
972 | #undef BPF_INSN_2_LBL | |
04fd61ab | 973 | u32 tail_call_cnt = 0; |
f5bffecd AS |
974 | |
975 | #define CONT ({ insn++; goto select_insn; }) | |
976 | #define CONT_JMP ({ insn++; goto select_insn; }) | |
977 | ||
f5bffecd AS |
978 | select_insn: |
979 | goto *jumptable[insn->code]; | |
980 | ||
981 | /* ALU */ | |
982 | #define ALU(OPCODE, OP) \ | |
983 | ALU64_##OPCODE##_X: \ | |
984 | DST = DST OP SRC; \ | |
985 | CONT; \ | |
986 | ALU_##OPCODE##_X: \ | |
987 | DST = (u32) DST OP (u32) SRC; \ | |
988 | CONT; \ | |
989 | ALU64_##OPCODE##_K: \ | |
990 | DST = DST OP IMM; \ | |
991 | CONT; \ | |
992 | ALU_##OPCODE##_K: \ | |
993 | DST = (u32) DST OP (u32) IMM; \ | |
994 | CONT; | |
995 | ||
996 | ALU(ADD, +) | |
997 | ALU(SUB, -) | |
998 | ALU(AND, &) | |
999 | ALU(OR, |) | |
1000 | ALU(LSH, <<) | |
1001 | ALU(RSH, >>) | |
1002 | ALU(XOR, ^) | |
1003 | ALU(MUL, *) | |
1004 | #undef ALU | |
1005 | ALU_NEG: | |
1006 | DST = (u32) -DST; | |
1007 | CONT; | |
1008 | ALU64_NEG: | |
1009 | DST = -DST; | |
1010 | CONT; | |
1011 | ALU_MOV_X: | |
1012 | DST = (u32) SRC; | |
1013 | CONT; | |
1014 | ALU_MOV_K: | |
1015 | DST = (u32) IMM; | |
1016 | CONT; | |
1017 | ALU64_MOV_X: | |
1018 | DST = SRC; | |
1019 | CONT; | |
1020 | ALU64_MOV_K: | |
1021 | DST = IMM; | |
1022 | CONT; | |
02ab695b AS |
1023 | LD_IMM_DW: |
1024 | DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32; | |
1025 | insn++; | |
1026 | CONT; | |
f5bffecd AS |
1027 | ALU64_ARSH_X: |
1028 | (*(s64 *) &DST) >>= SRC; | |
1029 | CONT; | |
1030 | ALU64_ARSH_K: | |
1031 | (*(s64 *) &DST) >>= IMM; | |
1032 | CONT; | |
1033 | ALU64_MOD_X: | |
876a7ae6 AS |
1034 | div64_u64_rem(DST, SRC, &tmp); |
1035 | DST = tmp; | |
f5bffecd AS |
1036 | CONT; |
1037 | ALU_MOD_X: | |
f5bffecd AS |
1038 | tmp = (u32) DST; |
1039 | DST = do_div(tmp, (u32) SRC); | |
1040 | CONT; | |
1041 | ALU64_MOD_K: | |
876a7ae6 AS |
1042 | div64_u64_rem(DST, IMM, &tmp); |
1043 | DST = tmp; | |
f5bffecd AS |
1044 | CONT; |
1045 | ALU_MOD_K: | |
1046 | tmp = (u32) DST; | |
1047 | DST = do_div(tmp, (u32) IMM); | |
1048 | CONT; | |
1049 | ALU64_DIV_X: | |
876a7ae6 | 1050 | DST = div64_u64(DST, SRC); |
f5bffecd AS |
1051 | CONT; |
1052 | ALU_DIV_X: | |
f5bffecd AS |
1053 | tmp = (u32) DST; |
1054 | do_div(tmp, (u32) SRC); | |
1055 | DST = (u32) tmp; | |
1056 | CONT; | |
1057 | ALU64_DIV_K: | |
876a7ae6 | 1058 | DST = div64_u64(DST, IMM); |
f5bffecd AS |
1059 | CONT; |
1060 | ALU_DIV_K: | |
1061 | tmp = (u32) DST; | |
1062 | do_div(tmp, (u32) IMM); | |
1063 | DST = (u32) tmp; | |
1064 | CONT; | |
1065 | ALU_END_TO_BE: | |
1066 | switch (IMM) { | |
1067 | case 16: | |
1068 | DST = (__force u16) cpu_to_be16(DST); | |
1069 | break; | |
1070 | case 32: | |
1071 | DST = (__force u32) cpu_to_be32(DST); | |
1072 | break; | |
1073 | case 64: | |
1074 | DST = (__force u64) cpu_to_be64(DST); | |
1075 | break; | |
1076 | } | |
1077 | CONT; | |
1078 | ALU_END_TO_LE: | |
1079 | switch (IMM) { | |
1080 | case 16: | |
1081 | DST = (__force u16) cpu_to_le16(DST); | |
1082 | break; | |
1083 | case 32: | |
1084 | DST = (__force u32) cpu_to_le32(DST); | |
1085 | break; | |
1086 | case 64: | |
1087 | DST = (__force u64) cpu_to_le64(DST); | |
1088 | break; | |
1089 | } | |
1090 | CONT; | |
1091 | ||
1092 | /* CALL */ | |
1093 | JMP_CALL: | |
1094 | /* Function call scratches BPF_R1-BPF_R5 registers, | |
1095 | * preserves BPF_R6-BPF_R9, and stores return value | |
1096 | * into BPF_R0. | |
1097 | */ | |
1098 | BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3, | |
1099 | BPF_R4, BPF_R5); | |
1100 | CONT; | |
1101 | ||
1ea47e01 AS |
1102 | JMP_CALL_ARGS: |
1103 | BPF_R0 = (__bpf_call_base_args + insn->imm)(BPF_R1, BPF_R2, | |
1104 | BPF_R3, BPF_R4, | |
1105 | BPF_R5, | |
1106 | insn + insn->off + 1); | |
1107 | CONT; | |
1108 | ||
04fd61ab AS |
1109 | JMP_TAIL_CALL: { |
1110 | struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2; | |
1111 | struct bpf_array *array = container_of(map, struct bpf_array, map); | |
1112 | struct bpf_prog *prog; | |
90caccdd | 1113 | u32 index = BPF_R3; |
04fd61ab AS |
1114 | |
1115 | if (unlikely(index >= array->map.max_entries)) | |
1116 | goto out; | |
04fd61ab AS |
1117 | if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT)) |
1118 | goto out; | |
1119 | ||
1120 | tail_call_cnt++; | |
1121 | ||
2a36f0b9 | 1122 | prog = READ_ONCE(array->ptrs[index]); |
1ca1cc98 | 1123 | if (!prog) |
04fd61ab AS |
1124 | goto out; |
1125 | ||
c4675f93 DB |
1126 | /* ARG1 at this point is guaranteed to point to CTX from |
1127 | * the verifier side due to the fact that the tail call is | |
1128 | * handeled like a helper, that is, bpf_tail_call_proto, | |
1129 | * where arg1_type is ARG_PTR_TO_CTX. | |
1130 | */ | |
04fd61ab AS |
1131 | insn = prog->insnsi; |
1132 | goto select_insn; | |
1133 | out: | |
1134 | CONT; | |
1135 | } | |
f5bffecd AS |
1136 | /* JMP */ |
1137 | JMP_JA: | |
1138 | insn += insn->off; | |
1139 | CONT; | |
1140 | JMP_JEQ_X: | |
1141 | if (DST == SRC) { | |
1142 | insn += insn->off; | |
1143 | CONT_JMP; | |
1144 | } | |
1145 | CONT; | |
1146 | JMP_JEQ_K: | |
1147 | if (DST == IMM) { | |
1148 | insn += insn->off; | |
1149 | CONT_JMP; | |
1150 | } | |
1151 | CONT; | |
1152 | JMP_JNE_X: | |
1153 | if (DST != SRC) { | |
1154 | insn += insn->off; | |
1155 | CONT_JMP; | |
1156 | } | |
1157 | CONT; | |
1158 | JMP_JNE_K: | |
1159 | if (DST != IMM) { | |
1160 | insn += insn->off; | |
1161 | CONT_JMP; | |
1162 | } | |
1163 | CONT; | |
1164 | JMP_JGT_X: | |
1165 | if (DST > SRC) { | |
1166 | insn += insn->off; | |
1167 | CONT_JMP; | |
1168 | } | |
1169 | CONT; | |
1170 | JMP_JGT_K: | |
1171 | if (DST > IMM) { | |
1172 | insn += insn->off; | |
1173 | CONT_JMP; | |
1174 | } | |
1175 | CONT; | |
92b31a9a DB |
1176 | JMP_JLT_X: |
1177 | if (DST < SRC) { | |
1178 | insn += insn->off; | |
1179 | CONT_JMP; | |
1180 | } | |
1181 | CONT; | |
1182 | JMP_JLT_K: | |
1183 | if (DST < IMM) { | |
1184 | insn += insn->off; | |
1185 | CONT_JMP; | |
1186 | } | |
1187 | CONT; | |
f5bffecd AS |
1188 | JMP_JGE_X: |
1189 | if (DST >= SRC) { | |
1190 | insn += insn->off; | |
1191 | CONT_JMP; | |
1192 | } | |
1193 | CONT; | |
1194 | JMP_JGE_K: | |
1195 | if (DST >= IMM) { | |
1196 | insn += insn->off; | |
1197 | CONT_JMP; | |
1198 | } | |
1199 | CONT; | |
92b31a9a DB |
1200 | JMP_JLE_X: |
1201 | if (DST <= SRC) { | |
1202 | insn += insn->off; | |
1203 | CONT_JMP; | |
1204 | } | |
1205 | CONT; | |
1206 | JMP_JLE_K: | |
1207 | if (DST <= IMM) { | |
1208 | insn += insn->off; | |
1209 | CONT_JMP; | |
1210 | } | |
1211 | CONT; | |
f5bffecd AS |
1212 | JMP_JSGT_X: |
1213 | if (((s64) DST) > ((s64) SRC)) { | |
1214 | insn += insn->off; | |
1215 | CONT_JMP; | |
1216 | } | |
1217 | CONT; | |
1218 | JMP_JSGT_K: | |
1219 | if (((s64) DST) > ((s64) IMM)) { | |
1220 | insn += insn->off; | |
1221 | CONT_JMP; | |
1222 | } | |
1223 | CONT; | |
92b31a9a DB |
1224 | JMP_JSLT_X: |
1225 | if (((s64) DST) < ((s64) SRC)) { | |
1226 | insn += insn->off; | |
1227 | CONT_JMP; | |
1228 | } | |
1229 | CONT; | |
1230 | JMP_JSLT_K: | |
1231 | if (((s64) DST) < ((s64) IMM)) { | |
1232 | insn += insn->off; | |
1233 | CONT_JMP; | |
1234 | } | |
1235 | CONT; | |
f5bffecd AS |
1236 | JMP_JSGE_X: |
1237 | if (((s64) DST) >= ((s64) SRC)) { | |
1238 | insn += insn->off; | |
1239 | CONT_JMP; | |
1240 | } | |
1241 | CONT; | |
1242 | JMP_JSGE_K: | |
1243 | if (((s64) DST) >= ((s64) IMM)) { | |
1244 | insn += insn->off; | |
1245 | CONT_JMP; | |
1246 | } | |
1247 | CONT; | |
92b31a9a DB |
1248 | JMP_JSLE_X: |
1249 | if (((s64) DST) <= ((s64) SRC)) { | |
1250 | insn += insn->off; | |
1251 | CONT_JMP; | |
1252 | } | |
1253 | CONT; | |
1254 | JMP_JSLE_K: | |
1255 | if (((s64) DST) <= ((s64) IMM)) { | |
1256 | insn += insn->off; | |
1257 | CONT_JMP; | |
1258 | } | |
1259 | CONT; | |
f5bffecd AS |
1260 | JMP_JSET_X: |
1261 | if (DST & SRC) { | |
1262 | insn += insn->off; | |
1263 | CONT_JMP; | |
1264 | } | |
1265 | CONT; | |
1266 | JMP_JSET_K: | |
1267 | if (DST & IMM) { | |
1268 | insn += insn->off; | |
1269 | CONT_JMP; | |
1270 | } | |
1271 | CONT; | |
1272 | JMP_EXIT: | |
1273 | return BPF_R0; | |
1274 | ||
1275 | /* STX and ST and LDX*/ | |
1276 | #define LDST(SIZEOP, SIZE) \ | |
1277 | STX_MEM_##SIZEOP: \ | |
1278 | *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \ | |
1279 | CONT; \ | |
1280 | ST_MEM_##SIZEOP: \ | |
1281 | *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \ | |
1282 | CONT; \ | |
1283 | LDX_MEM_##SIZEOP: \ | |
1284 | DST = *(SIZE *)(unsigned long) (SRC + insn->off); \ | |
1285 | CONT; | |
1286 | ||
1287 | LDST(B, u8) | |
1288 | LDST(H, u16) | |
1289 | LDST(W, u32) | |
1290 | LDST(DW, u64) | |
1291 | #undef LDST | |
1292 | STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */ | |
1293 | atomic_add((u32) SRC, (atomic_t *)(unsigned long) | |
1294 | (DST + insn->off)); | |
1295 | CONT; | |
1296 | STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */ | |
1297 | atomic64_add((u64) SRC, (atomic64_t *)(unsigned long) | |
1298 | (DST + insn->off)); | |
1299 | CONT; | |
f5bffecd AS |
1300 | |
1301 | default_label: | |
5e581dad DB |
1302 | /* If we ever reach this, we have a bug somewhere. Die hard here |
1303 | * instead of just returning 0; we could be somewhere in a subprog, | |
1304 | * so execution could continue otherwise which we do /not/ want. | |
1305 | * | |
1306 | * Note, verifier whitelists all opcodes in bpf_opcode_in_insntable(). | |
1307 | */ | |
1308 | pr_warn("BPF interpreter: unknown opcode %02x\n", insn->code); | |
1309 | BUG_ON(1); | |
f5bffecd AS |
1310 | return 0; |
1311 | } | |
f696b8f4 AS |
1312 | STACK_FRAME_NON_STANDARD(___bpf_prog_run); /* jump table */ |
1313 | ||
b870aa90 AS |
1314 | #define PROG_NAME(stack_size) __bpf_prog_run##stack_size |
1315 | #define DEFINE_BPF_PROG_RUN(stack_size) \ | |
1316 | static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \ | |
1317 | { \ | |
1318 | u64 stack[stack_size / sizeof(u64)]; \ | |
1319 | u64 regs[MAX_BPF_REG]; \ | |
1320 | \ | |
1321 | FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \ | |
1322 | ARG1 = (u64) (unsigned long) ctx; \ | |
1323 | return ___bpf_prog_run(regs, insn, stack); \ | |
f696b8f4 | 1324 | } |
f5bffecd | 1325 | |
1ea47e01 AS |
1326 | #define PROG_NAME_ARGS(stack_size) __bpf_prog_run_args##stack_size |
1327 | #define DEFINE_BPF_PROG_RUN_ARGS(stack_size) \ | |
1328 | static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \ | |
1329 | const struct bpf_insn *insn) \ | |
1330 | { \ | |
1331 | u64 stack[stack_size / sizeof(u64)]; \ | |
1332 | u64 regs[MAX_BPF_REG]; \ | |
1333 | \ | |
1334 | FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \ | |
1335 | BPF_R1 = r1; \ | |
1336 | BPF_R2 = r2; \ | |
1337 | BPF_R3 = r3; \ | |
1338 | BPF_R4 = r4; \ | |
1339 | BPF_R5 = r5; \ | |
1340 | return ___bpf_prog_run(regs, insn, stack); \ | |
1341 | } | |
1342 | ||
b870aa90 AS |
1343 | #define EVAL1(FN, X) FN(X) |
1344 | #define EVAL2(FN, X, Y...) FN(X) EVAL1(FN, Y) | |
1345 | #define EVAL3(FN, X, Y...) FN(X) EVAL2(FN, Y) | |
1346 | #define EVAL4(FN, X, Y...) FN(X) EVAL3(FN, Y) | |
1347 | #define EVAL5(FN, X, Y...) FN(X) EVAL4(FN, Y) | |
1348 | #define EVAL6(FN, X, Y...) FN(X) EVAL5(FN, Y) | |
1349 | ||
1350 | EVAL6(DEFINE_BPF_PROG_RUN, 32, 64, 96, 128, 160, 192); | |
1351 | EVAL6(DEFINE_BPF_PROG_RUN, 224, 256, 288, 320, 352, 384); | |
1352 | EVAL4(DEFINE_BPF_PROG_RUN, 416, 448, 480, 512); | |
1353 | ||
1ea47e01 AS |
1354 | EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 32, 64, 96, 128, 160, 192); |
1355 | EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 224, 256, 288, 320, 352, 384); | |
1356 | EVAL4(DEFINE_BPF_PROG_RUN_ARGS, 416, 448, 480, 512); | |
1357 | ||
b870aa90 AS |
1358 | #define PROG_NAME_LIST(stack_size) PROG_NAME(stack_size), |
1359 | ||
1360 | static unsigned int (*interpreters[])(const void *ctx, | |
1361 | const struct bpf_insn *insn) = { | |
1362 | EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192) | |
1363 | EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384) | |
1364 | EVAL4(PROG_NAME_LIST, 416, 448, 480, 512) | |
1365 | }; | |
1ea47e01 AS |
1366 | #undef PROG_NAME_LIST |
1367 | #define PROG_NAME_LIST(stack_size) PROG_NAME_ARGS(stack_size), | |
1368 | static u64 (*interpreters_args[])(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, | |
1369 | const struct bpf_insn *insn) = { | |
1370 | EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192) | |
1371 | EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384) | |
1372 | EVAL4(PROG_NAME_LIST, 416, 448, 480, 512) | |
1373 | }; | |
1374 | #undef PROG_NAME_LIST | |
1375 | ||
1376 | void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth) | |
1377 | { | |
1378 | stack_depth = max_t(u32, stack_depth, 1); | |
1379 | insn->off = (s16) insn->imm; | |
1380 | insn->imm = interpreters_args[(round_up(stack_depth, 32) / 32) - 1] - | |
1381 | __bpf_call_base_args; | |
1382 | insn->code = BPF_JMP | BPF_CALL_ARGS; | |
1383 | } | |
b870aa90 | 1384 | |
290af866 | 1385 | #else |
fa9dd599 DB |
1386 | static unsigned int __bpf_prog_ret0_warn(const void *ctx, |
1387 | const struct bpf_insn *insn) | |
290af866 | 1388 | { |
fa9dd599 DB |
1389 | /* If this handler ever gets executed, then BPF_JIT_ALWAYS_ON |
1390 | * is not working properly, so warn about it! | |
1391 | */ | |
1392 | WARN_ON_ONCE(1); | |
290af866 AS |
1393 | return 0; |
1394 | } | |
1395 | #endif | |
1396 | ||
3324b584 DB |
1397 | bool bpf_prog_array_compatible(struct bpf_array *array, |
1398 | const struct bpf_prog *fp) | |
04fd61ab | 1399 | { |
9802d865 JB |
1400 | if (fp->kprobe_override) |
1401 | return false; | |
1402 | ||
3324b584 DB |
1403 | if (!array->owner_prog_type) { |
1404 | /* There's no owner yet where we could check for | |
1405 | * compatibility. | |
1406 | */ | |
04fd61ab AS |
1407 | array->owner_prog_type = fp->type; |
1408 | array->owner_jited = fp->jited; | |
3324b584 DB |
1409 | |
1410 | return true; | |
04fd61ab | 1411 | } |
3324b584 DB |
1412 | |
1413 | return array->owner_prog_type == fp->type && | |
1414 | array->owner_jited == fp->jited; | |
04fd61ab AS |
1415 | } |
1416 | ||
3324b584 | 1417 | static int bpf_check_tail_call(const struct bpf_prog *fp) |
04fd61ab AS |
1418 | { |
1419 | struct bpf_prog_aux *aux = fp->aux; | |
1420 | int i; | |
1421 | ||
1422 | for (i = 0; i < aux->used_map_cnt; i++) { | |
3324b584 | 1423 | struct bpf_map *map = aux->used_maps[i]; |
04fd61ab | 1424 | struct bpf_array *array; |
04fd61ab | 1425 | |
04fd61ab AS |
1426 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
1427 | continue; | |
3324b584 | 1428 | |
04fd61ab AS |
1429 | array = container_of(map, struct bpf_array, map); |
1430 | if (!bpf_prog_array_compatible(array, fp)) | |
1431 | return -EINVAL; | |
1432 | } | |
1433 | ||
1434 | return 0; | |
1435 | } | |
1436 | ||
f5bffecd | 1437 | /** |
3324b584 | 1438 | * bpf_prog_select_runtime - select exec runtime for BPF program |
7ae457c1 | 1439 | * @fp: bpf_prog populated with internal BPF program |
d1c55ab5 | 1440 | * @err: pointer to error variable |
f5bffecd | 1441 | * |
3324b584 DB |
1442 | * Try to JIT eBPF program, if JIT is not available, use interpreter. |
1443 | * The BPF program will be executed via BPF_PROG_RUN() macro. | |
f5bffecd | 1444 | */ |
d1c55ab5 | 1445 | struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err) |
f5bffecd | 1446 | { |
290af866 | 1447 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
8007e40a MKL |
1448 | u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1); |
1449 | ||
1450 | fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1]; | |
290af866 | 1451 | #else |
fa9dd599 | 1452 | fp->bpf_func = __bpf_prog_ret0_warn; |
290af866 | 1453 | #endif |
f5bffecd | 1454 | |
d1c55ab5 DB |
1455 | /* eBPF JITs can rewrite the program in case constant |
1456 | * blinding is active. However, in case of error during | |
1457 | * blinding, bpf_int_jit_compile() must always return a | |
1458 | * valid program, which in this case would simply not | |
1459 | * be JITed, but falls back to the interpreter. | |
1460 | */ | |
ab3f0063 JK |
1461 | if (!bpf_prog_is_dev_bound(fp->aux)) { |
1462 | fp = bpf_int_jit_compile(fp); | |
290af866 AS |
1463 | #ifdef CONFIG_BPF_JIT_ALWAYS_ON |
1464 | if (!fp->jited) { | |
1465 | *err = -ENOTSUPP; | |
1466 | return fp; | |
1467 | } | |
1468 | #endif | |
ab3f0063 JK |
1469 | } else { |
1470 | *err = bpf_prog_offload_compile(fp); | |
1471 | if (*err) | |
1472 | return fp; | |
1473 | } | |
60a3b225 | 1474 | bpf_prog_lock_ro(fp); |
04fd61ab | 1475 | |
3324b584 DB |
1476 | /* The tail call compatibility check can only be done at |
1477 | * this late stage as we need to determine, if we deal | |
1478 | * with JITed or non JITed program concatenations and not | |
1479 | * all eBPF JITs might immediately support all features. | |
1480 | */ | |
d1c55ab5 DB |
1481 | *err = bpf_check_tail_call(fp); |
1482 | ||
1483 | return fp; | |
f5bffecd | 1484 | } |
7ae457c1 | 1485 | EXPORT_SYMBOL_GPL(bpf_prog_select_runtime); |
f5bffecd | 1486 | |
e87c6bc3 YS |
1487 | static unsigned int __bpf_prog_ret1(const void *ctx, |
1488 | const struct bpf_insn *insn) | |
1489 | { | |
1490 | return 1; | |
1491 | } | |
1492 | ||
1493 | static struct bpf_prog_dummy { | |
1494 | struct bpf_prog prog; | |
1495 | } dummy_bpf_prog = { | |
1496 | .prog = { | |
1497 | .bpf_func = __bpf_prog_ret1, | |
1498 | }, | |
1499 | }; | |
1500 | ||
324bda9e AS |
1501 | /* to avoid allocating empty bpf_prog_array for cgroups that |
1502 | * don't have bpf program attached use one global 'empty_prog_array' | |
1503 | * It will not be modified the caller of bpf_prog_array_alloc() | |
1504 | * (since caller requested prog_cnt == 0) | |
1505 | * that pointer should be 'freed' by bpf_prog_array_free() | |
1506 | */ | |
1507 | static struct { | |
1508 | struct bpf_prog_array hdr; | |
1509 | struct bpf_prog *null_prog; | |
1510 | } empty_prog_array = { | |
1511 | .null_prog = NULL, | |
1512 | }; | |
1513 | ||
1514 | struct bpf_prog_array __rcu *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags) | |
1515 | { | |
1516 | if (prog_cnt) | |
1517 | return kzalloc(sizeof(struct bpf_prog_array) + | |
1518 | sizeof(struct bpf_prog *) * (prog_cnt + 1), | |
1519 | flags); | |
1520 | ||
1521 | return &empty_prog_array.hdr; | |
1522 | } | |
1523 | ||
1524 | void bpf_prog_array_free(struct bpf_prog_array __rcu *progs) | |
1525 | { | |
1526 | if (!progs || | |
1527 | progs == (struct bpf_prog_array __rcu *)&empty_prog_array.hdr) | |
1528 | return; | |
1529 | kfree_rcu(progs, rcu); | |
1530 | } | |
1531 | ||
468e2f64 AS |
1532 | int bpf_prog_array_length(struct bpf_prog_array __rcu *progs) |
1533 | { | |
1534 | struct bpf_prog **prog; | |
1535 | u32 cnt = 0; | |
1536 | ||
1537 | rcu_read_lock(); | |
1538 | prog = rcu_dereference(progs)->progs; | |
1539 | for (; *prog; prog++) | |
c8c088ba YS |
1540 | if (*prog != &dummy_bpf_prog.prog) |
1541 | cnt++; | |
468e2f64 AS |
1542 | rcu_read_unlock(); |
1543 | return cnt; | |
1544 | } | |
1545 | ||
3a38bb98 YS |
1546 | static bool bpf_prog_array_copy_core(struct bpf_prog **prog, |
1547 | u32 *prog_ids, | |
1548 | u32 request_cnt) | |
1549 | { | |
1550 | int i = 0; | |
1551 | ||
1552 | for (; *prog; prog++) { | |
1553 | if (*prog == &dummy_bpf_prog.prog) | |
1554 | continue; | |
1555 | prog_ids[i] = (*prog)->aux->id; | |
1556 | if (++i == request_cnt) { | |
1557 | prog++; | |
1558 | break; | |
1559 | } | |
1560 | } | |
1561 | ||
1562 | return !!(*prog); | |
1563 | } | |
1564 | ||
468e2f64 AS |
1565 | int bpf_prog_array_copy_to_user(struct bpf_prog_array __rcu *progs, |
1566 | __u32 __user *prog_ids, u32 cnt) | |
1567 | { | |
1568 | struct bpf_prog **prog; | |
0911287c | 1569 | unsigned long err = 0; |
0911287c | 1570 | bool nospc; |
3a38bb98 | 1571 | u32 *ids; |
0911287c AS |
1572 | |
1573 | /* users of this function are doing: | |
1574 | * cnt = bpf_prog_array_length(); | |
1575 | * if (cnt > 0) | |
1576 | * bpf_prog_array_copy_to_user(..., cnt); | |
1577 | * so below kcalloc doesn't need extra cnt > 0 check, but | |
1578 | * bpf_prog_array_length() releases rcu lock and | |
1579 | * prog array could have been swapped with empty or larger array, | |
1580 | * so always copy 'cnt' prog_ids to the user. | |
1581 | * In a rare race the user will see zero prog_ids | |
1582 | */ | |
9c481b90 | 1583 | ids = kcalloc(cnt, sizeof(u32), GFP_USER | __GFP_NOWARN); |
0911287c AS |
1584 | if (!ids) |
1585 | return -ENOMEM; | |
468e2f64 AS |
1586 | rcu_read_lock(); |
1587 | prog = rcu_dereference(progs)->progs; | |
3a38bb98 | 1588 | nospc = bpf_prog_array_copy_core(prog, ids, cnt); |
468e2f64 | 1589 | rcu_read_unlock(); |
0911287c AS |
1590 | err = copy_to_user(prog_ids, ids, cnt * sizeof(u32)); |
1591 | kfree(ids); | |
1592 | if (err) | |
1593 | return -EFAULT; | |
1594 | if (nospc) | |
468e2f64 AS |
1595 | return -ENOSPC; |
1596 | return 0; | |
1597 | } | |
1598 | ||
e87c6bc3 YS |
1599 | void bpf_prog_array_delete_safe(struct bpf_prog_array __rcu *progs, |
1600 | struct bpf_prog *old_prog) | |
1601 | { | |
1602 | struct bpf_prog **prog = progs->progs; | |
1603 | ||
1604 | for (; *prog; prog++) | |
1605 | if (*prog == old_prog) { | |
1606 | WRITE_ONCE(*prog, &dummy_bpf_prog.prog); | |
1607 | break; | |
1608 | } | |
1609 | } | |
1610 | ||
1611 | int bpf_prog_array_copy(struct bpf_prog_array __rcu *old_array, | |
1612 | struct bpf_prog *exclude_prog, | |
1613 | struct bpf_prog *include_prog, | |
1614 | struct bpf_prog_array **new_array) | |
1615 | { | |
1616 | int new_prog_cnt, carry_prog_cnt = 0; | |
1617 | struct bpf_prog **existing_prog; | |
1618 | struct bpf_prog_array *array; | |
170a7e3e | 1619 | bool found_exclude = false; |
e87c6bc3 YS |
1620 | int new_prog_idx = 0; |
1621 | ||
1622 | /* Figure out how many existing progs we need to carry over to | |
1623 | * the new array. | |
1624 | */ | |
1625 | if (old_array) { | |
1626 | existing_prog = old_array->progs; | |
1627 | for (; *existing_prog; existing_prog++) { | |
170a7e3e SY |
1628 | if (*existing_prog == exclude_prog) { |
1629 | found_exclude = true; | |
1630 | continue; | |
1631 | } | |
1632 | if (*existing_prog != &dummy_bpf_prog.prog) | |
e87c6bc3 YS |
1633 | carry_prog_cnt++; |
1634 | if (*existing_prog == include_prog) | |
1635 | return -EEXIST; | |
1636 | } | |
1637 | } | |
1638 | ||
170a7e3e SY |
1639 | if (exclude_prog && !found_exclude) |
1640 | return -ENOENT; | |
1641 | ||
e87c6bc3 YS |
1642 | /* How many progs (not NULL) will be in the new array? */ |
1643 | new_prog_cnt = carry_prog_cnt; | |
1644 | if (include_prog) | |
1645 | new_prog_cnt += 1; | |
1646 | ||
1647 | /* Do we have any prog (not NULL) in the new array? */ | |
1648 | if (!new_prog_cnt) { | |
1649 | *new_array = NULL; | |
1650 | return 0; | |
1651 | } | |
1652 | ||
1653 | /* +1 as the end of prog_array is marked with NULL */ | |
1654 | array = bpf_prog_array_alloc(new_prog_cnt + 1, GFP_KERNEL); | |
1655 | if (!array) | |
1656 | return -ENOMEM; | |
1657 | ||
1658 | /* Fill in the new prog array */ | |
1659 | if (carry_prog_cnt) { | |
1660 | existing_prog = old_array->progs; | |
1661 | for (; *existing_prog; existing_prog++) | |
1662 | if (*existing_prog != exclude_prog && | |
1663 | *existing_prog != &dummy_bpf_prog.prog) | |
1664 | array->progs[new_prog_idx++] = *existing_prog; | |
1665 | } | |
1666 | if (include_prog) | |
1667 | array->progs[new_prog_idx++] = include_prog; | |
1668 | array->progs[new_prog_idx] = NULL; | |
1669 | *new_array = array; | |
1670 | return 0; | |
1671 | } | |
1672 | ||
f371b304 | 1673 | int bpf_prog_array_copy_info(struct bpf_prog_array __rcu *array, |
3a38bb98 YS |
1674 | u32 *prog_ids, u32 request_cnt, |
1675 | u32 *prog_cnt) | |
f371b304 | 1676 | { |
3a38bb98 | 1677 | struct bpf_prog **prog; |
f371b304 YS |
1678 | u32 cnt = 0; |
1679 | ||
1680 | if (array) | |
1681 | cnt = bpf_prog_array_length(array); | |
1682 | ||
3a38bb98 | 1683 | *prog_cnt = cnt; |
f371b304 YS |
1684 | |
1685 | /* return early if user requested only program count or nothing to copy */ | |
1686 | if (!request_cnt || !cnt) | |
1687 | return 0; | |
1688 | ||
3a38bb98 YS |
1689 | /* this function is called under trace/bpf_trace.c: bpf_event_mutex */ |
1690 | prog = rcu_dereference_check(array, 1)->progs; | |
1691 | return bpf_prog_array_copy_core(prog, prog_ids, request_cnt) ? -ENOSPC | |
1692 | : 0; | |
f371b304 YS |
1693 | } |
1694 | ||
60a3b225 DB |
1695 | static void bpf_prog_free_deferred(struct work_struct *work) |
1696 | { | |
09756af4 | 1697 | struct bpf_prog_aux *aux; |
1c2a088a | 1698 | int i; |
60a3b225 | 1699 | |
09756af4 | 1700 | aux = container_of(work, struct bpf_prog_aux, work); |
ab3f0063 JK |
1701 | if (bpf_prog_is_dev_bound(aux)) |
1702 | bpf_prog_offload_destroy(aux->prog); | |
c195651e YS |
1703 | #ifdef CONFIG_PERF_EVENTS |
1704 | if (aux->prog->has_callchain_buf) | |
1705 | put_callchain_buffers(); | |
1706 | #endif | |
1c2a088a AS |
1707 | for (i = 0; i < aux->func_cnt; i++) |
1708 | bpf_jit_free(aux->func[i]); | |
1709 | if (aux->func_cnt) { | |
1710 | kfree(aux->func); | |
1711 | bpf_prog_unlock_free(aux->prog); | |
1712 | } else { | |
1713 | bpf_jit_free(aux->prog); | |
1714 | } | |
60a3b225 DB |
1715 | } |
1716 | ||
1717 | /* Free internal BPF program */ | |
7ae457c1 | 1718 | void bpf_prog_free(struct bpf_prog *fp) |
f5bffecd | 1719 | { |
09756af4 | 1720 | struct bpf_prog_aux *aux = fp->aux; |
60a3b225 | 1721 | |
09756af4 | 1722 | INIT_WORK(&aux->work, bpf_prog_free_deferred); |
09756af4 | 1723 | schedule_work(&aux->work); |
f5bffecd | 1724 | } |
7ae457c1 | 1725 | EXPORT_SYMBOL_GPL(bpf_prog_free); |
f89b7755 | 1726 | |
3ad00405 DB |
1727 | /* RNG for unpriviledged user space with separated state from prandom_u32(). */ |
1728 | static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state); | |
1729 | ||
1730 | void bpf_user_rnd_init_once(void) | |
1731 | { | |
1732 | prandom_init_once(&bpf_user_rnd_state); | |
1733 | } | |
1734 | ||
f3694e00 | 1735 | BPF_CALL_0(bpf_user_rnd_u32) |
3ad00405 DB |
1736 | { |
1737 | /* Should someone ever have the rather unwise idea to use some | |
1738 | * of the registers passed into this function, then note that | |
1739 | * this function is called from native eBPF and classic-to-eBPF | |
1740 | * transformations. Register assignments from both sides are | |
1741 | * different, f.e. classic always sets fn(ctx, A, X) here. | |
1742 | */ | |
1743 | struct rnd_state *state; | |
1744 | u32 res; | |
1745 | ||
1746 | state = &get_cpu_var(bpf_user_rnd_state); | |
1747 | res = prandom_u32_state(state); | |
b761fe22 | 1748 | put_cpu_var(bpf_user_rnd_state); |
3ad00405 DB |
1749 | |
1750 | return res; | |
1751 | } | |
1752 | ||
3ba67dab DB |
1753 | /* Weak definitions of helper functions in case we don't have bpf syscall. */ |
1754 | const struct bpf_func_proto bpf_map_lookup_elem_proto __weak; | |
1755 | const struct bpf_func_proto bpf_map_update_elem_proto __weak; | |
1756 | const struct bpf_func_proto bpf_map_delete_elem_proto __weak; | |
1757 | ||
03e69b50 | 1758 | const struct bpf_func_proto bpf_get_prandom_u32_proto __weak; |
c04167ce | 1759 | const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak; |
2d0e30c3 | 1760 | const struct bpf_func_proto bpf_get_numa_node_id_proto __weak; |
17ca8cbf | 1761 | const struct bpf_func_proto bpf_ktime_get_ns_proto __weak; |
bd570ff9 | 1762 | |
ffeedafb AS |
1763 | const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak; |
1764 | const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak; | |
1765 | const struct bpf_func_proto bpf_get_current_comm_proto __weak; | |
6bdc9c4c | 1766 | const struct bpf_func_proto bpf_sock_map_update_proto __weak; |
81110384 | 1767 | const struct bpf_func_proto bpf_sock_hash_update_proto __weak; |
bd570ff9 | 1768 | |
0756ea3e AS |
1769 | const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void) |
1770 | { | |
1771 | return NULL; | |
1772 | } | |
03e69b50 | 1773 | |
555c8a86 DB |
1774 | u64 __weak |
1775 | bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size, | |
1776 | void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy) | |
bd570ff9 | 1777 | { |
555c8a86 | 1778 | return -ENOTSUPP; |
bd570ff9 | 1779 | } |
6cb5fb38 | 1780 | EXPORT_SYMBOL_GPL(bpf_event_output); |
bd570ff9 | 1781 | |
3324b584 DB |
1782 | /* Always built-in helper functions. */ |
1783 | const struct bpf_func_proto bpf_tail_call_proto = { | |
1784 | .func = NULL, | |
1785 | .gpl_only = false, | |
1786 | .ret_type = RET_VOID, | |
1787 | .arg1_type = ARG_PTR_TO_CTX, | |
1788 | .arg2_type = ARG_CONST_MAP_PTR, | |
1789 | .arg3_type = ARG_ANYTHING, | |
1790 | }; | |
1791 | ||
9383191d DB |
1792 | /* Stub for JITs that only support cBPF. eBPF programs are interpreted. |
1793 | * It is encouraged to implement bpf_int_jit_compile() instead, so that | |
1794 | * eBPF and implicitly also cBPF can get JITed! | |
1795 | */ | |
d1c55ab5 | 1796 | struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog) |
3324b584 | 1797 | { |
d1c55ab5 | 1798 | return prog; |
3324b584 DB |
1799 | } |
1800 | ||
9383191d DB |
1801 | /* Stub for JITs that support eBPF. All cBPF code gets transformed into |
1802 | * eBPF by the kernel and is later compiled by bpf_int_jit_compile(). | |
1803 | */ | |
1804 | void __weak bpf_jit_compile(struct bpf_prog *prog) | |
1805 | { | |
1806 | } | |
1807 | ||
17bedab2 | 1808 | bool __weak bpf_helper_changes_pkt_data(void *func) |
969bf05e AS |
1809 | { |
1810 | return false; | |
1811 | } | |
1812 | ||
f89b7755 AS |
1813 | /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call |
1814 | * skb_copy_bits(), so provide a weak definition of it for NET-less config. | |
1815 | */ | |
1816 | int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to, | |
1817 | int len) | |
1818 | { | |
1819 | return -EFAULT; | |
1820 | } | |
a67edbf4 DB |
1821 | |
1822 | /* All definitions of tracepoints related to BPF. */ | |
1823 | #define CREATE_TRACE_POINTS | |
1824 | #include <linux/bpf_trace.h> | |
1825 | ||
1826 | EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception); |