blob: dc12c4fd006e41617188acf70c855637b5025eac [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>
Daniel Borkmann74451e662017-02-16 22:24:50 +010031#include <linux/rbtree_latch.h>
32#include <linux/kallsyms.h>
33#include <linux/rcupdate.h>
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -070034
Daniel Borkmann3324b582015-05-29 23:23:07 +020035#include <asm/unaligned.h>
36
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -070037/* Registers */
38#define BPF_R0 regs[BPF_REG_0]
39#define BPF_R1 regs[BPF_REG_1]
40#define BPF_R2 regs[BPF_REG_2]
41#define BPF_R3 regs[BPF_REG_3]
42#define BPF_R4 regs[BPF_REG_4]
43#define BPF_R5 regs[BPF_REG_5]
44#define BPF_R6 regs[BPF_REG_6]
45#define BPF_R7 regs[BPF_REG_7]
46#define BPF_R8 regs[BPF_REG_8]
47#define BPF_R9 regs[BPF_REG_9]
48#define BPF_R10 regs[BPF_REG_10]
49
50/* Named registers */
51#define DST regs[insn->dst_reg]
52#define SRC regs[insn->src_reg]
53#define FP regs[BPF_REG_FP]
54#define ARG1 regs[BPF_REG_ARG1]
55#define CTX regs[BPF_REG_CTX]
56#define IMM insn->imm
57
58/* No hurry in this branch
59 *
60 * Exported for the bpf jit load helper.
61 */
62void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
63{
64 u8 *ptr = NULL;
65
66 if (k >= SKF_NET_OFF)
67 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
68 else if (k >= SKF_LL_OFF)
69 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
Daniel Borkmann3324b582015-05-29 23:23:07 +020070
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -070071 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
72 return ptr;
73
74 return NULL;
75}
76
Daniel Borkmann60a3b222014-09-02 22:53:44 +020077struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
78{
Michal Hocko19809c22017-05-08 15:57:44 -070079 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
Alexei Starovoitov09756af2014-09-26 00:17:00 -070080 struct bpf_prog_aux *aux;
Daniel Borkmann60a3b222014-09-02 22:53:44 +020081 struct bpf_prog *fp;
82
83 size = round_up(size, PAGE_SIZE);
84 fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
85 if (fp == NULL)
86 return NULL;
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
Daniel Borkmann74451e662017-02-16 22:24:50 +010098 INIT_LIST_HEAD_RCU(&fp->aux->ksym_lnode);
99
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200100 return fp;
101}
102EXPORT_SYMBOL_GPL(bpf_prog_alloc);
103
104struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
105 gfp_t gfp_extra_flags)
106{
Michal Hocko19809c22017-05-08 15:57:44 -0700107 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200108 struct bpf_prog *fp;
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100109 u32 pages, delta;
110 int ret;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200111
112 BUG_ON(fp_old == NULL);
113
114 size = round_up(size, PAGE_SIZE);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100115 pages = size / PAGE_SIZE;
116 if (pages <= fp_old->pages)
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200117 return fp_old;
118
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100119 delta = pages - fp_old->pages;
120 ret = __bpf_prog_charge(fp_old->aux->user, delta);
121 if (ret)
122 return NULL;
123
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200124 fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100125 if (fp == NULL) {
126 __bpf_prog_uncharge(fp_old->aux->user, delta);
127 } else {
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200128 memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100129 fp->pages = pages;
Daniel Borkmanne9d8afa2015-10-29 14:58:08 +0100130 fp->aux->prog = fp;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200131
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700132 /* We keep fp->aux from fp_old around in the new
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200133 * reallocated structure.
134 */
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700135 fp_old->aux = NULL;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200136 __bpf_prog_free(fp_old);
137 }
138
139 return fp;
140}
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200141
142void __bpf_prog_free(struct bpf_prog *fp)
143{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700144 kfree(fp->aux);
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200145 vfree(fp);
146}
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200147
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100148int bpf_prog_calc_tag(struct bpf_prog *fp)
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100149{
150 const u32 bits_offset = SHA_MESSAGE_BYTES - sizeof(__be64);
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100151 u32 raw_size = bpf_prog_tag_scratch_size(fp);
152 u32 digest[SHA_DIGEST_WORDS];
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100153 u32 ws[SHA_WORKSPACE_WORDS];
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100154 u32 i, bsize, psize, blocks;
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100155 struct bpf_insn *dst;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100156 bool was_ld_map;
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100157 u8 *raw, *todo;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100158 __be32 *result;
159 __be64 *bits;
160
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100161 raw = vmalloc(raw_size);
162 if (!raw)
163 return -ENOMEM;
164
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100165 sha_init(digest);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100166 memset(ws, 0, sizeof(ws));
167
168 /* We need to take out the map fd for the digest calculation
169 * since they are unstable from user space side.
170 */
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100171 dst = (void *)raw;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100172 for (i = 0, was_ld_map = false; i < fp->len; i++) {
173 dst[i] = fp->insnsi[i];
174 if (!was_ld_map &&
175 dst[i].code == (BPF_LD | BPF_IMM | BPF_DW) &&
176 dst[i].src_reg == BPF_PSEUDO_MAP_FD) {
177 was_ld_map = true;
178 dst[i].imm = 0;
179 } else if (was_ld_map &&
180 dst[i].code == 0 &&
181 dst[i].dst_reg == 0 &&
182 dst[i].src_reg == 0 &&
183 dst[i].off == 0) {
184 was_ld_map = false;
185 dst[i].imm = 0;
186 } else {
187 was_ld_map = false;
188 }
189 }
190
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100191 psize = bpf_prog_insn_size(fp);
192 memset(&raw[psize], 0, raw_size - psize);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100193 raw[psize++] = 0x80;
194
195 bsize = round_up(psize, SHA_MESSAGE_BYTES);
196 blocks = bsize / SHA_MESSAGE_BYTES;
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100197 todo = raw;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100198 if (bsize - psize >= sizeof(__be64)) {
199 bits = (__be64 *)(todo + bsize - sizeof(__be64));
200 } else {
201 bits = (__be64 *)(todo + bsize + bits_offset);
202 blocks++;
203 }
204 *bits = cpu_to_be64((psize - 1) << 3);
205
206 while (blocks--) {
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100207 sha_transform(digest, todo, ws);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100208 todo += SHA_MESSAGE_BYTES;
209 }
210
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100211 result = (__force __be32 *)digest;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100212 for (i = 0; i < SHA_DIGEST_WORDS; i++)
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100213 result[i] = cpu_to_be32(digest[i]);
214 memcpy(fp->tag, result, sizeof(fp->tag));
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100215
216 vfree(raw);
217 return 0;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100218}
219
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200220static void bpf_adj_branches(struct bpf_prog *prog, u32 pos, u32 delta)
221{
222 struct bpf_insn *insn = prog->insnsi;
223 u32 i, insn_cnt = prog->len;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800224 bool pseudo_call;
225 u8 code;
226 int off;
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200227
228 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800229 code = insn->code;
230 if (BPF_CLASS(code) != BPF_JMP)
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200231 continue;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800232 if (BPF_OP(code) == BPF_EXIT)
233 continue;
234 if (BPF_OP(code) == BPF_CALL) {
235 if (insn->src_reg == BPF_PSEUDO_CALL)
236 pseudo_call = true;
237 else
238 continue;
239 } else {
240 pseudo_call = false;
241 }
242 off = pseudo_call ? insn->imm : insn->off;
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200243
244 /* Adjust offset of jmps if we cross boundaries. */
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800245 if (i < pos && i + off + 1 > pos)
246 off += delta;
247 else if (i > pos + delta && i + off + 1 <= pos + delta)
248 off -= delta;
249
250 if (pseudo_call)
251 insn->imm = off;
252 else
253 insn->off = off;
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200254 }
255}
256
257struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
258 const struct bpf_insn *patch, u32 len)
259{
260 u32 insn_adj_cnt, insn_rest, insn_delta = len - 1;
261 struct bpf_prog *prog_adj;
262
263 /* Since our patchlet doesn't expand the image, we're done. */
264 if (insn_delta == 0) {
265 memcpy(prog->insnsi + off, patch, sizeof(*patch));
266 return prog;
267 }
268
269 insn_adj_cnt = prog->len + insn_delta;
270
271 /* Several new instructions need to be inserted. Make room
272 * for them. Likely, there's no need for a new allocation as
273 * last page could have large enough tailroom.
274 */
275 prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),
276 GFP_USER);
277 if (!prog_adj)
278 return NULL;
279
280 prog_adj->len = insn_adj_cnt;
281
282 /* Patching happens in 3 steps:
283 *
284 * 1) Move over tail of insnsi from next instruction onwards,
285 * so we can patch the single target insn with one or more
286 * new ones (patching is always from 1 to n insns, n > 0).
287 * 2) Inject new instructions at the target location.
288 * 3) Adjust branch offsets if necessary.
289 */
290 insn_rest = insn_adj_cnt - off - len;
291
292 memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1,
293 sizeof(*patch) * insn_rest);
294 memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len);
295
296 bpf_adj_branches(prog_adj, off, insn_delta);
297
298 return prog_adj;
299}
300
Daniel Borkmannb954d832014-09-10 15:01:02 +0200301#ifdef CONFIG_BPF_JIT
Daniel Borkmann74451e662017-02-16 22:24:50 +0100302static __always_inline void
303bpf_get_prog_addr_region(const struct bpf_prog *prog,
304 unsigned long *symbol_start,
305 unsigned long *symbol_end)
306{
307 const struct bpf_binary_header *hdr = bpf_jit_binary_hdr(prog);
308 unsigned long addr = (unsigned long)hdr;
309
310 WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog));
311
312 *symbol_start = addr;
313 *symbol_end = addr + hdr->pages * PAGE_SIZE;
314}
315
316static void bpf_get_prog_name(const struct bpf_prog *prog, char *sym)
317{
Martin KaFai Lau368211f2017-10-05 21:52:13 -0700318 const char *end = sym + KSYM_NAME_LEN;
319
Daniel Borkmann74451e662017-02-16 22:24:50 +0100320 BUILD_BUG_ON(sizeof("bpf_prog_") +
Martin KaFai Lau368211f2017-10-05 21:52:13 -0700321 sizeof(prog->tag) * 2 +
322 /* name has been null terminated.
323 * We should need +1 for the '_' preceding
324 * the name. However, the null character
325 * is double counted between the name and the
326 * sizeof("bpf_prog_") above, so we omit
327 * the +1 here.
328 */
329 sizeof(prog->aux->name) > KSYM_NAME_LEN);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100330
331 sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_");
332 sym = bin2hex(sym, prog->tag, sizeof(prog->tag));
Martin KaFai Lau368211f2017-10-05 21:52:13 -0700333 if (prog->aux->name[0])
334 snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name);
335 else
336 *sym = 0;
Daniel Borkmann74451e662017-02-16 22:24:50 +0100337}
338
339static __always_inline unsigned long
340bpf_get_prog_addr_start(struct latch_tree_node *n)
341{
342 unsigned long symbol_start, symbol_end;
343 const struct bpf_prog_aux *aux;
344
345 aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
346 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
347
348 return symbol_start;
349}
350
351static __always_inline bool bpf_tree_less(struct latch_tree_node *a,
352 struct latch_tree_node *b)
353{
354 return bpf_get_prog_addr_start(a) < bpf_get_prog_addr_start(b);
355}
356
357static __always_inline int bpf_tree_comp(void *key, struct latch_tree_node *n)
358{
359 unsigned long val = (unsigned long)key;
360 unsigned long symbol_start, symbol_end;
361 const struct bpf_prog_aux *aux;
362
363 aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
364 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
365
366 if (val < symbol_start)
367 return -1;
368 if (val >= symbol_end)
369 return 1;
370
371 return 0;
372}
373
374static const struct latch_tree_ops bpf_tree_ops = {
375 .less = bpf_tree_less,
376 .comp = bpf_tree_comp,
377};
378
379static DEFINE_SPINLOCK(bpf_lock);
380static LIST_HEAD(bpf_kallsyms);
381static struct latch_tree_root bpf_tree __cacheline_aligned;
382
383int bpf_jit_kallsyms __read_mostly;
384
385static void bpf_prog_ksym_node_add(struct bpf_prog_aux *aux)
386{
387 WARN_ON_ONCE(!list_empty(&aux->ksym_lnode));
388 list_add_tail_rcu(&aux->ksym_lnode, &bpf_kallsyms);
389 latch_tree_insert(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
390}
391
392static void bpf_prog_ksym_node_del(struct bpf_prog_aux *aux)
393{
394 if (list_empty(&aux->ksym_lnode))
395 return;
396
397 latch_tree_erase(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
398 list_del_rcu(&aux->ksym_lnode);
399}
400
401static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp)
402{
403 return fp->jited && !bpf_prog_was_classic(fp);
404}
405
406static bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp)
407{
408 return list_empty(&fp->aux->ksym_lnode) ||
409 fp->aux->ksym_lnode.prev == LIST_POISON2;
410}
411
412void bpf_prog_kallsyms_add(struct bpf_prog *fp)
413{
Daniel Borkmann74451e662017-02-16 22:24:50 +0100414 if (!bpf_prog_kallsyms_candidate(fp) ||
415 !capable(CAP_SYS_ADMIN))
416 return;
417
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200418 spin_lock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100419 bpf_prog_ksym_node_add(fp->aux);
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200420 spin_unlock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100421}
422
423void bpf_prog_kallsyms_del(struct bpf_prog *fp)
424{
Daniel Borkmann74451e662017-02-16 22:24:50 +0100425 if (!bpf_prog_kallsyms_candidate(fp))
426 return;
427
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200428 spin_lock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100429 bpf_prog_ksym_node_del(fp->aux);
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200430 spin_unlock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100431}
432
433static struct bpf_prog *bpf_prog_kallsyms_find(unsigned long addr)
434{
435 struct latch_tree_node *n;
436
437 if (!bpf_jit_kallsyms_enabled())
438 return NULL;
439
440 n = latch_tree_find((void *)addr, &bpf_tree, &bpf_tree_ops);
441 return n ?
442 container_of(n, struct bpf_prog_aux, ksym_tnode)->prog :
443 NULL;
444}
445
446const char *__bpf_address_lookup(unsigned long addr, unsigned long *size,
447 unsigned long *off, char *sym)
448{
449 unsigned long symbol_start, symbol_end;
450 struct bpf_prog *prog;
451 char *ret = NULL;
452
453 rcu_read_lock();
454 prog = bpf_prog_kallsyms_find(addr);
455 if (prog) {
456 bpf_get_prog_addr_region(prog, &symbol_start, &symbol_end);
457 bpf_get_prog_name(prog, sym);
458
459 ret = sym;
460 if (size)
461 *size = symbol_end - symbol_start;
462 if (off)
463 *off = addr - symbol_start;
464 }
465 rcu_read_unlock();
466
467 return ret;
468}
469
470bool is_bpf_text_address(unsigned long addr)
471{
472 bool ret;
473
474 rcu_read_lock();
475 ret = bpf_prog_kallsyms_find(addr) != NULL;
476 rcu_read_unlock();
477
478 return ret;
479}
480
481int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
482 char *sym)
483{
484 unsigned long symbol_start, symbol_end;
485 struct bpf_prog_aux *aux;
486 unsigned int it = 0;
487 int ret = -ERANGE;
488
489 if (!bpf_jit_kallsyms_enabled())
490 return ret;
491
492 rcu_read_lock();
493 list_for_each_entry_rcu(aux, &bpf_kallsyms, ksym_lnode) {
494 if (it++ != symnum)
495 continue;
496
497 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
498 bpf_get_prog_name(aux->prog, sym);
499
500 *value = symbol_start;
501 *type = BPF_SYM_ELF_TYPE;
502
503 ret = 0;
504 break;
505 }
506 rcu_read_unlock();
507
508 return ret;
509}
510
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200511struct bpf_binary_header *
512bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
513 unsigned int alignment,
514 bpf_jit_fill_hole_t bpf_fill_ill_insns)
515{
516 struct bpf_binary_header *hdr;
517 unsigned int size, hole, start;
518
519 /* Most of BPF filters are really small, but if some of them
520 * fill a page, allow at least 128 extra bytes to insert a
521 * random section of illegal instructions.
522 */
523 size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
524 hdr = module_alloc(size);
525 if (hdr == NULL)
526 return NULL;
527
528 /* Fill space with illegal/arch-dep instructions. */
529 bpf_fill_ill_insns(hdr, size);
530
531 hdr->pages = size / PAGE_SIZE;
532 hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
533 PAGE_SIZE - sizeof(*hdr));
Daniel Borkmannb7552e1b2016-05-18 14:14:28 +0200534 start = (get_random_int() % hole) & ~(alignment - 1);
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200535
536 /* Leave a random number of instructions before BPF code. */
537 *image_ptr = &hdr->image[start];
538
539 return hdr;
540}
541
542void bpf_jit_binary_free(struct bpf_binary_header *hdr)
543{
Rusty Russellbe1f2212015-01-20 09:07:05 +1030544 module_memfree(hdr);
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200545}
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200546
Daniel Borkmann74451e662017-02-16 22:24:50 +0100547/* This symbol is only overridden by archs that have different
548 * requirements than the usual eBPF JITs, f.e. when they only
549 * implement cBPF JIT, do not set images read-only, etc.
550 */
551void __weak bpf_jit_free(struct bpf_prog *fp)
552{
553 if (fp->jited) {
554 struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp);
555
556 bpf_jit_binary_unlock_ro(hdr);
557 bpf_jit_binary_free(hdr);
558
559 WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp));
560 }
561
562 bpf_prog_unlock_free(fp);
563}
564
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200565int bpf_jit_harden __read_mostly;
566
567static int bpf_jit_blind_insn(const struct bpf_insn *from,
568 const struct bpf_insn *aux,
569 struct bpf_insn *to_buff)
570{
571 struct bpf_insn *to = to_buff;
Daniel Borkmannb7552e1b2016-05-18 14:14:28 +0200572 u32 imm_rnd = get_random_int();
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200573 s16 off;
574
575 BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG);
576 BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG);
577
578 if (from->imm == 0 &&
579 (from->code == (BPF_ALU | BPF_MOV | BPF_K) ||
580 from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) {
581 *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg);
582 goto out;
583 }
584
585 switch (from->code) {
586 case BPF_ALU | BPF_ADD | BPF_K:
587 case BPF_ALU | BPF_SUB | BPF_K:
588 case BPF_ALU | BPF_AND | BPF_K:
589 case BPF_ALU | BPF_OR | BPF_K:
590 case BPF_ALU | BPF_XOR | BPF_K:
591 case BPF_ALU | BPF_MUL | BPF_K:
592 case BPF_ALU | BPF_MOV | BPF_K:
593 case BPF_ALU | BPF_DIV | BPF_K:
594 case BPF_ALU | BPF_MOD | BPF_K:
595 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
596 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
597 *to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX);
598 break;
599
600 case BPF_ALU64 | BPF_ADD | BPF_K:
601 case BPF_ALU64 | BPF_SUB | BPF_K:
602 case BPF_ALU64 | BPF_AND | BPF_K:
603 case BPF_ALU64 | BPF_OR | BPF_K:
604 case BPF_ALU64 | BPF_XOR | BPF_K:
605 case BPF_ALU64 | BPF_MUL | BPF_K:
606 case BPF_ALU64 | BPF_MOV | BPF_K:
607 case BPF_ALU64 | BPF_DIV | BPF_K:
608 case BPF_ALU64 | BPF_MOD | BPF_K:
609 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
610 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
611 *to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX);
612 break;
613
614 case BPF_JMP | BPF_JEQ | BPF_K:
615 case BPF_JMP | BPF_JNE | BPF_K:
616 case BPF_JMP | BPF_JGT | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200617 case BPF_JMP | BPF_JLT | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200618 case BPF_JMP | BPF_JGE | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200619 case BPF_JMP | BPF_JLE | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200620 case BPF_JMP | BPF_JSGT | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200621 case BPF_JMP | BPF_JSLT | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200622 case BPF_JMP | BPF_JSGE | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200623 case BPF_JMP | BPF_JSLE | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200624 case BPF_JMP | BPF_JSET | BPF_K:
625 /* Accommodate for extra offset in case of a backjump. */
626 off = from->off;
627 if (off < 0)
628 off -= 2;
629 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
630 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
631 *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off);
632 break;
633
634 case BPF_LD | BPF_ABS | BPF_W:
635 case BPF_LD | BPF_ABS | BPF_H:
636 case BPF_LD | BPF_ABS | BPF_B:
637 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
638 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
639 *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
640 break;
641
642 case BPF_LD | BPF_IND | BPF_W:
643 case BPF_LD | BPF_IND | BPF_H:
644 case BPF_LD | BPF_IND | BPF_B:
645 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
646 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
647 *to++ = BPF_ALU32_REG(BPF_ADD, BPF_REG_AX, from->src_reg);
648 *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
649 break;
650
651 case BPF_LD | BPF_IMM | BPF_DW:
652 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm);
653 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
654 *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
655 *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX);
656 break;
657 case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */
658 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm);
659 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
660 *to++ = BPF_ALU64_REG(BPF_OR, aux[0].dst_reg, BPF_REG_AX);
661 break;
662
663 case BPF_ST | BPF_MEM | BPF_DW:
664 case BPF_ST | BPF_MEM | BPF_W:
665 case BPF_ST | BPF_MEM | BPF_H:
666 case BPF_ST | BPF_MEM | BPF_B:
667 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
668 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
669 *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off);
670 break;
671 }
672out:
673 return to - to_buff;
674}
675
676static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other,
677 gfp_t gfp_extra_flags)
678{
Michal Hocko19809c22017-05-08 15:57:44 -0700679 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200680 struct bpf_prog *fp;
681
682 fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags, PAGE_KERNEL);
683 if (fp != NULL) {
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200684 /* aux->prog still points to the fp_other one, so
685 * when promoting the clone to the real program,
686 * this still needs to be adapted.
687 */
688 memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE);
689 }
690
691 return fp;
692}
693
694static void bpf_prog_clone_free(struct bpf_prog *fp)
695{
696 /* aux was stolen by the other clone, so we cannot free
697 * it from this path! It will be freed eventually by the
698 * other program on release.
699 *
700 * At this point, we don't need a deferred release since
701 * clone is guaranteed to not be locked.
702 */
703 fp->aux = NULL;
704 __bpf_prog_free(fp);
705}
706
707void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other)
708{
709 /* We have to repoint aux->prog to self, as we don't
710 * know whether fp here is the clone or the original.
711 */
712 fp->aux->prog = fp;
713 bpf_prog_clone_free(fp_other);
714}
715
716struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
717{
718 struct bpf_insn insn_buff[16], aux[2];
719 struct bpf_prog *clone, *tmp;
720 int insn_delta, insn_cnt;
721 struct bpf_insn *insn;
722 int i, rewritten;
723
724 if (!bpf_jit_blinding_enabled())
725 return prog;
726
727 clone = bpf_prog_clone_create(prog, GFP_USER);
728 if (!clone)
729 return ERR_PTR(-ENOMEM);
730
731 insn_cnt = clone->len;
732 insn = clone->insnsi;
733
734 for (i = 0; i < insn_cnt; i++, insn++) {
735 /* We temporarily need to hold the original ld64 insn
736 * so that we can still access the first part in the
737 * second blinding run.
738 */
739 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) &&
740 insn[1].code == 0)
741 memcpy(aux, insn, sizeof(aux));
742
743 rewritten = bpf_jit_blind_insn(insn, aux, insn_buff);
744 if (!rewritten)
745 continue;
746
747 tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);
748 if (!tmp) {
749 /* Patching may have repointed aux->prog during
750 * realloc from the original one, so we need to
751 * fix it up here on error.
752 */
753 bpf_jit_prog_release_other(prog, clone);
754 return ERR_PTR(-ENOMEM);
755 }
756
757 clone = tmp;
758 insn_delta = rewritten - 1;
759
760 /* Walk new program and skip insns we just inserted. */
761 insn = clone->insnsi + i + insn_delta;
762 insn_cnt += insn_delta;
763 i += insn_delta;
764 }
765
766 return clone;
767}
Daniel Borkmannb954d832014-09-10 15:01:02 +0200768#endif /* CONFIG_BPF_JIT */
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200769
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700770/* Base function for offset calculation. Needs to go into .text section,
771 * therefore keeping it non-static as well; will also be used by JITs
772 * anyway later on, so do not let the compiler omit it.
773 */
774noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
775{
776 return 0;
777}
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -0700778EXPORT_SYMBOL_GPL(__bpf_call_base);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700779
780/**
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700781 * __bpf_prog_run - run eBPF program on a given context
782 * @ctx: is the data we are operating on
783 * @insn: is the array of eBPF instructions
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700784 *
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700785 * Decode and execute eBPF instructions.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700786 */
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800787static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700788{
Alexei Starovoitovf696b8f2017-05-30 13:31:28 -0700789 u64 tmp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700790 static const void *jumptable[256] = {
791 [0 ... 255] = &&default_label,
792 /* Now overwrite non-defaults ... */
793 /* 32 bit ALU operations */
794 [BPF_ALU | BPF_ADD | BPF_X] = &&ALU_ADD_X,
795 [BPF_ALU | BPF_ADD | BPF_K] = &&ALU_ADD_K,
796 [BPF_ALU | BPF_SUB | BPF_X] = &&ALU_SUB_X,
797 [BPF_ALU | BPF_SUB | BPF_K] = &&ALU_SUB_K,
798 [BPF_ALU | BPF_AND | BPF_X] = &&ALU_AND_X,
799 [BPF_ALU | BPF_AND | BPF_K] = &&ALU_AND_K,
800 [BPF_ALU | BPF_OR | BPF_X] = &&ALU_OR_X,
801 [BPF_ALU | BPF_OR | BPF_K] = &&ALU_OR_K,
802 [BPF_ALU | BPF_LSH | BPF_X] = &&ALU_LSH_X,
803 [BPF_ALU | BPF_LSH | BPF_K] = &&ALU_LSH_K,
804 [BPF_ALU | BPF_RSH | BPF_X] = &&ALU_RSH_X,
805 [BPF_ALU | BPF_RSH | BPF_K] = &&ALU_RSH_K,
806 [BPF_ALU | BPF_XOR | BPF_X] = &&ALU_XOR_X,
807 [BPF_ALU | BPF_XOR | BPF_K] = &&ALU_XOR_K,
808 [BPF_ALU | BPF_MUL | BPF_X] = &&ALU_MUL_X,
809 [BPF_ALU | BPF_MUL | BPF_K] = &&ALU_MUL_K,
810 [BPF_ALU | BPF_MOV | BPF_X] = &&ALU_MOV_X,
811 [BPF_ALU | BPF_MOV | BPF_K] = &&ALU_MOV_K,
812 [BPF_ALU | BPF_DIV | BPF_X] = &&ALU_DIV_X,
813 [BPF_ALU | BPF_DIV | BPF_K] = &&ALU_DIV_K,
814 [BPF_ALU | BPF_MOD | BPF_X] = &&ALU_MOD_X,
815 [BPF_ALU | BPF_MOD | BPF_K] = &&ALU_MOD_K,
816 [BPF_ALU | BPF_NEG] = &&ALU_NEG,
817 [BPF_ALU | BPF_END | BPF_TO_BE] = &&ALU_END_TO_BE,
818 [BPF_ALU | BPF_END | BPF_TO_LE] = &&ALU_END_TO_LE,
819 /* 64 bit ALU operations */
820 [BPF_ALU64 | BPF_ADD | BPF_X] = &&ALU64_ADD_X,
821 [BPF_ALU64 | BPF_ADD | BPF_K] = &&ALU64_ADD_K,
822 [BPF_ALU64 | BPF_SUB | BPF_X] = &&ALU64_SUB_X,
823 [BPF_ALU64 | BPF_SUB | BPF_K] = &&ALU64_SUB_K,
824 [BPF_ALU64 | BPF_AND | BPF_X] = &&ALU64_AND_X,
825 [BPF_ALU64 | BPF_AND | BPF_K] = &&ALU64_AND_K,
826 [BPF_ALU64 | BPF_OR | BPF_X] = &&ALU64_OR_X,
827 [BPF_ALU64 | BPF_OR | BPF_K] = &&ALU64_OR_K,
828 [BPF_ALU64 | BPF_LSH | BPF_X] = &&ALU64_LSH_X,
829 [BPF_ALU64 | BPF_LSH | BPF_K] = &&ALU64_LSH_K,
830 [BPF_ALU64 | BPF_RSH | BPF_X] = &&ALU64_RSH_X,
831 [BPF_ALU64 | BPF_RSH | BPF_K] = &&ALU64_RSH_K,
832 [BPF_ALU64 | BPF_XOR | BPF_X] = &&ALU64_XOR_X,
833 [BPF_ALU64 | BPF_XOR | BPF_K] = &&ALU64_XOR_K,
834 [BPF_ALU64 | BPF_MUL | BPF_X] = &&ALU64_MUL_X,
835 [BPF_ALU64 | BPF_MUL | BPF_K] = &&ALU64_MUL_K,
836 [BPF_ALU64 | BPF_MOV | BPF_X] = &&ALU64_MOV_X,
837 [BPF_ALU64 | BPF_MOV | BPF_K] = &&ALU64_MOV_K,
838 [BPF_ALU64 | BPF_ARSH | BPF_X] = &&ALU64_ARSH_X,
839 [BPF_ALU64 | BPF_ARSH | BPF_K] = &&ALU64_ARSH_K,
840 [BPF_ALU64 | BPF_DIV | BPF_X] = &&ALU64_DIV_X,
841 [BPF_ALU64 | BPF_DIV | BPF_K] = &&ALU64_DIV_K,
842 [BPF_ALU64 | BPF_MOD | BPF_X] = &&ALU64_MOD_X,
843 [BPF_ALU64 | BPF_MOD | BPF_K] = &&ALU64_MOD_K,
844 [BPF_ALU64 | BPF_NEG] = &&ALU64_NEG,
845 /* Call instruction */
846 [BPF_JMP | BPF_CALL] = &&JMP_CALL,
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800847 [BPF_JMP | BPF_CALL_ARGS] = &&JMP_CALL_ARGS,
Alexei Starovoitov71189fa2017-05-30 13:31:27 -0700848 [BPF_JMP | BPF_TAIL_CALL] = &&JMP_TAIL_CALL,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700849 /* Jumps */
850 [BPF_JMP | BPF_JA] = &&JMP_JA,
851 [BPF_JMP | BPF_JEQ | BPF_X] = &&JMP_JEQ_X,
852 [BPF_JMP | BPF_JEQ | BPF_K] = &&JMP_JEQ_K,
853 [BPF_JMP | BPF_JNE | BPF_X] = &&JMP_JNE_X,
854 [BPF_JMP | BPF_JNE | BPF_K] = &&JMP_JNE_K,
855 [BPF_JMP | BPF_JGT | BPF_X] = &&JMP_JGT_X,
856 [BPF_JMP | BPF_JGT | BPF_K] = &&JMP_JGT_K,
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200857 [BPF_JMP | BPF_JLT | BPF_X] = &&JMP_JLT_X,
858 [BPF_JMP | BPF_JLT | BPF_K] = &&JMP_JLT_K,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700859 [BPF_JMP | BPF_JGE | BPF_X] = &&JMP_JGE_X,
860 [BPF_JMP | BPF_JGE | BPF_K] = &&JMP_JGE_K,
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200861 [BPF_JMP | BPF_JLE | BPF_X] = &&JMP_JLE_X,
862 [BPF_JMP | BPF_JLE | BPF_K] = &&JMP_JLE_K,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700863 [BPF_JMP | BPF_JSGT | BPF_X] = &&JMP_JSGT_X,
864 [BPF_JMP | BPF_JSGT | BPF_K] = &&JMP_JSGT_K,
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200865 [BPF_JMP | BPF_JSLT | BPF_X] = &&JMP_JSLT_X,
866 [BPF_JMP | BPF_JSLT | BPF_K] = &&JMP_JSLT_K,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700867 [BPF_JMP | BPF_JSGE | BPF_X] = &&JMP_JSGE_X,
868 [BPF_JMP | BPF_JSGE | BPF_K] = &&JMP_JSGE_K,
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200869 [BPF_JMP | BPF_JSLE | BPF_X] = &&JMP_JSLE_X,
870 [BPF_JMP | BPF_JSLE | BPF_K] = &&JMP_JSLE_K,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700871 [BPF_JMP | BPF_JSET | BPF_X] = &&JMP_JSET_X,
872 [BPF_JMP | BPF_JSET | BPF_K] = &&JMP_JSET_K,
873 /* Program return */
874 [BPF_JMP | BPF_EXIT] = &&JMP_EXIT,
875 /* Store instructions */
876 [BPF_STX | BPF_MEM | BPF_B] = &&STX_MEM_B,
877 [BPF_STX | BPF_MEM | BPF_H] = &&STX_MEM_H,
878 [BPF_STX | BPF_MEM | BPF_W] = &&STX_MEM_W,
879 [BPF_STX | BPF_MEM | BPF_DW] = &&STX_MEM_DW,
880 [BPF_STX | BPF_XADD | BPF_W] = &&STX_XADD_W,
881 [BPF_STX | BPF_XADD | BPF_DW] = &&STX_XADD_DW,
882 [BPF_ST | BPF_MEM | BPF_B] = &&ST_MEM_B,
883 [BPF_ST | BPF_MEM | BPF_H] = &&ST_MEM_H,
884 [BPF_ST | BPF_MEM | BPF_W] = &&ST_MEM_W,
885 [BPF_ST | BPF_MEM | BPF_DW] = &&ST_MEM_DW,
886 /* Load instructions */
887 [BPF_LDX | BPF_MEM | BPF_B] = &&LDX_MEM_B,
888 [BPF_LDX | BPF_MEM | BPF_H] = &&LDX_MEM_H,
889 [BPF_LDX | BPF_MEM | BPF_W] = &&LDX_MEM_W,
890 [BPF_LDX | BPF_MEM | BPF_DW] = &&LDX_MEM_DW,
891 [BPF_LD | BPF_ABS | BPF_W] = &&LD_ABS_W,
892 [BPF_LD | BPF_ABS | BPF_H] = &&LD_ABS_H,
893 [BPF_LD | BPF_ABS | BPF_B] = &&LD_ABS_B,
894 [BPF_LD | BPF_IND | BPF_W] = &&LD_IND_W,
895 [BPF_LD | BPF_IND | BPF_H] = &&LD_IND_H,
896 [BPF_LD | BPF_IND | BPF_B] = &&LD_IND_B,
Alexei Starovoitov02ab6952014-09-04 22:17:17 -0700897 [BPF_LD | BPF_IMM | BPF_DW] = &&LD_IMM_DW,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700898 };
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700899 u32 tail_call_cnt = 0;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700900 void *ptr;
901 int off;
902
903#define CONT ({ insn++; goto select_insn; })
904#define CONT_JMP ({ insn++; goto select_insn; })
905
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700906select_insn:
907 goto *jumptable[insn->code];
908
909 /* ALU */
910#define ALU(OPCODE, OP) \
911 ALU64_##OPCODE##_X: \
912 DST = DST OP SRC; \
913 CONT; \
914 ALU_##OPCODE##_X: \
915 DST = (u32) DST OP (u32) SRC; \
916 CONT; \
917 ALU64_##OPCODE##_K: \
918 DST = DST OP IMM; \
919 CONT; \
920 ALU_##OPCODE##_K: \
921 DST = (u32) DST OP (u32) IMM; \
922 CONT;
923
924 ALU(ADD, +)
925 ALU(SUB, -)
926 ALU(AND, &)
927 ALU(OR, |)
928 ALU(LSH, <<)
929 ALU(RSH, >>)
930 ALU(XOR, ^)
931 ALU(MUL, *)
932#undef ALU
933 ALU_NEG:
934 DST = (u32) -DST;
935 CONT;
936 ALU64_NEG:
937 DST = -DST;
938 CONT;
939 ALU_MOV_X:
940 DST = (u32) SRC;
941 CONT;
942 ALU_MOV_K:
943 DST = (u32) IMM;
944 CONT;
945 ALU64_MOV_X:
946 DST = SRC;
947 CONT;
948 ALU64_MOV_K:
949 DST = IMM;
950 CONT;
Alexei Starovoitov02ab6952014-09-04 22:17:17 -0700951 LD_IMM_DW:
952 DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
953 insn++;
954 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700955 ALU64_ARSH_X:
956 (*(s64 *) &DST) >>= SRC;
957 CONT;
958 ALU64_ARSH_K:
959 (*(s64 *) &DST) >>= IMM;
960 CONT;
961 ALU64_MOD_X:
962 if (unlikely(SRC == 0))
963 return 0;
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -0700964 div64_u64_rem(DST, SRC, &tmp);
965 DST = tmp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700966 CONT;
967 ALU_MOD_X:
968 if (unlikely(SRC == 0))
969 return 0;
970 tmp = (u32) DST;
971 DST = do_div(tmp, (u32) SRC);
972 CONT;
973 ALU64_MOD_K:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -0700974 div64_u64_rem(DST, IMM, &tmp);
975 DST = tmp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700976 CONT;
977 ALU_MOD_K:
978 tmp = (u32) DST;
979 DST = do_div(tmp, (u32) IMM);
980 CONT;
981 ALU64_DIV_X:
982 if (unlikely(SRC == 0))
983 return 0;
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -0700984 DST = div64_u64(DST, SRC);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700985 CONT;
986 ALU_DIV_X:
987 if (unlikely(SRC == 0))
988 return 0;
989 tmp = (u32) DST;
990 do_div(tmp, (u32) SRC);
991 DST = (u32) tmp;
992 CONT;
993 ALU64_DIV_K:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -0700994 DST = div64_u64(DST, IMM);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700995 CONT;
996 ALU_DIV_K:
997 tmp = (u32) DST;
998 do_div(tmp, (u32) IMM);
999 DST = (u32) tmp;
1000 CONT;
1001 ALU_END_TO_BE:
1002 switch (IMM) {
1003 case 16:
1004 DST = (__force u16) cpu_to_be16(DST);
1005 break;
1006 case 32:
1007 DST = (__force u32) cpu_to_be32(DST);
1008 break;
1009 case 64:
1010 DST = (__force u64) cpu_to_be64(DST);
1011 break;
1012 }
1013 CONT;
1014 ALU_END_TO_LE:
1015 switch (IMM) {
1016 case 16:
1017 DST = (__force u16) cpu_to_le16(DST);
1018 break;
1019 case 32:
1020 DST = (__force u32) cpu_to_le32(DST);
1021 break;
1022 case 64:
1023 DST = (__force u64) cpu_to_le64(DST);
1024 break;
1025 }
1026 CONT;
1027
1028 /* CALL */
1029 JMP_CALL:
1030 /* Function call scratches BPF_R1-BPF_R5 registers,
1031 * preserves BPF_R6-BPF_R9, and stores return value
1032 * into BPF_R0.
1033 */
1034 BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
1035 BPF_R4, BPF_R5);
1036 CONT;
1037
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001038 JMP_CALL_ARGS:
1039 BPF_R0 = (__bpf_call_base_args + insn->imm)(BPF_R1, BPF_R2,
1040 BPF_R3, BPF_R4,
1041 BPF_R5,
1042 insn + insn->off + 1);
1043 CONT;
1044
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001045 JMP_TAIL_CALL: {
1046 struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
1047 struct bpf_array *array = container_of(map, struct bpf_array, map);
1048 struct bpf_prog *prog;
Alexei Starovoitov90caccd2017-10-03 15:37:20 -07001049 u32 index = BPF_R3;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001050
1051 if (unlikely(index >= array->map.max_entries))
1052 goto out;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001053 if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
1054 goto out;
1055
1056 tail_call_cnt++;
1057
Wang Nan2a36f0b2015-08-06 07:02:33 +00001058 prog = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +02001059 if (!prog)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001060 goto out;
1061
Daniel Borkmannc4675f92015-07-13 20:49:32 +02001062 /* ARG1 at this point is guaranteed to point to CTX from
1063 * the verifier side due to the fact that the tail call is
1064 * handeled like a helper, that is, bpf_tail_call_proto,
1065 * where arg1_type is ARG_PTR_TO_CTX.
1066 */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001067 insn = prog->insnsi;
1068 goto select_insn;
1069out:
1070 CONT;
1071 }
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001072 /* JMP */
1073 JMP_JA:
1074 insn += insn->off;
1075 CONT;
1076 JMP_JEQ_X:
1077 if (DST == SRC) {
1078 insn += insn->off;
1079 CONT_JMP;
1080 }
1081 CONT;
1082 JMP_JEQ_K:
1083 if (DST == IMM) {
1084 insn += insn->off;
1085 CONT_JMP;
1086 }
1087 CONT;
1088 JMP_JNE_X:
1089 if (DST != SRC) {
1090 insn += insn->off;
1091 CONT_JMP;
1092 }
1093 CONT;
1094 JMP_JNE_K:
1095 if (DST != IMM) {
1096 insn += insn->off;
1097 CONT_JMP;
1098 }
1099 CONT;
1100 JMP_JGT_X:
1101 if (DST > SRC) {
1102 insn += insn->off;
1103 CONT_JMP;
1104 }
1105 CONT;
1106 JMP_JGT_K:
1107 if (DST > IMM) {
1108 insn += insn->off;
1109 CONT_JMP;
1110 }
1111 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001112 JMP_JLT_X:
1113 if (DST < SRC) {
1114 insn += insn->off;
1115 CONT_JMP;
1116 }
1117 CONT;
1118 JMP_JLT_K:
1119 if (DST < IMM) {
1120 insn += insn->off;
1121 CONT_JMP;
1122 }
1123 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001124 JMP_JGE_X:
1125 if (DST >= SRC) {
1126 insn += insn->off;
1127 CONT_JMP;
1128 }
1129 CONT;
1130 JMP_JGE_K:
1131 if (DST >= IMM) {
1132 insn += insn->off;
1133 CONT_JMP;
1134 }
1135 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001136 JMP_JLE_X:
1137 if (DST <= SRC) {
1138 insn += insn->off;
1139 CONT_JMP;
1140 }
1141 CONT;
1142 JMP_JLE_K:
1143 if (DST <= IMM) {
1144 insn += insn->off;
1145 CONT_JMP;
1146 }
1147 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001148 JMP_JSGT_X:
1149 if (((s64) DST) > ((s64) SRC)) {
1150 insn += insn->off;
1151 CONT_JMP;
1152 }
1153 CONT;
1154 JMP_JSGT_K:
1155 if (((s64) DST) > ((s64) IMM)) {
1156 insn += insn->off;
1157 CONT_JMP;
1158 }
1159 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001160 JMP_JSLT_X:
1161 if (((s64) DST) < ((s64) SRC)) {
1162 insn += insn->off;
1163 CONT_JMP;
1164 }
1165 CONT;
1166 JMP_JSLT_K:
1167 if (((s64) DST) < ((s64) IMM)) {
1168 insn += insn->off;
1169 CONT_JMP;
1170 }
1171 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001172 JMP_JSGE_X:
1173 if (((s64) DST) >= ((s64) SRC)) {
1174 insn += insn->off;
1175 CONT_JMP;
1176 }
1177 CONT;
1178 JMP_JSGE_K:
1179 if (((s64) DST) >= ((s64) IMM)) {
1180 insn += insn->off;
1181 CONT_JMP;
1182 }
1183 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001184 JMP_JSLE_X:
1185 if (((s64) DST) <= ((s64) SRC)) {
1186 insn += insn->off;
1187 CONT_JMP;
1188 }
1189 CONT;
1190 JMP_JSLE_K:
1191 if (((s64) DST) <= ((s64) IMM)) {
1192 insn += insn->off;
1193 CONT_JMP;
1194 }
1195 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001196 JMP_JSET_X:
1197 if (DST & SRC) {
1198 insn += insn->off;
1199 CONT_JMP;
1200 }
1201 CONT;
1202 JMP_JSET_K:
1203 if (DST & IMM) {
1204 insn += insn->off;
1205 CONT_JMP;
1206 }
1207 CONT;
1208 JMP_EXIT:
1209 return BPF_R0;
1210
1211 /* STX and ST and LDX*/
1212#define LDST(SIZEOP, SIZE) \
1213 STX_MEM_##SIZEOP: \
1214 *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \
1215 CONT; \
1216 ST_MEM_##SIZEOP: \
1217 *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \
1218 CONT; \
1219 LDX_MEM_##SIZEOP: \
1220 DST = *(SIZE *)(unsigned long) (SRC + insn->off); \
1221 CONT;
1222
1223 LDST(B, u8)
1224 LDST(H, u16)
1225 LDST(W, u32)
1226 LDST(DW, u64)
1227#undef LDST
1228 STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
1229 atomic_add((u32) SRC, (atomic_t *)(unsigned long)
1230 (DST + insn->off));
1231 CONT;
1232 STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
1233 atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
1234 (DST + insn->off));
1235 CONT;
1236 LD_ABS_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + imm32)) */
1237 off = IMM;
1238load_word:
Johannes Berg96a94cc2017-04-11 12:10:58 +02001239 /* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are only
1240 * appearing in the programs where ctx == skb
1241 * (see may_access_skb() in the verifier). All programs
1242 * keep 'ctx' in regs[BPF_REG_CTX] == BPF_R6,
1243 * bpf_convert_filter() saves it in BPF_R6, internal BPF
1244 * verifier will check that BPF_R6 == ctx.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001245 *
1246 * BPF_ABS and BPF_IND are wrappers of function calls,
1247 * so they scratch BPF_R1-BPF_R5 registers, preserve
1248 * BPF_R6-BPF_R9, and store return value into BPF_R0.
1249 *
1250 * Implicit input:
1251 * ctx == skb == BPF_R6 == CTX
1252 *
1253 * Explicit input:
1254 * SRC == any register
1255 * IMM == 32-bit immediate
1256 *
1257 * Output:
1258 * BPF_R0 - 8/16/32-bit skb data converted to cpu endianness
1259 */
1260
1261 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 4, &tmp);
1262 if (likely(ptr != NULL)) {
1263 BPF_R0 = get_unaligned_be32(ptr);
1264 CONT;
1265 }
1266
1267 return 0;
1268 LD_ABS_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + imm32)) */
1269 off = IMM;
1270load_half:
1271 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 2, &tmp);
1272 if (likely(ptr != NULL)) {
1273 BPF_R0 = get_unaligned_be16(ptr);
1274 CONT;
1275 }
1276
1277 return 0;
1278 LD_ABS_B: /* BPF_R0 = *(u8 *) (skb->data + imm32) */
1279 off = IMM;
1280load_byte:
1281 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 1, &tmp);
1282 if (likely(ptr != NULL)) {
1283 BPF_R0 = *(u8 *)ptr;
1284 CONT;
1285 }
1286
1287 return 0;
1288 LD_IND_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + src_reg + imm32)) */
1289 off = IMM + SRC;
1290 goto load_word;
1291 LD_IND_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + src_reg + imm32)) */
1292 off = IMM + SRC;
1293 goto load_half;
1294 LD_IND_B: /* BPF_R0 = *(u8 *) (skb->data + src_reg + imm32) */
1295 off = IMM + SRC;
1296 goto load_byte;
1297
1298 default_label:
1299 /* If we ever reach this, we have a bug somewhere. */
1300 WARN_RATELIMIT(1, "unknown opcode %02x\n", insn->code);
1301 return 0;
1302}
Alexei Starovoitovf696b8f2017-05-30 13:31:28 -07001303STACK_FRAME_NON_STANDARD(___bpf_prog_run); /* jump table */
1304
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001305#define PROG_NAME(stack_size) __bpf_prog_run##stack_size
1306#define DEFINE_BPF_PROG_RUN(stack_size) \
1307static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \
1308{ \
1309 u64 stack[stack_size / sizeof(u64)]; \
1310 u64 regs[MAX_BPF_REG]; \
1311\
1312 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1313 ARG1 = (u64) (unsigned long) ctx; \
1314 return ___bpf_prog_run(regs, insn, stack); \
Alexei Starovoitovf696b8f2017-05-30 13:31:28 -07001315}
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001316
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001317#define PROG_NAME_ARGS(stack_size) __bpf_prog_run_args##stack_size
1318#define DEFINE_BPF_PROG_RUN_ARGS(stack_size) \
1319static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \
1320 const struct bpf_insn *insn) \
1321{ \
1322 u64 stack[stack_size / sizeof(u64)]; \
1323 u64 regs[MAX_BPF_REG]; \
1324\
1325 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1326 BPF_R1 = r1; \
1327 BPF_R2 = r2; \
1328 BPF_R3 = r3; \
1329 BPF_R4 = r4; \
1330 BPF_R5 = r5; \
1331 return ___bpf_prog_run(regs, insn, stack); \
1332}
1333
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001334#define EVAL1(FN, X) FN(X)
1335#define EVAL2(FN, X, Y...) FN(X) EVAL1(FN, Y)
1336#define EVAL3(FN, X, Y...) FN(X) EVAL2(FN, Y)
1337#define EVAL4(FN, X, Y...) FN(X) EVAL3(FN, Y)
1338#define EVAL5(FN, X, Y...) FN(X) EVAL4(FN, Y)
1339#define EVAL6(FN, X, Y...) FN(X) EVAL5(FN, Y)
1340
1341EVAL6(DEFINE_BPF_PROG_RUN, 32, 64, 96, 128, 160, 192);
1342EVAL6(DEFINE_BPF_PROG_RUN, 224, 256, 288, 320, 352, 384);
1343EVAL4(DEFINE_BPF_PROG_RUN, 416, 448, 480, 512);
1344
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001345EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 32, 64, 96, 128, 160, 192);
1346EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 224, 256, 288, 320, 352, 384);
1347EVAL4(DEFINE_BPF_PROG_RUN_ARGS, 416, 448, 480, 512);
1348
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001349#define PROG_NAME_LIST(stack_size) PROG_NAME(stack_size),
1350
1351static unsigned int (*interpreters[])(const void *ctx,
1352 const struct bpf_insn *insn) = {
1353EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1354EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1355EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1356};
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001357#undef PROG_NAME_LIST
1358#define PROG_NAME_LIST(stack_size) PROG_NAME_ARGS(stack_size),
1359static u64 (*interpreters_args[])(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5,
1360 const struct bpf_insn *insn) = {
1361EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1362EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1363EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1364};
1365#undef PROG_NAME_LIST
1366
1367void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth)
1368{
1369 stack_depth = max_t(u32, stack_depth, 1);
1370 insn->off = (s16) insn->imm;
1371 insn->imm = interpreters_args[(round_up(stack_depth, 32) / 32) - 1] -
1372 __bpf_call_base_args;
1373 insn->code = BPF_JMP | BPF_CALL_ARGS;
1374}
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001375
Daniel Borkmann3324b582015-05-29 23:23:07 +02001376bool bpf_prog_array_compatible(struct bpf_array *array,
1377 const struct bpf_prog *fp)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001378{
Josef Bacik9802d862017-12-11 11:36:48 -05001379 if (fp->kprobe_override)
1380 return false;
1381
Daniel Borkmann3324b582015-05-29 23:23:07 +02001382 if (!array->owner_prog_type) {
1383 /* There's no owner yet where we could check for
1384 * compatibility.
1385 */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001386 array->owner_prog_type = fp->type;
1387 array->owner_jited = fp->jited;
Daniel Borkmann3324b582015-05-29 23:23:07 +02001388
1389 return true;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001390 }
Daniel Borkmann3324b582015-05-29 23:23:07 +02001391
1392 return array->owner_prog_type == fp->type &&
1393 array->owner_jited == fp->jited;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001394}
1395
Daniel Borkmann3324b582015-05-29 23:23:07 +02001396static int bpf_check_tail_call(const struct bpf_prog *fp)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001397{
1398 struct bpf_prog_aux *aux = fp->aux;
1399 int i;
1400
1401 for (i = 0; i < aux->used_map_cnt; i++) {
Daniel Borkmann3324b582015-05-29 23:23:07 +02001402 struct bpf_map *map = aux->used_maps[i];
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001403 struct bpf_array *array;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001404
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001405 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1406 continue;
Daniel Borkmann3324b582015-05-29 23:23:07 +02001407
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001408 array = container_of(map, struct bpf_array, map);
1409 if (!bpf_prog_array_compatible(array, fp))
1410 return -EINVAL;
1411 }
1412
1413 return 0;
1414}
1415
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001416/**
Daniel Borkmann3324b582015-05-29 23:23:07 +02001417 * bpf_prog_select_runtime - select exec runtime for BPF program
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001418 * @fp: bpf_prog populated with internal BPF program
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001419 * @err: pointer to error variable
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001420 *
Daniel Borkmann3324b582015-05-29 23:23:07 +02001421 * Try to JIT eBPF program, if JIT is not available, use interpreter.
1422 * The BPF program will be executed via BPF_PROG_RUN() macro.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001423 */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001424struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001425{
Martin KaFai Lau8007e402017-06-28 10:41:24 -07001426 u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1);
1427
1428 fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1];
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001429
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001430 /* eBPF JITs can rewrite the program in case constant
1431 * blinding is active. However, in case of error during
1432 * blinding, bpf_int_jit_compile() must always return a
1433 * valid program, which in this case would simply not
1434 * be JITed, but falls back to the interpreter.
1435 */
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001436 if (!bpf_prog_is_dev_bound(fp->aux)) {
1437 fp = bpf_int_jit_compile(fp);
1438 } else {
1439 *err = bpf_prog_offload_compile(fp);
1440 if (*err)
1441 return fp;
1442 }
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001443 bpf_prog_lock_ro(fp);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001444
Daniel Borkmann3324b582015-05-29 23:23:07 +02001445 /* The tail call compatibility check can only be done at
1446 * this late stage as we need to determine, if we deal
1447 * with JITed or non JITed program concatenations and not
1448 * all eBPF JITs might immediately support all features.
1449 */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001450 *err = bpf_check_tail_call(fp);
1451
1452 return fp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001453}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001454EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001455
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001456static unsigned int __bpf_prog_ret1(const void *ctx,
1457 const struct bpf_insn *insn)
1458{
1459 return 1;
1460}
1461
1462static struct bpf_prog_dummy {
1463 struct bpf_prog prog;
1464} dummy_bpf_prog = {
1465 .prog = {
1466 .bpf_func = __bpf_prog_ret1,
1467 },
1468};
1469
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001470/* to avoid allocating empty bpf_prog_array for cgroups that
1471 * don't have bpf program attached use one global 'empty_prog_array'
1472 * It will not be modified the caller of bpf_prog_array_alloc()
1473 * (since caller requested prog_cnt == 0)
1474 * that pointer should be 'freed' by bpf_prog_array_free()
1475 */
1476static struct {
1477 struct bpf_prog_array hdr;
1478 struct bpf_prog *null_prog;
1479} empty_prog_array = {
1480 .null_prog = NULL,
1481};
1482
1483struct bpf_prog_array __rcu *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags)
1484{
1485 if (prog_cnt)
1486 return kzalloc(sizeof(struct bpf_prog_array) +
1487 sizeof(struct bpf_prog *) * (prog_cnt + 1),
1488 flags);
1489
1490 return &empty_prog_array.hdr;
1491}
1492
1493void bpf_prog_array_free(struct bpf_prog_array __rcu *progs)
1494{
1495 if (!progs ||
1496 progs == (struct bpf_prog_array __rcu *)&empty_prog_array.hdr)
1497 return;
1498 kfree_rcu(progs, rcu);
1499}
1500
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001501int bpf_prog_array_length(struct bpf_prog_array __rcu *progs)
1502{
1503 struct bpf_prog **prog;
1504 u32 cnt = 0;
1505
1506 rcu_read_lock();
1507 prog = rcu_dereference(progs)->progs;
1508 for (; *prog; prog++)
Yonghong Songc8c088b2017-11-30 13:47:54 -08001509 if (*prog != &dummy_bpf_prog.prog)
1510 cnt++;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001511 rcu_read_unlock();
1512 return cnt;
1513}
1514
1515int bpf_prog_array_copy_to_user(struct bpf_prog_array __rcu *progs,
1516 __u32 __user *prog_ids, u32 cnt)
1517{
1518 struct bpf_prog **prog;
1519 u32 i = 0, id;
1520
1521 rcu_read_lock();
1522 prog = rcu_dereference(progs)->progs;
1523 for (; *prog; prog++) {
Yonghong Songf371b302017-12-11 11:39:02 -08001524 if (*prog == &dummy_bpf_prog.prog)
1525 continue;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001526 id = (*prog)->aux->id;
1527 if (copy_to_user(prog_ids + i, &id, sizeof(id))) {
1528 rcu_read_unlock();
1529 return -EFAULT;
1530 }
1531 if (++i == cnt) {
1532 prog++;
1533 break;
1534 }
1535 }
1536 rcu_read_unlock();
1537 if (*prog)
1538 return -ENOSPC;
1539 return 0;
1540}
1541
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001542void bpf_prog_array_delete_safe(struct bpf_prog_array __rcu *progs,
1543 struct bpf_prog *old_prog)
1544{
1545 struct bpf_prog **prog = progs->progs;
1546
1547 for (; *prog; prog++)
1548 if (*prog == old_prog) {
1549 WRITE_ONCE(*prog, &dummy_bpf_prog.prog);
1550 break;
1551 }
1552}
1553
1554int bpf_prog_array_copy(struct bpf_prog_array __rcu *old_array,
1555 struct bpf_prog *exclude_prog,
1556 struct bpf_prog *include_prog,
1557 struct bpf_prog_array **new_array)
1558{
1559 int new_prog_cnt, carry_prog_cnt = 0;
1560 struct bpf_prog **existing_prog;
1561 struct bpf_prog_array *array;
1562 int new_prog_idx = 0;
1563
1564 /* Figure out how many existing progs we need to carry over to
1565 * the new array.
1566 */
1567 if (old_array) {
1568 existing_prog = old_array->progs;
1569 for (; *existing_prog; existing_prog++) {
1570 if (*existing_prog != exclude_prog &&
1571 *existing_prog != &dummy_bpf_prog.prog)
1572 carry_prog_cnt++;
1573 if (*existing_prog == include_prog)
1574 return -EEXIST;
1575 }
1576 }
1577
1578 /* How many progs (not NULL) will be in the new array? */
1579 new_prog_cnt = carry_prog_cnt;
1580 if (include_prog)
1581 new_prog_cnt += 1;
1582
1583 /* Do we have any prog (not NULL) in the new array? */
1584 if (!new_prog_cnt) {
1585 *new_array = NULL;
1586 return 0;
1587 }
1588
1589 /* +1 as the end of prog_array is marked with NULL */
1590 array = bpf_prog_array_alloc(new_prog_cnt + 1, GFP_KERNEL);
1591 if (!array)
1592 return -ENOMEM;
1593
1594 /* Fill in the new prog array */
1595 if (carry_prog_cnt) {
1596 existing_prog = old_array->progs;
1597 for (; *existing_prog; existing_prog++)
1598 if (*existing_prog != exclude_prog &&
1599 *existing_prog != &dummy_bpf_prog.prog)
1600 array->progs[new_prog_idx++] = *existing_prog;
1601 }
1602 if (include_prog)
1603 array->progs[new_prog_idx++] = include_prog;
1604 array->progs[new_prog_idx] = NULL;
1605 *new_array = array;
1606 return 0;
1607}
1608
Yonghong Songf371b302017-12-11 11:39:02 -08001609int bpf_prog_array_copy_info(struct bpf_prog_array __rcu *array,
1610 __u32 __user *prog_ids, u32 request_cnt,
1611 __u32 __user *prog_cnt)
1612{
1613 u32 cnt = 0;
1614
1615 if (array)
1616 cnt = bpf_prog_array_length(array);
1617
1618 if (copy_to_user(prog_cnt, &cnt, sizeof(cnt)))
1619 return -EFAULT;
1620
1621 /* return early if user requested only program count or nothing to copy */
1622 if (!request_cnt || !cnt)
1623 return 0;
1624
1625 return bpf_prog_array_copy_to_user(array, prog_ids, request_cnt);
1626}
1627
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001628static void bpf_prog_free_deferred(struct work_struct *work)
1629{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001630 struct bpf_prog_aux *aux;
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001631
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001632 aux = container_of(work, struct bpf_prog_aux, work);
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001633 if (bpf_prog_is_dev_bound(aux))
1634 bpf_prog_offload_destroy(aux->prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001635 bpf_jit_free(aux->prog);
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001636}
1637
1638/* Free internal BPF program */
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001639void bpf_prog_free(struct bpf_prog *fp)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001640{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001641 struct bpf_prog_aux *aux = fp->aux;
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001642
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001643 INIT_WORK(&aux->work, bpf_prog_free_deferred);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001644 schedule_work(&aux->work);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001645}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001646EXPORT_SYMBOL_GPL(bpf_prog_free);
Alexei Starovoitovf89b7752014-10-23 18:41:08 -07001647
Daniel Borkmann3ad00402015-10-08 01:20:39 +02001648/* RNG for unpriviledged user space with separated state from prandom_u32(). */
1649static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
1650
1651void bpf_user_rnd_init_once(void)
1652{
1653 prandom_init_once(&bpf_user_rnd_state);
1654}
1655
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001656BPF_CALL_0(bpf_user_rnd_u32)
Daniel Borkmann3ad00402015-10-08 01:20:39 +02001657{
1658 /* Should someone ever have the rather unwise idea to use some
1659 * of the registers passed into this function, then note that
1660 * this function is called from native eBPF and classic-to-eBPF
1661 * transformations. Register assignments from both sides are
1662 * different, f.e. classic always sets fn(ctx, A, X) here.
1663 */
1664 struct rnd_state *state;
1665 u32 res;
1666
1667 state = &get_cpu_var(bpf_user_rnd_state);
1668 res = prandom_u32_state(state);
Shaohua Lib761fe22016-09-27 08:42:41 -07001669 put_cpu_var(bpf_user_rnd_state);
Daniel Borkmann3ad00402015-10-08 01:20:39 +02001670
1671 return res;
1672}
1673
Daniel Borkmann3ba67da2015-03-05 23:27:51 +01001674/* Weak definitions of helper functions in case we don't have bpf syscall. */
1675const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
1676const struct bpf_func_proto bpf_map_update_elem_proto __weak;
1677const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
1678
Daniel Borkmann03e69b52015-03-14 02:27:16 +01001679const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
Daniel Borkmannc04167c2015-03-14 02:27:17 +01001680const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
Daniel Borkmann2d0e30c2016-10-21 12:46:33 +02001681const struct bpf_func_proto bpf_get_numa_node_id_proto __weak;
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +02001682const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001683
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -07001684const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
1685const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
1686const struct bpf_func_proto bpf_get_current_comm_proto __weak;
John Fastabend6bdc9c42017-08-16 15:02:32 -07001687const struct bpf_func_proto bpf_sock_map_update_proto __weak;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001688
Alexei Starovoitov0756ea32015-06-12 19:39:13 -07001689const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
1690{
1691 return NULL;
1692}
Daniel Borkmann03e69b52015-03-14 02:27:16 +01001693
Daniel Borkmann555c8a82016-07-14 18:08:05 +02001694u64 __weak
1695bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1696 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001697{
Daniel Borkmann555c8a82016-07-14 18:08:05 +02001698 return -ENOTSUPP;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001699}
1700
Daniel Borkmann3324b582015-05-29 23:23:07 +02001701/* Always built-in helper functions. */
1702const struct bpf_func_proto bpf_tail_call_proto = {
1703 .func = NULL,
1704 .gpl_only = false,
1705 .ret_type = RET_VOID,
1706 .arg1_type = ARG_PTR_TO_CTX,
1707 .arg2_type = ARG_CONST_MAP_PTR,
1708 .arg3_type = ARG_ANYTHING,
1709};
1710
Daniel Borkmann93831912017-02-16 22:24:49 +01001711/* Stub for JITs that only support cBPF. eBPF programs are interpreted.
1712 * It is encouraged to implement bpf_int_jit_compile() instead, so that
1713 * eBPF and implicitly also cBPF can get JITed!
1714 */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001715struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
Daniel Borkmann3324b582015-05-29 23:23:07 +02001716{
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001717 return prog;
Daniel Borkmann3324b582015-05-29 23:23:07 +02001718}
1719
Daniel Borkmann93831912017-02-16 22:24:49 +01001720/* Stub for JITs that support eBPF. All cBPF code gets transformed into
1721 * eBPF by the kernel and is later compiled by bpf_int_jit_compile().
1722 */
1723void __weak bpf_jit_compile(struct bpf_prog *prog)
1724{
1725}
1726
Martin KaFai Lau17bedab2016-12-07 15:53:11 -08001727bool __weak bpf_helper_changes_pkt_data(void *func)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001728{
1729 return false;
1730}
1731
Alexei Starovoitovf89b7752014-10-23 18:41:08 -07001732/* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
1733 * skb_copy_bits(), so provide a weak definition of it for NET-less config.
1734 */
1735int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
1736 int len)
1737{
1738 return -EFAULT;
1739}
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001740
1741/* All definitions of tracepoints related to BPF. */
1742#define CREATE_TRACE_POINTS
1743#include <linux/bpf_trace.h>
1744
1745EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception);
1746
Steven Rostedt (VMware)9185a612017-10-12 18:40:02 -04001747/* These are only used within the BPF_SYSCALL code */
1748#ifdef CONFIG_BPF_SYSCALL
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001749EXPORT_TRACEPOINT_SYMBOL_GPL(bpf_prog_get_type);
1750EXPORT_TRACEPOINT_SYMBOL_GPL(bpf_prog_put_rcu);
Steven Rostedt (VMware)9185a612017-10-12 18:40:02 -04001751#endif