blob: e53458b022493d09bc9cc96aaac9d5821fd1bc3e [file] [log] [blame]
Alexei Starovoitov51580e72014-09-26 00:17:02 -07001/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002 * Copyright (c) 2016 Facebook
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13#include <linux/kernel.h>
14#include <linux/types.h>
15#include <linux/slab.h>
16#include <linux/bpf.h>
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +010017#include <linux/bpf_verifier.h>
Alexei Starovoitov51580e72014-09-26 00:17:02 -070018#include <linux/filter.h>
19#include <net/netlink.h>
20#include <linux/file.h>
21#include <linux/vmalloc.h>
Thomas Grafebb676d2016-10-27 11:23:51 +020022#include <linux/stringify.h>
Alexei Starovoitov51580e72014-09-26 00:17:02 -070023
24/* bpf_check() is a static code analyzer that walks eBPF program
25 * instruction by instruction and updates register/stack state.
26 * All paths of conditional branches are analyzed until 'bpf_exit' insn.
27 *
28 * The first pass is depth-first-search to check that the program is a DAG.
29 * It rejects the following programs:
30 * - larger than BPF_MAXINSNS insns
31 * - if loop is present (detected via back-edge)
32 * - unreachable insns exist (shouldn't be a forest. program = one function)
33 * - out of bounds or malformed jumps
34 * The second pass is all possible path descent from the 1st insn.
35 * Since it's analyzing all pathes through the program, the length of the
Gary Lineba38a92017-03-01 16:25:51 +080036 * analysis is limited to 64k insn, which may be hit even if total number of
Alexei Starovoitov51580e72014-09-26 00:17:02 -070037 * insn is less then 4K, but there are too many branches that change stack/regs.
38 * Number of 'branches to be analyzed' is limited to 1k
39 *
40 * On entry to each instruction, each register has a type, and the instruction
41 * changes the types of the registers depending on instruction semantics.
42 * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is
43 * copied to R1.
44 *
45 * All registers are 64-bit.
46 * R0 - return register
47 * R1-R5 argument passing registers
48 * R6-R9 callee saved registers
49 * R10 - frame pointer read-only
50 *
51 * At the start of BPF program the register R1 contains a pointer to bpf_context
52 * and has type PTR_TO_CTX.
53 *
54 * Verifier tracks arithmetic operations on pointers in case:
55 * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
56 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20),
57 * 1st insn copies R10 (which has FRAME_PTR) type into R1
58 * and 2nd arithmetic instruction is pattern matched to recognize
59 * that it wants to construct a pointer to some element within stack.
60 * So after 2nd insn, the register R1 has type PTR_TO_STACK
61 * (and -20 constant is saved for further stack bounds checking).
62 * Meaning that this reg is a pointer to stack plus known immediate constant.
63 *
Edward Creef1174f72017-08-07 15:26:19 +010064 * Most of the time the registers have SCALAR_VALUE type, which
Alexei Starovoitov51580e72014-09-26 00:17:02 -070065 * means the register has some value, but it's not a valid pointer.
Edward Creef1174f72017-08-07 15:26:19 +010066 * (like pointer plus pointer becomes SCALAR_VALUE type)
Alexei Starovoitov51580e72014-09-26 00:17:02 -070067 *
68 * When verifier sees load or store instructions the type of base register
Edward Creef1174f72017-08-07 15:26:19 +010069 * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK. These are three pointer
Alexei Starovoitov51580e72014-09-26 00:17:02 -070070 * types recognized by check_mem_access() function.
71 *
72 * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value'
73 * and the range of [ptr, ptr + map's value_size) is accessible.
74 *
75 * registers used to pass values to function calls are checked against
76 * function argument constraints.
77 *
78 * ARG_PTR_TO_MAP_KEY is one of such argument constraints.
79 * It means that the register type passed to this function must be
80 * PTR_TO_STACK and it will be used inside the function as
81 * 'pointer to map element key'
82 *
83 * For example the argument constraints for bpf_map_lookup_elem():
84 * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
85 * .arg1_type = ARG_CONST_MAP_PTR,
86 * .arg2_type = ARG_PTR_TO_MAP_KEY,
87 *
88 * ret_type says that this function returns 'pointer to map elem value or null'
89 * function expects 1st argument to be a const pointer to 'struct bpf_map' and
90 * 2nd argument should be a pointer to stack, which will be used inside
91 * the helper function as a pointer to map element key.
92 *
93 * On the kernel side the helper function looks like:
94 * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
95 * {
96 * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
97 * void *key = (void *) (unsigned long) r2;
98 * void *value;
99 *
100 * here kernel can access 'key' and 'map' pointers safely, knowing that
101 * [key, key + map->key_size) bytes are valid and were initialized on
102 * the stack of eBPF program.
103 * }
104 *
105 * Corresponding eBPF program may look like:
106 * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR
107 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK
108 * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP
109 * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
110 * here verifier looks at prototype of map_lookup_elem() and sees:
111 * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok,
112 * Now verifier knows that this map has key of R1->map_ptr->key_size bytes
113 *
114 * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far,
115 * Now verifier checks that [R2, R2 + map's key_size) are within stack limits
116 * and were initialized prior to this call.
117 * If it's ok, then verifier allows this BPF_CALL insn and looks at
118 * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets
119 * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function
120 * returns ether pointer to map value or NULL.
121 *
122 * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off'
123 * insn, the register holding that pointer in the true branch changes state to
124 * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false
125 * branch. See check_cond_jmp_op().
126 *
127 * After the call R0 is set to return type of the function and registers R1-R5
128 * are set to NOT_INIT to indicate that they are no longer readable.
129 */
130
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700131/* verifier_state + insn_idx are pushed to stack when branch is encountered */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100132struct bpf_verifier_stack_elem {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700133 /* verifer state is 'st'
134 * before processing instruction 'insn_idx'
135 * and after processing instruction 'prev_insn_idx'
136 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100137 struct bpf_verifier_state st;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700138 int insn_idx;
139 int prev_insn_idx;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100140 struct bpf_verifier_stack_elem *next;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700141};
142
Edward Cree8e17c1b2017-08-07 15:30:30 +0100143#define BPF_COMPLEXITY_LIMIT_INSNS 131072
Daniel Borkmann07016152016-04-05 22:33:17 +0200144#define BPF_COMPLEXITY_LIMIT_STACK 1024
145
Martin KaFai Laufad73a12017-03-22 10:00:32 -0700146#define BPF_MAP_PTR_POISON ((void *)0xeB9F + POISON_POINTER_DELTA)
147
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200148struct bpf_call_arg_meta {
149 struct bpf_map *map_ptr;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200150 bool raw_mode;
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200151 bool pkt_access;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200152 int regno;
153 int access_size;
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200154};
155
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700156/* verbose verifier prints what it's seeing
157 * bpf_check() is called under lock, so no race to access these global vars
158 */
Jakub Kicinskie7bf8242017-10-09 10:30:10 -0700159static struct bpf_verifer_log verifier_log;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700160
161static DEFINE_MUTEX(bpf_verifier_lock);
162
163/* log_level controls verbosity level of eBPF verifier.
164 * verbose() is used to dump the verification trace to the log, so the user
165 * can figure out what's wrong with the program
166 */
Daniel Borkmann1d056d92015-11-03 11:39:20 +0100167static __printf(1, 2) void verbose(const char *fmt, ...)
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700168{
Jakub Kicinskie7bf8242017-10-09 10:30:10 -0700169 struct bpf_verifer_log *log = &verifier_log;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700170 va_list args;
171
Jakub Kicinskie7bf8242017-10-09 10:30:10 -0700172 if (!log->level || bpf_verifier_log_full(log))
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700173 return;
174
175 va_start(args, fmt);
Jakub Kicinskie7bf8242017-10-09 10:30:10 -0700176 log->len_used += vscnprintf(log->kbuf + log->len_used,
177 log->len_total - log->len_used, fmt, args);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700178 va_end(args);
179}
180
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200181static bool type_is_pkt_pointer(enum bpf_reg_type type)
182{
183 return type == PTR_TO_PACKET ||
184 type == PTR_TO_PACKET_META;
185}
186
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700187/* string representation of 'enum bpf_reg_type' */
188static const char * const reg_type_str[] = {
189 [NOT_INIT] = "?",
Edward Creef1174f72017-08-07 15:26:19 +0100190 [SCALAR_VALUE] = "inv",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700191 [PTR_TO_CTX] = "ctx",
192 [CONST_PTR_TO_MAP] = "map_ptr",
193 [PTR_TO_MAP_VALUE] = "map_value",
194 [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700195 [PTR_TO_STACK] = "fp",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700196 [PTR_TO_PACKET] = "pkt",
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200197 [PTR_TO_PACKET_META] = "pkt_meta",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700198 [PTR_TO_PACKET_END] = "pkt_end",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700199};
200
Thomas Grafebb676d2016-10-27 11:23:51 +0200201#define __BPF_FUNC_STR_FN(x) [BPF_FUNC_ ## x] = __stringify(bpf_ ## x)
202static const char * const func_id_str[] = {
203 __BPF_FUNC_MAPPER(__BPF_FUNC_STR_FN)
204};
205#undef __BPF_FUNC_STR_FN
206
207static const char *func_id_name(int id)
208{
209 BUILD_BUG_ON(ARRAY_SIZE(func_id_str) != __BPF_FUNC_MAX_ID);
210
211 if (id >= 0 && id < __BPF_FUNC_MAX_ID && func_id_str[id])
212 return func_id_str[id];
213 else
214 return "unknown";
215}
216
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100217static void print_verifier_state(struct bpf_verifier_state *state)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700218{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100219 struct bpf_reg_state *reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700220 enum bpf_reg_type t;
221 int i;
222
223 for (i = 0; i < MAX_BPF_REG; i++) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700224 reg = &state->regs[i];
225 t = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700226 if (t == NOT_INIT)
227 continue;
228 verbose(" R%d=%s", i, reg_type_str[t]);
Edward Creef1174f72017-08-07 15:26:19 +0100229 if ((t == SCALAR_VALUE || t == PTR_TO_STACK) &&
230 tnum_is_const(reg->var_off)) {
231 /* reg->off should be 0 for SCALAR_VALUE */
232 verbose("%lld", reg->var_off.value + reg->off);
233 } else {
234 verbose("(id=%d", reg->id);
235 if (t != SCALAR_VALUE)
236 verbose(",off=%d", reg->off);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200237 if (type_is_pkt_pointer(t))
Edward Creef1174f72017-08-07 15:26:19 +0100238 verbose(",r=%d", reg->range);
239 else if (t == CONST_PTR_TO_MAP ||
240 t == PTR_TO_MAP_VALUE ||
241 t == PTR_TO_MAP_VALUE_OR_NULL)
242 verbose(",ks=%d,vs=%d",
243 reg->map_ptr->key_size,
244 reg->map_ptr->value_size);
Edward Cree7d1238f2017-08-07 15:26:56 +0100245 if (tnum_is_const(reg->var_off)) {
246 /* Typically an immediate SCALAR_VALUE, but
247 * could be a pointer whose offset is too big
248 * for reg->off
249 */
250 verbose(",imm=%llx", reg->var_off.value);
251 } else {
252 if (reg->smin_value != reg->umin_value &&
253 reg->smin_value != S64_MIN)
254 verbose(",smin_value=%lld",
255 (long long)reg->smin_value);
256 if (reg->smax_value != reg->umax_value &&
257 reg->smax_value != S64_MAX)
258 verbose(",smax_value=%lld",
259 (long long)reg->smax_value);
260 if (reg->umin_value != 0)
261 verbose(",umin_value=%llu",
262 (unsigned long long)reg->umin_value);
263 if (reg->umax_value != U64_MAX)
264 verbose(",umax_value=%llu",
265 (unsigned long long)reg->umax_value);
266 if (!tnum_is_unknown(reg->var_off)) {
267 char tn_buf[48];
Edward Creef1174f72017-08-07 15:26:19 +0100268
Edward Cree7d1238f2017-08-07 15:26:56 +0100269 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
270 verbose(",var_off=%s", tn_buf);
271 }
Edward Creef1174f72017-08-07 15:26:19 +0100272 }
273 verbose(")");
274 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700275 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700276 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700277 if (state->stack_slot_type[i] == STACK_SPILL)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700278 verbose(" fp%d=%s", -MAX_BPF_STACK + i,
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700279 reg_type_str[state->spilled_regs[i / BPF_REG_SIZE].type]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700280 }
281 verbose("\n");
282}
283
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700284static const char *const bpf_class_string[] = {
285 [BPF_LD] = "ld",
286 [BPF_LDX] = "ldx",
287 [BPF_ST] = "st",
288 [BPF_STX] = "stx",
289 [BPF_ALU] = "alu",
290 [BPF_JMP] = "jmp",
291 [BPF_RET] = "BUG",
292 [BPF_ALU64] = "alu64",
293};
294
Alexei Starovoitov687f0712015-09-08 13:40:01 -0700295static const char *const bpf_alu_string[16] = {
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700296 [BPF_ADD >> 4] = "+=",
297 [BPF_SUB >> 4] = "-=",
298 [BPF_MUL >> 4] = "*=",
299 [BPF_DIV >> 4] = "/=",
300 [BPF_OR >> 4] = "|=",
301 [BPF_AND >> 4] = "&=",
302 [BPF_LSH >> 4] = "<<=",
303 [BPF_RSH >> 4] = ">>=",
304 [BPF_NEG >> 4] = "neg",
305 [BPF_MOD >> 4] = "%=",
306 [BPF_XOR >> 4] = "^=",
307 [BPF_MOV >> 4] = "=",
308 [BPF_ARSH >> 4] = "s>>=",
309 [BPF_END >> 4] = "endian",
310};
311
312static const char *const bpf_ldst_string[] = {
313 [BPF_W >> 3] = "u32",
314 [BPF_H >> 3] = "u16",
315 [BPF_B >> 3] = "u8",
316 [BPF_DW >> 3] = "u64",
317};
318
Alexei Starovoitov687f0712015-09-08 13:40:01 -0700319static const char *const bpf_jmp_string[16] = {
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700320 [BPF_JA >> 4] = "jmp",
321 [BPF_JEQ >> 4] = "==",
322 [BPF_JGT >> 4] = ">",
Daniel Borkmannb4e432f2017-08-10 01:40:02 +0200323 [BPF_JLT >> 4] = "<",
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700324 [BPF_JGE >> 4] = ">=",
Daniel Borkmannb4e432f2017-08-10 01:40:02 +0200325 [BPF_JLE >> 4] = "<=",
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700326 [BPF_JSET >> 4] = "&",
327 [BPF_JNE >> 4] = "!=",
328 [BPF_JSGT >> 4] = "s>",
Daniel Borkmannb4e432f2017-08-10 01:40:02 +0200329 [BPF_JSLT >> 4] = "s<",
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700330 [BPF_JSGE >> 4] = "s>=",
Daniel Borkmannb4e432f2017-08-10 01:40:02 +0200331 [BPF_JSLE >> 4] = "s<=",
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700332 [BPF_CALL >> 4] = "call",
333 [BPF_EXIT >> 4] = "exit",
334};
335
Edward Cree2b7c6ba2017-09-26 16:35:13 +0100336static void print_bpf_end_insn(const struct bpf_verifier_env *env,
337 const struct bpf_insn *insn)
338{
339 verbose("(%02x) r%d = %s%d r%d\n", insn->code, insn->dst_reg,
340 BPF_SRC(insn->code) == BPF_TO_BE ? "be" : "le",
341 insn->imm, insn->dst_reg);
342}
343
Daniel Borkmann0d0e5762017-05-08 00:04:09 +0200344static void print_bpf_insn(const struct bpf_verifier_env *env,
345 const struct bpf_insn *insn)
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700346{
347 u8 class = BPF_CLASS(insn->code);
348
349 if (class == BPF_ALU || class == BPF_ALU64) {
Edward Cree2b7c6ba2017-09-26 16:35:13 +0100350 if (BPF_OP(insn->code) == BPF_END) {
351 if (class == BPF_ALU64)
352 verbose("BUG_alu64_%02x\n", insn->code);
353 else
354 print_bpf_end_insn(env, insn);
Edward Cree73c864b2017-09-26 16:35:29 +0100355 } else if (BPF_OP(insn->code) == BPF_NEG) {
356 verbose("(%02x) r%d = %s-r%d\n",
357 insn->code, insn->dst_reg,
358 class == BPF_ALU ? "(u32) " : "",
359 insn->dst_reg);
Edward Cree2b7c6ba2017-09-26 16:35:13 +0100360 } else if (BPF_SRC(insn->code) == BPF_X) {
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700361 verbose("(%02x) %sr%d %s %sr%d\n",
362 insn->code, class == BPF_ALU ? "(u32) " : "",
363 insn->dst_reg,
364 bpf_alu_string[BPF_OP(insn->code) >> 4],
365 class == BPF_ALU ? "(u32) " : "",
366 insn->src_reg);
Edward Cree2b7c6ba2017-09-26 16:35:13 +0100367 } else {
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700368 verbose("(%02x) %sr%d %s %s%d\n",
369 insn->code, class == BPF_ALU ? "(u32) " : "",
370 insn->dst_reg,
371 bpf_alu_string[BPF_OP(insn->code) >> 4],
372 class == BPF_ALU ? "(u32) " : "",
373 insn->imm);
Edward Cree2b7c6ba2017-09-26 16:35:13 +0100374 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700375 } else if (class == BPF_STX) {
376 if (BPF_MODE(insn->code) == BPF_MEM)
377 verbose("(%02x) *(%s *)(r%d %+d) = r%d\n",
378 insn->code,
379 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
380 insn->dst_reg,
381 insn->off, insn->src_reg);
382 else if (BPF_MODE(insn->code) == BPF_XADD)
383 verbose("(%02x) lock *(%s *)(r%d %+d) += r%d\n",
384 insn->code,
385 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
386 insn->dst_reg, insn->off,
387 insn->src_reg);
388 else
389 verbose("BUG_%02x\n", insn->code);
390 } else if (class == BPF_ST) {
391 if (BPF_MODE(insn->code) != BPF_MEM) {
392 verbose("BUG_st_%02x\n", insn->code);
393 return;
394 }
395 verbose("(%02x) *(%s *)(r%d %+d) = %d\n",
396 insn->code,
397 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
398 insn->dst_reg,
399 insn->off, insn->imm);
400 } else if (class == BPF_LDX) {
401 if (BPF_MODE(insn->code) != BPF_MEM) {
402 verbose("BUG_ldx_%02x\n", insn->code);
403 return;
404 }
405 verbose("(%02x) r%d = *(%s *)(r%d %+d)\n",
406 insn->code, insn->dst_reg,
407 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
408 insn->src_reg, insn->off);
409 } else if (class == BPF_LD) {
410 if (BPF_MODE(insn->code) == BPF_ABS) {
411 verbose("(%02x) r0 = *(%s *)skb[%d]\n",
412 insn->code,
413 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
414 insn->imm);
415 } else if (BPF_MODE(insn->code) == BPF_IND) {
416 verbose("(%02x) r0 = *(%s *)skb[r%d + %d]\n",
417 insn->code,
418 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
419 insn->src_reg, insn->imm);
Daniel Borkmann0d0e5762017-05-08 00:04:09 +0200420 } else if (BPF_MODE(insn->code) == BPF_IMM &&
421 BPF_SIZE(insn->code) == BPF_DW) {
422 /* At this point, we already made sure that the second
423 * part of the ldimm64 insn is accessible.
424 */
425 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
426 bool map_ptr = insn->src_reg == BPF_PSEUDO_MAP_FD;
427
428 if (map_ptr && !env->allow_ptr_leaks)
429 imm = 0;
430
431 verbose("(%02x) r%d = 0x%llx\n", insn->code,
432 insn->dst_reg, (unsigned long long)imm);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700433 } else {
434 verbose("BUG_ld_%02x\n", insn->code);
435 return;
436 }
437 } else if (class == BPF_JMP) {
438 u8 opcode = BPF_OP(insn->code);
439
440 if (opcode == BPF_CALL) {
Thomas Grafebb676d2016-10-27 11:23:51 +0200441 verbose("(%02x) call %s#%d\n", insn->code,
442 func_id_name(insn->imm), insn->imm);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700443 } else if (insn->code == (BPF_JMP | BPF_JA)) {
444 verbose("(%02x) goto pc%+d\n",
445 insn->code, insn->off);
446 } else if (insn->code == (BPF_JMP | BPF_EXIT)) {
447 verbose("(%02x) exit\n", insn->code);
448 } else if (BPF_SRC(insn->code) == BPF_X) {
449 verbose("(%02x) if r%d %s r%d goto pc%+d\n",
450 insn->code, insn->dst_reg,
451 bpf_jmp_string[BPF_OP(insn->code) >> 4],
452 insn->src_reg, insn->off);
453 } else {
454 verbose("(%02x) if r%d %s 0x%x goto pc%+d\n",
455 insn->code, insn->dst_reg,
456 bpf_jmp_string[BPF_OP(insn->code) >> 4],
457 insn->imm, insn->off);
458 }
459 } else {
460 verbose("(%02x) %s\n", insn->code, bpf_class_string[class]);
461 }
462}
463
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100464static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700465{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100466 struct bpf_verifier_stack_elem *elem;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700467 int insn_idx;
468
469 if (env->head == NULL)
470 return -1;
471
472 memcpy(&env->cur_state, &env->head->st, sizeof(env->cur_state));
473 insn_idx = env->head->insn_idx;
474 if (prev_insn_idx)
475 *prev_insn_idx = env->head->prev_insn_idx;
476 elem = env->head->next;
477 kfree(env->head);
478 env->head = elem;
479 env->stack_size--;
480 return insn_idx;
481}
482
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100483static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,
484 int insn_idx, int prev_insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700485{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100486 struct bpf_verifier_stack_elem *elem;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700487
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100488 elem = kmalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700489 if (!elem)
490 goto err;
491
492 memcpy(&elem->st, &env->cur_state, sizeof(env->cur_state));
493 elem->insn_idx = insn_idx;
494 elem->prev_insn_idx = prev_insn_idx;
495 elem->next = env->head;
496 env->head = elem;
497 env->stack_size++;
Daniel Borkmann07016152016-04-05 22:33:17 +0200498 if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700499 verbose("BPF program is too complex\n");
500 goto err;
501 }
502 return &elem->st;
503err:
504 /* pop all elements and return */
505 while (pop_stack(env, NULL) >= 0);
506 return NULL;
507}
508
509#define CALLER_SAVED_REGS 6
510static const int caller_saved[CALLER_SAVED_REGS] = {
511 BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
512};
513
Edward Creef1174f72017-08-07 15:26:19 +0100514static void __mark_reg_not_init(struct bpf_reg_state *reg);
515
Edward Creeb03c9f92017-08-07 15:26:36 +0100516/* Mark the unknown part of a register (variable offset or scalar value) as
517 * known to have the value @imm.
518 */
519static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
520{
521 reg->id = 0;
522 reg->var_off = tnum_const(imm);
523 reg->smin_value = (s64)imm;
524 reg->smax_value = (s64)imm;
525 reg->umin_value = imm;
526 reg->umax_value = imm;
527}
528
Edward Creef1174f72017-08-07 15:26:19 +0100529/* Mark the 'variable offset' part of a register as zero. This should be
530 * used only on registers holding a pointer type.
531 */
532static void __mark_reg_known_zero(struct bpf_reg_state *reg)
533{
Edward Creeb03c9f92017-08-07 15:26:36 +0100534 __mark_reg_known(reg, 0);
Edward Creef1174f72017-08-07 15:26:19 +0100535}
536
537static void mark_reg_known_zero(struct bpf_reg_state *regs, u32 regno)
538{
539 if (WARN_ON(regno >= MAX_BPF_REG)) {
540 verbose("mark_reg_known_zero(regs, %u)\n", regno);
541 /* Something bad happened, let's kill all regs */
542 for (regno = 0; regno < MAX_BPF_REG; regno++)
543 __mark_reg_not_init(regs + regno);
544 return;
545 }
546 __mark_reg_known_zero(regs + regno);
547}
548
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200549static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg)
550{
551 return type_is_pkt_pointer(reg->type);
552}
553
554static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg)
555{
556 return reg_is_pkt_pointer(reg) ||
557 reg->type == PTR_TO_PACKET_END;
558}
559
560/* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */
561static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg,
562 enum bpf_reg_type which)
563{
564 /* The register can already have a range from prior markings.
565 * This is fine as long as it hasn't been advanced from its
566 * origin.
567 */
568 return reg->type == which &&
569 reg->id == 0 &&
570 reg->off == 0 &&
571 tnum_equals_const(reg->var_off, 0);
572}
573
Edward Creeb03c9f92017-08-07 15:26:36 +0100574/* Attempts to improve min/max values based on var_off information */
575static void __update_reg_bounds(struct bpf_reg_state *reg)
576{
577 /* min signed is max(sign bit) | min(other bits) */
578 reg->smin_value = max_t(s64, reg->smin_value,
579 reg->var_off.value | (reg->var_off.mask & S64_MIN));
580 /* max signed is min(sign bit) | max(other bits) */
581 reg->smax_value = min_t(s64, reg->smax_value,
582 reg->var_off.value | (reg->var_off.mask & S64_MAX));
583 reg->umin_value = max(reg->umin_value, reg->var_off.value);
584 reg->umax_value = min(reg->umax_value,
585 reg->var_off.value | reg->var_off.mask);
586}
587
588/* Uses signed min/max values to inform unsigned, and vice-versa */
589static void __reg_deduce_bounds(struct bpf_reg_state *reg)
590{
591 /* Learn sign from signed bounds.
592 * If we cannot cross the sign boundary, then signed and unsigned bounds
593 * are the same, so combine. This works even in the negative case, e.g.
594 * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff.
595 */
596 if (reg->smin_value >= 0 || reg->smax_value < 0) {
597 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
598 reg->umin_value);
599 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
600 reg->umax_value);
601 return;
602 }
603 /* Learn sign from unsigned bounds. Signed bounds cross the sign
604 * boundary, so we must be careful.
605 */
606 if ((s64)reg->umax_value >= 0) {
607 /* Positive. We can't learn anything from the smin, but smax
608 * is positive, hence safe.
609 */
610 reg->smin_value = reg->umin_value;
611 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
612 reg->umax_value);
613 } else if ((s64)reg->umin_value < 0) {
614 /* Negative. We can't learn anything from the smax, but smin
615 * is negative, hence safe.
616 */
617 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
618 reg->umin_value);
619 reg->smax_value = reg->umax_value;
620 }
621}
622
623/* Attempts to improve var_off based on unsigned min/max information */
624static void __reg_bound_offset(struct bpf_reg_state *reg)
625{
626 reg->var_off = tnum_intersect(reg->var_off,
627 tnum_range(reg->umin_value,
628 reg->umax_value));
629}
630
631/* Reset the min/max bounds of a register */
632static void __mark_reg_unbounded(struct bpf_reg_state *reg)
633{
634 reg->smin_value = S64_MIN;
635 reg->smax_value = S64_MAX;
636 reg->umin_value = 0;
637 reg->umax_value = U64_MAX;
638}
639
Edward Creef1174f72017-08-07 15:26:19 +0100640/* Mark a register as having a completely unknown (scalar) value. */
641static void __mark_reg_unknown(struct bpf_reg_state *reg)
642{
643 reg->type = SCALAR_VALUE;
644 reg->id = 0;
645 reg->off = 0;
646 reg->var_off = tnum_unknown;
Edward Creeb03c9f92017-08-07 15:26:36 +0100647 __mark_reg_unbounded(reg);
Edward Creef1174f72017-08-07 15:26:19 +0100648}
649
650static void mark_reg_unknown(struct bpf_reg_state *regs, u32 regno)
651{
652 if (WARN_ON(regno >= MAX_BPF_REG)) {
653 verbose("mark_reg_unknown(regs, %u)\n", regno);
654 /* Something bad happened, let's kill all regs */
655 for (regno = 0; regno < MAX_BPF_REG; regno++)
656 __mark_reg_not_init(regs + regno);
657 return;
658 }
659 __mark_reg_unknown(regs + regno);
660}
661
662static void __mark_reg_not_init(struct bpf_reg_state *reg)
663{
664 __mark_reg_unknown(reg);
665 reg->type = NOT_INIT;
666}
667
Daniel Borkmanna9789ef2017-05-25 01:05:06 +0200668static void mark_reg_not_init(struct bpf_reg_state *regs, u32 regno)
669{
Edward Creef1174f72017-08-07 15:26:19 +0100670 if (WARN_ON(regno >= MAX_BPF_REG)) {
671 verbose("mark_reg_not_init(regs, %u)\n", regno);
672 /* Something bad happened, let's kill all regs */
673 for (regno = 0; regno < MAX_BPF_REG; regno++)
674 __mark_reg_not_init(regs + regno);
675 return;
676 }
677 __mark_reg_not_init(regs + regno);
Daniel Borkmanna9789ef2017-05-25 01:05:06 +0200678}
679
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100680static void init_reg_state(struct bpf_reg_state *regs)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700681{
682 int i;
683
Edward Creedc503a82017-08-15 20:34:35 +0100684 for (i = 0; i < MAX_BPF_REG; i++) {
Daniel Borkmanna9789ef2017-05-25 01:05:06 +0200685 mark_reg_not_init(regs, i);
Edward Creedc503a82017-08-15 20:34:35 +0100686 regs[i].live = REG_LIVE_NONE;
687 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700688
689 /* frame pointer */
Edward Creef1174f72017-08-07 15:26:19 +0100690 regs[BPF_REG_FP].type = PTR_TO_STACK;
691 mark_reg_known_zero(regs, BPF_REG_FP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700692
693 /* 1st arg to a function */
694 regs[BPF_REG_1].type = PTR_TO_CTX;
Edward Creef1174f72017-08-07 15:26:19 +0100695 mark_reg_known_zero(regs, BPF_REG_1);
Daniel Borkmann6760bf22016-12-18 01:52:59 +0100696}
697
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700698enum reg_arg_type {
699 SRC_OP, /* register is used as source operand */
700 DST_OP, /* register is used as destination operand */
701 DST_OP_NO_MARK /* same as above, check only, don't mark */
702};
703
Edward Creedc503a82017-08-15 20:34:35 +0100704static void mark_reg_read(const struct bpf_verifier_state *state, u32 regno)
705{
706 struct bpf_verifier_state *parent = state->parent;
707
Alexei Starovoitov8fe2d6c2017-10-05 16:20:56 -0700708 if (regno == BPF_REG_FP)
709 /* We don't need to worry about FP liveness because it's read-only */
710 return;
711
Edward Creedc503a82017-08-15 20:34:35 +0100712 while (parent) {
713 /* if read wasn't screened by an earlier write ... */
714 if (state->regs[regno].live & REG_LIVE_WRITTEN)
715 break;
716 /* ... then we depend on parent's value */
717 parent->regs[regno].live |= REG_LIVE_READ;
718 state = parent;
719 parent = state->parent;
720 }
721}
722
723static int check_reg_arg(struct bpf_verifier_env *env, u32 regno,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700724 enum reg_arg_type t)
725{
Edward Creedc503a82017-08-15 20:34:35 +0100726 struct bpf_reg_state *regs = env->cur_state.regs;
727
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700728 if (regno >= MAX_BPF_REG) {
729 verbose("R%d is invalid\n", regno);
730 return -EINVAL;
731 }
732
733 if (t == SRC_OP) {
734 /* check whether register used as source operand can be read */
735 if (regs[regno].type == NOT_INIT) {
736 verbose("R%d !read_ok\n", regno);
737 return -EACCES;
738 }
Edward Creedc503a82017-08-15 20:34:35 +0100739 mark_reg_read(&env->cur_state, regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700740 } else {
741 /* check whether register used as dest operand can be written to */
742 if (regno == BPF_REG_FP) {
743 verbose("frame pointer is read only\n");
744 return -EACCES;
745 }
Edward Creedc503a82017-08-15 20:34:35 +0100746 regs[regno].live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700747 if (t == DST_OP)
Edward Creef1174f72017-08-07 15:26:19 +0100748 mark_reg_unknown(regs, regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700749 }
750 return 0;
751}
752
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700753static bool is_spillable_regtype(enum bpf_reg_type type)
754{
755 switch (type) {
756 case PTR_TO_MAP_VALUE:
757 case PTR_TO_MAP_VALUE_OR_NULL:
758 case PTR_TO_STACK:
759 case PTR_TO_CTX:
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700760 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200761 case PTR_TO_PACKET_META:
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700762 case PTR_TO_PACKET_END:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700763 case CONST_PTR_TO_MAP:
764 return true;
765 default:
766 return false;
767 }
768}
769
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700770/* check_stack_read/write functions track spill/fill of registers,
771 * stack boundary and alignment are checked in check_mem_access()
772 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100773static int check_stack_write(struct bpf_verifier_state *state, int off,
774 int size, int value_regno)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700775{
Edward Creedc503a82017-08-15 20:34:35 +0100776 int i, spi = (MAX_BPF_STACK + off) / BPF_REG_SIZE;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700777 /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
778 * so it's aligned access and [off, off + size) are within stack limits
779 */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700780
781 if (value_regno >= 0 &&
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700782 is_spillable_regtype(state->regs[value_regno].type)) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700783
784 /* register containing pointer is being spilled into stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700785 if (size != BPF_REG_SIZE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700786 verbose("invalid size of register spill\n");
787 return -EACCES;
788 }
789
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700790 /* save register state */
Edward Creedc503a82017-08-15 20:34:35 +0100791 state->spilled_regs[spi] = state->regs[value_regno];
792 state->spilled_regs[spi].live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700793
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700794 for (i = 0; i < BPF_REG_SIZE; i++)
795 state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_SPILL;
796 } else {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700797 /* regular write of data into stack */
Edward Creedc503a82017-08-15 20:34:35 +0100798 state->spilled_regs[spi] = (struct bpf_reg_state) {};
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700799
800 for (i = 0; i < size; i++)
801 state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_MISC;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700802 }
803 return 0;
804}
805
Edward Creedc503a82017-08-15 20:34:35 +0100806static void mark_stack_slot_read(const struct bpf_verifier_state *state, int slot)
807{
808 struct bpf_verifier_state *parent = state->parent;
809
810 while (parent) {
811 /* if read wasn't screened by an earlier write ... */
812 if (state->spilled_regs[slot].live & REG_LIVE_WRITTEN)
813 break;
814 /* ... then we depend on parent's value */
815 parent->spilled_regs[slot].live |= REG_LIVE_READ;
816 state = parent;
817 parent = state->parent;
818 }
819}
820
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100821static int check_stack_read(struct bpf_verifier_state *state, int off, int size,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700822 int value_regno)
823{
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700824 u8 *slot_type;
Edward Creedc503a82017-08-15 20:34:35 +0100825 int i, spi;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700826
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700827 slot_type = &state->stack_slot_type[MAX_BPF_STACK + off];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700828
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700829 if (slot_type[0] == STACK_SPILL) {
830 if (size != BPF_REG_SIZE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700831 verbose("invalid size of register spill\n");
832 return -EACCES;
833 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700834 for (i = 1; i < BPF_REG_SIZE; i++) {
835 if (slot_type[i] != STACK_SPILL) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700836 verbose("corrupted spill memory\n");
837 return -EACCES;
838 }
839 }
840
Edward Creedc503a82017-08-15 20:34:35 +0100841 spi = (MAX_BPF_STACK + off) / BPF_REG_SIZE;
842
843 if (value_regno >= 0) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700844 /* restore register state from stack */
Edward Creedc503a82017-08-15 20:34:35 +0100845 state->regs[value_regno] = state->spilled_regs[spi];
846 mark_stack_slot_read(state, spi);
847 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700848 return 0;
849 } else {
850 for (i = 0; i < size; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700851 if (slot_type[i] != STACK_MISC) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700852 verbose("invalid read from stack off %d+%d size %d\n",
853 off, i, size);
854 return -EACCES;
855 }
856 }
857 if (value_regno >= 0)
858 /* have read misc data from the stack */
Edward Creef1174f72017-08-07 15:26:19 +0100859 mark_reg_unknown(state->regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700860 return 0;
861 }
862}
863
864/* check read/write into map element returned by bpf_map_lookup_elem() */
Edward Creef1174f72017-08-07 15:26:19 +0100865static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700866 int size)
867{
868 struct bpf_map *map = env->cur_state.regs[regno].map_ptr;
869
Gianluca Borello57225692017-01-09 10:19:47 -0800870 if (off < 0 || size <= 0 || off + size > map->value_size) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700871 verbose("invalid access to map value, value_size=%d off=%d size=%d\n",
872 map->value_size, off, size);
873 return -EACCES;
874 }
875 return 0;
876}
877
Edward Creef1174f72017-08-07 15:26:19 +0100878/* check read/write into a map element with possible variable offset */
879static int check_map_access(struct bpf_verifier_env *env, u32 regno,
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -0800880 int off, int size)
881{
882 struct bpf_verifier_state *state = &env->cur_state;
883 struct bpf_reg_state *reg = &state->regs[regno];
884 int err;
885
Edward Creef1174f72017-08-07 15:26:19 +0100886 /* We may have adjusted the register to this map value, so we
887 * need to try adding each of min_value and max_value to off
888 * to make sure our theoretical access will be safe.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -0800889 */
Jakub Kicinskie7bf8242017-10-09 10:30:10 -0700890 if (verifier_log.level)
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -0800891 print_verifier_state(state);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -0800892 /* The minimum value is only important with signed
893 * comparisons where we can't assume the floor of a
894 * value is 0. If we are using signed variables for our
895 * index'es we need to make sure that whatever we use
896 * will have a set floor within our range.
897 */
Edward Creeb03c9f92017-08-07 15:26:36 +0100898 if (reg->smin_value < 0) {
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -0800899 verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
900 regno);
901 return -EACCES;
902 }
Edward Creeb03c9f92017-08-07 15:26:36 +0100903 err = __check_map_access(env, regno, reg->smin_value + off, size);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -0800904 if (err) {
Edward Creef1174f72017-08-07 15:26:19 +0100905 verbose("R%d min value is outside of the array range\n", regno);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -0800906 return err;
907 }
908
Edward Creeb03c9f92017-08-07 15:26:36 +0100909 /* If we haven't set a max value then we need to bail since we can't be
910 * sure we won't do bad things.
911 * If reg->umax_value + off could overflow, treat that as unbounded too.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -0800912 */
Edward Creeb03c9f92017-08-07 15:26:36 +0100913 if (reg->umax_value >= BPF_MAX_VAR_OFF) {
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -0800914 verbose("R%d unbounded memory access, make sure to bounds check any array access into a map\n",
915 regno);
916 return -EACCES;
917 }
Edward Creeb03c9f92017-08-07 15:26:36 +0100918 err = __check_map_access(env, regno, reg->umax_value + off, size);
Edward Creef1174f72017-08-07 15:26:19 +0100919 if (err)
920 verbose("R%d max value is outside of the array range\n", regno);
921 return err;
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -0800922}
923
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700924#define MAX_PACKET_OFF 0xffff
925
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100926static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
Thomas Graf3a0af8f2016-11-30 17:10:10 +0100927 const struct bpf_call_arg_meta *meta,
928 enum bpf_access_type t)
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700929{
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200930 switch (env->prog->type) {
Thomas Graf3a0af8f2016-11-30 17:10:10 +0100931 case BPF_PROG_TYPE_LWT_IN:
932 case BPF_PROG_TYPE_LWT_OUT:
933 /* dst_input() and dst_output() can't write for now */
934 if (t == BPF_WRITE)
935 return false;
Alexander Alemayhu7e57fbb2017-02-14 00:02:35 +0100936 /* fallthrough */
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200937 case BPF_PROG_TYPE_SCHED_CLS:
938 case BPF_PROG_TYPE_SCHED_ACT:
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700939 case BPF_PROG_TYPE_XDP:
Thomas Graf3a0af8f2016-11-30 17:10:10 +0100940 case BPF_PROG_TYPE_LWT_XMIT:
John Fastabend8a31db52017-08-15 22:33:09 -0700941 case BPF_PROG_TYPE_SK_SKB:
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200942 if (meta)
943 return meta->pkt_access;
944
945 env->seen_direct_write = true;
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700946 return true;
947 default:
948 return false;
949 }
950}
951
Edward Creef1174f72017-08-07 15:26:19 +0100952static int __check_packet_access(struct bpf_verifier_env *env, u32 regno,
953 int off, int size)
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700954{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100955 struct bpf_reg_state *regs = env->cur_state.regs;
956 struct bpf_reg_state *reg = &regs[regno];
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700957
Edward Creef1174f72017-08-07 15:26:19 +0100958 if (off < 0 || size <= 0 || (u64)off + size > reg->range) {
Alexei Starovoitovd91b28e2016-05-19 18:17:13 -0700959 verbose("invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n",
960 off, size, regno, reg->id, reg->off, reg->range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700961 return -EACCES;
962 }
963 return 0;
964}
965
Edward Creef1174f72017-08-07 15:26:19 +0100966static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
967 int size)
968{
969 struct bpf_reg_state *regs = env->cur_state.regs;
970 struct bpf_reg_state *reg = &regs[regno];
971 int err;
972
973 /* We may have added a variable offset to the packet pointer; but any
974 * reg->range we have comes after that. We are only checking the fixed
975 * offset.
976 */
977
978 /* We don't allow negative numbers, because we aren't tracking enough
979 * detail to prove they're safe.
980 */
Edward Creeb03c9f92017-08-07 15:26:36 +0100981 if (reg->smin_value < 0) {
Edward Creef1174f72017-08-07 15:26:19 +0100982 verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
983 regno);
984 return -EACCES;
985 }
986 err = __check_packet_access(env, regno, off, size);
987 if (err) {
988 verbose("R%d offset is outside of the packet\n", regno);
989 return err;
990 }
991 return err;
992}
993
994/* check access to 'struct bpf_context' fields. Supports fixed offsets only */
Yonghong Song31fd8582017-06-13 15:52:13 -0700995static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700996 enum bpf_access_type t, enum bpf_reg_type *reg_type)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700997{
Daniel Borkmannf96da092017-07-02 02:13:27 +0200998 struct bpf_insn_access_aux info = {
999 .reg_type = *reg_type,
1000 };
Yonghong Song31fd8582017-06-13 15:52:13 -07001001
Jakub Kicinski13a27df2016-09-21 11:43:58 +01001002 /* for analyzer ctx accesses are already validated and converted */
1003 if (env->analyzer_ops)
1004 return 0;
1005
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001006 if (env->prog->aux->ops->is_valid_access &&
Yonghong Song23994632017-06-22 15:07:39 -07001007 env->prog->aux->ops->is_valid_access(off, size, t, &info)) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02001008 /* A non zero info.ctx_field_size indicates that this field is a
1009 * candidate for later verifier transformation to load the whole
1010 * field and then apply a mask when accessed with a narrower
1011 * access than actual ctx access size. A zero info.ctx_field_size
1012 * will only allow for whole field access and rejects any other
1013 * type of narrower access.
Yonghong Song31fd8582017-06-13 15:52:13 -07001014 */
Daniel Borkmannf96da092017-07-02 02:13:27 +02001015 env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size;
Yonghong Song23994632017-06-22 15:07:39 -07001016 *reg_type = info.reg_type;
Yonghong Song31fd8582017-06-13 15:52:13 -07001017
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07001018 /* remember the offset of last byte accessed in ctx */
1019 if (env->prog->aux->max_ctx_offset < off + size)
1020 env->prog->aux->max_ctx_offset = off + size;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001021 return 0;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07001022 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001023
1024 verbose("invalid bpf_context access off=%d size=%d\n", off, size);
1025 return -EACCES;
1026}
1027
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001028static bool __is_pointer_value(bool allow_ptr_leaks,
1029 const struct bpf_reg_state *reg)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001030{
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001031 if (allow_ptr_leaks)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001032 return false;
1033
Edward Creef1174f72017-08-07 15:26:19 +01001034 return reg->type != SCALAR_VALUE;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001035}
1036
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001037static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
1038{
1039 return __is_pointer_value(env->allow_ptr_leaks, &env->cur_state.regs[regno]);
1040}
1041
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001042static int check_pkt_ptr_alignment(const struct bpf_reg_state *reg,
David S. Millerd1174412017-05-10 11:22:52 -07001043 int off, int size, bool strict)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001044{
Edward Creef1174f72017-08-07 15:26:19 +01001045 struct tnum reg_off;
David S. Millere07b98d2017-05-10 11:38:07 -07001046 int ip_align;
David S. Millerd1174412017-05-10 11:22:52 -07001047
1048 /* Byte size accesses are always allowed. */
1049 if (!strict || size == 1)
1050 return 0;
1051
David S. Millere4eda882017-05-22 12:27:07 -04001052 /* For platforms that do not have a Kconfig enabling
1053 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of
1054 * NET_IP_ALIGN is universally set to '2'. And on platforms
1055 * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get
1056 * to this code only in strict mode where we want to emulate
1057 * the NET_IP_ALIGN==2 checking. Therefore use an
1058 * unconditional IP align value of '2'.
David S. Millere07b98d2017-05-10 11:38:07 -07001059 */
David S. Millere4eda882017-05-22 12:27:07 -04001060 ip_align = 2;
Edward Creef1174f72017-08-07 15:26:19 +01001061
1062 reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off));
1063 if (!tnum_is_aligned(reg_off, size)) {
1064 char tn_buf[48];
1065
1066 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
1067 verbose("misaligned packet access off %d+%s+%d+%d size %d\n",
1068 ip_align, tn_buf, reg->off, off, size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001069 return -EACCES;
1070 }
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001071
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001072 return 0;
1073}
1074
Edward Creef1174f72017-08-07 15:26:19 +01001075static int check_generic_ptr_alignment(const struct bpf_reg_state *reg,
1076 const char *pointer_desc,
1077 int off, int size, bool strict)
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001078{
Edward Creef1174f72017-08-07 15:26:19 +01001079 struct tnum reg_off;
1080
1081 /* Byte size accesses are always allowed. */
1082 if (!strict || size == 1)
1083 return 0;
1084
1085 reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off));
1086 if (!tnum_is_aligned(reg_off, size)) {
1087 char tn_buf[48];
1088
1089 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
1090 verbose("misaligned %saccess off %s+%d+%d size %d\n",
1091 pointer_desc, tn_buf, reg->off, off, size);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001092 return -EACCES;
1093 }
1094
1095 return 0;
1096}
1097
David S. Millere07b98d2017-05-10 11:38:07 -07001098static int check_ptr_alignment(struct bpf_verifier_env *env,
1099 const struct bpf_reg_state *reg,
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001100 int off, int size)
1101{
David S. Millere07b98d2017-05-10 11:38:07 -07001102 bool strict = env->strict_alignment;
Edward Creef1174f72017-08-07 15:26:19 +01001103 const char *pointer_desc = "";
David S. Millerd1174412017-05-10 11:22:52 -07001104
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001105 switch (reg->type) {
1106 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001107 case PTR_TO_PACKET_META:
1108 /* Special case, because of NET_IP_ALIGN. Given metadata sits
1109 * right in front, treat it the very same way.
1110 */
David S. Millerd1174412017-05-10 11:22:52 -07001111 return check_pkt_ptr_alignment(reg, off, size, strict);
Edward Creef1174f72017-08-07 15:26:19 +01001112 case PTR_TO_MAP_VALUE:
1113 pointer_desc = "value ";
1114 break;
1115 case PTR_TO_CTX:
1116 pointer_desc = "context ";
1117 break;
1118 case PTR_TO_STACK:
1119 pointer_desc = "stack ";
1120 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001121 default:
Edward Creef1174f72017-08-07 15:26:19 +01001122 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001123 }
Edward Creef1174f72017-08-07 15:26:19 +01001124 return check_generic_ptr_alignment(reg, pointer_desc, off, size, strict);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001125}
1126
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001127/* check whether memory at (regno + off) is accessible for t = (read | write)
1128 * if t==write, value_regno is a register which value is stored into memory
1129 * if t==read, value_regno is a register which will receive the value from memory
1130 * if t==write && value_regno==-1, some unknown value is stored into memory
1131 * if t==read && value_regno==-1, don't care what we read from memory
1132 */
Yonghong Song31fd8582017-06-13 15:52:13 -07001133static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, int off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001134 int bpf_size, enum bpf_access_type t,
1135 int value_regno)
1136{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001137 struct bpf_verifier_state *state = &env->cur_state;
1138 struct bpf_reg_state *reg = &state->regs[regno];
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001139 int size, err = 0;
1140
1141 size = bpf_size_to_bytes(bpf_size);
1142 if (size < 0)
1143 return size;
1144
Edward Creef1174f72017-08-07 15:26:19 +01001145 /* alignment checks will add in reg->off themselves */
David S. Millere07b98d2017-05-10 11:38:07 -07001146 err = check_ptr_alignment(env, reg, off, size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001147 if (err)
1148 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001149
Edward Creef1174f72017-08-07 15:26:19 +01001150 /* for access checks, reg->off is just part of off */
1151 off += reg->off;
1152
1153 if (reg->type == PTR_TO_MAP_VALUE) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001154 if (t == BPF_WRITE && value_regno >= 0 &&
1155 is_pointer_value(env, value_regno)) {
1156 verbose("R%d leaks addr into map\n", value_regno);
1157 return -EACCES;
1158 }
Josef Bacik48461132016-09-28 10:54:32 -04001159
Edward Creef1174f72017-08-07 15:26:19 +01001160 err = check_map_access(env, regno, off, size);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001161 if (!err && t == BPF_READ && value_regno >= 0)
Edward Creef1174f72017-08-07 15:26:19 +01001162 mark_reg_unknown(state->regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001163
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07001164 } else if (reg->type == PTR_TO_CTX) {
Edward Creef1174f72017-08-07 15:26:19 +01001165 enum bpf_reg_type reg_type = SCALAR_VALUE;
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07001166
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001167 if (t == BPF_WRITE && value_regno >= 0 &&
1168 is_pointer_value(env, value_regno)) {
1169 verbose("R%d leaks addr into ctx\n", value_regno);
1170 return -EACCES;
1171 }
Edward Creef1174f72017-08-07 15:26:19 +01001172 /* ctx accesses must be at a fixed offset, so that we can
1173 * determine what type of data were returned.
1174 */
1175 if (!tnum_is_const(reg->var_off)) {
1176 char tn_buf[48];
1177
1178 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
1179 verbose("variable ctx access var_off=%s off=%d size=%d",
1180 tn_buf, off, size);
1181 return -EACCES;
1182 }
1183 off += reg->var_off.value;
Yonghong Song31fd8582017-06-13 15:52:13 -07001184 err = check_ctx_access(env, insn_idx, off, size, t, &reg_type);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001185 if (!err && t == BPF_READ && value_regno >= 0) {
Edward Creef1174f72017-08-07 15:26:19 +01001186 /* ctx access returns either a scalar, or a
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001187 * PTR_TO_PACKET[_META,_END]. In the latter
1188 * case, we know the offset is zero.
Edward Creef1174f72017-08-07 15:26:19 +01001189 */
1190 if (reg_type == SCALAR_VALUE)
1191 mark_reg_unknown(state->regs, value_regno);
1192 else
1193 mark_reg_known_zero(state->regs, value_regno);
1194 state->regs[value_regno].id = 0;
1195 state->regs[value_regno].off = 0;
1196 state->regs[value_regno].range = 0;
Mickaël Salaün19553512016-09-24 20:01:50 +02001197 state->regs[value_regno].type = reg_type;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001198 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001199
Edward Creef1174f72017-08-07 15:26:19 +01001200 } else if (reg->type == PTR_TO_STACK) {
1201 /* stack accesses must be at a fixed offset, so that we can
1202 * determine what type of data were returned.
1203 * See check_stack_read().
1204 */
1205 if (!tnum_is_const(reg->var_off)) {
1206 char tn_buf[48];
1207
1208 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
1209 verbose("variable stack access var_off=%s off=%d size=%d",
1210 tn_buf, off, size);
1211 return -EACCES;
1212 }
1213 off += reg->var_off.value;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001214 if (off >= 0 || off < -MAX_BPF_STACK) {
1215 verbose("invalid stack off=%d size=%d\n", off, size);
1216 return -EACCES;
1217 }
Alexei Starovoitov87266792017-05-30 13:31:29 -07001218
1219 if (env->prog->aux->stack_depth < -off)
1220 env->prog->aux->stack_depth = -off;
1221
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001222 if (t == BPF_WRITE) {
1223 if (!env->allow_ptr_leaks &&
1224 state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL &&
1225 size != BPF_REG_SIZE) {
1226 verbose("attempt to corrupt spilled pointer on stack\n");
1227 return -EACCES;
1228 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001229 err = check_stack_write(state, off, size, value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001230 } else {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001231 err = check_stack_read(state, off, size, value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001232 }
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001233 } else if (reg_is_pkt_pointer(reg)) {
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001234 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001235 verbose("cannot write into packet\n");
1236 return -EACCES;
1237 }
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001238 if (t == BPF_WRITE && value_regno >= 0 &&
1239 is_pointer_value(env, value_regno)) {
1240 verbose("R%d leaks addr into packet\n", value_regno);
1241 return -EACCES;
1242 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001243 err = check_packet_access(env, regno, off, size);
1244 if (!err && t == BPF_READ && value_regno >= 0)
Edward Creef1174f72017-08-07 15:26:19 +01001245 mark_reg_unknown(state->regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001246 } else {
1247 verbose("R%d invalid mem access '%s'\n",
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07001248 regno, reg_type_str[reg->type]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001249 return -EACCES;
1250 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001251
Edward Creef1174f72017-08-07 15:26:19 +01001252 if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ &&
1253 state->regs[value_regno].type == SCALAR_VALUE) {
1254 /* b/h/w load zero-extends, mark upper bits as known 0 */
1255 state->regs[value_regno].var_off = tnum_cast(
1256 state->regs[value_regno].var_off, size);
Edward Creeb03c9f92017-08-07 15:26:36 +01001257 __update_reg_bounds(&state->regs[value_regno]);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001258 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001259 return err;
1260}
1261
Yonghong Song31fd8582017-06-13 15:52:13 -07001262static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001263{
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001264 int err;
1265
1266 if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
1267 insn->imm != 0) {
1268 verbose("BPF_XADD uses reserved fields\n");
1269 return -EINVAL;
1270 }
1271
1272 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01001273 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001274 if (err)
1275 return err;
1276
1277 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01001278 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001279 if (err)
1280 return err;
1281
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +02001282 if (is_pointer_value(env, insn->src_reg)) {
1283 verbose("R%d leaks addr into mem\n", insn->src_reg);
1284 return -EACCES;
1285 }
1286
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001287 /* check whether atomic_add can read the memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07001288 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001289 BPF_SIZE(insn->code), BPF_READ, -1);
1290 if (err)
1291 return err;
1292
1293 /* check whether atomic_add can write into the same memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07001294 return check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001295 BPF_SIZE(insn->code), BPF_WRITE, -1);
1296}
1297
Edward Creef1174f72017-08-07 15:26:19 +01001298/* Does this register contain a constant zero? */
1299static bool register_is_null(struct bpf_reg_state reg)
1300{
1301 return reg.type == SCALAR_VALUE && tnum_equals_const(reg.var_off, 0);
1302}
1303
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001304/* when register 'regno' is passed into function that will read 'access_size'
1305 * bytes from that pointer, make sure that it's within stack boundary
Edward Creef1174f72017-08-07 15:26:19 +01001306 * and all elements of stack are initialized.
1307 * Unlike most pointer bounds-checking functions, this one doesn't take an
1308 * 'off' argument, so it has to add in reg->off itself.
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001309 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001310static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
Daniel Borkmann435faee12016-04-13 00:10:51 +02001311 int access_size, bool zero_size_allowed,
1312 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001313{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001314 struct bpf_verifier_state *state = &env->cur_state;
1315 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001316 int off, i;
1317
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001318 if (regs[regno].type != PTR_TO_STACK) {
Edward Creef1174f72017-08-07 15:26:19 +01001319 /* Allow zero-byte read from NULL, regardless of pointer type */
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001320 if (zero_size_allowed && access_size == 0 &&
Edward Creef1174f72017-08-07 15:26:19 +01001321 register_is_null(regs[regno]))
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001322 return 0;
1323
1324 verbose("R%d type=%s expected=%s\n", regno,
1325 reg_type_str[regs[regno].type],
1326 reg_type_str[PTR_TO_STACK]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001327 return -EACCES;
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001328 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001329
Edward Creef1174f72017-08-07 15:26:19 +01001330 /* Only allow fixed-offset stack reads */
1331 if (!tnum_is_const(regs[regno].var_off)) {
1332 char tn_buf[48];
1333
1334 tnum_strn(tn_buf, sizeof(tn_buf), regs[regno].var_off);
1335 verbose("invalid variable stack read R%d var_off=%s\n",
1336 regno, tn_buf);
1337 }
1338 off = regs[regno].off + regs[regno].var_off.value;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001339 if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
1340 access_size <= 0) {
1341 verbose("invalid stack type R%d off=%d access_size=%d\n",
1342 regno, off, access_size);
1343 return -EACCES;
1344 }
1345
Alexei Starovoitov87266792017-05-30 13:31:29 -07001346 if (env->prog->aux->stack_depth < -off)
1347 env->prog->aux->stack_depth = -off;
1348
Daniel Borkmann435faee12016-04-13 00:10:51 +02001349 if (meta && meta->raw_mode) {
1350 meta->access_size = access_size;
1351 meta->regno = regno;
1352 return 0;
1353 }
1354
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001355 for (i = 0; i < access_size; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001356 if (state->stack_slot_type[MAX_BPF_STACK + off + i] != STACK_MISC) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001357 verbose("invalid indirect read from stack off %d+%d size %d\n",
1358 off, i, access_size);
1359 return -EACCES;
1360 }
1361 }
1362 return 0;
1363}
1364
Gianluca Borello06c1c042017-01-09 10:19:49 -08001365static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
1366 int access_size, bool zero_size_allowed,
1367 struct bpf_call_arg_meta *meta)
1368{
Edward Creef1174f72017-08-07 15:26:19 +01001369 struct bpf_reg_state *regs = env->cur_state.regs, *reg = &regs[regno];
Gianluca Borello06c1c042017-01-09 10:19:49 -08001370
Edward Creef1174f72017-08-07 15:26:19 +01001371 switch (reg->type) {
Gianluca Borello06c1c042017-01-09 10:19:49 -08001372 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001373 case PTR_TO_PACKET_META:
Edward Creef1174f72017-08-07 15:26:19 +01001374 return check_packet_access(env, regno, reg->off, access_size);
Gianluca Borello06c1c042017-01-09 10:19:49 -08001375 case PTR_TO_MAP_VALUE:
Edward Creef1174f72017-08-07 15:26:19 +01001376 return check_map_access(env, regno, reg->off, access_size);
1377 default: /* scalar_value|ptr_to_stack or invalid ptr */
Gianluca Borello06c1c042017-01-09 10:19:49 -08001378 return check_stack_boundary(env, regno, access_size,
1379 zero_size_allowed, meta);
1380 }
1381}
1382
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001383static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001384 enum bpf_arg_type arg_type,
1385 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001386{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001387 struct bpf_reg_state *regs = env->cur_state.regs, *reg = &regs[regno];
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001388 enum bpf_reg_type expected_type, type = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001389 int err = 0;
1390
Daniel Borkmann80f1d682015-03-12 17:21:42 +01001391 if (arg_type == ARG_DONTCARE)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001392 return 0;
1393
Edward Creedc503a82017-08-15 20:34:35 +01001394 err = check_reg_arg(env, regno, SRC_OP);
1395 if (err)
1396 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001397
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001398 if (arg_type == ARG_ANYTHING) {
1399 if (is_pointer_value(env, regno)) {
1400 verbose("R%d leaks addr into helper function\n", regno);
1401 return -EACCES;
1402 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01001403 return 0;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001404 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01001405
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001406 if (type_is_pkt_pointer(type) &&
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001407 !may_access_direct_pkt_data(env, meta, BPF_READ)) {
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001408 verbose("helper access to the packet is not allowed\n");
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001409 return -EACCES;
1410 }
1411
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001412 if (arg_type == ARG_PTR_TO_MAP_KEY ||
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001413 arg_type == ARG_PTR_TO_MAP_VALUE) {
1414 expected_type = PTR_TO_STACK;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001415 if (!type_is_pkt_pointer(type) &&
1416 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001417 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001418 } else if (arg_type == ARG_CONST_SIZE ||
1419 arg_type == ARG_CONST_SIZE_OR_ZERO) {
Edward Creef1174f72017-08-07 15:26:19 +01001420 expected_type = SCALAR_VALUE;
1421 if (type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001422 goto err_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001423 } else if (arg_type == ARG_CONST_MAP_PTR) {
1424 expected_type = CONST_PTR_TO_MAP;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001425 if (type != expected_type)
1426 goto err_type;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001427 } else if (arg_type == ARG_PTR_TO_CTX) {
1428 expected_type = PTR_TO_CTX;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001429 if (type != expected_type)
1430 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001431 } else if (arg_type == ARG_PTR_TO_MEM ||
1432 arg_type == ARG_PTR_TO_UNINIT_MEM) {
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001433 expected_type = PTR_TO_STACK;
1434 /* One exception here. In case function allows for NULL to be
Edward Creef1174f72017-08-07 15:26:19 +01001435 * passed in as argument, it's a SCALAR_VALUE type. Final test
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001436 * happens during stack boundary checking.
1437 */
Edward Creef1174f72017-08-07 15:26:19 +01001438 if (register_is_null(*reg))
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001439 /* final test in check_stack_boundary() */;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001440 else if (!type_is_pkt_pointer(type) &&
1441 type != PTR_TO_MAP_VALUE &&
Edward Creef1174f72017-08-07 15:26:19 +01001442 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001443 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001444 meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001445 } else {
1446 verbose("unsupported arg_type %d\n", arg_type);
1447 return -EFAULT;
1448 }
1449
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001450 if (arg_type == ARG_CONST_MAP_PTR) {
1451 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001452 meta->map_ptr = reg->map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001453 } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
1454 /* bpf_map_xxx(..., map_ptr, ..., key) call:
1455 * check that [key, key + map->key_size) are within
1456 * stack limits and initialized
1457 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001458 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001459 /* in function declaration map_ptr must come before
1460 * map_key, so that it's verified and known before
1461 * we have to check map_key here. Otherwise it means
1462 * that kernel subsystem misconfigured verifier
1463 */
1464 verbose("invalid map_ptr to access map->key\n");
1465 return -EACCES;
1466 }
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001467 if (type_is_pkt_pointer(type))
Edward Creef1174f72017-08-07 15:26:19 +01001468 err = check_packet_access(env, regno, reg->off,
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001469 meta->map_ptr->key_size);
1470 else
1471 err = check_stack_boundary(env, regno,
1472 meta->map_ptr->key_size,
1473 false, NULL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001474 } else if (arg_type == ARG_PTR_TO_MAP_VALUE) {
1475 /* bpf_map_xxx(..., map_ptr, ..., value) call:
1476 * check [value, value + map->value_size) validity
1477 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001478 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001479 /* kernel subsystem misconfigured verifier */
1480 verbose("invalid map_ptr to access map->value\n");
1481 return -EACCES;
1482 }
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001483 if (type_is_pkt_pointer(type))
Edward Creef1174f72017-08-07 15:26:19 +01001484 err = check_packet_access(env, regno, reg->off,
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001485 meta->map_ptr->value_size);
1486 else
1487 err = check_stack_boundary(env, regno,
1488 meta->map_ptr->value_size,
1489 false, NULL);
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001490 } else if (arg_type == ARG_CONST_SIZE ||
1491 arg_type == ARG_CONST_SIZE_OR_ZERO) {
1492 bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001493
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001494 /* bpf_xxx(..., buf, len) call will access 'len' bytes
1495 * from stack pointer 'buf'. Check it
1496 * note: regno == len, regno - 1 == buf
1497 */
1498 if (regno == 0) {
1499 /* kernel subsystem misconfigured verifier */
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001500 verbose("ARG_CONST_SIZE cannot be first argument\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001501 return -EACCES;
1502 }
Gianluca Borello06c1c042017-01-09 10:19:49 -08001503
Edward Creef1174f72017-08-07 15:26:19 +01001504 /* The register is SCALAR_VALUE; the access check
1505 * happens using its boundaries.
Gianluca Borello06c1c042017-01-09 10:19:49 -08001506 */
Edward Creef1174f72017-08-07 15:26:19 +01001507
1508 if (!tnum_is_const(reg->var_off))
Gianluca Borello06c1c042017-01-09 10:19:49 -08001509 /* For unprivileged variable accesses, disable raw
1510 * mode so that the program is required to
1511 * initialize all the memory that the helper could
1512 * just partially fill up.
1513 */
1514 meta = NULL;
1515
Edward Creeb03c9f92017-08-07 15:26:36 +01001516 if (reg->smin_value < 0) {
Edward Creef1174f72017-08-07 15:26:19 +01001517 verbose("R%d min value is negative, either use unsigned or 'var &= const'\n",
1518 regno);
1519 return -EACCES;
1520 }
Gianluca Borello06c1c042017-01-09 10:19:49 -08001521
Edward Creeb03c9f92017-08-07 15:26:36 +01001522 if (reg->umin_value == 0) {
Edward Creef1174f72017-08-07 15:26:19 +01001523 err = check_helper_mem_access(env, regno - 1, 0,
1524 zero_size_allowed,
1525 meta);
Gianluca Borello06c1c042017-01-09 10:19:49 -08001526 if (err)
1527 return err;
Gianluca Borello06c1c042017-01-09 10:19:49 -08001528 }
Edward Creef1174f72017-08-07 15:26:19 +01001529
Edward Creeb03c9f92017-08-07 15:26:36 +01001530 if (reg->umax_value >= BPF_MAX_VAR_SIZ) {
Edward Creef1174f72017-08-07 15:26:19 +01001531 verbose("R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
1532 regno);
1533 return -EACCES;
1534 }
1535 err = check_helper_mem_access(env, regno - 1,
Edward Creeb03c9f92017-08-07 15:26:36 +01001536 reg->umax_value,
Edward Creef1174f72017-08-07 15:26:19 +01001537 zero_size_allowed, meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001538 }
1539
1540 return err;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001541err_type:
1542 verbose("R%d type=%s expected=%s\n", regno,
1543 reg_type_str[type], reg_type_str[expected_type]);
1544 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001545}
1546
Kaixu Xia35578d72015-08-06 07:02:35 +00001547static int check_map_func_compatibility(struct bpf_map *map, int func_id)
1548{
Kaixu Xia35578d72015-08-06 07:02:35 +00001549 if (!map)
1550 return 0;
1551
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001552 /* We need a two way check, first is from map perspective ... */
1553 switch (map->map_type) {
1554 case BPF_MAP_TYPE_PROG_ARRAY:
1555 if (func_id != BPF_FUNC_tail_call)
1556 goto error;
1557 break;
1558 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
1559 if (func_id != BPF_FUNC_perf_event_read &&
Yonghong Song908432c2017-10-05 09:19:20 -07001560 func_id != BPF_FUNC_perf_event_output &&
1561 func_id != BPF_FUNC_perf_event_read_value)
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001562 goto error;
1563 break;
1564 case BPF_MAP_TYPE_STACK_TRACE:
1565 if (func_id != BPF_FUNC_get_stackid)
1566 goto error;
1567 break;
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -07001568 case BPF_MAP_TYPE_CGROUP_ARRAY:
David S. Miller60747ef2016-08-18 01:17:32 -04001569 if (func_id != BPF_FUNC_skb_under_cgroup &&
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001570 func_id != BPF_FUNC_current_task_under_cgroup)
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07001571 goto error;
1572 break;
John Fastabend546ac1f2017-07-17 09:28:56 -07001573 /* devmap returns a pointer to a live net_device ifindex that we cannot
1574 * allow to be modified from bpf side. So do not allow lookup elements
1575 * for now.
1576 */
1577 case BPF_MAP_TYPE_DEVMAP:
John Fastabend2ddf71e2017-07-17 09:30:02 -07001578 if (func_id != BPF_FUNC_redirect_map)
John Fastabend546ac1f2017-07-17 09:28:56 -07001579 goto error;
1580 break;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07001581 case BPF_MAP_TYPE_ARRAY_OF_MAPS:
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001582 case BPF_MAP_TYPE_HASH_OF_MAPS:
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07001583 if (func_id != BPF_FUNC_map_lookup_elem)
1584 goto error;
Martin KaFai Lau16a43622017-08-17 18:14:43 -07001585 break;
John Fastabend174a79f2017-08-15 22:32:47 -07001586 case BPF_MAP_TYPE_SOCKMAP:
1587 if (func_id != BPF_FUNC_sk_redirect_map &&
1588 func_id != BPF_FUNC_sock_map_update &&
1589 func_id != BPF_FUNC_map_delete_elem)
1590 goto error;
1591 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001592 default:
1593 break;
1594 }
1595
1596 /* ... and second from the function itself. */
1597 switch (func_id) {
1598 case BPF_FUNC_tail_call:
1599 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1600 goto error;
1601 break;
1602 case BPF_FUNC_perf_event_read:
1603 case BPF_FUNC_perf_event_output:
Yonghong Song908432c2017-10-05 09:19:20 -07001604 case BPF_FUNC_perf_event_read_value:
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001605 if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
1606 goto error;
1607 break;
1608 case BPF_FUNC_get_stackid:
1609 if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
1610 goto error;
1611 break;
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001612 case BPF_FUNC_current_task_under_cgroup:
Daniel Borkmann747ea552016-08-12 22:17:17 +02001613 case BPF_FUNC_skb_under_cgroup:
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07001614 if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
1615 goto error;
1616 break;
John Fastabend97f91a72017-07-17 09:29:18 -07001617 case BPF_FUNC_redirect_map:
1618 if (map->map_type != BPF_MAP_TYPE_DEVMAP)
1619 goto error;
1620 break;
John Fastabend174a79f2017-08-15 22:32:47 -07001621 case BPF_FUNC_sk_redirect_map:
1622 if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
1623 goto error;
1624 break;
1625 case BPF_FUNC_sock_map_update:
1626 if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
1627 goto error;
1628 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001629 default:
1630 break;
Kaixu Xia35578d72015-08-06 07:02:35 +00001631 }
1632
1633 return 0;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001634error:
Thomas Grafebb676d2016-10-27 11:23:51 +02001635 verbose("cannot pass map_type %d into func %s#%d\n",
1636 map->map_type, func_id_name(func_id), func_id);
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001637 return -EINVAL;
Kaixu Xia35578d72015-08-06 07:02:35 +00001638}
1639
Daniel Borkmann435faee12016-04-13 00:10:51 +02001640static int check_raw_mode(const struct bpf_func_proto *fn)
1641{
1642 int count = 0;
1643
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001644 if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02001645 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001646 if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02001647 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001648 if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02001649 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001650 if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02001651 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001652 if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02001653 count++;
1654
1655 return count > 1 ? -EINVAL : 0;
1656}
1657
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001658/* Packet data might have moved, any old PTR_TO_PACKET[_META,_END]
1659 * are now invalid, so turn them into unknown SCALAR_VALUE.
Edward Creef1174f72017-08-07 15:26:19 +01001660 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001661static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001662{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001663 struct bpf_verifier_state *state = &env->cur_state;
1664 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001665 int i;
1666
1667 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001668 if (reg_is_pkt_pointer_any(&regs[i]))
Edward Creef1174f72017-08-07 15:26:19 +01001669 mark_reg_unknown(regs, i);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001670
1671 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
1672 if (state->stack_slot_type[i] != STACK_SPILL)
1673 continue;
1674 reg = &state->spilled_regs[i / BPF_REG_SIZE];
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001675 if (reg_is_pkt_pointer_any(reg))
1676 __mark_reg_unknown(reg);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001677 }
1678}
1679
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07001680static int check_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001681{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001682 struct bpf_verifier_state *state = &env->cur_state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001683 const struct bpf_func_proto *fn = NULL;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001684 struct bpf_reg_state *regs = state->regs;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001685 struct bpf_call_arg_meta meta;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001686 bool changes_data;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001687 int i, err;
1688
1689 /* find function prototype */
1690 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
Thomas Grafebb676d2016-10-27 11:23:51 +02001691 verbose("invalid func %s#%d\n", func_id_name(func_id), func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001692 return -EINVAL;
1693 }
1694
1695 if (env->prog->aux->ops->get_func_proto)
1696 fn = env->prog->aux->ops->get_func_proto(func_id);
1697
1698 if (!fn) {
Thomas Grafebb676d2016-10-27 11:23:51 +02001699 verbose("unknown func %s#%d\n", func_id_name(func_id), func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001700 return -EINVAL;
1701 }
1702
1703 /* eBPF programs must be GPL compatible to use GPL-ed functions */
Daniel Borkmann24701ec2015-03-01 12:31:47 +01001704 if (!env->prog->gpl_compatible && fn->gpl_only) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001705 verbose("cannot call GPL only function from proprietary program\n");
1706 return -EINVAL;
1707 }
1708
Martin KaFai Lau17bedab2016-12-07 15:53:11 -08001709 changes_data = bpf_helper_changes_pkt_data(fn->func);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001710
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001711 memset(&meta, 0, sizeof(meta));
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001712 meta.pkt_access = fn->pkt_access;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001713
Daniel Borkmann435faee12016-04-13 00:10:51 +02001714 /* We only support one arg being in raw mode at the moment, which
1715 * is sufficient for the helper functions we have right now.
1716 */
1717 err = check_raw_mode(fn);
1718 if (err) {
Thomas Grafebb676d2016-10-27 11:23:51 +02001719 verbose("kernel subsystem misconfigured func %s#%d\n",
1720 func_id_name(func_id), func_id);
Daniel Borkmann435faee12016-04-13 00:10:51 +02001721 return err;
1722 }
1723
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001724 /* check args */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001725 err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001726 if (err)
1727 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001728 err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001729 if (err)
1730 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001731 err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001732 if (err)
1733 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001734 err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001735 if (err)
1736 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001737 err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001738 if (err)
1739 return err;
1740
Daniel Borkmann435faee12016-04-13 00:10:51 +02001741 /* Mark slots with STACK_MISC in case of raw mode, stack offset
1742 * is inferred from register state.
1743 */
1744 for (i = 0; i < meta.access_size; i++) {
Yonghong Song31fd8582017-06-13 15:52:13 -07001745 err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, BPF_WRITE, -1);
Daniel Borkmann435faee12016-04-13 00:10:51 +02001746 if (err)
1747 return err;
1748 }
1749
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001750 /* reset caller saved regs */
Edward Creedc503a82017-08-15 20:34:35 +01001751 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Daniel Borkmanna9789ef2017-05-25 01:05:06 +02001752 mark_reg_not_init(regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01001753 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
1754 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001755
Edward Creedc503a82017-08-15 20:34:35 +01001756 /* update return register (already marked as written above) */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001757 if (fn->ret_type == RET_INTEGER) {
Edward Creef1174f72017-08-07 15:26:19 +01001758 /* sets type to SCALAR_VALUE */
1759 mark_reg_unknown(regs, BPF_REG_0);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001760 } else if (fn->ret_type == RET_VOID) {
1761 regs[BPF_REG_0].type = NOT_INIT;
1762 } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) {
Martin KaFai Laufad73a12017-03-22 10:00:32 -07001763 struct bpf_insn_aux_data *insn_aux;
1764
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001765 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
Edward Creef1174f72017-08-07 15:26:19 +01001766 /* There is no offset yet applied, variable or fixed */
1767 mark_reg_known_zero(regs, BPF_REG_0);
1768 regs[BPF_REG_0].off = 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001769 /* remember map_ptr, so that check_map_access()
1770 * can check 'value_size' boundary of memory access
1771 * to map element returned from bpf_map_lookup_elem()
1772 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001773 if (meta.map_ptr == NULL) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001774 verbose("kernel subsystem misconfigured verifier\n");
1775 return -EINVAL;
1776 }
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001777 regs[BPF_REG_0].map_ptr = meta.map_ptr;
Thomas Graf57a09bf2016-10-18 19:51:19 +02001778 regs[BPF_REG_0].id = ++env->id_gen;
Martin KaFai Laufad73a12017-03-22 10:00:32 -07001779 insn_aux = &env->insn_aux_data[insn_idx];
1780 if (!insn_aux->map_ptr)
1781 insn_aux->map_ptr = meta.map_ptr;
1782 else if (insn_aux->map_ptr != meta.map_ptr)
1783 insn_aux->map_ptr = BPF_MAP_PTR_POISON;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001784 } else {
Thomas Grafebb676d2016-10-27 11:23:51 +02001785 verbose("unknown return type %d of func %s#%d\n",
1786 fn->ret_type, func_id_name(func_id), func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001787 return -EINVAL;
1788 }
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001789
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001790 err = check_map_func_compatibility(meta.map_ptr, func_id);
Kaixu Xia35578d72015-08-06 07:02:35 +00001791 if (err)
1792 return err;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001793
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001794 if (changes_data)
1795 clear_all_pkt_pointers(env);
1796 return 0;
1797}
1798
Edward Creef1174f72017-08-07 15:26:19 +01001799static void coerce_reg_to_32(struct bpf_reg_state *reg)
David S. Millerd1174412017-05-10 11:22:52 -07001800{
Edward Creef1174f72017-08-07 15:26:19 +01001801 /* clear high 32 bits */
1802 reg->var_off = tnum_cast(reg->var_off, 4);
Edward Creeb03c9f92017-08-07 15:26:36 +01001803 /* Update bounds */
1804 __update_reg_bounds(reg);
1805}
1806
1807static bool signed_add_overflows(s64 a, s64 b)
1808{
1809 /* Do the add in u64, where overflow is well-defined */
1810 s64 res = (s64)((u64)a + (u64)b);
1811
1812 if (b < 0)
1813 return res > a;
1814 return res < a;
1815}
1816
1817static bool signed_sub_overflows(s64 a, s64 b)
1818{
1819 /* Do the sub in u64, where overflow is well-defined */
1820 s64 res = (s64)((u64)a - (u64)b);
1821
1822 if (b < 0)
1823 return res < a;
1824 return res > a;
David S. Millerd1174412017-05-10 11:22:52 -07001825}
1826
Edward Creef1174f72017-08-07 15:26:19 +01001827/* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off.
Edward Creef1174f72017-08-07 15:26:19 +01001828 * Caller should also handle BPF_MOV case separately.
1829 * If we return -EACCES, caller may want to try again treating pointer as a
1830 * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks.
1831 */
1832static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
1833 struct bpf_insn *insn,
1834 const struct bpf_reg_state *ptr_reg,
1835 const struct bpf_reg_state *off_reg)
Josef Bacik48461132016-09-28 10:54:32 -04001836{
1837 struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg;
Edward Creef1174f72017-08-07 15:26:19 +01001838 bool known = tnum_is_const(off_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01001839 s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value,
1840 smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
1841 u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value,
1842 umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value;
Josef Bacik48461132016-09-28 10:54:32 -04001843 u8 opcode = BPF_OP(insn->code);
Edward Creef1174f72017-08-07 15:26:19 +01001844 u32 dst = insn->dst_reg;
Josef Bacik48461132016-09-28 10:54:32 -04001845
Edward Creef1174f72017-08-07 15:26:19 +01001846 dst_reg = &regs[dst];
Josef Bacik48461132016-09-28 10:54:32 -04001847
Edward Creeb03c9f92017-08-07 15:26:36 +01001848 if (WARN_ON_ONCE(known && (smin_val != smax_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01001849 print_verifier_state(&env->cur_state);
Edward Creeb03c9f92017-08-07 15:26:36 +01001850 verbose("verifier internal error: known but bad sbounds\n");
1851 return -EINVAL;
1852 }
1853 if (WARN_ON_ONCE(known && (umin_val != umax_val))) {
1854 print_verifier_state(&env->cur_state);
1855 verbose("verifier internal error: known but bad ubounds\n");
Edward Creef1174f72017-08-07 15:26:19 +01001856 return -EINVAL;
Josef Bacik48461132016-09-28 10:54:32 -04001857 }
1858
Edward Creef1174f72017-08-07 15:26:19 +01001859 if (BPF_CLASS(insn->code) != BPF_ALU64) {
1860 /* 32-bit ALU ops on pointers produce (meaningless) scalars */
1861 if (!env->allow_ptr_leaks)
1862 verbose("R%d 32-bit pointer arithmetic prohibited\n",
1863 dst);
1864 return -EACCES;
1865 }
David S. Millerd1174412017-05-10 11:22:52 -07001866
Edward Creef1174f72017-08-07 15:26:19 +01001867 if (ptr_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
1868 if (!env->allow_ptr_leaks)
1869 verbose("R%d pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL prohibited, null-check it first\n",
1870 dst);
1871 return -EACCES;
1872 }
1873 if (ptr_reg->type == CONST_PTR_TO_MAP) {
1874 if (!env->allow_ptr_leaks)
1875 verbose("R%d pointer arithmetic on CONST_PTR_TO_MAP prohibited\n",
1876 dst);
1877 return -EACCES;
1878 }
1879 if (ptr_reg->type == PTR_TO_PACKET_END) {
1880 if (!env->allow_ptr_leaks)
1881 verbose("R%d pointer arithmetic on PTR_TO_PACKET_END prohibited\n",
1882 dst);
1883 return -EACCES;
1884 }
1885
1886 /* In case of 'scalar += pointer', dst_reg inherits pointer type and id.
1887 * The id may be overwritten later if we create a new variable offset.
Josef Bacik48461132016-09-28 10:54:32 -04001888 */
Edward Creef1174f72017-08-07 15:26:19 +01001889 dst_reg->type = ptr_reg->type;
1890 dst_reg->id = ptr_reg->id;
Josef Bacikf23cc642016-11-14 15:45:36 -05001891
Josef Bacik48461132016-09-28 10:54:32 -04001892 switch (opcode) {
1893 case BPF_ADD:
Edward Creef1174f72017-08-07 15:26:19 +01001894 /* We can take a fixed offset as long as it doesn't overflow
1895 * the s32 'off' field
1896 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001897 if (known && (ptr_reg->off + smin_val ==
1898 (s64)(s32)(ptr_reg->off + smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01001899 /* pointer += K. Accumulate it into fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01001900 dst_reg->smin_value = smin_ptr;
1901 dst_reg->smax_value = smax_ptr;
1902 dst_reg->umin_value = umin_ptr;
1903 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01001904 dst_reg->var_off = ptr_reg->var_off;
Edward Creeb03c9f92017-08-07 15:26:36 +01001905 dst_reg->off = ptr_reg->off + smin_val;
Edward Creef1174f72017-08-07 15:26:19 +01001906 dst_reg->range = ptr_reg->range;
1907 break;
1908 }
Edward Creef1174f72017-08-07 15:26:19 +01001909 /* A new variable offset is created. Note that off_reg->off
1910 * == 0, since it's a scalar.
1911 * dst_reg gets the pointer type and since some positive
1912 * integer value was added to the pointer, give it a new 'id'
1913 * if it's a PTR_TO_PACKET.
1914 * this creates a new 'base' pointer, off_reg (variable) gets
1915 * added into the variable offset, and we copy the fixed offset
1916 * from ptr_reg.
1917 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001918 if (signed_add_overflows(smin_ptr, smin_val) ||
1919 signed_add_overflows(smax_ptr, smax_val)) {
1920 dst_reg->smin_value = S64_MIN;
1921 dst_reg->smax_value = S64_MAX;
1922 } else {
1923 dst_reg->smin_value = smin_ptr + smin_val;
1924 dst_reg->smax_value = smax_ptr + smax_val;
1925 }
1926 if (umin_ptr + umin_val < umin_ptr ||
1927 umax_ptr + umax_val < umax_ptr) {
1928 dst_reg->umin_value = 0;
1929 dst_reg->umax_value = U64_MAX;
1930 } else {
1931 dst_reg->umin_value = umin_ptr + umin_val;
1932 dst_reg->umax_value = umax_ptr + umax_val;
1933 }
Edward Creef1174f72017-08-07 15:26:19 +01001934 dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off);
1935 dst_reg->off = ptr_reg->off;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001936 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01001937 dst_reg->id = ++env->id_gen;
1938 /* something was added to pkt_ptr, set range to zero */
1939 dst_reg->range = 0;
1940 }
Josef Bacik48461132016-09-28 10:54:32 -04001941 break;
1942 case BPF_SUB:
Edward Creef1174f72017-08-07 15:26:19 +01001943 if (dst_reg == off_reg) {
1944 /* scalar -= pointer. Creates an unknown scalar */
1945 if (!env->allow_ptr_leaks)
1946 verbose("R%d tried to subtract pointer from scalar\n",
1947 dst);
1948 return -EACCES;
1949 }
1950 /* We don't allow subtraction from FP, because (according to
1951 * test_verifier.c test "invalid fp arithmetic", JITs might not
1952 * be able to deal with it.
Edward Cree93057062017-07-21 14:37:34 +01001953 */
Edward Creef1174f72017-08-07 15:26:19 +01001954 if (ptr_reg->type == PTR_TO_STACK) {
1955 if (!env->allow_ptr_leaks)
1956 verbose("R%d subtraction from stack pointer prohibited\n",
1957 dst);
1958 return -EACCES;
1959 }
Edward Creeb03c9f92017-08-07 15:26:36 +01001960 if (known && (ptr_reg->off - smin_val ==
1961 (s64)(s32)(ptr_reg->off - smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01001962 /* pointer -= K. Subtract it from fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01001963 dst_reg->smin_value = smin_ptr;
1964 dst_reg->smax_value = smax_ptr;
1965 dst_reg->umin_value = umin_ptr;
1966 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01001967 dst_reg->var_off = ptr_reg->var_off;
1968 dst_reg->id = ptr_reg->id;
Edward Creeb03c9f92017-08-07 15:26:36 +01001969 dst_reg->off = ptr_reg->off - smin_val;
Edward Creef1174f72017-08-07 15:26:19 +01001970 dst_reg->range = ptr_reg->range;
1971 break;
1972 }
Edward Creef1174f72017-08-07 15:26:19 +01001973 /* A new variable offset is created. If the subtrahend is known
1974 * nonnegative, then any reg->range we had before is still good.
1975 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001976 if (signed_sub_overflows(smin_ptr, smax_val) ||
1977 signed_sub_overflows(smax_ptr, smin_val)) {
1978 /* Overflow possible, we know nothing */
1979 dst_reg->smin_value = S64_MIN;
1980 dst_reg->smax_value = S64_MAX;
1981 } else {
1982 dst_reg->smin_value = smin_ptr - smax_val;
1983 dst_reg->smax_value = smax_ptr - smin_val;
1984 }
1985 if (umin_ptr < umax_val) {
1986 /* Overflow possible, we know nothing */
1987 dst_reg->umin_value = 0;
1988 dst_reg->umax_value = U64_MAX;
1989 } else {
1990 /* Cannot overflow (as long as bounds are consistent) */
1991 dst_reg->umin_value = umin_ptr - umax_val;
1992 dst_reg->umax_value = umax_ptr - umin_val;
1993 }
Edward Creef1174f72017-08-07 15:26:19 +01001994 dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off);
1995 dst_reg->off = ptr_reg->off;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001996 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01001997 dst_reg->id = ++env->id_gen;
1998 /* something was added to pkt_ptr, set range to zero */
Edward Creeb03c9f92017-08-07 15:26:36 +01001999 if (smin_val < 0)
Edward Creef1174f72017-08-07 15:26:19 +01002000 dst_reg->range = 0;
2001 }
2002 break;
2003 case BPF_AND:
2004 case BPF_OR:
2005 case BPF_XOR:
2006 /* bitwise ops on pointers are troublesome, prohibit for now.
2007 * (However, in principle we could allow some cases, e.g.
2008 * ptr &= ~3 which would reduce min_value by 3.)
2009 */
2010 if (!env->allow_ptr_leaks)
2011 verbose("R%d bitwise operator %s on pointer prohibited\n",
2012 dst, bpf_alu_string[opcode >> 4]);
2013 return -EACCES;
2014 default:
2015 /* other operators (e.g. MUL,LSH) produce non-pointer results */
2016 if (!env->allow_ptr_leaks)
2017 verbose("R%d pointer arithmetic with %s operator prohibited\n",
2018 dst, bpf_alu_string[opcode >> 4]);
2019 return -EACCES;
2020 }
2021
Edward Creeb03c9f92017-08-07 15:26:36 +01002022 __update_reg_bounds(dst_reg);
2023 __reg_deduce_bounds(dst_reg);
2024 __reg_bound_offset(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002025 return 0;
2026}
2027
2028static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
2029 struct bpf_insn *insn,
2030 struct bpf_reg_state *dst_reg,
2031 struct bpf_reg_state src_reg)
2032{
2033 struct bpf_reg_state *regs = env->cur_state.regs;
Edward Creef1174f72017-08-07 15:26:19 +01002034 u8 opcode = BPF_OP(insn->code);
2035 bool src_known, dst_known;
Edward Creeb03c9f92017-08-07 15:26:36 +01002036 s64 smin_val, smax_val;
2037 u64 umin_val, umax_val;
Edward Creef1174f72017-08-07 15:26:19 +01002038
2039 if (BPF_CLASS(insn->code) != BPF_ALU64) {
2040 /* 32-bit ALU ops are (32,32)->64 */
2041 coerce_reg_to_32(dst_reg);
2042 coerce_reg_to_32(&src_reg);
2043 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002044 smin_val = src_reg.smin_value;
2045 smax_val = src_reg.smax_value;
2046 umin_val = src_reg.umin_value;
2047 umax_val = src_reg.umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01002048 src_known = tnum_is_const(src_reg.var_off);
2049 dst_known = tnum_is_const(dst_reg->var_off);
2050
2051 switch (opcode) {
2052 case BPF_ADD:
Edward Creeb03c9f92017-08-07 15:26:36 +01002053 if (signed_add_overflows(dst_reg->smin_value, smin_val) ||
2054 signed_add_overflows(dst_reg->smax_value, smax_val)) {
2055 dst_reg->smin_value = S64_MIN;
2056 dst_reg->smax_value = S64_MAX;
2057 } else {
2058 dst_reg->smin_value += smin_val;
2059 dst_reg->smax_value += smax_val;
2060 }
2061 if (dst_reg->umin_value + umin_val < umin_val ||
2062 dst_reg->umax_value + umax_val < umax_val) {
2063 dst_reg->umin_value = 0;
2064 dst_reg->umax_value = U64_MAX;
2065 } else {
2066 dst_reg->umin_value += umin_val;
2067 dst_reg->umax_value += umax_val;
2068 }
Edward Creef1174f72017-08-07 15:26:19 +01002069 dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off);
2070 break;
2071 case BPF_SUB:
Edward Creeb03c9f92017-08-07 15:26:36 +01002072 if (signed_sub_overflows(dst_reg->smin_value, smax_val) ||
2073 signed_sub_overflows(dst_reg->smax_value, smin_val)) {
2074 /* Overflow possible, we know nothing */
2075 dst_reg->smin_value = S64_MIN;
2076 dst_reg->smax_value = S64_MAX;
2077 } else {
2078 dst_reg->smin_value -= smax_val;
2079 dst_reg->smax_value -= smin_val;
2080 }
2081 if (dst_reg->umin_value < umax_val) {
2082 /* Overflow possible, we know nothing */
2083 dst_reg->umin_value = 0;
2084 dst_reg->umax_value = U64_MAX;
2085 } else {
2086 /* Cannot overflow (as long as bounds are consistent) */
2087 dst_reg->umin_value -= umax_val;
2088 dst_reg->umax_value -= umin_val;
2089 }
Edward Creef1174f72017-08-07 15:26:19 +01002090 dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off);
Josef Bacik48461132016-09-28 10:54:32 -04002091 break;
2092 case BPF_MUL:
Edward Creeb03c9f92017-08-07 15:26:36 +01002093 dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off);
2094 if (smin_val < 0 || dst_reg->smin_value < 0) {
Edward Creef1174f72017-08-07 15:26:19 +01002095 /* Ain't nobody got time to multiply that sign */
Edward Creeb03c9f92017-08-07 15:26:36 +01002096 __mark_reg_unbounded(dst_reg);
2097 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002098 break;
2099 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002100 /* Both values are positive, so we can work with unsigned and
2101 * copy the result to signed (unless it exceeds S64_MAX).
Edward Creef1174f72017-08-07 15:26:19 +01002102 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002103 if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) {
2104 /* Potential overflow, we know nothing */
2105 __mark_reg_unbounded(dst_reg);
2106 /* (except what we can learn from the var_off) */
2107 __update_reg_bounds(dst_reg);
2108 break;
2109 }
2110 dst_reg->umin_value *= umin_val;
2111 dst_reg->umax_value *= umax_val;
2112 if (dst_reg->umax_value > S64_MAX) {
2113 /* Overflow possible, we know nothing */
2114 dst_reg->smin_value = S64_MIN;
2115 dst_reg->smax_value = S64_MAX;
2116 } else {
2117 dst_reg->smin_value = dst_reg->umin_value;
2118 dst_reg->smax_value = dst_reg->umax_value;
2119 }
Josef Bacik48461132016-09-28 10:54:32 -04002120 break;
2121 case BPF_AND:
Edward Creef1174f72017-08-07 15:26:19 +01002122 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01002123 __mark_reg_known(dst_reg, dst_reg->var_off.value &
2124 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01002125 break;
2126 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002127 /* We get our minimum from the var_off, since that's inherently
2128 * bitwise. Our maximum is the minimum of the operands' maxima.
Josef Bacikf23cc642016-11-14 15:45:36 -05002129 */
Edward Creef1174f72017-08-07 15:26:19 +01002130 dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01002131 dst_reg->umin_value = dst_reg->var_off.value;
2132 dst_reg->umax_value = min(dst_reg->umax_value, umax_val);
2133 if (dst_reg->smin_value < 0 || smin_val < 0) {
2134 /* Lose signed bounds when ANDing negative numbers,
2135 * ain't nobody got time for that.
2136 */
2137 dst_reg->smin_value = S64_MIN;
2138 dst_reg->smax_value = S64_MAX;
2139 } else {
2140 /* ANDing two positives gives a positive, so safe to
2141 * cast result into s64.
2142 */
2143 dst_reg->smin_value = dst_reg->umin_value;
2144 dst_reg->smax_value = dst_reg->umax_value;
2145 }
2146 /* We may learn something more from the var_off */
2147 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002148 break;
2149 case BPF_OR:
2150 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01002151 __mark_reg_known(dst_reg, dst_reg->var_off.value |
2152 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01002153 break;
2154 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002155 /* We get our maximum from the var_off, and our minimum is the
2156 * maximum of the operands' minima
Edward Creef1174f72017-08-07 15:26:19 +01002157 */
2158 dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01002159 dst_reg->umin_value = max(dst_reg->umin_value, umin_val);
2160 dst_reg->umax_value = dst_reg->var_off.value |
2161 dst_reg->var_off.mask;
2162 if (dst_reg->smin_value < 0 || smin_val < 0) {
2163 /* Lose signed bounds when ORing negative numbers,
2164 * ain't nobody got time for that.
2165 */
2166 dst_reg->smin_value = S64_MIN;
2167 dst_reg->smax_value = S64_MAX;
Edward Creef1174f72017-08-07 15:26:19 +01002168 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01002169 /* ORing two positives gives a positive, so safe to
2170 * cast result into s64.
2171 */
2172 dst_reg->smin_value = dst_reg->umin_value;
2173 dst_reg->smax_value = dst_reg->umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01002174 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002175 /* We may learn something more from the var_off */
2176 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002177 break;
2178 case BPF_LSH:
Edward Creeb03c9f92017-08-07 15:26:36 +01002179 if (umax_val > 63) {
2180 /* Shifts greater than 63 are undefined. This includes
2181 * shifts by a negative number.
2182 */
Edward Creef1174f72017-08-07 15:26:19 +01002183 mark_reg_unknown(regs, insn->dst_reg);
2184 break;
2185 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002186 /* We lose all sign bit information (except what we can pick
2187 * up from var_off)
Josef Bacik48461132016-09-28 10:54:32 -04002188 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002189 dst_reg->smin_value = S64_MIN;
2190 dst_reg->smax_value = S64_MAX;
2191 /* If we might shift our top bit out, then we know nothing */
2192 if (dst_reg->umax_value > 1ULL << (63 - umax_val)) {
2193 dst_reg->umin_value = 0;
2194 dst_reg->umax_value = U64_MAX;
David S. Millerd1174412017-05-10 11:22:52 -07002195 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01002196 dst_reg->umin_value <<= umin_val;
2197 dst_reg->umax_value <<= umax_val;
David S. Millerd1174412017-05-10 11:22:52 -07002198 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002199 if (src_known)
2200 dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val);
2201 else
2202 dst_reg->var_off = tnum_lshift(tnum_unknown, umin_val);
2203 /* We may learn something more from the var_off */
2204 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002205 break;
2206 case BPF_RSH:
Edward Creeb03c9f92017-08-07 15:26:36 +01002207 if (umax_val > 63) {
2208 /* Shifts greater than 63 are undefined. This includes
2209 * shifts by a negative number.
2210 */
Edward Creef1174f72017-08-07 15:26:19 +01002211 mark_reg_unknown(regs, insn->dst_reg);
2212 break;
2213 }
2214 /* BPF_RSH is an unsigned shift, so make the appropriate casts */
Edward Creeb03c9f92017-08-07 15:26:36 +01002215 if (dst_reg->smin_value < 0) {
2216 if (umin_val) {
Edward Creef1174f72017-08-07 15:26:19 +01002217 /* Sign bit will be cleared */
Edward Creeb03c9f92017-08-07 15:26:36 +01002218 dst_reg->smin_value = 0;
2219 } else {
2220 /* Lost sign bit information */
2221 dst_reg->smin_value = S64_MIN;
2222 dst_reg->smax_value = S64_MAX;
2223 }
David S. Millerd1174412017-05-10 11:22:52 -07002224 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01002225 dst_reg->smin_value =
2226 (u64)(dst_reg->smin_value) >> umax_val;
David S. Millerd1174412017-05-10 11:22:52 -07002227 }
Edward Creef1174f72017-08-07 15:26:19 +01002228 if (src_known)
Edward Creeb03c9f92017-08-07 15:26:36 +01002229 dst_reg->var_off = tnum_rshift(dst_reg->var_off,
2230 umin_val);
Edward Creef1174f72017-08-07 15:26:19 +01002231 else
Edward Creeb03c9f92017-08-07 15:26:36 +01002232 dst_reg->var_off = tnum_rshift(tnum_unknown, umin_val);
2233 dst_reg->umin_value >>= umax_val;
2234 dst_reg->umax_value >>= umin_val;
2235 /* We may learn something more from the var_off */
2236 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002237 break;
2238 default:
Edward Creef1174f72017-08-07 15:26:19 +01002239 mark_reg_unknown(regs, insn->dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002240 break;
2241 }
2242
Edward Creeb03c9f92017-08-07 15:26:36 +01002243 __reg_deduce_bounds(dst_reg);
2244 __reg_bound_offset(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002245 return 0;
2246}
2247
2248/* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max
2249 * and var_off.
2250 */
2251static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
2252 struct bpf_insn *insn)
2253{
2254 struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg, *src_reg;
2255 struct bpf_reg_state *ptr_reg = NULL, off_reg = {0};
2256 u8 opcode = BPF_OP(insn->code);
2257 int rc;
2258
2259 dst_reg = &regs[insn->dst_reg];
Edward Creef1174f72017-08-07 15:26:19 +01002260 src_reg = NULL;
2261 if (dst_reg->type != SCALAR_VALUE)
2262 ptr_reg = dst_reg;
2263 if (BPF_SRC(insn->code) == BPF_X) {
2264 src_reg = &regs[insn->src_reg];
Edward Creef1174f72017-08-07 15:26:19 +01002265 if (src_reg->type != SCALAR_VALUE) {
2266 if (dst_reg->type != SCALAR_VALUE) {
2267 /* Combining two pointers by any ALU op yields
2268 * an arbitrary scalar.
2269 */
2270 if (!env->allow_ptr_leaks) {
2271 verbose("R%d pointer %s pointer prohibited\n",
2272 insn->dst_reg,
2273 bpf_alu_string[opcode >> 4]);
2274 return -EACCES;
2275 }
2276 mark_reg_unknown(regs, insn->dst_reg);
2277 return 0;
2278 } else {
2279 /* scalar += pointer
2280 * This is legal, but we have to reverse our
2281 * src/dest handling in computing the range
2282 */
2283 rc = adjust_ptr_min_max_vals(env, insn,
2284 src_reg, dst_reg);
2285 if (rc == -EACCES && env->allow_ptr_leaks) {
2286 /* scalar += unknown scalar */
2287 __mark_reg_unknown(&off_reg);
2288 return adjust_scalar_min_max_vals(
2289 env, insn,
2290 dst_reg, off_reg);
2291 }
2292 return rc;
2293 }
2294 } else if (ptr_reg) {
2295 /* pointer += scalar */
2296 rc = adjust_ptr_min_max_vals(env, insn,
2297 dst_reg, src_reg);
2298 if (rc == -EACCES && env->allow_ptr_leaks) {
2299 /* unknown scalar += scalar */
2300 __mark_reg_unknown(dst_reg);
2301 return adjust_scalar_min_max_vals(
2302 env, insn, dst_reg, *src_reg);
2303 }
2304 return rc;
2305 }
2306 } else {
2307 /* Pretend the src is a reg with a known value, since we only
2308 * need to be able to read from this state.
2309 */
2310 off_reg.type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01002311 __mark_reg_known(&off_reg, insn->imm);
Edward Creef1174f72017-08-07 15:26:19 +01002312 src_reg = &off_reg;
Edward Creef1174f72017-08-07 15:26:19 +01002313 if (ptr_reg) { /* pointer += K */
2314 rc = adjust_ptr_min_max_vals(env, insn,
2315 ptr_reg, src_reg);
2316 if (rc == -EACCES && env->allow_ptr_leaks) {
2317 /* unknown scalar += K */
2318 __mark_reg_unknown(dst_reg);
2319 return adjust_scalar_min_max_vals(
2320 env, insn, dst_reg, off_reg);
2321 }
2322 return rc;
2323 }
2324 }
2325
2326 /* Got here implies adding two SCALAR_VALUEs */
2327 if (WARN_ON_ONCE(ptr_reg)) {
2328 print_verifier_state(&env->cur_state);
2329 verbose("verifier internal error: unexpected ptr_reg\n");
2330 return -EINVAL;
2331 }
2332 if (WARN_ON(!src_reg)) {
2333 print_verifier_state(&env->cur_state);
2334 verbose("verifier internal error: no src_reg\n");
2335 return -EINVAL;
2336 }
2337 return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002338}
2339
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002340/* check validity of 32-bit and 64-bit arithmetic operations */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002341static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002342{
Edward Creef1174f72017-08-07 15:26:19 +01002343 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002344 u8 opcode = BPF_OP(insn->code);
2345 int err;
2346
2347 if (opcode == BPF_END || opcode == BPF_NEG) {
2348 if (opcode == BPF_NEG) {
2349 if (BPF_SRC(insn->code) != 0 ||
2350 insn->src_reg != BPF_REG_0 ||
2351 insn->off != 0 || insn->imm != 0) {
2352 verbose("BPF_NEG uses reserved fields\n");
2353 return -EINVAL;
2354 }
2355 } else {
2356 if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
Edward Creee67b8a62017-09-15 14:37:38 +01002357 (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) ||
2358 BPF_CLASS(insn->code) == BPF_ALU64) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002359 verbose("BPF_END uses reserved fields\n");
2360 return -EINVAL;
2361 }
2362 }
2363
2364 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01002365 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002366 if (err)
2367 return err;
2368
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002369 if (is_pointer_value(env, insn->dst_reg)) {
2370 verbose("R%d pointer arithmetic prohibited\n",
2371 insn->dst_reg);
2372 return -EACCES;
2373 }
2374
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002375 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01002376 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002377 if (err)
2378 return err;
2379
2380 } else if (opcode == BPF_MOV) {
2381
2382 if (BPF_SRC(insn->code) == BPF_X) {
2383 if (insn->imm != 0 || insn->off != 0) {
2384 verbose("BPF_MOV uses reserved fields\n");
2385 return -EINVAL;
2386 }
2387
2388 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01002389 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002390 if (err)
2391 return err;
2392 } else {
2393 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
2394 verbose("BPF_MOV uses reserved fields\n");
2395 return -EINVAL;
2396 }
2397 }
2398
2399 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01002400 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002401 if (err)
2402 return err;
2403
2404 if (BPF_SRC(insn->code) == BPF_X) {
2405 if (BPF_CLASS(insn->code) == BPF_ALU64) {
2406 /* case: R1 = R2
2407 * copy register state to dest reg
2408 */
2409 regs[insn->dst_reg] = regs[insn->src_reg];
Alexei Starovoitov8fe2d6c2017-10-05 16:20:56 -07002410 regs[insn->dst_reg].live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002411 } else {
Edward Creef1174f72017-08-07 15:26:19 +01002412 /* R1 = (u32) R2 */
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002413 if (is_pointer_value(env, insn->src_reg)) {
2414 verbose("R%d partial copy of pointer\n",
2415 insn->src_reg);
2416 return -EACCES;
2417 }
Edward Creef1174f72017-08-07 15:26:19 +01002418 mark_reg_unknown(regs, insn->dst_reg);
Edward Creeb03c9f92017-08-07 15:26:36 +01002419 /* high 32 bits are known zero. */
Edward Creef1174f72017-08-07 15:26:19 +01002420 regs[insn->dst_reg].var_off = tnum_cast(
2421 regs[insn->dst_reg].var_off, 4);
Edward Creeb03c9f92017-08-07 15:26:36 +01002422 __update_reg_bounds(&regs[insn->dst_reg]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002423 }
2424 } else {
2425 /* case: R = imm
2426 * remember the value we stored into this reg
2427 */
Edward Creef1174f72017-08-07 15:26:19 +01002428 regs[insn->dst_reg].type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01002429 __mark_reg_known(regs + insn->dst_reg, insn->imm);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002430 }
2431
2432 } else if (opcode > BPF_END) {
2433 verbose("invalid BPF_ALU opcode %x\n", opcode);
2434 return -EINVAL;
2435
2436 } else { /* all other ALU ops: and, sub, xor, add, ... */
2437
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002438 if (BPF_SRC(insn->code) == BPF_X) {
2439 if (insn->imm != 0 || insn->off != 0) {
2440 verbose("BPF_ALU uses reserved fields\n");
2441 return -EINVAL;
2442 }
2443 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01002444 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002445 if (err)
2446 return err;
2447 } else {
2448 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
2449 verbose("BPF_ALU uses reserved fields\n");
2450 return -EINVAL;
2451 }
2452 }
2453
2454 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01002455 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002456 if (err)
2457 return err;
2458
2459 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
2460 BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
2461 verbose("div by zero\n");
2462 return -EINVAL;
2463 }
2464
Rabin Vincent229394e82016-01-12 20:17:08 +01002465 if ((opcode == BPF_LSH || opcode == BPF_RSH ||
2466 opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
2467 int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
2468
2469 if (insn->imm < 0 || insn->imm >= size) {
2470 verbose("invalid shift %d\n", insn->imm);
2471 return -EINVAL;
2472 }
2473 }
2474
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002475 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01002476 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002477 if (err)
2478 return err;
2479
Edward Creef1174f72017-08-07 15:26:19 +01002480 return adjust_reg_min_max_vals(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002481 }
2482
2483 return 0;
2484}
2485
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002486static void find_good_pkt_pointers(struct bpf_verifier_state *state,
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002487 struct bpf_reg_state *dst_reg,
2488 enum bpf_reg_type type)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002489{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002490 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002491 int i;
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002492
Edward Creef1174f72017-08-07 15:26:19 +01002493 if (dst_reg->off < 0)
2494 /* This doesn't give us any range */
2495 return;
2496
Edward Creeb03c9f92017-08-07 15:26:36 +01002497 if (dst_reg->umax_value > MAX_PACKET_OFF ||
2498 dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF)
Edward Creef1174f72017-08-07 15:26:19 +01002499 /* Risk of overflow. For instance, ptr + (1<<63) may be less
2500 * than pkt_end, but that's because it's also less than pkt.
2501 */
2502 return;
2503
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02002504 /* LLVM can generate four kind of checks:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002505 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02002506 * Type 1/2:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002507 *
2508 * r2 = r3;
2509 * r2 += 8;
2510 * if (r2 > pkt_end) goto <handle exception>
2511 * <access okay>
2512 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02002513 * r2 = r3;
2514 * r2 += 8;
2515 * if (r2 < pkt_end) goto <access okay>
2516 * <handle exception>
2517 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002518 * Where:
2519 * r2 == dst_reg, pkt_end == src_reg
2520 * r2=pkt(id=n,off=8,r=0)
2521 * r3=pkt(id=n,off=0,r=0)
2522 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02002523 * Type 3/4:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002524 *
2525 * r2 = r3;
2526 * r2 += 8;
2527 * if (pkt_end >= r2) goto <access okay>
2528 * <handle exception>
2529 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02002530 * r2 = r3;
2531 * r2 += 8;
2532 * if (pkt_end <= r2) goto <handle exception>
2533 * <access okay>
2534 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002535 * Where:
2536 * pkt_end == dst_reg, r2 == src_reg
2537 * r2=pkt(id=n,off=8,r=0)
2538 * r3=pkt(id=n,off=0,r=0)
2539 *
2540 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
2541 * so that range of bytes [r3, r3 + 8) is safe to access.
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002542 */
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002543
Edward Creef1174f72017-08-07 15:26:19 +01002544 /* If our ids match, then we must have the same max_value. And we
2545 * don't care about the other reg's fixed offset, since if it's too big
2546 * the range won't allow anything.
2547 * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16.
2548 */
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002549 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002550 if (regs[i].type == type && regs[i].id == dst_reg->id)
Alexei Starovoitovb1977682017-03-24 15:57:33 -07002551 /* keep the maximum range already checked */
Edward Creef1174f72017-08-07 15:26:19 +01002552 regs[i].range = max_t(u16, regs[i].range, dst_reg->off);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002553
2554 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
2555 if (state->stack_slot_type[i] != STACK_SPILL)
2556 continue;
2557 reg = &state->spilled_regs[i / BPF_REG_SIZE];
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002558 if (reg->type == type && reg->id == dst_reg->id)
Edward Creef1174f72017-08-07 15:26:19 +01002559 reg->range = max_t(u16, reg->range, dst_reg->off);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002560 }
2561}
2562
Josef Bacik48461132016-09-28 10:54:32 -04002563/* Adjusts the register min/max values in the case that the dst_reg is the
2564 * variable register that we are working on, and src_reg is a constant or we're
2565 * simply doing a BPF_K check.
Edward Creef1174f72017-08-07 15:26:19 +01002566 * In JEQ/JNE cases we also adjust the var_off values.
Josef Bacik48461132016-09-28 10:54:32 -04002567 */
2568static void reg_set_min_max(struct bpf_reg_state *true_reg,
2569 struct bpf_reg_state *false_reg, u64 val,
2570 u8 opcode)
2571{
Edward Creef1174f72017-08-07 15:26:19 +01002572 /* If the dst_reg is a pointer, we can't learn anything about its
2573 * variable offset from the compare (unless src_reg were a pointer into
2574 * the same object, but we don't bother with that.
2575 * Since false_reg and true_reg have the same type by construction, we
2576 * only need to check one of them for pointerness.
2577 */
2578 if (__is_pointer_value(false, false_reg))
2579 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02002580
Josef Bacik48461132016-09-28 10:54:32 -04002581 switch (opcode) {
2582 case BPF_JEQ:
2583 /* If this is false then we know nothing Jon Snow, but if it is
2584 * true then we know for sure.
2585 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002586 __mark_reg_known(true_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04002587 break;
2588 case BPF_JNE:
2589 /* If this is true we know nothing Jon Snow, but if it is false
2590 * we know the value for sure;
2591 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002592 __mark_reg_known(false_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04002593 break;
2594 case BPF_JGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01002595 false_reg->umax_value = min(false_reg->umax_value, val);
2596 true_reg->umin_value = max(true_reg->umin_value, val + 1);
2597 break;
Josef Bacik48461132016-09-28 10:54:32 -04002598 case BPF_JSGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01002599 false_reg->smax_value = min_t(s64, false_reg->smax_value, val);
2600 true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1);
Josef Bacik48461132016-09-28 10:54:32 -04002601 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02002602 case BPF_JLT:
2603 false_reg->umin_value = max(false_reg->umin_value, val);
2604 true_reg->umax_value = min(true_reg->umax_value, val - 1);
2605 break;
2606 case BPF_JSLT:
2607 false_reg->smin_value = max_t(s64, false_reg->smin_value, val);
2608 true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1);
2609 break;
Josef Bacik48461132016-09-28 10:54:32 -04002610 case BPF_JGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01002611 false_reg->umax_value = min(false_reg->umax_value, val - 1);
2612 true_reg->umin_value = max(true_reg->umin_value, val);
2613 break;
Josef Bacik48461132016-09-28 10:54:32 -04002614 case BPF_JSGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01002615 false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1);
2616 true_reg->smin_value = max_t(s64, true_reg->smin_value, val);
Josef Bacik48461132016-09-28 10:54:32 -04002617 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02002618 case BPF_JLE:
2619 false_reg->umin_value = max(false_reg->umin_value, val + 1);
2620 true_reg->umax_value = min(true_reg->umax_value, val);
2621 break;
2622 case BPF_JSLE:
2623 false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1);
2624 true_reg->smax_value = min_t(s64, true_reg->smax_value, val);
2625 break;
Josef Bacik48461132016-09-28 10:54:32 -04002626 default:
2627 break;
2628 }
2629
Edward Creeb03c9f92017-08-07 15:26:36 +01002630 __reg_deduce_bounds(false_reg);
2631 __reg_deduce_bounds(true_reg);
2632 /* We might have learned some bits from the bounds. */
2633 __reg_bound_offset(false_reg);
2634 __reg_bound_offset(true_reg);
2635 /* Intersecting with the old var_off might have improved our bounds
2636 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
2637 * then new var_off is (0; 0x7f...fc) which improves our umax.
2638 */
2639 __update_reg_bounds(false_reg);
2640 __update_reg_bounds(true_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002641}
2642
Edward Creef1174f72017-08-07 15:26:19 +01002643/* Same as above, but for the case that dst_reg holds a constant and src_reg is
2644 * the variable reg.
Josef Bacik48461132016-09-28 10:54:32 -04002645 */
2646static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
2647 struct bpf_reg_state *false_reg, u64 val,
2648 u8 opcode)
2649{
Edward Creef1174f72017-08-07 15:26:19 +01002650 if (__is_pointer_value(false, false_reg))
2651 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02002652
Josef Bacik48461132016-09-28 10:54:32 -04002653 switch (opcode) {
2654 case BPF_JEQ:
2655 /* If this is false then we know nothing Jon Snow, but if it is
2656 * true then we know for sure.
2657 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002658 __mark_reg_known(true_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04002659 break;
2660 case BPF_JNE:
2661 /* If this is true we know nothing Jon Snow, but if it is false
2662 * we know the value for sure;
2663 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002664 __mark_reg_known(false_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04002665 break;
2666 case BPF_JGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01002667 true_reg->umax_value = min(true_reg->umax_value, val - 1);
2668 false_reg->umin_value = max(false_reg->umin_value, val);
2669 break;
Josef Bacik48461132016-09-28 10:54:32 -04002670 case BPF_JSGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01002671 true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1);
2672 false_reg->smin_value = max_t(s64, false_reg->smin_value, val);
Josef Bacik48461132016-09-28 10:54:32 -04002673 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02002674 case BPF_JLT:
2675 true_reg->umin_value = max(true_reg->umin_value, val + 1);
2676 false_reg->umax_value = min(false_reg->umax_value, val);
2677 break;
2678 case BPF_JSLT:
2679 true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1);
2680 false_reg->smax_value = min_t(s64, false_reg->smax_value, val);
2681 break;
Josef Bacik48461132016-09-28 10:54:32 -04002682 case BPF_JGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01002683 true_reg->umax_value = min(true_reg->umax_value, val);
2684 false_reg->umin_value = max(false_reg->umin_value, val + 1);
2685 break;
Josef Bacik48461132016-09-28 10:54:32 -04002686 case BPF_JSGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01002687 true_reg->smax_value = min_t(s64, true_reg->smax_value, val);
2688 false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1);
Josef Bacik48461132016-09-28 10:54:32 -04002689 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02002690 case BPF_JLE:
2691 true_reg->umin_value = max(true_reg->umin_value, val);
2692 false_reg->umax_value = min(false_reg->umax_value, val - 1);
2693 break;
2694 case BPF_JSLE:
2695 true_reg->smin_value = max_t(s64, true_reg->smin_value, val);
2696 false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1);
2697 break;
Josef Bacik48461132016-09-28 10:54:32 -04002698 default:
2699 break;
2700 }
2701
Edward Creeb03c9f92017-08-07 15:26:36 +01002702 __reg_deduce_bounds(false_reg);
2703 __reg_deduce_bounds(true_reg);
2704 /* We might have learned some bits from the bounds. */
2705 __reg_bound_offset(false_reg);
2706 __reg_bound_offset(true_reg);
2707 /* Intersecting with the old var_off might have improved our bounds
2708 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
2709 * then new var_off is (0; 0x7f...fc) which improves our umax.
2710 */
2711 __update_reg_bounds(false_reg);
2712 __update_reg_bounds(true_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002713}
2714
2715/* Regs are known to be equal, so intersect their min/max/var_off */
2716static void __reg_combine_min_max(struct bpf_reg_state *src_reg,
2717 struct bpf_reg_state *dst_reg)
2718{
Edward Creeb03c9f92017-08-07 15:26:36 +01002719 src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value,
2720 dst_reg->umin_value);
2721 src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value,
2722 dst_reg->umax_value);
2723 src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value,
2724 dst_reg->smin_value);
2725 src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value,
2726 dst_reg->smax_value);
Edward Creef1174f72017-08-07 15:26:19 +01002727 src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off,
2728 dst_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01002729 /* We might have learned new bounds from the var_off. */
2730 __update_reg_bounds(src_reg);
2731 __update_reg_bounds(dst_reg);
2732 /* We might have learned something about the sign bit. */
2733 __reg_deduce_bounds(src_reg);
2734 __reg_deduce_bounds(dst_reg);
2735 /* We might have learned some bits from the bounds. */
2736 __reg_bound_offset(src_reg);
2737 __reg_bound_offset(dst_reg);
2738 /* Intersecting with the old var_off might have improved our bounds
2739 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
2740 * then new var_off is (0; 0x7f...fc) which improves our umax.
2741 */
2742 __update_reg_bounds(src_reg);
2743 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002744}
2745
2746static void reg_combine_min_max(struct bpf_reg_state *true_src,
2747 struct bpf_reg_state *true_dst,
2748 struct bpf_reg_state *false_src,
2749 struct bpf_reg_state *false_dst,
2750 u8 opcode)
2751{
2752 switch (opcode) {
2753 case BPF_JEQ:
2754 __reg_combine_min_max(true_src, true_dst);
2755 break;
2756 case BPF_JNE:
2757 __reg_combine_min_max(false_src, false_dst);
Edward Creeb03c9f92017-08-07 15:26:36 +01002758 break;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02002759 }
Josef Bacik48461132016-09-28 10:54:32 -04002760}
2761
Thomas Graf57a09bf2016-10-18 19:51:19 +02002762static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id,
Edward Creef1174f72017-08-07 15:26:19 +01002763 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02002764{
2765 struct bpf_reg_state *reg = &regs[regno];
2766
2767 if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) {
Edward Creef1174f72017-08-07 15:26:19 +01002768 /* Old offset (both fixed and variable parts) should
2769 * have been known-zero, because we don't allow pointer
2770 * arithmetic on pointers that might be NULL.
2771 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002772 if (WARN_ON_ONCE(reg->smin_value || reg->smax_value ||
2773 !tnum_equals_const(reg->var_off, 0) ||
Edward Creef1174f72017-08-07 15:26:19 +01002774 reg->off)) {
Edward Creeb03c9f92017-08-07 15:26:36 +01002775 __mark_reg_known_zero(reg);
2776 reg->off = 0;
Edward Creef1174f72017-08-07 15:26:19 +01002777 }
2778 if (is_null) {
2779 reg->type = SCALAR_VALUE;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07002780 } else if (reg->map_ptr->inner_map_meta) {
2781 reg->type = CONST_PTR_TO_MAP;
2782 reg->map_ptr = reg->map_ptr->inner_map_meta;
2783 } else {
Edward Creef1174f72017-08-07 15:26:19 +01002784 reg->type = PTR_TO_MAP_VALUE;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07002785 }
Daniel Borkmanna08dd0d2016-12-15 01:30:06 +01002786 /* We don't need id from this point onwards anymore, thus we
2787 * should better reset it, so that state pruning has chances
2788 * to take effect.
2789 */
2790 reg->id = 0;
Thomas Graf57a09bf2016-10-18 19:51:19 +02002791 }
2792}
2793
2794/* The logic is similar to find_good_pkt_pointers(), both could eventually
2795 * be folded together at some point.
2796 */
2797static void mark_map_regs(struct bpf_verifier_state *state, u32 regno,
Edward Creef1174f72017-08-07 15:26:19 +01002798 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02002799{
2800 struct bpf_reg_state *regs = state->regs;
Daniel Borkmanna08dd0d2016-12-15 01:30:06 +01002801 u32 id = regs[regno].id;
Thomas Graf57a09bf2016-10-18 19:51:19 +02002802 int i;
2803
2804 for (i = 0; i < MAX_BPF_REG; i++)
Edward Creef1174f72017-08-07 15:26:19 +01002805 mark_map_reg(regs, i, id, is_null);
Thomas Graf57a09bf2016-10-18 19:51:19 +02002806
2807 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
2808 if (state->stack_slot_type[i] != STACK_SPILL)
2809 continue;
Edward Creef1174f72017-08-07 15:26:19 +01002810 mark_map_reg(state->spilled_regs, i / BPF_REG_SIZE, id, is_null);
Thomas Graf57a09bf2016-10-18 19:51:19 +02002811 }
2812}
2813
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002814static int check_cond_jmp_op(struct bpf_verifier_env *env,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002815 struct bpf_insn *insn, int *insn_idx)
2816{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002817 struct bpf_verifier_state *other_branch, *this_branch = &env->cur_state;
2818 struct bpf_reg_state *regs = this_branch->regs, *dst_reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002819 u8 opcode = BPF_OP(insn->code);
2820 int err;
2821
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02002822 if (opcode > BPF_JSLE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002823 verbose("invalid BPF_JMP opcode %x\n", opcode);
2824 return -EINVAL;
2825 }
2826
2827 if (BPF_SRC(insn->code) == BPF_X) {
2828 if (insn->imm != 0) {
2829 verbose("BPF_JMP uses reserved fields\n");
2830 return -EINVAL;
2831 }
2832
2833 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01002834 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002835 if (err)
2836 return err;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002837
2838 if (is_pointer_value(env, insn->src_reg)) {
2839 verbose("R%d pointer comparison prohibited\n",
2840 insn->src_reg);
2841 return -EACCES;
2842 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002843 } else {
2844 if (insn->src_reg != BPF_REG_0) {
2845 verbose("BPF_JMP uses reserved fields\n");
2846 return -EINVAL;
2847 }
2848 }
2849
2850 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01002851 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002852 if (err)
2853 return err;
2854
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002855 dst_reg = &regs[insn->dst_reg];
2856
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002857 /* detect if R == 0 where R was initialized to zero earlier */
2858 if (BPF_SRC(insn->code) == BPF_K &&
2859 (opcode == BPF_JEQ || opcode == BPF_JNE) &&
Edward Creef1174f72017-08-07 15:26:19 +01002860 dst_reg->type == SCALAR_VALUE &&
2861 tnum_equals_const(dst_reg->var_off, insn->imm)) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002862 if (opcode == BPF_JEQ) {
2863 /* if (imm == imm) goto pc+off;
2864 * only follow the goto, ignore fall-through
2865 */
2866 *insn_idx += insn->off;
2867 return 0;
2868 } else {
2869 /* if (imm != imm) goto pc+off;
2870 * only follow fall-through branch, since
2871 * that's where the program will go
2872 */
2873 return 0;
2874 }
2875 }
2876
2877 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx);
2878 if (!other_branch)
2879 return -EFAULT;
2880
Josef Bacik48461132016-09-28 10:54:32 -04002881 /* detect if we are comparing against a constant value so we can adjust
2882 * our min/max values for our dst register.
Edward Creef1174f72017-08-07 15:26:19 +01002883 * this is only legit if both are scalars (or pointers to the same
2884 * object, I suppose, but we don't support that right now), because
2885 * otherwise the different base pointers mean the offsets aren't
2886 * comparable.
Josef Bacik48461132016-09-28 10:54:32 -04002887 */
2888 if (BPF_SRC(insn->code) == BPF_X) {
Edward Creef1174f72017-08-07 15:26:19 +01002889 if (dst_reg->type == SCALAR_VALUE &&
2890 regs[insn->src_reg].type == SCALAR_VALUE) {
2891 if (tnum_is_const(regs[insn->src_reg].var_off))
2892 reg_set_min_max(&other_branch->regs[insn->dst_reg],
2893 dst_reg, regs[insn->src_reg].var_off.value,
2894 opcode);
2895 else if (tnum_is_const(dst_reg->var_off))
2896 reg_set_min_max_inv(&other_branch->regs[insn->src_reg],
2897 &regs[insn->src_reg],
2898 dst_reg->var_off.value, opcode);
2899 else if (opcode == BPF_JEQ || opcode == BPF_JNE)
2900 /* Comparing for equality, we can combine knowledge */
2901 reg_combine_min_max(&other_branch->regs[insn->src_reg],
2902 &other_branch->regs[insn->dst_reg],
2903 &regs[insn->src_reg],
2904 &regs[insn->dst_reg], opcode);
2905 }
2906 } else if (dst_reg->type == SCALAR_VALUE) {
Josef Bacik48461132016-09-28 10:54:32 -04002907 reg_set_min_max(&other_branch->regs[insn->dst_reg],
2908 dst_reg, insn->imm, opcode);
2909 }
2910
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002911 /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002912 if (BPF_SRC(insn->code) == BPF_K &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002913 insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
2914 dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
Thomas Graf57a09bf2016-10-18 19:51:19 +02002915 /* Mark all identical map registers in each branch as either
2916 * safe or unknown depending R == 0 or R != 0 conditional.
2917 */
Edward Creef1174f72017-08-07 15:26:19 +01002918 mark_map_regs(this_branch, insn->dst_reg, opcode == BPF_JNE);
2919 mark_map_regs(other_branch, insn->dst_reg, opcode == BPF_JEQ);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002920 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT &&
2921 dst_reg->type == PTR_TO_PACKET &&
2922 regs[insn->src_reg].type == PTR_TO_PACKET_END) {
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002923 find_good_pkt_pointers(this_branch, dst_reg, PTR_TO_PACKET);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02002924 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JLT &&
2925 dst_reg->type == PTR_TO_PACKET &&
2926 regs[insn->src_reg].type == PTR_TO_PACKET_END) {
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002927 find_good_pkt_pointers(other_branch, dst_reg, PTR_TO_PACKET);
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002928 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGE &&
2929 dst_reg->type == PTR_TO_PACKET_END &&
2930 regs[insn->src_reg].type == PTR_TO_PACKET) {
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002931 find_good_pkt_pointers(other_branch, &regs[insn->src_reg],
2932 PTR_TO_PACKET);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02002933 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JLE &&
2934 dst_reg->type == PTR_TO_PACKET_END &&
2935 regs[insn->src_reg].type == PTR_TO_PACKET) {
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002936 find_good_pkt_pointers(this_branch, &regs[insn->src_reg],
2937 PTR_TO_PACKET);
2938 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT &&
2939 dst_reg->type == PTR_TO_PACKET_META &&
2940 reg_is_init_pkt_pointer(&regs[insn->src_reg], PTR_TO_PACKET)) {
2941 find_good_pkt_pointers(this_branch, dst_reg, PTR_TO_PACKET_META);
2942 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JLT &&
2943 dst_reg->type == PTR_TO_PACKET_META &&
2944 reg_is_init_pkt_pointer(&regs[insn->src_reg], PTR_TO_PACKET)) {
2945 find_good_pkt_pointers(other_branch, dst_reg, PTR_TO_PACKET_META);
2946 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGE &&
2947 reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
2948 regs[insn->src_reg].type == PTR_TO_PACKET_META) {
2949 find_good_pkt_pointers(other_branch, &regs[insn->src_reg],
2950 PTR_TO_PACKET_META);
2951 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JLE &&
2952 reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
2953 regs[insn->src_reg].type == PTR_TO_PACKET_META) {
2954 find_good_pkt_pointers(this_branch, &regs[insn->src_reg],
2955 PTR_TO_PACKET_META);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002956 } else if (is_pointer_value(env, insn->dst_reg)) {
2957 verbose("R%d pointer comparison prohibited\n", insn->dst_reg);
2958 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002959 }
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07002960 if (verifier_log.level)
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002961 print_verifier_state(this_branch);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002962 return 0;
2963}
2964
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002965/* return the map pointer stored inside BPF_LD_IMM64 instruction */
2966static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
2967{
2968 u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
2969
2970 return (struct bpf_map *) (unsigned long) imm64;
2971}
2972
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002973/* verify BPF_LD_IMM64 instruction */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002974static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002975{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002976 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002977 int err;
2978
2979 if (BPF_SIZE(insn->code) != BPF_DW) {
2980 verbose("invalid BPF_LD_IMM insn\n");
2981 return -EINVAL;
2982 }
2983 if (insn->off != 0) {
2984 verbose("BPF_LD_IMM64 uses reserved fields\n");
2985 return -EINVAL;
2986 }
2987
Edward Creedc503a82017-08-15 20:34:35 +01002988 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002989 if (err)
2990 return err;
2991
Jakub Kicinski6b173872016-09-21 11:43:59 +01002992 if (insn->src_reg == 0) {
Jakub Kicinski6b173872016-09-21 11:43:59 +01002993 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
2994
Edward Creef1174f72017-08-07 15:26:19 +01002995 regs[insn->dst_reg].type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01002996 __mark_reg_known(&regs[insn->dst_reg], imm);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002997 return 0;
Jakub Kicinski6b173872016-09-21 11:43:59 +01002998 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002999
3000 /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
3001 BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
3002
3003 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
3004 regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
3005 return 0;
3006}
3007
Daniel Borkmann96be4322015-03-01 12:31:46 +01003008static bool may_access_skb(enum bpf_prog_type type)
3009{
3010 switch (type) {
3011 case BPF_PROG_TYPE_SOCKET_FILTER:
3012 case BPF_PROG_TYPE_SCHED_CLS:
Daniel Borkmann94caee8c2015-03-20 15:11:11 +01003013 case BPF_PROG_TYPE_SCHED_ACT:
Daniel Borkmann96be4322015-03-01 12:31:46 +01003014 return true;
3015 default:
3016 return false;
3017 }
3018}
3019
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003020/* verify safety of LD_ABS|LD_IND instructions:
3021 * - they can only appear in the programs where ctx == skb
3022 * - since they are wrappers of function calls, they scratch R1-R5 registers,
3023 * preserve R6-R9, and store return value into R0
3024 *
3025 * Implicit input:
3026 * ctx == skb == R6 == CTX
3027 *
3028 * Explicit input:
3029 * SRC == any register
3030 * IMM == 32-bit immediate
3031 *
3032 * Output:
3033 * R0 - 8/16/32-bit skb data converted to cpu endianness
3034 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003035static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003036{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003037 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003038 u8 mode = BPF_MODE(insn->code);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003039 int i, err;
3040
Daniel Borkmann24701ec2015-03-01 12:31:47 +01003041 if (!may_access_skb(env->prog->type)) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003042 verbose("BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003043 return -EINVAL;
3044 }
3045
3046 if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
Alexei Starovoitovd82bccc2016-04-12 10:26:19 -07003047 BPF_SIZE(insn->code) == BPF_DW ||
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003048 (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003049 verbose("BPF_LD_[ABS|IND] uses reserved fields\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003050 return -EINVAL;
3051 }
3052
3053 /* check whether implicit source operand (register R6) is readable */
Edward Creedc503a82017-08-15 20:34:35 +01003054 err = check_reg_arg(env, BPF_REG_6, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003055 if (err)
3056 return err;
3057
3058 if (regs[BPF_REG_6].type != PTR_TO_CTX) {
3059 verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
3060 return -EINVAL;
3061 }
3062
3063 if (mode == BPF_IND) {
3064 /* check explicit source operand */
Edward Creedc503a82017-08-15 20:34:35 +01003065 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003066 if (err)
3067 return err;
3068 }
3069
3070 /* reset caller saved regs to unreadable */
Edward Creedc503a82017-08-15 20:34:35 +01003071 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Daniel Borkmanna9789ef2017-05-25 01:05:06 +02003072 mark_reg_not_init(regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01003073 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
3074 }
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003075
3076 /* mark destination R0 register as readable, since it contains
Edward Creedc503a82017-08-15 20:34:35 +01003077 * the value fetched from the packet.
3078 * Already marked as written above.
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003079 */
Edward Creef1174f72017-08-07 15:26:19 +01003080 mark_reg_unknown(regs, BPF_REG_0);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003081 return 0;
3082}
3083
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003084static int check_return_code(struct bpf_verifier_env *env)
3085{
3086 struct bpf_reg_state *reg;
3087 struct tnum range = tnum_range(0, 1);
3088
3089 switch (env->prog->type) {
3090 case BPF_PROG_TYPE_CGROUP_SKB:
3091 case BPF_PROG_TYPE_CGROUP_SOCK:
3092 case BPF_PROG_TYPE_SOCK_OPS:
3093 break;
3094 default:
3095 return 0;
3096 }
3097
3098 reg = &env->cur_state.regs[BPF_REG_0];
3099 if (reg->type != SCALAR_VALUE) {
3100 verbose("At program exit the register R0 is not a known value (%s)\n",
3101 reg_type_str[reg->type]);
3102 return -EINVAL;
3103 }
3104
3105 if (!tnum_in(range, reg->var_off)) {
3106 verbose("At program exit the register R0 ");
3107 if (!tnum_is_unknown(reg->var_off)) {
3108 char tn_buf[48];
3109
3110 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
3111 verbose("has value %s", tn_buf);
3112 } else {
3113 verbose("has unknown scalar value");
3114 }
3115 verbose(" should have been 0 or 1\n");
3116 return -EINVAL;
3117 }
3118 return 0;
3119}
3120
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003121/* non-recursive DFS pseudo code
3122 * 1 procedure DFS-iterative(G,v):
3123 * 2 label v as discovered
3124 * 3 let S be a stack
3125 * 4 S.push(v)
3126 * 5 while S is not empty
3127 * 6 t <- S.pop()
3128 * 7 if t is what we're looking for:
3129 * 8 return t
3130 * 9 for all edges e in G.adjacentEdges(t) do
3131 * 10 if edge e is already labelled
3132 * 11 continue with the next edge
3133 * 12 w <- G.adjacentVertex(t,e)
3134 * 13 if vertex w is not discovered and not explored
3135 * 14 label e as tree-edge
3136 * 15 label w as discovered
3137 * 16 S.push(w)
3138 * 17 continue at 5
3139 * 18 else if vertex w is discovered
3140 * 19 label e as back-edge
3141 * 20 else
3142 * 21 // vertex w is explored
3143 * 22 label e as forward- or cross-edge
3144 * 23 label t as explored
3145 * 24 S.pop()
3146 *
3147 * convention:
3148 * 0x10 - discovered
3149 * 0x11 - discovered and fall-through edge labelled
3150 * 0x12 - discovered and fall-through and branch edges labelled
3151 * 0x20 - explored
3152 */
3153
3154enum {
3155 DISCOVERED = 0x10,
3156 EXPLORED = 0x20,
3157 FALLTHROUGH = 1,
3158 BRANCH = 2,
3159};
3160
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003161#define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003162
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003163static int *insn_stack; /* stack of insns to process */
3164static int cur_stack; /* current stack index */
3165static int *insn_state;
3166
3167/* t, w, e - match pseudo-code above:
3168 * t - index of current instruction
3169 * w - next instruction
3170 * e - edge
3171 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003172static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003173{
3174 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
3175 return 0;
3176
3177 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
3178 return 0;
3179
3180 if (w < 0 || w >= env->prog->len) {
3181 verbose("jump out of range from insn %d to %d\n", t, w);
3182 return -EINVAL;
3183 }
3184
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003185 if (e == BRANCH)
3186 /* mark branch target for state pruning */
3187 env->explored_states[w] = STATE_LIST_MARK;
3188
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003189 if (insn_state[w] == 0) {
3190 /* tree-edge */
3191 insn_state[t] = DISCOVERED | e;
3192 insn_state[w] = DISCOVERED;
3193 if (cur_stack >= env->prog->len)
3194 return -E2BIG;
3195 insn_stack[cur_stack++] = w;
3196 return 1;
3197 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
3198 verbose("back-edge from insn %d to %d\n", t, w);
3199 return -EINVAL;
3200 } else if (insn_state[w] == EXPLORED) {
3201 /* forward- or cross-edge */
3202 insn_state[t] = DISCOVERED | e;
3203 } else {
3204 verbose("insn state internal bug\n");
3205 return -EFAULT;
3206 }
3207 return 0;
3208}
3209
3210/* non-recursive depth-first-search to detect loops in BPF program
3211 * loop == back-edge in directed graph
3212 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003213static int check_cfg(struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003214{
3215 struct bpf_insn *insns = env->prog->insnsi;
3216 int insn_cnt = env->prog->len;
3217 int ret = 0;
3218 int i, t;
3219
3220 insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
3221 if (!insn_state)
3222 return -ENOMEM;
3223
3224 insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
3225 if (!insn_stack) {
3226 kfree(insn_state);
3227 return -ENOMEM;
3228 }
3229
3230 insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
3231 insn_stack[0] = 0; /* 0 is the first instruction */
3232 cur_stack = 1;
3233
3234peek_stack:
3235 if (cur_stack == 0)
3236 goto check_state;
3237 t = insn_stack[cur_stack - 1];
3238
3239 if (BPF_CLASS(insns[t].code) == BPF_JMP) {
3240 u8 opcode = BPF_OP(insns[t].code);
3241
3242 if (opcode == BPF_EXIT) {
3243 goto mark_explored;
3244 } else if (opcode == BPF_CALL) {
3245 ret = push_insn(t, t + 1, FALLTHROUGH, env);
3246 if (ret == 1)
3247 goto peek_stack;
3248 else if (ret < 0)
3249 goto err_free;
Daniel Borkmann07016152016-04-05 22:33:17 +02003250 if (t + 1 < insn_cnt)
3251 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003252 } else if (opcode == BPF_JA) {
3253 if (BPF_SRC(insns[t].code) != BPF_K) {
3254 ret = -EINVAL;
3255 goto err_free;
3256 }
3257 /* unconditional jump with single edge */
3258 ret = push_insn(t, t + insns[t].off + 1,
3259 FALLTHROUGH, env);
3260 if (ret == 1)
3261 goto peek_stack;
3262 else if (ret < 0)
3263 goto err_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003264 /* tell verifier to check for equivalent states
3265 * after every call and jump
3266 */
Alexei Starovoitovc3de6312015-04-14 15:57:13 -07003267 if (t + 1 < insn_cnt)
3268 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003269 } else {
3270 /* conditional jump with two edges */
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02003271 env->explored_states[t] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003272 ret = push_insn(t, t + 1, FALLTHROUGH, env);
3273 if (ret == 1)
3274 goto peek_stack;
3275 else if (ret < 0)
3276 goto err_free;
3277
3278 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
3279 if (ret == 1)
3280 goto peek_stack;
3281 else if (ret < 0)
3282 goto err_free;
3283 }
3284 } else {
3285 /* all other non-branch instructions with single
3286 * fall-through edge
3287 */
3288 ret = push_insn(t, t + 1, FALLTHROUGH, env);
3289 if (ret == 1)
3290 goto peek_stack;
3291 else if (ret < 0)
3292 goto err_free;
3293 }
3294
3295mark_explored:
3296 insn_state[t] = EXPLORED;
3297 if (cur_stack-- <= 0) {
3298 verbose("pop stack internal bug\n");
3299 ret = -EFAULT;
3300 goto err_free;
3301 }
3302 goto peek_stack;
3303
3304check_state:
3305 for (i = 0; i < insn_cnt; i++) {
3306 if (insn_state[i] != EXPLORED) {
3307 verbose("unreachable insn %d\n", i);
3308 ret = -EINVAL;
3309 goto err_free;
3310 }
3311 }
3312 ret = 0; /* cfg looks good */
3313
3314err_free:
3315 kfree(insn_state);
3316 kfree(insn_stack);
3317 return ret;
3318}
3319
Edward Creef1174f72017-08-07 15:26:19 +01003320/* check %cur's range satisfies %old's */
3321static bool range_within(struct bpf_reg_state *old,
3322 struct bpf_reg_state *cur)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003323{
Edward Creeb03c9f92017-08-07 15:26:36 +01003324 return old->umin_value <= cur->umin_value &&
3325 old->umax_value >= cur->umax_value &&
3326 old->smin_value <= cur->smin_value &&
3327 old->smax_value >= cur->smax_value;
Edward Creef1174f72017-08-07 15:26:19 +01003328}
3329
3330/* Maximum number of register states that can exist at once */
3331#define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE)
3332struct idpair {
3333 u32 old;
3334 u32 cur;
3335};
3336
3337/* If in the old state two registers had the same id, then they need to have
3338 * the same id in the new state as well. But that id could be different from
3339 * the old state, so we need to track the mapping from old to new ids.
3340 * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent
3341 * regs with old id 5 must also have new id 9 for the new state to be safe. But
3342 * regs with a different old id could still have new id 9, we don't care about
3343 * that.
3344 * So we look through our idmap to see if this old id has been seen before. If
3345 * so, we require the new id to match; otherwise, we add the id pair to the map.
3346 */
3347static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap)
3348{
3349 unsigned int i;
3350
3351 for (i = 0; i < ID_MAP_SIZE; i++) {
3352 if (!idmap[i].old) {
3353 /* Reached an empty slot; haven't seen this id before */
3354 idmap[i].old = old_id;
3355 idmap[i].cur = cur_id;
3356 return true;
3357 }
3358 if (idmap[i].old == old_id)
3359 return idmap[i].cur == cur_id;
3360 }
3361 /* We ran out of idmap slots, which should be impossible */
3362 WARN_ON_ONCE(1);
3363 return false;
3364}
3365
3366/* Returns true if (rold safe implies rcur safe) */
Edward Cree1b688a12017-08-23 15:10:50 +01003367static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
3368 struct idpair *idmap)
Edward Creef1174f72017-08-07 15:26:19 +01003369{
Edward Creedc503a82017-08-15 20:34:35 +01003370 if (!(rold->live & REG_LIVE_READ))
3371 /* explored state didn't use this */
3372 return true;
3373
3374 if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, live)) == 0)
Edward Creef1174f72017-08-07 15:26:19 +01003375 return true;
3376
3377 if (rold->type == NOT_INIT)
3378 /* explored state can't have used this */
3379 return true;
3380 if (rcur->type == NOT_INIT)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003381 return false;
Edward Creef1174f72017-08-07 15:26:19 +01003382 switch (rold->type) {
3383 case SCALAR_VALUE:
3384 if (rcur->type == SCALAR_VALUE) {
3385 /* new val must satisfy old val knowledge */
3386 return range_within(rold, rcur) &&
3387 tnum_in(rold->var_off, rcur->var_off);
3388 } else {
3389 /* if we knew anything about the old value, we're not
3390 * equal, because we can't know anything about the
3391 * scalar value of the pointer in the new value.
3392 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003393 return rold->umin_value == 0 &&
3394 rold->umax_value == U64_MAX &&
3395 rold->smin_value == S64_MIN &&
3396 rold->smax_value == S64_MAX &&
Edward Creef1174f72017-08-07 15:26:19 +01003397 tnum_is_unknown(rold->var_off);
3398 }
3399 case PTR_TO_MAP_VALUE:
Edward Cree1b688a12017-08-23 15:10:50 +01003400 /* If the new min/max/var_off satisfy the old ones and
3401 * everything else matches, we are OK.
3402 * We don't care about the 'id' value, because nothing
3403 * uses it for PTR_TO_MAP_VALUE (only for ..._OR_NULL)
3404 */
3405 return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 &&
3406 range_within(rold, rcur) &&
3407 tnum_in(rold->var_off, rcur->var_off);
Edward Creef1174f72017-08-07 15:26:19 +01003408 case PTR_TO_MAP_VALUE_OR_NULL:
3409 /* a PTR_TO_MAP_VALUE could be safe to use as a
3410 * PTR_TO_MAP_VALUE_OR_NULL into the same map.
3411 * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL-
3412 * checked, doing so could have affected others with the same
3413 * id, and we can't check for that because we lost the id when
3414 * we converted to a PTR_TO_MAP_VALUE.
3415 */
3416 if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL)
3417 return false;
3418 if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)))
3419 return false;
3420 /* Check our ids match any regs they're supposed to */
3421 return check_ids(rold->id, rcur->id, idmap);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003422 case PTR_TO_PACKET_META:
Edward Creef1174f72017-08-07 15:26:19 +01003423 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003424 if (rcur->type != rold->type)
Edward Creef1174f72017-08-07 15:26:19 +01003425 return false;
3426 /* We must have at least as much range as the old ptr
3427 * did, so that any accesses which were safe before are
3428 * still safe. This is true even if old range < old off,
3429 * since someone could have accessed through (ptr - k), or
3430 * even done ptr -= k in a register, to get a safe access.
3431 */
3432 if (rold->range > rcur->range)
3433 return false;
3434 /* If the offsets don't match, we can't trust our alignment;
3435 * nor can we be sure that we won't fall out of range.
3436 */
3437 if (rold->off != rcur->off)
3438 return false;
3439 /* id relations must be preserved */
3440 if (rold->id && !check_ids(rold->id, rcur->id, idmap))
3441 return false;
3442 /* new val must satisfy old val knowledge */
3443 return range_within(rold, rcur) &&
3444 tnum_in(rold->var_off, rcur->var_off);
3445 case PTR_TO_CTX:
3446 case CONST_PTR_TO_MAP:
3447 case PTR_TO_STACK:
3448 case PTR_TO_PACKET_END:
3449 /* Only valid matches are exact, which memcmp() above
3450 * would have accepted
3451 */
3452 default:
3453 /* Don't know what's going on, just say it's not safe */
3454 return false;
3455 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003456
Edward Creef1174f72017-08-07 15:26:19 +01003457 /* Shouldn't get here; if we do, say it's not safe */
3458 WARN_ON_ONCE(1);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003459 return false;
3460}
3461
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003462/* compare two verifier states
3463 *
3464 * all states stored in state_list are known to be valid, since
3465 * verifier reached 'bpf_exit' instruction through them
3466 *
3467 * this function is called when verifier exploring different branches of
3468 * execution popped from the state stack. If it sees an old state that has
3469 * more strict register state and more strict stack state then this execution
3470 * branch doesn't need to be explored further, since verifier already
3471 * concluded that more strict state leads to valid finish.
3472 *
3473 * Therefore two states are equivalent if register state is more conservative
3474 * and explored stack state is more conservative than the current one.
3475 * Example:
3476 * explored current
3477 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
3478 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
3479 *
3480 * In other words if current stack state (one being explored) has more
3481 * valid slots than old one that already passed validation, it means
3482 * the verifier can stop exploring and conclude that current state is valid too
3483 *
3484 * Similarly with registers. If explored state has register type as invalid
3485 * whereas register type in current state is meaningful, it means that
3486 * the current state will reach 'bpf_exit' instruction safely
3487 */
Josef Bacik48461132016-09-28 10:54:32 -04003488static bool states_equal(struct bpf_verifier_env *env,
3489 struct bpf_verifier_state *old,
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003490 struct bpf_verifier_state *cur)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003491{
Edward Creef1174f72017-08-07 15:26:19 +01003492 struct idpair *idmap;
3493 bool ret = false;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003494 int i;
3495
Edward Creef1174f72017-08-07 15:26:19 +01003496 idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL);
3497 /* If we failed to allocate the idmap, just say it's not safe */
3498 if (!idmap)
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003499 return false;
Edward Creef1174f72017-08-07 15:26:19 +01003500
3501 for (i = 0; i < MAX_BPF_REG; i++) {
Edward Cree1b688a12017-08-23 15:10:50 +01003502 if (!regsafe(&old->regs[i], &cur->regs[i], idmap))
Edward Creef1174f72017-08-07 15:26:19 +01003503 goto out_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003504 }
3505
3506 for (i = 0; i < MAX_BPF_STACK; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07003507 if (old->stack_slot_type[i] == STACK_INVALID)
3508 continue;
3509 if (old->stack_slot_type[i] != cur->stack_slot_type[i])
3510 /* Ex: old explored (safe) state has STACK_SPILL in
3511 * this stack slot, but current has has STACK_MISC ->
3512 * this verifier states are not equivalent,
3513 * return false to continue verification of this path
3514 */
Edward Creef1174f72017-08-07 15:26:19 +01003515 goto out_free;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07003516 if (i % BPF_REG_SIZE)
3517 continue;
Daniel Borkmannd25da6c2017-06-11 00:50:41 +02003518 if (old->stack_slot_type[i] != STACK_SPILL)
3519 continue;
Edward Creef1174f72017-08-07 15:26:19 +01003520 if (!regsafe(&old->spilled_regs[i / BPF_REG_SIZE],
3521 &cur->spilled_regs[i / BPF_REG_SIZE],
Edward Cree1b688a12017-08-23 15:10:50 +01003522 idmap))
Edward Creef1174f72017-08-07 15:26:19 +01003523 /* when explored and current stack slot are both storing
3524 * spilled registers, check that stored pointers types
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07003525 * are the same as well.
3526 * Ex: explored safe path could have stored
Edward Creef1174f72017-08-07 15:26:19 +01003527 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8}
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07003528 * but current path has stored:
Edward Creef1174f72017-08-07 15:26:19 +01003529 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16}
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07003530 * such verifier states are not equivalent.
3531 * return false to continue verification of this path
3532 */
Edward Creef1174f72017-08-07 15:26:19 +01003533 goto out_free;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07003534 else
3535 continue;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003536 }
Edward Creef1174f72017-08-07 15:26:19 +01003537 ret = true;
3538out_free:
3539 kfree(idmap);
3540 return ret;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003541}
3542
Edward Cree8e9cd9c2017-08-23 15:11:21 +01003543/* A write screens off any subsequent reads; but write marks come from the
3544 * straight-line code between a state and its parent. When we arrive at a
3545 * jump target (in the first iteration of the propagate_liveness() loop),
3546 * we didn't arrive by the straight-line code, so read marks in state must
3547 * propagate to parent regardless of state's write marks.
3548 */
Edward Creedc503a82017-08-15 20:34:35 +01003549static bool do_propagate_liveness(const struct bpf_verifier_state *state,
3550 struct bpf_verifier_state *parent)
3551{
Edward Cree63f45f82017-08-23 15:10:03 +01003552 bool writes = parent == state->parent; /* Observe write marks */
Edward Creedc503a82017-08-15 20:34:35 +01003553 bool touched = false; /* any changes made? */
3554 int i;
3555
3556 if (!parent)
3557 return touched;
3558 /* Propagate read liveness of registers... */
3559 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
3560 /* We don't need to worry about FP liveness because it's read-only */
3561 for (i = 0; i < BPF_REG_FP; i++) {
3562 if (parent->regs[i].live & REG_LIVE_READ)
3563 continue;
Edward Cree63f45f82017-08-23 15:10:03 +01003564 if (writes && (state->regs[i].live & REG_LIVE_WRITTEN))
3565 continue;
3566 if (state->regs[i].live & REG_LIVE_READ) {
Edward Creedc503a82017-08-15 20:34:35 +01003567 parent->regs[i].live |= REG_LIVE_READ;
3568 touched = true;
3569 }
3570 }
3571 /* ... and stack slots */
3572 for (i = 0; i < MAX_BPF_STACK / BPF_REG_SIZE; i++) {
3573 if (parent->stack_slot_type[i * BPF_REG_SIZE] != STACK_SPILL)
3574 continue;
3575 if (state->stack_slot_type[i * BPF_REG_SIZE] != STACK_SPILL)
3576 continue;
3577 if (parent->spilled_regs[i].live & REG_LIVE_READ)
3578 continue;
Edward Cree63f45f82017-08-23 15:10:03 +01003579 if (writes && (state->spilled_regs[i].live & REG_LIVE_WRITTEN))
3580 continue;
3581 if (state->spilled_regs[i].live & REG_LIVE_READ) {
Daniel Borkmann1ab2de22017-08-17 14:59:40 +02003582 parent->spilled_regs[i].live |= REG_LIVE_READ;
Edward Creedc503a82017-08-15 20:34:35 +01003583 touched = true;
3584 }
3585 }
3586 return touched;
3587}
3588
Edward Cree8e9cd9c2017-08-23 15:11:21 +01003589/* "parent" is "a state from which we reach the current state", but initially
3590 * it is not the state->parent (i.e. "the state whose straight-line code leads
3591 * to the current state"), instead it is the state that happened to arrive at
3592 * a (prunable) equivalent of the current state. See comment above
3593 * do_propagate_liveness() for consequences of this.
3594 * This function is just a more efficient way of calling mark_reg_read() or
3595 * mark_stack_slot_read() on each reg in "parent" that is read in "state",
3596 * though it requires that parent != state->parent in the call arguments.
3597 */
Edward Creedc503a82017-08-15 20:34:35 +01003598static void propagate_liveness(const struct bpf_verifier_state *state,
3599 struct bpf_verifier_state *parent)
3600{
3601 while (do_propagate_liveness(state, parent)) {
3602 /* Something changed, so we need to feed those changes onward */
3603 state = parent;
3604 parent = state->parent;
3605 }
3606}
3607
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003608static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003609{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003610 struct bpf_verifier_state_list *new_sl;
3611 struct bpf_verifier_state_list *sl;
Edward Creedc503a82017-08-15 20:34:35 +01003612 int i;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003613
3614 sl = env->explored_states[insn_idx];
3615 if (!sl)
3616 /* this 'insn_idx' instruction wasn't marked, so we will not
3617 * be doing state search here
3618 */
3619 return 0;
3620
3621 while (sl != STATE_LIST_MARK) {
Edward Creedc503a82017-08-15 20:34:35 +01003622 if (states_equal(env, &sl->state, &env->cur_state)) {
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003623 /* reached equivalent register/stack state,
Edward Creedc503a82017-08-15 20:34:35 +01003624 * prune the search.
3625 * Registers read by the continuation are read by us.
Edward Cree8e9cd9c2017-08-23 15:11:21 +01003626 * If we have any write marks in env->cur_state, they
3627 * will prevent corresponding reads in the continuation
3628 * from reaching our parent (an explored_state). Our
3629 * own state will get the read marks recorded, but
3630 * they'll be immediately forgotten as we're pruning
3631 * this state and will pop a new one.
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003632 */
Edward Creedc503a82017-08-15 20:34:35 +01003633 propagate_liveness(&sl->state, &env->cur_state);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003634 return 1;
Edward Creedc503a82017-08-15 20:34:35 +01003635 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003636 sl = sl->next;
3637 }
3638
3639 /* there were no equivalent states, remember current one.
3640 * technically the current state is not proven to be safe yet,
3641 * but it will either reach bpf_exit (which means it's safe) or
3642 * it will be rejected. Since there are no loops, we won't be
3643 * seeing this 'insn_idx' instruction again on the way to bpf_exit
3644 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003645 new_sl = kmalloc(sizeof(struct bpf_verifier_state_list), GFP_USER);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003646 if (!new_sl)
3647 return -ENOMEM;
3648
3649 /* add new state to the head of linked list */
3650 memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state));
3651 new_sl->next = env->explored_states[insn_idx];
3652 env->explored_states[insn_idx] = new_sl;
Edward Creedc503a82017-08-15 20:34:35 +01003653 /* connect new state to parentage chain */
3654 env->cur_state.parent = &new_sl->state;
Edward Cree8e9cd9c2017-08-23 15:11:21 +01003655 /* clear write marks in current state: the writes we did are not writes
3656 * our child did, so they don't screen off its reads from us.
3657 * (There are no read marks in current state, because reads always mark
3658 * their parent and current state never has children yet. Only
3659 * explored_states can get read marks.)
3660 */
Edward Creedc503a82017-08-15 20:34:35 +01003661 for (i = 0; i < BPF_REG_FP; i++)
3662 env->cur_state.regs[i].live = REG_LIVE_NONE;
3663 for (i = 0; i < MAX_BPF_STACK / BPF_REG_SIZE; i++)
3664 if (env->cur_state.stack_slot_type[i * BPF_REG_SIZE] == STACK_SPILL)
3665 env->cur_state.spilled_regs[i].live = REG_LIVE_NONE;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003666 return 0;
3667}
3668
Jakub Kicinski13a27df2016-09-21 11:43:58 +01003669static int ext_analyzer_insn_hook(struct bpf_verifier_env *env,
3670 int insn_idx, int prev_insn_idx)
3671{
3672 if (!env->analyzer_ops || !env->analyzer_ops->insn_hook)
3673 return 0;
3674
3675 return env->analyzer_ops->insn_hook(env, insn_idx, prev_insn_idx);
3676}
3677
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003678static int do_check(struct bpf_verifier_env *env)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003679{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003680 struct bpf_verifier_state *state = &env->cur_state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003681 struct bpf_insn *insns = env->prog->insnsi;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003682 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003683 int insn_cnt = env->prog->len;
3684 int insn_idx, prev_insn_idx = 0;
3685 int insn_processed = 0;
3686 bool do_print_state = false;
3687
3688 init_reg_state(regs);
Edward Creedc503a82017-08-15 20:34:35 +01003689 state->parent = NULL;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003690 insn_idx = 0;
3691 for (;;) {
3692 struct bpf_insn *insn;
3693 u8 class;
3694 int err;
3695
3696 if (insn_idx >= insn_cnt) {
3697 verbose("invalid insn idx %d insn_cnt %d\n",
3698 insn_idx, insn_cnt);
3699 return -EFAULT;
3700 }
3701
3702 insn = &insns[insn_idx];
3703 class = BPF_CLASS(insn->code);
3704
Daniel Borkmann07016152016-04-05 22:33:17 +02003705 if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
Colin Ian Kingbc1750f2017-02-23 00:20:53 +00003706 verbose("BPF program is too large. Processed %d insn\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003707 insn_processed);
3708 return -E2BIG;
3709 }
3710
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003711 err = is_state_visited(env, insn_idx);
3712 if (err < 0)
3713 return err;
3714 if (err == 1) {
3715 /* found equivalent state, can prune the search */
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07003716 if (verifier_log.level) {
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003717 if (do_print_state)
3718 verbose("\nfrom %d to %d: safe\n",
3719 prev_insn_idx, insn_idx);
3720 else
3721 verbose("%d: safe\n", insn_idx);
3722 }
3723 goto process_bpf_exit;
3724 }
3725
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02003726 if (need_resched())
3727 cond_resched();
3728
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07003729 if (verifier_log.level > 1 ||
3730 (verifier_log.level && do_print_state)) {
3731 if (verifier_log.level > 1)
David S. Millerc5fc9692017-05-10 11:25:17 -07003732 verbose("%d:", insn_idx);
3733 else
3734 verbose("\nfrom %d to %d:",
3735 prev_insn_idx, insn_idx);
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003736 print_verifier_state(&env->cur_state);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003737 do_print_state = false;
3738 }
3739
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07003740 if (verifier_log.level) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003741 verbose("%d: ", insn_idx);
Daniel Borkmann0d0e5762017-05-08 00:04:09 +02003742 print_bpf_insn(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003743 }
3744
Jakub Kicinski13a27df2016-09-21 11:43:58 +01003745 err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx);
3746 if (err)
3747 return err;
3748
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003749 if (class == BPF_ALU || class == BPF_ALU64) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003750 err = check_alu_op(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003751 if (err)
3752 return err;
3753
3754 } else if (class == BPF_LDX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003755 enum bpf_reg_type *prev_src_type, src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003756
3757 /* check for reserved fields is already done */
3758
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003759 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01003760 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003761 if (err)
3762 return err;
3763
Edward Creedc503a82017-08-15 20:34:35 +01003764 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003765 if (err)
3766 return err;
3767
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07003768 src_reg_type = regs[insn->src_reg].type;
3769
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003770 /* check that memory (src_reg + off) is readable,
3771 * the state of dst_reg will be updated by this func
3772 */
Yonghong Song31fd8582017-06-13 15:52:13 -07003773 err = check_mem_access(env, insn_idx, insn->src_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003774 BPF_SIZE(insn->code), BPF_READ,
3775 insn->dst_reg);
3776 if (err)
3777 return err;
3778
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003779 prev_src_type = &env->insn_aux_data[insn_idx].ptr_type;
3780
3781 if (*prev_src_type == NOT_INIT) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003782 /* saw a valid insn
3783 * dst_reg = *(u32 *)(src_reg + off)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003784 * save type to validate intersecting paths
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003785 */
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003786 *prev_src_type = src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003787
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003788 } else if (src_reg_type != *prev_src_type &&
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003789 (src_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003790 *prev_src_type == PTR_TO_CTX)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003791 /* ABuser program is trying to use the same insn
3792 * dst_reg = *(u32*) (src_reg + off)
3793 * with different pointer types:
3794 * src_reg == ctx in one branch and
3795 * src_reg == stack|map in some other branch.
3796 * Reject it.
3797 */
3798 verbose("same insn cannot be used with different pointers\n");
3799 return -EINVAL;
3800 }
3801
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003802 } else if (class == BPF_STX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003803 enum bpf_reg_type *prev_dst_type, dst_reg_type;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003804
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003805 if (BPF_MODE(insn->code) == BPF_XADD) {
Yonghong Song31fd8582017-06-13 15:52:13 -07003806 err = check_xadd(env, insn_idx, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003807 if (err)
3808 return err;
3809 insn_idx++;
3810 continue;
3811 }
3812
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003813 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01003814 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003815 if (err)
3816 return err;
3817 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01003818 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003819 if (err)
3820 return err;
3821
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003822 dst_reg_type = regs[insn->dst_reg].type;
3823
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003824 /* check that memory (dst_reg + off) is writeable */
Yonghong Song31fd8582017-06-13 15:52:13 -07003825 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003826 BPF_SIZE(insn->code), BPF_WRITE,
3827 insn->src_reg);
3828 if (err)
3829 return err;
3830
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003831 prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type;
3832
3833 if (*prev_dst_type == NOT_INIT) {
3834 *prev_dst_type = dst_reg_type;
3835 } else if (dst_reg_type != *prev_dst_type &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003836 (dst_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003837 *prev_dst_type == PTR_TO_CTX)) {
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003838 verbose("same insn cannot be used with different pointers\n");
3839 return -EINVAL;
3840 }
3841
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003842 } else if (class == BPF_ST) {
3843 if (BPF_MODE(insn->code) != BPF_MEM ||
3844 insn->src_reg != BPF_REG_0) {
3845 verbose("BPF_ST uses reserved fields\n");
3846 return -EINVAL;
3847 }
3848 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01003849 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003850 if (err)
3851 return err;
3852
3853 /* check that memory (dst_reg + off) is writeable */
Yonghong Song31fd8582017-06-13 15:52:13 -07003854 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003855 BPF_SIZE(insn->code), BPF_WRITE,
3856 -1);
3857 if (err)
3858 return err;
3859
3860 } else if (class == BPF_JMP) {
3861 u8 opcode = BPF_OP(insn->code);
3862
3863 if (opcode == BPF_CALL) {
3864 if (BPF_SRC(insn->code) != BPF_K ||
3865 insn->off != 0 ||
3866 insn->src_reg != BPF_REG_0 ||
3867 insn->dst_reg != BPF_REG_0) {
3868 verbose("BPF_CALL uses reserved fields\n");
3869 return -EINVAL;
3870 }
3871
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07003872 err = check_call(env, insn->imm, insn_idx);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003873 if (err)
3874 return err;
3875
3876 } else if (opcode == BPF_JA) {
3877 if (BPF_SRC(insn->code) != BPF_K ||
3878 insn->imm != 0 ||
3879 insn->src_reg != BPF_REG_0 ||
3880 insn->dst_reg != BPF_REG_0) {
3881 verbose("BPF_JA uses reserved fields\n");
3882 return -EINVAL;
3883 }
3884
3885 insn_idx += insn->off + 1;
3886 continue;
3887
3888 } else if (opcode == BPF_EXIT) {
3889 if (BPF_SRC(insn->code) != BPF_K ||
3890 insn->imm != 0 ||
3891 insn->src_reg != BPF_REG_0 ||
3892 insn->dst_reg != BPF_REG_0) {
3893 verbose("BPF_EXIT uses reserved fields\n");
3894 return -EINVAL;
3895 }
3896
3897 /* eBPF calling convetion is such that R0 is used
3898 * to return the value from eBPF program.
3899 * Make sure that it's readable at this time
3900 * of bpf_exit, which means that program wrote
3901 * something into it earlier
3902 */
Edward Creedc503a82017-08-15 20:34:35 +01003903 err = check_reg_arg(env, BPF_REG_0, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003904 if (err)
3905 return err;
3906
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003907 if (is_pointer_value(env, BPF_REG_0)) {
3908 verbose("R0 leaks addr as return value\n");
3909 return -EACCES;
3910 }
3911
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003912 err = check_return_code(env);
3913 if (err)
3914 return err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003915process_bpf_exit:
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003916 insn_idx = pop_stack(env, &prev_insn_idx);
3917 if (insn_idx < 0) {
3918 break;
3919 } else {
3920 do_print_state = true;
3921 continue;
3922 }
3923 } else {
3924 err = check_cond_jmp_op(env, insn, &insn_idx);
3925 if (err)
3926 return err;
3927 }
3928 } else if (class == BPF_LD) {
3929 u8 mode = BPF_MODE(insn->code);
3930
3931 if (mode == BPF_ABS || mode == BPF_IND) {
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003932 err = check_ld_abs(env, insn);
3933 if (err)
3934 return err;
3935
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003936 } else if (mode == BPF_IMM) {
3937 err = check_ld_imm(env, insn);
3938 if (err)
3939 return err;
3940
3941 insn_idx++;
3942 } else {
3943 verbose("invalid BPF_LD mode\n");
3944 return -EINVAL;
3945 }
3946 } else {
3947 verbose("unknown insn class %d\n", class);
3948 return -EINVAL;
3949 }
3950
3951 insn_idx++;
3952 }
3953
Alexei Starovoitov87266792017-05-30 13:31:29 -07003954 verbose("processed %d insns, stack depth %d\n",
3955 insn_processed, env->prog->aux->stack_depth);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003956 return 0;
3957}
3958
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07003959static int check_map_prealloc(struct bpf_map *map)
3960{
3961 return (map->map_type != BPF_MAP_TYPE_HASH &&
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07003962 map->map_type != BPF_MAP_TYPE_PERCPU_HASH &&
3963 map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) ||
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07003964 !(map->map_flags & BPF_F_NO_PREALLOC);
3965}
3966
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07003967static int check_map_prog_compatibility(struct bpf_map *map,
3968 struct bpf_prog *prog)
3969
3970{
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07003971 /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use
3972 * preallocated hash maps, since doing memory allocation
3973 * in overflow_handler can crash depending on where nmi got
3974 * triggered.
3975 */
3976 if (prog->type == BPF_PROG_TYPE_PERF_EVENT) {
3977 if (!check_map_prealloc(map)) {
3978 verbose("perf_event programs can only use preallocated hash map\n");
3979 return -EINVAL;
3980 }
3981 if (map->inner_map_meta &&
3982 !check_map_prealloc(map->inner_map_meta)) {
3983 verbose("perf_event programs can only use preallocated inner hash map\n");
3984 return -EINVAL;
3985 }
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07003986 }
3987 return 0;
3988}
3989
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003990/* look for pseudo eBPF instructions that access map FDs and
3991 * replace them with actual map pointers
3992 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003993static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003994{
3995 struct bpf_insn *insn = env->prog->insnsi;
3996 int insn_cnt = env->prog->len;
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07003997 int i, j, err;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003998
Daniel Borkmannf1f77142017-01-13 23:38:15 +01003999 err = bpf_prog_calc_tag(env->prog);
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01004000 if (err)
4001 return err;
4002
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004003 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004004 if (BPF_CLASS(insn->code) == BPF_LDX &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004005 (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004006 verbose("BPF_LDX uses reserved fields\n");
4007 return -EINVAL;
4008 }
4009
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004010 if (BPF_CLASS(insn->code) == BPF_STX &&
4011 ((BPF_MODE(insn->code) != BPF_MEM &&
4012 BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
4013 verbose("BPF_STX uses reserved fields\n");
4014 return -EINVAL;
4015 }
4016
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004017 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
4018 struct bpf_map *map;
4019 struct fd f;
4020
4021 if (i == insn_cnt - 1 || insn[1].code != 0 ||
4022 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
4023 insn[1].off != 0) {
4024 verbose("invalid bpf_ld_imm64 insn\n");
4025 return -EINVAL;
4026 }
4027
4028 if (insn->src_reg == 0)
4029 /* valid generic load 64-bit imm */
4030 goto next_insn;
4031
4032 if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
4033 verbose("unrecognized bpf_ld_imm64 insn\n");
4034 return -EINVAL;
4035 }
4036
4037 f = fdget(insn->imm);
Daniel Borkmannc2101292015-10-29 14:58:07 +01004038 map = __bpf_map_get(f);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004039 if (IS_ERR(map)) {
4040 verbose("fd %d is not pointing to valid bpf_map\n",
4041 insn->imm);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004042 return PTR_ERR(map);
4043 }
4044
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07004045 err = check_map_prog_compatibility(map, env->prog);
4046 if (err) {
4047 fdput(f);
4048 return err;
4049 }
4050
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004051 /* store map pointer inside BPF_LD_IMM64 instruction */
4052 insn[0].imm = (u32) (unsigned long) map;
4053 insn[1].imm = ((u64) (unsigned long) map) >> 32;
4054
4055 /* check whether we recorded this map already */
4056 for (j = 0; j < env->used_map_cnt; j++)
4057 if (env->used_maps[j] == map) {
4058 fdput(f);
4059 goto next_insn;
4060 }
4061
4062 if (env->used_map_cnt >= MAX_USED_MAPS) {
4063 fdput(f);
4064 return -E2BIG;
4065 }
4066
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004067 /* hold the map. If the program is rejected by verifier,
4068 * the map will be released by release_maps() or it
4069 * will be used by the valid program until it's unloaded
4070 * and all maps are released in free_bpf_prog_info()
4071 */
Alexei Starovoitov92117d82016-04-27 18:56:20 -07004072 map = bpf_map_inc(map, false);
4073 if (IS_ERR(map)) {
4074 fdput(f);
4075 return PTR_ERR(map);
4076 }
4077 env->used_maps[env->used_map_cnt++] = map;
4078
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004079 fdput(f);
4080next_insn:
4081 insn++;
4082 i++;
4083 }
4084 }
4085
4086 /* now all pseudo BPF_LD_IMM64 instructions load valid
4087 * 'struct bpf_map *' into a register instead of user map_fd.
4088 * These pointers will be used later by verifier to validate map access.
4089 */
4090 return 0;
4091}
4092
4093/* drop refcnt of maps used by the rejected program */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004094static void release_maps(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004095{
4096 int i;
4097
4098 for (i = 0; i < env->used_map_cnt; i++)
4099 bpf_map_put(env->used_maps[i]);
4100}
4101
4102/* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004103static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004104{
4105 struct bpf_insn *insn = env->prog->insnsi;
4106 int insn_cnt = env->prog->len;
4107 int i;
4108
4109 for (i = 0; i < insn_cnt; i++, insn++)
4110 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
4111 insn->src_reg = 0;
4112}
4113
Alexei Starovoitov80419022017-03-15 18:26:41 -07004114/* single env->prog->insni[off] instruction was replaced with the range
4115 * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
4116 * [0, off) and [off, end) to new locations, so the patched range stays zero
4117 */
4118static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len,
4119 u32 off, u32 cnt)
4120{
4121 struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data;
4122
4123 if (cnt == 1)
4124 return 0;
4125 new_data = vzalloc(sizeof(struct bpf_insn_aux_data) * prog_len);
4126 if (!new_data)
4127 return -ENOMEM;
4128 memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off);
4129 memcpy(new_data + off + cnt - 1, old_data + off,
4130 sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
4131 env->insn_aux_data = new_data;
4132 vfree(old_data);
4133 return 0;
4134}
4135
4136static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
4137 const struct bpf_insn *patch, u32 len)
4138{
4139 struct bpf_prog *new_prog;
4140
4141 new_prog = bpf_patch_insn_single(env->prog, off, patch, len);
4142 if (!new_prog)
4143 return NULL;
4144 if (adjust_insn_aux_data(env, new_prog->len, off, len))
4145 return NULL;
4146 return new_prog;
4147}
4148
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004149/* convert load instructions that access fields of 'struct __sk_buff'
4150 * into sequence of instructions that access fields of 'struct sk_buff'
4151 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004152static int convert_ctx_accesses(struct bpf_verifier_env *env)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004153{
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004154 const struct bpf_verifier_ops *ops = env->prog->aux->ops;
Daniel Borkmannf96da092017-07-02 02:13:27 +02004155 int i, cnt, size, ctx_field_size, delta = 0;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004156 const int insn_cnt = env->prog->len;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004157 struct bpf_insn insn_buf[16], *insn;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004158 struct bpf_prog *new_prog;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004159 enum bpf_access_type type;
Daniel Borkmannf96da092017-07-02 02:13:27 +02004160 bool is_narrower_load;
4161 u32 target_size;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004162
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004163 if (ops->gen_prologue) {
4164 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
4165 env->prog);
4166 if (cnt >= ARRAY_SIZE(insn_buf)) {
4167 verbose("bpf verifier is misconfigured\n");
4168 return -EINVAL;
4169 } else if (cnt) {
Alexei Starovoitov80419022017-03-15 18:26:41 -07004170 new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004171 if (!new_prog)
4172 return -ENOMEM;
Alexei Starovoitov80419022017-03-15 18:26:41 -07004173
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004174 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004175 delta += cnt - 1;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004176 }
4177 }
4178
4179 if (!ops->convert_ctx_access)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004180 return 0;
4181
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004182 insn = env->prog->insnsi + delta;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004183
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004184 for (i = 0; i < insn_cnt; i++, insn++) {
Daniel Borkmann62c79892017-01-12 11:51:33 +01004185 if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) ||
4186 insn->code == (BPF_LDX | BPF_MEM | BPF_H) ||
4187 insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07004188 insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004189 type = BPF_READ;
Daniel Borkmann62c79892017-01-12 11:51:33 +01004190 else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) ||
4191 insn->code == (BPF_STX | BPF_MEM | BPF_H) ||
4192 insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07004193 insn->code == (BPF_STX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004194 type = BPF_WRITE;
4195 else
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004196 continue;
4197
Alexei Starovoitov80419022017-03-15 18:26:41 -07004198 if (env->insn_aux_data[i + delta].ptr_type != PTR_TO_CTX)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004199 continue;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004200
Yonghong Song31fd8582017-06-13 15:52:13 -07004201 ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size;
Daniel Borkmannf96da092017-07-02 02:13:27 +02004202 size = BPF_LDST_BYTES(insn);
Yonghong Song31fd8582017-06-13 15:52:13 -07004203
4204 /* If the read access is a narrower load of the field,
4205 * convert to a 4/8-byte load, to minimum program type specific
4206 * convert_ctx_access changes. If conversion is successful,
4207 * we will apply proper mask to the result.
4208 */
Daniel Borkmannf96da092017-07-02 02:13:27 +02004209 is_narrower_load = size < ctx_field_size;
Yonghong Song31fd8582017-06-13 15:52:13 -07004210 if (is_narrower_load) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02004211 u32 off = insn->off;
4212 u8 size_code;
Yonghong Song31fd8582017-06-13 15:52:13 -07004213
Daniel Borkmannf96da092017-07-02 02:13:27 +02004214 if (type == BPF_WRITE) {
4215 verbose("bpf verifier narrow ctx access misconfigured\n");
4216 return -EINVAL;
4217 }
4218
4219 size_code = BPF_H;
Yonghong Song31fd8582017-06-13 15:52:13 -07004220 if (ctx_field_size == 4)
4221 size_code = BPF_W;
4222 else if (ctx_field_size == 8)
4223 size_code = BPF_DW;
Daniel Borkmannf96da092017-07-02 02:13:27 +02004224
Yonghong Song31fd8582017-06-13 15:52:13 -07004225 insn->off = off & ~(ctx_field_size - 1);
4226 insn->code = BPF_LDX | BPF_MEM | size_code;
4227 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02004228
4229 target_size = 0;
4230 cnt = ops->convert_ctx_access(type, insn, insn_buf, env->prog,
4231 &target_size);
4232 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) ||
4233 (ctx_field_size && !target_size)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004234 verbose("bpf verifier is misconfigured\n");
4235 return -EINVAL;
4236 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02004237
4238 if (is_narrower_load && size < target_size) {
Yonghong Song31fd8582017-06-13 15:52:13 -07004239 if (ctx_field_size <= 4)
4240 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02004241 (1 << size * 8) - 1);
Yonghong Song31fd8582017-06-13 15:52:13 -07004242 else
4243 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02004244 (1 << size * 8) - 1);
Yonghong Song31fd8582017-06-13 15:52:13 -07004245 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004246
Alexei Starovoitov80419022017-03-15 18:26:41 -07004247 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004248 if (!new_prog)
4249 return -ENOMEM;
4250
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004251 delta += cnt - 1;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004252
4253 /* keep walking new program and skip insns we just inserted */
4254 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004255 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004256 }
4257
4258 return 0;
4259}
4260
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004261/* fixup insn->imm field of bpf_call instructions
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07004262 * and inline eligible helpers as explicit sequence of BPF instructions
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07004263 *
4264 * this function is called after eBPF program passed verification
4265 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004266static int fixup_bpf_calls(struct bpf_verifier_env *env)
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07004267{
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004268 struct bpf_prog *prog = env->prog;
4269 struct bpf_insn *insn = prog->insnsi;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07004270 const struct bpf_func_proto *fn;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004271 const int insn_cnt = prog->len;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07004272 struct bpf_insn insn_buf[16];
4273 struct bpf_prog *new_prog;
4274 struct bpf_map *map_ptr;
4275 int i, cnt, delta = 0;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07004276
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004277 for (i = 0; i < insn_cnt; i++, insn++) {
4278 if (insn->code != (BPF_JMP | BPF_CALL))
4279 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07004280
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004281 if (insn->imm == BPF_FUNC_get_route_realm)
4282 prog->dst_needed = 1;
4283 if (insn->imm == BPF_FUNC_get_prandom_u32)
4284 bpf_user_rnd_init_once();
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004285 if (insn->imm == BPF_FUNC_tail_call) {
David S. Miller7b9f6da2017-04-20 10:35:33 -04004286 /* If we tail call into other programs, we
4287 * cannot make any assumptions since they can
4288 * be replaced dynamically during runtime in
4289 * the program array.
4290 */
4291 prog->cb_access = 1;
Alexei Starovoitov80a58d02017-05-30 13:31:30 -07004292 env->prog->aux->stack_depth = MAX_BPF_STACK;
David S. Miller7b9f6da2017-04-20 10:35:33 -04004293
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004294 /* mark bpf_tail_call as different opcode to avoid
4295 * conditional branch in the interpeter for every normal
4296 * call and to prevent accidental JITing by JIT compiler
4297 * that doesn't support bpf_tail_call yet
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07004298 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004299 insn->imm = 0;
Alexei Starovoitov71189fa2017-05-30 13:31:27 -07004300 insn->code = BPF_JMP | BPF_TAIL_CALL;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004301 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07004302 }
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07004303
Daniel Borkmann89c63072017-08-19 03:12:45 +02004304 /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup
4305 * handlers are currently limited to 64 bit only.
4306 */
4307 if (ebpf_jit_enabled() && BITS_PER_LONG == 64 &&
4308 insn->imm == BPF_FUNC_map_lookup_elem) {
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07004309 map_ptr = env->insn_aux_data[i + delta].map_ptr;
Martin KaFai Laufad73a12017-03-22 10:00:32 -07004310 if (map_ptr == BPF_MAP_PTR_POISON ||
4311 !map_ptr->ops->map_gen_lookup)
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07004312 goto patch_call_imm;
4313
4314 cnt = map_ptr->ops->map_gen_lookup(map_ptr, insn_buf);
4315 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
4316 verbose("bpf verifier is misconfigured\n");
4317 return -EINVAL;
4318 }
4319
4320 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf,
4321 cnt);
4322 if (!new_prog)
4323 return -ENOMEM;
4324
4325 delta += cnt - 1;
4326
4327 /* keep walking new program and skip insns we just inserted */
4328 env->prog = prog = new_prog;
4329 insn = new_prog->insnsi + i + delta;
4330 continue;
4331 }
4332
Daniel Borkmann109980b2017-09-08 00:14:51 +02004333 if (insn->imm == BPF_FUNC_redirect_map) {
Daniel Borkmann7c300132017-09-20 00:44:21 +02004334 /* Note, we cannot use prog directly as imm as subsequent
4335 * rewrites would still change the prog pointer. The only
4336 * stable address we can use is aux, which also works with
4337 * prog clones during blinding.
4338 */
4339 u64 addr = (unsigned long)prog->aux;
Daniel Borkmann109980b2017-09-08 00:14:51 +02004340 struct bpf_insn r4_ld[] = {
4341 BPF_LD_IMM64(BPF_REG_4, addr),
4342 *insn,
4343 };
4344 cnt = ARRAY_SIZE(r4_ld);
4345
4346 new_prog = bpf_patch_insn_data(env, i + delta, r4_ld, cnt);
4347 if (!new_prog)
4348 return -ENOMEM;
4349
4350 delta += cnt - 1;
4351 env->prog = prog = new_prog;
4352 insn = new_prog->insnsi + i + delta;
4353 }
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07004354patch_call_imm:
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004355 fn = prog->aux->ops->get_func_proto(insn->imm);
4356 /* all functions that have prototype and verifier allowed
4357 * programs to call them, must be real in-kernel functions
4358 */
4359 if (!fn->func) {
4360 verbose("kernel subsystem misconfigured func %s#%d\n",
4361 func_id_name(insn->imm), insn->imm);
4362 return -EFAULT;
4363 }
4364 insn->imm = fn->func - __bpf_call_base;
4365 }
4366
4367 return 0;
4368}
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07004369
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004370static void free_states(struct bpf_verifier_env *env)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004371{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004372 struct bpf_verifier_state_list *sl, *sln;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004373 int i;
4374
4375 if (!env->explored_states)
4376 return;
4377
4378 for (i = 0; i < env->prog->len; i++) {
4379 sl = env->explored_states[i];
4380
4381 if (sl)
4382 while (sl != STATE_LIST_MARK) {
4383 sln = sl->next;
4384 kfree(sl);
4385 sl = sln;
4386 }
4387 }
4388
4389 kfree(env->explored_states);
4390}
4391
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004392int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
Alexei Starovoitov51580e72014-09-26 00:17:02 -07004393{
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07004394 struct bpf_verifer_log *log = &verifier_log;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004395 struct bpf_verifier_env *env;
Alexei Starovoitov51580e72014-09-26 00:17:02 -07004396 int ret = -EINVAL;
4397
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004398 /* 'struct bpf_verifier_env' can be global, but since it's not small,
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004399 * allocate/free it every time bpf_check() is called
4400 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004401 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004402 if (!env)
4403 return -ENOMEM;
4404
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004405 env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
4406 (*prog)->len);
4407 ret = -ENOMEM;
4408 if (!env->insn_aux_data)
4409 goto err_free_env;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004410 env->prog = *prog;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004411
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004412 /* grab the mutex to protect few globals used by verifier */
4413 mutex_lock(&bpf_verifier_lock);
4414
4415 if (attr->log_level || attr->log_buf || attr->log_size) {
4416 /* user requested verbose verifier output
4417 * and supplied buffer to store the verification trace
4418 */
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07004419 log->level = attr->log_level;
4420 log->ubuf = (char __user *) (unsigned long) attr->log_buf;
4421 log->len_total = attr->log_size;
4422 log->len_used = 0;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004423
4424 ret = -EINVAL;
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07004425 /* log attributes have to be sane */
4426 if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 ||
4427 !log->level || !log->ubuf)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004428 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004429
4430 ret = -ENOMEM;
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07004431 log->kbuf = vmalloc(log->len_total);
4432 if (!log->kbuf)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004433 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004434 } else {
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07004435 log->level = 0;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004436 }
Daniel Borkmann1ad2f582017-05-25 01:05:05 +02004437
4438 env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT);
4439 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
David S. Millere07b98d2017-05-10 11:38:07 -07004440 env->strict_alignment = true;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004441
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004442 ret = replace_map_fd_with_map_ptr(env);
4443 if (ret < 0)
4444 goto skip_full_check;
4445
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004446 env->explored_states = kcalloc(env->prog->len,
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004447 sizeof(struct bpf_verifier_state_list *),
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004448 GFP_USER);
4449 ret = -ENOMEM;
4450 if (!env->explored_states)
4451 goto skip_full_check;
4452
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004453 ret = check_cfg(env);
4454 if (ret < 0)
4455 goto skip_full_check;
4456
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004457 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
4458
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004459 ret = do_check(env);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004460
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004461skip_full_check:
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004462 while (pop_stack(env, NULL) >= 0);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004463 free_states(env);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004464
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004465 if (ret == 0)
4466 /* program is valid, convert *(u32*)(ctx + off) accesses */
4467 ret = convert_ctx_accesses(env);
4468
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07004469 if (ret == 0)
Alexei Starovoitov79741b32017-03-15 18:26:40 -07004470 ret = fixup_bpf_calls(env);
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07004471
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07004472 if (log->level && bpf_verifier_log_full(log)) {
4473 BUG_ON(log->len_used >= log->len_total);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004474 /* verifier log exceeded user supplied buffer */
4475 ret = -ENOSPC;
4476 /* fall through to return what was recorded */
4477 }
4478
4479 /* copy verifier log back to user space including trailing zero */
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07004480 if (log->level && copy_to_user(log->ubuf, log->kbuf,
4481 log->len_used + 1) != 0) {
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004482 ret = -EFAULT;
4483 goto free_log_buf;
4484 }
4485
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004486 if (ret == 0 && env->used_map_cnt) {
4487 /* if program passed verifier, update used_maps in bpf_prog_info */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004488 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
4489 sizeof(env->used_maps[0]),
4490 GFP_KERNEL);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004491
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004492 if (!env->prog->aux->used_maps) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004493 ret = -ENOMEM;
4494 goto free_log_buf;
4495 }
4496
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004497 memcpy(env->prog->aux->used_maps, env->used_maps,
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004498 sizeof(env->used_maps[0]) * env->used_map_cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004499 env->prog->aux->used_map_cnt = env->used_map_cnt;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004500
4501 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
4502 * bpf_ld_imm64 instructions
4503 */
4504 convert_pseudo_ld_imm64(env);
4505 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004506
4507free_log_buf:
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07004508 if (log->level)
4509 vfree(log->kbuf);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004510 if (!env->prog->aux->used_maps)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004511 /* if we didn't copy map pointers into bpf_prog_info, release
4512 * them now. Otherwise free_bpf_prog_info() will release them.
4513 */
4514 release_maps(env);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004515 *prog = env->prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004516err_unlock:
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07004517 mutex_unlock(&bpf_verifier_lock);
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004518 vfree(env->insn_aux_data);
4519err_free_env:
4520 kfree(env);
Alexei Starovoitov51580e72014-09-26 00:17:02 -07004521 return ret;
4522}
Jakub Kicinski13a27df2016-09-21 11:43:58 +01004523
4524int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops,
4525 void *priv)
4526{
4527 struct bpf_verifier_env *env;
4528 int ret;
4529
4530 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
4531 if (!env)
4532 return -ENOMEM;
4533
4534 env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
4535 prog->len);
4536 ret = -ENOMEM;
4537 if (!env->insn_aux_data)
4538 goto err_free_env;
4539 env->prog = prog;
4540 env->analyzer_ops = ops;
4541 env->analyzer_priv = priv;
4542
4543 /* grab the mutex to protect few globals used by verifier */
4544 mutex_lock(&bpf_verifier_lock);
4545
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07004546 verifier_log.level = 0;
Daniel Borkmann1ad2f582017-05-25 01:05:05 +02004547
David S. Millere07b98d2017-05-10 11:38:07 -07004548 env->strict_alignment = false;
Daniel Borkmann1ad2f582017-05-25 01:05:05 +02004549 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
4550 env->strict_alignment = true;
Jakub Kicinski13a27df2016-09-21 11:43:58 +01004551
4552 env->explored_states = kcalloc(env->prog->len,
4553 sizeof(struct bpf_verifier_state_list *),
4554 GFP_KERNEL);
4555 ret = -ENOMEM;
4556 if (!env->explored_states)
4557 goto skip_full_check;
4558
4559 ret = check_cfg(env);
4560 if (ret < 0)
4561 goto skip_full_check;
4562
4563 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
4564
4565 ret = do_check(env);
4566
4567skip_full_check:
4568 while (pop_stack(env, NULL) >= 0);
4569 free_states(env);
4570
4571 mutex_unlock(&bpf_verifier_lock);
4572 vfree(env->insn_aux_data);
4573err_free_env:
4574 kfree(env);
4575 return ret;
4576}
4577EXPORT_SYMBOL_GPL(bpf_analyzer);