Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1 | /* |
| 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 Starovoitov | 4df95ff | 2014-07-30 20:34:14 -0700 | [diff] [blame] | 21 | * Kris Katterjohn - Added many additional checks in bpf_check_classic() |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 22 | */ |
Daniel Borkmann | 738cbe7 | 2014-09-08 08:04:47 +0200 | [diff] [blame] | 23 | |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 24 | #include <linux/filter.h> |
| 25 | #include <linux/skbuff.h> |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 26 | #include <linux/vmalloc.h> |
Daniel Borkmann | 738cbe7 | 2014-09-08 08:04:47 +0200 | [diff] [blame] | 27 | #include <linux/random.h> |
| 28 | #include <linux/moduleloader.h> |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 29 | #include <linux/bpf.h> |
Josh Poimboeuf | 39853cc | 2016-02-28 22:22:37 -0600 | [diff] [blame] | 30 | #include <linux/frame.h> |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 31 | #include <linux/rbtree_latch.h> |
| 32 | #include <linux/kallsyms.h> |
| 33 | #include <linux/rcupdate.h> |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 34 | |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 35 | #include <asm/unaligned.h> |
| 36 | |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 37 | /* Registers */ |
| 38 | #define BPF_R0 regs[BPF_REG_0] |
| 39 | #define BPF_R1 regs[BPF_REG_1] |
| 40 | #define BPF_R2 regs[BPF_REG_2] |
| 41 | #define BPF_R3 regs[BPF_REG_3] |
| 42 | #define BPF_R4 regs[BPF_REG_4] |
| 43 | #define BPF_R5 regs[BPF_REG_5] |
| 44 | #define BPF_R6 regs[BPF_REG_6] |
| 45 | #define BPF_R7 regs[BPF_REG_7] |
| 46 | #define BPF_R8 regs[BPF_REG_8] |
| 47 | #define BPF_R9 regs[BPF_REG_9] |
| 48 | #define BPF_R10 regs[BPF_REG_10] |
| 49 | |
| 50 | /* Named registers */ |
| 51 | #define DST regs[insn->dst_reg] |
| 52 | #define SRC regs[insn->src_reg] |
| 53 | #define FP regs[BPF_REG_FP] |
| 54 | #define ARG1 regs[BPF_REG_ARG1] |
| 55 | #define CTX regs[BPF_REG_CTX] |
| 56 | #define IMM insn->imm |
| 57 | |
| 58 | /* No hurry in this branch |
| 59 | * |
| 60 | * Exported for the bpf jit load helper. |
| 61 | */ |
| 62 | void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size) |
| 63 | { |
| 64 | u8 *ptr = NULL; |
| 65 | |
| 66 | if (k >= SKF_NET_OFF) |
| 67 | ptr = skb_network_header(skb) + k - SKF_NET_OFF; |
| 68 | else if (k >= SKF_LL_OFF) |
| 69 | ptr = skb_mac_header(skb) + k - SKF_LL_OFF; |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 70 | |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 71 | if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb)) |
| 72 | return ptr; |
| 73 | |
| 74 | return NULL; |
| 75 | } |
| 76 | |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 77 | struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags) |
| 78 | { |
Michal Hocko | 19809c2 | 2017-05-08 15:57:44 -0700 | [diff] [blame] | 79 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 80 | struct bpf_prog_aux *aux; |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 81 | struct bpf_prog *fp; |
| 82 | |
| 83 | size = round_up(size, PAGE_SIZE); |
| 84 | fp = __vmalloc(size, gfp_flags, PAGE_KERNEL); |
| 85 | if (fp == NULL) |
| 86 | return NULL; |
| 87 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 88 | aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags); |
| 89 | if (aux == NULL) { |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 90 | vfree(fp); |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | fp->pages = size / PAGE_SIZE; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 95 | fp->aux = aux; |
Daniel Borkmann | e9d8afa | 2015-10-29 14:58:08 +0100 | [diff] [blame] | 96 | fp->aux->prog = fp; |
Alexei Starovoitov | 60b58afc | 2017-12-14 17:55:14 -0800 | [diff] [blame] | 97 | fp->jit_requested = ebpf_jit_enabled(); |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 98 | |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 99 | INIT_LIST_HEAD_RCU(&fp->aux->ksym_lnode); |
| 100 | |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 101 | return fp; |
| 102 | } |
| 103 | EXPORT_SYMBOL_GPL(bpf_prog_alloc); |
| 104 | |
| 105 | struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size, |
| 106 | gfp_t gfp_extra_flags) |
| 107 | { |
Michal Hocko | 19809c2 | 2017-05-08 15:57:44 -0700 | [diff] [blame] | 108 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags; |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 109 | struct bpf_prog *fp; |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 110 | u32 pages, delta; |
| 111 | int ret; |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 112 | |
| 113 | BUG_ON(fp_old == NULL); |
| 114 | |
| 115 | size = round_up(size, PAGE_SIZE); |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 116 | pages = size / PAGE_SIZE; |
| 117 | if (pages <= fp_old->pages) |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 118 | return fp_old; |
| 119 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 120 | delta = pages - fp_old->pages; |
| 121 | ret = __bpf_prog_charge(fp_old->aux->user, delta); |
| 122 | if (ret) |
| 123 | return NULL; |
| 124 | |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 125 | fp = __vmalloc(size, gfp_flags, PAGE_KERNEL); |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 126 | if (fp == NULL) { |
| 127 | __bpf_prog_uncharge(fp_old->aux->user, delta); |
| 128 | } else { |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 129 | memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE); |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 130 | fp->pages = pages; |
Daniel Borkmann | e9d8afa | 2015-10-29 14:58:08 +0100 | [diff] [blame] | 131 | fp->aux->prog = fp; |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 132 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 133 | /* We keep fp->aux from fp_old around in the new |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 134 | * reallocated structure. |
| 135 | */ |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 136 | fp_old->aux = NULL; |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 137 | __bpf_prog_free(fp_old); |
| 138 | } |
| 139 | |
| 140 | return fp; |
| 141 | } |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 142 | |
| 143 | void __bpf_prog_free(struct bpf_prog *fp) |
| 144 | { |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 145 | kfree(fp->aux); |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 146 | vfree(fp); |
| 147 | } |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 148 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 149 | int bpf_prog_calc_tag(struct bpf_prog *fp) |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 150 | { |
| 151 | const u32 bits_offset = SHA_MESSAGE_BYTES - sizeof(__be64); |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 152 | u32 raw_size = bpf_prog_tag_scratch_size(fp); |
| 153 | u32 digest[SHA_DIGEST_WORDS]; |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 154 | u32 ws[SHA_WORKSPACE_WORDS]; |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 155 | u32 i, bsize, psize, blocks; |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 156 | struct bpf_insn *dst; |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 157 | bool was_ld_map; |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 158 | u8 *raw, *todo; |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 159 | __be32 *result; |
| 160 | __be64 *bits; |
| 161 | |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 162 | raw = vmalloc(raw_size); |
| 163 | if (!raw) |
| 164 | return -ENOMEM; |
| 165 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 166 | sha_init(digest); |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 167 | memset(ws, 0, sizeof(ws)); |
| 168 | |
| 169 | /* We need to take out the map fd for the digest calculation |
| 170 | * since they are unstable from user space side. |
| 171 | */ |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 172 | dst = (void *)raw; |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 173 | for (i = 0, was_ld_map = false; i < fp->len; i++) { |
| 174 | dst[i] = fp->insnsi[i]; |
| 175 | if (!was_ld_map && |
| 176 | dst[i].code == (BPF_LD | BPF_IMM | BPF_DW) && |
| 177 | dst[i].src_reg == BPF_PSEUDO_MAP_FD) { |
| 178 | was_ld_map = true; |
| 179 | dst[i].imm = 0; |
| 180 | } else if (was_ld_map && |
| 181 | dst[i].code == 0 && |
| 182 | dst[i].dst_reg == 0 && |
| 183 | dst[i].src_reg == 0 && |
| 184 | dst[i].off == 0) { |
| 185 | was_ld_map = false; |
| 186 | dst[i].imm = 0; |
| 187 | } else { |
| 188 | was_ld_map = false; |
| 189 | } |
| 190 | } |
| 191 | |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 192 | psize = bpf_prog_insn_size(fp); |
| 193 | memset(&raw[psize], 0, raw_size - psize); |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 194 | raw[psize++] = 0x80; |
| 195 | |
| 196 | bsize = round_up(psize, SHA_MESSAGE_BYTES); |
| 197 | blocks = bsize / SHA_MESSAGE_BYTES; |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 198 | todo = raw; |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 199 | if (bsize - psize >= sizeof(__be64)) { |
| 200 | bits = (__be64 *)(todo + bsize - sizeof(__be64)); |
| 201 | } else { |
| 202 | bits = (__be64 *)(todo + bsize + bits_offset); |
| 203 | blocks++; |
| 204 | } |
| 205 | *bits = cpu_to_be64((psize - 1) << 3); |
| 206 | |
| 207 | while (blocks--) { |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 208 | sha_transform(digest, todo, ws); |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 209 | todo += SHA_MESSAGE_BYTES; |
| 210 | } |
| 211 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 212 | result = (__force __be32 *)digest; |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 213 | for (i = 0; i < SHA_DIGEST_WORDS; i++) |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 214 | result[i] = cpu_to_be32(digest[i]); |
| 215 | memcpy(fp->tag, result, sizeof(fp->tag)); |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 216 | |
| 217 | vfree(raw); |
| 218 | return 0; |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 219 | } |
| 220 | |
Daniel Borkmann | c237ee5 | 2016-05-13 19:08:30 +0200 | [diff] [blame] | 221 | static void bpf_adj_branches(struct bpf_prog *prog, u32 pos, u32 delta) |
| 222 | { |
| 223 | struct bpf_insn *insn = prog->insnsi; |
| 224 | u32 i, insn_cnt = prog->len; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 225 | bool pseudo_call; |
| 226 | u8 code; |
| 227 | int off; |
Daniel Borkmann | c237ee5 | 2016-05-13 19:08:30 +0200 | [diff] [blame] | 228 | |
| 229 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 230 | code = insn->code; |
| 231 | if (BPF_CLASS(code) != BPF_JMP) |
Daniel Borkmann | c237ee5 | 2016-05-13 19:08:30 +0200 | [diff] [blame] | 232 | continue; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 233 | if (BPF_OP(code) == BPF_EXIT) |
| 234 | continue; |
| 235 | if (BPF_OP(code) == BPF_CALL) { |
| 236 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 237 | pseudo_call = true; |
| 238 | else |
| 239 | continue; |
| 240 | } else { |
| 241 | pseudo_call = false; |
| 242 | } |
| 243 | off = pseudo_call ? insn->imm : insn->off; |
Daniel Borkmann | c237ee5 | 2016-05-13 19:08:30 +0200 | [diff] [blame] | 244 | |
| 245 | /* Adjust offset of jmps if we cross boundaries. */ |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 246 | if (i < pos && i + off + 1 > pos) |
| 247 | off += delta; |
| 248 | else if (i > pos + delta && i + off + 1 <= pos + delta) |
| 249 | off -= delta; |
| 250 | |
| 251 | if (pseudo_call) |
| 252 | insn->imm = off; |
| 253 | else |
| 254 | insn->off = off; |
Daniel Borkmann | c237ee5 | 2016-05-13 19:08:30 +0200 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off, |
| 259 | const struct bpf_insn *patch, u32 len) |
| 260 | { |
| 261 | u32 insn_adj_cnt, insn_rest, insn_delta = len - 1; |
| 262 | struct bpf_prog *prog_adj; |
| 263 | |
| 264 | /* Since our patchlet doesn't expand the image, we're done. */ |
| 265 | if (insn_delta == 0) { |
| 266 | memcpy(prog->insnsi + off, patch, sizeof(*patch)); |
| 267 | return prog; |
| 268 | } |
| 269 | |
| 270 | insn_adj_cnt = prog->len + insn_delta; |
| 271 | |
| 272 | /* Several new instructions need to be inserted. Make room |
| 273 | * for them. Likely, there's no need for a new allocation as |
| 274 | * last page could have large enough tailroom. |
| 275 | */ |
| 276 | prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt), |
| 277 | GFP_USER); |
| 278 | if (!prog_adj) |
| 279 | return NULL; |
| 280 | |
| 281 | prog_adj->len = insn_adj_cnt; |
| 282 | |
| 283 | /* Patching happens in 3 steps: |
| 284 | * |
| 285 | * 1) Move over tail of insnsi from next instruction onwards, |
| 286 | * so we can patch the single target insn with one or more |
| 287 | * new ones (patching is always from 1 to n insns, n > 0). |
| 288 | * 2) Inject new instructions at the target location. |
| 289 | * 3) Adjust branch offsets if necessary. |
| 290 | */ |
| 291 | insn_rest = insn_adj_cnt - off - len; |
| 292 | |
| 293 | memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1, |
| 294 | sizeof(*patch) * insn_rest); |
| 295 | memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len); |
| 296 | |
| 297 | bpf_adj_branches(prog_adj, off, insn_delta); |
| 298 | |
| 299 | return prog_adj; |
| 300 | } |
| 301 | |
Daniel Borkmann | b954d83 | 2014-09-10 15:01:02 +0200 | [diff] [blame] | 302 | #ifdef CONFIG_BPF_JIT |
Daniel Borkmann | fa9dd59 | 2018-01-20 01:24:33 +0100 | [diff] [blame] | 303 | /* All BPF JIT sysctl knobs here. */ |
| 304 | int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_ALWAYS_ON); |
| 305 | int bpf_jit_harden __read_mostly; |
| 306 | int bpf_jit_kallsyms __read_mostly; |
| 307 | |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 308 | static __always_inline void |
| 309 | bpf_get_prog_addr_region(const struct bpf_prog *prog, |
| 310 | unsigned long *symbol_start, |
| 311 | unsigned long *symbol_end) |
| 312 | { |
| 313 | const struct bpf_binary_header *hdr = bpf_jit_binary_hdr(prog); |
| 314 | unsigned long addr = (unsigned long)hdr; |
| 315 | |
| 316 | WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog)); |
| 317 | |
| 318 | *symbol_start = addr; |
| 319 | *symbol_end = addr + hdr->pages * PAGE_SIZE; |
| 320 | } |
| 321 | |
| 322 | static void bpf_get_prog_name(const struct bpf_prog *prog, char *sym) |
| 323 | { |
Martin KaFai Lau | 368211f | 2017-10-05 21:52:13 -0700 | [diff] [blame] | 324 | const char *end = sym + KSYM_NAME_LEN; |
| 325 | |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 326 | BUILD_BUG_ON(sizeof("bpf_prog_") + |
Martin KaFai Lau | 368211f | 2017-10-05 21:52:13 -0700 | [diff] [blame] | 327 | sizeof(prog->tag) * 2 + |
| 328 | /* name has been null terminated. |
| 329 | * We should need +1 for the '_' preceding |
| 330 | * the name. However, the null character |
| 331 | * is double counted between the name and the |
| 332 | * sizeof("bpf_prog_") above, so we omit |
| 333 | * the +1 here. |
| 334 | */ |
| 335 | sizeof(prog->aux->name) > KSYM_NAME_LEN); |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 336 | |
| 337 | sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_"); |
| 338 | sym = bin2hex(sym, prog->tag, sizeof(prog->tag)); |
Martin KaFai Lau | 368211f | 2017-10-05 21:52:13 -0700 | [diff] [blame] | 339 | if (prog->aux->name[0]) |
| 340 | snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name); |
| 341 | else |
| 342 | *sym = 0; |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | static __always_inline unsigned long |
| 346 | bpf_get_prog_addr_start(struct latch_tree_node *n) |
| 347 | { |
| 348 | unsigned long symbol_start, symbol_end; |
| 349 | const struct bpf_prog_aux *aux; |
| 350 | |
| 351 | aux = container_of(n, struct bpf_prog_aux, ksym_tnode); |
| 352 | bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end); |
| 353 | |
| 354 | return symbol_start; |
| 355 | } |
| 356 | |
| 357 | static __always_inline bool bpf_tree_less(struct latch_tree_node *a, |
| 358 | struct latch_tree_node *b) |
| 359 | { |
| 360 | return bpf_get_prog_addr_start(a) < bpf_get_prog_addr_start(b); |
| 361 | } |
| 362 | |
| 363 | static __always_inline int bpf_tree_comp(void *key, struct latch_tree_node *n) |
| 364 | { |
| 365 | unsigned long val = (unsigned long)key; |
| 366 | unsigned long symbol_start, symbol_end; |
| 367 | const struct bpf_prog_aux *aux; |
| 368 | |
| 369 | aux = container_of(n, struct bpf_prog_aux, ksym_tnode); |
| 370 | bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end); |
| 371 | |
| 372 | if (val < symbol_start) |
| 373 | return -1; |
| 374 | if (val >= symbol_end) |
| 375 | return 1; |
| 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | static const struct latch_tree_ops bpf_tree_ops = { |
| 381 | .less = bpf_tree_less, |
| 382 | .comp = bpf_tree_comp, |
| 383 | }; |
| 384 | |
| 385 | static DEFINE_SPINLOCK(bpf_lock); |
| 386 | static LIST_HEAD(bpf_kallsyms); |
| 387 | static struct latch_tree_root bpf_tree __cacheline_aligned; |
| 388 | |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 389 | static void bpf_prog_ksym_node_add(struct bpf_prog_aux *aux) |
| 390 | { |
| 391 | WARN_ON_ONCE(!list_empty(&aux->ksym_lnode)); |
| 392 | list_add_tail_rcu(&aux->ksym_lnode, &bpf_kallsyms); |
| 393 | latch_tree_insert(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops); |
| 394 | } |
| 395 | |
| 396 | static void bpf_prog_ksym_node_del(struct bpf_prog_aux *aux) |
| 397 | { |
| 398 | if (list_empty(&aux->ksym_lnode)) |
| 399 | return; |
| 400 | |
| 401 | latch_tree_erase(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops); |
| 402 | list_del_rcu(&aux->ksym_lnode); |
| 403 | } |
| 404 | |
| 405 | static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp) |
| 406 | { |
| 407 | return fp->jited && !bpf_prog_was_classic(fp); |
| 408 | } |
| 409 | |
| 410 | static bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp) |
| 411 | { |
| 412 | return list_empty(&fp->aux->ksym_lnode) || |
| 413 | fp->aux->ksym_lnode.prev == LIST_POISON2; |
| 414 | } |
| 415 | |
| 416 | void bpf_prog_kallsyms_add(struct bpf_prog *fp) |
| 417 | { |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 418 | if (!bpf_prog_kallsyms_candidate(fp) || |
| 419 | !capable(CAP_SYS_ADMIN)) |
| 420 | return; |
| 421 | |
Hannes Frederic Sowa | d24f7c7 | 2017-04-27 01:39:33 +0200 | [diff] [blame] | 422 | spin_lock_bh(&bpf_lock); |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 423 | bpf_prog_ksym_node_add(fp->aux); |
Hannes Frederic Sowa | d24f7c7 | 2017-04-27 01:39:33 +0200 | [diff] [blame] | 424 | spin_unlock_bh(&bpf_lock); |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | void bpf_prog_kallsyms_del(struct bpf_prog *fp) |
| 428 | { |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 429 | if (!bpf_prog_kallsyms_candidate(fp)) |
| 430 | return; |
| 431 | |
Hannes Frederic Sowa | d24f7c7 | 2017-04-27 01:39:33 +0200 | [diff] [blame] | 432 | spin_lock_bh(&bpf_lock); |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 433 | bpf_prog_ksym_node_del(fp->aux); |
Hannes Frederic Sowa | d24f7c7 | 2017-04-27 01:39:33 +0200 | [diff] [blame] | 434 | spin_unlock_bh(&bpf_lock); |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static struct bpf_prog *bpf_prog_kallsyms_find(unsigned long addr) |
| 438 | { |
| 439 | struct latch_tree_node *n; |
| 440 | |
| 441 | if (!bpf_jit_kallsyms_enabled()) |
| 442 | return NULL; |
| 443 | |
| 444 | n = latch_tree_find((void *)addr, &bpf_tree, &bpf_tree_ops); |
| 445 | return n ? |
| 446 | container_of(n, struct bpf_prog_aux, ksym_tnode)->prog : |
| 447 | NULL; |
| 448 | } |
| 449 | |
| 450 | const char *__bpf_address_lookup(unsigned long addr, unsigned long *size, |
| 451 | unsigned long *off, char *sym) |
| 452 | { |
| 453 | unsigned long symbol_start, symbol_end; |
| 454 | struct bpf_prog *prog; |
| 455 | char *ret = NULL; |
| 456 | |
| 457 | rcu_read_lock(); |
| 458 | prog = bpf_prog_kallsyms_find(addr); |
| 459 | if (prog) { |
| 460 | bpf_get_prog_addr_region(prog, &symbol_start, &symbol_end); |
| 461 | bpf_get_prog_name(prog, sym); |
| 462 | |
| 463 | ret = sym; |
| 464 | if (size) |
| 465 | *size = symbol_end - symbol_start; |
| 466 | if (off) |
| 467 | *off = addr - symbol_start; |
| 468 | } |
| 469 | rcu_read_unlock(); |
| 470 | |
| 471 | return ret; |
| 472 | } |
| 473 | |
| 474 | bool is_bpf_text_address(unsigned long addr) |
| 475 | { |
| 476 | bool ret; |
| 477 | |
| 478 | rcu_read_lock(); |
| 479 | ret = bpf_prog_kallsyms_find(addr) != NULL; |
| 480 | rcu_read_unlock(); |
| 481 | |
| 482 | return ret; |
| 483 | } |
| 484 | |
| 485 | int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type, |
| 486 | char *sym) |
| 487 | { |
| 488 | unsigned long symbol_start, symbol_end; |
| 489 | struct bpf_prog_aux *aux; |
| 490 | unsigned int it = 0; |
| 491 | int ret = -ERANGE; |
| 492 | |
| 493 | if (!bpf_jit_kallsyms_enabled()) |
| 494 | return ret; |
| 495 | |
| 496 | rcu_read_lock(); |
| 497 | list_for_each_entry_rcu(aux, &bpf_kallsyms, ksym_lnode) { |
| 498 | if (it++ != symnum) |
| 499 | continue; |
| 500 | |
| 501 | bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end); |
| 502 | bpf_get_prog_name(aux->prog, sym); |
| 503 | |
| 504 | *value = symbol_start; |
| 505 | *type = BPF_SYM_ELF_TYPE; |
| 506 | |
| 507 | ret = 0; |
| 508 | break; |
| 509 | } |
| 510 | rcu_read_unlock(); |
| 511 | |
| 512 | return ret; |
| 513 | } |
| 514 | |
Daniel Borkmann | 738cbe7 | 2014-09-08 08:04:47 +0200 | [diff] [blame] | 515 | struct bpf_binary_header * |
| 516 | bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr, |
| 517 | unsigned int alignment, |
| 518 | bpf_jit_fill_hole_t bpf_fill_ill_insns) |
| 519 | { |
| 520 | struct bpf_binary_header *hdr; |
| 521 | unsigned int size, hole, start; |
| 522 | |
| 523 | /* Most of BPF filters are really small, but if some of them |
| 524 | * fill a page, allow at least 128 extra bytes to insert a |
| 525 | * random section of illegal instructions. |
| 526 | */ |
| 527 | size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE); |
| 528 | hdr = module_alloc(size); |
| 529 | if (hdr == NULL) |
| 530 | return NULL; |
| 531 | |
| 532 | /* Fill space with illegal/arch-dep instructions. */ |
| 533 | bpf_fill_ill_insns(hdr, size); |
| 534 | |
| 535 | hdr->pages = size / PAGE_SIZE; |
| 536 | hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)), |
| 537 | PAGE_SIZE - sizeof(*hdr)); |
Daniel Borkmann | b7552e1b | 2016-05-18 14:14:28 +0200 | [diff] [blame] | 538 | start = (get_random_int() % hole) & ~(alignment - 1); |
Daniel Borkmann | 738cbe7 | 2014-09-08 08:04:47 +0200 | [diff] [blame] | 539 | |
| 540 | /* Leave a random number of instructions before BPF code. */ |
| 541 | *image_ptr = &hdr->image[start]; |
| 542 | |
| 543 | return hdr; |
| 544 | } |
| 545 | |
| 546 | void bpf_jit_binary_free(struct bpf_binary_header *hdr) |
| 547 | { |
Rusty Russell | be1f221 | 2015-01-20 09:07:05 +1030 | [diff] [blame] | 548 | module_memfree(hdr); |
Daniel Borkmann | 738cbe7 | 2014-09-08 08:04:47 +0200 | [diff] [blame] | 549 | } |
Daniel Borkmann | 4f3446b | 2016-05-13 19:08:32 +0200 | [diff] [blame] | 550 | |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 551 | /* This symbol is only overridden by archs that have different |
| 552 | * requirements than the usual eBPF JITs, f.e. when they only |
| 553 | * implement cBPF JIT, do not set images read-only, etc. |
| 554 | */ |
| 555 | void __weak bpf_jit_free(struct bpf_prog *fp) |
| 556 | { |
| 557 | if (fp->jited) { |
| 558 | struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp); |
| 559 | |
| 560 | bpf_jit_binary_unlock_ro(hdr); |
| 561 | bpf_jit_binary_free(hdr); |
| 562 | |
| 563 | WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp)); |
| 564 | } |
| 565 | |
| 566 | bpf_prog_unlock_free(fp); |
| 567 | } |
| 568 | |
Daniel Borkmann | 4f3446b | 2016-05-13 19:08:32 +0200 | [diff] [blame] | 569 | static int bpf_jit_blind_insn(const struct bpf_insn *from, |
| 570 | const struct bpf_insn *aux, |
| 571 | struct bpf_insn *to_buff) |
| 572 | { |
| 573 | struct bpf_insn *to = to_buff; |
Daniel Borkmann | b7552e1b | 2016-05-18 14:14:28 +0200 | [diff] [blame] | 574 | u32 imm_rnd = get_random_int(); |
Daniel Borkmann | 4f3446b | 2016-05-13 19:08:32 +0200 | [diff] [blame] | 575 | s16 off; |
| 576 | |
| 577 | BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG); |
| 578 | BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG); |
| 579 | |
| 580 | if (from->imm == 0 && |
| 581 | (from->code == (BPF_ALU | BPF_MOV | BPF_K) || |
| 582 | from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) { |
| 583 | *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg); |
| 584 | goto out; |
| 585 | } |
| 586 | |
| 587 | switch (from->code) { |
| 588 | case BPF_ALU | BPF_ADD | BPF_K: |
| 589 | case BPF_ALU | BPF_SUB | BPF_K: |
| 590 | case BPF_ALU | BPF_AND | BPF_K: |
| 591 | case BPF_ALU | BPF_OR | BPF_K: |
| 592 | case BPF_ALU | BPF_XOR | BPF_K: |
| 593 | case BPF_ALU | BPF_MUL | BPF_K: |
| 594 | case BPF_ALU | BPF_MOV | BPF_K: |
| 595 | case BPF_ALU | BPF_DIV | BPF_K: |
| 596 | case BPF_ALU | BPF_MOD | BPF_K: |
| 597 | *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); |
| 598 | *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); |
| 599 | *to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX); |
| 600 | break; |
| 601 | |
| 602 | case BPF_ALU64 | BPF_ADD | BPF_K: |
| 603 | case BPF_ALU64 | BPF_SUB | BPF_K: |
| 604 | case BPF_ALU64 | BPF_AND | BPF_K: |
| 605 | case BPF_ALU64 | BPF_OR | BPF_K: |
| 606 | case BPF_ALU64 | BPF_XOR | BPF_K: |
| 607 | case BPF_ALU64 | BPF_MUL | BPF_K: |
| 608 | case BPF_ALU64 | BPF_MOV | BPF_K: |
| 609 | case BPF_ALU64 | BPF_DIV | BPF_K: |
| 610 | case BPF_ALU64 | BPF_MOD | BPF_K: |
| 611 | *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); |
| 612 | *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); |
| 613 | *to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX); |
| 614 | break; |
| 615 | |
| 616 | case BPF_JMP | BPF_JEQ | BPF_K: |
| 617 | case BPF_JMP | BPF_JNE | BPF_K: |
| 618 | case BPF_JMP | BPF_JGT | BPF_K: |
Daniel Borkmann | 92b31a9 | 2017-08-10 01:39:55 +0200 | [diff] [blame] | 619 | case BPF_JMP | BPF_JLT | BPF_K: |
Daniel Borkmann | 4f3446b | 2016-05-13 19:08:32 +0200 | [diff] [blame] | 620 | case BPF_JMP | BPF_JGE | BPF_K: |
Daniel Borkmann | 92b31a9 | 2017-08-10 01:39:55 +0200 | [diff] [blame] | 621 | case BPF_JMP | BPF_JLE | BPF_K: |
Daniel Borkmann | 4f3446b | 2016-05-13 19:08:32 +0200 | [diff] [blame] | 622 | case BPF_JMP | BPF_JSGT | BPF_K: |
Daniel Borkmann | 92b31a9 | 2017-08-10 01:39:55 +0200 | [diff] [blame] | 623 | case BPF_JMP | BPF_JSLT | BPF_K: |
Daniel Borkmann | 4f3446b | 2016-05-13 19:08:32 +0200 | [diff] [blame] | 624 | case BPF_JMP | BPF_JSGE | BPF_K: |
Daniel Borkmann | 92b31a9 | 2017-08-10 01:39:55 +0200 | [diff] [blame] | 625 | case BPF_JMP | BPF_JSLE | BPF_K: |
Daniel Borkmann | 4f3446b | 2016-05-13 19:08:32 +0200 | [diff] [blame] | 626 | case BPF_JMP | BPF_JSET | BPF_K: |
| 627 | /* Accommodate for extra offset in case of a backjump. */ |
| 628 | off = from->off; |
| 629 | if (off < 0) |
| 630 | off -= 2; |
| 631 | *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); |
| 632 | *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); |
| 633 | *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off); |
| 634 | break; |
| 635 | |
| 636 | case BPF_LD | BPF_ABS | BPF_W: |
| 637 | case BPF_LD | BPF_ABS | BPF_H: |
| 638 | case BPF_LD | BPF_ABS | BPF_B: |
| 639 | *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); |
| 640 | *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); |
| 641 | *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0); |
| 642 | break; |
| 643 | |
| 644 | case BPF_LD | BPF_IND | BPF_W: |
| 645 | case BPF_LD | BPF_IND | BPF_H: |
| 646 | case BPF_LD | BPF_IND | BPF_B: |
| 647 | *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); |
| 648 | *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); |
| 649 | *to++ = BPF_ALU32_REG(BPF_ADD, BPF_REG_AX, from->src_reg); |
| 650 | *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0); |
| 651 | break; |
| 652 | |
| 653 | case BPF_LD | BPF_IMM | BPF_DW: |
| 654 | *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm); |
| 655 | *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); |
| 656 | *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); |
| 657 | *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX); |
| 658 | break; |
| 659 | case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */ |
| 660 | *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm); |
| 661 | *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); |
| 662 | *to++ = BPF_ALU64_REG(BPF_OR, aux[0].dst_reg, BPF_REG_AX); |
| 663 | break; |
| 664 | |
| 665 | case BPF_ST | BPF_MEM | BPF_DW: |
| 666 | case BPF_ST | BPF_MEM | BPF_W: |
| 667 | case BPF_ST | BPF_MEM | BPF_H: |
| 668 | case BPF_ST | BPF_MEM | BPF_B: |
| 669 | *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); |
| 670 | *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); |
| 671 | *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off); |
| 672 | break; |
| 673 | } |
| 674 | out: |
| 675 | return to - to_buff; |
| 676 | } |
| 677 | |
| 678 | static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other, |
| 679 | gfp_t gfp_extra_flags) |
| 680 | { |
Michal Hocko | 19809c2 | 2017-05-08 15:57:44 -0700 | [diff] [blame] | 681 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags; |
Daniel Borkmann | 4f3446b | 2016-05-13 19:08:32 +0200 | [diff] [blame] | 682 | struct bpf_prog *fp; |
| 683 | |
| 684 | fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags, PAGE_KERNEL); |
| 685 | if (fp != NULL) { |
Daniel Borkmann | 4f3446b | 2016-05-13 19:08:32 +0200 | [diff] [blame] | 686 | /* aux->prog still points to the fp_other one, so |
| 687 | * when promoting the clone to the real program, |
| 688 | * this still needs to be adapted. |
| 689 | */ |
| 690 | memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE); |
| 691 | } |
| 692 | |
| 693 | return fp; |
| 694 | } |
| 695 | |
| 696 | static void bpf_prog_clone_free(struct bpf_prog *fp) |
| 697 | { |
| 698 | /* aux was stolen by the other clone, so we cannot free |
| 699 | * it from this path! It will be freed eventually by the |
| 700 | * other program on release. |
| 701 | * |
| 702 | * At this point, we don't need a deferred release since |
| 703 | * clone is guaranteed to not be locked. |
| 704 | */ |
| 705 | fp->aux = NULL; |
| 706 | __bpf_prog_free(fp); |
| 707 | } |
| 708 | |
| 709 | void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other) |
| 710 | { |
| 711 | /* We have to repoint aux->prog to self, as we don't |
| 712 | * know whether fp here is the clone or the original. |
| 713 | */ |
| 714 | fp->aux->prog = fp; |
| 715 | bpf_prog_clone_free(fp_other); |
| 716 | } |
| 717 | |
| 718 | struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog) |
| 719 | { |
| 720 | struct bpf_insn insn_buff[16], aux[2]; |
| 721 | struct bpf_prog *clone, *tmp; |
| 722 | int insn_delta, insn_cnt; |
| 723 | struct bpf_insn *insn; |
| 724 | int i, rewritten; |
| 725 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 726 | if (!bpf_jit_blinding_enabled(prog) || prog->blinded) |
Daniel Borkmann | 4f3446b | 2016-05-13 19:08:32 +0200 | [diff] [blame] | 727 | return prog; |
| 728 | |
| 729 | clone = bpf_prog_clone_create(prog, GFP_USER); |
| 730 | if (!clone) |
| 731 | return ERR_PTR(-ENOMEM); |
| 732 | |
| 733 | insn_cnt = clone->len; |
| 734 | insn = clone->insnsi; |
| 735 | |
| 736 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 737 | /* We temporarily need to hold the original ld64 insn |
| 738 | * so that we can still access the first part in the |
| 739 | * second blinding run. |
| 740 | */ |
| 741 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) && |
| 742 | insn[1].code == 0) |
| 743 | memcpy(aux, insn, sizeof(aux)); |
| 744 | |
| 745 | rewritten = bpf_jit_blind_insn(insn, aux, insn_buff); |
| 746 | if (!rewritten) |
| 747 | continue; |
| 748 | |
| 749 | tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten); |
| 750 | if (!tmp) { |
| 751 | /* Patching may have repointed aux->prog during |
| 752 | * realloc from the original one, so we need to |
| 753 | * fix it up here on error. |
| 754 | */ |
| 755 | bpf_jit_prog_release_other(prog, clone); |
| 756 | return ERR_PTR(-ENOMEM); |
| 757 | } |
| 758 | |
| 759 | clone = tmp; |
| 760 | insn_delta = rewritten - 1; |
| 761 | |
| 762 | /* Walk new program and skip insns we just inserted. */ |
| 763 | insn = clone->insnsi + i + insn_delta; |
| 764 | insn_cnt += insn_delta; |
| 765 | i += insn_delta; |
| 766 | } |
| 767 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 768 | clone->blinded = 1; |
Daniel Borkmann | 4f3446b | 2016-05-13 19:08:32 +0200 | [diff] [blame] | 769 | return clone; |
| 770 | } |
Daniel Borkmann | b954d83 | 2014-09-10 15:01:02 +0200 | [diff] [blame] | 771 | #endif /* CONFIG_BPF_JIT */ |
Daniel Borkmann | 738cbe7 | 2014-09-08 08:04:47 +0200 | [diff] [blame] | 772 | |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 773 | /* Base function for offset calculation. Needs to go into .text section, |
| 774 | * therefore keeping it non-static as well; will also be used by JITs |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 775 | * anyway later on, so do not let the compiler omit it. This also needs |
| 776 | * to go into kallsyms for correlation from e.g. bpftool, so naming |
| 777 | * must not change. |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 778 | */ |
| 779 | noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 780 | { |
| 781 | return 0; |
| 782 | } |
Alexei Starovoitov | 4d9c5c5 | 2015-07-20 20:34:19 -0700 | [diff] [blame] | 783 | EXPORT_SYMBOL_GPL(__bpf_call_base); |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 784 | |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 785 | /* All UAPI available opcodes. */ |
| 786 | #define BPF_INSN_MAP(INSN_2, INSN_3) \ |
| 787 | /* 32 bit ALU operations. */ \ |
| 788 | /* Register based. */ \ |
| 789 | INSN_3(ALU, ADD, X), \ |
| 790 | INSN_3(ALU, SUB, X), \ |
| 791 | INSN_3(ALU, AND, X), \ |
| 792 | INSN_3(ALU, OR, X), \ |
| 793 | INSN_3(ALU, LSH, X), \ |
| 794 | INSN_3(ALU, RSH, X), \ |
| 795 | INSN_3(ALU, XOR, X), \ |
| 796 | INSN_3(ALU, MUL, X), \ |
| 797 | INSN_3(ALU, MOV, X), \ |
| 798 | INSN_3(ALU, DIV, X), \ |
| 799 | INSN_3(ALU, MOD, X), \ |
| 800 | INSN_2(ALU, NEG), \ |
| 801 | INSN_3(ALU, END, TO_BE), \ |
| 802 | INSN_3(ALU, END, TO_LE), \ |
| 803 | /* Immediate based. */ \ |
| 804 | INSN_3(ALU, ADD, K), \ |
| 805 | INSN_3(ALU, SUB, K), \ |
| 806 | INSN_3(ALU, AND, K), \ |
| 807 | INSN_3(ALU, OR, K), \ |
| 808 | INSN_3(ALU, LSH, K), \ |
| 809 | INSN_3(ALU, RSH, K), \ |
| 810 | INSN_3(ALU, XOR, K), \ |
| 811 | INSN_3(ALU, MUL, K), \ |
| 812 | INSN_3(ALU, MOV, K), \ |
| 813 | INSN_3(ALU, DIV, K), \ |
| 814 | INSN_3(ALU, MOD, K), \ |
| 815 | /* 64 bit ALU operations. */ \ |
| 816 | /* Register based. */ \ |
| 817 | INSN_3(ALU64, ADD, X), \ |
| 818 | INSN_3(ALU64, SUB, X), \ |
| 819 | INSN_3(ALU64, AND, X), \ |
| 820 | INSN_3(ALU64, OR, X), \ |
| 821 | INSN_3(ALU64, LSH, X), \ |
| 822 | INSN_3(ALU64, RSH, X), \ |
| 823 | INSN_3(ALU64, XOR, X), \ |
| 824 | INSN_3(ALU64, MUL, X), \ |
| 825 | INSN_3(ALU64, MOV, X), \ |
| 826 | INSN_3(ALU64, ARSH, X), \ |
| 827 | INSN_3(ALU64, DIV, X), \ |
| 828 | INSN_3(ALU64, MOD, X), \ |
| 829 | INSN_2(ALU64, NEG), \ |
| 830 | /* Immediate based. */ \ |
| 831 | INSN_3(ALU64, ADD, K), \ |
| 832 | INSN_3(ALU64, SUB, K), \ |
| 833 | INSN_3(ALU64, AND, K), \ |
| 834 | INSN_3(ALU64, OR, K), \ |
| 835 | INSN_3(ALU64, LSH, K), \ |
| 836 | INSN_3(ALU64, RSH, K), \ |
| 837 | INSN_3(ALU64, XOR, K), \ |
| 838 | INSN_3(ALU64, MUL, K), \ |
| 839 | INSN_3(ALU64, MOV, K), \ |
| 840 | INSN_3(ALU64, ARSH, K), \ |
| 841 | INSN_3(ALU64, DIV, K), \ |
| 842 | INSN_3(ALU64, MOD, K), \ |
| 843 | /* Call instruction. */ \ |
| 844 | INSN_2(JMP, CALL), \ |
| 845 | /* Exit instruction. */ \ |
| 846 | INSN_2(JMP, EXIT), \ |
| 847 | /* Jump instructions. */ \ |
| 848 | /* Register based. */ \ |
| 849 | INSN_3(JMP, JEQ, X), \ |
| 850 | INSN_3(JMP, JNE, X), \ |
| 851 | INSN_3(JMP, JGT, X), \ |
| 852 | INSN_3(JMP, JLT, X), \ |
| 853 | INSN_3(JMP, JGE, X), \ |
| 854 | INSN_3(JMP, JLE, X), \ |
| 855 | INSN_3(JMP, JSGT, X), \ |
| 856 | INSN_3(JMP, JSLT, X), \ |
| 857 | INSN_3(JMP, JSGE, X), \ |
| 858 | INSN_3(JMP, JSLE, X), \ |
| 859 | INSN_3(JMP, JSET, X), \ |
| 860 | /* Immediate based. */ \ |
| 861 | INSN_3(JMP, JEQ, K), \ |
| 862 | INSN_3(JMP, JNE, K), \ |
| 863 | INSN_3(JMP, JGT, K), \ |
| 864 | INSN_3(JMP, JLT, K), \ |
| 865 | INSN_3(JMP, JGE, K), \ |
| 866 | INSN_3(JMP, JLE, K), \ |
| 867 | INSN_3(JMP, JSGT, K), \ |
| 868 | INSN_3(JMP, JSLT, K), \ |
| 869 | INSN_3(JMP, JSGE, K), \ |
| 870 | INSN_3(JMP, JSLE, K), \ |
| 871 | INSN_3(JMP, JSET, K), \ |
| 872 | INSN_2(JMP, JA), \ |
| 873 | /* Store instructions. */ \ |
| 874 | /* Register based. */ \ |
| 875 | INSN_3(STX, MEM, B), \ |
| 876 | INSN_3(STX, MEM, H), \ |
| 877 | INSN_3(STX, MEM, W), \ |
| 878 | INSN_3(STX, MEM, DW), \ |
| 879 | INSN_3(STX, XADD, W), \ |
| 880 | INSN_3(STX, XADD, DW), \ |
| 881 | /* Immediate based. */ \ |
| 882 | INSN_3(ST, MEM, B), \ |
| 883 | INSN_3(ST, MEM, H), \ |
| 884 | INSN_3(ST, MEM, W), \ |
| 885 | INSN_3(ST, MEM, DW), \ |
| 886 | /* Load instructions. */ \ |
| 887 | /* Register based. */ \ |
| 888 | INSN_3(LDX, MEM, B), \ |
| 889 | INSN_3(LDX, MEM, H), \ |
| 890 | INSN_3(LDX, MEM, W), \ |
| 891 | INSN_3(LDX, MEM, DW), \ |
| 892 | /* Immediate based. */ \ |
| 893 | INSN_3(LD, IMM, DW), \ |
| 894 | /* Misc (old cBPF carry-over). */ \ |
| 895 | INSN_3(LD, ABS, B), \ |
| 896 | INSN_3(LD, ABS, H), \ |
| 897 | INSN_3(LD, ABS, W), \ |
| 898 | INSN_3(LD, IND, B), \ |
| 899 | INSN_3(LD, IND, H), \ |
| 900 | INSN_3(LD, IND, W) |
| 901 | |
| 902 | bool bpf_opcode_in_insntable(u8 code) |
| 903 | { |
| 904 | #define BPF_INSN_2_TBL(x, y) [BPF_##x | BPF_##y] = true |
| 905 | #define BPF_INSN_3_TBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = true |
| 906 | static const bool public_insntable[256] = { |
| 907 | [0 ... 255] = false, |
| 908 | /* Now overwrite non-defaults ... */ |
| 909 | BPF_INSN_MAP(BPF_INSN_2_TBL, BPF_INSN_3_TBL), |
| 910 | }; |
| 911 | #undef BPF_INSN_3_TBL |
| 912 | #undef BPF_INSN_2_TBL |
| 913 | return public_insntable[code]; |
| 914 | } |
| 915 | |
Alexei Starovoitov | 290af86 | 2018-01-09 10:04:29 -0800 | [diff] [blame] | 916 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 917 | /** |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 918 | * __bpf_prog_run - run eBPF program on a given context |
| 919 | * @ctx: is the data we are operating on |
| 920 | * @insn: is the array of eBPF instructions |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 921 | * |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 922 | * Decode and execute eBPF instructions. |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 923 | */ |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 924 | static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack) |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 925 | { |
Alexei Starovoitov | f696b8f | 2017-05-30 13:31:28 -0700 | [diff] [blame] | 926 | u64 tmp; |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 927 | #define BPF_INSN_2_LBL(x, y) [BPF_##x | BPF_##y] = &&x##_##y |
| 928 | #define BPF_INSN_3_LBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = &&x##_##y##_##z |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 929 | static const void *jumptable[256] = { |
| 930 | [0 ... 255] = &&default_label, |
| 931 | /* Now overwrite non-defaults ... */ |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 932 | BPF_INSN_MAP(BPF_INSN_2_LBL, BPF_INSN_3_LBL), |
| 933 | /* Non-UAPI available opcodes. */ |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 934 | [BPF_JMP | BPF_CALL_ARGS] = &&JMP_CALL_ARGS, |
Alexei Starovoitov | 71189fa | 2017-05-30 13:31:27 -0700 | [diff] [blame] | 935 | [BPF_JMP | BPF_TAIL_CALL] = &&JMP_TAIL_CALL, |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 936 | }; |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 937 | #undef BPF_INSN_3_LBL |
| 938 | #undef BPF_INSN_2_LBL |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 939 | u32 tail_call_cnt = 0; |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 940 | void *ptr; |
| 941 | int off; |
| 942 | |
| 943 | #define CONT ({ insn++; goto select_insn; }) |
| 944 | #define CONT_JMP ({ insn++; goto select_insn; }) |
| 945 | |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 946 | select_insn: |
| 947 | goto *jumptable[insn->code]; |
| 948 | |
| 949 | /* ALU */ |
| 950 | #define ALU(OPCODE, OP) \ |
| 951 | ALU64_##OPCODE##_X: \ |
| 952 | DST = DST OP SRC; \ |
| 953 | CONT; \ |
| 954 | ALU_##OPCODE##_X: \ |
| 955 | DST = (u32) DST OP (u32) SRC; \ |
| 956 | CONT; \ |
| 957 | ALU64_##OPCODE##_K: \ |
| 958 | DST = DST OP IMM; \ |
| 959 | CONT; \ |
| 960 | ALU_##OPCODE##_K: \ |
| 961 | DST = (u32) DST OP (u32) IMM; \ |
| 962 | CONT; |
| 963 | |
| 964 | ALU(ADD, +) |
| 965 | ALU(SUB, -) |
| 966 | ALU(AND, &) |
| 967 | ALU(OR, |) |
| 968 | ALU(LSH, <<) |
| 969 | ALU(RSH, >>) |
| 970 | ALU(XOR, ^) |
| 971 | ALU(MUL, *) |
| 972 | #undef ALU |
| 973 | ALU_NEG: |
| 974 | DST = (u32) -DST; |
| 975 | CONT; |
| 976 | ALU64_NEG: |
| 977 | DST = -DST; |
| 978 | CONT; |
| 979 | ALU_MOV_X: |
| 980 | DST = (u32) SRC; |
| 981 | CONT; |
| 982 | ALU_MOV_K: |
| 983 | DST = (u32) IMM; |
| 984 | CONT; |
| 985 | ALU64_MOV_X: |
| 986 | DST = SRC; |
| 987 | CONT; |
| 988 | ALU64_MOV_K: |
| 989 | DST = IMM; |
| 990 | CONT; |
Alexei Starovoitov | 02ab695 | 2014-09-04 22:17:17 -0700 | [diff] [blame] | 991 | LD_IMM_DW: |
| 992 | DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32; |
| 993 | insn++; |
| 994 | CONT; |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 995 | ALU64_ARSH_X: |
| 996 | (*(s64 *) &DST) >>= SRC; |
| 997 | CONT; |
| 998 | ALU64_ARSH_K: |
| 999 | (*(s64 *) &DST) >>= IMM; |
| 1000 | CONT; |
| 1001 | ALU64_MOD_X: |
Alexei Starovoitov | 876a7ae | 2015-04-27 14:40:37 -0700 | [diff] [blame] | 1002 | div64_u64_rem(DST, SRC, &tmp); |
| 1003 | DST = tmp; |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1004 | CONT; |
| 1005 | ALU_MOD_X: |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1006 | tmp = (u32) DST; |
| 1007 | DST = do_div(tmp, (u32) SRC); |
| 1008 | CONT; |
| 1009 | ALU64_MOD_K: |
Alexei Starovoitov | 876a7ae | 2015-04-27 14:40:37 -0700 | [diff] [blame] | 1010 | div64_u64_rem(DST, IMM, &tmp); |
| 1011 | DST = tmp; |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1012 | CONT; |
| 1013 | ALU_MOD_K: |
| 1014 | tmp = (u32) DST; |
| 1015 | DST = do_div(tmp, (u32) IMM); |
| 1016 | CONT; |
| 1017 | ALU64_DIV_X: |
Alexei Starovoitov | 876a7ae | 2015-04-27 14:40:37 -0700 | [diff] [blame] | 1018 | DST = div64_u64(DST, SRC); |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1019 | CONT; |
| 1020 | ALU_DIV_X: |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1021 | tmp = (u32) DST; |
| 1022 | do_div(tmp, (u32) SRC); |
| 1023 | DST = (u32) tmp; |
| 1024 | CONT; |
| 1025 | ALU64_DIV_K: |
Alexei Starovoitov | 876a7ae | 2015-04-27 14:40:37 -0700 | [diff] [blame] | 1026 | DST = div64_u64(DST, IMM); |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1027 | CONT; |
| 1028 | ALU_DIV_K: |
| 1029 | tmp = (u32) DST; |
| 1030 | do_div(tmp, (u32) IMM); |
| 1031 | DST = (u32) tmp; |
| 1032 | CONT; |
| 1033 | ALU_END_TO_BE: |
| 1034 | switch (IMM) { |
| 1035 | case 16: |
| 1036 | DST = (__force u16) cpu_to_be16(DST); |
| 1037 | break; |
| 1038 | case 32: |
| 1039 | DST = (__force u32) cpu_to_be32(DST); |
| 1040 | break; |
| 1041 | case 64: |
| 1042 | DST = (__force u64) cpu_to_be64(DST); |
| 1043 | break; |
| 1044 | } |
| 1045 | CONT; |
| 1046 | ALU_END_TO_LE: |
| 1047 | switch (IMM) { |
| 1048 | case 16: |
| 1049 | DST = (__force u16) cpu_to_le16(DST); |
| 1050 | break; |
| 1051 | case 32: |
| 1052 | DST = (__force u32) cpu_to_le32(DST); |
| 1053 | break; |
| 1054 | case 64: |
| 1055 | DST = (__force u64) cpu_to_le64(DST); |
| 1056 | break; |
| 1057 | } |
| 1058 | CONT; |
| 1059 | |
| 1060 | /* CALL */ |
| 1061 | JMP_CALL: |
| 1062 | /* Function call scratches BPF_R1-BPF_R5 registers, |
| 1063 | * preserves BPF_R6-BPF_R9, and stores return value |
| 1064 | * into BPF_R0. |
| 1065 | */ |
| 1066 | BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3, |
| 1067 | BPF_R4, BPF_R5); |
| 1068 | CONT; |
| 1069 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1070 | JMP_CALL_ARGS: |
| 1071 | BPF_R0 = (__bpf_call_base_args + insn->imm)(BPF_R1, BPF_R2, |
| 1072 | BPF_R3, BPF_R4, |
| 1073 | BPF_R5, |
| 1074 | insn + insn->off + 1); |
| 1075 | CONT; |
| 1076 | |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1077 | JMP_TAIL_CALL: { |
| 1078 | struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2; |
| 1079 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 1080 | struct bpf_prog *prog; |
Alexei Starovoitov | 90caccd | 2017-10-03 15:37:20 -0700 | [diff] [blame] | 1081 | u32 index = BPF_R3; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1082 | |
| 1083 | if (unlikely(index >= array->map.max_entries)) |
| 1084 | goto out; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1085 | if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT)) |
| 1086 | goto out; |
| 1087 | |
| 1088 | tail_call_cnt++; |
| 1089 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 1090 | prog = READ_ONCE(array->ptrs[index]); |
Daniel Borkmann | 1ca1cc9 | 2016-06-28 12:18:23 +0200 | [diff] [blame] | 1091 | if (!prog) |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1092 | goto out; |
| 1093 | |
Daniel Borkmann | c4675f9 | 2015-07-13 20:49:32 +0200 | [diff] [blame] | 1094 | /* ARG1 at this point is guaranteed to point to CTX from |
| 1095 | * the verifier side due to the fact that the tail call is |
| 1096 | * handeled like a helper, that is, bpf_tail_call_proto, |
| 1097 | * where arg1_type is ARG_PTR_TO_CTX. |
| 1098 | */ |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1099 | insn = prog->insnsi; |
| 1100 | goto select_insn; |
| 1101 | out: |
| 1102 | CONT; |
| 1103 | } |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1104 | /* JMP */ |
| 1105 | JMP_JA: |
| 1106 | insn += insn->off; |
| 1107 | CONT; |
| 1108 | JMP_JEQ_X: |
| 1109 | if (DST == SRC) { |
| 1110 | insn += insn->off; |
| 1111 | CONT_JMP; |
| 1112 | } |
| 1113 | CONT; |
| 1114 | JMP_JEQ_K: |
| 1115 | if (DST == IMM) { |
| 1116 | insn += insn->off; |
| 1117 | CONT_JMP; |
| 1118 | } |
| 1119 | CONT; |
| 1120 | JMP_JNE_X: |
| 1121 | if (DST != SRC) { |
| 1122 | insn += insn->off; |
| 1123 | CONT_JMP; |
| 1124 | } |
| 1125 | CONT; |
| 1126 | JMP_JNE_K: |
| 1127 | if (DST != IMM) { |
| 1128 | insn += insn->off; |
| 1129 | CONT_JMP; |
| 1130 | } |
| 1131 | CONT; |
| 1132 | JMP_JGT_X: |
| 1133 | if (DST > SRC) { |
| 1134 | insn += insn->off; |
| 1135 | CONT_JMP; |
| 1136 | } |
| 1137 | CONT; |
| 1138 | JMP_JGT_K: |
| 1139 | if (DST > IMM) { |
| 1140 | insn += insn->off; |
| 1141 | CONT_JMP; |
| 1142 | } |
| 1143 | CONT; |
Daniel Borkmann | 92b31a9 | 2017-08-10 01:39:55 +0200 | [diff] [blame] | 1144 | JMP_JLT_X: |
| 1145 | if (DST < SRC) { |
| 1146 | insn += insn->off; |
| 1147 | CONT_JMP; |
| 1148 | } |
| 1149 | CONT; |
| 1150 | JMP_JLT_K: |
| 1151 | if (DST < IMM) { |
| 1152 | insn += insn->off; |
| 1153 | CONT_JMP; |
| 1154 | } |
| 1155 | CONT; |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1156 | JMP_JGE_X: |
| 1157 | if (DST >= SRC) { |
| 1158 | insn += insn->off; |
| 1159 | CONT_JMP; |
| 1160 | } |
| 1161 | CONT; |
| 1162 | JMP_JGE_K: |
| 1163 | if (DST >= IMM) { |
| 1164 | insn += insn->off; |
| 1165 | CONT_JMP; |
| 1166 | } |
| 1167 | CONT; |
Daniel Borkmann | 92b31a9 | 2017-08-10 01:39:55 +0200 | [diff] [blame] | 1168 | JMP_JLE_X: |
| 1169 | if (DST <= SRC) { |
| 1170 | insn += insn->off; |
| 1171 | CONT_JMP; |
| 1172 | } |
| 1173 | CONT; |
| 1174 | JMP_JLE_K: |
| 1175 | if (DST <= IMM) { |
| 1176 | insn += insn->off; |
| 1177 | CONT_JMP; |
| 1178 | } |
| 1179 | CONT; |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1180 | JMP_JSGT_X: |
| 1181 | if (((s64) DST) > ((s64) SRC)) { |
| 1182 | insn += insn->off; |
| 1183 | CONT_JMP; |
| 1184 | } |
| 1185 | CONT; |
| 1186 | JMP_JSGT_K: |
| 1187 | if (((s64) DST) > ((s64) IMM)) { |
| 1188 | insn += insn->off; |
| 1189 | CONT_JMP; |
| 1190 | } |
| 1191 | CONT; |
Daniel Borkmann | 92b31a9 | 2017-08-10 01:39:55 +0200 | [diff] [blame] | 1192 | JMP_JSLT_X: |
| 1193 | if (((s64) DST) < ((s64) SRC)) { |
| 1194 | insn += insn->off; |
| 1195 | CONT_JMP; |
| 1196 | } |
| 1197 | CONT; |
| 1198 | JMP_JSLT_K: |
| 1199 | if (((s64) DST) < ((s64) IMM)) { |
| 1200 | insn += insn->off; |
| 1201 | CONT_JMP; |
| 1202 | } |
| 1203 | CONT; |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1204 | JMP_JSGE_X: |
| 1205 | if (((s64) DST) >= ((s64) SRC)) { |
| 1206 | insn += insn->off; |
| 1207 | CONT_JMP; |
| 1208 | } |
| 1209 | CONT; |
| 1210 | JMP_JSGE_K: |
| 1211 | if (((s64) DST) >= ((s64) IMM)) { |
| 1212 | insn += insn->off; |
| 1213 | CONT_JMP; |
| 1214 | } |
| 1215 | CONT; |
Daniel Borkmann | 92b31a9 | 2017-08-10 01:39:55 +0200 | [diff] [blame] | 1216 | JMP_JSLE_X: |
| 1217 | if (((s64) DST) <= ((s64) SRC)) { |
| 1218 | insn += insn->off; |
| 1219 | CONT_JMP; |
| 1220 | } |
| 1221 | CONT; |
| 1222 | JMP_JSLE_K: |
| 1223 | if (((s64) DST) <= ((s64) IMM)) { |
| 1224 | insn += insn->off; |
| 1225 | CONT_JMP; |
| 1226 | } |
| 1227 | CONT; |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1228 | JMP_JSET_X: |
| 1229 | if (DST & SRC) { |
| 1230 | insn += insn->off; |
| 1231 | CONT_JMP; |
| 1232 | } |
| 1233 | CONT; |
| 1234 | JMP_JSET_K: |
| 1235 | if (DST & IMM) { |
| 1236 | insn += insn->off; |
| 1237 | CONT_JMP; |
| 1238 | } |
| 1239 | CONT; |
| 1240 | JMP_EXIT: |
| 1241 | return BPF_R0; |
| 1242 | |
| 1243 | /* STX and ST and LDX*/ |
| 1244 | #define LDST(SIZEOP, SIZE) \ |
| 1245 | STX_MEM_##SIZEOP: \ |
| 1246 | *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \ |
| 1247 | CONT; \ |
| 1248 | ST_MEM_##SIZEOP: \ |
| 1249 | *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \ |
| 1250 | CONT; \ |
| 1251 | LDX_MEM_##SIZEOP: \ |
| 1252 | DST = *(SIZE *)(unsigned long) (SRC + insn->off); \ |
| 1253 | CONT; |
| 1254 | |
| 1255 | LDST(B, u8) |
| 1256 | LDST(H, u16) |
| 1257 | LDST(W, u32) |
| 1258 | LDST(DW, u64) |
| 1259 | #undef LDST |
| 1260 | STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */ |
| 1261 | atomic_add((u32) SRC, (atomic_t *)(unsigned long) |
| 1262 | (DST + insn->off)); |
| 1263 | CONT; |
| 1264 | STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */ |
| 1265 | atomic64_add((u64) SRC, (atomic64_t *)(unsigned long) |
| 1266 | (DST + insn->off)); |
| 1267 | CONT; |
| 1268 | LD_ABS_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + imm32)) */ |
| 1269 | off = IMM; |
| 1270 | load_word: |
Johannes Berg | 96a94cc | 2017-04-11 12:10:58 +0200 | [diff] [blame] | 1271 | /* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are only |
| 1272 | * appearing in the programs where ctx == skb |
| 1273 | * (see may_access_skb() in the verifier). All programs |
| 1274 | * keep 'ctx' in regs[BPF_REG_CTX] == BPF_R6, |
| 1275 | * bpf_convert_filter() saves it in BPF_R6, internal BPF |
| 1276 | * verifier will check that BPF_R6 == ctx. |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1277 | * |
| 1278 | * BPF_ABS and BPF_IND are wrappers of function calls, |
| 1279 | * so they scratch BPF_R1-BPF_R5 registers, preserve |
| 1280 | * BPF_R6-BPF_R9, and store return value into BPF_R0. |
| 1281 | * |
| 1282 | * Implicit input: |
| 1283 | * ctx == skb == BPF_R6 == CTX |
| 1284 | * |
| 1285 | * Explicit input: |
| 1286 | * SRC == any register |
| 1287 | * IMM == 32-bit immediate |
| 1288 | * |
| 1289 | * Output: |
| 1290 | * BPF_R0 - 8/16/32-bit skb data converted to cpu endianness |
| 1291 | */ |
| 1292 | |
| 1293 | ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 4, &tmp); |
| 1294 | if (likely(ptr != NULL)) { |
| 1295 | BPF_R0 = get_unaligned_be32(ptr); |
| 1296 | CONT; |
| 1297 | } |
| 1298 | |
| 1299 | return 0; |
| 1300 | LD_ABS_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + imm32)) */ |
| 1301 | off = IMM; |
| 1302 | load_half: |
| 1303 | ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 2, &tmp); |
| 1304 | if (likely(ptr != NULL)) { |
| 1305 | BPF_R0 = get_unaligned_be16(ptr); |
| 1306 | CONT; |
| 1307 | } |
| 1308 | |
| 1309 | return 0; |
| 1310 | LD_ABS_B: /* BPF_R0 = *(u8 *) (skb->data + imm32) */ |
| 1311 | off = IMM; |
| 1312 | load_byte: |
| 1313 | ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 1, &tmp); |
| 1314 | if (likely(ptr != NULL)) { |
| 1315 | BPF_R0 = *(u8 *)ptr; |
| 1316 | CONT; |
| 1317 | } |
| 1318 | |
| 1319 | return 0; |
| 1320 | LD_IND_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + src_reg + imm32)) */ |
| 1321 | off = IMM + SRC; |
| 1322 | goto load_word; |
| 1323 | LD_IND_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + src_reg + imm32)) */ |
| 1324 | off = IMM + SRC; |
| 1325 | goto load_half; |
| 1326 | LD_IND_B: /* BPF_R0 = *(u8 *) (skb->data + src_reg + imm32) */ |
| 1327 | off = IMM + SRC; |
| 1328 | goto load_byte; |
| 1329 | |
| 1330 | default_label: |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 1331 | /* If we ever reach this, we have a bug somewhere. Die hard here |
| 1332 | * instead of just returning 0; we could be somewhere in a subprog, |
| 1333 | * so execution could continue otherwise which we do /not/ want. |
| 1334 | * |
| 1335 | * Note, verifier whitelists all opcodes in bpf_opcode_in_insntable(). |
| 1336 | */ |
| 1337 | pr_warn("BPF interpreter: unknown opcode %02x\n", insn->code); |
| 1338 | BUG_ON(1); |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1339 | return 0; |
| 1340 | } |
Alexei Starovoitov | f696b8f | 2017-05-30 13:31:28 -0700 | [diff] [blame] | 1341 | STACK_FRAME_NON_STANDARD(___bpf_prog_run); /* jump table */ |
| 1342 | |
Alexei Starovoitov | b870aa9 | 2017-05-30 13:31:33 -0700 | [diff] [blame] | 1343 | #define PROG_NAME(stack_size) __bpf_prog_run##stack_size |
| 1344 | #define DEFINE_BPF_PROG_RUN(stack_size) \ |
| 1345 | static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \ |
| 1346 | { \ |
| 1347 | u64 stack[stack_size / sizeof(u64)]; \ |
| 1348 | u64 regs[MAX_BPF_REG]; \ |
| 1349 | \ |
| 1350 | FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \ |
| 1351 | ARG1 = (u64) (unsigned long) ctx; \ |
| 1352 | return ___bpf_prog_run(regs, insn, stack); \ |
Alexei Starovoitov | f696b8f | 2017-05-30 13:31:28 -0700 | [diff] [blame] | 1353 | } |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1354 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1355 | #define PROG_NAME_ARGS(stack_size) __bpf_prog_run_args##stack_size |
| 1356 | #define DEFINE_BPF_PROG_RUN_ARGS(stack_size) \ |
| 1357 | static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \ |
| 1358 | const struct bpf_insn *insn) \ |
| 1359 | { \ |
| 1360 | u64 stack[stack_size / sizeof(u64)]; \ |
| 1361 | u64 regs[MAX_BPF_REG]; \ |
| 1362 | \ |
| 1363 | FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \ |
| 1364 | BPF_R1 = r1; \ |
| 1365 | BPF_R2 = r2; \ |
| 1366 | BPF_R3 = r3; \ |
| 1367 | BPF_R4 = r4; \ |
| 1368 | BPF_R5 = r5; \ |
| 1369 | return ___bpf_prog_run(regs, insn, stack); \ |
| 1370 | } |
| 1371 | |
Alexei Starovoitov | b870aa9 | 2017-05-30 13:31:33 -0700 | [diff] [blame] | 1372 | #define EVAL1(FN, X) FN(X) |
| 1373 | #define EVAL2(FN, X, Y...) FN(X) EVAL1(FN, Y) |
| 1374 | #define EVAL3(FN, X, Y...) FN(X) EVAL2(FN, Y) |
| 1375 | #define EVAL4(FN, X, Y...) FN(X) EVAL3(FN, Y) |
| 1376 | #define EVAL5(FN, X, Y...) FN(X) EVAL4(FN, Y) |
| 1377 | #define EVAL6(FN, X, Y...) FN(X) EVAL5(FN, Y) |
| 1378 | |
| 1379 | EVAL6(DEFINE_BPF_PROG_RUN, 32, 64, 96, 128, 160, 192); |
| 1380 | EVAL6(DEFINE_BPF_PROG_RUN, 224, 256, 288, 320, 352, 384); |
| 1381 | EVAL4(DEFINE_BPF_PROG_RUN, 416, 448, 480, 512); |
| 1382 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1383 | EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 32, 64, 96, 128, 160, 192); |
| 1384 | EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 224, 256, 288, 320, 352, 384); |
| 1385 | EVAL4(DEFINE_BPF_PROG_RUN_ARGS, 416, 448, 480, 512); |
| 1386 | |
Alexei Starovoitov | b870aa9 | 2017-05-30 13:31:33 -0700 | [diff] [blame] | 1387 | #define PROG_NAME_LIST(stack_size) PROG_NAME(stack_size), |
| 1388 | |
| 1389 | static unsigned int (*interpreters[])(const void *ctx, |
| 1390 | const struct bpf_insn *insn) = { |
| 1391 | EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192) |
| 1392 | EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384) |
| 1393 | EVAL4(PROG_NAME_LIST, 416, 448, 480, 512) |
| 1394 | }; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1395 | #undef PROG_NAME_LIST |
| 1396 | #define PROG_NAME_LIST(stack_size) PROG_NAME_ARGS(stack_size), |
| 1397 | static u64 (*interpreters_args[])(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, |
| 1398 | const struct bpf_insn *insn) = { |
| 1399 | EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192) |
| 1400 | EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384) |
| 1401 | EVAL4(PROG_NAME_LIST, 416, 448, 480, 512) |
| 1402 | }; |
| 1403 | #undef PROG_NAME_LIST |
| 1404 | |
| 1405 | void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth) |
| 1406 | { |
| 1407 | stack_depth = max_t(u32, stack_depth, 1); |
| 1408 | insn->off = (s16) insn->imm; |
| 1409 | insn->imm = interpreters_args[(round_up(stack_depth, 32) / 32) - 1] - |
| 1410 | __bpf_call_base_args; |
| 1411 | insn->code = BPF_JMP | BPF_CALL_ARGS; |
| 1412 | } |
Alexei Starovoitov | b870aa9 | 2017-05-30 13:31:33 -0700 | [diff] [blame] | 1413 | |
Alexei Starovoitov | 290af86 | 2018-01-09 10:04:29 -0800 | [diff] [blame] | 1414 | #else |
Daniel Borkmann | fa9dd59 | 2018-01-20 01:24:33 +0100 | [diff] [blame] | 1415 | static unsigned int __bpf_prog_ret0_warn(const void *ctx, |
| 1416 | const struct bpf_insn *insn) |
Alexei Starovoitov | 290af86 | 2018-01-09 10:04:29 -0800 | [diff] [blame] | 1417 | { |
Daniel Borkmann | fa9dd59 | 2018-01-20 01:24:33 +0100 | [diff] [blame] | 1418 | /* If this handler ever gets executed, then BPF_JIT_ALWAYS_ON |
| 1419 | * is not working properly, so warn about it! |
| 1420 | */ |
| 1421 | WARN_ON_ONCE(1); |
Alexei Starovoitov | 290af86 | 2018-01-09 10:04:29 -0800 | [diff] [blame] | 1422 | return 0; |
| 1423 | } |
| 1424 | #endif |
| 1425 | |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1426 | bool bpf_prog_array_compatible(struct bpf_array *array, |
| 1427 | const struct bpf_prog *fp) |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1428 | { |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1429 | if (fp->kprobe_override) |
| 1430 | return false; |
| 1431 | |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1432 | if (!array->owner_prog_type) { |
| 1433 | /* There's no owner yet where we could check for |
| 1434 | * compatibility. |
| 1435 | */ |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1436 | array->owner_prog_type = fp->type; |
| 1437 | array->owner_jited = fp->jited; |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1438 | |
| 1439 | return true; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1440 | } |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1441 | |
| 1442 | return array->owner_prog_type == fp->type && |
| 1443 | array->owner_jited == fp->jited; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1446 | static int bpf_check_tail_call(const struct bpf_prog *fp) |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1447 | { |
| 1448 | struct bpf_prog_aux *aux = fp->aux; |
| 1449 | int i; |
| 1450 | |
| 1451 | for (i = 0; i < aux->used_map_cnt; i++) { |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1452 | struct bpf_map *map = aux->used_maps[i]; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1453 | struct bpf_array *array; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1454 | |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1455 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 1456 | continue; |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1457 | |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1458 | array = container_of(map, struct bpf_array, map); |
| 1459 | if (!bpf_prog_array_compatible(array, fp)) |
| 1460 | return -EINVAL; |
| 1461 | } |
| 1462 | |
| 1463 | return 0; |
| 1464 | } |
| 1465 | |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1466 | /** |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1467 | * bpf_prog_select_runtime - select exec runtime for BPF program |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1468 | * @fp: bpf_prog populated with internal BPF program |
Daniel Borkmann | d1c55ab | 2016-05-13 19:08:31 +0200 | [diff] [blame] | 1469 | * @err: pointer to error variable |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1470 | * |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1471 | * Try to JIT eBPF program, if JIT is not available, use interpreter. |
| 1472 | * The BPF program will be executed via BPF_PROG_RUN() macro. |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1473 | */ |
Daniel Borkmann | d1c55ab | 2016-05-13 19:08:31 +0200 | [diff] [blame] | 1474 | struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err) |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1475 | { |
Alexei Starovoitov | 290af86 | 2018-01-09 10:04:29 -0800 | [diff] [blame] | 1476 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Martin KaFai Lau | 8007e40 | 2017-06-28 10:41:24 -0700 | [diff] [blame] | 1477 | u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1); |
| 1478 | |
| 1479 | fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1]; |
Alexei Starovoitov | 290af86 | 2018-01-09 10:04:29 -0800 | [diff] [blame] | 1480 | #else |
Daniel Borkmann | fa9dd59 | 2018-01-20 01:24:33 +0100 | [diff] [blame] | 1481 | fp->bpf_func = __bpf_prog_ret0_warn; |
Alexei Starovoitov | 290af86 | 2018-01-09 10:04:29 -0800 | [diff] [blame] | 1482 | #endif |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1483 | |
Daniel Borkmann | d1c55ab | 2016-05-13 19:08:31 +0200 | [diff] [blame] | 1484 | /* eBPF JITs can rewrite the program in case constant |
| 1485 | * blinding is active. However, in case of error during |
| 1486 | * blinding, bpf_int_jit_compile() must always return a |
| 1487 | * valid program, which in this case would simply not |
| 1488 | * be JITed, but falls back to the interpreter. |
| 1489 | */ |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1490 | if (!bpf_prog_is_dev_bound(fp->aux)) { |
| 1491 | fp = bpf_int_jit_compile(fp); |
Alexei Starovoitov | 290af86 | 2018-01-09 10:04:29 -0800 | [diff] [blame] | 1492 | #ifdef CONFIG_BPF_JIT_ALWAYS_ON |
| 1493 | if (!fp->jited) { |
| 1494 | *err = -ENOTSUPP; |
| 1495 | return fp; |
| 1496 | } |
| 1497 | #endif |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1498 | } else { |
| 1499 | *err = bpf_prog_offload_compile(fp); |
| 1500 | if (*err) |
| 1501 | return fp; |
| 1502 | } |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 1503 | bpf_prog_lock_ro(fp); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1504 | |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1505 | /* The tail call compatibility check can only be done at |
| 1506 | * this late stage as we need to determine, if we deal |
| 1507 | * with JITed or non JITed program concatenations and not |
| 1508 | * all eBPF JITs might immediately support all features. |
| 1509 | */ |
Daniel Borkmann | d1c55ab | 2016-05-13 19:08:31 +0200 | [diff] [blame] | 1510 | *err = bpf_check_tail_call(fp); |
| 1511 | |
| 1512 | return fp; |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1513 | } |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1514 | EXPORT_SYMBOL_GPL(bpf_prog_select_runtime); |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1515 | |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1516 | static unsigned int __bpf_prog_ret1(const void *ctx, |
| 1517 | const struct bpf_insn *insn) |
| 1518 | { |
| 1519 | return 1; |
| 1520 | } |
| 1521 | |
| 1522 | static struct bpf_prog_dummy { |
| 1523 | struct bpf_prog prog; |
| 1524 | } dummy_bpf_prog = { |
| 1525 | .prog = { |
| 1526 | .bpf_func = __bpf_prog_ret1, |
| 1527 | }, |
| 1528 | }; |
| 1529 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1530 | /* to avoid allocating empty bpf_prog_array for cgroups that |
| 1531 | * don't have bpf program attached use one global 'empty_prog_array' |
| 1532 | * It will not be modified the caller of bpf_prog_array_alloc() |
| 1533 | * (since caller requested prog_cnt == 0) |
| 1534 | * that pointer should be 'freed' by bpf_prog_array_free() |
| 1535 | */ |
| 1536 | static struct { |
| 1537 | struct bpf_prog_array hdr; |
| 1538 | struct bpf_prog *null_prog; |
| 1539 | } empty_prog_array = { |
| 1540 | .null_prog = NULL, |
| 1541 | }; |
| 1542 | |
| 1543 | struct bpf_prog_array __rcu *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags) |
| 1544 | { |
| 1545 | if (prog_cnt) |
| 1546 | return kzalloc(sizeof(struct bpf_prog_array) + |
| 1547 | sizeof(struct bpf_prog *) * (prog_cnt + 1), |
| 1548 | flags); |
| 1549 | |
| 1550 | return &empty_prog_array.hdr; |
| 1551 | } |
| 1552 | |
| 1553 | void bpf_prog_array_free(struct bpf_prog_array __rcu *progs) |
| 1554 | { |
| 1555 | if (!progs || |
| 1556 | progs == (struct bpf_prog_array __rcu *)&empty_prog_array.hdr) |
| 1557 | return; |
| 1558 | kfree_rcu(progs, rcu); |
| 1559 | } |
| 1560 | |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1561 | int bpf_prog_array_length(struct bpf_prog_array __rcu *progs) |
| 1562 | { |
| 1563 | struct bpf_prog **prog; |
| 1564 | u32 cnt = 0; |
| 1565 | |
| 1566 | rcu_read_lock(); |
| 1567 | prog = rcu_dereference(progs)->progs; |
| 1568 | for (; *prog; prog++) |
Yonghong Song | c8c088b | 2017-11-30 13:47:54 -0800 | [diff] [blame] | 1569 | if (*prog != &dummy_bpf_prog.prog) |
| 1570 | cnt++; |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1571 | rcu_read_unlock(); |
| 1572 | return cnt; |
| 1573 | } |
| 1574 | |
| 1575 | int bpf_prog_array_copy_to_user(struct bpf_prog_array __rcu *progs, |
| 1576 | __u32 __user *prog_ids, u32 cnt) |
| 1577 | { |
| 1578 | struct bpf_prog **prog; |
Alexei Starovoitov | 0911287 | 2018-02-02 15:14:05 -0800 | [diff] [blame] | 1579 | unsigned long err = 0; |
| 1580 | u32 i = 0, *ids; |
| 1581 | bool nospc; |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1582 | |
Alexei Starovoitov | 0911287 | 2018-02-02 15:14:05 -0800 | [diff] [blame] | 1583 | /* users of this function are doing: |
| 1584 | * cnt = bpf_prog_array_length(); |
| 1585 | * if (cnt > 0) |
| 1586 | * bpf_prog_array_copy_to_user(..., cnt); |
| 1587 | * so below kcalloc doesn't need extra cnt > 0 check, but |
| 1588 | * bpf_prog_array_length() releases rcu lock and |
| 1589 | * prog array could have been swapped with empty or larger array, |
| 1590 | * so always copy 'cnt' prog_ids to the user. |
| 1591 | * In a rare race the user will see zero prog_ids |
| 1592 | */ |
Daniel Borkmann | 9c481b9 | 2018-02-14 15:31:00 +0100 | [diff] [blame^] | 1593 | ids = kcalloc(cnt, sizeof(u32), GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | 0911287 | 2018-02-02 15:14:05 -0800 | [diff] [blame] | 1594 | if (!ids) |
| 1595 | return -ENOMEM; |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1596 | rcu_read_lock(); |
| 1597 | prog = rcu_dereference(progs)->progs; |
| 1598 | for (; *prog; prog++) { |
Yonghong Song | f371b30 | 2017-12-11 11:39:02 -0800 | [diff] [blame] | 1599 | if (*prog == &dummy_bpf_prog.prog) |
| 1600 | continue; |
Alexei Starovoitov | 0911287 | 2018-02-02 15:14:05 -0800 | [diff] [blame] | 1601 | ids[i] = (*prog)->aux->id; |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1602 | if (++i == cnt) { |
| 1603 | prog++; |
| 1604 | break; |
| 1605 | } |
| 1606 | } |
Alexei Starovoitov | 0911287 | 2018-02-02 15:14:05 -0800 | [diff] [blame] | 1607 | nospc = !!(*prog); |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1608 | rcu_read_unlock(); |
Alexei Starovoitov | 0911287 | 2018-02-02 15:14:05 -0800 | [diff] [blame] | 1609 | err = copy_to_user(prog_ids, ids, cnt * sizeof(u32)); |
| 1610 | kfree(ids); |
| 1611 | if (err) |
| 1612 | return -EFAULT; |
| 1613 | if (nospc) |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1614 | return -ENOSPC; |
| 1615 | return 0; |
| 1616 | } |
| 1617 | |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1618 | void bpf_prog_array_delete_safe(struct bpf_prog_array __rcu *progs, |
| 1619 | struct bpf_prog *old_prog) |
| 1620 | { |
| 1621 | struct bpf_prog **prog = progs->progs; |
| 1622 | |
| 1623 | for (; *prog; prog++) |
| 1624 | if (*prog == old_prog) { |
| 1625 | WRITE_ONCE(*prog, &dummy_bpf_prog.prog); |
| 1626 | break; |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | int bpf_prog_array_copy(struct bpf_prog_array __rcu *old_array, |
| 1631 | struct bpf_prog *exclude_prog, |
| 1632 | struct bpf_prog *include_prog, |
| 1633 | struct bpf_prog_array **new_array) |
| 1634 | { |
| 1635 | int new_prog_cnt, carry_prog_cnt = 0; |
| 1636 | struct bpf_prog **existing_prog; |
| 1637 | struct bpf_prog_array *array; |
| 1638 | int new_prog_idx = 0; |
| 1639 | |
| 1640 | /* Figure out how many existing progs we need to carry over to |
| 1641 | * the new array. |
| 1642 | */ |
| 1643 | if (old_array) { |
| 1644 | existing_prog = old_array->progs; |
| 1645 | for (; *existing_prog; existing_prog++) { |
| 1646 | if (*existing_prog != exclude_prog && |
| 1647 | *existing_prog != &dummy_bpf_prog.prog) |
| 1648 | carry_prog_cnt++; |
| 1649 | if (*existing_prog == include_prog) |
| 1650 | return -EEXIST; |
| 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | /* How many progs (not NULL) will be in the new array? */ |
| 1655 | new_prog_cnt = carry_prog_cnt; |
| 1656 | if (include_prog) |
| 1657 | new_prog_cnt += 1; |
| 1658 | |
| 1659 | /* Do we have any prog (not NULL) in the new array? */ |
| 1660 | if (!new_prog_cnt) { |
| 1661 | *new_array = NULL; |
| 1662 | return 0; |
| 1663 | } |
| 1664 | |
| 1665 | /* +1 as the end of prog_array is marked with NULL */ |
| 1666 | array = bpf_prog_array_alloc(new_prog_cnt + 1, GFP_KERNEL); |
| 1667 | if (!array) |
| 1668 | return -ENOMEM; |
| 1669 | |
| 1670 | /* Fill in the new prog array */ |
| 1671 | if (carry_prog_cnt) { |
| 1672 | existing_prog = old_array->progs; |
| 1673 | for (; *existing_prog; existing_prog++) |
| 1674 | if (*existing_prog != exclude_prog && |
| 1675 | *existing_prog != &dummy_bpf_prog.prog) |
| 1676 | array->progs[new_prog_idx++] = *existing_prog; |
| 1677 | } |
| 1678 | if (include_prog) |
| 1679 | array->progs[new_prog_idx++] = include_prog; |
| 1680 | array->progs[new_prog_idx] = NULL; |
| 1681 | *new_array = array; |
| 1682 | return 0; |
| 1683 | } |
| 1684 | |
Yonghong Song | f371b30 | 2017-12-11 11:39:02 -0800 | [diff] [blame] | 1685 | int bpf_prog_array_copy_info(struct bpf_prog_array __rcu *array, |
| 1686 | __u32 __user *prog_ids, u32 request_cnt, |
| 1687 | __u32 __user *prog_cnt) |
| 1688 | { |
| 1689 | u32 cnt = 0; |
| 1690 | |
| 1691 | if (array) |
| 1692 | cnt = bpf_prog_array_length(array); |
| 1693 | |
| 1694 | if (copy_to_user(prog_cnt, &cnt, sizeof(cnt))) |
| 1695 | return -EFAULT; |
| 1696 | |
| 1697 | /* return early if user requested only program count or nothing to copy */ |
| 1698 | if (!request_cnt || !cnt) |
| 1699 | return 0; |
| 1700 | |
| 1701 | return bpf_prog_array_copy_to_user(array, prog_ids, request_cnt); |
| 1702 | } |
| 1703 | |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 1704 | static void bpf_prog_free_deferred(struct work_struct *work) |
| 1705 | { |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1706 | struct bpf_prog_aux *aux; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 1707 | int i; |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 1708 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1709 | aux = container_of(work, struct bpf_prog_aux, work); |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1710 | if (bpf_prog_is_dev_bound(aux)) |
| 1711 | bpf_prog_offload_destroy(aux->prog); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 1712 | for (i = 0; i < aux->func_cnt; i++) |
| 1713 | bpf_jit_free(aux->func[i]); |
| 1714 | if (aux->func_cnt) { |
| 1715 | kfree(aux->func); |
| 1716 | bpf_prog_unlock_free(aux->prog); |
| 1717 | } else { |
| 1718 | bpf_jit_free(aux->prog); |
| 1719 | } |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | /* Free internal BPF program */ |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1723 | void bpf_prog_free(struct bpf_prog *fp) |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1724 | { |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1725 | struct bpf_prog_aux *aux = fp->aux; |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 1726 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1727 | INIT_WORK(&aux->work, bpf_prog_free_deferred); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1728 | schedule_work(&aux->work); |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1729 | } |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1730 | EXPORT_SYMBOL_GPL(bpf_prog_free); |
Alexei Starovoitov | f89b775 | 2014-10-23 18:41:08 -0700 | [diff] [blame] | 1731 | |
Daniel Borkmann | 3ad0040 | 2015-10-08 01:20:39 +0200 | [diff] [blame] | 1732 | /* RNG for unpriviledged user space with separated state from prandom_u32(). */ |
| 1733 | static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state); |
| 1734 | |
| 1735 | void bpf_user_rnd_init_once(void) |
| 1736 | { |
| 1737 | prandom_init_once(&bpf_user_rnd_state); |
| 1738 | } |
| 1739 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 1740 | BPF_CALL_0(bpf_user_rnd_u32) |
Daniel Borkmann | 3ad0040 | 2015-10-08 01:20:39 +0200 | [diff] [blame] | 1741 | { |
| 1742 | /* Should someone ever have the rather unwise idea to use some |
| 1743 | * of the registers passed into this function, then note that |
| 1744 | * this function is called from native eBPF and classic-to-eBPF |
| 1745 | * transformations. Register assignments from both sides are |
| 1746 | * different, f.e. classic always sets fn(ctx, A, X) here. |
| 1747 | */ |
| 1748 | struct rnd_state *state; |
| 1749 | u32 res; |
| 1750 | |
| 1751 | state = &get_cpu_var(bpf_user_rnd_state); |
| 1752 | res = prandom_u32_state(state); |
Shaohua Li | b761fe2 | 2016-09-27 08:42:41 -0700 | [diff] [blame] | 1753 | put_cpu_var(bpf_user_rnd_state); |
Daniel Borkmann | 3ad0040 | 2015-10-08 01:20:39 +0200 | [diff] [blame] | 1754 | |
| 1755 | return res; |
| 1756 | } |
| 1757 | |
Daniel Borkmann | 3ba67da | 2015-03-05 23:27:51 +0100 | [diff] [blame] | 1758 | /* Weak definitions of helper functions in case we don't have bpf syscall. */ |
| 1759 | const struct bpf_func_proto bpf_map_lookup_elem_proto __weak; |
| 1760 | const struct bpf_func_proto bpf_map_update_elem_proto __weak; |
| 1761 | const struct bpf_func_proto bpf_map_delete_elem_proto __weak; |
| 1762 | |
Daniel Borkmann | 03e69b5 | 2015-03-14 02:27:16 +0100 | [diff] [blame] | 1763 | const struct bpf_func_proto bpf_get_prandom_u32_proto __weak; |
Daniel Borkmann | c04167c | 2015-03-14 02:27:17 +0100 | [diff] [blame] | 1764 | const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak; |
Daniel Borkmann | 2d0e30c | 2016-10-21 12:46:33 +0200 | [diff] [blame] | 1765 | const struct bpf_func_proto bpf_get_numa_node_id_proto __weak; |
Daniel Borkmann | 17ca8cb | 2015-05-29 23:23:06 +0200 | [diff] [blame] | 1766 | const struct bpf_func_proto bpf_ktime_get_ns_proto __weak; |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 1767 | |
Alexei Starovoitov | ffeedaf | 2015-06-12 19:39:12 -0700 | [diff] [blame] | 1768 | const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak; |
| 1769 | const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak; |
| 1770 | const struct bpf_func_proto bpf_get_current_comm_proto __weak; |
John Fastabend | 6bdc9c4 | 2017-08-16 15:02:32 -0700 | [diff] [blame] | 1771 | const struct bpf_func_proto bpf_sock_map_update_proto __weak; |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 1772 | |
Alexei Starovoitov | 0756ea3 | 2015-06-12 19:39:13 -0700 | [diff] [blame] | 1773 | const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void) |
| 1774 | { |
| 1775 | return NULL; |
| 1776 | } |
Daniel Borkmann | 03e69b5 | 2015-03-14 02:27:16 +0100 | [diff] [blame] | 1777 | |
Daniel Borkmann | 555c8a8 | 2016-07-14 18:08:05 +0200 | [diff] [blame] | 1778 | u64 __weak |
| 1779 | bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size, |
| 1780 | void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy) |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 1781 | { |
Daniel Borkmann | 555c8a8 | 2016-07-14 18:08:05 +0200 | [diff] [blame] | 1782 | return -ENOTSUPP; |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 1783 | } |
| 1784 | |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1785 | /* Always built-in helper functions. */ |
| 1786 | const struct bpf_func_proto bpf_tail_call_proto = { |
| 1787 | .func = NULL, |
| 1788 | .gpl_only = false, |
| 1789 | .ret_type = RET_VOID, |
| 1790 | .arg1_type = ARG_PTR_TO_CTX, |
| 1791 | .arg2_type = ARG_CONST_MAP_PTR, |
| 1792 | .arg3_type = ARG_ANYTHING, |
| 1793 | }; |
| 1794 | |
Daniel Borkmann | 9383191 | 2017-02-16 22:24:49 +0100 | [diff] [blame] | 1795 | /* Stub for JITs that only support cBPF. eBPF programs are interpreted. |
| 1796 | * It is encouraged to implement bpf_int_jit_compile() instead, so that |
| 1797 | * eBPF and implicitly also cBPF can get JITed! |
| 1798 | */ |
Daniel Borkmann | d1c55ab | 2016-05-13 19:08:31 +0200 | [diff] [blame] | 1799 | struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog) |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1800 | { |
Daniel Borkmann | d1c55ab | 2016-05-13 19:08:31 +0200 | [diff] [blame] | 1801 | return prog; |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1802 | } |
| 1803 | |
Daniel Borkmann | 9383191 | 2017-02-16 22:24:49 +0100 | [diff] [blame] | 1804 | /* Stub for JITs that support eBPF. All cBPF code gets transformed into |
| 1805 | * eBPF by the kernel and is later compiled by bpf_int_jit_compile(). |
| 1806 | */ |
| 1807 | void __weak bpf_jit_compile(struct bpf_prog *prog) |
| 1808 | { |
| 1809 | } |
| 1810 | |
Martin KaFai Lau | 17bedab | 2016-12-07 15:53:11 -0800 | [diff] [blame] | 1811 | bool __weak bpf_helper_changes_pkt_data(void *func) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1812 | { |
| 1813 | return false; |
| 1814 | } |
| 1815 | |
Alexei Starovoitov | f89b775 | 2014-10-23 18:41:08 -0700 | [diff] [blame] | 1816 | /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call |
| 1817 | * skb_copy_bits(), so provide a weak definition of it for NET-less config. |
| 1818 | */ |
| 1819 | int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to, |
| 1820 | int len) |
| 1821 | { |
| 1822 | return -EFAULT; |
| 1823 | } |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 1824 | |
| 1825 | /* All definitions of tracepoints related to BPF. */ |
| 1826 | #define CREATE_TRACE_POINTS |
| 1827 | #include <linux/bpf_trace.h> |
| 1828 | |
| 1829 | EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception); |
| 1830 | |
Steven Rostedt (VMware) | 9185a61 | 2017-10-12 18:40:02 -0400 | [diff] [blame] | 1831 | /* These are only used within the BPF_SYSCALL code */ |
| 1832 | #ifdef CONFIG_BPF_SYSCALL |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 1833 | EXPORT_TRACEPOINT_SYMBOL_GPL(bpf_prog_get_type); |
| 1834 | EXPORT_TRACEPOINT_SYMBOL_GPL(bpf_prog_put_rcu); |
Steven Rostedt (VMware) | 9185a61 | 2017-10-12 18:40:02 -0400 | [diff] [blame] | 1835 | #endif |