blob: 27370410c04d2eb6d88ffcd3170af31f111aee2c [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 Borkmann577aa832017-05-18 03:00:06 +0200142#define BPF_COMPLEXITY_LIMIT_INSNS 98304
Daniel Borkmann07016152016-04-05 22:33:17 +0200143#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
Daniel Borkmannced0a312017-05-08 00:04:09 +0200282static void print_bpf_insn(const struct bpf_verifier_env *env,
283 const struct bpf_insn *insn)
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700284{
285 u8 class = BPF_CLASS(insn->code);
286
287 if (class == BPF_ALU || class == BPF_ALU64) {
288 if (BPF_SRC(insn->code) == BPF_X)
289 verbose("(%02x) %sr%d %s %sr%d\n",
290 insn->code, class == BPF_ALU ? "(u32) " : "",
291 insn->dst_reg,
292 bpf_alu_string[BPF_OP(insn->code) >> 4],
293 class == BPF_ALU ? "(u32) " : "",
294 insn->src_reg);
295 else
296 verbose("(%02x) %sr%d %s %s%d\n",
297 insn->code, class == BPF_ALU ? "(u32) " : "",
298 insn->dst_reg,
299 bpf_alu_string[BPF_OP(insn->code) >> 4],
300 class == BPF_ALU ? "(u32) " : "",
301 insn->imm);
302 } else if (class == BPF_STX) {
303 if (BPF_MODE(insn->code) == BPF_MEM)
304 verbose("(%02x) *(%s *)(r%d %+d) = r%d\n",
305 insn->code,
306 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
307 insn->dst_reg,
308 insn->off, insn->src_reg);
309 else if (BPF_MODE(insn->code) == BPF_XADD)
310 verbose("(%02x) lock *(%s *)(r%d %+d) += r%d\n",
311 insn->code,
312 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
313 insn->dst_reg, insn->off,
314 insn->src_reg);
315 else
316 verbose("BUG_%02x\n", insn->code);
317 } else if (class == BPF_ST) {
318 if (BPF_MODE(insn->code) != BPF_MEM) {
319 verbose("BUG_st_%02x\n", insn->code);
320 return;
321 }
322 verbose("(%02x) *(%s *)(r%d %+d) = %d\n",
323 insn->code,
324 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
325 insn->dst_reg,
326 insn->off, insn->imm);
327 } else if (class == BPF_LDX) {
328 if (BPF_MODE(insn->code) != BPF_MEM) {
329 verbose("BUG_ldx_%02x\n", insn->code);
330 return;
331 }
332 verbose("(%02x) r%d = *(%s *)(r%d %+d)\n",
333 insn->code, insn->dst_reg,
334 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
335 insn->src_reg, insn->off);
336 } else if (class == BPF_LD) {
337 if (BPF_MODE(insn->code) == BPF_ABS) {
338 verbose("(%02x) r0 = *(%s *)skb[%d]\n",
339 insn->code,
340 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
341 insn->imm);
342 } else if (BPF_MODE(insn->code) == BPF_IND) {
343 verbose("(%02x) r0 = *(%s *)skb[r%d + %d]\n",
344 insn->code,
345 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
346 insn->src_reg, insn->imm);
Daniel Borkmannced0a312017-05-08 00:04:09 +0200347 } else if (BPF_MODE(insn->code) == BPF_IMM &&
348 BPF_SIZE(insn->code) == BPF_DW) {
349 /* At this point, we already made sure that the second
350 * part of the ldimm64 insn is accessible.
351 */
352 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
353 bool map_ptr = insn->src_reg == BPF_PSEUDO_MAP_FD;
354
355 if (map_ptr && !env->allow_ptr_leaks)
356 imm = 0;
357
358 verbose("(%02x) r%d = 0x%llx\n", insn->code,
359 insn->dst_reg, (unsigned long long)imm);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700360 } else {
361 verbose("BUG_ld_%02x\n", insn->code);
362 return;
363 }
364 } else if (class == BPF_JMP) {
365 u8 opcode = BPF_OP(insn->code);
366
367 if (opcode == BPF_CALL) {
368 verbose("(%02x) call %d\n", insn->code, insn->imm);
369 } else if (insn->code == (BPF_JMP | BPF_JA)) {
370 verbose("(%02x) goto pc%+d\n",
371 insn->code, insn->off);
372 } else if (insn->code == (BPF_JMP | BPF_EXIT)) {
373 verbose("(%02x) exit\n", insn->code);
374 } else if (BPF_SRC(insn->code) == BPF_X) {
375 verbose("(%02x) if r%d %s r%d goto pc%+d\n",
376 insn->code, insn->dst_reg,
377 bpf_jmp_string[BPF_OP(insn->code) >> 4],
378 insn->src_reg, insn->off);
379 } else {
380 verbose("(%02x) if r%d %s 0x%x goto pc%+d\n",
381 insn->code, insn->dst_reg,
382 bpf_jmp_string[BPF_OP(insn->code) >> 4],
383 insn->imm, insn->off);
384 }
385 } else {
386 verbose("(%02x) %s\n", insn->code, bpf_class_string[class]);
387 }
388}
389
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100390static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700391{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100392 struct bpf_verifier_stack_elem *elem;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700393 int insn_idx;
394
395 if (env->head == NULL)
396 return -1;
397
398 memcpy(&env->cur_state, &env->head->st, sizeof(env->cur_state));
399 insn_idx = env->head->insn_idx;
400 if (prev_insn_idx)
401 *prev_insn_idx = env->head->prev_insn_idx;
402 elem = env->head->next;
403 kfree(env->head);
404 env->head = elem;
405 env->stack_size--;
406 return insn_idx;
407}
408
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100409static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,
410 int insn_idx, int prev_insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700411{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100412 struct bpf_verifier_stack_elem *elem;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700413
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100414 elem = kmalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700415 if (!elem)
416 goto err;
417
418 memcpy(&elem->st, &env->cur_state, sizeof(env->cur_state));
419 elem->insn_idx = insn_idx;
420 elem->prev_insn_idx = prev_insn_idx;
421 elem->next = env->head;
422 env->head = elem;
423 env->stack_size++;
Daniel Borkmann07016152016-04-05 22:33:17 +0200424 if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700425 verbose("BPF program is too complex\n");
426 goto err;
427 }
428 return &elem->st;
429err:
430 /* pop all elements and return */
431 while (pop_stack(env, NULL) >= 0);
432 return NULL;
433}
434
435#define CALLER_SAVED_REGS 6
436static const int caller_saved[CALLER_SAVED_REGS] = {
437 BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
438};
439
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100440static void init_reg_state(struct bpf_reg_state *regs)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700441{
442 int i;
443
444 for (i = 0; i < MAX_BPF_REG; i++) {
445 regs[i].type = NOT_INIT;
446 regs[i].imm = 0;
Josef Bacik48461132016-09-28 10:54:32 -0400447 regs[i].min_value = BPF_REGISTER_MIN_RANGE;
448 regs[i].max_value = BPF_REGISTER_MAX_RANGE;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700449 }
450
451 /* frame pointer */
452 regs[BPF_REG_FP].type = FRAME_PTR;
453
454 /* 1st arg to a function */
455 regs[BPF_REG_1].type = PTR_TO_CTX;
456}
457
Daniel Borkmann0e0f1d62016-12-18 01:52:59 +0100458static void __mark_reg_unknown_value(struct bpf_reg_state *regs, u32 regno)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700459{
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700460 regs[regno].type = UNKNOWN_VALUE;
Thomas Graf14117072016-10-18 19:51:19 +0200461 regs[regno].id = 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700462 regs[regno].imm = 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700463}
464
Daniel Borkmann0e0f1d62016-12-18 01:52:59 +0100465static void mark_reg_unknown_value(struct bpf_reg_state *regs, u32 regno)
466{
467 BUG_ON(regno >= MAX_BPF_REG);
468 __mark_reg_unknown_value(regs, regno);
469}
470
Josef Bacik48461132016-09-28 10:54:32 -0400471static void reset_reg_range_values(struct bpf_reg_state *regs, u32 regno)
472{
473 regs[regno].min_value = BPF_REGISTER_MIN_RANGE;
474 regs[regno].max_value = BPF_REGISTER_MAX_RANGE;
475}
476
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700477enum reg_arg_type {
478 SRC_OP, /* register is used as source operand */
479 DST_OP, /* register is used as destination operand */
480 DST_OP_NO_MARK /* same as above, check only, don't mark */
481};
482
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100483static int check_reg_arg(struct bpf_reg_state *regs, u32 regno,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700484 enum reg_arg_type t)
485{
486 if (regno >= MAX_BPF_REG) {
487 verbose("R%d is invalid\n", regno);
488 return -EINVAL;
489 }
490
491 if (t == SRC_OP) {
492 /* check whether register used as source operand can be read */
493 if (regs[regno].type == NOT_INIT) {
494 verbose("R%d !read_ok\n", regno);
495 return -EACCES;
496 }
497 } else {
498 /* check whether register used as dest operand can be written to */
499 if (regno == BPF_REG_FP) {
500 verbose("frame pointer is read only\n");
501 return -EACCES;
502 }
503 if (t == DST_OP)
504 mark_reg_unknown_value(regs, regno);
505 }
506 return 0;
507}
508
509static int bpf_size_to_bytes(int bpf_size)
510{
511 if (bpf_size == BPF_W)
512 return 4;
513 else if (bpf_size == BPF_H)
514 return 2;
515 else if (bpf_size == BPF_B)
516 return 1;
517 else if (bpf_size == BPF_DW)
518 return 8;
519 else
520 return -EINVAL;
521}
522
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700523static bool is_spillable_regtype(enum bpf_reg_type type)
524{
525 switch (type) {
526 case PTR_TO_MAP_VALUE:
527 case PTR_TO_MAP_VALUE_OR_NULL:
528 case PTR_TO_STACK:
529 case PTR_TO_CTX:
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700530 case PTR_TO_PACKET:
531 case PTR_TO_PACKET_END:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700532 case FRAME_PTR:
533 case CONST_PTR_TO_MAP:
534 return true;
535 default:
536 return false;
537 }
538}
539
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700540/* check_stack_read/write functions track spill/fill of registers,
541 * stack boundary and alignment are checked in check_mem_access()
542 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100543static int check_stack_write(struct bpf_verifier_state *state, int off,
544 int size, int value_regno)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700545{
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700546 int i;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700547 /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
548 * so it's aligned access and [off, off + size) are within stack limits
549 */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700550
551 if (value_regno >= 0 &&
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700552 is_spillable_regtype(state->regs[value_regno].type)) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700553
554 /* register containing pointer is being spilled into stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700555 if (size != BPF_REG_SIZE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700556 verbose("invalid size of register spill\n");
557 return -EACCES;
558 }
559
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700560 /* save register state */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700561 state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] =
562 state->regs[value_regno];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700563
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700564 for (i = 0; i < BPF_REG_SIZE; i++)
565 state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_SPILL;
566 } else {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700567 /* regular write of data into stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700568 state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] =
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100569 (struct bpf_reg_state) {};
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700570
571 for (i = 0; i < size; i++)
572 state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_MISC;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700573 }
574 return 0;
575}
576
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100577static int check_stack_read(struct bpf_verifier_state *state, int off, int size,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700578 int value_regno)
579{
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700580 u8 *slot_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700581 int i;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700582
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700583 slot_type = &state->stack_slot_type[MAX_BPF_STACK + off];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700584
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700585 if (slot_type[0] == STACK_SPILL) {
586 if (size != BPF_REG_SIZE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700587 verbose("invalid size of register spill\n");
588 return -EACCES;
589 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700590 for (i = 1; i < BPF_REG_SIZE; i++) {
591 if (slot_type[i] != STACK_SPILL) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700592 verbose("corrupted spill memory\n");
593 return -EACCES;
594 }
595 }
596
597 if (value_regno >= 0)
598 /* restore register state from stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700599 state->regs[value_regno] =
600 state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700601 return 0;
602 } else {
603 for (i = 0; i < size; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700604 if (slot_type[i] != STACK_MISC) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700605 verbose("invalid read from stack off %d+%d size %d\n",
606 off, i, size);
607 return -EACCES;
608 }
609 }
610 if (value_regno >= 0)
611 /* have read misc data from the stack */
612 mark_reg_unknown_value(state->regs, value_regno);
613 return 0;
614 }
615}
616
617/* check read/write into map element returned by bpf_map_lookup_elem() */
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100618static int check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700619 int size)
620{
621 struct bpf_map *map = env->cur_state.regs[regno].map_ptr;
622
623 if (off < 0 || off + size > map->value_size) {
624 verbose("invalid access to map value, value_size=%d off=%d size=%d\n",
625 map->value_size, off, size);
626 return -EACCES;
627 }
628 return 0;
629}
630
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700631#define MAX_PACKET_OFF 0xffff
632
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100633static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200634 const struct bpf_call_arg_meta *meta)
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700635{
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200636 switch (env->prog->type) {
637 case BPF_PROG_TYPE_SCHED_CLS:
638 case BPF_PROG_TYPE_SCHED_ACT:
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700639 case BPF_PROG_TYPE_XDP:
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200640 if (meta)
641 return meta->pkt_access;
642
643 env->seen_direct_write = true;
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700644 return true;
645 default:
646 return false;
647 }
648}
649
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100650static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700651 int size)
652{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100653 struct bpf_reg_state *regs = env->cur_state.regs;
654 struct bpf_reg_state *reg = &regs[regno];
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700655
Alexei Starovoitovd91b28e2016-05-19 18:17:13 -0700656 off += reg->off;
Daniel Borkmannb399cf62016-09-20 00:26:12 +0200657 if (off < 0 || size <= 0 || off + size > reg->range) {
Alexei Starovoitovd91b28e2016-05-19 18:17:13 -0700658 verbose("invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n",
659 off, size, regno, reg->id, reg->off, reg->range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700660 return -EACCES;
661 }
662 return 0;
663}
664
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700665/* check access to 'struct bpf_context' fields */
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100666static int check_ctx_access(struct bpf_verifier_env *env, int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700667 enum bpf_access_type t, enum bpf_reg_type *reg_type)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700668{
Jakub Kicinski13a27df2016-09-21 11:43:58 +0100669 /* for analyzer ctx accesses are already validated and converted */
670 if (env->analyzer_ops)
671 return 0;
672
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700673 if (env->prog->aux->ops->is_valid_access &&
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700674 env->prog->aux->ops->is_valid_access(off, size, t, reg_type)) {
Alexei Starovoitov32bbe002016-04-06 18:43:28 -0700675 /* remember the offset of last byte accessed in ctx */
676 if (env->prog->aux->max_ctx_offset < off + size)
677 env->prog->aux->max_ctx_offset = off + size;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700678 return 0;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -0700679 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700680
681 verbose("invalid bpf_context access off=%d size=%d\n", off, size);
682 return -EACCES;
683}
684
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +0200685static bool __is_pointer_value(bool allow_ptr_leaks,
686 const struct bpf_reg_state *reg)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700687{
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +0200688 if (allow_ptr_leaks)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700689 return false;
690
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +0200691 switch (reg->type) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700692 case UNKNOWN_VALUE:
693 case CONST_IMM:
694 return false;
695 default:
696 return true;
697 }
698}
699
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +0200700static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
701{
702 return __is_pointer_value(env->allow_ptr_leaks, &env->cur_state.regs[regno]);
703}
704
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100705static int check_ptr_alignment(struct bpf_verifier_env *env,
706 struct bpf_reg_state *reg, int off, int size)
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700707{
Josef Bacik48461132016-09-28 10:54:32 -0400708 if (reg->type != PTR_TO_PACKET && reg->type != PTR_TO_MAP_VALUE_ADJ) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700709 if (off % size != 0) {
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100710 verbose("misaligned access off %d size %d\n",
711 off, size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700712 return -EACCES;
713 } else {
714 return 0;
715 }
716 }
717
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700718 if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
719 /* misaligned access to packet is ok on x86,arm,arm64 */
720 return 0;
721
722 if (reg->id && size != 1) {
723 verbose("Unknown packet alignment. Only byte-sized access allowed\n");
724 return -EACCES;
725 }
726
727 /* skb->data is NET_IP_ALIGN-ed */
Josef Bacik48461132016-09-28 10:54:32 -0400728 if (reg->type == PTR_TO_PACKET &&
729 (NET_IP_ALIGN + reg->off + off) % size != 0) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700730 verbose("misaligned packet access off %d+%d+%d size %d\n",
731 NET_IP_ALIGN, reg->off, off, size);
732 return -EACCES;
733 }
734 return 0;
735}
736
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700737/* check whether memory at (regno + off) is accessible for t = (read | write)
738 * if t==write, value_regno is a register which value is stored into memory
739 * if t==read, value_regno is a register which will receive the value from memory
740 * if t==write && value_regno==-1, some unknown value is stored into memory
741 * if t==read && value_regno==-1, don't care what we read from memory
742 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100743static int check_mem_access(struct bpf_verifier_env *env, u32 regno, int off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700744 int bpf_size, enum bpf_access_type t,
745 int value_regno)
746{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100747 struct bpf_verifier_state *state = &env->cur_state;
748 struct bpf_reg_state *reg = &state->regs[regno];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700749 int size, err = 0;
750
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700751 if (reg->type == PTR_TO_STACK)
752 off += reg->imm;
Alex Gartrell24b4d2a2015-07-23 14:24:40 -0700753
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700754 size = bpf_size_to_bytes(bpf_size);
755 if (size < 0)
756 return size;
757
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700758 err = check_ptr_alignment(env, reg, off, size);
759 if (err)
760 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700761
Josef Bacik48461132016-09-28 10:54:32 -0400762 if (reg->type == PTR_TO_MAP_VALUE ||
763 reg->type == PTR_TO_MAP_VALUE_ADJ) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700764 if (t == BPF_WRITE && value_regno >= 0 &&
765 is_pointer_value(env, value_regno)) {
766 verbose("R%d leaks addr into map\n", value_regno);
767 return -EACCES;
768 }
Josef Bacik48461132016-09-28 10:54:32 -0400769
770 /* If we adjusted the register to this map value at all then we
771 * need to change off and size to min_value and max_value
772 * respectively to make sure our theoretical access will be
773 * safe.
774 */
775 if (reg->type == PTR_TO_MAP_VALUE_ADJ) {
776 if (log_level)
777 print_verifier_state(state);
778 env->varlen_map_value_access = true;
779 /* The minimum value is only important with signed
780 * comparisons where we can't assume the floor of a
781 * value is 0. If we are using signed variables for our
782 * index'es we need to make sure that whatever we use
783 * will have a set floor within our range.
784 */
Josef Bacikf23cc642016-11-14 15:45:36 -0500785 if (reg->min_value < 0) {
Josef Bacik48461132016-09-28 10:54:32 -0400786 verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
787 regno);
788 return -EACCES;
789 }
790 err = check_map_access(env, regno, reg->min_value + off,
791 size);
792 if (err) {
793 verbose("R%d min value is outside of the array range\n",
794 regno);
795 return err;
796 }
797
798 /* If we haven't set a max value then we need to bail
799 * since we can't be sure we won't do bad things.
800 */
801 if (reg->max_value == BPF_REGISTER_MAX_RANGE) {
802 verbose("R%d unbounded memory access, make sure to bounds check any array access into a map\n",
803 regno);
804 return -EACCES;
805 }
806 off += reg->max_value;
807 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700808 err = check_map_access(env, regno, off, size);
809 if (!err && t == BPF_READ && value_regno >= 0)
810 mark_reg_unknown_value(state->regs, value_regno);
811
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700812 } else if (reg->type == PTR_TO_CTX) {
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700813 enum bpf_reg_type reg_type = UNKNOWN_VALUE;
814
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700815 if (t == BPF_WRITE && value_regno >= 0 &&
816 is_pointer_value(env, value_regno)) {
817 verbose("R%d leaks addr into ctx\n", value_regno);
818 return -EACCES;
819 }
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700820 err = check_ctx_access(env, off, size, t, &reg_type);
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700821 if (!err && t == BPF_READ && value_regno >= 0) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700822 mark_reg_unknown_value(state->regs, value_regno);
Mickaël Salaün19553512016-09-24 20:01:50 +0200823 /* note that reg.[id|off|range] == 0 */
824 state->regs[value_regno].type = reg_type;
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700825 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700826
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700827 } else if (reg->type == FRAME_PTR || reg->type == PTR_TO_STACK) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700828 if (off >= 0 || off < -MAX_BPF_STACK) {
829 verbose("invalid stack off=%d size=%d\n", off, size);
830 return -EACCES;
831 }
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700832 if (t == BPF_WRITE) {
833 if (!env->allow_ptr_leaks &&
834 state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL &&
835 size != BPF_REG_SIZE) {
836 verbose("attempt to corrupt spilled pointer on stack\n");
837 return -EACCES;
838 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700839 err = check_stack_write(state, off, size, value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700840 } else {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700841 err = check_stack_read(state, off, size, value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700842 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700843 } else if (state->regs[regno].type == PTR_TO_PACKET) {
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200844 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL)) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700845 verbose("cannot write into packet\n");
846 return -EACCES;
847 }
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700848 if (t == BPF_WRITE && value_regno >= 0 &&
849 is_pointer_value(env, value_regno)) {
850 verbose("R%d leaks addr into packet\n", value_regno);
851 return -EACCES;
852 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700853 err = check_packet_access(env, regno, off, size);
854 if (!err && t == BPF_READ && value_regno >= 0)
855 mark_reg_unknown_value(state->regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700856 } else {
857 verbose("R%d invalid mem access '%s'\n",
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700858 regno, reg_type_str[reg->type]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700859 return -EACCES;
860 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700861
862 if (!err && size <= 2 && value_regno >= 0 && env->allow_ptr_leaks &&
863 state->regs[value_regno].type == UNKNOWN_VALUE) {
864 /* 1 or 2 byte load zero-extends, determine the number of
865 * zero upper bits. Not doing it fo 4 byte load, since
866 * such values cannot be added to ptr_to_packet anyway.
867 */
868 state->regs[value_regno].imm = 64 - size * 8;
869 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700870 return err;
871}
872
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100873static int check_xadd(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700874{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100875 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700876 int err;
877
878 if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
879 insn->imm != 0) {
880 verbose("BPF_XADD uses reserved fields\n");
881 return -EINVAL;
882 }
883
884 /* check src1 operand */
885 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
886 if (err)
887 return err;
888
889 /* check src2 operand */
890 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
891 if (err)
892 return err;
893
Daniel Borkmanncd5de9c2017-06-29 03:04:59 +0200894 if (is_pointer_value(env, insn->src_reg)) {
895 verbose("R%d leaks addr into mem\n", insn->src_reg);
896 return -EACCES;
897 }
898
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700899 /* check whether atomic_add can read the memory */
900 err = check_mem_access(env, insn->dst_reg, insn->off,
901 BPF_SIZE(insn->code), BPF_READ, -1);
902 if (err)
903 return err;
904
905 /* check whether atomic_add can write into the same memory */
906 return check_mem_access(env, insn->dst_reg, insn->off,
907 BPF_SIZE(insn->code), BPF_WRITE, -1);
908}
909
910/* when register 'regno' is passed into function that will read 'access_size'
911 * bytes from that pointer, make sure that it's within stack boundary
912 * and all elements of stack are initialized
913 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100914static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
Daniel Borkmann435faee12016-04-13 00:10:51 +0200915 int access_size, bool zero_size_allowed,
916 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700917{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100918 struct bpf_verifier_state *state = &env->cur_state;
919 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700920 int off, i;
921
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +0100922 if (regs[regno].type != PTR_TO_STACK) {
923 if (zero_size_allowed && access_size == 0 &&
924 regs[regno].type == CONST_IMM &&
925 regs[regno].imm == 0)
926 return 0;
927
928 verbose("R%d type=%s expected=%s\n", regno,
929 reg_type_str[regs[regno].type],
930 reg_type_str[PTR_TO_STACK]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700931 return -EACCES;
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +0100932 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700933
934 off = regs[regno].imm;
935 if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
936 access_size <= 0) {
937 verbose("invalid stack type R%d off=%d access_size=%d\n",
938 regno, off, access_size);
939 return -EACCES;
940 }
941
Daniel Borkmann435faee12016-04-13 00:10:51 +0200942 if (meta && meta->raw_mode) {
943 meta->access_size = access_size;
944 meta->regno = regno;
945 return 0;
946 }
947
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700948 for (i = 0; i < access_size; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700949 if (state->stack_slot_type[MAX_BPF_STACK + off + i] != STACK_MISC) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700950 verbose("invalid indirect read from stack off %d+%d size %d\n",
951 off, i, access_size);
952 return -EACCES;
953 }
954 }
955 return 0;
956}
957
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100958static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200959 enum bpf_arg_type arg_type,
960 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700961{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100962 struct bpf_reg_state *regs = env->cur_state.regs, *reg = &regs[regno];
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700963 enum bpf_reg_type expected_type, type = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700964 int err = 0;
965
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100966 if (arg_type == ARG_DONTCARE)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700967 return 0;
968
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700969 if (type == NOT_INIT) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700970 verbose("R%d !read_ok\n", regno);
971 return -EACCES;
972 }
973
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700974 if (arg_type == ARG_ANYTHING) {
975 if (is_pointer_value(env, regno)) {
976 verbose("R%d leaks addr into helper function\n", regno);
977 return -EACCES;
978 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100979 return 0;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700980 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100981
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200982 if (type == PTR_TO_PACKET && !may_access_direct_pkt_data(env, meta)) {
983 verbose("helper access to the packet is not allowed\n");
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700984 return -EACCES;
985 }
986
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +0100987 if (arg_type == ARG_PTR_TO_MAP_KEY ||
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700988 arg_type == ARG_PTR_TO_MAP_VALUE) {
989 expected_type = PTR_TO_STACK;
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700990 if (type != PTR_TO_PACKET && type != expected_type)
991 goto err_type;
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +0100992 } else if (arg_type == ARG_CONST_STACK_SIZE ||
993 arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700994 expected_type = CONST_IMM;
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700995 if (type != expected_type)
996 goto err_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700997 } else if (arg_type == ARG_CONST_MAP_PTR) {
998 expected_type = CONST_PTR_TO_MAP;
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700999 if (type != expected_type)
1000 goto err_type;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001001 } else if (arg_type == ARG_PTR_TO_CTX) {
1002 expected_type = PTR_TO_CTX;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001003 if (type != expected_type)
1004 goto err_type;
Daniel Borkmann435faee12016-04-13 00:10:51 +02001005 } else if (arg_type == ARG_PTR_TO_STACK ||
1006 arg_type == ARG_PTR_TO_RAW_STACK) {
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001007 expected_type = PTR_TO_STACK;
1008 /* One exception here. In case function allows for NULL to be
1009 * passed in as argument, it's a CONST_IMM type. Final test
1010 * happens during stack boundary checking.
1011 */
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001012 if (type == CONST_IMM && reg->imm == 0)
1013 /* final test in check_stack_boundary() */;
1014 else if (type != PTR_TO_PACKET && type != expected_type)
1015 goto err_type;
Daniel Borkmann435faee12016-04-13 00:10:51 +02001016 meta->raw_mode = arg_type == ARG_PTR_TO_RAW_STACK;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001017 } else {
1018 verbose("unsupported arg_type %d\n", arg_type);
1019 return -EFAULT;
1020 }
1021
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001022 if (arg_type == ARG_CONST_MAP_PTR) {
1023 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001024 meta->map_ptr = reg->map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001025 } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
1026 /* bpf_map_xxx(..., map_ptr, ..., key) call:
1027 * check that [key, key + map->key_size) are within
1028 * stack limits and initialized
1029 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001030 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001031 /* in function declaration map_ptr must come before
1032 * map_key, so that it's verified and known before
1033 * we have to check map_key here. Otherwise it means
1034 * that kernel subsystem misconfigured verifier
1035 */
1036 verbose("invalid map_ptr to access map->key\n");
1037 return -EACCES;
1038 }
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001039 if (type == PTR_TO_PACKET)
1040 err = check_packet_access(env, regno, 0,
1041 meta->map_ptr->key_size);
1042 else
1043 err = check_stack_boundary(env, regno,
1044 meta->map_ptr->key_size,
1045 false, NULL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001046 } else if (arg_type == ARG_PTR_TO_MAP_VALUE) {
1047 /* bpf_map_xxx(..., map_ptr, ..., value) call:
1048 * check [value, value + map->value_size) validity
1049 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001050 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001051 /* kernel subsystem misconfigured verifier */
1052 verbose("invalid map_ptr to access map->value\n");
1053 return -EACCES;
1054 }
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001055 if (type == PTR_TO_PACKET)
1056 err = check_packet_access(env, regno, 0,
1057 meta->map_ptr->value_size);
1058 else
1059 err = check_stack_boundary(env, regno,
1060 meta->map_ptr->value_size,
1061 false, NULL);
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001062 } else if (arg_type == ARG_CONST_STACK_SIZE ||
1063 arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) {
1064 bool zero_size_allowed = (arg_type == ARG_CONST_STACK_SIZE_OR_ZERO);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001065
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001066 /* bpf_xxx(..., buf, len) call will access 'len' bytes
1067 * from stack pointer 'buf'. Check it
1068 * note: regno == len, regno - 1 == buf
1069 */
1070 if (regno == 0) {
1071 /* kernel subsystem misconfigured verifier */
1072 verbose("ARG_CONST_STACK_SIZE cannot be first argument\n");
1073 return -EACCES;
1074 }
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001075 if (regs[regno - 1].type == PTR_TO_PACKET)
1076 err = check_packet_access(env, regno - 1, 0, reg->imm);
1077 else
1078 err = check_stack_boundary(env, regno - 1, reg->imm,
1079 zero_size_allowed, meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001080 }
1081
1082 return err;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001083err_type:
1084 verbose("R%d type=%s expected=%s\n", regno,
1085 reg_type_str[type], reg_type_str[expected_type]);
1086 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001087}
1088
Kaixu Xia35578d72015-08-06 07:02:35 +00001089static int check_map_func_compatibility(struct bpf_map *map, int func_id)
1090{
Kaixu Xia35578d72015-08-06 07:02:35 +00001091 if (!map)
1092 return 0;
1093
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001094 /* We need a two way check, first is from map perspective ... */
1095 switch (map->map_type) {
1096 case BPF_MAP_TYPE_PROG_ARRAY:
1097 if (func_id != BPF_FUNC_tail_call)
1098 goto error;
1099 break;
1100 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
1101 if (func_id != BPF_FUNC_perf_event_read &&
1102 func_id != BPF_FUNC_perf_event_output)
1103 goto error;
1104 break;
1105 case BPF_MAP_TYPE_STACK_TRACE:
1106 if (func_id != BPF_FUNC_get_stackid)
1107 goto error;
1108 break;
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -07001109 case BPF_MAP_TYPE_CGROUP_ARRAY:
David S. Miller60747ef2016-08-18 01:17:32 -04001110 if (func_id != BPF_FUNC_skb_under_cgroup &&
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001111 func_id != BPF_FUNC_current_task_under_cgroup)
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07001112 goto error;
1113 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001114 default:
1115 break;
1116 }
1117
1118 /* ... and second from the function itself. */
1119 switch (func_id) {
1120 case BPF_FUNC_tail_call:
1121 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1122 goto error;
1123 break;
1124 case BPF_FUNC_perf_event_read:
1125 case BPF_FUNC_perf_event_output:
1126 if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
1127 goto error;
1128 break;
1129 case BPF_FUNC_get_stackid:
1130 if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
1131 goto error;
1132 break;
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001133 case BPF_FUNC_current_task_under_cgroup:
Daniel Borkmann747ea552016-08-12 22:17:17 +02001134 case BPF_FUNC_skb_under_cgroup:
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07001135 if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
1136 goto error;
1137 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001138 default:
1139 break;
Kaixu Xia35578d72015-08-06 07:02:35 +00001140 }
1141
1142 return 0;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001143error:
1144 verbose("cannot pass map_type %d into func %d\n",
1145 map->map_type, func_id);
1146 return -EINVAL;
Kaixu Xia35578d72015-08-06 07:02:35 +00001147}
1148
Daniel Borkmann435faee12016-04-13 00:10:51 +02001149static int check_raw_mode(const struct bpf_func_proto *fn)
1150{
1151 int count = 0;
1152
1153 if (fn->arg1_type == ARG_PTR_TO_RAW_STACK)
1154 count++;
1155 if (fn->arg2_type == ARG_PTR_TO_RAW_STACK)
1156 count++;
1157 if (fn->arg3_type == ARG_PTR_TO_RAW_STACK)
1158 count++;
1159 if (fn->arg4_type == ARG_PTR_TO_RAW_STACK)
1160 count++;
1161 if (fn->arg5_type == ARG_PTR_TO_RAW_STACK)
1162 count++;
1163
1164 return count > 1 ? -EINVAL : 0;
1165}
1166
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001167static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001168{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001169 struct bpf_verifier_state *state = &env->cur_state;
1170 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001171 int i;
1172
1173 for (i = 0; i < MAX_BPF_REG; i++)
1174 if (regs[i].type == PTR_TO_PACKET ||
1175 regs[i].type == PTR_TO_PACKET_END)
1176 mark_reg_unknown_value(regs, i);
1177
1178 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
1179 if (state->stack_slot_type[i] != STACK_SPILL)
1180 continue;
1181 reg = &state->spilled_regs[i / BPF_REG_SIZE];
1182 if (reg->type != PTR_TO_PACKET &&
1183 reg->type != PTR_TO_PACKET_END)
1184 continue;
1185 reg->type = UNKNOWN_VALUE;
1186 reg->imm = 0;
1187 }
1188}
1189
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001190static int check_call(struct bpf_verifier_env *env, int func_id)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001191{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001192 struct bpf_verifier_state *state = &env->cur_state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001193 const struct bpf_func_proto *fn = NULL;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001194 struct bpf_reg_state *regs = state->regs;
1195 struct bpf_reg_state *reg;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001196 struct bpf_call_arg_meta meta;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001197 bool changes_data;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001198 int i, err;
1199
1200 /* find function prototype */
1201 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
1202 verbose("invalid func %d\n", func_id);
1203 return -EINVAL;
1204 }
1205
1206 if (env->prog->aux->ops->get_func_proto)
1207 fn = env->prog->aux->ops->get_func_proto(func_id);
1208
1209 if (!fn) {
1210 verbose("unknown func %d\n", func_id);
1211 return -EINVAL;
1212 }
1213
1214 /* eBPF programs must be GPL compatible to use GPL-ed functions */
Daniel Borkmann24701ec2015-03-01 12:31:47 +01001215 if (!env->prog->gpl_compatible && fn->gpl_only) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001216 verbose("cannot call GPL only function from proprietary program\n");
1217 return -EINVAL;
1218 }
1219
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001220 changes_data = bpf_helper_changes_skb_data(fn->func);
1221
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001222 memset(&meta, 0, sizeof(meta));
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001223 meta.pkt_access = fn->pkt_access;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001224
Daniel Borkmann435faee12016-04-13 00:10:51 +02001225 /* We only support one arg being in raw mode at the moment, which
1226 * is sufficient for the helper functions we have right now.
1227 */
1228 err = check_raw_mode(fn);
1229 if (err) {
1230 verbose("kernel subsystem misconfigured func %d\n", func_id);
1231 return err;
1232 }
1233
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001234 /* check args */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001235 err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001236 if (err)
1237 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001238 err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001239 if (err)
1240 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001241 err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001242 if (err)
1243 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001244 err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001245 if (err)
1246 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001247 err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001248 if (err)
1249 return err;
1250
Daniel Borkmann435faee12016-04-13 00:10:51 +02001251 /* Mark slots with STACK_MISC in case of raw mode, stack offset
1252 * is inferred from register state.
1253 */
1254 for (i = 0; i < meta.access_size; i++) {
1255 err = check_mem_access(env, meta.regno, i, BPF_B, BPF_WRITE, -1);
1256 if (err)
1257 return err;
1258 }
1259
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001260 /* reset caller saved regs */
1261 for (i = 0; i < CALLER_SAVED_REGS; i++) {
1262 reg = regs + caller_saved[i];
1263 reg->type = NOT_INIT;
1264 reg->imm = 0;
1265 }
1266
1267 /* update return register */
1268 if (fn->ret_type == RET_INTEGER) {
1269 regs[BPF_REG_0].type = UNKNOWN_VALUE;
1270 } else if (fn->ret_type == RET_VOID) {
1271 regs[BPF_REG_0].type = NOT_INIT;
1272 } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) {
1273 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
Josef Bacik48461132016-09-28 10:54:32 -04001274 regs[BPF_REG_0].max_value = regs[BPF_REG_0].min_value = 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001275 /* remember map_ptr, so that check_map_access()
1276 * can check 'value_size' boundary of memory access
1277 * to map element returned from bpf_map_lookup_elem()
1278 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001279 if (meta.map_ptr == NULL) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001280 verbose("kernel subsystem misconfigured verifier\n");
1281 return -EINVAL;
1282 }
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001283 regs[BPF_REG_0].map_ptr = meta.map_ptr;
Thomas Graf14117072016-10-18 19:51:19 +02001284 regs[BPF_REG_0].id = ++env->id_gen;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001285 } else {
1286 verbose("unknown return type %d of func %d\n",
1287 fn->ret_type, func_id);
1288 return -EINVAL;
1289 }
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -07001290
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001291 err = check_map_func_compatibility(meta.map_ptr, func_id);
Kaixu Xia35578d72015-08-06 07:02:35 +00001292 if (err)
1293 return err;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -07001294
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001295 if (changes_data)
1296 clear_all_pkt_pointers(env);
1297 return 0;
1298}
1299
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001300static int check_packet_ptr_add(struct bpf_verifier_env *env,
1301 struct bpf_insn *insn)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001302{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001303 struct bpf_reg_state *regs = env->cur_state.regs;
1304 struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
1305 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
1306 struct bpf_reg_state tmp_reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001307 s32 imm;
1308
1309 if (BPF_SRC(insn->code) == BPF_K) {
1310 /* pkt_ptr += imm */
1311 imm = insn->imm;
1312
1313add_imm:
1314 if (imm <= 0) {
1315 verbose("addition of negative constant to packet pointer is not allowed\n");
1316 return -EACCES;
1317 }
1318 if (imm >= MAX_PACKET_OFF ||
1319 imm + dst_reg->off >= MAX_PACKET_OFF) {
1320 verbose("constant %d is too large to add to packet pointer\n",
1321 imm);
1322 return -EACCES;
1323 }
1324 /* a constant was added to pkt_ptr.
1325 * Remember it while keeping the same 'id'
1326 */
1327 dst_reg->off += imm;
1328 } else {
Alexei Starovoitov1b9b69e2016-05-19 18:17:14 -07001329 if (src_reg->type == PTR_TO_PACKET) {
1330 /* R6=pkt(id=0,off=0,r=62) R7=imm22; r7 += r6 */
1331 tmp_reg = *dst_reg; /* save r7 state */
1332 *dst_reg = *src_reg; /* copy pkt_ptr state r6 into r7 */
1333 src_reg = &tmp_reg; /* pretend it's src_reg state */
1334 /* if the checks below reject it, the copy won't matter,
1335 * since we're rejecting the whole program. If all ok,
1336 * then imm22 state will be added to r7
1337 * and r7 will be pkt(id=0,off=22,r=62) while
1338 * r6 will stay as pkt(id=0,off=0,r=62)
1339 */
1340 }
1341
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001342 if (src_reg->type == CONST_IMM) {
1343 /* pkt_ptr += reg where reg is known constant */
1344 imm = src_reg->imm;
1345 goto add_imm;
1346 }
1347 /* disallow pkt_ptr += reg
1348 * if reg is not uknown_value with guaranteed zero upper bits
1349 * otherwise pkt_ptr may overflow and addition will become
1350 * subtraction which is not allowed
1351 */
1352 if (src_reg->type != UNKNOWN_VALUE) {
1353 verbose("cannot add '%s' to ptr_to_packet\n",
1354 reg_type_str[src_reg->type]);
1355 return -EACCES;
1356 }
1357 if (src_reg->imm < 48) {
1358 verbose("cannot add integer value with %lld upper zero bits to ptr_to_packet\n",
1359 src_reg->imm);
1360 return -EACCES;
1361 }
1362 /* dst_reg stays as pkt_ptr type and since some positive
1363 * integer value was added to the pointer, increment its 'id'
1364 */
Jakub Kicinski1f415a72016-08-02 16:12:14 +01001365 dst_reg->id = ++env->id_gen;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001366
1367 /* something was added to pkt_ptr, set range and off to zero */
1368 dst_reg->off = 0;
1369 dst_reg->range = 0;
1370 }
1371 return 0;
1372}
1373
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001374static int evaluate_reg_alu(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001375{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001376 struct bpf_reg_state *regs = env->cur_state.regs;
1377 struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001378 u8 opcode = BPF_OP(insn->code);
1379 s64 imm_log2;
1380
1381 /* for type == UNKNOWN_VALUE:
1382 * imm > 0 -> number of zero upper bits
1383 * imm == 0 -> don't track which is the same as all bits can be non-zero
1384 */
1385
1386 if (BPF_SRC(insn->code) == BPF_X) {
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001387 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001388
1389 if (src_reg->type == UNKNOWN_VALUE && src_reg->imm > 0 &&
1390 dst_reg->imm && opcode == BPF_ADD) {
1391 /* dreg += sreg
1392 * where both have zero upper bits. Adding them
1393 * can only result making one more bit non-zero
1394 * in the larger value.
1395 * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47)
1396 * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47)
1397 */
1398 dst_reg->imm = min(dst_reg->imm, src_reg->imm);
1399 dst_reg->imm--;
1400 return 0;
1401 }
1402 if (src_reg->type == CONST_IMM && src_reg->imm > 0 &&
1403 dst_reg->imm && opcode == BPF_ADD) {
1404 /* dreg += sreg
1405 * where dreg has zero upper bits and sreg is const.
1406 * Adding them can only result making one more bit
1407 * non-zero in the larger value.
1408 */
1409 imm_log2 = __ilog2_u64((long long)src_reg->imm);
1410 dst_reg->imm = min(dst_reg->imm, 63 - imm_log2);
1411 dst_reg->imm--;
1412 return 0;
1413 }
1414 /* all other cases non supported yet, just mark dst_reg */
1415 dst_reg->imm = 0;
1416 return 0;
1417 }
1418
1419 /* sign extend 32-bit imm into 64-bit to make sure that
1420 * negative values occupy bit 63. Note ilog2() would have
1421 * been incorrect, since sizeof(insn->imm) == 4
1422 */
1423 imm_log2 = __ilog2_u64((long long)insn->imm);
1424
1425 if (dst_reg->imm && opcode == BPF_LSH) {
1426 /* reg <<= imm
1427 * if reg was a result of 2 byte load, then its imm == 48
1428 * which means that upper 48 bits are zero and shifting this reg
1429 * left by 4 would mean that upper 44 bits are still zero
1430 */
1431 dst_reg->imm -= insn->imm;
1432 } else if (dst_reg->imm && opcode == BPF_MUL) {
1433 /* reg *= imm
1434 * if multiplying by 14 subtract 4
1435 * This is conservative calculation of upper zero bits.
1436 * It's not trying to special case insn->imm == 1 or 0 cases
1437 */
1438 dst_reg->imm -= imm_log2 + 1;
1439 } else if (opcode == BPF_AND) {
1440 /* reg &= imm */
1441 dst_reg->imm = 63 - imm_log2;
1442 } else if (dst_reg->imm && opcode == BPF_ADD) {
1443 /* reg += imm */
1444 dst_reg->imm = min(dst_reg->imm, 63 - imm_log2);
1445 dst_reg->imm--;
1446 } else if (opcode == BPF_RSH) {
1447 /* reg >>= imm
1448 * which means that after right shift, upper bits will be zero
1449 * note that verifier already checked that
1450 * 0 <= imm < 64 for shift insn
1451 */
1452 dst_reg->imm += insn->imm;
1453 if (unlikely(dst_reg->imm > 64))
1454 /* some dumb code did:
1455 * r2 = *(u32 *)mem;
1456 * r2 >>= 32;
1457 * and all bits are zero now */
1458 dst_reg->imm = 64;
1459 } else {
1460 /* all other alu ops, means that we don't know what will
1461 * happen to the value, mark it with unknown number of zero bits
1462 */
1463 dst_reg->imm = 0;
1464 }
1465
1466 if (dst_reg->imm < 0) {
1467 /* all 64 bits of the register can contain non-zero bits
1468 * and such value cannot be added to ptr_to_packet, since it
1469 * may overflow, mark it as unknown to avoid further eval
1470 */
1471 dst_reg->imm = 0;
1472 }
1473 return 0;
1474}
1475
John Fastabende37bdee2017-07-02 02:13:30 +02001476static int evaluate_reg_imm_alu_unknown(struct bpf_verifier_env *env,
1477 struct bpf_insn *insn)
1478{
1479 struct bpf_reg_state *regs = env->cur_state.regs;
1480 struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
1481 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
1482 u8 opcode = BPF_OP(insn->code);
1483 s64 imm_log2 = __ilog2_u64((long long)dst_reg->imm);
1484
1485 /* BPF_X code with src_reg->type UNKNOWN_VALUE here. */
1486 if (src_reg->imm > 0 && dst_reg->imm) {
1487 switch (opcode) {
1488 case BPF_ADD:
1489 /* dreg += sreg
1490 * where both have zero upper bits. Adding them
1491 * can only result making one more bit non-zero
1492 * in the larger value.
1493 * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47)
1494 * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47)
1495 */
1496 dst_reg->imm = min(src_reg->imm, 63 - imm_log2);
1497 dst_reg->imm--;
1498 break;
1499 case BPF_AND:
1500 /* dreg &= sreg
1501 * AND can not extend zero bits only shrink
1502 * Ex. 0x00..00ffffff
1503 * & 0x0f..ffffffff
1504 * ----------------
1505 * 0x00..00ffffff
1506 */
1507 dst_reg->imm = max(src_reg->imm, 63 - imm_log2);
1508 break;
1509 case BPF_OR:
1510 /* dreg |= sreg
1511 * OR can only extend zero bits
1512 * Ex. 0x00..00ffffff
1513 * | 0x0f..ffffffff
1514 * ----------------
1515 * 0x0f..00ffffff
1516 */
1517 dst_reg->imm = min(src_reg->imm, 63 - imm_log2);
1518 break;
1519 case BPF_SUB:
1520 case BPF_MUL:
1521 case BPF_RSH:
1522 case BPF_LSH:
1523 /* These may be flushed out later */
1524 default:
1525 mark_reg_unknown_value(regs, insn->dst_reg);
1526 }
1527 } else {
1528 mark_reg_unknown_value(regs, insn->dst_reg);
1529 }
1530
1531 dst_reg->type = UNKNOWN_VALUE;
1532 return 0;
1533}
1534
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001535static int evaluate_reg_imm_alu(struct bpf_verifier_env *env,
1536 struct bpf_insn *insn)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001537{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001538 struct bpf_reg_state *regs = env->cur_state.regs;
1539 struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
1540 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001541 u8 opcode = BPF_OP(insn->code);
1542
John Fastabende37bdee2017-07-02 02:13:30 +02001543 if (BPF_SRC(insn->code) == BPF_X && src_reg->type == UNKNOWN_VALUE)
1544 return evaluate_reg_imm_alu_unknown(env, insn);
1545
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001546 /* dst_reg->type == CONST_IMM here, simulate execution of 'add' insn.
1547 * Don't care about overflow or negative values, just add them
1548 */
1549 if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_K)
1550 dst_reg->imm += insn->imm;
1551 else if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_X &&
1552 src_reg->type == CONST_IMM)
1553 dst_reg->imm += src_reg->imm;
1554 else
1555 mark_reg_unknown_value(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001556 return 0;
1557}
1558
Josef Bacik48461132016-09-28 10:54:32 -04001559static void check_reg_overflow(struct bpf_reg_state *reg)
1560{
1561 if (reg->max_value > BPF_REGISTER_MAX_RANGE)
1562 reg->max_value = BPF_REGISTER_MAX_RANGE;
Josef Bacikf23cc642016-11-14 15:45:36 -05001563 if (reg->min_value < BPF_REGISTER_MIN_RANGE ||
1564 reg->min_value > BPF_REGISTER_MAX_RANGE)
Josef Bacik48461132016-09-28 10:54:32 -04001565 reg->min_value = BPF_REGISTER_MIN_RANGE;
1566}
1567
1568static void adjust_reg_min_max_vals(struct bpf_verifier_env *env,
1569 struct bpf_insn *insn)
1570{
1571 struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg;
Josef Bacikf23cc642016-11-14 15:45:36 -05001572 s64 min_val = BPF_REGISTER_MIN_RANGE;
1573 u64 max_val = BPF_REGISTER_MAX_RANGE;
Josef Bacik48461132016-09-28 10:54:32 -04001574 bool min_set = false, max_set = false;
1575 u8 opcode = BPF_OP(insn->code);
1576
1577 dst_reg = &regs[insn->dst_reg];
1578 if (BPF_SRC(insn->code) == BPF_X) {
1579 check_reg_overflow(&regs[insn->src_reg]);
1580 min_val = regs[insn->src_reg].min_value;
1581 max_val = regs[insn->src_reg].max_value;
1582
1583 /* If the source register is a random pointer then the
1584 * min_value/max_value values represent the range of the known
1585 * accesses into that value, not the actual min/max value of the
1586 * register itself. In this case we have to reset the reg range
1587 * values so we know it is not safe to look at.
1588 */
1589 if (regs[insn->src_reg].type != CONST_IMM &&
1590 regs[insn->src_reg].type != UNKNOWN_VALUE) {
1591 min_val = BPF_REGISTER_MIN_RANGE;
1592 max_val = BPF_REGISTER_MAX_RANGE;
1593 }
1594 } else if (insn->imm < BPF_REGISTER_MAX_RANGE &&
1595 (s64)insn->imm > BPF_REGISTER_MIN_RANGE) {
1596 min_val = max_val = insn->imm;
1597 min_set = max_set = true;
1598 }
1599
1600 /* We don't know anything about what was done to this register, mark it
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02001601 * as unknown. Also, if both derived bounds came from signed/unsigned
1602 * mixed compares and one side is unbounded, we cannot really do anything
1603 * with them as boundaries cannot be trusted. Thus, arithmetic of two
1604 * regs of such kind will get invalidated bounds on the dst side.
Josef Bacik48461132016-09-28 10:54:32 -04001605 */
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02001606 if ((min_val == BPF_REGISTER_MIN_RANGE &&
1607 max_val == BPF_REGISTER_MAX_RANGE) ||
1608 (BPF_SRC(insn->code) == BPF_X &&
1609 ((min_val != BPF_REGISTER_MIN_RANGE &&
1610 max_val == BPF_REGISTER_MAX_RANGE) ||
1611 (min_val == BPF_REGISTER_MIN_RANGE &&
1612 max_val != BPF_REGISTER_MAX_RANGE) ||
1613 (dst_reg->min_value != BPF_REGISTER_MIN_RANGE &&
1614 dst_reg->max_value == BPF_REGISTER_MAX_RANGE) ||
1615 (dst_reg->min_value == BPF_REGISTER_MIN_RANGE &&
1616 dst_reg->max_value != BPF_REGISTER_MAX_RANGE)) &&
1617 regs[insn->dst_reg].value_from_signed !=
1618 regs[insn->src_reg].value_from_signed)) {
Josef Bacik48461132016-09-28 10:54:32 -04001619 reset_reg_range_values(regs, insn->dst_reg);
1620 return;
1621 }
1622
Josef Bacikf23cc642016-11-14 15:45:36 -05001623 /* If one of our values was at the end of our ranges then we can't just
1624 * do our normal operations to the register, we need to set the values
1625 * to the min/max since they are undefined.
1626 */
Edward Cree655da3d2017-07-21 14:37:34 +01001627 if (opcode != BPF_SUB) {
1628 if (min_val == BPF_REGISTER_MIN_RANGE)
1629 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
1630 if (max_val == BPF_REGISTER_MAX_RANGE)
1631 dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
1632 }
Josef Bacikf23cc642016-11-14 15:45:36 -05001633
Josef Bacik48461132016-09-28 10:54:32 -04001634 switch (opcode) {
1635 case BPF_ADD:
Josef Bacikf23cc642016-11-14 15:45:36 -05001636 if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
1637 dst_reg->min_value += min_val;
1638 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1639 dst_reg->max_value += max_val;
Josef Bacik48461132016-09-28 10:54:32 -04001640 break;
1641 case BPF_SUB:
Edward Cree655da3d2017-07-21 14:37:34 +01001642 /* If one of our values was at the end of our ranges, then the
1643 * _opposite_ value in the dst_reg goes to the end of our range.
1644 */
1645 if (min_val == BPF_REGISTER_MIN_RANGE)
1646 dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
1647 if (max_val == BPF_REGISTER_MAX_RANGE)
1648 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
Josef Bacikf23cc642016-11-14 15:45:36 -05001649 if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
Edward Cree655da3d2017-07-21 14:37:34 +01001650 dst_reg->min_value -= max_val;
Josef Bacikf23cc642016-11-14 15:45:36 -05001651 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
Edward Cree655da3d2017-07-21 14:37:34 +01001652 dst_reg->max_value -= min_val;
Josef Bacik48461132016-09-28 10:54:32 -04001653 break;
1654 case BPF_MUL:
Josef Bacikf23cc642016-11-14 15:45:36 -05001655 if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
1656 dst_reg->min_value *= min_val;
1657 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1658 dst_reg->max_value *= max_val;
Josef Bacik48461132016-09-28 10:54:32 -04001659 break;
1660 case BPF_AND:
Josef Bacikf23cc642016-11-14 15:45:36 -05001661 /* Disallow AND'ing of negative numbers, ain't nobody got time
1662 * for that. Otherwise the minimum is 0 and the max is the max
1663 * value we could AND against.
1664 */
1665 if (min_val < 0)
1666 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
1667 else
1668 dst_reg->min_value = 0;
Josef Bacik48461132016-09-28 10:54:32 -04001669 dst_reg->max_value = max_val;
1670 break;
1671 case BPF_LSH:
1672 /* Gotta have special overflow logic here, if we're shifting
1673 * more than MAX_RANGE then just assume we have an invalid
1674 * range.
1675 */
1676 if (min_val > ilog2(BPF_REGISTER_MAX_RANGE))
1677 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
Josef Bacikf23cc642016-11-14 15:45:36 -05001678 else if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
Josef Bacik48461132016-09-28 10:54:32 -04001679 dst_reg->min_value <<= min_val;
1680
1681 if (max_val > ilog2(BPF_REGISTER_MAX_RANGE))
1682 dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
Josef Bacikf23cc642016-11-14 15:45:36 -05001683 else if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
Josef Bacik48461132016-09-28 10:54:32 -04001684 dst_reg->max_value <<= max_val;
1685 break;
1686 case BPF_RSH:
Josef Bacikf23cc642016-11-14 15:45:36 -05001687 /* RSH by a negative number is undefined, and the BPF_RSH is an
1688 * unsigned shift, so make the appropriate casts.
Josef Bacik48461132016-09-28 10:54:32 -04001689 */
Josef Bacikf23cc642016-11-14 15:45:36 -05001690 if (min_val < 0 || dst_reg->min_value < 0)
1691 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
1692 else
1693 dst_reg->min_value =
1694 (u64)(dst_reg->min_value) >> min_val;
1695 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1696 dst_reg->max_value >>= max_val;
Josef Bacik48461132016-09-28 10:54:32 -04001697 break;
1698 default:
1699 reset_reg_range_values(regs, insn->dst_reg);
1700 break;
1701 }
1702
1703 check_reg_overflow(dst_reg);
1704}
1705
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001706/* check validity of 32-bit and 64-bit arithmetic operations */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001707static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001708{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001709 struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001710 u8 opcode = BPF_OP(insn->code);
1711 int err;
1712
1713 if (opcode == BPF_END || opcode == BPF_NEG) {
1714 if (opcode == BPF_NEG) {
1715 if (BPF_SRC(insn->code) != 0 ||
1716 insn->src_reg != BPF_REG_0 ||
1717 insn->off != 0 || insn->imm != 0) {
1718 verbose("BPF_NEG uses reserved fields\n");
1719 return -EINVAL;
1720 }
1721 } else {
1722 if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
Edward Creee1594922017-09-15 14:37:38 +01001723 (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) ||
1724 BPF_CLASS(insn->code) == BPF_ALU64) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001725 verbose("BPF_END uses reserved fields\n");
1726 return -EINVAL;
1727 }
1728 }
1729
1730 /* check src operand */
1731 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
1732 if (err)
1733 return err;
1734
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001735 if (is_pointer_value(env, insn->dst_reg)) {
1736 verbose("R%d pointer arithmetic prohibited\n",
1737 insn->dst_reg);
1738 return -EACCES;
1739 }
1740
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001741 /* check dest operand */
1742 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
1743 if (err)
1744 return err;
1745
1746 } else if (opcode == BPF_MOV) {
1747
1748 if (BPF_SRC(insn->code) == BPF_X) {
1749 if (insn->imm != 0 || insn->off != 0) {
1750 verbose("BPF_MOV uses reserved fields\n");
1751 return -EINVAL;
1752 }
1753
1754 /* check src operand */
1755 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1756 if (err)
1757 return err;
1758 } else {
1759 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
1760 verbose("BPF_MOV uses reserved fields\n");
1761 return -EINVAL;
1762 }
1763 }
1764
1765 /* check dest operand */
1766 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
1767 if (err)
1768 return err;
1769
Josef Bacik48461132016-09-28 10:54:32 -04001770 /* we are setting our register to something new, we need to
1771 * reset its range values.
1772 */
1773 reset_reg_range_values(regs, insn->dst_reg);
1774
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001775 if (BPF_SRC(insn->code) == BPF_X) {
1776 if (BPF_CLASS(insn->code) == BPF_ALU64) {
1777 /* case: R1 = R2
1778 * copy register state to dest reg
1779 */
1780 regs[insn->dst_reg] = regs[insn->src_reg];
1781 } else {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001782 if (is_pointer_value(env, insn->src_reg)) {
1783 verbose("R%d partial copy of pointer\n",
1784 insn->src_reg);
1785 return -EACCES;
1786 }
Thomas Graf14117072016-10-18 19:51:19 +02001787 mark_reg_unknown_value(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001788 }
1789 } else {
1790 /* case: R = imm
1791 * remember the value we stored into this reg
1792 */
Daniel Borkmann3695b3b2017-12-22 16:29:05 +01001793 u64 imm;
1794
1795 if (BPF_CLASS(insn->code) == BPF_ALU64)
1796 imm = insn->imm;
1797 else
1798 imm = (u32)insn->imm;
1799
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001800 regs[insn->dst_reg].type = CONST_IMM;
Daniel Borkmann3695b3b2017-12-22 16:29:05 +01001801 regs[insn->dst_reg].imm = imm;
1802 regs[insn->dst_reg].max_value = imm;
1803 regs[insn->dst_reg].min_value = imm;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001804 }
1805
1806 } else if (opcode > BPF_END) {
1807 verbose("invalid BPF_ALU opcode %x\n", opcode);
1808 return -EINVAL;
1809
1810 } else { /* all other ALU ops: and, sub, xor, add, ... */
1811
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001812 if (BPF_SRC(insn->code) == BPF_X) {
1813 if (insn->imm != 0 || insn->off != 0) {
1814 verbose("BPF_ALU uses reserved fields\n");
1815 return -EINVAL;
1816 }
1817 /* check src1 operand */
1818 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1819 if (err)
1820 return err;
1821 } else {
1822 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
1823 verbose("BPF_ALU uses reserved fields\n");
1824 return -EINVAL;
1825 }
1826 }
1827
1828 /* check src2 operand */
1829 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
1830 if (err)
1831 return err;
1832
1833 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
1834 BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
1835 verbose("div by zero\n");
1836 return -EINVAL;
1837 }
1838
Rabin Vincent229394e2016-01-12 20:17:08 +01001839 if ((opcode == BPF_LSH || opcode == BPF_RSH ||
1840 opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
1841 int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
1842
1843 if (insn->imm < 0 || insn->imm >= size) {
1844 verbose("invalid shift %d\n", insn->imm);
1845 return -EINVAL;
1846 }
1847 }
1848
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07001849 /* check dest operand */
1850 err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
1851 if (err)
1852 return err;
1853
1854 dst_reg = &regs[insn->dst_reg];
1855
Josef Bacik48461132016-09-28 10:54:32 -04001856 /* first we want to adjust our ranges. */
1857 adjust_reg_min_max_vals(env, insn);
1858
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001859 /* pattern match 'bpf_add Rx, imm' instruction */
1860 if (opcode == BPF_ADD && BPF_CLASS(insn->code) == BPF_ALU64 &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07001861 dst_reg->type == FRAME_PTR && BPF_SRC(insn->code) == BPF_K) {
1862 dst_reg->type = PTR_TO_STACK;
1863 dst_reg->imm = insn->imm;
1864 return 0;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001865 } else if (opcode == BPF_ADD &&
1866 BPF_CLASS(insn->code) == BPF_ALU64 &&
Yonghong Song7bca0a92017-04-29 22:52:42 -07001867 dst_reg->type == PTR_TO_STACK &&
1868 ((BPF_SRC(insn->code) == BPF_X &&
1869 regs[insn->src_reg].type == CONST_IMM) ||
1870 BPF_SRC(insn->code) == BPF_K)) {
Daniel Borkmannd75d3ee2017-12-22 16:29:04 +01001871 if (BPF_SRC(insn->code) == BPF_X) {
1872 /* check in case the register contains a big
1873 * 64-bit value
1874 */
1875 if (regs[insn->src_reg].imm < -MAX_BPF_STACK ||
1876 regs[insn->src_reg].imm > MAX_BPF_STACK) {
1877 verbose("R%d value too big in R%d pointer arithmetic\n",
1878 insn->src_reg, insn->dst_reg);
1879 return -EACCES;
1880 }
Yonghong Song7bca0a92017-04-29 22:52:42 -07001881 dst_reg->imm += regs[insn->src_reg].imm;
Daniel Borkmannd75d3ee2017-12-22 16:29:04 +01001882 } else {
1883 /* safe against overflow: addition of 32-bit
1884 * numbers in 64-bit representation
1885 */
Yonghong Song7bca0a92017-04-29 22:52:42 -07001886 dst_reg->imm += insn->imm;
Daniel Borkmannd75d3ee2017-12-22 16:29:04 +01001887 }
1888 if (dst_reg->imm > 0 || dst_reg->imm < -MAX_BPF_STACK) {
1889 verbose("R%d out-of-bounds pointer arithmetic\n",
1890 insn->dst_reg);
1891 return -EACCES;
1892 }
Yonghong Song7bca0a92017-04-29 22:52:42 -07001893 return 0;
1894 } else if (opcode == BPF_ADD &&
1895 BPF_CLASS(insn->code) == BPF_ALU64 &&
Alexei Starovoitov1b9b69e2016-05-19 18:17:14 -07001896 (dst_reg->type == PTR_TO_PACKET ||
1897 (BPF_SRC(insn->code) == BPF_X &&
1898 regs[insn->src_reg].type == PTR_TO_PACKET))) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001899 /* ptr_to_packet += K|X */
1900 return check_packet_ptr_add(env, insn);
1901 } else if (BPF_CLASS(insn->code) == BPF_ALU64 &&
1902 dst_reg->type == UNKNOWN_VALUE &&
1903 env->allow_ptr_leaks) {
1904 /* unknown += K|X */
1905 return evaluate_reg_alu(env, insn);
1906 } else if (BPF_CLASS(insn->code) == BPF_ALU64 &&
1907 dst_reg->type == CONST_IMM &&
1908 env->allow_ptr_leaks) {
1909 /* reg_imm += K|X */
1910 return evaluate_reg_imm_alu(env, insn);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001911 } else if (is_pointer_value(env, insn->dst_reg)) {
1912 verbose("R%d pointer arithmetic prohibited\n",
1913 insn->dst_reg);
1914 return -EACCES;
1915 } else if (BPF_SRC(insn->code) == BPF_X &&
1916 is_pointer_value(env, insn->src_reg)) {
1917 verbose("R%d pointer arithmetic prohibited\n",
1918 insn->src_reg);
1919 return -EACCES;
1920 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001921
Josef Bacik48461132016-09-28 10:54:32 -04001922 /* If we did pointer math on a map value then just set it to our
1923 * PTR_TO_MAP_VALUE_ADJ type so we can deal with any stores or
1924 * loads to this register appropriately, otherwise just mark the
1925 * register as unknown.
1926 */
1927 if (env->allow_ptr_leaks &&
Daniel Borkmann8d674be2017-03-31 02:24:02 +02001928 BPF_CLASS(insn->code) == BPF_ALU64 && opcode == BPF_ADD &&
Josef Bacik48461132016-09-28 10:54:32 -04001929 (dst_reg->type == PTR_TO_MAP_VALUE ||
1930 dst_reg->type == PTR_TO_MAP_VALUE_ADJ))
1931 dst_reg->type = PTR_TO_MAP_VALUE_ADJ;
1932 else
1933 mark_reg_unknown_value(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001934 }
1935
1936 return 0;
1937}
1938
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001939static void find_good_pkt_pointers(struct bpf_verifier_state *state,
1940 struct bpf_reg_state *dst_reg)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001941{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001942 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001943 int i;
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02001944
1945 /* LLVM can generate two kind of checks:
1946 *
1947 * Type 1:
1948 *
1949 * r2 = r3;
1950 * r2 += 8;
1951 * if (r2 > pkt_end) goto <handle exception>
1952 * <access okay>
1953 *
1954 * Where:
1955 * r2 == dst_reg, pkt_end == src_reg
1956 * r2=pkt(id=n,off=8,r=0)
1957 * r3=pkt(id=n,off=0,r=0)
1958 *
1959 * Type 2:
1960 *
1961 * r2 = r3;
1962 * r2 += 8;
1963 * if (pkt_end >= r2) goto <access okay>
1964 * <handle exception>
1965 *
1966 * Where:
1967 * pkt_end == dst_reg, r2 == src_reg
1968 * r2=pkt(id=n,off=8,r=0)
1969 * r3=pkt(id=n,off=0,r=0)
1970 *
1971 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
1972 * so that range of bytes [r3, r3 + 8) is safe to access.
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001973 */
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02001974
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001975 for (i = 0; i < MAX_BPF_REG; i++)
1976 if (regs[i].type == PTR_TO_PACKET && regs[i].id == dst_reg->id)
Alexei Starovoitov0ea3c232017-03-24 15:57:33 -07001977 /* keep the maximum range already checked */
1978 regs[i].range = max(regs[i].range, dst_reg->off);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001979
1980 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
1981 if (state->stack_slot_type[i] != STACK_SPILL)
1982 continue;
1983 reg = &state->spilled_regs[i / BPF_REG_SIZE];
1984 if (reg->type == PTR_TO_PACKET && reg->id == dst_reg->id)
Alexei Starovoitov0ea3c232017-03-24 15:57:33 -07001985 reg->range = max(reg->range, dst_reg->off);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001986 }
1987}
1988
Josef Bacik48461132016-09-28 10:54:32 -04001989/* Adjusts the register min/max values in the case that the dst_reg is the
1990 * variable register that we are working on, and src_reg is a constant or we're
1991 * simply doing a BPF_K check.
1992 */
1993static void reg_set_min_max(struct bpf_reg_state *true_reg,
1994 struct bpf_reg_state *false_reg, u64 val,
1995 u8 opcode)
1996{
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02001997 bool value_from_signed = true;
1998 bool is_range = true;
1999
Josef Bacik48461132016-09-28 10:54:32 -04002000 switch (opcode) {
2001 case BPF_JEQ:
2002 /* If this is false then we know nothing Jon Snow, but if it is
2003 * true then we know for sure.
2004 */
2005 true_reg->max_value = true_reg->min_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002006 is_range = false;
Josef Bacik48461132016-09-28 10:54:32 -04002007 break;
2008 case BPF_JNE:
2009 /* If this is true we know nothing Jon Snow, but if it is false
2010 * we know the value for sure;
2011 */
2012 false_reg->max_value = false_reg->min_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002013 is_range = false;
Josef Bacik48461132016-09-28 10:54:32 -04002014 break;
2015 case BPF_JGT:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002016 value_from_signed = false;
2017 /* fallthrough */
Josef Bacik48461132016-09-28 10:54:32 -04002018 case BPF_JSGT:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002019 if (true_reg->value_from_signed != value_from_signed)
2020 reset_reg_range_values(true_reg, 0);
2021 if (false_reg->value_from_signed != value_from_signed)
2022 reset_reg_range_values(false_reg, 0);
2023 if (opcode == BPF_JGT) {
2024 /* Unsigned comparison, the minimum value is 0. */
2025 false_reg->min_value = 0;
2026 }
Josef Bacik48461132016-09-28 10:54:32 -04002027 /* If this is false then we know the maximum val is val,
2028 * otherwise we know the min val is val+1.
2029 */
2030 false_reg->max_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002031 false_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002032 true_reg->min_value = val + 1;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002033 true_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002034 break;
2035 case BPF_JGE:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002036 value_from_signed = false;
2037 /* fallthrough */
Josef Bacik48461132016-09-28 10:54:32 -04002038 case BPF_JSGE:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002039 if (true_reg->value_from_signed != value_from_signed)
2040 reset_reg_range_values(true_reg, 0);
2041 if (false_reg->value_from_signed != value_from_signed)
2042 reset_reg_range_values(false_reg, 0);
2043 if (opcode == BPF_JGE) {
2044 /* Unsigned comparison, the minimum value is 0. */
2045 false_reg->min_value = 0;
2046 }
Josef Bacik48461132016-09-28 10:54:32 -04002047 /* If this is false then we know the maximum value is val - 1,
2048 * otherwise we know the mimimum value is val.
2049 */
2050 false_reg->max_value = val - 1;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002051 false_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002052 true_reg->min_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002053 true_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002054 break;
2055 default:
2056 break;
2057 }
2058
2059 check_reg_overflow(false_reg);
2060 check_reg_overflow(true_reg);
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002061 if (is_range) {
2062 if (__is_pointer_value(false, false_reg))
2063 reset_reg_range_values(false_reg, 0);
2064 if (__is_pointer_value(false, true_reg))
2065 reset_reg_range_values(true_reg, 0);
2066 }
Josef Bacik48461132016-09-28 10:54:32 -04002067}
2068
2069/* Same as above, but for the case that dst_reg is a CONST_IMM reg and src_reg
2070 * is the variable reg.
2071 */
2072static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
2073 struct bpf_reg_state *false_reg, u64 val,
2074 u8 opcode)
2075{
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002076 bool value_from_signed = true;
2077 bool is_range = true;
2078
Josef Bacik48461132016-09-28 10:54:32 -04002079 switch (opcode) {
2080 case BPF_JEQ:
2081 /* If this is false then we know nothing Jon Snow, but if it is
2082 * true then we know for sure.
2083 */
2084 true_reg->max_value = true_reg->min_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002085 is_range = false;
Josef Bacik48461132016-09-28 10:54:32 -04002086 break;
2087 case BPF_JNE:
2088 /* If this is true we know nothing Jon Snow, but if it is false
2089 * we know the value for sure;
2090 */
2091 false_reg->max_value = false_reg->min_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002092 is_range = false;
Josef Bacik48461132016-09-28 10:54:32 -04002093 break;
2094 case BPF_JGT:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002095 value_from_signed = false;
2096 /* fallthrough */
Josef Bacik48461132016-09-28 10:54:32 -04002097 case BPF_JSGT:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002098 if (true_reg->value_from_signed != value_from_signed)
2099 reset_reg_range_values(true_reg, 0);
2100 if (false_reg->value_from_signed != value_from_signed)
2101 reset_reg_range_values(false_reg, 0);
2102 if (opcode == BPF_JGT) {
2103 /* Unsigned comparison, the minimum value is 0. */
2104 true_reg->min_value = 0;
2105 }
Josef Bacik48461132016-09-28 10:54:32 -04002106 /*
2107 * If this is false, then the val is <= the register, if it is
2108 * true the register <= to the val.
2109 */
2110 false_reg->min_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002111 false_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002112 true_reg->max_value = val - 1;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002113 true_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002114 break;
2115 case BPF_JGE:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002116 value_from_signed = false;
2117 /* fallthrough */
Josef Bacik48461132016-09-28 10:54:32 -04002118 case BPF_JSGE:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002119 if (true_reg->value_from_signed != value_from_signed)
2120 reset_reg_range_values(true_reg, 0);
2121 if (false_reg->value_from_signed != value_from_signed)
2122 reset_reg_range_values(false_reg, 0);
2123 if (opcode == BPF_JGE) {
2124 /* Unsigned comparison, the minimum value is 0. */
2125 true_reg->min_value = 0;
2126 }
Josef Bacik48461132016-09-28 10:54:32 -04002127 /* If this is false then constant < register, if it is true then
2128 * the register < constant.
2129 */
2130 false_reg->min_value = val + 1;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002131 false_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002132 true_reg->max_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002133 true_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002134 break;
2135 default:
2136 break;
2137 }
2138
2139 check_reg_overflow(false_reg);
2140 check_reg_overflow(true_reg);
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002141 if (is_range) {
2142 if (__is_pointer_value(false, false_reg))
2143 reset_reg_range_values(false_reg, 0);
2144 if (__is_pointer_value(false, true_reg))
2145 reset_reg_range_values(true_reg, 0);
2146 }
Josef Bacik48461132016-09-28 10:54:32 -04002147}
2148
Thomas Graf14117072016-10-18 19:51:19 +02002149static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id,
2150 enum bpf_reg_type type)
2151{
2152 struct bpf_reg_state *reg = &regs[regno];
2153
2154 if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) {
2155 reg->type = type;
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01002156 /* We don't need id from this point onwards anymore, thus we
2157 * should better reset it, so that state pruning has chances
2158 * to take effect.
2159 */
2160 reg->id = 0;
Thomas Graf14117072016-10-18 19:51:19 +02002161 if (type == UNKNOWN_VALUE)
Daniel Borkmann0e0f1d62016-12-18 01:52:59 +01002162 __mark_reg_unknown_value(regs, regno);
Thomas Graf14117072016-10-18 19:51:19 +02002163 }
2164}
2165
2166/* The logic is similar to find_good_pkt_pointers(), both could eventually
2167 * be folded together at some point.
2168 */
2169static void mark_map_regs(struct bpf_verifier_state *state, u32 regno,
2170 enum bpf_reg_type type)
2171{
2172 struct bpf_reg_state *regs = state->regs;
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01002173 u32 id = regs[regno].id;
Thomas Graf14117072016-10-18 19:51:19 +02002174 int i;
2175
2176 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01002177 mark_map_reg(regs, i, id, type);
Thomas Graf14117072016-10-18 19:51:19 +02002178
2179 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
2180 if (state->stack_slot_type[i] != STACK_SPILL)
2181 continue;
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01002182 mark_map_reg(state->spilled_regs, i / BPF_REG_SIZE, id, type);
Thomas Graf14117072016-10-18 19:51:19 +02002183 }
2184}
2185
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002186static int check_cond_jmp_op(struct bpf_verifier_env *env,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002187 struct bpf_insn *insn, int *insn_idx)
2188{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002189 struct bpf_verifier_state *other_branch, *this_branch = &env->cur_state;
2190 struct bpf_reg_state *regs = this_branch->regs, *dst_reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002191 u8 opcode = BPF_OP(insn->code);
2192 int err;
2193
2194 if (opcode > BPF_EXIT) {
2195 verbose("invalid BPF_JMP opcode %x\n", opcode);
2196 return -EINVAL;
2197 }
2198
2199 if (BPF_SRC(insn->code) == BPF_X) {
2200 if (insn->imm != 0) {
2201 verbose("BPF_JMP uses reserved fields\n");
2202 return -EINVAL;
2203 }
2204
2205 /* check src1 operand */
2206 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2207 if (err)
2208 return err;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002209
2210 if (is_pointer_value(env, insn->src_reg)) {
2211 verbose("R%d pointer comparison prohibited\n",
2212 insn->src_reg);
2213 return -EACCES;
2214 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002215 } else {
2216 if (insn->src_reg != BPF_REG_0) {
2217 verbose("BPF_JMP uses reserved fields\n");
2218 return -EINVAL;
2219 }
2220 }
2221
2222 /* check src2 operand */
2223 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
2224 if (err)
2225 return err;
2226
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002227 dst_reg = &regs[insn->dst_reg];
2228
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002229 /* detect if R == 0 where R was initialized to zero earlier */
2230 if (BPF_SRC(insn->code) == BPF_K &&
2231 (opcode == BPF_JEQ || opcode == BPF_JNE) &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002232 dst_reg->type == CONST_IMM && dst_reg->imm == insn->imm) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002233 if (opcode == BPF_JEQ) {
2234 /* if (imm == imm) goto pc+off;
2235 * only follow the goto, ignore fall-through
2236 */
2237 *insn_idx += insn->off;
2238 return 0;
2239 } else {
2240 /* if (imm != imm) goto pc+off;
2241 * only follow fall-through branch, since
2242 * that's where the program will go
2243 */
2244 return 0;
2245 }
2246 }
2247
2248 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx);
2249 if (!other_branch)
2250 return -EFAULT;
2251
Josef Bacik48461132016-09-28 10:54:32 -04002252 /* detect if we are comparing against a constant value so we can adjust
2253 * our min/max values for our dst register.
2254 */
2255 if (BPF_SRC(insn->code) == BPF_X) {
2256 if (regs[insn->src_reg].type == CONST_IMM)
2257 reg_set_min_max(&other_branch->regs[insn->dst_reg],
2258 dst_reg, regs[insn->src_reg].imm,
2259 opcode);
2260 else if (dst_reg->type == CONST_IMM)
2261 reg_set_min_max_inv(&other_branch->regs[insn->src_reg],
2262 &regs[insn->src_reg], dst_reg->imm,
2263 opcode);
2264 } else {
2265 reg_set_min_max(&other_branch->regs[insn->dst_reg],
2266 dst_reg, insn->imm, opcode);
2267 }
2268
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002269 /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002270 if (BPF_SRC(insn->code) == BPF_K &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002271 insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
2272 dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
Thomas Graf14117072016-10-18 19:51:19 +02002273 /* Mark all identical map registers in each branch as either
2274 * safe or unknown depending R == 0 or R != 0 conditional.
2275 */
2276 mark_map_regs(this_branch, insn->dst_reg,
2277 opcode == BPF_JEQ ? PTR_TO_MAP_VALUE : UNKNOWN_VALUE);
2278 mark_map_regs(other_branch, insn->dst_reg,
2279 opcode == BPF_JEQ ? UNKNOWN_VALUE : PTR_TO_MAP_VALUE);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002280 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT &&
2281 dst_reg->type == PTR_TO_PACKET &&
2282 regs[insn->src_reg].type == PTR_TO_PACKET_END) {
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002283 find_good_pkt_pointers(this_branch, dst_reg);
2284 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGE &&
2285 dst_reg->type == PTR_TO_PACKET_END &&
2286 regs[insn->src_reg].type == PTR_TO_PACKET) {
2287 find_good_pkt_pointers(other_branch, &regs[insn->src_reg]);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002288 } else if (is_pointer_value(env, insn->dst_reg)) {
2289 verbose("R%d pointer comparison prohibited\n", insn->dst_reg);
2290 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002291 }
2292 if (log_level)
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002293 print_verifier_state(this_branch);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002294 return 0;
2295}
2296
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002297/* return the map pointer stored inside BPF_LD_IMM64 instruction */
2298static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
2299{
2300 u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
2301
2302 return (struct bpf_map *) (unsigned long) imm64;
2303}
2304
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002305/* verify BPF_LD_IMM64 instruction */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002306static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002307{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002308 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002309 int err;
2310
2311 if (BPF_SIZE(insn->code) != BPF_DW) {
2312 verbose("invalid BPF_LD_IMM insn\n");
2313 return -EINVAL;
2314 }
2315 if (insn->off != 0) {
2316 verbose("BPF_LD_IMM64 uses reserved fields\n");
2317 return -EINVAL;
2318 }
2319
2320 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
2321 if (err)
2322 return err;
2323
Jakub Kicinski6b173872016-09-21 11:43:59 +01002324 if (insn->src_reg == 0) {
2325 /* generic move 64-bit immediate into a register,
2326 * only analyzer needs to collect the ld_imm value.
2327 */
2328 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
2329
2330 if (!env->analyzer_ops)
2331 return 0;
2332
2333 regs[insn->dst_reg].type = CONST_IMM;
2334 regs[insn->dst_reg].imm = imm;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002335 return 0;
Jakub Kicinski6b173872016-09-21 11:43:59 +01002336 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002337
2338 /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
2339 BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
2340
2341 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
2342 regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
2343 return 0;
2344}
2345
Daniel Borkmann96be4322015-03-01 12:31:46 +01002346static bool may_access_skb(enum bpf_prog_type type)
2347{
2348 switch (type) {
2349 case BPF_PROG_TYPE_SOCKET_FILTER:
2350 case BPF_PROG_TYPE_SCHED_CLS:
Daniel Borkmann94caee82015-03-20 15:11:11 +01002351 case BPF_PROG_TYPE_SCHED_ACT:
Daniel Borkmann96be4322015-03-01 12:31:46 +01002352 return true;
2353 default:
2354 return false;
2355 }
2356}
2357
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002358/* verify safety of LD_ABS|LD_IND instructions:
2359 * - they can only appear in the programs where ctx == skb
2360 * - since they are wrappers of function calls, they scratch R1-R5 registers,
2361 * preserve R6-R9, and store return value into R0
2362 *
2363 * Implicit input:
2364 * ctx == skb == R6 == CTX
2365 *
2366 * Explicit input:
2367 * SRC == any register
2368 * IMM == 32-bit immediate
2369 *
2370 * Output:
2371 * R0 - 8/16/32-bit skb data converted to cpu endianness
2372 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002373static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002374{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002375 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002376 u8 mode = BPF_MODE(insn->code);
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002377 struct bpf_reg_state *reg;
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002378 int i, err;
2379
Daniel Borkmann24701ec2015-03-01 12:31:47 +01002380 if (!may_access_skb(env->prog->type)) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002381 verbose("BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002382 return -EINVAL;
2383 }
2384
2385 if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
Alexei Starovoitovd82bccc2016-04-12 10:26:19 -07002386 BPF_SIZE(insn->code) == BPF_DW ||
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002387 (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002388 verbose("BPF_LD_[ABS|IND] uses reserved fields\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002389 return -EINVAL;
2390 }
2391
2392 /* check whether implicit source operand (register R6) is readable */
2393 err = check_reg_arg(regs, BPF_REG_6, SRC_OP);
2394 if (err)
2395 return err;
2396
2397 if (regs[BPF_REG_6].type != PTR_TO_CTX) {
2398 verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
2399 return -EINVAL;
2400 }
2401
2402 if (mode == BPF_IND) {
2403 /* check explicit source operand */
2404 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2405 if (err)
2406 return err;
2407 }
2408
2409 /* reset caller saved regs to unreadable */
2410 for (i = 0; i < CALLER_SAVED_REGS; i++) {
2411 reg = regs + caller_saved[i];
2412 reg->type = NOT_INIT;
2413 reg->imm = 0;
2414 }
2415
2416 /* mark destination R0 register as readable, since it contains
2417 * the value fetched from the packet
2418 */
2419 regs[BPF_REG_0].type = UNKNOWN_VALUE;
2420 return 0;
2421}
2422
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002423/* non-recursive DFS pseudo code
2424 * 1 procedure DFS-iterative(G,v):
2425 * 2 label v as discovered
2426 * 3 let S be a stack
2427 * 4 S.push(v)
2428 * 5 while S is not empty
2429 * 6 t <- S.pop()
2430 * 7 if t is what we're looking for:
2431 * 8 return t
2432 * 9 for all edges e in G.adjacentEdges(t) do
2433 * 10 if edge e is already labelled
2434 * 11 continue with the next edge
2435 * 12 w <- G.adjacentVertex(t,e)
2436 * 13 if vertex w is not discovered and not explored
2437 * 14 label e as tree-edge
2438 * 15 label w as discovered
2439 * 16 S.push(w)
2440 * 17 continue at 5
2441 * 18 else if vertex w is discovered
2442 * 19 label e as back-edge
2443 * 20 else
2444 * 21 // vertex w is explored
2445 * 22 label e as forward- or cross-edge
2446 * 23 label t as explored
2447 * 24 S.pop()
2448 *
2449 * convention:
2450 * 0x10 - discovered
2451 * 0x11 - discovered and fall-through edge labelled
2452 * 0x12 - discovered and fall-through and branch edges labelled
2453 * 0x20 - explored
2454 */
2455
2456enum {
2457 DISCOVERED = 0x10,
2458 EXPLORED = 0x20,
2459 FALLTHROUGH = 1,
2460 BRANCH = 2,
2461};
2462
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002463#define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002464
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002465static int *insn_stack; /* stack of insns to process */
2466static int cur_stack; /* current stack index */
2467static int *insn_state;
2468
2469/* t, w, e - match pseudo-code above:
2470 * t - index of current instruction
2471 * w - next instruction
2472 * e - edge
2473 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002474static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002475{
2476 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
2477 return 0;
2478
2479 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
2480 return 0;
2481
2482 if (w < 0 || w >= env->prog->len) {
2483 verbose("jump out of range from insn %d to %d\n", t, w);
2484 return -EINVAL;
2485 }
2486
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002487 if (e == BRANCH)
2488 /* mark branch target for state pruning */
2489 env->explored_states[w] = STATE_LIST_MARK;
2490
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002491 if (insn_state[w] == 0) {
2492 /* tree-edge */
2493 insn_state[t] = DISCOVERED | e;
2494 insn_state[w] = DISCOVERED;
2495 if (cur_stack >= env->prog->len)
2496 return -E2BIG;
2497 insn_stack[cur_stack++] = w;
2498 return 1;
2499 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
2500 verbose("back-edge from insn %d to %d\n", t, w);
2501 return -EINVAL;
2502 } else if (insn_state[w] == EXPLORED) {
2503 /* forward- or cross-edge */
2504 insn_state[t] = DISCOVERED | e;
2505 } else {
2506 verbose("insn state internal bug\n");
2507 return -EFAULT;
2508 }
2509 return 0;
2510}
2511
2512/* non-recursive depth-first-search to detect loops in BPF program
2513 * loop == back-edge in directed graph
2514 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002515static int check_cfg(struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002516{
2517 struct bpf_insn *insns = env->prog->insnsi;
2518 int insn_cnt = env->prog->len;
2519 int ret = 0;
2520 int i, t;
2521
2522 insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
2523 if (!insn_state)
2524 return -ENOMEM;
2525
2526 insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
2527 if (!insn_stack) {
2528 kfree(insn_state);
2529 return -ENOMEM;
2530 }
2531
2532 insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
2533 insn_stack[0] = 0; /* 0 is the first instruction */
2534 cur_stack = 1;
2535
2536peek_stack:
2537 if (cur_stack == 0)
2538 goto check_state;
2539 t = insn_stack[cur_stack - 1];
2540
2541 if (BPF_CLASS(insns[t].code) == BPF_JMP) {
2542 u8 opcode = BPF_OP(insns[t].code);
2543
2544 if (opcode == BPF_EXIT) {
2545 goto mark_explored;
2546 } else if (opcode == BPF_CALL) {
2547 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2548 if (ret == 1)
2549 goto peek_stack;
2550 else if (ret < 0)
2551 goto err_free;
Daniel Borkmann07016152016-04-05 22:33:17 +02002552 if (t + 1 < insn_cnt)
2553 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002554 } else if (opcode == BPF_JA) {
2555 if (BPF_SRC(insns[t].code) != BPF_K) {
2556 ret = -EINVAL;
2557 goto err_free;
2558 }
2559 /* unconditional jump with single edge */
2560 ret = push_insn(t, t + insns[t].off + 1,
2561 FALLTHROUGH, env);
2562 if (ret == 1)
2563 goto peek_stack;
2564 else if (ret < 0)
2565 goto err_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002566 /* tell verifier to check for equivalent states
2567 * after every call and jump
2568 */
Alexei Starovoitovc3de6312015-04-14 15:57:13 -07002569 if (t + 1 < insn_cnt)
2570 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002571 } else {
2572 /* conditional jump with two edges */
Daniel Borkmann577aa832017-05-18 03:00:06 +02002573 env->explored_states[t] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002574 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2575 if (ret == 1)
2576 goto peek_stack;
2577 else if (ret < 0)
2578 goto err_free;
2579
2580 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
2581 if (ret == 1)
2582 goto peek_stack;
2583 else if (ret < 0)
2584 goto err_free;
2585 }
2586 } else {
2587 /* all other non-branch instructions with single
2588 * fall-through edge
2589 */
2590 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2591 if (ret == 1)
2592 goto peek_stack;
2593 else if (ret < 0)
2594 goto err_free;
2595 }
2596
2597mark_explored:
2598 insn_state[t] = EXPLORED;
2599 if (cur_stack-- <= 0) {
2600 verbose("pop stack internal bug\n");
2601 ret = -EFAULT;
2602 goto err_free;
2603 }
2604 goto peek_stack;
2605
2606check_state:
2607 for (i = 0; i < insn_cnt; i++) {
2608 if (insn_state[i] != EXPLORED) {
2609 verbose("unreachable insn %d\n", i);
2610 ret = -EINVAL;
2611 goto err_free;
2612 }
2613 }
2614 ret = 0; /* cfg looks good */
2615
2616err_free:
2617 kfree(insn_state);
2618 kfree(insn_stack);
2619 return ret;
2620}
2621
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002622/* the following conditions reduce the number of explored insns
2623 * from ~140k to ~80k for ultra large programs that use a lot of ptr_to_packet
2624 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002625static bool compare_ptrs_to_packet(struct bpf_reg_state *old,
2626 struct bpf_reg_state *cur)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002627{
2628 if (old->id != cur->id)
2629 return false;
2630
2631 /* old ptr_to_packet is more conservative, since it allows smaller
2632 * range. Ex:
2633 * old(off=0,r=10) is equal to cur(off=0,r=20), because
2634 * old(off=0,r=10) means that with range=10 the verifier proceeded
2635 * further and found no issues with the program. Now we're in the same
2636 * spot with cur(off=0,r=20), so we're safe too, since anything further
2637 * will only be looking at most 10 bytes after this pointer.
2638 */
2639 if (old->off == cur->off && old->range < cur->range)
2640 return true;
2641
2642 /* old(off=20,r=10) is equal to cur(off=22,re=22 or 5 or 0)
2643 * since both cannot be used for packet access and safe(old)
2644 * pointer has smaller off that could be used for further
2645 * 'if (ptr > data_end)' check
2646 * Ex:
2647 * old(off=20,r=10) and cur(off=22,r=22) and cur(off=22,r=0) mean
2648 * that we cannot access the packet.
2649 * The safe range is:
2650 * [ptr, ptr + range - off)
2651 * so whenever off >=range, it means no safe bytes from this pointer.
2652 * When comparing old->off <= cur->off, it means that older code
2653 * went with smaller offset and that offset was later
2654 * used to figure out the safe range after 'if (ptr > data_end)' check
2655 * Say, 'old' state was explored like:
2656 * ... R3(off=0, r=0)
2657 * R4 = R3 + 20
2658 * ... now R4(off=20,r=0) <-- here
2659 * if (R4 > data_end)
2660 * ... R4(off=20,r=20), R3(off=0,r=20) and R3 can be used to access.
2661 * ... the code further went all the way to bpf_exit.
2662 * Now the 'cur' state at the mark 'here' has R4(off=30,r=0).
2663 * old_R4(off=20,r=0) equal to cur_R4(off=30,r=0), since if the verifier
2664 * goes further, such cur_R4 will give larger safe packet range after
2665 * 'if (R4 > data_end)' and all further insn were already good with r=20,
2666 * so they will be good with r=30 and we can prune the search.
2667 */
2668 if (old->off <= cur->off &&
2669 old->off >= old->range && cur->off >= cur->range)
2670 return true;
2671
2672 return false;
2673}
2674
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002675/* compare two verifier states
2676 *
2677 * all states stored in state_list are known to be valid, since
2678 * verifier reached 'bpf_exit' instruction through them
2679 *
2680 * this function is called when verifier exploring different branches of
2681 * execution popped from the state stack. If it sees an old state that has
2682 * more strict register state and more strict stack state then this execution
2683 * branch doesn't need to be explored further, since verifier already
2684 * concluded that more strict state leads to valid finish.
2685 *
2686 * Therefore two states are equivalent if register state is more conservative
2687 * and explored stack state is more conservative than the current one.
2688 * Example:
2689 * explored current
2690 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
2691 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
2692 *
2693 * In other words if current stack state (one being explored) has more
2694 * valid slots than old one that already passed validation, it means
2695 * the verifier can stop exploring and conclude that current state is valid too
2696 *
2697 * Similarly with registers. If explored state has register type as invalid
2698 * whereas register type in current state is meaningful, it means that
2699 * the current state will reach 'bpf_exit' instruction safely
2700 */
Josef Bacik48461132016-09-28 10:54:32 -04002701static bool states_equal(struct bpf_verifier_env *env,
2702 struct bpf_verifier_state *old,
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002703 struct bpf_verifier_state *cur)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002704{
Josef Bacike2d2afe2016-11-29 12:27:09 -05002705 bool varlen_map_access = env->varlen_map_value_access;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002706 struct bpf_reg_state *rold, *rcur;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002707 int i;
2708
2709 for (i = 0; i < MAX_BPF_REG; i++) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002710 rold = &old->regs[i];
2711 rcur = &cur->regs[i];
2712
2713 if (memcmp(rold, rcur, sizeof(*rold)) == 0)
2714 continue;
2715
Josef Bacik48461132016-09-28 10:54:32 -04002716 /* If the ranges were not the same, but everything else was and
2717 * we didn't do a variable access into a map then we are a-ok.
2718 */
Josef Bacike2d2afe2016-11-29 12:27:09 -05002719 if (!varlen_map_access &&
Alexei Starovoitovb7f5aa12016-12-07 10:57:59 -08002720 memcmp(rold, rcur, offsetofend(struct bpf_reg_state, id)) == 0)
Josef Bacik48461132016-09-28 10:54:32 -04002721 continue;
2722
Josef Bacike2d2afe2016-11-29 12:27:09 -05002723 /* If we didn't map access then again we don't care about the
2724 * mismatched range values and it's ok if our old type was
Ben Hutchings37435f72017-12-23 02:26:17 +00002725 * UNKNOWN and we didn't go to a NOT_INIT'ed or pointer reg.
Josef Bacike2d2afe2016-11-29 12:27:09 -05002726 */
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002727 if (rold->type == NOT_INIT ||
Josef Bacike2d2afe2016-11-29 12:27:09 -05002728 (!varlen_map_access && rold->type == UNKNOWN_VALUE &&
Ben Hutchings37435f72017-12-23 02:26:17 +00002729 rcur->type != NOT_INIT &&
2730 !__is_pointer_value(env->allow_ptr_leaks, rcur)))
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002731 continue;
2732
Daniel Borkmann577aa832017-05-18 03:00:06 +02002733 /* Don't care about the reg->id in this case. */
2734 if (rold->type == PTR_TO_MAP_VALUE_OR_NULL &&
2735 rcur->type == PTR_TO_MAP_VALUE_OR_NULL &&
2736 rold->map_ptr == rcur->map_ptr)
2737 continue;
2738
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002739 if (rold->type == PTR_TO_PACKET && rcur->type == PTR_TO_PACKET &&
2740 compare_ptrs_to_packet(rold, rcur))
2741 continue;
2742
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002743 return false;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002744 }
2745
2746 for (i = 0; i < MAX_BPF_STACK; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002747 if (old->stack_slot_type[i] == STACK_INVALID)
2748 continue;
2749 if (old->stack_slot_type[i] != cur->stack_slot_type[i])
2750 /* Ex: old explored (safe) state has STACK_SPILL in
2751 * this stack slot, but current has has STACK_MISC ->
2752 * this verifier states are not equivalent,
2753 * return false to continue verification of this path
2754 */
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002755 return false;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002756 if (i % BPF_REG_SIZE)
2757 continue;
2758 if (memcmp(&old->spilled_regs[i / BPF_REG_SIZE],
2759 &cur->spilled_regs[i / BPF_REG_SIZE],
2760 sizeof(old->spilled_regs[0])))
2761 /* when explored and current stack slot types are
2762 * the same, check that stored pointers types
2763 * are the same as well.
2764 * Ex: explored safe path could have stored
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002765 * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -8}
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002766 * but current path has stored:
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002767 * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -16}
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002768 * such verifier states are not equivalent.
2769 * return false to continue verification of this path
2770 */
2771 return false;
2772 else
2773 continue;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002774 }
2775 return true;
2776}
2777
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002778static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002779{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002780 struct bpf_verifier_state_list *new_sl;
2781 struct bpf_verifier_state_list *sl;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002782
2783 sl = env->explored_states[insn_idx];
2784 if (!sl)
2785 /* this 'insn_idx' instruction wasn't marked, so we will not
2786 * be doing state search here
2787 */
2788 return 0;
2789
2790 while (sl != STATE_LIST_MARK) {
Josef Bacik48461132016-09-28 10:54:32 -04002791 if (states_equal(env, &sl->state, &env->cur_state))
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002792 /* reached equivalent register/stack state,
2793 * prune the search
2794 */
2795 return 1;
2796 sl = sl->next;
2797 }
2798
2799 /* there were no equivalent states, remember current one.
2800 * technically the current state is not proven to be safe yet,
2801 * but it will either reach bpf_exit (which means it's safe) or
2802 * it will be rejected. Since there are no loops, we won't be
2803 * seeing this 'insn_idx' instruction again on the way to bpf_exit
2804 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002805 new_sl = kmalloc(sizeof(struct bpf_verifier_state_list), GFP_USER);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002806 if (!new_sl)
2807 return -ENOMEM;
2808
2809 /* add new state to the head of linked list */
2810 memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state));
2811 new_sl->next = env->explored_states[insn_idx];
2812 env->explored_states[insn_idx] = new_sl;
2813 return 0;
2814}
2815
Jakub Kicinski13a27df2016-09-21 11:43:58 +01002816static int ext_analyzer_insn_hook(struct bpf_verifier_env *env,
2817 int insn_idx, int prev_insn_idx)
2818{
2819 if (!env->analyzer_ops || !env->analyzer_ops->insn_hook)
2820 return 0;
2821
2822 return env->analyzer_ops->insn_hook(env, insn_idx, prev_insn_idx);
2823}
2824
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002825static int do_check(struct bpf_verifier_env *env)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002826{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002827 struct bpf_verifier_state *state = &env->cur_state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002828 struct bpf_insn *insns = env->prog->insnsi;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002829 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002830 int insn_cnt = env->prog->len;
2831 int insn_idx, prev_insn_idx = 0;
2832 int insn_processed = 0;
2833 bool do_print_state = false;
2834
2835 init_reg_state(regs);
2836 insn_idx = 0;
Josef Bacik48461132016-09-28 10:54:32 -04002837 env->varlen_map_value_access = false;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002838 for (;;) {
2839 struct bpf_insn *insn;
2840 u8 class;
2841 int err;
2842
2843 if (insn_idx >= insn_cnt) {
2844 verbose("invalid insn idx %d insn_cnt %d\n",
2845 insn_idx, insn_cnt);
2846 return -EFAULT;
2847 }
2848
2849 insn = &insns[insn_idx];
2850 class = BPF_CLASS(insn->code);
2851
Daniel Borkmann07016152016-04-05 22:33:17 +02002852 if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002853 verbose("BPF program is too large. Proccessed %d insn\n",
2854 insn_processed);
2855 return -E2BIG;
2856 }
2857
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002858 err = is_state_visited(env, insn_idx);
2859 if (err < 0)
2860 return err;
2861 if (err == 1) {
2862 /* found equivalent state, can prune the search */
2863 if (log_level) {
2864 if (do_print_state)
2865 verbose("\nfrom %d to %d: safe\n",
2866 prev_insn_idx, insn_idx);
2867 else
2868 verbose("%d: safe\n", insn_idx);
2869 }
2870 goto process_bpf_exit;
2871 }
2872
Daniel Borkmann577aa832017-05-18 03:00:06 +02002873 if (need_resched())
2874 cond_resched();
2875
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002876 if (log_level && do_print_state) {
2877 verbose("\nfrom %d to %d:", prev_insn_idx, insn_idx);
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002878 print_verifier_state(&env->cur_state);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002879 do_print_state = false;
2880 }
2881
2882 if (log_level) {
2883 verbose("%d: ", insn_idx);
Daniel Borkmannced0a312017-05-08 00:04:09 +02002884 print_bpf_insn(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002885 }
2886
Jakub Kicinski13a27df2016-09-21 11:43:58 +01002887 err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx);
2888 if (err)
2889 return err;
2890
Daniel Borkmann7b5b73e2017-12-22 16:29:03 +01002891 env->insn_aux_data[insn_idx].seen = true;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002892 if (class == BPF_ALU || class == BPF_ALU64) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002893 err = check_alu_op(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002894 if (err)
2895 return err;
2896
2897 } else if (class == BPF_LDX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002898 enum bpf_reg_type *prev_src_type, src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002899
2900 /* check for reserved fields is already done */
2901
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002902 /* check src operand */
2903 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2904 if (err)
2905 return err;
2906
2907 err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
2908 if (err)
2909 return err;
2910
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07002911 src_reg_type = regs[insn->src_reg].type;
2912
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002913 /* check that memory (src_reg + off) is readable,
2914 * the state of dst_reg will be updated by this func
2915 */
2916 err = check_mem_access(env, insn->src_reg, insn->off,
2917 BPF_SIZE(insn->code), BPF_READ,
2918 insn->dst_reg);
2919 if (err)
2920 return err;
2921
Josef Bacik48461132016-09-28 10:54:32 -04002922 reset_reg_range_values(regs, insn->dst_reg);
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07002923 if (BPF_SIZE(insn->code) != BPF_W &&
2924 BPF_SIZE(insn->code) != BPF_DW) {
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07002925 insn_idx++;
2926 continue;
2927 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002928
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002929 prev_src_type = &env->insn_aux_data[insn_idx].ptr_type;
2930
2931 if (*prev_src_type == NOT_INIT) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002932 /* saw a valid insn
2933 * dst_reg = *(u32 *)(src_reg + off)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002934 * save type to validate intersecting paths
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002935 */
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002936 *prev_src_type = src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002937
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002938 } else if (src_reg_type != *prev_src_type &&
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002939 (src_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002940 *prev_src_type == PTR_TO_CTX)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002941 /* ABuser program is trying to use the same insn
2942 * dst_reg = *(u32*) (src_reg + off)
2943 * with different pointer types:
2944 * src_reg == ctx in one branch and
2945 * src_reg == stack|map in some other branch.
2946 * Reject it.
2947 */
2948 verbose("same insn cannot be used with different pointers\n");
2949 return -EINVAL;
2950 }
2951
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002952 } else if (class == BPF_STX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002953 enum bpf_reg_type *prev_dst_type, dst_reg_type;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002954
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002955 if (BPF_MODE(insn->code) == BPF_XADD) {
2956 err = check_xadd(env, insn);
2957 if (err)
2958 return err;
2959 insn_idx++;
2960 continue;
2961 }
2962
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002963 /* check src1 operand */
2964 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2965 if (err)
2966 return err;
2967 /* check src2 operand */
2968 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
2969 if (err)
2970 return err;
2971
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002972 dst_reg_type = regs[insn->dst_reg].type;
2973
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002974 /* check that memory (dst_reg + off) is writeable */
2975 err = check_mem_access(env, insn->dst_reg, insn->off,
2976 BPF_SIZE(insn->code), BPF_WRITE,
2977 insn->src_reg);
2978 if (err)
2979 return err;
2980
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002981 prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type;
2982
2983 if (*prev_dst_type == NOT_INIT) {
2984 *prev_dst_type = dst_reg_type;
2985 } else if (dst_reg_type != *prev_dst_type &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002986 (dst_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002987 *prev_dst_type == PTR_TO_CTX)) {
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002988 verbose("same insn cannot be used with different pointers\n");
2989 return -EINVAL;
2990 }
2991
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002992 } else if (class == BPF_ST) {
2993 if (BPF_MODE(insn->code) != BPF_MEM ||
2994 insn->src_reg != BPF_REG_0) {
2995 verbose("BPF_ST uses reserved fields\n");
2996 return -EINVAL;
2997 }
2998 /* check src operand */
2999 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
3000 if (err)
3001 return err;
3002
3003 /* check that memory (dst_reg + off) is writeable */
3004 err = check_mem_access(env, insn->dst_reg, insn->off,
3005 BPF_SIZE(insn->code), BPF_WRITE,
3006 -1);
3007 if (err)
3008 return err;
3009
3010 } else if (class == BPF_JMP) {
3011 u8 opcode = BPF_OP(insn->code);
3012
3013 if (opcode == BPF_CALL) {
3014 if (BPF_SRC(insn->code) != BPF_K ||
3015 insn->off != 0 ||
3016 insn->src_reg != BPF_REG_0 ||
3017 insn->dst_reg != BPF_REG_0) {
3018 verbose("BPF_CALL uses reserved fields\n");
3019 return -EINVAL;
3020 }
3021
3022 err = check_call(env, insn->imm);
3023 if (err)
3024 return err;
3025
3026 } else if (opcode == BPF_JA) {
3027 if (BPF_SRC(insn->code) != BPF_K ||
3028 insn->imm != 0 ||
3029 insn->src_reg != BPF_REG_0 ||
3030 insn->dst_reg != BPF_REG_0) {
3031 verbose("BPF_JA uses reserved fields\n");
3032 return -EINVAL;
3033 }
3034
3035 insn_idx += insn->off + 1;
3036 continue;
3037
3038 } else if (opcode == BPF_EXIT) {
3039 if (BPF_SRC(insn->code) != BPF_K ||
3040 insn->imm != 0 ||
3041 insn->src_reg != BPF_REG_0 ||
3042 insn->dst_reg != BPF_REG_0) {
3043 verbose("BPF_EXIT uses reserved fields\n");
3044 return -EINVAL;
3045 }
3046
3047 /* eBPF calling convetion is such that R0 is used
3048 * to return the value from eBPF program.
3049 * Make sure that it's readable at this time
3050 * of bpf_exit, which means that program wrote
3051 * something into it earlier
3052 */
3053 err = check_reg_arg(regs, BPF_REG_0, SRC_OP);
3054 if (err)
3055 return err;
3056
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003057 if (is_pointer_value(env, BPF_REG_0)) {
3058 verbose("R0 leaks addr as return value\n");
3059 return -EACCES;
3060 }
3061
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003062process_bpf_exit:
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003063 insn_idx = pop_stack(env, &prev_insn_idx);
3064 if (insn_idx < 0) {
3065 break;
3066 } else {
3067 do_print_state = true;
3068 continue;
3069 }
3070 } else {
3071 err = check_cond_jmp_op(env, insn, &insn_idx);
3072 if (err)
3073 return err;
3074 }
3075 } else if (class == BPF_LD) {
3076 u8 mode = BPF_MODE(insn->code);
3077
3078 if (mode == BPF_ABS || mode == BPF_IND) {
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003079 err = check_ld_abs(env, insn);
3080 if (err)
3081 return err;
3082
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003083 } else if (mode == BPF_IMM) {
3084 err = check_ld_imm(env, insn);
3085 if (err)
3086 return err;
3087
3088 insn_idx++;
Daniel Borkmann7b5b73e2017-12-22 16:29:03 +01003089 env->insn_aux_data[insn_idx].seen = true;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003090 } else {
3091 verbose("invalid BPF_LD mode\n");
3092 return -EINVAL;
3093 }
Josef Bacik48461132016-09-28 10:54:32 -04003094 reset_reg_range_values(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003095 } else {
3096 verbose("unknown insn class %d\n", class);
3097 return -EINVAL;
3098 }
3099
3100 insn_idx++;
3101 }
3102
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003103 verbose("processed %d insns\n", insn_processed);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003104 return 0;
3105}
3106
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07003107static int check_map_prog_compatibility(struct bpf_map *map,
3108 struct bpf_prog *prog)
3109
3110{
3111 if (prog->type == BPF_PROG_TYPE_PERF_EVENT &&
3112 (map->map_type == BPF_MAP_TYPE_HASH ||
3113 map->map_type == BPF_MAP_TYPE_PERCPU_HASH) &&
3114 (map->map_flags & BPF_F_NO_PREALLOC)) {
3115 verbose("perf_event programs can only use preallocated hash map\n");
3116 return -EINVAL;
3117 }
3118 return 0;
3119}
3120
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003121/* look for pseudo eBPF instructions that access map FDs and
3122 * replace them with actual map pointers
3123 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003124static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003125{
3126 struct bpf_insn *insn = env->prog->insnsi;
3127 int insn_cnt = env->prog->len;
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07003128 int i, j, err;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003129
3130 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003131 if (BPF_CLASS(insn->code) == BPF_LDX &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003132 (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003133 verbose("BPF_LDX uses reserved fields\n");
3134 return -EINVAL;
3135 }
3136
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003137 if (BPF_CLASS(insn->code) == BPF_STX &&
3138 ((BPF_MODE(insn->code) != BPF_MEM &&
3139 BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
3140 verbose("BPF_STX uses reserved fields\n");
3141 return -EINVAL;
3142 }
3143
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003144 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
3145 struct bpf_map *map;
3146 struct fd f;
3147
3148 if (i == insn_cnt - 1 || insn[1].code != 0 ||
3149 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
3150 insn[1].off != 0) {
3151 verbose("invalid bpf_ld_imm64 insn\n");
3152 return -EINVAL;
3153 }
3154
3155 if (insn->src_reg == 0)
3156 /* valid generic load 64-bit imm */
3157 goto next_insn;
3158
3159 if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
3160 verbose("unrecognized bpf_ld_imm64 insn\n");
3161 return -EINVAL;
3162 }
3163
3164 f = fdget(insn->imm);
Daniel Borkmannc2101292015-10-29 14:58:07 +01003165 map = __bpf_map_get(f);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003166 if (IS_ERR(map)) {
3167 verbose("fd %d is not pointing to valid bpf_map\n",
3168 insn->imm);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003169 return PTR_ERR(map);
3170 }
3171
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07003172 err = check_map_prog_compatibility(map, env->prog);
3173 if (err) {
3174 fdput(f);
3175 return err;
3176 }
3177
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003178 /* store map pointer inside BPF_LD_IMM64 instruction */
3179 insn[0].imm = (u32) (unsigned long) map;
3180 insn[1].imm = ((u64) (unsigned long) map) >> 32;
3181
3182 /* check whether we recorded this map already */
3183 for (j = 0; j < env->used_map_cnt; j++)
3184 if (env->used_maps[j] == map) {
3185 fdput(f);
3186 goto next_insn;
3187 }
3188
3189 if (env->used_map_cnt >= MAX_USED_MAPS) {
3190 fdput(f);
3191 return -E2BIG;
3192 }
3193
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003194 /* hold the map. If the program is rejected by verifier,
3195 * the map will be released by release_maps() or it
3196 * will be used by the valid program until it's unloaded
3197 * and all maps are released in free_bpf_prog_info()
3198 */
Alexei Starovoitov92117d82016-04-27 18:56:20 -07003199 map = bpf_map_inc(map, false);
3200 if (IS_ERR(map)) {
3201 fdput(f);
3202 return PTR_ERR(map);
3203 }
3204 env->used_maps[env->used_map_cnt++] = map;
3205
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003206 fdput(f);
3207next_insn:
3208 insn++;
3209 i++;
3210 }
3211 }
3212
3213 /* now all pseudo BPF_LD_IMM64 instructions load valid
3214 * 'struct bpf_map *' into a register instead of user map_fd.
3215 * These pointers will be used later by verifier to validate map access.
3216 */
3217 return 0;
3218}
3219
3220/* drop refcnt of maps used by the rejected program */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003221static void release_maps(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003222{
3223 int i;
3224
3225 for (i = 0; i < env->used_map_cnt; i++)
3226 bpf_map_put(env->used_maps[i]);
3227}
3228
3229/* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003230static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003231{
3232 struct bpf_insn *insn = env->prog->insnsi;
3233 int insn_cnt = env->prog->len;
3234 int i;
3235
3236 for (i = 0; i < insn_cnt; i++, insn++)
3237 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
3238 insn->src_reg = 0;
3239}
3240
Daniel Borkmann565f0122017-12-22 16:29:02 +01003241/* single env->prog->insni[off] instruction was replaced with the range
3242 * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
3243 * [0, off) and [off, end) to new locations, so the patched range stays zero
3244 */
3245static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len,
3246 u32 off, u32 cnt)
3247{
3248 struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data;
Daniel Borkmann7b5b73e2017-12-22 16:29:03 +01003249 int i;
Daniel Borkmann565f0122017-12-22 16:29:02 +01003250
3251 if (cnt == 1)
3252 return 0;
3253 new_data = vzalloc(sizeof(struct bpf_insn_aux_data) * prog_len);
3254 if (!new_data)
3255 return -ENOMEM;
3256 memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off);
3257 memcpy(new_data + off + cnt - 1, old_data + off,
3258 sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
Daniel Borkmann7b5b73e2017-12-22 16:29:03 +01003259 for (i = off; i < off + cnt - 1; i++)
3260 new_data[i].seen = true;
Daniel Borkmann565f0122017-12-22 16:29:02 +01003261 env->insn_aux_data = new_data;
3262 vfree(old_data);
3263 return 0;
3264}
3265
3266static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
3267 const struct bpf_insn *patch, u32 len)
3268{
3269 struct bpf_prog *new_prog;
3270
3271 new_prog = bpf_patch_insn_single(env->prog, off, patch, len);
3272 if (!new_prog)
3273 return NULL;
3274 if (adjust_insn_aux_data(env, new_prog->len, off, len))
3275 return NULL;
3276 return new_prog;
3277}
3278
Daniel Borkmann7b5b73e2017-12-22 16:29:03 +01003279/* The verifier does more data flow analysis than llvm and will not explore
3280 * branches that are dead at run time. Malicious programs can have dead code
3281 * too. Therefore replace all dead at-run-time code with nops.
3282 */
3283static void sanitize_dead_code(struct bpf_verifier_env *env)
3284{
3285 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
3286 struct bpf_insn nop = BPF_MOV64_REG(BPF_REG_0, BPF_REG_0);
3287 struct bpf_insn *insn = env->prog->insnsi;
3288 const int insn_cnt = env->prog->len;
3289 int i;
3290
3291 for (i = 0; i < insn_cnt; i++) {
3292 if (aux_data[i].seen)
3293 continue;
3294 memcpy(insn + i, &nop, sizeof(nop));
3295 }
3296}
3297
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003298/* convert load instructions that access fields of 'struct __sk_buff'
3299 * into sequence of instructions that access fields of 'struct sk_buff'
3300 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003301static int convert_ctx_accesses(struct bpf_verifier_env *env)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003302{
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003303 const struct bpf_verifier_ops *ops = env->prog->aux->ops;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003304 const int insn_cnt = env->prog->len;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003305 struct bpf_insn insn_buf[16], *insn;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003306 struct bpf_prog *new_prog;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003307 enum bpf_access_type type;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003308 int i, cnt, delta = 0;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003309
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003310 if (ops->gen_prologue) {
3311 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
3312 env->prog);
3313 if (cnt >= ARRAY_SIZE(insn_buf)) {
3314 verbose("bpf verifier is misconfigured\n");
3315 return -EINVAL;
3316 } else if (cnt) {
Daniel Borkmann565f0122017-12-22 16:29:02 +01003317 new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003318 if (!new_prog)
3319 return -ENOMEM;
Daniel Borkmann565f0122017-12-22 16:29:02 +01003320
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003321 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003322 delta += cnt - 1;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003323 }
3324 }
3325
3326 if (!ops->convert_ctx_access)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003327 return 0;
3328
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003329 insn = env->prog->insnsi + delta;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003330
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003331 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07003332 if (insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
3333 insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003334 type = BPF_READ;
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07003335 else if (insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
3336 insn->code == (BPF_STX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003337 type = BPF_WRITE;
3338 else
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003339 continue;
3340
Daniel Borkmann565f0122017-12-22 16:29:02 +01003341 if (env->insn_aux_data[i + delta].ptr_type != PTR_TO_CTX)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003342 continue;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003343
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003344 cnt = ops->convert_ctx_access(type, insn->dst_reg, insn->src_reg,
3345 insn->off, insn_buf, env->prog);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003346 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
3347 verbose("bpf verifier is misconfigured\n");
3348 return -EINVAL;
3349 }
3350
Daniel Borkmann565f0122017-12-22 16:29:02 +01003351 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003352 if (!new_prog)
3353 return -ENOMEM;
3354
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003355 delta += cnt - 1;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003356
3357 /* keep walking new program and skip insns we just inserted */
3358 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003359 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003360 }
3361
3362 return 0;
3363}
3364
Alexei Starovoitovf55093d2017-03-15 18:26:40 -07003365/* fixup insn->imm field of bpf_call instructions
Alexei Starovoitov28035362017-03-15 18:26:39 -07003366 *
3367 * this function is called after eBPF program passed verification
3368 */
Alexei Starovoitovf55093d2017-03-15 18:26:40 -07003369static int fixup_bpf_calls(struct bpf_verifier_env *env)
Alexei Starovoitov28035362017-03-15 18:26:39 -07003370{
Alexei Starovoitovf55093d2017-03-15 18:26:40 -07003371 struct bpf_prog *prog = env->prog;
3372 struct bpf_insn *insn = prog->insnsi;
Alexei Starovoitov28035362017-03-15 18:26:39 -07003373 const struct bpf_func_proto *fn;
Alexei Starovoitovf55093d2017-03-15 18:26:40 -07003374 const int insn_cnt = prog->len;
Alexei Starovoitov28035362017-03-15 18:26:39 -07003375 int i;
3376
Alexei Starovoitovf55093d2017-03-15 18:26:40 -07003377 for (i = 0; i < insn_cnt; i++, insn++) {
3378 if (insn->code != (BPF_JMP | BPF_CALL))
3379 continue;
Alexei Starovoitov28035362017-03-15 18:26:39 -07003380
Alexei Starovoitovf55093d2017-03-15 18:26:40 -07003381 if (insn->imm == BPF_FUNC_get_route_realm)
3382 prog->dst_needed = 1;
3383 if (insn->imm == BPF_FUNC_get_prandom_u32)
3384 bpf_user_rnd_init_once();
3385 if (insn->imm == BPF_FUNC_tail_call) {
3386 /* mark bpf_tail_call as different opcode to avoid
3387 * conditional branch in the interpeter for every normal
3388 * call and to prevent accidental JITing by JIT compiler
3389 * that doesn't support bpf_tail_call yet
3390 */
3391 insn->imm = 0;
3392 insn->code |= BPF_X;
3393 continue;
Alexei Starovoitov28035362017-03-15 18:26:39 -07003394 }
Alexei Starovoitovf55093d2017-03-15 18:26:40 -07003395
3396 fn = prog->aux->ops->get_func_proto(insn->imm);
3397 /* all functions that have prototype and verifier allowed
3398 * programs to call them, must be real in-kernel functions
3399 */
3400 if (!fn->func) {
3401 verbose("kernel subsystem misconfigured func %d\n",
3402 insn->imm);
3403 return -EFAULT;
3404 }
3405 insn->imm = fn->func - __bpf_call_base;
Alexei Starovoitov28035362017-03-15 18:26:39 -07003406 }
Alexei Starovoitovf55093d2017-03-15 18:26:40 -07003407
3408 return 0;
Alexei Starovoitov28035362017-03-15 18:26:39 -07003409}
3410
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003411static void free_states(struct bpf_verifier_env *env)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003412{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003413 struct bpf_verifier_state_list *sl, *sln;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003414 int i;
3415
3416 if (!env->explored_states)
3417 return;
3418
3419 for (i = 0; i < env->prog->len; i++) {
3420 sl = env->explored_states[i];
3421
3422 if (sl)
3423 while (sl != STATE_LIST_MARK) {
3424 sln = sl->next;
3425 kfree(sl);
3426 sl = sln;
3427 }
3428 }
3429
3430 kfree(env->explored_states);
3431}
3432
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003433int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003434{
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003435 char __user *log_ubuf = NULL;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003436 struct bpf_verifier_env *env;
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003437 int ret = -EINVAL;
3438
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003439 if ((*prog)->len <= 0 || (*prog)->len > BPF_MAXINSNS)
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003440 return -E2BIG;
3441
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003442 /* 'struct bpf_verifier_env' can be global, but since it's not small,
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003443 * allocate/free it every time bpf_check() is called
3444 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003445 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003446 if (!env)
3447 return -ENOMEM;
3448
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003449 env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
3450 (*prog)->len);
3451 ret = -ENOMEM;
3452 if (!env->insn_aux_data)
3453 goto err_free_env;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003454 env->prog = *prog;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003455
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003456 /* grab the mutex to protect few globals used by verifier */
3457 mutex_lock(&bpf_verifier_lock);
3458
3459 if (attr->log_level || attr->log_buf || attr->log_size) {
3460 /* user requested verbose verifier output
3461 * and supplied buffer to store the verification trace
3462 */
3463 log_level = attr->log_level;
3464 log_ubuf = (char __user *) (unsigned long) attr->log_buf;
3465 log_size = attr->log_size;
3466 log_len = 0;
3467
3468 ret = -EINVAL;
3469 /* log_* values have to be sane */
3470 if (log_size < 128 || log_size > UINT_MAX >> 8 ||
3471 log_level == 0 || log_ubuf == NULL)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003472 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003473
3474 ret = -ENOMEM;
3475 log_buf = vmalloc(log_size);
3476 if (!log_buf)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003477 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003478 } else {
3479 log_level = 0;
3480 }
3481
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003482 ret = replace_map_fd_with_map_ptr(env);
3483 if (ret < 0)
3484 goto skip_full_check;
3485
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003486 env->explored_states = kcalloc(env->prog->len,
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003487 sizeof(struct bpf_verifier_state_list *),
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003488 GFP_USER);
3489 ret = -ENOMEM;
3490 if (!env->explored_states)
3491 goto skip_full_check;
3492
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003493 ret = check_cfg(env);
3494 if (ret < 0)
3495 goto skip_full_check;
3496
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003497 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
3498
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003499 ret = do_check(env);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003500
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003501skip_full_check:
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003502 while (pop_stack(env, NULL) >= 0);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003503 free_states(env);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003504
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003505 if (ret == 0)
Daniel Borkmann7b5b73e2017-12-22 16:29:03 +01003506 sanitize_dead_code(env);
3507
3508 if (ret == 0)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003509 /* program is valid, convert *(u32*)(ctx + off) accesses */
3510 ret = convert_ctx_accesses(env);
3511
Alexei Starovoitov28035362017-03-15 18:26:39 -07003512 if (ret == 0)
Alexei Starovoitovf55093d2017-03-15 18:26:40 -07003513 ret = fixup_bpf_calls(env);
Alexei Starovoitov28035362017-03-15 18:26:39 -07003514
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003515 if (log_level && log_len >= log_size - 1) {
3516 BUG_ON(log_len >= log_size);
3517 /* verifier log exceeded user supplied buffer */
3518 ret = -ENOSPC;
3519 /* fall through to return what was recorded */
3520 }
3521
3522 /* copy verifier log back to user space including trailing zero */
3523 if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) {
3524 ret = -EFAULT;
3525 goto free_log_buf;
3526 }
3527
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003528 if (ret == 0 && env->used_map_cnt) {
3529 /* if program passed verifier, update used_maps in bpf_prog_info */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003530 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
3531 sizeof(env->used_maps[0]),
3532 GFP_KERNEL);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003533
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003534 if (!env->prog->aux->used_maps) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003535 ret = -ENOMEM;
3536 goto free_log_buf;
3537 }
3538
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003539 memcpy(env->prog->aux->used_maps, env->used_maps,
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003540 sizeof(env->used_maps[0]) * env->used_map_cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003541 env->prog->aux->used_map_cnt = env->used_map_cnt;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003542
3543 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
3544 * bpf_ld_imm64 instructions
3545 */
3546 convert_pseudo_ld_imm64(env);
3547 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003548
3549free_log_buf:
3550 if (log_level)
3551 vfree(log_buf);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003552 if (!env->prog->aux->used_maps)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003553 /* if we didn't copy map pointers into bpf_prog_info, release
3554 * them now. Otherwise free_bpf_prog_info() will release them.
3555 */
3556 release_maps(env);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003557 *prog = env->prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003558err_unlock:
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003559 mutex_unlock(&bpf_verifier_lock);
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003560 vfree(env->insn_aux_data);
3561err_free_env:
3562 kfree(env);
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003563 return ret;
3564}
Jakub Kicinski13a27df2016-09-21 11:43:58 +01003565
3566int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops,
3567 void *priv)
3568{
3569 struct bpf_verifier_env *env;
3570 int ret;
3571
3572 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
3573 if (!env)
3574 return -ENOMEM;
3575
3576 env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
3577 prog->len);
3578 ret = -ENOMEM;
3579 if (!env->insn_aux_data)
3580 goto err_free_env;
3581 env->prog = prog;
3582 env->analyzer_ops = ops;
3583 env->analyzer_priv = priv;
3584
3585 /* grab the mutex to protect few globals used by verifier */
3586 mutex_lock(&bpf_verifier_lock);
3587
3588 log_level = 0;
3589
3590 env->explored_states = kcalloc(env->prog->len,
3591 sizeof(struct bpf_verifier_state_list *),
3592 GFP_KERNEL);
3593 ret = -ENOMEM;
3594 if (!env->explored_states)
3595 goto skip_full_check;
3596
3597 ret = check_cfg(env);
3598 if (ret < 0)
3599 goto skip_full_check;
3600
3601 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
3602
3603 ret = do_check(env);
3604
3605skip_full_check:
3606 while (pop_stack(env, NULL) >= 0);
3607 free_states(env);
3608
3609 mutex_unlock(&bpf_verifier_lock);
3610 vfree(env->insn_aux_data);
3611err_free_env:
3612 kfree(env);
3613 return ret;
3614}
3615EXPORT_SYMBOL_GPL(bpf_analyzer);