blob: 372454aa7f37222e0f78b99f2c75e40a695eb80f [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 */
1793 regs[insn->dst_reg].type = CONST_IMM;
1794 regs[insn->dst_reg].imm = insn->imm;
Josef Bacik48461132016-09-28 10:54:32 -04001795 regs[insn->dst_reg].max_value = insn->imm;
1796 regs[insn->dst_reg].min_value = insn->imm;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001797 }
1798
1799 } else if (opcode > BPF_END) {
1800 verbose("invalid BPF_ALU opcode %x\n", opcode);
1801 return -EINVAL;
1802
1803 } else { /* all other ALU ops: and, sub, xor, add, ... */
1804
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001805 if (BPF_SRC(insn->code) == BPF_X) {
1806 if (insn->imm != 0 || insn->off != 0) {
1807 verbose("BPF_ALU uses reserved fields\n");
1808 return -EINVAL;
1809 }
1810 /* check src1 operand */
1811 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1812 if (err)
1813 return err;
1814 } else {
1815 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
1816 verbose("BPF_ALU uses reserved fields\n");
1817 return -EINVAL;
1818 }
1819 }
1820
1821 /* check src2 operand */
1822 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
1823 if (err)
1824 return err;
1825
1826 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
1827 BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
1828 verbose("div by zero\n");
1829 return -EINVAL;
1830 }
1831
Rabin Vincent229394e2016-01-12 20:17:08 +01001832 if ((opcode == BPF_LSH || opcode == BPF_RSH ||
1833 opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
1834 int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
1835
1836 if (insn->imm < 0 || insn->imm >= size) {
1837 verbose("invalid shift %d\n", insn->imm);
1838 return -EINVAL;
1839 }
1840 }
1841
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07001842 /* check dest operand */
1843 err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
1844 if (err)
1845 return err;
1846
1847 dst_reg = &regs[insn->dst_reg];
1848
Josef Bacik48461132016-09-28 10:54:32 -04001849 /* first we want to adjust our ranges. */
1850 adjust_reg_min_max_vals(env, insn);
1851
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001852 /* pattern match 'bpf_add Rx, imm' instruction */
1853 if (opcode == BPF_ADD && BPF_CLASS(insn->code) == BPF_ALU64 &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07001854 dst_reg->type == FRAME_PTR && BPF_SRC(insn->code) == BPF_K) {
1855 dst_reg->type = PTR_TO_STACK;
1856 dst_reg->imm = insn->imm;
1857 return 0;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001858 } else if (opcode == BPF_ADD &&
1859 BPF_CLASS(insn->code) == BPF_ALU64 &&
Yonghong Song7bca0a92017-04-29 22:52:42 -07001860 dst_reg->type == PTR_TO_STACK &&
1861 ((BPF_SRC(insn->code) == BPF_X &&
1862 regs[insn->src_reg].type == CONST_IMM) ||
1863 BPF_SRC(insn->code) == BPF_K)) {
1864 if (BPF_SRC(insn->code) == BPF_X)
1865 dst_reg->imm += regs[insn->src_reg].imm;
1866 else
1867 dst_reg->imm += insn->imm;
1868 return 0;
1869 } else if (opcode == BPF_ADD &&
1870 BPF_CLASS(insn->code) == BPF_ALU64 &&
Alexei Starovoitov1b9b69e2016-05-19 18:17:14 -07001871 (dst_reg->type == PTR_TO_PACKET ||
1872 (BPF_SRC(insn->code) == BPF_X &&
1873 regs[insn->src_reg].type == PTR_TO_PACKET))) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001874 /* ptr_to_packet += K|X */
1875 return check_packet_ptr_add(env, insn);
1876 } else if (BPF_CLASS(insn->code) == BPF_ALU64 &&
1877 dst_reg->type == UNKNOWN_VALUE &&
1878 env->allow_ptr_leaks) {
1879 /* unknown += K|X */
1880 return evaluate_reg_alu(env, insn);
1881 } else if (BPF_CLASS(insn->code) == BPF_ALU64 &&
1882 dst_reg->type == CONST_IMM &&
1883 env->allow_ptr_leaks) {
1884 /* reg_imm += K|X */
1885 return evaluate_reg_imm_alu(env, insn);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001886 } else if (is_pointer_value(env, insn->dst_reg)) {
1887 verbose("R%d pointer arithmetic prohibited\n",
1888 insn->dst_reg);
1889 return -EACCES;
1890 } else if (BPF_SRC(insn->code) == BPF_X &&
1891 is_pointer_value(env, insn->src_reg)) {
1892 verbose("R%d pointer arithmetic prohibited\n",
1893 insn->src_reg);
1894 return -EACCES;
1895 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001896
Josef Bacik48461132016-09-28 10:54:32 -04001897 /* If we did pointer math on a map value then just set it to our
1898 * PTR_TO_MAP_VALUE_ADJ type so we can deal with any stores or
1899 * loads to this register appropriately, otherwise just mark the
1900 * register as unknown.
1901 */
1902 if (env->allow_ptr_leaks &&
Daniel Borkmann8d674be2017-03-31 02:24:02 +02001903 BPF_CLASS(insn->code) == BPF_ALU64 && opcode == BPF_ADD &&
Josef Bacik48461132016-09-28 10:54:32 -04001904 (dst_reg->type == PTR_TO_MAP_VALUE ||
1905 dst_reg->type == PTR_TO_MAP_VALUE_ADJ))
1906 dst_reg->type = PTR_TO_MAP_VALUE_ADJ;
1907 else
1908 mark_reg_unknown_value(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001909 }
1910
1911 return 0;
1912}
1913
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001914static void find_good_pkt_pointers(struct bpf_verifier_state *state,
1915 struct bpf_reg_state *dst_reg)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001916{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001917 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001918 int i;
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02001919
1920 /* LLVM can generate two kind of checks:
1921 *
1922 * Type 1:
1923 *
1924 * r2 = r3;
1925 * r2 += 8;
1926 * if (r2 > pkt_end) goto <handle exception>
1927 * <access okay>
1928 *
1929 * Where:
1930 * r2 == dst_reg, pkt_end == src_reg
1931 * r2=pkt(id=n,off=8,r=0)
1932 * r3=pkt(id=n,off=0,r=0)
1933 *
1934 * Type 2:
1935 *
1936 * r2 = r3;
1937 * r2 += 8;
1938 * if (pkt_end >= r2) goto <access okay>
1939 * <handle exception>
1940 *
1941 * Where:
1942 * pkt_end == dst_reg, r2 == src_reg
1943 * r2=pkt(id=n,off=8,r=0)
1944 * r3=pkt(id=n,off=0,r=0)
1945 *
1946 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
1947 * so that range of bytes [r3, r3 + 8) is safe to access.
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001948 */
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02001949
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001950 for (i = 0; i < MAX_BPF_REG; i++)
1951 if (regs[i].type == PTR_TO_PACKET && regs[i].id == dst_reg->id)
Alexei Starovoitov0ea3c232017-03-24 15:57:33 -07001952 /* keep the maximum range already checked */
1953 regs[i].range = max(regs[i].range, dst_reg->off);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001954
1955 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
1956 if (state->stack_slot_type[i] != STACK_SPILL)
1957 continue;
1958 reg = &state->spilled_regs[i / BPF_REG_SIZE];
1959 if (reg->type == PTR_TO_PACKET && reg->id == dst_reg->id)
Alexei Starovoitov0ea3c232017-03-24 15:57:33 -07001960 reg->range = max(reg->range, dst_reg->off);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001961 }
1962}
1963
Josef Bacik48461132016-09-28 10:54:32 -04001964/* Adjusts the register min/max values in the case that the dst_reg is the
1965 * variable register that we are working on, and src_reg is a constant or we're
1966 * simply doing a BPF_K check.
1967 */
1968static void reg_set_min_max(struct bpf_reg_state *true_reg,
1969 struct bpf_reg_state *false_reg, u64 val,
1970 u8 opcode)
1971{
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02001972 bool value_from_signed = true;
1973 bool is_range = true;
1974
Josef Bacik48461132016-09-28 10:54:32 -04001975 switch (opcode) {
1976 case BPF_JEQ:
1977 /* If this is false then we know nothing Jon Snow, but if it is
1978 * true then we know for sure.
1979 */
1980 true_reg->max_value = true_reg->min_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02001981 is_range = false;
Josef Bacik48461132016-09-28 10:54:32 -04001982 break;
1983 case BPF_JNE:
1984 /* If this is true we know nothing Jon Snow, but if it is false
1985 * we know the value for sure;
1986 */
1987 false_reg->max_value = false_reg->min_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02001988 is_range = false;
Josef Bacik48461132016-09-28 10:54:32 -04001989 break;
1990 case BPF_JGT:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02001991 value_from_signed = false;
1992 /* fallthrough */
Josef Bacik48461132016-09-28 10:54:32 -04001993 case BPF_JSGT:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02001994 if (true_reg->value_from_signed != value_from_signed)
1995 reset_reg_range_values(true_reg, 0);
1996 if (false_reg->value_from_signed != value_from_signed)
1997 reset_reg_range_values(false_reg, 0);
1998 if (opcode == BPF_JGT) {
1999 /* Unsigned comparison, the minimum value is 0. */
2000 false_reg->min_value = 0;
2001 }
Josef Bacik48461132016-09-28 10:54:32 -04002002 /* If this is false then we know the maximum val is val,
2003 * otherwise we know the min val is val+1.
2004 */
2005 false_reg->max_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002006 false_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002007 true_reg->min_value = val + 1;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002008 true_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002009 break;
2010 case BPF_JGE:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002011 value_from_signed = false;
2012 /* fallthrough */
Josef Bacik48461132016-09-28 10:54:32 -04002013 case BPF_JSGE:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002014 if (true_reg->value_from_signed != value_from_signed)
2015 reset_reg_range_values(true_reg, 0);
2016 if (false_reg->value_from_signed != value_from_signed)
2017 reset_reg_range_values(false_reg, 0);
2018 if (opcode == BPF_JGE) {
2019 /* Unsigned comparison, the minimum value is 0. */
2020 false_reg->min_value = 0;
2021 }
Josef Bacik48461132016-09-28 10:54:32 -04002022 /* If this is false then we know the maximum value is val - 1,
2023 * otherwise we know the mimimum value is val.
2024 */
2025 false_reg->max_value = val - 1;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002026 false_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002027 true_reg->min_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002028 true_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002029 break;
2030 default:
2031 break;
2032 }
2033
2034 check_reg_overflow(false_reg);
2035 check_reg_overflow(true_reg);
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002036 if (is_range) {
2037 if (__is_pointer_value(false, false_reg))
2038 reset_reg_range_values(false_reg, 0);
2039 if (__is_pointer_value(false, true_reg))
2040 reset_reg_range_values(true_reg, 0);
2041 }
Josef Bacik48461132016-09-28 10:54:32 -04002042}
2043
2044/* Same as above, but for the case that dst_reg is a CONST_IMM reg and src_reg
2045 * is the variable reg.
2046 */
2047static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
2048 struct bpf_reg_state *false_reg, u64 val,
2049 u8 opcode)
2050{
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002051 bool value_from_signed = true;
2052 bool is_range = true;
2053
Josef Bacik48461132016-09-28 10:54:32 -04002054 switch (opcode) {
2055 case BPF_JEQ:
2056 /* If this is false then we know nothing Jon Snow, but if it is
2057 * true then we know for sure.
2058 */
2059 true_reg->max_value = true_reg->min_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002060 is_range = false;
Josef Bacik48461132016-09-28 10:54:32 -04002061 break;
2062 case BPF_JNE:
2063 /* If this is true we know nothing Jon Snow, but if it is false
2064 * we know the value for sure;
2065 */
2066 false_reg->max_value = false_reg->min_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002067 is_range = false;
Josef Bacik48461132016-09-28 10:54:32 -04002068 break;
2069 case BPF_JGT:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002070 value_from_signed = false;
2071 /* fallthrough */
Josef Bacik48461132016-09-28 10:54:32 -04002072 case BPF_JSGT:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002073 if (true_reg->value_from_signed != value_from_signed)
2074 reset_reg_range_values(true_reg, 0);
2075 if (false_reg->value_from_signed != value_from_signed)
2076 reset_reg_range_values(false_reg, 0);
2077 if (opcode == BPF_JGT) {
2078 /* Unsigned comparison, the minimum value is 0. */
2079 true_reg->min_value = 0;
2080 }
Josef Bacik48461132016-09-28 10:54:32 -04002081 /*
2082 * If this is false, then the val is <= the register, if it is
2083 * true the register <= to the val.
2084 */
2085 false_reg->min_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002086 false_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002087 true_reg->max_value = val - 1;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002088 true_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002089 break;
2090 case BPF_JGE:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002091 value_from_signed = false;
2092 /* fallthrough */
Josef Bacik48461132016-09-28 10:54:32 -04002093 case BPF_JSGE:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002094 if (true_reg->value_from_signed != value_from_signed)
2095 reset_reg_range_values(true_reg, 0);
2096 if (false_reg->value_from_signed != value_from_signed)
2097 reset_reg_range_values(false_reg, 0);
2098 if (opcode == BPF_JGE) {
2099 /* Unsigned comparison, the minimum value is 0. */
2100 true_reg->min_value = 0;
2101 }
Josef Bacik48461132016-09-28 10:54:32 -04002102 /* If this is false then constant < register, if it is true then
2103 * the register < constant.
2104 */
2105 false_reg->min_value = val + 1;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002106 false_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002107 true_reg->max_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002108 true_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002109 break;
2110 default:
2111 break;
2112 }
2113
2114 check_reg_overflow(false_reg);
2115 check_reg_overflow(true_reg);
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002116 if (is_range) {
2117 if (__is_pointer_value(false, false_reg))
2118 reset_reg_range_values(false_reg, 0);
2119 if (__is_pointer_value(false, true_reg))
2120 reset_reg_range_values(true_reg, 0);
2121 }
Josef Bacik48461132016-09-28 10:54:32 -04002122}
2123
Thomas Graf14117072016-10-18 19:51:19 +02002124static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id,
2125 enum bpf_reg_type type)
2126{
2127 struct bpf_reg_state *reg = &regs[regno];
2128
2129 if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) {
2130 reg->type = type;
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01002131 /* We don't need id from this point onwards anymore, thus we
2132 * should better reset it, so that state pruning has chances
2133 * to take effect.
2134 */
2135 reg->id = 0;
Thomas Graf14117072016-10-18 19:51:19 +02002136 if (type == UNKNOWN_VALUE)
Daniel Borkmann0e0f1d62016-12-18 01:52:59 +01002137 __mark_reg_unknown_value(regs, regno);
Thomas Graf14117072016-10-18 19:51:19 +02002138 }
2139}
2140
2141/* The logic is similar to find_good_pkt_pointers(), both could eventually
2142 * be folded together at some point.
2143 */
2144static void mark_map_regs(struct bpf_verifier_state *state, u32 regno,
2145 enum bpf_reg_type type)
2146{
2147 struct bpf_reg_state *regs = state->regs;
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01002148 u32 id = regs[regno].id;
Thomas Graf14117072016-10-18 19:51:19 +02002149 int i;
2150
2151 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01002152 mark_map_reg(regs, i, id, type);
Thomas Graf14117072016-10-18 19:51:19 +02002153
2154 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
2155 if (state->stack_slot_type[i] != STACK_SPILL)
2156 continue;
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01002157 mark_map_reg(state->spilled_regs, i / BPF_REG_SIZE, id, type);
Thomas Graf14117072016-10-18 19:51:19 +02002158 }
2159}
2160
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002161static int check_cond_jmp_op(struct bpf_verifier_env *env,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002162 struct bpf_insn *insn, int *insn_idx)
2163{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002164 struct bpf_verifier_state *other_branch, *this_branch = &env->cur_state;
2165 struct bpf_reg_state *regs = this_branch->regs, *dst_reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002166 u8 opcode = BPF_OP(insn->code);
2167 int err;
2168
2169 if (opcode > BPF_EXIT) {
2170 verbose("invalid BPF_JMP opcode %x\n", opcode);
2171 return -EINVAL;
2172 }
2173
2174 if (BPF_SRC(insn->code) == BPF_X) {
2175 if (insn->imm != 0) {
2176 verbose("BPF_JMP uses reserved fields\n");
2177 return -EINVAL;
2178 }
2179
2180 /* check src1 operand */
2181 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2182 if (err)
2183 return err;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002184
2185 if (is_pointer_value(env, insn->src_reg)) {
2186 verbose("R%d pointer comparison prohibited\n",
2187 insn->src_reg);
2188 return -EACCES;
2189 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002190 } else {
2191 if (insn->src_reg != BPF_REG_0) {
2192 verbose("BPF_JMP uses reserved fields\n");
2193 return -EINVAL;
2194 }
2195 }
2196
2197 /* check src2 operand */
2198 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
2199 if (err)
2200 return err;
2201
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002202 dst_reg = &regs[insn->dst_reg];
2203
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002204 /* detect if R == 0 where R was initialized to zero earlier */
2205 if (BPF_SRC(insn->code) == BPF_K &&
2206 (opcode == BPF_JEQ || opcode == BPF_JNE) &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002207 dst_reg->type == CONST_IMM && dst_reg->imm == insn->imm) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002208 if (opcode == BPF_JEQ) {
2209 /* if (imm == imm) goto pc+off;
2210 * only follow the goto, ignore fall-through
2211 */
2212 *insn_idx += insn->off;
2213 return 0;
2214 } else {
2215 /* if (imm != imm) goto pc+off;
2216 * only follow fall-through branch, since
2217 * that's where the program will go
2218 */
2219 return 0;
2220 }
2221 }
2222
2223 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx);
2224 if (!other_branch)
2225 return -EFAULT;
2226
Josef Bacik48461132016-09-28 10:54:32 -04002227 /* detect if we are comparing against a constant value so we can adjust
2228 * our min/max values for our dst register.
2229 */
2230 if (BPF_SRC(insn->code) == BPF_X) {
2231 if (regs[insn->src_reg].type == CONST_IMM)
2232 reg_set_min_max(&other_branch->regs[insn->dst_reg],
2233 dst_reg, regs[insn->src_reg].imm,
2234 opcode);
2235 else if (dst_reg->type == CONST_IMM)
2236 reg_set_min_max_inv(&other_branch->regs[insn->src_reg],
2237 &regs[insn->src_reg], dst_reg->imm,
2238 opcode);
2239 } else {
2240 reg_set_min_max(&other_branch->regs[insn->dst_reg],
2241 dst_reg, insn->imm, opcode);
2242 }
2243
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002244 /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002245 if (BPF_SRC(insn->code) == BPF_K &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002246 insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
2247 dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
Thomas Graf14117072016-10-18 19:51:19 +02002248 /* Mark all identical map registers in each branch as either
2249 * safe or unknown depending R == 0 or R != 0 conditional.
2250 */
2251 mark_map_regs(this_branch, insn->dst_reg,
2252 opcode == BPF_JEQ ? PTR_TO_MAP_VALUE : UNKNOWN_VALUE);
2253 mark_map_regs(other_branch, insn->dst_reg,
2254 opcode == BPF_JEQ ? UNKNOWN_VALUE : PTR_TO_MAP_VALUE);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002255 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT &&
2256 dst_reg->type == PTR_TO_PACKET &&
2257 regs[insn->src_reg].type == PTR_TO_PACKET_END) {
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002258 find_good_pkt_pointers(this_branch, dst_reg);
2259 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGE &&
2260 dst_reg->type == PTR_TO_PACKET_END &&
2261 regs[insn->src_reg].type == PTR_TO_PACKET) {
2262 find_good_pkt_pointers(other_branch, &regs[insn->src_reg]);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002263 } else if (is_pointer_value(env, insn->dst_reg)) {
2264 verbose("R%d pointer comparison prohibited\n", insn->dst_reg);
2265 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002266 }
2267 if (log_level)
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002268 print_verifier_state(this_branch);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002269 return 0;
2270}
2271
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002272/* return the map pointer stored inside BPF_LD_IMM64 instruction */
2273static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
2274{
2275 u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
2276
2277 return (struct bpf_map *) (unsigned long) imm64;
2278}
2279
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002280/* verify BPF_LD_IMM64 instruction */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002281static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002282{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002283 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002284 int err;
2285
2286 if (BPF_SIZE(insn->code) != BPF_DW) {
2287 verbose("invalid BPF_LD_IMM insn\n");
2288 return -EINVAL;
2289 }
2290 if (insn->off != 0) {
2291 verbose("BPF_LD_IMM64 uses reserved fields\n");
2292 return -EINVAL;
2293 }
2294
2295 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
2296 if (err)
2297 return err;
2298
Jakub Kicinski6b173872016-09-21 11:43:59 +01002299 if (insn->src_reg == 0) {
2300 /* generic move 64-bit immediate into a register,
2301 * only analyzer needs to collect the ld_imm value.
2302 */
2303 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
2304
2305 if (!env->analyzer_ops)
2306 return 0;
2307
2308 regs[insn->dst_reg].type = CONST_IMM;
2309 regs[insn->dst_reg].imm = imm;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002310 return 0;
Jakub Kicinski6b173872016-09-21 11:43:59 +01002311 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002312
2313 /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
2314 BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
2315
2316 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
2317 regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
2318 return 0;
2319}
2320
Daniel Borkmann96be4322015-03-01 12:31:46 +01002321static bool may_access_skb(enum bpf_prog_type type)
2322{
2323 switch (type) {
2324 case BPF_PROG_TYPE_SOCKET_FILTER:
2325 case BPF_PROG_TYPE_SCHED_CLS:
Daniel Borkmann94caee82015-03-20 15:11:11 +01002326 case BPF_PROG_TYPE_SCHED_ACT:
Daniel Borkmann96be4322015-03-01 12:31:46 +01002327 return true;
2328 default:
2329 return false;
2330 }
2331}
2332
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002333/* verify safety of LD_ABS|LD_IND instructions:
2334 * - they can only appear in the programs where ctx == skb
2335 * - since they are wrappers of function calls, they scratch R1-R5 registers,
2336 * preserve R6-R9, and store return value into R0
2337 *
2338 * Implicit input:
2339 * ctx == skb == R6 == CTX
2340 *
2341 * Explicit input:
2342 * SRC == any register
2343 * IMM == 32-bit immediate
2344 *
2345 * Output:
2346 * R0 - 8/16/32-bit skb data converted to cpu endianness
2347 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002348static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002349{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002350 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002351 u8 mode = BPF_MODE(insn->code);
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002352 struct bpf_reg_state *reg;
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002353 int i, err;
2354
Daniel Borkmann24701ec2015-03-01 12:31:47 +01002355 if (!may_access_skb(env->prog->type)) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002356 verbose("BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002357 return -EINVAL;
2358 }
2359
2360 if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
Alexei Starovoitovd82bccc2016-04-12 10:26:19 -07002361 BPF_SIZE(insn->code) == BPF_DW ||
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002362 (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002363 verbose("BPF_LD_[ABS|IND] uses reserved fields\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002364 return -EINVAL;
2365 }
2366
2367 /* check whether implicit source operand (register R6) is readable */
2368 err = check_reg_arg(regs, BPF_REG_6, SRC_OP);
2369 if (err)
2370 return err;
2371
2372 if (regs[BPF_REG_6].type != PTR_TO_CTX) {
2373 verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
2374 return -EINVAL;
2375 }
2376
2377 if (mode == BPF_IND) {
2378 /* check explicit source operand */
2379 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2380 if (err)
2381 return err;
2382 }
2383
2384 /* reset caller saved regs to unreadable */
2385 for (i = 0; i < CALLER_SAVED_REGS; i++) {
2386 reg = regs + caller_saved[i];
2387 reg->type = NOT_INIT;
2388 reg->imm = 0;
2389 }
2390
2391 /* mark destination R0 register as readable, since it contains
2392 * the value fetched from the packet
2393 */
2394 regs[BPF_REG_0].type = UNKNOWN_VALUE;
2395 return 0;
2396}
2397
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002398/* non-recursive DFS pseudo code
2399 * 1 procedure DFS-iterative(G,v):
2400 * 2 label v as discovered
2401 * 3 let S be a stack
2402 * 4 S.push(v)
2403 * 5 while S is not empty
2404 * 6 t <- S.pop()
2405 * 7 if t is what we're looking for:
2406 * 8 return t
2407 * 9 for all edges e in G.adjacentEdges(t) do
2408 * 10 if edge e is already labelled
2409 * 11 continue with the next edge
2410 * 12 w <- G.adjacentVertex(t,e)
2411 * 13 if vertex w is not discovered and not explored
2412 * 14 label e as tree-edge
2413 * 15 label w as discovered
2414 * 16 S.push(w)
2415 * 17 continue at 5
2416 * 18 else if vertex w is discovered
2417 * 19 label e as back-edge
2418 * 20 else
2419 * 21 // vertex w is explored
2420 * 22 label e as forward- or cross-edge
2421 * 23 label t as explored
2422 * 24 S.pop()
2423 *
2424 * convention:
2425 * 0x10 - discovered
2426 * 0x11 - discovered and fall-through edge labelled
2427 * 0x12 - discovered and fall-through and branch edges labelled
2428 * 0x20 - explored
2429 */
2430
2431enum {
2432 DISCOVERED = 0x10,
2433 EXPLORED = 0x20,
2434 FALLTHROUGH = 1,
2435 BRANCH = 2,
2436};
2437
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002438#define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002439
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002440static int *insn_stack; /* stack of insns to process */
2441static int cur_stack; /* current stack index */
2442static int *insn_state;
2443
2444/* t, w, e - match pseudo-code above:
2445 * t - index of current instruction
2446 * w - next instruction
2447 * e - edge
2448 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002449static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002450{
2451 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
2452 return 0;
2453
2454 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
2455 return 0;
2456
2457 if (w < 0 || w >= env->prog->len) {
2458 verbose("jump out of range from insn %d to %d\n", t, w);
2459 return -EINVAL;
2460 }
2461
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002462 if (e == BRANCH)
2463 /* mark branch target for state pruning */
2464 env->explored_states[w] = STATE_LIST_MARK;
2465
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002466 if (insn_state[w] == 0) {
2467 /* tree-edge */
2468 insn_state[t] = DISCOVERED | e;
2469 insn_state[w] = DISCOVERED;
2470 if (cur_stack >= env->prog->len)
2471 return -E2BIG;
2472 insn_stack[cur_stack++] = w;
2473 return 1;
2474 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
2475 verbose("back-edge from insn %d to %d\n", t, w);
2476 return -EINVAL;
2477 } else if (insn_state[w] == EXPLORED) {
2478 /* forward- or cross-edge */
2479 insn_state[t] = DISCOVERED | e;
2480 } else {
2481 verbose("insn state internal bug\n");
2482 return -EFAULT;
2483 }
2484 return 0;
2485}
2486
2487/* non-recursive depth-first-search to detect loops in BPF program
2488 * loop == back-edge in directed graph
2489 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002490static int check_cfg(struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002491{
2492 struct bpf_insn *insns = env->prog->insnsi;
2493 int insn_cnt = env->prog->len;
2494 int ret = 0;
2495 int i, t;
2496
2497 insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
2498 if (!insn_state)
2499 return -ENOMEM;
2500
2501 insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
2502 if (!insn_stack) {
2503 kfree(insn_state);
2504 return -ENOMEM;
2505 }
2506
2507 insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
2508 insn_stack[0] = 0; /* 0 is the first instruction */
2509 cur_stack = 1;
2510
2511peek_stack:
2512 if (cur_stack == 0)
2513 goto check_state;
2514 t = insn_stack[cur_stack - 1];
2515
2516 if (BPF_CLASS(insns[t].code) == BPF_JMP) {
2517 u8 opcode = BPF_OP(insns[t].code);
2518
2519 if (opcode == BPF_EXIT) {
2520 goto mark_explored;
2521 } else if (opcode == BPF_CALL) {
2522 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2523 if (ret == 1)
2524 goto peek_stack;
2525 else if (ret < 0)
2526 goto err_free;
Daniel Borkmann07016152016-04-05 22:33:17 +02002527 if (t + 1 < insn_cnt)
2528 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002529 } else if (opcode == BPF_JA) {
2530 if (BPF_SRC(insns[t].code) != BPF_K) {
2531 ret = -EINVAL;
2532 goto err_free;
2533 }
2534 /* unconditional jump with single edge */
2535 ret = push_insn(t, t + insns[t].off + 1,
2536 FALLTHROUGH, env);
2537 if (ret == 1)
2538 goto peek_stack;
2539 else if (ret < 0)
2540 goto err_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002541 /* tell verifier to check for equivalent states
2542 * after every call and jump
2543 */
Alexei Starovoitovc3de6312015-04-14 15:57:13 -07002544 if (t + 1 < insn_cnt)
2545 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002546 } else {
2547 /* conditional jump with two edges */
Daniel Borkmann577aa832017-05-18 03:00:06 +02002548 env->explored_states[t] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002549 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2550 if (ret == 1)
2551 goto peek_stack;
2552 else if (ret < 0)
2553 goto err_free;
2554
2555 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
2556 if (ret == 1)
2557 goto peek_stack;
2558 else if (ret < 0)
2559 goto err_free;
2560 }
2561 } else {
2562 /* all other non-branch instructions with single
2563 * fall-through edge
2564 */
2565 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2566 if (ret == 1)
2567 goto peek_stack;
2568 else if (ret < 0)
2569 goto err_free;
2570 }
2571
2572mark_explored:
2573 insn_state[t] = EXPLORED;
2574 if (cur_stack-- <= 0) {
2575 verbose("pop stack internal bug\n");
2576 ret = -EFAULT;
2577 goto err_free;
2578 }
2579 goto peek_stack;
2580
2581check_state:
2582 for (i = 0; i < insn_cnt; i++) {
2583 if (insn_state[i] != EXPLORED) {
2584 verbose("unreachable insn %d\n", i);
2585 ret = -EINVAL;
2586 goto err_free;
2587 }
2588 }
2589 ret = 0; /* cfg looks good */
2590
2591err_free:
2592 kfree(insn_state);
2593 kfree(insn_stack);
2594 return ret;
2595}
2596
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002597/* the following conditions reduce the number of explored insns
2598 * from ~140k to ~80k for ultra large programs that use a lot of ptr_to_packet
2599 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002600static bool compare_ptrs_to_packet(struct bpf_reg_state *old,
2601 struct bpf_reg_state *cur)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002602{
2603 if (old->id != cur->id)
2604 return false;
2605
2606 /* old ptr_to_packet is more conservative, since it allows smaller
2607 * range. Ex:
2608 * old(off=0,r=10) is equal to cur(off=0,r=20), because
2609 * old(off=0,r=10) means that with range=10 the verifier proceeded
2610 * further and found no issues with the program. Now we're in the same
2611 * spot with cur(off=0,r=20), so we're safe too, since anything further
2612 * will only be looking at most 10 bytes after this pointer.
2613 */
2614 if (old->off == cur->off && old->range < cur->range)
2615 return true;
2616
2617 /* old(off=20,r=10) is equal to cur(off=22,re=22 or 5 or 0)
2618 * since both cannot be used for packet access and safe(old)
2619 * pointer has smaller off that could be used for further
2620 * 'if (ptr > data_end)' check
2621 * Ex:
2622 * old(off=20,r=10) and cur(off=22,r=22) and cur(off=22,r=0) mean
2623 * that we cannot access the packet.
2624 * The safe range is:
2625 * [ptr, ptr + range - off)
2626 * so whenever off >=range, it means no safe bytes from this pointer.
2627 * When comparing old->off <= cur->off, it means that older code
2628 * went with smaller offset and that offset was later
2629 * used to figure out the safe range after 'if (ptr > data_end)' check
2630 * Say, 'old' state was explored like:
2631 * ... R3(off=0, r=0)
2632 * R4 = R3 + 20
2633 * ... now R4(off=20,r=0) <-- here
2634 * if (R4 > data_end)
2635 * ... R4(off=20,r=20), R3(off=0,r=20) and R3 can be used to access.
2636 * ... the code further went all the way to bpf_exit.
2637 * Now the 'cur' state at the mark 'here' has R4(off=30,r=0).
2638 * old_R4(off=20,r=0) equal to cur_R4(off=30,r=0), since if the verifier
2639 * goes further, such cur_R4 will give larger safe packet range after
2640 * 'if (R4 > data_end)' and all further insn were already good with r=20,
2641 * so they will be good with r=30 and we can prune the search.
2642 */
2643 if (old->off <= cur->off &&
2644 old->off >= old->range && cur->off >= cur->range)
2645 return true;
2646
2647 return false;
2648}
2649
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002650/* compare two verifier states
2651 *
2652 * all states stored in state_list are known to be valid, since
2653 * verifier reached 'bpf_exit' instruction through them
2654 *
2655 * this function is called when verifier exploring different branches of
2656 * execution popped from the state stack. If it sees an old state that has
2657 * more strict register state and more strict stack state then this execution
2658 * branch doesn't need to be explored further, since verifier already
2659 * concluded that more strict state leads to valid finish.
2660 *
2661 * Therefore two states are equivalent if register state is more conservative
2662 * and explored stack state is more conservative than the current one.
2663 * Example:
2664 * explored current
2665 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
2666 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
2667 *
2668 * In other words if current stack state (one being explored) has more
2669 * valid slots than old one that already passed validation, it means
2670 * the verifier can stop exploring and conclude that current state is valid too
2671 *
2672 * Similarly with registers. If explored state has register type as invalid
2673 * whereas register type in current state is meaningful, it means that
2674 * the current state will reach 'bpf_exit' instruction safely
2675 */
Josef Bacik48461132016-09-28 10:54:32 -04002676static bool states_equal(struct bpf_verifier_env *env,
2677 struct bpf_verifier_state *old,
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002678 struct bpf_verifier_state *cur)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002679{
Josef Bacike2d2afe2016-11-29 12:27:09 -05002680 bool varlen_map_access = env->varlen_map_value_access;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002681 struct bpf_reg_state *rold, *rcur;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002682 int i;
2683
2684 for (i = 0; i < MAX_BPF_REG; i++) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002685 rold = &old->regs[i];
2686 rcur = &cur->regs[i];
2687
2688 if (memcmp(rold, rcur, sizeof(*rold)) == 0)
2689 continue;
2690
Josef Bacik48461132016-09-28 10:54:32 -04002691 /* If the ranges were not the same, but everything else was and
2692 * we didn't do a variable access into a map then we are a-ok.
2693 */
Josef Bacike2d2afe2016-11-29 12:27:09 -05002694 if (!varlen_map_access &&
Alexei Starovoitovb7f5aa12016-12-07 10:57:59 -08002695 memcmp(rold, rcur, offsetofend(struct bpf_reg_state, id)) == 0)
Josef Bacik48461132016-09-28 10:54:32 -04002696 continue;
2697
Josef Bacike2d2afe2016-11-29 12:27:09 -05002698 /* If we didn't map access then again we don't care about the
2699 * mismatched range values and it's ok if our old type was
2700 * UNKNOWN and we didn't go to a NOT_INIT'ed reg.
2701 */
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002702 if (rold->type == NOT_INIT ||
Josef Bacike2d2afe2016-11-29 12:27:09 -05002703 (!varlen_map_access && rold->type == UNKNOWN_VALUE &&
2704 rcur->type != NOT_INIT))
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002705 continue;
2706
Daniel Borkmann577aa832017-05-18 03:00:06 +02002707 /* Don't care about the reg->id in this case. */
2708 if (rold->type == PTR_TO_MAP_VALUE_OR_NULL &&
2709 rcur->type == PTR_TO_MAP_VALUE_OR_NULL &&
2710 rold->map_ptr == rcur->map_ptr)
2711 continue;
2712
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002713 if (rold->type == PTR_TO_PACKET && rcur->type == PTR_TO_PACKET &&
2714 compare_ptrs_to_packet(rold, rcur))
2715 continue;
2716
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002717 return false;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002718 }
2719
2720 for (i = 0; i < MAX_BPF_STACK; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002721 if (old->stack_slot_type[i] == STACK_INVALID)
2722 continue;
2723 if (old->stack_slot_type[i] != cur->stack_slot_type[i])
2724 /* Ex: old explored (safe) state has STACK_SPILL in
2725 * this stack slot, but current has has STACK_MISC ->
2726 * this verifier states are not equivalent,
2727 * return false to continue verification of this path
2728 */
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002729 return false;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002730 if (i % BPF_REG_SIZE)
2731 continue;
2732 if (memcmp(&old->spilled_regs[i / BPF_REG_SIZE],
2733 &cur->spilled_regs[i / BPF_REG_SIZE],
2734 sizeof(old->spilled_regs[0])))
2735 /* when explored and current stack slot types are
2736 * the same, check that stored pointers types
2737 * are the same as well.
2738 * Ex: explored safe path could have stored
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002739 * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -8}
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002740 * but current path has stored:
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002741 * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -16}
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002742 * such verifier states are not equivalent.
2743 * return false to continue verification of this path
2744 */
2745 return false;
2746 else
2747 continue;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002748 }
2749 return true;
2750}
2751
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002752static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002753{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002754 struct bpf_verifier_state_list *new_sl;
2755 struct bpf_verifier_state_list *sl;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002756
2757 sl = env->explored_states[insn_idx];
2758 if (!sl)
2759 /* this 'insn_idx' instruction wasn't marked, so we will not
2760 * be doing state search here
2761 */
2762 return 0;
2763
2764 while (sl != STATE_LIST_MARK) {
Josef Bacik48461132016-09-28 10:54:32 -04002765 if (states_equal(env, &sl->state, &env->cur_state))
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002766 /* reached equivalent register/stack state,
2767 * prune the search
2768 */
2769 return 1;
2770 sl = sl->next;
2771 }
2772
2773 /* there were no equivalent states, remember current one.
2774 * technically the current state is not proven to be safe yet,
2775 * but it will either reach bpf_exit (which means it's safe) or
2776 * it will be rejected. Since there are no loops, we won't be
2777 * seeing this 'insn_idx' instruction again on the way to bpf_exit
2778 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002779 new_sl = kmalloc(sizeof(struct bpf_verifier_state_list), GFP_USER);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002780 if (!new_sl)
2781 return -ENOMEM;
2782
2783 /* add new state to the head of linked list */
2784 memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state));
2785 new_sl->next = env->explored_states[insn_idx];
2786 env->explored_states[insn_idx] = new_sl;
2787 return 0;
2788}
2789
Jakub Kicinski13a27df2016-09-21 11:43:58 +01002790static int ext_analyzer_insn_hook(struct bpf_verifier_env *env,
2791 int insn_idx, int prev_insn_idx)
2792{
2793 if (!env->analyzer_ops || !env->analyzer_ops->insn_hook)
2794 return 0;
2795
2796 return env->analyzer_ops->insn_hook(env, insn_idx, prev_insn_idx);
2797}
2798
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002799static int do_check(struct bpf_verifier_env *env)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002800{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002801 struct bpf_verifier_state *state = &env->cur_state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002802 struct bpf_insn *insns = env->prog->insnsi;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002803 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002804 int insn_cnt = env->prog->len;
2805 int insn_idx, prev_insn_idx = 0;
2806 int insn_processed = 0;
2807 bool do_print_state = false;
2808
2809 init_reg_state(regs);
2810 insn_idx = 0;
Josef Bacik48461132016-09-28 10:54:32 -04002811 env->varlen_map_value_access = false;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002812 for (;;) {
2813 struct bpf_insn *insn;
2814 u8 class;
2815 int err;
2816
2817 if (insn_idx >= insn_cnt) {
2818 verbose("invalid insn idx %d insn_cnt %d\n",
2819 insn_idx, insn_cnt);
2820 return -EFAULT;
2821 }
2822
2823 insn = &insns[insn_idx];
2824 class = BPF_CLASS(insn->code);
2825
Daniel Borkmann07016152016-04-05 22:33:17 +02002826 if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002827 verbose("BPF program is too large. Proccessed %d insn\n",
2828 insn_processed);
2829 return -E2BIG;
2830 }
2831
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002832 err = is_state_visited(env, insn_idx);
2833 if (err < 0)
2834 return err;
2835 if (err == 1) {
2836 /* found equivalent state, can prune the search */
2837 if (log_level) {
2838 if (do_print_state)
2839 verbose("\nfrom %d to %d: safe\n",
2840 prev_insn_idx, insn_idx);
2841 else
2842 verbose("%d: safe\n", insn_idx);
2843 }
2844 goto process_bpf_exit;
2845 }
2846
Daniel Borkmann577aa832017-05-18 03:00:06 +02002847 if (need_resched())
2848 cond_resched();
2849
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002850 if (log_level && do_print_state) {
2851 verbose("\nfrom %d to %d:", prev_insn_idx, insn_idx);
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002852 print_verifier_state(&env->cur_state);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002853 do_print_state = false;
2854 }
2855
2856 if (log_level) {
2857 verbose("%d: ", insn_idx);
Daniel Borkmannced0a312017-05-08 00:04:09 +02002858 print_bpf_insn(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002859 }
2860
Jakub Kicinski13a27df2016-09-21 11:43:58 +01002861 err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx);
2862 if (err)
2863 return err;
2864
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002865 if (class == BPF_ALU || class == BPF_ALU64) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002866 err = check_alu_op(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002867 if (err)
2868 return err;
2869
2870 } else if (class == BPF_LDX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002871 enum bpf_reg_type *prev_src_type, src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002872
2873 /* check for reserved fields is already done */
2874
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002875 /* check src operand */
2876 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2877 if (err)
2878 return err;
2879
2880 err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
2881 if (err)
2882 return err;
2883
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07002884 src_reg_type = regs[insn->src_reg].type;
2885
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002886 /* check that memory (src_reg + off) is readable,
2887 * the state of dst_reg will be updated by this func
2888 */
2889 err = check_mem_access(env, insn->src_reg, insn->off,
2890 BPF_SIZE(insn->code), BPF_READ,
2891 insn->dst_reg);
2892 if (err)
2893 return err;
2894
Josef Bacik48461132016-09-28 10:54:32 -04002895 reset_reg_range_values(regs, insn->dst_reg);
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07002896 if (BPF_SIZE(insn->code) != BPF_W &&
2897 BPF_SIZE(insn->code) != BPF_DW) {
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07002898 insn_idx++;
2899 continue;
2900 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002901
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002902 prev_src_type = &env->insn_aux_data[insn_idx].ptr_type;
2903
2904 if (*prev_src_type == NOT_INIT) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002905 /* saw a valid insn
2906 * dst_reg = *(u32 *)(src_reg + off)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002907 * save type to validate intersecting paths
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002908 */
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002909 *prev_src_type = src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002910
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002911 } else if (src_reg_type != *prev_src_type &&
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002912 (src_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002913 *prev_src_type == PTR_TO_CTX)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002914 /* ABuser program is trying to use the same insn
2915 * dst_reg = *(u32*) (src_reg + off)
2916 * with different pointer types:
2917 * src_reg == ctx in one branch and
2918 * src_reg == stack|map in some other branch.
2919 * Reject it.
2920 */
2921 verbose("same insn cannot be used with different pointers\n");
2922 return -EINVAL;
2923 }
2924
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002925 } else if (class == BPF_STX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002926 enum bpf_reg_type *prev_dst_type, dst_reg_type;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002927
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002928 if (BPF_MODE(insn->code) == BPF_XADD) {
2929 err = check_xadd(env, insn);
2930 if (err)
2931 return err;
2932 insn_idx++;
2933 continue;
2934 }
2935
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002936 /* check src1 operand */
2937 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2938 if (err)
2939 return err;
2940 /* check src2 operand */
2941 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
2942 if (err)
2943 return err;
2944
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002945 dst_reg_type = regs[insn->dst_reg].type;
2946
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002947 /* check that memory (dst_reg + off) is writeable */
2948 err = check_mem_access(env, insn->dst_reg, insn->off,
2949 BPF_SIZE(insn->code), BPF_WRITE,
2950 insn->src_reg);
2951 if (err)
2952 return err;
2953
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002954 prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type;
2955
2956 if (*prev_dst_type == NOT_INIT) {
2957 *prev_dst_type = dst_reg_type;
2958 } else if (dst_reg_type != *prev_dst_type &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002959 (dst_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002960 *prev_dst_type == PTR_TO_CTX)) {
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002961 verbose("same insn cannot be used with different pointers\n");
2962 return -EINVAL;
2963 }
2964
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002965 } else if (class == BPF_ST) {
2966 if (BPF_MODE(insn->code) != BPF_MEM ||
2967 insn->src_reg != BPF_REG_0) {
2968 verbose("BPF_ST uses reserved fields\n");
2969 return -EINVAL;
2970 }
2971 /* check src operand */
2972 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
2973 if (err)
2974 return err;
2975
2976 /* check that memory (dst_reg + off) is writeable */
2977 err = check_mem_access(env, insn->dst_reg, insn->off,
2978 BPF_SIZE(insn->code), BPF_WRITE,
2979 -1);
2980 if (err)
2981 return err;
2982
2983 } else if (class == BPF_JMP) {
2984 u8 opcode = BPF_OP(insn->code);
2985
2986 if (opcode == BPF_CALL) {
2987 if (BPF_SRC(insn->code) != BPF_K ||
2988 insn->off != 0 ||
2989 insn->src_reg != BPF_REG_0 ||
2990 insn->dst_reg != BPF_REG_0) {
2991 verbose("BPF_CALL uses reserved fields\n");
2992 return -EINVAL;
2993 }
2994
2995 err = check_call(env, insn->imm);
2996 if (err)
2997 return err;
2998
2999 } else if (opcode == BPF_JA) {
3000 if (BPF_SRC(insn->code) != BPF_K ||
3001 insn->imm != 0 ||
3002 insn->src_reg != BPF_REG_0 ||
3003 insn->dst_reg != BPF_REG_0) {
3004 verbose("BPF_JA uses reserved fields\n");
3005 return -EINVAL;
3006 }
3007
3008 insn_idx += insn->off + 1;
3009 continue;
3010
3011 } else if (opcode == BPF_EXIT) {
3012 if (BPF_SRC(insn->code) != BPF_K ||
3013 insn->imm != 0 ||
3014 insn->src_reg != BPF_REG_0 ||
3015 insn->dst_reg != BPF_REG_0) {
3016 verbose("BPF_EXIT uses reserved fields\n");
3017 return -EINVAL;
3018 }
3019
3020 /* eBPF calling convetion is such that R0 is used
3021 * to return the value from eBPF program.
3022 * Make sure that it's readable at this time
3023 * of bpf_exit, which means that program wrote
3024 * something into it earlier
3025 */
3026 err = check_reg_arg(regs, BPF_REG_0, SRC_OP);
3027 if (err)
3028 return err;
3029
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003030 if (is_pointer_value(env, BPF_REG_0)) {
3031 verbose("R0 leaks addr as return value\n");
3032 return -EACCES;
3033 }
3034
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003035process_bpf_exit:
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003036 insn_idx = pop_stack(env, &prev_insn_idx);
3037 if (insn_idx < 0) {
3038 break;
3039 } else {
3040 do_print_state = true;
3041 continue;
3042 }
3043 } else {
3044 err = check_cond_jmp_op(env, insn, &insn_idx);
3045 if (err)
3046 return err;
3047 }
3048 } else if (class == BPF_LD) {
3049 u8 mode = BPF_MODE(insn->code);
3050
3051 if (mode == BPF_ABS || mode == BPF_IND) {
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003052 err = check_ld_abs(env, insn);
3053 if (err)
3054 return err;
3055
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003056 } else if (mode == BPF_IMM) {
3057 err = check_ld_imm(env, insn);
3058 if (err)
3059 return err;
3060
3061 insn_idx++;
3062 } else {
3063 verbose("invalid BPF_LD mode\n");
3064 return -EINVAL;
3065 }
Josef Bacik48461132016-09-28 10:54:32 -04003066 reset_reg_range_values(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003067 } else {
3068 verbose("unknown insn class %d\n", class);
3069 return -EINVAL;
3070 }
3071
3072 insn_idx++;
3073 }
3074
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003075 verbose("processed %d insns\n", insn_processed);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003076 return 0;
3077}
3078
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07003079static int check_map_prog_compatibility(struct bpf_map *map,
3080 struct bpf_prog *prog)
3081
3082{
3083 if (prog->type == BPF_PROG_TYPE_PERF_EVENT &&
3084 (map->map_type == BPF_MAP_TYPE_HASH ||
3085 map->map_type == BPF_MAP_TYPE_PERCPU_HASH) &&
3086 (map->map_flags & BPF_F_NO_PREALLOC)) {
3087 verbose("perf_event programs can only use preallocated hash map\n");
3088 return -EINVAL;
3089 }
3090 return 0;
3091}
3092
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003093/* look for pseudo eBPF instructions that access map FDs and
3094 * replace them with actual map pointers
3095 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003096static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003097{
3098 struct bpf_insn *insn = env->prog->insnsi;
3099 int insn_cnt = env->prog->len;
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07003100 int i, j, err;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003101
3102 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003103 if (BPF_CLASS(insn->code) == BPF_LDX &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003104 (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003105 verbose("BPF_LDX uses reserved fields\n");
3106 return -EINVAL;
3107 }
3108
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003109 if (BPF_CLASS(insn->code) == BPF_STX &&
3110 ((BPF_MODE(insn->code) != BPF_MEM &&
3111 BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
3112 verbose("BPF_STX uses reserved fields\n");
3113 return -EINVAL;
3114 }
3115
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003116 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
3117 struct bpf_map *map;
3118 struct fd f;
3119
3120 if (i == insn_cnt - 1 || insn[1].code != 0 ||
3121 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
3122 insn[1].off != 0) {
3123 verbose("invalid bpf_ld_imm64 insn\n");
3124 return -EINVAL;
3125 }
3126
3127 if (insn->src_reg == 0)
3128 /* valid generic load 64-bit imm */
3129 goto next_insn;
3130
3131 if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
3132 verbose("unrecognized bpf_ld_imm64 insn\n");
3133 return -EINVAL;
3134 }
3135
3136 f = fdget(insn->imm);
Daniel Borkmannc2101292015-10-29 14:58:07 +01003137 map = __bpf_map_get(f);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003138 if (IS_ERR(map)) {
3139 verbose("fd %d is not pointing to valid bpf_map\n",
3140 insn->imm);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003141 return PTR_ERR(map);
3142 }
3143
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07003144 err = check_map_prog_compatibility(map, env->prog);
3145 if (err) {
3146 fdput(f);
3147 return err;
3148 }
3149
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003150 /* store map pointer inside BPF_LD_IMM64 instruction */
3151 insn[0].imm = (u32) (unsigned long) map;
3152 insn[1].imm = ((u64) (unsigned long) map) >> 32;
3153
3154 /* check whether we recorded this map already */
3155 for (j = 0; j < env->used_map_cnt; j++)
3156 if (env->used_maps[j] == map) {
3157 fdput(f);
3158 goto next_insn;
3159 }
3160
3161 if (env->used_map_cnt >= MAX_USED_MAPS) {
3162 fdput(f);
3163 return -E2BIG;
3164 }
3165
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003166 /* hold the map. If the program is rejected by verifier,
3167 * the map will be released by release_maps() or it
3168 * will be used by the valid program until it's unloaded
3169 * and all maps are released in free_bpf_prog_info()
3170 */
Alexei Starovoitov92117d82016-04-27 18:56:20 -07003171 map = bpf_map_inc(map, false);
3172 if (IS_ERR(map)) {
3173 fdput(f);
3174 return PTR_ERR(map);
3175 }
3176 env->used_maps[env->used_map_cnt++] = map;
3177
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003178 fdput(f);
3179next_insn:
3180 insn++;
3181 i++;
3182 }
3183 }
3184
3185 /* now all pseudo BPF_LD_IMM64 instructions load valid
3186 * 'struct bpf_map *' into a register instead of user map_fd.
3187 * These pointers will be used later by verifier to validate map access.
3188 */
3189 return 0;
3190}
3191
3192/* drop refcnt of maps used by the rejected program */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003193static void release_maps(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003194{
3195 int i;
3196
3197 for (i = 0; i < env->used_map_cnt; i++)
3198 bpf_map_put(env->used_maps[i]);
3199}
3200
3201/* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003202static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003203{
3204 struct bpf_insn *insn = env->prog->insnsi;
3205 int insn_cnt = env->prog->len;
3206 int i;
3207
3208 for (i = 0; i < insn_cnt; i++, insn++)
3209 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
3210 insn->src_reg = 0;
3211}
3212
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003213/* convert load instructions that access fields of 'struct __sk_buff'
3214 * into sequence of instructions that access fields of 'struct sk_buff'
3215 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003216static int convert_ctx_accesses(struct bpf_verifier_env *env)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003217{
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003218 const struct bpf_verifier_ops *ops = env->prog->aux->ops;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003219 const int insn_cnt = env->prog->len;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003220 struct bpf_insn insn_buf[16], *insn;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003221 struct bpf_prog *new_prog;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003222 enum bpf_access_type type;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003223 int i, cnt, delta = 0;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003224
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003225 if (ops->gen_prologue) {
3226 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
3227 env->prog);
3228 if (cnt >= ARRAY_SIZE(insn_buf)) {
3229 verbose("bpf verifier is misconfigured\n");
3230 return -EINVAL;
3231 } else if (cnt) {
3232 new_prog = bpf_patch_insn_single(env->prog, 0,
3233 insn_buf, cnt);
3234 if (!new_prog)
3235 return -ENOMEM;
3236 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003237 delta += cnt - 1;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003238 }
3239 }
3240
3241 if (!ops->convert_ctx_access)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003242 return 0;
3243
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003244 insn = env->prog->insnsi + delta;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003245
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003246 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07003247 if (insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
3248 insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003249 type = BPF_READ;
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07003250 else if (insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
3251 insn->code == (BPF_STX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003252 type = BPF_WRITE;
3253 else
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003254 continue;
3255
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003256 if (env->insn_aux_data[i].ptr_type != PTR_TO_CTX)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003257 continue;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003258
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003259 cnt = ops->convert_ctx_access(type, insn->dst_reg, insn->src_reg,
3260 insn->off, insn_buf, env->prog);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003261 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
3262 verbose("bpf verifier is misconfigured\n");
3263 return -EINVAL;
3264 }
3265
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003266 new_prog = bpf_patch_insn_single(env->prog, i + delta, insn_buf,
3267 cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003268 if (!new_prog)
3269 return -ENOMEM;
3270
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003271 delta += cnt - 1;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003272
3273 /* keep walking new program and skip insns we just inserted */
3274 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003275 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003276 }
3277
3278 return 0;
3279}
3280
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003281static void free_states(struct bpf_verifier_env *env)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003282{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003283 struct bpf_verifier_state_list *sl, *sln;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003284 int i;
3285
3286 if (!env->explored_states)
3287 return;
3288
3289 for (i = 0; i < env->prog->len; i++) {
3290 sl = env->explored_states[i];
3291
3292 if (sl)
3293 while (sl != STATE_LIST_MARK) {
3294 sln = sl->next;
3295 kfree(sl);
3296 sl = sln;
3297 }
3298 }
3299
3300 kfree(env->explored_states);
3301}
3302
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003303int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003304{
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003305 char __user *log_ubuf = NULL;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003306 struct bpf_verifier_env *env;
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003307 int ret = -EINVAL;
3308
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003309 if ((*prog)->len <= 0 || (*prog)->len > BPF_MAXINSNS)
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003310 return -E2BIG;
3311
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003312 /* 'struct bpf_verifier_env' can be global, but since it's not small,
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003313 * allocate/free it every time bpf_check() is called
3314 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003315 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003316 if (!env)
3317 return -ENOMEM;
3318
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003319 env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
3320 (*prog)->len);
3321 ret = -ENOMEM;
3322 if (!env->insn_aux_data)
3323 goto err_free_env;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003324 env->prog = *prog;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003325
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003326 /* grab the mutex to protect few globals used by verifier */
3327 mutex_lock(&bpf_verifier_lock);
3328
3329 if (attr->log_level || attr->log_buf || attr->log_size) {
3330 /* user requested verbose verifier output
3331 * and supplied buffer to store the verification trace
3332 */
3333 log_level = attr->log_level;
3334 log_ubuf = (char __user *) (unsigned long) attr->log_buf;
3335 log_size = attr->log_size;
3336 log_len = 0;
3337
3338 ret = -EINVAL;
3339 /* log_* values have to be sane */
3340 if (log_size < 128 || log_size > UINT_MAX >> 8 ||
3341 log_level == 0 || log_ubuf == NULL)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003342 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003343
3344 ret = -ENOMEM;
3345 log_buf = vmalloc(log_size);
3346 if (!log_buf)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003347 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003348 } else {
3349 log_level = 0;
3350 }
3351
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003352 ret = replace_map_fd_with_map_ptr(env);
3353 if (ret < 0)
3354 goto skip_full_check;
3355
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003356 env->explored_states = kcalloc(env->prog->len,
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003357 sizeof(struct bpf_verifier_state_list *),
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003358 GFP_USER);
3359 ret = -ENOMEM;
3360 if (!env->explored_states)
3361 goto skip_full_check;
3362
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003363 ret = check_cfg(env);
3364 if (ret < 0)
3365 goto skip_full_check;
3366
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003367 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
3368
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003369 ret = do_check(env);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003370
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003371skip_full_check:
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003372 while (pop_stack(env, NULL) >= 0);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003373 free_states(env);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003374
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003375 if (ret == 0)
3376 /* program is valid, convert *(u32*)(ctx + off) accesses */
3377 ret = convert_ctx_accesses(env);
3378
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003379 if (log_level && log_len >= log_size - 1) {
3380 BUG_ON(log_len >= log_size);
3381 /* verifier log exceeded user supplied buffer */
3382 ret = -ENOSPC;
3383 /* fall through to return what was recorded */
3384 }
3385
3386 /* copy verifier log back to user space including trailing zero */
3387 if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) {
3388 ret = -EFAULT;
3389 goto free_log_buf;
3390 }
3391
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003392 if (ret == 0 && env->used_map_cnt) {
3393 /* if program passed verifier, update used_maps in bpf_prog_info */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003394 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
3395 sizeof(env->used_maps[0]),
3396 GFP_KERNEL);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003397
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003398 if (!env->prog->aux->used_maps) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003399 ret = -ENOMEM;
3400 goto free_log_buf;
3401 }
3402
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003403 memcpy(env->prog->aux->used_maps, env->used_maps,
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003404 sizeof(env->used_maps[0]) * env->used_map_cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003405 env->prog->aux->used_map_cnt = env->used_map_cnt;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003406
3407 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
3408 * bpf_ld_imm64 instructions
3409 */
3410 convert_pseudo_ld_imm64(env);
3411 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003412
3413free_log_buf:
3414 if (log_level)
3415 vfree(log_buf);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003416 if (!env->prog->aux->used_maps)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003417 /* if we didn't copy map pointers into bpf_prog_info, release
3418 * them now. Otherwise free_bpf_prog_info() will release them.
3419 */
3420 release_maps(env);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003421 *prog = env->prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003422err_unlock:
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003423 mutex_unlock(&bpf_verifier_lock);
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003424 vfree(env->insn_aux_data);
3425err_free_env:
3426 kfree(env);
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003427 return ret;
3428}
Jakub Kicinski13a27df2016-09-21 11:43:58 +01003429
3430int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops,
3431 void *priv)
3432{
3433 struct bpf_verifier_env *env;
3434 int ret;
3435
3436 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
3437 if (!env)
3438 return -ENOMEM;
3439
3440 env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
3441 prog->len);
3442 ret = -ENOMEM;
3443 if (!env->insn_aux_data)
3444 goto err_free_env;
3445 env->prog = prog;
3446 env->analyzer_ops = ops;
3447 env->analyzer_priv = priv;
3448
3449 /* grab the mutex to protect few globals used by verifier */
3450 mutex_lock(&bpf_verifier_lock);
3451
3452 log_level = 0;
3453
3454 env->explored_states = kcalloc(env->prog->len,
3455 sizeof(struct bpf_verifier_state_list *),
3456 GFP_KERNEL);
3457 ret = -ENOMEM;
3458 if (!env->explored_states)
3459 goto skip_full_check;
3460
3461 ret = check_cfg(env);
3462 if (ret < 0)
3463 goto skip_full_check;
3464
3465 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
3466
3467 ret = do_check(env);
3468
3469skip_full_check:
3470 while (pop_stack(env, NULL) >= 0);
3471 free_states(env);
3472
3473 mutex_unlock(&bpf_verifier_lock);
3474 vfree(env->insn_aux_data);
3475err_free_env:
3476 kfree(env);
3477 return ret;
3478}
3479EXPORT_SYMBOL_GPL(bpf_analyzer);