blob: 85d1c9423ccb3deb888ccab2a76b352b8c13ba3f [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 &&
Alexei Starovoitov1b9b69e2016-05-19 18:17:14 -07001752 (dst_reg->type == PTR_TO_PACKET ||
1753 (BPF_SRC(insn->code) == BPF_X &&
1754 regs[insn->src_reg].type == PTR_TO_PACKET))) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001755 /* ptr_to_packet += K|X */
1756 return check_packet_ptr_add(env, insn);
1757 } else if (BPF_CLASS(insn->code) == BPF_ALU64 &&
1758 dst_reg->type == UNKNOWN_VALUE &&
1759 env->allow_ptr_leaks) {
1760 /* unknown += K|X */
1761 return evaluate_reg_alu(env, insn);
1762 } else if (BPF_CLASS(insn->code) == BPF_ALU64 &&
1763 dst_reg->type == CONST_IMM &&
1764 env->allow_ptr_leaks) {
1765 /* reg_imm += K|X */
1766 return evaluate_reg_imm_alu(env, insn);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001767 } else if (is_pointer_value(env, insn->dst_reg)) {
1768 verbose("R%d pointer arithmetic prohibited\n",
1769 insn->dst_reg);
1770 return -EACCES;
1771 } else if (BPF_SRC(insn->code) == BPF_X &&
1772 is_pointer_value(env, insn->src_reg)) {
1773 verbose("R%d pointer arithmetic prohibited\n",
1774 insn->src_reg);
1775 return -EACCES;
1776 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001777
Josef Bacik48461132016-09-28 10:54:32 -04001778 /* If we did pointer math on a map value then just set it to our
1779 * PTR_TO_MAP_VALUE_ADJ type so we can deal with any stores or
1780 * loads to this register appropriately, otherwise just mark the
1781 * register as unknown.
1782 */
1783 if (env->allow_ptr_leaks &&
1784 (dst_reg->type == PTR_TO_MAP_VALUE ||
1785 dst_reg->type == PTR_TO_MAP_VALUE_ADJ))
1786 dst_reg->type = PTR_TO_MAP_VALUE_ADJ;
1787 else
1788 mark_reg_unknown_value(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001789 }
1790
1791 return 0;
1792}
1793
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001794static void find_good_pkt_pointers(struct bpf_verifier_state *state,
1795 struct bpf_reg_state *dst_reg)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001796{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001797 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001798 int i;
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02001799
1800 /* LLVM can generate two kind of checks:
1801 *
1802 * Type 1:
1803 *
1804 * r2 = r3;
1805 * r2 += 8;
1806 * if (r2 > pkt_end) goto <handle exception>
1807 * <access okay>
1808 *
1809 * Where:
1810 * r2 == dst_reg, pkt_end == src_reg
1811 * r2=pkt(id=n,off=8,r=0)
1812 * r3=pkt(id=n,off=0,r=0)
1813 *
1814 * Type 2:
1815 *
1816 * r2 = r3;
1817 * r2 += 8;
1818 * if (pkt_end >= r2) goto <access okay>
1819 * <handle exception>
1820 *
1821 * Where:
1822 * pkt_end == dst_reg, r2 == src_reg
1823 * r2=pkt(id=n,off=8,r=0)
1824 * r3=pkt(id=n,off=0,r=0)
1825 *
1826 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
1827 * so that range of bytes [r3, r3 + 8) is safe to access.
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001828 */
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02001829
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001830 for (i = 0; i < MAX_BPF_REG; i++)
1831 if (regs[i].type == PTR_TO_PACKET && regs[i].id == dst_reg->id)
1832 regs[i].range = dst_reg->off;
1833
1834 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
1835 if (state->stack_slot_type[i] != STACK_SPILL)
1836 continue;
1837 reg = &state->spilled_regs[i / BPF_REG_SIZE];
1838 if (reg->type == PTR_TO_PACKET && reg->id == dst_reg->id)
1839 reg->range = dst_reg->off;
1840 }
1841}
1842
Josef Bacik48461132016-09-28 10:54:32 -04001843/* Adjusts the register min/max values in the case that the dst_reg is the
1844 * variable register that we are working on, and src_reg is a constant or we're
1845 * simply doing a BPF_K check.
1846 */
1847static void reg_set_min_max(struct bpf_reg_state *true_reg,
1848 struct bpf_reg_state *false_reg, u64 val,
1849 u8 opcode)
1850{
1851 switch (opcode) {
1852 case BPF_JEQ:
1853 /* If this is false then we know nothing Jon Snow, but if it is
1854 * true then we know for sure.
1855 */
1856 true_reg->max_value = true_reg->min_value = val;
1857 break;
1858 case BPF_JNE:
1859 /* If this is true we know nothing Jon Snow, but if it is false
1860 * we know the value for sure;
1861 */
1862 false_reg->max_value = false_reg->min_value = val;
1863 break;
1864 case BPF_JGT:
1865 /* Unsigned comparison, the minimum value is 0. */
1866 false_reg->min_value = 0;
1867 case BPF_JSGT:
1868 /* If this is false then we know the maximum val is val,
1869 * otherwise we know the min val is val+1.
1870 */
1871 false_reg->max_value = val;
1872 true_reg->min_value = val + 1;
1873 break;
1874 case BPF_JGE:
1875 /* Unsigned comparison, the minimum value is 0. */
1876 false_reg->min_value = 0;
1877 case BPF_JSGE:
1878 /* If this is false then we know the maximum value is val - 1,
1879 * otherwise we know the mimimum value is val.
1880 */
1881 false_reg->max_value = val - 1;
1882 true_reg->min_value = val;
1883 break;
1884 default:
1885 break;
1886 }
1887
1888 check_reg_overflow(false_reg);
1889 check_reg_overflow(true_reg);
1890}
1891
1892/* Same as above, but for the case that dst_reg is a CONST_IMM reg and src_reg
1893 * is the variable reg.
1894 */
1895static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
1896 struct bpf_reg_state *false_reg, u64 val,
1897 u8 opcode)
1898{
1899 switch (opcode) {
1900 case BPF_JEQ:
1901 /* If this is false then we know nothing Jon Snow, but if it is
1902 * true then we know for sure.
1903 */
1904 true_reg->max_value = true_reg->min_value = val;
1905 break;
1906 case BPF_JNE:
1907 /* If this is true we know nothing Jon Snow, but if it is false
1908 * we know the value for sure;
1909 */
1910 false_reg->max_value = false_reg->min_value = val;
1911 break;
1912 case BPF_JGT:
1913 /* Unsigned comparison, the minimum value is 0. */
1914 true_reg->min_value = 0;
1915 case BPF_JSGT:
1916 /*
1917 * If this is false, then the val is <= the register, if it is
1918 * true the register <= to the val.
1919 */
1920 false_reg->min_value = val;
1921 true_reg->max_value = val - 1;
1922 break;
1923 case BPF_JGE:
1924 /* Unsigned comparison, the minimum value is 0. */
1925 true_reg->min_value = 0;
1926 case BPF_JSGE:
1927 /* If this is false then constant < register, if it is true then
1928 * the register < constant.
1929 */
1930 false_reg->min_value = val + 1;
1931 true_reg->max_value = val;
1932 break;
1933 default:
1934 break;
1935 }
1936
1937 check_reg_overflow(false_reg);
1938 check_reg_overflow(true_reg);
1939}
1940
Thomas Graf14117072016-10-18 19:51:19 +02001941static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id,
1942 enum bpf_reg_type type)
1943{
1944 struct bpf_reg_state *reg = &regs[regno];
1945
1946 if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) {
1947 reg->type = type;
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01001948 /* We don't need id from this point onwards anymore, thus we
1949 * should better reset it, so that state pruning has chances
1950 * to take effect.
1951 */
1952 reg->id = 0;
Thomas Graf14117072016-10-18 19:51:19 +02001953 if (type == UNKNOWN_VALUE)
Daniel Borkmann0e0f1d62016-12-18 01:52:59 +01001954 __mark_reg_unknown_value(regs, regno);
Thomas Graf14117072016-10-18 19:51:19 +02001955 }
1956}
1957
1958/* The logic is similar to find_good_pkt_pointers(), both could eventually
1959 * be folded together at some point.
1960 */
1961static void mark_map_regs(struct bpf_verifier_state *state, u32 regno,
1962 enum bpf_reg_type type)
1963{
1964 struct bpf_reg_state *regs = state->regs;
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01001965 u32 id = regs[regno].id;
Thomas Graf14117072016-10-18 19:51:19 +02001966 int i;
1967
1968 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01001969 mark_map_reg(regs, i, id, type);
Thomas Graf14117072016-10-18 19:51:19 +02001970
1971 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
1972 if (state->stack_slot_type[i] != STACK_SPILL)
1973 continue;
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01001974 mark_map_reg(state->spilled_regs, i / BPF_REG_SIZE, id, type);
Thomas Graf14117072016-10-18 19:51:19 +02001975 }
1976}
1977
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001978static int check_cond_jmp_op(struct bpf_verifier_env *env,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001979 struct bpf_insn *insn, int *insn_idx)
1980{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001981 struct bpf_verifier_state *other_branch, *this_branch = &env->cur_state;
1982 struct bpf_reg_state *regs = this_branch->regs, *dst_reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001983 u8 opcode = BPF_OP(insn->code);
1984 int err;
1985
1986 if (opcode > BPF_EXIT) {
1987 verbose("invalid BPF_JMP opcode %x\n", opcode);
1988 return -EINVAL;
1989 }
1990
1991 if (BPF_SRC(insn->code) == BPF_X) {
1992 if (insn->imm != 0) {
1993 verbose("BPF_JMP uses reserved fields\n");
1994 return -EINVAL;
1995 }
1996
1997 /* check src1 operand */
1998 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1999 if (err)
2000 return err;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002001
2002 if (is_pointer_value(env, insn->src_reg)) {
2003 verbose("R%d pointer comparison prohibited\n",
2004 insn->src_reg);
2005 return -EACCES;
2006 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002007 } else {
2008 if (insn->src_reg != BPF_REG_0) {
2009 verbose("BPF_JMP uses reserved fields\n");
2010 return -EINVAL;
2011 }
2012 }
2013
2014 /* check src2 operand */
2015 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
2016 if (err)
2017 return err;
2018
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002019 dst_reg = &regs[insn->dst_reg];
2020
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002021 /* detect if R == 0 where R was initialized to zero earlier */
2022 if (BPF_SRC(insn->code) == BPF_K &&
2023 (opcode == BPF_JEQ || opcode == BPF_JNE) &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002024 dst_reg->type == CONST_IMM && dst_reg->imm == insn->imm) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002025 if (opcode == BPF_JEQ) {
2026 /* if (imm == imm) goto pc+off;
2027 * only follow the goto, ignore fall-through
2028 */
2029 *insn_idx += insn->off;
2030 return 0;
2031 } else {
2032 /* if (imm != imm) goto pc+off;
2033 * only follow fall-through branch, since
2034 * that's where the program will go
2035 */
2036 return 0;
2037 }
2038 }
2039
2040 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx);
2041 if (!other_branch)
2042 return -EFAULT;
2043
Josef Bacik48461132016-09-28 10:54:32 -04002044 /* detect if we are comparing against a constant value so we can adjust
2045 * our min/max values for our dst register.
2046 */
2047 if (BPF_SRC(insn->code) == BPF_X) {
2048 if (regs[insn->src_reg].type == CONST_IMM)
2049 reg_set_min_max(&other_branch->regs[insn->dst_reg],
2050 dst_reg, regs[insn->src_reg].imm,
2051 opcode);
2052 else if (dst_reg->type == CONST_IMM)
2053 reg_set_min_max_inv(&other_branch->regs[insn->src_reg],
2054 &regs[insn->src_reg], dst_reg->imm,
2055 opcode);
2056 } else {
2057 reg_set_min_max(&other_branch->regs[insn->dst_reg],
2058 dst_reg, insn->imm, opcode);
2059 }
2060
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002061 /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002062 if (BPF_SRC(insn->code) == BPF_K &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002063 insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
2064 dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
Thomas Graf14117072016-10-18 19:51:19 +02002065 /* Mark all identical map registers in each branch as either
2066 * safe or unknown depending R == 0 or R != 0 conditional.
2067 */
2068 mark_map_regs(this_branch, insn->dst_reg,
2069 opcode == BPF_JEQ ? PTR_TO_MAP_VALUE : UNKNOWN_VALUE);
2070 mark_map_regs(other_branch, insn->dst_reg,
2071 opcode == BPF_JEQ ? UNKNOWN_VALUE : PTR_TO_MAP_VALUE);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002072 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT &&
2073 dst_reg->type == PTR_TO_PACKET &&
2074 regs[insn->src_reg].type == PTR_TO_PACKET_END) {
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002075 find_good_pkt_pointers(this_branch, dst_reg);
2076 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGE &&
2077 dst_reg->type == PTR_TO_PACKET_END &&
2078 regs[insn->src_reg].type == PTR_TO_PACKET) {
2079 find_good_pkt_pointers(other_branch, &regs[insn->src_reg]);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002080 } else if (is_pointer_value(env, insn->dst_reg)) {
2081 verbose("R%d pointer comparison prohibited\n", insn->dst_reg);
2082 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002083 }
2084 if (log_level)
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002085 print_verifier_state(this_branch);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002086 return 0;
2087}
2088
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002089/* return the map pointer stored inside BPF_LD_IMM64 instruction */
2090static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
2091{
2092 u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
2093
2094 return (struct bpf_map *) (unsigned long) imm64;
2095}
2096
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002097/* verify BPF_LD_IMM64 instruction */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002098static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002099{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002100 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002101 int err;
2102
2103 if (BPF_SIZE(insn->code) != BPF_DW) {
2104 verbose("invalid BPF_LD_IMM insn\n");
2105 return -EINVAL;
2106 }
2107 if (insn->off != 0) {
2108 verbose("BPF_LD_IMM64 uses reserved fields\n");
2109 return -EINVAL;
2110 }
2111
2112 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
2113 if (err)
2114 return err;
2115
Jakub Kicinski6b173872016-09-21 11:43:59 +01002116 if (insn->src_reg == 0) {
2117 /* generic move 64-bit immediate into a register,
2118 * only analyzer needs to collect the ld_imm value.
2119 */
2120 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
2121
2122 if (!env->analyzer_ops)
2123 return 0;
2124
2125 regs[insn->dst_reg].type = CONST_IMM;
2126 regs[insn->dst_reg].imm = imm;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002127 return 0;
Jakub Kicinski6b173872016-09-21 11:43:59 +01002128 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002129
2130 /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
2131 BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
2132
2133 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
2134 regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
2135 return 0;
2136}
2137
Daniel Borkmann96be4322015-03-01 12:31:46 +01002138static bool may_access_skb(enum bpf_prog_type type)
2139{
2140 switch (type) {
2141 case BPF_PROG_TYPE_SOCKET_FILTER:
2142 case BPF_PROG_TYPE_SCHED_CLS:
Daniel Borkmann94caee82015-03-20 15:11:11 +01002143 case BPF_PROG_TYPE_SCHED_ACT:
Daniel Borkmann96be4322015-03-01 12:31:46 +01002144 return true;
2145 default:
2146 return false;
2147 }
2148}
2149
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002150/* verify safety of LD_ABS|LD_IND instructions:
2151 * - they can only appear in the programs where ctx == skb
2152 * - since they are wrappers of function calls, they scratch R1-R5 registers,
2153 * preserve R6-R9, and store return value into R0
2154 *
2155 * Implicit input:
2156 * ctx == skb == R6 == CTX
2157 *
2158 * Explicit input:
2159 * SRC == any register
2160 * IMM == 32-bit immediate
2161 *
2162 * Output:
2163 * R0 - 8/16/32-bit skb data converted to cpu endianness
2164 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002165static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002166{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002167 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002168 u8 mode = BPF_MODE(insn->code);
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002169 struct bpf_reg_state *reg;
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002170 int i, err;
2171
Daniel Borkmann24701ec2015-03-01 12:31:47 +01002172 if (!may_access_skb(env->prog->type)) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002173 verbose("BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002174 return -EINVAL;
2175 }
2176
2177 if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
Alexei Starovoitovd82bccc2016-04-12 10:26:19 -07002178 BPF_SIZE(insn->code) == BPF_DW ||
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002179 (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002180 verbose("BPF_LD_[ABS|IND] uses reserved fields\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002181 return -EINVAL;
2182 }
2183
2184 /* check whether implicit source operand (register R6) is readable */
2185 err = check_reg_arg(regs, BPF_REG_6, SRC_OP);
2186 if (err)
2187 return err;
2188
2189 if (regs[BPF_REG_6].type != PTR_TO_CTX) {
2190 verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
2191 return -EINVAL;
2192 }
2193
2194 if (mode == BPF_IND) {
2195 /* check explicit source operand */
2196 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2197 if (err)
2198 return err;
2199 }
2200
2201 /* reset caller saved regs to unreadable */
2202 for (i = 0; i < CALLER_SAVED_REGS; i++) {
2203 reg = regs + caller_saved[i];
2204 reg->type = NOT_INIT;
2205 reg->imm = 0;
2206 }
2207
2208 /* mark destination R0 register as readable, since it contains
2209 * the value fetched from the packet
2210 */
2211 regs[BPF_REG_0].type = UNKNOWN_VALUE;
2212 return 0;
2213}
2214
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002215/* non-recursive DFS pseudo code
2216 * 1 procedure DFS-iterative(G,v):
2217 * 2 label v as discovered
2218 * 3 let S be a stack
2219 * 4 S.push(v)
2220 * 5 while S is not empty
2221 * 6 t <- S.pop()
2222 * 7 if t is what we're looking for:
2223 * 8 return t
2224 * 9 for all edges e in G.adjacentEdges(t) do
2225 * 10 if edge e is already labelled
2226 * 11 continue with the next edge
2227 * 12 w <- G.adjacentVertex(t,e)
2228 * 13 if vertex w is not discovered and not explored
2229 * 14 label e as tree-edge
2230 * 15 label w as discovered
2231 * 16 S.push(w)
2232 * 17 continue at 5
2233 * 18 else if vertex w is discovered
2234 * 19 label e as back-edge
2235 * 20 else
2236 * 21 // vertex w is explored
2237 * 22 label e as forward- or cross-edge
2238 * 23 label t as explored
2239 * 24 S.pop()
2240 *
2241 * convention:
2242 * 0x10 - discovered
2243 * 0x11 - discovered and fall-through edge labelled
2244 * 0x12 - discovered and fall-through and branch edges labelled
2245 * 0x20 - explored
2246 */
2247
2248enum {
2249 DISCOVERED = 0x10,
2250 EXPLORED = 0x20,
2251 FALLTHROUGH = 1,
2252 BRANCH = 2,
2253};
2254
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002255#define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002256
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002257static int *insn_stack; /* stack of insns to process */
2258static int cur_stack; /* current stack index */
2259static int *insn_state;
2260
2261/* t, w, e - match pseudo-code above:
2262 * t - index of current instruction
2263 * w - next instruction
2264 * e - edge
2265 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002266static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002267{
2268 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
2269 return 0;
2270
2271 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
2272 return 0;
2273
2274 if (w < 0 || w >= env->prog->len) {
2275 verbose("jump out of range from insn %d to %d\n", t, w);
2276 return -EINVAL;
2277 }
2278
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002279 if (e == BRANCH)
2280 /* mark branch target for state pruning */
2281 env->explored_states[w] = STATE_LIST_MARK;
2282
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002283 if (insn_state[w] == 0) {
2284 /* tree-edge */
2285 insn_state[t] = DISCOVERED | e;
2286 insn_state[w] = DISCOVERED;
2287 if (cur_stack >= env->prog->len)
2288 return -E2BIG;
2289 insn_stack[cur_stack++] = w;
2290 return 1;
2291 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
2292 verbose("back-edge from insn %d to %d\n", t, w);
2293 return -EINVAL;
2294 } else if (insn_state[w] == EXPLORED) {
2295 /* forward- or cross-edge */
2296 insn_state[t] = DISCOVERED | e;
2297 } else {
2298 verbose("insn state internal bug\n");
2299 return -EFAULT;
2300 }
2301 return 0;
2302}
2303
2304/* non-recursive depth-first-search to detect loops in BPF program
2305 * loop == back-edge in directed graph
2306 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002307static int check_cfg(struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002308{
2309 struct bpf_insn *insns = env->prog->insnsi;
2310 int insn_cnt = env->prog->len;
2311 int ret = 0;
2312 int i, t;
2313
2314 insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
2315 if (!insn_state)
2316 return -ENOMEM;
2317
2318 insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
2319 if (!insn_stack) {
2320 kfree(insn_state);
2321 return -ENOMEM;
2322 }
2323
2324 insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
2325 insn_stack[0] = 0; /* 0 is the first instruction */
2326 cur_stack = 1;
2327
2328peek_stack:
2329 if (cur_stack == 0)
2330 goto check_state;
2331 t = insn_stack[cur_stack - 1];
2332
2333 if (BPF_CLASS(insns[t].code) == BPF_JMP) {
2334 u8 opcode = BPF_OP(insns[t].code);
2335
2336 if (opcode == BPF_EXIT) {
2337 goto mark_explored;
2338 } else if (opcode == BPF_CALL) {
2339 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2340 if (ret == 1)
2341 goto peek_stack;
2342 else if (ret < 0)
2343 goto err_free;
Daniel Borkmann07016152016-04-05 22:33:17 +02002344 if (t + 1 < insn_cnt)
2345 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002346 } else if (opcode == BPF_JA) {
2347 if (BPF_SRC(insns[t].code) != BPF_K) {
2348 ret = -EINVAL;
2349 goto err_free;
2350 }
2351 /* unconditional jump with single edge */
2352 ret = push_insn(t, t + insns[t].off + 1,
2353 FALLTHROUGH, env);
2354 if (ret == 1)
2355 goto peek_stack;
2356 else if (ret < 0)
2357 goto err_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002358 /* tell verifier to check for equivalent states
2359 * after every call and jump
2360 */
Alexei Starovoitovc3de6312015-04-14 15:57:13 -07002361 if (t + 1 < insn_cnt)
2362 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002363 } else {
2364 /* conditional jump with two edges */
2365 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2366 if (ret == 1)
2367 goto peek_stack;
2368 else if (ret < 0)
2369 goto err_free;
2370
2371 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
2372 if (ret == 1)
2373 goto peek_stack;
2374 else if (ret < 0)
2375 goto err_free;
2376 }
2377 } else {
2378 /* all other non-branch instructions with single
2379 * fall-through edge
2380 */
2381 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2382 if (ret == 1)
2383 goto peek_stack;
2384 else if (ret < 0)
2385 goto err_free;
2386 }
2387
2388mark_explored:
2389 insn_state[t] = EXPLORED;
2390 if (cur_stack-- <= 0) {
2391 verbose("pop stack internal bug\n");
2392 ret = -EFAULT;
2393 goto err_free;
2394 }
2395 goto peek_stack;
2396
2397check_state:
2398 for (i = 0; i < insn_cnt; i++) {
2399 if (insn_state[i] != EXPLORED) {
2400 verbose("unreachable insn %d\n", i);
2401 ret = -EINVAL;
2402 goto err_free;
2403 }
2404 }
2405 ret = 0; /* cfg looks good */
2406
2407err_free:
2408 kfree(insn_state);
2409 kfree(insn_stack);
2410 return ret;
2411}
2412
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002413/* the following conditions reduce the number of explored insns
2414 * from ~140k to ~80k for ultra large programs that use a lot of ptr_to_packet
2415 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002416static bool compare_ptrs_to_packet(struct bpf_reg_state *old,
2417 struct bpf_reg_state *cur)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002418{
2419 if (old->id != cur->id)
2420 return false;
2421
2422 /* old ptr_to_packet is more conservative, since it allows smaller
2423 * range. Ex:
2424 * old(off=0,r=10) is equal to cur(off=0,r=20), because
2425 * old(off=0,r=10) means that with range=10 the verifier proceeded
2426 * further and found no issues with the program. Now we're in the same
2427 * spot with cur(off=0,r=20), so we're safe too, since anything further
2428 * will only be looking at most 10 bytes after this pointer.
2429 */
2430 if (old->off == cur->off && old->range < cur->range)
2431 return true;
2432
2433 /* old(off=20,r=10) is equal to cur(off=22,re=22 or 5 or 0)
2434 * since both cannot be used for packet access and safe(old)
2435 * pointer has smaller off that could be used for further
2436 * 'if (ptr > data_end)' check
2437 * Ex:
2438 * old(off=20,r=10) and cur(off=22,r=22) and cur(off=22,r=0) mean
2439 * that we cannot access the packet.
2440 * The safe range is:
2441 * [ptr, ptr + range - off)
2442 * so whenever off >=range, it means no safe bytes from this pointer.
2443 * When comparing old->off <= cur->off, it means that older code
2444 * went with smaller offset and that offset was later
2445 * used to figure out the safe range after 'if (ptr > data_end)' check
2446 * Say, 'old' state was explored like:
2447 * ... R3(off=0, r=0)
2448 * R4 = R3 + 20
2449 * ... now R4(off=20,r=0) <-- here
2450 * if (R4 > data_end)
2451 * ... R4(off=20,r=20), R3(off=0,r=20) and R3 can be used to access.
2452 * ... the code further went all the way to bpf_exit.
2453 * Now the 'cur' state at the mark 'here' has R4(off=30,r=0).
2454 * old_R4(off=20,r=0) equal to cur_R4(off=30,r=0), since if the verifier
2455 * goes further, such cur_R4 will give larger safe packet range after
2456 * 'if (R4 > data_end)' and all further insn were already good with r=20,
2457 * so they will be good with r=30 and we can prune the search.
2458 */
2459 if (old->off <= cur->off &&
2460 old->off >= old->range && cur->off >= cur->range)
2461 return true;
2462
2463 return false;
2464}
2465
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002466/* compare two verifier states
2467 *
2468 * all states stored in state_list are known to be valid, since
2469 * verifier reached 'bpf_exit' instruction through them
2470 *
2471 * this function is called when verifier exploring different branches of
2472 * execution popped from the state stack. If it sees an old state that has
2473 * more strict register state and more strict stack state then this execution
2474 * branch doesn't need to be explored further, since verifier already
2475 * concluded that more strict state leads to valid finish.
2476 *
2477 * Therefore two states are equivalent if register state is more conservative
2478 * and explored stack state is more conservative than the current one.
2479 * Example:
2480 * explored current
2481 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
2482 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
2483 *
2484 * In other words if current stack state (one being explored) has more
2485 * valid slots than old one that already passed validation, it means
2486 * the verifier can stop exploring and conclude that current state is valid too
2487 *
2488 * Similarly with registers. If explored state has register type as invalid
2489 * whereas register type in current state is meaningful, it means that
2490 * the current state will reach 'bpf_exit' instruction safely
2491 */
Josef Bacik48461132016-09-28 10:54:32 -04002492static bool states_equal(struct bpf_verifier_env *env,
2493 struct bpf_verifier_state *old,
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002494 struct bpf_verifier_state *cur)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002495{
Josef Bacike2d2afe2016-11-29 12:27:09 -05002496 bool varlen_map_access = env->varlen_map_value_access;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002497 struct bpf_reg_state *rold, *rcur;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002498 int i;
2499
2500 for (i = 0; i < MAX_BPF_REG; i++) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002501 rold = &old->regs[i];
2502 rcur = &cur->regs[i];
2503
2504 if (memcmp(rold, rcur, sizeof(*rold)) == 0)
2505 continue;
2506
Josef Bacik48461132016-09-28 10:54:32 -04002507 /* If the ranges were not the same, but everything else was and
2508 * we didn't do a variable access into a map then we are a-ok.
2509 */
Josef Bacike2d2afe2016-11-29 12:27:09 -05002510 if (!varlen_map_access &&
Alexei Starovoitovb7f5aa12016-12-07 10:57:59 -08002511 memcmp(rold, rcur, offsetofend(struct bpf_reg_state, id)) == 0)
Josef Bacik48461132016-09-28 10:54:32 -04002512 continue;
2513
Josef Bacike2d2afe2016-11-29 12:27:09 -05002514 /* If we didn't map access then again we don't care about the
2515 * mismatched range values and it's ok if our old type was
2516 * UNKNOWN and we didn't go to a NOT_INIT'ed reg.
2517 */
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002518 if (rold->type == NOT_INIT ||
Josef Bacike2d2afe2016-11-29 12:27:09 -05002519 (!varlen_map_access && rold->type == UNKNOWN_VALUE &&
2520 rcur->type != NOT_INIT))
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002521 continue;
2522
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002523 if (rold->type == PTR_TO_PACKET && rcur->type == PTR_TO_PACKET &&
2524 compare_ptrs_to_packet(rold, rcur))
2525 continue;
2526
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002527 return false;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002528 }
2529
2530 for (i = 0; i < MAX_BPF_STACK; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002531 if (old->stack_slot_type[i] == STACK_INVALID)
2532 continue;
2533 if (old->stack_slot_type[i] != cur->stack_slot_type[i])
2534 /* Ex: old explored (safe) state has STACK_SPILL in
2535 * this stack slot, but current has has STACK_MISC ->
2536 * this verifier states are not equivalent,
2537 * return false to continue verification of this path
2538 */
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002539 return false;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002540 if (i % BPF_REG_SIZE)
2541 continue;
2542 if (memcmp(&old->spilled_regs[i / BPF_REG_SIZE],
2543 &cur->spilled_regs[i / BPF_REG_SIZE],
2544 sizeof(old->spilled_regs[0])))
2545 /* when explored and current stack slot types are
2546 * the same, check that stored pointers types
2547 * are the same as well.
2548 * Ex: explored safe path could have stored
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002549 * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -8}
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002550 * but current path has stored:
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002551 * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -16}
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002552 * such verifier states are not equivalent.
2553 * return false to continue verification of this path
2554 */
2555 return false;
2556 else
2557 continue;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002558 }
2559 return true;
2560}
2561
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002562static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002563{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002564 struct bpf_verifier_state_list *new_sl;
2565 struct bpf_verifier_state_list *sl;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002566
2567 sl = env->explored_states[insn_idx];
2568 if (!sl)
2569 /* this 'insn_idx' instruction wasn't marked, so we will not
2570 * be doing state search here
2571 */
2572 return 0;
2573
2574 while (sl != STATE_LIST_MARK) {
Josef Bacik48461132016-09-28 10:54:32 -04002575 if (states_equal(env, &sl->state, &env->cur_state))
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002576 /* reached equivalent register/stack state,
2577 * prune the search
2578 */
2579 return 1;
2580 sl = sl->next;
2581 }
2582
2583 /* there were no equivalent states, remember current one.
2584 * technically the current state is not proven to be safe yet,
2585 * but it will either reach bpf_exit (which means it's safe) or
2586 * it will be rejected. Since there are no loops, we won't be
2587 * seeing this 'insn_idx' instruction again on the way to bpf_exit
2588 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002589 new_sl = kmalloc(sizeof(struct bpf_verifier_state_list), GFP_USER);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002590 if (!new_sl)
2591 return -ENOMEM;
2592
2593 /* add new state to the head of linked list */
2594 memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state));
2595 new_sl->next = env->explored_states[insn_idx];
2596 env->explored_states[insn_idx] = new_sl;
2597 return 0;
2598}
2599
Jakub Kicinski13a27df2016-09-21 11:43:58 +01002600static int ext_analyzer_insn_hook(struct bpf_verifier_env *env,
2601 int insn_idx, int prev_insn_idx)
2602{
2603 if (!env->analyzer_ops || !env->analyzer_ops->insn_hook)
2604 return 0;
2605
2606 return env->analyzer_ops->insn_hook(env, insn_idx, prev_insn_idx);
2607}
2608
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002609static int do_check(struct bpf_verifier_env *env)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002610{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002611 struct bpf_verifier_state *state = &env->cur_state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002612 struct bpf_insn *insns = env->prog->insnsi;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002613 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002614 int insn_cnt = env->prog->len;
2615 int insn_idx, prev_insn_idx = 0;
2616 int insn_processed = 0;
2617 bool do_print_state = false;
2618
2619 init_reg_state(regs);
2620 insn_idx = 0;
Josef Bacik48461132016-09-28 10:54:32 -04002621 env->varlen_map_value_access = false;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002622 for (;;) {
2623 struct bpf_insn *insn;
2624 u8 class;
2625 int err;
2626
2627 if (insn_idx >= insn_cnt) {
2628 verbose("invalid insn idx %d insn_cnt %d\n",
2629 insn_idx, insn_cnt);
2630 return -EFAULT;
2631 }
2632
2633 insn = &insns[insn_idx];
2634 class = BPF_CLASS(insn->code);
2635
Daniel Borkmann07016152016-04-05 22:33:17 +02002636 if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002637 verbose("BPF program is too large. Proccessed %d insn\n",
2638 insn_processed);
2639 return -E2BIG;
2640 }
2641
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002642 err = is_state_visited(env, insn_idx);
2643 if (err < 0)
2644 return err;
2645 if (err == 1) {
2646 /* found equivalent state, can prune the search */
2647 if (log_level) {
2648 if (do_print_state)
2649 verbose("\nfrom %d to %d: safe\n",
2650 prev_insn_idx, insn_idx);
2651 else
2652 verbose("%d: safe\n", insn_idx);
2653 }
2654 goto process_bpf_exit;
2655 }
2656
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002657 if (log_level && do_print_state) {
2658 verbose("\nfrom %d to %d:", prev_insn_idx, insn_idx);
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002659 print_verifier_state(&env->cur_state);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002660 do_print_state = false;
2661 }
2662
2663 if (log_level) {
2664 verbose("%d: ", insn_idx);
2665 print_bpf_insn(insn);
2666 }
2667
Jakub Kicinski13a27df2016-09-21 11:43:58 +01002668 err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx);
2669 if (err)
2670 return err;
2671
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002672 if (class == BPF_ALU || class == BPF_ALU64) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002673 err = check_alu_op(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002674 if (err)
2675 return err;
2676
2677 } else if (class == BPF_LDX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002678 enum bpf_reg_type *prev_src_type, src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002679
2680 /* check for reserved fields is already done */
2681
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002682 /* check src operand */
2683 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2684 if (err)
2685 return err;
2686
2687 err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
2688 if (err)
2689 return err;
2690
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07002691 src_reg_type = regs[insn->src_reg].type;
2692
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002693 /* check that memory (src_reg + off) is readable,
2694 * the state of dst_reg will be updated by this func
2695 */
2696 err = check_mem_access(env, insn->src_reg, insn->off,
2697 BPF_SIZE(insn->code), BPF_READ,
2698 insn->dst_reg);
2699 if (err)
2700 return err;
2701
Josef Bacik48461132016-09-28 10:54:32 -04002702 reset_reg_range_values(regs, insn->dst_reg);
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07002703 if (BPF_SIZE(insn->code) != BPF_W &&
2704 BPF_SIZE(insn->code) != BPF_DW) {
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07002705 insn_idx++;
2706 continue;
2707 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002708
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002709 prev_src_type = &env->insn_aux_data[insn_idx].ptr_type;
2710
2711 if (*prev_src_type == NOT_INIT) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002712 /* saw a valid insn
2713 * dst_reg = *(u32 *)(src_reg + off)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002714 * save type to validate intersecting paths
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002715 */
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002716 *prev_src_type = src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002717
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002718 } else if (src_reg_type != *prev_src_type &&
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002719 (src_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002720 *prev_src_type == PTR_TO_CTX)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002721 /* ABuser program is trying to use the same insn
2722 * dst_reg = *(u32*) (src_reg + off)
2723 * with different pointer types:
2724 * src_reg == ctx in one branch and
2725 * src_reg == stack|map in some other branch.
2726 * Reject it.
2727 */
2728 verbose("same insn cannot be used with different pointers\n");
2729 return -EINVAL;
2730 }
2731
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002732 } else if (class == BPF_STX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002733 enum bpf_reg_type *prev_dst_type, dst_reg_type;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002734
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002735 if (BPF_MODE(insn->code) == BPF_XADD) {
2736 err = check_xadd(env, insn);
2737 if (err)
2738 return err;
2739 insn_idx++;
2740 continue;
2741 }
2742
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002743 /* check src1 operand */
2744 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2745 if (err)
2746 return err;
2747 /* check src2 operand */
2748 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
2749 if (err)
2750 return err;
2751
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002752 dst_reg_type = regs[insn->dst_reg].type;
2753
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002754 /* check that memory (dst_reg + off) is writeable */
2755 err = check_mem_access(env, insn->dst_reg, insn->off,
2756 BPF_SIZE(insn->code), BPF_WRITE,
2757 insn->src_reg);
2758 if (err)
2759 return err;
2760
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002761 prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type;
2762
2763 if (*prev_dst_type == NOT_INIT) {
2764 *prev_dst_type = dst_reg_type;
2765 } else if (dst_reg_type != *prev_dst_type &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002766 (dst_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002767 *prev_dst_type == PTR_TO_CTX)) {
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002768 verbose("same insn cannot be used with different pointers\n");
2769 return -EINVAL;
2770 }
2771
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002772 } else if (class == BPF_ST) {
2773 if (BPF_MODE(insn->code) != BPF_MEM ||
2774 insn->src_reg != BPF_REG_0) {
2775 verbose("BPF_ST uses reserved fields\n");
2776 return -EINVAL;
2777 }
2778 /* check src operand */
2779 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
2780 if (err)
2781 return err;
2782
2783 /* check that memory (dst_reg + off) is writeable */
2784 err = check_mem_access(env, insn->dst_reg, insn->off,
2785 BPF_SIZE(insn->code), BPF_WRITE,
2786 -1);
2787 if (err)
2788 return err;
2789
2790 } else if (class == BPF_JMP) {
2791 u8 opcode = BPF_OP(insn->code);
2792
2793 if (opcode == BPF_CALL) {
2794 if (BPF_SRC(insn->code) != BPF_K ||
2795 insn->off != 0 ||
2796 insn->src_reg != BPF_REG_0 ||
2797 insn->dst_reg != BPF_REG_0) {
2798 verbose("BPF_CALL uses reserved fields\n");
2799 return -EINVAL;
2800 }
2801
2802 err = check_call(env, insn->imm);
2803 if (err)
2804 return err;
2805
2806 } else if (opcode == BPF_JA) {
2807 if (BPF_SRC(insn->code) != BPF_K ||
2808 insn->imm != 0 ||
2809 insn->src_reg != BPF_REG_0 ||
2810 insn->dst_reg != BPF_REG_0) {
2811 verbose("BPF_JA uses reserved fields\n");
2812 return -EINVAL;
2813 }
2814
2815 insn_idx += insn->off + 1;
2816 continue;
2817
2818 } else if (opcode == BPF_EXIT) {
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_EXIT uses reserved fields\n");
2824 return -EINVAL;
2825 }
2826
2827 /* eBPF calling convetion is such that R0 is used
2828 * to return the value from eBPF program.
2829 * Make sure that it's readable at this time
2830 * of bpf_exit, which means that program wrote
2831 * something into it earlier
2832 */
2833 err = check_reg_arg(regs, BPF_REG_0, SRC_OP);
2834 if (err)
2835 return err;
2836
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002837 if (is_pointer_value(env, BPF_REG_0)) {
2838 verbose("R0 leaks addr as return value\n");
2839 return -EACCES;
2840 }
2841
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002842process_bpf_exit:
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002843 insn_idx = pop_stack(env, &prev_insn_idx);
2844 if (insn_idx < 0) {
2845 break;
2846 } else {
2847 do_print_state = true;
2848 continue;
2849 }
2850 } else {
2851 err = check_cond_jmp_op(env, insn, &insn_idx);
2852 if (err)
2853 return err;
2854 }
2855 } else if (class == BPF_LD) {
2856 u8 mode = BPF_MODE(insn->code);
2857
2858 if (mode == BPF_ABS || mode == BPF_IND) {
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002859 err = check_ld_abs(env, insn);
2860 if (err)
2861 return err;
2862
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002863 } else if (mode == BPF_IMM) {
2864 err = check_ld_imm(env, insn);
2865 if (err)
2866 return err;
2867
2868 insn_idx++;
2869 } else {
2870 verbose("invalid BPF_LD mode\n");
2871 return -EINVAL;
2872 }
Josef Bacik48461132016-09-28 10:54:32 -04002873 reset_reg_range_values(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002874 } else {
2875 verbose("unknown insn class %d\n", class);
2876 return -EINVAL;
2877 }
2878
2879 insn_idx++;
2880 }
2881
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002882 verbose("processed %d insns\n", insn_processed);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002883 return 0;
2884}
2885
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07002886static int check_map_prog_compatibility(struct bpf_map *map,
2887 struct bpf_prog *prog)
2888
2889{
2890 if (prog->type == BPF_PROG_TYPE_PERF_EVENT &&
2891 (map->map_type == BPF_MAP_TYPE_HASH ||
2892 map->map_type == BPF_MAP_TYPE_PERCPU_HASH) &&
2893 (map->map_flags & BPF_F_NO_PREALLOC)) {
2894 verbose("perf_event programs can only use preallocated hash map\n");
2895 return -EINVAL;
2896 }
2897 return 0;
2898}
2899
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002900/* look for pseudo eBPF instructions that access map FDs and
2901 * replace them with actual map pointers
2902 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002903static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002904{
2905 struct bpf_insn *insn = env->prog->insnsi;
2906 int insn_cnt = env->prog->len;
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07002907 int i, j, err;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002908
2909 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002910 if (BPF_CLASS(insn->code) == BPF_LDX &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002911 (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002912 verbose("BPF_LDX uses reserved fields\n");
2913 return -EINVAL;
2914 }
2915
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002916 if (BPF_CLASS(insn->code) == BPF_STX &&
2917 ((BPF_MODE(insn->code) != BPF_MEM &&
2918 BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
2919 verbose("BPF_STX uses reserved fields\n");
2920 return -EINVAL;
2921 }
2922
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002923 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
2924 struct bpf_map *map;
2925 struct fd f;
2926
2927 if (i == insn_cnt - 1 || insn[1].code != 0 ||
2928 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
2929 insn[1].off != 0) {
2930 verbose("invalid bpf_ld_imm64 insn\n");
2931 return -EINVAL;
2932 }
2933
2934 if (insn->src_reg == 0)
2935 /* valid generic load 64-bit imm */
2936 goto next_insn;
2937
2938 if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
2939 verbose("unrecognized bpf_ld_imm64 insn\n");
2940 return -EINVAL;
2941 }
2942
2943 f = fdget(insn->imm);
Daniel Borkmannc2101292015-10-29 14:58:07 +01002944 map = __bpf_map_get(f);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002945 if (IS_ERR(map)) {
2946 verbose("fd %d is not pointing to valid bpf_map\n",
2947 insn->imm);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002948 return PTR_ERR(map);
2949 }
2950
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07002951 err = check_map_prog_compatibility(map, env->prog);
2952 if (err) {
2953 fdput(f);
2954 return err;
2955 }
2956
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002957 /* store map pointer inside BPF_LD_IMM64 instruction */
2958 insn[0].imm = (u32) (unsigned long) map;
2959 insn[1].imm = ((u64) (unsigned long) map) >> 32;
2960
2961 /* check whether we recorded this map already */
2962 for (j = 0; j < env->used_map_cnt; j++)
2963 if (env->used_maps[j] == map) {
2964 fdput(f);
2965 goto next_insn;
2966 }
2967
2968 if (env->used_map_cnt >= MAX_USED_MAPS) {
2969 fdput(f);
2970 return -E2BIG;
2971 }
2972
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002973 /* hold the map. If the program is rejected by verifier,
2974 * the map will be released by release_maps() or it
2975 * will be used by the valid program until it's unloaded
2976 * and all maps are released in free_bpf_prog_info()
2977 */
Alexei Starovoitov92117d82016-04-27 18:56:20 -07002978 map = bpf_map_inc(map, false);
2979 if (IS_ERR(map)) {
2980 fdput(f);
2981 return PTR_ERR(map);
2982 }
2983 env->used_maps[env->used_map_cnt++] = map;
2984
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002985 fdput(f);
2986next_insn:
2987 insn++;
2988 i++;
2989 }
2990 }
2991
2992 /* now all pseudo BPF_LD_IMM64 instructions load valid
2993 * 'struct bpf_map *' into a register instead of user map_fd.
2994 * These pointers will be used later by verifier to validate map access.
2995 */
2996 return 0;
2997}
2998
2999/* drop refcnt of maps used by the rejected program */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003000static void release_maps(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003001{
3002 int i;
3003
3004 for (i = 0; i < env->used_map_cnt; i++)
3005 bpf_map_put(env->used_maps[i]);
3006}
3007
3008/* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003009static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003010{
3011 struct bpf_insn *insn = env->prog->insnsi;
3012 int insn_cnt = env->prog->len;
3013 int i;
3014
3015 for (i = 0; i < insn_cnt; i++, insn++)
3016 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
3017 insn->src_reg = 0;
3018}
3019
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003020/* convert load instructions that access fields of 'struct __sk_buff'
3021 * into sequence of instructions that access fields of 'struct sk_buff'
3022 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003023static int convert_ctx_accesses(struct bpf_verifier_env *env)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003024{
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003025 const struct bpf_verifier_ops *ops = env->prog->aux->ops;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003026 const int insn_cnt = env->prog->len;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003027 struct bpf_insn insn_buf[16], *insn;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003028 struct bpf_prog *new_prog;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003029 enum bpf_access_type type;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003030 int i, cnt, delta = 0;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003031
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003032 if (ops->gen_prologue) {
3033 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
3034 env->prog);
3035 if (cnt >= ARRAY_SIZE(insn_buf)) {
3036 verbose("bpf verifier is misconfigured\n");
3037 return -EINVAL;
3038 } else if (cnt) {
3039 new_prog = bpf_patch_insn_single(env->prog, 0,
3040 insn_buf, cnt);
3041 if (!new_prog)
3042 return -ENOMEM;
3043 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003044 delta += cnt - 1;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003045 }
3046 }
3047
3048 if (!ops->convert_ctx_access)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003049 return 0;
3050
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003051 insn = env->prog->insnsi + delta;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003052
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003053 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07003054 if (insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
3055 insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003056 type = BPF_READ;
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07003057 else if (insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
3058 insn->code == (BPF_STX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003059 type = BPF_WRITE;
3060 else
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003061 continue;
3062
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003063 if (env->insn_aux_data[i].ptr_type != PTR_TO_CTX)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003064 continue;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003065
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003066 cnt = ops->convert_ctx_access(type, insn->dst_reg, insn->src_reg,
3067 insn->off, insn_buf, env->prog);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003068 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
3069 verbose("bpf verifier is misconfigured\n");
3070 return -EINVAL;
3071 }
3072
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003073 new_prog = bpf_patch_insn_single(env->prog, i + delta, insn_buf,
3074 cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003075 if (!new_prog)
3076 return -ENOMEM;
3077
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003078 delta += cnt - 1;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003079
3080 /* keep walking new program and skip insns we just inserted */
3081 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003082 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003083 }
3084
3085 return 0;
3086}
3087
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003088static void free_states(struct bpf_verifier_env *env)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003089{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003090 struct bpf_verifier_state_list *sl, *sln;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003091 int i;
3092
3093 if (!env->explored_states)
3094 return;
3095
3096 for (i = 0; i < env->prog->len; i++) {
3097 sl = env->explored_states[i];
3098
3099 if (sl)
3100 while (sl != STATE_LIST_MARK) {
3101 sln = sl->next;
3102 kfree(sl);
3103 sl = sln;
3104 }
3105 }
3106
3107 kfree(env->explored_states);
3108}
3109
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003110int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003111{
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003112 char __user *log_ubuf = NULL;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003113 struct bpf_verifier_env *env;
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003114 int ret = -EINVAL;
3115
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003116 if ((*prog)->len <= 0 || (*prog)->len > BPF_MAXINSNS)
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003117 return -E2BIG;
3118
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003119 /* 'struct bpf_verifier_env' can be global, but since it's not small,
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003120 * allocate/free it every time bpf_check() is called
3121 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003122 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003123 if (!env)
3124 return -ENOMEM;
3125
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003126 env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
3127 (*prog)->len);
3128 ret = -ENOMEM;
3129 if (!env->insn_aux_data)
3130 goto err_free_env;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003131 env->prog = *prog;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003132
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003133 /* grab the mutex to protect few globals used by verifier */
3134 mutex_lock(&bpf_verifier_lock);
3135
3136 if (attr->log_level || attr->log_buf || attr->log_size) {
3137 /* user requested verbose verifier output
3138 * and supplied buffer to store the verification trace
3139 */
3140 log_level = attr->log_level;
3141 log_ubuf = (char __user *) (unsigned long) attr->log_buf;
3142 log_size = attr->log_size;
3143 log_len = 0;
3144
3145 ret = -EINVAL;
3146 /* log_* values have to be sane */
3147 if (log_size < 128 || log_size > UINT_MAX >> 8 ||
3148 log_level == 0 || log_ubuf == NULL)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003149 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003150
3151 ret = -ENOMEM;
3152 log_buf = vmalloc(log_size);
3153 if (!log_buf)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003154 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003155 } else {
3156 log_level = 0;
3157 }
3158
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003159 ret = replace_map_fd_with_map_ptr(env);
3160 if (ret < 0)
3161 goto skip_full_check;
3162
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003163 env->explored_states = kcalloc(env->prog->len,
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003164 sizeof(struct bpf_verifier_state_list *),
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003165 GFP_USER);
3166 ret = -ENOMEM;
3167 if (!env->explored_states)
3168 goto skip_full_check;
3169
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003170 ret = check_cfg(env);
3171 if (ret < 0)
3172 goto skip_full_check;
3173
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003174 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
3175
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003176 ret = do_check(env);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003177
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003178skip_full_check:
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003179 while (pop_stack(env, NULL) >= 0);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003180 free_states(env);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003181
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003182 if (ret == 0)
3183 /* program is valid, convert *(u32*)(ctx + off) accesses */
3184 ret = convert_ctx_accesses(env);
3185
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003186 if (log_level && log_len >= log_size - 1) {
3187 BUG_ON(log_len >= log_size);
3188 /* verifier log exceeded user supplied buffer */
3189 ret = -ENOSPC;
3190 /* fall through to return what was recorded */
3191 }
3192
3193 /* copy verifier log back to user space including trailing zero */
3194 if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) {
3195 ret = -EFAULT;
3196 goto free_log_buf;
3197 }
3198
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003199 if (ret == 0 && env->used_map_cnt) {
3200 /* if program passed verifier, update used_maps in bpf_prog_info */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003201 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
3202 sizeof(env->used_maps[0]),
3203 GFP_KERNEL);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003204
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003205 if (!env->prog->aux->used_maps) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003206 ret = -ENOMEM;
3207 goto free_log_buf;
3208 }
3209
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003210 memcpy(env->prog->aux->used_maps, env->used_maps,
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003211 sizeof(env->used_maps[0]) * env->used_map_cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003212 env->prog->aux->used_map_cnt = env->used_map_cnt;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003213
3214 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
3215 * bpf_ld_imm64 instructions
3216 */
3217 convert_pseudo_ld_imm64(env);
3218 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003219
3220free_log_buf:
3221 if (log_level)
3222 vfree(log_buf);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003223 if (!env->prog->aux->used_maps)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003224 /* if we didn't copy map pointers into bpf_prog_info, release
3225 * them now. Otherwise free_bpf_prog_info() will release them.
3226 */
3227 release_maps(env);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003228 *prog = env->prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003229err_unlock:
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003230 mutex_unlock(&bpf_verifier_lock);
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003231 vfree(env->insn_aux_data);
3232err_free_env:
3233 kfree(env);
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003234 return ret;
3235}
Jakub Kicinski13a27df2016-09-21 11:43:58 +01003236
3237int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops,
3238 void *priv)
3239{
3240 struct bpf_verifier_env *env;
3241 int ret;
3242
3243 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
3244 if (!env)
3245 return -ENOMEM;
3246
3247 env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
3248 prog->len);
3249 ret = -ENOMEM;
3250 if (!env->insn_aux_data)
3251 goto err_free_env;
3252 env->prog = prog;
3253 env->analyzer_ops = ops;
3254 env->analyzer_priv = priv;
3255
3256 /* grab the mutex to protect few globals used by verifier */
3257 mutex_lock(&bpf_verifier_lock);
3258
3259 log_level = 0;
3260
3261 env->explored_states = kcalloc(env->prog->len,
3262 sizeof(struct bpf_verifier_state_list *),
3263 GFP_KERNEL);
3264 ret = -ENOMEM;
3265 if (!env->explored_states)
3266 goto skip_full_check;
3267
3268 ret = check_cfg(env);
3269 if (ret < 0)
3270 goto skip_full_check;
3271
3272 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
3273
3274 ret = do_check(env);
3275
3276skip_full_check:
3277 while (pop_stack(env, NULL) >= 0);
3278 free_states(env);
3279
3280 mutex_unlock(&bpf_verifier_lock);
3281 vfree(env->insn_aux_data);
3282err_free_env:
3283 kfree(env);
3284 return ret;
3285}
3286EXPORT_SYMBOL_GPL(bpf_analyzer);