blob: 768704e9a2c791edfae820dc6fc9eab0a8c5c42f [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{
Ben Hutchings9c33b842018-12-05 22:45:15 +0000546 int i, spi = (MAX_BPF_STACK + off) / BPF_REG_SIZE;
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 */
Ben Hutchings9c33b842018-12-05 22:45:15 +0000561 state->spilled_regs[spi] = state->regs[value_regno];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700562
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700563 for (i = 0; i < BPF_REG_SIZE; i++)
564 state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_SPILL;
565 } else {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700566 /* regular write of data into stack */
Ben Hutchings9c33b842018-12-05 22:45:15 +0000567 state->spilled_regs[spi] = (struct bpf_reg_state) {};
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700568
569 for (i = 0; i < size; i++)
570 state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_MISC;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700571 }
572 return 0;
573}
574
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100575static int check_stack_read(struct bpf_verifier_state *state, int off, int size,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700576 int value_regno)
577{
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700578 u8 *slot_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700579 int i;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700580
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700581 slot_type = &state->stack_slot_type[MAX_BPF_STACK + off];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700582
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700583 if (slot_type[0] == STACK_SPILL) {
584 if (size != BPF_REG_SIZE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700585 verbose("invalid size of register spill\n");
586 return -EACCES;
587 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700588 for (i = 1; i < BPF_REG_SIZE; i++) {
589 if (slot_type[i] != STACK_SPILL) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700590 verbose("corrupted spill memory\n");
591 return -EACCES;
592 }
593 }
594
595 if (value_regno >= 0)
596 /* restore register state from stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700597 state->regs[value_regno] =
598 state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700599 return 0;
600 } else {
601 for (i = 0; i < size; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700602 if (slot_type[i] != STACK_MISC) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700603 verbose("invalid read from stack off %d+%d size %d\n",
604 off, i, size);
605 return -EACCES;
606 }
607 }
608 if (value_regno >= 0)
609 /* have read misc data from the stack */
610 mark_reg_unknown_value(state->regs, value_regno);
611 return 0;
612 }
613}
614
615/* check read/write into map element returned by bpf_map_lookup_elem() */
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100616static int check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700617 int size)
618{
619 struct bpf_map *map = env->cur_state.regs[regno].map_ptr;
620
621 if (off < 0 || off + size > map->value_size) {
622 verbose("invalid access to map value, value_size=%d off=%d size=%d\n",
623 map->value_size, off, size);
624 return -EACCES;
625 }
626 return 0;
627}
628
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700629#define MAX_PACKET_OFF 0xffff
630
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100631static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200632 const struct bpf_call_arg_meta *meta)
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700633{
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200634 switch (env->prog->type) {
635 case BPF_PROG_TYPE_SCHED_CLS:
636 case BPF_PROG_TYPE_SCHED_ACT:
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700637 case BPF_PROG_TYPE_XDP:
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200638 if (meta)
639 return meta->pkt_access;
640
641 env->seen_direct_write = true;
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700642 return true;
643 default:
644 return false;
645 }
646}
647
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100648static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700649 int size)
650{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100651 struct bpf_reg_state *regs = env->cur_state.regs;
652 struct bpf_reg_state *reg = &regs[regno];
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700653
Alexei Starovoitovd91b28e2016-05-19 18:17:13 -0700654 off += reg->off;
Daniel Borkmannb399cf62016-09-20 00:26:12 +0200655 if (off < 0 || size <= 0 || off + size > reg->range) {
Alexei Starovoitovd91b28e2016-05-19 18:17:13 -0700656 verbose("invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n",
657 off, size, regno, reg->id, reg->off, reg->range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700658 return -EACCES;
659 }
660 return 0;
661}
662
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700663/* check access to 'struct bpf_context' fields */
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100664static int check_ctx_access(struct bpf_verifier_env *env, int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700665 enum bpf_access_type t, enum bpf_reg_type *reg_type)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700666{
Jakub Kicinski13a27df2016-09-21 11:43:58 +0100667 /* for analyzer ctx accesses are already validated and converted */
668 if (env->analyzer_ops)
669 return 0;
670
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700671 if (env->prog->aux->ops->is_valid_access &&
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700672 env->prog->aux->ops->is_valid_access(off, size, t, reg_type)) {
Alexei Starovoitov32bbe002016-04-06 18:43:28 -0700673 /* remember the offset of last byte accessed in ctx */
674 if (env->prog->aux->max_ctx_offset < off + size)
675 env->prog->aux->max_ctx_offset = off + size;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700676 return 0;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -0700677 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700678
679 verbose("invalid bpf_context access off=%d size=%d\n", off, size);
680 return -EACCES;
681}
682
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +0200683static bool __is_pointer_value(bool allow_ptr_leaks,
684 const struct bpf_reg_state *reg)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700685{
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +0200686 if (allow_ptr_leaks)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700687 return false;
688
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +0200689 switch (reg->type) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700690 case UNKNOWN_VALUE:
691 case CONST_IMM:
692 return false;
693 default:
694 return true;
695 }
696}
697
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +0200698static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
699{
700 return __is_pointer_value(env->allow_ptr_leaks, &env->cur_state.regs[regno]);
701}
702
Daniel Borkmannf531fbb2018-01-29 02:49:01 +0100703static bool is_ctx_reg(struct bpf_verifier_env *env, int regno)
704{
705 const struct bpf_reg_state *reg = &env->cur_state.regs[regno];
706
707 return reg->type == PTR_TO_CTX;
708}
709
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100710static int check_ptr_alignment(struct bpf_verifier_env *env,
711 struct bpf_reg_state *reg, int off, int size)
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700712{
Josef Bacik48461132016-09-28 10:54:32 -0400713 if (reg->type != PTR_TO_PACKET && reg->type != PTR_TO_MAP_VALUE_ADJ) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700714 if (off % size != 0) {
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100715 verbose("misaligned access off %d size %d\n",
716 off, size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700717 return -EACCES;
718 } else {
719 return 0;
720 }
721 }
722
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700723 if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
724 /* misaligned access to packet is ok on x86,arm,arm64 */
725 return 0;
726
727 if (reg->id && size != 1) {
728 verbose("Unknown packet alignment. Only byte-sized access allowed\n");
729 return -EACCES;
730 }
731
732 /* skb->data is NET_IP_ALIGN-ed */
Josef Bacik48461132016-09-28 10:54:32 -0400733 if (reg->type == PTR_TO_PACKET &&
734 (NET_IP_ALIGN + reg->off + off) % size != 0) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700735 verbose("misaligned packet access off %d+%d+%d size %d\n",
736 NET_IP_ALIGN, reg->off, off, size);
737 return -EACCES;
738 }
739 return 0;
740}
741
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700742/* check whether memory at (regno + off) is accessible for t = (read | write)
743 * if t==write, value_regno is a register which value is stored into memory
744 * if t==read, value_regno is a register which will receive the value from memory
745 * if t==write && value_regno==-1, some unknown value is stored into memory
746 * if t==read && value_regno==-1, don't care what we read from memory
747 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100748static int check_mem_access(struct bpf_verifier_env *env, u32 regno, int off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700749 int bpf_size, enum bpf_access_type t,
750 int value_regno)
751{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100752 struct bpf_verifier_state *state = &env->cur_state;
753 struct bpf_reg_state *reg = &state->regs[regno];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700754 int size, err = 0;
755
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700756 if (reg->type == PTR_TO_STACK)
757 off += reg->imm;
Alex Gartrell24b4d2a2015-07-23 14:24:40 -0700758
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700759 size = bpf_size_to_bytes(bpf_size);
760 if (size < 0)
761 return size;
762
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700763 err = check_ptr_alignment(env, reg, off, size);
764 if (err)
765 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700766
Josef Bacik48461132016-09-28 10:54:32 -0400767 if (reg->type == PTR_TO_MAP_VALUE ||
768 reg->type == PTR_TO_MAP_VALUE_ADJ) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700769 if (t == BPF_WRITE && value_regno >= 0 &&
770 is_pointer_value(env, value_regno)) {
771 verbose("R%d leaks addr into map\n", value_regno);
772 return -EACCES;
773 }
Josef Bacik48461132016-09-28 10:54:32 -0400774
775 /* If we adjusted the register to this map value at all then we
776 * need to change off and size to min_value and max_value
777 * respectively to make sure our theoretical access will be
778 * safe.
779 */
780 if (reg->type == PTR_TO_MAP_VALUE_ADJ) {
781 if (log_level)
782 print_verifier_state(state);
783 env->varlen_map_value_access = true;
784 /* The minimum value is only important with signed
785 * comparisons where we can't assume the floor of a
786 * value is 0. If we are using signed variables for our
787 * index'es we need to make sure that whatever we use
788 * will have a set floor within our range.
789 */
Josef Bacikf23cc642016-11-14 15:45:36 -0500790 if (reg->min_value < 0) {
Josef Bacik48461132016-09-28 10:54:32 -0400791 verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
792 regno);
793 return -EACCES;
794 }
795 err = check_map_access(env, regno, reg->min_value + off,
796 size);
797 if (err) {
798 verbose("R%d min value is outside of the array range\n",
799 regno);
800 return err;
801 }
802
803 /* If we haven't set a max value then we need to bail
804 * since we can't be sure we won't do bad things.
805 */
806 if (reg->max_value == BPF_REGISTER_MAX_RANGE) {
807 verbose("R%d unbounded memory access, make sure to bounds check any array access into a map\n",
808 regno);
809 return -EACCES;
810 }
811 off += reg->max_value;
812 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700813 err = check_map_access(env, regno, off, size);
814 if (!err && t == BPF_READ && value_regno >= 0)
815 mark_reg_unknown_value(state->regs, value_regno);
816
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700817 } else if (reg->type == PTR_TO_CTX) {
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700818 enum bpf_reg_type reg_type = UNKNOWN_VALUE;
819
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700820 if (t == BPF_WRITE && value_regno >= 0 &&
821 is_pointer_value(env, value_regno)) {
822 verbose("R%d leaks addr into ctx\n", value_regno);
823 return -EACCES;
824 }
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700825 err = check_ctx_access(env, off, size, t, &reg_type);
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700826 if (!err && t == BPF_READ && value_regno >= 0) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700827 mark_reg_unknown_value(state->regs, value_regno);
Mickaël Salaün19553512016-09-24 20:01:50 +0200828 /* note that reg.[id|off|range] == 0 */
829 state->regs[value_regno].type = reg_type;
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700830 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700831
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700832 } else if (reg->type == FRAME_PTR || reg->type == PTR_TO_STACK) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700833 if (off >= 0 || off < -MAX_BPF_STACK) {
834 verbose("invalid stack off=%d size=%d\n", off, size);
835 return -EACCES;
836 }
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700837 if (t == BPF_WRITE) {
838 if (!env->allow_ptr_leaks &&
839 state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL &&
840 size != BPF_REG_SIZE) {
841 verbose("attempt to corrupt spilled pointer on stack\n");
842 return -EACCES;
843 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700844 err = check_stack_write(state, off, size, value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700845 } else {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700846 err = check_stack_read(state, off, size, value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700847 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700848 } else if (state->regs[regno].type == PTR_TO_PACKET) {
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200849 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL)) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700850 verbose("cannot write into packet\n");
851 return -EACCES;
852 }
Brenden Blanco4acf6c02016-07-19 12:16:56 -0700853 if (t == BPF_WRITE && value_regno >= 0 &&
854 is_pointer_value(env, value_regno)) {
855 verbose("R%d leaks addr into packet\n", value_regno);
856 return -EACCES;
857 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700858 err = check_packet_access(env, regno, off, size);
859 if (!err && t == BPF_READ && value_regno >= 0)
860 mark_reg_unknown_value(state->regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700861 } else {
862 verbose("R%d invalid mem access '%s'\n",
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700863 regno, reg_type_str[reg->type]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700864 return -EACCES;
865 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700866
867 if (!err && size <= 2 && value_regno >= 0 && env->allow_ptr_leaks &&
868 state->regs[value_regno].type == UNKNOWN_VALUE) {
869 /* 1 or 2 byte load zero-extends, determine the number of
870 * zero upper bits. Not doing it fo 4 byte load, since
871 * such values cannot be added to ptr_to_packet anyway.
872 */
873 state->regs[value_regno].imm = 64 - size * 8;
874 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700875 return err;
876}
877
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100878static int check_xadd(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700879{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100880 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700881 int err;
882
883 if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
884 insn->imm != 0) {
885 verbose("BPF_XADD uses reserved fields\n");
886 return -EINVAL;
887 }
888
889 /* check src1 operand */
890 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
891 if (err)
892 return err;
893
894 /* check src2 operand */
895 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
896 if (err)
897 return err;
898
Daniel Borkmanncd5de9c2017-06-29 03:04:59 +0200899 if (is_pointer_value(env, insn->src_reg)) {
900 verbose("R%d leaks addr into mem\n", insn->src_reg);
901 return -EACCES;
902 }
903
Daniel Borkmannf531fbb2018-01-29 02:49:01 +0100904 if (is_ctx_reg(env, insn->dst_reg)) {
905 verbose("BPF_XADD stores into R%d context is not allowed\n",
906 insn->dst_reg);
907 return -EACCES;
908 }
909
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700910 /* check whether atomic_add can read the memory */
911 err = check_mem_access(env, insn->dst_reg, insn->off,
912 BPF_SIZE(insn->code), BPF_READ, -1);
913 if (err)
914 return err;
915
916 /* check whether atomic_add can write into the same memory */
917 return check_mem_access(env, insn->dst_reg, insn->off,
918 BPF_SIZE(insn->code), BPF_WRITE, -1);
919}
920
921/* when register 'regno' is passed into function that will read 'access_size'
922 * bytes from that pointer, make sure that it's within stack boundary
923 * and all elements of stack are initialized
924 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100925static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
Daniel Borkmann435faee12016-04-13 00:10:51 +0200926 int access_size, bool zero_size_allowed,
927 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700928{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100929 struct bpf_verifier_state *state = &env->cur_state;
930 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700931 int off, i;
932
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +0100933 if (regs[regno].type != PTR_TO_STACK) {
934 if (zero_size_allowed && access_size == 0 &&
935 regs[regno].type == CONST_IMM &&
936 regs[regno].imm == 0)
937 return 0;
938
939 verbose("R%d type=%s expected=%s\n", regno,
940 reg_type_str[regs[regno].type],
941 reg_type_str[PTR_TO_STACK]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700942 return -EACCES;
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +0100943 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700944
945 off = regs[regno].imm;
946 if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
947 access_size <= 0) {
948 verbose("invalid stack type R%d off=%d access_size=%d\n",
949 regno, off, access_size);
950 return -EACCES;
951 }
952
Daniel Borkmann435faee12016-04-13 00:10:51 +0200953 if (meta && meta->raw_mode) {
954 meta->access_size = access_size;
955 meta->regno = regno;
956 return 0;
957 }
958
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700959 for (i = 0; i < access_size; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700960 if (state->stack_slot_type[MAX_BPF_STACK + off + i] != STACK_MISC) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700961 verbose("invalid indirect read from stack off %d+%d size %d\n",
962 off, i, access_size);
963 return -EACCES;
964 }
965 }
966 return 0;
967}
968
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100969static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200970 enum bpf_arg_type arg_type,
971 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700972{
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100973 struct bpf_reg_state *regs = env->cur_state.regs, *reg = &regs[regno];
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700974 enum bpf_reg_type expected_type, type = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700975 int err = 0;
976
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100977 if (arg_type == ARG_DONTCARE)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700978 return 0;
979
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700980 if (type == NOT_INIT) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700981 verbose("R%d !read_ok\n", regno);
982 return -EACCES;
983 }
984
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700985 if (arg_type == ARG_ANYTHING) {
986 if (is_pointer_value(env, regno)) {
987 verbose("R%d leaks addr into helper function\n", regno);
988 return -EACCES;
989 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100990 return 0;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700991 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100992
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200993 if (type == PTR_TO_PACKET && !may_access_direct_pkt_data(env, meta)) {
994 verbose("helper access to the packet is not allowed\n");
Alexei Starovoitov6841de82016-08-11 18:17:16 -0700995 return -EACCES;
996 }
997
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +0100998 if (arg_type == ARG_PTR_TO_MAP_KEY ||
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700999 arg_type == ARG_PTR_TO_MAP_VALUE) {
1000 expected_type = PTR_TO_STACK;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001001 if (type != PTR_TO_PACKET && type != expected_type)
1002 goto err_type;
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001003 } else if (arg_type == ARG_CONST_STACK_SIZE ||
1004 arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001005 expected_type = CONST_IMM;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001006 if (type != expected_type)
1007 goto err_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001008 } else if (arg_type == ARG_CONST_MAP_PTR) {
1009 expected_type = CONST_PTR_TO_MAP;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001010 if (type != expected_type)
1011 goto err_type;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001012 } else if (arg_type == ARG_PTR_TO_CTX) {
1013 expected_type = PTR_TO_CTX;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001014 if (type != expected_type)
1015 goto err_type;
Daniel Borkmann435faee12016-04-13 00:10:51 +02001016 } else if (arg_type == ARG_PTR_TO_STACK ||
1017 arg_type == ARG_PTR_TO_RAW_STACK) {
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001018 expected_type = PTR_TO_STACK;
1019 /* One exception here. In case function allows for NULL to be
1020 * passed in as argument, it's a CONST_IMM type. Final test
1021 * happens during stack boundary checking.
1022 */
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001023 if (type == CONST_IMM && reg->imm == 0)
1024 /* final test in check_stack_boundary() */;
1025 else if (type != PTR_TO_PACKET && type != expected_type)
1026 goto err_type;
Daniel Borkmann435faee12016-04-13 00:10:51 +02001027 meta->raw_mode = arg_type == ARG_PTR_TO_RAW_STACK;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001028 } else {
1029 verbose("unsupported arg_type %d\n", arg_type);
1030 return -EFAULT;
1031 }
1032
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001033 if (arg_type == ARG_CONST_MAP_PTR) {
1034 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001035 meta->map_ptr = reg->map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001036 } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
1037 /* bpf_map_xxx(..., map_ptr, ..., key) call:
1038 * check that [key, key + map->key_size) are within
1039 * stack limits and initialized
1040 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001041 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001042 /* in function declaration map_ptr must come before
1043 * map_key, so that it's verified and known before
1044 * we have to check map_key here. Otherwise it means
1045 * that kernel subsystem misconfigured verifier
1046 */
1047 verbose("invalid map_ptr to access map->key\n");
1048 return -EACCES;
1049 }
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001050 if (type == PTR_TO_PACKET)
1051 err = check_packet_access(env, regno, 0,
1052 meta->map_ptr->key_size);
1053 else
1054 err = check_stack_boundary(env, regno,
1055 meta->map_ptr->key_size,
1056 false, NULL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001057 } else if (arg_type == ARG_PTR_TO_MAP_VALUE) {
1058 /* bpf_map_xxx(..., map_ptr, ..., value) call:
1059 * check [value, value + map->value_size) validity
1060 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001061 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001062 /* kernel subsystem misconfigured verifier */
1063 verbose("invalid map_ptr to access map->value\n");
1064 return -EACCES;
1065 }
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001066 if (type == PTR_TO_PACKET)
1067 err = check_packet_access(env, regno, 0,
1068 meta->map_ptr->value_size);
1069 else
1070 err = check_stack_boundary(env, regno,
1071 meta->map_ptr->value_size,
1072 false, NULL);
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001073 } else if (arg_type == ARG_CONST_STACK_SIZE ||
1074 arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) {
1075 bool zero_size_allowed = (arg_type == ARG_CONST_STACK_SIZE_OR_ZERO);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001076
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001077 /* bpf_xxx(..., buf, len) call will access 'len' bytes
1078 * from stack pointer 'buf'. Check it
1079 * note: regno == len, regno - 1 == buf
1080 */
1081 if (regno == 0) {
1082 /* kernel subsystem misconfigured verifier */
1083 verbose("ARG_CONST_STACK_SIZE cannot be first argument\n");
1084 return -EACCES;
1085 }
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001086 if (regs[regno - 1].type == PTR_TO_PACKET)
1087 err = check_packet_access(env, regno - 1, 0, reg->imm);
1088 else
1089 err = check_stack_boundary(env, regno - 1, reg->imm,
1090 zero_size_allowed, meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001091 }
1092
1093 return err;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001094err_type:
1095 verbose("R%d type=%s expected=%s\n", regno,
1096 reg_type_str[type], reg_type_str[expected_type]);
1097 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001098}
1099
Kaixu Xia35578d72015-08-06 07:02:35 +00001100static int check_map_func_compatibility(struct bpf_map *map, int func_id)
1101{
Kaixu Xia35578d72015-08-06 07:02:35 +00001102 if (!map)
1103 return 0;
1104
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001105 /* We need a two way check, first is from map perspective ... */
1106 switch (map->map_type) {
1107 case BPF_MAP_TYPE_PROG_ARRAY:
1108 if (func_id != BPF_FUNC_tail_call)
1109 goto error;
1110 break;
1111 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
1112 if (func_id != BPF_FUNC_perf_event_read &&
1113 func_id != BPF_FUNC_perf_event_output)
1114 goto error;
1115 break;
1116 case BPF_MAP_TYPE_STACK_TRACE:
1117 if (func_id != BPF_FUNC_get_stackid)
1118 goto error;
1119 break;
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -07001120 case BPF_MAP_TYPE_CGROUP_ARRAY:
David S. Miller60747ef2016-08-18 01:17:32 -04001121 if (func_id != BPF_FUNC_skb_under_cgroup &&
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001122 func_id != BPF_FUNC_current_task_under_cgroup)
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07001123 goto error;
1124 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001125 default:
1126 break;
1127 }
1128
1129 /* ... and second from the function itself. */
1130 switch (func_id) {
1131 case BPF_FUNC_tail_call:
1132 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1133 goto error;
1134 break;
1135 case BPF_FUNC_perf_event_read:
1136 case BPF_FUNC_perf_event_output:
1137 if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
1138 goto error;
1139 break;
1140 case BPF_FUNC_get_stackid:
1141 if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
1142 goto error;
1143 break;
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001144 case BPF_FUNC_current_task_under_cgroup:
Daniel Borkmann747ea552016-08-12 22:17:17 +02001145 case BPF_FUNC_skb_under_cgroup:
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07001146 if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
1147 goto error;
1148 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001149 default:
1150 break;
Kaixu Xia35578d72015-08-06 07:02:35 +00001151 }
1152
1153 return 0;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001154error:
1155 verbose("cannot pass map_type %d into func %d\n",
1156 map->map_type, func_id);
1157 return -EINVAL;
Kaixu Xia35578d72015-08-06 07:02:35 +00001158}
1159
Daniel Borkmann435faee12016-04-13 00:10:51 +02001160static int check_raw_mode(const struct bpf_func_proto *fn)
1161{
1162 int count = 0;
1163
1164 if (fn->arg1_type == ARG_PTR_TO_RAW_STACK)
1165 count++;
1166 if (fn->arg2_type == ARG_PTR_TO_RAW_STACK)
1167 count++;
1168 if (fn->arg3_type == ARG_PTR_TO_RAW_STACK)
1169 count++;
1170 if (fn->arg4_type == ARG_PTR_TO_RAW_STACK)
1171 count++;
1172 if (fn->arg5_type == ARG_PTR_TO_RAW_STACK)
1173 count++;
1174
1175 return count > 1 ? -EINVAL : 0;
1176}
1177
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001178static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001179{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001180 struct bpf_verifier_state *state = &env->cur_state;
1181 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001182 int i;
1183
1184 for (i = 0; i < MAX_BPF_REG; i++)
1185 if (regs[i].type == PTR_TO_PACKET ||
1186 regs[i].type == PTR_TO_PACKET_END)
1187 mark_reg_unknown_value(regs, i);
1188
1189 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
1190 if (state->stack_slot_type[i] != STACK_SPILL)
1191 continue;
1192 reg = &state->spilled_regs[i / BPF_REG_SIZE];
1193 if (reg->type != PTR_TO_PACKET &&
1194 reg->type != PTR_TO_PACKET_END)
1195 continue;
1196 reg->type = UNKNOWN_VALUE;
1197 reg->imm = 0;
1198 }
1199}
1200
Alexei Starovoitova9bfac142018-01-07 17:33:02 -08001201static int check_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001202{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001203 struct bpf_verifier_state *state = &env->cur_state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001204 const struct bpf_func_proto *fn = NULL;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001205 struct bpf_reg_state *regs = state->regs;
1206 struct bpf_reg_state *reg;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001207 struct bpf_call_arg_meta meta;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001208 bool changes_data;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001209 int i, err;
1210
1211 /* find function prototype */
1212 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
1213 verbose("invalid func %d\n", func_id);
1214 return -EINVAL;
1215 }
1216
1217 if (env->prog->aux->ops->get_func_proto)
1218 fn = env->prog->aux->ops->get_func_proto(func_id);
1219
1220 if (!fn) {
1221 verbose("unknown func %d\n", func_id);
1222 return -EINVAL;
1223 }
1224
1225 /* eBPF programs must be GPL compatible to use GPL-ed functions */
Daniel Borkmann24701ec2015-03-01 12:31:47 +01001226 if (!env->prog->gpl_compatible && fn->gpl_only) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001227 verbose("cannot call GPL only function from proprietary program\n");
1228 return -EINVAL;
1229 }
1230
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001231 changes_data = bpf_helper_changes_skb_data(fn->func);
1232
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001233 memset(&meta, 0, sizeof(meta));
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001234 meta.pkt_access = fn->pkt_access;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001235
Daniel Borkmann435faee12016-04-13 00:10:51 +02001236 /* We only support one arg being in raw mode at the moment, which
1237 * is sufficient for the helper functions we have right now.
1238 */
1239 err = check_raw_mode(fn);
1240 if (err) {
1241 verbose("kernel subsystem misconfigured func %d\n", func_id);
1242 return err;
1243 }
1244
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001245 /* check args */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001246 err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001247 if (err)
1248 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001249 err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001250 if (err)
1251 return err;
Alexei Starovoitova9bfac142018-01-07 17:33:02 -08001252 if (func_id == BPF_FUNC_tail_call) {
1253 if (meta.map_ptr == NULL) {
1254 verbose("verifier bug\n");
1255 return -EINVAL;
1256 }
1257 env->insn_aux_data[insn_idx].map_ptr = meta.map_ptr;
1258 }
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001259 err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001260 if (err)
1261 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001262 err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001263 if (err)
1264 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001265 err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001266 if (err)
1267 return err;
1268
Daniel Borkmann435faee12016-04-13 00:10:51 +02001269 /* Mark slots with STACK_MISC in case of raw mode, stack offset
1270 * is inferred from register state.
1271 */
1272 for (i = 0; i < meta.access_size; i++) {
1273 err = check_mem_access(env, meta.regno, i, BPF_B, BPF_WRITE, -1);
1274 if (err)
1275 return err;
1276 }
1277
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001278 /* reset caller saved regs */
1279 for (i = 0; i < CALLER_SAVED_REGS; i++) {
1280 reg = regs + caller_saved[i];
1281 reg->type = NOT_INIT;
1282 reg->imm = 0;
1283 }
1284
1285 /* update return register */
1286 if (fn->ret_type == RET_INTEGER) {
1287 regs[BPF_REG_0].type = UNKNOWN_VALUE;
1288 } else if (fn->ret_type == RET_VOID) {
1289 regs[BPF_REG_0].type = NOT_INIT;
1290 } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) {
1291 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
Josef Bacik48461132016-09-28 10:54:32 -04001292 regs[BPF_REG_0].max_value = regs[BPF_REG_0].min_value = 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001293 /* remember map_ptr, so that check_map_access()
1294 * can check 'value_size' boundary of memory access
1295 * to map element returned from bpf_map_lookup_elem()
1296 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001297 if (meta.map_ptr == NULL) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001298 verbose("kernel subsystem misconfigured verifier\n");
1299 return -EINVAL;
1300 }
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001301 regs[BPF_REG_0].map_ptr = meta.map_ptr;
Thomas Graf14117072016-10-18 19:51:19 +02001302 regs[BPF_REG_0].id = ++env->id_gen;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001303 } else {
1304 verbose("unknown return type %d of func %d\n",
1305 fn->ret_type, func_id);
1306 return -EINVAL;
1307 }
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -07001308
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001309 err = check_map_func_compatibility(meta.map_ptr, func_id);
Kaixu Xia35578d72015-08-06 07:02:35 +00001310 if (err)
1311 return err;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -07001312
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001313 if (changes_data)
1314 clear_all_pkt_pointers(env);
1315 return 0;
1316}
1317
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001318static int check_packet_ptr_add(struct bpf_verifier_env *env,
1319 struct bpf_insn *insn)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001320{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001321 struct bpf_reg_state *regs = env->cur_state.regs;
1322 struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
1323 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
1324 struct bpf_reg_state tmp_reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001325 s32 imm;
1326
1327 if (BPF_SRC(insn->code) == BPF_K) {
1328 /* pkt_ptr += imm */
1329 imm = insn->imm;
1330
1331add_imm:
1332 if (imm <= 0) {
1333 verbose("addition of negative constant to packet pointer is not allowed\n");
1334 return -EACCES;
1335 }
1336 if (imm >= MAX_PACKET_OFF ||
1337 imm + dst_reg->off >= MAX_PACKET_OFF) {
1338 verbose("constant %d is too large to add to packet pointer\n",
1339 imm);
1340 return -EACCES;
1341 }
1342 /* a constant was added to pkt_ptr.
1343 * Remember it while keeping the same 'id'
1344 */
1345 dst_reg->off += imm;
1346 } else {
Alexei Starovoitov1b9b69e2016-05-19 18:17:14 -07001347 if (src_reg->type == PTR_TO_PACKET) {
1348 /* R6=pkt(id=0,off=0,r=62) R7=imm22; r7 += r6 */
1349 tmp_reg = *dst_reg; /* save r7 state */
1350 *dst_reg = *src_reg; /* copy pkt_ptr state r6 into r7 */
1351 src_reg = &tmp_reg; /* pretend it's src_reg state */
1352 /* if the checks below reject it, the copy won't matter,
1353 * since we're rejecting the whole program. If all ok,
1354 * then imm22 state will be added to r7
1355 * and r7 will be pkt(id=0,off=22,r=62) while
1356 * r6 will stay as pkt(id=0,off=0,r=62)
1357 */
1358 }
1359
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001360 if (src_reg->type == CONST_IMM) {
1361 /* pkt_ptr += reg where reg is known constant */
1362 imm = src_reg->imm;
1363 goto add_imm;
1364 }
1365 /* disallow pkt_ptr += reg
1366 * if reg is not uknown_value with guaranteed zero upper bits
1367 * otherwise pkt_ptr may overflow and addition will become
1368 * subtraction which is not allowed
1369 */
1370 if (src_reg->type != UNKNOWN_VALUE) {
1371 verbose("cannot add '%s' to ptr_to_packet\n",
1372 reg_type_str[src_reg->type]);
1373 return -EACCES;
1374 }
1375 if (src_reg->imm < 48) {
1376 verbose("cannot add integer value with %lld upper zero bits to ptr_to_packet\n",
1377 src_reg->imm);
1378 return -EACCES;
1379 }
1380 /* dst_reg stays as pkt_ptr type and since some positive
1381 * integer value was added to the pointer, increment its 'id'
1382 */
Jakub Kicinski1f415a72016-08-02 16:12:14 +01001383 dst_reg->id = ++env->id_gen;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001384
1385 /* something was added to pkt_ptr, set range and off to zero */
1386 dst_reg->off = 0;
1387 dst_reg->range = 0;
1388 }
1389 return 0;
1390}
1391
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001392static int evaluate_reg_alu(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001393{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001394 struct bpf_reg_state *regs = env->cur_state.regs;
1395 struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001396 u8 opcode = BPF_OP(insn->code);
1397 s64 imm_log2;
1398
1399 /* for type == UNKNOWN_VALUE:
1400 * imm > 0 -> number of zero upper bits
1401 * imm == 0 -> don't track which is the same as all bits can be non-zero
1402 */
1403
1404 if (BPF_SRC(insn->code) == BPF_X) {
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001405 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001406
1407 if (src_reg->type == UNKNOWN_VALUE && src_reg->imm > 0 &&
1408 dst_reg->imm && opcode == BPF_ADD) {
1409 /* dreg += sreg
1410 * where both have zero upper bits. Adding them
1411 * can only result making one more bit non-zero
1412 * in the larger value.
1413 * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47)
1414 * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47)
1415 */
1416 dst_reg->imm = min(dst_reg->imm, src_reg->imm);
1417 dst_reg->imm--;
1418 return 0;
1419 }
1420 if (src_reg->type == CONST_IMM && src_reg->imm > 0 &&
1421 dst_reg->imm && opcode == BPF_ADD) {
1422 /* dreg += sreg
1423 * where dreg has zero upper bits and sreg is const.
1424 * Adding them can only result making one more bit
1425 * non-zero in the larger value.
1426 */
1427 imm_log2 = __ilog2_u64((long long)src_reg->imm);
1428 dst_reg->imm = min(dst_reg->imm, 63 - imm_log2);
1429 dst_reg->imm--;
1430 return 0;
1431 }
1432 /* all other cases non supported yet, just mark dst_reg */
1433 dst_reg->imm = 0;
1434 return 0;
1435 }
1436
1437 /* sign extend 32-bit imm into 64-bit to make sure that
1438 * negative values occupy bit 63. Note ilog2() would have
1439 * been incorrect, since sizeof(insn->imm) == 4
1440 */
1441 imm_log2 = __ilog2_u64((long long)insn->imm);
1442
1443 if (dst_reg->imm && opcode == BPF_LSH) {
1444 /* reg <<= imm
1445 * if reg was a result of 2 byte load, then its imm == 48
1446 * which means that upper 48 bits are zero and shifting this reg
1447 * left by 4 would mean that upper 44 bits are still zero
1448 */
1449 dst_reg->imm -= insn->imm;
1450 } else if (dst_reg->imm && opcode == BPF_MUL) {
1451 /* reg *= imm
1452 * if multiplying by 14 subtract 4
1453 * This is conservative calculation of upper zero bits.
1454 * It's not trying to special case insn->imm == 1 or 0 cases
1455 */
1456 dst_reg->imm -= imm_log2 + 1;
1457 } else if (opcode == BPF_AND) {
1458 /* reg &= imm */
1459 dst_reg->imm = 63 - imm_log2;
1460 } else if (dst_reg->imm && opcode == BPF_ADD) {
1461 /* reg += imm */
1462 dst_reg->imm = min(dst_reg->imm, 63 - imm_log2);
1463 dst_reg->imm--;
1464 } else if (opcode == BPF_RSH) {
1465 /* reg >>= imm
1466 * which means that after right shift, upper bits will be zero
1467 * note that verifier already checked that
1468 * 0 <= imm < 64 for shift insn
1469 */
1470 dst_reg->imm += insn->imm;
1471 if (unlikely(dst_reg->imm > 64))
1472 /* some dumb code did:
1473 * r2 = *(u32 *)mem;
1474 * r2 >>= 32;
1475 * and all bits are zero now */
1476 dst_reg->imm = 64;
1477 } else {
1478 /* all other alu ops, means that we don't know what will
1479 * happen to the value, mark it with unknown number of zero bits
1480 */
1481 dst_reg->imm = 0;
1482 }
1483
1484 if (dst_reg->imm < 0) {
1485 /* all 64 bits of the register can contain non-zero bits
1486 * and such value cannot be added to ptr_to_packet, since it
1487 * may overflow, mark it as unknown to avoid further eval
1488 */
1489 dst_reg->imm = 0;
1490 }
1491 return 0;
1492}
1493
John Fastabende37bdee2017-07-02 02:13:30 +02001494static int evaluate_reg_imm_alu_unknown(struct bpf_verifier_env *env,
1495 struct bpf_insn *insn)
1496{
1497 struct bpf_reg_state *regs = env->cur_state.regs;
1498 struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
1499 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
1500 u8 opcode = BPF_OP(insn->code);
1501 s64 imm_log2 = __ilog2_u64((long long)dst_reg->imm);
1502
1503 /* BPF_X code with src_reg->type UNKNOWN_VALUE here. */
1504 if (src_reg->imm > 0 && dst_reg->imm) {
1505 switch (opcode) {
1506 case BPF_ADD:
1507 /* dreg += sreg
1508 * where both have zero upper bits. Adding them
1509 * can only result making one more bit non-zero
1510 * in the larger value.
1511 * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47)
1512 * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47)
1513 */
1514 dst_reg->imm = min(src_reg->imm, 63 - imm_log2);
1515 dst_reg->imm--;
1516 break;
1517 case BPF_AND:
1518 /* dreg &= sreg
1519 * AND can not extend zero bits only shrink
1520 * Ex. 0x00..00ffffff
1521 * & 0x0f..ffffffff
1522 * ----------------
1523 * 0x00..00ffffff
1524 */
1525 dst_reg->imm = max(src_reg->imm, 63 - imm_log2);
1526 break;
1527 case BPF_OR:
1528 /* dreg |= sreg
1529 * OR can only extend zero bits
1530 * Ex. 0x00..00ffffff
1531 * | 0x0f..ffffffff
1532 * ----------------
1533 * 0x0f..00ffffff
1534 */
1535 dst_reg->imm = min(src_reg->imm, 63 - imm_log2);
1536 break;
1537 case BPF_SUB:
1538 case BPF_MUL:
1539 case BPF_RSH:
1540 case BPF_LSH:
1541 /* These may be flushed out later */
1542 default:
1543 mark_reg_unknown_value(regs, insn->dst_reg);
1544 }
1545 } else {
1546 mark_reg_unknown_value(regs, insn->dst_reg);
1547 }
1548
1549 dst_reg->type = UNKNOWN_VALUE;
1550 return 0;
1551}
1552
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001553static int evaluate_reg_imm_alu(struct bpf_verifier_env *env,
1554 struct bpf_insn *insn)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001555{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001556 struct bpf_reg_state *regs = env->cur_state.regs;
1557 struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
1558 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001559 u8 opcode = BPF_OP(insn->code);
1560
John Fastabende37bdee2017-07-02 02:13:30 +02001561 if (BPF_SRC(insn->code) == BPF_X && src_reg->type == UNKNOWN_VALUE)
1562 return evaluate_reg_imm_alu_unknown(env, insn);
1563
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001564 /* dst_reg->type == CONST_IMM here, simulate execution of 'add' insn.
1565 * Don't care about overflow or negative values, just add them
1566 */
1567 if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_K)
1568 dst_reg->imm += insn->imm;
1569 else if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_X &&
1570 src_reg->type == CONST_IMM)
1571 dst_reg->imm += src_reg->imm;
1572 else
1573 mark_reg_unknown_value(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001574 return 0;
1575}
1576
Josef Bacik48461132016-09-28 10:54:32 -04001577static void check_reg_overflow(struct bpf_reg_state *reg)
1578{
1579 if (reg->max_value > BPF_REGISTER_MAX_RANGE)
1580 reg->max_value = BPF_REGISTER_MAX_RANGE;
Josef Bacikf23cc642016-11-14 15:45:36 -05001581 if (reg->min_value < BPF_REGISTER_MIN_RANGE ||
1582 reg->min_value > BPF_REGISTER_MAX_RANGE)
Josef Bacik48461132016-09-28 10:54:32 -04001583 reg->min_value = BPF_REGISTER_MIN_RANGE;
1584}
1585
1586static void adjust_reg_min_max_vals(struct bpf_verifier_env *env,
1587 struct bpf_insn *insn)
1588{
1589 struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg;
Josef Bacikf23cc642016-11-14 15:45:36 -05001590 s64 min_val = BPF_REGISTER_MIN_RANGE;
1591 u64 max_val = BPF_REGISTER_MAX_RANGE;
Josef Bacik48461132016-09-28 10:54:32 -04001592 bool min_set = false, max_set = false;
1593 u8 opcode = BPF_OP(insn->code);
1594
1595 dst_reg = &regs[insn->dst_reg];
1596 if (BPF_SRC(insn->code) == BPF_X) {
1597 check_reg_overflow(&regs[insn->src_reg]);
1598 min_val = regs[insn->src_reg].min_value;
1599 max_val = regs[insn->src_reg].max_value;
1600
1601 /* If the source register is a random pointer then the
1602 * min_value/max_value values represent the range of the known
1603 * accesses into that value, not the actual min/max value of the
1604 * register itself. In this case we have to reset the reg range
1605 * values so we know it is not safe to look at.
1606 */
1607 if (regs[insn->src_reg].type != CONST_IMM &&
1608 regs[insn->src_reg].type != UNKNOWN_VALUE) {
1609 min_val = BPF_REGISTER_MIN_RANGE;
1610 max_val = BPF_REGISTER_MAX_RANGE;
1611 }
1612 } else if (insn->imm < BPF_REGISTER_MAX_RANGE &&
1613 (s64)insn->imm > BPF_REGISTER_MIN_RANGE) {
1614 min_val = max_val = insn->imm;
1615 min_set = max_set = true;
1616 }
1617
1618 /* We don't know anything about what was done to this register, mark it
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02001619 * as unknown. Also, if both derived bounds came from signed/unsigned
1620 * mixed compares and one side is unbounded, we cannot really do anything
1621 * with them as boundaries cannot be trusted. Thus, arithmetic of two
1622 * regs of such kind will get invalidated bounds on the dst side.
Josef Bacik48461132016-09-28 10:54:32 -04001623 */
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02001624 if ((min_val == BPF_REGISTER_MIN_RANGE &&
1625 max_val == BPF_REGISTER_MAX_RANGE) ||
1626 (BPF_SRC(insn->code) == BPF_X &&
1627 ((min_val != BPF_REGISTER_MIN_RANGE &&
1628 max_val == BPF_REGISTER_MAX_RANGE) ||
1629 (min_val == BPF_REGISTER_MIN_RANGE &&
1630 max_val != BPF_REGISTER_MAX_RANGE) ||
1631 (dst_reg->min_value != BPF_REGISTER_MIN_RANGE &&
1632 dst_reg->max_value == BPF_REGISTER_MAX_RANGE) ||
1633 (dst_reg->min_value == BPF_REGISTER_MIN_RANGE &&
1634 dst_reg->max_value != BPF_REGISTER_MAX_RANGE)) &&
1635 regs[insn->dst_reg].value_from_signed !=
1636 regs[insn->src_reg].value_from_signed)) {
Josef Bacik48461132016-09-28 10:54:32 -04001637 reset_reg_range_values(regs, insn->dst_reg);
1638 return;
1639 }
1640
Josef Bacikf23cc642016-11-14 15:45:36 -05001641 /* If one of our values was at the end of our ranges then we can't just
1642 * do our normal operations to the register, we need to set the values
1643 * to the min/max since they are undefined.
1644 */
Edward Cree655da3d2017-07-21 14:37:34 +01001645 if (opcode != BPF_SUB) {
1646 if (min_val == BPF_REGISTER_MIN_RANGE)
1647 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
1648 if (max_val == BPF_REGISTER_MAX_RANGE)
1649 dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
1650 }
Josef Bacikf23cc642016-11-14 15:45:36 -05001651
Josef Bacik48461132016-09-28 10:54:32 -04001652 switch (opcode) {
1653 case BPF_ADD:
Josef Bacikf23cc642016-11-14 15:45:36 -05001654 if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
1655 dst_reg->min_value += min_val;
1656 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1657 dst_reg->max_value += max_val;
Josef Bacik48461132016-09-28 10:54:32 -04001658 break;
1659 case BPF_SUB:
Edward Cree655da3d2017-07-21 14:37:34 +01001660 /* If one of our values was at the end of our ranges, then the
1661 * _opposite_ value in the dst_reg goes to the end of our range.
1662 */
1663 if (min_val == BPF_REGISTER_MIN_RANGE)
1664 dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
1665 if (max_val == BPF_REGISTER_MAX_RANGE)
1666 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
Josef Bacikf23cc642016-11-14 15:45:36 -05001667 if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
Edward Cree655da3d2017-07-21 14:37:34 +01001668 dst_reg->min_value -= max_val;
Josef Bacikf23cc642016-11-14 15:45:36 -05001669 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
Edward Cree655da3d2017-07-21 14:37:34 +01001670 dst_reg->max_value -= min_val;
Josef Bacik48461132016-09-28 10:54:32 -04001671 break;
1672 case BPF_MUL:
Josef Bacikf23cc642016-11-14 15:45:36 -05001673 if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
1674 dst_reg->min_value *= min_val;
1675 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1676 dst_reg->max_value *= max_val;
Josef Bacik48461132016-09-28 10:54:32 -04001677 break;
1678 case BPF_AND:
Josef Bacikf23cc642016-11-14 15:45:36 -05001679 /* Disallow AND'ing of negative numbers, ain't nobody got time
1680 * for that. Otherwise the minimum is 0 and the max is the max
1681 * value we could AND against.
1682 */
1683 if (min_val < 0)
1684 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
1685 else
1686 dst_reg->min_value = 0;
Josef Bacik48461132016-09-28 10:54:32 -04001687 dst_reg->max_value = max_val;
1688 break;
1689 case BPF_LSH:
1690 /* Gotta have special overflow logic here, if we're shifting
1691 * more than MAX_RANGE then just assume we have an invalid
1692 * range.
1693 */
1694 if (min_val > ilog2(BPF_REGISTER_MAX_RANGE))
1695 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
Josef Bacikf23cc642016-11-14 15:45:36 -05001696 else if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
Josef Bacik48461132016-09-28 10:54:32 -04001697 dst_reg->min_value <<= min_val;
1698
1699 if (max_val > ilog2(BPF_REGISTER_MAX_RANGE))
1700 dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
Josef Bacikf23cc642016-11-14 15:45:36 -05001701 else if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
Josef Bacik48461132016-09-28 10:54:32 -04001702 dst_reg->max_value <<= max_val;
1703 break;
1704 case BPF_RSH:
Josef Bacikf23cc642016-11-14 15:45:36 -05001705 /* RSH by a negative number is undefined, and the BPF_RSH is an
1706 * unsigned shift, so make the appropriate casts.
Josef Bacik48461132016-09-28 10:54:32 -04001707 */
Josef Bacikf23cc642016-11-14 15:45:36 -05001708 if (min_val < 0 || dst_reg->min_value < 0)
1709 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
1710 else
1711 dst_reg->min_value =
1712 (u64)(dst_reg->min_value) >> min_val;
1713 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1714 dst_reg->max_value >>= max_val;
Josef Bacik48461132016-09-28 10:54:32 -04001715 break;
1716 default:
1717 reset_reg_range_values(regs, insn->dst_reg);
1718 break;
1719 }
1720
1721 check_reg_overflow(dst_reg);
1722}
1723
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001724/* check validity of 32-bit and 64-bit arithmetic operations */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001725static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001726{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001727 struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001728 u8 opcode = BPF_OP(insn->code);
1729 int err;
1730
1731 if (opcode == BPF_END || opcode == BPF_NEG) {
1732 if (opcode == BPF_NEG) {
1733 if (BPF_SRC(insn->code) != 0 ||
1734 insn->src_reg != BPF_REG_0 ||
1735 insn->off != 0 || insn->imm != 0) {
1736 verbose("BPF_NEG uses reserved fields\n");
1737 return -EINVAL;
1738 }
1739 } else {
1740 if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
Edward Creee1594922017-09-15 14:37:38 +01001741 (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) ||
1742 BPF_CLASS(insn->code) == BPF_ALU64) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001743 verbose("BPF_END uses reserved fields\n");
1744 return -EINVAL;
1745 }
1746 }
1747
1748 /* check src operand */
1749 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
1750 if (err)
1751 return err;
1752
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001753 if (is_pointer_value(env, insn->dst_reg)) {
1754 verbose("R%d pointer arithmetic prohibited\n",
1755 insn->dst_reg);
1756 return -EACCES;
1757 }
1758
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001759 /* check dest operand */
1760 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
1761 if (err)
1762 return err;
1763
1764 } else if (opcode == BPF_MOV) {
1765
1766 if (BPF_SRC(insn->code) == BPF_X) {
1767 if (insn->imm != 0 || insn->off != 0) {
1768 verbose("BPF_MOV uses reserved fields\n");
1769 return -EINVAL;
1770 }
1771
1772 /* check src operand */
1773 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1774 if (err)
1775 return err;
1776 } else {
1777 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
1778 verbose("BPF_MOV uses reserved fields\n");
1779 return -EINVAL;
1780 }
1781 }
1782
1783 /* check dest operand */
1784 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
1785 if (err)
1786 return err;
1787
Josef Bacik48461132016-09-28 10:54:32 -04001788 /* we are setting our register to something new, we need to
1789 * reset its range values.
1790 */
1791 reset_reg_range_values(regs, insn->dst_reg);
1792
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001793 if (BPF_SRC(insn->code) == BPF_X) {
1794 if (BPF_CLASS(insn->code) == BPF_ALU64) {
1795 /* case: R1 = R2
1796 * copy register state to dest reg
1797 */
1798 regs[insn->dst_reg] = regs[insn->src_reg];
1799 } else {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001800 if (is_pointer_value(env, insn->src_reg)) {
1801 verbose("R%d partial copy of pointer\n",
1802 insn->src_reg);
1803 return -EACCES;
1804 }
Thomas Graf14117072016-10-18 19:51:19 +02001805 mark_reg_unknown_value(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001806 }
1807 } else {
1808 /* case: R = imm
1809 * remember the value we stored into this reg
1810 */
Daniel Borkmann3695b3b2017-12-22 16:29:05 +01001811 u64 imm;
1812
1813 if (BPF_CLASS(insn->code) == BPF_ALU64)
1814 imm = insn->imm;
1815 else
1816 imm = (u32)insn->imm;
1817
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001818 regs[insn->dst_reg].type = CONST_IMM;
Daniel Borkmann3695b3b2017-12-22 16:29:05 +01001819 regs[insn->dst_reg].imm = imm;
1820 regs[insn->dst_reg].max_value = imm;
1821 regs[insn->dst_reg].min_value = imm;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001822 }
1823
1824 } else if (opcode > BPF_END) {
1825 verbose("invalid BPF_ALU opcode %x\n", opcode);
1826 return -EINVAL;
1827
1828 } else { /* all other ALU ops: and, sub, xor, add, ... */
1829
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001830 if (BPF_SRC(insn->code) == BPF_X) {
1831 if (insn->imm != 0 || insn->off != 0) {
1832 verbose("BPF_ALU uses reserved fields\n");
1833 return -EINVAL;
1834 }
1835 /* check src1 operand */
1836 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1837 if (err)
1838 return err;
1839 } else {
1840 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
1841 verbose("BPF_ALU uses reserved fields\n");
1842 return -EINVAL;
1843 }
1844 }
1845
1846 /* check src2 operand */
1847 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
1848 if (err)
1849 return err;
1850
1851 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
1852 BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
1853 verbose("div by zero\n");
1854 return -EINVAL;
1855 }
1856
Daniel Borkmannfcabc6d2018-01-29 02:48:57 +01001857 if (opcode == BPF_ARSH && BPF_CLASS(insn->code) != BPF_ALU64) {
1858 verbose("BPF_ARSH not supported for 32 bit ALU\n");
1859 return -EINVAL;
1860 }
1861
Rabin Vincent229394e2016-01-12 20:17:08 +01001862 if ((opcode == BPF_LSH || opcode == BPF_RSH ||
1863 opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
1864 int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
1865
1866 if (insn->imm < 0 || insn->imm >= size) {
1867 verbose("invalid shift %d\n", insn->imm);
1868 return -EINVAL;
1869 }
1870 }
1871
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07001872 /* check dest operand */
1873 err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
1874 if (err)
1875 return err;
1876
1877 dst_reg = &regs[insn->dst_reg];
1878
Josef Bacik48461132016-09-28 10:54:32 -04001879 /* first we want to adjust our ranges. */
1880 adjust_reg_min_max_vals(env, insn);
1881
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001882 /* pattern match 'bpf_add Rx, imm' instruction */
1883 if (opcode == BPF_ADD && BPF_CLASS(insn->code) == BPF_ALU64 &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07001884 dst_reg->type == FRAME_PTR && BPF_SRC(insn->code) == BPF_K) {
1885 dst_reg->type = PTR_TO_STACK;
1886 dst_reg->imm = insn->imm;
1887 return 0;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001888 } else if (opcode == BPF_ADD &&
1889 BPF_CLASS(insn->code) == BPF_ALU64 &&
Yonghong Song7bca0a92017-04-29 22:52:42 -07001890 dst_reg->type == PTR_TO_STACK &&
1891 ((BPF_SRC(insn->code) == BPF_X &&
1892 regs[insn->src_reg].type == CONST_IMM) ||
1893 BPF_SRC(insn->code) == BPF_K)) {
Daniel Borkmannd75d3ee2017-12-22 16:29:04 +01001894 if (BPF_SRC(insn->code) == BPF_X) {
1895 /* check in case the register contains a big
1896 * 64-bit value
1897 */
1898 if (regs[insn->src_reg].imm < -MAX_BPF_STACK ||
1899 regs[insn->src_reg].imm > MAX_BPF_STACK) {
1900 verbose("R%d value too big in R%d pointer arithmetic\n",
1901 insn->src_reg, insn->dst_reg);
1902 return -EACCES;
1903 }
Yonghong Song7bca0a92017-04-29 22:52:42 -07001904 dst_reg->imm += regs[insn->src_reg].imm;
Daniel Borkmannd75d3ee2017-12-22 16:29:04 +01001905 } else {
1906 /* safe against overflow: addition of 32-bit
1907 * numbers in 64-bit representation
1908 */
Yonghong Song7bca0a92017-04-29 22:52:42 -07001909 dst_reg->imm += insn->imm;
Daniel Borkmannd75d3ee2017-12-22 16:29:04 +01001910 }
1911 if (dst_reg->imm > 0 || dst_reg->imm < -MAX_BPF_STACK) {
1912 verbose("R%d out-of-bounds pointer arithmetic\n",
1913 insn->dst_reg);
1914 return -EACCES;
1915 }
Yonghong Song7bca0a92017-04-29 22:52:42 -07001916 return 0;
1917 } else if (opcode == BPF_ADD &&
1918 BPF_CLASS(insn->code) == BPF_ALU64 &&
Alexei Starovoitov1b9b69e2016-05-19 18:17:14 -07001919 (dst_reg->type == PTR_TO_PACKET ||
1920 (BPF_SRC(insn->code) == BPF_X &&
1921 regs[insn->src_reg].type == PTR_TO_PACKET))) {
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001922 /* ptr_to_packet += K|X */
1923 return check_packet_ptr_add(env, insn);
1924 } else if (BPF_CLASS(insn->code) == BPF_ALU64 &&
1925 dst_reg->type == UNKNOWN_VALUE &&
1926 env->allow_ptr_leaks) {
1927 /* unknown += K|X */
1928 return evaluate_reg_alu(env, insn);
1929 } else if (BPF_CLASS(insn->code) == BPF_ALU64 &&
1930 dst_reg->type == CONST_IMM &&
1931 env->allow_ptr_leaks) {
1932 /* reg_imm += K|X */
1933 return evaluate_reg_imm_alu(env, insn);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001934 } else if (is_pointer_value(env, insn->dst_reg)) {
1935 verbose("R%d pointer arithmetic prohibited\n",
1936 insn->dst_reg);
1937 return -EACCES;
1938 } else if (BPF_SRC(insn->code) == BPF_X &&
1939 is_pointer_value(env, insn->src_reg)) {
1940 verbose("R%d pointer arithmetic prohibited\n",
1941 insn->src_reg);
1942 return -EACCES;
1943 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001944
Josef Bacik48461132016-09-28 10:54:32 -04001945 /* If we did pointer math on a map value then just set it to our
1946 * PTR_TO_MAP_VALUE_ADJ type so we can deal with any stores or
1947 * loads to this register appropriately, otherwise just mark the
1948 * register as unknown.
1949 */
1950 if (env->allow_ptr_leaks &&
Daniel Borkmann8d674be2017-03-31 02:24:02 +02001951 BPF_CLASS(insn->code) == BPF_ALU64 && opcode == BPF_ADD &&
Josef Bacik48461132016-09-28 10:54:32 -04001952 (dst_reg->type == PTR_TO_MAP_VALUE ||
1953 dst_reg->type == PTR_TO_MAP_VALUE_ADJ))
1954 dst_reg->type = PTR_TO_MAP_VALUE_ADJ;
1955 else
1956 mark_reg_unknown_value(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001957 }
1958
1959 return 0;
1960}
1961
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001962static void find_good_pkt_pointers(struct bpf_verifier_state *state,
1963 struct bpf_reg_state *dst_reg)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001964{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001965 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001966 int i;
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02001967
1968 /* LLVM can generate two kind of checks:
1969 *
1970 * Type 1:
1971 *
1972 * r2 = r3;
1973 * r2 += 8;
1974 * if (r2 > pkt_end) goto <handle exception>
1975 * <access okay>
1976 *
1977 * Where:
1978 * r2 == dst_reg, pkt_end == src_reg
1979 * r2=pkt(id=n,off=8,r=0)
1980 * r3=pkt(id=n,off=0,r=0)
1981 *
1982 * Type 2:
1983 *
1984 * r2 = r3;
1985 * r2 += 8;
1986 * if (pkt_end >= r2) goto <access okay>
1987 * <handle exception>
1988 *
1989 * Where:
1990 * pkt_end == dst_reg, r2 == src_reg
1991 * r2=pkt(id=n,off=8,r=0)
1992 * r3=pkt(id=n,off=0,r=0)
1993 *
1994 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
1995 * so that range of bytes [r3, r3 + 8) is safe to access.
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001996 */
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02001997
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001998 for (i = 0; i < MAX_BPF_REG; i++)
1999 if (regs[i].type == PTR_TO_PACKET && regs[i].id == dst_reg->id)
Alexei Starovoitov0ea3c232017-03-24 15:57:33 -07002000 /* keep the maximum range already checked */
2001 regs[i].range = max(regs[i].range, dst_reg->off);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002002
2003 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
2004 if (state->stack_slot_type[i] != STACK_SPILL)
2005 continue;
2006 reg = &state->spilled_regs[i / BPF_REG_SIZE];
2007 if (reg->type == PTR_TO_PACKET && reg->id == dst_reg->id)
Alexei Starovoitov0ea3c232017-03-24 15:57:33 -07002008 reg->range = max(reg->range, dst_reg->off);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002009 }
2010}
2011
Josef Bacik48461132016-09-28 10:54:32 -04002012/* Adjusts the register min/max values in the case that the dst_reg is the
2013 * variable register that we are working on, and src_reg is a constant or we're
2014 * simply doing a BPF_K check.
2015 */
2016static void reg_set_min_max(struct bpf_reg_state *true_reg,
2017 struct bpf_reg_state *false_reg, u64 val,
2018 u8 opcode)
2019{
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002020 bool value_from_signed = true;
2021 bool is_range = true;
2022
Josef Bacik48461132016-09-28 10:54:32 -04002023 switch (opcode) {
2024 case BPF_JEQ:
2025 /* If this is false then we know nothing Jon Snow, but if it is
2026 * true then we know for sure.
2027 */
2028 true_reg->max_value = true_reg->min_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002029 is_range = false;
Josef Bacik48461132016-09-28 10:54:32 -04002030 break;
2031 case BPF_JNE:
2032 /* If this is true we know nothing Jon Snow, but if it is false
2033 * we know the value for sure;
2034 */
2035 false_reg->max_value = false_reg->min_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002036 is_range = false;
Josef Bacik48461132016-09-28 10:54:32 -04002037 break;
2038 case BPF_JGT:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002039 value_from_signed = false;
2040 /* fallthrough */
Josef Bacik48461132016-09-28 10:54:32 -04002041 case BPF_JSGT:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002042 if (true_reg->value_from_signed != value_from_signed)
2043 reset_reg_range_values(true_reg, 0);
2044 if (false_reg->value_from_signed != value_from_signed)
2045 reset_reg_range_values(false_reg, 0);
2046 if (opcode == BPF_JGT) {
2047 /* Unsigned comparison, the minimum value is 0. */
2048 false_reg->min_value = 0;
2049 }
Josef Bacik48461132016-09-28 10:54:32 -04002050 /* If this is false then we know the maximum val is val,
2051 * otherwise we know the min val is val+1.
2052 */
2053 false_reg->max_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002054 false_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002055 true_reg->min_value = val + 1;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002056 true_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002057 break;
2058 case BPF_JGE:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002059 value_from_signed = false;
2060 /* fallthrough */
Josef Bacik48461132016-09-28 10:54:32 -04002061 case BPF_JSGE:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002062 if (true_reg->value_from_signed != value_from_signed)
2063 reset_reg_range_values(true_reg, 0);
2064 if (false_reg->value_from_signed != value_from_signed)
2065 reset_reg_range_values(false_reg, 0);
2066 if (opcode == BPF_JGE) {
2067 /* Unsigned comparison, the minimum value is 0. */
2068 false_reg->min_value = 0;
2069 }
Josef Bacik48461132016-09-28 10:54:32 -04002070 /* If this is false then we know the maximum value is val - 1,
2071 * otherwise we know the mimimum value is val.
2072 */
2073 false_reg->max_value = val - 1;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002074 false_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002075 true_reg->min_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002076 true_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002077 break;
2078 default:
2079 break;
2080 }
2081
2082 check_reg_overflow(false_reg);
2083 check_reg_overflow(true_reg);
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002084 if (is_range) {
2085 if (__is_pointer_value(false, false_reg))
2086 reset_reg_range_values(false_reg, 0);
2087 if (__is_pointer_value(false, true_reg))
2088 reset_reg_range_values(true_reg, 0);
2089 }
Josef Bacik48461132016-09-28 10:54:32 -04002090}
2091
2092/* Same as above, but for the case that dst_reg is a CONST_IMM reg and src_reg
2093 * is the variable reg.
2094 */
2095static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
2096 struct bpf_reg_state *false_reg, u64 val,
2097 u8 opcode)
2098{
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002099 bool value_from_signed = true;
2100 bool is_range = true;
2101
Josef Bacik48461132016-09-28 10:54:32 -04002102 switch (opcode) {
2103 case BPF_JEQ:
2104 /* If this is false then we know nothing Jon Snow, but if it is
2105 * true then we know for sure.
2106 */
2107 true_reg->max_value = true_reg->min_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002108 is_range = false;
Josef Bacik48461132016-09-28 10:54:32 -04002109 break;
2110 case BPF_JNE:
2111 /* If this is true we know nothing Jon Snow, but if it is false
2112 * we know the value for sure;
2113 */
2114 false_reg->max_value = false_reg->min_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002115 is_range = false;
Josef Bacik48461132016-09-28 10:54:32 -04002116 break;
2117 case BPF_JGT:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002118 value_from_signed = false;
2119 /* fallthrough */
Josef Bacik48461132016-09-28 10:54:32 -04002120 case BPF_JSGT:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002121 if (true_reg->value_from_signed != value_from_signed)
2122 reset_reg_range_values(true_reg, 0);
2123 if (false_reg->value_from_signed != value_from_signed)
2124 reset_reg_range_values(false_reg, 0);
2125 if (opcode == BPF_JGT) {
2126 /* Unsigned comparison, the minimum value is 0. */
2127 true_reg->min_value = 0;
2128 }
Josef Bacik48461132016-09-28 10:54:32 -04002129 /*
2130 * If this is false, then the val is <= the register, if it is
2131 * true the register <= to the val.
2132 */
2133 false_reg->min_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002134 false_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002135 true_reg->max_value = val - 1;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002136 true_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002137 break;
2138 case BPF_JGE:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002139 value_from_signed = false;
2140 /* fallthrough */
Josef Bacik48461132016-09-28 10:54:32 -04002141 case BPF_JSGE:
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002142 if (true_reg->value_from_signed != value_from_signed)
2143 reset_reg_range_values(true_reg, 0);
2144 if (false_reg->value_from_signed != value_from_signed)
2145 reset_reg_range_values(false_reg, 0);
2146 if (opcode == BPF_JGE) {
2147 /* Unsigned comparison, the minimum value is 0. */
2148 true_reg->min_value = 0;
2149 }
Josef Bacik48461132016-09-28 10:54:32 -04002150 /* If this is false then constant < register, if it is true then
2151 * the register < constant.
2152 */
2153 false_reg->min_value = val + 1;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002154 false_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002155 true_reg->max_value = val;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002156 true_reg->value_from_signed = value_from_signed;
Josef Bacik48461132016-09-28 10:54:32 -04002157 break;
2158 default:
2159 break;
2160 }
2161
2162 check_reg_overflow(false_reg);
2163 check_reg_overflow(true_reg);
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +02002164 if (is_range) {
2165 if (__is_pointer_value(false, false_reg))
2166 reset_reg_range_values(false_reg, 0);
2167 if (__is_pointer_value(false, true_reg))
2168 reset_reg_range_values(true_reg, 0);
2169 }
Josef Bacik48461132016-09-28 10:54:32 -04002170}
2171
Thomas Graf14117072016-10-18 19:51:19 +02002172static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id,
2173 enum bpf_reg_type type)
2174{
2175 struct bpf_reg_state *reg = &regs[regno];
2176
2177 if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) {
2178 reg->type = type;
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01002179 /* We don't need id from this point onwards anymore, thus we
2180 * should better reset it, so that state pruning has chances
2181 * to take effect.
2182 */
2183 reg->id = 0;
Thomas Graf14117072016-10-18 19:51:19 +02002184 if (type == UNKNOWN_VALUE)
Daniel Borkmann0e0f1d62016-12-18 01:52:59 +01002185 __mark_reg_unknown_value(regs, regno);
Thomas Graf14117072016-10-18 19:51:19 +02002186 }
2187}
2188
2189/* The logic is similar to find_good_pkt_pointers(), both could eventually
2190 * be folded together at some point.
2191 */
2192static void mark_map_regs(struct bpf_verifier_state *state, u32 regno,
2193 enum bpf_reg_type type)
2194{
2195 struct bpf_reg_state *regs = state->regs;
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01002196 u32 id = regs[regno].id;
Thomas Graf14117072016-10-18 19:51:19 +02002197 int i;
2198
2199 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01002200 mark_map_reg(regs, i, id, type);
Thomas Graf14117072016-10-18 19:51:19 +02002201
2202 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
2203 if (state->stack_slot_type[i] != STACK_SPILL)
2204 continue;
Daniel Borkmann1889d6d2016-12-15 01:30:06 +01002205 mark_map_reg(state->spilled_regs, i / BPF_REG_SIZE, id, type);
Thomas Graf14117072016-10-18 19:51:19 +02002206 }
2207}
2208
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002209static int check_cond_jmp_op(struct bpf_verifier_env *env,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002210 struct bpf_insn *insn, int *insn_idx)
2211{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002212 struct bpf_verifier_state *other_branch, *this_branch = &env->cur_state;
2213 struct bpf_reg_state *regs = this_branch->regs, *dst_reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002214 u8 opcode = BPF_OP(insn->code);
2215 int err;
2216
2217 if (opcode > BPF_EXIT) {
2218 verbose("invalid BPF_JMP opcode %x\n", opcode);
2219 return -EINVAL;
2220 }
2221
2222 if (BPF_SRC(insn->code) == BPF_X) {
2223 if (insn->imm != 0) {
2224 verbose("BPF_JMP uses reserved fields\n");
2225 return -EINVAL;
2226 }
2227
2228 /* check src1 operand */
2229 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2230 if (err)
2231 return err;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002232
2233 if (is_pointer_value(env, insn->src_reg)) {
2234 verbose("R%d pointer comparison prohibited\n",
2235 insn->src_reg);
2236 return -EACCES;
2237 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002238 } else {
2239 if (insn->src_reg != BPF_REG_0) {
2240 verbose("BPF_JMP uses reserved fields\n");
2241 return -EINVAL;
2242 }
2243 }
2244
2245 /* check src2 operand */
2246 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
2247 if (err)
2248 return err;
2249
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002250 dst_reg = &regs[insn->dst_reg];
2251
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002252 /* detect if R == 0 where R was initialized to zero earlier */
2253 if (BPF_SRC(insn->code) == BPF_K &&
2254 (opcode == BPF_JEQ || opcode == BPF_JNE) &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002255 dst_reg->type == CONST_IMM && dst_reg->imm == insn->imm) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002256 if (opcode == BPF_JEQ) {
2257 /* if (imm == imm) goto pc+off;
2258 * only follow the goto, ignore fall-through
2259 */
2260 *insn_idx += insn->off;
2261 return 0;
2262 } else {
2263 /* if (imm != imm) goto pc+off;
2264 * only follow fall-through branch, since
2265 * that's where the program will go
2266 */
2267 return 0;
2268 }
2269 }
2270
2271 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx);
2272 if (!other_branch)
2273 return -EFAULT;
2274
Josef Bacik48461132016-09-28 10:54:32 -04002275 /* detect if we are comparing against a constant value so we can adjust
2276 * our min/max values for our dst register.
2277 */
2278 if (BPF_SRC(insn->code) == BPF_X) {
2279 if (regs[insn->src_reg].type == CONST_IMM)
2280 reg_set_min_max(&other_branch->regs[insn->dst_reg],
2281 dst_reg, regs[insn->src_reg].imm,
2282 opcode);
2283 else if (dst_reg->type == CONST_IMM)
2284 reg_set_min_max_inv(&other_branch->regs[insn->src_reg],
2285 &regs[insn->src_reg], dst_reg->imm,
2286 opcode);
2287 } else {
2288 reg_set_min_max(&other_branch->regs[insn->dst_reg],
2289 dst_reg, insn->imm, opcode);
2290 }
2291
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002292 /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002293 if (BPF_SRC(insn->code) == BPF_K &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002294 insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
2295 dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
Thomas Graf14117072016-10-18 19:51:19 +02002296 /* Mark all identical map registers in each branch as either
2297 * safe or unknown depending R == 0 or R != 0 conditional.
2298 */
2299 mark_map_regs(this_branch, insn->dst_reg,
2300 opcode == BPF_JEQ ? PTR_TO_MAP_VALUE : UNKNOWN_VALUE);
2301 mark_map_regs(other_branch, insn->dst_reg,
2302 opcode == BPF_JEQ ? UNKNOWN_VALUE : PTR_TO_MAP_VALUE);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002303 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT &&
2304 dst_reg->type == PTR_TO_PACKET &&
2305 regs[insn->src_reg].type == PTR_TO_PACKET_END) {
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002306 find_good_pkt_pointers(this_branch, dst_reg);
2307 } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGE &&
2308 dst_reg->type == PTR_TO_PACKET_END &&
2309 regs[insn->src_reg].type == PTR_TO_PACKET) {
2310 find_good_pkt_pointers(other_branch, &regs[insn->src_reg]);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002311 } else if (is_pointer_value(env, insn->dst_reg)) {
2312 verbose("R%d pointer comparison prohibited\n", insn->dst_reg);
2313 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002314 }
2315 if (log_level)
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02002316 print_verifier_state(this_branch);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002317 return 0;
2318}
2319
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002320/* return the map pointer stored inside BPF_LD_IMM64 instruction */
2321static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
2322{
2323 u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
2324
2325 return (struct bpf_map *) (unsigned long) imm64;
2326}
2327
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002328/* verify BPF_LD_IMM64 instruction */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002329static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002330{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002331 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002332 int err;
2333
2334 if (BPF_SIZE(insn->code) != BPF_DW) {
2335 verbose("invalid BPF_LD_IMM insn\n");
2336 return -EINVAL;
2337 }
2338 if (insn->off != 0) {
2339 verbose("BPF_LD_IMM64 uses reserved fields\n");
2340 return -EINVAL;
2341 }
2342
2343 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
2344 if (err)
2345 return err;
2346
Jakub Kicinski6b173872016-09-21 11:43:59 +01002347 if (insn->src_reg == 0) {
2348 /* generic move 64-bit immediate into a register,
2349 * only analyzer needs to collect the ld_imm value.
2350 */
2351 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
2352
2353 if (!env->analyzer_ops)
2354 return 0;
2355
2356 regs[insn->dst_reg].type = CONST_IMM;
2357 regs[insn->dst_reg].imm = imm;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002358 return 0;
Jakub Kicinski6b173872016-09-21 11:43:59 +01002359 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002360
2361 /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
2362 BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
2363
2364 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
2365 regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
2366 return 0;
2367}
2368
Daniel Borkmann96be4322015-03-01 12:31:46 +01002369static bool may_access_skb(enum bpf_prog_type type)
2370{
2371 switch (type) {
2372 case BPF_PROG_TYPE_SOCKET_FILTER:
2373 case BPF_PROG_TYPE_SCHED_CLS:
Daniel Borkmann94caee82015-03-20 15:11:11 +01002374 case BPF_PROG_TYPE_SCHED_ACT:
Daniel Borkmann96be4322015-03-01 12:31:46 +01002375 return true;
2376 default:
2377 return false;
2378 }
2379}
2380
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002381/* verify safety of LD_ABS|LD_IND instructions:
2382 * - they can only appear in the programs where ctx == skb
2383 * - since they are wrappers of function calls, they scratch R1-R5 registers,
2384 * preserve R6-R9, and store return value into R0
2385 *
2386 * Implicit input:
2387 * ctx == skb == R6 == CTX
2388 *
2389 * Explicit input:
2390 * SRC == any register
2391 * IMM == 32-bit immediate
2392 *
2393 * Output:
2394 * R0 - 8/16/32-bit skb data converted to cpu endianness
2395 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002396static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002397{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002398 struct bpf_reg_state *regs = env->cur_state.regs;
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002399 u8 mode = BPF_MODE(insn->code);
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002400 struct bpf_reg_state *reg;
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002401 int i, err;
2402
Daniel Borkmann24701ec2015-03-01 12:31:47 +01002403 if (!may_access_skb(env->prog->type)) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002404 verbose("BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002405 return -EINVAL;
2406 }
2407
2408 if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
Alexei Starovoitovd82bccc2016-04-12 10:26:19 -07002409 BPF_SIZE(insn->code) == BPF_DW ||
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002410 (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002411 verbose("BPF_LD_[ABS|IND] uses reserved fields\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08002412 return -EINVAL;
2413 }
2414
2415 /* check whether implicit source operand (register R6) is readable */
2416 err = check_reg_arg(regs, BPF_REG_6, SRC_OP);
2417 if (err)
2418 return err;
2419
2420 if (regs[BPF_REG_6].type != PTR_TO_CTX) {
2421 verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
2422 return -EINVAL;
2423 }
2424
2425 if (mode == BPF_IND) {
2426 /* check explicit source operand */
2427 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2428 if (err)
2429 return err;
2430 }
2431
2432 /* reset caller saved regs to unreadable */
2433 for (i = 0; i < CALLER_SAVED_REGS; i++) {
2434 reg = regs + caller_saved[i];
2435 reg->type = NOT_INIT;
2436 reg->imm = 0;
2437 }
2438
2439 /* mark destination R0 register as readable, since it contains
2440 * the value fetched from the packet
2441 */
2442 regs[BPF_REG_0].type = UNKNOWN_VALUE;
2443 return 0;
2444}
2445
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002446/* non-recursive DFS pseudo code
2447 * 1 procedure DFS-iterative(G,v):
2448 * 2 label v as discovered
2449 * 3 let S be a stack
2450 * 4 S.push(v)
2451 * 5 while S is not empty
2452 * 6 t <- S.pop()
2453 * 7 if t is what we're looking for:
2454 * 8 return t
2455 * 9 for all edges e in G.adjacentEdges(t) do
2456 * 10 if edge e is already labelled
2457 * 11 continue with the next edge
2458 * 12 w <- G.adjacentVertex(t,e)
2459 * 13 if vertex w is not discovered and not explored
2460 * 14 label e as tree-edge
2461 * 15 label w as discovered
2462 * 16 S.push(w)
2463 * 17 continue at 5
2464 * 18 else if vertex w is discovered
2465 * 19 label e as back-edge
2466 * 20 else
2467 * 21 // vertex w is explored
2468 * 22 label e as forward- or cross-edge
2469 * 23 label t as explored
2470 * 24 S.pop()
2471 *
2472 * convention:
2473 * 0x10 - discovered
2474 * 0x11 - discovered and fall-through edge labelled
2475 * 0x12 - discovered and fall-through and branch edges labelled
2476 * 0x20 - explored
2477 */
2478
2479enum {
2480 DISCOVERED = 0x10,
2481 EXPLORED = 0x20,
2482 FALLTHROUGH = 1,
2483 BRANCH = 2,
2484};
2485
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002486#define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002487
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002488static int *insn_stack; /* stack of insns to process */
2489static int cur_stack; /* current stack index */
2490static int *insn_state;
2491
2492/* t, w, e - match pseudo-code above:
2493 * t - index of current instruction
2494 * w - next instruction
2495 * e - edge
2496 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002497static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002498{
2499 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
2500 return 0;
2501
2502 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
2503 return 0;
2504
2505 if (w < 0 || w >= env->prog->len) {
2506 verbose("jump out of range from insn %d to %d\n", t, w);
2507 return -EINVAL;
2508 }
2509
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002510 if (e == BRANCH)
2511 /* mark branch target for state pruning */
2512 env->explored_states[w] = STATE_LIST_MARK;
2513
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002514 if (insn_state[w] == 0) {
2515 /* tree-edge */
2516 insn_state[t] = DISCOVERED | e;
2517 insn_state[w] = DISCOVERED;
2518 if (cur_stack >= env->prog->len)
2519 return -E2BIG;
2520 insn_stack[cur_stack++] = w;
2521 return 1;
2522 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
2523 verbose("back-edge from insn %d to %d\n", t, w);
2524 return -EINVAL;
2525 } else if (insn_state[w] == EXPLORED) {
2526 /* forward- or cross-edge */
2527 insn_state[t] = DISCOVERED | e;
2528 } else {
2529 verbose("insn state internal bug\n");
2530 return -EFAULT;
2531 }
2532 return 0;
2533}
2534
2535/* non-recursive depth-first-search to detect loops in BPF program
2536 * loop == back-edge in directed graph
2537 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002538static int check_cfg(struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002539{
2540 struct bpf_insn *insns = env->prog->insnsi;
2541 int insn_cnt = env->prog->len;
2542 int ret = 0;
2543 int i, t;
2544
2545 insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
2546 if (!insn_state)
2547 return -ENOMEM;
2548
2549 insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
2550 if (!insn_stack) {
2551 kfree(insn_state);
2552 return -ENOMEM;
2553 }
2554
2555 insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
2556 insn_stack[0] = 0; /* 0 is the first instruction */
2557 cur_stack = 1;
2558
2559peek_stack:
2560 if (cur_stack == 0)
2561 goto check_state;
2562 t = insn_stack[cur_stack - 1];
2563
2564 if (BPF_CLASS(insns[t].code) == BPF_JMP) {
2565 u8 opcode = BPF_OP(insns[t].code);
2566
2567 if (opcode == BPF_EXIT) {
2568 goto mark_explored;
2569 } else if (opcode == BPF_CALL) {
2570 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2571 if (ret == 1)
2572 goto peek_stack;
2573 else if (ret < 0)
2574 goto err_free;
Daniel Borkmann07016152016-04-05 22:33:17 +02002575 if (t + 1 < insn_cnt)
2576 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002577 } else if (opcode == BPF_JA) {
2578 if (BPF_SRC(insns[t].code) != BPF_K) {
2579 ret = -EINVAL;
2580 goto err_free;
2581 }
2582 /* unconditional jump with single edge */
2583 ret = push_insn(t, t + insns[t].off + 1,
2584 FALLTHROUGH, env);
2585 if (ret == 1)
2586 goto peek_stack;
2587 else if (ret < 0)
2588 goto err_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002589 /* tell verifier to check for equivalent states
2590 * after every call and jump
2591 */
Alexei Starovoitovc3de6312015-04-14 15:57:13 -07002592 if (t + 1 < insn_cnt)
2593 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002594 } else {
2595 /* conditional jump with two edges */
Daniel Borkmann577aa832017-05-18 03:00:06 +02002596 env->explored_states[t] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002597 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2598 if (ret == 1)
2599 goto peek_stack;
2600 else if (ret < 0)
2601 goto err_free;
2602
2603 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
2604 if (ret == 1)
2605 goto peek_stack;
2606 else if (ret < 0)
2607 goto err_free;
2608 }
2609 } else {
2610 /* all other non-branch instructions with single
2611 * fall-through edge
2612 */
2613 ret = push_insn(t, t + 1, FALLTHROUGH, env);
2614 if (ret == 1)
2615 goto peek_stack;
2616 else if (ret < 0)
2617 goto err_free;
2618 }
2619
2620mark_explored:
2621 insn_state[t] = EXPLORED;
2622 if (cur_stack-- <= 0) {
2623 verbose("pop stack internal bug\n");
2624 ret = -EFAULT;
2625 goto err_free;
2626 }
2627 goto peek_stack;
2628
2629check_state:
2630 for (i = 0; i < insn_cnt; i++) {
2631 if (insn_state[i] != EXPLORED) {
2632 verbose("unreachable insn %d\n", i);
2633 ret = -EINVAL;
2634 goto err_free;
2635 }
2636 }
2637 ret = 0; /* cfg looks good */
2638
2639err_free:
2640 kfree(insn_state);
2641 kfree(insn_stack);
2642 return ret;
2643}
2644
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002645/* the following conditions reduce the number of explored insns
2646 * from ~140k to ~80k for ultra large programs that use a lot of ptr_to_packet
2647 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002648static bool compare_ptrs_to_packet(struct bpf_reg_state *old,
2649 struct bpf_reg_state *cur)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002650{
2651 if (old->id != cur->id)
2652 return false;
2653
2654 /* old ptr_to_packet is more conservative, since it allows smaller
2655 * range. Ex:
2656 * old(off=0,r=10) is equal to cur(off=0,r=20), because
2657 * old(off=0,r=10) means that with range=10 the verifier proceeded
2658 * further and found no issues with the program. Now we're in the same
2659 * spot with cur(off=0,r=20), so we're safe too, since anything further
2660 * will only be looking at most 10 bytes after this pointer.
2661 */
2662 if (old->off == cur->off && old->range < cur->range)
2663 return true;
2664
2665 /* old(off=20,r=10) is equal to cur(off=22,re=22 or 5 or 0)
2666 * since both cannot be used for packet access and safe(old)
2667 * pointer has smaller off that could be used for further
2668 * 'if (ptr > data_end)' check
2669 * Ex:
2670 * old(off=20,r=10) and cur(off=22,r=22) and cur(off=22,r=0) mean
2671 * that we cannot access the packet.
2672 * The safe range is:
2673 * [ptr, ptr + range - off)
2674 * so whenever off >=range, it means no safe bytes from this pointer.
2675 * When comparing old->off <= cur->off, it means that older code
2676 * went with smaller offset and that offset was later
2677 * used to figure out the safe range after 'if (ptr > data_end)' check
2678 * Say, 'old' state was explored like:
2679 * ... R3(off=0, r=0)
2680 * R4 = R3 + 20
2681 * ... now R4(off=20,r=0) <-- here
2682 * if (R4 > data_end)
2683 * ... R4(off=20,r=20), R3(off=0,r=20) and R3 can be used to access.
2684 * ... the code further went all the way to bpf_exit.
2685 * Now the 'cur' state at the mark 'here' has R4(off=30,r=0).
2686 * old_R4(off=20,r=0) equal to cur_R4(off=30,r=0), since if the verifier
2687 * goes further, such cur_R4 will give larger safe packet range after
2688 * 'if (R4 > data_end)' and all further insn were already good with r=20,
2689 * so they will be good with r=30 and we can prune the search.
2690 */
2691 if (old->off <= cur->off &&
2692 old->off >= old->range && cur->off >= cur->range)
2693 return true;
2694
2695 return false;
2696}
2697
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002698/* compare two verifier states
2699 *
2700 * all states stored in state_list are known to be valid, since
2701 * verifier reached 'bpf_exit' instruction through them
2702 *
2703 * this function is called when verifier exploring different branches of
2704 * execution popped from the state stack. If it sees an old state that has
2705 * more strict register state and more strict stack state then this execution
2706 * branch doesn't need to be explored further, since verifier already
2707 * concluded that more strict state leads to valid finish.
2708 *
2709 * Therefore two states are equivalent if register state is more conservative
2710 * and explored stack state is more conservative than the current one.
2711 * Example:
2712 * explored current
2713 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
2714 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
2715 *
2716 * In other words if current stack state (one being explored) has more
2717 * valid slots than old one that already passed validation, it means
2718 * the verifier can stop exploring and conclude that current state is valid too
2719 *
2720 * Similarly with registers. If explored state has register type as invalid
2721 * whereas register type in current state is meaningful, it means that
2722 * the current state will reach 'bpf_exit' instruction safely
2723 */
Josef Bacik48461132016-09-28 10:54:32 -04002724static bool states_equal(struct bpf_verifier_env *env,
2725 struct bpf_verifier_state *old,
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002726 struct bpf_verifier_state *cur)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002727{
Josef Bacike2d2afe2016-11-29 12:27:09 -05002728 bool varlen_map_access = env->varlen_map_value_access;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002729 struct bpf_reg_state *rold, *rcur;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002730 int i;
2731
2732 for (i = 0; i < MAX_BPF_REG; i++) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002733 rold = &old->regs[i];
2734 rcur = &cur->regs[i];
2735
2736 if (memcmp(rold, rcur, sizeof(*rold)) == 0)
2737 continue;
2738
Josef Bacik48461132016-09-28 10:54:32 -04002739 /* If the ranges were not the same, but everything else was and
2740 * we didn't do a variable access into a map then we are a-ok.
2741 */
Josef Bacike2d2afe2016-11-29 12:27:09 -05002742 if (!varlen_map_access &&
Alexei Starovoitovb7f5aa12016-12-07 10:57:59 -08002743 memcmp(rold, rcur, offsetofend(struct bpf_reg_state, id)) == 0)
Josef Bacik48461132016-09-28 10:54:32 -04002744 continue;
2745
Josef Bacike2d2afe2016-11-29 12:27:09 -05002746 /* If we didn't map access then again we don't care about the
2747 * mismatched range values and it's ok if our old type was
Ben Hutchings37435f72017-12-23 02:26:17 +00002748 * UNKNOWN and we didn't go to a NOT_INIT'ed or pointer reg.
Josef Bacike2d2afe2016-11-29 12:27:09 -05002749 */
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002750 if (rold->type == NOT_INIT ||
Josef Bacike2d2afe2016-11-29 12:27:09 -05002751 (!varlen_map_access && rold->type == UNKNOWN_VALUE &&
Ben Hutchings37435f72017-12-23 02:26:17 +00002752 rcur->type != NOT_INIT &&
2753 !__is_pointer_value(env->allow_ptr_leaks, rcur)))
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002754 continue;
2755
Daniel Borkmann577aa832017-05-18 03:00:06 +02002756 /* Don't care about the reg->id in this case. */
2757 if (rold->type == PTR_TO_MAP_VALUE_OR_NULL &&
2758 rcur->type == PTR_TO_MAP_VALUE_OR_NULL &&
2759 rold->map_ptr == rcur->map_ptr)
2760 continue;
2761
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002762 if (rold->type == PTR_TO_PACKET && rcur->type == PTR_TO_PACKET &&
2763 compare_ptrs_to_packet(rold, rcur))
2764 continue;
2765
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002766 return false;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002767 }
2768
2769 for (i = 0; i < MAX_BPF_STACK; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002770 if (old->stack_slot_type[i] == STACK_INVALID)
2771 continue;
2772 if (old->stack_slot_type[i] != cur->stack_slot_type[i])
2773 /* Ex: old explored (safe) state has STACK_SPILL in
2774 * this stack slot, but current has has STACK_MISC ->
2775 * this verifier states are not equivalent,
2776 * return false to continue verification of this path
2777 */
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002778 return false;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002779 if (i % BPF_REG_SIZE)
2780 continue;
2781 if (memcmp(&old->spilled_regs[i / BPF_REG_SIZE],
2782 &cur->spilled_regs[i / BPF_REG_SIZE],
2783 sizeof(old->spilled_regs[0])))
2784 /* when explored and current stack slot types are
2785 * the same, check that stored pointers types
2786 * are the same as well.
2787 * Ex: explored safe path could have stored
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002788 * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -8}
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002789 * but current path has stored:
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002790 * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -16}
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002791 * such verifier states are not equivalent.
2792 * return false to continue verification of this path
2793 */
2794 return false;
2795 else
2796 continue;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002797 }
2798 return true;
2799}
2800
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002801static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002802{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002803 struct bpf_verifier_state_list *new_sl;
2804 struct bpf_verifier_state_list *sl;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002805
2806 sl = env->explored_states[insn_idx];
2807 if (!sl)
2808 /* this 'insn_idx' instruction wasn't marked, so we will not
2809 * be doing state search here
2810 */
2811 return 0;
2812
2813 while (sl != STATE_LIST_MARK) {
Josef Bacik48461132016-09-28 10:54:32 -04002814 if (states_equal(env, &sl->state, &env->cur_state))
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002815 /* reached equivalent register/stack state,
2816 * prune the search
2817 */
2818 return 1;
2819 sl = sl->next;
2820 }
2821
2822 /* there were no equivalent states, remember current one.
2823 * technically the current state is not proven to be safe yet,
2824 * but it will either reach bpf_exit (which means it's safe) or
2825 * it will be rejected. Since there are no loops, we won't be
2826 * seeing this 'insn_idx' instruction again on the way to bpf_exit
2827 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002828 new_sl = kmalloc(sizeof(struct bpf_verifier_state_list), GFP_USER);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002829 if (!new_sl)
2830 return -ENOMEM;
2831
2832 /* add new state to the head of linked list */
2833 memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state));
2834 new_sl->next = env->explored_states[insn_idx];
2835 env->explored_states[insn_idx] = new_sl;
2836 return 0;
2837}
2838
Jakub Kicinski13a27df2016-09-21 11:43:58 +01002839static int ext_analyzer_insn_hook(struct bpf_verifier_env *env,
2840 int insn_idx, int prev_insn_idx)
2841{
2842 if (!env->analyzer_ops || !env->analyzer_ops->insn_hook)
2843 return 0;
2844
2845 return env->analyzer_ops->insn_hook(env, insn_idx, prev_insn_idx);
2846}
2847
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002848static int do_check(struct bpf_verifier_env *env)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002849{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002850 struct bpf_verifier_state *state = &env->cur_state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002851 struct bpf_insn *insns = env->prog->insnsi;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01002852 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002853 int insn_cnt = env->prog->len;
2854 int insn_idx, prev_insn_idx = 0;
2855 int insn_processed = 0;
2856 bool do_print_state = false;
2857
2858 init_reg_state(regs);
2859 insn_idx = 0;
Josef Bacik48461132016-09-28 10:54:32 -04002860 env->varlen_map_value_access = false;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002861 for (;;) {
2862 struct bpf_insn *insn;
2863 u8 class;
2864 int err;
2865
2866 if (insn_idx >= insn_cnt) {
2867 verbose("invalid insn idx %d insn_cnt %d\n",
2868 insn_idx, insn_cnt);
2869 return -EFAULT;
2870 }
2871
2872 insn = &insns[insn_idx];
2873 class = BPF_CLASS(insn->code);
2874
Daniel Borkmann07016152016-04-05 22:33:17 +02002875 if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002876 verbose("BPF program is too large. Proccessed %d insn\n",
2877 insn_processed);
2878 return -E2BIG;
2879 }
2880
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002881 err = is_state_visited(env, insn_idx);
2882 if (err < 0)
2883 return err;
2884 if (err == 1) {
2885 /* found equivalent state, can prune the search */
2886 if (log_level) {
2887 if (do_print_state)
2888 verbose("\nfrom %d to %d: safe\n",
2889 prev_insn_idx, insn_idx);
2890 else
2891 verbose("%d: safe\n", insn_idx);
2892 }
2893 goto process_bpf_exit;
2894 }
2895
Daniel Borkmann577aa832017-05-18 03:00:06 +02002896 if (need_resched())
2897 cond_resched();
2898
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002899 if (log_level && do_print_state) {
2900 verbose("\nfrom %d to %d:", prev_insn_idx, insn_idx);
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002901 print_verifier_state(&env->cur_state);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002902 do_print_state = false;
2903 }
2904
2905 if (log_level) {
2906 verbose("%d: ", insn_idx);
Daniel Borkmannced0a312017-05-08 00:04:09 +02002907 print_bpf_insn(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002908 }
2909
Jakub Kicinski13a27df2016-09-21 11:43:58 +01002910 err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx);
2911 if (err)
2912 return err;
2913
Daniel Borkmann7b5b73e2017-12-22 16:29:03 +01002914 env->insn_aux_data[insn_idx].seen = true;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002915 if (class == BPF_ALU || class == BPF_ALU64) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002916 err = check_alu_op(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002917 if (err)
2918 return err;
2919
2920 } else if (class == BPF_LDX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002921 enum bpf_reg_type *prev_src_type, src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002922
2923 /* check for reserved fields is already done */
2924
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002925 /* check src operand */
2926 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2927 if (err)
2928 return err;
2929
2930 err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
2931 if (err)
2932 return err;
2933
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07002934 src_reg_type = regs[insn->src_reg].type;
2935
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002936 /* check that memory (src_reg + off) is readable,
2937 * the state of dst_reg will be updated by this func
2938 */
2939 err = check_mem_access(env, insn->src_reg, insn->off,
2940 BPF_SIZE(insn->code), BPF_READ,
2941 insn->dst_reg);
2942 if (err)
2943 return err;
2944
Josef Bacik48461132016-09-28 10:54:32 -04002945 reset_reg_range_values(regs, insn->dst_reg);
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07002946 if (BPF_SIZE(insn->code) != BPF_W &&
2947 BPF_SIZE(insn->code) != BPF_DW) {
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07002948 insn_idx++;
2949 continue;
2950 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002951
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002952 prev_src_type = &env->insn_aux_data[insn_idx].ptr_type;
2953
2954 if (*prev_src_type == NOT_INIT) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002955 /* saw a valid insn
2956 * dst_reg = *(u32 *)(src_reg + off)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002957 * save type to validate intersecting paths
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002958 */
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002959 *prev_src_type = src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002960
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002961 } else if (src_reg_type != *prev_src_type &&
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002962 (src_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002963 *prev_src_type == PTR_TO_CTX)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002964 /* ABuser program is trying to use the same insn
2965 * dst_reg = *(u32*) (src_reg + off)
2966 * with different pointer types:
2967 * src_reg == ctx in one branch and
2968 * src_reg == stack|map in some other branch.
2969 * Reject it.
2970 */
2971 verbose("same insn cannot be used with different pointers\n");
2972 return -EINVAL;
2973 }
2974
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002975 } else if (class == BPF_STX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01002976 enum bpf_reg_type *prev_dst_type, dst_reg_type;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002977
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002978 if (BPF_MODE(insn->code) == BPF_XADD) {
2979 err = check_xadd(env, insn);
2980 if (err)
2981 return err;
2982 insn_idx++;
2983 continue;
2984 }
2985
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002986 /* check src1 operand */
2987 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
2988 if (err)
2989 return err;
2990 /* check src2 operand */
2991 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
2992 if (err)
2993 return err;
2994
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002995 dst_reg_type = regs[insn->dst_reg].type;
2996
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002997 /* check that memory (dst_reg + off) is writeable */
2998 err = check_mem_access(env, insn->dst_reg, insn->off,
2999 BPF_SIZE(insn->code), BPF_WRITE,
3000 insn->src_reg);
3001 if (err)
3002 return err;
3003
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003004 prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type;
3005
3006 if (*prev_dst_type == NOT_INIT) {
3007 *prev_dst_type = dst_reg_type;
3008 } else if (dst_reg_type != *prev_dst_type &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003009 (dst_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003010 *prev_dst_type == PTR_TO_CTX)) {
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003011 verbose("same insn cannot be used with different pointers\n");
3012 return -EINVAL;
3013 }
3014
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003015 } else if (class == BPF_ST) {
3016 if (BPF_MODE(insn->code) != BPF_MEM ||
3017 insn->src_reg != BPF_REG_0) {
3018 verbose("BPF_ST uses reserved fields\n");
3019 return -EINVAL;
3020 }
3021 /* check src operand */
3022 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
3023 if (err)
3024 return err;
3025
Daniel Borkmannf531fbb2018-01-29 02:49:01 +01003026 if (is_ctx_reg(env, insn->dst_reg)) {
3027 verbose("BPF_ST stores into R%d context is not allowed\n",
3028 insn->dst_reg);
3029 return -EACCES;
3030 }
3031
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003032 /* check that memory (dst_reg + off) is writeable */
3033 err = check_mem_access(env, insn->dst_reg, insn->off,
3034 BPF_SIZE(insn->code), BPF_WRITE,
3035 -1);
3036 if (err)
3037 return err;
3038
3039 } else if (class == BPF_JMP) {
3040 u8 opcode = BPF_OP(insn->code);
3041
3042 if (opcode == BPF_CALL) {
3043 if (BPF_SRC(insn->code) != BPF_K ||
3044 insn->off != 0 ||
3045 insn->src_reg != BPF_REG_0 ||
3046 insn->dst_reg != BPF_REG_0) {
3047 verbose("BPF_CALL uses reserved fields\n");
3048 return -EINVAL;
3049 }
3050
Alexei Starovoitova9bfac142018-01-07 17:33:02 -08003051 err = check_call(env, insn->imm, insn_idx);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003052 if (err)
3053 return err;
3054
3055 } else if (opcode == BPF_JA) {
3056 if (BPF_SRC(insn->code) != BPF_K ||
3057 insn->imm != 0 ||
3058 insn->src_reg != BPF_REG_0 ||
3059 insn->dst_reg != BPF_REG_0) {
3060 verbose("BPF_JA uses reserved fields\n");
3061 return -EINVAL;
3062 }
3063
3064 insn_idx += insn->off + 1;
3065 continue;
3066
3067 } else if (opcode == BPF_EXIT) {
3068 if (BPF_SRC(insn->code) != BPF_K ||
3069 insn->imm != 0 ||
3070 insn->src_reg != BPF_REG_0 ||
3071 insn->dst_reg != BPF_REG_0) {
3072 verbose("BPF_EXIT uses reserved fields\n");
3073 return -EINVAL;
3074 }
3075
3076 /* eBPF calling convetion is such that R0 is used
3077 * to return the value from eBPF program.
3078 * Make sure that it's readable at this time
3079 * of bpf_exit, which means that program wrote
3080 * something into it earlier
3081 */
3082 err = check_reg_arg(regs, BPF_REG_0, SRC_OP);
3083 if (err)
3084 return err;
3085
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003086 if (is_pointer_value(env, BPF_REG_0)) {
3087 verbose("R0 leaks addr as return value\n");
3088 return -EACCES;
3089 }
3090
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003091process_bpf_exit:
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003092 insn_idx = pop_stack(env, &prev_insn_idx);
3093 if (insn_idx < 0) {
3094 break;
3095 } else {
3096 do_print_state = true;
3097 continue;
3098 }
3099 } else {
3100 err = check_cond_jmp_op(env, insn, &insn_idx);
3101 if (err)
3102 return err;
3103 }
3104 } else if (class == BPF_LD) {
3105 u8 mode = BPF_MODE(insn->code);
3106
3107 if (mode == BPF_ABS || mode == BPF_IND) {
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003108 err = check_ld_abs(env, insn);
3109 if (err)
3110 return err;
3111
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003112 } else if (mode == BPF_IMM) {
3113 err = check_ld_imm(env, insn);
3114 if (err)
3115 return err;
3116
3117 insn_idx++;
Daniel Borkmann7b5b73e2017-12-22 16:29:03 +01003118 env->insn_aux_data[insn_idx].seen = true;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003119 } else {
3120 verbose("invalid BPF_LD mode\n");
3121 return -EINVAL;
3122 }
Josef Bacik48461132016-09-28 10:54:32 -04003123 reset_reg_range_values(regs, insn->dst_reg);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003124 } else {
3125 verbose("unknown insn class %d\n", class);
3126 return -EINVAL;
3127 }
3128
3129 insn_idx++;
3130 }
3131
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003132 verbose("processed %d insns\n", insn_processed);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003133 return 0;
3134}
3135
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07003136static int check_map_prog_compatibility(struct bpf_map *map,
3137 struct bpf_prog *prog)
3138
3139{
3140 if (prog->type == BPF_PROG_TYPE_PERF_EVENT &&
3141 (map->map_type == BPF_MAP_TYPE_HASH ||
3142 map->map_type == BPF_MAP_TYPE_PERCPU_HASH) &&
3143 (map->map_flags & BPF_F_NO_PREALLOC)) {
3144 verbose("perf_event programs can only use preallocated hash map\n");
3145 return -EINVAL;
3146 }
3147 return 0;
3148}
3149
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003150/* look for pseudo eBPF instructions that access map FDs and
3151 * replace them with actual map pointers
3152 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003153static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003154{
3155 struct bpf_insn *insn = env->prog->insnsi;
3156 int insn_cnt = env->prog->len;
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07003157 int i, j, err;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003158
3159 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003160 if (BPF_CLASS(insn->code) == BPF_LDX &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003161 (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003162 verbose("BPF_LDX uses reserved fields\n");
3163 return -EINVAL;
3164 }
3165
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003166 if (BPF_CLASS(insn->code) == BPF_STX &&
3167 ((BPF_MODE(insn->code) != BPF_MEM &&
3168 BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
3169 verbose("BPF_STX uses reserved fields\n");
3170 return -EINVAL;
3171 }
3172
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003173 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
3174 struct bpf_map *map;
3175 struct fd f;
3176
3177 if (i == insn_cnt - 1 || insn[1].code != 0 ||
3178 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
3179 insn[1].off != 0) {
3180 verbose("invalid bpf_ld_imm64 insn\n");
3181 return -EINVAL;
3182 }
3183
3184 if (insn->src_reg == 0)
3185 /* valid generic load 64-bit imm */
3186 goto next_insn;
3187
3188 if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
3189 verbose("unrecognized bpf_ld_imm64 insn\n");
3190 return -EINVAL;
3191 }
3192
3193 f = fdget(insn->imm);
Daniel Borkmannc2101292015-10-29 14:58:07 +01003194 map = __bpf_map_get(f);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003195 if (IS_ERR(map)) {
3196 verbose("fd %d is not pointing to valid bpf_map\n",
3197 insn->imm);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003198 return PTR_ERR(map);
3199 }
3200
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07003201 err = check_map_prog_compatibility(map, env->prog);
3202 if (err) {
3203 fdput(f);
3204 return err;
3205 }
3206
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003207 /* store map pointer inside BPF_LD_IMM64 instruction */
3208 insn[0].imm = (u32) (unsigned long) map;
3209 insn[1].imm = ((u64) (unsigned long) map) >> 32;
3210
3211 /* check whether we recorded this map already */
3212 for (j = 0; j < env->used_map_cnt; j++)
3213 if (env->used_maps[j] == map) {
3214 fdput(f);
3215 goto next_insn;
3216 }
3217
3218 if (env->used_map_cnt >= MAX_USED_MAPS) {
3219 fdput(f);
3220 return -E2BIG;
3221 }
3222
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003223 /* hold the map. If the program is rejected by verifier,
3224 * the map will be released by release_maps() or it
3225 * will be used by the valid program until it's unloaded
Jakub Kicinskie31a06e2018-05-03 18:37:17 -07003226 * and all maps are released in free_used_maps()
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003227 */
Alexei Starovoitov92117d82016-04-27 18:56:20 -07003228 map = bpf_map_inc(map, false);
3229 if (IS_ERR(map)) {
3230 fdput(f);
3231 return PTR_ERR(map);
3232 }
3233 env->used_maps[env->used_map_cnt++] = map;
3234
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003235 fdput(f);
3236next_insn:
3237 insn++;
3238 i++;
3239 }
3240 }
3241
3242 /* now all pseudo BPF_LD_IMM64 instructions load valid
3243 * 'struct bpf_map *' into a register instead of user map_fd.
3244 * These pointers will be used later by verifier to validate map access.
3245 */
3246 return 0;
3247}
3248
3249/* drop refcnt of maps used by the rejected program */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003250static void release_maps(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003251{
3252 int i;
3253
3254 for (i = 0; i < env->used_map_cnt; i++)
3255 bpf_map_put(env->used_maps[i]);
3256}
3257
3258/* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003259static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003260{
3261 struct bpf_insn *insn = env->prog->insnsi;
3262 int insn_cnt = env->prog->len;
3263 int i;
3264
3265 for (i = 0; i < insn_cnt; i++, insn++)
3266 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
3267 insn->src_reg = 0;
3268}
3269
Daniel Borkmann565f0122017-12-22 16:29:02 +01003270/* single env->prog->insni[off] instruction was replaced with the range
3271 * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
3272 * [0, off) and [off, end) to new locations, so the patched range stays zero
3273 */
3274static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len,
3275 u32 off, u32 cnt)
3276{
3277 struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data;
Daniel Borkmann7b5b73e2017-12-22 16:29:03 +01003278 int i;
Daniel Borkmann565f0122017-12-22 16:29:02 +01003279
3280 if (cnt == 1)
3281 return 0;
3282 new_data = vzalloc(sizeof(struct bpf_insn_aux_data) * prog_len);
3283 if (!new_data)
3284 return -ENOMEM;
3285 memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off);
3286 memcpy(new_data + off + cnt - 1, old_data + off,
3287 sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
Daniel Borkmann7b5b73e2017-12-22 16:29:03 +01003288 for (i = off; i < off + cnt - 1; i++)
3289 new_data[i].seen = true;
Daniel Borkmann565f0122017-12-22 16:29:02 +01003290 env->insn_aux_data = new_data;
3291 vfree(old_data);
3292 return 0;
3293}
3294
3295static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
3296 const struct bpf_insn *patch, u32 len)
3297{
3298 struct bpf_prog *new_prog;
3299
3300 new_prog = bpf_patch_insn_single(env->prog, off, patch, len);
3301 if (!new_prog)
3302 return NULL;
3303 if (adjust_insn_aux_data(env, new_prog->len, off, len))
3304 return NULL;
3305 return new_prog;
3306}
3307
Daniel Borkmann7b5b73e2017-12-22 16:29:03 +01003308/* The verifier does more data flow analysis than llvm and will not explore
3309 * branches that are dead at run time. Malicious programs can have dead code
3310 * too. Therefore replace all dead at-run-time code with nops.
3311 */
3312static void sanitize_dead_code(struct bpf_verifier_env *env)
3313{
3314 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
3315 struct bpf_insn nop = BPF_MOV64_REG(BPF_REG_0, BPF_REG_0);
3316 struct bpf_insn *insn = env->prog->insnsi;
3317 const int insn_cnt = env->prog->len;
3318 int i;
3319
3320 for (i = 0; i < insn_cnt; i++) {
3321 if (aux_data[i].seen)
3322 continue;
3323 memcpy(insn + i, &nop, sizeof(nop));
3324 }
3325}
3326
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003327/* convert load instructions that access fields of 'struct __sk_buff'
3328 * into sequence of instructions that access fields of 'struct sk_buff'
3329 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003330static int convert_ctx_accesses(struct bpf_verifier_env *env)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003331{
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003332 const struct bpf_verifier_ops *ops = env->prog->aux->ops;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003333 const int insn_cnt = env->prog->len;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003334 struct bpf_insn insn_buf[16], *insn;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003335 struct bpf_prog *new_prog;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003336 enum bpf_access_type type;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003337 int i, cnt, delta = 0;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003338
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003339 if (ops->gen_prologue) {
3340 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
3341 env->prog);
3342 if (cnt >= ARRAY_SIZE(insn_buf)) {
3343 verbose("bpf verifier is misconfigured\n");
3344 return -EINVAL;
3345 } else if (cnt) {
Daniel Borkmann565f0122017-12-22 16:29:02 +01003346 new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003347 if (!new_prog)
3348 return -ENOMEM;
Daniel Borkmann565f0122017-12-22 16:29:02 +01003349
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003350 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003351 delta += cnt - 1;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003352 }
3353 }
3354
3355 if (!ops->convert_ctx_access)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003356 return 0;
3357
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003358 insn = env->prog->insnsi + delta;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003359
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003360 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07003361 if (insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
3362 insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003363 type = BPF_READ;
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07003364 else if (insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
3365 insn->code == (BPF_STX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07003366 type = BPF_WRITE;
3367 else
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003368 continue;
3369
Daniel Borkmann565f0122017-12-22 16:29:02 +01003370 if (env->insn_aux_data[i + delta].ptr_type != PTR_TO_CTX)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003371 continue;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003372
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003373 cnt = ops->convert_ctx_access(type, insn->dst_reg, insn->src_reg,
3374 insn->off, insn_buf, env->prog);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003375 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
3376 verbose("bpf verifier is misconfigured\n");
3377 return -EINVAL;
3378 }
3379
Daniel Borkmann565f0122017-12-22 16:29:02 +01003380 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003381 if (!new_prog)
3382 return -ENOMEM;
3383
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003384 delta += cnt - 1;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003385
3386 /* keep walking new program and skip insns we just inserted */
3387 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003388 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003389 }
3390
3391 return 0;
3392}
3393
Alexei Starovoitovf55093d2017-03-15 18:26:40 -07003394/* fixup insn->imm field of bpf_call instructions
Alexei Starovoitov28035362017-03-15 18:26:39 -07003395 *
3396 * this function is called after eBPF program passed verification
3397 */
Alexei Starovoitovf55093d2017-03-15 18:26:40 -07003398static int fixup_bpf_calls(struct bpf_verifier_env *env)
Alexei Starovoitov28035362017-03-15 18:26:39 -07003399{
Alexei Starovoitovf55093d2017-03-15 18:26:40 -07003400 struct bpf_prog *prog = env->prog;
3401 struct bpf_insn *insn = prog->insnsi;
Alexei Starovoitov28035362017-03-15 18:26:39 -07003402 const struct bpf_func_proto *fn;
Alexei Starovoitovf55093d2017-03-15 18:26:40 -07003403 const int insn_cnt = prog->len;
Alexei Starovoitova9bfac142018-01-07 17:33:02 -08003404 struct bpf_insn insn_buf[16];
3405 struct bpf_prog *new_prog;
3406 struct bpf_map *map_ptr;
3407 int i, cnt, delta = 0;
3408
Alexei Starovoitov28035362017-03-15 18:26:39 -07003409
Alexei Starovoitovf55093d2017-03-15 18:26:40 -07003410 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov265d7652018-01-29 02:49:00 +01003411 if (insn->code == (BPF_ALU | BPF_MOD | BPF_X) ||
3412 insn->code == (BPF_ALU | BPF_DIV | BPF_X)) {
3413 /* due to JIT bugs clear upper 32-bits of src register
3414 * before div/mod operation
3415 */
3416 insn_buf[0] = BPF_MOV32_REG(insn->src_reg, insn->src_reg);
3417 insn_buf[1] = *insn;
3418 cnt = 2;
3419 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
3420 if (!new_prog)
3421 return -ENOMEM;
3422
3423 delta += cnt - 1;
3424 env->prog = prog = new_prog;
3425 insn = new_prog->insnsi + i + delta;
3426 continue;
3427 }
3428
Alexei Starovoitovf55093d2017-03-15 18:26:40 -07003429 if (insn->code != (BPF_JMP | BPF_CALL))
3430 continue;
Alexei Starovoitov28035362017-03-15 18:26:39 -07003431
Alexei Starovoitovf55093d2017-03-15 18:26:40 -07003432 if (insn->imm == BPF_FUNC_get_route_realm)
3433 prog->dst_needed = 1;
3434 if (insn->imm == BPF_FUNC_get_prandom_u32)
3435 bpf_user_rnd_init_once();
3436 if (insn->imm == BPF_FUNC_tail_call) {
3437 /* mark bpf_tail_call as different opcode to avoid
3438 * conditional branch in the interpeter for every normal
3439 * call and to prevent accidental JITing by JIT compiler
3440 * that doesn't support bpf_tail_call yet
3441 */
3442 insn->imm = 0;
3443 insn->code |= BPF_X;
Alexei Starovoitova9bfac142018-01-07 17:33:02 -08003444
3445 /* instead of changing every JIT dealing with tail_call
3446 * emit two extra insns:
3447 * if (index >= max_entries) goto out;
3448 * index &= array->index_mask;
3449 * to avoid out-of-bounds cpu speculation
3450 */
3451 map_ptr = env->insn_aux_data[i + delta].map_ptr;
3452 if (!map_ptr->unpriv_array)
3453 continue;
3454 insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3,
3455 map_ptr->max_entries, 2);
3456 insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3,
3457 container_of(map_ptr,
3458 struct bpf_array,
3459 map)->index_mask);
3460 insn_buf[2] = *insn;
3461 cnt = 3;
3462 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
3463 if (!new_prog)
3464 return -ENOMEM;
3465
3466 delta += cnt - 1;
3467 env->prog = prog = new_prog;
3468 insn = new_prog->insnsi + i + delta;
Alexei Starovoitovf55093d2017-03-15 18:26:40 -07003469 continue;
Alexei Starovoitov28035362017-03-15 18:26:39 -07003470 }
Alexei Starovoitovf55093d2017-03-15 18:26:40 -07003471
3472 fn = prog->aux->ops->get_func_proto(insn->imm);
3473 /* all functions that have prototype and verifier allowed
3474 * programs to call them, must be real in-kernel functions
3475 */
3476 if (!fn->func) {
3477 verbose("kernel subsystem misconfigured func %d\n",
3478 insn->imm);
3479 return -EFAULT;
3480 }
3481 insn->imm = fn->func - __bpf_call_base;
Alexei Starovoitov28035362017-03-15 18:26:39 -07003482 }
Alexei Starovoitovf55093d2017-03-15 18:26:40 -07003483
3484 return 0;
Alexei Starovoitov28035362017-03-15 18:26:39 -07003485}
3486
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003487static void free_states(struct bpf_verifier_env *env)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003488{
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003489 struct bpf_verifier_state_list *sl, *sln;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003490 int i;
3491
3492 if (!env->explored_states)
3493 return;
3494
3495 for (i = 0; i < env->prog->len; i++) {
3496 sl = env->explored_states[i];
3497
3498 if (sl)
3499 while (sl != STATE_LIST_MARK) {
3500 sln = sl->next;
3501 kfree(sl);
3502 sl = sln;
3503 }
3504 }
3505
3506 kfree(env->explored_states);
3507}
3508
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003509int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003510{
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003511 char __user *log_ubuf = NULL;
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003512 struct bpf_verifier_env *env;
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003513 int ret = -EINVAL;
3514
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003515 if ((*prog)->len <= 0 || (*prog)->len > BPF_MAXINSNS)
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003516 return -E2BIG;
3517
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003518 /* 'struct bpf_verifier_env' can be global, but since it's not small,
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003519 * allocate/free it every time bpf_check() is called
3520 */
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003521 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003522 if (!env)
3523 return -ENOMEM;
3524
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003525 env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
3526 (*prog)->len);
3527 ret = -ENOMEM;
3528 if (!env->insn_aux_data)
3529 goto err_free_env;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003530 env->prog = *prog;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003531
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003532 /* grab the mutex to protect few globals used by verifier */
3533 mutex_lock(&bpf_verifier_lock);
3534
3535 if (attr->log_level || attr->log_buf || attr->log_size) {
3536 /* user requested verbose verifier output
3537 * and supplied buffer to store the verification trace
3538 */
3539 log_level = attr->log_level;
3540 log_ubuf = (char __user *) (unsigned long) attr->log_buf;
3541 log_size = attr->log_size;
3542 log_len = 0;
3543
3544 ret = -EINVAL;
3545 /* log_* values have to be sane */
3546 if (log_size < 128 || log_size > UINT_MAX >> 8 ||
3547 log_level == 0 || log_ubuf == NULL)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003548 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003549
3550 ret = -ENOMEM;
3551 log_buf = vmalloc(log_size);
3552 if (!log_buf)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003553 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003554 } else {
3555 log_level = 0;
3556 }
3557
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003558 ret = replace_map_fd_with_map_ptr(env);
3559 if (ret < 0)
3560 goto skip_full_check;
3561
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003562 env->explored_states = kcalloc(env->prog->len,
Jakub Kicinski58e2af82016-09-21 11:43:57 +01003563 sizeof(struct bpf_verifier_state_list *),
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003564 GFP_USER);
3565 ret = -ENOMEM;
3566 if (!env->explored_states)
3567 goto skip_full_check;
3568
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003569 ret = check_cfg(env);
3570 if (ret < 0)
3571 goto skip_full_check;
3572
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003573 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
3574
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003575 ret = do_check(env);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003576
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003577skip_full_check:
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003578 while (pop_stack(env, NULL) >= 0);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003579 free_states(env);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003580
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003581 if (ret == 0)
Daniel Borkmann7b5b73e2017-12-22 16:29:03 +01003582 sanitize_dead_code(env);
3583
3584 if (ret == 0)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003585 /* program is valid, convert *(u32*)(ctx + off) accesses */
3586 ret = convert_ctx_accesses(env);
3587
Alexei Starovoitov28035362017-03-15 18:26:39 -07003588 if (ret == 0)
Alexei Starovoitovf55093d2017-03-15 18:26:40 -07003589 ret = fixup_bpf_calls(env);
Alexei Starovoitov28035362017-03-15 18:26:39 -07003590
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003591 if (log_level && log_len >= log_size - 1) {
3592 BUG_ON(log_len >= log_size);
3593 /* verifier log exceeded user supplied buffer */
3594 ret = -ENOSPC;
3595 /* fall through to return what was recorded */
3596 }
3597
3598 /* copy verifier log back to user space including trailing zero */
3599 if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) {
3600 ret = -EFAULT;
3601 goto free_log_buf;
3602 }
3603
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003604 if (ret == 0 && env->used_map_cnt) {
3605 /* if program passed verifier, update used_maps in bpf_prog_info */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003606 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
3607 sizeof(env->used_maps[0]),
3608 GFP_KERNEL);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003609
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003610 if (!env->prog->aux->used_maps) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003611 ret = -ENOMEM;
3612 goto free_log_buf;
3613 }
3614
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003615 memcpy(env->prog->aux->used_maps, env->used_maps,
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003616 sizeof(env->used_maps[0]) * env->used_map_cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003617 env->prog->aux->used_map_cnt = env->used_map_cnt;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003618
3619 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
3620 * bpf_ld_imm64 instructions
3621 */
3622 convert_pseudo_ld_imm64(env);
3623 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003624
3625free_log_buf:
3626 if (log_level)
3627 vfree(log_buf);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003628 if (!env->prog->aux->used_maps)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003629 /* if we didn't copy map pointers into bpf_prog_info, release
Jakub Kicinskie31a06e2018-05-03 18:37:17 -07003630 * them now. Otherwise free_used_maps() will release them.
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003631 */
3632 release_maps(env);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07003633 *prog = env->prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003634err_unlock:
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07003635 mutex_unlock(&bpf_verifier_lock);
Jakub Kicinski3df126f2016-09-21 11:43:56 +01003636 vfree(env->insn_aux_data);
3637err_free_env:
3638 kfree(env);
Alexei Starovoitov51580e72014-09-26 00:17:02 -07003639 return ret;
3640}
Jakub Kicinski13a27df2016-09-21 11:43:58 +01003641
3642int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops,
3643 void *priv)
3644{
3645 struct bpf_verifier_env *env;
3646 int ret;
3647
3648 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
3649 if (!env)
3650 return -ENOMEM;
3651
3652 env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
3653 prog->len);
3654 ret = -ENOMEM;
3655 if (!env->insn_aux_data)
3656 goto err_free_env;
3657 env->prog = prog;
3658 env->analyzer_ops = ops;
3659 env->analyzer_priv = priv;
3660
3661 /* grab the mutex to protect few globals used by verifier */
3662 mutex_lock(&bpf_verifier_lock);
3663
3664 log_level = 0;
3665
3666 env->explored_states = kcalloc(env->prog->len,
3667 sizeof(struct bpf_verifier_state_list *),
3668 GFP_KERNEL);
3669 ret = -ENOMEM;
3670 if (!env->explored_states)
3671 goto skip_full_check;
3672
3673 ret = check_cfg(env);
3674 if (ret < 0)
3675 goto skip_full_check;
3676
3677 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
3678
3679 ret = do_check(env);
3680
3681skip_full_check:
3682 while (pop_stack(env, NULL) >= 0);
3683 free_states(env);
3684
3685 mutex_unlock(&bpf_verifier_lock);
3686 vfree(env->insn_aux_data);
3687err_free_env:
3688 kfree(env);
3689 return ret;
3690}
3691EXPORT_SYMBOL_GPL(bpf_analyzer);