Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016 Facebook |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of version 2 of the GNU General Public |
| 6 | * License as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | */ |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/bpf.h> |
| 17 | #include <linux/filter.h> |
| 18 | #include <net/netlink.h> |
| 19 | #include <linux/file.h> |
| 20 | #include <linux/vmalloc.h> |
| 21 | |
| 22 | /* bpf_check() is a static code analyzer that walks eBPF program |
| 23 | * instruction by instruction and updates register/stack state. |
| 24 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. |
| 25 | * |
| 26 | * The first pass is depth-first-search to check that the program is a DAG. |
| 27 | * It rejects the following programs: |
| 28 | * - larger than BPF_MAXINSNS insns |
| 29 | * - if loop is present (detected via back-edge) |
| 30 | * - unreachable insns exist (shouldn't be a forest. program = one function) |
| 31 | * - out of bounds or malformed jumps |
| 32 | * The second pass is all possible path descent from the 1st insn. |
| 33 | * Since it's analyzing all pathes through the program, the length of the |
| 34 | * analysis is limited to 32k insn, which may be hit even if total number of |
| 35 | * insn is less then 4K, but there are too many branches that change stack/regs. |
| 36 | * Number of 'branches to be analyzed' is limited to 1k |
| 37 | * |
| 38 | * On entry to each instruction, each register has a type, and the instruction |
| 39 | * changes the types of the registers depending on instruction semantics. |
| 40 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is |
| 41 | * copied to R1. |
| 42 | * |
| 43 | * All registers are 64-bit. |
| 44 | * R0 - return register |
| 45 | * R1-R5 argument passing registers |
| 46 | * R6-R9 callee saved registers |
| 47 | * R10 - frame pointer read-only |
| 48 | * |
| 49 | * At the start of BPF program the register R1 contains a pointer to bpf_context |
| 50 | * and has type PTR_TO_CTX. |
| 51 | * |
| 52 | * Verifier tracks arithmetic operations on pointers in case: |
| 53 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), |
| 54 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), |
| 55 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 |
| 56 | * and 2nd arithmetic instruction is pattern matched to recognize |
| 57 | * that it wants to construct a pointer to some element within stack. |
| 58 | * So after 2nd insn, the register R1 has type PTR_TO_STACK |
| 59 | * (and -20 constant is saved for further stack bounds checking). |
| 60 | * Meaning that this reg is a pointer to stack plus known immediate constant. |
| 61 | * |
| 62 | * Most of the time the registers have UNKNOWN_VALUE type, which |
| 63 | * means the register has some value, but it's not a valid pointer. |
| 64 | * (like pointer plus pointer becomes UNKNOWN_VALUE type) |
| 65 | * |
| 66 | * When verifier sees load or store instructions the type of base register |
| 67 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, FRAME_PTR. These are three pointer |
| 68 | * types recognized by check_mem_access() function. |
| 69 | * |
| 70 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' |
| 71 | * and the range of [ptr, ptr + map's value_size) is accessible. |
| 72 | * |
| 73 | * registers used to pass values to function calls are checked against |
| 74 | * function argument constraints. |
| 75 | * |
| 76 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. |
| 77 | * It means that the register type passed to this function must be |
| 78 | * PTR_TO_STACK and it will be used inside the function as |
| 79 | * 'pointer to map element key' |
| 80 | * |
| 81 | * For example the argument constraints for bpf_map_lookup_elem(): |
| 82 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, |
| 83 | * .arg1_type = ARG_CONST_MAP_PTR, |
| 84 | * .arg2_type = ARG_PTR_TO_MAP_KEY, |
| 85 | * |
| 86 | * ret_type says that this function returns 'pointer to map elem value or null' |
| 87 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and |
| 88 | * 2nd argument should be a pointer to stack, which will be used inside |
| 89 | * the helper function as a pointer to map element key. |
| 90 | * |
| 91 | * On the kernel side the helper function looks like: |
| 92 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 93 | * { |
| 94 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; |
| 95 | * void *key = (void *) (unsigned long) r2; |
| 96 | * void *value; |
| 97 | * |
| 98 | * here kernel can access 'key' and 'map' pointers safely, knowing that |
| 99 | * [key, key + map->key_size) bytes are valid and were initialized on |
| 100 | * the stack of eBPF program. |
| 101 | * } |
| 102 | * |
| 103 | * Corresponding eBPF program may look like: |
| 104 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR |
| 105 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK |
| 106 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP |
| 107 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), |
| 108 | * here verifier looks at prototype of map_lookup_elem() and sees: |
| 109 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, |
| 110 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes |
| 111 | * |
| 112 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, |
| 113 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits |
| 114 | * and were initialized prior to this call. |
| 115 | * If it's ok, then verifier allows this BPF_CALL insn and looks at |
| 116 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets |
| 117 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function |
| 118 | * returns ether pointer to map value or NULL. |
| 119 | * |
| 120 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' |
| 121 | * insn, the register holding that pointer in the true branch changes state to |
| 122 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false |
| 123 | * branch. See check_cond_jmp_op(). |
| 124 | * |
| 125 | * After the call R0 is set to return type of the function and registers R1-R5 |
| 126 | * are set to NOT_INIT to indicate that they are no longer readable. |
| 127 | */ |
| 128 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 129 | struct reg_state { |
| 130 | enum bpf_reg_type type; |
| 131 | union { |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 132 | /* valid when type == CONST_IMM | PTR_TO_STACK | UNKNOWN_VALUE */ |
| 133 | s64 imm; |
| 134 | |
| 135 | /* valid when type == PTR_TO_PACKET* */ |
| 136 | struct { |
| 137 | u32 id; |
| 138 | u16 off; |
| 139 | u16 range; |
| 140 | }; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 141 | |
| 142 | /* valid when type == CONST_PTR_TO_MAP | PTR_TO_MAP_VALUE | |
| 143 | * PTR_TO_MAP_VALUE_OR_NULL |
| 144 | */ |
| 145 | struct bpf_map *map_ptr; |
| 146 | }; |
| 147 | }; |
| 148 | |
| 149 | enum bpf_stack_slot_type { |
| 150 | STACK_INVALID, /* nothing was stored in this stack slot */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 151 | STACK_SPILL, /* register spilled into stack */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 152 | STACK_MISC /* BPF program wrote some data into this slot */ |
| 153 | }; |
| 154 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 155 | #define BPF_REG_SIZE 8 /* size of eBPF register in bytes */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 156 | |
| 157 | /* state of the program: |
| 158 | * type of all registers and stack info |
| 159 | */ |
| 160 | struct verifier_state { |
| 161 | struct reg_state regs[MAX_BPF_REG]; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 162 | u8 stack_slot_type[MAX_BPF_STACK]; |
| 163 | struct reg_state spilled_regs[MAX_BPF_STACK / BPF_REG_SIZE]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | /* linked list of verifier states used to prune search */ |
| 167 | struct verifier_state_list { |
| 168 | struct verifier_state state; |
| 169 | struct verifier_state_list *next; |
| 170 | }; |
| 171 | |
| 172 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
| 173 | struct verifier_stack_elem { |
| 174 | /* verifer state is 'st' |
| 175 | * before processing instruction 'insn_idx' |
| 176 | * and after processing instruction 'prev_insn_idx' |
| 177 | */ |
| 178 | struct verifier_state st; |
| 179 | int insn_idx; |
| 180 | int prev_insn_idx; |
| 181 | struct verifier_stack_elem *next; |
| 182 | }; |
| 183 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 184 | #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */ |
| 185 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 186 | /* single container for all structs |
| 187 | * one verifier_env per bpf_check() call |
| 188 | */ |
| 189 | struct verifier_env { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 190 | struct bpf_prog *prog; /* eBPF program being verified */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 191 | struct verifier_stack_elem *head; /* stack of verifier states to be processed */ |
| 192 | int stack_size; /* number of states to be processed */ |
| 193 | struct verifier_state cur_state; /* current verifier state */ |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 194 | struct verifier_state_list **explored_states; /* search pruning optimization */ |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 195 | struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */ |
| 196 | u32 used_map_cnt; /* number of used maps */ |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 197 | bool allow_ptr_leaks; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 198 | }; |
| 199 | |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 200 | #define BPF_COMPLEXITY_LIMIT_INSNS 65536 |
| 201 | #define BPF_COMPLEXITY_LIMIT_STACK 1024 |
| 202 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 203 | struct bpf_call_arg_meta { |
| 204 | struct bpf_map *map_ptr; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 205 | bool raw_mode; |
| 206 | int regno; |
| 207 | int access_size; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 208 | }; |
| 209 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 210 | /* verbose verifier prints what it's seeing |
| 211 | * bpf_check() is called under lock, so no race to access these global vars |
| 212 | */ |
| 213 | static u32 log_level, log_size, log_len; |
| 214 | static char *log_buf; |
| 215 | |
| 216 | static DEFINE_MUTEX(bpf_verifier_lock); |
| 217 | |
| 218 | /* log_level controls verbosity level of eBPF verifier. |
| 219 | * verbose() is used to dump the verification trace to the log, so the user |
| 220 | * can figure out what's wrong with the program |
| 221 | */ |
Daniel Borkmann | 1d056d9 | 2015-11-03 11:39:20 +0100 | [diff] [blame] | 222 | static __printf(1, 2) void verbose(const char *fmt, ...) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 223 | { |
| 224 | va_list args; |
| 225 | |
| 226 | if (log_level == 0 || log_len >= log_size - 1) |
| 227 | return; |
| 228 | |
| 229 | va_start(args, fmt); |
| 230 | log_len += vscnprintf(log_buf + log_len, log_size - log_len, fmt, args); |
| 231 | va_end(args); |
| 232 | } |
| 233 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 234 | /* string representation of 'enum bpf_reg_type' */ |
| 235 | static const char * const reg_type_str[] = { |
| 236 | [NOT_INIT] = "?", |
| 237 | [UNKNOWN_VALUE] = "inv", |
| 238 | [PTR_TO_CTX] = "ctx", |
| 239 | [CONST_PTR_TO_MAP] = "map_ptr", |
| 240 | [PTR_TO_MAP_VALUE] = "map_value", |
| 241 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", |
| 242 | [FRAME_PTR] = "fp", |
| 243 | [PTR_TO_STACK] = "fp", |
| 244 | [CONST_IMM] = "imm", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 245 | [PTR_TO_PACKET] = "pkt", |
| 246 | [PTR_TO_PACKET_END] = "pkt_end", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 247 | }; |
| 248 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 249 | static void print_verifier_state(struct verifier_state *state) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 250 | { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 251 | struct reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 252 | enum bpf_reg_type t; |
| 253 | int i; |
| 254 | |
| 255 | for (i = 0; i < MAX_BPF_REG; i++) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 256 | reg = &state->regs[i]; |
| 257 | t = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 258 | if (t == NOT_INIT) |
| 259 | continue; |
| 260 | verbose(" R%d=%s", i, reg_type_str[t]); |
| 261 | if (t == CONST_IMM || t == PTR_TO_STACK) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 262 | verbose("%lld", reg->imm); |
| 263 | else if (t == PTR_TO_PACKET) |
| 264 | verbose("(id=%d,off=%d,r=%d)", |
| 265 | reg->id, reg->off, reg->range); |
| 266 | else if (t == UNKNOWN_VALUE && reg->imm) |
| 267 | verbose("%lld", reg->imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 268 | else if (t == CONST_PTR_TO_MAP || t == PTR_TO_MAP_VALUE || |
| 269 | t == PTR_TO_MAP_VALUE_OR_NULL) |
| 270 | verbose("(ks=%d,vs=%d)", |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 271 | reg->map_ptr->key_size, |
| 272 | reg->map_ptr->value_size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 273 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 274 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 275 | if (state->stack_slot_type[i] == STACK_SPILL) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 276 | verbose(" fp%d=%s", -MAX_BPF_STACK + i, |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 277 | reg_type_str[state->spilled_regs[i / BPF_REG_SIZE].type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 278 | } |
| 279 | verbose("\n"); |
| 280 | } |
| 281 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 282 | static const char *const bpf_class_string[] = { |
| 283 | [BPF_LD] = "ld", |
| 284 | [BPF_LDX] = "ldx", |
| 285 | [BPF_ST] = "st", |
| 286 | [BPF_STX] = "stx", |
| 287 | [BPF_ALU] = "alu", |
| 288 | [BPF_JMP] = "jmp", |
| 289 | [BPF_RET] = "BUG", |
| 290 | [BPF_ALU64] = "alu64", |
| 291 | }; |
| 292 | |
Alexei Starovoitov | 687f071 | 2015-09-08 13:40:01 -0700 | [diff] [blame] | 293 | static const char *const bpf_alu_string[16] = { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 294 | [BPF_ADD >> 4] = "+=", |
| 295 | [BPF_SUB >> 4] = "-=", |
| 296 | [BPF_MUL >> 4] = "*=", |
| 297 | [BPF_DIV >> 4] = "/=", |
| 298 | [BPF_OR >> 4] = "|=", |
| 299 | [BPF_AND >> 4] = "&=", |
| 300 | [BPF_LSH >> 4] = "<<=", |
| 301 | [BPF_RSH >> 4] = ">>=", |
| 302 | [BPF_NEG >> 4] = "neg", |
| 303 | [BPF_MOD >> 4] = "%=", |
| 304 | [BPF_XOR >> 4] = "^=", |
| 305 | [BPF_MOV >> 4] = "=", |
| 306 | [BPF_ARSH >> 4] = "s>>=", |
| 307 | [BPF_END >> 4] = "endian", |
| 308 | }; |
| 309 | |
| 310 | static const char *const bpf_ldst_string[] = { |
| 311 | [BPF_W >> 3] = "u32", |
| 312 | [BPF_H >> 3] = "u16", |
| 313 | [BPF_B >> 3] = "u8", |
| 314 | [BPF_DW >> 3] = "u64", |
| 315 | }; |
| 316 | |
Alexei Starovoitov | 687f071 | 2015-09-08 13:40:01 -0700 | [diff] [blame] | 317 | static const char *const bpf_jmp_string[16] = { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 318 | [BPF_JA >> 4] = "jmp", |
| 319 | [BPF_JEQ >> 4] = "==", |
| 320 | [BPF_JGT >> 4] = ">", |
| 321 | [BPF_JGE >> 4] = ">=", |
| 322 | [BPF_JSET >> 4] = "&", |
| 323 | [BPF_JNE >> 4] = "!=", |
| 324 | [BPF_JSGT >> 4] = "s>", |
| 325 | [BPF_JSGE >> 4] = "s>=", |
| 326 | [BPF_CALL >> 4] = "call", |
| 327 | [BPF_EXIT >> 4] = "exit", |
| 328 | }; |
| 329 | |
| 330 | static void print_bpf_insn(struct bpf_insn *insn) |
| 331 | { |
| 332 | u8 class = BPF_CLASS(insn->code); |
| 333 | |
| 334 | if (class == BPF_ALU || class == BPF_ALU64) { |
| 335 | if (BPF_SRC(insn->code) == BPF_X) |
| 336 | verbose("(%02x) %sr%d %s %sr%d\n", |
| 337 | insn->code, class == BPF_ALU ? "(u32) " : "", |
| 338 | insn->dst_reg, |
| 339 | bpf_alu_string[BPF_OP(insn->code) >> 4], |
| 340 | class == BPF_ALU ? "(u32) " : "", |
| 341 | insn->src_reg); |
| 342 | else |
| 343 | verbose("(%02x) %sr%d %s %s%d\n", |
| 344 | insn->code, class == BPF_ALU ? "(u32) " : "", |
| 345 | insn->dst_reg, |
| 346 | bpf_alu_string[BPF_OP(insn->code) >> 4], |
| 347 | class == BPF_ALU ? "(u32) " : "", |
| 348 | insn->imm); |
| 349 | } else if (class == BPF_STX) { |
| 350 | if (BPF_MODE(insn->code) == BPF_MEM) |
| 351 | verbose("(%02x) *(%s *)(r%d %+d) = r%d\n", |
| 352 | insn->code, |
| 353 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 354 | insn->dst_reg, |
| 355 | insn->off, insn->src_reg); |
| 356 | else if (BPF_MODE(insn->code) == BPF_XADD) |
| 357 | verbose("(%02x) lock *(%s *)(r%d %+d) += r%d\n", |
| 358 | insn->code, |
| 359 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 360 | insn->dst_reg, insn->off, |
| 361 | insn->src_reg); |
| 362 | else |
| 363 | verbose("BUG_%02x\n", insn->code); |
| 364 | } else if (class == BPF_ST) { |
| 365 | if (BPF_MODE(insn->code) != BPF_MEM) { |
| 366 | verbose("BUG_st_%02x\n", insn->code); |
| 367 | return; |
| 368 | } |
| 369 | verbose("(%02x) *(%s *)(r%d %+d) = %d\n", |
| 370 | insn->code, |
| 371 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 372 | insn->dst_reg, |
| 373 | insn->off, insn->imm); |
| 374 | } else if (class == BPF_LDX) { |
| 375 | if (BPF_MODE(insn->code) != BPF_MEM) { |
| 376 | verbose("BUG_ldx_%02x\n", insn->code); |
| 377 | return; |
| 378 | } |
| 379 | verbose("(%02x) r%d = *(%s *)(r%d %+d)\n", |
| 380 | insn->code, insn->dst_reg, |
| 381 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 382 | insn->src_reg, insn->off); |
| 383 | } else if (class == BPF_LD) { |
| 384 | if (BPF_MODE(insn->code) == BPF_ABS) { |
| 385 | verbose("(%02x) r0 = *(%s *)skb[%d]\n", |
| 386 | insn->code, |
| 387 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 388 | insn->imm); |
| 389 | } else if (BPF_MODE(insn->code) == BPF_IND) { |
| 390 | verbose("(%02x) r0 = *(%s *)skb[r%d + %d]\n", |
| 391 | insn->code, |
| 392 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 393 | insn->src_reg, insn->imm); |
| 394 | } else if (BPF_MODE(insn->code) == BPF_IMM) { |
| 395 | verbose("(%02x) r%d = 0x%x\n", |
| 396 | insn->code, insn->dst_reg, insn->imm); |
| 397 | } else { |
| 398 | verbose("BUG_ld_%02x\n", insn->code); |
| 399 | return; |
| 400 | } |
| 401 | } else if (class == BPF_JMP) { |
| 402 | u8 opcode = BPF_OP(insn->code); |
| 403 | |
| 404 | if (opcode == BPF_CALL) { |
| 405 | verbose("(%02x) call %d\n", insn->code, insn->imm); |
| 406 | } else if (insn->code == (BPF_JMP | BPF_JA)) { |
| 407 | verbose("(%02x) goto pc%+d\n", |
| 408 | insn->code, insn->off); |
| 409 | } else if (insn->code == (BPF_JMP | BPF_EXIT)) { |
| 410 | verbose("(%02x) exit\n", insn->code); |
| 411 | } else if (BPF_SRC(insn->code) == BPF_X) { |
| 412 | verbose("(%02x) if r%d %s r%d goto pc%+d\n", |
| 413 | insn->code, insn->dst_reg, |
| 414 | bpf_jmp_string[BPF_OP(insn->code) >> 4], |
| 415 | insn->src_reg, insn->off); |
| 416 | } else { |
| 417 | verbose("(%02x) if r%d %s 0x%x goto pc%+d\n", |
| 418 | insn->code, insn->dst_reg, |
| 419 | bpf_jmp_string[BPF_OP(insn->code) >> 4], |
| 420 | insn->imm, insn->off); |
| 421 | } |
| 422 | } else { |
| 423 | verbose("(%02x) %s\n", insn->code, bpf_class_string[class]); |
| 424 | } |
| 425 | } |
| 426 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 427 | static int pop_stack(struct verifier_env *env, int *prev_insn_idx) |
| 428 | { |
| 429 | struct verifier_stack_elem *elem; |
| 430 | int insn_idx; |
| 431 | |
| 432 | if (env->head == NULL) |
| 433 | return -1; |
| 434 | |
| 435 | memcpy(&env->cur_state, &env->head->st, sizeof(env->cur_state)); |
| 436 | insn_idx = env->head->insn_idx; |
| 437 | if (prev_insn_idx) |
| 438 | *prev_insn_idx = env->head->prev_insn_idx; |
| 439 | elem = env->head->next; |
| 440 | kfree(env->head); |
| 441 | env->head = elem; |
| 442 | env->stack_size--; |
| 443 | return insn_idx; |
| 444 | } |
| 445 | |
| 446 | static struct verifier_state *push_stack(struct verifier_env *env, int insn_idx, |
| 447 | int prev_insn_idx) |
| 448 | { |
| 449 | struct verifier_stack_elem *elem; |
| 450 | |
| 451 | elem = kmalloc(sizeof(struct verifier_stack_elem), GFP_KERNEL); |
| 452 | if (!elem) |
| 453 | goto err; |
| 454 | |
| 455 | memcpy(&elem->st, &env->cur_state, sizeof(env->cur_state)); |
| 456 | elem->insn_idx = insn_idx; |
| 457 | elem->prev_insn_idx = prev_insn_idx; |
| 458 | elem->next = env->head; |
| 459 | env->head = elem; |
| 460 | env->stack_size++; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 461 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 462 | verbose("BPF program is too complex\n"); |
| 463 | goto err; |
| 464 | } |
| 465 | return &elem->st; |
| 466 | err: |
| 467 | /* pop all elements and return */ |
| 468 | while (pop_stack(env, NULL) >= 0); |
| 469 | return NULL; |
| 470 | } |
| 471 | |
| 472 | #define CALLER_SAVED_REGS 6 |
| 473 | static const int caller_saved[CALLER_SAVED_REGS] = { |
| 474 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 |
| 475 | }; |
| 476 | |
| 477 | static void init_reg_state(struct reg_state *regs) |
| 478 | { |
| 479 | int i; |
| 480 | |
| 481 | for (i = 0; i < MAX_BPF_REG; i++) { |
| 482 | regs[i].type = NOT_INIT; |
| 483 | regs[i].imm = 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | /* frame pointer */ |
| 487 | regs[BPF_REG_FP].type = FRAME_PTR; |
| 488 | |
| 489 | /* 1st arg to a function */ |
| 490 | regs[BPF_REG_1].type = PTR_TO_CTX; |
| 491 | } |
| 492 | |
| 493 | static void mark_reg_unknown_value(struct reg_state *regs, u32 regno) |
| 494 | { |
| 495 | BUG_ON(regno >= MAX_BPF_REG); |
| 496 | regs[regno].type = UNKNOWN_VALUE; |
| 497 | regs[regno].imm = 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | enum reg_arg_type { |
| 501 | SRC_OP, /* register is used as source operand */ |
| 502 | DST_OP, /* register is used as destination operand */ |
| 503 | DST_OP_NO_MARK /* same as above, check only, don't mark */ |
| 504 | }; |
| 505 | |
| 506 | static int check_reg_arg(struct reg_state *regs, u32 regno, |
| 507 | enum reg_arg_type t) |
| 508 | { |
| 509 | if (regno >= MAX_BPF_REG) { |
| 510 | verbose("R%d is invalid\n", regno); |
| 511 | return -EINVAL; |
| 512 | } |
| 513 | |
| 514 | if (t == SRC_OP) { |
| 515 | /* check whether register used as source operand can be read */ |
| 516 | if (regs[regno].type == NOT_INIT) { |
| 517 | verbose("R%d !read_ok\n", regno); |
| 518 | return -EACCES; |
| 519 | } |
| 520 | } else { |
| 521 | /* check whether register used as dest operand can be written to */ |
| 522 | if (regno == BPF_REG_FP) { |
| 523 | verbose("frame pointer is read only\n"); |
| 524 | return -EACCES; |
| 525 | } |
| 526 | if (t == DST_OP) |
| 527 | mark_reg_unknown_value(regs, regno); |
| 528 | } |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | static int bpf_size_to_bytes(int bpf_size) |
| 533 | { |
| 534 | if (bpf_size == BPF_W) |
| 535 | return 4; |
| 536 | else if (bpf_size == BPF_H) |
| 537 | return 2; |
| 538 | else if (bpf_size == BPF_B) |
| 539 | return 1; |
| 540 | else if (bpf_size == BPF_DW) |
| 541 | return 8; |
| 542 | else |
| 543 | return -EINVAL; |
| 544 | } |
| 545 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 546 | static bool is_spillable_regtype(enum bpf_reg_type type) |
| 547 | { |
| 548 | switch (type) { |
| 549 | case PTR_TO_MAP_VALUE: |
| 550 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 551 | case PTR_TO_STACK: |
| 552 | case PTR_TO_CTX: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 553 | case PTR_TO_PACKET: |
| 554 | case PTR_TO_PACKET_END: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 555 | case FRAME_PTR: |
| 556 | case CONST_PTR_TO_MAP: |
| 557 | return true; |
| 558 | default: |
| 559 | return false; |
| 560 | } |
| 561 | } |
| 562 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 563 | /* check_stack_read/write functions track spill/fill of registers, |
| 564 | * stack boundary and alignment are checked in check_mem_access() |
| 565 | */ |
| 566 | static int check_stack_write(struct verifier_state *state, int off, int size, |
| 567 | int value_regno) |
| 568 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 569 | int i; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 570 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
| 571 | * so it's aligned access and [off, off + size) are within stack limits |
| 572 | */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 573 | |
| 574 | if (value_regno >= 0 && |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 575 | is_spillable_regtype(state->regs[value_regno].type)) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 576 | |
| 577 | /* register containing pointer is being spilled into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 578 | if (size != BPF_REG_SIZE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 579 | verbose("invalid size of register spill\n"); |
| 580 | return -EACCES; |
| 581 | } |
| 582 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 583 | /* save register state */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 584 | state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] = |
| 585 | state->regs[value_regno]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 586 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 587 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 588 | state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_SPILL; |
| 589 | } else { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 590 | /* regular write of data into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 591 | state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] = |
| 592 | (struct reg_state) {}; |
| 593 | |
| 594 | for (i = 0; i < size; i++) |
| 595 | state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_MISC; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 596 | } |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | static int check_stack_read(struct verifier_state *state, int off, int size, |
| 601 | int value_regno) |
| 602 | { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 603 | u8 *slot_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 604 | int i; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 605 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 606 | slot_type = &state->stack_slot_type[MAX_BPF_STACK + off]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 607 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 608 | if (slot_type[0] == STACK_SPILL) { |
| 609 | if (size != BPF_REG_SIZE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 610 | verbose("invalid size of register spill\n"); |
| 611 | return -EACCES; |
| 612 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 613 | for (i = 1; i < BPF_REG_SIZE; i++) { |
| 614 | if (slot_type[i] != STACK_SPILL) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 615 | verbose("corrupted spill memory\n"); |
| 616 | return -EACCES; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | if (value_regno >= 0) |
| 621 | /* restore register state from stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 622 | state->regs[value_regno] = |
| 623 | state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 624 | return 0; |
| 625 | } else { |
| 626 | for (i = 0; i < size; i++) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 627 | if (slot_type[i] != STACK_MISC) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 628 | verbose("invalid read from stack off %d+%d size %d\n", |
| 629 | off, i, size); |
| 630 | return -EACCES; |
| 631 | } |
| 632 | } |
| 633 | if (value_regno >= 0) |
| 634 | /* have read misc data from the stack */ |
| 635 | mark_reg_unknown_value(state->regs, value_regno); |
| 636 | return 0; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | /* check read/write into map element returned by bpf_map_lookup_elem() */ |
| 641 | static int check_map_access(struct verifier_env *env, u32 regno, int off, |
| 642 | int size) |
| 643 | { |
| 644 | struct bpf_map *map = env->cur_state.regs[regno].map_ptr; |
| 645 | |
| 646 | if (off < 0 || off + size > map->value_size) { |
| 647 | verbose("invalid access to map value, value_size=%d off=%d size=%d\n", |
| 648 | map->value_size, off, size); |
| 649 | return -EACCES; |
| 650 | } |
| 651 | return 0; |
| 652 | } |
| 653 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 654 | #define MAX_PACKET_OFF 0xffff |
| 655 | |
| 656 | static int check_packet_access(struct verifier_env *env, u32 regno, int off, |
| 657 | int size) |
| 658 | { |
| 659 | struct reg_state *regs = env->cur_state.regs; |
| 660 | struct reg_state *reg = ®s[regno]; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 661 | |
Alexei Starovoitov | d91b28e | 2016-05-19 18:17:13 -0700 | [diff] [blame] | 662 | off += reg->off; |
| 663 | if (off < 0 || off + size > reg->range) { |
| 664 | verbose("invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", |
| 665 | off, size, regno, reg->id, reg->off, reg->range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 666 | return -EACCES; |
| 667 | } |
| 668 | return 0; |
| 669 | } |
| 670 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 671 | /* check access to 'struct bpf_context' fields */ |
| 672 | static int check_ctx_access(struct verifier_env *env, int off, int size, |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 673 | enum bpf_access_type t, enum bpf_reg_type *reg_type) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 674 | { |
| 675 | if (env->prog->aux->ops->is_valid_access && |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 676 | env->prog->aux->ops->is_valid_access(off, size, t, reg_type)) { |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 677 | /* remember the offset of last byte accessed in ctx */ |
| 678 | if (env->prog->aux->max_ctx_offset < off + size) |
| 679 | env->prog->aux->max_ctx_offset = off + size; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 680 | return 0; |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 681 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 682 | |
| 683 | verbose("invalid bpf_context access off=%d size=%d\n", off, size); |
| 684 | return -EACCES; |
| 685 | } |
| 686 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 687 | static bool is_pointer_value(struct verifier_env *env, int regno) |
| 688 | { |
| 689 | if (env->allow_ptr_leaks) |
| 690 | return false; |
| 691 | |
| 692 | switch (env->cur_state.regs[regno].type) { |
| 693 | case UNKNOWN_VALUE: |
| 694 | case CONST_IMM: |
| 695 | return false; |
| 696 | default: |
| 697 | return true; |
| 698 | } |
| 699 | } |
| 700 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 701 | static int check_ptr_alignment(struct verifier_env *env, struct reg_state *reg, |
| 702 | int off, int size) |
| 703 | { |
| 704 | if (reg->type != PTR_TO_PACKET) { |
| 705 | if (off % size != 0) { |
| 706 | verbose("misaligned access off %d size %d\n", off, size); |
| 707 | return -EACCES; |
| 708 | } else { |
| 709 | return 0; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | switch (env->prog->type) { |
| 714 | case BPF_PROG_TYPE_SCHED_CLS: |
| 715 | case BPF_PROG_TYPE_SCHED_ACT: |
| 716 | break; |
| 717 | default: |
| 718 | verbose("verifier is misconfigured\n"); |
| 719 | return -EACCES; |
| 720 | } |
| 721 | |
| 722 | if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
| 723 | /* misaligned access to packet is ok on x86,arm,arm64 */ |
| 724 | return 0; |
| 725 | |
| 726 | if (reg->id && size != 1) { |
| 727 | verbose("Unknown packet alignment. Only byte-sized access allowed\n"); |
| 728 | return -EACCES; |
| 729 | } |
| 730 | |
| 731 | /* skb->data is NET_IP_ALIGN-ed */ |
| 732 | if ((NET_IP_ALIGN + reg->off + off) % size != 0) { |
| 733 | verbose("misaligned packet access off %d+%d+%d size %d\n", |
| 734 | NET_IP_ALIGN, reg->off, off, size); |
| 735 | return -EACCES; |
| 736 | } |
| 737 | return 0; |
| 738 | } |
| 739 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 740 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 741 | * if t==write, value_regno is a register which value is stored into memory |
| 742 | * if t==read, value_regno is a register which will receive the value from memory |
| 743 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 744 | * if t==read && value_regno==-1, don't care what we read from memory |
| 745 | */ |
| 746 | static int check_mem_access(struct verifier_env *env, u32 regno, int off, |
| 747 | int bpf_size, enum bpf_access_type t, |
| 748 | int value_regno) |
| 749 | { |
| 750 | struct verifier_state *state = &env->cur_state; |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 751 | struct reg_state *reg = &state->regs[regno]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 752 | int size, err = 0; |
| 753 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 754 | if (reg->type == PTR_TO_STACK) |
| 755 | off += reg->imm; |
Alex Gartrell | 24b4d2a | 2015-07-23 14:24:40 -0700 | [diff] [blame] | 756 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 757 | size = bpf_size_to_bytes(bpf_size); |
| 758 | if (size < 0) |
| 759 | return size; |
| 760 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 761 | err = check_ptr_alignment(env, reg, off, size); |
| 762 | if (err) |
| 763 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 764 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 765 | if (reg->type == PTR_TO_MAP_VALUE) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 766 | if (t == BPF_WRITE && value_regno >= 0 && |
| 767 | is_pointer_value(env, value_regno)) { |
| 768 | verbose("R%d leaks addr into map\n", value_regno); |
| 769 | return -EACCES; |
| 770 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 771 | err = check_map_access(env, regno, off, size); |
| 772 | if (!err && t == BPF_READ && value_regno >= 0) |
| 773 | mark_reg_unknown_value(state->regs, value_regno); |
| 774 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 775 | } else if (reg->type == PTR_TO_CTX) { |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 776 | enum bpf_reg_type reg_type = UNKNOWN_VALUE; |
| 777 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 778 | if (t == BPF_WRITE && value_regno >= 0 && |
| 779 | is_pointer_value(env, value_regno)) { |
| 780 | verbose("R%d leaks addr into ctx\n", value_regno); |
| 781 | return -EACCES; |
| 782 | } |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 783 | err = check_ctx_access(env, off, size, t, ®_type); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 784 | if (!err && t == BPF_READ && value_regno >= 0) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 785 | mark_reg_unknown_value(state->regs, value_regno); |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 786 | if (env->allow_ptr_leaks) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 787 | /* note that reg.[id|off|range] == 0 */ |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 788 | state->regs[value_regno].type = reg_type; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 789 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 790 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 791 | } else if (reg->type == FRAME_PTR || reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 792 | if (off >= 0 || off < -MAX_BPF_STACK) { |
| 793 | verbose("invalid stack off=%d size=%d\n", off, size); |
| 794 | return -EACCES; |
| 795 | } |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 796 | if (t == BPF_WRITE) { |
| 797 | if (!env->allow_ptr_leaks && |
| 798 | state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL && |
| 799 | size != BPF_REG_SIZE) { |
| 800 | verbose("attempt to corrupt spilled pointer on stack\n"); |
| 801 | return -EACCES; |
| 802 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 803 | err = check_stack_write(state, off, size, value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 804 | } else { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 805 | err = check_stack_read(state, off, size, value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 806 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 807 | } else if (state->regs[regno].type == PTR_TO_PACKET) { |
| 808 | if (t == BPF_WRITE) { |
| 809 | verbose("cannot write into packet\n"); |
| 810 | return -EACCES; |
| 811 | } |
| 812 | err = check_packet_access(env, regno, off, size); |
| 813 | if (!err && t == BPF_READ && value_regno >= 0) |
| 814 | mark_reg_unknown_value(state->regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 815 | } else { |
| 816 | verbose("R%d invalid mem access '%s'\n", |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 817 | regno, reg_type_str[reg->type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 818 | return -EACCES; |
| 819 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 820 | |
| 821 | if (!err && size <= 2 && value_regno >= 0 && env->allow_ptr_leaks && |
| 822 | state->regs[value_regno].type == UNKNOWN_VALUE) { |
| 823 | /* 1 or 2 byte load zero-extends, determine the number of |
| 824 | * zero upper bits. Not doing it fo 4 byte load, since |
| 825 | * such values cannot be added to ptr_to_packet anyway. |
| 826 | */ |
| 827 | state->regs[value_regno].imm = 64 - size * 8; |
| 828 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 829 | return err; |
| 830 | } |
| 831 | |
| 832 | static int check_xadd(struct verifier_env *env, struct bpf_insn *insn) |
| 833 | { |
| 834 | struct reg_state *regs = env->cur_state.regs; |
| 835 | int err; |
| 836 | |
| 837 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || |
| 838 | insn->imm != 0) { |
| 839 | verbose("BPF_XADD uses reserved fields\n"); |
| 840 | return -EINVAL; |
| 841 | } |
| 842 | |
| 843 | /* check src1 operand */ |
| 844 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 845 | if (err) |
| 846 | return err; |
| 847 | |
| 848 | /* check src2 operand */ |
| 849 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 850 | if (err) |
| 851 | return err; |
| 852 | |
| 853 | /* check whether atomic_add can read the memory */ |
| 854 | err = check_mem_access(env, insn->dst_reg, insn->off, |
| 855 | BPF_SIZE(insn->code), BPF_READ, -1); |
| 856 | if (err) |
| 857 | return err; |
| 858 | |
| 859 | /* check whether atomic_add can write into the same memory */ |
| 860 | return check_mem_access(env, insn->dst_reg, insn->off, |
| 861 | BPF_SIZE(insn->code), BPF_WRITE, -1); |
| 862 | } |
| 863 | |
| 864 | /* when register 'regno' is passed into function that will read 'access_size' |
| 865 | * bytes from that pointer, make sure that it's within stack boundary |
| 866 | * and all elements of stack are initialized |
| 867 | */ |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 868 | static int check_stack_boundary(struct verifier_env *env, int regno, |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 869 | int access_size, bool zero_size_allowed, |
| 870 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 871 | { |
| 872 | struct verifier_state *state = &env->cur_state; |
| 873 | struct reg_state *regs = state->regs; |
| 874 | int off, i; |
| 875 | |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 876 | if (regs[regno].type != PTR_TO_STACK) { |
| 877 | if (zero_size_allowed && access_size == 0 && |
| 878 | regs[regno].type == CONST_IMM && |
| 879 | regs[regno].imm == 0) |
| 880 | return 0; |
| 881 | |
| 882 | verbose("R%d type=%s expected=%s\n", regno, |
| 883 | reg_type_str[regs[regno].type], |
| 884 | reg_type_str[PTR_TO_STACK]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 885 | return -EACCES; |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 886 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 887 | |
| 888 | off = regs[regno].imm; |
| 889 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
| 890 | access_size <= 0) { |
| 891 | verbose("invalid stack type R%d off=%d access_size=%d\n", |
| 892 | regno, off, access_size); |
| 893 | return -EACCES; |
| 894 | } |
| 895 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 896 | if (meta && meta->raw_mode) { |
| 897 | meta->access_size = access_size; |
| 898 | meta->regno = regno; |
| 899 | return 0; |
| 900 | } |
| 901 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 902 | for (i = 0; i < access_size; i++) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 903 | if (state->stack_slot_type[MAX_BPF_STACK + off + i] != STACK_MISC) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 904 | verbose("invalid indirect read from stack off %d+%d size %d\n", |
| 905 | off, i, access_size); |
| 906 | return -EACCES; |
| 907 | } |
| 908 | } |
| 909 | return 0; |
| 910 | } |
| 911 | |
| 912 | static int check_func_arg(struct verifier_env *env, u32 regno, |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 913 | enum bpf_arg_type arg_type, |
| 914 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 915 | { |
| 916 | struct reg_state *reg = env->cur_state.regs + regno; |
| 917 | enum bpf_reg_type expected_type; |
| 918 | int err = 0; |
| 919 | |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 920 | if (arg_type == ARG_DONTCARE) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 921 | return 0; |
| 922 | |
| 923 | if (reg->type == NOT_INIT) { |
| 924 | verbose("R%d !read_ok\n", regno); |
| 925 | return -EACCES; |
| 926 | } |
| 927 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 928 | if (arg_type == ARG_ANYTHING) { |
| 929 | if (is_pointer_value(env, regno)) { |
| 930 | verbose("R%d leaks addr into helper function\n", regno); |
| 931 | return -EACCES; |
| 932 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 933 | return 0; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 934 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 935 | |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 936 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 937 | arg_type == ARG_PTR_TO_MAP_VALUE) { |
| 938 | expected_type = PTR_TO_STACK; |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 939 | } else if (arg_type == ARG_CONST_STACK_SIZE || |
| 940 | arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 941 | expected_type = CONST_IMM; |
| 942 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
| 943 | expected_type = CONST_PTR_TO_MAP; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 944 | } else if (arg_type == ARG_PTR_TO_CTX) { |
| 945 | expected_type = PTR_TO_CTX; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 946 | } else if (arg_type == ARG_PTR_TO_STACK || |
| 947 | arg_type == ARG_PTR_TO_RAW_STACK) { |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 948 | expected_type = PTR_TO_STACK; |
| 949 | /* One exception here. In case function allows for NULL to be |
| 950 | * passed in as argument, it's a CONST_IMM type. Final test |
| 951 | * happens during stack boundary checking. |
| 952 | */ |
| 953 | if (reg->type == CONST_IMM && reg->imm == 0) |
| 954 | expected_type = CONST_IMM; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 955 | meta->raw_mode = arg_type == ARG_PTR_TO_RAW_STACK; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 956 | } else { |
| 957 | verbose("unsupported arg_type %d\n", arg_type); |
| 958 | return -EFAULT; |
| 959 | } |
| 960 | |
| 961 | if (reg->type != expected_type) { |
| 962 | verbose("R%d type=%s expected=%s\n", regno, |
| 963 | reg_type_str[reg->type], reg_type_str[expected_type]); |
| 964 | return -EACCES; |
| 965 | } |
| 966 | |
| 967 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 968 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 969 | meta->map_ptr = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 970 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 971 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 972 | * check that [key, key + map->key_size) are within |
| 973 | * stack limits and initialized |
| 974 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 975 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 976 | /* in function declaration map_ptr must come before |
| 977 | * map_key, so that it's verified and known before |
| 978 | * we have to check map_key here. Otherwise it means |
| 979 | * that kernel subsystem misconfigured verifier |
| 980 | */ |
| 981 | verbose("invalid map_ptr to access map->key\n"); |
| 982 | return -EACCES; |
| 983 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 984 | err = check_stack_boundary(env, regno, meta->map_ptr->key_size, |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 985 | false, NULL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 986 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE) { |
| 987 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 988 | * check [value, value + map->value_size) validity |
| 989 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 990 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 991 | /* kernel subsystem misconfigured verifier */ |
| 992 | verbose("invalid map_ptr to access map->value\n"); |
| 993 | return -EACCES; |
| 994 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 995 | err = check_stack_boundary(env, regno, |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 996 | meta->map_ptr->value_size, |
| 997 | false, NULL); |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 998 | } else if (arg_type == ARG_CONST_STACK_SIZE || |
| 999 | arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) { |
| 1000 | bool zero_size_allowed = (arg_type == ARG_CONST_STACK_SIZE_OR_ZERO); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1001 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1002 | /* bpf_xxx(..., buf, len) call will access 'len' bytes |
| 1003 | * from stack pointer 'buf'. Check it |
| 1004 | * note: regno == len, regno - 1 == buf |
| 1005 | */ |
| 1006 | if (regno == 0) { |
| 1007 | /* kernel subsystem misconfigured verifier */ |
| 1008 | verbose("ARG_CONST_STACK_SIZE cannot be first argument\n"); |
| 1009 | return -EACCES; |
| 1010 | } |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1011 | err = check_stack_boundary(env, regno - 1, reg->imm, |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1012 | zero_size_allowed, meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | return err; |
| 1016 | } |
| 1017 | |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1018 | static int check_map_func_compatibility(struct bpf_map *map, int func_id) |
| 1019 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1020 | if (!map) |
| 1021 | return 0; |
| 1022 | |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1023 | /* We need a two way check, first is from map perspective ... */ |
| 1024 | switch (map->map_type) { |
| 1025 | case BPF_MAP_TYPE_PROG_ARRAY: |
| 1026 | if (func_id != BPF_FUNC_tail_call) |
| 1027 | goto error; |
| 1028 | break; |
| 1029 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
| 1030 | if (func_id != BPF_FUNC_perf_event_read && |
| 1031 | func_id != BPF_FUNC_perf_event_output) |
| 1032 | goto error; |
| 1033 | break; |
| 1034 | case BPF_MAP_TYPE_STACK_TRACE: |
| 1035 | if (func_id != BPF_FUNC_get_stackid) |
| 1036 | goto error; |
| 1037 | break; |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 1038 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 1039 | if (func_id != BPF_FUNC_skb_in_cgroup) |
| 1040 | goto error; |
| 1041 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1042 | default: |
| 1043 | break; |
| 1044 | } |
| 1045 | |
| 1046 | /* ... and second from the function itself. */ |
| 1047 | switch (func_id) { |
| 1048 | case BPF_FUNC_tail_call: |
| 1049 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 1050 | goto error; |
| 1051 | break; |
| 1052 | case BPF_FUNC_perf_event_read: |
| 1053 | case BPF_FUNC_perf_event_output: |
| 1054 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
| 1055 | goto error; |
| 1056 | break; |
| 1057 | case BPF_FUNC_get_stackid: |
| 1058 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) |
| 1059 | goto error; |
| 1060 | break; |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 1061 | case BPF_FUNC_skb_in_cgroup: |
| 1062 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
| 1063 | goto error; |
| 1064 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1065 | default: |
| 1066 | break; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | return 0; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1070 | error: |
| 1071 | verbose("cannot pass map_type %d into func %d\n", |
| 1072 | map->map_type, func_id); |
| 1073 | return -EINVAL; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1076 | static int check_raw_mode(const struct bpf_func_proto *fn) |
| 1077 | { |
| 1078 | int count = 0; |
| 1079 | |
| 1080 | if (fn->arg1_type == ARG_PTR_TO_RAW_STACK) |
| 1081 | count++; |
| 1082 | if (fn->arg2_type == ARG_PTR_TO_RAW_STACK) |
| 1083 | count++; |
| 1084 | if (fn->arg3_type == ARG_PTR_TO_RAW_STACK) |
| 1085 | count++; |
| 1086 | if (fn->arg4_type == ARG_PTR_TO_RAW_STACK) |
| 1087 | count++; |
| 1088 | if (fn->arg5_type == ARG_PTR_TO_RAW_STACK) |
| 1089 | count++; |
| 1090 | |
| 1091 | return count > 1 ? -EINVAL : 0; |
| 1092 | } |
| 1093 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1094 | static void clear_all_pkt_pointers(struct verifier_env *env) |
| 1095 | { |
| 1096 | struct verifier_state *state = &env->cur_state; |
| 1097 | struct reg_state *regs = state->regs, *reg; |
| 1098 | int i; |
| 1099 | |
| 1100 | for (i = 0; i < MAX_BPF_REG; i++) |
| 1101 | if (regs[i].type == PTR_TO_PACKET || |
| 1102 | regs[i].type == PTR_TO_PACKET_END) |
| 1103 | mark_reg_unknown_value(regs, i); |
| 1104 | |
| 1105 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { |
| 1106 | if (state->stack_slot_type[i] != STACK_SPILL) |
| 1107 | continue; |
| 1108 | reg = &state->spilled_regs[i / BPF_REG_SIZE]; |
| 1109 | if (reg->type != PTR_TO_PACKET && |
| 1110 | reg->type != PTR_TO_PACKET_END) |
| 1111 | continue; |
| 1112 | reg->type = UNKNOWN_VALUE; |
| 1113 | reg->imm = 0; |
| 1114 | } |
| 1115 | } |
| 1116 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1117 | static int check_call(struct verifier_env *env, int func_id) |
| 1118 | { |
| 1119 | struct verifier_state *state = &env->cur_state; |
| 1120 | const struct bpf_func_proto *fn = NULL; |
| 1121 | struct reg_state *regs = state->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1122 | struct reg_state *reg; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1123 | struct bpf_call_arg_meta meta; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1124 | bool changes_data; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1125 | int i, err; |
| 1126 | |
| 1127 | /* find function prototype */ |
| 1128 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
| 1129 | verbose("invalid func %d\n", func_id); |
| 1130 | return -EINVAL; |
| 1131 | } |
| 1132 | |
| 1133 | if (env->prog->aux->ops->get_func_proto) |
| 1134 | fn = env->prog->aux->ops->get_func_proto(func_id); |
| 1135 | |
| 1136 | if (!fn) { |
| 1137 | verbose("unknown func %d\n", func_id); |
| 1138 | return -EINVAL; |
| 1139 | } |
| 1140 | |
| 1141 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 1142 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1143 | verbose("cannot call GPL only function from proprietary program\n"); |
| 1144 | return -EINVAL; |
| 1145 | } |
| 1146 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1147 | changes_data = bpf_helper_changes_skb_data(fn->func); |
| 1148 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1149 | memset(&meta, 0, sizeof(meta)); |
| 1150 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1151 | /* We only support one arg being in raw mode at the moment, which |
| 1152 | * is sufficient for the helper functions we have right now. |
| 1153 | */ |
| 1154 | err = check_raw_mode(fn); |
| 1155 | if (err) { |
| 1156 | verbose("kernel subsystem misconfigured func %d\n", func_id); |
| 1157 | return err; |
| 1158 | } |
| 1159 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1160 | /* check args */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1161 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1162 | if (err) |
| 1163 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1164 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1165 | if (err) |
| 1166 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1167 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1168 | if (err) |
| 1169 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1170 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1171 | if (err) |
| 1172 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1173 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1174 | if (err) |
| 1175 | return err; |
| 1176 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1177 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
| 1178 | * is inferred from register state. |
| 1179 | */ |
| 1180 | for (i = 0; i < meta.access_size; i++) { |
| 1181 | err = check_mem_access(env, meta.regno, i, BPF_B, BPF_WRITE, -1); |
| 1182 | if (err) |
| 1183 | return err; |
| 1184 | } |
| 1185 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1186 | /* reset caller saved regs */ |
| 1187 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 1188 | reg = regs + caller_saved[i]; |
| 1189 | reg->type = NOT_INIT; |
| 1190 | reg->imm = 0; |
| 1191 | } |
| 1192 | |
| 1193 | /* update return register */ |
| 1194 | if (fn->ret_type == RET_INTEGER) { |
| 1195 | regs[BPF_REG_0].type = UNKNOWN_VALUE; |
| 1196 | } else if (fn->ret_type == RET_VOID) { |
| 1197 | regs[BPF_REG_0].type = NOT_INIT; |
| 1198 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) { |
| 1199 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
| 1200 | /* remember map_ptr, so that check_map_access() |
| 1201 | * can check 'value_size' boundary of memory access |
| 1202 | * to map element returned from bpf_map_lookup_elem() |
| 1203 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1204 | if (meta.map_ptr == NULL) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1205 | verbose("kernel subsystem misconfigured verifier\n"); |
| 1206 | return -EINVAL; |
| 1207 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1208 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1209 | } else { |
| 1210 | verbose("unknown return type %d of func %d\n", |
| 1211 | fn->ret_type, func_id); |
| 1212 | return -EINVAL; |
| 1213 | } |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1214 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1215 | err = check_map_func_compatibility(meta.map_ptr, func_id); |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1216 | if (err) |
| 1217 | return err; |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1218 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1219 | if (changes_data) |
| 1220 | clear_all_pkt_pointers(env); |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
| 1224 | static int check_packet_ptr_add(struct verifier_env *env, struct bpf_insn *insn) |
| 1225 | { |
| 1226 | struct reg_state *regs = env->cur_state.regs; |
| 1227 | struct reg_state *dst_reg = ®s[insn->dst_reg]; |
| 1228 | struct reg_state *src_reg = ®s[insn->src_reg]; |
Alexei Starovoitov | 1b9b69e | 2016-05-19 18:17:14 -0700 | [diff] [blame] | 1229 | struct reg_state tmp_reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1230 | s32 imm; |
| 1231 | |
| 1232 | if (BPF_SRC(insn->code) == BPF_K) { |
| 1233 | /* pkt_ptr += imm */ |
| 1234 | imm = insn->imm; |
| 1235 | |
| 1236 | add_imm: |
| 1237 | if (imm <= 0) { |
| 1238 | verbose("addition of negative constant to packet pointer is not allowed\n"); |
| 1239 | return -EACCES; |
| 1240 | } |
| 1241 | if (imm >= MAX_PACKET_OFF || |
| 1242 | imm + dst_reg->off >= MAX_PACKET_OFF) { |
| 1243 | verbose("constant %d is too large to add to packet pointer\n", |
| 1244 | imm); |
| 1245 | return -EACCES; |
| 1246 | } |
| 1247 | /* a constant was added to pkt_ptr. |
| 1248 | * Remember it while keeping the same 'id' |
| 1249 | */ |
| 1250 | dst_reg->off += imm; |
| 1251 | } else { |
Alexei Starovoitov | 1b9b69e | 2016-05-19 18:17:14 -0700 | [diff] [blame] | 1252 | if (src_reg->type == PTR_TO_PACKET) { |
| 1253 | /* R6=pkt(id=0,off=0,r=62) R7=imm22; r7 += r6 */ |
| 1254 | tmp_reg = *dst_reg; /* save r7 state */ |
| 1255 | *dst_reg = *src_reg; /* copy pkt_ptr state r6 into r7 */ |
| 1256 | src_reg = &tmp_reg; /* pretend it's src_reg state */ |
| 1257 | /* if the checks below reject it, the copy won't matter, |
| 1258 | * since we're rejecting the whole program. If all ok, |
| 1259 | * then imm22 state will be added to r7 |
| 1260 | * and r7 will be pkt(id=0,off=22,r=62) while |
| 1261 | * r6 will stay as pkt(id=0,off=0,r=62) |
| 1262 | */ |
| 1263 | } |
| 1264 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1265 | if (src_reg->type == CONST_IMM) { |
| 1266 | /* pkt_ptr += reg where reg is known constant */ |
| 1267 | imm = src_reg->imm; |
| 1268 | goto add_imm; |
| 1269 | } |
| 1270 | /* disallow pkt_ptr += reg |
| 1271 | * if reg is not uknown_value with guaranteed zero upper bits |
| 1272 | * otherwise pkt_ptr may overflow and addition will become |
| 1273 | * subtraction which is not allowed |
| 1274 | */ |
| 1275 | if (src_reg->type != UNKNOWN_VALUE) { |
| 1276 | verbose("cannot add '%s' to ptr_to_packet\n", |
| 1277 | reg_type_str[src_reg->type]); |
| 1278 | return -EACCES; |
| 1279 | } |
| 1280 | if (src_reg->imm < 48) { |
| 1281 | verbose("cannot add integer value with %lld upper zero bits to ptr_to_packet\n", |
| 1282 | src_reg->imm); |
| 1283 | return -EACCES; |
| 1284 | } |
| 1285 | /* dst_reg stays as pkt_ptr type and since some positive |
| 1286 | * integer value was added to the pointer, increment its 'id' |
| 1287 | */ |
| 1288 | dst_reg->id++; |
| 1289 | |
| 1290 | /* something was added to pkt_ptr, set range and off to zero */ |
| 1291 | dst_reg->off = 0; |
| 1292 | dst_reg->range = 0; |
| 1293 | } |
| 1294 | return 0; |
| 1295 | } |
| 1296 | |
| 1297 | static int evaluate_reg_alu(struct verifier_env *env, struct bpf_insn *insn) |
| 1298 | { |
| 1299 | struct reg_state *regs = env->cur_state.regs; |
| 1300 | struct reg_state *dst_reg = ®s[insn->dst_reg]; |
| 1301 | u8 opcode = BPF_OP(insn->code); |
| 1302 | s64 imm_log2; |
| 1303 | |
| 1304 | /* for type == UNKNOWN_VALUE: |
| 1305 | * imm > 0 -> number of zero upper bits |
| 1306 | * imm == 0 -> don't track which is the same as all bits can be non-zero |
| 1307 | */ |
| 1308 | |
| 1309 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1310 | struct reg_state *src_reg = ®s[insn->src_reg]; |
| 1311 | |
| 1312 | if (src_reg->type == UNKNOWN_VALUE && src_reg->imm > 0 && |
| 1313 | dst_reg->imm && opcode == BPF_ADD) { |
| 1314 | /* dreg += sreg |
| 1315 | * where both have zero upper bits. Adding them |
| 1316 | * can only result making one more bit non-zero |
| 1317 | * in the larger value. |
| 1318 | * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47) |
| 1319 | * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47) |
| 1320 | */ |
| 1321 | dst_reg->imm = min(dst_reg->imm, src_reg->imm); |
| 1322 | dst_reg->imm--; |
| 1323 | return 0; |
| 1324 | } |
| 1325 | if (src_reg->type == CONST_IMM && src_reg->imm > 0 && |
| 1326 | dst_reg->imm && opcode == BPF_ADD) { |
| 1327 | /* dreg += sreg |
| 1328 | * where dreg has zero upper bits and sreg is const. |
| 1329 | * Adding them can only result making one more bit |
| 1330 | * non-zero in the larger value. |
| 1331 | */ |
| 1332 | imm_log2 = __ilog2_u64((long long)src_reg->imm); |
| 1333 | dst_reg->imm = min(dst_reg->imm, 63 - imm_log2); |
| 1334 | dst_reg->imm--; |
| 1335 | return 0; |
| 1336 | } |
| 1337 | /* all other cases non supported yet, just mark dst_reg */ |
| 1338 | dst_reg->imm = 0; |
| 1339 | return 0; |
| 1340 | } |
| 1341 | |
| 1342 | /* sign extend 32-bit imm into 64-bit to make sure that |
| 1343 | * negative values occupy bit 63. Note ilog2() would have |
| 1344 | * been incorrect, since sizeof(insn->imm) == 4 |
| 1345 | */ |
| 1346 | imm_log2 = __ilog2_u64((long long)insn->imm); |
| 1347 | |
| 1348 | if (dst_reg->imm && opcode == BPF_LSH) { |
| 1349 | /* reg <<= imm |
| 1350 | * if reg was a result of 2 byte load, then its imm == 48 |
| 1351 | * which means that upper 48 bits are zero and shifting this reg |
| 1352 | * left by 4 would mean that upper 44 bits are still zero |
| 1353 | */ |
| 1354 | dst_reg->imm -= insn->imm; |
| 1355 | } else if (dst_reg->imm && opcode == BPF_MUL) { |
| 1356 | /* reg *= imm |
| 1357 | * if multiplying by 14 subtract 4 |
| 1358 | * This is conservative calculation of upper zero bits. |
| 1359 | * It's not trying to special case insn->imm == 1 or 0 cases |
| 1360 | */ |
| 1361 | dst_reg->imm -= imm_log2 + 1; |
| 1362 | } else if (opcode == BPF_AND) { |
| 1363 | /* reg &= imm */ |
| 1364 | dst_reg->imm = 63 - imm_log2; |
| 1365 | } else if (dst_reg->imm && opcode == BPF_ADD) { |
| 1366 | /* reg += imm */ |
| 1367 | dst_reg->imm = min(dst_reg->imm, 63 - imm_log2); |
| 1368 | dst_reg->imm--; |
| 1369 | } else if (opcode == BPF_RSH) { |
| 1370 | /* reg >>= imm |
| 1371 | * which means that after right shift, upper bits will be zero |
| 1372 | * note that verifier already checked that |
| 1373 | * 0 <= imm < 64 for shift insn |
| 1374 | */ |
| 1375 | dst_reg->imm += insn->imm; |
| 1376 | if (unlikely(dst_reg->imm > 64)) |
| 1377 | /* some dumb code did: |
| 1378 | * r2 = *(u32 *)mem; |
| 1379 | * r2 >>= 32; |
| 1380 | * and all bits are zero now */ |
| 1381 | dst_reg->imm = 64; |
| 1382 | } else { |
| 1383 | /* all other alu ops, means that we don't know what will |
| 1384 | * happen to the value, mark it with unknown number of zero bits |
| 1385 | */ |
| 1386 | dst_reg->imm = 0; |
| 1387 | } |
| 1388 | |
| 1389 | if (dst_reg->imm < 0) { |
| 1390 | /* all 64 bits of the register can contain non-zero bits |
| 1391 | * and such value cannot be added to ptr_to_packet, since it |
| 1392 | * may overflow, mark it as unknown to avoid further eval |
| 1393 | */ |
| 1394 | dst_reg->imm = 0; |
| 1395 | } |
| 1396 | return 0; |
| 1397 | } |
| 1398 | |
| 1399 | static int evaluate_reg_imm_alu(struct verifier_env *env, struct bpf_insn *insn) |
| 1400 | { |
| 1401 | struct reg_state *regs = env->cur_state.regs; |
| 1402 | struct reg_state *dst_reg = ®s[insn->dst_reg]; |
| 1403 | struct reg_state *src_reg = ®s[insn->src_reg]; |
| 1404 | u8 opcode = BPF_OP(insn->code); |
| 1405 | |
| 1406 | /* dst_reg->type == CONST_IMM here, simulate execution of 'add' insn. |
| 1407 | * Don't care about overflow or negative values, just add them |
| 1408 | */ |
| 1409 | if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_K) |
| 1410 | dst_reg->imm += insn->imm; |
| 1411 | else if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_X && |
| 1412 | src_reg->type == CONST_IMM) |
| 1413 | dst_reg->imm += src_reg->imm; |
| 1414 | else |
| 1415 | mark_reg_unknown_value(regs, insn->dst_reg); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1416 | return 0; |
| 1417 | } |
| 1418 | |
| 1419 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1420 | static int check_alu_op(struct verifier_env *env, struct bpf_insn *insn) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1421 | { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1422 | struct reg_state *regs = env->cur_state.regs, *dst_reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1423 | u8 opcode = BPF_OP(insn->code); |
| 1424 | int err; |
| 1425 | |
| 1426 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 1427 | if (opcode == BPF_NEG) { |
| 1428 | if (BPF_SRC(insn->code) != 0 || |
| 1429 | insn->src_reg != BPF_REG_0 || |
| 1430 | insn->off != 0 || insn->imm != 0) { |
| 1431 | verbose("BPF_NEG uses reserved fields\n"); |
| 1432 | return -EINVAL; |
| 1433 | } |
| 1434 | } else { |
| 1435 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
| 1436 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64)) { |
| 1437 | verbose("BPF_END uses reserved fields\n"); |
| 1438 | return -EINVAL; |
| 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | /* check src operand */ |
| 1443 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 1444 | if (err) |
| 1445 | return err; |
| 1446 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1447 | if (is_pointer_value(env, insn->dst_reg)) { |
| 1448 | verbose("R%d pointer arithmetic prohibited\n", |
| 1449 | insn->dst_reg); |
| 1450 | return -EACCES; |
| 1451 | } |
| 1452 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1453 | /* check dest operand */ |
| 1454 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); |
| 1455 | if (err) |
| 1456 | return err; |
| 1457 | |
| 1458 | } else if (opcode == BPF_MOV) { |
| 1459 | |
| 1460 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1461 | if (insn->imm != 0 || insn->off != 0) { |
| 1462 | verbose("BPF_MOV uses reserved fields\n"); |
| 1463 | return -EINVAL; |
| 1464 | } |
| 1465 | |
| 1466 | /* check src operand */ |
| 1467 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 1468 | if (err) |
| 1469 | return err; |
| 1470 | } else { |
| 1471 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
| 1472 | verbose("BPF_MOV uses reserved fields\n"); |
| 1473 | return -EINVAL; |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | /* check dest operand */ |
| 1478 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); |
| 1479 | if (err) |
| 1480 | return err; |
| 1481 | |
| 1482 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1483 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 1484 | /* case: R1 = R2 |
| 1485 | * copy register state to dest reg |
| 1486 | */ |
| 1487 | regs[insn->dst_reg] = regs[insn->src_reg]; |
| 1488 | } else { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1489 | if (is_pointer_value(env, insn->src_reg)) { |
| 1490 | verbose("R%d partial copy of pointer\n", |
| 1491 | insn->src_reg); |
| 1492 | return -EACCES; |
| 1493 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1494 | regs[insn->dst_reg].type = UNKNOWN_VALUE; |
| 1495 | regs[insn->dst_reg].map_ptr = NULL; |
| 1496 | } |
| 1497 | } else { |
| 1498 | /* case: R = imm |
| 1499 | * remember the value we stored into this reg |
| 1500 | */ |
| 1501 | regs[insn->dst_reg].type = CONST_IMM; |
| 1502 | regs[insn->dst_reg].imm = insn->imm; |
| 1503 | } |
| 1504 | |
| 1505 | } else if (opcode > BPF_END) { |
| 1506 | verbose("invalid BPF_ALU opcode %x\n", opcode); |
| 1507 | return -EINVAL; |
| 1508 | |
| 1509 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 1510 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1511 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1512 | if (insn->imm != 0 || insn->off != 0) { |
| 1513 | verbose("BPF_ALU uses reserved fields\n"); |
| 1514 | return -EINVAL; |
| 1515 | } |
| 1516 | /* check src1 operand */ |
| 1517 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 1518 | if (err) |
| 1519 | return err; |
| 1520 | } else { |
| 1521 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
| 1522 | verbose("BPF_ALU uses reserved fields\n"); |
| 1523 | return -EINVAL; |
| 1524 | } |
| 1525 | } |
| 1526 | |
| 1527 | /* check src2 operand */ |
| 1528 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 1529 | if (err) |
| 1530 | return err; |
| 1531 | |
| 1532 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 1533 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
| 1534 | verbose("div by zero\n"); |
| 1535 | return -EINVAL; |
| 1536 | } |
| 1537 | |
Rabin Vincent | 229394e | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 1538 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
| 1539 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { |
| 1540 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; |
| 1541 | |
| 1542 | if (insn->imm < 0 || insn->imm >= size) { |
| 1543 | verbose("invalid shift %d\n", insn->imm); |
| 1544 | return -EINVAL; |
| 1545 | } |
| 1546 | } |
| 1547 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1548 | /* check dest operand */ |
| 1549 | err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK); |
| 1550 | if (err) |
| 1551 | return err; |
| 1552 | |
| 1553 | dst_reg = ®s[insn->dst_reg]; |
| 1554 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1555 | /* pattern match 'bpf_add Rx, imm' instruction */ |
| 1556 | if (opcode == BPF_ADD && BPF_CLASS(insn->code) == BPF_ALU64 && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1557 | dst_reg->type == FRAME_PTR && BPF_SRC(insn->code) == BPF_K) { |
| 1558 | dst_reg->type = PTR_TO_STACK; |
| 1559 | dst_reg->imm = insn->imm; |
| 1560 | return 0; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1561 | } else if (opcode == BPF_ADD && |
| 1562 | BPF_CLASS(insn->code) == BPF_ALU64 && |
Alexei Starovoitov | 1b9b69e | 2016-05-19 18:17:14 -0700 | [diff] [blame] | 1563 | (dst_reg->type == PTR_TO_PACKET || |
| 1564 | (BPF_SRC(insn->code) == BPF_X && |
| 1565 | regs[insn->src_reg].type == PTR_TO_PACKET))) { |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1566 | /* ptr_to_packet += K|X */ |
| 1567 | return check_packet_ptr_add(env, insn); |
| 1568 | } else if (BPF_CLASS(insn->code) == BPF_ALU64 && |
| 1569 | dst_reg->type == UNKNOWN_VALUE && |
| 1570 | env->allow_ptr_leaks) { |
| 1571 | /* unknown += K|X */ |
| 1572 | return evaluate_reg_alu(env, insn); |
| 1573 | } else if (BPF_CLASS(insn->code) == BPF_ALU64 && |
| 1574 | dst_reg->type == CONST_IMM && |
| 1575 | env->allow_ptr_leaks) { |
| 1576 | /* reg_imm += K|X */ |
| 1577 | return evaluate_reg_imm_alu(env, insn); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1578 | } else if (is_pointer_value(env, insn->dst_reg)) { |
| 1579 | verbose("R%d pointer arithmetic prohibited\n", |
| 1580 | insn->dst_reg); |
| 1581 | return -EACCES; |
| 1582 | } else if (BPF_SRC(insn->code) == BPF_X && |
| 1583 | is_pointer_value(env, insn->src_reg)) { |
| 1584 | verbose("R%d pointer arithmetic prohibited\n", |
| 1585 | insn->src_reg); |
| 1586 | return -EACCES; |
| 1587 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1588 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1589 | /* mark dest operand */ |
| 1590 | mark_reg_unknown_value(regs, insn->dst_reg); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | return 0; |
| 1594 | } |
| 1595 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1596 | static void find_good_pkt_pointers(struct verifier_env *env, |
| 1597 | struct reg_state *dst_reg) |
| 1598 | { |
| 1599 | struct verifier_state *state = &env->cur_state; |
| 1600 | struct reg_state *regs = state->regs, *reg; |
| 1601 | int i; |
| 1602 | /* r2 = r3; |
| 1603 | * r2 += 8 |
| 1604 | * if (r2 > pkt_end) goto somewhere |
| 1605 | * r2 == dst_reg, pkt_end == src_reg, |
| 1606 | * r2=pkt(id=n,off=8,r=0) |
| 1607 | * r3=pkt(id=n,off=0,r=0) |
| 1608 | * find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) |
| 1609 | * so that range of bytes [r3, r3 + 8) is safe to access |
| 1610 | */ |
| 1611 | for (i = 0; i < MAX_BPF_REG; i++) |
| 1612 | if (regs[i].type == PTR_TO_PACKET && regs[i].id == dst_reg->id) |
| 1613 | regs[i].range = dst_reg->off; |
| 1614 | |
| 1615 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { |
| 1616 | if (state->stack_slot_type[i] != STACK_SPILL) |
| 1617 | continue; |
| 1618 | reg = &state->spilled_regs[i / BPF_REG_SIZE]; |
| 1619 | if (reg->type == PTR_TO_PACKET && reg->id == dst_reg->id) |
| 1620 | reg->range = dst_reg->off; |
| 1621 | } |
| 1622 | } |
| 1623 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1624 | static int check_cond_jmp_op(struct verifier_env *env, |
| 1625 | struct bpf_insn *insn, int *insn_idx) |
| 1626 | { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1627 | struct reg_state *regs = env->cur_state.regs, *dst_reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1628 | struct verifier_state *other_branch; |
| 1629 | u8 opcode = BPF_OP(insn->code); |
| 1630 | int err; |
| 1631 | |
| 1632 | if (opcode > BPF_EXIT) { |
| 1633 | verbose("invalid BPF_JMP opcode %x\n", opcode); |
| 1634 | return -EINVAL; |
| 1635 | } |
| 1636 | |
| 1637 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1638 | if (insn->imm != 0) { |
| 1639 | verbose("BPF_JMP uses reserved fields\n"); |
| 1640 | return -EINVAL; |
| 1641 | } |
| 1642 | |
| 1643 | /* check src1 operand */ |
| 1644 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 1645 | if (err) |
| 1646 | return err; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1647 | |
| 1648 | if (is_pointer_value(env, insn->src_reg)) { |
| 1649 | verbose("R%d pointer comparison prohibited\n", |
| 1650 | insn->src_reg); |
| 1651 | return -EACCES; |
| 1652 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1653 | } else { |
| 1654 | if (insn->src_reg != BPF_REG_0) { |
| 1655 | verbose("BPF_JMP uses reserved fields\n"); |
| 1656 | return -EINVAL; |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | /* check src2 operand */ |
| 1661 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 1662 | if (err) |
| 1663 | return err; |
| 1664 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1665 | dst_reg = ®s[insn->dst_reg]; |
| 1666 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1667 | /* detect if R == 0 where R was initialized to zero earlier */ |
| 1668 | if (BPF_SRC(insn->code) == BPF_K && |
| 1669 | (opcode == BPF_JEQ || opcode == BPF_JNE) && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1670 | dst_reg->type == CONST_IMM && dst_reg->imm == insn->imm) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1671 | if (opcode == BPF_JEQ) { |
| 1672 | /* if (imm == imm) goto pc+off; |
| 1673 | * only follow the goto, ignore fall-through |
| 1674 | */ |
| 1675 | *insn_idx += insn->off; |
| 1676 | return 0; |
| 1677 | } else { |
| 1678 | /* if (imm != imm) goto pc+off; |
| 1679 | * only follow fall-through branch, since |
| 1680 | * that's where the program will go |
| 1681 | */ |
| 1682 | return 0; |
| 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx); |
| 1687 | if (!other_branch) |
| 1688 | return -EFAULT; |
| 1689 | |
| 1690 | /* detect if R == 0 where R is returned value from bpf_map_lookup_elem() */ |
| 1691 | if (BPF_SRC(insn->code) == BPF_K && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1692 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
| 1693 | dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1694 | if (opcode == BPF_JEQ) { |
| 1695 | /* next fallthrough insn can access memory via |
| 1696 | * this register |
| 1697 | */ |
| 1698 | regs[insn->dst_reg].type = PTR_TO_MAP_VALUE; |
| 1699 | /* branch targer cannot access it, since reg == 0 */ |
Alexei Starovoitov | 735b433 | 2016-05-05 19:49:11 -0700 | [diff] [blame] | 1700 | mark_reg_unknown_value(other_branch->regs, |
| 1701 | insn->dst_reg); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1702 | } else { |
| 1703 | other_branch->regs[insn->dst_reg].type = PTR_TO_MAP_VALUE; |
Alexei Starovoitov | 735b433 | 2016-05-05 19:49:11 -0700 | [diff] [blame] | 1704 | mark_reg_unknown_value(regs, insn->dst_reg); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1705 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1706 | } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT && |
| 1707 | dst_reg->type == PTR_TO_PACKET && |
| 1708 | regs[insn->src_reg].type == PTR_TO_PACKET_END) { |
| 1709 | find_good_pkt_pointers(env, dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1710 | } else if (is_pointer_value(env, insn->dst_reg)) { |
| 1711 | verbose("R%d pointer comparison prohibited\n", insn->dst_reg); |
| 1712 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1713 | } |
| 1714 | if (log_level) |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1715 | print_verifier_state(&env->cur_state); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1716 | return 0; |
| 1717 | } |
| 1718 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 1719 | /* return the map pointer stored inside BPF_LD_IMM64 instruction */ |
| 1720 | static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn) |
| 1721 | { |
| 1722 | u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32; |
| 1723 | |
| 1724 | return (struct bpf_map *) (unsigned long) imm64; |
| 1725 | } |
| 1726 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1727 | /* verify BPF_LD_IMM64 instruction */ |
| 1728 | static int check_ld_imm(struct verifier_env *env, struct bpf_insn *insn) |
| 1729 | { |
| 1730 | struct reg_state *regs = env->cur_state.regs; |
| 1731 | int err; |
| 1732 | |
| 1733 | if (BPF_SIZE(insn->code) != BPF_DW) { |
| 1734 | verbose("invalid BPF_LD_IMM insn\n"); |
| 1735 | return -EINVAL; |
| 1736 | } |
| 1737 | if (insn->off != 0) { |
| 1738 | verbose("BPF_LD_IMM64 uses reserved fields\n"); |
| 1739 | return -EINVAL; |
| 1740 | } |
| 1741 | |
| 1742 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); |
| 1743 | if (err) |
| 1744 | return err; |
| 1745 | |
| 1746 | if (insn->src_reg == 0) |
| 1747 | /* generic move 64-bit immediate into a register */ |
| 1748 | return 0; |
| 1749 | |
| 1750 | /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */ |
| 1751 | BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD); |
| 1752 | |
| 1753 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; |
| 1754 | regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn); |
| 1755 | return 0; |
| 1756 | } |
| 1757 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 1758 | static bool may_access_skb(enum bpf_prog_type type) |
| 1759 | { |
| 1760 | switch (type) { |
| 1761 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 1762 | case BPF_PROG_TYPE_SCHED_CLS: |
Daniel Borkmann | 94caee8 | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 1763 | case BPF_PROG_TYPE_SCHED_ACT: |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 1764 | return true; |
| 1765 | default: |
| 1766 | return false; |
| 1767 | } |
| 1768 | } |
| 1769 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 1770 | /* verify safety of LD_ABS|LD_IND instructions: |
| 1771 | * - they can only appear in the programs where ctx == skb |
| 1772 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 1773 | * preserve R6-R9, and store return value into R0 |
| 1774 | * |
| 1775 | * Implicit input: |
| 1776 | * ctx == skb == R6 == CTX |
| 1777 | * |
| 1778 | * Explicit input: |
| 1779 | * SRC == any register |
| 1780 | * IMM == 32-bit immediate |
| 1781 | * |
| 1782 | * Output: |
| 1783 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 1784 | */ |
| 1785 | static int check_ld_abs(struct verifier_env *env, struct bpf_insn *insn) |
| 1786 | { |
| 1787 | struct reg_state *regs = env->cur_state.regs; |
| 1788 | u8 mode = BPF_MODE(insn->code); |
| 1789 | struct reg_state *reg; |
| 1790 | int i, err; |
| 1791 | |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 1792 | if (!may_access_skb(env->prog->type)) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1793 | verbose("BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 1794 | return -EINVAL; |
| 1795 | } |
| 1796 | |
| 1797 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
Alexei Starovoitov | d82bccc | 2016-04-12 10:26:19 -0700 | [diff] [blame] | 1798 | BPF_SIZE(insn->code) == BPF_DW || |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 1799 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1800 | verbose("BPF_LD_[ABS|IND] uses reserved fields\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 1801 | return -EINVAL; |
| 1802 | } |
| 1803 | |
| 1804 | /* check whether implicit source operand (register R6) is readable */ |
| 1805 | err = check_reg_arg(regs, BPF_REG_6, SRC_OP); |
| 1806 | if (err) |
| 1807 | return err; |
| 1808 | |
| 1809 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { |
| 1810 | verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); |
| 1811 | return -EINVAL; |
| 1812 | } |
| 1813 | |
| 1814 | if (mode == BPF_IND) { |
| 1815 | /* check explicit source operand */ |
| 1816 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 1817 | if (err) |
| 1818 | return err; |
| 1819 | } |
| 1820 | |
| 1821 | /* reset caller saved regs to unreadable */ |
| 1822 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 1823 | reg = regs + caller_saved[i]; |
| 1824 | reg->type = NOT_INIT; |
| 1825 | reg->imm = 0; |
| 1826 | } |
| 1827 | |
| 1828 | /* mark destination R0 register as readable, since it contains |
| 1829 | * the value fetched from the packet |
| 1830 | */ |
| 1831 | regs[BPF_REG_0].type = UNKNOWN_VALUE; |
| 1832 | return 0; |
| 1833 | } |
| 1834 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 1835 | /* non-recursive DFS pseudo code |
| 1836 | * 1 procedure DFS-iterative(G,v): |
| 1837 | * 2 label v as discovered |
| 1838 | * 3 let S be a stack |
| 1839 | * 4 S.push(v) |
| 1840 | * 5 while S is not empty |
| 1841 | * 6 t <- S.pop() |
| 1842 | * 7 if t is what we're looking for: |
| 1843 | * 8 return t |
| 1844 | * 9 for all edges e in G.adjacentEdges(t) do |
| 1845 | * 10 if edge e is already labelled |
| 1846 | * 11 continue with the next edge |
| 1847 | * 12 w <- G.adjacentVertex(t,e) |
| 1848 | * 13 if vertex w is not discovered and not explored |
| 1849 | * 14 label e as tree-edge |
| 1850 | * 15 label w as discovered |
| 1851 | * 16 S.push(w) |
| 1852 | * 17 continue at 5 |
| 1853 | * 18 else if vertex w is discovered |
| 1854 | * 19 label e as back-edge |
| 1855 | * 20 else |
| 1856 | * 21 // vertex w is explored |
| 1857 | * 22 label e as forward- or cross-edge |
| 1858 | * 23 label t as explored |
| 1859 | * 24 S.pop() |
| 1860 | * |
| 1861 | * convention: |
| 1862 | * 0x10 - discovered |
| 1863 | * 0x11 - discovered and fall-through edge labelled |
| 1864 | * 0x12 - discovered and fall-through and branch edges labelled |
| 1865 | * 0x20 - explored |
| 1866 | */ |
| 1867 | |
| 1868 | enum { |
| 1869 | DISCOVERED = 0x10, |
| 1870 | EXPLORED = 0x20, |
| 1871 | FALLTHROUGH = 1, |
| 1872 | BRANCH = 2, |
| 1873 | }; |
| 1874 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 1875 | #define STATE_LIST_MARK ((struct verifier_state_list *) -1L) |
| 1876 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 1877 | static int *insn_stack; /* stack of insns to process */ |
| 1878 | static int cur_stack; /* current stack index */ |
| 1879 | static int *insn_state; |
| 1880 | |
| 1881 | /* t, w, e - match pseudo-code above: |
| 1882 | * t - index of current instruction |
| 1883 | * w - next instruction |
| 1884 | * e - edge |
| 1885 | */ |
| 1886 | static int push_insn(int t, int w, int e, struct verifier_env *env) |
| 1887 | { |
| 1888 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
| 1889 | return 0; |
| 1890 | |
| 1891 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
| 1892 | return 0; |
| 1893 | |
| 1894 | if (w < 0 || w >= env->prog->len) { |
| 1895 | verbose("jump out of range from insn %d to %d\n", t, w); |
| 1896 | return -EINVAL; |
| 1897 | } |
| 1898 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 1899 | if (e == BRANCH) |
| 1900 | /* mark branch target for state pruning */ |
| 1901 | env->explored_states[w] = STATE_LIST_MARK; |
| 1902 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 1903 | if (insn_state[w] == 0) { |
| 1904 | /* tree-edge */ |
| 1905 | insn_state[t] = DISCOVERED | e; |
| 1906 | insn_state[w] = DISCOVERED; |
| 1907 | if (cur_stack >= env->prog->len) |
| 1908 | return -E2BIG; |
| 1909 | insn_stack[cur_stack++] = w; |
| 1910 | return 1; |
| 1911 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
| 1912 | verbose("back-edge from insn %d to %d\n", t, w); |
| 1913 | return -EINVAL; |
| 1914 | } else if (insn_state[w] == EXPLORED) { |
| 1915 | /* forward- or cross-edge */ |
| 1916 | insn_state[t] = DISCOVERED | e; |
| 1917 | } else { |
| 1918 | verbose("insn state internal bug\n"); |
| 1919 | return -EFAULT; |
| 1920 | } |
| 1921 | return 0; |
| 1922 | } |
| 1923 | |
| 1924 | /* non-recursive depth-first-search to detect loops in BPF program |
| 1925 | * loop == back-edge in directed graph |
| 1926 | */ |
| 1927 | static int check_cfg(struct verifier_env *env) |
| 1928 | { |
| 1929 | struct bpf_insn *insns = env->prog->insnsi; |
| 1930 | int insn_cnt = env->prog->len; |
| 1931 | int ret = 0; |
| 1932 | int i, t; |
| 1933 | |
| 1934 | insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 1935 | if (!insn_state) |
| 1936 | return -ENOMEM; |
| 1937 | |
| 1938 | insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 1939 | if (!insn_stack) { |
| 1940 | kfree(insn_state); |
| 1941 | return -ENOMEM; |
| 1942 | } |
| 1943 | |
| 1944 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 1945 | insn_stack[0] = 0; /* 0 is the first instruction */ |
| 1946 | cur_stack = 1; |
| 1947 | |
| 1948 | peek_stack: |
| 1949 | if (cur_stack == 0) |
| 1950 | goto check_state; |
| 1951 | t = insn_stack[cur_stack - 1]; |
| 1952 | |
| 1953 | if (BPF_CLASS(insns[t].code) == BPF_JMP) { |
| 1954 | u8 opcode = BPF_OP(insns[t].code); |
| 1955 | |
| 1956 | if (opcode == BPF_EXIT) { |
| 1957 | goto mark_explored; |
| 1958 | } else if (opcode == BPF_CALL) { |
| 1959 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 1960 | if (ret == 1) |
| 1961 | goto peek_stack; |
| 1962 | else if (ret < 0) |
| 1963 | goto err_free; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 1964 | if (t + 1 < insn_cnt) |
| 1965 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 1966 | } else if (opcode == BPF_JA) { |
| 1967 | if (BPF_SRC(insns[t].code) != BPF_K) { |
| 1968 | ret = -EINVAL; |
| 1969 | goto err_free; |
| 1970 | } |
| 1971 | /* unconditional jump with single edge */ |
| 1972 | ret = push_insn(t, t + insns[t].off + 1, |
| 1973 | FALLTHROUGH, env); |
| 1974 | if (ret == 1) |
| 1975 | goto peek_stack; |
| 1976 | else if (ret < 0) |
| 1977 | goto err_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 1978 | /* tell verifier to check for equivalent states |
| 1979 | * after every call and jump |
| 1980 | */ |
Alexei Starovoitov | c3de631 | 2015-04-14 15:57:13 -0700 | [diff] [blame] | 1981 | if (t + 1 < insn_cnt) |
| 1982 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 1983 | } else { |
| 1984 | /* conditional jump with two edges */ |
| 1985 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 1986 | if (ret == 1) |
| 1987 | goto peek_stack; |
| 1988 | else if (ret < 0) |
| 1989 | goto err_free; |
| 1990 | |
| 1991 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); |
| 1992 | if (ret == 1) |
| 1993 | goto peek_stack; |
| 1994 | else if (ret < 0) |
| 1995 | goto err_free; |
| 1996 | } |
| 1997 | } else { |
| 1998 | /* all other non-branch instructions with single |
| 1999 | * fall-through edge |
| 2000 | */ |
| 2001 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 2002 | if (ret == 1) |
| 2003 | goto peek_stack; |
| 2004 | else if (ret < 0) |
| 2005 | goto err_free; |
| 2006 | } |
| 2007 | |
| 2008 | mark_explored: |
| 2009 | insn_state[t] = EXPLORED; |
| 2010 | if (cur_stack-- <= 0) { |
| 2011 | verbose("pop stack internal bug\n"); |
| 2012 | ret = -EFAULT; |
| 2013 | goto err_free; |
| 2014 | } |
| 2015 | goto peek_stack; |
| 2016 | |
| 2017 | check_state: |
| 2018 | for (i = 0; i < insn_cnt; i++) { |
| 2019 | if (insn_state[i] != EXPLORED) { |
| 2020 | verbose("unreachable insn %d\n", i); |
| 2021 | ret = -EINVAL; |
| 2022 | goto err_free; |
| 2023 | } |
| 2024 | } |
| 2025 | ret = 0; /* cfg looks good */ |
| 2026 | |
| 2027 | err_free: |
| 2028 | kfree(insn_state); |
| 2029 | kfree(insn_stack); |
| 2030 | return ret; |
| 2031 | } |
| 2032 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2033 | /* the following conditions reduce the number of explored insns |
| 2034 | * from ~140k to ~80k for ultra large programs that use a lot of ptr_to_packet |
| 2035 | */ |
| 2036 | static bool compare_ptrs_to_packet(struct reg_state *old, struct reg_state *cur) |
| 2037 | { |
| 2038 | if (old->id != cur->id) |
| 2039 | return false; |
| 2040 | |
| 2041 | /* old ptr_to_packet is more conservative, since it allows smaller |
| 2042 | * range. Ex: |
| 2043 | * old(off=0,r=10) is equal to cur(off=0,r=20), because |
| 2044 | * old(off=0,r=10) means that with range=10 the verifier proceeded |
| 2045 | * further and found no issues with the program. Now we're in the same |
| 2046 | * spot with cur(off=0,r=20), so we're safe too, since anything further |
| 2047 | * will only be looking at most 10 bytes after this pointer. |
| 2048 | */ |
| 2049 | if (old->off == cur->off && old->range < cur->range) |
| 2050 | return true; |
| 2051 | |
| 2052 | /* old(off=20,r=10) is equal to cur(off=22,re=22 or 5 or 0) |
| 2053 | * since both cannot be used for packet access and safe(old) |
| 2054 | * pointer has smaller off that could be used for further |
| 2055 | * 'if (ptr > data_end)' check |
| 2056 | * Ex: |
| 2057 | * old(off=20,r=10) and cur(off=22,r=22) and cur(off=22,r=0) mean |
| 2058 | * that we cannot access the packet. |
| 2059 | * The safe range is: |
| 2060 | * [ptr, ptr + range - off) |
| 2061 | * so whenever off >=range, it means no safe bytes from this pointer. |
| 2062 | * When comparing old->off <= cur->off, it means that older code |
| 2063 | * went with smaller offset and that offset was later |
| 2064 | * used to figure out the safe range after 'if (ptr > data_end)' check |
| 2065 | * Say, 'old' state was explored like: |
| 2066 | * ... R3(off=0, r=0) |
| 2067 | * R4 = R3 + 20 |
| 2068 | * ... now R4(off=20,r=0) <-- here |
| 2069 | * if (R4 > data_end) |
| 2070 | * ... R4(off=20,r=20), R3(off=0,r=20) and R3 can be used to access. |
| 2071 | * ... the code further went all the way to bpf_exit. |
| 2072 | * Now the 'cur' state at the mark 'here' has R4(off=30,r=0). |
| 2073 | * old_R4(off=20,r=0) equal to cur_R4(off=30,r=0), since if the verifier |
| 2074 | * goes further, such cur_R4 will give larger safe packet range after |
| 2075 | * 'if (R4 > data_end)' and all further insn were already good with r=20, |
| 2076 | * so they will be good with r=30 and we can prune the search. |
| 2077 | */ |
| 2078 | if (old->off <= cur->off && |
| 2079 | old->off >= old->range && cur->off >= cur->range) |
| 2080 | return true; |
| 2081 | |
| 2082 | return false; |
| 2083 | } |
| 2084 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2085 | /* compare two verifier states |
| 2086 | * |
| 2087 | * all states stored in state_list are known to be valid, since |
| 2088 | * verifier reached 'bpf_exit' instruction through them |
| 2089 | * |
| 2090 | * this function is called when verifier exploring different branches of |
| 2091 | * execution popped from the state stack. If it sees an old state that has |
| 2092 | * more strict register state and more strict stack state then this execution |
| 2093 | * branch doesn't need to be explored further, since verifier already |
| 2094 | * concluded that more strict state leads to valid finish. |
| 2095 | * |
| 2096 | * Therefore two states are equivalent if register state is more conservative |
| 2097 | * and explored stack state is more conservative than the current one. |
| 2098 | * Example: |
| 2099 | * explored current |
| 2100 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 2101 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 2102 | * |
| 2103 | * In other words if current stack state (one being explored) has more |
| 2104 | * valid slots than old one that already passed validation, it means |
| 2105 | * the verifier can stop exploring and conclude that current state is valid too |
| 2106 | * |
| 2107 | * Similarly with registers. If explored state has register type as invalid |
| 2108 | * whereas register type in current state is meaningful, it means that |
| 2109 | * the current state will reach 'bpf_exit' instruction safely |
| 2110 | */ |
| 2111 | static bool states_equal(struct verifier_state *old, struct verifier_state *cur) |
| 2112 | { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2113 | struct reg_state *rold, *rcur; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2114 | int i; |
| 2115 | |
| 2116 | for (i = 0; i < MAX_BPF_REG; i++) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2117 | rold = &old->regs[i]; |
| 2118 | rcur = &cur->regs[i]; |
| 2119 | |
| 2120 | if (memcmp(rold, rcur, sizeof(*rold)) == 0) |
| 2121 | continue; |
| 2122 | |
| 2123 | if (rold->type == NOT_INIT || |
| 2124 | (rold->type == UNKNOWN_VALUE && rcur->type != NOT_INIT)) |
| 2125 | continue; |
| 2126 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2127 | if (rold->type == PTR_TO_PACKET && rcur->type == PTR_TO_PACKET && |
| 2128 | compare_ptrs_to_packet(rold, rcur)) |
| 2129 | continue; |
| 2130 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2131 | return false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | for (i = 0; i < MAX_BPF_STACK; i++) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2135 | if (old->stack_slot_type[i] == STACK_INVALID) |
| 2136 | continue; |
| 2137 | if (old->stack_slot_type[i] != cur->stack_slot_type[i]) |
| 2138 | /* Ex: old explored (safe) state has STACK_SPILL in |
| 2139 | * this stack slot, but current has has STACK_MISC -> |
| 2140 | * this verifier states are not equivalent, |
| 2141 | * return false to continue verification of this path |
| 2142 | */ |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2143 | return false; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2144 | if (i % BPF_REG_SIZE) |
| 2145 | continue; |
| 2146 | if (memcmp(&old->spilled_regs[i / BPF_REG_SIZE], |
| 2147 | &cur->spilled_regs[i / BPF_REG_SIZE], |
| 2148 | sizeof(old->spilled_regs[0]))) |
| 2149 | /* when explored and current stack slot types are |
| 2150 | * the same, check that stored pointers types |
| 2151 | * are the same as well. |
| 2152 | * Ex: explored safe path could have stored |
| 2153 | * (struct reg_state) {.type = PTR_TO_STACK, .imm = -8} |
| 2154 | * but current path has stored: |
| 2155 | * (struct reg_state) {.type = PTR_TO_STACK, .imm = -16} |
| 2156 | * such verifier states are not equivalent. |
| 2157 | * return false to continue verification of this path |
| 2158 | */ |
| 2159 | return false; |
| 2160 | else |
| 2161 | continue; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2162 | } |
| 2163 | return true; |
| 2164 | } |
| 2165 | |
| 2166 | static int is_state_visited(struct verifier_env *env, int insn_idx) |
| 2167 | { |
| 2168 | struct verifier_state_list *new_sl; |
| 2169 | struct verifier_state_list *sl; |
| 2170 | |
| 2171 | sl = env->explored_states[insn_idx]; |
| 2172 | if (!sl) |
| 2173 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 2174 | * be doing state search here |
| 2175 | */ |
| 2176 | return 0; |
| 2177 | |
| 2178 | while (sl != STATE_LIST_MARK) { |
| 2179 | if (states_equal(&sl->state, &env->cur_state)) |
| 2180 | /* reached equivalent register/stack state, |
| 2181 | * prune the search |
| 2182 | */ |
| 2183 | return 1; |
| 2184 | sl = sl->next; |
| 2185 | } |
| 2186 | |
| 2187 | /* there were no equivalent states, remember current one. |
| 2188 | * technically the current state is not proven to be safe yet, |
| 2189 | * but it will either reach bpf_exit (which means it's safe) or |
| 2190 | * it will be rejected. Since there are no loops, we won't be |
| 2191 | * seeing this 'insn_idx' instruction again on the way to bpf_exit |
| 2192 | */ |
| 2193 | new_sl = kmalloc(sizeof(struct verifier_state_list), GFP_USER); |
| 2194 | if (!new_sl) |
| 2195 | return -ENOMEM; |
| 2196 | |
| 2197 | /* add new state to the head of linked list */ |
| 2198 | memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state)); |
| 2199 | new_sl->next = env->explored_states[insn_idx]; |
| 2200 | env->explored_states[insn_idx] = new_sl; |
| 2201 | return 0; |
| 2202 | } |
| 2203 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2204 | static int do_check(struct verifier_env *env) |
| 2205 | { |
| 2206 | struct verifier_state *state = &env->cur_state; |
| 2207 | struct bpf_insn *insns = env->prog->insnsi; |
| 2208 | struct reg_state *regs = state->regs; |
| 2209 | int insn_cnt = env->prog->len; |
| 2210 | int insn_idx, prev_insn_idx = 0; |
| 2211 | int insn_processed = 0; |
| 2212 | bool do_print_state = false; |
| 2213 | |
| 2214 | init_reg_state(regs); |
| 2215 | insn_idx = 0; |
| 2216 | for (;;) { |
| 2217 | struct bpf_insn *insn; |
| 2218 | u8 class; |
| 2219 | int err; |
| 2220 | |
| 2221 | if (insn_idx >= insn_cnt) { |
| 2222 | verbose("invalid insn idx %d insn_cnt %d\n", |
| 2223 | insn_idx, insn_cnt); |
| 2224 | return -EFAULT; |
| 2225 | } |
| 2226 | |
| 2227 | insn = &insns[insn_idx]; |
| 2228 | class = BPF_CLASS(insn->code); |
| 2229 | |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 2230 | if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2231 | verbose("BPF program is too large. Proccessed %d insn\n", |
| 2232 | insn_processed); |
| 2233 | return -E2BIG; |
| 2234 | } |
| 2235 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2236 | err = is_state_visited(env, insn_idx); |
| 2237 | if (err < 0) |
| 2238 | return err; |
| 2239 | if (err == 1) { |
| 2240 | /* found equivalent state, can prune the search */ |
| 2241 | if (log_level) { |
| 2242 | if (do_print_state) |
| 2243 | verbose("\nfrom %d to %d: safe\n", |
| 2244 | prev_insn_idx, insn_idx); |
| 2245 | else |
| 2246 | verbose("%d: safe\n", insn_idx); |
| 2247 | } |
| 2248 | goto process_bpf_exit; |
| 2249 | } |
| 2250 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2251 | if (log_level && do_print_state) { |
| 2252 | verbose("\nfrom %d to %d:", prev_insn_idx, insn_idx); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2253 | print_verifier_state(&env->cur_state); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2254 | do_print_state = false; |
| 2255 | } |
| 2256 | |
| 2257 | if (log_level) { |
| 2258 | verbose("%d: ", insn_idx); |
| 2259 | print_bpf_insn(insn); |
| 2260 | } |
| 2261 | |
| 2262 | if (class == BPF_ALU || class == BPF_ALU64) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2263 | err = check_alu_op(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2264 | if (err) |
| 2265 | return err; |
| 2266 | |
| 2267 | } else if (class == BPF_LDX) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2268 | enum bpf_reg_type src_reg_type; |
| 2269 | |
| 2270 | /* check for reserved fields is already done */ |
| 2271 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2272 | /* check src operand */ |
| 2273 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 2274 | if (err) |
| 2275 | return err; |
| 2276 | |
| 2277 | err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK); |
| 2278 | if (err) |
| 2279 | return err; |
| 2280 | |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 2281 | src_reg_type = regs[insn->src_reg].type; |
| 2282 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2283 | /* check that memory (src_reg + off) is readable, |
| 2284 | * the state of dst_reg will be updated by this func |
| 2285 | */ |
| 2286 | err = check_mem_access(env, insn->src_reg, insn->off, |
| 2287 | BPF_SIZE(insn->code), BPF_READ, |
| 2288 | insn->dst_reg); |
| 2289 | if (err) |
| 2290 | return err; |
| 2291 | |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 2292 | if (BPF_SIZE(insn->code) != BPF_W) { |
| 2293 | insn_idx++; |
| 2294 | continue; |
| 2295 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2296 | |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 2297 | if (insn->imm == 0) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2298 | /* saw a valid insn |
| 2299 | * dst_reg = *(u32 *)(src_reg + off) |
| 2300 | * use reserved 'imm' field to mark this insn |
| 2301 | */ |
| 2302 | insn->imm = src_reg_type; |
| 2303 | |
| 2304 | } else if (src_reg_type != insn->imm && |
| 2305 | (src_reg_type == PTR_TO_CTX || |
| 2306 | insn->imm == PTR_TO_CTX)) { |
| 2307 | /* ABuser program is trying to use the same insn |
| 2308 | * dst_reg = *(u32*) (src_reg + off) |
| 2309 | * with different pointer types: |
| 2310 | * src_reg == ctx in one branch and |
| 2311 | * src_reg == stack|map in some other branch. |
| 2312 | * Reject it. |
| 2313 | */ |
| 2314 | verbose("same insn cannot be used with different pointers\n"); |
| 2315 | return -EINVAL; |
| 2316 | } |
| 2317 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2318 | } else if (class == BPF_STX) { |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2319 | enum bpf_reg_type dst_reg_type; |
| 2320 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2321 | if (BPF_MODE(insn->code) == BPF_XADD) { |
| 2322 | err = check_xadd(env, insn); |
| 2323 | if (err) |
| 2324 | return err; |
| 2325 | insn_idx++; |
| 2326 | continue; |
| 2327 | } |
| 2328 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2329 | /* check src1 operand */ |
| 2330 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 2331 | if (err) |
| 2332 | return err; |
| 2333 | /* check src2 operand */ |
| 2334 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 2335 | if (err) |
| 2336 | return err; |
| 2337 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2338 | dst_reg_type = regs[insn->dst_reg].type; |
| 2339 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2340 | /* check that memory (dst_reg + off) is writeable */ |
| 2341 | err = check_mem_access(env, insn->dst_reg, insn->off, |
| 2342 | BPF_SIZE(insn->code), BPF_WRITE, |
| 2343 | insn->src_reg); |
| 2344 | if (err) |
| 2345 | return err; |
| 2346 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2347 | if (insn->imm == 0) { |
| 2348 | insn->imm = dst_reg_type; |
| 2349 | } else if (dst_reg_type != insn->imm && |
| 2350 | (dst_reg_type == PTR_TO_CTX || |
| 2351 | insn->imm == PTR_TO_CTX)) { |
| 2352 | verbose("same insn cannot be used with different pointers\n"); |
| 2353 | return -EINVAL; |
| 2354 | } |
| 2355 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2356 | } else if (class == BPF_ST) { |
| 2357 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 2358 | insn->src_reg != BPF_REG_0) { |
| 2359 | verbose("BPF_ST uses reserved fields\n"); |
| 2360 | return -EINVAL; |
| 2361 | } |
| 2362 | /* check src operand */ |
| 2363 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 2364 | if (err) |
| 2365 | return err; |
| 2366 | |
| 2367 | /* check that memory (dst_reg + off) is writeable */ |
| 2368 | err = check_mem_access(env, insn->dst_reg, insn->off, |
| 2369 | BPF_SIZE(insn->code), BPF_WRITE, |
| 2370 | -1); |
| 2371 | if (err) |
| 2372 | return err; |
| 2373 | |
| 2374 | } else if (class == BPF_JMP) { |
| 2375 | u8 opcode = BPF_OP(insn->code); |
| 2376 | |
| 2377 | if (opcode == BPF_CALL) { |
| 2378 | if (BPF_SRC(insn->code) != BPF_K || |
| 2379 | insn->off != 0 || |
| 2380 | insn->src_reg != BPF_REG_0 || |
| 2381 | insn->dst_reg != BPF_REG_0) { |
| 2382 | verbose("BPF_CALL uses reserved fields\n"); |
| 2383 | return -EINVAL; |
| 2384 | } |
| 2385 | |
| 2386 | err = check_call(env, insn->imm); |
| 2387 | if (err) |
| 2388 | return err; |
| 2389 | |
| 2390 | } else if (opcode == BPF_JA) { |
| 2391 | if (BPF_SRC(insn->code) != BPF_K || |
| 2392 | insn->imm != 0 || |
| 2393 | insn->src_reg != BPF_REG_0 || |
| 2394 | insn->dst_reg != BPF_REG_0) { |
| 2395 | verbose("BPF_JA uses reserved fields\n"); |
| 2396 | return -EINVAL; |
| 2397 | } |
| 2398 | |
| 2399 | insn_idx += insn->off + 1; |
| 2400 | continue; |
| 2401 | |
| 2402 | } else if (opcode == BPF_EXIT) { |
| 2403 | if (BPF_SRC(insn->code) != BPF_K || |
| 2404 | insn->imm != 0 || |
| 2405 | insn->src_reg != BPF_REG_0 || |
| 2406 | insn->dst_reg != BPF_REG_0) { |
| 2407 | verbose("BPF_EXIT uses reserved fields\n"); |
| 2408 | return -EINVAL; |
| 2409 | } |
| 2410 | |
| 2411 | /* eBPF calling convetion is such that R0 is used |
| 2412 | * to return the value from eBPF program. |
| 2413 | * Make sure that it's readable at this time |
| 2414 | * of bpf_exit, which means that program wrote |
| 2415 | * something into it earlier |
| 2416 | */ |
| 2417 | err = check_reg_arg(regs, BPF_REG_0, SRC_OP); |
| 2418 | if (err) |
| 2419 | return err; |
| 2420 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2421 | if (is_pointer_value(env, BPF_REG_0)) { |
| 2422 | verbose("R0 leaks addr as return value\n"); |
| 2423 | return -EACCES; |
| 2424 | } |
| 2425 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2426 | process_bpf_exit: |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2427 | insn_idx = pop_stack(env, &prev_insn_idx); |
| 2428 | if (insn_idx < 0) { |
| 2429 | break; |
| 2430 | } else { |
| 2431 | do_print_state = true; |
| 2432 | continue; |
| 2433 | } |
| 2434 | } else { |
| 2435 | err = check_cond_jmp_op(env, insn, &insn_idx); |
| 2436 | if (err) |
| 2437 | return err; |
| 2438 | } |
| 2439 | } else if (class == BPF_LD) { |
| 2440 | u8 mode = BPF_MODE(insn->code); |
| 2441 | |
| 2442 | if (mode == BPF_ABS || mode == BPF_IND) { |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 2443 | err = check_ld_abs(env, insn); |
| 2444 | if (err) |
| 2445 | return err; |
| 2446 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2447 | } else if (mode == BPF_IMM) { |
| 2448 | err = check_ld_imm(env, insn); |
| 2449 | if (err) |
| 2450 | return err; |
| 2451 | |
| 2452 | insn_idx++; |
| 2453 | } else { |
| 2454 | verbose("invalid BPF_LD mode\n"); |
| 2455 | return -EINVAL; |
| 2456 | } |
| 2457 | } else { |
| 2458 | verbose("unknown insn class %d\n", class); |
| 2459 | return -EINVAL; |
| 2460 | } |
| 2461 | |
| 2462 | insn_idx++; |
| 2463 | } |
| 2464 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2465 | verbose("processed %d insns\n", insn_processed); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2466 | return 0; |
| 2467 | } |
| 2468 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2469 | /* look for pseudo eBPF instructions that access map FDs and |
| 2470 | * replace them with actual map pointers |
| 2471 | */ |
| 2472 | static int replace_map_fd_with_map_ptr(struct verifier_env *env) |
| 2473 | { |
| 2474 | struct bpf_insn *insn = env->prog->insnsi; |
| 2475 | int insn_cnt = env->prog->len; |
| 2476 | int i, j; |
| 2477 | |
| 2478 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2479 | if (BPF_CLASS(insn->code) == BPF_LDX && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2480 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2481 | verbose("BPF_LDX uses reserved fields\n"); |
| 2482 | return -EINVAL; |
| 2483 | } |
| 2484 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2485 | if (BPF_CLASS(insn->code) == BPF_STX && |
| 2486 | ((BPF_MODE(insn->code) != BPF_MEM && |
| 2487 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { |
| 2488 | verbose("BPF_STX uses reserved fields\n"); |
| 2489 | return -EINVAL; |
| 2490 | } |
| 2491 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2492 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
| 2493 | struct bpf_map *map; |
| 2494 | struct fd f; |
| 2495 | |
| 2496 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 2497 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 2498 | insn[1].off != 0) { |
| 2499 | verbose("invalid bpf_ld_imm64 insn\n"); |
| 2500 | return -EINVAL; |
| 2501 | } |
| 2502 | |
| 2503 | if (insn->src_reg == 0) |
| 2504 | /* valid generic load 64-bit imm */ |
| 2505 | goto next_insn; |
| 2506 | |
| 2507 | if (insn->src_reg != BPF_PSEUDO_MAP_FD) { |
| 2508 | verbose("unrecognized bpf_ld_imm64 insn\n"); |
| 2509 | return -EINVAL; |
| 2510 | } |
| 2511 | |
| 2512 | f = fdget(insn->imm); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 2513 | map = __bpf_map_get(f); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2514 | if (IS_ERR(map)) { |
| 2515 | verbose("fd %d is not pointing to valid bpf_map\n", |
| 2516 | insn->imm); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2517 | return PTR_ERR(map); |
| 2518 | } |
| 2519 | |
| 2520 | /* store map pointer inside BPF_LD_IMM64 instruction */ |
| 2521 | insn[0].imm = (u32) (unsigned long) map; |
| 2522 | insn[1].imm = ((u64) (unsigned long) map) >> 32; |
| 2523 | |
| 2524 | /* check whether we recorded this map already */ |
| 2525 | for (j = 0; j < env->used_map_cnt; j++) |
| 2526 | if (env->used_maps[j] == map) { |
| 2527 | fdput(f); |
| 2528 | goto next_insn; |
| 2529 | } |
| 2530 | |
| 2531 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 2532 | fdput(f); |
| 2533 | return -E2BIG; |
| 2534 | } |
| 2535 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2536 | /* hold the map. If the program is rejected by verifier, |
| 2537 | * the map will be released by release_maps() or it |
| 2538 | * will be used by the valid program until it's unloaded |
| 2539 | * and all maps are released in free_bpf_prog_info() |
| 2540 | */ |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 2541 | map = bpf_map_inc(map, false); |
| 2542 | if (IS_ERR(map)) { |
| 2543 | fdput(f); |
| 2544 | return PTR_ERR(map); |
| 2545 | } |
| 2546 | env->used_maps[env->used_map_cnt++] = map; |
| 2547 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2548 | fdput(f); |
| 2549 | next_insn: |
| 2550 | insn++; |
| 2551 | i++; |
| 2552 | } |
| 2553 | } |
| 2554 | |
| 2555 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 2556 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 2557 | * These pointers will be used later by verifier to validate map access. |
| 2558 | */ |
| 2559 | return 0; |
| 2560 | } |
| 2561 | |
| 2562 | /* drop refcnt of maps used by the rejected program */ |
| 2563 | static void release_maps(struct verifier_env *env) |
| 2564 | { |
| 2565 | int i; |
| 2566 | |
| 2567 | for (i = 0; i < env->used_map_cnt; i++) |
| 2568 | bpf_map_put(env->used_maps[i]); |
| 2569 | } |
| 2570 | |
| 2571 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
| 2572 | static void convert_pseudo_ld_imm64(struct verifier_env *env) |
| 2573 | { |
| 2574 | struct bpf_insn *insn = env->prog->insnsi; |
| 2575 | int insn_cnt = env->prog->len; |
| 2576 | int i; |
| 2577 | |
| 2578 | for (i = 0; i < insn_cnt; i++, insn++) |
| 2579 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) |
| 2580 | insn->src_reg = 0; |
| 2581 | } |
| 2582 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2583 | /* convert load instructions that access fields of 'struct __sk_buff' |
| 2584 | * into sequence of instructions that access fields of 'struct sk_buff' |
| 2585 | */ |
| 2586 | static int convert_ctx_accesses(struct verifier_env *env) |
| 2587 | { |
| 2588 | struct bpf_insn *insn = env->prog->insnsi; |
| 2589 | int insn_cnt = env->prog->len; |
| 2590 | struct bpf_insn insn_buf[16]; |
| 2591 | struct bpf_prog *new_prog; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2592 | enum bpf_access_type type; |
Daniel Borkmann | c237ee5 | 2016-05-13 19:08:30 +0200 | [diff] [blame] | 2593 | int i; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2594 | |
| 2595 | if (!env->prog->aux->ops->convert_ctx_access) |
| 2596 | return 0; |
| 2597 | |
| 2598 | for (i = 0; i < insn_cnt; i++, insn++) { |
Daniel Borkmann | c237ee5 | 2016-05-13 19:08:30 +0200 | [diff] [blame] | 2599 | u32 insn_delta, cnt; |
| 2600 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2601 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_W)) |
| 2602 | type = BPF_READ; |
| 2603 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_W)) |
| 2604 | type = BPF_WRITE; |
| 2605 | else |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2606 | continue; |
| 2607 | |
| 2608 | if (insn->imm != PTR_TO_CTX) { |
| 2609 | /* clear internal mark */ |
| 2610 | insn->imm = 0; |
| 2611 | continue; |
| 2612 | } |
| 2613 | |
| 2614 | cnt = env->prog->aux->ops-> |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2615 | convert_ctx_access(type, insn->dst_reg, insn->src_reg, |
Alexei Starovoitov | ff936a0 | 2015-10-07 10:55:41 -0700 | [diff] [blame] | 2616 | insn->off, insn_buf, env->prog); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2617 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 2618 | verbose("bpf verifier is misconfigured\n"); |
| 2619 | return -EINVAL; |
| 2620 | } |
| 2621 | |
Daniel Borkmann | c237ee5 | 2016-05-13 19:08:30 +0200 | [diff] [blame] | 2622 | new_prog = bpf_patch_insn_single(env->prog, i, insn_buf, cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2623 | if (!new_prog) |
| 2624 | return -ENOMEM; |
| 2625 | |
Daniel Borkmann | c237ee5 | 2016-05-13 19:08:30 +0200 | [diff] [blame] | 2626 | insn_delta = cnt - 1; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2627 | |
| 2628 | /* keep walking new program and skip insns we just inserted */ |
| 2629 | env->prog = new_prog; |
Daniel Borkmann | c237ee5 | 2016-05-13 19:08:30 +0200 | [diff] [blame] | 2630 | insn = new_prog->insnsi + i + insn_delta; |
| 2631 | |
| 2632 | insn_cnt += insn_delta; |
| 2633 | i += insn_delta; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2634 | } |
| 2635 | |
| 2636 | return 0; |
| 2637 | } |
| 2638 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2639 | static void free_states(struct verifier_env *env) |
| 2640 | { |
| 2641 | struct verifier_state_list *sl, *sln; |
| 2642 | int i; |
| 2643 | |
| 2644 | if (!env->explored_states) |
| 2645 | return; |
| 2646 | |
| 2647 | for (i = 0; i < env->prog->len; i++) { |
| 2648 | sl = env->explored_states[i]; |
| 2649 | |
| 2650 | if (sl) |
| 2651 | while (sl != STATE_LIST_MARK) { |
| 2652 | sln = sl->next; |
| 2653 | kfree(sl); |
| 2654 | sl = sln; |
| 2655 | } |
| 2656 | } |
| 2657 | |
| 2658 | kfree(env->explored_states); |
| 2659 | } |
| 2660 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2661 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 2662 | { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 2663 | char __user *log_ubuf = NULL; |
| 2664 | struct verifier_env *env; |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 2665 | int ret = -EINVAL; |
| 2666 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2667 | if ((*prog)->len <= 0 || (*prog)->len > BPF_MAXINSNS) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 2668 | return -E2BIG; |
| 2669 | |
| 2670 | /* 'struct verifier_env' can be global, but since it's not small, |
| 2671 | * allocate/free it every time bpf_check() is called |
| 2672 | */ |
| 2673 | env = kzalloc(sizeof(struct verifier_env), GFP_KERNEL); |
| 2674 | if (!env) |
| 2675 | return -ENOMEM; |
| 2676 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2677 | env->prog = *prog; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2678 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 2679 | /* grab the mutex to protect few globals used by verifier */ |
| 2680 | mutex_lock(&bpf_verifier_lock); |
| 2681 | |
| 2682 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 2683 | /* user requested verbose verifier output |
| 2684 | * and supplied buffer to store the verification trace |
| 2685 | */ |
| 2686 | log_level = attr->log_level; |
| 2687 | log_ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 2688 | log_size = attr->log_size; |
| 2689 | log_len = 0; |
| 2690 | |
| 2691 | ret = -EINVAL; |
| 2692 | /* log_* values have to be sane */ |
| 2693 | if (log_size < 128 || log_size > UINT_MAX >> 8 || |
| 2694 | log_level == 0 || log_ubuf == NULL) |
| 2695 | goto free_env; |
| 2696 | |
| 2697 | ret = -ENOMEM; |
| 2698 | log_buf = vmalloc(log_size); |
| 2699 | if (!log_buf) |
| 2700 | goto free_env; |
| 2701 | } else { |
| 2702 | log_level = 0; |
| 2703 | } |
| 2704 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2705 | ret = replace_map_fd_with_map_ptr(env); |
| 2706 | if (ret < 0) |
| 2707 | goto skip_full_check; |
| 2708 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2709 | env->explored_states = kcalloc(env->prog->len, |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2710 | sizeof(struct verifier_state_list *), |
| 2711 | GFP_USER); |
| 2712 | ret = -ENOMEM; |
| 2713 | if (!env->explored_states) |
| 2714 | goto skip_full_check; |
| 2715 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 2716 | ret = check_cfg(env); |
| 2717 | if (ret < 0) |
| 2718 | goto skip_full_check; |
| 2719 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2720 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); |
| 2721 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2722 | ret = do_check(env); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 2723 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2724 | skip_full_check: |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2725 | while (pop_stack(env, NULL) >= 0); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2726 | free_states(env); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2727 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2728 | if (ret == 0) |
| 2729 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 2730 | ret = convert_ctx_accesses(env); |
| 2731 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 2732 | if (log_level && log_len >= log_size - 1) { |
| 2733 | BUG_ON(log_len >= log_size); |
| 2734 | /* verifier log exceeded user supplied buffer */ |
| 2735 | ret = -ENOSPC; |
| 2736 | /* fall through to return what was recorded */ |
| 2737 | } |
| 2738 | |
| 2739 | /* copy verifier log back to user space including trailing zero */ |
| 2740 | if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) { |
| 2741 | ret = -EFAULT; |
| 2742 | goto free_log_buf; |
| 2743 | } |
| 2744 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2745 | if (ret == 0 && env->used_map_cnt) { |
| 2746 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2747 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 2748 | sizeof(env->used_maps[0]), |
| 2749 | GFP_KERNEL); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2750 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2751 | if (!env->prog->aux->used_maps) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2752 | ret = -ENOMEM; |
| 2753 | goto free_log_buf; |
| 2754 | } |
| 2755 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2756 | memcpy(env->prog->aux->used_maps, env->used_maps, |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2757 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2758 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2759 | |
| 2760 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 2761 | * bpf_ld_imm64 instructions |
| 2762 | */ |
| 2763 | convert_pseudo_ld_imm64(env); |
| 2764 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 2765 | |
| 2766 | free_log_buf: |
| 2767 | if (log_level) |
| 2768 | vfree(log_buf); |
| 2769 | free_env: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2770 | if (!env->prog->aux->used_maps) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2771 | /* if we didn't copy map pointers into bpf_prog_info, release |
| 2772 | * them now. Otherwise free_bpf_prog_info() will release them. |
| 2773 | */ |
| 2774 | release_maps(env); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2775 | *prog = env->prog; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 2776 | kfree(env); |
| 2777 | mutex_unlock(&bpf_verifier_lock); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 2778 | return ret; |
| 2779 | } |