blob: bda911644b1cba4fe6b05033df9593886ff89f1f [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;
Alexei Starovoitov60b58afc2017-12-14 17:55:14 -080097 fp->jit_requested = ebpf_jit_enabled();
Daniel Borkmann60a3b222014-09-02 22:53:44 +020098
Daniel Borkmann74451e662017-02-16 22:24:50 +010099 INIT_LIST_HEAD_RCU(&fp->aux->ksym_lnode);
100
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200101 return fp;
102}
103EXPORT_SYMBOL_GPL(bpf_prog_alloc);
104
105struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
106 gfp_t gfp_extra_flags)
107{
Michal Hocko19809c22017-05-08 15:57:44 -0700108 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200109 struct bpf_prog *fp;
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100110 u32 pages, delta;
111 int ret;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200112
113 BUG_ON(fp_old == NULL);
114
115 size = round_up(size, PAGE_SIZE);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100116 pages = size / PAGE_SIZE;
117 if (pages <= fp_old->pages)
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200118 return fp_old;
119
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100120 delta = pages - fp_old->pages;
121 ret = __bpf_prog_charge(fp_old->aux->user, delta);
122 if (ret)
123 return NULL;
124
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200125 fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100126 if (fp == NULL) {
127 __bpf_prog_uncharge(fp_old->aux->user, delta);
128 } else {
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200129 memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100130 fp->pages = pages;
Daniel Borkmanne9d8afa2015-10-29 14:58:08 +0100131 fp->aux->prog = fp;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200132
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700133 /* We keep fp->aux from fp_old around in the new
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200134 * reallocated structure.
135 */
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700136 fp_old->aux = NULL;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200137 __bpf_prog_free(fp_old);
138 }
139
140 return fp;
141}
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200142
143void __bpf_prog_free(struct bpf_prog *fp)
144{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700145 kfree(fp->aux);
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200146 vfree(fp);
147}
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200148
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100149int bpf_prog_calc_tag(struct bpf_prog *fp)
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100150{
151 const u32 bits_offset = SHA_MESSAGE_BYTES - sizeof(__be64);
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100152 u32 raw_size = bpf_prog_tag_scratch_size(fp);
153 u32 digest[SHA_DIGEST_WORDS];
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100154 u32 ws[SHA_WORKSPACE_WORDS];
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100155 u32 i, bsize, psize, blocks;
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100156 struct bpf_insn *dst;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100157 bool was_ld_map;
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100158 u8 *raw, *todo;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100159 __be32 *result;
160 __be64 *bits;
161
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100162 raw = vmalloc(raw_size);
163 if (!raw)
164 return -ENOMEM;
165
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100166 sha_init(digest);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100167 memset(ws, 0, sizeof(ws));
168
169 /* We need to take out the map fd for the digest calculation
170 * since they are unstable from user space side.
171 */
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100172 dst = (void *)raw;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100173 for (i = 0, was_ld_map = false; i < fp->len; i++) {
174 dst[i] = fp->insnsi[i];
175 if (!was_ld_map &&
176 dst[i].code == (BPF_LD | BPF_IMM | BPF_DW) &&
177 dst[i].src_reg == BPF_PSEUDO_MAP_FD) {
178 was_ld_map = true;
179 dst[i].imm = 0;
180 } else if (was_ld_map &&
181 dst[i].code == 0 &&
182 dst[i].dst_reg == 0 &&
183 dst[i].src_reg == 0 &&
184 dst[i].off == 0) {
185 was_ld_map = false;
186 dst[i].imm = 0;
187 } else {
188 was_ld_map = false;
189 }
190 }
191
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100192 psize = bpf_prog_insn_size(fp);
193 memset(&raw[psize], 0, raw_size - psize);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100194 raw[psize++] = 0x80;
195
196 bsize = round_up(psize, SHA_MESSAGE_BYTES);
197 blocks = bsize / SHA_MESSAGE_BYTES;
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100198 todo = raw;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100199 if (bsize - psize >= sizeof(__be64)) {
200 bits = (__be64 *)(todo + bsize - sizeof(__be64));
201 } else {
202 bits = (__be64 *)(todo + bsize + bits_offset);
203 blocks++;
204 }
205 *bits = cpu_to_be64((psize - 1) << 3);
206
207 while (blocks--) {
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100208 sha_transform(digest, todo, ws);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100209 todo += SHA_MESSAGE_BYTES;
210 }
211
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100212 result = (__force __be32 *)digest;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100213 for (i = 0; i < SHA_DIGEST_WORDS; i++)
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100214 result[i] = cpu_to_be32(digest[i]);
215 memcpy(fp->tag, result, sizeof(fp->tag));
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100216
217 vfree(raw);
218 return 0;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100219}
220
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200221static void bpf_adj_branches(struct bpf_prog *prog, u32 pos, u32 delta)
222{
223 struct bpf_insn *insn = prog->insnsi;
224 u32 i, insn_cnt = prog->len;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800225 bool pseudo_call;
226 u8 code;
227 int off;
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200228
229 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800230 code = insn->code;
231 if (BPF_CLASS(code) != BPF_JMP)
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200232 continue;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800233 if (BPF_OP(code) == BPF_EXIT)
234 continue;
235 if (BPF_OP(code) == BPF_CALL) {
236 if (insn->src_reg == BPF_PSEUDO_CALL)
237 pseudo_call = true;
238 else
239 continue;
240 } else {
241 pseudo_call = false;
242 }
243 off = pseudo_call ? insn->imm : insn->off;
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200244
245 /* Adjust offset of jmps if we cross boundaries. */
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800246 if (i < pos && i + off + 1 > pos)
247 off += delta;
248 else if (i > pos + delta && i + off + 1 <= pos + delta)
249 off -= delta;
250
251 if (pseudo_call)
252 insn->imm = off;
253 else
254 insn->off = off;
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200255 }
256}
257
258struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
259 const struct bpf_insn *patch, u32 len)
260{
261 u32 insn_adj_cnt, insn_rest, insn_delta = len - 1;
262 struct bpf_prog *prog_adj;
263
264 /* Since our patchlet doesn't expand the image, we're done. */
265 if (insn_delta == 0) {
266 memcpy(prog->insnsi + off, patch, sizeof(*patch));
267 return prog;
268 }
269
270 insn_adj_cnt = prog->len + insn_delta;
271
272 /* Several new instructions need to be inserted. Make room
273 * for them. Likely, there's no need for a new allocation as
274 * last page could have large enough tailroom.
275 */
276 prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),
277 GFP_USER);
278 if (!prog_adj)
279 return NULL;
280
281 prog_adj->len = insn_adj_cnt;
282
283 /* Patching happens in 3 steps:
284 *
285 * 1) Move over tail of insnsi from next instruction onwards,
286 * so we can patch the single target insn with one or more
287 * new ones (patching is always from 1 to n insns, n > 0).
288 * 2) Inject new instructions at the target location.
289 * 3) Adjust branch offsets if necessary.
290 */
291 insn_rest = insn_adj_cnt - off - len;
292
293 memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1,
294 sizeof(*patch) * insn_rest);
295 memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len);
296
297 bpf_adj_branches(prog_adj, off, insn_delta);
298
299 return prog_adj;
300}
301
Daniel Borkmannb954d832014-09-10 15:01:02 +0200302#ifdef CONFIG_BPF_JIT
Daniel Borkmann74451e662017-02-16 22:24:50 +0100303static __always_inline void
304bpf_get_prog_addr_region(const struct bpf_prog *prog,
305 unsigned long *symbol_start,
306 unsigned long *symbol_end)
307{
308 const struct bpf_binary_header *hdr = bpf_jit_binary_hdr(prog);
309 unsigned long addr = (unsigned long)hdr;
310
311 WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog));
312
313 *symbol_start = addr;
314 *symbol_end = addr + hdr->pages * PAGE_SIZE;
315}
316
317static void bpf_get_prog_name(const struct bpf_prog *prog, char *sym)
318{
Martin KaFai Lau368211f2017-10-05 21:52:13 -0700319 const char *end = sym + KSYM_NAME_LEN;
320
Daniel Borkmann74451e662017-02-16 22:24:50 +0100321 BUILD_BUG_ON(sizeof("bpf_prog_") +
Martin KaFai Lau368211f2017-10-05 21:52:13 -0700322 sizeof(prog->tag) * 2 +
323 /* name has been null terminated.
324 * We should need +1 for the '_' preceding
325 * the name. However, the null character
326 * is double counted between the name and the
327 * sizeof("bpf_prog_") above, so we omit
328 * the +1 here.
329 */
330 sizeof(prog->aux->name) > KSYM_NAME_LEN);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100331
332 sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_");
333 sym = bin2hex(sym, prog->tag, sizeof(prog->tag));
Martin KaFai Lau368211f2017-10-05 21:52:13 -0700334 if (prog->aux->name[0])
335 snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name);
336 else
337 *sym = 0;
Daniel Borkmann74451e662017-02-16 22:24:50 +0100338}
339
340static __always_inline unsigned long
341bpf_get_prog_addr_start(struct latch_tree_node *n)
342{
343 unsigned long symbol_start, symbol_end;
344 const struct bpf_prog_aux *aux;
345
346 aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
347 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
348
349 return symbol_start;
350}
351
352static __always_inline bool bpf_tree_less(struct latch_tree_node *a,
353 struct latch_tree_node *b)
354{
355 return bpf_get_prog_addr_start(a) < bpf_get_prog_addr_start(b);
356}
357
358static __always_inline int bpf_tree_comp(void *key, struct latch_tree_node *n)
359{
360 unsigned long val = (unsigned long)key;
361 unsigned long symbol_start, symbol_end;
362 const struct bpf_prog_aux *aux;
363
364 aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
365 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
366
367 if (val < symbol_start)
368 return -1;
369 if (val >= symbol_end)
370 return 1;
371
372 return 0;
373}
374
375static const struct latch_tree_ops bpf_tree_ops = {
376 .less = bpf_tree_less,
377 .comp = bpf_tree_comp,
378};
379
380static DEFINE_SPINLOCK(bpf_lock);
381static LIST_HEAD(bpf_kallsyms);
382static struct latch_tree_root bpf_tree __cacheline_aligned;
383
384int bpf_jit_kallsyms __read_mostly;
385
386static void bpf_prog_ksym_node_add(struct bpf_prog_aux *aux)
387{
388 WARN_ON_ONCE(!list_empty(&aux->ksym_lnode));
389 list_add_tail_rcu(&aux->ksym_lnode, &bpf_kallsyms);
390 latch_tree_insert(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
391}
392
393static void bpf_prog_ksym_node_del(struct bpf_prog_aux *aux)
394{
395 if (list_empty(&aux->ksym_lnode))
396 return;
397
398 latch_tree_erase(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
399 list_del_rcu(&aux->ksym_lnode);
400}
401
402static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp)
403{
404 return fp->jited && !bpf_prog_was_classic(fp);
405}
406
407static bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp)
408{
409 return list_empty(&fp->aux->ksym_lnode) ||
410 fp->aux->ksym_lnode.prev == LIST_POISON2;
411}
412
413void bpf_prog_kallsyms_add(struct bpf_prog *fp)
414{
Daniel Borkmann74451e662017-02-16 22:24:50 +0100415 if (!bpf_prog_kallsyms_candidate(fp) ||
416 !capable(CAP_SYS_ADMIN))
417 return;
418
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200419 spin_lock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100420 bpf_prog_ksym_node_add(fp->aux);
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200421 spin_unlock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100422}
423
424void bpf_prog_kallsyms_del(struct bpf_prog *fp)
425{
Daniel Borkmann74451e662017-02-16 22:24:50 +0100426 if (!bpf_prog_kallsyms_candidate(fp))
427 return;
428
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200429 spin_lock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100430 bpf_prog_ksym_node_del(fp->aux);
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200431 spin_unlock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100432}
433
434static struct bpf_prog *bpf_prog_kallsyms_find(unsigned long addr)
435{
436 struct latch_tree_node *n;
437
438 if (!bpf_jit_kallsyms_enabled())
439 return NULL;
440
441 n = latch_tree_find((void *)addr, &bpf_tree, &bpf_tree_ops);
442 return n ?
443 container_of(n, struct bpf_prog_aux, ksym_tnode)->prog :
444 NULL;
445}
446
447const char *__bpf_address_lookup(unsigned long addr, unsigned long *size,
448 unsigned long *off, char *sym)
449{
450 unsigned long symbol_start, symbol_end;
451 struct bpf_prog *prog;
452 char *ret = NULL;
453
454 rcu_read_lock();
455 prog = bpf_prog_kallsyms_find(addr);
456 if (prog) {
457 bpf_get_prog_addr_region(prog, &symbol_start, &symbol_end);
458 bpf_get_prog_name(prog, sym);
459
460 ret = sym;
461 if (size)
462 *size = symbol_end - symbol_start;
463 if (off)
464 *off = addr - symbol_start;
465 }
466 rcu_read_unlock();
467
468 return ret;
469}
470
471bool is_bpf_text_address(unsigned long addr)
472{
473 bool ret;
474
475 rcu_read_lock();
476 ret = bpf_prog_kallsyms_find(addr) != NULL;
477 rcu_read_unlock();
478
479 return ret;
480}
481
482int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
483 char *sym)
484{
485 unsigned long symbol_start, symbol_end;
486 struct bpf_prog_aux *aux;
487 unsigned int it = 0;
488 int ret = -ERANGE;
489
490 if (!bpf_jit_kallsyms_enabled())
491 return ret;
492
493 rcu_read_lock();
494 list_for_each_entry_rcu(aux, &bpf_kallsyms, ksym_lnode) {
495 if (it++ != symnum)
496 continue;
497
498 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
499 bpf_get_prog_name(aux->prog, sym);
500
501 *value = symbol_start;
502 *type = BPF_SYM_ELF_TYPE;
503
504 ret = 0;
505 break;
506 }
507 rcu_read_unlock();
508
509 return ret;
510}
511
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200512struct bpf_binary_header *
513bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
514 unsigned int alignment,
515 bpf_jit_fill_hole_t bpf_fill_ill_insns)
516{
517 struct bpf_binary_header *hdr;
518 unsigned int size, hole, start;
519
520 /* Most of BPF filters are really small, but if some of them
521 * fill a page, allow at least 128 extra bytes to insert a
522 * random section of illegal instructions.
523 */
524 size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
525 hdr = module_alloc(size);
526 if (hdr == NULL)
527 return NULL;
528
529 /* Fill space with illegal/arch-dep instructions. */
530 bpf_fill_ill_insns(hdr, size);
531
532 hdr->pages = size / PAGE_SIZE;
533 hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
534 PAGE_SIZE - sizeof(*hdr));
Daniel Borkmannb7552e1b2016-05-18 14:14:28 +0200535 start = (get_random_int() % hole) & ~(alignment - 1);
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200536
537 /* Leave a random number of instructions before BPF code. */
538 *image_ptr = &hdr->image[start];
539
540 return hdr;
541}
542
543void bpf_jit_binary_free(struct bpf_binary_header *hdr)
544{
Rusty Russellbe1f2212015-01-20 09:07:05 +1030545 module_memfree(hdr);
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200546}
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200547
Daniel Borkmann74451e662017-02-16 22:24:50 +0100548/* This symbol is only overridden by archs that have different
549 * requirements than the usual eBPF JITs, f.e. when they only
550 * implement cBPF JIT, do not set images read-only, etc.
551 */
552void __weak bpf_jit_free(struct bpf_prog *fp)
553{
554 if (fp->jited) {
555 struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp);
556
557 bpf_jit_binary_unlock_ro(hdr);
558 bpf_jit_binary_free(hdr);
559
560 WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp));
561 }
562
563 bpf_prog_unlock_free(fp);
564}
565
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200566int bpf_jit_harden __read_mostly;
567
568static int bpf_jit_blind_insn(const struct bpf_insn *from,
569 const struct bpf_insn *aux,
570 struct bpf_insn *to_buff)
571{
572 struct bpf_insn *to = to_buff;
Daniel Borkmannb7552e1b2016-05-18 14:14:28 +0200573 u32 imm_rnd = get_random_int();
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200574 s16 off;
575
576 BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG);
577 BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG);
578
579 if (from->imm == 0 &&
580 (from->code == (BPF_ALU | BPF_MOV | BPF_K) ||
581 from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) {
582 *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg);
583 goto out;
584 }
585
586 switch (from->code) {
587 case BPF_ALU | BPF_ADD | BPF_K:
588 case BPF_ALU | BPF_SUB | BPF_K:
589 case BPF_ALU | BPF_AND | BPF_K:
590 case BPF_ALU | BPF_OR | BPF_K:
591 case BPF_ALU | BPF_XOR | BPF_K:
592 case BPF_ALU | BPF_MUL | BPF_K:
593 case BPF_ALU | BPF_MOV | BPF_K:
594 case BPF_ALU | BPF_DIV | BPF_K:
595 case BPF_ALU | BPF_MOD | BPF_K:
596 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
597 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
598 *to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX);
599 break;
600
601 case BPF_ALU64 | BPF_ADD | BPF_K:
602 case BPF_ALU64 | BPF_SUB | BPF_K:
603 case BPF_ALU64 | BPF_AND | BPF_K:
604 case BPF_ALU64 | BPF_OR | BPF_K:
605 case BPF_ALU64 | BPF_XOR | BPF_K:
606 case BPF_ALU64 | BPF_MUL | BPF_K:
607 case BPF_ALU64 | BPF_MOV | BPF_K:
608 case BPF_ALU64 | BPF_DIV | BPF_K:
609 case BPF_ALU64 | BPF_MOD | BPF_K:
610 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
611 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
612 *to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX);
613 break;
614
615 case BPF_JMP | BPF_JEQ | BPF_K:
616 case BPF_JMP | BPF_JNE | BPF_K:
617 case BPF_JMP | BPF_JGT | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200618 case BPF_JMP | BPF_JLT | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200619 case BPF_JMP | BPF_JGE | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200620 case BPF_JMP | BPF_JLE | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200621 case BPF_JMP | BPF_JSGT | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200622 case BPF_JMP | BPF_JSLT | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200623 case BPF_JMP | BPF_JSGE | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200624 case BPF_JMP | BPF_JSLE | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200625 case BPF_JMP | BPF_JSET | BPF_K:
626 /* Accommodate for extra offset in case of a backjump. */
627 off = from->off;
628 if (off < 0)
629 off -= 2;
630 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
631 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
632 *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off);
633 break;
634
635 case BPF_LD | BPF_ABS | BPF_W:
636 case BPF_LD | BPF_ABS | BPF_H:
637 case BPF_LD | BPF_ABS | BPF_B:
638 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
639 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
640 *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
641 break;
642
643 case BPF_LD | BPF_IND | BPF_W:
644 case BPF_LD | BPF_IND | BPF_H:
645 case BPF_LD | BPF_IND | BPF_B:
646 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
647 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
648 *to++ = BPF_ALU32_REG(BPF_ADD, BPF_REG_AX, from->src_reg);
649 *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
650 break;
651
652 case BPF_LD | BPF_IMM | BPF_DW:
653 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm);
654 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
655 *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
656 *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX);
657 break;
658 case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */
659 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm);
660 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
661 *to++ = BPF_ALU64_REG(BPF_OR, aux[0].dst_reg, BPF_REG_AX);
662 break;
663
664 case BPF_ST | BPF_MEM | BPF_DW:
665 case BPF_ST | BPF_MEM | BPF_W:
666 case BPF_ST | BPF_MEM | BPF_H:
667 case BPF_ST | BPF_MEM | BPF_B:
668 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
669 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
670 *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off);
671 break;
672 }
673out:
674 return to - to_buff;
675}
676
677static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other,
678 gfp_t gfp_extra_flags)
679{
Michal Hocko19809c22017-05-08 15:57:44 -0700680 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200681 struct bpf_prog *fp;
682
683 fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags, PAGE_KERNEL);
684 if (fp != NULL) {
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200685 /* aux->prog still points to the fp_other one, so
686 * when promoting the clone to the real program,
687 * this still needs to be adapted.
688 */
689 memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE);
690 }
691
692 return fp;
693}
694
695static void bpf_prog_clone_free(struct bpf_prog *fp)
696{
697 /* aux was stolen by the other clone, so we cannot free
698 * it from this path! It will be freed eventually by the
699 * other program on release.
700 *
701 * At this point, we don't need a deferred release since
702 * clone is guaranteed to not be locked.
703 */
704 fp->aux = NULL;
705 __bpf_prog_free(fp);
706}
707
708void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other)
709{
710 /* We have to repoint aux->prog to self, as we don't
711 * know whether fp here is the clone or the original.
712 */
713 fp->aux->prog = fp;
714 bpf_prog_clone_free(fp_other);
715}
716
717struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
718{
719 struct bpf_insn insn_buff[16], aux[2];
720 struct bpf_prog *clone, *tmp;
721 int insn_delta, insn_cnt;
722 struct bpf_insn *insn;
723 int i, rewritten;
724
Alexei Starovoitov60b58afc2017-12-14 17:55:14 -0800725 if (!bpf_jit_blinding_enabled(prog))
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200726 return prog;
727
728 clone = bpf_prog_clone_create(prog, GFP_USER);
729 if (!clone)
730 return ERR_PTR(-ENOMEM);
731
732 insn_cnt = clone->len;
733 insn = clone->insnsi;
734
735 for (i = 0; i < insn_cnt; i++, insn++) {
736 /* We temporarily need to hold the original ld64 insn
737 * so that we can still access the first part in the
738 * second blinding run.
739 */
740 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) &&
741 insn[1].code == 0)
742 memcpy(aux, insn, sizeof(aux));
743
744 rewritten = bpf_jit_blind_insn(insn, aux, insn_buff);
745 if (!rewritten)
746 continue;
747
748 tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);
749 if (!tmp) {
750 /* Patching may have repointed aux->prog during
751 * realloc from the original one, so we need to
752 * fix it up here on error.
753 */
754 bpf_jit_prog_release_other(prog, clone);
755 return ERR_PTR(-ENOMEM);
756 }
757
758 clone = tmp;
759 insn_delta = rewritten - 1;
760
761 /* Walk new program and skip insns we just inserted. */
762 insn = clone->insnsi + i + insn_delta;
763 insn_cnt += insn_delta;
764 i += insn_delta;
765 }
766
767 return clone;
768}
Daniel Borkmannb954d832014-09-10 15:01:02 +0200769#endif /* CONFIG_BPF_JIT */
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200770
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700771/* Base function for offset calculation. Needs to go into .text section,
772 * therefore keeping it non-static as well; will also be used by JITs
773 * anyway later on, so do not let the compiler omit it.
774 */
775noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
776{
777 return 0;
778}
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -0700779EXPORT_SYMBOL_GPL(__bpf_call_base);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700780
781/**
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700782 * __bpf_prog_run - run eBPF program on a given context
783 * @ctx: is the data we are operating on
784 * @insn: is the array of eBPF instructions
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700785 *
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700786 * Decode and execute eBPF instructions.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700787 */
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800788static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700789{
Alexei Starovoitovf696b8f2017-05-30 13:31:28 -0700790 u64 tmp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700791 static const void *jumptable[256] = {
792 [0 ... 255] = &&default_label,
793 /* Now overwrite non-defaults ... */
794 /* 32 bit ALU operations */
795 [BPF_ALU | BPF_ADD | BPF_X] = &&ALU_ADD_X,
796 [BPF_ALU | BPF_ADD | BPF_K] = &&ALU_ADD_K,
797 [BPF_ALU | BPF_SUB | BPF_X] = &&ALU_SUB_X,
798 [BPF_ALU | BPF_SUB | BPF_K] = &&ALU_SUB_K,
799 [BPF_ALU | BPF_AND | BPF_X] = &&ALU_AND_X,
800 [BPF_ALU | BPF_AND | BPF_K] = &&ALU_AND_K,
801 [BPF_ALU | BPF_OR | BPF_X] = &&ALU_OR_X,
802 [BPF_ALU | BPF_OR | BPF_K] = &&ALU_OR_K,
803 [BPF_ALU | BPF_LSH | BPF_X] = &&ALU_LSH_X,
804 [BPF_ALU | BPF_LSH | BPF_K] = &&ALU_LSH_K,
805 [BPF_ALU | BPF_RSH | BPF_X] = &&ALU_RSH_X,
806 [BPF_ALU | BPF_RSH | BPF_K] = &&ALU_RSH_K,
807 [BPF_ALU | BPF_XOR | BPF_X] = &&ALU_XOR_X,
808 [BPF_ALU | BPF_XOR | BPF_K] = &&ALU_XOR_K,
809 [BPF_ALU | BPF_MUL | BPF_X] = &&ALU_MUL_X,
810 [BPF_ALU | BPF_MUL | BPF_K] = &&ALU_MUL_K,
811 [BPF_ALU | BPF_MOV | BPF_X] = &&ALU_MOV_X,
812 [BPF_ALU | BPF_MOV | BPF_K] = &&ALU_MOV_K,
813 [BPF_ALU | BPF_DIV | BPF_X] = &&ALU_DIV_X,
814 [BPF_ALU | BPF_DIV | BPF_K] = &&ALU_DIV_K,
815 [BPF_ALU | BPF_MOD | BPF_X] = &&ALU_MOD_X,
816 [BPF_ALU | BPF_MOD | BPF_K] = &&ALU_MOD_K,
817 [BPF_ALU | BPF_NEG] = &&ALU_NEG,
818 [BPF_ALU | BPF_END | BPF_TO_BE] = &&ALU_END_TO_BE,
819 [BPF_ALU | BPF_END | BPF_TO_LE] = &&ALU_END_TO_LE,
820 /* 64 bit ALU operations */
821 [BPF_ALU64 | BPF_ADD | BPF_X] = &&ALU64_ADD_X,
822 [BPF_ALU64 | BPF_ADD | BPF_K] = &&ALU64_ADD_K,
823 [BPF_ALU64 | BPF_SUB | BPF_X] = &&ALU64_SUB_X,
824 [BPF_ALU64 | BPF_SUB | BPF_K] = &&ALU64_SUB_K,
825 [BPF_ALU64 | BPF_AND | BPF_X] = &&ALU64_AND_X,
826 [BPF_ALU64 | BPF_AND | BPF_K] = &&ALU64_AND_K,
827 [BPF_ALU64 | BPF_OR | BPF_X] = &&ALU64_OR_X,
828 [BPF_ALU64 | BPF_OR | BPF_K] = &&ALU64_OR_K,
829 [BPF_ALU64 | BPF_LSH | BPF_X] = &&ALU64_LSH_X,
830 [BPF_ALU64 | BPF_LSH | BPF_K] = &&ALU64_LSH_K,
831 [BPF_ALU64 | BPF_RSH | BPF_X] = &&ALU64_RSH_X,
832 [BPF_ALU64 | BPF_RSH | BPF_K] = &&ALU64_RSH_K,
833 [BPF_ALU64 | BPF_XOR | BPF_X] = &&ALU64_XOR_X,
834 [BPF_ALU64 | BPF_XOR | BPF_K] = &&ALU64_XOR_K,
835 [BPF_ALU64 | BPF_MUL | BPF_X] = &&ALU64_MUL_X,
836 [BPF_ALU64 | BPF_MUL | BPF_K] = &&ALU64_MUL_K,
837 [BPF_ALU64 | BPF_MOV | BPF_X] = &&ALU64_MOV_X,
838 [BPF_ALU64 | BPF_MOV | BPF_K] = &&ALU64_MOV_K,
839 [BPF_ALU64 | BPF_ARSH | BPF_X] = &&ALU64_ARSH_X,
840 [BPF_ALU64 | BPF_ARSH | BPF_K] = &&ALU64_ARSH_K,
841 [BPF_ALU64 | BPF_DIV | BPF_X] = &&ALU64_DIV_X,
842 [BPF_ALU64 | BPF_DIV | BPF_K] = &&ALU64_DIV_K,
843 [BPF_ALU64 | BPF_MOD | BPF_X] = &&ALU64_MOD_X,
844 [BPF_ALU64 | BPF_MOD | BPF_K] = &&ALU64_MOD_K,
845 [BPF_ALU64 | BPF_NEG] = &&ALU64_NEG,
846 /* Call instruction */
847 [BPF_JMP | BPF_CALL] = &&JMP_CALL,
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800848 [BPF_JMP | BPF_CALL_ARGS] = &&JMP_CALL_ARGS,
Alexei Starovoitov71189fa2017-05-30 13:31:27 -0700849 [BPF_JMP | BPF_TAIL_CALL] = &&JMP_TAIL_CALL,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700850 /* Jumps */
851 [BPF_JMP | BPF_JA] = &&JMP_JA,
852 [BPF_JMP | BPF_JEQ | BPF_X] = &&JMP_JEQ_X,
853 [BPF_JMP | BPF_JEQ | BPF_K] = &&JMP_JEQ_K,
854 [BPF_JMP | BPF_JNE | BPF_X] = &&JMP_JNE_X,
855 [BPF_JMP | BPF_JNE | BPF_K] = &&JMP_JNE_K,
856 [BPF_JMP | BPF_JGT | BPF_X] = &&JMP_JGT_X,
857 [BPF_JMP | BPF_JGT | BPF_K] = &&JMP_JGT_K,
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200858 [BPF_JMP | BPF_JLT | BPF_X] = &&JMP_JLT_X,
859 [BPF_JMP | BPF_JLT | BPF_K] = &&JMP_JLT_K,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700860 [BPF_JMP | BPF_JGE | BPF_X] = &&JMP_JGE_X,
861 [BPF_JMP | BPF_JGE | BPF_K] = &&JMP_JGE_K,
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200862 [BPF_JMP | BPF_JLE | BPF_X] = &&JMP_JLE_X,
863 [BPF_JMP | BPF_JLE | BPF_K] = &&JMP_JLE_K,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700864 [BPF_JMP | BPF_JSGT | BPF_X] = &&JMP_JSGT_X,
865 [BPF_JMP | BPF_JSGT | BPF_K] = &&JMP_JSGT_K,
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200866 [BPF_JMP | BPF_JSLT | BPF_X] = &&JMP_JSLT_X,
867 [BPF_JMP | BPF_JSLT | BPF_K] = &&JMP_JSLT_K,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700868 [BPF_JMP | BPF_JSGE | BPF_X] = &&JMP_JSGE_X,
869 [BPF_JMP | BPF_JSGE | BPF_K] = &&JMP_JSGE_K,
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200870 [BPF_JMP | BPF_JSLE | BPF_X] = &&JMP_JSLE_X,
871 [BPF_JMP | BPF_JSLE | BPF_K] = &&JMP_JSLE_K,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700872 [BPF_JMP | BPF_JSET | BPF_X] = &&JMP_JSET_X,
873 [BPF_JMP | BPF_JSET | BPF_K] = &&JMP_JSET_K,
874 /* Program return */
875 [BPF_JMP | BPF_EXIT] = &&JMP_EXIT,
876 /* Store instructions */
877 [BPF_STX | BPF_MEM | BPF_B] = &&STX_MEM_B,
878 [BPF_STX | BPF_MEM | BPF_H] = &&STX_MEM_H,
879 [BPF_STX | BPF_MEM | BPF_W] = &&STX_MEM_W,
880 [BPF_STX | BPF_MEM | BPF_DW] = &&STX_MEM_DW,
881 [BPF_STX | BPF_XADD | BPF_W] = &&STX_XADD_W,
882 [BPF_STX | BPF_XADD | BPF_DW] = &&STX_XADD_DW,
883 [BPF_ST | BPF_MEM | BPF_B] = &&ST_MEM_B,
884 [BPF_ST | BPF_MEM | BPF_H] = &&ST_MEM_H,
885 [BPF_ST | BPF_MEM | BPF_W] = &&ST_MEM_W,
886 [BPF_ST | BPF_MEM | BPF_DW] = &&ST_MEM_DW,
887 /* Load instructions */
888 [BPF_LDX | BPF_MEM | BPF_B] = &&LDX_MEM_B,
889 [BPF_LDX | BPF_MEM | BPF_H] = &&LDX_MEM_H,
890 [BPF_LDX | BPF_MEM | BPF_W] = &&LDX_MEM_W,
891 [BPF_LDX | BPF_MEM | BPF_DW] = &&LDX_MEM_DW,
892 [BPF_LD | BPF_ABS | BPF_W] = &&LD_ABS_W,
893 [BPF_LD | BPF_ABS | BPF_H] = &&LD_ABS_H,
894 [BPF_LD | BPF_ABS | BPF_B] = &&LD_ABS_B,
895 [BPF_LD | BPF_IND | BPF_W] = &&LD_IND_W,
896 [BPF_LD | BPF_IND | BPF_H] = &&LD_IND_H,
897 [BPF_LD | BPF_IND | BPF_B] = &&LD_IND_B,
Alexei Starovoitov02ab6952014-09-04 22:17:17 -0700898 [BPF_LD | BPF_IMM | BPF_DW] = &&LD_IMM_DW,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700899 };
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700900 u32 tail_call_cnt = 0;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700901 void *ptr;
902 int off;
903
904#define CONT ({ insn++; goto select_insn; })
905#define CONT_JMP ({ insn++; goto select_insn; })
906
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700907select_insn:
908 goto *jumptable[insn->code];
909
910 /* ALU */
911#define ALU(OPCODE, OP) \
912 ALU64_##OPCODE##_X: \
913 DST = DST OP SRC; \
914 CONT; \
915 ALU_##OPCODE##_X: \
916 DST = (u32) DST OP (u32) SRC; \
917 CONT; \
918 ALU64_##OPCODE##_K: \
919 DST = DST OP IMM; \
920 CONT; \
921 ALU_##OPCODE##_K: \
922 DST = (u32) DST OP (u32) IMM; \
923 CONT;
924
925 ALU(ADD, +)
926 ALU(SUB, -)
927 ALU(AND, &)
928 ALU(OR, |)
929 ALU(LSH, <<)
930 ALU(RSH, >>)
931 ALU(XOR, ^)
932 ALU(MUL, *)
933#undef ALU
934 ALU_NEG:
935 DST = (u32) -DST;
936 CONT;
937 ALU64_NEG:
938 DST = -DST;
939 CONT;
940 ALU_MOV_X:
941 DST = (u32) SRC;
942 CONT;
943 ALU_MOV_K:
944 DST = (u32) IMM;
945 CONT;
946 ALU64_MOV_X:
947 DST = SRC;
948 CONT;
949 ALU64_MOV_K:
950 DST = IMM;
951 CONT;
Alexei Starovoitov02ab6952014-09-04 22:17:17 -0700952 LD_IMM_DW:
953 DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
954 insn++;
955 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700956 ALU64_ARSH_X:
957 (*(s64 *) &DST) >>= SRC;
958 CONT;
959 ALU64_ARSH_K:
960 (*(s64 *) &DST) >>= IMM;
961 CONT;
962 ALU64_MOD_X:
963 if (unlikely(SRC == 0))
964 return 0;
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -0700965 div64_u64_rem(DST, SRC, &tmp);
966 DST = tmp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700967 CONT;
968 ALU_MOD_X:
969 if (unlikely(SRC == 0))
970 return 0;
971 tmp = (u32) DST;
972 DST = do_div(tmp, (u32) SRC);
973 CONT;
974 ALU64_MOD_K:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -0700975 div64_u64_rem(DST, IMM, &tmp);
976 DST = tmp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700977 CONT;
978 ALU_MOD_K:
979 tmp = (u32) DST;
980 DST = do_div(tmp, (u32) IMM);
981 CONT;
982 ALU64_DIV_X:
983 if (unlikely(SRC == 0))
984 return 0;
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -0700985 DST = div64_u64(DST, SRC);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700986 CONT;
987 ALU_DIV_X:
988 if (unlikely(SRC == 0))
989 return 0;
990 tmp = (u32) DST;
991 do_div(tmp, (u32) SRC);
992 DST = (u32) tmp;
993 CONT;
994 ALU64_DIV_K:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -0700995 DST = div64_u64(DST, IMM);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700996 CONT;
997 ALU_DIV_K:
998 tmp = (u32) DST;
999 do_div(tmp, (u32) IMM);
1000 DST = (u32) tmp;
1001 CONT;
1002 ALU_END_TO_BE:
1003 switch (IMM) {
1004 case 16:
1005 DST = (__force u16) cpu_to_be16(DST);
1006 break;
1007 case 32:
1008 DST = (__force u32) cpu_to_be32(DST);
1009 break;
1010 case 64:
1011 DST = (__force u64) cpu_to_be64(DST);
1012 break;
1013 }
1014 CONT;
1015 ALU_END_TO_LE:
1016 switch (IMM) {
1017 case 16:
1018 DST = (__force u16) cpu_to_le16(DST);
1019 break;
1020 case 32:
1021 DST = (__force u32) cpu_to_le32(DST);
1022 break;
1023 case 64:
1024 DST = (__force u64) cpu_to_le64(DST);
1025 break;
1026 }
1027 CONT;
1028
1029 /* CALL */
1030 JMP_CALL:
1031 /* Function call scratches BPF_R1-BPF_R5 registers,
1032 * preserves BPF_R6-BPF_R9, and stores return value
1033 * into BPF_R0.
1034 */
1035 BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
1036 BPF_R4, BPF_R5);
1037 CONT;
1038
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001039 JMP_CALL_ARGS:
1040 BPF_R0 = (__bpf_call_base_args + insn->imm)(BPF_R1, BPF_R2,
1041 BPF_R3, BPF_R4,
1042 BPF_R5,
1043 insn + insn->off + 1);
1044 CONT;
1045
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001046 JMP_TAIL_CALL: {
1047 struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
1048 struct bpf_array *array = container_of(map, struct bpf_array, map);
1049 struct bpf_prog *prog;
Alexei Starovoitov90caccd2017-10-03 15:37:20 -07001050 u32 index = BPF_R3;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001051
1052 if (unlikely(index >= array->map.max_entries))
1053 goto out;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001054 if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
1055 goto out;
1056
1057 tail_call_cnt++;
1058
Wang Nan2a36f0b2015-08-06 07:02:33 +00001059 prog = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +02001060 if (!prog)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001061 goto out;
1062
Daniel Borkmannc4675f92015-07-13 20:49:32 +02001063 /* ARG1 at this point is guaranteed to point to CTX from
1064 * the verifier side due to the fact that the tail call is
1065 * handeled like a helper, that is, bpf_tail_call_proto,
1066 * where arg1_type is ARG_PTR_TO_CTX.
1067 */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001068 insn = prog->insnsi;
1069 goto select_insn;
1070out:
1071 CONT;
1072 }
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001073 /* JMP */
1074 JMP_JA:
1075 insn += insn->off;
1076 CONT;
1077 JMP_JEQ_X:
1078 if (DST == SRC) {
1079 insn += insn->off;
1080 CONT_JMP;
1081 }
1082 CONT;
1083 JMP_JEQ_K:
1084 if (DST == IMM) {
1085 insn += insn->off;
1086 CONT_JMP;
1087 }
1088 CONT;
1089 JMP_JNE_X:
1090 if (DST != SRC) {
1091 insn += insn->off;
1092 CONT_JMP;
1093 }
1094 CONT;
1095 JMP_JNE_K:
1096 if (DST != IMM) {
1097 insn += insn->off;
1098 CONT_JMP;
1099 }
1100 CONT;
1101 JMP_JGT_X:
1102 if (DST > SRC) {
1103 insn += insn->off;
1104 CONT_JMP;
1105 }
1106 CONT;
1107 JMP_JGT_K:
1108 if (DST > IMM) {
1109 insn += insn->off;
1110 CONT_JMP;
1111 }
1112 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001113 JMP_JLT_X:
1114 if (DST < SRC) {
1115 insn += insn->off;
1116 CONT_JMP;
1117 }
1118 CONT;
1119 JMP_JLT_K:
1120 if (DST < IMM) {
1121 insn += insn->off;
1122 CONT_JMP;
1123 }
1124 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001125 JMP_JGE_X:
1126 if (DST >= SRC) {
1127 insn += insn->off;
1128 CONT_JMP;
1129 }
1130 CONT;
1131 JMP_JGE_K:
1132 if (DST >= IMM) {
1133 insn += insn->off;
1134 CONT_JMP;
1135 }
1136 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001137 JMP_JLE_X:
1138 if (DST <= SRC) {
1139 insn += insn->off;
1140 CONT_JMP;
1141 }
1142 CONT;
1143 JMP_JLE_K:
1144 if (DST <= IMM) {
1145 insn += insn->off;
1146 CONT_JMP;
1147 }
1148 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001149 JMP_JSGT_X:
1150 if (((s64) DST) > ((s64) SRC)) {
1151 insn += insn->off;
1152 CONT_JMP;
1153 }
1154 CONT;
1155 JMP_JSGT_K:
1156 if (((s64) DST) > ((s64) IMM)) {
1157 insn += insn->off;
1158 CONT_JMP;
1159 }
1160 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001161 JMP_JSLT_X:
1162 if (((s64) DST) < ((s64) SRC)) {
1163 insn += insn->off;
1164 CONT_JMP;
1165 }
1166 CONT;
1167 JMP_JSLT_K:
1168 if (((s64) DST) < ((s64) IMM)) {
1169 insn += insn->off;
1170 CONT_JMP;
1171 }
1172 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001173 JMP_JSGE_X:
1174 if (((s64) DST) >= ((s64) SRC)) {
1175 insn += insn->off;
1176 CONT_JMP;
1177 }
1178 CONT;
1179 JMP_JSGE_K:
1180 if (((s64) DST) >= ((s64) IMM)) {
1181 insn += insn->off;
1182 CONT_JMP;
1183 }
1184 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001185 JMP_JSLE_X:
1186 if (((s64) DST) <= ((s64) SRC)) {
1187 insn += insn->off;
1188 CONT_JMP;
1189 }
1190 CONT;
1191 JMP_JSLE_K:
1192 if (((s64) DST) <= ((s64) IMM)) {
1193 insn += insn->off;
1194 CONT_JMP;
1195 }
1196 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001197 JMP_JSET_X:
1198 if (DST & SRC) {
1199 insn += insn->off;
1200 CONT_JMP;
1201 }
1202 CONT;
1203 JMP_JSET_K:
1204 if (DST & IMM) {
1205 insn += insn->off;
1206 CONT_JMP;
1207 }
1208 CONT;
1209 JMP_EXIT:
1210 return BPF_R0;
1211
1212 /* STX and ST and LDX*/
1213#define LDST(SIZEOP, SIZE) \
1214 STX_MEM_##SIZEOP: \
1215 *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \
1216 CONT; \
1217 ST_MEM_##SIZEOP: \
1218 *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \
1219 CONT; \
1220 LDX_MEM_##SIZEOP: \
1221 DST = *(SIZE *)(unsigned long) (SRC + insn->off); \
1222 CONT;
1223
1224 LDST(B, u8)
1225 LDST(H, u16)
1226 LDST(W, u32)
1227 LDST(DW, u64)
1228#undef LDST
1229 STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
1230 atomic_add((u32) SRC, (atomic_t *)(unsigned long)
1231 (DST + insn->off));
1232 CONT;
1233 STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
1234 atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
1235 (DST + insn->off));
1236 CONT;
1237 LD_ABS_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + imm32)) */
1238 off = IMM;
1239load_word:
Johannes Berg96a94cc2017-04-11 12:10:58 +02001240 /* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are only
1241 * appearing in the programs where ctx == skb
1242 * (see may_access_skb() in the verifier). All programs
1243 * keep 'ctx' in regs[BPF_REG_CTX] == BPF_R6,
1244 * bpf_convert_filter() saves it in BPF_R6, internal BPF
1245 * verifier will check that BPF_R6 == ctx.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001246 *
1247 * BPF_ABS and BPF_IND are wrappers of function calls,
1248 * so they scratch BPF_R1-BPF_R5 registers, preserve
1249 * BPF_R6-BPF_R9, and store return value into BPF_R0.
1250 *
1251 * Implicit input:
1252 * ctx == skb == BPF_R6 == CTX
1253 *
1254 * Explicit input:
1255 * SRC == any register
1256 * IMM == 32-bit immediate
1257 *
1258 * Output:
1259 * BPF_R0 - 8/16/32-bit skb data converted to cpu endianness
1260 */
1261
1262 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 4, &tmp);
1263 if (likely(ptr != NULL)) {
1264 BPF_R0 = get_unaligned_be32(ptr);
1265 CONT;
1266 }
1267
1268 return 0;
1269 LD_ABS_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + imm32)) */
1270 off = IMM;
1271load_half:
1272 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 2, &tmp);
1273 if (likely(ptr != NULL)) {
1274 BPF_R0 = get_unaligned_be16(ptr);
1275 CONT;
1276 }
1277
1278 return 0;
1279 LD_ABS_B: /* BPF_R0 = *(u8 *) (skb->data + imm32) */
1280 off = IMM;
1281load_byte:
1282 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 1, &tmp);
1283 if (likely(ptr != NULL)) {
1284 BPF_R0 = *(u8 *)ptr;
1285 CONT;
1286 }
1287
1288 return 0;
1289 LD_IND_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + src_reg + imm32)) */
1290 off = IMM + SRC;
1291 goto load_word;
1292 LD_IND_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + src_reg + imm32)) */
1293 off = IMM + SRC;
1294 goto load_half;
1295 LD_IND_B: /* BPF_R0 = *(u8 *) (skb->data + src_reg + imm32) */
1296 off = IMM + SRC;
1297 goto load_byte;
1298
1299 default_label:
1300 /* If we ever reach this, we have a bug somewhere. */
1301 WARN_RATELIMIT(1, "unknown opcode %02x\n", insn->code);
1302 return 0;
1303}
Alexei Starovoitovf696b8f2017-05-30 13:31:28 -07001304STACK_FRAME_NON_STANDARD(___bpf_prog_run); /* jump table */
1305
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001306#define PROG_NAME(stack_size) __bpf_prog_run##stack_size
1307#define DEFINE_BPF_PROG_RUN(stack_size) \
1308static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \
1309{ \
1310 u64 stack[stack_size / sizeof(u64)]; \
1311 u64 regs[MAX_BPF_REG]; \
1312\
1313 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1314 ARG1 = (u64) (unsigned long) ctx; \
1315 return ___bpf_prog_run(regs, insn, stack); \
Alexei Starovoitovf696b8f2017-05-30 13:31:28 -07001316}
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001317
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001318#define PROG_NAME_ARGS(stack_size) __bpf_prog_run_args##stack_size
1319#define DEFINE_BPF_PROG_RUN_ARGS(stack_size) \
1320static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \
1321 const struct bpf_insn *insn) \
1322{ \
1323 u64 stack[stack_size / sizeof(u64)]; \
1324 u64 regs[MAX_BPF_REG]; \
1325\
1326 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1327 BPF_R1 = r1; \
1328 BPF_R2 = r2; \
1329 BPF_R3 = r3; \
1330 BPF_R4 = r4; \
1331 BPF_R5 = r5; \
1332 return ___bpf_prog_run(regs, insn, stack); \
1333}
1334
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001335#define EVAL1(FN, X) FN(X)
1336#define EVAL2(FN, X, Y...) FN(X) EVAL1(FN, Y)
1337#define EVAL3(FN, X, Y...) FN(X) EVAL2(FN, Y)
1338#define EVAL4(FN, X, Y...) FN(X) EVAL3(FN, Y)
1339#define EVAL5(FN, X, Y...) FN(X) EVAL4(FN, Y)
1340#define EVAL6(FN, X, Y...) FN(X) EVAL5(FN, Y)
1341
1342EVAL6(DEFINE_BPF_PROG_RUN, 32, 64, 96, 128, 160, 192);
1343EVAL6(DEFINE_BPF_PROG_RUN, 224, 256, 288, 320, 352, 384);
1344EVAL4(DEFINE_BPF_PROG_RUN, 416, 448, 480, 512);
1345
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001346EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 32, 64, 96, 128, 160, 192);
1347EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 224, 256, 288, 320, 352, 384);
1348EVAL4(DEFINE_BPF_PROG_RUN_ARGS, 416, 448, 480, 512);
1349
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001350#define PROG_NAME_LIST(stack_size) PROG_NAME(stack_size),
1351
1352static unsigned int (*interpreters[])(const void *ctx,
1353 const struct bpf_insn *insn) = {
1354EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1355EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1356EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1357};
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001358#undef PROG_NAME_LIST
1359#define PROG_NAME_LIST(stack_size) PROG_NAME_ARGS(stack_size),
1360static u64 (*interpreters_args[])(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5,
1361 const struct bpf_insn *insn) = {
1362EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1363EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1364EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1365};
1366#undef PROG_NAME_LIST
1367
1368void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth)
1369{
1370 stack_depth = max_t(u32, stack_depth, 1);
1371 insn->off = (s16) insn->imm;
1372 insn->imm = interpreters_args[(round_up(stack_depth, 32) / 32) - 1] -
1373 __bpf_call_base_args;
1374 insn->code = BPF_JMP | BPF_CALL_ARGS;
1375}
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001376
Daniel Borkmann3324b582015-05-29 23:23:07 +02001377bool bpf_prog_array_compatible(struct bpf_array *array,
1378 const struct bpf_prog *fp)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001379{
Josef Bacik9802d862017-12-11 11:36:48 -05001380 if (fp->kprobe_override)
1381 return false;
1382
Daniel Borkmann3324b582015-05-29 23:23:07 +02001383 if (!array->owner_prog_type) {
1384 /* There's no owner yet where we could check for
1385 * compatibility.
1386 */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001387 array->owner_prog_type = fp->type;
1388 array->owner_jited = fp->jited;
Daniel Borkmann3324b582015-05-29 23:23:07 +02001389
1390 return true;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001391 }
Daniel Borkmann3324b582015-05-29 23:23:07 +02001392
1393 return array->owner_prog_type == fp->type &&
1394 array->owner_jited == fp->jited;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001395}
1396
Daniel Borkmann3324b582015-05-29 23:23:07 +02001397static int bpf_check_tail_call(const struct bpf_prog *fp)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001398{
1399 struct bpf_prog_aux *aux = fp->aux;
1400 int i;
1401
1402 for (i = 0; i < aux->used_map_cnt; i++) {
Daniel Borkmann3324b582015-05-29 23:23:07 +02001403 struct bpf_map *map = aux->used_maps[i];
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001404 struct bpf_array *array;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001405
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001406 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1407 continue;
Daniel Borkmann3324b582015-05-29 23:23:07 +02001408
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001409 array = container_of(map, struct bpf_array, map);
1410 if (!bpf_prog_array_compatible(array, fp))
1411 return -EINVAL;
1412 }
1413
1414 return 0;
1415}
1416
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001417/**
Daniel Borkmann3324b582015-05-29 23:23:07 +02001418 * bpf_prog_select_runtime - select exec runtime for BPF program
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001419 * @fp: bpf_prog populated with internal BPF program
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001420 * @err: pointer to error variable
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001421 *
Daniel Borkmann3324b582015-05-29 23:23:07 +02001422 * Try to JIT eBPF program, if JIT is not available, use interpreter.
1423 * The BPF program will be executed via BPF_PROG_RUN() macro.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001424 */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001425struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001426{
Martin KaFai Lau8007e402017-06-28 10:41:24 -07001427 u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1);
1428
1429 fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1];
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001430
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001431 /* eBPF JITs can rewrite the program in case constant
1432 * blinding is active. However, in case of error during
1433 * blinding, bpf_int_jit_compile() must always return a
1434 * valid program, which in this case would simply not
1435 * be JITed, but falls back to the interpreter.
1436 */
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001437 if (!bpf_prog_is_dev_bound(fp->aux)) {
1438 fp = bpf_int_jit_compile(fp);
1439 } else {
1440 *err = bpf_prog_offload_compile(fp);
1441 if (*err)
1442 return fp;
1443 }
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001444 bpf_prog_lock_ro(fp);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001445
Daniel Borkmann3324b582015-05-29 23:23:07 +02001446 /* The tail call compatibility check can only be done at
1447 * this late stage as we need to determine, if we deal
1448 * with JITed or non JITed program concatenations and not
1449 * all eBPF JITs might immediately support all features.
1450 */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001451 *err = bpf_check_tail_call(fp);
1452
1453 return fp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001454}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001455EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001456
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001457static unsigned int __bpf_prog_ret1(const void *ctx,
1458 const struct bpf_insn *insn)
1459{
1460 return 1;
1461}
1462
1463static struct bpf_prog_dummy {
1464 struct bpf_prog prog;
1465} dummy_bpf_prog = {
1466 .prog = {
1467 .bpf_func = __bpf_prog_ret1,
1468 },
1469};
1470
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001471/* to avoid allocating empty bpf_prog_array for cgroups that
1472 * don't have bpf program attached use one global 'empty_prog_array'
1473 * It will not be modified the caller of bpf_prog_array_alloc()
1474 * (since caller requested prog_cnt == 0)
1475 * that pointer should be 'freed' by bpf_prog_array_free()
1476 */
1477static struct {
1478 struct bpf_prog_array hdr;
1479 struct bpf_prog *null_prog;
1480} empty_prog_array = {
1481 .null_prog = NULL,
1482};
1483
1484struct bpf_prog_array __rcu *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags)
1485{
1486 if (prog_cnt)
1487 return kzalloc(sizeof(struct bpf_prog_array) +
1488 sizeof(struct bpf_prog *) * (prog_cnt + 1),
1489 flags);
1490
1491 return &empty_prog_array.hdr;
1492}
1493
1494void bpf_prog_array_free(struct bpf_prog_array __rcu *progs)
1495{
1496 if (!progs ||
1497 progs == (struct bpf_prog_array __rcu *)&empty_prog_array.hdr)
1498 return;
1499 kfree_rcu(progs, rcu);
1500}
1501
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001502int bpf_prog_array_length(struct bpf_prog_array __rcu *progs)
1503{
1504 struct bpf_prog **prog;
1505 u32 cnt = 0;
1506
1507 rcu_read_lock();
1508 prog = rcu_dereference(progs)->progs;
1509 for (; *prog; prog++)
Yonghong Songc8c088b2017-11-30 13:47:54 -08001510 if (*prog != &dummy_bpf_prog.prog)
1511 cnt++;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001512 rcu_read_unlock();
1513 return cnt;
1514}
1515
1516int bpf_prog_array_copy_to_user(struct bpf_prog_array __rcu *progs,
1517 __u32 __user *prog_ids, u32 cnt)
1518{
1519 struct bpf_prog **prog;
1520 u32 i = 0, id;
1521
1522 rcu_read_lock();
1523 prog = rcu_dereference(progs)->progs;
1524 for (; *prog; prog++) {
Yonghong Songf371b302017-12-11 11:39:02 -08001525 if (*prog == &dummy_bpf_prog.prog)
1526 continue;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001527 id = (*prog)->aux->id;
1528 if (copy_to_user(prog_ids + i, &id, sizeof(id))) {
1529 rcu_read_unlock();
1530 return -EFAULT;
1531 }
1532 if (++i == cnt) {
1533 prog++;
1534 break;
1535 }
1536 }
1537 rcu_read_unlock();
1538 if (*prog)
1539 return -ENOSPC;
1540 return 0;
1541}
1542
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001543void bpf_prog_array_delete_safe(struct bpf_prog_array __rcu *progs,
1544 struct bpf_prog *old_prog)
1545{
1546 struct bpf_prog **prog = progs->progs;
1547
1548 for (; *prog; prog++)
1549 if (*prog == old_prog) {
1550 WRITE_ONCE(*prog, &dummy_bpf_prog.prog);
1551 break;
1552 }
1553}
1554
1555int bpf_prog_array_copy(struct bpf_prog_array __rcu *old_array,
1556 struct bpf_prog *exclude_prog,
1557 struct bpf_prog *include_prog,
1558 struct bpf_prog_array **new_array)
1559{
1560 int new_prog_cnt, carry_prog_cnt = 0;
1561 struct bpf_prog **existing_prog;
1562 struct bpf_prog_array *array;
1563 int new_prog_idx = 0;
1564
1565 /* Figure out how many existing progs we need to carry over to
1566 * the new array.
1567 */
1568 if (old_array) {
1569 existing_prog = old_array->progs;
1570 for (; *existing_prog; existing_prog++) {
1571 if (*existing_prog != exclude_prog &&
1572 *existing_prog != &dummy_bpf_prog.prog)
1573 carry_prog_cnt++;
1574 if (*existing_prog == include_prog)
1575 return -EEXIST;
1576 }
1577 }
1578
1579 /* How many progs (not NULL) will be in the new array? */
1580 new_prog_cnt = carry_prog_cnt;
1581 if (include_prog)
1582 new_prog_cnt += 1;
1583
1584 /* Do we have any prog (not NULL) in the new array? */
1585 if (!new_prog_cnt) {
1586 *new_array = NULL;
1587 return 0;
1588 }
1589
1590 /* +1 as the end of prog_array is marked with NULL */
1591 array = bpf_prog_array_alloc(new_prog_cnt + 1, GFP_KERNEL);
1592 if (!array)
1593 return -ENOMEM;
1594
1595 /* Fill in the new prog array */
1596 if (carry_prog_cnt) {
1597 existing_prog = old_array->progs;
1598 for (; *existing_prog; existing_prog++)
1599 if (*existing_prog != exclude_prog &&
1600 *existing_prog != &dummy_bpf_prog.prog)
1601 array->progs[new_prog_idx++] = *existing_prog;
1602 }
1603 if (include_prog)
1604 array->progs[new_prog_idx++] = include_prog;
1605 array->progs[new_prog_idx] = NULL;
1606 *new_array = array;
1607 return 0;
1608}
1609
Yonghong Songf371b302017-12-11 11:39:02 -08001610int bpf_prog_array_copy_info(struct bpf_prog_array __rcu *array,
1611 __u32 __user *prog_ids, u32 request_cnt,
1612 __u32 __user *prog_cnt)
1613{
1614 u32 cnt = 0;
1615
1616 if (array)
1617 cnt = bpf_prog_array_length(array);
1618
1619 if (copy_to_user(prog_cnt, &cnt, sizeof(cnt)))
1620 return -EFAULT;
1621
1622 /* return early if user requested only program count or nothing to copy */
1623 if (!request_cnt || !cnt)
1624 return 0;
1625
1626 return bpf_prog_array_copy_to_user(array, prog_ids, request_cnt);
1627}
1628
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001629static void bpf_prog_free_deferred(struct work_struct *work)
1630{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001631 struct bpf_prog_aux *aux;
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001632
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001633 aux = container_of(work, struct bpf_prog_aux, work);
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001634 if (bpf_prog_is_dev_bound(aux))
1635 bpf_prog_offload_destroy(aux->prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001636 bpf_jit_free(aux->prog);
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001637}
1638
1639/* Free internal BPF program */
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001640void bpf_prog_free(struct bpf_prog *fp)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001641{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001642 struct bpf_prog_aux *aux = fp->aux;
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001643
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001644 INIT_WORK(&aux->work, bpf_prog_free_deferred);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001645 schedule_work(&aux->work);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001646}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001647EXPORT_SYMBOL_GPL(bpf_prog_free);
Alexei Starovoitovf89b7752014-10-23 18:41:08 -07001648
Daniel Borkmann3ad00402015-10-08 01:20:39 +02001649/* RNG for unpriviledged user space with separated state from prandom_u32(). */
1650static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
1651
1652void bpf_user_rnd_init_once(void)
1653{
1654 prandom_init_once(&bpf_user_rnd_state);
1655}
1656
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001657BPF_CALL_0(bpf_user_rnd_u32)
Daniel Borkmann3ad00402015-10-08 01:20:39 +02001658{
1659 /* Should someone ever have the rather unwise idea to use some
1660 * of the registers passed into this function, then note that
1661 * this function is called from native eBPF and classic-to-eBPF
1662 * transformations. Register assignments from both sides are
1663 * different, f.e. classic always sets fn(ctx, A, X) here.
1664 */
1665 struct rnd_state *state;
1666 u32 res;
1667
1668 state = &get_cpu_var(bpf_user_rnd_state);
1669 res = prandom_u32_state(state);
Shaohua Lib761fe22016-09-27 08:42:41 -07001670 put_cpu_var(bpf_user_rnd_state);
Daniel Borkmann3ad00402015-10-08 01:20:39 +02001671
1672 return res;
1673}
1674
Daniel Borkmann3ba67da2015-03-05 23:27:51 +01001675/* Weak definitions of helper functions in case we don't have bpf syscall. */
1676const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
1677const struct bpf_func_proto bpf_map_update_elem_proto __weak;
1678const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
1679
Daniel Borkmann03e69b52015-03-14 02:27:16 +01001680const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
Daniel Borkmannc04167c2015-03-14 02:27:17 +01001681const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
Daniel Borkmann2d0e30c2016-10-21 12:46:33 +02001682const struct bpf_func_proto bpf_get_numa_node_id_proto __weak;
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +02001683const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001684
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -07001685const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
1686const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
1687const struct bpf_func_proto bpf_get_current_comm_proto __weak;
John Fastabend6bdc9c42017-08-16 15:02:32 -07001688const struct bpf_func_proto bpf_sock_map_update_proto __weak;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001689
Alexei Starovoitov0756ea32015-06-12 19:39:13 -07001690const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
1691{
1692 return NULL;
1693}
Daniel Borkmann03e69b52015-03-14 02:27:16 +01001694
Daniel Borkmann555c8a82016-07-14 18:08:05 +02001695u64 __weak
1696bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1697 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001698{
Daniel Borkmann555c8a82016-07-14 18:08:05 +02001699 return -ENOTSUPP;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001700}
1701
Daniel Borkmann3324b582015-05-29 23:23:07 +02001702/* Always built-in helper functions. */
1703const struct bpf_func_proto bpf_tail_call_proto = {
1704 .func = NULL,
1705 .gpl_only = false,
1706 .ret_type = RET_VOID,
1707 .arg1_type = ARG_PTR_TO_CTX,
1708 .arg2_type = ARG_CONST_MAP_PTR,
1709 .arg3_type = ARG_ANYTHING,
1710};
1711
Daniel Borkmann93831912017-02-16 22:24:49 +01001712/* Stub for JITs that only support cBPF. eBPF programs are interpreted.
1713 * It is encouraged to implement bpf_int_jit_compile() instead, so that
1714 * eBPF and implicitly also cBPF can get JITed!
1715 */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001716struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
Daniel Borkmann3324b582015-05-29 23:23:07 +02001717{
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001718 return prog;
Daniel Borkmann3324b582015-05-29 23:23:07 +02001719}
1720
Daniel Borkmann93831912017-02-16 22:24:49 +01001721/* Stub for JITs that support eBPF. All cBPF code gets transformed into
1722 * eBPF by the kernel and is later compiled by bpf_int_jit_compile().
1723 */
1724void __weak bpf_jit_compile(struct bpf_prog *prog)
1725{
1726}
1727
Martin KaFai Lau17bedab2016-12-07 15:53:11 -08001728bool __weak bpf_helper_changes_pkt_data(void *func)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001729{
1730 return false;
1731}
1732
Alexei Starovoitovf89b7752014-10-23 18:41:08 -07001733/* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
1734 * skb_copy_bits(), so provide a weak definition of it for NET-less config.
1735 */
1736int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
1737 int len)
1738{
1739 return -EFAULT;
1740}
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001741
1742/* All definitions of tracepoints related to BPF. */
1743#define CREATE_TRACE_POINTS
1744#include <linux/bpf_trace.h>
1745
1746EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception);
1747
Steven Rostedt (VMware)9185a612017-10-12 18:40:02 -04001748/* These are only used within the BPF_SYSCALL code */
1749#ifdef CONFIG_BPF_SYSCALL
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001750EXPORT_TRACEPOINT_SYMBOL_GPL(bpf_prog_get_type);
1751EXPORT_TRACEPOINT_SYMBOL_GPL(bpf_prog_put_rcu);
Steven Rostedt (VMware)9185a612017-10-12 18:40:02 -04001752#endif