blob: 738e919efdf03782dcd99ef2679dcb6af9768ef2 [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 Starovoitovcc8b0b92017-12-14 17:55:05 -080023#include <linux/bsearch.h>
24#include <linux/sort.h>
Alexei Starovoitov51580e72014-09-26 00:17:02 -070025
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -070026#include "disasm.h"
27
Jakub Kicinski00176a32017-10-16 16:40:54 -070028static const struct bpf_verifier_ops * const bpf_verifier_ops[] = {
29#define BPF_PROG_TYPE(_id, _name) \
30 [_id] = & _name ## _verifier_ops,
31#define BPF_MAP_TYPE(_id, _ops)
32#include <linux/bpf_types.h>
33#undef BPF_PROG_TYPE
34#undef BPF_MAP_TYPE
35};
36
Alexei Starovoitov51580e72014-09-26 00:17:02 -070037/* bpf_check() is a static code analyzer that walks eBPF program
38 * instruction by instruction and updates register/stack state.
39 * All paths of conditional branches are analyzed until 'bpf_exit' insn.
40 *
41 * The first pass is depth-first-search to check that the program is a DAG.
42 * It rejects the following programs:
43 * - larger than BPF_MAXINSNS insns
44 * - if loop is present (detected via back-edge)
45 * - unreachable insns exist (shouldn't be a forest. program = one function)
46 * - out of bounds or malformed jumps
47 * The second pass is all possible path descent from the 1st insn.
48 * Since it's analyzing all pathes through the program, the length of the
Gary Lineba38a92017-03-01 16:25:51 +080049 * analysis is limited to 64k insn, which may be hit even if total number of
Alexei Starovoitov51580e72014-09-26 00:17:02 -070050 * insn is less then 4K, but there are too many branches that change stack/regs.
51 * Number of 'branches to be analyzed' is limited to 1k
52 *
53 * On entry to each instruction, each register has a type, and the instruction
54 * changes the types of the registers depending on instruction semantics.
55 * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is
56 * copied to R1.
57 *
58 * All registers are 64-bit.
59 * R0 - return register
60 * R1-R5 argument passing registers
61 * R6-R9 callee saved registers
62 * R10 - frame pointer read-only
63 *
64 * At the start of BPF program the register R1 contains a pointer to bpf_context
65 * and has type PTR_TO_CTX.
66 *
67 * Verifier tracks arithmetic operations on pointers in case:
68 * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
69 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20),
70 * 1st insn copies R10 (which has FRAME_PTR) type into R1
71 * and 2nd arithmetic instruction is pattern matched to recognize
72 * that it wants to construct a pointer to some element within stack.
73 * So after 2nd insn, the register R1 has type PTR_TO_STACK
74 * (and -20 constant is saved for further stack bounds checking).
75 * Meaning that this reg is a pointer to stack plus known immediate constant.
76 *
Edward Creef1174f72017-08-07 15:26:19 +010077 * Most of the time the registers have SCALAR_VALUE type, which
Alexei Starovoitov51580e72014-09-26 00:17:02 -070078 * means the register has some value, but it's not a valid pointer.
Edward Creef1174f72017-08-07 15:26:19 +010079 * (like pointer plus pointer becomes SCALAR_VALUE type)
Alexei Starovoitov51580e72014-09-26 00:17:02 -070080 *
81 * When verifier sees load or store instructions the type of base register
Edward Creef1174f72017-08-07 15:26:19 +010082 * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK. These are three pointer
Alexei Starovoitov51580e72014-09-26 00:17:02 -070083 * types recognized by check_mem_access() function.
84 *
85 * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value'
86 * and the range of [ptr, ptr + map's value_size) is accessible.
87 *
88 * registers used to pass values to function calls are checked against
89 * function argument constraints.
90 *
91 * ARG_PTR_TO_MAP_KEY is one of such argument constraints.
92 * It means that the register type passed to this function must be
93 * PTR_TO_STACK and it will be used inside the function as
94 * 'pointer to map element key'
95 *
96 * For example the argument constraints for bpf_map_lookup_elem():
97 * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
98 * .arg1_type = ARG_CONST_MAP_PTR,
99 * .arg2_type = ARG_PTR_TO_MAP_KEY,
100 *
101 * ret_type says that this function returns 'pointer to map elem value or null'
102 * function expects 1st argument to be a const pointer to 'struct bpf_map' and
103 * 2nd argument should be a pointer to stack, which will be used inside
104 * the helper function as a pointer to map element key.
105 *
106 * On the kernel side the helper function looks like:
107 * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
108 * {
109 * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
110 * void *key = (void *) (unsigned long) r2;
111 * void *value;
112 *
113 * here kernel can access 'key' and 'map' pointers safely, knowing that
114 * [key, key + map->key_size) bytes are valid and were initialized on
115 * the stack of eBPF program.
116 * }
117 *
118 * Corresponding eBPF program may look like:
119 * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR
120 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK
121 * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP
122 * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
123 * here verifier looks at prototype of map_lookup_elem() and sees:
124 * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok,
125 * Now verifier knows that this map has key of R1->map_ptr->key_size bytes
126 *
127 * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far,
128 * Now verifier checks that [R2, R2 + map's key_size) are within stack limits
129 * and were initialized prior to this call.
130 * If it's ok, then verifier allows this BPF_CALL insn and looks at
131 * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets
132 * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function
133 * returns ether pointer to map value or NULL.
134 *
135 * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off'
136 * insn, the register holding that pointer in the true branch changes state to
137 * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false
138 * branch. See check_cond_jmp_op().
139 *
140 * After the call R0 is set to return type of the function and registers R1-R5
141 * are set to NOT_INIT to indicate that they are no longer readable.
142 */
143
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700144/* verifier_state + insn_idx are pushed to stack when branch is encountered */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100145struct bpf_verifier_stack_elem {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700146 /* verifer state is 'st'
147 * before processing instruction 'insn_idx'
148 * and after processing instruction 'prev_insn_idx'
149 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100150 struct bpf_verifier_state st;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700151 int insn_idx;
152 int prev_insn_idx;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100153 struct bpf_verifier_stack_elem *next;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700154};
155
Edward Cree8e17c1b2017-08-07 15:30:30 +0100156#define BPF_COMPLEXITY_LIMIT_INSNS 131072
Daniel Borkmann07016152016-04-05 22:33:17 +0200157#define BPF_COMPLEXITY_LIMIT_STACK 1024
158
Martin KaFai Laufad73a12017-03-22 10:00:32 -0700159#define BPF_MAP_PTR_POISON ((void *)0xeB9F + POISON_POINTER_DELTA)
160
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200161struct bpf_call_arg_meta {
162 struct bpf_map *map_ptr;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200163 bool raw_mode;
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200164 bool pkt_access;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200165 int regno;
166 int access_size;
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200167};
168
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700169static DEFINE_MUTEX(bpf_verifier_lock);
170
171/* log_level controls verbosity level of eBPF verifier.
172 * verbose() is used to dump the verification trace to the log, so the user
173 * can figure out what's wrong with the program
174 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700175static __printf(2, 3) void verbose(struct bpf_verifier_env *env,
176 const char *fmt, ...)
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700177{
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700178 struct bpf_verifer_log *log = &env->log;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700179 unsigned int n;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700180 va_list args;
181
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700182 if (!log->level || !log->ubuf || bpf_verifier_log_full(log))
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700183 return;
184
185 va_start(args, fmt);
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700186 n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700187 va_end(args);
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700188
189 WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1,
190 "verifier log line truncated - local buffer too short\n");
191
192 n = min(log->len_total - log->len_used - 1, n);
193 log->kbuf[n] = '\0';
194
195 if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1))
196 log->len_used += n;
197 else
198 log->ubuf = NULL;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700199}
200
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200201static bool type_is_pkt_pointer(enum bpf_reg_type type)
202{
203 return type == PTR_TO_PACKET ||
204 type == PTR_TO_PACKET_META;
205}
206
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700207/* string representation of 'enum bpf_reg_type' */
208static const char * const reg_type_str[] = {
209 [NOT_INIT] = "?",
Edward Creef1174f72017-08-07 15:26:19 +0100210 [SCALAR_VALUE] = "inv",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700211 [PTR_TO_CTX] = "ctx",
212 [CONST_PTR_TO_MAP] = "map_ptr",
213 [PTR_TO_MAP_VALUE] = "map_value",
214 [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700215 [PTR_TO_STACK] = "fp",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700216 [PTR_TO_PACKET] = "pkt",
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200217 [PTR_TO_PACKET_META] = "pkt_meta",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700218 [PTR_TO_PACKET_END] = "pkt_end",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700219};
220
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800221static void print_liveness(struct bpf_verifier_env *env,
222 enum bpf_reg_liveness live)
223{
224 if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN))
225 verbose(env, "_");
226 if (live & REG_LIVE_READ)
227 verbose(env, "r");
228 if (live & REG_LIVE_WRITTEN)
229 verbose(env, "w");
230}
231
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800232static struct bpf_func_state *func(struct bpf_verifier_env *env,
233 const struct bpf_reg_state *reg)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700234{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800235 struct bpf_verifier_state *cur = env->cur_state;
236
237 return cur->frame[reg->frameno];
238}
239
240static void print_verifier_state(struct bpf_verifier_env *env,
241 const struct bpf_func_state *state)
242{
243 const struct bpf_reg_state *reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700244 enum bpf_reg_type t;
245 int i;
246
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800247 if (state->frameno)
248 verbose(env, " frame%d:", state->frameno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700249 for (i = 0; i < MAX_BPF_REG; i++) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700250 reg = &state->regs[i];
251 t = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700252 if (t == NOT_INIT)
253 continue;
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800254 verbose(env, " R%d", i);
255 print_liveness(env, reg->live);
256 verbose(env, "=%s", reg_type_str[t]);
Edward Creef1174f72017-08-07 15:26:19 +0100257 if ((t == SCALAR_VALUE || t == PTR_TO_STACK) &&
258 tnum_is_const(reg->var_off)) {
259 /* reg->off should be 0 for SCALAR_VALUE */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700260 verbose(env, "%lld", reg->var_off.value + reg->off);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800261 if (t == PTR_TO_STACK)
262 verbose(env, ",call_%d", func(env, reg)->callsite);
Edward Creef1174f72017-08-07 15:26:19 +0100263 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700264 verbose(env, "(id=%d", reg->id);
Edward Creef1174f72017-08-07 15:26:19 +0100265 if (t != SCALAR_VALUE)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700266 verbose(env, ",off=%d", reg->off);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200267 if (type_is_pkt_pointer(t))
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700268 verbose(env, ",r=%d", reg->range);
Edward Creef1174f72017-08-07 15:26:19 +0100269 else if (t == CONST_PTR_TO_MAP ||
270 t == PTR_TO_MAP_VALUE ||
271 t == PTR_TO_MAP_VALUE_OR_NULL)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700272 verbose(env, ",ks=%d,vs=%d",
Edward Creef1174f72017-08-07 15:26:19 +0100273 reg->map_ptr->key_size,
274 reg->map_ptr->value_size);
Edward Cree7d1238f2017-08-07 15:26:56 +0100275 if (tnum_is_const(reg->var_off)) {
276 /* Typically an immediate SCALAR_VALUE, but
277 * could be a pointer whose offset is too big
278 * for reg->off
279 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700280 verbose(env, ",imm=%llx", reg->var_off.value);
Edward Cree7d1238f2017-08-07 15:26:56 +0100281 } else {
282 if (reg->smin_value != reg->umin_value &&
283 reg->smin_value != S64_MIN)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700284 verbose(env, ",smin_value=%lld",
Edward Cree7d1238f2017-08-07 15:26:56 +0100285 (long long)reg->smin_value);
286 if (reg->smax_value != reg->umax_value &&
287 reg->smax_value != S64_MAX)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700288 verbose(env, ",smax_value=%lld",
Edward Cree7d1238f2017-08-07 15:26:56 +0100289 (long long)reg->smax_value);
290 if (reg->umin_value != 0)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700291 verbose(env, ",umin_value=%llu",
Edward Cree7d1238f2017-08-07 15:26:56 +0100292 (unsigned long long)reg->umin_value);
293 if (reg->umax_value != U64_MAX)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700294 verbose(env, ",umax_value=%llu",
Edward Cree7d1238f2017-08-07 15:26:56 +0100295 (unsigned long long)reg->umax_value);
296 if (!tnum_is_unknown(reg->var_off)) {
297 char tn_buf[48];
Edward Creef1174f72017-08-07 15:26:19 +0100298
Edward Cree7d1238f2017-08-07 15:26:56 +0100299 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700300 verbose(env, ",var_off=%s", tn_buf);
Edward Cree7d1238f2017-08-07 15:26:56 +0100301 }
Edward Creef1174f72017-08-07 15:26:19 +0100302 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700303 verbose(env, ")");
Edward Creef1174f72017-08-07 15:26:19 +0100304 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700305 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700306 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800307 if (state->stack[i].slot_type[0] == STACK_SPILL) {
308 verbose(env, " fp%d",
309 (-i - 1) * BPF_REG_SIZE);
310 print_liveness(env, state->stack[i].spilled_ptr.live);
311 verbose(env, "=%s",
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700312 reg_type_str[state->stack[i].spilled_ptr.type]);
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800313 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800314 if (state->stack[i].slot_type[0] == STACK_ZERO)
315 verbose(env, " fp%d=0", (-i - 1) * BPF_REG_SIZE);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700316 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700317 verbose(env, "\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700318}
319
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800320static int copy_stack_state(struct bpf_func_state *dst,
321 const struct bpf_func_state *src)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700322{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700323 if (!src->stack)
324 return 0;
325 if (WARN_ON_ONCE(dst->allocated_stack < src->allocated_stack)) {
326 /* internal bug, make state invalid to reject the program */
327 memset(dst, 0, sizeof(*dst));
328 return -EFAULT;
329 }
330 memcpy(dst->stack, src->stack,
331 sizeof(*src->stack) * (src->allocated_stack / BPF_REG_SIZE));
332 return 0;
333}
334
335/* do_check() starts with zero-sized stack in struct bpf_verifier_state to
336 * make it consume minimal amount of memory. check_stack_write() access from
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800337 * the program calls into realloc_func_state() to grow the stack size.
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700338 * Note there is a non-zero 'parent' pointer inside bpf_verifier_state
339 * which this function copies over. It points to previous bpf_verifier_state
340 * which is never reallocated
341 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800342static int realloc_func_state(struct bpf_func_state *state, int size,
343 bool copy_old)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700344{
345 u32 old_size = state->allocated_stack;
346 struct bpf_stack_state *new_stack;
347 int slot = size / BPF_REG_SIZE;
348
349 if (size <= old_size || !size) {
350 if (copy_old)
351 return 0;
352 state->allocated_stack = slot * BPF_REG_SIZE;
353 if (!size && old_size) {
354 kfree(state->stack);
355 state->stack = NULL;
356 }
357 return 0;
358 }
359 new_stack = kmalloc_array(slot, sizeof(struct bpf_stack_state),
360 GFP_KERNEL);
361 if (!new_stack)
362 return -ENOMEM;
363 if (copy_old) {
364 if (state->stack)
365 memcpy(new_stack, state->stack,
366 sizeof(*new_stack) * (old_size / BPF_REG_SIZE));
367 memset(new_stack + old_size / BPF_REG_SIZE, 0,
368 sizeof(*new_stack) * (size - old_size) / BPF_REG_SIZE);
369 }
370 state->allocated_stack = slot * BPF_REG_SIZE;
371 kfree(state->stack);
372 state->stack = new_stack;
373 return 0;
374}
375
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800376static void free_func_state(struct bpf_func_state *state)
377{
378 kfree(state->stack);
379 kfree(state);
380}
381
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700382static void free_verifier_state(struct bpf_verifier_state *state,
383 bool free_self)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700384{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800385 int i;
386
387 for (i = 0; i <= state->curframe; i++) {
388 free_func_state(state->frame[i]);
389 state->frame[i] = NULL;
390 }
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700391 if (free_self)
392 kfree(state);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700393}
394
395/* copy verifier state from src to dst growing dst stack space
396 * when necessary to accommodate larger src stack
397 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800398static int copy_func_state(struct bpf_func_state *dst,
399 const struct bpf_func_state *src)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700400{
401 int err;
402
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800403 err = realloc_func_state(dst, src->allocated_stack, false);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700404 if (err)
405 return err;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800406 memcpy(dst, src, offsetof(struct bpf_func_state, allocated_stack));
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700407 return copy_stack_state(dst, src);
408}
409
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800410static int copy_verifier_state(struct bpf_verifier_state *dst_state,
411 const struct bpf_verifier_state *src)
412{
413 struct bpf_func_state *dst;
414 int i, err;
415
416 /* if dst has more stack frames then src frame, free them */
417 for (i = src->curframe + 1; i <= dst_state->curframe; i++) {
418 free_func_state(dst_state->frame[i]);
419 dst_state->frame[i] = NULL;
420 }
421 dst_state->curframe = src->curframe;
422 dst_state->parent = src->parent;
423 for (i = 0; i <= src->curframe; i++) {
424 dst = dst_state->frame[i];
425 if (!dst) {
426 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
427 if (!dst)
428 return -ENOMEM;
429 dst_state->frame[i] = dst;
430 }
431 err = copy_func_state(dst, src->frame[i]);
432 if (err)
433 return err;
434 }
435 return 0;
436}
437
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700438static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx,
439 int *insn_idx)
440{
441 struct bpf_verifier_state *cur = env->cur_state;
442 struct bpf_verifier_stack_elem *elem, *head = env->head;
443 int err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700444
445 if (env->head == NULL)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700446 return -ENOENT;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700447
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700448 if (cur) {
449 err = copy_verifier_state(cur, &head->st);
450 if (err)
451 return err;
452 }
453 if (insn_idx)
454 *insn_idx = head->insn_idx;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700455 if (prev_insn_idx)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700456 *prev_insn_idx = head->prev_insn_idx;
457 elem = head->next;
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700458 free_verifier_state(&head->st, false);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700459 kfree(head);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700460 env->head = elem;
461 env->stack_size--;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700462 return 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700463}
464
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100465static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,
466 int insn_idx, int prev_insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700467{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700468 struct bpf_verifier_state *cur = env->cur_state;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100469 struct bpf_verifier_stack_elem *elem;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700470 int err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700471
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700472 elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700473 if (!elem)
474 goto err;
475
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700476 elem->insn_idx = insn_idx;
477 elem->prev_insn_idx = prev_insn_idx;
478 elem->next = env->head;
479 env->head = elem;
480 env->stack_size++;
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700481 err = copy_verifier_state(&elem->st, cur);
482 if (err)
483 goto err;
Daniel Borkmann07016152016-04-05 22:33:17 +0200484 if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700485 verbose(env, "BPF program is too complex\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700486 goto err;
487 }
488 return &elem->st;
489err:
490 /* pop all elements and return */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700491 while (!pop_stack(env, NULL, NULL));
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700492 return NULL;
493}
494
495#define CALLER_SAVED_REGS 6
496static const int caller_saved[CALLER_SAVED_REGS] = {
497 BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
498};
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800499#define CALLEE_SAVED_REGS 5
500static const int callee_saved[CALLEE_SAVED_REGS] = {
501 BPF_REG_6, BPF_REG_7, BPF_REG_8, BPF_REG_9
502};
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700503
Edward Creef1174f72017-08-07 15:26:19 +0100504static void __mark_reg_not_init(struct bpf_reg_state *reg);
505
Edward Creeb03c9f92017-08-07 15:26:36 +0100506/* Mark the unknown part of a register (variable offset or scalar value) as
507 * known to have the value @imm.
508 */
509static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
510{
511 reg->id = 0;
512 reg->var_off = tnum_const(imm);
513 reg->smin_value = (s64)imm;
514 reg->smax_value = (s64)imm;
515 reg->umin_value = imm;
516 reg->umax_value = imm;
517}
518
Edward Creef1174f72017-08-07 15:26:19 +0100519/* Mark the 'variable offset' part of a register as zero. This should be
520 * used only on registers holding a pointer type.
521 */
522static void __mark_reg_known_zero(struct bpf_reg_state *reg)
523{
Edward Creeb03c9f92017-08-07 15:26:36 +0100524 __mark_reg_known(reg, 0);
Edward Creef1174f72017-08-07 15:26:19 +0100525}
526
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800527static void __mark_reg_const_zero(struct bpf_reg_state *reg)
528{
529 __mark_reg_known(reg, 0);
530 reg->off = 0;
531 reg->type = SCALAR_VALUE;
532}
533
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700534static void mark_reg_known_zero(struct bpf_verifier_env *env,
535 struct bpf_reg_state *regs, u32 regno)
Edward Creef1174f72017-08-07 15:26:19 +0100536{
537 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700538 verbose(env, "mark_reg_known_zero(regs, %u)\n", regno);
Edward Creef1174f72017-08-07 15:26:19 +0100539 /* Something bad happened, let's kill all regs */
540 for (regno = 0; regno < MAX_BPF_REG; regno++)
541 __mark_reg_not_init(regs + regno);
542 return;
543 }
544 __mark_reg_known_zero(regs + regno);
545}
546
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200547static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg)
548{
549 return type_is_pkt_pointer(reg->type);
550}
551
552static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg)
553{
554 return reg_is_pkt_pointer(reg) ||
555 reg->type == PTR_TO_PACKET_END;
556}
557
558/* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */
559static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg,
560 enum bpf_reg_type which)
561{
562 /* The register can already have a range from prior markings.
563 * This is fine as long as it hasn't been advanced from its
564 * origin.
565 */
566 return reg->type == which &&
567 reg->id == 0 &&
568 reg->off == 0 &&
569 tnum_equals_const(reg->var_off, 0);
570}
571
Edward Creeb03c9f92017-08-07 15:26:36 +0100572/* Attempts to improve min/max values based on var_off information */
573static void __update_reg_bounds(struct bpf_reg_state *reg)
574{
575 /* min signed is max(sign bit) | min(other bits) */
576 reg->smin_value = max_t(s64, reg->smin_value,
577 reg->var_off.value | (reg->var_off.mask & S64_MIN));
578 /* max signed is min(sign bit) | max(other bits) */
579 reg->smax_value = min_t(s64, reg->smax_value,
580 reg->var_off.value | (reg->var_off.mask & S64_MAX));
581 reg->umin_value = max(reg->umin_value, reg->var_off.value);
582 reg->umax_value = min(reg->umax_value,
583 reg->var_off.value | reg->var_off.mask);
584}
585
586/* Uses signed min/max values to inform unsigned, and vice-versa */
587static void __reg_deduce_bounds(struct bpf_reg_state *reg)
588{
589 /* Learn sign from signed bounds.
590 * If we cannot cross the sign boundary, then signed and unsigned bounds
591 * are the same, so combine. This works even in the negative case, e.g.
592 * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff.
593 */
594 if (reg->smin_value >= 0 || reg->smax_value < 0) {
595 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
596 reg->umin_value);
597 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
598 reg->umax_value);
599 return;
600 }
601 /* Learn sign from unsigned bounds. Signed bounds cross the sign
602 * boundary, so we must be careful.
603 */
604 if ((s64)reg->umax_value >= 0) {
605 /* Positive. We can't learn anything from the smin, but smax
606 * is positive, hence safe.
607 */
608 reg->smin_value = reg->umin_value;
609 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
610 reg->umax_value);
611 } else if ((s64)reg->umin_value < 0) {
612 /* Negative. We can't learn anything from the smax, but smin
613 * is negative, hence safe.
614 */
615 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
616 reg->umin_value);
617 reg->smax_value = reg->umax_value;
618 }
619}
620
621/* Attempts to improve var_off based on unsigned min/max information */
622static void __reg_bound_offset(struct bpf_reg_state *reg)
623{
624 reg->var_off = tnum_intersect(reg->var_off,
625 tnum_range(reg->umin_value,
626 reg->umax_value));
627}
628
629/* Reset the min/max bounds of a register */
630static void __mark_reg_unbounded(struct bpf_reg_state *reg)
631{
632 reg->smin_value = S64_MIN;
633 reg->smax_value = S64_MAX;
634 reg->umin_value = 0;
635 reg->umax_value = U64_MAX;
636}
637
Edward Creef1174f72017-08-07 15:26:19 +0100638/* Mark a register as having a completely unknown (scalar) value. */
639static void __mark_reg_unknown(struct bpf_reg_state *reg)
640{
641 reg->type = SCALAR_VALUE;
642 reg->id = 0;
643 reg->off = 0;
644 reg->var_off = tnum_unknown;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800645 reg->frameno = 0;
Edward Creeb03c9f92017-08-07 15:26:36 +0100646 __mark_reg_unbounded(reg);
Edward Creef1174f72017-08-07 15:26:19 +0100647}
648
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700649static void mark_reg_unknown(struct bpf_verifier_env *env,
650 struct bpf_reg_state *regs, u32 regno)
Edward Creef1174f72017-08-07 15:26:19 +0100651{
652 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700653 verbose(env, "mark_reg_unknown(regs, %u)\n", regno);
Alexei Starovoitov19ceb412017-11-30 21:31:37 -0800654 /* Something bad happened, let's kill all regs except FP */
655 for (regno = 0; regno < BPF_REG_FP; regno++)
Edward Creef1174f72017-08-07 15:26:19 +0100656 __mark_reg_not_init(regs + regno);
657 return;
658 }
659 __mark_reg_unknown(regs + regno);
660}
661
662static void __mark_reg_not_init(struct bpf_reg_state *reg)
663{
664 __mark_reg_unknown(reg);
665 reg->type = NOT_INIT;
666}
667
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700668static void mark_reg_not_init(struct bpf_verifier_env *env,
669 struct bpf_reg_state *regs, u32 regno)
Daniel Borkmanna9789ef2017-05-25 01:05:06 +0200670{
Edward Creef1174f72017-08-07 15:26:19 +0100671 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700672 verbose(env, "mark_reg_not_init(regs, %u)\n", regno);
Alexei Starovoitov19ceb412017-11-30 21:31:37 -0800673 /* Something bad happened, let's kill all regs except FP */
674 for (regno = 0; regno < BPF_REG_FP; regno++)
Edward Creef1174f72017-08-07 15:26:19 +0100675 __mark_reg_not_init(regs + regno);
676 return;
677 }
678 __mark_reg_not_init(regs + regno);
Daniel Borkmanna9789ef2017-05-25 01:05:06 +0200679}
680
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700681static void init_reg_state(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800682 struct bpf_func_state *state)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700683{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800684 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700685 int i;
686
Edward Creedc503a82017-08-15 20:34:35 +0100687 for (i = 0; i < MAX_BPF_REG; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700688 mark_reg_not_init(env, regs, i);
Edward Creedc503a82017-08-15 20:34:35 +0100689 regs[i].live = REG_LIVE_NONE;
690 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700691
692 /* frame pointer */
Edward Creef1174f72017-08-07 15:26:19 +0100693 regs[BPF_REG_FP].type = PTR_TO_STACK;
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700694 mark_reg_known_zero(env, regs, BPF_REG_FP);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800695 regs[BPF_REG_FP].frameno = state->frameno;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700696
697 /* 1st arg to a function */
698 regs[BPF_REG_1].type = PTR_TO_CTX;
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700699 mark_reg_known_zero(env, regs, BPF_REG_1);
Daniel Borkmann6760bf22016-12-18 01:52:59 +0100700}
701
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800702#define BPF_MAIN_FUNC (-1)
703static void init_func_state(struct bpf_verifier_env *env,
704 struct bpf_func_state *state,
705 int callsite, int frameno, int subprogno)
706{
707 state->callsite = callsite;
708 state->frameno = frameno;
709 state->subprogno = subprogno;
710 init_reg_state(env, state);
711}
712
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700713enum reg_arg_type {
714 SRC_OP, /* register is used as source operand */
715 DST_OP, /* register is used as destination operand */
716 DST_OP_NO_MARK /* same as above, check only, don't mark */
717};
718
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800719static int cmp_subprogs(const void *a, const void *b)
720{
721 return *(int *)a - *(int *)b;
722}
723
724static int find_subprog(struct bpf_verifier_env *env, int off)
725{
726 u32 *p;
727
728 p = bsearch(&off, env->subprog_starts, env->subprog_cnt,
729 sizeof(env->subprog_starts[0]), cmp_subprogs);
730 if (!p)
731 return -ENOENT;
732 return p - env->subprog_starts;
733
734}
735
736static int add_subprog(struct bpf_verifier_env *env, int off)
737{
738 int insn_cnt = env->prog->len;
739 int ret;
740
741 if (off >= insn_cnt || off < 0) {
742 verbose(env, "call to invalid destination\n");
743 return -EINVAL;
744 }
745 ret = find_subprog(env, off);
746 if (ret >= 0)
747 return 0;
748 if (env->subprog_cnt >= BPF_MAX_SUBPROGS) {
749 verbose(env, "too many subprograms\n");
750 return -E2BIG;
751 }
752 env->subprog_starts[env->subprog_cnt++] = off;
753 sort(env->subprog_starts, env->subprog_cnt,
754 sizeof(env->subprog_starts[0]), cmp_subprogs, NULL);
755 return 0;
756}
757
758static int check_subprogs(struct bpf_verifier_env *env)
759{
760 int i, ret, subprog_start, subprog_end, off, cur_subprog = 0;
761 struct bpf_insn *insn = env->prog->insnsi;
762 int insn_cnt = env->prog->len;
763
764 /* determine subprog starts. The end is one before the next starts */
765 for (i = 0; i < insn_cnt; i++) {
766 if (insn[i].code != (BPF_JMP | BPF_CALL))
767 continue;
768 if (insn[i].src_reg != BPF_PSEUDO_CALL)
769 continue;
770 if (!env->allow_ptr_leaks) {
771 verbose(env, "function calls to other bpf functions are allowed for root only\n");
772 return -EPERM;
773 }
774 if (bpf_prog_is_dev_bound(env->prog->aux)) {
Colin Ian Kinge90004d52017-12-18 14:03:12 +0000775 verbose(env, "function calls in offloaded programs are not supported yet\n");
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800776 return -EINVAL;
777 }
778 ret = add_subprog(env, i + insn[i].imm + 1);
779 if (ret < 0)
780 return ret;
781 }
782
783 if (env->log.level > 1)
784 for (i = 0; i < env->subprog_cnt; i++)
785 verbose(env, "func#%d @%d\n", i, env->subprog_starts[i]);
786
787 /* now check that all jumps are within the same subprog */
788 subprog_start = 0;
789 if (env->subprog_cnt == cur_subprog)
790 subprog_end = insn_cnt;
791 else
792 subprog_end = env->subprog_starts[cur_subprog++];
793 for (i = 0; i < insn_cnt; i++) {
794 u8 code = insn[i].code;
795
796 if (BPF_CLASS(code) != BPF_JMP)
797 goto next;
798 if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL)
799 goto next;
800 off = i + insn[i].off + 1;
801 if (off < subprog_start || off >= subprog_end) {
802 verbose(env, "jump out of range from insn %d to %d\n", i, off);
803 return -EINVAL;
804 }
805next:
806 if (i == subprog_end - 1) {
807 /* to avoid fall-through from one subprog into another
808 * the last insn of the subprog should be either exit
809 * or unconditional jump back
810 */
811 if (code != (BPF_JMP | BPF_EXIT) &&
812 code != (BPF_JMP | BPF_JA)) {
813 verbose(env, "last insn is not an exit or jmp\n");
814 return -EINVAL;
815 }
816 subprog_start = subprog_end;
817 if (env->subprog_cnt == cur_subprog)
818 subprog_end = insn_cnt;
819 else
820 subprog_end = env->subprog_starts[cur_subprog++];
821 }
822 }
823 return 0;
824}
825
Colin Ian Kingfa2d41a2017-12-18 17:47:07 +0000826static
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800827struct bpf_verifier_state *skip_callee(struct bpf_verifier_env *env,
828 const struct bpf_verifier_state *state,
829 struct bpf_verifier_state *parent,
830 u32 regno)
Edward Creedc503a82017-08-15 20:34:35 +0100831{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800832 struct bpf_verifier_state *tmp = NULL;
833
834 /* 'parent' could be a state of caller and
835 * 'state' could be a state of callee. In such case
836 * parent->curframe < state->curframe
837 * and it's ok for r1 - r5 registers
838 *
839 * 'parent' could be a callee's state after it bpf_exit-ed.
840 * In such case parent->curframe > state->curframe
841 * and it's ok for r0 only
842 */
843 if (parent->curframe == state->curframe ||
844 (parent->curframe < state->curframe &&
845 regno >= BPF_REG_1 && regno <= BPF_REG_5) ||
846 (parent->curframe > state->curframe &&
847 regno == BPF_REG_0))
848 return parent;
849
850 if (parent->curframe > state->curframe &&
851 regno >= BPF_REG_6) {
852 /* for callee saved regs we have to skip the whole chain
853 * of states that belong to callee and mark as LIVE_READ
854 * the registers before the call
855 */
856 tmp = parent;
857 while (tmp && tmp->curframe != state->curframe) {
858 tmp = tmp->parent;
859 }
860 if (!tmp)
861 goto bug;
862 parent = tmp;
863 } else {
864 goto bug;
865 }
866 return parent;
867bug:
868 verbose(env, "verifier bug regno %d tmp %p\n", regno, tmp);
869 verbose(env, "regno %d parent frame %d current frame %d\n",
870 regno, parent->curframe, state->curframe);
Colin Ian Kingfa2d41a2017-12-18 17:47:07 +0000871 return NULL;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800872}
873
874static int mark_reg_read(struct bpf_verifier_env *env,
875 const struct bpf_verifier_state *state,
876 struct bpf_verifier_state *parent,
877 u32 regno)
878{
879 bool writes = parent == state->parent; /* Observe write marks */
Edward Creedc503a82017-08-15 20:34:35 +0100880
Alexei Starovoitov8fe2d6c2017-10-05 16:20:56 -0700881 if (regno == BPF_REG_FP)
882 /* We don't need to worry about FP liveness because it's read-only */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800883 return 0;
Alexei Starovoitov8fe2d6c2017-10-05 16:20:56 -0700884
Edward Creedc503a82017-08-15 20:34:35 +0100885 while (parent) {
886 /* if read wasn't screened by an earlier write ... */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800887 if (writes && state->frame[state->curframe]->regs[regno].live & REG_LIVE_WRITTEN)
Edward Creedc503a82017-08-15 20:34:35 +0100888 break;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800889 parent = skip_callee(env, state, parent, regno);
890 if (!parent)
891 return -EFAULT;
Edward Creedc503a82017-08-15 20:34:35 +0100892 /* ... then we depend on parent's value */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800893 parent->frame[parent->curframe]->regs[regno].live |= REG_LIVE_READ;
Edward Creedc503a82017-08-15 20:34:35 +0100894 state = parent;
895 parent = state->parent;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800896 writes = true;
Edward Creedc503a82017-08-15 20:34:35 +0100897 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800898 return 0;
Edward Creedc503a82017-08-15 20:34:35 +0100899}
900
901static int check_reg_arg(struct bpf_verifier_env *env, u32 regno,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700902 enum reg_arg_type t)
903{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800904 struct bpf_verifier_state *vstate = env->cur_state;
905 struct bpf_func_state *state = vstate->frame[vstate->curframe];
906 struct bpf_reg_state *regs = state->regs;
Edward Creedc503a82017-08-15 20:34:35 +0100907
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700908 if (regno >= MAX_BPF_REG) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700909 verbose(env, "R%d is invalid\n", regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700910 return -EINVAL;
911 }
912
913 if (t == SRC_OP) {
914 /* check whether register used as source operand can be read */
915 if (regs[regno].type == NOT_INIT) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700916 verbose(env, "R%d !read_ok\n", regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700917 return -EACCES;
918 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800919 return mark_reg_read(env, vstate, vstate->parent, regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700920 } else {
921 /* check whether register used as dest operand can be written to */
922 if (regno == BPF_REG_FP) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700923 verbose(env, "frame pointer is read only\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700924 return -EACCES;
925 }
Edward Creedc503a82017-08-15 20:34:35 +0100926 regs[regno].live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700927 if (t == DST_OP)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700928 mark_reg_unknown(env, regs, regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700929 }
930 return 0;
931}
932
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700933static bool is_spillable_regtype(enum bpf_reg_type type)
934{
935 switch (type) {
936 case PTR_TO_MAP_VALUE:
937 case PTR_TO_MAP_VALUE_OR_NULL:
938 case PTR_TO_STACK:
939 case PTR_TO_CTX:
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700940 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200941 case PTR_TO_PACKET_META:
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700942 case PTR_TO_PACKET_END:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700943 case CONST_PTR_TO_MAP:
944 return true;
945 default:
946 return false;
947 }
948}
949
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800950/* Does this register contain a constant zero? */
951static bool register_is_null(struct bpf_reg_state *reg)
952{
953 return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0);
954}
955
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700956/* check_stack_read/write functions track spill/fill of registers,
957 * stack boundary and alignment are checked in check_mem_access()
958 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700959static int check_stack_write(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800960 struct bpf_func_state *state, /* func where register points to */
961 int off, int size, int value_regno)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700962{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800963 struct bpf_func_state *cur; /* state of the current function */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700964 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800965 enum bpf_reg_type type;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700966
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800967 err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE),
968 true);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700969 if (err)
970 return err;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700971 /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
972 * so it's aligned access and [off, off + size) are within stack limits
973 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700974 if (!env->allow_ptr_leaks &&
975 state->stack[spi].slot_type[0] == STACK_SPILL &&
976 size != BPF_REG_SIZE) {
977 verbose(env, "attempt to corrupt spilled pointer on stack\n");
978 return -EACCES;
979 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700980
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800981 cur = env->cur_state->frame[env->cur_state->curframe];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700982 if (value_regno >= 0 &&
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800983 is_spillable_regtype((type = cur->regs[value_regno].type))) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700984
985 /* register containing pointer is being spilled into stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700986 if (size != BPF_REG_SIZE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700987 verbose(env, "invalid size of register spill\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700988 return -EACCES;
989 }
990
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800991 if (state != cur && type == PTR_TO_STACK) {
992 verbose(env, "cannot spill pointers to stack into stack frame of the caller\n");
993 return -EINVAL;
994 }
995
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700996 /* save register state */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800997 state->stack[spi].spilled_ptr = cur->regs[value_regno];
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700998 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700999
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001000 for (i = 0; i < BPF_REG_SIZE; i++)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001001 state->stack[spi].slot_type[i] = STACK_SPILL;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001002 } else {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001003 u8 type = STACK_MISC;
1004
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001005 /* regular write of data into stack */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001006 state->stack[spi].spilled_ptr = (struct bpf_reg_state) {};
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001007
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001008 /* only mark the slot as written if all 8 bytes were written
1009 * otherwise read propagation may incorrectly stop too soon
1010 * when stack slots are partially written.
1011 * This heuristic means that read propagation will be
1012 * conservative, since it will add reg_live_read marks
1013 * to stack slots all the way to first state when programs
1014 * writes+reads less than 8 bytes
1015 */
1016 if (size == BPF_REG_SIZE)
1017 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
1018
1019 /* when we zero initialize stack slots mark them as such */
1020 if (value_regno >= 0 &&
1021 register_is_null(&cur->regs[value_regno]))
1022 type = STACK_ZERO;
1023
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001024 for (i = 0; i < size; i++)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001025 state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] =
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001026 type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001027 }
1028 return 0;
1029}
1030
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001031/* registers of every function are unique and mark_reg_read() propagates
1032 * the liveness in the following cases:
1033 * - from callee into caller for R1 - R5 that were used as arguments
1034 * - from caller into callee for R0 that used as result of the call
1035 * - from caller to the same caller skipping states of the callee for R6 - R9,
1036 * since R6 - R9 are callee saved by implicit function prologue and
1037 * caller's R6 != callee's R6, so when we propagate liveness up to
1038 * parent states we need to skip callee states for R6 - R9.
1039 *
1040 * stack slot marking is different, since stacks of caller and callee are
1041 * accessible in both (since caller can pass a pointer to caller's stack to
1042 * callee which can pass it to another function), hence mark_stack_slot_read()
1043 * has to propagate the stack liveness to all parent states at given frame number.
1044 * Consider code:
1045 * f1() {
1046 * ptr = fp - 8;
1047 * *ptr = ctx;
1048 * call f2 {
1049 * .. = *ptr;
1050 * }
1051 * .. = *ptr;
1052 * }
1053 * First *ptr is reading from f1's stack and mark_stack_slot_read() has
1054 * to mark liveness at the f1's frame and not f2's frame.
1055 * Second *ptr is also reading from f1's stack and mark_stack_slot_read() has
1056 * to propagate liveness to f2 states at f1's frame level and further into
1057 * f1 states at f1's frame level until write into that stack slot
1058 */
1059static void mark_stack_slot_read(struct bpf_verifier_env *env,
1060 const struct bpf_verifier_state *state,
1061 struct bpf_verifier_state *parent,
1062 int slot, int frameno)
Edward Creedc503a82017-08-15 20:34:35 +01001063{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001064 bool writes = parent == state->parent; /* Observe write marks */
Edward Creedc503a82017-08-15 20:34:35 +01001065
1066 while (parent) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001067 if (parent->frame[frameno]->allocated_stack <= slot * BPF_REG_SIZE)
1068 /* since LIVE_WRITTEN mark is only done for full 8-byte
1069 * write the read marks are conservative and parent
1070 * state may not even have the stack allocated. In such case
1071 * end the propagation, since the loop reached beginning
1072 * of the function
1073 */
1074 break;
Edward Creedc503a82017-08-15 20:34:35 +01001075 /* if read wasn't screened by an earlier write ... */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001076 if (writes && state->frame[frameno]->stack[slot].spilled_ptr.live & REG_LIVE_WRITTEN)
Edward Creedc503a82017-08-15 20:34:35 +01001077 break;
1078 /* ... then we depend on parent's value */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001079 parent->frame[frameno]->stack[slot].spilled_ptr.live |= REG_LIVE_READ;
Edward Creedc503a82017-08-15 20:34:35 +01001080 state = parent;
1081 parent = state->parent;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001082 writes = true;
Edward Creedc503a82017-08-15 20:34:35 +01001083 }
1084}
1085
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001086static int check_stack_read(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001087 struct bpf_func_state *reg_state /* func where register points to */,
1088 int off, int size, int value_regno)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001089{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001090 struct bpf_verifier_state *vstate = env->cur_state;
1091 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001092 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE;
1093 u8 *stype;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001094
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001095 if (reg_state->allocated_stack <= slot) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001096 verbose(env, "invalid read from stack off %d+0 size %d\n",
1097 off, size);
1098 return -EACCES;
1099 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001100 stype = reg_state->stack[spi].slot_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001101
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001102 if (stype[0] == STACK_SPILL) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001103 if (size != BPF_REG_SIZE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001104 verbose(env, "invalid size of register spill\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001105 return -EACCES;
1106 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001107 for (i = 1; i < BPF_REG_SIZE; i++) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001108 if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001109 verbose(env, "corrupted spill memory\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001110 return -EACCES;
1111 }
1112 }
1113
Edward Creedc503a82017-08-15 20:34:35 +01001114 if (value_regno >= 0) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001115 /* restore register state from stack */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001116 state->regs[value_regno] = reg_state->stack[spi].spilled_ptr;
Alexei Starovoitov2f18f622017-11-30 21:31:38 -08001117 /* mark reg as written since spilled pointer state likely
1118 * has its liveness marks cleared by is_state_visited()
1119 * which resets stack/reg liveness for state transitions
1120 */
1121 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
Edward Creedc503a82017-08-15 20:34:35 +01001122 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001123 mark_stack_slot_read(env, vstate, vstate->parent, spi,
1124 reg_state->frameno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001125 return 0;
1126 } else {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001127 int zeros = 0;
1128
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001129 for (i = 0; i < size; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001130 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC)
1131 continue;
1132 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) {
1133 zeros++;
1134 continue;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001135 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001136 verbose(env, "invalid read from stack off %d+%d size %d\n",
1137 off, i, size);
1138 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001139 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001140 mark_stack_slot_read(env, vstate, vstate->parent, spi,
1141 reg_state->frameno);
1142 if (value_regno >= 0) {
1143 if (zeros == size) {
1144 /* any size read into register is zero extended,
1145 * so the whole register == const_zero
1146 */
1147 __mark_reg_const_zero(&state->regs[value_regno]);
1148 } else {
1149 /* have read misc data from the stack */
1150 mark_reg_unknown(env, state->regs, value_regno);
1151 }
1152 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
1153 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001154 return 0;
1155 }
1156}
1157
1158/* check read/write into map element returned by bpf_map_lookup_elem() */
Edward Creef1174f72017-08-07 15:26:19 +01001159static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001160 int size, bool zero_size_allowed)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001161{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001162 struct bpf_reg_state *regs = cur_regs(env);
1163 struct bpf_map *map = regs[regno].map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001164
Yonghong Song9fd29c02017-11-12 14:49:09 -08001165 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
1166 off + size > map->value_size) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001167 verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001168 map->value_size, off, size);
1169 return -EACCES;
1170 }
1171 return 0;
1172}
1173
Edward Creef1174f72017-08-07 15:26:19 +01001174/* check read/write into a map element with possible variable offset */
1175static int check_map_access(struct bpf_verifier_env *env, u32 regno,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001176 int off, int size, bool zero_size_allowed)
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001177{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001178 struct bpf_verifier_state *vstate = env->cur_state;
1179 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001180 struct bpf_reg_state *reg = &state->regs[regno];
1181 int err;
1182
Edward Creef1174f72017-08-07 15:26:19 +01001183 /* We may have adjusted the register to this map value, so we
1184 * need to try adding each of min_value and max_value to off
1185 * to make sure our theoretical access will be safe.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001186 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001187 if (env->log.level)
1188 print_verifier_state(env, state);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001189 /* The minimum value is only important with signed
1190 * comparisons where we can't assume the floor of a
1191 * value is 0. If we are using signed variables for our
1192 * index'es we need to make sure that whatever we use
1193 * will have a set floor within our range.
1194 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001195 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001196 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 -08001197 regno);
1198 return -EACCES;
1199 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001200 err = __check_map_access(env, regno, reg->smin_value + off, size,
1201 zero_size_allowed);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001202 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001203 verbose(env, "R%d min value is outside of the array range\n",
1204 regno);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001205 return err;
1206 }
1207
Edward Creeb03c9f92017-08-07 15:26:36 +01001208 /* If we haven't set a max value then we need to bail since we can't be
1209 * sure we won't do bad things.
1210 * If reg->umax_value + off could overflow, treat that as unbounded too.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001211 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001212 if (reg->umax_value >= BPF_MAX_VAR_OFF) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001213 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 -08001214 regno);
1215 return -EACCES;
1216 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001217 err = __check_map_access(env, regno, reg->umax_value + off, size,
1218 zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01001219 if (err)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001220 verbose(env, "R%d max value is outside of the array range\n",
1221 regno);
Edward Creef1174f72017-08-07 15:26:19 +01001222 return err;
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001223}
1224
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001225#define MAX_PACKET_OFF 0xffff
1226
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001227static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001228 const struct bpf_call_arg_meta *meta,
1229 enum bpf_access_type t)
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001230{
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001231 switch (env->prog->type) {
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001232 case BPF_PROG_TYPE_LWT_IN:
1233 case BPF_PROG_TYPE_LWT_OUT:
1234 /* dst_input() and dst_output() can't write for now */
1235 if (t == BPF_WRITE)
1236 return false;
Alexander Alemayhu7e57fbb2017-02-14 00:02:35 +01001237 /* fallthrough */
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001238 case BPF_PROG_TYPE_SCHED_CLS:
1239 case BPF_PROG_TYPE_SCHED_ACT:
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001240 case BPF_PROG_TYPE_XDP:
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001241 case BPF_PROG_TYPE_LWT_XMIT:
John Fastabend8a31db52017-08-15 22:33:09 -07001242 case BPF_PROG_TYPE_SK_SKB:
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001243 if (meta)
1244 return meta->pkt_access;
1245
1246 env->seen_direct_write = true;
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001247 return true;
1248 default:
1249 return false;
1250 }
1251}
1252
Edward Creef1174f72017-08-07 15:26:19 +01001253static int __check_packet_access(struct bpf_verifier_env *env, u32 regno,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001254 int off, int size, bool zero_size_allowed)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001255{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001256 struct bpf_reg_state *regs = cur_regs(env);
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001257 struct bpf_reg_state *reg = &regs[regno];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001258
Yonghong Song9fd29c02017-11-12 14:49:09 -08001259 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
1260 (u64)off + size > reg->range) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001261 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 -07001262 off, size, regno, reg->id, reg->off, reg->range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001263 return -EACCES;
1264 }
1265 return 0;
1266}
1267
Edward Creef1174f72017-08-07 15:26:19 +01001268static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001269 int size, bool zero_size_allowed)
Edward Creef1174f72017-08-07 15:26:19 +01001270{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001271 struct bpf_reg_state *regs = cur_regs(env);
Edward Creef1174f72017-08-07 15:26:19 +01001272 struct bpf_reg_state *reg = &regs[regno];
1273 int err;
1274
1275 /* We may have added a variable offset to the packet pointer; but any
1276 * reg->range we have comes after that. We are only checking the fixed
1277 * offset.
1278 */
1279
1280 /* We don't allow negative numbers, because we aren't tracking enough
1281 * detail to prove they're safe.
1282 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001283 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001284 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 +01001285 regno);
1286 return -EACCES;
1287 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001288 err = __check_packet_access(env, regno, off, size, zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01001289 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001290 verbose(env, "R%d offset is outside of the packet\n", regno);
Edward Creef1174f72017-08-07 15:26:19 +01001291 return err;
1292 }
1293 return err;
1294}
1295
1296/* check access to 'struct bpf_context' fields. Supports fixed offsets only */
Yonghong Song31fd8582017-06-13 15:52:13 -07001297static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07001298 enum bpf_access_type t, enum bpf_reg_type *reg_type)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001299{
Daniel Borkmannf96da092017-07-02 02:13:27 +02001300 struct bpf_insn_access_aux info = {
1301 .reg_type = *reg_type,
1302 };
Yonghong Song31fd8582017-06-13 15:52:13 -07001303
Jakub Kicinski4f9218a2017-10-16 16:40:55 -07001304 if (env->ops->is_valid_access &&
1305 env->ops->is_valid_access(off, size, t, &info)) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02001306 /* A non zero info.ctx_field_size indicates that this field is a
1307 * candidate for later verifier transformation to load the whole
1308 * field and then apply a mask when accessed with a narrower
1309 * access than actual ctx access size. A zero info.ctx_field_size
1310 * will only allow for whole field access and rejects any other
1311 * type of narrower access.
Yonghong Song31fd8582017-06-13 15:52:13 -07001312 */
Yonghong Song23994632017-06-22 15:07:39 -07001313 *reg_type = info.reg_type;
Yonghong Song31fd8582017-06-13 15:52:13 -07001314
Jakub Kicinski4f9218a2017-10-16 16:40:55 -07001315 env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07001316 /* remember the offset of last byte accessed in ctx */
1317 if (env->prog->aux->max_ctx_offset < off + size)
1318 env->prog->aux->max_ctx_offset = off + size;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001319 return 0;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07001320 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001321
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001322 verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001323 return -EACCES;
1324}
1325
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001326static bool __is_pointer_value(bool allow_ptr_leaks,
1327 const struct bpf_reg_state *reg)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001328{
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001329 if (allow_ptr_leaks)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001330 return false;
1331
Edward Creef1174f72017-08-07 15:26:19 +01001332 return reg->type != SCALAR_VALUE;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001333}
1334
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001335static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
1336{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001337 return __is_pointer_value(env->allow_ptr_leaks, cur_regs(env) + regno);
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001338}
1339
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001340static int check_pkt_ptr_alignment(struct bpf_verifier_env *env,
1341 const struct bpf_reg_state *reg,
David S. Millerd1174412017-05-10 11:22:52 -07001342 int off, int size, bool strict)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001343{
Edward Creef1174f72017-08-07 15:26:19 +01001344 struct tnum reg_off;
David S. Millere07b98d2017-05-10 11:38:07 -07001345 int ip_align;
David S. Millerd1174412017-05-10 11:22:52 -07001346
1347 /* Byte size accesses are always allowed. */
1348 if (!strict || size == 1)
1349 return 0;
1350
David S. Millere4eda882017-05-22 12:27:07 -04001351 /* For platforms that do not have a Kconfig enabling
1352 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of
1353 * NET_IP_ALIGN is universally set to '2'. And on platforms
1354 * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get
1355 * to this code only in strict mode where we want to emulate
1356 * the NET_IP_ALIGN==2 checking. Therefore use an
1357 * unconditional IP align value of '2'.
David S. Millere07b98d2017-05-10 11:38:07 -07001358 */
David S. Millere4eda882017-05-22 12:27:07 -04001359 ip_align = 2;
Edward Creef1174f72017-08-07 15:26:19 +01001360
1361 reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off));
1362 if (!tnum_is_aligned(reg_off, size)) {
1363 char tn_buf[48];
1364
1365 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001366 verbose(env,
1367 "misaligned packet access off %d+%s+%d+%d size %d\n",
Edward Creef1174f72017-08-07 15:26:19 +01001368 ip_align, tn_buf, reg->off, off, size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001369 return -EACCES;
1370 }
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001371
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001372 return 0;
1373}
1374
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001375static int check_generic_ptr_alignment(struct bpf_verifier_env *env,
1376 const struct bpf_reg_state *reg,
Edward Creef1174f72017-08-07 15:26:19 +01001377 const char *pointer_desc,
1378 int off, int size, bool strict)
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001379{
Edward Creef1174f72017-08-07 15:26:19 +01001380 struct tnum reg_off;
1381
1382 /* Byte size accesses are always allowed. */
1383 if (!strict || size == 1)
1384 return 0;
1385
1386 reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off));
1387 if (!tnum_is_aligned(reg_off, size)) {
1388 char tn_buf[48];
1389
1390 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001391 verbose(env, "misaligned %saccess off %s+%d+%d size %d\n",
Edward Creef1174f72017-08-07 15:26:19 +01001392 pointer_desc, tn_buf, reg->off, off, size);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001393 return -EACCES;
1394 }
1395
1396 return 0;
1397}
1398
David S. Millere07b98d2017-05-10 11:38:07 -07001399static int check_ptr_alignment(struct bpf_verifier_env *env,
1400 const struct bpf_reg_state *reg,
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001401 int off, int size)
1402{
David S. Millere07b98d2017-05-10 11:38:07 -07001403 bool strict = env->strict_alignment;
Edward Creef1174f72017-08-07 15:26:19 +01001404 const char *pointer_desc = "";
David S. Millerd1174412017-05-10 11:22:52 -07001405
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001406 switch (reg->type) {
1407 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001408 case PTR_TO_PACKET_META:
1409 /* Special case, because of NET_IP_ALIGN. Given metadata sits
1410 * right in front, treat it the very same way.
1411 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001412 return check_pkt_ptr_alignment(env, reg, off, size, strict);
Edward Creef1174f72017-08-07 15:26:19 +01001413 case PTR_TO_MAP_VALUE:
1414 pointer_desc = "value ";
1415 break;
1416 case PTR_TO_CTX:
1417 pointer_desc = "context ";
1418 break;
1419 case PTR_TO_STACK:
1420 pointer_desc = "stack ";
1421 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001422 default:
Edward Creef1174f72017-08-07 15:26:19 +01001423 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001424 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001425 return check_generic_ptr_alignment(env, reg, pointer_desc, off, size,
1426 strict);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001427}
1428
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001429static int update_stack_depth(struct bpf_verifier_env *env,
1430 const struct bpf_func_state *func,
1431 int off)
1432{
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001433 u16 stack = env->subprog_stack_depth[func->subprogno];
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001434
1435 if (stack >= -off)
1436 return 0;
1437
1438 /* update known max for given subprogram */
1439 env->subprog_stack_depth[func->subprogno] = -off;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001440 return 0;
1441}
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001442
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001443/* starting from main bpf function walk all instructions of the function
1444 * and recursively walk all callees that given function can call.
1445 * Ignore jump and exit insns.
1446 * Since recursion is prevented by check_cfg() this algorithm
1447 * only needs a local stack of MAX_CALL_FRAMES to remember callsites
1448 */
1449static int check_max_stack_depth(struct bpf_verifier_env *env)
1450{
1451 int depth = 0, frame = 0, subprog = 0, i = 0, subprog_end;
1452 struct bpf_insn *insn = env->prog->insnsi;
1453 int insn_cnt = env->prog->len;
1454 int ret_insn[MAX_CALL_FRAMES];
1455 int ret_prog[MAX_CALL_FRAMES];
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001456
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001457process_func:
1458 /* round up to 32-bytes, since this is granularity
1459 * of interpreter stack size
1460 */
1461 depth += round_up(max_t(u32, env->subprog_stack_depth[subprog], 1), 32);
1462 if (depth > MAX_BPF_STACK) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001463 verbose(env, "combined stack size of %d calls is %d. Too large\n",
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001464 frame + 1, depth);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001465 return -EACCES;
1466 }
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001467continue_func:
1468 if (env->subprog_cnt == subprog)
1469 subprog_end = insn_cnt;
1470 else
1471 subprog_end = env->subprog_starts[subprog];
1472 for (; i < subprog_end; i++) {
1473 if (insn[i].code != (BPF_JMP | BPF_CALL))
1474 continue;
1475 if (insn[i].src_reg != BPF_PSEUDO_CALL)
1476 continue;
1477 /* remember insn and function to return to */
1478 ret_insn[frame] = i + 1;
1479 ret_prog[frame] = subprog;
1480
1481 /* find the callee */
1482 i = i + insn[i].imm + 1;
1483 subprog = find_subprog(env, i);
1484 if (subprog < 0) {
1485 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
1486 i);
1487 return -EFAULT;
1488 }
1489 subprog++;
1490 frame++;
1491 if (frame >= MAX_CALL_FRAMES) {
1492 WARN_ONCE(1, "verifier bug. Call stack is too deep\n");
1493 return -EFAULT;
1494 }
1495 goto process_func;
1496 }
1497 /* end of for() loop means the last insn of the 'subprog'
1498 * was reached. Doesn't matter whether it was JA or EXIT
1499 */
1500 if (frame == 0)
1501 return 0;
1502 depth -= round_up(max_t(u32, env->subprog_stack_depth[subprog], 1), 32);
1503 frame--;
1504 i = ret_insn[frame];
1505 subprog = ret_prog[frame];
1506 goto continue_func;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001507}
1508
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001509static int get_callee_stack_depth(struct bpf_verifier_env *env,
1510 const struct bpf_insn *insn, int idx)
1511{
1512 int start = idx + insn->imm + 1, subprog;
1513
1514 subprog = find_subprog(env, start);
1515 if (subprog < 0) {
1516 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
1517 start);
1518 return -EFAULT;
1519 }
1520 subprog++;
1521 return env->subprog_stack_depth[subprog];
1522}
1523
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001524/* check whether memory at (regno + off) is accessible for t = (read | write)
1525 * if t==write, value_regno is a register which value is stored into memory
1526 * if t==read, value_regno is a register which will receive the value from memory
1527 * if t==write && value_regno==-1, some unknown value is stored into memory
1528 * if t==read && value_regno==-1, don't care what we read from memory
1529 */
Yonghong Song31fd8582017-06-13 15:52:13 -07001530static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, int off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001531 int bpf_size, enum bpf_access_type t,
1532 int value_regno)
1533{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001534 struct bpf_reg_state *regs = cur_regs(env);
1535 struct bpf_reg_state *reg = regs + regno;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001536 struct bpf_func_state *state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001537 int size, err = 0;
1538
1539 size = bpf_size_to_bytes(bpf_size);
1540 if (size < 0)
1541 return size;
1542
Edward Creef1174f72017-08-07 15:26:19 +01001543 /* alignment checks will add in reg->off themselves */
David S. Millere07b98d2017-05-10 11:38:07 -07001544 err = check_ptr_alignment(env, reg, off, size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001545 if (err)
1546 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001547
Edward Creef1174f72017-08-07 15:26:19 +01001548 /* for access checks, reg->off is just part of off */
1549 off += reg->off;
1550
1551 if (reg->type == PTR_TO_MAP_VALUE) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001552 if (t == BPF_WRITE && value_regno >= 0 &&
1553 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001554 verbose(env, "R%d leaks addr into map\n", value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001555 return -EACCES;
1556 }
Josef Bacik48461132016-09-28 10:54:32 -04001557
Yonghong Song9fd29c02017-11-12 14:49:09 -08001558 err = check_map_access(env, regno, off, size, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001559 if (!err && t == BPF_READ && value_regno >= 0)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001560 mark_reg_unknown(env, regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001561
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07001562 } else if (reg->type == PTR_TO_CTX) {
Edward Creef1174f72017-08-07 15:26:19 +01001563 enum bpf_reg_type reg_type = SCALAR_VALUE;
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07001564
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001565 if (t == BPF_WRITE && value_regno >= 0 &&
1566 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001567 verbose(env, "R%d leaks addr into ctx\n", value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001568 return -EACCES;
1569 }
Edward Creef1174f72017-08-07 15:26:19 +01001570 /* ctx accesses must be at a fixed offset, so that we can
1571 * determine what type of data were returned.
1572 */
Jakub Kicinski28e33f92017-10-16 11:16:55 -07001573 if (reg->off) {
David S. Millerf8ddadc2017-10-22 13:36:53 +01001574 verbose(env,
1575 "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 -07001576 regno, reg->off, off - reg->off);
1577 return -EACCES;
1578 }
1579 if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
Edward Creef1174f72017-08-07 15:26:19 +01001580 char tn_buf[48];
1581
1582 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001583 verbose(env,
1584 "variable ctx access var_off=%s off=%d size=%d",
Edward Creef1174f72017-08-07 15:26:19 +01001585 tn_buf, off, size);
1586 return -EACCES;
1587 }
Yonghong Song31fd8582017-06-13 15:52:13 -07001588 err = check_ctx_access(env, insn_idx, off, size, t, &reg_type);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001589 if (!err && t == BPF_READ && value_regno >= 0) {
Edward Creef1174f72017-08-07 15:26:19 +01001590 /* ctx access returns either a scalar, or a
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001591 * PTR_TO_PACKET[_META,_END]. In the latter
1592 * case, we know the offset is zero.
Edward Creef1174f72017-08-07 15:26:19 +01001593 */
1594 if (reg_type == SCALAR_VALUE)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001595 mark_reg_unknown(env, regs, value_regno);
Edward Creef1174f72017-08-07 15:26:19 +01001596 else
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001597 mark_reg_known_zero(env, regs,
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001598 value_regno);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001599 regs[value_regno].id = 0;
1600 regs[value_regno].off = 0;
1601 regs[value_regno].range = 0;
1602 regs[value_regno].type = reg_type;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001603 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001604
Edward Creef1174f72017-08-07 15:26:19 +01001605 } else if (reg->type == PTR_TO_STACK) {
1606 /* stack accesses must be at a fixed offset, so that we can
1607 * determine what type of data were returned.
1608 * See check_stack_read().
1609 */
1610 if (!tnum_is_const(reg->var_off)) {
1611 char tn_buf[48];
1612
1613 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001614 verbose(env, "variable stack access var_off=%s off=%d size=%d",
Edward Creef1174f72017-08-07 15:26:19 +01001615 tn_buf, off, size);
1616 return -EACCES;
1617 }
1618 off += reg->var_off.value;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001619 if (off >= 0 || off < -MAX_BPF_STACK) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001620 verbose(env, "invalid stack off=%d size=%d\n", off,
1621 size);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001622 return -EACCES;
1623 }
Alexei Starovoitov87266792017-05-30 13:31:29 -07001624
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001625 state = func(env, reg);
1626 err = update_stack_depth(env, state, off);
1627 if (err)
1628 return err;
Alexei Starovoitov87266792017-05-30 13:31:29 -07001629
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001630 if (t == BPF_WRITE)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001631 err = check_stack_write(env, state, off, size,
1632 value_regno);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001633 else
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001634 err = check_stack_read(env, state, off, size,
1635 value_regno);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001636 } else if (reg_is_pkt_pointer(reg)) {
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001637 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001638 verbose(env, "cannot write into packet\n");
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001639 return -EACCES;
1640 }
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001641 if (t == BPF_WRITE && value_regno >= 0 &&
1642 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001643 verbose(env, "R%d leaks addr into packet\n",
1644 value_regno);
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001645 return -EACCES;
1646 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001647 err = check_packet_access(env, regno, off, size, false);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001648 if (!err && t == BPF_READ && value_regno >= 0)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001649 mark_reg_unknown(env, regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001650 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001651 verbose(env, "R%d invalid mem access '%s'\n", regno,
1652 reg_type_str[reg->type]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001653 return -EACCES;
1654 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001655
Edward Creef1174f72017-08-07 15:26:19 +01001656 if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ &&
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001657 regs[value_regno].type == SCALAR_VALUE) {
Edward Creef1174f72017-08-07 15:26:19 +01001658 /* b/h/w load zero-extends, mark upper bits as known 0 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001659 regs[value_regno].var_off =
1660 tnum_cast(regs[value_regno].var_off, size);
1661 __update_reg_bounds(&regs[value_regno]);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001662 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001663 return err;
1664}
1665
Yonghong Song31fd8582017-06-13 15:52:13 -07001666static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001667{
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001668 int err;
1669
1670 if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
1671 insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001672 verbose(env, "BPF_XADD uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001673 return -EINVAL;
1674 }
1675
1676 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01001677 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001678 if (err)
1679 return err;
1680
1681 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01001682 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001683 if (err)
1684 return err;
1685
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +02001686 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001687 verbose(env, "R%d leaks addr into mem\n", insn->src_reg);
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +02001688 return -EACCES;
1689 }
1690
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001691 /* check whether atomic_add can read the memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07001692 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001693 BPF_SIZE(insn->code), BPF_READ, -1);
1694 if (err)
1695 return err;
1696
1697 /* check whether atomic_add can write into the same memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07001698 return check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001699 BPF_SIZE(insn->code), BPF_WRITE, -1);
1700}
1701
1702/* when register 'regno' is passed into function that will read 'access_size'
1703 * bytes from that pointer, make sure that it's within stack boundary
Edward Creef1174f72017-08-07 15:26:19 +01001704 * and all elements of stack are initialized.
1705 * Unlike most pointer bounds-checking functions, this one doesn't take an
1706 * 'off' argument, so it has to add in reg->off itself.
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001707 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001708static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
Daniel Borkmann435faee12016-04-13 00:10:51 +02001709 int access_size, bool zero_size_allowed,
1710 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001711{
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001712 struct bpf_reg_state *reg = cur_regs(env) + regno;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001713 struct bpf_func_state *state = func(env, reg);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001714 int off, i, slot, spi;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001715
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001716 if (reg->type != PTR_TO_STACK) {
Edward Creef1174f72017-08-07 15:26:19 +01001717 /* Allow zero-byte read from NULL, regardless of pointer type */
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001718 if (zero_size_allowed && access_size == 0 &&
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001719 register_is_null(reg))
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001720 return 0;
1721
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001722 verbose(env, "R%d type=%s expected=%s\n", regno,
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001723 reg_type_str[reg->type],
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001724 reg_type_str[PTR_TO_STACK]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001725 return -EACCES;
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001726 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001727
Edward Creef1174f72017-08-07 15:26:19 +01001728 /* Only allow fixed-offset stack reads */
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001729 if (!tnum_is_const(reg->var_off)) {
Edward Creef1174f72017-08-07 15:26:19 +01001730 char tn_buf[48];
1731
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001732 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001733 verbose(env, "invalid variable stack read R%d var_off=%s\n",
Edward Creef1174f72017-08-07 15:26:19 +01001734 regno, tn_buf);
1735 }
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001736 off = reg->off + reg->var_off.value;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001737 if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
Yonghong Song9fd29c02017-11-12 14:49:09 -08001738 access_size < 0 || (access_size == 0 && !zero_size_allowed)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001739 verbose(env, "invalid stack type R%d off=%d access_size=%d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001740 regno, off, access_size);
1741 return -EACCES;
1742 }
1743
Daniel Borkmann435faee12016-04-13 00:10:51 +02001744 if (meta && meta->raw_mode) {
1745 meta->access_size = access_size;
1746 meta->regno = regno;
1747 return 0;
1748 }
1749
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001750 for (i = 0; i < access_size; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001751 u8 *stype;
1752
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001753 slot = -(off + i) - 1;
1754 spi = slot / BPF_REG_SIZE;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001755 if (state->allocated_stack <= slot)
1756 goto err;
1757 stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE];
1758 if (*stype == STACK_MISC)
1759 goto mark;
1760 if (*stype == STACK_ZERO) {
1761 /* helper can write anything into the stack */
1762 *stype = STACK_MISC;
1763 goto mark;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001764 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001765err:
1766 verbose(env, "invalid indirect read from stack off %d+%d size %d\n",
1767 off, i, access_size);
1768 return -EACCES;
1769mark:
1770 /* reading any byte out of 8-byte 'spill_slot' will cause
1771 * the whole slot to be marked as 'read'
1772 */
1773 mark_stack_slot_read(env, env->cur_state, env->cur_state->parent,
1774 spi, state->frameno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001775 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001776 return update_stack_depth(env, state, off);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001777}
1778
Gianluca Borello06c1c042017-01-09 10:19:49 -08001779static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
1780 int access_size, bool zero_size_allowed,
1781 struct bpf_call_arg_meta *meta)
1782{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001783 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
Gianluca Borello06c1c042017-01-09 10:19:49 -08001784
Edward Creef1174f72017-08-07 15:26:19 +01001785 switch (reg->type) {
Gianluca Borello06c1c042017-01-09 10:19:49 -08001786 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001787 case PTR_TO_PACKET_META:
Yonghong Song9fd29c02017-11-12 14:49:09 -08001788 return check_packet_access(env, regno, reg->off, access_size,
1789 zero_size_allowed);
Gianluca Borello06c1c042017-01-09 10:19:49 -08001790 case PTR_TO_MAP_VALUE:
Yonghong Song9fd29c02017-11-12 14:49:09 -08001791 return check_map_access(env, regno, reg->off, access_size,
1792 zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01001793 default: /* scalar_value|ptr_to_stack or invalid ptr */
Gianluca Borello06c1c042017-01-09 10:19:49 -08001794 return check_stack_boundary(env, regno, access_size,
1795 zero_size_allowed, meta);
1796 }
1797}
1798
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001799static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001800 enum bpf_arg_type arg_type,
1801 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001802{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001803 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001804 enum bpf_reg_type expected_type, type = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001805 int err = 0;
1806
Daniel Borkmann80f1d682015-03-12 17:21:42 +01001807 if (arg_type == ARG_DONTCARE)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001808 return 0;
1809
Edward Creedc503a82017-08-15 20:34:35 +01001810 err = check_reg_arg(env, regno, SRC_OP);
1811 if (err)
1812 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001813
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001814 if (arg_type == ARG_ANYTHING) {
1815 if (is_pointer_value(env, regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001816 verbose(env, "R%d leaks addr into helper function\n",
1817 regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001818 return -EACCES;
1819 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01001820 return 0;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001821 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01001822
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001823 if (type_is_pkt_pointer(type) &&
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001824 !may_access_direct_pkt_data(env, meta, BPF_READ)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001825 verbose(env, "helper access to the packet is not allowed\n");
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001826 return -EACCES;
1827 }
1828
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001829 if (arg_type == ARG_PTR_TO_MAP_KEY ||
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001830 arg_type == ARG_PTR_TO_MAP_VALUE) {
1831 expected_type = PTR_TO_STACK;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001832 if (!type_is_pkt_pointer(type) &&
1833 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001834 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001835 } else if (arg_type == ARG_CONST_SIZE ||
1836 arg_type == ARG_CONST_SIZE_OR_ZERO) {
Edward Creef1174f72017-08-07 15:26:19 +01001837 expected_type = SCALAR_VALUE;
1838 if (type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001839 goto err_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001840 } else if (arg_type == ARG_CONST_MAP_PTR) {
1841 expected_type = CONST_PTR_TO_MAP;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001842 if (type != expected_type)
1843 goto err_type;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001844 } else if (arg_type == ARG_PTR_TO_CTX) {
1845 expected_type = PTR_TO_CTX;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001846 if (type != expected_type)
1847 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001848 } else if (arg_type == ARG_PTR_TO_MEM ||
Gianluca Borellodb1ac492017-11-22 18:32:53 +00001849 arg_type == ARG_PTR_TO_MEM_OR_NULL ||
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001850 arg_type == ARG_PTR_TO_UNINIT_MEM) {
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001851 expected_type = PTR_TO_STACK;
1852 /* One exception here. In case function allows for NULL to be
Edward Creef1174f72017-08-07 15:26:19 +01001853 * passed in as argument, it's a SCALAR_VALUE type. Final test
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001854 * happens during stack boundary checking.
1855 */
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001856 if (register_is_null(reg) &&
Gianluca Borellodb1ac492017-11-22 18:32:53 +00001857 arg_type == ARG_PTR_TO_MEM_OR_NULL)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001858 /* final test in check_stack_boundary() */;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001859 else if (!type_is_pkt_pointer(type) &&
1860 type != PTR_TO_MAP_VALUE &&
Edward Creef1174f72017-08-07 15:26:19 +01001861 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001862 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001863 meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001864 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001865 verbose(env, "unsupported arg_type %d\n", arg_type);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001866 return -EFAULT;
1867 }
1868
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001869 if (arg_type == ARG_CONST_MAP_PTR) {
1870 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001871 meta->map_ptr = reg->map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001872 } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
1873 /* bpf_map_xxx(..., map_ptr, ..., key) call:
1874 * check that [key, key + map->key_size) are within
1875 * stack limits and initialized
1876 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001877 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001878 /* in function declaration map_ptr must come before
1879 * map_key, so that it's verified and known before
1880 * we have to check map_key here. Otherwise it means
1881 * that kernel subsystem misconfigured verifier
1882 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001883 verbose(env, "invalid map_ptr to access map->key\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001884 return -EACCES;
1885 }
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001886 if (type_is_pkt_pointer(type))
Edward Creef1174f72017-08-07 15:26:19 +01001887 err = check_packet_access(env, regno, reg->off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001888 meta->map_ptr->key_size,
1889 false);
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001890 else
1891 err = check_stack_boundary(env, regno,
1892 meta->map_ptr->key_size,
1893 false, NULL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001894 } else if (arg_type == ARG_PTR_TO_MAP_VALUE) {
1895 /* bpf_map_xxx(..., map_ptr, ..., value) call:
1896 * check [value, value + map->value_size) validity
1897 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001898 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001899 /* kernel subsystem misconfigured verifier */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001900 verbose(env, "invalid map_ptr to access map->value\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001901 return -EACCES;
1902 }
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001903 if (type_is_pkt_pointer(type))
Edward Creef1174f72017-08-07 15:26:19 +01001904 err = check_packet_access(env, regno, reg->off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001905 meta->map_ptr->value_size,
1906 false);
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001907 else
1908 err = check_stack_boundary(env, regno,
1909 meta->map_ptr->value_size,
1910 false, NULL);
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001911 } else if (arg_type == ARG_CONST_SIZE ||
1912 arg_type == ARG_CONST_SIZE_OR_ZERO) {
1913 bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001914
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001915 /* bpf_xxx(..., buf, len) call will access 'len' bytes
1916 * from stack pointer 'buf'. Check it
1917 * note: regno == len, regno - 1 == buf
1918 */
1919 if (regno == 0) {
1920 /* kernel subsystem misconfigured verifier */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001921 verbose(env,
1922 "ARG_CONST_SIZE cannot be first argument\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001923 return -EACCES;
1924 }
Gianluca Borello06c1c042017-01-09 10:19:49 -08001925
Edward Creef1174f72017-08-07 15:26:19 +01001926 /* The register is SCALAR_VALUE; the access check
1927 * happens using its boundaries.
Gianluca Borello06c1c042017-01-09 10:19:49 -08001928 */
Edward Creef1174f72017-08-07 15:26:19 +01001929
1930 if (!tnum_is_const(reg->var_off))
Gianluca Borello06c1c042017-01-09 10:19:49 -08001931 /* For unprivileged variable accesses, disable raw
1932 * mode so that the program is required to
1933 * initialize all the memory that the helper could
1934 * just partially fill up.
1935 */
1936 meta = NULL;
1937
Edward Creeb03c9f92017-08-07 15:26:36 +01001938 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001939 verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n",
Edward Creef1174f72017-08-07 15:26:19 +01001940 regno);
1941 return -EACCES;
1942 }
Gianluca Borello06c1c042017-01-09 10:19:49 -08001943
Edward Creeb03c9f92017-08-07 15:26:36 +01001944 if (reg->umin_value == 0) {
Edward Creef1174f72017-08-07 15:26:19 +01001945 err = check_helper_mem_access(env, regno - 1, 0,
1946 zero_size_allowed,
1947 meta);
Gianluca Borello06c1c042017-01-09 10:19:49 -08001948 if (err)
1949 return err;
Gianluca Borello06c1c042017-01-09 10:19:49 -08001950 }
Edward Creef1174f72017-08-07 15:26:19 +01001951
Edward Creeb03c9f92017-08-07 15:26:36 +01001952 if (reg->umax_value >= BPF_MAX_VAR_SIZ) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001953 verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
Edward Creef1174f72017-08-07 15:26:19 +01001954 regno);
1955 return -EACCES;
1956 }
1957 err = check_helper_mem_access(env, regno - 1,
Edward Creeb03c9f92017-08-07 15:26:36 +01001958 reg->umax_value,
Edward Creef1174f72017-08-07 15:26:19 +01001959 zero_size_allowed, meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001960 }
1961
1962 return err;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001963err_type:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001964 verbose(env, "R%d type=%s expected=%s\n", regno,
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001965 reg_type_str[type], reg_type_str[expected_type]);
1966 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001967}
1968
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001969static int check_map_func_compatibility(struct bpf_verifier_env *env,
1970 struct bpf_map *map, int func_id)
Kaixu Xia35578d72015-08-06 07:02:35 +00001971{
Kaixu Xia35578d72015-08-06 07:02:35 +00001972 if (!map)
1973 return 0;
1974
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001975 /* We need a two way check, first is from map perspective ... */
1976 switch (map->map_type) {
1977 case BPF_MAP_TYPE_PROG_ARRAY:
1978 if (func_id != BPF_FUNC_tail_call)
1979 goto error;
1980 break;
1981 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
1982 if (func_id != BPF_FUNC_perf_event_read &&
Yonghong Song908432c2017-10-05 09:19:20 -07001983 func_id != BPF_FUNC_perf_event_output &&
1984 func_id != BPF_FUNC_perf_event_read_value)
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001985 goto error;
1986 break;
1987 case BPF_MAP_TYPE_STACK_TRACE:
1988 if (func_id != BPF_FUNC_get_stackid)
1989 goto error;
1990 break;
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -07001991 case BPF_MAP_TYPE_CGROUP_ARRAY:
David S. Miller60747ef2016-08-18 01:17:32 -04001992 if (func_id != BPF_FUNC_skb_under_cgroup &&
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001993 func_id != BPF_FUNC_current_task_under_cgroup)
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07001994 goto error;
1995 break;
John Fastabend546ac1f2017-07-17 09:28:56 -07001996 /* devmap returns a pointer to a live net_device ifindex that we cannot
1997 * allow to be modified from bpf side. So do not allow lookup elements
1998 * for now.
1999 */
2000 case BPF_MAP_TYPE_DEVMAP:
John Fastabend2ddf71e2017-07-17 09:30:02 -07002001 if (func_id != BPF_FUNC_redirect_map)
John Fastabend546ac1f2017-07-17 09:28:56 -07002002 goto error;
2003 break;
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02002004 /* Restrict bpf side of cpumap, open when use-cases appear */
2005 case BPF_MAP_TYPE_CPUMAP:
2006 if (func_id != BPF_FUNC_redirect_map)
2007 goto error;
2008 break;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07002009 case BPF_MAP_TYPE_ARRAY_OF_MAPS:
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07002010 case BPF_MAP_TYPE_HASH_OF_MAPS:
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07002011 if (func_id != BPF_FUNC_map_lookup_elem)
2012 goto error;
Martin KaFai Lau16a43622017-08-17 18:14:43 -07002013 break;
John Fastabend174a79f2017-08-15 22:32:47 -07002014 case BPF_MAP_TYPE_SOCKMAP:
2015 if (func_id != BPF_FUNC_sk_redirect_map &&
2016 func_id != BPF_FUNC_sock_map_update &&
2017 func_id != BPF_FUNC_map_delete_elem)
2018 goto error;
2019 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002020 default:
2021 break;
2022 }
2023
2024 /* ... and second from the function itself. */
2025 switch (func_id) {
2026 case BPF_FUNC_tail_call:
2027 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
2028 goto error;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002029 if (env->subprog_cnt) {
2030 verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n");
2031 return -EINVAL;
2032 }
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002033 break;
2034 case BPF_FUNC_perf_event_read:
2035 case BPF_FUNC_perf_event_output:
Yonghong Song908432c2017-10-05 09:19:20 -07002036 case BPF_FUNC_perf_event_read_value:
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002037 if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
2038 goto error;
2039 break;
2040 case BPF_FUNC_get_stackid:
2041 if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
2042 goto error;
2043 break;
Sargun Dhillon60d20f92016-08-12 08:56:52 -07002044 case BPF_FUNC_current_task_under_cgroup:
Daniel Borkmann747ea552016-08-12 22:17:17 +02002045 case BPF_FUNC_skb_under_cgroup:
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002046 if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
2047 goto error;
2048 break;
John Fastabend97f91a72017-07-17 09:29:18 -07002049 case BPF_FUNC_redirect_map:
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02002050 if (map->map_type != BPF_MAP_TYPE_DEVMAP &&
2051 map->map_type != BPF_MAP_TYPE_CPUMAP)
John Fastabend97f91a72017-07-17 09:29:18 -07002052 goto error;
2053 break;
John Fastabend174a79f2017-08-15 22:32:47 -07002054 case BPF_FUNC_sk_redirect_map:
2055 if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
2056 goto error;
2057 break;
2058 case BPF_FUNC_sock_map_update:
2059 if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
2060 goto error;
2061 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002062 default:
2063 break;
Kaixu Xia35578d72015-08-06 07:02:35 +00002064 }
2065
2066 return 0;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002067error:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002068 verbose(env, "cannot pass map_type %d into func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02002069 map->map_type, func_id_name(func_id), func_id);
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002070 return -EINVAL;
Kaixu Xia35578d72015-08-06 07:02:35 +00002071}
2072
Daniel Borkmann435faee12016-04-13 00:10:51 +02002073static int check_raw_mode(const struct bpf_func_proto *fn)
2074{
2075 int count = 0;
2076
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002077 if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002078 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002079 if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002080 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002081 if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002082 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002083 if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002084 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002085 if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002086 count++;
2087
2088 return count > 1 ? -EINVAL : 0;
2089}
2090
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002091/* Packet data might have moved, any old PTR_TO_PACKET[_META,_END]
2092 * are now invalid, so turn them into unknown SCALAR_VALUE.
Edward Creef1174f72017-08-07 15:26:19 +01002093 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002094static void __clear_all_pkt_pointers(struct bpf_verifier_env *env,
2095 struct bpf_func_state *state)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002096{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002097 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002098 int i;
2099
2100 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002101 if (reg_is_pkt_pointer_any(&regs[i]))
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002102 mark_reg_unknown(env, regs, i);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002103
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002104 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
2105 if (state->stack[i].slot_type[0] != STACK_SPILL)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002106 continue;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002107 reg = &state->stack[i].spilled_ptr;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002108 if (reg_is_pkt_pointer_any(reg))
2109 __mark_reg_unknown(reg);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002110 }
2111}
2112
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002113static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
2114{
2115 struct bpf_verifier_state *vstate = env->cur_state;
2116 int i;
2117
2118 for (i = 0; i <= vstate->curframe; i++)
2119 __clear_all_pkt_pointers(env, vstate->frame[i]);
2120}
2121
2122static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
2123 int *insn_idx)
2124{
2125 struct bpf_verifier_state *state = env->cur_state;
2126 struct bpf_func_state *caller, *callee;
2127 int i, subprog, target_insn;
2128
2129 if (state->curframe >= MAX_CALL_FRAMES) {
2130 verbose(env, "the call stack of %d frames is too deep\n",
2131 state->curframe);
2132 return -E2BIG;
2133 }
2134
2135 target_insn = *insn_idx + insn->imm;
2136 subprog = find_subprog(env, target_insn + 1);
2137 if (subprog < 0) {
2138 verbose(env, "verifier bug. No program starts at insn %d\n",
2139 target_insn + 1);
2140 return -EFAULT;
2141 }
2142
2143 caller = state->frame[state->curframe];
2144 if (state->frame[state->curframe + 1]) {
2145 verbose(env, "verifier bug. Frame %d already allocated\n",
2146 state->curframe + 1);
2147 return -EFAULT;
2148 }
2149
2150 callee = kzalloc(sizeof(*callee), GFP_KERNEL);
2151 if (!callee)
2152 return -ENOMEM;
2153 state->frame[state->curframe + 1] = callee;
2154
2155 /* callee cannot access r0, r6 - r9 for reading and has to write
2156 * into its own stack before reading from it.
2157 * callee can read/write into caller's stack
2158 */
2159 init_func_state(env, callee,
2160 /* remember the callsite, it will be used by bpf_exit */
2161 *insn_idx /* callsite */,
2162 state->curframe + 1 /* frameno within this callchain */,
2163 subprog + 1 /* subprog number within this prog */);
2164
2165 /* copy r1 - r5 args that callee can access */
2166 for (i = BPF_REG_1; i <= BPF_REG_5; i++)
2167 callee->regs[i] = caller->regs[i];
2168
2169 /* after the call regsiters r0 - r5 were scratched */
2170 for (i = 0; i < CALLER_SAVED_REGS; i++) {
2171 mark_reg_not_init(env, caller->regs, caller_saved[i]);
2172 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
2173 }
2174
2175 /* only increment it after check_reg_arg() finished */
2176 state->curframe++;
2177
2178 /* and go analyze first insn of the callee */
2179 *insn_idx = target_insn;
2180
2181 if (env->log.level) {
2182 verbose(env, "caller:\n");
2183 print_verifier_state(env, caller);
2184 verbose(env, "callee:\n");
2185 print_verifier_state(env, callee);
2186 }
2187 return 0;
2188}
2189
2190static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
2191{
2192 struct bpf_verifier_state *state = env->cur_state;
2193 struct bpf_func_state *caller, *callee;
2194 struct bpf_reg_state *r0;
2195
2196 callee = state->frame[state->curframe];
2197 r0 = &callee->regs[BPF_REG_0];
2198 if (r0->type == PTR_TO_STACK) {
2199 /* technically it's ok to return caller's stack pointer
2200 * (or caller's caller's pointer) back to the caller,
2201 * since these pointers are valid. Only current stack
2202 * pointer will be invalid as soon as function exits,
2203 * but let's be conservative
2204 */
2205 verbose(env, "cannot return stack pointer to the caller\n");
2206 return -EINVAL;
2207 }
2208
2209 state->curframe--;
2210 caller = state->frame[state->curframe];
2211 /* return to the caller whatever r0 had in the callee */
2212 caller->regs[BPF_REG_0] = *r0;
2213
2214 *insn_idx = callee->callsite + 1;
2215 if (env->log.level) {
2216 verbose(env, "returning from callee:\n");
2217 print_verifier_state(env, callee);
2218 verbose(env, "to caller at %d:\n", *insn_idx);
2219 print_verifier_state(env, caller);
2220 }
2221 /* clear everything in the callee */
2222 free_func_state(callee);
2223 state->frame[state->curframe + 1] = NULL;
2224 return 0;
2225}
2226
2227static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002228{
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002229 const struct bpf_func_proto *fn = NULL;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002230 struct bpf_reg_state *regs;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002231 struct bpf_call_arg_meta meta;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002232 bool changes_data;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002233 int i, err;
2234
2235 /* find function prototype */
2236 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002237 verbose(env, "invalid func %s#%d\n", func_id_name(func_id),
2238 func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002239 return -EINVAL;
2240 }
2241
Jakub Kicinski00176a32017-10-16 16:40:54 -07002242 if (env->ops->get_func_proto)
2243 fn = env->ops->get_func_proto(func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002244
2245 if (!fn) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002246 verbose(env, "unknown func %s#%d\n", func_id_name(func_id),
2247 func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002248 return -EINVAL;
2249 }
2250
2251 /* eBPF programs must be GPL compatible to use GPL-ed functions */
Daniel Borkmann24701ec2015-03-01 12:31:47 +01002252 if (!env->prog->gpl_compatible && fn->gpl_only) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002253 verbose(env, "cannot call GPL only function from proprietary program\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002254 return -EINVAL;
2255 }
2256
Martin KaFai Lau17bedab2016-12-07 15:53:11 -08002257 changes_data = bpf_helper_changes_pkt_data(fn->func);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002258
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002259 memset(&meta, 0, sizeof(meta));
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002260 meta.pkt_access = fn->pkt_access;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002261
Daniel Borkmann435faee12016-04-13 00:10:51 +02002262 /* We only support one arg being in raw mode at the moment, which
2263 * is sufficient for the helper functions we have right now.
2264 */
2265 err = check_raw_mode(fn);
2266 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002267 verbose(env, "kernel subsystem misconfigured func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02002268 func_id_name(func_id), func_id);
Daniel Borkmann435faee12016-04-13 00:10:51 +02002269 return err;
2270 }
2271
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002272 /* check args */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002273 err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002274 if (err)
2275 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002276 err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002277 if (err)
2278 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002279 err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002280 if (err)
2281 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002282 err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002283 if (err)
2284 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002285 err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002286 if (err)
2287 return err;
2288
Daniel Borkmann435faee12016-04-13 00:10:51 +02002289 /* Mark slots with STACK_MISC in case of raw mode, stack offset
2290 * is inferred from register state.
2291 */
2292 for (i = 0; i < meta.access_size; i++) {
Yonghong Song31fd8582017-06-13 15:52:13 -07002293 err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, BPF_WRITE, -1);
Daniel Borkmann435faee12016-04-13 00:10:51 +02002294 if (err)
2295 return err;
2296 }
2297
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002298 regs = cur_regs(env);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002299 /* reset caller saved regs */
Edward Creedc503a82017-08-15 20:34:35 +01002300 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002301 mark_reg_not_init(env, regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01002302 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
2303 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002304
Edward Creedc503a82017-08-15 20:34:35 +01002305 /* update return register (already marked as written above) */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002306 if (fn->ret_type == RET_INTEGER) {
Edward Creef1174f72017-08-07 15:26:19 +01002307 /* sets type to SCALAR_VALUE */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002308 mark_reg_unknown(env, regs, BPF_REG_0);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002309 } else if (fn->ret_type == RET_VOID) {
2310 regs[BPF_REG_0].type = NOT_INIT;
2311 } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) {
Martin KaFai Laufad73a12017-03-22 10:00:32 -07002312 struct bpf_insn_aux_data *insn_aux;
2313
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002314 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
Edward Creef1174f72017-08-07 15:26:19 +01002315 /* There is no offset yet applied, variable or fixed */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002316 mark_reg_known_zero(env, regs, BPF_REG_0);
Edward Creef1174f72017-08-07 15:26:19 +01002317 regs[BPF_REG_0].off = 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002318 /* remember map_ptr, so that check_map_access()
2319 * can check 'value_size' boundary of memory access
2320 * to map element returned from bpf_map_lookup_elem()
2321 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002322 if (meta.map_ptr == NULL) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002323 verbose(env,
2324 "kernel subsystem misconfigured verifier\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002325 return -EINVAL;
2326 }
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002327 regs[BPF_REG_0].map_ptr = meta.map_ptr;
Thomas Graf57a09bf2016-10-18 19:51:19 +02002328 regs[BPF_REG_0].id = ++env->id_gen;
Martin KaFai Laufad73a12017-03-22 10:00:32 -07002329 insn_aux = &env->insn_aux_data[insn_idx];
2330 if (!insn_aux->map_ptr)
2331 insn_aux->map_ptr = meta.map_ptr;
2332 else if (insn_aux->map_ptr != meta.map_ptr)
2333 insn_aux->map_ptr = BPF_MAP_PTR_POISON;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002334 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002335 verbose(env, "unknown return type %d of func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02002336 fn->ret_type, func_id_name(func_id), func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002337 return -EINVAL;
2338 }
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07002339
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002340 err = check_map_func_compatibility(env, meta.map_ptr, func_id);
Kaixu Xia35578d72015-08-06 07:02:35 +00002341 if (err)
2342 return err;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07002343
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002344 if (changes_data)
2345 clear_all_pkt_pointers(env);
2346 return 0;
2347}
2348
Edward Creef1174f72017-08-07 15:26:19 +01002349static void coerce_reg_to_32(struct bpf_reg_state *reg)
David S. Millerd1174412017-05-10 11:22:52 -07002350{
Edward Creef1174f72017-08-07 15:26:19 +01002351 /* clear high 32 bits */
2352 reg->var_off = tnum_cast(reg->var_off, 4);
Edward Creeb03c9f92017-08-07 15:26:36 +01002353 /* Update bounds */
2354 __update_reg_bounds(reg);
2355}
2356
2357static bool signed_add_overflows(s64 a, s64 b)
2358{
2359 /* Do the add in u64, where overflow is well-defined */
2360 s64 res = (s64)((u64)a + (u64)b);
2361
2362 if (b < 0)
2363 return res > a;
2364 return res < a;
2365}
2366
2367static bool signed_sub_overflows(s64 a, s64 b)
2368{
2369 /* Do the sub in u64, where overflow is well-defined */
2370 s64 res = (s64)((u64)a - (u64)b);
2371
2372 if (b < 0)
2373 return res < a;
2374 return res > a;
David S. Millerd1174412017-05-10 11:22:52 -07002375}
2376
Edward Creef1174f72017-08-07 15:26:19 +01002377/* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off.
Edward Creef1174f72017-08-07 15:26:19 +01002378 * Caller should also handle BPF_MOV case separately.
2379 * If we return -EACCES, caller may want to try again treating pointer as a
2380 * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks.
2381 */
2382static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
2383 struct bpf_insn *insn,
2384 const struct bpf_reg_state *ptr_reg,
2385 const struct bpf_reg_state *off_reg)
Josef Bacik48461132016-09-28 10:54:32 -04002386{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002387 struct bpf_verifier_state *vstate = env->cur_state;
2388 struct bpf_func_state *state = vstate->frame[vstate->curframe];
2389 struct bpf_reg_state *regs = state->regs, *dst_reg;
Edward Creef1174f72017-08-07 15:26:19 +01002390 bool known = tnum_is_const(off_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01002391 s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value,
2392 smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
2393 u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value,
2394 umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value;
Josef Bacik48461132016-09-28 10:54:32 -04002395 u8 opcode = BPF_OP(insn->code);
Edward Creef1174f72017-08-07 15:26:19 +01002396 u32 dst = insn->dst_reg;
Josef Bacik48461132016-09-28 10:54:32 -04002397
Edward Creef1174f72017-08-07 15:26:19 +01002398 dst_reg = &regs[dst];
Josef Bacik48461132016-09-28 10:54:32 -04002399
Edward Creeb03c9f92017-08-07 15:26:36 +01002400 if (WARN_ON_ONCE(known && (smin_val != smax_val))) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002401 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002402 verbose(env,
2403 "verifier internal error: known but bad sbounds\n");
Edward Creeb03c9f92017-08-07 15:26:36 +01002404 return -EINVAL;
2405 }
2406 if (WARN_ON_ONCE(known && (umin_val != umax_val))) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002407 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002408 verbose(env,
2409 "verifier internal error: known but bad ubounds\n");
Edward Creef1174f72017-08-07 15:26:19 +01002410 return -EINVAL;
Josef Bacik48461132016-09-28 10:54:32 -04002411 }
2412
Edward Creef1174f72017-08-07 15:26:19 +01002413 if (BPF_CLASS(insn->code) != BPF_ALU64) {
2414 /* 32-bit ALU ops on pointers produce (meaningless) scalars */
2415 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002416 verbose(env,
2417 "R%d 32-bit pointer arithmetic prohibited\n",
Edward Creef1174f72017-08-07 15:26:19 +01002418 dst);
2419 return -EACCES;
2420 }
David S. Millerd1174412017-05-10 11:22:52 -07002421
Edward Creef1174f72017-08-07 15:26:19 +01002422 if (ptr_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
2423 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002424 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 +01002425 dst);
2426 return -EACCES;
2427 }
2428 if (ptr_reg->type == CONST_PTR_TO_MAP) {
2429 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002430 verbose(env, "R%d pointer arithmetic on CONST_PTR_TO_MAP prohibited\n",
Edward Creef1174f72017-08-07 15:26:19 +01002431 dst);
2432 return -EACCES;
2433 }
2434 if (ptr_reg->type == PTR_TO_PACKET_END) {
2435 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002436 verbose(env, "R%d pointer arithmetic on PTR_TO_PACKET_END prohibited\n",
Edward Creef1174f72017-08-07 15:26:19 +01002437 dst);
2438 return -EACCES;
2439 }
2440
2441 /* In case of 'scalar += pointer', dst_reg inherits pointer type and id.
2442 * The id may be overwritten later if we create a new variable offset.
Josef Bacik48461132016-09-28 10:54:32 -04002443 */
Edward Creef1174f72017-08-07 15:26:19 +01002444 dst_reg->type = ptr_reg->type;
2445 dst_reg->id = ptr_reg->id;
Josef Bacikf23cc642016-11-14 15:45:36 -05002446
Josef Bacik48461132016-09-28 10:54:32 -04002447 switch (opcode) {
2448 case BPF_ADD:
Edward Creef1174f72017-08-07 15:26:19 +01002449 /* We can take a fixed offset as long as it doesn't overflow
2450 * the s32 'off' field
2451 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002452 if (known && (ptr_reg->off + smin_val ==
2453 (s64)(s32)(ptr_reg->off + smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01002454 /* pointer += K. Accumulate it into fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01002455 dst_reg->smin_value = smin_ptr;
2456 dst_reg->smax_value = smax_ptr;
2457 dst_reg->umin_value = umin_ptr;
2458 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01002459 dst_reg->var_off = ptr_reg->var_off;
Edward Creeb03c9f92017-08-07 15:26:36 +01002460 dst_reg->off = ptr_reg->off + smin_val;
Edward Creef1174f72017-08-07 15:26:19 +01002461 dst_reg->range = ptr_reg->range;
2462 break;
2463 }
Edward Creef1174f72017-08-07 15:26:19 +01002464 /* A new variable offset is created. Note that off_reg->off
2465 * == 0, since it's a scalar.
2466 * dst_reg gets the pointer type and since some positive
2467 * integer value was added to the pointer, give it a new 'id'
2468 * if it's a PTR_TO_PACKET.
2469 * this creates a new 'base' pointer, off_reg (variable) gets
2470 * added into the variable offset, and we copy the fixed offset
2471 * from ptr_reg.
2472 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002473 if (signed_add_overflows(smin_ptr, smin_val) ||
2474 signed_add_overflows(smax_ptr, smax_val)) {
2475 dst_reg->smin_value = S64_MIN;
2476 dst_reg->smax_value = S64_MAX;
2477 } else {
2478 dst_reg->smin_value = smin_ptr + smin_val;
2479 dst_reg->smax_value = smax_ptr + smax_val;
2480 }
2481 if (umin_ptr + umin_val < umin_ptr ||
2482 umax_ptr + umax_val < umax_ptr) {
2483 dst_reg->umin_value = 0;
2484 dst_reg->umax_value = U64_MAX;
2485 } else {
2486 dst_reg->umin_value = umin_ptr + umin_val;
2487 dst_reg->umax_value = umax_ptr + umax_val;
2488 }
Edward Creef1174f72017-08-07 15:26:19 +01002489 dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off);
2490 dst_reg->off = ptr_reg->off;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002491 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01002492 dst_reg->id = ++env->id_gen;
2493 /* something was added to pkt_ptr, set range to zero */
2494 dst_reg->range = 0;
2495 }
Josef Bacik48461132016-09-28 10:54:32 -04002496 break;
2497 case BPF_SUB:
Edward Creef1174f72017-08-07 15:26:19 +01002498 if (dst_reg == off_reg) {
2499 /* scalar -= pointer. Creates an unknown scalar */
2500 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002501 verbose(env, "R%d tried to subtract pointer from scalar\n",
Edward Creef1174f72017-08-07 15:26:19 +01002502 dst);
2503 return -EACCES;
2504 }
2505 /* We don't allow subtraction from FP, because (according to
2506 * test_verifier.c test "invalid fp arithmetic", JITs might not
2507 * be able to deal with it.
Edward Cree93057062017-07-21 14:37:34 +01002508 */
Edward Creef1174f72017-08-07 15:26:19 +01002509 if (ptr_reg->type == PTR_TO_STACK) {
2510 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002511 verbose(env, "R%d subtraction from stack pointer prohibited\n",
Edward Creef1174f72017-08-07 15:26:19 +01002512 dst);
2513 return -EACCES;
2514 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002515 if (known && (ptr_reg->off - smin_val ==
2516 (s64)(s32)(ptr_reg->off - smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01002517 /* pointer -= K. Subtract it from fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01002518 dst_reg->smin_value = smin_ptr;
2519 dst_reg->smax_value = smax_ptr;
2520 dst_reg->umin_value = umin_ptr;
2521 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01002522 dst_reg->var_off = ptr_reg->var_off;
2523 dst_reg->id = ptr_reg->id;
Edward Creeb03c9f92017-08-07 15:26:36 +01002524 dst_reg->off = ptr_reg->off - smin_val;
Edward Creef1174f72017-08-07 15:26:19 +01002525 dst_reg->range = ptr_reg->range;
2526 break;
2527 }
Edward Creef1174f72017-08-07 15:26:19 +01002528 /* A new variable offset is created. If the subtrahend is known
2529 * nonnegative, then any reg->range we had before is still good.
2530 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002531 if (signed_sub_overflows(smin_ptr, smax_val) ||
2532 signed_sub_overflows(smax_ptr, smin_val)) {
2533 /* Overflow possible, we know nothing */
2534 dst_reg->smin_value = S64_MIN;
2535 dst_reg->smax_value = S64_MAX;
2536 } else {
2537 dst_reg->smin_value = smin_ptr - smax_val;
2538 dst_reg->smax_value = smax_ptr - smin_val;
2539 }
2540 if (umin_ptr < umax_val) {
2541 /* Overflow possible, we know nothing */
2542 dst_reg->umin_value = 0;
2543 dst_reg->umax_value = U64_MAX;
2544 } else {
2545 /* Cannot overflow (as long as bounds are consistent) */
2546 dst_reg->umin_value = umin_ptr - umax_val;
2547 dst_reg->umax_value = umax_ptr - umin_val;
2548 }
Edward Creef1174f72017-08-07 15:26:19 +01002549 dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off);
2550 dst_reg->off = ptr_reg->off;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002551 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01002552 dst_reg->id = ++env->id_gen;
2553 /* something was added to pkt_ptr, set range to zero */
Edward Creeb03c9f92017-08-07 15:26:36 +01002554 if (smin_val < 0)
Edward Creef1174f72017-08-07 15:26:19 +01002555 dst_reg->range = 0;
2556 }
2557 break;
2558 case BPF_AND:
2559 case BPF_OR:
2560 case BPF_XOR:
2561 /* bitwise ops on pointers are troublesome, prohibit for now.
2562 * (However, in principle we could allow some cases, e.g.
2563 * ptr &= ~3 which would reduce min_value by 3.)
2564 */
2565 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002566 verbose(env, "R%d bitwise operator %s on pointer prohibited\n",
Edward Creef1174f72017-08-07 15:26:19 +01002567 dst, bpf_alu_string[opcode >> 4]);
2568 return -EACCES;
2569 default:
2570 /* other operators (e.g. MUL,LSH) produce non-pointer results */
2571 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002572 verbose(env, "R%d pointer arithmetic with %s operator prohibited\n",
Edward Creef1174f72017-08-07 15:26:19 +01002573 dst, bpf_alu_string[opcode >> 4]);
2574 return -EACCES;
2575 }
2576
Edward Creeb03c9f92017-08-07 15:26:36 +01002577 __update_reg_bounds(dst_reg);
2578 __reg_deduce_bounds(dst_reg);
2579 __reg_bound_offset(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002580 return 0;
2581}
2582
2583static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
2584 struct bpf_insn *insn,
2585 struct bpf_reg_state *dst_reg,
2586 struct bpf_reg_state src_reg)
2587{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002588 struct bpf_reg_state *regs = cur_regs(env);
Edward Creef1174f72017-08-07 15:26:19 +01002589 u8 opcode = BPF_OP(insn->code);
2590 bool src_known, dst_known;
Edward Creeb03c9f92017-08-07 15:26:36 +01002591 s64 smin_val, smax_val;
2592 u64 umin_val, umax_val;
Edward Creef1174f72017-08-07 15:26:19 +01002593
2594 if (BPF_CLASS(insn->code) != BPF_ALU64) {
2595 /* 32-bit ALU ops are (32,32)->64 */
2596 coerce_reg_to_32(dst_reg);
2597 coerce_reg_to_32(&src_reg);
2598 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002599 smin_val = src_reg.smin_value;
2600 smax_val = src_reg.smax_value;
2601 umin_val = src_reg.umin_value;
2602 umax_val = src_reg.umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01002603 src_known = tnum_is_const(src_reg.var_off);
2604 dst_known = tnum_is_const(dst_reg->var_off);
2605
2606 switch (opcode) {
2607 case BPF_ADD:
Edward Creeb03c9f92017-08-07 15:26:36 +01002608 if (signed_add_overflows(dst_reg->smin_value, smin_val) ||
2609 signed_add_overflows(dst_reg->smax_value, smax_val)) {
2610 dst_reg->smin_value = S64_MIN;
2611 dst_reg->smax_value = S64_MAX;
2612 } else {
2613 dst_reg->smin_value += smin_val;
2614 dst_reg->smax_value += smax_val;
2615 }
2616 if (dst_reg->umin_value + umin_val < umin_val ||
2617 dst_reg->umax_value + umax_val < umax_val) {
2618 dst_reg->umin_value = 0;
2619 dst_reg->umax_value = U64_MAX;
2620 } else {
2621 dst_reg->umin_value += umin_val;
2622 dst_reg->umax_value += umax_val;
2623 }
Edward Creef1174f72017-08-07 15:26:19 +01002624 dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off);
2625 break;
2626 case BPF_SUB:
Edward Creeb03c9f92017-08-07 15:26:36 +01002627 if (signed_sub_overflows(dst_reg->smin_value, smax_val) ||
2628 signed_sub_overflows(dst_reg->smax_value, smin_val)) {
2629 /* Overflow possible, we know nothing */
2630 dst_reg->smin_value = S64_MIN;
2631 dst_reg->smax_value = S64_MAX;
2632 } else {
2633 dst_reg->smin_value -= smax_val;
2634 dst_reg->smax_value -= smin_val;
2635 }
2636 if (dst_reg->umin_value < umax_val) {
2637 /* Overflow possible, we know nothing */
2638 dst_reg->umin_value = 0;
2639 dst_reg->umax_value = U64_MAX;
2640 } else {
2641 /* Cannot overflow (as long as bounds are consistent) */
2642 dst_reg->umin_value -= umax_val;
2643 dst_reg->umax_value -= umin_val;
2644 }
Edward Creef1174f72017-08-07 15:26:19 +01002645 dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off);
Josef Bacik48461132016-09-28 10:54:32 -04002646 break;
2647 case BPF_MUL:
Edward Creeb03c9f92017-08-07 15:26:36 +01002648 dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off);
2649 if (smin_val < 0 || dst_reg->smin_value < 0) {
Edward Creef1174f72017-08-07 15:26:19 +01002650 /* Ain't nobody got time to multiply that sign */
Edward Creeb03c9f92017-08-07 15:26:36 +01002651 __mark_reg_unbounded(dst_reg);
2652 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002653 break;
2654 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002655 /* Both values are positive, so we can work with unsigned and
2656 * copy the result to signed (unless it exceeds S64_MAX).
Edward Creef1174f72017-08-07 15:26:19 +01002657 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002658 if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) {
2659 /* Potential overflow, we know nothing */
2660 __mark_reg_unbounded(dst_reg);
2661 /* (except what we can learn from the var_off) */
2662 __update_reg_bounds(dst_reg);
2663 break;
2664 }
2665 dst_reg->umin_value *= umin_val;
2666 dst_reg->umax_value *= umax_val;
2667 if (dst_reg->umax_value > S64_MAX) {
2668 /* Overflow possible, we know nothing */
2669 dst_reg->smin_value = S64_MIN;
2670 dst_reg->smax_value = S64_MAX;
2671 } else {
2672 dst_reg->smin_value = dst_reg->umin_value;
2673 dst_reg->smax_value = dst_reg->umax_value;
2674 }
Josef Bacik48461132016-09-28 10:54:32 -04002675 break;
2676 case BPF_AND:
Edward Creef1174f72017-08-07 15:26:19 +01002677 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01002678 __mark_reg_known(dst_reg, dst_reg->var_off.value &
2679 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01002680 break;
2681 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002682 /* We get our minimum from the var_off, since that's inherently
2683 * bitwise. Our maximum is the minimum of the operands' maxima.
Josef Bacikf23cc642016-11-14 15:45:36 -05002684 */
Edward Creef1174f72017-08-07 15:26:19 +01002685 dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01002686 dst_reg->umin_value = dst_reg->var_off.value;
2687 dst_reg->umax_value = min(dst_reg->umax_value, umax_val);
2688 if (dst_reg->smin_value < 0 || smin_val < 0) {
2689 /* Lose signed bounds when ANDing negative numbers,
2690 * ain't nobody got time for that.
2691 */
2692 dst_reg->smin_value = S64_MIN;
2693 dst_reg->smax_value = S64_MAX;
2694 } else {
2695 /* ANDing two positives gives a positive, so safe to
2696 * cast result into s64.
2697 */
2698 dst_reg->smin_value = dst_reg->umin_value;
2699 dst_reg->smax_value = dst_reg->umax_value;
2700 }
2701 /* We may learn something more from the var_off */
2702 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002703 break;
2704 case BPF_OR:
2705 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01002706 __mark_reg_known(dst_reg, dst_reg->var_off.value |
2707 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01002708 break;
2709 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002710 /* We get our maximum from the var_off, and our minimum is the
2711 * maximum of the operands' minima
Edward Creef1174f72017-08-07 15:26:19 +01002712 */
2713 dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01002714 dst_reg->umin_value = max(dst_reg->umin_value, umin_val);
2715 dst_reg->umax_value = dst_reg->var_off.value |
2716 dst_reg->var_off.mask;
2717 if (dst_reg->smin_value < 0 || smin_val < 0) {
2718 /* Lose signed bounds when ORing negative numbers,
2719 * ain't nobody got time for that.
2720 */
2721 dst_reg->smin_value = S64_MIN;
2722 dst_reg->smax_value = S64_MAX;
Edward Creef1174f72017-08-07 15:26:19 +01002723 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01002724 /* ORing two positives gives a positive, so safe to
2725 * cast result into s64.
2726 */
2727 dst_reg->smin_value = dst_reg->umin_value;
2728 dst_reg->smax_value = dst_reg->umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01002729 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002730 /* We may learn something more from the var_off */
2731 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002732 break;
2733 case BPF_LSH:
Edward Creeb03c9f92017-08-07 15:26:36 +01002734 if (umax_val > 63) {
2735 /* Shifts greater than 63 are undefined. This includes
2736 * shifts by a negative number.
2737 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002738 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002739 break;
2740 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002741 /* We lose all sign bit information (except what we can pick
2742 * up from var_off)
Josef Bacik48461132016-09-28 10:54:32 -04002743 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002744 dst_reg->smin_value = S64_MIN;
2745 dst_reg->smax_value = S64_MAX;
2746 /* If we might shift our top bit out, then we know nothing */
2747 if (dst_reg->umax_value > 1ULL << (63 - umax_val)) {
2748 dst_reg->umin_value = 0;
2749 dst_reg->umax_value = U64_MAX;
David S. Millerd1174412017-05-10 11:22:52 -07002750 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01002751 dst_reg->umin_value <<= umin_val;
2752 dst_reg->umax_value <<= umax_val;
David S. Millerd1174412017-05-10 11:22:52 -07002753 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002754 if (src_known)
2755 dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val);
2756 else
2757 dst_reg->var_off = tnum_lshift(tnum_unknown, umin_val);
2758 /* We may learn something more from the var_off */
2759 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002760 break;
2761 case BPF_RSH:
Edward Creeb03c9f92017-08-07 15:26:36 +01002762 if (umax_val > 63) {
2763 /* Shifts greater than 63 are undefined. This includes
2764 * shifts by a negative number.
2765 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002766 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002767 break;
2768 }
2769 /* BPF_RSH is an unsigned shift, so make the appropriate casts */
Edward Creeb03c9f92017-08-07 15:26:36 +01002770 if (dst_reg->smin_value < 0) {
2771 if (umin_val) {
Edward Creef1174f72017-08-07 15:26:19 +01002772 /* Sign bit will be cleared */
Edward Creeb03c9f92017-08-07 15:26:36 +01002773 dst_reg->smin_value = 0;
2774 } else {
2775 /* Lost sign bit information */
2776 dst_reg->smin_value = S64_MIN;
2777 dst_reg->smax_value = S64_MAX;
2778 }
David S. Millerd1174412017-05-10 11:22:52 -07002779 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01002780 dst_reg->smin_value =
2781 (u64)(dst_reg->smin_value) >> umax_val;
David S. Millerd1174412017-05-10 11:22:52 -07002782 }
Edward Creef1174f72017-08-07 15:26:19 +01002783 if (src_known)
Edward Creeb03c9f92017-08-07 15:26:36 +01002784 dst_reg->var_off = tnum_rshift(dst_reg->var_off,
2785 umin_val);
Edward Creef1174f72017-08-07 15:26:19 +01002786 else
Edward Creeb03c9f92017-08-07 15:26:36 +01002787 dst_reg->var_off = tnum_rshift(tnum_unknown, umin_val);
2788 dst_reg->umin_value >>= umax_val;
2789 dst_reg->umax_value >>= umin_val;
2790 /* We may learn something more from the var_off */
2791 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002792 break;
2793 default:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002794 mark_reg_unknown(env, regs, insn->dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002795 break;
2796 }
2797
Edward Creeb03c9f92017-08-07 15:26:36 +01002798 __reg_deduce_bounds(dst_reg);
2799 __reg_bound_offset(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002800 return 0;
2801}
2802
2803/* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max
2804 * and var_off.
2805 */
2806static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
2807 struct bpf_insn *insn)
2808{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002809 struct bpf_verifier_state *vstate = env->cur_state;
2810 struct bpf_func_state *state = vstate->frame[vstate->curframe];
2811 struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg;
Edward Creef1174f72017-08-07 15:26:19 +01002812 struct bpf_reg_state *ptr_reg = NULL, off_reg = {0};
2813 u8 opcode = BPF_OP(insn->code);
2814 int rc;
2815
2816 dst_reg = &regs[insn->dst_reg];
Edward Creef1174f72017-08-07 15:26:19 +01002817 src_reg = NULL;
2818 if (dst_reg->type != SCALAR_VALUE)
2819 ptr_reg = dst_reg;
2820 if (BPF_SRC(insn->code) == BPF_X) {
2821 src_reg = &regs[insn->src_reg];
Edward Creef1174f72017-08-07 15:26:19 +01002822 if (src_reg->type != SCALAR_VALUE) {
2823 if (dst_reg->type != SCALAR_VALUE) {
2824 /* Combining two pointers by any ALU op yields
2825 * an arbitrary scalar.
2826 */
2827 if (!env->allow_ptr_leaks) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002828 verbose(env, "R%d pointer %s pointer prohibited\n",
Edward Creef1174f72017-08-07 15:26:19 +01002829 insn->dst_reg,
2830 bpf_alu_string[opcode >> 4]);
2831 return -EACCES;
2832 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002833 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002834 return 0;
2835 } else {
2836 /* scalar += pointer
2837 * This is legal, but we have to reverse our
2838 * src/dest handling in computing the range
2839 */
2840 rc = adjust_ptr_min_max_vals(env, insn,
2841 src_reg, dst_reg);
2842 if (rc == -EACCES && env->allow_ptr_leaks) {
2843 /* scalar += unknown scalar */
2844 __mark_reg_unknown(&off_reg);
2845 return adjust_scalar_min_max_vals(
2846 env, insn,
2847 dst_reg, off_reg);
2848 }
2849 return rc;
2850 }
2851 } else if (ptr_reg) {
2852 /* pointer += scalar */
2853 rc = adjust_ptr_min_max_vals(env, insn,
2854 dst_reg, src_reg);
2855 if (rc == -EACCES && env->allow_ptr_leaks) {
2856 /* unknown scalar += scalar */
2857 __mark_reg_unknown(dst_reg);
2858 return adjust_scalar_min_max_vals(
2859 env, insn, dst_reg, *src_reg);
2860 }
2861 return rc;
2862 }
2863 } else {
2864 /* Pretend the src is a reg with a known value, since we only
2865 * need to be able to read from this state.
2866 */
2867 off_reg.type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01002868 __mark_reg_known(&off_reg, insn->imm);
Edward Creef1174f72017-08-07 15:26:19 +01002869 src_reg = &off_reg;
Edward Creef1174f72017-08-07 15:26:19 +01002870 if (ptr_reg) { /* pointer += K */
2871 rc = adjust_ptr_min_max_vals(env, insn,
2872 ptr_reg, src_reg);
2873 if (rc == -EACCES && env->allow_ptr_leaks) {
2874 /* unknown scalar += K */
2875 __mark_reg_unknown(dst_reg);
2876 return adjust_scalar_min_max_vals(
2877 env, insn, dst_reg, off_reg);
2878 }
2879 return rc;
2880 }
2881 }
2882
2883 /* Got here implies adding two SCALAR_VALUEs */
2884 if (WARN_ON_ONCE(ptr_reg)) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002885 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002886 verbose(env, "verifier internal error: unexpected ptr_reg\n");
Edward Creef1174f72017-08-07 15:26:19 +01002887 return -EINVAL;
2888 }
2889 if (WARN_ON(!src_reg)) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002890 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002891 verbose(env, "verifier internal error: no src_reg\n");
Edward Creef1174f72017-08-07 15:26:19 +01002892 return -EINVAL;
2893 }
2894 return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002895}
2896
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002897/* check validity of 32-bit and 64-bit arithmetic operations */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002898static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002899{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002900 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002901 u8 opcode = BPF_OP(insn->code);
2902 int err;
2903
2904 if (opcode == BPF_END || opcode == BPF_NEG) {
2905 if (opcode == BPF_NEG) {
2906 if (BPF_SRC(insn->code) != 0 ||
2907 insn->src_reg != BPF_REG_0 ||
2908 insn->off != 0 || insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002909 verbose(env, "BPF_NEG uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002910 return -EINVAL;
2911 }
2912 } else {
2913 if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
Edward Creee67b8a62017-09-15 14:37:38 +01002914 (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) ||
2915 BPF_CLASS(insn->code) == BPF_ALU64) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002916 verbose(env, "BPF_END uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002917 return -EINVAL;
2918 }
2919 }
2920
2921 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01002922 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002923 if (err)
2924 return err;
2925
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002926 if (is_pointer_value(env, insn->dst_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002927 verbose(env, "R%d pointer arithmetic prohibited\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002928 insn->dst_reg);
2929 return -EACCES;
2930 }
2931
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002932 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01002933 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002934 if (err)
2935 return err;
2936
2937 } else if (opcode == BPF_MOV) {
2938
2939 if (BPF_SRC(insn->code) == BPF_X) {
2940 if (insn->imm != 0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002941 verbose(env, "BPF_MOV uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002942 return -EINVAL;
2943 }
2944
2945 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01002946 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002947 if (err)
2948 return err;
2949 } else {
2950 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002951 verbose(env, "BPF_MOV uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002952 return -EINVAL;
2953 }
2954 }
2955
2956 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01002957 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002958 if (err)
2959 return err;
2960
2961 if (BPF_SRC(insn->code) == BPF_X) {
2962 if (BPF_CLASS(insn->code) == BPF_ALU64) {
2963 /* case: R1 = R2
2964 * copy register state to dest reg
2965 */
2966 regs[insn->dst_reg] = regs[insn->src_reg];
Alexei Starovoitov8fe2d6c2017-10-05 16:20:56 -07002967 regs[insn->dst_reg].live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002968 } else {
Edward Creef1174f72017-08-07 15:26:19 +01002969 /* R1 = (u32) R2 */
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002970 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002971 verbose(env,
2972 "R%d partial copy of pointer\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002973 insn->src_reg);
2974 return -EACCES;
2975 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002976 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creeb03c9f92017-08-07 15:26:36 +01002977 /* high 32 bits are known zero. */
Edward Creef1174f72017-08-07 15:26:19 +01002978 regs[insn->dst_reg].var_off = tnum_cast(
2979 regs[insn->dst_reg].var_off, 4);
Edward Creeb03c9f92017-08-07 15:26:36 +01002980 __update_reg_bounds(&regs[insn->dst_reg]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002981 }
2982 } else {
2983 /* case: R = imm
2984 * remember the value we stored into this reg
2985 */
Edward Creef1174f72017-08-07 15:26:19 +01002986 regs[insn->dst_reg].type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01002987 __mark_reg_known(regs + insn->dst_reg, insn->imm);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002988 }
2989
2990 } else if (opcode > BPF_END) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002991 verbose(env, "invalid BPF_ALU opcode %x\n", opcode);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002992 return -EINVAL;
2993
2994 } else { /* all other ALU ops: and, sub, xor, add, ... */
2995
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002996 if (BPF_SRC(insn->code) == BPF_X) {
2997 if (insn->imm != 0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002998 verbose(env, "BPF_ALU uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002999 return -EINVAL;
3000 }
3001 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01003002 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003003 if (err)
3004 return err;
3005 } else {
3006 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003007 verbose(env, "BPF_ALU uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003008 return -EINVAL;
3009 }
3010 }
3011
3012 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01003013 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003014 if (err)
3015 return err;
3016
3017 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
3018 BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003019 verbose(env, "div by zero\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003020 return -EINVAL;
3021 }
3022
Rabin Vincent229394e82016-01-12 20:17:08 +01003023 if ((opcode == BPF_LSH || opcode == BPF_RSH ||
3024 opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
3025 int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
3026
3027 if (insn->imm < 0 || insn->imm >= size) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003028 verbose(env, "invalid shift %d\n", insn->imm);
Rabin Vincent229394e82016-01-12 20:17:08 +01003029 return -EINVAL;
3030 }
3031 }
3032
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003033 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01003034 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003035 if (err)
3036 return err;
3037
Edward Creef1174f72017-08-07 15:26:19 +01003038 return adjust_reg_min_max_vals(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003039 }
3040
3041 return 0;
3042}
3043
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003044static void find_good_pkt_pointers(struct bpf_verifier_state *vstate,
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003045 struct bpf_reg_state *dst_reg,
David S. Millerf8ddadc2017-10-22 13:36:53 +01003046 enum bpf_reg_type type,
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003047 bool range_right_open)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003048{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003049 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003050 struct bpf_reg_state *regs = state->regs, *reg;
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003051 u16 new_range;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003052 int i, j;
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003053
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003054 if (dst_reg->off < 0 ||
3055 (dst_reg->off == 0 && range_right_open))
Edward Creef1174f72017-08-07 15:26:19 +01003056 /* This doesn't give us any range */
3057 return;
3058
Edward Creeb03c9f92017-08-07 15:26:36 +01003059 if (dst_reg->umax_value > MAX_PACKET_OFF ||
3060 dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF)
Edward Creef1174f72017-08-07 15:26:19 +01003061 /* Risk of overflow. For instance, ptr + (1<<63) may be less
3062 * than pkt_end, but that's because it's also less than pkt.
3063 */
3064 return;
3065
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003066 new_range = dst_reg->off;
3067 if (range_right_open)
3068 new_range--;
3069
3070 /* Examples for register markings:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003071 *
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003072 * pkt_data in dst register:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003073 *
3074 * r2 = r3;
3075 * r2 += 8;
3076 * if (r2 > pkt_end) goto <handle exception>
3077 * <access okay>
3078 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003079 * r2 = r3;
3080 * r2 += 8;
3081 * if (r2 < pkt_end) goto <access okay>
3082 * <handle exception>
3083 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003084 * Where:
3085 * r2 == dst_reg, pkt_end == src_reg
3086 * r2=pkt(id=n,off=8,r=0)
3087 * r3=pkt(id=n,off=0,r=0)
3088 *
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003089 * pkt_data in src register:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003090 *
3091 * r2 = r3;
3092 * r2 += 8;
3093 * if (pkt_end >= r2) goto <access okay>
3094 * <handle exception>
3095 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003096 * r2 = r3;
3097 * r2 += 8;
3098 * if (pkt_end <= r2) goto <handle exception>
3099 * <access okay>
3100 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003101 * Where:
3102 * pkt_end == dst_reg, r2 == src_reg
3103 * r2=pkt(id=n,off=8,r=0)
3104 * r3=pkt(id=n,off=0,r=0)
3105 *
3106 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003107 * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8)
3108 * and [r3, r3 + 8-1) respectively is safe to access depending on
3109 * the check.
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003110 */
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003111
Edward Creef1174f72017-08-07 15:26:19 +01003112 /* If our ids match, then we must have the same max_value. And we
3113 * don't care about the other reg's fixed offset, since if it's too big
3114 * the range won't allow anything.
3115 * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16.
3116 */
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003117 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003118 if (regs[i].type == type && regs[i].id == dst_reg->id)
Alexei Starovoitovb1977682017-03-24 15:57:33 -07003119 /* keep the maximum range already checked */
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003120 regs[i].range = max(regs[i].range, new_range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003121
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003122 for (j = 0; j <= vstate->curframe; j++) {
3123 state = vstate->frame[j];
3124 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
3125 if (state->stack[i].slot_type[0] != STACK_SPILL)
3126 continue;
3127 reg = &state->stack[i].spilled_ptr;
3128 if (reg->type == type && reg->id == dst_reg->id)
3129 reg->range = max(reg->range, new_range);
3130 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003131 }
3132}
3133
Josef Bacik48461132016-09-28 10:54:32 -04003134/* Adjusts the register min/max values in the case that the dst_reg is the
3135 * variable register that we are working on, and src_reg is a constant or we're
3136 * simply doing a BPF_K check.
Edward Creef1174f72017-08-07 15:26:19 +01003137 * In JEQ/JNE cases we also adjust the var_off values.
Josef Bacik48461132016-09-28 10:54:32 -04003138 */
3139static void reg_set_min_max(struct bpf_reg_state *true_reg,
3140 struct bpf_reg_state *false_reg, u64 val,
3141 u8 opcode)
3142{
Edward Creef1174f72017-08-07 15:26:19 +01003143 /* If the dst_reg is a pointer, we can't learn anything about its
3144 * variable offset from the compare (unless src_reg were a pointer into
3145 * the same object, but we don't bother with that.
3146 * Since false_reg and true_reg have the same type by construction, we
3147 * only need to check one of them for pointerness.
3148 */
3149 if (__is_pointer_value(false, false_reg))
3150 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02003151
Josef Bacik48461132016-09-28 10:54:32 -04003152 switch (opcode) {
3153 case BPF_JEQ:
3154 /* If this is false then we know nothing Jon Snow, but if it is
3155 * true then we know for sure.
3156 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003157 __mark_reg_known(true_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04003158 break;
3159 case BPF_JNE:
3160 /* If this is true we know nothing Jon Snow, but if it is false
3161 * we know the value for sure;
3162 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003163 __mark_reg_known(false_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04003164 break;
3165 case BPF_JGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01003166 false_reg->umax_value = min(false_reg->umax_value, val);
3167 true_reg->umin_value = max(true_reg->umin_value, val + 1);
3168 break;
Josef Bacik48461132016-09-28 10:54:32 -04003169 case BPF_JSGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01003170 false_reg->smax_value = min_t(s64, false_reg->smax_value, val);
3171 true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1);
Josef Bacik48461132016-09-28 10:54:32 -04003172 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003173 case BPF_JLT:
3174 false_reg->umin_value = max(false_reg->umin_value, val);
3175 true_reg->umax_value = min(true_reg->umax_value, val - 1);
3176 break;
3177 case BPF_JSLT:
3178 false_reg->smin_value = max_t(s64, false_reg->smin_value, val);
3179 true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1);
3180 break;
Josef Bacik48461132016-09-28 10:54:32 -04003181 case BPF_JGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01003182 false_reg->umax_value = min(false_reg->umax_value, val - 1);
3183 true_reg->umin_value = max(true_reg->umin_value, val);
3184 break;
Josef Bacik48461132016-09-28 10:54:32 -04003185 case BPF_JSGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01003186 false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1);
3187 true_reg->smin_value = max_t(s64, true_reg->smin_value, val);
Josef Bacik48461132016-09-28 10:54:32 -04003188 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003189 case BPF_JLE:
3190 false_reg->umin_value = max(false_reg->umin_value, val + 1);
3191 true_reg->umax_value = min(true_reg->umax_value, val);
3192 break;
3193 case BPF_JSLE:
3194 false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1);
3195 true_reg->smax_value = min_t(s64, true_reg->smax_value, val);
3196 break;
Josef Bacik48461132016-09-28 10:54:32 -04003197 default:
3198 break;
3199 }
3200
Edward Creeb03c9f92017-08-07 15:26:36 +01003201 __reg_deduce_bounds(false_reg);
3202 __reg_deduce_bounds(true_reg);
3203 /* We might have learned some bits from the bounds. */
3204 __reg_bound_offset(false_reg);
3205 __reg_bound_offset(true_reg);
3206 /* Intersecting with the old var_off might have improved our bounds
3207 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
3208 * then new var_off is (0; 0x7f...fc) which improves our umax.
3209 */
3210 __update_reg_bounds(false_reg);
3211 __update_reg_bounds(true_reg);
Josef Bacik48461132016-09-28 10:54:32 -04003212}
3213
Edward Creef1174f72017-08-07 15:26:19 +01003214/* Same as above, but for the case that dst_reg holds a constant and src_reg is
3215 * the variable reg.
Josef Bacik48461132016-09-28 10:54:32 -04003216 */
3217static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
3218 struct bpf_reg_state *false_reg, u64 val,
3219 u8 opcode)
3220{
Edward Creef1174f72017-08-07 15:26:19 +01003221 if (__is_pointer_value(false, false_reg))
3222 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02003223
Josef Bacik48461132016-09-28 10:54:32 -04003224 switch (opcode) {
3225 case BPF_JEQ:
3226 /* If this is false then we know nothing Jon Snow, but if it is
3227 * true then we know for sure.
3228 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003229 __mark_reg_known(true_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04003230 break;
3231 case BPF_JNE:
3232 /* If this is true we know nothing Jon Snow, but if it is false
3233 * we know the value for sure;
3234 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003235 __mark_reg_known(false_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04003236 break;
3237 case BPF_JGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01003238 true_reg->umax_value = min(true_reg->umax_value, val - 1);
3239 false_reg->umin_value = max(false_reg->umin_value, val);
3240 break;
Josef Bacik48461132016-09-28 10:54:32 -04003241 case BPF_JSGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01003242 true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1);
3243 false_reg->smin_value = max_t(s64, false_reg->smin_value, val);
Josef Bacik48461132016-09-28 10:54:32 -04003244 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003245 case BPF_JLT:
3246 true_reg->umin_value = max(true_reg->umin_value, val + 1);
3247 false_reg->umax_value = min(false_reg->umax_value, val);
3248 break;
3249 case BPF_JSLT:
3250 true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1);
3251 false_reg->smax_value = min_t(s64, false_reg->smax_value, val);
3252 break;
Josef Bacik48461132016-09-28 10:54:32 -04003253 case BPF_JGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01003254 true_reg->umax_value = min(true_reg->umax_value, val);
3255 false_reg->umin_value = max(false_reg->umin_value, val + 1);
3256 break;
Josef Bacik48461132016-09-28 10:54:32 -04003257 case BPF_JSGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01003258 true_reg->smax_value = min_t(s64, true_reg->smax_value, val);
3259 false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1);
Josef Bacik48461132016-09-28 10:54:32 -04003260 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003261 case BPF_JLE:
3262 true_reg->umin_value = max(true_reg->umin_value, val);
3263 false_reg->umax_value = min(false_reg->umax_value, val - 1);
3264 break;
3265 case BPF_JSLE:
3266 true_reg->smin_value = max_t(s64, true_reg->smin_value, val);
3267 false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1);
3268 break;
Josef Bacik48461132016-09-28 10:54:32 -04003269 default:
3270 break;
3271 }
3272
Edward Creeb03c9f92017-08-07 15:26:36 +01003273 __reg_deduce_bounds(false_reg);
3274 __reg_deduce_bounds(true_reg);
3275 /* We might have learned some bits from the bounds. */
3276 __reg_bound_offset(false_reg);
3277 __reg_bound_offset(true_reg);
3278 /* Intersecting with the old var_off might have improved our bounds
3279 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
3280 * then new var_off is (0; 0x7f...fc) which improves our umax.
3281 */
3282 __update_reg_bounds(false_reg);
3283 __update_reg_bounds(true_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003284}
3285
3286/* Regs are known to be equal, so intersect their min/max/var_off */
3287static void __reg_combine_min_max(struct bpf_reg_state *src_reg,
3288 struct bpf_reg_state *dst_reg)
3289{
Edward Creeb03c9f92017-08-07 15:26:36 +01003290 src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value,
3291 dst_reg->umin_value);
3292 src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value,
3293 dst_reg->umax_value);
3294 src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value,
3295 dst_reg->smin_value);
3296 src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value,
3297 dst_reg->smax_value);
Edward Creef1174f72017-08-07 15:26:19 +01003298 src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off,
3299 dst_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01003300 /* We might have learned new bounds from the var_off. */
3301 __update_reg_bounds(src_reg);
3302 __update_reg_bounds(dst_reg);
3303 /* We might have learned something about the sign bit. */
3304 __reg_deduce_bounds(src_reg);
3305 __reg_deduce_bounds(dst_reg);
3306 /* We might have learned some bits from the bounds. */
3307 __reg_bound_offset(src_reg);
3308 __reg_bound_offset(dst_reg);
3309 /* Intersecting with the old var_off might have improved our bounds
3310 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
3311 * then new var_off is (0; 0x7f...fc) which improves our umax.
3312 */
3313 __update_reg_bounds(src_reg);
3314 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003315}
3316
3317static void reg_combine_min_max(struct bpf_reg_state *true_src,
3318 struct bpf_reg_state *true_dst,
3319 struct bpf_reg_state *false_src,
3320 struct bpf_reg_state *false_dst,
3321 u8 opcode)
3322{
3323 switch (opcode) {
3324 case BPF_JEQ:
3325 __reg_combine_min_max(true_src, true_dst);
3326 break;
3327 case BPF_JNE:
3328 __reg_combine_min_max(false_src, false_dst);
Edward Creeb03c9f92017-08-07 15:26:36 +01003329 break;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02003330 }
Josef Bacik48461132016-09-28 10:54:32 -04003331}
3332
Thomas Graf57a09bf2016-10-18 19:51:19 +02003333static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id,
Edward Creef1174f72017-08-07 15:26:19 +01003334 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02003335{
3336 struct bpf_reg_state *reg = &regs[regno];
3337
3338 if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) {
Edward Creef1174f72017-08-07 15:26:19 +01003339 /* Old offset (both fixed and variable parts) should
3340 * have been known-zero, because we don't allow pointer
3341 * arithmetic on pointers that might be NULL.
3342 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003343 if (WARN_ON_ONCE(reg->smin_value || reg->smax_value ||
3344 !tnum_equals_const(reg->var_off, 0) ||
Edward Creef1174f72017-08-07 15:26:19 +01003345 reg->off)) {
Edward Creeb03c9f92017-08-07 15:26:36 +01003346 __mark_reg_known_zero(reg);
3347 reg->off = 0;
Edward Creef1174f72017-08-07 15:26:19 +01003348 }
3349 if (is_null) {
3350 reg->type = SCALAR_VALUE;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07003351 } else if (reg->map_ptr->inner_map_meta) {
3352 reg->type = CONST_PTR_TO_MAP;
3353 reg->map_ptr = reg->map_ptr->inner_map_meta;
3354 } else {
Edward Creef1174f72017-08-07 15:26:19 +01003355 reg->type = PTR_TO_MAP_VALUE;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07003356 }
Daniel Borkmanna08dd0d2016-12-15 01:30:06 +01003357 /* We don't need id from this point onwards anymore, thus we
3358 * should better reset it, so that state pruning has chances
3359 * to take effect.
3360 */
3361 reg->id = 0;
Thomas Graf57a09bf2016-10-18 19:51:19 +02003362 }
3363}
3364
3365/* The logic is similar to find_good_pkt_pointers(), both could eventually
3366 * be folded together at some point.
3367 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003368static void mark_map_regs(struct bpf_verifier_state *vstate, u32 regno,
Edward Creef1174f72017-08-07 15:26:19 +01003369 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02003370{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003371 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Thomas Graf57a09bf2016-10-18 19:51:19 +02003372 struct bpf_reg_state *regs = state->regs;
Daniel Borkmanna08dd0d2016-12-15 01:30:06 +01003373 u32 id = regs[regno].id;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003374 int i, j;
Thomas Graf57a09bf2016-10-18 19:51:19 +02003375
3376 for (i = 0; i < MAX_BPF_REG; i++)
Edward Creef1174f72017-08-07 15:26:19 +01003377 mark_map_reg(regs, i, id, is_null);
Thomas Graf57a09bf2016-10-18 19:51:19 +02003378
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003379 for (j = 0; j <= vstate->curframe; j++) {
3380 state = vstate->frame[j];
3381 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
3382 if (state->stack[i].slot_type[0] != STACK_SPILL)
3383 continue;
3384 mark_map_reg(&state->stack[i].spilled_ptr, 0, id, is_null);
3385 }
Thomas Graf57a09bf2016-10-18 19:51:19 +02003386 }
3387}
3388
Daniel Borkmann5beca082017-11-01 23:58:10 +01003389static bool try_match_pkt_pointers(const struct bpf_insn *insn,
3390 struct bpf_reg_state *dst_reg,
3391 struct bpf_reg_state *src_reg,
3392 struct bpf_verifier_state *this_branch,
3393 struct bpf_verifier_state *other_branch)
3394{
3395 if (BPF_SRC(insn->code) != BPF_X)
3396 return false;
3397
3398 switch (BPF_OP(insn->code)) {
3399 case BPF_JGT:
3400 if ((dst_reg->type == PTR_TO_PACKET &&
3401 src_reg->type == PTR_TO_PACKET_END) ||
3402 (dst_reg->type == PTR_TO_PACKET_META &&
3403 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
3404 /* pkt_data' > pkt_end, pkt_meta' > pkt_data */
3405 find_good_pkt_pointers(this_branch, dst_reg,
3406 dst_reg->type, false);
3407 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
3408 src_reg->type == PTR_TO_PACKET) ||
3409 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
3410 src_reg->type == PTR_TO_PACKET_META)) {
3411 /* pkt_end > pkt_data', pkt_data > pkt_meta' */
3412 find_good_pkt_pointers(other_branch, src_reg,
3413 src_reg->type, true);
3414 } else {
3415 return false;
3416 }
3417 break;
3418 case BPF_JLT:
3419 if ((dst_reg->type == PTR_TO_PACKET &&
3420 src_reg->type == PTR_TO_PACKET_END) ||
3421 (dst_reg->type == PTR_TO_PACKET_META &&
3422 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
3423 /* pkt_data' < pkt_end, pkt_meta' < pkt_data */
3424 find_good_pkt_pointers(other_branch, dst_reg,
3425 dst_reg->type, true);
3426 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
3427 src_reg->type == PTR_TO_PACKET) ||
3428 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
3429 src_reg->type == PTR_TO_PACKET_META)) {
3430 /* pkt_end < pkt_data', pkt_data > pkt_meta' */
3431 find_good_pkt_pointers(this_branch, src_reg,
3432 src_reg->type, false);
3433 } else {
3434 return false;
3435 }
3436 break;
3437 case BPF_JGE:
3438 if ((dst_reg->type == PTR_TO_PACKET &&
3439 src_reg->type == PTR_TO_PACKET_END) ||
3440 (dst_reg->type == PTR_TO_PACKET_META &&
3441 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
3442 /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */
3443 find_good_pkt_pointers(this_branch, dst_reg,
3444 dst_reg->type, true);
3445 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
3446 src_reg->type == PTR_TO_PACKET) ||
3447 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
3448 src_reg->type == PTR_TO_PACKET_META)) {
3449 /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */
3450 find_good_pkt_pointers(other_branch, src_reg,
3451 src_reg->type, false);
3452 } else {
3453 return false;
3454 }
3455 break;
3456 case BPF_JLE:
3457 if ((dst_reg->type == PTR_TO_PACKET &&
3458 src_reg->type == PTR_TO_PACKET_END) ||
3459 (dst_reg->type == PTR_TO_PACKET_META &&
3460 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
3461 /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */
3462 find_good_pkt_pointers(other_branch, dst_reg,
3463 dst_reg->type, false);
3464 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
3465 src_reg->type == PTR_TO_PACKET) ||
3466 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
3467 src_reg->type == PTR_TO_PACKET_META)) {
3468 /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */
3469 find_good_pkt_pointers(this_branch, src_reg,
3470 src_reg->type, true);
3471 } else {
3472 return false;
3473 }
3474 break;
3475 default:
3476 return false;
3477 }
3478
3479 return true;
3480}
3481
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003482static int check_cond_jmp_op(struct bpf_verifier_env *env,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003483 struct bpf_insn *insn, int *insn_idx)
3484{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003485 struct bpf_verifier_state *this_branch = env->cur_state;
3486 struct bpf_verifier_state *other_branch;
3487 struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs;
3488 struct bpf_reg_state *dst_reg, *other_branch_regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003489 u8 opcode = BPF_OP(insn->code);
3490 int err;
3491
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003492 if (opcode > BPF_JSLE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003493 verbose(env, "invalid BPF_JMP opcode %x\n", opcode);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003494 return -EINVAL;
3495 }
3496
3497 if (BPF_SRC(insn->code) == BPF_X) {
3498 if (insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003499 verbose(env, "BPF_JMP uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003500 return -EINVAL;
3501 }
3502
3503 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01003504 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003505 if (err)
3506 return err;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003507
3508 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003509 verbose(env, "R%d pointer comparison prohibited\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003510 insn->src_reg);
3511 return -EACCES;
3512 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003513 } else {
3514 if (insn->src_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003515 verbose(env, "BPF_JMP uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003516 return -EINVAL;
3517 }
3518 }
3519
3520 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01003521 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003522 if (err)
3523 return err;
3524
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003525 dst_reg = &regs[insn->dst_reg];
3526
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003527 /* detect if R == 0 where R was initialized to zero earlier */
3528 if (BPF_SRC(insn->code) == BPF_K &&
3529 (opcode == BPF_JEQ || opcode == BPF_JNE) &&
Edward Creef1174f72017-08-07 15:26:19 +01003530 dst_reg->type == SCALAR_VALUE &&
Alexei Starovoitov3bf15922017-11-30 21:31:39 -08003531 tnum_is_const(dst_reg->var_off)) {
3532 if ((opcode == BPF_JEQ && dst_reg->var_off.value == insn->imm) ||
3533 (opcode == BPF_JNE && dst_reg->var_off.value != insn->imm)) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003534 /* if (imm == imm) goto pc+off;
3535 * only follow the goto, ignore fall-through
3536 */
3537 *insn_idx += insn->off;
3538 return 0;
3539 } else {
3540 /* if (imm != imm) goto pc+off;
3541 * only follow fall-through branch, since
3542 * that's where the program will go
3543 */
3544 return 0;
3545 }
3546 }
3547
3548 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx);
3549 if (!other_branch)
3550 return -EFAULT;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003551 other_branch_regs = other_branch->frame[other_branch->curframe]->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003552
Josef Bacik48461132016-09-28 10:54:32 -04003553 /* detect if we are comparing against a constant value so we can adjust
3554 * our min/max values for our dst register.
Edward Creef1174f72017-08-07 15:26:19 +01003555 * this is only legit if both are scalars (or pointers to the same
3556 * object, I suppose, but we don't support that right now), because
3557 * otherwise the different base pointers mean the offsets aren't
3558 * comparable.
Josef Bacik48461132016-09-28 10:54:32 -04003559 */
3560 if (BPF_SRC(insn->code) == BPF_X) {
Edward Creef1174f72017-08-07 15:26:19 +01003561 if (dst_reg->type == SCALAR_VALUE &&
3562 regs[insn->src_reg].type == SCALAR_VALUE) {
3563 if (tnum_is_const(regs[insn->src_reg].var_off))
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003564 reg_set_min_max(&other_branch_regs[insn->dst_reg],
Edward Creef1174f72017-08-07 15:26:19 +01003565 dst_reg, regs[insn->src_reg].var_off.value,
3566 opcode);
3567 else if (tnum_is_const(dst_reg->var_off))
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003568 reg_set_min_max_inv(&other_branch_regs[insn->src_reg],
Edward Creef1174f72017-08-07 15:26:19 +01003569 &regs[insn->src_reg],
3570 dst_reg->var_off.value, opcode);
3571 else if (opcode == BPF_JEQ || opcode == BPF_JNE)
3572 /* Comparing for equality, we can combine knowledge */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003573 reg_combine_min_max(&other_branch_regs[insn->src_reg],
3574 &other_branch_regs[insn->dst_reg],
Edward Creef1174f72017-08-07 15:26:19 +01003575 &regs[insn->src_reg],
3576 &regs[insn->dst_reg], opcode);
3577 }
3578 } else if (dst_reg->type == SCALAR_VALUE) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003579 reg_set_min_max(&other_branch_regs[insn->dst_reg],
Josef Bacik48461132016-09-28 10:54:32 -04003580 dst_reg, insn->imm, opcode);
3581 }
3582
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003583 /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003584 if (BPF_SRC(insn->code) == BPF_K &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003585 insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
3586 dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
Thomas Graf57a09bf2016-10-18 19:51:19 +02003587 /* Mark all identical map registers in each branch as either
3588 * safe or unknown depending R == 0 or R != 0 conditional.
3589 */
Edward Creef1174f72017-08-07 15:26:19 +01003590 mark_map_regs(this_branch, insn->dst_reg, opcode == BPF_JNE);
3591 mark_map_regs(other_branch, insn->dst_reg, opcode == BPF_JEQ);
Daniel Borkmann5beca082017-11-01 23:58:10 +01003592 } else if (!try_match_pkt_pointers(insn, dst_reg, &regs[insn->src_reg],
3593 this_branch, other_branch) &&
3594 is_pointer_value(env, insn->dst_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003595 verbose(env, "R%d pointer comparison prohibited\n",
3596 insn->dst_reg);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003597 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003598 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003599 if (env->log.level)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003600 print_verifier_state(env, this_branch->frame[this_branch->curframe]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003601 return 0;
3602}
3603
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003604/* return the map pointer stored inside BPF_LD_IMM64 instruction */
3605static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
3606{
3607 u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
3608
3609 return (struct bpf_map *) (unsigned long) imm64;
3610}
3611
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003612/* verify BPF_LD_IMM64 instruction */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003613static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003614{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003615 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003616 int err;
3617
3618 if (BPF_SIZE(insn->code) != BPF_DW) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003619 verbose(env, "invalid BPF_LD_IMM insn\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003620 return -EINVAL;
3621 }
3622 if (insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003623 verbose(env, "BPF_LD_IMM64 uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003624 return -EINVAL;
3625 }
3626
Edward Creedc503a82017-08-15 20:34:35 +01003627 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003628 if (err)
3629 return err;
3630
Jakub Kicinski6b173872016-09-21 11:43:59 +01003631 if (insn->src_reg == 0) {
Jakub Kicinski6b173872016-09-21 11:43:59 +01003632 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
3633
Edward Creef1174f72017-08-07 15:26:19 +01003634 regs[insn->dst_reg].type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01003635 __mark_reg_known(&regs[insn->dst_reg], imm);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003636 return 0;
Jakub Kicinski6b173872016-09-21 11:43:59 +01003637 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003638
3639 /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
3640 BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
3641
3642 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
3643 regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
3644 return 0;
3645}
3646
Daniel Borkmann96be4322015-03-01 12:31:46 +01003647static bool may_access_skb(enum bpf_prog_type type)
3648{
3649 switch (type) {
3650 case BPF_PROG_TYPE_SOCKET_FILTER:
3651 case BPF_PROG_TYPE_SCHED_CLS:
Daniel Borkmann94caee8c2015-03-20 15:11:11 +01003652 case BPF_PROG_TYPE_SCHED_ACT:
Daniel Borkmann96be4322015-03-01 12:31:46 +01003653 return true;
3654 default:
3655 return false;
3656 }
3657}
3658
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003659/* verify safety of LD_ABS|LD_IND instructions:
3660 * - they can only appear in the programs where ctx == skb
3661 * - since they are wrappers of function calls, they scratch R1-R5 registers,
3662 * preserve R6-R9, and store return value into R0
3663 *
3664 * Implicit input:
3665 * ctx == skb == R6 == CTX
3666 *
3667 * Explicit input:
3668 * SRC == any register
3669 * IMM == 32-bit immediate
3670 *
3671 * Output:
3672 * R0 - 8/16/32-bit skb data converted to cpu endianness
3673 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003674static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003675{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003676 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003677 u8 mode = BPF_MODE(insn->code);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003678 int i, err;
3679
Daniel Borkmann24701ec2015-03-01 12:31:47 +01003680 if (!may_access_skb(env->prog->type)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003681 verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003682 return -EINVAL;
3683 }
3684
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003685 if (env->subprog_cnt) {
3686 /* when program has LD_ABS insn JITs and interpreter assume
3687 * that r1 == ctx == skb which is not the case for callees
3688 * that can have arbitrary arguments. It's problematic
3689 * for main prog as well since JITs would need to analyze
3690 * all functions in order to make proper register save/restore
3691 * decisions in the main prog. Hence disallow LD_ABS with calls
3692 */
3693 verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n");
3694 return -EINVAL;
3695 }
3696
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003697 if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
Alexei Starovoitovd82bccc2016-04-12 10:26:19 -07003698 BPF_SIZE(insn->code) == BPF_DW ||
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003699 (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003700 verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003701 return -EINVAL;
3702 }
3703
3704 /* check whether implicit source operand (register R6) is readable */
Edward Creedc503a82017-08-15 20:34:35 +01003705 err = check_reg_arg(env, BPF_REG_6, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003706 if (err)
3707 return err;
3708
3709 if (regs[BPF_REG_6].type != PTR_TO_CTX) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003710 verbose(env,
3711 "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003712 return -EINVAL;
3713 }
3714
3715 if (mode == BPF_IND) {
3716 /* check explicit source operand */
Edward Creedc503a82017-08-15 20:34:35 +01003717 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003718 if (err)
3719 return err;
3720 }
3721
3722 /* reset caller saved regs to unreadable */
Edward Creedc503a82017-08-15 20:34:35 +01003723 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003724 mark_reg_not_init(env, regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01003725 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
3726 }
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003727
3728 /* mark destination R0 register as readable, since it contains
Edward Creedc503a82017-08-15 20:34:35 +01003729 * the value fetched from the packet.
3730 * Already marked as written above.
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003731 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003732 mark_reg_unknown(env, regs, BPF_REG_0);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003733 return 0;
3734}
3735
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003736static int check_return_code(struct bpf_verifier_env *env)
3737{
3738 struct bpf_reg_state *reg;
3739 struct tnum range = tnum_range(0, 1);
3740
3741 switch (env->prog->type) {
3742 case BPF_PROG_TYPE_CGROUP_SKB:
3743 case BPF_PROG_TYPE_CGROUP_SOCK:
3744 case BPF_PROG_TYPE_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05003745 case BPF_PROG_TYPE_CGROUP_DEVICE:
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003746 break;
3747 default:
3748 return 0;
3749 }
3750
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003751 reg = cur_regs(env) + BPF_REG_0;
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003752 if (reg->type != SCALAR_VALUE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003753 verbose(env, "At program exit the register R0 is not a known value (%s)\n",
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003754 reg_type_str[reg->type]);
3755 return -EINVAL;
3756 }
3757
3758 if (!tnum_in(range, reg->var_off)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003759 verbose(env, "At program exit the register R0 ");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003760 if (!tnum_is_unknown(reg->var_off)) {
3761 char tn_buf[48];
3762
3763 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003764 verbose(env, "has value %s", tn_buf);
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003765 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003766 verbose(env, "has unknown scalar value");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003767 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003768 verbose(env, " should have been 0 or 1\n");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003769 return -EINVAL;
3770 }
3771 return 0;
3772}
3773
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003774/* non-recursive DFS pseudo code
3775 * 1 procedure DFS-iterative(G,v):
3776 * 2 label v as discovered
3777 * 3 let S be a stack
3778 * 4 S.push(v)
3779 * 5 while S is not empty
3780 * 6 t <- S.pop()
3781 * 7 if t is what we're looking for:
3782 * 8 return t
3783 * 9 for all edges e in G.adjacentEdges(t) do
3784 * 10 if edge e is already labelled
3785 * 11 continue with the next edge
3786 * 12 w <- G.adjacentVertex(t,e)
3787 * 13 if vertex w is not discovered and not explored
3788 * 14 label e as tree-edge
3789 * 15 label w as discovered
3790 * 16 S.push(w)
3791 * 17 continue at 5
3792 * 18 else if vertex w is discovered
3793 * 19 label e as back-edge
3794 * 20 else
3795 * 21 // vertex w is explored
3796 * 22 label e as forward- or cross-edge
3797 * 23 label t as explored
3798 * 24 S.pop()
3799 *
3800 * convention:
3801 * 0x10 - discovered
3802 * 0x11 - discovered and fall-through edge labelled
3803 * 0x12 - discovered and fall-through and branch edges labelled
3804 * 0x20 - explored
3805 */
3806
3807enum {
3808 DISCOVERED = 0x10,
3809 EXPLORED = 0x20,
3810 FALLTHROUGH = 1,
3811 BRANCH = 2,
3812};
3813
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003814#define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003815
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003816static int *insn_stack; /* stack of insns to process */
3817static int cur_stack; /* current stack index */
3818static int *insn_state;
3819
3820/* t, w, e - match pseudo-code above:
3821 * t - index of current instruction
3822 * w - next instruction
3823 * e - edge
3824 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003825static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003826{
3827 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
3828 return 0;
3829
3830 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
3831 return 0;
3832
3833 if (w < 0 || w >= env->prog->len) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003834 verbose(env, "jump out of range from insn %d to %d\n", t, w);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003835 return -EINVAL;
3836 }
3837
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003838 if (e == BRANCH)
3839 /* mark branch target for state pruning */
3840 env->explored_states[w] = STATE_LIST_MARK;
3841
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003842 if (insn_state[w] == 0) {
3843 /* tree-edge */
3844 insn_state[t] = DISCOVERED | e;
3845 insn_state[w] = DISCOVERED;
3846 if (cur_stack >= env->prog->len)
3847 return -E2BIG;
3848 insn_stack[cur_stack++] = w;
3849 return 1;
3850 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003851 verbose(env, "back-edge from insn %d to %d\n", t, w);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003852 return -EINVAL;
3853 } else if (insn_state[w] == EXPLORED) {
3854 /* forward- or cross-edge */
3855 insn_state[t] = DISCOVERED | e;
3856 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003857 verbose(env, "insn state internal bug\n");
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003858 return -EFAULT;
3859 }
3860 return 0;
3861}
3862
3863/* non-recursive depth-first-search to detect loops in BPF program
3864 * loop == back-edge in directed graph
3865 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003866static int check_cfg(struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003867{
3868 struct bpf_insn *insns = env->prog->insnsi;
3869 int insn_cnt = env->prog->len;
3870 int ret = 0;
3871 int i, t;
3872
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08003873 ret = check_subprogs(env);
3874 if (ret < 0)
3875 return ret;
3876
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003877 insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
3878 if (!insn_state)
3879 return -ENOMEM;
3880
3881 insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
3882 if (!insn_stack) {
3883 kfree(insn_state);
3884 return -ENOMEM;
3885 }
3886
3887 insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
3888 insn_stack[0] = 0; /* 0 is the first instruction */
3889 cur_stack = 1;
3890
3891peek_stack:
3892 if (cur_stack == 0)
3893 goto check_state;
3894 t = insn_stack[cur_stack - 1];
3895
3896 if (BPF_CLASS(insns[t].code) == BPF_JMP) {
3897 u8 opcode = BPF_OP(insns[t].code);
3898
3899 if (opcode == BPF_EXIT) {
3900 goto mark_explored;
3901 } else if (opcode == BPF_CALL) {
3902 ret = push_insn(t, t + 1, FALLTHROUGH, env);
3903 if (ret == 1)
3904 goto peek_stack;
3905 else if (ret < 0)
3906 goto err_free;
Daniel Borkmann07016152016-04-05 22:33:17 +02003907 if (t + 1 < insn_cnt)
3908 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08003909 if (insns[t].src_reg == BPF_PSEUDO_CALL) {
3910 env->explored_states[t] = STATE_LIST_MARK;
3911 ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env);
3912 if (ret == 1)
3913 goto peek_stack;
3914 else if (ret < 0)
3915 goto err_free;
3916 }
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003917 } else if (opcode == BPF_JA) {
3918 if (BPF_SRC(insns[t].code) != BPF_K) {
3919 ret = -EINVAL;
3920 goto err_free;
3921 }
3922 /* unconditional jump with single edge */
3923 ret = push_insn(t, t + insns[t].off + 1,
3924 FALLTHROUGH, env);
3925 if (ret == 1)
3926 goto peek_stack;
3927 else if (ret < 0)
3928 goto err_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003929 /* tell verifier to check for equivalent states
3930 * after every call and jump
3931 */
Alexei Starovoitovc3de6312015-04-14 15:57:13 -07003932 if (t + 1 < insn_cnt)
3933 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003934 } else {
3935 /* conditional jump with two edges */
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02003936 env->explored_states[t] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003937 ret = push_insn(t, t + 1, FALLTHROUGH, env);
3938 if (ret == 1)
3939 goto peek_stack;
3940 else if (ret < 0)
3941 goto err_free;
3942
3943 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
3944 if (ret == 1)
3945 goto peek_stack;
3946 else if (ret < 0)
3947 goto err_free;
3948 }
3949 } else {
3950 /* all other non-branch instructions with single
3951 * fall-through edge
3952 */
3953 ret = push_insn(t, t + 1, FALLTHROUGH, env);
3954 if (ret == 1)
3955 goto peek_stack;
3956 else if (ret < 0)
3957 goto err_free;
3958 }
3959
3960mark_explored:
3961 insn_state[t] = EXPLORED;
3962 if (cur_stack-- <= 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003963 verbose(env, "pop stack internal bug\n");
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003964 ret = -EFAULT;
3965 goto err_free;
3966 }
3967 goto peek_stack;
3968
3969check_state:
3970 for (i = 0; i < insn_cnt; i++) {
3971 if (insn_state[i] != EXPLORED) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003972 verbose(env, "unreachable insn %d\n", i);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003973 ret = -EINVAL;
3974 goto err_free;
3975 }
3976 }
3977 ret = 0; /* cfg looks good */
3978
3979err_free:
3980 kfree(insn_state);
3981 kfree(insn_stack);
3982 return ret;
3983}
3984
Edward Creef1174f72017-08-07 15:26:19 +01003985/* check %cur's range satisfies %old's */
3986static bool range_within(struct bpf_reg_state *old,
3987 struct bpf_reg_state *cur)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003988{
Edward Creeb03c9f92017-08-07 15:26:36 +01003989 return old->umin_value <= cur->umin_value &&
3990 old->umax_value >= cur->umax_value &&
3991 old->smin_value <= cur->smin_value &&
3992 old->smax_value >= cur->smax_value;
Edward Creef1174f72017-08-07 15:26:19 +01003993}
3994
3995/* Maximum number of register states that can exist at once */
3996#define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE)
3997struct idpair {
3998 u32 old;
3999 u32 cur;
4000};
4001
4002/* If in the old state two registers had the same id, then they need to have
4003 * the same id in the new state as well. But that id could be different from
4004 * the old state, so we need to track the mapping from old to new ids.
4005 * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent
4006 * regs with old id 5 must also have new id 9 for the new state to be safe. But
4007 * regs with a different old id could still have new id 9, we don't care about
4008 * that.
4009 * So we look through our idmap to see if this old id has been seen before. If
4010 * so, we require the new id to match; otherwise, we add the id pair to the map.
4011 */
4012static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap)
4013{
4014 unsigned int i;
4015
4016 for (i = 0; i < ID_MAP_SIZE; i++) {
4017 if (!idmap[i].old) {
4018 /* Reached an empty slot; haven't seen this id before */
4019 idmap[i].old = old_id;
4020 idmap[i].cur = cur_id;
4021 return true;
4022 }
4023 if (idmap[i].old == old_id)
4024 return idmap[i].cur == cur_id;
4025 }
4026 /* We ran out of idmap slots, which should be impossible */
4027 WARN_ON_ONCE(1);
4028 return false;
4029}
4030
4031/* Returns true if (rold safe implies rcur safe) */
Edward Cree1b688a12017-08-23 15:10:50 +01004032static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
4033 struct idpair *idmap)
Edward Creef1174f72017-08-07 15:26:19 +01004034{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004035 bool equal;
4036
Edward Creedc503a82017-08-15 20:34:35 +01004037 if (!(rold->live & REG_LIVE_READ))
4038 /* explored state didn't use this */
4039 return true;
4040
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004041 equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, frameno)) == 0;
4042
4043 if (rold->type == PTR_TO_STACK)
4044 /* two stack pointers are equal only if they're pointing to
4045 * the same stack frame, since fp-8 in foo != fp-8 in bar
4046 */
4047 return equal && rold->frameno == rcur->frameno;
4048
4049 if (equal)
Edward Creef1174f72017-08-07 15:26:19 +01004050 return true;
4051
4052 if (rold->type == NOT_INIT)
4053 /* explored state can't have used this */
4054 return true;
4055 if (rcur->type == NOT_INIT)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004056 return false;
Edward Creef1174f72017-08-07 15:26:19 +01004057 switch (rold->type) {
4058 case SCALAR_VALUE:
4059 if (rcur->type == SCALAR_VALUE) {
4060 /* new val must satisfy old val knowledge */
4061 return range_within(rold, rcur) &&
4062 tnum_in(rold->var_off, rcur->var_off);
4063 } else {
4064 /* if we knew anything about the old value, we're not
4065 * equal, because we can't know anything about the
4066 * scalar value of the pointer in the new value.
4067 */
Edward Creeb03c9f92017-08-07 15:26:36 +01004068 return rold->umin_value == 0 &&
4069 rold->umax_value == U64_MAX &&
4070 rold->smin_value == S64_MIN &&
4071 rold->smax_value == S64_MAX &&
Edward Creef1174f72017-08-07 15:26:19 +01004072 tnum_is_unknown(rold->var_off);
4073 }
4074 case PTR_TO_MAP_VALUE:
Edward Cree1b688a12017-08-23 15:10:50 +01004075 /* If the new min/max/var_off satisfy the old ones and
4076 * everything else matches, we are OK.
4077 * We don't care about the 'id' value, because nothing
4078 * uses it for PTR_TO_MAP_VALUE (only for ..._OR_NULL)
4079 */
4080 return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 &&
4081 range_within(rold, rcur) &&
4082 tnum_in(rold->var_off, rcur->var_off);
Edward Creef1174f72017-08-07 15:26:19 +01004083 case PTR_TO_MAP_VALUE_OR_NULL:
4084 /* a PTR_TO_MAP_VALUE could be safe to use as a
4085 * PTR_TO_MAP_VALUE_OR_NULL into the same map.
4086 * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL-
4087 * checked, doing so could have affected others with the same
4088 * id, and we can't check for that because we lost the id when
4089 * we converted to a PTR_TO_MAP_VALUE.
4090 */
4091 if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL)
4092 return false;
4093 if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)))
4094 return false;
4095 /* Check our ids match any regs they're supposed to */
4096 return check_ids(rold->id, rcur->id, idmap);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004097 case PTR_TO_PACKET_META:
Edward Creef1174f72017-08-07 15:26:19 +01004098 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004099 if (rcur->type != rold->type)
Edward Creef1174f72017-08-07 15:26:19 +01004100 return false;
4101 /* We must have at least as much range as the old ptr
4102 * did, so that any accesses which were safe before are
4103 * still safe. This is true even if old range < old off,
4104 * since someone could have accessed through (ptr - k), or
4105 * even done ptr -= k in a register, to get a safe access.
4106 */
4107 if (rold->range > rcur->range)
4108 return false;
4109 /* If the offsets don't match, we can't trust our alignment;
4110 * nor can we be sure that we won't fall out of range.
4111 */
4112 if (rold->off != rcur->off)
4113 return false;
4114 /* id relations must be preserved */
4115 if (rold->id && !check_ids(rold->id, rcur->id, idmap))
4116 return false;
4117 /* new val must satisfy old val knowledge */
4118 return range_within(rold, rcur) &&
4119 tnum_in(rold->var_off, rcur->var_off);
4120 case PTR_TO_CTX:
4121 case CONST_PTR_TO_MAP:
Edward Creef1174f72017-08-07 15:26:19 +01004122 case PTR_TO_PACKET_END:
4123 /* Only valid matches are exact, which memcmp() above
4124 * would have accepted
4125 */
4126 default:
4127 /* Don't know what's going on, just say it's not safe */
4128 return false;
4129 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004130
Edward Creef1174f72017-08-07 15:26:19 +01004131 /* Shouldn't get here; if we do, say it's not safe */
4132 WARN_ON_ONCE(1);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004133 return false;
4134}
4135
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004136static bool stacksafe(struct bpf_func_state *old,
4137 struct bpf_func_state *cur,
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004138 struct idpair *idmap)
4139{
4140 int i, spi;
4141
4142 /* if explored stack has more populated slots than current stack
4143 * such stacks are not equivalent
4144 */
4145 if (old->allocated_stack > cur->allocated_stack)
4146 return false;
4147
4148 /* walk slots of the explored stack and ignore any additional
4149 * slots in the current stack, since explored(safe) state
4150 * didn't use them
4151 */
4152 for (i = 0; i < old->allocated_stack; i++) {
4153 spi = i / BPF_REG_SIZE;
4154
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08004155 if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ))
4156 /* explored state didn't use this */
Gianluca Borellofd05e572017-12-23 10:09:55 +00004157 continue;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08004158
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004159 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID)
4160 continue;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08004161 /* if old state was safe with misc data in the stack
4162 * it will be safe with zero-initialized stack.
4163 * The opposite is not true
4164 */
4165 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC &&
4166 cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO)
4167 continue;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004168 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] !=
4169 cur->stack[spi].slot_type[i % BPF_REG_SIZE])
4170 /* Ex: old explored (safe) state has STACK_SPILL in
4171 * this stack slot, but current has has STACK_MISC ->
4172 * this verifier states are not equivalent,
4173 * return false to continue verification of this path
4174 */
4175 return false;
4176 if (i % BPF_REG_SIZE)
4177 continue;
4178 if (old->stack[spi].slot_type[0] != STACK_SPILL)
4179 continue;
4180 if (!regsafe(&old->stack[spi].spilled_ptr,
4181 &cur->stack[spi].spilled_ptr,
4182 idmap))
4183 /* when explored and current stack slot are both storing
4184 * spilled registers, check that stored pointers types
4185 * are the same as well.
4186 * Ex: explored safe path could have stored
4187 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8}
4188 * but current path has stored:
4189 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16}
4190 * such verifier states are not equivalent.
4191 * return false to continue verification of this path
4192 */
4193 return false;
4194 }
4195 return true;
4196}
4197
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004198/* compare two verifier states
4199 *
4200 * all states stored in state_list are known to be valid, since
4201 * verifier reached 'bpf_exit' instruction through them
4202 *
4203 * this function is called when verifier exploring different branches of
4204 * execution popped from the state stack. If it sees an old state that has
4205 * more strict register state and more strict stack state then this execution
4206 * branch doesn't need to be explored further, since verifier already
4207 * concluded that more strict state leads to valid finish.
4208 *
4209 * Therefore two states are equivalent if register state is more conservative
4210 * and explored stack state is more conservative than the current one.
4211 * Example:
4212 * explored current
4213 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
4214 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
4215 *
4216 * In other words if current stack state (one being explored) has more
4217 * valid slots than old one that already passed validation, it means
4218 * the verifier can stop exploring and conclude that current state is valid too
4219 *
4220 * Similarly with registers. If explored state has register type as invalid
4221 * whereas register type in current state is meaningful, it means that
4222 * the current state will reach 'bpf_exit' instruction safely
4223 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004224static bool func_states_equal(struct bpf_func_state *old,
4225 struct bpf_func_state *cur)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004226{
Edward Creef1174f72017-08-07 15:26:19 +01004227 struct idpair *idmap;
4228 bool ret = false;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004229 int i;
4230
Edward Creef1174f72017-08-07 15:26:19 +01004231 idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL);
4232 /* If we failed to allocate the idmap, just say it's not safe */
4233 if (!idmap)
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07004234 return false;
Edward Creef1174f72017-08-07 15:26:19 +01004235
4236 for (i = 0; i < MAX_BPF_REG; i++) {
Edward Cree1b688a12017-08-23 15:10:50 +01004237 if (!regsafe(&old->regs[i], &cur->regs[i], idmap))
Edward Creef1174f72017-08-07 15:26:19 +01004238 goto out_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004239 }
4240
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004241 if (!stacksafe(old, cur, idmap))
4242 goto out_free;
Edward Creef1174f72017-08-07 15:26:19 +01004243 ret = true;
4244out_free:
4245 kfree(idmap);
4246 return ret;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004247}
4248
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004249static bool states_equal(struct bpf_verifier_env *env,
4250 struct bpf_verifier_state *old,
4251 struct bpf_verifier_state *cur)
Edward Creedc503a82017-08-15 20:34:35 +01004252{
Edward Creedc503a82017-08-15 20:34:35 +01004253 int i;
4254
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004255 if (old->curframe != cur->curframe)
4256 return false;
4257
4258 /* for states to be equal callsites have to be the same
4259 * and all frame states need to be equivalent
4260 */
4261 for (i = 0; i <= old->curframe; i++) {
4262 if (old->frame[i]->callsite != cur->frame[i]->callsite)
4263 return false;
4264 if (!func_states_equal(old->frame[i], cur->frame[i]))
4265 return false;
4266 }
4267 return true;
4268}
4269
4270/* A write screens off any subsequent reads; but write marks come from the
4271 * straight-line code between a state and its parent. When we arrive at an
4272 * equivalent state (jump target or such) we didn't arrive by the straight-line
4273 * code, so read marks in the state must propagate to the parent regardless
4274 * of the state's write marks. That's what 'parent == state->parent' comparison
4275 * in mark_reg_read() and mark_stack_slot_read() is for.
4276 */
4277static int propagate_liveness(struct bpf_verifier_env *env,
4278 const struct bpf_verifier_state *vstate,
4279 struct bpf_verifier_state *vparent)
4280{
4281 int i, frame, err = 0;
4282 struct bpf_func_state *state, *parent;
4283
4284 if (vparent->curframe != vstate->curframe) {
4285 WARN(1, "propagate_live: parent frame %d current frame %d\n",
4286 vparent->curframe, vstate->curframe);
4287 return -EFAULT;
4288 }
Edward Creedc503a82017-08-15 20:34:35 +01004289 /* Propagate read liveness of registers... */
4290 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
4291 /* We don't need to worry about FP liveness because it's read-only */
4292 for (i = 0; i < BPF_REG_FP; i++) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004293 if (vparent->frame[vparent->curframe]->regs[i].live & REG_LIVE_READ)
Edward Creedc503a82017-08-15 20:34:35 +01004294 continue;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004295 if (vstate->frame[vstate->curframe]->regs[i].live & REG_LIVE_READ) {
4296 err = mark_reg_read(env, vstate, vparent, i);
4297 if (err)
4298 return err;
Edward Creedc503a82017-08-15 20:34:35 +01004299 }
4300 }
Edward Creedc503a82017-08-15 20:34:35 +01004301
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004302 /* ... and stack slots */
4303 for (frame = 0; frame <= vstate->curframe; frame++) {
4304 state = vstate->frame[frame];
4305 parent = vparent->frame[frame];
4306 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE &&
4307 i < parent->allocated_stack / BPF_REG_SIZE; i++) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004308 if (parent->stack[i].spilled_ptr.live & REG_LIVE_READ)
4309 continue;
4310 if (state->stack[i].spilled_ptr.live & REG_LIVE_READ)
4311 mark_stack_slot_read(env, vstate, vparent, i, frame);
4312 }
Edward Creedc503a82017-08-15 20:34:35 +01004313 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004314 return err;
Edward Creedc503a82017-08-15 20:34:35 +01004315}
4316
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004317static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004318{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004319 struct bpf_verifier_state_list *new_sl;
4320 struct bpf_verifier_state_list *sl;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004321 struct bpf_verifier_state *cur = env->cur_state;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004322 int i, j, err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004323
4324 sl = env->explored_states[insn_idx];
4325 if (!sl)
4326 /* this 'insn_idx' instruction wasn't marked, so we will not
4327 * be doing state search here
4328 */
4329 return 0;
4330
4331 while (sl != STATE_LIST_MARK) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004332 if (states_equal(env, &sl->state, cur)) {
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004333 /* reached equivalent register/stack state,
Edward Creedc503a82017-08-15 20:34:35 +01004334 * prune the search.
4335 * Registers read by the continuation are read by us.
Edward Cree8e9cd9c2017-08-23 15:11:21 +01004336 * If we have any write marks in env->cur_state, they
4337 * will prevent corresponding reads in the continuation
4338 * from reaching our parent (an explored_state). Our
4339 * own state will get the read marks recorded, but
4340 * they'll be immediately forgotten as we're pruning
4341 * this state and will pop a new one.
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004342 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004343 err = propagate_liveness(env, &sl->state, cur);
4344 if (err)
4345 return err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004346 return 1;
Edward Creedc503a82017-08-15 20:34:35 +01004347 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004348 sl = sl->next;
4349 }
4350
4351 /* there were no equivalent states, remember current one.
4352 * technically the current state is not proven to be safe yet,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004353 * but it will either reach outer most bpf_exit (which means it's safe)
4354 * or it will be rejected. Since there are no loops, we won't be
4355 * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx)
4356 * again on the way to bpf_exit
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004357 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004358 new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004359 if (!new_sl)
4360 return -ENOMEM;
4361
4362 /* add new state to the head of linked list */
Alexei Starovoitov1969db42017-11-01 00:08:04 -07004363 err = copy_verifier_state(&new_sl->state, cur);
4364 if (err) {
4365 free_verifier_state(&new_sl->state, false);
4366 kfree(new_sl);
4367 return err;
4368 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004369 new_sl->next = env->explored_states[insn_idx];
4370 env->explored_states[insn_idx] = new_sl;
Edward Creedc503a82017-08-15 20:34:35 +01004371 /* connect new state to parentage chain */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004372 cur->parent = &new_sl->state;
Edward Cree8e9cd9c2017-08-23 15:11:21 +01004373 /* clear write marks in current state: the writes we did are not writes
4374 * our child did, so they don't screen off its reads from us.
4375 * (There are no read marks in current state, because reads always mark
4376 * their parent and current state never has children yet. Only
4377 * explored_states can get read marks.)
4378 */
Edward Creedc503a82017-08-15 20:34:35 +01004379 for (i = 0; i < BPF_REG_FP; i++)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004380 cur->frame[cur->curframe]->regs[i].live = REG_LIVE_NONE;
4381
4382 /* all stack frames are accessible from callee, clear them all */
4383 for (j = 0; j <= cur->curframe; j++) {
4384 struct bpf_func_state *frame = cur->frame[j];
4385
4386 for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++)
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08004387 frame->stack[i].spilled_ptr.live = REG_LIVE_NONE;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004388 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004389 return 0;
4390}
4391
Jakub Kicinski13a27df2016-09-21 11:43:58 +01004392static int ext_analyzer_insn_hook(struct bpf_verifier_env *env,
4393 int insn_idx, int prev_insn_idx)
4394{
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07004395 if (env->dev_ops && env->dev_ops->insn_hook)
4396 return env->dev_ops->insn_hook(env, insn_idx, prev_insn_idx);
Jakub Kicinski13a27df2016-09-21 11:43:58 +01004397
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07004398 return 0;
Jakub Kicinski13a27df2016-09-21 11:43:58 +01004399}
4400
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004401static int do_check(struct bpf_verifier_env *env)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004402{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004403 struct bpf_verifier_state *state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004404 struct bpf_insn *insns = env->prog->insnsi;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004405 struct bpf_reg_state *regs;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004406 int insn_cnt = env->prog->len, i;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004407 int insn_idx, prev_insn_idx = 0;
4408 int insn_processed = 0;
4409 bool do_print_state = false;
4410
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004411 state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL);
4412 if (!state)
4413 return -ENOMEM;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004414 state->curframe = 0;
Edward Creedc503a82017-08-15 20:34:35 +01004415 state->parent = NULL;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004416 state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL);
4417 if (!state->frame[0]) {
4418 kfree(state);
4419 return -ENOMEM;
4420 }
4421 env->cur_state = state;
4422 init_func_state(env, state->frame[0],
4423 BPF_MAIN_FUNC /* callsite */,
4424 0 /* frameno */,
4425 0 /* subprogno, zero == main subprog */);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004426 insn_idx = 0;
4427 for (;;) {
4428 struct bpf_insn *insn;
4429 u8 class;
4430 int err;
4431
4432 if (insn_idx >= insn_cnt) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004433 verbose(env, "invalid insn idx %d insn_cnt %d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004434 insn_idx, insn_cnt);
4435 return -EFAULT;
4436 }
4437
4438 insn = &insns[insn_idx];
4439 class = BPF_CLASS(insn->code);
4440
Daniel Borkmann07016152016-04-05 22:33:17 +02004441 if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004442 verbose(env,
4443 "BPF program is too large. Processed %d insn\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004444 insn_processed);
4445 return -E2BIG;
4446 }
4447
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004448 err = is_state_visited(env, insn_idx);
4449 if (err < 0)
4450 return err;
4451 if (err == 1) {
4452 /* found equivalent state, can prune the search */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004453 if (env->log.level) {
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004454 if (do_print_state)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004455 verbose(env, "\nfrom %d to %d: safe\n",
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004456 prev_insn_idx, insn_idx);
4457 else
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004458 verbose(env, "%d: safe\n", insn_idx);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004459 }
4460 goto process_bpf_exit;
4461 }
4462
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02004463 if (need_resched())
4464 cond_resched();
4465
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004466 if (env->log.level > 1 || (env->log.level && do_print_state)) {
4467 if (env->log.level > 1)
4468 verbose(env, "%d:", insn_idx);
David S. Millerc5fc9692017-05-10 11:25:17 -07004469 else
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004470 verbose(env, "\nfrom %d to %d:",
David S. Millerc5fc9692017-05-10 11:25:17 -07004471 prev_insn_idx, insn_idx);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004472 print_verifier_state(env, state->frame[state->curframe]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004473 do_print_state = false;
4474 }
4475
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004476 if (env->log.level) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01004477 const struct bpf_insn_cbs cbs = {
4478 .cb_print = verbose,
4479 };
4480
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004481 verbose(env, "%d: ", insn_idx);
Daniel Borkmann7105e822017-12-20 13:42:57 +01004482 print_bpf_insn(&cbs, env, insn, env->allow_ptr_leaks);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004483 }
4484
Jakub Kicinski13a27df2016-09-21 11:43:58 +01004485 err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx);
4486 if (err)
4487 return err;
4488
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004489 regs = cur_regs(env);
Alexei Starovoitovc1311872017-11-22 16:42:05 -08004490 env->insn_aux_data[insn_idx].seen = true;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004491 if (class == BPF_ALU || class == BPF_ALU64) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004492 err = check_alu_op(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004493 if (err)
4494 return err;
4495
4496 } else if (class == BPF_LDX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004497 enum bpf_reg_type *prev_src_type, src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004498
4499 /* check for reserved fields is already done */
4500
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004501 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01004502 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004503 if (err)
4504 return err;
4505
Edward Creedc503a82017-08-15 20:34:35 +01004506 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004507 if (err)
4508 return err;
4509
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07004510 src_reg_type = regs[insn->src_reg].type;
4511
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004512 /* check that memory (src_reg + off) is readable,
4513 * the state of dst_reg will be updated by this func
4514 */
Yonghong Song31fd8582017-06-13 15:52:13 -07004515 err = check_mem_access(env, insn_idx, insn->src_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004516 BPF_SIZE(insn->code), BPF_READ,
4517 insn->dst_reg);
4518 if (err)
4519 return err;
4520
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004521 prev_src_type = &env->insn_aux_data[insn_idx].ptr_type;
4522
4523 if (*prev_src_type == NOT_INIT) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004524 /* saw a valid insn
4525 * dst_reg = *(u32 *)(src_reg + off)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004526 * save type to validate intersecting paths
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004527 */
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004528 *prev_src_type = src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004529
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004530 } else if (src_reg_type != *prev_src_type &&
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004531 (src_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004532 *prev_src_type == PTR_TO_CTX)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004533 /* ABuser program is trying to use the same insn
4534 * dst_reg = *(u32*) (src_reg + off)
4535 * with different pointer types:
4536 * src_reg == ctx in one branch and
4537 * src_reg == stack|map in some other branch.
4538 * Reject it.
4539 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004540 verbose(env, "same insn cannot be used with different pointers\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004541 return -EINVAL;
4542 }
4543
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004544 } else if (class == BPF_STX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004545 enum bpf_reg_type *prev_dst_type, dst_reg_type;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004546
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004547 if (BPF_MODE(insn->code) == BPF_XADD) {
Yonghong Song31fd8582017-06-13 15:52:13 -07004548 err = check_xadd(env, insn_idx, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004549 if (err)
4550 return err;
4551 insn_idx++;
4552 continue;
4553 }
4554
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004555 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01004556 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004557 if (err)
4558 return err;
4559 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01004560 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004561 if (err)
4562 return err;
4563
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004564 dst_reg_type = regs[insn->dst_reg].type;
4565
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004566 /* check that memory (dst_reg + off) is writeable */
Yonghong Song31fd8582017-06-13 15:52:13 -07004567 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004568 BPF_SIZE(insn->code), BPF_WRITE,
4569 insn->src_reg);
4570 if (err)
4571 return err;
4572
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004573 prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type;
4574
4575 if (*prev_dst_type == NOT_INIT) {
4576 *prev_dst_type = dst_reg_type;
4577 } else if (dst_reg_type != *prev_dst_type &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004578 (dst_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004579 *prev_dst_type == PTR_TO_CTX)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004580 verbose(env, "same insn cannot be used with different pointers\n");
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004581 return -EINVAL;
4582 }
4583
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004584 } else if (class == BPF_ST) {
4585 if (BPF_MODE(insn->code) != BPF_MEM ||
4586 insn->src_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004587 verbose(env, "BPF_ST uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004588 return -EINVAL;
4589 }
4590 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01004591 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004592 if (err)
4593 return err;
4594
4595 /* check that memory (dst_reg + off) is writeable */
Yonghong Song31fd8582017-06-13 15:52:13 -07004596 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004597 BPF_SIZE(insn->code), BPF_WRITE,
4598 -1);
4599 if (err)
4600 return err;
4601
4602 } else if (class == BPF_JMP) {
4603 u8 opcode = BPF_OP(insn->code);
4604
4605 if (opcode == BPF_CALL) {
4606 if (BPF_SRC(insn->code) != BPF_K ||
4607 insn->off != 0 ||
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004608 (insn->src_reg != BPF_REG_0 &&
4609 insn->src_reg != BPF_PSEUDO_CALL) ||
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004610 insn->dst_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004611 verbose(env, "BPF_CALL uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004612 return -EINVAL;
4613 }
4614
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004615 if (insn->src_reg == BPF_PSEUDO_CALL)
4616 err = check_func_call(env, insn, &insn_idx);
4617 else
4618 err = check_helper_call(env, insn->imm, insn_idx);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004619 if (err)
4620 return err;
4621
4622 } else if (opcode == BPF_JA) {
4623 if (BPF_SRC(insn->code) != BPF_K ||
4624 insn->imm != 0 ||
4625 insn->src_reg != BPF_REG_0 ||
4626 insn->dst_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004627 verbose(env, "BPF_JA uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004628 return -EINVAL;
4629 }
4630
4631 insn_idx += insn->off + 1;
4632 continue;
4633
4634 } else if (opcode == BPF_EXIT) {
4635 if (BPF_SRC(insn->code) != BPF_K ||
4636 insn->imm != 0 ||
4637 insn->src_reg != BPF_REG_0 ||
4638 insn->dst_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004639 verbose(env, "BPF_EXIT uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004640 return -EINVAL;
4641 }
4642
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004643 if (state->curframe) {
4644 /* exit from nested function */
4645 prev_insn_idx = insn_idx;
4646 err = prepare_func_exit(env, &insn_idx);
4647 if (err)
4648 return err;
4649 do_print_state = true;
4650 continue;
4651 }
4652
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004653 /* eBPF calling convetion is such that R0 is used
4654 * to return the value from eBPF program.
4655 * Make sure that it's readable at this time
4656 * of bpf_exit, which means that program wrote
4657 * something into it earlier
4658 */
Edward Creedc503a82017-08-15 20:34:35 +01004659 err = check_reg_arg(env, BPF_REG_0, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004660 if (err)
4661 return err;
4662
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004663 if (is_pointer_value(env, BPF_REG_0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004664 verbose(env, "R0 leaks addr as return value\n");
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004665 return -EACCES;
4666 }
4667
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07004668 err = check_return_code(env);
4669 if (err)
4670 return err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004671process_bpf_exit:
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004672 err = pop_stack(env, &prev_insn_idx, &insn_idx);
4673 if (err < 0) {
4674 if (err != -ENOENT)
4675 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004676 break;
4677 } else {
4678 do_print_state = true;
4679 continue;
4680 }
4681 } else {
4682 err = check_cond_jmp_op(env, insn, &insn_idx);
4683 if (err)
4684 return err;
4685 }
4686 } else if (class == BPF_LD) {
4687 u8 mode = BPF_MODE(insn->code);
4688
4689 if (mode == BPF_ABS || mode == BPF_IND) {
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004690 err = check_ld_abs(env, insn);
4691 if (err)
4692 return err;
4693
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004694 } else if (mode == BPF_IMM) {
4695 err = check_ld_imm(env, insn);
4696 if (err)
4697 return err;
4698
4699 insn_idx++;
Alexei Starovoitovc1311872017-11-22 16:42:05 -08004700 env->insn_aux_data[insn_idx].seen = true;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004701 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004702 verbose(env, "invalid BPF_LD mode\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004703 return -EINVAL;
4704 }
4705 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004706 verbose(env, "unknown insn class %d\n", class);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004707 return -EINVAL;
4708 }
4709
4710 insn_idx++;
4711 }
4712
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004713 verbose(env, "processed %d insns, stack depth ", insn_processed);
4714 for (i = 0; i < env->subprog_cnt + 1; i++) {
4715 u32 depth = env->subprog_stack_depth[i];
4716
4717 verbose(env, "%d", depth);
4718 if (i + 1 < env->subprog_cnt + 1)
4719 verbose(env, "+");
4720 }
4721 verbose(env, "\n");
4722 env->prog->aux->stack_depth = env->subprog_stack_depth[0];
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004723 return 0;
4724}
4725
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004726static int check_map_prealloc(struct bpf_map *map)
4727{
4728 return (map->map_type != BPF_MAP_TYPE_HASH &&
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07004729 map->map_type != BPF_MAP_TYPE_PERCPU_HASH &&
4730 map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) ||
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004731 !(map->map_flags & BPF_F_NO_PREALLOC);
4732}
4733
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004734static int check_map_prog_compatibility(struct bpf_verifier_env *env,
4735 struct bpf_map *map,
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07004736 struct bpf_prog *prog)
4737
4738{
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004739 /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use
4740 * preallocated hash maps, since doing memory allocation
4741 * in overflow_handler can crash depending on where nmi got
4742 * triggered.
4743 */
4744 if (prog->type == BPF_PROG_TYPE_PERF_EVENT) {
4745 if (!check_map_prealloc(map)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004746 verbose(env, "perf_event programs can only use preallocated hash map\n");
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004747 return -EINVAL;
4748 }
4749 if (map->inner_map_meta &&
4750 !check_map_prealloc(map->inner_map_meta)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004751 verbose(env, "perf_event programs can only use preallocated inner hash map\n");
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004752 return -EINVAL;
4753 }
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07004754 }
4755 return 0;
4756}
4757
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004758/* look for pseudo eBPF instructions that access map FDs and
4759 * replace them with actual map pointers
4760 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004761static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004762{
4763 struct bpf_insn *insn = env->prog->insnsi;
4764 int insn_cnt = env->prog->len;
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07004765 int i, j, err;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004766
Daniel Borkmannf1f77142017-01-13 23:38:15 +01004767 err = bpf_prog_calc_tag(env->prog);
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01004768 if (err)
4769 return err;
4770
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004771 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004772 if (BPF_CLASS(insn->code) == BPF_LDX &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004773 (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004774 verbose(env, "BPF_LDX uses reserved fields\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004775 return -EINVAL;
4776 }
4777
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004778 if (BPF_CLASS(insn->code) == BPF_STX &&
4779 ((BPF_MODE(insn->code) != BPF_MEM &&
4780 BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004781 verbose(env, "BPF_STX uses reserved fields\n");
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004782 return -EINVAL;
4783 }
4784
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004785 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
4786 struct bpf_map *map;
4787 struct fd f;
4788
4789 if (i == insn_cnt - 1 || insn[1].code != 0 ||
4790 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
4791 insn[1].off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004792 verbose(env, "invalid bpf_ld_imm64 insn\n");
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004793 return -EINVAL;
4794 }
4795
4796 if (insn->src_reg == 0)
4797 /* valid generic load 64-bit imm */
4798 goto next_insn;
4799
4800 if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004801 verbose(env,
4802 "unrecognized bpf_ld_imm64 insn\n");
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004803 return -EINVAL;
4804 }
4805
4806 f = fdget(insn->imm);
Daniel Borkmannc2101292015-10-29 14:58:07 +01004807 map = __bpf_map_get(f);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004808 if (IS_ERR(map)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004809 verbose(env, "fd %d is not pointing to valid bpf_map\n",
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004810 insn->imm);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004811 return PTR_ERR(map);
4812 }
4813
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004814 err = check_map_prog_compatibility(env, map, env->prog);
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07004815 if (err) {
4816 fdput(f);
4817 return err;
4818 }
4819
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004820 /* store map pointer inside BPF_LD_IMM64 instruction */
4821 insn[0].imm = (u32) (unsigned long) map;
4822 insn[1].imm = ((u64) (unsigned long) map) >> 32;
4823
4824 /* check whether we recorded this map already */
4825 for (j = 0; j < env->used_map_cnt; j++)
4826 if (env->used_maps[j] == map) {
4827 fdput(f);
4828 goto next_insn;
4829 }
4830
4831 if (env->used_map_cnt >= MAX_USED_MAPS) {
4832 fdput(f);
4833 return -E2BIG;
4834 }
4835
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004836 /* hold the map. If the program is rejected by verifier,
4837 * the map will be released by release_maps() or it
4838 * will be used by the valid program until it's unloaded
4839 * and all maps are released in free_bpf_prog_info()
4840 */
Alexei Starovoitov92117d82016-04-27 18:56:20 -07004841 map = bpf_map_inc(map, false);
4842 if (IS_ERR(map)) {
4843 fdput(f);
4844 return PTR_ERR(map);
4845 }
4846 env->used_maps[env->used_map_cnt++] = map;
4847
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004848 fdput(f);
4849next_insn:
4850 insn++;
4851 i++;
4852 }
4853 }
4854
4855 /* now all pseudo BPF_LD_IMM64 instructions load valid
4856 * 'struct bpf_map *' into a register instead of user map_fd.
4857 * These pointers will be used later by verifier to validate map access.
4858 */
4859 return 0;
4860}
4861
4862/* drop refcnt of maps used by the rejected program */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004863static void release_maps(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004864{
4865 int i;
4866
4867 for (i = 0; i < env->used_map_cnt; i++)
4868 bpf_map_put(env->used_maps[i]);
4869}
4870
4871/* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004872static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004873{
4874 struct bpf_insn *insn = env->prog->insnsi;
4875 int insn_cnt = env->prog->len;
4876 int i;
4877
4878 for (i = 0; i < insn_cnt; i++, insn++)
4879 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
4880 insn->src_reg = 0;
4881}
4882
Alexei Starovoitov80419022017-03-15 18:26:41 -07004883/* single env->prog->insni[off] instruction was replaced with the range
4884 * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
4885 * [0, off) and [off, end) to new locations, so the patched range stays zero
4886 */
4887static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len,
4888 u32 off, u32 cnt)
4889{
4890 struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data;
Alexei Starovoitovc1311872017-11-22 16:42:05 -08004891 int i;
Alexei Starovoitov80419022017-03-15 18:26:41 -07004892
4893 if (cnt == 1)
4894 return 0;
4895 new_data = vzalloc(sizeof(struct bpf_insn_aux_data) * prog_len);
4896 if (!new_data)
4897 return -ENOMEM;
4898 memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off);
4899 memcpy(new_data + off + cnt - 1, old_data + off,
4900 sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
Alexei Starovoitovc1311872017-11-22 16:42:05 -08004901 for (i = off; i < off + cnt - 1; i++)
4902 new_data[i].seen = true;
Alexei Starovoitov80419022017-03-15 18:26:41 -07004903 env->insn_aux_data = new_data;
4904 vfree(old_data);
4905 return 0;
4906}
4907
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08004908static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len)
4909{
4910 int i;
4911
4912 if (len == 1)
4913 return;
4914 for (i = 0; i < env->subprog_cnt; i++) {
4915 if (env->subprog_starts[i] < off)
4916 continue;
4917 env->subprog_starts[i] += len - 1;
4918 }
4919}
4920
Alexei Starovoitov80419022017-03-15 18:26:41 -07004921static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
4922 const struct bpf_insn *patch, u32 len)
4923{
4924 struct bpf_prog *new_prog;
4925
4926 new_prog = bpf_patch_insn_single(env->prog, off, patch, len);
4927 if (!new_prog)
4928 return NULL;
4929 if (adjust_insn_aux_data(env, new_prog->len, off, len))
4930 return NULL;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08004931 adjust_subprog_starts(env, off, len);
Alexei Starovoitov80419022017-03-15 18:26:41 -07004932 return new_prog;
4933}
4934
Alexei Starovoitovc1311872017-11-22 16:42:05 -08004935/* The verifier does more data flow analysis than llvm and will not explore
4936 * branches that are dead at run time. Malicious programs can have dead code
4937 * too. Therefore replace all dead at-run-time code with nops.
4938 */
4939static void sanitize_dead_code(struct bpf_verifier_env *env)
4940{
4941 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
4942 struct bpf_insn nop = BPF_MOV64_REG(BPF_REG_0, BPF_REG_0);
4943 struct bpf_insn *insn = env->prog->insnsi;
4944 const int insn_cnt = env->prog->len;
4945 int i;
4946
4947 for (i = 0; i < insn_cnt; i++) {
4948 if (aux_data[i].seen)
4949 continue;
4950 memcpy(insn + i, &nop, sizeof(nop));
4951 }
4952}
4953
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004954/* convert load instructions that access fields of 'struct __sk_buff'
4955 * into sequence of instructions that access fields of 'struct sk_buff'
4956 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004957static int convert_ctx_accesses(struct bpf_verifier_env *env)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004958{
Jakub Kicinski00176a32017-10-16 16:40:54 -07004959 const struct bpf_verifier_ops *ops = env->ops;
Daniel Borkmannf96da092017-07-02 02:13:27 +02004960 int i, cnt, size, ctx_field_size, delta = 0;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004961 const int insn_cnt = env->prog->len;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004962 struct bpf_insn insn_buf[16], *insn;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004963 struct bpf_prog *new_prog;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004964 enum bpf_access_type type;
Daniel Borkmannf96da092017-07-02 02:13:27 +02004965 bool is_narrower_load;
4966 u32 target_size;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004967
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004968 if (ops->gen_prologue) {
4969 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
4970 env->prog);
4971 if (cnt >= ARRAY_SIZE(insn_buf)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004972 verbose(env, "bpf verifier is misconfigured\n");
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004973 return -EINVAL;
4974 } else if (cnt) {
Alexei Starovoitov80419022017-03-15 18:26:41 -07004975 new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004976 if (!new_prog)
4977 return -ENOMEM;
Alexei Starovoitov80419022017-03-15 18:26:41 -07004978
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004979 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004980 delta += cnt - 1;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004981 }
4982 }
4983
4984 if (!ops->convert_ctx_access)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004985 return 0;
4986
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004987 insn = env->prog->insnsi + delta;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004988
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004989 for (i = 0; i < insn_cnt; i++, insn++) {
Daniel Borkmann62c79892017-01-12 11:51:33 +01004990 if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) ||
4991 insn->code == (BPF_LDX | BPF_MEM | BPF_H) ||
4992 insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07004993 insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004994 type = BPF_READ;
Daniel Borkmann62c79892017-01-12 11:51:33 +01004995 else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) ||
4996 insn->code == (BPF_STX | BPF_MEM | BPF_H) ||
4997 insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07004998 insn->code == (BPF_STX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004999 type = BPF_WRITE;
5000 else
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005001 continue;
5002
Alexei Starovoitov80419022017-03-15 18:26:41 -07005003 if (env->insn_aux_data[i + delta].ptr_type != PTR_TO_CTX)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005004 continue;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005005
Yonghong Song31fd8582017-06-13 15:52:13 -07005006 ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size;
Daniel Borkmannf96da092017-07-02 02:13:27 +02005007 size = BPF_LDST_BYTES(insn);
Yonghong Song31fd8582017-06-13 15:52:13 -07005008
5009 /* If the read access is a narrower load of the field,
5010 * convert to a 4/8-byte load, to minimum program type specific
5011 * convert_ctx_access changes. If conversion is successful,
5012 * we will apply proper mask to the result.
5013 */
Daniel Borkmannf96da092017-07-02 02:13:27 +02005014 is_narrower_load = size < ctx_field_size;
Yonghong Song31fd8582017-06-13 15:52:13 -07005015 if (is_narrower_load) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02005016 u32 off = insn->off;
5017 u8 size_code;
Yonghong Song31fd8582017-06-13 15:52:13 -07005018
Daniel Borkmannf96da092017-07-02 02:13:27 +02005019 if (type == BPF_WRITE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005020 verbose(env, "bpf verifier narrow ctx access misconfigured\n");
Daniel Borkmannf96da092017-07-02 02:13:27 +02005021 return -EINVAL;
5022 }
5023
5024 size_code = BPF_H;
Yonghong Song31fd8582017-06-13 15:52:13 -07005025 if (ctx_field_size == 4)
5026 size_code = BPF_W;
5027 else if (ctx_field_size == 8)
5028 size_code = BPF_DW;
Daniel Borkmannf96da092017-07-02 02:13:27 +02005029
Yonghong Song31fd8582017-06-13 15:52:13 -07005030 insn->off = off & ~(ctx_field_size - 1);
5031 insn->code = BPF_LDX | BPF_MEM | size_code;
5032 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02005033
5034 target_size = 0;
5035 cnt = ops->convert_ctx_access(type, insn, insn_buf, env->prog,
5036 &target_size);
5037 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) ||
5038 (ctx_field_size && !target_size)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005039 verbose(env, "bpf verifier is misconfigured\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005040 return -EINVAL;
5041 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02005042
5043 if (is_narrower_load && size < target_size) {
Yonghong Song31fd8582017-06-13 15:52:13 -07005044 if (ctx_field_size <= 4)
5045 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02005046 (1 << size * 8) - 1);
Yonghong Song31fd8582017-06-13 15:52:13 -07005047 else
5048 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02005049 (1 << size * 8) - 1);
Yonghong Song31fd8582017-06-13 15:52:13 -07005050 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005051
Alexei Starovoitov80419022017-03-15 18:26:41 -07005052 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005053 if (!new_prog)
5054 return -ENOMEM;
5055
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005056 delta += cnt - 1;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005057
5058 /* keep walking new program and skip insns we just inserted */
5059 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005060 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005061 }
5062
5063 return 0;
5064}
5065
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005066static int jit_subprogs(struct bpf_verifier_env *env)
5067{
5068 struct bpf_prog *prog = env->prog, **func, *tmp;
5069 int i, j, subprog_start, subprog_end = 0, len, subprog;
Daniel Borkmann7105e822017-12-20 13:42:57 +01005070 struct bpf_insn *insn;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005071 void *old_bpf_func;
5072 int err = -ENOMEM;
5073
5074 if (env->subprog_cnt == 0)
5075 return 0;
5076
Daniel Borkmann7105e822017-12-20 13:42:57 +01005077 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005078 if (insn->code != (BPF_JMP | BPF_CALL) ||
5079 insn->src_reg != BPF_PSEUDO_CALL)
5080 continue;
5081 subprog = find_subprog(env, i + insn->imm + 1);
5082 if (subprog < 0) {
5083 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
5084 i + insn->imm + 1);
5085 return -EFAULT;
5086 }
5087 /* temporarily remember subprog id inside insn instead of
5088 * aux_data, since next loop will split up all insns into funcs
5089 */
5090 insn->off = subprog + 1;
5091 /* remember original imm in case JIT fails and fallback
5092 * to interpreter will be needed
5093 */
5094 env->insn_aux_data[i].call_imm = insn->imm;
5095 /* point imm to __bpf_call_base+1 from JITs point of view */
5096 insn->imm = 1;
5097 }
5098
5099 func = kzalloc(sizeof(prog) * (env->subprog_cnt + 1), GFP_KERNEL);
5100 if (!func)
5101 return -ENOMEM;
5102
5103 for (i = 0; i <= env->subprog_cnt; i++) {
5104 subprog_start = subprog_end;
5105 if (env->subprog_cnt == i)
5106 subprog_end = prog->len;
5107 else
5108 subprog_end = env->subprog_starts[i];
5109
5110 len = subprog_end - subprog_start;
5111 func[i] = bpf_prog_alloc(bpf_prog_size(len), GFP_USER);
5112 if (!func[i])
5113 goto out_free;
5114 memcpy(func[i]->insnsi, &prog->insnsi[subprog_start],
5115 len * sizeof(struct bpf_insn));
Daniel Borkmann4f74d802017-12-20 13:42:56 +01005116 func[i]->type = prog->type;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005117 func[i]->len = len;
Daniel Borkmann4f74d802017-12-20 13:42:56 +01005118 if (bpf_prog_calc_tag(func[i]))
5119 goto out_free;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005120 func[i]->is_func = 1;
5121 /* Use bpf_prog_F_tag to indicate functions in stack traces.
5122 * Long term would need debug info to populate names
5123 */
5124 func[i]->aux->name[0] = 'F';
5125 func[i]->aux->stack_depth = env->subprog_stack_depth[i];
5126 func[i]->jit_requested = 1;
5127 func[i] = bpf_int_jit_compile(func[i]);
5128 if (!func[i]->jited) {
5129 err = -ENOTSUPP;
5130 goto out_free;
5131 }
5132 cond_resched();
5133 }
5134 /* at this point all bpf functions were successfully JITed
5135 * now populate all bpf_calls with correct addresses and
5136 * run last pass of JIT
5137 */
5138 for (i = 0; i <= env->subprog_cnt; i++) {
5139 insn = func[i]->insnsi;
5140 for (j = 0; j < func[i]->len; j++, insn++) {
5141 if (insn->code != (BPF_JMP | BPF_CALL) ||
5142 insn->src_reg != BPF_PSEUDO_CALL)
5143 continue;
5144 subprog = insn->off;
5145 insn->off = 0;
5146 insn->imm = (u64 (*)(u64, u64, u64, u64, u64))
5147 func[subprog]->bpf_func -
5148 __bpf_call_base;
5149 }
5150 }
5151 for (i = 0; i <= env->subprog_cnt; i++) {
5152 old_bpf_func = func[i]->bpf_func;
5153 tmp = bpf_int_jit_compile(func[i]);
5154 if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) {
5155 verbose(env, "JIT doesn't support bpf-to-bpf calls\n");
5156 err = -EFAULT;
5157 goto out_free;
5158 }
5159 cond_resched();
5160 }
5161
5162 /* finally lock prog and jit images for all functions and
5163 * populate kallsysm
5164 */
5165 for (i = 0; i <= env->subprog_cnt; i++) {
5166 bpf_prog_lock_ro(func[i]);
5167 bpf_prog_kallsyms_add(func[i]);
5168 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01005169
5170 /* Last step: make now unused interpreter insns from main
5171 * prog consistent for later dump requests, so they can
5172 * later look the same as if they were interpreted only.
5173 */
5174 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
5175 unsigned long addr;
5176
5177 if (insn->code != (BPF_JMP | BPF_CALL) ||
5178 insn->src_reg != BPF_PSEUDO_CALL)
5179 continue;
5180 insn->off = env->insn_aux_data[i].call_imm;
5181 subprog = find_subprog(env, i + insn->off + 1);
5182 addr = (unsigned long)func[subprog + 1]->bpf_func;
5183 addr &= PAGE_MASK;
5184 insn->imm = (u64 (*)(u64, u64, u64, u64, u64))
5185 addr - __bpf_call_base;
5186 }
5187
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005188 prog->jited = 1;
5189 prog->bpf_func = func[0]->bpf_func;
5190 prog->aux->func = func;
5191 prog->aux->func_cnt = env->subprog_cnt + 1;
5192 return 0;
5193out_free:
5194 for (i = 0; i <= env->subprog_cnt; i++)
5195 if (func[i])
5196 bpf_jit_free(func[i]);
5197 kfree(func);
5198 /* cleanup main prog to be interpreted */
5199 prog->jit_requested = 0;
5200 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
5201 if (insn->code != (BPF_JMP | BPF_CALL) ||
5202 insn->src_reg != BPF_PSEUDO_CALL)
5203 continue;
5204 insn->off = 0;
5205 insn->imm = env->insn_aux_data[i].call_imm;
5206 }
5207 return err;
5208}
5209
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08005210static int fixup_call_args(struct bpf_verifier_env *env)
5211{
5212 struct bpf_prog *prog = env->prog;
5213 struct bpf_insn *insn = prog->insnsi;
5214 int i, depth;
5215
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005216 if (env->prog->jit_requested)
5217 if (jit_subprogs(env) == 0)
5218 return 0;
5219
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08005220 for (i = 0; i < prog->len; i++, insn++) {
5221 if (insn->code != (BPF_JMP | BPF_CALL) ||
5222 insn->src_reg != BPF_PSEUDO_CALL)
5223 continue;
5224 depth = get_callee_stack_depth(env, insn, i);
5225 if (depth < 0)
5226 return depth;
5227 bpf_patch_call_args(insn, depth);
5228 }
5229 return 0;
5230}
5231
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005232/* fixup insn->imm field of bpf_call instructions
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07005233 * and inline eligible helpers as explicit sequence of BPF instructions
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005234 *
5235 * this function is called after eBPF program passed verification
5236 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005237static int fixup_bpf_calls(struct bpf_verifier_env *env)
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005238{
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005239 struct bpf_prog *prog = env->prog;
5240 struct bpf_insn *insn = prog->insnsi;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005241 const struct bpf_func_proto *fn;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005242 const int insn_cnt = prog->len;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07005243 struct bpf_insn insn_buf[16];
5244 struct bpf_prog *new_prog;
5245 struct bpf_map *map_ptr;
5246 int i, cnt, delta = 0;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005247
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005248 for (i = 0; i < insn_cnt; i++, insn++) {
5249 if (insn->code != (BPF_JMP | BPF_CALL))
5250 continue;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08005251 if (insn->src_reg == BPF_PSEUDO_CALL)
5252 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005253
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005254 if (insn->imm == BPF_FUNC_get_route_realm)
5255 prog->dst_needed = 1;
5256 if (insn->imm == BPF_FUNC_get_prandom_u32)
5257 bpf_user_rnd_init_once();
Josef Bacik9802d862017-12-11 11:36:48 -05005258 if (insn->imm == BPF_FUNC_override_return)
5259 prog->kprobe_override = 1;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005260 if (insn->imm == BPF_FUNC_tail_call) {
David S. Miller7b9f6da2017-04-20 10:35:33 -04005261 /* If we tail call into other programs, we
5262 * cannot make any assumptions since they can
5263 * be replaced dynamically during runtime in
5264 * the program array.
5265 */
5266 prog->cb_access = 1;
Alexei Starovoitov80a58d02017-05-30 13:31:30 -07005267 env->prog->aux->stack_depth = MAX_BPF_STACK;
David S. Miller7b9f6da2017-04-20 10:35:33 -04005268
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005269 /* mark bpf_tail_call as different opcode to avoid
5270 * conditional branch in the interpeter for every normal
5271 * call and to prevent accidental JITing by JIT compiler
5272 * that doesn't support bpf_tail_call yet
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005273 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005274 insn->imm = 0;
Alexei Starovoitov71189fa2017-05-30 13:31:27 -07005275 insn->code = BPF_JMP | BPF_TAIL_CALL;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005276 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005277 }
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005278
Daniel Borkmann89c63072017-08-19 03:12:45 +02005279 /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup
5280 * handlers are currently limited to 64 bit only.
5281 */
Alexei Starovoitov60b58afc2017-12-14 17:55:14 -08005282 if (prog->jit_requested && BITS_PER_LONG == 64 &&
Daniel Borkmann89c63072017-08-19 03:12:45 +02005283 insn->imm == BPF_FUNC_map_lookup_elem) {
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07005284 map_ptr = env->insn_aux_data[i + delta].map_ptr;
Martin KaFai Laufad73a12017-03-22 10:00:32 -07005285 if (map_ptr == BPF_MAP_PTR_POISON ||
5286 !map_ptr->ops->map_gen_lookup)
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07005287 goto patch_call_imm;
5288
5289 cnt = map_ptr->ops->map_gen_lookup(map_ptr, insn_buf);
5290 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005291 verbose(env, "bpf verifier is misconfigured\n");
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07005292 return -EINVAL;
5293 }
5294
5295 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf,
5296 cnt);
5297 if (!new_prog)
5298 return -ENOMEM;
5299
5300 delta += cnt - 1;
5301
5302 /* keep walking new program and skip insns we just inserted */
5303 env->prog = prog = new_prog;
5304 insn = new_prog->insnsi + i + delta;
5305 continue;
5306 }
5307
Daniel Borkmann109980b2017-09-08 00:14:51 +02005308 if (insn->imm == BPF_FUNC_redirect_map) {
Daniel Borkmann7c300132017-09-20 00:44:21 +02005309 /* Note, we cannot use prog directly as imm as subsequent
5310 * rewrites would still change the prog pointer. The only
5311 * stable address we can use is aux, which also works with
5312 * prog clones during blinding.
5313 */
5314 u64 addr = (unsigned long)prog->aux;
Daniel Borkmann109980b2017-09-08 00:14:51 +02005315 struct bpf_insn r4_ld[] = {
5316 BPF_LD_IMM64(BPF_REG_4, addr),
5317 *insn,
5318 };
5319 cnt = ARRAY_SIZE(r4_ld);
5320
5321 new_prog = bpf_patch_insn_data(env, i + delta, r4_ld, cnt);
5322 if (!new_prog)
5323 return -ENOMEM;
5324
5325 delta += cnt - 1;
5326 env->prog = prog = new_prog;
5327 insn = new_prog->insnsi + i + delta;
5328 }
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07005329patch_call_imm:
Jakub Kicinski00176a32017-10-16 16:40:54 -07005330 fn = env->ops->get_func_proto(insn->imm);
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005331 /* all functions that have prototype and verifier allowed
5332 * programs to call them, must be real in-kernel functions
5333 */
5334 if (!fn->func) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005335 verbose(env,
5336 "kernel subsystem misconfigured func %s#%d\n",
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005337 func_id_name(insn->imm), insn->imm);
5338 return -EFAULT;
5339 }
5340 insn->imm = fn->func - __bpf_call_base;
5341 }
5342
5343 return 0;
5344}
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005345
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005346static void free_states(struct bpf_verifier_env *env)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005347{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005348 struct bpf_verifier_state_list *sl, *sln;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005349 int i;
5350
5351 if (!env->explored_states)
5352 return;
5353
5354 for (i = 0; i < env->prog->len; i++) {
5355 sl = env->explored_states[i];
5356
5357 if (sl)
5358 while (sl != STATE_LIST_MARK) {
5359 sln = sl->next;
Alexei Starovoitov1969db42017-11-01 00:08:04 -07005360 free_verifier_state(&sl->state, false);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005361 kfree(sl);
5362 sl = sln;
5363 }
5364 }
5365
5366 kfree(env->explored_states);
5367}
5368
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005369int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
Alexei Starovoitov51580e72014-09-26 00:17:02 -07005370{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005371 struct bpf_verifier_env *env;
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005372 struct bpf_verifer_log *log;
Alexei Starovoitov51580e72014-09-26 00:17:02 -07005373 int ret = -EINVAL;
5374
Arnd Bergmanneba0c922017-11-02 12:05:52 +01005375 /* no program is valid */
5376 if (ARRAY_SIZE(bpf_verifier_ops) == 0)
5377 return -EINVAL;
5378
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005379 /* 'struct bpf_verifier_env' can be global, but since it's not small,
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005380 * allocate/free it every time bpf_check() is called
5381 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005382 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005383 if (!env)
5384 return -ENOMEM;
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005385 log = &env->log;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005386
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005387 env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
5388 (*prog)->len);
5389 ret = -ENOMEM;
5390 if (!env->insn_aux_data)
5391 goto err_free_env;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005392 env->prog = *prog;
Jakub Kicinski00176a32017-10-16 16:40:54 -07005393 env->ops = bpf_verifier_ops[env->prog->type];
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005394
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005395 /* grab the mutex to protect few globals used by verifier */
5396 mutex_lock(&bpf_verifier_lock);
5397
5398 if (attr->log_level || attr->log_buf || attr->log_size) {
5399 /* user requested verbose verifier output
5400 * and supplied buffer to store the verification trace
5401 */
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07005402 log->level = attr->log_level;
5403 log->ubuf = (char __user *) (unsigned long) attr->log_buf;
5404 log->len_total = attr->log_size;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005405
5406 ret = -EINVAL;
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07005407 /* log attributes have to be sane */
5408 if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 ||
5409 !log->level || !log->ubuf)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005410 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005411 }
Daniel Borkmann1ad2f582017-05-25 01:05:05 +02005412
5413 env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT);
5414 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
David S. Millere07b98d2017-05-10 11:38:07 -07005415 env->strict_alignment = true;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005416
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07005417 if (env->prog->aux->offload) {
5418 ret = bpf_prog_offload_verifier_prep(env);
5419 if (ret)
5420 goto err_unlock;
5421 }
5422
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005423 ret = replace_map_fd_with_map_ptr(env);
5424 if (ret < 0)
5425 goto skip_full_check;
5426
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005427 env->explored_states = kcalloc(env->prog->len,
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005428 sizeof(struct bpf_verifier_state_list *),
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005429 GFP_USER);
5430 ret = -ENOMEM;
5431 if (!env->explored_states)
5432 goto skip_full_check;
5433
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08005434 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
5435
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005436 ret = check_cfg(env);
5437 if (ret < 0)
5438 goto skip_full_check;
5439
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005440 ret = do_check(env);
Craig Gallek8c01c4f2017-11-02 11:18:01 -04005441 if (env->cur_state) {
5442 free_verifier_state(env->cur_state, true);
5443 env->cur_state = NULL;
5444 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005445
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005446skip_full_check:
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005447 while (!pop_stack(env, NULL, NULL));
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005448 free_states(env);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005449
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005450 if (ret == 0)
Alexei Starovoitovc1311872017-11-22 16:42:05 -08005451 sanitize_dead_code(env);
5452
5453 if (ret == 0)
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08005454 ret = check_max_stack_depth(env);
5455
5456 if (ret == 0)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005457 /* program is valid, convert *(u32*)(ctx + off) accesses */
5458 ret = convert_ctx_accesses(env);
5459
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005460 if (ret == 0)
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005461 ret = fixup_bpf_calls(env);
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005462
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08005463 if (ret == 0)
5464 ret = fixup_call_args(env);
5465
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07005466 if (log->level && bpf_verifier_log_full(log))
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005467 ret = -ENOSPC;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07005468 if (log->level && !log->ubuf) {
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005469 ret = -EFAULT;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07005470 goto err_release_maps;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005471 }
5472
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005473 if (ret == 0 && env->used_map_cnt) {
5474 /* if program passed verifier, update used_maps in bpf_prog_info */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005475 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
5476 sizeof(env->used_maps[0]),
5477 GFP_KERNEL);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005478
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005479 if (!env->prog->aux->used_maps) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005480 ret = -ENOMEM;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07005481 goto err_release_maps;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005482 }
5483
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005484 memcpy(env->prog->aux->used_maps, env->used_maps,
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005485 sizeof(env->used_maps[0]) * env->used_map_cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005486 env->prog->aux->used_map_cnt = env->used_map_cnt;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005487
5488 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
5489 * bpf_ld_imm64 instructions
5490 */
5491 convert_pseudo_ld_imm64(env);
5492 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005493
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07005494err_release_maps:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005495 if (!env->prog->aux->used_maps)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005496 /* if we didn't copy map pointers into bpf_prog_info, release
5497 * them now. Otherwise free_bpf_prog_info() will release them.
5498 */
5499 release_maps(env);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005500 *prog = env->prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005501err_unlock:
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005502 mutex_unlock(&bpf_verifier_lock);
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005503 vfree(env->insn_aux_data);
5504err_free_env:
5505 kfree(env);
Alexei Starovoitov51580e72014-09-26 00:17:02 -07005506 return ret;
5507}