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