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