blob: 2ee2e7970df63e8de1e06fbb8658a08d267ef0e8 [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
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100685static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700686{
687 if (env->allow_ptr_leaks)
688 return false;
689
690 switch (env->cur_state.regs[regno].type) {
691 case UNKNOWN_VALUE:
692 case CONST_IMM:
693 return false;
694 default:
695 return true;
696 }
697}
698
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100699static int check_ptr_alignment(struct bpf_verifier_env *env,
700 struct bpf_reg_state *reg, int off, int size)
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700701{
Josef Bacik48461132016-09-28 10:54:32 -0400702 if (reg->type != PTR_TO_PACKET && reg->type != PTR_TO_MAP_VALUE_ADJ) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700703 if (off % size != 0) {
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100704 verbose("misaligned access off %d size %d\n",
705 off, size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700706 return -EACCES;
707 } else {
708 return 0;
709 }
710 }
711
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700712 if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
713 /* misaligned access to packet is ok on x86,arm,arm64 */
714 return 0;
715
716 if (reg->id && size != 1) {
717 verbose("Unknown packet alignment. Only byte-sized access allowed\n");
718 return -EACCES;
719 }
720
721 /* skb->data is NET_IP_ALIGN-ed */
Josef Bacik48461132016-09-28 10:54:32 -0400722 if (reg->type == PTR_TO_PACKET &&
723 (NET_IP_ALIGN + reg->off + off) % size != 0) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700724 verbose("misaligned packet access off %d+%d+%d size %d\n",
725 NET_IP_ALIGN, reg->off, off, size);
726 return -EACCES;
727 }
728 return 0;
729}
730
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700731/* check whether memory at (regno + off) is accessible for t = (read | write)
732 * if t==write, value_regno is a register which value is stored into memory
733 * if t==read, value_regno is a register which will receive the value from memory
734 * if t==write && value_regno==-1, some unknown value is stored into memory
735 * if t==read && value_regno==-1, don't care what we read from memory
736 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100737static int check_mem_access(struct bpf_verifier_env *env, u32 regno, int off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700738 int bpf_size, enum bpf_access_type t,
739 int value_regno)
740{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100741 struct bpf_verifier_state *state = &env->cur_state;
742 struct bpf_reg_state *reg = &state->regs[regno];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700743 int size, err = 0;
744
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700745 if (reg->type == PTR_TO_STACK)
746 off += reg->imm;
Alex Gartrell24b4d2a2015-07-23 14:24:40 -0700747
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700748 size = bpf_size_to_bytes(bpf_size);
749 if (size < 0)
750 return size;
751
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700752 err = check_ptr_alignment(env, reg, off, size);
753 if (err)
754 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700755
Josef Bacik48461132016-09-28 10:54:32 -0400756 if (reg->type == PTR_TO_MAP_VALUE ||
757 reg->type == PTR_TO_MAP_VALUE_ADJ) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700758 if (t == BPF_WRITE && value_regno >= 0 &&
759 is_pointer_value(env, value_regno)) {
760 verbose("R%d leaks addr into map\n", value_regno);
761 return -EACCES;
762 }
Josef Bacik48461132016-09-28 10:54:32 -0400763
764 /* If we adjusted the register to this map value at all then we
765 * need to change off and size to min_value and max_value
766 * respectively to make sure our theoretical access will be
767 * safe.
768 */
769 if (reg->type == PTR_TO_MAP_VALUE_ADJ) {
770 if (log_level)
771 print_verifier_state(state);
772 env->varlen_map_value_access = true;
773 /* The minimum value is only important with signed
774 * comparisons where we can't assume the floor of a
775 * value is 0. If we are using signed variables for our
776 * index'es we need to make sure that whatever we use
777 * will have a set floor within our range.
778 */
Josef Bacikf23cc642016-11-14 15:45:36 -0500779 if (reg->min_value < 0) {
Josef Bacik48461132016-09-28 10:54:32 -0400780 verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
781 regno);
782 return -EACCES;
783 }
784 err = check_map_access(env, regno, reg->min_value + off,
785 size);
786 if (err) {
787 verbose("R%d min value is outside of the array range\n",
788 regno);
789 return err;
790 }
791
792 /* If we haven't set a max value then we need to bail
793 * since we can't be sure we won't do bad things.
794 */
795 if (reg->max_value == BPF_REGISTER_MAX_RANGE) {
796 verbose("R%d unbounded memory access, make sure to bounds check any array access into a map\n",
797 regno);
798 return -EACCES;
799 }
800 off += reg->max_value;
801 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700802 err = check_map_access(env, regno, off, size);
803 if (!err && t == BPF_READ && value_regno >= 0)
804 mark_reg_unknown_value(state->regs, value_regno);
805
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700806 } else if (reg->type == PTR_TO_CTX) {
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700807 enum bpf_reg_type reg_type = UNKNOWN_VALUE;
808
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700809 if (t == BPF_WRITE && value_regno >= 0 &&
810 is_pointer_value(env, value_regno)) {
811 verbose("R%d leaks addr into ctx\n", value_regno);
812 return -EACCES;
813 }
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700814 err = check_ctx_access(env, off, size, t, &reg_type);
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700815 if (!err && t == BPF_READ && value_regno >= 0) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700816 mark_reg_unknown_value(state->regs, value_regno);
Mickaël Salaün19553512016-09-24 20:01:50 +0200817 /* note that reg.[id|off|range] == 0 */
818 state->regs[value_regno].type = reg_type;
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700819 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700820
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700821 } else if (reg->type == FRAME_PTR || reg->type == PTR_TO_STACK) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700822 if (off >= 0 || off < -MAX_BPF_STACK) {
823 verbose("invalid stack off=%d size=%d\n", off, size);
824 return -EACCES;
825 }
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700826 if (t == BPF_WRITE) {
827 if (!env->allow_ptr_leaks &&
828 state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL &&
829 size != BPF_REG_SIZE) {
830 verbose("attempt to corrupt spilled pointer on stack\n");
831 return -EACCES;
832 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700833 err = check_stack_write(state, off, size, value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700834 } else {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700835 err = check_stack_read(state, off, size, value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700836 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700837 } else if (state->regs[regno].type == PTR_TO_PACKET) {
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200838 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL)) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700839 verbose("cannot write into packet\n");
840 return -EACCES;
841 }
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700842 if (t == BPF_WRITE && value_regno >= 0 &&
843 is_pointer_value(env, value_regno)) {
844 verbose("R%d leaks addr into packet\n", value_regno);
845 return -EACCES;
846 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700847 err = check_packet_access(env, regno, off, size);
848 if (!err && t == BPF_READ && value_regno >= 0)
849 mark_reg_unknown_value(state->regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700850 } else {
851 verbose("R%d invalid mem access '%s'\n",
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700852 regno, reg_type_str[reg->type]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700853 return -EACCES;
854 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700855
856 if (!err && size <= 2 && value_regno >= 0 && env->allow_ptr_leaks &&
857 state->regs[value_regno].type == UNKNOWN_VALUE) {
858 /* 1 or 2 byte load zero-extends, determine the number of
859 * zero upper bits. Not doing it fo 4 byte load, since
860 * such values cannot be added to ptr_to_packet anyway.
861 */
862 state->regs[value_regno].imm = 64 - size * 8;
863 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700864 return err;
865}
866
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100867static int check_xadd(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700868{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100869 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700870 int err;
871
872 if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
873 insn->imm != 0) {
874 verbose("BPF_XADD uses reserved fields\n");
875 return -EINVAL;
876 }
877
878 /* check src1 operand */
879 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
880 if (err)
881 return err;
882
883 /* check src2 operand */
884 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
885 if (err)
886 return err;
887
Daniel Borkmanncd5de9c2017-06-29 03:04:59 +0200888 if (is_pointer_value(env, insn->src_reg)) {
889 verbose("R%d leaks addr into mem\n", insn->src_reg);
890 return -EACCES;
891 }
892
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700893 /* check whether atomic_add can read the memory */
894 err = check_mem_access(env, insn->dst_reg, insn->off,
895 BPF_SIZE(insn->code), BPF_READ, -1);
896 if (err)
897 return err;
898
899 /* check whether atomic_add can write into the same memory */
900 return check_mem_access(env, insn->dst_reg, insn->off,
901 BPF_SIZE(insn->code), BPF_WRITE, -1);
902}
903
904/* when register 'regno' is passed into function that will read 'access_size'
905 * bytes from that pointer, make sure that it's within stack boundary
906 * and all elements of stack are initialized
907 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100908static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
Daniel Borkmann435faee12016-04-13 00:10:51 +0200909 int access_size, bool zero_size_allowed,
910 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700911{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100912 struct bpf_verifier_state *state = &env->cur_state;
913 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700914 int off, i;
915
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +0100916 if (regs[regno].type != PTR_TO_STACK) {
917 if (zero_size_allowed && access_size == 0 &&
918 regs[regno].type == CONST_IMM &&
919 regs[regno].imm == 0)
920 return 0;
921
922 verbose("R%d type=%s expected=%s\n", regno,
923 reg_type_str[regs[regno].type],
924 reg_type_str[PTR_TO_STACK]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700925 return -EACCES;
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +0100926 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700927
928 off = regs[regno].imm;
929 if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
930 access_size <= 0) {
931 verbose("invalid stack type R%d off=%d access_size=%d\n",
932 regno, off, access_size);
933 return -EACCES;
934 }
935
Daniel Borkmann435faee12016-04-13 00:10:51 +0200936 if (meta && meta->raw_mode) {
937 meta->access_size = access_size;
938 meta->regno = regno;
939 return 0;
940 }
941
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700942 for (i = 0; i < access_size; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700943 if (state->stack_slot_type[MAX_BPF_STACK + off + i] != STACK_MISC) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700944 verbose("invalid indirect read from stack off %d+%d size %d\n",
945 off, i, access_size);
946 return -EACCES;
947 }
948 }
949 return 0;
950}
951
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100952static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200953 enum bpf_arg_type arg_type,
954 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700955{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100956 struct bpf_reg_state *regs = env->cur_state.regs, *reg = &regs[regno];
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700957 enum bpf_reg_type expected_type, type = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700958 int err = 0;
959
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100960 if (arg_type == ARG_DONTCARE)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700961 return 0;
962
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700963 if (type == NOT_INIT) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700964 verbose("R%d !read_ok\n", regno);
965 return -EACCES;
966 }
967
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700968 if (arg_type == ARG_ANYTHING) {
969 if (is_pointer_value(env, regno)) {
970 verbose("R%d leaks addr into helper function\n", regno);
971 return -EACCES;
972 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100973 return 0;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700974 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100975
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200976 if (type == PTR_TO_PACKET && !may_access_direct_pkt_data(env, meta)) {
977 verbose("helper access to the packet is not allowed\n");
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700978 return -EACCES;
979 }
980
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +0100981 if (arg_type == ARG_PTR_TO_MAP_KEY ||
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700982 arg_type == ARG_PTR_TO_MAP_VALUE) {
983 expected_type = PTR_TO_STACK;
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700984 if (type != PTR_TO_PACKET && type != expected_type)
985 goto err_type;
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +0100986 } else if (arg_type == ARG_CONST_STACK_SIZE ||
987 arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700988 expected_type = CONST_IMM;
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700989 if (type != expected_type)
990 goto err_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700991 } else if (arg_type == ARG_CONST_MAP_PTR) {
992 expected_type = CONST_PTR_TO_MAP;
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700993 if (type != expected_type)
994 goto err_type;
Alexei Starovoitov608cd712015-03-26 19:53:57 -0700995 } else if (arg_type == ARG_PTR_TO_CTX) {
996 expected_type = PTR_TO_CTX;
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700997 if (type != expected_type)
998 goto err_type;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200999 } else if (arg_type == ARG_PTR_TO_STACK ||
1000 arg_type == ARG_PTR_TO_RAW_STACK) {
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001001 expected_type = PTR_TO_STACK;
1002 /* One exception here. In case function allows for NULL to be
1003 * passed in as argument, it's a CONST_IMM type. Final test
1004 * happens during stack boundary checking.
1005 */
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001006 if (type == CONST_IMM && reg->imm == 0)
1007 /* final test in check_stack_boundary() */;
1008 else if (type != PTR_TO_PACKET && type != expected_type)
1009 goto err_type;
Daniel Borkmann435faee12016-04-13 00:10:51 +02001010 meta->raw_mode = arg_type == ARG_PTR_TO_RAW_STACK;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001011 } else {
1012 verbose("unsupported arg_type %d\n", arg_type);
1013 return -EFAULT;
1014 }
1015
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001016 if (arg_type == ARG_CONST_MAP_PTR) {
1017 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001018 meta->map_ptr = reg->map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001019 } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
1020 /* bpf_map_xxx(..., map_ptr, ..., key) call:
1021 * check that [key, key + map->key_size) are within
1022 * stack limits and initialized
1023 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001024 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001025 /* in function declaration map_ptr must come before
1026 * map_key, so that it's verified and known before
1027 * we have to check map_key here. Otherwise it means
1028 * that kernel subsystem misconfigured verifier
1029 */
1030 verbose("invalid map_ptr to access map->key\n");
1031 return -EACCES;
1032 }
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001033 if (type == PTR_TO_PACKET)
1034 err = check_packet_access(env, regno, 0,
1035 meta->map_ptr->key_size);
1036 else
1037 err = check_stack_boundary(env, regno,
1038 meta->map_ptr->key_size,
1039 false, NULL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001040 } else if (arg_type == ARG_PTR_TO_MAP_VALUE) {
1041 /* bpf_map_xxx(..., map_ptr, ..., value) call:
1042 * check [value, value + map->value_size) validity
1043 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001044 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001045 /* kernel subsystem misconfigured verifier */
1046 verbose("invalid map_ptr to access map->value\n");
1047 return -EACCES;
1048 }
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001049 if (type == PTR_TO_PACKET)
1050 err = check_packet_access(env, regno, 0,
1051 meta->map_ptr->value_size);
1052 else
1053 err = check_stack_boundary(env, regno,
1054 meta->map_ptr->value_size,
1055 false, NULL);
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001056 } else if (arg_type == ARG_CONST_STACK_SIZE ||
1057 arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) {
1058 bool zero_size_allowed = (arg_type == ARG_CONST_STACK_SIZE_OR_ZERO);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001059
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001060 /* bpf_xxx(..., buf, len) call will access 'len' bytes
1061 * from stack pointer 'buf'. Check it
1062 * note: regno == len, regno - 1 == buf
1063 */
1064 if (regno == 0) {
1065 /* kernel subsystem misconfigured verifier */
1066 verbose("ARG_CONST_STACK_SIZE cannot be first argument\n");
1067 return -EACCES;
1068 }
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001069 if (regs[regno - 1].type == PTR_TO_PACKET)
1070 err = check_packet_access(env, regno - 1, 0, reg->imm);
1071 else
1072 err = check_stack_boundary(env, regno - 1, reg->imm,
1073 zero_size_allowed, meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001074 }
1075
1076 return err;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001077err_type:
1078 verbose("R%d type=%s expected=%s\n", regno,
1079 reg_type_str[type], reg_type_str[expected_type]);
1080 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001081}
1082
Kaixu Xia35578d72015-08-06 07:02:35 +00001083static int check_map_func_compatibility(struct bpf_map *map, int func_id)
1084{
Kaixu Xia35578d72015-08-06 07:02:35 +00001085 if (!map)
1086 return 0;
1087
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001088 /* We need a two way check, first is from map perspective ... */
1089 switch (map->map_type) {
1090 case BPF_MAP_TYPE_PROG_ARRAY:
1091 if (func_id != BPF_FUNC_tail_call)
1092 goto error;
1093 break;
1094 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
1095 if (func_id != BPF_FUNC_perf_event_read &&
1096 func_id != BPF_FUNC_perf_event_output)
1097 goto error;
1098 break;
1099 case BPF_MAP_TYPE_STACK_TRACE:
1100 if (func_id != BPF_FUNC_get_stackid)
1101 goto error;
1102 break;
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -07001103 case BPF_MAP_TYPE_CGROUP_ARRAY:
David S. Miller60747ef2016-08-18 01:17:32 -04001104 if (func_id != BPF_FUNC_skb_under_cgroup &&
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001105 func_id != BPF_FUNC_current_task_under_cgroup)
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07001106 goto error;
1107 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001108 default:
1109 break;
1110 }
1111
1112 /* ... and second from the function itself. */
1113 switch (func_id) {
1114 case BPF_FUNC_tail_call:
1115 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1116 goto error;
1117 break;
1118 case BPF_FUNC_perf_event_read:
1119 case BPF_FUNC_perf_event_output:
1120 if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
1121 goto error;
1122 break;
1123 case BPF_FUNC_get_stackid:
1124 if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
1125 goto error;
1126 break;
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001127 case BPF_FUNC_current_task_under_cgroup:
Daniel Borkmann747ea552016-08-12 22:17:17 +02001128 case BPF_FUNC_skb_under_cgroup:
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07001129 if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
1130 goto error;
1131 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001132 default:
1133 break;
Kaixu Xia35578d72015-08-06 07:02:35 +00001134 }
1135
1136 return 0;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001137error:
1138 verbose("cannot pass map_type %d into func %d\n",
1139 map->map_type, func_id);
1140 return -EINVAL;
Kaixu Xia35578d72015-08-06 07:02:35 +00001141}
1142
Daniel Borkmann435faee12016-04-13 00:10:51 +02001143static int check_raw_mode(const struct bpf_func_proto *fn)
1144{
1145 int count = 0;
1146
1147 if (fn->arg1_type == ARG_PTR_TO_RAW_STACK)
1148 count++;
1149 if (fn->arg2_type == ARG_PTR_TO_RAW_STACK)
1150 count++;
1151 if (fn->arg3_type == ARG_PTR_TO_RAW_STACK)
1152 count++;
1153 if (fn->arg4_type == ARG_PTR_TO_RAW_STACK)
1154 count++;
1155 if (fn->arg5_type == ARG_PTR_TO_RAW_STACK)
1156 count++;
1157
1158 return count > 1 ? -EINVAL : 0;
1159}
1160
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001161static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001162{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001163 struct bpf_verifier_state *state = &env->cur_state;
1164 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001165 int i;
1166
1167 for (i = 0; i < MAX_BPF_REG; i++)
1168 if (regs[i].type == PTR_TO_PACKET ||
1169 regs[i].type == PTR_TO_PACKET_END)
1170 mark_reg_unknown_value(regs, i);
1171
1172 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
1173 if (state->stack_slot_type[i] != STACK_SPILL)
1174 continue;
1175 reg = &state->spilled_regs[i / BPF_REG_SIZE];
1176 if (reg->type != PTR_TO_PACKET &&
1177 reg->type != PTR_TO_PACKET_END)
1178 continue;
1179 reg->type = UNKNOWN_VALUE;
1180 reg->imm = 0;
1181 }
1182}
1183
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001184static int check_call(struct bpf_verifier_env *env, int func_id)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001185{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001186 struct bpf_verifier_state *state = &env->cur_state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001187 const struct bpf_func_proto *fn = NULL;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001188 struct bpf_reg_state *regs = state->regs;
1189 struct bpf_reg_state *reg;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001190 struct bpf_call_arg_meta meta;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001191 bool changes_data;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001192 int i, err;
1193
1194 /* find function prototype */
1195 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
1196 verbose("invalid func %d\n", func_id);
1197 return -EINVAL;
1198 }
1199
1200 if (env->prog->aux->ops->get_func_proto)
1201 fn = env->prog->aux->ops->get_func_proto(func_id);
1202
1203 if (!fn) {
1204 verbose("unknown func %d\n", func_id);
1205 return -EINVAL;
1206 }
1207
1208 /* eBPF programs must be GPL compatible to use GPL-ed functions */
Daniel Borkmann24701ec2015-03-01 12:31:47 +01001209 if (!env->prog->gpl_compatible && fn->gpl_only) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001210 verbose("cannot call GPL only function from proprietary program\n");
1211 return -EINVAL;
1212 }
1213
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001214 changes_data = bpf_helper_changes_skb_data(fn->func);
1215
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001216 memset(&meta, 0, sizeof(meta));
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001217 meta.pkt_access = fn->pkt_access;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001218
Daniel Borkmann435faee12016-04-13 00:10:51 +02001219 /* We only support one arg being in raw mode at the moment, which
1220 * is sufficient for the helper functions we have right now.
1221 */
1222 err = check_raw_mode(fn);
1223 if (err) {
1224 verbose("kernel subsystem misconfigured func %d\n", func_id);
1225 return err;
1226 }
1227
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001228 /* check args */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001229 err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001230 if (err)
1231 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001232 err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001233 if (err)
1234 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001235 err = check_func_arg(env, BPF_REG_3, fn->arg3_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_4, fn->arg4_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_5, fn->arg5_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001242 if (err)
1243 return err;
1244
Daniel Borkmann435faee12016-04-13 00:10:51 +02001245 /* Mark slots with STACK_MISC in case of raw mode, stack offset
1246 * is inferred from register state.
1247 */
1248 for (i = 0; i < meta.access_size; i++) {
1249 err = check_mem_access(env, meta.regno, i, BPF_B, BPF_WRITE, -1);
1250 if (err)
1251 return err;
1252 }
1253
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001254 /* reset caller saved regs */
1255 for (i = 0; i < CALLER_SAVED_REGS; i++) {
1256 reg = regs + caller_saved[i];
1257 reg->type = NOT_INIT;
1258 reg->imm = 0;
1259 }
1260
1261 /* update return register */
1262 if (fn->ret_type == RET_INTEGER) {
1263 regs[BPF_REG_0].type = UNKNOWN_VALUE;
1264 } else if (fn->ret_type == RET_VOID) {
1265 regs[BPF_REG_0].type = NOT_INIT;
1266 } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) {
1267 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
Josef Bacik48461132016-09-28 10:54:32 -04001268 regs[BPF_REG_0].max_value = regs[BPF_REG_0].min_value = 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001269 /* remember map_ptr, so that check_map_access()
1270 * can check 'value_size' boundary of memory access
1271 * to map element returned from bpf_map_lookup_elem()
1272 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001273 if (meta.map_ptr == NULL) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001274 verbose("kernel subsystem misconfigured verifier\n");
1275 return -EINVAL;
1276 }
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001277 regs[BPF_REG_0].map_ptr = meta.map_ptr;
Thomas Graf14117072016-10-18 19:51:19 +02001278 regs[BPF_REG_0].id = ++env->id_gen;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001279 } else {
1280 verbose("unknown return type %d of func %d\n",
1281 fn->ret_type, func_id);
1282 return -EINVAL;
1283 }
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -07001284
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001285 err = check_map_func_compatibility(meta.map_ptr, func_id);
Kaixu Xia35578d72015-08-06 07:02:35 +00001286 if (err)
1287 return err;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -07001288
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001289 if (changes_data)
1290 clear_all_pkt_pointers(env);
1291 return 0;
1292}
1293
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001294static int check_packet_ptr_add(struct bpf_verifier_env *env,
1295 struct bpf_insn *insn)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001296{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001297 struct bpf_reg_state *regs = env->cur_state.regs;
1298 struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
1299 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
1300 struct bpf_reg_state tmp_reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001301 s32 imm;
1302
1303 if (BPF_SRC(insn->code) == BPF_K) {
1304 /* pkt_ptr += imm */
1305 imm = insn->imm;
1306
1307add_imm:
1308 if (imm <= 0) {
1309 verbose("addition of negative constant to packet pointer is not allowed\n");
1310 return -EACCES;
1311 }
1312 if (imm >= MAX_PACKET_OFF ||
1313 imm + dst_reg->off >= MAX_PACKET_OFF) {
1314 verbose("constant %d is too large to add to packet pointer\n",
1315 imm);
1316 return -EACCES;
1317 }
1318 /* a constant was added to pkt_ptr.
1319 * Remember it while keeping the same 'id'
1320 */
1321 dst_reg->off += imm;
1322 } else {
Alexei Starovoitov1b9b69e2016-05-19 18:17:14 -07001323 if (src_reg->type == PTR_TO_PACKET) {
1324 /* R6=pkt(id=0,off=0,r=62) R7=imm22; r7 += r6 */
1325 tmp_reg = *dst_reg; /* save r7 state */
1326 *dst_reg = *src_reg; /* copy pkt_ptr state r6 into r7 */
1327 src_reg = &tmp_reg; /* pretend it's src_reg state */
1328 /* if the checks below reject it, the copy won't matter,
1329 * since we're rejecting the whole program. If all ok,
1330 * then imm22 state will be added to r7
1331 * and r7 will be pkt(id=0,off=22,r=62) while
1332 * r6 will stay as pkt(id=0,off=0,r=62)
1333 */
1334 }
1335
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001336 if (src_reg->type == CONST_IMM) {
1337 /* pkt_ptr += reg where reg is known constant */
1338 imm = src_reg->imm;
1339 goto add_imm;
1340 }
1341 /* disallow pkt_ptr += reg
1342 * if reg is not uknown_value with guaranteed zero upper bits
1343 * otherwise pkt_ptr may overflow and addition will become
1344 * subtraction which is not allowed
1345 */
1346 if (src_reg->type != UNKNOWN_VALUE) {
1347 verbose("cannot add '%s' to ptr_to_packet\n",
1348 reg_type_str[src_reg->type]);
1349 return -EACCES;
1350 }
1351 if (src_reg->imm < 48) {
1352 verbose("cannot add integer value with %lld upper zero bits to ptr_to_packet\n",
1353 src_reg->imm);
1354 return -EACCES;
1355 }
1356 /* dst_reg stays as pkt_ptr type and since some positive
1357 * integer value was added to the pointer, increment its 'id'
1358 */
Jakub Kicinski1f415a72016-08-02 16:12:14 +01001359 dst_reg->id = ++env->id_gen;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001360
1361 /* something was added to pkt_ptr, set range and off to zero */
1362 dst_reg->off = 0;
1363 dst_reg->range = 0;
1364 }
1365 return 0;
1366}
1367
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001368static int evaluate_reg_alu(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001369{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001370 struct bpf_reg_state *regs = env->cur_state.regs;
1371 struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001372 u8 opcode = BPF_OP(insn->code);
1373 s64 imm_log2;
1374
1375 /* for type == UNKNOWN_VALUE:
1376 * imm > 0 -> number of zero upper bits
1377 * imm == 0 -> don't track which is the same as all bits can be non-zero
1378 */
1379
1380 if (BPF_SRC(insn->code) == BPF_X) {
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001381 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001382
1383 if (src_reg->type == UNKNOWN_VALUE && src_reg->imm > 0 &&
1384 dst_reg->imm && opcode == BPF_ADD) {
1385 /* dreg += sreg
1386 * where both have zero upper bits. Adding them
1387 * can only result making one more bit non-zero
1388 * in the larger value.
1389 * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47)
1390 * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47)
1391 */
1392 dst_reg->imm = min(dst_reg->imm, src_reg->imm);
1393 dst_reg->imm--;
1394 return 0;
1395 }
1396 if (src_reg->type == CONST_IMM && src_reg->imm > 0 &&
1397 dst_reg->imm && opcode == BPF_ADD) {
1398 /* dreg += sreg
1399 * where dreg has zero upper bits and sreg is const.
1400 * Adding them can only result making one more bit
1401 * non-zero in the larger value.
1402 */
1403 imm_log2 = __ilog2_u64((long long)src_reg->imm);
1404 dst_reg->imm = min(dst_reg->imm, 63 - imm_log2);
1405 dst_reg->imm--;
1406 return 0;
1407 }
1408 /* all other cases non supported yet, just mark dst_reg */
1409 dst_reg->imm = 0;
1410 return 0;
1411 }
1412
1413 /* sign extend 32-bit imm into 64-bit to make sure that
1414 * negative values occupy bit 63. Note ilog2() would have
1415 * been incorrect, since sizeof(insn->imm) == 4
1416 */
1417 imm_log2 = __ilog2_u64((long long)insn->imm);
1418
1419 if (dst_reg->imm && opcode == BPF_LSH) {
1420 /* reg <<= imm
1421 * if reg was a result of 2 byte load, then its imm == 48
1422 * which means that upper 48 bits are zero and shifting this reg
1423 * left by 4 would mean that upper 44 bits are still zero
1424 */
1425 dst_reg->imm -= insn->imm;
1426 } else if (dst_reg->imm && opcode == BPF_MUL) {
1427 /* reg *= imm
1428 * if multiplying by 14 subtract 4
1429 * This is conservative calculation of upper zero bits.
1430 * It's not trying to special case insn->imm == 1 or 0 cases
1431 */
1432 dst_reg->imm -= imm_log2 + 1;
1433 } else if (opcode == BPF_AND) {
1434 /* reg &= imm */
1435 dst_reg->imm = 63 - imm_log2;
1436 } else if (dst_reg->imm && opcode == BPF_ADD) {
1437 /* reg += imm */
1438 dst_reg->imm = min(dst_reg->imm, 63 - imm_log2);
1439 dst_reg->imm--;
1440 } else if (opcode == BPF_RSH) {
1441 /* reg >>= imm
1442 * which means that after right shift, upper bits will be zero
1443 * note that verifier already checked that
1444 * 0 <= imm < 64 for shift insn
1445 */
1446 dst_reg->imm += insn->imm;
1447 if (unlikely(dst_reg->imm > 64))
1448 /* some dumb code did:
1449 * r2 = *(u32 *)mem;
1450 * r2 >>= 32;
1451 * and all bits are zero now */
1452 dst_reg->imm = 64;
1453 } else {
1454 /* all other alu ops, means that we don't know what will
1455 * happen to the value, mark it with unknown number of zero bits
1456 */
1457 dst_reg->imm = 0;
1458 }
1459
1460 if (dst_reg->imm < 0) {
1461 /* all 64 bits of the register can contain non-zero bits
1462 * and such value cannot be added to ptr_to_packet, since it
1463 * may overflow, mark it as unknown to avoid further eval
1464 */
1465 dst_reg->imm = 0;
1466 }
1467 return 0;
1468}
1469
John Fastabende37bdee2017-07-02 02:13:30 +02001470static int evaluate_reg_imm_alu_unknown(struct bpf_verifier_env *env,
1471 struct bpf_insn *insn)
1472{
1473 struct bpf_reg_state *regs = env->cur_state.regs;
1474 struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
1475 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
1476 u8 opcode = BPF_OP(insn->code);
1477 s64 imm_log2 = __ilog2_u64((long long)dst_reg->imm);
1478
1479 /* BPF_X code with src_reg->type UNKNOWN_VALUE here. */
1480 if (src_reg->imm > 0 && dst_reg->imm) {
1481 switch (opcode) {
1482 case BPF_ADD:
1483 /* dreg += sreg
1484 * where both have zero upper bits. Adding them
1485 * can only result making one more bit non-zero
1486 * in the larger value.
1487 * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47)
1488 * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47)
1489 */
1490 dst_reg->imm = min(src_reg->imm, 63 - imm_log2);
1491 dst_reg->imm--;
1492 break;
1493 case BPF_AND:
1494 /* dreg &= sreg
1495 * AND can not extend zero bits only shrink
1496 * Ex. 0x00..00ffffff
1497 * & 0x0f..ffffffff
1498 * ----------------
1499 * 0x00..00ffffff
1500 */
1501 dst_reg->imm = max(src_reg->imm, 63 - imm_log2);
1502 break;
1503 case BPF_OR:
1504 /* dreg |= sreg
1505 * OR can only extend zero bits
1506 * Ex. 0x00..00ffffff
1507 * | 0x0f..ffffffff
1508 * ----------------
1509 * 0x0f..00ffffff
1510 */
1511 dst_reg->imm = min(src_reg->imm, 63 - imm_log2);
1512 break;
1513 case BPF_SUB:
1514 case BPF_MUL:
1515 case BPF_RSH:
1516 case BPF_LSH:
1517 /* These may be flushed out later */
1518 default:
1519 mark_reg_unknown_value(regs, insn->dst_reg);
1520 }
1521 } else {
1522 mark_reg_unknown_value(regs, insn->dst_reg);
1523 }
1524
1525 dst_reg->type = UNKNOWN_VALUE;
1526 return 0;
1527}
1528
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001529static int evaluate_reg_imm_alu(struct bpf_verifier_env *env,
1530 struct bpf_insn *insn)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001531{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001532 struct bpf_reg_state *regs = env->cur_state.regs;
1533 struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
1534 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001535 u8 opcode = BPF_OP(insn->code);
1536
John Fastabende37bdee2017-07-02 02:13:30 +02001537 if (BPF_SRC(insn->code) == BPF_X && src_reg->type == UNKNOWN_VALUE)
1538 return evaluate_reg_imm_alu_unknown(env, insn);
1539
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001540 /* dst_reg->type == CONST_IMM here, simulate execution of 'add' insn.
1541 * Don't care about overflow or negative values, just add them
1542 */
1543 if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_K)
1544 dst_reg->imm += insn->imm;
1545 else if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_X &&
1546 src_reg->type == CONST_IMM)
1547 dst_reg->imm += src_reg->imm;
1548 else
1549 mark_reg_unknown_value(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001550 return 0;
1551}
1552
Josef Bacik48461132016-09-28 10:54:32 -04001553static void check_reg_overflow(struct bpf_reg_state *reg)
1554{
1555 if (reg->max_value > BPF_REGISTER_MAX_RANGE)
1556 reg->max_value = BPF_REGISTER_MAX_RANGE;
Josef Bacikf23cc642016-11-14 15:45:36 -05001557 if (reg->min_value < BPF_REGISTER_MIN_RANGE ||
1558 reg->min_value > BPF_REGISTER_MAX_RANGE)
Josef Bacik48461132016-09-28 10:54:32 -04001559 reg->min_value = BPF_REGISTER_MIN_RANGE;
1560}
1561
1562static void adjust_reg_min_max_vals(struct bpf_verifier_env *env,
1563 struct bpf_insn *insn)
1564{
1565 struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg;
Josef Bacikf23cc642016-11-14 15:45:36 -05001566 s64 min_val = BPF_REGISTER_MIN_RANGE;
1567 u64 max_val = BPF_REGISTER_MAX_RANGE;
Josef Bacik48461132016-09-28 10:54:32 -04001568 bool min_set = false, max_set = false;
1569 u8 opcode = BPF_OP(insn->code);
1570
1571 dst_reg = &regs[insn->dst_reg];
1572 if (BPF_SRC(insn->code) == BPF_X) {
1573 check_reg_overflow(&regs[insn->src_reg]);
1574 min_val = regs[insn->src_reg].min_value;
1575 max_val = regs[insn->src_reg].max_value;
1576
1577 /* If the source register is a random pointer then the
1578 * min_value/max_value values represent the range of the known
1579 * accesses into that value, not the actual min/max value of the
1580 * register itself. In this case we have to reset the reg range
1581 * values so we know it is not safe to look at.
1582 */
1583 if (regs[insn->src_reg].type != CONST_IMM &&
1584 regs[insn->src_reg].type != UNKNOWN_VALUE) {
1585 min_val = BPF_REGISTER_MIN_RANGE;
1586 max_val = BPF_REGISTER_MAX_RANGE;
1587 }
1588 } else if (insn->imm < BPF_REGISTER_MAX_RANGE &&
1589 (s64)insn->imm > BPF_REGISTER_MIN_RANGE) {
1590 min_val = max_val = insn->imm;
1591 min_set = max_set = true;
1592 }
1593
1594 /* We don't know anything about what was done to this register, mark it
1595 * as unknown.
1596 */
1597 if (min_val == BPF_REGISTER_MIN_RANGE &&
1598 max_val == BPF_REGISTER_MAX_RANGE) {
1599 reset_reg_range_values(regs, insn->dst_reg);
1600 return;
1601 }
1602
Josef Bacikf23cc642016-11-14 15:45:36 -05001603 /* If one of our values was at the end of our ranges then we can't just
1604 * do our normal operations to the register, we need to set the values
1605 * to the min/max since they are undefined.
1606 */
1607 if (min_val == BPF_REGISTER_MIN_RANGE)
1608 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
1609 if (max_val == BPF_REGISTER_MAX_RANGE)
1610 dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
1611
Josef Bacik48461132016-09-28 10:54:32 -04001612 switch (opcode) {
1613 case BPF_ADD:
Josef Bacikf23cc642016-11-14 15:45:36 -05001614 if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
1615 dst_reg->min_value += min_val;
1616 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1617 dst_reg->max_value += max_val;
Josef Bacik48461132016-09-28 10:54:32 -04001618 break;
1619 case BPF_SUB:
Josef Bacikf23cc642016-11-14 15:45:36 -05001620 if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
1621 dst_reg->min_value -= min_val;
1622 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1623 dst_reg->max_value -= max_val;
Josef Bacik48461132016-09-28 10:54:32 -04001624 break;
1625 case BPF_MUL:
Josef Bacikf23cc642016-11-14 15:45:36 -05001626 if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
1627 dst_reg->min_value *= min_val;
1628 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1629 dst_reg->max_value *= max_val;
Josef Bacik48461132016-09-28 10:54:32 -04001630 break;
1631 case BPF_AND:
Josef Bacikf23cc642016-11-14 15:45:36 -05001632 /* Disallow AND'ing of negative numbers, ain't nobody got time
1633 * for that. Otherwise the minimum is 0 and the max is the max
1634 * value we could AND against.
1635 */
1636 if (min_val < 0)
1637 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
1638 else
1639 dst_reg->min_value = 0;
Josef Bacik48461132016-09-28 10:54:32 -04001640 dst_reg->max_value = max_val;
1641 break;
1642 case BPF_LSH:
1643 /* Gotta have special overflow logic here, if we're shifting
1644 * more than MAX_RANGE then just assume we have an invalid
1645 * range.
1646 */
1647 if (min_val > ilog2(BPF_REGISTER_MAX_RANGE))
1648 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
Josef Bacikf23cc642016-11-14 15:45:36 -05001649 else if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
Josef Bacik48461132016-09-28 10:54:32 -04001650 dst_reg->min_value <<= min_val;
1651
1652 if (max_val > ilog2(BPF_REGISTER_MAX_RANGE))
1653 dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
Josef Bacikf23cc642016-11-14 15:45:36 -05001654 else if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
Josef Bacik48461132016-09-28 10:54:32 -04001655 dst_reg->max_value <<= max_val;
1656 break;
1657 case BPF_RSH:
Josef Bacikf23cc642016-11-14 15:45:36 -05001658 /* RSH by a negative number is undefined, and the BPF_RSH is an
1659 * unsigned shift, so make the appropriate casts.
Josef Bacik48461132016-09-28 10:54:32 -04001660 */
Josef Bacikf23cc642016-11-14 15:45:36 -05001661 if (min_val < 0 || dst_reg->min_value < 0)
1662 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
1663 else
1664 dst_reg->min_value =
1665 (u64)(dst_reg->min_value) >> min_val;
1666 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1667 dst_reg->max_value >>= max_val;
Josef Bacik48461132016-09-28 10:54:32 -04001668 break;
1669 default:
1670 reset_reg_range_values(regs, insn->dst_reg);
1671 break;
1672 }
1673
1674 check_reg_overflow(dst_reg);
1675}
1676
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001677/* check validity of 32-bit and 64-bit arithmetic operations */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001678static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001679{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001680 struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001681 u8 opcode = BPF_OP(insn->code);
1682 int err;
1683
1684 if (opcode == BPF_END || opcode == BPF_NEG) {
1685 if (opcode == BPF_NEG) {
1686 if (BPF_SRC(insn->code) != 0 ||
1687 insn->src_reg != BPF_REG_0 ||
1688 insn->off != 0 || insn->imm != 0) {
1689 verbose("BPF_NEG uses reserved fields\n");
1690 return -EINVAL;
1691 }
1692 } else {
1693 if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
1694 (insn->imm != 16 && insn->imm != 32 && insn->imm != 64)) {
1695 verbose("BPF_END uses reserved fields\n");
1696 return -EINVAL;
1697 }
1698 }
1699
1700 /* check src operand */
1701 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
1702 if (err)
1703 return err;
1704
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001705 if (is_pointer_value(env, insn->dst_reg)) {
1706 verbose("R%d pointer arithmetic prohibited\n",
1707 insn->dst_reg);
1708 return -EACCES;
1709 }
1710
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001711 /* check dest operand */
1712 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
1713 if (err)
1714 return err;
1715
1716 } else if (opcode == BPF_MOV) {
1717
1718 if (BPF_SRC(insn->code) == BPF_X) {
1719 if (insn->imm != 0 || insn->off != 0) {
1720 verbose("BPF_MOV uses reserved fields\n");
1721 return -EINVAL;
1722 }
1723
1724 /* check src operand */
1725 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1726 if (err)
1727 return err;
1728 } else {
1729 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
1730 verbose("BPF_MOV uses reserved fields\n");
1731 return -EINVAL;
1732 }
1733 }
1734
1735 /* check dest operand */
1736 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
1737 if (err)
1738 return err;
1739
Josef Bacik48461132016-09-28 10:54:32 -04001740 /* we are setting our register to something new, we need to
1741 * reset its range values.
1742 */
1743 reset_reg_range_values(regs, insn->dst_reg);
1744
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001745 if (BPF_SRC(insn->code) == BPF_X) {
1746 if (BPF_CLASS(insn->code) == BPF_ALU64) {
1747 /* case: R1 = R2
1748 * copy register state to dest reg
1749 */
1750 regs[insn->dst_reg] = regs[insn->src_reg];
1751 } else {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001752 if (is_pointer_value(env, insn->src_reg)) {
1753 verbose("R%d partial copy of pointer\n",
1754 insn->src_reg);
1755 return -EACCES;
1756 }
Thomas Graf14117072016-10-18 19:51:19 +02001757 mark_reg_unknown_value(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001758 }
1759 } else {
1760 /* case: R = imm
1761 * remember the value we stored into this reg
1762 */
1763 regs[insn->dst_reg].type = CONST_IMM;
1764 regs[insn->dst_reg].imm = insn->imm;
Josef Bacik48461132016-09-28 10:54:32 -04001765 regs[insn->dst_reg].max_value = insn->imm;
1766 regs[insn->dst_reg].min_value = insn->imm;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001767 }
1768
1769 } else if (opcode > BPF_END) {
1770 verbose("invalid BPF_ALU opcode %x\n", opcode);
1771 return -EINVAL;
1772
1773 } else { /* all other ALU ops: and, sub, xor, add, ... */
1774
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001775 if (BPF_SRC(insn->code) == BPF_X) {
1776 if (insn->imm != 0 || insn->off != 0) {
1777 verbose("BPF_ALU uses reserved fields\n");
1778 return -EINVAL;
1779 }
1780 /* check src1 operand */
1781 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1782 if (err)
1783 return err;
1784 } else {
1785 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
1786 verbose("BPF_ALU uses reserved fields\n");
1787 return -EINVAL;
1788 }
1789 }
1790
1791 /* check src2 operand */
1792 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
1793 if (err)
1794 return err;
1795
1796 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
1797 BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
1798 verbose("div by zero\n");
1799 return -EINVAL;
1800 }
1801
Rabin Vincent229394e2016-01-12 20:17:08 +01001802 if ((opcode == BPF_LSH || opcode == BPF_RSH ||
1803 opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
1804 int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
1805
1806 if (insn->imm < 0 || insn->imm >= size) {
1807 verbose("invalid shift %d\n", insn->imm);
1808 return -EINVAL;
1809 }
1810 }
1811
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07001812 /* check dest operand */
1813 err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
1814 if (err)
1815 return err;
1816
1817 dst_reg = &regs[insn->dst_reg];
1818
Josef Bacik48461132016-09-28 10:54:32 -04001819 /* first we want to adjust our ranges. */
1820 adjust_reg_min_max_vals(env, insn);
1821
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001822 /* pattern match 'bpf_add Rx, imm' instruction */
1823 if (opcode == BPF_ADD && BPF_CLASS(insn->code) == BPF_ALU64 &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07001824 dst_reg->type == FRAME_PTR && BPF_SRC(insn->code) == BPF_K) {
1825 dst_reg->type = PTR_TO_STACK;
1826 dst_reg->imm = insn->imm;
1827 return 0;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001828 } else if (opcode == BPF_ADD &&
1829 BPF_CLASS(insn->code) == BPF_ALU64 &&
Yonghong Song7bca0a92017-04-29 22:52:42 -07001830 dst_reg->type == PTR_TO_STACK &&
1831 ((BPF_SRC(insn->code) == BPF_X &&
1832 regs[insn->src_reg].type == CONST_IMM) ||
1833 BPF_SRC(insn->code) == BPF_K)) {
1834 if (BPF_SRC(insn->code) == BPF_X)
1835 dst_reg->imm += regs[insn->src_reg].imm;
1836 else
1837 dst_reg->imm += insn->imm;
1838 return 0;
1839 } else if (opcode == BPF_ADD &&
1840 BPF_CLASS(insn->code) == BPF_ALU64 &&
Alexei Starovoitov1b9b69e2016-05-19 18:17:14 -07001841 (dst_reg->type == PTR_TO_PACKET ||
1842 (BPF_SRC(insn->code) == BPF_X &&
1843 regs[insn->src_reg].type == PTR_TO_PACKET))) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001844 /* ptr_to_packet += K|X */
1845 return check_packet_ptr_add(env, insn);
1846 } else if (BPF_CLASS(insn->code) == BPF_ALU64 &&
1847 dst_reg->type == UNKNOWN_VALUE &&
1848 env->allow_ptr_leaks) {
1849 /* unknown += K|X */
1850 return evaluate_reg_alu(env, insn);
1851 } else if (BPF_CLASS(insn->code) == BPF_ALU64 &&
1852 dst_reg->type == CONST_IMM &&
1853 env->allow_ptr_leaks) {
1854 /* reg_imm += K|X */
1855 return evaluate_reg_imm_alu(env, insn);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001856 } else if (is_pointer_value(env, insn->dst_reg)) {
1857 verbose("R%d pointer arithmetic prohibited\n",
1858 insn->dst_reg);
1859 return -EACCES;
1860 } else if (BPF_SRC(insn->code) == BPF_X &&
1861 is_pointer_value(env, insn->src_reg)) {
1862 verbose("R%d pointer arithmetic prohibited\n",
1863 insn->src_reg);
1864 return -EACCES;
1865 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001866
Josef Bacik48461132016-09-28 10:54:32 -04001867 /* If we did pointer math on a map value then just set it to our
1868 * PTR_TO_MAP_VALUE_ADJ type so we can deal with any stores or
1869 * loads to this register appropriately, otherwise just mark the
1870 * register as unknown.
1871 */
1872 if (env->allow_ptr_leaks &&
1873 (dst_reg->type == PTR_TO_MAP_VALUE ||
1874 dst_reg->type == PTR_TO_MAP_VALUE_ADJ))
1875 dst_reg->type = PTR_TO_MAP_VALUE_ADJ;
1876 else
1877 mark_reg_unknown_value(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001878 }
1879
1880 return 0;
1881}
1882
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001883static void find_good_pkt_pointers(struct bpf_verifier_state *state,
1884 struct bpf_reg_state *dst_reg)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001885{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001886 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001887 int i;
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02001888
1889 /* LLVM can generate two kind of checks:
1890 *
1891 * Type 1:
1892 *
1893 * r2 = r3;
1894 * r2 += 8;
1895 * if (r2 > pkt_end) goto <handle exception>
1896 * <access okay>
1897 *
1898 * Where:
1899 * r2 == dst_reg, pkt_end == src_reg
1900 * r2=pkt(id=n,off=8,r=0)
1901 * r3=pkt(id=n,off=0,r=0)
1902 *
1903 * Type 2:
1904 *
1905 * r2 = r3;
1906 * r2 += 8;
1907 * if (pkt_end >= r2) goto <access okay>
1908 * <handle exception>
1909 *
1910 * Where:
1911 * pkt_end == dst_reg, r2 == src_reg
1912 * r2=pkt(id=n,off=8,r=0)
1913 * r3=pkt(id=n,off=0,r=0)
1914 *
1915 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
1916 * so that range of bytes [r3, r3 + 8) is safe to access.
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001917 */
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02001918
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001919 for (i = 0; i < MAX_BPF_REG; i++)
1920 if (regs[i].type == PTR_TO_PACKET && regs[i].id == dst_reg->id)
Alexei Starovoitov0ea3c232017-03-24 15:57:33 -07001921 /* keep the maximum range already checked */
1922 regs[i].range = max(regs[i].range, dst_reg->off);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001923
1924 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
1925 if (state->stack_slot_type[i] != STACK_SPILL)
1926 continue;
1927 reg = &state->spilled_regs[i / BPF_REG_SIZE];
1928 if (reg->type == PTR_TO_PACKET && reg->id == dst_reg->id)
Alexei Starovoitov0ea3c232017-03-24 15:57:33 -07001929 reg->range = max(reg->range, dst_reg->off);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001930 }
1931}
1932
Josef Bacik48461132016-09-28 10:54:32 -04001933/* Adjusts the register min/max values in the case that the dst_reg is the
1934 * variable register that we are working on, and src_reg is a constant or we're
1935 * simply doing a BPF_K check.
1936 */
1937static void reg_set_min_max(struct bpf_reg_state *true_reg,
1938 struct bpf_reg_state *false_reg, u64 val,
1939 u8 opcode)
1940{
1941 switch (opcode) {
1942 case BPF_JEQ:
1943 /* If this is false then we know nothing Jon Snow, but if it is
1944 * true then we know for sure.
1945 */
1946 true_reg->max_value = true_reg->min_value = val;
1947 break;
1948 case BPF_JNE:
1949 /* If this is true we know nothing Jon Snow, but if it is false
1950 * we know the value for sure;
1951 */
1952 false_reg->max_value = false_reg->min_value = val;
1953 break;
1954 case BPF_JGT:
1955 /* Unsigned comparison, the minimum value is 0. */
1956 false_reg->min_value = 0;
1957 case BPF_JSGT:
1958 /* If this is false then we know the maximum val is val,
1959 * otherwise we know the min val is val+1.
1960 */
1961 false_reg->max_value = val;
1962 true_reg->min_value = val + 1;
1963 break;
1964 case BPF_JGE:
1965 /* Unsigned comparison, the minimum value is 0. */
1966 false_reg->min_value = 0;
1967 case BPF_JSGE:
1968 /* If this is false then we know the maximum value is val - 1,
1969 * otherwise we know the mimimum value is val.
1970 */
1971 false_reg->max_value = val - 1;
1972 true_reg->min_value = val;
1973 break;
1974 default:
1975 break;
1976 }
1977
1978 check_reg_overflow(false_reg);
1979 check_reg_overflow(true_reg);
1980}
1981
1982/* Same as above, but for the case that dst_reg is a CONST_IMM reg and src_reg
1983 * is the variable reg.
1984 */
1985static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
1986 struct bpf_reg_state *false_reg, u64 val,
1987 u8 opcode)
1988{
1989 switch (opcode) {
1990 case BPF_JEQ:
1991 /* If this is false then we know nothing Jon Snow, but if it is
1992 * true then we know for sure.
1993 */
1994 true_reg->max_value = true_reg->min_value = val;
1995 break;
1996 case BPF_JNE:
1997 /* If this is true we know nothing Jon Snow, but if it is false
1998 * we know the value for sure;
1999 */
2000 false_reg->max_value = false_reg->min_value = val;
2001 break;
2002 case BPF_JGT:
2003 /* Unsigned comparison, the minimum value is 0. */
2004 true_reg->min_value = 0;
2005 case BPF_JSGT:
2006 /*
2007 * If this is false, then the val is <= the register, if it is
2008 * true the register <= to the val.
2009 */
2010 false_reg->min_value = val;
2011 true_reg->max_value = val - 1;
2012 break;
2013 case BPF_JGE:
2014 /* Unsigned comparison, the minimum value is 0. */
2015 true_reg->min_value = 0;
2016 case BPF_JSGE:
2017 /* If this is false then constant < register, if it is true then
2018 * the register < constant.
2019 */
2020 false_reg->min_value = val + 1;
2021 true_reg->max_value = val;
2022 break;
2023 default:
2024 break;
2025 }
2026
2027 check_reg_overflow(false_reg);
2028 check_reg_overflow(true_reg);
2029}
2030
Thomas Graf14117072016-10-18 19:51:19 +02002031static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id,
2032 enum bpf_reg_type type)
2033{
2034 struct bpf_reg_state *reg = &regs[regno];
2035
2036 if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) {
2037 reg->type = type;
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01002038 /* We don't need id from this point onwards anymore, thus we
2039 * should better reset it, so that state pruning has chances
2040 * to take effect.
2041 */
2042 reg->id = 0;
Thomas Graf14117072016-10-18 19:51:19 +02002043 if (type == UNKNOWN_VALUE)
Daniel Borkmann0e0f1d62016-12-18 01:52:59 +01002044 __mark_reg_unknown_value(regs, regno);
Thomas Graf14117072016-10-18 19:51:19 +02002045 }
2046}
2047
2048/* The logic is similar to find_good_pkt_pointers(), both could eventually
2049 * be folded together at some point.
2050 */
2051static void mark_map_regs(struct bpf_verifier_state *state, u32 regno,
2052 enum bpf_reg_type type)
2053{
2054 struct bpf_reg_state *regs = state->regs;
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01002055 u32 id = regs[regno].id;
Thomas Graf14117072016-10-18 19:51:19 +02002056 int i;
2057
2058 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01002059 mark_map_reg(regs, i, id, type);
Thomas Graf14117072016-10-18 19:51:19 +02002060
2061 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
2062 if (state->stack_slot_type[i] != STACK_SPILL)
2063 continue;
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01002064 mark_map_reg(state->spilled_regs, i / BPF_REG_SIZE, id, type);
Thomas Graf14117072016-10-18 19:51:19 +02002065 }
2066}
2067
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002068static int check_cond_jmp_op(struct bpf_verifier_env *env,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002069 struct bpf_insn *insn, int *insn_idx)
2070{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002071 struct bpf_verifier_state *other_branch, *this_branch = &env->cur_state;
2072 struct bpf_reg_state *regs = this_branch->regs, *dst_reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002073 u8 opcode = BPF_OP(insn->code);
2074 int err;
2075
2076 if (opcode > BPF_EXIT) {
2077 verbose("invalid BPF_JMP opcode %x\n", opcode);
2078 return -EINVAL;
2079 }
2080
2081 if (BPF_SRC(insn->code) == BPF_X) {
2082 if (insn->imm != 0) {
2083 verbose("BPF_JMP uses reserved fields\n");
2084 return -EINVAL;
2085 }
2086
2087 /* check src1 operand */
2088 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2089 if (err)
2090 return err;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002091
2092 if (is_pointer_value(env, insn->src_reg)) {
2093 verbose("R%d pointer comparison prohibited\n",
2094 insn->src_reg);
2095 return -EACCES;
2096 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002097 } else {
2098 if (insn->src_reg != BPF_REG_0) {
2099 verbose("BPF_JMP uses reserved fields\n");
2100 return -EINVAL;
2101 }
2102 }
2103
2104 /* check src2 operand */
2105 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
2106 if (err)
2107 return err;
2108
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002109 dst_reg = &regs[insn->dst_reg];
2110
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002111 /* detect if R == 0 where R was initialized to zero earlier */
2112 if (BPF_SRC(insn->code) == BPF_K &&
2113 (opcode == BPF_JEQ || opcode == BPF_JNE) &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002114 dst_reg->type == CONST_IMM && dst_reg->imm == insn->imm) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002115 if (opcode == BPF_JEQ) {
2116 /* if (imm == imm) goto pc+off;
2117 * only follow the goto, ignore fall-through
2118 */
2119 *insn_idx += insn->off;
2120 return 0;
2121 } else {
2122 /* if (imm != imm) goto pc+off;
2123 * only follow fall-through branch, since
2124 * that's where the program will go
2125 */
2126 return 0;
2127 }
2128 }
2129
2130 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx);
2131 if (!other_branch)
2132 return -EFAULT;
2133
Josef Bacik48461132016-09-28 10:54:32 -04002134 /* detect if we are comparing against a constant value so we can adjust
2135 * our min/max values for our dst register.
2136 */
2137 if (BPF_SRC(insn->code) == BPF_X) {
2138 if (regs[insn->src_reg].type == CONST_IMM)
2139 reg_set_min_max(&other_branch->regs[insn->dst_reg],
2140 dst_reg, regs[insn->src_reg].imm,
2141 opcode);
2142 else if (dst_reg->type == CONST_IMM)
2143 reg_set_min_max_inv(&other_branch->regs[insn->src_reg],
2144 &regs[insn->src_reg], dst_reg->imm,
2145 opcode);
2146 } else {
2147 reg_set_min_max(&other_branch->regs[insn->dst_reg],
2148 dst_reg, insn->imm, opcode);
2149 }
2150
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002151 /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002152 if (BPF_SRC(insn->code) == BPF_K &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002153 insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
2154 dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
Thomas Graf14117072016-10-18 19:51:19 +02002155 /* Mark all identical map registers in each branch as either
2156 * safe or unknown depending R == 0 or R != 0 conditional.
2157 */
2158 mark_map_regs(this_branch, insn->dst_reg,
2159 opcode == BPF_JEQ ? PTR_TO_MAP_VALUE : UNKNOWN_VALUE);
2160 mark_map_regs(other_branch, insn->dst_reg,
2161 opcode == BPF_JEQ ? UNKNOWN_VALUE : PTR_TO_MAP_VALUE);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002162 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT &&
2163 dst_reg->type == PTR_TO_PACKET &&
2164 regs[insn->src_reg].type == PTR_TO_PACKET_END) {
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002165 find_good_pkt_pointers(this_branch, dst_reg);
2166 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGE &&
2167 dst_reg->type == PTR_TO_PACKET_END &&
2168 regs[insn->src_reg].type == PTR_TO_PACKET) {
2169 find_good_pkt_pointers(other_branch, &regs[insn->src_reg]);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002170 } else if (is_pointer_value(env, insn->dst_reg)) {
2171 verbose("R%d pointer comparison prohibited\n", insn->dst_reg);
2172 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002173 }
2174 if (log_level)
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002175 print_verifier_state(this_branch);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002176 return 0;
2177}
2178
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002179/* return the map pointer stored inside BPF_LD_IMM64 instruction */
2180static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
2181{
2182 u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
2183
2184 return (struct bpf_map *) (unsigned long) imm64;
2185}
2186
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002187/* verify BPF_LD_IMM64 instruction */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002188static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002189{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002190 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002191 int err;
2192
2193 if (BPF_SIZE(insn->code) != BPF_DW) {
2194 verbose("invalid BPF_LD_IMM insn\n");
2195 return -EINVAL;
2196 }
2197 if (insn->off != 0) {
2198 verbose("BPF_LD_IMM64 uses reserved fields\n");
2199 return -EINVAL;
2200 }
2201
2202 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
2203 if (err)
2204 return err;
2205
Jakub Kicinski6b173872016-09-21 11:43:59 +01002206 if (insn->src_reg == 0) {
2207 /* generic move 64-bit immediate into a register,
2208 * only analyzer needs to collect the ld_imm value.
2209 */
2210 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
2211
2212 if (!env->analyzer_ops)
2213 return 0;
2214
2215 regs[insn->dst_reg].type = CONST_IMM;
2216 regs[insn->dst_reg].imm = imm;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002217 return 0;
Jakub Kicinski6b173872016-09-21 11:43:59 +01002218 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002219
2220 /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
2221 BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
2222
2223 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
2224 regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
2225 return 0;
2226}
2227
Daniel Borkmann96be4322015-03-01 12:31:46 +01002228static bool may_access_skb(enum bpf_prog_type type)
2229{
2230 switch (type) {
2231 case BPF_PROG_TYPE_SOCKET_FILTER:
2232 case BPF_PROG_TYPE_SCHED_CLS:
Daniel Borkmann94caee82015-03-20 15:11:11 +01002233 case BPF_PROG_TYPE_SCHED_ACT:
Daniel Borkmann96be4322015-03-01 12:31:46 +01002234 return true;
2235 default:
2236 return false;
2237 }
2238}
2239
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002240/* verify safety of LD_ABS|LD_IND instructions:
2241 * - they can only appear in the programs where ctx == skb
2242 * - since they are wrappers of function calls, they scratch R1-R5 registers,
2243 * preserve R6-R9, and store return value into R0
2244 *
2245 * Implicit input:
2246 * ctx == skb == R6 == CTX
2247 *
2248 * Explicit input:
2249 * SRC == any register
2250 * IMM == 32-bit immediate
2251 *
2252 * Output:
2253 * R0 - 8/16/32-bit skb data converted to cpu endianness
2254 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002255static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002256{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002257 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002258 u8 mode = BPF_MODE(insn->code);
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002259 struct bpf_reg_state *reg;
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002260 int i, err;
2261
Daniel Borkmann24701ec2015-03-01 12:31:47 +01002262 if (!may_access_skb(env->prog->type)) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002263 verbose("BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002264 return -EINVAL;
2265 }
2266
2267 if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
Alexei Starovoitovd82bccc2016-04-12 10:26:19 -07002268 BPF_SIZE(insn->code) == BPF_DW ||
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002269 (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002270 verbose("BPF_LD_[ABS|IND] uses reserved fields\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002271 return -EINVAL;
2272 }
2273
2274 /* check whether implicit source operand (register R6) is readable */
2275 err = check_reg_arg(regs, BPF_REG_6, SRC_OP);
2276 if (err)
2277 return err;
2278
2279 if (regs[BPF_REG_6].type != PTR_TO_CTX) {
2280 verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
2281 return -EINVAL;
2282 }
2283
2284 if (mode == BPF_IND) {
2285 /* check explicit source operand */
2286 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2287 if (err)
2288 return err;
2289 }
2290
2291 /* reset caller saved regs to unreadable */
2292 for (i = 0; i < CALLER_SAVED_REGS; i++) {
2293 reg = regs + caller_saved[i];
2294 reg->type = NOT_INIT;
2295 reg->imm = 0;
2296 }
2297
2298 /* mark destination R0 register as readable, since it contains
2299 * the value fetched from the packet
2300 */
2301 regs[BPF_REG_0].type = UNKNOWN_VALUE;
2302 return 0;
2303}
2304
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002305/* non-recursive DFS pseudo code
2306 * 1 procedure DFS-iterative(G,v):
2307 * 2 label v as discovered
2308 * 3 let S be a stack
2309 * 4 S.push(v)
2310 * 5 while S is not empty
2311 * 6 t <- S.pop()
2312 * 7 if t is what we're looking for:
2313 * 8 return t
2314 * 9 for all edges e in G.adjacentEdges(t) do
2315 * 10 if edge e is already labelled
2316 * 11 continue with the next edge
2317 * 12 w <- G.adjacentVertex(t,e)
2318 * 13 if vertex w is not discovered and not explored
2319 * 14 label e as tree-edge
2320 * 15 label w as discovered
2321 * 16 S.push(w)
2322 * 17 continue at 5
2323 * 18 else if vertex w is discovered
2324 * 19 label e as back-edge
2325 * 20 else
2326 * 21 // vertex w is explored
2327 * 22 label e as forward- or cross-edge
2328 * 23 label t as explored
2329 * 24 S.pop()
2330 *
2331 * convention:
2332 * 0x10 - discovered
2333 * 0x11 - discovered and fall-through edge labelled
2334 * 0x12 - discovered and fall-through and branch edges labelled
2335 * 0x20 - explored
2336 */
2337
2338enum {
2339 DISCOVERED = 0x10,
2340 EXPLORED = 0x20,
2341 FALLTHROUGH = 1,
2342 BRANCH = 2,
2343};
2344
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002345#define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002346
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002347static int *insn_stack; /* stack of insns to process */
2348static int cur_stack; /* current stack index */
2349static int *insn_state;
2350
2351/* t, w, e - match pseudo-code above:
2352 * t - index of current instruction
2353 * w - next instruction
2354 * e - edge
2355 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002356static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002357{
2358 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
2359 return 0;
2360
2361 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
2362 return 0;
2363
2364 if (w < 0 || w >= env->prog->len) {
2365 verbose("jump out of range from insn %d to %d\n", t, w);
2366 return -EINVAL;
2367 }
2368
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002369 if (e == BRANCH)
2370 /* mark branch target for state pruning */
2371 env->explored_states[w] = STATE_LIST_MARK;
2372
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002373 if (insn_state[w] == 0) {
2374 /* tree-edge */
2375 insn_state[t] = DISCOVERED | e;
2376 insn_state[w] = DISCOVERED;
2377 if (cur_stack >= env->prog->len)
2378 return -E2BIG;
2379 insn_stack[cur_stack++] = w;
2380 return 1;
2381 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
2382 verbose("back-edge from insn %d to %d\n", t, w);
2383 return -EINVAL;
2384 } else if (insn_state[w] == EXPLORED) {
2385 /* forward- or cross-edge */
2386 insn_state[t] = DISCOVERED | e;
2387 } else {
2388 verbose("insn state internal bug\n");
2389 return -EFAULT;
2390 }
2391 return 0;
2392}
2393
2394/* non-recursive depth-first-search to detect loops in BPF program
2395 * loop == back-edge in directed graph
2396 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002397static int check_cfg(struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002398{
2399 struct bpf_insn *insns = env->prog->insnsi;
2400 int insn_cnt = env->prog->len;
2401 int ret = 0;
2402 int i, t;
2403
2404 insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
2405 if (!insn_state)
2406 return -ENOMEM;
2407
2408 insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
2409 if (!insn_stack) {
2410 kfree(insn_state);
2411 return -ENOMEM;
2412 }
2413
2414 insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
2415 insn_stack[0] = 0; /* 0 is the first instruction */
2416 cur_stack = 1;
2417
2418peek_stack:
2419 if (cur_stack == 0)
2420 goto check_state;
2421 t = insn_stack[cur_stack - 1];
2422
2423 if (BPF_CLASS(insns[t].code) == BPF_JMP) {
2424 u8 opcode = BPF_OP(insns[t].code);
2425
2426 if (opcode == BPF_EXIT) {
2427 goto mark_explored;
2428 } else if (opcode == BPF_CALL) {
2429 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2430 if (ret == 1)
2431 goto peek_stack;
2432 else if (ret < 0)
2433 goto err_free;
Daniel Borkmann07016152016-04-05 22:33:17 +02002434 if (t + 1 < insn_cnt)
2435 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002436 } else if (opcode == BPF_JA) {
2437 if (BPF_SRC(insns[t].code) != BPF_K) {
2438 ret = -EINVAL;
2439 goto err_free;
2440 }
2441 /* unconditional jump with single edge */
2442 ret = push_insn(t, t + insns[t].off + 1,
2443 FALLTHROUGH, env);
2444 if (ret == 1)
2445 goto peek_stack;
2446 else if (ret < 0)
2447 goto err_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002448 /* tell verifier to check for equivalent states
2449 * after every call and jump
2450 */
Alexei Starovoitovc3de6312015-04-14 15:57:13 -07002451 if (t + 1 < insn_cnt)
2452 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002453 } else {
2454 /* conditional jump with two edges */
Daniel Borkmann577aa832017-05-18 03:00:06 +02002455 env->explored_states[t] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002456 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2457 if (ret == 1)
2458 goto peek_stack;
2459 else if (ret < 0)
2460 goto err_free;
2461
2462 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
2463 if (ret == 1)
2464 goto peek_stack;
2465 else if (ret < 0)
2466 goto err_free;
2467 }
2468 } else {
2469 /* all other non-branch instructions with single
2470 * fall-through edge
2471 */
2472 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2473 if (ret == 1)
2474 goto peek_stack;
2475 else if (ret < 0)
2476 goto err_free;
2477 }
2478
2479mark_explored:
2480 insn_state[t] = EXPLORED;
2481 if (cur_stack-- <= 0) {
2482 verbose("pop stack internal bug\n");
2483 ret = -EFAULT;
2484 goto err_free;
2485 }
2486 goto peek_stack;
2487
2488check_state:
2489 for (i = 0; i < insn_cnt; i++) {
2490 if (insn_state[i] != EXPLORED) {
2491 verbose("unreachable insn %d\n", i);
2492 ret = -EINVAL;
2493 goto err_free;
2494 }
2495 }
2496 ret = 0; /* cfg looks good */
2497
2498err_free:
2499 kfree(insn_state);
2500 kfree(insn_stack);
2501 return ret;
2502}
2503
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002504/* the following conditions reduce the number of explored insns
2505 * from ~140k to ~80k for ultra large programs that use a lot of ptr_to_packet
2506 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002507static bool compare_ptrs_to_packet(struct bpf_reg_state *old,
2508 struct bpf_reg_state *cur)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002509{
2510 if (old->id != cur->id)
2511 return false;
2512
2513 /* old ptr_to_packet is more conservative, since it allows smaller
2514 * range. Ex:
2515 * old(off=0,r=10) is equal to cur(off=0,r=20), because
2516 * old(off=0,r=10) means that with range=10 the verifier proceeded
2517 * further and found no issues with the program. Now we're in the same
2518 * spot with cur(off=0,r=20), so we're safe too, since anything further
2519 * will only be looking at most 10 bytes after this pointer.
2520 */
2521 if (old->off == cur->off && old->range < cur->range)
2522 return true;
2523
2524 /* old(off=20,r=10) is equal to cur(off=22,re=22 or 5 or 0)
2525 * since both cannot be used for packet access and safe(old)
2526 * pointer has smaller off that could be used for further
2527 * 'if (ptr > data_end)' check
2528 * Ex:
2529 * old(off=20,r=10) and cur(off=22,r=22) and cur(off=22,r=0) mean
2530 * that we cannot access the packet.
2531 * The safe range is:
2532 * [ptr, ptr + range - off)
2533 * so whenever off >=range, it means no safe bytes from this pointer.
2534 * When comparing old->off <= cur->off, it means that older code
2535 * went with smaller offset and that offset was later
2536 * used to figure out the safe range after 'if (ptr > data_end)' check
2537 * Say, 'old' state was explored like:
2538 * ... R3(off=0, r=0)
2539 * R4 = R3 + 20
2540 * ... now R4(off=20,r=0) <-- here
2541 * if (R4 > data_end)
2542 * ... R4(off=20,r=20), R3(off=0,r=20) and R3 can be used to access.
2543 * ... the code further went all the way to bpf_exit.
2544 * Now the 'cur' state at the mark 'here' has R4(off=30,r=0).
2545 * old_R4(off=20,r=0) equal to cur_R4(off=30,r=0), since if the verifier
2546 * goes further, such cur_R4 will give larger safe packet range after
2547 * 'if (R4 > data_end)' and all further insn were already good with r=20,
2548 * so they will be good with r=30 and we can prune the search.
2549 */
2550 if (old->off <= cur->off &&
2551 old->off >= old->range && cur->off >= cur->range)
2552 return true;
2553
2554 return false;
2555}
2556
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002557/* compare two verifier states
2558 *
2559 * all states stored in state_list are known to be valid, since
2560 * verifier reached 'bpf_exit' instruction through them
2561 *
2562 * this function is called when verifier exploring different branches of
2563 * execution popped from the state stack. If it sees an old state that has
2564 * more strict register state and more strict stack state then this execution
2565 * branch doesn't need to be explored further, since verifier already
2566 * concluded that more strict state leads to valid finish.
2567 *
2568 * Therefore two states are equivalent if register state is more conservative
2569 * and explored stack state is more conservative than the current one.
2570 * Example:
2571 * explored current
2572 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
2573 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
2574 *
2575 * In other words if current stack state (one being explored) has more
2576 * valid slots than old one that already passed validation, it means
2577 * the verifier can stop exploring and conclude that current state is valid too
2578 *
2579 * Similarly with registers. If explored state has register type as invalid
2580 * whereas register type in current state is meaningful, it means that
2581 * the current state will reach 'bpf_exit' instruction safely
2582 */
Josef Bacik48461132016-09-28 10:54:32 -04002583static bool states_equal(struct bpf_verifier_env *env,
2584 struct bpf_verifier_state *old,
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002585 struct bpf_verifier_state *cur)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002586{
Josef Bacike2d2afe2016-11-29 12:27:09 -05002587 bool varlen_map_access = env->varlen_map_value_access;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002588 struct bpf_reg_state *rold, *rcur;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002589 int i;
2590
2591 for (i = 0; i < MAX_BPF_REG; i++) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002592 rold = &old->regs[i];
2593 rcur = &cur->regs[i];
2594
2595 if (memcmp(rold, rcur, sizeof(*rold)) == 0)
2596 continue;
2597
Josef Bacik48461132016-09-28 10:54:32 -04002598 /* If the ranges were not the same, but everything else was and
2599 * we didn't do a variable access into a map then we are a-ok.
2600 */
Josef Bacike2d2afe2016-11-29 12:27:09 -05002601 if (!varlen_map_access &&
Alexei Starovoitovb7f5aa12016-12-07 10:57:59 -08002602 memcmp(rold, rcur, offsetofend(struct bpf_reg_state, id)) == 0)
Josef Bacik48461132016-09-28 10:54:32 -04002603 continue;
2604
Josef Bacike2d2afe2016-11-29 12:27:09 -05002605 /* If we didn't map access then again we don't care about the
2606 * mismatched range values and it's ok if our old type was
2607 * UNKNOWN and we didn't go to a NOT_INIT'ed reg.
2608 */
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002609 if (rold->type == NOT_INIT ||
Josef Bacike2d2afe2016-11-29 12:27:09 -05002610 (!varlen_map_access && rold->type == UNKNOWN_VALUE &&
2611 rcur->type != NOT_INIT))
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002612 continue;
2613
Daniel Borkmann577aa832017-05-18 03:00:06 +02002614 /* Don't care about the reg->id in this case. */
2615 if (rold->type == PTR_TO_MAP_VALUE_OR_NULL &&
2616 rcur->type == PTR_TO_MAP_VALUE_OR_NULL &&
2617 rold->map_ptr == rcur->map_ptr)
2618 continue;
2619
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002620 if (rold->type == PTR_TO_PACKET && rcur->type == PTR_TO_PACKET &&
2621 compare_ptrs_to_packet(rold, rcur))
2622 continue;
2623
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002624 return false;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002625 }
2626
2627 for (i = 0; i < MAX_BPF_STACK; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002628 if (old->stack_slot_type[i] == STACK_INVALID)
2629 continue;
2630 if (old->stack_slot_type[i] != cur->stack_slot_type[i])
2631 /* Ex: old explored (safe) state has STACK_SPILL in
2632 * this stack slot, but current has has STACK_MISC ->
2633 * this verifier states are not equivalent,
2634 * return false to continue verification of this path
2635 */
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002636 return false;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002637 if (i % BPF_REG_SIZE)
2638 continue;
2639 if (memcmp(&old->spilled_regs[i / BPF_REG_SIZE],
2640 &cur->spilled_regs[i / BPF_REG_SIZE],
2641 sizeof(old->spilled_regs[0])))
2642 /* when explored and current stack slot types are
2643 * the same, check that stored pointers types
2644 * are the same as well.
2645 * Ex: explored safe path could have stored
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002646 * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -8}
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002647 * but current path has stored:
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002648 * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -16}
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002649 * such verifier states are not equivalent.
2650 * return false to continue verification of this path
2651 */
2652 return false;
2653 else
2654 continue;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002655 }
2656 return true;
2657}
2658
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002659static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002660{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002661 struct bpf_verifier_state_list *new_sl;
2662 struct bpf_verifier_state_list *sl;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002663
2664 sl = env->explored_states[insn_idx];
2665 if (!sl)
2666 /* this 'insn_idx' instruction wasn't marked, so we will not
2667 * be doing state search here
2668 */
2669 return 0;
2670
2671 while (sl != STATE_LIST_MARK) {
Josef Bacik48461132016-09-28 10:54:32 -04002672 if (states_equal(env, &sl->state, &env->cur_state))
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002673 /* reached equivalent register/stack state,
2674 * prune the search
2675 */
2676 return 1;
2677 sl = sl->next;
2678 }
2679
2680 /* there were no equivalent states, remember current one.
2681 * technically the current state is not proven to be safe yet,
2682 * but it will either reach bpf_exit (which means it's safe) or
2683 * it will be rejected. Since there are no loops, we won't be
2684 * seeing this 'insn_idx' instruction again on the way to bpf_exit
2685 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002686 new_sl = kmalloc(sizeof(struct bpf_verifier_state_list), GFP_USER);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002687 if (!new_sl)
2688 return -ENOMEM;
2689
2690 /* add new state to the head of linked list */
2691 memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state));
2692 new_sl->next = env->explored_states[insn_idx];
2693 env->explored_states[insn_idx] = new_sl;
2694 return 0;
2695}
2696
Jakub Kicinski13a27df2016-09-21 11:43:58 +01002697static int ext_analyzer_insn_hook(struct bpf_verifier_env *env,
2698 int insn_idx, int prev_insn_idx)
2699{
2700 if (!env->analyzer_ops || !env->analyzer_ops->insn_hook)
2701 return 0;
2702
2703 return env->analyzer_ops->insn_hook(env, insn_idx, prev_insn_idx);
2704}
2705
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002706static int do_check(struct bpf_verifier_env *env)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002707{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002708 struct bpf_verifier_state *state = &env->cur_state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002709 struct bpf_insn *insns = env->prog->insnsi;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002710 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002711 int insn_cnt = env->prog->len;
2712 int insn_idx, prev_insn_idx = 0;
2713 int insn_processed = 0;
2714 bool do_print_state = false;
2715
2716 init_reg_state(regs);
2717 insn_idx = 0;
Josef Bacik48461132016-09-28 10:54:32 -04002718 env->varlen_map_value_access = false;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002719 for (;;) {
2720 struct bpf_insn *insn;
2721 u8 class;
2722 int err;
2723
2724 if (insn_idx >= insn_cnt) {
2725 verbose("invalid insn idx %d insn_cnt %d\n",
2726 insn_idx, insn_cnt);
2727 return -EFAULT;
2728 }
2729
2730 insn = &insns[insn_idx];
2731 class = BPF_CLASS(insn->code);
2732
Daniel Borkmann07016152016-04-05 22:33:17 +02002733 if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002734 verbose("BPF program is too large. Proccessed %d insn\n",
2735 insn_processed);
2736 return -E2BIG;
2737 }
2738
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002739 err = is_state_visited(env, insn_idx);
2740 if (err < 0)
2741 return err;
2742 if (err == 1) {
2743 /* found equivalent state, can prune the search */
2744 if (log_level) {
2745 if (do_print_state)
2746 verbose("\nfrom %d to %d: safe\n",
2747 prev_insn_idx, insn_idx);
2748 else
2749 verbose("%d: safe\n", insn_idx);
2750 }
2751 goto process_bpf_exit;
2752 }
2753
Daniel Borkmann577aa832017-05-18 03:00:06 +02002754 if (need_resched())
2755 cond_resched();
2756
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002757 if (log_level && do_print_state) {
2758 verbose("\nfrom %d to %d:", prev_insn_idx, insn_idx);
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002759 print_verifier_state(&env->cur_state);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002760 do_print_state = false;
2761 }
2762
2763 if (log_level) {
2764 verbose("%d: ", insn_idx);
Daniel Borkmannced0a312017-05-08 00:04:09 +02002765 print_bpf_insn(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002766 }
2767
Jakub Kicinski13a27df2016-09-21 11:43:58 +01002768 err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx);
2769 if (err)
2770 return err;
2771
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002772 if (class == BPF_ALU || class == BPF_ALU64) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002773 err = check_alu_op(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002774 if (err)
2775 return err;
2776
2777 } else if (class == BPF_LDX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002778 enum bpf_reg_type *prev_src_type, src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002779
2780 /* check for reserved fields is already done */
2781
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002782 /* check src operand */
2783 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2784 if (err)
2785 return err;
2786
2787 err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
2788 if (err)
2789 return err;
2790
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07002791 src_reg_type = regs[insn->src_reg].type;
2792
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002793 /* check that memory (src_reg + off) is readable,
2794 * the state of dst_reg will be updated by this func
2795 */
2796 err = check_mem_access(env, insn->src_reg, insn->off,
2797 BPF_SIZE(insn->code), BPF_READ,
2798 insn->dst_reg);
2799 if (err)
2800 return err;
2801
Josef Bacik48461132016-09-28 10:54:32 -04002802 reset_reg_range_values(regs, insn->dst_reg);
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07002803 if (BPF_SIZE(insn->code) != BPF_W &&
2804 BPF_SIZE(insn->code) != BPF_DW) {
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07002805 insn_idx++;
2806 continue;
2807 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002808
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002809 prev_src_type = &env->insn_aux_data[insn_idx].ptr_type;
2810
2811 if (*prev_src_type == NOT_INIT) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002812 /* saw a valid insn
2813 * dst_reg = *(u32 *)(src_reg + off)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002814 * save type to validate intersecting paths
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002815 */
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002816 *prev_src_type = src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002817
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002818 } else if (src_reg_type != *prev_src_type &&
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002819 (src_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002820 *prev_src_type == PTR_TO_CTX)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002821 /* ABuser program is trying to use the same insn
2822 * dst_reg = *(u32*) (src_reg + off)
2823 * with different pointer types:
2824 * src_reg == ctx in one branch and
2825 * src_reg == stack|map in some other branch.
2826 * Reject it.
2827 */
2828 verbose("same insn cannot be used with different pointers\n");
2829 return -EINVAL;
2830 }
2831
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002832 } else if (class == BPF_STX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002833 enum bpf_reg_type *prev_dst_type, dst_reg_type;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002834
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002835 if (BPF_MODE(insn->code) == BPF_XADD) {
2836 err = check_xadd(env, insn);
2837 if (err)
2838 return err;
2839 insn_idx++;
2840 continue;
2841 }
2842
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002843 /* check src1 operand */
2844 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2845 if (err)
2846 return err;
2847 /* check src2 operand */
2848 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
2849 if (err)
2850 return err;
2851
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002852 dst_reg_type = regs[insn->dst_reg].type;
2853
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002854 /* check that memory (dst_reg + off) is writeable */
2855 err = check_mem_access(env, insn->dst_reg, insn->off,
2856 BPF_SIZE(insn->code), BPF_WRITE,
2857 insn->src_reg);
2858 if (err)
2859 return err;
2860
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002861 prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type;
2862
2863 if (*prev_dst_type == NOT_INIT) {
2864 *prev_dst_type = dst_reg_type;
2865 } else if (dst_reg_type != *prev_dst_type &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002866 (dst_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002867 *prev_dst_type == PTR_TO_CTX)) {
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002868 verbose("same insn cannot be used with different pointers\n");
2869 return -EINVAL;
2870 }
2871
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002872 } else if (class == BPF_ST) {
2873 if (BPF_MODE(insn->code) != BPF_MEM ||
2874 insn->src_reg != BPF_REG_0) {
2875 verbose("BPF_ST uses reserved fields\n");
2876 return -EINVAL;
2877 }
2878 /* check src operand */
2879 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
2880 if (err)
2881 return err;
2882
2883 /* check that memory (dst_reg + off) is writeable */
2884 err = check_mem_access(env, insn->dst_reg, insn->off,
2885 BPF_SIZE(insn->code), BPF_WRITE,
2886 -1);
2887 if (err)
2888 return err;
2889
2890 } else if (class == BPF_JMP) {
2891 u8 opcode = BPF_OP(insn->code);
2892
2893 if (opcode == BPF_CALL) {
2894 if (BPF_SRC(insn->code) != BPF_K ||
2895 insn->off != 0 ||
2896 insn->src_reg != BPF_REG_0 ||
2897 insn->dst_reg != BPF_REG_0) {
2898 verbose("BPF_CALL uses reserved fields\n");
2899 return -EINVAL;
2900 }
2901
2902 err = check_call(env, insn->imm);
2903 if (err)
2904 return err;
2905
2906 } else if (opcode == BPF_JA) {
2907 if (BPF_SRC(insn->code) != BPF_K ||
2908 insn->imm != 0 ||
2909 insn->src_reg != BPF_REG_0 ||
2910 insn->dst_reg != BPF_REG_0) {
2911 verbose("BPF_JA uses reserved fields\n");
2912 return -EINVAL;
2913 }
2914
2915 insn_idx += insn->off + 1;
2916 continue;
2917
2918 } else if (opcode == BPF_EXIT) {
2919 if (BPF_SRC(insn->code) != BPF_K ||
2920 insn->imm != 0 ||
2921 insn->src_reg != BPF_REG_0 ||
2922 insn->dst_reg != BPF_REG_0) {
2923 verbose("BPF_EXIT uses reserved fields\n");
2924 return -EINVAL;
2925 }
2926
2927 /* eBPF calling convetion is such that R0 is used
2928 * to return the value from eBPF program.
2929 * Make sure that it's readable at this time
2930 * of bpf_exit, which means that program wrote
2931 * something into it earlier
2932 */
2933 err = check_reg_arg(regs, BPF_REG_0, SRC_OP);
2934 if (err)
2935 return err;
2936
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002937 if (is_pointer_value(env, BPF_REG_0)) {
2938 verbose("R0 leaks addr as return value\n");
2939 return -EACCES;
2940 }
2941
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002942process_bpf_exit:
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002943 insn_idx = pop_stack(env, &prev_insn_idx);
2944 if (insn_idx < 0) {
2945 break;
2946 } else {
2947 do_print_state = true;
2948 continue;
2949 }
2950 } else {
2951 err = check_cond_jmp_op(env, insn, &insn_idx);
2952 if (err)
2953 return err;
2954 }
2955 } else if (class == BPF_LD) {
2956 u8 mode = BPF_MODE(insn->code);
2957
2958 if (mode == BPF_ABS || mode == BPF_IND) {
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002959 err = check_ld_abs(env, insn);
2960 if (err)
2961 return err;
2962
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002963 } else if (mode == BPF_IMM) {
2964 err = check_ld_imm(env, insn);
2965 if (err)
2966 return err;
2967
2968 insn_idx++;
2969 } else {
2970 verbose("invalid BPF_LD mode\n");
2971 return -EINVAL;
2972 }
Josef Bacik48461132016-09-28 10:54:32 -04002973 reset_reg_range_values(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002974 } else {
2975 verbose("unknown insn class %d\n", class);
2976 return -EINVAL;
2977 }
2978
2979 insn_idx++;
2980 }
2981
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002982 verbose("processed %d insns\n", insn_processed);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002983 return 0;
2984}
2985
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07002986static int check_map_prog_compatibility(struct bpf_map *map,
2987 struct bpf_prog *prog)
2988
2989{
2990 if (prog->type == BPF_PROG_TYPE_PERF_EVENT &&
2991 (map->map_type == BPF_MAP_TYPE_HASH ||
2992 map->map_type == BPF_MAP_TYPE_PERCPU_HASH) &&
2993 (map->map_flags & BPF_F_NO_PREALLOC)) {
2994 verbose("perf_event programs can only use preallocated hash map\n");
2995 return -EINVAL;
2996 }
2997 return 0;
2998}
2999
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003000/* look for pseudo eBPF instructions that access map FDs and
3001 * replace them with actual map pointers
3002 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003003static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003004{
3005 struct bpf_insn *insn = env->prog->insnsi;
3006 int insn_cnt = env->prog->len;
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07003007 int i, j, err;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003008
3009 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003010 if (BPF_CLASS(insn->code) == BPF_LDX &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003011 (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003012 verbose("BPF_LDX uses reserved fields\n");
3013 return -EINVAL;
3014 }
3015
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003016 if (BPF_CLASS(insn->code) == BPF_STX &&
3017 ((BPF_MODE(insn->code) != BPF_MEM &&
3018 BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
3019 verbose("BPF_STX uses reserved fields\n");
3020 return -EINVAL;
3021 }
3022
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003023 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
3024 struct bpf_map *map;
3025 struct fd f;
3026
3027 if (i == insn_cnt - 1 || insn[1].code != 0 ||
3028 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
3029 insn[1].off != 0) {
3030 verbose("invalid bpf_ld_imm64 insn\n");
3031 return -EINVAL;
3032 }
3033
3034 if (insn->src_reg == 0)
3035 /* valid generic load 64-bit imm */
3036 goto next_insn;
3037
3038 if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
3039 verbose("unrecognized bpf_ld_imm64 insn\n");
3040 return -EINVAL;
3041 }
3042
3043 f = fdget(insn->imm);
Daniel Borkmannc2101292015-10-29 14:58:07 +01003044 map = __bpf_map_get(f);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003045 if (IS_ERR(map)) {
3046 verbose("fd %d is not pointing to valid bpf_map\n",
3047 insn->imm);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003048 return PTR_ERR(map);
3049 }
3050
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07003051 err = check_map_prog_compatibility(map, env->prog);
3052 if (err) {
3053 fdput(f);
3054 return err;
3055 }
3056
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003057 /* store map pointer inside BPF_LD_IMM64 instruction */
3058 insn[0].imm = (u32) (unsigned long) map;
3059 insn[1].imm = ((u64) (unsigned long) map) >> 32;
3060
3061 /* check whether we recorded this map already */
3062 for (j = 0; j < env->used_map_cnt; j++)
3063 if (env->used_maps[j] == map) {
3064 fdput(f);
3065 goto next_insn;
3066 }
3067
3068 if (env->used_map_cnt >= MAX_USED_MAPS) {
3069 fdput(f);
3070 return -E2BIG;
3071 }
3072
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003073 /* hold the map. If the program is rejected by verifier,
3074 * the map will be released by release_maps() or it
3075 * will be used by the valid program until it's unloaded
3076 * and all maps are released in free_bpf_prog_info()
3077 */
Alexei Starovoitov92117d82016-04-27 18:56:20 -07003078 map = bpf_map_inc(map, false);
3079 if (IS_ERR(map)) {
3080 fdput(f);
3081 return PTR_ERR(map);
3082 }
3083 env->used_maps[env->used_map_cnt++] = map;
3084
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003085 fdput(f);
3086next_insn:
3087 insn++;
3088 i++;
3089 }
3090 }
3091
3092 /* now all pseudo BPF_LD_IMM64 instructions load valid
3093 * 'struct bpf_map *' into a register instead of user map_fd.
3094 * These pointers will be used later by verifier to validate map access.
3095 */
3096 return 0;
3097}
3098
3099/* drop refcnt of maps used by the rejected program */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003100static void release_maps(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003101{
3102 int i;
3103
3104 for (i = 0; i < env->used_map_cnt; i++)
3105 bpf_map_put(env->used_maps[i]);
3106}
3107
3108/* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003109static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003110{
3111 struct bpf_insn *insn = env->prog->insnsi;
3112 int insn_cnt = env->prog->len;
3113 int i;
3114
3115 for (i = 0; i < insn_cnt; i++, insn++)
3116 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
3117 insn->src_reg = 0;
3118}
3119
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003120/* convert load instructions that access fields of 'struct __sk_buff'
3121 * into sequence of instructions that access fields of 'struct sk_buff'
3122 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003123static int convert_ctx_accesses(struct bpf_verifier_env *env)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003124{
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003125 const struct bpf_verifier_ops *ops = env->prog->aux->ops;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003126 const int insn_cnt = env->prog->len;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003127 struct bpf_insn insn_buf[16], *insn;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003128 struct bpf_prog *new_prog;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003129 enum bpf_access_type type;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003130 int i, cnt, delta = 0;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003131
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003132 if (ops->gen_prologue) {
3133 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
3134 env->prog);
3135 if (cnt >= ARRAY_SIZE(insn_buf)) {
3136 verbose("bpf verifier is misconfigured\n");
3137 return -EINVAL;
3138 } else if (cnt) {
3139 new_prog = bpf_patch_insn_single(env->prog, 0,
3140 insn_buf, cnt);
3141 if (!new_prog)
3142 return -ENOMEM;
3143 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003144 delta += cnt - 1;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003145 }
3146 }
3147
3148 if (!ops->convert_ctx_access)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003149 return 0;
3150
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003151 insn = env->prog->insnsi + delta;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003152
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003153 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07003154 if (insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
3155 insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003156 type = BPF_READ;
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07003157 else if (insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
3158 insn->code == (BPF_STX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003159 type = BPF_WRITE;
3160 else
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003161 continue;
3162
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003163 if (env->insn_aux_data[i].ptr_type != PTR_TO_CTX)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003164 continue;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003165
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003166 cnt = ops->convert_ctx_access(type, insn->dst_reg, insn->src_reg,
3167 insn->off, insn_buf, env->prog);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003168 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
3169 verbose("bpf verifier is misconfigured\n");
3170 return -EINVAL;
3171 }
3172
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003173 new_prog = bpf_patch_insn_single(env->prog, i + delta, insn_buf,
3174 cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003175 if (!new_prog)
3176 return -ENOMEM;
3177
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003178 delta += cnt - 1;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003179
3180 /* keep walking new program and skip insns we just inserted */
3181 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003182 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003183 }
3184
3185 return 0;
3186}
3187
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003188static void free_states(struct bpf_verifier_env *env)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003189{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003190 struct bpf_verifier_state_list *sl, *sln;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003191 int i;
3192
3193 if (!env->explored_states)
3194 return;
3195
3196 for (i = 0; i < env->prog->len; i++) {
3197 sl = env->explored_states[i];
3198
3199 if (sl)
3200 while (sl != STATE_LIST_MARK) {
3201 sln = sl->next;
3202 kfree(sl);
3203 sl = sln;
3204 }
3205 }
3206
3207 kfree(env->explored_states);
3208}
3209
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003210int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003211{
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003212 char __user *log_ubuf = NULL;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003213 struct bpf_verifier_env *env;
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003214 int ret = -EINVAL;
3215
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003216 if ((*prog)->len <= 0 || (*prog)->len > BPF_MAXINSNS)
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003217 return -E2BIG;
3218
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003219 /* 'struct bpf_verifier_env' can be global, but since it's not small,
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003220 * allocate/free it every time bpf_check() is called
3221 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003222 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003223 if (!env)
3224 return -ENOMEM;
3225
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003226 env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
3227 (*prog)->len);
3228 ret = -ENOMEM;
3229 if (!env->insn_aux_data)
3230 goto err_free_env;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003231 env->prog = *prog;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003232
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003233 /* grab the mutex to protect few globals used by verifier */
3234 mutex_lock(&bpf_verifier_lock);
3235
3236 if (attr->log_level || attr->log_buf || attr->log_size) {
3237 /* user requested verbose verifier output
3238 * and supplied buffer to store the verification trace
3239 */
3240 log_level = attr->log_level;
3241 log_ubuf = (char __user *) (unsigned long) attr->log_buf;
3242 log_size = attr->log_size;
3243 log_len = 0;
3244
3245 ret = -EINVAL;
3246 /* log_* values have to be sane */
3247 if (log_size < 128 || log_size > UINT_MAX >> 8 ||
3248 log_level == 0 || log_ubuf == NULL)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003249 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003250
3251 ret = -ENOMEM;
3252 log_buf = vmalloc(log_size);
3253 if (!log_buf)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003254 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003255 } else {
3256 log_level = 0;
3257 }
3258
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003259 ret = replace_map_fd_with_map_ptr(env);
3260 if (ret < 0)
3261 goto skip_full_check;
3262
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003263 env->explored_states = kcalloc(env->prog->len,
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003264 sizeof(struct bpf_verifier_state_list *),
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003265 GFP_USER);
3266 ret = -ENOMEM;
3267 if (!env->explored_states)
3268 goto skip_full_check;
3269
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003270 ret = check_cfg(env);
3271 if (ret < 0)
3272 goto skip_full_check;
3273
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003274 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
3275
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003276 ret = do_check(env);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003277
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003278skip_full_check:
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003279 while (pop_stack(env, NULL) >= 0);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003280 free_states(env);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003281
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003282 if (ret == 0)
3283 /* program is valid, convert *(u32*)(ctx + off) accesses */
3284 ret = convert_ctx_accesses(env);
3285
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003286 if (log_level && log_len >= log_size - 1) {
3287 BUG_ON(log_len >= log_size);
3288 /* verifier log exceeded user supplied buffer */
3289 ret = -ENOSPC;
3290 /* fall through to return what was recorded */
3291 }
3292
3293 /* copy verifier log back to user space including trailing zero */
3294 if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) {
3295 ret = -EFAULT;
3296 goto free_log_buf;
3297 }
3298
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003299 if (ret == 0 && env->used_map_cnt) {
3300 /* if program passed verifier, update used_maps in bpf_prog_info */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003301 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
3302 sizeof(env->used_maps[0]),
3303 GFP_KERNEL);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003304
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003305 if (!env->prog->aux->used_maps) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003306 ret = -ENOMEM;
3307 goto free_log_buf;
3308 }
3309
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003310 memcpy(env->prog->aux->used_maps, env->used_maps,
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003311 sizeof(env->used_maps[0]) * env->used_map_cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003312 env->prog->aux->used_map_cnt = env->used_map_cnt;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003313
3314 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
3315 * bpf_ld_imm64 instructions
3316 */
3317 convert_pseudo_ld_imm64(env);
3318 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003319
3320free_log_buf:
3321 if (log_level)
3322 vfree(log_buf);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003323 if (!env->prog->aux->used_maps)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003324 /* if we didn't copy map pointers into bpf_prog_info, release
3325 * them now. Otherwise free_bpf_prog_info() will release them.
3326 */
3327 release_maps(env);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003328 *prog = env->prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003329err_unlock:
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003330 mutex_unlock(&bpf_verifier_lock);
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003331 vfree(env->insn_aux_data);
3332err_free_env:
3333 kfree(env);
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003334 return ret;
3335}
Jakub Kicinski13a27df2016-09-21 11:43:58 +01003336
3337int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops,
3338 void *priv)
3339{
3340 struct bpf_verifier_env *env;
3341 int ret;
3342
3343 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
3344 if (!env)
3345 return -ENOMEM;
3346
3347 env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
3348 prog->len);
3349 ret = -ENOMEM;
3350 if (!env->insn_aux_data)
3351 goto err_free_env;
3352 env->prog = prog;
3353 env->analyzer_ops = ops;
3354 env->analyzer_priv = priv;
3355
3356 /* grab the mutex to protect few globals used by verifier */
3357 mutex_lock(&bpf_verifier_lock);
3358
3359 log_level = 0;
3360
3361 env->explored_states = kcalloc(env->prog->len,
3362 sizeof(struct bpf_verifier_state_list *),
3363 GFP_KERNEL);
3364 ret = -ENOMEM;
3365 if (!env->explored_states)
3366 goto skip_full_check;
3367
3368 ret = check_cfg(env);
3369 if (ret < 0)
3370 goto skip_full_check;
3371
3372 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
3373
3374 ret = do_check(env);
3375
3376skip_full_check:
3377 while (pop_stack(env, NULL) >= 0);
3378 free_states(env);
3379
3380 mutex_unlock(&bpf_verifier_lock);
3381 vfree(env->insn_aux_data);
3382err_free_env:
3383 kfree(env);
3384 return ret;
3385}
3386EXPORT_SYMBOL_GPL(bpf_analyzer);