blob: b2890c268cb340cab7424ef750ec47bcc5488ad4 [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 Borkmann050fad72018-05-17 01:44:11 +0200222static int bpf_adj_delta_to_imm(struct bpf_insn *insn, u32 pos, u32 delta,
223 u32 curr, const bool probe_pass)
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200224{
Daniel Borkmann050fad72018-05-17 01:44:11 +0200225 const s64 imm_min = S32_MIN, imm_max = S32_MAX;
226 s64 imm = insn->imm;
227
228 if (curr < pos && curr + imm + 1 > pos)
229 imm += delta;
230 else if (curr > pos + delta && curr + imm + 1 <= pos + delta)
231 imm -= delta;
232 if (imm < imm_min || imm > imm_max)
233 return -ERANGE;
234 if (!probe_pass)
235 insn->imm = imm;
236 return 0;
237}
238
239static int bpf_adj_delta_to_off(struct bpf_insn *insn, u32 pos, u32 delta,
240 u32 curr, const bool probe_pass)
241{
242 const s32 off_min = S16_MIN, off_max = S16_MAX;
243 s32 off = insn->off;
244
245 if (curr < pos && curr + off + 1 > pos)
246 off += delta;
247 else if (curr > pos + delta && curr + off + 1 <= pos + delta)
248 off -= delta;
249 if (off < off_min || off > off_max)
250 return -ERANGE;
251 if (!probe_pass)
252 insn->off = off;
253 return 0;
254}
255
256static int bpf_adj_branches(struct bpf_prog *prog, u32 pos, u32 delta,
257 const bool probe_pass)
258{
259 u32 i, insn_cnt = prog->len + (probe_pass ? delta : 0);
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200260 struct bpf_insn *insn = prog->insnsi;
Daniel Borkmann050fad72018-05-17 01:44:11 +0200261 int ret = 0;
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200262
263 for (i = 0; i < insn_cnt; i++, insn++) {
Daniel Borkmann050fad72018-05-17 01:44:11 +0200264 u8 code;
265
266 /* In the probing pass we still operate on the original,
267 * unpatched image in order to check overflows before we
268 * do any other adjustments. Therefore skip the patchlet.
269 */
270 if (probe_pass && i == pos) {
271 i += delta + 1;
272 insn++;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800273 }
Daniel Borkmann050fad72018-05-17 01:44:11 +0200274 code = insn->code;
275 if (BPF_CLASS(code) != BPF_JMP ||
276 BPF_OP(code) == BPF_EXIT)
277 continue;
278 /* Adjust offset of jmps if we cross patch boundaries. */
279 if (BPF_OP(code) == BPF_CALL) {
280 if (insn->src_reg != BPF_PSEUDO_CALL)
281 continue;
282 ret = bpf_adj_delta_to_imm(insn, pos, delta, i,
283 probe_pass);
284 } else {
285 ret = bpf_adj_delta_to_off(insn, pos, delta, i,
286 probe_pass);
287 }
288 if (ret)
289 break;
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200290 }
Daniel Borkmann050fad72018-05-17 01:44:11 +0200291
292 return ret;
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200293}
294
295struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
296 const struct bpf_insn *patch, u32 len)
297{
298 u32 insn_adj_cnt, insn_rest, insn_delta = len - 1;
Daniel Borkmann050fad72018-05-17 01:44:11 +0200299 const u32 cnt_max = S16_MAX;
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200300 struct bpf_prog *prog_adj;
301
302 /* Since our patchlet doesn't expand the image, we're done. */
303 if (insn_delta == 0) {
304 memcpy(prog->insnsi + off, patch, sizeof(*patch));
305 return prog;
306 }
307
308 insn_adj_cnt = prog->len + insn_delta;
309
Daniel Borkmann050fad72018-05-17 01:44:11 +0200310 /* Reject anything that would potentially let the insn->off
311 * target overflow when we have excessive program expansions.
312 * We need to probe here before we do any reallocation where
313 * we afterwards may not fail anymore.
314 */
315 if (insn_adj_cnt > cnt_max &&
316 bpf_adj_branches(prog, off, insn_delta, true))
317 return NULL;
318
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200319 /* Several new instructions need to be inserted. Make room
320 * for them. Likely, there's no need for a new allocation as
321 * last page could have large enough tailroom.
322 */
323 prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),
324 GFP_USER);
325 if (!prog_adj)
326 return NULL;
327
328 prog_adj->len = insn_adj_cnt;
329
330 /* Patching happens in 3 steps:
331 *
332 * 1) Move over tail of insnsi from next instruction onwards,
333 * so we can patch the single target insn with one or more
334 * new ones (patching is always from 1 to n insns, n > 0).
335 * 2) Inject new instructions at the target location.
336 * 3) Adjust branch offsets if necessary.
337 */
338 insn_rest = insn_adj_cnt - off - len;
339
340 memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1,
341 sizeof(*patch) * insn_rest);
342 memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len);
343
Daniel Borkmann050fad72018-05-17 01:44:11 +0200344 /* We are guaranteed to not fail at this point, otherwise
345 * the ship has sailed to reverse to the original state. An
346 * overflow cannot happen at this point.
347 */
348 BUG_ON(bpf_adj_branches(prog_adj, off, insn_delta, false));
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200349
350 return prog_adj;
351}
352
Daniel Borkmann7d1982b2018-06-15 02:30:47 +0200353void bpf_prog_kallsyms_del_subprogs(struct bpf_prog *fp)
354{
355 int i;
356
357 for (i = 0; i < fp->aux->func_cnt; i++)
358 bpf_prog_kallsyms_del(fp->aux->func[i]);
359}
360
361void bpf_prog_kallsyms_del_all(struct bpf_prog *fp)
362{
363 bpf_prog_kallsyms_del_subprogs(fp);
364 bpf_prog_kallsyms_del(fp);
365}
366
Daniel Borkmannb954d832014-09-10 15:01:02 +0200367#ifdef CONFIG_BPF_JIT
Daniel Borkmannfa9dd592018-01-20 01:24:33 +0100368/* All BPF JIT sysctl knobs here. */
369int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_ALWAYS_ON);
370int bpf_jit_harden __read_mostly;
371int bpf_jit_kallsyms __read_mostly;
Daniel Borkmannfdadd042018-12-11 12:14:12 +0100372long bpf_jit_limit __read_mostly;
Daniel Borkmannfa9dd592018-01-20 01:24:33 +0100373
Daniel Borkmann74451e662017-02-16 22:24:50 +0100374static __always_inline void
375bpf_get_prog_addr_region(const struct bpf_prog *prog,
376 unsigned long *symbol_start,
377 unsigned long *symbol_end)
378{
379 const struct bpf_binary_header *hdr = bpf_jit_binary_hdr(prog);
380 unsigned long addr = (unsigned long)hdr;
381
382 WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog));
383
384 *symbol_start = addr;
385 *symbol_end = addr + hdr->pages * PAGE_SIZE;
386}
387
388static void bpf_get_prog_name(const struct bpf_prog *prog, char *sym)
389{
Martin KaFai Lau368211f2017-10-05 21:52:13 -0700390 const char *end = sym + KSYM_NAME_LEN;
391
Daniel Borkmann74451e662017-02-16 22:24:50 +0100392 BUILD_BUG_ON(sizeof("bpf_prog_") +
Martin KaFai Lau368211f2017-10-05 21:52:13 -0700393 sizeof(prog->tag) * 2 +
394 /* name has been null terminated.
395 * We should need +1 for the '_' preceding
396 * the name. However, the null character
397 * is double counted between the name and the
398 * sizeof("bpf_prog_") above, so we omit
399 * the +1 here.
400 */
401 sizeof(prog->aux->name) > KSYM_NAME_LEN);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100402
403 sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_");
404 sym = bin2hex(sym, prog->tag, sizeof(prog->tag));
Martin KaFai Lau368211f2017-10-05 21:52:13 -0700405 if (prog->aux->name[0])
406 snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name);
407 else
408 *sym = 0;
Daniel Borkmann74451e662017-02-16 22:24:50 +0100409}
410
411static __always_inline unsigned long
412bpf_get_prog_addr_start(struct latch_tree_node *n)
413{
414 unsigned long symbol_start, symbol_end;
415 const struct bpf_prog_aux *aux;
416
417 aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
418 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
419
420 return symbol_start;
421}
422
423static __always_inline bool bpf_tree_less(struct latch_tree_node *a,
424 struct latch_tree_node *b)
425{
426 return bpf_get_prog_addr_start(a) < bpf_get_prog_addr_start(b);
427}
428
429static __always_inline int bpf_tree_comp(void *key, struct latch_tree_node *n)
430{
431 unsigned long val = (unsigned long)key;
432 unsigned long symbol_start, symbol_end;
433 const struct bpf_prog_aux *aux;
434
435 aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
436 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
437
438 if (val < symbol_start)
439 return -1;
440 if (val >= symbol_end)
441 return 1;
442
443 return 0;
444}
445
446static const struct latch_tree_ops bpf_tree_ops = {
447 .less = bpf_tree_less,
448 .comp = bpf_tree_comp,
449};
450
451static DEFINE_SPINLOCK(bpf_lock);
452static LIST_HEAD(bpf_kallsyms);
453static struct latch_tree_root bpf_tree __cacheline_aligned;
454
Daniel Borkmann74451e662017-02-16 22:24:50 +0100455static void bpf_prog_ksym_node_add(struct bpf_prog_aux *aux)
456{
457 WARN_ON_ONCE(!list_empty(&aux->ksym_lnode));
458 list_add_tail_rcu(&aux->ksym_lnode, &bpf_kallsyms);
459 latch_tree_insert(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
460}
461
462static void bpf_prog_ksym_node_del(struct bpf_prog_aux *aux)
463{
464 if (list_empty(&aux->ksym_lnode))
465 return;
466
467 latch_tree_erase(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
468 list_del_rcu(&aux->ksym_lnode);
469}
470
471static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp)
472{
473 return fp->jited && !bpf_prog_was_classic(fp);
474}
475
476static bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp)
477{
478 return list_empty(&fp->aux->ksym_lnode) ||
479 fp->aux->ksym_lnode.prev == LIST_POISON2;
480}
481
482void bpf_prog_kallsyms_add(struct bpf_prog *fp)
483{
Daniel Borkmann74451e662017-02-16 22:24:50 +0100484 if (!bpf_prog_kallsyms_candidate(fp) ||
485 !capable(CAP_SYS_ADMIN))
486 return;
487
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200488 spin_lock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100489 bpf_prog_ksym_node_add(fp->aux);
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200490 spin_unlock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100491}
492
493void bpf_prog_kallsyms_del(struct bpf_prog *fp)
494{
Daniel Borkmann74451e662017-02-16 22:24:50 +0100495 if (!bpf_prog_kallsyms_candidate(fp))
496 return;
497
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200498 spin_lock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100499 bpf_prog_ksym_node_del(fp->aux);
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200500 spin_unlock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100501}
502
503static struct bpf_prog *bpf_prog_kallsyms_find(unsigned long addr)
504{
505 struct latch_tree_node *n;
506
507 if (!bpf_jit_kallsyms_enabled())
508 return NULL;
509
510 n = latch_tree_find((void *)addr, &bpf_tree, &bpf_tree_ops);
511 return n ?
512 container_of(n, struct bpf_prog_aux, ksym_tnode)->prog :
513 NULL;
514}
515
516const char *__bpf_address_lookup(unsigned long addr, unsigned long *size,
517 unsigned long *off, char *sym)
518{
519 unsigned long symbol_start, symbol_end;
520 struct bpf_prog *prog;
521 char *ret = NULL;
522
523 rcu_read_lock();
524 prog = bpf_prog_kallsyms_find(addr);
525 if (prog) {
526 bpf_get_prog_addr_region(prog, &symbol_start, &symbol_end);
527 bpf_get_prog_name(prog, sym);
528
529 ret = sym;
530 if (size)
531 *size = symbol_end - symbol_start;
532 if (off)
533 *off = addr - symbol_start;
534 }
535 rcu_read_unlock();
536
537 return ret;
538}
539
540bool is_bpf_text_address(unsigned long addr)
541{
542 bool ret;
543
544 rcu_read_lock();
545 ret = bpf_prog_kallsyms_find(addr) != NULL;
546 rcu_read_unlock();
547
548 return ret;
549}
550
551int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
552 char *sym)
553{
Daniel Borkmann74451e662017-02-16 22:24:50 +0100554 struct bpf_prog_aux *aux;
555 unsigned int it = 0;
556 int ret = -ERANGE;
557
558 if (!bpf_jit_kallsyms_enabled())
559 return ret;
560
561 rcu_read_lock();
562 list_for_each_entry_rcu(aux, &bpf_kallsyms, ksym_lnode) {
563 if (it++ != symnum)
564 continue;
565
Daniel Borkmann74451e662017-02-16 22:24:50 +0100566 bpf_get_prog_name(aux->prog, sym);
567
Song Liudf073472018-11-02 10:16:15 -0700568 *value = (unsigned long)aux->prog->bpf_func;
Daniel Borkmann74451e662017-02-16 22:24:50 +0100569 *type = BPF_SYM_ELF_TYPE;
570
571 ret = 0;
572 break;
573 }
574 rcu_read_unlock();
575
576 return ret;
577}
578
Daniel Borkmannede95a632018-10-23 01:11:04 +0200579static atomic_long_t bpf_jit_current;
580
Daniel Borkmannfdadd042018-12-11 12:14:12 +0100581/* Can be overridden by an arch's JIT compiler if it has a custom,
582 * dedicated BPF backend memory area, or if neither of the two
583 * below apply.
584 */
585u64 __weak bpf_jit_alloc_exec_limit(void)
586{
Daniel Borkmannede95a632018-10-23 01:11:04 +0200587#if defined(MODULES_VADDR)
Daniel Borkmannfdadd042018-12-11 12:14:12 +0100588 return MODULES_END - MODULES_VADDR;
589#else
590 return VMALLOC_END - VMALLOC_START;
591#endif
592}
593
Daniel Borkmannede95a632018-10-23 01:11:04 +0200594static int __init bpf_jit_charge_init(void)
595{
596 /* Only used as heuristic here to derive limit. */
Daniel Borkmannfdadd042018-12-11 12:14:12 +0100597 bpf_jit_limit = min_t(u64, round_up(bpf_jit_alloc_exec_limit() >> 2,
598 PAGE_SIZE), LONG_MAX);
Daniel Borkmannede95a632018-10-23 01:11:04 +0200599 return 0;
600}
601pure_initcall(bpf_jit_charge_init);
Daniel Borkmannede95a632018-10-23 01:11:04 +0200602
603static int bpf_jit_charge_modmem(u32 pages)
604{
605 if (atomic_long_add_return(pages, &bpf_jit_current) >
606 (bpf_jit_limit >> PAGE_SHIFT)) {
607 if (!capable(CAP_SYS_ADMIN)) {
608 atomic_long_sub(pages, &bpf_jit_current);
609 return -EPERM;
610 }
611 }
612
613 return 0;
614}
615
616static void bpf_jit_uncharge_modmem(u32 pages)
617{
618 atomic_long_sub(pages, &bpf_jit_current);
619}
620
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200621struct bpf_binary_header *
622bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
623 unsigned int alignment,
624 bpf_jit_fill_hole_t bpf_fill_ill_insns)
625{
626 struct bpf_binary_header *hdr;
Daniel Borkmannede95a632018-10-23 01:11:04 +0200627 u32 size, hole, start, pages;
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200628
629 /* Most of BPF filters are really small, but if some of them
630 * fill a page, allow at least 128 extra bytes to insert a
631 * random section of illegal instructions.
632 */
633 size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
Daniel Borkmannede95a632018-10-23 01:11:04 +0200634 pages = size / PAGE_SIZE;
635
636 if (bpf_jit_charge_modmem(pages))
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200637 return NULL;
Daniel Borkmannede95a632018-10-23 01:11:04 +0200638 hdr = module_alloc(size);
639 if (!hdr) {
640 bpf_jit_uncharge_modmem(pages);
641 return NULL;
642 }
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200643
644 /* Fill space with illegal/arch-dep instructions. */
645 bpf_fill_ill_insns(hdr, size);
646
Daniel Borkmannede95a632018-10-23 01:11:04 +0200647 hdr->pages = pages;
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200648 hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
649 PAGE_SIZE - sizeof(*hdr));
Daniel Borkmannb7552e1b2016-05-18 14:14:28 +0200650 start = (get_random_int() % hole) & ~(alignment - 1);
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200651
652 /* Leave a random number of instructions before BPF code. */
653 *image_ptr = &hdr->image[start];
654
655 return hdr;
656}
657
658void bpf_jit_binary_free(struct bpf_binary_header *hdr)
659{
Daniel Borkmannede95a632018-10-23 01:11:04 +0200660 u32 pages = hdr->pages;
661
Rusty Russellbe1f2212015-01-20 09:07:05 +1030662 module_memfree(hdr);
Daniel Borkmannede95a632018-10-23 01:11:04 +0200663 bpf_jit_uncharge_modmem(pages);
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200664}
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200665
Daniel Borkmann74451e662017-02-16 22:24:50 +0100666/* This symbol is only overridden by archs that have different
667 * requirements than the usual eBPF JITs, f.e. when they only
668 * implement cBPF JIT, do not set images read-only, etc.
669 */
670void __weak bpf_jit_free(struct bpf_prog *fp)
671{
672 if (fp->jited) {
673 struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp);
674
675 bpf_jit_binary_unlock_ro(hdr);
676 bpf_jit_binary_free(hdr);
677
678 WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp));
679 }
680
681 bpf_prog_unlock_free(fp);
682}
683
Daniel Borkmanne2c95a62018-11-26 14:05:38 +0100684int bpf_jit_get_func_addr(const struct bpf_prog *prog,
685 const struct bpf_insn *insn, bool extra_pass,
686 u64 *func_addr, bool *func_addr_fixed)
687{
688 s16 off = insn->off;
689 s32 imm = insn->imm;
690 u8 *addr;
691
692 *func_addr_fixed = insn->src_reg != BPF_PSEUDO_CALL;
693 if (!*func_addr_fixed) {
694 /* Place-holder address till the last pass has collected
695 * all addresses for JITed subprograms in which case we
696 * can pick them up from prog->aux.
697 */
698 if (!extra_pass)
699 addr = NULL;
700 else if (prog->aux->func &&
701 off >= 0 && off < prog->aux->func_cnt)
702 addr = (u8 *)prog->aux->func[off]->bpf_func;
703 else
704 return -EINVAL;
705 } else {
706 /* Address of a BPF helper call. Since part of the core
707 * kernel, it's always at a fixed location. __bpf_call_base
708 * and the helper with imm relative to it are both in core
709 * kernel.
710 */
711 addr = (u8 *)__bpf_call_base + imm;
712 }
713
714 *func_addr = (unsigned long)addr;
715 return 0;
716}
717
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200718static int bpf_jit_blind_insn(const struct bpf_insn *from,
719 const struct bpf_insn *aux,
720 struct bpf_insn *to_buff)
721{
722 struct bpf_insn *to = to_buff;
Daniel Borkmannb7552e1b2016-05-18 14:14:28 +0200723 u32 imm_rnd = get_random_int();
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200724 s16 off;
725
726 BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG);
727 BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG);
728
729 if (from->imm == 0 &&
730 (from->code == (BPF_ALU | BPF_MOV | BPF_K) ||
731 from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) {
732 *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg);
733 goto out;
734 }
735
736 switch (from->code) {
737 case BPF_ALU | BPF_ADD | BPF_K:
738 case BPF_ALU | BPF_SUB | BPF_K:
739 case BPF_ALU | BPF_AND | BPF_K:
740 case BPF_ALU | BPF_OR | BPF_K:
741 case BPF_ALU | BPF_XOR | BPF_K:
742 case BPF_ALU | BPF_MUL | BPF_K:
743 case BPF_ALU | BPF_MOV | BPF_K:
744 case BPF_ALU | BPF_DIV | BPF_K:
745 case BPF_ALU | BPF_MOD | BPF_K:
746 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
747 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
748 *to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX);
749 break;
750
751 case BPF_ALU64 | BPF_ADD | BPF_K:
752 case BPF_ALU64 | BPF_SUB | BPF_K:
753 case BPF_ALU64 | BPF_AND | BPF_K:
754 case BPF_ALU64 | BPF_OR | BPF_K:
755 case BPF_ALU64 | BPF_XOR | BPF_K:
756 case BPF_ALU64 | BPF_MUL | BPF_K:
757 case BPF_ALU64 | BPF_MOV | BPF_K:
758 case BPF_ALU64 | BPF_DIV | BPF_K:
759 case BPF_ALU64 | BPF_MOD | BPF_K:
760 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
761 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
762 *to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX);
763 break;
764
765 case BPF_JMP | BPF_JEQ | BPF_K:
766 case BPF_JMP | BPF_JNE | BPF_K:
767 case BPF_JMP | BPF_JGT | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200768 case BPF_JMP | BPF_JLT | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200769 case BPF_JMP | BPF_JGE | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200770 case BPF_JMP | BPF_JLE | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200771 case BPF_JMP | BPF_JSGT | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200772 case BPF_JMP | BPF_JSLT | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200773 case BPF_JMP | BPF_JSGE | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200774 case BPF_JMP | BPF_JSLE | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200775 case BPF_JMP | BPF_JSET | BPF_K:
776 /* Accommodate for extra offset in case of a backjump. */
777 off = from->off;
778 if (off < 0)
779 off -= 2;
780 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
781 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
782 *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off);
783 break;
784
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200785 case BPF_LD | BPF_IMM | BPF_DW:
786 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm);
787 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
788 *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
789 *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX);
790 break;
791 case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */
792 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm);
793 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
794 *to++ = BPF_ALU64_REG(BPF_OR, aux[0].dst_reg, BPF_REG_AX);
795 break;
796
797 case BPF_ST | BPF_MEM | BPF_DW:
798 case BPF_ST | BPF_MEM | BPF_W:
799 case BPF_ST | BPF_MEM | BPF_H:
800 case BPF_ST | BPF_MEM | BPF_B:
801 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
802 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
803 *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off);
804 break;
805 }
806out:
807 return to - to_buff;
808}
809
810static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other,
811 gfp_t gfp_extra_flags)
812{
Michal Hocko19809c22017-05-08 15:57:44 -0700813 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200814 struct bpf_prog *fp;
815
816 fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags, PAGE_KERNEL);
817 if (fp != NULL) {
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200818 /* aux->prog still points to the fp_other one, so
819 * when promoting the clone to the real program,
820 * this still needs to be adapted.
821 */
822 memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE);
823 }
824
825 return fp;
826}
827
828static void bpf_prog_clone_free(struct bpf_prog *fp)
829{
830 /* aux was stolen by the other clone, so we cannot free
831 * it from this path! It will be freed eventually by the
832 * other program on release.
833 *
834 * At this point, we don't need a deferred release since
835 * clone is guaranteed to not be locked.
836 */
837 fp->aux = NULL;
838 __bpf_prog_free(fp);
839}
840
841void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other)
842{
843 /* We have to repoint aux->prog to self, as we don't
844 * know whether fp here is the clone or the original.
845 */
846 fp->aux->prog = fp;
847 bpf_prog_clone_free(fp_other);
848}
849
850struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
851{
852 struct bpf_insn insn_buff[16], aux[2];
853 struct bpf_prog *clone, *tmp;
854 int insn_delta, insn_cnt;
855 struct bpf_insn *insn;
856 int i, rewritten;
857
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -0800858 if (!bpf_jit_blinding_enabled(prog) || prog->blinded)
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200859 return prog;
860
861 clone = bpf_prog_clone_create(prog, GFP_USER);
862 if (!clone)
863 return ERR_PTR(-ENOMEM);
864
865 insn_cnt = clone->len;
866 insn = clone->insnsi;
867
868 for (i = 0; i < insn_cnt; i++, insn++) {
869 /* We temporarily need to hold the original ld64 insn
870 * so that we can still access the first part in the
871 * second blinding run.
872 */
873 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) &&
874 insn[1].code == 0)
875 memcpy(aux, insn, sizeof(aux));
876
877 rewritten = bpf_jit_blind_insn(insn, aux, insn_buff);
878 if (!rewritten)
879 continue;
880
881 tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);
882 if (!tmp) {
883 /* Patching may have repointed aux->prog during
884 * realloc from the original one, so we need to
885 * fix it up here on error.
886 */
887 bpf_jit_prog_release_other(prog, clone);
888 return ERR_PTR(-ENOMEM);
889 }
890
891 clone = tmp;
892 insn_delta = rewritten - 1;
893
894 /* Walk new program and skip insns we just inserted. */
895 insn = clone->insnsi + i + insn_delta;
896 insn_cnt += insn_delta;
897 i += insn_delta;
898 }
899
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -0800900 clone->blinded = 1;
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200901 return clone;
902}
Daniel Borkmannb954d832014-09-10 15:01:02 +0200903#endif /* CONFIG_BPF_JIT */
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200904
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700905/* Base function for offset calculation. Needs to go into .text section,
906 * therefore keeping it non-static as well; will also be used by JITs
Daniel Borkmann7105e822017-12-20 13:42:57 +0100907 * anyway later on, so do not let the compiler omit it. This also needs
908 * to go into kallsyms for correlation from e.g. bpftool, so naming
909 * must not change.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700910 */
911noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
912{
913 return 0;
914}
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -0700915EXPORT_SYMBOL_GPL(__bpf_call_base);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700916
Daniel Borkmann5e581da2018-01-26 23:33:38 +0100917/* All UAPI available opcodes. */
918#define BPF_INSN_MAP(INSN_2, INSN_3) \
919 /* 32 bit ALU operations. */ \
920 /* Register based. */ \
921 INSN_3(ALU, ADD, X), \
922 INSN_3(ALU, SUB, X), \
923 INSN_3(ALU, AND, X), \
924 INSN_3(ALU, OR, X), \
925 INSN_3(ALU, LSH, X), \
926 INSN_3(ALU, RSH, X), \
927 INSN_3(ALU, XOR, X), \
928 INSN_3(ALU, MUL, X), \
929 INSN_3(ALU, MOV, X), \
930 INSN_3(ALU, DIV, X), \
931 INSN_3(ALU, MOD, X), \
932 INSN_2(ALU, NEG), \
933 INSN_3(ALU, END, TO_BE), \
934 INSN_3(ALU, END, TO_LE), \
935 /* Immediate based. */ \
936 INSN_3(ALU, ADD, K), \
937 INSN_3(ALU, SUB, K), \
938 INSN_3(ALU, AND, K), \
939 INSN_3(ALU, OR, K), \
940 INSN_3(ALU, LSH, K), \
941 INSN_3(ALU, RSH, K), \
942 INSN_3(ALU, XOR, K), \
943 INSN_3(ALU, MUL, K), \
944 INSN_3(ALU, MOV, K), \
945 INSN_3(ALU, DIV, K), \
946 INSN_3(ALU, MOD, K), \
947 /* 64 bit ALU operations. */ \
948 /* Register based. */ \
949 INSN_3(ALU64, ADD, X), \
950 INSN_3(ALU64, SUB, X), \
951 INSN_3(ALU64, AND, X), \
952 INSN_3(ALU64, OR, X), \
953 INSN_3(ALU64, LSH, X), \
954 INSN_3(ALU64, RSH, X), \
955 INSN_3(ALU64, XOR, X), \
956 INSN_3(ALU64, MUL, X), \
957 INSN_3(ALU64, MOV, X), \
958 INSN_3(ALU64, ARSH, X), \
959 INSN_3(ALU64, DIV, X), \
960 INSN_3(ALU64, MOD, X), \
961 INSN_2(ALU64, NEG), \
962 /* Immediate based. */ \
963 INSN_3(ALU64, ADD, K), \
964 INSN_3(ALU64, SUB, K), \
965 INSN_3(ALU64, AND, K), \
966 INSN_3(ALU64, OR, K), \
967 INSN_3(ALU64, LSH, K), \
968 INSN_3(ALU64, RSH, K), \
969 INSN_3(ALU64, XOR, K), \
970 INSN_3(ALU64, MUL, K), \
971 INSN_3(ALU64, MOV, K), \
972 INSN_3(ALU64, ARSH, K), \
973 INSN_3(ALU64, DIV, K), \
974 INSN_3(ALU64, MOD, K), \
975 /* Call instruction. */ \
976 INSN_2(JMP, CALL), \
977 /* Exit instruction. */ \
978 INSN_2(JMP, EXIT), \
979 /* Jump instructions. */ \
980 /* Register based. */ \
981 INSN_3(JMP, JEQ, X), \
982 INSN_3(JMP, JNE, X), \
983 INSN_3(JMP, JGT, X), \
984 INSN_3(JMP, JLT, X), \
985 INSN_3(JMP, JGE, X), \
986 INSN_3(JMP, JLE, X), \
987 INSN_3(JMP, JSGT, X), \
988 INSN_3(JMP, JSLT, X), \
989 INSN_3(JMP, JSGE, X), \
990 INSN_3(JMP, JSLE, X), \
991 INSN_3(JMP, JSET, X), \
992 /* Immediate based. */ \
993 INSN_3(JMP, JEQ, K), \
994 INSN_3(JMP, JNE, K), \
995 INSN_3(JMP, JGT, K), \
996 INSN_3(JMP, JLT, K), \
997 INSN_3(JMP, JGE, K), \
998 INSN_3(JMP, JLE, K), \
999 INSN_3(JMP, JSGT, K), \
1000 INSN_3(JMP, JSLT, K), \
1001 INSN_3(JMP, JSGE, K), \
1002 INSN_3(JMP, JSLE, K), \
1003 INSN_3(JMP, JSET, K), \
1004 INSN_2(JMP, JA), \
1005 /* Store instructions. */ \
1006 /* Register based. */ \
1007 INSN_3(STX, MEM, B), \
1008 INSN_3(STX, MEM, H), \
1009 INSN_3(STX, MEM, W), \
1010 INSN_3(STX, MEM, DW), \
1011 INSN_3(STX, XADD, W), \
1012 INSN_3(STX, XADD, DW), \
1013 /* Immediate based. */ \
1014 INSN_3(ST, MEM, B), \
1015 INSN_3(ST, MEM, H), \
1016 INSN_3(ST, MEM, W), \
1017 INSN_3(ST, MEM, DW), \
1018 /* Load instructions. */ \
1019 /* Register based. */ \
1020 INSN_3(LDX, MEM, B), \
1021 INSN_3(LDX, MEM, H), \
1022 INSN_3(LDX, MEM, W), \
1023 INSN_3(LDX, MEM, DW), \
1024 /* Immediate based. */ \
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +02001025 INSN_3(LD, IMM, DW)
Daniel Borkmann5e581da2018-01-26 23:33:38 +01001026
1027bool bpf_opcode_in_insntable(u8 code)
1028{
1029#define BPF_INSN_2_TBL(x, y) [BPF_##x | BPF_##y] = true
1030#define BPF_INSN_3_TBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = true
1031 static const bool public_insntable[256] = {
1032 [0 ... 255] = false,
1033 /* Now overwrite non-defaults ... */
1034 BPF_INSN_MAP(BPF_INSN_2_TBL, BPF_INSN_3_TBL),
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +02001035 /* UAPI exposed, but rewritten opcodes. cBPF carry-over. */
1036 [BPF_LD | BPF_ABS | BPF_B] = true,
1037 [BPF_LD | BPF_ABS | BPF_H] = true,
1038 [BPF_LD | BPF_ABS | BPF_W] = true,
1039 [BPF_LD | BPF_IND | BPF_B] = true,
1040 [BPF_LD | BPF_IND | BPF_H] = true,
1041 [BPF_LD | BPF_IND | BPF_W] = true,
Daniel Borkmann5e581da2018-01-26 23:33:38 +01001042 };
1043#undef BPF_INSN_3_TBL
1044#undef BPF_INSN_2_TBL
1045 return public_insntable[code];
1046}
1047
Alexei Starovoitov290af862018-01-09 10:04:29 -08001048#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001049/**
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001050 * __bpf_prog_run - run eBPF program on a given context
1051 * @ctx: is the data we are operating on
1052 * @insn: is the array of eBPF instructions
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001053 *
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001054 * Decode and execute eBPF instructions.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001055 */
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001056static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001057{
Alexei Starovoitovf696b8f2017-05-30 13:31:28 -07001058 u64 tmp;
Daniel Borkmann5e581da2018-01-26 23:33:38 +01001059#define BPF_INSN_2_LBL(x, y) [BPF_##x | BPF_##y] = &&x##_##y
1060#define BPF_INSN_3_LBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = &&x##_##y##_##z
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001061 static const void *jumptable[256] = {
1062 [0 ... 255] = &&default_label,
1063 /* Now overwrite non-defaults ... */
Daniel Borkmann5e581da2018-01-26 23:33:38 +01001064 BPF_INSN_MAP(BPF_INSN_2_LBL, BPF_INSN_3_LBL),
1065 /* Non-UAPI available opcodes. */
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001066 [BPF_JMP | BPF_CALL_ARGS] = &&JMP_CALL_ARGS,
Alexei Starovoitov71189fa2017-05-30 13:31:27 -07001067 [BPF_JMP | BPF_TAIL_CALL] = &&JMP_TAIL_CALL,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001068 };
Daniel Borkmann5e581da2018-01-26 23:33:38 +01001069#undef BPF_INSN_3_LBL
1070#undef BPF_INSN_2_LBL
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001071 u32 tail_call_cnt = 0;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001072
1073#define CONT ({ insn++; goto select_insn; })
1074#define CONT_JMP ({ insn++; goto select_insn; })
1075
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001076select_insn:
1077 goto *jumptable[insn->code];
1078
1079 /* ALU */
1080#define ALU(OPCODE, OP) \
1081 ALU64_##OPCODE##_X: \
1082 DST = DST OP SRC; \
1083 CONT; \
1084 ALU_##OPCODE##_X: \
1085 DST = (u32) DST OP (u32) SRC; \
1086 CONT; \
1087 ALU64_##OPCODE##_K: \
1088 DST = DST OP IMM; \
1089 CONT; \
1090 ALU_##OPCODE##_K: \
1091 DST = (u32) DST OP (u32) IMM; \
1092 CONT;
1093
1094 ALU(ADD, +)
1095 ALU(SUB, -)
1096 ALU(AND, &)
1097 ALU(OR, |)
1098 ALU(LSH, <<)
1099 ALU(RSH, >>)
1100 ALU(XOR, ^)
1101 ALU(MUL, *)
1102#undef ALU
1103 ALU_NEG:
1104 DST = (u32) -DST;
1105 CONT;
1106 ALU64_NEG:
1107 DST = -DST;
1108 CONT;
1109 ALU_MOV_X:
1110 DST = (u32) SRC;
1111 CONT;
1112 ALU_MOV_K:
1113 DST = (u32) IMM;
1114 CONT;
1115 ALU64_MOV_X:
1116 DST = SRC;
1117 CONT;
1118 ALU64_MOV_K:
1119 DST = IMM;
1120 CONT;
Alexei Starovoitov02ab6952014-09-04 22:17:17 -07001121 LD_IMM_DW:
1122 DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
1123 insn++;
1124 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001125 ALU64_ARSH_X:
1126 (*(s64 *) &DST) >>= SRC;
1127 CONT;
1128 ALU64_ARSH_K:
1129 (*(s64 *) &DST) >>= IMM;
1130 CONT;
1131 ALU64_MOD_X:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -07001132 div64_u64_rem(DST, SRC, &tmp);
1133 DST = tmp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001134 CONT;
1135 ALU_MOD_X:
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001136 tmp = (u32) DST;
1137 DST = do_div(tmp, (u32) SRC);
1138 CONT;
1139 ALU64_MOD_K:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -07001140 div64_u64_rem(DST, IMM, &tmp);
1141 DST = tmp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001142 CONT;
1143 ALU_MOD_K:
1144 tmp = (u32) DST;
1145 DST = do_div(tmp, (u32) IMM);
1146 CONT;
1147 ALU64_DIV_X:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -07001148 DST = div64_u64(DST, SRC);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001149 CONT;
1150 ALU_DIV_X:
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001151 tmp = (u32) DST;
1152 do_div(tmp, (u32) SRC);
1153 DST = (u32) tmp;
1154 CONT;
1155 ALU64_DIV_K:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -07001156 DST = div64_u64(DST, IMM);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001157 CONT;
1158 ALU_DIV_K:
1159 tmp = (u32) DST;
1160 do_div(tmp, (u32) IMM);
1161 DST = (u32) tmp;
1162 CONT;
1163 ALU_END_TO_BE:
1164 switch (IMM) {
1165 case 16:
1166 DST = (__force u16) cpu_to_be16(DST);
1167 break;
1168 case 32:
1169 DST = (__force u32) cpu_to_be32(DST);
1170 break;
1171 case 64:
1172 DST = (__force u64) cpu_to_be64(DST);
1173 break;
1174 }
1175 CONT;
1176 ALU_END_TO_LE:
1177 switch (IMM) {
1178 case 16:
1179 DST = (__force u16) cpu_to_le16(DST);
1180 break;
1181 case 32:
1182 DST = (__force u32) cpu_to_le32(DST);
1183 break;
1184 case 64:
1185 DST = (__force u64) cpu_to_le64(DST);
1186 break;
1187 }
1188 CONT;
1189
1190 /* CALL */
1191 JMP_CALL:
1192 /* Function call scratches BPF_R1-BPF_R5 registers,
1193 * preserves BPF_R6-BPF_R9, and stores return value
1194 * into BPF_R0.
1195 */
1196 BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
1197 BPF_R4, BPF_R5);
1198 CONT;
1199
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001200 JMP_CALL_ARGS:
1201 BPF_R0 = (__bpf_call_base_args + insn->imm)(BPF_R1, BPF_R2,
1202 BPF_R3, BPF_R4,
1203 BPF_R5,
1204 insn + insn->off + 1);
1205 CONT;
1206
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001207 JMP_TAIL_CALL: {
1208 struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
1209 struct bpf_array *array = container_of(map, struct bpf_array, map);
1210 struct bpf_prog *prog;
Alexei Starovoitov90caccd2017-10-03 15:37:20 -07001211 u32 index = BPF_R3;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001212
1213 if (unlikely(index >= array->map.max_entries))
1214 goto out;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001215 if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
1216 goto out;
1217
1218 tail_call_cnt++;
1219
Wang Nan2a36f0b2015-08-06 07:02:33 +00001220 prog = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +02001221 if (!prog)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001222 goto out;
1223
Daniel Borkmannc4675f92015-07-13 20:49:32 +02001224 /* ARG1 at this point is guaranteed to point to CTX from
1225 * the verifier side due to the fact that the tail call is
1226 * handeled like a helper, that is, bpf_tail_call_proto,
1227 * where arg1_type is ARG_PTR_TO_CTX.
1228 */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001229 insn = prog->insnsi;
1230 goto select_insn;
1231out:
1232 CONT;
1233 }
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001234 /* JMP */
1235 JMP_JA:
1236 insn += insn->off;
1237 CONT;
1238 JMP_JEQ_X:
1239 if (DST == SRC) {
1240 insn += insn->off;
1241 CONT_JMP;
1242 }
1243 CONT;
1244 JMP_JEQ_K:
1245 if (DST == IMM) {
1246 insn += insn->off;
1247 CONT_JMP;
1248 }
1249 CONT;
1250 JMP_JNE_X:
1251 if (DST != SRC) {
1252 insn += insn->off;
1253 CONT_JMP;
1254 }
1255 CONT;
1256 JMP_JNE_K:
1257 if (DST != IMM) {
1258 insn += insn->off;
1259 CONT_JMP;
1260 }
1261 CONT;
1262 JMP_JGT_X:
1263 if (DST > SRC) {
1264 insn += insn->off;
1265 CONT_JMP;
1266 }
1267 CONT;
1268 JMP_JGT_K:
1269 if (DST > IMM) {
1270 insn += insn->off;
1271 CONT_JMP;
1272 }
1273 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001274 JMP_JLT_X:
1275 if (DST < SRC) {
1276 insn += insn->off;
1277 CONT_JMP;
1278 }
1279 CONT;
1280 JMP_JLT_K:
1281 if (DST < IMM) {
1282 insn += insn->off;
1283 CONT_JMP;
1284 }
1285 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001286 JMP_JGE_X:
1287 if (DST >= SRC) {
1288 insn += insn->off;
1289 CONT_JMP;
1290 }
1291 CONT;
1292 JMP_JGE_K:
1293 if (DST >= IMM) {
1294 insn += insn->off;
1295 CONT_JMP;
1296 }
1297 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001298 JMP_JLE_X:
1299 if (DST <= SRC) {
1300 insn += insn->off;
1301 CONT_JMP;
1302 }
1303 CONT;
1304 JMP_JLE_K:
1305 if (DST <= IMM) {
1306 insn += insn->off;
1307 CONT_JMP;
1308 }
1309 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001310 JMP_JSGT_X:
1311 if (((s64) DST) > ((s64) SRC)) {
1312 insn += insn->off;
1313 CONT_JMP;
1314 }
1315 CONT;
1316 JMP_JSGT_K:
1317 if (((s64) DST) > ((s64) IMM)) {
1318 insn += insn->off;
1319 CONT_JMP;
1320 }
1321 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001322 JMP_JSLT_X:
1323 if (((s64) DST) < ((s64) SRC)) {
1324 insn += insn->off;
1325 CONT_JMP;
1326 }
1327 CONT;
1328 JMP_JSLT_K:
1329 if (((s64) DST) < ((s64) IMM)) {
1330 insn += insn->off;
1331 CONT_JMP;
1332 }
1333 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001334 JMP_JSGE_X:
1335 if (((s64) DST) >= ((s64) SRC)) {
1336 insn += insn->off;
1337 CONT_JMP;
1338 }
1339 CONT;
1340 JMP_JSGE_K:
1341 if (((s64) DST) >= ((s64) IMM)) {
1342 insn += insn->off;
1343 CONT_JMP;
1344 }
1345 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001346 JMP_JSLE_X:
1347 if (((s64) DST) <= ((s64) SRC)) {
1348 insn += insn->off;
1349 CONT_JMP;
1350 }
1351 CONT;
1352 JMP_JSLE_K:
1353 if (((s64) DST) <= ((s64) IMM)) {
1354 insn += insn->off;
1355 CONT_JMP;
1356 }
1357 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001358 JMP_JSET_X:
1359 if (DST & SRC) {
1360 insn += insn->off;
1361 CONT_JMP;
1362 }
1363 CONT;
1364 JMP_JSET_K:
1365 if (DST & IMM) {
1366 insn += insn->off;
1367 CONT_JMP;
1368 }
1369 CONT;
1370 JMP_EXIT:
1371 return BPF_R0;
1372
1373 /* STX and ST and LDX*/
1374#define LDST(SIZEOP, SIZE) \
1375 STX_MEM_##SIZEOP: \
1376 *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \
1377 CONT; \
1378 ST_MEM_##SIZEOP: \
1379 *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \
1380 CONT; \
1381 LDX_MEM_##SIZEOP: \
1382 DST = *(SIZE *)(unsigned long) (SRC + insn->off); \
1383 CONT;
1384
1385 LDST(B, u8)
1386 LDST(H, u16)
1387 LDST(W, u32)
1388 LDST(DW, u64)
1389#undef LDST
1390 STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
1391 atomic_add((u32) SRC, (atomic_t *)(unsigned long)
1392 (DST + insn->off));
1393 CONT;
1394 STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
1395 atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
1396 (DST + insn->off));
1397 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001398
1399 default_label:
Daniel Borkmann5e581da2018-01-26 23:33:38 +01001400 /* If we ever reach this, we have a bug somewhere. Die hard here
1401 * instead of just returning 0; we could be somewhere in a subprog,
1402 * so execution could continue otherwise which we do /not/ want.
1403 *
1404 * Note, verifier whitelists all opcodes in bpf_opcode_in_insntable().
1405 */
1406 pr_warn("BPF interpreter: unknown opcode %02x\n", insn->code);
1407 BUG_ON(1);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001408 return 0;
1409}
Alexei Starovoitovf696b8f2017-05-30 13:31:28 -07001410STACK_FRAME_NON_STANDARD(___bpf_prog_run); /* jump table */
1411
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001412#define PROG_NAME(stack_size) __bpf_prog_run##stack_size
1413#define DEFINE_BPF_PROG_RUN(stack_size) \
1414static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \
1415{ \
1416 u64 stack[stack_size / sizeof(u64)]; \
1417 u64 regs[MAX_BPF_REG]; \
1418\
1419 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1420 ARG1 = (u64) (unsigned long) ctx; \
1421 return ___bpf_prog_run(regs, insn, stack); \
Alexei Starovoitovf696b8f2017-05-30 13:31:28 -07001422}
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001423
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001424#define PROG_NAME_ARGS(stack_size) __bpf_prog_run_args##stack_size
1425#define DEFINE_BPF_PROG_RUN_ARGS(stack_size) \
1426static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \
1427 const struct bpf_insn *insn) \
1428{ \
1429 u64 stack[stack_size / sizeof(u64)]; \
1430 u64 regs[MAX_BPF_REG]; \
1431\
1432 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1433 BPF_R1 = r1; \
1434 BPF_R2 = r2; \
1435 BPF_R3 = r3; \
1436 BPF_R4 = r4; \
1437 BPF_R5 = r5; \
1438 return ___bpf_prog_run(regs, insn, stack); \
1439}
1440
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001441#define EVAL1(FN, X) FN(X)
1442#define EVAL2(FN, X, Y...) FN(X) EVAL1(FN, Y)
1443#define EVAL3(FN, X, Y...) FN(X) EVAL2(FN, Y)
1444#define EVAL4(FN, X, Y...) FN(X) EVAL3(FN, Y)
1445#define EVAL5(FN, X, Y...) FN(X) EVAL4(FN, Y)
1446#define EVAL6(FN, X, Y...) FN(X) EVAL5(FN, Y)
1447
1448EVAL6(DEFINE_BPF_PROG_RUN, 32, 64, 96, 128, 160, 192);
1449EVAL6(DEFINE_BPF_PROG_RUN, 224, 256, 288, 320, 352, 384);
1450EVAL4(DEFINE_BPF_PROG_RUN, 416, 448, 480, 512);
1451
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001452EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 32, 64, 96, 128, 160, 192);
1453EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 224, 256, 288, 320, 352, 384);
1454EVAL4(DEFINE_BPF_PROG_RUN_ARGS, 416, 448, 480, 512);
1455
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001456#define PROG_NAME_LIST(stack_size) PROG_NAME(stack_size),
1457
1458static unsigned int (*interpreters[])(const void *ctx,
1459 const struct bpf_insn *insn) = {
1460EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1461EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1462EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1463};
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001464#undef PROG_NAME_LIST
1465#define PROG_NAME_LIST(stack_size) PROG_NAME_ARGS(stack_size),
1466static u64 (*interpreters_args[])(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5,
1467 const struct bpf_insn *insn) = {
1468EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1469EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1470EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1471};
1472#undef PROG_NAME_LIST
1473
1474void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth)
1475{
1476 stack_depth = max_t(u32, stack_depth, 1);
1477 insn->off = (s16) insn->imm;
1478 insn->imm = interpreters_args[(round_up(stack_depth, 32) / 32) - 1] -
1479 __bpf_call_base_args;
1480 insn->code = BPF_JMP | BPF_CALL_ARGS;
1481}
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001482
Alexei Starovoitov290af862018-01-09 10:04:29 -08001483#else
Daniel Borkmannfa9dd592018-01-20 01:24:33 +01001484static unsigned int __bpf_prog_ret0_warn(const void *ctx,
1485 const struct bpf_insn *insn)
Alexei Starovoitov290af862018-01-09 10:04:29 -08001486{
Daniel Borkmannfa9dd592018-01-20 01:24:33 +01001487 /* If this handler ever gets executed, then BPF_JIT_ALWAYS_ON
1488 * is not working properly, so warn about it!
1489 */
1490 WARN_ON_ONCE(1);
Alexei Starovoitov290af862018-01-09 10:04:29 -08001491 return 0;
1492}
1493#endif
1494
Daniel Borkmann3324b582015-05-29 23:23:07 +02001495bool bpf_prog_array_compatible(struct bpf_array *array,
1496 const struct bpf_prog *fp)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001497{
Josef Bacik9802d862017-12-11 11:36:48 -05001498 if (fp->kprobe_override)
1499 return false;
1500
Daniel Borkmann3324b582015-05-29 23:23:07 +02001501 if (!array->owner_prog_type) {
1502 /* There's no owner yet where we could check for
1503 * compatibility.
1504 */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001505 array->owner_prog_type = fp->type;
1506 array->owner_jited = fp->jited;
Daniel Borkmann3324b582015-05-29 23:23:07 +02001507
1508 return true;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001509 }
Daniel Borkmann3324b582015-05-29 23:23:07 +02001510
1511 return array->owner_prog_type == fp->type &&
1512 array->owner_jited == fp->jited;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001513}
1514
Daniel Borkmann3324b582015-05-29 23:23:07 +02001515static int bpf_check_tail_call(const struct bpf_prog *fp)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001516{
1517 struct bpf_prog_aux *aux = fp->aux;
1518 int i;
1519
1520 for (i = 0; i < aux->used_map_cnt; i++) {
Daniel Borkmann3324b582015-05-29 23:23:07 +02001521 struct bpf_map *map = aux->used_maps[i];
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001522 struct bpf_array *array;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001523
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001524 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1525 continue;
Daniel Borkmann3324b582015-05-29 23:23:07 +02001526
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001527 array = container_of(map, struct bpf_array, map);
1528 if (!bpf_prog_array_compatible(array, fp))
1529 return -EINVAL;
1530 }
1531
1532 return 0;
1533}
1534
Daniel Borkmann9facc332018-06-15 02:30:48 +02001535static void bpf_prog_select_func(struct bpf_prog *fp)
1536{
1537#ifndef CONFIG_BPF_JIT_ALWAYS_ON
1538 u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1);
1539
1540 fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1];
1541#else
1542 fp->bpf_func = __bpf_prog_ret0_warn;
1543#endif
1544}
1545
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001546/**
Daniel Borkmann3324b582015-05-29 23:23:07 +02001547 * bpf_prog_select_runtime - select exec runtime for BPF program
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001548 * @fp: bpf_prog populated with internal BPF program
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001549 * @err: pointer to error variable
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001550 *
Daniel Borkmann3324b582015-05-29 23:23:07 +02001551 * Try to JIT eBPF program, if JIT is not available, use interpreter.
1552 * The BPF program will be executed via BPF_PROG_RUN() macro.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001553 */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001554struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001555{
Daniel Borkmann9facc332018-06-15 02:30:48 +02001556 /* In case of BPF to BPF calls, verifier did all the prep
1557 * work with regards to JITing, etc.
1558 */
1559 if (fp->bpf_func)
1560 goto finalize;
Martin KaFai Lau8007e402017-06-28 10:41:24 -07001561
Daniel Borkmann9facc332018-06-15 02:30:48 +02001562 bpf_prog_select_func(fp);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001563
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001564 /* eBPF JITs can rewrite the program in case constant
1565 * blinding is active. However, in case of error during
1566 * blinding, bpf_int_jit_compile() must always return a
1567 * valid program, which in this case would simply not
1568 * be JITed, but falls back to the interpreter.
1569 */
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001570 if (!bpf_prog_is_dev_bound(fp->aux)) {
1571 fp = bpf_int_jit_compile(fp);
Alexei Starovoitov290af862018-01-09 10:04:29 -08001572#ifdef CONFIG_BPF_JIT_ALWAYS_ON
1573 if (!fp->jited) {
1574 *err = -ENOTSUPP;
1575 return fp;
1576 }
1577#endif
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001578 } else {
1579 *err = bpf_prog_offload_compile(fp);
1580 if (*err)
1581 return fp;
1582 }
Daniel Borkmann9facc332018-06-15 02:30:48 +02001583
1584finalize:
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001585 bpf_prog_lock_ro(fp);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001586
Daniel Borkmann3324b582015-05-29 23:23:07 +02001587 /* The tail call compatibility check can only be done at
1588 * this late stage as we need to determine, if we deal
1589 * with JITed or non JITed program concatenations and not
1590 * all eBPF JITs might immediately support all features.
1591 */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001592 *err = bpf_check_tail_call(fp);
1593
1594 return fp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001595}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001596EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001597
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001598static unsigned int __bpf_prog_ret1(const void *ctx,
1599 const struct bpf_insn *insn)
1600{
1601 return 1;
1602}
1603
1604static struct bpf_prog_dummy {
1605 struct bpf_prog prog;
1606} dummy_bpf_prog = {
1607 .prog = {
1608 .bpf_func = __bpf_prog_ret1,
1609 },
1610};
1611
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001612/* to avoid allocating empty bpf_prog_array for cgroups that
1613 * don't have bpf program attached use one global 'empty_prog_array'
1614 * It will not be modified the caller of bpf_prog_array_alloc()
1615 * (since caller requested prog_cnt == 0)
1616 * that pointer should be 'freed' by bpf_prog_array_free()
1617 */
1618static struct {
1619 struct bpf_prog_array hdr;
1620 struct bpf_prog *null_prog;
1621} empty_prog_array = {
1622 .null_prog = NULL,
1623};
1624
Roman Gushchind29ab6e2018-07-13 12:41:10 -07001625struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags)
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001626{
1627 if (prog_cnt)
1628 return kzalloc(sizeof(struct bpf_prog_array) +
Roman Gushchin394e40a2018-08-02 14:27:21 -07001629 sizeof(struct bpf_prog_array_item) *
1630 (prog_cnt + 1),
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001631 flags);
1632
1633 return &empty_prog_array.hdr;
1634}
1635
1636void bpf_prog_array_free(struct bpf_prog_array __rcu *progs)
1637{
1638 if (!progs ||
1639 progs == (struct bpf_prog_array __rcu *)&empty_prog_array.hdr)
1640 return;
1641 kfree_rcu(progs, rcu);
1642}
1643
Roman Gushchin394e40a2018-08-02 14:27:21 -07001644int bpf_prog_array_length(struct bpf_prog_array __rcu *array)
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001645{
Roman Gushchin394e40a2018-08-02 14:27:21 -07001646 struct bpf_prog_array_item *item;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001647 u32 cnt = 0;
1648
1649 rcu_read_lock();
Roman Gushchin394e40a2018-08-02 14:27:21 -07001650 item = rcu_dereference(array)->items;
1651 for (; item->prog; item++)
1652 if (item->prog != &dummy_bpf_prog.prog)
Yonghong Songc8c088b2017-11-30 13:47:54 -08001653 cnt++;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001654 rcu_read_unlock();
1655 return cnt;
1656}
1657
Roman Gushchin394e40a2018-08-02 14:27:21 -07001658
1659static bool bpf_prog_array_copy_core(struct bpf_prog_array __rcu *array,
Yonghong Song3a38bb92018-04-10 09:37:32 -07001660 u32 *prog_ids,
1661 u32 request_cnt)
1662{
Roman Gushchin394e40a2018-08-02 14:27:21 -07001663 struct bpf_prog_array_item *item;
Yonghong Song3a38bb92018-04-10 09:37:32 -07001664 int i = 0;
1665
Yonghong Song965931e2018-08-14 11:01:12 -07001666 item = rcu_dereference_check(array, 1)->items;
Roman Gushchin394e40a2018-08-02 14:27:21 -07001667 for (; item->prog; item++) {
1668 if (item->prog == &dummy_bpf_prog.prog)
Yonghong Song3a38bb92018-04-10 09:37:32 -07001669 continue;
Roman Gushchin394e40a2018-08-02 14:27:21 -07001670 prog_ids[i] = item->prog->aux->id;
Yonghong Song3a38bb92018-04-10 09:37:32 -07001671 if (++i == request_cnt) {
Roman Gushchin394e40a2018-08-02 14:27:21 -07001672 item++;
Yonghong Song3a38bb92018-04-10 09:37:32 -07001673 break;
1674 }
1675 }
1676
Roman Gushchin394e40a2018-08-02 14:27:21 -07001677 return !!(item->prog);
Yonghong Song3a38bb92018-04-10 09:37:32 -07001678}
1679
Roman Gushchin394e40a2018-08-02 14:27:21 -07001680int bpf_prog_array_copy_to_user(struct bpf_prog_array __rcu *array,
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001681 __u32 __user *prog_ids, u32 cnt)
1682{
Alexei Starovoitov09112872018-02-02 15:14:05 -08001683 unsigned long err = 0;
Alexei Starovoitov09112872018-02-02 15:14:05 -08001684 bool nospc;
Yonghong Song3a38bb92018-04-10 09:37:32 -07001685 u32 *ids;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001686
Alexei Starovoitov09112872018-02-02 15:14:05 -08001687 /* users of this function are doing:
1688 * cnt = bpf_prog_array_length();
1689 * if (cnt > 0)
1690 * bpf_prog_array_copy_to_user(..., cnt);
1691 * so below kcalloc doesn't need extra cnt > 0 check, but
1692 * bpf_prog_array_length() releases rcu lock and
1693 * prog array could have been swapped with empty or larger array,
1694 * so always copy 'cnt' prog_ids to the user.
1695 * In a rare race the user will see zero prog_ids
1696 */
Daniel Borkmann9c481b92018-02-14 15:31:00 +01001697 ids = kcalloc(cnt, sizeof(u32), GFP_USER | __GFP_NOWARN);
Alexei Starovoitov09112872018-02-02 15:14:05 -08001698 if (!ids)
1699 return -ENOMEM;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001700 rcu_read_lock();
Roman Gushchin394e40a2018-08-02 14:27:21 -07001701 nospc = bpf_prog_array_copy_core(array, ids, cnt);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001702 rcu_read_unlock();
Alexei Starovoitov09112872018-02-02 15:14:05 -08001703 err = copy_to_user(prog_ids, ids, cnt * sizeof(u32));
1704 kfree(ids);
1705 if (err)
1706 return -EFAULT;
1707 if (nospc)
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001708 return -ENOSPC;
1709 return 0;
1710}
1711
Roman Gushchin394e40a2018-08-02 14:27:21 -07001712void bpf_prog_array_delete_safe(struct bpf_prog_array __rcu *array,
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001713 struct bpf_prog *old_prog)
1714{
Roman Gushchin394e40a2018-08-02 14:27:21 -07001715 struct bpf_prog_array_item *item = array->items;
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001716
Roman Gushchin394e40a2018-08-02 14:27:21 -07001717 for (; item->prog; item++)
1718 if (item->prog == old_prog) {
1719 WRITE_ONCE(item->prog, &dummy_bpf_prog.prog);
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001720 break;
1721 }
1722}
1723
1724int bpf_prog_array_copy(struct bpf_prog_array __rcu *old_array,
1725 struct bpf_prog *exclude_prog,
1726 struct bpf_prog *include_prog,
1727 struct bpf_prog_array **new_array)
1728{
1729 int new_prog_cnt, carry_prog_cnt = 0;
Roman Gushchin394e40a2018-08-02 14:27:21 -07001730 struct bpf_prog_array_item *existing;
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001731 struct bpf_prog_array *array;
Sean Young170a7e32018-05-27 12:24:08 +01001732 bool found_exclude = false;
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001733 int new_prog_idx = 0;
1734
1735 /* Figure out how many existing progs we need to carry over to
1736 * the new array.
1737 */
1738 if (old_array) {
Roman Gushchin394e40a2018-08-02 14:27:21 -07001739 existing = old_array->items;
1740 for (; existing->prog; existing++) {
1741 if (existing->prog == exclude_prog) {
Sean Young170a7e32018-05-27 12:24:08 +01001742 found_exclude = true;
1743 continue;
1744 }
Roman Gushchin394e40a2018-08-02 14:27:21 -07001745 if (existing->prog != &dummy_bpf_prog.prog)
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001746 carry_prog_cnt++;
Roman Gushchin394e40a2018-08-02 14:27:21 -07001747 if (existing->prog == include_prog)
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001748 return -EEXIST;
1749 }
1750 }
1751
Sean Young170a7e32018-05-27 12:24:08 +01001752 if (exclude_prog && !found_exclude)
1753 return -ENOENT;
1754
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001755 /* How many progs (not NULL) will be in the new array? */
1756 new_prog_cnt = carry_prog_cnt;
1757 if (include_prog)
1758 new_prog_cnt += 1;
1759
1760 /* Do we have any prog (not NULL) in the new array? */
1761 if (!new_prog_cnt) {
1762 *new_array = NULL;
1763 return 0;
1764 }
1765
1766 /* +1 as the end of prog_array is marked with NULL */
1767 array = bpf_prog_array_alloc(new_prog_cnt + 1, GFP_KERNEL);
1768 if (!array)
1769 return -ENOMEM;
1770
1771 /* Fill in the new prog array */
1772 if (carry_prog_cnt) {
Roman Gushchin394e40a2018-08-02 14:27:21 -07001773 existing = old_array->items;
1774 for (; existing->prog; existing++)
1775 if (existing->prog != exclude_prog &&
1776 existing->prog != &dummy_bpf_prog.prog) {
1777 array->items[new_prog_idx++].prog =
1778 existing->prog;
1779 }
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001780 }
1781 if (include_prog)
Roman Gushchin394e40a2018-08-02 14:27:21 -07001782 array->items[new_prog_idx++].prog = include_prog;
1783 array->items[new_prog_idx].prog = NULL;
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001784 *new_array = array;
1785 return 0;
1786}
1787
Yonghong Songf371b302017-12-11 11:39:02 -08001788int bpf_prog_array_copy_info(struct bpf_prog_array __rcu *array,
Yonghong Song3a38bb92018-04-10 09:37:32 -07001789 u32 *prog_ids, u32 request_cnt,
1790 u32 *prog_cnt)
Yonghong Songf371b302017-12-11 11:39:02 -08001791{
1792 u32 cnt = 0;
1793
1794 if (array)
1795 cnt = bpf_prog_array_length(array);
1796
Yonghong Song3a38bb92018-04-10 09:37:32 -07001797 *prog_cnt = cnt;
Yonghong Songf371b302017-12-11 11:39:02 -08001798
1799 /* return early if user requested only program count or nothing to copy */
1800 if (!request_cnt || !cnt)
1801 return 0;
1802
Yonghong Song3a38bb92018-04-10 09:37:32 -07001803 /* this function is called under trace/bpf_trace.c: bpf_event_mutex */
Roman Gushchin394e40a2018-08-02 14:27:21 -07001804 return bpf_prog_array_copy_core(array, prog_ids, request_cnt) ? -ENOSPC
Yonghong Song3a38bb92018-04-10 09:37:32 -07001805 : 0;
Yonghong Songf371b302017-12-11 11:39:02 -08001806}
1807
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001808static void bpf_prog_free_deferred(struct work_struct *work)
1809{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001810 struct bpf_prog_aux *aux;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001811 int i;
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001812
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001813 aux = container_of(work, struct bpf_prog_aux, work);
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001814 if (bpf_prog_is_dev_bound(aux))
1815 bpf_prog_offload_destroy(aux->prog);
Yonghong Songc195651e2018-04-28 22:28:08 -07001816#ifdef CONFIG_PERF_EVENTS
1817 if (aux->prog->has_callchain_buf)
1818 put_callchain_buffers();
1819#endif
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001820 for (i = 0; i < aux->func_cnt; i++)
1821 bpf_jit_free(aux->func[i]);
1822 if (aux->func_cnt) {
1823 kfree(aux->func);
1824 bpf_prog_unlock_free(aux->prog);
1825 } else {
1826 bpf_jit_free(aux->prog);
1827 }
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001828}
1829
1830/* Free internal BPF program */
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001831void bpf_prog_free(struct bpf_prog *fp)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001832{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001833 struct bpf_prog_aux *aux = fp->aux;
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001834
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001835 INIT_WORK(&aux->work, bpf_prog_free_deferred);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001836 schedule_work(&aux->work);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001837}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001838EXPORT_SYMBOL_GPL(bpf_prog_free);
Alexei Starovoitovf89b7752014-10-23 18:41:08 -07001839
Daniel Borkmann3ad00402015-10-08 01:20:39 +02001840/* RNG for unpriviledged user space with separated state from prandom_u32(). */
1841static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
1842
1843void bpf_user_rnd_init_once(void)
1844{
1845 prandom_init_once(&bpf_user_rnd_state);
1846}
1847
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001848BPF_CALL_0(bpf_user_rnd_u32)
Daniel Borkmann3ad00402015-10-08 01:20:39 +02001849{
1850 /* Should someone ever have the rather unwise idea to use some
1851 * of the registers passed into this function, then note that
1852 * this function is called from native eBPF and classic-to-eBPF
1853 * transformations. Register assignments from both sides are
1854 * different, f.e. classic always sets fn(ctx, A, X) here.
1855 */
1856 struct rnd_state *state;
1857 u32 res;
1858
1859 state = &get_cpu_var(bpf_user_rnd_state);
1860 res = prandom_u32_state(state);
Shaohua Lib761fe22016-09-27 08:42:41 -07001861 put_cpu_var(bpf_user_rnd_state);
Daniel Borkmann3ad00402015-10-08 01:20:39 +02001862
1863 return res;
1864}
1865
Daniel Borkmann3ba67da2015-03-05 23:27:51 +01001866/* Weak definitions of helper functions in case we don't have bpf syscall. */
1867const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
1868const struct bpf_func_proto bpf_map_update_elem_proto __weak;
1869const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02001870const struct bpf_func_proto bpf_map_push_elem_proto __weak;
1871const struct bpf_func_proto bpf_map_pop_elem_proto __weak;
1872const struct bpf_func_proto bpf_map_peek_elem_proto __weak;
Daniel Borkmann3ba67da2015-03-05 23:27:51 +01001873
Daniel Borkmann03e69b52015-03-14 02:27:16 +01001874const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
Daniel Borkmannc04167c2015-03-14 02:27:17 +01001875const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
Daniel Borkmann2d0e30c2016-10-21 12:46:33 +02001876const struct bpf_func_proto bpf_get_numa_node_id_proto __weak;
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +02001877const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001878
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -07001879const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
1880const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
1881const struct bpf_func_proto bpf_get_current_comm_proto __weak;
Yonghong Songbf6fa2c82018-06-03 15:59:41 -07001882const struct bpf_func_proto bpf_get_current_cgroup_id_proto __weak;
Roman Gushchincd339432018-08-02 14:27:24 -07001883const struct bpf_func_proto bpf_get_local_storage_proto __weak;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001884
Alexei Starovoitov0756ea32015-06-12 19:39:13 -07001885const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
1886{
1887 return NULL;
1888}
Daniel Borkmann03e69b52015-03-14 02:27:16 +01001889
Daniel Borkmann555c8a82016-07-14 18:08:05 +02001890u64 __weak
1891bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1892 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001893{
Daniel Borkmann555c8a82016-07-14 18:08:05 +02001894 return -ENOTSUPP;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001895}
Jakub Kicinski6cb5fb32018-05-03 18:37:10 -07001896EXPORT_SYMBOL_GPL(bpf_event_output);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001897
Daniel Borkmann3324b582015-05-29 23:23:07 +02001898/* Always built-in helper functions. */
1899const struct bpf_func_proto bpf_tail_call_proto = {
1900 .func = NULL,
1901 .gpl_only = false,
1902 .ret_type = RET_VOID,
1903 .arg1_type = ARG_PTR_TO_CTX,
1904 .arg2_type = ARG_CONST_MAP_PTR,
1905 .arg3_type = ARG_ANYTHING,
1906};
1907
Daniel Borkmann93831912017-02-16 22:24:49 +01001908/* Stub for JITs that only support cBPF. eBPF programs are interpreted.
1909 * It is encouraged to implement bpf_int_jit_compile() instead, so that
1910 * eBPF and implicitly also cBPF can get JITed!
1911 */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001912struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
Daniel Borkmann3324b582015-05-29 23:23:07 +02001913{
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001914 return prog;
Daniel Borkmann3324b582015-05-29 23:23:07 +02001915}
1916
Daniel Borkmann93831912017-02-16 22:24:49 +01001917/* Stub for JITs that support eBPF. All cBPF code gets transformed into
1918 * eBPF by the kernel and is later compiled by bpf_int_jit_compile().
1919 */
1920void __weak bpf_jit_compile(struct bpf_prog *prog)
1921{
1922}
1923
Martin KaFai Lau17bedab2016-12-07 15:53:11 -08001924bool __weak bpf_helper_changes_pkt_data(void *func)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001925{
1926 return false;
1927}
1928
Alexei Starovoitovf89b7752014-10-23 18:41:08 -07001929/* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
1930 * skb_copy_bits(), so provide a weak definition of it for NET-less config.
1931 */
1932int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
1933 int len)
1934{
1935 return -EFAULT;
1936}
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001937
1938/* All definitions of tracepoints related to BPF. */
1939#define CREATE_TRACE_POINTS
1940#include <linux/bpf_trace.h>
1941
1942EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception);