blob: c428c9f85186fcfebefc98df34197b14d4b9e550 [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 Kicinski58e2af82016-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>
22
23/* bpf_check() is a static code analyzer that walks eBPF program
24 * instruction by instruction and updates register/stack state.
25 * All paths of conditional branches are analyzed until 'bpf_exit' insn.
26 *
27 * The first pass is depth-first-search to check that the program is a DAG.
28 * It rejects the following programs:
29 * - larger than BPF_MAXINSNS insns
30 * - if loop is present (detected via back-edge)
31 * - unreachable insns exist (shouldn't be a forest. program = one function)
32 * - out of bounds or malformed jumps
33 * The second pass is all possible path descent from the 1st insn.
34 * Since it's analyzing all pathes through the program, the length of the
35 * analysis is limited to 32k insn, which may be hit even if total number of
36 * insn is less then 4K, but there are too many branches that change stack/regs.
37 * Number of 'branches to be analyzed' is limited to 1k
38 *
39 * On entry to each instruction, each register has a type, and the instruction
40 * changes the types of the registers depending on instruction semantics.
41 * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is
42 * copied to R1.
43 *
44 * All registers are 64-bit.
45 * R0 - return register
46 * R1-R5 argument passing registers
47 * R6-R9 callee saved registers
48 * R10 - frame pointer read-only
49 *
50 * At the start of BPF program the register R1 contains a pointer to bpf_context
51 * and has type PTR_TO_CTX.
52 *
53 * Verifier tracks arithmetic operations on pointers in case:
54 * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
55 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20),
56 * 1st insn copies R10 (which has FRAME_PTR) type into R1
57 * and 2nd arithmetic instruction is pattern matched to recognize
58 * that it wants to construct a pointer to some element within stack.
59 * So after 2nd insn, the register R1 has type PTR_TO_STACK
60 * (and -20 constant is saved for further stack bounds checking).
61 * Meaning that this reg is a pointer to stack plus known immediate constant.
62 *
63 * Most of the time the registers have UNKNOWN_VALUE type, which
64 * means the register has some value, but it's not a valid pointer.
65 * (like pointer plus pointer becomes UNKNOWN_VALUE type)
66 *
67 * When verifier sees load or store instructions the type of base register
68 * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, FRAME_PTR. These are three pointer
69 * types recognized by check_mem_access() function.
70 *
71 * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value'
72 * and the range of [ptr, ptr + map's value_size) is accessible.
73 *
74 * registers used to pass values to function calls are checked against
75 * function argument constraints.
76 *
77 * ARG_PTR_TO_MAP_KEY is one of such argument constraints.
78 * It means that the register type passed to this function must be
79 * PTR_TO_STACK and it will be used inside the function as
80 * 'pointer to map element key'
81 *
82 * For example the argument constraints for bpf_map_lookup_elem():
83 * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
84 * .arg1_type = ARG_CONST_MAP_PTR,
85 * .arg2_type = ARG_PTR_TO_MAP_KEY,
86 *
87 * ret_type says that this function returns 'pointer to map elem value or null'
88 * function expects 1st argument to be a const pointer to 'struct bpf_map' and
89 * 2nd argument should be a pointer to stack, which will be used inside
90 * the helper function as a pointer to map element key.
91 *
92 * On the kernel side the helper function looks like:
93 * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
94 * {
95 * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
96 * void *key = (void *) (unsigned long) r2;
97 * void *value;
98 *
99 * here kernel can access 'key' and 'map' pointers safely, knowing that
100 * [key, key + map->key_size) bytes are valid and were initialized on
101 * the stack of eBPF program.
102 * }
103 *
104 * Corresponding eBPF program may look like:
105 * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR
106 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK
107 * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP
108 * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
109 * here verifier looks at prototype of map_lookup_elem() and sees:
110 * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok,
111 * Now verifier knows that this map has key of R1->map_ptr->key_size bytes
112 *
113 * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far,
114 * Now verifier checks that [R2, R2 + map's key_size) are within stack limits
115 * and were initialized prior to this call.
116 * If it's ok, then verifier allows this BPF_CALL insn and looks at
117 * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets
118 * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function
119 * returns ether pointer to map value or NULL.
120 *
121 * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off'
122 * insn, the register holding that pointer in the true branch changes state to
123 * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false
124 * branch. See check_cond_jmp_op().
125 *
126 * After the call R0 is set to return type of the function and registers R1-R5
127 * are set to NOT_INIT to indicate that they are no longer readable.
128 */
129
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700130/* verifier_state + insn_idx are pushed to stack when branch is encountered */
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100131struct bpf_verifier_stack_elem {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700132 /* verifer state is 'st'
133 * before processing instruction 'insn_idx'
134 * and after processing instruction 'prev_insn_idx'
135 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100136 struct bpf_verifier_state st;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700137 int insn_idx;
138 int prev_insn_idx;
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100139 struct bpf_verifier_stack_elem *next;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700140};
141
Daniel Borkmann07016152016-04-05 22:33:17 +0200142#define BPF_COMPLEXITY_LIMIT_INSNS 65536
143#define BPF_COMPLEXITY_LIMIT_STACK 1024
144
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200145struct bpf_call_arg_meta {
146 struct bpf_map *map_ptr;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200147 bool raw_mode;
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200148 bool pkt_access;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200149 int regno;
150 int access_size;
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200151};
152
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700153/* verbose verifier prints what it's seeing
154 * bpf_check() is called under lock, so no race to access these global vars
155 */
156static u32 log_level, log_size, log_len;
157static char *log_buf;
158
159static DEFINE_MUTEX(bpf_verifier_lock);
160
161/* log_level controls verbosity level of eBPF verifier.
162 * verbose() is used to dump the verification trace to the log, so the user
163 * can figure out what's wrong with the program
164 */
Daniel Borkmann1d056d92015-11-03 11:39:20 +0100165static __printf(1, 2) void verbose(const char *fmt, ...)
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700166{
167 va_list args;
168
169 if (log_level == 0 || log_len >= log_size - 1)
170 return;
171
172 va_start(args, fmt);
173 log_len += vscnprintf(log_buf + log_len, log_size - log_len, fmt, args);
174 va_end(args);
175}
176
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700177/* string representation of 'enum bpf_reg_type' */
178static const char * const reg_type_str[] = {
179 [NOT_INIT] = "?",
180 [UNKNOWN_VALUE] = "inv",
181 [PTR_TO_CTX] = "ctx",
182 [CONST_PTR_TO_MAP] = "map_ptr",
183 [PTR_TO_MAP_VALUE] = "map_value",
184 [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
Josef Bacik48461132016-09-28 10:54:32 -0400185 [PTR_TO_MAP_VALUE_ADJ] = "map_value_adj",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700186 [FRAME_PTR] = "fp",
187 [PTR_TO_STACK] = "fp",
188 [CONST_IMM] = "imm",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700189 [PTR_TO_PACKET] = "pkt",
190 [PTR_TO_PACKET_END] = "pkt_end",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700191};
192
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100193static void print_verifier_state(struct bpf_verifier_state *state)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700194{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100195 struct bpf_reg_state *reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700196 enum bpf_reg_type t;
197 int i;
198
199 for (i = 0; i < MAX_BPF_REG; i++) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700200 reg = &state->regs[i];
201 t = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700202 if (t == NOT_INIT)
203 continue;
204 verbose(" R%d=%s", i, reg_type_str[t]);
205 if (t == CONST_IMM || t == PTR_TO_STACK)
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700206 verbose("%lld", reg->imm);
207 else if (t == PTR_TO_PACKET)
208 verbose("(id=%d,off=%d,r=%d)",
209 reg->id, reg->off, reg->range);
210 else if (t == UNKNOWN_VALUE && reg->imm)
211 verbose("%lld", reg->imm);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700212 else if (t == CONST_PTR_TO_MAP || t == PTR_TO_MAP_VALUE ||
Josef Bacik48461132016-09-28 10:54:32 -0400213 t == PTR_TO_MAP_VALUE_OR_NULL ||
214 t == PTR_TO_MAP_VALUE_ADJ)
Thomas Graf14117072016-10-18 19:51:19 +0200215 verbose("(ks=%d,vs=%d,id=%u)",
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700216 reg->map_ptr->key_size,
Thomas Graf14117072016-10-18 19:51:19 +0200217 reg->map_ptr->value_size,
218 reg->id);
Josef Bacik48461132016-09-28 10:54:32 -0400219 if (reg->min_value != BPF_REGISTER_MIN_RANGE)
Josef Bacikf23cc642016-11-14 15:45:36 -0500220 verbose(",min_value=%lld",
221 (long long)reg->min_value);
Josef Bacik48461132016-09-28 10:54:32 -0400222 if (reg->max_value != BPF_REGISTER_MAX_RANGE)
223 verbose(",max_value=%llu",
224 (unsigned long long)reg->max_value);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700225 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700226 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700227 if (state->stack_slot_type[i] == STACK_SPILL)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700228 verbose(" fp%d=%s", -MAX_BPF_STACK + i,
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700229 reg_type_str[state->spilled_regs[i / BPF_REG_SIZE].type]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700230 }
231 verbose("\n");
232}
233
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700234static const char *const bpf_class_string[] = {
235 [BPF_LD] = "ld",
236 [BPF_LDX] = "ldx",
237 [BPF_ST] = "st",
238 [BPF_STX] = "stx",
239 [BPF_ALU] = "alu",
240 [BPF_JMP] = "jmp",
241 [BPF_RET] = "BUG",
242 [BPF_ALU64] = "alu64",
243};
244
Alexei Starovoitov687f0712015-09-08 13:40:01 -0700245static const char *const bpf_alu_string[16] = {
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700246 [BPF_ADD >> 4] = "+=",
247 [BPF_SUB >> 4] = "-=",
248 [BPF_MUL >> 4] = "*=",
249 [BPF_DIV >> 4] = "/=",
250 [BPF_OR >> 4] = "|=",
251 [BPF_AND >> 4] = "&=",
252 [BPF_LSH >> 4] = "<<=",
253 [BPF_RSH >> 4] = ">>=",
254 [BPF_NEG >> 4] = "neg",
255 [BPF_MOD >> 4] = "%=",
256 [BPF_XOR >> 4] = "^=",
257 [BPF_MOV >> 4] = "=",
258 [BPF_ARSH >> 4] = "s>>=",
259 [BPF_END >> 4] = "endian",
260};
261
262static const char *const bpf_ldst_string[] = {
263 [BPF_W >> 3] = "u32",
264 [BPF_H >> 3] = "u16",
265 [BPF_B >> 3] = "u8",
266 [BPF_DW >> 3] = "u64",
267};
268
Alexei Starovoitov687f0712015-09-08 13:40:01 -0700269static const char *const bpf_jmp_string[16] = {
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700270 [BPF_JA >> 4] = "jmp",
271 [BPF_JEQ >> 4] = "==",
272 [BPF_JGT >> 4] = ">",
273 [BPF_JGE >> 4] = ">=",
274 [BPF_JSET >> 4] = "&",
275 [BPF_JNE >> 4] = "!=",
276 [BPF_JSGT >> 4] = "s>",
277 [BPF_JSGE >> 4] = "s>=",
278 [BPF_CALL >> 4] = "call",
279 [BPF_EXIT >> 4] = "exit",
280};
281
282static void print_bpf_insn(struct bpf_insn *insn)
283{
284 u8 class = BPF_CLASS(insn->code);
285
286 if (class == BPF_ALU || class == BPF_ALU64) {
287 if (BPF_SRC(insn->code) == BPF_X)
288 verbose("(%02x) %sr%d %s %sr%d\n",
289 insn->code, class == BPF_ALU ? "(u32) " : "",
290 insn->dst_reg,
291 bpf_alu_string[BPF_OP(insn->code) >> 4],
292 class == BPF_ALU ? "(u32) " : "",
293 insn->src_reg);
294 else
295 verbose("(%02x) %sr%d %s %s%d\n",
296 insn->code, class == BPF_ALU ? "(u32) " : "",
297 insn->dst_reg,
298 bpf_alu_string[BPF_OP(insn->code) >> 4],
299 class == BPF_ALU ? "(u32) " : "",
300 insn->imm);
301 } else if (class == BPF_STX) {
302 if (BPF_MODE(insn->code) == BPF_MEM)
303 verbose("(%02x) *(%s *)(r%d %+d) = r%d\n",
304 insn->code,
305 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
306 insn->dst_reg,
307 insn->off, insn->src_reg);
308 else if (BPF_MODE(insn->code) == BPF_XADD)
309 verbose("(%02x) lock *(%s *)(r%d %+d) += r%d\n",
310 insn->code,
311 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
312 insn->dst_reg, insn->off,
313 insn->src_reg);
314 else
315 verbose("BUG_%02x\n", insn->code);
316 } else if (class == BPF_ST) {
317 if (BPF_MODE(insn->code) != BPF_MEM) {
318 verbose("BUG_st_%02x\n", insn->code);
319 return;
320 }
321 verbose("(%02x) *(%s *)(r%d %+d) = %d\n",
322 insn->code,
323 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
324 insn->dst_reg,
325 insn->off, insn->imm);
326 } else if (class == BPF_LDX) {
327 if (BPF_MODE(insn->code) != BPF_MEM) {
328 verbose("BUG_ldx_%02x\n", insn->code);
329 return;
330 }
331 verbose("(%02x) r%d = *(%s *)(r%d %+d)\n",
332 insn->code, insn->dst_reg,
333 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
334 insn->src_reg, insn->off);
335 } else if (class == BPF_LD) {
336 if (BPF_MODE(insn->code) == BPF_ABS) {
337 verbose("(%02x) r0 = *(%s *)skb[%d]\n",
338 insn->code,
339 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
340 insn->imm);
341 } else if (BPF_MODE(insn->code) == BPF_IND) {
342 verbose("(%02x) r0 = *(%s *)skb[r%d + %d]\n",
343 insn->code,
344 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
345 insn->src_reg, insn->imm);
346 } else if (BPF_MODE(insn->code) == BPF_IMM) {
347 verbose("(%02x) r%d = 0x%x\n",
348 insn->code, insn->dst_reg, insn->imm);
349 } else {
350 verbose("BUG_ld_%02x\n", insn->code);
351 return;
352 }
353 } else if (class == BPF_JMP) {
354 u8 opcode = BPF_OP(insn->code);
355
356 if (opcode == BPF_CALL) {
357 verbose("(%02x) call %d\n", insn->code, insn->imm);
358 } else if (insn->code == (BPF_JMP | BPF_JA)) {
359 verbose("(%02x) goto pc%+d\n",
360 insn->code, insn->off);
361 } else if (insn->code == (BPF_JMP | BPF_EXIT)) {
362 verbose("(%02x) exit\n", insn->code);
363 } else if (BPF_SRC(insn->code) == BPF_X) {
364 verbose("(%02x) if r%d %s r%d goto pc%+d\n",
365 insn->code, insn->dst_reg,
366 bpf_jmp_string[BPF_OP(insn->code) >> 4],
367 insn->src_reg, insn->off);
368 } else {
369 verbose("(%02x) if r%d %s 0x%x goto pc%+d\n",
370 insn->code, insn->dst_reg,
371 bpf_jmp_string[BPF_OP(insn->code) >> 4],
372 insn->imm, insn->off);
373 }
374 } else {
375 verbose("(%02x) %s\n", insn->code, bpf_class_string[class]);
376 }
377}
378
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100379static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700380{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100381 struct bpf_verifier_stack_elem *elem;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700382 int insn_idx;
383
384 if (env->head == NULL)
385 return -1;
386
387 memcpy(&env->cur_state, &env->head->st, sizeof(env->cur_state));
388 insn_idx = env->head->insn_idx;
389 if (prev_insn_idx)
390 *prev_insn_idx = env->head->prev_insn_idx;
391 elem = env->head->next;
392 kfree(env->head);
393 env->head = elem;
394 env->stack_size--;
395 return insn_idx;
396}
397
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100398static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,
399 int insn_idx, int prev_insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700400{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100401 struct bpf_verifier_stack_elem *elem;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700402
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100403 elem = kmalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700404 if (!elem)
405 goto err;
406
407 memcpy(&elem->st, &env->cur_state, sizeof(env->cur_state));
408 elem->insn_idx = insn_idx;
409 elem->prev_insn_idx = prev_insn_idx;
410 elem->next = env->head;
411 env->head = elem;
412 env->stack_size++;
Daniel Borkmann07016152016-04-05 22:33:17 +0200413 if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700414 verbose("BPF program is too complex\n");
415 goto err;
416 }
417 return &elem->st;
418err:
419 /* pop all elements and return */
420 while (pop_stack(env, NULL) >= 0);
421 return NULL;
422}
423
424#define CALLER_SAVED_REGS 6
425static const int caller_saved[CALLER_SAVED_REGS] = {
426 BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
427};
428
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100429static void init_reg_state(struct bpf_reg_state *regs)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700430{
431 int i;
432
433 for (i = 0; i < MAX_BPF_REG; i++) {
434 regs[i].type = NOT_INIT;
435 regs[i].imm = 0;
Josef Bacik48461132016-09-28 10:54:32 -0400436 regs[i].min_value = BPF_REGISTER_MIN_RANGE;
437 regs[i].max_value = BPF_REGISTER_MAX_RANGE;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700438 }
439
440 /* frame pointer */
441 regs[BPF_REG_FP].type = FRAME_PTR;
442
443 /* 1st arg to a function */
444 regs[BPF_REG_1].type = PTR_TO_CTX;
445}
446
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100447static void mark_reg_unknown_value(struct bpf_reg_state *regs, u32 regno)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700448{
449 BUG_ON(regno >= MAX_BPF_REG);
450 regs[regno].type = UNKNOWN_VALUE;
Thomas Graf14117072016-10-18 19:51:19 +0200451 regs[regno].id = 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700452 regs[regno].imm = 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700453}
454
Josef Bacik48461132016-09-28 10:54:32 -0400455static void reset_reg_range_values(struct bpf_reg_state *regs, u32 regno)
456{
457 regs[regno].min_value = BPF_REGISTER_MIN_RANGE;
458 regs[regno].max_value = BPF_REGISTER_MAX_RANGE;
459}
460
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700461enum reg_arg_type {
462 SRC_OP, /* register is used as source operand */
463 DST_OP, /* register is used as destination operand */
464 DST_OP_NO_MARK /* same as above, check only, don't mark */
465};
466
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100467static int check_reg_arg(struct bpf_reg_state *regs, u32 regno,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700468 enum reg_arg_type t)
469{
470 if (regno >= MAX_BPF_REG) {
471 verbose("R%d is invalid\n", regno);
472 return -EINVAL;
473 }
474
475 if (t == SRC_OP) {
476 /* check whether register used as source operand can be read */
477 if (regs[regno].type == NOT_INIT) {
478 verbose("R%d !read_ok\n", regno);
479 return -EACCES;
480 }
481 } else {
482 /* check whether register used as dest operand can be written to */
483 if (regno == BPF_REG_FP) {
484 verbose("frame pointer is read only\n");
485 return -EACCES;
486 }
487 if (t == DST_OP)
488 mark_reg_unknown_value(regs, regno);
489 }
490 return 0;
491}
492
493static int bpf_size_to_bytes(int bpf_size)
494{
495 if (bpf_size == BPF_W)
496 return 4;
497 else if (bpf_size == BPF_H)
498 return 2;
499 else if (bpf_size == BPF_B)
500 return 1;
501 else if (bpf_size == BPF_DW)
502 return 8;
503 else
504 return -EINVAL;
505}
506
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700507static bool is_spillable_regtype(enum bpf_reg_type type)
508{
509 switch (type) {
510 case PTR_TO_MAP_VALUE:
511 case PTR_TO_MAP_VALUE_OR_NULL:
512 case PTR_TO_STACK:
513 case PTR_TO_CTX:
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700514 case PTR_TO_PACKET:
515 case PTR_TO_PACKET_END:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700516 case FRAME_PTR:
517 case CONST_PTR_TO_MAP:
518 return true;
519 default:
520 return false;
521 }
522}
523
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700524/* check_stack_read/write functions track spill/fill of registers,
525 * stack boundary and alignment are checked in check_mem_access()
526 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100527static int check_stack_write(struct bpf_verifier_state *state, int off,
528 int size, int value_regno)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700529{
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700530 int i;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700531 /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
532 * so it's aligned access and [off, off + size) are within stack limits
533 */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700534
535 if (value_regno >= 0 &&
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700536 is_spillable_regtype(state->regs[value_regno].type)) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700537
538 /* register containing pointer is being spilled into stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700539 if (size != BPF_REG_SIZE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700540 verbose("invalid size of register spill\n");
541 return -EACCES;
542 }
543
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700544 /* save register state */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700545 state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] =
546 state->regs[value_regno];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700547
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700548 for (i = 0; i < BPF_REG_SIZE; i++)
549 state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_SPILL;
550 } else {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700551 /* regular write of data into stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700552 state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] =
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100553 (struct bpf_reg_state) {};
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700554
555 for (i = 0; i < size; i++)
556 state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_MISC;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700557 }
558 return 0;
559}
560
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100561static int check_stack_read(struct bpf_verifier_state *state, int off, int size,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700562 int value_regno)
563{
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700564 u8 *slot_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700565 int i;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700566
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700567 slot_type = &state->stack_slot_type[MAX_BPF_STACK + off];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700568
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700569 if (slot_type[0] == STACK_SPILL) {
570 if (size != BPF_REG_SIZE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700571 verbose("invalid size of register spill\n");
572 return -EACCES;
573 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700574 for (i = 1; i < BPF_REG_SIZE; i++) {
575 if (slot_type[i] != STACK_SPILL) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700576 verbose("corrupted spill memory\n");
577 return -EACCES;
578 }
579 }
580
581 if (value_regno >= 0)
582 /* restore register state from stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700583 state->regs[value_regno] =
584 state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700585 return 0;
586 } else {
587 for (i = 0; i < size; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700588 if (slot_type[i] != STACK_MISC) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700589 verbose("invalid read from stack off %d+%d size %d\n",
590 off, i, size);
591 return -EACCES;
592 }
593 }
594 if (value_regno >= 0)
595 /* have read misc data from the stack */
596 mark_reg_unknown_value(state->regs, value_regno);
597 return 0;
598 }
599}
600
601/* check read/write into map element returned by bpf_map_lookup_elem() */
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100602static int check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700603 int size)
604{
605 struct bpf_map *map = env->cur_state.regs[regno].map_ptr;
606
607 if (off < 0 || off + size > map->value_size) {
608 verbose("invalid access to map value, value_size=%d off=%d size=%d\n",
609 map->value_size, off, size);
610 return -EACCES;
611 }
612 return 0;
613}
614
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700615#define MAX_PACKET_OFF 0xffff
616
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100617static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200618 const struct bpf_call_arg_meta *meta)
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700619{
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200620 switch (env->prog->type) {
621 case BPF_PROG_TYPE_SCHED_CLS:
622 case BPF_PROG_TYPE_SCHED_ACT:
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700623 case BPF_PROG_TYPE_XDP:
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200624 if (meta)
625 return meta->pkt_access;
626
627 env->seen_direct_write = true;
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700628 return true;
629 default:
630 return false;
631 }
632}
633
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100634static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700635 int size)
636{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100637 struct bpf_reg_state *regs = env->cur_state.regs;
638 struct bpf_reg_state *reg = &regs[regno];
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700639
Alexei Starovoitovd91b28e2016-05-19 18:17:13 -0700640 off += reg->off;
Daniel Borkmannb399cf62016-09-20 00:26:12 +0200641 if (off < 0 || size <= 0 || off + size > reg->range) {
Alexei Starovoitovd91b28e2016-05-19 18:17:13 -0700642 verbose("invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n",
643 off, size, regno, reg->id, reg->off, reg->range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700644 return -EACCES;
645 }
646 return 0;
647}
648
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700649/* check access to 'struct bpf_context' fields */
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100650static int check_ctx_access(struct bpf_verifier_env *env, int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700651 enum bpf_access_type t, enum bpf_reg_type *reg_type)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700652{
Jakub Kicinski13a27df2016-09-21 11:43:58 +0100653 /* for analyzer ctx accesses are already validated and converted */
654 if (env->analyzer_ops)
655 return 0;
656
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700657 if (env->prog->aux->ops->is_valid_access &&
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700658 env->prog->aux->ops->is_valid_access(off, size, t, reg_type)) {
Alexei Starovoitov32bbe002016-04-06 18:43:28 -0700659 /* remember the offset of last byte accessed in ctx */
660 if (env->prog->aux->max_ctx_offset < off + size)
661 env->prog->aux->max_ctx_offset = off + size;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700662 return 0;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -0700663 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700664
665 verbose("invalid bpf_context access off=%d size=%d\n", off, size);
666 return -EACCES;
667}
668
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100669static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700670{
671 if (env->allow_ptr_leaks)
672 return false;
673
674 switch (env->cur_state.regs[regno].type) {
675 case UNKNOWN_VALUE:
676 case CONST_IMM:
677 return false;
678 default:
679 return true;
680 }
681}
682
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100683static int check_ptr_alignment(struct bpf_verifier_env *env,
684 struct bpf_reg_state *reg, int off, int size)
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700685{
Josef Bacik48461132016-09-28 10:54:32 -0400686 if (reg->type != PTR_TO_PACKET && reg->type != PTR_TO_MAP_VALUE_ADJ) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700687 if (off % size != 0) {
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100688 verbose("misaligned access off %d size %d\n",
689 off, size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700690 return -EACCES;
691 } else {
692 return 0;
693 }
694 }
695
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700696 if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
697 /* misaligned access to packet is ok on x86,arm,arm64 */
698 return 0;
699
700 if (reg->id && size != 1) {
701 verbose("Unknown packet alignment. Only byte-sized access allowed\n");
702 return -EACCES;
703 }
704
705 /* skb->data is NET_IP_ALIGN-ed */
Josef Bacik48461132016-09-28 10:54:32 -0400706 if (reg->type == PTR_TO_PACKET &&
707 (NET_IP_ALIGN + reg->off + off) % size != 0) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700708 verbose("misaligned packet access off %d+%d+%d size %d\n",
709 NET_IP_ALIGN, reg->off, off, size);
710 return -EACCES;
711 }
712 return 0;
713}
714
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700715/* check whether memory at (regno + off) is accessible for t = (read | write)
716 * if t==write, value_regno is a register which value is stored into memory
717 * if t==read, value_regno is a register which will receive the value from memory
718 * if t==write && value_regno==-1, some unknown value is stored into memory
719 * if t==read && value_regno==-1, don't care what we read from memory
720 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100721static int check_mem_access(struct bpf_verifier_env *env, u32 regno, int off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700722 int bpf_size, enum bpf_access_type t,
723 int value_regno)
724{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100725 struct bpf_verifier_state *state = &env->cur_state;
726 struct bpf_reg_state *reg = &state->regs[regno];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700727 int size, err = 0;
728
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700729 if (reg->type == PTR_TO_STACK)
730 off += reg->imm;
Alex Gartrell24b4d2a2015-07-23 14:24:40 -0700731
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700732 size = bpf_size_to_bytes(bpf_size);
733 if (size < 0)
734 return size;
735
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700736 err = check_ptr_alignment(env, reg, off, size);
737 if (err)
738 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700739
Josef Bacik48461132016-09-28 10:54:32 -0400740 if (reg->type == PTR_TO_MAP_VALUE ||
741 reg->type == PTR_TO_MAP_VALUE_ADJ) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700742 if (t == BPF_WRITE && value_regno >= 0 &&
743 is_pointer_value(env, value_regno)) {
744 verbose("R%d leaks addr into map\n", value_regno);
745 return -EACCES;
746 }
Josef Bacik48461132016-09-28 10:54:32 -0400747
748 /* If we adjusted the register to this map value at all then we
749 * need to change off and size to min_value and max_value
750 * respectively to make sure our theoretical access will be
751 * safe.
752 */
753 if (reg->type == PTR_TO_MAP_VALUE_ADJ) {
754 if (log_level)
755 print_verifier_state(state);
756 env->varlen_map_value_access = true;
757 /* The minimum value is only important with signed
758 * comparisons where we can't assume the floor of a
759 * value is 0. If we are using signed variables for our
760 * index'es we need to make sure that whatever we use
761 * will have a set floor within our range.
762 */
Josef Bacikf23cc642016-11-14 15:45:36 -0500763 if (reg->min_value < 0) {
Josef Bacik48461132016-09-28 10:54:32 -0400764 verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
765 regno);
766 return -EACCES;
767 }
768 err = check_map_access(env, regno, reg->min_value + off,
769 size);
770 if (err) {
771 verbose("R%d min value is outside of the array range\n",
772 regno);
773 return err;
774 }
775
776 /* If we haven't set a max value then we need to bail
777 * since we can't be sure we won't do bad things.
778 */
779 if (reg->max_value == BPF_REGISTER_MAX_RANGE) {
780 verbose("R%d unbounded memory access, make sure to bounds check any array access into a map\n",
781 regno);
782 return -EACCES;
783 }
784 off += reg->max_value;
785 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700786 err = check_map_access(env, regno, off, size);
787 if (!err && t == BPF_READ && value_regno >= 0)
788 mark_reg_unknown_value(state->regs, value_regno);
789
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700790 } else if (reg->type == PTR_TO_CTX) {
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700791 enum bpf_reg_type reg_type = UNKNOWN_VALUE;
792
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700793 if (t == BPF_WRITE && value_regno >= 0 &&
794 is_pointer_value(env, value_regno)) {
795 verbose("R%d leaks addr into ctx\n", value_regno);
796 return -EACCES;
797 }
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700798 err = check_ctx_access(env, off, size, t, &reg_type);
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700799 if (!err && t == BPF_READ && value_regno >= 0) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700800 mark_reg_unknown_value(state->regs, value_regno);
Mickaël Salaün19553512016-09-24 20:01:50 +0200801 /* note that reg.[id|off|range] == 0 */
802 state->regs[value_regno].type = reg_type;
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700803 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700804
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700805 } else if (reg->type == FRAME_PTR || reg->type == PTR_TO_STACK) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700806 if (off >= 0 || off < -MAX_BPF_STACK) {
807 verbose("invalid stack off=%d size=%d\n", off, size);
808 return -EACCES;
809 }
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700810 if (t == BPF_WRITE) {
811 if (!env->allow_ptr_leaks &&
812 state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL &&
813 size != BPF_REG_SIZE) {
814 verbose("attempt to corrupt spilled pointer on stack\n");
815 return -EACCES;
816 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700817 err = check_stack_write(state, off, size, value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700818 } else {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700819 err = check_stack_read(state, off, size, value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700820 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700821 } else if (state->regs[regno].type == PTR_TO_PACKET) {
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200822 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL)) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700823 verbose("cannot write into packet\n");
824 return -EACCES;
825 }
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700826 if (t == BPF_WRITE && value_regno >= 0 &&
827 is_pointer_value(env, value_regno)) {
828 verbose("R%d leaks addr into packet\n", value_regno);
829 return -EACCES;
830 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700831 err = check_packet_access(env, regno, off, size);
832 if (!err && t == BPF_READ && value_regno >= 0)
833 mark_reg_unknown_value(state->regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700834 } else {
835 verbose("R%d invalid mem access '%s'\n",
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700836 regno, reg_type_str[reg->type]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700837 return -EACCES;
838 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700839
840 if (!err && size <= 2 && value_regno >= 0 && env->allow_ptr_leaks &&
841 state->regs[value_regno].type == UNKNOWN_VALUE) {
842 /* 1 or 2 byte load zero-extends, determine the number of
843 * zero upper bits. Not doing it fo 4 byte load, since
844 * such values cannot be added to ptr_to_packet anyway.
845 */
846 state->regs[value_regno].imm = 64 - size * 8;
847 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700848 return err;
849}
850
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100851static int check_xadd(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700852{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100853 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700854 int err;
855
856 if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
857 insn->imm != 0) {
858 verbose("BPF_XADD uses reserved fields\n");
859 return -EINVAL;
860 }
861
862 /* check src1 operand */
863 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
864 if (err)
865 return err;
866
867 /* check src2 operand */
868 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
869 if (err)
870 return err;
871
872 /* check whether atomic_add can read the memory */
873 err = check_mem_access(env, insn->dst_reg, insn->off,
874 BPF_SIZE(insn->code), BPF_READ, -1);
875 if (err)
876 return err;
877
878 /* check whether atomic_add can write into the same memory */
879 return check_mem_access(env, insn->dst_reg, insn->off,
880 BPF_SIZE(insn->code), BPF_WRITE, -1);
881}
882
883/* when register 'regno' is passed into function that will read 'access_size'
884 * bytes from that pointer, make sure that it's within stack boundary
885 * and all elements of stack are initialized
886 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100887static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
Daniel Borkmann435faee12016-04-13 00:10:51 +0200888 int access_size, bool zero_size_allowed,
889 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700890{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100891 struct bpf_verifier_state *state = &env->cur_state;
892 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700893 int off, i;
894
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +0100895 if (regs[regno].type != PTR_TO_STACK) {
896 if (zero_size_allowed && access_size == 0 &&
897 regs[regno].type == CONST_IMM &&
898 regs[regno].imm == 0)
899 return 0;
900
901 verbose("R%d type=%s expected=%s\n", regno,
902 reg_type_str[regs[regno].type],
903 reg_type_str[PTR_TO_STACK]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700904 return -EACCES;
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +0100905 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700906
907 off = regs[regno].imm;
908 if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
909 access_size <= 0) {
910 verbose("invalid stack type R%d off=%d access_size=%d\n",
911 regno, off, access_size);
912 return -EACCES;
913 }
914
Daniel Borkmann435faee12016-04-13 00:10:51 +0200915 if (meta && meta->raw_mode) {
916 meta->access_size = access_size;
917 meta->regno = regno;
918 return 0;
919 }
920
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700921 for (i = 0; i < access_size; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700922 if (state->stack_slot_type[MAX_BPF_STACK + off + i] != STACK_MISC) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700923 verbose("invalid indirect read from stack off %d+%d size %d\n",
924 off, i, access_size);
925 return -EACCES;
926 }
927 }
928 return 0;
929}
930
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100931static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200932 enum bpf_arg_type arg_type,
933 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700934{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100935 struct bpf_reg_state *regs = env->cur_state.regs, *reg = &regs[regno];
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700936 enum bpf_reg_type expected_type, type = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700937 int err = 0;
938
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100939 if (arg_type == ARG_DONTCARE)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700940 return 0;
941
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700942 if (type == NOT_INIT) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700943 verbose("R%d !read_ok\n", regno);
944 return -EACCES;
945 }
946
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700947 if (arg_type == ARG_ANYTHING) {
948 if (is_pointer_value(env, regno)) {
949 verbose("R%d leaks addr into helper function\n", regno);
950 return -EACCES;
951 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100952 return 0;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700953 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100954
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200955 if (type == PTR_TO_PACKET && !may_access_direct_pkt_data(env, meta)) {
956 verbose("helper access to the packet is not allowed\n");
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700957 return -EACCES;
958 }
959
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +0100960 if (arg_type == ARG_PTR_TO_MAP_KEY ||
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700961 arg_type == ARG_PTR_TO_MAP_VALUE) {
962 expected_type = PTR_TO_STACK;
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700963 if (type != PTR_TO_PACKET && type != expected_type)
964 goto err_type;
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +0100965 } else if (arg_type == ARG_CONST_STACK_SIZE ||
966 arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700967 expected_type = CONST_IMM;
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700968 if (type != expected_type)
969 goto err_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700970 } else if (arg_type == ARG_CONST_MAP_PTR) {
971 expected_type = CONST_PTR_TO_MAP;
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700972 if (type != expected_type)
973 goto err_type;
Alexei Starovoitov608cd712015-03-26 19:53:57 -0700974 } else if (arg_type == ARG_PTR_TO_CTX) {
975 expected_type = PTR_TO_CTX;
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700976 if (type != expected_type)
977 goto err_type;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200978 } else if (arg_type == ARG_PTR_TO_STACK ||
979 arg_type == ARG_PTR_TO_RAW_STACK) {
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +0100980 expected_type = PTR_TO_STACK;
981 /* One exception here. In case function allows for NULL to be
982 * passed in as argument, it's a CONST_IMM type. Final test
983 * happens during stack boundary checking.
984 */
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700985 if (type == CONST_IMM && reg->imm == 0)
986 /* final test in check_stack_boundary() */;
987 else if (type != PTR_TO_PACKET && type != expected_type)
988 goto err_type;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200989 meta->raw_mode = arg_type == ARG_PTR_TO_RAW_STACK;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700990 } else {
991 verbose("unsupported arg_type %d\n", arg_type);
992 return -EFAULT;
993 }
994
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700995 if (arg_type == ARG_CONST_MAP_PTR) {
996 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200997 meta->map_ptr = reg->map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700998 } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
999 /* bpf_map_xxx(..., map_ptr, ..., key) call:
1000 * check that [key, key + map->key_size) are within
1001 * stack limits and initialized
1002 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001003 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001004 /* in function declaration map_ptr must come before
1005 * map_key, so that it's verified and known before
1006 * we have to check map_key here. Otherwise it means
1007 * that kernel subsystem misconfigured verifier
1008 */
1009 verbose("invalid map_ptr to access map->key\n");
1010 return -EACCES;
1011 }
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001012 if (type == PTR_TO_PACKET)
1013 err = check_packet_access(env, regno, 0,
1014 meta->map_ptr->key_size);
1015 else
1016 err = check_stack_boundary(env, regno,
1017 meta->map_ptr->key_size,
1018 false, NULL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001019 } else if (arg_type == ARG_PTR_TO_MAP_VALUE) {
1020 /* bpf_map_xxx(..., map_ptr, ..., value) call:
1021 * check [value, value + map->value_size) validity
1022 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001023 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001024 /* kernel subsystem misconfigured verifier */
1025 verbose("invalid map_ptr to access map->value\n");
1026 return -EACCES;
1027 }
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001028 if (type == PTR_TO_PACKET)
1029 err = check_packet_access(env, regno, 0,
1030 meta->map_ptr->value_size);
1031 else
1032 err = check_stack_boundary(env, regno,
1033 meta->map_ptr->value_size,
1034 false, NULL);
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001035 } else if (arg_type == ARG_CONST_STACK_SIZE ||
1036 arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) {
1037 bool zero_size_allowed = (arg_type == ARG_CONST_STACK_SIZE_OR_ZERO);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001038
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001039 /* bpf_xxx(..., buf, len) call will access 'len' bytes
1040 * from stack pointer 'buf'. Check it
1041 * note: regno == len, regno - 1 == buf
1042 */
1043 if (regno == 0) {
1044 /* kernel subsystem misconfigured verifier */
1045 verbose("ARG_CONST_STACK_SIZE cannot be first argument\n");
1046 return -EACCES;
1047 }
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001048 if (regs[regno - 1].type == PTR_TO_PACKET)
1049 err = check_packet_access(env, regno - 1, 0, reg->imm);
1050 else
1051 err = check_stack_boundary(env, regno - 1, reg->imm,
1052 zero_size_allowed, meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001053 }
1054
1055 return err;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001056err_type:
1057 verbose("R%d type=%s expected=%s\n", regno,
1058 reg_type_str[type], reg_type_str[expected_type]);
1059 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001060}
1061
Kaixu Xia35578d72015-08-06 07:02:35 +00001062static int check_map_func_compatibility(struct bpf_map *map, int func_id)
1063{
Kaixu Xia35578d72015-08-06 07:02:35 +00001064 if (!map)
1065 return 0;
1066
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001067 /* We need a two way check, first is from map perspective ... */
1068 switch (map->map_type) {
1069 case BPF_MAP_TYPE_PROG_ARRAY:
1070 if (func_id != BPF_FUNC_tail_call)
1071 goto error;
1072 break;
1073 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
1074 if (func_id != BPF_FUNC_perf_event_read &&
1075 func_id != BPF_FUNC_perf_event_output)
1076 goto error;
1077 break;
1078 case BPF_MAP_TYPE_STACK_TRACE:
1079 if (func_id != BPF_FUNC_get_stackid)
1080 goto error;
1081 break;
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -07001082 case BPF_MAP_TYPE_CGROUP_ARRAY:
David S. Miller60747ef2016-08-18 01:17:32 -04001083 if (func_id != BPF_FUNC_skb_under_cgroup &&
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001084 func_id != BPF_FUNC_current_task_under_cgroup)
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07001085 goto error;
1086 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001087 default:
1088 break;
1089 }
1090
1091 /* ... and second from the function itself. */
1092 switch (func_id) {
1093 case BPF_FUNC_tail_call:
1094 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1095 goto error;
1096 break;
1097 case BPF_FUNC_perf_event_read:
1098 case BPF_FUNC_perf_event_output:
1099 if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
1100 goto error;
1101 break;
1102 case BPF_FUNC_get_stackid:
1103 if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
1104 goto error;
1105 break;
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001106 case BPF_FUNC_current_task_under_cgroup:
Daniel Borkmann747ea552016-08-12 22:17:17 +02001107 case BPF_FUNC_skb_under_cgroup:
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07001108 if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
1109 goto error;
1110 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001111 default:
1112 break;
Kaixu Xia35578d72015-08-06 07:02:35 +00001113 }
1114
1115 return 0;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001116error:
1117 verbose("cannot pass map_type %d into func %d\n",
1118 map->map_type, func_id);
1119 return -EINVAL;
Kaixu Xia35578d72015-08-06 07:02:35 +00001120}
1121
Daniel Borkmann435faee12016-04-13 00:10:51 +02001122static int check_raw_mode(const struct bpf_func_proto *fn)
1123{
1124 int count = 0;
1125
1126 if (fn->arg1_type == ARG_PTR_TO_RAW_STACK)
1127 count++;
1128 if (fn->arg2_type == ARG_PTR_TO_RAW_STACK)
1129 count++;
1130 if (fn->arg3_type == ARG_PTR_TO_RAW_STACK)
1131 count++;
1132 if (fn->arg4_type == ARG_PTR_TO_RAW_STACK)
1133 count++;
1134 if (fn->arg5_type == ARG_PTR_TO_RAW_STACK)
1135 count++;
1136
1137 return count > 1 ? -EINVAL : 0;
1138}
1139
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001140static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001141{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001142 struct bpf_verifier_state *state = &env->cur_state;
1143 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001144 int i;
1145
1146 for (i = 0; i < MAX_BPF_REG; i++)
1147 if (regs[i].type == PTR_TO_PACKET ||
1148 regs[i].type == PTR_TO_PACKET_END)
1149 mark_reg_unknown_value(regs, i);
1150
1151 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
1152 if (state->stack_slot_type[i] != STACK_SPILL)
1153 continue;
1154 reg = &state->spilled_regs[i / BPF_REG_SIZE];
1155 if (reg->type != PTR_TO_PACKET &&
1156 reg->type != PTR_TO_PACKET_END)
1157 continue;
1158 reg->type = UNKNOWN_VALUE;
1159 reg->imm = 0;
1160 }
1161}
1162
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001163static int check_call(struct bpf_verifier_env *env, int func_id)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001164{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001165 struct bpf_verifier_state *state = &env->cur_state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001166 const struct bpf_func_proto *fn = NULL;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001167 struct bpf_reg_state *regs = state->regs;
1168 struct bpf_reg_state *reg;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001169 struct bpf_call_arg_meta meta;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001170 bool changes_data;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001171 int i, err;
1172
1173 /* find function prototype */
1174 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
1175 verbose("invalid func %d\n", func_id);
1176 return -EINVAL;
1177 }
1178
1179 if (env->prog->aux->ops->get_func_proto)
1180 fn = env->prog->aux->ops->get_func_proto(func_id);
1181
1182 if (!fn) {
1183 verbose("unknown func %d\n", func_id);
1184 return -EINVAL;
1185 }
1186
1187 /* eBPF programs must be GPL compatible to use GPL-ed functions */
Daniel Borkmann24701ec2015-03-01 12:31:47 +01001188 if (!env->prog->gpl_compatible && fn->gpl_only) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001189 verbose("cannot call GPL only function from proprietary program\n");
1190 return -EINVAL;
1191 }
1192
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001193 changes_data = bpf_helper_changes_skb_data(fn->func);
1194
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001195 memset(&meta, 0, sizeof(meta));
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001196 meta.pkt_access = fn->pkt_access;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001197
Daniel Borkmann435faee12016-04-13 00:10:51 +02001198 /* We only support one arg being in raw mode at the moment, which
1199 * is sufficient for the helper functions we have right now.
1200 */
1201 err = check_raw_mode(fn);
1202 if (err) {
1203 verbose("kernel subsystem misconfigured func %d\n", func_id);
1204 return err;
1205 }
1206
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001207 /* check args */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001208 err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001209 if (err)
1210 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001211 err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001212 if (err)
1213 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001214 err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001215 if (err)
1216 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001217 err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001218 if (err)
1219 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001220 err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001221 if (err)
1222 return err;
1223
Daniel Borkmann435faee12016-04-13 00:10:51 +02001224 /* Mark slots with STACK_MISC in case of raw mode, stack offset
1225 * is inferred from register state.
1226 */
1227 for (i = 0; i < meta.access_size; i++) {
1228 err = check_mem_access(env, meta.regno, i, BPF_B, BPF_WRITE, -1);
1229 if (err)
1230 return err;
1231 }
1232
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001233 /* reset caller saved regs */
1234 for (i = 0; i < CALLER_SAVED_REGS; i++) {
1235 reg = regs + caller_saved[i];
1236 reg->type = NOT_INIT;
1237 reg->imm = 0;
1238 }
1239
1240 /* update return register */
1241 if (fn->ret_type == RET_INTEGER) {
1242 regs[BPF_REG_0].type = UNKNOWN_VALUE;
1243 } else if (fn->ret_type == RET_VOID) {
1244 regs[BPF_REG_0].type = NOT_INIT;
1245 } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) {
1246 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
Josef Bacik48461132016-09-28 10:54:32 -04001247 regs[BPF_REG_0].max_value = regs[BPF_REG_0].min_value = 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001248 /* remember map_ptr, so that check_map_access()
1249 * can check 'value_size' boundary of memory access
1250 * to map element returned from bpf_map_lookup_elem()
1251 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001252 if (meta.map_ptr == NULL) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001253 verbose("kernel subsystem misconfigured verifier\n");
1254 return -EINVAL;
1255 }
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001256 regs[BPF_REG_0].map_ptr = meta.map_ptr;
Thomas Graf14117072016-10-18 19:51:19 +02001257 regs[BPF_REG_0].id = ++env->id_gen;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001258 } else {
1259 verbose("unknown return type %d of func %d\n",
1260 fn->ret_type, func_id);
1261 return -EINVAL;
1262 }
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -07001263
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001264 err = check_map_func_compatibility(meta.map_ptr, func_id);
Kaixu Xia35578d72015-08-06 07:02:35 +00001265 if (err)
1266 return err;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -07001267
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001268 if (changes_data)
1269 clear_all_pkt_pointers(env);
1270 return 0;
1271}
1272
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001273static int check_packet_ptr_add(struct bpf_verifier_env *env,
1274 struct bpf_insn *insn)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001275{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001276 struct bpf_reg_state *regs = env->cur_state.regs;
1277 struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
1278 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
1279 struct bpf_reg_state tmp_reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001280 s32 imm;
1281
1282 if (BPF_SRC(insn->code) == BPF_K) {
1283 /* pkt_ptr += imm */
1284 imm = insn->imm;
1285
1286add_imm:
1287 if (imm <= 0) {
1288 verbose("addition of negative constant to packet pointer is not allowed\n");
1289 return -EACCES;
1290 }
1291 if (imm >= MAX_PACKET_OFF ||
1292 imm + dst_reg->off >= MAX_PACKET_OFF) {
1293 verbose("constant %d is too large to add to packet pointer\n",
1294 imm);
1295 return -EACCES;
1296 }
1297 /* a constant was added to pkt_ptr.
1298 * Remember it while keeping the same 'id'
1299 */
1300 dst_reg->off += imm;
1301 } else {
Alexei Starovoitov1b9b69e2016-05-19 18:17:14 -07001302 if (src_reg->type == PTR_TO_PACKET) {
1303 /* R6=pkt(id=0,off=0,r=62) R7=imm22; r7 += r6 */
1304 tmp_reg = *dst_reg; /* save r7 state */
1305 *dst_reg = *src_reg; /* copy pkt_ptr state r6 into r7 */
1306 src_reg = &tmp_reg; /* pretend it's src_reg state */
1307 /* if the checks below reject it, the copy won't matter,
1308 * since we're rejecting the whole program. If all ok,
1309 * then imm22 state will be added to r7
1310 * and r7 will be pkt(id=0,off=22,r=62) while
1311 * r6 will stay as pkt(id=0,off=0,r=62)
1312 */
1313 }
1314
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001315 if (src_reg->type == CONST_IMM) {
1316 /* pkt_ptr += reg where reg is known constant */
1317 imm = src_reg->imm;
1318 goto add_imm;
1319 }
1320 /* disallow pkt_ptr += reg
1321 * if reg is not uknown_value with guaranteed zero upper bits
1322 * otherwise pkt_ptr may overflow and addition will become
1323 * subtraction which is not allowed
1324 */
1325 if (src_reg->type != UNKNOWN_VALUE) {
1326 verbose("cannot add '%s' to ptr_to_packet\n",
1327 reg_type_str[src_reg->type]);
1328 return -EACCES;
1329 }
1330 if (src_reg->imm < 48) {
1331 verbose("cannot add integer value with %lld upper zero bits to ptr_to_packet\n",
1332 src_reg->imm);
1333 return -EACCES;
1334 }
1335 /* dst_reg stays as pkt_ptr type and since some positive
1336 * integer value was added to the pointer, increment its 'id'
1337 */
Jakub Kicinski1f415a72016-08-02 16:12:14 +01001338 dst_reg->id = ++env->id_gen;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001339
1340 /* something was added to pkt_ptr, set range and off to zero */
1341 dst_reg->off = 0;
1342 dst_reg->range = 0;
1343 }
1344 return 0;
1345}
1346
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001347static int evaluate_reg_alu(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001348{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001349 struct bpf_reg_state *regs = env->cur_state.regs;
1350 struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001351 u8 opcode = BPF_OP(insn->code);
1352 s64 imm_log2;
1353
1354 /* for type == UNKNOWN_VALUE:
1355 * imm > 0 -> number of zero upper bits
1356 * imm == 0 -> don't track which is the same as all bits can be non-zero
1357 */
1358
1359 if (BPF_SRC(insn->code) == BPF_X) {
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001360 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001361
1362 if (src_reg->type == UNKNOWN_VALUE && src_reg->imm > 0 &&
1363 dst_reg->imm && opcode == BPF_ADD) {
1364 /* dreg += sreg
1365 * where both have zero upper bits. Adding them
1366 * can only result making one more bit non-zero
1367 * in the larger value.
1368 * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47)
1369 * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47)
1370 */
1371 dst_reg->imm = min(dst_reg->imm, src_reg->imm);
1372 dst_reg->imm--;
1373 return 0;
1374 }
1375 if (src_reg->type == CONST_IMM && src_reg->imm > 0 &&
1376 dst_reg->imm && opcode == BPF_ADD) {
1377 /* dreg += sreg
1378 * where dreg has zero upper bits and sreg is const.
1379 * Adding them can only result making one more bit
1380 * non-zero in the larger value.
1381 */
1382 imm_log2 = __ilog2_u64((long long)src_reg->imm);
1383 dst_reg->imm = min(dst_reg->imm, 63 - imm_log2);
1384 dst_reg->imm--;
1385 return 0;
1386 }
1387 /* all other cases non supported yet, just mark dst_reg */
1388 dst_reg->imm = 0;
1389 return 0;
1390 }
1391
1392 /* sign extend 32-bit imm into 64-bit to make sure that
1393 * negative values occupy bit 63. Note ilog2() would have
1394 * been incorrect, since sizeof(insn->imm) == 4
1395 */
1396 imm_log2 = __ilog2_u64((long long)insn->imm);
1397
1398 if (dst_reg->imm && opcode == BPF_LSH) {
1399 /* reg <<= imm
1400 * if reg was a result of 2 byte load, then its imm == 48
1401 * which means that upper 48 bits are zero and shifting this reg
1402 * left by 4 would mean that upper 44 bits are still zero
1403 */
1404 dst_reg->imm -= insn->imm;
1405 } else if (dst_reg->imm && opcode == BPF_MUL) {
1406 /* reg *= imm
1407 * if multiplying by 14 subtract 4
1408 * This is conservative calculation of upper zero bits.
1409 * It's not trying to special case insn->imm == 1 or 0 cases
1410 */
1411 dst_reg->imm -= imm_log2 + 1;
1412 } else if (opcode == BPF_AND) {
1413 /* reg &= imm */
1414 dst_reg->imm = 63 - imm_log2;
1415 } else if (dst_reg->imm && opcode == BPF_ADD) {
1416 /* reg += imm */
1417 dst_reg->imm = min(dst_reg->imm, 63 - imm_log2);
1418 dst_reg->imm--;
1419 } else if (opcode == BPF_RSH) {
1420 /* reg >>= imm
1421 * which means that after right shift, upper bits will be zero
1422 * note that verifier already checked that
1423 * 0 <= imm < 64 for shift insn
1424 */
1425 dst_reg->imm += insn->imm;
1426 if (unlikely(dst_reg->imm > 64))
1427 /* some dumb code did:
1428 * r2 = *(u32 *)mem;
1429 * r2 >>= 32;
1430 * and all bits are zero now */
1431 dst_reg->imm = 64;
1432 } else {
1433 /* all other alu ops, means that we don't know what will
1434 * happen to the value, mark it with unknown number of zero bits
1435 */
1436 dst_reg->imm = 0;
1437 }
1438
1439 if (dst_reg->imm < 0) {
1440 /* all 64 bits of the register can contain non-zero bits
1441 * and such value cannot be added to ptr_to_packet, since it
1442 * may overflow, mark it as unknown to avoid further eval
1443 */
1444 dst_reg->imm = 0;
1445 }
1446 return 0;
1447}
1448
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001449static int evaluate_reg_imm_alu(struct bpf_verifier_env *env,
1450 struct bpf_insn *insn)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001451{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001452 struct bpf_reg_state *regs = env->cur_state.regs;
1453 struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
1454 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001455 u8 opcode = BPF_OP(insn->code);
1456
1457 /* dst_reg->type == CONST_IMM here, simulate execution of 'add' insn.
1458 * Don't care about overflow or negative values, just add them
1459 */
1460 if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_K)
1461 dst_reg->imm += insn->imm;
1462 else if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_X &&
1463 src_reg->type == CONST_IMM)
1464 dst_reg->imm += src_reg->imm;
1465 else
1466 mark_reg_unknown_value(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001467 return 0;
1468}
1469
Josef Bacik48461132016-09-28 10:54:32 -04001470static void check_reg_overflow(struct bpf_reg_state *reg)
1471{
1472 if (reg->max_value > BPF_REGISTER_MAX_RANGE)
1473 reg->max_value = BPF_REGISTER_MAX_RANGE;
Josef Bacikf23cc642016-11-14 15:45:36 -05001474 if (reg->min_value < BPF_REGISTER_MIN_RANGE ||
1475 reg->min_value > BPF_REGISTER_MAX_RANGE)
Josef Bacik48461132016-09-28 10:54:32 -04001476 reg->min_value = BPF_REGISTER_MIN_RANGE;
1477}
1478
1479static void adjust_reg_min_max_vals(struct bpf_verifier_env *env,
1480 struct bpf_insn *insn)
1481{
1482 struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg;
Josef Bacikf23cc642016-11-14 15:45:36 -05001483 s64 min_val = BPF_REGISTER_MIN_RANGE;
1484 u64 max_val = BPF_REGISTER_MAX_RANGE;
Josef Bacik48461132016-09-28 10:54:32 -04001485 bool min_set = false, max_set = false;
1486 u8 opcode = BPF_OP(insn->code);
1487
1488 dst_reg = &regs[insn->dst_reg];
1489 if (BPF_SRC(insn->code) == BPF_X) {
1490 check_reg_overflow(&regs[insn->src_reg]);
1491 min_val = regs[insn->src_reg].min_value;
1492 max_val = regs[insn->src_reg].max_value;
1493
1494 /* If the source register is a random pointer then the
1495 * min_value/max_value values represent the range of the known
1496 * accesses into that value, not the actual min/max value of the
1497 * register itself. In this case we have to reset the reg range
1498 * values so we know it is not safe to look at.
1499 */
1500 if (regs[insn->src_reg].type != CONST_IMM &&
1501 regs[insn->src_reg].type != UNKNOWN_VALUE) {
1502 min_val = BPF_REGISTER_MIN_RANGE;
1503 max_val = BPF_REGISTER_MAX_RANGE;
1504 }
1505 } else if (insn->imm < BPF_REGISTER_MAX_RANGE &&
1506 (s64)insn->imm > BPF_REGISTER_MIN_RANGE) {
1507 min_val = max_val = insn->imm;
1508 min_set = max_set = true;
1509 }
1510
1511 /* We don't know anything about what was done to this register, mark it
1512 * as unknown.
1513 */
1514 if (min_val == BPF_REGISTER_MIN_RANGE &&
1515 max_val == BPF_REGISTER_MAX_RANGE) {
1516 reset_reg_range_values(regs, insn->dst_reg);
1517 return;
1518 }
1519
Josef Bacikf23cc642016-11-14 15:45:36 -05001520 /* If one of our values was at the end of our ranges then we can't just
1521 * do our normal operations to the register, we need to set the values
1522 * to the min/max since they are undefined.
1523 */
1524 if (min_val == BPF_REGISTER_MIN_RANGE)
1525 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
1526 if (max_val == BPF_REGISTER_MAX_RANGE)
1527 dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
1528
Josef Bacik48461132016-09-28 10:54:32 -04001529 switch (opcode) {
1530 case BPF_ADD:
Josef Bacikf23cc642016-11-14 15:45:36 -05001531 if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
1532 dst_reg->min_value += min_val;
1533 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1534 dst_reg->max_value += max_val;
Josef Bacik48461132016-09-28 10:54:32 -04001535 break;
1536 case BPF_SUB:
Josef Bacikf23cc642016-11-14 15:45:36 -05001537 if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
1538 dst_reg->min_value -= min_val;
1539 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1540 dst_reg->max_value -= max_val;
Josef Bacik48461132016-09-28 10:54:32 -04001541 break;
1542 case BPF_MUL:
Josef Bacikf23cc642016-11-14 15:45:36 -05001543 if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
1544 dst_reg->min_value *= min_val;
1545 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1546 dst_reg->max_value *= max_val;
Josef Bacik48461132016-09-28 10:54:32 -04001547 break;
1548 case BPF_AND:
Josef Bacikf23cc642016-11-14 15:45:36 -05001549 /* Disallow AND'ing of negative numbers, ain't nobody got time
1550 * for that. Otherwise the minimum is 0 and the max is the max
1551 * value we could AND against.
1552 */
1553 if (min_val < 0)
1554 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
1555 else
1556 dst_reg->min_value = 0;
Josef Bacik48461132016-09-28 10:54:32 -04001557 dst_reg->max_value = max_val;
1558 break;
1559 case BPF_LSH:
1560 /* Gotta have special overflow logic here, if we're shifting
1561 * more than MAX_RANGE then just assume we have an invalid
1562 * range.
1563 */
1564 if (min_val > ilog2(BPF_REGISTER_MAX_RANGE))
1565 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
Josef Bacikf23cc642016-11-14 15:45:36 -05001566 else if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
Josef Bacik48461132016-09-28 10:54:32 -04001567 dst_reg->min_value <<= min_val;
1568
1569 if (max_val > ilog2(BPF_REGISTER_MAX_RANGE))
1570 dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
Josef Bacikf23cc642016-11-14 15:45:36 -05001571 else if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
Josef Bacik48461132016-09-28 10:54:32 -04001572 dst_reg->max_value <<= max_val;
1573 break;
1574 case BPF_RSH:
Josef Bacikf23cc642016-11-14 15:45:36 -05001575 /* RSH by a negative number is undefined, and the BPF_RSH is an
1576 * unsigned shift, so make the appropriate casts.
Josef Bacik48461132016-09-28 10:54:32 -04001577 */
Josef Bacikf23cc642016-11-14 15:45:36 -05001578 if (min_val < 0 || dst_reg->min_value < 0)
1579 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
1580 else
1581 dst_reg->min_value =
1582 (u64)(dst_reg->min_value) >> min_val;
1583 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1584 dst_reg->max_value >>= max_val;
Josef Bacik48461132016-09-28 10:54:32 -04001585 break;
1586 default:
1587 reset_reg_range_values(regs, insn->dst_reg);
1588 break;
1589 }
1590
1591 check_reg_overflow(dst_reg);
1592}
1593
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001594/* check validity of 32-bit and 64-bit arithmetic operations */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001595static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001596{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001597 struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001598 u8 opcode = BPF_OP(insn->code);
1599 int err;
1600
1601 if (opcode == BPF_END || opcode == BPF_NEG) {
1602 if (opcode == BPF_NEG) {
1603 if (BPF_SRC(insn->code) != 0 ||
1604 insn->src_reg != BPF_REG_0 ||
1605 insn->off != 0 || insn->imm != 0) {
1606 verbose("BPF_NEG uses reserved fields\n");
1607 return -EINVAL;
1608 }
1609 } else {
1610 if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
1611 (insn->imm != 16 && insn->imm != 32 && insn->imm != 64)) {
1612 verbose("BPF_END uses reserved fields\n");
1613 return -EINVAL;
1614 }
1615 }
1616
1617 /* check src operand */
1618 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
1619 if (err)
1620 return err;
1621
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001622 if (is_pointer_value(env, insn->dst_reg)) {
1623 verbose("R%d pointer arithmetic prohibited\n",
1624 insn->dst_reg);
1625 return -EACCES;
1626 }
1627
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001628 /* check dest operand */
1629 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
1630 if (err)
1631 return err;
1632
1633 } else if (opcode == BPF_MOV) {
1634
1635 if (BPF_SRC(insn->code) == BPF_X) {
1636 if (insn->imm != 0 || insn->off != 0) {
1637 verbose("BPF_MOV uses reserved fields\n");
1638 return -EINVAL;
1639 }
1640
1641 /* check src operand */
1642 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1643 if (err)
1644 return err;
1645 } else {
1646 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
1647 verbose("BPF_MOV uses reserved fields\n");
1648 return -EINVAL;
1649 }
1650 }
1651
1652 /* check dest operand */
1653 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
1654 if (err)
1655 return err;
1656
Josef Bacik48461132016-09-28 10:54:32 -04001657 /* we are setting our register to something new, we need to
1658 * reset its range values.
1659 */
1660 reset_reg_range_values(regs, insn->dst_reg);
1661
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001662 if (BPF_SRC(insn->code) == BPF_X) {
1663 if (BPF_CLASS(insn->code) == BPF_ALU64) {
1664 /* case: R1 = R2
1665 * copy register state to dest reg
1666 */
1667 regs[insn->dst_reg] = regs[insn->src_reg];
1668 } else {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001669 if (is_pointer_value(env, insn->src_reg)) {
1670 verbose("R%d partial copy of pointer\n",
1671 insn->src_reg);
1672 return -EACCES;
1673 }
Thomas Graf14117072016-10-18 19:51:19 +02001674 mark_reg_unknown_value(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001675 }
1676 } else {
1677 /* case: R = imm
1678 * remember the value we stored into this reg
1679 */
1680 regs[insn->dst_reg].type = CONST_IMM;
1681 regs[insn->dst_reg].imm = insn->imm;
Josef Bacik48461132016-09-28 10:54:32 -04001682 regs[insn->dst_reg].max_value = insn->imm;
1683 regs[insn->dst_reg].min_value = insn->imm;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001684 }
1685
1686 } else if (opcode > BPF_END) {
1687 verbose("invalid BPF_ALU opcode %x\n", opcode);
1688 return -EINVAL;
1689
1690 } else { /* all other ALU ops: and, sub, xor, add, ... */
1691
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001692 if (BPF_SRC(insn->code) == BPF_X) {
1693 if (insn->imm != 0 || insn->off != 0) {
1694 verbose("BPF_ALU uses reserved fields\n");
1695 return -EINVAL;
1696 }
1697 /* check src1 operand */
1698 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1699 if (err)
1700 return err;
1701 } else {
1702 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
1703 verbose("BPF_ALU uses reserved fields\n");
1704 return -EINVAL;
1705 }
1706 }
1707
1708 /* check src2 operand */
1709 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
1710 if (err)
1711 return err;
1712
1713 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
1714 BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
1715 verbose("div by zero\n");
1716 return -EINVAL;
1717 }
1718
Rabin Vincent229394e2016-01-12 20:17:08 +01001719 if ((opcode == BPF_LSH || opcode == BPF_RSH ||
1720 opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
1721 int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
1722
1723 if (insn->imm < 0 || insn->imm >= size) {
1724 verbose("invalid shift %d\n", insn->imm);
1725 return -EINVAL;
1726 }
1727 }
1728
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07001729 /* check dest operand */
1730 err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
1731 if (err)
1732 return err;
1733
1734 dst_reg = &regs[insn->dst_reg];
1735
Josef Bacik48461132016-09-28 10:54:32 -04001736 /* first we want to adjust our ranges. */
1737 adjust_reg_min_max_vals(env, insn);
1738
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001739 /* pattern match 'bpf_add Rx, imm' instruction */
1740 if (opcode == BPF_ADD && BPF_CLASS(insn->code) == BPF_ALU64 &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07001741 dst_reg->type == FRAME_PTR && BPF_SRC(insn->code) == BPF_K) {
1742 dst_reg->type = PTR_TO_STACK;
1743 dst_reg->imm = insn->imm;
1744 return 0;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001745 } else if (opcode == BPF_ADD &&
1746 BPF_CLASS(insn->code) == BPF_ALU64 &&
Alexei Starovoitov1b9b69e2016-05-19 18:17:14 -07001747 (dst_reg->type == PTR_TO_PACKET ||
1748 (BPF_SRC(insn->code) == BPF_X &&
1749 regs[insn->src_reg].type == PTR_TO_PACKET))) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001750 /* ptr_to_packet += K|X */
1751 return check_packet_ptr_add(env, insn);
1752 } else if (BPF_CLASS(insn->code) == BPF_ALU64 &&
1753 dst_reg->type == UNKNOWN_VALUE &&
1754 env->allow_ptr_leaks) {
1755 /* unknown += K|X */
1756 return evaluate_reg_alu(env, insn);
1757 } else if (BPF_CLASS(insn->code) == BPF_ALU64 &&
1758 dst_reg->type == CONST_IMM &&
1759 env->allow_ptr_leaks) {
1760 /* reg_imm += K|X */
1761 return evaluate_reg_imm_alu(env, insn);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001762 } else if (is_pointer_value(env, insn->dst_reg)) {
1763 verbose("R%d pointer arithmetic prohibited\n",
1764 insn->dst_reg);
1765 return -EACCES;
1766 } else if (BPF_SRC(insn->code) == BPF_X &&
1767 is_pointer_value(env, insn->src_reg)) {
1768 verbose("R%d pointer arithmetic prohibited\n",
1769 insn->src_reg);
1770 return -EACCES;
1771 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001772
Josef Bacik48461132016-09-28 10:54:32 -04001773 /* If we did pointer math on a map value then just set it to our
1774 * PTR_TO_MAP_VALUE_ADJ type so we can deal with any stores or
1775 * loads to this register appropriately, otherwise just mark the
1776 * register as unknown.
1777 */
1778 if (env->allow_ptr_leaks &&
1779 (dst_reg->type == PTR_TO_MAP_VALUE ||
1780 dst_reg->type == PTR_TO_MAP_VALUE_ADJ))
1781 dst_reg->type = PTR_TO_MAP_VALUE_ADJ;
1782 else
1783 mark_reg_unknown_value(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001784 }
1785
1786 return 0;
1787}
1788
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001789static void find_good_pkt_pointers(struct bpf_verifier_state *state,
1790 struct bpf_reg_state *dst_reg)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001791{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001792 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001793 int i;
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02001794
1795 /* LLVM can generate two kind of checks:
1796 *
1797 * Type 1:
1798 *
1799 * r2 = r3;
1800 * r2 += 8;
1801 * if (r2 > pkt_end) goto <handle exception>
1802 * <access okay>
1803 *
1804 * Where:
1805 * r2 == dst_reg, pkt_end == src_reg
1806 * r2=pkt(id=n,off=8,r=0)
1807 * r3=pkt(id=n,off=0,r=0)
1808 *
1809 * Type 2:
1810 *
1811 * r2 = r3;
1812 * r2 += 8;
1813 * if (pkt_end >= r2) goto <access okay>
1814 * <handle exception>
1815 *
1816 * Where:
1817 * pkt_end == dst_reg, r2 == src_reg
1818 * r2=pkt(id=n,off=8,r=0)
1819 * r3=pkt(id=n,off=0,r=0)
1820 *
1821 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
1822 * so that range of bytes [r3, r3 + 8) is safe to access.
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001823 */
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02001824
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001825 for (i = 0; i < MAX_BPF_REG; i++)
1826 if (regs[i].type == PTR_TO_PACKET && regs[i].id == dst_reg->id)
1827 regs[i].range = dst_reg->off;
1828
1829 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
1830 if (state->stack_slot_type[i] != STACK_SPILL)
1831 continue;
1832 reg = &state->spilled_regs[i / BPF_REG_SIZE];
1833 if (reg->type == PTR_TO_PACKET && reg->id == dst_reg->id)
1834 reg->range = dst_reg->off;
1835 }
1836}
1837
Josef Bacik48461132016-09-28 10:54:32 -04001838/* Adjusts the register min/max values in the case that the dst_reg is the
1839 * variable register that we are working on, and src_reg is a constant or we're
1840 * simply doing a BPF_K check.
1841 */
1842static void reg_set_min_max(struct bpf_reg_state *true_reg,
1843 struct bpf_reg_state *false_reg, u64 val,
1844 u8 opcode)
1845{
1846 switch (opcode) {
1847 case BPF_JEQ:
1848 /* If this is false then we know nothing Jon Snow, but if it is
1849 * true then we know for sure.
1850 */
1851 true_reg->max_value = true_reg->min_value = val;
1852 break;
1853 case BPF_JNE:
1854 /* If this is true we know nothing Jon Snow, but if it is false
1855 * we know the value for sure;
1856 */
1857 false_reg->max_value = false_reg->min_value = val;
1858 break;
1859 case BPF_JGT:
1860 /* Unsigned comparison, the minimum value is 0. */
1861 false_reg->min_value = 0;
1862 case BPF_JSGT:
1863 /* If this is false then we know the maximum val is val,
1864 * otherwise we know the min val is val+1.
1865 */
1866 false_reg->max_value = val;
1867 true_reg->min_value = val + 1;
1868 break;
1869 case BPF_JGE:
1870 /* Unsigned comparison, the minimum value is 0. */
1871 false_reg->min_value = 0;
1872 case BPF_JSGE:
1873 /* If this is false then we know the maximum value is val - 1,
1874 * otherwise we know the mimimum value is val.
1875 */
1876 false_reg->max_value = val - 1;
1877 true_reg->min_value = val;
1878 break;
1879 default:
1880 break;
1881 }
1882
1883 check_reg_overflow(false_reg);
1884 check_reg_overflow(true_reg);
1885}
1886
1887/* Same as above, but for the case that dst_reg is a CONST_IMM reg and src_reg
1888 * is the variable reg.
1889 */
1890static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
1891 struct bpf_reg_state *false_reg, u64 val,
1892 u8 opcode)
1893{
1894 switch (opcode) {
1895 case BPF_JEQ:
1896 /* If this is false then we know nothing Jon Snow, but if it is
1897 * true then we know for sure.
1898 */
1899 true_reg->max_value = true_reg->min_value = val;
1900 break;
1901 case BPF_JNE:
1902 /* If this is true we know nothing Jon Snow, but if it is false
1903 * we know the value for sure;
1904 */
1905 false_reg->max_value = false_reg->min_value = val;
1906 break;
1907 case BPF_JGT:
1908 /* Unsigned comparison, the minimum value is 0. */
1909 true_reg->min_value = 0;
1910 case BPF_JSGT:
1911 /*
1912 * If this is false, then the val is <= the register, if it is
1913 * true the register <= to the val.
1914 */
1915 false_reg->min_value = val;
1916 true_reg->max_value = val - 1;
1917 break;
1918 case BPF_JGE:
1919 /* Unsigned comparison, the minimum value is 0. */
1920 true_reg->min_value = 0;
1921 case BPF_JSGE:
1922 /* If this is false then constant < register, if it is true then
1923 * the register < constant.
1924 */
1925 false_reg->min_value = val + 1;
1926 true_reg->max_value = val;
1927 break;
1928 default:
1929 break;
1930 }
1931
1932 check_reg_overflow(false_reg);
1933 check_reg_overflow(true_reg);
1934}
1935
Thomas Graf14117072016-10-18 19:51:19 +02001936static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id,
1937 enum bpf_reg_type type)
1938{
1939 struct bpf_reg_state *reg = &regs[regno];
1940
1941 if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) {
1942 reg->type = type;
1943 if (type == UNKNOWN_VALUE)
1944 mark_reg_unknown_value(regs, regno);
1945 }
1946}
1947
1948/* The logic is similar to find_good_pkt_pointers(), both could eventually
1949 * be folded together at some point.
1950 */
1951static void mark_map_regs(struct bpf_verifier_state *state, u32 regno,
1952 enum bpf_reg_type type)
1953{
1954 struct bpf_reg_state *regs = state->regs;
1955 int i;
1956
1957 for (i = 0; i < MAX_BPF_REG; i++)
1958 mark_map_reg(regs, i, regs[regno].id, type);
1959
1960 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
1961 if (state->stack_slot_type[i] != STACK_SPILL)
1962 continue;
1963 mark_map_reg(state->spilled_regs, i / BPF_REG_SIZE,
1964 regs[regno].id, type);
1965 }
1966}
1967
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001968static int check_cond_jmp_op(struct bpf_verifier_env *env,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001969 struct bpf_insn *insn, int *insn_idx)
1970{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001971 struct bpf_verifier_state *other_branch, *this_branch = &env->cur_state;
1972 struct bpf_reg_state *regs = this_branch->regs, *dst_reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001973 u8 opcode = BPF_OP(insn->code);
1974 int err;
1975
1976 if (opcode > BPF_EXIT) {
1977 verbose("invalid BPF_JMP opcode %x\n", opcode);
1978 return -EINVAL;
1979 }
1980
1981 if (BPF_SRC(insn->code) == BPF_X) {
1982 if (insn->imm != 0) {
1983 verbose("BPF_JMP uses reserved fields\n");
1984 return -EINVAL;
1985 }
1986
1987 /* check src1 operand */
1988 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1989 if (err)
1990 return err;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001991
1992 if (is_pointer_value(env, insn->src_reg)) {
1993 verbose("R%d pointer comparison prohibited\n",
1994 insn->src_reg);
1995 return -EACCES;
1996 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001997 } else {
1998 if (insn->src_reg != BPF_REG_0) {
1999 verbose("BPF_JMP uses reserved fields\n");
2000 return -EINVAL;
2001 }
2002 }
2003
2004 /* check src2 operand */
2005 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
2006 if (err)
2007 return err;
2008
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002009 dst_reg = &regs[insn->dst_reg];
2010
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002011 /* detect if R == 0 where R was initialized to zero earlier */
2012 if (BPF_SRC(insn->code) == BPF_K &&
2013 (opcode == BPF_JEQ || opcode == BPF_JNE) &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002014 dst_reg->type == CONST_IMM && dst_reg->imm == insn->imm) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002015 if (opcode == BPF_JEQ) {
2016 /* if (imm == imm) goto pc+off;
2017 * only follow the goto, ignore fall-through
2018 */
2019 *insn_idx += insn->off;
2020 return 0;
2021 } else {
2022 /* if (imm != imm) goto pc+off;
2023 * only follow fall-through branch, since
2024 * that's where the program will go
2025 */
2026 return 0;
2027 }
2028 }
2029
2030 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx);
2031 if (!other_branch)
2032 return -EFAULT;
2033
Josef Bacik48461132016-09-28 10:54:32 -04002034 /* detect if we are comparing against a constant value so we can adjust
2035 * our min/max values for our dst register.
2036 */
2037 if (BPF_SRC(insn->code) == BPF_X) {
2038 if (regs[insn->src_reg].type == CONST_IMM)
2039 reg_set_min_max(&other_branch->regs[insn->dst_reg],
2040 dst_reg, regs[insn->src_reg].imm,
2041 opcode);
2042 else if (dst_reg->type == CONST_IMM)
2043 reg_set_min_max_inv(&other_branch->regs[insn->src_reg],
2044 &regs[insn->src_reg], dst_reg->imm,
2045 opcode);
2046 } else {
2047 reg_set_min_max(&other_branch->regs[insn->dst_reg],
2048 dst_reg, insn->imm, opcode);
2049 }
2050
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002051 /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002052 if (BPF_SRC(insn->code) == BPF_K &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002053 insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
2054 dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
Thomas Graf14117072016-10-18 19:51:19 +02002055 /* Mark all identical map registers in each branch as either
2056 * safe or unknown depending R == 0 or R != 0 conditional.
2057 */
2058 mark_map_regs(this_branch, insn->dst_reg,
2059 opcode == BPF_JEQ ? PTR_TO_MAP_VALUE : UNKNOWN_VALUE);
2060 mark_map_regs(other_branch, insn->dst_reg,
2061 opcode == BPF_JEQ ? UNKNOWN_VALUE : PTR_TO_MAP_VALUE);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002062 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT &&
2063 dst_reg->type == PTR_TO_PACKET &&
2064 regs[insn->src_reg].type == PTR_TO_PACKET_END) {
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002065 find_good_pkt_pointers(this_branch, dst_reg);
2066 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGE &&
2067 dst_reg->type == PTR_TO_PACKET_END &&
2068 regs[insn->src_reg].type == PTR_TO_PACKET) {
2069 find_good_pkt_pointers(other_branch, &regs[insn->src_reg]);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002070 } else if (is_pointer_value(env, insn->dst_reg)) {
2071 verbose("R%d pointer comparison prohibited\n", insn->dst_reg);
2072 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002073 }
2074 if (log_level)
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002075 print_verifier_state(this_branch);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002076 return 0;
2077}
2078
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002079/* return the map pointer stored inside BPF_LD_IMM64 instruction */
2080static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
2081{
2082 u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
2083
2084 return (struct bpf_map *) (unsigned long) imm64;
2085}
2086
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002087/* verify BPF_LD_IMM64 instruction */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002088static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002089{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002090 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002091 int err;
2092
2093 if (BPF_SIZE(insn->code) != BPF_DW) {
2094 verbose("invalid BPF_LD_IMM insn\n");
2095 return -EINVAL;
2096 }
2097 if (insn->off != 0) {
2098 verbose("BPF_LD_IMM64 uses reserved fields\n");
2099 return -EINVAL;
2100 }
2101
2102 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
2103 if (err)
2104 return err;
2105
Jakub Kicinski6b173872016-09-21 11:43:59 +01002106 if (insn->src_reg == 0) {
2107 /* generic move 64-bit immediate into a register,
2108 * only analyzer needs to collect the ld_imm value.
2109 */
2110 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
2111
2112 if (!env->analyzer_ops)
2113 return 0;
2114
2115 regs[insn->dst_reg].type = CONST_IMM;
2116 regs[insn->dst_reg].imm = imm;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002117 return 0;
Jakub Kicinski6b173872016-09-21 11:43:59 +01002118 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002119
2120 /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
2121 BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
2122
2123 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
2124 regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
2125 return 0;
2126}
2127
Daniel Borkmann96be4322015-03-01 12:31:46 +01002128static bool may_access_skb(enum bpf_prog_type type)
2129{
2130 switch (type) {
2131 case BPF_PROG_TYPE_SOCKET_FILTER:
2132 case BPF_PROG_TYPE_SCHED_CLS:
Daniel Borkmann94caee82015-03-20 15:11:11 +01002133 case BPF_PROG_TYPE_SCHED_ACT:
Daniel Borkmann96be4322015-03-01 12:31:46 +01002134 return true;
2135 default:
2136 return false;
2137 }
2138}
2139
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002140/* verify safety of LD_ABS|LD_IND instructions:
2141 * - they can only appear in the programs where ctx == skb
2142 * - since they are wrappers of function calls, they scratch R1-R5 registers,
2143 * preserve R6-R9, and store return value into R0
2144 *
2145 * Implicit input:
2146 * ctx == skb == R6 == CTX
2147 *
2148 * Explicit input:
2149 * SRC == any register
2150 * IMM == 32-bit immediate
2151 *
2152 * Output:
2153 * R0 - 8/16/32-bit skb data converted to cpu endianness
2154 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002155static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002156{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002157 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002158 u8 mode = BPF_MODE(insn->code);
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002159 struct bpf_reg_state *reg;
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002160 int i, err;
2161
Daniel Borkmann24701ec2015-03-01 12:31:47 +01002162 if (!may_access_skb(env->prog->type)) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002163 verbose("BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002164 return -EINVAL;
2165 }
2166
2167 if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
Alexei Starovoitovd82bccc2016-04-12 10:26:19 -07002168 BPF_SIZE(insn->code) == BPF_DW ||
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002169 (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002170 verbose("BPF_LD_[ABS|IND] uses reserved fields\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002171 return -EINVAL;
2172 }
2173
2174 /* check whether implicit source operand (register R6) is readable */
2175 err = check_reg_arg(regs, BPF_REG_6, SRC_OP);
2176 if (err)
2177 return err;
2178
2179 if (regs[BPF_REG_6].type != PTR_TO_CTX) {
2180 verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
2181 return -EINVAL;
2182 }
2183
2184 if (mode == BPF_IND) {
2185 /* check explicit source operand */
2186 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2187 if (err)
2188 return err;
2189 }
2190
2191 /* reset caller saved regs to unreadable */
2192 for (i = 0; i < CALLER_SAVED_REGS; i++) {
2193 reg = regs + caller_saved[i];
2194 reg->type = NOT_INIT;
2195 reg->imm = 0;
2196 }
2197
2198 /* mark destination R0 register as readable, since it contains
2199 * the value fetched from the packet
2200 */
2201 regs[BPF_REG_0].type = UNKNOWN_VALUE;
2202 return 0;
2203}
2204
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002205/* non-recursive DFS pseudo code
2206 * 1 procedure DFS-iterative(G,v):
2207 * 2 label v as discovered
2208 * 3 let S be a stack
2209 * 4 S.push(v)
2210 * 5 while S is not empty
2211 * 6 t <- S.pop()
2212 * 7 if t is what we're looking for:
2213 * 8 return t
2214 * 9 for all edges e in G.adjacentEdges(t) do
2215 * 10 if edge e is already labelled
2216 * 11 continue with the next edge
2217 * 12 w <- G.adjacentVertex(t,e)
2218 * 13 if vertex w is not discovered and not explored
2219 * 14 label e as tree-edge
2220 * 15 label w as discovered
2221 * 16 S.push(w)
2222 * 17 continue at 5
2223 * 18 else if vertex w is discovered
2224 * 19 label e as back-edge
2225 * 20 else
2226 * 21 // vertex w is explored
2227 * 22 label e as forward- or cross-edge
2228 * 23 label t as explored
2229 * 24 S.pop()
2230 *
2231 * convention:
2232 * 0x10 - discovered
2233 * 0x11 - discovered and fall-through edge labelled
2234 * 0x12 - discovered and fall-through and branch edges labelled
2235 * 0x20 - explored
2236 */
2237
2238enum {
2239 DISCOVERED = 0x10,
2240 EXPLORED = 0x20,
2241 FALLTHROUGH = 1,
2242 BRANCH = 2,
2243};
2244
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002245#define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002246
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002247static int *insn_stack; /* stack of insns to process */
2248static int cur_stack; /* current stack index */
2249static int *insn_state;
2250
2251/* t, w, e - match pseudo-code above:
2252 * t - index of current instruction
2253 * w - next instruction
2254 * e - edge
2255 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002256static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002257{
2258 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
2259 return 0;
2260
2261 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
2262 return 0;
2263
2264 if (w < 0 || w >= env->prog->len) {
2265 verbose("jump out of range from insn %d to %d\n", t, w);
2266 return -EINVAL;
2267 }
2268
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002269 if (e == BRANCH)
2270 /* mark branch target for state pruning */
2271 env->explored_states[w] = STATE_LIST_MARK;
2272
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002273 if (insn_state[w] == 0) {
2274 /* tree-edge */
2275 insn_state[t] = DISCOVERED | e;
2276 insn_state[w] = DISCOVERED;
2277 if (cur_stack >= env->prog->len)
2278 return -E2BIG;
2279 insn_stack[cur_stack++] = w;
2280 return 1;
2281 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
2282 verbose("back-edge from insn %d to %d\n", t, w);
2283 return -EINVAL;
2284 } else if (insn_state[w] == EXPLORED) {
2285 /* forward- or cross-edge */
2286 insn_state[t] = DISCOVERED | e;
2287 } else {
2288 verbose("insn state internal bug\n");
2289 return -EFAULT;
2290 }
2291 return 0;
2292}
2293
2294/* non-recursive depth-first-search to detect loops in BPF program
2295 * loop == back-edge in directed graph
2296 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002297static int check_cfg(struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002298{
2299 struct bpf_insn *insns = env->prog->insnsi;
2300 int insn_cnt = env->prog->len;
2301 int ret = 0;
2302 int i, t;
2303
2304 insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
2305 if (!insn_state)
2306 return -ENOMEM;
2307
2308 insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
2309 if (!insn_stack) {
2310 kfree(insn_state);
2311 return -ENOMEM;
2312 }
2313
2314 insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
2315 insn_stack[0] = 0; /* 0 is the first instruction */
2316 cur_stack = 1;
2317
2318peek_stack:
2319 if (cur_stack == 0)
2320 goto check_state;
2321 t = insn_stack[cur_stack - 1];
2322
2323 if (BPF_CLASS(insns[t].code) == BPF_JMP) {
2324 u8 opcode = BPF_OP(insns[t].code);
2325
2326 if (opcode == BPF_EXIT) {
2327 goto mark_explored;
2328 } else if (opcode == BPF_CALL) {
2329 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2330 if (ret == 1)
2331 goto peek_stack;
2332 else if (ret < 0)
2333 goto err_free;
Daniel Borkmann07016152016-04-05 22:33:17 +02002334 if (t + 1 < insn_cnt)
2335 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002336 } else if (opcode == BPF_JA) {
2337 if (BPF_SRC(insns[t].code) != BPF_K) {
2338 ret = -EINVAL;
2339 goto err_free;
2340 }
2341 /* unconditional jump with single edge */
2342 ret = push_insn(t, t + insns[t].off + 1,
2343 FALLTHROUGH, env);
2344 if (ret == 1)
2345 goto peek_stack;
2346 else if (ret < 0)
2347 goto err_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002348 /* tell verifier to check for equivalent states
2349 * after every call and jump
2350 */
Alexei Starovoitovc3de6312015-04-14 15:57:13 -07002351 if (t + 1 < insn_cnt)
2352 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002353 } else {
2354 /* conditional jump with two edges */
2355 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2356 if (ret == 1)
2357 goto peek_stack;
2358 else if (ret < 0)
2359 goto err_free;
2360
2361 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
2362 if (ret == 1)
2363 goto peek_stack;
2364 else if (ret < 0)
2365 goto err_free;
2366 }
2367 } else {
2368 /* all other non-branch instructions with single
2369 * fall-through edge
2370 */
2371 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2372 if (ret == 1)
2373 goto peek_stack;
2374 else if (ret < 0)
2375 goto err_free;
2376 }
2377
2378mark_explored:
2379 insn_state[t] = EXPLORED;
2380 if (cur_stack-- <= 0) {
2381 verbose("pop stack internal bug\n");
2382 ret = -EFAULT;
2383 goto err_free;
2384 }
2385 goto peek_stack;
2386
2387check_state:
2388 for (i = 0; i < insn_cnt; i++) {
2389 if (insn_state[i] != EXPLORED) {
2390 verbose("unreachable insn %d\n", i);
2391 ret = -EINVAL;
2392 goto err_free;
2393 }
2394 }
2395 ret = 0; /* cfg looks good */
2396
2397err_free:
2398 kfree(insn_state);
2399 kfree(insn_stack);
2400 return ret;
2401}
2402
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002403/* the following conditions reduce the number of explored insns
2404 * from ~140k to ~80k for ultra large programs that use a lot of ptr_to_packet
2405 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002406static bool compare_ptrs_to_packet(struct bpf_reg_state *old,
2407 struct bpf_reg_state *cur)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002408{
2409 if (old->id != cur->id)
2410 return false;
2411
2412 /* old ptr_to_packet is more conservative, since it allows smaller
2413 * range. Ex:
2414 * old(off=0,r=10) is equal to cur(off=0,r=20), because
2415 * old(off=0,r=10) means that with range=10 the verifier proceeded
2416 * further and found no issues with the program. Now we're in the same
2417 * spot with cur(off=0,r=20), so we're safe too, since anything further
2418 * will only be looking at most 10 bytes after this pointer.
2419 */
2420 if (old->off == cur->off && old->range < cur->range)
2421 return true;
2422
2423 /* old(off=20,r=10) is equal to cur(off=22,re=22 or 5 or 0)
2424 * since both cannot be used for packet access and safe(old)
2425 * pointer has smaller off that could be used for further
2426 * 'if (ptr > data_end)' check
2427 * Ex:
2428 * old(off=20,r=10) and cur(off=22,r=22) and cur(off=22,r=0) mean
2429 * that we cannot access the packet.
2430 * The safe range is:
2431 * [ptr, ptr + range - off)
2432 * so whenever off >=range, it means no safe bytes from this pointer.
2433 * When comparing old->off <= cur->off, it means that older code
2434 * went with smaller offset and that offset was later
2435 * used to figure out the safe range after 'if (ptr > data_end)' check
2436 * Say, 'old' state was explored like:
2437 * ... R3(off=0, r=0)
2438 * R4 = R3 + 20
2439 * ... now R4(off=20,r=0) <-- here
2440 * if (R4 > data_end)
2441 * ... R4(off=20,r=20), R3(off=0,r=20) and R3 can be used to access.
2442 * ... the code further went all the way to bpf_exit.
2443 * Now the 'cur' state at the mark 'here' has R4(off=30,r=0).
2444 * old_R4(off=20,r=0) equal to cur_R4(off=30,r=0), since if the verifier
2445 * goes further, such cur_R4 will give larger safe packet range after
2446 * 'if (R4 > data_end)' and all further insn were already good with r=20,
2447 * so they will be good with r=30 and we can prune the search.
2448 */
2449 if (old->off <= cur->off &&
2450 old->off >= old->range && cur->off >= cur->range)
2451 return true;
2452
2453 return false;
2454}
2455
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002456/* compare two verifier states
2457 *
2458 * all states stored in state_list are known to be valid, since
2459 * verifier reached 'bpf_exit' instruction through them
2460 *
2461 * this function is called when verifier exploring different branches of
2462 * execution popped from the state stack. If it sees an old state that has
2463 * more strict register state and more strict stack state then this execution
2464 * branch doesn't need to be explored further, since verifier already
2465 * concluded that more strict state leads to valid finish.
2466 *
2467 * Therefore two states are equivalent if register state is more conservative
2468 * and explored stack state is more conservative than the current one.
2469 * Example:
2470 * explored current
2471 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
2472 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
2473 *
2474 * In other words if current stack state (one being explored) has more
2475 * valid slots than old one that already passed validation, it means
2476 * the verifier can stop exploring and conclude that current state is valid too
2477 *
2478 * Similarly with registers. If explored state has register type as invalid
2479 * whereas register type in current state is meaningful, it means that
2480 * the current state will reach 'bpf_exit' instruction safely
2481 */
Josef Bacik48461132016-09-28 10:54:32 -04002482static bool states_equal(struct bpf_verifier_env *env,
2483 struct bpf_verifier_state *old,
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002484 struct bpf_verifier_state *cur)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002485{
Josef Bacike2d2afe2016-11-29 12:27:09 -05002486 bool varlen_map_access = env->varlen_map_value_access;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002487 struct bpf_reg_state *rold, *rcur;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002488 int i;
2489
2490 for (i = 0; i < MAX_BPF_REG; i++) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002491 rold = &old->regs[i];
2492 rcur = &cur->regs[i];
2493
2494 if (memcmp(rold, rcur, sizeof(*rold)) == 0)
2495 continue;
2496
Josef Bacik48461132016-09-28 10:54:32 -04002497 /* If the ranges were not the same, but everything else was and
2498 * we didn't do a variable access into a map then we are a-ok.
2499 */
Josef Bacike2d2afe2016-11-29 12:27:09 -05002500 if (!varlen_map_access &&
Josef Bacik48461132016-09-28 10:54:32 -04002501 rold->type == rcur->type && rold->imm == rcur->imm)
2502 continue;
2503
Josef Bacike2d2afe2016-11-29 12:27:09 -05002504 /* If we didn't map access then again we don't care about the
2505 * mismatched range values and it's ok if our old type was
2506 * UNKNOWN and we didn't go to a NOT_INIT'ed reg.
2507 */
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002508 if (rold->type == NOT_INIT ||
Josef Bacike2d2afe2016-11-29 12:27:09 -05002509 (!varlen_map_access && rold->type == UNKNOWN_VALUE &&
2510 rcur->type != NOT_INIT))
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002511 continue;
2512
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002513 if (rold->type == PTR_TO_PACKET && rcur->type == PTR_TO_PACKET &&
2514 compare_ptrs_to_packet(rold, rcur))
2515 continue;
2516
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002517 return false;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002518 }
2519
2520 for (i = 0; i < MAX_BPF_STACK; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002521 if (old->stack_slot_type[i] == STACK_INVALID)
2522 continue;
2523 if (old->stack_slot_type[i] != cur->stack_slot_type[i])
2524 /* Ex: old explored (safe) state has STACK_SPILL in
2525 * this stack slot, but current has has STACK_MISC ->
2526 * this verifier states are not equivalent,
2527 * return false to continue verification of this path
2528 */
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002529 return false;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002530 if (i % BPF_REG_SIZE)
2531 continue;
2532 if (memcmp(&old->spilled_regs[i / BPF_REG_SIZE],
2533 &cur->spilled_regs[i / BPF_REG_SIZE],
2534 sizeof(old->spilled_regs[0])))
2535 /* when explored and current stack slot types are
2536 * the same, check that stored pointers types
2537 * are the same as well.
2538 * Ex: explored safe path could have stored
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002539 * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -8}
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002540 * but current path has stored:
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002541 * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -16}
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002542 * such verifier states are not equivalent.
2543 * return false to continue verification of this path
2544 */
2545 return false;
2546 else
2547 continue;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002548 }
2549 return true;
2550}
2551
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002552static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002553{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002554 struct bpf_verifier_state_list *new_sl;
2555 struct bpf_verifier_state_list *sl;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002556
2557 sl = env->explored_states[insn_idx];
2558 if (!sl)
2559 /* this 'insn_idx' instruction wasn't marked, so we will not
2560 * be doing state search here
2561 */
2562 return 0;
2563
2564 while (sl != STATE_LIST_MARK) {
Josef Bacik48461132016-09-28 10:54:32 -04002565 if (states_equal(env, &sl->state, &env->cur_state))
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002566 /* reached equivalent register/stack state,
2567 * prune the search
2568 */
2569 return 1;
2570 sl = sl->next;
2571 }
2572
2573 /* there were no equivalent states, remember current one.
2574 * technically the current state is not proven to be safe yet,
2575 * but it will either reach bpf_exit (which means it's safe) or
2576 * it will be rejected. Since there are no loops, we won't be
2577 * seeing this 'insn_idx' instruction again on the way to bpf_exit
2578 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002579 new_sl = kmalloc(sizeof(struct bpf_verifier_state_list), GFP_USER);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002580 if (!new_sl)
2581 return -ENOMEM;
2582
2583 /* add new state to the head of linked list */
2584 memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state));
2585 new_sl->next = env->explored_states[insn_idx];
2586 env->explored_states[insn_idx] = new_sl;
2587 return 0;
2588}
2589
Jakub Kicinski13a27df2016-09-21 11:43:58 +01002590static int ext_analyzer_insn_hook(struct bpf_verifier_env *env,
2591 int insn_idx, int prev_insn_idx)
2592{
2593 if (!env->analyzer_ops || !env->analyzer_ops->insn_hook)
2594 return 0;
2595
2596 return env->analyzer_ops->insn_hook(env, insn_idx, prev_insn_idx);
2597}
2598
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002599static int do_check(struct bpf_verifier_env *env)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002600{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002601 struct bpf_verifier_state *state = &env->cur_state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002602 struct bpf_insn *insns = env->prog->insnsi;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002603 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002604 int insn_cnt = env->prog->len;
2605 int insn_idx, prev_insn_idx = 0;
2606 int insn_processed = 0;
2607 bool do_print_state = false;
2608
2609 init_reg_state(regs);
2610 insn_idx = 0;
Josef Bacik48461132016-09-28 10:54:32 -04002611 env->varlen_map_value_access = false;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002612 for (;;) {
2613 struct bpf_insn *insn;
2614 u8 class;
2615 int err;
2616
2617 if (insn_idx >= insn_cnt) {
2618 verbose("invalid insn idx %d insn_cnt %d\n",
2619 insn_idx, insn_cnt);
2620 return -EFAULT;
2621 }
2622
2623 insn = &insns[insn_idx];
2624 class = BPF_CLASS(insn->code);
2625
Daniel Borkmann07016152016-04-05 22:33:17 +02002626 if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002627 verbose("BPF program is too large. Proccessed %d insn\n",
2628 insn_processed);
2629 return -E2BIG;
2630 }
2631
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002632 err = is_state_visited(env, insn_idx);
2633 if (err < 0)
2634 return err;
2635 if (err == 1) {
2636 /* found equivalent state, can prune the search */
2637 if (log_level) {
2638 if (do_print_state)
2639 verbose("\nfrom %d to %d: safe\n",
2640 prev_insn_idx, insn_idx);
2641 else
2642 verbose("%d: safe\n", insn_idx);
2643 }
2644 goto process_bpf_exit;
2645 }
2646
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002647 if (log_level && do_print_state) {
2648 verbose("\nfrom %d to %d:", prev_insn_idx, insn_idx);
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002649 print_verifier_state(&env->cur_state);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002650 do_print_state = false;
2651 }
2652
2653 if (log_level) {
2654 verbose("%d: ", insn_idx);
2655 print_bpf_insn(insn);
2656 }
2657
Jakub Kicinski13a27df2016-09-21 11:43:58 +01002658 err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx);
2659 if (err)
2660 return err;
2661
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002662 if (class == BPF_ALU || class == BPF_ALU64) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002663 err = check_alu_op(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002664 if (err)
2665 return err;
2666
2667 } else if (class == BPF_LDX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002668 enum bpf_reg_type *prev_src_type, src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002669
2670 /* check for reserved fields is already done */
2671
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002672 /* check src operand */
2673 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2674 if (err)
2675 return err;
2676
2677 err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
2678 if (err)
2679 return err;
2680
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07002681 src_reg_type = regs[insn->src_reg].type;
2682
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002683 /* check that memory (src_reg + off) is readable,
2684 * the state of dst_reg will be updated by this func
2685 */
2686 err = check_mem_access(env, insn->src_reg, insn->off,
2687 BPF_SIZE(insn->code), BPF_READ,
2688 insn->dst_reg);
2689 if (err)
2690 return err;
2691
Josef Bacik48461132016-09-28 10:54:32 -04002692 reset_reg_range_values(regs, insn->dst_reg);
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07002693 if (BPF_SIZE(insn->code) != BPF_W &&
2694 BPF_SIZE(insn->code) != BPF_DW) {
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07002695 insn_idx++;
2696 continue;
2697 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002698
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002699 prev_src_type = &env->insn_aux_data[insn_idx].ptr_type;
2700
2701 if (*prev_src_type == NOT_INIT) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002702 /* saw a valid insn
2703 * dst_reg = *(u32 *)(src_reg + off)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002704 * save type to validate intersecting paths
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002705 */
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002706 *prev_src_type = src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002707
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002708 } else if (src_reg_type != *prev_src_type &&
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002709 (src_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002710 *prev_src_type == PTR_TO_CTX)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002711 /* ABuser program is trying to use the same insn
2712 * dst_reg = *(u32*) (src_reg + off)
2713 * with different pointer types:
2714 * src_reg == ctx in one branch and
2715 * src_reg == stack|map in some other branch.
2716 * Reject it.
2717 */
2718 verbose("same insn cannot be used with different pointers\n");
2719 return -EINVAL;
2720 }
2721
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002722 } else if (class == BPF_STX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002723 enum bpf_reg_type *prev_dst_type, dst_reg_type;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002724
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002725 if (BPF_MODE(insn->code) == BPF_XADD) {
2726 err = check_xadd(env, insn);
2727 if (err)
2728 return err;
2729 insn_idx++;
2730 continue;
2731 }
2732
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002733 /* check src1 operand */
2734 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2735 if (err)
2736 return err;
2737 /* check src2 operand */
2738 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
2739 if (err)
2740 return err;
2741
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002742 dst_reg_type = regs[insn->dst_reg].type;
2743
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002744 /* check that memory (dst_reg + off) is writeable */
2745 err = check_mem_access(env, insn->dst_reg, insn->off,
2746 BPF_SIZE(insn->code), BPF_WRITE,
2747 insn->src_reg);
2748 if (err)
2749 return err;
2750
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002751 prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type;
2752
2753 if (*prev_dst_type == NOT_INIT) {
2754 *prev_dst_type = dst_reg_type;
2755 } else if (dst_reg_type != *prev_dst_type &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002756 (dst_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002757 *prev_dst_type == PTR_TO_CTX)) {
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002758 verbose("same insn cannot be used with different pointers\n");
2759 return -EINVAL;
2760 }
2761
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002762 } else if (class == BPF_ST) {
2763 if (BPF_MODE(insn->code) != BPF_MEM ||
2764 insn->src_reg != BPF_REG_0) {
2765 verbose("BPF_ST uses reserved fields\n");
2766 return -EINVAL;
2767 }
2768 /* check src operand */
2769 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
2770 if (err)
2771 return err;
2772
2773 /* check that memory (dst_reg + off) is writeable */
2774 err = check_mem_access(env, insn->dst_reg, insn->off,
2775 BPF_SIZE(insn->code), BPF_WRITE,
2776 -1);
2777 if (err)
2778 return err;
2779
2780 } else if (class == BPF_JMP) {
2781 u8 opcode = BPF_OP(insn->code);
2782
2783 if (opcode == BPF_CALL) {
2784 if (BPF_SRC(insn->code) != BPF_K ||
2785 insn->off != 0 ||
2786 insn->src_reg != BPF_REG_0 ||
2787 insn->dst_reg != BPF_REG_0) {
2788 verbose("BPF_CALL uses reserved fields\n");
2789 return -EINVAL;
2790 }
2791
2792 err = check_call(env, insn->imm);
2793 if (err)
2794 return err;
2795
2796 } else if (opcode == BPF_JA) {
2797 if (BPF_SRC(insn->code) != BPF_K ||
2798 insn->imm != 0 ||
2799 insn->src_reg != BPF_REG_0 ||
2800 insn->dst_reg != BPF_REG_0) {
2801 verbose("BPF_JA uses reserved fields\n");
2802 return -EINVAL;
2803 }
2804
2805 insn_idx += insn->off + 1;
2806 continue;
2807
2808 } else if (opcode == BPF_EXIT) {
2809 if (BPF_SRC(insn->code) != BPF_K ||
2810 insn->imm != 0 ||
2811 insn->src_reg != BPF_REG_0 ||
2812 insn->dst_reg != BPF_REG_0) {
2813 verbose("BPF_EXIT uses reserved fields\n");
2814 return -EINVAL;
2815 }
2816
2817 /* eBPF calling convetion is such that R0 is used
2818 * to return the value from eBPF program.
2819 * Make sure that it's readable at this time
2820 * of bpf_exit, which means that program wrote
2821 * something into it earlier
2822 */
2823 err = check_reg_arg(regs, BPF_REG_0, SRC_OP);
2824 if (err)
2825 return err;
2826
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002827 if (is_pointer_value(env, BPF_REG_0)) {
2828 verbose("R0 leaks addr as return value\n");
2829 return -EACCES;
2830 }
2831
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002832process_bpf_exit:
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002833 insn_idx = pop_stack(env, &prev_insn_idx);
2834 if (insn_idx < 0) {
2835 break;
2836 } else {
2837 do_print_state = true;
2838 continue;
2839 }
2840 } else {
2841 err = check_cond_jmp_op(env, insn, &insn_idx);
2842 if (err)
2843 return err;
2844 }
2845 } else if (class == BPF_LD) {
2846 u8 mode = BPF_MODE(insn->code);
2847
2848 if (mode == BPF_ABS || mode == BPF_IND) {
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002849 err = check_ld_abs(env, insn);
2850 if (err)
2851 return err;
2852
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002853 } else if (mode == BPF_IMM) {
2854 err = check_ld_imm(env, insn);
2855 if (err)
2856 return err;
2857
2858 insn_idx++;
2859 } else {
2860 verbose("invalid BPF_LD mode\n");
2861 return -EINVAL;
2862 }
Josef Bacik48461132016-09-28 10:54:32 -04002863 reset_reg_range_values(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002864 } else {
2865 verbose("unknown insn class %d\n", class);
2866 return -EINVAL;
2867 }
2868
2869 insn_idx++;
2870 }
2871
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002872 verbose("processed %d insns\n", insn_processed);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002873 return 0;
2874}
2875
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07002876static int check_map_prog_compatibility(struct bpf_map *map,
2877 struct bpf_prog *prog)
2878
2879{
2880 if (prog->type == BPF_PROG_TYPE_PERF_EVENT &&
2881 (map->map_type == BPF_MAP_TYPE_HASH ||
2882 map->map_type == BPF_MAP_TYPE_PERCPU_HASH) &&
2883 (map->map_flags & BPF_F_NO_PREALLOC)) {
2884 verbose("perf_event programs can only use preallocated hash map\n");
2885 return -EINVAL;
2886 }
2887 return 0;
2888}
2889
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002890/* look for pseudo eBPF instructions that access map FDs and
2891 * replace them with actual map pointers
2892 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002893static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002894{
2895 struct bpf_insn *insn = env->prog->insnsi;
2896 int insn_cnt = env->prog->len;
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07002897 int i, j, err;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002898
2899 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002900 if (BPF_CLASS(insn->code) == BPF_LDX &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002901 (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002902 verbose("BPF_LDX uses reserved fields\n");
2903 return -EINVAL;
2904 }
2905
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002906 if (BPF_CLASS(insn->code) == BPF_STX &&
2907 ((BPF_MODE(insn->code) != BPF_MEM &&
2908 BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
2909 verbose("BPF_STX uses reserved fields\n");
2910 return -EINVAL;
2911 }
2912
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002913 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
2914 struct bpf_map *map;
2915 struct fd f;
2916
2917 if (i == insn_cnt - 1 || insn[1].code != 0 ||
2918 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
2919 insn[1].off != 0) {
2920 verbose("invalid bpf_ld_imm64 insn\n");
2921 return -EINVAL;
2922 }
2923
2924 if (insn->src_reg == 0)
2925 /* valid generic load 64-bit imm */
2926 goto next_insn;
2927
2928 if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
2929 verbose("unrecognized bpf_ld_imm64 insn\n");
2930 return -EINVAL;
2931 }
2932
2933 f = fdget(insn->imm);
Daniel Borkmannc2101292015-10-29 14:58:07 +01002934 map = __bpf_map_get(f);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002935 if (IS_ERR(map)) {
2936 verbose("fd %d is not pointing to valid bpf_map\n",
2937 insn->imm);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002938 return PTR_ERR(map);
2939 }
2940
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07002941 err = check_map_prog_compatibility(map, env->prog);
2942 if (err) {
2943 fdput(f);
2944 return err;
2945 }
2946
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002947 /* store map pointer inside BPF_LD_IMM64 instruction */
2948 insn[0].imm = (u32) (unsigned long) map;
2949 insn[1].imm = ((u64) (unsigned long) map) >> 32;
2950
2951 /* check whether we recorded this map already */
2952 for (j = 0; j < env->used_map_cnt; j++)
2953 if (env->used_maps[j] == map) {
2954 fdput(f);
2955 goto next_insn;
2956 }
2957
2958 if (env->used_map_cnt >= MAX_USED_MAPS) {
2959 fdput(f);
2960 return -E2BIG;
2961 }
2962
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002963 /* hold the map. If the program is rejected by verifier,
2964 * the map will be released by release_maps() or it
2965 * will be used by the valid program until it's unloaded
2966 * and all maps are released in free_bpf_prog_info()
2967 */
Alexei Starovoitov92117d82016-04-27 18:56:20 -07002968 map = bpf_map_inc(map, false);
2969 if (IS_ERR(map)) {
2970 fdput(f);
2971 return PTR_ERR(map);
2972 }
2973 env->used_maps[env->used_map_cnt++] = map;
2974
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002975 fdput(f);
2976next_insn:
2977 insn++;
2978 i++;
2979 }
2980 }
2981
2982 /* now all pseudo BPF_LD_IMM64 instructions load valid
2983 * 'struct bpf_map *' into a register instead of user map_fd.
2984 * These pointers will be used later by verifier to validate map access.
2985 */
2986 return 0;
2987}
2988
2989/* drop refcnt of maps used by the rejected program */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002990static void release_maps(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002991{
2992 int i;
2993
2994 for (i = 0; i < env->used_map_cnt; i++)
2995 bpf_map_put(env->used_maps[i]);
2996}
2997
2998/* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002999static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003000{
3001 struct bpf_insn *insn = env->prog->insnsi;
3002 int insn_cnt = env->prog->len;
3003 int i;
3004
3005 for (i = 0; i < insn_cnt; i++, insn++)
3006 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
3007 insn->src_reg = 0;
3008}
3009
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003010/* convert load instructions that access fields of 'struct __sk_buff'
3011 * into sequence of instructions that access fields of 'struct sk_buff'
3012 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003013static int convert_ctx_accesses(struct bpf_verifier_env *env)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003014{
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003015 const struct bpf_verifier_ops *ops = env->prog->aux->ops;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003016 const int insn_cnt = env->prog->len;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003017 struct bpf_insn insn_buf[16], *insn;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003018 struct bpf_prog *new_prog;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003019 enum bpf_access_type type;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003020 int i, cnt, delta = 0;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003021
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003022 if (ops->gen_prologue) {
3023 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
3024 env->prog);
3025 if (cnt >= ARRAY_SIZE(insn_buf)) {
3026 verbose("bpf verifier is misconfigured\n");
3027 return -EINVAL;
3028 } else if (cnt) {
3029 new_prog = bpf_patch_insn_single(env->prog, 0,
3030 insn_buf, cnt);
3031 if (!new_prog)
3032 return -ENOMEM;
3033 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003034 delta += cnt - 1;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003035 }
3036 }
3037
3038 if (!ops->convert_ctx_access)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003039 return 0;
3040
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003041 insn = env->prog->insnsi + delta;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003042
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003043 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07003044 if (insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
3045 insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003046 type = BPF_READ;
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07003047 else if (insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
3048 insn->code == (BPF_STX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003049 type = BPF_WRITE;
3050 else
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003051 continue;
3052
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003053 if (env->insn_aux_data[i].ptr_type != PTR_TO_CTX)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003054 continue;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003055
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003056 cnt = ops->convert_ctx_access(type, insn->dst_reg, insn->src_reg,
3057 insn->off, insn_buf, env->prog);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003058 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
3059 verbose("bpf verifier is misconfigured\n");
3060 return -EINVAL;
3061 }
3062
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003063 new_prog = bpf_patch_insn_single(env->prog, i + delta, insn_buf,
3064 cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003065 if (!new_prog)
3066 return -ENOMEM;
3067
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003068 delta += cnt - 1;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003069
3070 /* keep walking new program and skip insns we just inserted */
3071 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003072 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003073 }
3074
3075 return 0;
3076}
3077
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003078static void free_states(struct bpf_verifier_env *env)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003079{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003080 struct bpf_verifier_state_list *sl, *sln;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003081 int i;
3082
3083 if (!env->explored_states)
3084 return;
3085
3086 for (i = 0; i < env->prog->len; i++) {
3087 sl = env->explored_states[i];
3088
3089 if (sl)
3090 while (sl != STATE_LIST_MARK) {
3091 sln = sl->next;
3092 kfree(sl);
3093 sl = sln;
3094 }
3095 }
3096
3097 kfree(env->explored_states);
3098}
3099
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003100int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003101{
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003102 char __user *log_ubuf = NULL;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003103 struct bpf_verifier_env *env;
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003104 int ret = -EINVAL;
3105
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003106 if ((*prog)->len <= 0 || (*prog)->len > BPF_MAXINSNS)
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003107 return -E2BIG;
3108
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003109 /* 'struct bpf_verifier_env' can be global, but since it's not small,
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003110 * allocate/free it every time bpf_check() is called
3111 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003112 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003113 if (!env)
3114 return -ENOMEM;
3115
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003116 env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
3117 (*prog)->len);
3118 ret = -ENOMEM;
3119 if (!env->insn_aux_data)
3120 goto err_free_env;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003121 env->prog = *prog;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003122
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003123 /* grab the mutex to protect few globals used by verifier */
3124 mutex_lock(&bpf_verifier_lock);
3125
3126 if (attr->log_level || attr->log_buf || attr->log_size) {
3127 /* user requested verbose verifier output
3128 * and supplied buffer to store the verification trace
3129 */
3130 log_level = attr->log_level;
3131 log_ubuf = (char __user *) (unsigned long) attr->log_buf;
3132 log_size = attr->log_size;
3133 log_len = 0;
3134
3135 ret = -EINVAL;
3136 /* log_* values have to be sane */
3137 if (log_size < 128 || log_size > UINT_MAX >> 8 ||
3138 log_level == 0 || log_ubuf == NULL)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003139 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003140
3141 ret = -ENOMEM;
3142 log_buf = vmalloc(log_size);
3143 if (!log_buf)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003144 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003145 } else {
3146 log_level = 0;
3147 }
3148
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003149 ret = replace_map_fd_with_map_ptr(env);
3150 if (ret < 0)
3151 goto skip_full_check;
3152
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003153 env->explored_states = kcalloc(env->prog->len,
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003154 sizeof(struct bpf_verifier_state_list *),
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003155 GFP_USER);
3156 ret = -ENOMEM;
3157 if (!env->explored_states)
3158 goto skip_full_check;
3159
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003160 ret = check_cfg(env);
3161 if (ret < 0)
3162 goto skip_full_check;
3163
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003164 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
3165
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003166 ret = do_check(env);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003167
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003168skip_full_check:
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003169 while (pop_stack(env, NULL) >= 0);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003170 free_states(env);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003171
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003172 if (ret == 0)
3173 /* program is valid, convert *(u32*)(ctx + off) accesses */
3174 ret = convert_ctx_accesses(env);
3175
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003176 if (log_level && log_len >= log_size - 1) {
3177 BUG_ON(log_len >= log_size);
3178 /* verifier log exceeded user supplied buffer */
3179 ret = -ENOSPC;
3180 /* fall through to return what was recorded */
3181 }
3182
3183 /* copy verifier log back to user space including trailing zero */
3184 if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) {
3185 ret = -EFAULT;
3186 goto free_log_buf;
3187 }
3188
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003189 if (ret == 0 && env->used_map_cnt) {
3190 /* if program passed verifier, update used_maps in bpf_prog_info */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003191 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
3192 sizeof(env->used_maps[0]),
3193 GFP_KERNEL);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003194
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003195 if (!env->prog->aux->used_maps) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003196 ret = -ENOMEM;
3197 goto free_log_buf;
3198 }
3199
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003200 memcpy(env->prog->aux->used_maps, env->used_maps,
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003201 sizeof(env->used_maps[0]) * env->used_map_cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003202 env->prog->aux->used_map_cnt = env->used_map_cnt;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003203
3204 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
3205 * bpf_ld_imm64 instructions
3206 */
3207 convert_pseudo_ld_imm64(env);
3208 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003209
3210free_log_buf:
3211 if (log_level)
3212 vfree(log_buf);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003213 if (!env->prog->aux->used_maps)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003214 /* if we didn't copy map pointers into bpf_prog_info, release
3215 * them now. Otherwise free_bpf_prog_info() will release them.
3216 */
3217 release_maps(env);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003218 *prog = env->prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003219err_unlock:
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003220 mutex_unlock(&bpf_verifier_lock);
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003221 vfree(env->insn_aux_data);
3222err_free_env:
3223 kfree(env);
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003224 return ret;
3225}
Jakub Kicinski13a27df2016-09-21 11:43:58 +01003226
3227int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops,
3228 void *priv)
3229{
3230 struct bpf_verifier_env *env;
3231 int ret;
3232
3233 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
3234 if (!env)
3235 return -ENOMEM;
3236
3237 env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
3238 prog->len);
3239 ret = -ENOMEM;
3240 if (!env->insn_aux_data)
3241 goto err_free_env;
3242 env->prog = prog;
3243 env->analyzer_ops = ops;
3244 env->analyzer_priv = priv;
3245
3246 /* grab the mutex to protect few globals used by verifier */
3247 mutex_lock(&bpf_verifier_lock);
3248
3249 log_level = 0;
3250
3251 env->explored_states = kcalloc(env->prog->len,
3252 sizeof(struct bpf_verifier_state_list *),
3253 GFP_KERNEL);
3254 ret = -ENOMEM;
3255 if (!env->explored_states)
3256 goto skip_full_check;
3257
3258 ret = check_cfg(env);
3259 if (ret < 0)
3260 goto skip_full_check;
3261
3262 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
3263
3264 ret = do_check(env);
3265
3266skip_full_check:
3267 while (pop_stack(env, NULL) >= 0);
3268 free_states(env);
3269
3270 mutex_unlock(&bpf_verifier_lock);
3271 vfree(env->insn_aux_data);
3272err_free_env:
3273 kfree(env);
3274 return ret;
3275}
3276EXPORT_SYMBOL_GPL(bpf_analyzer);