blob: 64fcab1d8cd9ad923a9c23873f6364af55e374dd [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
Daniel Borkmann0e0f1d62016-12-18 01:52:59 +0100447static void __mark_reg_unknown_value(struct bpf_reg_state *regs, u32 regno)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700448{
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700449 regs[regno].type = UNKNOWN_VALUE;
Thomas Graf14117072016-10-18 19:51:19 +0200450 regs[regno].id = 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700451 regs[regno].imm = 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700452}
453
Daniel Borkmann0e0f1d62016-12-18 01:52:59 +0100454static void mark_reg_unknown_value(struct bpf_reg_state *regs, u32 regno)
455{
456 BUG_ON(regno >= MAX_BPF_REG);
457 __mark_reg_unknown_value(regs, regno);
458}
459
Josef Bacik48461132016-09-28 10:54:32 -0400460static void reset_reg_range_values(struct bpf_reg_state *regs, u32 regno)
461{
462 regs[regno].min_value = BPF_REGISTER_MIN_RANGE;
463 regs[regno].max_value = BPF_REGISTER_MAX_RANGE;
464}
465
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700466enum reg_arg_type {
467 SRC_OP, /* register is used as source operand */
468 DST_OP, /* register is used as destination operand */
469 DST_OP_NO_MARK /* same as above, check only, don't mark */
470};
471
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100472static int check_reg_arg(struct bpf_reg_state *regs, u32 regno,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700473 enum reg_arg_type t)
474{
475 if (regno >= MAX_BPF_REG) {
476 verbose("R%d is invalid\n", regno);
477 return -EINVAL;
478 }
479
480 if (t == SRC_OP) {
481 /* check whether register used as source operand can be read */
482 if (regs[regno].type == NOT_INIT) {
483 verbose("R%d !read_ok\n", regno);
484 return -EACCES;
485 }
486 } else {
487 /* check whether register used as dest operand can be written to */
488 if (regno == BPF_REG_FP) {
489 verbose("frame pointer is read only\n");
490 return -EACCES;
491 }
492 if (t == DST_OP)
493 mark_reg_unknown_value(regs, regno);
494 }
495 return 0;
496}
497
498static int bpf_size_to_bytes(int bpf_size)
499{
500 if (bpf_size == BPF_W)
501 return 4;
502 else if (bpf_size == BPF_H)
503 return 2;
504 else if (bpf_size == BPF_B)
505 return 1;
506 else if (bpf_size == BPF_DW)
507 return 8;
508 else
509 return -EINVAL;
510}
511
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700512static bool is_spillable_regtype(enum bpf_reg_type type)
513{
514 switch (type) {
515 case PTR_TO_MAP_VALUE:
516 case PTR_TO_MAP_VALUE_OR_NULL:
517 case PTR_TO_STACK:
518 case PTR_TO_CTX:
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700519 case PTR_TO_PACKET:
520 case PTR_TO_PACKET_END:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700521 case FRAME_PTR:
522 case CONST_PTR_TO_MAP:
523 return true;
524 default:
525 return false;
526 }
527}
528
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700529/* check_stack_read/write functions track spill/fill of registers,
530 * stack boundary and alignment are checked in check_mem_access()
531 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100532static int check_stack_write(struct bpf_verifier_state *state, int off,
533 int size, int value_regno)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700534{
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700535 int i;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700536 /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
537 * so it's aligned access and [off, off + size) are within stack limits
538 */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700539
540 if (value_regno >= 0 &&
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700541 is_spillable_regtype(state->regs[value_regno].type)) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700542
543 /* register containing pointer is being spilled into stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700544 if (size != BPF_REG_SIZE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700545 verbose("invalid size of register spill\n");
546 return -EACCES;
547 }
548
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700549 /* save register state */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700550 state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] =
551 state->regs[value_regno];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700552
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700553 for (i = 0; i < BPF_REG_SIZE; i++)
554 state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_SPILL;
555 } else {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700556 /* regular write of data into stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700557 state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] =
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100558 (struct bpf_reg_state) {};
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700559
560 for (i = 0; i < size; i++)
561 state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_MISC;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700562 }
563 return 0;
564}
565
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100566static int check_stack_read(struct bpf_verifier_state *state, int off, int size,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700567 int value_regno)
568{
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700569 u8 *slot_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700570 int i;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700571
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700572 slot_type = &state->stack_slot_type[MAX_BPF_STACK + off];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700573
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700574 if (slot_type[0] == STACK_SPILL) {
575 if (size != BPF_REG_SIZE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700576 verbose("invalid size of register spill\n");
577 return -EACCES;
578 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700579 for (i = 1; i < BPF_REG_SIZE; i++) {
580 if (slot_type[i] != STACK_SPILL) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700581 verbose("corrupted spill memory\n");
582 return -EACCES;
583 }
584 }
585
586 if (value_regno >= 0)
587 /* restore register state from stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700588 state->regs[value_regno] =
589 state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700590 return 0;
591 } else {
592 for (i = 0; i < size; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700593 if (slot_type[i] != STACK_MISC) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700594 verbose("invalid read from stack off %d+%d size %d\n",
595 off, i, size);
596 return -EACCES;
597 }
598 }
599 if (value_regno >= 0)
600 /* have read misc data from the stack */
601 mark_reg_unknown_value(state->regs, value_regno);
602 return 0;
603 }
604}
605
606/* check read/write into map element returned by bpf_map_lookup_elem() */
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100607static int check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700608 int size)
609{
610 struct bpf_map *map = env->cur_state.regs[regno].map_ptr;
611
612 if (off < 0 || off + size > map->value_size) {
613 verbose("invalid access to map value, value_size=%d off=%d size=%d\n",
614 map->value_size, off, size);
615 return -EACCES;
616 }
617 return 0;
618}
619
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700620#define MAX_PACKET_OFF 0xffff
621
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100622static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200623 const struct bpf_call_arg_meta *meta)
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700624{
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200625 switch (env->prog->type) {
626 case BPF_PROG_TYPE_SCHED_CLS:
627 case BPF_PROG_TYPE_SCHED_ACT:
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700628 case BPF_PROG_TYPE_XDP:
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200629 if (meta)
630 return meta->pkt_access;
631
632 env->seen_direct_write = true;
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700633 return true;
634 default:
635 return false;
636 }
637}
638
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100639static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700640 int size)
641{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100642 struct bpf_reg_state *regs = env->cur_state.regs;
643 struct bpf_reg_state *reg = &regs[regno];
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700644
Alexei Starovoitovd91b28e2016-05-19 18:17:13 -0700645 off += reg->off;
Daniel Borkmannb399cf62016-09-20 00:26:12 +0200646 if (off < 0 || size <= 0 || off + size > reg->range) {
Alexei Starovoitovd91b28e2016-05-19 18:17:13 -0700647 verbose("invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n",
648 off, size, regno, reg->id, reg->off, reg->range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700649 return -EACCES;
650 }
651 return 0;
652}
653
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700654/* check access to 'struct bpf_context' fields */
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100655static int check_ctx_access(struct bpf_verifier_env *env, int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700656 enum bpf_access_type t, enum bpf_reg_type *reg_type)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700657{
Jakub Kicinski13a27df2016-09-21 11:43:58 +0100658 /* for analyzer ctx accesses are already validated and converted */
659 if (env->analyzer_ops)
660 return 0;
661
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700662 if (env->prog->aux->ops->is_valid_access &&
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700663 env->prog->aux->ops->is_valid_access(off, size, t, reg_type)) {
Alexei Starovoitov32bbe002016-04-06 18:43:28 -0700664 /* remember the offset of last byte accessed in ctx */
665 if (env->prog->aux->max_ctx_offset < off + size)
666 env->prog->aux->max_ctx_offset = off + size;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700667 return 0;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -0700668 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700669
670 verbose("invalid bpf_context access off=%d size=%d\n", off, size);
671 return -EACCES;
672}
673
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100674static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700675{
676 if (env->allow_ptr_leaks)
677 return false;
678
679 switch (env->cur_state.regs[regno].type) {
680 case UNKNOWN_VALUE:
681 case CONST_IMM:
682 return false;
683 default:
684 return true;
685 }
686}
687
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100688static int check_ptr_alignment(struct bpf_verifier_env *env,
689 struct bpf_reg_state *reg, int off, int size)
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700690{
Josef Bacik48461132016-09-28 10:54:32 -0400691 if (reg->type != PTR_TO_PACKET && reg->type != PTR_TO_MAP_VALUE_ADJ) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700692 if (off % size != 0) {
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100693 verbose("misaligned access off %d size %d\n",
694 off, size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700695 return -EACCES;
696 } else {
697 return 0;
698 }
699 }
700
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700701 if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
702 /* misaligned access to packet is ok on x86,arm,arm64 */
703 return 0;
704
705 if (reg->id && size != 1) {
706 verbose("Unknown packet alignment. Only byte-sized access allowed\n");
707 return -EACCES;
708 }
709
710 /* skb->data is NET_IP_ALIGN-ed */
Josef Bacik48461132016-09-28 10:54:32 -0400711 if (reg->type == PTR_TO_PACKET &&
712 (NET_IP_ALIGN + reg->off + off) % size != 0) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700713 verbose("misaligned packet access off %d+%d+%d size %d\n",
714 NET_IP_ALIGN, reg->off, off, size);
715 return -EACCES;
716 }
717 return 0;
718}
719
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700720/* check whether memory at (regno + off) is accessible for t = (read | write)
721 * if t==write, value_regno is a register which value is stored into memory
722 * if t==read, value_regno is a register which will receive the value from memory
723 * if t==write && value_regno==-1, some unknown value is stored into memory
724 * if t==read && value_regno==-1, don't care what we read from memory
725 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100726static int check_mem_access(struct bpf_verifier_env *env, u32 regno, int off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700727 int bpf_size, enum bpf_access_type t,
728 int value_regno)
729{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100730 struct bpf_verifier_state *state = &env->cur_state;
731 struct bpf_reg_state *reg = &state->regs[regno];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700732 int size, err = 0;
733
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700734 if (reg->type == PTR_TO_STACK)
735 off += reg->imm;
Alex Gartrell24b4d2a2015-07-23 14:24:40 -0700736
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700737 size = bpf_size_to_bytes(bpf_size);
738 if (size < 0)
739 return size;
740
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700741 err = check_ptr_alignment(env, reg, off, size);
742 if (err)
743 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700744
Josef Bacik48461132016-09-28 10:54:32 -0400745 if (reg->type == PTR_TO_MAP_VALUE ||
746 reg->type == PTR_TO_MAP_VALUE_ADJ) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700747 if (t == BPF_WRITE && value_regno >= 0 &&
748 is_pointer_value(env, value_regno)) {
749 verbose("R%d leaks addr into map\n", value_regno);
750 return -EACCES;
751 }
Josef Bacik48461132016-09-28 10:54:32 -0400752
753 /* If we adjusted the register to this map value at all then we
754 * need to change off and size to min_value and max_value
755 * respectively to make sure our theoretical access will be
756 * safe.
757 */
758 if (reg->type == PTR_TO_MAP_VALUE_ADJ) {
759 if (log_level)
760 print_verifier_state(state);
761 env->varlen_map_value_access = true;
762 /* The minimum value is only important with signed
763 * comparisons where we can't assume the floor of a
764 * value is 0. If we are using signed variables for our
765 * index'es we need to make sure that whatever we use
766 * will have a set floor within our range.
767 */
Josef Bacikf23cc642016-11-14 15:45:36 -0500768 if (reg->min_value < 0) {
Josef Bacik48461132016-09-28 10:54:32 -0400769 verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
770 regno);
771 return -EACCES;
772 }
773 err = check_map_access(env, regno, reg->min_value + off,
774 size);
775 if (err) {
776 verbose("R%d min value is outside of the array range\n",
777 regno);
778 return err;
779 }
780
781 /* If we haven't set a max value then we need to bail
782 * since we can't be sure we won't do bad things.
783 */
784 if (reg->max_value == BPF_REGISTER_MAX_RANGE) {
785 verbose("R%d unbounded memory access, make sure to bounds check any array access into a map\n",
786 regno);
787 return -EACCES;
788 }
789 off += reg->max_value;
790 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700791 err = check_map_access(env, regno, off, size);
792 if (!err && t == BPF_READ && value_regno >= 0)
793 mark_reg_unknown_value(state->regs, value_regno);
794
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700795 } else if (reg->type == PTR_TO_CTX) {
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700796 enum bpf_reg_type reg_type = UNKNOWN_VALUE;
797
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700798 if (t == BPF_WRITE && value_regno >= 0 &&
799 is_pointer_value(env, value_regno)) {
800 verbose("R%d leaks addr into ctx\n", value_regno);
801 return -EACCES;
802 }
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700803 err = check_ctx_access(env, off, size, t, &reg_type);
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700804 if (!err && t == BPF_READ && value_regno >= 0) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700805 mark_reg_unknown_value(state->regs, value_regno);
Mickaël Salaün19553512016-09-24 20:01:50 +0200806 /* note that reg.[id|off|range] == 0 */
807 state->regs[value_regno].type = reg_type;
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700808 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700809
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700810 } else if (reg->type == FRAME_PTR || reg->type == PTR_TO_STACK) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700811 if (off >= 0 || off < -MAX_BPF_STACK) {
812 verbose("invalid stack off=%d size=%d\n", off, size);
813 return -EACCES;
814 }
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700815 if (t == BPF_WRITE) {
816 if (!env->allow_ptr_leaks &&
817 state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL &&
818 size != BPF_REG_SIZE) {
819 verbose("attempt to corrupt spilled pointer on stack\n");
820 return -EACCES;
821 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700822 err = check_stack_write(state, off, size, value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700823 } else {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700824 err = check_stack_read(state, off, size, value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700825 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700826 } else if (state->regs[regno].type == PTR_TO_PACKET) {
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200827 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL)) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700828 verbose("cannot write into packet\n");
829 return -EACCES;
830 }
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700831 if (t == BPF_WRITE && value_regno >= 0 &&
832 is_pointer_value(env, value_regno)) {
833 verbose("R%d leaks addr into packet\n", value_regno);
834 return -EACCES;
835 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700836 err = check_packet_access(env, regno, off, size);
837 if (!err && t == BPF_READ && value_regno >= 0)
838 mark_reg_unknown_value(state->regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700839 } else {
840 verbose("R%d invalid mem access '%s'\n",
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700841 regno, reg_type_str[reg->type]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700842 return -EACCES;
843 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700844
845 if (!err && size <= 2 && value_regno >= 0 && env->allow_ptr_leaks &&
846 state->regs[value_regno].type == UNKNOWN_VALUE) {
847 /* 1 or 2 byte load zero-extends, determine the number of
848 * zero upper bits. Not doing it fo 4 byte load, since
849 * such values cannot be added to ptr_to_packet anyway.
850 */
851 state->regs[value_regno].imm = 64 - size * 8;
852 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700853 return err;
854}
855
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100856static int check_xadd(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700857{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100858 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700859 int err;
860
861 if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
862 insn->imm != 0) {
863 verbose("BPF_XADD uses reserved fields\n");
864 return -EINVAL;
865 }
866
867 /* check src1 operand */
868 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
869 if (err)
870 return err;
871
872 /* check src2 operand */
873 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
874 if (err)
875 return err;
876
877 /* check whether atomic_add can read the memory */
878 err = check_mem_access(env, insn->dst_reg, insn->off,
879 BPF_SIZE(insn->code), BPF_READ, -1);
880 if (err)
881 return err;
882
883 /* check whether atomic_add can write into the same memory */
884 return check_mem_access(env, insn->dst_reg, insn->off,
885 BPF_SIZE(insn->code), BPF_WRITE, -1);
886}
887
888/* when register 'regno' is passed into function that will read 'access_size'
889 * bytes from that pointer, make sure that it's within stack boundary
890 * and all elements of stack are initialized
891 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100892static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
Daniel Borkmann435faee12016-04-13 00:10:51 +0200893 int access_size, bool zero_size_allowed,
894 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700895{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100896 struct bpf_verifier_state *state = &env->cur_state;
897 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700898 int off, i;
899
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +0100900 if (regs[regno].type != PTR_TO_STACK) {
901 if (zero_size_allowed && access_size == 0 &&
902 regs[regno].type == CONST_IMM &&
903 regs[regno].imm == 0)
904 return 0;
905
906 verbose("R%d type=%s expected=%s\n", regno,
907 reg_type_str[regs[regno].type],
908 reg_type_str[PTR_TO_STACK]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700909 return -EACCES;
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +0100910 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700911
912 off = regs[regno].imm;
913 if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
914 access_size <= 0) {
915 verbose("invalid stack type R%d off=%d access_size=%d\n",
916 regno, off, access_size);
917 return -EACCES;
918 }
919
Daniel Borkmann435faee12016-04-13 00:10:51 +0200920 if (meta && meta->raw_mode) {
921 meta->access_size = access_size;
922 meta->regno = regno;
923 return 0;
924 }
925
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700926 for (i = 0; i < access_size; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700927 if (state->stack_slot_type[MAX_BPF_STACK + off + i] != STACK_MISC) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700928 verbose("invalid indirect read from stack off %d+%d size %d\n",
929 off, i, access_size);
930 return -EACCES;
931 }
932 }
933 return 0;
934}
935
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100936static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200937 enum bpf_arg_type arg_type,
938 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700939{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100940 struct bpf_reg_state *regs = env->cur_state.regs, *reg = &regs[regno];
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700941 enum bpf_reg_type expected_type, type = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700942 int err = 0;
943
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100944 if (arg_type == ARG_DONTCARE)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700945 return 0;
946
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700947 if (type == NOT_INIT) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700948 verbose("R%d !read_ok\n", regno);
949 return -EACCES;
950 }
951
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700952 if (arg_type == ARG_ANYTHING) {
953 if (is_pointer_value(env, regno)) {
954 verbose("R%d leaks addr into helper function\n", regno);
955 return -EACCES;
956 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100957 return 0;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700958 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100959
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200960 if (type == PTR_TO_PACKET && !may_access_direct_pkt_data(env, meta)) {
961 verbose("helper access to the packet is not allowed\n");
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700962 return -EACCES;
963 }
964
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +0100965 if (arg_type == ARG_PTR_TO_MAP_KEY ||
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700966 arg_type == ARG_PTR_TO_MAP_VALUE) {
967 expected_type = PTR_TO_STACK;
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700968 if (type != PTR_TO_PACKET && type != expected_type)
969 goto err_type;
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +0100970 } else if (arg_type == ARG_CONST_STACK_SIZE ||
971 arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700972 expected_type = CONST_IMM;
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700973 if (type != expected_type)
974 goto err_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700975 } else if (arg_type == ARG_CONST_MAP_PTR) {
976 expected_type = CONST_PTR_TO_MAP;
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700977 if (type != expected_type)
978 goto err_type;
Alexei Starovoitov608cd712015-03-26 19:53:57 -0700979 } else if (arg_type == ARG_PTR_TO_CTX) {
980 expected_type = PTR_TO_CTX;
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700981 if (type != expected_type)
982 goto err_type;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200983 } else if (arg_type == ARG_PTR_TO_STACK ||
984 arg_type == ARG_PTR_TO_RAW_STACK) {
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +0100985 expected_type = PTR_TO_STACK;
986 /* One exception here. In case function allows for NULL to be
987 * passed in as argument, it's a CONST_IMM type. Final test
988 * happens during stack boundary checking.
989 */
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700990 if (type == CONST_IMM && reg->imm == 0)
991 /* final test in check_stack_boundary() */;
992 else if (type != PTR_TO_PACKET && type != expected_type)
993 goto err_type;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200994 meta->raw_mode = arg_type == ARG_PTR_TO_RAW_STACK;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700995 } else {
996 verbose("unsupported arg_type %d\n", arg_type);
997 return -EFAULT;
998 }
999
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001000 if (arg_type == ARG_CONST_MAP_PTR) {
1001 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001002 meta->map_ptr = reg->map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001003 } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
1004 /* bpf_map_xxx(..., map_ptr, ..., key) call:
1005 * check that [key, key + map->key_size) are within
1006 * stack limits and initialized
1007 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001008 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001009 /* in function declaration map_ptr must come before
1010 * map_key, so that it's verified and known before
1011 * we have to check map_key here. Otherwise it means
1012 * that kernel subsystem misconfigured verifier
1013 */
1014 verbose("invalid map_ptr to access map->key\n");
1015 return -EACCES;
1016 }
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001017 if (type == PTR_TO_PACKET)
1018 err = check_packet_access(env, regno, 0,
1019 meta->map_ptr->key_size);
1020 else
1021 err = check_stack_boundary(env, regno,
1022 meta->map_ptr->key_size,
1023 false, NULL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001024 } else if (arg_type == ARG_PTR_TO_MAP_VALUE) {
1025 /* bpf_map_xxx(..., map_ptr, ..., value) call:
1026 * check [value, value + map->value_size) validity
1027 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001028 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001029 /* kernel subsystem misconfigured verifier */
1030 verbose("invalid map_ptr to access map->value\n");
1031 return -EACCES;
1032 }
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001033 if (type == PTR_TO_PACKET)
1034 err = check_packet_access(env, regno, 0,
1035 meta->map_ptr->value_size);
1036 else
1037 err = check_stack_boundary(env, regno,
1038 meta->map_ptr->value_size,
1039 false, NULL);
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001040 } else if (arg_type == ARG_CONST_STACK_SIZE ||
1041 arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) {
1042 bool zero_size_allowed = (arg_type == ARG_CONST_STACK_SIZE_OR_ZERO);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001043
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001044 /* bpf_xxx(..., buf, len) call will access 'len' bytes
1045 * from stack pointer 'buf'. Check it
1046 * note: regno == len, regno - 1 == buf
1047 */
1048 if (regno == 0) {
1049 /* kernel subsystem misconfigured verifier */
1050 verbose("ARG_CONST_STACK_SIZE cannot be first argument\n");
1051 return -EACCES;
1052 }
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001053 if (regs[regno - 1].type == PTR_TO_PACKET)
1054 err = check_packet_access(env, regno - 1, 0, reg->imm);
1055 else
1056 err = check_stack_boundary(env, regno - 1, reg->imm,
1057 zero_size_allowed, meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001058 }
1059
1060 return err;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001061err_type:
1062 verbose("R%d type=%s expected=%s\n", regno,
1063 reg_type_str[type], reg_type_str[expected_type]);
1064 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001065}
1066
Kaixu Xia35578d72015-08-06 07:02:35 +00001067static int check_map_func_compatibility(struct bpf_map *map, int func_id)
1068{
Kaixu Xia35578d72015-08-06 07:02:35 +00001069 if (!map)
1070 return 0;
1071
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001072 /* We need a two way check, first is from map perspective ... */
1073 switch (map->map_type) {
1074 case BPF_MAP_TYPE_PROG_ARRAY:
1075 if (func_id != BPF_FUNC_tail_call)
1076 goto error;
1077 break;
1078 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
1079 if (func_id != BPF_FUNC_perf_event_read &&
1080 func_id != BPF_FUNC_perf_event_output)
1081 goto error;
1082 break;
1083 case BPF_MAP_TYPE_STACK_TRACE:
1084 if (func_id != BPF_FUNC_get_stackid)
1085 goto error;
1086 break;
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -07001087 case BPF_MAP_TYPE_CGROUP_ARRAY:
David S. Miller60747ef2016-08-18 01:17:32 -04001088 if (func_id != BPF_FUNC_skb_under_cgroup &&
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001089 func_id != BPF_FUNC_current_task_under_cgroup)
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07001090 goto error;
1091 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001092 default:
1093 break;
1094 }
1095
1096 /* ... and second from the function itself. */
1097 switch (func_id) {
1098 case BPF_FUNC_tail_call:
1099 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1100 goto error;
1101 break;
1102 case BPF_FUNC_perf_event_read:
1103 case BPF_FUNC_perf_event_output:
1104 if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
1105 goto error;
1106 break;
1107 case BPF_FUNC_get_stackid:
1108 if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
1109 goto error;
1110 break;
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001111 case BPF_FUNC_current_task_under_cgroup:
Daniel Borkmann747ea552016-08-12 22:17:17 +02001112 case BPF_FUNC_skb_under_cgroup:
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07001113 if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
1114 goto error;
1115 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001116 default:
1117 break;
Kaixu Xia35578d72015-08-06 07:02:35 +00001118 }
1119
1120 return 0;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001121error:
1122 verbose("cannot pass map_type %d into func %d\n",
1123 map->map_type, func_id);
1124 return -EINVAL;
Kaixu Xia35578d72015-08-06 07:02:35 +00001125}
1126
Daniel Borkmann435faee12016-04-13 00:10:51 +02001127static int check_raw_mode(const struct bpf_func_proto *fn)
1128{
1129 int count = 0;
1130
1131 if (fn->arg1_type == ARG_PTR_TO_RAW_STACK)
1132 count++;
1133 if (fn->arg2_type == ARG_PTR_TO_RAW_STACK)
1134 count++;
1135 if (fn->arg3_type == ARG_PTR_TO_RAW_STACK)
1136 count++;
1137 if (fn->arg4_type == ARG_PTR_TO_RAW_STACK)
1138 count++;
1139 if (fn->arg5_type == ARG_PTR_TO_RAW_STACK)
1140 count++;
1141
1142 return count > 1 ? -EINVAL : 0;
1143}
1144
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001145static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001146{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001147 struct bpf_verifier_state *state = &env->cur_state;
1148 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001149 int i;
1150
1151 for (i = 0; i < MAX_BPF_REG; i++)
1152 if (regs[i].type == PTR_TO_PACKET ||
1153 regs[i].type == PTR_TO_PACKET_END)
1154 mark_reg_unknown_value(regs, i);
1155
1156 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
1157 if (state->stack_slot_type[i] != STACK_SPILL)
1158 continue;
1159 reg = &state->spilled_regs[i / BPF_REG_SIZE];
1160 if (reg->type != PTR_TO_PACKET &&
1161 reg->type != PTR_TO_PACKET_END)
1162 continue;
1163 reg->type = UNKNOWN_VALUE;
1164 reg->imm = 0;
1165 }
1166}
1167
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001168static int check_call(struct bpf_verifier_env *env, int func_id)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001169{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001170 struct bpf_verifier_state *state = &env->cur_state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001171 const struct bpf_func_proto *fn = NULL;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001172 struct bpf_reg_state *regs = state->regs;
1173 struct bpf_reg_state *reg;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001174 struct bpf_call_arg_meta meta;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001175 bool changes_data;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001176 int i, err;
1177
1178 /* find function prototype */
1179 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
1180 verbose("invalid func %d\n", func_id);
1181 return -EINVAL;
1182 }
1183
1184 if (env->prog->aux->ops->get_func_proto)
1185 fn = env->prog->aux->ops->get_func_proto(func_id);
1186
1187 if (!fn) {
1188 verbose("unknown func %d\n", func_id);
1189 return -EINVAL;
1190 }
1191
1192 /* eBPF programs must be GPL compatible to use GPL-ed functions */
Daniel Borkmann24701ec2015-03-01 12:31:47 +01001193 if (!env->prog->gpl_compatible && fn->gpl_only) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001194 verbose("cannot call GPL only function from proprietary program\n");
1195 return -EINVAL;
1196 }
1197
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001198 changes_data = bpf_helper_changes_skb_data(fn->func);
1199
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001200 memset(&meta, 0, sizeof(meta));
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001201 meta.pkt_access = fn->pkt_access;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001202
Daniel Borkmann435faee12016-04-13 00:10:51 +02001203 /* We only support one arg being in raw mode at the moment, which
1204 * is sufficient for the helper functions we have right now.
1205 */
1206 err = check_raw_mode(fn);
1207 if (err) {
1208 verbose("kernel subsystem misconfigured func %d\n", func_id);
1209 return err;
1210 }
1211
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001212 /* check args */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001213 err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001214 if (err)
1215 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001216 err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001217 if (err)
1218 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001219 err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001220 if (err)
1221 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001222 err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001223 if (err)
1224 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001225 err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001226 if (err)
1227 return err;
1228
Daniel Borkmann435faee12016-04-13 00:10:51 +02001229 /* Mark slots with STACK_MISC in case of raw mode, stack offset
1230 * is inferred from register state.
1231 */
1232 for (i = 0; i < meta.access_size; i++) {
1233 err = check_mem_access(env, meta.regno, i, BPF_B, BPF_WRITE, -1);
1234 if (err)
1235 return err;
1236 }
1237
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001238 /* reset caller saved regs */
1239 for (i = 0; i < CALLER_SAVED_REGS; i++) {
1240 reg = regs + caller_saved[i];
1241 reg->type = NOT_INIT;
1242 reg->imm = 0;
1243 }
1244
1245 /* update return register */
1246 if (fn->ret_type == RET_INTEGER) {
1247 regs[BPF_REG_0].type = UNKNOWN_VALUE;
1248 } else if (fn->ret_type == RET_VOID) {
1249 regs[BPF_REG_0].type = NOT_INIT;
1250 } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) {
1251 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
Josef Bacik48461132016-09-28 10:54:32 -04001252 regs[BPF_REG_0].max_value = regs[BPF_REG_0].min_value = 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001253 /* remember map_ptr, so that check_map_access()
1254 * can check 'value_size' boundary of memory access
1255 * to map element returned from bpf_map_lookup_elem()
1256 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001257 if (meta.map_ptr == NULL) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001258 verbose("kernel subsystem misconfigured verifier\n");
1259 return -EINVAL;
1260 }
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001261 regs[BPF_REG_0].map_ptr = meta.map_ptr;
Thomas Graf14117072016-10-18 19:51:19 +02001262 regs[BPF_REG_0].id = ++env->id_gen;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001263 } else {
1264 verbose("unknown return type %d of func %d\n",
1265 fn->ret_type, func_id);
1266 return -EINVAL;
1267 }
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -07001268
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001269 err = check_map_func_compatibility(meta.map_ptr, func_id);
Kaixu Xia35578d72015-08-06 07:02:35 +00001270 if (err)
1271 return err;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -07001272
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001273 if (changes_data)
1274 clear_all_pkt_pointers(env);
1275 return 0;
1276}
1277
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001278static int check_packet_ptr_add(struct bpf_verifier_env *env,
1279 struct bpf_insn *insn)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001280{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001281 struct bpf_reg_state *regs = env->cur_state.regs;
1282 struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
1283 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
1284 struct bpf_reg_state tmp_reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001285 s32 imm;
1286
1287 if (BPF_SRC(insn->code) == BPF_K) {
1288 /* pkt_ptr += imm */
1289 imm = insn->imm;
1290
1291add_imm:
1292 if (imm <= 0) {
1293 verbose("addition of negative constant to packet pointer is not allowed\n");
1294 return -EACCES;
1295 }
1296 if (imm >= MAX_PACKET_OFF ||
1297 imm + dst_reg->off >= MAX_PACKET_OFF) {
1298 verbose("constant %d is too large to add to packet pointer\n",
1299 imm);
1300 return -EACCES;
1301 }
1302 /* a constant was added to pkt_ptr.
1303 * Remember it while keeping the same 'id'
1304 */
1305 dst_reg->off += imm;
1306 } else {
Alexei Starovoitov1b9b69e2016-05-19 18:17:14 -07001307 if (src_reg->type == PTR_TO_PACKET) {
1308 /* R6=pkt(id=0,off=0,r=62) R7=imm22; r7 += r6 */
1309 tmp_reg = *dst_reg; /* save r7 state */
1310 *dst_reg = *src_reg; /* copy pkt_ptr state r6 into r7 */
1311 src_reg = &tmp_reg; /* pretend it's src_reg state */
1312 /* if the checks below reject it, the copy won't matter,
1313 * since we're rejecting the whole program. If all ok,
1314 * then imm22 state will be added to r7
1315 * and r7 will be pkt(id=0,off=22,r=62) while
1316 * r6 will stay as pkt(id=0,off=0,r=62)
1317 */
1318 }
1319
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001320 if (src_reg->type == CONST_IMM) {
1321 /* pkt_ptr += reg where reg is known constant */
1322 imm = src_reg->imm;
1323 goto add_imm;
1324 }
1325 /* disallow pkt_ptr += reg
1326 * if reg is not uknown_value with guaranteed zero upper bits
1327 * otherwise pkt_ptr may overflow and addition will become
1328 * subtraction which is not allowed
1329 */
1330 if (src_reg->type != UNKNOWN_VALUE) {
1331 verbose("cannot add '%s' to ptr_to_packet\n",
1332 reg_type_str[src_reg->type]);
1333 return -EACCES;
1334 }
1335 if (src_reg->imm < 48) {
1336 verbose("cannot add integer value with %lld upper zero bits to ptr_to_packet\n",
1337 src_reg->imm);
1338 return -EACCES;
1339 }
1340 /* dst_reg stays as pkt_ptr type and since some positive
1341 * integer value was added to the pointer, increment its 'id'
1342 */
Jakub Kicinski1f415a72016-08-02 16:12:14 +01001343 dst_reg->id = ++env->id_gen;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001344
1345 /* something was added to pkt_ptr, set range and off to zero */
1346 dst_reg->off = 0;
1347 dst_reg->range = 0;
1348 }
1349 return 0;
1350}
1351
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001352static int evaluate_reg_alu(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001353{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001354 struct bpf_reg_state *regs = env->cur_state.regs;
1355 struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001356 u8 opcode = BPF_OP(insn->code);
1357 s64 imm_log2;
1358
1359 /* for type == UNKNOWN_VALUE:
1360 * imm > 0 -> number of zero upper bits
1361 * imm == 0 -> don't track which is the same as all bits can be non-zero
1362 */
1363
1364 if (BPF_SRC(insn->code) == BPF_X) {
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001365 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001366
1367 if (src_reg->type == UNKNOWN_VALUE && src_reg->imm > 0 &&
1368 dst_reg->imm && opcode == BPF_ADD) {
1369 /* dreg += sreg
1370 * where both have zero upper bits. Adding them
1371 * can only result making one more bit non-zero
1372 * in the larger value.
1373 * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47)
1374 * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47)
1375 */
1376 dst_reg->imm = min(dst_reg->imm, src_reg->imm);
1377 dst_reg->imm--;
1378 return 0;
1379 }
1380 if (src_reg->type == CONST_IMM && src_reg->imm > 0 &&
1381 dst_reg->imm && opcode == BPF_ADD) {
1382 /* dreg += sreg
1383 * where dreg has zero upper bits and sreg is const.
1384 * Adding them can only result making one more bit
1385 * non-zero in the larger value.
1386 */
1387 imm_log2 = __ilog2_u64((long long)src_reg->imm);
1388 dst_reg->imm = min(dst_reg->imm, 63 - imm_log2);
1389 dst_reg->imm--;
1390 return 0;
1391 }
1392 /* all other cases non supported yet, just mark dst_reg */
1393 dst_reg->imm = 0;
1394 return 0;
1395 }
1396
1397 /* sign extend 32-bit imm into 64-bit to make sure that
1398 * negative values occupy bit 63. Note ilog2() would have
1399 * been incorrect, since sizeof(insn->imm) == 4
1400 */
1401 imm_log2 = __ilog2_u64((long long)insn->imm);
1402
1403 if (dst_reg->imm && opcode == BPF_LSH) {
1404 /* reg <<= imm
1405 * if reg was a result of 2 byte load, then its imm == 48
1406 * which means that upper 48 bits are zero and shifting this reg
1407 * left by 4 would mean that upper 44 bits are still zero
1408 */
1409 dst_reg->imm -= insn->imm;
1410 } else if (dst_reg->imm && opcode == BPF_MUL) {
1411 /* reg *= imm
1412 * if multiplying by 14 subtract 4
1413 * This is conservative calculation of upper zero bits.
1414 * It's not trying to special case insn->imm == 1 or 0 cases
1415 */
1416 dst_reg->imm -= imm_log2 + 1;
1417 } else if (opcode == BPF_AND) {
1418 /* reg &= imm */
1419 dst_reg->imm = 63 - imm_log2;
1420 } else if (dst_reg->imm && opcode == BPF_ADD) {
1421 /* reg += imm */
1422 dst_reg->imm = min(dst_reg->imm, 63 - imm_log2);
1423 dst_reg->imm--;
1424 } else if (opcode == BPF_RSH) {
1425 /* reg >>= imm
1426 * which means that after right shift, upper bits will be zero
1427 * note that verifier already checked that
1428 * 0 <= imm < 64 for shift insn
1429 */
1430 dst_reg->imm += insn->imm;
1431 if (unlikely(dst_reg->imm > 64))
1432 /* some dumb code did:
1433 * r2 = *(u32 *)mem;
1434 * r2 >>= 32;
1435 * and all bits are zero now */
1436 dst_reg->imm = 64;
1437 } else {
1438 /* all other alu ops, means that we don't know what will
1439 * happen to the value, mark it with unknown number of zero bits
1440 */
1441 dst_reg->imm = 0;
1442 }
1443
1444 if (dst_reg->imm < 0) {
1445 /* all 64 bits of the register can contain non-zero bits
1446 * and such value cannot be added to ptr_to_packet, since it
1447 * may overflow, mark it as unknown to avoid further eval
1448 */
1449 dst_reg->imm = 0;
1450 }
1451 return 0;
1452}
1453
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001454static int evaluate_reg_imm_alu(struct bpf_verifier_env *env,
1455 struct bpf_insn *insn)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001456{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001457 struct bpf_reg_state *regs = env->cur_state.regs;
1458 struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
1459 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001460 u8 opcode = BPF_OP(insn->code);
1461
1462 /* dst_reg->type == CONST_IMM here, simulate execution of 'add' insn.
1463 * Don't care about overflow or negative values, just add them
1464 */
1465 if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_K)
1466 dst_reg->imm += insn->imm;
1467 else if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_X &&
1468 src_reg->type == CONST_IMM)
1469 dst_reg->imm += src_reg->imm;
1470 else
1471 mark_reg_unknown_value(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001472 return 0;
1473}
1474
Josef Bacik48461132016-09-28 10:54:32 -04001475static void check_reg_overflow(struct bpf_reg_state *reg)
1476{
1477 if (reg->max_value > BPF_REGISTER_MAX_RANGE)
1478 reg->max_value = BPF_REGISTER_MAX_RANGE;
Josef Bacikf23cc642016-11-14 15:45:36 -05001479 if (reg->min_value < BPF_REGISTER_MIN_RANGE ||
1480 reg->min_value > BPF_REGISTER_MAX_RANGE)
Josef Bacik48461132016-09-28 10:54:32 -04001481 reg->min_value = BPF_REGISTER_MIN_RANGE;
1482}
1483
1484static void adjust_reg_min_max_vals(struct bpf_verifier_env *env,
1485 struct bpf_insn *insn)
1486{
1487 struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg;
Josef Bacikf23cc642016-11-14 15:45:36 -05001488 s64 min_val = BPF_REGISTER_MIN_RANGE;
1489 u64 max_val = BPF_REGISTER_MAX_RANGE;
Josef Bacik48461132016-09-28 10:54:32 -04001490 bool min_set = false, max_set = false;
1491 u8 opcode = BPF_OP(insn->code);
1492
1493 dst_reg = &regs[insn->dst_reg];
1494 if (BPF_SRC(insn->code) == BPF_X) {
1495 check_reg_overflow(&regs[insn->src_reg]);
1496 min_val = regs[insn->src_reg].min_value;
1497 max_val = regs[insn->src_reg].max_value;
1498
1499 /* If the source register is a random pointer then the
1500 * min_value/max_value values represent the range of the known
1501 * accesses into that value, not the actual min/max value of the
1502 * register itself. In this case we have to reset the reg range
1503 * values so we know it is not safe to look at.
1504 */
1505 if (regs[insn->src_reg].type != CONST_IMM &&
1506 regs[insn->src_reg].type != UNKNOWN_VALUE) {
1507 min_val = BPF_REGISTER_MIN_RANGE;
1508 max_val = BPF_REGISTER_MAX_RANGE;
1509 }
1510 } else if (insn->imm < BPF_REGISTER_MAX_RANGE &&
1511 (s64)insn->imm > BPF_REGISTER_MIN_RANGE) {
1512 min_val = max_val = insn->imm;
1513 min_set = max_set = true;
1514 }
1515
1516 /* We don't know anything about what was done to this register, mark it
1517 * as unknown.
1518 */
1519 if (min_val == BPF_REGISTER_MIN_RANGE &&
1520 max_val == BPF_REGISTER_MAX_RANGE) {
1521 reset_reg_range_values(regs, insn->dst_reg);
1522 return;
1523 }
1524
Josef Bacikf23cc642016-11-14 15:45:36 -05001525 /* If one of our values was at the end of our ranges then we can't just
1526 * do our normal operations to the register, we need to set the values
1527 * to the min/max since they are undefined.
1528 */
1529 if (min_val == BPF_REGISTER_MIN_RANGE)
1530 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
1531 if (max_val == BPF_REGISTER_MAX_RANGE)
1532 dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
1533
Josef Bacik48461132016-09-28 10:54:32 -04001534 switch (opcode) {
1535 case BPF_ADD:
Josef Bacikf23cc642016-11-14 15:45:36 -05001536 if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
1537 dst_reg->min_value += min_val;
1538 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1539 dst_reg->max_value += max_val;
Josef Bacik48461132016-09-28 10:54:32 -04001540 break;
1541 case BPF_SUB:
Josef Bacikf23cc642016-11-14 15:45:36 -05001542 if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
1543 dst_reg->min_value -= min_val;
1544 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1545 dst_reg->max_value -= max_val;
Josef Bacik48461132016-09-28 10:54:32 -04001546 break;
1547 case BPF_MUL:
Josef Bacikf23cc642016-11-14 15:45:36 -05001548 if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
1549 dst_reg->min_value *= min_val;
1550 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1551 dst_reg->max_value *= max_val;
Josef Bacik48461132016-09-28 10:54:32 -04001552 break;
1553 case BPF_AND:
Josef Bacikf23cc642016-11-14 15:45:36 -05001554 /* Disallow AND'ing of negative numbers, ain't nobody got time
1555 * for that. Otherwise the minimum is 0 and the max is the max
1556 * value we could AND against.
1557 */
1558 if (min_val < 0)
1559 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
1560 else
1561 dst_reg->min_value = 0;
Josef Bacik48461132016-09-28 10:54:32 -04001562 dst_reg->max_value = max_val;
1563 break;
1564 case BPF_LSH:
1565 /* Gotta have special overflow logic here, if we're shifting
1566 * more than MAX_RANGE then just assume we have an invalid
1567 * range.
1568 */
1569 if (min_val > ilog2(BPF_REGISTER_MAX_RANGE))
1570 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
Josef Bacikf23cc642016-11-14 15:45:36 -05001571 else if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
Josef Bacik48461132016-09-28 10:54:32 -04001572 dst_reg->min_value <<= min_val;
1573
1574 if (max_val > ilog2(BPF_REGISTER_MAX_RANGE))
1575 dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
Josef Bacikf23cc642016-11-14 15:45:36 -05001576 else if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
Josef Bacik48461132016-09-28 10:54:32 -04001577 dst_reg->max_value <<= max_val;
1578 break;
1579 case BPF_RSH:
Josef Bacikf23cc642016-11-14 15:45:36 -05001580 /* RSH by a negative number is undefined, and the BPF_RSH is an
1581 * unsigned shift, so make the appropriate casts.
Josef Bacik48461132016-09-28 10:54:32 -04001582 */
Josef Bacikf23cc642016-11-14 15:45:36 -05001583 if (min_val < 0 || dst_reg->min_value < 0)
1584 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
1585 else
1586 dst_reg->min_value =
1587 (u64)(dst_reg->min_value) >> min_val;
1588 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1589 dst_reg->max_value >>= max_val;
Josef Bacik48461132016-09-28 10:54:32 -04001590 break;
1591 default:
1592 reset_reg_range_values(regs, insn->dst_reg);
1593 break;
1594 }
1595
1596 check_reg_overflow(dst_reg);
1597}
1598
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001599/* check validity of 32-bit and 64-bit arithmetic operations */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001600static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001601{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001602 struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001603 u8 opcode = BPF_OP(insn->code);
1604 int err;
1605
1606 if (opcode == BPF_END || opcode == BPF_NEG) {
1607 if (opcode == BPF_NEG) {
1608 if (BPF_SRC(insn->code) != 0 ||
1609 insn->src_reg != BPF_REG_0 ||
1610 insn->off != 0 || insn->imm != 0) {
1611 verbose("BPF_NEG uses reserved fields\n");
1612 return -EINVAL;
1613 }
1614 } else {
1615 if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
1616 (insn->imm != 16 && insn->imm != 32 && insn->imm != 64)) {
1617 verbose("BPF_END uses reserved fields\n");
1618 return -EINVAL;
1619 }
1620 }
1621
1622 /* check src operand */
1623 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
1624 if (err)
1625 return err;
1626
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001627 if (is_pointer_value(env, insn->dst_reg)) {
1628 verbose("R%d pointer arithmetic prohibited\n",
1629 insn->dst_reg);
1630 return -EACCES;
1631 }
1632
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001633 /* check dest operand */
1634 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
1635 if (err)
1636 return err;
1637
1638 } else if (opcode == BPF_MOV) {
1639
1640 if (BPF_SRC(insn->code) == BPF_X) {
1641 if (insn->imm != 0 || insn->off != 0) {
1642 verbose("BPF_MOV uses reserved fields\n");
1643 return -EINVAL;
1644 }
1645
1646 /* check src operand */
1647 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1648 if (err)
1649 return err;
1650 } else {
1651 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
1652 verbose("BPF_MOV uses reserved fields\n");
1653 return -EINVAL;
1654 }
1655 }
1656
1657 /* check dest operand */
1658 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
1659 if (err)
1660 return err;
1661
Josef Bacik48461132016-09-28 10:54:32 -04001662 /* we are setting our register to something new, we need to
1663 * reset its range values.
1664 */
1665 reset_reg_range_values(regs, insn->dst_reg);
1666
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001667 if (BPF_SRC(insn->code) == BPF_X) {
1668 if (BPF_CLASS(insn->code) == BPF_ALU64) {
1669 /* case: R1 = R2
1670 * copy register state to dest reg
1671 */
1672 regs[insn->dst_reg] = regs[insn->src_reg];
1673 } else {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001674 if (is_pointer_value(env, insn->src_reg)) {
1675 verbose("R%d partial copy of pointer\n",
1676 insn->src_reg);
1677 return -EACCES;
1678 }
Thomas Graf14117072016-10-18 19:51:19 +02001679 mark_reg_unknown_value(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001680 }
1681 } else {
1682 /* case: R = imm
1683 * remember the value we stored into this reg
1684 */
1685 regs[insn->dst_reg].type = CONST_IMM;
1686 regs[insn->dst_reg].imm = insn->imm;
Josef Bacik48461132016-09-28 10:54:32 -04001687 regs[insn->dst_reg].max_value = insn->imm;
1688 regs[insn->dst_reg].min_value = insn->imm;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001689 }
1690
1691 } else if (opcode > BPF_END) {
1692 verbose("invalid BPF_ALU opcode %x\n", opcode);
1693 return -EINVAL;
1694
1695 } else { /* all other ALU ops: and, sub, xor, add, ... */
1696
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001697 if (BPF_SRC(insn->code) == BPF_X) {
1698 if (insn->imm != 0 || insn->off != 0) {
1699 verbose("BPF_ALU uses reserved fields\n");
1700 return -EINVAL;
1701 }
1702 /* check src1 operand */
1703 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1704 if (err)
1705 return err;
1706 } else {
1707 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
1708 verbose("BPF_ALU uses reserved fields\n");
1709 return -EINVAL;
1710 }
1711 }
1712
1713 /* check src2 operand */
1714 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
1715 if (err)
1716 return err;
1717
1718 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
1719 BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
1720 verbose("div by zero\n");
1721 return -EINVAL;
1722 }
1723
Rabin Vincent229394e2016-01-12 20:17:08 +01001724 if ((opcode == BPF_LSH || opcode == BPF_RSH ||
1725 opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
1726 int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
1727
1728 if (insn->imm < 0 || insn->imm >= size) {
1729 verbose("invalid shift %d\n", insn->imm);
1730 return -EINVAL;
1731 }
1732 }
1733
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07001734 /* check dest operand */
1735 err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
1736 if (err)
1737 return err;
1738
1739 dst_reg = &regs[insn->dst_reg];
1740
Josef Bacik48461132016-09-28 10:54:32 -04001741 /* first we want to adjust our ranges. */
1742 adjust_reg_min_max_vals(env, insn);
1743
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001744 /* pattern match 'bpf_add Rx, imm' instruction */
1745 if (opcode == BPF_ADD && BPF_CLASS(insn->code) == BPF_ALU64 &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07001746 dst_reg->type == FRAME_PTR && BPF_SRC(insn->code) == BPF_K) {
1747 dst_reg->type = PTR_TO_STACK;
1748 dst_reg->imm = insn->imm;
1749 return 0;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001750 } else if (opcode == BPF_ADD &&
1751 BPF_CLASS(insn->code) == BPF_ALU64 &&
Yonghong Song7bca0a92017-04-29 22:52:42 -07001752 dst_reg->type == PTR_TO_STACK &&
1753 ((BPF_SRC(insn->code) == BPF_X &&
1754 regs[insn->src_reg].type == CONST_IMM) ||
1755 BPF_SRC(insn->code) == BPF_K)) {
1756 if (BPF_SRC(insn->code) == BPF_X)
1757 dst_reg->imm += regs[insn->src_reg].imm;
1758 else
1759 dst_reg->imm += insn->imm;
1760 return 0;
1761 } else if (opcode == BPF_ADD &&
1762 BPF_CLASS(insn->code) == BPF_ALU64 &&
Alexei Starovoitov1b9b69e2016-05-19 18:17:14 -07001763 (dst_reg->type == PTR_TO_PACKET ||
1764 (BPF_SRC(insn->code) == BPF_X &&
1765 regs[insn->src_reg].type == PTR_TO_PACKET))) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001766 /* ptr_to_packet += K|X */
1767 return check_packet_ptr_add(env, insn);
1768 } else if (BPF_CLASS(insn->code) == BPF_ALU64 &&
1769 dst_reg->type == UNKNOWN_VALUE &&
1770 env->allow_ptr_leaks) {
1771 /* unknown += K|X */
1772 return evaluate_reg_alu(env, insn);
1773 } else if (BPF_CLASS(insn->code) == BPF_ALU64 &&
1774 dst_reg->type == CONST_IMM &&
1775 env->allow_ptr_leaks) {
1776 /* reg_imm += K|X */
1777 return evaluate_reg_imm_alu(env, insn);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001778 } else if (is_pointer_value(env, insn->dst_reg)) {
1779 verbose("R%d pointer arithmetic prohibited\n",
1780 insn->dst_reg);
1781 return -EACCES;
1782 } else if (BPF_SRC(insn->code) == BPF_X &&
1783 is_pointer_value(env, insn->src_reg)) {
1784 verbose("R%d pointer arithmetic prohibited\n",
1785 insn->src_reg);
1786 return -EACCES;
1787 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001788
Josef Bacik48461132016-09-28 10:54:32 -04001789 /* If we did pointer math on a map value then just set it to our
1790 * PTR_TO_MAP_VALUE_ADJ type so we can deal with any stores or
1791 * loads to this register appropriately, otherwise just mark the
1792 * register as unknown.
1793 */
1794 if (env->allow_ptr_leaks &&
1795 (dst_reg->type == PTR_TO_MAP_VALUE ||
1796 dst_reg->type == PTR_TO_MAP_VALUE_ADJ))
1797 dst_reg->type = PTR_TO_MAP_VALUE_ADJ;
1798 else
1799 mark_reg_unknown_value(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001800 }
1801
1802 return 0;
1803}
1804
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001805static void find_good_pkt_pointers(struct bpf_verifier_state *state,
1806 struct bpf_reg_state *dst_reg)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001807{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001808 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001809 int i;
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02001810
1811 /* LLVM can generate two kind of checks:
1812 *
1813 * Type 1:
1814 *
1815 * r2 = r3;
1816 * r2 += 8;
1817 * if (r2 > pkt_end) goto <handle exception>
1818 * <access okay>
1819 *
1820 * Where:
1821 * r2 == dst_reg, pkt_end == src_reg
1822 * r2=pkt(id=n,off=8,r=0)
1823 * r3=pkt(id=n,off=0,r=0)
1824 *
1825 * Type 2:
1826 *
1827 * r2 = r3;
1828 * r2 += 8;
1829 * if (pkt_end >= r2) goto <access okay>
1830 * <handle exception>
1831 *
1832 * Where:
1833 * pkt_end == dst_reg, r2 == src_reg
1834 * r2=pkt(id=n,off=8,r=0)
1835 * r3=pkt(id=n,off=0,r=0)
1836 *
1837 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
1838 * so that range of bytes [r3, r3 + 8) is safe to access.
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001839 */
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02001840
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001841 for (i = 0; i < MAX_BPF_REG; i++)
1842 if (regs[i].type == PTR_TO_PACKET && regs[i].id == dst_reg->id)
Alexei Starovoitov0ea3c232017-03-24 15:57:33 -07001843 /* keep the maximum range already checked */
1844 regs[i].range = max(regs[i].range, dst_reg->off);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001845
1846 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
1847 if (state->stack_slot_type[i] != STACK_SPILL)
1848 continue;
1849 reg = &state->spilled_regs[i / BPF_REG_SIZE];
1850 if (reg->type == PTR_TO_PACKET && reg->id == dst_reg->id)
Alexei Starovoitov0ea3c232017-03-24 15:57:33 -07001851 reg->range = max(reg->range, dst_reg->off);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001852 }
1853}
1854
Josef Bacik48461132016-09-28 10:54:32 -04001855/* Adjusts the register min/max values in the case that the dst_reg is the
1856 * variable register that we are working on, and src_reg is a constant or we're
1857 * simply doing a BPF_K check.
1858 */
1859static void reg_set_min_max(struct bpf_reg_state *true_reg,
1860 struct bpf_reg_state *false_reg, u64 val,
1861 u8 opcode)
1862{
1863 switch (opcode) {
1864 case BPF_JEQ:
1865 /* If this is false then we know nothing Jon Snow, but if it is
1866 * true then we know for sure.
1867 */
1868 true_reg->max_value = true_reg->min_value = val;
1869 break;
1870 case BPF_JNE:
1871 /* If this is true we know nothing Jon Snow, but if it is false
1872 * we know the value for sure;
1873 */
1874 false_reg->max_value = false_reg->min_value = val;
1875 break;
1876 case BPF_JGT:
1877 /* Unsigned comparison, the minimum value is 0. */
1878 false_reg->min_value = 0;
1879 case BPF_JSGT:
1880 /* If this is false then we know the maximum val is val,
1881 * otherwise we know the min val is val+1.
1882 */
1883 false_reg->max_value = val;
1884 true_reg->min_value = val + 1;
1885 break;
1886 case BPF_JGE:
1887 /* Unsigned comparison, the minimum value is 0. */
1888 false_reg->min_value = 0;
1889 case BPF_JSGE:
1890 /* If this is false then we know the maximum value is val - 1,
1891 * otherwise we know the mimimum value is val.
1892 */
1893 false_reg->max_value = val - 1;
1894 true_reg->min_value = val;
1895 break;
1896 default:
1897 break;
1898 }
1899
1900 check_reg_overflow(false_reg);
1901 check_reg_overflow(true_reg);
1902}
1903
1904/* Same as above, but for the case that dst_reg is a CONST_IMM reg and src_reg
1905 * is the variable reg.
1906 */
1907static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
1908 struct bpf_reg_state *false_reg, u64 val,
1909 u8 opcode)
1910{
1911 switch (opcode) {
1912 case BPF_JEQ:
1913 /* If this is false then we know nothing Jon Snow, but if it is
1914 * true then we know for sure.
1915 */
1916 true_reg->max_value = true_reg->min_value = val;
1917 break;
1918 case BPF_JNE:
1919 /* If this is true we know nothing Jon Snow, but if it is false
1920 * we know the value for sure;
1921 */
1922 false_reg->max_value = false_reg->min_value = val;
1923 break;
1924 case BPF_JGT:
1925 /* Unsigned comparison, the minimum value is 0. */
1926 true_reg->min_value = 0;
1927 case BPF_JSGT:
1928 /*
1929 * If this is false, then the val is <= the register, if it is
1930 * true the register <= to the val.
1931 */
1932 false_reg->min_value = val;
1933 true_reg->max_value = val - 1;
1934 break;
1935 case BPF_JGE:
1936 /* Unsigned comparison, the minimum value is 0. */
1937 true_reg->min_value = 0;
1938 case BPF_JSGE:
1939 /* If this is false then constant < register, if it is true then
1940 * the register < constant.
1941 */
1942 false_reg->min_value = val + 1;
1943 true_reg->max_value = val;
1944 break;
1945 default:
1946 break;
1947 }
1948
1949 check_reg_overflow(false_reg);
1950 check_reg_overflow(true_reg);
1951}
1952
Thomas Graf14117072016-10-18 19:51:19 +02001953static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id,
1954 enum bpf_reg_type type)
1955{
1956 struct bpf_reg_state *reg = &regs[regno];
1957
1958 if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) {
1959 reg->type = type;
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01001960 /* We don't need id from this point onwards anymore, thus we
1961 * should better reset it, so that state pruning has chances
1962 * to take effect.
1963 */
1964 reg->id = 0;
Thomas Graf14117072016-10-18 19:51:19 +02001965 if (type == UNKNOWN_VALUE)
Daniel Borkmann0e0f1d62016-12-18 01:52:59 +01001966 __mark_reg_unknown_value(regs, regno);
Thomas Graf14117072016-10-18 19:51:19 +02001967 }
1968}
1969
1970/* The logic is similar to find_good_pkt_pointers(), both could eventually
1971 * be folded together at some point.
1972 */
1973static void mark_map_regs(struct bpf_verifier_state *state, u32 regno,
1974 enum bpf_reg_type type)
1975{
1976 struct bpf_reg_state *regs = state->regs;
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01001977 u32 id = regs[regno].id;
Thomas Graf14117072016-10-18 19:51:19 +02001978 int i;
1979
1980 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01001981 mark_map_reg(regs, i, id, type);
Thomas Graf14117072016-10-18 19:51:19 +02001982
1983 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
1984 if (state->stack_slot_type[i] != STACK_SPILL)
1985 continue;
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01001986 mark_map_reg(state->spilled_regs, i / BPF_REG_SIZE, id, type);
Thomas Graf14117072016-10-18 19:51:19 +02001987 }
1988}
1989
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001990static int check_cond_jmp_op(struct bpf_verifier_env *env,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001991 struct bpf_insn *insn, int *insn_idx)
1992{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001993 struct bpf_verifier_state *other_branch, *this_branch = &env->cur_state;
1994 struct bpf_reg_state *regs = this_branch->regs, *dst_reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001995 u8 opcode = BPF_OP(insn->code);
1996 int err;
1997
1998 if (opcode > BPF_EXIT) {
1999 verbose("invalid BPF_JMP opcode %x\n", opcode);
2000 return -EINVAL;
2001 }
2002
2003 if (BPF_SRC(insn->code) == BPF_X) {
2004 if (insn->imm != 0) {
2005 verbose("BPF_JMP uses reserved fields\n");
2006 return -EINVAL;
2007 }
2008
2009 /* check src1 operand */
2010 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2011 if (err)
2012 return err;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002013
2014 if (is_pointer_value(env, insn->src_reg)) {
2015 verbose("R%d pointer comparison prohibited\n",
2016 insn->src_reg);
2017 return -EACCES;
2018 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002019 } else {
2020 if (insn->src_reg != BPF_REG_0) {
2021 verbose("BPF_JMP uses reserved fields\n");
2022 return -EINVAL;
2023 }
2024 }
2025
2026 /* check src2 operand */
2027 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
2028 if (err)
2029 return err;
2030
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002031 dst_reg = &regs[insn->dst_reg];
2032
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002033 /* detect if R == 0 where R was initialized to zero earlier */
2034 if (BPF_SRC(insn->code) == BPF_K &&
2035 (opcode == BPF_JEQ || opcode == BPF_JNE) &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002036 dst_reg->type == CONST_IMM && dst_reg->imm == insn->imm) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002037 if (opcode == BPF_JEQ) {
2038 /* if (imm == imm) goto pc+off;
2039 * only follow the goto, ignore fall-through
2040 */
2041 *insn_idx += insn->off;
2042 return 0;
2043 } else {
2044 /* if (imm != imm) goto pc+off;
2045 * only follow fall-through branch, since
2046 * that's where the program will go
2047 */
2048 return 0;
2049 }
2050 }
2051
2052 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx);
2053 if (!other_branch)
2054 return -EFAULT;
2055
Josef Bacik48461132016-09-28 10:54:32 -04002056 /* detect if we are comparing against a constant value so we can adjust
2057 * our min/max values for our dst register.
2058 */
2059 if (BPF_SRC(insn->code) == BPF_X) {
2060 if (regs[insn->src_reg].type == CONST_IMM)
2061 reg_set_min_max(&other_branch->regs[insn->dst_reg],
2062 dst_reg, regs[insn->src_reg].imm,
2063 opcode);
2064 else if (dst_reg->type == CONST_IMM)
2065 reg_set_min_max_inv(&other_branch->regs[insn->src_reg],
2066 &regs[insn->src_reg], dst_reg->imm,
2067 opcode);
2068 } else {
2069 reg_set_min_max(&other_branch->regs[insn->dst_reg],
2070 dst_reg, insn->imm, opcode);
2071 }
2072
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002073 /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002074 if (BPF_SRC(insn->code) == BPF_K &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002075 insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
2076 dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
Thomas Graf14117072016-10-18 19:51:19 +02002077 /* Mark all identical map registers in each branch as either
2078 * safe or unknown depending R == 0 or R != 0 conditional.
2079 */
2080 mark_map_regs(this_branch, insn->dst_reg,
2081 opcode == BPF_JEQ ? PTR_TO_MAP_VALUE : UNKNOWN_VALUE);
2082 mark_map_regs(other_branch, insn->dst_reg,
2083 opcode == BPF_JEQ ? UNKNOWN_VALUE : PTR_TO_MAP_VALUE);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002084 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT &&
2085 dst_reg->type == PTR_TO_PACKET &&
2086 regs[insn->src_reg].type == PTR_TO_PACKET_END) {
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002087 find_good_pkt_pointers(this_branch, dst_reg);
2088 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGE &&
2089 dst_reg->type == PTR_TO_PACKET_END &&
2090 regs[insn->src_reg].type == PTR_TO_PACKET) {
2091 find_good_pkt_pointers(other_branch, &regs[insn->src_reg]);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002092 } else if (is_pointer_value(env, insn->dst_reg)) {
2093 verbose("R%d pointer comparison prohibited\n", insn->dst_reg);
2094 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002095 }
2096 if (log_level)
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002097 print_verifier_state(this_branch);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002098 return 0;
2099}
2100
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002101/* return the map pointer stored inside BPF_LD_IMM64 instruction */
2102static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
2103{
2104 u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
2105
2106 return (struct bpf_map *) (unsigned long) imm64;
2107}
2108
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002109/* verify BPF_LD_IMM64 instruction */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002110static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002111{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002112 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002113 int err;
2114
2115 if (BPF_SIZE(insn->code) != BPF_DW) {
2116 verbose("invalid BPF_LD_IMM insn\n");
2117 return -EINVAL;
2118 }
2119 if (insn->off != 0) {
2120 verbose("BPF_LD_IMM64 uses reserved fields\n");
2121 return -EINVAL;
2122 }
2123
2124 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
2125 if (err)
2126 return err;
2127
Jakub Kicinski6b173872016-09-21 11:43:59 +01002128 if (insn->src_reg == 0) {
2129 /* generic move 64-bit immediate into a register,
2130 * only analyzer needs to collect the ld_imm value.
2131 */
2132 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
2133
2134 if (!env->analyzer_ops)
2135 return 0;
2136
2137 regs[insn->dst_reg].type = CONST_IMM;
2138 regs[insn->dst_reg].imm = imm;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002139 return 0;
Jakub Kicinski6b173872016-09-21 11:43:59 +01002140 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002141
2142 /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
2143 BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
2144
2145 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
2146 regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
2147 return 0;
2148}
2149
Daniel Borkmann96be4322015-03-01 12:31:46 +01002150static bool may_access_skb(enum bpf_prog_type type)
2151{
2152 switch (type) {
2153 case BPF_PROG_TYPE_SOCKET_FILTER:
2154 case BPF_PROG_TYPE_SCHED_CLS:
Daniel Borkmann94caee82015-03-20 15:11:11 +01002155 case BPF_PROG_TYPE_SCHED_ACT:
Daniel Borkmann96be4322015-03-01 12:31:46 +01002156 return true;
2157 default:
2158 return false;
2159 }
2160}
2161
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002162/* verify safety of LD_ABS|LD_IND instructions:
2163 * - they can only appear in the programs where ctx == skb
2164 * - since they are wrappers of function calls, they scratch R1-R5 registers,
2165 * preserve R6-R9, and store return value into R0
2166 *
2167 * Implicit input:
2168 * ctx == skb == R6 == CTX
2169 *
2170 * Explicit input:
2171 * SRC == any register
2172 * IMM == 32-bit immediate
2173 *
2174 * Output:
2175 * R0 - 8/16/32-bit skb data converted to cpu endianness
2176 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002177static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002178{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002179 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002180 u8 mode = BPF_MODE(insn->code);
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002181 struct bpf_reg_state *reg;
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002182 int i, err;
2183
Daniel Borkmann24701ec2015-03-01 12:31:47 +01002184 if (!may_access_skb(env->prog->type)) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002185 verbose("BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002186 return -EINVAL;
2187 }
2188
2189 if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
Alexei Starovoitovd82bccc2016-04-12 10:26:19 -07002190 BPF_SIZE(insn->code) == BPF_DW ||
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002191 (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002192 verbose("BPF_LD_[ABS|IND] uses reserved fields\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002193 return -EINVAL;
2194 }
2195
2196 /* check whether implicit source operand (register R6) is readable */
2197 err = check_reg_arg(regs, BPF_REG_6, SRC_OP);
2198 if (err)
2199 return err;
2200
2201 if (regs[BPF_REG_6].type != PTR_TO_CTX) {
2202 verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
2203 return -EINVAL;
2204 }
2205
2206 if (mode == BPF_IND) {
2207 /* check explicit source operand */
2208 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2209 if (err)
2210 return err;
2211 }
2212
2213 /* reset caller saved regs to unreadable */
2214 for (i = 0; i < CALLER_SAVED_REGS; i++) {
2215 reg = regs + caller_saved[i];
2216 reg->type = NOT_INIT;
2217 reg->imm = 0;
2218 }
2219
2220 /* mark destination R0 register as readable, since it contains
2221 * the value fetched from the packet
2222 */
2223 regs[BPF_REG_0].type = UNKNOWN_VALUE;
2224 return 0;
2225}
2226
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002227/* non-recursive DFS pseudo code
2228 * 1 procedure DFS-iterative(G,v):
2229 * 2 label v as discovered
2230 * 3 let S be a stack
2231 * 4 S.push(v)
2232 * 5 while S is not empty
2233 * 6 t <- S.pop()
2234 * 7 if t is what we're looking for:
2235 * 8 return t
2236 * 9 for all edges e in G.adjacentEdges(t) do
2237 * 10 if edge e is already labelled
2238 * 11 continue with the next edge
2239 * 12 w <- G.adjacentVertex(t,e)
2240 * 13 if vertex w is not discovered and not explored
2241 * 14 label e as tree-edge
2242 * 15 label w as discovered
2243 * 16 S.push(w)
2244 * 17 continue at 5
2245 * 18 else if vertex w is discovered
2246 * 19 label e as back-edge
2247 * 20 else
2248 * 21 // vertex w is explored
2249 * 22 label e as forward- or cross-edge
2250 * 23 label t as explored
2251 * 24 S.pop()
2252 *
2253 * convention:
2254 * 0x10 - discovered
2255 * 0x11 - discovered and fall-through edge labelled
2256 * 0x12 - discovered and fall-through and branch edges labelled
2257 * 0x20 - explored
2258 */
2259
2260enum {
2261 DISCOVERED = 0x10,
2262 EXPLORED = 0x20,
2263 FALLTHROUGH = 1,
2264 BRANCH = 2,
2265};
2266
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002267#define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002268
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002269static int *insn_stack; /* stack of insns to process */
2270static int cur_stack; /* current stack index */
2271static int *insn_state;
2272
2273/* t, w, e - match pseudo-code above:
2274 * t - index of current instruction
2275 * w - next instruction
2276 * e - edge
2277 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002278static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002279{
2280 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
2281 return 0;
2282
2283 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
2284 return 0;
2285
2286 if (w < 0 || w >= env->prog->len) {
2287 verbose("jump out of range from insn %d to %d\n", t, w);
2288 return -EINVAL;
2289 }
2290
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002291 if (e == BRANCH)
2292 /* mark branch target for state pruning */
2293 env->explored_states[w] = STATE_LIST_MARK;
2294
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002295 if (insn_state[w] == 0) {
2296 /* tree-edge */
2297 insn_state[t] = DISCOVERED | e;
2298 insn_state[w] = DISCOVERED;
2299 if (cur_stack >= env->prog->len)
2300 return -E2BIG;
2301 insn_stack[cur_stack++] = w;
2302 return 1;
2303 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
2304 verbose("back-edge from insn %d to %d\n", t, w);
2305 return -EINVAL;
2306 } else if (insn_state[w] == EXPLORED) {
2307 /* forward- or cross-edge */
2308 insn_state[t] = DISCOVERED | e;
2309 } else {
2310 verbose("insn state internal bug\n");
2311 return -EFAULT;
2312 }
2313 return 0;
2314}
2315
2316/* non-recursive depth-first-search to detect loops in BPF program
2317 * loop == back-edge in directed graph
2318 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002319static int check_cfg(struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002320{
2321 struct bpf_insn *insns = env->prog->insnsi;
2322 int insn_cnt = env->prog->len;
2323 int ret = 0;
2324 int i, t;
2325
2326 insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
2327 if (!insn_state)
2328 return -ENOMEM;
2329
2330 insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
2331 if (!insn_stack) {
2332 kfree(insn_state);
2333 return -ENOMEM;
2334 }
2335
2336 insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
2337 insn_stack[0] = 0; /* 0 is the first instruction */
2338 cur_stack = 1;
2339
2340peek_stack:
2341 if (cur_stack == 0)
2342 goto check_state;
2343 t = insn_stack[cur_stack - 1];
2344
2345 if (BPF_CLASS(insns[t].code) == BPF_JMP) {
2346 u8 opcode = BPF_OP(insns[t].code);
2347
2348 if (opcode == BPF_EXIT) {
2349 goto mark_explored;
2350 } else if (opcode == BPF_CALL) {
2351 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2352 if (ret == 1)
2353 goto peek_stack;
2354 else if (ret < 0)
2355 goto err_free;
Daniel Borkmann07016152016-04-05 22:33:17 +02002356 if (t + 1 < insn_cnt)
2357 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002358 } else if (opcode == BPF_JA) {
2359 if (BPF_SRC(insns[t].code) != BPF_K) {
2360 ret = -EINVAL;
2361 goto err_free;
2362 }
2363 /* unconditional jump with single edge */
2364 ret = push_insn(t, t + insns[t].off + 1,
2365 FALLTHROUGH, env);
2366 if (ret == 1)
2367 goto peek_stack;
2368 else if (ret < 0)
2369 goto err_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002370 /* tell verifier to check for equivalent states
2371 * after every call and jump
2372 */
Alexei Starovoitovc3de6312015-04-14 15:57:13 -07002373 if (t + 1 < insn_cnt)
2374 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002375 } else {
2376 /* conditional jump with two edges */
2377 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2378 if (ret == 1)
2379 goto peek_stack;
2380 else if (ret < 0)
2381 goto err_free;
2382
2383 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
2384 if (ret == 1)
2385 goto peek_stack;
2386 else if (ret < 0)
2387 goto err_free;
2388 }
2389 } else {
2390 /* all other non-branch instructions with single
2391 * fall-through edge
2392 */
2393 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2394 if (ret == 1)
2395 goto peek_stack;
2396 else if (ret < 0)
2397 goto err_free;
2398 }
2399
2400mark_explored:
2401 insn_state[t] = EXPLORED;
2402 if (cur_stack-- <= 0) {
2403 verbose("pop stack internal bug\n");
2404 ret = -EFAULT;
2405 goto err_free;
2406 }
2407 goto peek_stack;
2408
2409check_state:
2410 for (i = 0; i < insn_cnt; i++) {
2411 if (insn_state[i] != EXPLORED) {
2412 verbose("unreachable insn %d\n", i);
2413 ret = -EINVAL;
2414 goto err_free;
2415 }
2416 }
2417 ret = 0; /* cfg looks good */
2418
2419err_free:
2420 kfree(insn_state);
2421 kfree(insn_stack);
2422 return ret;
2423}
2424
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002425/* the following conditions reduce the number of explored insns
2426 * from ~140k to ~80k for ultra large programs that use a lot of ptr_to_packet
2427 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002428static bool compare_ptrs_to_packet(struct bpf_reg_state *old,
2429 struct bpf_reg_state *cur)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002430{
2431 if (old->id != cur->id)
2432 return false;
2433
2434 /* old ptr_to_packet is more conservative, since it allows smaller
2435 * range. Ex:
2436 * old(off=0,r=10) is equal to cur(off=0,r=20), because
2437 * old(off=0,r=10) means that with range=10 the verifier proceeded
2438 * further and found no issues with the program. Now we're in the same
2439 * spot with cur(off=0,r=20), so we're safe too, since anything further
2440 * will only be looking at most 10 bytes after this pointer.
2441 */
2442 if (old->off == cur->off && old->range < cur->range)
2443 return true;
2444
2445 /* old(off=20,r=10) is equal to cur(off=22,re=22 or 5 or 0)
2446 * since both cannot be used for packet access and safe(old)
2447 * pointer has smaller off that could be used for further
2448 * 'if (ptr > data_end)' check
2449 * Ex:
2450 * old(off=20,r=10) and cur(off=22,r=22) and cur(off=22,r=0) mean
2451 * that we cannot access the packet.
2452 * The safe range is:
2453 * [ptr, ptr + range - off)
2454 * so whenever off >=range, it means no safe bytes from this pointer.
2455 * When comparing old->off <= cur->off, it means that older code
2456 * went with smaller offset and that offset was later
2457 * used to figure out the safe range after 'if (ptr > data_end)' check
2458 * Say, 'old' state was explored like:
2459 * ... R3(off=0, r=0)
2460 * R4 = R3 + 20
2461 * ... now R4(off=20,r=0) <-- here
2462 * if (R4 > data_end)
2463 * ... R4(off=20,r=20), R3(off=0,r=20) and R3 can be used to access.
2464 * ... the code further went all the way to bpf_exit.
2465 * Now the 'cur' state at the mark 'here' has R4(off=30,r=0).
2466 * old_R4(off=20,r=0) equal to cur_R4(off=30,r=0), since if the verifier
2467 * goes further, such cur_R4 will give larger safe packet range after
2468 * 'if (R4 > data_end)' and all further insn were already good with r=20,
2469 * so they will be good with r=30 and we can prune the search.
2470 */
2471 if (old->off <= cur->off &&
2472 old->off >= old->range && cur->off >= cur->range)
2473 return true;
2474
2475 return false;
2476}
2477
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002478/* compare two verifier states
2479 *
2480 * all states stored in state_list are known to be valid, since
2481 * verifier reached 'bpf_exit' instruction through them
2482 *
2483 * this function is called when verifier exploring different branches of
2484 * execution popped from the state stack. If it sees an old state that has
2485 * more strict register state and more strict stack state then this execution
2486 * branch doesn't need to be explored further, since verifier already
2487 * concluded that more strict state leads to valid finish.
2488 *
2489 * Therefore two states are equivalent if register state is more conservative
2490 * and explored stack state is more conservative than the current one.
2491 * Example:
2492 * explored current
2493 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
2494 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
2495 *
2496 * In other words if current stack state (one being explored) has more
2497 * valid slots than old one that already passed validation, it means
2498 * the verifier can stop exploring and conclude that current state is valid too
2499 *
2500 * Similarly with registers. If explored state has register type as invalid
2501 * whereas register type in current state is meaningful, it means that
2502 * the current state will reach 'bpf_exit' instruction safely
2503 */
Josef Bacik48461132016-09-28 10:54:32 -04002504static bool states_equal(struct bpf_verifier_env *env,
2505 struct bpf_verifier_state *old,
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002506 struct bpf_verifier_state *cur)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002507{
Josef Bacike2d2afe2016-11-29 12:27:09 -05002508 bool varlen_map_access = env->varlen_map_value_access;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002509 struct bpf_reg_state *rold, *rcur;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002510 int i;
2511
2512 for (i = 0; i < MAX_BPF_REG; i++) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002513 rold = &old->regs[i];
2514 rcur = &cur->regs[i];
2515
2516 if (memcmp(rold, rcur, sizeof(*rold)) == 0)
2517 continue;
2518
Josef Bacik48461132016-09-28 10:54:32 -04002519 /* If the ranges were not the same, but everything else was and
2520 * we didn't do a variable access into a map then we are a-ok.
2521 */
Josef Bacike2d2afe2016-11-29 12:27:09 -05002522 if (!varlen_map_access &&
Alexei Starovoitovb7f5aa12016-12-07 10:57:59 -08002523 memcmp(rold, rcur, offsetofend(struct bpf_reg_state, id)) == 0)
Josef Bacik48461132016-09-28 10:54:32 -04002524 continue;
2525
Josef Bacike2d2afe2016-11-29 12:27:09 -05002526 /* If we didn't map access then again we don't care about the
2527 * mismatched range values and it's ok if our old type was
2528 * UNKNOWN and we didn't go to a NOT_INIT'ed reg.
2529 */
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002530 if (rold->type == NOT_INIT ||
Josef Bacike2d2afe2016-11-29 12:27:09 -05002531 (!varlen_map_access && rold->type == UNKNOWN_VALUE &&
2532 rcur->type != NOT_INIT))
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002533 continue;
2534
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002535 if (rold->type == PTR_TO_PACKET && rcur->type == PTR_TO_PACKET &&
2536 compare_ptrs_to_packet(rold, rcur))
2537 continue;
2538
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002539 return false;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002540 }
2541
2542 for (i = 0; i < MAX_BPF_STACK; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002543 if (old->stack_slot_type[i] == STACK_INVALID)
2544 continue;
2545 if (old->stack_slot_type[i] != cur->stack_slot_type[i])
2546 /* Ex: old explored (safe) state has STACK_SPILL in
2547 * this stack slot, but current has has STACK_MISC ->
2548 * this verifier states are not equivalent,
2549 * return false to continue verification of this path
2550 */
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002551 return false;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002552 if (i % BPF_REG_SIZE)
2553 continue;
2554 if (memcmp(&old->spilled_regs[i / BPF_REG_SIZE],
2555 &cur->spilled_regs[i / BPF_REG_SIZE],
2556 sizeof(old->spilled_regs[0])))
2557 /* when explored and current stack slot types are
2558 * the same, check that stored pointers types
2559 * are the same as well.
2560 * Ex: explored safe path could have stored
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002561 * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -8}
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002562 * but current path has stored:
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002563 * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -16}
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002564 * such verifier states are not equivalent.
2565 * return false to continue verification of this path
2566 */
2567 return false;
2568 else
2569 continue;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002570 }
2571 return true;
2572}
2573
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002574static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002575{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002576 struct bpf_verifier_state_list *new_sl;
2577 struct bpf_verifier_state_list *sl;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002578
2579 sl = env->explored_states[insn_idx];
2580 if (!sl)
2581 /* this 'insn_idx' instruction wasn't marked, so we will not
2582 * be doing state search here
2583 */
2584 return 0;
2585
2586 while (sl != STATE_LIST_MARK) {
Josef Bacik48461132016-09-28 10:54:32 -04002587 if (states_equal(env, &sl->state, &env->cur_state))
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002588 /* reached equivalent register/stack state,
2589 * prune the search
2590 */
2591 return 1;
2592 sl = sl->next;
2593 }
2594
2595 /* there were no equivalent states, remember current one.
2596 * technically the current state is not proven to be safe yet,
2597 * but it will either reach bpf_exit (which means it's safe) or
2598 * it will be rejected. Since there are no loops, we won't be
2599 * seeing this 'insn_idx' instruction again on the way to bpf_exit
2600 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002601 new_sl = kmalloc(sizeof(struct bpf_verifier_state_list), GFP_USER);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002602 if (!new_sl)
2603 return -ENOMEM;
2604
2605 /* add new state to the head of linked list */
2606 memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state));
2607 new_sl->next = env->explored_states[insn_idx];
2608 env->explored_states[insn_idx] = new_sl;
2609 return 0;
2610}
2611
Jakub Kicinski13a27df2016-09-21 11:43:58 +01002612static int ext_analyzer_insn_hook(struct bpf_verifier_env *env,
2613 int insn_idx, int prev_insn_idx)
2614{
2615 if (!env->analyzer_ops || !env->analyzer_ops->insn_hook)
2616 return 0;
2617
2618 return env->analyzer_ops->insn_hook(env, insn_idx, prev_insn_idx);
2619}
2620
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002621static int do_check(struct bpf_verifier_env *env)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002622{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002623 struct bpf_verifier_state *state = &env->cur_state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002624 struct bpf_insn *insns = env->prog->insnsi;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002625 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002626 int insn_cnt = env->prog->len;
2627 int insn_idx, prev_insn_idx = 0;
2628 int insn_processed = 0;
2629 bool do_print_state = false;
2630
2631 init_reg_state(regs);
2632 insn_idx = 0;
Josef Bacik48461132016-09-28 10:54:32 -04002633 env->varlen_map_value_access = false;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002634 for (;;) {
2635 struct bpf_insn *insn;
2636 u8 class;
2637 int err;
2638
2639 if (insn_idx >= insn_cnt) {
2640 verbose("invalid insn idx %d insn_cnt %d\n",
2641 insn_idx, insn_cnt);
2642 return -EFAULT;
2643 }
2644
2645 insn = &insns[insn_idx];
2646 class = BPF_CLASS(insn->code);
2647
Daniel Borkmann07016152016-04-05 22:33:17 +02002648 if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002649 verbose("BPF program is too large. Proccessed %d insn\n",
2650 insn_processed);
2651 return -E2BIG;
2652 }
2653
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002654 err = is_state_visited(env, insn_idx);
2655 if (err < 0)
2656 return err;
2657 if (err == 1) {
2658 /* found equivalent state, can prune the search */
2659 if (log_level) {
2660 if (do_print_state)
2661 verbose("\nfrom %d to %d: safe\n",
2662 prev_insn_idx, insn_idx);
2663 else
2664 verbose("%d: safe\n", insn_idx);
2665 }
2666 goto process_bpf_exit;
2667 }
2668
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002669 if (log_level && do_print_state) {
2670 verbose("\nfrom %d to %d:", prev_insn_idx, insn_idx);
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002671 print_verifier_state(&env->cur_state);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002672 do_print_state = false;
2673 }
2674
2675 if (log_level) {
2676 verbose("%d: ", insn_idx);
2677 print_bpf_insn(insn);
2678 }
2679
Jakub Kicinski13a27df2016-09-21 11:43:58 +01002680 err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx);
2681 if (err)
2682 return err;
2683
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002684 if (class == BPF_ALU || class == BPF_ALU64) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002685 err = check_alu_op(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002686 if (err)
2687 return err;
2688
2689 } else if (class == BPF_LDX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002690 enum bpf_reg_type *prev_src_type, src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002691
2692 /* check for reserved fields is already done */
2693
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002694 /* check src operand */
2695 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2696 if (err)
2697 return err;
2698
2699 err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
2700 if (err)
2701 return err;
2702
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07002703 src_reg_type = regs[insn->src_reg].type;
2704
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002705 /* check that memory (src_reg + off) is readable,
2706 * the state of dst_reg will be updated by this func
2707 */
2708 err = check_mem_access(env, insn->src_reg, insn->off,
2709 BPF_SIZE(insn->code), BPF_READ,
2710 insn->dst_reg);
2711 if (err)
2712 return err;
2713
Josef Bacik48461132016-09-28 10:54:32 -04002714 reset_reg_range_values(regs, insn->dst_reg);
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07002715 if (BPF_SIZE(insn->code) != BPF_W &&
2716 BPF_SIZE(insn->code) != BPF_DW) {
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07002717 insn_idx++;
2718 continue;
2719 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002720
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002721 prev_src_type = &env->insn_aux_data[insn_idx].ptr_type;
2722
2723 if (*prev_src_type == NOT_INIT) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002724 /* saw a valid insn
2725 * dst_reg = *(u32 *)(src_reg + off)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002726 * save type to validate intersecting paths
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002727 */
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002728 *prev_src_type = src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002729
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002730 } else if (src_reg_type != *prev_src_type &&
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002731 (src_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002732 *prev_src_type == PTR_TO_CTX)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002733 /* ABuser program is trying to use the same insn
2734 * dst_reg = *(u32*) (src_reg + off)
2735 * with different pointer types:
2736 * src_reg == ctx in one branch and
2737 * src_reg == stack|map in some other branch.
2738 * Reject it.
2739 */
2740 verbose("same insn cannot be used with different pointers\n");
2741 return -EINVAL;
2742 }
2743
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002744 } else if (class == BPF_STX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002745 enum bpf_reg_type *prev_dst_type, dst_reg_type;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002746
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002747 if (BPF_MODE(insn->code) == BPF_XADD) {
2748 err = check_xadd(env, insn);
2749 if (err)
2750 return err;
2751 insn_idx++;
2752 continue;
2753 }
2754
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002755 /* check src1 operand */
2756 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2757 if (err)
2758 return err;
2759 /* check src2 operand */
2760 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
2761 if (err)
2762 return err;
2763
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002764 dst_reg_type = regs[insn->dst_reg].type;
2765
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002766 /* check that memory (dst_reg + off) is writeable */
2767 err = check_mem_access(env, insn->dst_reg, insn->off,
2768 BPF_SIZE(insn->code), BPF_WRITE,
2769 insn->src_reg);
2770 if (err)
2771 return err;
2772
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002773 prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type;
2774
2775 if (*prev_dst_type == NOT_INIT) {
2776 *prev_dst_type = dst_reg_type;
2777 } else if (dst_reg_type != *prev_dst_type &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002778 (dst_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002779 *prev_dst_type == PTR_TO_CTX)) {
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002780 verbose("same insn cannot be used with different pointers\n");
2781 return -EINVAL;
2782 }
2783
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002784 } else if (class == BPF_ST) {
2785 if (BPF_MODE(insn->code) != BPF_MEM ||
2786 insn->src_reg != BPF_REG_0) {
2787 verbose("BPF_ST uses reserved fields\n");
2788 return -EINVAL;
2789 }
2790 /* check src operand */
2791 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
2792 if (err)
2793 return err;
2794
2795 /* check that memory (dst_reg + off) is writeable */
2796 err = check_mem_access(env, insn->dst_reg, insn->off,
2797 BPF_SIZE(insn->code), BPF_WRITE,
2798 -1);
2799 if (err)
2800 return err;
2801
2802 } else if (class == BPF_JMP) {
2803 u8 opcode = BPF_OP(insn->code);
2804
2805 if (opcode == BPF_CALL) {
2806 if (BPF_SRC(insn->code) != BPF_K ||
2807 insn->off != 0 ||
2808 insn->src_reg != BPF_REG_0 ||
2809 insn->dst_reg != BPF_REG_0) {
2810 verbose("BPF_CALL uses reserved fields\n");
2811 return -EINVAL;
2812 }
2813
2814 err = check_call(env, insn->imm);
2815 if (err)
2816 return err;
2817
2818 } else if (opcode == BPF_JA) {
2819 if (BPF_SRC(insn->code) != BPF_K ||
2820 insn->imm != 0 ||
2821 insn->src_reg != BPF_REG_0 ||
2822 insn->dst_reg != BPF_REG_0) {
2823 verbose("BPF_JA uses reserved fields\n");
2824 return -EINVAL;
2825 }
2826
2827 insn_idx += insn->off + 1;
2828 continue;
2829
2830 } else if (opcode == BPF_EXIT) {
2831 if (BPF_SRC(insn->code) != BPF_K ||
2832 insn->imm != 0 ||
2833 insn->src_reg != BPF_REG_0 ||
2834 insn->dst_reg != BPF_REG_0) {
2835 verbose("BPF_EXIT uses reserved fields\n");
2836 return -EINVAL;
2837 }
2838
2839 /* eBPF calling convetion is such that R0 is used
2840 * to return the value from eBPF program.
2841 * Make sure that it's readable at this time
2842 * of bpf_exit, which means that program wrote
2843 * something into it earlier
2844 */
2845 err = check_reg_arg(regs, BPF_REG_0, SRC_OP);
2846 if (err)
2847 return err;
2848
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002849 if (is_pointer_value(env, BPF_REG_0)) {
2850 verbose("R0 leaks addr as return value\n");
2851 return -EACCES;
2852 }
2853
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002854process_bpf_exit:
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002855 insn_idx = pop_stack(env, &prev_insn_idx);
2856 if (insn_idx < 0) {
2857 break;
2858 } else {
2859 do_print_state = true;
2860 continue;
2861 }
2862 } else {
2863 err = check_cond_jmp_op(env, insn, &insn_idx);
2864 if (err)
2865 return err;
2866 }
2867 } else if (class == BPF_LD) {
2868 u8 mode = BPF_MODE(insn->code);
2869
2870 if (mode == BPF_ABS || mode == BPF_IND) {
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002871 err = check_ld_abs(env, insn);
2872 if (err)
2873 return err;
2874
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002875 } else if (mode == BPF_IMM) {
2876 err = check_ld_imm(env, insn);
2877 if (err)
2878 return err;
2879
2880 insn_idx++;
2881 } else {
2882 verbose("invalid BPF_LD mode\n");
2883 return -EINVAL;
2884 }
Josef Bacik48461132016-09-28 10:54:32 -04002885 reset_reg_range_values(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002886 } else {
2887 verbose("unknown insn class %d\n", class);
2888 return -EINVAL;
2889 }
2890
2891 insn_idx++;
2892 }
2893
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002894 verbose("processed %d insns\n", insn_processed);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002895 return 0;
2896}
2897
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07002898static int check_map_prog_compatibility(struct bpf_map *map,
2899 struct bpf_prog *prog)
2900
2901{
2902 if (prog->type == BPF_PROG_TYPE_PERF_EVENT &&
2903 (map->map_type == BPF_MAP_TYPE_HASH ||
2904 map->map_type == BPF_MAP_TYPE_PERCPU_HASH) &&
2905 (map->map_flags & BPF_F_NO_PREALLOC)) {
2906 verbose("perf_event programs can only use preallocated hash map\n");
2907 return -EINVAL;
2908 }
2909 return 0;
2910}
2911
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002912/* look for pseudo eBPF instructions that access map FDs and
2913 * replace them with actual map pointers
2914 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002915static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002916{
2917 struct bpf_insn *insn = env->prog->insnsi;
2918 int insn_cnt = env->prog->len;
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07002919 int i, j, err;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002920
2921 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002922 if (BPF_CLASS(insn->code) == BPF_LDX &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002923 (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002924 verbose("BPF_LDX uses reserved fields\n");
2925 return -EINVAL;
2926 }
2927
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002928 if (BPF_CLASS(insn->code) == BPF_STX &&
2929 ((BPF_MODE(insn->code) != BPF_MEM &&
2930 BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
2931 verbose("BPF_STX uses reserved fields\n");
2932 return -EINVAL;
2933 }
2934
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002935 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
2936 struct bpf_map *map;
2937 struct fd f;
2938
2939 if (i == insn_cnt - 1 || insn[1].code != 0 ||
2940 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
2941 insn[1].off != 0) {
2942 verbose("invalid bpf_ld_imm64 insn\n");
2943 return -EINVAL;
2944 }
2945
2946 if (insn->src_reg == 0)
2947 /* valid generic load 64-bit imm */
2948 goto next_insn;
2949
2950 if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
2951 verbose("unrecognized bpf_ld_imm64 insn\n");
2952 return -EINVAL;
2953 }
2954
2955 f = fdget(insn->imm);
Daniel Borkmannc2101292015-10-29 14:58:07 +01002956 map = __bpf_map_get(f);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002957 if (IS_ERR(map)) {
2958 verbose("fd %d is not pointing to valid bpf_map\n",
2959 insn->imm);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002960 return PTR_ERR(map);
2961 }
2962
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07002963 err = check_map_prog_compatibility(map, env->prog);
2964 if (err) {
2965 fdput(f);
2966 return err;
2967 }
2968
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002969 /* store map pointer inside BPF_LD_IMM64 instruction */
2970 insn[0].imm = (u32) (unsigned long) map;
2971 insn[1].imm = ((u64) (unsigned long) map) >> 32;
2972
2973 /* check whether we recorded this map already */
2974 for (j = 0; j < env->used_map_cnt; j++)
2975 if (env->used_maps[j] == map) {
2976 fdput(f);
2977 goto next_insn;
2978 }
2979
2980 if (env->used_map_cnt >= MAX_USED_MAPS) {
2981 fdput(f);
2982 return -E2BIG;
2983 }
2984
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002985 /* hold the map. If the program is rejected by verifier,
2986 * the map will be released by release_maps() or it
2987 * will be used by the valid program until it's unloaded
2988 * and all maps are released in free_bpf_prog_info()
2989 */
Alexei Starovoitov92117d82016-04-27 18:56:20 -07002990 map = bpf_map_inc(map, false);
2991 if (IS_ERR(map)) {
2992 fdput(f);
2993 return PTR_ERR(map);
2994 }
2995 env->used_maps[env->used_map_cnt++] = map;
2996
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002997 fdput(f);
2998next_insn:
2999 insn++;
3000 i++;
3001 }
3002 }
3003
3004 /* now all pseudo BPF_LD_IMM64 instructions load valid
3005 * 'struct bpf_map *' into a register instead of user map_fd.
3006 * These pointers will be used later by verifier to validate map access.
3007 */
3008 return 0;
3009}
3010
3011/* drop refcnt of maps used by the rejected program */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003012static void release_maps(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003013{
3014 int i;
3015
3016 for (i = 0; i < env->used_map_cnt; i++)
3017 bpf_map_put(env->used_maps[i]);
3018}
3019
3020/* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003021static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003022{
3023 struct bpf_insn *insn = env->prog->insnsi;
3024 int insn_cnt = env->prog->len;
3025 int i;
3026
3027 for (i = 0; i < insn_cnt; i++, insn++)
3028 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
3029 insn->src_reg = 0;
3030}
3031
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003032/* convert load instructions that access fields of 'struct __sk_buff'
3033 * into sequence of instructions that access fields of 'struct sk_buff'
3034 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003035static int convert_ctx_accesses(struct bpf_verifier_env *env)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003036{
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003037 const struct bpf_verifier_ops *ops = env->prog->aux->ops;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003038 const int insn_cnt = env->prog->len;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003039 struct bpf_insn insn_buf[16], *insn;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003040 struct bpf_prog *new_prog;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003041 enum bpf_access_type type;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003042 int i, cnt, delta = 0;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003043
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003044 if (ops->gen_prologue) {
3045 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
3046 env->prog);
3047 if (cnt >= ARRAY_SIZE(insn_buf)) {
3048 verbose("bpf verifier is misconfigured\n");
3049 return -EINVAL;
3050 } else if (cnt) {
3051 new_prog = bpf_patch_insn_single(env->prog, 0,
3052 insn_buf, cnt);
3053 if (!new_prog)
3054 return -ENOMEM;
3055 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003056 delta += cnt - 1;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003057 }
3058 }
3059
3060 if (!ops->convert_ctx_access)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003061 return 0;
3062
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003063 insn = env->prog->insnsi + delta;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003064
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003065 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07003066 if (insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
3067 insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003068 type = BPF_READ;
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07003069 else if (insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
3070 insn->code == (BPF_STX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003071 type = BPF_WRITE;
3072 else
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003073 continue;
3074
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003075 if (env->insn_aux_data[i].ptr_type != PTR_TO_CTX)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003076 continue;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003077
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003078 cnt = ops->convert_ctx_access(type, insn->dst_reg, insn->src_reg,
3079 insn->off, insn_buf, env->prog);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003080 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
3081 verbose("bpf verifier is misconfigured\n");
3082 return -EINVAL;
3083 }
3084
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003085 new_prog = bpf_patch_insn_single(env->prog, i + delta, insn_buf,
3086 cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003087 if (!new_prog)
3088 return -ENOMEM;
3089
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003090 delta += cnt - 1;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003091
3092 /* keep walking new program and skip insns we just inserted */
3093 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003094 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003095 }
3096
3097 return 0;
3098}
3099
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003100static void free_states(struct bpf_verifier_env *env)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003101{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003102 struct bpf_verifier_state_list *sl, *sln;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003103 int i;
3104
3105 if (!env->explored_states)
3106 return;
3107
3108 for (i = 0; i < env->prog->len; i++) {
3109 sl = env->explored_states[i];
3110
3111 if (sl)
3112 while (sl != STATE_LIST_MARK) {
3113 sln = sl->next;
3114 kfree(sl);
3115 sl = sln;
3116 }
3117 }
3118
3119 kfree(env->explored_states);
3120}
3121
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003122int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003123{
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003124 char __user *log_ubuf = NULL;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003125 struct bpf_verifier_env *env;
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003126 int ret = -EINVAL;
3127
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003128 if ((*prog)->len <= 0 || (*prog)->len > BPF_MAXINSNS)
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003129 return -E2BIG;
3130
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003131 /* 'struct bpf_verifier_env' can be global, but since it's not small,
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003132 * allocate/free it every time bpf_check() is called
3133 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003134 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003135 if (!env)
3136 return -ENOMEM;
3137
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003138 env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
3139 (*prog)->len);
3140 ret = -ENOMEM;
3141 if (!env->insn_aux_data)
3142 goto err_free_env;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003143 env->prog = *prog;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003144
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003145 /* grab the mutex to protect few globals used by verifier */
3146 mutex_lock(&bpf_verifier_lock);
3147
3148 if (attr->log_level || attr->log_buf || attr->log_size) {
3149 /* user requested verbose verifier output
3150 * and supplied buffer to store the verification trace
3151 */
3152 log_level = attr->log_level;
3153 log_ubuf = (char __user *) (unsigned long) attr->log_buf;
3154 log_size = attr->log_size;
3155 log_len = 0;
3156
3157 ret = -EINVAL;
3158 /* log_* values have to be sane */
3159 if (log_size < 128 || log_size > UINT_MAX >> 8 ||
3160 log_level == 0 || log_ubuf == NULL)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003161 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003162
3163 ret = -ENOMEM;
3164 log_buf = vmalloc(log_size);
3165 if (!log_buf)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003166 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003167 } else {
3168 log_level = 0;
3169 }
3170
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003171 ret = replace_map_fd_with_map_ptr(env);
3172 if (ret < 0)
3173 goto skip_full_check;
3174
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003175 env->explored_states = kcalloc(env->prog->len,
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003176 sizeof(struct bpf_verifier_state_list *),
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003177 GFP_USER);
3178 ret = -ENOMEM;
3179 if (!env->explored_states)
3180 goto skip_full_check;
3181
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003182 ret = check_cfg(env);
3183 if (ret < 0)
3184 goto skip_full_check;
3185
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003186 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
3187
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003188 ret = do_check(env);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003189
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003190skip_full_check:
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003191 while (pop_stack(env, NULL) >= 0);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003192 free_states(env);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003193
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003194 if (ret == 0)
3195 /* program is valid, convert *(u32*)(ctx + off) accesses */
3196 ret = convert_ctx_accesses(env);
3197
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003198 if (log_level && log_len >= log_size - 1) {
3199 BUG_ON(log_len >= log_size);
3200 /* verifier log exceeded user supplied buffer */
3201 ret = -ENOSPC;
3202 /* fall through to return what was recorded */
3203 }
3204
3205 /* copy verifier log back to user space including trailing zero */
3206 if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) {
3207 ret = -EFAULT;
3208 goto free_log_buf;
3209 }
3210
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003211 if (ret == 0 && env->used_map_cnt) {
3212 /* if program passed verifier, update used_maps in bpf_prog_info */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003213 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
3214 sizeof(env->used_maps[0]),
3215 GFP_KERNEL);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003216
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003217 if (!env->prog->aux->used_maps) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003218 ret = -ENOMEM;
3219 goto free_log_buf;
3220 }
3221
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003222 memcpy(env->prog->aux->used_maps, env->used_maps,
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003223 sizeof(env->used_maps[0]) * env->used_map_cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003224 env->prog->aux->used_map_cnt = env->used_map_cnt;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003225
3226 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
3227 * bpf_ld_imm64 instructions
3228 */
3229 convert_pseudo_ld_imm64(env);
3230 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003231
3232free_log_buf:
3233 if (log_level)
3234 vfree(log_buf);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003235 if (!env->prog->aux->used_maps)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003236 /* if we didn't copy map pointers into bpf_prog_info, release
3237 * them now. Otherwise free_bpf_prog_info() will release them.
3238 */
3239 release_maps(env);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003240 *prog = env->prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003241err_unlock:
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003242 mutex_unlock(&bpf_verifier_lock);
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003243 vfree(env->insn_aux_data);
3244err_free_env:
3245 kfree(env);
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003246 return ret;
3247}
Jakub Kicinski13a27df2016-09-21 11:43:58 +01003248
3249int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops,
3250 void *priv)
3251{
3252 struct bpf_verifier_env *env;
3253 int ret;
3254
3255 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
3256 if (!env)
3257 return -ENOMEM;
3258
3259 env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
3260 prog->len);
3261 ret = -ENOMEM;
3262 if (!env->insn_aux_data)
3263 goto err_free_env;
3264 env->prog = prog;
3265 env->analyzer_ops = ops;
3266 env->analyzer_priv = priv;
3267
3268 /* grab the mutex to protect few globals used by verifier */
3269 mutex_lock(&bpf_verifier_lock);
3270
3271 log_level = 0;
3272
3273 env->explored_states = kcalloc(env->prog->len,
3274 sizeof(struct bpf_verifier_state_list *),
3275 GFP_KERNEL);
3276 ret = -ENOMEM;
3277 if (!env->explored_states)
3278 goto skip_full_check;
3279
3280 ret = check_cfg(env);
3281 if (ret < 0)
3282 goto skip_full_check;
3283
3284 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
3285
3286 ret = do_check(env);
3287
3288skip_full_check:
3289 while (pop_stack(env, NULL) >= 0);
3290 free_states(env);
3291
3292 mutex_unlock(&bpf_verifier_lock);
3293 vfree(env->insn_aux_data);
3294err_free_env:
3295 kfree(env);
3296 return ret;
3297}
3298EXPORT_SYMBOL_GPL(bpf_analyzer);