blob: 002d67c62c8b75ab0e4d9e9899dcf8c448a266e9 [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
Yonghong Song838e9692018-11-19 15:29:11 -080024#include <uapi/linux/btf.h>
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -070025#include <linux/filter.h>
26#include <linux/skbuff.h>
Daniel Borkmann60a3b222014-09-02 22:53:44 +020027#include <linux/vmalloc.h>
Daniel Borkmann738cbe72014-09-08 08:04:47 +020028#include <linux/random.h>
29#include <linux/moduleloader.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070030#include <linux/bpf.h>
Yonghong Song838e9692018-11-19 15:29:11 -080031#include <linux/btf.h>
Josh Poimboeuf39853cc2016-02-28 22:22:37 -060032#include <linux/frame.h>
Daniel Borkmann74451e662017-02-16 22:24:50 +010033#include <linux/rbtree_latch.h>
34#include <linux/kallsyms.h>
35#include <linux/rcupdate.h>
Yonghong Songc195651e2018-04-28 22:28:08 -070036#include <linux/perf_event.h>
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -070037
Daniel Borkmann3324b582015-05-29 23:23:07 +020038#include <asm/unaligned.h>
39
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -070040/* Registers */
41#define BPF_R0 regs[BPF_REG_0]
42#define BPF_R1 regs[BPF_REG_1]
43#define BPF_R2 regs[BPF_REG_2]
44#define BPF_R3 regs[BPF_REG_3]
45#define BPF_R4 regs[BPF_REG_4]
46#define BPF_R5 regs[BPF_REG_5]
47#define BPF_R6 regs[BPF_REG_6]
48#define BPF_R7 regs[BPF_REG_7]
49#define BPF_R8 regs[BPF_REG_8]
50#define BPF_R9 regs[BPF_REG_9]
51#define BPF_R10 regs[BPF_REG_10]
52
53/* Named registers */
54#define DST regs[insn->dst_reg]
55#define SRC regs[insn->src_reg]
56#define FP regs[BPF_REG_FP]
57#define ARG1 regs[BPF_REG_ARG1]
58#define CTX regs[BPF_REG_CTX]
59#define IMM insn->imm
60
61/* No hurry in this branch
62 *
63 * Exported for the bpf jit load helper.
64 */
65void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
66{
67 u8 *ptr = NULL;
68
69 if (k >= SKF_NET_OFF)
70 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
71 else if (k >= SKF_LL_OFF)
72 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
Daniel Borkmann3324b582015-05-29 23:23:07 +020073
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -070074 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
75 return ptr;
76
77 return NULL;
78}
79
Daniel Borkmann60a3b222014-09-02 22:53:44 +020080struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
81{
Michal Hocko19809c22017-05-08 15:57:44 -070082 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
Alexei Starovoitov09756af2014-09-26 00:17:00 -070083 struct bpf_prog_aux *aux;
Daniel Borkmann60a3b222014-09-02 22:53:44 +020084 struct bpf_prog *fp;
85
86 size = round_up(size, PAGE_SIZE);
87 fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
88 if (fp == NULL)
89 return NULL;
90
Alexei Starovoitov09756af2014-09-26 00:17:00 -070091 aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);
92 if (aux == NULL) {
Daniel Borkmann60a3b222014-09-02 22:53:44 +020093 vfree(fp);
94 return NULL;
95 }
96
97 fp->pages = size / PAGE_SIZE;
Alexei Starovoitov09756af2014-09-26 00:17:00 -070098 fp->aux = aux;
Daniel Borkmanne9d8afa2015-10-29 14:58:08 +010099 fp->aux->prog = fp;
Alexei Starovoitov60b58afc2017-12-14 17:55:14 -0800100 fp->jit_requested = ebpf_jit_enabled();
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200101
Daniel Borkmann74451e662017-02-16 22:24:50 +0100102 INIT_LIST_HEAD_RCU(&fp->aux->ksym_lnode);
103
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200104 return fp;
105}
106EXPORT_SYMBOL_GPL(bpf_prog_alloc);
107
108struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
109 gfp_t gfp_extra_flags)
110{
Michal Hocko19809c22017-05-08 15:57:44 -0700111 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200112 struct bpf_prog *fp;
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100113 u32 pages, delta;
114 int ret;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200115
116 BUG_ON(fp_old == NULL);
117
118 size = round_up(size, PAGE_SIZE);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100119 pages = size / PAGE_SIZE;
120 if (pages <= fp_old->pages)
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200121 return fp_old;
122
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100123 delta = pages - fp_old->pages;
124 ret = __bpf_prog_charge(fp_old->aux->user, delta);
125 if (ret)
126 return NULL;
127
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200128 fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100129 if (fp == NULL) {
130 __bpf_prog_uncharge(fp_old->aux->user, delta);
131 } else {
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200132 memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100133 fp->pages = pages;
Daniel Borkmanne9d8afa2015-10-29 14:58:08 +0100134 fp->aux->prog = fp;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200135
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700136 /* We keep fp->aux from fp_old around in the new
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200137 * reallocated structure.
138 */
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700139 fp_old->aux = NULL;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200140 __bpf_prog_free(fp_old);
141 }
142
143 return fp;
144}
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200145
146void __bpf_prog_free(struct bpf_prog *fp)
147{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700148 kfree(fp->aux);
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200149 vfree(fp);
150}
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200151
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100152int bpf_prog_calc_tag(struct bpf_prog *fp)
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100153{
154 const u32 bits_offset = SHA_MESSAGE_BYTES - sizeof(__be64);
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100155 u32 raw_size = bpf_prog_tag_scratch_size(fp);
156 u32 digest[SHA_DIGEST_WORDS];
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100157 u32 ws[SHA_WORKSPACE_WORDS];
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100158 u32 i, bsize, psize, blocks;
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100159 struct bpf_insn *dst;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100160 bool was_ld_map;
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100161 u8 *raw, *todo;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100162 __be32 *result;
163 __be64 *bits;
164
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100165 raw = vmalloc(raw_size);
166 if (!raw)
167 return -ENOMEM;
168
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100169 sha_init(digest);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100170 memset(ws, 0, sizeof(ws));
171
172 /* We need to take out the map fd for the digest calculation
173 * since they are unstable from user space side.
174 */
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100175 dst = (void *)raw;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100176 for (i = 0, was_ld_map = false; i < fp->len; i++) {
177 dst[i] = fp->insnsi[i];
178 if (!was_ld_map &&
179 dst[i].code == (BPF_LD | BPF_IMM | BPF_DW) &&
180 dst[i].src_reg == BPF_PSEUDO_MAP_FD) {
181 was_ld_map = true;
182 dst[i].imm = 0;
183 } else if (was_ld_map &&
184 dst[i].code == 0 &&
185 dst[i].dst_reg == 0 &&
186 dst[i].src_reg == 0 &&
187 dst[i].off == 0) {
188 was_ld_map = false;
189 dst[i].imm = 0;
190 } else {
191 was_ld_map = false;
192 }
193 }
194
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100195 psize = bpf_prog_insn_size(fp);
196 memset(&raw[psize], 0, raw_size - psize);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100197 raw[psize++] = 0x80;
198
199 bsize = round_up(psize, SHA_MESSAGE_BYTES);
200 blocks = bsize / SHA_MESSAGE_BYTES;
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100201 todo = raw;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100202 if (bsize - psize >= sizeof(__be64)) {
203 bits = (__be64 *)(todo + bsize - sizeof(__be64));
204 } else {
205 bits = (__be64 *)(todo + bsize + bits_offset);
206 blocks++;
207 }
208 *bits = cpu_to_be64((psize - 1) << 3);
209
210 while (blocks--) {
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100211 sha_transform(digest, todo, ws);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100212 todo += SHA_MESSAGE_BYTES;
213 }
214
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100215 result = (__force __be32 *)digest;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100216 for (i = 0; i < SHA_DIGEST_WORDS; i++)
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100217 result[i] = cpu_to_be32(digest[i]);
218 memcpy(fp->tag, result, sizeof(fp->tag));
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100219
220 vfree(raw);
221 return 0;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100222}
223
Daniel Borkmann050fad72018-05-17 01:44:11 +0200224static int bpf_adj_delta_to_imm(struct bpf_insn *insn, u32 pos, u32 delta,
225 u32 curr, const bool probe_pass)
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200226{
Daniel Borkmann050fad72018-05-17 01:44:11 +0200227 const s64 imm_min = S32_MIN, imm_max = S32_MAX;
228 s64 imm = insn->imm;
229
230 if (curr < pos && curr + imm + 1 > pos)
231 imm += delta;
232 else if (curr > pos + delta && curr + imm + 1 <= pos + delta)
233 imm -= delta;
234 if (imm < imm_min || imm > imm_max)
235 return -ERANGE;
236 if (!probe_pass)
237 insn->imm = imm;
238 return 0;
239}
240
241static int bpf_adj_delta_to_off(struct bpf_insn *insn, u32 pos, u32 delta,
242 u32 curr, const bool probe_pass)
243{
244 const s32 off_min = S16_MIN, off_max = S16_MAX;
245 s32 off = insn->off;
246
247 if (curr < pos && curr + off + 1 > pos)
248 off += delta;
249 else if (curr > pos + delta && curr + off + 1 <= pos + delta)
250 off -= delta;
251 if (off < off_min || off > off_max)
252 return -ERANGE;
253 if (!probe_pass)
254 insn->off = off;
255 return 0;
256}
257
258static int bpf_adj_branches(struct bpf_prog *prog, u32 pos, u32 delta,
259 const bool probe_pass)
260{
261 u32 i, insn_cnt = prog->len + (probe_pass ? delta : 0);
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200262 struct bpf_insn *insn = prog->insnsi;
Daniel Borkmann050fad72018-05-17 01:44:11 +0200263 int ret = 0;
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200264
265 for (i = 0; i < insn_cnt; i++, insn++) {
Daniel Borkmann050fad72018-05-17 01:44:11 +0200266 u8 code;
267
268 /* In the probing pass we still operate on the original,
269 * unpatched image in order to check overflows before we
270 * do any other adjustments. Therefore skip the patchlet.
271 */
272 if (probe_pass && i == pos) {
273 i += delta + 1;
274 insn++;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800275 }
Daniel Borkmann050fad72018-05-17 01:44:11 +0200276 code = insn->code;
277 if (BPF_CLASS(code) != BPF_JMP ||
278 BPF_OP(code) == BPF_EXIT)
279 continue;
280 /* Adjust offset of jmps if we cross patch boundaries. */
281 if (BPF_OP(code) == BPF_CALL) {
282 if (insn->src_reg != BPF_PSEUDO_CALL)
283 continue;
284 ret = bpf_adj_delta_to_imm(insn, pos, delta, i,
285 probe_pass);
286 } else {
287 ret = bpf_adj_delta_to_off(insn, pos, delta, i,
288 probe_pass);
289 }
290 if (ret)
291 break;
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200292 }
Daniel Borkmann050fad72018-05-17 01:44:11 +0200293
294 return ret;
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200295}
296
297struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
298 const struct bpf_insn *patch, u32 len)
299{
300 u32 insn_adj_cnt, insn_rest, insn_delta = len - 1;
Daniel Borkmann050fad72018-05-17 01:44:11 +0200301 const u32 cnt_max = S16_MAX;
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200302 struct bpf_prog *prog_adj;
303
304 /* Since our patchlet doesn't expand the image, we're done. */
305 if (insn_delta == 0) {
306 memcpy(prog->insnsi + off, patch, sizeof(*patch));
307 return prog;
308 }
309
310 insn_adj_cnt = prog->len + insn_delta;
311
Daniel Borkmann050fad72018-05-17 01:44:11 +0200312 /* Reject anything that would potentially let the insn->off
313 * target overflow when we have excessive program expansions.
314 * We need to probe here before we do any reallocation where
315 * we afterwards may not fail anymore.
316 */
317 if (insn_adj_cnt > cnt_max &&
318 bpf_adj_branches(prog, off, insn_delta, true))
319 return NULL;
320
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200321 /* Several new instructions need to be inserted. Make room
322 * for them. Likely, there's no need for a new allocation as
323 * last page could have large enough tailroom.
324 */
325 prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),
326 GFP_USER);
327 if (!prog_adj)
328 return NULL;
329
330 prog_adj->len = insn_adj_cnt;
331
332 /* Patching happens in 3 steps:
333 *
334 * 1) Move over tail of insnsi from next instruction onwards,
335 * so we can patch the single target insn with one or more
336 * new ones (patching is always from 1 to n insns, n > 0).
337 * 2) Inject new instructions at the target location.
338 * 3) Adjust branch offsets if necessary.
339 */
340 insn_rest = insn_adj_cnt - off - len;
341
342 memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1,
343 sizeof(*patch) * insn_rest);
344 memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len);
345
Daniel Borkmann050fad72018-05-17 01:44:11 +0200346 /* We are guaranteed to not fail at this point, otherwise
347 * the ship has sailed to reverse to the original state. An
348 * overflow cannot happen at this point.
349 */
350 BUG_ON(bpf_adj_branches(prog_adj, off, insn_delta, false));
Daniel Borkmannc237ee52016-05-13 19:08:30 +0200351
352 return prog_adj;
353}
354
Daniel Borkmann7d1982b2018-06-15 02:30:47 +0200355void bpf_prog_kallsyms_del_subprogs(struct bpf_prog *fp)
356{
357 int i;
358
359 for (i = 0; i < fp->aux->func_cnt; i++)
360 bpf_prog_kallsyms_del(fp->aux->func[i]);
361}
362
363void bpf_prog_kallsyms_del_all(struct bpf_prog *fp)
364{
365 bpf_prog_kallsyms_del_subprogs(fp);
366 bpf_prog_kallsyms_del(fp);
367}
368
Daniel Borkmannb954d832014-09-10 15:01:02 +0200369#ifdef CONFIG_BPF_JIT
Daniel Borkmannede95a632018-10-23 01:11:04 +0200370# define BPF_JIT_LIMIT_DEFAULT (PAGE_SIZE * 40000)
371
Daniel Borkmannfa9dd592018-01-20 01:24:33 +0100372/* All BPF JIT sysctl knobs here. */
373int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_ALWAYS_ON);
374int bpf_jit_harden __read_mostly;
375int bpf_jit_kallsyms __read_mostly;
Daniel Borkmannede95a632018-10-23 01:11:04 +0200376int bpf_jit_limit __read_mostly = BPF_JIT_LIMIT_DEFAULT;
Daniel Borkmannfa9dd592018-01-20 01:24:33 +0100377
Daniel Borkmann74451e662017-02-16 22:24:50 +0100378static __always_inline void
379bpf_get_prog_addr_region(const struct bpf_prog *prog,
380 unsigned long *symbol_start,
381 unsigned long *symbol_end)
382{
383 const struct bpf_binary_header *hdr = bpf_jit_binary_hdr(prog);
384 unsigned long addr = (unsigned long)hdr;
385
386 WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog));
387
388 *symbol_start = addr;
389 *symbol_end = addr + hdr->pages * PAGE_SIZE;
390}
391
392static void bpf_get_prog_name(const struct bpf_prog *prog, char *sym)
393{
Martin KaFai Lau368211f2017-10-05 21:52:13 -0700394 const char *end = sym + KSYM_NAME_LEN;
Yonghong Song838e9692018-11-19 15:29:11 -0800395 const struct btf_type *type;
396 const char *func_name;
Martin KaFai Lau368211f2017-10-05 21:52:13 -0700397
Daniel Borkmann74451e662017-02-16 22:24:50 +0100398 BUILD_BUG_ON(sizeof("bpf_prog_") +
Martin KaFai Lau368211f2017-10-05 21:52:13 -0700399 sizeof(prog->tag) * 2 +
400 /* name has been null terminated.
401 * We should need +1 for the '_' preceding
402 * the name. However, the null character
403 * is double counted between the name and the
404 * sizeof("bpf_prog_") above, so we omit
405 * the +1 here.
406 */
407 sizeof(prog->aux->name) > KSYM_NAME_LEN);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100408
409 sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_");
410 sym = bin2hex(sym, prog->tag, sizeof(prog->tag));
Yonghong Song838e9692018-11-19 15:29:11 -0800411
412 /* prog->aux->name will be ignored if full btf name is available */
413 if (prog->aux->btf) {
Yonghong Songba64e7d2018-11-24 23:20:44 -0800414 type = btf_type_by_id(prog->aux->btf,
415 prog->aux->func_info[prog->aux->func_idx].type_id);
Yonghong Song838e9692018-11-19 15:29:11 -0800416 func_name = btf_name_by_offset(prog->aux->btf, type->name_off);
417 snprintf(sym, (size_t)(end - sym), "_%s", func_name);
418 return;
419 }
420
Martin KaFai Lau368211f2017-10-05 21:52:13 -0700421 if (prog->aux->name[0])
422 snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name);
423 else
424 *sym = 0;
Daniel Borkmann74451e662017-02-16 22:24:50 +0100425}
426
427static __always_inline unsigned long
428bpf_get_prog_addr_start(struct latch_tree_node *n)
429{
430 unsigned long symbol_start, symbol_end;
431 const struct bpf_prog_aux *aux;
432
433 aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
434 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
435
436 return symbol_start;
437}
438
439static __always_inline bool bpf_tree_less(struct latch_tree_node *a,
440 struct latch_tree_node *b)
441{
442 return bpf_get_prog_addr_start(a) < bpf_get_prog_addr_start(b);
443}
444
445static __always_inline int bpf_tree_comp(void *key, struct latch_tree_node *n)
446{
447 unsigned long val = (unsigned long)key;
448 unsigned long symbol_start, symbol_end;
449 const struct bpf_prog_aux *aux;
450
451 aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
452 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
453
454 if (val < symbol_start)
455 return -1;
456 if (val >= symbol_end)
457 return 1;
458
459 return 0;
460}
461
462static const struct latch_tree_ops bpf_tree_ops = {
463 .less = bpf_tree_less,
464 .comp = bpf_tree_comp,
465};
466
467static DEFINE_SPINLOCK(bpf_lock);
468static LIST_HEAD(bpf_kallsyms);
469static struct latch_tree_root bpf_tree __cacheline_aligned;
470
Daniel Borkmann74451e662017-02-16 22:24:50 +0100471static void bpf_prog_ksym_node_add(struct bpf_prog_aux *aux)
472{
473 WARN_ON_ONCE(!list_empty(&aux->ksym_lnode));
474 list_add_tail_rcu(&aux->ksym_lnode, &bpf_kallsyms);
475 latch_tree_insert(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
476}
477
478static void bpf_prog_ksym_node_del(struct bpf_prog_aux *aux)
479{
480 if (list_empty(&aux->ksym_lnode))
481 return;
482
483 latch_tree_erase(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
484 list_del_rcu(&aux->ksym_lnode);
485}
486
487static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp)
488{
489 return fp->jited && !bpf_prog_was_classic(fp);
490}
491
492static bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp)
493{
494 return list_empty(&fp->aux->ksym_lnode) ||
495 fp->aux->ksym_lnode.prev == LIST_POISON2;
496}
497
498void bpf_prog_kallsyms_add(struct bpf_prog *fp)
499{
Daniel Borkmann74451e662017-02-16 22:24:50 +0100500 if (!bpf_prog_kallsyms_candidate(fp) ||
501 !capable(CAP_SYS_ADMIN))
502 return;
503
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200504 spin_lock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100505 bpf_prog_ksym_node_add(fp->aux);
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200506 spin_unlock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100507}
508
509void bpf_prog_kallsyms_del(struct bpf_prog *fp)
510{
Daniel Borkmann74451e662017-02-16 22:24:50 +0100511 if (!bpf_prog_kallsyms_candidate(fp))
512 return;
513
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200514 spin_lock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100515 bpf_prog_ksym_node_del(fp->aux);
Hannes Frederic Sowad24f7c72017-04-27 01:39:33 +0200516 spin_unlock_bh(&bpf_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100517}
518
519static struct bpf_prog *bpf_prog_kallsyms_find(unsigned long addr)
520{
521 struct latch_tree_node *n;
522
523 if (!bpf_jit_kallsyms_enabled())
524 return NULL;
525
526 n = latch_tree_find((void *)addr, &bpf_tree, &bpf_tree_ops);
527 return n ?
528 container_of(n, struct bpf_prog_aux, ksym_tnode)->prog :
529 NULL;
530}
531
532const char *__bpf_address_lookup(unsigned long addr, unsigned long *size,
533 unsigned long *off, char *sym)
534{
535 unsigned long symbol_start, symbol_end;
536 struct bpf_prog *prog;
537 char *ret = NULL;
538
539 rcu_read_lock();
540 prog = bpf_prog_kallsyms_find(addr);
541 if (prog) {
542 bpf_get_prog_addr_region(prog, &symbol_start, &symbol_end);
543 bpf_get_prog_name(prog, sym);
544
545 ret = sym;
546 if (size)
547 *size = symbol_end - symbol_start;
548 if (off)
549 *off = addr - symbol_start;
550 }
551 rcu_read_unlock();
552
553 return ret;
554}
555
556bool is_bpf_text_address(unsigned long addr)
557{
558 bool ret;
559
560 rcu_read_lock();
561 ret = bpf_prog_kallsyms_find(addr) != NULL;
562 rcu_read_unlock();
563
564 return ret;
565}
566
567int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
568 char *sym)
569{
Daniel Borkmann74451e662017-02-16 22:24:50 +0100570 struct bpf_prog_aux *aux;
571 unsigned int it = 0;
572 int ret = -ERANGE;
573
574 if (!bpf_jit_kallsyms_enabled())
575 return ret;
576
577 rcu_read_lock();
578 list_for_each_entry_rcu(aux, &bpf_kallsyms, ksym_lnode) {
579 if (it++ != symnum)
580 continue;
581
Daniel Borkmann74451e662017-02-16 22:24:50 +0100582 bpf_get_prog_name(aux->prog, sym);
583
Song Liudf073472018-11-02 10:16:15 -0700584 *value = (unsigned long)aux->prog->bpf_func;
Daniel Borkmann74451e662017-02-16 22:24:50 +0100585 *type = BPF_SYM_ELF_TYPE;
586
587 ret = 0;
588 break;
589 }
590 rcu_read_unlock();
591
592 return ret;
593}
594
Daniel Borkmannede95a632018-10-23 01:11:04 +0200595static atomic_long_t bpf_jit_current;
596
597#if defined(MODULES_VADDR)
598static int __init bpf_jit_charge_init(void)
599{
600 /* Only used as heuristic here to derive limit. */
601 bpf_jit_limit = min_t(u64, round_up((MODULES_END - MODULES_VADDR) >> 2,
602 PAGE_SIZE), INT_MAX);
603 return 0;
604}
605pure_initcall(bpf_jit_charge_init);
606#endif
607
608static int bpf_jit_charge_modmem(u32 pages)
609{
610 if (atomic_long_add_return(pages, &bpf_jit_current) >
611 (bpf_jit_limit >> PAGE_SHIFT)) {
612 if (!capable(CAP_SYS_ADMIN)) {
613 atomic_long_sub(pages, &bpf_jit_current);
614 return -EPERM;
615 }
616 }
617
618 return 0;
619}
620
621static void bpf_jit_uncharge_modmem(u32 pages)
622{
623 atomic_long_sub(pages, &bpf_jit_current);
624}
625
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200626struct bpf_binary_header *
627bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
628 unsigned int alignment,
629 bpf_jit_fill_hole_t bpf_fill_ill_insns)
630{
631 struct bpf_binary_header *hdr;
Daniel Borkmannede95a632018-10-23 01:11:04 +0200632 u32 size, hole, start, pages;
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200633
634 /* Most of BPF filters are really small, but if some of them
635 * fill a page, allow at least 128 extra bytes to insert a
636 * random section of illegal instructions.
637 */
638 size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
Daniel Borkmannede95a632018-10-23 01:11:04 +0200639 pages = size / PAGE_SIZE;
640
641 if (bpf_jit_charge_modmem(pages))
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200642 return NULL;
Daniel Borkmannede95a632018-10-23 01:11:04 +0200643 hdr = module_alloc(size);
644 if (!hdr) {
645 bpf_jit_uncharge_modmem(pages);
646 return NULL;
647 }
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200648
649 /* Fill space with illegal/arch-dep instructions. */
650 bpf_fill_ill_insns(hdr, size);
651
Daniel Borkmannede95a632018-10-23 01:11:04 +0200652 hdr->pages = pages;
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200653 hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
654 PAGE_SIZE - sizeof(*hdr));
Daniel Borkmannb7552e1b2016-05-18 14:14:28 +0200655 start = (get_random_int() % hole) & ~(alignment - 1);
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200656
657 /* Leave a random number of instructions before BPF code. */
658 *image_ptr = &hdr->image[start];
659
660 return hdr;
661}
662
663void bpf_jit_binary_free(struct bpf_binary_header *hdr)
664{
Daniel Borkmannede95a632018-10-23 01:11:04 +0200665 u32 pages = hdr->pages;
666
Rusty Russellbe1f2212015-01-20 09:07:05 +1030667 module_memfree(hdr);
Daniel Borkmannede95a632018-10-23 01:11:04 +0200668 bpf_jit_uncharge_modmem(pages);
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200669}
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200670
Daniel Borkmann74451e662017-02-16 22:24:50 +0100671/* This symbol is only overridden by archs that have different
672 * requirements than the usual eBPF JITs, f.e. when they only
673 * implement cBPF JIT, do not set images read-only, etc.
674 */
675void __weak bpf_jit_free(struct bpf_prog *fp)
676{
677 if (fp->jited) {
678 struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp);
679
680 bpf_jit_binary_unlock_ro(hdr);
681 bpf_jit_binary_free(hdr);
682
683 WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp));
684 }
685
686 bpf_prog_unlock_free(fp);
687}
688
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200689static int bpf_jit_blind_insn(const struct bpf_insn *from,
690 const struct bpf_insn *aux,
691 struct bpf_insn *to_buff)
692{
693 struct bpf_insn *to = to_buff;
Daniel Borkmannb7552e1b2016-05-18 14:14:28 +0200694 u32 imm_rnd = get_random_int();
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200695 s16 off;
696
697 BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG);
698 BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG);
699
700 if (from->imm == 0 &&
701 (from->code == (BPF_ALU | BPF_MOV | BPF_K) ||
702 from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) {
703 *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg);
704 goto out;
705 }
706
707 switch (from->code) {
708 case BPF_ALU | BPF_ADD | BPF_K:
709 case BPF_ALU | BPF_SUB | BPF_K:
710 case BPF_ALU | BPF_AND | BPF_K:
711 case BPF_ALU | BPF_OR | BPF_K:
712 case BPF_ALU | BPF_XOR | BPF_K:
713 case BPF_ALU | BPF_MUL | BPF_K:
714 case BPF_ALU | BPF_MOV | BPF_K:
715 case BPF_ALU | BPF_DIV | BPF_K:
716 case BPF_ALU | BPF_MOD | BPF_K:
717 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
718 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
719 *to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX);
720 break;
721
722 case BPF_ALU64 | BPF_ADD | BPF_K:
723 case BPF_ALU64 | BPF_SUB | BPF_K:
724 case BPF_ALU64 | BPF_AND | BPF_K:
725 case BPF_ALU64 | BPF_OR | BPF_K:
726 case BPF_ALU64 | BPF_XOR | BPF_K:
727 case BPF_ALU64 | BPF_MUL | BPF_K:
728 case BPF_ALU64 | BPF_MOV | BPF_K:
729 case BPF_ALU64 | BPF_DIV | BPF_K:
730 case BPF_ALU64 | BPF_MOD | BPF_K:
731 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
732 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
733 *to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX);
734 break;
735
736 case BPF_JMP | BPF_JEQ | BPF_K:
737 case BPF_JMP | BPF_JNE | BPF_K:
738 case BPF_JMP | BPF_JGT | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200739 case BPF_JMP | BPF_JLT | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200740 case BPF_JMP | BPF_JGE | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200741 case BPF_JMP | BPF_JLE | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200742 case BPF_JMP | BPF_JSGT | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200743 case BPF_JMP | BPF_JSLT | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200744 case BPF_JMP | BPF_JSGE | BPF_K:
Daniel Borkmann92b31a92017-08-10 01:39:55 +0200745 case BPF_JMP | BPF_JSLE | BPF_K:
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200746 case BPF_JMP | BPF_JSET | BPF_K:
747 /* Accommodate for extra offset in case of a backjump. */
748 off = from->off;
749 if (off < 0)
750 off -= 2;
751 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
752 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
753 *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off);
754 break;
755
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200756 case BPF_LD | BPF_IMM | BPF_DW:
757 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm);
758 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
759 *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
760 *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX);
761 break;
762 case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */
763 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm);
764 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
765 *to++ = BPF_ALU64_REG(BPF_OR, aux[0].dst_reg, BPF_REG_AX);
766 break;
767
768 case BPF_ST | BPF_MEM | BPF_DW:
769 case BPF_ST | BPF_MEM | BPF_W:
770 case BPF_ST | BPF_MEM | BPF_H:
771 case BPF_ST | BPF_MEM | BPF_B:
772 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
773 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
774 *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off);
775 break;
776 }
777out:
778 return to - to_buff;
779}
780
781static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other,
782 gfp_t gfp_extra_flags)
783{
Michal Hocko19809c22017-05-08 15:57:44 -0700784 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200785 struct bpf_prog *fp;
786
787 fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags, PAGE_KERNEL);
788 if (fp != NULL) {
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200789 /* aux->prog still points to the fp_other one, so
790 * when promoting the clone to the real program,
791 * this still needs to be adapted.
792 */
793 memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE);
794 }
795
796 return fp;
797}
798
799static void bpf_prog_clone_free(struct bpf_prog *fp)
800{
801 /* aux was stolen by the other clone, so we cannot free
802 * it from this path! It will be freed eventually by the
803 * other program on release.
804 *
805 * At this point, we don't need a deferred release since
806 * clone is guaranteed to not be locked.
807 */
808 fp->aux = NULL;
809 __bpf_prog_free(fp);
810}
811
812void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other)
813{
814 /* We have to repoint aux->prog to self, as we don't
815 * know whether fp here is the clone or the original.
816 */
817 fp->aux->prog = fp;
818 bpf_prog_clone_free(fp_other);
819}
820
821struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
822{
823 struct bpf_insn insn_buff[16], aux[2];
824 struct bpf_prog *clone, *tmp;
825 int insn_delta, insn_cnt;
826 struct bpf_insn *insn;
827 int i, rewritten;
828
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -0800829 if (!bpf_jit_blinding_enabled(prog) || prog->blinded)
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200830 return prog;
831
832 clone = bpf_prog_clone_create(prog, GFP_USER);
833 if (!clone)
834 return ERR_PTR(-ENOMEM);
835
836 insn_cnt = clone->len;
837 insn = clone->insnsi;
838
839 for (i = 0; i < insn_cnt; i++, insn++) {
840 /* We temporarily need to hold the original ld64 insn
841 * so that we can still access the first part in the
842 * second blinding run.
843 */
844 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) &&
845 insn[1].code == 0)
846 memcpy(aux, insn, sizeof(aux));
847
848 rewritten = bpf_jit_blind_insn(insn, aux, insn_buff);
849 if (!rewritten)
850 continue;
851
852 tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);
853 if (!tmp) {
854 /* Patching may have repointed aux->prog during
855 * realloc from the original one, so we need to
856 * fix it up here on error.
857 */
858 bpf_jit_prog_release_other(prog, clone);
859 return ERR_PTR(-ENOMEM);
860 }
861
862 clone = tmp;
863 insn_delta = rewritten - 1;
864
865 /* Walk new program and skip insns we just inserted. */
866 insn = clone->insnsi + i + insn_delta;
867 insn_cnt += insn_delta;
868 i += insn_delta;
869 }
870
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -0800871 clone->blinded = 1;
Daniel Borkmann4f3446b2016-05-13 19:08:32 +0200872 return clone;
873}
Daniel Borkmannb954d832014-09-10 15:01:02 +0200874#endif /* CONFIG_BPF_JIT */
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200875
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700876/* Base function for offset calculation. Needs to go into .text section,
877 * therefore keeping it non-static as well; will also be used by JITs
Daniel Borkmann7105e822017-12-20 13:42:57 +0100878 * anyway later on, so do not let the compiler omit it. This also needs
879 * to go into kallsyms for correlation from e.g. bpftool, so naming
880 * must not change.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700881 */
882noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
883{
884 return 0;
885}
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -0700886EXPORT_SYMBOL_GPL(__bpf_call_base);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700887
Daniel Borkmann5e581da2018-01-26 23:33:38 +0100888/* All UAPI available opcodes. */
889#define BPF_INSN_MAP(INSN_2, INSN_3) \
890 /* 32 bit ALU operations. */ \
891 /* Register based. */ \
892 INSN_3(ALU, ADD, X), \
893 INSN_3(ALU, SUB, X), \
894 INSN_3(ALU, AND, X), \
895 INSN_3(ALU, OR, X), \
896 INSN_3(ALU, LSH, X), \
897 INSN_3(ALU, RSH, X), \
898 INSN_3(ALU, XOR, X), \
899 INSN_3(ALU, MUL, X), \
900 INSN_3(ALU, MOV, X), \
901 INSN_3(ALU, DIV, X), \
902 INSN_3(ALU, MOD, X), \
903 INSN_2(ALU, NEG), \
904 INSN_3(ALU, END, TO_BE), \
905 INSN_3(ALU, END, TO_LE), \
906 /* Immediate based. */ \
907 INSN_3(ALU, ADD, K), \
908 INSN_3(ALU, SUB, K), \
909 INSN_3(ALU, AND, K), \
910 INSN_3(ALU, OR, K), \
911 INSN_3(ALU, LSH, K), \
912 INSN_3(ALU, RSH, K), \
913 INSN_3(ALU, XOR, K), \
914 INSN_3(ALU, MUL, K), \
915 INSN_3(ALU, MOV, K), \
916 INSN_3(ALU, DIV, K), \
917 INSN_3(ALU, MOD, K), \
918 /* 64 bit ALU operations. */ \
919 /* Register based. */ \
920 INSN_3(ALU64, ADD, X), \
921 INSN_3(ALU64, SUB, X), \
922 INSN_3(ALU64, AND, X), \
923 INSN_3(ALU64, OR, X), \
924 INSN_3(ALU64, LSH, X), \
925 INSN_3(ALU64, RSH, X), \
926 INSN_3(ALU64, XOR, X), \
927 INSN_3(ALU64, MUL, X), \
928 INSN_3(ALU64, MOV, X), \
929 INSN_3(ALU64, ARSH, X), \
930 INSN_3(ALU64, DIV, X), \
931 INSN_3(ALU64, MOD, X), \
932 INSN_2(ALU64, NEG), \
933 /* Immediate based. */ \
934 INSN_3(ALU64, ADD, K), \
935 INSN_3(ALU64, SUB, K), \
936 INSN_3(ALU64, AND, K), \
937 INSN_3(ALU64, OR, K), \
938 INSN_3(ALU64, LSH, K), \
939 INSN_3(ALU64, RSH, K), \
940 INSN_3(ALU64, XOR, K), \
941 INSN_3(ALU64, MUL, K), \
942 INSN_3(ALU64, MOV, K), \
943 INSN_3(ALU64, ARSH, K), \
944 INSN_3(ALU64, DIV, K), \
945 INSN_3(ALU64, MOD, K), \
946 /* Call instruction. */ \
947 INSN_2(JMP, CALL), \
948 /* Exit instruction. */ \
949 INSN_2(JMP, EXIT), \
950 /* Jump instructions. */ \
951 /* Register based. */ \
952 INSN_3(JMP, JEQ, X), \
953 INSN_3(JMP, JNE, X), \
954 INSN_3(JMP, JGT, X), \
955 INSN_3(JMP, JLT, X), \
956 INSN_3(JMP, JGE, X), \
957 INSN_3(JMP, JLE, X), \
958 INSN_3(JMP, JSGT, X), \
959 INSN_3(JMP, JSLT, X), \
960 INSN_3(JMP, JSGE, X), \
961 INSN_3(JMP, JSLE, X), \
962 INSN_3(JMP, JSET, X), \
963 /* Immediate based. */ \
964 INSN_3(JMP, JEQ, K), \
965 INSN_3(JMP, JNE, K), \
966 INSN_3(JMP, JGT, K), \
967 INSN_3(JMP, JLT, K), \
968 INSN_3(JMP, JGE, K), \
969 INSN_3(JMP, JLE, K), \
970 INSN_3(JMP, JSGT, K), \
971 INSN_3(JMP, JSLT, K), \
972 INSN_3(JMP, JSGE, K), \
973 INSN_3(JMP, JSLE, K), \
974 INSN_3(JMP, JSET, K), \
975 INSN_2(JMP, JA), \
976 /* Store instructions. */ \
977 /* Register based. */ \
978 INSN_3(STX, MEM, B), \
979 INSN_3(STX, MEM, H), \
980 INSN_3(STX, MEM, W), \
981 INSN_3(STX, MEM, DW), \
982 INSN_3(STX, XADD, W), \
983 INSN_3(STX, XADD, DW), \
984 /* Immediate based. */ \
985 INSN_3(ST, MEM, B), \
986 INSN_3(ST, MEM, H), \
987 INSN_3(ST, MEM, W), \
988 INSN_3(ST, MEM, DW), \
989 /* Load instructions. */ \
990 /* Register based. */ \
991 INSN_3(LDX, MEM, B), \
992 INSN_3(LDX, MEM, H), \
993 INSN_3(LDX, MEM, W), \
994 INSN_3(LDX, MEM, DW), \
995 /* Immediate based. */ \
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +0200996 INSN_3(LD, IMM, DW)
Daniel Borkmann5e581da2018-01-26 23:33:38 +0100997
998bool bpf_opcode_in_insntable(u8 code)
999{
1000#define BPF_INSN_2_TBL(x, y) [BPF_##x | BPF_##y] = true
1001#define BPF_INSN_3_TBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = true
1002 static const bool public_insntable[256] = {
1003 [0 ... 255] = false,
1004 /* Now overwrite non-defaults ... */
1005 BPF_INSN_MAP(BPF_INSN_2_TBL, BPF_INSN_3_TBL),
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +02001006 /* UAPI exposed, but rewritten opcodes. cBPF carry-over. */
1007 [BPF_LD | BPF_ABS | BPF_B] = true,
1008 [BPF_LD | BPF_ABS | BPF_H] = true,
1009 [BPF_LD | BPF_ABS | BPF_W] = true,
1010 [BPF_LD | BPF_IND | BPF_B] = true,
1011 [BPF_LD | BPF_IND | BPF_H] = true,
1012 [BPF_LD | BPF_IND | BPF_W] = true,
Daniel Borkmann5e581da2018-01-26 23:33:38 +01001013 };
1014#undef BPF_INSN_3_TBL
1015#undef BPF_INSN_2_TBL
1016 return public_insntable[code];
1017}
1018
Alexei Starovoitov290af862018-01-09 10:04:29 -08001019#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001020/**
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001021 * __bpf_prog_run - run eBPF program on a given context
1022 * @ctx: is the data we are operating on
1023 * @insn: is the array of eBPF instructions
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001024 *
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001025 * Decode and execute eBPF instructions.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001026 */
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001027static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001028{
Alexei Starovoitovf696b8f2017-05-30 13:31:28 -07001029 u64 tmp;
Daniel Borkmann5e581da2018-01-26 23:33:38 +01001030#define BPF_INSN_2_LBL(x, y) [BPF_##x | BPF_##y] = &&x##_##y
1031#define BPF_INSN_3_LBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = &&x##_##y##_##z
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001032 static const void *jumptable[256] = {
1033 [0 ... 255] = &&default_label,
1034 /* Now overwrite non-defaults ... */
Daniel Borkmann5e581da2018-01-26 23:33:38 +01001035 BPF_INSN_MAP(BPF_INSN_2_LBL, BPF_INSN_3_LBL),
1036 /* Non-UAPI available opcodes. */
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001037 [BPF_JMP | BPF_CALL_ARGS] = &&JMP_CALL_ARGS,
Alexei Starovoitov71189fa2017-05-30 13:31:27 -07001038 [BPF_JMP | BPF_TAIL_CALL] = &&JMP_TAIL_CALL,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001039 };
Daniel Borkmann5e581da2018-01-26 23:33:38 +01001040#undef BPF_INSN_3_LBL
1041#undef BPF_INSN_2_LBL
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001042 u32 tail_call_cnt = 0;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001043
1044#define CONT ({ insn++; goto select_insn; })
1045#define CONT_JMP ({ insn++; goto select_insn; })
1046
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001047select_insn:
1048 goto *jumptable[insn->code];
1049
1050 /* ALU */
1051#define ALU(OPCODE, OP) \
1052 ALU64_##OPCODE##_X: \
1053 DST = DST OP SRC; \
1054 CONT; \
1055 ALU_##OPCODE##_X: \
1056 DST = (u32) DST OP (u32) SRC; \
1057 CONT; \
1058 ALU64_##OPCODE##_K: \
1059 DST = DST OP IMM; \
1060 CONT; \
1061 ALU_##OPCODE##_K: \
1062 DST = (u32) DST OP (u32) IMM; \
1063 CONT;
1064
1065 ALU(ADD, +)
1066 ALU(SUB, -)
1067 ALU(AND, &)
1068 ALU(OR, |)
1069 ALU(LSH, <<)
1070 ALU(RSH, >>)
1071 ALU(XOR, ^)
1072 ALU(MUL, *)
1073#undef ALU
1074 ALU_NEG:
1075 DST = (u32) -DST;
1076 CONT;
1077 ALU64_NEG:
1078 DST = -DST;
1079 CONT;
1080 ALU_MOV_X:
1081 DST = (u32) SRC;
1082 CONT;
1083 ALU_MOV_K:
1084 DST = (u32) IMM;
1085 CONT;
1086 ALU64_MOV_X:
1087 DST = SRC;
1088 CONT;
1089 ALU64_MOV_K:
1090 DST = IMM;
1091 CONT;
Alexei Starovoitov02ab6952014-09-04 22:17:17 -07001092 LD_IMM_DW:
1093 DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
1094 insn++;
1095 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001096 ALU64_ARSH_X:
1097 (*(s64 *) &DST) >>= SRC;
1098 CONT;
1099 ALU64_ARSH_K:
1100 (*(s64 *) &DST) >>= IMM;
1101 CONT;
1102 ALU64_MOD_X:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -07001103 div64_u64_rem(DST, SRC, &tmp);
1104 DST = tmp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001105 CONT;
1106 ALU_MOD_X:
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001107 tmp = (u32) DST;
1108 DST = do_div(tmp, (u32) SRC);
1109 CONT;
1110 ALU64_MOD_K:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -07001111 div64_u64_rem(DST, IMM, &tmp);
1112 DST = tmp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001113 CONT;
1114 ALU_MOD_K:
1115 tmp = (u32) DST;
1116 DST = do_div(tmp, (u32) IMM);
1117 CONT;
1118 ALU64_DIV_X:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -07001119 DST = div64_u64(DST, SRC);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001120 CONT;
1121 ALU_DIV_X:
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001122 tmp = (u32) DST;
1123 do_div(tmp, (u32) SRC);
1124 DST = (u32) tmp;
1125 CONT;
1126 ALU64_DIV_K:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -07001127 DST = div64_u64(DST, IMM);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001128 CONT;
1129 ALU_DIV_K:
1130 tmp = (u32) DST;
1131 do_div(tmp, (u32) IMM);
1132 DST = (u32) tmp;
1133 CONT;
1134 ALU_END_TO_BE:
1135 switch (IMM) {
1136 case 16:
1137 DST = (__force u16) cpu_to_be16(DST);
1138 break;
1139 case 32:
1140 DST = (__force u32) cpu_to_be32(DST);
1141 break;
1142 case 64:
1143 DST = (__force u64) cpu_to_be64(DST);
1144 break;
1145 }
1146 CONT;
1147 ALU_END_TO_LE:
1148 switch (IMM) {
1149 case 16:
1150 DST = (__force u16) cpu_to_le16(DST);
1151 break;
1152 case 32:
1153 DST = (__force u32) cpu_to_le32(DST);
1154 break;
1155 case 64:
1156 DST = (__force u64) cpu_to_le64(DST);
1157 break;
1158 }
1159 CONT;
1160
1161 /* CALL */
1162 JMP_CALL:
1163 /* Function call scratches BPF_R1-BPF_R5 registers,
1164 * preserves BPF_R6-BPF_R9, and stores return value
1165 * into BPF_R0.
1166 */
1167 BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
1168 BPF_R4, BPF_R5);
1169 CONT;
1170
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001171 JMP_CALL_ARGS:
1172 BPF_R0 = (__bpf_call_base_args + insn->imm)(BPF_R1, BPF_R2,
1173 BPF_R3, BPF_R4,
1174 BPF_R5,
1175 insn + insn->off + 1);
1176 CONT;
1177
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001178 JMP_TAIL_CALL: {
1179 struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
1180 struct bpf_array *array = container_of(map, struct bpf_array, map);
1181 struct bpf_prog *prog;
Alexei Starovoitov90caccd2017-10-03 15:37:20 -07001182 u32 index = BPF_R3;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001183
1184 if (unlikely(index >= array->map.max_entries))
1185 goto out;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001186 if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
1187 goto out;
1188
1189 tail_call_cnt++;
1190
Wang Nan2a36f0b2015-08-06 07:02:33 +00001191 prog = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +02001192 if (!prog)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001193 goto out;
1194
Daniel Borkmannc4675f92015-07-13 20:49:32 +02001195 /* ARG1 at this point is guaranteed to point to CTX from
1196 * the verifier side due to the fact that the tail call is
1197 * handeled like a helper, that is, bpf_tail_call_proto,
1198 * where arg1_type is ARG_PTR_TO_CTX.
1199 */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001200 insn = prog->insnsi;
1201 goto select_insn;
1202out:
1203 CONT;
1204 }
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001205 /* JMP */
1206 JMP_JA:
1207 insn += insn->off;
1208 CONT;
1209 JMP_JEQ_X:
1210 if (DST == SRC) {
1211 insn += insn->off;
1212 CONT_JMP;
1213 }
1214 CONT;
1215 JMP_JEQ_K:
1216 if (DST == IMM) {
1217 insn += insn->off;
1218 CONT_JMP;
1219 }
1220 CONT;
1221 JMP_JNE_X:
1222 if (DST != SRC) {
1223 insn += insn->off;
1224 CONT_JMP;
1225 }
1226 CONT;
1227 JMP_JNE_K:
1228 if (DST != IMM) {
1229 insn += insn->off;
1230 CONT_JMP;
1231 }
1232 CONT;
1233 JMP_JGT_X:
1234 if (DST > SRC) {
1235 insn += insn->off;
1236 CONT_JMP;
1237 }
1238 CONT;
1239 JMP_JGT_K:
1240 if (DST > IMM) {
1241 insn += insn->off;
1242 CONT_JMP;
1243 }
1244 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001245 JMP_JLT_X:
1246 if (DST < SRC) {
1247 insn += insn->off;
1248 CONT_JMP;
1249 }
1250 CONT;
1251 JMP_JLT_K:
1252 if (DST < IMM) {
1253 insn += insn->off;
1254 CONT_JMP;
1255 }
1256 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001257 JMP_JGE_X:
1258 if (DST >= SRC) {
1259 insn += insn->off;
1260 CONT_JMP;
1261 }
1262 CONT;
1263 JMP_JGE_K:
1264 if (DST >= IMM) {
1265 insn += insn->off;
1266 CONT_JMP;
1267 }
1268 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001269 JMP_JLE_X:
1270 if (DST <= SRC) {
1271 insn += insn->off;
1272 CONT_JMP;
1273 }
1274 CONT;
1275 JMP_JLE_K:
1276 if (DST <= IMM) {
1277 insn += insn->off;
1278 CONT_JMP;
1279 }
1280 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001281 JMP_JSGT_X:
1282 if (((s64) DST) > ((s64) SRC)) {
1283 insn += insn->off;
1284 CONT_JMP;
1285 }
1286 CONT;
1287 JMP_JSGT_K:
1288 if (((s64) DST) > ((s64) IMM)) {
1289 insn += insn->off;
1290 CONT_JMP;
1291 }
1292 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001293 JMP_JSLT_X:
1294 if (((s64) DST) < ((s64) SRC)) {
1295 insn += insn->off;
1296 CONT_JMP;
1297 }
1298 CONT;
1299 JMP_JSLT_K:
1300 if (((s64) DST) < ((s64) IMM)) {
1301 insn += insn->off;
1302 CONT_JMP;
1303 }
1304 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001305 JMP_JSGE_X:
1306 if (((s64) DST) >= ((s64) SRC)) {
1307 insn += insn->off;
1308 CONT_JMP;
1309 }
1310 CONT;
1311 JMP_JSGE_K:
1312 if (((s64) DST) >= ((s64) IMM)) {
1313 insn += insn->off;
1314 CONT_JMP;
1315 }
1316 CONT;
Daniel Borkmann92b31a92017-08-10 01:39:55 +02001317 JMP_JSLE_X:
1318 if (((s64) DST) <= ((s64) SRC)) {
1319 insn += insn->off;
1320 CONT_JMP;
1321 }
1322 CONT;
1323 JMP_JSLE_K:
1324 if (((s64) DST) <= ((s64) IMM)) {
1325 insn += insn->off;
1326 CONT_JMP;
1327 }
1328 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001329 JMP_JSET_X:
1330 if (DST & SRC) {
1331 insn += insn->off;
1332 CONT_JMP;
1333 }
1334 CONT;
1335 JMP_JSET_K:
1336 if (DST & IMM) {
1337 insn += insn->off;
1338 CONT_JMP;
1339 }
1340 CONT;
1341 JMP_EXIT:
1342 return BPF_R0;
1343
1344 /* STX and ST and LDX*/
1345#define LDST(SIZEOP, SIZE) \
1346 STX_MEM_##SIZEOP: \
1347 *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \
1348 CONT; \
1349 ST_MEM_##SIZEOP: \
1350 *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \
1351 CONT; \
1352 LDX_MEM_##SIZEOP: \
1353 DST = *(SIZE *)(unsigned long) (SRC + insn->off); \
1354 CONT;
1355
1356 LDST(B, u8)
1357 LDST(H, u16)
1358 LDST(W, u32)
1359 LDST(DW, u64)
1360#undef LDST
1361 STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
1362 atomic_add((u32) SRC, (atomic_t *)(unsigned long)
1363 (DST + insn->off));
1364 CONT;
1365 STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
1366 atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
1367 (DST + insn->off));
1368 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001369
1370 default_label:
Daniel Borkmann5e581da2018-01-26 23:33:38 +01001371 /* If we ever reach this, we have a bug somewhere. Die hard here
1372 * instead of just returning 0; we could be somewhere in a subprog,
1373 * so execution could continue otherwise which we do /not/ want.
1374 *
1375 * Note, verifier whitelists all opcodes in bpf_opcode_in_insntable().
1376 */
1377 pr_warn("BPF interpreter: unknown opcode %02x\n", insn->code);
1378 BUG_ON(1);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001379 return 0;
1380}
Alexei Starovoitovf696b8f2017-05-30 13:31:28 -07001381STACK_FRAME_NON_STANDARD(___bpf_prog_run); /* jump table */
1382
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001383#define PROG_NAME(stack_size) __bpf_prog_run##stack_size
1384#define DEFINE_BPF_PROG_RUN(stack_size) \
1385static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \
1386{ \
1387 u64 stack[stack_size / sizeof(u64)]; \
1388 u64 regs[MAX_BPF_REG]; \
1389\
1390 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1391 ARG1 = (u64) (unsigned long) ctx; \
1392 return ___bpf_prog_run(regs, insn, stack); \
Alexei Starovoitovf696b8f2017-05-30 13:31:28 -07001393}
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001394
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001395#define PROG_NAME_ARGS(stack_size) __bpf_prog_run_args##stack_size
1396#define DEFINE_BPF_PROG_RUN_ARGS(stack_size) \
1397static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \
1398 const struct bpf_insn *insn) \
1399{ \
1400 u64 stack[stack_size / sizeof(u64)]; \
1401 u64 regs[MAX_BPF_REG]; \
1402\
1403 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1404 BPF_R1 = r1; \
1405 BPF_R2 = r2; \
1406 BPF_R3 = r3; \
1407 BPF_R4 = r4; \
1408 BPF_R5 = r5; \
1409 return ___bpf_prog_run(regs, insn, stack); \
1410}
1411
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001412#define EVAL1(FN, X) FN(X)
1413#define EVAL2(FN, X, Y...) FN(X) EVAL1(FN, Y)
1414#define EVAL3(FN, X, Y...) FN(X) EVAL2(FN, Y)
1415#define EVAL4(FN, X, Y...) FN(X) EVAL3(FN, Y)
1416#define EVAL5(FN, X, Y...) FN(X) EVAL4(FN, Y)
1417#define EVAL6(FN, X, Y...) FN(X) EVAL5(FN, Y)
1418
1419EVAL6(DEFINE_BPF_PROG_RUN, 32, 64, 96, 128, 160, 192);
1420EVAL6(DEFINE_BPF_PROG_RUN, 224, 256, 288, 320, 352, 384);
1421EVAL4(DEFINE_BPF_PROG_RUN, 416, 448, 480, 512);
1422
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001423EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 32, 64, 96, 128, 160, 192);
1424EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 224, 256, 288, 320, 352, 384);
1425EVAL4(DEFINE_BPF_PROG_RUN_ARGS, 416, 448, 480, 512);
1426
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001427#define PROG_NAME_LIST(stack_size) PROG_NAME(stack_size),
1428
1429static unsigned int (*interpreters[])(const void *ctx,
1430 const struct bpf_insn *insn) = {
1431EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1432EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1433EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1434};
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001435#undef PROG_NAME_LIST
1436#define PROG_NAME_LIST(stack_size) PROG_NAME_ARGS(stack_size),
1437static u64 (*interpreters_args[])(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5,
1438 const struct bpf_insn *insn) = {
1439EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1440EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1441EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1442};
1443#undef PROG_NAME_LIST
1444
1445void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth)
1446{
1447 stack_depth = max_t(u32, stack_depth, 1);
1448 insn->off = (s16) insn->imm;
1449 insn->imm = interpreters_args[(round_up(stack_depth, 32) / 32) - 1] -
1450 __bpf_call_base_args;
1451 insn->code = BPF_JMP | BPF_CALL_ARGS;
1452}
Alexei Starovoitovb870aa92017-05-30 13:31:33 -07001453
Alexei Starovoitov290af862018-01-09 10:04:29 -08001454#else
Daniel Borkmannfa9dd592018-01-20 01:24:33 +01001455static unsigned int __bpf_prog_ret0_warn(const void *ctx,
1456 const struct bpf_insn *insn)
Alexei Starovoitov290af862018-01-09 10:04:29 -08001457{
Daniel Borkmannfa9dd592018-01-20 01:24:33 +01001458 /* If this handler ever gets executed, then BPF_JIT_ALWAYS_ON
1459 * is not working properly, so warn about it!
1460 */
1461 WARN_ON_ONCE(1);
Alexei Starovoitov290af862018-01-09 10:04:29 -08001462 return 0;
1463}
1464#endif
1465
Daniel Borkmann3324b582015-05-29 23:23:07 +02001466bool bpf_prog_array_compatible(struct bpf_array *array,
1467 const struct bpf_prog *fp)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001468{
Josef Bacik9802d862017-12-11 11:36:48 -05001469 if (fp->kprobe_override)
1470 return false;
1471
Daniel Borkmann3324b582015-05-29 23:23:07 +02001472 if (!array->owner_prog_type) {
1473 /* There's no owner yet where we could check for
1474 * compatibility.
1475 */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001476 array->owner_prog_type = fp->type;
1477 array->owner_jited = fp->jited;
Daniel Borkmann3324b582015-05-29 23:23:07 +02001478
1479 return true;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001480 }
Daniel Borkmann3324b582015-05-29 23:23:07 +02001481
1482 return array->owner_prog_type == fp->type &&
1483 array->owner_jited == fp->jited;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001484}
1485
Daniel Borkmann3324b582015-05-29 23:23:07 +02001486static int bpf_check_tail_call(const struct bpf_prog *fp)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001487{
1488 struct bpf_prog_aux *aux = fp->aux;
1489 int i;
1490
1491 for (i = 0; i < aux->used_map_cnt; i++) {
Daniel Borkmann3324b582015-05-29 23:23:07 +02001492 struct bpf_map *map = aux->used_maps[i];
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001493 struct bpf_array *array;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001494
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001495 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1496 continue;
Daniel Borkmann3324b582015-05-29 23:23:07 +02001497
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001498 array = container_of(map, struct bpf_array, map);
1499 if (!bpf_prog_array_compatible(array, fp))
1500 return -EINVAL;
1501 }
1502
1503 return 0;
1504}
1505
Daniel Borkmann9facc332018-06-15 02:30:48 +02001506static void bpf_prog_select_func(struct bpf_prog *fp)
1507{
1508#ifndef CONFIG_BPF_JIT_ALWAYS_ON
1509 u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1);
1510
1511 fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1];
1512#else
1513 fp->bpf_func = __bpf_prog_ret0_warn;
1514#endif
1515}
1516
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001517/**
Daniel Borkmann3324b582015-05-29 23:23:07 +02001518 * bpf_prog_select_runtime - select exec runtime for BPF program
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001519 * @fp: bpf_prog populated with internal BPF program
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001520 * @err: pointer to error variable
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001521 *
Daniel Borkmann3324b582015-05-29 23:23:07 +02001522 * Try to JIT eBPF program, if JIT is not available, use interpreter.
1523 * The BPF program will be executed via BPF_PROG_RUN() macro.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001524 */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001525struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001526{
Daniel Borkmann9facc332018-06-15 02:30:48 +02001527 /* In case of BPF to BPF calls, verifier did all the prep
1528 * work with regards to JITing, etc.
1529 */
1530 if (fp->bpf_func)
1531 goto finalize;
Martin KaFai Lau8007e402017-06-28 10:41:24 -07001532
Daniel Borkmann9facc332018-06-15 02:30:48 +02001533 bpf_prog_select_func(fp);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001534
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001535 /* eBPF JITs can rewrite the program in case constant
1536 * blinding is active. However, in case of error during
1537 * blinding, bpf_int_jit_compile() must always return a
1538 * valid program, which in this case would simply not
1539 * be JITed, but falls back to the interpreter.
1540 */
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001541 if (!bpf_prog_is_dev_bound(fp->aux)) {
1542 fp = bpf_int_jit_compile(fp);
Alexei Starovoitov290af862018-01-09 10:04:29 -08001543#ifdef CONFIG_BPF_JIT_ALWAYS_ON
1544 if (!fp->jited) {
1545 *err = -ENOTSUPP;
1546 return fp;
1547 }
1548#endif
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001549 } else {
1550 *err = bpf_prog_offload_compile(fp);
1551 if (*err)
1552 return fp;
1553 }
Daniel Borkmann9facc332018-06-15 02:30:48 +02001554
1555finalize:
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001556 bpf_prog_lock_ro(fp);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001557
Daniel Borkmann3324b582015-05-29 23:23:07 +02001558 /* The tail call compatibility check can only be done at
1559 * this late stage as we need to determine, if we deal
1560 * with JITed or non JITed program concatenations and not
1561 * all eBPF JITs might immediately support all features.
1562 */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001563 *err = bpf_check_tail_call(fp);
1564
1565 return fp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001566}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001567EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001568
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001569static unsigned int __bpf_prog_ret1(const void *ctx,
1570 const struct bpf_insn *insn)
1571{
1572 return 1;
1573}
1574
1575static struct bpf_prog_dummy {
1576 struct bpf_prog prog;
1577} dummy_bpf_prog = {
1578 .prog = {
1579 .bpf_func = __bpf_prog_ret1,
1580 },
1581};
1582
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001583/* to avoid allocating empty bpf_prog_array for cgroups that
1584 * don't have bpf program attached use one global 'empty_prog_array'
1585 * It will not be modified the caller of bpf_prog_array_alloc()
1586 * (since caller requested prog_cnt == 0)
1587 * that pointer should be 'freed' by bpf_prog_array_free()
1588 */
1589static struct {
1590 struct bpf_prog_array hdr;
1591 struct bpf_prog *null_prog;
1592} empty_prog_array = {
1593 .null_prog = NULL,
1594};
1595
Roman Gushchind29ab6e2018-07-13 12:41:10 -07001596struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags)
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001597{
1598 if (prog_cnt)
1599 return kzalloc(sizeof(struct bpf_prog_array) +
Roman Gushchin394e40a2018-08-02 14:27:21 -07001600 sizeof(struct bpf_prog_array_item) *
1601 (prog_cnt + 1),
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001602 flags);
1603
1604 return &empty_prog_array.hdr;
1605}
1606
1607void bpf_prog_array_free(struct bpf_prog_array __rcu *progs)
1608{
1609 if (!progs ||
1610 progs == (struct bpf_prog_array __rcu *)&empty_prog_array.hdr)
1611 return;
1612 kfree_rcu(progs, rcu);
1613}
1614
Roman Gushchin394e40a2018-08-02 14:27:21 -07001615int bpf_prog_array_length(struct bpf_prog_array __rcu *array)
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001616{
Roman Gushchin394e40a2018-08-02 14:27:21 -07001617 struct bpf_prog_array_item *item;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001618 u32 cnt = 0;
1619
1620 rcu_read_lock();
Roman Gushchin394e40a2018-08-02 14:27:21 -07001621 item = rcu_dereference(array)->items;
1622 for (; item->prog; item++)
1623 if (item->prog != &dummy_bpf_prog.prog)
Yonghong Songc8c088b2017-11-30 13:47:54 -08001624 cnt++;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001625 rcu_read_unlock();
1626 return cnt;
1627}
1628
Roman Gushchin394e40a2018-08-02 14:27:21 -07001629
1630static bool bpf_prog_array_copy_core(struct bpf_prog_array __rcu *array,
Yonghong Song3a38bb92018-04-10 09:37:32 -07001631 u32 *prog_ids,
1632 u32 request_cnt)
1633{
Roman Gushchin394e40a2018-08-02 14:27:21 -07001634 struct bpf_prog_array_item *item;
Yonghong Song3a38bb92018-04-10 09:37:32 -07001635 int i = 0;
1636
Yonghong Song965931e2018-08-14 11:01:12 -07001637 item = rcu_dereference_check(array, 1)->items;
Roman Gushchin394e40a2018-08-02 14:27:21 -07001638 for (; item->prog; item++) {
1639 if (item->prog == &dummy_bpf_prog.prog)
Yonghong Song3a38bb92018-04-10 09:37:32 -07001640 continue;
Roman Gushchin394e40a2018-08-02 14:27:21 -07001641 prog_ids[i] = item->prog->aux->id;
Yonghong Song3a38bb92018-04-10 09:37:32 -07001642 if (++i == request_cnt) {
Roman Gushchin394e40a2018-08-02 14:27:21 -07001643 item++;
Yonghong Song3a38bb92018-04-10 09:37:32 -07001644 break;
1645 }
1646 }
1647
Roman Gushchin394e40a2018-08-02 14:27:21 -07001648 return !!(item->prog);
Yonghong Song3a38bb92018-04-10 09:37:32 -07001649}
1650
Roman Gushchin394e40a2018-08-02 14:27:21 -07001651int bpf_prog_array_copy_to_user(struct bpf_prog_array __rcu *array,
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001652 __u32 __user *prog_ids, u32 cnt)
1653{
Alexei Starovoitov09112872018-02-02 15:14:05 -08001654 unsigned long err = 0;
Alexei Starovoitov09112872018-02-02 15:14:05 -08001655 bool nospc;
Yonghong Song3a38bb92018-04-10 09:37:32 -07001656 u32 *ids;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001657
Alexei Starovoitov09112872018-02-02 15:14:05 -08001658 /* users of this function are doing:
1659 * cnt = bpf_prog_array_length();
1660 * if (cnt > 0)
1661 * bpf_prog_array_copy_to_user(..., cnt);
1662 * so below kcalloc doesn't need extra cnt > 0 check, but
1663 * bpf_prog_array_length() releases rcu lock and
1664 * prog array could have been swapped with empty or larger array,
1665 * so always copy 'cnt' prog_ids to the user.
1666 * In a rare race the user will see zero prog_ids
1667 */
Daniel Borkmann9c481b92018-02-14 15:31:00 +01001668 ids = kcalloc(cnt, sizeof(u32), GFP_USER | __GFP_NOWARN);
Alexei Starovoitov09112872018-02-02 15:14:05 -08001669 if (!ids)
1670 return -ENOMEM;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001671 rcu_read_lock();
Roman Gushchin394e40a2018-08-02 14:27:21 -07001672 nospc = bpf_prog_array_copy_core(array, ids, cnt);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001673 rcu_read_unlock();
Alexei Starovoitov09112872018-02-02 15:14:05 -08001674 err = copy_to_user(prog_ids, ids, cnt * sizeof(u32));
1675 kfree(ids);
1676 if (err)
1677 return -EFAULT;
1678 if (nospc)
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001679 return -ENOSPC;
1680 return 0;
1681}
1682
Roman Gushchin394e40a2018-08-02 14:27:21 -07001683void bpf_prog_array_delete_safe(struct bpf_prog_array __rcu *array,
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001684 struct bpf_prog *old_prog)
1685{
Roman Gushchin394e40a2018-08-02 14:27:21 -07001686 struct bpf_prog_array_item *item = array->items;
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001687
Roman Gushchin394e40a2018-08-02 14:27:21 -07001688 for (; item->prog; item++)
1689 if (item->prog == old_prog) {
1690 WRITE_ONCE(item->prog, &dummy_bpf_prog.prog);
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001691 break;
1692 }
1693}
1694
1695int bpf_prog_array_copy(struct bpf_prog_array __rcu *old_array,
1696 struct bpf_prog *exclude_prog,
1697 struct bpf_prog *include_prog,
1698 struct bpf_prog_array **new_array)
1699{
1700 int new_prog_cnt, carry_prog_cnt = 0;
Roman Gushchin394e40a2018-08-02 14:27:21 -07001701 struct bpf_prog_array_item *existing;
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001702 struct bpf_prog_array *array;
Sean Young170a7e32018-05-27 12:24:08 +01001703 bool found_exclude = false;
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001704 int new_prog_idx = 0;
1705
1706 /* Figure out how many existing progs we need to carry over to
1707 * the new array.
1708 */
1709 if (old_array) {
Roman Gushchin394e40a2018-08-02 14:27:21 -07001710 existing = old_array->items;
1711 for (; existing->prog; existing++) {
1712 if (existing->prog == exclude_prog) {
Sean Young170a7e32018-05-27 12:24:08 +01001713 found_exclude = true;
1714 continue;
1715 }
Roman Gushchin394e40a2018-08-02 14:27:21 -07001716 if (existing->prog != &dummy_bpf_prog.prog)
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001717 carry_prog_cnt++;
Roman Gushchin394e40a2018-08-02 14:27:21 -07001718 if (existing->prog == include_prog)
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001719 return -EEXIST;
1720 }
1721 }
1722
Sean Young170a7e32018-05-27 12:24:08 +01001723 if (exclude_prog && !found_exclude)
1724 return -ENOENT;
1725
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001726 /* How many progs (not NULL) will be in the new array? */
1727 new_prog_cnt = carry_prog_cnt;
1728 if (include_prog)
1729 new_prog_cnt += 1;
1730
1731 /* Do we have any prog (not NULL) in the new array? */
1732 if (!new_prog_cnt) {
1733 *new_array = NULL;
1734 return 0;
1735 }
1736
1737 /* +1 as the end of prog_array is marked with NULL */
1738 array = bpf_prog_array_alloc(new_prog_cnt + 1, GFP_KERNEL);
1739 if (!array)
1740 return -ENOMEM;
1741
1742 /* Fill in the new prog array */
1743 if (carry_prog_cnt) {
Roman Gushchin394e40a2018-08-02 14:27:21 -07001744 existing = old_array->items;
1745 for (; existing->prog; existing++)
1746 if (existing->prog != exclude_prog &&
1747 existing->prog != &dummy_bpf_prog.prog) {
1748 array->items[new_prog_idx++].prog =
1749 existing->prog;
1750 }
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001751 }
1752 if (include_prog)
Roman Gushchin394e40a2018-08-02 14:27:21 -07001753 array->items[new_prog_idx++].prog = include_prog;
1754 array->items[new_prog_idx].prog = NULL;
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001755 *new_array = array;
1756 return 0;
1757}
1758
Yonghong Songf371b302017-12-11 11:39:02 -08001759int bpf_prog_array_copy_info(struct bpf_prog_array __rcu *array,
Yonghong Song3a38bb92018-04-10 09:37:32 -07001760 u32 *prog_ids, u32 request_cnt,
1761 u32 *prog_cnt)
Yonghong Songf371b302017-12-11 11:39:02 -08001762{
1763 u32 cnt = 0;
1764
1765 if (array)
1766 cnt = bpf_prog_array_length(array);
1767
Yonghong Song3a38bb92018-04-10 09:37:32 -07001768 *prog_cnt = cnt;
Yonghong Songf371b302017-12-11 11:39:02 -08001769
1770 /* return early if user requested only program count or nothing to copy */
1771 if (!request_cnt || !cnt)
1772 return 0;
1773
Yonghong Song3a38bb92018-04-10 09:37:32 -07001774 /* this function is called under trace/bpf_trace.c: bpf_event_mutex */
Roman Gushchin394e40a2018-08-02 14:27:21 -07001775 return bpf_prog_array_copy_core(array, prog_ids, request_cnt) ? -ENOSPC
Yonghong Song3a38bb92018-04-10 09:37:32 -07001776 : 0;
Yonghong Songf371b302017-12-11 11:39:02 -08001777}
1778
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001779static void bpf_prog_free_deferred(struct work_struct *work)
1780{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001781 struct bpf_prog_aux *aux;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001782 int i;
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001783
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001784 aux = container_of(work, struct bpf_prog_aux, work);
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001785 if (bpf_prog_is_dev_bound(aux))
1786 bpf_prog_offload_destroy(aux->prog);
Yonghong Songc195651e2018-04-28 22:28:08 -07001787#ifdef CONFIG_PERF_EVENTS
1788 if (aux->prog->has_callchain_buf)
1789 put_callchain_buffers();
1790#endif
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001791 for (i = 0; i < aux->func_cnt; i++)
1792 bpf_jit_free(aux->func[i]);
1793 if (aux->func_cnt) {
1794 kfree(aux->func);
1795 bpf_prog_unlock_free(aux->prog);
1796 } else {
1797 bpf_jit_free(aux->prog);
1798 }
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001799}
1800
1801/* Free internal BPF program */
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001802void bpf_prog_free(struct bpf_prog *fp)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001803{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001804 struct bpf_prog_aux *aux = fp->aux;
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001805
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001806 INIT_WORK(&aux->work, bpf_prog_free_deferred);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001807 schedule_work(&aux->work);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001808}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001809EXPORT_SYMBOL_GPL(bpf_prog_free);
Alexei Starovoitovf89b7752014-10-23 18:41:08 -07001810
Daniel Borkmann3ad00402015-10-08 01:20:39 +02001811/* RNG for unpriviledged user space with separated state from prandom_u32(). */
1812static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
1813
1814void bpf_user_rnd_init_once(void)
1815{
1816 prandom_init_once(&bpf_user_rnd_state);
1817}
1818
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001819BPF_CALL_0(bpf_user_rnd_u32)
Daniel Borkmann3ad00402015-10-08 01:20:39 +02001820{
1821 /* Should someone ever have the rather unwise idea to use some
1822 * of the registers passed into this function, then note that
1823 * this function is called from native eBPF and classic-to-eBPF
1824 * transformations. Register assignments from both sides are
1825 * different, f.e. classic always sets fn(ctx, A, X) here.
1826 */
1827 struct rnd_state *state;
1828 u32 res;
1829
1830 state = &get_cpu_var(bpf_user_rnd_state);
1831 res = prandom_u32_state(state);
Shaohua Lib761fe22016-09-27 08:42:41 -07001832 put_cpu_var(bpf_user_rnd_state);
Daniel Borkmann3ad00402015-10-08 01:20:39 +02001833
1834 return res;
1835}
1836
Daniel Borkmann3ba67da2015-03-05 23:27:51 +01001837/* Weak definitions of helper functions in case we don't have bpf syscall. */
1838const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
1839const struct bpf_func_proto bpf_map_update_elem_proto __weak;
1840const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02001841const struct bpf_func_proto bpf_map_push_elem_proto __weak;
1842const struct bpf_func_proto bpf_map_pop_elem_proto __weak;
1843const struct bpf_func_proto bpf_map_peek_elem_proto __weak;
Daniel Borkmann3ba67da2015-03-05 23:27:51 +01001844
Daniel Borkmann03e69b52015-03-14 02:27:16 +01001845const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
Daniel Borkmannc04167c2015-03-14 02:27:17 +01001846const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
Daniel Borkmann2d0e30c2016-10-21 12:46:33 +02001847const struct bpf_func_proto bpf_get_numa_node_id_proto __weak;
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +02001848const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001849
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -07001850const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
1851const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
1852const struct bpf_func_proto bpf_get_current_comm_proto __weak;
Yonghong Songbf6fa2c82018-06-03 15:59:41 -07001853const struct bpf_func_proto bpf_get_current_cgroup_id_proto __weak;
Roman Gushchincd339432018-08-02 14:27:24 -07001854const struct bpf_func_proto bpf_get_local_storage_proto __weak;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001855
Alexei Starovoitov0756ea32015-06-12 19:39:13 -07001856const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
1857{
1858 return NULL;
1859}
Daniel Borkmann03e69b52015-03-14 02:27:16 +01001860
Daniel Borkmann555c8a82016-07-14 18:08:05 +02001861u64 __weak
1862bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1863 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001864{
Daniel Borkmann555c8a82016-07-14 18:08:05 +02001865 return -ENOTSUPP;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001866}
Jakub Kicinski6cb5fb32018-05-03 18:37:10 -07001867EXPORT_SYMBOL_GPL(bpf_event_output);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001868
Daniel Borkmann3324b582015-05-29 23:23:07 +02001869/* Always built-in helper functions. */
1870const struct bpf_func_proto bpf_tail_call_proto = {
1871 .func = NULL,
1872 .gpl_only = false,
1873 .ret_type = RET_VOID,
1874 .arg1_type = ARG_PTR_TO_CTX,
1875 .arg2_type = ARG_CONST_MAP_PTR,
1876 .arg3_type = ARG_ANYTHING,
1877};
1878
Daniel Borkmann93831912017-02-16 22:24:49 +01001879/* Stub for JITs that only support cBPF. eBPF programs are interpreted.
1880 * It is encouraged to implement bpf_int_jit_compile() instead, so that
1881 * eBPF and implicitly also cBPF can get JITed!
1882 */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001883struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
Daniel Borkmann3324b582015-05-29 23:23:07 +02001884{
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001885 return prog;
Daniel Borkmann3324b582015-05-29 23:23:07 +02001886}
1887
Daniel Borkmann93831912017-02-16 22:24:49 +01001888/* Stub for JITs that support eBPF. All cBPF code gets transformed into
1889 * eBPF by the kernel and is later compiled by bpf_int_jit_compile().
1890 */
1891void __weak bpf_jit_compile(struct bpf_prog *prog)
1892{
1893}
1894
Martin KaFai Lau17bedab2016-12-07 15:53:11 -08001895bool __weak bpf_helper_changes_pkt_data(void *func)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001896{
1897 return false;
1898}
1899
Alexei Starovoitovf89b7752014-10-23 18:41:08 -07001900/* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
1901 * skb_copy_bits(), so provide a weak definition of it for NET-less config.
1902 */
1903int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
1904 int len)
1905{
1906 return -EFAULT;
1907}
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001908
1909/* All definitions of tracepoints related to BPF. */
1910#define CREATE_TRACE_POINTS
1911#include <linux/bpf_trace.h>
1912
1913EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception);