blob: 8301de2d1f9638df765d2da942b1270b66586623 [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 Borkmannfa9dd592018-01-20 01:24:33 +0100303/* All BPF JIT sysctl knobs here. */
304int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_ALWAYS_ON);
305int bpf_jit_harden __read_mostly;
306int bpf_jit_kallsyms __read_mostly;
307
Daniel Borkmann74451e662017-02-16 22:24:50 +0100308static __always_inline void
309bpf_get_prog_addr_region(const struct bpf_prog *prog,
310 unsigned long *symbol_start,
311 unsigned long *symbol_end)
312{
313 const struct bpf_binary_header *hdr = bpf_jit_binary_hdr(prog);
314 unsigned long addr = (unsigned long)hdr;
315
316 WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog));
317
318 *symbol_start = addr;
319 *symbol_end = addr + hdr->pages * PAGE_SIZE;
320}
321
322static void bpf_get_prog_name(const struct bpf_prog *prog, char *sym)
323{
Martin KaFai Lau368211f2017-10-05 21:52:13 -0700324 const char *end = sym + KSYM_NAME_LEN;
325
Daniel Borkmann74451e662017-02-16 22:24:50 +0100326 BUILD_BUG_ON(sizeof("bpf_prog_") +
Martin KaFai Lau368211f2017-10-05 21:52:13 -0700327 sizeof(prog->tag) * 2 +
328 /* name has been null terminated.
329 * We should need +1 for the '_' preceding
330 * the name. However, the null character
331 * is double counted between the name and the
332 * sizeof("bpf_prog_") above, so we omit
333 * the +1 here.
334 */
335 sizeof(prog->aux->name) > KSYM_NAME_LEN);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100336
337 sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_");
338 sym = bin2hex(sym, prog->tag, sizeof(prog->tag));
Martin KaFai Lau368211f2017-10-05 21:52:13 -0700339 if (prog->aux->name[0])
340 snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name);
341 else
342 *sym = 0;
Daniel Borkmann74451e662017-02-16 22:24:50 +0100343}
344
345static __always_inline unsigned long
346bpf_get_prog_addr_start(struct latch_tree_node *n)
347{
348 unsigned long symbol_start, symbol_end;
349 const struct bpf_prog_aux *aux;
350
351 aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
352 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
353
354 return symbol_start;
355}
356
357static __always_inline bool bpf_tree_less(struct latch_tree_node *a,
358 struct latch_tree_node *b)
359{
360 return bpf_get_prog_addr_start(a) < bpf_get_prog_addr_start(b);
361}
362
363static __always_inline int bpf_tree_comp(void *key, struct latch_tree_node *n)
364{
365 unsigned long val = (unsigned long)key;
366 unsigned long symbol_start, symbol_end;
367 const struct bpf_prog_aux *aux;
368
369 aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
370 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
371
372 if (val < symbol_start)
373 return -1;
374 if (val >= symbol_end)
375 return 1;
376
377 return 0;
378}
379
380static const struct latch_tree_ops bpf_tree_ops = {
381 .less = bpf_tree_less,
382 .comp = bpf_tree_comp,
383};
384
385static DEFINE_SPINLOCK(bpf_lock);
386static LIST_HEAD(bpf_kallsyms);
387static struct latch_tree_root bpf_tree __cacheline_aligned;
388
Daniel Borkmann74451e662017-02-16 22:24:50 +0100389static void bpf_prog_ksym_node_add(struct bpf_prog_aux *aux)
390{
391 WARN_ON_ONCE(!list_empty(&aux->ksym_lnode));
392 list_add_tail_rcu(&aux->ksym_lnode, &bpf_kallsyms);
393 latch_tree_insert(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
394}
395
396static void bpf_prog_ksym_node_del(struct bpf_prog_aux *aux)
397{
398 if (list_empty(&aux->ksym_lnode))
399 return;
400
401 latch_tree_erase(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
402 list_del_rcu(&aux->ksym_lnode);
403}
404
405static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp)
406{
407 return fp->jited && !bpf_prog_was_classic(fp);
408}
409
410static bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp)
411{
412 return list_empty(&fp->aux->ksym_lnode) ||
413 fp->aux->ksym_lnode.prev == LIST_POISON2;
414}
415
416void bpf_prog_kallsyms_add(struct bpf_prog *fp)
417{
Daniel Borkmann74451e662017-02-16 22:24:50 +0100418 if (!bpf_prog_kallsyms_candidate(fp) ||
419 !capable(CAP_SYS_ADMIN))
420 return;
421
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200422 spin_lock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100423 bpf_prog_ksym_node_add(fp->aux);
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200424 spin_unlock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100425}
426
427void bpf_prog_kallsyms_del(struct bpf_prog *fp)
428{
Daniel Borkmann74451e662017-02-16 22:24:50 +0100429 if (!bpf_prog_kallsyms_candidate(fp))
430 return;
431
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200432 spin_lock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100433 bpf_prog_ksym_node_del(fp->aux);
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200434 spin_unlock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100435}
436
437static struct bpf_prog *bpf_prog_kallsyms_find(unsigned long addr)
438{
439 struct latch_tree_node *n;
440
441 if (!bpf_jit_kallsyms_enabled())
442 return NULL;
443
444 n = latch_tree_find((void *)addr, &bpf_tree, &bpf_tree_ops);
445 return n ?
446 container_of(n, struct bpf_prog_aux, ksym_tnode)->prog :
447 NULL;
448}
449
450const char *__bpf_address_lookup(unsigned long addr, unsigned long *size,
451 unsigned long *off, char *sym)
452{
453 unsigned long symbol_start, symbol_end;
454 struct bpf_prog *prog;
455 char *ret = NULL;
456
457 rcu_read_lock();
458 prog = bpf_prog_kallsyms_find(addr);
459 if (prog) {
460 bpf_get_prog_addr_region(prog, &symbol_start, &symbol_end);
461 bpf_get_prog_name(prog, sym);
462
463 ret = sym;
464 if (size)
465 *size = symbol_end - symbol_start;
466 if (off)
467 *off = addr - symbol_start;
468 }
469 rcu_read_unlock();
470
471 return ret;
472}
473
474bool is_bpf_text_address(unsigned long addr)
475{
476 bool ret;
477
478 rcu_read_lock();
479 ret = bpf_prog_kallsyms_find(addr) != NULL;
480 rcu_read_unlock();
481
482 return ret;
483}
484
485int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
486 char *sym)
487{
488 unsigned long symbol_start, symbol_end;
489 struct bpf_prog_aux *aux;
490 unsigned int it = 0;
491 int ret = -ERANGE;
492
493 if (!bpf_jit_kallsyms_enabled())
494 return ret;
495
496 rcu_read_lock();
497 list_for_each_entry_rcu(aux, &bpf_kallsyms, ksym_lnode) {
498 if (it++ != symnum)
499 continue;
500
501 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
502 bpf_get_prog_name(aux->prog, sym);
503
504 *value = symbol_start;
505 *type = BPF_SYM_ELF_TYPE;
506
507 ret = 0;
508 break;
509 }
510 rcu_read_unlock();
511
512 return ret;
513}
514
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200515struct bpf_binary_header *
516bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
517 unsigned int alignment,
518 bpf_jit_fill_hole_t bpf_fill_ill_insns)
519{
520 struct bpf_binary_header *hdr;
521 unsigned int size, hole, start;
522
523 /* Most of BPF filters are really small, but if some of them
524 * fill a page, allow at least 128 extra bytes to insert a
525 * random section of illegal instructions.
526 */
527 size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
528 hdr = module_alloc(size);
529 if (hdr == NULL)
530 return NULL;
531
532 /* Fill space with illegal/arch-dep instructions. */
533 bpf_fill_ill_insns(hdr, size);
534
535 hdr->pages = size / PAGE_SIZE;
536 hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
537 PAGE_SIZE - sizeof(*hdr));
Daniel Borkmannb7552e1b2016-05-18 14:14:28 +0200538 start = (get_random_int() % hole) & ~(alignment - 1);
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200539
540 /* Leave a random number of instructions before BPF code. */
541 *image_ptr = &hdr->image[start];
542
543 return hdr;
544}
545
546void bpf_jit_binary_free(struct bpf_binary_header *hdr)
547{
Rusty Russellbe1f2212015-01-20 09:07:05 +1030548 module_memfree(hdr);
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200549}
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200550
Daniel Borkmann74451e662017-02-16 22:24:50 +0100551/* This symbol is only overridden by archs that have different
552 * requirements than the usual eBPF JITs, f.e. when they only
553 * implement cBPF JIT, do not set images read-only, etc.
554 */
555void __weak bpf_jit_free(struct bpf_prog *fp)
556{
557 if (fp->jited) {
558 struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp);
559
560 bpf_jit_binary_unlock_ro(hdr);
561 bpf_jit_binary_free(hdr);
562
563 WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp));
564 }
565
566 bpf_prog_unlock_free(fp);
567}
568
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200569static int bpf_jit_blind_insn(const struct bpf_insn *from,
570 const struct bpf_insn *aux,
571 struct bpf_insn *to_buff)
572{
573 struct bpf_insn *to = to_buff;
Daniel Borkmannb7552e1b2016-05-18 14:14:28 +0200574 u32 imm_rnd = get_random_int();
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200575 s16 off;
576
577 BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG);
578 BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG);
579
580 if (from->imm == 0 &&
581 (from->code == (BPF_ALU | BPF_MOV | BPF_K) ||
582 from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) {
583 *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg);
584 goto out;
585 }
586
587 switch (from->code) {
588 case BPF_ALU | BPF_ADD | BPF_K:
589 case BPF_ALU | BPF_SUB | BPF_K:
590 case BPF_ALU | BPF_AND | BPF_K:
591 case BPF_ALU | BPF_OR | BPF_K:
592 case BPF_ALU | BPF_XOR | BPF_K:
593 case BPF_ALU | BPF_MUL | BPF_K:
594 case BPF_ALU | BPF_MOV | BPF_K:
595 case BPF_ALU | BPF_DIV | BPF_K:
596 case BPF_ALU | BPF_MOD | BPF_K:
597 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
598 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
599 *to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX);
600 break;
601
602 case BPF_ALU64 | BPF_ADD | BPF_K:
603 case BPF_ALU64 | BPF_SUB | BPF_K:
604 case BPF_ALU64 | BPF_AND | BPF_K:
605 case BPF_ALU64 | BPF_OR | BPF_K:
606 case BPF_ALU64 | BPF_XOR | BPF_K:
607 case BPF_ALU64 | BPF_MUL | BPF_K:
608 case BPF_ALU64 | BPF_MOV | BPF_K:
609 case BPF_ALU64 | BPF_DIV | BPF_K:
610 case BPF_ALU64 | BPF_MOD | BPF_K:
611 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
612 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
613 *to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX);
614 break;
615
616 case BPF_JMP | BPF_JEQ | BPF_K:
617 case BPF_JMP | BPF_JNE | BPF_K:
618 case BPF_JMP | BPF_JGT | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200619 case BPF_JMP | BPF_JLT | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200620 case BPF_JMP | BPF_JGE | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200621 case BPF_JMP | BPF_JLE | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200622 case BPF_JMP | BPF_JSGT | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200623 case BPF_JMP | BPF_JSLT | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200624 case BPF_JMP | BPF_JSGE | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200625 case BPF_JMP | BPF_JSLE | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200626 case BPF_JMP | BPF_JSET | BPF_K:
627 /* Accommodate for extra offset in case of a backjump. */
628 off = from->off;
629 if (off < 0)
630 off -= 2;
631 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
632 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
633 *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off);
634 break;
635
636 case BPF_LD | BPF_ABS | BPF_W:
637 case BPF_LD | BPF_ABS | BPF_H:
638 case BPF_LD | BPF_ABS | BPF_B:
639 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
640 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
641 *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
642 break;
643
644 case BPF_LD | BPF_IND | BPF_W:
645 case BPF_LD | BPF_IND | BPF_H:
646 case BPF_LD | BPF_IND | BPF_B:
647 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
648 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
649 *to++ = BPF_ALU32_REG(BPF_ADD, BPF_REG_AX, from->src_reg);
650 *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
651 break;
652
653 case BPF_LD | BPF_IMM | BPF_DW:
654 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm);
655 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
656 *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
657 *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX);
658 break;
659 case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */
660 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm);
661 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
662 *to++ = BPF_ALU64_REG(BPF_OR, aux[0].dst_reg, BPF_REG_AX);
663 break;
664
665 case BPF_ST | BPF_MEM | BPF_DW:
666 case BPF_ST | BPF_MEM | BPF_W:
667 case BPF_ST | BPF_MEM | BPF_H:
668 case BPF_ST | BPF_MEM | BPF_B:
669 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
670 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
671 *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off);
672 break;
673 }
674out:
675 return to - to_buff;
676}
677
678static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other,
679 gfp_t gfp_extra_flags)
680{
Michal Hocko19809c22017-05-08 15:57:44 -0700681 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200682 struct bpf_prog *fp;
683
684 fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags, PAGE_KERNEL);
685 if (fp != NULL) {
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200686 /* aux->prog still points to the fp_other one, so
687 * when promoting the clone to the real program,
688 * this still needs to be adapted.
689 */
690 memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE);
691 }
692
693 return fp;
694}
695
696static void bpf_prog_clone_free(struct bpf_prog *fp)
697{
698 /* aux was stolen by the other clone, so we cannot free
699 * it from this path! It will be freed eventually by the
700 * other program on release.
701 *
702 * At this point, we don't need a deferred release since
703 * clone is guaranteed to not be locked.
704 */
705 fp->aux = NULL;
706 __bpf_prog_free(fp);
707}
708
709void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other)
710{
711 /* We have to repoint aux->prog to self, as we don't
712 * know whether fp here is the clone or the original.
713 */
714 fp->aux->prog = fp;
715 bpf_prog_clone_free(fp_other);
716}
717
718struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
719{
720 struct bpf_insn insn_buff[16], aux[2];
721 struct bpf_prog *clone, *tmp;
722 int insn_delta, insn_cnt;
723 struct bpf_insn *insn;
724 int i, rewritten;
725
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -0800726 if (!bpf_jit_blinding_enabled(prog) || prog->blinded)
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200727 return prog;
728
729 clone = bpf_prog_clone_create(prog, GFP_USER);
730 if (!clone)
731 return ERR_PTR(-ENOMEM);
732
733 insn_cnt = clone->len;
734 insn = clone->insnsi;
735
736 for (i = 0; i < insn_cnt; i++, insn++) {
737 /* We temporarily need to hold the original ld64 insn
738 * so that we can still access the first part in the
739 * second blinding run.
740 */
741 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) &&
742 insn[1].code == 0)
743 memcpy(aux, insn, sizeof(aux));
744
745 rewritten = bpf_jit_blind_insn(insn, aux, insn_buff);
746 if (!rewritten)
747 continue;
748
749 tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);
750 if (!tmp) {
751 /* Patching may have repointed aux->prog during
752 * realloc from the original one, so we need to
753 * fix it up here on error.
754 */
755 bpf_jit_prog_release_other(prog, clone);
756 return ERR_PTR(-ENOMEM);
757 }
758
759 clone = tmp;
760 insn_delta = rewritten - 1;
761
762 /* Walk new program and skip insns we just inserted. */
763 insn = clone->insnsi + i + insn_delta;
764 insn_cnt += insn_delta;
765 i += insn_delta;
766 }
767
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -0800768 clone->blinded = 1;
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200769 return clone;
770}
Daniel Borkmannb954d832014-09-10 15:01:02 +0200771#endif /* CONFIG_BPF_JIT */
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200772
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700773/* Base function for offset calculation. Needs to go into .text section,
774 * therefore keeping it non-static as well; will also be used by JITs
Daniel Borkmann7105e822017-12-20 13:42:57 +0100775 * anyway later on, so do not let the compiler omit it. This also needs
776 * to go into kallsyms for correlation from e.g. bpftool, so naming
777 * must not change.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700778 */
779noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
780{
781 return 0;
782}
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -0700783EXPORT_SYMBOL_GPL(__bpf_call_base);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700784
Daniel Borkmann5e581da2018-01-26 23:33:38 +0100785/* All UAPI available opcodes. */
786#define BPF_INSN_MAP(INSN_2, INSN_3) \
787 /* 32 bit ALU operations. */ \
788 /* Register based. */ \
789 INSN_3(ALU, ADD, X), \
790 INSN_3(ALU, SUB, X), \
791 INSN_3(ALU, AND, X), \
792 INSN_3(ALU, OR, X), \
793 INSN_3(ALU, LSH, X), \
794 INSN_3(ALU, RSH, X), \
795 INSN_3(ALU, XOR, X), \
796 INSN_3(ALU, MUL, X), \
797 INSN_3(ALU, MOV, X), \
798 INSN_3(ALU, DIV, X), \
799 INSN_3(ALU, MOD, X), \
800 INSN_2(ALU, NEG), \
801 INSN_3(ALU, END, TO_BE), \
802 INSN_3(ALU, END, TO_LE), \
803 /* Immediate based. */ \
804 INSN_3(ALU, ADD, K), \
805 INSN_3(ALU, SUB, K), \
806 INSN_3(ALU, AND, K), \
807 INSN_3(ALU, OR, K), \
808 INSN_3(ALU, LSH, K), \
809 INSN_3(ALU, RSH, K), \
810 INSN_3(ALU, XOR, K), \
811 INSN_3(ALU, MUL, K), \
812 INSN_3(ALU, MOV, K), \
813 INSN_3(ALU, DIV, K), \
814 INSN_3(ALU, MOD, K), \
815 /* 64 bit ALU operations. */ \
816 /* Register based. */ \
817 INSN_3(ALU64, ADD, X), \
818 INSN_3(ALU64, SUB, X), \
819 INSN_3(ALU64, AND, X), \
820 INSN_3(ALU64, OR, X), \
821 INSN_3(ALU64, LSH, X), \
822 INSN_3(ALU64, RSH, X), \
823 INSN_3(ALU64, XOR, X), \
824 INSN_3(ALU64, MUL, X), \
825 INSN_3(ALU64, MOV, X), \
826 INSN_3(ALU64, ARSH, X), \
827 INSN_3(ALU64, DIV, X), \
828 INSN_3(ALU64, MOD, X), \
829 INSN_2(ALU64, NEG), \
830 /* Immediate based. */ \
831 INSN_3(ALU64, ADD, K), \
832 INSN_3(ALU64, SUB, K), \
833 INSN_3(ALU64, AND, K), \
834 INSN_3(ALU64, OR, K), \
835 INSN_3(ALU64, LSH, K), \
836 INSN_3(ALU64, RSH, K), \
837 INSN_3(ALU64, XOR, K), \
838 INSN_3(ALU64, MUL, K), \
839 INSN_3(ALU64, MOV, K), \
840 INSN_3(ALU64, ARSH, K), \
841 INSN_3(ALU64, DIV, K), \
842 INSN_3(ALU64, MOD, K), \
843 /* Call instruction. */ \
844 INSN_2(JMP, CALL), \
845 /* Exit instruction. */ \
846 INSN_2(JMP, EXIT), \
847 /* Jump instructions. */ \
848 /* Register based. */ \
849 INSN_3(JMP, JEQ, X), \
850 INSN_3(JMP, JNE, X), \
851 INSN_3(JMP, JGT, X), \
852 INSN_3(JMP, JLT, X), \
853 INSN_3(JMP, JGE, X), \
854 INSN_3(JMP, JLE, X), \
855 INSN_3(JMP, JSGT, X), \
856 INSN_3(JMP, JSLT, X), \
857 INSN_3(JMP, JSGE, X), \
858 INSN_3(JMP, JSLE, X), \
859 INSN_3(JMP, JSET, X), \
860 /* Immediate based. */ \
861 INSN_3(JMP, JEQ, K), \
862 INSN_3(JMP, JNE, K), \
863 INSN_3(JMP, JGT, K), \
864 INSN_3(JMP, JLT, K), \
865 INSN_3(JMP, JGE, K), \
866 INSN_3(JMP, JLE, K), \
867 INSN_3(JMP, JSGT, K), \
868 INSN_3(JMP, JSLT, K), \
869 INSN_3(JMP, JSGE, K), \
870 INSN_3(JMP, JSLE, K), \
871 INSN_3(JMP, JSET, K), \
872 INSN_2(JMP, JA), \
873 /* Store instructions. */ \
874 /* Register based. */ \
875 INSN_3(STX, MEM, B), \
876 INSN_3(STX, MEM, H), \
877 INSN_3(STX, MEM, W), \
878 INSN_3(STX, MEM, DW), \
879 INSN_3(STX, XADD, W), \
880 INSN_3(STX, XADD, DW), \
881 /* Immediate based. */ \
882 INSN_3(ST, MEM, B), \
883 INSN_3(ST, MEM, H), \
884 INSN_3(ST, MEM, W), \
885 INSN_3(ST, MEM, DW), \
886 /* Load instructions. */ \
887 /* Register based. */ \
888 INSN_3(LDX, MEM, B), \
889 INSN_3(LDX, MEM, H), \
890 INSN_3(LDX, MEM, W), \
891 INSN_3(LDX, MEM, DW), \
892 /* Immediate based. */ \
893 INSN_3(LD, IMM, DW), \
894 /* Misc (old cBPF carry-over). */ \
895 INSN_3(LD, ABS, B), \
896 INSN_3(LD, ABS, H), \
897 INSN_3(LD, ABS, W), \
898 INSN_3(LD, IND, B), \
899 INSN_3(LD, IND, H), \
900 INSN_3(LD, IND, W)
901
902bool bpf_opcode_in_insntable(u8 code)
903{
904#define BPF_INSN_2_TBL(x, y) [BPF_##x | BPF_##y] = true
905#define BPF_INSN_3_TBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = true
906 static const bool public_insntable[256] = {
907 [0 ... 255] = false,
908 /* Now overwrite non-defaults ... */
909 BPF_INSN_MAP(BPF_INSN_2_TBL, BPF_INSN_3_TBL),
910 };
911#undef BPF_INSN_3_TBL
912#undef BPF_INSN_2_TBL
913 return public_insntable[code];
914}
915
Alexei Starovoitov290af862018-01-09 10:04:29 -0800916#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700917/**
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700918 * __bpf_prog_run - run eBPF program on a given context
919 * @ctx: is the data we are operating on
920 * @insn: is the array of eBPF instructions
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700921 *
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700922 * Decode and execute eBPF instructions.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700923 */
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800924static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700925{
Alexei Starovoitovf696b8f2017-05-30 13:31:28 -0700926 u64 tmp;
Daniel Borkmann5e581da2018-01-26 23:33:38 +0100927#define BPF_INSN_2_LBL(x, y) [BPF_##x | BPF_##y] = &&x##_##y
928#define BPF_INSN_3_LBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = &&x##_##y##_##z
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700929 static const void *jumptable[256] = {
930 [0 ... 255] = &&default_label,
931 /* Now overwrite non-defaults ... */
Daniel Borkmann5e581da2018-01-26 23:33:38 +0100932 BPF_INSN_MAP(BPF_INSN_2_LBL, BPF_INSN_3_LBL),
933 /* Non-UAPI available opcodes. */
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800934 [BPF_JMP | BPF_CALL_ARGS] = &&JMP_CALL_ARGS,
Alexei Starovoitov71189fa2017-05-30 13:31:27 -0700935 [BPF_JMP | BPF_TAIL_CALL] = &&JMP_TAIL_CALL,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700936 };
Daniel Borkmann5e581da2018-01-26 23:33:38 +0100937#undef BPF_INSN_3_LBL
938#undef BPF_INSN_2_LBL
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700939 u32 tail_call_cnt = 0;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700940 void *ptr;
941 int off;
942
943#define CONT ({ insn++; goto select_insn; })
944#define CONT_JMP ({ insn++; goto select_insn; })
945
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700946select_insn:
947 goto *jumptable[insn->code];
948
949 /* ALU */
950#define ALU(OPCODE, OP) \
951 ALU64_##OPCODE##_X: \
952 DST = DST OP SRC; \
953 CONT; \
954 ALU_##OPCODE##_X: \
955 DST = (u32) DST OP (u32) SRC; \
956 CONT; \
957 ALU64_##OPCODE##_K: \
958 DST = DST OP IMM; \
959 CONT; \
960 ALU_##OPCODE##_K: \
961 DST = (u32) DST OP (u32) IMM; \
962 CONT;
963
964 ALU(ADD, +)
965 ALU(SUB, -)
966 ALU(AND, &)
967 ALU(OR, |)
968 ALU(LSH, <<)
969 ALU(RSH, >>)
970 ALU(XOR, ^)
971 ALU(MUL, *)
972#undef ALU
973 ALU_NEG:
974 DST = (u32) -DST;
975 CONT;
976 ALU64_NEG:
977 DST = -DST;
978 CONT;
979 ALU_MOV_X:
980 DST = (u32) SRC;
981 CONT;
982 ALU_MOV_K:
983 DST = (u32) IMM;
984 CONT;
985 ALU64_MOV_X:
986 DST = SRC;
987 CONT;
988 ALU64_MOV_K:
989 DST = IMM;
990 CONT;
Alexei Starovoitov02ab6952014-09-04 22:17:17 -0700991 LD_IMM_DW:
992 DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
993 insn++;
994 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700995 ALU64_ARSH_X:
996 (*(s64 *) &DST) >>= SRC;
997 CONT;
998 ALU64_ARSH_K:
999 (*(s64 *) &DST) >>= IMM;
1000 CONT;
1001 ALU64_MOD_X:
1002 if (unlikely(SRC == 0))
1003 return 0;
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -07001004 div64_u64_rem(DST, SRC, &tmp);
1005 DST = tmp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001006 CONT;
1007 ALU_MOD_X:
Eric Dumazetc3662872018-01-12 17:43:23 -08001008 if (unlikely((u32)SRC == 0))
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001009 return 0;
1010 tmp = (u32) DST;
1011 DST = do_div(tmp, (u32) SRC);
1012 CONT;
1013 ALU64_MOD_K:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -07001014 div64_u64_rem(DST, IMM, &tmp);
1015 DST = tmp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001016 CONT;
1017 ALU_MOD_K:
1018 tmp = (u32) DST;
1019 DST = do_div(tmp, (u32) IMM);
1020 CONT;
1021 ALU64_DIV_X:
1022 if (unlikely(SRC == 0))
1023 return 0;
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -07001024 DST = div64_u64(DST, SRC);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001025 CONT;
1026 ALU_DIV_X:
Eric Dumazetc3662872018-01-12 17:43:23 -08001027 if (unlikely((u32)SRC == 0))
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001028 return 0;
1029 tmp = (u32) DST;
1030 do_div(tmp, (u32) SRC);
1031 DST = (u32) tmp;
1032 CONT;
1033 ALU64_DIV_K:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -07001034 DST = div64_u64(DST, IMM);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001035 CONT;
1036 ALU_DIV_K:
1037 tmp = (u32) DST;
1038 do_div(tmp, (u32) IMM);
1039 DST = (u32) tmp;
1040 CONT;
1041 ALU_END_TO_BE:
1042 switch (IMM) {
1043 case 16:
1044 DST = (__force u16) cpu_to_be16(DST);
1045 break;
1046 case 32:
1047 DST = (__force u32) cpu_to_be32(DST);
1048 break;
1049 case 64:
1050 DST = (__force u64) cpu_to_be64(DST);
1051 break;
1052 }
1053 CONT;
1054 ALU_END_TO_LE:
1055 switch (IMM) {
1056 case 16:
1057 DST = (__force u16) cpu_to_le16(DST);
1058 break;
1059 case 32:
1060 DST = (__force u32) cpu_to_le32(DST);
1061 break;
1062 case 64:
1063 DST = (__force u64) cpu_to_le64(DST);
1064 break;
1065 }
1066 CONT;
1067
1068 /* CALL */
1069 JMP_CALL:
1070 /* Function call scratches BPF_R1-BPF_R5 registers,
1071 * preserves BPF_R6-BPF_R9, and stores return value
1072 * into BPF_R0.
1073 */
1074 BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
1075 BPF_R4, BPF_R5);
1076 CONT;
1077
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001078 JMP_CALL_ARGS:
1079 BPF_R0 = (__bpf_call_base_args + insn->imm)(BPF_R1, BPF_R2,
1080 BPF_R3, BPF_R4,
1081 BPF_R5,
1082 insn + insn->off + 1);
1083 CONT;
1084
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001085 JMP_TAIL_CALL: {
1086 struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
1087 struct bpf_array *array = container_of(map, struct bpf_array, map);
1088 struct bpf_prog *prog;
Alexei Starovoitov90caccd2017-10-03 15:37:20 -07001089 u32 index = BPF_R3;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001090
1091 if (unlikely(index >= array->map.max_entries))
1092 goto out;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001093 if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
1094 goto out;
1095
1096 tail_call_cnt++;
1097
Wang Nan2a36f0b2015-08-06 07:02:33 +00001098 prog = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +02001099 if (!prog)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001100 goto out;
1101
Daniel Borkmannc4675f92015-07-13 20:49:32 +02001102 /* ARG1 at this point is guaranteed to point to CTX from
1103 * the verifier side due to the fact that the tail call is
1104 * handeled like a helper, that is, bpf_tail_call_proto,
1105 * where arg1_type is ARG_PTR_TO_CTX.
1106 */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001107 insn = prog->insnsi;
1108 goto select_insn;
1109out:
1110 CONT;
1111 }
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001112 /* JMP */
1113 JMP_JA:
1114 insn += insn->off;
1115 CONT;
1116 JMP_JEQ_X:
1117 if (DST == SRC) {
1118 insn += insn->off;
1119 CONT_JMP;
1120 }
1121 CONT;
1122 JMP_JEQ_K:
1123 if (DST == IMM) {
1124 insn += insn->off;
1125 CONT_JMP;
1126 }
1127 CONT;
1128 JMP_JNE_X:
1129 if (DST != SRC) {
1130 insn += insn->off;
1131 CONT_JMP;
1132 }
1133 CONT;
1134 JMP_JNE_K:
1135 if (DST != IMM) {
1136 insn += insn->off;
1137 CONT_JMP;
1138 }
1139 CONT;
1140 JMP_JGT_X:
1141 if (DST > SRC) {
1142 insn += insn->off;
1143 CONT_JMP;
1144 }
1145 CONT;
1146 JMP_JGT_K:
1147 if (DST > IMM) {
1148 insn += insn->off;
1149 CONT_JMP;
1150 }
1151 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001152 JMP_JLT_X:
1153 if (DST < SRC) {
1154 insn += insn->off;
1155 CONT_JMP;
1156 }
1157 CONT;
1158 JMP_JLT_K:
1159 if (DST < IMM) {
1160 insn += insn->off;
1161 CONT_JMP;
1162 }
1163 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001164 JMP_JGE_X:
1165 if (DST >= SRC) {
1166 insn += insn->off;
1167 CONT_JMP;
1168 }
1169 CONT;
1170 JMP_JGE_K:
1171 if (DST >= IMM) {
1172 insn += insn->off;
1173 CONT_JMP;
1174 }
1175 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001176 JMP_JLE_X:
1177 if (DST <= SRC) {
1178 insn += insn->off;
1179 CONT_JMP;
1180 }
1181 CONT;
1182 JMP_JLE_K:
1183 if (DST <= IMM) {
1184 insn += insn->off;
1185 CONT_JMP;
1186 }
1187 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001188 JMP_JSGT_X:
1189 if (((s64) DST) > ((s64) SRC)) {
1190 insn += insn->off;
1191 CONT_JMP;
1192 }
1193 CONT;
1194 JMP_JSGT_K:
1195 if (((s64) DST) > ((s64) IMM)) {
1196 insn += insn->off;
1197 CONT_JMP;
1198 }
1199 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001200 JMP_JSLT_X:
1201 if (((s64) DST) < ((s64) SRC)) {
1202 insn += insn->off;
1203 CONT_JMP;
1204 }
1205 CONT;
1206 JMP_JSLT_K:
1207 if (((s64) DST) < ((s64) IMM)) {
1208 insn += insn->off;
1209 CONT_JMP;
1210 }
1211 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001212 JMP_JSGE_X:
1213 if (((s64) DST) >= ((s64) SRC)) {
1214 insn += insn->off;
1215 CONT_JMP;
1216 }
1217 CONT;
1218 JMP_JSGE_K:
1219 if (((s64) DST) >= ((s64) IMM)) {
1220 insn += insn->off;
1221 CONT_JMP;
1222 }
1223 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001224 JMP_JSLE_X:
1225 if (((s64) DST) <= ((s64) SRC)) {
1226 insn += insn->off;
1227 CONT_JMP;
1228 }
1229 CONT;
1230 JMP_JSLE_K:
1231 if (((s64) DST) <= ((s64) IMM)) {
1232 insn += insn->off;
1233 CONT_JMP;
1234 }
1235 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001236 JMP_JSET_X:
1237 if (DST & SRC) {
1238 insn += insn->off;
1239 CONT_JMP;
1240 }
1241 CONT;
1242 JMP_JSET_K:
1243 if (DST & IMM) {
1244 insn += insn->off;
1245 CONT_JMP;
1246 }
1247 CONT;
1248 JMP_EXIT:
1249 return BPF_R0;
1250
1251 /* STX and ST and LDX*/
1252#define LDST(SIZEOP, SIZE) \
1253 STX_MEM_##SIZEOP: \
1254 *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \
1255 CONT; \
1256 ST_MEM_##SIZEOP: \
1257 *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \
1258 CONT; \
1259 LDX_MEM_##SIZEOP: \
1260 DST = *(SIZE *)(unsigned long) (SRC + insn->off); \
1261 CONT;
1262
1263 LDST(B, u8)
1264 LDST(H, u16)
1265 LDST(W, u32)
1266 LDST(DW, u64)
1267#undef LDST
1268 STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
1269 atomic_add((u32) SRC, (atomic_t *)(unsigned long)
1270 (DST + insn->off));
1271 CONT;
1272 STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
1273 atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
1274 (DST + insn->off));
1275 CONT;
1276 LD_ABS_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + imm32)) */
1277 off = IMM;
1278load_word:
Johannes Berg96a94cc2017-04-11 12:10:58 +02001279 /* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are only
1280 * appearing in the programs where ctx == skb
1281 * (see may_access_skb() in the verifier). All programs
1282 * keep 'ctx' in regs[BPF_REG_CTX] == BPF_R6,
1283 * bpf_convert_filter() saves it in BPF_R6, internal BPF
1284 * verifier will check that BPF_R6 == ctx.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001285 *
1286 * BPF_ABS and BPF_IND are wrappers of function calls,
1287 * so they scratch BPF_R1-BPF_R5 registers, preserve
1288 * BPF_R6-BPF_R9, and store return value into BPF_R0.
1289 *
1290 * Implicit input:
1291 * ctx == skb == BPF_R6 == CTX
1292 *
1293 * Explicit input:
1294 * SRC == any register
1295 * IMM == 32-bit immediate
1296 *
1297 * Output:
1298 * BPF_R0 - 8/16/32-bit skb data converted to cpu endianness
1299 */
1300
1301 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 4, &tmp);
1302 if (likely(ptr != NULL)) {
1303 BPF_R0 = get_unaligned_be32(ptr);
1304 CONT;
1305 }
1306
1307 return 0;
1308 LD_ABS_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + imm32)) */
1309 off = IMM;
1310load_half:
1311 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 2, &tmp);
1312 if (likely(ptr != NULL)) {
1313 BPF_R0 = get_unaligned_be16(ptr);
1314 CONT;
1315 }
1316
1317 return 0;
1318 LD_ABS_B: /* BPF_R0 = *(u8 *) (skb->data + imm32) */
1319 off = IMM;
1320load_byte:
1321 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 1, &tmp);
1322 if (likely(ptr != NULL)) {
1323 BPF_R0 = *(u8 *)ptr;
1324 CONT;
1325 }
1326
1327 return 0;
1328 LD_IND_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + src_reg + imm32)) */
1329 off = IMM + SRC;
1330 goto load_word;
1331 LD_IND_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + src_reg + imm32)) */
1332 off = IMM + SRC;
1333 goto load_half;
1334 LD_IND_B: /* BPF_R0 = *(u8 *) (skb->data + src_reg + imm32) */
1335 off = IMM + SRC;
1336 goto load_byte;
1337
1338 default_label:
Daniel Borkmann5e581da2018-01-26 23:33:38 +01001339 /* If we ever reach this, we have a bug somewhere. Die hard here
1340 * instead of just returning 0; we could be somewhere in a subprog,
1341 * so execution could continue otherwise which we do /not/ want.
1342 *
1343 * Note, verifier whitelists all opcodes in bpf_opcode_in_insntable().
1344 */
1345 pr_warn("BPF interpreter: unknown opcode %02x\n", insn->code);
1346 BUG_ON(1);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001347 return 0;
1348}
Alexei Starovoitovf696b8f2017-05-30 13:31:28 -07001349STACK_FRAME_NON_STANDARD(___bpf_prog_run); /* jump table */
1350
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001351#define PROG_NAME(stack_size) __bpf_prog_run##stack_size
1352#define DEFINE_BPF_PROG_RUN(stack_size) \
1353static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \
1354{ \
1355 u64 stack[stack_size / sizeof(u64)]; \
1356 u64 regs[MAX_BPF_REG]; \
1357\
1358 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1359 ARG1 = (u64) (unsigned long) ctx; \
1360 return ___bpf_prog_run(regs, insn, stack); \
Alexei Starovoitovf696b8f2017-05-30 13:31:28 -07001361}
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001362
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001363#define PROG_NAME_ARGS(stack_size) __bpf_prog_run_args##stack_size
1364#define DEFINE_BPF_PROG_RUN_ARGS(stack_size) \
1365static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \
1366 const struct bpf_insn *insn) \
1367{ \
1368 u64 stack[stack_size / sizeof(u64)]; \
1369 u64 regs[MAX_BPF_REG]; \
1370\
1371 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1372 BPF_R1 = r1; \
1373 BPF_R2 = r2; \
1374 BPF_R3 = r3; \
1375 BPF_R4 = r4; \
1376 BPF_R5 = r5; \
1377 return ___bpf_prog_run(regs, insn, stack); \
1378}
1379
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001380#define EVAL1(FN, X) FN(X)
1381#define EVAL2(FN, X, Y...) FN(X) EVAL1(FN, Y)
1382#define EVAL3(FN, X, Y...) FN(X) EVAL2(FN, Y)
1383#define EVAL4(FN, X, Y...) FN(X) EVAL3(FN, Y)
1384#define EVAL5(FN, X, Y...) FN(X) EVAL4(FN, Y)
1385#define EVAL6(FN, X, Y...) FN(X) EVAL5(FN, Y)
1386
1387EVAL6(DEFINE_BPF_PROG_RUN, 32, 64, 96, 128, 160, 192);
1388EVAL6(DEFINE_BPF_PROG_RUN, 224, 256, 288, 320, 352, 384);
1389EVAL4(DEFINE_BPF_PROG_RUN, 416, 448, 480, 512);
1390
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001391EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 32, 64, 96, 128, 160, 192);
1392EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 224, 256, 288, 320, 352, 384);
1393EVAL4(DEFINE_BPF_PROG_RUN_ARGS, 416, 448, 480, 512);
1394
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001395#define PROG_NAME_LIST(stack_size) PROG_NAME(stack_size),
1396
1397static unsigned int (*interpreters[])(const void *ctx,
1398 const struct bpf_insn *insn) = {
1399EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1400EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1401EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1402};
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001403#undef PROG_NAME_LIST
1404#define PROG_NAME_LIST(stack_size) PROG_NAME_ARGS(stack_size),
1405static u64 (*interpreters_args[])(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5,
1406 const struct bpf_insn *insn) = {
1407EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1408EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1409EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1410};
1411#undef PROG_NAME_LIST
1412
1413void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth)
1414{
1415 stack_depth = max_t(u32, stack_depth, 1);
1416 insn->off = (s16) insn->imm;
1417 insn->imm = interpreters_args[(round_up(stack_depth, 32) / 32) - 1] -
1418 __bpf_call_base_args;
1419 insn->code = BPF_JMP | BPF_CALL_ARGS;
1420}
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001421
Alexei Starovoitov290af862018-01-09 10:04:29 -08001422#else
Daniel Borkmannfa9dd592018-01-20 01:24:33 +01001423static unsigned int __bpf_prog_ret0_warn(const void *ctx,
1424 const struct bpf_insn *insn)
Alexei Starovoitov290af862018-01-09 10:04:29 -08001425{
Daniel Borkmannfa9dd592018-01-20 01:24:33 +01001426 /* If this handler ever gets executed, then BPF_JIT_ALWAYS_ON
1427 * is not working properly, so warn about it!
1428 */
1429 WARN_ON_ONCE(1);
Alexei Starovoitov290af862018-01-09 10:04:29 -08001430 return 0;
1431}
1432#endif
1433
Daniel Borkmann3324b582015-05-29 23:23:07 +02001434bool bpf_prog_array_compatible(struct bpf_array *array,
1435 const struct bpf_prog *fp)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001436{
Josef Bacik9802d862017-12-11 11:36:48 -05001437 if (fp->kprobe_override)
1438 return false;
1439
Daniel Borkmann3324b582015-05-29 23:23:07 +02001440 if (!array->owner_prog_type) {
1441 /* There's no owner yet where we could check for
1442 * compatibility.
1443 */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001444 array->owner_prog_type = fp->type;
1445 array->owner_jited = fp->jited;
Daniel Borkmann3324b582015-05-29 23:23:07 +02001446
1447 return true;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001448 }
Daniel Borkmann3324b582015-05-29 23:23:07 +02001449
1450 return array->owner_prog_type == fp->type &&
1451 array->owner_jited == fp->jited;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001452}
1453
Daniel Borkmann3324b582015-05-29 23:23:07 +02001454static int bpf_check_tail_call(const struct bpf_prog *fp)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001455{
1456 struct bpf_prog_aux *aux = fp->aux;
1457 int i;
1458
1459 for (i = 0; i < aux->used_map_cnt; i++) {
Daniel Borkmann3324b582015-05-29 23:23:07 +02001460 struct bpf_map *map = aux->used_maps[i];
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001461 struct bpf_array *array;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001462
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001463 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1464 continue;
Daniel Borkmann3324b582015-05-29 23:23:07 +02001465
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001466 array = container_of(map, struct bpf_array, map);
1467 if (!bpf_prog_array_compatible(array, fp))
1468 return -EINVAL;
1469 }
1470
1471 return 0;
1472}
1473
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001474/**
Daniel Borkmann3324b582015-05-29 23:23:07 +02001475 * bpf_prog_select_runtime - select exec runtime for BPF program
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001476 * @fp: bpf_prog populated with internal BPF program
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001477 * @err: pointer to error variable
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001478 *
Daniel Borkmann3324b582015-05-29 23:23:07 +02001479 * Try to JIT eBPF program, if JIT is not available, use interpreter.
1480 * The BPF program will be executed via BPF_PROG_RUN() macro.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001481 */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001482struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001483{
Alexei Starovoitov290af862018-01-09 10:04:29 -08001484#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Martin KaFai Lau8007e402017-06-28 10:41:24 -07001485 u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1);
1486
1487 fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1];
Alexei Starovoitov290af862018-01-09 10:04:29 -08001488#else
Daniel Borkmannfa9dd592018-01-20 01:24:33 +01001489 fp->bpf_func = __bpf_prog_ret0_warn;
Alexei Starovoitov290af862018-01-09 10:04:29 -08001490#endif
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001491
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001492 /* eBPF JITs can rewrite the program in case constant
1493 * blinding is active. However, in case of error during
1494 * blinding, bpf_int_jit_compile() must always return a
1495 * valid program, which in this case would simply not
1496 * be JITed, but falls back to the interpreter.
1497 */
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001498 if (!bpf_prog_is_dev_bound(fp->aux)) {
1499 fp = bpf_int_jit_compile(fp);
Alexei Starovoitov290af862018-01-09 10:04:29 -08001500#ifdef CONFIG_BPF_JIT_ALWAYS_ON
1501 if (!fp->jited) {
1502 *err = -ENOTSUPP;
1503 return fp;
1504 }
1505#endif
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001506 } else {
1507 *err = bpf_prog_offload_compile(fp);
1508 if (*err)
1509 return fp;
1510 }
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001511 bpf_prog_lock_ro(fp);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001512
Daniel Borkmann3324b582015-05-29 23:23:07 +02001513 /* The tail call compatibility check can only be done at
1514 * this late stage as we need to determine, if we deal
1515 * with JITed or non JITed program concatenations and not
1516 * all eBPF JITs might immediately support all features.
1517 */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001518 *err = bpf_check_tail_call(fp);
1519
1520 return fp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001521}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001522EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001523
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001524static unsigned int __bpf_prog_ret1(const void *ctx,
1525 const struct bpf_insn *insn)
1526{
1527 return 1;
1528}
1529
1530static struct bpf_prog_dummy {
1531 struct bpf_prog prog;
1532} dummy_bpf_prog = {
1533 .prog = {
1534 .bpf_func = __bpf_prog_ret1,
1535 },
1536};
1537
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001538/* to avoid allocating empty bpf_prog_array for cgroups that
1539 * don't have bpf program attached use one global 'empty_prog_array'
1540 * It will not be modified the caller of bpf_prog_array_alloc()
1541 * (since caller requested prog_cnt == 0)
1542 * that pointer should be 'freed' by bpf_prog_array_free()
1543 */
1544static struct {
1545 struct bpf_prog_array hdr;
1546 struct bpf_prog *null_prog;
1547} empty_prog_array = {
1548 .null_prog = NULL,
1549};
1550
1551struct bpf_prog_array __rcu *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags)
1552{
1553 if (prog_cnt)
1554 return kzalloc(sizeof(struct bpf_prog_array) +
1555 sizeof(struct bpf_prog *) * (prog_cnt + 1),
1556 flags);
1557
1558 return &empty_prog_array.hdr;
1559}
1560
1561void bpf_prog_array_free(struct bpf_prog_array __rcu *progs)
1562{
1563 if (!progs ||
1564 progs == (struct bpf_prog_array __rcu *)&empty_prog_array.hdr)
1565 return;
1566 kfree_rcu(progs, rcu);
1567}
1568
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001569int bpf_prog_array_length(struct bpf_prog_array __rcu *progs)
1570{
1571 struct bpf_prog **prog;
1572 u32 cnt = 0;
1573
1574 rcu_read_lock();
1575 prog = rcu_dereference(progs)->progs;
1576 for (; *prog; prog++)
Yonghong Songc8c088b2017-11-30 13:47:54 -08001577 if (*prog != &dummy_bpf_prog.prog)
1578 cnt++;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001579 rcu_read_unlock();
1580 return cnt;
1581}
1582
1583int bpf_prog_array_copy_to_user(struct bpf_prog_array __rcu *progs,
1584 __u32 __user *prog_ids, u32 cnt)
1585{
1586 struct bpf_prog **prog;
1587 u32 i = 0, id;
1588
1589 rcu_read_lock();
1590 prog = rcu_dereference(progs)->progs;
1591 for (; *prog; prog++) {
Yonghong Songf371b302017-12-11 11:39:02 -08001592 if (*prog == &dummy_bpf_prog.prog)
1593 continue;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001594 id = (*prog)->aux->id;
1595 if (copy_to_user(prog_ids + i, &id, sizeof(id))) {
1596 rcu_read_unlock();
1597 return -EFAULT;
1598 }
1599 if (++i == cnt) {
1600 prog++;
1601 break;
1602 }
1603 }
1604 rcu_read_unlock();
1605 if (*prog)
1606 return -ENOSPC;
1607 return 0;
1608}
1609
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001610void bpf_prog_array_delete_safe(struct bpf_prog_array __rcu *progs,
1611 struct bpf_prog *old_prog)
1612{
1613 struct bpf_prog **prog = progs->progs;
1614
1615 for (; *prog; prog++)
1616 if (*prog == old_prog) {
1617 WRITE_ONCE(*prog, &dummy_bpf_prog.prog);
1618 break;
1619 }
1620}
1621
1622int bpf_prog_array_copy(struct bpf_prog_array __rcu *old_array,
1623 struct bpf_prog *exclude_prog,
1624 struct bpf_prog *include_prog,
1625 struct bpf_prog_array **new_array)
1626{
1627 int new_prog_cnt, carry_prog_cnt = 0;
1628 struct bpf_prog **existing_prog;
1629 struct bpf_prog_array *array;
1630 int new_prog_idx = 0;
1631
1632 /* Figure out how many existing progs we need to carry over to
1633 * the new array.
1634 */
1635 if (old_array) {
1636 existing_prog = old_array->progs;
1637 for (; *existing_prog; existing_prog++) {
1638 if (*existing_prog != exclude_prog &&
1639 *existing_prog != &dummy_bpf_prog.prog)
1640 carry_prog_cnt++;
1641 if (*existing_prog == include_prog)
1642 return -EEXIST;
1643 }
1644 }
1645
1646 /* How many progs (not NULL) will be in the new array? */
1647 new_prog_cnt = carry_prog_cnt;
1648 if (include_prog)
1649 new_prog_cnt += 1;
1650
1651 /* Do we have any prog (not NULL) in the new array? */
1652 if (!new_prog_cnt) {
1653 *new_array = NULL;
1654 return 0;
1655 }
1656
1657 /* +1 as the end of prog_array is marked with NULL */
1658 array = bpf_prog_array_alloc(new_prog_cnt + 1, GFP_KERNEL);
1659 if (!array)
1660 return -ENOMEM;
1661
1662 /* Fill in the new prog array */
1663 if (carry_prog_cnt) {
1664 existing_prog = old_array->progs;
1665 for (; *existing_prog; existing_prog++)
1666 if (*existing_prog != exclude_prog &&
1667 *existing_prog != &dummy_bpf_prog.prog)
1668 array->progs[new_prog_idx++] = *existing_prog;
1669 }
1670 if (include_prog)
1671 array->progs[new_prog_idx++] = include_prog;
1672 array->progs[new_prog_idx] = NULL;
1673 *new_array = array;
1674 return 0;
1675}
1676
Yonghong Songf371b302017-12-11 11:39:02 -08001677int bpf_prog_array_copy_info(struct bpf_prog_array __rcu *array,
1678 __u32 __user *prog_ids, u32 request_cnt,
1679 __u32 __user *prog_cnt)
1680{
1681 u32 cnt = 0;
1682
1683 if (array)
1684 cnt = bpf_prog_array_length(array);
1685
1686 if (copy_to_user(prog_cnt, &cnt, sizeof(cnt)))
1687 return -EFAULT;
1688
1689 /* return early if user requested only program count or nothing to copy */
1690 if (!request_cnt || !cnt)
1691 return 0;
1692
1693 return bpf_prog_array_copy_to_user(array, prog_ids, request_cnt);
1694}
1695
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001696static void bpf_prog_free_deferred(struct work_struct *work)
1697{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001698 struct bpf_prog_aux *aux;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001699 int i;
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001700
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001701 aux = container_of(work, struct bpf_prog_aux, work);
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001702 if (bpf_prog_is_dev_bound(aux))
1703 bpf_prog_offload_destroy(aux->prog);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001704 for (i = 0; i < aux->func_cnt; i++)
1705 bpf_jit_free(aux->func[i]);
1706 if (aux->func_cnt) {
1707 kfree(aux->func);
1708 bpf_prog_unlock_free(aux->prog);
1709 } else {
1710 bpf_jit_free(aux->prog);
1711 }
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001712}
1713
1714/* Free internal BPF program */
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001715void bpf_prog_free(struct bpf_prog *fp)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001716{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001717 struct bpf_prog_aux *aux = fp->aux;
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001718
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001719 INIT_WORK(&aux->work, bpf_prog_free_deferred);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001720 schedule_work(&aux->work);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001721}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001722EXPORT_SYMBOL_GPL(bpf_prog_free);
Alexei Starovoitovf89b7752014-10-23 18:41:08 -07001723
Daniel Borkmann3ad00402015-10-08 01:20:39 +02001724/* RNG for unpriviledged user space with separated state from prandom_u32(). */
1725static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
1726
1727void bpf_user_rnd_init_once(void)
1728{
1729 prandom_init_once(&bpf_user_rnd_state);
1730}
1731
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001732BPF_CALL_0(bpf_user_rnd_u32)
Daniel Borkmann3ad00402015-10-08 01:20:39 +02001733{
1734 /* Should someone ever have the rather unwise idea to use some
1735 * of the registers passed into this function, then note that
1736 * this function is called from native eBPF and classic-to-eBPF
1737 * transformations. Register assignments from both sides are
1738 * different, f.e. classic always sets fn(ctx, A, X) here.
1739 */
1740 struct rnd_state *state;
1741 u32 res;
1742
1743 state = &get_cpu_var(bpf_user_rnd_state);
1744 res = prandom_u32_state(state);
Shaohua Lib761fe22016-09-27 08:42:41 -07001745 put_cpu_var(bpf_user_rnd_state);
Daniel Borkmann3ad00402015-10-08 01:20:39 +02001746
1747 return res;
1748}
1749
Daniel Borkmann3ba67da2015-03-05 23:27:51 +01001750/* Weak definitions of helper functions in case we don't have bpf syscall. */
1751const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
1752const struct bpf_func_proto bpf_map_update_elem_proto __weak;
1753const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
1754
Daniel Borkmann03e69b52015-03-14 02:27:16 +01001755const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
Daniel Borkmannc04167c2015-03-14 02:27:17 +01001756const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
Daniel Borkmann2d0e30c2016-10-21 12:46:33 +02001757const struct bpf_func_proto bpf_get_numa_node_id_proto __weak;
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +02001758const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001759
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -07001760const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
1761const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
1762const struct bpf_func_proto bpf_get_current_comm_proto __weak;
John Fastabend6bdc9c42017-08-16 15:02:32 -07001763const struct bpf_func_proto bpf_sock_map_update_proto __weak;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001764
Alexei Starovoitov0756ea32015-06-12 19:39:13 -07001765const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
1766{
1767 return NULL;
1768}
Daniel Borkmann03e69b52015-03-14 02:27:16 +01001769
Daniel Borkmann555c8a82016-07-14 18:08:05 +02001770u64 __weak
1771bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1772 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001773{
Daniel Borkmann555c8a82016-07-14 18:08:05 +02001774 return -ENOTSUPP;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001775}
1776
Daniel Borkmann3324b582015-05-29 23:23:07 +02001777/* Always built-in helper functions. */
1778const struct bpf_func_proto bpf_tail_call_proto = {
1779 .func = NULL,
1780 .gpl_only = false,
1781 .ret_type = RET_VOID,
1782 .arg1_type = ARG_PTR_TO_CTX,
1783 .arg2_type = ARG_CONST_MAP_PTR,
1784 .arg3_type = ARG_ANYTHING,
1785};
1786
Daniel Borkmann93831912017-02-16 22:24:49 +01001787/* Stub for JITs that only support cBPF. eBPF programs are interpreted.
1788 * It is encouraged to implement bpf_int_jit_compile() instead, so that
1789 * eBPF and implicitly also cBPF can get JITed!
1790 */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001791struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
Daniel Borkmann3324b582015-05-29 23:23:07 +02001792{
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001793 return prog;
Daniel Borkmann3324b582015-05-29 23:23:07 +02001794}
1795
Daniel Borkmann93831912017-02-16 22:24:49 +01001796/* Stub for JITs that support eBPF. All cBPF code gets transformed into
1797 * eBPF by the kernel and is later compiled by bpf_int_jit_compile().
1798 */
1799void __weak bpf_jit_compile(struct bpf_prog *prog)
1800{
1801}
1802
Martin KaFai Lau17bedab2016-12-07 15:53:11 -08001803bool __weak bpf_helper_changes_pkt_data(void *func)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001804{
1805 return false;
1806}
1807
Alexei Starovoitovf89b7752014-10-23 18:41:08 -07001808/* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
1809 * skb_copy_bits(), so provide a weak definition of it for NET-less config.
1810 */
1811int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
1812 int len)
1813{
1814 return -EFAULT;
1815}
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001816
1817/* All definitions of tracepoints related to BPF. */
1818#define CREATE_TRACE_POINTS
1819#include <linux/bpf_trace.h>
1820
1821EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception);
1822
Steven Rostedt (VMware)9185a612017-10-12 18:40:02 -04001823/* These are only used within the BPF_SYSCALL code */
1824#ifdef CONFIG_BPF_SYSCALL
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001825EXPORT_TRACEPOINT_SYMBOL_GPL(bpf_prog_get_type);
1826EXPORT_TRACEPOINT_SYMBOL_GPL(bpf_prog_put_rcu);
Steven Rostedt (VMware)9185a612017-10-12 18:40:02 -04001827#endif