blob: 9349a5db3cf2e8c0ee241215d6c2211d9ad294de [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>
Yonghong Songc195651e2018-04-28 22:28:08 -070034#include <linux/perf_event.h>
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -070035
Daniel Borkmann3324b582015-05-29 23:23:07 +020036#include <asm/unaligned.h>
37
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -070038/* Registers */
39#define BPF_R0 regs[BPF_REG_0]
40#define BPF_R1 regs[BPF_REG_1]
41#define BPF_R2 regs[BPF_REG_2]
42#define BPF_R3 regs[BPF_REG_3]
43#define BPF_R4 regs[BPF_REG_4]
44#define BPF_R5 regs[BPF_REG_5]
45#define BPF_R6 regs[BPF_REG_6]
46#define BPF_R7 regs[BPF_REG_7]
47#define BPF_R8 regs[BPF_REG_8]
48#define BPF_R9 regs[BPF_REG_9]
49#define BPF_R10 regs[BPF_REG_10]
50
51/* Named registers */
52#define DST regs[insn->dst_reg]
53#define SRC regs[insn->src_reg]
54#define FP regs[BPF_REG_FP]
55#define ARG1 regs[BPF_REG_ARG1]
56#define CTX regs[BPF_REG_CTX]
57#define IMM insn->imm
58
59/* No hurry in this branch
60 *
61 * Exported for the bpf jit load helper.
62 */
63void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
64{
65 u8 *ptr = NULL;
66
67 if (k >= SKF_NET_OFF)
68 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
69 else if (k >= SKF_LL_OFF)
70 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
Daniel Borkmann3324b582015-05-29 23:23:07 +020071
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -070072 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
73 return ptr;
74
75 return NULL;
76}
77
Daniel Borkmann60a3b222014-09-02 22:53:44 +020078struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
79{
Michal Hocko19809c22017-05-08 15:57:44 -070080 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
Alexei Starovoitov09756af2014-09-26 00:17:00 -070081 struct bpf_prog_aux *aux;
Daniel Borkmann60a3b222014-09-02 22:53:44 +020082 struct bpf_prog *fp;
83
84 size = round_up(size, PAGE_SIZE);
85 fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
86 if (fp == NULL)
87 return NULL;
88
Alexei Starovoitov09756af2014-09-26 00:17:00 -070089 aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);
90 if (aux == NULL) {
Daniel Borkmann60a3b222014-09-02 22:53:44 +020091 vfree(fp);
92 return NULL;
93 }
94
95 fp->pages = size / PAGE_SIZE;
Alexei Starovoitov09756af2014-09-26 00:17:00 -070096 fp->aux = aux;
Daniel Borkmanne9d8afa2015-10-29 14:58:08 +010097 fp->aux->prog = fp;
Alexei Starovoitov60b58afc2017-12-14 17:55:14 -080098 fp->jit_requested = ebpf_jit_enabled();
Daniel Borkmann60a3b222014-09-02 22:53:44 +020099
Daniel Borkmann74451e662017-02-16 22:24:50 +0100100 INIT_LIST_HEAD_RCU(&fp->aux->ksym_lnode);
101
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200102 return fp;
103}
104EXPORT_SYMBOL_GPL(bpf_prog_alloc);
105
106struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
107 gfp_t gfp_extra_flags)
108{
Michal Hocko19809c22017-05-08 15:57:44 -0700109 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200110 struct bpf_prog *fp;
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100111 u32 pages, delta;
112 int ret;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200113
114 BUG_ON(fp_old == NULL);
115
116 size = round_up(size, PAGE_SIZE);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100117 pages = size / PAGE_SIZE;
118 if (pages <= fp_old->pages)
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200119 return fp_old;
120
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100121 delta = pages - fp_old->pages;
122 ret = __bpf_prog_charge(fp_old->aux->user, delta);
123 if (ret)
124 return NULL;
125
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200126 fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100127 if (fp == NULL) {
128 __bpf_prog_uncharge(fp_old->aux->user, delta);
129 } else {
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200130 memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100131 fp->pages = pages;
Daniel Borkmanne9d8afa2015-10-29 14:58:08 +0100132 fp->aux->prog = fp;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200133
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700134 /* We keep fp->aux from fp_old around in the new
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200135 * reallocated structure.
136 */
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700137 fp_old->aux = NULL;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200138 __bpf_prog_free(fp_old);
139 }
140
141 return fp;
142}
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200143
144void __bpf_prog_free(struct bpf_prog *fp)
145{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700146 kfree(fp->aux);
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200147 vfree(fp);
148}
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200149
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100150int bpf_prog_calc_tag(struct bpf_prog *fp)
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100151{
152 const u32 bits_offset = SHA_MESSAGE_BYTES - sizeof(__be64);
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100153 u32 raw_size = bpf_prog_tag_scratch_size(fp);
154 u32 digest[SHA_DIGEST_WORDS];
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100155 u32 ws[SHA_WORKSPACE_WORDS];
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100156 u32 i, bsize, psize, blocks;
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100157 struct bpf_insn *dst;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100158 bool was_ld_map;
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100159 u8 *raw, *todo;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100160 __be32 *result;
161 __be64 *bits;
162
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100163 raw = vmalloc(raw_size);
164 if (!raw)
165 return -ENOMEM;
166
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100167 sha_init(digest);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100168 memset(ws, 0, sizeof(ws));
169
170 /* We need to take out the map fd for the digest calculation
171 * since they are unstable from user space side.
172 */
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100173 dst = (void *)raw;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100174 for (i = 0, was_ld_map = false; i < fp->len; i++) {
175 dst[i] = fp->insnsi[i];
176 if (!was_ld_map &&
177 dst[i].code == (BPF_LD | BPF_IMM | BPF_DW) &&
178 dst[i].src_reg == BPF_PSEUDO_MAP_FD) {
179 was_ld_map = true;
180 dst[i].imm = 0;
181 } else if (was_ld_map &&
182 dst[i].code == 0 &&
183 dst[i].dst_reg == 0 &&
184 dst[i].src_reg == 0 &&
185 dst[i].off == 0) {
186 was_ld_map = false;
187 dst[i].imm = 0;
188 } else {
189 was_ld_map = false;
190 }
191 }
192
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100193 psize = bpf_prog_insn_size(fp);
194 memset(&raw[psize], 0, raw_size - psize);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100195 raw[psize++] = 0x80;
196
197 bsize = round_up(psize, SHA_MESSAGE_BYTES);
198 blocks = bsize / SHA_MESSAGE_BYTES;
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100199 todo = raw;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100200 if (bsize - psize >= sizeof(__be64)) {
201 bits = (__be64 *)(todo + bsize - sizeof(__be64));
202 } else {
203 bits = (__be64 *)(todo + bsize + bits_offset);
204 blocks++;
205 }
206 *bits = cpu_to_be64((psize - 1) << 3);
207
208 while (blocks--) {
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100209 sha_transform(digest, todo, ws);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100210 todo += SHA_MESSAGE_BYTES;
211 }
212
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100213 result = (__force __be32 *)digest;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100214 for (i = 0; i < SHA_DIGEST_WORDS; i++)
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100215 result[i] = cpu_to_be32(digest[i]);
216 memcpy(fp->tag, result, sizeof(fp->tag));
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100217
218 vfree(raw);
219 return 0;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100220}
221
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200222static void bpf_adj_branches(struct bpf_prog *prog, u32 pos, u32 delta)
223{
224 struct bpf_insn *insn = prog->insnsi;
225 u32 i, insn_cnt = prog->len;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800226 bool pseudo_call;
227 u8 code;
228 int off;
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200229
230 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800231 code = insn->code;
232 if (BPF_CLASS(code) != BPF_JMP)
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200233 continue;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800234 if (BPF_OP(code) == BPF_EXIT)
235 continue;
236 if (BPF_OP(code) == BPF_CALL) {
237 if (insn->src_reg == BPF_PSEUDO_CALL)
238 pseudo_call = true;
239 else
240 continue;
241 } else {
242 pseudo_call = false;
243 }
244 off = pseudo_call ? insn->imm : insn->off;
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200245
246 /* Adjust offset of jmps if we cross boundaries. */
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800247 if (i < pos && i + off + 1 > pos)
248 off += delta;
249 else if (i > pos + delta && i + off + 1 <= pos + delta)
250 off -= delta;
251
252 if (pseudo_call)
253 insn->imm = off;
254 else
255 insn->off = off;
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200256 }
257}
258
259struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
260 const struct bpf_insn *patch, u32 len)
261{
262 u32 insn_adj_cnt, insn_rest, insn_delta = len - 1;
263 struct bpf_prog *prog_adj;
264
265 /* Since our patchlet doesn't expand the image, we're done. */
266 if (insn_delta == 0) {
267 memcpy(prog->insnsi + off, patch, sizeof(*patch));
268 return prog;
269 }
270
271 insn_adj_cnt = prog->len + insn_delta;
272
273 /* Several new instructions need to be inserted. Make room
274 * for them. Likely, there's no need for a new allocation as
275 * last page could have large enough tailroom.
276 */
277 prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),
278 GFP_USER);
279 if (!prog_adj)
280 return NULL;
281
282 prog_adj->len = insn_adj_cnt;
283
284 /* Patching happens in 3 steps:
285 *
286 * 1) Move over tail of insnsi from next instruction onwards,
287 * so we can patch the single target insn with one or more
288 * new ones (patching is always from 1 to n insns, n > 0).
289 * 2) Inject new instructions at the target location.
290 * 3) Adjust branch offsets if necessary.
291 */
292 insn_rest = insn_adj_cnt - off - len;
293
294 memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1,
295 sizeof(*patch) * insn_rest);
296 memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len);
297
298 bpf_adj_branches(prog_adj, off, insn_delta);
299
300 return prog_adj;
301}
302
Daniel Borkmannb954d832014-09-10 15:01:02 +0200303#ifdef CONFIG_BPF_JIT
Daniel Borkmannfa9dd592018-01-20 01:24:33 +0100304/* All BPF JIT sysctl knobs here. */
305int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_ALWAYS_ON);
306int bpf_jit_harden __read_mostly;
307int bpf_jit_kallsyms __read_mostly;
308
Daniel Borkmann74451e662017-02-16 22:24:50 +0100309static __always_inline void
310bpf_get_prog_addr_region(const struct bpf_prog *prog,
311 unsigned long *symbol_start,
312 unsigned long *symbol_end)
313{
314 const struct bpf_binary_header *hdr = bpf_jit_binary_hdr(prog);
315 unsigned long addr = (unsigned long)hdr;
316
317 WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog));
318
319 *symbol_start = addr;
320 *symbol_end = addr + hdr->pages * PAGE_SIZE;
321}
322
323static void bpf_get_prog_name(const struct bpf_prog *prog, char *sym)
324{
Martin KaFai Lau368211f2017-10-05 21:52:13 -0700325 const char *end = sym + KSYM_NAME_LEN;
326
Daniel Borkmann74451e662017-02-16 22:24:50 +0100327 BUILD_BUG_ON(sizeof("bpf_prog_") +
Martin KaFai Lau368211f2017-10-05 21:52:13 -0700328 sizeof(prog->tag) * 2 +
329 /* name has been null terminated.
330 * We should need +1 for the '_' preceding
331 * the name. However, the null character
332 * is double counted between the name and the
333 * sizeof("bpf_prog_") above, so we omit
334 * the +1 here.
335 */
336 sizeof(prog->aux->name) > KSYM_NAME_LEN);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100337
338 sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_");
339 sym = bin2hex(sym, prog->tag, sizeof(prog->tag));
Martin KaFai Lau368211f2017-10-05 21:52:13 -0700340 if (prog->aux->name[0])
341 snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name);
342 else
343 *sym = 0;
Daniel Borkmann74451e662017-02-16 22:24:50 +0100344}
345
346static __always_inline unsigned long
347bpf_get_prog_addr_start(struct latch_tree_node *n)
348{
349 unsigned long symbol_start, symbol_end;
350 const struct bpf_prog_aux *aux;
351
352 aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
353 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
354
355 return symbol_start;
356}
357
358static __always_inline bool bpf_tree_less(struct latch_tree_node *a,
359 struct latch_tree_node *b)
360{
361 return bpf_get_prog_addr_start(a) < bpf_get_prog_addr_start(b);
362}
363
364static __always_inline int bpf_tree_comp(void *key, struct latch_tree_node *n)
365{
366 unsigned long val = (unsigned long)key;
367 unsigned long symbol_start, symbol_end;
368 const struct bpf_prog_aux *aux;
369
370 aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
371 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
372
373 if (val < symbol_start)
374 return -1;
375 if (val >= symbol_end)
376 return 1;
377
378 return 0;
379}
380
381static const struct latch_tree_ops bpf_tree_ops = {
382 .less = bpf_tree_less,
383 .comp = bpf_tree_comp,
384};
385
386static DEFINE_SPINLOCK(bpf_lock);
387static LIST_HEAD(bpf_kallsyms);
388static struct latch_tree_root bpf_tree __cacheline_aligned;
389
Daniel Borkmann74451e662017-02-16 22:24:50 +0100390static void bpf_prog_ksym_node_add(struct bpf_prog_aux *aux)
391{
392 WARN_ON_ONCE(!list_empty(&aux->ksym_lnode));
393 list_add_tail_rcu(&aux->ksym_lnode, &bpf_kallsyms);
394 latch_tree_insert(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
395}
396
397static void bpf_prog_ksym_node_del(struct bpf_prog_aux *aux)
398{
399 if (list_empty(&aux->ksym_lnode))
400 return;
401
402 latch_tree_erase(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
403 list_del_rcu(&aux->ksym_lnode);
404}
405
406static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp)
407{
408 return fp->jited && !bpf_prog_was_classic(fp);
409}
410
411static bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp)
412{
413 return list_empty(&fp->aux->ksym_lnode) ||
414 fp->aux->ksym_lnode.prev == LIST_POISON2;
415}
416
417void bpf_prog_kallsyms_add(struct bpf_prog *fp)
418{
Daniel Borkmann74451e662017-02-16 22:24:50 +0100419 if (!bpf_prog_kallsyms_candidate(fp) ||
420 !capable(CAP_SYS_ADMIN))
421 return;
422
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200423 spin_lock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100424 bpf_prog_ksym_node_add(fp->aux);
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200425 spin_unlock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100426}
427
428void bpf_prog_kallsyms_del(struct bpf_prog *fp)
429{
Daniel Borkmann74451e662017-02-16 22:24:50 +0100430 if (!bpf_prog_kallsyms_candidate(fp))
431 return;
432
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200433 spin_lock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100434 bpf_prog_ksym_node_del(fp->aux);
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200435 spin_unlock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100436}
437
438static struct bpf_prog *bpf_prog_kallsyms_find(unsigned long addr)
439{
440 struct latch_tree_node *n;
441
442 if (!bpf_jit_kallsyms_enabled())
443 return NULL;
444
445 n = latch_tree_find((void *)addr, &bpf_tree, &bpf_tree_ops);
446 return n ?
447 container_of(n, struct bpf_prog_aux, ksym_tnode)->prog :
448 NULL;
449}
450
451const char *__bpf_address_lookup(unsigned long addr, unsigned long *size,
452 unsigned long *off, char *sym)
453{
454 unsigned long symbol_start, symbol_end;
455 struct bpf_prog *prog;
456 char *ret = NULL;
457
458 rcu_read_lock();
459 prog = bpf_prog_kallsyms_find(addr);
460 if (prog) {
461 bpf_get_prog_addr_region(prog, &symbol_start, &symbol_end);
462 bpf_get_prog_name(prog, sym);
463
464 ret = sym;
465 if (size)
466 *size = symbol_end - symbol_start;
467 if (off)
468 *off = addr - symbol_start;
469 }
470 rcu_read_unlock();
471
472 return ret;
473}
474
475bool is_bpf_text_address(unsigned long addr)
476{
477 bool ret;
478
479 rcu_read_lock();
480 ret = bpf_prog_kallsyms_find(addr) != NULL;
481 rcu_read_unlock();
482
483 return ret;
484}
485
486int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
487 char *sym)
488{
489 unsigned long symbol_start, symbol_end;
490 struct bpf_prog_aux *aux;
491 unsigned int it = 0;
492 int ret = -ERANGE;
493
494 if (!bpf_jit_kallsyms_enabled())
495 return ret;
496
497 rcu_read_lock();
498 list_for_each_entry_rcu(aux, &bpf_kallsyms, ksym_lnode) {
499 if (it++ != symnum)
500 continue;
501
502 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
503 bpf_get_prog_name(aux->prog, sym);
504
505 *value = symbol_start;
506 *type = BPF_SYM_ELF_TYPE;
507
508 ret = 0;
509 break;
510 }
511 rcu_read_unlock();
512
513 return ret;
514}
515
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200516struct bpf_binary_header *
517bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
518 unsigned int alignment,
519 bpf_jit_fill_hole_t bpf_fill_ill_insns)
520{
521 struct bpf_binary_header *hdr;
522 unsigned int size, hole, start;
523
524 /* Most of BPF filters are really small, but if some of them
525 * fill a page, allow at least 128 extra bytes to insert a
526 * random section of illegal instructions.
527 */
528 size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
529 hdr = module_alloc(size);
530 if (hdr == NULL)
531 return NULL;
532
533 /* Fill space with illegal/arch-dep instructions. */
534 bpf_fill_ill_insns(hdr, size);
535
536 hdr->pages = size / PAGE_SIZE;
537 hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
538 PAGE_SIZE - sizeof(*hdr));
Daniel Borkmannb7552e1b2016-05-18 14:14:28 +0200539 start = (get_random_int() % hole) & ~(alignment - 1);
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200540
541 /* Leave a random number of instructions before BPF code. */
542 *image_ptr = &hdr->image[start];
543
544 return hdr;
545}
546
547void bpf_jit_binary_free(struct bpf_binary_header *hdr)
548{
Rusty Russellbe1f2212015-01-20 09:07:05 +1030549 module_memfree(hdr);
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200550}
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200551
Daniel Borkmann74451e662017-02-16 22:24:50 +0100552/* This symbol is only overridden by archs that have different
553 * requirements than the usual eBPF JITs, f.e. when they only
554 * implement cBPF JIT, do not set images read-only, etc.
555 */
556void __weak bpf_jit_free(struct bpf_prog *fp)
557{
558 if (fp->jited) {
559 struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp);
560
561 bpf_jit_binary_unlock_ro(hdr);
562 bpf_jit_binary_free(hdr);
563
564 WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp));
565 }
566
567 bpf_prog_unlock_free(fp);
568}
569
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200570static int bpf_jit_blind_insn(const struct bpf_insn *from,
571 const struct bpf_insn *aux,
572 struct bpf_insn *to_buff)
573{
574 struct bpf_insn *to = to_buff;
Daniel Borkmannb7552e1b2016-05-18 14:14:28 +0200575 u32 imm_rnd = get_random_int();
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200576 s16 off;
577
578 BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG);
579 BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG);
580
581 if (from->imm == 0 &&
582 (from->code == (BPF_ALU | BPF_MOV | BPF_K) ||
583 from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) {
584 *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg);
585 goto out;
586 }
587
588 switch (from->code) {
589 case BPF_ALU | BPF_ADD | BPF_K:
590 case BPF_ALU | BPF_SUB | BPF_K:
591 case BPF_ALU | BPF_AND | BPF_K:
592 case BPF_ALU | BPF_OR | BPF_K:
593 case BPF_ALU | BPF_XOR | BPF_K:
594 case BPF_ALU | BPF_MUL | BPF_K:
595 case BPF_ALU | BPF_MOV | BPF_K:
596 case BPF_ALU | BPF_DIV | BPF_K:
597 case BPF_ALU | BPF_MOD | BPF_K:
598 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
599 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
600 *to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX);
601 break;
602
603 case BPF_ALU64 | BPF_ADD | BPF_K:
604 case BPF_ALU64 | BPF_SUB | BPF_K:
605 case BPF_ALU64 | BPF_AND | BPF_K:
606 case BPF_ALU64 | BPF_OR | BPF_K:
607 case BPF_ALU64 | BPF_XOR | BPF_K:
608 case BPF_ALU64 | BPF_MUL | BPF_K:
609 case BPF_ALU64 | BPF_MOV | BPF_K:
610 case BPF_ALU64 | BPF_DIV | BPF_K:
611 case BPF_ALU64 | BPF_MOD | BPF_K:
612 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
613 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
614 *to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX);
615 break;
616
617 case BPF_JMP | BPF_JEQ | BPF_K:
618 case BPF_JMP | BPF_JNE | BPF_K:
619 case BPF_JMP | BPF_JGT | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200620 case BPF_JMP | BPF_JLT | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200621 case BPF_JMP | BPF_JGE | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200622 case BPF_JMP | BPF_JLE | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200623 case BPF_JMP | BPF_JSGT | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200624 case BPF_JMP | BPF_JSLT | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200625 case BPF_JMP | BPF_JSGE | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200626 case BPF_JMP | BPF_JSLE | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200627 case BPF_JMP | BPF_JSET | BPF_K:
628 /* Accommodate for extra offset in case of a backjump. */
629 off = from->off;
630 if (off < 0)
631 off -= 2;
632 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
633 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
634 *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off);
635 break;
636
637 case BPF_LD | BPF_ABS | BPF_W:
638 case BPF_LD | BPF_ABS | BPF_H:
639 case BPF_LD | BPF_ABS | BPF_B:
640 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
641 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
642 *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
643 break;
644
645 case BPF_LD | BPF_IND | BPF_W:
646 case BPF_LD | BPF_IND | BPF_H:
647 case BPF_LD | BPF_IND | BPF_B:
648 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
649 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
650 *to++ = BPF_ALU32_REG(BPF_ADD, BPF_REG_AX, from->src_reg);
651 *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
652 break;
653
654 case BPF_LD | BPF_IMM | BPF_DW:
655 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm);
656 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
657 *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
658 *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX);
659 break;
660 case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */
661 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm);
662 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
663 *to++ = BPF_ALU64_REG(BPF_OR, aux[0].dst_reg, BPF_REG_AX);
664 break;
665
666 case BPF_ST | BPF_MEM | BPF_DW:
667 case BPF_ST | BPF_MEM | BPF_W:
668 case BPF_ST | BPF_MEM | BPF_H:
669 case BPF_ST | BPF_MEM | BPF_B:
670 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
671 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
672 *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off);
673 break;
674 }
675out:
676 return to - to_buff;
677}
678
679static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other,
680 gfp_t gfp_extra_flags)
681{
Michal Hocko19809c22017-05-08 15:57:44 -0700682 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200683 struct bpf_prog *fp;
684
685 fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags, PAGE_KERNEL);
686 if (fp != NULL) {
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200687 /* aux->prog still points to the fp_other one, so
688 * when promoting the clone to the real program,
689 * this still needs to be adapted.
690 */
691 memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE);
692 }
693
694 return fp;
695}
696
697static void bpf_prog_clone_free(struct bpf_prog *fp)
698{
699 /* aux was stolen by the other clone, so we cannot free
700 * it from this path! It will be freed eventually by the
701 * other program on release.
702 *
703 * At this point, we don't need a deferred release since
704 * clone is guaranteed to not be locked.
705 */
706 fp->aux = NULL;
707 __bpf_prog_free(fp);
708}
709
710void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other)
711{
712 /* We have to repoint aux->prog to self, as we don't
713 * know whether fp here is the clone or the original.
714 */
715 fp->aux->prog = fp;
716 bpf_prog_clone_free(fp_other);
717}
718
719struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
720{
721 struct bpf_insn insn_buff[16], aux[2];
722 struct bpf_prog *clone, *tmp;
723 int insn_delta, insn_cnt;
724 struct bpf_insn *insn;
725 int i, rewritten;
726
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -0800727 if (!bpf_jit_blinding_enabled(prog) || prog->blinded)
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200728 return prog;
729
730 clone = bpf_prog_clone_create(prog, GFP_USER);
731 if (!clone)
732 return ERR_PTR(-ENOMEM);
733
734 insn_cnt = clone->len;
735 insn = clone->insnsi;
736
737 for (i = 0; i < insn_cnt; i++, insn++) {
738 /* We temporarily need to hold the original ld64 insn
739 * so that we can still access the first part in the
740 * second blinding run.
741 */
742 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) &&
743 insn[1].code == 0)
744 memcpy(aux, insn, sizeof(aux));
745
746 rewritten = bpf_jit_blind_insn(insn, aux, insn_buff);
747 if (!rewritten)
748 continue;
749
750 tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);
751 if (!tmp) {
752 /* Patching may have repointed aux->prog during
753 * realloc from the original one, so we need to
754 * fix it up here on error.
755 */
756 bpf_jit_prog_release_other(prog, clone);
757 return ERR_PTR(-ENOMEM);
758 }
759
760 clone = tmp;
761 insn_delta = rewritten - 1;
762
763 /* Walk new program and skip insns we just inserted. */
764 insn = clone->insnsi + i + insn_delta;
765 insn_cnt += insn_delta;
766 i += insn_delta;
767 }
768
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -0800769 clone->blinded = 1;
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200770 return clone;
771}
Daniel Borkmannb954d832014-09-10 15:01:02 +0200772#endif /* CONFIG_BPF_JIT */
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200773
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700774/* Base function for offset calculation. Needs to go into .text section,
775 * therefore keeping it non-static as well; will also be used by JITs
Daniel Borkmann7105e822017-12-20 13:42:57 +0100776 * anyway later on, so do not let the compiler omit it. This also needs
777 * to go into kallsyms for correlation from e.g. bpftool, so naming
778 * must not change.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700779 */
780noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
781{
782 return 0;
783}
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -0700784EXPORT_SYMBOL_GPL(__bpf_call_base);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700785
Daniel Borkmann5e581da2018-01-26 23:33:38 +0100786/* All UAPI available opcodes. */
787#define BPF_INSN_MAP(INSN_2, INSN_3) \
788 /* 32 bit ALU operations. */ \
789 /* Register based. */ \
790 INSN_3(ALU, ADD, X), \
791 INSN_3(ALU, SUB, X), \
792 INSN_3(ALU, AND, X), \
793 INSN_3(ALU, OR, X), \
794 INSN_3(ALU, LSH, X), \
795 INSN_3(ALU, RSH, X), \
796 INSN_3(ALU, XOR, X), \
797 INSN_3(ALU, MUL, X), \
798 INSN_3(ALU, MOV, X), \
799 INSN_3(ALU, DIV, X), \
800 INSN_3(ALU, MOD, X), \
801 INSN_2(ALU, NEG), \
802 INSN_3(ALU, END, TO_BE), \
803 INSN_3(ALU, END, TO_LE), \
804 /* Immediate based. */ \
805 INSN_3(ALU, ADD, K), \
806 INSN_3(ALU, SUB, K), \
807 INSN_3(ALU, AND, K), \
808 INSN_3(ALU, OR, K), \
809 INSN_3(ALU, LSH, K), \
810 INSN_3(ALU, RSH, K), \
811 INSN_3(ALU, XOR, K), \
812 INSN_3(ALU, MUL, K), \
813 INSN_3(ALU, MOV, K), \
814 INSN_3(ALU, DIV, K), \
815 INSN_3(ALU, MOD, K), \
816 /* 64 bit ALU operations. */ \
817 /* Register based. */ \
818 INSN_3(ALU64, ADD, X), \
819 INSN_3(ALU64, SUB, X), \
820 INSN_3(ALU64, AND, X), \
821 INSN_3(ALU64, OR, X), \
822 INSN_3(ALU64, LSH, X), \
823 INSN_3(ALU64, RSH, X), \
824 INSN_3(ALU64, XOR, X), \
825 INSN_3(ALU64, MUL, X), \
826 INSN_3(ALU64, MOV, X), \
827 INSN_3(ALU64, ARSH, X), \
828 INSN_3(ALU64, DIV, X), \
829 INSN_3(ALU64, MOD, X), \
830 INSN_2(ALU64, NEG), \
831 /* Immediate based. */ \
832 INSN_3(ALU64, ADD, K), \
833 INSN_3(ALU64, SUB, K), \
834 INSN_3(ALU64, AND, K), \
835 INSN_3(ALU64, OR, K), \
836 INSN_3(ALU64, LSH, K), \
837 INSN_3(ALU64, RSH, K), \
838 INSN_3(ALU64, XOR, K), \
839 INSN_3(ALU64, MUL, K), \
840 INSN_3(ALU64, MOV, K), \
841 INSN_3(ALU64, ARSH, K), \
842 INSN_3(ALU64, DIV, K), \
843 INSN_3(ALU64, MOD, K), \
844 /* Call instruction. */ \
845 INSN_2(JMP, CALL), \
846 /* Exit instruction. */ \
847 INSN_2(JMP, EXIT), \
848 /* Jump instructions. */ \
849 /* Register based. */ \
850 INSN_3(JMP, JEQ, X), \
851 INSN_3(JMP, JNE, X), \
852 INSN_3(JMP, JGT, X), \
853 INSN_3(JMP, JLT, X), \
854 INSN_3(JMP, JGE, X), \
855 INSN_3(JMP, JLE, X), \
856 INSN_3(JMP, JSGT, X), \
857 INSN_3(JMP, JSLT, X), \
858 INSN_3(JMP, JSGE, X), \
859 INSN_3(JMP, JSLE, X), \
860 INSN_3(JMP, JSET, X), \
861 /* Immediate based. */ \
862 INSN_3(JMP, JEQ, K), \
863 INSN_3(JMP, JNE, K), \
864 INSN_3(JMP, JGT, K), \
865 INSN_3(JMP, JLT, K), \
866 INSN_3(JMP, JGE, K), \
867 INSN_3(JMP, JLE, K), \
868 INSN_3(JMP, JSGT, K), \
869 INSN_3(JMP, JSLT, K), \
870 INSN_3(JMP, JSGE, K), \
871 INSN_3(JMP, JSLE, K), \
872 INSN_3(JMP, JSET, K), \
873 INSN_2(JMP, JA), \
874 /* Store instructions. */ \
875 /* Register based. */ \
876 INSN_3(STX, MEM, B), \
877 INSN_3(STX, MEM, H), \
878 INSN_3(STX, MEM, W), \
879 INSN_3(STX, MEM, DW), \
880 INSN_3(STX, XADD, W), \
881 INSN_3(STX, XADD, DW), \
882 /* Immediate based. */ \
883 INSN_3(ST, MEM, B), \
884 INSN_3(ST, MEM, H), \
885 INSN_3(ST, MEM, W), \
886 INSN_3(ST, MEM, DW), \
887 /* Load instructions. */ \
888 /* Register based. */ \
889 INSN_3(LDX, MEM, B), \
890 INSN_3(LDX, MEM, H), \
891 INSN_3(LDX, MEM, W), \
892 INSN_3(LDX, MEM, DW), \
893 /* Immediate based. */ \
894 INSN_3(LD, IMM, DW), \
895 /* Misc (old cBPF carry-over). */ \
896 INSN_3(LD, ABS, B), \
897 INSN_3(LD, ABS, H), \
898 INSN_3(LD, ABS, W), \
899 INSN_3(LD, IND, B), \
900 INSN_3(LD, IND, H), \
901 INSN_3(LD, IND, W)
902
903bool bpf_opcode_in_insntable(u8 code)
904{
905#define BPF_INSN_2_TBL(x, y) [BPF_##x | BPF_##y] = true
906#define BPF_INSN_3_TBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = true
907 static const bool public_insntable[256] = {
908 [0 ... 255] = false,
909 /* Now overwrite non-defaults ... */
910 BPF_INSN_MAP(BPF_INSN_2_TBL, BPF_INSN_3_TBL),
911 };
912#undef BPF_INSN_3_TBL
913#undef BPF_INSN_2_TBL
914 return public_insntable[code];
915}
916
Alexei Starovoitov290af862018-01-09 10:04:29 -0800917#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700918/**
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700919 * __bpf_prog_run - run eBPF program on a given context
920 * @ctx: is the data we are operating on
921 * @insn: is the array of eBPF instructions
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700922 *
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700923 * Decode and execute eBPF instructions.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700924 */
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800925static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700926{
Alexei Starovoitovf696b8f2017-05-30 13:31:28 -0700927 u64 tmp;
Daniel Borkmann5e581da2018-01-26 23:33:38 +0100928#define BPF_INSN_2_LBL(x, y) [BPF_##x | BPF_##y] = &&x##_##y
929#define BPF_INSN_3_LBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = &&x##_##y##_##z
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700930 static const void *jumptable[256] = {
931 [0 ... 255] = &&default_label,
932 /* Now overwrite non-defaults ... */
Daniel Borkmann5e581da2018-01-26 23:33:38 +0100933 BPF_INSN_MAP(BPF_INSN_2_LBL, BPF_INSN_3_LBL),
934 /* Non-UAPI available opcodes. */
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800935 [BPF_JMP | BPF_CALL_ARGS] = &&JMP_CALL_ARGS,
Alexei Starovoitov71189fa2017-05-30 13:31:27 -0700936 [BPF_JMP | BPF_TAIL_CALL] = &&JMP_TAIL_CALL,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700937 };
Daniel Borkmann5e581da2018-01-26 23:33:38 +0100938#undef BPF_INSN_3_LBL
939#undef BPF_INSN_2_LBL
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700940 u32 tail_call_cnt = 0;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700941 void *ptr;
942 int off;
943
944#define CONT ({ insn++; goto select_insn; })
945#define CONT_JMP ({ insn++; goto select_insn; })
946
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700947select_insn:
948 goto *jumptable[insn->code];
949
950 /* ALU */
951#define ALU(OPCODE, OP) \
952 ALU64_##OPCODE##_X: \
953 DST = DST OP SRC; \
954 CONT; \
955 ALU_##OPCODE##_X: \
956 DST = (u32) DST OP (u32) SRC; \
957 CONT; \
958 ALU64_##OPCODE##_K: \
959 DST = DST OP IMM; \
960 CONT; \
961 ALU_##OPCODE##_K: \
962 DST = (u32) DST OP (u32) IMM; \
963 CONT;
964
965 ALU(ADD, +)
966 ALU(SUB, -)
967 ALU(AND, &)
968 ALU(OR, |)
969 ALU(LSH, <<)
970 ALU(RSH, >>)
971 ALU(XOR, ^)
972 ALU(MUL, *)
973#undef ALU
974 ALU_NEG:
975 DST = (u32) -DST;
976 CONT;
977 ALU64_NEG:
978 DST = -DST;
979 CONT;
980 ALU_MOV_X:
981 DST = (u32) SRC;
982 CONT;
983 ALU_MOV_K:
984 DST = (u32) IMM;
985 CONT;
986 ALU64_MOV_X:
987 DST = SRC;
988 CONT;
989 ALU64_MOV_K:
990 DST = IMM;
991 CONT;
Alexei Starovoitov02ab6952014-09-04 22:17:17 -0700992 LD_IMM_DW:
993 DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
994 insn++;
995 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700996 ALU64_ARSH_X:
997 (*(s64 *) &DST) >>= SRC;
998 CONT;
999 ALU64_ARSH_K:
1000 (*(s64 *) &DST) >>= IMM;
1001 CONT;
1002 ALU64_MOD_X:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -07001003 div64_u64_rem(DST, SRC, &tmp);
1004 DST = tmp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001005 CONT;
1006 ALU_MOD_X:
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001007 tmp = (u32) DST;
1008 DST = do_div(tmp, (u32) SRC);
1009 CONT;
1010 ALU64_MOD_K:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -07001011 div64_u64_rem(DST, IMM, &tmp);
1012 DST = tmp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001013 CONT;
1014 ALU_MOD_K:
1015 tmp = (u32) DST;
1016 DST = do_div(tmp, (u32) IMM);
1017 CONT;
1018 ALU64_DIV_X:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -07001019 DST = div64_u64(DST, SRC);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001020 CONT;
1021 ALU_DIV_X:
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001022 tmp = (u32) DST;
1023 do_div(tmp, (u32) SRC);
1024 DST = (u32) tmp;
1025 CONT;
1026 ALU64_DIV_K:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -07001027 DST = div64_u64(DST, IMM);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001028 CONT;
1029 ALU_DIV_K:
1030 tmp = (u32) DST;
1031 do_div(tmp, (u32) IMM);
1032 DST = (u32) tmp;
1033 CONT;
1034 ALU_END_TO_BE:
1035 switch (IMM) {
1036 case 16:
1037 DST = (__force u16) cpu_to_be16(DST);
1038 break;
1039 case 32:
1040 DST = (__force u32) cpu_to_be32(DST);
1041 break;
1042 case 64:
1043 DST = (__force u64) cpu_to_be64(DST);
1044 break;
1045 }
1046 CONT;
1047 ALU_END_TO_LE:
1048 switch (IMM) {
1049 case 16:
1050 DST = (__force u16) cpu_to_le16(DST);
1051 break;
1052 case 32:
1053 DST = (__force u32) cpu_to_le32(DST);
1054 break;
1055 case 64:
1056 DST = (__force u64) cpu_to_le64(DST);
1057 break;
1058 }
1059 CONT;
1060
1061 /* CALL */
1062 JMP_CALL:
1063 /* Function call scratches BPF_R1-BPF_R5 registers,
1064 * preserves BPF_R6-BPF_R9, and stores return value
1065 * into BPF_R0.
1066 */
1067 BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
1068 BPF_R4, BPF_R5);
1069 CONT;
1070
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001071 JMP_CALL_ARGS:
1072 BPF_R0 = (__bpf_call_base_args + insn->imm)(BPF_R1, BPF_R2,
1073 BPF_R3, BPF_R4,
1074 BPF_R5,
1075 insn + insn->off + 1);
1076 CONT;
1077
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001078 JMP_TAIL_CALL: {
1079 struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
1080 struct bpf_array *array = container_of(map, struct bpf_array, map);
1081 struct bpf_prog *prog;
Alexei Starovoitov90caccd2017-10-03 15:37:20 -07001082 u32 index = BPF_R3;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001083
1084 if (unlikely(index >= array->map.max_entries))
1085 goto out;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001086 if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
1087 goto out;
1088
1089 tail_call_cnt++;
1090
Wang Nan2a36f0b2015-08-06 07:02:33 +00001091 prog = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +02001092 if (!prog)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001093 goto out;
1094
Daniel Borkmannc4675f92015-07-13 20:49:32 +02001095 /* ARG1 at this point is guaranteed to point to CTX from
1096 * the verifier side due to the fact that the tail call is
1097 * handeled like a helper, that is, bpf_tail_call_proto,
1098 * where arg1_type is ARG_PTR_TO_CTX.
1099 */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001100 insn = prog->insnsi;
1101 goto select_insn;
1102out:
1103 CONT;
1104 }
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001105 /* JMP */
1106 JMP_JA:
1107 insn += insn->off;
1108 CONT;
1109 JMP_JEQ_X:
1110 if (DST == SRC) {
1111 insn += insn->off;
1112 CONT_JMP;
1113 }
1114 CONT;
1115 JMP_JEQ_K:
1116 if (DST == IMM) {
1117 insn += insn->off;
1118 CONT_JMP;
1119 }
1120 CONT;
1121 JMP_JNE_X:
1122 if (DST != SRC) {
1123 insn += insn->off;
1124 CONT_JMP;
1125 }
1126 CONT;
1127 JMP_JNE_K:
1128 if (DST != IMM) {
1129 insn += insn->off;
1130 CONT_JMP;
1131 }
1132 CONT;
1133 JMP_JGT_X:
1134 if (DST > SRC) {
1135 insn += insn->off;
1136 CONT_JMP;
1137 }
1138 CONT;
1139 JMP_JGT_K:
1140 if (DST > IMM) {
1141 insn += insn->off;
1142 CONT_JMP;
1143 }
1144 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001145 JMP_JLT_X:
1146 if (DST < SRC) {
1147 insn += insn->off;
1148 CONT_JMP;
1149 }
1150 CONT;
1151 JMP_JLT_K:
1152 if (DST < IMM) {
1153 insn += insn->off;
1154 CONT_JMP;
1155 }
1156 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001157 JMP_JGE_X:
1158 if (DST >= SRC) {
1159 insn += insn->off;
1160 CONT_JMP;
1161 }
1162 CONT;
1163 JMP_JGE_K:
1164 if (DST >= IMM) {
1165 insn += insn->off;
1166 CONT_JMP;
1167 }
1168 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001169 JMP_JLE_X:
1170 if (DST <= SRC) {
1171 insn += insn->off;
1172 CONT_JMP;
1173 }
1174 CONT;
1175 JMP_JLE_K:
1176 if (DST <= IMM) {
1177 insn += insn->off;
1178 CONT_JMP;
1179 }
1180 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001181 JMP_JSGT_X:
1182 if (((s64) DST) > ((s64) SRC)) {
1183 insn += insn->off;
1184 CONT_JMP;
1185 }
1186 CONT;
1187 JMP_JSGT_K:
1188 if (((s64) DST) > ((s64) IMM)) {
1189 insn += insn->off;
1190 CONT_JMP;
1191 }
1192 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001193 JMP_JSLT_X:
1194 if (((s64) DST) < ((s64) SRC)) {
1195 insn += insn->off;
1196 CONT_JMP;
1197 }
1198 CONT;
1199 JMP_JSLT_K:
1200 if (((s64) DST) < ((s64) IMM)) {
1201 insn += insn->off;
1202 CONT_JMP;
1203 }
1204 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001205 JMP_JSGE_X:
1206 if (((s64) DST) >= ((s64) SRC)) {
1207 insn += insn->off;
1208 CONT_JMP;
1209 }
1210 CONT;
1211 JMP_JSGE_K:
1212 if (((s64) DST) >= ((s64) IMM)) {
1213 insn += insn->off;
1214 CONT_JMP;
1215 }
1216 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001217 JMP_JSLE_X:
1218 if (((s64) DST) <= ((s64) SRC)) {
1219 insn += insn->off;
1220 CONT_JMP;
1221 }
1222 CONT;
1223 JMP_JSLE_K:
1224 if (((s64) DST) <= ((s64) IMM)) {
1225 insn += insn->off;
1226 CONT_JMP;
1227 }
1228 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001229 JMP_JSET_X:
1230 if (DST & SRC) {
1231 insn += insn->off;
1232 CONT_JMP;
1233 }
1234 CONT;
1235 JMP_JSET_K:
1236 if (DST & IMM) {
1237 insn += insn->off;
1238 CONT_JMP;
1239 }
1240 CONT;
1241 JMP_EXIT:
1242 return BPF_R0;
1243
1244 /* STX and ST and LDX*/
1245#define LDST(SIZEOP, SIZE) \
1246 STX_MEM_##SIZEOP: \
1247 *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \
1248 CONT; \
1249 ST_MEM_##SIZEOP: \
1250 *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \
1251 CONT; \
1252 LDX_MEM_##SIZEOP: \
1253 DST = *(SIZE *)(unsigned long) (SRC + insn->off); \
1254 CONT;
1255
1256 LDST(B, u8)
1257 LDST(H, u16)
1258 LDST(W, u32)
1259 LDST(DW, u64)
1260#undef LDST
1261 STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
1262 atomic_add((u32) SRC, (atomic_t *)(unsigned long)
1263 (DST + insn->off));
1264 CONT;
1265 STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
1266 atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
1267 (DST + insn->off));
1268 CONT;
1269 LD_ABS_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + imm32)) */
1270 off = IMM;
1271load_word:
Johannes Berg96a94cc2017-04-11 12:10:58 +02001272 /* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are only
1273 * appearing in the programs where ctx == skb
1274 * (see may_access_skb() in the verifier). All programs
1275 * keep 'ctx' in regs[BPF_REG_CTX] == BPF_R6,
1276 * bpf_convert_filter() saves it in BPF_R6, internal BPF
1277 * verifier will check that BPF_R6 == ctx.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001278 *
1279 * BPF_ABS and BPF_IND are wrappers of function calls,
1280 * so they scratch BPF_R1-BPF_R5 registers, preserve
1281 * BPF_R6-BPF_R9, and store return value into BPF_R0.
1282 *
1283 * Implicit input:
1284 * ctx == skb == BPF_R6 == CTX
1285 *
1286 * Explicit input:
1287 * SRC == any register
1288 * IMM == 32-bit immediate
1289 *
1290 * Output:
1291 * BPF_R0 - 8/16/32-bit skb data converted to cpu endianness
1292 */
1293
1294 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 4, &tmp);
1295 if (likely(ptr != NULL)) {
1296 BPF_R0 = get_unaligned_be32(ptr);
1297 CONT;
1298 }
1299
1300 return 0;
1301 LD_ABS_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + imm32)) */
1302 off = IMM;
1303load_half:
1304 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 2, &tmp);
1305 if (likely(ptr != NULL)) {
1306 BPF_R0 = get_unaligned_be16(ptr);
1307 CONT;
1308 }
1309
1310 return 0;
1311 LD_ABS_B: /* BPF_R0 = *(u8 *) (skb->data + imm32) */
1312 off = IMM;
1313load_byte:
1314 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 1, &tmp);
1315 if (likely(ptr != NULL)) {
1316 BPF_R0 = *(u8 *)ptr;
1317 CONT;
1318 }
1319
1320 return 0;
1321 LD_IND_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + src_reg + imm32)) */
1322 off = IMM + SRC;
1323 goto load_word;
1324 LD_IND_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + src_reg + imm32)) */
1325 off = IMM + SRC;
1326 goto load_half;
1327 LD_IND_B: /* BPF_R0 = *(u8 *) (skb->data + src_reg + imm32) */
1328 off = IMM + SRC;
1329 goto load_byte;
1330
1331 default_label:
Daniel Borkmann5e581da2018-01-26 23:33:38 +01001332 /* If we ever reach this, we have a bug somewhere. Die hard here
1333 * instead of just returning 0; we could be somewhere in a subprog,
1334 * so execution could continue otherwise which we do /not/ want.
1335 *
1336 * Note, verifier whitelists all opcodes in bpf_opcode_in_insntable().
1337 */
1338 pr_warn("BPF interpreter: unknown opcode %02x\n", insn->code);
1339 BUG_ON(1);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001340 return 0;
1341}
Alexei Starovoitovf696b8f2017-05-30 13:31:28 -07001342STACK_FRAME_NON_STANDARD(___bpf_prog_run); /* jump table */
1343
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001344#define PROG_NAME(stack_size) __bpf_prog_run##stack_size
1345#define DEFINE_BPF_PROG_RUN(stack_size) \
1346static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \
1347{ \
1348 u64 stack[stack_size / sizeof(u64)]; \
1349 u64 regs[MAX_BPF_REG]; \
1350\
1351 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1352 ARG1 = (u64) (unsigned long) ctx; \
1353 return ___bpf_prog_run(regs, insn, stack); \
Alexei Starovoitovf696b8f2017-05-30 13:31:28 -07001354}
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001355
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001356#define PROG_NAME_ARGS(stack_size) __bpf_prog_run_args##stack_size
1357#define DEFINE_BPF_PROG_RUN_ARGS(stack_size) \
1358static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \
1359 const struct bpf_insn *insn) \
1360{ \
1361 u64 stack[stack_size / sizeof(u64)]; \
1362 u64 regs[MAX_BPF_REG]; \
1363\
1364 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1365 BPF_R1 = r1; \
1366 BPF_R2 = r2; \
1367 BPF_R3 = r3; \
1368 BPF_R4 = r4; \
1369 BPF_R5 = r5; \
1370 return ___bpf_prog_run(regs, insn, stack); \
1371}
1372
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001373#define EVAL1(FN, X) FN(X)
1374#define EVAL2(FN, X, Y...) FN(X) EVAL1(FN, Y)
1375#define EVAL3(FN, X, Y...) FN(X) EVAL2(FN, Y)
1376#define EVAL4(FN, X, Y...) FN(X) EVAL3(FN, Y)
1377#define EVAL5(FN, X, Y...) FN(X) EVAL4(FN, Y)
1378#define EVAL6(FN, X, Y...) FN(X) EVAL5(FN, Y)
1379
1380EVAL6(DEFINE_BPF_PROG_RUN, 32, 64, 96, 128, 160, 192);
1381EVAL6(DEFINE_BPF_PROG_RUN, 224, 256, 288, 320, 352, 384);
1382EVAL4(DEFINE_BPF_PROG_RUN, 416, 448, 480, 512);
1383
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001384EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 32, 64, 96, 128, 160, 192);
1385EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 224, 256, 288, 320, 352, 384);
1386EVAL4(DEFINE_BPF_PROG_RUN_ARGS, 416, 448, 480, 512);
1387
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001388#define PROG_NAME_LIST(stack_size) PROG_NAME(stack_size),
1389
1390static unsigned int (*interpreters[])(const void *ctx,
1391 const struct bpf_insn *insn) = {
1392EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1393EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1394EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1395};
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001396#undef PROG_NAME_LIST
1397#define PROG_NAME_LIST(stack_size) PROG_NAME_ARGS(stack_size),
1398static u64 (*interpreters_args[])(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5,
1399 const struct bpf_insn *insn) = {
1400EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1401EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1402EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1403};
1404#undef PROG_NAME_LIST
1405
1406void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth)
1407{
1408 stack_depth = max_t(u32, stack_depth, 1);
1409 insn->off = (s16) insn->imm;
1410 insn->imm = interpreters_args[(round_up(stack_depth, 32) / 32) - 1] -
1411 __bpf_call_base_args;
1412 insn->code = BPF_JMP | BPF_CALL_ARGS;
1413}
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001414
Alexei Starovoitov290af862018-01-09 10:04:29 -08001415#else
Daniel Borkmannfa9dd592018-01-20 01:24:33 +01001416static unsigned int __bpf_prog_ret0_warn(const void *ctx,
1417 const struct bpf_insn *insn)
Alexei Starovoitov290af862018-01-09 10:04:29 -08001418{
Daniel Borkmannfa9dd592018-01-20 01:24:33 +01001419 /* If this handler ever gets executed, then BPF_JIT_ALWAYS_ON
1420 * is not working properly, so warn about it!
1421 */
1422 WARN_ON_ONCE(1);
Alexei Starovoitov290af862018-01-09 10:04:29 -08001423 return 0;
1424}
1425#endif
1426
Daniel Borkmann3324b582015-05-29 23:23:07 +02001427bool bpf_prog_array_compatible(struct bpf_array *array,
1428 const struct bpf_prog *fp)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001429{
Josef Bacik9802d862017-12-11 11:36:48 -05001430 if (fp->kprobe_override)
1431 return false;
1432
Daniel Borkmann3324b582015-05-29 23:23:07 +02001433 if (!array->owner_prog_type) {
1434 /* There's no owner yet where we could check for
1435 * compatibility.
1436 */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001437 array->owner_prog_type = fp->type;
1438 array->owner_jited = fp->jited;
Daniel Borkmann3324b582015-05-29 23:23:07 +02001439
1440 return true;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001441 }
Daniel Borkmann3324b582015-05-29 23:23:07 +02001442
1443 return array->owner_prog_type == fp->type &&
1444 array->owner_jited == fp->jited;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001445}
1446
Daniel Borkmann3324b582015-05-29 23:23:07 +02001447static int bpf_check_tail_call(const struct bpf_prog *fp)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001448{
1449 struct bpf_prog_aux *aux = fp->aux;
1450 int i;
1451
1452 for (i = 0; i < aux->used_map_cnt; i++) {
Daniel Borkmann3324b582015-05-29 23:23:07 +02001453 struct bpf_map *map = aux->used_maps[i];
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001454 struct bpf_array *array;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001455
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001456 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1457 continue;
Daniel Borkmann3324b582015-05-29 23:23:07 +02001458
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001459 array = container_of(map, struct bpf_array, map);
1460 if (!bpf_prog_array_compatible(array, fp))
1461 return -EINVAL;
1462 }
1463
1464 return 0;
1465}
1466
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001467/**
Daniel Borkmann3324b582015-05-29 23:23:07 +02001468 * bpf_prog_select_runtime - select exec runtime for BPF program
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001469 * @fp: bpf_prog populated with internal BPF program
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001470 * @err: pointer to error variable
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001471 *
Daniel Borkmann3324b582015-05-29 23:23:07 +02001472 * Try to JIT eBPF program, if JIT is not available, use interpreter.
1473 * The BPF program will be executed via BPF_PROG_RUN() macro.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001474 */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001475struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001476{
Alexei Starovoitov290af862018-01-09 10:04:29 -08001477#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Martin KaFai Lau8007e402017-06-28 10:41:24 -07001478 u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1);
1479
1480 fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1];
Alexei Starovoitov290af862018-01-09 10:04:29 -08001481#else
Daniel Borkmannfa9dd592018-01-20 01:24:33 +01001482 fp->bpf_func = __bpf_prog_ret0_warn;
Alexei Starovoitov290af862018-01-09 10:04:29 -08001483#endif
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001484
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001485 /* eBPF JITs can rewrite the program in case constant
1486 * blinding is active. However, in case of error during
1487 * blinding, bpf_int_jit_compile() must always return a
1488 * valid program, which in this case would simply not
1489 * be JITed, but falls back to the interpreter.
1490 */
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001491 if (!bpf_prog_is_dev_bound(fp->aux)) {
1492 fp = bpf_int_jit_compile(fp);
Alexei Starovoitov290af862018-01-09 10:04:29 -08001493#ifdef CONFIG_BPF_JIT_ALWAYS_ON
1494 if (!fp->jited) {
1495 *err = -ENOTSUPP;
1496 return fp;
1497 }
1498#endif
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001499 } else {
1500 *err = bpf_prog_offload_compile(fp);
1501 if (*err)
1502 return fp;
1503 }
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001504 bpf_prog_lock_ro(fp);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001505
Daniel Borkmann3324b582015-05-29 23:23:07 +02001506 /* The tail call compatibility check can only be done at
1507 * this late stage as we need to determine, if we deal
1508 * with JITed or non JITed program concatenations and not
1509 * all eBPF JITs might immediately support all features.
1510 */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001511 *err = bpf_check_tail_call(fp);
1512
1513 return fp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001514}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001515EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001516
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001517static unsigned int __bpf_prog_ret1(const void *ctx,
1518 const struct bpf_insn *insn)
1519{
1520 return 1;
1521}
1522
1523static struct bpf_prog_dummy {
1524 struct bpf_prog prog;
1525} dummy_bpf_prog = {
1526 .prog = {
1527 .bpf_func = __bpf_prog_ret1,
1528 },
1529};
1530
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001531/* to avoid allocating empty bpf_prog_array for cgroups that
1532 * don't have bpf program attached use one global 'empty_prog_array'
1533 * It will not be modified the caller of bpf_prog_array_alloc()
1534 * (since caller requested prog_cnt == 0)
1535 * that pointer should be 'freed' by bpf_prog_array_free()
1536 */
1537static struct {
1538 struct bpf_prog_array hdr;
1539 struct bpf_prog *null_prog;
1540} empty_prog_array = {
1541 .null_prog = NULL,
1542};
1543
1544struct bpf_prog_array __rcu *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags)
1545{
1546 if (prog_cnt)
1547 return kzalloc(sizeof(struct bpf_prog_array) +
1548 sizeof(struct bpf_prog *) * (prog_cnt + 1),
1549 flags);
1550
1551 return &empty_prog_array.hdr;
1552}
1553
1554void bpf_prog_array_free(struct bpf_prog_array __rcu *progs)
1555{
1556 if (!progs ||
1557 progs == (struct bpf_prog_array __rcu *)&empty_prog_array.hdr)
1558 return;
1559 kfree_rcu(progs, rcu);
1560}
1561
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001562int bpf_prog_array_length(struct bpf_prog_array __rcu *progs)
1563{
1564 struct bpf_prog **prog;
1565 u32 cnt = 0;
1566
1567 rcu_read_lock();
1568 prog = rcu_dereference(progs)->progs;
1569 for (; *prog; prog++)
Yonghong Songc8c088b2017-11-30 13:47:54 -08001570 if (*prog != &dummy_bpf_prog.prog)
1571 cnt++;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001572 rcu_read_unlock();
1573 return cnt;
1574}
1575
Yonghong Song3a38bb92018-04-10 09:37:32 -07001576static bool bpf_prog_array_copy_core(struct bpf_prog **prog,
1577 u32 *prog_ids,
1578 u32 request_cnt)
1579{
1580 int i = 0;
1581
1582 for (; *prog; prog++) {
1583 if (*prog == &dummy_bpf_prog.prog)
1584 continue;
1585 prog_ids[i] = (*prog)->aux->id;
1586 if (++i == request_cnt) {
1587 prog++;
1588 break;
1589 }
1590 }
1591
1592 return !!(*prog);
1593}
1594
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001595int bpf_prog_array_copy_to_user(struct bpf_prog_array __rcu *progs,
1596 __u32 __user *prog_ids, u32 cnt)
1597{
1598 struct bpf_prog **prog;
Alexei Starovoitov09112872018-02-02 15:14:05 -08001599 unsigned long err = 0;
Alexei Starovoitov09112872018-02-02 15:14:05 -08001600 bool nospc;
Yonghong Song3a38bb92018-04-10 09:37:32 -07001601 u32 *ids;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001602
Alexei Starovoitov09112872018-02-02 15:14:05 -08001603 /* users of this function are doing:
1604 * cnt = bpf_prog_array_length();
1605 * if (cnt > 0)
1606 * bpf_prog_array_copy_to_user(..., cnt);
1607 * so below kcalloc doesn't need extra cnt > 0 check, but
1608 * bpf_prog_array_length() releases rcu lock and
1609 * prog array could have been swapped with empty or larger array,
1610 * so always copy 'cnt' prog_ids to the user.
1611 * In a rare race the user will see zero prog_ids
1612 */
Daniel Borkmann9c481b92018-02-14 15:31:00 +01001613 ids = kcalloc(cnt, sizeof(u32), GFP_USER | __GFP_NOWARN);
Alexei Starovoitov09112872018-02-02 15:14:05 -08001614 if (!ids)
1615 return -ENOMEM;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001616 rcu_read_lock();
1617 prog = rcu_dereference(progs)->progs;
Yonghong Song3a38bb92018-04-10 09:37:32 -07001618 nospc = bpf_prog_array_copy_core(prog, ids, cnt);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001619 rcu_read_unlock();
Alexei Starovoitov09112872018-02-02 15:14:05 -08001620 err = copy_to_user(prog_ids, ids, cnt * sizeof(u32));
1621 kfree(ids);
1622 if (err)
1623 return -EFAULT;
1624 if (nospc)
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001625 return -ENOSPC;
1626 return 0;
1627}
1628
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001629void bpf_prog_array_delete_safe(struct bpf_prog_array __rcu *progs,
1630 struct bpf_prog *old_prog)
1631{
1632 struct bpf_prog **prog = progs->progs;
1633
1634 for (; *prog; prog++)
1635 if (*prog == old_prog) {
1636 WRITE_ONCE(*prog, &dummy_bpf_prog.prog);
1637 break;
1638 }
1639}
1640
1641int bpf_prog_array_copy(struct bpf_prog_array __rcu *old_array,
1642 struct bpf_prog *exclude_prog,
1643 struct bpf_prog *include_prog,
1644 struct bpf_prog_array **new_array)
1645{
1646 int new_prog_cnt, carry_prog_cnt = 0;
1647 struct bpf_prog **existing_prog;
1648 struct bpf_prog_array *array;
1649 int new_prog_idx = 0;
1650
1651 /* Figure out how many existing progs we need to carry over to
1652 * the new array.
1653 */
1654 if (old_array) {
1655 existing_prog = old_array->progs;
1656 for (; *existing_prog; existing_prog++) {
1657 if (*existing_prog != exclude_prog &&
1658 *existing_prog != &dummy_bpf_prog.prog)
1659 carry_prog_cnt++;
1660 if (*existing_prog == include_prog)
1661 return -EEXIST;
1662 }
1663 }
1664
1665 /* How many progs (not NULL) will be in the new array? */
1666 new_prog_cnt = carry_prog_cnt;
1667 if (include_prog)
1668 new_prog_cnt += 1;
1669
1670 /* Do we have any prog (not NULL) in the new array? */
1671 if (!new_prog_cnt) {
1672 *new_array = NULL;
1673 return 0;
1674 }
1675
1676 /* +1 as the end of prog_array is marked with NULL */
1677 array = bpf_prog_array_alloc(new_prog_cnt + 1, GFP_KERNEL);
1678 if (!array)
1679 return -ENOMEM;
1680
1681 /* Fill in the new prog array */
1682 if (carry_prog_cnt) {
1683 existing_prog = old_array->progs;
1684 for (; *existing_prog; existing_prog++)
1685 if (*existing_prog != exclude_prog &&
1686 *existing_prog != &dummy_bpf_prog.prog)
1687 array->progs[new_prog_idx++] = *existing_prog;
1688 }
1689 if (include_prog)
1690 array->progs[new_prog_idx++] = include_prog;
1691 array->progs[new_prog_idx] = NULL;
1692 *new_array = array;
1693 return 0;
1694}
1695
Yonghong Songf371b302017-12-11 11:39:02 -08001696int bpf_prog_array_copy_info(struct bpf_prog_array __rcu *array,
Yonghong Song3a38bb92018-04-10 09:37:32 -07001697 u32 *prog_ids, u32 request_cnt,
1698 u32 *prog_cnt)
Yonghong Songf371b302017-12-11 11:39:02 -08001699{
Yonghong Song3a38bb92018-04-10 09:37:32 -07001700 struct bpf_prog **prog;
Yonghong Songf371b302017-12-11 11:39:02 -08001701 u32 cnt = 0;
1702
1703 if (array)
1704 cnt = bpf_prog_array_length(array);
1705
Yonghong Song3a38bb92018-04-10 09:37:32 -07001706 *prog_cnt = cnt;
Yonghong Songf371b302017-12-11 11:39:02 -08001707
1708 /* return early if user requested only program count or nothing to copy */
1709 if (!request_cnt || !cnt)
1710 return 0;
1711
Yonghong Song3a38bb92018-04-10 09:37:32 -07001712 /* this function is called under trace/bpf_trace.c: bpf_event_mutex */
1713 prog = rcu_dereference_check(array, 1)->progs;
1714 return bpf_prog_array_copy_core(prog, prog_ids, request_cnt) ? -ENOSPC
1715 : 0;
Yonghong Songf371b302017-12-11 11:39:02 -08001716}
1717
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001718static void bpf_prog_free_deferred(struct work_struct *work)
1719{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001720 struct bpf_prog_aux *aux;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001721 int i;
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001722
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001723 aux = container_of(work, struct bpf_prog_aux, work);
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001724 if (bpf_prog_is_dev_bound(aux))
1725 bpf_prog_offload_destroy(aux->prog);
Yonghong Songc195651e2018-04-28 22:28:08 -07001726#ifdef CONFIG_PERF_EVENTS
1727 if (aux->prog->has_callchain_buf)
1728 put_callchain_buffers();
1729#endif
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001730 for (i = 0; i < aux->func_cnt; i++)
1731 bpf_jit_free(aux->func[i]);
1732 if (aux->func_cnt) {
1733 kfree(aux->func);
1734 bpf_prog_unlock_free(aux->prog);
1735 } else {
1736 bpf_jit_free(aux->prog);
1737 }
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001738}
1739
1740/* Free internal BPF program */
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001741void bpf_prog_free(struct bpf_prog *fp)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001742{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001743 struct bpf_prog_aux *aux = fp->aux;
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001744
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001745 INIT_WORK(&aux->work, bpf_prog_free_deferred);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001746 schedule_work(&aux->work);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001747}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001748EXPORT_SYMBOL_GPL(bpf_prog_free);
Alexei Starovoitovf89b7752014-10-23 18:41:08 -07001749
Daniel Borkmann3ad00402015-10-08 01:20:39 +02001750/* RNG for unpriviledged user space with separated state from prandom_u32(). */
1751static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
1752
1753void bpf_user_rnd_init_once(void)
1754{
1755 prandom_init_once(&bpf_user_rnd_state);
1756}
1757
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001758BPF_CALL_0(bpf_user_rnd_u32)
Daniel Borkmann3ad00402015-10-08 01:20:39 +02001759{
1760 /* Should someone ever have the rather unwise idea to use some
1761 * of the registers passed into this function, then note that
1762 * this function is called from native eBPF and classic-to-eBPF
1763 * transformations. Register assignments from both sides are
1764 * different, f.e. classic always sets fn(ctx, A, X) here.
1765 */
1766 struct rnd_state *state;
1767 u32 res;
1768
1769 state = &get_cpu_var(bpf_user_rnd_state);
1770 res = prandom_u32_state(state);
Shaohua Lib761fe22016-09-27 08:42:41 -07001771 put_cpu_var(bpf_user_rnd_state);
Daniel Borkmann3ad00402015-10-08 01:20:39 +02001772
1773 return res;
1774}
1775
Daniel Borkmann3ba67da2015-03-05 23:27:51 +01001776/* Weak definitions of helper functions in case we don't have bpf syscall. */
1777const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
1778const struct bpf_func_proto bpf_map_update_elem_proto __weak;
1779const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
1780
Daniel Borkmann03e69b52015-03-14 02:27:16 +01001781const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
Daniel Borkmannc04167c2015-03-14 02:27:17 +01001782const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
Daniel Borkmann2d0e30c2016-10-21 12:46:33 +02001783const struct bpf_func_proto bpf_get_numa_node_id_proto __weak;
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +02001784const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001785
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -07001786const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
1787const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
1788const struct bpf_func_proto bpf_get_current_comm_proto __weak;
John Fastabend6bdc9c42017-08-16 15:02:32 -07001789const struct bpf_func_proto bpf_sock_map_update_proto __weak;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001790
Alexei Starovoitov0756ea32015-06-12 19:39:13 -07001791const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
1792{
1793 return NULL;
1794}
Daniel Borkmann03e69b52015-03-14 02:27:16 +01001795
Daniel Borkmann555c8a82016-07-14 18:08:05 +02001796u64 __weak
1797bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1798 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001799{
Daniel Borkmann555c8a82016-07-14 18:08:05 +02001800 return -ENOTSUPP;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001801}
1802
Daniel Borkmann3324b582015-05-29 23:23:07 +02001803/* Always built-in helper functions. */
1804const struct bpf_func_proto bpf_tail_call_proto = {
1805 .func = NULL,
1806 .gpl_only = false,
1807 .ret_type = RET_VOID,
1808 .arg1_type = ARG_PTR_TO_CTX,
1809 .arg2_type = ARG_CONST_MAP_PTR,
1810 .arg3_type = ARG_ANYTHING,
1811};
1812
Daniel Borkmann93831912017-02-16 22:24:49 +01001813/* Stub for JITs that only support cBPF. eBPF programs are interpreted.
1814 * It is encouraged to implement bpf_int_jit_compile() instead, so that
1815 * eBPF and implicitly also cBPF can get JITed!
1816 */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001817struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
Daniel Borkmann3324b582015-05-29 23:23:07 +02001818{
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001819 return prog;
Daniel Borkmann3324b582015-05-29 23:23:07 +02001820}
1821
Daniel Borkmann93831912017-02-16 22:24:49 +01001822/* Stub for JITs that support eBPF. All cBPF code gets transformed into
1823 * eBPF by the kernel and is later compiled by bpf_int_jit_compile().
1824 */
1825void __weak bpf_jit_compile(struct bpf_prog *prog)
1826{
1827}
1828
Martin KaFai Lau17bedab2016-12-07 15:53:11 -08001829bool __weak bpf_helper_changes_pkt_data(void *func)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001830{
1831 return false;
1832}
1833
Alexei Starovoitovf89b7752014-10-23 18:41:08 -07001834/* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
1835 * skb_copy_bits(), so provide a weak definition of it for NET-less config.
1836 */
1837int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
1838 int len)
1839{
1840 return -EFAULT;
1841}
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001842
1843/* All definitions of tracepoints related to BPF. */
1844#define CREATE_TRACE_POINTS
1845#include <linux/bpf_trace.h>
1846
1847EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception);
1848
Steven Rostedt (VMware)9185a612017-10-12 18:40:02 -04001849/* These are only used within the BPF_SYSCALL code */
1850#ifdef CONFIG_BPF_SYSCALL
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001851EXPORT_TRACEPOINT_SYMBOL_GPL(bpf_prog_get_type);
1852EXPORT_TRACEPOINT_SYMBOL_GPL(bpf_prog_put_rcu);
Steven Rostedt (VMware)9185a612017-10-12 18:40:02 -04001853#endif