]>
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> |
f5bffecd | 31 | |
3324b584 DB |
32 | #include <asm/unaligned.h> |
33 | ||
f5bffecd AS |
34 | /* Registers */ |
35 | #define BPF_R0 regs[BPF_REG_0] | |
36 | #define BPF_R1 regs[BPF_REG_1] | |
37 | #define BPF_R2 regs[BPF_REG_2] | |
38 | #define BPF_R3 regs[BPF_REG_3] | |
39 | #define BPF_R4 regs[BPF_REG_4] | |
40 | #define BPF_R5 regs[BPF_REG_5] | |
41 | #define BPF_R6 regs[BPF_REG_6] | |
42 | #define BPF_R7 regs[BPF_REG_7] | |
43 | #define BPF_R8 regs[BPF_REG_8] | |
44 | #define BPF_R9 regs[BPF_REG_9] | |
45 | #define BPF_R10 regs[BPF_REG_10] | |
46 | ||
47 | /* Named registers */ | |
48 | #define DST regs[insn->dst_reg] | |
49 | #define SRC regs[insn->src_reg] | |
50 | #define FP regs[BPF_REG_FP] | |
51 | #define ARG1 regs[BPF_REG_ARG1] | |
52 | #define CTX regs[BPF_REG_CTX] | |
53 | #define IMM insn->imm | |
54 | ||
55 | /* No hurry in this branch | |
56 | * | |
57 | * Exported for the bpf jit load helper. | |
58 | */ | |
59 | void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size) | |
60 | { | |
61 | u8 *ptr = NULL; | |
62 | ||
63 | if (k >= SKF_NET_OFF) | |
64 | ptr = skb_network_header(skb) + k - SKF_NET_OFF; | |
65 | else if (k >= SKF_LL_OFF) | |
66 | ptr = skb_mac_header(skb) + k - SKF_LL_OFF; | |
3324b584 | 67 | |
f5bffecd AS |
68 | if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb)) |
69 | return ptr; | |
70 | ||
71 | return NULL; | |
72 | } | |
73 | ||
60a3b225 DB |
74 | struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags) |
75 | { | |
76 | gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO | | |
77 | gfp_extra_flags; | |
09756af4 | 78 | struct bpf_prog_aux *aux; |
60a3b225 DB |
79 | struct bpf_prog *fp; |
80 | ||
81 | size = round_up(size, PAGE_SIZE); | |
82 | fp = __vmalloc(size, gfp_flags, PAGE_KERNEL); | |
83 | if (fp == NULL) | |
84 | return NULL; | |
85 | ||
a91263d5 DB |
86 | kmemcheck_annotate_bitfield(fp, meta); |
87 | ||
09756af4 AS |
88 | aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags); |
89 | if (aux == NULL) { | |
60a3b225 DB |
90 | vfree(fp); |
91 | return NULL; | |
92 | } | |
93 | ||
94 | fp->pages = size / PAGE_SIZE; | |
09756af4 | 95 | fp->aux = aux; |
e9d8afa9 | 96 | fp->aux->prog = fp; |
60a3b225 DB |
97 | |
98 | return fp; | |
99 | } | |
100 | EXPORT_SYMBOL_GPL(bpf_prog_alloc); | |
101 | ||
102 | struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size, | |
103 | gfp_t gfp_extra_flags) | |
104 | { | |
105 | gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO | | |
106 | gfp_extra_flags; | |
107 | struct bpf_prog *fp; | |
108 | ||
109 | BUG_ON(fp_old == NULL); | |
110 | ||
111 | size = round_up(size, PAGE_SIZE); | |
112 | if (size <= fp_old->pages * PAGE_SIZE) | |
113 | return fp_old; | |
114 | ||
115 | fp = __vmalloc(size, gfp_flags, PAGE_KERNEL); | |
116 | if (fp != NULL) { | |
a91263d5 DB |
117 | kmemcheck_annotate_bitfield(fp, meta); |
118 | ||
60a3b225 DB |
119 | memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE); |
120 | fp->pages = size / PAGE_SIZE; | |
e9d8afa9 | 121 | fp->aux->prog = fp; |
60a3b225 | 122 | |
09756af4 | 123 | /* We keep fp->aux from fp_old around in the new |
60a3b225 DB |
124 | * reallocated structure. |
125 | */ | |
09756af4 | 126 | fp_old->aux = NULL; |
60a3b225 DB |
127 | __bpf_prog_free(fp_old); |
128 | } | |
129 | ||
130 | return fp; | |
131 | } | |
60a3b225 DB |
132 | |
133 | void __bpf_prog_free(struct bpf_prog *fp) | |
134 | { | |
09756af4 | 135 | kfree(fp->aux); |
60a3b225 DB |
136 | vfree(fp); |
137 | } | |
60a3b225 | 138 | |
c237ee5e DB |
139 | static bool bpf_is_jmp_and_has_target(const struct bpf_insn *insn) |
140 | { | |
141 | return BPF_CLASS(insn->code) == BPF_JMP && | |
142 | /* Call and Exit are both special jumps with no | |
143 | * target inside the BPF instruction image. | |
144 | */ | |
145 | BPF_OP(insn->code) != BPF_CALL && | |
146 | BPF_OP(insn->code) != BPF_EXIT; | |
147 | } | |
148 | ||
149 | static void bpf_adj_branches(struct bpf_prog *prog, u32 pos, u32 delta) | |
150 | { | |
151 | struct bpf_insn *insn = prog->insnsi; | |
152 | u32 i, insn_cnt = prog->len; | |
153 | ||
154 | for (i = 0; i < insn_cnt; i++, insn++) { | |
155 | if (!bpf_is_jmp_and_has_target(insn)) | |
156 | continue; | |
157 | ||
158 | /* Adjust offset of jmps if we cross boundaries. */ | |
159 | if (i < pos && i + insn->off + 1 > pos) | |
160 | insn->off += delta; | |
161 | else if (i > pos + delta && i + insn->off + 1 <= pos + delta) | |
162 | insn->off -= delta; | |
163 | } | |
164 | } | |
165 | ||
166 | struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off, | |
167 | const struct bpf_insn *patch, u32 len) | |
168 | { | |
169 | u32 insn_adj_cnt, insn_rest, insn_delta = len - 1; | |
170 | struct bpf_prog *prog_adj; | |
171 | ||
172 | /* Since our patchlet doesn't expand the image, we're done. */ | |
173 | if (insn_delta == 0) { | |
174 | memcpy(prog->insnsi + off, patch, sizeof(*patch)); | |
175 | return prog; | |
176 | } | |
177 | ||
178 | insn_adj_cnt = prog->len + insn_delta; | |
179 | ||
180 | /* Several new instructions need to be inserted. Make room | |
181 | * for them. Likely, there's no need for a new allocation as | |
182 | * last page could have large enough tailroom. | |
183 | */ | |
184 | prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt), | |
185 | GFP_USER); | |
186 | if (!prog_adj) | |
187 | return NULL; | |
188 | ||
189 | prog_adj->len = insn_adj_cnt; | |
190 | ||
191 | /* Patching happens in 3 steps: | |
192 | * | |
193 | * 1) Move over tail of insnsi from next instruction onwards, | |
194 | * so we can patch the single target insn with one or more | |
195 | * new ones (patching is always from 1 to n insns, n > 0). | |
196 | * 2) Inject new instructions at the target location. | |
197 | * 3) Adjust branch offsets if necessary. | |
198 | */ | |
199 | insn_rest = insn_adj_cnt - off - len; | |
200 | ||
201 | memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1, | |
202 | sizeof(*patch) * insn_rest); | |
203 | memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len); | |
204 | ||
205 | bpf_adj_branches(prog_adj, off, insn_delta); | |
206 | ||
207 | return prog_adj; | |
208 | } | |
209 | ||
b954d834 | 210 | #ifdef CONFIG_BPF_JIT |
738cbe72 DB |
211 | struct bpf_binary_header * |
212 | bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr, | |
213 | unsigned int alignment, | |
214 | bpf_jit_fill_hole_t bpf_fill_ill_insns) | |
215 | { | |
216 | struct bpf_binary_header *hdr; | |
217 | unsigned int size, hole, start; | |
218 | ||
219 | /* Most of BPF filters are really small, but if some of them | |
220 | * fill a page, allow at least 128 extra bytes to insert a | |
221 | * random section of illegal instructions. | |
222 | */ | |
223 | size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE); | |
224 | hdr = module_alloc(size); | |
225 | if (hdr == NULL) | |
226 | return NULL; | |
227 | ||
228 | /* Fill space with illegal/arch-dep instructions. */ | |
229 | bpf_fill_ill_insns(hdr, size); | |
230 | ||
231 | hdr->pages = size / PAGE_SIZE; | |
232 | hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)), | |
233 | PAGE_SIZE - sizeof(*hdr)); | |
234 | start = (prandom_u32() % hole) & ~(alignment - 1); | |
235 | ||
236 | /* Leave a random number of instructions before BPF code. */ | |
237 | *image_ptr = &hdr->image[start]; | |
238 | ||
239 | return hdr; | |
240 | } | |
241 | ||
242 | void bpf_jit_binary_free(struct bpf_binary_header *hdr) | |
243 | { | |
be1f221c | 244 | module_memfree(hdr); |
738cbe72 | 245 | } |
b954d834 | 246 | #endif /* CONFIG_BPF_JIT */ |
738cbe72 | 247 | |
f5bffecd AS |
248 | /* Base function for offset calculation. Needs to go into .text section, |
249 | * therefore keeping it non-static as well; will also be used by JITs | |
250 | * anyway later on, so do not let the compiler omit it. | |
251 | */ | |
252 | noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
253 | { | |
254 | return 0; | |
255 | } | |
4d9c5c53 | 256 | EXPORT_SYMBOL_GPL(__bpf_call_base); |
f5bffecd AS |
257 | |
258 | /** | |
7ae457c1 AS |
259 | * __bpf_prog_run - run eBPF program on a given context |
260 | * @ctx: is the data we are operating on | |
261 | * @insn: is the array of eBPF instructions | |
f5bffecd | 262 | * |
7ae457c1 | 263 | * Decode and execute eBPF instructions. |
f5bffecd | 264 | */ |
7ae457c1 | 265 | static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn) |
f5bffecd AS |
266 | { |
267 | u64 stack[MAX_BPF_STACK / sizeof(u64)]; | |
268 | u64 regs[MAX_BPF_REG], tmp; | |
269 | static const void *jumptable[256] = { | |
270 | [0 ... 255] = &&default_label, | |
271 | /* Now overwrite non-defaults ... */ | |
272 | /* 32 bit ALU operations */ | |
273 | [BPF_ALU | BPF_ADD | BPF_X] = &&ALU_ADD_X, | |
274 | [BPF_ALU | BPF_ADD | BPF_K] = &&ALU_ADD_K, | |
275 | [BPF_ALU | BPF_SUB | BPF_X] = &&ALU_SUB_X, | |
276 | [BPF_ALU | BPF_SUB | BPF_K] = &&ALU_SUB_K, | |
277 | [BPF_ALU | BPF_AND | BPF_X] = &&ALU_AND_X, | |
278 | [BPF_ALU | BPF_AND | BPF_K] = &&ALU_AND_K, | |
279 | [BPF_ALU | BPF_OR | BPF_X] = &&ALU_OR_X, | |
280 | [BPF_ALU | BPF_OR | BPF_K] = &&ALU_OR_K, | |
281 | [BPF_ALU | BPF_LSH | BPF_X] = &&ALU_LSH_X, | |
282 | [BPF_ALU | BPF_LSH | BPF_K] = &&ALU_LSH_K, | |
283 | [BPF_ALU | BPF_RSH | BPF_X] = &&ALU_RSH_X, | |
284 | [BPF_ALU | BPF_RSH | BPF_K] = &&ALU_RSH_K, | |
285 | [BPF_ALU | BPF_XOR | BPF_X] = &&ALU_XOR_X, | |
286 | [BPF_ALU | BPF_XOR | BPF_K] = &&ALU_XOR_K, | |
287 | [BPF_ALU | BPF_MUL | BPF_X] = &&ALU_MUL_X, | |
288 | [BPF_ALU | BPF_MUL | BPF_K] = &&ALU_MUL_K, | |
289 | [BPF_ALU | BPF_MOV | BPF_X] = &&ALU_MOV_X, | |
290 | [BPF_ALU | BPF_MOV | BPF_K] = &&ALU_MOV_K, | |
291 | [BPF_ALU | BPF_DIV | BPF_X] = &&ALU_DIV_X, | |
292 | [BPF_ALU | BPF_DIV | BPF_K] = &&ALU_DIV_K, | |
293 | [BPF_ALU | BPF_MOD | BPF_X] = &&ALU_MOD_X, | |
294 | [BPF_ALU | BPF_MOD | BPF_K] = &&ALU_MOD_K, | |
295 | [BPF_ALU | BPF_NEG] = &&ALU_NEG, | |
296 | [BPF_ALU | BPF_END | BPF_TO_BE] = &&ALU_END_TO_BE, | |
297 | [BPF_ALU | BPF_END | BPF_TO_LE] = &&ALU_END_TO_LE, | |
298 | /* 64 bit ALU operations */ | |
299 | [BPF_ALU64 | BPF_ADD | BPF_X] = &&ALU64_ADD_X, | |
300 | [BPF_ALU64 | BPF_ADD | BPF_K] = &&ALU64_ADD_K, | |
301 | [BPF_ALU64 | BPF_SUB | BPF_X] = &&ALU64_SUB_X, | |
302 | [BPF_ALU64 | BPF_SUB | BPF_K] = &&ALU64_SUB_K, | |
303 | [BPF_ALU64 | BPF_AND | BPF_X] = &&ALU64_AND_X, | |
304 | [BPF_ALU64 | BPF_AND | BPF_K] = &&ALU64_AND_K, | |
305 | [BPF_ALU64 | BPF_OR | BPF_X] = &&ALU64_OR_X, | |
306 | [BPF_ALU64 | BPF_OR | BPF_K] = &&ALU64_OR_K, | |
307 | [BPF_ALU64 | BPF_LSH | BPF_X] = &&ALU64_LSH_X, | |
308 | [BPF_ALU64 | BPF_LSH | BPF_K] = &&ALU64_LSH_K, | |
309 | [BPF_ALU64 | BPF_RSH | BPF_X] = &&ALU64_RSH_X, | |
310 | [BPF_ALU64 | BPF_RSH | BPF_K] = &&ALU64_RSH_K, | |
311 | [BPF_ALU64 | BPF_XOR | BPF_X] = &&ALU64_XOR_X, | |
312 | [BPF_ALU64 | BPF_XOR | BPF_K] = &&ALU64_XOR_K, | |
313 | [BPF_ALU64 | BPF_MUL | BPF_X] = &&ALU64_MUL_X, | |
314 | [BPF_ALU64 | BPF_MUL | BPF_K] = &&ALU64_MUL_K, | |
315 | [BPF_ALU64 | BPF_MOV | BPF_X] = &&ALU64_MOV_X, | |
316 | [BPF_ALU64 | BPF_MOV | BPF_K] = &&ALU64_MOV_K, | |
317 | [BPF_ALU64 | BPF_ARSH | BPF_X] = &&ALU64_ARSH_X, | |
318 | [BPF_ALU64 | BPF_ARSH | BPF_K] = &&ALU64_ARSH_K, | |
319 | [BPF_ALU64 | BPF_DIV | BPF_X] = &&ALU64_DIV_X, | |
320 | [BPF_ALU64 | BPF_DIV | BPF_K] = &&ALU64_DIV_K, | |
321 | [BPF_ALU64 | BPF_MOD | BPF_X] = &&ALU64_MOD_X, | |
322 | [BPF_ALU64 | BPF_MOD | BPF_K] = &&ALU64_MOD_K, | |
323 | [BPF_ALU64 | BPF_NEG] = &&ALU64_NEG, | |
324 | /* Call instruction */ | |
325 | [BPF_JMP | BPF_CALL] = &&JMP_CALL, | |
04fd61ab | 326 | [BPF_JMP | BPF_CALL | BPF_X] = &&JMP_TAIL_CALL, |
f5bffecd AS |
327 | /* Jumps */ |
328 | [BPF_JMP | BPF_JA] = &&JMP_JA, | |
329 | [BPF_JMP | BPF_JEQ | BPF_X] = &&JMP_JEQ_X, | |
330 | [BPF_JMP | BPF_JEQ | BPF_K] = &&JMP_JEQ_K, | |
331 | [BPF_JMP | BPF_JNE | BPF_X] = &&JMP_JNE_X, | |
332 | [BPF_JMP | BPF_JNE | BPF_K] = &&JMP_JNE_K, | |
333 | [BPF_JMP | BPF_JGT | BPF_X] = &&JMP_JGT_X, | |
334 | [BPF_JMP | BPF_JGT | BPF_K] = &&JMP_JGT_K, | |
335 | [BPF_JMP | BPF_JGE | BPF_X] = &&JMP_JGE_X, | |
336 | [BPF_JMP | BPF_JGE | BPF_K] = &&JMP_JGE_K, | |
337 | [BPF_JMP | BPF_JSGT | BPF_X] = &&JMP_JSGT_X, | |
338 | [BPF_JMP | BPF_JSGT | BPF_K] = &&JMP_JSGT_K, | |
339 | [BPF_JMP | BPF_JSGE | BPF_X] = &&JMP_JSGE_X, | |
340 | [BPF_JMP | BPF_JSGE | BPF_K] = &&JMP_JSGE_K, | |
341 | [BPF_JMP | BPF_JSET | BPF_X] = &&JMP_JSET_X, | |
342 | [BPF_JMP | BPF_JSET | BPF_K] = &&JMP_JSET_K, | |
343 | /* Program return */ | |
344 | [BPF_JMP | BPF_EXIT] = &&JMP_EXIT, | |
345 | /* Store instructions */ | |
346 | [BPF_STX | BPF_MEM | BPF_B] = &&STX_MEM_B, | |
347 | [BPF_STX | BPF_MEM | BPF_H] = &&STX_MEM_H, | |
348 | [BPF_STX | BPF_MEM | BPF_W] = &&STX_MEM_W, | |
349 | [BPF_STX | BPF_MEM | BPF_DW] = &&STX_MEM_DW, | |
350 | [BPF_STX | BPF_XADD | BPF_W] = &&STX_XADD_W, | |
351 | [BPF_STX | BPF_XADD | BPF_DW] = &&STX_XADD_DW, | |
352 | [BPF_ST | BPF_MEM | BPF_B] = &&ST_MEM_B, | |
353 | [BPF_ST | BPF_MEM | BPF_H] = &&ST_MEM_H, | |
354 | [BPF_ST | BPF_MEM | BPF_W] = &&ST_MEM_W, | |
355 | [BPF_ST | BPF_MEM | BPF_DW] = &&ST_MEM_DW, | |
356 | /* Load instructions */ | |
357 | [BPF_LDX | BPF_MEM | BPF_B] = &&LDX_MEM_B, | |
358 | [BPF_LDX | BPF_MEM | BPF_H] = &&LDX_MEM_H, | |
359 | [BPF_LDX | BPF_MEM | BPF_W] = &&LDX_MEM_W, | |
360 | [BPF_LDX | BPF_MEM | BPF_DW] = &&LDX_MEM_DW, | |
361 | [BPF_LD | BPF_ABS | BPF_W] = &&LD_ABS_W, | |
362 | [BPF_LD | BPF_ABS | BPF_H] = &&LD_ABS_H, | |
363 | [BPF_LD | BPF_ABS | BPF_B] = &&LD_ABS_B, | |
364 | [BPF_LD | BPF_IND | BPF_W] = &&LD_IND_W, | |
365 | [BPF_LD | BPF_IND | BPF_H] = &&LD_IND_H, | |
366 | [BPF_LD | BPF_IND | BPF_B] = &&LD_IND_B, | |
02ab695b | 367 | [BPF_LD | BPF_IMM | BPF_DW] = &&LD_IMM_DW, |
f5bffecd | 368 | }; |
04fd61ab | 369 | u32 tail_call_cnt = 0; |
f5bffecd AS |
370 | void *ptr; |
371 | int off; | |
372 | ||
373 | #define CONT ({ insn++; goto select_insn; }) | |
374 | #define CONT_JMP ({ insn++; goto select_insn; }) | |
375 | ||
376 | FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; | |
377 | ARG1 = (u64) (unsigned long) ctx; | |
378 | ||
f5bffecd AS |
379 | select_insn: |
380 | goto *jumptable[insn->code]; | |
381 | ||
382 | /* ALU */ | |
383 | #define ALU(OPCODE, OP) \ | |
384 | ALU64_##OPCODE##_X: \ | |
385 | DST = DST OP SRC; \ | |
386 | CONT; \ | |
387 | ALU_##OPCODE##_X: \ | |
388 | DST = (u32) DST OP (u32) SRC; \ | |
389 | CONT; \ | |
390 | ALU64_##OPCODE##_K: \ | |
391 | DST = DST OP IMM; \ | |
392 | CONT; \ | |
393 | ALU_##OPCODE##_K: \ | |
394 | DST = (u32) DST OP (u32) IMM; \ | |
395 | CONT; | |
396 | ||
397 | ALU(ADD, +) | |
398 | ALU(SUB, -) | |
399 | ALU(AND, &) | |
400 | ALU(OR, |) | |
401 | ALU(LSH, <<) | |
402 | ALU(RSH, >>) | |
403 | ALU(XOR, ^) | |
404 | ALU(MUL, *) | |
405 | #undef ALU | |
406 | ALU_NEG: | |
407 | DST = (u32) -DST; | |
408 | CONT; | |
409 | ALU64_NEG: | |
410 | DST = -DST; | |
411 | CONT; | |
412 | ALU_MOV_X: | |
413 | DST = (u32) SRC; | |
414 | CONT; | |
415 | ALU_MOV_K: | |
416 | DST = (u32) IMM; | |
417 | CONT; | |
418 | ALU64_MOV_X: | |
419 | DST = SRC; | |
420 | CONT; | |
421 | ALU64_MOV_K: | |
422 | DST = IMM; | |
423 | CONT; | |
02ab695b AS |
424 | LD_IMM_DW: |
425 | DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32; | |
426 | insn++; | |
427 | CONT; | |
f5bffecd AS |
428 | ALU64_ARSH_X: |
429 | (*(s64 *) &DST) >>= SRC; | |
430 | CONT; | |
431 | ALU64_ARSH_K: | |
432 | (*(s64 *) &DST) >>= IMM; | |
433 | CONT; | |
434 | ALU64_MOD_X: | |
435 | if (unlikely(SRC == 0)) | |
436 | return 0; | |
876a7ae6 AS |
437 | div64_u64_rem(DST, SRC, &tmp); |
438 | DST = tmp; | |
f5bffecd AS |
439 | CONT; |
440 | ALU_MOD_X: | |
441 | if (unlikely(SRC == 0)) | |
442 | return 0; | |
443 | tmp = (u32) DST; | |
444 | DST = do_div(tmp, (u32) SRC); | |
445 | CONT; | |
446 | ALU64_MOD_K: | |
876a7ae6 AS |
447 | div64_u64_rem(DST, IMM, &tmp); |
448 | DST = tmp; | |
f5bffecd AS |
449 | CONT; |
450 | ALU_MOD_K: | |
451 | tmp = (u32) DST; | |
452 | DST = do_div(tmp, (u32) IMM); | |
453 | CONT; | |
454 | ALU64_DIV_X: | |
455 | if (unlikely(SRC == 0)) | |
456 | return 0; | |
876a7ae6 | 457 | DST = div64_u64(DST, SRC); |
f5bffecd AS |
458 | CONT; |
459 | ALU_DIV_X: | |
460 | if (unlikely(SRC == 0)) | |
461 | return 0; | |
462 | tmp = (u32) DST; | |
463 | do_div(tmp, (u32) SRC); | |
464 | DST = (u32) tmp; | |
465 | CONT; | |
466 | ALU64_DIV_K: | |
876a7ae6 | 467 | DST = div64_u64(DST, IMM); |
f5bffecd AS |
468 | CONT; |
469 | ALU_DIV_K: | |
470 | tmp = (u32) DST; | |
471 | do_div(tmp, (u32) IMM); | |
472 | DST = (u32) tmp; | |
473 | CONT; | |
474 | ALU_END_TO_BE: | |
475 | switch (IMM) { | |
476 | case 16: | |
477 | DST = (__force u16) cpu_to_be16(DST); | |
478 | break; | |
479 | case 32: | |
480 | DST = (__force u32) cpu_to_be32(DST); | |
481 | break; | |
482 | case 64: | |
483 | DST = (__force u64) cpu_to_be64(DST); | |
484 | break; | |
485 | } | |
486 | CONT; | |
487 | ALU_END_TO_LE: | |
488 | switch (IMM) { | |
489 | case 16: | |
490 | DST = (__force u16) cpu_to_le16(DST); | |
491 | break; | |
492 | case 32: | |
493 | DST = (__force u32) cpu_to_le32(DST); | |
494 | break; | |
495 | case 64: | |
496 | DST = (__force u64) cpu_to_le64(DST); | |
497 | break; | |
498 | } | |
499 | CONT; | |
500 | ||
501 | /* CALL */ | |
502 | JMP_CALL: | |
503 | /* Function call scratches BPF_R1-BPF_R5 registers, | |
504 | * preserves BPF_R6-BPF_R9, and stores return value | |
505 | * into BPF_R0. | |
506 | */ | |
507 | BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3, | |
508 | BPF_R4, BPF_R5); | |
509 | CONT; | |
510 | ||
04fd61ab AS |
511 | JMP_TAIL_CALL: { |
512 | struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2; | |
513 | struct bpf_array *array = container_of(map, struct bpf_array, map); | |
514 | struct bpf_prog *prog; | |
515 | u64 index = BPF_R3; | |
516 | ||
517 | if (unlikely(index >= array->map.max_entries)) | |
518 | goto out; | |
519 | ||
520 | if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT)) | |
521 | goto out; | |
522 | ||
523 | tail_call_cnt++; | |
524 | ||
2a36f0b9 | 525 | prog = READ_ONCE(array->ptrs[index]); |
04fd61ab AS |
526 | if (unlikely(!prog)) |
527 | goto out; | |
528 | ||
c4675f93 DB |
529 | /* ARG1 at this point is guaranteed to point to CTX from |
530 | * the verifier side due to the fact that the tail call is | |
531 | * handeled like a helper, that is, bpf_tail_call_proto, | |
532 | * where arg1_type is ARG_PTR_TO_CTX. | |
533 | */ | |
04fd61ab AS |
534 | insn = prog->insnsi; |
535 | goto select_insn; | |
536 | out: | |
537 | CONT; | |
538 | } | |
f5bffecd AS |
539 | /* JMP */ |
540 | JMP_JA: | |
541 | insn += insn->off; | |
542 | CONT; | |
543 | JMP_JEQ_X: | |
544 | if (DST == SRC) { | |
545 | insn += insn->off; | |
546 | CONT_JMP; | |
547 | } | |
548 | CONT; | |
549 | JMP_JEQ_K: | |
550 | if (DST == IMM) { | |
551 | insn += insn->off; | |
552 | CONT_JMP; | |
553 | } | |
554 | CONT; | |
555 | JMP_JNE_X: | |
556 | if (DST != SRC) { | |
557 | insn += insn->off; | |
558 | CONT_JMP; | |
559 | } | |
560 | CONT; | |
561 | JMP_JNE_K: | |
562 | if (DST != IMM) { | |
563 | insn += insn->off; | |
564 | CONT_JMP; | |
565 | } | |
566 | CONT; | |
567 | JMP_JGT_X: | |
568 | if (DST > SRC) { | |
569 | insn += insn->off; | |
570 | CONT_JMP; | |
571 | } | |
572 | CONT; | |
573 | JMP_JGT_K: | |
574 | if (DST > IMM) { | |
575 | insn += insn->off; | |
576 | CONT_JMP; | |
577 | } | |
578 | CONT; | |
579 | JMP_JGE_X: | |
580 | if (DST >= SRC) { | |
581 | insn += insn->off; | |
582 | CONT_JMP; | |
583 | } | |
584 | CONT; | |
585 | JMP_JGE_K: | |
586 | if (DST >= IMM) { | |
587 | insn += insn->off; | |
588 | CONT_JMP; | |
589 | } | |
590 | CONT; | |
591 | JMP_JSGT_X: | |
592 | if (((s64) DST) > ((s64) SRC)) { | |
593 | insn += insn->off; | |
594 | CONT_JMP; | |
595 | } | |
596 | CONT; | |
597 | JMP_JSGT_K: | |
598 | if (((s64) DST) > ((s64) IMM)) { | |
599 | insn += insn->off; | |
600 | CONT_JMP; | |
601 | } | |
602 | CONT; | |
603 | JMP_JSGE_X: | |
604 | if (((s64) DST) >= ((s64) SRC)) { | |
605 | insn += insn->off; | |
606 | CONT_JMP; | |
607 | } | |
608 | CONT; | |
609 | JMP_JSGE_K: | |
610 | if (((s64) DST) >= ((s64) IMM)) { | |
611 | insn += insn->off; | |
612 | CONT_JMP; | |
613 | } | |
614 | CONT; | |
615 | JMP_JSET_X: | |
616 | if (DST & SRC) { | |
617 | insn += insn->off; | |
618 | CONT_JMP; | |
619 | } | |
620 | CONT; | |
621 | JMP_JSET_K: | |
622 | if (DST & IMM) { | |
623 | insn += insn->off; | |
624 | CONT_JMP; | |
625 | } | |
626 | CONT; | |
627 | JMP_EXIT: | |
628 | return BPF_R0; | |
629 | ||
630 | /* STX and ST and LDX*/ | |
631 | #define LDST(SIZEOP, SIZE) \ | |
632 | STX_MEM_##SIZEOP: \ | |
633 | *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \ | |
634 | CONT; \ | |
635 | ST_MEM_##SIZEOP: \ | |
636 | *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \ | |
637 | CONT; \ | |
638 | LDX_MEM_##SIZEOP: \ | |
639 | DST = *(SIZE *)(unsigned long) (SRC + insn->off); \ | |
640 | CONT; | |
641 | ||
642 | LDST(B, u8) | |
643 | LDST(H, u16) | |
644 | LDST(W, u32) | |
645 | LDST(DW, u64) | |
646 | #undef LDST | |
647 | STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */ | |
648 | atomic_add((u32) SRC, (atomic_t *)(unsigned long) | |
649 | (DST + insn->off)); | |
650 | CONT; | |
651 | STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */ | |
652 | atomic64_add((u64) SRC, (atomic64_t *)(unsigned long) | |
653 | (DST + insn->off)); | |
654 | CONT; | |
655 | LD_ABS_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + imm32)) */ | |
656 | off = IMM; | |
657 | load_word: | |
658 | /* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are | |
659 | * only appearing in the programs where ctx == | |
660 | * skb. All programs keep 'ctx' in regs[BPF_REG_CTX] | |
8fb575ca | 661 | * == BPF_R6, bpf_convert_filter() saves it in BPF_R6, |
f5bffecd AS |
662 | * internal BPF verifier will check that BPF_R6 == |
663 | * ctx. | |
664 | * | |
665 | * BPF_ABS and BPF_IND are wrappers of function calls, | |
666 | * so they scratch BPF_R1-BPF_R5 registers, preserve | |
667 | * BPF_R6-BPF_R9, and store return value into BPF_R0. | |
668 | * | |
669 | * Implicit input: | |
670 | * ctx == skb == BPF_R6 == CTX | |
671 | * | |
672 | * Explicit input: | |
673 | * SRC == any register | |
674 | * IMM == 32-bit immediate | |
675 | * | |
676 | * Output: | |
677 | * BPF_R0 - 8/16/32-bit skb data converted to cpu endianness | |
678 | */ | |
679 | ||
680 | ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 4, &tmp); | |
681 | if (likely(ptr != NULL)) { | |
682 | BPF_R0 = get_unaligned_be32(ptr); | |
683 | CONT; | |
684 | } | |
685 | ||
686 | return 0; | |
687 | LD_ABS_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + imm32)) */ | |
688 | off = IMM; | |
689 | load_half: | |
690 | ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 2, &tmp); | |
691 | if (likely(ptr != NULL)) { | |
692 | BPF_R0 = get_unaligned_be16(ptr); | |
693 | CONT; | |
694 | } | |
695 | ||
696 | return 0; | |
697 | LD_ABS_B: /* BPF_R0 = *(u8 *) (skb->data + imm32) */ | |
698 | off = IMM; | |
699 | load_byte: | |
700 | ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 1, &tmp); | |
701 | if (likely(ptr != NULL)) { | |
702 | BPF_R0 = *(u8 *)ptr; | |
703 | CONT; | |
704 | } | |
705 | ||
706 | return 0; | |
707 | LD_IND_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + src_reg + imm32)) */ | |
708 | off = IMM + SRC; | |
709 | goto load_word; | |
710 | LD_IND_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + src_reg + imm32)) */ | |
711 | off = IMM + SRC; | |
712 | goto load_half; | |
713 | LD_IND_B: /* BPF_R0 = *(u8 *) (skb->data + src_reg + imm32) */ | |
714 | off = IMM + SRC; | |
715 | goto load_byte; | |
716 | ||
717 | default_label: | |
718 | /* If we ever reach this, we have a bug somewhere. */ | |
719 | WARN_RATELIMIT(1, "unknown opcode %02x\n", insn->code); | |
720 | return 0; | |
721 | } | |
39853cc0 | 722 | STACK_FRAME_NON_STANDARD(__bpf_prog_run); /* jump table */ |
f5bffecd | 723 | |
3324b584 DB |
724 | bool bpf_prog_array_compatible(struct bpf_array *array, |
725 | const struct bpf_prog *fp) | |
04fd61ab | 726 | { |
3324b584 DB |
727 | if (!array->owner_prog_type) { |
728 | /* There's no owner yet where we could check for | |
729 | * compatibility. | |
730 | */ | |
04fd61ab AS |
731 | array->owner_prog_type = fp->type; |
732 | array->owner_jited = fp->jited; | |
3324b584 DB |
733 | |
734 | return true; | |
04fd61ab | 735 | } |
3324b584 DB |
736 | |
737 | return array->owner_prog_type == fp->type && | |
738 | array->owner_jited == fp->jited; | |
04fd61ab AS |
739 | } |
740 | ||
3324b584 | 741 | static int bpf_check_tail_call(const struct bpf_prog *fp) |
04fd61ab AS |
742 | { |
743 | struct bpf_prog_aux *aux = fp->aux; | |
744 | int i; | |
745 | ||
746 | for (i = 0; i < aux->used_map_cnt; i++) { | |
3324b584 | 747 | struct bpf_map *map = aux->used_maps[i]; |
04fd61ab | 748 | struct bpf_array *array; |
04fd61ab | 749 | |
04fd61ab AS |
750 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
751 | continue; | |
3324b584 | 752 | |
04fd61ab AS |
753 | array = container_of(map, struct bpf_array, map); |
754 | if (!bpf_prog_array_compatible(array, fp)) | |
755 | return -EINVAL; | |
756 | } | |
757 | ||
758 | return 0; | |
759 | } | |
760 | ||
f5bffecd | 761 | /** |
3324b584 | 762 | * bpf_prog_select_runtime - select exec runtime for BPF program |
7ae457c1 | 763 | * @fp: bpf_prog populated with internal BPF program |
d1c55ab5 | 764 | * @err: pointer to error variable |
f5bffecd | 765 | * |
3324b584 DB |
766 | * Try to JIT eBPF program, if JIT is not available, use interpreter. |
767 | * The BPF program will be executed via BPF_PROG_RUN() macro. | |
f5bffecd | 768 | */ |
d1c55ab5 | 769 | struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err) |
f5bffecd | 770 | { |
7ae457c1 | 771 | fp->bpf_func = (void *) __bpf_prog_run; |
f5bffecd | 772 | |
d1c55ab5 DB |
773 | /* eBPF JITs can rewrite the program in case constant |
774 | * blinding is active. However, in case of error during | |
775 | * blinding, bpf_int_jit_compile() must always return a | |
776 | * valid program, which in this case would simply not | |
777 | * be JITed, but falls back to the interpreter. | |
778 | */ | |
779 | fp = bpf_int_jit_compile(fp); | |
60a3b225 | 780 | bpf_prog_lock_ro(fp); |
04fd61ab | 781 | |
3324b584 DB |
782 | /* The tail call compatibility check can only be done at |
783 | * this late stage as we need to determine, if we deal | |
784 | * with JITed or non JITed program concatenations and not | |
785 | * all eBPF JITs might immediately support all features. | |
786 | */ | |
d1c55ab5 DB |
787 | *err = bpf_check_tail_call(fp); |
788 | ||
789 | return fp; | |
f5bffecd | 790 | } |
7ae457c1 | 791 | EXPORT_SYMBOL_GPL(bpf_prog_select_runtime); |
f5bffecd | 792 | |
60a3b225 DB |
793 | static void bpf_prog_free_deferred(struct work_struct *work) |
794 | { | |
09756af4 | 795 | struct bpf_prog_aux *aux; |
60a3b225 | 796 | |
09756af4 AS |
797 | aux = container_of(work, struct bpf_prog_aux, work); |
798 | bpf_jit_free(aux->prog); | |
60a3b225 DB |
799 | } |
800 | ||
801 | /* Free internal BPF program */ | |
7ae457c1 | 802 | void bpf_prog_free(struct bpf_prog *fp) |
f5bffecd | 803 | { |
09756af4 | 804 | struct bpf_prog_aux *aux = fp->aux; |
60a3b225 | 805 | |
09756af4 | 806 | INIT_WORK(&aux->work, bpf_prog_free_deferred); |
09756af4 | 807 | schedule_work(&aux->work); |
f5bffecd | 808 | } |
7ae457c1 | 809 | EXPORT_SYMBOL_GPL(bpf_prog_free); |
f89b7755 | 810 | |
3ad00405 DB |
811 | /* RNG for unpriviledged user space with separated state from prandom_u32(). */ |
812 | static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state); | |
813 | ||
814 | void bpf_user_rnd_init_once(void) | |
815 | { | |
816 | prandom_init_once(&bpf_user_rnd_state); | |
817 | } | |
818 | ||
819 | u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
820 | { | |
821 | /* Should someone ever have the rather unwise idea to use some | |
822 | * of the registers passed into this function, then note that | |
823 | * this function is called from native eBPF and classic-to-eBPF | |
824 | * transformations. Register assignments from both sides are | |
825 | * different, f.e. classic always sets fn(ctx, A, X) here. | |
826 | */ | |
827 | struct rnd_state *state; | |
828 | u32 res; | |
829 | ||
830 | state = &get_cpu_var(bpf_user_rnd_state); | |
831 | res = prandom_u32_state(state); | |
832 | put_cpu_var(state); | |
833 | ||
834 | return res; | |
835 | } | |
836 | ||
3ba67dab DB |
837 | /* Weak definitions of helper functions in case we don't have bpf syscall. */ |
838 | const struct bpf_func_proto bpf_map_lookup_elem_proto __weak; | |
839 | const struct bpf_func_proto bpf_map_update_elem_proto __weak; | |
840 | const struct bpf_func_proto bpf_map_delete_elem_proto __weak; | |
841 | ||
03e69b50 | 842 | const struct bpf_func_proto bpf_get_prandom_u32_proto __weak; |
c04167ce | 843 | const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak; |
17ca8cbf | 844 | const struct bpf_func_proto bpf_ktime_get_ns_proto __weak; |
bd570ff9 | 845 | |
ffeedafb AS |
846 | const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak; |
847 | const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak; | |
848 | const struct bpf_func_proto bpf_get_current_comm_proto __weak; | |
bd570ff9 | 849 | |
0756ea3e AS |
850 | const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void) |
851 | { | |
852 | return NULL; | |
853 | } | |
03e69b50 | 854 | |
bd570ff9 DB |
855 | const struct bpf_func_proto * __weak bpf_get_event_output_proto(void) |
856 | { | |
857 | return NULL; | |
858 | } | |
859 | ||
3324b584 DB |
860 | /* Always built-in helper functions. */ |
861 | const struct bpf_func_proto bpf_tail_call_proto = { | |
862 | .func = NULL, | |
863 | .gpl_only = false, | |
864 | .ret_type = RET_VOID, | |
865 | .arg1_type = ARG_PTR_TO_CTX, | |
866 | .arg2_type = ARG_CONST_MAP_PTR, | |
867 | .arg3_type = ARG_ANYTHING, | |
868 | }; | |
869 | ||
870 | /* For classic BPF JITs that don't implement bpf_int_jit_compile(). */ | |
d1c55ab5 | 871 | struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog) |
3324b584 | 872 | { |
d1c55ab5 | 873 | return prog; |
3324b584 DB |
874 | } |
875 | ||
969bf05e AS |
876 | bool __weak bpf_helper_changes_skb_data(void *func) |
877 | { | |
878 | return false; | |
879 | } | |
880 | ||
f89b7755 AS |
881 | /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call |
882 | * skb_copy_bits(), so provide a weak definition of it for NET-less config. | |
883 | */ | |
884 | int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to, | |
885 | int len) | |
886 | { | |
887 | return -EFAULT; | |
888 | } |