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> |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 17 | #include <linux/bpf_verifier.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 18 | #include <linux/filter.h> |
| 19 | #include <net/netlink.h> |
| 20 | #include <linux/file.h> |
| 21 | #include <linux/vmalloc.h> |
| 22 | |
| 23 | /* bpf_check() is a static code analyzer that walks eBPF program |
| 24 | * instruction by instruction and updates register/stack state. |
| 25 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. |
| 26 | * |
| 27 | * The first pass is depth-first-search to check that the program is a DAG. |
| 28 | * It rejects the following programs: |
| 29 | * - larger than BPF_MAXINSNS insns |
| 30 | * - if loop is present (detected via back-edge) |
| 31 | * - unreachable insns exist (shouldn't be a forest. program = one function) |
| 32 | * - out of bounds or malformed jumps |
| 33 | * The second pass is all possible path descent from the 1st insn. |
| 34 | * Since it's analyzing all pathes through the program, the length of the |
| 35 | * analysis is limited to 32k insn, which may be hit even if total number of |
| 36 | * insn is less then 4K, but there are too many branches that change stack/regs. |
| 37 | * Number of 'branches to be analyzed' is limited to 1k |
| 38 | * |
| 39 | * On entry to each instruction, each register has a type, and the instruction |
| 40 | * changes the types of the registers depending on instruction semantics. |
| 41 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is |
| 42 | * copied to R1. |
| 43 | * |
| 44 | * All registers are 64-bit. |
| 45 | * R0 - return register |
| 46 | * R1-R5 argument passing registers |
| 47 | * R6-R9 callee saved registers |
| 48 | * R10 - frame pointer read-only |
| 49 | * |
| 50 | * At the start of BPF program the register R1 contains a pointer to bpf_context |
| 51 | * and has type PTR_TO_CTX. |
| 52 | * |
| 53 | * Verifier tracks arithmetic operations on pointers in case: |
| 54 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), |
| 55 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), |
| 56 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 |
| 57 | * and 2nd arithmetic instruction is pattern matched to recognize |
| 58 | * that it wants to construct a pointer to some element within stack. |
| 59 | * So after 2nd insn, the register R1 has type PTR_TO_STACK |
| 60 | * (and -20 constant is saved for further stack bounds checking). |
| 61 | * Meaning that this reg is a pointer to stack plus known immediate constant. |
| 62 | * |
| 63 | * Most of the time the registers have UNKNOWN_VALUE type, which |
| 64 | * means the register has some value, but it's not a valid pointer. |
| 65 | * (like pointer plus pointer becomes UNKNOWN_VALUE type) |
| 66 | * |
| 67 | * When verifier sees load or store instructions the type of base register |
| 68 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, FRAME_PTR. These are three pointer |
| 69 | * types recognized by check_mem_access() function. |
| 70 | * |
| 71 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' |
| 72 | * and the range of [ptr, ptr + map's value_size) is accessible. |
| 73 | * |
| 74 | * registers used to pass values to function calls are checked against |
| 75 | * function argument constraints. |
| 76 | * |
| 77 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. |
| 78 | * It means that the register type passed to this function must be |
| 79 | * PTR_TO_STACK and it will be used inside the function as |
| 80 | * 'pointer to map element key' |
| 81 | * |
| 82 | * For example the argument constraints for bpf_map_lookup_elem(): |
| 83 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, |
| 84 | * .arg1_type = ARG_CONST_MAP_PTR, |
| 85 | * .arg2_type = ARG_PTR_TO_MAP_KEY, |
| 86 | * |
| 87 | * ret_type says that this function returns 'pointer to map elem value or null' |
| 88 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and |
| 89 | * 2nd argument should be a pointer to stack, which will be used inside |
| 90 | * the helper function as a pointer to map element key. |
| 91 | * |
| 92 | * On the kernel side the helper function looks like: |
| 93 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 94 | * { |
| 95 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; |
| 96 | * void *key = (void *) (unsigned long) r2; |
| 97 | * void *value; |
| 98 | * |
| 99 | * here kernel can access 'key' and 'map' pointers safely, knowing that |
| 100 | * [key, key + map->key_size) bytes are valid and were initialized on |
| 101 | * the stack of eBPF program. |
| 102 | * } |
| 103 | * |
| 104 | * Corresponding eBPF program may look like: |
| 105 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR |
| 106 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK |
| 107 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP |
| 108 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), |
| 109 | * here verifier looks at prototype of map_lookup_elem() and sees: |
| 110 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, |
| 111 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes |
| 112 | * |
| 113 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, |
| 114 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits |
| 115 | * and were initialized prior to this call. |
| 116 | * If it's ok, then verifier allows this BPF_CALL insn and looks at |
| 117 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets |
| 118 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function |
| 119 | * returns ether pointer to map value or NULL. |
| 120 | * |
| 121 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' |
| 122 | * insn, the register holding that pointer in the true branch changes state to |
| 123 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false |
| 124 | * branch. See check_cond_jmp_op(). |
| 125 | * |
| 126 | * After the call R0 is set to return type of the function and registers R1-R5 |
| 127 | * are set to NOT_INIT to indicate that they are no longer readable. |
| 128 | */ |
| 129 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 130 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 131 | struct bpf_verifier_stack_elem { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 132 | /* verifer state is 'st' |
| 133 | * before processing instruction 'insn_idx' |
| 134 | * and after processing instruction 'prev_insn_idx' |
| 135 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 136 | struct bpf_verifier_state st; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 137 | int insn_idx; |
| 138 | int prev_insn_idx; |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 139 | struct bpf_verifier_stack_elem *next; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
Daniel Borkmann | 577aa83 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 142 | #define BPF_COMPLEXITY_LIMIT_INSNS 98304 |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 143 | #define BPF_COMPLEXITY_LIMIT_STACK 1024 |
| 144 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 145 | struct bpf_call_arg_meta { |
| 146 | struct bpf_map *map_ptr; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 147 | bool raw_mode; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 148 | bool pkt_access; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 149 | int regno; |
| 150 | int access_size; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 151 | }; |
| 152 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 153 | /* verbose verifier prints what it's seeing |
| 154 | * bpf_check() is called under lock, so no race to access these global vars |
| 155 | */ |
| 156 | static u32 log_level, log_size, log_len; |
| 157 | static char *log_buf; |
| 158 | |
| 159 | static DEFINE_MUTEX(bpf_verifier_lock); |
| 160 | |
| 161 | /* log_level controls verbosity level of eBPF verifier. |
| 162 | * verbose() is used to dump the verification trace to the log, so the user |
| 163 | * can figure out what's wrong with the program |
| 164 | */ |
Daniel Borkmann | 1d056d9 | 2015-11-03 11:39:20 +0100 | [diff] [blame] | 165 | static __printf(1, 2) void verbose(const char *fmt, ...) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 166 | { |
| 167 | va_list args; |
| 168 | |
| 169 | if (log_level == 0 || log_len >= log_size - 1) |
| 170 | return; |
| 171 | |
| 172 | va_start(args, fmt); |
| 173 | log_len += vscnprintf(log_buf + log_len, log_size - log_len, fmt, args); |
| 174 | va_end(args); |
| 175 | } |
| 176 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 177 | /* string representation of 'enum bpf_reg_type' */ |
| 178 | static const char * const reg_type_str[] = { |
| 179 | [NOT_INIT] = "?", |
| 180 | [UNKNOWN_VALUE] = "inv", |
| 181 | [PTR_TO_CTX] = "ctx", |
| 182 | [CONST_PTR_TO_MAP] = "map_ptr", |
| 183 | [PTR_TO_MAP_VALUE] = "map_value", |
| 184 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 185 | [PTR_TO_MAP_VALUE_ADJ] = "map_value_adj", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 186 | [FRAME_PTR] = "fp", |
| 187 | [PTR_TO_STACK] = "fp", |
| 188 | [CONST_IMM] = "imm", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 189 | [PTR_TO_PACKET] = "pkt", |
| 190 | [PTR_TO_PACKET_END] = "pkt_end", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 193 | static void print_verifier_state(struct bpf_verifier_state *state) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 194 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 195 | struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 196 | enum bpf_reg_type t; |
| 197 | int i; |
| 198 | |
| 199 | for (i = 0; i < MAX_BPF_REG; i++) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 200 | reg = &state->regs[i]; |
| 201 | t = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 202 | if (t == NOT_INIT) |
| 203 | continue; |
| 204 | verbose(" R%d=%s", i, reg_type_str[t]); |
| 205 | if (t == CONST_IMM || t == PTR_TO_STACK) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 206 | verbose("%lld", reg->imm); |
| 207 | else if (t == PTR_TO_PACKET) |
| 208 | verbose("(id=%d,off=%d,r=%d)", |
| 209 | reg->id, reg->off, reg->range); |
| 210 | else if (t == UNKNOWN_VALUE && reg->imm) |
| 211 | verbose("%lld", reg->imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 212 | else if (t == CONST_PTR_TO_MAP || t == PTR_TO_MAP_VALUE || |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 213 | t == PTR_TO_MAP_VALUE_OR_NULL || |
| 214 | t == PTR_TO_MAP_VALUE_ADJ) |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 215 | verbose("(ks=%d,vs=%d,id=%u)", |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 216 | reg->map_ptr->key_size, |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 217 | reg->map_ptr->value_size, |
| 218 | reg->id); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 219 | if (reg->min_value != BPF_REGISTER_MIN_RANGE) |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 220 | verbose(",min_value=%lld", |
| 221 | (long long)reg->min_value); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 222 | if (reg->max_value != BPF_REGISTER_MAX_RANGE) |
| 223 | verbose(",max_value=%llu", |
| 224 | (unsigned long long)reg->max_value); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 225 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 226 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 227 | if (state->stack_slot_type[i] == STACK_SPILL) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 228 | verbose(" fp%d=%s", -MAX_BPF_STACK + i, |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 229 | reg_type_str[state->spilled_regs[i / BPF_REG_SIZE].type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 230 | } |
| 231 | verbose("\n"); |
| 232 | } |
| 233 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 234 | static const char *const bpf_class_string[] = { |
| 235 | [BPF_LD] = "ld", |
| 236 | [BPF_LDX] = "ldx", |
| 237 | [BPF_ST] = "st", |
| 238 | [BPF_STX] = "stx", |
| 239 | [BPF_ALU] = "alu", |
| 240 | [BPF_JMP] = "jmp", |
| 241 | [BPF_RET] = "BUG", |
| 242 | [BPF_ALU64] = "alu64", |
| 243 | }; |
| 244 | |
Alexei Starovoitov | 687f071 | 2015-09-08 13:40:01 -0700 | [diff] [blame] | 245 | static const char *const bpf_alu_string[16] = { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 246 | [BPF_ADD >> 4] = "+=", |
| 247 | [BPF_SUB >> 4] = "-=", |
| 248 | [BPF_MUL >> 4] = "*=", |
| 249 | [BPF_DIV >> 4] = "/=", |
| 250 | [BPF_OR >> 4] = "|=", |
| 251 | [BPF_AND >> 4] = "&=", |
| 252 | [BPF_LSH >> 4] = "<<=", |
| 253 | [BPF_RSH >> 4] = ">>=", |
| 254 | [BPF_NEG >> 4] = "neg", |
| 255 | [BPF_MOD >> 4] = "%=", |
| 256 | [BPF_XOR >> 4] = "^=", |
| 257 | [BPF_MOV >> 4] = "=", |
| 258 | [BPF_ARSH >> 4] = "s>>=", |
| 259 | [BPF_END >> 4] = "endian", |
| 260 | }; |
| 261 | |
| 262 | static const char *const bpf_ldst_string[] = { |
| 263 | [BPF_W >> 3] = "u32", |
| 264 | [BPF_H >> 3] = "u16", |
| 265 | [BPF_B >> 3] = "u8", |
| 266 | [BPF_DW >> 3] = "u64", |
| 267 | }; |
| 268 | |
Alexei Starovoitov | 687f071 | 2015-09-08 13:40:01 -0700 | [diff] [blame] | 269 | static const char *const bpf_jmp_string[16] = { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 270 | [BPF_JA >> 4] = "jmp", |
| 271 | [BPF_JEQ >> 4] = "==", |
| 272 | [BPF_JGT >> 4] = ">", |
| 273 | [BPF_JGE >> 4] = ">=", |
| 274 | [BPF_JSET >> 4] = "&", |
| 275 | [BPF_JNE >> 4] = "!=", |
| 276 | [BPF_JSGT >> 4] = "s>", |
| 277 | [BPF_JSGE >> 4] = "s>=", |
| 278 | [BPF_CALL >> 4] = "call", |
| 279 | [BPF_EXIT >> 4] = "exit", |
| 280 | }; |
| 281 | |
Daniel Borkmann | ced0a31 | 2017-05-08 00:04:09 +0200 | [diff] [blame] | 282 | static void print_bpf_insn(const struct bpf_verifier_env *env, |
| 283 | const struct bpf_insn *insn) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 284 | { |
| 285 | u8 class = BPF_CLASS(insn->code); |
| 286 | |
| 287 | if (class == BPF_ALU || class == BPF_ALU64) { |
| 288 | if (BPF_SRC(insn->code) == BPF_X) |
| 289 | verbose("(%02x) %sr%d %s %sr%d\n", |
| 290 | insn->code, class == BPF_ALU ? "(u32) " : "", |
| 291 | insn->dst_reg, |
| 292 | bpf_alu_string[BPF_OP(insn->code) >> 4], |
| 293 | class == BPF_ALU ? "(u32) " : "", |
| 294 | insn->src_reg); |
| 295 | else |
| 296 | verbose("(%02x) %sr%d %s %s%d\n", |
| 297 | insn->code, class == BPF_ALU ? "(u32) " : "", |
| 298 | insn->dst_reg, |
| 299 | bpf_alu_string[BPF_OP(insn->code) >> 4], |
| 300 | class == BPF_ALU ? "(u32) " : "", |
| 301 | insn->imm); |
| 302 | } else if (class == BPF_STX) { |
| 303 | if (BPF_MODE(insn->code) == BPF_MEM) |
| 304 | verbose("(%02x) *(%s *)(r%d %+d) = r%d\n", |
| 305 | insn->code, |
| 306 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 307 | insn->dst_reg, |
| 308 | insn->off, insn->src_reg); |
| 309 | else if (BPF_MODE(insn->code) == BPF_XADD) |
| 310 | verbose("(%02x) lock *(%s *)(r%d %+d) += r%d\n", |
| 311 | insn->code, |
| 312 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 313 | insn->dst_reg, insn->off, |
| 314 | insn->src_reg); |
| 315 | else |
| 316 | verbose("BUG_%02x\n", insn->code); |
| 317 | } else if (class == BPF_ST) { |
| 318 | if (BPF_MODE(insn->code) != BPF_MEM) { |
| 319 | verbose("BUG_st_%02x\n", insn->code); |
| 320 | return; |
| 321 | } |
| 322 | verbose("(%02x) *(%s *)(r%d %+d) = %d\n", |
| 323 | insn->code, |
| 324 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 325 | insn->dst_reg, |
| 326 | insn->off, insn->imm); |
| 327 | } else if (class == BPF_LDX) { |
| 328 | if (BPF_MODE(insn->code) != BPF_MEM) { |
| 329 | verbose("BUG_ldx_%02x\n", insn->code); |
| 330 | return; |
| 331 | } |
| 332 | verbose("(%02x) r%d = *(%s *)(r%d %+d)\n", |
| 333 | insn->code, insn->dst_reg, |
| 334 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 335 | insn->src_reg, insn->off); |
| 336 | } else if (class == BPF_LD) { |
| 337 | if (BPF_MODE(insn->code) == BPF_ABS) { |
| 338 | verbose("(%02x) r0 = *(%s *)skb[%d]\n", |
| 339 | insn->code, |
| 340 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 341 | insn->imm); |
| 342 | } else if (BPF_MODE(insn->code) == BPF_IND) { |
| 343 | verbose("(%02x) r0 = *(%s *)skb[r%d + %d]\n", |
| 344 | insn->code, |
| 345 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 346 | insn->src_reg, insn->imm); |
Daniel Borkmann | ced0a31 | 2017-05-08 00:04:09 +0200 | [diff] [blame] | 347 | } else if (BPF_MODE(insn->code) == BPF_IMM && |
| 348 | BPF_SIZE(insn->code) == BPF_DW) { |
| 349 | /* At this point, we already made sure that the second |
| 350 | * part of the ldimm64 insn is accessible. |
| 351 | */ |
| 352 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 353 | bool map_ptr = insn->src_reg == BPF_PSEUDO_MAP_FD; |
| 354 | |
| 355 | if (map_ptr && !env->allow_ptr_leaks) |
| 356 | imm = 0; |
| 357 | |
| 358 | verbose("(%02x) r%d = 0x%llx\n", insn->code, |
| 359 | insn->dst_reg, (unsigned long long)imm); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 360 | } else { |
| 361 | verbose("BUG_ld_%02x\n", insn->code); |
| 362 | return; |
| 363 | } |
| 364 | } else if (class == BPF_JMP) { |
| 365 | u8 opcode = BPF_OP(insn->code); |
| 366 | |
| 367 | if (opcode == BPF_CALL) { |
| 368 | verbose("(%02x) call %d\n", insn->code, insn->imm); |
| 369 | } else if (insn->code == (BPF_JMP | BPF_JA)) { |
| 370 | verbose("(%02x) goto pc%+d\n", |
| 371 | insn->code, insn->off); |
| 372 | } else if (insn->code == (BPF_JMP | BPF_EXIT)) { |
| 373 | verbose("(%02x) exit\n", insn->code); |
| 374 | } else if (BPF_SRC(insn->code) == BPF_X) { |
| 375 | verbose("(%02x) if r%d %s r%d goto pc%+d\n", |
| 376 | insn->code, insn->dst_reg, |
| 377 | bpf_jmp_string[BPF_OP(insn->code) >> 4], |
| 378 | insn->src_reg, insn->off); |
| 379 | } else { |
| 380 | verbose("(%02x) if r%d %s 0x%x goto pc%+d\n", |
| 381 | insn->code, insn->dst_reg, |
| 382 | bpf_jmp_string[BPF_OP(insn->code) >> 4], |
| 383 | insn->imm, insn->off); |
| 384 | } |
| 385 | } else { |
| 386 | verbose("(%02x) %s\n", insn->code, bpf_class_string[class]); |
| 387 | } |
| 388 | } |
| 389 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 390 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 391 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 392 | struct bpf_verifier_stack_elem *elem; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 393 | int insn_idx; |
| 394 | |
| 395 | if (env->head == NULL) |
| 396 | return -1; |
| 397 | |
| 398 | memcpy(&env->cur_state, &env->head->st, sizeof(env->cur_state)); |
| 399 | insn_idx = env->head->insn_idx; |
| 400 | if (prev_insn_idx) |
| 401 | *prev_insn_idx = env->head->prev_insn_idx; |
| 402 | elem = env->head->next; |
| 403 | kfree(env->head); |
| 404 | env->head = elem; |
| 405 | env->stack_size--; |
| 406 | return insn_idx; |
| 407 | } |
| 408 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 409 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
| 410 | int insn_idx, int prev_insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 411 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 412 | struct bpf_verifier_stack_elem *elem; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 413 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 414 | elem = kmalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 415 | if (!elem) |
| 416 | goto err; |
| 417 | |
| 418 | memcpy(&elem->st, &env->cur_state, sizeof(env->cur_state)); |
| 419 | elem->insn_idx = insn_idx; |
| 420 | elem->prev_insn_idx = prev_insn_idx; |
| 421 | elem->next = env->head; |
| 422 | env->head = elem; |
| 423 | env->stack_size++; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 424 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 425 | verbose("BPF program is too complex\n"); |
| 426 | goto err; |
| 427 | } |
| 428 | return &elem->st; |
| 429 | err: |
| 430 | /* pop all elements and return */ |
| 431 | while (pop_stack(env, NULL) >= 0); |
| 432 | return NULL; |
| 433 | } |
| 434 | |
| 435 | #define CALLER_SAVED_REGS 6 |
| 436 | static const int caller_saved[CALLER_SAVED_REGS] = { |
| 437 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 |
| 438 | }; |
| 439 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 440 | static void init_reg_state(struct bpf_reg_state *regs) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 441 | { |
| 442 | int i; |
| 443 | |
| 444 | for (i = 0; i < MAX_BPF_REG; i++) { |
| 445 | regs[i].type = NOT_INIT; |
| 446 | regs[i].imm = 0; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 447 | regs[i].min_value = BPF_REGISTER_MIN_RANGE; |
| 448 | regs[i].max_value = BPF_REGISTER_MAX_RANGE; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /* frame pointer */ |
| 452 | regs[BPF_REG_FP].type = FRAME_PTR; |
| 453 | |
| 454 | /* 1st arg to a function */ |
| 455 | regs[BPF_REG_1].type = PTR_TO_CTX; |
| 456 | } |
| 457 | |
Daniel Borkmann | 0e0f1d6 | 2016-12-18 01:52:59 +0100 | [diff] [blame] | 458 | static void __mark_reg_unknown_value(struct bpf_reg_state *regs, u32 regno) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 459 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 460 | regs[regno].type = UNKNOWN_VALUE; |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 461 | regs[regno].id = 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 462 | regs[regno].imm = 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Daniel Borkmann | 0e0f1d6 | 2016-12-18 01:52:59 +0100 | [diff] [blame] | 465 | static void mark_reg_unknown_value(struct bpf_reg_state *regs, u32 regno) |
| 466 | { |
| 467 | BUG_ON(regno >= MAX_BPF_REG); |
| 468 | __mark_reg_unknown_value(regs, regno); |
| 469 | } |
| 470 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 471 | static void reset_reg_range_values(struct bpf_reg_state *regs, u32 regno) |
| 472 | { |
| 473 | regs[regno].min_value = BPF_REGISTER_MIN_RANGE; |
| 474 | regs[regno].max_value = BPF_REGISTER_MAX_RANGE; |
| 475 | } |
| 476 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 477 | enum reg_arg_type { |
| 478 | SRC_OP, /* register is used as source operand */ |
| 479 | DST_OP, /* register is used as destination operand */ |
| 480 | DST_OP_NO_MARK /* same as above, check only, don't mark */ |
| 481 | }; |
| 482 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 483 | static int check_reg_arg(struct bpf_reg_state *regs, u32 regno, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 484 | enum reg_arg_type t) |
| 485 | { |
| 486 | if (regno >= MAX_BPF_REG) { |
| 487 | verbose("R%d is invalid\n", regno); |
| 488 | return -EINVAL; |
| 489 | } |
| 490 | |
| 491 | if (t == SRC_OP) { |
| 492 | /* check whether register used as source operand can be read */ |
| 493 | if (regs[regno].type == NOT_INIT) { |
| 494 | verbose("R%d !read_ok\n", regno); |
| 495 | return -EACCES; |
| 496 | } |
| 497 | } else { |
| 498 | /* check whether register used as dest operand can be written to */ |
| 499 | if (regno == BPF_REG_FP) { |
| 500 | verbose("frame pointer is read only\n"); |
| 501 | return -EACCES; |
| 502 | } |
| 503 | if (t == DST_OP) |
| 504 | mark_reg_unknown_value(regs, regno); |
| 505 | } |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | static int bpf_size_to_bytes(int bpf_size) |
| 510 | { |
| 511 | if (bpf_size == BPF_W) |
| 512 | return 4; |
| 513 | else if (bpf_size == BPF_H) |
| 514 | return 2; |
| 515 | else if (bpf_size == BPF_B) |
| 516 | return 1; |
| 517 | else if (bpf_size == BPF_DW) |
| 518 | return 8; |
| 519 | else |
| 520 | return -EINVAL; |
| 521 | } |
| 522 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 523 | static bool is_spillable_regtype(enum bpf_reg_type type) |
| 524 | { |
| 525 | switch (type) { |
| 526 | case PTR_TO_MAP_VALUE: |
| 527 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 528 | case PTR_TO_STACK: |
| 529 | case PTR_TO_CTX: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 530 | case PTR_TO_PACKET: |
| 531 | case PTR_TO_PACKET_END: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 532 | case FRAME_PTR: |
| 533 | case CONST_PTR_TO_MAP: |
| 534 | return true; |
| 535 | default: |
| 536 | return false; |
| 537 | } |
| 538 | } |
| 539 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 540 | /* check_stack_read/write functions track spill/fill of registers, |
| 541 | * stack boundary and alignment are checked in check_mem_access() |
| 542 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 543 | static int check_stack_write(struct bpf_verifier_state *state, int off, |
| 544 | int size, int value_regno) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 545 | { |
Ben Hutchings | 9c33b84 | 2018-12-05 22:45:15 +0000 | [diff] [blame^] | 546 | int i, spi = (MAX_BPF_STACK + off) / BPF_REG_SIZE; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 547 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
| 548 | * so it's aligned access and [off, off + size) are within stack limits |
| 549 | */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 550 | |
| 551 | if (value_regno >= 0 && |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 552 | is_spillable_regtype(state->regs[value_regno].type)) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 553 | |
| 554 | /* register containing pointer is being spilled into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 555 | if (size != BPF_REG_SIZE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 556 | verbose("invalid size of register spill\n"); |
| 557 | return -EACCES; |
| 558 | } |
| 559 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 560 | /* save register state */ |
Ben Hutchings | 9c33b84 | 2018-12-05 22:45:15 +0000 | [diff] [blame^] | 561 | state->spilled_regs[spi] = state->regs[value_regno]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 562 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 563 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 564 | state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_SPILL; |
| 565 | } else { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 566 | /* regular write of data into stack */ |
Ben Hutchings | 9c33b84 | 2018-12-05 22:45:15 +0000 | [diff] [blame^] | 567 | state->spilled_regs[spi] = (struct bpf_reg_state) {}; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 568 | |
| 569 | for (i = 0; i < size; i++) |
| 570 | state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_MISC; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 571 | } |
| 572 | return 0; |
| 573 | } |
| 574 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 575 | static int check_stack_read(struct bpf_verifier_state *state, int off, int size, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 576 | int value_regno) |
| 577 | { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 578 | u8 *slot_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 579 | int i; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 580 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 581 | slot_type = &state->stack_slot_type[MAX_BPF_STACK + off]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 582 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 583 | if (slot_type[0] == STACK_SPILL) { |
| 584 | if (size != BPF_REG_SIZE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 585 | verbose("invalid size of register spill\n"); |
| 586 | return -EACCES; |
| 587 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 588 | for (i = 1; i < BPF_REG_SIZE; i++) { |
| 589 | if (slot_type[i] != STACK_SPILL) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 590 | verbose("corrupted spill memory\n"); |
| 591 | return -EACCES; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | if (value_regno >= 0) |
| 596 | /* restore register state from stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 597 | state->regs[value_regno] = |
| 598 | state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 599 | return 0; |
| 600 | } else { |
| 601 | for (i = 0; i < size; i++) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 602 | if (slot_type[i] != STACK_MISC) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 603 | verbose("invalid read from stack off %d+%d size %d\n", |
| 604 | off, i, size); |
| 605 | return -EACCES; |
| 606 | } |
| 607 | } |
| 608 | if (value_regno >= 0) |
| 609 | /* have read misc data from the stack */ |
| 610 | mark_reg_unknown_value(state->regs, value_regno); |
| 611 | return 0; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | /* check read/write into map element returned by bpf_map_lookup_elem() */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 616 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, int off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 617 | int size) |
| 618 | { |
| 619 | struct bpf_map *map = env->cur_state.regs[regno].map_ptr; |
| 620 | |
| 621 | if (off < 0 || off + size > map->value_size) { |
| 622 | verbose("invalid access to map value, value_size=%d off=%d size=%d\n", |
| 623 | map->value_size, off, size); |
| 624 | return -EACCES; |
| 625 | } |
| 626 | return 0; |
| 627 | } |
| 628 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 629 | #define MAX_PACKET_OFF 0xffff |
| 630 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 631 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 632 | const struct bpf_call_arg_meta *meta) |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 633 | { |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 634 | switch (env->prog->type) { |
| 635 | case BPF_PROG_TYPE_SCHED_CLS: |
| 636 | case BPF_PROG_TYPE_SCHED_ACT: |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 637 | case BPF_PROG_TYPE_XDP: |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 638 | if (meta) |
| 639 | return meta->pkt_access; |
| 640 | |
| 641 | env->seen_direct_write = true; |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 642 | return true; |
| 643 | default: |
| 644 | return false; |
| 645 | } |
| 646 | } |
| 647 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 648 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 649 | int size) |
| 650 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 651 | struct bpf_reg_state *regs = env->cur_state.regs; |
| 652 | struct bpf_reg_state *reg = ®s[regno]; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 653 | |
Alexei Starovoitov | d91b28e | 2016-05-19 18:17:13 -0700 | [diff] [blame] | 654 | off += reg->off; |
Daniel Borkmann | b399cf6 | 2016-09-20 00:26:12 +0200 | [diff] [blame] | 655 | if (off < 0 || size <= 0 || off + size > reg->range) { |
Alexei Starovoitov | d91b28e | 2016-05-19 18:17:13 -0700 | [diff] [blame] | 656 | verbose("invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", |
| 657 | off, size, regno, reg->id, reg->off, reg->range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 658 | return -EACCES; |
| 659 | } |
| 660 | return 0; |
| 661 | } |
| 662 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 663 | /* check access to 'struct bpf_context' fields */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 664 | static int check_ctx_access(struct bpf_verifier_env *env, int off, int size, |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 665 | enum bpf_access_type t, enum bpf_reg_type *reg_type) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 666 | { |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 667 | /* for analyzer ctx accesses are already validated and converted */ |
| 668 | if (env->analyzer_ops) |
| 669 | return 0; |
| 670 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 671 | if (env->prog->aux->ops->is_valid_access && |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 672 | env->prog->aux->ops->is_valid_access(off, size, t, reg_type)) { |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 673 | /* remember the offset of last byte accessed in ctx */ |
| 674 | if (env->prog->aux->max_ctx_offset < off + size) |
| 675 | env->prog->aux->max_ctx_offset = off + size; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 676 | return 0; |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 677 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 678 | |
| 679 | verbose("invalid bpf_context access off=%d size=%d\n", off, size); |
| 680 | return -EACCES; |
| 681 | } |
| 682 | |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 683 | static bool __is_pointer_value(bool allow_ptr_leaks, |
| 684 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 685 | { |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 686 | if (allow_ptr_leaks) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 687 | return false; |
| 688 | |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 689 | switch (reg->type) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 690 | case UNKNOWN_VALUE: |
| 691 | case CONST_IMM: |
| 692 | return false; |
| 693 | default: |
| 694 | return true; |
| 695 | } |
| 696 | } |
| 697 | |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 698 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
| 699 | { |
| 700 | return __is_pointer_value(env->allow_ptr_leaks, &env->cur_state.regs[regno]); |
| 701 | } |
| 702 | |
Daniel Borkmann | f531fbb | 2018-01-29 02:49:01 +0100 | [diff] [blame] | 703 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
| 704 | { |
| 705 | const struct bpf_reg_state *reg = &env->cur_state.regs[regno]; |
| 706 | |
| 707 | return reg->type == PTR_TO_CTX; |
| 708 | } |
| 709 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 710 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
| 711 | struct bpf_reg_state *reg, int off, int size) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 712 | { |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 713 | if (reg->type != PTR_TO_PACKET && reg->type != PTR_TO_MAP_VALUE_ADJ) { |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 714 | if (off % size != 0) { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 715 | verbose("misaligned access off %d size %d\n", |
| 716 | off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 717 | return -EACCES; |
| 718 | } else { |
| 719 | return 0; |
| 720 | } |
| 721 | } |
| 722 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 723 | if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
| 724 | /* misaligned access to packet is ok on x86,arm,arm64 */ |
| 725 | return 0; |
| 726 | |
| 727 | if (reg->id && size != 1) { |
| 728 | verbose("Unknown packet alignment. Only byte-sized access allowed\n"); |
| 729 | return -EACCES; |
| 730 | } |
| 731 | |
| 732 | /* skb->data is NET_IP_ALIGN-ed */ |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 733 | if (reg->type == PTR_TO_PACKET && |
| 734 | (NET_IP_ALIGN + reg->off + off) % size != 0) { |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 735 | verbose("misaligned packet access off %d+%d+%d size %d\n", |
| 736 | NET_IP_ALIGN, reg->off, off, size); |
| 737 | return -EACCES; |
| 738 | } |
| 739 | return 0; |
| 740 | } |
| 741 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 742 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 743 | * if t==write, value_regno is a register which value is stored into memory |
| 744 | * if t==read, value_regno is a register which will receive the value from memory |
| 745 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 746 | * if t==read && value_regno==-1, don't care what we read from memory |
| 747 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 748 | static int check_mem_access(struct bpf_verifier_env *env, u32 regno, int off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 749 | int bpf_size, enum bpf_access_type t, |
| 750 | int value_regno) |
| 751 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 752 | struct bpf_verifier_state *state = &env->cur_state; |
| 753 | struct bpf_reg_state *reg = &state->regs[regno]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 754 | int size, err = 0; |
| 755 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 756 | if (reg->type == PTR_TO_STACK) |
| 757 | off += reg->imm; |
Alex Gartrell | 24b4d2a | 2015-07-23 14:24:40 -0700 | [diff] [blame] | 758 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 759 | size = bpf_size_to_bytes(bpf_size); |
| 760 | if (size < 0) |
| 761 | return size; |
| 762 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 763 | err = check_ptr_alignment(env, reg, off, size); |
| 764 | if (err) |
| 765 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 766 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 767 | if (reg->type == PTR_TO_MAP_VALUE || |
| 768 | reg->type == PTR_TO_MAP_VALUE_ADJ) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 769 | if (t == BPF_WRITE && value_regno >= 0 && |
| 770 | is_pointer_value(env, value_regno)) { |
| 771 | verbose("R%d leaks addr into map\n", value_regno); |
| 772 | return -EACCES; |
| 773 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 774 | |
| 775 | /* If we adjusted the register to this map value at all then we |
| 776 | * need to change off and size to min_value and max_value |
| 777 | * respectively to make sure our theoretical access will be |
| 778 | * safe. |
| 779 | */ |
| 780 | if (reg->type == PTR_TO_MAP_VALUE_ADJ) { |
| 781 | if (log_level) |
| 782 | print_verifier_state(state); |
| 783 | env->varlen_map_value_access = true; |
| 784 | /* The minimum value is only important with signed |
| 785 | * comparisons where we can't assume the floor of a |
| 786 | * value is 0. If we are using signed variables for our |
| 787 | * index'es we need to make sure that whatever we use |
| 788 | * will have a set floor within our range. |
| 789 | */ |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 790 | if (reg->min_value < 0) { |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 791 | verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
| 792 | regno); |
| 793 | return -EACCES; |
| 794 | } |
| 795 | err = check_map_access(env, regno, reg->min_value + off, |
| 796 | size); |
| 797 | if (err) { |
| 798 | verbose("R%d min value is outside of the array range\n", |
| 799 | regno); |
| 800 | return err; |
| 801 | } |
| 802 | |
| 803 | /* If we haven't set a max value then we need to bail |
| 804 | * since we can't be sure we won't do bad things. |
| 805 | */ |
| 806 | if (reg->max_value == BPF_REGISTER_MAX_RANGE) { |
| 807 | verbose("R%d unbounded memory access, make sure to bounds check any array access into a map\n", |
| 808 | regno); |
| 809 | return -EACCES; |
| 810 | } |
| 811 | off += reg->max_value; |
| 812 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 813 | err = check_map_access(env, regno, off, size); |
| 814 | if (!err && t == BPF_READ && value_regno >= 0) |
| 815 | mark_reg_unknown_value(state->regs, value_regno); |
| 816 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 817 | } else if (reg->type == PTR_TO_CTX) { |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 818 | enum bpf_reg_type reg_type = UNKNOWN_VALUE; |
| 819 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 820 | if (t == BPF_WRITE && value_regno >= 0 && |
| 821 | is_pointer_value(env, value_regno)) { |
| 822 | verbose("R%d leaks addr into ctx\n", value_regno); |
| 823 | return -EACCES; |
| 824 | } |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 825 | err = check_ctx_access(env, off, size, t, ®_type); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 826 | if (!err && t == BPF_READ && value_regno >= 0) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 827 | mark_reg_unknown_value(state->regs, value_regno); |
Mickaël Salaün | 1955351 | 2016-09-24 20:01:50 +0200 | [diff] [blame] | 828 | /* note that reg.[id|off|range] == 0 */ |
| 829 | state->regs[value_regno].type = reg_type; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 830 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 831 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 832 | } else if (reg->type == FRAME_PTR || reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 833 | if (off >= 0 || off < -MAX_BPF_STACK) { |
| 834 | verbose("invalid stack off=%d size=%d\n", off, size); |
| 835 | return -EACCES; |
| 836 | } |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 837 | if (t == BPF_WRITE) { |
| 838 | if (!env->allow_ptr_leaks && |
| 839 | state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL && |
| 840 | size != BPF_REG_SIZE) { |
| 841 | verbose("attempt to corrupt spilled pointer on stack\n"); |
| 842 | return -EACCES; |
| 843 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 844 | err = check_stack_write(state, off, size, value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 845 | } else { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 846 | err = check_stack_read(state, off, size, value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 847 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 848 | } else if (state->regs[regno].type == PTR_TO_PACKET) { |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 849 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL)) { |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 850 | verbose("cannot write into packet\n"); |
| 851 | return -EACCES; |
| 852 | } |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 853 | if (t == BPF_WRITE && value_regno >= 0 && |
| 854 | is_pointer_value(env, value_regno)) { |
| 855 | verbose("R%d leaks addr into packet\n", value_regno); |
| 856 | return -EACCES; |
| 857 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 858 | err = check_packet_access(env, regno, off, size); |
| 859 | if (!err && t == BPF_READ && value_regno >= 0) |
| 860 | mark_reg_unknown_value(state->regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 861 | } else { |
| 862 | verbose("R%d invalid mem access '%s'\n", |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 863 | regno, reg_type_str[reg->type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 864 | return -EACCES; |
| 865 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 866 | |
| 867 | if (!err && size <= 2 && value_regno >= 0 && env->allow_ptr_leaks && |
| 868 | state->regs[value_regno].type == UNKNOWN_VALUE) { |
| 869 | /* 1 or 2 byte load zero-extends, determine the number of |
| 870 | * zero upper bits. Not doing it fo 4 byte load, since |
| 871 | * such values cannot be added to ptr_to_packet anyway. |
| 872 | */ |
| 873 | state->regs[value_regno].imm = 64 - size * 8; |
| 874 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 875 | return err; |
| 876 | } |
| 877 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 878 | static int check_xadd(struct bpf_verifier_env *env, struct bpf_insn *insn) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 879 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 880 | struct bpf_reg_state *regs = env->cur_state.regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 881 | int err; |
| 882 | |
| 883 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || |
| 884 | insn->imm != 0) { |
| 885 | verbose("BPF_XADD uses reserved fields\n"); |
| 886 | return -EINVAL; |
| 887 | } |
| 888 | |
| 889 | /* check src1 operand */ |
| 890 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 891 | if (err) |
| 892 | return err; |
| 893 | |
| 894 | /* check src2 operand */ |
| 895 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 896 | if (err) |
| 897 | return err; |
| 898 | |
Daniel Borkmann | cd5de9c | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 899 | if (is_pointer_value(env, insn->src_reg)) { |
| 900 | verbose("R%d leaks addr into mem\n", insn->src_reg); |
| 901 | return -EACCES; |
| 902 | } |
| 903 | |
Daniel Borkmann | f531fbb | 2018-01-29 02:49:01 +0100 | [diff] [blame] | 904 | if (is_ctx_reg(env, insn->dst_reg)) { |
| 905 | verbose("BPF_XADD stores into R%d context is not allowed\n", |
| 906 | insn->dst_reg); |
| 907 | return -EACCES; |
| 908 | } |
| 909 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 910 | /* check whether atomic_add can read the memory */ |
| 911 | err = check_mem_access(env, insn->dst_reg, insn->off, |
| 912 | BPF_SIZE(insn->code), BPF_READ, -1); |
| 913 | if (err) |
| 914 | return err; |
| 915 | |
| 916 | /* check whether atomic_add can write into the same memory */ |
| 917 | return check_mem_access(env, insn->dst_reg, insn->off, |
| 918 | BPF_SIZE(insn->code), BPF_WRITE, -1); |
| 919 | } |
| 920 | |
| 921 | /* when register 'regno' is passed into function that will read 'access_size' |
| 922 | * bytes from that pointer, make sure that it's within stack boundary |
| 923 | * and all elements of stack are initialized |
| 924 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 925 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 926 | int access_size, bool zero_size_allowed, |
| 927 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 928 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 929 | struct bpf_verifier_state *state = &env->cur_state; |
| 930 | struct bpf_reg_state *regs = state->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 931 | int off, i; |
| 932 | |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 933 | if (regs[regno].type != PTR_TO_STACK) { |
| 934 | if (zero_size_allowed && access_size == 0 && |
| 935 | regs[regno].type == CONST_IMM && |
| 936 | regs[regno].imm == 0) |
| 937 | return 0; |
| 938 | |
| 939 | verbose("R%d type=%s expected=%s\n", regno, |
| 940 | reg_type_str[regs[regno].type], |
| 941 | reg_type_str[PTR_TO_STACK]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 942 | return -EACCES; |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 943 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 944 | |
| 945 | off = regs[regno].imm; |
| 946 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
| 947 | access_size <= 0) { |
| 948 | verbose("invalid stack type R%d off=%d access_size=%d\n", |
| 949 | regno, off, access_size); |
| 950 | return -EACCES; |
| 951 | } |
| 952 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 953 | if (meta && meta->raw_mode) { |
| 954 | meta->access_size = access_size; |
| 955 | meta->regno = regno; |
| 956 | return 0; |
| 957 | } |
| 958 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 959 | for (i = 0; i < access_size; i++) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 960 | if (state->stack_slot_type[MAX_BPF_STACK + off + i] != STACK_MISC) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 961 | verbose("invalid indirect read from stack off %d+%d size %d\n", |
| 962 | off, i, access_size); |
| 963 | return -EACCES; |
| 964 | } |
| 965 | } |
| 966 | return 0; |
| 967 | } |
| 968 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 969 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 970 | enum bpf_arg_type arg_type, |
| 971 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 972 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 973 | struct bpf_reg_state *regs = env->cur_state.regs, *reg = ®s[regno]; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 974 | enum bpf_reg_type expected_type, type = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 975 | int err = 0; |
| 976 | |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 977 | if (arg_type == ARG_DONTCARE) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 978 | return 0; |
| 979 | |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 980 | if (type == NOT_INIT) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 981 | verbose("R%d !read_ok\n", regno); |
| 982 | return -EACCES; |
| 983 | } |
| 984 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 985 | if (arg_type == ARG_ANYTHING) { |
| 986 | if (is_pointer_value(env, regno)) { |
| 987 | verbose("R%d leaks addr into helper function\n", regno); |
| 988 | return -EACCES; |
| 989 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 990 | return 0; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 991 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 992 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 993 | if (type == PTR_TO_PACKET && !may_access_direct_pkt_data(env, meta)) { |
| 994 | verbose("helper access to the packet is not allowed\n"); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 995 | return -EACCES; |
| 996 | } |
| 997 | |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 998 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 999 | arg_type == ARG_PTR_TO_MAP_VALUE) { |
| 1000 | expected_type = PTR_TO_STACK; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1001 | if (type != PTR_TO_PACKET && type != expected_type) |
| 1002 | goto err_type; |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1003 | } else if (arg_type == ARG_CONST_STACK_SIZE || |
| 1004 | arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1005 | expected_type = CONST_IMM; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1006 | if (type != expected_type) |
| 1007 | goto err_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1008 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
| 1009 | expected_type = CONST_PTR_TO_MAP; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1010 | if (type != expected_type) |
| 1011 | goto err_type; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1012 | } else if (arg_type == ARG_PTR_TO_CTX) { |
| 1013 | expected_type = PTR_TO_CTX; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1014 | if (type != expected_type) |
| 1015 | goto err_type; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1016 | } else if (arg_type == ARG_PTR_TO_STACK || |
| 1017 | arg_type == ARG_PTR_TO_RAW_STACK) { |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1018 | expected_type = PTR_TO_STACK; |
| 1019 | /* One exception here. In case function allows for NULL to be |
| 1020 | * passed in as argument, it's a CONST_IMM type. Final test |
| 1021 | * happens during stack boundary checking. |
| 1022 | */ |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1023 | if (type == CONST_IMM && reg->imm == 0) |
| 1024 | /* final test in check_stack_boundary() */; |
| 1025 | else if (type != PTR_TO_PACKET && type != expected_type) |
| 1026 | goto err_type; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1027 | meta->raw_mode = arg_type == ARG_PTR_TO_RAW_STACK; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1028 | } else { |
| 1029 | verbose("unsupported arg_type %d\n", arg_type); |
| 1030 | return -EFAULT; |
| 1031 | } |
| 1032 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1033 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 1034 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1035 | meta->map_ptr = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1036 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 1037 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 1038 | * check that [key, key + map->key_size) are within |
| 1039 | * stack limits and initialized |
| 1040 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1041 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1042 | /* in function declaration map_ptr must come before |
| 1043 | * map_key, so that it's verified and known before |
| 1044 | * we have to check map_key here. Otherwise it means |
| 1045 | * that kernel subsystem misconfigured verifier |
| 1046 | */ |
| 1047 | verbose("invalid map_ptr to access map->key\n"); |
| 1048 | return -EACCES; |
| 1049 | } |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1050 | if (type == PTR_TO_PACKET) |
| 1051 | err = check_packet_access(env, regno, 0, |
| 1052 | meta->map_ptr->key_size); |
| 1053 | else |
| 1054 | err = check_stack_boundary(env, regno, |
| 1055 | meta->map_ptr->key_size, |
| 1056 | false, NULL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1057 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE) { |
| 1058 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 1059 | * check [value, value + map->value_size) validity |
| 1060 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1061 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1062 | /* kernel subsystem misconfigured verifier */ |
| 1063 | verbose("invalid map_ptr to access map->value\n"); |
| 1064 | return -EACCES; |
| 1065 | } |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1066 | if (type == PTR_TO_PACKET) |
| 1067 | err = check_packet_access(env, regno, 0, |
| 1068 | meta->map_ptr->value_size); |
| 1069 | else |
| 1070 | err = check_stack_boundary(env, regno, |
| 1071 | meta->map_ptr->value_size, |
| 1072 | false, NULL); |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1073 | } else if (arg_type == ARG_CONST_STACK_SIZE || |
| 1074 | arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) { |
| 1075 | bool zero_size_allowed = (arg_type == ARG_CONST_STACK_SIZE_OR_ZERO); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1076 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1077 | /* bpf_xxx(..., buf, len) call will access 'len' bytes |
| 1078 | * from stack pointer 'buf'. Check it |
| 1079 | * note: regno == len, regno - 1 == buf |
| 1080 | */ |
| 1081 | if (regno == 0) { |
| 1082 | /* kernel subsystem misconfigured verifier */ |
| 1083 | verbose("ARG_CONST_STACK_SIZE cannot be first argument\n"); |
| 1084 | return -EACCES; |
| 1085 | } |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1086 | if (regs[regno - 1].type == PTR_TO_PACKET) |
| 1087 | err = check_packet_access(env, regno - 1, 0, reg->imm); |
| 1088 | else |
| 1089 | err = check_stack_boundary(env, regno - 1, reg->imm, |
| 1090 | zero_size_allowed, meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | return err; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1094 | err_type: |
| 1095 | verbose("R%d type=%s expected=%s\n", regno, |
| 1096 | reg_type_str[type], reg_type_str[expected_type]); |
| 1097 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1100 | static int check_map_func_compatibility(struct bpf_map *map, int func_id) |
| 1101 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1102 | if (!map) |
| 1103 | return 0; |
| 1104 | |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1105 | /* We need a two way check, first is from map perspective ... */ |
| 1106 | switch (map->map_type) { |
| 1107 | case BPF_MAP_TYPE_PROG_ARRAY: |
| 1108 | if (func_id != BPF_FUNC_tail_call) |
| 1109 | goto error; |
| 1110 | break; |
| 1111 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
| 1112 | if (func_id != BPF_FUNC_perf_event_read && |
| 1113 | func_id != BPF_FUNC_perf_event_output) |
| 1114 | goto error; |
| 1115 | break; |
| 1116 | case BPF_MAP_TYPE_STACK_TRACE: |
| 1117 | if (func_id != BPF_FUNC_get_stackid) |
| 1118 | goto error; |
| 1119 | break; |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 1120 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
David S. Miller | 60747ef | 2016-08-18 01:17:32 -0400 | [diff] [blame] | 1121 | if (func_id != BPF_FUNC_skb_under_cgroup && |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 1122 | func_id != BPF_FUNC_current_task_under_cgroup) |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 1123 | goto error; |
| 1124 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1125 | default: |
| 1126 | break; |
| 1127 | } |
| 1128 | |
| 1129 | /* ... and second from the function itself. */ |
| 1130 | switch (func_id) { |
| 1131 | case BPF_FUNC_tail_call: |
| 1132 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 1133 | goto error; |
| 1134 | break; |
| 1135 | case BPF_FUNC_perf_event_read: |
| 1136 | case BPF_FUNC_perf_event_output: |
| 1137 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
| 1138 | goto error; |
| 1139 | break; |
| 1140 | case BPF_FUNC_get_stackid: |
| 1141 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) |
| 1142 | goto error; |
| 1143 | break; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 1144 | case BPF_FUNC_current_task_under_cgroup: |
Daniel Borkmann | 747ea55 | 2016-08-12 22:17:17 +0200 | [diff] [blame] | 1145 | case BPF_FUNC_skb_under_cgroup: |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 1146 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
| 1147 | goto error; |
| 1148 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1149 | default: |
| 1150 | break; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | return 0; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1154 | error: |
| 1155 | verbose("cannot pass map_type %d into func %d\n", |
| 1156 | map->map_type, func_id); |
| 1157 | return -EINVAL; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1160 | static int check_raw_mode(const struct bpf_func_proto *fn) |
| 1161 | { |
| 1162 | int count = 0; |
| 1163 | |
| 1164 | if (fn->arg1_type == ARG_PTR_TO_RAW_STACK) |
| 1165 | count++; |
| 1166 | if (fn->arg2_type == ARG_PTR_TO_RAW_STACK) |
| 1167 | count++; |
| 1168 | if (fn->arg3_type == ARG_PTR_TO_RAW_STACK) |
| 1169 | count++; |
| 1170 | if (fn->arg4_type == ARG_PTR_TO_RAW_STACK) |
| 1171 | count++; |
| 1172 | if (fn->arg5_type == ARG_PTR_TO_RAW_STACK) |
| 1173 | count++; |
| 1174 | |
| 1175 | return count > 1 ? -EINVAL : 0; |
| 1176 | } |
| 1177 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1178 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1179 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1180 | struct bpf_verifier_state *state = &env->cur_state; |
| 1181 | struct bpf_reg_state *regs = state->regs, *reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1182 | int i; |
| 1183 | |
| 1184 | for (i = 0; i < MAX_BPF_REG; i++) |
| 1185 | if (regs[i].type == PTR_TO_PACKET || |
| 1186 | regs[i].type == PTR_TO_PACKET_END) |
| 1187 | mark_reg_unknown_value(regs, i); |
| 1188 | |
| 1189 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { |
| 1190 | if (state->stack_slot_type[i] != STACK_SPILL) |
| 1191 | continue; |
| 1192 | reg = &state->spilled_regs[i / BPF_REG_SIZE]; |
| 1193 | if (reg->type != PTR_TO_PACKET && |
| 1194 | reg->type != PTR_TO_PACKET_END) |
| 1195 | continue; |
| 1196 | reg->type = UNKNOWN_VALUE; |
| 1197 | reg->imm = 0; |
| 1198 | } |
| 1199 | } |
| 1200 | |
Alexei Starovoitov | a9bfac14 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 1201 | static int check_call(struct bpf_verifier_env *env, int func_id, int insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1202 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1203 | struct bpf_verifier_state *state = &env->cur_state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1204 | const struct bpf_func_proto *fn = NULL; |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1205 | struct bpf_reg_state *regs = state->regs; |
| 1206 | struct bpf_reg_state *reg; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1207 | struct bpf_call_arg_meta meta; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1208 | bool changes_data; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1209 | int i, err; |
| 1210 | |
| 1211 | /* find function prototype */ |
| 1212 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
| 1213 | verbose("invalid func %d\n", func_id); |
| 1214 | return -EINVAL; |
| 1215 | } |
| 1216 | |
| 1217 | if (env->prog->aux->ops->get_func_proto) |
| 1218 | fn = env->prog->aux->ops->get_func_proto(func_id); |
| 1219 | |
| 1220 | if (!fn) { |
| 1221 | verbose("unknown func %d\n", func_id); |
| 1222 | return -EINVAL; |
| 1223 | } |
| 1224 | |
| 1225 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 1226 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1227 | verbose("cannot call GPL only function from proprietary program\n"); |
| 1228 | return -EINVAL; |
| 1229 | } |
| 1230 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1231 | changes_data = bpf_helper_changes_skb_data(fn->func); |
| 1232 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1233 | memset(&meta, 0, sizeof(meta)); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1234 | meta.pkt_access = fn->pkt_access; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1235 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1236 | /* We only support one arg being in raw mode at the moment, which |
| 1237 | * is sufficient for the helper functions we have right now. |
| 1238 | */ |
| 1239 | err = check_raw_mode(fn); |
| 1240 | if (err) { |
| 1241 | verbose("kernel subsystem misconfigured func %d\n", func_id); |
| 1242 | return err; |
| 1243 | } |
| 1244 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1245 | /* check args */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1246 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1247 | if (err) |
| 1248 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1249 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1250 | if (err) |
| 1251 | return err; |
Alexei Starovoitov | a9bfac14 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 1252 | if (func_id == BPF_FUNC_tail_call) { |
| 1253 | if (meta.map_ptr == NULL) { |
| 1254 | verbose("verifier bug\n"); |
| 1255 | return -EINVAL; |
| 1256 | } |
| 1257 | env->insn_aux_data[insn_idx].map_ptr = meta.map_ptr; |
| 1258 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1259 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1260 | if (err) |
| 1261 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1262 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1263 | if (err) |
| 1264 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1265 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1266 | if (err) |
| 1267 | return err; |
| 1268 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1269 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
| 1270 | * is inferred from register state. |
| 1271 | */ |
| 1272 | for (i = 0; i < meta.access_size; i++) { |
| 1273 | err = check_mem_access(env, meta.regno, i, BPF_B, BPF_WRITE, -1); |
| 1274 | if (err) |
| 1275 | return err; |
| 1276 | } |
| 1277 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1278 | /* reset caller saved regs */ |
| 1279 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 1280 | reg = regs + caller_saved[i]; |
| 1281 | reg->type = NOT_INIT; |
| 1282 | reg->imm = 0; |
| 1283 | } |
| 1284 | |
| 1285 | /* update return register */ |
| 1286 | if (fn->ret_type == RET_INTEGER) { |
| 1287 | regs[BPF_REG_0].type = UNKNOWN_VALUE; |
| 1288 | } else if (fn->ret_type == RET_VOID) { |
| 1289 | regs[BPF_REG_0].type = NOT_INIT; |
| 1290 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) { |
| 1291 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1292 | regs[BPF_REG_0].max_value = regs[BPF_REG_0].min_value = 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1293 | /* remember map_ptr, so that check_map_access() |
| 1294 | * can check 'value_size' boundary of memory access |
| 1295 | * to map element returned from bpf_map_lookup_elem() |
| 1296 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1297 | if (meta.map_ptr == NULL) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1298 | verbose("kernel subsystem misconfigured verifier\n"); |
| 1299 | return -EINVAL; |
| 1300 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1301 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 1302 | regs[BPF_REG_0].id = ++env->id_gen; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1303 | } else { |
| 1304 | verbose("unknown return type %d of func %d\n", |
| 1305 | fn->ret_type, func_id); |
| 1306 | return -EINVAL; |
| 1307 | } |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1308 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1309 | err = check_map_func_compatibility(meta.map_ptr, func_id); |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1310 | if (err) |
| 1311 | return err; |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1312 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1313 | if (changes_data) |
| 1314 | clear_all_pkt_pointers(env); |
| 1315 | return 0; |
| 1316 | } |
| 1317 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1318 | static int check_packet_ptr_add(struct bpf_verifier_env *env, |
| 1319 | struct bpf_insn *insn) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1320 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1321 | struct bpf_reg_state *regs = env->cur_state.regs; |
| 1322 | struct bpf_reg_state *dst_reg = ®s[insn->dst_reg]; |
| 1323 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
| 1324 | struct bpf_reg_state tmp_reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1325 | s32 imm; |
| 1326 | |
| 1327 | if (BPF_SRC(insn->code) == BPF_K) { |
| 1328 | /* pkt_ptr += imm */ |
| 1329 | imm = insn->imm; |
| 1330 | |
| 1331 | add_imm: |
| 1332 | if (imm <= 0) { |
| 1333 | verbose("addition of negative constant to packet pointer is not allowed\n"); |
| 1334 | return -EACCES; |
| 1335 | } |
| 1336 | if (imm >= MAX_PACKET_OFF || |
| 1337 | imm + dst_reg->off >= MAX_PACKET_OFF) { |
| 1338 | verbose("constant %d is too large to add to packet pointer\n", |
| 1339 | imm); |
| 1340 | return -EACCES; |
| 1341 | } |
| 1342 | /* a constant was added to pkt_ptr. |
| 1343 | * Remember it while keeping the same 'id' |
| 1344 | */ |
| 1345 | dst_reg->off += imm; |
| 1346 | } else { |
Alexei Starovoitov | 1b9b69e | 2016-05-19 18:17:14 -0700 | [diff] [blame] | 1347 | if (src_reg->type == PTR_TO_PACKET) { |
| 1348 | /* R6=pkt(id=0,off=0,r=62) R7=imm22; r7 += r6 */ |
| 1349 | tmp_reg = *dst_reg; /* save r7 state */ |
| 1350 | *dst_reg = *src_reg; /* copy pkt_ptr state r6 into r7 */ |
| 1351 | src_reg = &tmp_reg; /* pretend it's src_reg state */ |
| 1352 | /* if the checks below reject it, the copy won't matter, |
| 1353 | * since we're rejecting the whole program. If all ok, |
| 1354 | * then imm22 state will be added to r7 |
| 1355 | * and r7 will be pkt(id=0,off=22,r=62) while |
| 1356 | * r6 will stay as pkt(id=0,off=0,r=62) |
| 1357 | */ |
| 1358 | } |
| 1359 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1360 | if (src_reg->type == CONST_IMM) { |
| 1361 | /* pkt_ptr += reg where reg is known constant */ |
| 1362 | imm = src_reg->imm; |
| 1363 | goto add_imm; |
| 1364 | } |
| 1365 | /* disallow pkt_ptr += reg |
| 1366 | * if reg is not uknown_value with guaranteed zero upper bits |
| 1367 | * otherwise pkt_ptr may overflow and addition will become |
| 1368 | * subtraction which is not allowed |
| 1369 | */ |
| 1370 | if (src_reg->type != UNKNOWN_VALUE) { |
| 1371 | verbose("cannot add '%s' to ptr_to_packet\n", |
| 1372 | reg_type_str[src_reg->type]); |
| 1373 | return -EACCES; |
| 1374 | } |
| 1375 | if (src_reg->imm < 48) { |
| 1376 | verbose("cannot add integer value with %lld upper zero bits to ptr_to_packet\n", |
| 1377 | src_reg->imm); |
| 1378 | return -EACCES; |
| 1379 | } |
| 1380 | /* dst_reg stays as pkt_ptr type and since some positive |
| 1381 | * integer value was added to the pointer, increment its 'id' |
| 1382 | */ |
Jakub Kicinski | 1f415a7 | 2016-08-02 16:12:14 +0100 | [diff] [blame] | 1383 | dst_reg->id = ++env->id_gen; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1384 | |
| 1385 | /* something was added to pkt_ptr, set range and off to zero */ |
| 1386 | dst_reg->off = 0; |
| 1387 | dst_reg->range = 0; |
| 1388 | } |
| 1389 | return 0; |
| 1390 | } |
| 1391 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1392 | static int evaluate_reg_alu(struct bpf_verifier_env *env, struct bpf_insn *insn) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1393 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1394 | struct bpf_reg_state *regs = env->cur_state.regs; |
| 1395 | struct bpf_reg_state *dst_reg = ®s[insn->dst_reg]; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1396 | u8 opcode = BPF_OP(insn->code); |
| 1397 | s64 imm_log2; |
| 1398 | |
| 1399 | /* for type == UNKNOWN_VALUE: |
| 1400 | * imm > 0 -> number of zero upper bits |
| 1401 | * imm == 0 -> don't track which is the same as all bits can be non-zero |
| 1402 | */ |
| 1403 | |
| 1404 | if (BPF_SRC(insn->code) == BPF_X) { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1405 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1406 | |
| 1407 | if (src_reg->type == UNKNOWN_VALUE && src_reg->imm > 0 && |
| 1408 | dst_reg->imm && opcode == BPF_ADD) { |
| 1409 | /* dreg += sreg |
| 1410 | * where both have zero upper bits. Adding them |
| 1411 | * can only result making one more bit non-zero |
| 1412 | * in the larger value. |
| 1413 | * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47) |
| 1414 | * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47) |
| 1415 | */ |
| 1416 | dst_reg->imm = min(dst_reg->imm, src_reg->imm); |
| 1417 | dst_reg->imm--; |
| 1418 | return 0; |
| 1419 | } |
| 1420 | if (src_reg->type == CONST_IMM && src_reg->imm > 0 && |
| 1421 | dst_reg->imm && opcode == BPF_ADD) { |
| 1422 | /* dreg += sreg |
| 1423 | * where dreg has zero upper bits and sreg is const. |
| 1424 | * Adding them can only result making one more bit |
| 1425 | * non-zero in the larger value. |
| 1426 | */ |
| 1427 | imm_log2 = __ilog2_u64((long long)src_reg->imm); |
| 1428 | dst_reg->imm = min(dst_reg->imm, 63 - imm_log2); |
| 1429 | dst_reg->imm--; |
| 1430 | return 0; |
| 1431 | } |
| 1432 | /* all other cases non supported yet, just mark dst_reg */ |
| 1433 | dst_reg->imm = 0; |
| 1434 | return 0; |
| 1435 | } |
| 1436 | |
| 1437 | /* sign extend 32-bit imm into 64-bit to make sure that |
| 1438 | * negative values occupy bit 63. Note ilog2() would have |
| 1439 | * been incorrect, since sizeof(insn->imm) == 4 |
| 1440 | */ |
| 1441 | imm_log2 = __ilog2_u64((long long)insn->imm); |
| 1442 | |
| 1443 | if (dst_reg->imm && opcode == BPF_LSH) { |
| 1444 | /* reg <<= imm |
| 1445 | * if reg was a result of 2 byte load, then its imm == 48 |
| 1446 | * which means that upper 48 bits are zero and shifting this reg |
| 1447 | * left by 4 would mean that upper 44 bits are still zero |
| 1448 | */ |
| 1449 | dst_reg->imm -= insn->imm; |
| 1450 | } else if (dst_reg->imm && opcode == BPF_MUL) { |
| 1451 | /* reg *= imm |
| 1452 | * if multiplying by 14 subtract 4 |
| 1453 | * This is conservative calculation of upper zero bits. |
| 1454 | * It's not trying to special case insn->imm == 1 or 0 cases |
| 1455 | */ |
| 1456 | dst_reg->imm -= imm_log2 + 1; |
| 1457 | } else if (opcode == BPF_AND) { |
| 1458 | /* reg &= imm */ |
| 1459 | dst_reg->imm = 63 - imm_log2; |
| 1460 | } else if (dst_reg->imm && opcode == BPF_ADD) { |
| 1461 | /* reg += imm */ |
| 1462 | dst_reg->imm = min(dst_reg->imm, 63 - imm_log2); |
| 1463 | dst_reg->imm--; |
| 1464 | } else if (opcode == BPF_RSH) { |
| 1465 | /* reg >>= imm |
| 1466 | * which means that after right shift, upper bits will be zero |
| 1467 | * note that verifier already checked that |
| 1468 | * 0 <= imm < 64 for shift insn |
| 1469 | */ |
| 1470 | dst_reg->imm += insn->imm; |
| 1471 | if (unlikely(dst_reg->imm > 64)) |
| 1472 | /* some dumb code did: |
| 1473 | * r2 = *(u32 *)mem; |
| 1474 | * r2 >>= 32; |
| 1475 | * and all bits are zero now */ |
| 1476 | dst_reg->imm = 64; |
| 1477 | } else { |
| 1478 | /* all other alu ops, means that we don't know what will |
| 1479 | * happen to the value, mark it with unknown number of zero bits |
| 1480 | */ |
| 1481 | dst_reg->imm = 0; |
| 1482 | } |
| 1483 | |
| 1484 | if (dst_reg->imm < 0) { |
| 1485 | /* all 64 bits of the register can contain non-zero bits |
| 1486 | * and such value cannot be added to ptr_to_packet, since it |
| 1487 | * may overflow, mark it as unknown to avoid further eval |
| 1488 | */ |
| 1489 | dst_reg->imm = 0; |
| 1490 | } |
| 1491 | return 0; |
| 1492 | } |
| 1493 | |
John Fastabend | e37bdee | 2017-07-02 02:13:30 +0200 | [diff] [blame] | 1494 | static int evaluate_reg_imm_alu_unknown(struct bpf_verifier_env *env, |
| 1495 | struct bpf_insn *insn) |
| 1496 | { |
| 1497 | struct bpf_reg_state *regs = env->cur_state.regs; |
| 1498 | struct bpf_reg_state *dst_reg = ®s[insn->dst_reg]; |
| 1499 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
| 1500 | u8 opcode = BPF_OP(insn->code); |
| 1501 | s64 imm_log2 = __ilog2_u64((long long)dst_reg->imm); |
| 1502 | |
| 1503 | /* BPF_X code with src_reg->type UNKNOWN_VALUE here. */ |
| 1504 | if (src_reg->imm > 0 && dst_reg->imm) { |
| 1505 | switch (opcode) { |
| 1506 | case BPF_ADD: |
| 1507 | /* dreg += sreg |
| 1508 | * where both have zero upper bits. Adding them |
| 1509 | * can only result making one more bit non-zero |
| 1510 | * in the larger value. |
| 1511 | * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47) |
| 1512 | * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47) |
| 1513 | */ |
| 1514 | dst_reg->imm = min(src_reg->imm, 63 - imm_log2); |
| 1515 | dst_reg->imm--; |
| 1516 | break; |
| 1517 | case BPF_AND: |
| 1518 | /* dreg &= sreg |
| 1519 | * AND can not extend zero bits only shrink |
| 1520 | * Ex. 0x00..00ffffff |
| 1521 | * & 0x0f..ffffffff |
| 1522 | * ---------------- |
| 1523 | * 0x00..00ffffff |
| 1524 | */ |
| 1525 | dst_reg->imm = max(src_reg->imm, 63 - imm_log2); |
| 1526 | break; |
| 1527 | case BPF_OR: |
| 1528 | /* dreg |= sreg |
| 1529 | * OR can only extend zero bits |
| 1530 | * Ex. 0x00..00ffffff |
| 1531 | * | 0x0f..ffffffff |
| 1532 | * ---------------- |
| 1533 | * 0x0f..00ffffff |
| 1534 | */ |
| 1535 | dst_reg->imm = min(src_reg->imm, 63 - imm_log2); |
| 1536 | break; |
| 1537 | case BPF_SUB: |
| 1538 | case BPF_MUL: |
| 1539 | case BPF_RSH: |
| 1540 | case BPF_LSH: |
| 1541 | /* These may be flushed out later */ |
| 1542 | default: |
| 1543 | mark_reg_unknown_value(regs, insn->dst_reg); |
| 1544 | } |
| 1545 | } else { |
| 1546 | mark_reg_unknown_value(regs, insn->dst_reg); |
| 1547 | } |
| 1548 | |
| 1549 | dst_reg->type = UNKNOWN_VALUE; |
| 1550 | return 0; |
| 1551 | } |
| 1552 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1553 | static int evaluate_reg_imm_alu(struct bpf_verifier_env *env, |
| 1554 | struct bpf_insn *insn) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1555 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1556 | struct bpf_reg_state *regs = env->cur_state.regs; |
| 1557 | struct bpf_reg_state *dst_reg = ®s[insn->dst_reg]; |
| 1558 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1559 | u8 opcode = BPF_OP(insn->code); |
| 1560 | |
John Fastabend | e37bdee | 2017-07-02 02:13:30 +0200 | [diff] [blame] | 1561 | if (BPF_SRC(insn->code) == BPF_X && src_reg->type == UNKNOWN_VALUE) |
| 1562 | return evaluate_reg_imm_alu_unknown(env, insn); |
| 1563 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1564 | /* dst_reg->type == CONST_IMM here, simulate execution of 'add' insn. |
| 1565 | * Don't care about overflow or negative values, just add them |
| 1566 | */ |
| 1567 | if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_K) |
| 1568 | dst_reg->imm += insn->imm; |
| 1569 | else if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_X && |
| 1570 | src_reg->type == CONST_IMM) |
| 1571 | dst_reg->imm += src_reg->imm; |
| 1572 | else |
| 1573 | mark_reg_unknown_value(regs, insn->dst_reg); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1574 | return 0; |
| 1575 | } |
| 1576 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1577 | static void check_reg_overflow(struct bpf_reg_state *reg) |
| 1578 | { |
| 1579 | if (reg->max_value > BPF_REGISTER_MAX_RANGE) |
| 1580 | reg->max_value = BPF_REGISTER_MAX_RANGE; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1581 | if (reg->min_value < BPF_REGISTER_MIN_RANGE || |
| 1582 | reg->min_value > BPF_REGISTER_MAX_RANGE) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1583 | reg->min_value = BPF_REGISTER_MIN_RANGE; |
| 1584 | } |
| 1585 | |
| 1586 | static void adjust_reg_min_max_vals(struct bpf_verifier_env *env, |
| 1587 | struct bpf_insn *insn) |
| 1588 | { |
| 1589 | struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1590 | s64 min_val = BPF_REGISTER_MIN_RANGE; |
| 1591 | u64 max_val = BPF_REGISTER_MAX_RANGE; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1592 | bool min_set = false, max_set = false; |
| 1593 | u8 opcode = BPF_OP(insn->code); |
| 1594 | |
| 1595 | dst_reg = ®s[insn->dst_reg]; |
| 1596 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1597 | check_reg_overflow(®s[insn->src_reg]); |
| 1598 | min_val = regs[insn->src_reg].min_value; |
| 1599 | max_val = regs[insn->src_reg].max_value; |
| 1600 | |
| 1601 | /* If the source register is a random pointer then the |
| 1602 | * min_value/max_value values represent the range of the known |
| 1603 | * accesses into that value, not the actual min/max value of the |
| 1604 | * register itself. In this case we have to reset the reg range |
| 1605 | * values so we know it is not safe to look at. |
| 1606 | */ |
| 1607 | if (regs[insn->src_reg].type != CONST_IMM && |
| 1608 | regs[insn->src_reg].type != UNKNOWN_VALUE) { |
| 1609 | min_val = BPF_REGISTER_MIN_RANGE; |
| 1610 | max_val = BPF_REGISTER_MAX_RANGE; |
| 1611 | } |
| 1612 | } else if (insn->imm < BPF_REGISTER_MAX_RANGE && |
| 1613 | (s64)insn->imm > BPF_REGISTER_MIN_RANGE) { |
| 1614 | min_val = max_val = insn->imm; |
| 1615 | min_set = max_set = true; |
| 1616 | } |
| 1617 | |
| 1618 | /* We don't know anything about what was done to this register, mark it |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1619 | * as unknown. Also, if both derived bounds came from signed/unsigned |
| 1620 | * mixed compares and one side is unbounded, we cannot really do anything |
| 1621 | * with them as boundaries cannot be trusted. Thus, arithmetic of two |
| 1622 | * regs of such kind will get invalidated bounds on the dst side. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1623 | */ |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1624 | if ((min_val == BPF_REGISTER_MIN_RANGE && |
| 1625 | max_val == BPF_REGISTER_MAX_RANGE) || |
| 1626 | (BPF_SRC(insn->code) == BPF_X && |
| 1627 | ((min_val != BPF_REGISTER_MIN_RANGE && |
| 1628 | max_val == BPF_REGISTER_MAX_RANGE) || |
| 1629 | (min_val == BPF_REGISTER_MIN_RANGE && |
| 1630 | max_val != BPF_REGISTER_MAX_RANGE) || |
| 1631 | (dst_reg->min_value != BPF_REGISTER_MIN_RANGE && |
| 1632 | dst_reg->max_value == BPF_REGISTER_MAX_RANGE) || |
| 1633 | (dst_reg->min_value == BPF_REGISTER_MIN_RANGE && |
| 1634 | dst_reg->max_value != BPF_REGISTER_MAX_RANGE)) && |
| 1635 | regs[insn->dst_reg].value_from_signed != |
| 1636 | regs[insn->src_reg].value_from_signed)) { |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1637 | reset_reg_range_values(regs, insn->dst_reg); |
| 1638 | return; |
| 1639 | } |
| 1640 | |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1641 | /* If one of our values was at the end of our ranges then we can't just |
| 1642 | * do our normal operations to the register, we need to set the values |
| 1643 | * to the min/max since they are undefined. |
| 1644 | */ |
Edward Cree | 655da3d | 2017-07-21 14:37:34 +0100 | [diff] [blame] | 1645 | if (opcode != BPF_SUB) { |
| 1646 | if (min_val == BPF_REGISTER_MIN_RANGE) |
| 1647 | dst_reg->min_value = BPF_REGISTER_MIN_RANGE; |
| 1648 | if (max_val == BPF_REGISTER_MAX_RANGE) |
| 1649 | dst_reg->max_value = BPF_REGISTER_MAX_RANGE; |
| 1650 | } |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1651 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1652 | switch (opcode) { |
| 1653 | case BPF_ADD: |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1654 | if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE) |
| 1655 | dst_reg->min_value += min_val; |
| 1656 | if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE) |
| 1657 | dst_reg->max_value += max_val; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1658 | break; |
| 1659 | case BPF_SUB: |
Edward Cree | 655da3d | 2017-07-21 14:37:34 +0100 | [diff] [blame] | 1660 | /* If one of our values was at the end of our ranges, then the |
| 1661 | * _opposite_ value in the dst_reg goes to the end of our range. |
| 1662 | */ |
| 1663 | if (min_val == BPF_REGISTER_MIN_RANGE) |
| 1664 | dst_reg->max_value = BPF_REGISTER_MAX_RANGE; |
| 1665 | if (max_val == BPF_REGISTER_MAX_RANGE) |
| 1666 | dst_reg->min_value = BPF_REGISTER_MIN_RANGE; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1667 | if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE) |
Edward Cree | 655da3d | 2017-07-21 14:37:34 +0100 | [diff] [blame] | 1668 | dst_reg->min_value -= max_val; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1669 | if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE) |
Edward Cree | 655da3d | 2017-07-21 14:37:34 +0100 | [diff] [blame] | 1670 | dst_reg->max_value -= min_val; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1671 | break; |
| 1672 | case BPF_MUL: |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1673 | if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE) |
| 1674 | dst_reg->min_value *= min_val; |
| 1675 | if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE) |
| 1676 | dst_reg->max_value *= max_val; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1677 | break; |
| 1678 | case BPF_AND: |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1679 | /* Disallow AND'ing of negative numbers, ain't nobody got time |
| 1680 | * for that. Otherwise the minimum is 0 and the max is the max |
| 1681 | * value we could AND against. |
| 1682 | */ |
| 1683 | if (min_val < 0) |
| 1684 | dst_reg->min_value = BPF_REGISTER_MIN_RANGE; |
| 1685 | else |
| 1686 | dst_reg->min_value = 0; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1687 | dst_reg->max_value = max_val; |
| 1688 | break; |
| 1689 | case BPF_LSH: |
| 1690 | /* Gotta have special overflow logic here, if we're shifting |
| 1691 | * more than MAX_RANGE then just assume we have an invalid |
| 1692 | * range. |
| 1693 | */ |
| 1694 | if (min_val > ilog2(BPF_REGISTER_MAX_RANGE)) |
| 1695 | dst_reg->min_value = BPF_REGISTER_MIN_RANGE; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1696 | else if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1697 | dst_reg->min_value <<= min_val; |
| 1698 | |
| 1699 | if (max_val > ilog2(BPF_REGISTER_MAX_RANGE)) |
| 1700 | dst_reg->max_value = BPF_REGISTER_MAX_RANGE; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1701 | else if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1702 | dst_reg->max_value <<= max_val; |
| 1703 | break; |
| 1704 | case BPF_RSH: |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1705 | /* RSH by a negative number is undefined, and the BPF_RSH is an |
| 1706 | * unsigned shift, so make the appropriate casts. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1707 | */ |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1708 | if (min_val < 0 || dst_reg->min_value < 0) |
| 1709 | dst_reg->min_value = BPF_REGISTER_MIN_RANGE; |
| 1710 | else |
| 1711 | dst_reg->min_value = |
| 1712 | (u64)(dst_reg->min_value) >> min_val; |
| 1713 | if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE) |
| 1714 | dst_reg->max_value >>= max_val; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1715 | break; |
| 1716 | default: |
| 1717 | reset_reg_range_values(regs, insn->dst_reg); |
| 1718 | break; |
| 1719 | } |
| 1720 | |
| 1721 | check_reg_overflow(dst_reg); |
| 1722 | } |
| 1723 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1724 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1725 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1726 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1727 | struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1728 | u8 opcode = BPF_OP(insn->code); |
| 1729 | int err; |
| 1730 | |
| 1731 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 1732 | if (opcode == BPF_NEG) { |
| 1733 | if (BPF_SRC(insn->code) != 0 || |
| 1734 | insn->src_reg != BPF_REG_0 || |
| 1735 | insn->off != 0 || insn->imm != 0) { |
| 1736 | verbose("BPF_NEG uses reserved fields\n"); |
| 1737 | return -EINVAL; |
| 1738 | } |
| 1739 | } else { |
| 1740 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
Edward Cree | e159492 | 2017-09-15 14:37:38 +0100 | [diff] [blame] | 1741 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
| 1742 | BPF_CLASS(insn->code) == BPF_ALU64) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1743 | verbose("BPF_END uses reserved fields\n"); |
| 1744 | return -EINVAL; |
| 1745 | } |
| 1746 | } |
| 1747 | |
| 1748 | /* check src operand */ |
| 1749 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 1750 | if (err) |
| 1751 | return err; |
| 1752 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1753 | if (is_pointer_value(env, insn->dst_reg)) { |
| 1754 | verbose("R%d pointer arithmetic prohibited\n", |
| 1755 | insn->dst_reg); |
| 1756 | return -EACCES; |
| 1757 | } |
| 1758 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1759 | /* check dest operand */ |
| 1760 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); |
| 1761 | if (err) |
| 1762 | return err; |
| 1763 | |
| 1764 | } else if (opcode == BPF_MOV) { |
| 1765 | |
| 1766 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1767 | if (insn->imm != 0 || insn->off != 0) { |
| 1768 | verbose("BPF_MOV uses reserved fields\n"); |
| 1769 | return -EINVAL; |
| 1770 | } |
| 1771 | |
| 1772 | /* check src operand */ |
| 1773 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 1774 | if (err) |
| 1775 | return err; |
| 1776 | } else { |
| 1777 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
| 1778 | verbose("BPF_MOV uses reserved fields\n"); |
| 1779 | return -EINVAL; |
| 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | /* check dest operand */ |
| 1784 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); |
| 1785 | if (err) |
| 1786 | return err; |
| 1787 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1788 | /* we are setting our register to something new, we need to |
| 1789 | * reset its range values. |
| 1790 | */ |
| 1791 | reset_reg_range_values(regs, insn->dst_reg); |
| 1792 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1793 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1794 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 1795 | /* case: R1 = R2 |
| 1796 | * copy register state to dest reg |
| 1797 | */ |
| 1798 | regs[insn->dst_reg] = regs[insn->src_reg]; |
| 1799 | } else { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1800 | if (is_pointer_value(env, insn->src_reg)) { |
| 1801 | verbose("R%d partial copy of pointer\n", |
| 1802 | insn->src_reg); |
| 1803 | return -EACCES; |
| 1804 | } |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 1805 | mark_reg_unknown_value(regs, insn->dst_reg); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1806 | } |
| 1807 | } else { |
| 1808 | /* case: R = imm |
| 1809 | * remember the value we stored into this reg |
| 1810 | */ |
Daniel Borkmann | 3695b3b | 2017-12-22 16:29:05 +0100 | [diff] [blame] | 1811 | u64 imm; |
| 1812 | |
| 1813 | if (BPF_CLASS(insn->code) == BPF_ALU64) |
| 1814 | imm = insn->imm; |
| 1815 | else |
| 1816 | imm = (u32)insn->imm; |
| 1817 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1818 | regs[insn->dst_reg].type = CONST_IMM; |
Daniel Borkmann | 3695b3b | 2017-12-22 16:29:05 +0100 | [diff] [blame] | 1819 | regs[insn->dst_reg].imm = imm; |
| 1820 | regs[insn->dst_reg].max_value = imm; |
| 1821 | regs[insn->dst_reg].min_value = imm; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | } else if (opcode > BPF_END) { |
| 1825 | verbose("invalid BPF_ALU opcode %x\n", opcode); |
| 1826 | return -EINVAL; |
| 1827 | |
| 1828 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 1829 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1830 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1831 | if (insn->imm != 0 || insn->off != 0) { |
| 1832 | verbose("BPF_ALU uses reserved fields\n"); |
| 1833 | return -EINVAL; |
| 1834 | } |
| 1835 | /* check src1 operand */ |
| 1836 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 1837 | if (err) |
| 1838 | return err; |
| 1839 | } else { |
| 1840 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
| 1841 | verbose("BPF_ALU uses reserved fields\n"); |
| 1842 | return -EINVAL; |
| 1843 | } |
| 1844 | } |
| 1845 | |
| 1846 | /* check src2 operand */ |
| 1847 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 1848 | if (err) |
| 1849 | return err; |
| 1850 | |
| 1851 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 1852 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
| 1853 | verbose("div by zero\n"); |
| 1854 | return -EINVAL; |
| 1855 | } |
| 1856 | |
Daniel Borkmann | fcabc6d | 2018-01-29 02:48:57 +0100 | [diff] [blame] | 1857 | if (opcode == BPF_ARSH && BPF_CLASS(insn->code) != BPF_ALU64) { |
| 1858 | verbose("BPF_ARSH not supported for 32 bit ALU\n"); |
| 1859 | return -EINVAL; |
| 1860 | } |
| 1861 | |
Rabin Vincent | 229394e | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 1862 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
| 1863 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { |
| 1864 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; |
| 1865 | |
| 1866 | if (insn->imm < 0 || insn->imm >= size) { |
| 1867 | verbose("invalid shift %d\n", insn->imm); |
| 1868 | return -EINVAL; |
| 1869 | } |
| 1870 | } |
| 1871 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1872 | /* check dest operand */ |
| 1873 | err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK); |
| 1874 | if (err) |
| 1875 | return err; |
| 1876 | |
| 1877 | dst_reg = ®s[insn->dst_reg]; |
| 1878 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1879 | /* first we want to adjust our ranges. */ |
| 1880 | adjust_reg_min_max_vals(env, insn); |
| 1881 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1882 | /* pattern match 'bpf_add Rx, imm' instruction */ |
| 1883 | if (opcode == BPF_ADD && BPF_CLASS(insn->code) == BPF_ALU64 && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1884 | dst_reg->type == FRAME_PTR && BPF_SRC(insn->code) == BPF_K) { |
| 1885 | dst_reg->type = PTR_TO_STACK; |
| 1886 | dst_reg->imm = insn->imm; |
| 1887 | return 0; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1888 | } else if (opcode == BPF_ADD && |
| 1889 | BPF_CLASS(insn->code) == BPF_ALU64 && |
Yonghong Song | 7bca0a9 | 2017-04-29 22:52:42 -0700 | [diff] [blame] | 1890 | dst_reg->type == PTR_TO_STACK && |
| 1891 | ((BPF_SRC(insn->code) == BPF_X && |
| 1892 | regs[insn->src_reg].type == CONST_IMM) || |
| 1893 | BPF_SRC(insn->code) == BPF_K)) { |
Daniel Borkmann | d75d3ee | 2017-12-22 16:29:04 +0100 | [diff] [blame] | 1894 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1895 | /* check in case the register contains a big |
| 1896 | * 64-bit value |
| 1897 | */ |
| 1898 | if (regs[insn->src_reg].imm < -MAX_BPF_STACK || |
| 1899 | regs[insn->src_reg].imm > MAX_BPF_STACK) { |
| 1900 | verbose("R%d value too big in R%d pointer arithmetic\n", |
| 1901 | insn->src_reg, insn->dst_reg); |
| 1902 | return -EACCES; |
| 1903 | } |
Yonghong Song | 7bca0a9 | 2017-04-29 22:52:42 -0700 | [diff] [blame] | 1904 | dst_reg->imm += regs[insn->src_reg].imm; |
Daniel Borkmann | d75d3ee | 2017-12-22 16:29:04 +0100 | [diff] [blame] | 1905 | } else { |
| 1906 | /* safe against overflow: addition of 32-bit |
| 1907 | * numbers in 64-bit representation |
| 1908 | */ |
Yonghong Song | 7bca0a9 | 2017-04-29 22:52:42 -0700 | [diff] [blame] | 1909 | dst_reg->imm += insn->imm; |
Daniel Borkmann | d75d3ee | 2017-12-22 16:29:04 +0100 | [diff] [blame] | 1910 | } |
| 1911 | if (dst_reg->imm > 0 || dst_reg->imm < -MAX_BPF_STACK) { |
| 1912 | verbose("R%d out-of-bounds pointer arithmetic\n", |
| 1913 | insn->dst_reg); |
| 1914 | return -EACCES; |
| 1915 | } |
Yonghong Song | 7bca0a9 | 2017-04-29 22:52:42 -0700 | [diff] [blame] | 1916 | return 0; |
| 1917 | } else if (opcode == BPF_ADD && |
| 1918 | BPF_CLASS(insn->code) == BPF_ALU64 && |
Alexei Starovoitov | 1b9b69e | 2016-05-19 18:17:14 -0700 | [diff] [blame] | 1919 | (dst_reg->type == PTR_TO_PACKET || |
| 1920 | (BPF_SRC(insn->code) == BPF_X && |
| 1921 | regs[insn->src_reg].type == PTR_TO_PACKET))) { |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1922 | /* ptr_to_packet += K|X */ |
| 1923 | return check_packet_ptr_add(env, insn); |
| 1924 | } else if (BPF_CLASS(insn->code) == BPF_ALU64 && |
| 1925 | dst_reg->type == UNKNOWN_VALUE && |
| 1926 | env->allow_ptr_leaks) { |
| 1927 | /* unknown += K|X */ |
| 1928 | return evaluate_reg_alu(env, insn); |
| 1929 | } else if (BPF_CLASS(insn->code) == BPF_ALU64 && |
| 1930 | dst_reg->type == CONST_IMM && |
| 1931 | env->allow_ptr_leaks) { |
| 1932 | /* reg_imm += K|X */ |
| 1933 | return evaluate_reg_imm_alu(env, insn); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1934 | } else if (is_pointer_value(env, insn->dst_reg)) { |
| 1935 | verbose("R%d pointer arithmetic prohibited\n", |
| 1936 | insn->dst_reg); |
| 1937 | return -EACCES; |
| 1938 | } else if (BPF_SRC(insn->code) == BPF_X && |
| 1939 | is_pointer_value(env, insn->src_reg)) { |
| 1940 | verbose("R%d pointer arithmetic prohibited\n", |
| 1941 | insn->src_reg); |
| 1942 | return -EACCES; |
| 1943 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1944 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1945 | /* If we did pointer math on a map value then just set it to our |
| 1946 | * PTR_TO_MAP_VALUE_ADJ type so we can deal with any stores or |
| 1947 | * loads to this register appropriately, otherwise just mark the |
| 1948 | * register as unknown. |
| 1949 | */ |
| 1950 | if (env->allow_ptr_leaks && |
Daniel Borkmann | 8d674be | 2017-03-31 02:24:02 +0200 | [diff] [blame] | 1951 | BPF_CLASS(insn->code) == BPF_ALU64 && opcode == BPF_ADD && |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1952 | (dst_reg->type == PTR_TO_MAP_VALUE || |
| 1953 | dst_reg->type == PTR_TO_MAP_VALUE_ADJ)) |
| 1954 | dst_reg->type = PTR_TO_MAP_VALUE_ADJ; |
| 1955 | else |
| 1956 | mark_reg_unknown_value(regs, insn->dst_reg); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1957 | } |
| 1958 | |
| 1959 | return 0; |
| 1960 | } |
| 1961 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1962 | static void find_good_pkt_pointers(struct bpf_verifier_state *state, |
| 1963 | struct bpf_reg_state *dst_reg) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1964 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1965 | struct bpf_reg_state *regs = state->regs, *reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1966 | int i; |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 1967 | |
| 1968 | /* LLVM can generate two kind of checks: |
| 1969 | * |
| 1970 | * Type 1: |
| 1971 | * |
| 1972 | * r2 = r3; |
| 1973 | * r2 += 8; |
| 1974 | * if (r2 > pkt_end) goto <handle exception> |
| 1975 | * <access okay> |
| 1976 | * |
| 1977 | * Where: |
| 1978 | * r2 == dst_reg, pkt_end == src_reg |
| 1979 | * r2=pkt(id=n,off=8,r=0) |
| 1980 | * r3=pkt(id=n,off=0,r=0) |
| 1981 | * |
| 1982 | * Type 2: |
| 1983 | * |
| 1984 | * r2 = r3; |
| 1985 | * r2 += 8; |
| 1986 | * if (pkt_end >= r2) goto <access okay> |
| 1987 | * <handle exception> |
| 1988 | * |
| 1989 | * Where: |
| 1990 | * pkt_end == dst_reg, r2 == src_reg |
| 1991 | * r2=pkt(id=n,off=8,r=0) |
| 1992 | * r3=pkt(id=n,off=0,r=0) |
| 1993 | * |
| 1994 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) |
| 1995 | * so that range of bytes [r3, r3 + 8) is safe to access. |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1996 | */ |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 1997 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1998 | for (i = 0; i < MAX_BPF_REG; i++) |
| 1999 | if (regs[i].type == PTR_TO_PACKET && regs[i].id == dst_reg->id) |
Alexei Starovoitov | 0ea3c23 | 2017-03-24 15:57:33 -0700 | [diff] [blame] | 2000 | /* keep the maximum range already checked */ |
| 2001 | regs[i].range = max(regs[i].range, dst_reg->off); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2002 | |
| 2003 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { |
| 2004 | if (state->stack_slot_type[i] != STACK_SPILL) |
| 2005 | continue; |
| 2006 | reg = &state->spilled_regs[i / BPF_REG_SIZE]; |
| 2007 | if (reg->type == PTR_TO_PACKET && reg->id == dst_reg->id) |
Alexei Starovoitov | 0ea3c23 | 2017-03-24 15:57:33 -0700 | [diff] [blame] | 2008 | reg->range = max(reg->range, dst_reg->off); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2009 | } |
| 2010 | } |
| 2011 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2012 | /* Adjusts the register min/max values in the case that the dst_reg is the |
| 2013 | * variable register that we are working on, and src_reg is a constant or we're |
| 2014 | * simply doing a BPF_K check. |
| 2015 | */ |
| 2016 | static void reg_set_min_max(struct bpf_reg_state *true_reg, |
| 2017 | struct bpf_reg_state *false_reg, u64 val, |
| 2018 | u8 opcode) |
| 2019 | { |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2020 | bool value_from_signed = true; |
| 2021 | bool is_range = true; |
| 2022 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2023 | switch (opcode) { |
| 2024 | case BPF_JEQ: |
| 2025 | /* If this is false then we know nothing Jon Snow, but if it is |
| 2026 | * true then we know for sure. |
| 2027 | */ |
| 2028 | true_reg->max_value = true_reg->min_value = val; |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2029 | is_range = false; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2030 | break; |
| 2031 | case BPF_JNE: |
| 2032 | /* If this is true we know nothing Jon Snow, but if it is false |
| 2033 | * we know the value for sure; |
| 2034 | */ |
| 2035 | false_reg->max_value = false_reg->min_value = val; |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2036 | is_range = false; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2037 | break; |
| 2038 | case BPF_JGT: |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2039 | value_from_signed = false; |
| 2040 | /* fallthrough */ |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2041 | case BPF_JSGT: |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2042 | if (true_reg->value_from_signed != value_from_signed) |
| 2043 | reset_reg_range_values(true_reg, 0); |
| 2044 | if (false_reg->value_from_signed != value_from_signed) |
| 2045 | reset_reg_range_values(false_reg, 0); |
| 2046 | if (opcode == BPF_JGT) { |
| 2047 | /* Unsigned comparison, the minimum value is 0. */ |
| 2048 | false_reg->min_value = 0; |
| 2049 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2050 | /* If this is false then we know the maximum val is val, |
| 2051 | * otherwise we know the min val is val+1. |
| 2052 | */ |
| 2053 | false_reg->max_value = val; |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2054 | false_reg->value_from_signed = value_from_signed; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2055 | true_reg->min_value = val + 1; |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2056 | true_reg->value_from_signed = value_from_signed; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2057 | break; |
| 2058 | case BPF_JGE: |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2059 | value_from_signed = false; |
| 2060 | /* fallthrough */ |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2061 | case BPF_JSGE: |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2062 | if (true_reg->value_from_signed != value_from_signed) |
| 2063 | reset_reg_range_values(true_reg, 0); |
| 2064 | if (false_reg->value_from_signed != value_from_signed) |
| 2065 | reset_reg_range_values(false_reg, 0); |
| 2066 | if (opcode == BPF_JGE) { |
| 2067 | /* Unsigned comparison, the minimum value is 0. */ |
| 2068 | false_reg->min_value = 0; |
| 2069 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2070 | /* If this is false then we know the maximum value is val - 1, |
| 2071 | * otherwise we know the mimimum value is val. |
| 2072 | */ |
| 2073 | false_reg->max_value = val - 1; |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2074 | false_reg->value_from_signed = value_from_signed; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2075 | true_reg->min_value = val; |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2076 | true_reg->value_from_signed = value_from_signed; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2077 | break; |
| 2078 | default: |
| 2079 | break; |
| 2080 | } |
| 2081 | |
| 2082 | check_reg_overflow(false_reg); |
| 2083 | check_reg_overflow(true_reg); |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2084 | if (is_range) { |
| 2085 | if (__is_pointer_value(false, false_reg)) |
| 2086 | reset_reg_range_values(false_reg, 0); |
| 2087 | if (__is_pointer_value(false, true_reg)) |
| 2088 | reset_reg_range_values(true_reg, 0); |
| 2089 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | /* Same as above, but for the case that dst_reg is a CONST_IMM reg and src_reg |
| 2093 | * is the variable reg. |
| 2094 | */ |
| 2095 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, |
| 2096 | struct bpf_reg_state *false_reg, u64 val, |
| 2097 | u8 opcode) |
| 2098 | { |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2099 | bool value_from_signed = true; |
| 2100 | bool is_range = true; |
| 2101 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2102 | switch (opcode) { |
| 2103 | case BPF_JEQ: |
| 2104 | /* If this is false then we know nothing Jon Snow, but if it is |
| 2105 | * true then we know for sure. |
| 2106 | */ |
| 2107 | true_reg->max_value = true_reg->min_value = val; |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2108 | is_range = false; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2109 | break; |
| 2110 | case BPF_JNE: |
| 2111 | /* If this is true we know nothing Jon Snow, but if it is false |
| 2112 | * we know the value for sure; |
| 2113 | */ |
| 2114 | false_reg->max_value = false_reg->min_value = val; |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2115 | is_range = false; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2116 | break; |
| 2117 | case BPF_JGT: |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2118 | value_from_signed = false; |
| 2119 | /* fallthrough */ |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2120 | case BPF_JSGT: |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2121 | if (true_reg->value_from_signed != value_from_signed) |
| 2122 | reset_reg_range_values(true_reg, 0); |
| 2123 | if (false_reg->value_from_signed != value_from_signed) |
| 2124 | reset_reg_range_values(false_reg, 0); |
| 2125 | if (opcode == BPF_JGT) { |
| 2126 | /* Unsigned comparison, the minimum value is 0. */ |
| 2127 | true_reg->min_value = 0; |
| 2128 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2129 | /* |
| 2130 | * If this is false, then the val is <= the register, if it is |
| 2131 | * true the register <= to the val. |
| 2132 | */ |
| 2133 | false_reg->min_value = val; |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2134 | false_reg->value_from_signed = value_from_signed; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2135 | true_reg->max_value = val - 1; |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2136 | true_reg->value_from_signed = value_from_signed; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2137 | break; |
| 2138 | case BPF_JGE: |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2139 | value_from_signed = false; |
| 2140 | /* fallthrough */ |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2141 | case BPF_JSGE: |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2142 | if (true_reg->value_from_signed != value_from_signed) |
| 2143 | reset_reg_range_values(true_reg, 0); |
| 2144 | if (false_reg->value_from_signed != value_from_signed) |
| 2145 | reset_reg_range_values(false_reg, 0); |
| 2146 | if (opcode == BPF_JGE) { |
| 2147 | /* Unsigned comparison, the minimum value is 0. */ |
| 2148 | true_reg->min_value = 0; |
| 2149 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2150 | /* If this is false then constant < register, if it is true then |
| 2151 | * the register < constant. |
| 2152 | */ |
| 2153 | false_reg->min_value = val + 1; |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2154 | false_reg->value_from_signed = value_from_signed; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2155 | true_reg->max_value = val; |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2156 | true_reg->value_from_signed = value_from_signed; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2157 | break; |
| 2158 | default: |
| 2159 | break; |
| 2160 | } |
| 2161 | |
| 2162 | check_reg_overflow(false_reg); |
| 2163 | check_reg_overflow(true_reg); |
Daniel Borkmann | bf5b91b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2164 | if (is_range) { |
| 2165 | if (__is_pointer_value(false, false_reg)) |
| 2166 | reset_reg_range_values(false_reg, 0); |
| 2167 | if (__is_pointer_value(false, true_reg)) |
| 2168 | reset_reg_range_values(true_reg, 0); |
| 2169 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2170 | } |
| 2171 | |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2172 | static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id, |
| 2173 | enum bpf_reg_type type) |
| 2174 | { |
| 2175 | struct bpf_reg_state *reg = ®s[regno]; |
| 2176 | |
| 2177 | if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) { |
| 2178 | reg->type = type; |
Daniel Borkmann | 1889d6d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 2179 | /* We don't need id from this point onwards anymore, thus we |
| 2180 | * should better reset it, so that state pruning has chances |
| 2181 | * to take effect. |
| 2182 | */ |
| 2183 | reg->id = 0; |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2184 | if (type == UNKNOWN_VALUE) |
Daniel Borkmann | 0e0f1d6 | 2016-12-18 01:52:59 +0100 | [diff] [blame] | 2185 | __mark_reg_unknown_value(regs, regno); |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2186 | } |
| 2187 | } |
| 2188 | |
| 2189 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
| 2190 | * be folded together at some point. |
| 2191 | */ |
| 2192 | static void mark_map_regs(struct bpf_verifier_state *state, u32 regno, |
| 2193 | enum bpf_reg_type type) |
| 2194 | { |
| 2195 | struct bpf_reg_state *regs = state->regs; |
Daniel Borkmann | 1889d6d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 2196 | u32 id = regs[regno].id; |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2197 | int i; |
| 2198 | |
| 2199 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | 1889d6d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 2200 | mark_map_reg(regs, i, id, type); |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2201 | |
| 2202 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { |
| 2203 | if (state->stack_slot_type[i] != STACK_SPILL) |
| 2204 | continue; |
Daniel Borkmann | 1889d6d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 2205 | mark_map_reg(state->spilled_regs, i / BPF_REG_SIZE, id, type); |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2206 | } |
| 2207 | } |
| 2208 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2209 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2210 | struct bpf_insn *insn, int *insn_idx) |
| 2211 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2212 | struct bpf_verifier_state *other_branch, *this_branch = &env->cur_state; |
| 2213 | struct bpf_reg_state *regs = this_branch->regs, *dst_reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2214 | u8 opcode = BPF_OP(insn->code); |
| 2215 | int err; |
| 2216 | |
| 2217 | if (opcode > BPF_EXIT) { |
| 2218 | verbose("invalid BPF_JMP opcode %x\n", opcode); |
| 2219 | return -EINVAL; |
| 2220 | } |
| 2221 | |
| 2222 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2223 | if (insn->imm != 0) { |
| 2224 | verbose("BPF_JMP uses reserved fields\n"); |
| 2225 | return -EINVAL; |
| 2226 | } |
| 2227 | |
| 2228 | /* check src1 operand */ |
| 2229 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 2230 | if (err) |
| 2231 | return err; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2232 | |
| 2233 | if (is_pointer_value(env, insn->src_reg)) { |
| 2234 | verbose("R%d pointer comparison prohibited\n", |
| 2235 | insn->src_reg); |
| 2236 | return -EACCES; |
| 2237 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2238 | } else { |
| 2239 | if (insn->src_reg != BPF_REG_0) { |
| 2240 | verbose("BPF_JMP uses reserved fields\n"); |
| 2241 | return -EINVAL; |
| 2242 | } |
| 2243 | } |
| 2244 | |
| 2245 | /* check src2 operand */ |
| 2246 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 2247 | if (err) |
| 2248 | return err; |
| 2249 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2250 | dst_reg = ®s[insn->dst_reg]; |
| 2251 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2252 | /* detect if R == 0 where R was initialized to zero earlier */ |
| 2253 | if (BPF_SRC(insn->code) == BPF_K && |
| 2254 | (opcode == BPF_JEQ || opcode == BPF_JNE) && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2255 | dst_reg->type == CONST_IMM && dst_reg->imm == insn->imm) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2256 | if (opcode == BPF_JEQ) { |
| 2257 | /* if (imm == imm) goto pc+off; |
| 2258 | * only follow the goto, ignore fall-through |
| 2259 | */ |
| 2260 | *insn_idx += insn->off; |
| 2261 | return 0; |
| 2262 | } else { |
| 2263 | /* if (imm != imm) goto pc+off; |
| 2264 | * only follow fall-through branch, since |
| 2265 | * that's where the program will go |
| 2266 | */ |
| 2267 | return 0; |
| 2268 | } |
| 2269 | } |
| 2270 | |
| 2271 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx); |
| 2272 | if (!other_branch) |
| 2273 | return -EFAULT; |
| 2274 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2275 | /* detect if we are comparing against a constant value so we can adjust |
| 2276 | * our min/max values for our dst register. |
| 2277 | */ |
| 2278 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2279 | if (regs[insn->src_reg].type == CONST_IMM) |
| 2280 | reg_set_min_max(&other_branch->regs[insn->dst_reg], |
| 2281 | dst_reg, regs[insn->src_reg].imm, |
| 2282 | opcode); |
| 2283 | else if (dst_reg->type == CONST_IMM) |
| 2284 | reg_set_min_max_inv(&other_branch->regs[insn->src_reg], |
| 2285 | ®s[insn->src_reg], dst_reg->imm, |
| 2286 | opcode); |
| 2287 | } else { |
| 2288 | reg_set_min_max(&other_branch->regs[insn->dst_reg], |
| 2289 | dst_reg, insn->imm, opcode); |
| 2290 | } |
| 2291 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2292 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2293 | if (BPF_SRC(insn->code) == BPF_K && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2294 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
| 2295 | dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2296 | /* Mark all identical map registers in each branch as either |
| 2297 | * safe or unknown depending R == 0 or R != 0 conditional. |
| 2298 | */ |
| 2299 | mark_map_regs(this_branch, insn->dst_reg, |
| 2300 | opcode == BPF_JEQ ? PTR_TO_MAP_VALUE : UNKNOWN_VALUE); |
| 2301 | mark_map_regs(other_branch, insn->dst_reg, |
| 2302 | opcode == BPF_JEQ ? UNKNOWN_VALUE : PTR_TO_MAP_VALUE); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2303 | } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT && |
| 2304 | dst_reg->type == PTR_TO_PACKET && |
| 2305 | regs[insn->src_reg].type == PTR_TO_PACKET_END) { |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 2306 | find_good_pkt_pointers(this_branch, dst_reg); |
| 2307 | } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGE && |
| 2308 | dst_reg->type == PTR_TO_PACKET_END && |
| 2309 | regs[insn->src_reg].type == PTR_TO_PACKET) { |
| 2310 | find_good_pkt_pointers(other_branch, ®s[insn->src_reg]); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2311 | } else if (is_pointer_value(env, insn->dst_reg)) { |
| 2312 | verbose("R%d pointer comparison prohibited\n", insn->dst_reg); |
| 2313 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2314 | } |
| 2315 | if (log_level) |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 2316 | print_verifier_state(this_branch); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2317 | return 0; |
| 2318 | } |
| 2319 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2320 | /* return the map pointer stored inside BPF_LD_IMM64 instruction */ |
| 2321 | static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn) |
| 2322 | { |
| 2323 | u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32; |
| 2324 | |
| 2325 | return (struct bpf_map *) (unsigned long) imm64; |
| 2326 | } |
| 2327 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2328 | /* verify BPF_LD_IMM64 instruction */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2329 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2330 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2331 | struct bpf_reg_state *regs = env->cur_state.regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2332 | int err; |
| 2333 | |
| 2334 | if (BPF_SIZE(insn->code) != BPF_DW) { |
| 2335 | verbose("invalid BPF_LD_IMM insn\n"); |
| 2336 | return -EINVAL; |
| 2337 | } |
| 2338 | if (insn->off != 0) { |
| 2339 | verbose("BPF_LD_IMM64 uses reserved fields\n"); |
| 2340 | return -EINVAL; |
| 2341 | } |
| 2342 | |
| 2343 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); |
| 2344 | if (err) |
| 2345 | return err; |
| 2346 | |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 2347 | if (insn->src_reg == 0) { |
| 2348 | /* generic move 64-bit immediate into a register, |
| 2349 | * only analyzer needs to collect the ld_imm value. |
| 2350 | */ |
| 2351 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 2352 | |
| 2353 | if (!env->analyzer_ops) |
| 2354 | return 0; |
| 2355 | |
| 2356 | regs[insn->dst_reg].type = CONST_IMM; |
| 2357 | regs[insn->dst_reg].imm = imm; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2358 | return 0; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 2359 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2360 | |
| 2361 | /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */ |
| 2362 | BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD); |
| 2363 | |
| 2364 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; |
| 2365 | regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn); |
| 2366 | return 0; |
| 2367 | } |
| 2368 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 2369 | static bool may_access_skb(enum bpf_prog_type type) |
| 2370 | { |
| 2371 | switch (type) { |
| 2372 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 2373 | case BPF_PROG_TYPE_SCHED_CLS: |
Daniel Borkmann | 94caee8 | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 2374 | case BPF_PROG_TYPE_SCHED_ACT: |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 2375 | return true; |
| 2376 | default: |
| 2377 | return false; |
| 2378 | } |
| 2379 | } |
| 2380 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 2381 | /* verify safety of LD_ABS|LD_IND instructions: |
| 2382 | * - they can only appear in the programs where ctx == skb |
| 2383 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 2384 | * preserve R6-R9, and store return value into R0 |
| 2385 | * |
| 2386 | * Implicit input: |
| 2387 | * ctx == skb == R6 == CTX |
| 2388 | * |
| 2389 | * Explicit input: |
| 2390 | * SRC == any register |
| 2391 | * IMM == 32-bit immediate |
| 2392 | * |
| 2393 | * Output: |
| 2394 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 2395 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2396 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 2397 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2398 | struct bpf_reg_state *regs = env->cur_state.regs; |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 2399 | u8 mode = BPF_MODE(insn->code); |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2400 | struct bpf_reg_state *reg; |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 2401 | int i, err; |
| 2402 | |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 2403 | if (!may_access_skb(env->prog->type)) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2404 | 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] | 2405 | return -EINVAL; |
| 2406 | } |
| 2407 | |
| 2408 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
Alexei Starovoitov | d82bccc | 2016-04-12 10:26:19 -0700 | [diff] [blame] | 2409 | BPF_SIZE(insn->code) == BPF_DW || |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 2410 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2411 | verbose("BPF_LD_[ABS|IND] uses reserved fields\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 2412 | return -EINVAL; |
| 2413 | } |
| 2414 | |
| 2415 | /* check whether implicit source operand (register R6) is readable */ |
| 2416 | err = check_reg_arg(regs, BPF_REG_6, SRC_OP); |
| 2417 | if (err) |
| 2418 | return err; |
| 2419 | |
| 2420 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { |
| 2421 | verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); |
| 2422 | return -EINVAL; |
| 2423 | } |
| 2424 | |
| 2425 | if (mode == BPF_IND) { |
| 2426 | /* check explicit source operand */ |
| 2427 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 2428 | if (err) |
| 2429 | return err; |
| 2430 | } |
| 2431 | |
| 2432 | /* reset caller saved regs to unreadable */ |
| 2433 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 2434 | reg = regs + caller_saved[i]; |
| 2435 | reg->type = NOT_INIT; |
| 2436 | reg->imm = 0; |
| 2437 | } |
| 2438 | |
| 2439 | /* mark destination R0 register as readable, since it contains |
| 2440 | * the value fetched from the packet |
| 2441 | */ |
| 2442 | regs[BPF_REG_0].type = UNKNOWN_VALUE; |
| 2443 | return 0; |
| 2444 | } |
| 2445 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 2446 | /* non-recursive DFS pseudo code |
| 2447 | * 1 procedure DFS-iterative(G,v): |
| 2448 | * 2 label v as discovered |
| 2449 | * 3 let S be a stack |
| 2450 | * 4 S.push(v) |
| 2451 | * 5 while S is not empty |
| 2452 | * 6 t <- S.pop() |
| 2453 | * 7 if t is what we're looking for: |
| 2454 | * 8 return t |
| 2455 | * 9 for all edges e in G.adjacentEdges(t) do |
| 2456 | * 10 if edge e is already labelled |
| 2457 | * 11 continue with the next edge |
| 2458 | * 12 w <- G.adjacentVertex(t,e) |
| 2459 | * 13 if vertex w is not discovered and not explored |
| 2460 | * 14 label e as tree-edge |
| 2461 | * 15 label w as discovered |
| 2462 | * 16 S.push(w) |
| 2463 | * 17 continue at 5 |
| 2464 | * 18 else if vertex w is discovered |
| 2465 | * 19 label e as back-edge |
| 2466 | * 20 else |
| 2467 | * 21 // vertex w is explored |
| 2468 | * 22 label e as forward- or cross-edge |
| 2469 | * 23 label t as explored |
| 2470 | * 24 S.pop() |
| 2471 | * |
| 2472 | * convention: |
| 2473 | * 0x10 - discovered |
| 2474 | * 0x11 - discovered and fall-through edge labelled |
| 2475 | * 0x12 - discovered and fall-through and branch edges labelled |
| 2476 | * 0x20 - explored |
| 2477 | */ |
| 2478 | |
| 2479 | enum { |
| 2480 | DISCOVERED = 0x10, |
| 2481 | EXPLORED = 0x20, |
| 2482 | FALLTHROUGH = 1, |
| 2483 | BRANCH = 2, |
| 2484 | }; |
| 2485 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2486 | #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2487 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 2488 | static int *insn_stack; /* stack of insns to process */ |
| 2489 | static int cur_stack; /* current stack index */ |
| 2490 | static int *insn_state; |
| 2491 | |
| 2492 | /* t, w, e - match pseudo-code above: |
| 2493 | * t - index of current instruction |
| 2494 | * w - next instruction |
| 2495 | * e - edge |
| 2496 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2497 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 2498 | { |
| 2499 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
| 2500 | return 0; |
| 2501 | |
| 2502 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
| 2503 | return 0; |
| 2504 | |
| 2505 | if (w < 0 || w >= env->prog->len) { |
| 2506 | verbose("jump out of range from insn %d to %d\n", t, w); |
| 2507 | return -EINVAL; |
| 2508 | } |
| 2509 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2510 | if (e == BRANCH) |
| 2511 | /* mark branch target for state pruning */ |
| 2512 | env->explored_states[w] = STATE_LIST_MARK; |
| 2513 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 2514 | if (insn_state[w] == 0) { |
| 2515 | /* tree-edge */ |
| 2516 | insn_state[t] = DISCOVERED | e; |
| 2517 | insn_state[w] = DISCOVERED; |
| 2518 | if (cur_stack >= env->prog->len) |
| 2519 | return -E2BIG; |
| 2520 | insn_stack[cur_stack++] = w; |
| 2521 | return 1; |
| 2522 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
| 2523 | verbose("back-edge from insn %d to %d\n", t, w); |
| 2524 | return -EINVAL; |
| 2525 | } else if (insn_state[w] == EXPLORED) { |
| 2526 | /* forward- or cross-edge */ |
| 2527 | insn_state[t] = DISCOVERED | e; |
| 2528 | } else { |
| 2529 | verbose("insn state internal bug\n"); |
| 2530 | return -EFAULT; |
| 2531 | } |
| 2532 | return 0; |
| 2533 | } |
| 2534 | |
| 2535 | /* non-recursive depth-first-search to detect loops in BPF program |
| 2536 | * loop == back-edge in directed graph |
| 2537 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2538 | static int check_cfg(struct bpf_verifier_env *env) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 2539 | { |
| 2540 | struct bpf_insn *insns = env->prog->insnsi; |
| 2541 | int insn_cnt = env->prog->len; |
| 2542 | int ret = 0; |
| 2543 | int i, t; |
| 2544 | |
| 2545 | insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 2546 | if (!insn_state) |
| 2547 | return -ENOMEM; |
| 2548 | |
| 2549 | insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 2550 | if (!insn_stack) { |
| 2551 | kfree(insn_state); |
| 2552 | return -ENOMEM; |
| 2553 | } |
| 2554 | |
| 2555 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 2556 | insn_stack[0] = 0; /* 0 is the first instruction */ |
| 2557 | cur_stack = 1; |
| 2558 | |
| 2559 | peek_stack: |
| 2560 | if (cur_stack == 0) |
| 2561 | goto check_state; |
| 2562 | t = insn_stack[cur_stack - 1]; |
| 2563 | |
| 2564 | if (BPF_CLASS(insns[t].code) == BPF_JMP) { |
| 2565 | u8 opcode = BPF_OP(insns[t].code); |
| 2566 | |
| 2567 | if (opcode == BPF_EXIT) { |
| 2568 | goto mark_explored; |
| 2569 | } else if (opcode == BPF_CALL) { |
| 2570 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 2571 | if (ret == 1) |
| 2572 | goto peek_stack; |
| 2573 | else if (ret < 0) |
| 2574 | goto err_free; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 2575 | if (t + 1 < insn_cnt) |
| 2576 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 2577 | } else if (opcode == BPF_JA) { |
| 2578 | if (BPF_SRC(insns[t].code) != BPF_K) { |
| 2579 | ret = -EINVAL; |
| 2580 | goto err_free; |
| 2581 | } |
| 2582 | /* unconditional jump with single edge */ |
| 2583 | ret = push_insn(t, t + insns[t].off + 1, |
| 2584 | FALLTHROUGH, env); |
| 2585 | if (ret == 1) |
| 2586 | goto peek_stack; |
| 2587 | else if (ret < 0) |
| 2588 | goto err_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2589 | /* tell verifier to check for equivalent states |
| 2590 | * after every call and jump |
| 2591 | */ |
Alexei Starovoitov | c3de631 | 2015-04-14 15:57:13 -0700 | [diff] [blame] | 2592 | if (t + 1 < insn_cnt) |
| 2593 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 2594 | } else { |
| 2595 | /* conditional jump with two edges */ |
Daniel Borkmann | 577aa83 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 2596 | env->explored_states[t] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 2597 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 2598 | if (ret == 1) |
| 2599 | goto peek_stack; |
| 2600 | else if (ret < 0) |
| 2601 | goto err_free; |
| 2602 | |
| 2603 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); |
| 2604 | if (ret == 1) |
| 2605 | goto peek_stack; |
| 2606 | else if (ret < 0) |
| 2607 | goto err_free; |
| 2608 | } |
| 2609 | } else { |
| 2610 | /* all other non-branch instructions with single |
| 2611 | * fall-through edge |
| 2612 | */ |
| 2613 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 2614 | if (ret == 1) |
| 2615 | goto peek_stack; |
| 2616 | else if (ret < 0) |
| 2617 | goto err_free; |
| 2618 | } |
| 2619 | |
| 2620 | mark_explored: |
| 2621 | insn_state[t] = EXPLORED; |
| 2622 | if (cur_stack-- <= 0) { |
| 2623 | verbose("pop stack internal bug\n"); |
| 2624 | ret = -EFAULT; |
| 2625 | goto err_free; |
| 2626 | } |
| 2627 | goto peek_stack; |
| 2628 | |
| 2629 | check_state: |
| 2630 | for (i = 0; i < insn_cnt; i++) { |
| 2631 | if (insn_state[i] != EXPLORED) { |
| 2632 | verbose("unreachable insn %d\n", i); |
| 2633 | ret = -EINVAL; |
| 2634 | goto err_free; |
| 2635 | } |
| 2636 | } |
| 2637 | ret = 0; /* cfg looks good */ |
| 2638 | |
| 2639 | err_free: |
| 2640 | kfree(insn_state); |
| 2641 | kfree(insn_stack); |
| 2642 | return ret; |
| 2643 | } |
| 2644 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2645 | /* the following conditions reduce the number of explored insns |
| 2646 | * from ~140k to ~80k for ultra large programs that use a lot of ptr_to_packet |
| 2647 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2648 | static bool compare_ptrs_to_packet(struct bpf_reg_state *old, |
| 2649 | struct bpf_reg_state *cur) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2650 | { |
| 2651 | if (old->id != cur->id) |
| 2652 | return false; |
| 2653 | |
| 2654 | /* old ptr_to_packet is more conservative, since it allows smaller |
| 2655 | * range. Ex: |
| 2656 | * old(off=0,r=10) is equal to cur(off=0,r=20), because |
| 2657 | * old(off=0,r=10) means that with range=10 the verifier proceeded |
| 2658 | * further and found no issues with the program. Now we're in the same |
| 2659 | * spot with cur(off=0,r=20), so we're safe too, since anything further |
| 2660 | * will only be looking at most 10 bytes after this pointer. |
| 2661 | */ |
| 2662 | if (old->off == cur->off && old->range < cur->range) |
| 2663 | return true; |
| 2664 | |
| 2665 | /* old(off=20,r=10) is equal to cur(off=22,re=22 or 5 or 0) |
| 2666 | * since both cannot be used for packet access and safe(old) |
| 2667 | * pointer has smaller off that could be used for further |
| 2668 | * 'if (ptr > data_end)' check |
| 2669 | * Ex: |
| 2670 | * old(off=20,r=10) and cur(off=22,r=22) and cur(off=22,r=0) mean |
| 2671 | * that we cannot access the packet. |
| 2672 | * The safe range is: |
| 2673 | * [ptr, ptr + range - off) |
| 2674 | * so whenever off >=range, it means no safe bytes from this pointer. |
| 2675 | * When comparing old->off <= cur->off, it means that older code |
| 2676 | * went with smaller offset and that offset was later |
| 2677 | * used to figure out the safe range after 'if (ptr > data_end)' check |
| 2678 | * Say, 'old' state was explored like: |
| 2679 | * ... R3(off=0, r=0) |
| 2680 | * R4 = R3 + 20 |
| 2681 | * ... now R4(off=20,r=0) <-- here |
| 2682 | * if (R4 > data_end) |
| 2683 | * ... R4(off=20,r=20), R3(off=0,r=20) and R3 can be used to access. |
| 2684 | * ... the code further went all the way to bpf_exit. |
| 2685 | * Now the 'cur' state at the mark 'here' has R4(off=30,r=0). |
| 2686 | * old_R4(off=20,r=0) equal to cur_R4(off=30,r=0), since if the verifier |
| 2687 | * goes further, such cur_R4 will give larger safe packet range after |
| 2688 | * 'if (R4 > data_end)' and all further insn were already good with r=20, |
| 2689 | * so they will be good with r=30 and we can prune the search. |
| 2690 | */ |
| 2691 | if (old->off <= cur->off && |
| 2692 | old->off >= old->range && cur->off >= cur->range) |
| 2693 | return true; |
| 2694 | |
| 2695 | return false; |
| 2696 | } |
| 2697 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2698 | /* compare two verifier states |
| 2699 | * |
| 2700 | * all states stored in state_list are known to be valid, since |
| 2701 | * verifier reached 'bpf_exit' instruction through them |
| 2702 | * |
| 2703 | * this function is called when verifier exploring different branches of |
| 2704 | * execution popped from the state stack. If it sees an old state that has |
| 2705 | * more strict register state and more strict stack state then this execution |
| 2706 | * branch doesn't need to be explored further, since verifier already |
| 2707 | * concluded that more strict state leads to valid finish. |
| 2708 | * |
| 2709 | * Therefore two states are equivalent if register state is more conservative |
| 2710 | * and explored stack state is more conservative than the current one. |
| 2711 | * Example: |
| 2712 | * explored current |
| 2713 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 2714 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 2715 | * |
| 2716 | * In other words if current stack state (one being explored) has more |
| 2717 | * valid slots than old one that already passed validation, it means |
| 2718 | * the verifier can stop exploring and conclude that current state is valid too |
| 2719 | * |
| 2720 | * Similarly with registers. If explored state has register type as invalid |
| 2721 | * whereas register type in current state is meaningful, it means that |
| 2722 | * the current state will reach 'bpf_exit' instruction safely |
| 2723 | */ |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2724 | static bool states_equal(struct bpf_verifier_env *env, |
| 2725 | struct bpf_verifier_state *old, |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2726 | struct bpf_verifier_state *cur) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2727 | { |
Josef Bacik | e2d2afe | 2016-11-29 12:27:09 -0500 | [diff] [blame] | 2728 | bool varlen_map_access = env->varlen_map_value_access; |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2729 | struct bpf_reg_state *rold, *rcur; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2730 | int i; |
| 2731 | |
| 2732 | for (i = 0; i < MAX_BPF_REG; i++) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2733 | rold = &old->regs[i]; |
| 2734 | rcur = &cur->regs[i]; |
| 2735 | |
| 2736 | if (memcmp(rold, rcur, sizeof(*rold)) == 0) |
| 2737 | continue; |
| 2738 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2739 | /* If the ranges were not the same, but everything else was and |
| 2740 | * we didn't do a variable access into a map then we are a-ok. |
| 2741 | */ |
Josef Bacik | e2d2afe | 2016-11-29 12:27:09 -0500 | [diff] [blame] | 2742 | if (!varlen_map_access && |
Alexei Starovoitov | b7f5aa1 | 2016-12-07 10:57:59 -0800 | [diff] [blame] | 2743 | memcmp(rold, rcur, offsetofend(struct bpf_reg_state, id)) == 0) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2744 | continue; |
| 2745 | |
Josef Bacik | e2d2afe | 2016-11-29 12:27:09 -0500 | [diff] [blame] | 2746 | /* If we didn't map access then again we don't care about the |
| 2747 | * mismatched range values and it's ok if our old type was |
Ben Hutchings | 37435f7 | 2017-12-23 02:26:17 +0000 | [diff] [blame] | 2748 | * UNKNOWN and we didn't go to a NOT_INIT'ed or pointer reg. |
Josef Bacik | e2d2afe | 2016-11-29 12:27:09 -0500 | [diff] [blame] | 2749 | */ |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2750 | if (rold->type == NOT_INIT || |
Josef Bacik | e2d2afe | 2016-11-29 12:27:09 -0500 | [diff] [blame] | 2751 | (!varlen_map_access && rold->type == UNKNOWN_VALUE && |
Ben Hutchings | 37435f7 | 2017-12-23 02:26:17 +0000 | [diff] [blame] | 2752 | rcur->type != NOT_INIT && |
| 2753 | !__is_pointer_value(env->allow_ptr_leaks, rcur))) |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2754 | continue; |
| 2755 | |
Daniel Borkmann | 577aa83 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 2756 | /* Don't care about the reg->id in this case. */ |
| 2757 | if (rold->type == PTR_TO_MAP_VALUE_OR_NULL && |
| 2758 | rcur->type == PTR_TO_MAP_VALUE_OR_NULL && |
| 2759 | rold->map_ptr == rcur->map_ptr) |
| 2760 | continue; |
| 2761 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2762 | if (rold->type == PTR_TO_PACKET && rcur->type == PTR_TO_PACKET && |
| 2763 | compare_ptrs_to_packet(rold, rcur)) |
| 2764 | continue; |
| 2765 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2766 | return false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2767 | } |
| 2768 | |
| 2769 | for (i = 0; i < MAX_BPF_STACK; i++) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2770 | if (old->stack_slot_type[i] == STACK_INVALID) |
| 2771 | continue; |
| 2772 | if (old->stack_slot_type[i] != cur->stack_slot_type[i]) |
| 2773 | /* Ex: old explored (safe) state has STACK_SPILL in |
| 2774 | * this stack slot, but current has has STACK_MISC -> |
| 2775 | * this verifier states are not equivalent, |
| 2776 | * return false to continue verification of this path |
| 2777 | */ |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2778 | return false; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2779 | if (i % BPF_REG_SIZE) |
| 2780 | continue; |
| 2781 | if (memcmp(&old->spilled_regs[i / BPF_REG_SIZE], |
| 2782 | &cur->spilled_regs[i / BPF_REG_SIZE], |
| 2783 | sizeof(old->spilled_regs[0]))) |
| 2784 | /* when explored and current stack slot types are |
| 2785 | * the same, check that stored pointers types |
| 2786 | * are the same as well. |
| 2787 | * Ex: explored safe path could have stored |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2788 | * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -8} |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2789 | * but current path has stored: |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2790 | * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -16} |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2791 | * such verifier states are not equivalent. |
| 2792 | * return false to continue verification of this path |
| 2793 | */ |
| 2794 | return false; |
| 2795 | else |
| 2796 | continue; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2797 | } |
| 2798 | return true; |
| 2799 | } |
| 2800 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2801 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2802 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2803 | struct bpf_verifier_state_list *new_sl; |
| 2804 | struct bpf_verifier_state_list *sl; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2805 | |
| 2806 | sl = env->explored_states[insn_idx]; |
| 2807 | if (!sl) |
| 2808 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 2809 | * be doing state search here |
| 2810 | */ |
| 2811 | return 0; |
| 2812 | |
| 2813 | while (sl != STATE_LIST_MARK) { |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2814 | if (states_equal(env, &sl->state, &env->cur_state)) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2815 | /* reached equivalent register/stack state, |
| 2816 | * prune the search |
| 2817 | */ |
| 2818 | return 1; |
| 2819 | sl = sl->next; |
| 2820 | } |
| 2821 | |
| 2822 | /* there were no equivalent states, remember current one. |
| 2823 | * technically the current state is not proven to be safe yet, |
| 2824 | * but it will either reach bpf_exit (which means it's safe) or |
| 2825 | * it will be rejected. Since there are no loops, we won't be |
| 2826 | * seeing this 'insn_idx' instruction again on the way to bpf_exit |
| 2827 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2828 | new_sl = kmalloc(sizeof(struct bpf_verifier_state_list), GFP_USER); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2829 | if (!new_sl) |
| 2830 | return -ENOMEM; |
| 2831 | |
| 2832 | /* add new state to the head of linked list */ |
| 2833 | memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state)); |
| 2834 | new_sl->next = env->explored_states[insn_idx]; |
| 2835 | env->explored_states[insn_idx] = new_sl; |
| 2836 | return 0; |
| 2837 | } |
| 2838 | |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 2839 | static int ext_analyzer_insn_hook(struct bpf_verifier_env *env, |
| 2840 | int insn_idx, int prev_insn_idx) |
| 2841 | { |
| 2842 | if (!env->analyzer_ops || !env->analyzer_ops->insn_hook) |
| 2843 | return 0; |
| 2844 | |
| 2845 | return env->analyzer_ops->insn_hook(env, insn_idx, prev_insn_idx); |
| 2846 | } |
| 2847 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2848 | static int do_check(struct bpf_verifier_env *env) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2849 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2850 | struct bpf_verifier_state *state = &env->cur_state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2851 | struct bpf_insn *insns = env->prog->insnsi; |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2852 | struct bpf_reg_state *regs = state->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2853 | int insn_cnt = env->prog->len; |
| 2854 | int insn_idx, prev_insn_idx = 0; |
| 2855 | int insn_processed = 0; |
| 2856 | bool do_print_state = false; |
| 2857 | |
| 2858 | init_reg_state(regs); |
| 2859 | insn_idx = 0; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2860 | env->varlen_map_value_access = false; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2861 | for (;;) { |
| 2862 | struct bpf_insn *insn; |
| 2863 | u8 class; |
| 2864 | int err; |
| 2865 | |
| 2866 | if (insn_idx >= insn_cnt) { |
| 2867 | verbose("invalid insn idx %d insn_cnt %d\n", |
| 2868 | insn_idx, insn_cnt); |
| 2869 | return -EFAULT; |
| 2870 | } |
| 2871 | |
| 2872 | insn = &insns[insn_idx]; |
| 2873 | class = BPF_CLASS(insn->code); |
| 2874 | |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 2875 | if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2876 | verbose("BPF program is too large. Proccessed %d insn\n", |
| 2877 | insn_processed); |
| 2878 | return -E2BIG; |
| 2879 | } |
| 2880 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2881 | err = is_state_visited(env, insn_idx); |
| 2882 | if (err < 0) |
| 2883 | return err; |
| 2884 | if (err == 1) { |
| 2885 | /* found equivalent state, can prune the search */ |
| 2886 | if (log_level) { |
| 2887 | if (do_print_state) |
| 2888 | verbose("\nfrom %d to %d: safe\n", |
| 2889 | prev_insn_idx, insn_idx); |
| 2890 | else |
| 2891 | verbose("%d: safe\n", insn_idx); |
| 2892 | } |
| 2893 | goto process_bpf_exit; |
| 2894 | } |
| 2895 | |
Daniel Borkmann | 577aa83 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 2896 | if (need_resched()) |
| 2897 | cond_resched(); |
| 2898 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2899 | if (log_level && do_print_state) { |
| 2900 | verbose("\nfrom %d to %d:", prev_insn_idx, insn_idx); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2901 | print_verifier_state(&env->cur_state); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2902 | do_print_state = false; |
| 2903 | } |
| 2904 | |
| 2905 | if (log_level) { |
| 2906 | verbose("%d: ", insn_idx); |
Daniel Borkmann | ced0a31 | 2017-05-08 00:04:09 +0200 | [diff] [blame] | 2907 | print_bpf_insn(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2908 | } |
| 2909 | |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 2910 | err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx); |
| 2911 | if (err) |
| 2912 | return err; |
| 2913 | |
Daniel Borkmann | 7b5b73e | 2017-12-22 16:29:03 +0100 | [diff] [blame] | 2914 | env->insn_aux_data[insn_idx].seen = true; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2915 | if (class == BPF_ALU || class == BPF_ALU64) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2916 | err = check_alu_op(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2917 | if (err) |
| 2918 | return err; |
| 2919 | |
| 2920 | } else if (class == BPF_LDX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 2921 | enum bpf_reg_type *prev_src_type, src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2922 | |
| 2923 | /* check for reserved fields is already done */ |
| 2924 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2925 | /* check src operand */ |
| 2926 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 2927 | if (err) |
| 2928 | return err; |
| 2929 | |
| 2930 | err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK); |
| 2931 | if (err) |
| 2932 | return err; |
| 2933 | |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 2934 | src_reg_type = regs[insn->src_reg].type; |
| 2935 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2936 | /* check that memory (src_reg + off) is readable, |
| 2937 | * the state of dst_reg will be updated by this func |
| 2938 | */ |
| 2939 | err = check_mem_access(env, insn->src_reg, insn->off, |
| 2940 | BPF_SIZE(insn->code), BPF_READ, |
| 2941 | insn->dst_reg); |
| 2942 | if (err) |
| 2943 | return err; |
| 2944 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2945 | reset_reg_range_values(regs, insn->dst_reg); |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 2946 | if (BPF_SIZE(insn->code) != BPF_W && |
| 2947 | BPF_SIZE(insn->code) != BPF_DW) { |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 2948 | insn_idx++; |
| 2949 | continue; |
| 2950 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2951 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 2952 | prev_src_type = &env->insn_aux_data[insn_idx].ptr_type; |
| 2953 | |
| 2954 | if (*prev_src_type == NOT_INIT) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2955 | /* saw a valid insn |
| 2956 | * dst_reg = *(u32 *)(src_reg + off) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 2957 | * save type to validate intersecting paths |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2958 | */ |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 2959 | *prev_src_type = src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2960 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 2961 | } else if (src_reg_type != *prev_src_type && |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2962 | (src_reg_type == PTR_TO_CTX || |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 2963 | *prev_src_type == PTR_TO_CTX)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2964 | /* ABuser program is trying to use the same insn |
| 2965 | * dst_reg = *(u32*) (src_reg + off) |
| 2966 | * with different pointer types: |
| 2967 | * src_reg == ctx in one branch and |
| 2968 | * src_reg == stack|map in some other branch. |
| 2969 | * Reject it. |
| 2970 | */ |
| 2971 | verbose("same insn cannot be used with different pointers\n"); |
| 2972 | return -EINVAL; |
| 2973 | } |
| 2974 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2975 | } else if (class == BPF_STX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 2976 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2977 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2978 | if (BPF_MODE(insn->code) == BPF_XADD) { |
| 2979 | err = check_xadd(env, insn); |
| 2980 | if (err) |
| 2981 | return err; |
| 2982 | insn_idx++; |
| 2983 | continue; |
| 2984 | } |
| 2985 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2986 | /* check src1 operand */ |
| 2987 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 2988 | if (err) |
| 2989 | return err; |
| 2990 | /* check src2 operand */ |
| 2991 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 2992 | if (err) |
| 2993 | return err; |
| 2994 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2995 | dst_reg_type = regs[insn->dst_reg].type; |
| 2996 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2997 | /* check that memory (dst_reg + off) is writeable */ |
| 2998 | err = check_mem_access(env, insn->dst_reg, insn->off, |
| 2999 | BPF_SIZE(insn->code), BPF_WRITE, |
| 3000 | insn->src_reg); |
| 3001 | if (err) |
| 3002 | return err; |
| 3003 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3004 | prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type; |
| 3005 | |
| 3006 | if (*prev_dst_type == NOT_INIT) { |
| 3007 | *prev_dst_type = dst_reg_type; |
| 3008 | } else if (dst_reg_type != *prev_dst_type && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 3009 | (dst_reg_type == PTR_TO_CTX || |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3010 | *prev_dst_type == PTR_TO_CTX)) { |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 3011 | verbose("same insn cannot be used with different pointers\n"); |
| 3012 | return -EINVAL; |
| 3013 | } |
| 3014 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3015 | } else if (class == BPF_ST) { |
| 3016 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 3017 | insn->src_reg != BPF_REG_0) { |
| 3018 | verbose("BPF_ST uses reserved fields\n"); |
| 3019 | return -EINVAL; |
| 3020 | } |
| 3021 | /* check src operand */ |
| 3022 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 3023 | if (err) |
| 3024 | return err; |
| 3025 | |
Daniel Borkmann | f531fbb | 2018-01-29 02:49:01 +0100 | [diff] [blame] | 3026 | if (is_ctx_reg(env, insn->dst_reg)) { |
| 3027 | verbose("BPF_ST stores into R%d context is not allowed\n", |
| 3028 | insn->dst_reg); |
| 3029 | return -EACCES; |
| 3030 | } |
| 3031 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3032 | /* check that memory (dst_reg + off) is writeable */ |
| 3033 | err = check_mem_access(env, insn->dst_reg, insn->off, |
| 3034 | BPF_SIZE(insn->code), BPF_WRITE, |
| 3035 | -1); |
| 3036 | if (err) |
| 3037 | return err; |
| 3038 | |
| 3039 | } else if (class == BPF_JMP) { |
| 3040 | u8 opcode = BPF_OP(insn->code); |
| 3041 | |
| 3042 | if (opcode == BPF_CALL) { |
| 3043 | if (BPF_SRC(insn->code) != BPF_K || |
| 3044 | insn->off != 0 || |
| 3045 | insn->src_reg != BPF_REG_0 || |
| 3046 | insn->dst_reg != BPF_REG_0) { |
| 3047 | verbose("BPF_CALL uses reserved fields\n"); |
| 3048 | return -EINVAL; |
| 3049 | } |
| 3050 | |
Alexei Starovoitov | a9bfac14 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 3051 | err = check_call(env, insn->imm, insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3052 | if (err) |
| 3053 | return err; |
| 3054 | |
| 3055 | } else if (opcode == BPF_JA) { |
| 3056 | if (BPF_SRC(insn->code) != BPF_K || |
| 3057 | insn->imm != 0 || |
| 3058 | insn->src_reg != BPF_REG_0 || |
| 3059 | insn->dst_reg != BPF_REG_0) { |
| 3060 | verbose("BPF_JA uses reserved fields\n"); |
| 3061 | return -EINVAL; |
| 3062 | } |
| 3063 | |
| 3064 | insn_idx += insn->off + 1; |
| 3065 | continue; |
| 3066 | |
| 3067 | } else if (opcode == BPF_EXIT) { |
| 3068 | if (BPF_SRC(insn->code) != BPF_K || |
| 3069 | insn->imm != 0 || |
| 3070 | insn->src_reg != BPF_REG_0 || |
| 3071 | insn->dst_reg != BPF_REG_0) { |
| 3072 | verbose("BPF_EXIT uses reserved fields\n"); |
| 3073 | return -EINVAL; |
| 3074 | } |
| 3075 | |
| 3076 | /* eBPF calling convetion is such that R0 is used |
| 3077 | * to return the value from eBPF program. |
| 3078 | * Make sure that it's readable at this time |
| 3079 | * of bpf_exit, which means that program wrote |
| 3080 | * something into it earlier |
| 3081 | */ |
| 3082 | err = check_reg_arg(regs, BPF_REG_0, SRC_OP); |
| 3083 | if (err) |
| 3084 | return err; |
| 3085 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3086 | if (is_pointer_value(env, BPF_REG_0)) { |
| 3087 | verbose("R0 leaks addr as return value\n"); |
| 3088 | return -EACCES; |
| 3089 | } |
| 3090 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3091 | process_bpf_exit: |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3092 | insn_idx = pop_stack(env, &prev_insn_idx); |
| 3093 | if (insn_idx < 0) { |
| 3094 | break; |
| 3095 | } else { |
| 3096 | do_print_state = true; |
| 3097 | continue; |
| 3098 | } |
| 3099 | } else { |
| 3100 | err = check_cond_jmp_op(env, insn, &insn_idx); |
| 3101 | if (err) |
| 3102 | return err; |
| 3103 | } |
| 3104 | } else if (class == BPF_LD) { |
| 3105 | u8 mode = BPF_MODE(insn->code); |
| 3106 | |
| 3107 | if (mode == BPF_ABS || mode == BPF_IND) { |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3108 | err = check_ld_abs(env, insn); |
| 3109 | if (err) |
| 3110 | return err; |
| 3111 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3112 | } else if (mode == BPF_IMM) { |
| 3113 | err = check_ld_imm(env, insn); |
| 3114 | if (err) |
| 3115 | return err; |
| 3116 | |
| 3117 | insn_idx++; |
Daniel Borkmann | 7b5b73e | 2017-12-22 16:29:03 +0100 | [diff] [blame] | 3118 | env->insn_aux_data[insn_idx].seen = true; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3119 | } else { |
| 3120 | verbose("invalid BPF_LD mode\n"); |
| 3121 | return -EINVAL; |
| 3122 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3123 | reset_reg_range_values(regs, insn->dst_reg); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3124 | } else { |
| 3125 | verbose("unknown insn class %d\n", class); |
| 3126 | return -EINVAL; |
| 3127 | } |
| 3128 | |
| 3129 | insn_idx++; |
| 3130 | } |
| 3131 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3132 | verbose("processed %d insns\n", insn_processed); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3133 | return 0; |
| 3134 | } |
| 3135 | |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 3136 | static int check_map_prog_compatibility(struct bpf_map *map, |
| 3137 | struct bpf_prog *prog) |
| 3138 | |
| 3139 | { |
| 3140 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT && |
| 3141 | (map->map_type == BPF_MAP_TYPE_HASH || |
| 3142 | map->map_type == BPF_MAP_TYPE_PERCPU_HASH) && |
| 3143 | (map->map_flags & BPF_F_NO_PREALLOC)) { |
| 3144 | verbose("perf_event programs can only use preallocated hash map\n"); |
| 3145 | return -EINVAL; |
| 3146 | } |
| 3147 | return 0; |
| 3148 | } |
| 3149 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3150 | /* look for pseudo eBPF instructions that access map FDs and |
| 3151 | * replace them with actual map pointers |
| 3152 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3153 | static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3154 | { |
| 3155 | struct bpf_insn *insn = env->prog->insnsi; |
| 3156 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 3157 | int i, j, err; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3158 | |
| 3159 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3160 | if (BPF_CLASS(insn->code) == BPF_LDX && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 3161 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3162 | verbose("BPF_LDX uses reserved fields\n"); |
| 3163 | return -EINVAL; |
| 3164 | } |
| 3165 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 3166 | if (BPF_CLASS(insn->code) == BPF_STX && |
| 3167 | ((BPF_MODE(insn->code) != BPF_MEM && |
| 3168 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { |
| 3169 | verbose("BPF_STX uses reserved fields\n"); |
| 3170 | return -EINVAL; |
| 3171 | } |
| 3172 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3173 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
| 3174 | struct bpf_map *map; |
| 3175 | struct fd f; |
| 3176 | |
| 3177 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 3178 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 3179 | insn[1].off != 0) { |
| 3180 | verbose("invalid bpf_ld_imm64 insn\n"); |
| 3181 | return -EINVAL; |
| 3182 | } |
| 3183 | |
| 3184 | if (insn->src_reg == 0) |
| 3185 | /* valid generic load 64-bit imm */ |
| 3186 | goto next_insn; |
| 3187 | |
| 3188 | if (insn->src_reg != BPF_PSEUDO_MAP_FD) { |
| 3189 | verbose("unrecognized bpf_ld_imm64 insn\n"); |
| 3190 | return -EINVAL; |
| 3191 | } |
| 3192 | |
| 3193 | f = fdget(insn->imm); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 3194 | map = __bpf_map_get(f); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3195 | if (IS_ERR(map)) { |
| 3196 | verbose("fd %d is not pointing to valid bpf_map\n", |
| 3197 | insn->imm); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3198 | return PTR_ERR(map); |
| 3199 | } |
| 3200 | |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 3201 | err = check_map_prog_compatibility(map, env->prog); |
| 3202 | if (err) { |
| 3203 | fdput(f); |
| 3204 | return err; |
| 3205 | } |
| 3206 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3207 | /* store map pointer inside BPF_LD_IMM64 instruction */ |
| 3208 | insn[0].imm = (u32) (unsigned long) map; |
| 3209 | insn[1].imm = ((u64) (unsigned long) map) >> 32; |
| 3210 | |
| 3211 | /* check whether we recorded this map already */ |
| 3212 | for (j = 0; j < env->used_map_cnt; j++) |
| 3213 | if (env->used_maps[j] == map) { |
| 3214 | fdput(f); |
| 3215 | goto next_insn; |
| 3216 | } |
| 3217 | |
| 3218 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 3219 | fdput(f); |
| 3220 | return -E2BIG; |
| 3221 | } |
| 3222 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3223 | /* hold the map. If the program is rejected by verifier, |
| 3224 | * the map will be released by release_maps() or it |
| 3225 | * will be used by the valid program until it's unloaded |
Jakub Kicinski | e31a06e | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 3226 | * and all maps are released in free_used_maps() |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3227 | */ |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 3228 | map = bpf_map_inc(map, false); |
| 3229 | if (IS_ERR(map)) { |
| 3230 | fdput(f); |
| 3231 | return PTR_ERR(map); |
| 3232 | } |
| 3233 | env->used_maps[env->used_map_cnt++] = map; |
| 3234 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3235 | fdput(f); |
| 3236 | next_insn: |
| 3237 | insn++; |
| 3238 | i++; |
| 3239 | } |
| 3240 | } |
| 3241 | |
| 3242 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 3243 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 3244 | * These pointers will be used later by verifier to validate map access. |
| 3245 | */ |
| 3246 | return 0; |
| 3247 | } |
| 3248 | |
| 3249 | /* drop refcnt of maps used by the rejected program */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3250 | static void release_maps(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3251 | { |
| 3252 | int i; |
| 3253 | |
| 3254 | for (i = 0; i < env->used_map_cnt; i++) |
| 3255 | bpf_map_put(env->used_maps[i]); |
| 3256 | } |
| 3257 | |
| 3258 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3259 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3260 | { |
| 3261 | struct bpf_insn *insn = env->prog->insnsi; |
| 3262 | int insn_cnt = env->prog->len; |
| 3263 | int i; |
| 3264 | |
| 3265 | for (i = 0; i < insn_cnt; i++, insn++) |
| 3266 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) |
| 3267 | insn->src_reg = 0; |
| 3268 | } |
| 3269 | |
Daniel Borkmann | 565f012 | 2017-12-22 16:29:02 +0100 | [diff] [blame] | 3270 | /* single env->prog->insni[off] instruction was replaced with the range |
| 3271 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying |
| 3272 | * [0, off) and [off, end) to new locations, so the patched range stays zero |
| 3273 | */ |
| 3274 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len, |
| 3275 | u32 off, u32 cnt) |
| 3276 | { |
| 3277 | struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data; |
Daniel Borkmann | 7b5b73e | 2017-12-22 16:29:03 +0100 | [diff] [blame] | 3278 | int i; |
Daniel Borkmann | 565f012 | 2017-12-22 16:29:02 +0100 | [diff] [blame] | 3279 | |
| 3280 | if (cnt == 1) |
| 3281 | return 0; |
| 3282 | new_data = vzalloc(sizeof(struct bpf_insn_aux_data) * prog_len); |
| 3283 | if (!new_data) |
| 3284 | return -ENOMEM; |
| 3285 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
| 3286 | memcpy(new_data + off + cnt - 1, old_data + off, |
| 3287 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); |
Daniel Borkmann | 7b5b73e | 2017-12-22 16:29:03 +0100 | [diff] [blame] | 3288 | for (i = off; i < off + cnt - 1; i++) |
| 3289 | new_data[i].seen = true; |
Daniel Borkmann | 565f012 | 2017-12-22 16:29:02 +0100 | [diff] [blame] | 3290 | env->insn_aux_data = new_data; |
| 3291 | vfree(old_data); |
| 3292 | return 0; |
| 3293 | } |
| 3294 | |
| 3295 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
| 3296 | const struct bpf_insn *patch, u32 len) |
| 3297 | { |
| 3298 | struct bpf_prog *new_prog; |
| 3299 | |
| 3300 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); |
| 3301 | if (!new_prog) |
| 3302 | return NULL; |
| 3303 | if (adjust_insn_aux_data(env, new_prog->len, off, len)) |
| 3304 | return NULL; |
| 3305 | return new_prog; |
| 3306 | } |
| 3307 | |
Daniel Borkmann | 7b5b73e | 2017-12-22 16:29:03 +0100 | [diff] [blame] | 3308 | /* The verifier does more data flow analysis than llvm and will not explore |
| 3309 | * branches that are dead at run time. Malicious programs can have dead code |
| 3310 | * too. Therefore replace all dead at-run-time code with nops. |
| 3311 | */ |
| 3312 | static void sanitize_dead_code(struct bpf_verifier_env *env) |
| 3313 | { |
| 3314 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 3315 | struct bpf_insn nop = BPF_MOV64_REG(BPF_REG_0, BPF_REG_0); |
| 3316 | struct bpf_insn *insn = env->prog->insnsi; |
| 3317 | const int insn_cnt = env->prog->len; |
| 3318 | int i; |
| 3319 | |
| 3320 | for (i = 0; i < insn_cnt; i++) { |
| 3321 | if (aux_data[i].seen) |
| 3322 | continue; |
| 3323 | memcpy(insn + i, &nop, sizeof(nop)); |
| 3324 | } |
| 3325 | } |
| 3326 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3327 | /* convert load instructions that access fields of 'struct __sk_buff' |
| 3328 | * into sequence of instructions that access fields of 'struct sk_buff' |
| 3329 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3330 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3331 | { |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 3332 | const struct bpf_verifier_ops *ops = env->prog->aux->ops; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3333 | const int insn_cnt = env->prog->len; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 3334 | struct bpf_insn insn_buf[16], *insn; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3335 | struct bpf_prog *new_prog; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 3336 | enum bpf_access_type type; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3337 | int i, cnt, delta = 0; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3338 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 3339 | if (ops->gen_prologue) { |
| 3340 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
| 3341 | env->prog); |
| 3342 | if (cnt >= ARRAY_SIZE(insn_buf)) { |
| 3343 | verbose("bpf verifier is misconfigured\n"); |
| 3344 | return -EINVAL; |
| 3345 | } else if (cnt) { |
Daniel Borkmann | 565f012 | 2017-12-22 16:29:02 +0100 | [diff] [blame] | 3346 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 3347 | if (!new_prog) |
| 3348 | return -ENOMEM; |
Daniel Borkmann | 565f012 | 2017-12-22 16:29:02 +0100 | [diff] [blame] | 3349 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 3350 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3351 | delta += cnt - 1; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 3352 | } |
| 3353 | } |
| 3354 | |
| 3355 | if (!ops->convert_ctx_access) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3356 | return 0; |
| 3357 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3358 | insn = env->prog->insnsi + delta; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 3359 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3360 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 3361 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_W) || |
| 3362 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 3363 | type = BPF_READ; |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 3364 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_W) || |
| 3365 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 3366 | type = BPF_WRITE; |
| 3367 | else |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3368 | continue; |
| 3369 | |
Daniel Borkmann | 565f012 | 2017-12-22 16:29:02 +0100 | [diff] [blame] | 3370 | if (env->insn_aux_data[i + delta].ptr_type != PTR_TO_CTX) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3371 | continue; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3372 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 3373 | cnt = ops->convert_ctx_access(type, insn->dst_reg, insn->src_reg, |
| 3374 | insn->off, insn_buf, env->prog); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3375 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 3376 | verbose("bpf verifier is misconfigured\n"); |
| 3377 | return -EINVAL; |
| 3378 | } |
| 3379 | |
Daniel Borkmann | 565f012 | 2017-12-22 16:29:02 +0100 | [diff] [blame] | 3380 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3381 | if (!new_prog) |
| 3382 | return -ENOMEM; |
| 3383 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3384 | delta += cnt - 1; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3385 | |
| 3386 | /* keep walking new program and skip insns we just inserted */ |
| 3387 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3388 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3389 | } |
| 3390 | |
| 3391 | return 0; |
| 3392 | } |
| 3393 | |
Alexei Starovoitov | f55093d | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 3394 | /* fixup insn->imm field of bpf_call instructions |
Alexei Starovoitov | 2803536 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 3395 | * |
| 3396 | * this function is called after eBPF program passed verification |
| 3397 | */ |
Alexei Starovoitov | f55093d | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 3398 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
Alexei Starovoitov | 2803536 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 3399 | { |
Alexei Starovoitov | f55093d | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 3400 | struct bpf_prog *prog = env->prog; |
| 3401 | struct bpf_insn *insn = prog->insnsi; |
Alexei Starovoitov | 2803536 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 3402 | const struct bpf_func_proto *fn; |
Alexei Starovoitov | f55093d | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 3403 | const int insn_cnt = prog->len; |
Alexei Starovoitov | a9bfac14 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 3404 | struct bpf_insn insn_buf[16]; |
| 3405 | struct bpf_prog *new_prog; |
| 3406 | struct bpf_map *map_ptr; |
| 3407 | int i, cnt, delta = 0; |
| 3408 | |
Alexei Starovoitov | 2803536 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 3409 | |
Alexei Starovoitov | f55093d | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 3410 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 265d765 | 2018-01-29 02:49:00 +0100 | [diff] [blame] | 3411 | if (insn->code == (BPF_ALU | BPF_MOD | BPF_X) || |
| 3412 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
| 3413 | /* due to JIT bugs clear upper 32-bits of src register |
| 3414 | * before div/mod operation |
| 3415 | */ |
| 3416 | insn_buf[0] = BPF_MOV32_REG(insn->src_reg, insn->src_reg); |
| 3417 | insn_buf[1] = *insn; |
| 3418 | cnt = 2; |
| 3419 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 3420 | if (!new_prog) |
| 3421 | return -ENOMEM; |
| 3422 | |
| 3423 | delta += cnt - 1; |
| 3424 | env->prog = prog = new_prog; |
| 3425 | insn = new_prog->insnsi + i + delta; |
| 3426 | continue; |
| 3427 | } |
| 3428 | |
Alexei Starovoitov | f55093d | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 3429 | if (insn->code != (BPF_JMP | BPF_CALL)) |
| 3430 | continue; |
Alexei Starovoitov | 2803536 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 3431 | |
Alexei Starovoitov | f55093d | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 3432 | if (insn->imm == BPF_FUNC_get_route_realm) |
| 3433 | prog->dst_needed = 1; |
| 3434 | if (insn->imm == BPF_FUNC_get_prandom_u32) |
| 3435 | bpf_user_rnd_init_once(); |
| 3436 | if (insn->imm == BPF_FUNC_tail_call) { |
| 3437 | /* mark bpf_tail_call as different opcode to avoid |
| 3438 | * conditional branch in the interpeter for every normal |
| 3439 | * call and to prevent accidental JITing by JIT compiler |
| 3440 | * that doesn't support bpf_tail_call yet |
| 3441 | */ |
| 3442 | insn->imm = 0; |
| 3443 | insn->code |= BPF_X; |
Alexei Starovoitov | a9bfac14 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 3444 | |
| 3445 | /* instead of changing every JIT dealing with tail_call |
| 3446 | * emit two extra insns: |
| 3447 | * if (index >= max_entries) goto out; |
| 3448 | * index &= array->index_mask; |
| 3449 | * to avoid out-of-bounds cpu speculation |
| 3450 | */ |
| 3451 | map_ptr = env->insn_aux_data[i + delta].map_ptr; |
| 3452 | if (!map_ptr->unpriv_array) |
| 3453 | continue; |
| 3454 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
| 3455 | map_ptr->max_entries, 2); |
| 3456 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, |
| 3457 | container_of(map_ptr, |
| 3458 | struct bpf_array, |
| 3459 | map)->index_mask); |
| 3460 | insn_buf[2] = *insn; |
| 3461 | cnt = 3; |
| 3462 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 3463 | if (!new_prog) |
| 3464 | return -ENOMEM; |
| 3465 | |
| 3466 | delta += cnt - 1; |
| 3467 | env->prog = prog = new_prog; |
| 3468 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | f55093d | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 3469 | continue; |
Alexei Starovoitov | 2803536 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 3470 | } |
Alexei Starovoitov | f55093d | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 3471 | |
| 3472 | fn = prog->aux->ops->get_func_proto(insn->imm); |
| 3473 | /* all functions that have prototype and verifier allowed |
| 3474 | * programs to call them, must be real in-kernel functions |
| 3475 | */ |
| 3476 | if (!fn->func) { |
| 3477 | verbose("kernel subsystem misconfigured func %d\n", |
| 3478 | insn->imm); |
| 3479 | return -EFAULT; |
| 3480 | } |
| 3481 | insn->imm = fn->func - __bpf_call_base; |
Alexei Starovoitov | 2803536 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 3482 | } |
Alexei Starovoitov | f55093d | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 3483 | |
| 3484 | return 0; |
Alexei Starovoitov | 2803536 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 3485 | } |
| 3486 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3487 | static void free_states(struct bpf_verifier_env *env) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3488 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3489 | struct bpf_verifier_state_list *sl, *sln; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3490 | int i; |
| 3491 | |
| 3492 | if (!env->explored_states) |
| 3493 | return; |
| 3494 | |
| 3495 | for (i = 0; i < env->prog->len; i++) { |
| 3496 | sl = env->explored_states[i]; |
| 3497 | |
| 3498 | if (sl) |
| 3499 | while (sl != STATE_LIST_MARK) { |
| 3500 | sln = sl->next; |
| 3501 | kfree(sl); |
| 3502 | sl = sln; |
| 3503 | } |
| 3504 | } |
| 3505 | |
| 3506 | kfree(env->explored_states); |
| 3507 | } |
| 3508 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3509 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 3510 | { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 3511 | char __user *log_ubuf = NULL; |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3512 | struct bpf_verifier_env *env; |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 3513 | int ret = -EINVAL; |
| 3514 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3515 | if ((*prog)->len <= 0 || (*prog)->len > BPF_MAXINSNS) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 3516 | return -E2BIG; |
| 3517 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3518 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 3519 | * allocate/free it every time bpf_check() is called |
| 3520 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3521 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 3522 | if (!env) |
| 3523 | return -ENOMEM; |
| 3524 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3525 | env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) * |
| 3526 | (*prog)->len); |
| 3527 | ret = -ENOMEM; |
| 3528 | if (!env->insn_aux_data) |
| 3529 | goto err_free_env; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3530 | env->prog = *prog; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3531 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 3532 | /* grab the mutex to protect few globals used by verifier */ |
| 3533 | mutex_lock(&bpf_verifier_lock); |
| 3534 | |
| 3535 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 3536 | /* user requested verbose verifier output |
| 3537 | * and supplied buffer to store the verification trace |
| 3538 | */ |
| 3539 | log_level = attr->log_level; |
| 3540 | log_ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 3541 | log_size = attr->log_size; |
| 3542 | log_len = 0; |
| 3543 | |
| 3544 | ret = -EINVAL; |
| 3545 | /* log_* values have to be sane */ |
| 3546 | if (log_size < 128 || log_size > UINT_MAX >> 8 || |
| 3547 | log_level == 0 || log_ubuf == NULL) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3548 | goto err_unlock; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 3549 | |
| 3550 | ret = -ENOMEM; |
| 3551 | log_buf = vmalloc(log_size); |
| 3552 | if (!log_buf) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3553 | goto err_unlock; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 3554 | } else { |
| 3555 | log_level = 0; |
| 3556 | } |
| 3557 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3558 | ret = replace_map_fd_with_map_ptr(env); |
| 3559 | if (ret < 0) |
| 3560 | goto skip_full_check; |
| 3561 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3562 | env->explored_states = kcalloc(env->prog->len, |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3563 | sizeof(struct bpf_verifier_state_list *), |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3564 | GFP_USER); |
| 3565 | ret = -ENOMEM; |
| 3566 | if (!env->explored_states) |
| 3567 | goto skip_full_check; |
| 3568 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3569 | ret = check_cfg(env); |
| 3570 | if (ret < 0) |
| 3571 | goto skip_full_check; |
| 3572 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3573 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); |
| 3574 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3575 | ret = do_check(env); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 3576 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3577 | skip_full_check: |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3578 | while (pop_stack(env, NULL) >= 0); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3579 | free_states(env); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3580 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3581 | if (ret == 0) |
Daniel Borkmann | 7b5b73e | 2017-12-22 16:29:03 +0100 | [diff] [blame] | 3582 | sanitize_dead_code(env); |
| 3583 | |
| 3584 | if (ret == 0) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3585 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 3586 | ret = convert_ctx_accesses(env); |
| 3587 | |
Alexei Starovoitov | 2803536 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 3588 | if (ret == 0) |
Alexei Starovoitov | f55093d | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 3589 | ret = fixup_bpf_calls(env); |
Alexei Starovoitov | 2803536 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 3590 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 3591 | if (log_level && log_len >= log_size - 1) { |
| 3592 | BUG_ON(log_len >= log_size); |
| 3593 | /* verifier log exceeded user supplied buffer */ |
| 3594 | ret = -ENOSPC; |
| 3595 | /* fall through to return what was recorded */ |
| 3596 | } |
| 3597 | |
| 3598 | /* copy verifier log back to user space including trailing zero */ |
| 3599 | if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) { |
| 3600 | ret = -EFAULT; |
| 3601 | goto free_log_buf; |
| 3602 | } |
| 3603 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3604 | if (ret == 0 && env->used_map_cnt) { |
| 3605 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3606 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 3607 | sizeof(env->used_maps[0]), |
| 3608 | GFP_KERNEL); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3609 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3610 | if (!env->prog->aux->used_maps) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3611 | ret = -ENOMEM; |
| 3612 | goto free_log_buf; |
| 3613 | } |
| 3614 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3615 | memcpy(env->prog->aux->used_maps, env->used_maps, |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3616 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3617 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3618 | |
| 3619 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 3620 | * bpf_ld_imm64 instructions |
| 3621 | */ |
| 3622 | convert_pseudo_ld_imm64(env); |
| 3623 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 3624 | |
| 3625 | free_log_buf: |
| 3626 | if (log_level) |
| 3627 | vfree(log_buf); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3628 | if (!env->prog->aux->used_maps) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3629 | /* if we didn't copy map pointers into bpf_prog_info, release |
Jakub Kicinski | e31a06e | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 3630 | * them now. Otherwise free_used_maps() will release them. |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3631 | */ |
| 3632 | release_maps(env); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3633 | *prog = env->prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3634 | err_unlock: |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 3635 | mutex_unlock(&bpf_verifier_lock); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3636 | vfree(env->insn_aux_data); |
| 3637 | err_free_env: |
| 3638 | kfree(env); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 3639 | return ret; |
| 3640 | } |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 3641 | |
| 3642 | int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops, |
| 3643 | void *priv) |
| 3644 | { |
| 3645 | struct bpf_verifier_env *env; |
| 3646 | int ret; |
| 3647 | |
| 3648 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
| 3649 | if (!env) |
| 3650 | return -ENOMEM; |
| 3651 | |
| 3652 | env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) * |
| 3653 | prog->len); |
| 3654 | ret = -ENOMEM; |
| 3655 | if (!env->insn_aux_data) |
| 3656 | goto err_free_env; |
| 3657 | env->prog = prog; |
| 3658 | env->analyzer_ops = ops; |
| 3659 | env->analyzer_priv = priv; |
| 3660 | |
| 3661 | /* grab the mutex to protect few globals used by verifier */ |
| 3662 | mutex_lock(&bpf_verifier_lock); |
| 3663 | |
| 3664 | log_level = 0; |
| 3665 | |
| 3666 | env->explored_states = kcalloc(env->prog->len, |
| 3667 | sizeof(struct bpf_verifier_state_list *), |
| 3668 | GFP_KERNEL); |
| 3669 | ret = -ENOMEM; |
| 3670 | if (!env->explored_states) |
| 3671 | goto skip_full_check; |
| 3672 | |
| 3673 | ret = check_cfg(env); |
| 3674 | if (ret < 0) |
| 3675 | goto skip_full_check; |
| 3676 | |
| 3677 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); |
| 3678 | |
| 3679 | ret = do_check(env); |
| 3680 | |
| 3681 | skip_full_check: |
| 3682 | while (pop_stack(env, NULL) >= 0); |
| 3683 | free_states(env); |
| 3684 | |
| 3685 | mutex_unlock(&bpf_verifier_lock); |
| 3686 | vfree(env->insn_aux_data); |
| 3687 | err_free_env: |
| 3688 | kfree(env); |
| 3689 | return ret; |
| 3690 | } |
| 3691 | EXPORT_SYMBOL_GPL(bpf_analyzer); |