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 | * |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 64 | * Most of the time the registers have SCALAR_VALUE type, which |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 65 | * means the register has some value, but it's not a valid pointer. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 66 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 67 | * |
| 68 | * When verifier sees load or store instructions the type of base register |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 69 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK. These are three pointer |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 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 | |
Edward Cree | 8e17c1b | 2017-08-07 15:30:30 +0100 | [diff] [blame] | 143 | #define BPF_COMPLEXITY_LIMIT_INSNS 131072 |
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 | */ |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame^] | 159 | static struct bpf_verifer_log verifier_log; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 160 | |
| 161 | static DEFINE_MUTEX(bpf_verifier_lock); |
| 162 | |
| 163 | /* log_level controls verbosity level of eBPF verifier. |
| 164 | * verbose() is used to dump the verification trace to the log, so the user |
| 165 | * can figure out what's wrong with the program |
| 166 | */ |
Daniel Borkmann | 1d056d9 | 2015-11-03 11:39:20 +0100 | [diff] [blame] | 167 | static __printf(1, 2) void verbose(const char *fmt, ...) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 168 | { |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame^] | 169 | struct bpf_verifer_log *log = &verifier_log; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 170 | va_list args; |
| 171 | |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame^] | 172 | if (!log->level || bpf_verifier_log_full(log)) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 173 | return; |
| 174 | |
| 175 | va_start(args, fmt); |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame^] | 176 | log->len_used += vscnprintf(log->kbuf + log->len_used, |
| 177 | log->len_total - log->len_used, fmt, args); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 178 | va_end(args); |
| 179 | } |
| 180 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 181 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
| 182 | { |
| 183 | return type == PTR_TO_PACKET || |
| 184 | type == PTR_TO_PACKET_META; |
| 185 | } |
| 186 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 187 | /* string representation of 'enum bpf_reg_type' */ |
| 188 | static const char * const reg_type_str[] = { |
| 189 | [NOT_INIT] = "?", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 190 | [SCALAR_VALUE] = "inv", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 191 | [PTR_TO_CTX] = "ctx", |
| 192 | [CONST_PTR_TO_MAP] = "map_ptr", |
| 193 | [PTR_TO_MAP_VALUE] = "map_value", |
| 194 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 195 | [PTR_TO_STACK] = "fp", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 196 | [PTR_TO_PACKET] = "pkt", |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 197 | [PTR_TO_PACKET_META] = "pkt_meta", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 198 | [PTR_TO_PACKET_END] = "pkt_end", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 199 | }; |
| 200 | |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 201 | #define __BPF_FUNC_STR_FN(x) [BPF_FUNC_ ## x] = __stringify(bpf_ ## x) |
| 202 | static const char * const func_id_str[] = { |
| 203 | __BPF_FUNC_MAPPER(__BPF_FUNC_STR_FN) |
| 204 | }; |
| 205 | #undef __BPF_FUNC_STR_FN |
| 206 | |
| 207 | static const char *func_id_name(int id) |
| 208 | { |
| 209 | BUILD_BUG_ON(ARRAY_SIZE(func_id_str) != __BPF_FUNC_MAX_ID); |
| 210 | |
| 211 | if (id >= 0 && id < __BPF_FUNC_MAX_ID && func_id_str[id]) |
| 212 | return func_id_str[id]; |
| 213 | else |
| 214 | return "unknown"; |
| 215 | } |
| 216 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 217 | static void print_verifier_state(struct bpf_verifier_state *state) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 218 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 219 | struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 220 | enum bpf_reg_type t; |
| 221 | int i; |
| 222 | |
| 223 | for (i = 0; i < MAX_BPF_REG; i++) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 224 | reg = &state->regs[i]; |
| 225 | t = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 226 | if (t == NOT_INIT) |
| 227 | continue; |
| 228 | verbose(" R%d=%s", i, reg_type_str[t]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 229 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
| 230 | tnum_is_const(reg->var_off)) { |
| 231 | /* reg->off should be 0 for SCALAR_VALUE */ |
| 232 | verbose("%lld", reg->var_off.value + reg->off); |
| 233 | } else { |
| 234 | verbose("(id=%d", reg->id); |
| 235 | if (t != SCALAR_VALUE) |
| 236 | verbose(",off=%d", reg->off); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 237 | if (type_is_pkt_pointer(t)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 238 | verbose(",r=%d", reg->range); |
| 239 | else if (t == CONST_PTR_TO_MAP || |
| 240 | t == PTR_TO_MAP_VALUE || |
| 241 | t == PTR_TO_MAP_VALUE_OR_NULL) |
| 242 | verbose(",ks=%d,vs=%d", |
| 243 | reg->map_ptr->key_size, |
| 244 | reg->map_ptr->value_size); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 245 | if (tnum_is_const(reg->var_off)) { |
| 246 | /* Typically an immediate SCALAR_VALUE, but |
| 247 | * could be a pointer whose offset is too big |
| 248 | * for reg->off |
| 249 | */ |
| 250 | verbose(",imm=%llx", reg->var_off.value); |
| 251 | } else { |
| 252 | if (reg->smin_value != reg->umin_value && |
| 253 | reg->smin_value != S64_MIN) |
| 254 | verbose(",smin_value=%lld", |
| 255 | (long long)reg->smin_value); |
| 256 | if (reg->smax_value != reg->umax_value && |
| 257 | reg->smax_value != S64_MAX) |
| 258 | verbose(",smax_value=%lld", |
| 259 | (long long)reg->smax_value); |
| 260 | if (reg->umin_value != 0) |
| 261 | verbose(",umin_value=%llu", |
| 262 | (unsigned long long)reg->umin_value); |
| 263 | if (reg->umax_value != U64_MAX) |
| 264 | verbose(",umax_value=%llu", |
| 265 | (unsigned long long)reg->umax_value); |
| 266 | if (!tnum_is_unknown(reg->var_off)) { |
| 267 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 268 | |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 269 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 270 | verbose(",var_off=%s", tn_buf); |
| 271 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 272 | } |
| 273 | verbose(")"); |
| 274 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 275 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 276 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 277 | if (state->stack_slot_type[i] == STACK_SPILL) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 278 | verbose(" fp%d=%s", -MAX_BPF_STACK + i, |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 279 | reg_type_str[state->spilled_regs[i / BPF_REG_SIZE].type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 280 | } |
| 281 | verbose("\n"); |
| 282 | } |
| 283 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 284 | static const char *const bpf_class_string[] = { |
| 285 | [BPF_LD] = "ld", |
| 286 | [BPF_LDX] = "ldx", |
| 287 | [BPF_ST] = "st", |
| 288 | [BPF_STX] = "stx", |
| 289 | [BPF_ALU] = "alu", |
| 290 | [BPF_JMP] = "jmp", |
| 291 | [BPF_RET] = "BUG", |
| 292 | [BPF_ALU64] = "alu64", |
| 293 | }; |
| 294 | |
Alexei Starovoitov | 687f071 | 2015-09-08 13:40:01 -0700 | [diff] [blame] | 295 | static const char *const bpf_alu_string[16] = { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 296 | [BPF_ADD >> 4] = "+=", |
| 297 | [BPF_SUB >> 4] = "-=", |
| 298 | [BPF_MUL >> 4] = "*=", |
| 299 | [BPF_DIV >> 4] = "/=", |
| 300 | [BPF_OR >> 4] = "|=", |
| 301 | [BPF_AND >> 4] = "&=", |
| 302 | [BPF_LSH >> 4] = "<<=", |
| 303 | [BPF_RSH >> 4] = ">>=", |
| 304 | [BPF_NEG >> 4] = "neg", |
| 305 | [BPF_MOD >> 4] = "%=", |
| 306 | [BPF_XOR >> 4] = "^=", |
| 307 | [BPF_MOV >> 4] = "=", |
| 308 | [BPF_ARSH >> 4] = "s>>=", |
| 309 | [BPF_END >> 4] = "endian", |
| 310 | }; |
| 311 | |
| 312 | static const char *const bpf_ldst_string[] = { |
| 313 | [BPF_W >> 3] = "u32", |
| 314 | [BPF_H >> 3] = "u16", |
| 315 | [BPF_B >> 3] = "u8", |
| 316 | [BPF_DW >> 3] = "u64", |
| 317 | }; |
| 318 | |
Alexei Starovoitov | 687f071 | 2015-09-08 13:40:01 -0700 | [diff] [blame] | 319 | static const char *const bpf_jmp_string[16] = { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 320 | [BPF_JA >> 4] = "jmp", |
| 321 | [BPF_JEQ >> 4] = "==", |
| 322 | [BPF_JGT >> 4] = ">", |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 323 | [BPF_JLT >> 4] = "<", |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 324 | [BPF_JGE >> 4] = ">=", |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 325 | [BPF_JLE >> 4] = "<=", |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 326 | [BPF_JSET >> 4] = "&", |
| 327 | [BPF_JNE >> 4] = "!=", |
| 328 | [BPF_JSGT >> 4] = "s>", |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 329 | [BPF_JSLT >> 4] = "s<", |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 330 | [BPF_JSGE >> 4] = "s>=", |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 331 | [BPF_JSLE >> 4] = "s<=", |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 332 | [BPF_CALL >> 4] = "call", |
| 333 | [BPF_EXIT >> 4] = "exit", |
| 334 | }; |
| 335 | |
Edward Cree | 2b7c6ba | 2017-09-26 16:35:13 +0100 | [diff] [blame] | 336 | static void print_bpf_end_insn(const struct bpf_verifier_env *env, |
| 337 | const struct bpf_insn *insn) |
| 338 | { |
| 339 | verbose("(%02x) r%d = %s%d r%d\n", insn->code, insn->dst_reg, |
| 340 | BPF_SRC(insn->code) == BPF_TO_BE ? "be" : "le", |
| 341 | insn->imm, insn->dst_reg); |
| 342 | } |
| 343 | |
Daniel Borkmann | 0d0e576 | 2017-05-08 00:04:09 +0200 | [diff] [blame] | 344 | static void print_bpf_insn(const struct bpf_verifier_env *env, |
| 345 | const struct bpf_insn *insn) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 346 | { |
| 347 | u8 class = BPF_CLASS(insn->code); |
| 348 | |
| 349 | if (class == BPF_ALU || class == BPF_ALU64) { |
Edward Cree | 2b7c6ba | 2017-09-26 16:35:13 +0100 | [diff] [blame] | 350 | if (BPF_OP(insn->code) == BPF_END) { |
| 351 | if (class == BPF_ALU64) |
| 352 | verbose("BUG_alu64_%02x\n", insn->code); |
| 353 | else |
| 354 | print_bpf_end_insn(env, insn); |
Edward Cree | 73c864b | 2017-09-26 16:35:29 +0100 | [diff] [blame] | 355 | } else if (BPF_OP(insn->code) == BPF_NEG) { |
| 356 | verbose("(%02x) r%d = %s-r%d\n", |
| 357 | insn->code, insn->dst_reg, |
| 358 | class == BPF_ALU ? "(u32) " : "", |
| 359 | insn->dst_reg); |
Edward Cree | 2b7c6ba | 2017-09-26 16:35:13 +0100 | [diff] [blame] | 360 | } else if (BPF_SRC(insn->code) == BPF_X) { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 361 | verbose("(%02x) %sr%d %s %sr%d\n", |
| 362 | insn->code, class == BPF_ALU ? "(u32) " : "", |
| 363 | insn->dst_reg, |
| 364 | bpf_alu_string[BPF_OP(insn->code) >> 4], |
| 365 | class == BPF_ALU ? "(u32) " : "", |
| 366 | insn->src_reg); |
Edward Cree | 2b7c6ba | 2017-09-26 16:35:13 +0100 | [diff] [blame] | 367 | } else { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 368 | verbose("(%02x) %sr%d %s %s%d\n", |
| 369 | insn->code, class == BPF_ALU ? "(u32) " : "", |
| 370 | insn->dst_reg, |
| 371 | bpf_alu_string[BPF_OP(insn->code) >> 4], |
| 372 | class == BPF_ALU ? "(u32) " : "", |
| 373 | insn->imm); |
Edward Cree | 2b7c6ba | 2017-09-26 16:35:13 +0100 | [diff] [blame] | 374 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 375 | } else if (class == BPF_STX) { |
| 376 | if (BPF_MODE(insn->code) == BPF_MEM) |
| 377 | verbose("(%02x) *(%s *)(r%d %+d) = r%d\n", |
| 378 | insn->code, |
| 379 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 380 | insn->dst_reg, |
| 381 | insn->off, insn->src_reg); |
| 382 | else if (BPF_MODE(insn->code) == BPF_XADD) |
| 383 | verbose("(%02x) lock *(%s *)(r%d %+d) += r%d\n", |
| 384 | insn->code, |
| 385 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 386 | insn->dst_reg, insn->off, |
| 387 | insn->src_reg); |
| 388 | else |
| 389 | verbose("BUG_%02x\n", insn->code); |
| 390 | } else if (class == BPF_ST) { |
| 391 | if (BPF_MODE(insn->code) != BPF_MEM) { |
| 392 | verbose("BUG_st_%02x\n", insn->code); |
| 393 | return; |
| 394 | } |
| 395 | verbose("(%02x) *(%s *)(r%d %+d) = %d\n", |
| 396 | insn->code, |
| 397 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 398 | insn->dst_reg, |
| 399 | insn->off, insn->imm); |
| 400 | } else if (class == BPF_LDX) { |
| 401 | if (BPF_MODE(insn->code) != BPF_MEM) { |
| 402 | verbose("BUG_ldx_%02x\n", insn->code); |
| 403 | return; |
| 404 | } |
| 405 | verbose("(%02x) r%d = *(%s *)(r%d %+d)\n", |
| 406 | insn->code, insn->dst_reg, |
| 407 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 408 | insn->src_reg, insn->off); |
| 409 | } else if (class == BPF_LD) { |
| 410 | if (BPF_MODE(insn->code) == BPF_ABS) { |
| 411 | verbose("(%02x) r0 = *(%s *)skb[%d]\n", |
| 412 | insn->code, |
| 413 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 414 | insn->imm); |
| 415 | } else if (BPF_MODE(insn->code) == BPF_IND) { |
| 416 | verbose("(%02x) r0 = *(%s *)skb[r%d + %d]\n", |
| 417 | insn->code, |
| 418 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 419 | insn->src_reg, insn->imm); |
Daniel Borkmann | 0d0e576 | 2017-05-08 00:04:09 +0200 | [diff] [blame] | 420 | } else if (BPF_MODE(insn->code) == BPF_IMM && |
| 421 | BPF_SIZE(insn->code) == BPF_DW) { |
| 422 | /* At this point, we already made sure that the second |
| 423 | * part of the ldimm64 insn is accessible. |
| 424 | */ |
| 425 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 426 | bool map_ptr = insn->src_reg == BPF_PSEUDO_MAP_FD; |
| 427 | |
| 428 | if (map_ptr && !env->allow_ptr_leaks) |
| 429 | imm = 0; |
| 430 | |
| 431 | verbose("(%02x) r%d = 0x%llx\n", insn->code, |
| 432 | insn->dst_reg, (unsigned long long)imm); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 433 | } else { |
| 434 | verbose("BUG_ld_%02x\n", insn->code); |
| 435 | return; |
| 436 | } |
| 437 | } else if (class == BPF_JMP) { |
| 438 | u8 opcode = BPF_OP(insn->code); |
| 439 | |
| 440 | if (opcode == BPF_CALL) { |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 441 | verbose("(%02x) call %s#%d\n", insn->code, |
| 442 | func_id_name(insn->imm), insn->imm); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 443 | } else if (insn->code == (BPF_JMP | BPF_JA)) { |
| 444 | verbose("(%02x) goto pc%+d\n", |
| 445 | insn->code, insn->off); |
| 446 | } else if (insn->code == (BPF_JMP | BPF_EXIT)) { |
| 447 | verbose("(%02x) exit\n", insn->code); |
| 448 | } else if (BPF_SRC(insn->code) == BPF_X) { |
| 449 | verbose("(%02x) if r%d %s r%d goto pc%+d\n", |
| 450 | insn->code, insn->dst_reg, |
| 451 | bpf_jmp_string[BPF_OP(insn->code) >> 4], |
| 452 | insn->src_reg, insn->off); |
| 453 | } else { |
| 454 | verbose("(%02x) if r%d %s 0x%x goto pc%+d\n", |
| 455 | insn->code, insn->dst_reg, |
| 456 | bpf_jmp_string[BPF_OP(insn->code) >> 4], |
| 457 | insn->imm, insn->off); |
| 458 | } |
| 459 | } else { |
| 460 | verbose("(%02x) %s\n", insn->code, bpf_class_string[class]); |
| 461 | } |
| 462 | } |
| 463 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 464 | 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] | 465 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 466 | struct bpf_verifier_stack_elem *elem; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 467 | int insn_idx; |
| 468 | |
| 469 | if (env->head == NULL) |
| 470 | return -1; |
| 471 | |
| 472 | memcpy(&env->cur_state, &env->head->st, sizeof(env->cur_state)); |
| 473 | insn_idx = env->head->insn_idx; |
| 474 | if (prev_insn_idx) |
| 475 | *prev_insn_idx = env->head->prev_insn_idx; |
| 476 | elem = env->head->next; |
| 477 | kfree(env->head); |
| 478 | env->head = elem; |
| 479 | env->stack_size--; |
| 480 | return insn_idx; |
| 481 | } |
| 482 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 483 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
| 484 | int insn_idx, int prev_insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 485 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 486 | struct bpf_verifier_stack_elem *elem; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 487 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 488 | elem = kmalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 489 | if (!elem) |
| 490 | goto err; |
| 491 | |
| 492 | memcpy(&elem->st, &env->cur_state, sizeof(env->cur_state)); |
| 493 | elem->insn_idx = insn_idx; |
| 494 | elem->prev_insn_idx = prev_insn_idx; |
| 495 | elem->next = env->head; |
| 496 | env->head = elem; |
| 497 | env->stack_size++; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 498 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 499 | verbose("BPF program is too complex\n"); |
| 500 | goto err; |
| 501 | } |
| 502 | return &elem->st; |
| 503 | err: |
| 504 | /* pop all elements and return */ |
| 505 | while (pop_stack(env, NULL) >= 0); |
| 506 | return NULL; |
| 507 | } |
| 508 | |
| 509 | #define CALLER_SAVED_REGS 6 |
| 510 | static const int caller_saved[CALLER_SAVED_REGS] = { |
| 511 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 |
| 512 | }; |
| 513 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 514 | static void __mark_reg_not_init(struct bpf_reg_state *reg); |
| 515 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 516 | /* Mark the unknown part of a register (variable offset or scalar value) as |
| 517 | * known to have the value @imm. |
| 518 | */ |
| 519 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
| 520 | { |
| 521 | reg->id = 0; |
| 522 | reg->var_off = tnum_const(imm); |
| 523 | reg->smin_value = (s64)imm; |
| 524 | reg->smax_value = (s64)imm; |
| 525 | reg->umin_value = imm; |
| 526 | reg->umax_value = imm; |
| 527 | } |
| 528 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 529 | /* Mark the 'variable offset' part of a register as zero. This should be |
| 530 | * used only on registers holding a pointer type. |
| 531 | */ |
| 532 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) |
| 533 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 534 | __mark_reg_known(reg, 0); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | static void mark_reg_known_zero(struct bpf_reg_state *regs, u32 regno) |
| 538 | { |
| 539 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
| 540 | verbose("mark_reg_known_zero(regs, %u)\n", regno); |
| 541 | /* Something bad happened, let's kill all regs */ |
| 542 | for (regno = 0; regno < MAX_BPF_REG; regno++) |
| 543 | __mark_reg_not_init(regs + regno); |
| 544 | return; |
| 545 | } |
| 546 | __mark_reg_known_zero(regs + regno); |
| 547 | } |
| 548 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 549 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
| 550 | { |
| 551 | return type_is_pkt_pointer(reg->type); |
| 552 | } |
| 553 | |
| 554 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) |
| 555 | { |
| 556 | return reg_is_pkt_pointer(reg) || |
| 557 | reg->type == PTR_TO_PACKET_END; |
| 558 | } |
| 559 | |
| 560 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ |
| 561 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, |
| 562 | enum bpf_reg_type which) |
| 563 | { |
| 564 | /* The register can already have a range from prior markings. |
| 565 | * This is fine as long as it hasn't been advanced from its |
| 566 | * origin. |
| 567 | */ |
| 568 | return reg->type == which && |
| 569 | reg->id == 0 && |
| 570 | reg->off == 0 && |
| 571 | tnum_equals_const(reg->var_off, 0); |
| 572 | } |
| 573 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 574 | /* Attempts to improve min/max values based on var_off information */ |
| 575 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
| 576 | { |
| 577 | /* min signed is max(sign bit) | min(other bits) */ |
| 578 | reg->smin_value = max_t(s64, reg->smin_value, |
| 579 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); |
| 580 | /* max signed is min(sign bit) | max(other bits) */ |
| 581 | reg->smax_value = min_t(s64, reg->smax_value, |
| 582 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); |
| 583 | reg->umin_value = max(reg->umin_value, reg->var_off.value); |
| 584 | reg->umax_value = min(reg->umax_value, |
| 585 | reg->var_off.value | reg->var_off.mask); |
| 586 | } |
| 587 | |
| 588 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
| 589 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
| 590 | { |
| 591 | /* Learn sign from signed bounds. |
| 592 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 593 | * are the same, so combine. This works even in the negative case, e.g. |
| 594 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 595 | */ |
| 596 | if (reg->smin_value >= 0 || reg->smax_value < 0) { |
| 597 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 598 | reg->umin_value); |
| 599 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 600 | reg->umax_value); |
| 601 | return; |
| 602 | } |
| 603 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 604 | * boundary, so we must be careful. |
| 605 | */ |
| 606 | if ((s64)reg->umax_value >= 0) { |
| 607 | /* Positive. We can't learn anything from the smin, but smax |
| 608 | * is positive, hence safe. |
| 609 | */ |
| 610 | reg->smin_value = reg->umin_value; |
| 611 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 612 | reg->umax_value); |
| 613 | } else if ((s64)reg->umin_value < 0) { |
| 614 | /* Negative. We can't learn anything from the smax, but smin |
| 615 | * is negative, hence safe. |
| 616 | */ |
| 617 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 618 | reg->umin_value); |
| 619 | reg->smax_value = reg->umax_value; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | /* Attempts to improve var_off based on unsigned min/max information */ |
| 624 | static void __reg_bound_offset(struct bpf_reg_state *reg) |
| 625 | { |
| 626 | reg->var_off = tnum_intersect(reg->var_off, |
| 627 | tnum_range(reg->umin_value, |
| 628 | reg->umax_value)); |
| 629 | } |
| 630 | |
| 631 | /* Reset the min/max bounds of a register */ |
| 632 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) |
| 633 | { |
| 634 | reg->smin_value = S64_MIN; |
| 635 | reg->smax_value = S64_MAX; |
| 636 | reg->umin_value = 0; |
| 637 | reg->umax_value = U64_MAX; |
| 638 | } |
| 639 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 640 | /* Mark a register as having a completely unknown (scalar) value. */ |
| 641 | static void __mark_reg_unknown(struct bpf_reg_state *reg) |
| 642 | { |
| 643 | reg->type = SCALAR_VALUE; |
| 644 | reg->id = 0; |
| 645 | reg->off = 0; |
| 646 | reg->var_off = tnum_unknown; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 647 | __mark_reg_unbounded(reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | static void mark_reg_unknown(struct bpf_reg_state *regs, u32 regno) |
| 651 | { |
| 652 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
| 653 | verbose("mark_reg_unknown(regs, %u)\n", regno); |
| 654 | /* Something bad happened, let's kill all regs */ |
| 655 | for (regno = 0; regno < MAX_BPF_REG; regno++) |
| 656 | __mark_reg_not_init(regs + regno); |
| 657 | return; |
| 658 | } |
| 659 | __mark_reg_unknown(regs + regno); |
| 660 | } |
| 661 | |
| 662 | static void __mark_reg_not_init(struct bpf_reg_state *reg) |
| 663 | { |
| 664 | __mark_reg_unknown(reg); |
| 665 | reg->type = NOT_INIT; |
| 666 | } |
| 667 | |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 668 | static void mark_reg_not_init(struct bpf_reg_state *regs, u32 regno) |
| 669 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 670 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
| 671 | verbose("mark_reg_not_init(regs, %u)\n", regno); |
| 672 | /* Something bad happened, let's kill all regs */ |
| 673 | for (regno = 0; regno < MAX_BPF_REG; regno++) |
| 674 | __mark_reg_not_init(regs + regno); |
| 675 | return; |
| 676 | } |
| 677 | __mark_reg_not_init(regs + regno); |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 678 | } |
| 679 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 680 | static void init_reg_state(struct bpf_reg_state *regs) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 681 | { |
| 682 | int i; |
| 683 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 684 | for (i = 0; i < MAX_BPF_REG; i++) { |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 685 | mark_reg_not_init(regs, i); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 686 | regs[i].live = REG_LIVE_NONE; |
| 687 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 688 | |
| 689 | /* frame pointer */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 690 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
| 691 | mark_reg_known_zero(regs, BPF_REG_FP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 692 | |
| 693 | /* 1st arg to a function */ |
| 694 | regs[BPF_REG_1].type = PTR_TO_CTX; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 695 | mark_reg_known_zero(regs, BPF_REG_1); |
Daniel Borkmann | 6760bf2 | 2016-12-18 01:52:59 +0100 | [diff] [blame] | 696 | } |
| 697 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 698 | enum reg_arg_type { |
| 699 | SRC_OP, /* register is used as source operand */ |
| 700 | DST_OP, /* register is used as destination operand */ |
| 701 | DST_OP_NO_MARK /* same as above, check only, don't mark */ |
| 702 | }; |
| 703 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 704 | static void mark_reg_read(const struct bpf_verifier_state *state, u32 regno) |
| 705 | { |
| 706 | struct bpf_verifier_state *parent = state->parent; |
| 707 | |
Alexei Starovoitov | 8fe2d6c | 2017-10-05 16:20:56 -0700 | [diff] [blame] | 708 | if (regno == BPF_REG_FP) |
| 709 | /* We don't need to worry about FP liveness because it's read-only */ |
| 710 | return; |
| 711 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 712 | while (parent) { |
| 713 | /* if read wasn't screened by an earlier write ... */ |
| 714 | if (state->regs[regno].live & REG_LIVE_WRITTEN) |
| 715 | break; |
| 716 | /* ... then we depend on parent's value */ |
| 717 | parent->regs[regno].live |= REG_LIVE_READ; |
| 718 | state = parent; |
| 719 | parent = state->parent; |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 724 | enum reg_arg_type t) |
| 725 | { |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 726 | struct bpf_reg_state *regs = env->cur_state.regs; |
| 727 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 728 | if (regno >= MAX_BPF_REG) { |
| 729 | verbose("R%d is invalid\n", regno); |
| 730 | return -EINVAL; |
| 731 | } |
| 732 | |
| 733 | if (t == SRC_OP) { |
| 734 | /* check whether register used as source operand can be read */ |
| 735 | if (regs[regno].type == NOT_INIT) { |
| 736 | verbose("R%d !read_ok\n", regno); |
| 737 | return -EACCES; |
| 738 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 739 | mark_reg_read(&env->cur_state, regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 740 | } else { |
| 741 | /* check whether register used as dest operand can be written to */ |
| 742 | if (regno == BPF_REG_FP) { |
| 743 | verbose("frame pointer is read only\n"); |
| 744 | return -EACCES; |
| 745 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 746 | regs[regno].live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 747 | if (t == DST_OP) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 748 | mark_reg_unknown(regs, regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 749 | } |
| 750 | return 0; |
| 751 | } |
| 752 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 753 | static bool is_spillable_regtype(enum bpf_reg_type type) |
| 754 | { |
| 755 | switch (type) { |
| 756 | case PTR_TO_MAP_VALUE: |
| 757 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 758 | case PTR_TO_STACK: |
| 759 | case PTR_TO_CTX: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 760 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 761 | case PTR_TO_PACKET_META: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 762 | case PTR_TO_PACKET_END: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 763 | case CONST_PTR_TO_MAP: |
| 764 | return true; |
| 765 | default: |
| 766 | return false; |
| 767 | } |
| 768 | } |
| 769 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 770 | /* check_stack_read/write functions track spill/fill of registers, |
| 771 | * stack boundary and alignment are checked in check_mem_access() |
| 772 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 773 | static int check_stack_write(struct bpf_verifier_state *state, int off, |
| 774 | int size, int value_regno) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 775 | { |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 776 | int i, spi = (MAX_BPF_STACK + off) / BPF_REG_SIZE; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 777 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
| 778 | * so it's aligned access and [off, off + size) are within stack limits |
| 779 | */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 780 | |
| 781 | if (value_regno >= 0 && |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 782 | is_spillable_regtype(state->regs[value_regno].type)) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 783 | |
| 784 | /* register containing pointer is being spilled into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 785 | if (size != BPF_REG_SIZE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 786 | verbose("invalid size of register spill\n"); |
| 787 | return -EACCES; |
| 788 | } |
| 789 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 790 | /* save register state */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 791 | state->spilled_regs[spi] = state->regs[value_regno]; |
| 792 | state->spilled_regs[spi].live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 793 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 794 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 795 | state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_SPILL; |
| 796 | } else { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 797 | /* regular write of data into stack */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 798 | state->spilled_regs[spi] = (struct bpf_reg_state) {}; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 799 | |
| 800 | for (i = 0; i < size; i++) |
| 801 | state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_MISC; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 802 | } |
| 803 | return 0; |
| 804 | } |
| 805 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 806 | static void mark_stack_slot_read(const struct bpf_verifier_state *state, int slot) |
| 807 | { |
| 808 | struct bpf_verifier_state *parent = state->parent; |
| 809 | |
| 810 | while (parent) { |
| 811 | /* if read wasn't screened by an earlier write ... */ |
| 812 | if (state->spilled_regs[slot].live & REG_LIVE_WRITTEN) |
| 813 | break; |
| 814 | /* ... then we depend on parent's value */ |
| 815 | parent->spilled_regs[slot].live |= REG_LIVE_READ; |
| 816 | state = parent; |
| 817 | parent = state->parent; |
| 818 | } |
| 819 | } |
| 820 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 821 | 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] | 822 | int value_regno) |
| 823 | { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 824 | u8 *slot_type; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 825 | int i, spi; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 826 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 827 | slot_type = &state->stack_slot_type[MAX_BPF_STACK + off]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 828 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 829 | if (slot_type[0] == STACK_SPILL) { |
| 830 | if (size != BPF_REG_SIZE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 831 | verbose("invalid size of register spill\n"); |
| 832 | return -EACCES; |
| 833 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 834 | for (i = 1; i < BPF_REG_SIZE; i++) { |
| 835 | if (slot_type[i] != STACK_SPILL) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 836 | verbose("corrupted spill memory\n"); |
| 837 | return -EACCES; |
| 838 | } |
| 839 | } |
| 840 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 841 | spi = (MAX_BPF_STACK + off) / BPF_REG_SIZE; |
| 842 | |
| 843 | if (value_regno >= 0) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 844 | /* restore register state from stack */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 845 | state->regs[value_regno] = state->spilled_regs[spi]; |
| 846 | mark_stack_slot_read(state, spi); |
| 847 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 848 | return 0; |
| 849 | } else { |
| 850 | for (i = 0; i < size; i++) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 851 | if (slot_type[i] != STACK_MISC) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 852 | verbose("invalid read from stack off %d+%d size %d\n", |
| 853 | off, i, size); |
| 854 | return -EACCES; |
| 855 | } |
| 856 | } |
| 857 | if (value_regno >= 0) |
| 858 | /* have read misc data from the stack */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 859 | mark_reg_unknown(state->regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 860 | return 0; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | /* check read/write into map element returned by bpf_map_lookup_elem() */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 865 | 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] | 866 | int size) |
| 867 | { |
| 868 | struct bpf_map *map = env->cur_state.regs[regno].map_ptr; |
| 869 | |
Gianluca Borello | 5722569 | 2017-01-09 10:19:47 -0800 | [diff] [blame] | 870 | if (off < 0 || size <= 0 || off + size > map->value_size) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 871 | verbose("invalid access to map value, value_size=%d off=%d size=%d\n", |
| 872 | map->value_size, off, size); |
| 873 | return -EACCES; |
| 874 | } |
| 875 | return 0; |
| 876 | } |
| 877 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 878 | /* check read/write into a map element with possible variable offset */ |
| 879 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 880 | int off, int size) |
| 881 | { |
| 882 | struct bpf_verifier_state *state = &env->cur_state; |
| 883 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 884 | int err; |
| 885 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 886 | /* We may have adjusted the register to this map value, so we |
| 887 | * need to try adding each of min_value and max_value to off |
| 888 | * to make sure our theoretical access will be safe. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 889 | */ |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame^] | 890 | if (verifier_log.level) |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 891 | print_verifier_state(state); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 892 | /* The minimum value is only important with signed |
| 893 | * comparisons where we can't assume the floor of a |
| 894 | * value is 0. If we are using signed variables for our |
| 895 | * index'es we need to make sure that whatever we use |
| 896 | * will have a set floor within our range. |
| 897 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 898 | if (reg->smin_value < 0) { |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 899 | verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
| 900 | regno); |
| 901 | return -EACCES; |
| 902 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 903 | err = __check_map_access(env, regno, reg->smin_value + off, size); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 904 | if (err) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 905 | verbose("R%d min value is outside of the array range\n", regno); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 906 | return err; |
| 907 | } |
| 908 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 909 | /* If we haven't set a max value then we need to bail since we can't be |
| 910 | * sure we won't do bad things. |
| 911 | * If reg->umax_value + off could overflow, treat that as unbounded too. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 912 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 913 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 914 | verbose("R%d unbounded memory access, make sure to bounds check any array access into a map\n", |
| 915 | regno); |
| 916 | return -EACCES; |
| 917 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 918 | err = __check_map_access(env, regno, reg->umax_value + off, size); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 919 | if (err) |
| 920 | verbose("R%d max value is outside of the array range\n", regno); |
| 921 | return err; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 922 | } |
| 923 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 924 | #define MAX_PACKET_OFF 0xffff |
| 925 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 926 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 927 | const struct bpf_call_arg_meta *meta, |
| 928 | enum bpf_access_type t) |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 929 | { |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 930 | switch (env->prog->type) { |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 931 | case BPF_PROG_TYPE_LWT_IN: |
| 932 | case BPF_PROG_TYPE_LWT_OUT: |
| 933 | /* dst_input() and dst_output() can't write for now */ |
| 934 | if (t == BPF_WRITE) |
| 935 | return false; |
Alexander Alemayhu | 7e57fbb | 2017-02-14 00:02:35 +0100 | [diff] [blame] | 936 | /* fallthrough */ |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 937 | case BPF_PROG_TYPE_SCHED_CLS: |
| 938 | case BPF_PROG_TYPE_SCHED_ACT: |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 939 | case BPF_PROG_TYPE_XDP: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 940 | case BPF_PROG_TYPE_LWT_XMIT: |
John Fastabend | 8a31db5 | 2017-08-15 22:33:09 -0700 | [diff] [blame] | 941 | case BPF_PROG_TYPE_SK_SKB: |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 942 | if (meta) |
| 943 | return meta->pkt_access; |
| 944 | |
| 945 | env->seen_direct_write = true; |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 946 | return true; |
| 947 | default: |
| 948 | return false; |
| 949 | } |
| 950 | } |
| 951 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 952 | static int __check_packet_access(struct bpf_verifier_env *env, u32 regno, |
| 953 | int off, int size) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 954 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 955 | struct bpf_reg_state *regs = env->cur_state.regs; |
| 956 | struct bpf_reg_state *reg = ®s[regno]; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 957 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 958 | if (off < 0 || size <= 0 || (u64)off + size > reg->range) { |
Alexei Starovoitov | d91b28e | 2016-05-19 18:17:13 -0700 | [diff] [blame] | 959 | verbose("invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", |
| 960 | off, size, regno, reg->id, reg->off, reg->range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 961 | return -EACCES; |
| 962 | } |
| 963 | return 0; |
| 964 | } |
| 965 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 966 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
| 967 | int size) |
| 968 | { |
| 969 | struct bpf_reg_state *regs = env->cur_state.regs; |
| 970 | struct bpf_reg_state *reg = ®s[regno]; |
| 971 | int err; |
| 972 | |
| 973 | /* We may have added a variable offset to the packet pointer; but any |
| 974 | * reg->range we have comes after that. We are only checking the fixed |
| 975 | * offset. |
| 976 | */ |
| 977 | |
| 978 | /* We don't allow negative numbers, because we aren't tracking enough |
| 979 | * detail to prove they're safe. |
| 980 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 981 | if (reg->smin_value < 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 982 | verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
| 983 | regno); |
| 984 | return -EACCES; |
| 985 | } |
| 986 | err = __check_packet_access(env, regno, off, size); |
| 987 | if (err) { |
| 988 | verbose("R%d offset is outside of the packet\n", regno); |
| 989 | return err; |
| 990 | } |
| 991 | return err; |
| 992 | } |
| 993 | |
| 994 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 995 | 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] | 996 | enum bpf_access_type t, enum bpf_reg_type *reg_type) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 997 | { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 998 | struct bpf_insn_access_aux info = { |
| 999 | .reg_type = *reg_type, |
| 1000 | }; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1001 | |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 1002 | /* for analyzer ctx accesses are already validated and converted */ |
| 1003 | if (env->analyzer_ops) |
| 1004 | return 0; |
| 1005 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1006 | if (env->prog->aux->ops->is_valid_access && |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 1007 | env->prog->aux->ops->is_valid_access(off, size, t, &info)) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 1008 | /* A non zero info.ctx_field_size indicates that this field is a |
| 1009 | * candidate for later verifier transformation to load the whole |
| 1010 | * field and then apply a mask when accessed with a narrower |
| 1011 | * access than actual ctx access size. A zero info.ctx_field_size |
| 1012 | * will only allow for whole field access and rejects any other |
| 1013 | * type of narrower access. |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1014 | */ |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 1015 | 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] | 1016 | *reg_type = info.reg_type; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1017 | |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 1018 | /* remember the offset of last byte accessed in ctx */ |
| 1019 | if (env->prog->aux->max_ctx_offset < off + size) |
| 1020 | env->prog->aux->max_ctx_offset = off + size; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1021 | return 0; |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 1022 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1023 | |
| 1024 | verbose("invalid bpf_context access off=%d size=%d\n", off, size); |
| 1025 | return -EACCES; |
| 1026 | } |
| 1027 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1028 | static bool __is_pointer_value(bool allow_ptr_leaks, |
| 1029 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1030 | { |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1031 | if (allow_ptr_leaks) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1032 | return false; |
| 1033 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1034 | return reg->type != SCALAR_VALUE; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1037 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
| 1038 | { |
| 1039 | return __is_pointer_value(env->allow_ptr_leaks, &env->cur_state.regs[regno]); |
| 1040 | } |
| 1041 | |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1042 | 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] | 1043 | int off, int size, bool strict) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1044 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1045 | struct tnum reg_off; |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1046 | int ip_align; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1047 | |
| 1048 | /* Byte size accesses are always allowed. */ |
| 1049 | if (!strict || size == 1) |
| 1050 | return 0; |
| 1051 | |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1052 | /* For platforms that do not have a Kconfig enabling |
| 1053 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of |
| 1054 | * NET_IP_ALIGN is universally set to '2'. And on platforms |
| 1055 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get |
| 1056 | * to this code only in strict mode where we want to emulate |
| 1057 | * the NET_IP_ALIGN==2 checking. Therefore use an |
| 1058 | * unconditional IP align value of '2'. |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1059 | */ |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1060 | ip_align = 2; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1061 | |
| 1062 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); |
| 1063 | if (!tnum_is_aligned(reg_off, size)) { |
| 1064 | char tn_buf[48]; |
| 1065 | |
| 1066 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 1067 | verbose("misaligned packet access off %d+%s+%d+%d size %d\n", |
| 1068 | ip_align, tn_buf, reg->off, off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1069 | return -EACCES; |
| 1070 | } |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1071 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1072 | return 0; |
| 1073 | } |
| 1074 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1075 | static int check_generic_ptr_alignment(const struct bpf_reg_state *reg, |
| 1076 | const char *pointer_desc, |
| 1077 | int off, int size, bool strict) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1078 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1079 | struct tnum reg_off; |
| 1080 | |
| 1081 | /* Byte size accesses are always allowed. */ |
| 1082 | if (!strict || size == 1) |
| 1083 | return 0; |
| 1084 | |
| 1085 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); |
| 1086 | if (!tnum_is_aligned(reg_off, size)) { |
| 1087 | char tn_buf[48]; |
| 1088 | |
| 1089 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 1090 | verbose("misaligned %saccess off %s+%d+%d size %d\n", |
| 1091 | pointer_desc, tn_buf, reg->off, off, size); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1092 | return -EACCES; |
| 1093 | } |
| 1094 | |
| 1095 | return 0; |
| 1096 | } |
| 1097 | |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1098 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
| 1099 | const struct bpf_reg_state *reg, |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1100 | int off, int size) |
| 1101 | { |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1102 | bool strict = env->strict_alignment; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1103 | const char *pointer_desc = ""; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1104 | |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1105 | switch (reg->type) { |
| 1106 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1107 | case PTR_TO_PACKET_META: |
| 1108 | /* Special case, because of NET_IP_ALIGN. Given metadata sits |
| 1109 | * right in front, treat it the very same way. |
| 1110 | */ |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1111 | return check_pkt_ptr_alignment(reg, off, size, strict); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1112 | case PTR_TO_MAP_VALUE: |
| 1113 | pointer_desc = "value "; |
| 1114 | break; |
| 1115 | case PTR_TO_CTX: |
| 1116 | pointer_desc = "context "; |
| 1117 | break; |
| 1118 | case PTR_TO_STACK: |
| 1119 | pointer_desc = "stack "; |
| 1120 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1121 | default: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1122 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1123 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1124 | return check_generic_ptr_alignment(reg, pointer_desc, off, size, strict); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1125 | } |
| 1126 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1127 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 1128 | * if t==write, value_regno is a register which value is stored into memory |
| 1129 | * if t==read, value_regno is a register which will receive the value from memory |
| 1130 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 1131 | * if t==read && value_regno==-1, don't care what we read from memory |
| 1132 | */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1133 | 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] | 1134 | int bpf_size, enum bpf_access_type t, |
| 1135 | int value_regno) |
| 1136 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1137 | struct bpf_verifier_state *state = &env->cur_state; |
| 1138 | struct bpf_reg_state *reg = &state->regs[regno]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1139 | int size, err = 0; |
| 1140 | |
| 1141 | size = bpf_size_to_bytes(bpf_size); |
| 1142 | if (size < 0) |
| 1143 | return size; |
| 1144 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1145 | /* alignment checks will add in reg->off themselves */ |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1146 | err = check_ptr_alignment(env, reg, off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1147 | if (err) |
| 1148 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1149 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1150 | /* for access checks, reg->off is just part of off */ |
| 1151 | off += reg->off; |
| 1152 | |
| 1153 | if (reg->type == PTR_TO_MAP_VALUE) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1154 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1155 | is_pointer_value(env, value_regno)) { |
| 1156 | verbose("R%d leaks addr into map\n", value_regno); |
| 1157 | return -EACCES; |
| 1158 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1159 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1160 | err = check_map_access(env, regno, off, size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1161 | if (!err && t == BPF_READ && value_regno >= 0) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1162 | mark_reg_unknown(state->regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1163 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1164 | } else if (reg->type == PTR_TO_CTX) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1165 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 1166 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1167 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1168 | is_pointer_value(env, value_regno)) { |
| 1169 | verbose("R%d leaks addr into ctx\n", value_regno); |
| 1170 | return -EACCES; |
| 1171 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1172 | /* ctx accesses must be at a fixed offset, so that we can |
| 1173 | * determine what type of data were returned. |
| 1174 | */ |
| 1175 | if (!tnum_is_const(reg->var_off)) { |
| 1176 | char tn_buf[48]; |
| 1177 | |
| 1178 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 1179 | verbose("variable ctx access var_off=%s off=%d size=%d", |
| 1180 | tn_buf, off, size); |
| 1181 | return -EACCES; |
| 1182 | } |
| 1183 | off += reg->var_off.value; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1184 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1185 | if (!err && t == BPF_READ && value_regno >= 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1186 | /* ctx access returns either a scalar, or a |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1187 | * PTR_TO_PACKET[_META,_END]. In the latter |
| 1188 | * case, we know the offset is zero. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1189 | */ |
| 1190 | if (reg_type == SCALAR_VALUE) |
| 1191 | mark_reg_unknown(state->regs, value_regno); |
| 1192 | else |
| 1193 | mark_reg_known_zero(state->regs, value_regno); |
| 1194 | state->regs[value_regno].id = 0; |
| 1195 | state->regs[value_regno].off = 0; |
| 1196 | state->regs[value_regno].range = 0; |
Mickaël Salaün | 1955351 | 2016-09-24 20:01:50 +0200 | [diff] [blame] | 1197 | state->regs[value_regno].type = reg_type; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1198 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1199 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1200 | } else if (reg->type == PTR_TO_STACK) { |
| 1201 | /* stack accesses must be at a fixed offset, so that we can |
| 1202 | * determine what type of data were returned. |
| 1203 | * See check_stack_read(). |
| 1204 | */ |
| 1205 | if (!tnum_is_const(reg->var_off)) { |
| 1206 | char tn_buf[48]; |
| 1207 | |
| 1208 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 1209 | verbose("variable stack access var_off=%s off=%d size=%d", |
| 1210 | tn_buf, off, size); |
| 1211 | return -EACCES; |
| 1212 | } |
| 1213 | off += reg->var_off.value; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1214 | if (off >= 0 || off < -MAX_BPF_STACK) { |
| 1215 | verbose("invalid stack off=%d size=%d\n", off, size); |
| 1216 | return -EACCES; |
| 1217 | } |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 1218 | |
| 1219 | if (env->prog->aux->stack_depth < -off) |
| 1220 | env->prog->aux->stack_depth = -off; |
| 1221 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1222 | if (t == BPF_WRITE) { |
| 1223 | if (!env->allow_ptr_leaks && |
| 1224 | state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL && |
| 1225 | size != BPF_REG_SIZE) { |
| 1226 | verbose("attempt to corrupt spilled pointer on stack\n"); |
| 1227 | return -EACCES; |
| 1228 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1229 | err = check_stack_write(state, off, size, value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1230 | } else { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1231 | err = check_stack_read(state, off, size, value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1232 | } |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1233 | } else if (reg_is_pkt_pointer(reg)) { |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1234 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1235 | verbose("cannot write into packet\n"); |
| 1236 | return -EACCES; |
| 1237 | } |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1238 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1239 | is_pointer_value(env, value_regno)) { |
| 1240 | verbose("R%d leaks addr into packet\n", value_regno); |
| 1241 | return -EACCES; |
| 1242 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1243 | err = check_packet_access(env, regno, off, size); |
| 1244 | if (!err && t == BPF_READ && value_regno >= 0) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1245 | mark_reg_unknown(state->regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1246 | } else { |
| 1247 | verbose("R%d invalid mem access '%s'\n", |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1248 | regno, reg_type_str[reg->type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1249 | return -EACCES; |
| 1250 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1251 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1252 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
| 1253 | state->regs[value_regno].type == SCALAR_VALUE) { |
| 1254 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
| 1255 | state->regs[value_regno].var_off = tnum_cast( |
| 1256 | state->regs[value_regno].var_off, size); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1257 | __update_reg_bounds(&state->regs[value_regno]); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1258 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1259 | return err; |
| 1260 | } |
| 1261 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1262 | 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] | 1263 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1264 | int err; |
| 1265 | |
| 1266 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || |
| 1267 | insn->imm != 0) { |
| 1268 | verbose("BPF_XADD uses reserved fields\n"); |
| 1269 | return -EINVAL; |
| 1270 | } |
| 1271 | |
| 1272 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1273 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1274 | if (err) |
| 1275 | return err; |
| 1276 | |
| 1277 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1278 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1279 | if (err) |
| 1280 | return err; |
| 1281 | |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 1282 | if (is_pointer_value(env, insn->src_reg)) { |
| 1283 | verbose("R%d leaks addr into mem\n", insn->src_reg); |
| 1284 | return -EACCES; |
| 1285 | } |
| 1286 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1287 | /* check whether atomic_add can read the memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1288 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1289 | BPF_SIZE(insn->code), BPF_READ, -1); |
| 1290 | if (err) |
| 1291 | return err; |
| 1292 | |
| 1293 | /* check whether atomic_add can write into the same memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1294 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1295 | BPF_SIZE(insn->code), BPF_WRITE, -1); |
| 1296 | } |
| 1297 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1298 | /* Does this register contain a constant zero? */ |
| 1299 | static bool register_is_null(struct bpf_reg_state reg) |
| 1300 | { |
| 1301 | return reg.type == SCALAR_VALUE && tnum_equals_const(reg.var_off, 0); |
| 1302 | } |
| 1303 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1304 | /* when register 'regno' is passed into function that will read 'access_size' |
| 1305 | * bytes from that pointer, make sure that it's within stack boundary |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1306 | * and all elements of stack are initialized. |
| 1307 | * Unlike most pointer bounds-checking functions, this one doesn't take an |
| 1308 | * 'off' argument, so it has to add in reg->off itself. |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1309 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1310 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1311 | int access_size, bool zero_size_allowed, |
| 1312 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1313 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1314 | struct bpf_verifier_state *state = &env->cur_state; |
| 1315 | struct bpf_reg_state *regs = state->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1316 | int off, i; |
| 1317 | |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1318 | if (regs[regno].type != PTR_TO_STACK) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1319 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1320 | if (zero_size_allowed && access_size == 0 && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1321 | register_is_null(regs[regno])) |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1322 | return 0; |
| 1323 | |
| 1324 | verbose("R%d type=%s expected=%s\n", regno, |
| 1325 | reg_type_str[regs[regno].type], |
| 1326 | reg_type_str[PTR_TO_STACK]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1327 | return -EACCES; |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1328 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1329 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1330 | /* Only allow fixed-offset stack reads */ |
| 1331 | if (!tnum_is_const(regs[regno].var_off)) { |
| 1332 | char tn_buf[48]; |
| 1333 | |
| 1334 | tnum_strn(tn_buf, sizeof(tn_buf), regs[regno].var_off); |
| 1335 | verbose("invalid variable stack read R%d var_off=%s\n", |
| 1336 | regno, tn_buf); |
| 1337 | } |
| 1338 | off = regs[regno].off + regs[regno].var_off.value; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1339 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
| 1340 | access_size <= 0) { |
| 1341 | verbose("invalid stack type R%d off=%d access_size=%d\n", |
| 1342 | regno, off, access_size); |
| 1343 | return -EACCES; |
| 1344 | } |
| 1345 | |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 1346 | if (env->prog->aux->stack_depth < -off) |
| 1347 | env->prog->aux->stack_depth = -off; |
| 1348 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1349 | if (meta && meta->raw_mode) { |
| 1350 | meta->access_size = access_size; |
| 1351 | meta->regno = regno; |
| 1352 | return 0; |
| 1353 | } |
| 1354 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1355 | for (i = 0; i < access_size; i++) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1356 | if (state->stack_slot_type[MAX_BPF_STACK + off + i] != STACK_MISC) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1357 | verbose("invalid indirect read from stack off %d+%d size %d\n", |
| 1358 | off, i, access_size); |
| 1359 | return -EACCES; |
| 1360 | } |
| 1361 | } |
| 1362 | return 0; |
| 1363 | } |
| 1364 | |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1365 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
| 1366 | int access_size, bool zero_size_allowed, |
| 1367 | struct bpf_call_arg_meta *meta) |
| 1368 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1369 | struct bpf_reg_state *regs = env->cur_state.regs, *reg = ®s[regno]; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1370 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1371 | switch (reg->type) { |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1372 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1373 | case PTR_TO_PACKET_META: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1374 | return check_packet_access(env, regno, reg->off, access_size); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1375 | case PTR_TO_MAP_VALUE: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1376 | return check_map_access(env, regno, reg->off, access_size); |
| 1377 | default: /* scalar_value|ptr_to_stack or invalid ptr */ |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1378 | return check_stack_boundary(env, regno, access_size, |
| 1379 | zero_size_allowed, meta); |
| 1380 | } |
| 1381 | } |
| 1382 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1383 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1384 | enum bpf_arg_type arg_type, |
| 1385 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1386 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1387 | struct bpf_reg_state *regs = env->cur_state.regs, *reg = ®s[regno]; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1388 | enum bpf_reg_type expected_type, type = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1389 | int err = 0; |
| 1390 | |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 1391 | if (arg_type == ARG_DONTCARE) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1392 | return 0; |
| 1393 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1394 | err = check_reg_arg(env, regno, SRC_OP); |
| 1395 | if (err) |
| 1396 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1397 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1398 | if (arg_type == ARG_ANYTHING) { |
| 1399 | if (is_pointer_value(env, regno)) { |
| 1400 | verbose("R%d leaks addr into helper function\n", regno); |
| 1401 | return -EACCES; |
| 1402 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 1403 | return 0; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1404 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 1405 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1406 | if (type_is_pkt_pointer(type) && |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1407 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1408 | verbose("helper access to the packet is not allowed\n"); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1409 | return -EACCES; |
| 1410 | } |
| 1411 | |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1412 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1413 | arg_type == ARG_PTR_TO_MAP_VALUE) { |
| 1414 | expected_type = PTR_TO_STACK; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1415 | if (!type_is_pkt_pointer(type) && |
| 1416 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1417 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1418 | } else if (arg_type == ARG_CONST_SIZE || |
| 1419 | arg_type == ARG_CONST_SIZE_OR_ZERO) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1420 | expected_type = SCALAR_VALUE; |
| 1421 | if (type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1422 | goto err_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1423 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
| 1424 | expected_type = CONST_PTR_TO_MAP; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1425 | if (type != expected_type) |
| 1426 | goto err_type; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1427 | } else if (arg_type == ARG_PTR_TO_CTX) { |
| 1428 | expected_type = PTR_TO_CTX; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1429 | if (type != expected_type) |
| 1430 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1431 | } else if (arg_type == ARG_PTR_TO_MEM || |
| 1432 | arg_type == ARG_PTR_TO_UNINIT_MEM) { |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1433 | expected_type = PTR_TO_STACK; |
| 1434 | /* One exception here. In case function allows for NULL to be |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1435 | * passed in as argument, it's a SCALAR_VALUE type. Final test |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1436 | * happens during stack boundary checking. |
| 1437 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1438 | if (register_is_null(*reg)) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1439 | /* final test in check_stack_boundary() */; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1440 | else if (!type_is_pkt_pointer(type) && |
| 1441 | type != PTR_TO_MAP_VALUE && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1442 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1443 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1444 | meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1445 | } else { |
| 1446 | verbose("unsupported arg_type %d\n", arg_type); |
| 1447 | return -EFAULT; |
| 1448 | } |
| 1449 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1450 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 1451 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1452 | meta->map_ptr = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1453 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 1454 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 1455 | * check that [key, key + map->key_size) are within |
| 1456 | * stack limits and initialized |
| 1457 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1458 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1459 | /* in function declaration map_ptr must come before |
| 1460 | * map_key, so that it's verified and known before |
| 1461 | * we have to check map_key here. Otherwise it means |
| 1462 | * that kernel subsystem misconfigured verifier |
| 1463 | */ |
| 1464 | verbose("invalid map_ptr to access map->key\n"); |
| 1465 | return -EACCES; |
| 1466 | } |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1467 | if (type_is_pkt_pointer(type)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1468 | err = check_packet_access(env, regno, reg->off, |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1469 | meta->map_ptr->key_size); |
| 1470 | else |
| 1471 | err = check_stack_boundary(env, regno, |
| 1472 | meta->map_ptr->key_size, |
| 1473 | false, NULL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1474 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE) { |
| 1475 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 1476 | * check [value, value + map->value_size) validity |
| 1477 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1478 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1479 | /* kernel subsystem misconfigured verifier */ |
| 1480 | verbose("invalid map_ptr to access map->value\n"); |
| 1481 | return -EACCES; |
| 1482 | } |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1483 | if (type_is_pkt_pointer(type)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1484 | err = check_packet_access(env, regno, reg->off, |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1485 | meta->map_ptr->value_size); |
| 1486 | else |
| 1487 | err = check_stack_boundary(env, regno, |
| 1488 | meta->map_ptr->value_size, |
| 1489 | false, NULL); |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1490 | } else if (arg_type == ARG_CONST_SIZE || |
| 1491 | arg_type == ARG_CONST_SIZE_OR_ZERO) { |
| 1492 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1493 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1494 | /* bpf_xxx(..., buf, len) call will access 'len' bytes |
| 1495 | * from stack pointer 'buf'. Check it |
| 1496 | * note: regno == len, regno - 1 == buf |
| 1497 | */ |
| 1498 | if (regno == 0) { |
| 1499 | /* kernel subsystem misconfigured verifier */ |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1500 | verbose("ARG_CONST_SIZE cannot be first argument\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1501 | return -EACCES; |
| 1502 | } |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1503 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1504 | /* The register is SCALAR_VALUE; the access check |
| 1505 | * happens using its boundaries. |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1506 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1507 | |
| 1508 | if (!tnum_is_const(reg->var_off)) |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1509 | /* For unprivileged variable accesses, disable raw |
| 1510 | * mode so that the program is required to |
| 1511 | * initialize all the memory that the helper could |
| 1512 | * just partially fill up. |
| 1513 | */ |
| 1514 | meta = NULL; |
| 1515 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1516 | if (reg->smin_value < 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1517 | verbose("R%d min value is negative, either use unsigned or 'var &= const'\n", |
| 1518 | regno); |
| 1519 | return -EACCES; |
| 1520 | } |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1521 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1522 | if (reg->umin_value == 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1523 | err = check_helper_mem_access(env, regno - 1, 0, |
| 1524 | zero_size_allowed, |
| 1525 | meta); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1526 | if (err) |
| 1527 | return err; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1528 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1529 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1530 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1531 | verbose("R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", |
| 1532 | regno); |
| 1533 | return -EACCES; |
| 1534 | } |
| 1535 | err = check_helper_mem_access(env, regno - 1, |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1536 | reg->umax_value, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1537 | zero_size_allowed, meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | return err; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1541 | err_type: |
| 1542 | verbose("R%d type=%s expected=%s\n", regno, |
| 1543 | reg_type_str[type], reg_type_str[expected_type]); |
| 1544 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1545 | } |
| 1546 | |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1547 | static int check_map_func_compatibility(struct bpf_map *map, int func_id) |
| 1548 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1549 | if (!map) |
| 1550 | return 0; |
| 1551 | |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1552 | /* We need a two way check, first is from map perspective ... */ |
| 1553 | switch (map->map_type) { |
| 1554 | case BPF_MAP_TYPE_PROG_ARRAY: |
| 1555 | if (func_id != BPF_FUNC_tail_call) |
| 1556 | goto error; |
| 1557 | break; |
| 1558 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
| 1559 | if (func_id != BPF_FUNC_perf_event_read && |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 1560 | func_id != BPF_FUNC_perf_event_output && |
| 1561 | func_id != BPF_FUNC_perf_event_read_value) |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1562 | goto error; |
| 1563 | break; |
| 1564 | case BPF_MAP_TYPE_STACK_TRACE: |
| 1565 | if (func_id != BPF_FUNC_get_stackid) |
| 1566 | goto error; |
| 1567 | break; |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 1568 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
David S. Miller | 60747ef | 2016-08-18 01:17:32 -0400 | [diff] [blame] | 1569 | if (func_id != BPF_FUNC_skb_under_cgroup && |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 1570 | func_id != BPF_FUNC_current_task_under_cgroup) |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 1571 | goto error; |
| 1572 | break; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 1573 | /* devmap returns a pointer to a live net_device ifindex that we cannot |
| 1574 | * allow to be modified from bpf side. So do not allow lookup elements |
| 1575 | * for now. |
| 1576 | */ |
| 1577 | case BPF_MAP_TYPE_DEVMAP: |
John Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 1578 | if (func_id != BPF_FUNC_redirect_map) |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 1579 | goto error; |
| 1580 | break; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 1581 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 1582 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 1583 | if (func_id != BPF_FUNC_map_lookup_elem) |
| 1584 | goto error; |
Martin KaFai Lau | 16a4362 | 2017-08-17 18:14:43 -0700 | [diff] [blame] | 1585 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1586 | case BPF_MAP_TYPE_SOCKMAP: |
| 1587 | if (func_id != BPF_FUNC_sk_redirect_map && |
| 1588 | func_id != BPF_FUNC_sock_map_update && |
| 1589 | func_id != BPF_FUNC_map_delete_elem) |
| 1590 | goto error; |
| 1591 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1592 | default: |
| 1593 | break; |
| 1594 | } |
| 1595 | |
| 1596 | /* ... and second from the function itself. */ |
| 1597 | switch (func_id) { |
| 1598 | case BPF_FUNC_tail_call: |
| 1599 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 1600 | goto error; |
| 1601 | break; |
| 1602 | case BPF_FUNC_perf_event_read: |
| 1603 | case BPF_FUNC_perf_event_output: |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 1604 | case BPF_FUNC_perf_event_read_value: |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1605 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
| 1606 | goto error; |
| 1607 | break; |
| 1608 | case BPF_FUNC_get_stackid: |
| 1609 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) |
| 1610 | goto error; |
| 1611 | break; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 1612 | case BPF_FUNC_current_task_under_cgroup: |
Daniel Borkmann | 747ea55 | 2016-08-12 22:17:17 +0200 | [diff] [blame] | 1613 | case BPF_FUNC_skb_under_cgroup: |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 1614 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
| 1615 | goto error; |
| 1616 | break; |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 1617 | case BPF_FUNC_redirect_map: |
| 1618 | if (map->map_type != BPF_MAP_TYPE_DEVMAP) |
| 1619 | goto error; |
| 1620 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1621 | case BPF_FUNC_sk_redirect_map: |
| 1622 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 1623 | goto error; |
| 1624 | break; |
| 1625 | case BPF_FUNC_sock_map_update: |
| 1626 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 1627 | goto error; |
| 1628 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1629 | default: |
| 1630 | break; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | return 0; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1634 | error: |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 1635 | verbose("cannot pass map_type %d into func %s#%d\n", |
| 1636 | map->map_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1637 | return -EINVAL; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1638 | } |
| 1639 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1640 | static int check_raw_mode(const struct bpf_func_proto *fn) |
| 1641 | { |
| 1642 | int count = 0; |
| 1643 | |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1644 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1645 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1646 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1647 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1648 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1649 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1650 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1651 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1652 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1653 | count++; |
| 1654 | |
| 1655 | return count > 1 ? -EINVAL : 0; |
| 1656 | } |
| 1657 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1658 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
| 1659 | * are now invalid, so turn them into unknown SCALAR_VALUE. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1660 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1661 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1662 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1663 | struct bpf_verifier_state *state = &env->cur_state; |
| 1664 | struct bpf_reg_state *regs = state->regs, *reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1665 | int i; |
| 1666 | |
| 1667 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1668 | if (reg_is_pkt_pointer_any(®s[i])) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1669 | mark_reg_unknown(regs, i); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1670 | |
| 1671 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { |
| 1672 | if (state->stack_slot_type[i] != STACK_SPILL) |
| 1673 | continue; |
| 1674 | reg = &state->spilled_regs[i / BPF_REG_SIZE]; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1675 | if (reg_is_pkt_pointer_any(reg)) |
| 1676 | __mark_reg_unknown(reg); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1677 | } |
| 1678 | } |
| 1679 | |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 1680 | 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] | 1681 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1682 | struct bpf_verifier_state *state = &env->cur_state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1683 | const struct bpf_func_proto *fn = NULL; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1684 | struct bpf_reg_state *regs = state->regs; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1685 | struct bpf_call_arg_meta meta; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1686 | bool changes_data; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1687 | int i, err; |
| 1688 | |
| 1689 | /* find function prototype */ |
| 1690 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 1691 | 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] | 1692 | return -EINVAL; |
| 1693 | } |
| 1694 | |
| 1695 | if (env->prog->aux->ops->get_func_proto) |
| 1696 | fn = env->prog->aux->ops->get_func_proto(func_id); |
| 1697 | |
| 1698 | if (!fn) { |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 1699 | 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] | 1700 | return -EINVAL; |
| 1701 | } |
| 1702 | |
| 1703 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 1704 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1705 | verbose("cannot call GPL only function from proprietary program\n"); |
| 1706 | return -EINVAL; |
| 1707 | } |
| 1708 | |
Martin KaFai Lau | 17bedab | 2016-12-07 15:53:11 -0800 | [diff] [blame] | 1709 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1710 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1711 | memset(&meta, 0, sizeof(meta)); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1712 | meta.pkt_access = fn->pkt_access; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1713 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1714 | /* We only support one arg being in raw mode at the moment, which |
| 1715 | * is sufficient for the helper functions we have right now. |
| 1716 | */ |
| 1717 | err = check_raw_mode(fn); |
| 1718 | if (err) { |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 1719 | verbose("kernel subsystem misconfigured func %s#%d\n", |
| 1720 | func_id_name(func_id), func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1721 | return err; |
| 1722 | } |
| 1723 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1724 | /* check args */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1725 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1726 | if (err) |
| 1727 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1728 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1729 | if (err) |
| 1730 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1731 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1732 | if (err) |
| 1733 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1734 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1735 | if (err) |
| 1736 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1737 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1738 | if (err) |
| 1739 | return err; |
| 1740 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1741 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
| 1742 | * is inferred from register state. |
| 1743 | */ |
| 1744 | for (i = 0; i < meta.access_size; i++) { |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1745 | 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] | 1746 | if (err) |
| 1747 | return err; |
| 1748 | } |
| 1749 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1750 | /* reset caller saved regs */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1751 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 1752 | mark_reg_not_init(regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1753 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 1754 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1755 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1756 | /* update return register (already marked as written above) */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1757 | if (fn->ret_type == RET_INTEGER) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1758 | /* sets type to SCALAR_VALUE */ |
| 1759 | mark_reg_unknown(regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1760 | } else if (fn->ret_type == RET_VOID) { |
| 1761 | regs[BPF_REG_0].type = NOT_INIT; |
| 1762 | } 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] | 1763 | struct bpf_insn_aux_data *insn_aux; |
| 1764 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1765 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1766 | /* There is no offset yet applied, variable or fixed */ |
| 1767 | mark_reg_known_zero(regs, BPF_REG_0); |
| 1768 | regs[BPF_REG_0].off = 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1769 | /* remember map_ptr, so that check_map_access() |
| 1770 | * can check 'value_size' boundary of memory access |
| 1771 | * to map element returned from bpf_map_lookup_elem() |
| 1772 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1773 | if (meta.map_ptr == NULL) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1774 | verbose("kernel subsystem misconfigured verifier\n"); |
| 1775 | return -EINVAL; |
| 1776 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1777 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 1778 | regs[BPF_REG_0].id = ++env->id_gen; |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 1779 | insn_aux = &env->insn_aux_data[insn_idx]; |
| 1780 | if (!insn_aux->map_ptr) |
| 1781 | insn_aux->map_ptr = meta.map_ptr; |
| 1782 | else if (insn_aux->map_ptr != meta.map_ptr) |
| 1783 | insn_aux->map_ptr = BPF_MAP_PTR_POISON; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1784 | } else { |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 1785 | verbose("unknown return type %d of func %s#%d\n", |
| 1786 | fn->ret_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1787 | return -EINVAL; |
| 1788 | } |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1789 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1790 | err = check_map_func_compatibility(meta.map_ptr, func_id); |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1791 | if (err) |
| 1792 | return err; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1793 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1794 | if (changes_data) |
| 1795 | clear_all_pkt_pointers(env); |
| 1796 | return 0; |
| 1797 | } |
| 1798 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1799 | static void coerce_reg_to_32(struct bpf_reg_state *reg) |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1800 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1801 | /* clear high 32 bits */ |
| 1802 | reg->var_off = tnum_cast(reg->var_off, 4); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1803 | /* Update bounds */ |
| 1804 | __update_reg_bounds(reg); |
| 1805 | } |
| 1806 | |
| 1807 | static bool signed_add_overflows(s64 a, s64 b) |
| 1808 | { |
| 1809 | /* Do the add in u64, where overflow is well-defined */ |
| 1810 | s64 res = (s64)((u64)a + (u64)b); |
| 1811 | |
| 1812 | if (b < 0) |
| 1813 | return res > a; |
| 1814 | return res < a; |
| 1815 | } |
| 1816 | |
| 1817 | static bool signed_sub_overflows(s64 a, s64 b) |
| 1818 | { |
| 1819 | /* Do the sub in u64, where overflow is well-defined */ |
| 1820 | s64 res = (s64)((u64)a - (u64)b); |
| 1821 | |
| 1822 | if (b < 0) |
| 1823 | return res < a; |
| 1824 | return res > a; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1825 | } |
| 1826 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1827 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1828 | * Caller should also handle BPF_MOV case separately. |
| 1829 | * If we return -EACCES, caller may want to try again treating pointer as a |
| 1830 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. |
| 1831 | */ |
| 1832 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, |
| 1833 | struct bpf_insn *insn, |
| 1834 | const struct bpf_reg_state *ptr_reg, |
| 1835 | const struct bpf_reg_state *off_reg) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1836 | { |
| 1837 | struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1838 | bool known = tnum_is_const(off_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1839 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
| 1840 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; |
| 1841 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, |
| 1842 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1843 | u8 opcode = BPF_OP(insn->code); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1844 | u32 dst = insn->dst_reg; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1845 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1846 | dst_reg = ®s[dst]; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1847 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1848 | if (WARN_ON_ONCE(known && (smin_val != smax_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1849 | print_verifier_state(&env->cur_state); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1850 | verbose("verifier internal error: known but bad sbounds\n"); |
| 1851 | return -EINVAL; |
| 1852 | } |
| 1853 | if (WARN_ON_ONCE(known && (umin_val != umax_val))) { |
| 1854 | print_verifier_state(&env->cur_state); |
| 1855 | verbose("verifier internal error: known but bad ubounds\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1856 | return -EINVAL; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1857 | } |
| 1858 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1859 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 1860 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ |
| 1861 | if (!env->allow_ptr_leaks) |
| 1862 | verbose("R%d 32-bit pointer arithmetic prohibited\n", |
| 1863 | dst); |
| 1864 | return -EACCES; |
| 1865 | } |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1866 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1867 | if (ptr_reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
| 1868 | if (!env->allow_ptr_leaks) |
| 1869 | verbose("R%d pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL prohibited, null-check it first\n", |
| 1870 | dst); |
| 1871 | return -EACCES; |
| 1872 | } |
| 1873 | if (ptr_reg->type == CONST_PTR_TO_MAP) { |
| 1874 | if (!env->allow_ptr_leaks) |
| 1875 | verbose("R%d pointer arithmetic on CONST_PTR_TO_MAP prohibited\n", |
| 1876 | dst); |
| 1877 | return -EACCES; |
| 1878 | } |
| 1879 | if (ptr_reg->type == PTR_TO_PACKET_END) { |
| 1880 | if (!env->allow_ptr_leaks) |
| 1881 | verbose("R%d pointer arithmetic on PTR_TO_PACKET_END prohibited\n", |
| 1882 | dst); |
| 1883 | return -EACCES; |
| 1884 | } |
| 1885 | |
| 1886 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. |
| 1887 | * The id may be overwritten later if we create a new variable offset. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1888 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1889 | dst_reg->type = ptr_reg->type; |
| 1890 | dst_reg->id = ptr_reg->id; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1891 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1892 | switch (opcode) { |
| 1893 | case BPF_ADD: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1894 | /* We can take a fixed offset as long as it doesn't overflow |
| 1895 | * the s32 'off' field |
| 1896 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1897 | if (known && (ptr_reg->off + smin_val == |
| 1898 | (s64)(s32)(ptr_reg->off + smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1899 | /* pointer += K. Accumulate it into fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1900 | dst_reg->smin_value = smin_ptr; |
| 1901 | dst_reg->smax_value = smax_ptr; |
| 1902 | dst_reg->umin_value = umin_ptr; |
| 1903 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1904 | dst_reg->var_off = ptr_reg->var_off; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1905 | dst_reg->off = ptr_reg->off + smin_val; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1906 | dst_reg->range = ptr_reg->range; |
| 1907 | break; |
| 1908 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1909 | /* A new variable offset is created. Note that off_reg->off |
| 1910 | * == 0, since it's a scalar. |
| 1911 | * dst_reg gets the pointer type and since some positive |
| 1912 | * integer value was added to the pointer, give it a new 'id' |
| 1913 | * if it's a PTR_TO_PACKET. |
| 1914 | * this creates a new 'base' pointer, off_reg (variable) gets |
| 1915 | * added into the variable offset, and we copy the fixed offset |
| 1916 | * from ptr_reg. |
| 1917 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1918 | if (signed_add_overflows(smin_ptr, smin_val) || |
| 1919 | signed_add_overflows(smax_ptr, smax_val)) { |
| 1920 | dst_reg->smin_value = S64_MIN; |
| 1921 | dst_reg->smax_value = S64_MAX; |
| 1922 | } else { |
| 1923 | dst_reg->smin_value = smin_ptr + smin_val; |
| 1924 | dst_reg->smax_value = smax_ptr + smax_val; |
| 1925 | } |
| 1926 | if (umin_ptr + umin_val < umin_ptr || |
| 1927 | umax_ptr + umax_val < umax_ptr) { |
| 1928 | dst_reg->umin_value = 0; |
| 1929 | dst_reg->umax_value = U64_MAX; |
| 1930 | } else { |
| 1931 | dst_reg->umin_value = umin_ptr + umin_val; |
| 1932 | dst_reg->umax_value = umax_ptr + umax_val; |
| 1933 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1934 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
| 1935 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1936 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1937 | dst_reg->id = ++env->id_gen; |
| 1938 | /* something was added to pkt_ptr, set range to zero */ |
| 1939 | dst_reg->range = 0; |
| 1940 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1941 | break; |
| 1942 | case BPF_SUB: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1943 | if (dst_reg == off_reg) { |
| 1944 | /* scalar -= pointer. Creates an unknown scalar */ |
| 1945 | if (!env->allow_ptr_leaks) |
| 1946 | verbose("R%d tried to subtract pointer from scalar\n", |
| 1947 | dst); |
| 1948 | return -EACCES; |
| 1949 | } |
| 1950 | /* We don't allow subtraction from FP, because (according to |
| 1951 | * test_verifier.c test "invalid fp arithmetic", JITs might not |
| 1952 | * be able to deal with it. |
Edward Cree | 9305706 | 2017-07-21 14:37:34 +0100 | [diff] [blame] | 1953 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1954 | if (ptr_reg->type == PTR_TO_STACK) { |
| 1955 | if (!env->allow_ptr_leaks) |
| 1956 | verbose("R%d subtraction from stack pointer prohibited\n", |
| 1957 | dst); |
| 1958 | return -EACCES; |
| 1959 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1960 | if (known && (ptr_reg->off - smin_val == |
| 1961 | (s64)(s32)(ptr_reg->off - smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1962 | /* pointer -= K. Subtract it from fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1963 | dst_reg->smin_value = smin_ptr; |
| 1964 | dst_reg->smax_value = smax_ptr; |
| 1965 | dst_reg->umin_value = umin_ptr; |
| 1966 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1967 | dst_reg->var_off = ptr_reg->var_off; |
| 1968 | dst_reg->id = ptr_reg->id; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1969 | dst_reg->off = ptr_reg->off - smin_val; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1970 | dst_reg->range = ptr_reg->range; |
| 1971 | break; |
| 1972 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1973 | /* A new variable offset is created. If the subtrahend is known |
| 1974 | * nonnegative, then any reg->range we had before is still good. |
| 1975 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1976 | if (signed_sub_overflows(smin_ptr, smax_val) || |
| 1977 | signed_sub_overflows(smax_ptr, smin_val)) { |
| 1978 | /* Overflow possible, we know nothing */ |
| 1979 | dst_reg->smin_value = S64_MIN; |
| 1980 | dst_reg->smax_value = S64_MAX; |
| 1981 | } else { |
| 1982 | dst_reg->smin_value = smin_ptr - smax_val; |
| 1983 | dst_reg->smax_value = smax_ptr - smin_val; |
| 1984 | } |
| 1985 | if (umin_ptr < umax_val) { |
| 1986 | /* Overflow possible, we know nothing */ |
| 1987 | dst_reg->umin_value = 0; |
| 1988 | dst_reg->umax_value = U64_MAX; |
| 1989 | } else { |
| 1990 | /* Cannot overflow (as long as bounds are consistent) */ |
| 1991 | dst_reg->umin_value = umin_ptr - umax_val; |
| 1992 | dst_reg->umax_value = umax_ptr - umin_val; |
| 1993 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1994 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
| 1995 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1996 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1997 | dst_reg->id = ++env->id_gen; |
| 1998 | /* something was added to pkt_ptr, set range to zero */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1999 | if (smin_val < 0) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2000 | dst_reg->range = 0; |
| 2001 | } |
| 2002 | break; |
| 2003 | case BPF_AND: |
| 2004 | case BPF_OR: |
| 2005 | case BPF_XOR: |
| 2006 | /* bitwise ops on pointers are troublesome, prohibit for now. |
| 2007 | * (However, in principle we could allow some cases, e.g. |
| 2008 | * ptr &= ~3 which would reduce min_value by 3.) |
| 2009 | */ |
| 2010 | if (!env->allow_ptr_leaks) |
| 2011 | verbose("R%d bitwise operator %s on pointer prohibited\n", |
| 2012 | dst, bpf_alu_string[opcode >> 4]); |
| 2013 | return -EACCES; |
| 2014 | default: |
| 2015 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ |
| 2016 | if (!env->allow_ptr_leaks) |
| 2017 | verbose("R%d pointer arithmetic with %s operator prohibited\n", |
| 2018 | dst, bpf_alu_string[opcode >> 4]); |
| 2019 | return -EACCES; |
| 2020 | } |
| 2021 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2022 | __update_reg_bounds(dst_reg); |
| 2023 | __reg_deduce_bounds(dst_reg); |
| 2024 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2025 | return 0; |
| 2026 | } |
| 2027 | |
| 2028 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
| 2029 | struct bpf_insn *insn, |
| 2030 | struct bpf_reg_state *dst_reg, |
| 2031 | struct bpf_reg_state src_reg) |
| 2032 | { |
| 2033 | struct bpf_reg_state *regs = env->cur_state.regs; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2034 | u8 opcode = BPF_OP(insn->code); |
| 2035 | bool src_known, dst_known; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2036 | s64 smin_val, smax_val; |
| 2037 | u64 umin_val, umax_val; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2038 | |
| 2039 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 2040 | /* 32-bit ALU ops are (32,32)->64 */ |
| 2041 | coerce_reg_to_32(dst_reg); |
| 2042 | coerce_reg_to_32(&src_reg); |
| 2043 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2044 | smin_val = src_reg.smin_value; |
| 2045 | smax_val = src_reg.smax_value; |
| 2046 | umin_val = src_reg.umin_value; |
| 2047 | umax_val = src_reg.umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2048 | src_known = tnum_is_const(src_reg.var_off); |
| 2049 | dst_known = tnum_is_const(dst_reg->var_off); |
| 2050 | |
| 2051 | switch (opcode) { |
| 2052 | case BPF_ADD: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2053 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
| 2054 | signed_add_overflows(dst_reg->smax_value, smax_val)) { |
| 2055 | dst_reg->smin_value = S64_MIN; |
| 2056 | dst_reg->smax_value = S64_MAX; |
| 2057 | } else { |
| 2058 | dst_reg->smin_value += smin_val; |
| 2059 | dst_reg->smax_value += smax_val; |
| 2060 | } |
| 2061 | if (dst_reg->umin_value + umin_val < umin_val || |
| 2062 | dst_reg->umax_value + umax_val < umax_val) { |
| 2063 | dst_reg->umin_value = 0; |
| 2064 | dst_reg->umax_value = U64_MAX; |
| 2065 | } else { |
| 2066 | dst_reg->umin_value += umin_val; |
| 2067 | dst_reg->umax_value += umax_val; |
| 2068 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2069 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
| 2070 | break; |
| 2071 | case BPF_SUB: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2072 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
| 2073 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { |
| 2074 | /* Overflow possible, we know nothing */ |
| 2075 | dst_reg->smin_value = S64_MIN; |
| 2076 | dst_reg->smax_value = S64_MAX; |
| 2077 | } else { |
| 2078 | dst_reg->smin_value -= smax_val; |
| 2079 | dst_reg->smax_value -= smin_val; |
| 2080 | } |
| 2081 | if (dst_reg->umin_value < umax_val) { |
| 2082 | /* Overflow possible, we know nothing */ |
| 2083 | dst_reg->umin_value = 0; |
| 2084 | dst_reg->umax_value = U64_MAX; |
| 2085 | } else { |
| 2086 | /* Cannot overflow (as long as bounds are consistent) */ |
| 2087 | dst_reg->umin_value -= umax_val; |
| 2088 | dst_reg->umax_value -= umin_val; |
| 2089 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2090 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2091 | break; |
| 2092 | case BPF_MUL: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2093 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
| 2094 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2095 | /* Ain't nobody got time to multiply that sign */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2096 | __mark_reg_unbounded(dst_reg); |
| 2097 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2098 | break; |
| 2099 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2100 | /* Both values are positive, so we can work with unsigned and |
| 2101 | * copy the result to signed (unless it exceeds S64_MAX). |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2102 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2103 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
| 2104 | /* Potential overflow, we know nothing */ |
| 2105 | __mark_reg_unbounded(dst_reg); |
| 2106 | /* (except what we can learn from the var_off) */ |
| 2107 | __update_reg_bounds(dst_reg); |
| 2108 | break; |
| 2109 | } |
| 2110 | dst_reg->umin_value *= umin_val; |
| 2111 | dst_reg->umax_value *= umax_val; |
| 2112 | if (dst_reg->umax_value > S64_MAX) { |
| 2113 | /* Overflow possible, we know nothing */ |
| 2114 | dst_reg->smin_value = S64_MIN; |
| 2115 | dst_reg->smax_value = S64_MAX; |
| 2116 | } else { |
| 2117 | dst_reg->smin_value = dst_reg->umin_value; |
| 2118 | dst_reg->smax_value = dst_reg->umax_value; |
| 2119 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2120 | break; |
| 2121 | case BPF_AND: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2122 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2123 | __mark_reg_known(dst_reg, dst_reg->var_off.value & |
| 2124 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2125 | break; |
| 2126 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2127 | /* We get our minimum from the var_off, since that's inherently |
| 2128 | * bitwise. Our maximum is the minimum of the operands' maxima. |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 2129 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2130 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2131 | dst_reg->umin_value = dst_reg->var_off.value; |
| 2132 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); |
| 2133 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 2134 | /* Lose signed bounds when ANDing negative numbers, |
| 2135 | * ain't nobody got time for that. |
| 2136 | */ |
| 2137 | dst_reg->smin_value = S64_MIN; |
| 2138 | dst_reg->smax_value = S64_MAX; |
| 2139 | } else { |
| 2140 | /* ANDing two positives gives a positive, so safe to |
| 2141 | * cast result into s64. |
| 2142 | */ |
| 2143 | dst_reg->smin_value = dst_reg->umin_value; |
| 2144 | dst_reg->smax_value = dst_reg->umax_value; |
| 2145 | } |
| 2146 | /* We may learn something more from the var_off */ |
| 2147 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2148 | break; |
| 2149 | case BPF_OR: |
| 2150 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2151 | __mark_reg_known(dst_reg, dst_reg->var_off.value | |
| 2152 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2153 | break; |
| 2154 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2155 | /* We get our maximum from the var_off, and our minimum is the |
| 2156 | * maximum of the operands' minima |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2157 | */ |
| 2158 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2159 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
| 2160 | dst_reg->umax_value = dst_reg->var_off.value | |
| 2161 | dst_reg->var_off.mask; |
| 2162 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 2163 | /* Lose signed bounds when ORing negative numbers, |
| 2164 | * ain't nobody got time for that. |
| 2165 | */ |
| 2166 | dst_reg->smin_value = S64_MIN; |
| 2167 | dst_reg->smax_value = S64_MAX; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2168 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2169 | /* ORing two positives gives a positive, so safe to |
| 2170 | * cast result into s64. |
| 2171 | */ |
| 2172 | dst_reg->smin_value = dst_reg->umin_value; |
| 2173 | dst_reg->smax_value = dst_reg->umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2174 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2175 | /* We may learn something more from the var_off */ |
| 2176 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2177 | break; |
| 2178 | case BPF_LSH: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2179 | if (umax_val > 63) { |
| 2180 | /* Shifts greater than 63 are undefined. This includes |
| 2181 | * shifts by a negative number. |
| 2182 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2183 | mark_reg_unknown(regs, insn->dst_reg); |
| 2184 | break; |
| 2185 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2186 | /* We lose all sign bit information (except what we can pick |
| 2187 | * up from var_off) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2188 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2189 | dst_reg->smin_value = S64_MIN; |
| 2190 | dst_reg->smax_value = S64_MAX; |
| 2191 | /* If we might shift our top bit out, then we know nothing */ |
| 2192 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { |
| 2193 | dst_reg->umin_value = 0; |
| 2194 | dst_reg->umax_value = U64_MAX; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2195 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2196 | dst_reg->umin_value <<= umin_val; |
| 2197 | dst_reg->umax_value <<= umax_val; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2198 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2199 | if (src_known) |
| 2200 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
| 2201 | else |
| 2202 | dst_reg->var_off = tnum_lshift(tnum_unknown, umin_val); |
| 2203 | /* We may learn something more from the var_off */ |
| 2204 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2205 | break; |
| 2206 | case BPF_RSH: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2207 | if (umax_val > 63) { |
| 2208 | /* Shifts greater than 63 are undefined. This includes |
| 2209 | * shifts by a negative number. |
| 2210 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2211 | mark_reg_unknown(regs, insn->dst_reg); |
| 2212 | break; |
| 2213 | } |
| 2214 | /* BPF_RSH is an unsigned shift, so make the appropriate casts */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2215 | if (dst_reg->smin_value < 0) { |
| 2216 | if (umin_val) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2217 | /* Sign bit will be cleared */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2218 | dst_reg->smin_value = 0; |
| 2219 | } else { |
| 2220 | /* Lost sign bit information */ |
| 2221 | dst_reg->smin_value = S64_MIN; |
| 2222 | dst_reg->smax_value = S64_MAX; |
| 2223 | } |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2224 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2225 | dst_reg->smin_value = |
| 2226 | (u64)(dst_reg->smin_value) >> umax_val; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2227 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2228 | if (src_known) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2229 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, |
| 2230 | umin_val); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2231 | else |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2232 | dst_reg->var_off = tnum_rshift(tnum_unknown, umin_val); |
| 2233 | dst_reg->umin_value >>= umax_val; |
| 2234 | dst_reg->umax_value >>= umin_val; |
| 2235 | /* We may learn something more from the var_off */ |
| 2236 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2237 | break; |
| 2238 | default: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2239 | mark_reg_unknown(regs, insn->dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2240 | break; |
| 2241 | } |
| 2242 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2243 | __reg_deduce_bounds(dst_reg); |
| 2244 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2245 | return 0; |
| 2246 | } |
| 2247 | |
| 2248 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max |
| 2249 | * and var_off. |
| 2250 | */ |
| 2251 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, |
| 2252 | struct bpf_insn *insn) |
| 2253 | { |
| 2254 | struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg, *src_reg; |
| 2255 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
| 2256 | u8 opcode = BPF_OP(insn->code); |
| 2257 | int rc; |
| 2258 | |
| 2259 | dst_reg = ®s[insn->dst_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2260 | src_reg = NULL; |
| 2261 | if (dst_reg->type != SCALAR_VALUE) |
| 2262 | ptr_reg = dst_reg; |
| 2263 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2264 | src_reg = ®s[insn->src_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2265 | if (src_reg->type != SCALAR_VALUE) { |
| 2266 | if (dst_reg->type != SCALAR_VALUE) { |
| 2267 | /* Combining two pointers by any ALU op yields |
| 2268 | * an arbitrary scalar. |
| 2269 | */ |
| 2270 | if (!env->allow_ptr_leaks) { |
| 2271 | verbose("R%d pointer %s pointer prohibited\n", |
| 2272 | insn->dst_reg, |
| 2273 | bpf_alu_string[opcode >> 4]); |
| 2274 | return -EACCES; |
| 2275 | } |
| 2276 | mark_reg_unknown(regs, insn->dst_reg); |
| 2277 | return 0; |
| 2278 | } else { |
| 2279 | /* scalar += pointer |
| 2280 | * This is legal, but we have to reverse our |
| 2281 | * src/dest handling in computing the range |
| 2282 | */ |
| 2283 | rc = adjust_ptr_min_max_vals(env, insn, |
| 2284 | src_reg, dst_reg); |
| 2285 | if (rc == -EACCES && env->allow_ptr_leaks) { |
| 2286 | /* scalar += unknown scalar */ |
| 2287 | __mark_reg_unknown(&off_reg); |
| 2288 | return adjust_scalar_min_max_vals( |
| 2289 | env, insn, |
| 2290 | dst_reg, off_reg); |
| 2291 | } |
| 2292 | return rc; |
| 2293 | } |
| 2294 | } else if (ptr_reg) { |
| 2295 | /* pointer += scalar */ |
| 2296 | rc = adjust_ptr_min_max_vals(env, insn, |
| 2297 | dst_reg, src_reg); |
| 2298 | if (rc == -EACCES && env->allow_ptr_leaks) { |
| 2299 | /* unknown scalar += scalar */ |
| 2300 | __mark_reg_unknown(dst_reg); |
| 2301 | return adjust_scalar_min_max_vals( |
| 2302 | env, insn, dst_reg, *src_reg); |
| 2303 | } |
| 2304 | return rc; |
| 2305 | } |
| 2306 | } else { |
| 2307 | /* Pretend the src is a reg with a known value, since we only |
| 2308 | * need to be able to read from this state. |
| 2309 | */ |
| 2310 | off_reg.type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2311 | __mark_reg_known(&off_reg, insn->imm); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2312 | src_reg = &off_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2313 | if (ptr_reg) { /* pointer += K */ |
| 2314 | rc = adjust_ptr_min_max_vals(env, insn, |
| 2315 | ptr_reg, src_reg); |
| 2316 | if (rc == -EACCES && env->allow_ptr_leaks) { |
| 2317 | /* unknown scalar += K */ |
| 2318 | __mark_reg_unknown(dst_reg); |
| 2319 | return adjust_scalar_min_max_vals( |
| 2320 | env, insn, dst_reg, off_reg); |
| 2321 | } |
| 2322 | return rc; |
| 2323 | } |
| 2324 | } |
| 2325 | |
| 2326 | /* Got here implies adding two SCALAR_VALUEs */ |
| 2327 | if (WARN_ON_ONCE(ptr_reg)) { |
| 2328 | print_verifier_state(&env->cur_state); |
| 2329 | verbose("verifier internal error: unexpected ptr_reg\n"); |
| 2330 | return -EINVAL; |
| 2331 | } |
| 2332 | if (WARN_ON(!src_reg)) { |
| 2333 | print_verifier_state(&env->cur_state); |
| 2334 | verbose("verifier internal error: no src_reg\n"); |
| 2335 | return -EINVAL; |
| 2336 | } |
| 2337 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2338 | } |
| 2339 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2340 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2341 | 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] | 2342 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2343 | struct bpf_reg_state *regs = env->cur_state.regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2344 | u8 opcode = BPF_OP(insn->code); |
| 2345 | int err; |
| 2346 | |
| 2347 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 2348 | if (opcode == BPF_NEG) { |
| 2349 | if (BPF_SRC(insn->code) != 0 || |
| 2350 | insn->src_reg != BPF_REG_0 || |
| 2351 | insn->off != 0 || insn->imm != 0) { |
| 2352 | verbose("BPF_NEG uses reserved fields\n"); |
| 2353 | return -EINVAL; |
| 2354 | } |
| 2355 | } else { |
| 2356 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
Edward Cree | e67b8a6 | 2017-09-15 14:37:38 +0100 | [diff] [blame] | 2357 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
| 2358 | BPF_CLASS(insn->code) == BPF_ALU64) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2359 | verbose("BPF_END uses reserved fields\n"); |
| 2360 | return -EINVAL; |
| 2361 | } |
| 2362 | } |
| 2363 | |
| 2364 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2365 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2366 | if (err) |
| 2367 | return err; |
| 2368 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2369 | if (is_pointer_value(env, insn->dst_reg)) { |
| 2370 | verbose("R%d pointer arithmetic prohibited\n", |
| 2371 | insn->dst_reg); |
| 2372 | return -EACCES; |
| 2373 | } |
| 2374 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2375 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2376 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2377 | if (err) |
| 2378 | return err; |
| 2379 | |
| 2380 | } else if (opcode == BPF_MOV) { |
| 2381 | |
| 2382 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2383 | if (insn->imm != 0 || insn->off != 0) { |
| 2384 | verbose("BPF_MOV uses reserved fields\n"); |
| 2385 | return -EINVAL; |
| 2386 | } |
| 2387 | |
| 2388 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2389 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2390 | if (err) |
| 2391 | return err; |
| 2392 | } else { |
| 2393 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
| 2394 | verbose("BPF_MOV uses reserved fields\n"); |
| 2395 | return -EINVAL; |
| 2396 | } |
| 2397 | } |
| 2398 | |
| 2399 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2400 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2401 | if (err) |
| 2402 | return err; |
| 2403 | |
| 2404 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2405 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 2406 | /* case: R1 = R2 |
| 2407 | * copy register state to dest reg |
| 2408 | */ |
| 2409 | regs[insn->dst_reg] = regs[insn->src_reg]; |
Alexei Starovoitov | 8fe2d6c | 2017-10-05 16:20:56 -0700 | [diff] [blame] | 2410 | regs[insn->dst_reg].live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2411 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2412 | /* R1 = (u32) R2 */ |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2413 | if (is_pointer_value(env, insn->src_reg)) { |
| 2414 | verbose("R%d partial copy of pointer\n", |
| 2415 | insn->src_reg); |
| 2416 | return -EACCES; |
| 2417 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2418 | mark_reg_unknown(regs, insn->dst_reg); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2419 | /* high 32 bits are known zero. */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2420 | regs[insn->dst_reg].var_off = tnum_cast( |
| 2421 | regs[insn->dst_reg].var_off, 4); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2422 | __update_reg_bounds(®s[insn->dst_reg]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2423 | } |
| 2424 | } else { |
| 2425 | /* case: R = imm |
| 2426 | * remember the value we stored into this reg |
| 2427 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2428 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2429 | __mark_reg_known(regs + insn->dst_reg, insn->imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2430 | } |
| 2431 | |
| 2432 | } else if (opcode > BPF_END) { |
| 2433 | verbose("invalid BPF_ALU opcode %x\n", opcode); |
| 2434 | return -EINVAL; |
| 2435 | |
| 2436 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 2437 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2438 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2439 | if (insn->imm != 0 || insn->off != 0) { |
| 2440 | verbose("BPF_ALU uses reserved fields\n"); |
| 2441 | return -EINVAL; |
| 2442 | } |
| 2443 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2444 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2445 | if (err) |
| 2446 | return err; |
| 2447 | } else { |
| 2448 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
| 2449 | verbose("BPF_ALU uses reserved fields\n"); |
| 2450 | return -EINVAL; |
| 2451 | } |
| 2452 | } |
| 2453 | |
| 2454 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2455 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2456 | if (err) |
| 2457 | return err; |
| 2458 | |
| 2459 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 2460 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
| 2461 | verbose("div by zero\n"); |
| 2462 | return -EINVAL; |
| 2463 | } |
| 2464 | |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 2465 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
| 2466 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { |
| 2467 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; |
| 2468 | |
| 2469 | if (insn->imm < 0 || insn->imm >= size) { |
| 2470 | verbose("invalid shift %d\n", insn->imm); |
| 2471 | return -EINVAL; |
| 2472 | } |
| 2473 | } |
| 2474 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2475 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2476 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2477 | if (err) |
| 2478 | return err; |
| 2479 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2480 | return adjust_reg_min_max_vals(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2481 | } |
| 2482 | |
| 2483 | return 0; |
| 2484 | } |
| 2485 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2486 | static void find_good_pkt_pointers(struct bpf_verifier_state *state, |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2487 | struct bpf_reg_state *dst_reg, |
| 2488 | enum bpf_reg_type type) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2489 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2490 | struct bpf_reg_state *regs = state->regs, *reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2491 | int i; |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 2492 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2493 | if (dst_reg->off < 0) |
| 2494 | /* This doesn't give us any range */ |
| 2495 | return; |
| 2496 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2497 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
| 2498 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2499 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
| 2500 | * than pkt_end, but that's because it's also less than pkt. |
| 2501 | */ |
| 2502 | return; |
| 2503 | |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 2504 | /* LLVM can generate four kind of checks: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 2505 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 2506 | * Type 1/2: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 2507 | * |
| 2508 | * r2 = r3; |
| 2509 | * r2 += 8; |
| 2510 | * if (r2 > pkt_end) goto <handle exception> |
| 2511 | * <access okay> |
| 2512 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 2513 | * r2 = r3; |
| 2514 | * r2 += 8; |
| 2515 | * if (r2 < pkt_end) goto <access okay> |
| 2516 | * <handle exception> |
| 2517 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 2518 | * Where: |
| 2519 | * r2 == dst_reg, pkt_end == src_reg |
| 2520 | * r2=pkt(id=n,off=8,r=0) |
| 2521 | * r3=pkt(id=n,off=0,r=0) |
| 2522 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 2523 | * Type 3/4: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 2524 | * |
| 2525 | * r2 = r3; |
| 2526 | * r2 += 8; |
| 2527 | * if (pkt_end >= r2) goto <access okay> |
| 2528 | * <handle exception> |
| 2529 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 2530 | * r2 = r3; |
| 2531 | * r2 += 8; |
| 2532 | * if (pkt_end <= r2) goto <handle exception> |
| 2533 | * <access okay> |
| 2534 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 2535 | * Where: |
| 2536 | * pkt_end == dst_reg, r2 == src_reg |
| 2537 | * r2=pkt(id=n,off=8,r=0) |
| 2538 | * r3=pkt(id=n,off=0,r=0) |
| 2539 | * |
| 2540 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) |
| 2541 | * so that range of bytes [r3, r3 + 8) is safe to access. |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2542 | */ |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 2543 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2544 | /* If our ids match, then we must have the same max_value. And we |
| 2545 | * don't care about the other reg's fixed offset, since if it's too big |
| 2546 | * the range won't allow anything. |
| 2547 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. |
| 2548 | */ |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2549 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2550 | if (regs[i].type == type && regs[i].id == dst_reg->id) |
Alexei Starovoitov | b197768 | 2017-03-24 15:57:33 -0700 | [diff] [blame] | 2551 | /* keep the maximum range already checked */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2552 | regs[i].range = max_t(u16, regs[i].range, dst_reg->off); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2553 | |
| 2554 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { |
| 2555 | if (state->stack_slot_type[i] != STACK_SPILL) |
| 2556 | continue; |
| 2557 | reg = &state->spilled_regs[i / BPF_REG_SIZE]; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2558 | if (reg->type == type && reg->id == dst_reg->id) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2559 | reg->range = max_t(u16, reg->range, dst_reg->off); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2560 | } |
| 2561 | } |
| 2562 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2563 | /* Adjusts the register min/max values in the case that the dst_reg is the |
| 2564 | * variable register that we are working on, and src_reg is a constant or we're |
| 2565 | * simply doing a BPF_K check. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2566 | * In JEQ/JNE cases we also adjust the var_off values. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2567 | */ |
| 2568 | static void reg_set_min_max(struct bpf_reg_state *true_reg, |
| 2569 | struct bpf_reg_state *false_reg, u64 val, |
| 2570 | u8 opcode) |
| 2571 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2572 | /* If the dst_reg is a pointer, we can't learn anything about its |
| 2573 | * variable offset from the compare (unless src_reg were a pointer into |
| 2574 | * the same object, but we don't bother with that. |
| 2575 | * Since false_reg and true_reg have the same type by construction, we |
| 2576 | * only need to check one of them for pointerness. |
| 2577 | */ |
| 2578 | if (__is_pointer_value(false, false_reg)) |
| 2579 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2580 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2581 | switch (opcode) { |
| 2582 | case BPF_JEQ: |
| 2583 | /* If this is false then we know nothing Jon Snow, but if it is |
| 2584 | * true then we know for sure. |
| 2585 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2586 | __mark_reg_known(true_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2587 | break; |
| 2588 | case BPF_JNE: |
| 2589 | /* If this is true we know nothing Jon Snow, but if it is false |
| 2590 | * we know the value for sure; |
| 2591 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2592 | __mark_reg_known(false_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2593 | break; |
| 2594 | case BPF_JGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2595 | false_reg->umax_value = min(false_reg->umax_value, val); |
| 2596 | true_reg->umin_value = max(true_reg->umin_value, val + 1); |
| 2597 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2598 | case BPF_JSGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2599 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); |
| 2600 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2601 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 2602 | case BPF_JLT: |
| 2603 | false_reg->umin_value = max(false_reg->umin_value, val); |
| 2604 | true_reg->umax_value = min(true_reg->umax_value, val - 1); |
| 2605 | break; |
| 2606 | case BPF_JSLT: |
| 2607 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); |
| 2608 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); |
| 2609 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2610 | case BPF_JGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2611 | false_reg->umax_value = min(false_reg->umax_value, val - 1); |
| 2612 | true_reg->umin_value = max(true_reg->umin_value, val); |
| 2613 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2614 | case BPF_JSGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2615 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); |
| 2616 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2617 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 2618 | case BPF_JLE: |
| 2619 | false_reg->umin_value = max(false_reg->umin_value, val + 1); |
| 2620 | true_reg->umax_value = min(true_reg->umax_value, val); |
| 2621 | break; |
| 2622 | case BPF_JSLE: |
| 2623 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1); |
| 2624 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); |
| 2625 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2626 | default: |
| 2627 | break; |
| 2628 | } |
| 2629 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2630 | __reg_deduce_bounds(false_reg); |
| 2631 | __reg_deduce_bounds(true_reg); |
| 2632 | /* We might have learned some bits from the bounds. */ |
| 2633 | __reg_bound_offset(false_reg); |
| 2634 | __reg_bound_offset(true_reg); |
| 2635 | /* Intersecting with the old var_off might have improved our bounds |
| 2636 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 2637 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 2638 | */ |
| 2639 | __update_reg_bounds(false_reg); |
| 2640 | __update_reg_bounds(true_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2641 | } |
| 2642 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2643 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
| 2644 | * the variable reg. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2645 | */ |
| 2646 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, |
| 2647 | struct bpf_reg_state *false_reg, u64 val, |
| 2648 | u8 opcode) |
| 2649 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2650 | if (__is_pointer_value(false, false_reg)) |
| 2651 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2652 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2653 | switch (opcode) { |
| 2654 | case BPF_JEQ: |
| 2655 | /* If this is false then we know nothing Jon Snow, but if it is |
| 2656 | * true then we know for sure. |
| 2657 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2658 | __mark_reg_known(true_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2659 | break; |
| 2660 | case BPF_JNE: |
| 2661 | /* If this is true we know nothing Jon Snow, but if it is false |
| 2662 | * we know the value for sure; |
| 2663 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2664 | __mark_reg_known(false_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2665 | break; |
| 2666 | case BPF_JGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2667 | true_reg->umax_value = min(true_reg->umax_value, val - 1); |
| 2668 | false_reg->umin_value = max(false_reg->umin_value, val); |
| 2669 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2670 | case BPF_JSGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2671 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); |
| 2672 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2673 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 2674 | case BPF_JLT: |
| 2675 | true_reg->umin_value = max(true_reg->umin_value, val + 1); |
| 2676 | false_reg->umax_value = min(false_reg->umax_value, val); |
| 2677 | break; |
| 2678 | case BPF_JSLT: |
| 2679 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1); |
| 2680 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); |
| 2681 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2682 | case BPF_JGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2683 | true_reg->umax_value = min(true_reg->umax_value, val); |
| 2684 | false_reg->umin_value = max(false_reg->umin_value, val + 1); |
| 2685 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2686 | case BPF_JSGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2687 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); |
| 2688 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2689 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 2690 | case BPF_JLE: |
| 2691 | true_reg->umin_value = max(true_reg->umin_value, val); |
| 2692 | false_reg->umax_value = min(false_reg->umax_value, val - 1); |
| 2693 | break; |
| 2694 | case BPF_JSLE: |
| 2695 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); |
| 2696 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); |
| 2697 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2698 | default: |
| 2699 | break; |
| 2700 | } |
| 2701 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2702 | __reg_deduce_bounds(false_reg); |
| 2703 | __reg_deduce_bounds(true_reg); |
| 2704 | /* We might have learned some bits from the bounds. */ |
| 2705 | __reg_bound_offset(false_reg); |
| 2706 | __reg_bound_offset(true_reg); |
| 2707 | /* Intersecting with the old var_off might have improved our bounds |
| 2708 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 2709 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 2710 | */ |
| 2711 | __update_reg_bounds(false_reg); |
| 2712 | __update_reg_bounds(true_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2713 | } |
| 2714 | |
| 2715 | /* Regs are known to be equal, so intersect their min/max/var_off */ |
| 2716 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, |
| 2717 | struct bpf_reg_state *dst_reg) |
| 2718 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2719 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
| 2720 | dst_reg->umin_value); |
| 2721 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, |
| 2722 | dst_reg->umax_value); |
| 2723 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, |
| 2724 | dst_reg->smin_value); |
| 2725 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, |
| 2726 | dst_reg->smax_value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2727 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
| 2728 | dst_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2729 | /* We might have learned new bounds from the var_off. */ |
| 2730 | __update_reg_bounds(src_reg); |
| 2731 | __update_reg_bounds(dst_reg); |
| 2732 | /* We might have learned something about the sign bit. */ |
| 2733 | __reg_deduce_bounds(src_reg); |
| 2734 | __reg_deduce_bounds(dst_reg); |
| 2735 | /* We might have learned some bits from the bounds. */ |
| 2736 | __reg_bound_offset(src_reg); |
| 2737 | __reg_bound_offset(dst_reg); |
| 2738 | /* Intersecting with the old var_off might have improved our bounds |
| 2739 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 2740 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 2741 | */ |
| 2742 | __update_reg_bounds(src_reg); |
| 2743 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2744 | } |
| 2745 | |
| 2746 | static void reg_combine_min_max(struct bpf_reg_state *true_src, |
| 2747 | struct bpf_reg_state *true_dst, |
| 2748 | struct bpf_reg_state *false_src, |
| 2749 | struct bpf_reg_state *false_dst, |
| 2750 | u8 opcode) |
| 2751 | { |
| 2752 | switch (opcode) { |
| 2753 | case BPF_JEQ: |
| 2754 | __reg_combine_min_max(true_src, true_dst); |
| 2755 | break; |
| 2756 | case BPF_JNE: |
| 2757 | __reg_combine_min_max(false_src, false_dst); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2758 | break; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2759 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2760 | } |
| 2761 | |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2762 | static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2763 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2764 | { |
| 2765 | struct bpf_reg_state *reg = ®s[regno]; |
| 2766 | |
| 2767 | if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2768 | /* Old offset (both fixed and variable parts) should |
| 2769 | * have been known-zero, because we don't allow pointer |
| 2770 | * arithmetic on pointers that might be NULL. |
| 2771 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2772 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
| 2773 | !tnum_equals_const(reg->var_off, 0) || |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2774 | reg->off)) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2775 | __mark_reg_known_zero(reg); |
| 2776 | reg->off = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2777 | } |
| 2778 | if (is_null) { |
| 2779 | reg->type = SCALAR_VALUE; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2780 | } else if (reg->map_ptr->inner_map_meta) { |
| 2781 | reg->type = CONST_PTR_TO_MAP; |
| 2782 | reg->map_ptr = reg->map_ptr->inner_map_meta; |
| 2783 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2784 | reg->type = PTR_TO_MAP_VALUE; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2785 | } |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 2786 | /* We don't need id from this point onwards anymore, thus we |
| 2787 | * should better reset it, so that state pruning has chances |
| 2788 | * to take effect. |
| 2789 | */ |
| 2790 | reg->id = 0; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2791 | } |
| 2792 | } |
| 2793 | |
| 2794 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
| 2795 | * be folded together at some point. |
| 2796 | */ |
| 2797 | static void mark_map_regs(struct bpf_verifier_state *state, u32 regno, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2798 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2799 | { |
| 2800 | struct bpf_reg_state *regs = state->regs; |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 2801 | u32 id = regs[regno].id; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2802 | int i; |
| 2803 | |
| 2804 | for (i = 0; i < MAX_BPF_REG; i++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2805 | mark_map_reg(regs, i, id, is_null); |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2806 | |
| 2807 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { |
| 2808 | if (state->stack_slot_type[i] != STACK_SPILL) |
| 2809 | continue; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2810 | mark_map_reg(state->spilled_regs, i / BPF_REG_SIZE, id, is_null); |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2811 | } |
| 2812 | } |
| 2813 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2814 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2815 | struct bpf_insn *insn, int *insn_idx) |
| 2816 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2817 | struct bpf_verifier_state *other_branch, *this_branch = &env->cur_state; |
| 2818 | struct bpf_reg_state *regs = this_branch->regs, *dst_reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2819 | u8 opcode = BPF_OP(insn->code); |
| 2820 | int err; |
| 2821 | |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 2822 | if (opcode > BPF_JSLE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2823 | verbose("invalid BPF_JMP opcode %x\n", opcode); |
| 2824 | return -EINVAL; |
| 2825 | } |
| 2826 | |
| 2827 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2828 | if (insn->imm != 0) { |
| 2829 | verbose("BPF_JMP uses reserved fields\n"); |
| 2830 | return -EINVAL; |
| 2831 | } |
| 2832 | |
| 2833 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2834 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2835 | if (err) |
| 2836 | return err; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2837 | |
| 2838 | if (is_pointer_value(env, insn->src_reg)) { |
| 2839 | verbose("R%d pointer comparison prohibited\n", |
| 2840 | insn->src_reg); |
| 2841 | return -EACCES; |
| 2842 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2843 | } else { |
| 2844 | if (insn->src_reg != BPF_REG_0) { |
| 2845 | verbose("BPF_JMP uses reserved fields\n"); |
| 2846 | return -EINVAL; |
| 2847 | } |
| 2848 | } |
| 2849 | |
| 2850 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2851 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2852 | if (err) |
| 2853 | return err; |
| 2854 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2855 | dst_reg = ®s[insn->dst_reg]; |
| 2856 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2857 | /* detect if R == 0 where R was initialized to zero earlier */ |
| 2858 | if (BPF_SRC(insn->code) == BPF_K && |
| 2859 | (opcode == BPF_JEQ || opcode == BPF_JNE) && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2860 | dst_reg->type == SCALAR_VALUE && |
| 2861 | tnum_equals_const(dst_reg->var_off, insn->imm)) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2862 | if (opcode == BPF_JEQ) { |
| 2863 | /* if (imm == imm) goto pc+off; |
| 2864 | * only follow the goto, ignore fall-through |
| 2865 | */ |
| 2866 | *insn_idx += insn->off; |
| 2867 | return 0; |
| 2868 | } else { |
| 2869 | /* if (imm != imm) goto pc+off; |
| 2870 | * only follow fall-through branch, since |
| 2871 | * that's where the program will go |
| 2872 | */ |
| 2873 | return 0; |
| 2874 | } |
| 2875 | } |
| 2876 | |
| 2877 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx); |
| 2878 | if (!other_branch) |
| 2879 | return -EFAULT; |
| 2880 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2881 | /* detect if we are comparing against a constant value so we can adjust |
| 2882 | * our min/max values for our dst register. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2883 | * this is only legit if both are scalars (or pointers to the same |
| 2884 | * object, I suppose, but we don't support that right now), because |
| 2885 | * otherwise the different base pointers mean the offsets aren't |
| 2886 | * comparable. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2887 | */ |
| 2888 | if (BPF_SRC(insn->code) == BPF_X) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2889 | if (dst_reg->type == SCALAR_VALUE && |
| 2890 | regs[insn->src_reg].type == SCALAR_VALUE) { |
| 2891 | if (tnum_is_const(regs[insn->src_reg].var_off)) |
| 2892 | reg_set_min_max(&other_branch->regs[insn->dst_reg], |
| 2893 | dst_reg, regs[insn->src_reg].var_off.value, |
| 2894 | opcode); |
| 2895 | else if (tnum_is_const(dst_reg->var_off)) |
| 2896 | reg_set_min_max_inv(&other_branch->regs[insn->src_reg], |
| 2897 | ®s[insn->src_reg], |
| 2898 | dst_reg->var_off.value, opcode); |
| 2899 | else if (opcode == BPF_JEQ || opcode == BPF_JNE) |
| 2900 | /* Comparing for equality, we can combine knowledge */ |
| 2901 | reg_combine_min_max(&other_branch->regs[insn->src_reg], |
| 2902 | &other_branch->regs[insn->dst_reg], |
| 2903 | ®s[insn->src_reg], |
| 2904 | ®s[insn->dst_reg], opcode); |
| 2905 | } |
| 2906 | } else if (dst_reg->type == SCALAR_VALUE) { |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2907 | reg_set_min_max(&other_branch->regs[insn->dst_reg], |
| 2908 | dst_reg, insn->imm, opcode); |
| 2909 | } |
| 2910 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2911 | /* 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] | 2912 | if (BPF_SRC(insn->code) == BPF_K && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2913 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
| 2914 | dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2915 | /* Mark all identical map registers in each branch as either |
| 2916 | * safe or unknown depending R == 0 or R != 0 conditional. |
| 2917 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2918 | mark_map_regs(this_branch, insn->dst_reg, opcode == BPF_JNE); |
| 2919 | mark_map_regs(other_branch, insn->dst_reg, opcode == BPF_JEQ); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2920 | } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT && |
| 2921 | dst_reg->type == PTR_TO_PACKET && |
| 2922 | regs[insn->src_reg].type == PTR_TO_PACKET_END) { |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2923 | find_good_pkt_pointers(this_branch, dst_reg, PTR_TO_PACKET); |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 2924 | } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JLT && |
| 2925 | dst_reg->type == PTR_TO_PACKET && |
| 2926 | regs[insn->src_reg].type == PTR_TO_PACKET_END) { |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2927 | find_good_pkt_pointers(other_branch, dst_reg, PTR_TO_PACKET); |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 2928 | } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGE && |
| 2929 | dst_reg->type == PTR_TO_PACKET_END && |
| 2930 | regs[insn->src_reg].type == PTR_TO_PACKET) { |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2931 | find_good_pkt_pointers(other_branch, ®s[insn->src_reg], |
| 2932 | PTR_TO_PACKET); |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 2933 | } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JLE && |
| 2934 | dst_reg->type == PTR_TO_PACKET_END && |
| 2935 | regs[insn->src_reg].type == PTR_TO_PACKET) { |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2936 | find_good_pkt_pointers(this_branch, ®s[insn->src_reg], |
| 2937 | PTR_TO_PACKET); |
| 2938 | } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT && |
| 2939 | dst_reg->type == PTR_TO_PACKET_META && |
| 2940 | reg_is_init_pkt_pointer(®s[insn->src_reg], PTR_TO_PACKET)) { |
| 2941 | find_good_pkt_pointers(this_branch, dst_reg, PTR_TO_PACKET_META); |
| 2942 | } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JLT && |
| 2943 | dst_reg->type == PTR_TO_PACKET_META && |
| 2944 | reg_is_init_pkt_pointer(®s[insn->src_reg], PTR_TO_PACKET)) { |
| 2945 | find_good_pkt_pointers(other_branch, dst_reg, PTR_TO_PACKET_META); |
| 2946 | } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGE && |
| 2947 | reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 2948 | regs[insn->src_reg].type == PTR_TO_PACKET_META) { |
| 2949 | find_good_pkt_pointers(other_branch, ®s[insn->src_reg], |
| 2950 | PTR_TO_PACKET_META); |
| 2951 | } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JLE && |
| 2952 | reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 2953 | regs[insn->src_reg].type == PTR_TO_PACKET_META) { |
| 2954 | find_good_pkt_pointers(this_branch, ®s[insn->src_reg], |
| 2955 | PTR_TO_PACKET_META); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2956 | } else if (is_pointer_value(env, insn->dst_reg)) { |
| 2957 | verbose("R%d pointer comparison prohibited\n", insn->dst_reg); |
| 2958 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2959 | } |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame^] | 2960 | if (verifier_log.level) |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 2961 | print_verifier_state(this_branch); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2962 | return 0; |
| 2963 | } |
| 2964 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2965 | /* return the map pointer stored inside BPF_LD_IMM64 instruction */ |
| 2966 | static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn) |
| 2967 | { |
| 2968 | u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32; |
| 2969 | |
| 2970 | return (struct bpf_map *) (unsigned long) imm64; |
| 2971 | } |
| 2972 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2973 | /* verify BPF_LD_IMM64 instruction */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2974 | 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] | 2975 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2976 | struct bpf_reg_state *regs = env->cur_state.regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2977 | int err; |
| 2978 | |
| 2979 | if (BPF_SIZE(insn->code) != BPF_DW) { |
| 2980 | verbose("invalid BPF_LD_IMM insn\n"); |
| 2981 | return -EINVAL; |
| 2982 | } |
| 2983 | if (insn->off != 0) { |
| 2984 | verbose("BPF_LD_IMM64 uses reserved fields\n"); |
| 2985 | return -EINVAL; |
| 2986 | } |
| 2987 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2988 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2989 | if (err) |
| 2990 | return err; |
| 2991 | |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 2992 | if (insn->src_reg == 0) { |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 2993 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 2994 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2995 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2996 | __mark_reg_known(®s[insn->dst_reg], imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2997 | return 0; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 2998 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2999 | |
| 3000 | /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */ |
| 3001 | BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD); |
| 3002 | |
| 3003 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; |
| 3004 | regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn); |
| 3005 | return 0; |
| 3006 | } |
| 3007 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 3008 | static bool may_access_skb(enum bpf_prog_type type) |
| 3009 | { |
| 3010 | switch (type) { |
| 3011 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 3012 | case BPF_PROG_TYPE_SCHED_CLS: |
Daniel Borkmann | 94caee8c | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 3013 | case BPF_PROG_TYPE_SCHED_ACT: |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 3014 | return true; |
| 3015 | default: |
| 3016 | return false; |
| 3017 | } |
| 3018 | } |
| 3019 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3020 | /* verify safety of LD_ABS|LD_IND instructions: |
| 3021 | * - they can only appear in the programs where ctx == skb |
| 3022 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 3023 | * preserve R6-R9, and store return value into R0 |
| 3024 | * |
| 3025 | * Implicit input: |
| 3026 | * ctx == skb == R6 == CTX |
| 3027 | * |
| 3028 | * Explicit input: |
| 3029 | * SRC == any register |
| 3030 | * IMM == 32-bit immediate |
| 3031 | * |
| 3032 | * Output: |
| 3033 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 3034 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3035 | 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] | 3036 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3037 | struct bpf_reg_state *regs = env->cur_state.regs; |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3038 | u8 mode = BPF_MODE(insn->code); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3039 | int i, err; |
| 3040 | |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 3041 | if (!may_access_skb(env->prog->type)) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3042 | 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] | 3043 | return -EINVAL; |
| 3044 | } |
| 3045 | |
| 3046 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
Alexei Starovoitov | d82bccc | 2016-04-12 10:26:19 -0700 | [diff] [blame] | 3047 | BPF_SIZE(insn->code) == BPF_DW || |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3048 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3049 | verbose("BPF_LD_[ABS|IND] uses reserved fields\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3050 | return -EINVAL; |
| 3051 | } |
| 3052 | |
| 3053 | /* check whether implicit source operand (register R6) is readable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3054 | err = check_reg_arg(env, BPF_REG_6, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3055 | if (err) |
| 3056 | return err; |
| 3057 | |
| 3058 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { |
| 3059 | verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); |
| 3060 | return -EINVAL; |
| 3061 | } |
| 3062 | |
| 3063 | if (mode == BPF_IND) { |
| 3064 | /* check explicit source operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3065 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3066 | if (err) |
| 3067 | return err; |
| 3068 | } |
| 3069 | |
| 3070 | /* reset caller saved regs to unreadable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3071 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 3072 | mark_reg_not_init(regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3073 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 3074 | } |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3075 | |
| 3076 | /* mark destination R0 register as readable, since it contains |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3077 | * the value fetched from the packet. |
| 3078 | * Already marked as written above. |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3079 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3080 | mark_reg_unknown(regs, BPF_REG_0); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3081 | return 0; |
| 3082 | } |
| 3083 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3084 | static int check_return_code(struct bpf_verifier_env *env) |
| 3085 | { |
| 3086 | struct bpf_reg_state *reg; |
| 3087 | struct tnum range = tnum_range(0, 1); |
| 3088 | |
| 3089 | switch (env->prog->type) { |
| 3090 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 3091 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 3092 | case BPF_PROG_TYPE_SOCK_OPS: |
| 3093 | break; |
| 3094 | default: |
| 3095 | return 0; |
| 3096 | } |
| 3097 | |
| 3098 | reg = &env->cur_state.regs[BPF_REG_0]; |
| 3099 | if (reg->type != SCALAR_VALUE) { |
| 3100 | verbose("At program exit the register R0 is not a known value (%s)\n", |
| 3101 | reg_type_str[reg->type]); |
| 3102 | return -EINVAL; |
| 3103 | } |
| 3104 | |
| 3105 | if (!tnum_in(range, reg->var_off)) { |
| 3106 | verbose("At program exit the register R0 "); |
| 3107 | if (!tnum_is_unknown(reg->var_off)) { |
| 3108 | char tn_buf[48]; |
| 3109 | |
| 3110 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3111 | verbose("has value %s", tn_buf); |
| 3112 | } else { |
| 3113 | verbose("has unknown scalar value"); |
| 3114 | } |
| 3115 | verbose(" should have been 0 or 1\n"); |
| 3116 | return -EINVAL; |
| 3117 | } |
| 3118 | return 0; |
| 3119 | } |
| 3120 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3121 | /* non-recursive DFS pseudo code |
| 3122 | * 1 procedure DFS-iterative(G,v): |
| 3123 | * 2 label v as discovered |
| 3124 | * 3 let S be a stack |
| 3125 | * 4 S.push(v) |
| 3126 | * 5 while S is not empty |
| 3127 | * 6 t <- S.pop() |
| 3128 | * 7 if t is what we're looking for: |
| 3129 | * 8 return t |
| 3130 | * 9 for all edges e in G.adjacentEdges(t) do |
| 3131 | * 10 if edge e is already labelled |
| 3132 | * 11 continue with the next edge |
| 3133 | * 12 w <- G.adjacentVertex(t,e) |
| 3134 | * 13 if vertex w is not discovered and not explored |
| 3135 | * 14 label e as tree-edge |
| 3136 | * 15 label w as discovered |
| 3137 | * 16 S.push(w) |
| 3138 | * 17 continue at 5 |
| 3139 | * 18 else if vertex w is discovered |
| 3140 | * 19 label e as back-edge |
| 3141 | * 20 else |
| 3142 | * 21 // vertex w is explored |
| 3143 | * 22 label e as forward- or cross-edge |
| 3144 | * 23 label t as explored |
| 3145 | * 24 S.pop() |
| 3146 | * |
| 3147 | * convention: |
| 3148 | * 0x10 - discovered |
| 3149 | * 0x11 - discovered and fall-through edge labelled |
| 3150 | * 0x12 - discovered and fall-through and branch edges labelled |
| 3151 | * 0x20 - explored |
| 3152 | */ |
| 3153 | |
| 3154 | enum { |
| 3155 | DISCOVERED = 0x10, |
| 3156 | EXPLORED = 0x20, |
| 3157 | FALLTHROUGH = 1, |
| 3158 | BRANCH = 2, |
| 3159 | }; |
| 3160 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3161 | #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3162 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3163 | static int *insn_stack; /* stack of insns to process */ |
| 3164 | static int cur_stack; /* current stack index */ |
| 3165 | static int *insn_state; |
| 3166 | |
| 3167 | /* t, w, e - match pseudo-code above: |
| 3168 | * t - index of current instruction |
| 3169 | * w - next instruction |
| 3170 | * e - edge |
| 3171 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3172 | 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] | 3173 | { |
| 3174 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
| 3175 | return 0; |
| 3176 | |
| 3177 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
| 3178 | return 0; |
| 3179 | |
| 3180 | if (w < 0 || w >= env->prog->len) { |
| 3181 | verbose("jump out of range from insn %d to %d\n", t, w); |
| 3182 | return -EINVAL; |
| 3183 | } |
| 3184 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3185 | if (e == BRANCH) |
| 3186 | /* mark branch target for state pruning */ |
| 3187 | env->explored_states[w] = STATE_LIST_MARK; |
| 3188 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3189 | if (insn_state[w] == 0) { |
| 3190 | /* tree-edge */ |
| 3191 | insn_state[t] = DISCOVERED | e; |
| 3192 | insn_state[w] = DISCOVERED; |
| 3193 | if (cur_stack >= env->prog->len) |
| 3194 | return -E2BIG; |
| 3195 | insn_stack[cur_stack++] = w; |
| 3196 | return 1; |
| 3197 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
| 3198 | verbose("back-edge from insn %d to %d\n", t, w); |
| 3199 | return -EINVAL; |
| 3200 | } else if (insn_state[w] == EXPLORED) { |
| 3201 | /* forward- or cross-edge */ |
| 3202 | insn_state[t] = DISCOVERED | e; |
| 3203 | } else { |
| 3204 | verbose("insn state internal bug\n"); |
| 3205 | return -EFAULT; |
| 3206 | } |
| 3207 | return 0; |
| 3208 | } |
| 3209 | |
| 3210 | /* non-recursive depth-first-search to detect loops in BPF program |
| 3211 | * loop == back-edge in directed graph |
| 3212 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3213 | static int check_cfg(struct bpf_verifier_env *env) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3214 | { |
| 3215 | struct bpf_insn *insns = env->prog->insnsi; |
| 3216 | int insn_cnt = env->prog->len; |
| 3217 | int ret = 0; |
| 3218 | int i, t; |
| 3219 | |
| 3220 | insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 3221 | if (!insn_state) |
| 3222 | return -ENOMEM; |
| 3223 | |
| 3224 | insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 3225 | if (!insn_stack) { |
| 3226 | kfree(insn_state); |
| 3227 | return -ENOMEM; |
| 3228 | } |
| 3229 | |
| 3230 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 3231 | insn_stack[0] = 0; /* 0 is the first instruction */ |
| 3232 | cur_stack = 1; |
| 3233 | |
| 3234 | peek_stack: |
| 3235 | if (cur_stack == 0) |
| 3236 | goto check_state; |
| 3237 | t = insn_stack[cur_stack - 1]; |
| 3238 | |
| 3239 | if (BPF_CLASS(insns[t].code) == BPF_JMP) { |
| 3240 | u8 opcode = BPF_OP(insns[t].code); |
| 3241 | |
| 3242 | if (opcode == BPF_EXIT) { |
| 3243 | goto mark_explored; |
| 3244 | } else if (opcode == BPF_CALL) { |
| 3245 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 3246 | if (ret == 1) |
| 3247 | goto peek_stack; |
| 3248 | else if (ret < 0) |
| 3249 | goto err_free; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 3250 | if (t + 1 < insn_cnt) |
| 3251 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3252 | } else if (opcode == BPF_JA) { |
| 3253 | if (BPF_SRC(insns[t].code) != BPF_K) { |
| 3254 | ret = -EINVAL; |
| 3255 | goto err_free; |
| 3256 | } |
| 3257 | /* unconditional jump with single edge */ |
| 3258 | ret = push_insn(t, t + insns[t].off + 1, |
| 3259 | FALLTHROUGH, env); |
| 3260 | if (ret == 1) |
| 3261 | goto peek_stack; |
| 3262 | else if (ret < 0) |
| 3263 | goto err_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3264 | /* tell verifier to check for equivalent states |
| 3265 | * after every call and jump |
| 3266 | */ |
Alexei Starovoitov | c3de631 | 2015-04-14 15:57:13 -0700 | [diff] [blame] | 3267 | if (t + 1 < insn_cnt) |
| 3268 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3269 | } else { |
| 3270 | /* conditional jump with two edges */ |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 3271 | env->explored_states[t] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3272 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 3273 | if (ret == 1) |
| 3274 | goto peek_stack; |
| 3275 | else if (ret < 0) |
| 3276 | goto err_free; |
| 3277 | |
| 3278 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); |
| 3279 | if (ret == 1) |
| 3280 | goto peek_stack; |
| 3281 | else if (ret < 0) |
| 3282 | goto err_free; |
| 3283 | } |
| 3284 | } else { |
| 3285 | /* all other non-branch instructions with single |
| 3286 | * fall-through edge |
| 3287 | */ |
| 3288 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 3289 | if (ret == 1) |
| 3290 | goto peek_stack; |
| 3291 | else if (ret < 0) |
| 3292 | goto err_free; |
| 3293 | } |
| 3294 | |
| 3295 | mark_explored: |
| 3296 | insn_state[t] = EXPLORED; |
| 3297 | if (cur_stack-- <= 0) { |
| 3298 | verbose("pop stack internal bug\n"); |
| 3299 | ret = -EFAULT; |
| 3300 | goto err_free; |
| 3301 | } |
| 3302 | goto peek_stack; |
| 3303 | |
| 3304 | check_state: |
| 3305 | for (i = 0; i < insn_cnt; i++) { |
| 3306 | if (insn_state[i] != EXPLORED) { |
| 3307 | verbose("unreachable insn %d\n", i); |
| 3308 | ret = -EINVAL; |
| 3309 | goto err_free; |
| 3310 | } |
| 3311 | } |
| 3312 | ret = 0; /* cfg looks good */ |
| 3313 | |
| 3314 | err_free: |
| 3315 | kfree(insn_state); |
| 3316 | kfree(insn_stack); |
| 3317 | return ret; |
| 3318 | } |
| 3319 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3320 | /* check %cur's range satisfies %old's */ |
| 3321 | static bool range_within(struct bpf_reg_state *old, |
| 3322 | struct bpf_reg_state *cur) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3323 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3324 | return old->umin_value <= cur->umin_value && |
| 3325 | old->umax_value >= cur->umax_value && |
| 3326 | old->smin_value <= cur->smin_value && |
| 3327 | old->smax_value >= cur->smax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3328 | } |
| 3329 | |
| 3330 | /* Maximum number of register states that can exist at once */ |
| 3331 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) |
| 3332 | struct idpair { |
| 3333 | u32 old; |
| 3334 | u32 cur; |
| 3335 | }; |
| 3336 | |
| 3337 | /* If in the old state two registers had the same id, then they need to have |
| 3338 | * the same id in the new state as well. But that id could be different from |
| 3339 | * the old state, so we need to track the mapping from old to new ids. |
| 3340 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent |
| 3341 | * regs with old id 5 must also have new id 9 for the new state to be safe. But |
| 3342 | * regs with a different old id could still have new id 9, we don't care about |
| 3343 | * that. |
| 3344 | * So we look through our idmap to see if this old id has been seen before. If |
| 3345 | * so, we require the new id to match; otherwise, we add the id pair to the map. |
| 3346 | */ |
| 3347 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
| 3348 | { |
| 3349 | unsigned int i; |
| 3350 | |
| 3351 | for (i = 0; i < ID_MAP_SIZE; i++) { |
| 3352 | if (!idmap[i].old) { |
| 3353 | /* Reached an empty slot; haven't seen this id before */ |
| 3354 | idmap[i].old = old_id; |
| 3355 | idmap[i].cur = cur_id; |
| 3356 | return true; |
| 3357 | } |
| 3358 | if (idmap[i].old == old_id) |
| 3359 | return idmap[i].cur == cur_id; |
| 3360 | } |
| 3361 | /* We ran out of idmap slots, which should be impossible */ |
| 3362 | WARN_ON_ONCE(1); |
| 3363 | return false; |
| 3364 | } |
| 3365 | |
| 3366 | /* Returns true if (rold safe implies rcur safe) */ |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 3367 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
| 3368 | struct idpair *idmap) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3369 | { |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3370 | if (!(rold->live & REG_LIVE_READ)) |
| 3371 | /* explored state didn't use this */ |
| 3372 | return true; |
| 3373 | |
| 3374 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, live)) == 0) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3375 | return true; |
| 3376 | |
| 3377 | if (rold->type == NOT_INIT) |
| 3378 | /* explored state can't have used this */ |
| 3379 | return true; |
| 3380 | if (rcur->type == NOT_INIT) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3381 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3382 | switch (rold->type) { |
| 3383 | case SCALAR_VALUE: |
| 3384 | if (rcur->type == SCALAR_VALUE) { |
| 3385 | /* new val must satisfy old val knowledge */ |
| 3386 | return range_within(rold, rcur) && |
| 3387 | tnum_in(rold->var_off, rcur->var_off); |
| 3388 | } else { |
| 3389 | /* if we knew anything about the old value, we're not |
| 3390 | * equal, because we can't know anything about the |
| 3391 | * scalar value of the pointer in the new value. |
| 3392 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3393 | return rold->umin_value == 0 && |
| 3394 | rold->umax_value == U64_MAX && |
| 3395 | rold->smin_value == S64_MIN && |
| 3396 | rold->smax_value == S64_MAX && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3397 | tnum_is_unknown(rold->var_off); |
| 3398 | } |
| 3399 | case PTR_TO_MAP_VALUE: |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 3400 | /* If the new min/max/var_off satisfy the old ones and |
| 3401 | * everything else matches, we are OK. |
| 3402 | * We don't care about the 'id' value, because nothing |
| 3403 | * uses it for PTR_TO_MAP_VALUE (only for ..._OR_NULL) |
| 3404 | */ |
| 3405 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && |
| 3406 | range_within(rold, rcur) && |
| 3407 | tnum_in(rold->var_off, rcur->var_off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3408 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 3409 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
| 3410 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. |
| 3411 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- |
| 3412 | * checked, doing so could have affected others with the same |
| 3413 | * id, and we can't check for that because we lost the id when |
| 3414 | * we converted to a PTR_TO_MAP_VALUE. |
| 3415 | */ |
| 3416 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) |
| 3417 | return false; |
| 3418 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) |
| 3419 | return false; |
| 3420 | /* Check our ids match any regs they're supposed to */ |
| 3421 | return check_ids(rold->id, rcur->id, idmap); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3422 | case PTR_TO_PACKET_META: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3423 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3424 | if (rcur->type != rold->type) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3425 | return false; |
| 3426 | /* We must have at least as much range as the old ptr |
| 3427 | * did, so that any accesses which were safe before are |
| 3428 | * still safe. This is true even if old range < old off, |
| 3429 | * since someone could have accessed through (ptr - k), or |
| 3430 | * even done ptr -= k in a register, to get a safe access. |
| 3431 | */ |
| 3432 | if (rold->range > rcur->range) |
| 3433 | return false; |
| 3434 | /* If the offsets don't match, we can't trust our alignment; |
| 3435 | * nor can we be sure that we won't fall out of range. |
| 3436 | */ |
| 3437 | if (rold->off != rcur->off) |
| 3438 | return false; |
| 3439 | /* id relations must be preserved */ |
| 3440 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) |
| 3441 | return false; |
| 3442 | /* new val must satisfy old val knowledge */ |
| 3443 | return range_within(rold, rcur) && |
| 3444 | tnum_in(rold->var_off, rcur->var_off); |
| 3445 | case PTR_TO_CTX: |
| 3446 | case CONST_PTR_TO_MAP: |
| 3447 | case PTR_TO_STACK: |
| 3448 | case PTR_TO_PACKET_END: |
| 3449 | /* Only valid matches are exact, which memcmp() above |
| 3450 | * would have accepted |
| 3451 | */ |
| 3452 | default: |
| 3453 | /* Don't know what's going on, just say it's not safe */ |
| 3454 | return false; |
| 3455 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3456 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3457 | /* Shouldn't get here; if we do, say it's not safe */ |
| 3458 | WARN_ON_ONCE(1); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3459 | return false; |
| 3460 | } |
| 3461 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3462 | /* compare two verifier states |
| 3463 | * |
| 3464 | * all states stored in state_list are known to be valid, since |
| 3465 | * verifier reached 'bpf_exit' instruction through them |
| 3466 | * |
| 3467 | * this function is called when verifier exploring different branches of |
| 3468 | * execution popped from the state stack. If it sees an old state that has |
| 3469 | * more strict register state and more strict stack state then this execution |
| 3470 | * branch doesn't need to be explored further, since verifier already |
| 3471 | * concluded that more strict state leads to valid finish. |
| 3472 | * |
| 3473 | * Therefore two states are equivalent if register state is more conservative |
| 3474 | * and explored stack state is more conservative than the current one. |
| 3475 | * Example: |
| 3476 | * explored current |
| 3477 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 3478 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 3479 | * |
| 3480 | * In other words if current stack state (one being explored) has more |
| 3481 | * valid slots than old one that already passed validation, it means |
| 3482 | * the verifier can stop exploring and conclude that current state is valid too |
| 3483 | * |
| 3484 | * Similarly with registers. If explored state has register type as invalid |
| 3485 | * whereas register type in current state is meaningful, it means that |
| 3486 | * the current state will reach 'bpf_exit' instruction safely |
| 3487 | */ |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3488 | static bool states_equal(struct bpf_verifier_env *env, |
| 3489 | struct bpf_verifier_state *old, |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3490 | struct bpf_verifier_state *cur) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3491 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3492 | struct idpair *idmap; |
| 3493 | bool ret = false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3494 | int i; |
| 3495 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3496 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
| 3497 | /* If we failed to allocate the idmap, just say it's not safe */ |
| 3498 | if (!idmap) |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3499 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3500 | |
| 3501 | for (i = 0; i < MAX_BPF_REG; i++) { |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 3502 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3503 | goto out_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3504 | } |
| 3505 | |
| 3506 | for (i = 0; i < MAX_BPF_STACK; i++) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 3507 | if (old->stack_slot_type[i] == STACK_INVALID) |
| 3508 | continue; |
| 3509 | if (old->stack_slot_type[i] != cur->stack_slot_type[i]) |
| 3510 | /* Ex: old explored (safe) state has STACK_SPILL in |
| 3511 | * this stack slot, but current has has STACK_MISC -> |
| 3512 | * this verifier states are not equivalent, |
| 3513 | * return false to continue verification of this path |
| 3514 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3515 | goto out_free; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 3516 | if (i % BPF_REG_SIZE) |
| 3517 | continue; |
Daniel Borkmann | d25da6c | 2017-06-11 00:50:41 +0200 | [diff] [blame] | 3518 | if (old->stack_slot_type[i] != STACK_SPILL) |
| 3519 | continue; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3520 | if (!regsafe(&old->spilled_regs[i / BPF_REG_SIZE], |
| 3521 | &cur->spilled_regs[i / BPF_REG_SIZE], |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 3522 | idmap)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3523 | /* when explored and current stack slot are both storing |
| 3524 | * spilled registers, check that stored pointers types |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 3525 | * are the same as well. |
| 3526 | * Ex: explored safe path could have stored |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3527 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 3528 | * but current path has stored: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3529 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 3530 | * such verifier states are not equivalent. |
| 3531 | * return false to continue verification of this path |
| 3532 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3533 | goto out_free; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 3534 | else |
| 3535 | continue; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3536 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3537 | ret = true; |
| 3538 | out_free: |
| 3539 | kfree(idmap); |
| 3540 | return ret; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3541 | } |
| 3542 | |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 3543 | /* A write screens off any subsequent reads; but write marks come from the |
| 3544 | * straight-line code between a state and its parent. When we arrive at a |
| 3545 | * jump target (in the first iteration of the propagate_liveness() loop), |
| 3546 | * we didn't arrive by the straight-line code, so read marks in state must |
| 3547 | * propagate to parent regardless of state's write marks. |
| 3548 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3549 | static bool do_propagate_liveness(const struct bpf_verifier_state *state, |
| 3550 | struct bpf_verifier_state *parent) |
| 3551 | { |
Edward Cree | 63f45f8 | 2017-08-23 15:10:03 +0100 | [diff] [blame] | 3552 | bool writes = parent == state->parent; /* Observe write marks */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3553 | bool touched = false; /* any changes made? */ |
| 3554 | int i; |
| 3555 | |
| 3556 | if (!parent) |
| 3557 | return touched; |
| 3558 | /* Propagate read liveness of registers... */ |
| 3559 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
| 3560 | /* We don't need to worry about FP liveness because it's read-only */ |
| 3561 | for (i = 0; i < BPF_REG_FP; i++) { |
| 3562 | if (parent->regs[i].live & REG_LIVE_READ) |
| 3563 | continue; |
Edward Cree | 63f45f8 | 2017-08-23 15:10:03 +0100 | [diff] [blame] | 3564 | if (writes && (state->regs[i].live & REG_LIVE_WRITTEN)) |
| 3565 | continue; |
| 3566 | if (state->regs[i].live & REG_LIVE_READ) { |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3567 | parent->regs[i].live |= REG_LIVE_READ; |
| 3568 | touched = true; |
| 3569 | } |
| 3570 | } |
| 3571 | /* ... and stack slots */ |
| 3572 | for (i = 0; i < MAX_BPF_STACK / BPF_REG_SIZE; i++) { |
| 3573 | if (parent->stack_slot_type[i * BPF_REG_SIZE] != STACK_SPILL) |
| 3574 | continue; |
| 3575 | if (state->stack_slot_type[i * BPF_REG_SIZE] != STACK_SPILL) |
| 3576 | continue; |
| 3577 | if (parent->spilled_regs[i].live & REG_LIVE_READ) |
| 3578 | continue; |
Edward Cree | 63f45f8 | 2017-08-23 15:10:03 +0100 | [diff] [blame] | 3579 | if (writes && (state->spilled_regs[i].live & REG_LIVE_WRITTEN)) |
| 3580 | continue; |
| 3581 | if (state->spilled_regs[i].live & REG_LIVE_READ) { |
Daniel Borkmann | 1ab2de2 | 2017-08-17 14:59:40 +0200 | [diff] [blame] | 3582 | parent->spilled_regs[i].live |= REG_LIVE_READ; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3583 | touched = true; |
| 3584 | } |
| 3585 | } |
| 3586 | return touched; |
| 3587 | } |
| 3588 | |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 3589 | /* "parent" is "a state from which we reach the current state", but initially |
| 3590 | * it is not the state->parent (i.e. "the state whose straight-line code leads |
| 3591 | * to the current state"), instead it is the state that happened to arrive at |
| 3592 | * a (prunable) equivalent of the current state. See comment above |
| 3593 | * do_propagate_liveness() for consequences of this. |
| 3594 | * This function is just a more efficient way of calling mark_reg_read() or |
| 3595 | * mark_stack_slot_read() on each reg in "parent" that is read in "state", |
| 3596 | * though it requires that parent != state->parent in the call arguments. |
| 3597 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3598 | static void propagate_liveness(const struct bpf_verifier_state *state, |
| 3599 | struct bpf_verifier_state *parent) |
| 3600 | { |
| 3601 | while (do_propagate_liveness(state, parent)) { |
| 3602 | /* Something changed, so we need to feed those changes onward */ |
| 3603 | state = parent; |
| 3604 | parent = state->parent; |
| 3605 | } |
| 3606 | } |
| 3607 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3608 | 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] | 3609 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3610 | struct bpf_verifier_state_list *new_sl; |
| 3611 | struct bpf_verifier_state_list *sl; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3612 | int i; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3613 | |
| 3614 | sl = env->explored_states[insn_idx]; |
| 3615 | if (!sl) |
| 3616 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 3617 | * be doing state search here |
| 3618 | */ |
| 3619 | return 0; |
| 3620 | |
| 3621 | while (sl != STATE_LIST_MARK) { |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3622 | if (states_equal(env, &sl->state, &env->cur_state)) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3623 | /* reached equivalent register/stack state, |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3624 | * prune the search. |
| 3625 | * Registers read by the continuation are read by us. |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 3626 | * If we have any write marks in env->cur_state, they |
| 3627 | * will prevent corresponding reads in the continuation |
| 3628 | * from reaching our parent (an explored_state). Our |
| 3629 | * own state will get the read marks recorded, but |
| 3630 | * they'll be immediately forgotten as we're pruning |
| 3631 | * this state and will pop a new one. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3632 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3633 | propagate_liveness(&sl->state, &env->cur_state); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3634 | return 1; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3635 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3636 | sl = sl->next; |
| 3637 | } |
| 3638 | |
| 3639 | /* there were no equivalent states, remember current one. |
| 3640 | * technically the current state is not proven to be safe yet, |
| 3641 | * but it will either reach bpf_exit (which means it's safe) or |
| 3642 | * it will be rejected. Since there are no loops, we won't be |
| 3643 | * seeing this 'insn_idx' instruction again on the way to bpf_exit |
| 3644 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3645 | new_sl = kmalloc(sizeof(struct bpf_verifier_state_list), GFP_USER); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3646 | if (!new_sl) |
| 3647 | return -ENOMEM; |
| 3648 | |
| 3649 | /* add new state to the head of linked list */ |
| 3650 | memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state)); |
| 3651 | new_sl->next = env->explored_states[insn_idx]; |
| 3652 | env->explored_states[insn_idx] = new_sl; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3653 | /* connect new state to parentage chain */ |
| 3654 | env->cur_state.parent = &new_sl->state; |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 3655 | /* clear write marks in current state: the writes we did are not writes |
| 3656 | * our child did, so they don't screen off its reads from us. |
| 3657 | * (There are no read marks in current state, because reads always mark |
| 3658 | * their parent and current state never has children yet. Only |
| 3659 | * explored_states can get read marks.) |
| 3660 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3661 | for (i = 0; i < BPF_REG_FP; i++) |
| 3662 | env->cur_state.regs[i].live = REG_LIVE_NONE; |
| 3663 | for (i = 0; i < MAX_BPF_STACK / BPF_REG_SIZE; i++) |
| 3664 | if (env->cur_state.stack_slot_type[i * BPF_REG_SIZE] == STACK_SPILL) |
| 3665 | env->cur_state.spilled_regs[i].live = REG_LIVE_NONE; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3666 | return 0; |
| 3667 | } |
| 3668 | |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 3669 | static int ext_analyzer_insn_hook(struct bpf_verifier_env *env, |
| 3670 | int insn_idx, int prev_insn_idx) |
| 3671 | { |
| 3672 | if (!env->analyzer_ops || !env->analyzer_ops->insn_hook) |
| 3673 | return 0; |
| 3674 | |
| 3675 | return env->analyzer_ops->insn_hook(env, insn_idx, prev_insn_idx); |
| 3676 | } |
| 3677 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3678 | static int do_check(struct bpf_verifier_env *env) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3679 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3680 | struct bpf_verifier_state *state = &env->cur_state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3681 | struct bpf_insn *insns = env->prog->insnsi; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3682 | struct bpf_reg_state *regs = state->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3683 | int insn_cnt = env->prog->len; |
| 3684 | int insn_idx, prev_insn_idx = 0; |
| 3685 | int insn_processed = 0; |
| 3686 | bool do_print_state = false; |
| 3687 | |
| 3688 | init_reg_state(regs); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3689 | state->parent = NULL; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3690 | insn_idx = 0; |
| 3691 | for (;;) { |
| 3692 | struct bpf_insn *insn; |
| 3693 | u8 class; |
| 3694 | int err; |
| 3695 | |
| 3696 | if (insn_idx >= insn_cnt) { |
| 3697 | verbose("invalid insn idx %d insn_cnt %d\n", |
| 3698 | insn_idx, insn_cnt); |
| 3699 | return -EFAULT; |
| 3700 | } |
| 3701 | |
| 3702 | insn = &insns[insn_idx]; |
| 3703 | class = BPF_CLASS(insn->code); |
| 3704 | |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 3705 | if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
Colin Ian King | bc1750f | 2017-02-23 00:20:53 +0000 | [diff] [blame] | 3706 | verbose("BPF program is too large. Processed %d insn\n", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3707 | insn_processed); |
| 3708 | return -E2BIG; |
| 3709 | } |
| 3710 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3711 | err = is_state_visited(env, insn_idx); |
| 3712 | if (err < 0) |
| 3713 | return err; |
| 3714 | if (err == 1) { |
| 3715 | /* found equivalent state, can prune the search */ |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame^] | 3716 | if (verifier_log.level) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3717 | if (do_print_state) |
| 3718 | verbose("\nfrom %d to %d: safe\n", |
| 3719 | prev_insn_idx, insn_idx); |
| 3720 | else |
| 3721 | verbose("%d: safe\n", insn_idx); |
| 3722 | } |
| 3723 | goto process_bpf_exit; |
| 3724 | } |
| 3725 | |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 3726 | if (need_resched()) |
| 3727 | cond_resched(); |
| 3728 | |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame^] | 3729 | if (verifier_log.level > 1 || |
| 3730 | (verifier_log.level && do_print_state)) { |
| 3731 | if (verifier_log.level > 1) |
David S. Miller | c5fc969 | 2017-05-10 11:25:17 -0700 | [diff] [blame] | 3732 | verbose("%d:", insn_idx); |
| 3733 | else |
| 3734 | verbose("\nfrom %d to %d:", |
| 3735 | prev_insn_idx, insn_idx); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3736 | print_verifier_state(&env->cur_state); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3737 | do_print_state = false; |
| 3738 | } |
| 3739 | |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame^] | 3740 | if (verifier_log.level) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3741 | verbose("%d: ", insn_idx); |
Daniel Borkmann | 0d0e576 | 2017-05-08 00:04:09 +0200 | [diff] [blame] | 3742 | print_bpf_insn(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3743 | } |
| 3744 | |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 3745 | err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx); |
| 3746 | if (err) |
| 3747 | return err; |
| 3748 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3749 | if (class == BPF_ALU || class == BPF_ALU64) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3750 | err = check_alu_op(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3751 | if (err) |
| 3752 | return err; |
| 3753 | |
| 3754 | } else if (class == BPF_LDX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3755 | enum bpf_reg_type *prev_src_type, src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3756 | |
| 3757 | /* check for reserved fields is already done */ |
| 3758 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3759 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3760 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3761 | if (err) |
| 3762 | return err; |
| 3763 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3764 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3765 | if (err) |
| 3766 | return err; |
| 3767 | |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 3768 | src_reg_type = regs[insn->src_reg].type; |
| 3769 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3770 | /* check that memory (src_reg + off) is readable, |
| 3771 | * the state of dst_reg will be updated by this func |
| 3772 | */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3773 | err = check_mem_access(env, insn_idx, insn->src_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3774 | BPF_SIZE(insn->code), BPF_READ, |
| 3775 | insn->dst_reg); |
| 3776 | if (err) |
| 3777 | return err; |
| 3778 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3779 | prev_src_type = &env->insn_aux_data[insn_idx].ptr_type; |
| 3780 | |
| 3781 | if (*prev_src_type == NOT_INIT) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3782 | /* saw a valid insn |
| 3783 | * dst_reg = *(u32 *)(src_reg + off) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3784 | * save type to validate intersecting paths |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3785 | */ |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3786 | *prev_src_type = src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3787 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3788 | } else if (src_reg_type != *prev_src_type && |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3789 | (src_reg_type == PTR_TO_CTX || |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3790 | *prev_src_type == PTR_TO_CTX)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3791 | /* ABuser program is trying to use the same insn |
| 3792 | * dst_reg = *(u32*) (src_reg + off) |
| 3793 | * with different pointer types: |
| 3794 | * src_reg == ctx in one branch and |
| 3795 | * src_reg == stack|map in some other branch. |
| 3796 | * Reject it. |
| 3797 | */ |
| 3798 | verbose("same insn cannot be used with different pointers\n"); |
| 3799 | return -EINVAL; |
| 3800 | } |
| 3801 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3802 | } else if (class == BPF_STX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3803 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 3804 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3805 | if (BPF_MODE(insn->code) == BPF_XADD) { |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3806 | err = check_xadd(env, insn_idx, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3807 | if (err) |
| 3808 | return err; |
| 3809 | insn_idx++; |
| 3810 | continue; |
| 3811 | } |
| 3812 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3813 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3814 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3815 | if (err) |
| 3816 | return err; |
| 3817 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3818 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3819 | if (err) |
| 3820 | return err; |
| 3821 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 3822 | dst_reg_type = regs[insn->dst_reg].type; |
| 3823 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3824 | /* check that memory (dst_reg + off) is writeable */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3825 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3826 | BPF_SIZE(insn->code), BPF_WRITE, |
| 3827 | insn->src_reg); |
| 3828 | if (err) |
| 3829 | return err; |
| 3830 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3831 | prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type; |
| 3832 | |
| 3833 | if (*prev_dst_type == NOT_INIT) { |
| 3834 | *prev_dst_type = dst_reg_type; |
| 3835 | } else if (dst_reg_type != *prev_dst_type && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 3836 | (dst_reg_type == PTR_TO_CTX || |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3837 | *prev_dst_type == PTR_TO_CTX)) { |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 3838 | verbose("same insn cannot be used with different pointers\n"); |
| 3839 | return -EINVAL; |
| 3840 | } |
| 3841 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3842 | } else if (class == BPF_ST) { |
| 3843 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 3844 | insn->src_reg != BPF_REG_0) { |
| 3845 | verbose("BPF_ST uses reserved fields\n"); |
| 3846 | return -EINVAL; |
| 3847 | } |
| 3848 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3849 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3850 | if (err) |
| 3851 | return err; |
| 3852 | |
| 3853 | /* check that memory (dst_reg + off) is writeable */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3854 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3855 | BPF_SIZE(insn->code), BPF_WRITE, |
| 3856 | -1); |
| 3857 | if (err) |
| 3858 | return err; |
| 3859 | |
| 3860 | } else if (class == BPF_JMP) { |
| 3861 | u8 opcode = BPF_OP(insn->code); |
| 3862 | |
| 3863 | if (opcode == BPF_CALL) { |
| 3864 | if (BPF_SRC(insn->code) != BPF_K || |
| 3865 | insn->off != 0 || |
| 3866 | insn->src_reg != BPF_REG_0 || |
| 3867 | insn->dst_reg != BPF_REG_0) { |
| 3868 | verbose("BPF_CALL uses reserved fields\n"); |
| 3869 | return -EINVAL; |
| 3870 | } |
| 3871 | |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 3872 | err = check_call(env, insn->imm, insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3873 | if (err) |
| 3874 | return err; |
| 3875 | |
| 3876 | } else if (opcode == BPF_JA) { |
| 3877 | if (BPF_SRC(insn->code) != BPF_K || |
| 3878 | insn->imm != 0 || |
| 3879 | insn->src_reg != BPF_REG_0 || |
| 3880 | insn->dst_reg != BPF_REG_0) { |
| 3881 | verbose("BPF_JA uses reserved fields\n"); |
| 3882 | return -EINVAL; |
| 3883 | } |
| 3884 | |
| 3885 | insn_idx += insn->off + 1; |
| 3886 | continue; |
| 3887 | |
| 3888 | } else if (opcode == BPF_EXIT) { |
| 3889 | if (BPF_SRC(insn->code) != BPF_K || |
| 3890 | insn->imm != 0 || |
| 3891 | insn->src_reg != BPF_REG_0 || |
| 3892 | insn->dst_reg != BPF_REG_0) { |
| 3893 | verbose("BPF_EXIT uses reserved fields\n"); |
| 3894 | return -EINVAL; |
| 3895 | } |
| 3896 | |
| 3897 | /* eBPF calling convetion is such that R0 is used |
| 3898 | * to return the value from eBPF program. |
| 3899 | * Make sure that it's readable at this time |
| 3900 | * of bpf_exit, which means that program wrote |
| 3901 | * something into it earlier |
| 3902 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3903 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3904 | if (err) |
| 3905 | return err; |
| 3906 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3907 | if (is_pointer_value(env, BPF_REG_0)) { |
| 3908 | verbose("R0 leaks addr as return value\n"); |
| 3909 | return -EACCES; |
| 3910 | } |
| 3911 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3912 | err = check_return_code(env); |
| 3913 | if (err) |
| 3914 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3915 | process_bpf_exit: |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3916 | insn_idx = pop_stack(env, &prev_insn_idx); |
| 3917 | if (insn_idx < 0) { |
| 3918 | break; |
| 3919 | } else { |
| 3920 | do_print_state = true; |
| 3921 | continue; |
| 3922 | } |
| 3923 | } else { |
| 3924 | err = check_cond_jmp_op(env, insn, &insn_idx); |
| 3925 | if (err) |
| 3926 | return err; |
| 3927 | } |
| 3928 | } else if (class == BPF_LD) { |
| 3929 | u8 mode = BPF_MODE(insn->code); |
| 3930 | |
| 3931 | if (mode == BPF_ABS || mode == BPF_IND) { |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3932 | err = check_ld_abs(env, insn); |
| 3933 | if (err) |
| 3934 | return err; |
| 3935 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3936 | } else if (mode == BPF_IMM) { |
| 3937 | err = check_ld_imm(env, insn); |
| 3938 | if (err) |
| 3939 | return err; |
| 3940 | |
| 3941 | insn_idx++; |
| 3942 | } else { |
| 3943 | verbose("invalid BPF_LD mode\n"); |
| 3944 | return -EINVAL; |
| 3945 | } |
| 3946 | } else { |
| 3947 | verbose("unknown insn class %d\n", class); |
| 3948 | return -EINVAL; |
| 3949 | } |
| 3950 | |
| 3951 | insn_idx++; |
| 3952 | } |
| 3953 | |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 3954 | verbose("processed %d insns, stack depth %d\n", |
| 3955 | insn_processed, env->prog->aux->stack_depth); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3956 | return 0; |
| 3957 | } |
| 3958 | |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 3959 | static int check_map_prealloc(struct bpf_map *map) |
| 3960 | { |
| 3961 | return (map->map_type != BPF_MAP_TYPE_HASH && |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 3962 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
| 3963 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 3964 | !(map->map_flags & BPF_F_NO_PREALLOC); |
| 3965 | } |
| 3966 | |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 3967 | static int check_map_prog_compatibility(struct bpf_map *map, |
| 3968 | struct bpf_prog *prog) |
| 3969 | |
| 3970 | { |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 3971 | /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use |
| 3972 | * preallocated hash maps, since doing memory allocation |
| 3973 | * in overflow_handler can crash depending on where nmi got |
| 3974 | * triggered. |
| 3975 | */ |
| 3976 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { |
| 3977 | if (!check_map_prealloc(map)) { |
| 3978 | verbose("perf_event programs can only use preallocated hash map\n"); |
| 3979 | return -EINVAL; |
| 3980 | } |
| 3981 | if (map->inner_map_meta && |
| 3982 | !check_map_prealloc(map->inner_map_meta)) { |
| 3983 | verbose("perf_event programs can only use preallocated inner hash map\n"); |
| 3984 | return -EINVAL; |
| 3985 | } |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 3986 | } |
| 3987 | return 0; |
| 3988 | } |
| 3989 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3990 | /* look for pseudo eBPF instructions that access map FDs and |
| 3991 | * replace them with actual map pointers |
| 3992 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3993 | 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] | 3994 | { |
| 3995 | struct bpf_insn *insn = env->prog->insnsi; |
| 3996 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 3997 | int i, j, err; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3998 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 3999 | err = bpf_prog_calc_tag(env->prog); |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 4000 | if (err) |
| 4001 | return err; |
| 4002 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4003 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4004 | if (BPF_CLASS(insn->code) == BPF_LDX && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4005 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4006 | verbose("BPF_LDX uses reserved fields\n"); |
| 4007 | return -EINVAL; |
| 4008 | } |
| 4009 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4010 | if (BPF_CLASS(insn->code) == BPF_STX && |
| 4011 | ((BPF_MODE(insn->code) != BPF_MEM && |
| 4012 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { |
| 4013 | verbose("BPF_STX uses reserved fields\n"); |
| 4014 | return -EINVAL; |
| 4015 | } |
| 4016 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4017 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
| 4018 | struct bpf_map *map; |
| 4019 | struct fd f; |
| 4020 | |
| 4021 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 4022 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 4023 | insn[1].off != 0) { |
| 4024 | verbose("invalid bpf_ld_imm64 insn\n"); |
| 4025 | return -EINVAL; |
| 4026 | } |
| 4027 | |
| 4028 | if (insn->src_reg == 0) |
| 4029 | /* valid generic load 64-bit imm */ |
| 4030 | goto next_insn; |
| 4031 | |
| 4032 | if (insn->src_reg != BPF_PSEUDO_MAP_FD) { |
| 4033 | verbose("unrecognized bpf_ld_imm64 insn\n"); |
| 4034 | return -EINVAL; |
| 4035 | } |
| 4036 | |
| 4037 | f = fdget(insn->imm); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 4038 | map = __bpf_map_get(f); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4039 | if (IS_ERR(map)) { |
| 4040 | verbose("fd %d is not pointing to valid bpf_map\n", |
| 4041 | insn->imm); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4042 | return PTR_ERR(map); |
| 4043 | } |
| 4044 | |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 4045 | err = check_map_prog_compatibility(map, env->prog); |
| 4046 | if (err) { |
| 4047 | fdput(f); |
| 4048 | return err; |
| 4049 | } |
| 4050 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4051 | /* store map pointer inside BPF_LD_IMM64 instruction */ |
| 4052 | insn[0].imm = (u32) (unsigned long) map; |
| 4053 | insn[1].imm = ((u64) (unsigned long) map) >> 32; |
| 4054 | |
| 4055 | /* check whether we recorded this map already */ |
| 4056 | for (j = 0; j < env->used_map_cnt; j++) |
| 4057 | if (env->used_maps[j] == map) { |
| 4058 | fdput(f); |
| 4059 | goto next_insn; |
| 4060 | } |
| 4061 | |
| 4062 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 4063 | fdput(f); |
| 4064 | return -E2BIG; |
| 4065 | } |
| 4066 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4067 | /* hold the map. If the program is rejected by verifier, |
| 4068 | * the map will be released by release_maps() or it |
| 4069 | * will be used by the valid program until it's unloaded |
| 4070 | * and all maps are released in free_bpf_prog_info() |
| 4071 | */ |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 4072 | map = bpf_map_inc(map, false); |
| 4073 | if (IS_ERR(map)) { |
| 4074 | fdput(f); |
| 4075 | return PTR_ERR(map); |
| 4076 | } |
| 4077 | env->used_maps[env->used_map_cnt++] = map; |
| 4078 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4079 | fdput(f); |
| 4080 | next_insn: |
| 4081 | insn++; |
| 4082 | i++; |
| 4083 | } |
| 4084 | } |
| 4085 | |
| 4086 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 4087 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 4088 | * These pointers will be used later by verifier to validate map access. |
| 4089 | */ |
| 4090 | return 0; |
| 4091 | } |
| 4092 | |
| 4093 | /* drop refcnt of maps used by the rejected program */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4094 | static void release_maps(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4095 | { |
| 4096 | int i; |
| 4097 | |
| 4098 | for (i = 0; i < env->used_map_cnt; i++) |
| 4099 | bpf_map_put(env->used_maps[i]); |
| 4100 | } |
| 4101 | |
| 4102 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4103 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4104 | { |
| 4105 | struct bpf_insn *insn = env->prog->insnsi; |
| 4106 | int insn_cnt = env->prog->len; |
| 4107 | int i; |
| 4108 | |
| 4109 | for (i = 0; i < insn_cnt; i++, insn++) |
| 4110 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) |
| 4111 | insn->src_reg = 0; |
| 4112 | } |
| 4113 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 4114 | /* single env->prog->insni[off] instruction was replaced with the range |
| 4115 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying |
| 4116 | * [0, off) and [off, end) to new locations, so the patched range stays zero |
| 4117 | */ |
| 4118 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len, |
| 4119 | u32 off, u32 cnt) |
| 4120 | { |
| 4121 | struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data; |
| 4122 | |
| 4123 | if (cnt == 1) |
| 4124 | return 0; |
| 4125 | new_data = vzalloc(sizeof(struct bpf_insn_aux_data) * prog_len); |
| 4126 | if (!new_data) |
| 4127 | return -ENOMEM; |
| 4128 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
| 4129 | memcpy(new_data + off + cnt - 1, old_data + off, |
| 4130 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); |
| 4131 | env->insn_aux_data = new_data; |
| 4132 | vfree(old_data); |
| 4133 | return 0; |
| 4134 | } |
| 4135 | |
| 4136 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
| 4137 | const struct bpf_insn *patch, u32 len) |
| 4138 | { |
| 4139 | struct bpf_prog *new_prog; |
| 4140 | |
| 4141 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); |
| 4142 | if (!new_prog) |
| 4143 | return NULL; |
| 4144 | if (adjust_insn_aux_data(env, new_prog->len, off, len)) |
| 4145 | return NULL; |
| 4146 | return new_prog; |
| 4147 | } |
| 4148 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4149 | /* convert load instructions that access fields of 'struct __sk_buff' |
| 4150 | * into sequence of instructions that access fields of 'struct sk_buff' |
| 4151 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4152 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4153 | { |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 4154 | const struct bpf_verifier_ops *ops = env->prog->aux->ops; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4155 | int i, cnt, size, ctx_field_size, delta = 0; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4156 | const int insn_cnt = env->prog->len; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 4157 | struct bpf_insn insn_buf[16], *insn; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4158 | struct bpf_prog *new_prog; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4159 | enum bpf_access_type type; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4160 | bool is_narrower_load; |
| 4161 | u32 target_size; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4162 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 4163 | if (ops->gen_prologue) { |
| 4164 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
| 4165 | env->prog); |
| 4166 | if (cnt >= ARRAY_SIZE(insn_buf)) { |
| 4167 | verbose("bpf verifier is misconfigured\n"); |
| 4168 | return -EINVAL; |
| 4169 | } else if (cnt) { |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 4170 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 4171 | if (!new_prog) |
| 4172 | return -ENOMEM; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 4173 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 4174 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4175 | delta += cnt - 1; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 4176 | } |
| 4177 | } |
| 4178 | |
| 4179 | if (!ops->convert_ctx_access) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4180 | return 0; |
| 4181 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4182 | insn = env->prog->insnsi + delta; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 4183 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4184 | for (i = 0; i < insn_cnt; i++, insn++) { |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 4185 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
| 4186 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || |
| 4187 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 4188 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4189 | type = BPF_READ; |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 4190 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
| 4191 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || |
| 4192 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 4193 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4194 | type = BPF_WRITE; |
| 4195 | else |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4196 | continue; |
| 4197 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 4198 | if (env->insn_aux_data[i + delta].ptr_type != PTR_TO_CTX) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4199 | continue; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4200 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4201 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4202 | size = BPF_LDST_BYTES(insn); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4203 | |
| 4204 | /* If the read access is a narrower load of the field, |
| 4205 | * convert to a 4/8-byte load, to minimum program type specific |
| 4206 | * convert_ctx_access changes. If conversion is successful, |
| 4207 | * we will apply proper mask to the result. |
| 4208 | */ |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4209 | is_narrower_load = size < ctx_field_size; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4210 | if (is_narrower_load) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4211 | u32 off = insn->off; |
| 4212 | u8 size_code; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4213 | |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4214 | if (type == BPF_WRITE) { |
| 4215 | verbose("bpf verifier narrow ctx access misconfigured\n"); |
| 4216 | return -EINVAL; |
| 4217 | } |
| 4218 | |
| 4219 | size_code = BPF_H; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4220 | if (ctx_field_size == 4) |
| 4221 | size_code = BPF_W; |
| 4222 | else if (ctx_field_size == 8) |
| 4223 | size_code = BPF_DW; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4224 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4225 | insn->off = off & ~(ctx_field_size - 1); |
| 4226 | insn->code = BPF_LDX | BPF_MEM | size_code; |
| 4227 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4228 | |
| 4229 | target_size = 0; |
| 4230 | cnt = ops->convert_ctx_access(type, insn, insn_buf, env->prog, |
| 4231 | &target_size); |
| 4232 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
| 4233 | (ctx_field_size && !target_size)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4234 | verbose("bpf verifier is misconfigured\n"); |
| 4235 | return -EINVAL; |
| 4236 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4237 | |
| 4238 | if (is_narrower_load && size < target_size) { |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4239 | if (ctx_field_size <= 4) |
| 4240 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4241 | (1 << size * 8) - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4242 | else |
| 4243 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4244 | (1 << size * 8) - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4245 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4246 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 4247 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4248 | if (!new_prog) |
| 4249 | return -ENOMEM; |
| 4250 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4251 | delta += cnt - 1; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4252 | |
| 4253 | /* keep walking new program and skip insns we just inserted */ |
| 4254 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4255 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4256 | } |
| 4257 | |
| 4258 | return 0; |
| 4259 | } |
| 4260 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4261 | /* fixup insn->imm field of bpf_call instructions |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 4262 | * and inline eligible helpers as explicit sequence of BPF instructions |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 4263 | * |
| 4264 | * this function is called after eBPF program passed verification |
| 4265 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4266 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 4267 | { |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4268 | struct bpf_prog *prog = env->prog; |
| 4269 | struct bpf_insn *insn = prog->insnsi; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 4270 | const struct bpf_func_proto *fn; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4271 | const int insn_cnt = prog->len; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 4272 | struct bpf_insn insn_buf[16]; |
| 4273 | struct bpf_prog *new_prog; |
| 4274 | struct bpf_map *map_ptr; |
| 4275 | int i, cnt, delta = 0; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 4276 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4277 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 4278 | if (insn->code != (BPF_JMP | BPF_CALL)) |
| 4279 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 4280 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4281 | if (insn->imm == BPF_FUNC_get_route_realm) |
| 4282 | prog->dst_needed = 1; |
| 4283 | if (insn->imm == BPF_FUNC_get_prandom_u32) |
| 4284 | bpf_user_rnd_init_once(); |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4285 | if (insn->imm == BPF_FUNC_tail_call) { |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 4286 | /* If we tail call into other programs, we |
| 4287 | * cannot make any assumptions since they can |
| 4288 | * be replaced dynamically during runtime in |
| 4289 | * the program array. |
| 4290 | */ |
| 4291 | prog->cb_access = 1; |
Alexei Starovoitov | 80a58d0 | 2017-05-30 13:31:30 -0700 | [diff] [blame] | 4292 | env->prog->aux->stack_depth = MAX_BPF_STACK; |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 4293 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4294 | /* mark bpf_tail_call as different opcode to avoid |
| 4295 | * conditional branch in the interpeter for every normal |
| 4296 | * call and to prevent accidental JITing by JIT compiler |
| 4297 | * that doesn't support bpf_tail_call yet |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 4298 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4299 | insn->imm = 0; |
Alexei Starovoitov | 71189fa | 2017-05-30 13:31:27 -0700 | [diff] [blame] | 4300 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4301 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 4302 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 4303 | |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 4304 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
| 4305 | * handlers are currently limited to 64 bit only. |
| 4306 | */ |
| 4307 | if (ebpf_jit_enabled() && BITS_PER_LONG == 64 && |
| 4308 | insn->imm == BPF_FUNC_map_lookup_elem) { |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 4309 | map_ptr = env->insn_aux_data[i + delta].map_ptr; |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 4310 | if (map_ptr == BPF_MAP_PTR_POISON || |
| 4311 | !map_ptr->ops->map_gen_lookup) |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 4312 | goto patch_call_imm; |
| 4313 | |
| 4314 | cnt = map_ptr->ops->map_gen_lookup(map_ptr, insn_buf); |
| 4315 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 4316 | verbose("bpf verifier is misconfigured\n"); |
| 4317 | return -EINVAL; |
| 4318 | } |
| 4319 | |
| 4320 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, |
| 4321 | cnt); |
| 4322 | if (!new_prog) |
| 4323 | return -ENOMEM; |
| 4324 | |
| 4325 | delta += cnt - 1; |
| 4326 | |
| 4327 | /* keep walking new program and skip insns we just inserted */ |
| 4328 | env->prog = prog = new_prog; |
| 4329 | insn = new_prog->insnsi + i + delta; |
| 4330 | continue; |
| 4331 | } |
| 4332 | |
Daniel Borkmann | 109980b | 2017-09-08 00:14:51 +0200 | [diff] [blame] | 4333 | if (insn->imm == BPF_FUNC_redirect_map) { |
Daniel Borkmann | 7c30013 | 2017-09-20 00:44:21 +0200 | [diff] [blame] | 4334 | /* Note, we cannot use prog directly as imm as subsequent |
| 4335 | * rewrites would still change the prog pointer. The only |
| 4336 | * stable address we can use is aux, which also works with |
| 4337 | * prog clones during blinding. |
| 4338 | */ |
| 4339 | u64 addr = (unsigned long)prog->aux; |
Daniel Borkmann | 109980b | 2017-09-08 00:14:51 +0200 | [diff] [blame] | 4340 | struct bpf_insn r4_ld[] = { |
| 4341 | BPF_LD_IMM64(BPF_REG_4, addr), |
| 4342 | *insn, |
| 4343 | }; |
| 4344 | cnt = ARRAY_SIZE(r4_ld); |
| 4345 | |
| 4346 | new_prog = bpf_patch_insn_data(env, i + delta, r4_ld, cnt); |
| 4347 | if (!new_prog) |
| 4348 | return -ENOMEM; |
| 4349 | |
| 4350 | delta += cnt - 1; |
| 4351 | env->prog = prog = new_prog; |
| 4352 | insn = new_prog->insnsi + i + delta; |
| 4353 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 4354 | patch_call_imm: |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4355 | fn = prog->aux->ops->get_func_proto(insn->imm); |
| 4356 | /* all functions that have prototype and verifier allowed |
| 4357 | * programs to call them, must be real in-kernel functions |
| 4358 | */ |
| 4359 | if (!fn->func) { |
| 4360 | verbose("kernel subsystem misconfigured func %s#%d\n", |
| 4361 | func_id_name(insn->imm), insn->imm); |
| 4362 | return -EFAULT; |
| 4363 | } |
| 4364 | insn->imm = fn->func - __bpf_call_base; |
| 4365 | } |
| 4366 | |
| 4367 | return 0; |
| 4368 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 4369 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4370 | static void free_states(struct bpf_verifier_env *env) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4371 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4372 | struct bpf_verifier_state_list *sl, *sln; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4373 | int i; |
| 4374 | |
| 4375 | if (!env->explored_states) |
| 4376 | return; |
| 4377 | |
| 4378 | for (i = 0; i < env->prog->len; i++) { |
| 4379 | sl = env->explored_states[i]; |
| 4380 | |
| 4381 | if (sl) |
| 4382 | while (sl != STATE_LIST_MARK) { |
| 4383 | sln = sl->next; |
| 4384 | kfree(sl); |
| 4385 | sl = sln; |
| 4386 | } |
| 4387 | } |
| 4388 | |
| 4389 | kfree(env->explored_states); |
| 4390 | } |
| 4391 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4392 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 4393 | { |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame^] | 4394 | struct bpf_verifer_log *log = &verifier_log; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4395 | struct bpf_verifier_env *env; |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 4396 | int ret = -EINVAL; |
| 4397 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4398 | /* '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] | 4399 | * allocate/free it every time bpf_check() is called |
| 4400 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4401 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4402 | if (!env) |
| 4403 | return -ENOMEM; |
| 4404 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4405 | env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) * |
| 4406 | (*prog)->len); |
| 4407 | ret = -ENOMEM; |
| 4408 | if (!env->insn_aux_data) |
| 4409 | goto err_free_env; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4410 | env->prog = *prog; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4411 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4412 | /* grab the mutex to protect few globals used by verifier */ |
| 4413 | mutex_lock(&bpf_verifier_lock); |
| 4414 | |
| 4415 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 4416 | /* user requested verbose verifier output |
| 4417 | * and supplied buffer to store the verification trace |
| 4418 | */ |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame^] | 4419 | log->level = attr->log_level; |
| 4420 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 4421 | log->len_total = attr->log_size; |
| 4422 | log->len_used = 0; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4423 | |
| 4424 | ret = -EINVAL; |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame^] | 4425 | /* log attributes have to be sane */ |
| 4426 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 || |
| 4427 | !log->level || !log->ubuf) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4428 | goto err_unlock; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4429 | |
| 4430 | ret = -ENOMEM; |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame^] | 4431 | log->kbuf = vmalloc(log->len_total); |
| 4432 | if (!log->kbuf) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4433 | goto err_unlock; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4434 | } else { |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame^] | 4435 | log->level = 0; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4436 | } |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 4437 | |
| 4438 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
| 4439 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 4440 | env->strict_alignment = true; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4441 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4442 | ret = replace_map_fd_with_map_ptr(env); |
| 4443 | if (ret < 0) |
| 4444 | goto skip_full_check; |
| 4445 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4446 | env->explored_states = kcalloc(env->prog->len, |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4447 | sizeof(struct bpf_verifier_state_list *), |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4448 | GFP_USER); |
| 4449 | ret = -ENOMEM; |
| 4450 | if (!env->explored_states) |
| 4451 | goto skip_full_check; |
| 4452 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4453 | ret = check_cfg(env); |
| 4454 | if (ret < 0) |
| 4455 | goto skip_full_check; |
| 4456 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4457 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); |
| 4458 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4459 | ret = do_check(env); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4460 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4461 | skip_full_check: |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4462 | while (pop_stack(env, NULL) >= 0); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4463 | free_states(env); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4464 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4465 | if (ret == 0) |
| 4466 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 4467 | ret = convert_ctx_accesses(env); |
| 4468 | |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 4469 | if (ret == 0) |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4470 | ret = fixup_bpf_calls(env); |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 4471 | |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame^] | 4472 | if (log->level && bpf_verifier_log_full(log)) { |
| 4473 | BUG_ON(log->len_used >= log->len_total); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4474 | /* verifier log exceeded user supplied buffer */ |
| 4475 | ret = -ENOSPC; |
| 4476 | /* fall through to return what was recorded */ |
| 4477 | } |
| 4478 | |
| 4479 | /* copy verifier log back to user space including trailing zero */ |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame^] | 4480 | if (log->level && copy_to_user(log->ubuf, log->kbuf, |
| 4481 | log->len_used + 1) != 0) { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4482 | ret = -EFAULT; |
| 4483 | goto free_log_buf; |
| 4484 | } |
| 4485 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4486 | if (ret == 0 && env->used_map_cnt) { |
| 4487 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4488 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 4489 | sizeof(env->used_maps[0]), |
| 4490 | GFP_KERNEL); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4491 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4492 | if (!env->prog->aux->used_maps) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4493 | ret = -ENOMEM; |
| 4494 | goto free_log_buf; |
| 4495 | } |
| 4496 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4497 | memcpy(env->prog->aux->used_maps, env->used_maps, |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4498 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4499 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4500 | |
| 4501 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 4502 | * bpf_ld_imm64 instructions |
| 4503 | */ |
| 4504 | convert_pseudo_ld_imm64(env); |
| 4505 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4506 | |
| 4507 | free_log_buf: |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame^] | 4508 | if (log->level) |
| 4509 | vfree(log->kbuf); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4510 | if (!env->prog->aux->used_maps) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4511 | /* if we didn't copy map pointers into bpf_prog_info, release |
| 4512 | * them now. Otherwise free_bpf_prog_info() will release them. |
| 4513 | */ |
| 4514 | release_maps(env); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4515 | *prog = env->prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4516 | err_unlock: |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4517 | mutex_unlock(&bpf_verifier_lock); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4518 | vfree(env->insn_aux_data); |
| 4519 | err_free_env: |
| 4520 | kfree(env); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 4521 | return ret; |
| 4522 | } |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 4523 | |
| 4524 | int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops, |
| 4525 | void *priv) |
| 4526 | { |
| 4527 | struct bpf_verifier_env *env; |
| 4528 | int ret; |
| 4529 | |
| 4530 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
| 4531 | if (!env) |
| 4532 | return -ENOMEM; |
| 4533 | |
| 4534 | env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) * |
| 4535 | prog->len); |
| 4536 | ret = -ENOMEM; |
| 4537 | if (!env->insn_aux_data) |
| 4538 | goto err_free_env; |
| 4539 | env->prog = prog; |
| 4540 | env->analyzer_ops = ops; |
| 4541 | env->analyzer_priv = priv; |
| 4542 | |
| 4543 | /* grab the mutex to protect few globals used by verifier */ |
| 4544 | mutex_lock(&bpf_verifier_lock); |
| 4545 | |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame^] | 4546 | verifier_log.level = 0; |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 4547 | |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 4548 | env->strict_alignment = false; |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 4549 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
| 4550 | env->strict_alignment = true; |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 4551 | |
| 4552 | env->explored_states = kcalloc(env->prog->len, |
| 4553 | sizeof(struct bpf_verifier_state_list *), |
| 4554 | GFP_KERNEL); |
| 4555 | ret = -ENOMEM; |
| 4556 | if (!env->explored_states) |
| 4557 | goto skip_full_check; |
| 4558 | |
| 4559 | ret = check_cfg(env); |
| 4560 | if (ret < 0) |
| 4561 | goto skip_full_check; |
| 4562 | |
| 4563 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); |
| 4564 | |
| 4565 | ret = do_check(env); |
| 4566 | |
| 4567 | skip_full_check: |
| 4568 | while (pop_stack(env, NULL) >= 0); |
| 4569 | free_states(env); |
| 4570 | |
| 4571 | mutex_unlock(&bpf_verifier_lock); |
| 4572 | vfree(env->insn_aux_data); |
| 4573 | err_free_env: |
| 4574 | kfree(env); |
| 4575 | return ret; |
| 4576 | } |
| 4577 | EXPORT_SYMBOL_GPL(bpf_analyzer); |