blob: e807bda7fe295ae5d44ad2389187d46a07d25071 [file] [log] [blame]
Alexei Starovoitov51580e72014-09-26 00:17:02 -07001/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002 * Copyright (c) 2016 Facebook
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003 *
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 Kicinski58e2af8b2016-09-21 11:43:57 +010017#include <linux/bpf_verifier.h>
Alexei Starovoitov51580e72014-09-26 00:17:02 -070018#include <linux/filter.h>
19#include <net/netlink.h>
20#include <linux/file.h>
21#include <linux/vmalloc.h>
Thomas Grafebb676d2016-10-27 11:23:51 +020022#include <linux/stringify.h>
Alexei Starovoitov51580e72014-09-26 00:17:02 -070023
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -070024#include "disasm.h"
25
Jakub Kicinski00176a32017-10-16 16:40:54 -070026static const struct bpf_verifier_ops * const bpf_verifier_ops[] = {
27#define BPF_PROG_TYPE(_id, _name) \
28 [_id] = & _name ## _verifier_ops,
29#define BPF_MAP_TYPE(_id, _ops)
30#include <linux/bpf_types.h>
31#undef BPF_PROG_TYPE
32#undef BPF_MAP_TYPE
33};
34
Alexei Starovoitov51580e72014-09-26 00:17:02 -070035/* bpf_check() is a static code analyzer that walks eBPF program
36 * instruction by instruction and updates register/stack state.
37 * All paths of conditional branches are analyzed until 'bpf_exit' insn.
38 *
39 * The first pass is depth-first-search to check that the program is a DAG.
40 * It rejects the following programs:
41 * - larger than BPF_MAXINSNS insns
42 * - if loop is present (detected via back-edge)
43 * - unreachable insns exist (shouldn't be a forest. program = one function)
44 * - out of bounds or malformed jumps
45 * The second pass is all possible path descent from the 1st insn.
46 * Since it's analyzing all pathes through the program, the length of the
Gary Lineba38a92017-03-01 16:25:51 +080047 * analysis is limited to 64k insn, which may be hit even if total number of
Alexei Starovoitov51580e72014-09-26 00:17:02 -070048 * insn is less then 4K, but there are too many branches that change stack/regs.
49 * Number of 'branches to be analyzed' is limited to 1k
50 *
51 * On entry to each instruction, each register has a type, and the instruction
52 * changes the types of the registers depending on instruction semantics.
53 * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is
54 * copied to R1.
55 *
56 * All registers are 64-bit.
57 * R0 - return register
58 * R1-R5 argument passing registers
59 * R6-R9 callee saved registers
60 * R10 - frame pointer read-only
61 *
62 * At the start of BPF program the register R1 contains a pointer to bpf_context
63 * and has type PTR_TO_CTX.
64 *
65 * Verifier tracks arithmetic operations on pointers in case:
66 * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
67 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20),
68 * 1st insn copies R10 (which has FRAME_PTR) type into R1
69 * and 2nd arithmetic instruction is pattern matched to recognize
70 * that it wants to construct a pointer to some element within stack.
71 * So after 2nd insn, the register R1 has type PTR_TO_STACK
72 * (and -20 constant is saved for further stack bounds checking).
73 * Meaning that this reg is a pointer to stack plus known immediate constant.
74 *
Edward Creef1174f72017-08-07 15:26:19 +010075 * Most of the time the registers have SCALAR_VALUE type, which
Alexei Starovoitov51580e72014-09-26 00:17:02 -070076 * means the register has some value, but it's not a valid pointer.
Edward Creef1174f72017-08-07 15:26:19 +010077 * (like pointer plus pointer becomes SCALAR_VALUE type)
Alexei Starovoitov51580e72014-09-26 00:17:02 -070078 *
79 * When verifier sees load or store instructions the type of base register
Edward Creef1174f72017-08-07 15:26:19 +010080 * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK. These are three pointer
Alexei Starovoitov51580e72014-09-26 00:17:02 -070081 * types recognized by check_mem_access() function.
82 *
83 * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value'
84 * and the range of [ptr, ptr + map's value_size) is accessible.
85 *
86 * registers used to pass values to function calls are checked against
87 * function argument constraints.
88 *
89 * ARG_PTR_TO_MAP_KEY is one of such argument constraints.
90 * It means that the register type passed to this function must be
91 * PTR_TO_STACK and it will be used inside the function as
92 * 'pointer to map element key'
93 *
94 * For example the argument constraints for bpf_map_lookup_elem():
95 * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
96 * .arg1_type = ARG_CONST_MAP_PTR,
97 * .arg2_type = ARG_PTR_TO_MAP_KEY,
98 *
99 * ret_type says that this function returns 'pointer to map elem value or null'
100 * function expects 1st argument to be a const pointer to 'struct bpf_map' and
101 * 2nd argument should be a pointer to stack, which will be used inside
102 * the helper function as a pointer to map element key.
103 *
104 * On the kernel side the helper function looks like:
105 * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
106 * {
107 * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
108 * void *key = (void *) (unsigned long) r2;
109 * void *value;
110 *
111 * here kernel can access 'key' and 'map' pointers safely, knowing that
112 * [key, key + map->key_size) bytes are valid and were initialized on
113 * the stack of eBPF program.
114 * }
115 *
116 * Corresponding eBPF program may look like:
117 * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR
118 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK
119 * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP
120 * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
121 * here verifier looks at prototype of map_lookup_elem() and sees:
122 * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok,
123 * Now verifier knows that this map has key of R1->map_ptr->key_size bytes
124 *
125 * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far,
126 * Now verifier checks that [R2, R2 + map's key_size) are within stack limits
127 * and were initialized prior to this call.
128 * If it's ok, then verifier allows this BPF_CALL insn and looks at
129 * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets
130 * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function
131 * returns ether pointer to map value or NULL.
132 *
133 * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off'
134 * insn, the register holding that pointer in the true branch changes state to
135 * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false
136 * branch. See check_cond_jmp_op().
137 *
138 * After the call R0 is set to return type of the function and registers R1-R5
139 * are set to NOT_INIT to indicate that they are no longer readable.
140 */
141
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700142/* verifier_state + insn_idx are pushed to stack when branch is encountered */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100143struct bpf_verifier_stack_elem {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700144 /* verifer state is 'st'
145 * before processing instruction 'insn_idx'
146 * and after processing instruction 'prev_insn_idx'
147 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100148 struct bpf_verifier_state st;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700149 int insn_idx;
150 int prev_insn_idx;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100151 struct bpf_verifier_stack_elem *next;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700152};
153
Edward Cree8e17c1b2017-08-07 15:30:30 +0100154#define BPF_COMPLEXITY_LIMIT_INSNS 131072
Daniel Borkmann07016152016-04-05 22:33:17 +0200155#define BPF_COMPLEXITY_LIMIT_STACK 1024
156
Martin KaFai Laufad73a12017-03-22 10:00:32 -0700157#define BPF_MAP_PTR_POISON ((void *)0xeB9F + POISON_POINTER_DELTA)
158
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200159struct bpf_call_arg_meta {
160 struct bpf_map *map_ptr;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200161 bool raw_mode;
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200162 bool pkt_access;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200163 int regno;
164 int access_size;
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200165};
166
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700167static DEFINE_MUTEX(bpf_verifier_lock);
168
169/* log_level controls verbosity level of eBPF verifier.
170 * verbose() is used to dump the verification trace to the log, so the user
171 * can figure out what's wrong with the program
172 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700173static __printf(2, 3) void verbose(struct bpf_verifier_env *env,
174 const char *fmt, ...)
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700175{
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700176 struct bpf_verifer_log *log = &env->log;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700177 unsigned int n;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700178 va_list args;
179
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700180 if (!log->level || !log->ubuf || bpf_verifier_log_full(log))
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700181 return;
182
183 va_start(args, fmt);
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700184 n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700185 va_end(args);
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700186
187 WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1,
188 "verifier log line truncated - local buffer too short\n");
189
190 n = min(log->len_total - log->len_used - 1, n);
191 log->kbuf[n] = '\0';
192
193 if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1))
194 log->len_used += n;
195 else
196 log->ubuf = NULL;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700197}
198
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200199static bool type_is_pkt_pointer(enum bpf_reg_type type)
200{
201 return type == PTR_TO_PACKET ||
202 type == PTR_TO_PACKET_META;
203}
204
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700205/* string representation of 'enum bpf_reg_type' */
206static const char * const reg_type_str[] = {
207 [NOT_INIT] = "?",
Edward Creef1174f72017-08-07 15:26:19 +0100208 [SCALAR_VALUE] = "inv",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700209 [PTR_TO_CTX] = "ctx",
210 [CONST_PTR_TO_MAP] = "map_ptr",
211 [PTR_TO_MAP_VALUE] = "map_value",
212 [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700213 [PTR_TO_STACK] = "fp",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700214 [PTR_TO_PACKET] = "pkt",
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200215 [PTR_TO_PACKET_META] = "pkt_meta",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700216 [PTR_TO_PACKET_END] = "pkt_end",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700217};
218
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800219static void print_liveness(struct bpf_verifier_env *env,
220 enum bpf_reg_liveness live)
221{
222 if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN))
223 verbose(env, "_");
224 if (live & REG_LIVE_READ)
225 verbose(env, "r");
226 if (live & REG_LIVE_WRITTEN)
227 verbose(env, "w");
228}
229
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700230static void print_verifier_state(struct bpf_verifier_env *env,
231 struct bpf_verifier_state *state)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700232{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100233 struct bpf_reg_state *reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700234 enum bpf_reg_type t;
235 int i;
236
237 for (i = 0; i < MAX_BPF_REG; i++) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700238 reg = &state->regs[i];
239 t = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700240 if (t == NOT_INIT)
241 continue;
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800242 verbose(env, " R%d", i);
243 print_liveness(env, reg->live);
244 verbose(env, "=%s", reg_type_str[t]);
Edward Creef1174f72017-08-07 15:26:19 +0100245 if ((t == SCALAR_VALUE || t == PTR_TO_STACK) &&
246 tnum_is_const(reg->var_off)) {
247 /* reg->off should be 0 for SCALAR_VALUE */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700248 verbose(env, "%lld", reg->var_off.value + reg->off);
Edward Creef1174f72017-08-07 15:26:19 +0100249 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700250 verbose(env, "(id=%d", reg->id);
Edward Creef1174f72017-08-07 15:26:19 +0100251 if (t != SCALAR_VALUE)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700252 verbose(env, ",off=%d", reg->off);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200253 if (type_is_pkt_pointer(t))
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700254 verbose(env, ",r=%d", reg->range);
Edward Creef1174f72017-08-07 15:26:19 +0100255 else if (t == CONST_PTR_TO_MAP ||
256 t == PTR_TO_MAP_VALUE ||
257 t == PTR_TO_MAP_VALUE_OR_NULL)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700258 verbose(env, ",ks=%d,vs=%d",
Edward Creef1174f72017-08-07 15:26:19 +0100259 reg->map_ptr->key_size,
260 reg->map_ptr->value_size);
Edward Cree7d1238f2017-08-07 15:26:56 +0100261 if (tnum_is_const(reg->var_off)) {
262 /* Typically an immediate SCALAR_VALUE, but
263 * could be a pointer whose offset is too big
264 * for reg->off
265 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700266 verbose(env, ",imm=%llx", reg->var_off.value);
Edward Cree7d1238f2017-08-07 15:26:56 +0100267 } else {
268 if (reg->smin_value != reg->umin_value &&
269 reg->smin_value != S64_MIN)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700270 verbose(env, ",smin_value=%lld",
Edward Cree7d1238f2017-08-07 15:26:56 +0100271 (long long)reg->smin_value);
272 if (reg->smax_value != reg->umax_value &&
273 reg->smax_value != S64_MAX)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700274 verbose(env, ",smax_value=%lld",
Edward Cree7d1238f2017-08-07 15:26:56 +0100275 (long long)reg->smax_value);
276 if (reg->umin_value != 0)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700277 verbose(env, ",umin_value=%llu",
Edward Cree7d1238f2017-08-07 15:26:56 +0100278 (unsigned long long)reg->umin_value);
279 if (reg->umax_value != U64_MAX)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700280 verbose(env, ",umax_value=%llu",
Edward Cree7d1238f2017-08-07 15:26:56 +0100281 (unsigned long long)reg->umax_value);
282 if (!tnum_is_unknown(reg->var_off)) {
283 char tn_buf[48];
Edward Creef1174f72017-08-07 15:26:19 +0100284
Edward Cree7d1238f2017-08-07 15:26:56 +0100285 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700286 verbose(env, ",var_off=%s", tn_buf);
Edward Cree7d1238f2017-08-07 15:26:56 +0100287 }
Edward Creef1174f72017-08-07 15:26:19 +0100288 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700289 verbose(env, ")");
Edward Creef1174f72017-08-07 15:26:19 +0100290 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700291 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700292 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800293 if (state->stack[i].slot_type[0] == STACK_SPILL) {
294 verbose(env, " fp%d",
295 (-i - 1) * BPF_REG_SIZE);
296 print_liveness(env, state->stack[i].spilled_ptr.live);
297 verbose(env, "=%s",
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700298 reg_type_str[state->stack[i].spilled_ptr.type]);
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800299 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700300 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700301 verbose(env, "\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700302}
303
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700304static int copy_stack_state(struct bpf_verifier_state *dst,
305 const struct bpf_verifier_state *src)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700306{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700307 if (!src->stack)
308 return 0;
309 if (WARN_ON_ONCE(dst->allocated_stack < src->allocated_stack)) {
310 /* internal bug, make state invalid to reject the program */
311 memset(dst, 0, sizeof(*dst));
312 return -EFAULT;
313 }
314 memcpy(dst->stack, src->stack,
315 sizeof(*src->stack) * (src->allocated_stack / BPF_REG_SIZE));
316 return 0;
317}
318
319/* do_check() starts with zero-sized stack in struct bpf_verifier_state to
320 * make it consume minimal amount of memory. check_stack_write() access from
321 * the program calls into realloc_verifier_state() to grow the stack size.
322 * Note there is a non-zero 'parent' pointer inside bpf_verifier_state
323 * which this function copies over. It points to previous bpf_verifier_state
324 * which is never reallocated
325 */
326static int realloc_verifier_state(struct bpf_verifier_state *state, int size,
327 bool copy_old)
328{
329 u32 old_size = state->allocated_stack;
330 struct bpf_stack_state *new_stack;
331 int slot = size / BPF_REG_SIZE;
332
333 if (size <= old_size || !size) {
334 if (copy_old)
335 return 0;
336 state->allocated_stack = slot * BPF_REG_SIZE;
337 if (!size && old_size) {
338 kfree(state->stack);
339 state->stack = NULL;
340 }
341 return 0;
342 }
343 new_stack = kmalloc_array(slot, sizeof(struct bpf_stack_state),
344 GFP_KERNEL);
345 if (!new_stack)
346 return -ENOMEM;
347 if (copy_old) {
348 if (state->stack)
349 memcpy(new_stack, state->stack,
350 sizeof(*new_stack) * (old_size / BPF_REG_SIZE));
351 memset(new_stack + old_size / BPF_REG_SIZE, 0,
352 sizeof(*new_stack) * (size - old_size) / BPF_REG_SIZE);
353 }
354 state->allocated_stack = slot * BPF_REG_SIZE;
355 kfree(state->stack);
356 state->stack = new_stack;
357 return 0;
358}
359
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700360static void free_verifier_state(struct bpf_verifier_state *state,
361 bool free_self)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700362{
363 kfree(state->stack);
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700364 if (free_self)
365 kfree(state);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700366}
367
368/* copy verifier state from src to dst growing dst stack space
369 * when necessary to accommodate larger src stack
370 */
371static int copy_verifier_state(struct bpf_verifier_state *dst,
372 const struct bpf_verifier_state *src)
373{
374 int err;
375
376 err = realloc_verifier_state(dst, src->allocated_stack, false);
377 if (err)
378 return err;
379 memcpy(dst, src, offsetof(struct bpf_verifier_state, allocated_stack));
380 return copy_stack_state(dst, src);
381}
382
383static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx,
384 int *insn_idx)
385{
386 struct bpf_verifier_state *cur = env->cur_state;
387 struct bpf_verifier_stack_elem *elem, *head = env->head;
388 int err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700389
390 if (env->head == NULL)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700391 return -ENOENT;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700392
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700393 if (cur) {
394 err = copy_verifier_state(cur, &head->st);
395 if (err)
396 return err;
397 }
398 if (insn_idx)
399 *insn_idx = head->insn_idx;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700400 if (prev_insn_idx)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700401 *prev_insn_idx = head->prev_insn_idx;
402 elem = head->next;
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700403 free_verifier_state(&head->st, false);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700404 kfree(head);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700405 env->head = elem;
406 env->stack_size--;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700407 return 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700408}
409
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100410static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,
411 int insn_idx, int prev_insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700412{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700413 struct bpf_verifier_state *cur = env->cur_state;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100414 struct bpf_verifier_stack_elem *elem;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700415 int err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700416
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700417 elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700418 if (!elem)
419 goto err;
420
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700421 elem->insn_idx = insn_idx;
422 elem->prev_insn_idx = prev_insn_idx;
423 elem->next = env->head;
424 env->head = elem;
425 env->stack_size++;
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700426 err = copy_verifier_state(&elem->st, cur);
427 if (err)
428 goto err;
Daniel Borkmann07016152016-04-05 22:33:17 +0200429 if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700430 verbose(env, "BPF program is too complex\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700431 goto err;
432 }
433 return &elem->st;
434err:
435 /* pop all elements and return */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700436 while (!pop_stack(env, NULL, NULL));
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700437 return NULL;
438}
439
440#define CALLER_SAVED_REGS 6
441static const int caller_saved[CALLER_SAVED_REGS] = {
442 BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
443};
444
Edward Creef1174f72017-08-07 15:26:19 +0100445static void __mark_reg_not_init(struct bpf_reg_state *reg);
446
Edward Creeb03c9f92017-08-07 15:26:36 +0100447/* Mark the unknown part of a register (variable offset or scalar value) as
448 * known to have the value @imm.
449 */
450static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
451{
452 reg->id = 0;
453 reg->var_off = tnum_const(imm);
454 reg->smin_value = (s64)imm;
455 reg->smax_value = (s64)imm;
456 reg->umin_value = imm;
457 reg->umax_value = imm;
458}
459
Edward Creef1174f72017-08-07 15:26:19 +0100460/* Mark the 'variable offset' part of a register as zero. This should be
461 * used only on registers holding a pointer type.
462 */
463static void __mark_reg_known_zero(struct bpf_reg_state *reg)
464{
Edward Creeb03c9f92017-08-07 15:26:36 +0100465 __mark_reg_known(reg, 0);
Edward Creef1174f72017-08-07 15:26:19 +0100466}
467
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700468static void mark_reg_known_zero(struct bpf_verifier_env *env,
469 struct bpf_reg_state *regs, u32 regno)
Edward Creef1174f72017-08-07 15:26:19 +0100470{
471 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700472 verbose(env, "mark_reg_known_zero(regs, %u)\n", regno);
Edward Creef1174f72017-08-07 15:26:19 +0100473 /* Something bad happened, let's kill all regs */
474 for (regno = 0; regno < MAX_BPF_REG; regno++)
475 __mark_reg_not_init(regs + regno);
476 return;
477 }
478 __mark_reg_known_zero(regs + regno);
479}
480
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200481static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg)
482{
483 return type_is_pkt_pointer(reg->type);
484}
485
486static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg)
487{
488 return reg_is_pkt_pointer(reg) ||
489 reg->type == PTR_TO_PACKET_END;
490}
491
492/* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */
493static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg,
494 enum bpf_reg_type which)
495{
496 /* The register can already have a range from prior markings.
497 * This is fine as long as it hasn't been advanced from its
498 * origin.
499 */
500 return reg->type == which &&
501 reg->id == 0 &&
502 reg->off == 0 &&
503 tnum_equals_const(reg->var_off, 0);
504}
505
Edward Creeb03c9f92017-08-07 15:26:36 +0100506/* Attempts to improve min/max values based on var_off information */
507static void __update_reg_bounds(struct bpf_reg_state *reg)
508{
509 /* min signed is max(sign bit) | min(other bits) */
510 reg->smin_value = max_t(s64, reg->smin_value,
511 reg->var_off.value | (reg->var_off.mask & S64_MIN));
512 /* max signed is min(sign bit) | max(other bits) */
513 reg->smax_value = min_t(s64, reg->smax_value,
514 reg->var_off.value | (reg->var_off.mask & S64_MAX));
515 reg->umin_value = max(reg->umin_value, reg->var_off.value);
516 reg->umax_value = min(reg->umax_value,
517 reg->var_off.value | reg->var_off.mask);
518}
519
520/* Uses signed min/max values to inform unsigned, and vice-versa */
521static void __reg_deduce_bounds(struct bpf_reg_state *reg)
522{
523 /* Learn sign from signed bounds.
524 * If we cannot cross the sign boundary, then signed and unsigned bounds
525 * are the same, so combine. This works even in the negative case, e.g.
526 * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff.
527 */
528 if (reg->smin_value >= 0 || reg->smax_value < 0) {
529 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
530 reg->umin_value);
531 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
532 reg->umax_value);
533 return;
534 }
535 /* Learn sign from unsigned bounds. Signed bounds cross the sign
536 * boundary, so we must be careful.
537 */
538 if ((s64)reg->umax_value >= 0) {
539 /* Positive. We can't learn anything from the smin, but smax
540 * is positive, hence safe.
541 */
542 reg->smin_value = reg->umin_value;
543 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
544 reg->umax_value);
545 } else if ((s64)reg->umin_value < 0) {
546 /* Negative. We can't learn anything from the smax, but smin
547 * is negative, hence safe.
548 */
549 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
550 reg->umin_value);
551 reg->smax_value = reg->umax_value;
552 }
553}
554
555/* Attempts to improve var_off based on unsigned min/max information */
556static void __reg_bound_offset(struct bpf_reg_state *reg)
557{
558 reg->var_off = tnum_intersect(reg->var_off,
559 tnum_range(reg->umin_value,
560 reg->umax_value));
561}
562
563/* Reset the min/max bounds of a register */
564static void __mark_reg_unbounded(struct bpf_reg_state *reg)
565{
566 reg->smin_value = S64_MIN;
567 reg->smax_value = S64_MAX;
568 reg->umin_value = 0;
569 reg->umax_value = U64_MAX;
570}
571
Edward Creef1174f72017-08-07 15:26:19 +0100572/* Mark a register as having a completely unknown (scalar) value. */
573static void __mark_reg_unknown(struct bpf_reg_state *reg)
574{
575 reg->type = SCALAR_VALUE;
576 reg->id = 0;
577 reg->off = 0;
578 reg->var_off = tnum_unknown;
Edward Creeb03c9f92017-08-07 15:26:36 +0100579 __mark_reg_unbounded(reg);
Edward Creef1174f72017-08-07 15:26:19 +0100580}
581
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700582static void mark_reg_unknown(struct bpf_verifier_env *env,
583 struct bpf_reg_state *regs, u32 regno)
Edward Creef1174f72017-08-07 15:26:19 +0100584{
585 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700586 verbose(env, "mark_reg_unknown(regs, %u)\n", regno);
Alexei Starovoitov19ceb412017-11-30 21:31:37 -0800587 /* Something bad happened, let's kill all regs except FP */
588 for (regno = 0; regno < BPF_REG_FP; regno++)
Edward Creef1174f72017-08-07 15:26:19 +0100589 __mark_reg_not_init(regs + regno);
590 return;
591 }
592 __mark_reg_unknown(regs + regno);
593}
594
595static void __mark_reg_not_init(struct bpf_reg_state *reg)
596{
597 __mark_reg_unknown(reg);
598 reg->type = NOT_INIT;
599}
600
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700601static void mark_reg_not_init(struct bpf_verifier_env *env,
602 struct bpf_reg_state *regs, u32 regno)
Daniel Borkmanna9789ef2017-05-25 01:05:06 +0200603{
Edward Creef1174f72017-08-07 15:26:19 +0100604 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700605 verbose(env, "mark_reg_not_init(regs, %u)\n", regno);
Alexei Starovoitov19ceb412017-11-30 21:31:37 -0800606 /* Something bad happened, let's kill all regs except FP */
607 for (regno = 0; regno < BPF_REG_FP; regno++)
Edward Creef1174f72017-08-07 15:26:19 +0100608 __mark_reg_not_init(regs + regno);
609 return;
610 }
611 __mark_reg_not_init(regs + regno);
Daniel Borkmanna9789ef2017-05-25 01:05:06 +0200612}
613
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700614static void init_reg_state(struct bpf_verifier_env *env,
615 struct bpf_reg_state *regs)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700616{
617 int i;
618
Edward Creedc503a82017-08-15 20:34:35 +0100619 for (i = 0; i < MAX_BPF_REG; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700620 mark_reg_not_init(env, regs, i);
Edward Creedc503a82017-08-15 20:34:35 +0100621 regs[i].live = REG_LIVE_NONE;
622 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700623
624 /* frame pointer */
Edward Creef1174f72017-08-07 15:26:19 +0100625 regs[BPF_REG_FP].type = PTR_TO_STACK;
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700626 mark_reg_known_zero(env, regs, BPF_REG_FP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700627
628 /* 1st arg to a function */
629 regs[BPF_REG_1].type = PTR_TO_CTX;
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700630 mark_reg_known_zero(env, regs, BPF_REG_1);
Daniel Borkmann6760bf22016-12-18 01:52:59 +0100631}
632
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700633enum reg_arg_type {
634 SRC_OP, /* register is used as source operand */
635 DST_OP, /* register is used as destination operand */
636 DST_OP_NO_MARK /* same as above, check only, don't mark */
637};
638
Edward Creedc503a82017-08-15 20:34:35 +0100639static void mark_reg_read(const struct bpf_verifier_state *state, u32 regno)
640{
641 struct bpf_verifier_state *parent = state->parent;
642
Alexei Starovoitov8fe2d6c2017-10-05 16:20:56 -0700643 if (regno == BPF_REG_FP)
644 /* We don't need to worry about FP liveness because it's read-only */
645 return;
646
Edward Creedc503a82017-08-15 20:34:35 +0100647 while (parent) {
648 /* if read wasn't screened by an earlier write ... */
649 if (state->regs[regno].live & REG_LIVE_WRITTEN)
650 break;
651 /* ... then we depend on parent's value */
652 parent->regs[regno].live |= REG_LIVE_READ;
653 state = parent;
654 parent = state->parent;
655 }
656}
657
658static int check_reg_arg(struct bpf_verifier_env *env, u32 regno,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700659 enum reg_arg_type t)
660{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700661 struct bpf_reg_state *regs = env->cur_state->regs;
Edward Creedc503a82017-08-15 20:34:35 +0100662
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700663 if (regno >= MAX_BPF_REG) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700664 verbose(env, "R%d is invalid\n", regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700665 return -EINVAL;
666 }
667
668 if (t == SRC_OP) {
669 /* check whether register used as source operand can be read */
670 if (regs[regno].type == NOT_INIT) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700671 verbose(env, "R%d !read_ok\n", regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700672 return -EACCES;
673 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700674 mark_reg_read(env->cur_state, regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700675 } else {
676 /* check whether register used as dest operand can be written to */
677 if (regno == BPF_REG_FP) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700678 verbose(env, "frame pointer is read only\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700679 return -EACCES;
680 }
Edward Creedc503a82017-08-15 20:34:35 +0100681 regs[regno].live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700682 if (t == DST_OP)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700683 mark_reg_unknown(env, regs, regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700684 }
685 return 0;
686}
687
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700688static bool is_spillable_regtype(enum bpf_reg_type type)
689{
690 switch (type) {
691 case PTR_TO_MAP_VALUE:
692 case PTR_TO_MAP_VALUE_OR_NULL:
693 case PTR_TO_STACK:
694 case PTR_TO_CTX:
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700695 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200696 case PTR_TO_PACKET_META:
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700697 case PTR_TO_PACKET_END:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700698 case CONST_PTR_TO_MAP:
699 return true;
700 default:
701 return false;
702 }
703}
704
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700705/* check_stack_read/write functions track spill/fill of registers,
706 * stack boundary and alignment are checked in check_mem_access()
707 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700708static int check_stack_write(struct bpf_verifier_env *env,
709 struct bpf_verifier_state *state, int off,
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100710 int size, int value_regno)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700711{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700712 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err;
713
714 err = realloc_verifier_state(state, round_up(slot + 1, BPF_REG_SIZE),
715 true);
716 if (err)
717 return err;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700718 /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
719 * so it's aligned access and [off, off + size) are within stack limits
720 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700721 if (!env->allow_ptr_leaks &&
722 state->stack[spi].slot_type[0] == STACK_SPILL &&
723 size != BPF_REG_SIZE) {
724 verbose(env, "attempt to corrupt spilled pointer on stack\n");
725 return -EACCES;
726 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700727
728 if (value_regno >= 0 &&
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700729 is_spillable_regtype(state->regs[value_regno].type)) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700730
731 /* register containing pointer is being spilled into stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700732 if (size != BPF_REG_SIZE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700733 verbose(env, "invalid size of register spill\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700734 return -EACCES;
735 }
736
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700737 /* save register state */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700738 state->stack[spi].spilled_ptr = state->regs[value_regno];
739 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700740
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700741 for (i = 0; i < BPF_REG_SIZE; i++)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700742 state->stack[spi].slot_type[i] = STACK_SPILL;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700743 } else {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700744 /* regular write of data into stack */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700745 state->stack[spi].spilled_ptr = (struct bpf_reg_state) {};
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700746
747 for (i = 0; i < size; i++)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700748 state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] =
749 STACK_MISC;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700750 }
751 return 0;
752}
753
Edward Creedc503a82017-08-15 20:34:35 +0100754static void mark_stack_slot_read(const struct bpf_verifier_state *state, int slot)
755{
756 struct bpf_verifier_state *parent = state->parent;
757
758 while (parent) {
759 /* if read wasn't screened by an earlier write ... */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700760 if (state->stack[slot].spilled_ptr.live & REG_LIVE_WRITTEN)
Edward Creedc503a82017-08-15 20:34:35 +0100761 break;
762 /* ... then we depend on parent's value */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700763 parent->stack[slot].spilled_ptr.live |= REG_LIVE_READ;
Edward Creedc503a82017-08-15 20:34:35 +0100764 state = parent;
765 parent = state->parent;
766 }
767}
768
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700769static int check_stack_read(struct bpf_verifier_env *env,
770 struct bpf_verifier_state *state, int off, int size,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700771 int value_regno)
772{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700773 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE;
774 u8 *stype;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700775
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700776 if (state->allocated_stack <= slot) {
777 verbose(env, "invalid read from stack off %d+0 size %d\n",
778 off, size);
779 return -EACCES;
780 }
781 stype = state->stack[spi].slot_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700782
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700783 if (stype[0] == STACK_SPILL) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700784 if (size != BPF_REG_SIZE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700785 verbose(env, "invalid size of register spill\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700786 return -EACCES;
787 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700788 for (i = 1; i < BPF_REG_SIZE; i++) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700789 if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700790 verbose(env, "corrupted spill memory\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700791 return -EACCES;
792 }
793 }
794
Edward Creedc503a82017-08-15 20:34:35 +0100795 if (value_regno >= 0) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700796 /* restore register state from stack */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700797 state->regs[value_regno] = state->stack[spi].spilled_ptr;
Alexei Starovoitov2f18f622017-11-30 21:31:38 -0800798 /* mark reg as written since spilled pointer state likely
799 * has its liveness marks cleared by is_state_visited()
800 * which resets stack/reg liveness for state transitions
801 */
802 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
Edward Creedc503a82017-08-15 20:34:35 +0100803 mark_stack_slot_read(state, spi);
804 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700805 return 0;
806 } else {
807 for (i = 0; i < size; i++) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700808 if (stype[(slot - i) % BPF_REG_SIZE] != STACK_MISC) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700809 verbose(env, "invalid read from stack off %d+%d size %d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700810 off, i, size);
811 return -EACCES;
812 }
813 }
814 if (value_regno >= 0)
815 /* have read misc data from the stack */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700816 mark_reg_unknown(env, state->regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700817 return 0;
818 }
819}
820
821/* check read/write into map element returned by bpf_map_lookup_elem() */
Edward Creef1174f72017-08-07 15:26:19 +0100822static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
Yonghong Song9fd29c02017-11-12 14:49:09 -0800823 int size, bool zero_size_allowed)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700824{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700825 struct bpf_reg_state *regs = cur_regs(env);
826 struct bpf_map *map = regs[regno].map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700827
Yonghong Song9fd29c02017-11-12 14:49:09 -0800828 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
829 off + size > map->value_size) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700830 verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700831 map->value_size, off, size);
832 return -EACCES;
833 }
834 return 0;
835}
836
Edward Creef1174f72017-08-07 15:26:19 +0100837/* check read/write into a map element with possible variable offset */
838static int check_map_access(struct bpf_verifier_env *env, u32 regno,
Yonghong Song9fd29c02017-11-12 14:49:09 -0800839 int off, int size, bool zero_size_allowed)
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -0800840{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700841 struct bpf_verifier_state *state = env->cur_state;
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -0800842 struct bpf_reg_state *reg = &state->regs[regno];
843 int err;
844
Edward Creef1174f72017-08-07 15:26:19 +0100845 /* We may have adjusted the register to this map value, so we
846 * need to try adding each of min_value and max_value to off
847 * to make sure our theoretical access will be safe.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -0800848 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700849 if (env->log.level)
850 print_verifier_state(env, state);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -0800851 /* The minimum value is only important with signed
852 * comparisons where we can't assume the floor of a
853 * value is 0. If we are using signed variables for our
854 * index'es we need to make sure that whatever we use
855 * will have a set floor within our range.
856 */
Edward Creeb03c9f92017-08-07 15:26:36 +0100857 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700858 verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -0800859 regno);
860 return -EACCES;
861 }
Yonghong Song9fd29c02017-11-12 14:49:09 -0800862 err = __check_map_access(env, regno, reg->smin_value + off, size,
863 zero_size_allowed);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -0800864 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700865 verbose(env, "R%d min value is outside of the array range\n",
866 regno);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -0800867 return err;
868 }
869
Edward Creeb03c9f92017-08-07 15:26:36 +0100870 /* If we haven't set a max value then we need to bail since we can't be
871 * sure we won't do bad things.
872 * If reg->umax_value + off could overflow, treat that as unbounded too.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -0800873 */
Edward Creeb03c9f92017-08-07 15:26:36 +0100874 if (reg->umax_value >= BPF_MAX_VAR_OFF) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700875 verbose(env, "R%d unbounded memory access, make sure to bounds check any array access into a map\n",
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -0800876 regno);
877 return -EACCES;
878 }
Yonghong Song9fd29c02017-11-12 14:49:09 -0800879 err = __check_map_access(env, regno, reg->umax_value + off, size,
880 zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +0100881 if (err)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700882 verbose(env, "R%d max value is outside of the array range\n",
883 regno);
Edward Creef1174f72017-08-07 15:26:19 +0100884 return err;
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -0800885}
886
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700887#define MAX_PACKET_OFF 0xffff
888
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100889static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
Thomas Graf3a0af8f2016-11-30 17:10:10 +0100890 const struct bpf_call_arg_meta *meta,
891 enum bpf_access_type t)
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700892{
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200893 switch (env->prog->type) {
Thomas Graf3a0af8f2016-11-30 17:10:10 +0100894 case BPF_PROG_TYPE_LWT_IN:
895 case BPF_PROG_TYPE_LWT_OUT:
896 /* dst_input() and dst_output() can't write for now */
897 if (t == BPF_WRITE)
898 return false;
Alexander Alemayhu7e57fbb2017-02-14 00:02:35 +0100899 /* fallthrough */
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200900 case BPF_PROG_TYPE_SCHED_CLS:
901 case BPF_PROG_TYPE_SCHED_ACT:
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700902 case BPF_PROG_TYPE_XDP:
Thomas Graf3a0af8f2016-11-30 17:10:10 +0100903 case BPF_PROG_TYPE_LWT_XMIT:
John Fastabend8a31db52017-08-15 22:33:09 -0700904 case BPF_PROG_TYPE_SK_SKB:
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200905 if (meta)
906 return meta->pkt_access;
907
908 env->seen_direct_write = true;
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700909 return true;
910 default:
911 return false;
912 }
913}
914
Edward Creef1174f72017-08-07 15:26:19 +0100915static int __check_packet_access(struct bpf_verifier_env *env, u32 regno,
Yonghong Song9fd29c02017-11-12 14:49:09 -0800916 int off, int size, bool zero_size_allowed)
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700917{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700918 struct bpf_reg_state *regs = cur_regs(env);
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100919 struct bpf_reg_state *reg = &regs[regno];
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700920
Yonghong Song9fd29c02017-11-12 14:49:09 -0800921 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
922 (u64)off + size > reg->range) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700923 verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n",
Alexei Starovoitovd91b28e2016-05-19 18:17:13 -0700924 off, size, regno, reg->id, reg->off, reg->range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700925 return -EACCES;
926 }
927 return 0;
928}
929
Edward Creef1174f72017-08-07 15:26:19 +0100930static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
Yonghong Song9fd29c02017-11-12 14:49:09 -0800931 int size, bool zero_size_allowed)
Edward Creef1174f72017-08-07 15:26:19 +0100932{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700933 struct bpf_reg_state *regs = cur_regs(env);
Edward Creef1174f72017-08-07 15:26:19 +0100934 struct bpf_reg_state *reg = &regs[regno];
935 int err;
936
937 /* We may have added a variable offset to the packet pointer; but any
938 * reg->range we have comes after that. We are only checking the fixed
939 * offset.
940 */
941
942 /* We don't allow negative numbers, because we aren't tracking enough
943 * detail to prove they're safe.
944 */
Edward Creeb03c9f92017-08-07 15:26:36 +0100945 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700946 verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
Edward Creef1174f72017-08-07 15:26:19 +0100947 regno);
948 return -EACCES;
949 }
Yonghong Song9fd29c02017-11-12 14:49:09 -0800950 err = __check_packet_access(env, regno, off, size, zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +0100951 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700952 verbose(env, "R%d offset is outside of the packet\n", regno);
Edward Creef1174f72017-08-07 15:26:19 +0100953 return err;
954 }
955 return err;
956}
957
958/* check access to 'struct bpf_context' fields. Supports fixed offsets only */
Yonghong Song31fd8582017-06-13 15:52:13 -0700959static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700960 enum bpf_access_type t, enum bpf_reg_type *reg_type)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700961{
Daniel Borkmannf96da092017-07-02 02:13:27 +0200962 struct bpf_insn_access_aux info = {
963 .reg_type = *reg_type,
964 };
Yonghong Song31fd8582017-06-13 15:52:13 -0700965
Jakub Kicinski4f9218a2017-10-16 16:40:55 -0700966 if (env->ops->is_valid_access &&
967 env->ops->is_valid_access(off, size, t, &info)) {
Daniel Borkmannf96da092017-07-02 02:13:27 +0200968 /* A non zero info.ctx_field_size indicates that this field is a
969 * candidate for later verifier transformation to load the whole
970 * field and then apply a mask when accessed with a narrower
971 * access than actual ctx access size. A zero info.ctx_field_size
972 * will only allow for whole field access and rejects any other
973 * type of narrower access.
Yonghong Song31fd8582017-06-13 15:52:13 -0700974 */
Yonghong Song23994632017-06-22 15:07:39 -0700975 *reg_type = info.reg_type;
Yonghong Song31fd8582017-06-13 15:52:13 -0700976
Jakub Kicinski4f9218a2017-10-16 16:40:55 -0700977 env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -0700978 /* remember the offset of last byte accessed in ctx */
979 if (env->prog->aux->max_ctx_offset < off + size)
980 env->prog->aux->max_ctx_offset = off + size;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700981 return 0;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -0700982 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700983
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700984 verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700985 return -EACCES;
986}
987
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +0200988static bool __is_pointer_value(bool allow_ptr_leaks,
989 const struct bpf_reg_state *reg)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700990{
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +0200991 if (allow_ptr_leaks)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700992 return false;
993
Edward Creef1174f72017-08-07 15:26:19 +0100994 return reg->type != SCALAR_VALUE;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700995}
996
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +0200997static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
998{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700999 return __is_pointer_value(env->allow_ptr_leaks, cur_regs(env) + regno);
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001000}
1001
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001002static int check_pkt_ptr_alignment(struct bpf_verifier_env *env,
1003 const struct bpf_reg_state *reg,
David S. Millerd1174412017-05-10 11:22:52 -07001004 int off, int size, bool strict)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001005{
Edward Creef1174f72017-08-07 15:26:19 +01001006 struct tnum reg_off;
David S. Millere07b98d2017-05-10 11:38:07 -07001007 int ip_align;
David S. Millerd1174412017-05-10 11:22:52 -07001008
1009 /* Byte size accesses are always allowed. */
1010 if (!strict || size == 1)
1011 return 0;
1012
David S. Millere4eda882017-05-22 12:27:07 -04001013 /* For platforms that do not have a Kconfig enabling
1014 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of
1015 * NET_IP_ALIGN is universally set to '2'. And on platforms
1016 * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get
1017 * to this code only in strict mode where we want to emulate
1018 * the NET_IP_ALIGN==2 checking. Therefore use an
1019 * unconditional IP align value of '2'.
David S. Millere07b98d2017-05-10 11:38:07 -07001020 */
David S. Millere4eda882017-05-22 12:27:07 -04001021 ip_align = 2;
Edward Creef1174f72017-08-07 15:26:19 +01001022
1023 reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off));
1024 if (!tnum_is_aligned(reg_off, size)) {
1025 char tn_buf[48];
1026
1027 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001028 verbose(env,
1029 "misaligned packet access off %d+%s+%d+%d size %d\n",
Edward Creef1174f72017-08-07 15:26:19 +01001030 ip_align, tn_buf, reg->off, off, size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001031 return -EACCES;
1032 }
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001033
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001034 return 0;
1035}
1036
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001037static int check_generic_ptr_alignment(struct bpf_verifier_env *env,
1038 const struct bpf_reg_state *reg,
Edward Creef1174f72017-08-07 15:26:19 +01001039 const char *pointer_desc,
1040 int off, int size, bool strict)
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001041{
Edward Creef1174f72017-08-07 15:26:19 +01001042 struct tnum reg_off;
1043
1044 /* Byte size accesses are always allowed. */
1045 if (!strict || size == 1)
1046 return 0;
1047
1048 reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off));
1049 if (!tnum_is_aligned(reg_off, size)) {
1050 char tn_buf[48];
1051
1052 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001053 verbose(env, "misaligned %saccess off %s+%d+%d size %d\n",
Edward Creef1174f72017-08-07 15:26:19 +01001054 pointer_desc, tn_buf, reg->off, off, size);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001055 return -EACCES;
1056 }
1057
1058 return 0;
1059}
1060
David S. Millere07b98d2017-05-10 11:38:07 -07001061static int check_ptr_alignment(struct bpf_verifier_env *env,
1062 const struct bpf_reg_state *reg,
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001063 int off, int size)
1064{
David S. Millere07b98d2017-05-10 11:38:07 -07001065 bool strict = env->strict_alignment;
Edward Creef1174f72017-08-07 15:26:19 +01001066 const char *pointer_desc = "";
David S. Millerd1174412017-05-10 11:22:52 -07001067
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001068 switch (reg->type) {
1069 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001070 case PTR_TO_PACKET_META:
1071 /* Special case, because of NET_IP_ALIGN. Given metadata sits
1072 * right in front, treat it the very same way.
1073 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001074 return check_pkt_ptr_alignment(env, reg, off, size, strict);
Edward Creef1174f72017-08-07 15:26:19 +01001075 case PTR_TO_MAP_VALUE:
1076 pointer_desc = "value ";
1077 break;
1078 case PTR_TO_CTX:
1079 pointer_desc = "context ";
1080 break;
1081 case PTR_TO_STACK:
1082 pointer_desc = "stack ";
1083 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001084 default:
Edward Creef1174f72017-08-07 15:26:19 +01001085 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001086 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001087 return check_generic_ptr_alignment(env, reg, pointer_desc, off, size,
1088 strict);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001089}
1090
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001091/* check whether memory at (regno + off) is accessible for t = (read | write)
1092 * if t==write, value_regno is a register which value is stored into memory
1093 * if t==read, value_regno is a register which will receive the value from memory
1094 * if t==write && value_regno==-1, some unknown value is stored into memory
1095 * if t==read && value_regno==-1, don't care what we read from memory
1096 */
Yonghong Song31fd8582017-06-13 15:52:13 -07001097static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, int off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001098 int bpf_size, enum bpf_access_type t,
1099 int value_regno)
1100{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001101 struct bpf_verifier_state *state = env->cur_state;
1102 struct bpf_reg_state *regs = cur_regs(env);
1103 struct bpf_reg_state *reg = regs + regno;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001104 int size, err = 0;
1105
1106 size = bpf_size_to_bytes(bpf_size);
1107 if (size < 0)
1108 return size;
1109
Edward Creef1174f72017-08-07 15:26:19 +01001110 /* alignment checks will add in reg->off themselves */
David S. Millere07b98d2017-05-10 11:38:07 -07001111 err = check_ptr_alignment(env, reg, off, size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001112 if (err)
1113 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001114
Edward Creef1174f72017-08-07 15:26:19 +01001115 /* for access checks, reg->off is just part of off */
1116 off += reg->off;
1117
1118 if (reg->type == PTR_TO_MAP_VALUE) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001119 if (t == BPF_WRITE && value_regno >= 0 &&
1120 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001121 verbose(env, "R%d leaks addr into map\n", value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001122 return -EACCES;
1123 }
Josef Bacik48461132016-09-28 10:54:32 -04001124
Yonghong Song9fd29c02017-11-12 14:49:09 -08001125 err = check_map_access(env, regno, off, size, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001126 if (!err && t == BPF_READ && value_regno >= 0)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001127 mark_reg_unknown(env, regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001128
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07001129 } else if (reg->type == PTR_TO_CTX) {
Edward Creef1174f72017-08-07 15:26:19 +01001130 enum bpf_reg_type reg_type = SCALAR_VALUE;
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07001131
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001132 if (t == BPF_WRITE && value_regno >= 0 &&
1133 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001134 verbose(env, "R%d leaks addr into ctx\n", value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001135 return -EACCES;
1136 }
Edward Creef1174f72017-08-07 15:26:19 +01001137 /* ctx accesses must be at a fixed offset, so that we can
1138 * determine what type of data were returned.
1139 */
Jakub Kicinski28e33f92017-10-16 11:16:55 -07001140 if (reg->off) {
David S. Millerf8ddadc2017-10-22 13:36:53 +01001141 verbose(env,
1142 "dereference of modified ctx ptr R%d off=%d+%d, ctx+const is allowed, ctx+const+const is not\n",
Jakub Kicinski28e33f92017-10-16 11:16:55 -07001143 regno, reg->off, off - reg->off);
1144 return -EACCES;
1145 }
1146 if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
Edward Creef1174f72017-08-07 15:26:19 +01001147 char tn_buf[48];
1148
1149 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001150 verbose(env,
1151 "variable ctx access var_off=%s off=%d size=%d",
Edward Creef1174f72017-08-07 15:26:19 +01001152 tn_buf, off, size);
1153 return -EACCES;
1154 }
Yonghong Song31fd8582017-06-13 15:52:13 -07001155 err = check_ctx_access(env, insn_idx, off, size, t, &reg_type);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001156 if (!err && t == BPF_READ && value_regno >= 0) {
Edward Creef1174f72017-08-07 15:26:19 +01001157 /* ctx access returns either a scalar, or a
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001158 * PTR_TO_PACKET[_META,_END]. In the latter
1159 * case, we know the offset is zero.
Edward Creef1174f72017-08-07 15:26:19 +01001160 */
1161 if (reg_type == SCALAR_VALUE)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001162 mark_reg_unknown(env, regs, value_regno);
Edward Creef1174f72017-08-07 15:26:19 +01001163 else
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001164 mark_reg_known_zero(env, regs,
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001165 value_regno);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001166 regs[value_regno].id = 0;
1167 regs[value_regno].off = 0;
1168 regs[value_regno].range = 0;
1169 regs[value_regno].type = reg_type;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001170 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001171
Edward Creef1174f72017-08-07 15:26:19 +01001172 } else if (reg->type == PTR_TO_STACK) {
1173 /* stack accesses must be at a fixed offset, so that we can
1174 * determine what type of data were returned.
1175 * See check_stack_read().
1176 */
1177 if (!tnum_is_const(reg->var_off)) {
1178 char tn_buf[48];
1179
1180 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001181 verbose(env, "variable stack access var_off=%s off=%d size=%d",
Edward Creef1174f72017-08-07 15:26:19 +01001182 tn_buf, off, size);
1183 return -EACCES;
1184 }
1185 off += reg->var_off.value;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001186 if (off >= 0 || off < -MAX_BPF_STACK) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001187 verbose(env, "invalid stack off=%d size=%d\n", off,
1188 size);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001189 return -EACCES;
1190 }
Alexei Starovoitov87266792017-05-30 13:31:29 -07001191
1192 if (env->prog->aux->stack_depth < -off)
1193 env->prog->aux->stack_depth = -off;
1194
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001195 if (t == BPF_WRITE)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001196 err = check_stack_write(env, state, off, size,
1197 value_regno);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001198 else
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001199 err = check_stack_read(env, state, off, size,
1200 value_regno);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001201 } else if (reg_is_pkt_pointer(reg)) {
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001202 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001203 verbose(env, "cannot write into packet\n");
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001204 return -EACCES;
1205 }
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001206 if (t == BPF_WRITE && value_regno >= 0 &&
1207 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001208 verbose(env, "R%d leaks addr into packet\n",
1209 value_regno);
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001210 return -EACCES;
1211 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001212 err = check_packet_access(env, regno, off, size, false);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001213 if (!err && t == BPF_READ && value_regno >= 0)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001214 mark_reg_unknown(env, regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001215 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001216 verbose(env, "R%d invalid mem access '%s'\n", regno,
1217 reg_type_str[reg->type]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001218 return -EACCES;
1219 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001220
Edward Creef1174f72017-08-07 15:26:19 +01001221 if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ &&
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001222 regs[value_regno].type == SCALAR_VALUE) {
Edward Creef1174f72017-08-07 15:26:19 +01001223 /* b/h/w load zero-extends, mark upper bits as known 0 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001224 regs[value_regno].var_off =
1225 tnum_cast(regs[value_regno].var_off, size);
1226 __update_reg_bounds(&regs[value_regno]);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001227 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001228 return err;
1229}
1230
Yonghong Song31fd8582017-06-13 15:52:13 -07001231static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001232{
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001233 int err;
1234
1235 if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
1236 insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001237 verbose(env, "BPF_XADD uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001238 return -EINVAL;
1239 }
1240
1241 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01001242 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001243 if (err)
1244 return err;
1245
1246 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01001247 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001248 if (err)
1249 return err;
1250
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +02001251 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001252 verbose(env, "R%d leaks addr into mem\n", insn->src_reg);
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +02001253 return -EACCES;
1254 }
1255
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001256 /* check whether atomic_add can read the memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07001257 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001258 BPF_SIZE(insn->code), BPF_READ, -1);
1259 if (err)
1260 return err;
1261
1262 /* check whether atomic_add can write into the same memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07001263 return check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001264 BPF_SIZE(insn->code), BPF_WRITE, -1);
1265}
1266
Edward Creef1174f72017-08-07 15:26:19 +01001267/* Does this register contain a constant zero? */
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001268static bool register_is_null(struct bpf_reg_state *reg)
Edward Creef1174f72017-08-07 15:26:19 +01001269{
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001270 return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0);
Edward Creef1174f72017-08-07 15:26:19 +01001271}
1272
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001273/* when register 'regno' is passed into function that will read 'access_size'
1274 * bytes from that pointer, make sure that it's within stack boundary
Edward Creef1174f72017-08-07 15:26:19 +01001275 * and all elements of stack are initialized.
1276 * Unlike most pointer bounds-checking functions, this one doesn't take an
1277 * 'off' argument, so it has to add in reg->off itself.
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001278 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001279static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
Daniel Borkmann435faee12016-04-13 00:10:51 +02001280 int access_size, bool zero_size_allowed,
1281 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001282{
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001283 struct bpf_reg_state *reg = cur_regs(env) + regno;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001284 struct bpf_verifier_state *state = env->cur_state;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001285 int off, i, slot, spi;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001286
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001287 if (reg->type != PTR_TO_STACK) {
Edward Creef1174f72017-08-07 15:26:19 +01001288 /* Allow zero-byte read from NULL, regardless of pointer type */
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001289 if (zero_size_allowed && access_size == 0 &&
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001290 register_is_null(reg))
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001291 return 0;
1292
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001293 verbose(env, "R%d type=%s expected=%s\n", regno,
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001294 reg_type_str[reg->type],
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001295 reg_type_str[PTR_TO_STACK]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001296 return -EACCES;
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001297 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001298
Edward Creef1174f72017-08-07 15:26:19 +01001299 /* Only allow fixed-offset stack reads */
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001300 if (!tnum_is_const(reg->var_off)) {
Edward Creef1174f72017-08-07 15:26:19 +01001301 char tn_buf[48];
1302
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001303 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001304 verbose(env, "invalid variable stack read R%d var_off=%s\n",
Edward Creef1174f72017-08-07 15:26:19 +01001305 regno, tn_buf);
1306 }
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001307 off = reg->off + reg->var_off.value;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001308 if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
Yonghong Song9fd29c02017-11-12 14:49:09 -08001309 access_size < 0 || (access_size == 0 && !zero_size_allowed)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001310 verbose(env, "invalid stack type R%d off=%d access_size=%d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001311 regno, off, access_size);
1312 return -EACCES;
1313 }
1314
Alexei Starovoitov87266792017-05-30 13:31:29 -07001315 if (env->prog->aux->stack_depth < -off)
1316 env->prog->aux->stack_depth = -off;
1317
Daniel Borkmann435faee12016-04-13 00:10:51 +02001318 if (meta && meta->raw_mode) {
1319 meta->access_size = access_size;
1320 meta->regno = regno;
1321 return 0;
1322 }
1323
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001324 for (i = 0; i < access_size; i++) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001325 slot = -(off + i) - 1;
1326 spi = slot / BPF_REG_SIZE;
1327 if (state->allocated_stack <= slot ||
1328 state->stack[spi].slot_type[slot % BPF_REG_SIZE] !=
1329 STACK_MISC) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001330 verbose(env, "invalid indirect read from stack off %d+%d size %d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001331 off, i, access_size);
1332 return -EACCES;
1333 }
1334 }
1335 return 0;
1336}
1337
Gianluca Borello06c1c042017-01-09 10:19:49 -08001338static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
1339 int access_size, bool zero_size_allowed,
1340 struct bpf_call_arg_meta *meta)
1341{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001342 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
Gianluca Borello06c1c042017-01-09 10:19:49 -08001343
Edward Creef1174f72017-08-07 15:26:19 +01001344 switch (reg->type) {
Gianluca Borello06c1c042017-01-09 10:19:49 -08001345 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001346 case PTR_TO_PACKET_META:
Yonghong Song9fd29c02017-11-12 14:49:09 -08001347 return check_packet_access(env, regno, reg->off, access_size,
1348 zero_size_allowed);
Gianluca Borello06c1c042017-01-09 10:19:49 -08001349 case PTR_TO_MAP_VALUE:
Yonghong Song9fd29c02017-11-12 14:49:09 -08001350 return check_map_access(env, regno, reg->off, access_size,
1351 zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01001352 default: /* scalar_value|ptr_to_stack or invalid ptr */
Gianluca Borello06c1c042017-01-09 10:19:49 -08001353 return check_stack_boundary(env, regno, access_size,
1354 zero_size_allowed, meta);
1355 }
1356}
1357
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001358static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001359 enum bpf_arg_type arg_type,
1360 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001361{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001362 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001363 enum bpf_reg_type expected_type, type = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001364 int err = 0;
1365
Daniel Borkmann80f1d682015-03-12 17:21:42 +01001366 if (arg_type == ARG_DONTCARE)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001367 return 0;
1368
Edward Creedc503a82017-08-15 20:34:35 +01001369 err = check_reg_arg(env, regno, SRC_OP);
1370 if (err)
1371 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001372
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001373 if (arg_type == ARG_ANYTHING) {
1374 if (is_pointer_value(env, regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001375 verbose(env, "R%d leaks addr into helper function\n",
1376 regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001377 return -EACCES;
1378 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01001379 return 0;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001380 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01001381
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001382 if (type_is_pkt_pointer(type) &&
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001383 !may_access_direct_pkt_data(env, meta, BPF_READ)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001384 verbose(env, "helper access to the packet is not allowed\n");
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001385 return -EACCES;
1386 }
1387
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001388 if (arg_type == ARG_PTR_TO_MAP_KEY ||
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001389 arg_type == ARG_PTR_TO_MAP_VALUE) {
1390 expected_type = PTR_TO_STACK;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001391 if (!type_is_pkt_pointer(type) &&
1392 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001393 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001394 } else if (arg_type == ARG_CONST_SIZE ||
1395 arg_type == ARG_CONST_SIZE_OR_ZERO) {
Edward Creef1174f72017-08-07 15:26:19 +01001396 expected_type = SCALAR_VALUE;
1397 if (type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001398 goto err_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001399 } else if (arg_type == ARG_CONST_MAP_PTR) {
1400 expected_type = CONST_PTR_TO_MAP;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001401 if (type != expected_type)
1402 goto err_type;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001403 } else if (arg_type == ARG_PTR_TO_CTX) {
1404 expected_type = PTR_TO_CTX;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001405 if (type != expected_type)
1406 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001407 } else if (arg_type == ARG_PTR_TO_MEM ||
Gianluca Borellodb1ac492017-11-22 18:32:53 +00001408 arg_type == ARG_PTR_TO_MEM_OR_NULL ||
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001409 arg_type == ARG_PTR_TO_UNINIT_MEM) {
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001410 expected_type = PTR_TO_STACK;
1411 /* One exception here. In case function allows for NULL to be
Edward Creef1174f72017-08-07 15:26:19 +01001412 * passed in as argument, it's a SCALAR_VALUE type. Final test
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001413 * happens during stack boundary checking.
1414 */
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001415 if (register_is_null(reg) &&
Gianluca Borellodb1ac492017-11-22 18:32:53 +00001416 arg_type == ARG_PTR_TO_MEM_OR_NULL)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001417 /* final test in check_stack_boundary() */;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001418 else if (!type_is_pkt_pointer(type) &&
1419 type != PTR_TO_MAP_VALUE &&
Edward Creef1174f72017-08-07 15:26:19 +01001420 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001421 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001422 meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001423 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001424 verbose(env, "unsupported arg_type %d\n", arg_type);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001425 return -EFAULT;
1426 }
1427
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001428 if (arg_type == ARG_CONST_MAP_PTR) {
1429 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001430 meta->map_ptr = reg->map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001431 } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
1432 /* bpf_map_xxx(..., map_ptr, ..., key) call:
1433 * check that [key, key + map->key_size) are within
1434 * stack limits and initialized
1435 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001436 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001437 /* in function declaration map_ptr must come before
1438 * map_key, so that it's verified and known before
1439 * we have to check map_key here. Otherwise it means
1440 * that kernel subsystem misconfigured verifier
1441 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001442 verbose(env, "invalid map_ptr to access map->key\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001443 return -EACCES;
1444 }
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001445 if (type_is_pkt_pointer(type))
Edward Creef1174f72017-08-07 15:26:19 +01001446 err = check_packet_access(env, regno, reg->off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001447 meta->map_ptr->key_size,
1448 false);
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001449 else
1450 err = check_stack_boundary(env, regno,
1451 meta->map_ptr->key_size,
1452 false, NULL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001453 } else if (arg_type == ARG_PTR_TO_MAP_VALUE) {
1454 /* bpf_map_xxx(..., map_ptr, ..., value) call:
1455 * check [value, value + map->value_size) validity
1456 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001457 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001458 /* kernel subsystem misconfigured verifier */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001459 verbose(env, "invalid map_ptr to access map->value\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001460 return -EACCES;
1461 }
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001462 if (type_is_pkt_pointer(type))
Edward Creef1174f72017-08-07 15:26:19 +01001463 err = check_packet_access(env, regno, reg->off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001464 meta->map_ptr->value_size,
1465 false);
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001466 else
1467 err = check_stack_boundary(env, regno,
1468 meta->map_ptr->value_size,
1469 false, NULL);
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001470 } else if (arg_type == ARG_CONST_SIZE ||
1471 arg_type == ARG_CONST_SIZE_OR_ZERO) {
1472 bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001473
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001474 /* bpf_xxx(..., buf, len) call will access 'len' bytes
1475 * from stack pointer 'buf'. Check it
1476 * note: regno == len, regno - 1 == buf
1477 */
1478 if (regno == 0) {
1479 /* kernel subsystem misconfigured verifier */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001480 verbose(env,
1481 "ARG_CONST_SIZE cannot be first argument\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001482 return -EACCES;
1483 }
Gianluca Borello06c1c042017-01-09 10:19:49 -08001484
Edward Creef1174f72017-08-07 15:26:19 +01001485 /* The register is SCALAR_VALUE; the access check
1486 * happens using its boundaries.
Gianluca Borello06c1c042017-01-09 10:19:49 -08001487 */
Edward Creef1174f72017-08-07 15:26:19 +01001488
1489 if (!tnum_is_const(reg->var_off))
Gianluca Borello06c1c042017-01-09 10:19:49 -08001490 /* For unprivileged variable accesses, disable raw
1491 * mode so that the program is required to
1492 * initialize all the memory that the helper could
1493 * just partially fill up.
1494 */
1495 meta = NULL;
1496
Edward Creeb03c9f92017-08-07 15:26:36 +01001497 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001498 verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n",
Edward Creef1174f72017-08-07 15:26:19 +01001499 regno);
1500 return -EACCES;
1501 }
Gianluca Borello06c1c042017-01-09 10:19:49 -08001502
Edward Creeb03c9f92017-08-07 15:26:36 +01001503 if (reg->umin_value == 0) {
Edward Creef1174f72017-08-07 15:26:19 +01001504 err = check_helper_mem_access(env, regno - 1, 0,
1505 zero_size_allowed,
1506 meta);
Gianluca Borello06c1c042017-01-09 10:19:49 -08001507 if (err)
1508 return err;
Gianluca Borello06c1c042017-01-09 10:19:49 -08001509 }
Edward Creef1174f72017-08-07 15:26:19 +01001510
Edward Creeb03c9f92017-08-07 15:26:36 +01001511 if (reg->umax_value >= BPF_MAX_VAR_SIZ) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001512 verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
Edward Creef1174f72017-08-07 15:26:19 +01001513 regno);
1514 return -EACCES;
1515 }
1516 err = check_helper_mem_access(env, regno - 1,
Edward Creeb03c9f92017-08-07 15:26:36 +01001517 reg->umax_value,
Edward Creef1174f72017-08-07 15:26:19 +01001518 zero_size_allowed, meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001519 }
1520
1521 return err;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001522err_type:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001523 verbose(env, "R%d type=%s expected=%s\n", regno,
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001524 reg_type_str[type], reg_type_str[expected_type]);
1525 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001526}
1527
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001528static int check_map_func_compatibility(struct bpf_verifier_env *env,
1529 struct bpf_map *map, int func_id)
Kaixu Xia35578d72015-08-06 07:02:35 +00001530{
Kaixu Xia35578d72015-08-06 07:02:35 +00001531 if (!map)
1532 return 0;
1533
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001534 /* We need a two way check, first is from map perspective ... */
1535 switch (map->map_type) {
1536 case BPF_MAP_TYPE_PROG_ARRAY:
1537 if (func_id != BPF_FUNC_tail_call)
1538 goto error;
1539 break;
1540 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
1541 if (func_id != BPF_FUNC_perf_event_read &&
Yonghong Song908432c2017-10-05 09:19:20 -07001542 func_id != BPF_FUNC_perf_event_output &&
1543 func_id != BPF_FUNC_perf_event_read_value)
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001544 goto error;
1545 break;
1546 case BPF_MAP_TYPE_STACK_TRACE:
1547 if (func_id != BPF_FUNC_get_stackid)
1548 goto error;
1549 break;
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -07001550 case BPF_MAP_TYPE_CGROUP_ARRAY:
David S. Miller60747ef2016-08-18 01:17:32 -04001551 if (func_id != BPF_FUNC_skb_under_cgroup &&
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001552 func_id != BPF_FUNC_current_task_under_cgroup)
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07001553 goto error;
1554 break;
John Fastabend546ac1f2017-07-17 09:28:56 -07001555 /* devmap returns a pointer to a live net_device ifindex that we cannot
1556 * allow to be modified from bpf side. So do not allow lookup elements
1557 * for now.
1558 */
1559 case BPF_MAP_TYPE_DEVMAP:
John Fastabend2ddf71e2017-07-17 09:30:02 -07001560 if (func_id != BPF_FUNC_redirect_map)
John Fastabend546ac1f2017-07-17 09:28:56 -07001561 goto error;
1562 break;
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02001563 /* Restrict bpf side of cpumap, open when use-cases appear */
1564 case BPF_MAP_TYPE_CPUMAP:
1565 if (func_id != BPF_FUNC_redirect_map)
1566 goto error;
1567 break;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07001568 case BPF_MAP_TYPE_ARRAY_OF_MAPS:
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001569 case BPF_MAP_TYPE_HASH_OF_MAPS:
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07001570 if (func_id != BPF_FUNC_map_lookup_elem)
1571 goto error;
Martin KaFai Lau16a43622017-08-17 18:14:43 -07001572 break;
John Fastabend174a79f2017-08-15 22:32:47 -07001573 case BPF_MAP_TYPE_SOCKMAP:
1574 if (func_id != BPF_FUNC_sk_redirect_map &&
1575 func_id != BPF_FUNC_sock_map_update &&
1576 func_id != BPF_FUNC_map_delete_elem)
1577 goto error;
1578 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001579 default:
1580 break;
1581 }
1582
1583 /* ... and second from the function itself. */
1584 switch (func_id) {
1585 case BPF_FUNC_tail_call:
1586 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1587 goto error;
1588 break;
1589 case BPF_FUNC_perf_event_read:
1590 case BPF_FUNC_perf_event_output:
Yonghong Song908432c2017-10-05 09:19:20 -07001591 case BPF_FUNC_perf_event_read_value:
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001592 if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
1593 goto error;
1594 break;
1595 case BPF_FUNC_get_stackid:
1596 if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
1597 goto error;
1598 break;
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001599 case BPF_FUNC_current_task_under_cgroup:
Daniel Borkmann747ea552016-08-12 22:17:17 +02001600 case BPF_FUNC_skb_under_cgroup:
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07001601 if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
1602 goto error;
1603 break;
John Fastabend97f91a72017-07-17 09:29:18 -07001604 case BPF_FUNC_redirect_map:
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02001605 if (map->map_type != BPF_MAP_TYPE_DEVMAP &&
1606 map->map_type != BPF_MAP_TYPE_CPUMAP)
John Fastabend97f91a72017-07-17 09:29:18 -07001607 goto error;
1608 break;
John Fastabend174a79f2017-08-15 22:32:47 -07001609 case BPF_FUNC_sk_redirect_map:
1610 if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
1611 goto error;
1612 break;
1613 case BPF_FUNC_sock_map_update:
1614 if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
1615 goto error;
1616 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001617 default:
1618 break;
Kaixu Xia35578d72015-08-06 07:02:35 +00001619 }
1620
1621 return 0;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001622error:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001623 verbose(env, "cannot pass map_type %d into func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02001624 map->map_type, func_id_name(func_id), func_id);
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001625 return -EINVAL;
Kaixu Xia35578d72015-08-06 07:02:35 +00001626}
1627
Daniel Borkmann435faee12016-04-13 00:10:51 +02001628static int check_raw_mode(const struct bpf_func_proto *fn)
1629{
1630 int count = 0;
1631
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001632 if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02001633 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001634 if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02001635 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001636 if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02001637 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001638 if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02001639 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001640 if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02001641 count++;
1642
1643 return count > 1 ? -EINVAL : 0;
1644}
1645
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001646/* Packet data might have moved, any old PTR_TO_PACKET[_META,_END]
1647 * are now invalid, so turn them into unknown SCALAR_VALUE.
Edward Creef1174f72017-08-07 15:26:19 +01001648 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001649static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001650{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001651 struct bpf_verifier_state *state = env->cur_state;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001652 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001653 int i;
1654
1655 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001656 if (reg_is_pkt_pointer_any(&regs[i]))
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001657 mark_reg_unknown(env, regs, i);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001658
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001659 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
1660 if (state->stack[i].slot_type[0] != STACK_SPILL)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001661 continue;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001662 reg = &state->stack[i].spilled_ptr;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001663 if (reg_is_pkt_pointer_any(reg))
1664 __mark_reg_unknown(reg);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001665 }
1666}
1667
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07001668static int check_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001669{
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001670 const struct bpf_func_proto *fn = NULL;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001671 struct bpf_reg_state *regs;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001672 struct bpf_call_arg_meta meta;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001673 bool changes_data;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001674 int i, err;
1675
1676 /* find function prototype */
1677 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001678 verbose(env, "invalid func %s#%d\n", func_id_name(func_id),
1679 func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001680 return -EINVAL;
1681 }
1682
Jakub Kicinski00176a32017-10-16 16:40:54 -07001683 if (env->ops->get_func_proto)
1684 fn = env->ops->get_func_proto(func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001685
1686 if (!fn) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001687 verbose(env, "unknown func %s#%d\n", func_id_name(func_id),
1688 func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001689 return -EINVAL;
1690 }
1691
1692 /* eBPF programs must be GPL compatible to use GPL-ed functions */
Daniel Borkmann24701ec2015-03-01 12:31:47 +01001693 if (!env->prog->gpl_compatible && fn->gpl_only) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001694 verbose(env, "cannot call GPL only function from proprietary program\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001695 return -EINVAL;
1696 }
1697
Martin KaFai Lau17bedab2016-12-07 15:53:11 -08001698 changes_data = bpf_helper_changes_pkt_data(fn->func);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001699
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001700 memset(&meta, 0, sizeof(meta));
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001701 meta.pkt_access = fn->pkt_access;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001702
Daniel Borkmann435faee12016-04-13 00:10:51 +02001703 /* We only support one arg being in raw mode at the moment, which
1704 * is sufficient for the helper functions we have right now.
1705 */
1706 err = check_raw_mode(fn);
1707 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001708 verbose(env, "kernel subsystem misconfigured func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02001709 func_id_name(func_id), func_id);
Daniel Borkmann435faee12016-04-13 00:10:51 +02001710 return err;
1711 }
1712
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001713 /* check args */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001714 err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001715 if (err)
1716 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001717 err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001718 if (err)
1719 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001720 err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001721 if (err)
1722 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001723 err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001724 if (err)
1725 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001726 err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001727 if (err)
1728 return err;
1729
Daniel Borkmann435faee12016-04-13 00:10:51 +02001730 /* Mark slots with STACK_MISC in case of raw mode, stack offset
1731 * is inferred from register state.
1732 */
1733 for (i = 0; i < meta.access_size; i++) {
Yonghong Song31fd8582017-06-13 15:52:13 -07001734 err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, BPF_WRITE, -1);
Daniel Borkmann435faee12016-04-13 00:10:51 +02001735 if (err)
1736 return err;
1737 }
1738
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001739 regs = cur_regs(env);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001740 /* reset caller saved regs */
Edward Creedc503a82017-08-15 20:34:35 +01001741 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001742 mark_reg_not_init(env, regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01001743 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
1744 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001745
Edward Creedc503a82017-08-15 20:34:35 +01001746 /* update return register (already marked as written above) */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001747 if (fn->ret_type == RET_INTEGER) {
Edward Creef1174f72017-08-07 15:26:19 +01001748 /* sets type to SCALAR_VALUE */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001749 mark_reg_unknown(env, regs, BPF_REG_0);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001750 } else if (fn->ret_type == RET_VOID) {
1751 regs[BPF_REG_0].type = NOT_INIT;
1752 } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) {
Martin KaFai Laufad73a12017-03-22 10:00:32 -07001753 struct bpf_insn_aux_data *insn_aux;
1754
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001755 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
Edward Creef1174f72017-08-07 15:26:19 +01001756 /* There is no offset yet applied, variable or fixed */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001757 mark_reg_known_zero(env, regs, BPF_REG_0);
Edward Creef1174f72017-08-07 15:26:19 +01001758 regs[BPF_REG_0].off = 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001759 /* remember map_ptr, so that check_map_access()
1760 * can check 'value_size' boundary of memory access
1761 * to map element returned from bpf_map_lookup_elem()
1762 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001763 if (meta.map_ptr == NULL) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001764 verbose(env,
1765 "kernel subsystem misconfigured verifier\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001766 return -EINVAL;
1767 }
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001768 regs[BPF_REG_0].map_ptr = meta.map_ptr;
Thomas Graf57a09bf2016-10-18 19:51:19 +02001769 regs[BPF_REG_0].id = ++env->id_gen;
Martin KaFai Laufad73a12017-03-22 10:00:32 -07001770 insn_aux = &env->insn_aux_data[insn_idx];
1771 if (!insn_aux->map_ptr)
1772 insn_aux->map_ptr = meta.map_ptr;
1773 else if (insn_aux->map_ptr != meta.map_ptr)
1774 insn_aux->map_ptr = BPF_MAP_PTR_POISON;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001775 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001776 verbose(env, "unknown return type %d of func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02001777 fn->ret_type, func_id_name(func_id), func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001778 return -EINVAL;
1779 }
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001780
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001781 err = check_map_func_compatibility(env, meta.map_ptr, func_id);
Kaixu Xia35578d72015-08-06 07:02:35 +00001782 if (err)
1783 return err;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001784
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001785 if (changes_data)
1786 clear_all_pkt_pointers(env);
1787 return 0;
1788}
1789
Edward Creef1174f72017-08-07 15:26:19 +01001790static void coerce_reg_to_32(struct bpf_reg_state *reg)
David S. Millerd1174412017-05-10 11:22:52 -07001791{
Edward Creef1174f72017-08-07 15:26:19 +01001792 /* clear high 32 bits */
1793 reg->var_off = tnum_cast(reg->var_off, 4);
Edward Creeb03c9f92017-08-07 15:26:36 +01001794 /* Update bounds */
1795 __update_reg_bounds(reg);
1796}
1797
1798static bool signed_add_overflows(s64 a, s64 b)
1799{
1800 /* Do the add in u64, where overflow is well-defined */
1801 s64 res = (s64)((u64)a + (u64)b);
1802
1803 if (b < 0)
1804 return res > a;
1805 return res < a;
1806}
1807
1808static bool signed_sub_overflows(s64 a, s64 b)
1809{
1810 /* Do the sub in u64, where overflow is well-defined */
1811 s64 res = (s64)((u64)a - (u64)b);
1812
1813 if (b < 0)
1814 return res < a;
1815 return res > a;
David S. Millerd1174412017-05-10 11:22:52 -07001816}
1817
Edward Creef1174f72017-08-07 15:26:19 +01001818/* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off.
Edward Creef1174f72017-08-07 15:26:19 +01001819 * Caller should also handle BPF_MOV case separately.
1820 * If we return -EACCES, caller may want to try again treating pointer as a
1821 * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks.
1822 */
1823static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
1824 struct bpf_insn *insn,
1825 const struct bpf_reg_state *ptr_reg,
1826 const struct bpf_reg_state *off_reg)
Josef Bacik48461132016-09-28 10:54:32 -04001827{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001828 struct bpf_reg_state *regs = cur_regs(env), *dst_reg;
Edward Creef1174f72017-08-07 15:26:19 +01001829 bool known = tnum_is_const(off_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01001830 s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value,
1831 smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
1832 u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value,
1833 umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value;
Josef Bacik48461132016-09-28 10:54:32 -04001834 u8 opcode = BPF_OP(insn->code);
Edward Creef1174f72017-08-07 15:26:19 +01001835 u32 dst = insn->dst_reg;
Josef Bacik48461132016-09-28 10:54:32 -04001836
Edward Creef1174f72017-08-07 15:26:19 +01001837 dst_reg = &regs[dst];
Josef Bacik48461132016-09-28 10:54:32 -04001838
Edward Creeb03c9f92017-08-07 15:26:36 +01001839 if (WARN_ON_ONCE(known && (smin_val != smax_val))) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001840 print_verifier_state(env, env->cur_state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001841 verbose(env,
1842 "verifier internal error: known but bad sbounds\n");
Edward Creeb03c9f92017-08-07 15:26:36 +01001843 return -EINVAL;
1844 }
1845 if (WARN_ON_ONCE(known && (umin_val != umax_val))) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001846 print_verifier_state(env, env->cur_state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001847 verbose(env,
1848 "verifier internal error: known but bad ubounds\n");
Edward Creef1174f72017-08-07 15:26:19 +01001849 return -EINVAL;
Josef Bacik48461132016-09-28 10:54:32 -04001850 }
1851
Edward Creef1174f72017-08-07 15:26:19 +01001852 if (BPF_CLASS(insn->code) != BPF_ALU64) {
1853 /* 32-bit ALU ops on pointers produce (meaningless) scalars */
1854 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001855 verbose(env,
1856 "R%d 32-bit pointer arithmetic prohibited\n",
Edward Creef1174f72017-08-07 15:26:19 +01001857 dst);
1858 return -EACCES;
1859 }
David S. Millerd1174412017-05-10 11:22:52 -07001860
Edward Creef1174f72017-08-07 15:26:19 +01001861 if (ptr_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
1862 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001863 verbose(env, "R%d pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL prohibited, null-check it first\n",
Edward Creef1174f72017-08-07 15:26:19 +01001864 dst);
1865 return -EACCES;
1866 }
1867 if (ptr_reg->type == CONST_PTR_TO_MAP) {
1868 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001869 verbose(env, "R%d pointer arithmetic on CONST_PTR_TO_MAP prohibited\n",
Edward Creef1174f72017-08-07 15:26:19 +01001870 dst);
1871 return -EACCES;
1872 }
1873 if (ptr_reg->type == PTR_TO_PACKET_END) {
1874 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001875 verbose(env, "R%d pointer arithmetic on PTR_TO_PACKET_END prohibited\n",
Edward Creef1174f72017-08-07 15:26:19 +01001876 dst);
1877 return -EACCES;
1878 }
1879
1880 /* In case of 'scalar += pointer', dst_reg inherits pointer type and id.
1881 * The id may be overwritten later if we create a new variable offset.
Josef Bacik48461132016-09-28 10:54:32 -04001882 */
Edward Creef1174f72017-08-07 15:26:19 +01001883 dst_reg->type = ptr_reg->type;
1884 dst_reg->id = ptr_reg->id;
Josef Bacikf23cc642016-11-14 15:45:36 -05001885
Josef Bacik48461132016-09-28 10:54:32 -04001886 switch (opcode) {
1887 case BPF_ADD:
Edward Creef1174f72017-08-07 15:26:19 +01001888 /* We can take a fixed offset as long as it doesn't overflow
1889 * the s32 'off' field
1890 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001891 if (known && (ptr_reg->off + smin_val ==
1892 (s64)(s32)(ptr_reg->off + smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01001893 /* pointer += K. Accumulate it into fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01001894 dst_reg->smin_value = smin_ptr;
1895 dst_reg->smax_value = smax_ptr;
1896 dst_reg->umin_value = umin_ptr;
1897 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01001898 dst_reg->var_off = ptr_reg->var_off;
Edward Creeb03c9f92017-08-07 15:26:36 +01001899 dst_reg->off = ptr_reg->off + smin_val;
Edward Creef1174f72017-08-07 15:26:19 +01001900 dst_reg->range = ptr_reg->range;
1901 break;
1902 }
Edward Creef1174f72017-08-07 15:26:19 +01001903 /* A new variable offset is created. Note that off_reg->off
1904 * == 0, since it's a scalar.
1905 * dst_reg gets the pointer type and since some positive
1906 * integer value was added to the pointer, give it a new 'id'
1907 * if it's a PTR_TO_PACKET.
1908 * this creates a new 'base' pointer, off_reg (variable) gets
1909 * added into the variable offset, and we copy the fixed offset
1910 * from ptr_reg.
1911 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001912 if (signed_add_overflows(smin_ptr, smin_val) ||
1913 signed_add_overflows(smax_ptr, smax_val)) {
1914 dst_reg->smin_value = S64_MIN;
1915 dst_reg->smax_value = S64_MAX;
1916 } else {
1917 dst_reg->smin_value = smin_ptr + smin_val;
1918 dst_reg->smax_value = smax_ptr + smax_val;
1919 }
1920 if (umin_ptr + umin_val < umin_ptr ||
1921 umax_ptr + umax_val < umax_ptr) {
1922 dst_reg->umin_value = 0;
1923 dst_reg->umax_value = U64_MAX;
1924 } else {
1925 dst_reg->umin_value = umin_ptr + umin_val;
1926 dst_reg->umax_value = umax_ptr + umax_val;
1927 }
Edward Creef1174f72017-08-07 15:26:19 +01001928 dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off);
1929 dst_reg->off = ptr_reg->off;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001930 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01001931 dst_reg->id = ++env->id_gen;
1932 /* something was added to pkt_ptr, set range to zero */
1933 dst_reg->range = 0;
1934 }
Josef Bacik48461132016-09-28 10:54:32 -04001935 break;
1936 case BPF_SUB:
Edward Creef1174f72017-08-07 15:26:19 +01001937 if (dst_reg == off_reg) {
1938 /* scalar -= pointer. Creates an unknown scalar */
1939 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001940 verbose(env, "R%d tried to subtract pointer from scalar\n",
Edward Creef1174f72017-08-07 15:26:19 +01001941 dst);
1942 return -EACCES;
1943 }
1944 /* We don't allow subtraction from FP, because (according to
1945 * test_verifier.c test "invalid fp arithmetic", JITs might not
1946 * be able to deal with it.
Edward Cree93057062017-07-21 14:37:34 +01001947 */
Edward Creef1174f72017-08-07 15:26:19 +01001948 if (ptr_reg->type == PTR_TO_STACK) {
1949 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001950 verbose(env, "R%d subtraction from stack pointer prohibited\n",
Edward Creef1174f72017-08-07 15:26:19 +01001951 dst);
1952 return -EACCES;
1953 }
Edward Creeb03c9f92017-08-07 15:26:36 +01001954 if (known && (ptr_reg->off - smin_val ==
1955 (s64)(s32)(ptr_reg->off - smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01001956 /* pointer -= K. Subtract it from fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01001957 dst_reg->smin_value = smin_ptr;
1958 dst_reg->smax_value = smax_ptr;
1959 dst_reg->umin_value = umin_ptr;
1960 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01001961 dst_reg->var_off = ptr_reg->var_off;
1962 dst_reg->id = ptr_reg->id;
Edward Creeb03c9f92017-08-07 15:26:36 +01001963 dst_reg->off = ptr_reg->off - smin_val;
Edward Creef1174f72017-08-07 15:26:19 +01001964 dst_reg->range = ptr_reg->range;
1965 break;
1966 }
Edward Creef1174f72017-08-07 15:26:19 +01001967 /* A new variable offset is created. If the subtrahend is known
1968 * nonnegative, then any reg->range we had before is still good.
1969 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001970 if (signed_sub_overflows(smin_ptr, smax_val) ||
1971 signed_sub_overflows(smax_ptr, smin_val)) {
1972 /* Overflow possible, we know nothing */
1973 dst_reg->smin_value = S64_MIN;
1974 dst_reg->smax_value = S64_MAX;
1975 } else {
1976 dst_reg->smin_value = smin_ptr - smax_val;
1977 dst_reg->smax_value = smax_ptr - smin_val;
1978 }
1979 if (umin_ptr < umax_val) {
1980 /* Overflow possible, we know nothing */
1981 dst_reg->umin_value = 0;
1982 dst_reg->umax_value = U64_MAX;
1983 } else {
1984 /* Cannot overflow (as long as bounds are consistent) */
1985 dst_reg->umin_value = umin_ptr - umax_val;
1986 dst_reg->umax_value = umax_ptr - umin_val;
1987 }
Edward Creef1174f72017-08-07 15:26:19 +01001988 dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off);
1989 dst_reg->off = ptr_reg->off;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001990 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01001991 dst_reg->id = ++env->id_gen;
1992 /* something was added to pkt_ptr, set range to zero */
Edward Creeb03c9f92017-08-07 15:26:36 +01001993 if (smin_val < 0)
Edward Creef1174f72017-08-07 15:26:19 +01001994 dst_reg->range = 0;
1995 }
1996 break;
1997 case BPF_AND:
1998 case BPF_OR:
1999 case BPF_XOR:
2000 /* bitwise ops on pointers are troublesome, prohibit for now.
2001 * (However, in principle we could allow some cases, e.g.
2002 * ptr &= ~3 which would reduce min_value by 3.)
2003 */
2004 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002005 verbose(env, "R%d bitwise operator %s on pointer prohibited\n",
Edward Creef1174f72017-08-07 15:26:19 +01002006 dst, bpf_alu_string[opcode >> 4]);
2007 return -EACCES;
2008 default:
2009 /* other operators (e.g. MUL,LSH) produce non-pointer results */
2010 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002011 verbose(env, "R%d pointer arithmetic with %s operator prohibited\n",
Edward Creef1174f72017-08-07 15:26:19 +01002012 dst, bpf_alu_string[opcode >> 4]);
2013 return -EACCES;
2014 }
2015
Edward Creeb03c9f92017-08-07 15:26:36 +01002016 __update_reg_bounds(dst_reg);
2017 __reg_deduce_bounds(dst_reg);
2018 __reg_bound_offset(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002019 return 0;
2020}
2021
2022static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
2023 struct bpf_insn *insn,
2024 struct bpf_reg_state *dst_reg,
2025 struct bpf_reg_state src_reg)
2026{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002027 struct bpf_reg_state *regs = cur_regs(env);
Edward Creef1174f72017-08-07 15:26:19 +01002028 u8 opcode = BPF_OP(insn->code);
2029 bool src_known, dst_known;
Edward Creeb03c9f92017-08-07 15:26:36 +01002030 s64 smin_val, smax_val;
2031 u64 umin_val, umax_val;
Edward Creef1174f72017-08-07 15:26:19 +01002032
2033 if (BPF_CLASS(insn->code) != BPF_ALU64) {
2034 /* 32-bit ALU ops are (32,32)->64 */
2035 coerce_reg_to_32(dst_reg);
2036 coerce_reg_to_32(&src_reg);
2037 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002038 smin_val = src_reg.smin_value;
2039 smax_val = src_reg.smax_value;
2040 umin_val = src_reg.umin_value;
2041 umax_val = src_reg.umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01002042 src_known = tnum_is_const(src_reg.var_off);
2043 dst_known = tnum_is_const(dst_reg->var_off);
2044
2045 switch (opcode) {
2046 case BPF_ADD:
Edward Creeb03c9f92017-08-07 15:26:36 +01002047 if (signed_add_overflows(dst_reg->smin_value, smin_val) ||
2048 signed_add_overflows(dst_reg->smax_value, smax_val)) {
2049 dst_reg->smin_value = S64_MIN;
2050 dst_reg->smax_value = S64_MAX;
2051 } else {
2052 dst_reg->smin_value += smin_val;
2053 dst_reg->smax_value += smax_val;
2054 }
2055 if (dst_reg->umin_value + umin_val < umin_val ||
2056 dst_reg->umax_value + umax_val < umax_val) {
2057 dst_reg->umin_value = 0;
2058 dst_reg->umax_value = U64_MAX;
2059 } else {
2060 dst_reg->umin_value += umin_val;
2061 dst_reg->umax_value += umax_val;
2062 }
Edward Creef1174f72017-08-07 15:26:19 +01002063 dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off);
2064 break;
2065 case BPF_SUB:
Edward Creeb03c9f92017-08-07 15:26:36 +01002066 if (signed_sub_overflows(dst_reg->smin_value, smax_val) ||
2067 signed_sub_overflows(dst_reg->smax_value, smin_val)) {
2068 /* Overflow possible, we know nothing */
2069 dst_reg->smin_value = S64_MIN;
2070 dst_reg->smax_value = S64_MAX;
2071 } else {
2072 dst_reg->smin_value -= smax_val;
2073 dst_reg->smax_value -= smin_val;
2074 }
2075 if (dst_reg->umin_value < umax_val) {
2076 /* Overflow possible, we know nothing */
2077 dst_reg->umin_value = 0;
2078 dst_reg->umax_value = U64_MAX;
2079 } else {
2080 /* Cannot overflow (as long as bounds are consistent) */
2081 dst_reg->umin_value -= umax_val;
2082 dst_reg->umax_value -= umin_val;
2083 }
Edward Creef1174f72017-08-07 15:26:19 +01002084 dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off);
Josef Bacik48461132016-09-28 10:54:32 -04002085 break;
2086 case BPF_MUL:
Edward Creeb03c9f92017-08-07 15:26:36 +01002087 dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off);
2088 if (smin_val < 0 || dst_reg->smin_value < 0) {
Edward Creef1174f72017-08-07 15:26:19 +01002089 /* Ain't nobody got time to multiply that sign */
Edward Creeb03c9f92017-08-07 15:26:36 +01002090 __mark_reg_unbounded(dst_reg);
2091 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002092 break;
2093 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002094 /* Both values are positive, so we can work with unsigned and
2095 * copy the result to signed (unless it exceeds S64_MAX).
Edward Creef1174f72017-08-07 15:26:19 +01002096 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002097 if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) {
2098 /* Potential overflow, we know nothing */
2099 __mark_reg_unbounded(dst_reg);
2100 /* (except what we can learn from the var_off) */
2101 __update_reg_bounds(dst_reg);
2102 break;
2103 }
2104 dst_reg->umin_value *= umin_val;
2105 dst_reg->umax_value *= umax_val;
2106 if (dst_reg->umax_value > S64_MAX) {
2107 /* Overflow possible, we know nothing */
2108 dst_reg->smin_value = S64_MIN;
2109 dst_reg->smax_value = S64_MAX;
2110 } else {
2111 dst_reg->smin_value = dst_reg->umin_value;
2112 dst_reg->smax_value = dst_reg->umax_value;
2113 }
Josef Bacik48461132016-09-28 10:54:32 -04002114 break;
2115 case BPF_AND:
Edward Creef1174f72017-08-07 15:26:19 +01002116 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01002117 __mark_reg_known(dst_reg, dst_reg->var_off.value &
2118 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01002119 break;
2120 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002121 /* We get our minimum from the var_off, since that's inherently
2122 * bitwise. Our maximum is the minimum of the operands' maxima.
Josef Bacikf23cc642016-11-14 15:45:36 -05002123 */
Edward Creef1174f72017-08-07 15:26:19 +01002124 dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01002125 dst_reg->umin_value = dst_reg->var_off.value;
2126 dst_reg->umax_value = min(dst_reg->umax_value, umax_val);
2127 if (dst_reg->smin_value < 0 || smin_val < 0) {
2128 /* Lose signed bounds when ANDing negative numbers,
2129 * ain't nobody got time for that.
2130 */
2131 dst_reg->smin_value = S64_MIN;
2132 dst_reg->smax_value = S64_MAX;
2133 } else {
2134 /* ANDing two positives gives a positive, so safe to
2135 * cast result into s64.
2136 */
2137 dst_reg->smin_value = dst_reg->umin_value;
2138 dst_reg->smax_value = dst_reg->umax_value;
2139 }
2140 /* We may learn something more from the var_off */
2141 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002142 break;
2143 case BPF_OR:
2144 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01002145 __mark_reg_known(dst_reg, dst_reg->var_off.value |
2146 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01002147 break;
2148 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002149 /* We get our maximum from the var_off, and our minimum is the
2150 * maximum of the operands' minima
Edward Creef1174f72017-08-07 15:26:19 +01002151 */
2152 dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01002153 dst_reg->umin_value = max(dst_reg->umin_value, umin_val);
2154 dst_reg->umax_value = dst_reg->var_off.value |
2155 dst_reg->var_off.mask;
2156 if (dst_reg->smin_value < 0 || smin_val < 0) {
2157 /* Lose signed bounds when ORing negative numbers,
2158 * ain't nobody got time for that.
2159 */
2160 dst_reg->smin_value = S64_MIN;
2161 dst_reg->smax_value = S64_MAX;
Edward Creef1174f72017-08-07 15:26:19 +01002162 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01002163 /* ORing two positives gives a positive, so safe to
2164 * cast result into s64.
2165 */
2166 dst_reg->smin_value = dst_reg->umin_value;
2167 dst_reg->smax_value = dst_reg->umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01002168 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002169 /* We may learn something more from the var_off */
2170 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002171 break;
2172 case BPF_LSH:
Edward Creeb03c9f92017-08-07 15:26:36 +01002173 if (umax_val > 63) {
2174 /* Shifts greater than 63 are undefined. This includes
2175 * shifts by a negative number.
2176 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002177 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002178 break;
2179 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002180 /* We lose all sign bit information (except what we can pick
2181 * up from var_off)
Josef Bacik48461132016-09-28 10:54:32 -04002182 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002183 dst_reg->smin_value = S64_MIN;
2184 dst_reg->smax_value = S64_MAX;
2185 /* If we might shift our top bit out, then we know nothing */
2186 if (dst_reg->umax_value > 1ULL << (63 - umax_val)) {
2187 dst_reg->umin_value = 0;
2188 dst_reg->umax_value = U64_MAX;
David S. Millerd1174412017-05-10 11:22:52 -07002189 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01002190 dst_reg->umin_value <<= umin_val;
2191 dst_reg->umax_value <<= umax_val;
David S. Millerd1174412017-05-10 11:22:52 -07002192 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002193 if (src_known)
2194 dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val);
2195 else
2196 dst_reg->var_off = tnum_lshift(tnum_unknown, umin_val);
2197 /* We may learn something more from the var_off */
2198 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002199 break;
2200 case BPF_RSH:
Edward Creeb03c9f92017-08-07 15:26:36 +01002201 if (umax_val > 63) {
2202 /* Shifts greater than 63 are undefined. This includes
2203 * shifts by a negative number.
2204 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002205 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002206 break;
2207 }
2208 /* BPF_RSH is an unsigned shift, so make the appropriate casts */
Edward Creeb03c9f92017-08-07 15:26:36 +01002209 if (dst_reg->smin_value < 0) {
2210 if (umin_val) {
Edward Creef1174f72017-08-07 15:26:19 +01002211 /* Sign bit will be cleared */
Edward Creeb03c9f92017-08-07 15:26:36 +01002212 dst_reg->smin_value = 0;
2213 } else {
2214 /* Lost sign bit information */
2215 dst_reg->smin_value = S64_MIN;
2216 dst_reg->smax_value = S64_MAX;
2217 }
David S. Millerd1174412017-05-10 11:22:52 -07002218 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01002219 dst_reg->smin_value =
2220 (u64)(dst_reg->smin_value) >> umax_val;
David S. Millerd1174412017-05-10 11:22:52 -07002221 }
Edward Creef1174f72017-08-07 15:26:19 +01002222 if (src_known)
Edward Creeb03c9f92017-08-07 15:26:36 +01002223 dst_reg->var_off = tnum_rshift(dst_reg->var_off,
2224 umin_val);
Edward Creef1174f72017-08-07 15:26:19 +01002225 else
Edward Creeb03c9f92017-08-07 15:26:36 +01002226 dst_reg->var_off = tnum_rshift(tnum_unknown, umin_val);
2227 dst_reg->umin_value >>= umax_val;
2228 dst_reg->umax_value >>= umin_val;
2229 /* We may learn something more from the var_off */
2230 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002231 break;
2232 default:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002233 mark_reg_unknown(env, regs, insn->dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002234 break;
2235 }
2236
Edward Creeb03c9f92017-08-07 15:26:36 +01002237 __reg_deduce_bounds(dst_reg);
2238 __reg_bound_offset(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002239 return 0;
2240}
2241
2242/* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max
2243 * and var_off.
2244 */
2245static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
2246 struct bpf_insn *insn)
2247{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002248 struct bpf_reg_state *regs = cur_regs(env), *dst_reg, *src_reg;
Edward Creef1174f72017-08-07 15:26:19 +01002249 struct bpf_reg_state *ptr_reg = NULL, off_reg = {0};
2250 u8 opcode = BPF_OP(insn->code);
2251 int rc;
2252
2253 dst_reg = &regs[insn->dst_reg];
Edward Creef1174f72017-08-07 15:26:19 +01002254 src_reg = NULL;
2255 if (dst_reg->type != SCALAR_VALUE)
2256 ptr_reg = dst_reg;
2257 if (BPF_SRC(insn->code) == BPF_X) {
2258 src_reg = &regs[insn->src_reg];
Edward Creef1174f72017-08-07 15:26:19 +01002259 if (src_reg->type != SCALAR_VALUE) {
2260 if (dst_reg->type != SCALAR_VALUE) {
2261 /* Combining two pointers by any ALU op yields
2262 * an arbitrary scalar.
2263 */
2264 if (!env->allow_ptr_leaks) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002265 verbose(env, "R%d pointer %s pointer prohibited\n",
Edward Creef1174f72017-08-07 15:26:19 +01002266 insn->dst_reg,
2267 bpf_alu_string[opcode >> 4]);
2268 return -EACCES;
2269 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002270 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002271 return 0;
2272 } else {
2273 /* scalar += pointer
2274 * This is legal, but we have to reverse our
2275 * src/dest handling in computing the range
2276 */
2277 rc = adjust_ptr_min_max_vals(env, insn,
2278 src_reg, dst_reg);
2279 if (rc == -EACCES && env->allow_ptr_leaks) {
2280 /* scalar += unknown scalar */
2281 __mark_reg_unknown(&off_reg);
2282 return adjust_scalar_min_max_vals(
2283 env, insn,
2284 dst_reg, off_reg);
2285 }
2286 return rc;
2287 }
2288 } else if (ptr_reg) {
2289 /* pointer += scalar */
2290 rc = adjust_ptr_min_max_vals(env, insn,
2291 dst_reg, src_reg);
2292 if (rc == -EACCES && env->allow_ptr_leaks) {
2293 /* unknown scalar += scalar */
2294 __mark_reg_unknown(dst_reg);
2295 return adjust_scalar_min_max_vals(
2296 env, insn, dst_reg, *src_reg);
2297 }
2298 return rc;
2299 }
2300 } else {
2301 /* Pretend the src is a reg with a known value, since we only
2302 * need to be able to read from this state.
2303 */
2304 off_reg.type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01002305 __mark_reg_known(&off_reg, insn->imm);
Edward Creef1174f72017-08-07 15:26:19 +01002306 src_reg = &off_reg;
Edward Creef1174f72017-08-07 15:26:19 +01002307 if (ptr_reg) { /* pointer += K */
2308 rc = adjust_ptr_min_max_vals(env, insn,
2309 ptr_reg, src_reg);
2310 if (rc == -EACCES && env->allow_ptr_leaks) {
2311 /* unknown scalar += K */
2312 __mark_reg_unknown(dst_reg);
2313 return adjust_scalar_min_max_vals(
2314 env, insn, dst_reg, off_reg);
2315 }
2316 return rc;
2317 }
2318 }
2319
2320 /* Got here implies adding two SCALAR_VALUEs */
2321 if (WARN_ON_ONCE(ptr_reg)) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002322 print_verifier_state(env, env->cur_state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002323 verbose(env, "verifier internal error: unexpected ptr_reg\n");
Edward Creef1174f72017-08-07 15:26:19 +01002324 return -EINVAL;
2325 }
2326 if (WARN_ON(!src_reg)) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002327 print_verifier_state(env, env->cur_state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002328 verbose(env, "verifier internal error: no src_reg\n");
Edward Creef1174f72017-08-07 15:26:19 +01002329 return -EINVAL;
2330 }
2331 return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002332}
2333
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002334/* check validity of 32-bit and 64-bit arithmetic operations */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002335static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002336{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002337 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002338 u8 opcode = BPF_OP(insn->code);
2339 int err;
2340
2341 if (opcode == BPF_END || opcode == BPF_NEG) {
2342 if (opcode == BPF_NEG) {
2343 if (BPF_SRC(insn->code) != 0 ||
2344 insn->src_reg != BPF_REG_0 ||
2345 insn->off != 0 || insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002346 verbose(env, "BPF_NEG uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002347 return -EINVAL;
2348 }
2349 } else {
2350 if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
Edward Creee67b8a62017-09-15 14:37:38 +01002351 (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) ||
2352 BPF_CLASS(insn->code) == BPF_ALU64) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002353 verbose(env, "BPF_END uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002354 return -EINVAL;
2355 }
2356 }
2357
2358 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01002359 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002360 if (err)
2361 return err;
2362
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002363 if (is_pointer_value(env, insn->dst_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002364 verbose(env, "R%d pointer arithmetic prohibited\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002365 insn->dst_reg);
2366 return -EACCES;
2367 }
2368
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002369 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01002370 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002371 if (err)
2372 return err;
2373
2374 } else if (opcode == BPF_MOV) {
2375
2376 if (BPF_SRC(insn->code) == BPF_X) {
2377 if (insn->imm != 0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002378 verbose(env, "BPF_MOV uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002379 return -EINVAL;
2380 }
2381
2382 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01002383 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002384 if (err)
2385 return err;
2386 } else {
2387 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002388 verbose(env, "BPF_MOV uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002389 return -EINVAL;
2390 }
2391 }
2392
2393 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01002394 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002395 if (err)
2396 return err;
2397
2398 if (BPF_SRC(insn->code) == BPF_X) {
2399 if (BPF_CLASS(insn->code) == BPF_ALU64) {
2400 /* case: R1 = R2
2401 * copy register state to dest reg
2402 */
2403 regs[insn->dst_reg] = regs[insn->src_reg];
Alexei Starovoitov8fe2d6c2017-10-05 16:20:56 -07002404 regs[insn->dst_reg].live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002405 } else {
Edward Creef1174f72017-08-07 15:26:19 +01002406 /* R1 = (u32) R2 */
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002407 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002408 verbose(env,
2409 "R%d partial copy of pointer\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002410 insn->src_reg);
2411 return -EACCES;
2412 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002413 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creeb03c9f92017-08-07 15:26:36 +01002414 /* high 32 bits are known zero. */
Edward Creef1174f72017-08-07 15:26:19 +01002415 regs[insn->dst_reg].var_off = tnum_cast(
2416 regs[insn->dst_reg].var_off, 4);
Edward Creeb03c9f92017-08-07 15:26:36 +01002417 __update_reg_bounds(&regs[insn->dst_reg]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002418 }
2419 } else {
2420 /* case: R = imm
2421 * remember the value we stored into this reg
2422 */
Edward Creef1174f72017-08-07 15:26:19 +01002423 regs[insn->dst_reg].type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01002424 __mark_reg_known(regs + insn->dst_reg, insn->imm);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002425 }
2426
2427 } else if (opcode > BPF_END) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002428 verbose(env, "invalid BPF_ALU opcode %x\n", opcode);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002429 return -EINVAL;
2430
2431 } else { /* all other ALU ops: and, sub, xor, add, ... */
2432
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002433 if (BPF_SRC(insn->code) == BPF_X) {
2434 if (insn->imm != 0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002435 verbose(env, "BPF_ALU uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002436 return -EINVAL;
2437 }
2438 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01002439 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002440 if (err)
2441 return err;
2442 } else {
2443 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002444 verbose(env, "BPF_ALU uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002445 return -EINVAL;
2446 }
2447 }
2448
2449 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01002450 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002451 if (err)
2452 return err;
2453
2454 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
2455 BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002456 verbose(env, "div by zero\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002457 return -EINVAL;
2458 }
2459
Rabin Vincent229394e82016-01-12 20:17:08 +01002460 if ((opcode == BPF_LSH || opcode == BPF_RSH ||
2461 opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
2462 int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
2463
2464 if (insn->imm < 0 || insn->imm >= size) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002465 verbose(env, "invalid shift %d\n", insn->imm);
Rabin Vincent229394e82016-01-12 20:17:08 +01002466 return -EINVAL;
2467 }
2468 }
2469
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002470 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01002471 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002472 if (err)
2473 return err;
2474
Edward Creef1174f72017-08-07 15:26:19 +01002475 return adjust_reg_min_max_vals(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002476 }
2477
2478 return 0;
2479}
2480
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002481static void find_good_pkt_pointers(struct bpf_verifier_state *state,
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002482 struct bpf_reg_state *dst_reg,
David S. Millerf8ddadc2017-10-22 13:36:53 +01002483 enum bpf_reg_type type,
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02002484 bool range_right_open)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002485{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002486 struct bpf_reg_state *regs = state->regs, *reg;
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02002487 u16 new_range;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002488 int i;
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002489
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02002490 if (dst_reg->off < 0 ||
2491 (dst_reg->off == 0 && range_right_open))
Edward Creef1174f72017-08-07 15:26:19 +01002492 /* This doesn't give us any range */
2493 return;
2494
Edward Creeb03c9f92017-08-07 15:26:36 +01002495 if (dst_reg->umax_value > MAX_PACKET_OFF ||
2496 dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF)
Edward Creef1174f72017-08-07 15:26:19 +01002497 /* Risk of overflow. For instance, ptr + (1<<63) may be less
2498 * than pkt_end, but that's because it's also less than pkt.
2499 */
2500 return;
2501
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02002502 new_range = dst_reg->off;
2503 if (range_right_open)
2504 new_range--;
2505
2506 /* Examples for register markings:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002507 *
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02002508 * pkt_data in dst register:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002509 *
2510 * r2 = r3;
2511 * r2 += 8;
2512 * if (r2 > pkt_end) goto <handle exception>
2513 * <access okay>
2514 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02002515 * r2 = r3;
2516 * r2 += 8;
2517 * if (r2 < pkt_end) goto <access okay>
2518 * <handle exception>
2519 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002520 * Where:
2521 * r2 == dst_reg, pkt_end == src_reg
2522 * r2=pkt(id=n,off=8,r=0)
2523 * r3=pkt(id=n,off=0,r=0)
2524 *
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02002525 * pkt_data in src register:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002526 *
2527 * r2 = r3;
2528 * r2 += 8;
2529 * if (pkt_end >= r2) goto <access okay>
2530 * <handle exception>
2531 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02002532 * r2 = r3;
2533 * r2 += 8;
2534 * if (pkt_end <= r2) goto <handle exception>
2535 * <access okay>
2536 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002537 * Where:
2538 * pkt_end == dst_reg, r2 == src_reg
2539 * r2=pkt(id=n,off=8,r=0)
2540 * r3=pkt(id=n,off=0,r=0)
2541 *
2542 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02002543 * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8)
2544 * and [r3, r3 + 8-1) respectively is safe to access depending on
2545 * the check.
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002546 */
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002547
Edward Creef1174f72017-08-07 15:26:19 +01002548 /* If our ids match, then we must have the same max_value. And we
2549 * don't care about the other reg's fixed offset, since if it's too big
2550 * the range won't allow anything.
2551 * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16.
2552 */
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002553 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002554 if (regs[i].type == type && regs[i].id == dst_reg->id)
Alexei Starovoitovb1977682017-03-24 15:57:33 -07002555 /* keep the maximum range already checked */
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02002556 regs[i].range = max(regs[i].range, new_range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002557
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002558 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
2559 if (state->stack[i].slot_type[0] != STACK_SPILL)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002560 continue;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002561 reg = &state->stack[i].spilled_ptr;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002562 if (reg->type == type && reg->id == dst_reg->id)
Daniel Borkmannb06723d2017-11-01 23:58:09 +01002563 reg->range = max(reg->range, new_range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002564 }
2565}
2566
Josef Bacik48461132016-09-28 10:54:32 -04002567/* Adjusts the register min/max values in the case that the dst_reg is the
2568 * variable register that we are working on, and src_reg is a constant or we're
2569 * simply doing a BPF_K check.
Edward Creef1174f72017-08-07 15:26:19 +01002570 * In JEQ/JNE cases we also adjust the var_off values.
Josef Bacik48461132016-09-28 10:54:32 -04002571 */
2572static void reg_set_min_max(struct bpf_reg_state *true_reg,
2573 struct bpf_reg_state *false_reg, u64 val,
2574 u8 opcode)
2575{
Edward Creef1174f72017-08-07 15:26:19 +01002576 /* If the dst_reg is a pointer, we can't learn anything about its
2577 * variable offset from the compare (unless src_reg were a pointer into
2578 * the same object, but we don't bother with that.
2579 * Since false_reg and true_reg have the same type by construction, we
2580 * only need to check one of them for pointerness.
2581 */
2582 if (__is_pointer_value(false, false_reg))
2583 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02002584
Josef Bacik48461132016-09-28 10:54:32 -04002585 switch (opcode) {
2586 case BPF_JEQ:
2587 /* If this is false then we know nothing Jon Snow, but if it is
2588 * true then we know for sure.
2589 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002590 __mark_reg_known(true_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04002591 break;
2592 case BPF_JNE:
2593 /* If this is true we know nothing Jon Snow, but if it is false
2594 * we know the value for sure;
2595 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002596 __mark_reg_known(false_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04002597 break;
2598 case BPF_JGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01002599 false_reg->umax_value = min(false_reg->umax_value, val);
2600 true_reg->umin_value = max(true_reg->umin_value, val + 1);
2601 break;
Josef Bacik48461132016-09-28 10:54:32 -04002602 case BPF_JSGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01002603 false_reg->smax_value = min_t(s64, false_reg->smax_value, val);
2604 true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1);
Josef Bacik48461132016-09-28 10:54:32 -04002605 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02002606 case BPF_JLT:
2607 false_reg->umin_value = max(false_reg->umin_value, val);
2608 true_reg->umax_value = min(true_reg->umax_value, val - 1);
2609 break;
2610 case BPF_JSLT:
2611 false_reg->smin_value = max_t(s64, false_reg->smin_value, val);
2612 true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1);
2613 break;
Josef Bacik48461132016-09-28 10:54:32 -04002614 case BPF_JGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01002615 false_reg->umax_value = min(false_reg->umax_value, val - 1);
2616 true_reg->umin_value = max(true_reg->umin_value, val);
2617 break;
Josef Bacik48461132016-09-28 10:54:32 -04002618 case BPF_JSGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01002619 false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1);
2620 true_reg->smin_value = max_t(s64, true_reg->smin_value, val);
Josef Bacik48461132016-09-28 10:54:32 -04002621 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02002622 case BPF_JLE:
2623 false_reg->umin_value = max(false_reg->umin_value, val + 1);
2624 true_reg->umax_value = min(true_reg->umax_value, val);
2625 break;
2626 case BPF_JSLE:
2627 false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1);
2628 true_reg->smax_value = min_t(s64, true_reg->smax_value, val);
2629 break;
Josef Bacik48461132016-09-28 10:54:32 -04002630 default:
2631 break;
2632 }
2633
Edward Creeb03c9f92017-08-07 15:26:36 +01002634 __reg_deduce_bounds(false_reg);
2635 __reg_deduce_bounds(true_reg);
2636 /* We might have learned some bits from the bounds. */
2637 __reg_bound_offset(false_reg);
2638 __reg_bound_offset(true_reg);
2639 /* Intersecting with the old var_off might have improved our bounds
2640 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
2641 * then new var_off is (0; 0x7f...fc) which improves our umax.
2642 */
2643 __update_reg_bounds(false_reg);
2644 __update_reg_bounds(true_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002645}
2646
Edward Creef1174f72017-08-07 15:26:19 +01002647/* Same as above, but for the case that dst_reg holds a constant and src_reg is
2648 * the variable reg.
Josef Bacik48461132016-09-28 10:54:32 -04002649 */
2650static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
2651 struct bpf_reg_state *false_reg, u64 val,
2652 u8 opcode)
2653{
Edward Creef1174f72017-08-07 15:26:19 +01002654 if (__is_pointer_value(false, false_reg))
2655 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02002656
Josef Bacik48461132016-09-28 10:54:32 -04002657 switch (opcode) {
2658 case BPF_JEQ:
2659 /* If this is false then we know nothing Jon Snow, but if it is
2660 * true then we know for sure.
2661 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002662 __mark_reg_known(true_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04002663 break;
2664 case BPF_JNE:
2665 /* If this is true we know nothing Jon Snow, but if it is false
2666 * we know the value for sure;
2667 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002668 __mark_reg_known(false_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04002669 break;
2670 case BPF_JGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01002671 true_reg->umax_value = min(true_reg->umax_value, val - 1);
2672 false_reg->umin_value = max(false_reg->umin_value, val);
2673 break;
Josef Bacik48461132016-09-28 10:54:32 -04002674 case BPF_JSGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01002675 true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1);
2676 false_reg->smin_value = max_t(s64, false_reg->smin_value, val);
Josef Bacik48461132016-09-28 10:54:32 -04002677 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02002678 case BPF_JLT:
2679 true_reg->umin_value = max(true_reg->umin_value, val + 1);
2680 false_reg->umax_value = min(false_reg->umax_value, val);
2681 break;
2682 case BPF_JSLT:
2683 true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1);
2684 false_reg->smax_value = min_t(s64, false_reg->smax_value, val);
2685 break;
Josef Bacik48461132016-09-28 10:54:32 -04002686 case BPF_JGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01002687 true_reg->umax_value = min(true_reg->umax_value, val);
2688 false_reg->umin_value = max(false_reg->umin_value, val + 1);
2689 break;
Josef Bacik48461132016-09-28 10:54:32 -04002690 case BPF_JSGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01002691 true_reg->smax_value = min_t(s64, true_reg->smax_value, val);
2692 false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1);
Josef Bacik48461132016-09-28 10:54:32 -04002693 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02002694 case BPF_JLE:
2695 true_reg->umin_value = max(true_reg->umin_value, val);
2696 false_reg->umax_value = min(false_reg->umax_value, val - 1);
2697 break;
2698 case BPF_JSLE:
2699 true_reg->smin_value = max_t(s64, true_reg->smin_value, val);
2700 false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1);
2701 break;
Josef Bacik48461132016-09-28 10:54:32 -04002702 default:
2703 break;
2704 }
2705
Edward Creeb03c9f92017-08-07 15:26:36 +01002706 __reg_deduce_bounds(false_reg);
2707 __reg_deduce_bounds(true_reg);
2708 /* We might have learned some bits from the bounds. */
2709 __reg_bound_offset(false_reg);
2710 __reg_bound_offset(true_reg);
2711 /* Intersecting with the old var_off might have improved our bounds
2712 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
2713 * then new var_off is (0; 0x7f...fc) which improves our umax.
2714 */
2715 __update_reg_bounds(false_reg);
2716 __update_reg_bounds(true_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002717}
2718
2719/* Regs are known to be equal, so intersect their min/max/var_off */
2720static void __reg_combine_min_max(struct bpf_reg_state *src_reg,
2721 struct bpf_reg_state *dst_reg)
2722{
Edward Creeb03c9f92017-08-07 15:26:36 +01002723 src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value,
2724 dst_reg->umin_value);
2725 src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value,
2726 dst_reg->umax_value);
2727 src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value,
2728 dst_reg->smin_value);
2729 src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value,
2730 dst_reg->smax_value);
Edward Creef1174f72017-08-07 15:26:19 +01002731 src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off,
2732 dst_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01002733 /* We might have learned new bounds from the var_off. */
2734 __update_reg_bounds(src_reg);
2735 __update_reg_bounds(dst_reg);
2736 /* We might have learned something about the sign bit. */
2737 __reg_deduce_bounds(src_reg);
2738 __reg_deduce_bounds(dst_reg);
2739 /* We might have learned some bits from the bounds. */
2740 __reg_bound_offset(src_reg);
2741 __reg_bound_offset(dst_reg);
2742 /* Intersecting with the old var_off might have improved our bounds
2743 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
2744 * then new var_off is (0; 0x7f...fc) which improves our umax.
2745 */
2746 __update_reg_bounds(src_reg);
2747 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002748}
2749
2750static void reg_combine_min_max(struct bpf_reg_state *true_src,
2751 struct bpf_reg_state *true_dst,
2752 struct bpf_reg_state *false_src,
2753 struct bpf_reg_state *false_dst,
2754 u8 opcode)
2755{
2756 switch (opcode) {
2757 case BPF_JEQ:
2758 __reg_combine_min_max(true_src, true_dst);
2759 break;
2760 case BPF_JNE:
2761 __reg_combine_min_max(false_src, false_dst);
Edward Creeb03c9f92017-08-07 15:26:36 +01002762 break;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02002763 }
Josef Bacik48461132016-09-28 10:54:32 -04002764}
2765
Thomas Graf57a09bf2016-10-18 19:51:19 +02002766static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id,
Edward Creef1174f72017-08-07 15:26:19 +01002767 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02002768{
2769 struct bpf_reg_state *reg = &regs[regno];
2770
2771 if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) {
Edward Creef1174f72017-08-07 15:26:19 +01002772 /* Old offset (both fixed and variable parts) should
2773 * have been known-zero, because we don't allow pointer
2774 * arithmetic on pointers that might be NULL.
2775 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002776 if (WARN_ON_ONCE(reg->smin_value || reg->smax_value ||
2777 !tnum_equals_const(reg->var_off, 0) ||
Edward Creef1174f72017-08-07 15:26:19 +01002778 reg->off)) {
Edward Creeb03c9f92017-08-07 15:26:36 +01002779 __mark_reg_known_zero(reg);
2780 reg->off = 0;
Edward Creef1174f72017-08-07 15:26:19 +01002781 }
2782 if (is_null) {
2783 reg->type = SCALAR_VALUE;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07002784 } else if (reg->map_ptr->inner_map_meta) {
2785 reg->type = CONST_PTR_TO_MAP;
2786 reg->map_ptr = reg->map_ptr->inner_map_meta;
2787 } else {
Edward Creef1174f72017-08-07 15:26:19 +01002788 reg->type = PTR_TO_MAP_VALUE;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07002789 }
Daniel Borkmanna08dd0d2016-12-15 01:30:06 +01002790 /* We don't need id from this point onwards anymore, thus we
2791 * should better reset it, so that state pruning has chances
2792 * to take effect.
2793 */
2794 reg->id = 0;
Thomas Graf57a09bf2016-10-18 19:51:19 +02002795 }
2796}
2797
2798/* The logic is similar to find_good_pkt_pointers(), both could eventually
2799 * be folded together at some point.
2800 */
2801static void mark_map_regs(struct bpf_verifier_state *state, u32 regno,
Edward Creef1174f72017-08-07 15:26:19 +01002802 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02002803{
2804 struct bpf_reg_state *regs = state->regs;
Daniel Borkmanna08dd0d2016-12-15 01:30:06 +01002805 u32 id = regs[regno].id;
Thomas Graf57a09bf2016-10-18 19:51:19 +02002806 int i;
2807
2808 for (i = 0; i < MAX_BPF_REG; i++)
Edward Creef1174f72017-08-07 15:26:19 +01002809 mark_map_reg(regs, i, id, is_null);
Thomas Graf57a09bf2016-10-18 19:51:19 +02002810
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002811 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
2812 if (state->stack[i].slot_type[0] != STACK_SPILL)
Thomas Graf57a09bf2016-10-18 19:51:19 +02002813 continue;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002814 mark_map_reg(&state->stack[i].spilled_ptr, 0, id, is_null);
Thomas Graf57a09bf2016-10-18 19:51:19 +02002815 }
2816}
2817
Daniel Borkmann5beca082017-11-01 23:58:10 +01002818static bool try_match_pkt_pointers(const struct bpf_insn *insn,
2819 struct bpf_reg_state *dst_reg,
2820 struct bpf_reg_state *src_reg,
2821 struct bpf_verifier_state *this_branch,
2822 struct bpf_verifier_state *other_branch)
2823{
2824 if (BPF_SRC(insn->code) != BPF_X)
2825 return false;
2826
2827 switch (BPF_OP(insn->code)) {
2828 case BPF_JGT:
2829 if ((dst_reg->type == PTR_TO_PACKET &&
2830 src_reg->type == PTR_TO_PACKET_END) ||
2831 (dst_reg->type == PTR_TO_PACKET_META &&
2832 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
2833 /* pkt_data' > pkt_end, pkt_meta' > pkt_data */
2834 find_good_pkt_pointers(this_branch, dst_reg,
2835 dst_reg->type, false);
2836 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
2837 src_reg->type == PTR_TO_PACKET) ||
2838 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
2839 src_reg->type == PTR_TO_PACKET_META)) {
2840 /* pkt_end > pkt_data', pkt_data > pkt_meta' */
2841 find_good_pkt_pointers(other_branch, src_reg,
2842 src_reg->type, true);
2843 } else {
2844 return false;
2845 }
2846 break;
2847 case BPF_JLT:
2848 if ((dst_reg->type == PTR_TO_PACKET &&
2849 src_reg->type == PTR_TO_PACKET_END) ||
2850 (dst_reg->type == PTR_TO_PACKET_META &&
2851 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
2852 /* pkt_data' < pkt_end, pkt_meta' < pkt_data */
2853 find_good_pkt_pointers(other_branch, dst_reg,
2854 dst_reg->type, true);
2855 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
2856 src_reg->type == PTR_TO_PACKET) ||
2857 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
2858 src_reg->type == PTR_TO_PACKET_META)) {
2859 /* pkt_end < pkt_data', pkt_data > pkt_meta' */
2860 find_good_pkt_pointers(this_branch, src_reg,
2861 src_reg->type, false);
2862 } else {
2863 return false;
2864 }
2865 break;
2866 case BPF_JGE:
2867 if ((dst_reg->type == PTR_TO_PACKET &&
2868 src_reg->type == PTR_TO_PACKET_END) ||
2869 (dst_reg->type == PTR_TO_PACKET_META &&
2870 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
2871 /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */
2872 find_good_pkt_pointers(this_branch, dst_reg,
2873 dst_reg->type, true);
2874 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
2875 src_reg->type == PTR_TO_PACKET) ||
2876 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
2877 src_reg->type == PTR_TO_PACKET_META)) {
2878 /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */
2879 find_good_pkt_pointers(other_branch, src_reg,
2880 src_reg->type, false);
2881 } else {
2882 return false;
2883 }
2884 break;
2885 case BPF_JLE:
2886 if ((dst_reg->type == PTR_TO_PACKET &&
2887 src_reg->type == PTR_TO_PACKET_END) ||
2888 (dst_reg->type == PTR_TO_PACKET_META &&
2889 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
2890 /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */
2891 find_good_pkt_pointers(other_branch, dst_reg,
2892 dst_reg->type, false);
2893 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
2894 src_reg->type == PTR_TO_PACKET) ||
2895 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
2896 src_reg->type == PTR_TO_PACKET_META)) {
2897 /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */
2898 find_good_pkt_pointers(this_branch, src_reg,
2899 src_reg->type, true);
2900 } else {
2901 return false;
2902 }
2903 break;
2904 default:
2905 return false;
2906 }
2907
2908 return true;
2909}
2910
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002911static int check_cond_jmp_op(struct bpf_verifier_env *env,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002912 struct bpf_insn *insn, int *insn_idx)
2913{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002914 struct bpf_verifier_state *other_branch, *this_branch = env->cur_state;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002915 struct bpf_reg_state *regs = this_branch->regs, *dst_reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002916 u8 opcode = BPF_OP(insn->code);
2917 int err;
2918
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02002919 if (opcode > BPF_JSLE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002920 verbose(env, "invalid BPF_JMP opcode %x\n", opcode);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002921 return -EINVAL;
2922 }
2923
2924 if (BPF_SRC(insn->code) == BPF_X) {
2925 if (insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002926 verbose(env, "BPF_JMP uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002927 return -EINVAL;
2928 }
2929
2930 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01002931 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002932 if (err)
2933 return err;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002934
2935 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002936 verbose(env, "R%d pointer comparison prohibited\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002937 insn->src_reg);
2938 return -EACCES;
2939 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002940 } else {
2941 if (insn->src_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002942 verbose(env, "BPF_JMP uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002943 return -EINVAL;
2944 }
2945 }
2946
2947 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01002948 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002949 if (err)
2950 return err;
2951
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002952 dst_reg = &regs[insn->dst_reg];
2953
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002954 /* detect if R == 0 where R was initialized to zero earlier */
2955 if (BPF_SRC(insn->code) == BPF_K &&
2956 (opcode == BPF_JEQ || opcode == BPF_JNE) &&
Edward Creef1174f72017-08-07 15:26:19 +01002957 dst_reg->type == SCALAR_VALUE &&
Alexei Starovoitov3bf15922017-11-30 21:31:39 -08002958 tnum_is_const(dst_reg->var_off)) {
2959 if ((opcode == BPF_JEQ && dst_reg->var_off.value == insn->imm) ||
2960 (opcode == BPF_JNE && dst_reg->var_off.value != insn->imm)) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002961 /* if (imm == imm) goto pc+off;
2962 * only follow the goto, ignore fall-through
2963 */
2964 *insn_idx += insn->off;
2965 return 0;
2966 } else {
2967 /* if (imm != imm) goto pc+off;
2968 * only follow fall-through branch, since
2969 * that's where the program will go
2970 */
2971 return 0;
2972 }
2973 }
2974
2975 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx);
2976 if (!other_branch)
2977 return -EFAULT;
2978
Josef Bacik48461132016-09-28 10:54:32 -04002979 /* detect if we are comparing against a constant value so we can adjust
2980 * our min/max values for our dst register.
Edward Creef1174f72017-08-07 15:26:19 +01002981 * this is only legit if both are scalars (or pointers to the same
2982 * object, I suppose, but we don't support that right now), because
2983 * otherwise the different base pointers mean the offsets aren't
2984 * comparable.
Josef Bacik48461132016-09-28 10:54:32 -04002985 */
2986 if (BPF_SRC(insn->code) == BPF_X) {
Edward Creef1174f72017-08-07 15:26:19 +01002987 if (dst_reg->type == SCALAR_VALUE &&
2988 regs[insn->src_reg].type == SCALAR_VALUE) {
2989 if (tnum_is_const(regs[insn->src_reg].var_off))
2990 reg_set_min_max(&other_branch->regs[insn->dst_reg],
2991 dst_reg, regs[insn->src_reg].var_off.value,
2992 opcode);
2993 else if (tnum_is_const(dst_reg->var_off))
2994 reg_set_min_max_inv(&other_branch->regs[insn->src_reg],
2995 &regs[insn->src_reg],
2996 dst_reg->var_off.value, opcode);
2997 else if (opcode == BPF_JEQ || opcode == BPF_JNE)
2998 /* Comparing for equality, we can combine knowledge */
2999 reg_combine_min_max(&other_branch->regs[insn->src_reg],
3000 &other_branch->regs[insn->dst_reg],
3001 &regs[insn->src_reg],
3002 &regs[insn->dst_reg], opcode);
3003 }
3004 } else if (dst_reg->type == SCALAR_VALUE) {
Josef Bacik48461132016-09-28 10:54:32 -04003005 reg_set_min_max(&other_branch->regs[insn->dst_reg],
3006 dst_reg, insn->imm, opcode);
3007 }
3008
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003009 /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003010 if (BPF_SRC(insn->code) == BPF_K &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003011 insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
3012 dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
Thomas Graf57a09bf2016-10-18 19:51:19 +02003013 /* Mark all identical map registers in each branch as either
3014 * safe or unknown depending R == 0 or R != 0 conditional.
3015 */
Edward Creef1174f72017-08-07 15:26:19 +01003016 mark_map_regs(this_branch, insn->dst_reg, opcode == BPF_JNE);
3017 mark_map_regs(other_branch, insn->dst_reg, opcode == BPF_JEQ);
Daniel Borkmann5beca082017-11-01 23:58:10 +01003018 } else if (!try_match_pkt_pointers(insn, dst_reg, &regs[insn->src_reg],
3019 this_branch, other_branch) &&
3020 is_pointer_value(env, insn->dst_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003021 verbose(env, "R%d pointer comparison prohibited\n",
3022 insn->dst_reg);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003023 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003024 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003025 if (env->log.level)
3026 print_verifier_state(env, this_branch);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003027 return 0;
3028}
3029
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003030/* return the map pointer stored inside BPF_LD_IMM64 instruction */
3031static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
3032{
3033 u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
3034
3035 return (struct bpf_map *) (unsigned long) imm64;
3036}
3037
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003038/* verify BPF_LD_IMM64 instruction */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003039static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003040{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003041 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003042 int err;
3043
3044 if (BPF_SIZE(insn->code) != BPF_DW) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003045 verbose(env, "invalid BPF_LD_IMM insn\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003046 return -EINVAL;
3047 }
3048 if (insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003049 verbose(env, "BPF_LD_IMM64 uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003050 return -EINVAL;
3051 }
3052
Edward Creedc503a82017-08-15 20:34:35 +01003053 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003054 if (err)
3055 return err;
3056
Jakub Kicinski6b173872016-09-21 11:43:59 +01003057 if (insn->src_reg == 0) {
Jakub Kicinski6b173872016-09-21 11:43:59 +01003058 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
3059
Edward Creef1174f72017-08-07 15:26:19 +01003060 regs[insn->dst_reg].type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01003061 __mark_reg_known(&regs[insn->dst_reg], imm);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003062 return 0;
Jakub Kicinski6b173872016-09-21 11:43:59 +01003063 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003064
3065 /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
3066 BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
3067
3068 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
3069 regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
3070 return 0;
3071}
3072
Daniel Borkmann96be4322015-03-01 12:31:46 +01003073static bool may_access_skb(enum bpf_prog_type type)
3074{
3075 switch (type) {
3076 case BPF_PROG_TYPE_SOCKET_FILTER:
3077 case BPF_PROG_TYPE_SCHED_CLS:
Daniel Borkmann94caee8c2015-03-20 15:11:11 +01003078 case BPF_PROG_TYPE_SCHED_ACT:
Daniel Borkmann96be4322015-03-01 12:31:46 +01003079 return true;
3080 default:
3081 return false;
3082 }
3083}
3084
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003085/* verify safety of LD_ABS|LD_IND instructions:
3086 * - they can only appear in the programs where ctx == skb
3087 * - since they are wrappers of function calls, they scratch R1-R5 registers,
3088 * preserve R6-R9, and store return value into R0
3089 *
3090 * Implicit input:
3091 * ctx == skb == R6 == CTX
3092 *
3093 * Explicit input:
3094 * SRC == any register
3095 * IMM == 32-bit immediate
3096 *
3097 * Output:
3098 * R0 - 8/16/32-bit skb data converted to cpu endianness
3099 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003100static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003101{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003102 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003103 u8 mode = BPF_MODE(insn->code);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003104 int i, err;
3105
Daniel Borkmann24701ec2015-03-01 12:31:47 +01003106 if (!may_access_skb(env->prog->type)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003107 verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003108 return -EINVAL;
3109 }
3110
3111 if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
Alexei Starovoitovd82bccc2016-04-12 10:26:19 -07003112 BPF_SIZE(insn->code) == BPF_DW ||
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003113 (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003114 verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003115 return -EINVAL;
3116 }
3117
3118 /* check whether implicit source operand (register R6) is readable */
Edward Creedc503a82017-08-15 20:34:35 +01003119 err = check_reg_arg(env, BPF_REG_6, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003120 if (err)
3121 return err;
3122
3123 if (regs[BPF_REG_6].type != PTR_TO_CTX) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003124 verbose(env,
3125 "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003126 return -EINVAL;
3127 }
3128
3129 if (mode == BPF_IND) {
3130 /* check explicit source operand */
Edward Creedc503a82017-08-15 20:34:35 +01003131 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003132 if (err)
3133 return err;
3134 }
3135
3136 /* reset caller saved regs to unreadable */
Edward Creedc503a82017-08-15 20:34:35 +01003137 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003138 mark_reg_not_init(env, regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01003139 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
3140 }
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003141
3142 /* mark destination R0 register as readable, since it contains
Edward Creedc503a82017-08-15 20:34:35 +01003143 * the value fetched from the packet.
3144 * Already marked as written above.
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003145 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003146 mark_reg_unknown(env, regs, BPF_REG_0);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003147 return 0;
3148}
3149
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003150static int check_return_code(struct bpf_verifier_env *env)
3151{
3152 struct bpf_reg_state *reg;
3153 struct tnum range = tnum_range(0, 1);
3154
3155 switch (env->prog->type) {
3156 case BPF_PROG_TYPE_CGROUP_SKB:
3157 case BPF_PROG_TYPE_CGROUP_SOCK:
3158 case BPF_PROG_TYPE_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05003159 case BPF_PROG_TYPE_CGROUP_DEVICE:
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003160 break;
3161 default:
3162 return 0;
3163 }
3164
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003165 reg = cur_regs(env) + BPF_REG_0;
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003166 if (reg->type != SCALAR_VALUE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003167 verbose(env, "At program exit the register R0 is not a known value (%s)\n",
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003168 reg_type_str[reg->type]);
3169 return -EINVAL;
3170 }
3171
3172 if (!tnum_in(range, reg->var_off)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003173 verbose(env, "At program exit the register R0 ");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003174 if (!tnum_is_unknown(reg->var_off)) {
3175 char tn_buf[48];
3176
3177 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003178 verbose(env, "has value %s", tn_buf);
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003179 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003180 verbose(env, "has unknown scalar value");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003181 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003182 verbose(env, " should have been 0 or 1\n");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003183 return -EINVAL;
3184 }
3185 return 0;
3186}
3187
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003188/* non-recursive DFS pseudo code
3189 * 1 procedure DFS-iterative(G,v):
3190 * 2 label v as discovered
3191 * 3 let S be a stack
3192 * 4 S.push(v)
3193 * 5 while S is not empty
3194 * 6 t <- S.pop()
3195 * 7 if t is what we're looking for:
3196 * 8 return t
3197 * 9 for all edges e in G.adjacentEdges(t) do
3198 * 10 if edge e is already labelled
3199 * 11 continue with the next edge
3200 * 12 w <- G.adjacentVertex(t,e)
3201 * 13 if vertex w is not discovered and not explored
3202 * 14 label e as tree-edge
3203 * 15 label w as discovered
3204 * 16 S.push(w)
3205 * 17 continue at 5
3206 * 18 else if vertex w is discovered
3207 * 19 label e as back-edge
3208 * 20 else
3209 * 21 // vertex w is explored
3210 * 22 label e as forward- or cross-edge
3211 * 23 label t as explored
3212 * 24 S.pop()
3213 *
3214 * convention:
3215 * 0x10 - discovered
3216 * 0x11 - discovered and fall-through edge labelled
3217 * 0x12 - discovered and fall-through and branch edges labelled
3218 * 0x20 - explored
3219 */
3220
3221enum {
3222 DISCOVERED = 0x10,
3223 EXPLORED = 0x20,
3224 FALLTHROUGH = 1,
3225 BRANCH = 2,
3226};
3227
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003228#define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003229
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003230static int *insn_stack; /* stack of insns to process */
3231static int cur_stack; /* current stack index */
3232static int *insn_state;
3233
3234/* t, w, e - match pseudo-code above:
3235 * t - index of current instruction
3236 * w - next instruction
3237 * e - edge
3238 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003239static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003240{
3241 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
3242 return 0;
3243
3244 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
3245 return 0;
3246
3247 if (w < 0 || w >= env->prog->len) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003248 verbose(env, "jump out of range from insn %d to %d\n", t, w);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003249 return -EINVAL;
3250 }
3251
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003252 if (e == BRANCH)
3253 /* mark branch target for state pruning */
3254 env->explored_states[w] = STATE_LIST_MARK;
3255
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003256 if (insn_state[w] == 0) {
3257 /* tree-edge */
3258 insn_state[t] = DISCOVERED | e;
3259 insn_state[w] = DISCOVERED;
3260 if (cur_stack >= env->prog->len)
3261 return -E2BIG;
3262 insn_stack[cur_stack++] = w;
3263 return 1;
3264 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003265 verbose(env, "back-edge from insn %d to %d\n", t, w);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003266 return -EINVAL;
3267 } else if (insn_state[w] == EXPLORED) {
3268 /* forward- or cross-edge */
3269 insn_state[t] = DISCOVERED | e;
3270 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003271 verbose(env, "insn state internal bug\n");
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003272 return -EFAULT;
3273 }
3274 return 0;
3275}
3276
3277/* non-recursive depth-first-search to detect loops in BPF program
3278 * loop == back-edge in directed graph
3279 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003280static int check_cfg(struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003281{
3282 struct bpf_insn *insns = env->prog->insnsi;
3283 int insn_cnt = env->prog->len;
3284 int ret = 0;
3285 int i, t;
3286
3287 insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
3288 if (!insn_state)
3289 return -ENOMEM;
3290
3291 insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
3292 if (!insn_stack) {
3293 kfree(insn_state);
3294 return -ENOMEM;
3295 }
3296
3297 insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
3298 insn_stack[0] = 0; /* 0 is the first instruction */
3299 cur_stack = 1;
3300
3301peek_stack:
3302 if (cur_stack == 0)
3303 goto check_state;
3304 t = insn_stack[cur_stack - 1];
3305
3306 if (BPF_CLASS(insns[t].code) == BPF_JMP) {
3307 u8 opcode = BPF_OP(insns[t].code);
3308
3309 if (opcode == BPF_EXIT) {
3310 goto mark_explored;
3311 } else if (opcode == BPF_CALL) {
3312 ret = push_insn(t, t + 1, FALLTHROUGH, env);
3313 if (ret == 1)
3314 goto peek_stack;
3315 else if (ret < 0)
3316 goto err_free;
Daniel Borkmann07016152016-04-05 22:33:17 +02003317 if (t + 1 < insn_cnt)
3318 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003319 } else if (opcode == BPF_JA) {
3320 if (BPF_SRC(insns[t].code) != BPF_K) {
3321 ret = -EINVAL;
3322 goto err_free;
3323 }
3324 /* unconditional jump with single edge */
3325 ret = push_insn(t, t + insns[t].off + 1,
3326 FALLTHROUGH, env);
3327 if (ret == 1)
3328 goto peek_stack;
3329 else if (ret < 0)
3330 goto err_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003331 /* tell verifier to check for equivalent states
3332 * after every call and jump
3333 */
Alexei Starovoitovc3de6312015-04-14 15:57:13 -07003334 if (t + 1 < insn_cnt)
3335 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003336 } else {
3337 /* conditional jump with two edges */
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02003338 env->explored_states[t] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003339 ret = push_insn(t, t + 1, FALLTHROUGH, env);
3340 if (ret == 1)
3341 goto peek_stack;
3342 else if (ret < 0)
3343 goto err_free;
3344
3345 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
3346 if (ret == 1)
3347 goto peek_stack;
3348 else if (ret < 0)
3349 goto err_free;
3350 }
3351 } else {
3352 /* all other non-branch instructions with single
3353 * fall-through edge
3354 */
3355 ret = push_insn(t, t + 1, FALLTHROUGH, env);
3356 if (ret == 1)
3357 goto peek_stack;
3358 else if (ret < 0)
3359 goto err_free;
3360 }
3361
3362mark_explored:
3363 insn_state[t] = EXPLORED;
3364 if (cur_stack-- <= 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003365 verbose(env, "pop stack internal bug\n");
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003366 ret = -EFAULT;
3367 goto err_free;
3368 }
3369 goto peek_stack;
3370
3371check_state:
3372 for (i = 0; i < insn_cnt; i++) {
3373 if (insn_state[i] != EXPLORED) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003374 verbose(env, "unreachable insn %d\n", i);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003375 ret = -EINVAL;
3376 goto err_free;
3377 }
3378 }
3379 ret = 0; /* cfg looks good */
3380
3381err_free:
3382 kfree(insn_state);
3383 kfree(insn_stack);
3384 return ret;
3385}
3386
Edward Creef1174f72017-08-07 15:26:19 +01003387/* check %cur's range satisfies %old's */
3388static bool range_within(struct bpf_reg_state *old,
3389 struct bpf_reg_state *cur)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003390{
Edward Creeb03c9f92017-08-07 15:26:36 +01003391 return old->umin_value <= cur->umin_value &&
3392 old->umax_value >= cur->umax_value &&
3393 old->smin_value <= cur->smin_value &&
3394 old->smax_value >= cur->smax_value;
Edward Creef1174f72017-08-07 15:26:19 +01003395}
3396
3397/* Maximum number of register states that can exist at once */
3398#define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE)
3399struct idpair {
3400 u32 old;
3401 u32 cur;
3402};
3403
3404/* If in the old state two registers had the same id, then they need to have
3405 * the same id in the new state as well. But that id could be different from
3406 * the old state, so we need to track the mapping from old to new ids.
3407 * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent
3408 * regs with old id 5 must also have new id 9 for the new state to be safe. But
3409 * regs with a different old id could still have new id 9, we don't care about
3410 * that.
3411 * So we look through our idmap to see if this old id has been seen before. If
3412 * so, we require the new id to match; otherwise, we add the id pair to the map.
3413 */
3414static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap)
3415{
3416 unsigned int i;
3417
3418 for (i = 0; i < ID_MAP_SIZE; i++) {
3419 if (!idmap[i].old) {
3420 /* Reached an empty slot; haven't seen this id before */
3421 idmap[i].old = old_id;
3422 idmap[i].cur = cur_id;
3423 return true;
3424 }
3425 if (idmap[i].old == old_id)
3426 return idmap[i].cur == cur_id;
3427 }
3428 /* We ran out of idmap slots, which should be impossible */
3429 WARN_ON_ONCE(1);
3430 return false;
3431}
3432
3433/* Returns true if (rold safe implies rcur safe) */
Edward Cree1b688a12017-08-23 15:10:50 +01003434static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
3435 struct idpair *idmap)
Edward Creef1174f72017-08-07 15:26:19 +01003436{
Edward Creedc503a82017-08-15 20:34:35 +01003437 if (!(rold->live & REG_LIVE_READ))
3438 /* explored state didn't use this */
3439 return true;
3440
3441 if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, live)) == 0)
Edward Creef1174f72017-08-07 15:26:19 +01003442 return true;
3443
3444 if (rold->type == NOT_INIT)
3445 /* explored state can't have used this */
3446 return true;
3447 if (rcur->type == NOT_INIT)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003448 return false;
Edward Creef1174f72017-08-07 15:26:19 +01003449 switch (rold->type) {
3450 case SCALAR_VALUE:
3451 if (rcur->type == SCALAR_VALUE) {
3452 /* new val must satisfy old val knowledge */
3453 return range_within(rold, rcur) &&
3454 tnum_in(rold->var_off, rcur->var_off);
3455 } else {
3456 /* if we knew anything about the old value, we're not
3457 * equal, because we can't know anything about the
3458 * scalar value of the pointer in the new value.
3459 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003460 return rold->umin_value == 0 &&
3461 rold->umax_value == U64_MAX &&
3462 rold->smin_value == S64_MIN &&
3463 rold->smax_value == S64_MAX &&
Edward Creef1174f72017-08-07 15:26:19 +01003464 tnum_is_unknown(rold->var_off);
3465 }
3466 case PTR_TO_MAP_VALUE:
Edward Cree1b688a12017-08-23 15:10:50 +01003467 /* If the new min/max/var_off satisfy the old ones and
3468 * everything else matches, we are OK.
3469 * We don't care about the 'id' value, because nothing
3470 * uses it for PTR_TO_MAP_VALUE (only for ..._OR_NULL)
3471 */
3472 return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 &&
3473 range_within(rold, rcur) &&
3474 tnum_in(rold->var_off, rcur->var_off);
Edward Creef1174f72017-08-07 15:26:19 +01003475 case PTR_TO_MAP_VALUE_OR_NULL:
3476 /* a PTR_TO_MAP_VALUE could be safe to use as a
3477 * PTR_TO_MAP_VALUE_OR_NULL into the same map.
3478 * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL-
3479 * checked, doing so could have affected others with the same
3480 * id, and we can't check for that because we lost the id when
3481 * we converted to a PTR_TO_MAP_VALUE.
3482 */
3483 if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL)
3484 return false;
3485 if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)))
3486 return false;
3487 /* Check our ids match any regs they're supposed to */
3488 return check_ids(rold->id, rcur->id, idmap);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003489 case PTR_TO_PACKET_META:
Edward Creef1174f72017-08-07 15:26:19 +01003490 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003491 if (rcur->type != rold->type)
Edward Creef1174f72017-08-07 15:26:19 +01003492 return false;
3493 /* We must have at least as much range as the old ptr
3494 * did, so that any accesses which were safe before are
3495 * still safe. This is true even if old range < old off,
3496 * since someone could have accessed through (ptr - k), or
3497 * even done ptr -= k in a register, to get a safe access.
3498 */
3499 if (rold->range > rcur->range)
3500 return false;
3501 /* If the offsets don't match, we can't trust our alignment;
3502 * nor can we be sure that we won't fall out of range.
3503 */
3504 if (rold->off != rcur->off)
3505 return false;
3506 /* id relations must be preserved */
3507 if (rold->id && !check_ids(rold->id, rcur->id, idmap))
3508 return false;
3509 /* new val must satisfy old val knowledge */
3510 return range_within(rold, rcur) &&
3511 tnum_in(rold->var_off, rcur->var_off);
3512 case PTR_TO_CTX:
3513 case CONST_PTR_TO_MAP:
3514 case PTR_TO_STACK:
3515 case PTR_TO_PACKET_END:
3516 /* Only valid matches are exact, which memcmp() above
3517 * would have accepted
3518 */
3519 default:
3520 /* Don't know what's going on, just say it's not safe */
3521 return false;
3522 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003523
Edward Creef1174f72017-08-07 15:26:19 +01003524 /* Shouldn't get here; if we do, say it's not safe */
3525 WARN_ON_ONCE(1);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003526 return false;
3527}
3528
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003529static bool stacksafe(struct bpf_verifier_state *old,
3530 struct bpf_verifier_state *cur,
3531 struct idpair *idmap)
3532{
3533 int i, spi;
3534
3535 /* if explored stack has more populated slots than current stack
3536 * such stacks are not equivalent
3537 */
3538 if (old->allocated_stack > cur->allocated_stack)
3539 return false;
3540
3541 /* walk slots of the explored stack and ignore any additional
3542 * slots in the current stack, since explored(safe) state
3543 * didn't use them
3544 */
3545 for (i = 0; i < old->allocated_stack; i++) {
3546 spi = i / BPF_REG_SIZE;
3547
3548 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID)
3549 continue;
3550 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] !=
3551 cur->stack[spi].slot_type[i % BPF_REG_SIZE])
3552 /* Ex: old explored (safe) state has STACK_SPILL in
3553 * this stack slot, but current has has STACK_MISC ->
3554 * this verifier states are not equivalent,
3555 * return false to continue verification of this path
3556 */
3557 return false;
3558 if (i % BPF_REG_SIZE)
3559 continue;
3560 if (old->stack[spi].slot_type[0] != STACK_SPILL)
3561 continue;
3562 if (!regsafe(&old->stack[spi].spilled_ptr,
3563 &cur->stack[spi].spilled_ptr,
3564 idmap))
3565 /* when explored and current stack slot are both storing
3566 * spilled registers, check that stored pointers types
3567 * are the same as well.
3568 * Ex: explored safe path could have stored
3569 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8}
3570 * but current path has stored:
3571 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16}
3572 * such verifier states are not equivalent.
3573 * return false to continue verification of this path
3574 */
3575 return false;
3576 }
3577 return true;
3578}
3579
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003580/* compare two verifier states
3581 *
3582 * all states stored in state_list are known to be valid, since
3583 * verifier reached 'bpf_exit' instruction through them
3584 *
3585 * this function is called when verifier exploring different branches of
3586 * execution popped from the state stack. If it sees an old state that has
3587 * more strict register state and more strict stack state then this execution
3588 * branch doesn't need to be explored further, since verifier already
3589 * concluded that more strict state leads to valid finish.
3590 *
3591 * Therefore two states are equivalent if register state is more conservative
3592 * and explored stack state is more conservative than the current one.
3593 * Example:
3594 * explored current
3595 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
3596 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
3597 *
3598 * In other words if current stack state (one being explored) has more
3599 * valid slots than old one that already passed validation, it means
3600 * the verifier can stop exploring and conclude that current state is valid too
3601 *
3602 * Similarly with registers. If explored state has register type as invalid
3603 * whereas register type in current state is meaningful, it means that
3604 * the current state will reach 'bpf_exit' instruction safely
3605 */
Josef Bacik48461132016-09-28 10:54:32 -04003606static bool states_equal(struct bpf_verifier_env *env,
3607 struct bpf_verifier_state *old,
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003608 struct bpf_verifier_state *cur)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003609{
Edward Creef1174f72017-08-07 15:26:19 +01003610 struct idpair *idmap;
3611 bool ret = false;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003612 int i;
3613
Edward Creef1174f72017-08-07 15:26:19 +01003614 idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL);
3615 /* If we failed to allocate the idmap, just say it's not safe */
3616 if (!idmap)
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003617 return false;
Edward Creef1174f72017-08-07 15:26:19 +01003618
3619 for (i = 0; i < MAX_BPF_REG; i++) {
Edward Cree1b688a12017-08-23 15:10:50 +01003620 if (!regsafe(&old->regs[i], &cur->regs[i], idmap))
Edward Creef1174f72017-08-07 15:26:19 +01003621 goto out_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003622 }
3623
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003624 if (!stacksafe(old, cur, idmap))
3625 goto out_free;
Edward Creef1174f72017-08-07 15:26:19 +01003626 ret = true;
3627out_free:
3628 kfree(idmap);
3629 return ret;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003630}
3631
Edward Cree8e9cd9c2017-08-23 15:11:21 +01003632/* A write screens off any subsequent reads; but write marks come from the
3633 * straight-line code between a state and its parent. When we arrive at a
3634 * jump target (in the first iteration of the propagate_liveness() loop),
3635 * we didn't arrive by the straight-line code, so read marks in state must
3636 * propagate to parent regardless of state's write marks.
3637 */
Edward Creedc503a82017-08-15 20:34:35 +01003638static bool do_propagate_liveness(const struct bpf_verifier_state *state,
3639 struct bpf_verifier_state *parent)
3640{
Edward Cree63f45f82017-08-23 15:10:03 +01003641 bool writes = parent == state->parent; /* Observe write marks */
Edward Creedc503a82017-08-15 20:34:35 +01003642 bool touched = false; /* any changes made? */
3643 int i;
3644
3645 if (!parent)
3646 return touched;
3647 /* Propagate read liveness of registers... */
3648 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
3649 /* We don't need to worry about FP liveness because it's read-only */
3650 for (i = 0; i < BPF_REG_FP; i++) {
3651 if (parent->regs[i].live & REG_LIVE_READ)
3652 continue;
Edward Cree63f45f82017-08-23 15:10:03 +01003653 if (writes && (state->regs[i].live & REG_LIVE_WRITTEN))
3654 continue;
3655 if (state->regs[i].live & REG_LIVE_READ) {
Edward Creedc503a82017-08-15 20:34:35 +01003656 parent->regs[i].live |= REG_LIVE_READ;
3657 touched = true;
3658 }
3659 }
3660 /* ... and stack slots */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003661 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE &&
3662 i < parent->allocated_stack / BPF_REG_SIZE; i++) {
3663 if (parent->stack[i].slot_type[0] != STACK_SPILL)
Edward Creedc503a82017-08-15 20:34:35 +01003664 continue;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003665 if (state->stack[i].slot_type[0] != STACK_SPILL)
Edward Creedc503a82017-08-15 20:34:35 +01003666 continue;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003667 if (parent->stack[i].spilled_ptr.live & REG_LIVE_READ)
Edward Creedc503a82017-08-15 20:34:35 +01003668 continue;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003669 if (writes &&
3670 (state->stack[i].spilled_ptr.live & REG_LIVE_WRITTEN))
Edward Cree63f45f82017-08-23 15:10:03 +01003671 continue;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003672 if (state->stack[i].spilled_ptr.live & REG_LIVE_READ) {
3673 parent->stack[i].spilled_ptr.live |= REG_LIVE_READ;
Edward Creedc503a82017-08-15 20:34:35 +01003674 touched = true;
3675 }
3676 }
3677 return touched;
3678}
3679
Edward Cree8e9cd9c2017-08-23 15:11:21 +01003680/* "parent" is "a state from which we reach the current state", but initially
3681 * it is not the state->parent (i.e. "the state whose straight-line code leads
3682 * to the current state"), instead it is the state that happened to arrive at
3683 * a (prunable) equivalent of the current state. See comment above
3684 * do_propagate_liveness() for consequences of this.
3685 * This function is just a more efficient way of calling mark_reg_read() or
3686 * mark_stack_slot_read() on each reg in "parent" that is read in "state",
3687 * though it requires that parent != state->parent in the call arguments.
3688 */
Edward Creedc503a82017-08-15 20:34:35 +01003689static void propagate_liveness(const struct bpf_verifier_state *state,
3690 struct bpf_verifier_state *parent)
3691{
3692 while (do_propagate_liveness(state, parent)) {
3693 /* Something changed, so we need to feed those changes onward */
3694 state = parent;
3695 parent = state->parent;
3696 }
3697}
3698
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003699static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003700{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003701 struct bpf_verifier_state_list *new_sl;
3702 struct bpf_verifier_state_list *sl;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003703 struct bpf_verifier_state *cur = env->cur_state;
Alexei Starovoitov1969db42017-11-01 00:08:04 -07003704 int i, err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003705
3706 sl = env->explored_states[insn_idx];
3707 if (!sl)
3708 /* this 'insn_idx' instruction wasn't marked, so we will not
3709 * be doing state search here
3710 */
3711 return 0;
3712
3713 while (sl != STATE_LIST_MARK) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003714 if (states_equal(env, &sl->state, cur)) {
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003715 /* reached equivalent register/stack state,
Edward Creedc503a82017-08-15 20:34:35 +01003716 * prune the search.
3717 * Registers read by the continuation are read by us.
Edward Cree8e9cd9c2017-08-23 15:11:21 +01003718 * If we have any write marks in env->cur_state, they
3719 * will prevent corresponding reads in the continuation
3720 * from reaching our parent (an explored_state). Our
3721 * own state will get the read marks recorded, but
3722 * they'll be immediately forgotten as we're pruning
3723 * this state and will pop a new one.
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003724 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003725 propagate_liveness(&sl->state, cur);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003726 return 1;
Edward Creedc503a82017-08-15 20:34:35 +01003727 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003728 sl = sl->next;
3729 }
3730
3731 /* there were no equivalent states, remember current one.
3732 * technically the current state is not proven to be safe yet,
3733 * but it will either reach bpf_exit (which means it's safe) or
3734 * it will be rejected. Since there are no loops, we won't be
3735 * seeing this 'insn_idx' instruction again on the way to bpf_exit
3736 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003737 new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003738 if (!new_sl)
3739 return -ENOMEM;
3740
3741 /* add new state to the head of linked list */
Alexei Starovoitov1969db42017-11-01 00:08:04 -07003742 err = copy_verifier_state(&new_sl->state, cur);
3743 if (err) {
3744 free_verifier_state(&new_sl->state, false);
3745 kfree(new_sl);
3746 return err;
3747 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003748 new_sl->next = env->explored_states[insn_idx];
3749 env->explored_states[insn_idx] = new_sl;
Edward Creedc503a82017-08-15 20:34:35 +01003750 /* connect new state to parentage chain */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003751 cur->parent = &new_sl->state;
Edward Cree8e9cd9c2017-08-23 15:11:21 +01003752 /* clear write marks in current state: the writes we did are not writes
3753 * our child did, so they don't screen off its reads from us.
3754 * (There are no read marks in current state, because reads always mark
3755 * their parent and current state never has children yet. Only
3756 * explored_states can get read marks.)
3757 */
Edward Creedc503a82017-08-15 20:34:35 +01003758 for (i = 0; i < BPF_REG_FP; i++)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003759 cur->regs[i].live = REG_LIVE_NONE;
3760 for (i = 0; i < cur->allocated_stack / BPF_REG_SIZE; i++)
3761 if (cur->stack[i].slot_type[0] == STACK_SPILL)
3762 cur->stack[i].spilled_ptr.live = REG_LIVE_NONE;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003763 return 0;
3764}
3765
Jakub Kicinski13a27df2016-09-21 11:43:58 +01003766static int ext_analyzer_insn_hook(struct bpf_verifier_env *env,
3767 int insn_idx, int prev_insn_idx)
3768{
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07003769 if (env->dev_ops && env->dev_ops->insn_hook)
3770 return env->dev_ops->insn_hook(env, insn_idx, prev_insn_idx);
Jakub Kicinski13a27df2016-09-21 11:43:58 +01003771
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07003772 return 0;
Jakub Kicinski13a27df2016-09-21 11:43:58 +01003773}
3774
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003775static int do_check(struct bpf_verifier_env *env)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003776{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003777 struct bpf_verifier_state *state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003778 struct bpf_insn *insns = env->prog->insnsi;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003779 struct bpf_reg_state *regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003780 int insn_cnt = env->prog->len;
3781 int insn_idx, prev_insn_idx = 0;
3782 int insn_processed = 0;
3783 bool do_print_state = false;
3784
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003785 state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL);
3786 if (!state)
3787 return -ENOMEM;
3788 env->cur_state = state;
3789 init_reg_state(env, state->regs);
Edward Creedc503a82017-08-15 20:34:35 +01003790 state->parent = NULL;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003791 insn_idx = 0;
3792 for (;;) {
3793 struct bpf_insn *insn;
3794 u8 class;
3795 int err;
3796
3797 if (insn_idx >= insn_cnt) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003798 verbose(env, "invalid insn idx %d insn_cnt %d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003799 insn_idx, insn_cnt);
3800 return -EFAULT;
3801 }
3802
3803 insn = &insns[insn_idx];
3804 class = BPF_CLASS(insn->code);
3805
Daniel Borkmann07016152016-04-05 22:33:17 +02003806 if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003807 verbose(env,
3808 "BPF program is too large. Processed %d insn\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003809 insn_processed);
3810 return -E2BIG;
3811 }
3812
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003813 err = is_state_visited(env, insn_idx);
3814 if (err < 0)
3815 return err;
3816 if (err == 1) {
3817 /* found equivalent state, can prune the search */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003818 if (env->log.level) {
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003819 if (do_print_state)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003820 verbose(env, "\nfrom %d to %d: safe\n",
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003821 prev_insn_idx, insn_idx);
3822 else
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003823 verbose(env, "%d: safe\n", insn_idx);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003824 }
3825 goto process_bpf_exit;
3826 }
3827
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02003828 if (need_resched())
3829 cond_resched();
3830
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003831 if (env->log.level > 1 || (env->log.level && do_print_state)) {
3832 if (env->log.level > 1)
3833 verbose(env, "%d:", insn_idx);
David S. Millerc5fc9692017-05-10 11:25:17 -07003834 else
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003835 verbose(env, "\nfrom %d to %d:",
David S. Millerc5fc9692017-05-10 11:25:17 -07003836 prev_insn_idx, insn_idx);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003837 print_verifier_state(env, state);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003838 do_print_state = false;
3839 }
3840
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003841 if (env->log.level) {
3842 verbose(env, "%d: ", insn_idx);
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -07003843 print_bpf_insn(verbose, env, insn,
3844 env->allow_ptr_leaks);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003845 }
3846
Jakub Kicinski13a27df2016-09-21 11:43:58 +01003847 err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx);
3848 if (err)
3849 return err;
3850
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003851 regs = cur_regs(env);
Alexei Starovoitovc1311872017-11-22 16:42:05 -08003852 env->insn_aux_data[insn_idx].seen = true;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003853 if (class == BPF_ALU || class == BPF_ALU64) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003854 err = check_alu_op(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003855 if (err)
3856 return err;
3857
3858 } else if (class == BPF_LDX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003859 enum bpf_reg_type *prev_src_type, src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003860
3861 /* check for reserved fields is already done */
3862
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003863 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01003864 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003865 if (err)
3866 return err;
3867
Edward Creedc503a82017-08-15 20:34:35 +01003868 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003869 if (err)
3870 return err;
3871
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07003872 src_reg_type = regs[insn->src_reg].type;
3873
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003874 /* check that memory (src_reg + off) is readable,
3875 * the state of dst_reg will be updated by this func
3876 */
Yonghong Song31fd8582017-06-13 15:52:13 -07003877 err = check_mem_access(env, insn_idx, insn->src_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003878 BPF_SIZE(insn->code), BPF_READ,
3879 insn->dst_reg);
3880 if (err)
3881 return err;
3882
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003883 prev_src_type = &env->insn_aux_data[insn_idx].ptr_type;
3884
3885 if (*prev_src_type == NOT_INIT) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003886 /* saw a valid insn
3887 * dst_reg = *(u32 *)(src_reg + off)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003888 * save type to validate intersecting paths
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003889 */
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003890 *prev_src_type = src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003891
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003892 } else if (src_reg_type != *prev_src_type &&
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003893 (src_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003894 *prev_src_type == PTR_TO_CTX)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003895 /* ABuser program is trying to use the same insn
3896 * dst_reg = *(u32*) (src_reg + off)
3897 * with different pointer types:
3898 * src_reg == ctx in one branch and
3899 * src_reg == stack|map in some other branch.
3900 * Reject it.
3901 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003902 verbose(env, "same insn cannot be used with different pointers\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003903 return -EINVAL;
3904 }
3905
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003906 } else if (class == BPF_STX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003907 enum bpf_reg_type *prev_dst_type, dst_reg_type;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003908
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003909 if (BPF_MODE(insn->code) == BPF_XADD) {
Yonghong Song31fd8582017-06-13 15:52:13 -07003910 err = check_xadd(env, insn_idx, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003911 if (err)
3912 return err;
3913 insn_idx++;
3914 continue;
3915 }
3916
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003917 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01003918 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003919 if (err)
3920 return err;
3921 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01003922 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003923 if (err)
3924 return err;
3925
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003926 dst_reg_type = regs[insn->dst_reg].type;
3927
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003928 /* check that memory (dst_reg + off) is writeable */
Yonghong Song31fd8582017-06-13 15:52:13 -07003929 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003930 BPF_SIZE(insn->code), BPF_WRITE,
3931 insn->src_reg);
3932 if (err)
3933 return err;
3934
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003935 prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type;
3936
3937 if (*prev_dst_type == NOT_INIT) {
3938 *prev_dst_type = dst_reg_type;
3939 } else if (dst_reg_type != *prev_dst_type &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003940 (dst_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003941 *prev_dst_type == PTR_TO_CTX)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003942 verbose(env, "same insn cannot be used with different pointers\n");
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003943 return -EINVAL;
3944 }
3945
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003946 } else if (class == BPF_ST) {
3947 if (BPF_MODE(insn->code) != BPF_MEM ||
3948 insn->src_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003949 verbose(env, "BPF_ST uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003950 return -EINVAL;
3951 }
3952 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01003953 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003954 if (err)
3955 return err;
3956
3957 /* check that memory (dst_reg + off) is writeable */
Yonghong Song31fd8582017-06-13 15:52:13 -07003958 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003959 BPF_SIZE(insn->code), BPF_WRITE,
3960 -1);
3961 if (err)
3962 return err;
3963
3964 } else if (class == BPF_JMP) {
3965 u8 opcode = BPF_OP(insn->code);
3966
3967 if (opcode == BPF_CALL) {
3968 if (BPF_SRC(insn->code) != BPF_K ||
3969 insn->off != 0 ||
3970 insn->src_reg != BPF_REG_0 ||
3971 insn->dst_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003972 verbose(env, "BPF_CALL uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003973 return -EINVAL;
3974 }
3975
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07003976 err = check_call(env, insn->imm, insn_idx);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003977 if (err)
3978 return err;
3979
3980 } else if (opcode == BPF_JA) {
3981 if (BPF_SRC(insn->code) != BPF_K ||
3982 insn->imm != 0 ||
3983 insn->src_reg != BPF_REG_0 ||
3984 insn->dst_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003985 verbose(env, "BPF_JA uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003986 return -EINVAL;
3987 }
3988
3989 insn_idx += insn->off + 1;
3990 continue;
3991
3992 } else if (opcode == BPF_EXIT) {
3993 if (BPF_SRC(insn->code) != BPF_K ||
3994 insn->imm != 0 ||
3995 insn->src_reg != BPF_REG_0 ||
3996 insn->dst_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003997 verbose(env, "BPF_EXIT uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003998 return -EINVAL;
3999 }
4000
4001 /* eBPF calling convetion is such that R0 is used
4002 * to return the value from eBPF program.
4003 * Make sure that it's readable at this time
4004 * of bpf_exit, which means that program wrote
4005 * something into it earlier
4006 */
Edward Creedc503a82017-08-15 20:34:35 +01004007 err = check_reg_arg(env, BPF_REG_0, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004008 if (err)
4009 return err;
4010
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004011 if (is_pointer_value(env, BPF_REG_0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004012 verbose(env, "R0 leaks addr as return value\n");
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004013 return -EACCES;
4014 }
4015
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07004016 err = check_return_code(env);
4017 if (err)
4018 return err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004019process_bpf_exit:
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004020 err = pop_stack(env, &prev_insn_idx, &insn_idx);
4021 if (err < 0) {
4022 if (err != -ENOENT)
4023 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004024 break;
4025 } else {
4026 do_print_state = true;
4027 continue;
4028 }
4029 } else {
4030 err = check_cond_jmp_op(env, insn, &insn_idx);
4031 if (err)
4032 return err;
4033 }
4034 } else if (class == BPF_LD) {
4035 u8 mode = BPF_MODE(insn->code);
4036
4037 if (mode == BPF_ABS || mode == BPF_IND) {
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004038 err = check_ld_abs(env, insn);
4039 if (err)
4040 return err;
4041
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004042 } else if (mode == BPF_IMM) {
4043 err = check_ld_imm(env, insn);
4044 if (err)
4045 return err;
4046
4047 insn_idx++;
Alexei Starovoitovc1311872017-11-22 16:42:05 -08004048 env->insn_aux_data[insn_idx].seen = true;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004049 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004050 verbose(env, "invalid BPF_LD mode\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004051 return -EINVAL;
4052 }
4053 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004054 verbose(env, "unknown insn class %d\n", class);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004055 return -EINVAL;
4056 }
4057
4058 insn_idx++;
4059 }
4060
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004061 verbose(env, "processed %d insns, stack depth %d\n", insn_processed,
4062 env->prog->aux->stack_depth);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004063 return 0;
4064}
4065
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004066static int check_map_prealloc(struct bpf_map *map)
4067{
4068 return (map->map_type != BPF_MAP_TYPE_HASH &&
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07004069 map->map_type != BPF_MAP_TYPE_PERCPU_HASH &&
4070 map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) ||
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004071 !(map->map_flags & BPF_F_NO_PREALLOC);
4072}
4073
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004074static int check_map_prog_compatibility(struct bpf_verifier_env *env,
4075 struct bpf_map *map,
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07004076 struct bpf_prog *prog)
4077
4078{
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004079 /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use
4080 * preallocated hash maps, since doing memory allocation
4081 * in overflow_handler can crash depending on where nmi got
4082 * triggered.
4083 */
4084 if (prog->type == BPF_PROG_TYPE_PERF_EVENT) {
4085 if (!check_map_prealloc(map)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004086 verbose(env, "perf_event programs can only use preallocated hash map\n");
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004087 return -EINVAL;
4088 }
4089 if (map->inner_map_meta &&
4090 !check_map_prealloc(map->inner_map_meta)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004091 verbose(env, "perf_event programs can only use preallocated inner hash map\n");
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004092 return -EINVAL;
4093 }
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07004094 }
4095 return 0;
4096}
4097
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004098/* look for pseudo eBPF instructions that access map FDs and
4099 * replace them with actual map pointers
4100 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004101static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004102{
4103 struct bpf_insn *insn = env->prog->insnsi;
4104 int insn_cnt = env->prog->len;
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07004105 int i, j, err;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004106
Daniel Borkmannf1f77142017-01-13 23:38:15 +01004107 err = bpf_prog_calc_tag(env->prog);
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01004108 if (err)
4109 return err;
4110
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004111 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004112 if (BPF_CLASS(insn->code) == BPF_LDX &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004113 (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004114 verbose(env, "BPF_LDX uses reserved fields\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004115 return -EINVAL;
4116 }
4117
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004118 if (BPF_CLASS(insn->code) == BPF_STX &&
4119 ((BPF_MODE(insn->code) != BPF_MEM &&
4120 BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004121 verbose(env, "BPF_STX uses reserved fields\n");
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004122 return -EINVAL;
4123 }
4124
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004125 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
4126 struct bpf_map *map;
4127 struct fd f;
4128
4129 if (i == insn_cnt - 1 || insn[1].code != 0 ||
4130 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
4131 insn[1].off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004132 verbose(env, "invalid bpf_ld_imm64 insn\n");
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004133 return -EINVAL;
4134 }
4135
4136 if (insn->src_reg == 0)
4137 /* valid generic load 64-bit imm */
4138 goto next_insn;
4139
4140 if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004141 verbose(env,
4142 "unrecognized bpf_ld_imm64 insn\n");
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004143 return -EINVAL;
4144 }
4145
4146 f = fdget(insn->imm);
Daniel Borkmannc2101292015-10-29 14:58:07 +01004147 map = __bpf_map_get(f);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004148 if (IS_ERR(map)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004149 verbose(env, "fd %d is not pointing to valid bpf_map\n",
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004150 insn->imm);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004151 return PTR_ERR(map);
4152 }
4153
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004154 err = check_map_prog_compatibility(env, map, env->prog);
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07004155 if (err) {
4156 fdput(f);
4157 return err;
4158 }
4159
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004160 /* store map pointer inside BPF_LD_IMM64 instruction */
4161 insn[0].imm = (u32) (unsigned long) map;
4162 insn[1].imm = ((u64) (unsigned long) map) >> 32;
4163
4164 /* check whether we recorded this map already */
4165 for (j = 0; j < env->used_map_cnt; j++)
4166 if (env->used_maps[j] == map) {
4167 fdput(f);
4168 goto next_insn;
4169 }
4170
4171 if (env->used_map_cnt >= MAX_USED_MAPS) {
4172 fdput(f);
4173 return -E2BIG;
4174 }
4175
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004176 /* hold the map. If the program is rejected by verifier,
4177 * the map will be released by release_maps() or it
4178 * will be used by the valid program until it's unloaded
4179 * and all maps are released in free_bpf_prog_info()
4180 */
Alexei Starovoitov92117d82016-04-27 18:56:20 -07004181 map = bpf_map_inc(map, false);
4182 if (IS_ERR(map)) {
4183 fdput(f);
4184 return PTR_ERR(map);
4185 }
4186 env->used_maps[env->used_map_cnt++] = map;
4187
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004188 fdput(f);
4189next_insn:
4190 insn++;
4191 i++;
4192 }
4193 }
4194
4195 /* now all pseudo BPF_LD_IMM64 instructions load valid
4196 * 'struct bpf_map *' into a register instead of user map_fd.
4197 * These pointers will be used later by verifier to validate map access.
4198 */
4199 return 0;
4200}
4201
4202/* drop refcnt of maps used by the rejected program */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004203static void release_maps(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004204{
4205 int i;
4206
4207 for (i = 0; i < env->used_map_cnt; i++)
4208 bpf_map_put(env->used_maps[i]);
4209}
4210
4211/* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004212static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004213{
4214 struct bpf_insn *insn = env->prog->insnsi;
4215 int insn_cnt = env->prog->len;
4216 int i;
4217
4218 for (i = 0; i < insn_cnt; i++, insn++)
4219 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
4220 insn->src_reg = 0;
4221}
4222
Alexei Starovoitov80419022017-03-15 18:26:41 -07004223/* single env->prog->insni[off] instruction was replaced with the range
4224 * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
4225 * [0, off) and [off, end) to new locations, so the patched range stays zero
4226 */
4227static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len,
4228 u32 off, u32 cnt)
4229{
4230 struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data;
Alexei Starovoitovc1311872017-11-22 16:42:05 -08004231 int i;
Alexei Starovoitov80419022017-03-15 18:26:41 -07004232
4233 if (cnt == 1)
4234 return 0;
4235 new_data = vzalloc(sizeof(struct bpf_insn_aux_data) * prog_len);
4236 if (!new_data)
4237 return -ENOMEM;
4238 memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off);
4239 memcpy(new_data + off + cnt - 1, old_data + off,
4240 sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
Alexei Starovoitovc1311872017-11-22 16:42:05 -08004241 for (i = off; i < off + cnt - 1; i++)
4242 new_data[i].seen = true;
Alexei Starovoitov80419022017-03-15 18:26:41 -07004243 env->insn_aux_data = new_data;
4244 vfree(old_data);
4245 return 0;
4246}
4247
4248static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
4249 const struct bpf_insn *patch, u32 len)
4250{
4251 struct bpf_prog *new_prog;
4252
4253 new_prog = bpf_patch_insn_single(env->prog, off, patch, len);
4254 if (!new_prog)
4255 return NULL;
4256 if (adjust_insn_aux_data(env, new_prog->len, off, len))
4257 return NULL;
4258 return new_prog;
4259}
4260
Alexei Starovoitovc1311872017-11-22 16:42:05 -08004261/* The verifier does more data flow analysis than llvm and will not explore
4262 * branches that are dead at run time. Malicious programs can have dead code
4263 * too. Therefore replace all dead at-run-time code with nops.
4264 */
4265static void sanitize_dead_code(struct bpf_verifier_env *env)
4266{
4267 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
4268 struct bpf_insn nop = BPF_MOV64_REG(BPF_REG_0, BPF_REG_0);
4269 struct bpf_insn *insn = env->prog->insnsi;
4270 const int insn_cnt = env->prog->len;
4271 int i;
4272
4273 for (i = 0; i < insn_cnt; i++) {
4274 if (aux_data[i].seen)
4275 continue;
4276 memcpy(insn + i, &nop, sizeof(nop));
4277 }
4278}
4279
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004280/* convert load instructions that access fields of 'struct __sk_buff'
4281 * into sequence of instructions that access fields of 'struct sk_buff'
4282 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004283static int convert_ctx_accesses(struct bpf_verifier_env *env)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004284{
Jakub Kicinski00176a32017-10-16 16:40:54 -07004285 const struct bpf_verifier_ops *ops = env->ops;
Daniel Borkmannf96da092017-07-02 02:13:27 +02004286 int i, cnt, size, ctx_field_size, delta = 0;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004287 const int insn_cnt = env->prog->len;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004288 struct bpf_insn insn_buf[16], *insn;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004289 struct bpf_prog *new_prog;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004290 enum bpf_access_type type;
Daniel Borkmannf96da092017-07-02 02:13:27 +02004291 bool is_narrower_load;
4292 u32 target_size;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004293
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004294 if (ops->gen_prologue) {
4295 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
4296 env->prog);
4297 if (cnt >= ARRAY_SIZE(insn_buf)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004298 verbose(env, "bpf verifier is misconfigured\n");
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004299 return -EINVAL;
4300 } else if (cnt) {
Alexei Starovoitov80419022017-03-15 18:26:41 -07004301 new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004302 if (!new_prog)
4303 return -ENOMEM;
Alexei Starovoitov80419022017-03-15 18:26:41 -07004304
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004305 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004306 delta += cnt - 1;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004307 }
4308 }
4309
4310 if (!ops->convert_ctx_access)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004311 return 0;
4312
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004313 insn = env->prog->insnsi + delta;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004314
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004315 for (i = 0; i < insn_cnt; i++, insn++) {
Daniel Borkmann62c79892017-01-12 11:51:33 +01004316 if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) ||
4317 insn->code == (BPF_LDX | BPF_MEM | BPF_H) ||
4318 insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07004319 insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004320 type = BPF_READ;
Daniel Borkmann62c79892017-01-12 11:51:33 +01004321 else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) ||
4322 insn->code == (BPF_STX | BPF_MEM | BPF_H) ||
4323 insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07004324 insn->code == (BPF_STX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004325 type = BPF_WRITE;
4326 else
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004327 continue;
4328
Alexei Starovoitov80419022017-03-15 18:26:41 -07004329 if (env->insn_aux_data[i + delta].ptr_type != PTR_TO_CTX)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004330 continue;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004331
Yonghong Song31fd8582017-06-13 15:52:13 -07004332 ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size;
Daniel Borkmannf96da092017-07-02 02:13:27 +02004333 size = BPF_LDST_BYTES(insn);
Yonghong Song31fd8582017-06-13 15:52:13 -07004334
4335 /* If the read access is a narrower load of the field,
4336 * convert to a 4/8-byte load, to minimum program type specific
4337 * convert_ctx_access changes. If conversion is successful,
4338 * we will apply proper mask to the result.
4339 */
Daniel Borkmannf96da092017-07-02 02:13:27 +02004340 is_narrower_load = size < ctx_field_size;
Yonghong Song31fd8582017-06-13 15:52:13 -07004341 if (is_narrower_load) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02004342 u32 off = insn->off;
4343 u8 size_code;
Yonghong Song31fd8582017-06-13 15:52:13 -07004344
Daniel Borkmannf96da092017-07-02 02:13:27 +02004345 if (type == BPF_WRITE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004346 verbose(env, "bpf verifier narrow ctx access misconfigured\n");
Daniel Borkmannf96da092017-07-02 02:13:27 +02004347 return -EINVAL;
4348 }
4349
4350 size_code = BPF_H;
Yonghong Song31fd8582017-06-13 15:52:13 -07004351 if (ctx_field_size == 4)
4352 size_code = BPF_W;
4353 else if (ctx_field_size == 8)
4354 size_code = BPF_DW;
Daniel Borkmannf96da092017-07-02 02:13:27 +02004355
Yonghong Song31fd8582017-06-13 15:52:13 -07004356 insn->off = off & ~(ctx_field_size - 1);
4357 insn->code = BPF_LDX | BPF_MEM | size_code;
4358 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02004359
4360 target_size = 0;
4361 cnt = ops->convert_ctx_access(type, insn, insn_buf, env->prog,
4362 &target_size);
4363 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) ||
4364 (ctx_field_size && !target_size)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004365 verbose(env, "bpf verifier is misconfigured\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004366 return -EINVAL;
4367 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02004368
4369 if (is_narrower_load && size < target_size) {
Yonghong Song31fd8582017-06-13 15:52:13 -07004370 if (ctx_field_size <= 4)
4371 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02004372 (1 << size * 8) - 1);
Yonghong Song31fd8582017-06-13 15:52:13 -07004373 else
4374 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02004375 (1 << size * 8) - 1);
Yonghong Song31fd8582017-06-13 15:52:13 -07004376 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004377
Alexei Starovoitov80419022017-03-15 18:26:41 -07004378 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004379 if (!new_prog)
4380 return -ENOMEM;
4381
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004382 delta += cnt - 1;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004383
4384 /* keep walking new program and skip insns we just inserted */
4385 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004386 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004387 }
4388
4389 return 0;
4390}
4391
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004392/* fixup insn->imm field of bpf_call instructions
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07004393 * and inline eligible helpers as explicit sequence of BPF instructions
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07004394 *
4395 * this function is called after eBPF program passed verification
4396 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004397static int fixup_bpf_calls(struct bpf_verifier_env *env)
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07004398{
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004399 struct bpf_prog *prog = env->prog;
4400 struct bpf_insn *insn = prog->insnsi;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07004401 const struct bpf_func_proto *fn;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004402 const int insn_cnt = prog->len;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07004403 struct bpf_insn insn_buf[16];
4404 struct bpf_prog *new_prog;
4405 struct bpf_map *map_ptr;
4406 int i, cnt, delta = 0;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07004407
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004408 for (i = 0; i < insn_cnt; i++, insn++) {
4409 if (insn->code != (BPF_JMP | BPF_CALL))
4410 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07004411
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004412 if (insn->imm == BPF_FUNC_get_route_realm)
4413 prog->dst_needed = 1;
4414 if (insn->imm == BPF_FUNC_get_prandom_u32)
4415 bpf_user_rnd_init_once();
Josef Bacik9802d862017-12-11 11:36:48 -05004416 if (insn->imm == BPF_FUNC_override_return)
4417 prog->kprobe_override = 1;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004418 if (insn->imm == BPF_FUNC_tail_call) {
David S. Miller7b9f6da2017-04-20 10:35:33 -04004419 /* If we tail call into other programs, we
4420 * cannot make any assumptions since they can
4421 * be replaced dynamically during runtime in
4422 * the program array.
4423 */
4424 prog->cb_access = 1;
Alexei Starovoitov80a58d02017-05-30 13:31:30 -07004425 env->prog->aux->stack_depth = MAX_BPF_STACK;
David S. Miller7b9f6da2017-04-20 10:35:33 -04004426
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004427 /* mark bpf_tail_call as different opcode to avoid
4428 * conditional branch in the interpeter for every normal
4429 * call and to prevent accidental JITing by JIT compiler
4430 * that doesn't support bpf_tail_call yet
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07004431 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004432 insn->imm = 0;
Alexei Starovoitov71189fa2017-05-30 13:31:27 -07004433 insn->code = BPF_JMP | BPF_TAIL_CALL;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004434 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07004435 }
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07004436
Daniel Borkmann89c63072017-08-19 03:12:45 +02004437 /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup
4438 * handlers are currently limited to 64 bit only.
4439 */
4440 if (ebpf_jit_enabled() && BITS_PER_LONG == 64 &&
4441 insn->imm == BPF_FUNC_map_lookup_elem) {
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07004442 map_ptr = env->insn_aux_data[i + delta].map_ptr;
Martin KaFai Laufad73a12017-03-22 10:00:32 -07004443 if (map_ptr == BPF_MAP_PTR_POISON ||
4444 !map_ptr->ops->map_gen_lookup)
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07004445 goto patch_call_imm;
4446
4447 cnt = map_ptr->ops->map_gen_lookup(map_ptr, insn_buf);
4448 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004449 verbose(env, "bpf verifier is misconfigured\n");
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07004450 return -EINVAL;
4451 }
4452
4453 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf,
4454 cnt);
4455 if (!new_prog)
4456 return -ENOMEM;
4457
4458 delta += cnt - 1;
4459
4460 /* keep walking new program and skip insns we just inserted */
4461 env->prog = prog = new_prog;
4462 insn = new_prog->insnsi + i + delta;
4463 continue;
4464 }
4465
Daniel Borkmann109980b2017-09-08 00:14:51 +02004466 if (insn->imm == BPF_FUNC_redirect_map) {
Daniel Borkmann7c300132017-09-20 00:44:21 +02004467 /* Note, we cannot use prog directly as imm as subsequent
4468 * rewrites would still change the prog pointer. The only
4469 * stable address we can use is aux, which also works with
4470 * prog clones during blinding.
4471 */
4472 u64 addr = (unsigned long)prog->aux;
Daniel Borkmann109980b2017-09-08 00:14:51 +02004473 struct bpf_insn r4_ld[] = {
4474 BPF_LD_IMM64(BPF_REG_4, addr),
4475 *insn,
4476 };
4477 cnt = ARRAY_SIZE(r4_ld);
4478
4479 new_prog = bpf_patch_insn_data(env, i + delta, r4_ld, cnt);
4480 if (!new_prog)
4481 return -ENOMEM;
4482
4483 delta += cnt - 1;
4484 env->prog = prog = new_prog;
4485 insn = new_prog->insnsi + i + delta;
4486 }
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07004487patch_call_imm:
Jakub Kicinski00176a32017-10-16 16:40:54 -07004488 fn = env->ops->get_func_proto(insn->imm);
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004489 /* all functions that have prototype and verifier allowed
4490 * programs to call them, must be real in-kernel functions
4491 */
4492 if (!fn->func) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004493 verbose(env,
4494 "kernel subsystem misconfigured func %s#%d\n",
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004495 func_id_name(insn->imm), insn->imm);
4496 return -EFAULT;
4497 }
4498 insn->imm = fn->func - __bpf_call_base;
4499 }
4500
4501 return 0;
4502}
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07004503
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004504static void free_states(struct bpf_verifier_env *env)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004505{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004506 struct bpf_verifier_state_list *sl, *sln;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004507 int i;
4508
4509 if (!env->explored_states)
4510 return;
4511
4512 for (i = 0; i < env->prog->len; i++) {
4513 sl = env->explored_states[i];
4514
4515 if (sl)
4516 while (sl != STATE_LIST_MARK) {
4517 sln = sl->next;
Alexei Starovoitov1969db42017-11-01 00:08:04 -07004518 free_verifier_state(&sl->state, false);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004519 kfree(sl);
4520 sl = sln;
4521 }
4522 }
4523
4524 kfree(env->explored_states);
4525}
4526
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004527int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
Alexei Starovoitov51580e72014-09-26 00:17:02 -07004528{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004529 struct bpf_verifier_env *env;
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004530 struct bpf_verifer_log *log;
Alexei Starovoitov51580e72014-09-26 00:17:02 -07004531 int ret = -EINVAL;
4532
Arnd Bergmanneba0c922017-11-02 12:05:52 +01004533 /* no program is valid */
4534 if (ARRAY_SIZE(bpf_verifier_ops) == 0)
4535 return -EINVAL;
4536
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004537 /* 'struct bpf_verifier_env' can be global, but since it's not small,
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004538 * allocate/free it every time bpf_check() is called
4539 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004540 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004541 if (!env)
4542 return -ENOMEM;
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004543 log = &env->log;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004544
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004545 env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
4546 (*prog)->len);
4547 ret = -ENOMEM;
4548 if (!env->insn_aux_data)
4549 goto err_free_env;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004550 env->prog = *prog;
Jakub Kicinski00176a32017-10-16 16:40:54 -07004551 env->ops = bpf_verifier_ops[env->prog->type];
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004552
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004553 /* grab the mutex to protect few globals used by verifier */
4554 mutex_lock(&bpf_verifier_lock);
4555
4556 if (attr->log_level || attr->log_buf || attr->log_size) {
4557 /* user requested verbose verifier output
4558 * and supplied buffer to store the verification trace
4559 */
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07004560 log->level = attr->log_level;
4561 log->ubuf = (char __user *) (unsigned long) attr->log_buf;
4562 log->len_total = attr->log_size;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004563
4564 ret = -EINVAL;
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07004565 /* log attributes have to be sane */
4566 if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 ||
4567 !log->level || !log->ubuf)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004568 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004569 }
Daniel Borkmann1ad2f582017-05-25 01:05:05 +02004570
4571 env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT);
4572 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
David S. Millere07b98d2017-05-10 11:38:07 -07004573 env->strict_alignment = true;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004574
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07004575 if (env->prog->aux->offload) {
4576 ret = bpf_prog_offload_verifier_prep(env);
4577 if (ret)
4578 goto err_unlock;
4579 }
4580
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004581 ret = replace_map_fd_with_map_ptr(env);
4582 if (ret < 0)
4583 goto skip_full_check;
4584
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004585 env->explored_states = kcalloc(env->prog->len,
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004586 sizeof(struct bpf_verifier_state_list *),
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004587 GFP_USER);
4588 ret = -ENOMEM;
4589 if (!env->explored_states)
4590 goto skip_full_check;
4591
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004592 ret = check_cfg(env);
4593 if (ret < 0)
4594 goto skip_full_check;
4595
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004596 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
4597
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004598 ret = do_check(env);
Craig Gallek8c01c4f2017-11-02 11:18:01 -04004599 if (env->cur_state) {
4600 free_verifier_state(env->cur_state, true);
4601 env->cur_state = NULL;
4602 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004603
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004604skip_full_check:
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004605 while (!pop_stack(env, NULL, NULL));
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004606 free_states(env);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004607
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004608 if (ret == 0)
Alexei Starovoitovc1311872017-11-22 16:42:05 -08004609 sanitize_dead_code(env);
4610
4611 if (ret == 0)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004612 /* program is valid, convert *(u32*)(ctx + off) accesses */
4613 ret = convert_ctx_accesses(env);
4614
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07004615 if (ret == 0)
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004616 ret = fixup_bpf_calls(env);
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07004617
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07004618 if (log->level && bpf_verifier_log_full(log))
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004619 ret = -ENOSPC;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07004620 if (log->level && !log->ubuf) {
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004621 ret = -EFAULT;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07004622 goto err_release_maps;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004623 }
4624
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004625 if (ret == 0 && env->used_map_cnt) {
4626 /* if program passed verifier, update used_maps in bpf_prog_info */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004627 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
4628 sizeof(env->used_maps[0]),
4629 GFP_KERNEL);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004630
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004631 if (!env->prog->aux->used_maps) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004632 ret = -ENOMEM;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07004633 goto err_release_maps;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004634 }
4635
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004636 memcpy(env->prog->aux->used_maps, env->used_maps,
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004637 sizeof(env->used_maps[0]) * env->used_map_cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004638 env->prog->aux->used_map_cnt = env->used_map_cnt;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004639
4640 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
4641 * bpf_ld_imm64 instructions
4642 */
4643 convert_pseudo_ld_imm64(env);
4644 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004645
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07004646err_release_maps:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004647 if (!env->prog->aux->used_maps)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004648 /* if we didn't copy map pointers into bpf_prog_info, release
4649 * them now. Otherwise free_bpf_prog_info() will release them.
4650 */
4651 release_maps(env);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004652 *prog = env->prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004653err_unlock:
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004654 mutex_unlock(&bpf_verifier_lock);
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004655 vfree(env->insn_aux_data);
4656err_free_env:
4657 kfree(env);
Alexei Starovoitov51580e72014-09-26 00:17:02 -07004658 return ret;
4659}