blob: 70f0821aca47e2503fd819ec1a5cd5dd6ec23d77 [file] [log] [blame]
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001/*
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.
Alexei Starovoitov4df95ff2014-07-30 20:34:14 -070021 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -070022 */
Daniel Borkmann738cbe72014-09-08 08:04:47 +020023
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -070024#include <linux/filter.h>
25#include <linux/skbuff.h>
Daniel Borkmann60a3b222014-09-02 22:53:44 +020026#include <linux/vmalloc.h>
Daniel Borkmann738cbe72014-09-08 08:04:47 +020027#include <linux/random.h>
28#include <linux/moduleloader.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070029#include <linux/bpf.h>
Josh Poimboeuf39853cc2016-02-28 22:22:37 -060030#include <linux/frame.h>
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -070031
Daniel Borkmann3324b582015-05-29 23:23:07 +020032#include <asm/unaligned.h>
33
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -070034/* 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 */
59void *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;
Daniel Borkmann3324b582015-05-29 23:23:07 +020067
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -070068 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
69 return ptr;
70
71 return NULL;
72}
73
Daniel Borkmann60a3b222014-09-02 22:53:44 +020074struct 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;
Alexei Starovoitov09756af2014-09-26 00:17:00 -070078 struct bpf_prog_aux *aux;
Daniel Borkmann60a3b222014-09-02 22:53:44 +020079 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
Daniel Borkmanna91263d2015-09-30 01:41:50 +020086 kmemcheck_annotate_bitfield(fp, meta);
87
Alexei Starovoitov09756af2014-09-26 00:17:00 -070088 aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);
89 if (aux == NULL) {
Daniel Borkmann60a3b222014-09-02 22:53:44 +020090 vfree(fp);
91 return NULL;
92 }
93
94 fp->pages = size / PAGE_SIZE;
Alexei Starovoitov09756af2014-09-26 00:17:00 -070095 fp->aux = aux;
Daniel Borkmanne9d8afa2015-10-29 14:58:08 +010096 fp->aux->prog = fp;
Daniel Borkmann60a3b222014-09-02 22:53:44 +020097
98 return fp;
99}
100EXPORT_SYMBOL_GPL(bpf_prog_alloc);
101
102struct 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) {
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200117 kmemcheck_annotate_bitfield(fp, meta);
118
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200119 memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
120 fp->pages = size / PAGE_SIZE;
Daniel Borkmanne9d8afa2015-10-29 14:58:08 +0100121 fp->aux->prog = fp;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200122
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700123 /* We keep fp->aux from fp_old around in the new
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200124 * reallocated structure.
125 */
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700126 fp_old->aux = NULL;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200127 __bpf_prog_free(fp_old);
128 }
129
130 return fp;
131}
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200132
133void __bpf_prog_free(struct bpf_prog *fp)
134{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700135 kfree(fp->aux);
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200136 vfree(fp);
137}
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200138
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200139static 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
149static 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
166struct 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
Daniel Borkmannb954d832014-09-10 15:01:02 +0200210#ifdef CONFIG_BPF_JIT
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200211struct bpf_binary_header *
212bpf_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
242void bpf_jit_binary_free(struct bpf_binary_header *hdr)
243{
Rusty Russellbe1f2212015-01-20 09:07:05 +1030244 module_memfree(hdr);
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200245}
Daniel Borkmannb954d832014-09-10 15:01:02 +0200246#endif /* CONFIG_BPF_JIT */
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200247
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700248/* 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 */
252noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
253{
254 return 0;
255}
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -0700256EXPORT_SYMBOL_GPL(__bpf_call_base);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700257
258/**
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700259 * __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
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700262 *
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700263 * Decode and execute eBPF instructions.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700264 */
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700265static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700266{
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,
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700326 [BPF_JMP | BPF_CALL | BPF_X] = &&JMP_TAIL_CALL,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700327 /* 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,
Alexei Starovoitov02ab6952014-09-04 22:17:17 -0700367 [BPF_LD | BPF_IMM | BPF_DW] = &&LD_IMM_DW,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700368 };
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700369 u32 tail_call_cnt = 0;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700370 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
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700379select_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;
Alexei Starovoitov02ab6952014-09-04 22:17:17 -0700424 LD_IMM_DW:
425 DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
426 insn++;
427 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700428 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;
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -0700437 div64_u64_rem(DST, SRC, &tmp);
438 DST = tmp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700439 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:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -0700447 div64_u64_rem(DST, IMM, &tmp);
448 DST = tmp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700449 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;
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -0700457 DST = div64_u64(DST, SRC);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700458 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:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -0700467 DST = div64_u64(DST, IMM);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700468 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
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700511 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
Wang Nan2a36f0b2015-08-06 07:02:33 +0000525 prog = READ_ONCE(array->ptrs[index]);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700526 if (unlikely(!prog))
527 goto out;
528
Daniel Borkmannc4675f92015-07-13 20:49:32 +0200529 /* 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 */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700534 insn = prog->insnsi;
535 goto select_insn;
536out:
537 CONT;
538 }
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700539 /* 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;
657load_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]
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700661 * == BPF_R6, bpf_convert_filter() saves it in BPF_R6,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700662 * 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;
689load_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;
699load_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}
Josh Poimboeuf39853cc2016-02-28 22:22:37 -0600722STACK_FRAME_NON_STANDARD(__bpf_prog_run); /* jump table */
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700723
Daniel Borkmann3324b582015-05-29 23:23:07 +0200724bool bpf_prog_array_compatible(struct bpf_array *array,
725 const struct bpf_prog *fp)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700726{
Daniel Borkmann3324b582015-05-29 23:23:07 +0200727 if (!array->owner_prog_type) {
728 /* There's no owner yet where we could check for
729 * compatibility.
730 */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700731 array->owner_prog_type = fp->type;
732 array->owner_jited = fp->jited;
Daniel Borkmann3324b582015-05-29 23:23:07 +0200733
734 return true;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700735 }
Daniel Borkmann3324b582015-05-29 23:23:07 +0200736
737 return array->owner_prog_type == fp->type &&
738 array->owner_jited == fp->jited;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700739}
740
Daniel Borkmann3324b582015-05-29 23:23:07 +0200741static int bpf_check_tail_call(const struct bpf_prog *fp)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700742{
743 struct bpf_prog_aux *aux = fp->aux;
744 int i;
745
746 for (i = 0; i < aux->used_map_cnt; i++) {
Daniel Borkmann3324b582015-05-29 23:23:07 +0200747 struct bpf_map *map = aux->used_maps[i];
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700748 struct bpf_array *array;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700749
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700750 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
751 continue;
Daniel Borkmann3324b582015-05-29 23:23:07 +0200752
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700753 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
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700761/**
Daniel Borkmann3324b582015-05-29 23:23:07 +0200762 * bpf_prog_select_runtime - select exec runtime for BPF program
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700763 * @fp: bpf_prog populated with internal BPF program
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +0200764 * @err: pointer to error variable
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700765 *
Daniel Borkmann3324b582015-05-29 23:23:07 +0200766 * Try to JIT eBPF program, if JIT is not available, use interpreter.
767 * The BPF program will be executed via BPF_PROG_RUN() macro.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700768 */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +0200769struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700770{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700771 fp->bpf_func = (void *) __bpf_prog_run;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700772
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +0200773 /* 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);
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200780 bpf_prog_lock_ro(fp);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700781
Daniel Borkmann3324b582015-05-29 23:23:07 +0200782 /* 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 */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +0200787 *err = bpf_check_tail_call(fp);
788
789 return fp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700790}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700791EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700792
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200793static void bpf_prog_free_deferred(struct work_struct *work)
794{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700795 struct bpf_prog_aux *aux;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200796
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700797 aux = container_of(work, struct bpf_prog_aux, work);
798 bpf_jit_free(aux->prog);
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200799}
800
801/* Free internal BPF program */
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700802void bpf_prog_free(struct bpf_prog *fp)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700803{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700804 struct bpf_prog_aux *aux = fp->aux;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200805
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700806 INIT_WORK(&aux->work, bpf_prog_free_deferred);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700807 schedule_work(&aux->work);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700808}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700809EXPORT_SYMBOL_GPL(bpf_prog_free);
Alexei Starovoitovf89b7752014-10-23 18:41:08 -0700810
Daniel Borkmann3ad00402015-10-08 01:20:39 +0200811/* RNG for unpriviledged user space with separated state from prandom_u32(). */
812static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
813
814void bpf_user_rnd_init_once(void)
815{
816 prandom_init_once(&bpf_user_rnd_state);
817}
818
819u64 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
Daniel Borkmann3ba67da2015-03-05 23:27:51 +0100837/* Weak definitions of helper functions in case we don't have bpf syscall. */
838const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
839const struct bpf_func_proto bpf_map_update_elem_proto __weak;
840const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
841
Daniel Borkmann03e69b52015-03-14 02:27:16 +0100842const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
Daniel Borkmannc04167c2015-03-14 02:27:17 +0100843const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +0200844const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200845
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700846const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
847const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
848const struct bpf_func_proto bpf_get_current_comm_proto __weak;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200849
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700850const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
851{
852 return NULL;
853}
Daniel Borkmann03e69b52015-03-14 02:27:16 +0100854
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200855const struct bpf_func_proto * __weak bpf_get_event_output_proto(void)
856{
857 return NULL;
858}
859
Daniel Borkmann3324b582015-05-29 23:23:07 +0200860/* Always built-in helper functions. */
861const 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(). */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +0200871struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
Daniel Borkmann3324b582015-05-29 23:23:07 +0200872{
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +0200873 return prog;
Daniel Borkmann3324b582015-05-29 23:23:07 +0200874}
875
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700876bool __weak bpf_helper_changes_skb_data(void *func)
877{
878 return false;
879}
880
Alexei Starovoitovf89b7752014-10-23 18:41:08 -0700881/* 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 */
884int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
885 int len)
886{
887 return -EFAULT;
888}