blob: ebe9b38ff52227767c3083cab06f28dcc98a50a9 [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 *
64 * Most of the time the registers have UNKNOWN_VALUE type, which
65 * means the register has some value, but it's not a valid pointer.
66 * (like pointer plus pointer becomes UNKNOWN_VALUE type)
67 *
68 * When verifier sees load or store instructions the type of base register
69 * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, FRAME_PTR. These are three pointer
70 * types recognized by check_mem_access() function.
71 *
72 * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value'
73 * and the range of [ptr, ptr + map's value_size) is accessible.
74 *
75 * registers used to pass values to function calls are checked against
76 * function argument constraints.
77 *
78 * ARG_PTR_TO_MAP_KEY is one of such argument constraints.
79 * It means that the register type passed to this function must be
80 * PTR_TO_STACK and it will be used inside the function as
81 * 'pointer to map element key'
82 *
83 * For example the argument constraints for bpf_map_lookup_elem():
84 * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
85 * .arg1_type = ARG_CONST_MAP_PTR,
86 * .arg2_type = ARG_PTR_TO_MAP_KEY,
87 *
88 * ret_type says that this function returns 'pointer to map elem value or null'
89 * function expects 1st argument to be a const pointer to 'struct bpf_map' and
90 * 2nd argument should be a pointer to stack, which will be used inside
91 * the helper function as a pointer to map element key.
92 *
93 * On the kernel side the helper function looks like:
94 * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
95 * {
96 * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
97 * void *key = (void *) (unsigned long) r2;
98 * void *value;
99 *
100 * here kernel can access 'key' and 'map' pointers safely, knowing that
101 * [key, key + map->key_size) bytes are valid and were initialized on
102 * the stack of eBPF program.
103 * }
104 *
105 * Corresponding eBPF program may look like:
106 * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR
107 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK
108 * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP
109 * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
110 * here verifier looks at prototype of map_lookup_elem() and sees:
111 * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok,
112 * Now verifier knows that this map has key of R1->map_ptr->key_size bytes
113 *
114 * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far,
115 * Now verifier checks that [R2, R2 + map's key_size) are within stack limits
116 * and were initialized prior to this call.
117 * If it's ok, then verifier allows this BPF_CALL insn and looks at
118 * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets
119 * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function
120 * returns ether pointer to map value or NULL.
121 *
122 * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off'
123 * insn, the register holding that pointer in the true branch changes state to
124 * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false
125 * branch. See check_cond_jmp_op().
126 *
127 * After the call R0 is set to return type of the function and registers R1-R5
128 * are set to NOT_INIT to indicate that they are no longer readable.
129 */
130
Alexei 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
Daniel Borkmann3c2ce602017-05-18 03:00:06 +0200143#define BPF_COMPLEXITY_LIMIT_INSNS 98304
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 */
159static u32 log_level, log_size, log_len;
160static char *log_buf;
161
162static DEFINE_MUTEX(bpf_verifier_lock);
163
164/* log_level controls verbosity level of eBPF verifier.
165 * verbose() is used to dump the verification trace to the log, so the user
166 * can figure out what's wrong with the program
167 */
Daniel Borkmann1d056d92015-11-03 11:39:20 +0100168static __printf(1, 2) void verbose(const char *fmt, ...)
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700169{
170 va_list args;
171
172 if (log_level == 0 || log_len >= log_size - 1)
173 return;
174
175 va_start(args, fmt);
176 log_len += vscnprintf(log_buf + log_len, log_size - log_len, fmt, args);
177 va_end(args);
178}
179
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700180/* string representation of 'enum bpf_reg_type' */
181static const char * const reg_type_str[] = {
182 [NOT_INIT] = "?",
183 [UNKNOWN_VALUE] = "inv",
184 [PTR_TO_CTX] = "ctx",
185 [CONST_PTR_TO_MAP] = "map_ptr",
186 [PTR_TO_MAP_VALUE] = "map_value",
187 [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
Josef Bacik48461132016-09-28 10:54:32 -0400188 [PTR_TO_MAP_VALUE_ADJ] = "map_value_adj",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700189 [FRAME_PTR] = "fp",
190 [PTR_TO_STACK] = "fp",
191 [CONST_IMM] = "imm",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700192 [PTR_TO_PACKET] = "pkt",
193 [PTR_TO_PACKET_END] = "pkt_end",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700194};
195
Thomas Grafebb676d2016-10-27 11:23:51 +0200196#define __BPF_FUNC_STR_FN(x) [BPF_FUNC_ ## x] = __stringify(bpf_ ## x)
197static const char * const func_id_str[] = {
198 __BPF_FUNC_MAPPER(__BPF_FUNC_STR_FN)
199};
200#undef __BPF_FUNC_STR_FN
201
202static const char *func_id_name(int id)
203{
204 BUILD_BUG_ON(ARRAY_SIZE(func_id_str) != __BPF_FUNC_MAX_ID);
205
206 if (id >= 0 && id < __BPF_FUNC_MAX_ID && func_id_str[id])
207 return func_id_str[id];
208 else
209 return "unknown";
210}
211
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100212static void print_verifier_state(struct bpf_verifier_state *state)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700213{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100214 struct bpf_reg_state *reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700215 enum bpf_reg_type t;
216 int i;
217
218 for (i = 0; i < MAX_BPF_REG; i++) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700219 reg = &state->regs[i];
220 t = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700221 if (t == NOT_INIT)
222 continue;
223 verbose(" R%d=%s", i, reg_type_str[t]);
224 if (t == CONST_IMM || t == PTR_TO_STACK)
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700225 verbose("%lld", reg->imm);
226 else if (t == PTR_TO_PACKET)
227 verbose("(id=%d,off=%d,r=%d)",
228 reg->id, reg->off, reg->range);
229 else if (t == UNKNOWN_VALUE && reg->imm)
230 verbose("%lld", reg->imm);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700231 else if (t == CONST_PTR_TO_MAP || t == PTR_TO_MAP_VALUE ||
Josef Bacik48461132016-09-28 10:54:32 -0400232 t == PTR_TO_MAP_VALUE_OR_NULL ||
233 t == PTR_TO_MAP_VALUE_ADJ)
Thomas Graf57a09bf2016-10-18 19:51:19 +0200234 verbose("(ks=%d,vs=%d,id=%u)",
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700235 reg->map_ptr->key_size,
Thomas Graf57a09bf2016-10-18 19:51:19 +0200236 reg->map_ptr->value_size,
237 reg->id);
Josef Bacik48461132016-09-28 10:54:32 -0400238 if (reg->min_value != BPF_REGISTER_MIN_RANGE)
Josef Bacikf23cc642016-11-14 15:45:36 -0500239 verbose(",min_value=%lld",
240 (long long)reg->min_value);
Josef Bacik48461132016-09-28 10:54:32 -0400241 if (reg->max_value != BPF_REGISTER_MAX_RANGE)
242 verbose(",max_value=%llu",
243 (unsigned long long)reg->max_value);
David S. Millerd1174412017-05-10 11:22:52 -0700244 if (reg->min_align)
245 verbose(",min_align=%u", reg->min_align);
246 if (reg->aux_off)
247 verbose(",aux_off=%u", reg->aux_off);
248 if (reg->aux_off_align)
249 verbose(",aux_off_align=%u", reg->aux_off_align);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700250 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700251 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700252 if (state->stack_slot_type[i] == STACK_SPILL)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700253 verbose(" fp%d=%s", -MAX_BPF_STACK + i,
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700254 reg_type_str[state->spilled_regs[i / BPF_REG_SIZE].type]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700255 }
256 verbose("\n");
257}
258
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700259static const char *const bpf_class_string[] = {
260 [BPF_LD] = "ld",
261 [BPF_LDX] = "ldx",
262 [BPF_ST] = "st",
263 [BPF_STX] = "stx",
264 [BPF_ALU] = "alu",
265 [BPF_JMP] = "jmp",
266 [BPF_RET] = "BUG",
267 [BPF_ALU64] = "alu64",
268};
269
Alexei Starovoitov687f0712015-09-08 13:40:01 -0700270static const char *const bpf_alu_string[16] = {
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700271 [BPF_ADD >> 4] = "+=",
272 [BPF_SUB >> 4] = "-=",
273 [BPF_MUL >> 4] = "*=",
274 [BPF_DIV >> 4] = "/=",
275 [BPF_OR >> 4] = "|=",
276 [BPF_AND >> 4] = "&=",
277 [BPF_LSH >> 4] = "<<=",
278 [BPF_RSH >> 4] = ">>=",
279 [BPF_NEG >> 4] = "neg",
280 [BPF_MOD >> 4] = "%=",
281 [BPF_XOR >> 4] = "^=",
282 [BPF_MOV >> 4] = "=",
283 [BPF_ARSH >> 4] = "s>>=",
284 [BPF_END >> 4] = "endian",
285};
286
287static const char *const bpf_ldst_string[] = {
288 [BPF_W >> 3] = "u32",
289 [BPF_H >> 3] = "u16",
290 [BPF_B >> 3] = "u8",
291 [BPF_DW >> 3] = "u64",
292};
293
Alexei Starovoitov687f0712015-09-08 13:40:01 -0700294static const char *const bpf_jmp_string[16] = {
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700295 [BPF_JA >> 4] = "jmp",
296 [BPF_JEQ >> 4] = "==",
297 [BPF_JGT >> 4] = ">",
298 [BPF_JGE >> 4] = ">=",
299 [BPF_JSET >> 4] = "&",
300 [BPF_JNE >> 4] = "!=",
301 [BPF_JSGT >> 4] = "s>",
302 [BPF_JSGE >> 4] = "s>=",
303 [BPF_CALL >> 4] = "call",
304 [BPF_EXIT >> 4] = "exit",
305};
306
Daniel Borkmann0d0e5762017-05-08 00:04:09 +0200307static void print_bpf_insn(const struct bpf_verifier_env *env,
308 const struct bpf_insn *insn)
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700309{
310 u8 class = BPF_CLASS(insn->code);
311
312 if (class == BPF_ALU || class == BPF_ALU64) {
313 if (BPF_SRC(insn->code) == BPF_X)
314 verbose("(%02x) %sr%d %s %sr%d\n",
315 insn->code, class == BPF_ALU ? "(u32) " : "",
316 insn->dst_reg,
317 bpf_alu_string[BPF_OP(insn->code) >> 4],
318 class == BPF_ALU ? "(u32) " : "",
319 insn->src_reg);
320 else
321 verbose("(%02x) %sr%d %s %s%d\n",
322 insn->code, class == BPF_ALU ? "(u32) " : "",
323 insn->dst_reg,
324 bpf_alu_string[BPF_OP(insn->code) >> 4],
325 class == BPF_ALU ? "(u32) " : "",
326 insn->imm);
327 } else if (class == BPF_STX) {
328 if (BPF_MODE(insn->code) == BPF_MEM)
329 verbose("(%02x) *(%s *)(r%d %+d) = r%d\n",
330 insn->code,
331 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
332 insn->dst_reg,
333 insn->off, insn->src_reg);
334 else if (BPF_MODE(insn->code) == BPF_XADD)
335 verbose("(%02x) lock *(%s *)(r%d %+d) += r%d\n",
336 insn->code,
337 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
338 insn->dst_reg, insn->off,
339 insn->src_reg);
340 else
341 verbose("BUG_%02x\n", insn->code);
342 } else if (class == BPF_ST) {
343 if (BPF_MODE(insn->code) != BPF_MEM) {
344 verbose("BUG_st_%02x\n", insn->code);
345 return;
346 }
347 verbose("(%02x) *(%s *)(r%d %+d) = %d\n",
348 insn->code,
349 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
350 insn->dst_reg,
351 insn->off, insn->imm);
352 } else if (class == BPF_LDX) {
353 if (BPF_MODE(insn->code) != BPF_MEM) {
354 verbose("BUG_ldx_%02x\n", insn->code);
355 return;
356 }
357 verbose("(%02x) r%d = *(%s *)(r%d %+d)\n",
358 insn->code, insn->dst_reg,
359 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
360 insn->src_reg, insn->off);
361 } else if (class == BPF_LD) {
362 if (BPF_MODE(insn->code) == BPF_ABS) {
363 verbose("(%02x) r0 = *(%s *)skb[%d]\n",
364 insn->code,
365 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
366 insn->imm);
367 } else if (BPF_MODE(insn->code) == BPF_IND) {
368 verbose("(%02x) r0 = *(%s *)skb[r%d + %d]\n",
369 insn->code,
370 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
371 insn->src_reg, insn->imm);
Daniel Borkmann0d0e5762017-05-08 00:04:09 +0200372 } else if (BPF_MODE(insn->code) == BPF_IMM &&
373 BPF_SIZE(insn->code) == BPF_DW) {
374 /* At this point, we already made sure that the second
375 * part of the ldimm64 insn is accessible.
376 */
377 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
378 bool map_ptr = insn->src_reg == BPF_PSEUDO_MAP_FD;
379
380 if (map_ptr && !env->allow_ptr_leaks)
381 imm = 0;
382
383 verbose("(%02x) r%d = 0x%llx\n", insn->code,
384 insn->dst_reg, (unsigned long long)imm);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700385 } else {
386 verbose("BUG_ld_%02x\n", insn->code);
387 return;
388 }
389 } else if (class == BPF_JMP) {
390 u8 opcode = BPF_OP(insn->code);
391
392 if (opcode == BPF_CALL) {
Thomas Grafebb676d2016-10-27 11:23:51 +0200393 verbose("(%02x) call %s#%d\n", insn->code,
394 func_id_name(insn->imm), insn->imm);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700395 } else if (insn->code == (BPF_JMP | BPF_JA)) {
396 verbose("(%02x) goto pc%+d\n",
397 insn->code, insn->off);
398 } else if (insn->code == (BPF_JMP | BPF_EXIT)) {
399 verbose("(%02x) exit\n", insn->code);
400 } else if (BPF_SRC(insn->code) == BPF_X) {
401 verbose("(%02x) if r%d %s r%d goto pc%+d\n",
402 insn->code, insn->dst_reg,
403 bpf_jmp_string[BPF_OP(insn->code) >> 4],
404 insn->src_reg, insn->off);
405 } else {
406 verbose("(%02x) if r%d %s 0x%x goto pc%+d\n",
407 insn->code, insn->dst_reg,
408 bpf_jmp_string[BPF_OP(insn->code) >> 4],
409 insn->imm, insn->off);
410 }
411 } else {
412 verbose("(%02x) %s\n", insn->code, bpf_class_string[class]);
413 }
414}
415
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100416static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700417{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100418 struct bpf_verifier_stack_elem *elem;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700419 int insn_idx;
420
421 if (env->head == NULL)
422 return -1;
423
424 memcpy(&env->cur_state, &env->head->st, sizeof(env->cur_state));
425 insn_idx = env->head->insn_idx;
426 if (prev_insn_idx)
427 *prev_insn_idx = env->head->prev_insn_idx;
428 elem = env->head->next;
429 kfree(env->head);
430 env->head = elem;
431 env->stack_size--;
432 return insn_idx;
433}
434
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100435static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,
436 int insn_idx, int prev_insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700437{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100438 struct bpf_verifier_stack_elem *elem;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700439
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100440 elem = kmalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700441 if (!elem)
442 goto err;
443
444 memcpy(&elem->st, &env->cur_state, sizeof(env->cur_state));
445 elem->insn_idx = insn_idx;
446 elem->prev_insn_idx = prev_insn_idx;
447 elem->next = env->head;
448 env->head = elem;
449 env->stack_size++;
Daniel Borkmann07016152016-04-05 22:33:17 +0200450 if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700451 verbose("BPF program is too complex\n");
452 goto err;
453 }
454 return &elem->st;
455err:
456 /* pop all elements and return */
457 while (pop_stack(env, NULL) >= 0);
458 return NULL;
459}
460
461#define CALLER_SAVED_REGS 6
462static const int caller_saved[CALLER_SAVED_REGS] = {
463 BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
464};
465
Daniel Borkmanna9789ef2017-05-25 01:05:06 +0200466static void mark_reg_not_init(struct bpf_reg_state *regs, u32 regno)
467{
468 BUG_ON(regno >= MAX_BPF_REG);
469
470 memset(&regs[regno], 0, sizeof(regs[regno]));
471 regs[regno].type = NOT_INIT;
472 regs[regno].min_value = BPF_REGISTER_MIN_RANGE;
473 regs[regno].max_value = BPF_REGISTER_MAX_RANGE;
474}
475
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100476static void init_reg_state(struct bpf_reg_state *regs)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700477{
478 int i;
479
Daniel Borkmanna9789ef2017-05-25 01:05:06 +0200480 for (i = 0; i < MAX_BPF_REG; i++)
481 mark_reg_not_init(regs, i);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700482
483 /* frame pointer */
484 regs[BPF_REG_FP].type = FRAME_PTR;
485
486 /* 1st arg to a function */
487 regs[BPF_REG_1].type = PTR_TO_CTX;
488}
489
Daniel Borkmann6760bf22016-12-18 01:52:59 +0100490static void __mark_reg_unknown_value(struct bpf_reg_state *regs, u32 regno)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700491{
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700492 regs[regno].type = UNKNOWN_VALUE;
Thomas Graf57a09bf2016-10-18 19:51:19 +0200493 regs[regno].id = 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700494 regs[regno].imm = 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700495}
496
Daniel Borkmann6760bf22016-12-18 01:52:59 +0100497static void mark_reg_unknown_value(struct bpf_reg_state *regs, u32 regno)
498{
499 BUG_ON(regno >= MAX_BPF_REG);
500 __mark_reg_unknown_value(regs, regno);
501}
502
Josef Bacik48461132016-09-28 10:54:32 -0400503static void reset_reg_range_values(struct bpf_reg_state *regs, u32 regno)
504{
505 regs[regno].min_value = BPF_REGISTER_MIN_RANGE;
506 regs[regno].max_value = BPF_REGISTER_MAX_RANGE;
David S. Millerd1174412017-05-10 11:22:52 -0700507 regs[regno].min_align = 0;
Josef Bacik48461132016-09-28 10:54:32 -0400508}
509
Gianluca Borellof0318d02017-01-09 10:19:48 -0800510static void mark_reg_unknown_value_and_range(struct bpf_reg_state *regs,
511 u32 regno)
512{
513 mark_reg_unknown_value(regs, regno);
514 reset_reg_range_values(regs, regno);
515}
516
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700517enum reg_arg_type {
518 SRC_OP, /* register is used as source operand */
519 DST_OP, /* register is used as destination operand */
520 DST_OP_NO_MARK /* same as above, check only, don't mark */
521};
522
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100523static int check_reg_arg(struct bpf_reg_state *regs, u32 regno,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700524 enum reg_arg_type t)
525{
526 if (regno >= MAX_BPF_REG) {
527 verbose("R%d is invalid\n", regno);
528 return -EINVAL;
529 }
530
531 if (t == SRC_OP) {
532 /* check whether register used as source operand can be read */
533 if (regs[regno].type == NOT_INIT) {
534 verbose("R%d !read_ok\n", regno);
535 return -EACCES;
536 }
537 } else {
538 /* check whether register used as dest operand can be written to */
539 if (regno == BPF_REG_FP) {
540 verbose("frame pointer is read only\n");
541 return -EACCES;
542 }
543 if (t == DST_OP)
544 mark_reg_unknown_value(regs, regno);
545 }
546 return 0;
547}
548
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700549static bool is_spillable_regtype(enum bpf_reg_type type)
550{
551 switch (type) {
552 case PTR_TO_MAP_VALUE:
553 case PTR_TO_MAP_VALUE_OR_NULL:
Gianluca Borellof0318d02017-01-09 10:19:48 -0800554 case PTR_TO_MAP_VALUE_ADJ:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700555 case PTR_TO_STACK:
556 case PTR_TO_CTX:
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700557 case PTR_TO_PACKET:
558 case PTR_TO_PACKET_END:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700559 case FRAME_PTR:
560 case CONST_PTR_TO_MAP:
561 return true;
562 default:
563 return false;
564 }
565}
566
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700567/* check_stack_read/write functions track spill/fill of registers,
568 * stack boundary and alignment are checked in check_mem_access()
569 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100570static int check_stack_write(struct bpf_verifier_state *state, int off,
571 int size, int value_regno)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700572{
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700573 int i;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700574 /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
575 * so it's aligned access and [off, off + size) are within stack limits
576 */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700577
578 if (value_regno >= 0 &&
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700579 is_spillable_regtype(state->regs[value_regno].type)) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700580
581 /* register containing pointer is being spilled into stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700582 if (size != BPF_REG_SIZE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700583 verbose("invalid size of register spill\n");
584 return -EACCES;
585 }
586
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700587 /* save register state */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700588 state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] =
589 state->regs[value_regno];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700590
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700591 for (i = 0; i < BPF_REG_SIZE; i++)
592 state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_SPILL;
593 } else {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700594 /* regular write of data into stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700595 state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] =
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100596 (struct bpf_reg_state) {};
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700597
598 for (i = 0; i < size; i++)
599 state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_MISC;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700600 }
601 return 0;
602}
603
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100604static int check_stack_read(struct bpf_verifier_state *state, int off, int size,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700605 int value_regno)
606{
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700607 u8 *slot_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700608 int i;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700609
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700610 slot_type = &state->stack_slot_type[MAX_BPF_STACK + off];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700611
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700612 if (slot_type[0] == STACK_SPILL) {
613 if (size != BPF_REG_SIZE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700614 verbose("invalid size of register spill\n");
615 return -EACCES;
616 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700617 for (i = 1; i < BPF_REG_SIZE; i++) {
618 if (slot_type[i] != STACK_SPILL) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700619 verbose("corrupted spill memory\n");
620 return -EACCES;
621 }
622 }
623
624 if (value_regno >= 0)
625 /* restore register state from stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700626 state->regs[value_regno] =
627 state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700628 return 0;
629 } else {
630 for (i = 0; i < size; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700631 if (slot_type[i] != STACK_MISC) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700632 verbose("invalid read from stack off %d+%d size %d\n",
633 off, i, size);
634 return -EACCES;
635 }
636 }
637 if (value_regno >= 0)
638 /* have read misc data from the stack */
Gianluca Borellof0318d02017-01-09 10:19:48 -0800639 mark_reg_unknown_value_and_range(state->regs,
640 value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700641 return 0;
642 }
643}
644
645/* check read/write into map element returned by bpf_map_lookup_elem() */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100646static int check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700647 int size)
648{
649 struct bpf_map *map = env->cur_state.regs[regno].map_ptr;
650
Gianluca Borello57225692017-01-09 10:19:47 -0800651 if (off < 0 || size <= 0 || off + size > map->value_size) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700652 verbose("invalid access to map value, value_size=%d off=%d size=%d\n",
653 map->value_size, off, size);
654 return -EACCES;
655 }
656 return 0;
657}
658
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -0800659/* check read/write into an adjusted map element */
660static int check_map_access_adj(struct bpf_verifier_env *env, u32 regno,
661 int off, int size)
662{
663 struct bpf_verifier_state *state = &env->cur_state;
664 struct bpf_reg_state *reg = &state->regs[regno];
665 int err;
666
667 /* We adjusted the register to this map value, so we
668 * need to change off and size to min_value and max_value
669 * respectively to make sure our theoretical access will be
670 * safe.
671 */
672 if (log_level)
673 print_verifier_state(state);
674 env->varlen_map_value_access = true;
675 /* The minimum value is only important with signed
676 * comparisons where we can't assume the floor of a
677 * value is 0. If we are using signed variables for our
678 * index'es we need to make sure that whatever we use
679 * will have a set floor within our range.
680 */
681 if (reg->min_value < 0) {
682 verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
683 regno);
684 return -EACCES;
685 }
686 err = check_map_access(env, regno, reg->min_value + off, size);
687 if (err) {
688 verbose("R%d min value is outside of the array range\n",
689 regno);
690 return err;
691 }
692
693 /* If we haven't set a max value then we need to bail
694 * since we can't be sure we won't do bad things.
695 */
696 if (reg->max_value == BPF_REGISTER_MAX_RANGE) {
697 verbose("R%d unbounded memory access, make sure to bounds check any array access into a map\n",
698 regno);
699 return -EACCES;
700 }
701 return check_map_access(env, regno, reg->max_value + off, size);
702}
703
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700704#define MAX_PACKET_OFF 0xffff
705
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100706static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
Thomas Graf3a0af8f2016-11-30 17:10:10 +0100707 const struct bpf_call_arg_meta *meta,
708 enum bpf_access_type t)
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700709{
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200710 switch (env->prog->type) {
Thomas Graf3a0af8f2016-11-30 17:10:10 +0100711 case BPF_PROG_TYPE_LWT_IN:
712 case BPF_PROG_TYPE_LWT_OUT:
713 /* dst_input() and dst_output() can't write for now */
714 if (t == BPF_WRITE)
715 return false;
Alexander Alemayhu7e57fbb2017-02-14 00:02:35 +0100716 /* fallthrough */
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200717 case BPF_PROG_TYPE_SCHED_CLS:
718 case BPF_PROG_TYPE_SCHED_ACT:
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700719 case BPF_PROG_TYPE_XDP:
Thomas Graf3a0af8f2016-11-30 17:10:10 +0100720 case BPF_PROG_TYPE_LWT_XMIT:
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200721 if (meta)
722 return meta->pkt_access;
723
724 env->seen_direct_write = true;
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700725 return true;
726 default:
727 return false;
728 }
729}
730
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100731static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700732 int size)
733{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100734 struct bpf_reg_state *regs = env->cur_state.regs;
735 struct bpf_reg_state *reg = &regs[regno];
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700736
Alexei Starovoitovd91b28e2016-05-19 18:17:13 -0700737 off += reg->off;
Daniel Borkmannb399cf62016-09-20 00:26:12 +0200738 if (off < 0 || size <= 0 || off + size > reg->range) {
Alexei Starovoitovd91b28e2016-05-19 18:17:13 -0700739 verbose("invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n",
740 off, size, regno, reg->id, reg->off, reg->range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700741 return -EACCES;
742 }
743 return 0;
744}
745
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700746/* check access to 'struct bpf_context' fields */
Yonghong Song31fd8582017-06-13 15:52:13 -0700747static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700748 enum bpf_access_type t, enum bpf_reg_type *reg_type)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700749{
Daniel Borkmannf96da092017-07-02 02:13:27 +0200750 struct bpf_insn_access_aux info = {
751 .reg_type = *reg_type,
752 };
Yonghong Song31fd8582017-06-13 15:52:13 -0700753
Jakub Kicinski13a27df2016-09-21 11:43:58 +0100754 /* for analyzer ctx accesses are already validated and converted */
755 if (env->analyzer_ops)
756 return 0;
757
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700758 if (env->prog->aux->ops->is_valid_access &&
Yonghong Song23994632017-06-22 15:07:39 -0700759 env->prog->aux->ops->is_valid_access(off, size, t, &info)) {
Daniel Borkmannf96da092017-07-02 02:13:27 +0200760 /* A non zero info.ctx_field_size indicates that this field is a
761 * candidate for later verifier transformation to load the whole
762 * field and then apply a mask when accessed with a narrower
763 * access than actual ctx access size. A zero info.ctx_field_size
764 * will only allow for whole field access and rejects any other
765 * type of narrower access.
Yonghong Song31fd8582017-06-13 15:52:13 -0700766 */
Daniel Borkmannf96da092017-07-02 02:13:27 +0200767 env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size;
Yonghong Song23994632017-06-22 15:07:39 -0700768 *reg_type = info.reg_type;
Yonghong Song31fd8582017-06-13 15:52:13 -0700769
Alexei Starovoitov32bbe002016-04-06 18:43:28 -0700770 /* remember the offset of last byte accessed in ctx */
771 if (env->prog->aux->max_ctx_offset < off + size)
772 env->prog->aux->max_ctx_offset = off + size;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700773 return 0;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -0700774 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700775
776 verbose("invalid bpf_context access off=%d size=%d\n", off, size);
777 return -EACCES;
778}
779
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100780static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700781{
782 if (env->allow_ptr_leaks)
783 return false;
784
785 switch (env->cur_state.regs[regno].type) {
786 case UNKNOWN_VALUE:
787 case CONST_IMM:
788 return false;
789 default:
790 return true;
791 }
792}
793
Daniel Borkmann79adffc2017-03-31 02:24:03 +0200794static int check_pkt_ptr_alignment(const struct bpf_reg_state *reg,
David S. Millerd1174412017-05-10 11:22:52 -0700795 int off, int size, bool strict)
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700796{
David S. Millere07b98d2017-05-10 11:38:07 -0700797 int ip_align;
David S. Millerd1174412017-05-10 11:22:52 -0700798 int reg_off;
799
800 /* Byte size accesses are always allowed. */
801 if (!strict || size == 1)
802 return 0;
803
804 reg_off = reg->off;
805 if (reg->id) {
806 if (reg->aux_off_align % size) {
807 verbose("Packet access is only %u byte aligned, %d byte access not allowed\n",
808 reg->aux_off_align, size);
809 return -EACCES;
810 }
811 reg_off += reg->aux_off;
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700812 }
813
David S. Millere4eda882017-05-22 12:27:07 -0400814 /* For platforms that do not have a Kconfig enabling
815 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of
816 * NET_IP_ALIGN is universally set to '2'. And on platforms
817 * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get
818 * to this code only in strict mode where we want to emulate
819 * the NET_IP_ALIGN==2 checking. Therefore use an
820 * unconditional IP align value of '2'.
David S. Millere07b98d2017-05-10 11:38:07 -0700821 */
David S. Millere4eda882017-05-22 12:27:07 -0400822 ip_align = 2;
David S. Millere07b98d2017-05-10 11:38:07 -0700823 if ((ip_align + reg_off + off) % size != 0) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700824 verbose("misaligned packet access off %d+%d+%d size %d\n",
David S. Millere07b98d2017-05-10 11:38:07 -0700825 ip_align, reg_off, off, size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700826 return -EACCES;
827 }
Daniel Borkmann79adffc2017-03-31 02:24:03 +0200828
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700829 return 0;
830}
831
Daniel Borkmann79adffc2017-03-31 02:24:03 +0200832static int check_val_ptr_alignment(const struct bpf_reg_state *reg,
David S. Millerd1174412017-05-10 11:22:52 -0700833 int size, bool strict)
Daniel Borkmann79adffc2017-03-31 02:24:03 +0200834{
David S. Millerd1174412017-05-10 11:22:52 -0700835 if (strict && size != 1) {
Daniel Borkmann79adffc2017-03-31 02:24:03 +0200836 verbose("Unknown alignment. Only byte-sized access allowed in value access.\n");
837 return -EACCES;
838 }
839
840 return 0;
841}
842
David S. Millere07b98d2017-05-10 11:38:07 -0700843static int check_ptr_alignment(struct bpf_verifier_env *env,
844 const struct bpf_reg_state *reg,
Daniel Borkmann79adffc2017-03-31 02:24:03 +0200845 int off, int size)
846{
David S. Millere07b98d2017-05-10 11:38:07 -0700847 bool strict = env->strict_alignment;
David S. Millerd1174412017-05-10 11:22:52 -0700848
Daniel Borkmann79adffc2017-03-31 02:24:03 +0200849 switch (reg->type) {
850 case PTR_TO_PACKET:
David S. Millerd1174412017-05-10 11:22:52 -0700851 return check_pkt_ptr_alignment(reg, off, size, strict);
Daniel Borkmann79adffc2017-03-31 02:24:03 +0200852 case PTR_TO_MAP_VALUE_ADJ:
David S. Millerd1174412017-05-10 11:22:52 -0700853 return check_val_ptr_alignment(reg, size, strict);
Daniel Borkmann79adffc2017-03-31 02:24:03 +0200854 default:
855 if (off % size != 0) {
856 verbose("misaligned access off %d size %d\n",
857 off, size);
858 return -EACCES;
859 }
860
861 return 0;
862 }
863}
864
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700865/* check whether memory at (regno + off) is accessible for t = (read | write)
866 * if t==write, value_regno is a register which value is stored into memory
867 * if t==read, value_regno is a register which will receive the value from memory
868 * if t==write && value_regno==-1, some unknown value is stored into memory
869 * if t==read && value_regno==-1, don't care what we read from memory
870 */
Yonghong Song31fd8582017-06-13 15:52:13 -0700871static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, int off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700872 int bpf_size, enum bpf_access_type t,
873 int value_regno)
874{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100875 struct bpf_verifier_state *state = &env->cur_state;
876 struct bpf_reg_state *reg = &state->regs[regno];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700877 int size, err = 0;
878
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700879 if (reg->type == PTR_TO_STACK)
880 off += reg->imm;
Alex Gartrell24b4d2a2015-07-23 14:24:40 -0700881
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700882 size = bpf_size_to_bytes(bpf_size);
883 if (size < 0)
884 return size;
885
David S. Millere07b98d2017-05-10 11:38:07 -0700886 err = check_ptr_alignment(env, reg, off, size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700887 if (err)
888 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700889
Josef Bacik48461132016-09-28 10:54:32 -0400890 if (reg->type == PTR_TO_MAP_VALUE ||
891 reg->type == PTR_TO_MAP_VALUE_ADJ) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700892 if (t == BPF_WRITE && value_regno >= 0 &&
893 is_pointer_value(env, value_regno)) {
894 verbose("R%d leaks addr into map\n", value_regno);
895 return -EACCES;
896 }
Josef Bacik48461132016-09-28 10:54:32 -0400897
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -0800898 if (reg->type == PTR_TO_MAP_VALUE_ADJ)
899 err = check_map_access_adj(env, regno, off, size);
900 else
901 err = check_map_access(env, regno, off, size);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700902 if (!err && t == BPF_READ && value_regno >= 0)
Gianluca Borellof0318d02017-01-09 10:19:48 -0800903 mark_reg_unknown_value_and_range(state->regs,
904 value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700905
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700906 } else if (reg->type == PTR_TO_CTX) {
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700907 enum bpf_reg_type reg_type = UNKNOWN_VALUE;
908
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700909 if (t == BPF_WRITE && value_regno >= 0 &&
910 is_pointer_value(env, value_regno)) {
911 verbose("R%d leaks addr into ctx\n", value_regno);
912 return -EACCES;
913 }
Yonghong Song31fd8582017-06-13 15:52:13 -0700914 err = check_ctx_access(env, insn_idx, off, size, t, &reg_type);
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700915 if (!err && t == BPF_READ && value_regno >= 0) {
Gianluca Borellof0318d02017-01-09 10:19:48 -0800916 mark_reg_unknown_value_and_range(state->regs,
917 value_regno);
Mickaël Salaün19553512016-09-24 20:01:50 +0200918 /* note that reg.[id|off|range] == 0 */
919 state->regs[value_regno].type = reg_type;
David S. Millerd1174412017-05-10 11:22:52 -0700920 state->regs[value_regno].aux_off = 0;
921 state->regs[value_regno].aux_off_align = 0;
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700922 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700923
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700924 } else if (reg->type == FRAME_PTR || reg->type == PTR_TO_STACK) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700925 if (off >= 0 || off < -MAX_BPF_STACK) {
926 verbose("invalid stack off=%d size=%d\n", off, size);
927 return -EACCES;
928 }
Alexei Starovoitov87266792017-05-30 13:31:29 -0700929
930 if (env->prog->aux->stack_depth < -off)
931 env->prog->aux->stack_depth = -off;
932
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700933 if (t == BPF_WRITE) {
934 if (!env->allow_ptr_leaks &&
935 state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL &&
936 size != BPF_REG_SIZE) {
937 verbose("attempt to corrupt spilled pointer on stack\n");
938 return -EACCES;
939 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700940 err = check_stack_write(state, off, size, value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700941 } else {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700942 err = check_stack_read(state, off, size, value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700943 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700944 } else if (state->regs[regno].type == PTR_TO_PACKET) {
Thomas Graf3a0af8f2016-11-30 17:10:10 +0100945 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700946 verbose("cannot write into packet\n");
947 return -EACCES;
948 }
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700949 if (t == BPF_WRITE && value_regno >= 0 &&
950 is_pointer_value(env, value_regno)) {
951 verbose("R%d leaks addr into packet\n", value_regno);
952 return -EACCES;
953 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700954 err = check_packet_access(env, regno, off, size);
955 if (!err && t == BPF_READ && value_regno >= 0)
Gianluca Borellof0318d02017-01-09 10:19:48 -0800956 mark_reg_unknown_value_and_range(state->regs,
957 value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700958 } else {
959 verbose("R%d invalid mem access '%s'\n",
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700960 regno, reg_type_str[reg->type]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700961 return -EACCES;
962 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700963
964 if (!err && size <= 2 && value_regno >= 0 && env->allow_ptr_leaks &&
965 state->regs[value_regno].type == UNKNOWN_VALUE) {
966 /* 1 or 2 byte load zero-extends, determine the number of
967 * zero upper bits. Not doing it fo 4 byte load, since
968 * such values cannot be added to ptr_to_packet anyway.
969 */
970 state->regs[value_regno].imm = 64 - size * 8;
971 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700972 return err;
973}
974
Yonghong Song31fd8582017-06-13 15:52:13 -0700975static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700976{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100977 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700978 int err;
979
980 if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
981 insn->imm != 0) {
982 verbose("BPF_XADD uses reserved fields\n");
983 return -EINVAL;
984 }
985
986 /* check src1 operand */
987 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
988 if (err)
989 return err;
990
991 /* check src2 operand */
992 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
993 if (err)
994 return err;
995
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +0200996 if (is_pointer_value(env, insn->src_reg)) {
997 verbose("R%d leaks addr into mem\n", insn->src_reg);
998 return -EACCES;
999 }
1000
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001001 /* check whether atomic_add can read the memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07001002 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001003 BPF_SIZE(insn->code), BPF_READ, -1);
1004 if (err)
1005 return err;
1006
1007 /* check whether atomic_add can write into the same memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07001008 return check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001009 BPF_SIZE(insn->code), BPF_WRITE, -1);
1010}
1011
1012/* when register 'regno' is passed into function that will read 'access_size'
1013 * bytes from that pointer, make sure that it's within stack boundary
1014 * and all elements of stack are initialized
1015 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001016static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
Daniel Borkmann435faee12016-04-13 00:10:51 +02001017 int access_size, bool zero_size_allowed,
1018 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001019{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001020 struct bpf_verifier_state *state = &env->cur_state;
1021 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001022 int off, i;
1023
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001024 if (regs[regno].type != PTR_TO_STACK) {
1025 if (zero_size_allowed && access_size == 0 &&
1026 regs[regno].type == CONST_IMM &&
1027 regs[regno].imm == 0)
1028 return 0;
1029
1030 verbose("R%d type=%s expected=%s\n", regno,
1031 reg_type_str[regs[regno].type],
1032 reg_type_str[PTR_TO_STACK]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001033 return -EACCES;
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001034 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001035
1036 off = regs[regno].imm;
1037 if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
1038 access_size <= 0) {
1039 verbose("invalid stack type R%d off=%d access_size=%d\n",
1040 regno, off, access_size);
1041 return -EACCES;
1042 }
1043
Alexei Starovoitov87266792017-05-30 13:31:29 -07001044 if (env->prog->aux->stack_depth < -off)
1045 env->prog->aux->stack_depth = -off;
1046
Daniel Borkmann435faee12016-04-13 00:10:51 +02001047 if (meta && meta->raw_mode) {
1048 meta->access_size = access_size;
1049 meta->regno = regno;
1050 return 0;
1051 }
1052
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001053 for (i = 0; i < access_size; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001054 if (state->stack_slot_type[MAX_BPF_STACK + off + i] != STACK_MISC) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001055 verbose("invalid indirect read from stack off %d+%d size %d\n",
1056 off, i, access_size);
1057 return -EACCES;
1058 }
1059 }
1060 return 0;
1061}
1062
Gianluca Borello06c1c042017-01-09 10:19:49 -08001063static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
1064 int access_size, bool zero_size_allowed,
1065 struct bpf_call_arg_meta *meta)
1066{
1067 struct bpf_reg_state *regs = env->cur_state.regs;
1068
1069 switch (regs[regno].type) {
1070 case PTR_TO_PACKET:
1071 return check_packet_access(env, regno, 0, access_size);
1072 case PTR_TO_MAP_VALUE:
1073 return check_map_access(env, regno, 0, access_size);
1074 case PTR_TO_MAP_VALUE_ADJ:
1075 return check_map_access_adj(env, regno, 0, access_size);
1076 default: /* const_imm|ptr_to_stack or invalid ptr */
1077 return check_stack_boundary(env, regno, access_size,
1078 zero_size_allowed, meta);
1079 }
1080}
1081
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001082static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001083 enum bpf_arg_type arg_type,
1084 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001085{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001086 struct bpf_reg_state *regs = env->cur_state.regs, *reg = &regs[regno];
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001087 enum bpf_reg_type expected_type, type = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001088 int err = 0;
1089
Daniel Borkmann80f1d682015-03-12 17:21:42 +01001090 if (arg_type == ARG_DONTCARE)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001091 return 0;
1092
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001093 if (type == NOT_INIT) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001094 verbose("R%d !read_ok\n", regno);
1095 return -EACCES;
1096 }
1097
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001098 if (arg_type == ARG_ANYTHING) {
1099 if (is_pointer_value(env, regno)) {
1100 verbose("R%d leaks addr into helper function\n", regno);
1101 return -EACCES;
1102 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01001103 return 0;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001104 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01001105
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001106 if (type == PTR_TO_PACKET &&
1107 !may_access_direct_pkt_data(env, meta, BPF_READ)) {
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001108 verbose("helper access to the packet is not allowed\n");
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001109 return -EACCES;
1110 }
1111
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001112 if (arg_type == ARG_PTR_TO_MAP_KEY ||
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001113 arg_type == ARG_PTR_TO_MAP_VALUE) {
1114 expected_type = PTR_TO_STACK;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001115 if (type != PTR_TO_PACKET && type != expected_type)
1116 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001117 } else if (arg_type == ARG_CONST_SIZE ||
1118 arg_type == ARG_CONST_SIZE_OR_ZERO) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001119 expected_type = CONST_IMM;
Gianluca Borello06c1c042017-01-09 10:19:49 -08001120 /* One exception. Allow UNKNOWN_VALUE registers when the
1121 * boundaries are known and don't cause unsafe memory accesses
1122 */
1123 if (type != UNKNOWN_VALUE && type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001124 goto err_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001125 } else if (arg_type == ARG_CONST_MAP_PTR) {
1126 expected_type = CONST_PTR_TO_MAP;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001127 if (type != expected_type)
1128 goto err_type;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001129 } else if (arg_type == ARG_PTR_TO_CTX) {
1130 expected_type = PTR_TO_CTX;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001131 if (type != expected_type)
1132 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001133 } else if (arg_type == ARG_PTR_TO_MEM ||
1134 arg_type == ARG_PTR_TO_UNINIT_MEM) {
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001135 expected_type = PTR_TO_STACK;
1136 /* One exception here. In case function allows for NULL to be
1137 * passed in as argument, it's a CONST_IMM type. Final test
1138 * happens during stack boundary checking.
1139 */
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001140 if (type == CONST_IMM && reg->imm == 0)
1141 /* final test in check_stack_boundary() */;
Gianluca Borello57225692017-01-09 10:19:47 -08001142 else if (type != PTR_TO_PACKET && type != PTR_TO_MAP_VALUE &&
1143 type != PTR_TO_MAP_VALUE_ADJ && type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001144 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001145 meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001146 } else {
1147 verbose("unsupported arg_type %d\n", arg_type);
1148 return -EFAULT;
1149 }
1150
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001151 if (arg_type == ARG_CONST_MAP_PTR) {
1152 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001153 meta->map_ptr = reg->map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001154 } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
1155 /* bpf_map_xxx(..., map_ptr, ..., key) call:
1156 * check that [key, key + map->key_size) are within
1157 * stack limits and initialized
1158 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001159 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001160 /* in function declaration map_ptr must come before
1161 * map_key, so that it's verified and known before
1162 * we have to check map_key here. Otherwise it means
1163 * that kernel subsystem misconfigured verifier
1164 */
1165 verbose("invalid map_ptr to access map->key\n");
1166 return -EACCES;
1167 }
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001168 if (type == PTR_TO_PACKET)
1169 err = check_packet_access(env, regno, 0,
1170 meta->map_ptr->key_size);
1171 else
1172 err = check_stack_boundary(env, regno,
1173 meta->map_ptr->key_size,
1174 false, NULL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001175 } else if (arg_type == ARG_PTR_TO_MAP_VALUE) {
1176 /* bpf_map_xxx(..., map_ptr, ..., value) call:
1177 * check [value, value + map->value_size) validity
1178 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001179 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001180 /* kernel subsystem misconfigured verifier */
1181 verbose("invalid map_ptr to access map->value\n");
1182 return -EACCES;
1183 }
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001184 if (type == PTR_TO_PACKET)
1185 err = check_packet_access(env, regno, 0,
1186 meta->map_ptr->value_size);
1187 else
1188 err = check_stack_boundary(env, regno,
1189 meta->map_ptr->value_size,
1190 false, NULL);
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001191 } else if (arg_type == ARG_CONST_SIZE ||
1192 arg_type == ARG_CONST_SIZE_OR_ZERO) {
1193 bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001194
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001195 /* bpf_xxx(..., buf, len) call will access 'len' bytes
1196 * from stack pointer 'buf'. Check it
1197 * note: regno == len, regno - 1 == buf
1198 */
1199 if (regno == 0) {
1200 /* kernel subsystem misconfigured verifier */
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001201 verbose("ARG_CONST_SIZE cannot be first argument\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001202 return -EACCES;
1203 }
Gianluca Borello06c1c042017-01-09 10:19:49 -08001204
1205 /* If the register is UNKNOWN_VALUE, the access check happens
1206 * using its boundaries. Otherwise, just use its imm
1207 */
1208 if (type == UNKNOWN_VALUE) {
1209 /* For unprivileged variable accesses, disable raw
1210 * mode so that the program is required to
1211 * initialize all the memory that the helper could
1212 * just partially fill up.
1213 */
1214 meta = NULL;
1215
1216 if (reg->min_value < 0) {
1217 verbose("R%d min value is negative, either use unsigned or 'var &= const'\n",
1218 regno);
1219 return -EACCES;
1220 }
1221
1222 if (reg->min_value == 0) {
1223 err = check_helper_mem_access(env, regno - 1, 0,
1224 zero_size_allowed,
1225 meta);
1226 if (err)
1227 return err;
1228 }
1229
1230 if (reg->max_value == BPF_REGISTER_MAX_RANGE) {
1231 verbose("R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
1232 regno);
1233 return -EACCES;
1234 }
1235 err = check_helper_mem_access(env, regno - 1,
1236 reg->max_value,
1237 zero_size_allowed, meta);
1238 if (err)
1239 return err;
1240 } else {
1241 /* register is CONST_IMM */
1242 err = check_helper_mem_access(env, regno - 1, reg->imm,
1243 zero_size_allowed, meta);
1244 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001245 }
1246
1247 return err;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001248err_type:
1249 verbose("R%d type=%s expected=%s\n", regno,
1250 reg_type_str[type], reg_type_str[expected_type]);
1251 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001252}
1253
Kaixu Xia35578d72015-08-06 07:02:35 +00001254static int check_map_func_compatibility(struct bpf_map *map, int func_id)
1255{
Kaixu Xia35578d72015-08-06 07:02:35 +00001256 if (!map)
1257 return 0;
1258
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001259 /* We need a two way check, first is from map perspective ... */
1260 switch (map->map_type) {
1261 case BPF_MAP_TYPE_PROG_ARRAY:
1262 if (func_id != BPF_FUNC_tail_call)
1263 goto error;
1264 break;
1265 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
1266 if (func_id != BPF_FUNC_perf_event_read &&
1267 func_id != BPF_FUNC_perf_event_output)
1268 goto error;
1269 break;
1270 case BPF_MAP_TYPE_STACK_TRACE:
1271 if (func_id != BPF_FUNC_get_stackid)
1272 goto error;
1273 break;
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -07001274 case BPF_MAP_TYPE_CGROUP_ARRAY:
David S. Miller60747ef2016-08-18 01:17:32 -04001275 if (func_id != BPF_FUNC_skb_under_cgroup &&
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001276 func_id != BPF_FUNC_current_task_under_cgroup)
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07001277 goto error;
1278 break;
John Fastabend546ac1f2017-07-17 09:28:56 -07001279 /* devmap returns a pointer to a live net_device ifindex that we cannot
1280 * allow to be modified from bpf side. So do not allow lookup elements
1281 * for now.
1282 */
1283 case BPF_MAP_TYPE_DEVMAP:
John Fastabend2ddf71e2017-07-17 09:30:02 -07001284 if (func_id != BPF_FUNC_redirect_map)
John Fastabend546ac1f2017-07-17 09:28:56 -07001285 goto error;
1286 break;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07001287 case BPF_MAP_TYPE_ARRAY_OF_MAPS:
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001288 case BPF_MAP_TYPE_HASH_OF_MAPS:
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07001289 if (func_id != BPF_FUNC_map_lookup_elem)
1290 goto error;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001291 default:
1292 break;
1293 }
1294
1295 /* ... and second from the function itself. */
1296 switch (func_id) {
1297 case BPF_FUNC_tail_call:
1298 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1299 goto error;
1300 break;
1301 case BPF_FUNC_perf_event_read:
1302 case BPF_FUNC_perf_event_output:
1303 if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
1304 goto error;
1305 break;
1306 case BPF_FUNC_get_stackid:
1307 if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
1308 goto error;
1309 break;
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001310 case BPF_FUNC_current_task_under_cgroup:
Daniel Borkmann747ea552016-08-12 22:17:17 +02001311 case BPF_FUNC_skb_under_cgroup:
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07001312 if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
1313 goto error;
1314 break;
John Fastabend97f91a72017-07-17 09:29:18 -07001315 case BPF_FUNC_redirect_map:
1316 if (map->map_type != BPF_MAP_TYPE_DEVMAP)
1317 goto error;
1318 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001319 default:
1320 break;
Kaixu Xia35578d72015-08-06 07:02:35 +00001321 }
1322
1323 return 0;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001324error:
Thomas Grafebb676d2016-10-27 11:23:51 +02001325 verbose("cannot pass map_type %d into func %s#%d\n",
1326 map->map_type, func_id_name(func_id), func_id);
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001327 return -EINVAL;
Kaixu Xia35578d72015-08-06 07:02:35 +00001328}
1329
Daniel Borkmann435faee12016-04-13 00:10:51 +02001330static int check_raw_mode(const struct bpf_func_proto *fn)
1331{
1332 int count = 0;
1333
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001334 if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02001335 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001336 if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02001337 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001338 if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02001339 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001340 if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02001341 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001342 if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02001343 count++;
1344
1345 return count > 1 ? -EINVAL : 0;
1346}
1347
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001348static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001349{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001350 struct bpf_verifier_state *state = &env->cur_state;
1351 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001352 int i;
1353
1354 for (i = 0; i < MAX_BPF_REG; i++)
1355 if (regs[i].type == PTR_TO_PACKET ||
1356 regs[i].type == PTR_TO_PACKET_END)
1357 mark_reg_unknown_value(regs, i);
1358
1359 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
1360 if (state->stack_slot_type[i] != STACK_SPILL)
1361 continue;
1362 reg = &state->spilled_regs[i / BPF_REG_SIZE];
1363 if (reg->type != PTR_TO_PACKET &&
1364 reg->type != PTR_TO_PACKET_END)
1365 continue;
Daniel Borkmann36e24c02017-06-11 00:50:43 +02001366 __mark_reg_unknown_value(state->spilled_regs,
1367 i / BPF_REG_SIZE);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001368 }
1369}
1370
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07001371static int check_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001372{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001373 struct bpf_verifier_state *state = &env->cur_state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001374 const struct bpf_func_proto *fn = NULL;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001375 struct bpf_reg_state *regs = state->regs;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001376 struct bpf_call_arg_meta meta;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001377 bool changes_data;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001378 int i, err;
1379
1380 /* find function prototype */
1381 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
Thomas Grafebb676d2016-10-27 11:23:51 +02001382 verbose("invalid func %s#%d\n", func_id_name(func_id), func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001383 return -EINVAL;
1384 }
1385
1386 if (env->prog->aux->ops->get_func_proto)
1387 fn = env->prog->aux->ops->get_func_proto(func_id);
1388
1389 if (!fn) {
Thomas Grafebb676d2016-10-27 11:23:51 +02001390 verbose("unknown func %s#%d\n", func_id_name(func_id), func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001391 return -EINVAL;
1392 }
1393
1394 /* eBPF programs must be GPL compatible to use GPL-ed functions */
Daniel Borkmann24701ec2015-03-01 12:31:47 +01001395 if (!env->prog->gpl_compatible && fn->gpl_only) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001396 verbose("cannot call GPL only function from proprietary program\n");
1397 return -EINVAL;
1398 }
1399
Martin KaFai Lau17bedab2016-12-07 15:53:11 -08001400 changes_data = bpf_helper_changes_pkt_data(fn->func);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001401
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001402 memset(&meta, 0, sizeof(meta));
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001403 meta.pkt_access = fn->pkt_access;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001404
Daniel Borkmann435faee12016-04-13 00:10:51 +02001405 /* We only support one arg being in raw mode at the moment, which
1406 * is sufficient for the helper functions we have right now.
1407 */
1408 err = check_raw_mode(fn);
1409 if (err) {
Thomas Grafebb676d2016-10-27 11:23:51 +02001410 verbose("kernel subsystem misconfigured func %s#%d\n",
1411 func_id_name(func_id), func_id);
Daniel Borkmann435faee12016-04-13 00:10:51 +02001412 return err;
1413 }
1414
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001415 /* check args */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001416 err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001417 if (err)
1418 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001419 err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001420 if (err)
1421 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001422 err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001423 if (err)
1424 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001425 err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001426 if (err)
1427 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001428 err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001429 if (err)
1430 return err;
1431
Daniel Borkmann435faee12016-04-13 00:10:51 +02001432 /* Mark slots with STACK_MISC in case of raw mode, stack offset
1433 * is inferred from register state.
1434 */
1435 for (i = 0; i < meta.access_size; i++) {
Yonghong Song31fd8582017-06-13 15:52:13 -07001436 err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, BPF_WRITE, -1);
Daniel Borkmann435faee12016-04-13 00:10:51 +02001437 if (err)
1438 return err;
1439 }
1440
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001441 /* reset caller saved regs */
Daniel Borkmanna9789ef2017-05-25 01:05:06 +02001442 for (i = 0; i < CALLER_SAVED_REGS; i++)
1443 mark_reg_not_init(regs, caller_saved[i]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001444
1445 /* update return register */
1446 if (fn->ret_type == RET_INTEGER) {
1447 regs[BPF_REG_0].type = UNKNOWN_VALUE;
1448 } else if (fn->ret_type == RET_VOID) {
1449 regs[BPF_REG_0].type = NOT_INIT;
1450 } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) {
Martin KaFai Laufad73a12017-03-22 10:00:32 -07001451 struct bpf_insn_aux_data *insn_aux;
1452
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001453 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
Josef Bacik48461132016-09-28 10:54:32 -04001454 regs[BPF_REG_0].max_value = regs[BPF_REG_0].min_value = 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001455 /* remember map_ptr, so that check_map_access()
1456 * can check 'value_size' boundary of memory access
1457 * to map element returned from bpf_map_lookup_elem()
1458 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001459 if (meta.map_ptr == NULL) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001460 verbose("kernel subsystem misconfigured verifier\n");
1461 return -EINVAL;
1462 }
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001463 regs[BPF_REG_0].map_ptr = meta.map_ptr;
Thomas Graf57a09bf2016-10-18 19:51:19 +02001464 regs[BPF_REG_0].id = ++env->id_gen;
Martin KaFai Laufad73a12017-03-22 10:00:32 -07001465 insn_aux = &env->insn_aux_data[insn_idx];
1466 if (!insn_aux->map_ptr)
1467 insn_aux->map_ptr = meta.map_ptr;
1468 else if (insn_aux->map_ptr != meta.map_ptr)
1469 insn_aux->map_ptr = BPF_MAP_PTR_POISON;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001470 } else {
Thomas Grafebb676d2016-10-27 11:23:51 +02001471 verbose("unknown return type %d of func %s#%d\n",
1472 fn->ret_type, func_id_name(func_id), func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001473 return -EINVAL;
1474 }
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001475
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001476 err = check_map_func_compatibility(meta.map_ptr, func_id);
Kaixu Xia35578d72015-08-06 07:02:35 +00001477 if (err)
1478 return err;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001479
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001480 if (changes_data)
1481 clear_all_pkt_pointers(env);
1482 return 0;
1483}
1484
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001485static int check_packet_ptr_add(struct bpf_verifier_env *env,
1486 struct bpf_insn *insn)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001487{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001488 struct bpf_reg_state *regs = env->cur_state.regs;
1489 struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
1490 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
1491 struct bpf_reg_state tmp_reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001492 s32 imm;
1493
1494 if (BPF_SRC(insn->code) == BPF_K) {
1495 /* pkt_ptr += imm */
1496 imm = insn->imm;
1497
1498add_imm:
William Tu63dfef72017-02-04 08:37:29 -08001499 if (imm < 0) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001500 verbose("addition of negative constant to packet pointer is not allowed\n");
1501 return -EACCES;
1502 }
1503 if (imm >= MAX_PACKET_OFF ||
1504 imm + dst_reg->off >= MAX_PACKET_OFF) {
1505 verbose("constant %d is too large to add to packet pointer\n",
1506 imm);
1507 return -EACCES;
1508 }
1509 /* a constant was added to pkt_ptr.
1510 * Remember it while keeping the same 'id'
1511 */
1512 dst_reg->off += imm;
1513 } else {
David S. Millerd1174412017-05-10 11:22:52 -07001514 bool had_id;
1515
Alexei Starovoitov1b9b69e2016-05-19 18:17:14 -07001516 if (src_reg->type == PTR_TO_PACKET) {
1517 /* R6=pkt(id=0,off=0,r=62) R7=imm22; r7 += r6 */
1518 tmp_reg = *dst_reg; /* save r7 state */
1519 *dst_reg = *src_reg; /* copy pkt_ptr state r6 into r7 */
1520 src_reg = &tmp_reg; /* pretend it's src_reg state */
1521 /* if the checks below reject it, the copy won't matter,
1522 * since we're rejecting the whole program. If all ok,
1523 * then imm22 state will be added to r7
1524 * and r7 will be pkt(id=0,off=22,r=62) while
1525 * r6 will stay as pkt(id=0,off=0,r=62)
1526 */
1527 }
1528
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001529 if (src_reg->type == CONST_IMM) {
1530 /* pkt_ptr += reg where reg is known constant */
1531 imm = src_reg->imm;
1532 goto add_imm;
1533 }
1534 /* disallow pkt_ptr += reg
1535 * if reg is not uknown_value with guaranteed zero upper bits
1536 * otherwise pkt_ptr may overflow and addition will become
1537 * subtraction which is not allowed
1538 */
1539 if (src_reg->type != UNKNOWN_VALUE) {
1540 verbose("cannot add '%s' to ptr_to_packet\n",
1541 reg_type_str[src_reg->type]);
1542 return -EACCES;
1543 }
1544 if (src_reg->imm < 48) {
1545 verbose("cannot add integer value with %lld upper zero bits to ptr_to_packet\n",
1546 src_reg->imm);
1547 return -EACCES;
1548 }
David S. Millerd1174412017-05-10 11:22:52 -07001549
1550 had_id = (dst_reg->id != 0);
1551
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001552 /* dst_reg stays as pkt_ptr type and since some positive
1553 * integer value was added to the pointer, increment its 'id'
1554 */
Jakub Kicinski1f415a72016-08-02 16:12:14 +01001555 dst_reg->id = ++env->id_gen;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001556
David S. Millerd1174412017-05-10 11:22:52 -07001557 /* something was added to pkt_ptr, set range to zero */
David S. Miller6832a332017-05-11 19:30:02 -07001558 dst_reg->aux_off += dst_reg->off;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001559 dst_reg->off = 0;
1560 dst_reg->range = 0;
David S. Millerd1174412017-05-10 11:22:52 -07001561 if (had_id)
1562 dst_reg->aux_off_align = min(dst_reg->aux_off_align,
1563 src_reg->min_align);
1564 else
1565 dst_reg->aux_off_align = src_reg->min_align;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001566 }
1567 return 0;
1568}
1569
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001570static int evaluate_reg_alu(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001571{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001572 struct bpf_reg_state *regs = env->cur_state.regs;
1573 struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001574 u8 opcode = BPF_OP(insn->code);
1575 s64 imm_log2;
1576
1577 /* for type == UNKNOWN_VALUE:
1578 * imm > 0 -> number of zero upper bits
1579 * imm == 0 -> don't track which is the same as all bits can be non-zero
1580 */
1581
1582 if (BPF_SRC(insn->code) == BPF_X) {
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001583 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001584
1585 if (src_reg->type == UNKNOWN_VALUE && src_reg->imm > 0 &&
1586 dst_reg->imm && opcode == BPF_ADD) {
1587 /* dreg += sreg
1588 * where both have zero upper bits. Adding them
1589 * can only result making one more bit non-zero
1590 * in the larger value.
1591 * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47)
1592 * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47)
1593 */
1594 dst_reg->imm = min(dst_reg->imm, src_reg->imm);
1595 dst_reg->imm--;
1596 return 0;
1597 }
1598 if (src_reg->type == CONST_IMM && src_reg->imm > 0 &&
1599 dst_reg->imm && opcode == BPF_ADD) {
1600 /* dreg += sreg
1601 * where dreg has zero upper bits and sreg is const.
1602 * Adding them can only result making one more bit
1603 * non-zero in the larger value.
1604 */
1605 imm_log2 = __ilog2_u64((long long)src_reg->imm);
1606 dst_reg->imm = min(dst_reg->imm, 63 - imm_log2);
1607 dst_reg->imm--;
1608 return 0;
1609 }
1610 /* all other cases non supported yet, just mark dst_reg */
1611 dst_reg->imm = 0;
1612 return 0;
1613 }
1614
1615 /* sign extend 32-bit imm into 64-bit to make sure that
1616 * negative values occupy bit 63. Note ilog2() would have
1617 * been incorrect, since sizeof(insn->imm) == 4
1618 */
1619 imm_log2 = __ilog2_u64((long long)insn->imm);
1620
1621 if (dst_reg->imm && opcode == BPF_LSH) {
1622 /* reg <<= imm
1623 * if reg was a result of 2 byte load, then its imm == 48
1624 * which means that upper 48 bits are zero and shifting this reg
1625 * left by 4 would mean that upper 44 bits are still zero
1626 */
1627 dst_reg->imm -= insn->imm;
1628 } else if (dst_reg->imm && opcode == BPF_MUL) {
1629 /* reg *= imm
1630 * if multiplying by 14 subtract 4
1631 * This is conservative calculation of upper zero bits.
1632 * It's not trying to special case insn->imm == 1 or 0 cases
1633 */
1634 dst_reg->imm -= imm_log2 + 1;
1635 } else if (opcode == BPF_AND) {
1636 /* reg &= imm */
1637 dst_reg->imm = 63 - imm_log2;
1638 } else if (dst_reg->imm && opcode == BPF_ADD) {
1639 /* reg += imm */
1640 dst_reg->imm = min(dst_reg->imm, 63 - imm_log2);
1641 dst_reg->imm--;
1642 } else if (opcode == BPF_RSH) {
1643 /* reg >>= imm
1644 * which means that after right shift, upper bits will be zero
1645 * note that verifier already checked that
1646 * 0 <= imm < 64 for shift insn
1647 */
1648 dst_reg->imm += insn->imm;
1649 if (unlikely(dst_reg->imm > 64))
1650 /* some dumb code did:
1651 * r2 = *(u32 *)mem;
1652 * r2 >>= 32;
1653 * and all bits are zero now */
1654 dst_reg->imm = 64;
1655 } else {
1656 /* all other alu ops, means that we don't know what will
1657 * happen to the value, mark it with unknown number of zero bits
1658 */
1659 dst_reg->imm = 0;
1660 }
1661
1662 if (dst_reg->imm < 0) {
1663 /* all 64 bits of the register can contain non-zero bits
1664 * and such value cannot be added to ptr_to_packet, since it
1665 * may overflow, mark it as unknown to avoid further eval
1666 */
1667 dst_reg->imm = 0;
1668 }
1669 return 0;
1670}
1671
John Fastabend43188702017-07-02 02:13:30 +02001672static int evaluate_reg_imm_alu_unknown(struct bpf_verifier_env *env,
1673 struct bpf_insn *insn)
1674{
1675 struct bpf_reg_state *regs = env->cur_state.regs;
1676 struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
1677 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
1678 u8 opcode = BPF_OP(insn->code);
1679 s64 imm_log2 = __ilog2_u64((long long)dst_reg->imm);
1680
1681 /* BPF_X code with src_reg->type UNKNOWN_VALUE here. */
1682 if (src_reg->imm > 0 && dst_reg->imm) {
1683 switch (opcode) {
1684 case BPF_ADD:
1685 /* dreg += sreg
1686 * where both have zero upper bits. Adding them
1687 * can only result making one more bit non-zero
1688 * in the larger value.
1689 * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47)
1690 * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47)
1691 */
1692 dst_reg->imm = min(src_reg->imm, 63 - imm_log2);
1693 dst_reg->imm--;
1694 break;
1695 case BPF_AND:
1696 /* dreg &= sreg
1697 * AND can not extend zero bits only shrink
1698 * Ex. 0x00..00ffffff
1699 * & 0x0f..ffffffff
1700 * ----------------
1701 * 0x00..00ffffff
1702 */
1703 dst_reg->imm = max(src_reg->imm, 63 - imm_log2);
1704 break;
1705 case BPF_OR:
1706 /* dreg |= sreg
1707 * OR can only extend zero bits
1708 * Ex. 0x00..00ffffff
1709 * | 0x0f..ffffffff
1710 * ----------------
1711 * 0x0f..00ffffff
1712 */
1713 dst_reg->imm = min(src_reg->imm, 63 - imm_log2);
1714 break;
1715 case BPF_SUB:
1716 case BPF_MUL:
1717 case BPF_RSH:
1718 case BPF_LSH:
1719 /* These may be flushed out later */
1720 default:
1721 mark_reg_unknown_value(regs, insn->dst_reg);
1722 }
1723 } else {
1724 mark_reg_unknown_value(regs, insn->dst_reg);
1725 }
1726
1727 dst_reg->type = UNKNOWN_VALUE;
1728 return 0;
1729}
1730
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001731static int evaluate_reg_imm_alu(struct bpf_verifier_env *env,
1732 struct bpf_insn *insn)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001733{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001734 struct bpf_reg_state *regs = env->cur_state.regs;
1735 struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
1736 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001737 u8 opcode = BPF_OP(insn->code);
Daniel Borkmann3fadc802017-01-24 01:06:30 +01001738 u64 dst_imm = dst_reg->imm;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001739
John Fastabend43188702017-07-02 02:13:30 +02001740 if (BPF_SRC(insn->code) == BPF_X && src_reg->type == UNKNOWN_VALUE)
1741 return evaluate_reg_imm_alu_unknown(env, insn);
1742
Daniel Borkmann3fadc802017-01-24 01:06:30 +01001743 /* dst_reg->type == CONST_IMM here. Simulate execution of insns
1744 * containing ALU ops. Don't care about overflow or negative
1745 * values, just add/sub/... them; registers are in u64.
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001746 */
Daniel Borkmann3fadc802017-01-24 01:06:30 +01001747 if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_K) {
1748 dst_imm += insn->imm;
1749 } else if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_X &&
1750 src_reg->type == CONST_IMM) {
1751 dst_imm += src_reg->imm;
1752 } else if (opcode == BPF_SUB && BPF_SRC(insn->code) == BPF_K) {
1753 dst_imm -= insn->imm;
1754 } else if (opcode == BPF_SUB && BPF_SRC(insn->code) == BPF_X &&
1755 src_reg->type == CONST_IMM) {
1756 dst_imm -= src_reg->imm;
1757 } else if (opcode == BPF_MUL && BPF_SRC(insn->code) == BPF_K) {
1758 dst_imm *= insn->imm;
1759 } else if (opcode == BPF_MUL && BPF_SRC(insn->code) == BPF_X &&
1760 src_reg->type == CONST_IMM) {
1761 dst_imm *= src_reg->imm;
1762 } else if (opcode == BPF_OR && BPF_SRC(insn->code) == BPF_K) {
1763 dst_imm |= insn->imm;
1764 } else if (opcode == BPF_OR && BPF_SRC(insn->code) == BPF_X &&
1765 src_reg->type == CONST_IMM) {
1766 dst_imm |= src_reg->imm;
1767 } else if (opcode == BPF_AND && BPF_SRC(insn->code) == BPF_K) {
1768 dst_imm &= insn->imm;
1769 } else if (opcode == BPF_AND && BPF_SRC(insn->code) == BPF_X &&
1770 src_reg->type == CONST_IMM) {
1771 dst_imm &= src_reg->imm;
1772 } else if (opcode == BPF_RSH && BPF_SRC(insn->code) == BPF_K) {
1773 dst_imm >>= insn->imm;
1774 } else if (opcode == BPF_RSH && BPF_SRC(insn->code) == BPF_X &&
1775 src_reg->type == CONST_IMM) {
1776 dst_imm >>= src_reg->imm;
1777 } else if (opcode == BPF_LSH && BPF_SRC(insn->code) == BPF_K) {
1778 dst_imm <<= insn->imm;
1779 } else if (opcode == BPF_LSH && BPF_SRC(insn->code) == BPF_X &&
1780 src_reg->type == CONST_IMM) {
1781 dst_imm <<= src_reg->imm;
1782 } else {
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001783 mark_reg_unknown_value(regs, insn->dst_reg);
Daniel Borkmann3fadc802017-01-24 01:06:30 +01001784 goto out;
1785 }
1786
1787 dst_reg->imm = dst_imm;
1788out:
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001789 return 0;
1790}
1791
Josef Bacik48461132016-09-28 10:54:32 -04001792static void check_reg_overflow(struct bpf_reg_state *reg)
1793{
1794 if (reg->max_value > BPF_REGISTER_MAX_RANGE)
1795 reg->max_value = BPF_REGISTER_MAX_RANGE;
Josef Bacikf23cc642016-11-14 15:45:36 -05001796 if (reg->min_value < BPF_REGISTER_MIN_RANGE ||
1797 reg->min_value > BPF_REGISTER_MAX_RANGE)
Josef Bacik48461132016-09-28 10:54:32 -04001798 reg->min_value = BPF_REGISTER_MIN_RANGE;
1799}
1800
David S. Millerd1174412017-05-10 11:22:52 -07001801static u32 calc_align(u32 imm)
1802{
1803 if (!imm)
1804 return 1U << 31;
1805 return imm - ((imm - 1) & imm);
1806}
1807
Josef Bacik48461132016-09-28 10:54:32 -04001808static void adjust_reg_min_max_vals(struct bpf_verifier_env *env,
1809 struct bpf_insn *insn)
1810{
1811 struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg;
Josef Bacikf23cc642016-11-14 15:45:36 -05001812 s64 min_val = BPF_REGISTER_MIN_RANGE;
1813 u64 max_val = BPF_REGISTER_MAX_RANGE;
Josef Bacik48461132016-09-28 10:54:32 -04001814 u8 opcode = BPF_OP(insn->code);
David S. Millerd1174412017-05-10 11:22:52 -07001815 u32 dst_align, src_align;
Josef Bacik48461132016-09-28 10:54:32 -04001816
1817 dst_reg = &regs[insn->dst_reg];
David S. Millerd1174412017-05-10 11:22:52 -07001818 src_align = 0;
Josef Bacik48461132016-09-28 10:54:32 -04001819 if (BPF_SRC(insn->code) == BPF_X) {
1820 check_reg_overflow(&regs[insn->src_reg]);
1821 min_val = regs[insn->src_reg].min_value;
1822 max_val = regs[insn->src_reg].max_value;
1823
1824 /* If the source register is a random pointer then the
1825 * min_value/max_value values represent the range of the known
1826 * accesses into that value, not the actual min/max value of the
1827 * register itself. In this case we have to reset the reg range
1828 * values so we know it is not safe to look at.
1829 */
1830 if (regs[insn->src_reg].type != CONST_IMM &&
1831 regs[insn->src_reg].type != UNKNOWN_VALUE) {
1832 min_val = BPF_REGISTER_MIN_RANGE;
1833 max_val = BPF_REGISTER_MAX_RANGE;
David S. Millerd1174412017-05-10 11:22:52 -07001834 src_align = 0;
1835 } else {
1836 src_align = regs[insn->src_reg].min_align;
Josef Bacik48461132016-09-28 10:54:32 -04001837 }
1838 } else if (insn->imm < BPF_REGISTER_MAX_RANGE &&
1839 (s64)insn->imm > BPF_REGISTER_MIN_RANGE) {
1840 min_val = max_val = insn->imm;
David S. Millerd1174412017-05-10 11:22:52 -07001841 src_align = calc_align(insn->imm);
Josef Bacik48461132016-09-28 10:54:32 -04001842 }
1843
David S. Millerd1174412017-05-10 11:22:52 -07001844 dst_align = dst_reg->min_align;
1845
Josef Bacik48461132016-09-28 10:54:32 -04001846 /* We don't know anything about what was done to this register, mark it
1847 * as unknown.
1848 */
1849 if (min_val == BPF_REGISTER_MIN_RANGE &&
1850 max_val == BPF_REGISTER_MAX_RANGE) {
1851 reset_reg_range_values(regs, insn->dst_reg);
1852 return;
1853 }
1854
Josef Bacikf23cc642016-11-14 15:45:36 -05001855 /* If one of our values was at the end of our ranges then we can't just
1856 * do our normal operations to the register, we need to set the values
1857 * to the min/max since they are undefined.
1858 */
1859 if (min_val == BPF_REGISTER_MIN_RANGE)
1860 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
1861 if (max_val == BPF_REGISTER_MAX_RANGE)
1862 dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
1863
Josef Bacik48461132016-09-28 10:54:32 -04001864 switch (opcode) {
1865 case BPF_ADD:
Josef Bacikf23cc642016-11-14 15:45:36 -05001866 if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
1867 dst_reg->min_value += min_val;
1868 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1869 dst_reg->max_value += max_val;
David S. Millerd1174412017-05-10 11:22:52 -07001870 dst_reg->min_align = min(src_align, dst_align);
Josef Bacik48461132016-09-28 10:54:32 -04001871 break;
1872 case BPF_SUB:
Josef Bacikf23cc642016-11-14 15:45:36 -05001873 if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
1874 dst_reg->min_value -= min_val;
1875 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1876 dst_reg->max_value -= max_val;
David S. Millerd1174412017-05-10 11:22:52 -07001877 dst_reg->min_align = min(src_align, dst_align);
Josef Bacik48461132016-09-28 10:54:32 -04001878 break;
1879 case BPF_MUL:
Josef Bacikf23cc642016-11-14 15:45:36 -05001880 if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
1881 dst_reg->min_value *= min_val;
1882 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1883 dst_reg->max_value *= max_val;
David S. Millerd1174412017-05-10 11:22:52 -07001884 dst_reg->min_align = max(src_align, dst_align);
Josef Bacik48461132016-09-28 10:54:32 -04001885 break;
1886 case BPF_AND:
Josef Bacikf23cc642016-11-14 15:45:36 -05001887 /* Disallow AND'ing of negative numbers, ain't nobody got time
1888 * for that. Otherwise the minimum is 0 and the max is the max
1889 * value we could AND against.
1890 */
1891 if (min_val < 0)
1892 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
1893 else
1894 dst_reg->min_value = 0;
Josef Bacik48461132016-09-28 10:54:32 -04001895 dst_reg->max_value = max_val;
David S. Millerd1174412017-05-10 11:22:52 -07001896 dst_reg->min_align = max(src_align, dst_align);
Josef Bacik48461132016-09-28 10:54:32 -04001897 break;
1898 case BPF_LSH:
1899 /* Gotta have special overflow logic here, if we're shifting
1900 * more than MAX_RANGE then just assume we have an invalid
1901 * range.
1902 */
David S. Millerd1174412017-05-10 11:22:52 -07001903 if (min_val > ilog2(BPF_REGISTER_MAX_RANGE)) {
Josef Bacik48461132016-09-28 10:54:32 -04001904 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
David S. Millerd1174412017-05-10 11:22:52 -07001905 dst_reg->min_align = 1;
1906 } else {
1907 if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
1908 dst_reg->min_value <<= min_val;
1909 if (!dst_reg->min_align)
1910 dst_reg->min_align = 1;
1911 dst_reg->min_align <<= min_val;
1912 }
Josef Bacik48461132016-09-28 10:54:32 -04001913 if (max_val > ilog2(BPF_REGISTER_MAX_RANGE))
1914 dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
Josef Bacikf23cc642016-11-14 15:45:36 -05001915 else if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
Josef Bacik48461132016-09-28 10:54:32 -04001916 dst_reg->max_value <<= max_val;
1917 break;
1918 case BPF_RSH:
Josef Bacikf23cc642016-11-14 15:45:36 -05001919 /* RSH by a negative number is undefined, and the BPF_RSH is an
1920 * unsigned shift, so make the appropriate casts.
Josef Bacik48461132016-09-28 10:54:32 -04001921 */
David S. Millerd1174412017-05-10 11:22:52 -07001922 if (min_val < 0 || dst_reg->min_value < 0) {
Josef Bacikf23cc642016-11-14 15:45:36 -05001923 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
David S. Millerd1174412017-05-10 11:22:52 -07001924 } else {
Josef Bacikf23cc642016-11-14 15:45:36 -05001925 dst_reg->min_value =
1926 (u64)(dst_reg->min_value) >> min_val;
David S. Millerd1174412017-05-10 11:22:52 -07001927 }
1928 if (min_val < 0) {
1929 dst_reg->min_align = 1;
1930 } else {
1931 dst_reg->min_align >>= (u64) min_val;
1932 if (!dst_reg->min_align)
1933 dst_reg->min_align = 1;
1934 }
Josef Bacikf23cc642016-11-14 15:45:36 -05001935 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1936 dst_reg->max_value >>= max_val;
Josef Bacik48461132016-09-28 10:54:32 -04001937 break;
1938 default:
1939 reset_reg_range_values(regs, insn->dst_reg);
1940 break;
1941 }
1942
1943 check_reg_overflow(dst_reg);
1944}
1945
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001946/* check validity of 32-bit and 64-bit arithmetic operations */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001947static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001948{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001949 struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001950 u8 opcode = BPF_OP(insn->code);
1951 int err;
1952
1953 if (opcode == BPF_END || opcode == BPF_NEG) {
1954 if (opcode == BPF_NEG) {
1955 if (BPF_SRC(insn->code) != 0 ||
1956 insn->src_reg != BPF_REG_0 ||
1957 insn->off != 0 || insn->imm != 0) {
1958 verbose("BPF_NEG uses reserved fields\n");
1959 return -EINVAL;
1960 }
1961 } else {
1962 if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
1963 (insn->imm != 16 && insn->imm != 32 && insn->imm != 64)) {
1964 verbose("BPF_END uses reserved fields\n");
1965 return -EINVAL;
1966 }
1967 }
1968
1969 /* check src operand */
1970 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
1971 if (err)
1972 return err;
1973
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001974 if (is_pointer_value(env, insn->dst_reg)) {
1975 verbose("R%d pointer arithmetic prohibited\n",
1976 insn->dst_reg);
1977 return -EACCES;
1978 }
1979
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001980 /* check dest operand */
1981 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
1982 if (err)
1983 return err;
1984
1985 } else if (opcode == BPF_MOV) {
1986
1987 if (BPF_SRC(insn->code) == BPF_X) {
1988 if (insn->imm != 0 || insn->off != 0) {
1989 verbose("BPF_MOV uses reserved fields\n");
1990 return -EINVAL;
1991 }
1992
1993 /* check src operand */
1994 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1995 if (err)
1996 return err;
1997 } else {
1998 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
1999 verbose("BPF_MOV uses reserved fields\n");
2000 return -EINVAL;
2001 }
2002 }
2003
2004 /* check dest operand */
2005 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
2006 if (err)
2007 return err;
2008
Josef Bacik48461132016-09-28 10:54:32 -04002009 /* we are setting our register to something new, we need to
2010 * reset its range values.
2011 */
2012 reset_reg_range_values(regs, insn->dst_reg);
2013
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002014 if (BPF_SRC(insn->code) == BPF_X) {
2015 if (BPF_CLASS(insn->code) == BPF_ALU64) {
2016 /* case: R1 = R2
2017 * copy register state to dest reg
2018 */
2019 regs[insn->dst_reg] = regs[insn->src_reg];
2020 } else {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002021 if (is_pointer_value(env, insn->src_reg)) {
2022 verbose("R%d partial copy of pointer\n",
2023 insn->src_reg);
2024 return -EACCES;
2025 }
Thomas Graf57a09bf2016-10-18 19:51:19 +02002026 mark_reg_unknown_value(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002027 }
2028 } else {
2029 /* case: R = imm
2030 * remember the value we stored into this reg
2031 */
2032 regs[insn->dst_reg].type = CONST_IMM;
2033 regs[insn->dst_reg].imm = insn->imm;
Daniel Borkmann4a2ff552017-06-11 00:50:42 +02002034 regs[insn->dst_reg].id = 0;
Josef Bacik48461132016-09-28 10:54:32 -04002035 regs[insn->dst_reg].max_value = insn->imm;
2036 regs[insn->dst_reg].min_value = insn->imm;
David S. Millerd1174412017-05-10 11:22:52 -07002037 regs[insn->dst_reg].min_align = calc_align(insn->imm);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002038 }
2039
2040 } else if (opcode > BPF_END) {
2041 verbose("invalid BPF_ALU opcode %x\n", opcode);
2042 return -EINVAL;
2043
2044 } else { /* all other ALU ops: and, sub, xor, add, ... */
2045
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002046 if (BPF_SRC(insn->code) == BPF_X) {
2047 if (insn->imm != 0 || insn->off != 0) {
2048 verbose("BPF_ALU uses reserved fields\n");
2049 return -EINVAL;
2050 }
2051 /* check src1 operand */
2052 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2053 if (err)
2054 return err;
2055 } else {
2056 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
2057 verbose("BPF_ALU uses reserved fields\n");
2058 return -EINVAL;
2059 }
2060 }
2061
2062 /* check src2 operand */
2063 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
2064 if (err)
2065 return err;
2066
2067 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
2068 BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
2069 verbose("div by zero\n");
2070 return -EINVAL;
2071 }
2072
Rabin Vincent229394e82016-01-12 20:17:08 +01002073 if ((opcode == BPF_LSH || opcode == BPF_RSH ||
2074 opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
2075 int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
2076
2077 if (insn->imm < 0 || insn->imm >= size) {
2078 verbose("invalid shift %d\n", insn->imm);
2079 return -EINVAL;
2080 }
2081 }
2082
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002083 /* check dest operand */
2084 err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
2085 if (err)
2086 return err;
2087
2088 dst_reg = &regs[insn->dst_reg];
2089
Josef Bacik48461132016-09-28 10:54:32 -04002090 /* first we want to adjust our ranges. */
2091 adjust_reg_min_max_vals(env, insn);
2092
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002093 /* pattern match 'bpf_add Rx, imm' instruction */
2094 if (opcode == BPF_ADD && BPF_CLASS(insn->code) == BPF_ALU64 &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002095 dst_reg->type == FRAME_PTR && BPF_SRC(insn->code) == BPF_K) {
2096 dst_reg->type = PTR_TO_STACK;
2097 dst_reg->imm = insn->imm;
2098 return 0;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002099 } else if (opcode == BPF_ADD &&
2100 BPF_CLASS(insn->code) == BPF_ALU64 &&
Yonghong Song332270f2017-04-29 22:52:42 -07002101 dst_reg->type == PTR_TO_STACK &&
2102 ((BPF_SRC(insn->code) == BPF_X &&
2103 regs[insn->src_reg].type == CONST_IMM) ||
2104 BPF_SRC(insn->code) == BPF_K)) {
2105 if (BPF_SRC(insn->code) == BPF_X)
2106 dst_reg->imm += regs[insn->src_reg].imm;
2107 else
2108 dst_reg->imm += insn->imm;
2109 return 0;
2110 } else if (opcode == BPF_ADD &&
2111 BPF_CLASS(insn->code) == BPF_ALU64 &&
Alexei Starovoitov1b9b69e2016-05-19 18:17:14 -07002112 (dst_reg->type == PTR_TO_PACKET ||
2113 (BPF_SRC(insn->code) == BPF_X &&
2114 regs[insn->src_reg].type == PTR_TO_PACKET))) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002115 /* ptr_to_packet += K|X */
2116 return check_packet_ptr_add(env, insn);
2117 } else if (BPF_CLASS(insn->code) == BPF_ALU64 &&
2118 dst_reg->type == UNKNOWN_VALUE &&
2119 env->allow_ptr_leaks) {
2120 /* unknown += K|X */
2121 return evaluate_reg_alu(env, insn);
2122 } else if (BPF_CLASS(insn->code) == BPF_ALU64 &&
2123 dst_reg->type == CONST_IMM &&
2124 env->allow_ptr_leaks) {
2125 /* reg_imm += K|X */
2126 return evaluate_reg_imm_alu(env, insn);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002127 } else if (is_pointer_value(env, insn->dst_reg)) {
2128 verbose("R%d pointer arithmetic prohibited\n",
2129 insn->dst_reg);
2130 return -EACCES;
2131 } else if (BPF_SRC(insn->code) == BPF_X &&
2132 is_pointer_value(env, insn->src_reg)) {
2133 verbose("R%d pointer arithmetic prohibited\n",
2134 insn->src_reg);
2135 return -EACCES;
2136 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002137
Josef Bacik48461132016-09-28 10:54:32 -04002138 /* If we did pointer math on a map value then just set it to our
2139 * PTR_TO_MAP_VALUE_ADJ type so we can deal with any stores or
2140 * loads to this register appropriately, otherwise just mark the
2141 * register as unknown.
2142 */
2143 if (env->allow_ptr_leaks &&
Daniel Borkmannfce366a2017-03-31 02:24:02 +02002144 BPF_CLASS(insn->code) == BPF_ALU64 && opcode == BPF_ADD &&
Josef Bacik48461132016-09-28 10:54:32 -04002145 (dst_reg->type == PTR_TO_MAP_VALUE ||
2146 dst_reg->type == PTR_TO_MAP_VALUE_ADJ))
2147 dst_reg->type = PTR_TO_MAP_VALUE_ADJ;
2148 else
2149 mark_reg_unknown_value(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002150 }
2151
2152 return 0;
2153}
2154
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002155static void find_good_pkt_pointers(struct bpf_verifier_state *state,
2156 struct bpf_reg_state *dst_reg)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002157{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002158 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002159 int i;
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002160
2161 /* LLVM can generate two kind of checks:
2162 *
2163 * Type 1:
2164 *
2165 * r2 = r3;
2166 * r2 += 8;
2167 * if (r2 > pkt_end) goto <handle exception>
2168 * <access okay>
2169 *
2170 * Where:
2171 * r2 == dst_reg, pkt_end == src_reg
2172 * r2=pkt(id=n,off=8,r=0)
2173 * r3=pkt(id=n,off=0,r=0)
2174 *
2175 * Type 2:
2176 *
2177 * r2 = r3;
2178 * r2 += 8;
2179 * if (pkt_end >= r2) goto <access okay>
2180 * <handle exception>
2181 *
2182 * Where:
2183 * pkt_end == dst_reg, r2 == src_reg
2184 * r2=pkt(id=n,off=8,r=0)
2185 * r3=pkt(id=n,off=0,r=0)
2186 *
2187 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
2188 * so that range of bytes [r3, r3 + 8) is safe to access.
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002189 */
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002190
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002191 for (i = 0; i < MAX_BPF_REG; i++)
2192 if (regs[i].type == PTR_TO_PACKET && regs[i].id == dst_reg->id)
Alexei Starovoitovb1977682017-03-24 15:57:33 -07002193 /* keep the maximum range already checked */
2194 regs[i].range = max(regs[i].range, dst_reg->off);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002195
2196 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
2197 if (state->stack_slot_type[i] != STACK_SPILL)
2198 continue;
2199 reg = &state->spilled_regs[i / BPF_REG_SIZE];
2200 if (reg->type == PTR_TO_PACKET && reg->id == dst_reg->id)
Alexei Starovoitovb1977682017-03-24 15:57:33 -07002201 reg->range = max(reg->range, dst_reg->off);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002202 }
2203}
2204
Josef Bacik48461132016-09-28 10:54:32 -04002205/* Adjusts the register min/max values in the case that the dst_reg is the
2206 * variable register that we are working on, and src_reg is a constant or we're
2207 * simply doing a BPF_K check.
2208 */
2209static void reg_set_min_max(struct bpf_reg_state *true_reg,
2210 struct bpf_reg_state *false_reg, u64 val,
2211 u8 opcode)
2212{
2213 switch (opcode) {
2214 case BPF_JEQ:
2215 /* If this is false then we know nothing Jon Snow, but if it is
2216 * true then we know for sure.
2217 */
2218 true_reg->max_value = true_reg->min_value = val;
2219 break;
2220 case BPF_JNE:
2221 /* If this is true we know nothing Jon Snow, but if it is false
2222 * we know the value for sure;
2223 */
2224 false_reg->max_value = false_reg->min_value = val;
2225 break;
2226 case BPF_JGT:
2227 /* Unsigned comparison, the minimum value is 0. */
2228 false_reg->min_value = 0;
Alexander Alemayhu7e57fbb2017-02-14 00:02:35 +01002229 /* fallthrough */
Josef Bacik48461132016-09-28 10:54:32 -04002230 case BPF_JSGT:
2231 /* If this is false then we know the maximum val is val,
2232 * otherwise we know the min val is val+1.
2233 */
2234 false_reg->max_value = val;
2235 true_reg->min_value = val + 1;
2236 break;
2237 case BPF_JGE:
2238 /* Unsigned comparison, the minimum value is 0. */
2239 false_reg->min_value = 0;
Alexander Alemayhu7e57fbb2017-02-14 00:02:35 +01002240 /* fallthrough */
Josef Bacik48461132016-09-28 10:54:32 -04002241 case BPF_JSGE:
2242 /* If this is false then we know the maximum value is val - 1,
2243 * otherwise we know the mimimum value is val.
2244 */
2245 false_reg->max_value = val - 1;
2246 true_reg->min_value = val;
2247 break;
2248 default:
2249 break;
2250 }
2251
2252 check_reg_overflow(false_reg);
2253 check_reg_overflow(true_reg);
2254}
2255
2256/* Same as above, but for the case that dst_reg is a CONST_IMM reg and src_reg
2257 * is the variable reg.
2258 */
2259static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
2260 struct bpf_reg_state *false_reg, u64 val,
2261 u8 opcode)
2262{
2263 switch (opcode) {
2264 case BPF_JEQ:
2265 /* If this is false then we know nothing Jon Snow, but if it is
2266 * true then we know for sure.
2267 */
2268 true_reg->max_value = true_reg->min_value = val;
2269 break;
2270 case BPF_JNE:
2271 /* If this is true we know nothing Jon Snow, but if it is false
2272 * we know the value for sure;
2273 */
2274 false_reg->max_value = false_reg->min_value = val;
2275 break;
2276 case BPF_JGT:
2277 /* Unsigned comparison, the minimum value is 0. */
2278 true_reg->min_value = 0;
Alexander Alemayhu7e57fbb2017-02-14 00:02:35 +01002279 /* fallthrough */
Josef Bacik48461132016-09-28 10:54:32 -04002280 case BPF_JSGT:
2281 /*
2282 * If this is false, then the val is <= the register, if it is
2283 * true the register <= to the val.
2284 */
2285 false_reg->min_value = val;
2286 true_reg->max_value = val - 1;
2287 break;
2288 case BPF_JGE:
2289 /* Unsigned comparison, the minimum value is 0. */
2290 true_reg->min_value = 0;
Alexander Alemayhu7e57fbb2017-02-14 00:02:35 +01002291 /* fallthrough */
Josef Bacik48461132016-09-28 10:54:32 -04002292 case BPF_JSGE:
2293 /* If this is false then constant < register, if it is true then
2294 * the register < constant.
2295 */
2296 false_reg->min_value = val + 1;
2297 true_reg->max_value = val;
2298 break;
2299 default:
2300 break;
2301 }
2302
2303 check_reg_overflow(false_reg);
2304 check_reg_overflow(true_reg);
2305}
2306
Thomas Graf57a09bf2016-10-18 19:51:19 +02002307static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id,
2308 enum bpf_reg_type type)
2309{
2310 struct bpf_reg_state *reg = &regs[regno];
2311
2312 if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) {
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07002313 if (type == UNKNOWN_VALUE) {
2314 __mark_reg_unknown_value(regs, regno);
2315 } else if (reg->map_ptr->inner_map_meta) {
2316 reg->type = CONST_PTR_TO_MAP;
2317 reg->map_ptr = reg->map_ptr->inner_map_meta;
2318 } else {
2319 reg->type = type;
2320 }
Daniel Borkmanna08dd0d2016-12-15 01:30:06 +01002321 /* We don't need id from this point onwards anymore, thus we
2322 * should better reset it, so that state pruning has chances
2323 * to take effect.
2324 */
2325 reg->id = 0;
Thomas Graf57a09bf2016-10-18 19:51:19 +02002326 }
2327}
2328
2329/* The logic is similar to find_good_pkt_pointers(), both could eventually
2330 * be folded together at some point.
2331 */
2332static void mark_map_regs(struct bpf_verifier_state *state, u32 regno,
2333 enum bpf_reg_type type)
2334{
2335 struct bpf_reg_state *regs = state->regs;
Daniel Borkmanna08dd0d2016-12-15 01:30:06 +01002336 u32 id = regs[regno].id;
Thomas Graf57a09bf2016-10-18 19:51:19 +02002337 int i;
2338
2339 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmanna08dd0d2016-12-15 01:30:06 +01002340 mark_map_reg(regs, i, id, type);
Thomas Graf57a09bf2016-10-18 19:51:19 +02002341
2342 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
2343 if (state->stack_slot_type[i] != STACK_SPILL)
2344 continue;
Daniel Borkmanna08dd0d2016-12-15 01:30:06 +01002345 mark_map_reg(state->spilled_regs, i / BPF_REG_SIZE, id, type);
Thomas Graf57a09bf2016-10-18 19:51:19 +02002346 }
2347}
2348
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002349static int check_cond_jmp_op(struct bpf_verifier_env *env,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002350 struct bpf_insn *insn, int *insn_idx)
2351{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002352 struct bpf_verifier_state *other_branch, *this_branch = &env->cur_state;
2353 struct bpf_reg_state *regs = this_branch->regs, *dst_reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002354 u8 opcode = BPF_OP(insn->code);
2355 int err;
2356
2357 if (opcode > BPF_EXIT) {
2358 verbose("invalid BPF_JMP opcode %x\n", opcode);
2359 return -EINVAL;
2360 }
2361
2362 if (BPF_SRC(insn->code) == BPF_X) {
2363 if (insn->imm != 0) {
2364 verbose("BPF_JMP uses reserved fields\n");
2365 return -EINVAL;
2366 }
2367
2368 /* check src1 operand */
2369 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2370 if (err)
2371 return err;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002372
2373 if (is_pointer_value(env, insn->src_reg)) {
2374 verbose("R%d pointer comparison prohibited\n",
2375 insn->src_reg);
2376 return -EACCES;
2377 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002378 } else {
2379 if (insn->src_reg != BPF_REG_0) {
2380 verbose("BPF_JMP uses reserved fields\n");
2381 return -EINVAL;
2382 }
2383 }
2384
2385 /* check src2 operand */
2386 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
2387 if (err)
2388 return err;
2389
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002390 dst_reg = &regs[insn->dst_reg];
2391
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002392 /* detect if R == 0 where R was initialized to zero earlier */
2393 if (BPF_SRC(insn->code) == BPF_K &&
2394 (opcode == BPF_JEQ || opcode == BPF_JNE) &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002395 dst_reg->type == CONST_IMM && dst_reg->imm == insn->imm) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002396 if (opcode == BPF_JEQ) {
2397 /* if (imm == imm) goto pc+off;
2398 * only follow the goto, ignore fall-through
2399 */
2400 *insn_idx += insn->off;
2401 return 0;
2402 } else {
2403 /* if (imm != imm) goto pc+off;
2404 * only follow fall-through branch, since
2405 * that's where the program will go
2406 */
2407 return 0;
2408 }
2409 }
2410
2411 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx);
2412 if (!other_branch)
2413 return -EFAULT;
2414
Josef Bacik48461132016-09-28 10:54:32 -04002415 /* detect if we are comparing against a constant value so we can adjust
2416 * our min/max values for our dst register.
2417 */
2418 if (BPF_SRC(insn->code) == BPF_X) {
2419 if (regs[insn->src_reg].type == CONST_IMM)
2420 reg_set_min_max(&other_branch->regs[insn->dst_reg],
2421 dst_reg, regs[insn->src_reg].imm,
2422 opcode);
2423 else if (dst_reg->type == CONST_IMM)
2424 reg_set_min_max_inv(&other_branch->regs[insn->src_reg],
2425 &regs[insn->src_reg], dst_reg->imm,
2426 opcode);
2427 } else {
2428 reg_set_min_max(&other_branch->regs[insn->dst_reg],
2429 dst_reg, insn->imm, opcode);
2430 }
2431
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002432 /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002433 if (BPF_SRC(insn->code) == BPF_K &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002434 insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
2435 dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
Thomas Graf57a09bf2016-10-18 19:51:19 +02002436 /* Mark all identical map registers in each branch as either
2437 * safe or unknown depending R == 0 or R != 0 conditional.
2438 */
2439 mark_map_regs(this_branch, insn->dst_reg,
2440 opcode == BPF_JEQ ? PTR_TO_MAP_VALUE : UNKNOWN_VALUE);
2441 mark_map_regs(other_branch, insn->dst_reg,
2442 opcode == BPF_JEQ ? UNKNOWN_VALUE : PTR_TO_MAP_VALUE);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002443 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT &&
2444 dst_reg->type == PTR_TO_PACKET &&
2445 regs[insn->src_reg].type == PTR_TO_PACKET_END) {
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002446 find_good_pkt_pointers(this_branch, dst_reg);
2447 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGE &&
2448 dst_reg->type == PTR_TO_PACKET_END &&
2449 regs[insn->src_reg].type == PTR_TO_PACKET) {
2450 find_good_pkt_pointers(other_branch, &regs[insn->src_reg]);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002451 } else if (is_pointer_value(env, insn->dst_reg)) {
2452 verbose("R%d pointer comparison prohibited\n", insn->dst_reg);
2453 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002454 }
2455 if (log_level)
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002456 print_verifier_state(this_branch);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002457 return 0;
2458}
2459
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002460/* return the map pointer stored inside BPF_LD_IMM64 instruction */
2461static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
2462{
2463 u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
2464
2465 return (struct bpf_map *) (unsigned long) imm64;
2466}
2467
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002468/* verify BPF_LD_IMM64 instruction */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002469static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002470{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002471 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002472 int err;
2473
2474 if (BPF_SIZE(insn->code) != BPF_DW) {
2475 verbose("invalid BPF_LD_IMM insn\n");
2476 return -EINVAL;
2477 }
2478 if (insn->off != 0) {
2479 verbose("BPF_LD_IMM64 uses reserved fields\n");
2480 return -EINVAL;
2481 }
2482
2483 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
2484 if (err)
2485 return err;
2486
Jakub Kicinski6b173872016-09-21 11:43:59 +01002487 if (insn->src_reg == 0) {
Jakub Kicinski6b173872016-09-21 11:43:59 +01002488 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
2489
Jakub Kicinski6b173872016-09-21 11:43:59 +01002490 regs[insn->dst_reg].type = CONST_IMM;
2491 regs[insn->dst_reg].imm = imm;
Daniel Borkmann4a2ff552017-06-11 00:50:42 +02002492 regs[insn->dst_reg].id = 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002493 return 0;
Jakub Kicinski6b173872016-09-21 11:43:59 +01002494 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002495
2496 /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
2497 BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
2498
2499 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
2500 regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
2501 return 0;
2502}
2503
Daniel Borkmann96be4322015-03-01 12:31:46 +01002504static bool may_access_skb(enum bpf_prog_type type)
2505{
2506 switch (type) {
2507 case BPF_PROG_TYPE_SOCKET_FILTER:
2508 case BPF_PROG_TYPE_SCHED_CLS:
Daniel Borkmann94caee8c2015-03-20 15:11:11 +01002509 case BPF_PROG_TYPE_SCHED_ACT:
Daniel Borkmann96be4322015-03-01 12:31:46 +01002510 return true;
2511 default:
2512 return false;
2513 }
2514}
2515
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002516/* verify safety of LD_ABS|LD_IND instructions:
2517 * - they can only appear in the programs where ctx == skb
2518 * - since they are wrappers of function calls, they scratch R1-R5 registers,
2519 * preserve R6-R9, and store return value into R0
2520 *
2521 * Implicit input:
2522 * ctx == skb == R6 == CTX
2523 *
2524 * Explicit input:
2525 * SRC == any register
2526 * IMM == 32-bit immediate
2527 *
2528 * Output:
2529 * R0 - 8/16/32-bit skb data converted to cpu endianness
2530 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002531static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002532{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002533 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002534 u8 mode = BPF_MODE(insn->code);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002535 int i, err;
2536
Daniel Borkmann24701ec2015-03-01 12:31:47 +01002537 if (!may_access_skb(env->prog->type)) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002538 verbose("BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002539 return -EINVAL;
2540 }
2541
2542 if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
Alexei Starovoitovd82bccc2016-04-12 10:26:19 -07002543 BPF_SIZE(insn->code) == BPF_DW ||
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002544 (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002545 verbose("BPF_LD_[ABS|IND] uses reserved fields\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002546 return -EINVAL;
2547 }
2548
2549 /* check whether implicit source operand (register R6) is readable */
2550 err = check_reg_arg(regs, BPF_REG_6, SRC_OP);
2551 if (err)
2552 return err;
2553
2554 if (regs[BPF_REG_6].type != PTR_TO_CTX) {
2555 verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
2556 return -EINVAL;
2557 }
2558
2559 if (mode == BPF_IND) {
2560 /* check explicit source operand */
2561 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2562 if (err)
2563 return err;
2564 }
2565
2566 /* reset caller saved regs to unreadable */
Daniel Borkmanna9789ef2017-05-25 01:05:06 +02002567 for (i = 0; i < CALLER_SAVED_REGS; i++)
2568 mark_reg_not_init(regs, caller_saved[i]);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002569
2570 /* mark destination R0 register as readable, since it contains
2571 * the value fetched from the packet
2572 */
2573 regs[BPF_REG_0].type = UNKNOWN_VALUE;
2574 return 0;
2575}
2576
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002577/* non-recursive DFS pseudo code
2578 * 1 procedure DFS-iterative(G,v):
2579 * 2 label v as discovered
2580 * 3 let S be a stack
2581 * 4 S.push(v)
2582 * 5 while S is not empty
2583 * 6 t <- S.pop()
2584 * 7 if t is what we're looking for:
2585 * 8 return t
2586 * 9 for all edges e in G.adjacentEdges(t) do
2587 * 10 if edge e is already labelled
2588 * 11 continue with the next edge
2589 * 12 w <- G.adjacentVertex(t,e)
2590 * 13 if vertex w is not discovered and not explored
2591 * 14 label e as tree-edge
2592 * 15 label w as discovered
2593 * 16 S.push(w)
2594 * 17 continue at 5
2595 * 18 else if vertex w is discovered
2596 * 19 label e as back-edge
2597 * 20 else
2598 * 21 // vertex w is explored
2599 * 22 label e as forward- or cross-edge
2600 * 23 label t as explored
2601 * 24 S.pop()
2602 *
2603 * convention:
2604 * 0x10 - discovered
2605 * 0x11 - discovered and fall-through edge labelled
2606 * 0x12 - discovered and fall-through and branch edges labelled
2607 * 0x20 - explored
2608 */
2609
2610enum {
2611 DISCOVERED = 0x10,
2612 EXPLORED = 0x20,
2613 FALLTHROUGH = 1,
2614 BRANCH = 2,
2615};
2616
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002617#define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002618
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002619static int *insn_stack; /* stack of insns to process */
2620static int cur_stack; /* current stack index */
2621static int *insn_state;
2622
2623/* t, w, e - match pseudo-code above:
2624 * t - index of current instruction
2625 * w - next instruction
2626 * e - edge
2627 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002628static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002629{
2630 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
2631 return 0;
2632
2633 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
2634 return 0;
2635
2636 if (w < 0 || w >= env->prog->len) {
2637 verbose("jump out of range from insn %d to %d\n", t, w);
2638 return -EINVAL;
2639 }
2640
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002641 if (e == BRANCH)
2642 /* mark branch target for state pruning */
2643 env->explored_states[w] = STATE_LIST_MARK;
2644
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002645 if (insn_state[w] == 0) {
2646 /* tree-edge */
2647 insn_state[t] = DISCOVERED | e;
2648 insn_state[w] = DISCOVERED;
2649 if (cur_stack >= env->prog->len)
2650 return -E2BIG;
2651 insn_stack[cur_stack++] = w;
2652 return 1;
2653 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
2654 verbose("back-edge from insn %d to %d\n", t, w);
2655 return -EINVAL;
2656 } else if (insn_state[w] == EXPLORED) {
2657 /* forward- or cross-edge */
2658 insn_state[t] = DISCOVERED | e;
2659 } else {
2660 verbose("insn state internal bug\n");
2661 return -EFAULT;
2662 }
2663 return 0;
2664}
2665
2666/* non-recursive depth-first-search to detect loops in BPF program
2667 * loop == back-edge in directed graph
2668 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002669static int check_cfg(struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002670{
2671 struct bpf_insn *insns = env->prog->insnsi;
2672 int insn_cnt = env->prog->len;
2673 int ret = 0;
2674 int i, t;
2675
2676 insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
2677 if (!insn_state)
2678 return -ENOMEM;
2679
2680 insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
2681 if (!insn_stack) {
2682 kfree(insn_state);
2683 return -ENOMEM;
2684 }
2685
2686 insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
2687 insn_stack[0] = 0; /* 0 is the first instruction */
2688 cur_stack = 1;
2689
2690peek_stack:
2691 if (cur_stack == 0)
2692 goto check_state;
2693 t = insn_stack[cur_stack - 1];
2694
2695 if (BPF_CLASS(insns[t].code) == BPF_JMP) {
2696 u8 opcode = BPF_OP(insns[t].code);
2697
2698 if (opcode == BPF_EXIT) {
2699 goto mark_explored;
2700 } else if (opcode == BPF_CALL) {
2701 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2702 if (ret == 1)
2703 goto peek_stack;
2704 else if (ret < 0)
2705 goto err_free;
Daniel Borkmann07016152016-04-05 22:33:17 +02002706 if (t + 1 < insn_cnt)
2707 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002708 } else if (opcode == BPF_JA) {
2709 if (BPF_SRC(insns[t].code) != BPF_K) {
2710 ret = -EINVAL;
2711 goto err_free;
2712 }
2713 /* unconditional jump with single edge */
2714 ret = push_insn(t, t + insns[t].off + 1,
2715 FALLTHROUGH, env);
2716 if (ret == 1)
2717 goto peek_stack;
2718 else if (ret < 0)
2719 goto err_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002720 /* tell verifier to check for equivalent states
2721 * after every call and jump
2722 */
Alexei Starovoitovc3de6312015-04-14 15:57:13 -07002723 if (t + 1 < insn_cnt)
2724 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002725 } else {
2726 /* conditional jump with two edges */
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02002727 env->explored_states[t] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002728 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2729 if (ret == 1)
2730 goto peek_stack;
2731 else if (ret < 0)
2732 goto err_free;
2733
2734 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
2735 if (ret == 1)
2736 goto peek_stack;
2737 else if (ret < 0)
2738 goto err_free;
2739 }
2740 } else {
2741 /* all other non-branch instructions with single
2742 * fall-through edge
2743 */
2744 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2745 if (ret == 1)
2746 goto peek_stack;
2747 else if (ret < 0)
2748 goto err_free;
2749 }
2750
2751mark_explored:
2752 insn_state[t] = EXPLORED;
2753 if (cur_stack-- <= 0) {
2754 verbose("pop stack internal bug\n");
2755 ret = -EFAULT;
2756 goto err_free;
2757 }
2758 goto peek_stack;
2759
2760check_state:
2761 for (i = 0; i < insn_cnt; i++) {
2762 if (insn_state[i] != EXPLORED) {
2763 verbose("unreachable insn %d\n", i);
2764 ret = -EINVAL;
2765 goto err_free;
2766 }
2767 }
2768 ret = 0; /* cfg looks good */
2769
2770err_free:
2771 kfree(insn_state);
2772 kfree(insn_stack);
2773 return ret;
2774}
2775
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002776/* the following conditions reduce the number of explored insns
2777 * from ~140k to ~80k for ultra large programs that use a lot of ptr_to_packet
2778 */
Daniel Borkmann1ad2f582017-05-25 01:05:05 +02002779static bool compare_ptrs_to_packet(struct bpf_verifier_env *env,
2780 struct bpf_reg_state *old,
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002781 struct bpf_reg_state *cur)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002782{
2783 if (old->id != cur->id)
2784 return false;
2785
2786 /* old ptr_to_packet is more conservative, since it allows smaller
2787 * range. Ex:
2788 * old(off=0,r=10) is equal to cur(off=0,r=20), because
2789 * old(off=0,r=10) means that with range=10 the verifier proceeded
2790 * further and found no issues with the program. Now we're in the same
2791 * spot with cur(off=0,r=20), so we're safe too, since anything further
2792 * will only be looking at most 10 bytes after this pointer.
2793 */
2794 if (old->off == cur->off && old->range < cur->range)
2795 return true;
2796
2797 /* old(off=20,r=10) is equal to cur(off=22,re=22 or 5 or 0)
2798 * since both cannot be used for packet access and safe(old)
2799 * pointer has smaller off that could be used for further
2800 * 'if (ptr > data_end)' check
2801 * Ex:
2802 * old(off=20,r=10) and cur(off=22,r=22) and cur(off=22,r=0) mean
2803 * that we cannot access the packet.
2804 * The safe range is:
2805 * [ptr, ptr + range - off)
2806 * so whenever off >=range, it means no safe bytes from this pointer.
2807 * When comparing old->off <= cur->off, it means that older code
2808 * went with smaller offset and that offset was later
2809 * used to figure out the safe range after 'if (ptr > data_end)' check
2810 * Say, 'old' state was explored like:
2811 * ... R3(off=0, r=0)
2812 * R4 = R3 + 20
2813 * ... now R4(off=20,r=0) <-- here
2814 * if (R4 > data_end)
2815 * ... R4(off=20,r=20), R3(off=0,r=20) and R3 can be used to access.
2816 * ... the code further went all the way to bpf_exit.
2817 * Now the 'cur' state at the mark 'here' has R4(off=30,r=0).
2818 * old_R4(off=20,r=0) equal to cur_R4(off=30,r=0), since if the verifier
2819 * goes further, such cur_R4 will give larger safe packet range after
2820 * 'if (R4 > data_end)' and all further insn were already good with r=20,
2821 * so they will be good with r=30 and we can prune the search.
2822 */
Daniel Borkmann1ad2f582017-05-25 01:05:05 +02002823 if (!env->strict_alignment && old->off <= cur->off &&
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002824 old->off >= old->range && cur->off >= cur->range)
2825 return true;
2826
2827 return false;
2828}
2829
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002830/* compare two verifier states
2831 *
2832 * all states stored in state_list are known to be valid, since
2833 * verifier reached 'bpf_exit' instruction through them
2834 *
2835 * this function is called when verifier exploring different branches of
2836 * execution popped from the state stack. If it sees an old state that has
2837 * more strict register state and more strict stack state then this execution
2838 * branch doesn't need to be explored further, since verifier already
2839 * concluded that more strict state leads to valid finish.
2840 *
2841 * Therefore two states are equivalent if register state is more conservative
2842 * and explored stack state is more conservative than the current one.
2843 * Example:
2844 * explored current
2845 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
2846 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
2847 *
2848 * In other words if current stack state (one being explored) has more
2849 * valid slots than old one that already passed validation, it means
2850 * the verifier can stop exploring and conclude that current state is valid too
2851 *
2852 * Similarly with registers. If explored state has register type as invalid
2853 * whereas register type in current state is meaningful, it means that
2854 * the current state will reach 'bpf_exit' instruction safely
2855 */
Josef Bacik48461132016-09-28 10:54:32 -04002856static bool states_equal(struct bpf_verifier_env *env,
2857 struct bpf_verifier_state *old,
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002858 struct bpf_verifier_state *cur)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002859{
Josef Bacike2d2afe2016-11-29 12:27:09 -05002860 bool varlen_map_access = env->varlen_map_value_access;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002861 struct bpf_reg_state *rold, *rcur;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002862 int i;
2863
2864 for (i = 0; i < MAX_BPF_REG; i++) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002865 rold = &old->regs[i];
2866 rcur = &cur->regs[i];
2867
2868 if (memcmp(rold, rcur, sizeof(*rold)) == 0)
2869 continue;
2870
Josef Bacik48461132016-09-28 10:54:32 -04002871 /* If the ranges were not the same, but everything else was and
2872 * we didn't do a variable access into a map then we are a-ok.
2873 */
Josef Bacike2d2afe2016-11-29 12:27:09 -05002874 if (!varlen_map_access &&
Alexei Starovoitovd2a4dd32016-12-07 10:57:59 -08002875 memcmp(rold, rcur, offsetofend(struct bpf_reg_state, id)) == 0)
Josef Bacik48461132016-09-28 10:54:32 -04002876 continue;
2877
Josef Bacike2d2afe2016-11-29 12:27:09 -05002878 /* If we didn't map access then again we don't care about the
2879 * mismatched range values and it's ok if our old type was
2880 * UNKNOWN and we didn't go to a NOT_INIT'ed reg.
2881 */
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002882 if (rold->type == NOT_INIT ||
Josef Bacike2d2afe2016-11-29 12:27:09 -05002883 (!varlen_map_access && rold->type == UNKNOWN_VALUE &&
2884 rcur->type != NOT_INIT))
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002885 continue;
2886
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02002887 /* Don't care about the reg->id in this case. */
2888 if (rold->type == PTR_TO_MAP_VALUE_OR_NULL &&
2889 rcur->type == PTR_TO_MAP_VALUE_OR_NULL &&
2890 rold->map_ptr == rcur->map_ptr)
2891 continue;
2892
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002893 if (rold->type == PTR_TO_PACKET && rcur->type == PTR_TO_PACKET &&
Daniel Borkmann1ad2f582017-05-25 01:05:05 +02002894 compare_ptrs_to_packet(env, rold, rcur))
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002895 continue;
2896
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002897 return false;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002898 }
2899
2900 for (i = 0; i < MAX_BPF_STACK; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002901 if (old->stack_slot_type[i] == STACK_INVALID)
2902 continue;
2903 if (old->stack_slot_type[i] != cur->stack_slot_type[i])
2904 /* Ex: old explored (safe) state has STACK_SPILL in
2905 * this stack slot, but current has has STACK_MISC ->
2906 * this verifier states are not equivalent,
2907 * return false to continue verification of this path
2908 */
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002909 return false;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002910 if (i % BPF_REG_SIZE)
2911 continue;
Daniel Borkmannd25da6c2017-06-11 00:50:41 +02002912 if (old->stack_slot_type[i] != STACK_SPILL)
2913 continue;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002914 if (memcmp(&old->spilled_regs[i / BPF_REG_SIZE],
2915 &cur->spilled_regs[i / BPF_REG_SIZE],
2916 sizeof(old->spilled_regs[0])))
2917 /* when explored and current stack slot types are
2918 * the same, check that stored pointers types
2919 * are the same as well.
2920 * Ex: explored safe path could have stored
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002921 * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -8}
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002922 * but current path has stored:
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002923 * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -16}
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002924 * such verifier states are not equivalent.
2925 * return false to continue verification of this path
2926 */
2927 return false;
2928 else
2929 continue;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002930 }
2931 return true;
2932}
2933
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002934static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002935{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002936 struct bpf_verifier_state_list *new_sl;
2937 struct bpf_verifier_state_list *sl;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002938
2939 sl = env->explored_states[insn_idx];
2940 if (!sl)
2941 /* this 'insn_idx' instruction wasn't marked, so we will not
2942 * be doing state search here
2943 */
2944 return 0;
2945
2946 while (sl != STATE_LIST_MARK) {
Josef Bacik48461132016-09-28 10:54:32 -04002947 if (states_equal(env, &sl->state, &env->cur_state))
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002948 /* reached equivalent register/stack state,
2949 * prune the search
2950 */
2951 return 1;
2952 sl = sl->next;
2953 }
2954
2955 /* there were no equivalent states, remember current one.
2956 * technically the current state is not proven to be safe yet,
2957 * but it will either reach bpf_exit (which means it's safe) or
2958 * it will be rejected. Since there are no loops, we won't be
2959 * seeing this 'insn_idx' instruction again on the way to bpf_exit
2960 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002961 new_sl = kmalloc(sizeof(struct bpf_verifier_state_list), GFP_USER);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002962 if (!new_sl)
2963 return -ENOMEM;
2964
2965 /* add new state to the head of linked list */
2966 memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state));
2967 new_sl->next = env->explored_states[insn_idx];
2968 env->explored_states[insn_idx] = new_sl;
2969 return 0;
2970}
2971
Jakub Kicinski13a27df2016-09-21 11:43:58 +01002972static int ext_analyzer_insn_hook(struct bpf_verifier_env *env,
2973 int insn_idx, int prev_insn_idx)
2974{
2975 if (!env->analyzer_ops || !env->analyzer_ops->insn_hook)
2976 return 0;
2977
2978 return env->analyzer_ops->insn_hook(env, insn_idx, prev_insn_idx);
2979}
2980
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002981static int do_check(struct bpf_verifier_env *env)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002982{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002983 struct bpf_verifier_state *state = &env->cur_state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002984 struct bpf_insn *insns = env->prog->insnsi;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002985 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002986 int insn_cnt = env->prog->len;
2987 int insn_idx, prev_insn_idx = 0;
2988 int insn_processed = 0;
2989 bool do_print_state = false;
2990
2991 init_reg_state(regs);
2992 insn_idx = 0;
Josef Bacik48461132016-09-28 10:54:32 -04002993 env->varlen_map_value_access = false;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002994 for (;;) {
2995 struct bpf_insn *insn;
2996 u8 class;
2997 int err;
2998
2999 if (insn_idx >= insn_cnt) {
3000 verbose("invalid insn idx %d insn_cnt %d\n",
3001 insn_idx, insn_cnt);
3002 return -EFAULT;
3003 }
3004
3005 insn = &insns[insn_idx];
3006 class = BPF_CLASS(insn->code);
3007
Daniel Borkmann07016152016-04-05 22:33:17 +02003008 if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
Colin Ian Kingbc1750f2017-02-23 00:20:53 +00003009 verbose("BPF program is too large. Processed %d insn\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003010 insn_processed);
3011 return -E2BIG;
3012 }
3013
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003014 err = is_state_visited(env, insn_idx);
3015 if (err < 0)
3016 return err;
3017 if (err == 1) {
3018 /* found equivalent state, can prune the search */
3019 if (log_level) {
3020 if (do_print_state)
3021 verbose("\nfrom %d to %d: safe\n",
3022 prev_insn_idx, insn_idx);
3023 else
3024 verbose("%d: safe\n", insn_idx);
3025 }
3026 goto process_bpf_exit;
3027 }
3028
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02003029 if (need_resched())
3030 cond_resched();
3031
David S. Millerc5fc9692017-05-10 11:25:17 -07003032 if (log_level > 1 || (log_level && do_print_state)) {
3033 if (log_level > 1)
3034 verbose("%d:", insn_idx);
3035 else
3036 verbose("\nfrom %d to %d:",
3037 prev_insn_idx, insn_idx);
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003038 print_verifier_state(&env->cur_state);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003039 do_print_state = false;
3040 }
3041
3042 if (log_level) {
3043 verbose("%d: ", insn_idx);
Daniel Borkmann0d0e5762017-05-08 00:04:09 +02003044 print_bpf_insn(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003045 }
3046
Jakub Kicinski13a27df2016-09-21 11:43:58 +01003047 err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx);
3048 if (err)
3049 return err;
3050
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003051 if (class == BPF_ALU || class == BPF_ALU64) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003052 err = check_alu_op(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003053 if (err)
3054 return err;
3055
3056 } else if (class == BPF_LDX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003057 enum bpf_reg_type *prev_src_type, src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003058
3059 /* check for reserved fields is already done */
3060
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003061 /* check src operand */
3062 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
3063 if (err)
3064 return err;
3065
3066 err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
3067 if (err)
3068 return err;
3069
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07003070 src_reg_type = regs[insn->src_reg].type;
3071
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003072 /* check that memory (src_reg + off) is readable,
3073 * the state of dst_reg will be updated by this func
3074 */
Yonghong Song31fd8582017-06-13 15:52:13 -07003075 err = check_mem_access(env, insn_idx, insn->src_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003076 BPF_SIZE(insn->code), BPF_READ,
3077 insn->dst_reg);
3078 if (err)
3079 return err;
3080
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003081 prev_src_type = &env->insn_aux_data[insn_idx].ptr_type;
3082
3083 if (*prev_src_type == NOT_INIT) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003084 /* saw a valid insn
3085 * dst_reg = *(u32 *)(src_reg + off)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003086 * save type to validate intersecting paths
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003087 */
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003088 *prev_src_type = src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003089
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003090 } else if (src_reg_type != *prev_src_type &&
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003091 (src_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003092 *prev_src_type == PTR_TO_CTX)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003093 /* ABuser program is trying to use the same insn
3094 * dst_reg = *(u32*) (src_reg + off)
3095 * with different pointer types:
3096 * src_reg == ctx in one branch and
3097 * src_reg == stack|map in some other branch.
3098 * Reject it.
3099 */
3100 verbose("same insn cannot be used with different pointers\n");
3101 return -EINVAL;
3102 }
3103
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003104 } else if (class == BPF_STX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003105 enum bpf_reg_type *prev_dst_type, dst_reg_type;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003106
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003107 if (BPF_MODE(insn->code) == BPF_XADD) {
Yonghong Song31fd8582017-06-13 15:52:13 -07003108 err = check_xadd(env, insn_idx, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003109 if (err)
3110 return err;
3111 insn_idx++;
3112 continue;
3113 }
3114
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003115 /* check src1 operand */
3116 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
3117 if (err)
3118 return err;
3119 /* check src2 operand */
3120 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
3121 if (err)
3122 return err;
3123
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003124 dst_reg_type = regs[insn->dst_reg].type;
3125
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003126 /* check that memory (dst_reg + off) is writeable */
Yonghong Song31fd8582017-06-13 15:52:13 -07003127 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003128 BPF_SIZE(insn->code), BPF_WRITE,
3129 insn->src_reg);
3130 if (err)
3131 return err;
3132
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003133 prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type;
3134
3135 if (*prev_dst_type == NOT_INIT) {
3136 *prev_dst_type = dst_reg_type;
3137 } else if (dst_reg_type != *prev_dst_type &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003138 (dst_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003139 *prev_dst_type == PTR_TO_CTX)) {
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003140 verbose("same insn cannot be used with different pointers\n");
3141 return -EINVAL;
3142 }
3143
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003144 } else if (class == BPF_ST) {
3145 if (BPF_MODE(insn->code) != BPF_MEM ||
3146 insn->src_reg != BPF_REG_0) {
3147 verbose("BPF_ST uses reserved fields\n");
3148 return -EINVAL;
3149 }
3150 /* check src operand */
3151 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
3152 if (err)
3153 return err;
3154
3155 /* check that memory (dst_reg + off) is writeable */
Yonghong Song31fd8582017-06-13 15:52:13 -07003156 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003157 BPF_SIZE(insn->code), BPF_WRITE,
3158 -1);
3159 if (err)
3160 return err;
3161
3162 } else if (class == BPF_JMP) {
3163 u8 opcode = BPF_OP(insn->code);
3164
3165 if (opcode == BPF_CALL) {
3166 if (BPF_SRC(insn->code) != BPF_K ||
3167 insn->off != 0 ||
3168 insn->src_reg != BPF_REG_0 ||
3169 insn->dst_reg != BPF_REG_0) {
3170 verbose("BPF_CALL uses reserved fields\n");
3171 return -EINVAL;
3172 }
3173
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07003174 err = check_call(env, insn->imm, insn_idx);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003175 if (err)
3176 return err;
3177
3178 } else if (opcode == BPF_JA) {
3179 if (BPF_SRC(insn->code) != BPF_K ||
3180 insn->imm != 0 ||
3181 insn->src_reg != BPF_REG_0 ||
3182 insn->dst_reg != BPF_REG_0) {
3183 verbose("BPF_JA uses reserved fields\n");
3184 return -EINVAL;
3185 }
3186
3187 insn_idx += insn->off + 1;
3188 continue;
3189
3190 } else if (opcode == BPF_EXIT) {
3191 if (BPF_SRC(insn->code) != BPF_K ||
3192 insn->imm != 0 ||
3193 insn->src_reg != BPF_REG_0 ||
3194 insn->dst_reg != BPF_REG_0) {
3195 verbose("BPF_EXIT uses reserved fields\n");
3196 return -EINVAL;
3197 }
3198
3199 /* eBPF calling convetion is such that R0 is used
3200 * to return the value from eBPF program.
3201 * Make sure that it's readable at this time
3202 * of bpf_exit, which means that program wrote
3203 * something into it earlier
3204 */
3205 err = check_reg_arg(regs, BPF_REG_0, SRC_OP);
3206 if (err)
3207 return err;
3208
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003209 if (is_pointer_value(env, BPF_REG_0)) {
3210 verbose("R0 leaks addr as return value\n");
3211 return -EACCES;
3212 }
3213
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003214process_bpf_exit:
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003215 insn_idx = pop_stack(env, &prev_insn_idx);
3216 if (insn_idx < 0) {
3217 break;
3218 } else {
3219 do_print_state = true;
3220 continue;
3221 }
3222 } else {
3223 err = check_cond_jmp_op(env, insn, &insn_idx);
3224 if (err)
3225 return err;
3226 }
3227 } else if (class == BPF_LD) {
3228 u8 mode = BPF_MODE(insn->code);
3229
3230 if (mode == BPF_ABS || mode == BPF_IND) {
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003231 err = check_ld_abs(env, insn);
3232 if (err)
3233 return err;
3234
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003235 } else if (mode == BPF_IMM) {
3236 err = check_ld_imm(env, insn);
3237 if (err)
3238 return err;
3239
3240 insn_idx++;
3241 } else {
3242 verbose("invalid BPF_LD mode\n");
3243 return -EINVAL;
3244 }
Josef Bacik48461132016-09-28 10:54:32 -04003245 reset_reg_range_values(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003246 } else {
3247 verbose("unknown insn class %d\n", class);
3248 return -EINVAL;
3249 }
3250
3251 insn_idx++;
3252 }
3253
Alexei Starovoitov87266792017-05-30 13:31:29 -07003254 verbose("processed %d insns, stack depth %d\n",
3255 insn_processed, env->prog->aux->stack_depth);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003256 return 0;
3257}
3258
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07003259static int check_map_prealloc(struct bpf_map *map)
3260{
3261 return (map->map_type != BPF_MAP_TYPE_HASH &&
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07003262 map->map_type != BPF_MAP_TYPE_PERCPU_HASH &&
3263 map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) ||
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07003264 !(map->map_flags & BPF_F_NO_PREALLOC);
3265}
3266
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07003267static int check_map_prog_compatibility(struct bpf_map *map,
3268 struct bpf_prog *prog)
3269
3270{
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07003271 /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use
3272 * preallocated hash maps, since doing memory allocation
3273 * in overflow_handler can crash depending on where nmi got
3274 * triggered.
3275 */
3276 if (prog->type == BPF_PROG_TYPE_PERF_EVENT) {
3277 if (!check_map_prealloc(map)) {
3278 verbose("perf_event programs can only use preallocated hash map\n");
3279 return -EINVAL;
3280 }
3281 if (map->inner_map_meta &&
3282 !check_map_prealloc(map->inner_map_meta)) {
3283 verbose("perf_event programs can only use preallocated inner hash map\n");
3284 return -EINVAL;
3285 }
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07003286 }
3287 return 0;
3288}
3289
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003290/* look for pseudo eBPF instructions that access map FDs and
3291 * replace them with actual map pointers
3292 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003293static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003294{
3295 struct bpf_insn *insn = env->prog->insnsi;
3296 int insn_cnt = env->prog->len;
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07003297 int i, j, err;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003298
Daniel Borkmannf1f77142017-01-13 23:38:15 +01003299 err = bpf_prog_calc_tag(env->prog);
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01003300 if (err)
3301 return err;
3302
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003303 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003304 if (BPF_CLASS(insn->code) == BPF_LDX &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003305 (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003306 verbose("BPF_LDX uses reserved fields\n");
3307 return -EINVAL;
3308 }
3309
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003310 if (BPF_CLASS(insn->code) == BPF_STX &&
3311 ((BPF_MODE(insn->code) != BPF_MEM &&
3312 BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
3313 verbose("BPF_STX uses reserved fields\n");
3314 return -EINVAL;
3315 }
3316
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003317 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
3318 struct bpf_map *map;
3319 struct fd f;
3320
3321 if (i == insn_cnt - 1 || insn[1].code != 0 ||
3322 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
3323 insn[1].off != 0) {
3324 verbose("invalid bpf_ld_imm64 insn\n");
3325 return -EINVAL;
3326 }
3327
3328 if (insn->src_reg == 0)
3329 /* valid generic load 64-bit imm */
3330 goto next_insn;
3331
3332 if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
3333 verbose("unrecognized bpf_ld_imm64 insn\n");
3334 return -EINVAL;
3335 }
3336
3337 f = fdget(insn->imm);
Daniel Borkmannc2101292015-10-29 14:58:07 +01003338 map = __bpf_map_get(f);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003339 if (IS_ERR(map)) {
3340 verbose("fd %d is not pointing to valid bpf_map\n",
3341 insn->imm);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003342 return PTR_ERR(map);
3343 }
3344
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07003345 err = check_map_prog_compatibility(map, env->prog);
3346 if (err) {
3347 fdput(f);
3348 return err;
3349 }
3350
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003351 /* store map pointer inside BPF_LD_IMM64 instruction */
3352 insn[0].imm = (u32) (unsigned long) map;
3353 insn[1].imm = ((u64) (unsigned long) map) >> 32;
3354
3355 /* check whether we recorded this map already */
3356 for (j = 0; j < env->used_map_cnt; j++)
3357 if (env->used_maps[j] == map) {
3358 fdput(f);
3359 goto next_insn;
3360 }
3361
3362 if (env->used_map_cnt >= MAX_USED_MAPS) {
3363 fdput(f);
3364 return -E2BIG;
3365 }
3366
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003367 /* hold the map. If the program is rejected by verifier,
3368 * the map will be released by release_maps() or it
3369 * will be used by the valid program until it's unloaded
3370 * and all maps are released in free_bpf_prog_info()
3371 */
Alexei Starovoitov92117d82016-04-27 18:56:20 -07003372 map = bpf_map_inc(map, false);
3373 if (IS_ERR(map)) {
3374 fdput(f);
3375 return PTR_ERR(map);
3376 }
3377 env->used_maps[env->used_map_cnt++] = map;
3378
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003379 fdput(f);
3380next_insn:
3381 insn++;
3382 i++;
3383 }
3384 }
3385
3386 /* now all pseudo BPF_LD_IMM64 instructions load valid
3387 * 'struct bpf_map *' into a register instead of user map_fd.
3388 * These pointers will be used later by verifier to validate map access.
3389 */
3390 return 0;
3391}
3392
3393/* drop refcnt of maps used by the rejected program */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003394static void release_maps(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003395{
3396 int i;
3397
3398 for (i = 0; i < env->used_map_cnt; i++)
3399 bpf_map_put(env->used_maps[i]);
3400}
3401
3402/* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003403static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003404{
3405 struct bpf_insn *insn = env->prog->insnsi;
3406 int insn_cnt = env->prog->len;
3407 int i;
3408
3409 for (i = 0; i < insn_cnt; i++, insn++)
3410 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
3411 insn->src_reg = 0;
3412}
3413
Alexei Starovoitov80419022017-03-15 18:26:41 -07003414/* single env->prog->insni[off] instruction was replaced with the range
3415 * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
3416 * [0, off) and [off, end) to new locations, so the patched range stays zero
3417 */
3418static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len,
3419 u32 off, u32 cnt)
3420{
3421 struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data;
3422
3423 if (cnt == 1)
3424 return 0;
3425 new_data = vzalloc(sizeof(struct bpf_insn_aux_data) * prog_len);
3426 if (!new_data)
3427 return -ENOMEM;
3428 memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off);
3429 memcpy(new_data + off + cnt - 1, old_data + off,
3430 sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
3431 env->insn_aux_data = new_data;
3432 vfree(old_data);
3433 return 0;
3434}
3435
3436static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
3437 const struct bpf_insn *patch, u32 len)
3438{
3439 struct bpf_prog *new_prog;
3440
3441 new_prog = bpf_patch_insn_single(env->prog, off, patch, len);
3442 if (!new_prog)
3443 return NULL;
3444 if (adjust_insn_aux_data(env, new_prog->len, off, len))
3445 return NULL;
3446 return new_prog;
3447}
3448
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003449/* convert load instructions that access fields of 'struct __sk_buff'
3450 * into sequence of instructions that access fields of 'struct sk_buff'
3451 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003452static int convert_ctx_accesses(struct bpf_verifier_env *env)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003453{
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003454 const struct bpf_verifier_ops *ops = env->prog->aux->ops;
Daniel Borkmannf96da092017-07-02 02:13:27 +02003455 int i, cnt, size, ctx_field_size, delta = 0;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003456 const int insn_cnt = env->prog->len;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003457 struct bpf_insn insn_buf[16], *insn;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003458 struct bpf_prog *new_prog;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003459 enum bpf_access_type type;
Daniel Borkmannf96da092017-07-02 02:13:27 +02003460 bool is_narrower_load;
3461 u32 target_size;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003462
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003463 if (ops->gen_prologue) {
3464 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
3465 env->prog);
3466 if (cnt >= ARRAY_SIZE(insn_buf)) {
3467 verbose("bpf verifier is misconfigured\n");
3468 return -EINVAL;
3469 } else if (cnt) {
Alexei Starovoitov80419022017-03-15 18:26:41 -07003470 new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003471 if (!new_prog)
3472 return -ENOMEM;
Alexei Starovoitov80419022017-03-15 18:26:41 -07003473
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003474 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003475 delta += cnt - 1;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003476 }
3477 }
3478
3479 if (!ops->convert_ctx_access)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003480 return 0;
3481
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003482 insn = env->prog->insnsi + delta;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003483
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003484 for (i = 0; i < insn_cnt; i++, insn++) {
Daniel Borkmann62c79892017-01-12 11:51:33 +01003485 if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) ||
3486 insn->code == (BPF_LDX | BPF_MEM | BPF_H) ||
3487 insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07003488 insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003489 type = BPF_READ;
Daniel Borkmann62c79892017-01-12 11:51:33 +01003490 else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) ||
3491 insn->code == (BPF_STX | BPF_MEM | BPF_H) ||
3492 insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07003493 insn->code == (BPF_STX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003494 type = BPF_WRITE;
3495 else
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003496 continue;
3497
Alexei Starovoitov80419022017-03-15 18:26:41 -07003498 if (env->insn_aux_data[i + delta].ptr_type != PTR_TO_CTX)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003499 continue;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003500
Yonghong Song31fd8582017-06-13 15:52:13 -07003501 ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size;
Daniel Borkmannf96da092017-07-02 02:13:27 +02003502 size = BPF_LDST_BYTES(insn);
Yonghong Song31fd8582017-06-13 15:52:13 -07003503
3504 /* If the read access is a narrower load of the field,
3505 * convert to a 4/8-byte load, to minimum program type specific
3506 * convert_ctx_access changes. If conversion is successful,
3507 * we will apply proper mask to the result.
3508 */
Daniel Borkmannf96da092017-07-02 02:13:27 +02003509 is_narrower_load = size < ctx_field_size;
Yonghong Song31fd8582017-06-13 15:52:13 -07003510 if (is_narrower_load) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02003511 u32 off = insn->off;
3512 u8 size_code;
Yonghong Song31fd8582017-06-13 15:52:13 -07003513
Daniel Borkmannf96da092017-07-02 02:13:27 +02003514 if (type == BPF_WRITE) {
3515 verbose("bpf verifier narrow ctx access misconfigured\n");
3516 return -EINVAL;
3517 }
3518
3519 size_code = BPF_H;
Yonghong Song31fd8582017-06-13 15:52:13 -07003520 if (ctx_field_size == 4)
3521 size_code = BPF_W;
3522 else if (ctx_field_size == 8)
3523 size_code = BPF_DW;
Daniel Borkmannf96da092017-07-02 02:13:27 +02003524
Yonghong Song31fd8582017-06-13 15:52:13 -07003525 insn->off = off & ~(ctx_field_size - 1);
3526 insn->code = BPF_LDX | BPF_MEM | size_code;
3527 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02003528
3529 target_size = 0;
3530 cnt = ops->convert_ctx_access(type, insn, insn_buf, env->prog,
3531 &target_size);
3532 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) ||
3533 (ctx_field_size && !target_size)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003534 verbose("bpf verifier is misconfigured\n");
3535 return -EINVAL;
3536 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02003537
3538 if (is_narrower_load && size < target_size) {
Yonghong Song31fd8582017-06-13 15:52:13 -07003539 if (ctx_field_size <= 4)
3540 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02003541 (1 << size * 8) - 1);
Yonghong Song31fd8582017-06-13 15:52:13 -07003542 else
3543 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02003544 (1 << size * 8) - 1);
Yonghong Song31fd8582017-06-13 15:52:13 -07003545 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003546
Alexei Starovoitov80419022017-03-15 18:26:41 -07003547 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003548 if (!new_prog)
3549 return -ENOMEM;
3550
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003551 delta += cnt - 1;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003552
3553 /* keep walking new program and skip insns we just inserted */
3554 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003555 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003556 }
3557
3558 return 0;
3559}
3560
Alexei Starovoitov79741b32017-03-15 18:26:40 -07003561/* fixup insn->imm field of bpf_call instructions
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07003562 * and inline eligible helpers as explicit sequence of BPF instructions
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07003563 *
3564 * this function is called after eBPF program passed verification
3565 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07003566static int fixup_bpf_calls(struct bpf_verifier_env *env)
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07003567{
Alexei Starovoitov79741b32017-03-15 18:26:40 -07003568 struct bpf_prog *prog = env->prog;
3569 struct bpf_insn *insn = prog->insnsi;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07003570 const struct bpf_func_proto *fn;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07003571 const int insn_cnt = prog->len;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07003572 struct bpf_insn insn_buf[16];
3573 struct bpf_prog *new_prog;
3574 struct bpf_map *map_ptr;
3575 int i, cnt, delta = 0;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07003576
Alexei Starovoitov79741b32017-03-15 18:26:40 -07003577 for (i = 0; i < insn_cnt; i++, insn++) {
3578 if (insn->code != (BPF_JMP | BPF_CALL))
3579 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07003580
Alexei Starovoitov79741b32017-03-15 18:26:40 -07003581 if (insn->imm == BPF_FUNC_get_route_realm)
3582 prog->dst_needed = 1;
3583 if (insn->imm == BPF_FUNC_get_prandom_u32)
3584 bpf_user_rnd_init_once();
Alexei Starovoitov79741b32017-03-15 18:26:40 -07003585 if (insn->imm == BPF_FUNC_tail_call) {
David S. Miller7b9f6da2017-04-20 10:35:33 -04003586 /* If we tail call into other programs, we
3587 * cannot make any assumptions since they can
3588 * be replaced dynamically during runtime in
3589 * the program array.
3590 */
3591 prog->cb_access = 1;
Alexei Starovoitov80a58d02017-05-30 13:31:30 -07003592 env->prog->aux->stack_depth = MAX_BPF_STACK;
David S. Miller7b9f6da2017-04-20 10:35:33 -04003593
Alexei Starovoitov79741b32017-03-15 18:26:40 -07003594 /* mark bpf_tail_call as different opcode to avoid
3595 * conditional branch in the interpeter for every normal
3596 * call and to prevent accidental JITing by JIT compiler
3597 * that doesn't support bpf_tail_call yet
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07003598 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07003599 insn->imm = 0;
Alexei Starovoitov71189fa2017-05-30 13:31:27 -07003600 insn->code = BPF_JMP | BPF_TAIL_CALL;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07003601 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07003602 }
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07003603
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07003604 if (ebpf_jit_enabled() && insn->imm == BPF_FUNC_map_lookup_elem) {
3605 map_ptr = env->insn_aux_data[i + delta].map_ptr;
Martin KaFai Laufad73a12017-03-22 10:00:32 -07003606 if (map_ptr == BPF_MAP_PTR_POISON ||
3607 !map_ptr->ops->map_gen_lookup)
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07003608 goto patch_call_imm;
3609
3610 cnt = map_ptr->ops->map_gen_lookup(map_ptr, insn_buf);
3611 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
3612 verbose("bpf verifier is misconfigured\n");
3613 return -EINVAL;
3614 }
3615
3616 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf,
3617 cnt);
3618 if (!new_prog)
3619 return -ENOMEM;
3620
3621 delta += cnt - 1;
3622
3623 /* keep walking new program and skip insns we just inserted */
3624 env->prog = prog = new_prog;
3625 insn = new_prog->insnsi + i + delta;
3626 continue;
3627 }
3628
3629patch_call_imm:
Alexei Starovoitov79741b32017-03-15 18:26:40 -07003630 fn = prog->aux->ops->get_func_proto(insn->imm);
3631 /* all functions that have prototype and verifier allowed
3632 * programs to call them, must be real in-kernel functions
3633 */
3634 if (!fn->func) {
3635 verbose("kernel subsystem misconfigured func %s#%d\n",
3636 func_id_name(insn->imm), insn->imm);
3637 return -EFAULT;
3638 }
3639 insn->imm = fn->func - __bpf_call_base;
3640 }
3641
3642 return 0;
3643}
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07003644
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003645static void free_states(struct bpf_verifier_env *env)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003646{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003647 struct bpf_verifier_state_list *sl, *sln;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003648 int i;
3649
3650 if (!env->explored_states)
3651 return;
3652
3653 for (i = 0; i < env->prog->len; i++) {
3654 sl = env->explored_states[i];
3655
3656 if (sl)
3657 while (sl != STATE_LIST_MARK) {
3658 sln = sl->next;
3659 kfree(sl);
3660 sl = sln;
3661 }
3662 }
3663
3664 kfree(env->explored_states);
3665}
3666
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003667int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003668{
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003669 char __user *log_ubuf = NULL;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003670 struct bpf_verifier_env *env;
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003671 int ret = -EINVAL;
3672
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003673 /* 'struct bpf_verifier_env' can be global, but since it's not small,
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003674 * allocate/free it every time bpf_check() is called
3675 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003676 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003677 if (!env)
3678 return -ENOMEM;
3679
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003680 env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
3681 (*prog)->len);
3682 ret = -ENOMEM;
3683 if (!env->insn_aux_data)
3684 goto err_free_env;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003685 env->prog = *prog;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003686
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003687 /* grab the mutex to protect few globals used by verifier */
3688 mutex_lock(&bpf_verifier_lock);
3689
3690 if (attr->log_level || attr->log_buf || attr->log_size) {
3691 /* user requested verbose verifier output
3692 * and supplied buffer to store the verification trace
3693 */
3694 log_level = attr->log_level;
3695 log_ubuf = (char __user *) (unsigned long) attr->log_buf;
3696 log_size = attr->log_size;
3697 log_len = 0;
3698
3699 ret = -EINVAL;
3700 /* log_* values have to be sane */
3701 if (log_size < 128 || log_size > UINT_MAX >> 8 ||
3702 log_level == 0 || log_ubuf == NULL)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003703 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003704
3705 ret = -ENOMEM;
3706 log_buf = vmalloc(log_size);
3707 if (!log_buf)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003708 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003709 } else {
3710 log_level = 0;
3711 }
Daniel Borkmann1ad2f582017-05-25 01:05:05 +02003712
3713 env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT);
3714 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
David S. Millere07b98d2017-05-10 11:38:07 -07003715 env->strict_alignment = true;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003716
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003717 ret = replace_map_fd_with_map_ptr(env);
3718 if (ret < 0)
3719 goto skip_full_check;
3720
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003721 env->explored_states = kcalloc(env->prog->len,
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003722 sizeof(struct bpf_verifier_state_list *),
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003723 GFP_USER);
3724 ret = -ENOMEM;
3725 if (!env->explored_states)
3726 goto skip_full_check;
3727
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003728 ret = check_cfg(env);
3729 if (ret < 0)
3730 goto skip_full_check;
3731
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003732 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
3733
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003734 ret = do_check(env);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003735
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003736skip_full_check:
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003737 while (pop_stack(env, NULL) >= 0);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003738 free_states(env);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003739
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003740 if (ret == 0)
3741 /* program is valid, convert *(u32*)(ctx + off) accesses */
3742 ret = convert_ctx_accesses(env);
3743
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07003744 if (ret == 0)
Alexei Starovoitov79741b32017-03-15 18:26:40 -07003745 ret = fixup_bpf_calls(env);
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07003746
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003747 if (log_level && log_len >= log_size - 1) {
3748 BUG_ON(log_len >= log_size);
3749 /* verifier log exceeded user supplied buffer */
3750 ret = -ENOSPC;
3751 /* fall through to return what was recorded */
3752 }
3753
3754 /* copy verifier log back to user space including trailing zero */
3755 if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) {
3756 ret = -EFAULT;
3757 goto free_log_buf;
3758 }
3759
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003760 if (ret == 0 && env->used_map_cnt) {
3761 /* if program passed verifier, update used_maps in bpf_prog_info */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003762 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
3763 sizeof(env->used_maps[0]),
3764 GFP_KERNEL);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003765
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003766 if (!env->prog->aux->used_maps) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003767 ret = -ENOMEM;
3768 goto free_log_buf;
3769 }
3770
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003771 memcpy(env->prog->aux->used_maps, env->used_maps,
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003772 sizeof(env->used_maps[0]) * env->used_map_cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003773 env->prog->aux->used_map_cnt = env->used_map_cnt;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003774
3775 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
3776 * bpf_ld_imm64 instructions
3777 */
3778 convert_pseudo_ld_imm64(env);
3779 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003780
3781free_log_buf:
3782 if (log_level)
3783 vfree(log_buf);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003784 if (!env->prog->aux->used_maps)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003785 /* if we didn't copy map pointers into bpf_prog_info, release
3786 * them now. Otherwise free_bpf_prog_info() will release them.
3787 */
3788 release_maps(env);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003789 *prog = env->prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003790err_unlock:
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003791 mutex_unlock(&bpf_verifier_lock);
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003792 vfree(env->insn_aux_data);
3793err_free_env:
3794 kfree(env);
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003795 return ret;
3796}
Jakub Kicinski13a27df2016-09-21 11:43:58 +01003797
3798int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops,
3799 void *priv)
3800{
3801 struct bpf_verifier_env *env;
3802 int ret;
3803
3804 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
3805 if (!env)
3806 return -ENOMEM;
3807
3808 env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
3809 prog->len);
3810 ret = -ENOMEM;
3811 if (!env->insn_aux_data)
3812 goto err_free_env;
3813 env->prog = prog;
3814 env->analyzer_ops = ops;
3815 env->analyzer_priv = priv;
3816
3817 /* grab the mutex to protect few globals used by verifier */
3818 mutex_lock(&bpf_verifier_lock);
3819
3820 log_level = 0;
Daniel Borkmann1ad2f582017-05-25 01:05:05 +02003821
David S. Millere07b98d2017-05-10 11:38:07 -07003822 env->strict_alignment = false;
Daniel Borkmann1ad2f582017-05-25 01:05:05 +02003823 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
3824 env->strict_alignment = true;
Jakub Kicinski13a27df2016-09-21 11:43:58 +01003825
3826 env->explored_states = kcalloc(env->prog->len,
3827 sizeof(struct bpf_verifier_state_list *),
3828 GFP_KERNEL);
3829 ret = -ENOMEM;
3830 if (!env->explored_states)
3831 goto skip_full_check;
3832
3833 ret = check_cfg(env);
3834 if (ret < 0)
3835 goto skip_full_check;
3836
3837 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
3838
3839 ret = do_check(env);
3840
3841skip_full_check:
3842 while (pop_stack(env, NULL) >= 0);
3843 free_states(env);
3844
3845 mutex_unlock(&bpf_verifier_lock);
3846 vfree(env->insn_aux_data);
3847err_free_env:
3848 kfree(env);
3849 return ret;
3850}
3851EXPORT_SYMBOL_GPL(bpf_analyzer);