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 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 24 | #include <uapi/linux/btf.h> |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 25 | #include <linux/filter.h> |
| 26 | #include <linux/skbuff.h> |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 27 | #include <linux/vmalloc.h> |
Daniel Borkmann | 738cbe7 | 2014-09-08 08:04:47 +0200 | [diff] [blame] | 28 | #include <linux/random.h> |
| 29 | #include <linux/moduleloader.h> |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 30 | #include <linux/bpf.h> |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 31 | #include <linux/btf.h> |
Josh Poimboeuf | 39853cc | 2016-02-28 22:22:37 -0600 | [diff] [blame] | 32 | #include <linux/frame.h> |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 33 | #include <linux/rbtree_latch.h> |
| 34 | #include <linux/kallsyms.h> |
| 35 | #include <linux/rcupdate.h> |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 36 | #include <linux/perf_event.h> |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 37 | |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 38 | #include <asm/unaligned.h> |
| 39 | |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 40 | /* 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 | */ |
| 65 | void *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 Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 73 | |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 74 | if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb)) |
| 75 | return ptr; |
| 76 | |
| 77 | return NULL; |
| 78 | } |
| 79 | |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 80 | struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags) |
| 81 | { |
Michal Hocko | 19809c2 | 2017-05-08 15:57:44 -0700 | [diff] [blame] | 82 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 83 | struct bpf_prog_aux *aux; |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 84 | 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 Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 91 | aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags); |
| 92 | if (aux == NULL) { |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 93 | vfree(fp); |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | fp->pages = size / PAGE_SIZE; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 98 | fp->aux = aux; |
Daniel Borkmann | e9d8afa | 2015-10-29 14:58:08 +0100 | [diff] [blame] | 99 | fp->aux->prog = fp; |
Alexei Starovoitov | 60b58afc | 2017-12-14 17:55:14 -0800 | [diff] [blame] | 100 | fp->jit_requested = ebpf_jit_enabled(); |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 101 | |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 102 | INIT_LIST_HEAD_RCU(&fp->aux->ksym_lnode); |
| 103 | |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 104 | return fp; |
| 105 | } |
| 106 | EXPORT_SYMBOL_GPL(bpf_prog_alloc); |
| 107 | |
| 108 | struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size, |
| 109 | gfp_t gfp_extra_flags) |
| 110 | { |
Michal Hocko | 19809c2 | 2017-05-08 15:57:44 -0700 | [diff] [blame] | 111 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags; |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 112 | struct bpf_prog *fp; |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 113 | u32 pages, delta; |
| 114 | int ret; |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 115 | |
| 116 | BUG_ON(fp_old == NULL); |
| 117 | |
| 118 | size = round_up(size, PAGE_SIZE); |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 119 | pages = size / PAGE_SIZE; |
| 120 | if (pages <= fp_old->pages) |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 121 | return fp_old; |
| 122 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 123 | delta = pages - fp_old->pages; |
| 124 | ret = __bpf_prog_charge(fp_old->aux->user, delta); |
| 125 | if (ret) |
| 126 | return NULL; |
| 127 | |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 128 | fp = __vmalloc(size, gfp_flags, PAGE_KERNEL); |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 129 | if (fp == NULL) { |
| 130 | __bpf_prog_uncharge(fp_old->aux->user, delta); |
| 131 | } else { |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 132 | memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE); |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 133 | fp->pages = pages; |
Daniel Borkmann | e9d8afa | 2015-10-29 14:58:08 +0100 | [diff] [blame] | 134 | fp->aux->prog = fp; |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 135 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 136 | /* We keep fp->aux from fp_old around in the new |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 137 | * reallocated structure. |
| 138 | */ |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 139 | fp_old->aux = NULL; |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 140 | __bpf_prog_free(fp_old); |
| 141 | } |
| 142 | |
| 143 | return fp; |
| 144 | } |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 145 | |
| 146 | void __bpf_prog_free(struct bpf_prog *fp) |
| 147 | { |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 148 | kfree(fp->aux); |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 149 | vfree(fp); |
| 150 | } |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 151 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 152 | int bpf_prog_calc_tag(struct bpf_prog *fp) |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 153 | { |
| 154 | const u32 bits_offset = SHA_MESSAGE_BYTES - sizeof(__be64); |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 155 | u32 raw_size = bpf_prog_tag_scratch_size(fp); |
| 156 | u32 digest[SHA_DIGEST_WORDS]; |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 157 | u32 ws[SHA_WORKSPACE_WORDS]; |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 158 | u32 i, bsize, psize, blocks; |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 159 | struct bpf_insn *dst; |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 160 | bool was_ld_map; |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 161 | u8 *raw, *todo; |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 162 | __be32 *result; |
| 163 | __be64 *bits; |
| 164 | |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 165 | raw = vmalloc(raw_size); |
| 166 | if (!raw) |
| 167 | return -ENOMEM; |
| 168 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 169 | sha_init(digest); |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 170 | 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 Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 175 | dst = (void *)raw; |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 176 | 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 Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 195 | psize = bpf_prog_insn_size(fp); |
| 196 | memset(&raw[psize], 0, raw_size - psize); |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 197 | raw[psize++] = 0x80; |
| 198 | |
| 199 | bsize = round_up(psize, SHA_MESSAGE_BYTES); |
| 200 | blocks = bsize / SHA_MESSAGE_BYTES; |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 201 | todo = raw; |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 202 | 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 Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 211 | sha_transform(digest, todo, ws); |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 212 | todo += SHA_MESSAGE_BYTES; |
| 213 | } |
| 214 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 215 | result = (__force __be32 *)digest; |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 216 | for (i = 0; i < SHA_DIGEST_WORDS; i++) |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 217 | result[i] = cpu_to_be32(digest[i]); |
| 218 | memcpy(fp->tag, result, sizeof(fp->tag)); |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 219 | |
| 220 | vfree(raw); |
| 221 | return 0; |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 222 | } |
| 223 | |
Daniel Borkmann | 050fad7 | 2018-05-17 01:44:11 +0200 | [diff] [blame] | 224 | static int bpf_adj_delta_to_imm(struct bpf_insn *insn, u32 pos, u32 delta, |
| 225 | u32 curr, const bool probe_pass) |
Daniel Borkmann | c237ee5 | 2016-05-13 19:08:30 +0200 | [diff] [blame] | 226 | { |
Daniel Borkmann | 050fad7 | 2018-05-17 01:44:11 +0200 | [diff] [blame] | 227 | 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 | |
| 241 | static 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 | |
| 258 | static 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 Borkmann | c237ee5 | 2016-05-13 19:08:30 +0200 | [diff] [blame] | 262 | struct bpf_insn *insn = prog->insnsi; |
Daniel Borkmann | 050fad7 | 2018-05-17 01:44:11 +0200 | [diff] [blame] | 263 | int ret = 0; |
Daniel Borkmann | c237ee5 | 2016-05-13 19:08:30 +0200 | [diff] [blame] | 264 | |
| 265 | for (i = 0; i < insn_cnt; i++, insn++) { |
Daniel Borkmann | 050fad7 | 2018-05-17 01:44:11 +0200 | [diff] [blame] | 266 | 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 Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 275 | } |
Daniel Borkmann | 050fad7 | 2018-05-17 01:44:11 +0200 | [diff] [blame] | 276 | 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 Borkmann | c237ee5 | 2016-05-13 19:08:30 +0200 | [diff] [blame] | 292 | } |
Daniel Borkmann | 050fad7 | 2018-05-17 01:44:11 +0200 | [diff] [blame] | 293 | |
| 294 | return ret; |
Daniel Borkmann | c237ee5 | 2016-05-13 19:08:30 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | struct 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 Borkmann | 050fad7 | 2018-05-17 01:44:11 +0200 | [diff] [blame] | 301 | const u32 cnt_max = S16_MAX; |
Daniel Borkmann | c237ee5 | 2016-05-13 19:08:30 +0200 | [diff] [blame] | 302 | 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 Borkmann | 050fad7 | 2018-05-17 01:44:11 +0200 | [diff] [blame] | 312 | /* 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 Borkmann | c237ee5 | 2016-05-13 19:08:30 +0200 | [diff] [blame] | 321 | /* 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 Borkmann | 050fad7 | 2018-05-17 01:44:11 +0200 | [diff] [blame] | 346 | /* 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 Borkmann | c237ee5 | 2016-05-13 19:08:30 +0200 | [diff] [blame] | 351 | |
| 352 | return prog_adj; |
| 353 | } |
| 354 | |
Daniel Borkmann | 7d1982b | 2018-06-15 02:30:47 +0200 | [diff] [blame] | 355 | void 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 | |
| 363 | void 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 Borkmann | b954d83 | 2014-09-10 15:01:02 +0200 | [diff] [blame] | 369 | #ifdef CONFIG_BPF_JIT |
Daniel Borkmann | ede95a63 | 2018-10-23 01:11:04 +0200 | [diff] [blame] | 370 | # define BPF_JIT_LIMIT_DEFAULT (PAGE_SIZE * 40000) |
| 371 | |
Daniel Borkmann | fa9dd59 | 2018-01-20 01:24:33 +0100 | [diff] [blame] | 372 | /* All BPF JIT sysctl knobs here. */ |
| 373 | int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_ALWAYS_ON); |
| 374 | int bpf_jit_harden __read_mostly; |
| 375 | int bpf_jit_kallsyms __read_mostly; |
Daniel Borkmann | ede95a63 | 2018-10-23 01:11:04 +0200 | [diff] [blame] | 376 | int bpf_jit_limit __read_mostly = BPF_JIT_LIMIT_DEFAULT; |
Daniel Borkmann | fa9dd59 | 2018-01-20 01:24:33 +0100 | [diff] [blame] | 377 | |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 378 | static __always_inline void |
| 379 | bpf_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 | |
| 392 | static void bpf_get_prog_name(const struct bpf_prog *prog, char *sym) |
| 393 | { |
Martin KaFai Lau | 368211f | 2017-10-05 21:52:13 -0700 | [diff] [blame] | 394 | const char *end = sym + KSYM_NAME_LEN; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 395 | const struct btf_type *type; |
| 396 | const char *func_name; |
Martin KaFai Lau | 368211f | 2017-10-05 21:52:13 -0700 | [diff] [blame] | 397 | |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 398 | BUILD_BUG_ON(sizeof("bpf_prog_") + |
Martin KaFai Lau | 368211f | 2017-10-05 21:52:13 -0700 | [diff] [blame] | 399 | 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 Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 408 | |
| 409 | sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_"); |
| 410 | sym = bin2hex(sym, prog->tag, sizeof(prog->tag)); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 411 | |
| 412 | /* prog->aux->name will be ignored if full btf name is available */ |
Martin KaFai Lau | 7337224 | 2018-12-05 17:35:43 -0800 | [diff] [blame] | 413 | if (prog->aux->func_info_cnt) { |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 414 | type = btf_type_by_id(prog->aux->btf, |
| 415 | prog->aux->func_info[prog->aux->func_idx].type_id); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 416 | 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 Lau | 368211f | 2017-10-05 21:52:13 -0700 | [diff] [blame] | 421 | if (prog->aux->name[0]) |
| 422 | snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name); |
| 423 | else |
| 424 | *sym = 0; |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | static __always_inline unsigned long |
| 428 | bpf_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 | |
| 439 | static __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 | |
| 445 | static __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 | |
| 462 | static const struct latch_tree_ops bpf_tree_ops = { |
| 463 | .less = bpf_tree_less, |
| 464 | .comp = bpf_tree_comp, |
| 465 | }; |
| 466 | |
| 467 | static DEFINE_SPINLOCK(bpf_lock); |
| 468 | static LIST_HEAD(bpf_kallsyms); |
| 469 | static struct latch_tree_root bpf_tree __cacheline_aligned; |
| 470 | |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 471 | static 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 | |
| 478 | static 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 | |
| 487 | static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp) |
| 488 | { |
| 489 | return fp->jited && !bpf_prog_was_classic(fp); |
| 490 | } |
| 491 | |
| 492 | static 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 | |
| 498 | void bpf_prog_kallsyms_add(struct bpf_prog *fp) |
| 499 | { |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 500 | if (!bpf_prog_kallsyms_candidate(fp) || |
| 501 | !capable(CAP_SYS_ADMIN)) |
| 502 | return; |
| 503 | |
Hannes Frederic Sowa | d24f7c7 | 2017-04-27 01:39:33 +0200 | [diff] [blame] | 504 | spin_lock_bh(&bpf_lock); |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 505 | bpf_prog_ksym_node_add(fp->aux); |
Hannes Frederic Sowa | d24f7c7 | 2017-04-27 01:39:33 +0200 | [diff] [blame] | 506 | spin_unlock_bh(&bpf_lock); |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | void bpf_prog_kallsyms_del(struct bpf_prog *fp) |
| 510 | { |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 511 | if (!bpf_prog_kallsyms_candidate(fp)) |
| 512 | return; |
| 513 | |
Hannes Frederic Sowa | d24f7c7 | 2017-04-27 01:39:33 +0200 | [diff] [blame] | 514 | spin_lock_bh(&bpf_lock); |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 515 | bpf_prog_ksym_node_del(fp->aux); |
Hannes Frederic Sowa | d24f7c7 | 2017-04-27 01:39:33 +0200 | [diff] [blame] | 516 | spin_unlock_bh(&bpf_lock); |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | static 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 | |
| 532 | const 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 | |
| 556 | bool 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 | |
| 567 | int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type, |
| 568 | char *sym) |
| 569 | { |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 570 | 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 Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 582 | bpf_get_prog_name(aux->prog, sym); |
| 583 | |
Song Liu | df07347 | 2018-11-02 10:16:15 -0700 | [diff] [blame] | 584 | *value = (unsigned long)aux->prog->bpf_func; |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 585 | *type = BPF_SYM_ELF_TYPE; |
| 586 | |
| 587 | ret = 0; |
| 588 | break; |
| 589 | } |
| 590 | rcu_read_unlock(); |
| 591 | |
| 592 | return ret; |
| 593 | } |
| 594 | |
Daniel Borkmann | ede95a63 | 2018-10-23 01:11:04 +0200 | [diff] [blame] | 595 | static atomic_long_t bpf_jit_current; |
| 596 | |
| 597 | #if defined(MODULES_VADDR) |
| 598 | static 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 | } |
| 605 | pure_initcall(bpf_jit_charge_init); |
| 606 | #endif |
| 607 | |
| 608 | static 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 | |
| 621 | static void bpf_jit_uncharge_modmem(u32 pages) |
| 622 | { |
| 623 | atomic_long_sub(pages, &bpf_jit_current); |
| 624 | } |
| 625 | |
Ard Biesheuvel | dc002bb | 2018-11-23 23:18:03 +0100 | [diff] [blame] | 626 | void *__weak bpf_jit_alloc_exec(unsigned long size) |
| 627 | { |
| 628 | return module_alloc(size); |
| 629 | } |
| 630 | |
| 631 | void __weak bpf_jit_free_exec(void *addr) |
| 632 | { |
| 633 | module_memfree(addr); |
| 634 | } |
| 635 | |
Daniel Borkmann | 738cbe7 | 2014-09-08 08:04:47 +0200 | [diff] [blame] | 636 | struct bpf_binary_header * |
| 637 | bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr, |
| 638 | unsigned int alignment, |
| 639 | bpf_jit_fill_hole_t bpf_fill_ill_insns) |
| 640 | { |
| 641 | struct bpf_binary_header *hdr; |
Daniel Borkmann | ede95a63 | 2018-10-23 01:11:04 +0200 | [diff] [blame] | 642 | u32 size, hole, start, pages; |
Daniel Borkmann | 738cbe7 | 2014-09-08 08:04:47 +0200 | [diff] [blame] | 643 | |
| 644 | /* Most of BPF filters are really small, but if some of them |
| 645 | * fill a page, allow at least 128 extra bytes to insert a |
| 646 | * random section of illegal instructions. |
| 647 | */ |
| 648 | size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE); |
Daniel Borkmann | ede95a63 | 2018-10-23 01:11:04 +0200 | [diff] [blame] | 649 | pages = size / PAGE_SIZE; |
| 650 | |
| 651 | if (bpf_jit_charge_modmem(pages)) |
Daniel Borkmann | 738cbe7 | 2014-09-08 08:04:47 +0200 | [diff] [blame] | 652 | return NULL; |
Ard Biesheuvel | dc002bb | 2018-11-23 23:18:03 +0100 | [diff] [blame] | 653 | hdr = bpf_jit_alloc_exec(size); |
Daniel Borkmann | ede95a63 | 2018-10-23 01:11:04 +0200 | [diff] [blame] | 654 | if (!hdr) { |
| 655 | bpf_jit_uncharge_modmem(pages); |
| 656 | return NULL; |
| 657 | } |
Daniel Borkmann | 738cbe7 | 2014-09-08 08:04:47 +0200 | [diff] [blame] | 658 | |
| 659 | /* Fill space with illegal/arch-dep instructions. */ |
| 660 | bpf_fill_ill_insns(hdr, size); |
| 661 | |
Daniel Borkmann | ede95a63 | 2018-10-23 01:11:04 +0200 | [diff] [blame] | 662 | hdr->pages = pages; |
Daniel Borkmann | 738cbe7 | 2014-09-08 08:04:47 +0200 | [diff] [blame] | 663 | hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)), |
| 664 | PAGE_SIZE - sizeof(*hdr)); |
Daniel Borkmann | b7552e1b | 2016-05-18 14:14:28 +0200 | [diff] [blame] | 665 | start = (get_random_int() % hole) & ~(alignment - 1); |
Daniel Borkmann | 738cbe7 | 2014-09-08 08:04:47 +0200 | [diff] [blame] | 666 | |
| 667 | /* Leave a random number of instructions before BPF code. */ |
| 668 | *image_ptr = &hdr->image[start]; |
| 669 | |
| 670 | return hdr; |
| 671 | } |
| 672 | |
| 673 | void bpf_jit_binary_free(struct bpf_binary_header *hdr) |
| 674 | { |
Daniel Borkmann | ede95a63 | 2018-10-23 01:11:04 +0200 | [diff] [blame] | 675 | u32 pages = hdr->pages; |
| 676 | |
Ard Biesheuvel | dc002bb | 2018-11-23 23:18:03 +0100 | [diff] [blame] | 677 | bpf_jit_free_exec(hdr); |
Daniel Borkmann | ede95a63 | 2018-10-23 01:11:04 +0200 | [diff] [blame] | 678 | bpf_jit_uncharge_modmem(pages); |
Daniel Borkmann | 738cbe7 | 2014-09-08 08:04:47 +0200 | [diff] [blame] | 679 | } |
Daniel Borkmann | 4f3446b | 2016-05-13 19:08:32 +0200 | [diff] [blame] | 680 | |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 681 | /* This symbol is only overridden by archs that have different |
| 682 | * requirements than the usual eBPF JITs, f.e. when they only |
| 683 | * implement cBPF JIT, do not set images read-only, etc. |
| 684 | */ |
| 685 | void __weak bpf_jit_free(struct bpf_prog *fp) |
| 686 | { |
| 687 | if (fp->jited) { |
| 688 | struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp); |
| 689 | |
| 690 | bpf_jit_binary_unlock_ro(hdr); |
| 691 | bpf_jit_binary_free(hdr); |
| 692 | |
| 693 | WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp)); |
| 694 | } |
| 695 | |
| 696 | bpf_prog_unlock_free(fp); |
| 697 | } |
| 698 | |
Daniel Borkmann | e2c95a6 | 2018-11-26 14:05:38 +0100 | [diff] [blame] | 699 | int bpf_jit_get_func_addr(const struct bpf_prog *prog, |
| 700 | const struct bpf_insn *insn, bool extra_pass, |
| 701 | u64 *func_addr, bool *func_addr_fixed) |
| 702 | { |
| 703 | s16 off = insn->off; |
| 704 | s32 imm = insn->imm; |
| 705 | u8 *addr; |
| 706 | |
| 707 | *func_addr_fixed = insn->src_reg != BPF_PSEUDO_CALL; |
| 708 | if (!*func_addr_fixed) { |
| 709 | /* Place-holder address till the last pass has collected |
| 710 | * all addresses for JITed subprograms in which case we |
| 711 | * can pick them up from prog->aux. |
| 712 | */ |
| 713 | if (!extra_pass) |
| 714 | addr = NULL; |
| 715 | else if (prog->aux->func && |
| 716 | off >= 0 && off < prog->aux->func_cnt) |
| 717 | addr = (u8 *)prog->aux->func[off]->bpf_func; |
| 718 | else |
| 719 | return -EINVAL; |
| 720 | } else { |
| 721 | /* Address of a BPF helper call. Since part of the core |
| 722 | * kernel, it's always at a fixed location. __bpf_call_base |
| 723 | * and the helper with imm relative to it are both in core |
| 724 | * kernel. |
| 725 | */ |
| 726 | addr = (u8 *)__bpf_call_base + imm; |
| 727 | } |
| 728 | |
| 729 | *func_addr = (unsigned long)addr; |
| 730 | return 0; |
| 731 | } |
| 732 | |
Daniel Borkmann | 4f3446b | 2016-05-13 19:08:32 +0200 | [diff] [blame] | 733 | static int bpf_jit_blind_insn(const struct bpf_insn *from, |
| 734 | const struct bpf_insn *aux, |
| 735 | struct bpf_insn *to_buff) |
| 736 | { |
| 737 | struct bpf_insn *to = to_buff; |
Daniel Borkmann | b7552e1b | 2016-05-18 14:14:28 +0200 | [diff] [blame] | 738 | u32 imm_rnd = get_random_int(); |
Daniel Borkmann | 4f3446b | 2016-05-13 19:08:32 +0200 | [diff] [blame] | 739 | s16 off; |
| 740 | |
| 741 | BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG); |
| 742 | BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG); |
| 743 | |
| 744 | if (from->imm == 0 && |
| 745 | (from->code == (BPF_ALU | BPF_MOV | BPF_K) || |
| 746 | from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) { |
| 747 | *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg); |
| 748 | goto out; |
| 749 | } |
| 750 | |
| 751 | switch (from->code) { |
| 752 | case BPF_ALU | BPF_ADD | BPF_K: |
| 753 | case BPF_ALU | BPF_SUB | BPF_K: |
| 754 | case BPF_ALU | BPF_AND | BPF_K: |
| 755 | case BPF_ALU | BPF_OR | BPF_K: |
| 756 | case BPF_ALU | BPF_XOR | BPF_K: |
| 757 | case BPF_ALU | BPF_MUL | BPF_K: |
| 758 | case BPF_ALU | BPF_MOV | BPF_K: |
| 759 | case BPF_ALU | BPF_DIV | BPF_K: |
| 760 | case BPF_ALU | BPF_MOD | BPF_K: |
| 761 | *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); |
| 762 | *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); |
| 763 | *to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX); |
| 764 | break; |
| 765 | |
| 766 | case BPF_ALU64 | BPF_ADD | BPF_K: |
| 767 | case BPF_ALU64 | BPF_SUB | BPF_K: |
| 768 | case BPF_ALU64 | BPF_AND | BPF_K: |
| 769 | case BPF_ALU64 | BPF_OR | BPF_K: |
| 770 | case BPF_ALU64 | BPF_XOR | BPF_K: |
| 771 | case BPF_ALU64 | BPF_MUL | BPF_K: |
| 772 | case BPF_ALU64 | BPF_MOV | BPF_K: |
| 773 | case BPF_ALU64 | BPF_DIV | BPF_K: |
| 774 | case BPF_ALU64 | BPF_MOD | BPF_K: |
| 775 | *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); |
| 776 | *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); |
| 777 | *to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX); |
| 778 | break; |
| 779 | |
| 780 | case BPF_JMP | BPF_JEQ | BPF_K: |
| 781 | case BPF_JMP | BPF_JNE | BPF_K: |
| 782 | case BPF_JMP | BPF_JGT | BPF_K: |
Daniel Borkmann | 92b31a9 | 2017-08-10 01:39:55 +0200 | [diff] [blame] | 783 | case BPF_JMP | BPF_JLT | BPF_K: |
Daniel Borkmann | 4f3446b | 2016-05-13 19:08:32 +0200 | [diff] [blame] | 784 | case BPF_JMP | BPF_JGE | BPF_K: |
Daniel Borkmann | 92b31a9 | 2017-08-10 01:39:55 +0200 | [diff] [blame] | 785 | case BPF_JMP | BPF_JLE | BPF_K: |
Daniel Borkmann | 4f3446b | 2016-05-13 19:08:32 +0200 | [diff] [blame] | 786 | case BPF_JMP | BPF_JSGT | BPF_K: |
Daniel Borkmann | 92b31a9 | 2017-08-10 01:39:55 +0200 | [diff] [blame] | 787 | case BPF_JMP | BPF_JSLT | BPF_K: |
Daniel Borkmann | 4f3446b | 2016-05-13 19:08:32 +0200 | [diff] [blame] | 788 | case BPF_JMP | BPF_JSGE | BPF_K: |
Daniel Borkmann | 92b31a9 | 2017-08-10 01:39:55 +0200 | [diff] [blame] | 789 | case BPF_JMP | BPF_JSLE | BPF_K: |
Daniel Borkmann | 4f3446b | 2016-05-13 19:08:32 +0200 | [diff] [blame] | 790 | case BPF_JMP | BPF_JSET | BPF_K: |
| 791 | /* Accommodate for extra offset in case of a backjump. */ |
| 792 | off = from->off; |
| 793 | if (off < 0) |
| 794 | off -= 2; |
| 795 | *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); |
| 796 | *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); |
| 797 | *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off); |
| 798 | break; |
| 799 | |
Daniel Borkmann | 4f3446b | 2016-05-13 19:08:32 +0200 | [diff] [blame] | 800 | case BPF_LD | BPF_IMM | BPF_DW: |
| 801 | *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm); |
| 802 | *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); |
| 803 | *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); |
| 804 | *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX); |
| 805 | break; |
| 806 | case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */ |
| 807 | *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm); |
| 808 | *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); |
| 809 | *to++ = BPF_ALU64_REG(BPF_OR, aux[0].dst_reg, BPF_REG_AX); |
| 810 | break; |
| 811 | |
| 812 | case BPF_ST | BPF_MEM | BPF_DW: |
| 813 | case BPF_ST | BPF_MEM | BPF_W: |
| 814 | case BPF_ST | BPF_MEM | BPF_H: |
| 815 | case BPF_ST | BPF_MEM | BPF_B: |
| 816 | *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); |
| 817 | *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); |
| 818 | *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off); |
| 819 | break; |
| 820 | } |
| 821 | out: |
| 822 | return to - to_buff; |
| 823 | } |
| 824 | |
| 825 | static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other, |
| 826 | gfp_t gfp_extra_flags) |
| 827 | { |
Michal Hocko | 19809c2 | 2017-05-08 15:57:44 -0700 | [diff] [blame] | 828 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags; |
Daniel Borkmann | 4f3446b | 2016-05-13 19:08:32 +0200 | [diff] [blame] | 829 | struct bpf_prog *fp; |
| 830 | |
| 831 | fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags, PAGE_KERNEL); |
| 832 | if (fp != NULL) { |
Daniel Borkmann | 4f3446b | 2016-05-13 19:08:32 +0200 | [diff] [blame] | 833 | /* aux->prog still points to the fp_other one, so |
| 834 | * when promoting the clone to the real program, |
| 835 | * this still needs to be adapted. |
| 836 | */ |
| 837 | memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE); |
| 838 | } |
| 839 | |
| 840 | return fp; |
| 841 | } |
| 842 | |
| 843 | static void bpf_prog_clone_free(struct bpf_prog *fp) |
| 844 | { |
| 845 | /* aux was stolen by the other clone, so we cannot free |
| 846 | * it from this path! It will be freed eventually by the |
| 847 | * other program on release. |
| 848 | * |
| 849 | * At this point, we don't need a deferred release since |
| 850 | * clone is guaranteed to not be locked. |
| 851 | */ |
| 852 | fp->aux = NULL; |
| 853 | __bpf_prog_free(fp); |
| 854 | } |
| 855 | |
| 856 | void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other) |
| 857 | { |
| 858 | /* We have to repoint aux->prog to self, as we don't |
| 859 | * know whether fp here is the clone or the original. |
| 860 | */ |
| 861 | fp->aux->prog = fp; |
| 862 | bpf_prog_clone_free(fp_other); |
| 863 | } |
| 864 | |
| 865 | struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog) |
| 866 | { |
| 867 | struct bpf_insn insn_buff[16], aux[2]; |
| 868 | struct bpf_prog *clone, *tmp; |
| 869 | int insn_delta, insn_cnt; |
| 870 | struct bpf_insn *insn; |
| 871 | int i, rewritten; |
| 872 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 873 | if (!bpf_jit_blinding_enabled(prog) || prog->blinded) |
Daniel Borkmann | 4f3446b | 2016-05-13 19:08:32 +0200 | [diff] [blame] | 874 | return prog; |
| 875 | |
| 876 | clone = bpf_prog_clone_create(prog, GFP_USER); |
| 877 | if (!clone) |
| 878 | return ERR_PTR(-ENOMEM); |
| 879 | |
| 880 | insn_cnt = clone->len; |
| 881 | insn = clone->insnsi; |
| 882 | |
| 883 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 884 | /* We temporarily need to hold the original ld64 insn |
| 885 | * so that we can still access the first part in the |
| 886 | * second blinding run. |
| 887 | */ |
| 888 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) && |
| 889 | insn[1].code == 0) |
| 890 | memcpy(aux, insn, sizeof(aux)); |
| 891 | |
| 892 | rewritten = bpf_jit_blind_insn(insn, aux, insn_buff); |
| 893 | if (!rewritten) |
| 894 | continue; |
| 895 | |
| 896 | tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten); |
| 897 | if (!tmp) { |
| 898 | /* Patching may have repointed aux->prog during |
| 899 | * realloc from the original one, so we need to |
| 900 | * fix it up here on error. |
| 901 | */ |
| 902 | bpf_jit_prog_release_other(prog, clone); |
| 903 | return ERR_PTR(-ENOMEM); |
| 904 | } |
| 905 | |
| 906 | clone = tmp; |
| 907 | insn_delta = rewritten - 1; |
| 908 | |
| 909 | /* Walk new program and skip insns we just inserted. */ |
| 910 | insn = clone->insnsi + i + insn_delta; |
| 911 | insn_cnt += insn_delta; |
| 912 | i += insn_delta; |
| 913 | } |
| 914 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 915 | clone->blinded = 1; |
Daniel Borkmann | 4f3446b | 2016-05-13 19:08:32 +0200 | [diff] [blame] | 916 | return clone; |
| 917 | } |
Daniel Borkmann | b954d83 | 2014-09-10 15:01:02 +0200 | [diff] [blame] | 918 | #endif /* CONFIG_BPF_JIT */ |
Daniel Borkmann | 738cbe7 | 2014-09-08 08:04:47 +0200 | [diff] [blame] | 919 | |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 920 | /* Base function for offset calculation. Needs to go into .text section, |
| 921 | * 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] | 922 | * anyway later on, so do not let the compiler omit it. This also needs |
| 923 | * to go into kallsyms for correlation from e.g. bpftool, so naming |
| 924 | * must not change. |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 925 | */ |
| 926 | noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 927 | { |
| 928 | return 0; |
| 929 | } |
Alexei Starovoitov | 4d9c5c5 | 2015-07-20 20:34:19 -0700 | [diff] [blame] | 930 | EXPORT_SYMBOL_GPL(__bpf_call_base); |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 931 | |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 932 | /* All UAPI available opcodes. */ |
| 933 | #define BPF_INSN_MAP(INSN_2, INSN_3) \ |
| 934 | /* 32 bit ALU operations. */ \ |
| 935 | /* Register based. */ \ |
Jiong Wang | 2dc6b10 | 2018-12-05 13:52:34 -0500 | [diff] [blame^] | 936 | INSN_3(ALU, ADD, X), \ |
| 937 | INSN_3(ALU, SUB, X), \ |
| 938 | INSN_3(ALU, AND, X), \ |
| 939 | INSN_3(ALU, OR, X), \ |
| 940 | INSN_3(ALU, LSH, X), \ |
| 941 | INSN_3(ALU, RSH, X), \ |
| 942 | INSN_3(ALU, XOR, X), \ |
| 943 | INSN_3(ALU, MUL, X), \ |
| 944 | INSN_3(ALU, MOV, X), \ |
| 945 | INSN_3(ALU, ARSH, X), \ |
| 946 | INSN_3(ALU, DIV, X), \ |
| 947 | INSN_3(ALU, MOD, X), \ |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 948 | INSN_2(ALU, NEG), \ |
| 949 | INSN_3(ALU, END, TO_BE), \ |
| 950 | INSN_3(ALU, END, TO_LE), \ |
| 951 | /* Immediate based. */ \ |
Jiong Wang | 2dc6b10 | 2018-12-05 13:52:34 -0500 | [diff] [blame^] | 952 | INSN_3(ALU, ADD, K), \ |
| 953 | INSN_3(ALU, SUB, K), \ |
| 954 | INSN_3(ALU, AND, K), \ |
| 955 | INSN_3(ALU, OR, K), \ |
| 956 | INSN_3(ALU, LSH, K), \ |
| 957 | INSN_3(ALU, RSH, K), \ |
| 958 | INSN_3(ALU, XOR, K), \ |
| 959 | INSN_3(ALU, MUL, K), \ |
| 960 | INSN_3(ALU, MOV, K), \ |
| 961 | INSN_3(ALU, ARSH, K), \ |
| 962 | INSN_3(ALU, DIV, K), \ |
| 963 | INSN_3(ALU, MOD, K), \ |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 964 | /* 64 bit ALU operations. */ \ |
| 965 | /* Register based. */ \ |
| 966 | INSN_3(ALU64, ADD, X), \ |
| 967 | INSN_3(ALU64, SUB, X), \ |
| 968 | INSN_3(ALU64, AND, X), \ |
| 969 | INSN_3(ALU64, OR, X), \ |
| 970 | INSN_3(ALU64, LSH, X), \ |
| 971 | INSN_3(ALU64, RSH, X), \ |
| 972 | INSN_3(ALU64, XOR, X), \ |
| 973 | INSN_3(ALU64, MUL, X), \ |
| 974 | INSN_3(ALU64, MOV, X), \ |
| 975 | INSN_3(ALU64, ARSH, X), \ |
| 976 | INSN_3(ALU64, DIV, X), \ |
| 977 | INSN_3(ALU64, MOD, X), \ |
| 978 | INSN_2(ALU64, NEG), \ |
| 979 | /* Immediate based. */ \ |
| 980 | INSN_3(ALU64, ADD, K), \ |
| 981 | INSN_3(ALU64, SUB, K), \ |
| 982 | INSN_3(ALU64, AND, K), \ |
| 983 | INSN_3(ALU64, OR, K), \ |
| 984 | INSN_3(ALU64, LSH, K), \ |
| 985 | INSN_3(ALU64, RSH, K), \ |
| 986 | INSN_3(ALU64, XOR, K), \ |
| 987 | INSN_3(ALU64, MUL, K), \ |
| 988 | INSN_3(ALU64, MOV, K), \ |
| 989 | INSN_3(ALU64, ARSH, K), \ |
| 990 | INSN_3(ALU64, DIV, K), \ |
| 991 | INSN_3(ALU64, MOD, K), \ |
| 992 | /* Call instruction. */ \ |
| 993 | INSN_2(JMP, CALL), \ |
| 994 | /* Exit instruction. */ \ |
| 995 | INSN_2(JMP, EXIT), \ |
| 996 | /* Jump instructions. */ \ |
| 997 | /* Register based. */ \ |
| 998 | INSN_3(JMP, JEQ, X), \ |
| 999 | INSN_3(JMP, JNE, X), \ |
| 1000 | INSN_3(JMP, JGT, X), \ |
| 1001 | INSN_3(JMP, JLT, X), \ |
| 1002 | INSN_3(JMP, JGE, X), \ |
| 1003 | INSN_3(JMP, JLE, X), \ |
| 1004 | INSN_3(JMP, JSGT, X), \ |
| 1005 | INSN_3(JMP, JSLT, X), \ |
| 1006 | INSN_3(JMP, JSGE, X), \ |
| 1007 | INSN_3(JMP, JSLE, X), \ |
| 1008 | INSN_3(JMP, JSET, X), \ |
| 1009 | /* Immediate based. */ \ |
| 1010 | INSN_3(JMP, JEQ, K), \ |
| 1011 | INSN_3(JMP, JNE, K), \ |
| 1012 | INSN_3(JMP, JGT, K), \ |
| 1013 | INSN_3(JMP, JLT, K), \ |
| 1014 | INSN_3(JMP, JGE, K), \ |
| 1015 | INSN_3(JMP, JLE, K), \ |
| 1016 | INSN_3(JMP, JSGT, K), \ |
| 1017 | INSN_3(JMP, JSLT, K), \ |
| 1018 | INSN_3(JMP, JSGE, K), \ |
| 1019 | INSN_3(JMP, JSLE, K), \ |
| 1020 | INSN_3(JMP, JSET, K), \ |
| 1021 | INSN_2(JMP, JA), \ |
| 1022 | /* Store instructions. */ \ |
| 1023 | /* Register based. */ \ |
| 1024 | INSN_3(STX, MEM, B), \ |
| 1025 | INSN_3(STX, MEM, H), \ |
| 1026 | INSN_3(STX, MEM, W), \ |
| 1027 | INSN_3(STX, MEM, DW), \ |
| 1028 | INSN_3(STX, XADD, W), \ |
| 1029 | INSN_3(STX, XADD, DW), \ |
| 1030 | /* Immediate based. */ \ |
| 1031 | INSN_3(ST, MEM, B), \ |
| 1032 | INSN_3(ST, MEM, H), \ |
| 1033 | INSN_3(ST, MEM, W), \ |
| 1034 | INSN_3(ST, MEM, DW), \ |
| 1035 | /* Load instructions. */ \ |
| 1036 | /* Register based. */ \ |
| 1037 | INSN_3(LDX, MEM, B), \ |
| 1038 | INSN_3(LDX, MEM, H), \ |
| 1039 | INSN_3(LDX, MEM, W), \ |
| 1040 | INSN_3(LDX, MEM, DW), \ |
| 1041 | /* Immediate based. */ \ |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 1042 | INSN_3(LD, IMM, DW) |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 1043 | |
| 1044 | bool bpf_opcode_in_insntable(u8 code) |
| 1045 | { |
| 1046 | #define BPF_INSN_2_TBL(x, y) [BPF_##x | BPF_##y] = true |
| 1047 | #define BPF_INSN_3_TBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = true |
| 1048 | static const bool public_insntable[256] = { |
| 1049 | [0 ... 255] = false, |
| 1050 | /* Now overwrite non-defaults ... */ |
| 1051 | BPF_INSN_MAP(BPF_INSN_2_TBL, BPF_INSN_3_TBL), |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 1052 | /* UAPI exposed, but rewritten opcodes. cBPF carry-over. */ |
| 1053 | [BPF_LD | BPF_ABS | BPF_B] = true, |
| 1054 | [BPF_LD | BPF_ABS | BPF_H] = true, |
| 1055 | [BPF_LD | BPF_ABS | BPF_W] = true, |
| 1056 | [BPF_LD | BPF_IND | BPF_B] = true, |
| 1057 | [BPF_LD | BPF_IND | BPF_H] = true, |
| 1058 | [BPF_LD | BPF_IND | BPF_W] = true, |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 1059 | }; |
| 1060 | #undef BPF_INSN_3_TBL |
| 1061 | #undef BPF_INSN_2_TBL |
| 1062 | return public_insntable[code]; |
| 1063 | } |
| 1064 | |
Alexei Starovoitov | 290af86 | 2018-01-09 10:04:29 -0800 | [diff] [blame] | 1065 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1066 | /** |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1067 | * __bpf_prog_run - run eBPF program on a given context |
| 1068 | * @ctx: is the data we are operating on |
| 1069 | * @insn: is the array of eBPF instructions |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1070 | * |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1071 | * Decode and execute eBPF instructions. |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1072 | */ |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1073 | 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] | 1074 | { |
Alexei Starovoitov | f696b8f | 2017-05-30 13:31:28 -0700 | [diff] [blame] | 1075 | u64 tmp; |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 1076 | #define BPF_INSN_2_LBL(x, y) [BPF_##x | BPF_##y] = &&x##_##y |
| 1077 | #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] | 1078 | static const void *jumptable[256] = { |
| 1079 | [0 ... 255] = &&default_label, |
| 1080 | /* Now overwrite non-defaults ... */ |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 1081 | BPF_INSN_MAP(BPF_INSN_2_LBL, BPF_INSN_3_LBL), |
| 1082 | /* Non-UAPI available opcodes. */ |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1083 | [BPF_JMP | BPF_CALL_ARGS] = &&JMP_CALL_ARGS, |
Alexei Starovoitov | 71189fa | 2017-05-30 13:31:27 -0700 | [diff] [blame] | 1084 | [BPF_JMP | BPF_TAIL_CALL] = &&JMP_TAIL_CALL, |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1085 | }; |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 1086 | #undef BPF_INSN_3_LBL |
| 1087 | #undef BPF_INSN_2_LBL |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1088 | u32 tail_call_cnt = 0; |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1089 | |
| 1090 | #define CONT ({ insn++; goto select_insn; }) |
| 1091 | #define CONT_JMP ({ insn++; goto select_insn; }) |
| 1092 | |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1093 | select_insn: |
| 1094 | goto *jumptable[insn->code]; |
| 1095 | |
| 1096 | /* ALU */ |
| 1097 | #define ALU(OPCODE, OP) \ |
| 1098 | ALU64_##OPCODE##_X: \ |
| 1099 | DST = DST OP SRC; \ |
| 1100 | CONT; \ |
| 1101 | ALU_##OPCODE##_X: \ |
| 1102 | DST = (u32) DST OP (u32) SRC; \ |
| 1103 | CONT; \ |
| 1104 | ALU64_##OPCODE##_K: \ |
| 1105 | DST = DST OP IMM; \ |
| 1106 | CONT; \ |
| 1107 | ALU_##OPCODE##_K: \ |
| 1108 | DST = (u32) DST OP (u32) IMM; \ |
| 1109 | CONT; |
| 1110 | |
| 1111 | ALU(ADD, +) |
| 1112 | ALU(SUB, -) |
| 1113 | ALU(AND, &) |
| 1114 | ALU(OR, |) |
| 1115 | ALU(LSH, <<) |
| 1116 | ALU(RSH, >>) |
| 1117 | ALU(XOR, ^) |
| 1118 | ALU(MUL, *) |
| 1119 | #undef ALU |
| 1120 | ALU_NEG: |
| 1121 | DST = (u32) -DST; |
| 1122 | CONT; |
| 1123 | ALU64_NEG: |
| 1124 | DST = -DST; |
| 1125 | CONT; |
| 1126 | ALU_MOV_X: |
| 1127 | DST = (u32) SRC; |
| 1128 | CONT; |
| 1129 | ALU_MOV_K: |
| 1130 | DST = (u32) IMM; |
| 1131 | CONT; |
| 1132 | ALU64_MOV_X: |
| 1133 | DST = SRC; |
| 1134 | CONT; |
| 1135 | ALU64_MOV_K: |
| 1136 | DST = IMM; |
| 1137 | CONT; |
Alexei Starovoitov | 02ab695 | 2014-09-04 22:17:17 -0700 | [diff] [blame] | 1138 | LD_IMM_DW: |
| 1139 | DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32; |
| 1140 | insn++; |
| 1141 | CONT; |
Jiong Wang | 2dc6b10 | 2018-12-05 13:52:34 -0500 | [diff] [blame^] | 1142 | ALU_ARSH_X: |
| 1143 | DST = (u64) (u32) ((*(s32 *) &DST) >> SRC); |
| 1144 | CONT; |
| 1145 | ALU_ARSH_K: |
| 1146 | DST = (u64) (u32) ((*(s32 *) &DST) >> IMM); |
| 1147 | CONT; |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1148 | ALU64_ARSH_X: |
| 1149 | (*(s64 *) &DST) >>= SRC; |
| 1150 | CONT; |
| 1151 | ALU64_ARSH_K: |
| 1152 | (*(s64 *) &DST) >>= IMM; |
| 1153 | CONT; |
| 1154 | ALU64_MOD_X: |
Alexei Starovoitov | 876a7ae | 2015-04-27 14:40:37 -0700 | [diff] [blame] | 1155 | div64_u64_rem(DST, SRC, &tmp); |
| 1156 | DST = tmp; |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1157 | CONT; |
| 1158 | ALU_MOD_X: |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1159 | tmp = (u32) DST; |
| 1160 | DST = do_div(tmp, (u32) SRC); |
| 1161 | CONT; |
| 1162 | ALU64_MOD_K: |
Alexei Starovoitov | 876a7ae | 2015-04-27 14:40:37 -0700 | [diff] [blame] | 1163 | div64_u64_rem(DST, IMM, &tmp); |
| 1164 | DST = tmp; |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1165 | CONT; |
| 1166 | ALU_MOD_K: |
| 1167 | tmp = (u32) DST; |
| 1168 | DST = do_div(tmp, (u32) IMM); |
| 1169 | CONT; |
| 1170 | ALU64_DIV_X: |
Alexei Starovoitov | 876a7ae | 2015-04-27 14:40:37 -0700 | [diff] [blame] | 1171 | DST = div64_u64(DST, SRC); |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1172 | CONT; |
| 1173 | ALU_DIV_X: |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1174 | tmp = (u32) DST; |
| 1175 | do_div(tmp, (u32) SRC); |
| 1176 | DST = (u32) tmp; |
| 1177 | CONT; |
| 1178 | ALU64_DIV_K: |
Alexei Starovoitov | 876a7ae | 2015-04-27 14:40:37 -0700 | [diff] [blame] | 1179 | DST = div64_u64(DST, IMM); |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1180 | CONT; |
| 1181 | ALU_DIV_K: |
| 1182 | tmp = (u32) DST; |
| 1183 | do_div(tmp, (u32) IMM); |
| 1184 | DST = (u32) tmp; |
| 1185 | CONT; |
| 1186 | ALU_END_TO_BE: |
| 1187 | switch (IMM) { |
| 1188 | case 16: |
| 1189 | DST = (__force u16) cpu_to_be16(DST); |
| 1190 | break; |
| 1191 | case 32: |
| 1192 | DST = (__force u32) cpu_to_be32(DST); |
| 1193 | break; |
| 1194 | case 64: |
| 1195 | DST = (__force u64) cpu_to_be64(DST); |
| 1196 | break; |
| 1197 | } |
| 1198 | CONT; |
| 1199 | ALU_END_TO_LE: |
| 1200 | switch (IMM) { |
| 1201 | case 16: |
| 1202 | DST = (__force u16) cpu_to_le16(DST); |
| 1203 | break; |
| 1204 | case 32: |
| 1205 | DST = (__force u32) cpu_to_le32(DST); |
| 1206 | break; |
| 1207 | case 64: |
| 1208 | DST = (__force u64) cpu_to_le64(DST); |
| 1209 | break; |
| 1210 | } |
| 1211 | CONT; |
| 1212 | |
| 1213 | /* CALL */ |
| 1214 | JMP_CALL: |
| 1215 | /* Function call scratches BPF_R1-BPF_R5 registers, |
| 1216 | * preserves BPF_R6-BPF_R9, and stores return value |
| 1217 | * into BPF_R0. |
| 1218 | */ |
| 1219 | BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3, |
| 1220 | BPF_R4, BPF_R5); |
| 1221 | CONT; |
| 1222 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1223 | JMP_CALL_ARGS: |
| 1224 | BPF_R0 = (__bpf_call_base_args + insn->imm)(BPF_R1, BPF_R2, |
| 1225 | BPF_R3, BPF_R4, |
| 1226 | BPF_R5, |
| 1227 | insn + insn->off + 1); |
| 1228 | CONT; |
| 1229 | |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1230 | JMP_TAIL_CALL: { |
| 1231 | struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2; |
| 1232 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 1233 | struct bpf_prog *prog; |
Alexei Starovoitov | 90caccd | 2017-10-03 15:37:20 -0700 | [diff] [blame] | 1234 | u32 index = BPF_R3; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1235 | |
| 1236 | if (unlikely(index >= array->map.max_entries)) |
| 1237 | goto out; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1238 | if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT)) |
| 1239 | goto out; |
| 1240 | |
| 1241 | tail_call_cnt++; |
| 1242 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 1243 | prog = READ_ONCE(array->ptrs[index]); |
Daniel Borkmann | 1ca1cc9 | 2016-06-28 12:18:23 +0200 | [diff] [blame] | 1244 | if (!prog) |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1245 | goto out; |
| 1246 | |
Daniel Borkmann | c4675f9 | 2015-07-13 20:49:32 +0200 | [diff] [blame] | 1247 | /* ARG1 at this point is guaranteed to point to CTX from |
| 1248 | * the verifier side due to the fact that the tail call is |
| 1249 | * handeled like a helper, that is, bpf_tail_call_proto, |
| 1250 | * where arg1_type is ARG_PTR_TO_CTX. |
| 1251 | */ |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1252 | insn = prog->insnsi; |
| 1253 | goto select_insn; |
| 1254 | out: |
| 1255 | CONT; |
| 1256 | } |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1257 | /* JMP */ |
| 1258 | JMP_JA: |
| 1259 | insn += insn->off; |
| 1260 | CONT; |
| 1261 | JMP_JEQ_X: |
| 1262 | if (DST == SRC) { |
| 1263 | insn += insn->off; |
| 1264 | CONT_JMP; |
| 1265 | } |
| 1266 | CONT; |
| 1267 | JMP_JEQ_K: |
| 1268 | if (DST == IMM) { |
| 1269 | insn += insn->off; |
| 1270 | CONT_JMP; |
| 1271 | } |
| 1272 | CONT; |
| 1273 | JMP_JNE_X: |
| 1274 | if (DST != SRC) { |
| 1275 | insn += insn->off; |
| 1276 | CONT_JMP; |
| 1277 | } |
| 1278 | CONT; |
| 1279 | JMP_JNE_K: |
| 1280 | if (DST != IMM) { |
| 1281 | insn += insn->off; |
| 1282 | CONT_JMP; |
| 1283 | } |
| 1284 | CONT; |
| 1285 | JMP_JGT_X: |
| 1286 | if (DST > SRC) { |
| 1287 | insn += insn->off; |
| 1288 | CONT_JMP; |
| 1289 | } |
| 1290 | CONT; |
| 1291 | JMP_JGT_K: |
| 1292 | if (DST > IMM) { |
| 1293 | insn += insn->off; |
| 1294 | CONT_JMP; |
| 1295 | } |
| 1296 | CONT; |
Daniel Borkmann | 92b31a9 | 2017-08-10 01:39:55 +0200 | [diff] [blame] | 1297 | JMP_JLT_X: |
| 1298 | if (DST < SRC) { |
| 1299 | insn += insn->off; |
| 1300 | CONT_JMP; |
| 1301 | } |
| 1302 | CONT; |
| 1303 | JMP_JLT_K: |
| 1304 | if (DST < IMM) { |
| 1305 | insn += insn->off; |
| 1306 | CONT_JMP; |
| 1307 | } |
| 1308 | CONT; |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1309 | JMP_JGE_X: |
| 1310 | if (DST >= SRC) { |
| 1311 | insn += insn->off; |
| 1312 | CONT_JMP; |
| 1313 | } |
| 1314 | CONT; |
| 1315 | JMP_JGE_K: |
| 1316 | if (DST >= IMM) { |
| 1317 | insn += insn->off; |
| 1318 | CONT_JMP; |
| 1319 | } |
| 1320 | CONT; |
Daniel Borkmann | 92b31a9 | 2017-08-10 01:39:55 +0200 | [diff] [blame] | 1321 | JMP_JLE_X: |
| 1322 | if (DST <= SRC) { |
| 1323 | insn += insn->off; |
| 1324 | CONT_JMP; |
| 1325 | } |
| 1326 | CONT; |
| 1327 | JMP_JLE_K: |
| 1328 | if (DST <= IMM) { |
| 1329 | insn += insn->off; |
| 1330 | CONT_JMP; |
| 1331 | } |
| 1332 | CONT; |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1333 | JMP_JSGT_X: |
| 1334 | if (((s64) DST) > ((s64) SRC)) { |
| 1335 | insn += insn->off; |
| 1336 | CONT_JMP; |
| 1337 | } |
| 1338 | CONT; |
| 1339 | JMP_JSGT_K: |
| 1340 | if (((s64) DST) > ((s64) IMM)) { |
| 1341 | insn += insn->off; |
| 1342 | CONT_JMP; |
| 1343 | } |
| 1344 | CONT; |
Daniel Borkmann | 92b31a9 | 2017-08-10 01:39:55 +0200 | [diff] [blame] | 1345 | JMP_JSLT_X: |
| 1346 | if (((s64) DST) < ((s64) SRC)) { |
| 1347 | insn += insn->off; |
| 1348 | CONT_JMP; |
| 1349 | } |
| 1350 | CONT; |
| 1351 | JMP_JSLT_K: |
| 1352 | if (((s64) DST) < ((s64) IMM)) { |
| 1353 | insn += insn->off; |
| 1354 | CONT_JMP; |
| 1355 | } |
| 1356 | CONT; |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1357 | JMP_JSGE_X: |
| 1358 | if (((s64) DST) >= ((s64) SRC)) { |
| 1359 | insn += insn->off; |
| 1360 | CONT_JMP; |
| 1361 | } |
| 1362 | CONT; |
| 1363 | JMP_JSGE_K: |
| 1364 | if (((s64) DST) >= ((s64) IMM)) { |
| 1365 | insn += insn->off; |
| 1366 | CONT_JMP; |
| 1367 | } |
| 1368 | CONT; |
Daniel Borkmann | 92b31a9 | 2017-08-10 01:39:55 +0200 | [diff] [blame] | 1369 | JMP_JSLE_X: |
| 1370 | if (((s64) DST) <= ((s64) SRC)) { |
| 1371 | insn += insn->off; |
| 1372 | CONT_JMP; |
| 1373 | } |
| 1374 | CONT; |
| 1375 | JMP_JSLE_K: |
| 1376 | if (((s64) DST) <= ((s64) IMM)) { |
| 1377 | insn += insn->off; |
| 1378 | CONT_JMP; |
| 1379 | } |
| 1380 | CONT; |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1381 | JMP_JSET_X: |
| 1382 | if (DST & SRC) { |
| 1383 | insn += insn->off; |
| 1384 | CONT_JMP; |
| 1385 | } |
| 1386 | CONT; |
| 1387 | JMP_JSET_K: |
| 1388 | if (DST & IMM) { |
| 1389 | insn += insn->off; |
| 1390 | CONT_JMP; |
| 1391 | } |
| 1392 | CONT; |
| 1393 | JMP_EXIT: |
| 1394 | return BPF_R0; |
| 1395 | |
| 1396 | /* STX and ST and LDX*/ |
| 1397 | #define LDST(SIZEOP, SIZE) \ |
| 1398 | STX_MEM_##SIZEOP: \ |
| 1399 | *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \ |
| 1400 | CONT; \ |
| 1401 | ST_MEM_##SIZEOP: \ |
| 1402 | *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \ |
| 1403 | CONT; \ |
| 1404 | LDX_MEM_##SIZEOP: \ |
| 1405 | DST = *(SIZE *)(unsigned long) (SRC + insn->off); \ |
| 1406 | CONT; |
| 1407 | |
| 1408 | LDST(B, u8) |
| 1409 | LDST(H, u16) |
| 1410 | LDST(W, u32) |
| 1411 | LDST(DW, u64) |
| 1412 | #undef LDST |
| 1413 | STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */ |
| 1414 | atomic_add((u32) SRC, (atomic_t *)(unsigned long) |
| 1415 | (DST + insn->off)); |
| 1416 | CONT; |
| 1417 | STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */ |
| 1418 | atomic64_add((u64) SRC, (atomic64_t *)(unsigned long) |
| 1419 | (DST + insn->off)); |
| 1420 | CONT; |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1421 | |
| 1422 | default_label: |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 1423 | /* If we ever reach this, we have a bug somewhere. Die hard here |
| 1424 | * instead of just returning 0; we could be somewhere in a subprog, |
| 1425 | * so execution could continue otherwise which we do /not/ want. |
| 1426 | * |
| 1427 | * Note, verifier whitelists all opcodes in bpf_opcode_in_insntable(). |
| 1428 | */ |
| 1429 | pr_warn("BPF interpreter: unknown opcode %02x\n", insn->code); |
| 1430 | BUG_ON(1); |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1431 | return 0; |
| 1432 | } |
Alexei Starovoitov | f696b8f | 2017-05-30 13:31:28 -0700 | [diff] [blame] | 1433 | STACK_FRAME_NON_STANDARD(___bpf_prog_run); /* jump table */ |
| 1434 | |
Alexei Starovoitov | b870aa9 | 2017-05-30 13:31:33 -0700 | [diff] [blame] | 1435 | #define PROG_NAME(stack_size) __bpf_prog_run##stack_size |
| 1436 | #define DEFINE_BPF_PROG_RUN(stack_size) \ |
| 1437 | static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \ |
| 1438 | { \ |
| 1439 | u64 stack[stack_size / sizeof(u64)]; \ |
| 1440 | u64 regs[MAX_BPF_REG]; \ |
| 1441 | \ |
| 1442 | FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \ |
| 1443 | ARG1 = (u64) (unsigned long) ctx; \ |
| 1444 | return ___bpf_prog_run(regs, insn, stack); \ |
Alexei Starovoitov | f696b8f | 2017-05-30 13:31:28 -0700 | [diff] [blame] | 1445 | } |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1446 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1447 | #define PROG_NAME_ARGS(stack_size) __bpf_prog_run_args##stack_size |
| 1448 | #define DEFINE_BPF_PROG_RUN_ARGS(stack_size) \ |
| 1449 | static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \ |
| 1450 | const struct bpf_insn *insn) \ |
| 1451 | { \ |
| 1452 | u64 stack[stack_size / sizeof(u64)]; \ |
| 1453 | u64 regs[MAX_BPF_REG]; \ |
| 1454 | \ |
| 1455 | FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \ |
| 1456 | BPF_R1 = r1; \ |
| 1457 | BPF_R2 = r2; \ |
| 1458 | BPF_R3 = r3; \ |
| 1459 | BPF_R4 = r4; \ |
| 1460 | BPF_R5 = r5; \ |
| 1461 | return ___bpf_prog_run(regs, insn, stack); \ |
| 1462 | } |
| 1463 | |
Alexei Starovoitov | b870aa9 | 2017-05-30 13:31:33 -0700 | [diff] [blame] | 1464 | #define EVAL1(FN, X) FN(X) |
| 1465 | #define EVAL2(FN, X, Y...) FN(X) EVAL1(FN, Y) |
| 1466 | #define EVAL3(FN, X, Y...) FN(X) EVAL2(FN, Y) |
| 1467 | #define EVAL4(FN, X, Y...) FN(X) EVAL3(FN, Y) |
| 1468 | #define EVAL5(FN, X, Y...) FN(X) EVAL4(FN, Y) |
| 1469 | #define EVAL6(FN, X, Y...) FN(X) EVAL5(FN, Y) |
| 1470 | |
| 1471 | EVAL6(DEFINE_BPF_PROG_RUN, 32, 64, 96, 128, 160, 192); |
| 1472 | EVAL6(DEFINE_BPF_PROG_RUN, 224, 256, 288, 320, 352, 384); |
| 1473 | EVAL4(DEFINE_BPF_PROG_RUN, 416, 448, 480, 512); |
| 1474 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1475 | EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 32, 64, 96, 128, 160, 192); |
| 1476 | EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 224, 256, 288, 320, 352, 384); |
| 1477 | EVAL4(DEFINE_BPF_PROG_RUN_ARGS, 416, 448, 480, 512); |
| 1478 | |
Alexei Starovoitov | b870aa9 | 2017-05-30 13:31:33 -0700 | [diff] [blame] | 1479 | #define PROG_NAME_LIST(stack_size) PROG_NAME(stack_size), |
| 1480 | |
| 1481 | static unsigned int (*interpreters[])(const void *ctx, |
| 1482 | const struct bpf_insn *insn) = { |
| 1483 | EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192) |
| 1484 | EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384) |
| 1485 | EVAL4(PROG_NAME_LIST, 416, 448, 480, 512) |
| 1486 | }; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1487 | #undef PROG_NAME_LIST |
| 1488 | #define PROG_NAME_LIST(stack_size) PROG_NAME_ARGS(stack_size), |
| 1489 | static u64 (*interpreters_args[])(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, |
| 1490 | const struct bpf_insn *insn) = { |
| 1491 | EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192) |
| 1492 | EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384) |
| 1493 | EVAL4(PROG_NAME_LIST, 416, 448, 480, 512) |
| 1494 | }; |
| 1495 | #undef PROG_NAME_LIST |
| 1496 | |
| 1497 | void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth) |
| 1498 | { |
| 1499 | stack_depth = max_t(u32, stack_depth, 1); |
| 1500 | insn->off = (s16) insn->imm; |
| 1501 | insn->imm = interpreters_args[(round_up(stack_depth, 32) / 32) - 1] - |
| 1502 | __bpf_call_base_args; |
| 1503 | insn->code = BPF_JMP | BPF_CALL_ARGS; |
| 1504 | } |
Alexei Starovoitov | b870aa9 | 2017-05-30 13:31:33 -0700 | [diff] [blame] | 1505 | |
Alexei Starovoitov | 290af86 | 2018-01-09 10:04:29 -0800 | [diff] [blame] | 1506 | #else |
Daniel Borkmann | fa9dd59 | 2018-01-20 01:24:33 +0100 | [diff] [blame] | 1507 | static unsigned int __bpf_prog_ret0_warn(const void *ctx, |
| 1508 | const struct bpf_insn *insn) |
Alexei Starovoitov | 290af86 | 2018-01-09 10:04:29 -0800 | [diff] [blame] | 1509 | { |
Daniel Borkmann | fa9dd59 | 2018-01-20 01:24:33 +0100 | [diff] [blame] | 1510 | /* If this handler ever gets executed, then BPF_JIT_ALWAYS_ON |
| 1511 | * is not working properly, so warn about it! |
| 1512 | */ |
| 1513 | WARN_ON_ONCE(1); |
Alexei Starovoitov | 290af86 | 2018-01-09 10:04:29 -0800 | [diff] [blame] | 1514 | return 0; |
| 1515 | } |
| 1516 | #endif |
| 1517 | |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1518 | bool bpf_prog_array_compatible(struct bpf_array *array, |
| 1519 | const struct bpf_prog *fp) |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1520 | { |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1521 | if (fp->kprobe_override) |
| 1522 | return false; |
| 1523 | |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1524 | if (!array->owner_prog_type) { |
| 1525 | /* There's no owner yet where we could check for |
| 1526 | * compatibility. |
| 1527 | */ |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1528 | array->owner_prog_type = fp->type; |
| 1529 | array->owner_jited = fp->jited; |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1530 | |
| 1531 | return true; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1532 | } |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1533 | |
| 1534 | return array->owner_prog_type == fp->type && |
| 1535 | array->owner_jited == fp->jited; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1536 | } |
| 1537 | |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1538 | static int bpf_check_tail_call(const struct bpf_prog *fp) |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1539 | { |
| 1540 | struct bpf_prog_aux *aux = fp->aux; |
| 1541 | int i; |
| 1542 | |
| 1543 | for (i = 0; i < aux->used_map_cnt; i++) { |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1544 | struct bpf_map *map = aux->used_maps[i]; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1545 | struct bpf_array *array; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1546 | |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1547 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 1548 | continue; |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1549 | |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1550 | array = container_of(map, struct bpf_array, map); |
| 1551 | if (!bpf_prog_array_compatible(array, fp)) |
| 1552 | return -EINVAL; |
| 1553 | } |
| 1554 | |
| 1555 | return 0; |
| 1556 | } |
| 1557 | |
Daniel Borkmann | 9facc33 | 2018-06-15 02:30:48 +0200 | [diff] [blame] | 1558 | static void bpf_prog_select_func(struct bpf_prog *fp) |
| 1559 | { |
| 1560 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
| 1561 | u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1); |
| 1562 | |
| 1563 | fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1]; |
| 1564 | #else |
| 1565 | fp->bpf_func = __bpf_prog_ret0_warn; |
| 1566 | #endif |
| 1567 | } |
| 1568 | |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1569 | /** |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1570 | * bpf_prog_select_runtime - select exec runtime for BPF program |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1571 | * @fp: bpf_prog populated with internal BPF program |
Daniel Borkmann | d1c55ab | 2016-05-13 19:08:31 +0200 | [diff] [blame] | 1572 | * @err: pointer to error variable |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1573 | * |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1574 | * Try to JIT eBPF program, if JIT is not available, use interpreter. |
| 1575 | * The BPF program will be executed via BPF_PROG_RUN() macro. |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1576 | */ |
Daniel Borkmann | d1c55ab | 2016-05-13 19:08:31 +0200 | [diff] [blame] | 1577 | 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] | 1578 | { |
Daniel Borkmann | 9facc33 | 2018-06-15 02:30:48 +0200 | [diff] [blame] | 1579 | /* In case of BPF to BPF calls, verifier did all the prep |
| 1580 | * work with regards to JITing, etc. |
| 1581 | */ |
| 1582 | if (fp->bpf_func) |
| 1583 | goto finalize; |
Martin KaFai Lau | 8007e40 | 2017-06-28 10:41:24 -0700 | [diff] [blame] | 1584 | |
Daniel Borkmann | 9facc33 | 2018-06-15 02:30:48 +0200 | [diff] [blame] | 1585 | bpf_prog_select_func(fp); |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1586 | |
Daniel Borkmann | d1c55ab | 2016-05-13 19:08:31 +0200 | [diff] [blame] | 1587 | /* eBPF JITs can rewrite the program in case constant |
| 1588 | * blinding is active. However, in case of error during |
| 1589 | * blinding, bpf_int_jit_compile() must always return a |
| 1590 | * valid program, which in this case would simply not |
| 1591 | * be JITed, but falls back to the interpreter. |
| 1592 | */ |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1593 | if (!bpf_prog_is_dev_bound(fp->aux)) { |
| 1594 | fp = bpf_int_jit_compile(fp); |
Alexei Starovoitov | 290af86 | 2018-01-09 10:04:29 -0800 | [diff] [blame] | 1595 | #ifdef CONFIG_BPF_JIT_ALWAYS_ON |
| 1596 | if (!fp->jited) { |
| 1597 | *err = -ENOTSUPP; |
| 1598 | return fp; |
| 1599 | } |
| 1600 | #endif |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1601 | } else { |
| 1602 | *err = bpf_prog_offload_compile(fp); |
| 1603 | if (*err) |
| 1604 | return fp; |
| 1605 | } |
Daniel Borkmann | 9facc33 | 2018-06-15 02:30:48 +0200 | [diff] [blame] | 1606 | |
| 1607 | finalize: |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 1608 | bpf_prog_lock_ro(fp); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1609 | |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1610 | /* The tail call compatibility check can only be done at |
| 1611 | * this late stage as we need to determine, if we deal |
| 1612 | * with JITed or non JITed program concatenations and not |
| 1613 | * all eBPF JITs might immediately support all features. |
| 1614 | */ |
Daniel Borkmann | d1c55ab | 2016-05-13 19:08:31 +0200 | [diff] [blame] | 1615 | *err = bpf_check_tail_call(fp); |
| 1616 | |
| 1617 | return fp; |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1618 | } |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1619 | EXPORT_SYMBOL_GPL(bpf_prog_select_runtime); |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1620 | |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1621 | static unsigned int __bpf_prog_ret1(const void *ctx, |
| 1622 | const struct bpf_insn *insn) |
| 1623 | { |
| 1624 | return 1; |
| 1625 | } |
| 1626 | |
| 1627 | static struct bpf_prog_dummy { |
| 1628 | struct bpf_prog prog; |
| 1629 | } dummy_bpf_prog = { |
| 1630 | .prog = { |
| 1631 | .bpf_func = __bpf_prog_ret1, |
| 1632 | }, |
| 1633 | }; |
| 1634 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1635 | /* to avoid allocating empty bpf_prog_array for cgroups that |
| 1636 | * don't have bpf program attached use one global 'empty_prog_array' |
| 1637 | * It will not be modified the caller of bpf_prog_array_alloc() |
| 1638 | * (since caller requested prog_cnt == 0) |
| 1639 | * that pointer should be 'freed' by bpf_prog_array_free() |
| 1640 | */ |
| 1641 | static struct { |
| 1642 | struct bpf_prog_array hdr; |
| 1643 | struct bpf_prog *null_prog; |
| 1644 | } empty_prog_array = { |
| 1645 | .null_prog = NULL, |
| 1646 | }; |
| 1647 | |
Roman Gushchin | d29ab6e | 2018-07-13 12:41:10 -0700 | [diff] [blame] | 1648 | struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags) |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1649 | { |
| 1650 | if (prog_cnt) |
| 1651 | return kzalloc(sizeof(struct bpf_prog_array) + |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 1652 | sizeof(struct bpf_prog_array_item) * |
| 1653 | (prog_cnt + 1), |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1654 | flags); |
| 1655 | |
| 1656 | return &empty_prog_array.hdr; |
| 1657 | } |
| 1658 | |
| 1659 | void bpf_prog_array_free(struct bpf_prog_array __rcu *progs) |
| 1660 | { |
| 1661 | if (!progs || |
| 1662 | progs == (struct bpf_prog_array __rcu *)&empty_prog_array.hdr) |
| 1663 | return; |
| 1664 | kfree_rcu(progs, rcu); |
| 1665 | } |
| 1666 | |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 1667 | int bpf_prog_array_length(struct bpf_prog_array __rcu *array) |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1668 | { |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 1669 | struct bpf_prog_array_item *item; |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1670 | u32 cnt = 0; |
| 1671 | |
| 1672 | rcu_read_lock(); |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 1673 | item = rcu_dereference(array)->items; |
| 1674 | for (; item->prog; item++) |
| 1675 | if (item->prog != &dummy_bpf_prog.prog) |
Yonghong Song | c8c088b | 2017-11-30 13:47:54 -0800 | [diff] [blame] | 1676 | cnt++; |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1677 | rcu_read_unlock(); |
| 1678 | return cnt; |
| 1679 | } |
| 1680 | |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 1681 | |
| 1682 | static bool bpf_prog_array_copy_core(struct bpf_prog_array __rcu *array, |
Yonghong Song | 3a38bb9 | 2018-04-10 09:37:32 -0700 | [diff] [blame] | 1683 | u32 *prog_ids, |
| 1684 | u32 request_cnt) |
| 1685 | { |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 1686 | struct bpf_prog_array_item *item; |
Yonghong Song | 3a38bb9 | 2018-04-10 09:37:32 -0700 | [diff] [blame] | 1687 | int i = 0; |
| 1688 | |
Yonghong Song | 965931e | 2018-08-14 11:01:12 -0700 | [diff] [blame] | 1689 | item = rcu_dereference_check(array, 1)->items; |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 1690 | for (; item->prog; item++) { |
| 1691 | if (item->prog == &dummy_bpf_prog.prog) |
Yonghong Song | 3a38bb9 | 2018-04-10 09:37:32 -0700 | [diff] [blame] | 1692 | continue; |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 1693 | prog_ids[i] = item->prog->aux->id; |
Yonghong Song | 3a38bb9 | 2018-04-10 09:37:32 -0700 | [diff] [blame] | 1694 | if (++i == request_cnt) { |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 1695 | item++; |
Yonghong Song | 3a38bb9 | 2018-04-10 09:37:32 -0700 | [diff] [blame] | 1696 | break; |
| 1697 | } |
| 1698 | } |
| 1699 | |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 1700 | return !!(item->prog); |
Yonghong Song | 3a38bb9 | 2018-04-10 09:37:32 -0700 | [diff] [blame] | 1701 | } |
| 1702 | |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 1703 | int bpf_prog_array_copy_to_user(struct bpf_prog_array __rcu *array, |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1704 | __u32 __user *prog_ids, u32 cnt) |
| 1705 | { |
Alexei Starovoitov | 0911287 | 2018-02-02 15:14:05 -0800 | [diff] [blame] | 1706 | unsigned long err = 0; |
Alexei Starovoitov | 0911287 | 2018-02-02 15:14:05 -0800 | [diff] [blame] | 1707 | bool nospc; |
Yonghong Song | 3a38bb9 | 2018-04-10 09:37:32 -0700 | [diff] [blame] | 1708 | u32 *ids; |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1709 | |
Alexei Starovoitov | 0911287 | 2018-02-02 15:14:05 -0800 | [diff] [blame] | 1710 | /* users of this function are doing: |
| 1711 | * cnt = bpf_prog_array_length(); |
| 1712 | * if (cnt > 0) |
| 1713 | * bpf_prog_array_copy_to_user(..., cnt); |
| 1714 | * so below kcalloc doesn't need extra cnt > 0 check, but |
| 1715 | * bpf_prog_array_length() releases rcu lock and |
| 1716 | * prog array could have been swapped with empty or larger array, |
| 1717 | * so always copy 'cnt' prog_ids to the user. |
| 1718 | * In a rare race the user will see zero prog_ids |
| 1719 | */ |
Daniel Borkmann | 9c481b9 | 2018-02-14 15:31:00 +0100 | [diff] [blame] | 1720 | ids = kcalloc(cnt, sizeof(u32), GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | 0911287 | 2018-02-02 15:14:05 -0800 | [diff] [blame] | 1721 | if (!ids) |
| 1722 | return -ENOMEM; |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1723 | rcu_read_lock(); |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 1724 | nospc = bpf_prog_array_copy_core(array, ids, cnt); |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1725 | rcu_read_unlock(); |
Alexei Starovoitov | 0911287 | 2018-02-02 15:14:05 -0800 | [diff] [blame] | 1726 | err = copy_to_user(prog_ids, ids, cnt * sizeof(u32)); |
| 1727 | kfree(ids); |
| 1728 | if (err) |
| 1729 | return -EFAULT; |
| 1730 | if (nospc) |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1731 | return -ENOSPC; |
| 1732 | return 0; |
| 1733 | } |
| 1734 | |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 1735 | void bpf_prog_array_delete_safe(struct bpf_prog_array __rcu *array, |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1736 | struct bpf_prog *old_prog) |
| 1737 | { |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 1738 | struct bpf_prog_array_item *item = array->items; |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1739 | |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 1740 | for (; item->prog; item++) |
| 1741 | if (item->prog == old_prog) { |
| 1742 | WRITE_ONCE(item->prog, &dummy_bpf_prog.prog); |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1743 | break; |
| 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | int bpf_prog_array_copy(struct bpf_prog_array __rcu *old_array, |
| 1748 | struct bpf_prog *exclude_prog, |
| 1749 | struct bpf_prog *include_prog, |
| 1750 | struct bpf_prog_array **new_array) |
| 1751 | { |
| 1752 | int new_prog_cnt, carry_prog_cnt = 0; |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 1753 | struct bpf_prog_array_item *existing; |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1754 | struct bpf_prog_array *array; |
Sean Young | 170a7e3 | 2018-05-27 12:24:08 +0100 | [diff] [blame] | 1755 | bool found_exclude = false; |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1756 | int new_prog_idx = 0; |
| 1757 | |
| 1758 | /* Figure out how many existing progs we need to carry over to |
| 1759 | * the new array. |
| 1760 | */ |
| 1761 | if (old_array) { |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 1762 | existing = old_array->items; |
| 1763 | for (; existing->prog; existing++) { |
| 1764 | if (existing->prog == exclude_prog) { |
Sean Young | 170a7e3 | 2018-05-27 12:24:08 +0100 | [diff] [blame] | 1765 | found_exclude = true; |
| 1766 | continue; |
| 1767 | } |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 1768 | if (existing->prog != &dummy_bpf_prog.prog) |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1769 | carry_prog_cnt++; |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 1770 | if (existing->prog == include_prog) |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1771 | return -EEXIST; |
| 1772 | } |
| 1773 | } |
| 1774 | |
Sean Young | 170a7e3 | 2018-05-27 12:24:08 +0100 | [diff] [blame] | 1775 | if (exclude_prog && !found_exclude) |
| 1776 | return -ENOENT; |
| 1777 | |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1778 | /* How many progs (not NULL) will be in the new array? */ |
| 1779 | new_prog_cnt = carry_prog_cnt; |
| 1780 | if (include_prog) |
| 1781 | new_prog_cnt += 1; |
| 1782 | |
| 1783 | /* Do we have any prog (not NULL) in the new array? */ |
| 1784 | if (!new_prog_cnt) { |
| 1785 | *new_array = NULL; |
| 1786 | return 0; |
| 1787 | } |
| 1788 | |
| 1789 | /* +1 as the end of prog_array is marked with NULL */ |
| 1790 | array = bpf_prog_array_alloc(new_prog_cnt + 1, GFP_KERNEL); |
| 1791 | if (!array) |
| 1792 | return -ENOMEM; |
| 1793 | |
| 1794 | /* Fill in the new prog array */ |
| 1795 | if (carry_prog_cnt) { |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 1796 | existing = old_array->items; |
| 1797 | for (; existing->prog; existing++) |
| 1798 | if (existing->prog != exclude_prog && |
| 1799 | existing->prog != &dummy_bpf_prog.prog) { |
| 1800 | array->items[new_prog_idx++].prog = |
| 1801 | existing->prog; |
| 1802 | } |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1803 | } |
| 1804 | if (include_prog) |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 1805 | array->items[new_prog_idx++].prog = include_prog; |
| 1806 | array->items[new_prog_idx].prog = NULL; |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1807 | *new_array = array; |
| 1808 | return 0; |
| 1809 | } |
| 1810 | |
Yonghong Song | f371b30 | 2017-12-11 11:39:02 -0800 | [diff] [blame] | 1811 | int bpf_prog_array_copy_info(struct bpf_prog_array __rcu *array, |
Yonghong Song | 3a38bb9 | 2018-04-10 09:37:32 -0700 | [diff] [blame] | 1812 | u32 *prog_ids, u32 request_cnt, |
| 1813 | u32 *prog_cnt) |
Yonghong Song | f371b30 | 2017-12-11 11:39:02 -0800 | [diff] [blame] | 1814 | { |
| 1815 | u32 cnt = 0; |
| 1816 | |
| 1817 | if (array) |
| 1818 | cnt = bpf_prog_array_length(array); |
| 1819 | |
Yonghong Song | 3a38bb9 | 2018-04-10 09:37:32 -0700 | [diff] [blame] | 1820 | *prog_cnt = cnt; |
Yonghong Song | f371b30 | 2017-12-11 11:39:02 -0800 | [diff] [blame] | 1821 | |
| 1822 | /* return early if user requested only program count or nothing to copy */ |
| 1823 | if (!request_cnt || !cnt) |
| 1824 | return 0; |
| 1825 | |
Yonghong Song | 3a38bb9 | 2018-04-10 09:37:32 -0700 | [diff] [blame] | 1826 | /* this function is called under trace/bpf_trace.c: bpf_event_mutex */ |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 1827 | return bpf_prog_array_copy_core(array, prog_ids, request_cnt) ? -ENOSPC |
Yonghong Song | 3a38bb9 | 2018-04-10 09:37:32 -0700 | [diff] [blame] | 1828 | : 0; |
Yonghong Song | f371b30 | 2017-12-11 11:39:02 -0800 | [diff] [blame] | 1829 | } |
| 1830 | |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 1831 | static void bpf_prog_free_deferred(struct work_struct *work) |
| 1832 | { |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1833 | struct bpf_prog_aux *aux; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 1834 | int i; |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 1835 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1836 | aux = container_of(work, struct bpf_prog_aux, work); |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1837 | if (bpf_prog_is_dev_bound(aux)) |
| 1838 | bpf_prog_offload_destroy(aux->prog); |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 1839 | #ifdef CONFIG_PERF_EVENTS |
| 1840 | if (aux->prog->has_callchain_buf) |
| 1841 | put_callchain_buffers(); |
| 1842 | #endif |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 1843 | for (i = 0; i < aux->func_cnt; i++) |
| 1844 | bpf_jit_free(aux->func[i]); |
| 1845 | if (aux->func_cnt) { |
| 1846 | kfree(aux->func); |
| 1847 | bpf_prog_unlock_free(aux->prog); |
| 1848 | } else { |
| 1849 | bpf_jit_free(aux->prog); |
| 1850 | } |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | /* Free internal BPF program */ |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1854 | void bpf_prog_free(struct bpf_prog *fp) |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1855 | { |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1856 | struct bpf_prog_aux *aux = fp->aux; |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 1857 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1858 | INIT_WORK(&aux->work, bpf_prog_free_deferred); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1859 | schedule_work(&aux->work); |
Alexei Starovoitov | f5bffec | 2014-07-22 23:01:58 -0700 | [diff] [blame] | 1860 | } |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1861 | EXPORT_SYMBOL_GPL(bpf_prog_free); |
Alexei Starovoitov | f89b775 | 2014-10-23 18:41:08 -0700 | [diff] [blame] | 1862 | |
Daniel Borkmann | 3ad0040 | 2015-10-08 01:20:39 +0200 | [diff] [blame] | 1863 | /* RNG for unpriviledged user space with separated state from prandom_u32(). */ |
| 1864 | static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state); |
| 1865 | |
| 1866 | void bpf_user_rnd_init_once(void) |
| 1867 | { |
| 1868 | prandom_init_once(&bpf_user_rnd_state); |
| 1869 | } |
| 1870 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 1871 | BPF_CALL_0(bpf_user_rnd_u32) |
Daniel Borkmann | 3ad0040 | 2015-10-08 01:20:39 +0200 | [diff] [blame] | 1872 | { |
| 1873 | /* Should someone ever have the rather unwise idea to use some |
| 1874 | * of the registers passed into this function, then note that |
| 1875 | * this function is called from native eBPF and classic-to-eBPF |
| 1876 | * transformations. Register assignments from both sides are |
| 1877 | * different, f.e. classic always sets fn(ctx, A, X) here. |
| 1878 | */ |
| 1879 | struct rnd_state *state; |
| 1880 | u32 res; |
| 1881 | |
| 1882 | state = &get_cpu_var(bpf_user_rnd_state); |
| 1883 | res = prandom_u32_state(state); |
Shaohua Li | b761fe2 | 2016-09-27 08:42:41 -0700 | [diff] [blame] | 1884 | put_cpu_var(bpf_user_rnd_state); |
Daniel Borkmann | 3ad0040 | 2015-10-08 01:20:39 +0200 | [diff] [blame] | 1885 | |
| 1886 | return res; |
| 1887 | } |
| 1888 | |
Daniel Borkmann | 3ba67da | 2015-03-05 23:27:51 +0100 | [diff] [blame] | 1889 | /* Weak definitions of helper functions in case we don't have bpf syscall. */ |
| 1890 | const struct bpf_func_proto bpf_map_lookup_elem_proto __weak; |
| 1891 | const struct bpf_func_proto bpf_map_update_elem_proto __weak; |
| 1892 | const struct bpf_func_proto bpf_map_delete_elem_proto __weak; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 1893 | const struct bpf_func_proto bpf_map_push_elem_proto __weak; |
| 1894 | const struct bpf_func_proto bpf_map_pop_elem_proto __weak; |
| 1895 | const struct bpf_func_proto bpf_map_peek_elem_proto __weak; |
Daniel Borkmann | 3ba67da | 2015-03-05 23:27:51 +0100 | [diff] [blame] | 1896 | |
Daniel Borkmann | 03e69b5 | 2015-03-14 02:27:16 +0100 | [diff] [blame] | 1897 | const struct bpf_func_proto bpf_get_prandom_u32_proto __weak; |
Daniel Borkmann | c04167c | 2015-03-14 02:27:17 +0100 | [diff] [blame] | 1898 | const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak; |
Daniel Borkmann | 2d0e30c | 2016-10-21 12:46:33 +0200 | [diff] [blame] | 1899 | const struct bpf_func_proto bpf_get_numa_node_id_proto __weak; |
Daniel Borkmann | 17ca8cb | 2015-05-29 23:23:06 +0200 | [diff] [blame] | 1900 | const struct bpf_func_proto bpf_ktime_get_ns_proto __weak; |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 1901 | |
Alexei Starovoitov | ffeedaf | 2015-06-12 19:39:12 -0700 | [diff] [blame] | 1902 | const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak; |
| 1903 | const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak; |
| 1904 | const struct bpf_func_proto bpf_get_current_comm_proto __weak; |
Yonghong Song | bf6fa2c8 | 2018-06-03 15:59:41 -0700 | [diff] [blame] | 1905 | const struct bpf_func_proto bpf_get_current_cgroup_id_proto __weak; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 1906 | const struct bpf_func_proto bpf_get_local_storage_proto __weak; |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 1907 | |
Alexei Starovoitov | 0756ea3 | 2015-06-12 19:39:13 -0700 | [diff] [blame] | 1908 | const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void) |
| 1909 | { |
| 1910 | return NULL; |
| 1911 | } |
Daniel Borkmann | 03e69b5 | 2015-03-14 02:27:16 +0100 | [diff] [blame] | 1912 | |
Daniel Borkmann | 555c8a8 | 2016-07-14 18:08:05 +0200 | [diff] [blame] | 1913 | u64 __weak |
| 1914 | bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size, |
| 1915 | void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy) |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 1916 | { |
Daniel Borkmann | 555c8a8 | 2016-07-14 18:08:05 +0200 | [diff] [blame] | 1917 | return -ENOTSUPP; |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 1918 | } |
Jakub Kicinski | 6cb5fb3 | 2018-05-03 18:37:10 -0700 | [diff] [blame] | 1919 | EXPORT_SYMBOL_GPL(bpf_event_output); |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 1920 | |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1921 | /* Always built-in helper functions. */ |
| 1922 | const struct bpf_func_proto bpf_tail_call_proto = { |
| 1923 | .func = NULL, |
| 1924 | .gpl_only = false, |
| 1925 | .ret_type = RET_VOID, |
| 1926 | .arg1_type = ARG_PTR_TO_CTX, |
| 1927 | .arg2_type = ARG_CONST_MAP_PTR, |
| 1928 | .arg3_type = ARG_ANYTHING, |
| 1929 | }; |
| 1930 | |
Daniel Borkmann | 9383191 | 2017-02-16 22:24:49 +0100 | [diff] [blame] | 1931 | /* Stub for JITs that only support cBPF. eBPF programs are interpreted. |
| 1932 | * It is encouraged to implement bpf_int_jit_compile() instead, so that |
| 1933 | * eBPF and implicitly also cBPF can get JITed! |
| 1934 | */ |
Daniel Borkmann | d1c55ab | 2016-05-13 19:08:31 +0200 | [diff] [blame] | 1935 | struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog) |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1936 | { |
Daniel Borkmann | d1c55ab | 2016-05-13 19:08:31 +0200 | [diff] [blame] | 1937 | return prog; |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 1938 | } |
| 1939 | |
Daniel Borkmann | 9383191 | 2017-02-16 22:24:49 +0100 | [diff] [blame] | 1940 | /* Stub for JITs that support eBPF. All cBPF code gets transformed into |
| 1941 | * eBPF by the kernel and is later compiled by bpf_int_jit_compile(). |
| 1942 | */ |
| 1943 | void __weak bpf_jit_compile(struct bpf_prog *prog) |
| 1944 | { |
| 1945 | } |
| 1946 | |
Martin KaFai Lau | 17bedab | 2016-12-07 15:53:11 -0800 | [diff] [blame] | 1947 | bool __weak bpf_helper_changes_pkt_data(void *func) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1948 | { |
| 1949 | return false; |
| 1950 | } |
| 1951 | |
Alexei Starovoitov | f89b775 | 2014-10-23 18:41:08 -0700 | [diff] [blame] | 1952 | /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call |
| 1953 | * skb_copy_bits(), so provide a weak definition of it for NET-less config. |
| 1954 | */ |
| 1955 | int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to, |
| 1956 | int len) |
| 1957 | { |
| 1958 | return -EFAULT; |
| 1959 | } |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 1960 | |
| 1961 | /* All definitions of tracepoints related to BPF. */ |
| 1962 | #define CREATE_TRACE_POINTS |
| 1963 | #include <linux/bpf_trace.h> |
| 1964 | |
| 1965 | EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception); |