blob: 52c9b32d58bfb94c15390bbe6b638077c2166617 [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 Kicinski58e2af8b2016-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>
Thomas Grafebb676d2016-10-27 11:23:51 +020022#include <linux/stringify.h>
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -080023#include <linux/bsearch.h>
24#include <linux/sort.h>
Alexei Starovoitov51580e72014-09-26 00:17:02 -070025
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -070026#include "disasm.h"
27
Jakub Kicinski00176a32017-10-16 16:40:54 -070028static const struct bpf_verifier_ops * const bpf_verifier_ops[] = {
29#define BPF_PROG_TYPE(_id, _name) \
30 [_id] = & _name ## _verifier_ops,
31#define BPF_MAP_TYPE(_id, _ops)
32#include <linux/bpf_types.h>
33#undef BPF_PROG_TYPE
34#undef BPF_MAP_TYPE
35};
36
Alexei Starovoitov51580e72014-09-26 00:17:02 -070037/* bpf_check() is a static code analyzer that walks eBPF program
38 * instruction by instruction and updates register/stack state.
39 * All paths of conditional branches are analyzed until 'bpf_exit' insn.
40 *
41 * The first pass is depth-first-search to check that the program is a DAG.
42 * It rejects the following programs:
43 * - larger than BPF_MAXINSNS insns
44 * - if loop is present (detected via back-edge)
45 * - unreachable insns exist (shouldn't be a forest. program = one function)
46 * - out of bounds or malformed jumps
47 * The second pass is all possible path descent from the 1st insn.
48 * Since it's analyzing all pathes through the program, the length of the
Gary Lineba38a92017-03-01 16:25:51 +080049 * analysis is limited to 64k insn, which may be hit even if total number of
Alexei Starovoitov51580e72014-09-26 00:17:02 -070050 * insn is less then 4K, but there are too many branches that change stack/regs.
51 * Number of 'branches to be analyzed' is limited to 1k
52 *
53 * On entry to each instruction, each register has a type, and the instruction
54 * changes the types of the registers depending on instruction semantics.
55 * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is
56 * copied to R1.
57 *
58 * All registers are 64-bit.
59 * R0 - return register
60 * R1-R5 argument passing registers
61 * R6-R9 callee saved registers
62 * R10 - frame pointer read-only
63 *
64 * At the start of BPF program the register R1 contains a pointer to bpf_context
65 * and has type PTR_TO_CTX.
66 *
67 * Verifier tracks arithmetic operations on pointers in case:
68 * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
69 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20),
70 * 1st insn copies R10 (which has FRAME_PTR) type into R1
71 * and 2nd arithmetic instruction is pattern matched to recognize
72 * that it wants to construct a pointer to some element within stack.
73 * So after 2nd insn, the register R1 has type PTR_TO_STACK
74 * (and -20 constant is saved for further stack bounds checking).
75 * Meaning that this reg is a pointer to stack plus known immediate constant.
76 *
Edward Creef1174f72017-08-07 15:26:19 +010077 * Most of the time the registers have SCALAR_VALUE type, which
Alexei Starovoitov51580e72014-09-26 00:17:02 -070078 * means the register has some value, but it's not a valid pointer.
Edward Creef1174f72017-08-07 15:26:19 +010079 * (like pointer plus pointer becomes SCALAR_VALUE type)
Alexei Starovoitov51580e72014-09-26 00:17:02 -070080 *
81 * When verifier sees load or store instructions the type of base register
Edward Creef1174f72017-08-07 15:26:19 +010082 * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK. These are three pointer
Alexei Starovoitov51580e72014-09-26 00:17:02 -070083 * types recognized by check_mem_access() function.
84 *
85 * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value'
86 * and the range of [ptr, ptr + map's value_size) is accessible.
87 *
88 * registers used to pass values to function calls are checked against
89 * function argument constraints.
90 *
91 * ARG_PTR_TO_MAP_KEY is one of such argument constraints.
92 * It means that the register type passed to this function must be
93 * PTR_TO_STACK and it will be used inside the function as
94 * 'pointer to map element key'
95 *
96 * For example the argument constraints for bpf_map_lookup_elem():
97 * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
98 * .arg1_type = ARG_CONST_MAP_PTR,
99 * .arg2_type = ARG_PTR_TO_MAP_KEY,
100 *
101 * ret_type says that this function returns 'pointer to map elem value or null'
102 * function expects 1st argument to be a const pointer to 'struct bpf_map' and
103 * 2nd argument should be a pointer to stack, which will be used inside
104 * the helper function as a pointer to map element key.
105 *
106 * On the kernel side the helper function looks like:
107 * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
108 * {
109 * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
110 * void *key = (void *) (unsigned long) r2;
111 * void *value;
112 *
113 * here kernel can access 'key' and 'map' pointers safely, knowing that
114 * [key, key + map->key_size) bytes are valid and were initialized on
115 * the stack of eBPF program.
116 * }
117 *
118 * Corresponding eBPF program may look like:
119 * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR
120 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK
121 * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP
122 * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
123 * here verifier looks at prototype of map_lookup_elem() and sees:
124 * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok,
125 * Now verifier knows that this map has key of R1->map_ptr->key_size bytes
126 *
127 * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far,
128 * Now verifier checks that [R2, R2 + map's key_size) are within stack limits
129 * and were initialized prior to this call.
130 * If it's ok, then verifier allows this BPF_CALL insn and looks at
131 * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets
132 * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function
133 * returns ether pointer to map value or NULL.
134 *
135 * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off'
136 * insn, the register holding that pointer in the true branch changes state to
137 * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false
138 * branch. See check_cond_jmp_op().
139 *
140 * After the call R0 is set to return type of the function and registers R1-R5
141 * are set to NOT_INIT to indicate that they are no longer readable.
142 */
143
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700144/* verifier_state + insn_idx are pushed to stack when branch is encountered */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100145struct bpf_verifier_stack_elem {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700146 /* verifer state is 'st'
147 * before processing instruction 'insn_idx'
148 * and after processing instruction 'prev_insn_idx'
149 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100150 struct bpf_verifier_state st;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700151 int insn_idx;
152 int prev_insn_idx;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100153 struct bpf_verifier_stack_elem *next;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700154};
155
Edward Cree8e17c1b2017-08-07 15:30:30 +0100156#define BPF_COMPLEXITY_LIMIT_INSNS 131072
Daniel Borkmann07016152016-04-05 22:33:17 +0200157#define BPF_COMPLEXITY_LIMIT_STACK 1024
158
Martin KaFai Laufad73a12017-03-22 10:00:32 -0700159#define BPF_MAP_PTR_POISON ((void *)0xeB9F + POISON_POINTER_DELTA)
160
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200161struct bpf_call_arg_meta {
162 struct bpf_map *map_ptr;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200163 bool raw_mode;
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200164 bool pkt_access;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200165 int regno;
166 int access_size;
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200167};
168
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700169static DEFINE_MUTEX(bpf_verifier_lock);
170
171/* log_level controls verbosity level of eBPF verifier.
172 * verbose() is used to dump the verification trace to the log, so the user
173 * can figure out what's wrong with the program
174 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700175static __printf(2, 3) void verbose(struct bpf_verifier_env *env,
176 const char *fmt, ...)
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700177{
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700178 struct bpf_verifer_log *log = &env->log;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700179 unsigned int n;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700180 va_list args;
181
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700182 if (!log->level || !log->ubuf || bpf_verifier_log_full(log))
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700183 return;
184
185 va_start(args, fmt);
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700186 n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700187 va_end(args);
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700188
189 WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1,
190 "verifier log line truncated - local buffer too short\n");
191
192 n = min(log->len_total - log->len_used - 1, n);
193 log->kbuf[n] = '\0';
194
195 if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1))
196 log->len_used += n;
197 else
198 log->ubuf = NULL;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700199}
200
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200201static bool type_is_pkt_pointer(enum bpf_reg_type type)
202{
203 return type == PTR_TO_PACKET ||
204 type == PTR_TO_PACKET_META;
205}
206
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700207/* string representation of 'enum bpf_reg_type' */
208static const char * const reg_type_str[] = {
209 [NOT_INIT] = "?",
Edward Creef1174f72017-08-07 15:26:19 +0100210 [SCALAR_VALUE] = "inv",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700211 [PTR_TO_CTX] = "ctx",
212 [CONST_PTR_TO_MAP] = "map_ptr",
213 [PTR_TO_MAP_VALUE] = "map_value",
214 [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700215 [PTR_TO_STACK] = "fp",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700216 [PTR_TO_PACKET] = "pkt",
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200217 [PTR_TO_PACKET_META] = "pkt_meta",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700218 [PTR_TO_PACKET_END] = "pkt_end",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700219};
220
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800221static void print_liveness(struct bpf_verifier_env *env,
222 enum bpf_reg_liveness live)
223{
224 if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN))
225 verbose(env, "_");
226 if (live & REG_LIVE_READ)
227 verbose(env, "r");
228 if (live & REG_LIVE_WRITTEN)
229 verbose(env, "w");
230}
231
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800232static struct bpf_func_state *func(struct bpf_verifier_env *env,
233 const struct bpf_reg_state *reg)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700234{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800235 struct bpf_verifier_state *cur = env->cur_state;
236
237 return cur->frame[reg->frameno];
238}
239
240static void print_verifier_state(struct bpf_verifier_env *env,
241 const struct bpf_func_state *state)
242{
243 const struct bpf_reg_state *reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700244 enum bpf_reg_type t;
245 int i;
246
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800247 if (state->frameno)
248 verbose(env, " frame%d:", state->frameno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700249 for (i = 0; i < MAX_BPF_REG; i++) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700250 reg = &state->regs[i];
251 t = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700252 if (t == NOT_INIT)
253 continue;
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800254 verbose(env, " R%d", i);
255 print_liveness(env, reg->live);
256 verbose(env, "=%s", reg_type_str[t]);
Edward Creef1174f72017-08-07 15:26:19 +0100257 if ((t == SCALAR_VALUE || t == PTR_TO_STACK) &&
258 tnum_is_const(reg->var_off)) {
259 /* reg->off should be 0 for SCALAR_VALUE */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700260 verbose(env, "%lld", reg->var_off.value + reg->off);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800261 if (t == PTR_TO_STACK)
262 verbose(env, ",call_%d", func(env, reg)->callsite);
Edward Creef1174f72017-08-07 15:26:19 +0100263 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700264 verbose(env, "(id=%d", reg->id);
Edward Creef1174f72017-08-07 15:26:19 +0100265 if (t != SCALAR_VALUE)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700266 verbose(env, ",off=%d", reg->off);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200267 if (type_is_pkt_pointer(t))
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700268 verbose(env, ",r=%d", reg->range);
Edward Creef1174f72017-08-07 15:26:19 +0100269 else if (t == CONST_PTR_TO_MAP ||
270 t == PTR_TO_MAP_VALUE ||
271 t == PTR_TO_MAP_VALUE_OR_NULL)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700272 verbose(env, ",ks=%d,vs=%d",
Edward Creef1174f72017-08-07 15:26:19 +0100273 reg->map_ptr->key_size,
274 reg->map_ptr->value_size);
Edward Cree7d1238f2017-08-07 15:26:56 +0100275 if (tnum_is_const(reg->var_off)) {
276 /* Typically an immediate SCALAR_VALUE, but
277 * could be a pointer whose offset is too big
278 * for reg->off
279 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700280 verbose(env, ",imm=%llx", reg->var_off.value);
Edward Cree7d1238f2017-08-07 15:26:56 +0100281 } else {
282 if (reg->smin_value != reg->umin_value &&
283 reg->smin_value != S64_MIN)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700284 verbose(env, ",smin_value=%lld",
Edward Cree7d1238f2017-08-07 15:26:56 +0100285 (long long)reg->smin_value);
286 if (reg->smax_value != reg->umax_value &&
287 reg->smax_value != S64_MAX)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700288 verbose(env, ",smax_value=%lld",
Edward Cree7d1238f2017-08-07 15:26:56 +0100289 (long long)reg->smax_value);
290 if (reg->umin_value != 0)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700291 verbose(env, ",umin_value=%llu",
Edward Cree7d1238f2017-08-07 15:26:56 +0100292 (unsigned long long)reg->umin_value);
293 if (reg->umax_value != U64_MAX)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700294 verbose(env, ",umax_value=%llu",
Edward Cree7d1238f2017-08-07 15:26:56 +0100295 (unsigned long long)reg->umax_value);
296 if (!tnum_is_unknown(reg->var_off)) {
297 char tn_buf[48];
Edward Creef1174f72017-08-07 15:26:19 +0100298
Edward Cree7d1238f2017-08-07 15:26:56 +0100299 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700300 verbose(env, ",var_off=%s", tn_buf);
Edward Cree7d1238f2017-08-07 15:26:56 +0100301 }
Edward Creef1174f72017-08-07 15:26:19 +0100302 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700303 verbose(env, ")");
Edward Creef1174f72017-08-07 15:26:19 +0100304 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700305 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700306 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800307 if (state->stack[i].slot_type[0] == STACK_SPILL) {
308 verbose(env, " fp%d",
309 (-i - 1) * BPF_REG_SIZE);
310 print_liveness(env, state->stack[i].spilled_ptr.live);
311 verbose(env, "=%s",
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700312 reg_type_str[state->stack[i].spilled_ptr.type]);
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800313 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800314 if (state->stack[i].slot_type[0] == STACK_ZERO)
315 verbose(env, " fp%d=0", (-i - 1) * BPF_REG_SIZE);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700316 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700317 verbose(env, "\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700318}
319
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800320static int copy_stack_state(struct bpf_func_state *dst,
321 const struct bpf_func_state *src)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700322{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700323 if (!src->stack)
324 return 0;
325 if (WARN_ON_ONCE(dst->allocated_stack < src->allocated_stack)) {
326 /* internal bug, make state invalid to reject the program */
327 memset(dst, 0, sizeof(*dst));
328 return -EFAULT;
329 }
330 memcpy(dst->stack, src->stack,
331 sizeof(*src->stack) * (src->allocated_stack / BPF_REG_SIZE));
332 return 0;
333}
334
335/* do_check() starts with zero-sized stack in struct bpf_verifier_state to
336 * make it consume minimal amount of memory. check_stack_write() access from
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800337 * the program calls into realloc_func_state() to grow the stack size.
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700338 * Note there is a non-zero 'parent' pointer inside bpf_verifier_state
339 * which this function copies over. It points to previous bpf_verifier_state
340 * which is never reallocated
341 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800342static int realloc_func_state(struct bpf_func_state *state, int size,
343 bool copy_old)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700344{
345 u32 old_size = state->allocated_stack;
346 struct bpf_stack_state *new_stack;
347 int slot = size / BPF_REG_SIZE;
348
349 if (size <= old_size || !size) {
350 if (copy_old)
351 return 0;
352 state->allocated_stack = slot * BPF_REG_SIZE;
353 if (!size && old_size) {
354 kfree(state->stack);
355 state->stack = NULL;
356 }
357 return 0;
358 }
359 new_stack = kmalloc_array(slot, sizeof(struct bpf_stack_state),
360 GFP_KERNEL);
361 if (!new_stack)
362 return -ENOMEM;
363 if (copy_old) {
364 if (state->stack)
365 memcpy(new_stack, state->stack,
366 sizeof(*new_stack) * (old_size / BPF_REG_SIZE));
367 memset(new_stack + old_size / BPF_REG_SIZE, 0,
368 sizeof(*new_stack) * (size - old_size) / BPF_REG_SIZE);
369 }
370 state->allocated_stack = slot * BPF_REG_SIZE;
371 kfree(state->stack);
372 state->stack = new_stack;
373 return 0;
374}
375
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800376static void free_func_state(struct bpf_func_state *state)
377{
378 kfree(state->stack);
379 kfree(state);
380}
381
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700382static void free_verifier_state(struct bpf_verifier_state *state,
383 bool free_self)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700384{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800385 int i;
386
387 for (i = 0; i <= state->curframe; i++) {
388 free_func_state(state->frame[i]);
389 state->frame[i] = NULL;
390 }
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700391 if (free_self)
392 kfree(state);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700393}
394
395/* copy verifier state from src to dst growing dst stack space
396 * when necessary to accommodate larger src stack
397 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800398static int copy_func_state(struct bpf_func_state *dst,
399 const struct bpf_func_state *src)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700400{
401 int err;
402
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800403 err = realloc_func_state(dst, src->allocated_stack, false);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700404 if (err)
405 return err;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800406 memcpy(dst, src, offsetof(struct bpf_func_state, allocated_stack));
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700407 return copy_stack_state(dst, src);
408}
409
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800410static int copy_verifier_state(struct bpf_verifier_state *dst_state,
411 const struct bpf_verifier_state *src)
412{
413 struct bpf_func_state *dst;
414 int i, err;
415
416 /* if dst has more stack frames then src frame, free them */
417 for (i = src->curframe + 1; i <= dst_state->curframe; i++) {
418 free_func_state(dst_state->frame[i]);
419 dst_state->frame[i] = NULL;
420 }
421 dst_state->curframe = src->curframe;
422 dst_state->parent = src->parent;
423 for (i = 0; i <= src->curframe; i++) {
424 dst = dst_state->frame[i];
425 if (!dst) {
426 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
427 if (!dst)
428 return -ENOMEM;
429 dst_state->frame[i] = dst;
430 }
431 err = copy_func_state(dst, src->frame[i]);
432 if (err)
433 return err;
434 }
435 return 0;
436}
437
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700438static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx,
439 int *insn_idx)
440{
441 struct bpf_verifier_state *cur = env->cur_state;
442 struct bpf_verifier_stack_elem *elem, *head = env->head;
443 int err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700444
445 if (env->head == NULL)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700446 return -ENOENT;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700447
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700448 if (cur) {
449 err = copy_verifier_state(cur, &head->st);
450 if (err)
451 return err;
452 }
453 if (insn_idx)
454 *insn_idx = head->insn_idx;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700455 if (prev_insn_idx)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700456 *prev_insn_idx = head->prev_insn_idx;
457 elem = head->next;
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700458 free_verifier_state(&head->st, false);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700459 kfree(head);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700460 env->head = elem;
461 env->stack_size--;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700462 return 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700463}
464
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100465static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,
466 int insn_idx, int prev_insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700467{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700468 struct bpf_verifier_state *cur = env->cur_state;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100469 struct bpf_verifier_stack_elem *elem;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700470 int err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700471
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700472 elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700473 if (!elem)
474 goto err;
475
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700476 elem->insn_idx = insn_idx;
477 elem->prev_insn_idx = prev_insn_idx;
478 elem->next = env->head;
479 env->head = elem;
480 env->stack_size++;
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700481 err = copy_verifier_state(&elem->st, cur);
482 if (err)
483 goto err;
Daniel Borkmann07016152016-04-05 22:33:17 +0200484 if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700485 verbose(env, "BPF program is too complex\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700486 goto err;
487 }
488 return &elem->st;
489err:
490 /* pop all elements and return */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700491 while (!pop_stack(env, NULL, NULL));
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700492 return NULL;
493}
494
495#define CALLER_SAVED_REGS 6
496static const int caller_saved[CALLER_SAVED_REGS] = {
497 BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
498};
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800499#define CALLEE_SAVED_REGS 5
500static const int callee_saved[CALLEE_SAVED_REGS] = {
501 BPF_REG_6, BPF_REG_7, BPF_REG_8, BPF_REG_9
502};
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700503
Edward Creef1174f72017-08-07 15:26:19 +0100504static void __mark_reg_not_init(struct bpf_reg_state *reg);
505
Edward Creeb03c9f92017-08-07 15:26:36 +0100506/* Mark the unknown part of a register (variable offset or scalar value) as
507 * known to have the value @imm.
508 */
509static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
510{
511 reg->id = 0;
512 reg->var_off = tnum_const(imm);
513 reg->smin_value = (s64)imm;
514 reg->smax_value = (s64)imm;
515 reg->umin_value = imm;
516 reg->umax_value = imm;
517}
518
Edward Creef1174f72017-08-07 15:26:19 +0100519/* Mark the 'variable offset' part of a register as zero. This should be
520 * used only on registers holding a pointer type.
521 */
522static void __mark_reg_known_zero(struct bpf_reg_state *reg)
523{
Edward Creeb03c9f92017-08-07 15:26:36 +0100524 __mark_reg_known(reg, 0);
Edward Creef1174f72017-08-07 15:26:19 +0100525}
526
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800527static void __mark_reg_const_zero(struct bpf_reg_state *reg)
528{
529 __mark_reg_known(reg, 0);
530 reg->off = 0;
531 reg->type = SCALAR_VALUE;
532}
533
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700534static void mark_reg_known_zero(struct bpf_verifier_env *env,
535 struct bpf_reg_state *regs, u32 regno)
Edward Creef1174f72017-08-07 15:26:19 +0100536{
537 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700538 verbose(env, "mark_reg_known_zero(regs, %u)\n", regno);
Edward Creef1174f72017-08-07 15:26:19 +0100539 /* Something bad happened, let's kill all regs */
540 for (regno = 0; regno < MAX_BPF_REG; regno++)
541 __mark_reg_not_init(regs + regno);
542 return;
543 }
544 __mark_reg_known_zero(regs + regno);
545}
546
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200547static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg)
548{
549 return type_is_pkt_pointer(reg->type);
550}
551
552static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg)
553{
554 return reg_is_pkt_pointer(reg) ||
555 reg->type == PTR_TO_PACKET_END;
556}
557
558/* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */
559static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg,
560 enum bpf_reg_type which)
561{
562 /* The register can already have a range from prior markings.
563 * This is fine as long as it hasn't been advanced from its
564 * origin.
565 */
566 return reg->type == which &&
567 reg->id == 0 &&
568 reg->off == 0 &&
569 tnum_equals_const(reg->var_off, 0);
570}
571
Edward Creeb03c9f92017-08-07 15:26:36 +0100572/* Attempts to improve min/max values based on var_off information */
573static void __update_reg_bounds(struct bpf_reg_state *reg)
574{
575 /* min signed is max(sign bit) | min(other bits) */
576 reg->smin_value = max_t(s64, reg->smin_value,
577 reg->var_off.value | (reg->var_off.mask & S64_MIN));
578 /* max signed is min(sign bit) | max(other bits) */
579 reg->smax_value = min_t(s64, reg->smax_value,
580 reg->var_off.value | (reg->var_off.mask & S64_MAX));
581 reg->umin_value = max(reg->umin_value, reg->var_off.value);
582 reg->umax_value = min(reg->umax_value,
583 reg->var_off.value | reg->var_off.mask);
584}
585
586/* Uses signed min/max values to inform unsigned, and vice-versa */
587static void __reg_deduce_bounds(struct bpf_reg_state *reg)
588{
589 /* Learn sign from signed bounds.
590 * If we cannot cross the sign boundary, then signed and unsigned bounds
591 * are the same, so combine. This works even in the negative case, e.g.
592 * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff.
593 */
594 if (reg->smin_value >= 0 || reg->smax_value < 0) {
595 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
596 reg->umin_value);
597 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
598 reg->umax_value);
599 return;
600 }
601 /* Learn sign from unsigned bounds. Signed bounds cross the sign
602 * boundary, so we must be careful.
603 */
604 if ((s64)reg->umax_value >= 0) {
605 /* Positive. We can't learn anything from the smin, but smax
606 * is positive, hence safe.
607 */
608 reg->smin_value = reg->umin_value;
609 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
610 reg->umax_value);
611 } else if ((s64)reg->umin_value < 0) {
612 /* Negative. We can't learn anything from the smax, but smin
613 * is negative, hence safe.
614 */
615 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
616 reg->umin_value);
617 reg->smax_value = reg->umax_value;
618 }
619}
620
621/* Attempts to improve var_off based on unsigned min/max information */
622static void __reg_bound_offset(struct bpf_reg_state *reg)
623{
624 reg->var_off = tnum_intersect(reg->var_off,
625 tnum_range(reg->umin_value,
626 reg->umax_value));
627}
628
629/* Reset the min/max bounds of a register */
630static void __mark_reg_unbounded(struct bpf_reg_state *reg)
631{
632 reg->smin_value = S64_MIN;
633 reg->smax_value = S64_MAX;
634 reg->umin_value = 0;
635 reg->umax_value = U64_MAX;
636}
637
Edward Creef1174f72017-08-07 15:26:19 +0100638/* Mark a register as having a completely unknown (scalar) value. */
639static void __mark_reg_unknown(struct bpf_reg_state *reg)
640{
641 reg->type = SCALAR_VALUE;
642 reg->id = 0;
643 reg->off = 0;
644 reg->var_off = tnum_unknown;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800645 reg->frameno = 0;
Edward Creeb03c9f92017-08-07 15:26:36 +0100646 __mark_reg_unbounded(reg);
Edward Creef1174f72017-08-07 15:26:19 +0100647}
648
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700649static void mark_reg_unknown(struct bpf_verifier_env *env,
650 struct bpf_reg_state *regs, u32 regno)
Edward Creef1174f72017-08-07 15:26:19 +0100651{
652 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700653 verbose(env, "mark_reg_unknown(regs, %u)\n", regno);
Alexei Starovoitov19ceb412017-11-30 21:31:37 -0800654 /* Something bad happened, let's kill all regs except FP */
655 for (regno = 0; regno < BPF_REG_FP; regno++)
Edward Creef1174f72017-08-07 15:26:19 +0100656 __mark_reg_not_init(regs + regno);
657 return;
658 }
659 __mark_reg_unknown(regs + regno);
660}
661
662static void __mark_reg_not_init(struct bpf_reg_state *reg)
663{
664 __mark_reg_unknown(reg);
665 reg->type = NOT_INIT;
666}
667
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700668static void mark_reg_not_init(struct bpf_verifier_env *env,
669 struct bpf_reg_state *regs, u32 regno)
Daniel Borkmanna9789ef2017-05-25 01:05:06 +0200670{
Edward Creef1174f72017-08-07 15:26:19 +0100671 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700672 verbose(env, "mark_reg_not_init(regs, %u)\n", regno);
Alexei Starovoitov19ceb412017-11-30 21:31:37 -0800673 /* Something bad happened, let's kill all regs except FP */
674 for (regno = 0; regno < BPF_REG_FP; regno++)
Edward Creef1174f72017-08-07 15:26:19 +0100675 __mark_reg_not_init(regs + regno);
676 return;
677 }
678 __mark_reg_not_init(regs + regno);
Daniel Borkmanna9789ef2017-05-25 01:05:06 +0200679}
680
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700681static void init_reg_state(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800682 struct bpf_func_state *state)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700683{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800684 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700685 int i;
686
Edward Creedc503a82017-08-15 20:34:35 +0100687 for (i = 0; i < MAX_BPF_REG; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700688 mark_reg_not_init(env, regs, i);
Edward Creedc503a82017-08-15 20:34:35 +0100689 regs[i].live = REG_LIVE_NONE;
690 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700691
692 /* frame pointer */
Edward Creef1174f72017-08-07 15:26:19 +0100693 regs[BPF_REG_FP].type = PTR_TO_STACK;
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700694 mark_reg_known_zero(env, regs, BPF_REG_FP);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800695 regs[BPF_REG_FP].frameno = state->frameno;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700696
697 /* 1st arg to a function */
698 regs[BPF_REG_1].type = PTR_TO_CTX;
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700699 mark_reg_known_zero(env, regs, BPF_REG_1);
Daniel Borkmann6760bf22016-12-18 01:52:59 +0100700}
701
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800702#define BPF_MAIN_FUNC (-1)
703static void init_func_state(struct bpf_verifier_env *env,
704 struct bpf_func_state *state,
705 int callsite, int frameno, int subprogno)
706{
707 state->callsite = callsite;
708 state->frameno = frameno;
709 state->subprogno = subprogno;
710 init_reg_state(env, state);
711}
712
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700713enum reg_arg_type {
714 SRC_OP, /* register is used as source operand */
715 DST_OP, /* register is used as destination operand */
716 DST_OP_NO_MARK /* same as above, check only, don't mark */
717};
718
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800719static int cmp_subprogs(const void *a, const void *b)
720{
721 return *(int *)a - *(int *)b;
722}
723
724static int find_subprog(struct bpf_verifier_env *env, int off)
725{
726 u32 *p;
727
728 p = bsearch(&off, env->subprog_starts, env->subprog_cnt,
729 sizeof(env->subprog_starts[0]), cmp_subprogs);
730 if (!p)
731 return -ENOENT;
732 return p - env->subprog_starts;
733
734}
735
736static int add_subprog(struct bpf_verifier_env *env, int off)
737{
738 int insn_cnt = env->prog->len;
739 int ret;
740
741 if (off >= insn_cnt || off < 0) {
742 verbose(env, "call to invalid destination\n");
743 return -EINVAL;
744 }
745 ret = find_subprog(env, off);
746 if (ret >= 0)
747 return 0;
748 if (env->subprog_cnt >= BPF_MAX_SUBPROGS) {
749 verbose(env, "too many subprograms\n");
750 return -E2BIG;
751 }
752 env->subprog_starts[env->subprog_cnt++] = off;
753 sort(env->subprog_starts, env->subprog_cnt,
754 sizeof(env->subprog_starts[0]), cmp_subprogs, NULL);
755 return 0;
756}
757
758static int check_subprogs(struct bpf_verifier_env *env)
759{
760 int i, ret, subprog_start, subprog_end, off, cur_subprog = 0;
761 struct bpf_insn *insn = env->prog->insnsi;
762 int insn_cnt = env->prog->len;
763
764 /* determine subprog starts. The end is one before the next starts */
765 for (i = 0; i < insn_cnt; i++) {
766 if (insn[i].code != (BPF_JMP | BPF_CALL))
767 continue;
768 if (insn[i].src_reg != BPF_PSEUDO_CALL)
769 continue;
770 if (!env->allow_ptr_leaks) {
771 verbose(env, "function calls to other bpf functions are allowed for root only\n");
772 return -EPERM;
773 }
774 if (bpf_prog_is_dev_bound(env->prog->aux)) {
Colin Ian Kinge90004d52017-12-18 14:03:12 +0000775 verbose(env, "function calls in offloaded programs are not supported yet\n");
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800776 return -EINVAL;
777 }
778 ret = add_subprog(env, i + insn[i].imm + 1);
779 if (ret < 0)
780 return ret;
781 }
782
783 if (env->log.level > 1)
784 for (i = 0; i < env->subprog_cnt; i++)
785 verbose(env, "func#%d @%d\n", i, env->subprog_starts[i]);
786
787 /* now check that all jumps are within the same subprog */
788 subprog_start = 0;
789 if (env->subprog_cnt == cur_subprog)
790 subprog_end = insn_cnt;
791 else
792 subprog_end = env->subprog_starts[cur_subprog++];
793 for (i = 0; i < insn_cnt; i++) {
794 u8 code = insn[i].code;
795
796 if (BPF_CLASS(code) != BPF_JMP)
797 goto next;
798 if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL)
799 goto next;
800 off = i + insn[i].off + 1;
801 if (off < subprog_start || off >= subprog_end) {
802 verbose(env, "jump out of range from insn %d to %d\n", i, off);
803 return -EINVAL;
804 }
805next:
806 if (i == subprog_end - 1) {
807 /* to avoid fall-through from one subprog into another
808 * the last insn of the subprog should be either exit
809 * or unconditional jump back
810 */
811 if (code != (BPF_JMP | BPF_EXIT) &&
812 code != (BPF_JMP | BPF_JA)) {
813 verbose(env, "last insn is not an exit or jmp\n");
814 return -EINVAL;
815 }
816 subprog_start = subprog_end;
817 if (env->subprog_cnt == cur_subprog)
818 subprog_end = insn_cnt;
819 else
820 subprog_end = env->subprog_starts[cur_subprog++];
821 }
822 }
823 return 0;
824}
825
Colin Ian Kingfa2d41a2017-12-18 17:47:07 +0000826static
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800827struct bpf_verifier_state *skip_callee(struct bpf_verifier_env *env,
828 const struct bpf_verifier_state *state,
829 struct bpf_verifier_state *parent,
830 u32 regno)
Edward Creedc503a82017-08-15 20:34:35 +0100831{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800832 struct bpf_verifier_state *tmp = NULL;
833
834 /* 'parent' could be a state of caller and
835 * 'state' could be a state of callee. In such case
836 * parent->curframe < state->curframe
837 * and it's ok for r1 - r5 registers
838 *
839 * 'parent' could be a callee's state after it bpf_exit-ed.
840 * In such case parent->curframe > state->curframe
841 * and it's ok for r0 only
842 */
843 if (parent->curframe == state->curframe ||
844 (parent->curframe < state->curframe &&
845 regno >= BPF_REG_1 && regno <= BPF_REG_5) ||
846 (parent->curframe > state->curframe &&
847 regno == BPF_REG_0))
848 return parent;
849
850 if (parent->curframe > state->curframe &&
851 regno >= BPF_REG_6) {
852 /* for callee saved regs we have to skip the whole chain
853 * of states that belong to callee and mark as LIVE_READ
854 * the registers before the call
855 */
856 tmp = parent;
857 while (tmp && tmp->curframe != state->curframe) {
858 tmp = tmp->parent;
859 }
860 if (!tmp)
861 goto bug;
862 parent = tmp;
863 } else {
864 goto bug;
865 }
866 return parent;
867bug:
868 verbose(env, "verifier bug regno %d tmp %p\n", regno, tmp);
869 verbose(env, "regno %d parent frame %d current frame %d\n",
870 regno, parent->curframe, state->curframe);
Colin Ian Kingfa2d41a2017-12-18 17:47:07 +0000871 return NULL;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800872}
873
874static int mark_reg_read(struct bpf_verifier_env *env,
875 const struct bpf_verifier_state *state,
876 struct bpf_verifier_state *parent,
877 u32 regno)
878{
879 bool writes = parent == state->parent; /* Observe write marks */
Edward Creedc503a82017-08-15 20:34:35 +0100880
Alexei Starovoitov8fe2d6c2017-10-05 16:20:56 -0700881 if (regno == BPF_REG_FP)
882 /* We don't need to worry about FP liveness because it's read-only */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800883 return 0;
Alexei Starovoitov8fe2d6c2017-10-05 16:20:56 -0700884
Edward Creedc503a82017-08-15 20:34:35 +0100885 while (parent) {
886 /* if read wasn't screened by an earlier write ... */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800887 if (writes && state->frame[state->curframe]->regs[regno].live & REG_LIVE_WRITTEN)
Edward Creedc503a82017-08-15 20:34:35 +0100888 break;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800889 parent = skip_callee(env, state, parent, regno);
890 if (!parent)
891 return -EFAULT;
Edward Creedc503a82017-08-15 20:34:35 +0100892 /* ... then we depend on parent's value */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800893 parent->frame[parent->curframe]->regs[regno].live |= REG_LIVE_READ;
Edward Creedc503a82017-08-15 20:34:35 +0100894 state = parent;
895 parent = state->parent;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800896 writes = true;
Edward Creedc503a82017-08-15 20:34:35 +0100897 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800898 return 0;
Edward Creedc503a82017-08-15 20:34:35 +0100899}
900
901static int check_reg_arg(struct bpf_verifier_env *env, u32 regno,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700902 enum reg_arg_type t)
903{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800904 struct bpf_verifier_state *vstate = env->cur_state;
905 struct bpf_func_state *state = vstate->frame[vstate->curframe];
906 struct bpf_reg_state *regs = state->regs;
Edward Creedc503a82017-08-15 20:34:35 +0100907
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700908 if (regno >= MAX_BPF_REG) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700909 verbose(env, "R%d is invalid\n", regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700910 return -EINVAL;
911 }
912
913 if (t == SRC_OP) {
914 /* check whether register used as source operand can be read */
915 if (regs[regno].type == NOT_INIT) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700916 verbose(env, "R%d !read_ok\n", regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700917 return -EACCES;
918 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800919 return mark_reg_read(env, vstate, vstate->parent, regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700920 } else {
921 /* check whether register used as dest operand can be written to */
922 if (regno == BPF_REG_FP) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700923 verbose(env, "frame pointer is read only\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700924 return -EACCES;
925 }
Edward Creedc503a82017-08-15 20:34:35 +0100926 regs[regno].live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700927 if (t == DST_OP)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700928 mark_reg_unknown(env, regs, regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700929 }
930 return 0;
931}
932
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700933static bool is_spillable_regtype(enum bpf_reg_type type)
934{
935 switch (type) {
936 case PTR_TO_MAP_VALUE:
937 case PTR_TO_MAP_VALUE_OR_NULL:
938 case PTR_TO_STACK:
939 case PTR_TO_CTX:
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700940 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200941 case PTR_TO_PACKET_META:
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700942 case PTR_TO_PACKET_END:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700943 case CONST_PTR_TO_MAP:
944 return true;
945 default:
946 return false;
947 }
948}
949
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800950/* Does this register contain a constant zero? */
951static bool register_is_null(struct bpf_reg_state *reg)
952{
953 return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0);
954}
955
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700956/* check_stack_read/write functions track spill/fill of registers,
957 * stack boundary and alignment are checked in check_mem_access()
958 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700959static int check_stack_write(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800960 struct bpf_func_state *state, /* func where register points to */
961 int off, int size, int value_regno)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700962{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800963 struct bpf_func_state *cur; /* state of the current function */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700964 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800965 enum bpf_reg_type type;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700966
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800967 err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE),
968 true);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700969 if (err)
970 return err;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700971 /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
972 * so it's aligned access and [off, off + size) are within stack limits
973 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700974 if (!env->allow_ptr_leaks &&
975 state->stack[spi].slot_type[0] == STACK_SPILL &&
976 size != BPF_REG_SIZE) {
977 verbose(env, "attempt to corrupt spilled pointer on stack\n");
978 return -EACCES;
979 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700980
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800981 cur = env->cur_state->frame[env->cur_state->curframe];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700982 if (value_regno >= 0 &&
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800983 is_spillable_regtype((type = cur->regs[value_regno].type))) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700984
985 /* register containing pointer is being spilled into stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700986 if (size != BPF_REG_SIZE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700987 verbose(env, "invalid size of register spill\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700988 return -EACCES;
989 }
990
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800991 if (state != cur && type == PTR_TO_STACK) {
992 verbose(env, "cannot spill pointers to stack into stack frame of the caller\n");
993 return -EINVAL;
994 }
995
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700996 /* save register state */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800997 state->stack[spi].spilled_ptr = cur->regs[value_regno];
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700998 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700999
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001000 for (i = 0; i < BPF_REG_SIZE; i++)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001001 state->stack[spi].slot_type[i] = STACK_SPILL;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001002 } else {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001003 u8 type = STACK_MISC;
1004
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001005 /* regular write of data into stack */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001006 state->stack[spi].spilled_ptr = (struct bpf_reg_state) {};
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001007
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001008 /* only mark the slot as written if all 8 bytes were written
1009 * otherwise read propagation may incorrectly stop too soon
1010 * when stack slots are partially written.
1011 * This heuristic means that read propagation will be
1012 * conservative, since it will add reg_live_read marks
1013 * to stack slots all the way to first state when programs
1014 * writes+reads less than 8 bytes
1015 */
1016 if (size == BPF_REG_SIZE)
1017 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
1018
1019 /* when we zero initialize stack slots mark them as such */
1020 if (value_regno >= 0 &&
1021 register_is_null(&cur->regs[value_regno]))
1022 type = STACK_ZERO;
1023
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001024 for (i = 0; i < size; i++)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001025 state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] =
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001026 type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001027 }
1028 return 0;
1029}
1030
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001031/* registers of every function are unique and mark_reg_read() propagates
1032 * the liveness in the following cases:
1033 * - from callee into caller for R1 - R5 that were used as arguments
1034 * - from caller into callee for R0 that used as result of the call
1035 * - from caller to the same caller skipping states of the callee for R6 - R9,
1036 * since R6 - R9 are callee saved by implicit function prologue and
1037 * caller's R6 != callee's R6, so when we propagate liveness up to
1038 * parent states we need to skip callee states for R6 - R9.
1039 *
1040 * stack slot marking is different, since stacks of caller and callee are
1041 * accessible in both (since caller can pass a pointer to caller's stack to
1042 * callee which can pass it to another function), hence mark_stack_slot_read()
1043 * has to propagate the stack liveness to all parent states at given frame number.
1044 * Consider code:
1045 * f1() {
1046 * ptr = fp - 8;
1047 * *ptr = ctx;
1048 * call f2 {
1049 * .. = *ptr;
1050 * }
1051 * .. = *ptr;
1052 * }
1053 * First *ptr is reading from f1's stack and mark_stack_slot_read() has
1054 * to mark liveness at the f1's frame and not f2's frame.
1055 * Second *ptr is also reading from f1's stack and mark_stack_slot_read() has
1056 * to propagate liveness to f2 states at f1's frame level and further into
1057 * f1 states at f1's frame level until write into that stack slot
1058 */
1059static void mark_stack_slot_read(struct bpf_verifier_env *env,
1060 const struct bpf_verifier_state *state,
1061 struct bpf_verifier_state *parent,
1062 int slot, int frameno)
Edward Creedc503a82017-08-15 20:34:35 +01001063{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001064 bool writes = parent == state->parent; /* Observe write marks */
Edward Creedc503a82017-08-15 20:34:35 +01001065
1066 while (parent) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001067 if (parent->frame[frameno]->allocated_stack <= slot * BPF_REG_SIZE)
1068 /* since LIVE_WRITTEN mark is only done for full 8-byte
1069 * write the read marks are conservative and parent
1070 * state may not even have the stack allocated. In such case
1071 * end the propagation, since the loop reached beginning
1072 * of the function
1073 */
1074 break;
Edward Creedc503a82017-08-15 20:34:35 +01001075 /* if read wasn't screened by an earlier write ... */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001076 if (writes && state->frame[frameno]->stack[slot].spilled_ptr.live & REG_LIVE_WRITTEN)
Edward Creedc503a82017-08-15 20:34:35 +01001077 break;
1078 /* ... then we depend on parent's value */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001079 parent->frame[frameno]->stack[slot].spilled_ptr.live |= REG_LIVE_READ;
Edward Creedc503a82017-08-15 20:34:35 +01001080 state = parent;
1081 parent = state->parent;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001082 writes = true;
Edward Creedc503a82017-08-15 20:34:35 +01001083 }
1084}
1085
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001086static int check_stack_read(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001087 struct bpf_func_state *reg_state /* func where register points to */,
1088 int off, int size, int value_regno)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001089{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001090 struct bpf_verifier_state *vstate = env->cur_state;
1091 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001092 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE;
1093 u8 *stype;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001094
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001095 if (reg_state->allocated_stack <= slot) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001096 verbose(env, "invalid read from stack off %d+0 size %d\n",
1097 off, size);
1098 return -EACCES;
1099 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001100 stype = reg_state->stack[spi].slot_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001101
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001102 if (stype[0] == STACK_SPILL) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001103 if (size != BPF_REG_SIZE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001104 verbose(env, "invalid size of register spill\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001105 return -EACCES;
1106 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001107 for (i = 1; i < BPF_REG_SIZE; i++) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001108 if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001109 verbose(env, "corrupted spill memory\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001110 return -EACCES;
1111 }
1112 }
1113
Edward Creedc503a82017-08-15 20:34:35 +01001114 if (value_regno >= 0) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001115 /* restore register state from stack */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001116 state->regs[value_regno] = reg_state->stack[spi].spilled_ptr;
Alexei Starovoitov2f18f622017-11-30 21:31:38 -08001117 /* mark reg as written since spilled pointer state likely
1118 * has its liveness marks cleared by is_state_visited()
1119 * which resets stack/reg liveness for state transitions
1120 */
1121 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
Edward Creedc503a82017-08-15 20:34:35 +01001122 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001123 mark_stack_slot_read(env, vstate, vstate->parent, spi,
1124 reg_state->frameno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001125 return 0;
1126 } else {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001127 int zeros = 0;
1128
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001129 for (i = 0; i < size; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001130 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC)
1131 continue;
1132 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) {
1133 zeros++;
1134 continue;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001135 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001136 verbose(env, "invalid read from stack off %d+%d size %d\n",
1137 off, i, size);
1138 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001139 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001140 mark_stack_slot_read(env, vstate, vstate->parent, spi,
1141 reg_state->frameno);
1142 if (value_regno >= 0) {
1143 if (zeros == size) {
1144 /* any size read into register is zero extended,
1145 * so the whole register == const_zero
1146 */
1147 __mark_reg_const_zero(&state->regs[value_regno]);
1148 } else {
1149 /* have read misc data from the stack */
1150 mark_reg_unknown(env, state->regs, value_regno);
1151 }
1152 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
1153 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001154 return 0;
1155 }
1156}
1157
1158/* check read/write into map element returned by bpf_map_lookup_elem() */
Edward Creef1174f72017-08-07 15:26:19 +01001159static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001160 int size, bool zero_size_allowed)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001161{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001162 struct bpf_reg_state *regs = cur_regs(env);
1163 struct bpf_map *map = regs[regno].map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001164
Yonghong Song9fd29c02017-11-12 14:49:09 -08001165 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
1166 off + size > map->value_size) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001167 verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001168 map->value_size, off, size);
1169 return -EACCES;
1170 }
1171 return 0;
1172}
1173
Edward Creef1174f72017-08-07 15:26:19 +01001174/* check read/write into a map element with possible variable offset */
1175static int check_map_access(struct bpf_verifier_env *env, u32 regno,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001176 int off, int size, bool zero_size_allowed)
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001177{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001178 struct bpf_verifier_state *vstate = env->cur_state;
1179 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001180 struct bpf_reg_state *reg = &state->regs[regno];
1181 int err;
1182
Edward Creef1174f72017-08-07 15:26:19 +01001183 /* We may have adjusted the register to this map value, so we
1184 * need to try adding each of min_value and max_value to off
1185 * to make sure our theoretical access will be safe.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001186 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001187 if (env->log.level)
1188 print_verifier_state(env, state);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001189 /* The minimum value is only important with signed
1190 * comparisons where we can't assume the floor of a
1191 * value is 0. If we are using signed variables for our
1192 * index'es we need to make sure that whatever we use
1193 * will have a set floor within our range.
1194 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001195 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001196 verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001197 regno);
1198 return -EACCES;
1199 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001200 err = __check_map_access(env, regno, reg->smin_value + off, size,
1201 zero_size_allowed);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001202 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001203 verbose(env, "R%d min value is outside of the array range\n",
1204 regno);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001205 return err;
1206 }
1207
Edward Creeb03c9f92017-08-07 15:26:36 +01001208 /* If we haven't set a max value then we need to bail since we can't be
1209 * sure we won't do bad things.
1210 * If reg->umax_value + off could overflow, treat that as unbounded too.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001211 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001212 if (reg->umax_value >= BPF_MAX_VAR_OFF) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001213 verbose(env, "R%d unbounded memory access, make sure to bounds check any array access into a map\n",
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001214 regno);
1215 return -EACCES;
1216 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001217 err = __check_map_access(env, regno, reg->umax_value + off, size,
1218 zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01001219 if (err)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001220 verbose(env, "R%d max value is outside of the array range\n",
1221 regno);
Edward Creef1174f72017-08-07 15:26:19 +01001222 return err;
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001223}
1224
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001225#define MAX_PACKET_OFF 0xffff
1226
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001227static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001228 const struct bpf_call_arg_meta *meta,
1229 enum bpf_access_type t)
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001230{
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001231 switch (env->prog->type) {
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001232 case BPF_PROG_TYPE_LWT_IN:
1233 case BPF_PROG_TYPE_LWT_OUT:
1234 /* dst_input() and dst_output() can't write for now */
1235 if (t == BPF_WRITE)
1236 return false;
Alexander Alemayhu7e57fbb2017-02-14 00:02:35 +01001237 /* fallthrough */
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001238 case BPF_PROG_TYPE_SCHED_CLS:
1239 case BPF_PROG_TYPE_SCHED_ACT:
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001240 case BPF_PROG_TYPE_XDP:
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001241 case BPF_PROG_TYPE_LWT_XMIT:
John Fastabend8a31db52017-08-15 22:33:09 -07001242 case BPF_PROG_TYPE_SK_SKB:
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001243 if (meta)
1244 return meta->pkt_access;
1245
1246 env->seen_direct_write = true;
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001247 return true;
1248 default:
1249 return false;
1250 }
1251}
1252
Edward Creef1174f72017-08-07 15:26:19 +01001253static int __check_packet_access(struct bpf_verifier_env *env, u32 regno,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001254 int off, int size, bool zero_size_allowed)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001255{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001256 struct bpf_reg_state *regs = cur_regs(env);
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001257 struct bpf_reg_state *reg = &regs[regno];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001258
Yonghong Song9fd29c02017-11-12 14:49:09 -08001259 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
1260 (u64)off + size > reg->range) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001261 verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n",
Alexei Starovoitovd91b28e2016-05-19 18:17:13 -07001262 off, size, regno, reg->id, reg->off, reg->range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001263 return -EACCES;
1264 }
1265 return 0;
1266}
1267
Edward Creef1174f72017-08-07 15:26:19 +01001268static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001269 int size, bool zero_size_allowed)
Edward Creef1174f72017-08-07 15:26:19 +01001270{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001271 struct bpf_reg_state *regs = cur_regs(env);
Edward Creef1174f72017-08-07 15:26:19 +01001272 struct bpf_reg_state *reg = &regs[regno];
1273 int err;
1274
1275 /* We may have added a variable offset to the packet pointer; but any
1276 * reg->range we have comes after that. We are only checking the fixed
1277 * offset.
1278 */
1279
1280 /* We don't allow negative numbers, because we aren't tracking enough
1281 * detail to prove they're safe.
1282 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001283 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001284 verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
Edward Creef1174f72017-08-07 15:26:19 +01001285 regno);
1286 return -EACCES;
1287 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001288 err = __check_packet_access(env, regno, off, size, zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01001289 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001290 verbose(env, "R%d offset is outside of the packet\n", regno);
Edward Creef1174f72017-08-07 15:26:19 +01001291 return err;
1292 }
1293 return err;
1294}
1295
1296/* check access to 'struct bpf_context' fields. Supports fixed offsets only */
Yonghong Song31fd8582017-06-13 15:52:13 -07001297static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07001298 enum bpf_access_type t, enum bpf_reg_type *reg_type)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001299{
Daniel Borkmannf96da092017-07-02 02:13:27 +02001300 struct bpf_insn_access_aux info = {
1301 .reg_type = *reg_type,
1302 };
Yonghong Song31fd8582017-06-13 15:52:13 -07001303
Jakub Kicinski4f9218a2017-10-16 16:40:55 -07001304 if (env->ops->is_valid_access &&
1305 env->ops->is_valid_access(off, size, t, &info)) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02001306 /* A non zero info.ctx_field_size indicates that this field is a
1307 * candidate for later verifier transformation to load the whole
1308 * field and then apply a mask when accessed with a narrower
1309 * access than actual ctx access size. A zero info.ctx_field_size
1310 * will only allow for whole field access and rejects any other
1311 * type of narrower access.
Yonghong Song31fd8582017-06-13 15:52:13 -07001312 */
Yonghong Song23994632017-06-22 15:07:39 -07001313 *reg_type = info.reg_type;
Yonghong Song31fd8582017-06-13 15:52:13 -07001314
Jakub Kicinski4f9218a2017-10-16 16:40:55 -07001315 env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07001316 /* remember the offset of last byte accessed in ctx */
1317 if (env->prog->aux->max_ctx_offset < off + size)
1318 env->prog->aux->max_ctx_offset = off + size;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001319 return 0;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07001320 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001321
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001322 verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001323 return -EACCES;
1324}
1325
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001326static bool __is_pointer_value(bool allow_ptr_leaks,
1327 const struct bpf_reg_state *reg)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001328{
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001329 if (allow_ptr_leaks)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001330 return false;
1331
Edward Creef1174f72017-08-07 15:26:19 +01001332 return reg->type != SCALAR_VALUE;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001333}
1334
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001335static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
1336{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001337 return __is_pointer_value(env->allow_ptr_leaks, cur_regs(env) + regno);
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001338}
1339
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001340static int check_pkt_ptr_alignment(struct bpf_verifier_env *env,
1341 const struct bpf_reg_state *reg,
David S. Millerd1174412017-05-10 11:22:52 -07001342 int off, int size, bool strict)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001343{
Edward Creef1174f72017-08-07 15:26:19 +01001344 struct tnum reg_off;
David S. Millere07b98d2017-05-10 11:38:07 -07001345 int ip_align;
David S. Millerd1174412017-05-10 11:22:52 -07001346
1347 /* Byte size accesses are always allowed. */
1348 if (!strict || size == 1)
1349 return 0;
1350
David S. Millere4eda882017-05-22 12:27:07 -04001351 /* For platforms that do not have a Kconfig enabling
1352 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of
1353 * NET_IP_ALIGN is universally set to '2'. And on platforms
1354 * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get
1355 * to this code only in strict mode where we want to emulate
1356 * the NET_IP_ALIGN==2 checking. Therefore use an
1357 * unconditional IP align value of '2'.
David S. Millere07b98d2017-05-10 11:38:07 -07001358 */
David S. Millere4eda882017-05-22 12:27:07 -04001359 ip_align = 2;
Edward Creef1174f72017-08-07 15:26:19 +01001360
1361 reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off));
1362 if (!tnum_is_aligned(reg_off, size)) {
1363 char tn_buf[48];
1364
1365 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001366 verbose(env,
1367 "misaligned packet access off %d+%s+%d+%d size %d\n",
Edward Creef1174f72017-08-07 15:26:19 +01001368 ip_align, tn_buf, reg->off, off, size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001369 return -EACCES;
1370 }
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001371
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001372 return 0;
1373}
1374
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001375static int check_generic_ptr_alignment(struct bpf_verifier_env *env,
1376 const struct bpf_reg_state *reg,
Edward Creef1174f72017-08-07 15:26:19 +01001377 const char *pointer_desc,
1378 int off, int size, bool strict)
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001379{
Edward Creef1174f72017-08-07 15:26:19 +01001380 struct tnum reg_off;
1381
1382 /* Byte size accesses are always allowed. */
1383 if (!strict || size == 1)
1384 return 0;
1385
1386 reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off));
1387 if (!tnum_is_aligned(reg_off, size)) {
1388 char tn_buf[48];
1389
1390 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001391 verbose(env, "misaligned %saccess off %s+%d+%d size %d\n",
Edward Creef1174f72017-08-07 15:26:19 +01001392 pointer_desc, tn_buf, reg->off, off, size);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001393 return -EACCES;
1394 }
1395
1396 return 0;
1397}
1398
David S. Millere07b98d2017-05-10 11:38:07 -07001399static int check_ptr_alignment(struct bpf_verifier_env *env,
1400 const struct bpf_reg_state *reg,
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001401 int off, int size)
1402{
David S. Millere07b98d2017-05-10 11:38:07 -07001403 bool strict = env->strict_alignment;
Edward Creef1174f72017-08-07 15:26:19 +01001404 const char *pointer_desc = "";
David S. Millerd1174412017-05-10 11:22:52 -07001405
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001406 switch (reg->type) {
1407 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001408 case PTR_TO_PACKET_META:
1409 /* Special case, because of NET_IP_ALIGN. Given metadata sits
1410 * right in front, treat it the very same way.
1411 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001412 return check_pkt_ptr_alignment(env, reg, off, size, strict);
Edward Creef1174f72017-08-07 15:26:19 +01001413 case PTR_TO_MAP_VALUE:
1414 pointer_desc = "value ";
1415 break;
1416 case PTR_TO_CTX:
1417 pointer_desc = "context ";
1418 break;
1419 case PTR_TO_STACK:
1420 pointer_desc = "stack ";
1421 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001422 default:
Edward Creef1174f72017-08-07 15:26:19 +01001423 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001424 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001425 return check_generic_ptr_alignment(env, reg, pointer_desc, off, size,
1426 strict);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001427}
1428
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001429static int update_stack_depth(struct bpf_verifier_env *env,
1430 const struct bpf_func_state *func,
1431 int off)
1432{
1433 u16 stack = env->subprog_stack_depth[func->subprogno], total = 0;
1434 struct bpf_verifier_state *cur = env->cur_state;
1435 int i;
1436
1437 if (stack >= -off)
1438 return 0;
1439
1440 /* update known max for given subprogram */
1441 env->subprog_stack_depth[func->subprogno] = -off;
1442
1443 /* compute the total for current call chain */
1444 for (i = 0; i <= cur->curframe; i++) {
1445 u32 depth = env->subprog_stack_depth[cur->frame[i]->subprogno];
1446
1447 /* round up to 32-bytes, since this is granularity
1448 * of interpreter stack sizes
1449 */
1450 depth = round_up(depth, 32);
1451 total += depth;
1452 }
1453
1454 if (total > MAX_BPF_STACK) {
1455 verbose(env, "combined stack size of %d calls is %d. Too large\n",
1456 cur->curframe, total);
1457 return -EACCES;
1458 }
1459 return 0;
1460}
1461
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001462static int get_callee_stack_depth(struct bpf_verifier_env *env,
1463 const struct bpf_insn *insn, int idx)
1464{
1465 int start = idx + insn->imm + 1, subprog;
1466
1467 subprog = find_subprog(env, start);
1468 if (subprog < 0) {
1469 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
1470 start);
1471 return -EFAULT;
1472 }
1473 subprog++;
1474 return env->subprog_stack_depth[subprog];
1475}
1476
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001477/* check whether memory at (regno + off) is accessible for t = (read | write)
1478 * if t==write, value_regno is a register which value is stored into memory
1479 * if t==read, value_regno is a register which will receive the value from memory
1480 * if t==write && value_regno==-1, some unknown value is stored into memory
1481 * if t==read && value_regno==-1, don't care what we read from memory
1482 */
Yonghong Song31fd8582017-06-13 15:52:13 -07001483static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, int off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001484 int bpf_size, enum bpf_access_type t,
1485 int value_regno)
1486{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001487 struct bpf_reg_state *regs = cur_regs(env);
1488 struct bpf_reg_state *reg = regs + regno;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001489 struct bpf_func_state *state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001490 int size, err = 0;
1491
1492 size = bpf_size_to_bytes(bpf_size);
1493 if (size < 0)
1494 return size;
1495
Edward Creef1174f72017-08-07 15:26:19 +01001496 /* alignment checks will add in reg->off themselves */
David S. Millere07b98d2017-05-10 11:38:07 -07001497 err = check_ptr_alignment(env, reg, off, size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001498 if (err)
1499 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001500
Edward Creef1174f72017-08-07 15:26:19 +01001501 /* for access checks, reg->off is just part of off */
1502 off += reg->off;
1503
1504 if (reg->type == PTR_TO_MAP_VALUE) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001505 if (t == BPF_WRITE && value_regno >= 0 &&
1506 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001507 verbose(env, "R%d leaks addr into map\n", value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001508 return -EACCES;
1509 }
Josef Bacik48461132016-09-28 10:54:32 -04001510
Yonghong Song9fd29c02017-11-12 14:49:09 -08001511 err = check_map_access(env, regno, off, size, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001512 if (!err && t == BPF_READ && value_regno >= 0)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001513 mark_reg_unknown(env, regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001514
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07001515 } else if (reg->type == PTR_TO_CTX) {
Edward Creef1174f72017-08-07 15:26:19 +01001516 enum bpf_reg_type reg_type = SCALAR_VALUE;
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07001517
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001518 if (t == BPF_WRITE && value_regno >= 0 &&
1519 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001520 verbose(env, "R%d leaks addr into ctx\n", value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001521 return -EACCES;
1522 }
Edward Creef1174f72017-08-07 15:26:19 +01001523 /* ctx accesses must be at a fixed offset, so that we can
1524 * determine what type of data were returned.
1525 */
Jakub Kicinski28e33f92017-10-16 11:16:55 -07001526 if (reg->off) {
David S. Millerf8ddadc2017-10-22 13:36:53 +01001527 verbose(env,
1528 "dereference of modified ctx ptr R%d off=%d+%d, ctx+const is allowed, ctx+const+const is not\n",
Jakub Kicinski28e33f92017-10-16 11:16:55 -07001529 regno, reg->off, off - reg->off);
1530 return -EACCES;
1531 }
1532 if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
Edward Creef1174f72017-08-07 15:26:19 +01001533 char tn_buf[48];
1534
1535 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001536 verbose(env,
1537 "variable ctx access var_off=%s off=%d size=%d",
Edward Creef1174f72017-08-07 15:26:19 +01001538 tn_buf, off, size);
1539 return -EACCES;
1540 }
Yonghong Song31fd8582017-06-13 15:52:13 -07001541 err = check_ctx_access(env, insn_idx, off, size, t, &reg_type);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001542 if (!err && t == BPF_READ && value_regno >= 0) {
Edward Creef1174f72017-08-07 15:26:19 +01001543 /* ctx access returns either a scalar, or a
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001544 * PTR_TO_PACKET[_META,_END]. In the latter
1545 * case, we know the offset is zero.
Edward Creef1174f72017-08-07 15:26:19 +01001546 */
1547 if (reg_type == SCALAR_VALUE)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001548 mark_reg_unknown(env, regs, value_regno);
Edward Creef1174f72017-08-07 15:26:19 +01001549 else
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001550 mark_reg_known_zero(env, regs,
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001551 value_regno);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001552 regs[value_regno].id = 0;
1553 regs[value_regno].off = 0;
1554 regs[value_regno].range = 0;
1555 regs[value_regno].type = reg_type;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001556 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001557
Edward Creef1174f72017-08-07 15:26:19 +01001558 } else if (reg->type == PTR_TO_STACK) {
1559 /* stack accesses must be at a fixed offset, so that we can
1560 * determine what type of data were returned.
1561 * See check_stack_read().
1562 */
1563 if (!tnum_is_const(reg->var_off)) {
1564 char tn_buf[48];
1565
1566 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001567 verbose(env, "variable stack access var_off=%s off=%d size=%d",
Edward Creef1174f72017-08-07 15:26:19 +01001568 tn_buf, off, size);
1569 return -EACCES;
1570 }
1571 off += reg->var_off.value;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001572 if (off >= 0 || off < -MAX_BPF_STACK) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001573 verbose(env, "invalid stack off=%d size=%d\n", off,
1574 size);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001575 return -EACCES;
1576 }
Alexei Starovoitov87266792017-05-30 13:31:29 -07001577
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001578 state = func(env, reg);
1579 err = update_stack_depth(env, state, off);
1580 if (err)
1581 return err;
Alexei Starovoitov87266792017-05-30 13:31:29 -07001582
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001583 if (t == BPF_WRITE)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001584 err = check_stack_write(env, state, off, size,
1585 value_regno);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001586 else
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001587 err = check_stack_read(env, state, off, size,
1588 value_regno);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001589 } else if (reg_is_pkt_pointer(reg)) {
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001590 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001591 verbose(env, "cannot write into packet\n");
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001592 return -EACCES;
1593 }
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001594 if (t == BPF_WRITE && value_regno >= 0 &&
1595 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001596 verbose(env, "R%d leaks addr into packet\n",
1597 value_regno);
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001598 return -EACCES;
1599 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001600 err = check_packet_access(env, regno, off, size, false);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001601 if (!err && t == BPF_READ && value_regno >= 0)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001602 mark_reg_unknown(env, regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001603 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001604 verbose(env, "R%d invalid mem access '%s'\n", regno,
1605 reg_type_str[reg->type]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001606 return -EACCES;
1607 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001608
Edward Creef1174f72017-08-07 15:26:19 +01001609 if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ &&
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001610 regs[value_regno].type == SCALAR_VALUE) {
Edward Creef1174f72017-08-07 15:26:19 +01001611 /* b/h/w load zero-extends, mark upper bits as known 0 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001612 regs[value_regno].var_off =
1613 tnum_cast(regs[value_regno].var_off, size);
1614 __update_reg_bounds(&regs[value_regno]);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001615 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001616 return err;
1617}
1618
Yonghong Song31fd8582017-06-13 15:52:13 -07001619static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001620{
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001621 int err;
1622
1623 if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
1624 insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001625 verbose(env, "BPF_XADD uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001626 return -EINVAL;
1627 }
1628
1629 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01001630 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001631 if (err)
1632 return err;
1633
1634 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01001635 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001636 if (err)
1637 return err;
1638
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +02001639 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001640 verbose(env, "R%d leaks addr into mem\n", insn->src_reg);
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +02001641 return -EACCES;
1642 }
1643
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001644 /* check whether atomic_add can read the memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07001645 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001646 BPF_SIZE(insn->code), BPF_READ, -1);
1647 if (err)
1648 return err;
1649
1650 /* check whether atomic_add can write into the same memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07001651 return check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001652 BPF_SIZE(insn->code), BPF_WRITE, -1);
1653}
1654
1655/* when register 'regno' is passed into function that will read 'access_size'
1656 * bytes from that pointer, make sure that it's within stack boundary
Edward Creef1174f72017-08-07 15:26:19 +01001657 * and all elements of stack are initialized.
1658 * Unlike most pointer bounds-checking functions, this one doesn't take an
1659 * 'off' argument, so it has to add in reg->off itself.
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001660 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001661static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
Daniel Borkmann435faee12016-04-13 00:10:51 +02001662 int access_size, bool zero_size_allowed,
1663 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001664{
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001665 struct bpf_reg_state *reg = cur_regs(env) + regno;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001666 struct bpf_func_state *state = func(env, reg);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001667 int off, i, slot, spi;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001668
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001669 if (reg->type != PTR_TO_STACK) {
Edward Creef1174f72017-08-07 15:26:19 +01001670 /* Allow zero-byte read from NULL, regardless of pointer type */
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001671 if (zero_size_allowed && access_size == 0 &&
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001672 register_is_null(reg))
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001673 return 0;
1674
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001675 verbose(env, "R%d type=%s expected=%s\n", regno,
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001676 reg_type_str[reg->type],
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001677 reg_type_str[PTR_TO_STACK]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001678 return -EACCES;
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001679 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001680
Edward Creef1174f72017-08-07 15:26:19 +01001681 /* Only allow fixed-offset stack reads */
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001682 if (!tnum_is_const(reg->var_off)) {
Edward Creef1174f72017-08-07 15:26:19 +01001683 char tn_buf[48];
1684
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001685 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001686 verbose(env, "invalid variable stack read R%d var_off=%s\n",
Edward Creef1174f72017-08-07 15:26:19 +01001687 regno, tn_buf);
1688 }
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001689 off = reg->off + reg->var_off.value;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001690 if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
Yonghong Song9fd29c02017-11-12 14:49:09 -08001691 access_size < 0 || (access_size == 0 && !zero_size_allowed)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001692 verbose(env, "invalid stack type R%d off=%d access_size=%d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001693 regno, off, access_size);
1694 return -EACCES;
1695 }
1696
Daniel Borkmann435faee12016-04-13 00:10:51 +02001697 if (meta && meta->raw_mode) {
1698 meta->access_size = access_size;
1699 meta->regno = regno;
1700 return 0;
1701 }
1702
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001703 for (i = 0; i < access_size; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001704 u8 *stype;
1705
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001706 slot = -(off + i) - 1;
1707 spi = slot / BPF_REG_SIZE;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001708 if (state->allocated_stack <= slot)
1709 goto err;
1710 stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE];
1711 if (*stype == STACK_MISC)
1712 goto mark;
1713 if (*stype == STACK_ZERO) {
1714 /* helper can write anything into the stack */
1715 *stype = STACK_MISC;
1716 goto mark;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001717 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001718err:
1719 verbose(env, "invalid indirect read from stack off %d+%d size %d\n",
1720 off, i, access_size);
1721 return -EACCES;
1722mark:
1723 /* reading any byte out of 8-byte 'spill_slot' will cause
1724 * the whole slot to be marked as 'read'
1725 */
1726 mark_stack_slot_read(env, env->cur_state, env->cur_state->parent,
1727 spi, state->frameno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001728 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001729 return update_stack_depth(env, state, off);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001730}
1731
Gianluca Borello06c1c042017-01-09 10:19:49 -08001732static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
1733 int access_size, bool zero_size_allowed,
1734 struct bpf_call_arg_meta *meta)
1735{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001736 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
Gianluca Borello06c1c042017-01-09 10:19:49 -08001737
Edward Creef1174f72017-08-07 15:26:19 +01001738 switch (reg->type) {
Gianluca Borello06c1c042017-01-09 10:19:49 -08001739 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001740 case PTR_TO_PACKET_META:
Yonghong Song9fd29c02017-11-12 14:49:09 -08001741 return check_packet_access(env, regno, reg->off, access_size,
1742 zero_size_allowed);
Gianluca Borello06c1c042017-01-09 10:19:49 -08001743 case PTR_TO_MAP_VALUE:
Yonghong Song9fd29c02017-11-12 14:49:09 -08001744 return check_map_access(env, regno, reg->off, access_size,
1745 zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01001746 default: /* scalar_value|ptr_to_stack or invalid ptr */
Gianluca Borello06c1c042017-01-09 10:19:49 -08001747 return check_stack_boundary(env, regno, access_size,
1748 zero_size_allowed, meta);
1749 }
1750}
1751
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001752static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001753 enum bpf_arg_type arg_type,
1754 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001755{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001756 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001757 enum bpf_reg_type expected_type, type = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001758 int err = 0;
1759
Daniel Borkmann80f1d682015-03-12 17:21:42 +01001760 if (arg_type == ARG_DONTCARE)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001761 return 0;
1762
Edward Creedc503a82017-08-15 20:34:35 +01001763 err = check_reg_arg(env, regno, SRC_OP);
1764 if (err)
1765 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001766
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001767 if (arg_type == ARG_ANYTHING) {
1768 if (is_pointer_value(env, regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001769 verbose(env, "R%d leaks addr into helper function\n",
1770 regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001771 return -EACCES;
1772 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01001773 return 0;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001774 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01001775
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001776 if (type_is_pkt_pointer(type) &&
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001777 !may_access_direct_pkt_data(env, meta, BPF_READ)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001778 verbose(env, "helper access to the packet is not allowed\n");
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001779 return -EACCES;
1780 }
1781
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001782 if (arg_type == ARG_PTR_TO_MAP_KEY ||
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001783 arg_type == ARG_PTR_TO_MAP_VALUE) {
1784 expected_type = PTR_TO_STACK;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001785 if (!type_is_pkt_pointer(type) &&
1786 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001787 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001788 } else if (arg_type == ARG_CONST_SIZE ||
1789 arg_type == ARG_CONST_SIZE_OR_ZERO) {
Edward Creef1174f72017-08-07 15:26:19 +01001790 expected_type = SCALAR_VALUE;
1791 if (type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001792 goto err_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001793 } else if (arg_type == ARG_CONST_MAP_PTR) {
1794 expected_type = CONST_PTR_TO_MAP;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001795 if (type != expected_type)
1796 goto err_type;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001797 } else if (arg_type == ARG_PTR_TO_CTX) {
1798 expected_type = PTR_TO_CTX;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001799 if (type != expected_type)
1800 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001801 } else if (arg_type == ARG_PTR_TO_MEM ||
Gianluca Borellodb1ac492017-11-22 18:32:53 +00001802 arg_type == ARG_PTR_TO_MEM_OR_NULL ||
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001803 arg_type == ARG_PTR_TO_UNINIT_MEM) {
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001804 expected_type = PTR_TO_STACK;
1805 /* One exception here. In case function allows for NULL to be
Edward Creef1174f72017-08-07 15:26:19 +01001806 * passed in as argument, it's a SCALAR_VALUE type. Final test
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001807 * happens during stack boundary checking.
1808 */
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001809 if (register_is_null(reg) &&
Gianluca Borellodb1ac492017-11-22 18:32:53 +00001810 arg_type == ARG_PTR_TO_MEM_OR_NULL)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001811 /* final test in check_stack_boundary() */;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001812 else if (!type_is_pkt_pointer(type) &&
1813 type != PTR_TO_MAP_VALUE &&
Edward Creef1174f72017-08-07 15:26:19 +01001814 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001815 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001816 meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001817 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001818 verbose(env, "unsupported arg_type %d\n", arg_type);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001819 return -EFAULT;
1820 }
1821
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001822 if (arg_type == ARG_CONST_MAP_PTR) {
1823 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001824 meta->map_ptr = reg->map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001825 } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
1826 /* bpf_map_xxx(..., map_ptr, ..., key) call:
1827 * check that [key, key + map->key_size) are within
1828 * stack limits and initialized
1829 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001830 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001831 /* in function declaration map_ptr must come before
1832 * map_key, so that it's verified and known before
1833 * we have to check map_key here. Otherwise it means
1834 * that kernel subsystem misconfigured verifier
1835 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001836 verbose(env, "invalid map_ptr to access map->key\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001837 return -EACCES;
1838 }
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001839 if (type_is_pkt_pointer(type))
Edward Creef1174f72017-08-07 15:26:19 +01001840 err = check_packet_access(env, regno, reg->off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001841 meta->map_ptr->key_size,
1842 false);
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001843 else
1844 err = check_stack_boundary(env, regno,
1845 meta->map_ptr->key_size,
1846 false, NULL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001847 } else if (arg_type == ARG_PTR_TO_MAP_VALUE) {
1848 /* bpf_map_xxx(..., map_ptr, ..., value) call:
1849 * check [value, value + map->value_size) validity
1850 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001851 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001852 /* kernel subsystem misconfigured verifier */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001853 verbose(env, "invalid map_ptr to access map->value\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001854 return -EACCES;
1855 }
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001856 if (type_is_pkt_pointer(type))
Edward Creef1174f72017-08-07 15:26:19 +01001857 err = check_packet_access(env, regno, reg->off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001858 meta->map_ptr->value_size,
1859 false);
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001860 else
1861 err = check_stack_boundary(env, regno,
1862 meta->map_ptr->value_size,
1863 false, NULL);
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001864 } else if (arg_type == ARG_CONST_SIZE ||
1865 arg_type == ARG_CONST_SIZE_OR_ZERO) {
1866 bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001867
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001868 /* bpf_xxx(..., buf, len) call will access 'len' bytes
1869 * from stack pointer 'buf'. Check it
1870 * note: regno == len, regno - 1 == buf
1871 */
1872 if (regno == 0) {
1873 /* kernel subsystem misconfigured verifier */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001874 verbose(env,
1875 "ARG_CONST_SIZE cannot be first argument\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001876 return -EACCES;
1877 }
Gianluca Borello06c1c042017-01-09 10:19:49 -08001878
Edward Creef1174f72017-08-07 15:26:19 +01001879 /* The register is SCALAR_VALUE; the access check
1880 * happens using its boundaries.
Gianluca Borello06c1c042017-01-09 10:19:49 -08001881 */
Edward Creef1174f72017-08-07 15:26:19 +01001882
1883 if (!tnum_is_const(reg->var_off))
Gianluca Borello06c1c042017-01-09 10:19:49 -08001884 /* For unprivileged variable accesses, disable raw
1885 * mode so that the program is required to
1886 * initialize all the memory that the helper could
1887 * just partially fill up.
1888 */
1889 meta = NULL;
1890
Edward Creeb03c9f92017-08-07 15:26:36 +01001891 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001892 verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n",
Edward Creef1174f72017-08-07 15:26:19 +01001893 regno);
1894 return -EACCES;
1895 }
Gianluca Borello06c1c042017-01-09 10:19:49 -08001896
Edward Creeb03c9f92017-08-07 15:26:36 +01001897 if (reg->umin_value == 0) {
Edward Creef1174f72017-08-07 15:26:19 +01001898 err = check_helper_mem_access(env, regno - 1, 0,
1899 zero_size_allowed,
1900 meta);
Gianluca Borello06c1c042017-01-09 10:19:49 -08001901 if (err)
1902 return err;
Gianluca Borello06c1c042017-01-09 10:19:49 -08001903 }
Edward Creef1174f72017-08-07 15:26:19 +01001904
Edward Creeb03c9f92017-08-07 15:26:36 +01001905 if (reg->umax_value >= BPF_MAX_VAR_SIZ) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001906 verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
Edward Creef1174f72017-08-07 15:26:19 +01001907 regno);
1908 return -EACCES;
1909 }
1910 err = check_helper_mem_access(env, regno - 1,
Edward Creeb03c9f92017-08-07 15:26:36 +01001911 reg->umax_value,
Edward Creef1174f72017-08-07 15:26:19 +01001912 zero_size_allowed, meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001913 }
1914
1915 return err;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001916err_type:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001917 verbose(env, "R%d type=%s expected=%s\n", regno,
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001918 reg_type_str[type], reg_type_str[expected_type]);
1919 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001920}
1921
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001922static int check_map_func_compatibility(struct bpf_verifier_env *env,
1923 struct bpf_map *map, int func_id)
Kaixu Xia35578d72015-08-06 07:02:35 +00001924{
Kaixu Xia35578d72015-08-06 07:02:35 +00001925 if (!map)
1926 return 0;
1927
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001928 /* We need a two way check, first is from map perspective ... */
1929 switch (map->map_type) {
1930 case BPF_MAP_TYPE_PROG_ARRAY:
1931 if (func_id != BPF_FUNC_tail_call)
1932 goto error;
1933 break;
1934 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
1935 if (func_id != BPF_FUNC_perf_event_read &&
Yonghong Song908432c2017-10-05 09:19:20 -07001936 func_id != BPF_FUNC_perf_event_output &&
1937 func_id != BPF_FUNC_perf_event_read_value)
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001938 goto error;
1939 break;
1940 case BPF_MAP_TYPE_STACK_TRACE:
1941 if (func_id != BPF_FUNC_get_stackid)
1942 goto error;
1943 break;
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -07001944 case BPF_MAP_TYPE_CGROUP_ARRAY:
David S. Miller60747ef2016-08-18 01:17:32 -04001945 if (func_id != BPF_FUNC_skb_under_cgroup &&
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001946 func_id != BPF_FUNC_current_task_under_cgroup)
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07001947 goto error;
1948 break;
John Fastabend546ac1f2017-07-17 09:28:56 -07001949 /* devmap returns a pointer to a live net_device ifindex that we cannot
1950 * allow to be modified from bpf side. So do not allow lookup elements
1951 * for now.
1952 */
1953 case BPF_MAP_TYPE_DEVMAP:
John Fastabend2ddf71e2017-07-17 09:30:02 -07001954 if (func_id != BPF_FUNC_redirect_map)
John Fastabend546ac1f2017-07-17 09:28:56 -07001955 goto error;
1956 break;
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02001957 /* Restrict bpf side of cpumap, open when use-cases appear */
1958 case BPF_MAP_TYPE_CPUMAP:
1959 if (func_id != BPF_FUNC_redirect_map)
1960 goto error;
1961 break;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07001962 case BPF_MAP_TYPE_ARRAY_OF_MAPS:
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001963 case BPF_MAP_TYPE_HASH_OF_MAPS:
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07001964 if (func_id != BPF_FUNC_map_lookup_elem)
1965 goto error;
Martin KaFai Lau16a43622017-08-17 18:14:43 -07001966 break;
John Fastabend174a79f2017-08-15 22:32:47 -07001967 case BPF_MAP_TYPE_SOCKMAP:
1968 if (func_id != BPF_FUNC_sk_redirect_map &&
1969 func_id != BPF_FUNC_sock_map_update &&
1970 func_id != BPF_FUNC_map_delete_elem)
1971 goto error;
1972 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001973 default:
1974 break;
1975 }
1976
1977 /* ... and second from the function itself. */
1978 switch (func_id) {
1979 case BPF_FUNC_tail_call:
1980 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1981 goto error;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001982 if (env->subprog_cnt) {
1983 verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n");
1984 return -EINVAL;
1985 }
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001986 break;
1987 case BPF_FUNC_perf_event_read:
1988 case BPF_FUNC_perf_event_output:
Yonghong Song908432c2017-10-05 09:19:20 -07001989 case BPF_FUNC_perf_event_read_value:
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07001990 if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
1991 goto error;
1992 break;
1993 case BPF_FUNC_get_stackid:
1994 if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
1995 goto error;
1996 break;
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001997 case BPF_FUNC_current_task_under_cgroup:
Daniel Borkmann747ea552016-08-12 22:17:17 +02001998 case BPF_FUNC_skb_under_cgroup:
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07001999 if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
2000 goto error;
2001 break;
John Fastabend97f91a72017-07-17 09:29:18 -07002002 case BPF_FUNC_redirect_map:
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02002003 if (map->map_type != BPF_MAP_TYPE_DEVMAP &&
2004 map->map_type != BPF_MAP_TYPE_CPUMAP)
John Fastabend97f91a72017-07-17 09:29:18 -07002005 goto error;
2006 break;
John Fastabend174a79f2017-08-15 22:32:47 -07002007 case BPF_FUNC_sk_redirect_map:
2008 if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
2009 goto error;
2010 break;
2011 case BPF_FUNC_sock_map_update:
2012 if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
2013 goto error;
2014 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002015 default:
2016 break;
Kaixu Xia35578d72015-08-06 07:02:35 +00002017 }
2018
2019 return 0;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002020error:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002021 verbose(env, "cannot pass map_type %d into func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02002022 map->map_type, func_id_name(func_id), func_id);
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002023 return -EINVAL;
Kaixu Xia35578d72015-08-06 07:02:35 +00002024}
2025
Daniel Borkmann435faee12016-04-13 00:10:51 +02002026static int check_raw_mode(const struct bpf_func_proto *fn)
2027{
2028 int count = 0;
2029
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002030 if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002031 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002032 if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002033 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002034 if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002035 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002036 if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002037 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002038 if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002039 count++;
2040
2041 return count > 1 ? -EINVAL : 0;
2042}
2043
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002044/* Packet data might have moved, any old PTR_TO_PACKET[_META,_END]
2045 * are now invalid, so turn them into unknown SCALAR_VALUE.
Edward Creef1174f72017-08-07 15:26:19 +01002046 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002047static void __clear_all_pkt_pointers(struct bpf_verifier_env *env,
2048 struct bpf_func_state *state)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002049{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002050 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002051 int i;
2052
2053 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002054 if (reg_is_pkt_pointer_any(&regs[i]))
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002055 mark_reg_unknown(env, regs, i);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002056
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002057 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
2058 if (state->stack[i].slot_type[0] != STACK_SPILL)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002059 continue;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002060 reg = &state->stack[i].spilled_ptr;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002061 if (reg_is_pkt_pointer_any(reg))
2062 __mark_reg_unknown(reg);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002063 }
2064}
2065
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002066static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
2067{
2068 struct bpf_verifier_state *vstate = env->cur_state;
2069 int i;
2070
2071 for (i = 0; i <= vstate->curframe; i++)
2072 __clear_all_pkt_pointers(env, vstate->frame[i]);
2073}
2074
2075static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
2076 int *insn_idx)
2077{
2078 struct bpf_verifier_state *state = env->cur_state;
2079 struct bpf_func_state *caller, *callee;
2080 int i, subprog, target_insn;
2081
2082 if (state->curframe >= MAX_CALL_FRAMES) {
2083 verbose(env, "the call stack of %d frames is too deep\n",
2084 state->curframe);
2085 return -E2BIG;
2086 }
2087
2088 target_insn = *insn_idx + insn->imm;
2089 subprog = find_subprog(env, target_insn + 1);
2090 if (subprog < 0) {
2091 verbose(env, "verifier bug. No program starts at insn %d\n",
2092 target_insn + 1);
2093 return -EFAULT;
2094 }
2095
2096 caller = state->frame[state->curframe];
2097 if (state->frame[state->curframe + 1]) {
2098 verbose(env, "verifier bug. Frame %d already allocated\n",
2099 state->curframe + 1);
2100 return -EFAULT;
2101 }
2102
2103 callee = kzalloc(sizeof(*callee), GFP_KERNEL);
2104 if (!callee)
2105 return -ENOMEM;
2106 state->frame[state->curframe + 1] = callee;
2107
2108 /* callee cannot access r0, r6 - r9 for reading and has to write
2109 * into its own stack before reading from it.
2110 * callee can read/write into caller's stack
2111 */
2112 init_func_state(env, callee,
2113 /* remember the callsite, it will be used by bpf_exit */
2114 *insn_idx /* callsite */,
2115 state->curframe + 1 /* frameno within this callchain */,
2116 subprog + 1 /* subprog number within this prog */);
2117
2118 /* copy r1 - r5 args that callee can access */
2119 for (i = BPF_REG_1; i <= BPF_REG_5; i++)
2120 callee->regs[i] = caller->regs[i];
2121
2122 /* after the call regsiters r0 - r5 were scratched */
2123 for (i = 0; i < CALLER_SAVED_REGS; i++) {
2124 mark_reg_not_init(env, caller->regs, caller_saved[i]);
2125 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
2126 }
2127
2128 /* only increment it after check_reg_arg() finished */
2129 state->curframe++;
2130
2131 /* and go analyze first insn of the callee */
2132 *insn_idx = target_insn;
2133
2134 if (env->log.level) {
2135 verbose(env, "caller:\n");
2136 print_verifier_state(env, caller);
2137 verbose(env, "callee:\n");
2138 print_verifier_state(env, callee);
2139 }
2140 return 0;
2141}
2142
2143static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
2144{
2145 struct bpf_verifier_state *state = env->cur_state;
2146 struct bpf_func_state *caller, *callee;
2147 struct bpf_reg_state *r0;
2148
2149 callee = state->frame[state->curframe];
2150 r0 = &callee->regs[BPF_REG_0];
2151 if (r0->type == PTR_TO_STACK) {
2152 /* technically it's ok to return caller's stack pointer
2153 * (or caller's caller's pointer) back to the caller,
2154 * since these pointers are valid. Only current stack
2155 * pointer will be invalid as soon as function exits,
2156 * but let's be conservative
2157 */
2158 verbose(env, "cannot return stack pointer to the caller\n");
2159 return -EINVAL;
2160 }
2161
2162 state->curframe--;
2163 caller = state->frame[state->curframe];
2164 /* return to the caller whatever r0 had in the callee */
2165 caller->regs[BPF_REG_0] = *r0;
2166
2167 *insn_idx = callee->callsite + 1;
2168 if (env->log.level) {
2169 verbose(env, "returning from callee:\n");
2170 print_verifier_state(env, callee);
2171 verbose(env, "to caller at %d:\n", *insn_idx);
2172 print_verifier_state(env, caller);
2173 }
2174 /* clear everything in the callee */
2175 free_func_state(callee);
2176 state->frame[state->curframe + 1] = NULL;
2177 return 0;
2178}
2179
2180static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002181{
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002182 const struct bpf_func_proto *fn = NULL;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002183 struct bpf_reg_state *regs;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002184 struct bpf_call_arg_meta meta;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002185 bool changes_data;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002186 int i, err;
2187
2188 /* find function prototype */
2189 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002190 verbose(env, "invalid func %s#%d\n", func_id_name(func_id),
2191 func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002192 return -EINVAL;
2193 }
2194
Jakub Kicinski00176a32017-10-16 16:40:54 -07002195 if (env->ops->get_func_proto)
2196 fn = env->ops->get_func_proto(func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002197
2198 if (!fn) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002199 verbose(env, "unknown func %s#%d\n", func_id_name(func_id),
2200 func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002201 return -EINVAL;
2202 }
2203
2204 /* eBPF programs must be GPL compatible to use GPL-ed functions */
Daniel Borkmann24701ec2015-03-01 12:31:47 +01002205 if (!env->prog->gpl_compatible && fn->gpl_only) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002206 verbose(env, "cannot call GPL only function from proprietary program\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002207 return -EINVAL;
2208 }
2209
Martin KaFai Lau17bedab2016-12-07 15:53:11 -08002210 changes_data = bpf_helper_changes_pkt_data(fn->func);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002211
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002212 memset(&meta, 0, sizeof(meta));
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002213 meta.pkt_access = fn->pkt_access;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002214
Daniel Borkmann435faee12016-04-13 00:10:51 +02002215 /* We only support one arg being in raw mode at the moment, which
2216 * is sufficient for the helper functions we have right now.
2217 */
2218 err = check_raw_mode(fn);
2219 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002220 verbose(env, "kernel subsystem misconfigured func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02002221 func_id_name(func_id), func_id);
Daniel Borkmann435faee12016-04-13 00:10:51 +02002222 return err;
2223 }
2224
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002225 /* check args */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002226 err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002227 if (err)
2228 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002229 err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002230 if (err)
2231 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002232 err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002233 if (err)
2234 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002235 err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002236 if (err)
2237 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002238 err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002239 if (err)
2240 return err;
2241
Daniel Borkmann435faee12016-04-13 00:10:51 +02002242 /* Mark slots with STACK_MISC in case of raw mode, stack offset
2243 * is inferred from register state.
2244 */
2245 for (i = 0; i < meta.access_size; i++) {
Yonghong Song31fd8582017-06-13 15:52:13 -07002246 err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, BPF_WRITE, -1);
Daniel Borkmann435faee12016-04-13 00:10:51 +02002247 if (err)
2248 return err;
2249 }
2250
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002251 regs = cur_regs(env);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002252 /* reset caller saved regs */
Edward Creedc503a82017-08-15 20:34:35 +01002253 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002254 mark_reg_not_init(env, regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01002255 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
2256 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002257
Edward Creedc503a82017-08-15 20:34:35 +01002258 /* update return register (already marked as written above) */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002259 if (fn->ret_type == RET_INTEGER) {
Edward Creef1174f72017-08-07 15:26:19 +01002260 /* sets type to SCALAR_VALUE */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002261 mark_reg_unknown(env, regs, BPF_REG_0);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002262 } else if (fn->ret_type == RET_VOID) {
2263 regs[BPF_REG_0].type = NOT_INIT;
2264 } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) {
Martin KaFai Laufad73a12017-03-22 10:00:32 -07002265 struct bpf_insn_aux_data *insn_aux;
2266
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002267 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
Edward Creef1174f72017-08-07 15:26:19 +01002268 /* There is no offset yet applied, variable or fixed */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002269 mark_reg_known_zero(env, regs, BPF_REG_0);
Edward Creef1174f72017-08-07 15:26:19 +01002270 regs[BPF_REG_0].off = 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002271 /* remember map_ptr, so that check_map_access()
2272 * can check 'value_size' boundary of memory access
2273 * to map element returned from bpf_map_lookup_elem()
2274 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002275 if (meta.map_ptr == NULL) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002276 verbose(env,
2277 "kernel subsystem misconfigured verifier\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002278 return -EINVAL;
2279 }
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002280 regs[BPF_REG_0].map_ptr = meta.map_ptr;
Thomas Graf57a09bf2016-10-18 19:51:19 +02002281 regs[BPF_REG_0].id = ++env->id_gen;
Martin KaFai Laufad73a12017-03-22 10:00:32 -07002282 insn_aux = &env->insn_aux_data[insn_idx];
2283 if (!insn_aux->map_ptr)
2284 insn_aux->map_ptr = meta.map_ptr;
2285 else if (insn_aux->map_ptr != meta.map_ptr)
2286 insn_aux->map_ptr = BPF_MAP_PTR_POISON;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002287 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002288 verbose(env, "unknown return type %d of func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02002289 fn->ret_type, func_id_name(func_id), func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002290 return -EINVAL;
2291 }
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07002292
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002293 err = check_map_func_compatibility(env, meta.map_ptr, func_id);
Kaixu Xia35578d72015-08-06 07:02:35 +00002294 if (err)
2295 return err;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07002296
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002297 if (changes_data)
2298 clear_all_pkt_pointers(env);
2299 return 0;
2300}
2301
Edward Creef1174f72017-08-07 15:26:19 +01002302static void coerce_reg_to_32(struct bpf_reg_state *reg)
David S. Millerd1174412017-05-10 11:22:52 -07002303{
Edward Creef1174f72017-08-07 15:26:19 +01002304 /* clear high 32 bits */
2305 reg->var_off = tnum_cast(reg->var_off, 4);
Edward Creeb03c9f92017-08-07 15:26:36 +01002306 /* Update bounds */
2307 __update_reg_bounds(reg);
2308}
2309
2310static bool signed_add_overflows(s64 a, s64 b)
2311{
2312 /* Do the add in u64, where overflow is well-defined */
2313 s64 res = (s64)((u64)a + (u64)b);
2314
2315 if (b < 0)
2316 return res > a;
2317 return res < a;
2318}
2319
2320static bool signed_sub_overflows(s64 a, s64 b)
2321{
2322 /* Do the sub in u64, where overflow is well-defined */
2323 s64 res = (s64)((u64)a - (u64)b);
2324
2325 if (b < 0)
2326 return res < a;
2327 return res > a;
David S. Millerd1174412017-05-10 11:22:52 -07002328}
2329
Edward Creef1174f72017-08-07 15:26:19 +01002330/* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off.
Edward Creef1174f72017-08-07 15:26:19 +01002331 * Caller should also handle BPF_MOV case separately.
2332 * If we return -EACCES, caller may want to try again treating pointer as a
2333 * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks.
2334 */
2335static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
2336 struct bpf_insn *insn,
2337 const struct bpf_reg_state *ptr_reg,
2338 const struct bpf_reg_state *off_reg)
Josef Bacik48461132016-09-28 10:54:32 -04002339{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002340 struct bpf_verifier_state *vstate = env->cur_state;
2341 struct bpf_func_state *state = vstate->frame[vstate->curframe];
2342 struct bpf_reg_state *regs = state->regs, *dst_reg;
Edward Creef1174f72017-08-07 15:26:19 +01002343 bool known = tnum_is_const(off_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01002344 s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value,
2345 smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
2346 u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value,
2347 umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value;
Josef Bacik48461132016-09-28 10:54:32 -04002348 u8 opcode = BPF_OP(insn->code);
Edward Creef1174f72017-08-07 15:26:19 +01002349 u32 dst = insn->dst_reg;
Josef Bacik48461132016-09-28 10:54:32 -04002350
Edward Creef1174f72017-08-07 15:26:19 +01002351 dst_reg = &regs[dst];
Josef Bacik48461132016-09-28 10:54:32 -04002352
Edward Creeb03c9f92017-08-07 15:26:36 +01002353 if (WARN_ON_ONCE(known && (smin_val != smax_val))) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002354 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002355 verbose(env,
2356 "verifier internal error: known but bad sbounds\n");
Edward Creeb03c9f92017-08-07 15:26:36 +01002357 return -EINVAL;
2358 }
2359 if (WARN_ON_ONCE(known && (umin_val != umax_val))) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002360 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002361 verbose(env,
2362 "verifier internal error: known but bad ubounds\n");
Edward Creef1174f72017-08-07 15:26:19 +01002363 return -EINVAL;
Josef Bacik48461132016-09-28 10:54:32 -04002364 }
2365
Edward Creef1174f72017-08-07 15:26:19 +01002366 if (BPF_CLASS(insn->code) != BPF_ALU64) {
2367 /* 32-bit ALU ops on pointers produce (meaningless) scalars */
2368 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002369 verbose(env,
2370 "R%d 32-bit pointer arithmetic prohibited\n",
Edward Creef1174f72017-08-07 15:26:19 +01002371 dst);
2372 return -EACCES;
2373 }
David S. Millerd1174412017-05-10 11:22:52 -07002374
Edward Creef1174f72017-08-07 15:26:19 +01002375 if (ptr_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
2376 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002377 verbose(env, "R%d pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL prohibited, null-check it first\n",
Edward Creef1174f72017-08-07 15:26:19 +01002378 dst);
2379 return -EACCES;
2380 }
2381 if (ptr_reg->type == CONST_PTR_TO_MAP) {
2382 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002383 verbose(env, "R%d pointer arithmetic on CONST_PTR_TO_MAP prohibited\n",
Edward Creef1174f72017-08-07 15:26:19 +01002384 dst);
2385 return -EACCES;
2386 }
2387 if (ptr_reg->type == PTR_TO_PACKET_END) {
2388 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002389 verbose(env, "R%d pointer arithmetic on PTR_TO_PACKET_END prohibited\n",
Edward Creef1174f72017-08-07 15:26:19 +01002390 dst);
2391 return -EACCES;
2392 }
2393
2394 /* In case of 'scalar += pointer', dst_reg inherits pointer type and id.
2395 * The id may be overwritten later if we create a new variable offset.
Josef Bacik48461132016-09-28 10:54:32 -04002396 */
Edward Creef1174f72017-08-07 15:26:19 +01002397 dst_reg->type = ptr_reg->type;
2398 dst_reg->id = ptr_reg->id;
Josef Bacikf23cc642016-11-14 15:45:36 -05002399
Josef Bacik48461132016-09-28 10:54:32 -04002400 switch (opcode) {
2401 case BPF_ADD:
Edward Creef1174f72017-08-07 15:26:19 +01002402 /* We can take a fixed offset as long as it doesn't overflow
2403 * the s32 'off' field
2404 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002405 if (known && (ptr_reg->off + smin_val ==
2406 (s64)(s32)(ptr_reg->off + smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01002407 /* pointer += K. Accumulate it into fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01002408 dst_reg->smin_value = smin_ptr;
2409 dst_reg->smax_value = smax_ptr;
2410 dst_reg->umin_value = umin_ptr;
2411 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01002412 dst_reg->var_off = ptr_reg->var_off;
Edward Creeb03c9f92017-08-07 15:26:36 +01002413 dst_reg->off = ptr_reg->off + smin_val;
Edward Creef1174f72017-08-07 15:26:19 +01002414 dst_reg->range = ptr_reg->range;
2415 break;
2416 }
Edward Creef1174f72017-08-07 15:26:19 +01002417 /* A new variable offset is created. Note that off_reg->off
2418 * == 0, since it's a scalar.
2419 * dst_reg gets the pointer type and since some positive
2420 * integer value was added to the pointer, give it a new 'id'
2421 * if it's a PTR_TO_PACKET.
2422 * this creates a new 'base' pointer, off_reg (variable) gets
2423 * added into the variable offset, and we copy the fixed offset
2424 * from ptr_reg.
2425 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002426 if (signed_add_overflows(smin_ptr, smin_val) ||
2427 signed_add_overflows(smax_ptr, smax_val)) {
2428 dst_reg->smin_value = S64_MIN;
2429 dst_reg->smax_value = S64_MAX;
2430 } else {
2431 dst_reg->smin_value = smin_ptr + smin_val;
2432 dst_reg->smax_value = smax_ptr + smax_val;
2433 }
2434 if (umin_ptr + umin_val < umin_ptr ||
2435 umax_ptr + umax_val < umax_ptr) {
2436 dst_reg->umin_value = 0;
2437 dst_reg->umax_value = U64_MAX;
2438 } else {
2439 dst_reg->umin_value = umin_ptr + umin_val;
2440 dst_reg->umax_value = umax_ptr + umax_val;
2441 }
Edward Creef1174f72017-08-07 15:26:19 +01002442 dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off);
2443 dst_reg->off = ptr_reg->off;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002444 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01002445 dst_reg->id = ++env->id_gen;
2446 /* something was added to pkt_ptr, set range to zero */
2447 dst_reg->range = 0;
2448 }
Josef Bacik48461132016-09-28 10:54:32 -04002449 break;
2450 case BPF_SUB:
Edward Creef1174f72017-08-07 15:26:19 +01002451 if (dst_reg == off_reg) {
2452 /* scalar -= pointer. Creates an unknown scalar */
2453 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002454 verbose(env, "R%d tried to subtract pointer from scalar\n",
Edward Creef1174f72017-08-07 15:26:19 +01002455 dst);
2456 return -EACCES;
2457 }
2458 /* We don't allow subtraction from FP, because (according to
2459 * test_verifier.c test "invalid fp arithmetic", JITs might not
2460 * be able to deal with it.
Edward Cree93057062017-07-21 14:37:34 +01002461 */
Edward Creef1174f72017-08-07 15:26:19 +01002462 if (ptr_reg->type == PTR_TO_STACK) {
2463 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002464 verbose(env, "R%d subtraction from stack pointer prohibited\n",
Edward Creef1174f72017-08-07 15:26:19 +01002465 dst);
2466 return -EACCES;
2467 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002468 if (known && (ptr_reg->off - smin_val ==
2469 (s64)(s32)(ptr_reg->off - smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01002470 /* pointer -= K. Subtract it from fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01002471 dst_reg->smin_value = smin_ptr;
2472 dst_reg->smax_value = smax_ptr;
2473 dst_reg->umin_value = umin_ptr;
2474 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01002475 dst_reg->var_off = ptr_reg->var_off;
2476 dst_reg->id = ptr_reg->id;
Edward Creeb03c9f92017-08-07 15:26:36 +01002477 dst_reg->off = ptr_reg->off - smin_val;
Edward Creef1174f72017-08-07 15:26:19 +01002478 dst_reg->range = ptr_reg->range;
2479 break;
2480 }
Edward Creef1174f72017-08-07 15:26:19 +01002481 /* A new variable offset is created. If the subtrahend is known
2482 * nonnegative, then any reg->range we had before is still good.
2483 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002484 if (signed_sub_overflows(smin_ptr, smax_val) ||
2485 signed_sub_overflows(smax_ptr, smin_val)) {
2486 /* Overflow possible, we know nothing */
2487 dst_reg->smin_value = S64_MIN;
2488 dst_reg->smax_value = S64_MAX;
2489 } else {
2490 dst_reg->smin_value = smin_ptr - smax_val;
2491 dst_reg->smax_value = smax_ptr - smin_val;
2492 }
2493 if (umin_ptr < umax_val) {
2494 /* Overflow possible, we know nothing */
2495 dst_reg->umin_value = 0;
2496 dst_reg->umax_value = U64_MAX;
2497 } else {
2498 /* Cannot overflow (as long as bounds are consistent) */
2499 dst_reg->umin_value = umin_ptr - umax_val;
2500 dst_reg->umax_value = umax_ptr - umin_val;
2501 }
Edward Creef1174f72017-08-07 15:26:19 +01002502 dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off);
2503 dst_reg->off = ptr_reg->off;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002504 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01002505 dst_reg->id = ++env->id_gen;
2506 /* something was added to pkt_ptr, set range to zero */
Edward Creeb03c9f92017-08-07 15:26:36 +01002507 if (smin_val < 0)
Edward Creef1174f72017-08-07 15:26:19 +01002508 dst_reg->range = 0;
2509 }
2510 break;
2511 case BPF_AND:
2512 case BPF_OR:
2513 case BPF_XOR:
2514 /* bitwise ops on pointers are troublesome, prohibit for now.
2515 * (However, in principle we could allow some cases, e.g.
2516 * ptr &= ~3 which would reduce min_value by 3.)
2517 */
2518 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002519 verbose(env, "R%d bitwise operator %s on pointer prohibited\n",
Edward Creef1174f72017-08-07 15:26:19 +01002520 dst, bpf_alu_string[opcode >> 4]);
2521 return -EACCES;
2522 default:
2523 /* other operators (e.g. MUL,LSH) produce non-pointer results */
2524 if (!env->allow_ptr_leaks)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002525 verbose(env, "R%d pointer arithmetic with %s operator prohibited\n",
Edward Creef1174f72017-08-07 15:26:19 +01002526 dst, bpf_alu_string[opcode >> 4]);
2527 return -EACCES;
2528 }
2529
Edward Creeb03c9f92017-08-07 15:26:36 +01002530 __update_reg_bounds(dst_reg);
2531 __reg_deduce_bounds(dst_reg);
2532 __reg_bound_offset(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002533 return 0;
2534}
2535
2536static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
2537 struct bpf_insn *insn,
2538 struct bpf_reg_state *dst_reg,
2539 struct bpf_reg_state src_reg)
2540{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002541 struct bpf_reg_state *regs = cur_regs(env);
Edward Creef1174f72017-08-07 15:26:19 +01002542 u8 opcode = BPF_OP(insn->code);
2543 bool src_known, dst_known;
Edward Creeb03c9f92017-08-07 15:26:36 +01002544 s64 smin_val, smax_val;
2545 u64 umin_val, umax_val;
Edward Creef1174f72017-08-07 15:26:19 +01002546
2547 if (BPF_CLASS(insn->code) != BPF_ALU64) {
2548 /* 32-bit ALU ops are (32,32)->64 */
2549 coerce_reg_to_32(dst_reg);
2550 coerce_reg_to_32(&src_reg);
2551 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002552 smin_val = src_reg.smin_value;
2553 smax_val = src_reg.smax_value;
2554 umin_val = src_reg.umin_value;
2555 umax_val = src_reg.umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01002556 src_known = tnum_is_const(src_reg.var_off);
2557 dst_known = tnum_is_const(dst_reg->var_off);
2558
2559 switch (opcode) {
2560 case BPF_ADD:
Edward Creeb03c9f92017-08-07 15:26:36 +01002561 if (signed_add_overflows(dst_reg->smin_value, smin_val) ||
2562 signed_add_overflows(dst_reg->smax_value, smax_val)) {
2563 dst_reg->smin_value = S64_MIN;
2564 dst_reg->smax_value = S64_MAX;
2565 } else {
2566 dst_reg->smin_value += smin_val;
2567 dst_reg->smax_value += smax_val;
2568 }
2569 if (dst_reg->umin_value + umin_val < umin_val ||
2570 dst_reg->umax_value + umax_val < umax_val) {
2571 dst_reg->umin_value = 0;
2572 dst_reg->umax_value = U64_MAX;
2573 } else {
2574 dst_reg->umin_value += umin_val;
2575 dst_reg->umax_value += umax_val;
2576 }
Edward Creef1174f72017-08-07 15:26:19 +01002577 dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off);
2578 break;
2579 case BPF_SUB:
Edward Creeb03c9f92017-08-07 15:26:36 +01002580 if (signed_sub_overflows(dst_reg->smin_value, smax_val) ||
2581 signed_sub_overflows(dst_reg->smax_value, smin_val)) {
2582 /* Overflow possible, we know nothing */
2583 dst_reg->smin_value = S64_MIN;
2584 dst_reg->smax_value = S64_MAX;
2585 } else {
2586 dst_reg->smin_value -= smax_val;
2587 dst_reg->smax_value -= smin_val;
2588 }
2589 if (dst_reg->umin_value < umax_val) {
2590 /* Overflow possible, we know nothing */
2591 dst_reg->umin_value = 0;
2592 dst_reg->umax_value = U64_MAX;
2593 } else {
2594 /* Cannot overflow (as long as bounds are consistent) */
2595 dst_reg->umin_value -= umax_val;
2596 dst_reg->umax_value -= umin_val;
2597 }
Edward Creef1174f72017-08-07 15:26:19 +01002598 dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off);
Josef Bacik48461132016-09-28 10:54:32 -04002599 break;
2600 case BPF_MUL:
Edward Creeb03c9f92017-08-07 15:26:36 +01002601 dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off);
2602 if (smin_val < 0 || dst_reg->smin_value < 0) {
Edward Creef1174f72017-08-07 15:26:19 +01002603 /* Ain't nobody got time to multiply that sign */
Edward Creeb03c9f92017-08-07 15:26:36 +01002604 __mark_reg_unbounded(dst_reg);
2605 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002606 break;
2607 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002608 /* Both values are positive, so we can work with unsigned and
2609 * copy the result to signed (unless it exceeds S64_MAX).
Edward Creef1174f72017-08-07 15:26:19 +01002610 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002611 if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) {
2612 /* Potential overflow, we know nothing */
2613 __mark_reg_unbounded(dst_reg);
2614 /* (except what we can learn from the var_off) */
2615 __update_reg_bounds(dst_reg);
2616 break;
2617 }
2618 dst_reg->umin_value *= umin_val;
2619 dst_reg->umax_value *= umax_val;
2620 if (dst_reg->umax_value > S64_MAX) {
2621 /* Overflow possible, we know nothing */
2622 dst_reg->smin_value = S64_MIN;
2623 dst_reg->smax_value = S64_MAX;
2624 } else {
2625 dst_reg->smin_value = dst_reg->umin_value;
2626 dst_reg->smax_value = dst_reg->umax_value;
2627 }
Josef Bacik48461132016-09-28 10:54:32 -04002628 break;
2629 case BPF_AND:
Edward Creef1174f72017-08-07 15:26:19 +01002630 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01002631 __mark_reg_known(dst_reg, dst_reg->var_off.value &
2632 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01002633 break;
2634 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002635 /* We get our minimum from the var_off, since that's inherently
2636 * bitwise. Our maximum is the minimum of the operands' maxima.
Josef Bacikf23cc642016-11-14 15:45:36 -05002637 */
Edward Creef1174f72017-08-07 15:26:19 +01002638 dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01002639 dst_reg->umin_value = dst_reg->var_off.value;
2640 dst_reg->umax_value = min(dst_reg->umax_value, umax_val);
2641 if (dst_reg->smin_value < 0 || smin_val < 0) {
2642 /* Lose signed bounds when ANDing negative numbers,
2643 * ain't nobody got time for that.
2644 */
2645 dst_reg->smin_value = S64_MIN;
2646 dst_reg->smax_value = S64_MAX;
2647 } else {
2648 /* ANDing two positives gives a positive, so safe to
2649 * cast result into s64.
2650 */
2651 dst_reg->smin_value = dst_reg->umin_value;
2652 dst_reg->smax_value = dst_reg->umax_value;
2653 }
2654 /* We may learn something more from the var_off */
2655 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002656 break;
2657 case BPF_OR:
2658 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01002659 __mark_reg_known(dst_reg, dst_reg->var_off.value |
2660 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01002661 break;
2662 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002663 /* We get our maximum from the var_off, and our minimum is the
2664 * maximum of the operands' minima
Edward Creef1174f72017-08-07 15:26:19 +01002665 */
2666 dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01002667 dst_reg->umin_value = max(dst_reg->umin_value, umin_val);
2668 dst_reg->umax_value = dst_reg->var_off.value |
2669 dst_reg->var_off.mask;
2670 if (dst_reg->smin_value < 0 || smin_val < 0) {
2671 /* Lose signed bounds when ORing negative numbers,
2672 * ain't nobody got time for that.
2673 */
2674 dst_reg->smin_value = S64_MIN;
2675 dst_reg->smax_value = S64_MAX;
Edward Creef1174f72017-08-07 15:26:19 +01002676 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01002677 /* ORing two positives gives a positive, so safe to
2678 * cast result into s64.
2679 */
2680 dst_reg->smin_value = dst_reg->umin_value;
2681 dst_reg->smax_value = dst_reg->umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01002682 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002683 /* We may learn something more from the var_off */
2684 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002685 break;
2686 case BPF_LSH:
Edward Creeb03c9f92017-08-07 15:26:36 +01002687 if (umax_val > 63) {
2688 /* Shifts greater than 63 are undefined. This includes
2689 * shifts by a negative number.
2690 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002691 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002692 break;
2693 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002694 /* We lose all sign bit information (except what we can pick
2695 * up from var_off)
Josef Bacik48461132016-09-28 10:54:32 -04002696 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002697 dst_reg->smin_value = S64_MIN;
2698 dst_reg->smax_value = S64_MAX;
2699 /* If we might shift our top bit out, then we know nothing */
2700 if (dst_reg->umax_value > 1ULL << (63 - umax_val)) {
2701 dst_reg->umin_value = 0;
2702 dst_reg->umax_value = U64_MAX;
David S. Millerd1174412017-05-10 11:22:52 -07002703 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01002704 dst_reg->umin_value <<= umin_val;
2705 dst_reg->umax_value <<= umax_val;
David S. Millerd1174412017-05-10 11:22:52 -07002706 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002707 if (src_known)
2708 dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val);
2709 else
2710 dst_reg->var_off = tnum_lshift(tnum_unknown, umin_val);
2711 /* We may learn something more from the var_off */
2712 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002713 break;
2714 case BPF_RSH:
Edward Creeb03c9f92017-08-07 15:26:36 +01002715 if (umax_val > 63) {
2716 /* Shifts greater than 63 are undefined. This includes
2717 * shifts by a negative number.
2718 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002719 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002720 break;
2721 }
2722 /* BPF_RSH is an unsigned shift, so make the appropriate casts */
Edward Creeb03c9f92017-08-07 15:26:36 +01002723 if (dst_reg->smin_value < 0) {
2724 if (umin_val) {
Edward Creef1174f72017-08-07 15:26:19 +01002725 /* Sign bit will be cleared */
Edward Creeb03c9f92017-08-07 15:26:36 +01002726 dst_reg->smin_value = 0;
2727 } else {
2728 /* Lost sign bit information */
2729 dst_reg->smin_value = S64_MIN;
2730 dst_reg->smax_value = S64_MAX;
2731 }
David S. Millerd1174412017-05-10 11:22:52 -07002732 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01002733 dst_reg->smin_value =
2734 (u64)(dst_reg->smin_value) >> umax_val;
David S. Millerd1174412017-05-10 11:22:52 -07002735 }
Edward Creef1174f72017-08-07 15:26:19 +01002736 if (src_known)
Edward Creeb03c9f92017-08-07 15:26:36 +01002737 dst_reg->var_off = tnum_rshift(dst_reg->var_off,
2738 umin_val);
Edward Creef1174f72017-08-07 15:26:19 +01002739 else
Edward Creeb03c9f92017-08-07 15:26:36 +01002740 dst_reg->var_off = tnum_rshift(tnum_unknown, umin_val);
2741 dst_reg->umin_value >>= umax_val;
2742 dst_reg->umax_value >>= umin_val;
2743 /* We may learn something more from the var_off */
2744 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002745 break;
2746 default:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002747 mark_reg_unknown(env, regs, insn->dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002748 break;
2749 }
2750
Edward Creeb03c9f92017-08-07 15:26:36 +01002751 __reg_deduce_bounds(dst_reg);
2752 __reg_bound_offset(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002753 return 0;
2754}
2755
2756/* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max
2757 * and var_off.
2758 */
2759static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
2760 struct bpf_insn *insn)
2761{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002762 struct bpf_verifier_state *vstate = env->cur_state;
2763 struct bpf_func_state *state = vstate->frame[vstate->curframe];
2764 struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg;
Edward Creef1174f72017-08-07 15:26:19 +01002765 struct bpf_reg_state *ptr_reg = NULL, off_reg = {0};
2766 u8 opcode = BPF_OP(insn->code);
2767 int rc;
2768
2769 dst_reg = &regs[insn->dst_reg];
Edward Creef1174f72017-08-07 15:26:19 +01002770 src_reg = NULL;
2771 if (dst_reg->type != SCALAR_VALUE)
2772 ptr_reg = dst_reg;
2773 if (BPF_SRC(insn->code) == BPF_X) {
2774 src_reg = &regs[insn->src_reg];
Edward Creef1174f72017-08-07 15:26:19 +01002775 if (src_reg->type != SCALAR_VALUE) {
2776 if (dst_reg->type != SCALAR_VALUE) {
2777 /* Combining two pointers by any ALU op yields
2778 * an arbitrary scalar.
2779 */
2780 if (!env->allow_ptr_leaks) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002781 verbose(env, "R%d pointer %s pointer prohibited\n",
Edward Creef1174f72017-08-07 15:26:19 +01002782 insn->dst_reg,
2783 bpf_alu_string[opcode >> 4]);
2784 return -EACCES;
2785 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002786 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002787 return 0;
2788 } else {
2789 /* scalar += pointer
2790 * This is legal, but we have to reverse our
2791 * src/dest handling in computing the range
2792 */
2793 rc = adjust_ptr_min_max_vals(env, insn,
2794 src_reg, dst_reg);
2795 if (rc == -EACCES && env->allow_ptr_leaks) {
2796 /* scalar += unknown scalar */
2797 __mark_reg_unknown(&off_reg);
2798 return adjust_scalar_min_max_vals(
2799 env, insn,
2800 dst_reg, off_reg);
2801 }
2802 return rc;
2803 }
2804 } else if (ptr_reg) {
2805 /* pointer += scalar */
2806 rc = adjust_ptr_min_max_vals(env, insn,
2807 dst_reg, src_reg);
2808 if (rc == -EACCES && env->allow_ptr_leaks) {
2809 /* unknown scalar += scalar */
2810 __mark_reg_unknown(dst_reg);
2811 return adjust_scalar_min_max_vals(
2812 env, insn, dst_reg, *src_reg);
2813 }
2814 return rc;
2815 }
2816 } else {
2817 /* Pretend the src is a reg with a known value, since we only
2818 * need to be able to read from this state.
2819 */
2820 off_reg.type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01002821 __mark_reg_known(&off_reg, insn->imm);
Edward Creef1174f72017-08-07 15:26:19 +01002822 src_reg = &off_reg;
Edward Creef1174f72017-08-07 15:26:19 +01002823 if (ptr_reg) { /* pointer += K */
2824 rc = adjust_ptr_min_max_vals(env, insn,
2825 ptr_reg, src_reg);
2826 if (rc == -EACCES && env->allow_ptr_leaks) {
2827 /* unknown scalar += K */
2828 __mark_reg_unknown(dst_reg);
2829 return adjust_scalar_min_max_vals(
2830 env, insn, dst_reg, off_reg);
2831 }
2832 return rc;
2833 }
2834 }
2835
2836 /* Got here implies adding two SCALAR_VALUEs */
2837 if (WARN_ON_ONCE(ptr_reg)) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002838 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002839 verbose(env, "verifier internal error: unexpected ptr_reg\n");
Edward Creef1174f72017-08-07 15:26:19 +01002840 return -EINVAL;
2841 }
2842 if (WARN_ON(!src_reg)) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002843 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002844 verbose(env, "verifier internal error: no src_reg\n");
Edward Creef1174f72017-08-07 15:26:19 +01002845 return -EINVAL;
2846 }
2847 return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002848}
2849
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002850/* check validity of 32-bit and 64-bit arithmetic operations */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002851static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002852{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002853 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002854 u8 opcode = BPF_OP(insn->code);
2855 int err;
2856
2857 if (opcode == BPF_END || opcode == BPF_NEG) {
2858 if (opcode == BPF_NEG) {
2859 if (BPF_SRC(insn->code) != 0 ||
2860 insn->src_reg != BPF_REG_0 ||
2861 insn->off != 0 || insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002862 verbose(env, "BPF_NEG uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002863 return -EINVAL;
2864 }
2865 } else {
2866 if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
Edward Creee67b8a62017-09-15 14:37:38 +01002867 (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) ||
2868 BPF_CLASS(insn->code) == BPF_ALU64) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002869 verbose(env, "BPF_END uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002870 return -EINVAL;
2871 }
2872 }
2873
2874 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01002875 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002876 if (err)
2877 return err;
2878
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002879 if (is_pointer_value(env, insn->dst_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002880 verbose(env, "R%d pointer arithmetic prohibited\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002881 insn->dst_reg);
2882 return -EACCES;
2883 }
2884
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002885 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01002886 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002887 if (err)
2888 return err;
2889
2890 } else if (opcode == BPF_MOV) {
2891
2892 if (BPF_SRC(insn->code) == BPF_X) {
2893 if (insn->imm != 0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002894 verbose(env, "BPF_MOV uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002895 return -EINVAL;
2896 }
2897
2898 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01002899 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002900 if (err)
2901 return err;
2902 } else {
2903 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002904 verbose(env, "BPF_MOV uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002905 return -EINVAL;
2906 }
2907 }
2908
2909 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01002910 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002911 if (err)
2912 return err;
2913
2914 if (BPF_SRC(insn->code) == BPF_X) {
2915 if (BPF_CLASS(insn->code) == BPF_ALU64) {
2916 /* case: R1 = R2
2917 * copy register state to dest reg
2918 */
2919 regs[insn->dst_reg] = regs[insn->src_reg];
Alexei Starovoitov8fe2d6c2017-10-05 16:20:56 -07002920 regs[insn->dst_reg].live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002921 } else {
Edward Creef1174f72017-08-07 15:26:19 +01002922 /* R1 = (u32) R2 */
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002923 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002924 verbose(env,
2925 "R%d partial copy of pointer\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002926 insn->src_reg);
2927 return -EACCES;
2928 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002929 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creeb03c9f92017-08-07 15:26:36 +01002930 /* high 32 bits are known zero. */
Edward Creef1174f72017-08-07 15:26:19 +01002931 regs[insn->dst_reg].var_off = tnum_cast(
2932 regs[insn->dst_reg].var_off, 4);
Edward Creeb03c9f92017-08-07 15:26:36 +01002933 __update_reg_bounds(&regs[insn->dst_reg]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002934 }
2935 } else {
2936 /* case: R = imm
2937 * remember the value we stored into this reg
2938 */
Edward Creef1174f72017-08-07 15:26:19 +01002939 regs[insn->dst_reg].type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01002940 __mark_reg_known(regs + insn->dst_reg, insn->imm);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002941 }
2942
2943 } else if (opcode > BPF_END) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002944 verbose(env, "invalid BPF_ALU opcode %x\n", opcode);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002945 return -EINVAL;
2946
2947 } else { /* all other ALU ops: and, sub, xor, add, ... */
2948
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002949 if (BPF_SRC(insn->code) == BPF_X) {
2950 if (insn->imm != 0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002951 verbose(env, "BPF_ALU uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002952 return -EINVAL;
2953 }
2954 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01002955 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002956 if (err)
2957 return err;
2958 } else {
2959 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002960 verbose(env, "BPF_ALU uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002961 return -EINVAL;
2962 }
2963 }
2964
2965 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01002966 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002967 if (err)
2968 return err;
2969
2970 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
2971 BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002972 verbose(env, "div by zero\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002973 return -EINVAL;
2974 }
2975
Rabin Vincent229394e82016-01-12 20:17:08 +01002976 if ((opcode == BPF_LSH || opcode == BPF_RSH ||
2977 opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
2978 int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
2979
2980 if (insn->imm < 0 || insn->imm >= size) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002981 verbose(env, "invalid shift %d\n", insn->imm);
Rabin Vincent229394e82016-01-12 20:17:08 +01002982 return -EINVAL;
2983 }
2984 }
2985
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002986 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01002987 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002988 if (err)
2989 return err;
2990
Edward Creef1174f72017-08-07 15:26:19 +01002991 return adjust_reg_min_max_vals(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002992 }
2993
2994 return 0;
2995}
2996
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002997static void find_good_pkt_pointers(struct bpf_verifier_state *vstate,
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002998 struct bpf_reg_state *dst_reg,
David S. Millerf8ddadc2017-10-22 13:36:53 +01002999 enum bpf_reg_type type,
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003000 bool range_right_open)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003001{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003002 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003003 struct bpf_reg_state *regs = state->regs, *reg;
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003004 u16 new_range;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003005 int i, j;
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003006
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003007 if (dst_reg->off < 0 ||
3008 (dst_reg->off == 0 && range_right_open))
Edward Creef1174f72017-08-07 15:26:19 +01003009 /* This doesn't give us any range */
3010 return;
3011
Edward Creeb03c9f92017-08-07 15:26:36 +01003012 if (dst_reg->umax_value > MAX_PACKET_OFF ||
3013 dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF)
Edward Creef1174f72017-08-07 15:26:19 +01003014 /* Risk of overflow. For instance, ptr + (1<<63) may be less
3015 * than pkt_end, but that's because it's also less than pkt.
3016 */
3017 return;
3018
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003019 new_range = dst_reg->off;
3020 if (range_right_open)
3021 new_range--;
3022
3023 /* Examples for register markings:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003024 *
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003025 * pkt_data in dst register:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003026 *
3027 * r2 = r3;
3028 * r2 += 8;
3029 * if (r2 > pkt_end) goto <handle exception>
3030 * <access okay>
3031 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003032 * r2 = r3;
3033 * r2 += 8;
3034 * if (r2 < pkt_end) goto <access okay>
3035 * <handle exception>
3036 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003037 * Where:
3038 * r2 == dst_reg, pkt_end == src_reg
3039 * r2=pkt(id=n,off=8,r=0)
3040 * r3=pkt(id=n,off=0,r=0)
3041 *
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003042 * pkt_data in src register:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003043 *
3044 * r2 = r3;
3045 * r2 += 8;
3046 * if (pkt_end >= r2) goto <access okay>
3047 * <handle exception>
3048 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003049 * r2 = r3;
3050 * r2 += 8;
3051 * if (pkt_end <= r2) goto <handle exception>
3052 * <access okay>
3053 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003054 * Where:
3055 * pkt_end == dst_reg, r2 == src_reg
3056 * r2=pkt(id=n,off=8,r=0)
3057 * r3=pkt(id=n,off=0,r=0)
3058 *
3059 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003060 * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8)
3061 * and [r3, r3 + 8-1) respectively is safe to access depending on
3062 * the check.
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003063 */
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003064
Edward Creef1174f72017-08-07 15:26:19 +01003065 /* If our ids match, then we must have the same max_value. And we
3066 * don't care about the other reg's fixed offset, since if it's too big
3067 * the range won't allow anything.
3068 * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16.
3069 */
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003070 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003071 if (regs[i].type == type && regs[i].id == dst_reg->id)
Alexei Starovoitovb1977682017-03-24 15:57:33 -07003072 /* keep the maximum range already checked */
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003073 regs[i].range = max(regs[i].range, new_range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003074
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003075 for (j = 0; j <= vstate->curframe; j++) {
3076 state = vstate->frame[j];
3077 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
3078 if (state->stack[i].slot_type[0] != STACK_SPILL)
3079 continue;
3080 reg = &state->stack[i].spilled_ptr;
3081 if (reg->type == type && reg->id == dst_reg->id)
3082 reg->range = max(reg->range, new_range);
3083 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003084 }
3085}
3086
Josef Bacik48461132016-09-28 10:54:32 -04003087/* Adjusts the register min/max values in the case that the dst_reg is the
3088 * variable register that we are working on, and src_reg is a constant or we're
3089 * simply doing a BPF_K check.
Edward Creef1174f72017-08-07 15:26:19 +01003090 * In JEQ/JNE cases we also adjust the var_off values.
Josef Bacik48461132016-09-28 10:54:32 -04003091 */
3092static void reg_set_min_max(struct bpf_reg_state *true_reg,
3093 struct bpf_reg_state *false_reg, u64 val,
3094 u8 opcode)
3095{
Edward Creef1174f72017-08-07 15:26:19 +01003096 /* If the dst_reg is a pointer, we can't learn anything about its
3097 * variable offset from the compare (unless src_reg were a pointer into
3098 * the same object, but we don't bother with that.
3099 * Since false_reg and true_reg have the same type by construction, we
3100 * only need to check one of them for pointerness.
3101 */
3102 if (__is_pointer_value(false, false_reg))
3103 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02003104
Josef Bacik48461132016-09-28 10:54:32 -04003105 switch (opcode) {
3106 case BPF_JEQ:
3107 /* If this is false then we know nothing Jon Snow, but if it is
3108 * true then we know for sure.
3109 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003110 __mark_reg_known(true_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04003111 break;
3112 case BPF_JNE:
3113 /* If this is true we know nothing Jon Snow, but if it is false
3114 * we know the value for sure;
3115 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003116 __mark_reg_known(false_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04003117 break;
3118 case BPF_JGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01003119 false_reg->umax_value = min(false_reg->umax_value, val);
3120 true_reg->umin_value = max(true_reg->umin_value, val + 1);
3121 break;
Josef Bacik48461132016-09-28 10:54:32 -04003122 case BPF_JSGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01003123 false_reg->smax_value = min_t(s64, false_reg->smax_value, val);
3124 true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1);
Josef Bacik48461132016-09-28 10:54:32 -04003125 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003126 case BPF_JLT:
3127 false_reg->umin_value = max(false_reg->umin_value, val);
3128 true_reg->umax_value = min(true_reg->umax_value, val - 1);
3129 break;
3130 case BPF_JSLT:
3131 false_reg->smin_value = max_t(s64, false_reg->smin_value, val);
3132 true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1);
3133 break;
Josef Bacik48461132016-09-28 10:54:32 -04003134 case BPF_JGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01003135 false_reg->umax_value = min(false_reg->umax_value, val - 1);
3136 true_reg->umin_value = max(true_reg->umin_value, val);
3137 break;
Josef Bacik48461132016-09-28 10:54:32 -04003138 case BPF_JSGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01003139 false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1);
3140 true_reg->smin_value = max_t(s64, true_reg->smin_value, val);
Josef Bacik48461132016-09-28 10:54:32 -04003141 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003142 case BPF_JLE:
3143 false_reg->umin_value = max(false_reg->umin_value, val + 1);
3144 true_reg->umax_value = min(true_reg->umax_value, val);
3145 break;
3146 case BPF_JSLE:
3147 false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1);
3148 true_reg->smax_value = min_t(s64, true_reg->smax_value, val);
3149 break;
Josef Bacik48461132016-09-28 10:54:32 -04003150 default:
3151 break;
3152 }
3153
Edward Creeb03c9f92017-08-07 15:26:36 +01003154 __reg_deduce_bounds(false_reg);
3155 __reg_deduce_bounds(true_reg);
3156 /* We might have learned some bits from the bounds. */
3157 __reg_bound_offset(false_reg);
3158 __reg_bound_offset(true_reg);
3159 /* Intersecting with the old var_off might have improved our bounds
3160 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
3161 * then new var_off is (0; 0x7f...fc) which improves our umax.
3162 */
3163 __update_reg_bounds(false_reg);
3164 __update_reg_bounds(true_reg);
Josef Bacik48461132016-09-28 10:54:32 -04003165}
3166
Edward Creef1174f72017-08-07 15:26:19 +01003167/* Same as above, but for the case that dst_reg holds a constant and src_reg is
3168 * the variable reg.
Josef Bacik48461132016-09-28 10:54:32 -04003169 */
3170static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
3171 struct bpf_reg_state *false_reg, u64 val,
3172 u8 opcode)
3173{
Edward Creef1174f72017-08-07 15:26:19 +01003174 if (__is_pointer_value(false, false_reg))
3175 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02003176
Josef Bacik48461132016-09-28 10:54:32 -04003177 switch (opcode) {
3178 case BPF_JEQ:
3179 /* If this is false then we know nothing Jon Snow, but if it is
3180 * true then we know for sure.
3181 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003182 __mark_reg_known(true_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04003183 break;
3184 case BPF_JNE:
3185 /* If this is true we know nothing Jon Snow, but if it is false
3186 * we know the value for sure;
3187 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003188 __mark_reg_known(false_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04003189 break;
3190 case BPF_JGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01003191 true_reg->umax_value = min(true_reg->umax_value, val - 1);
3192 false_reg->umin_value = max(false_reg->umin_value, val);
3193 break;
Josef Bacik48461132016-09-28 10:54:32 -04003194 case BPF_JSGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01003195 true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1);
3196 false_reg->smin_value = max_t(s64, false_reg->smin_value, val);
Josef Bacik48461132016-09-28 10:54:32 -04003197 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003198 case BPF_JLT:
3199 true_reg->umin_value = max(true_reg->umin_value, val + 1);
3200 false_reg->umax_value = min(false_reg->umax_value, val);
3201 break;
3202 case BPF_JSLT:
3203 true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1);
3204 false_reg->smax_value = min_t(s64, false_reg->smax_value, val);
3205 break;
Josef Bacik48461132016-09-28 10:54:32 -04003206 case BPF_JGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01003207 true_reg->umax_value = min(true_reg->umax_value, val);
3208 false_reg->umin_value = max(false_reg->umin_value, val + 1);
3209 break;
Josef Bacik48461132016-09-28 10:54:32 -04003210 case BPF_JSGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01003211 true_reg->smax_value = min_t(s64, true_reg->smax_value, val);
3212 false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1);
Josef Bacik48461132016-09-28 10:54:32 -04003213 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003214 case BPF_JLE:
3215 true_reg->umin_value = max(true_reg->umin_value, val);
3216 false_reg->umax_value = min(false_reg->umax_value, val - 1);
3217 break;
3218 case BPF_JSLE:
3219 true_reg->smin_value = max_t(s64, true_reg->smin_value, val);
3220 false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1);
3221 break;
Josef Bacik48461132016-09-28 10:54:32 -04003222 default:
3223 break;
3224 }
3225
Edward Creeb03c9f92017-08-07 15:26:36 +01003226 __reg_deduce_bounds(false_reg);
3227 __reg_deduce_bounds(true_reg);
3228 /* We might have learned some bits from the bounds. */
3229 __reg_bound_offset(false_reg);
3230 __reg_bound_offset(true_reg);
3231 /* Intersecting with the old var_off might have improved our bounds
3232 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
3233 * then new var_off is (0; 0x7f...fc) which improves our umax.
3234 */
3235 __update_reg_bounds(false_reg);
3236 __update_reg_bounds(true_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003237}
3238
3239/* Regs are known to be equal, so intersect their min/max/var_off */
3240static void __reg_combine_min_max(struct bpf_reg_state *src_reg,
3241 struct bpf_reg_state *dst_reg)
3242{
Edward Creeb03c9f92017-08-07 15:26:36 +01003243 src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value,
3244 dst_reg->umin_value);
3245 src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value,
3246 dst_reg->umax_value);
3247 src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value,
3248 dst_reg->smin_value);
3249 src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value,
3250 dst_reg->smax_value);
Edward Creef1174f72017-08-07 15:26:19 +01003251 src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off,
3252 dst_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01003253 /* We might have learned new bounds from the var_off. */
3254 __update_reg_bounds(src_reg);
3255 __update_reg_bounds(dst_reg);
3256 /* We might have learned something about the sign bit. */
3257 __reg_deduce_bounds(src_reg);
3258 __reg_deduce_bounds(dst_reg);
3259 /* We might have learned some bits from the bounds. */
3260 __reg_bound_offset(src_reg);
3261 __reg_bound_offset(dst_reg);
3262 /* Intersecting with the old var_off might have improved our bounds
3263 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
3264 * then new var_off is (0; 0x7f...fc) which improves our umax.
3265 */
3266 __update_reg_bounds(src_reg);
3267 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003268}
3269
3270static void reg_combine_min_max(struct bpf_reg_state *true_src,
3271 struct bpf_reg_state *true_dst,
3272 struct bpf_reg_state *false_src,
3273 struct bpf_reg_state *false_dst,
3274 u8 opcode)
3275{
3276 switch (opcode) {
3277 case BPF_JEQ:
3278 __reg_combine_min_max(true_src, true_dst);
3279 break;
3280 case BPF_JNE:
3281 __reg_combine_min_max(false_src, false_dst);
Edward Creeb03c9f92017-08-07 15:26:36 +01003282 break;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02003283 }
Josef Bacik48461132016-09-28 10:54:32 -04003284}
3285
Thomas Graf57a09bf2016-10-18 19:51:19 +02003286static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id,
Edward Creef1174f72017-08-07 15:26:19 +01003287 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02003288{
3289 struct bpf_reg_state *reg = &regs[regno];
3290
3291 if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) {
Edward Creef1174f72017-08-07 15:26:19 +01003292 /* Old offset (both fixed and variable parts) should
3293 * have been known-zero, because we don't allow pointer
3294 * arithmetic on pointers that might be NULL.
3295 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003296 if (WARN_ON_ONCE(reg->smin_value || reg->smax_value ||
3297 !tnum_equals_const(reg->var_off, 0) ||
Edward Creef1174f72017-08-07 15:26:19 +01003298 reg->off)) {
Edward Creeb03c9f92017-08-07 15:26:36 +01003299 __mark_reg_known_zero(reg);
3300 reg->off = 0;
Edward Creef1174f72017-08-07 15:26:19 +01003301 }
3302 if (is_null) {
3303 reg->type = SCALAR_VALUE;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07003304 } else if (reg->map_ptr->inner_map_meta) {
3305 reg->type = CONST_PTR_TO_MAP;
3306 reg->map_ptr = reg->map_ptr->inner_map_meta;
3307 } else {
Edward Creef1174f72017-08-07 15:26:19 +01003308 reg->type = PTR_TO_MAP_VALUE;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07003309 }
Daniel Borkmanna08dd0d2016-12-15 01:30:06 +01003310 /* We don't need id from this point onwards anymore, thus we
3311 * should better reset it, so that state pruning has chances
3312 * to take effect.
3313 */
3314 reg->id = 0;
Thomas Graf57a09bf2016-10-18 19:51:19 +02003315 }
3316}
3317
3318/* The logic is similar to find_good_pkt_pointers(), both could eventually
3319 * be folded together at some point.
3320 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003321static void mark_map_regs(struct bpf_verifier_state *vstate, u32 regno,
Edward Creef1174f72017-08-07 15:26:19 +01003322 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02003323{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003324 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Thomas Graf57a09bf2016-10-18 19:51:19 +02003325 struct bpf_reg_state *regs = state->regs;
Daniel Borkmanna08dd0d2016-12-15 01:30:06 +01003326 u32 id = regs[regno].id;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003327 int i, j;
Thomas Graf57a09bf2016-10-18 19:51:19 +02003328
3329 for (i = 0; i < MAX_BPF_REG; i++)
Edward Creef1174f72017-08-07 15:26:19 +01003330 mark_map_reg(regs, i, id, is_null);
Thomas Graf57a09bf2016-10-18 19:51:19 +02003331
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003332 for (j = 0; j <= vstate->curframe; j++) {
3333 state = vstate->frame[j];
3334 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
3335 if (state->stack[i].slot_type[0] != STACK_SPILL)
3336 continue;
3337 mark_map_reg(&state->stack[i].spilled_ptr, 0, id, is_null);
3338 }
Thomas Graf57a09bf2016-10-18 19:51:19 +02003339 }
3340}
3341
Daniel Borkmann5beca082017-11-01 23:58:10 +01003342static bool try_match_pkt_pointers(const struct bpf_insn *insn,
3343 struct bpf_reg_state *dst_reg,
3344 struct bpf_reg_state *src_reg,
3345 struct bpf_verifier_state *this_branch,
3346 struct bpf_verifier_state *other_branch)
3347{
3348 if (BPF_SRC(insn->code) != BPF_X)
3349 return false;
3350
3351 switch (BPF_OP(insn->code)) {
3352 case BPF_JGT:
3353 if ((dst_reg->type == PTR_TO_PACKET &&
3354 src_reg->type == PTR_TO_PACKET_END) ||
3355 (dst_reg->type == PTR_TO_PACKET_META &&
3356 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
3357 /* pkt_data' > pkt_end, pkt_meta' > pkt_data */
3358 find_good_pkt_pointers(this_branch, dst_reg,
3359 dst_reg->type, false);
3360 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
3361 src_reg->type == PTR_TO_PACKET) ||
3362 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
3363 src_reg->type == PTR_TO_PACKET_META)) {
3364 /* pkt_end > pkt_data', pkt_data > pkt_meta' */
3365 find_good_pkt_pointers(other_branch, src_reg,
3366 src_reg->type, true);
3367 } else {
3368 return false;
3369 }
3370 break;
3371 case BPF_JLT:
3372 if ((dst_reg->type == PTR_TO_PACKET &&
3373 src_reg->type == PTR_TO_PACKET_END) ||
3374 (dst_reg->type == PTR_TO_PACKET_META &&
3375 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
3376 /* pkt_data' < pkt_end, pkt_meta' < pkt_data */
3377 find_good_pkt_pointers(other_branch, dst_reg,
3378 dst_reg->type, true);
3379 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
3380 src_reg->type == PTR_TO_PACKET) ||
3381 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
3382 src_reg->type == PTR_TO_PACKET_META)) {
3383 /* pkt_end < pkt_data', pkt_data > pkt_meta' */
3384 find_good_pkt_pointers(this_branch, src_reg,
3385 src_reg->type, false);
3386 } else {
3387 return false;
3388 }
3389 break;
3390 case BPF_JGE:
3391 if ((dst_reg->type == PTR_TO_PACKET &&
3392 src_reg->type == PTR_TO_PACKET_END) ||
3393 (dst_reg->type == PTR_TO_PACKET_META &&
3394 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
3395 /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */
3396 find_good_pkt_pointers(this_branch, dst_reg,
3397 dst_reg->type, true);
3398 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
3399 src_reg->type == PTR_TO_PACKET) ||
3400 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
3401 src_reg->type == PTR_TO_PACKET_META)) {
3402 /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */
3403 find_good_pkt_pointers(other_branch, src_reg,
3404 src_reg->type, false);
3405 } else {
3406 return false;
3407 }
3408 break;
3409 case BPF_JLE:
3410 if ((dst_reg->type == PTR_TO_PACKET &&
3411 src_reg->type == PTR_TO_PACKET_END) ||
3412 (dst_reg->type == PTR_TO_PACKET_META &&
3413 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
3414 /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */
3415 find_good_pkt_pointers(other_branch, dst_reg,
3416 dst_reg->type, false);
3417 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
3418 src_reg->type == PTR_TO_PACKET) ||
3419 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
3420 src_reg->type == PTR_TO_PACKET_META)) {
3421 /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */
3422 find_good_pkt_pointers(this_branch, src_reg,
3423 src_reg->type, true);
3424 } else {
3425 return false;
3426 }
3427 break;
3428 default:
3429 return false;
3430 }
3431
3432 return true;
3433}
3434
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003435static int check_cond_jmp_op(struct bpf_verifier_env *env,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003436 struct bpf_insn *insn, int *insn_idx)
3437{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003438 struct bpf_verifier_state *this_branch = env->cur_state;
3439 struct bpf_verifier_state *other_branch;
3440 struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs;
3441 struct bpf_reg_state *dst_reg, *other_branch_regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003442 u8 opcode = BPF_OP(insn->code);
3443 int err;
3444
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003445 if (opcode > BPF_JSLE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003446 verbose(env, "invalid BPF_JMP opcode %x\n", opcode);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003447 return -EINVAL;
3448 }
3449
3450 if (BPF_SRC(insn->code) == BPF_X) {
3451 if (insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003452 verbose(env, "BPF_JMP uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003453 return -EINVAL;
3454 }
3455
3456 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01003457 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003458 if (err)
3459 return err;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003460
3461 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003462 verbose(env, "R%d pointer comparison prohibited\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003463 insn->src_reg);
3464 return -EACCES;
3465 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003466 } else {
3467 if (insn->src_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003468 verbose(env, "BPF_JMP uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003469 return -EINVAL;
3470 }
3471 }
3472
3473 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01003474 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003475 if (err)
3476 return err;
3477
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003478 dst_reg = &regs[insn->dst_reg];
3479
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003480 /* detect if R == 0 where R was initialized to zero earlier */
3481 if (BPF_SRC(insn->code) == BPF_K &&
3482 (opcode == BPF_JEQ || opcode == BPF_JNE) &&
Edward Creef1174f72017-08-07 15:26:19 +01003483 dst_reg->type == SCALAR_VALUE &&
Alexei Starovoitov3bf15922017-11-30 21:31:39 -08003484 tnum_is_const(dst_reg->var_off)) {
3485 if ((opcode == BPF_JEQ && dst_reg->var_off.value == insn->imm) ||
3486 (opcode == BPF_JNE && dst_reg->var_off.value != insn->imm)) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003487 /* if (imm == imm) goto pc+off;
3488 * only follow the goto, ignore fall-through
3489 */
3490 *insn_idx += insn->off;
3491 return 0;
3492 } else {
3493 /* if (imm != imm) goto pc+off;
3494 * only follow fall-through branch, since
3495 * that's where the program will go
3496 */
3497 return 0;
3498 }
3499 }
3500
3501 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx);
3502 if (!other_branch)
3503 return -EFAULT;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003504 other_branch_regs = other_branch->frame[other_branch->curframe]->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003505
Josef Bacik48461132016-09-28 10:54:32 -04003506 /* detect if we are comparing against a constant value so we can adjust
3507 * our min/max values for our dst register.
Edward Creef1174f72017-08-07 15:26:19 +01003508 * this is only legit if both are scalars (or pointers to the same
3509 * object, I suppose, but we don't support that right now), because
3510 * otherwise the different base pointers mean the offsets aren't
3511 * comparable.
Josef Bacik48461132016-09-28 10:54:32 -04003512 */
3513 if (BPF_SRC(insn->code) == BPF_X) {
Edward Creef1174f72017-08-07 15:26:19 +01003514 if (dst_reg->type == SCALAR_VALUE &&
3515 regs[insn->src_reg].type == SCALAR_VALUE) {
3516 if (tnum_is_const(regs[insn->src_reg].var_off))
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003517 reg_set_min_max(&other_branch_regs[insn->dst_reg],
Edward Creef1174f72017-08-07 15:26:19 +01003518 dst_reg, regs[insn->src_reg].var_off.value,
3519 opcode);
3520 else if (tnum_is_const(dst_reg->var_off))
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003521 reg_set_min_max_inv(&other_branch_regs[insn->src_reg],
Edward Creef1174f72017-08-07 15:26:19 +01003522 &regs[insn->src_reg],
3523 dst_reg->var_off.value, opcode);
3524 else if (opcode == BPF_JEQ || opcode == BPF_JNE)
3525 /* Comparing for equality, we can combine knowledge */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003526 reg_combine_min_max(&other_branch_regs[insn->src_reg],
3527 &other_branch_regs[insn->dst_reg],
Edward Creef1174f72017-08-07 15:26:19 +01003528 &regs[insn->src_reg],
3529 &regs[insn->dst_reg], opcode);
3530 }
3531 } else if (dst_reg->type == SCALAR_VALUE) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003532 reg_set_min_max(&other_branch_regs[insn->dst_reg],
Josef Bacik48461132016-09-28 10:54:32 -04003533 dst_reg, insn->imm, opcode);
3534 }
3535
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003536 /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003537 if (BPF_SRC(insn->code) == BPF_K &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003538 insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
3539 dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
Thomas Graf57a09bf2016-10-18 19:51:19 +02003540 /* Mark all identical map registers in each branch as either
3541 * safe or unknown depending R == 0 or R != 0 conditional.
3542 */
Edward Creef1174f72017-08-07 15:26:19 +01003543 mark_map_regs(this_branch, insn->dst_reg, opcode == BPF_JNE);
3544 mark_map_regs(other_branch, insn->dst_reg, opcode == BPF_JEQ);
Daniel Borkmann5beca082017-11-01 23:58:10 +01003545 } else if (!try_match_pkt_pointers(insn, dst_reg, &regs[insn->src_reg],
3546 this_branch, other_branch) &&
3547 is_pointer_value(env, insn->dst_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003548 verbose(env, "R%d pointer comparison prohibited\n",
3549 insn->dst_reg);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003550 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003551 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003552 if (env->log.level)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003553 print_verifier_state(env, this_branch->frame[this_branch->curframe]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003554 return 0;
3555}
3556
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003557/* return the map pointer stored inside BPF_LD_IMM64 instruction */
3558static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
3559{
3560 u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
3561
3562 return (struct bpf_map *) (unsigned long) imm64;
3563}
3564
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003565/* verify BPF_LD_IMM64 instruction */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003566static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003567{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003568 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003569 int err;
3570
3571 if (BPF_SIZE(insn->code) != BPF_DW) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003572 verbose(env, "invalid BPF_LD_IMM insn\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003573 return -EINVAL;
3574 }
3575 if (insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003576 verbose(env, "BPF_LD_IMM64 uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003577 return -EINVAL;
3578 }
3579
Edward Creedc503a82017-08-15 20:34:35 +01003580 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003581 if (err)
3582 return err;
3583
Jakub Kicinski6b173872016-09-21 11:43:59 +01003584 if (insn->src_reg == 0) {
Jakub Kicinski6b173872016-09-21 11:43:59 +01003585 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
3586
Edward Creef1174f72017-08-07 15:26:19 +01003587 regs[insn->dst_reg].type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01003588 __mark_reg_known(&regs[insn->dst_reg], imm);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003589 return 0;
Jakub Kicinski6b173872016-09-21 11:43:59 +01003590 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003591
3592 /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
3593 BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
3594
3595 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
3596 regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
3597 return 0;
3598}
3599
Daniel Borkmann96be4322015-03-01 12:31:46 +01003600static bool may_access_skb(enum bpf_prog_type type)
3601{
3602 switch (type) {
3603 case BPF_PROG_TYPE_SOCKET_FILTER:
3604 case BPF_PROG_TYPE_SCHED_CLS:
Daniel Borkmann94caee8c2015-03-20 15:11:11 +01003605 case BPF_PROG_TYPE_SCHED_ACT:
Daniel Borkmann96be4322015-03-01 12:31:46 +01003606 return true;
3607 default:
3608 return false;
3609 }
3610}
3611
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003612/* verify safety of LD_ABS|LD_IND instructions:
3613 * - they can only appear in the programs where ctx == skb
3614 * - since they are wrappers of function calls, they scratch R1-R5 registers,
3615 * preserve R6-R9, and store return value into R0
3616 *
3617 * Implicit input:
3618 * ctx == skb == R6 == CTX
3619 *
3620 * Explicit input:
3621 * SRC == any register
3622 * IMM == 32-bit immediate
3623 *
3624 * Output:
3625 * R0 - 8/16/32-bit skb data converted to cpu endianness
3626 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003627static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003628{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003629 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003630 u8 mode = BPF_MODE(insn->code);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003631 int i, err;
3632
Daniel Borkmann24701ec2015-03-01 12:31:47 +01003633 if (!may_access_skb(env->prog->type)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003634 verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003635 return -EINVAL;
3636 }
3637
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003638 if (env->subprog_cnt) {
3639 /* when program has LD_ABS insn JITs and interpreter assume
3640 * that r1 == ctx == skb which is not the case for callees
3641 * that can have arbitrary arguments. It's problematic
3642 * for main prog as well since JITs would need to analyze
3643 * all functions in order to make proper register save/restore
3644 * decisions in the main prog. Hence disallow LD_ABS with calls
3645 */
3646 verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n");
3647 return -EINVAL;
3648 }
3649
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003650 if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
Alexei Starovoitovd82bccc2016-04-12 10:26:19 -07003651 BPF_SIZE(insn->code) == BPF_DW ||
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003652 (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003653 verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003654 return -EINVAL;
3655 }
3656
3657 /* check whether implicit source operand (register R6) is readable */
Edward Creedc503a82017-08-15 20:34:35 +01003658 err = check_reg_arg(env, BPF_REG_6, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003659 if (err)
3660 return err;
3661
3662 if (regs[BPF_REG_6].type != PTR_TO_CTX) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003663 verbose(env,
3664 "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003665 return -EINVAL;
3666 }
3667
3668 if (mode == BPF_IND) {
3669 /* check explicit source operand */
Edward Creedc503a82017-08-15 20:34:35 +01003670 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003671 if (err)
3672 return err;
3673 }
3674
3675 /* reset caller saved regs to unreadable */
Edward Creedc503a82017-08-15 20:34:35 +01003676 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003677 mark_reg_not_init(env, regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01003678 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
3679 }
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003680
3681 /* mark destination R0 register as readable, since it contains
Edward Creedc503a82017-08-15 20:34:35 +01003682 * the value fetched from the packet.
3683 * Already marked as written above.
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003684 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003685 mark_reg_unknown(env, regs, BPF_REG_0);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003686 return 0;
3687}
3688
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003689static int check_return_code(struct bpf_verifier_env *env)
3690{
3691 struct bpf_reg_state *reg;
3692 struct tnum range = tnum_range(0, 1);
3693
3694 switch (env->prog->type) {
3695 case BPF_PROG_TYPE_CGROUP_SKB:
3696 case BPF_PROG_TYPE_CGROUP_SOCK:
3697 case BPF_PROG_TYPE_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05003698 case BPF_PROG_TYPE_CGROUP_DEVICE:
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003699 break;
3700 default:
3701 return 0;
3702 }
3703
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003704 reg = cur_regs(env) + BPF_REG_0;
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003705 if (reg->type != SCALAR_VALUE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003706 verbose(env, "At program exit the register R0 is not a known value (%s)\n",
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003707 reg_type_str[reg->type]);
3708 return -EINVAL;
3709 }
3710
3711 if (!tnum_in(range, reg->var_off)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003712 verbose(env, "At program exit the register R0 ");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003713 if (!tnum_is_unknown(reg->var_off)) {
3714 char tn_buf[48];
3715
3716 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003717 verbose(env, "has value %s", tn_buf);
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003718 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003719 verbose(env, "has unknown scalar value");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003720 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003721 verbose(env, " should have been 0 or 1\n");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003722 return -EINVAL;
3723 }
3724 return 0;
3725}
3726
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003727/* non-recursive DFS pseudo code
3728 * 1 procedure DFS-iterative(G,v):
3729 * 2 label v as discovered
3730 * 3 let S be a stack
3731 * 4 S.push(v)
3732 * 5 while S is not empty
3733 * 6 t <- S.pop()
3734 * 7 if t is what we're looking for:
3735 * 8 return t
3736 * 9 for all edges e in G.adjacentEdges(t) do
3737 * 10 if edge e is already labelled
3738 * 11 continue with the next edge
3739 * 12 w <- G.adjacentVertex(t,e)
3740 * 13 if vertex w is not discovered and not explored
3741 * 14 label e as tree-edge
3742 * 15 label w as discovered
3743 * 16 S.push(w)
3744 * 17 continue at 5
3745 * 18 else if vertex w is discovered
3746 * 19 label e as back-edge
3747 * 20 else
3748 * 21 // vertex w is explored
3749 * 22 label e as forward- or cross-edge
3750 * 23 label t as explored
3751 * 24 S.pop()
3752 *
3753 * convention:
3754 * 0x10 - discovered
3755 * 0x11 - discovered and fall-through edge labelled
3756 * 0x12 - discovered and fall-through and branch edges labelled
3757 * 0x20 - explored
3758 */
3759
3760enum {
3761 DISCOVERED = 0x10,
3762 EXPLORED = 0x20,
3763 FALLTHROUGH = 1,
3764 BRANCH = 2,
3765};
3766
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003767#define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003768
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003769static int *insn_stack; /* stack of insns to process */
3770static int cur_stack; /* current stack index */
3771static int *insn_state;
3772
3773/* t, w, e - match pseudo-code above:
3774 * t - index of current instruction
3775 * w - next instruction
3776 * e - edge
3777 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003778static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003779{
3780 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
3781 return 0;
3782
3783 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
3784 return 0;
3785
3786 if (w < 0 || w >= env->prog->len) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003787 verbose(env, "jump out of range from insn %d to %d\n", t, w);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003788 return -EINVAL;
3789 }
3790
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003791 if (e == BRANCH)
3792 /* mark branch target for state pruning */
3793 env->explored_states[w] = STATE_LIST_MARK;
3794
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003795 if (insn_state[w] == 0) {
3796 /* tree-edge */
3797 insn_state[t] = DISCOVERED | e;
3798 insn_state[w] = DISCOVERED;
3799 if (cur_stack >= env->prog->len)
3800 return -E2BIG;
3801 insn_stack[cur_stack++] = w;
3802 return 1;
3803 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003804 verbose(env, "back-edge from insn %d to %d\n", t, w);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003805 return -EINVAL;
3806 } else if (insn_state[w] == EXPLORED) {
3807 /* forward- or cross-edge */
3808 insn_state[t] = DISCOVERED | e;
3809 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003810 verbose(env, "insn state internal bug\n");
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003811 return -EFAULT;
3812 }
3813 return 0;
3814}
3815
3816/* non-recursive depth-first-search to detect loops in BPF program
3817 * loop == back-edge in directed graph
3818 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003819static int check_cfg(struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003820{
3821 struct bpf_insn *insns = env->prog->insnsi;
3822 int insn_cnt = env->prog->len;
3823 int ret = 0;
3824 int i, t;
3825
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08003826 ret = check_subprogs(env);
3827 if (ret < 0)
3828 return ret;
3829
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003830 insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
3831 if (!insn_state)
3832 return -ENOMEM;
3833
3834 insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
3835 if (!insn_stack) {
3836 kfree(insn_state);
3837 return -ENOMEM;
3838 }
3839
3840 insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
3841 insn_stack[0] = 0; /* 0 is the first instruction */
3842 cur_stack = 1;
3843
3844peek_stack:
3845 if (cur_stack == 0)
3846 goto check_state;
3847 t = insn_stack[cur_stack - 1];
3848
3849 if (BPF_CLASS(insns[t].code) == BPF_JMP) {
3850 u8 opcode = BPF_OP(insns[t].code);
3851
3852 if (opcode == BPF_EXIT) {
3853 goto mark_explored;
3854 } else if (opcode == BPF_CALL) {
3855 ret = push_insn(t, t + 1, FALLTHROUGH, env);
3856 if (ret == 1)
3857 goto peek_stack;
3858 else if (ret < 0)
3859 goto err_free;
Daniel Borkmann07016152016-04-05 22:33:17 +02003860 if (t + 1 < insn_cnt)
3861 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08003862 if (insns[t].src_reg == BPF_PSEUDO_CALL) {
3863 env->explored_states[t] = STATE_LIST_MARK;
3864 ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env);
3865 if (ret == 1)
3866 goto peek_stack;
3867 else if (ret < 0)
3868 goto err_free;
3869 }
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003870 } else if (opcode == BPF_JA) {
3871 if (BPF_SRC(insns[t].code) != BPF_K) {
3872 ret = -EINVAL;
3873 goto err_free;
3874 }
3875 /* unconditional jump with single edge */
3876 ret = push_insn(t, t + insns[t].off + 1,
3877 FALLTHROUGH, env);
3878 if (ret == 1)
3879 goto peek_stack;
3880 else if (ret < 0)
3881 goto err_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07003882 /* tell verifier to check for equivalent states
3883 * after every call and jump
3884 */
Alexei Starovoitovc3de6312015-04-14 15:57:13 -07003885 if (t + 1 < insn_cnt)
3886 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003887 } else {
3888 /* conditional jump with two edges */
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02003889 env->explored_states[t] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003890 ret = push_insn(t, t + 1, FALLTHROUGH, env);
3891 if (ret == 1)
3892 goto peek_stack;
3893 else if (ret < 0)
3894 goto err_free;
3895
3896 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
3897 if (ret == 1)
3898 goto peek_stack;
3899 else if (ret < 0)
3900 goto err_free;
3901 }
3902 } else {
3903 /* all other non-branch instructions with single
3904 * fall-through edge
3905 */
3906 ret = push_insn(t, t + 1, FALLTHROUGH, env);
3907 if (ret == 1)
3908 goto peek_stack;
3909 else if (ret < 0)
3910 goto err_free;
3911 }
3912
3913mark_explored:
3914 insn_state[t] = EXPLORED;
3915 if (cur_stack-- <= 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003916 verbose(env, "pop stack internal bug\n");
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003917 ret = -EFAULT;
3918 goto err_free;
3919 }
3920 goto peek_stack;
3921
3922check_state:
3923 for (i = 0; i < insn_cnt; i++) {
3924 if (insn_state[i] != EXPLORED) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003925 verbose(env, "unreachable insn %d\n", i);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003926 ret = -EINVAL;
3927 goto err_free;
3928 }
3929 }
3930 ret = 0; /* cfg looks good */
3931
3932err_free:
3933 kfree(insn_state);
3934 kfree(insn_stack);
3935 return ret;
3936}
3937
Edward Creef1174f72017-08-07 15:26:19 +01003938/* check %cur's range satisfies %old's */
3939static bool range_within(struct bpf_reg_state *old,
3940 struct bpf_reg_state *cur)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003941{
Edward Creeb03c9f92017-08-07 15:26:36 +01003942 return old->umin_value <= cur->umin_value &&
3943 old->umax_value >= cur->umax_value &&
3944 old->smin_value <= cur->smin_value &&
3945 old->smax_value >= cur->smax_value;
Edward Creef1174f72017-08-07 15:26:19 +01003946}
3947
3948/* Maximum number of register states that can exist at once */
3949#define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE)
3950struct idpair {
3951 u32 old;
3952 u32 cur;
3953};
3954
3955/* If in the old state two registers had the same id, then they need to have
3956 * the same id in the new state as well. But that id could be different from
3957 * the old state, so we need to track the mapping from old to new ids.
3958 * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent
3959 * regs with old id 5 must also have new id 9 for the new state to be safe. But
3960 * regs with a different old id could still have new id 9, we don't care about
3961 * that.
3962 * So we look through our idmap to see if this old id has been seen before. If
3963 * so, we require the new id to match; otherwise, we add the id pair to the map.
3964 */
3965static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap)
3966{
3967 unsigned int i;
3968
3969 for (i = 0; i < ID_MAP_SIZE; i++) {
3970 if (!idmap[i].old) {
3971 /* Reached an empty slot; haven't seen this id before */
3972 idmap[i].old = old_id;
3973 idmap[i].cur = cur_id;
3974 return true;
3975 }
3976 if (idmap[i].old == old_id)
3977 return idmap[i].cur == cur_id;
3978 }
3979 /* We ran out of idmap slots, which should be impossible */
3980 WARN_ON_ONCE(1);
3981 return false;
3982}
3983
3984/* Returns true if (rold safe implies rcur safe) */
Edward Cree1b688a12017-08-23 15:10:50 +01003985static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
3986 struct idpair *idmap)
Edward Creef1174f72017-08-07 15:26:19 +01003987{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003988 bool equal;
3989
Edward Creedc503a82017-08-15 20:34:35 +01003990 if (!(rold->live & REG_LIVE_READ))
3991 /* explored state didn't use this */
3992 return true;
3993
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003994 equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, frameno)) == 0;
3995
3996 if (rold->type == PTR_TO_STACK)
3997 /* two stack pointers are equal only if they're pointing to
3998 * the same stack frame, since fp-8 in foo != fp-8 in bar
3999 */
4000 return equal && rold->frameno == rcur->frameno;
4001
4002 if (equal)
Edward Creef1174f72017-08-07 15:26:19 +01004003 return true;
4004
4005 if (rold->type == NOT_INIT)
4006 /* explored state can't have used this */
4007 return true;
4008 if (rcur->type == NOT_INIT)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004009 return false;
Edward Creef1174f72017-08-07 15:26:19 +01004010 switch (rold->type) {
4011 case SCALAR_VALUE:
4012 if (rcur->type == SCALAR_VALUE) {
4013 /* new val must satisfy old val knowledge */
4014 return range_within(rold, rcur) &&
4015 tnum_in(rold->var_off, rcur->var_off);
4016 } else {
4017 /* if we knew anything about the old value, we're not
4018 * equal, because we can't know anything about the
4019 * scalar value of the pointer in the new value.
4020 */
Edward Creeb03c9f92017-08-07 15:26:36 +01004021 return rold->umin_value == 0 &&
4022 rold->umax_value == U64_MAX &&
4023 rold->smin_value == S64_MIN &&
4024 rold->smax_value == S64_MAX &&
Edward Creef1174f72017-08-07 15:26:19 +01004025 tnum_is_unknown(rold->var_off);
4026 }
4027 case PTR_TO_MAP_VALUE:
Edward Cree1b688a12017-08-23 15:10:50 +01004028 /* If the new min/max/var_off satisfy the old ones and
4029 * everything else matches, we are OK.
4030 * We don't care about the 'id' value, because nothing
4031 * uses it for PTR_TO_MAP_VALUE (only for ..._OR_NULL)
4032 */
4033 return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 &&
4034 range_within(rold, rcur) &&
4035 tnum_in(rold->var_off, rcur->var_off);
Edward Creef1174f72017-08-07 15:26:19 +01004036 case PTR_TO_MAP_VALUE_OR_NULL:
4037 /* a PTR_TO_MAP_VALUE could be safe to use as a
4038 * PTR_TO_MAP_VALUE_OR_NULL into the same map.
4039 * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL-
4040 * checked, doing so could have affected others with the same
4041 * id, and we can't check for that because we lost the id when
4042 * we converted to a PTR_TO_MAP_VALUE.
4043 */
4044 if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL)
4045 return false;
4046 if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)))
4047 return false;
4048 /* Check our ids match any regs they're supposed to */
4049 return check_ids(rold->id, rcur->id, idmap);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004050 case PTR_TO_PACKET_META:
Edward Creef1174f72017-08-07 15:26:19 +01004051 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004052 if (rcur->type != rold->type)
Edward Creef1174f72017-08-07 15:26:19 +01004053 return false;
4054 /* We must have at least as much range as the old ptr
4055 * did, so that any accesses which were safe before are
4056 * still safe. This is true even if old range < old off,
4057 * since someone could have accessed through (ptr - k), or
4058 * even done ptr -= k in a register, to get a safe access.
4059 */
4060 if (rold->range > rcur->range)
4061 return false;
4062 /* If the offsets don't match, we can't trust our alignment;
4063 * nor can we be sure that we won't fall out of range.
4064 */
4065 if (rold->off != rcur->off)
4066 return false;
4067 /* id relations must be preserved */
4068 if (rold->id && !check_ids(rold->id, rcur->id, idmap))
4069 return false;
4070 /* new val must satisfy old val knowledge */
4071 return range_within(rold, rcur) &&
4072 tnum_in(rold->var_off, rcur->var_off);
4073 case PTR_TO_CTX:
4074 case CONST_PTR_TO_MAP:
Edward Creef1174f72017-08-07 15:26:19 +01004075 case PTR_TO_PACKET_END:
4076 /* Only valid matches are exact, which memcmp() above
4077 * would have accepted
4078 */
4079 default:
4080 /* Don't know what's going on, just say it's not safe */
4081 return false;
4082 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004083
Edward Creef1174f72017-08-07 15:26:19 +01004084 /* Shouldn't get here; if we do, say it's not safe */
4085 WARN_ON_ONCE(1);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004086 return false;
4087}
4088
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004089static bool stacksafe(struct bpf_func_state *old,
4090 struct bpf_func_state *cur,
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004091 struct idpair *idmap)
4092{
4093 int i, spi;
4094
4095 /* if explored stack has more populated slots than current stack
4096 * such stacks are not equivalent
4097 */
4098 if (old->allocated_stack > cur->allocated_stack)
4099 return false;
4100
4101 /* walk slots of the explored stack and ignore any additional
4102 * slots in the current stack, since explored(safe) state
4103 * didn't use them
4104 */
4105 for (i = 0; i < old->allocated_stack; i++) {
4106 spi = i / BPF_REG_SIZE;
4107
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08004108 if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ))
4109 /* explored state didn't use this */
4110 return true;
4111
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004112 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID)
4113 continue;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08004114 /* if old state was safe with misc data in the stack
4115 * it will be safe with zero-initialized stack.
4116 * The opposite is not true
4117 */
4118 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC &&
4119 cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO)
4120 continue;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004121 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] !=
4122 cur->stack[spi].slot_type[i % BPF_REG_SIZE])
4123 /* Ex: old explored (safe) state has STACK_SPILL in
4124 * this stack slot, but current has has STACK_MISC ->
4125 * this verifier states are not equivalent,
4126 * return false to continue verification of this path
4127 */
4128 return false;
4129 if (i % BPF_REG_SIZE)
4130 continue;
4131 if (old->stack[spi].slot_type[0] != STACK_SPILL)
4132 continue;
4133 if (!regsafe(&old->stack[spi].spilled_ptr,
4134 &cur->stack[spi].spilled_ptr,
4135 idmap))
4136 /* when explored and current stack slot are both storing
4137 * spilled registers, check that stored pointers types
4138 * are the same as well.
4139 * Ex: explored safe path could have stored
4140 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8}
4141 * but current path has stored:
4142 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16}
4143 * such verifier states are not equivalent.
4144 * return false to continue verification of this path
4145 */
4146 return false;
4147 }
4148 return true;
4149}
4150
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004151/* compare two verifier states
4152 *
4153 * all states stored in state_list are known to be valid, since
4154 * verifier reached 'bpf_exit' instruction through them
4155 *
4156 * this function is called when verifier exploring different branches of
4157 * execution popped from the state stack. If it sees an old state that has
4158 * more strict register state and more strict stack state then this execution
4159 * branch doesn't need to be explored further, since verifier already
4160 * concluded that more strict state leads to valid finish.
4161 *
4162 * Therefore two states are equivalent if register state is more conservative
4163 * and explored stack state is more conservative than the current one.
4164 * Example:
4165 * explored current
4166 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
4167 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
4168 *
4169 * In other words if current stack state (one being explored) has more
4170 * valid slots than old one that already passed validation, it means
4171 * the verifier can stop exploring and conclude that current state is valid too
4172 *
4173 * Similarly with registers. If explored state has register type as invalid
4174 * whereas register type in current state is meaningful, it means that
4175 * the current state will reach 'bpf_exit' instruction safely
4176 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004177static bool func_states_equal(struct bpf_func_state *old,
4178 struct bpf_func_state *cur)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004179{
Edward Creef1174f72017-08-07 15:26:19 +01004180 struct idpair *idmap;
4181 bool ret = false;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004182 int i;
4183
Edward Creef1174f72017-08-07 15:26:19 +01004184 idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL);
4185 /* If we failed to allocate the idmap, just say it's not safe */
4186 if (!idmap)
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07004187 return false;
Edward Creef1174f72017-08-07 15:26:19 +01004188
4189 for (i = 0; i < MAX_BPF_REG; i++) {
Edward Cree1b688a12017-08-23 15:10:50 +01004190 if (!regsafe(&old->regs[i], &cur->regs[i], idmap))
Edward Creef1174f72017-08-07 15:26:19 +01004191 goto out_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004192 }
4193
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004194 if (!stacksafe(old, cur, idmap))
4195 goto out_free;
Edward Creef1174f72017-08-07 15:26:19 +01004196 ret = true;
4197out_free:
4198 kfree(idmap);
4199 return ret;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004200}
4201
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004202static bool states_equal(struct bpf_verifier_env *env,
4203 struct bpf_verifier_state *old,
4204 struct bpf_verifier_state *cur)
Edward Creedc503a82017-08-15 20:34:35 +01004205{
Edward Creedc503a82017-08-15 20:34:35 +01004206 int i;
4207
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004208 if (old->curframe != cur->curframe)
4209 return false;
4210
4211 /* for states to be equal callsites have to be the same
4212 * and all frame states need to be equivalent
4213 */
4214 for (i = 0; i <= old->curframe; i++) {
4215 if (old->frame[i]->callsite != cur->frame[i]->callsite)
4216 return false;
4217 if (!func_states_equal(old->frame[i], cur->frame[i]))
4218 return false;
4219 }
4220 return true;
4221}
4222
4223/* A write screens off any subsequent reads; but write marks come from the
4224 * straight-line code between a state and its parent. When we arrive at an
4225 * equivalent state (jump target or such) we didn't arrive by the straight-line
4226 * code, so read marks in the state must propagate to the parent regardless
4227 * of the state's write marks. That's what 'parent == state->parent' comparison
4228 * in mark_reg_read() and mark_stack_slot_read() is for.
4229 */
4230static int propagate_liveness(struct bpf_verifier_env *env,
4231 const struct bpf_verifier_state *vstate,
4232 struct bpf_verifier_state *vparent)
4233{
4234 int i, frame, err = 0;
4235 struct bpf_func_state *state, *parent;
4236
4237 if (vparent->curframe != vstate->curframe) {
4238 WARN(1, "propagate_live: parent frame %d current frame %d\n",
4239 vparent->curframe, vstate->curframe);
4240 return -EFAULT;
4241 }
Edward Creedc503a82017-08-15 20:34:35 +01004242 /* Propagate read liveness of registers... */
4243 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
4244 /* We don't need to worry about FP liveness because it's read-only */
4245 for (i = 0; i < BPF_REG_FP; i++) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004246 if (vparent->frame[vparent->curframe]->regs[i].live & REG_LIVE_READ)
Edward Creedc503a82017-08-15 20:34:35 +01004247 continue;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004248 if (vstate->frame[vstate->curframe]->regs[i].live & REG_LIVE_READ) {
4249 err = mark_reg_read(env, vstate, vparent, i);
4250 if (err)
4251 return err;
Edward Creedc503a82017-08-15 20:34:35 +01004252 }
4253 }
Edward Creedc503a82017-08-15 20:34:35 +01004254
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004255 /* ... and stack slots */
4256 for (frame = 0; frame <= vstate->curframe; frame++) {
4257 state = vstate->frame[frame];
4258 parent = vparent->frame[frame];
4259 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE &&
4260 i < parent->allocated_stack / BPF_REG_SIZE; i++) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004261 if (parent->stack[i].spilled_ptr.live & REG_LIVE_READ)
4262 continue;
4263 if (state->stack[i].spilled_ptr.live & REG_LIVE_READ)
4264 mark_stack_slot_read(env, vstate, vparent, i, frame);
4265 }
Edward Creedc503a82017-08-15 20:34:35 +01004266 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004267 return err;
Edward Creedc503a82017-08-15 20:34:35 +01004268}
4269
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004270static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004271{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004272 struct bpf_verifier_state_list *new_sl;
4273 struct bpf_verifier_state_list *sl;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004274 struct bpf_verifier_state *cur = env->cur_state;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004275 int i, j, err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004276
4277 sl = env->explored_states[insn_idx];
4278 if (!sl)
4279 /* this 'insn_idx' instruction wasn't marked, so we will not
4280 * be doing state search here
4281 */
4282 return 0;
4283
4284 while (sl != STATE_LIST_MARK) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004285 if (states_equal(env, &sl->state, cur)) {
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004286 /* reached equivalent register/stack state,
Edward Creedc503a82017-08-15 20:34:35 +01004287 * prune the search.
4288 * Registers read by the continuation are read by us.
Edward Cree8e9cd9c2017-08-23 15:11:21 +01004289 * If we have any write marks in env->cur_state, they
4290 * will prevent corresponding reads in the continuation
4291 * from reaching our parent (an explored_state). Our
4292 * own state will get the read marks recorded, but
4293 * they'll be immediately forgotten as we're pruning
4294 * this state and will pop a new one.
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004295 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004296 err = propagate_liveness(env, &sl->state, cur);
4297 if (err)
4298 return err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004299 return 1;
Edward Creedc503a82017-08-15 20:34:35 +01004300 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004301 sl = sl->next;
4302 }
4303
4304 /* there were no equivalent states, remember current one.
4305 * technically the current state is not proven to be safe yet,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004306 * but it will either reach outer most bpf_exit (which means it's safe)
4307 * or it will be rejected. Since there are no loops, we won't be
4308 * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx)
4309 * again on the way to bpf_exit
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004310 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004311 new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004312 if (!new_sl)
4313 return -ENOMEM;
4314
4315 /* add new state to the head of linked list */
Alexei Starovoitov1969db42017-11-01 00:08:04 -07004316 err = copy_verifier_state(&new_sl->state, cur);
4317 if (err) {
4318 free_verifier_state(&new_sl->state, false);
4319 kfree(new_sl);
4320 return err;
4321 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004322 new_sl->next = env->explored_states[insn_idx];
4323 env->explored_states[insn_idx] = new_sl;
Edward Creedc503a82017-08-15 20:34:35 +01004324 /* connect new state to parentage chain */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004325 cur->parent = &new_sl->state;
Edward Cree8e9cd9c2017-08-23 15:11:21 +01004326 /* clear write marks in current state: the writes we did are not writes
4327 * our child did, so they don't screen off its reads from us.
4328 * (There are no read marks in current state, because reads always mark
4329 * their parent and current state never has children yet. Only
4330 * explored_states can get read marks.)
4331 */
Edward Creedc503a82017-08-15 20:34:35 +01004332 for (i = 0; i < BPF_REG_FP; i++)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004333 cur->frame[cur->curframe]->regs[i].live = REG_LIVE_NONE;
4334
4335 /* all stack frames are accessible from callee, clear them all */
4336 for (j = 0; j <= cur->curframe; j++) {
4337 struct bpf_func_state *frame = cur->frame[j];
4338
4339 for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++)
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08004340 frame->stack[i].spilled_ptr.live = REG_LIVE_NONE;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004341 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004342 return 0;
4343}
4344
Jakub Kicinski13a27df2016-09-21 11:43:58 +01004345static int ext_analyzer_insn_hook(struct bpf_verifier_env *env,
4346 int insn_idx, int prev_insn_idx)
4347{
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07004348 if (env->dev_ops && env->dev_ops->insn_hook)
4349 return env->dev_ops->insn_hook(env, insn_idx, prev_insn_idx);
Jakub Kicinski13a27df2016-09-21 11:43:58 +01004350
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07004351 return 0;
Jakub Kicinski13a27df2016-09-21 11:43:58 +01004352}
4353
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004354static int do_check(struct bpf_verifier_env *env)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004355{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004356 struct bpf_verifier_state *state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004357 struct bpf_insn *insns = env->prog->insnsi;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004358 struct bpf_reg_state *regs;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004359 int insn_cnt = env->prog->len, i;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004360 int insn_idx, prev_insn_idx = 0;
4361 int insn_processed = 0;
4362 bool do_print_state = false;
4363
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004364 state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL);
4365 if (!state)
4366 return -ENOMEM;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004367 state->curframe = 0;
Edward Creedc503a82017-08-15 20:34:35 +01004368 state->parent = NULL;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004369 state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL);
4370 if (!state->frame[0]) {
4371 kfree(state);
4372 return -ENOMEM;
4373 }
4374 env->cur_state = state;
4375 init_func_state(env, state->frame[0],
4376 BPF_MAIN_FUNC /* callsite */,
4377 0 /* frameno */,
4378 0 /* subprogno, zero == main subprog */);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004379 insn_idx = 0;
4380 for (;;) {
4381 struct bpf_insn *insn;
4382 u8 class;
4383 int err;
4384
4385 if (insn_idx >= insn_cnt) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004386 verbose(env, "invalid insn idx %d insn_cnt %d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004387 insn_idx, insn_cnt);
4388 return -EFAULT;
4389 }
4390
4391 insn = &insns[insn_idx];
4392 class = BPF_CLASS(insn->code);
4393
Daniel Borkmann07016152016-04-05 22:33:17 +02004394 if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004395 verbose(env,
4396 "BPF program is too large. Processed %d insn\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004397 insn_processed);
4398 return -E2BIG;
4399 }
4400
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004401 err = is_state_visited(env, insn_idx);
4402 if (err < 0)
4403 return err;
4404 if (err == 1) {
4405 /* found equivalent state, can prune the search */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004406 if (env->log.level) {
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004407 if (do_print_state)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004408 verbose(env, "\nfrom %d to %d: safe\n",
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004409 prev_insn_idx, insn_idx);
4410 else
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004411 verbose(env, "%d: safe\n", insn_idx);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004412 }
4413 goto process_bpf_exit;
4414 }
4415
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02004416 if (need_resched())
4417 cond_resched();
4418
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004419 if (env->log.level > 1 || (env->log.level && do_print_state)) {
4420 if (env->log.level > 1)
4421 verbose(env, "%d:", insn_idx);
David S. Millerc5fc9692017-05-10 11:25:17 -07004422 else
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004423 verbose(env, "\nfrom %d to %d:",
David S. Millerc5fc9692017-05-10 11:25:17 -07004424 prev_insn_idx, insn_idx);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004425 print_verifier_state(env, state->frame[state->curframe]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004426 do_print_state = false;
4427 }
4428
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004429 if (env->log.level) {
4430 verbose(env, "%d: ", insn_idx);
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -07004431 print_bpf_insn(verbose, env, insn,
4432 env->allow_ptr_leaks);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004433 }
4434
Jakub Kicinski13a27df2016-09-21 11:43:58 +01004435 err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx);
4436 if (err)
4437 return err;
4438
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004439 regs = cur_regs(env);
Alexei Starovoitovc1311872017-11-22 16:42:05 -08004440 env->insn_aux_data[insn_idx].seen = true;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004441 if (class == BPF_ALU || class == BPF_ALU64) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004442 err = check_alu_op(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004443 if (err)
4444 return err;
4445
4446 } else if (class == BPF_LDX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004447 enum bpf_reg_type *prev_src_type, src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004448
4449 /* check for reserved fields is already done */
4450
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004451 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01004452 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004453 if (err)
4454 return err;
4455
Edward Creedc503a82017-08-15 20:34:35 +01004456 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004457 if (err)
4458 return err;
4459
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07004460 src_reg_type = regs[insn->src_reg].type;
4461
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004462 /* check that memory (src_reg + off) is readable,
4463 * the state of dst_reg will be updated by this func
4464 */
Yonghong Song31fd8582017-06-13 15:52:13 -07004465 err = check_mem_access(env, insn_idx, insn->src_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004466 BPF_SIZE(insn->code), BPF_READ,
4467 insn->dst_reg);
4468 if (err)
4469 return err;
4470
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004471 prev_src_type = &env->insn_aux_data[insn_idx].ptr_type;
4472
4473 if (*prev_src_type == NOT_INIT) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004474 /* saw a valid insn
4475 * dst_reg = *(u32 *)(src_reg + off)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004476 * save type to validate intersecting paths
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004477 */
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004478 *prev_src_type = src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004479
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004480 } else if (src_reg_type != *prev_src_type &&
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004481 (src_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004482 *prev_src_type == PTR_TO_CTX)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004483 /* ABuser program is trying to use the same insn
4484 * dst_reg = *(u32*) (src_reg + off)
4485 * with different pointer types:
4486 * src_reg == ctx in one branch and
4487 * src_reg == stack|map in some other branch.
4488 * Reject it.
4489 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004490 verbose(env, "same insn cannot be used with different pointers\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004491 return -EINVAL;
4492 }
4493
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004494 } else if (class == BPF_STX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004495 enum bpf_reg_type *prev_dst_type, dst_reg_type;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004496
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004497 if (BPF_MODE(insn->code) == BPF_XADD) {
Yonghong Song31fd8582017-06-13 15:52:13 -07004498 err = check_xadd(env, insn_idx, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004499 if (err)
4500 return err;
4501 insn_idx++;
4502 continue;
4503 }
4504
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004505 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01004506 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004507 if (err)
4508 return err;
4509 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01004510 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004511 if (err)
4512 return err;
4513
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004514 dst_reg_type = regs[insn->dst_reg].type;
4515
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004516 /* check that memory (dst_reg + off) is writeable */
Yonghong Song31fd8582017-06-13 15:52:13 -07004517 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004518 BPF_SIZE(insn->code), BPF_WRITE,
4519 insn->src_reg);
4520 if (err)
4521 return err;
4522
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004523 prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type;
4524
4525 if (*prev_dst_type == NOT_INIT) {
4526 *prev_dst_type = dst_reg_type;
4527 } else if (dst_reg_type != *prev_dst_type &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004528 (dst_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004529 *prev_dst_type == PTR_TO_CTX)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004530 verbose(env, "same insn cannot be used with different pointers\n");
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004531 return -EINVAL;
4532 }
4533
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004534 } else if (class == BPF_ST) {
4535 if (BPF_MODE(insn->code) != BPF_MEM ||
4536 insn->src_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004537 verbose(env, "BPF_ST uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004538 return -EINVAL;
4539 }
4540 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01004541 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004542 if (err)
4543 return err;
4544
4545 /* check that memory (dst_reg + off) is writeable */
Yonghong Song31fd8582017-06-13 15:52:13 -07004546 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004547 BPF_SIZE(insn->code), BPF_WRITE,
4548 -1);
4549 if (err)
4550 return err;
4551
4552 } else if (class == BPF_JMP) {
4553 u8 opcode = BPF_OP(insn->code);
4554
4555 if (opcode == BPF_CALL) {
4556 if (BPF_SRC(insn->code) != BPF_K ||
4557 insn->off != 0 ||
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004558 (insn->src_reg != BPF_REG_0 &&
4559 insn->src_reg != BPF_PSEUDO_CALL) ||
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004560 insn->dst_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004561 verbose(env, "BPF_CALL uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004562 return -EINVAL;
4563 }
4564
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004565 if (insn->src_reg == BPF_PSEUDO_CALL)
4566 err = check_func_call(env, insn, &insn_idx);
4567 else
4568 err = check_helper_call(env, insn->imm, insn_idx);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004569 if (err)
4570 return err;
4571
4572 } else if (opcode == BPF_JA) {
4573 if (BPF_SRC(insn->code) != BPF_K ||
4574 insn->imm != 0 ||
4575 insn->src_reg != BPF_REG_0 ||
4576 insn->dst_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004577 verbose(env, "BPF_JA uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004578 return -EINVAL;
4579 }
4580
4581 insn_idx += insn->off + 1;
4582 continue;
4583
4584 } else if (opcode == BPF_EXIT) {
4585 if (BPF_SRC(insn->code) != BPF_K ||
4586 insn->imm != 0 ||
4587 insn->src_reg != BPF_REG_0 ||
4588 insn->dst_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004589 verbose(env, "BPF_EXIT uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004590 return -EINVAL;
4591 }
4592
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004593 if (state->curframe) {
4594 /* exit from nested function */
4595 prev_insn_idx = insn_idx;
4596 err = prepare_func_exit(env, &insn_idx);
4597 if (err)
4598 return err;
4599 do_print_state = true;
4600 continue;
4601 }
4602
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004603 /* eBPF calling convetion is such that R0 is used
4604 * to return the value from eBPF program.
4605 * Make sure that it's readable at this time
4606 * of bpf_exit, which means that program wrote
4607 * something into it earlier
4608 */
Edward Creedc503a82017-08-15 20:34:35 +01004609 err = check_reg_arg(env, BPF_REG_0, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004610 if (err)
4611 return err;
4612
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004613 if (is_pointer_value(env, BPF_REG_0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004614 verbose(env, "R0 leaks addr as return value\n");
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004615 return -EACCES;
4616 }
4617
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07004618 err = check_return_code(env);
4619 if (err)
4620 return err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004621process_bpf_exit:
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004622 err = pop_stack(env, &prev_insn_idx, &insn_idx);
4623 if (err < 0) {
4624 if (err != -ENOENT)
4625 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004626 break;
4627 } else {
4628 do_print_state = true;
4629 continue;
4630 }
4631 } else {
4632 err = check_cond_jmp_op(env, insn, &insn_idx);
4633 if (err)
4634 return err;
4635 }
4636 } else if (class == BPF_LD) {
4637 u8 mode = BPF_MODE(insn->code);
4638
4639 if (mode == BPF_ABS || mode == BPF_IND) {
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004640 err = check_ld_abs(env, insn);
4641 if (err)
4642 return err;
4643
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004644 } else if (mode == BPF_IMM) {
4645 err = check_ld_imm(env, insn);
4646 if (err)
4647 return err;
4648
4649 insn_idx++;
Alexei Starovoitovc1311872017-11-22 16:42:05 -08004650 env->insn_aux_data[insn_idx].seen = true;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004651 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004652 verbose(env, "invalid BPF_LD mode\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004653 return -EINVAL;
4654 }
4655 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004656 verbose(env, "unknown insn class %d\n", class);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004657 return -EINVAL;
4658 }
4659
4660 insn_idx++;
4661 }
4662
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004663 verbose(env, "processed %d insns, stack depth ", insn_processed);
4664 for (i = 0; i < env->subprog_cnt + 1; i++) {
4665 u32 depth = env->subprog_stack_depth[i];
4666
4667 verbose(env, "%d", depth);
4668 if (i + 1 < env->subprog_cnt + 1)
4669 verbose(env, "+");
4670 }
4671 verbose(env, "\n");
4672 env->prog->aux->stack_depth = env->subprog_stack_depth[0];
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004673 return 0;
4674}
4675
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004676static int check_map_prealloc(struct bpf_map *map)
4677{
4678 return (map->map_type != BPF_MAP_TYPE_HASH &&
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07004679 map->map_type != BPF_MAP_TYPE_PERCPU_HASH &&
4680 map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) ||
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004681 !(map->map_flags & BPF_F_NO_PREALLOC);
4682}
4683
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004684static int check_map_prog_compatibility(struct bpf_verifier_env *env,
4685 struct bpf_map *map,
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07004686 struct bpf_prog *prog)
4687
4688{
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004689 /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use
4690 * preallocated hash maps, since doing memory allocation
4691 * in overflow_handler can crash depending on where nmi got
4692 * triggered.
4693 */
4694 if (prog->type == BPF_PROG_TYPE_PERF_EVENT) {
4695 if (!check_map_prealloc(map)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004696 verbose(env, "perf_event programs can only use preallocated hash map\n");
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004697 return -EINVAL;
4698 }
4699 if (map->inner_map_meta &&
4700 !check_map_prealloc(map->inner_map_meta)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004701 verbose(env, "perf_event programs can only use preallocated inner hash map\n");
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004702 return -EINVAL;
4703 }
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07004704 }
4705 return 0;
4706}
4707
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004708/* look for pseudo eBPF instructions that access map FDs and
4709 * replace them with actual map pointers
4710 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004711static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004712{
4713 struct bpf_insn *insn = env->prog->insnsi;
4714 int insn_cnt = env->prog->len;
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07004715 int i, j, err;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004716
Daniel Borkmannf1f77142017-01-13 23:38:15 +01004717 err = bpf_prog_calc_tag(env->prog);
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01004718 if (err)
4719 return err;
4720
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004721 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004722 if (BPF_CLASS(insn->code) == BPF_LDX &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004723 (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004724 verbose(env, "BPF_LDX uses reserved fields\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004725 return -EINVAL;
4726 }
4727
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004728 if (BPF_CLASS(insn->code) == BPF_STX &&
4729 ((BPF_MODE(insn->code) != BPF_MEM &&
4730 BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004731 verbose(env, "BPF_STX uses reserved fields\n");
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004732 return -EINVAL;
4733 }
4734
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004735 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
4736 struct bpf_map *map;
4737 struct fd f;
4738
4739 if (i == insn_cnt - 1 || insn[1].code != 0 ||
4740 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
4741 insn[1].off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004742 verbose(env, "invalid bpf_ld_imm64 insn\n");
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004743 return -EINVAL;
4744 }
4745
4746 if (insn->src_reg == 0)
4747 /* valid generic load 64-bit imm */
4748 goto next_insn;
4749
4750 if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004751 verbose(env,
4752 "unrecognized bpf_ld_imm64 insn\n");
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004753 return -EINVAL;
4754 }
4755
4756 f = fdget(insn->imm);
Daniel Borkmannc2101292015-10-29 14:58:07 +01004757 map = __bpf_map_get(f);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004758 if (IS_ERR(map)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004759 verbose(env, "fd %d is not pointing to valid bpf_map\n",
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004760 insn->imm);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004761 return PTR_ERR(map);
4762 }
4763
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004764 err = check_map_prog_compatibility(env, map, env->prog);
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07004765 if (err) {
4766 fdput(f);
4767 return err;
4768 }
4769
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004770 /* store map pointer inside BPF_LD_IMM64 instruction */
4771 insn[0].imm = (u32) (unsigned long) map;
4772 insn[1].imm = ((u64) (unsigned long) map) >> 32;
4773
4774 /* check whether we recorded this map already */
4775 for (j = 0; j < env->used_map_cnt; j++)
4776 if (env->used_maps[j] == map) {
4777 fdput(f);
4778 goto next_insn;
4779 }
4780
4781 if (env->used_map_cnt >= MAX_USED_MAPS) {
4782 fdput(f);
4783 return -E2BIG;
4784 }
4785
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004786 /* hold the map. If the program is rejected by verifier,
4787 * the map will be released by release_maps() or it
4788 * will be used by the valid program until it's unloaded
4789 * and all maps are released in free_bpf_prog_info()
4790 */
Alexei Starovoitov92117d82016-04-27 18:56:20 -07004791 map = bpf_map_inc(map, false);
4792 if (IS_ERR(map)) {
4793 fdput(f);
4794 return PTR_ERR(map);
4795 }
4796 env->used_maps[env->used_map_cnt++] = map;
4797
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004798 fdput(f);
4799next_insn:
4800 insn++;
4801 i++;
4802 }
4803 }
4804
4805 /* now all pseudo BPF_LD_IMM64 instructions load valid
4806 * 'struct bpf_map *' into a register instead of user map_fd.
4807 * These pointers will be used later by verifier to validate map access.
4808 */
4809 return 0;
4810}
4811
4812/* drop refcnt of maps used by the rejected program */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004813static void release_maps(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004814{
4815 int i;
4816
4817 for (i = 0; i < env->used_map_cnt; i++)
4818 bpf_map_put(env->used_maps[i]);
4819}
4820
4821/* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004822static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004823{
4824 struct bpf_insn *insn = env->prog->insnsi;
4825 int insn_cnt = env->prog->len;
4826 int i;
4827
4828 for (i = 0; i < insn_cnt; i++, insn++)
4829 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
4830 insn->src_reg = 0;
4831}
4832
Alexei Starovoitov80419022017-03-15 18:26:41 -07004833/* single env->prog->insni[off] instruction was replaced with the range
4834 * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
4835 * [0, off) and [off, end) to new locations, so the patched range stays zero
4836 */
4837static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len,
4838 u32 off, u32 cnt)
4839{
4840 struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data;
Alexei Starovoitovc1311872017-11-22 16:42:05 -08004841 int i;
Alexei Starovoitov80419022017-03-15 18:26:41 -07004842
4843 if (cnt == 1)
4844 return 0;
4845 new_data = vzalloc(sizeof(struct bpf_insn_aux_data) * prog_len);
4846 if (!new_data)
4847 return -ENOMEM;
4848 memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off);
4849 memcpy(new_data + off + cnt - 1, old_data + off,
4850 sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
Alexei Starovoitovc1311872017-11-22 16:42:05 -08004851 for (i = off; i < off + cnt - 1; i++)
4852 new_data[i].seen = true;
Alexei Starovoitov80419022017-03-15 18:26:41 -07004853 env->insn_aux_data = new_data;
4854 vfree(old_data);
4855 return 0;
4856}
4857
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08004858static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len)
4859{
4860 int i;
4861
4862 if (len == 1)
4863 return;
4864 for (i = 0; i < env->subprog_cnt; i++) {
4865 if (env->subprog_starts[i] < off)
4866 continue;
4867 env->subprog_starts[i] += len - 1;
4868 }
4869}
4870
Alexei Starovoitov80419022017-03-15 18:26:41 -07004871static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
4872 const struct bpf_insn *patch, u32 len)
4873{
4874 struct bpf_prog *new_prog;
4875
4876 new_prog = bpf_patch_insn_single(env->prog, off, patch, len);
4877 if (!new_prog)
4878 return NULL;
4879 if (adjust_insn_aux_data(env, new_prog->len, off, len))
4880 return NULL;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08004881 adjust_subprog_starts(env, off, len);
Alexei Starovoitov80419022017-03-15 18:26:41 -07004882 return new_prog;
4883}
4884
Alexei Starovoitovc1311872017-11-22 16:42:05 -08004885/* The verifier does more data flow analysis than llvm and will not explore
4886 * branches that are dead at run time. Malicious programs can have dead code
4887 * too. Therefore replace all dead at-run-time code with nops.
4888 */
4889static void sanitize_dead_code(struct bpf_verifier_env *env)
4890{
4891 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
4892 struct bpf_insn nop = BPF_MOV64_REG(BPF_REG_0, BPF_REG_0);
4893 struct bpf_insn *insn = env->prog->insnsi;
4894 const int insn_cnt = env->prog->len;
4895 int i;
4896
4897 for (i = 0; i < insn_cnt; i++) {
4898 if (aux_data[i].seen)
4899 continue;
4900 memcpy(insn + i, &nop, sizeof(nop));
4901 }
4902}
4903
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004904/* convert load instructions that access fields of 'struct __sk_buff'
4905 * into sequence of instructions that access fields of 'struct sk_buff'
4906 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004907static int convert_ctx_accesses(struct bpf_verifier_env *env)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004908{
Jakub Kicinski00176a32017-10-16 16:40:54 -07004909 const struct bpf_verifier_ops *ops = env->ops;
Daniel Borkmannf96da092017-07-02 02:13:27 +02004910 int i, cnt, size, ctx_field_size, delta = 0;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004911 const int insn_cnt = env->prog->len;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004912 struct bpf_insn insn_buf[16], *insn;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004913 struct bpf_prog *new_prog;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004914 enum bpf_access_type type;
Daniel Borkmannf96da092017-07-02 02:13:27 +02004915 bool is_narrower_load;
4916 u32 target_size;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004917
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004918 if (ops->gen_prologue) {
4919 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
4920 env->prog);
4921 if (cnt >= ARRAY_SIZE(insn_buf)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004922 verbose(env, "bpf verifier is misconfigured\n");
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004923 return -EINVAL;
4924 } else if (cnt) {
Alexei Starovoitov80419022017-03-15 18:26:41 -07004925 new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004926 if (!new_prog)
4927 return -ENOMEM;
Alexei Starovoitov80419022017-03-15 18:26:41 -07004928
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004929 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004930 delta += cnt - 1;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004931 }
4932 }
4933
4934 if (!ops->convert_ctx_access)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004935 return 0;
4936
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004937 insn = env->prog->insnsi + delta;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02004938
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004939 for (i = 0; i < insn_cnt; i++, insn++) {
Daniel Borkmann62c79892017-01-12 11:51:33 +01004940 if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) ||
4941 insn->code == (BPF_LDX | BPF_MEM | BPF_H) ||
4942 insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07004943 insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004944 type = BPF_READ;
Daniel Borkmann62c79892017-01-12 11:51:33 +01004945 else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) ||
4946 insn->code == (BPF_STX | BPF_MEM | BPF_H) ||
4947 insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07004948 insn->code == (BPF_STX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004949 type = BPF_WRITE;
4950 else
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004951 continue;
4952
Alexei Starovoitov80419022017-03-15 18:26:41 -07004953 if (env->insn_aux_data[i + delta].ptr_type != PTR_TO_CTX)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004954 continue;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004955
Yonghong Song31fd8582017-06-13 15:52:13 -07004956 ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size;
Daniel Borkmannf96da092017-07-02 02:13:27 +02004957 size = BPF_LDST_BYTES(insn);
Yonghong Song31fd8582017-06-13 15:52:13 -07004958
4959 /* If the read access is a narrower load of the field,
4960 * convert to a 4/8-byte load, to minimum program type specific
4961 * convert_ctx_access changes. If conversion is successful,
4962 * we will apply proper mask to the result.
4963 */
Daniel Borkmannf96da092017-07-02 02:13:27 +02004964 is_narrower_load = size < ctx_field_size;
Yonghong Song31fd8582017-06-13 15:52:13 -07004965 if (is_narrower_load) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02004966 u32 off = insn->off;
4967 u8 size_code;
Yonghong Song31fd8582017-06-13 15:52:13 -07004968
Daniel Borkmannf96da092017-07-02 02:13:27 +02004969 if (type == BPF_WRITE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004970 verbose(env, "bpf verifier narrow ctx access misconfigured\n");
Daniel Borkmannf96da092017-07-02 02:13:27 +02004971 return -EINVAL;
4972 }
4973
4974 size_code = BPF_H;
Yonghong Song31fd8582017-06-13 15:52:13 -07004975 if (ctx_field_size == 4)
4976 size_code = BPF_W;
4977 else if (ctx_field_size == 8)
4978 size_code = BPF_DW;
Daniel Borkmannf96da092017-07-02 02:13:27 +02004979
Yonghong Song31fd8582017-06-13 15:52:13 -07004980 insn->off = off & ~(ctx_field_size - 1);
4981 insn->code = BPF_LDX | BPF_MEM | size_code;
4982 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02004983
4984 target_size = 0;
4985 cnt = ops->convert_ctx_access(type, insn, insn_buf, env->prog,
4986 &target_size);
4987 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) ||
4988 (ctx_field_size && !target_size)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004989 verbose(env, "bpf verifier is misconfigured\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004990 return -EINVAL;
4991 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02004992
4993 if (is_narrower_load && size < target_size) {
Yonghong Song31fd8582017-06-13 15:52:13 -07004994 if (ctx_field_size <= 4)
4995 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02004996 (1 << size * 8) - 1);
Yonghong Song31fd8582017-06-13 15:52:13 -07004997 else
4998 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02004999 (1 << size * 8) - 1);
Yonghong Song31fd8582017-06-13 15:52:13 -07005000 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005001
Alexei Starovoitov80419022017-03-15 18:26:41 -07005002 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005003 if (!new_prog)
5004 return -ENOMEM;
5005
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005006 delta += cnt - 1;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005007
5008 /* keep walking new program and skip insns we just inserted */
5009 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005010 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005011 }
5012
5013 return 0;
5014}
5015
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005016static int jit_subprogs(struct bpf_verifier_env *env)
5017{
5018 struct bpf_prog *prog = env->prog, **func, *tmp;
5019 int i, j, subprog_start, subprog_end = 0, len, subprog;
5020 struct bpf_insn *insn = prog->insnsi;
5021 void *old_bpf_func;
5022 int err = -ENOMEM;
5023
5024 if (env->subprog_cnt == 0)
5025 return 0;
5026
5027 for (i = 0; i < prog->len; i++, insn++) {
5028 if (insn->code != (BPF_JMP | BPF_CALL) ||
5029 insn->src_reg != BPF_PSEUDO_CALL)
5030 continue;
5031 subprog = find_subprog(env, i + insn->imm + 1);
5032 if (subprog < 0) {
5033 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
5034 i + insn->imm + 1);
5035 return -EFAULT;
5036 }
5037 /* temporarily remember subprog id inside insn instead of
5038 * aux_data, since next loop will split up all insns into funcs
5039 */
5040 insn->off = subprog + 1;
5041 /* remember original imm in case JIT fails and fallback
5042 * to interpreter will be needed
5043 */
5044 env->insn_aux_data[i].call_imm = insn->imm;
5045 /* point imm to __bpf_call_base+1 from JITs point of view */
5046 insn->imm = 1;
5047 }
5048
5049 func = kzalloc(sizeof(prog) * (env->subprog_cnt + 1), GFP_KERNEL);
5050 if (!func)
5051 return -ENOMEM;
5052
5053 for (i = 0; i <= env->subprog_cnt; i++) {
5054 subprog_start = subprog_end;
5055 if (env->subprog_cnt == i)
5056 subprog_end = prog->len;
5057 else
5058 subprog_end = env->subprog_starts[i];
5059
5060 len = subprog_end - subprog_start;
5061 func[i] = bpf_prog_alloc(bpf_prog_size(len), GFP_USER);
5062 if (!func[i])
5063 goto out_free;
5064 memcpy(func[i]->insnsi, &prog->insnsi[subprog_start],
5065 len * sizeof(struct bpf_insn));
5066 func[i]->len = len;
5067 func[i]->is_func = 1;
5068 /* Use bpf_prog_F_tag to indicate functions in stack traces.
5069 * Long term would need debug info to populate names
5070 */
5071 func[i]->aux->name[0] = 'F';
5072 func[i]->aux->stack_depth = env->subprog_stack_depth[i];
5073 func[i]->jit_requested = 1;
5074 func[i] = bpf_int_jit_compile(func[i]);
5075 if (!func[i]->jited) {
5076 err = -ENOTSUPP;
5077 goto out_free;
5078 }
5079 cond_resched();
5080 }
5081 /* at this point all bpf functions were successfully JITed
5082 * now populate all bpf_calls with correct addresses and
5083 * run last pass of JIT
5084 */
5085 for (i = 0; i <= env->subprog_cnt; i++) {
5086 insn = func[i]->insnsi;
5087 for (j = 0; j < func[i]->len; j++, insn++) {
5088 if (insn->code != (BPF_JMP | BPF_CALL) ||
5089 insn->src_reg != BPF_PSEUDO_CALL)
5090 continue;
5091 subprog = insn->off;
5092 insn->off = 0;
5093 insn->imm = (u64 (*)(u64, u64, u64, u64, u64))
5094 func[subprog]->bpf_func -
5095 __bpf_call_base;
5096 }
5097 }
5098 for (i = 0; i <= env->subprog_cnt; i++) {
5099 old_bpf_func = func[i]->bpf_func;
5100 tmp = bpf_int_jit_compile(func[i]);
5101 if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) {
5102 verbose(env, "JIT doesn't support bpf-to-bpf calls\n");
5103 err = -EFAULT;
5104 goto out_free;
5105 }
5106 cond_resched();
5107 }
5108
5109 /* finally lock prog and jit images for all functions and
5110 * populate kallsysm
5111 */
5112 for (i = 0; i <= env->subprog_cnt; i++) {
5113 bpf_prog_lock_ro(func[i]);
5114 bpf_prog_kallsyms_add(func[i]);
5115 }
5116 prog->jited = 1;
5117 prog->bpf_func = func[0]->bpf_func;
5118 prog->aux->func = func;
5119 prog->aux->func_cnt = env->subprog_cnt + 1;
5120 return 0;
5121out_free:
5122 for (i = 0; i <= env->subprog_cnt; i++)
5123 if (func[i])
5124 bpf_jit_free(func[i]);
5125 kfree(func);
5126 /* cleanup main prog to be interpreted */
5127 prog->jit_requested = 0;
5128 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
5129 if (insn->code != (BPF_JMP | BPF_CALL) ||
5130 insn->src_reg != BPF_PSEUDO_CALL)
5131 continue;
5132 insn->off = 0;
5133 insn->imm = env->insn_aux_data[i].call_imm;
5134 }
5135 return err;
5136}
5137
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08005138static int fixup_call_args(struct bpf_verifier_env *env)
5139{
5140 struct bpf_prog *prog = env->prog;
5141 struct bpf_insn *insn = prog->insnsi;
5142 int i, depth;
5143
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005144 if (env->prog->jit_requested)
5145 if (jit_subprogs(env) == 0)
5146 return 0;
5147
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08005148 for (i = 0; i < prog->len; i++, insn++) {
5149 if (insn->code != (BPF_JMP | BPF_CALL) ||
5150 insn->src_reg != BPF_PSEUDO_CALL)
5151 continue;
5152 depth = get_callee_stack_depth(env, insn, i);
5153 if (depth < 0)
5154 return depth;
5155 bpf_patch_call_args(insn, depth);
5156 }
5157 return 0;
5158}
5159
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005160/* fixup insn->imm field of bpf_call instructions
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07005161 * and inline eligible helpers as explicit sequence of BPF instructions
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005162 *
5163 * this function is called after eBPF program passed verification
5164 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005165static int fixup_bpf_calls(struct bpf_verifier_env *env)
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005166{
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005167 struct bpf_prog *prog = env->prog;
5168 struct bpf_insn *insn = prog->insnsi;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005169 const struct bpf_func_proto *fn;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005170 const int insn_cnt = prog->len;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07005171 struct bpf_insn insn_buf[16];
5172 struct bpf_prog *new_prog;
5173 struct bpf_map *map_ptr;
5174 int i, cnt, delta = 0;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005175
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005176 for (i = 0; i < insn_cnt; i++, insn++) {
5177 if (insn->code != (BPF_JMP | BPF_CALL))
5178 continue;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08005179 if (insn->src_reg == BPF_PSEUDO_CALL)
5180 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005181
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005182 if (insn->imm == BPF_FUNC_get_route_realm)
5183 prog->dst_needed = 1;
5184 if (insn->imm == BPF_FUNC_get_prandom_u32)
5185 bpf_user_rnd_init_once();
Josef Bacik9802d862017-12-11 11:36:48 -05005186 if (insn->imm == BPF_FUNC_override_return)
5187 prog->kprobe_override = 1;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005188 if (insn->imm == BPF_FUNC_tail_call) {
David S. Miller7b9f6da2017-04-20 10:35:33 -04005189 /* If we tail call into other programs, we
5190 * cannot make any assumptions since they can
5191 * be replaced dynamically during runtime in
5192 * the program array.
5193 */
5194 prog->cb_access = 1;
Alexei Starovoitov80a58d02017-05-30 13:31:30 -07005195 env->prog->aux->stack_depth = MAX_BPF_STACK;
David S. Miller7b9f6da2017-04-20 10:35:33 -04005196
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005197 /* mark bpf_tail_call as different opcode to avoid
5198 * conditional branch in the interpeter for every normal
5199 * call and to prevent accidental JITing by JIT compiler
5200 * that doesn't support bpf_tail_call yet
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005201 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005202 insn->imm = 0;
Alexei Starovoitov71189fa2017-05-30 13:31:27 -07005203 insn->code = BPF_JMP | BPF_TAIL_CALL;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005204 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005205 }
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005206
Daniel Borkmann89c63072017-08-19 03:12:45 +02005207 /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup
5208 * handlers are currently limited to 64 bit only.
5209 */
Alexei Starovoitov60b58afc2017-12-14 17:55:14 -08005210 if (prog->jit_requested && BITS_PER_LONG == 64 &&
Daniel Borkmann89c63072017-08-19 03:12:45 +02005211 insn->imm == BPF_FUNC_map_lookup_elem) {
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07005212 map_ptr = env->insn_aux_data[i + delta].map_ptr;
Martin KaFai Laufad73a12017-03-22 10:00:32 -07005213 if (map_ptr == BPF_MAP_PTR_POISON ||
5214 !map_ptr->ops->map_gen_lookup)
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07005215 goto patch_call_imm;
5216
5217 cnt = map_ptr->ops->map_gen_lookup(map_ptr, insn_buf);
5218 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005219 verbose(env, "bpf verifier is misconfigured\n");
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07005220 return -EINVAL;
5221 }
5222
5223 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf,
5224 cnt);
5225 if (!new_prog)
5226 return -ENOMEM;
5227
5228 delta += cnt - 1;
5229
5230 /* keep walking new program and skip insns we just inserted */
5231 env->prog = prog = new_prog;
5232 insn = new_prog->insnsi + i + delta;
5233 continue;
5234 }
5235
Daniel Borkmann109980b2017-09-08 00:14:51 +02005236 if (insn->imm == BPF_FUNC_redirect_map) {
Daniel Borkmann7c300132017-09-20 00:44:21 +02005237 /* Note, we cannot use prog directly as imm as subsequent
5238 * rewrites would still change the prog pointer. The only
5239 * stable address we can use is aux, which also works with
5240 * prog clones during blinding.
5241 */
5242 u64 addr = (unsigned long)prog->aux;
Daniel Borkmann109980b2017-09-08 00:14:51 +02005243 struct bpf_insn r4_ld[] = {
5244 BPF_LD_IMM64(BPF_REG_4, addr),
5245 *insn,
5246 };
5247 cnt = ARRAY_SIZE(r4_ld);
5248
5249 new_prog = bpf_patch_insn_data(env, i + delta, r4_ld, cnt);
5250 if (!new_prog)
5251 return -ENOMEM;
5252
5253 delta += cnt - 1;
5254 env->prog = prog = new_prog;
5255 insn = new_prog->insnsi + i + delta;
5256 }
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07005257patch_call_imm:
Jakub Kicinski00176a32017-10-16 16:40:54 -07005258 fn = env->ops->get_func_proto(insn->imm);
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005259 /* all functions that have prototype and verifier allowed
5260 * programs to call them, must be real in-kernel functions
5261 */
5262 if (!fn->func) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005263 verbose(env,
5264 "kernel subsystem misconfigured func %s#%d\n",
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005265 func_id_name(insn->imm), insn->imm);
5266 return -EFAULT;
5267 }
5268 insn->imm = fn->func - __bpf_call_base;
5269 }
5270
5271 return 0;
5272}
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005273
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005274static void free_states(struct bpf_verifier_env *env)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005275{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005276 struct bpf_verifier_state_list *sl, *sln;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005277 int i;
5278
5279 if (!env->explored_states)
5280 return;
5281
5282 for (i = 0; i < env->prog->len; i++) {
5283 sl = env->explored_states[i];
5284
5285 if (sl)
5286 while (sl != STATE_LIST_MARK) {
5287 sln = sl->next;
Alexei Starovoitov1969db42017-11-01 00:08:04 -07005288 free_verifier_state(&sl->state, false);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005289 kfree(sl);
5290 sl = sln;
5291 }
5292 }
5293
5294 kfree(env->explored_states);
5295}
5296
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005297int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
Alexei Starovoitov51580e72014-09-26 00:17:02 -07005298{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005299 struct bpf_verifier_env *env;
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005300 struct bpf_verifer_log *log;
Alexei Starovoitov51580e72014-09-26 00:17:02 -07005301 int ret = -EINVAL;
5302
Arnd Bergmanneba0c922017-11-02 12:05:52 +01005303 /* no program is valid */
5304 if (ARRAY_SIZE(bpf_verifier_ops) == 0)
5305 return -EINVAL;
5306
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005307 /* 'struct bpf_verifier_env' can be global, but since it's not small,
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005308 * allocate/free it every time bpf_check() is called
5309 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005310 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005311 if (!env)
5312 return -ENOMEM;
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005313 log = &env->log;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005314
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005315 env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
5316 (*prog)->len);
5317 ret = -ENOMEM;
5318 if (!env->insn_aux_data)
5319 goto err_free_env;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005320 env->prog = *prog;
Jakub Kicinski00176a32017-10-16 16:40:54 -07005321 env->ops = bpf_verifier_ops[env->prog->type];
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005322
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005323 /* grab the mutex to protect few globals used by verifier */
5324 mutex_lock(&bpf_verifier_lock);
5325
5326 if (attr->log_level || attr->log_buf || attr->log_size) {
5327 /* user requested verbose verifier output
5328 * and supplied buffer to store the verification trace
5329 */
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07005330 log->level = attr->log_level;
5331 log->ubuf = (char __user *) (unsigned long) attr->log_buf;
5332 log->len_total = attr->log_size;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005333
5334 ret = -EINVAL;
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07005335 /* log attributes have to be sane */
5336 if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 ||
5337 !log->level || !log->ubuf)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005338 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005339 }
Daniel Borkmann1ad2f582017-05-25 01:05:05 +02005340
5341 env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT);
5342 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
David S. Millere07b98d2017-05-10 11:38:07 -07005343 env->strict_alignment = true;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005344
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07005345 if (env->prog->aux->offload) {
5346 ret = bpf_prog_offload_verifier_prep(env);
5347 if (ret)
5348 goto err_unlock;
5349 }
5350
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005351 ret = replace_map_fd_with_map_ptr(env);
5352 if (ret < 0)
5353 goto skip_full_check;
5354
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005355 env->explored_states = kcalloc(env->prog->len,
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005356 sizeof(struct bpf_verifier_state_list *),
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005357 GFP_USER);
5358 ret = -ENOMEM;
5359 if (!env->explored_states)
5360 goto skip_full_check;
5361
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08005362 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
5363
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005364 ret = check_cfg(env);
5365 if (ret < 0)
5366 goto skip_full_check;
5367
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005368 ret = do_check(env);
Craig Gallek8c01c4f2017-11-02 11:18:01 -04005369 if (env->cur_state) {
5370 free_verifier_state(env->cur_state, true);
5371 env->cur_state = NULL;
5372 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005373
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005374skip_full_check:
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005375 while (!pop_stack(env, NULL, NULL));
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005376 free_states(env);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005377
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005378 if (ret == 0)
Alexei Starovoitovc1311872017-11-22 16:42:05 -08005379 sanitize_dead_code(env);
5380
5381 if (ret == 0)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005382 /* program is valid, convert *(u32*)(ctx + off) accesses */
5383 ret = convert_ctx_accesses(env);
5384
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005385 if (ret == 0)
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005386 ret = fixup_bpf_calls(env);
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005387
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08005388 if (ret == 0)
5389 ret = fixup_call_args(env);
5390
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07005391 if (log->level && bpf_verifier_log_full(log))
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005392 ret = -ENOSPC;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07005393 if (log->level && !log->ubuf) {
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005394 ret = -EFAULT;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07005395 goto err_release_maps;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005396 }
5397
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005398 if (ret == 0 && env->used_map_cnt) {
5399 /* if program passed verifier, update used_maps in bpf_prog_info */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005400 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
5401 sizeof(env->used_maps[0]),
5402 GFP_KERNEL);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005403
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005404 if (!env->prog->aux->used_maps) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005405 ret = -ENOMEM;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07005406 goto err_release_maps;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005407 }
5408
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005409 memcpy(env->prog->aux->used_maps, env->used_maps,
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005410 sizeof(env->used_maps[0]) * env->used_map_cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005411 env->prog->aux->used_map_cnt = env->used_map_cnt;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005412
5413 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
5414 * bpf_ld_imm64 instructions
5415 */
5416 convert_pseudo_ld_imm64(env);
5417 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005418
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07005419err_release_maps:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005420 if (!env->prog->aux->used_maps)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005421 /* if we didn't copy map pointers into bpf_prog_info, release
5422 * them now. Otherwise free_bpf_prog_info() will release them.
5423 */
5424 release_maps(env);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005425 *prog = env->prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005426err_unlock:
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005427 mutex_unlock(&bpf_verifier_lock);
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005428 vfree(env->insn_aux_data);
5429err_free_env:
5430 kfree(env);
Alexei Starovoitov51580e72014-09-26 00:17:02 -07005431 return ret;
5432}