blob: 37e0affa515e8deff35c3f51e66ecaaa76661c94 [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>
Yonghong Songc195651e2018-04-28 22:28:08 -070025#include <linux/perf_event.h>
Alexei Starovoitov51580e72014-09-26 00:17:02 -070026
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -070027#include "disasm.h"
28
Jakub Kicinski00176a32017-10-16 16:40:54 -070029static const struct bpf_verifier_ops * const bpf_verifier_ops[] = {
30#define BPF_PROG_TYPE(_id, _name) \
31 [_id] = & _name ## _verifier_ops,
32#define BPF_MAP_TYPE(_id, _ops)
33#include <linux/bpf_types.h>
34#undef BPF_PROG_TYPE
35#undef BPF_MAP_TYPE
36};
37
Alexei Starovoitov51580e72014-09-26 00:17:02 -070038/* bpf_check() is a static code analyzer that walks eBPF program
39 * instruction by instruction and updates register/stack state.
40 * All paths of conditional branches are analyzed until 'bpf_exit' insn.
41 *
42 * The first pass is depth-first-search to check that the program is a DAG.
43 * It rejects the following programs:
44 * - larger than BPF_MAXINSNS insns
45 * - if loop is present (detected via back-edge)
46 * - unreachable insns exist (shouldn't be a forest. program = one function)
47 * - out of bounds or malformed jumps
48 * The second pass is all possible path descent from the 1st insn.
49 * Since it's analyzing all pathes through the program, the length of the
Gary Lineba38a92017-03-01 16:25:51 +080050 * analysis is limited to 64k insn, which may be hit even if total number of
Alexei Starovoitov51580e72014-09-26 00:17:02 -070051 * insn is less then 4K, but there are too many branches that change stack/regs.
52 * Number of 'branches to be analyzed' is limited to 1k
53 *
54 * On entry to each instruction, each register has a type, and the instruction
55 * changes the types of the registers depending on instruction semantics.
56 * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is
57 * copied to R1.
58 *
59 * All registers are 64-bit.
60 * R0 - return register
61 * R1-R5 argument passing registers
62 * R6-R9 callee saved registers
63 * R10 - frame pointer read-only
64 *
65 * At the start of BPF program the register R1 contains a pointer to bpf_context
66 * and has type PTR_TO_CTX.
67 *
68 * Verifier tracks arithmetic operations on pointers in case:
69 * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
70 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20),
71 * 1st insn copies R10 (which has FRAME_PTR) type into R1
72 * and 2nd arithmetic instruction is pattern matched to recognize
73 * that it wants to construct a pointer to some element within stack.
74 * So after 2nd insn, the register R1 has type PTR_TO_STACK
75 * (and -20 constant is saved for further stack bounds checking).
76 * Meaning that this reg is a pointer to stack plus known immediate constant.
77 *
Edward Creef1174f72017-08-07 15:26:19 +010078 * Most of the time the registers have SCALAR_VALUE type, which
Alexei Starovoitov51580e72014-09-26 00:17:02 -070079 * means the register has some value, but it's not a valid pointer.
Edward Creef1174f72017-08-07 15:26:19 +010080 * (like pointer plus pointer becomes SCALAR_VALUE type)
Alexei Starovoitov51580e72014-09-26 00:17:02 -070081 *
82 * When verifier sees load or store instructions the type of base register
Edward Creef1174f72017-08-07 15:26:19 +010083 * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK. These are three pointer
Alexei Starovoitov51580e72014-09-26 00:17:02 -070084 * types recognized by check_mem_access() function.
85 *
86 * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value'
87 * and the range of [ptr, ptr + map's value_size) is accessible.
88 *
89 * registers used to pass values to function calls are checked against
90 * function argument constraints.
91 *
92 * ARG_PTR_TO_MAP_KEY is one of such argument constraints.
93 * It means that the register type passed to this function must be
94 * PTR_TO_STACK and it will be used inside the function as
95 * 'pointer to map element key'
96 *
97 * For example the argument constraints for bpf_map_lookup_elem():
98 * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
99 * .arg1_type = ARG_CONST_MAP_PTR,
100 * .arg2_type = ARG_PTR_TO_MAP_KEY,
101 *
102 * ret_type says that this function returns 'pointer to map elem value or null'
103 * function expects 1st argument to be a const pointer to 'struct bpf_map' and
104 * 2nd argument should be a pointer to stack, which will be used inside
105 * the helper function as a pointer to map element key.
106 *
107 * On the kernel side the helper function looks like:
108 * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
109 * {
110 * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
111 * void *key = (void *) (unsigned long) r2;
112 * void *value;
113 *
114 * here kernel can access 'key' and 'map' pointers safely, knowing that
115 * [key, key + map->key_size) bytes are valid and were initialized on
116 * the stack of eBPF program.
117 * }
118 *
119 * Corresponding eBPF program may look like:
120 * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR
121 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK
122 * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP
123 * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
124 * here verifier looks at prototype of map_lookup_elem() and sees:
125 * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok,
126 * Now verifier knows that this map has key of R1->map_ptr->key_size bytes
127 *
128 * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far,
129 * Now verifier checks that [R2, R2 + map's key_size) are within stack limits
130 * and were initialized prior to this call.
131 * If it's ok, then verifier allows this BPF_CALL insn and looks at
132 * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets
133 * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function
134 * returns ether pointer to map value or NULL.
135 *
136 * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off'
137 * insn, the register holding that pointer in the true branch changes state to
138 * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false
139 * branch. See check_cond_jmp_op().
140 *
141 * After the call R0 is set to return type of the function and registers R1-R5
142 * are set to NOT_INIT to indicate that they are no longer readable.
143 */
144
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700145/* verifier_state + insn_idx are pushed to stack when branch is encountered */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100146struct bpf_verifier_stack_elem {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700147 /* verifer state is 'st'
148 * before processing instruction 'insn_idx'
149 * and after processing instruction 'prev_insn_idx'
150 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100151 struct bpf_verifier_state st;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700152 int insn_idx;
153 int prev_insn_idx;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100154 struct bpf_verifier_stack_elem *next;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700155};
156
Edward Cree8e17c1b2017-08-07 15:30:30 +0100157#define BPF_COMPLEXITY_LIMIT_INSNS 131072
Daniel Borkmann07016152016-04-05 22:33:17 +0200158#define BPF_COMPLEXITY_LIMIT_STACK 1024
159
Martin KaFai Laufad73a12017-03-22 10:00:32 -0700160#define BPF_MAP_PTR_POISON ((void *)0xeB9F + POISON_POINTER_DELTA)
161
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200162struct bpf_call_arg_meta {
163 struct bpf_map *map_ptr;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200164 bool raw_mode;
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200165 bool pkt_access;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200166 int regno;
167 int access_size;
Yonghong Song849fa502018-04-28 22:28:09 -0700168 s64 msize_smax_value;
169 u64 msize_umax_value;
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200170};
171
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700172static DEFINE_MUTEX(bpf_verifier_lock);
173
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700174void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt,
175 va_list args)
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700176{
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700177 unsigned int n;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700178
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700179 n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args);
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700180
181 WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1,
182 "verifier log line truncated - local buffer too short\n");
183
184 n = min(log->len_total - log->len_used - 1, n);
185 log->kbuf[n] = '\0';
186
187 if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1))
188 log->len_used += n;
189 else
190 log->ubuf = NULL;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700191}
Jiri Olsaabe08842018-03-23 11:41:28 +0100192
193/* log_level controls verbosity level of eBPF verifier.
194 * bpf_verifier_log_write() is used to dump the verification trace to the log,
195 * so the user can figure out what's wrong with the program
Quentin Monnet430e68d2018-01-10 12:26:06 +0000196 */
Jiri Olsaabe08842018-03-23 11:41:28 +0100197__printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env,
198 const char *fmt, ...)
199{
200 va_list args;
201
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700202 if (!bpf_verifier_log_needed(&env->log))
203 return;
204
Jiri Olsaabe08842018-03-23 11:41:28 +0100205 va_start(args, fmt);
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700206 bpf_verifier_vlog(&env->log, fmt, args);
Jiri Olsaabe08842018-03-23 11:41:28 +0100207 va_end(args);
208}
209EXPORT_SYMBOL_GPL(bpf_verifier_log_write);
210
211__printf(2, 3) static void verbose(void *private_data, const char *fmt, ...)
212{
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700213 struct bpf_verifier_env *env = private_data;
Jiri Olsaabe08842018-03-23 11:41:28 +0100214 va_list args;
215
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700216 if (!bpf_verifier_log_needed(&env->log))
217 return;
218
Jiri Olsaabe08842018-03-23 11:41:28 +0100219 va_start(args, fmt);
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700220 bpf_verifier_vlog(&env->log, fmt, args);
Jiri Olsaabe08842018-03-23 11:41:28 +0100221 va_end(args);
222}
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700223
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200224static bool type_is_pkt_pointer(enum bpf_reg_type type)
225{
226 return type == PTR_TO_PACKET ||
227 type == PTR_TO_PACKET_META;
228}
229
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700230/* string representation of 'enum bpf_reg_type' */
231static const char * const reg_type_str[] = {
232 [NOT_INIT] = "?",
Edward Creef1174f72017-08-07 15:26:19 +0100233 [SCALAR_VALUE] = "inv",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700234 [PTR_TO_CTX] = "ctx",
235 [CONST_PTR_TO_MAP] = "map_ptr",
236 [PTR_TO_MAP_VALUE] = "map_value",
237 [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700238 [PTR_TO_STACK] = "fp",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700239 [PTR_TO_PACKET] = "pkt",
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200240 [PTR_TO_PACKET_META] = "pkt_meta",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700241 [PTR_TO_PACKET_END] = "pkt_end",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700242};
243
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800244static void print_liveness(struct bpf_verifier_env *env,
245 enum bpf_reg_liveness live)
246{
247 if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN))
248 verbose(env, "_");
249 if (live & REG_LIVE_READ)
250 verbose(env, "r");
251 if (live & REG_LIVE_WRITTEN)
252 verbose(env, "w");
253}
254
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800255static struct bpf_func_state *func(struct bpf_verifier_env *env,
256 const struct bpf_reg_state *reg)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700257{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800258 struct bpf_verifier_state *cur = env->cur_state;
259
260 return cur->frame[reg->frameno];
261}
262
263static void print_verifier_state(struct bpf_verifier_env *env,
264 const struct bpf_func_state *state)
265{
266 const struct bpf_reg_state *reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700267 enum bpf_reg_type t;
268 int i;
269
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800270 if (state->frameno)
271 verbose(env, " frame%d:", state->frameno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700272 for (i = 0; i < MAX_BPF_REG; i++) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700273 reg = &state->regs[i];
274 t = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700275 if (t == NOT_INIT)
276 continue;
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800277 verbose(env, " R%d", i);
278 print_liveness(env, reg->live);
279 verbose(env, "=%s", reg_type_str[t]);
Edward Creef1174f72017-08-07 15:26:19 +0100280 if ((t == SCALAR_VALUE || t == PTR_TO_STACK) &&
281 tnum_is_const(reg->var_off)) {
282 /* reg->off should be 0 for SCALAR_VALUE */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700283 verbose(env, "%lld", reg->var_off.value + reg->off);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800284 if (t == PTR_TO_STACK)
285 verbose(env, ",call_%d", func(env, reg)->callsite);
Edward Creef1174f72017-08-07 15:26:19 +0100286 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700287 verbose(env, "(id=%d", reg->id);
Edward Creef1174f72017-08-07 15:26:19 +0100288 if (t != SCALAR_VALUE)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700289 verbose(env, ",off=%d", reg->off);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200290 if (type_is_pkt_pointer(t))
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700291 verbose(env, ",r=%d", reg->range);
Edward Creef1174f72017-08-07 15:26:19 +0100292 else if (t == CONST_PTR_TO_MAP ||
293 t == PTR_TO_MAP_VALUE ||
294 t == PTR_TO_MAP_VALUE_OR_NULL)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700295 verbose(env, ",ks=%d,vs=%d",
Edward Creef1174f72017-08-07 15:26:19 +0100296 reg->map_ptr->key_size,
297 reg->map_ptr->value_size);
Edward Cree7d1238f2017-08-07 15:26:56 +0100298 if (tnum_is_const(reg->var_off)) {
299 /* Typically an immediate SCALAR_VALUE, but
300 * could be a pointer whose offset is too big
301 * for reg->off
302 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700303 verbose(env, ",imm=%llx", reg->var_off.value);
Edward Cree7d1238f2017-08-07 15:26:56 +0100304 } else {
305 if (reg->smin_value != reg->umin_value &&
306 reg->smin_value != S64_MIN)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700307 verbose(env, ",smin_value=%lld",
Edward Cree7d1238f2017-08-07 15:26:56 +0100308 (long long)reg->smin_value);
309 if (reg->smax_value != reg->umax_value &&
310 reg->smax_value != S64_MAX)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700311 verbose(env, ",smax_value=%lld",
Edward Cree7d1238f2017-08-07 15:26:56 +0100312 (long long)reg->smax_value);
313 if (reg->umin_value != 0)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700314 verbose(env, ",umin_value=%llu",
Edward Cree7d1238f2017-08-07 15:26:56 +0100315 (unsigned long long)reg->umin_value);
316 if (reg->umax_value != U64_MAX)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700317 verbose(env, ",umax_value=%llu",
Edward Cree7d1238f2017-08-07 15:26:56 +0100318 (unsigned long long)reg->umax_value);
319 if (!tnum_is_unknown(reg->var_off)) {
320 char tn_buf[48];
Edward Creef1174f72017-08-07 15:26:19 +0100321
Edward Cree7d1238f2017-08-07 15:26:56 +0100322 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700323 verbose(env, ",var_off=%s", tn_buf);
Edward Cree7d1238f2017-08-07 15:26:56 +0100324 }
Edward Creef1174f72017-08-07 15:26:19 +0100325 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700326 verbose(env, ")");
Edward Creef1174f72017-08-07 15:26:19 +0100327 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700328 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700329 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800330 if (state->stack[i].slot_type[0] == STACK_SPILL) {
331 verbose(env, " fp%d",
332 (-i - 1) * BPF_REG_SIZE);
333 print_liveness(env, state->stack[i].spilled_ptr.live);
334 verbose(env, "=%s",
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700335 reg_type_str[state->stack[i].spilled_ptr.type]);
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800336 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800337 if (state->stack[i].slot_type[0] == STACK_ZERO)
338 verbose(env, " fp%d=0", (-i - 1) * BPF_REG_SIZE);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700339 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700340 verbose(env, "\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700341}
342
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800343static int copy_stack_state(struct bpf_func_state *dst,
344 const struct bpf_func_state *src)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700345{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700346 if (!src->stack)
347 return 0;
348 if (WARN_ON_ONCE(dst->allocated_stack < src->allocated_stack)) {
349 /* internal bug, make state invalid to reject the program */
350 memset(dst, 0, sizeof(*dst));
351 return -EFAULT;
352 }
353 memcpy(dst->stack, src->stack,
354 sizeof(*src->stack) * (src->allocated_stack / BPF_REG_SIZE));
355 return 0;
356}
357
358/* do_check() starts with zero-sized stack in struct bpf_verifier_state to
359 * make it consume minimal amount of memory. check_stack_write() access from
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800360 * the program calls into realloc_func_state() to grow the stack size.
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700361 * Note there is a non-zero 'parent' pointer inside bpf_verifier_state
362 * which this function copies over. It points to previous bpf_verifier_state
363 * which is never reallocated
364 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800365static int realloc_func_state(struct bpf_func_state *state, int size,
366 bool copy_old)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700367{
368 u32 old_size = state->allocated_stack;
369 struct bpf_stack_state *new_stack;
370 int slot = size / BPF_REG_SIZE;
371
372 if (size <= old_size || !size) {
373 if (copy_old)
374 return 0;
375 state->allocated_stack = slot * BPF_REG_SIZE;
376 if (!size && old_size) {
377 kfree(state->stack);
378 state->stack = NULL;
379 }
380 return 0;
381 }
382 new_stack = kmalloc_array(slot, sizeof(struct bpf_stack_state),
383 GFP_KERNEL);
384 if (!new_stack)
385 return -ENOMEM;
386 if (copy_old) {
387 if (state->stack)
388 memcpy(new_stack, state->stack,
389 sizeof(*new_stack) * (old_size / BPF_REG_SIZE));
390 memset(new_stack + old_size / BPF_REG_SIZE, 0,
391 sizeof(*new_stack) * (size - old_size) / BPF_REG_SIZE);
392 }
393 state->allocated_stack = slot * BPF_REG_SIZE;
394 kfree(state->stack);
395 state->stack = new_stack;
396 return 0;
397}
398
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800399static void free_func_state(struct bpf_func_state *state)
400{
Alexei Starovoitov58963512018-01-08 07:51:17 -0800401 if (!state)
402 return;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800403 kfree(state->stack);
404 kfree(state);
405}
406
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700407static void free_verifier_state(struct bpf_verifier_state *state,
408 bool free_self)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700409{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800410 int i;
411
412 for (i = 0; i <= state->curframe; i++) {
413 free_func_state(state->frame[i]);
414 state->frame[i] = NULL;
415 }
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700416 if (free_self)
417 kfree(state);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700418}
419
420/* copy verifier state from src to dst growing dst stack space
421 * when necessary to accommodate larger src stack
422 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800423static int copy_func_state(struct bpf_func_state *dst,
424 const struct bpf_func_state *src)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700425{
426 int err;
427
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800428 err = realloc_func_state(dst, src->allocated_stack, false);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700429 if (err)
430 return err;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800431 memcpy(dst, src, offsetof(struct bpf_func_state, allocated_stack));
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700432 return copy_stack_state(dst, src);
433}
434
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800435static int copy_verifier_state(struct bpf_verifier_state *dst_state,
436 const struct bpf_verifier_state *src)
437{
438 struct bpf_func_state *dst;
439 int i, err;
440
441 /* if dst has more stack frames then src frame, free them */
442 for (i = src->curframe + 1; i <= dst_state->curframe; i++) {
443 free_func_state(dst_state->frame[i]);
444 dst_state->frame[i] = NULL;
445 }
446 dst_state->curframe = src->curframe;
447 dst_state->parent = src->parent;
448 for (i = 0; i <= src->curframe; i++) {
449 dst = dst_state->frame[i];
450 if (!dst) {
451 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
452 if (!dst)
453 return -ENOMEM;
454 dst_state->frame[i] = dst;
455 }
456 err = copy_func_state(dst, src->frame[i]);
457 if (err)
458 return err;
459 }
460 return 0;
461}
462
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700463static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx,
464 int *insn_idx)
465{
466 struct bpf_verifier_state *cur = env->cur_state;
467 struct bpf_verifier_stack_elem *elem, *head = env->head;
468 int err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700469
470 if (env->head == NULL)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700471 return -ENOENT;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700472
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700473 if (cur) {
474 err = copy_verifier_state(cur, &head->st);
475 if (err)
476 return err;
477 }
478 if (insn_idx)
479 *insn_idx = head->insn_idx;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700480 if (prev_insn_idx)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700481 *prev_insn_idx = head->prev_insn_idx;
482 elem = head->next;
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700483 free_verifier_state(&head->st, false);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700484 kfree(head);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700485 env->head = elem;
486 env->stack_size--;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700487 return 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700488}
489
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100490static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,
491 int insn_idx, int prev_insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700492{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700493 struct bpf_verifier_state *cur = env->cur_state;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100494 struct bpf_verifier_stack_elem *elem;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700495 int err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700496
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700497 elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700498 if (!elem)
499 goto err;
500
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700501 elem->insn_idx = insn_idx;
502 elem->prev_insn_idx = prev_insn_idx;
503 elem->next = env->head;
504 env->head = elem;
505 env->stack_size++;
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700506 err = copy_verifier_state(&elem->st, cur);
507 if (err)
508 goto err;
Daniel Borkmann07016152016-04-05 22:33:17 +0200509 if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700510 verbose(env, "BPF program is too complex\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700511 goto err;
512 }
513 return &elem->st;
514err:
Alexei Starovoitov58963512018-01-08 07:51:17 -0800515 free_verifier_state(env->cur_state, true);
516 env->cur_state = NULL;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700517 /* pop all elements and return */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700518 while (!pop_stack(env, NULL, NULL));
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700519 return NULL;
520}
521
522#define CALLER_SAVED_REGS 6
523static const int caller_saved[CALLER_SAVED_REGS] = {
524 BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
525};
526
Edward Creef1174f72017-08-07 15:26:19 +0100527static void __mark_reg_not_init(struct bpf_reg_state *reg);
528
Edward Creeb03c9f92017-08-07 15:26:36 +0100529/* Mark the unknown part of a register (variable offset or scalar value) as
530 * known to have the value @imm.
531 */
532static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
533{
534 reg->id = 0;
535 reg->var_off = tnum_const(imm);
536 reg->smin_value = (s64)imm;
537 reg->smax_value = (s64)imm;
538 reg->umin_value = imm;
539 reg->umax_value = imm;
540}
541
Edward Creef1174f72017-08-07 15:26:19 +0100542/* Mark the 'variable offset' part of a register as zero. This should be
543 * used only on registers holding a pointer type.
544 */
545static void __mark_reg_known_zero(struct bpf_reg_state *reg)
546{
Edward Creeb03c9f92017-08-07 15:26:36 +0100547 __mark_reg_known(reg, 0);
Edward Creef1174f72017-08-07 15:26:19 +0100548}
549
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800550static void __mark_reg_const_zero(struct bpf_reg_state *reg)
551{
552 __mark_reg_known(reg, 0);
553 reg->off = 0;
554 reg->type = SCALAR_VALUE;
555}
556
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700557static void mark_reg_known_zero(struct bpf_verifier_env *env,
558 struct bpf_reg_state *regs, u32 regno)
Edward Creef1174f72017-08-07 15:26:19 +0100559{
560 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700561 verbose(env, "mark_reg_known_zero(regs, %u)\n", regno);
Edward Creef1174f72017-08-07 15:26:19 +0100562 /* Something bad happened, let's kill all regs */
563 for (regno = 0; regno < MAX_BPF_REG; regno++)
564 __mark_reg_not_init(regs + regno);
565 return;
566 }
567 __mark_reg_known_zero(regs + regno);
568}
569
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200570static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg)
571{
572 return type_is_pkt_pointer(reg->type);
573}
574
575static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg)
576{
577 return reg_is_pkt_pointer(reg) ||
578 reg->type == PTR_TO_PACKET_END;
579}
580
581/* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */
582static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg,
583 enum bpf_reg_type which)
584{
585 /* The register can already have a range from prior markings.
586 * This is fine as long as it hasn't been advanced from its
587 * origin.
588 */
589 return reg->type == which &&
590 reg->id == 0 &&
591 reg->off == 0 &&
592 tnum_equals_const(reg->var_off, 0);
593}
594
Edward Creeb03c9f92017-08-07 15:26:36 +0100595/* Attempts to improve min/max values based on var_off information */
596static void __update_reg_bounds(struct bpf_reg_state *reg)
597{
598 /* min signed is max(sign bit) | min(other bits) */
599 reg->smin_value = max_t(s64, reg->smin_value,
600 reg->var_off.value | (reg->var_off.mask & S64_MIN));
601 /* max signed is min(sign bit) | max(other bits) */
602 reg->smax_value = min_t(s64, reg->smax_value,
603 reg->var_off.value | (reg->var_off.mask & S64_MAX));
604 reg->umin_value = max(reg->umin_value, reg->var_off.value);
605 reg->umax_value = min(reg->umax_value,
606 reg->var_off.value | reg->var_off.mask);
607}
608
609/* Uses signed min/max values to inform unsigned, and vice-versa */
610static void __reg_deduce_bounds(struct bpf_reg_state *reg)
611{
612 /* Learn sign from signed bounds.
613 * If we cannot cross the sign boundary, then signed and unsigned bounds
614 * are the same, so combine. This works even in the negative case, e.g.
615 * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff.
616 */
617 if (reg->smin_value >= 0 || reg->smax_value < 0) {
618 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
619 reg->umin_value);
620 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
621 reg->umax_value);
622 return;
623 }
624 /* Learn sign from unsigned bounds. Signed bounds cross the sign
625 * boundary, so we must be careful.
626 */
627 if ((s64)reg->umax_value >= 0) {
628 /* Positive. We can't learn anything from the smin, but smax
629 * is positive, hence safe.
630 */
631 reg->smin_value = reg->umin_value;
632 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
633 reg->umax_value);
634 } else if ((s64)reg->umin_value < 0) {
635 /* Negative. We can't learn anything from the smax, but smin
636 * is negative, hence safe.
637 */
638 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
639 reg->umin_value);
640 reg->smax_value = reg->umax_value;
641 }
642}
643
644/* Attempts to improve var_off based on unsigned min/max information */
645static void __reg_bound_offset(struct bpf_reg_state *reg)
646{
647 reg->var_off = tnum_intersect(reg->var_off,
648 tnum_range(reg->umin_value,
649 reg->umax_value));
650}
651
652/* Reset the min/max bounds of a register */
653static void __mark_reg_unbounded(struct bpf_reg_state *reg)
654{
655 reg->smin_value = S64_MIN;
656 reg->smax_value = S64_MAX;
657 reg->umin_value = 0;
658 reg->umax_value = U64_MAX;
659}
660
Edward Creef1174f72017-08-07 15:26:19 +0100661/* Mark a register as having a completely unknown (scalar) value. */
662static void __mark_reg_unknown(struct bpf_reg_state *reg)
663{
664 reg->type = SCALAR_VALUE;
665 reg->id = 0;
666 reg->off = 0;
667 reg->var_off = tnum_unknown;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800668 reg->frameno = 0;
Edward Creeb03c9f92017-08-07 15:26:36 +0100669 __mark_reg_unbounded(reg);
Edward Creef1174f72017-08-07 15:26:19 +0100670}
671
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700672static void mark_reg_unknown(struct bpf_verifier_env *env,
673 struct bpf_reg_state *regs, u32 regno)
Edward Creef1174f72017-08-07 15:26:19 +0100674{
675 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700676 verbose(env, "mark_reg_unknown(regs, %u)\n", regno);
Alexei Starovoitov19ceb412017-11-30 21:31:37 -0800677 /* Something bad happened, let's kill all regs except FP */
678 for (regno = 0; regno < BPF_REG_FP; regno++)
Edward Creef1174f72017-08-07 15:26:19 +0100679 __mark_reg_not_init(regs + regno);
680 return;
681 }
682 __mark_reg_unknown(regs + regno);
683}
684
685static void __mark_reg_not_init(struct bpf_reg_state *reg)
686{
687 __mark_reg_unknown(reg);
688 reg->type = NOT_INIT;
689}
690
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700691static void mark_reg_not_init(struct bpf_verifier_env *env,
692 struct bpf_reg_state *regs, u32 regno)
Daniel Borkmanna9789ef2017-05-25 01:05:06 +0200693{
Edward Creef1174f72017-08-07 15:26:19 +0100694 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700695 verbose(env, "mark_reg_not_init(regs, %u)\n", regno);
Alexei Starovoitov19ceb412017-11-30 21:31:37 -0800696 /* Something bad happened, let's kill all regs except FP */
697 for (regno = 0; regno < BPF_REG_FP; regno++)
Edward Creef1174f72017-08-07 15:26:19 +0100698 __mark_reg_not_init(regs + regno);
699 return;
700 }
701 __mark_reg_not_init(regs + regno);
Daniel Borkmanna9789ef2017-05-25 01:05:06 +0200702}
703
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700704static void init_reg_state(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800705 struct bpf_func_state *state)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700706{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800707 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700708 int i;
709
Edward Creedc503a82017-08-15 20:34:35 +0100710 for (i = 0; i < MAX_BPF_REG; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700711 mark_reg_not_init(env, regs, i);
Edward Creedc503a82017-08-15 20:34:35 +0100712 regs[i].live = REG_LIVE_NONE;
713 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700714
715 /* frame pointer */
Edward Creef1174f72017-08-07 15:26:19 +0100716 regs[BPF_REG_FP].type = PTR_TO_STACK;
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700717 mark_reg_known_zero(env, regs, BPF_REG_FP);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800718 regs[BPF_REG_FP].frameno = state->frameno;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700719
720 /* 1st arg to a function */
721 regs[BPF_REG_1].type = PTR_TO_CTX;
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700722 mark_reg_known_zero(env, regs, BPF_REG_1);
Daniel Borkmann6760bf22016-12-18 01:52:59 +0100723}
724
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800725#define BPF_MAIN_FUNC (-1)
726static void init_func_state(struct bpf_verifier_env *env,
727 struct bpf_func_state *state,
728 int callsite, int frameno, int subprogno)
729{
730 state->callsite = callsite;
731 state->frameno = frameno;
732 state->subprogno = subprogno;
733 init_reg_state(env, state);
734}
735
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700736enum reg_arg_type {
737 SRC_OP, /* register is used as source operand */
738 DST_OP, /* register is used as destination operand */
739 DST_OP_NO_MARK /* same as above, check only, don't mark */
740};
741
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800742static int cmp_subprogs(const void *a, const void *b)
743{
Jiong Wang9c8105b2018-05-02 16:17:18 -0400744 return ((struct bpf_subprog_info *)a)->start -
745 ((struct bpf_subprog_info *)b)->start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800746}
747
748static int find_subprog(struct bpf_verifier_env *env, int off)
749{
Jiong Wang9c8105b2018-05-02 16:17:18 -0400750 struct bpf_subprog_info *p;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800751
Jiong Wang9c8105b2018-05-02 16:17:18 -0400752 p = bsearch(&off, env->subprog_info, env->subprog_cnt,
753 sizeof(env->subprog_info[0]), cmp_subprogs);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800754 if (!p)
755 return -ENOENT;
Jiong Wang9c8105b2018-05-02 16:17:18 -0400756 return p - env->subprog_info;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800757
758}
759
760static int add_subprog(struct bpf_verifier_env *env, int off)
761{
762 int insn_cnt = env->prog->len;
763 int ret;
764
765 if (off >= insn_cnt || off < 0) {
766 verbose(env, "call to invalid destination\n");
767 return -EINVAL;
768 }
769 ret = find_subprog(env, off);
770 if (ret >= 0)
771 return 0;
Jiong Wang4cb3d992018-05-02 16:17:19 -0400772 if (env->subprog_cnt >= BPF_MAX_SUBPROGS) {
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800773 verbose(env, "too many subprograms\n");
774 return -E2BIG;
775 }
Jiong Wang9c8105b2018-05-02 16:17:18 -0400776 env->subprog_info[env->subprog_cnt++].start = off;
777 sort(env->subprog_info, env->subprog_cnt,
778 sizeof(env->subprog_info[0]), cmp_subprogs, NULL);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800779 return 0;
780}
781
782static int check_subprogs(struct bpf_verifier_env *env)
783{
784 int i, ret, subprog_start, subprog_end, off, cur_subprog = 0;
Jiong Wang9c8105b2018-05-02 16:17:18 -0400785 struct bpf_subprog_info *subprog = env->subprog_info;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800786 struct bpf_insn *insn = env->prog->insnsi;
787 int insn_cnt = env->prog->len;
788
Jiong Wangf910cef2018-05-02 16:17:17 -0400789 /* Add entry function. */
790 ret = add_subprog(env, 0);
791 if (ret < 0)
792 return ret;
793
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800794 /* determine subprog starts. The end is one before the next starts */
795 for (i = 0; i < insn_cnt; i++) {
796 if (insn[i].code != (BPF_JMP | BPF_CALL))
797 continue;
798 if (insn[i].src_reg != BPF_PSEUDO_CALL)
799 continue;
800 if (!env->allow_ptr_leaks) {
801 verbose(env, "function calls to other bpf functions are allowed for root only\n");
802 return -EPERM;
803 }
804 if (bpf_prog_is_dev_bound(env->prog->aux)) {
Colin Ian Kinge90004d52017-12-18 14:03:12 +0000805 verbose(env, "function calls in offloaded programs are not supported yet\n");
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800806 return -EINVAL;
807 }
808 ret = add_subprog(env, i + insn[i].imm + 1);
809 if (ret < 0)
810 return ret;
811 }
812
Jiong Wang4cb3d992018-05-02 16:17:19 -0400813 /* Add a fake 'exit' subprog which could simplify subprog iteration
814 * logic. 'subprog_cnt' should not be increased.
815 */
816 subprog[env->subprog_cnt].start = insn_cnt;
817
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800818 if (env->log.level > 1)
819 for (i = 0; i < env->subprog_cnt; i++)
Jiong Wang9c8105b2018-05-02 16:17:18 -0400820 verbose(env, "func#%d @%d\n", i, subprog[i].start);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800821
822 /* now check that all jumps are within the same subprog */
Jiong Wang4cb3d992018-05-02 16:17:19 -0400823 subprog_start = subprog[cur_subprog].start;
824 subprog_end = subprog[cur_subprog + 1].start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800825 for (i = 0; i < insn_cnt; i++) {
826 u8 code = insn[i].code;
827
828 if (BPF_CLASS(code) != BPF_JMP)
829 goto next;
830 if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL)
831 goto next;
832 off = i + insn[i].off + 1;
833 if (off < subprog_start || off >= subprog_end) {
834 verbose(env, "jump out of range from insn %d to %d\n", i, off);
835 return -EINVAL;
836 }
837next:
838 if (i == subprog_end - 1) {
839 /* to avoid fall-through from one subprog into another
840 * the last insn of the subprog should be either exit
841 * or unconditional jump back
842 */
843 if (code != (BPF_JMP | BPF_EXIT) &&
844 code != (BPF_JMP | BPF_JA)) {
845 verbose(env, "last insn is not an exit or jmp\n");
846 return -EINVAL;
847 }
848 subprog_start = subprog_end;
Jiong Wang4cb3d992018-05-02 16:17:19 -0400849 cur_subprog++;
850 if (cur_subprog < env->subprog_cnt)
Jiong Wang9c8105b2018-05-02 16:17:18 -0400851 subprog_end = subprog[cur_subprog + 1].start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800852 }
853 }
854 return 0;
855}
856
Colin Ian Kingfa2d41a2017-12-18 17:47:07 +0000857static
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800858struct bpf_verifier_state *skip_callee(struct bpf_verifier_env *env,
859 const struct bpf_verifier_state *state,
860 struct bpf_verifier_state *parent,
861 u32 regno)
Edward Creedc503a82017-08-15 20:34:35 +0100862{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800863 struct bpf_verifier_state *tmp = NULL;
864
865 /* 'parent' could be a state of caller and
866 * 'state' could be a state of callee. In such case
867 * parent->curframe < state->curframe
868 * and it's ok for r1 - r5 registers
869 *
870 * 'parent' could be a callee's state after it bpf_exit-ed.
871 * In such case parent->curframe > state->curframe
872 * and it's ok for r0 only
873 */
874 if (parent->curframe == state->curframe ||
875 (parent->curframe < state->curframe &&
876 regno >= BPF_REG_1 && regno <= BPF_REG_5) ||
877 (parent->curframe > state->curframe &&
878 regno == BPF_REG_0))
879 return parent;
880
881 if (parent->curframe > state->curframe &&
882 regno >= BPF_REG_6) {
883 /* for callee saved regs we have to skip the whole chain
884 * of states that belong to callee and mark as LIVE_READ
885 * the registers before the call
886 */
887 tmp = parent;
888 while (tmp && tmp->curframe != state->curframe) {
889 tmp = tmp->parent;
890 }
891 if (!tmp)
892 goto bug;
893 parent = tmp;
894 } else {
895 goto bug;
896 }
897 return parent;
898bug:
899 verbose(env, "verifier bug regno %d tmp %p\n", regno, tmp);
900 verbose(env, "regno %d parent frame %d current frame %d\n",
901 regno, parent->curframe, state->curframe);
Colin Ian Kingfa2d41a2017-12-18 17:47:07 +0000902 return NULL;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800903}
904
905static int mark_reg_read(struct bpf_verifier_env *env,
906 const struct bpf_verifier_state *state,
907 struct bpf_verifier_state *parent,
908 u32 regno)
909{
910 bool writes = parent == state->parent; /* Observe write marks */
Edward Creedc503a82017-08-15 20:34:35 +0100911
Alexei Starovoitov8fe2d6c2017-10-05 16:20:56 -0700912 if (regno == BPF_REG_FP)
913 /* We don't need to worry about FP liveness because it's read-only */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800914 return 0;
Alexei Starovoitov8fe2d6c2017-10-05 16:20:56 -0700915
Edward Creedc503a82017-08-15 20:34:35 +0100916 while (parent) {
917 /* if read wasn't screened by an earlier write ... */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800918 if (writes && state->frame[state->curframe]->regs[regno].live & REG_LIVE_WRITTEN)
Edward Creedc503a82017-08-15 20:34:35 +0100919 break;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800920 parent = skip_callee(env, state, parent, regno);
921 if (!parent)
922 return -EFAULT;
Edward Creedc503a82017-08-15 20:34:35 +0100923 /* ... then we depend on parent's value */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800924 parent->frame[parent->curframe]->regs[regno].live |= REG_LIVE_READ;
Edward Creedc503a82017-08-15 20:34:35 +0100925 state = parent;
926 parent = state->parent;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800927 writes = true;
Edward Creedc503a82017-08-15 20:34:35 +0100928 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800929 return 0;
Edward Creedc503a82017-08-15 20:34:35 +0100930}
931
932static int check_reg_arg(struct bpf_verifier_env *env, u32 regno,
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700933 enum reg_arg_type t)
934{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800935 struct bpf_verifier_state *vstate = env->cur_state;
936 struct bpf_func_state *state = vstate->frame[vstate->curframe];
937 struct bpf_reg_state *regs = state->regs;
Edward Creedc503a82017-08-15 20:34:35 +0100938
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700939 if (regno >= MAX_BPF_REG) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700940 verbose(env, "R%d is invalid\n", regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700941 return -EINVAL;
942 }
943
944 if (t == SRC_OP) {
945 /* check whether register used as source operand can be read */
946 if (regs[regno].type == NOT_INIT) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700947 verbose(env, "R%d !read_ok\n", regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700948 return -EACCES;
949 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800950 return mark_reg_read(env, vstate, vstate->parent, regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700951 } else {
952 /* check whether register used as dest operand can be written to */
953 if (regno == BPF_REG_FP) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700954 verbose(env, "frame pointer is read only\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700955 return -EACCES;
956 }
Edward Creedc503a82017-08-15 20:34:35 +0100957 regs[regno].live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700958 if (t == DST_OP)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700959 mark_reg_unknown(env, regs, regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700960 }
961 return 0;
962}
963
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700964static bool is_spillable_regtype(enum bpf_reg_type type)
965{
966 switch (type) {
967 case PTR_TO_MAP_VALUE:
968 case PTR_TO_MAP_VALUE_OR_NULL:
969 case PTR_TO_STACK:
970 case PTR_TO_CTX:
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700971 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200972 case PTR_TO_PACKET_META:
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700973 case PTR_TO_PACKET_END:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700974 case CONST_PTR_TO_MAP:
975 return true;
976 default:
977 return false;
978 }
979}
980
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800981/* Does this register contain a constant zero? */
982static bool register_is_null(struct bpf_reg_state *reg)
983{
984 return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0);
985}
986
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700987/* check_stack_read/write functions track spill/fill of registers,
988 * stack boundary and alignment are checked in check_mem_access()
989 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700990static int check_stack_write(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800991 struct bpf_func_state *state, /* func where register points to */
992 int off, int size, int value_regno)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700993{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800994 struct bpf_func_state *cur; /* state of the current function */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700995 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800996 enum bpf_reg_type type;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700997
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800998 err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE),
999 true);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001000 if (err)
1001 return err;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001002 /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
1003 * so it's aligned access and [off, off + size) are within stack limits
1004 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001005 if (!env->allow_ptr_leaks &&
1006 state->stack[spi].slot_type[0] == STACK_SPILL &&
1007 size != BPF_REG_SIZE) {
1008 verbose(env, "attempt to corrupt spilled pointer on stack\n");
1009 return -EACCES;
1010 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001011
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001012 cur = env->cur_state->frame[env->cur_state->curframe];
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001013 if (value_regno >= 0 &&
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001014 is_spillable_regtype((type = cur->regs[value_regno].type))) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001015
1016 /* register containing pointer is being spilled into stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001017 if (size != BPF_REG_SIZE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001018 verbose(env, "invalid size of register spill\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001019 return -EACCES;
1020 }
1021
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001022 if (state != cur && type == PTR_TO_STACK) {
1023 verbose(env, "cannot spill pointers to stack into stack frame of the caller\n");
1024 return -EINVAL;
1025 }
1026
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001027 /* save register state */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001028 state->stack[spi].spilled_ptr = cur->regs[value_regno];
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001029 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001030
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001031 for (i = 0; i < BPF_REG_SIZE; i++)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001032 state->stack[spi].slot_type[i] = STACK_SPILL;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001033 } else {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001034 u8 type = STACK_MISC;
1035
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001036 /* regular write of data into stack */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001037 state->stack[spi].spilled_ptr = (struct bpf_reg_state) {};
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001038
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001039 /* only mark the slot as written if all 8 bytes were written
1040 * otherwise read propagation may incorrectly stop too soon
1041 * when stack slots are partially written.
1042 * This heuristic means that read propagation will be
1043 * conservative, since it will add reg_live_read marks
1044 * to stack slots all the way to first state when programs
1045 * writes+reads less than 8 bytes
1046 */
1047 if (size == BPF_REG_SIZE)
1048 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
1049
1050 /* when we zero initialize stack slots mark them as such */
1051 if (value_regno >= 0 &&
1052 register_is_null(&cur->regs[value_regno]))
1053 type = STACK_ZERO;
1054
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001055 for (i = 0; i < size; i++)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001056 state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] =
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001057 type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001058 }
1059 return 0;
1060}
1061
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001062/* registers of every function are unique and mark_reg_read() propagates
1063 * the liveness in the following cases:
1064 * - from callee into caller for R1 - R5 that were used as arguments
1065 * - from caller into callee for R0 that used as result of the call
1066 * - from caller to the same caller skipping states of the callee for R6 - R9,
1067 * since R6 - R9 are callee saved by implicit function prologue and
1068 * caller's R6 != callee's R6, so when we propagate liveness up to
1069 * parent states we need to skip callee states for R6 - R9.
1070 *
1071 * stack slot marking is different, since stacks of caller and callee are
1072 * accessible in both (since caller can pass a pointer to caller's stack to
1073 * callee which can pass it to another function), hence mark_stack_slot_read()
1074 * has to propagate the stack liveness to all parent states at given frame number.
1075 * Consider code:
1076 * f1() {
1077 * ptr = fp - 8;
1078 * *ptr = ctx;
1079 * call f2 {
1080 * .. = *ptr;
1081 * }
1082 * .. = *ptr;
1083 * }
1084 * First *ptr is reading from f1's stack and mark_stack_slot_read() has
1085 * to mark liveness at the f1's frame and not f2's frame.
1086 * Second *ptr is also reading from f1's stack and mark_stack_slot_read() has
1087 * to propagate liveness to f2 states at f1's frame level and further into
1088 * f1 states at f1's frame level until write into that stack slot
1089 */
1090static void mark_stack_slot_read(struct bpf_verifier_env *env,
1091 const struct bpf_verifier_state *state,
1092 struct bpf_verifier_state *parent,
1093 int slot, int frameno)
Edward Creedc503a82017-08-15 20:34:35 +01001094{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001095 bool writes = parent == state->parent; /* Observe write marks */
Edward Creedc503a82017-08-15 20:34:35 +01001096
1097 while (parent) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001098 if (parent->frame[frameno]->allocated_stack <= slot * BPF_REG_SIZE)
1099 /* since LIVE_WRITTEN mark is only done for full 8-byte
1100 * write the read marks are conservative and parent
1101 * state may not even have the stack allocated. In such case
1102 * end the propagation, since the loop reached beginning
1103 * of the function
1104 */
1105 break;
Edward Creedc503a82017-08-15 20:34:35 +01001106 /* if read wasn't screened by an earlier write ... */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001107 if (writes && state->frame[frameno]->stack[slot].spilled_ptr.live & REG_LIVE_WRITTEN)
Edward Creedc503a82017-08-15 20:34:35 +01001108 break;
1109 /* ... then we depend on parent's value */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001110 parent->frame[frameno]->stack[slot].spilled_ptr.live |= REG_LIVE_READ;
Edward Creedc503a82017-08-15 20:34:35 +01001111 state = parent;
1112 parent = state->parent;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001113 writes = true;
Edward Creedc503a82017-08-15 20:34:35 +01001114 }
1115}
1116
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001117static int check_stack_read(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001118 struct bpf_func_state *reg_state /* func where register points to */,
1119 int off, int size, int value_regno)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001120{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001121 struct bpf_verifier_state *vstate = env->cur_state;
1122 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001123 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE;
1124 u8 *stype;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001125
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001126 if (reg_state->allocated_stack <= slot) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001127 verbose(env, "invalid read from stack off %d+0 size %d\n",
1128 off, size);
1129 return -EACCES;
1130 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001131 stype = reg_state->stack[spi].slot_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001132
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001133 if (stype[0] == STACK_SPILL) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001134 if (size != BPF_REG_SIZE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001135 verbose(env, "invalid size of register spill\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001136 return -EACCES;
1137 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001138 for (i = 1; i < BPF_REG_SIZE; i++) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001139 if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001140 verbose(env, "corrupted spill memory\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001141 return -EACCES;
1142 }
1143 }
1144
Edward Creedc503a82017-08-15 20:34:35 +01001145 if (value_regno >= 0) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001146 /* restore register state from stack */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001147 state->regs[value_regno] = reg_state->stack[spi].spilled_ptr;
Alexei Starovoitov2f18f622017-11-30 21:31:38 -08001148 /* mark reg as written since spilled pointer state likely
1149 * has its liveness marks cleared by is_state_visited()
1150 * which resets stack/reg liveness for state transitions
1151 */
1152 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
Edward Creedc503a82017-08-15 20:34:35 +01001153 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001154 mark_stack_slot_read(env, vstate, vstate->parent, spi,
1155 reg_state->frameno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001156 return 0;
1157 } else {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001158 int zeros = 0;
1159
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001160 for (i = 0; i < size; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001161 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC)
1162 continue;
1163 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) {
1164 zeros++;
1165 continue;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001166 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001167 verbose(env, "invalid read from stack off %d+%d size %d\n",
1168 off, i, size);
1169 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001170 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001171 mark_stack_slot_read(env, vstate, vstate->parent, spi,
1172 reg_state->frameno);
1173 if (value_regno >= 0) {
1174 if (zeros == size) {
1175 /* any size read into register is zero extended,
1176 * so the whole register == const_zero
1177 */
1178 __mark_reg_const_zero(&state->regs[value_regno]);
1179 } else {
1180 /* have read misc data from the stack */
1181 mark_reg_unknown(env, state->regs, value_regno);
1182 }
1183 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
1184 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001185 return 0;
1186 }
1187}
1188
1189/* check read/write into map element returned by bpf_map_lookup_elem() */
Edward Creef1174f72017-08-07 15:26:19 +01001190static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001191 int size, bool zero_size_allowed)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001192{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001193 struct bpf_reg_state *regs = cur_regs(env);
1194 struct bpf_map *map = regs[regno].map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001195
Yonghong Song9fd29c02017-11-12 14:49:09 -08001196 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
1197 off + size > map->value_size) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001198 verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001199 map->value_size, off, size);
1200 return -EACCES;
1201 }
1202 return 0;
1203}
1204
Edward Creef1174f72017-08-07 15:26:19 +01001205/* check read/write into a map element with possible variable offset */
1206static int check_map_access(struct bpf_verifier_env *env, u32 regno,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001207 int off, int size, bool zero_size_allowed)
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001208{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001209 struct bpf_verifier_state *vstate = env->cur_state;
1210 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001211 struct bpf_reg_state *reg = &state->regs[regno];
1212 int err;
1213
Edward Creef1174f72017-08-07 15:26:19 +01001214 /* We may have adjusted the register to this map value, so we
1215 * need to try adding each of min_value and max_value to off
1216 * to make sure our theoretical access will be safe.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001217 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001218 if (env->log.level)
1219 print_verifier_state(env, state);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001220 /* The minimum value is only important with signed
1221 * comparisons where we can't assume the floor of a
1222 * value is 0. If we are using signed variables for our
1223 * index'es we need to make sure that whatever we use
1224 * will have a set floor within our range.
1225 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001226 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001227 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 -08001228 regno);
1229 return -EACCES;
1230 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001231 err = __check_map_access(env, regno, reg->smin_value + off, size,
1232 zero_size_allowed);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001233 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001234 verbose(env, "R%d min value is outside of the array range\n",
1235 regno);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001236 return err;
1237 }
1238
Edward Creeb03c9f92017-08-07 15:26:36 +01001239 /* If we haven't set a max value then we need to bail since we can't be
1240 * sure we won't do bad things.
1241 * If reg->umax_value + off could overflow, treat that as unbounded too.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001242 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001243 if (reg->umax_value >= BPF_MAX_VAR_OFF) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001244 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 -08001245 regno);
1246 return -EACCES;
1247 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001248 err = __check_map_access(env, regno, reg->umax_value + off, size,
1249 zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01001250 if (err)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001251 verbose(env, "R%d max value is outside of the array range\n",
1252 regno);
Edward Creef1174f72017-08-07 15:26:19 +01001253 return err;
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001254}
1255
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001256#define MAX_PACKET_OFF 0xffff
1257
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001258static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001259 const struct bpf_call_arg_meta *meta,
1260 enum bpf_access_type t)
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001261{
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001262 switch (env->prog->type) {
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001263 case BPF_PROG_TYPE_LWT_IN:
1264 case BPF_PROG_TYPE_LWT_OUT:
1265 /* dst_input() and dst_output() can't write for now */
1266 if (t == BPF_WRITE)
1267 return false;
Alexander Alemayhu7e57fbb2017-02-14 00:02:35 +01001268 /* fallthrough */
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001269 case BPF_PROG_TYPE_SCHED_CLS:
1270 case BPF_PROG_TYPE_SCHED_ACT:
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001271 case BPF_PROG_TYPE_XDP:
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001272 case BPF_PROG_TYPE_LWT_XMIT:
John Fastabend8a31db52017-08-15 22:33:09 -07001273 case BPF_PROG_TYPE_SK_SKB:
John Fastabend4f738ad2018-03-18 12:57:10 -07001274 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001275 if (meta)
1276 return meta->pkt_access;
1277
1278 env->seen_direct_write = true;
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001279 return true;
1280 default:
1281 return false;
1282 }
1283}
1284
Edward Creef1174f72017-08-07 15:26:19 +01001285static int __check_packet_access(struct bpf_verifier_env *env, u32 regno,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001286 int off, int size, bool zero_size_allowed)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001287{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001288 struct bpf_reg_state *regs = cur_regs(env);
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001289 struct bpf_reg_state *reg = &regs[regno];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001290
Yonghong Song9fd29c02017-11-12 14:49:09 -08001291 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
1292 (u64)off + size > reg->range) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001293 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 -07001294 off, size, regno, reg->id, reg->off, reg->range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001295 return -EACCES;
1296 }
1297 return 0;
1298}
1299
Edward Creef1174f72017-08-07 15:26:19 +01001300static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001301 int size, bool zero_size_allowed)
Edward Creef1174f72017-08-07 15:26:19 +01001302{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001303 struct bpf_reg_state *regs = cur_regs(env);
Edward Creef1174f72017-08-07 15:26:19 +01001304 struct bpf_reg_state *reg = &regs[regno];
1305 int err;
1306
1307 /* We may have added a variable offset to the packet pointer; but any
1308 * reg->range we have comes after that. We are only checking the fixed
1309 * offset.
1310 */
1311
1312 /* We don't allow negative numbers, because we aren't tracking enough
1313 * detail to prove they're safe.
1314 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001315 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001316 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 +01001317 regno);
1318 return -EACCES;
1319 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001320 err = __check_packet_access(env, regno, off, size, zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01001321 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001322 verbose(env, "R%d offset is outside of the packet\n", regno);
Edward Creef1174f72017-08-07 15:26:19 +01001323 return err;
1324 }
1325 return err;
1326}
1327
1328/* check access to 'struct bpf_context' fields. Supports fixed offsets only */
Yonghong Song31fd8582017-06-13 15:52:13 -07001329static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07001330 enum bpf_access_type t, enum bpf_reg_type *reg_type)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001331{
Daniel Borkmannf96da092017-07-02 02:13:27 +02001332 struct bpf_insn_access_aux info = {
1333 .reg_type = *reg_type,
1334 };
Yonghong Song31fd8582017-06-13 15:52:13 -07001335
Jakub Kicinski4f9218a2017-10-16 16:40:55 -07001336 if (env->ops->is_valid_access &&
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001337 env->ops->is_valid_access(off, size, t, env->prog, &info)) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02001338 /* A non zero info.ctx_field_size indicates that this field is a
1339 * candidate for later verifier transformation to load the whole
1340 * field and then apply a mask when accessed with a narrower
1341 * access than actual ctx access size. A zero info.ctx_field_size
1342 * will only allow for whole field access and rejects any other
1343 * type of narrower access.
Yonghong Song31fd8582017-06-13 15:52:13 -07001344 */
Yonghong Song23994632017-06-22 15:07:39 -07001345 *reg_type = info.reg_type;
Yonghong Song31fd8582017-06-13 15:52:13 -07001346
Jakub Kicinski4f9218a2017-10-16 16:40:55 -07001347 env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07001348 /* remember the offset of last byte accessed in ctx */
1349 if (env->prog->aux->max_ctx_offset < off + size)
1350 env->prog->aux->max_ctx_offset = off + size;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001351 return 0;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07001352 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001353
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001354 verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001355 return -EACCES;
1356}
1357
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001358static bool __is_pointer_value(bool allow_ptr_leaks,
1359 const struct bpf_reg_state *reg)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001360{
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001361 if (allow_ptr_leaks)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001362 return false;
1363
Edward Creef1174f72017-08-07 15:26:19 +01001364 return reg->type != SCALAR_VALUE;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001365}
1366
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001367static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
1368{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001369 return __is_pointer_value(env->allow_ptr_leaks, cur_regs(env) + regno);
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001370}
1371
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01001372static bool is_ctx_reg(struct bpf_verifier_env *env, int regno)
1373{
1374 const struct bpf_reg_state *reg = cur_regs(env) + regno;
1375
1376 return reg->type == PTR_TO_CTX;
1377}
1378
Daniel Borkmannca369602018-02-23 22:29:05 +01001379static bool is_pkt_reg(struct bpf_verifier_env *env, int regno)
1380{
1381 const struct bpf_reg_state *reg = cur_regs(env) + regno;
1382
1383 return type_is_pkt_pointer(reg->type);
1384}
1385
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001386static int check_pkt_ptr_alignment(struct bpf_verifier_env *env,
1387 const struct bpf_reg_state *reg,
David S. Millerd1174412017-05-10 11:22:52 -07001388 int off, int size, bool strict)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001389{
Edward Creef1174f72017-08-07 15:26:19 +01001390 struct tnum reg_off;
David S. Millere07b98d2017-05-10 11:38:07 -07001391 int ip_align;
David S. Millerd1174412017-05-10 11:22:52 -07001392
1393 /* Byte size accesses are always allowed. */
1394 if (!strict || size == 1)
1395 return 0;
1396
David S. Millere4eda882017-05-22 12:27:07 -04001397 /* For platforms that do not have a Kconfig enabling
1398 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of
1399 * NET_IP_ALIGN is universally set to '2'. And on platforms
1400 * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get
1401 * to this code only in strict mode where we want to emulate
1402 * the NET_IP_ALIGN==2 checking. Therefore use an
1403 * unconditional IP align value of '2'.
David S. Millere07b98d2017-05-10 11:38:07 -07001404 */
David S. Millere4eda882017-05-22 12:27:07 -04001405 ip_align = 2;
Edward Creef1174f72017-08-07 15:26:19 +01001406
1407 reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off));
1408 if (!tnum_is_aligned(reg_off, size)) {
1409 char tn_buf[48];
1410
1411 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001412 verbose(env,
1413 "misaligned packet access off %d+%s+%d+%d size %d\n",
Edward Creef1174f72017-08-07 15:26:19 +01001414 ip_align, tn_buf, reg->off, off, size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001415 return -EACCES;
1416 }
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001417
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001418 return 0;
1419}
1420
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001421static int check_generic_ptr_alignment(struct bpf_verifier_env *env,
1422 const struct bpf_reg_state *reg,
Edward Creef1174f72017-08-07 15:26:19 +01001423 const char *pointer_desc,
1424 int off, int size, bool strict)
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001425{
Edward Creef1174f72017-08-07 15:26:19 +01001426 struct tnum reg_off;
1427
1428 /* Byte size accesses are always allowed. */
1429 if (!strict || size == 1)
1430 return 0;
1431
1432 reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off));
1433 if (!tnum_is_aligned(reg_off, size)) {
1434 char tn_buf[48];
1435
1436 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001437 verbose(env, "misaligned %saccess off %s+%d+%d size %d\n",
Edward Creef1174f72017-08-07 15:26:19 +01001438 pointer_desc, tn_buf, reg->off, off, size);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001439 return -EACCES;
1440 }
1441
1442 return 0;
1443}
1444
David S. Millere07b98d2017-05-10 11:38:07 -07001445static int check_ptr_alignment(struct bpf_verifier_env *env,
Daniel Borkmannca369602018-02-23 22:29:05 +01001446 const struct bpf_reg_state *reg, int off,
1447 int size, bool strict_alignment_once)
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001448{
Daniel Borkmannca369602018-02-23 22:29:05 +01001449 bool strict = env->strict_alignment || strict_alignment_once;
Edward Creef1174f72017-08-07 15:26:19 +01001450 const char *pointer_desc = "";
David S. Millerd1174412017-05-10 11:22:52 -07001451
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001452 switch (reg->type) {
1453 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001454 case PTR_TO_PACKET_META:
1455 /* Special case, because of NET_IP_ALIGN. Given metadata sits
1456 * right in front, treat it the very same way.
1457 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001458 return check_pkt_ptr_alignment(env, reg, off, size, strict);
Edward Creef1174f72017-08-07 15:26:19 +01001459 case PTR_TO_MAP_VALUE:
1460 pointer_desc = "value ";
1461 break;
1462 case PTR_TO_CTX:
1463 pointer_desc = "context ";
1464 break;
1465 case PTR_TO_STACK:
1466 pointer_desc = "stack ";
Jann Horna5ec6ae2017-12-18 20:11:58 -08001467 /* The stack spill tracking logic in check_stack_write()
1468 * and check_stack_read() relies on stack accesses being
1469 * aligned.
1470 */
1471 strict = true;
Edward Creef1174f72017-08-07 15:26:19 +01001472 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001473 default:
Edward Creef1174f72017-08-07 15:26:19 +01001474 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001475 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001476 return check_generic_ptr_alignment(env, reg, pointer_desc, off, size,
1477 strict);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001478}
1479
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001480static int update_stack_depth(struct bpf_verifier_env *env,
1481 const struct bpf_func_state *func,
1482 int off)
1483{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001484 u16 stack = env->subprog_info[func->subprogno].stack_depth;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001485
1486 if (stack >= -off)
1487 return 0;
1488
1489 /* update known max for given subprogram */
Jiong Wang9c8105b2018-05-02 16:17:18 -04001490 env->subprog_info[func->subprogno].stack_depth = -off;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001491 return 0;
1492}
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001493
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001494/* starting from main bpf function walk all instructions of the function
1495 * and recursively walk all callees that given function can call.
1496 * Ignore jump and exit insns.
1497 * Since recursion is prevented by check_cfg() this algorithm
1498 * only needs a local stack of MAX_CALL_FRAMES to remember callsites
1499 */
1500static int check_max_stack_depth(struct bpf_verifier_env *env)
1501{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001502 int depth = 0, frame = 0, idx = 0, i = 0, subprog_end;
1503 struct bpf_subprog_info *subprog = env->subprog_info;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001504 struct bpf_insn *insn = env->prog->insnsi;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001505 int ret_insn[MAX_CALL_FRAMES];
1506 int ret_prog[MAX_CALL_FRAMES];
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001507
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001508process_func:
1509 /* round up to 32-bytes, since this is granularity
1510 * of interpreter stack size
1511 */
Jiong Wang9c8105b2018-05-02 16:17:18 -04001512 depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32);
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001513 if (depth > MAX_BPF_STACK) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001514 verbose(env, "combined stack size of %d calls is %d. Too large\n",
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001515 frame + 1, depth);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001516 return -EACCES;
1517 }
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001518continue_func:
Jiong Wang4cb3d992018-05-02 16:17:19 -04001519 subprog_end = subprog[idx + 1].start;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001520 for (; i < subprog_end; i++) {
1521 if (insn[i].code != (BPF_JMP | BPF_CALL))
1522 continue;
1523 if (insn[i].src_reg != BPF_PSEUDO_CALL)
1524 continue;
1525 /* remember insn and function to return to */
1526 ret_insn[frame] = i + 1;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001527 ret_prog[frame] = idx;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001528
1529 /* find the callee */
1530 i = i + insn[i].imm + 1;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001531 idx = find_subprog(env, i);
1532 if (idx < 0) {
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001533 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
1534 i);
1535 return -EFAULT;
1536 }
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001537 frame++;
1538 if (frame >= MAX_CALL_FRAMES) {
1539 WARN_ONCE(1, "verifier bug. Call stack is too deep\n");
1540 return -EFAULT;
1541 }
1542 goto process_func;
1543 }
1544 /* end of for() loop means the last insn of the 'subprog'
1545 * was reached. Doesn't matter whether it was JA or EXIT
1546 */
1547 if (frame == 0)
1548 return 0;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001549 depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32);
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001550 frame--;
1551 i = ret_insn[frame];
Jiong Wang9c8105b2018-05-02 16:17:18 -04001552 idx = ret_prog[frame];
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001553 goto continue_func;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001554}
1555
David S. Miller19d28fb2018-01-11 21:27:54 -05001556#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001557static int get_callee_stack_depth(struct bpf_verifier_env *env,
1558 const struct bpf_insn *insn, int idx)
1559{
1560 int start = idx + insn->imm + 1, subprog;
1561
1562 subprog = find_subprog(env, start);
1563 if (subprog < 0) {
1564 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
1565 start);
1566 return -EFAULT;
1567 }
Jiong Wang9c8105b2018-05-02 16:17:18 -04001568 return env->subprog_info[subprog].stack_depth;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001569}
David S. Miller19d28fb2018-01-11 21:27:54 -05001570#endif
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001571
Jann Horn0c17d1d2017-12-18 20:11:55 -08001572/* truncate register to smaller size (in bytes)
1573 * must be called with size < BPF_REG_SIZE
1574 */
1575static void coerce_reg_to_size(struct bpf_reg_state *reg, int size)
1576{
1577 u64 mask;
1578
1579 /* clear high bits in bit representation */
1580 reg->var_off = tnum_cast(reg->var_off, size);
1581
1582 /* fix arithmetic bounds */
1583 mask = ((u64)1 << (size * 8)) - 1;
1584 if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) {
1585 reg->umin_value &= mask;
1586 reg->umax_value &= mask;
1587 } else {
1588 reg->umin_value = 0;
1589 reg->umax_value = mask;
1590 }
1591 reg->smin_value = reg->umin_value;
1592 reg->smax_value = reg->umax_value;
1593}
1594
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001595/* check whether memory at (regno + off) is accessible for t = (read | write)
1596 * if t==write, value_regno is a register which value is stored into memory
1597 * if t==read, value_regno is a register which will receive the value from memory
1598 * if t==write && value_regno==-1, some unknown value is stored into memory
1599 * if t==read && value_regno==-1, don't care what we read from memory
1600 */
Daniel Borkmannca369602018-02-23 22:29:05 +01001601static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno,
1602 int off, int bpf_size, enum bpf_access_type t,
1603 int value_regno, bool strict_alignment_once)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001604{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001605 struct bpf_reg_state *regs = cur_regs(env);
1606 struct bpf_reg_state *reg = regs + regno;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001607 struct bpf_func_state *state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001608 int size, err = 0;
1609
1610 size = bpf_size_to_bytes(bpf_size);
1611 if (size < 0)
1612 return size;
1613
Edward Creef1174f72017-08-07 15:26:19 +01001614 /* alignment checks will add in reg->off themselves */
Daniel Borkmannca369602018-02-23 22:29:05 +01001615 err = check_ptr_alignment(env, reg, off, size, strict_alignment_once);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001616 if (err)
1617 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001618
Edward Creef1174f72017-08-07 15:26:19 +01001619 /* for access checks, reg->off is just part of off */
1620 off += reg->off;
1621
1622 if (reg->type == PTR_TO_MAP_VALUE) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001623 if (t == BPF_WRITE && value_regno >= 0 &&
1624 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001625 verbose(env, "R%d leaks addr into map\n", value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001626 return -EACCES;
1627 }
Josef Bacik48461132016-09-28 10:54:32 -04001628
Yonghong Song9fd29c02017-11-12 14:49:09 -08001629 err = check_map_access(env, regno, off, size, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001630 if (!err && t == BPF_READ && value_regno >= 0)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001631 mark_reg_unknown(env, regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001632
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07001633 } else if (reg->type == PTR_TO_CTX) {
Edward Creef1174f72017-08-07 15:26:19 +01001634 enum bpf_reg_type reg_type = SCALAR_VALUE;
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07001635
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001636 if (t == BPF_WRITE && value_regno >= 0 &&
1637 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001638 verbose(env, "R%d leaks addr into ctx\n", value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001639 return -EACCES;
1640 }
Edward Creef1174f72017-08-07 15:26:19 +01001641 /* ctx accesses must be at a fixed offset, so that we can
1642 * determine what type of data were returned.
1643 */
Jakub Kicinski28e33f92017-10-16 11:16:55 -07001644 if (reg->off) {
David S. Millerf8ddadc2017-10-22 13:36:53 +01001645 verbose(env,
1646 "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 -07001647 regno, reg->off, off - reg->off);
1648 return -EACCES;
1649 }
1650 if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
Edward Creef1174f72017-08-07 15:26:19 +01001651 char tn_buf[48];
1652
1653 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001654 verbose(env,
1655 "variable ctx access var_off=%s off=%d size=%d",
Edward Creef1174f72017-08-07 15:26:19 +01001656 tn_buf, off, size);
1657 return -EACCES;
1658 }
Yonghong Song31fd8582017-06-13 15:52:13 -07001659 err = check_ctx_access(env, insn_idx, off, size, t, &reg_type);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001660 if (!err && t == BPF_READ && value_regno >= 0) {
Edward Creef1174f72017-08-07 15:26:19 +01001661 /* ctx access returns either a scalar, or a
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001662 * PTR_TO_PACKET[_META,_END]. In the latter
1663 * case, we know the offset is zero.
Edward Creef1174f72017-08-07 15:26:19 +01001664 */
1665 if (reg_type == SCALAR_VALUE)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001666 mark_reg_unknown(env, regs, value_regno);
Edward Creef1174f72017-08-07 15:26:19 +01001667 else
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001668 mark_reg_known_zero(env, regs,
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001669 value_regno);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001670 regs[value_regno].id = 0;
1671 regs[value_regno].off = 0;
1672 regs[value_regno].range = 0;
1673 regs[value_regno].type = reg_type;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001674 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001675
Edward Creef1174f72017-08-07 15:26:19 +01001676 } else if (reg->type == PTR_TO_STACK) {
1677 /* stack accesses must be at a fixed offset, so that we can
1678 * determine what type of data were returned.
1679 * See check_stack_read().
1680 */
1681 if (!tnum_is_const(reg->var_off)) {
1682 char tn_buf[48];
1683
1684 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001685 verbose(env, "variable stack access var_off=%s off=%d size=%d",
Edward Creef1174f72017-08-07 15:26:19 +01001686 tn_buf, off, size);
1687 return -EACCES;
1688 }
1689 off += reg->var_off.value;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001690 if (off >= 0 || off < -MAX_BPF_STACK) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001691 verbose(env, "invalid stack off=%d size=%d\n", off,
1692 size);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001693 return -EACCES;
1694 }
Alexei Starovoitov87266792017-05-30 13:31:29 -07001695
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001696 state = func(env, reg);
1697 err = update_stack_depth(env, state, off);
1698 if (err)
1699 return err;
Alexei Starovoitov87266792017-05-30 13:31:29 -07001700
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001701 if (t == BPF_WRITE)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001702 err = check_stack_write(env, state, off, size,
1703 value_regno);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001704 else
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001705 err = check_stack_read(env, state, off, size,
1706 value_regno);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001707 } else if (reg_is_pkt_pointer(reg)) {
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001708 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001709 verbose(env, "cannot write into packet\n");
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001710 return -EACCES;
1711 }
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001712 if (t == BPF_WRITE && value_regno >= 0 &&
1713 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001714 verbose(env, "R%d leaks addr into packet\n",
1715 value_regno);
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001716 return -EACCES;
1717 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001718 err = check_packet_access(env, regno, off, size, false);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001719 if (!err && t == BPF_READ && value_regno >= 0)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001720 mark_reg_unknown(env, regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001721 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001722 verbose(env, "R%d invalid mem access '%s'\n", regno,
1723 reg_type_str[reg->type]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001724 return -EACCES;
1725 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001726
Edward Creef1174f72017-08-07 15:26:19 +01001727 if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ &&
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001728 regs[value_regno].type == SCALAR_VALUE) {
Edward Creef1174f72017-08-07 15:26:19 +01001729 /* b/h/w load zero-extends, mark upper bits as known 0 */
Jann Horn0c17d1d2017-12-18 20:11:55 -08001730 coerce_reg_to_size(&regs[value_regno], size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001731 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001732 return err;
1733}
1734
Yonghong Song31fd8582017-06-13 15:52:13 -07001735static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001736{
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001737 int err;
1738
1739 if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
1740 insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001741 verbose(env, "BPF_XADD uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001742 return -EINVAL;
1743 }
1744
1745 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01001746 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001747 if (err)
1748 return err;
1749
1750 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01001751 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001752 if (err)
1753 return err;
1754
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +02001755 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001756 verbose(env, "R%d leaks addr into mem\n", insn->src_reg);
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +02001757 return -EACCES;
1758 }
1759
Daniel Borkmannca369602018-02-23 22:29:05 +01001760 if (is_ctx_reg(env, insn->dst_reg) ||
1761 is_pkt_reg(env, insn->dst_reg)) {
1762 verbose(env, "BPF_XADD stores into R%d %s is not allowed\n",
1763 insn->dst_reg, is_ctx_reg(env, insn->dst_reg) ?
1764 "context" : "packet");
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01001765 return -EACCES;
1766 }
1767
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001768 /* check whether atomic_add can read the memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07001769 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Daniel Borkmannca369602018-02-23 22:29:05 +01001770 BPF_SIZE(insn->code), BPF_READ, -1, true);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001771 if (err)
1772 return err;
1773
1774 /* check whether atomic_add can write into the same memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07001775 return check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Daniel Borkmannca369602018-02-23 22:29:05 +01001776 BPF_SIZE(insn->code), BPF_WRITE, -1, true);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001777}
1778
1779/* when register 'regno' is passed into function that will read 'access_size'
1780 * bytes from that pointer, make sure that it's within stack boundary
Edward Creef1174f72017-08-07 15:26:19 +01001781 * and all elements of stack are initialized.
1782 * Unlike most pointer bounds-checking functions, this one doesn't take an
1783 * 'off' argument, so it has to add in reg->off itself.
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001784 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001785static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
Daniel Borkmann435faee12016-04-13 00:10:51 +02001786 int access_size, bool zero_size_allowed,
1787 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001788{
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001789 struct bpf_reg_state *reg = cur_regs(env) + regno;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001790 struct bpf_func_state *state = func(env, reg);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001791 int off, i, slot, spi;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001792
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001793 if (reg->type != PTR_TO_STACK) {
Edward Creef1174f72017-08-07 15:26:19 +01001794 /* Allow zero-byte read from NULL, regardless of pointer type */
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001795 if (zero_size_allowed && access_size == 0 &&
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001796 register_is_null(reg))
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001797 return 0;
1798
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001799 verbose(env, "R%d type=%s expected=%s\n", regno,
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001800 reg_type_str[reg->type],
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001801 reg_type_str[PTR_TO_STACK]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001802 return -EACCES;
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001803 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001804
Edward Creef1174f72017-08-07 15:26:19 +01001805 /* Only allow fixed-offset stack reads */
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001806 if (!tnum_is_const(reg->var_off)) {
Edward Creef1174f72017-08-07 15:26:19 +01001807 char tn_buf[48];
1808
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001809 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001810 verbose(env, "invalid variable stack read R%d var_off=%s\n",
Edward Creef1174f72017-08-07 15:26:19 +01001811 regno, tn_buf);
Jann Hornea25f912017-12-18 20:11:57 -08001812 return -EACCES;
Edward Creef1174f72017-08-07 15:26:19 +01001813 }
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001814 off = reg->off + reg->var_off.value;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001815 if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
Yonghong Song9fd29c02017-11-12 14:49:09 -08001816 access_size < 0 || (access_size == 0 && !zero_size_allowed)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001817 verbose(env, "invalid stack type R%d off=%d access_size=%d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001818 regno, off, access_size);
1819 return -EACCES;
1820 }
1821
Daniel Borkmann435faee12016-04-13 00:10:51 +02001822 if (meta && meta->raw_mode) {
1823 meta->access_size = access_size;
1824 meta->regno = regno;
1825 return 0;
1826 }
1827
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001828 for (i = 0; i < access_size; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001829 u8 *stype;
1830
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001831 slot = -(off + i) - 1;
1832 spi = slot / BPF_REG_SIZE;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001833 if (state->allocated_stack <= slot)
1834 goto err;
1835 stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE];
1836 if (*stype == STACK_MISC)
1837 goto mark;
1838 if (*stype == STACK_ZERO) {
1839 /* helper can write anything into the stack */
1840 *stype = STACK_MISC;
1841 goto mark;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001842 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001843err:
1844 verbose(env, "invalid indirect read from stack off %d+%d size %d\n",
1845 off, i, access_size);
1846 return -EACCES;
1847mark:
1848 /* reading any byte out of 8-byte 'spill_slot' will cause
1849 * the whole slot to be marked as 'read'
1850 */
1851 mark_stack_slot_read(env, env->cur_state, env->cur_state->parent,
1852 spi, state->frameno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001853 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001854 return update_stack_depth(env, state, off);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001855}
1856
Gianluca Borello06c1c042017-01-09 10:19:49 -08001857static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
1858 int access_size, bool zero_size_allowed,
1859 struct bpf_call_arg_meta *meta)
1860{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001861 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
Gianluca Borello06c1c042017-01-09 10:19:49 -08001862
Edward Creef1174f72017-08-07 15:26:19 +01001863 switch (reg->type) {
Gianluca Borello06c1c042017-01-09 10:19:49 -08001864 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001865 case PTR_TO_PACKET_META:
Yonghong Song9fd29c02017-11-12 14:49:09 -08001866 return check_packet_access(env, regno, reg->off, access_size,
1867 zero_size_allowed);
Gianluca Borello06c1c042017-01-09 10:19:49 -08001868 case PTR_TO_MAP_VALUE:
Yonghong Song9fd29c02017-11-12 14:49:09 -08001869 return check_map_access(env, regno, reg->off, access_size,
1870 zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01001871 default: /* scalar_value|ptr_to_stack or invalid ptr */
Gianluca Borello06c1c042017-01-09 10:19:49 -08001872 return check_stack_boundary(env, regno, access_size,
1873 zero_size_allowed, meta);
1874 }
1875}
1876
Daniel Borkmann90133412018-01-20 01:24:29 +01001877static bool arg_type_is_mem_ptr(enum bpf_arg_type type)
1878{
1879 return type == ARG_PTR_TO_MEM ||
1880 type == ARG_PTR_TO_MEM_OR_NULL ||
1881 type == ARG_PTR_TO_UNINIT_MEM;
1882}
1883
1884static bool arg_type_is_mem_size(enum bpf_arg_type type)
1885{
1886 return type == ARG_CONST_SIZE ||
1887 type == ARG_CONST_SIZE_OR_ZERO;
1888}
1889
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001890static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001891 enum bpf_arg_type arg_type,
1892 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001893{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001894 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001895 enum bpf_reg_type expected_type, type = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001896 int err = 0;
1897
Daniel Borkmann80f1d682015-03-12 17:21:42 +01001898 if (arg_type == ARG_DONTCARE)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001899 return 0;
1900
Edward Creedc503a82017-08-15 20:34:35 +01001901 err = check_reg_arg(env, regno, SRC_OP);
1902 if (err)
1903 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001904
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001905 if (arg_type == ARG_ANYTHING) {
1906 if (is_pointer_value(env, regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001907 verbose(env, "R%d leaks addr into helper function\n",
1908 regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001909 return -EACCES;
1910 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01001911 return 0;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001912 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01001913
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001914 if (type_is_pkt_pointer(type) &&
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001915 !may_access_direct_pkt_data(env, meta, BPF_READ)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001916 verbose(env, "helper access to the packet is not allowed\n");
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001917 return -EACCES;
1918 }
1919
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001920 if (arg_type == ARG_PTR_TO_MAP_KEY ||
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001921 arg_type == ARG_PTR_TO_MAP_VALUE) {
1922 expected_type = PTR_TO_STACK;
Paul Chaignond71962f2018-04-24 15:07:54 +02001923 if (!type_is_pkt_pointer(type) && type != PTR_TO_MAP_VALUE &&
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001924 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001925 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001926 } else if (arg_type == ARG_CONST_SIZE ||
1927 arg_type == ARG_CONST_SIZE_OR_ZERO) {
Edward Creef1174f72017-08-07 15:26:19 +01001928 expected_type = SCALAR_VALUE;
1929 if (type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001930 goto err_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001931 } else if (arg_type == ARG_CONST_MAP_PTR) {
1932 expected_type = CONST_PTR_TO_MAP;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001933 if (type != expected_type)
1934 goto err_type;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001935 } else if (arg_type == ARG_PTR_TO_CTX) {
1936 expected_type = PTR_TO_CTX;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001937 if (type != expected_type)
1938 goto err_type;
Daniel Borkmann90133412018-01-20 01:24:29 +01001939 } else if (arg_type_is_mem_ptr(arg_type)) {
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001940 expected_type = PTR_TO_STACK;
1941 /* One exception here. In case function allows for NULL to be
Edward Creef1174f72017-08-07 15:26:19 +01001942 * passed in as argument, it's a SCALAR_VALUE type. Final test
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01001943 * happens during stack boundary checking.
1944 */
Alexei Starovoitov914cb782017-11-30 21:31:40 -08001945 if (register_is_null(reg) &&
Gianluca Borellodb1ac492017-11-22 18:32:53 +00001946 arg_type == ARG_PTR_TO_MEM_OR_NULL)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001947 /* final test in check_stack_boundary() */;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001948 else if (!type_is_pkt_pointer(type) &&
1949 type != PTR_TO_MAP_VALUE &&
Edward Creef1174f72017-08-07 15:26:19 +01001950 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07001951 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001952 meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001953 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001954 verbose(env, "unsupported arg_type %d\n", arg_type);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001955 return -EFAULT;
1956 }
1957
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001958 if (arg_type == ARG_CONST_MAP_PTR) {
1959 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001960 meta->map_ptr = reg->map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001961 } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
1962 /* bpf_map_xxx(..., map_ptr, ..., key) call:
1963 * check that [key, key + map->key_size) are within
1964 * stack limits and initialized
1965 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001966 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001967 /* in function declaration map_ptr must come before
1968 * map_key, so that it's verified and known before
1969 * we have to check map_key here. Otherwise it means
1970 * that kernel subsystem misconfigured verifier
1971 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001972 verbose(env, "invalid map_ptr to access map->key\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001973 return -EACCES;
1974 }
Paul Chaignond71962f2018-04-24 15:07:54 +02001975 err = check_helper_mem_access(env, regno,
1976 meta->map_ptr->key_size, false,
1977 NULL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001978 } else if (arg_type == ARG_PTR_TO_MAP_VALUE) {
1979 /* bpf_map_xxx(..., map_ptr, ..., value) call:
1980 * check [value, value + map->value_size) validity
1981 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02001982 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001983 /* kernel subsystem misconfigured verifier */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001984 verbose(env, "invalid map_ptr to access map->value\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001985 return -EACCES;
1986 }
Paul Chaignond71962f2018-04-24 15:07:54 +02001987 err = check_helper_mem_access(env, regno,
1988 meta->map_ptr->value_size, false,
1989 NULL);
Daniel Borkmann90133412018-01-20 01:24:29 +01001990 } else if (arg_type_is_mem_size(arg_type)) {
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001991 bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001992
Yonghong Song849fa502018-04-28 22:28:09 -07001993 /* remember the mem_size which may be used later
1994 * to refine return values.
1995 */
1996 meta->msize_smax_value = reg->smax_value;
1997 meta->msize_umax_value = reg->umax_value;
1998
Edward Creef1174f72017-08-07 15:26:19 +01001999 /* The register is SCALAR_VALUE; the access check
2000 * happens using its boundaries.
Gianluca Borello06c1c042017-01-09 10:19:49 -08002001 */
Edward Creef1174f72017-08-07 15:26:19 +01002002 if (!tnum_is_const(reg->var_off))
Gianluca Borello06c1c042017-01-09 10:19:49 -08002003 /* For unprivileged variable accesses, disable raw
2004 * mode so that the program is required to
2005 * initialize all the memory that the helper could
2006 * just partially fill up.
2007 */
2008 meta = NULL;
2009
Edward Creeb03c9f92017-08-07 15:26:36 +01002010 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002011 verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n",
Edward Creef1174f72017-08-07 15:26:19 +01002012 regno);
2013 return -EACCES;
2014 }
Gianluca Borello06c1c042017-01-09 10:19:49 -08002015
Edward Creeb03c9f92017-08-07 15:26:36 +01002016 if (reg->umin_value == 0) {
Edward Creef1174f72017-08-07 15:26:19 +01002017 err = check_helper_mem_access(env, regno - 1, 0,
2018 zero_size_allowed,
2019 meta);
Gianluca Borello06c1c042017-01-09 10:19:49 -08002020 if (err)
2021 return err;
Gianluca Borello06c1c042017-01-09 10:19:49 -08002022 }
Edward Creef1174f72017-08-07 15:26:19 +01002023
Edward Creeb03c9f92017-08-07 15:26:36 +01002024 if (reg->umax_value >= BPF_MAX_VAR_SIZ) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002025 verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
Edward Creef1174f72017-08-07 15:26:19 +01002026 regno);
2027 return -EACCES;
2028 }
2029 err = check_helper_mem_access(env, regno - 1,
Edward Creeb03c9f92017-08-07 15:26:36 +01002030 reg->umax_value,
Edward Creef1174f72017-08-07 15:26:19 +01002031 zero_size_allowed, meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002032 }
2033
2034 return err;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002035err_type:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002036 verbose(env, "R%d type=%s expected=%s\n", regno,
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002037 reg_type_str[type], reg_type_str[expected_type]);
2038 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002039}
2040
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002041static int check_map_func_compatibility(struct bpf_verifier_env *env,
2042 struct bpf_map *map, int func_id)
Kaixu Xia35578d72015-08-06 07:02:35 +00002043{
Kaixu Xia35578d72015-08-06 07:02:35 +00002044 if (!map)
2045 return 0;
2046
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002047 /* We need a two way check, first is from map perspective ... */
2048 switch (map->map_type) {
2049 case BPF_MAP_TYPE_PROG_ARRAY:
2050 if (func_id != BPF_FUNC_tail_call)
2051 goto error;
2052 break;
2053 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
2054 if (func_id != BPF_FUNC_perf_event_read &&
Yonghong Song908432c2017-10-05 09:19:20 -07002055 func_id != BPF_FUNC_perf_event_output &&
2056 func_id != BPF_FUNC_perf_event_read_value)
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002057 goto error;
2058 break;
2059 case BPF_MAP_TYPE_STACK_TRACE:
2060 if (func_id != BPF_FUNC_get_stackid)
2061 goto error;
2062 break;
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -07002063 case BPF_MAP_TYPE_CGROUP_ARRAY:
David S. Miller60747ef2016-08-18 01:17:32 -04002064 if (func_id != BPF_FUNC_skb_under_cgroup &&
Sargun Dhillon60d20f92016-08-12 08:56:52 -07002065 func_id != BPF_FUNC_current_task_under_cgroup)
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002066 goto error;
2067 break;
John Fastabend546ac1f2017-07-17 09:28:56 -07002068 /* devmap returns a pointer to a live net_device ifindex that we cannot
2069 * allow to be modified from bpf side. So do not allow lookup elements
2070 * for now.
2071 */
2072 case BPF_MAP_TYPE_DEVMAP:
John Fastabend2ddf71e2017-07-17 09:30:02 -07002073 if (func_id != BPF_FUNC_redirect_map)
John Fastabend546ac1f2017-07-17 09:28:56 -07002074 goto error;
2075 break;
Björn Töpelfbfc504a2018-05-02 13:01:28 +02002076 /* Restrict bpf side of cpumap and xskmap, open when use-cases
2077 * appear.
2078 */
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02002079 case BPF_MAP_TYPE_CPUMAP:
Björn Töpelfbfc504a2018-05-02 13:01:28 +02002080 case BPF_MAP_TYPE_XSKMAP:
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02002081 if (func_id != BPF_FUNC_redirect_map)
2082 goto error;
2083 break;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07002084 case BPF_MAP_TYPE_ARRAY_OF_MAPS:
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07002085 case BPF_MAP_TYPE_HASH_OF_MAPS:
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07002086 if (func_id != BPF_FUNC_map_lookup_elem)
2087 goto error;
Martin KaFai Lau16a43622017-08-17 18:14:43 -07002088 break;
John Fastabend174a79f2017-08-15 22:32:47 -07002089 case BPF_MAP_TYPE_SOCKMAP:
2090 if (func_id != BPF_FUNC_sk_redirect_map &&
2091 func_id != BPF_FUNC_sock_map_update &&
John Fastabend4f738ad2018-03-18 12:57:10 -07002092 func_id != BPF_FUNC_map_delete_elem &&
2093 func_id != BPF_FUNC_msg_redirect_map)
John Fastabend174a79f2017-08-15 22:32:47 -07002094 goto error;
2095 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002096 default:
2097 break;
2098 }
2099
2100 /* ... and second from the function itself. */
2101 switch (func_id) {
2102 case BPF_FUNC_tail_call:
2103 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
2104 goto error;
Jiong Wangf910cef2018-05-02 16:17:17 -04002105 if (env->subprog_cnt > 1) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002106 verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n");
2107 return -EINVAL;
2108 }
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002109 break;
2110 case BPF_FUNC_perf_event_read:
2111 case BPF_FUNC_perf_event_output:
Yonghong Song908432c2017-10-05 09:19:20 -07002112 case BPF_FUNC_perf_event_read_value:
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002113 if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
2114 goto error;
2115 break;
2116 case BPF_FUNC_get_stackid:
2117 if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
2118 goto error;
2119 break;
Sargun Dhillon60d20f92016-08-12 08:56:52 -07002120 case BPF_FUNC_current_task_under_cgroup:
Daniel Borkmann747ea552016-08-12 22:17:17 +02002121 case BPF_FUNC_skb_under_cgroup:
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002122 if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
2123 goto error;
2124 break;
John Fastabend97f91a72017-07-17 09:29:18 -07002125 case BPF_FUNC_redirect_map:
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02002126 if (map->map_type != BPF_MAP_TYPE_DEVMAP &&
Björn Töpelfbfc504a2018-05-02 13:01:28 +02002127 map->map_type != BPF_MAP_TYPE_CPUMAP &&
2128 map->map_type != BPF_MAP_TYPE_XSKMAP)
John Fastabend97f91a72017-07-17 09:29:18 -07002129 goto error;
2130 break;
John Fastabend174a79f2017-08-15 22:32:47 -07002131 case BPF_FUNC_sk_redirect_map:
John Fastabend4f738ad2018-03-18 12:57:10 -07002132 case BPF_FUNC_msg_redirect_map:
John Fastabend174a79f2017-08-15 22:32:47 -07002133 if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
2134 goto error;
2135 break;
2136 case BPF_FUNC_sock_map_update:
2137 if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
2138 goto error;
2139 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002140 default:
2141 break;
Kaixu Xia35578d72015-08-06 07:02:35 +00002142 }
2143
2144 return 0;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002145error:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002146 verbose(env, "cannot pass map_type %d into func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02002147 map->map_type, func_id_name(func_id), func_id);
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002148 return -EINVAL;
Kaixu Xia35578d72015-08-06 07:02:35 +00002149}
2150
Daniel Borkmann90133412018-01-20 01:24:29 +01002151static bool check_raw_mode_ok(const struct bpf_func_proto *fn)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002152{
2153 int count = 0;
2154
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002155 if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002156 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002157 if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002158 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002159 if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002160 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002161 if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002162 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002163 if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002164 count++;
2165
Daniel Borkmann90133412018-01-20 01:24:29 +01002166 /* We only support one arg being in raw mode at the moment,
2167 * which is sufficient for the helper functions we have
2168 * right now.
2169 */
2170 return count <= 1;
2171}
2172
2173static bool check_args_pair_invalid(enum bpf_arg_type arg_curr,
2174 enum bpf_arg_type arg_next)
2175{
2176 return (arg_type_is_mem_ptr(arg_curr) &&
2177 !arg_type_is_mem_size(arg_next)) ||
2178 (!arg_type_is_mem_ptr(arg_curr) &&
2179 arg_type_is_mem_size(arg_next));
2180}
2181
2182static bool check_arg_pair_ok(const struct bpf_func_proto *fn)
2183{
2184 /* bpf_xxx(..., buf, len) call will access 'len'
2185 * bytes from memory 'buf'. Both arg types need
2186 * to be paired, so make sure there's no buggy
2187 * helper function specification.
2188 */
2189 if (arg_type_is_mem_size(fn->arg1_type) ||
2190 arg_type_is_mem_ptr(fn->arg5_type) ||
2191 check_args_pair_invalid(fn->arg1_type, fn->arg2_type) ||
2192 check_args_pair_invalid(fn->arg2_type, fn->arg3_type) ||
2193 check_args_pair_invalid(fn->arg3_type, fn->arg4_type) ||
2194 check_args_pair_invalid(fn->arg4_type, fn->arg5_type))
2195 return false;
2196
2197 return true;
2198}
2199
2200static int check_func_proto(const struct bpf_func_proto *fn)
2201{
2202 return check_raw_mode_ok(fn) &&
2203 check_arg_pair_ok(fn) ? 0 : -EINVAL;
Daniel Borkmann435faee12016-04-13 00:10:51 +02002204}
2205
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002206/* Packet data might have moved, any old PTR_TO_PACKET[_META,_END]
2207 * are now invalid, so turn them into unknown SCALAR_VALUE.
Edward Creef1174f72017-08-07 15:26:19 +01002208 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002209static void __clear_all_pkt_pointers(struct bpf_verifier_env *env,
2210 struct bpf_func_state *state)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002211{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002212 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002213 int i;
2214
2215 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002216 if (reg_is_pkt_pointer_any(&regs[i]))
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002217 mark_reg_unknown(env, regs, i);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002218
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002219 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
2220 if (state->stack[i].slot_type[0] != STACK_SPILL)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002221 continue;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002222 reg = &state->stack[i].spilled_ptr;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002223 if (reg_is_pkt_pointer_any(reg))
2224 __mark_reg_unknown(reg);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002225 }
2226}
2227
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002228static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
2229{
2230 struct bpf_verifier_state *vstate = env->cur_state;
2231 int i;
2232
2233 for (i = 0; i <= vstate->curframe; i++)
2234 __clear_all_pkt_pointers(env, vstate->frame[i]);
2235}
2236
2237static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
2238 int *insn_idx)
2239{
2240 struct bpf_verifier_state *state = env->cur_state;
2241 struct bpf_func_state *caller, *callee;
2242 int i, subprog, target_insn;
2243
Alexei Starovoitovaada9ce2017-12-25 13:15:42 -08002244 if (state->curframe + 1 >= MAX_CALL_FRAMES) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002245 verbose(env, "the call stack of %d frames is too deep\n",
Alexei Starovoitovaada9ce2017-12-25 13:15:42 -08002246 state->curframe + 2);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002247 return -E2BIG;
2248 }
2249
2250 target_insn = *insn_idx + insn->imm;
2251 subprog = find_subprog(env, target_insn + 1);
2252 if (subprog < 0) {
2253 verbose(env, "verifier bug. No program starts at insn %d\n",
2254 target_insn + 1);
2255 return -EFAULT;
2256 }
2257
2258 caller = state->frame[state->curframe];
2259 if (state->frame[state->curframe + 1]) {
2260 verbose(env, "verifier bug. Frame %d already allocated\n",
2261 state->curframe + 1);
2262 return -EFAULT;
2263 }
2264
2265 callee = kzalloc(sizeof(*callee), GFP_KERNEL);
2266 if (!callee)
2267 return -ENOMEM;
2268 state->frame[state->curframe + 1] = callee;
2269
2270 /* callee cannot access r0, r6 - r9 for reading and has to write
2271 * into its own stack before reading from it.
2272 * callee can read/write into caller's stack
2273 */
2274 init_func_state(env, callee,
2275 /* remember the callsite, it will be used by bpf_exit */
2276 *insn_idx /* callsite */,
2277 state->curframe + 1 /* frameno within this callchain */,
Jiong Wangf910cef2018-05-02 16:17:17 -04002278 subprog /* subprog number within this prog */);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002279
2280 /* copy r1 - r5 args that callee can access */
2281 for (i = BPF_REG_1; i <= BPF_REG_5; i++)
2282 callee->regs[i] = caller->regs[i];
2283
2284 /* after the call regsiters r0 - r5 were scratched */
2285 for (i = 0; i < CALLER_SAVED_REGS; i++) {
2286 mark_reg_not_init(env, caller->regs, caller_saved[i]);
2287 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
2288 }
2289
2290 /* only increment it after check_reg_arg() finished */
2291 state->curframe++;
2292
2293 /* and go analyze first insn of the callee */
2294 *insn_idx = target_insn;
2295
2296 if (env->log.level) {
2297 verbose(env, "caller:\n");
2298 print_verifier_state(env, caller);
2299 verbose(env, "callee:\n");
2300 print_verifier_state(env, callee);
2301 }
2302 return 0;
2303}
2304
2305static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
2306{
2307 struct bpf_verifier_state *state = env->cur_state;
2308 struct bpf_func_state *caller, *callee;
2309 struct bpf_reg_state *r0;
2310
2311 callee = state->frame[state->curframe];
2312 r0 = &callee->regs[BPF_REG_0];
2313 if (r0->type == PTR_TO_STACK) {
2314 /* technically it's ok to return caller's stack pointer
2315 * (or caller's caller's pointer) back to the caller,
2316 * since these pointers are valid. Only current stack
2317 * pointer will be invalid as soon as function exits,
2318 * but let's be conservative
2319 */
2320 verbose(env, "cannot return stack pointer to the caller\n");
2321 return -EINVAL;
2322 }
2323
2324 state->curframe--;
2325 caller = state->frame[state->curframe];
2326 /* return to the caller whatever r0 had in the callee */
2327 caller->regs[BPF_REG_0] = *r0;
2328
2329 *insn_idx = callee->callsite + 1;
2330 if (env->log.level) {
2331 verbose(env, "returning from callee:\n");
2332 print_verifier_state(env, callee);
2333 verbose(env, "to caller at %d:\n", *insn_idx);
2334 print_verifier_state(env, caller);
2335 }
2336 /* clear everything in the callee */
2337 free_func_state(callee);
2338 state->frame[state->curframe + 1] = NULL;
2339 return 0;
2340}
2341
Yonghong Song849fa502018-04-28 22:28:09 -07002342static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type,
2343 int func_id,
2344 struct bpf_call_arg_meta *meta)
2345{
2346 struct bpf_reg_state *ret_reg = &regs[BPF_REG_0];
2347
2348 if (ret_type != RET_INTEGER ||
2349 (func_id != BPF_FUNC_get_stack &&
2350 func_id != BPF_FUNC_probe_read_str))
2351 return;
2352
2353 ret_reg->smax_value = meta->msize_smax_value;
2354 ret_reg->umax_value = meta->msize_umax_value;
2355 __reg_deduce_bounds(ret_reg);
2356 __reg_bound_offset(ret_reg);
2357}
2358
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002359static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002360{
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002361 const struct bpf_func_proto *fn = NULL;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002362 struct bpf_reg_state *regs;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002363 struct bpf_call_arg_meta meta;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002364 bool changes_data;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002365 int i, err;
2366
2367 /* find function prototype */
2368 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002369 verbose(env, "invalid func %s#%d\n", func_id_name(func_id),
2370 func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002371 return -EINVAL;
2372 }
2373
Jakub Kicinski00176a32017-10-16 16:40:54 -07002374 if (env->ops->get_func_proto)
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002375 fn = env->ops->get_func_proto(func_id, env->prog);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002376 if (!fn) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002377 verbose(env, "unknown func %s#%d\n", func_id_name(func_id),
2378 func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002379 return -EINVAL;
2380 }
2381
2382 /* eBPF programs must be GPL compatible to use GPL-ed functions */
Daniel Borkmann24701ec2015-03-01 12:31:47 +01002383 if (!env->prog->gpl_compatible && fn->gpl_only) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002384 verbose(env, "cannot call GPL only function from proprietary program\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002385 return -EINVAL;
2386 }
2387
Daniel Borkmann04514d12017-12-14 21:07:25 +01002388 /* With LD_ABS/IND some JITs save/restore skb from r1. */
Martin KaFai Lau17bedab2016-12-07 15:53:11 -08002389 changes_data = bpf_helper_changes_pkt_data(fn->func);
Daniel Borkmann04514d12017-12-14 21:07:25 +01002390 if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) {
2391 verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n",
2392 func_id_name(func_id), func_id);
2393 return -EINVAL;
2394 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002395
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002396 memset(&meta, 0, sizeof(meta));
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002397 meta.pkt_access = fn->pkt_access;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002398
Daniel Borkmann90133412018-01-20 01:24:29 +01002399 err = check_func_proto(fn);
Daniel Borkmann435faee12016-04-13 00:10:51 +02002400 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002401 verbose(env, "kernel subsystem misconfigured func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02002402 func_id_name(func_id), func_id);
Daniel Borkmann435faee12016-04-13 00:10:51 +02002403 return err;
2404 }
2405
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002406 /* check args */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002407 err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002408 if (err)
2409 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002410 err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002411 if (err)
2412 return err;
Alexei Starovoitovb2157392018-01-07 17:33:02 -08002413 if (func_id == BPF_FUNC_tail_call) {
2414 if (meta.map_ptr == NULL) {
2415 verbose(env, "verifier bug\n");
2416 return -EINVAL;
2417 }
2418 env->insn_aux_data[insn_idx].map_ptr = meta.map_ptr;
2419 }
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002420 err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002421 if (err)
2422 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002423 err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002424 if (err)
2425 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002426 err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002427 if (err)
2428 return err;
2429
Daniel Borkmann435faee12016-04-13 00:10:51 +02002430 /* Mark slots with STACK_MISC in case of raw mode, stack offset
2431 * is inferred from register state.
2432 */
2433 for (i = 0; i < meta.access_size; i++) {
Daniel Borkmannca369602018-02-23 22:29:05 +01002434 err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B,
2435 BPF_WRITE, -1, false);
Daniel Borkmann435faee12016-04-13 00:10:51 +02002436 if (err)
2437 return err;
2438 }
2439
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002440 regs = cur_regs(env);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002441 /* reset caller saved regs */
Edward Creedc503a82017-08-15 20:34:35 +01002442 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002443 mark_reg_not_init(env, regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01002444 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
2445 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002446
Edward Creedc503a82017-08-15 20:34:35 +01002447 /* update return register (already marked as written above) */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002448 if (fn->ret_type == RET_INTEGER) {
Edward Creef1174f72017-08-07 15:26:19 +01002449 /* sets type to SCALAR_VALUE */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002450 mark_reg_unknown(env, regs, BPF_REG_0);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002451 } else if (fn->ret_type == RET_VOID) {
2452 regs[BPF_REG_0].type = NOT_INIT;
2453 } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) {
Martin KaFai Laufad73a12017-03-22 10:00:32 -07002454 struct bpf_insn_aux_data *insn_aux;
2455
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002456 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
Edward Creef1174f72017-08-07 15:26:19 +01002457 /* There is no offset yet applied, variable or fixed */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002458 mark_reg_known_zero(env, regs, BPF_REG_0);
Edward Creef1174f72017-08-07 15:26:19 +01002459 regs[BPF_REG_0].off = 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002460 /* remember map_ptr, so that check_map_access()
2461 * can check 'value_size' boundary of memory access
2462 * to map element returned from bpf_map_lookup_elem()
2463 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002464 if (meta.map_ptr == NULL) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002465 verbose(env,
2466 "kernel subsystem misconfigured verifier\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002467 return -EINVAL;
2468 }
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002469 regs[BPF_REG_0].map_ptr = meta.map_ptr;
Thomas Graf57a09bf2016-10-18 19:51:19 +02002470 regs[BPF_REG_0].id = ++env->id_gen;
Martin KaFai Laufad73a12017-03-22 10:00:32 -07002471 insn_aux = &env->insn_aux_data[insn_idx];
2472 if (!insn_aux->map_ptr)
2473 insn_aux->map_ptr = meta.map_ptr;
2474 else if (insn_aux->map_ptr != meta.map_ptr)
2475 insn_aux->map_ptr = BPF_MAP_PTR_POISON;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002476 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002477 verbose(env, "unknown return type %d of func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02002478 fn->ret_type, func_id_name(func_id), func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002479 return -EINVAL;
2480 }
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07002481
Yonghong Song849fa502018-04-28 22:28:09 -07002482 do_refine_retval_range(regs, fn->ret_type, func_id, &meta);
2483
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002484 err = check_map_func_compatibility(env, meta.map_ptr, func_id);
Kaixu Xia35578d72015-08-06 07:02:35 +00002485 if (err)
2486 return err;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07002487
Yonghong Songc195651e2018-04-28 22:28:08 -07002488 if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) {
2489 const char *err_str;
2490
2491#ifdef CONFIG_PERF_EVENTS
2492 err = get_callchain_buffers(sysctl_perf_event_max_stack);
2493 err_str = "cannot get callchain buffer for func %s#%d\n";
2494#else
2495 err = -ENOTSUPP;
2496 err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n";
2497#endif
2498 if (err) {
2499 verbose(env, err_str, func_id_name(func_id), func_id);
2500 return err;
2501 }
2502
2503 env->prog->has_callchain_buf = true;
2504 }
2505
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002506 if (changes_data)
2507 clear_all_pkt_pointers(env);
2508 return 0;
2509}
2510
Edward Creeb03c9f92017-08-07 15:26:36 +01002511static bool signed_add_overflows(s64 a, s64 b)
2512{
2513 /* Do the add in u64, where overflow is well-defined */
2514 s64 res = (s64)((u64)a + (u64)b);
2515
2516 if (b < 0)
2517 return res > a;
2518 return res < a;
2519}
2520
2521static bool signed_sub_overflows(s64 a, s64 b)
2522{
2523 /* Do the sub in u64, where overflow is well-defined */
2524 s64 res = (s64)((u64)a - (u64)b);
2525
2526 if (b < 0)
2527 return res < a;
2528 return res > a;
David S. Millerd1174412017-05-10 11:22:52 -07002529}
2530
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08002531static bool check_reg_sane_offset(struct bpf_verifier_env *env,
2532 const struct bpf_reg_state *reg,
2533 enum bpf_reg_type type)
2534{
2535 bool known = tnum_is_const(reg->var_off);
2536 s64 val = reg->var_off.value;
2537 s64 smin = reg->smin_value;
2538
2539 if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) {
2540 verbose(env, "math between %s pointer and %lld is not allowed\n",
2541 reg_type_str[type], val);
2542 return false;
2543 }
2544
2545 if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) {
2546 verbose(env, "%s pointer offset %d is not allowed\n",
2547 reg_type_str[type], reg->off);
2548 return false;
2549 }
2550
2551 if (smin == S64_MIN) {
2552 verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n",
2553 reg_type_str[type]);
2554 return false;
2555 }
2556
2557 if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) {
2558 verbose(env, "value %lld makes %s pointer be out of bounds\n",
2559 smin, reg_type_str[type]);
2560 return false;
2561 }
2562
2563 return true;
2564}
2565
Edward Creef1174f72017-08-07 15:26:19 +01002566/* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off.
Edward Creef1174f72017-08-07 15:26:19 +01002567 * Caller should also handle BPF_MOV case separately.
2568 * If we return -EACCES, caller may want to try again treating pointer as a
2569 * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks.
2570 */
2571static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
2572 struct bpf_insn *insn,
2573 const struct bpf_reg_state *ptr_reg,
2574 const struct bpf_reg_state *off_reg)
Josef Bacik48461132016-09-28 10:54:32 -04002575{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002576 struct bpf_verifier_state *vstate = env->cur_state;
2577 struct bpf_func_state *state = vstate->frame[vstate->curframe];
2578 struct bpf_reg_state *regs = state->regs, *dst_reg;
Edward Creef1174f72017-08-07 15:26:19 +01002579 bool known = tnum_is_const(off_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01002580 s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value,
2581 smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
2582 u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value,
2583 umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value;
Josef Bacik48461132016-09-28 10:54:32 -04002584 u8 opcode = BPF_OP(insn->code);
Edward Creef1174f72017-08-07 15:26:19 +01002585 u32 dst = insn->dst_reg;
Josef Bacik48461132016-09-28 10:54:32 -04002586
Edward Creef1174f72017-08-07 15:26:19 +01002587 dst_reg = &regs[dst];
Josef Bacik48461132016-09-28 10:54:32 -04002588
Daniel Borkmann6f161012018-01-18 01:15:21 +01002589 if ((known && (smin_val != smax_val || umin_val != umax_val)) ||
2590 smin_val > smax_val || umin_val > umax_val) {
2591 /* Taint dst register if offset had invalid bounds derived from
2592 * e.g. dead branches.
2593 */
2594 __mark_reg_unknown(dst_reg);
2595 return 0;
Josef Bacik48461132016-09-28 10:54:32 -04002596 }
2597
Edward Creef1174f72017-08-07 15:26:19 +01002598 if (BPF_CLASS(insn->code) != BPF_ALU64) {
2599 /* 32-bit ALU ops on pointers produce (meaningless) scalars */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08002600 verbose(env,
2601 "R%d 32-bit pointer arithmetic prohibited\n",
2602 dst);
Edward Creef1174f72017-08-07 15:26:19 +01002603 return -EACCES;
2604 }
David S. Millerd1174412017-05-10 11:22:52 -07002605
Edward Creef1174f72017-08-07 15:26:19 +01002606 if (ptr_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08002607 verbose(env, "R%d pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL prohibited, null-check it first\n",
2608 dst);
Edward Creef1174f72017-08-07 15:26:19 +01002609 return -EACCES;
2610 }
2611 if (ptr_reg->type == CONST_PTR_TO_MAP) {
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08002612 verbose(env, "R%d pointer arithmetic on CONST_PTR_TO_MAP prohibited\n",
2613 dst);
Edward Creef1174f72017-08-07 15:26:19 +01002614 return -EACCES;
2615 }
2616 if (ptr_reg->type == PTR_TO_PACKET_END) {
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08002617 verbose(env, "R%d pointer arithmetic on PTR_TO_PACKET_END prohibited\n",
2618 dst);
Edward Creef1174f72017-08-07 15:26:19 +01002619 return -EACCES;
2620 }
2621
2622 /* In case of 'scalar += pointer', dst_reg inherits pointer type and id.
2623 * The id may be overwritten later if we create a new variable offset.
Josef Bacik48461132016-09-28 10:54:32 -04002624 */
Edward Creef1174f72017-08-07 15:26:19 +01002625 dst_reg->type = ptr_reg->type;
2626 dst_reg->id = ptr_reg->id;
Josef Bacikf23cc642016-11-14 15:45:36 -05002627
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08002628 if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) ||
2629 !check_reg_sane_offset(env, ptr_reg, ptr_reg->type))
2630 return -EINVAL;
2631
Josef Bacik48461132016-09-28 10:54:32 -04002632 switch (opcode) {
2633 case BPF_ADD:
Edward Creef1174f72017-08-07 15:26:19 +01002634 /* We can take a fixed offset as long as it doesn't overflow
2635 * the s32 'off' field
2636 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002637 if (known && (ptr_reg->off + smin_val ==
2638 (s64)(s32)(ptr_reg->off + smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01002639 /* pointer += K. Accumulate it into fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01002640 dst_reg->smin_value = smin_ptr;
2641 dst_reg->smax_value = smax_ptr;
2642 dst_reg->umin_value = umin_ptr;
2643 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01002644 dst_reg->var_off = ptr_reg->var_off;
Edward Creeb03c9f92017-08-07 15:26:36 +01002645 dst_reg->off = ptr_reg->off + smin_val;
Edward Creef1174f72017-08-07 15:26:19 +01002646 dst_reg->range = ptr_reg->range;
2647 break;
2648 }
Edward Creef1174f72017-08-07 15:26:19 +01002649 /* A new variable offset is created. Note that off_reg->off
2650 * == 0, since it's a scalar.
2651 * dst_reg gets the pointer type and since some positive
2652 * integer value was added to the pointer, give it a new 'id'
2653 * if it's a PTR_TO_PACKET.
2654 * this creates a new 'base' pointer, off_reg (variable) gets
2655 * added into the variable offset, and we copy the fixed offset
2656 * from ptr_reg.
2657 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002658 if (signed_add_overflows(smin_ptr, smin_val) ||
2659 signed_add_overflows(smax_ptr, smax_val)) {
2660 dst_reg->smin_value = S64_MIN;
2661 dst_reg->smax_value = S64_MAX;
2662 } else {
2663 dst_reg->smin_value = smin_ptr + smin_val;
2664 dst_reg->smax_value = smax_ptr + smax_val;
2665 }
2666 if (umin_ptr + umin_val < umin_ptr ||
2667 umax_ptr + umax_val < umax_ptr) {
2668 dst_reg->umin_value = 0;
2669 dst_reg->umax_value = U64_MAX;
2670 } else {
2671 dst_reg->umin_value = umin_ptr + umin_val;
2672 dst_reg->umax_value = umax_ptr + umax_val;
2673 }
Edward Creef1174f72017-08-07 15:26:19 +01002674 dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off);
2675 dst_reg->off = ptr_reg->off;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002676 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01002677 dst_reg->id = ++env->id_gen;
2678 /* something was added to pkt_ptr, set range to zero */
2679 dst_reg->range = 0;
2680 }
Josef Bacik48461132016-09-28 10:54:32 -04002681 break;
2682 case BPF_SUB:
Edward Creef1174f72017-08-07 15:26:19 +01002683 if (dst_reg == off_reg) {
2684 /* scalar -= pointer. Creates an unknown scalar */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08002685 verbose(env, "R%d tried to subtract pointer from scalar\n",
2686 dst);
Edward Creef1174f72017-08-07 15:26:19 +01002687 return -EACCES;
2688 }
2689 /* We don't allow subtraction from FP, because (according to
2690 * test_verifier.c test "invalid fp arithmetic", JITs might not
2691 * be able to deal with it.
Edward Cree93057062017-07-21 14:37:34 +01002692 */
Edward Creef1174f72017-08-07 15:26:19 +01002693 if (ptr_reg->type == PTR_TO_STACK) {
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08002694 verbose(env, "R%d subtraction from stack pointer prohibited\n",
2695 dst);
Edward Creef1174f72017-08-07 15:26:19 +01002696 return -EACCES;
2697 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002698 if (known && (ptr_reg->off - smin_val ==
2699 (s64)(s32)(ptr_reg->off - smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01002700 /* pointer -= K. Subtract it from fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01002701 dst_reg->smin_value = smin_ptr;
2702 dst_reg->smax_value = smax_ptr;
2703 dst_reg->umin_value = umin_ptr;
2704 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01002705 dst_reg->var_off = ptr_reg->var_off;
2706 dst_reg->id = ptr_reg->id;
Edward Creeb03c9f92017-08-07 15:26:36 +01002707 dst_reg->off = ptr_reg->off - smin_val;
Edward Creef1174f72017-08-07 15:26:19 +01002708 dst_reg->range = ptr_reg->range;
2709 break;
2710 }
Edward Creef1174f72017-08-07 15:26:19 +01002711 /* A new variable offset is created. If the subtrahend is known
2712 * nonnegative, then any reg->range we had before is still good.
2713 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002714 if (signed_sub_overflows(smin_ptr, smax_val) ||
2715 signed_sub_overflows(smax_ptr, smin_val)) {
2716 /* Overflow possible, we know nothing */
2717 dst_reg->smin_value = S64_MIN;
2718 dst_reg->smax_value = S64_MAX;
2719 } else {
2720 dst_reg->smin_value = smin_ptr - smax_val;
2721 dst_reg->smax_value = smax_ptr - smin_val;
2722 }
2723 if (umin_ptr < umax_val) {
2724 /* Overflow possible, we know nothing */
2725 dst_reg->umin_value = 0;
2726 dst_reg->umax_value = U64_MAX;
2727 } else {
2728 /* Cannot overflow (as long as bounds are consistent) */
2729 dst_reg->umin_value = umin_ptr - umax_val;
2730 dst_reg->umax_value = umax_ptr - umin_val;
2731 }
Edward Creef1174f72017-08-07 15:26:19 +01002732 dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off);
2733 dst_reg->off = ptr_reg->off;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002734 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01002735 dst_reg->id = ++env->id_gen;
2736 /* something was added to pkt_ptr, set range to zero */
Edward Creeb03c9f92017-08-07 15:26:36 +01002737 if (smin_val < 0)
Edward Creef1174f72017-08-07 15:26:19 +01002738 dst_reg->range = 0;
2739 }
2740 break;
2741 case BPF_AND:
2742 case BPF_OR:
2743 case BPF_XOR:
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08002744 /* bitwise ops on pointers are troublesome, prohibit. */
2745 verbose(env, "R%d bitwise operator %s on pointer prohibited\n",
2746 dst, bpf_alu_string[opcode >> 4]);
Edward Creef1174f72017-08-07 15:26:19 +01002747 return -EACCES;
2748 default:
2749 /* other operators (e.g. MUL,LSH) produce non-pointer results */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08002750 verbose(env, "R%d pointer arithmetic with %s operator prohibited\n",
2751 dst, bpf_alu_string[opcode >> 4]);
Edward Creef1174f72017-08-07 15:26:19 +01002752 return -EACCES;
2753 }
2754
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08002755 if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type))
2756 return -EINVAL;
2757
Edward Creeb03c9f92017-08-07 15:26:36 +01002758 __update_reg_bounds(dst_reg);
2759 __reg_deduce_bounds(dst_reg);
2760 __reg_bound_offset(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002761 return 0;
2762}
2763
Jann Horn468f6ea2017-12-18 20:11:56 -08002764/* WARNING: This function does calculations on 64-bit values, but the actual
2765 * execution may occur on 32-bit values. Therefore, things like bitshifts
2766 * need extra checks in the 32-bit case.
2767 */
Edward Creef1174f72017-08-07 15:26:19 +01002768static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
2769 struct bpf_insn *insn,
2770 struct bpf_reg_state *dst_reg,
2771 struct bpf_reg_state src_reg)
2772{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002773 struct bpf_reg_state *regs = cur_regs(env);
Edward Creef1174f72017-08-07 15:26:19 +01002774 u8 opcode = BPF_OP(insn->code);
2775 bool src_known, dst_known;
Edward Creeb03c9f92017-08-07 15:26:36 +01002776 s64 smin_val, smax_val;
2777 u64 umin_val, umax_val;
Jann Horn468f6ea2017-12-18 20:11:56 -08002778 u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32;
Edward Creef1174f72017-08-07 15:26:19 +01002779
Edward Creeb03c9f92017-08-07 15:26:36 +01002780 smin_val = src_reg.smin_value;
2781 smax_val = src_reg.smax_value;
2782 umin_val = src_reg.umin_value;
2783 umax_val = src_reg.umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01002784 src_known = tnum_is_const(src_reg.var_off);
2785 dst_known = tnum_is_const(dst_reg->var_off);
2786
Daniel Borkmann6f161012018-01-18 01:15:21 +01002787 if ((src_known && (smin_val != smax_val || umin_val != umax_val)) ||
2788 smin_val > smax_val || umin_val > umax_val) {
2789 /* Taint dst register if offset had invalid bounds derived from
2790 * e.g. dead branches.
2791 */
2792 __mark_reg_unknown(dst_reg);
2793 return 0;
2794 }
2795
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08002796 if (!src_known &&
2797 opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) {
2798 __mark_reg_unknown(dst_reg);
2799 return 0;
2800 }
2801
Edward Creef1174f72017-08-07 15:26:19 +01002802 switch (opcode) {
2803 case BPF_ADD:
Edward Creeb03c9f92017-08-07 15:26:36 +01002804 if (signed_add_overflows(dst_reg->smin_value, smin_val) ||
2805 signed_add_overflows(dst_reg->smax_value, smax_val)) {
2806 dst_reg->smin_value = S64_MIN;
2807 dst_reg->smax_value = S64_MAX;
2808 } else {
2809 dst_reg->smin_value += smin_val;
2810 dst_reg->smax_value += smax_val;
2811 }
2812 if (dst_reg->umin_value + umin_val < umin_val ||
2813 dst_reg->umax_value + umax_val < umax_val) {
2814 dst_reg->umin_value = 0;
2815 dst_reg->umax_value = U64_MAX;
2816 } else {
2817 dst_reg->umin_value += umin_val;
2818 dst_reg->umax_value += umax_val;
2819 }
Edward Creef1174f72017-08-07 15:26:19 +01002820 dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off);
2821 break;
2822 case BPF_SUB:
Edward Creeb03c9f92017-08-07 15:26:36 +01002823 if (signed_sub_overflows(dst_reg->smin_value, smax_val) ||
2824 signed_sub_overflows(dst_reg->smax_value, smin_val)) {
2825 /* Overflow possible, we know nothing */
2826 dst_reg->smin_value = S64_MIN;
2827 dst_reg->smax_value = S64_MAX;
2828 } else {
2829 dst_reg->smin_value -= smax_val;
2830 dst_reg->smax_value -= smin_val;
2831 }
2832 if (dst_reg->umin_value < umax_val) {
2833 /* Overflow possible, we know nothing */
2834 dst_reg->umin_value = 0;
2835 dst_reg->umax_value = U64_MAX;
2836 } else {
2837 /* Cannot overflow (as long as bounds are consistent) */
2838 dst_reg->umin_value -= umax_val;
2839 dst_reg->umax_value -= umin_val;
2840 }
Edward Creef1174f72017-08-07 15:26:19 +01002841 dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off);
Josef Bacik48461132016-09-28 10:54:32 -04002842 break;
2843 case BPF_MUL:
Edward Creeb03c9f92017-08-07 15:26:36 +01002844 dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off);
2845 if (smin_val < 0 || dst_reg->smin_value < 0) {
Edward Creef1174f72017-08-07 15:26:19 +01002846 /* Ain't nobody got time to multiply that sign */
Edward Creeb03c9f92017-08-07 15:26:36 +01002847 __mark_reg_unbounded(dst_reg);
2848 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002849 break;
2850 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002851 /* Both values are positive, so we can work with unsigned and
2852 * copy the result to signed (unless it exceeds S64_MAX).
Edward Creef1174f72017-08-07 15:26:19 +01002853 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002854 if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) {
2855 /* Potential overflow, we know nothing */
2856 __mark_reg_unbounded(dst_reg);
2857 /* (except what we can learn from the var_off) */
2858 __update_reg_bounds(dst_reg);
2859 break;
2860 }
2861 dst_reg->umin_value *= umin_val;
2862 dst_reg->umax_value *= umax_val;
2863 if (dst_reg->umax_value > S64_MAX) {
2864 /* Overflow possible, we know nothing */
2865 dst_reg->smin_value = S64_MIN;
2866 dst_reg->smax_value = S64_MAX;
2867 } else {
2868 dst_reg->smin_value = dst_reg->umin_value;
2869 dst_reg->smax_value = dst_reg->umax_value;
2870 }
Josef Bacik48461132016-09-28 10:54:32 -04002871 break;
2872 case BPF_AND:
Edward Creef1174f72017-08-07 15:26:19 +01002873 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01002874 __mark_reg_known(dst_reg, dst_reg->var_off.value &
2875 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01002876 break;
2877 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002878 /* We get our minimum from the var_off, since that's inherently
2879 * bitwise. Our maximum is the minimum of the operands' maxima.
Josef Bacikf23cc642016-11-14 15:45:36 -05002880 */
Edward Creef1174f72017-08-07 15:26:19 +01002881 dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01002882 dst_reg->umin_value = dst_reg->var_off.value;
2883 dst_reg->umax_value = min(dst_reg->umax_value, umax_val);
2884 if (dst_reg->smin_value < 0 || smin_val < 0) {
2885 /* Lose signed bounds when ANDing negative numbers,
2886 * ain't nobody got time for that.
2887 */
2888 dst_reg->smin_value = S64_MIN;
2889 dst_reg->smax_value = S64_MAX;
2890 } else {
2891 /* ANDing two positives gives a positive, so safe to
2892 * cast result into s64.
2893 */
2894 dst_reg->smin_value = dst_reg->umin_value;
2895 dst_reg->smax_value = dst_reg->umax_value;
2896 }
2897 /* We may learn something more from the var_off */
2898 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002899 break;
2900 case BPF_OR:
2901 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01002902 __mark_reg_known(dst_reg, dst_reg->var_off.value |
2903 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01002904 break;
2905 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002906 /* We get our maximum from the var_off, and our minimum is the
2907 * maximum of the operands' minima
Edward Creef1174f72017-08-07 15:26:19 +01002908 */
2909 dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01002910 dst_reg->umin_value = max(dst_reg->umin_value, umin_val);
2911 dst_reg->umax_value = dst_reg->var_off.value |
2912 dst_reg->var_off.mask;
2913 if (dst_reg->smin_value < 0 || smin_val < 0) {
2914 /* Lose signed bounds when ORing negative numbers,
2915 * ain't nobody got time for that.
2916 */
2917 dst_reg->smin_value = S64_MIN;
2918 dst_reg->smax_value = S64_MAX;
Edward Creef1174f72017-08-07 15:26:19 +01002919 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01002920 /* ORing two positives gives a positive, so safe to
2921 * cast result into s64.
2922 */
2923 dst_reg->smin_value = dst_reg->umin_value;
2924 dst_reg->smax_value = dst_reg->umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01002925 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002926 /* We may learn something more from the var_off */
2927 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002928 break;
2929 case BPF_LSH:
Jann Horn468f6ea2017-12-18 20:11:56 -08002930 if (umax_val >= insn_bitness) {
2931 /* Shifts greater than 31 or 63 are undefined.
2932 * This includes shifts by a negative number.
Edward Creeb03c9f92017-08-07 15:26:36 +01002933 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002934 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002935 break;
2936 }
Edward Creeb03c9f92017-08-07 15:26:36 +01002937 /* We lose all sign bit information (except what we can pick
2938 * up from var_off)
Josef Bacik48461132016-09-28 10:54:32 -04002939 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002940 dst_reg->smin_value = S64_MIN;
2941 dst_reg->smax_value = S64_MAX;
2942 /* If we might shift our top bit out, then we know nothing */
2943 if (dst_reg->umax_value > 1ULL << (63 - umax_val)) {
2944 dst_reg->umin_value = 0;
2945 dst_reg->umax_value = U64_MAX;
David S. Millerd1174412017-05-10 11:22:52 -07002946 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01002947 dst_reg->umin_value <<= umin_val;
2948 dst_reg->umax_value <<= umax_val;
David S. Millerd1174412017-05-10 11:22:52 -07002949 }
Yonghong Songafbe1a52018-04-28 22:28:10 -07002950 dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val);
Edward Creeb03c9f92017-08-07 15:26:36 +01002951 /* We may learn something more from the var_off */
2952 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002953 break;
2954 case BPF_RSH:
Jann Horn468f6ea2017-12-18 20:11:56 -08002955 if (umax_val >= insn_bitness) {
2956 /* Shifts greater than 31 or 63 are undefined.
2957 * This includes shifts by a negative number.
Edward Creeb03c9f92017-08-07 15:26:36 +01002958 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002959 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01002960 break;
2961 }
Edward Cree4374f252017-12-18 20:11:53 -08002962 /* BPF_RSH is an unsigned shift. If the value in dst_reg might
2963 * be negative, then either:
2964 * 1) src_reg might be zero, so the sign bit of the result is
2965 * unknown, so we lose our signed bounds
2966 * 2) it's known negative, thus the unsigned bounds capture the
2967 * signed bounds
2968 * 3) the signed bounds cross zero, so they tell us nothing
2969 * about the result
2970 * If the value in dst_reg is known nonnegative, then again the
2971 * unsigned bounts capture the signed bounds.
2972 * Thus, in all cases it suffices to blow away our signed bounds
2973 * and rely on inferring new ones from the unsigned bounds and
2974 * var_off of the result.
2975 */
2976 dst_reg->smin_value = S64_MIN;
2977 dst_reg->smax_value = S64_MAX;
Yonghong Songafbe1a52018-04-28 22:28:10 -07002978 dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val);
Edward Creeb03c9f92017-08-07 15:26:36 +01002979 dst_reg->umin_value >>= umax_val;
2980 dst_reg->umax_value >>= umin_val;
2981 /* We may learn something more from the var_off */
2982 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04002983 break;
Yonghong Song9cbe1f5a2018-04-28 22:28:11 -07002984 case BPF_ARSH:
2985 if (umax_val >= insn_bitness) {
2986 /* Shifts greater than 31 or 63 are undefined.
2987 * This includes shifts by a negative number.
2988 */
2989 mark_reg_unknown(env, regs, insn->dst_reg);
2990 break;
2991 }
2992
2993 /* Upon reaching here, src_known is true and
2994 * umax_val is equal to umin_val.
2995 */
2996 dst_reg->smin_value >>= umin_val;
2997 dst_reg->smax_value >>= umin_val;
2998 dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val);
2999
3000 /* blow away the dst_reg umin_value/umax_value and rely on
3001 * dst_reg var_off to refine the result.
3002 */
3003 dst_reg->umin_value = 0;
3004 dst_reg->umax_value = U64_MAX;
3005 __update_reg_bounds(dst_reg);
3006 break;
Josef Bacik48461132016-09-28 10:54:32 -04003007 default:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003008 mark_reg_unknown(env, regs, insn->dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04003009 break;
3010 }
3011
Jann Horn468f6ea2017-12-18 20:11:56 -08003012 if (BPF_CLASS(insn->code) != BPF_ALU64) {
3013 /* 32-bit ALU ops are (32,32)->32 */
3014 coerce_reg_to_size(dst_reg, 4);
3015 coerce_reg_to_size(&src_reg, 4);
3016 }
3017
Edward Creeb03c9f92017-08-07 15:26:36 +01003018 __reg_deduce_bounds(dst_reg);
3019 __reg_bound_offset(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003020 return 0;
3021}
3022
3023/* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max
3024 * and var_off.
3025 */
3026static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
3027 struct bpf_insn *insn)
3028{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003029 struct bpf_verifier_state *vstate = env->cur_state;
3030 struct bpf_func_state *state = vstate->frame[vstate->curframe];
3031 struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg;
Edward Creef1174f72017-08-07 15:26:19 +01003032 struct bpf_reg_state *ptr_reg = NULL, off_reg = {0};
3033 u8 opcode = BPF_OP(insn->code);
Edward Creef1174f72017-08-07 15:26:19 +01003034
3035 dst_reg = &regs[insn->dst_reg];
Edward Creef1174f72017-08-07 15:26:19 +01003036 src_reg = NULL;
3037 if (dst_reg->type != SCALAR_VALUE)
3038 ptr_reg = dst_reg;
3039 if (BPF_SRC(insn->code) == BPF_X) {
3040 src_reg = &regs[insn->src_reg];
Edward Creef1174f72017-08-07 15:26:19 +01003041 if (src_reg->type != SCALAR_VALUE) {
3042 if (dst_reg->type != SCALAR_VALUE) {
3043 /* Combining two pointers by any ALU op yields
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003044 * an arbitrary scalar. Disallow all math except
3045 * pointer subtraction
Edward Creef1174f72017-08-07 15:26:19 +01003046 */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003047 if (opcode == BPF_SUB){
3048 mark_reg_unknown(env, regs, insn->dst_reg);
3049 return 0;
Edward Creef1174f72017-08-07 15:26:19 +01003050 }
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003051 verbose(env, "R%d pointer %s pointer prohibited\n",
3052 insn->dst_reg,
3053 bpf_alu_string[opcode >> 4]);
3054 return -EACCES;
Edward Creef1174f72017-08-07 15:26:19 +01003055 } else {
3056 /* scalar += pointer
3057 * This is legal, but we have to reverse our
3058 * src/dest handling in computing the range
3059 */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003060 return adjust_ptr_min_max_vals(env, insn,
3061 src_reg, dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003062 }
3063 } else if (ptr_reg) {
3064 /* pointer += scalar */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003065 return adjust_ptr_min_max_vals(env, insn,
3066 dst_reg, src_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003067 }
3068 } else {
3069 /* Pretend the src is a reg with a known value, since we only
3070 * need to be able to read from this state.
3071 */
3072 off_reg.type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01003073 __mark_reg_known(&off_reg, insn->imm);
Edward Creef1174f72017-08-07 15:26:19 +01003074 src_reg = &off_reg;
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003075 if (ptr_reg) /* pointer += K */
3076 return adjust_ptr_min_max_vals(env, insn,
3077 ptr_reg, src_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003078 }
3079
3080 /* Got here implies adding two SCALAR_VALUEs */
3081 if (WARN_ON_ONCE(ptr_reg)) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003082 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003083 verbose(env, "verifier internal error: unexpected ptr_reg\n");
Edward Creef1174f72017-08-07 15:26:19 +01003084 return -EINVAL;
3085 }
3086 if (WARN_ON(!src_reg)) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003087 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003088 verbose(env, "verifier internal error: no src_reg\n");
Edward Creef1174f72017-08-07 15:26:19 +01003089 return -EINVAL;
3090 }
3091 return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg);
Josef Bacik48461132016-09-28 10:54:32 -04003092}
3093
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003094/* check validity of 32-bit and 64-bit arithmetic operations */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003095static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003096{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003097 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003098 u8 opcode = BPF_OP(insn->code);
3099 int err;
3100
3101 if (opcode == BPF_END || opcode == BPF_NEG) {
3102 if (opcode == BPF_NEG) {
3103 if (BPF_SRC(insn->code) != 0 ||
3104 insn->src_reg != BPF_REG_0 ||
3105 insn->off != 0 || insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003106 verbose(env, "BPF_NEG uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003107 return -EINVAL;
3108 }
3109 } else {
3110 if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
Edward Creee67b8a62017-09-15 14:37:38 +01003111 (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) ||
3112 BPF_CLASS(insn->code) == BPF_ALU64) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003113 verbose(env, "BPF_END uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003114 return -EINVAL;
3115 }
3116 }
3117
3118 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01003119 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003120 if (err)
3121 return err;
3122
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003123 if (is_pointer_value(env, insn->dst_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003124 verbose(env, "R%d pointer arithmetic prohibited\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003125 insn->dst_reg);
3126 return -EACCES;
3127 }
3128
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003129 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01003130 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003131 if (err)
3132 return err;
3133
3134 } else if (opcode == BPF_MOV) {
3135
3136 if (BPF_SRC(insn->code) == BPF_X) {
3137 if (insn->imm != 0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003138 verbose(env, "BPF_MOV uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003139 return -EINVAL;
3140 }
3141
3142 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01003143 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003144 if (err)
3145 return err;
3146 } else {
3147 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003148 verbose(env, "BPF_MOV uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003149 return -EINVAL;
3150 }
3151 }
3152
3153 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01003154 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003155 if (err)
3156 return err;
3157
3158 if (BPF_SRC(insn->code) == BPF_X) {
3159 if (BPF_CLASS(insn->code) == BPF_ALU64) {
3160 /* case: R1 = R2
3161 * copy register state to dest reg
3162 */
3163 regs[insn->dst_reg] = regs[insn->src_reg];
Alexei Starovoitov8fe2d6c2017-10-05 16:20:56 -07003164 regs[insn->dst_reg].live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003165 } else {
Edward Creef1174f72017-08-07 15:26:19 +01003166 /* R1 = (u32) R2 */
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003167 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003168 verbose(env,
3169 "R%d partial copy of pointer\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003170 insn->src_reg);
3171 return -EACCES;
3172 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003173 mark_reg_unknown(env, regs, insn->dst_reg);
Jann Horn0c17d1d2017-12-18 20:11:55 -08003174 coerce_reg_to_size(&regs[insn->dst_reg], 4);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003175 }
3176 } else {
3177 /* case: R = imm
3178 * remember the value we stored into this reg
3179 */
Edward Creef1174f72017-08-07 15:26:19 +01003180 regs[insn->dst_reg].type = SCALAR_VALUE;
Jann Horn95a762e2017-12-18 20:11:54 -08003181 if (BPF_CLASS(insn->code) == BPF_ALU64) {
3182 __mark_reg_known(regs + insn->dst_reg,
3183 insn->imm);
3184 } else {
3185 __mark_reg_known(regs + insn->dst_reg,
3186 (u32)insn->imm);
3187 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003188 }
3189
3190 } else if (opcode > BPF_END) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003191 verbose(env, "invalid BPF_ALU opcode %x\n", opcode);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003192 return -EINVAL;
3193
3194 } else { /* all other ALU ops: and, sub, xor, add, ... */
3195
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003196 if (BPF_SRC(insn->code) == BPF_X) {
3197 if (insn->imm != 0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003198 verbose(env, "BPF_ALU uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003199 return -EINVAL;
3200 }
3201 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01003202 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003203 if (err)
3204 return err;
3205 } else {
3206 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003207 verbose(env, "BPF_ALU uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003208 return -EINVAL;
3209 }
3210 }
3211
3212 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01003213 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003214 if (err)
3215 return err;
3216
3217 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
3218 BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003219 verbose(env, "div by zero\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003220 return -EINVAL;
3221 }
3222
Daniel Borkmann7891a872018-01-10 20:04:37 +01003223 if (opcode == BPF_ARSH && BPF_CLASS(insn->code) != BPF_ALU64) {
3224 verbose(env, "BPF_ARSH not supported for 32 bit ALU\n");
3225 return -EINVAL;
3226 }
3227
Rabin Vincent229394e82016-01-12 20:17:08 +01003228 if ((opcode == BPF_LSH || opcode == BPF_RSH ||
3229 opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
3230 int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
3231
3232 if (insn->imm < 0 || insn->imm >= size) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003233 verbose(env, "invalid shift %d\n", insn->imm);
Rabin Vincent229394e82016-01-12 20:17:08 +01003234 return -EINVAL;
3235 }
3236 }
3237
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003238 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01003239 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003240 if (err)
3241 return err;
3242
Edward Creef1174f72017-08-07 15:26:19 +01003243 return adjust_reg_min_max_vals(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003244 }
3245
3246 return 0;
3247}
3248
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003249static void find_good_pkt_pointers(struct bpf_verifier_state *vstate,
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003250 struct bpf_reg_state *dst_reg,
David S. Millerf8ddadc2017-10-22 13:36:53 +01003251 enum bpf_reg_type type,
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003252 bool range_right_open)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003253{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003254 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003255 struct bpf_reg_state *regs = state->regs, *reg;
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003256 u16 new_range;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003257 int i, j;
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003258
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003259 if (dst_reg->off < 0 ||
3260 (dst_reg->off == 0 && range_right_open))
Edward Creef1174f72017-08-07 15:26:19 +01003261 /* This doesn't give us any range */
3262 return;
3263
Edward Creeb03c9f92017-08-07 15:26:36 +01003264 if (dst_reg->umax_value > MAX_PACKET_OFF ||
3265 dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF)
Edward Creef1174f72017-08-07 15:26:19 +01003266 /* Risk of overflow. For instance, ptr + (1<<63) may be less
3267 * than pkt_end, but that's because it's also less than pkt.
3268 */
3269 return;
3270
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003271 new_range = dst_reg->off;
3272 if (range_right_open)
3273 new_range--;
3274
3275 /* Examples for register markings:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003276 *
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003277 * pkt_data in dst register:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003278 *
3279 * r2 = r3;
3280 * r2 += 8;
3281 * if (r2 > pkt_end) goto <handle exception>
3282 * <access okay>
3283 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003284 * r2 = r3;
3285 * r2 += 8;
3286 * if (r2 < pkt_end) goto <access okay>
3287 * <handle exception>
3288 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003289 * Where:
3290 * r2 == dst_reg, pkt_end == src_reg
3291 * r2=pkt(id=n,off=8,r=0)
3292 * r3=pkt(id=n,off=0,r=0)
3293 *
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003294 * pkt_data in src register:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003295 *
3296 * r2 = r3;
3297 * r2 += 8;
3298 * if (pkt_end >= r2) goto <access okay>
3299 * <handle exception>
3300 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003301 * r2 = r3;
3302 * r2 += 8;
3303 * if (pkt_end <= r2) goto <handle exception>
3304 * <access okay>
3305 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003306 * Where:
3307 * pkt_end == dst_reg, r2 == src_reg
3308 * r2=pkt(id=n,off=8,r=0)
3309 * r3=pkt(id=n,off=0,r=0)
3310 *
3311 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003312 * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8)
3313 * and [r3, r3 + 8-1) respectively is safe to access depending on
3314 * the check.
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003315 */
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003316
Edward Creef1174f72017-08-07 15:26:19 +01003317 /* If our ids match, then we must have the same max_value. And we
3318 * don't care about the other reg's fixed offset, since if it's too big
3319 * the range won't allow anything.
3320 * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16.
3321 */
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003322 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003323 if (regs[i].type == type && regs[i].id == dst_reg->id)
Alexei Starovoitovb1977682017-03-24 15:57:33 -07003324 /* keep the maximum range already checked */
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003325 regs[i].range = max(regs[i].range, new_range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003326
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003327 for (j = 0; j <= vstate->curframe; j++) {
3328 state = vstate->frame[j];
3329 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
3330 if (state->stack[i].slot_type[0] != STACK_SPILL)
3331 continue;
3332 reg = &state->stack[i].spilled_ptr;
3333 if (reg->type == type && reg->id == dst_reg->id)
3334 reg->range = max(reg->range, new_range);
3335 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003336 }
3337}
3338
Josef Bacik48461132016-09-28 10:54:32 -04003339/* Adjusts the register min/max values in the case that the dst_reg is the
3340 * variable register that we are working on, and src_reg is a constant or we're
3341 * simply doing a BPF_K check.
Edward Creef1174f72017-08-07 15:26:19 +01003342 * In JEQ/JNE cases we also adjust the var_off values.
Josef Bacik48461132016-09-28 10:54:32 -04003343 */
3344static void reg_set_min_max(struct bpf_reg_state *true_reg,
3345 struct bpf_reg_state *false_reg, u64 val,
3346 u8 opcode)
3347{
Edward Creef1174f72017-08-07 15:26:19 +01003348 /* If the dst_reg is a pointer, we can't learn anything about its
3349 * variable offset from the compare (unless src_reg were a pointer into
3350 * the same object, but we don't bother with that.
3351 * Since false_reg and true_reg have the same type by construction, we
3352 * only need to check one of them for pointerness.
3353 */
3354 if (__is_pointer_value(false, false_reg))
3355 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02003356
Josef Bacik48461132016-09-28 10:54:32 -04003357 switch (opcode) {
3358 case BPF_JEQ:
3359 /* If this is false then we know nothing Jon Snow, but if it is
3360 * true then we know for sure.
3361 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003362 __mark_reg_known(true_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04003363 break;
3364 case BPF_JNE:
3365 /* If this is true we know nothing Jon Snow, but if it is false
3366 * we know the value for sure;
3367 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003368 __mark_reg_known(false_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04003369 break;
3370 case BPF_JGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01003371 false_reg->umax_value = min(false_reg->umax_value, val);
3372 true_reg->umin_value = max(true_reg->umin_value, val + 1);
3373 break;
Josef Bacik48461132016-09-28 10:54:32 -04003374 case BPF_JSGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01003375 false_reg->smax_value = min_t(s64, false_reg->smax_value, val);
3376 true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1);
Josef Bacik48461132016-09-28 10:54:32 -04003377 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003378 case BPF_JLT:
3379 false_reg->umin_value = max(false_reg->umin_value, val);
3380 true_reg->umax_value = min(true_reg->umax_value, val - 1);
3381 break;
3382 case BPF_JSLT:
3383 false_reg->smin_value = max_t(s64, false_reg->smin_value, val);
3384 true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1);
3385 break;
Josef Bacik48461132016-09-28 10:54:32 -04003386 case BPF_JGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01003387 false_reg->umax_value = min(false_reg->umax_value, val - 1);
3388 true_reg->umin_value = max(true_reg->umin_value, val);
3389 break;
Josef Bacik48461132016-09-28 10:54:32 -04003390 case BPF_JSGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01003391 false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1);
3392 true_reg->smin_value = max_t(s64, true_reg->smin_value, val);
Josef Bacik48461132016-09-28 10:54:32 -04003393 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003394 case BPF_JLE:
3395 false_reg->umin_value = max(false_reg->umin_value, val + 1);
3396 true_reg->umax_value = min(true_reg->umax_value, val);
3397 break;
3398 case BPF_JSLE:
3399 false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1);
3400 true_reg->smax_value = min_t(s64, true_reg->smax_value, val);
3401 break;
Josef Bacik48461132016-09-28 10:54:32 -04003402 default:
3403 break;
3404 }
3405
Edward Creeb03c9f92017-08-07 15:26:36 +01003406 __reg_deduce_bounds(false_reg);
3407 __reg_deduce_bounds(true_reg);
3408 /* We might have learned some bits from the bounds. */
3409 __reg_bound_offset(false_reg);
3410 __reg_bound_offset(true_reg);
3411 /* Intersecting with the old var_off might have improved our bounds
3412 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
3413 * then new var_off is (0; 0x7f...fc) which improves our umax.
3414 */
3415 __update_reg_bounds(false_reg);
3416 __update_reg_bounds(true_reg);
Josef Bacik48461132016-09-28 10:54:32 -04003417}
3418
Edward Creef1174f72017-08-07 15:26:19 +01003419/* Same as above, but for the case that dst_reg holds a constant and src_reg is
3420 * the variable reg.
Josef Bacik48461132016-09-28 10:54:32 -04003421 */
3422static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
3423 struct bpf_reg_state *false_reg, u64 val,
3424 u8 opcode)
3425{
Edward Creef1174f72017-08-07 15:26:19 +01003426 if (__is_pointer_value(false, false_reg))
3427 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02003428
Josef Bacik48461132016-09-28 10:54:32 -04003429 switch (opcode) {
3430 case BPF_JEQ:
3431 /* If this is false then we know nothing Jon Snow, but if it is
3432 * true then we know for sure.
3433 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003434 __mark_reg_known(true_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04003435 break;
3436 case BPF_JNE:
3437 /* If this is true we know nothing Jon Snow, but if it is false
3438 * we know the value for sure;
3439 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003440 __mark_reg_known(false_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04003441 break;
3442 case BPF_JGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01003443 true_reg->umax_value = min(true_reg->umax_value, val - 1);
3444 false_reg->umin_value = max(false_reg->umin_value, val);
3445 break;
Josef Bacik48461132016-09-28 10:54:32 -04003446 case BPF_JSGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01003447 true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1);
3448 false_reg->smin_value = max_t(s64, false_reg->smin_value, val);
Josef Bacik48461132016-09-28 10:54:32 -04003449 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003450 case BPF_JLT:
3451 true_reg->umin_value = max(true_reg->umin_value, val + 1);
3452 false_reg->umax_value = min(false_reg->umax_value, val);
3453 break;
3454 case BPF_JSLT:
3455 true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1);
3456 false_reg->smax_value = min_t(s64, false_reg->smax_value, val);
3457 break;
Josef Bacik48461132016-09-28 10:54:32 -04003458 case BPF_JGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01003459 true_reg->umax_value = min(true_reg->umax_value, val);
3460 false_reg->umin_value = max(false_reg->umin_value, val + 1);
3461 break;
Josef Bacik48461132016-09-28 10:54:32 -04003462 case BPF_JSGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01003463 true_reg->smax_value = min_t(s64, true_reg->smax_value, val);
3464 false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1);
Josef Bacik48461132016-09-28 10:54:32 -04003465 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003466 case BPF_JLE:
3467 true_reg->umin_value = max(true_reg->umin_value, val);
3468 false_reg->umax_value = min(false_reg->umax_value, val - 1);
3469 break;
3470 case BPF_JSLE:
3471 true_reg->smin_value = max_t(s64, true_reg->smin_value, val);
3472 false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1);
3473 break;
Josef Bacik48461132016-09-28 10:54:32 -04003474 default:
3475 break;
3476 }
3477
Edward Creeb03c9f92017-08-07 15:26:36 +01003478 __reg_deduce_bounds(false_reg);
3479 __reg_deduce_bounds(true_reg);
3480 /* We might have learned some bits from the bounds. */
3481 __reg_bound_offset(false_reg);
3482 __reg_bound_offset(true_reg);
3483 /* Intersecting with the old var_off might have improved our bounds
3484 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
3485 * then new var_off is (0; 0x7f...fc) which improves our umax.
3486 */
3487 __update_reg_bounds(false_reg);
3488 __update_reg_bounds(true_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003489}
3490
3491/* Regs are known to be equal, so intersect their min/max/var_off */
3492static void __reg_combine_min_max(struct bpf_reg_state *src_reg,
3493 struct bpf_reg_state *dst_reg)
3494{
Edward Creeb03c9f92017-08-07 15:26:36 +01003495 src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value,
3496 dst_reg->umin_value);
3497 src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value,
3498 dst_reg->umax_value);
3499 src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value,
3500 dst_reg->smin_value);
3501 src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value,
3502 dst_reg->smax_value);
Edward Creef1174f72017-08-07 15:26:19 +01003503 src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off,
3504 dst_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01003505 /* We might have learned new bounds from the var_off. */
3506 __update_reg_bounds(src_reg);
3507 __update_reg_bounds(dst_reg);
3508 /* We might have learned something about the sign bit. */
3509 __reg_deduce_bounds(src_reg);
3510 __reg_deduce_bounds(dst_reg);
3511 /* We might have learned some bits from the bounds. */
3512 __reg_bound_offset(src_reg);
3513 __reg_bound_offset(dst_reg);
3514 /* Intersecting with the old var_off might have improved our bounds
3515 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
3516 * then new var_off is (0; 0x7f...fc) which improves our umax.
3517 */
3518 __update_reg_bounds(src_reg);
3519 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003520}
3521
3522static void reg_combine_min_max(struct bpf_reg_state *true_src,
3523 struct bpf_reg_state *true_dst,
3524 struct bpf_reg_state *false_src,
3525 struct bpf_reg_state *false_dst,
3526 u8 opcode)
3527{
3528 switch (opcode) {
3529 case BPF_JEQ:
3530 __reg_combine_min_max(true_src, true_dst);
3531 break;
3532 case BPF_JNE:
3533 __reg_combine_min_max(false_src, false_dst);
Edward Creeb03c9f92017-08-07 15:26:36 +01003534 break;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02003535 }
Josef Bacik48461132016-09-28 10:54:32 -04003536}
3537
Thomas Graf57a09bf2016-10-18 19:51:19 +02003538static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id,
Edward Creef1174f72017-08-07 15:26:19 +01003539 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02003540{
3541 struct bpf_reg_state *reg = &regs[regno];
3542
3543 if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) {
Edward Creef1174f72017-08-07 15:26:19 +01003544 /* Old offset (both fixed and variable parts) should
3545 * have been known-zero, because we don't allow pointer
3546 * arithmetic on pointers that might be NULL.
3547 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003548 if (WARN_ON_ONCE(reg->smin_value || reg->smax_value ||
3549 !tnum_equals_const(reg->var_off, 0) ||
Edward Creef1174f72017-08-07 15:26:19 +01003550 reg->off)) {
Edward Creeb03c9f92017-08-07 15:26:36 +01003551 __mark_reg_known_zero(reg);
3552 reg->off = 0;
Edward Creef1174f72017-08-07 15:26:19 +01003553 }
3554 if (is_null) {
3555 reg->type = SCALAR_VALUE;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07003556 } else if (reg->map_ptr->inner_map_meta) {
3557 reg->type = CONST_PTR_TO_MAP;
3558 reg->map_ptr = reg->map_ptr->inner_map_meta;
3559 } else {
Edward Creef1174f72017-08-07 15:26:19 +01003560 reg->type = PTR_TO_MAP_VALUE;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07003561 }
Daniel Borkmanna08dd0d2016-12-15 01:30:06 +01003562 /* We don't need id from this point onwards anymore, thus we
3563 * should better reset it, so that state pruning has chances
3564 * to take effect.
3565 */
3566 reg->id = 0;
Thomas Graf57a09bf2016-10-18 19:51:19 +02003567 }
3568}
3569
3570/* The logic is similar to find_good_pkt_pointers(), both could eventually
3571 * be folded together at some point.
3572 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003573static void mark_map_regs(struct bpf_verifier_state *vstate, u32 regno,
Edward Creef1174f72017-08-07 15:26:19 +01003574 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02003575{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003576 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Thomas Graf57a09bf2016-10-18 19:51:19 +02003577 struct bpf_reg_state *regs = state->regs;
Daniel Borkmanna08dd0d2016-12-15 01:30:06 +01003578 u32 id = regs[regno].id;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003579 int i, j;
Thomas Graf57a09bf2016-10-18 19:51:19 +02003580
3581 for (i = 0; i < MAX_BPF_REG; i++)
Edward Creef1174f72017-08-07 15:26:19 +01003582 mark_map_reg(regs, i, id, is_null);
Thomas Graf57a09bf2016-10-18 19:51:19 +02003583
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003584 for (j = 0; j <= vstate->curframe; j++) {
3585 state = vstate->frame[j];
3586 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
3587 if (state->stack[i].slot_type[0] != STACK_SPILL)
3588 continue;
3589 mark_map_reg(&state->stack[i].spilled_ptr, 0, id, is_null);
3590 }
Thomas Graf57a09bf2016-10-18 19:51:19 +02003591 }
3592}
3593
Daniel Borkmann5beca082017-11-01 23:58:10 +01003594static bool try_match_pkt_pointers(const struct bpf_insn *insn,
3595 struct bpf_reg_state *dst_reg,
3596 struct bpf_reg_state *src_reg,
3597 struct bpf_verifier_state *this_branch,
3598 struct bpf_verifier_state *other_branch)
3599{
3600 if (BPF_SRC(insn->code) != BPF_X)
3601 return false;
3602
3603 switch (BPF_OP(insn->code)) {
3604 case BPF_JGT:
3605 if ((dst_reg->type == PTR_TO_PACKET &&
3606 src_reg->type == PTR_TO_PACKET_END) ||
3607 (dst_reg->type == PTR_TO_PACKET_META &&
3608 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
3609 /* pkt_data' > pkt_end, pkt_meta' > pkt_data */
3610 find_good_pkt_pointers(this_branch, dst_reg,
3611 dst_reg->type, false);
3612 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
3613 src_reg->type == PTR_TO_PACKET) ||
3614 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
3615 src_reg->type == PTR_TO_PACKET_META)) {
3616 /* pkt_end > pkt_data', pkt_data > pkt_meta' */
3617 find_good_pkt_pointers(other_branch, src_reg,
3618 src_reg->type, true);
3619 } else {
3620 return false;
3621 }
3622 break;
3623 case BPF_JLT:
3624 if ((dst_reg->type == PTR_TO_PACKET &&
3625 src_reg->type == PTR_TO_PACKET_END) ||
3626 (dst_reg->type == PTR_TO_PACKET_META &&
3627 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
3628 /* pkt_data' < pkt_end, pkt_meta' < pkt_data */
3629 find_good_pkt_pointers(other_branch, dst_reg,
3630 dst_reg->type, true);
3631 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
3632 src_reg->type == PTR_TO_PACKET) ||
3633 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
3634 src_reg->type == PTR_TO_PACKET_META)) {
3635 /* pkt_end < pkt_data', pkt_data > pkt_meta' */
3636 find_good_pkt_pointers(this_branch, src_reg,
3637 src_reg->type, false);
3638 } else {
3639 return false;
3640 }
3641 break;
3642 case BPF_JGE:
3643 if ((dst_reg->type == PTR_TO_PACKET &&
3644 src_reg->type == PTR_TO_PACKET_END) ||
3645 (dst_reg->type == PTR_TO_PACKET_META &&
3646 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
3647 /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */
3648 find_good_pkt_pointers(this_branch, dst_reg,
3649 dst_reg->type, true);
3650 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
3651 src_reg->type == PTR_TO_PACKET) ||
3652 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
3653 src_reg->type == PTR_TO_PACKET_META)) {
3654 /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */
3655 find_good_pkt_pointers(other_branch, src_reg,
3656 src_reg->type, false);
3657 } else {
3658 return false;
3659 }
3660 break;
3661 case BPF_JLE:
3662 if ((dst_reg->type == PTR_TO_PACKET &&
3663 src_reg->type == PTR_TO_PACKET_END) ||
3664 (dst_reg->type == PTR_TO_PACKET_META &&
3665 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
3666 /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */
3667 find_good_pkt_pointers(other_branch, dst_reg,
3668 dst_reg->type, false);
3669 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
3670 src_reg->type == PTR_TO_PACKET) ||
3671 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
3672 src_reg->type == PTR_TO_PACKET_META)) {
3673 /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */
3674 find_good_pkt_pointers(this_branch, src_reg,
3675 src_reg->type, true);
3676 } else {
3677 return false;
3678 }
3679 break;
3680 default:
3681 return false;
3682 }
3683
3684 return true;
3685}
3686
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003687static int check_cond_jmp_op(struct bpf_verifier_env *env,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003688 struct bpf_insn *insn, int *insn_idx)
3689{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003690 struct bpf_verifier_state *this_branch = env->cur_state;
3691 struct bpf_verifier_state *other_branch;
3692 struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs;
3693 struct bpf_reg_state *dst_reg, *other_branch_regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003694 u8 opcode = BPF_OP(insn->code);
3695 int err;
3696
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003697 if (opcode > BPF_JSLE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003698 verbose(env, "invalid BPF_JMP opcode %x\n", opcode);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003699 return -EINVAL;
3700 }
3701
3702 if (BPF_SRC(insn->code) == BPF_X) {
3703 if (insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003704 verbose(env, "BPF_JMP uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003705 return -EINVAL;
3706 }
3707
3708 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01003709 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003710 if (err)
3711 return err;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003712
3713 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003714 verbose(env, "R%d pointer comparison prohibited\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003715 insn->src_reg);
3716 return -EACCES;
3717 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003718 } else {
3719 if (insn->src_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003720 verbose(env, "BPF_JMP uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003721 return -EINVAL;
3722 }
3723 }
3724
3725 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01003726 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003727 if (err)
3728 return err;
3729
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003730 dst_reg = &regs[insn->dst_reg];
3731
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003732 /* detect if R == 0 where R was initialized to zero earlier */
3733 if (BPF_SRC(insn->code) == BPF_K &&
3734 (opcode == BPF_JEQ || opcode == BPF_JNE) &&
Edward Creef1174f72017-08-07 15:26:19 +01003735 dst_reg->type == SCALAR_VALUE &&
Alexei Starovoitov3bf15922017-11-30 21:31:39 -08003736 tnum_is_const(dst_reg->var_off)) {
3737 if ((opcode == BPF_JEQ && dst_reg->var_off.value == insn->imm) ||
3738 (opcode == BPF_JNE && dst_reg->var_off.value != insn->imm)) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003739 /* if (imm == imm) goto pc+off;
3740 * only follow the goto, ignore fall-through
3741 */
3742 *insn_idx += insn->off;
3743 return 0;
3744 } else {
3745 /* if (imm != imm) goto pc+off;
3746 * only follow fall-through branch, since
3747 * that's where the program will go
3748 */
3749 return 0;
3750 }
3751 }
3752
3753 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx);
3754 if (!other_branch)
3755 return -EFAULT;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003756 other_branch_regs = other_branch->frame[other_branch->curframe]->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003757
Josef Bacik48461132016-09-28 10:54:32 -04003758 /* detect if we are comparing against a constant value so we can adjust
3759 * our min/max values for our dst register.
Edward Creef1174f72017-08-07 15:26:19 +01003760 * this is only legit if both are scalars (or pointers to the same
3761 * object, I suppose, but we don't support that right now), because
3762 * otherwise the different base pointers mean the offsets aren't
3763 * comparable.
Josef Bacik48461132016-09-28 10:54:32 -04003764 */
3765 if (BPF_SRC(insn->code) == BPF_X) {
Edward Creef1174f72017-08-07 15:26:19 +01003766 if (dst_reg->type == SCALAR_VALUE &&
3767 regs[insn->src_reg].type == SCALAR_VALUE) {
3768 if (tnum_is_const(regs[insn->src_reg].var_off))
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003769 reg_set_min_max(&other_branch_regs[insn->dst_reg],
Edward Creef1174f72017-08-07 15:26:19 +01003770 dst_reg, regs[insn->src_reg].var_off.value,
3771 opcode);
3772 else if (tnum_is_const(dst_reg->var_off))
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003773 reg_set_min_max_inv(&other_branch_regs[insn->src_reg],
Edward Creef1174f72017-08-07 15:26:19 +01003774 &regs[insn->src_reg],
3775 dst_reg->var_off.value, opcode);
3776 else if (opcode == BPF_JEQ || opcode == BPF_JNE)
3777 /* Comparing for equality, we can combine knowledge */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003778 reg_combine_min_max(&other_branch_regs[insn->src_reg],
3779 &other_branch_regs[insn->dst_reg],
Edward Creef1174f72017-08-07 15:26:19 +01003780 &regs[insn->src_reg],
3781 &regs[insn->dst_reg], opcode);
3782 }
3783 } else if (dst_reg->type == SCALAR_VALUE) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003784 reg_set_min_max(&other_branch_regs[insn->dst_reg],
Josef Bacik48461132016-09-28 10:54:32 -04003785 dst_reg, insn->imm, opcode);
3786 }
3787
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003788 /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003789 if (BPF_SRC(insn->code) == BPF_K &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003790 insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
3791 dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
Thomas Graf57a09bf2016-10-18 19:51:19 +02003792 /* Mark all identical map registers in each branch as either
3793 * safe or unknown depending R == 0 or R != 0 conditional.
3794 */
Edward Creef1174f72017-08-07 15:26:19 +01003795 mark_map_regs(this_branch, insn->dst_reg, opcode == BPF_JNE);
3796 mark_map_regs(other_branch, insn->dst_reg, opcode == BPF_JEQ);
Daniel Borkmann5beca082017-11-01 23:58:10 +01003797 } else if (!try_match_pkt_pointers(insn, dst_reg, &regs[insn->src_reg],
3798 this_branch, other_branch) &&
3799 is_pointer_value(env, insn->dst_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003800 verbose(env, "R%d pointer comparison prohibited\n",
3801 insn->dst_reg);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003802 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003803 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003804 if (env->log.level)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003805 print_verifier_state(env, this_branch->frame[this_branch->curframe]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003806 return 0;
3807}
3808
Alexei Starovoitov0246e642014-09-26 00:17:04 -07003809/* return the map pointer stored inside BPF_LD_IMM64 instruction */
3810static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
3811{
3812 u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
3813
3814 return (struct bpf_map *) (unsigned long) imm64;
3815}
3816
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003817/* verify BPF_LD_IMM64 instruction */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003818static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003819{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003820 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003821 int err;
3822
3823 if (BPF_SIZE(insn->code) != BPF_DW) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003824 verbose(env, "invalid BPF_LD_IMM insn\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003825 return -EINVAL;
3826 }
3827 if (insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003828 verbose(env, "BPF_LD_IMM64 uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003829 return -EINVAL;
3830 }
3831
Edward Creedc503a82017-08-15 20:34:35 +01003832 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003833 if (err)
3834 return err;
3835
Jakub Kicinski6b173872016-09-21 11:43:59 +01003836 if (insn->src_reg == 0) {
Jakub Kicinski6b173872016-09-21 11:43:59 +01003837 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
3838
Edward Creef1174f72017-08-07 15:26:19 +01003839 regs[insn->dst_reg].type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01003840 __mark_reg_known(&regs[insn->dst_reg], imm);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003841 return 0;
Jakub Kicinski6b173872016-09-21 11:43:59 +01003842 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003843
3844 /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
3845 BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
3846
3847 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
3848 regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
3849 return 0;
3850}
3851
Daniel Borkmann96be4322015-03-01 12:31:46 +01003852static bool may_access_skb(enum bpf_prog_type type)
3853{
3854 switch (type) {
3855 case BPF_PROG_TYPE_SOCKET_FILTER:
3856 case BPF_PROG_TYPE_SCHED_CLS:
Daniel Borkmann94caee8c2015-03-20 15:11:11 +01003857 case BPF_PROG_TYPE_SCHED_ACT:
Daniel Borkmann96be4322015-03-01 12:31:46 +01003858 return true;
3859 default:
3860 return false;
3861 }
3862}
3863
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003864/* verify safety of LD_ABS|LD_IND instructions:
3865 * - they can only appear in the programs where ctx == skb
3866 * - since they are wrappers of function calls, they scratch R1-R5 registers,
3867 * preserve R6-R9, and store return value into R0
3868 *
3869 * Implicit input:
3870 * ctx == skb == R6 == CTX
3871 *
3872 * Explicit input:
3873 * SRC == any register
3874 * IMM == 32-bit immediate
3875 *
3876 * Output:
3877 * R0 - 8/16/32-bit skb data converted to cpu endianness
3878 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003879static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003880{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003881 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003882 u8 mode = BPF_MODE(insn->code);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003883 int i, err;
3884
Daniel Borkmann24701ec2015-03-01 12:31:47 +01003885 if (!may_access_skb(env->prog->type)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003886 verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003887 return -EINVAL;
3888 }
3889
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +02003890 if (!env->ops->gen_ld_abs) {
3891 verbose(env, "bpf verifier is misconfigured\n");
3892 return -EINVAL;
3893 }
3894
Jiong Wangf910cef2018-05-02 16:17:17 -04003895 if (env->subprog_cnt > 1) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003896 /* when program has LD_ABS insn JITs and interpreter assume
3897 * that r1 == ctx == skb which is not the case for callees
3898 * that can have arbitrary arguments. It's problematic
3899 * for main prog as well since JITs would need to analyze
3900 * all functions in order to make proper register save/restore
3901 * decisions in the main prog. Hence disallow LD_ABS with calls
3902 */
3903 verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n");
3904 return -EINVAL;
3905 }
3906
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003907 if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
Alexei Starovoitovd82bccc2016-04-12 10:26:19 -07003908 BPF_SIZE(insn->code) == BPF_DW ||
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003909 (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003910 verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003911 return -EINVAL;
3912 }
3913
3914 /* check whether implicit source operand (register R6) is readable */
Edward Creedc503a82017-08-15 20:34:35 +01003915 err = check_reg_arg(env, BPF_REG_6, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003916 if (err)
3917 return err;
3918
3919 if (regs[BPF_REG_6].type != PTR_TO_CTX) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003920 verbose(env,
3921 "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003922 return -EINVAL;
3923 }
3924
3925 if (mode == BPF_IND) {
3926 /* check explicit source operand */
Edward Creedc503a82017-08-15 20:34:35 +01003927 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003928 if (err)
3929 return err;
3930 }
3931
3932 /* reset caller saved regs to unreadable */
Edward Creedc503a82017-08-15 20:34:35 +01003933 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003934 mark_reg_not_init(env, regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01003935 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
3936 }
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003937
3938 /* mark destination R0 register as readable, since it contains
Edward Creedc503a82017-08-15 20:34:35 +01003939 * the value fetched from the packet.
3940 * Already marked as written above.
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003941 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003942 mark_reg_unknown(env, regs, BPF_REG_0);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08003943 return 0;
3944}
3945
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003946static int check_return_code(struct bpf_verifier_env *env)
3947{
3948 struct bpf_reg_state *reg;
3949 struct tnum range = tnum_range(0, 1);
3950
3951 switch (env->prog->type) {
3952 case BPF_PROG_TYPE_CGROUP_SKB:
3953 case BPF_PROG_TYPE_CGROUP_SOCK:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07003954 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003955 case BPF_PROG_TYPE_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05003956 case BPF_PROG_TYPE_CGROUP_DEVICE:
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003957 break;
3958 default:
3959 return 0;
3960 }
3961
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003962 reg = cur_regs(env) + BPF_REG_0;
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003963 if (reg->type != SCALAR_VALUE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003964 verbose(env, "At program exit the register R0 is not a known value (%s)\n",
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003965 reg_type_str[reg->type]);
3966 return -EINVAL;
3967 }
3968
3969 if (!tnum_in(range, reg->var_off)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003970 verbose(env, "At program exit the register R0 ");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003971 if (!tnum_is_unknown(reg->var_off)) {
3972 char tn_buf[48];
3973
3974 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003975 verbose(env, "has value %s", tn_buf);
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003976 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003977 verbose(env, "has unknown scalar value");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003978 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003979 verbose(env, " should have been 0 or 1\n");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07003980 return -EINVAL;
3981 }
3982 return 0;
3983}
3984
Alexei Starovoitov475fb782014-09-26 00:17:05 -07003985/* non-recursive DFS pseudo code
3986 * 1 procedure DFS-iterative(G,v):
3987 * 2 label v as discovered
3988 * 3 let S be a stack
3989 * 4 S.push(v)
3990 * 5 while S is not empty
3991 * 6 t <- S.pop()
3992 * 7 if t is what we're looking for:
3993 * 8 return t
3994 * 9 for all edges e in G.adjacentEdges(t) do
3995 * 10 if edge e is already labelled
3996 * 11 continue with the next edge
3997 * 12 w <- G.adjacentVertex(t,e)
3998 * 13 if vertex w is not discovered and not explored
3999 * 14 label e as tree-edge
4000 * 15 label w as discovered
4001 * 16 S.push(w)
4002 * 17 continue at 5
4003 * 18 else if vertex w is discovered
4004 * 19 label e as back-edge
4005 * 20 else
4006 * 21 // vertex w is explored
4007 * 22 label e as forward- or cross-edge
4008 * 23 label t as explored
4009 * 24 S.pop()
4010 *
4011 * convention:
4012 * 0x10 - discovered
4013 * 0x11 - discovered and fall-through edge labelled
4014 * 0x12 - discovered and fall-through and branch edges labelled
4015 * 0x20 - explored
4016 */
4017
4018enum {
4019 DISCOVERED = 0x10,
4020 EXPLORED = 0x20,
4021 FALLTHROUGH = 1,
4022 BRANCH = 2,
4023};
4024
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004025#define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004026
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004027static int *insn_stack; /* stack of insns to process */
4028static int cur_stack; /* current stack index */
4029static int *insn_state;
4030
4031/* t, w, e - match pseudo-code above:
4032 * t - index of current instruction
4033 * w - next instruction
4034 * e - edge
4035 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004036static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004037{
4038 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
4039 return 0;
4040
4041 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
4042 return 0;
4043
4044 if (w < 0 || w >= env->prog->len) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004045 verbose(env, "jump out of range from insn %d to %d\n", t, w);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004046 return -EINVAL;
4047 }
4048
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004049 if (e == BRANCH)
4050 /* mark branch target for state pruning */
4051 env->explored_states[w] = STATE_LIST_MARK;
4052
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004053 if (insn_state[w] == 0) {
4054 /* tree-edge */
4055 insn_state[t] = DISCOVERED | e;
4056 insn_state[w] = DISCOVERED;
4057 if (cur_stack >= env->prog->len)
4058 return -E2BIG;
4059 insn_stack[cur_stack++] = w;
4060 return 1;
4061 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004062 verbose(env, "back-edge from insn %d to %d\n", t, w);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004063 return -EINVAL;
4064 } else if (insn_state[w] == EXPLORED) {
4065 /* forward- or cross-edge */
4066 insn_state[t] = DISCOVERED | e;
4067 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004068 verbose(env, "insn state internal bug\n");
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004069 return -EFAULT;
4070 }
4071 return 0;
4072}
4073
4074/* non-recursive depth-first-search to detect loops in BPF program
4075 * loop == back-edge in directed graph
4076 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004077static int check_cfg(struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004078{
4079 struct bpf_insn *insns = env->prog->insnsi;
4080 int insn_cnt = env->prog->len;
4081 int ret = 0;
4082 int i, t;
4083
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08004084 ret = check_subprogs(env);
4085 if (ret < 0)
4086 return ret;
4087
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004088 insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
4089 if (!insn_state)
4090 return -ENOMEM;
4091
4092 insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
4093 if (!insn_stack) {
4094 kfree(insn_state);
4095 return -ENOMEM;
4096 }
4097
4098 insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
4099 insn_stack[0] = 0; /* 0 is the first instruction */
4100 cur_stack = 1;
4101
4102peek_stack:
4103 if (cur_stack == 0)
4104 goto check_state;
4105 t = insn_stack[cur_stack - 1];
4106
4107 if (BPF_CLASS(insns[t].code) == BPF_JMP) {
4108 u8 opcode = BPF_OP(insns[t].code);
4109
4110 if (opcode == BPF_EXIT) {
4111 goto mark_explored;
4112 } else if (opcode == BPF_CALL) {
4113 ret = push_insn(t, t + 1, FALLTHROUGH, env);
4114 if (ret == 1)
4115 goto peek_stack;
4116 else if (ret < 0)
4117 goto err_free;
Daniel Borkmann07016152016-04-05 22:33:17 +02004118 if (t + 1 < insn_cnt)
4119 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08004120 if (insns[t].src_reg == BPF_PSEUDO_CALL) {
4121 env->explored_states[t] = STATE_LIST_MARK;
4122 ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env);
4123 if (ret == 1)
4124 goto peek_stack;
4125 else if (ret < 0)
4126 goto err_free;
4127 }
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004128 } else if (opcode == BPF_JA) {
4129 if (BPF_SRC(insns[t].code) != BPF_K) {
4130 ret = -EINVAL;
4131 goto err_free;
4132 }
4133 /* unconditional jump with single edge */
4134 ret = push_insn(t, t + insns[t].off + 1,
4135 FALLTHROUGH, env);
4136 if (ret == 1)
4137 goto peek_stack;
4138 else if (ret < 0)
4139 goto err_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004140 /* tell verifier to check for equivalent states
4141 * after every call and jump
4142 */
Alexei Starovoitovc3de6312015-04-14 15:57:13 -07004143 if (t + 1 < insn_cnt)
4144 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004145 } else {
4146 /* conditional jump with two edges */
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02004147 env->explored_states[t] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004148 ret = push_insn(t, t + 1, FALLTHROUGH, env);
4149 if (ret == 1)
4150 goto peek_stack;
4151 else if (ret < 0)
4152 goto err_free;
4153
4154 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
4155 if (ret == 1)
4156 goto peek_stack;
4157 else if (ret < 0)
4158 goto err_free;
4159 }
4160 } else {
4161 /* all other non-branch instructions with single
4162 * fall-through edge
4163 */
4164 ret = push_insn(t, t + 1, FALLTHROUGH, env);
4165 if (ret == 1)
4166 goto peek_stack;
4167 else if (ret < 0)
4168 goto err_free;
4169 }
4170
4171mark_explored:
4172 insn_state[t] = EXPLORED;
4173 if (cur_stack-- <= 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004174 verbose(env, "pop stack internal bug\n");
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004175 ret = -EFAULT;
4176 goto err_free;
4177 }
4178 goto peek_stack;
4179
4180check_state:
4181 for (i = 0; i < insn_cnt; i++) {
4182 if (insn_state[i] != EXPLORED) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004183 verbose(env, "unreachable insn %d\n", i);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004184 ret = -EINVAL;
4185 goto err_free;
4186 }
4187 }
4188 ret = 0; /* cfg looks good */
4189
4190err_free:
4191 kfree(insn_state);
4192 kfree(insn_stack);
4193 return ret;
4194}
4195
Edward Creef1174f72017-08-07 15:26:19 +01004196/* check %cur's range satisfies %old's */
4197static bool range_within(struct bpf_reg_state *old,
4198 struct bpf_reg_state *cur)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004199{
Edward Creeb03c9f92017-08-07 15:26:36 +01004200 return old->umin_value <= cur->umin_value &&
4201 old->umax_value >= cur->umax_value &&
4202 old->smin_value <= cur->smin_value &&
4203 old->smax_value >= cur->smax_value;
Edward Creef1174f72017-08-07 15:26:19 +01004204}
4205
4206/* Maximum number of register states that can exist at once */
4207#define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE)
4208struct idpair {
4209 u32 old;
4210 u32 cur;
4211};
4212
4213/* If in the old state two registers had the same id, then they need to have
4214 * the same id in the new state as well. But that id could be different from
4215 * the old state, so we need to track the mapping from old to new ids.
4216 * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent
4217 * regs with old id 5 must also have new id 9 for the new state to be safe. But
4218 * regs with a different old id could still have new id 9, we don't care about
4219 * that.
4220 * So we look through our idmap to see if this old id has been seen before. If
4221 * so, we require the new id to match; otherwise, we add the id pair to the map.
4222 */
4223static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap)
4224{
4225 unsigned int i;
4226
4227 for (i = 0; i < ID_MAP_SIZE; i++) {
4228 if (!idmap[i].old) {
4229 /* Reached an empty slot; haven't seen this id before */
4230 idmap[i].old = old_id;
4231 idmap[i].cur = cur_id;
4232 return true;
4233 }
4234 if (idmap[i].old == old_id)
4235 return idmap[i].cur == cur_id;
4236 }
4237 /* We ran out of idmap slots, which should be impossible */
4238 WARN_ON_ONCE(1);
4239 return false;
4240}
4241
4242/* Returns true if (rold safe implies rcur safe) */
Edward Cree1b688a12017-08-23 15:10:50 +01004243static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
4244 struct idpair *idmap)
Edward Creef1174f72017-08-07 15:26:19 +01004245{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004246 bool equal;
4247
Edward Creedc503a82017-08-15 20:34:35 +01004248 if (!(rold->live & REG_LIVE_READ))
4249 /* explored state didn't use this */
4250 return true;
4251
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004252 equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, frameno)) == 0;
4253
4254 if (rold->type == PTR_TO_STACK)
4255 /* two stack pointers are equal only if they're pointing to
4256 * the same stack frame, since fp-8 in foo != fp-8 in bar
4257 */
4258 return equal && rold->frameno == rcur->frameno;
4259
4260 if (equal)
Edward Creef1174f72017-08-07 15:26:19 +01004261 return true;
4262
4263 if (rold->type == NOT_INIT)
4264 /* explored state can't have used this */
4265 return true;
4266 if (rcur->type == NOT_INIT)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004267 return false;
Edward Creef1174f72017-08-07 15:26:19 +01004268 switch (rold->type) {
4269 case SCALAR_VALUE:
4270 if (rcur->type == SCALAR_VALUE) {
4271 /* new val must satisfy old val knowledge */
4272 return range_within(rold, rcur) &&
4273 tnum_in(rold->var_off, rcur->var_off);
4274 } else {
Jann Horn179d1c52017-12-18 20:11:59 -08004275 /* We're trying to use a pointer in place of a scalar.
4276 * Even if the scalar was unbounded, this could lead to
4277 * pointer leaks because scalars are allowed to leak
4278 * while pointers are not. We could make this safe in
4279 * special cases if root is calling us, but it's
4280 * probably not worth the hassle.
Edward Creef1174f72017-08-07 15:26:19 +01004281 */
Jann Horn179d1c52017-12-18 20:11:59 -08004282 return false;
Edward Creef1174f72017-08-07 15:26:19 +01004283 }
4284 case PTR_TO_MAP_VALUE:
Edward Cree1b688a12017-08-23 15:10:50 +01004285 /* If the new min/max/var_off satisfy the old ones and
4286 * everything else matches, we are OK.
4287 * We don't care about the 'id' value, because nothing
4288 * uses it for PTR_TO_MAP_VALUE (only for ..._OR_NULL)
4289 */
4290 return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 &&
4291 range_within(rold, rcur) &&
4292 tnum_in(rold->var_off, rcur->var_off);
Edward Creef1174f72017-08-07 15:26:19 +01004293 case PTR_TO_MAP_VALUE_OR_NULL:
4294 /* a PTR_TO_MAP_VALUE could be safe to use as a
4295 * PTR_TO_MAP_VALUE_OR_NULL into the same map.
4296 * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL-
4297 * checked, doing so could have affected others with the same
4298 * id, and we can't check for that because we lost the id when
4299 * we converted to a PTR_TO_MAP_VALUE.
4300 */
4301 if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL)
4302 return false;
4303 if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)))
4304 return false;
4305 /* Check our ids match any regs they're supposed to */
4306 return check_ids(rold->id, rcur->id, idmap);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004307 case PTR_TO_PACKET_META:
Edward Creef1174f72017-08-07 15:26:19 +01004308 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004309 if (rcur->type != rold->type)
Edward Creef1174f72017-08-07 15:26:19 +01004310 return false;
4311 /* We must have at least as much range as the old ptr
4312 * did, so that any accesses which were safe before are
4313 * still safe. This is true even if old range < old off,
4314 * since someone could have accessed through (ptr - k), or
4315 * even done ptr -= k in a register, to get a safe access.
4316 */
4317 if (rold->range > rcur->range)
4318 return false;
4319 /* If the offsets don't match, we can't trust our alignment;
4320 * nor can we be sure that we won't fall out of range.
4321 */
4322 if (rold->off != rcur->off)
4323 return false;
4324 /* id relations must be preserved */
4325 if (rold->id && !check_ids(rold->id, rcur->id, idmap))
4326 return false;
4327 /* new val must satisfy old val knowledge */
4328 return range_within(rold, rcur) &&
4329 tnum_in(rold->var_off, rcur->var_off);
4330 case PTR_TO_CTX:
4331 case CONST_PTR_TO_MAP:
Edward Creef1174f72017-08-07 15:26:19 +01004332 case PTR_TO_PACKET_END:
4333 /* Only valid matches are exact, which memcmp() above
4334 * would have accepted
4335 */
4336 default:
4337 /* Don't know what's going on, just say it's not safe */
4338 return false;
4339 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004340
Edward Creef1174f72017-08-07 15:26:19 +01004341 /* Shouldn't get here; if we do, say it's not safe */
4342 WARN_ON_ONCE(1);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004343 return false;
4344}
4345
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004346static bool stacksafe(struct bpf_func_state *old,
4347 struct bpf_func_state *cur,
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004348 struct idpair *idmap)
4349{
4350 int i, spi;
4351
4352 /* if explored stack has more populated slots than current stack
4353 * such stacks are not equivalent
4354 */
4355 if (old->allocated_stack > cur->allocated_stack)
4356 return false;
4357
4358 /* walk slots of the explored stack and ignore any additional
4359 * slots in the current stack, since explored(safe) state
4360 * didn't use them
4361 */
4362 for (i = 0; i < old->allocated_stack; i++) {
4363 spi = i / BPF_REG_SIZE;
4364
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08004365 if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ))
4366 /* explored state didn't use this */
Gianluca Borellofd05e572017-12-23 10:09:55 +00004367 continue;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08004368
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004369 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID)
4370 continue;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08004371 /* if old state was safe with misc data in the stack
4372 * it will be safe with zero-initialized stack.
4373 * The opposite is not true
4374 */
4375 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC &&
4376 cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO)
4377 continue;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004378 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] !=
4379 cur->stack[spi].slot_type[i % BPF_REG_SIZE])
4380 /* Ex: old explored (safe) state has STACK_SPILL in
4381 * this stack slot, but current has has STACK_MISC ->
4382 * this verifier states are not equivalent,
4383 * return false to continue verification of this path
4384 */
4385 return false;
4386 if (i % BPF_REG_SIZE)
4387 continue;
4388 if (old->stack[spi].slot_type[0] != STACK_SPILL)
4389 continue;
4390 if (!regsafe(&old->stack[spi].spilled_ptr,
4391 &cur->stack[spi].spilled_ptr,
4392 idmap))
4393 /* when explored and current stack slot are both storing
4394 * spilled registers, check that stored pointers types
4395 * are the same as well.
4396 * Ex: explored safe path could have stored
4397 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8}
4398 * but current path has stored:
4399 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16}
4400 * such verifier states are not equivalent.
4401 * return false to continue verification of this path
4402 */
4403 return false;
4404 }
4405 return true;
4406}
4407
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004408/* compare two verifier states
4409 *
4410 * all states stored in state_list are known to be valid, since
4411 * verifier reached 'bpf_exit' instruction through them
4412 *
4413 * this function is called when verifier exploring different branches of
4414 * execution popped from the state stack. If it sees an old state that has
4415 * more strict register state and more strict stack state then this execution
4416 * branch doesn't need to be explored further, since verifier already
4417 * concluded that more strict state leads to valid finish.
4418 *
4419 * Therefore two states are equivalent if register state is more conservative
4420 * and explored stack state is more conservative than the current one.
4421 * Example:
4422 * explored current
4423 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
4424 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
4425 *
4426 * In other words if current stack state (one being explored) has more
4427 * valid slots than old one that already passed validation, it means
4428 * the verifier can stop exploring and conclude that current state is valid too
4429 *
4430 * Similarly with registers. If explored state has register type as invalid
4431 * whereas register type in current state is meaningful, it means that
4432 * the current state will reach 'bpf_exit' instruction safely
4433 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004434static bool func_states_equal(struct bpf_func_state *old,
4435 struct bpf_func_state *cur)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004436{
Edward Creef1174f72017-08-07 15:26:19 +01004437 struct idpair *idmap;
4438 bool ret = false;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004439 int i;
4440
Edward Creef1174f72017-08-07 15:26:19 +01004441 idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL);
4442 /* If we failed to allocate the idmap, just say it's not safe */
4443 if (!idmap)
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07004444 return false;
Edward Creef1174f72017-08-07 15:26:19 +01004445
4446 for (i = 0; i < MAX_BPF_REG; i++) {
Edward Cree1b688a12017-08-23 15:10:50 +01004447 if (!regsafe(&old->regs[i], &cur->regs[i], idmap))
Edward Creef1174f72017-08-07 15:26:19 +01004448 goto out_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004449 }
4450
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004451 if (!stacksafe(old, cur, idmap))
4452 goto out_free;
Edward Creef1174f72017-08-07 15:26:19 +01004453 ret = true;
4454out_free:
4455 kfree(idmap);
4456 return ret;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004457}
4458
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004459static bool states_equal(struct bpf_verifier_env *env,
4460 struct bpf_verifier_state *old,
4461 struct bpf_verifier_state *cur)
Edward Creedc503a82017-08-15 20:34:35 +01004462{
Edward Creedc503a82017-08-15 20:34:35 +01004463 int i;
4464
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004465 if (old->curframe != cur->curframe)
4466 return false;
4467
4468 /* for states to be equal callsites have to be the same
4469 * and all frame states need to be equivalent
4470 */
4471 for (i = 0; i <= old->curframe; i++) {
4472 if (old->frame[i]->callsite != cur->frame[i]->callsite)
4473 return false;
4474 if (!func_states_equal(old->frame[i], cur->frame[i]))
4475 return false;
4476 }
4477 return true;
4478}
4479
4480/* A write screens off any subsequent reads; but write marks come from the
4481 * straight-line code between a state and its parent. When we arrive at an
4482 * equivalent state (jump target or such) we didn't arrive by the straight-line
4483 * code, so read marks in the state must propagate to the parent regardless
4484 * of the state's write marks. That's what 'parent == state->parent' comparison
4485 * in mark_reg_read() and mark_stack_slot_read() is for.
4486 */
4487static int propagate_liveness(struct bpf_verifier_env *env,
4488 const struct bpf_verifier_state *vstate,
4489 struct bpf_verifier_state *vparent)
4490{
4491 int i, frame, err = 0;
4492 struct bpf_func_state *state, *parent;
4493
4494 if (vparent->curframe != vstate->curframe) {
4495 WARN(1, "propagate_live: parent frame %d current frame %d\n",
4496 vparent->curframe, vstate->curframe);
4497 return -EFAULT;
4498 }
Edward Creedc503a82017-08-15 20:34:35 +01004499 /* Propagate read liveness of registers... */
4500 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
4501 /* We don't need to worry about FP liveness because it's read-only */
4502 for (i = 0; i < BPF_REG_FP; i++) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004503 if (vparent->frame[vparent->curframe]->regs[i].live & REG_LIVE_READ)
Edward Creedc503a82017-08-15 20:34:35 +01004504 continue;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004505 if (vstate->frame[vstate->curframe]->regs[i].live & REG_LIVE_READ) {
4506 err = mark_reg_read(env, vstate, vparent, i);
4507 if (err)
4508 return err;
Edward Creedc503a82017-08-15 20:34:35 +01004509 }
4510 }
Edward Creedc503a82017-08-15 20:34:35 +01004511
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004512 /* ... and stack slots */
4513 for (frame = 0; frame <= vstate->curframe; frame++) {
4514 state = vstate->frame[frame];
4515 parent = vparent->frame[frame];
4516 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE &&
4517 i < parent->allocated_stack / BPF_REG_SIZE; i++) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004518 if (parent->stack[i].spilled_ptr.live & REG_LIVE_READ)
4519 continue;
4520 if (state->stack[i].spilled_ptr.live & REG_LIVE_READ)
4521 mark_stack_slot_read(env, vstate, vparent, i, frame);
4522 }
Edward Creedc503a82017-08-15 20:34:35 +01004523 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004524 return err;
Edward Creedc503a82017-08-15 20:34:35 +01004525}
4526
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004527static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004528{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004529 struct bpf_verifier_state_list *new_sl;
4530 struct bpf_verifier_state_list *sl;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004531 struct bpf_verifier_state *cur = env->cur_state;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004532 int i, j, err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004533
4534 sl = env->explored_states[insn_idx];
4535 if (!sl)
4536 /* this 'insn_idx' instruction wasn't marked, so we will not
4537 * be doing state search here
4538 */
4539 return 0;
4540
4541 while (sl != STATE_LIST_MARK) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004542 if (states_equal(env, &sl->state, cur)) {
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004543 /* reached equivalent register/stack state,
Edward Creedc503a82017-08-15 20:34:35 +01004544 * prune the search.
4545 * Registers read by the continuation are read by us.
Edward Cree8e9cd9c2017-08-23 15:11:21 +01004546 * If we have any write marks in env->cur_state, they
4547 * will prevent corresponding reads in the continuation
4548 * from reaching our parent (an explored_state). Our
4549 * own state will get the read marks recorded, but
4550 * they'll be immediately forgotten as we're pruning
4551 * this state and will pop a new one.
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004552 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004553 err = propagate_liveness(env, &sl->state, cur);
4554 if (err)
4555 return err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004556 return 1;
Edward Creedc503a82017-08-15 20:34:35 +01004557 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004558 sl = sl->next;
4559 }
4560
4561 /* there were no equivalent states, remember current one.
4562 * technically the current state is not proven to be safe yet,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004563 * but it will either reach outer most bpf_exit (which means it's safe)
4564 * or it will be rejected. Since there are no loops, we won't be
4565 * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx)
4566 * again on the way to bpf_exit
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004567 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004568 new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004569 if (!new_sl)
4570 return -ENOMEM;
4571
4572 /* add new state to the head of linked list */
Alexei Starovoitov1969db42017-11-01 00:08:04 -07004573 err = copy_verifier_state(&new_sl->state, cur);
4574 if (err) {
4575 free_verifier_state(&new_sl->state, false);
4576 kfree(new_sl);
4577 return err;
4578 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004579 new_sl->next = env->explored_states[insn_idx];
4580 env->explored_states[insn_idx] = new_sl;
Edward Creedc503a82017-08-15 20:34:35 +01004581 /* connect new state to parentage chain */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004582 cur->parent = &new_sl->state;
Edward Cree8e9cd9c2017-08-23 15:11:21 +01004583 /* clear write marks in current state: the writes we did are not writes
4584 * our child did, so they don't screen off its reads from us.
4585 * (There are no read marks in current state, because reads always mark
4586 * their parent and current state never has children yet. Only
4587 * explored_states can get read marks.)
4588 */
Edward Creedc503a82017-08-15 20:34:35 +01004589 for (i = 0; i < BPF_REG_FP; i++)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004590 cur->frame[cur->curframe]->regs[i].live = REG_LIVE_NONE;
4591
4592 /* all stack frames are accessible from callee, clear them all */
4593 for (j = 0; j <= cur->curframe; j++) {
4594 struct bpf_func_state *frame = cur->frame[j];
4595
4596 for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++)
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08004597 frame->stack[i].spilled_ptr.live = REG_LIVE_NONE;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004598 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004599 return 0;
4600}
4601
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004602static int do_check(struct bpf_verifier_env *env)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004603{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004604 struct bpf_verifier_state *state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004605 struct bpf_insn *insns = env->prog->insnsi;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004606 struct bpf_reg_state *regs;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004607 int insn_cnt = env->prog->len, i;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004608 int insn_idx, prev_insn_idx = 0;
4609 int insn_processed = 0;
4610 bool do_print_state = false;
4611
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004612 state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL);
4613 if (!state)
4614 return -ENOMEM;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004615 state->curframe = 0;
Edward Creedc503a82017-08-15 20:34:35 +01004616 state->parent = NULL;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004617 state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL);
4618 if (!state->frame[0]) {
4619 kfree(state);
4620 return -ENOMEM;
4621 }
4622 env->cur_state = state;
4623 init_func_state(env, state->frame[0],
4624 BPF_MAIN_FUNC /* callsite */,
4625 0 /* frameno */,
4626 0 /* subprogno, zero == main subprog */);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004627 insn_idx = 0;
4628 for (;;) {
4629 struct bpf_insn *insn;
4630 u8 class;
4631 int err;
4632
4633 if (insn_idx >= insn_cnt) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004634 verbose(env, "invalid insn idx %d insn_cnt %d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004635 insn_idx, insn_cnt);
4636 return -EFAULT;
4637 }
4638
4639 insn = &insns[insn_idx];
4640 class = BPF_CLASS(insn->code);
4641
Daniel Borkmann07016152016-04-05 22:33:17 +02004642 if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004643 verbose(env,
4644 "BPF program is too large. Processed %d insn\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004645 insn_processed);
4646 return -E2BIG;
4647 }
4648
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004649 err = is_state_visited(env, insn_idx);
4650 if (err < 0)
4651 return err;
4652 if (err == 1) {
4653 /* found equivalent state, can prune the search */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004654 if (env->log.level) {
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004655 if (do_print_state)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004656 verbose(env, "\nfrom %d to %d: safe\n",
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004657 prev_insn_idx, insn_idx);
4658 else
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004659 verbose(env, "%d: safe\n", insn_idx);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004660 }
4661 goto process_bpf_exit;
4662 }
4663
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02004664 if (need_resched())
4665 cond_resched();
4666
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004667 if (env->log.level > 1 || (env->log.level && do_print_state)) {
4668 if (env->log.level > 1)
4669 verbose(env, "%d:", insn_idx);
David S. Millerc5fc9692017-05-10 11:25:17 -07004670 else
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004671 verbose(env, "\nfrom %d to %d:",
David S. Millerc5fc9692017-05-10 11:25:17 -07004672 prev_insn_idx, insn_idx);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004673 print_verifier_state(env, state->frame[state->curframe]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004674 do_print_state = false;
4675 }
4676
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004677 if (env->log.level) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01004678 const struct bpf_insn_cbs cbs = {
4679 .cb_print = verbose,
Jiri Olsaabe08842018-03-23 11:41:28 +01004680 .private_data = env,
Daniel Borkmann7105e822017-12-20 13:42:57 +01004681 };
4682
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004683 verbose(env, "%d: ", insn_idx);
Jiri Olsaabe08842018-03-23 11:41:28 +01004684 print_bpf_insn(&cbs, insn, env->allow_ptr_leaks);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004685 }
4686
Jakub Kicinskicae19272017-12-27 18:39:05 -08004687 if (bpf_prog_is_dev_bound(env->prog->aux)) {
4688 err = bpf_prog_offload_verify_insn(env, insn_idx,
4689 prev_insn_idx);
4690 if (err)
4691 return err;
4692 }
Jakub Kicinski13a27df2016-09-21 11:43:58 +01004693
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004694 regs = cur_regs(env);
Alexei Starovoitovc1311872017-11-22 16:42:05 -08004695 env->insn_aux_data[insn_idx].seen = true;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004696 if (class == BPF_ALU || class == BPF_ALU64) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004697 err = check_alu_op(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004698 if (err)
4699 return err;
4700
4701 } else if (class == BPF_LDX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004702 enum bpf_reg_type *prev_src_type, src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004703
4704 /* check for reserved fields is already done */
4705
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004706 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01004707 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004708 if (err)
4709 return err;
4710
Edward Creedc503a82017-08-15 20:34:35 +01004711 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004712 if (err)
4713 return err;
4714
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07004715 src_reg_type = regs[insn->src_reg].type;
4716
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004717 /* check that memory (src_reg + off) is readable,
4718 * the state of dst_reg will be updated by this func
4719 */
Yonghong Song31fd8582017-06-13 15:52:13 -07004720 err = check_mem_access(env, insn_idx, insn->src_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004721 BPF_SIZE(insn->code), BPF_READ,
Daniel Borkmannca369602018-02-23 22:29:05 +01004722 insn->dst_reg, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004723 if (err)
4724 return err;
4725
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004726 prev_src_type = &env->insn_aux_data[insn_idx].ptr_type;
4727
4728 if (*prev_src_type == NOT_INIT) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004729 /* saw a valid insn
4730 * dst_reg = *(u32 *)(src_reg + off)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004731 * save type to validate intersecting paths
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004732 */
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004733 *prev_src_type = src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004734
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004735 } else if (src_reg_type != *prev_src_type &&
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004736 (src_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004737 *prev_src_type == PTR_TO_CTX)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004738 /* ABuser program is trying to use the same insn
4739 * dst_reg = *(u32*) (src_reg + off)
4740 * with different pointer types:
4741 * src_reg == ctx in one branch and
4742 * src_reg == stack|map in some other branch.
4743 * Reject it.
4744 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004745 verbose(env, "same insn cannot be used with different pointers\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004746 return -EINVAL;
4747 }
4748
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004749 } else if (class == BPF_STX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004750 enum bpf_reg_type *prev_dst_type, dst_reg_type;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004751
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004752 if (BPF_MODE(insn->code) == BPF_XADD) {
Yonghong Song31fd8582017-06-13 15:52:13 -07004753 err = check_xadd(env, insn_idx, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004754 if (err)
4755 return err;
4756 insn_idx++;
4757 continue;
4758 }
4759
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004760 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01004761 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004762 if (err)
4763 return err;
4764 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01004765 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004766 if (err)
4767 return err;
4768
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004769 dst_reg_type = regs[insn->dst_reg].type;
4770
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004771 /* check that memory (dst_reg + off) is writeable */
Yonghong Song31fd8582017-06-13 15:52:13 -07004772 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004773 BPF_SIZE(insn->code), BPF_WRITE,
Daniel Borkmannca369602018-02-23 22:29:05 +01004774 insn->src_reg, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004775 if (err)
4776 return err;
4777
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004778 prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type;
4779
4780 if (*prev_dst_type == NOT_INIT) {
4781 *prev_dst_type = dst_reg_type;
4782 } else if (dst_reg_type != *prev_dst_type &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004783 (dst_reg_type == PTR_TO_CTX ||
Jakub Kicinski3df126f2016-09-21 11:43:56 +01004784 *prev_dst_type == PTR_TO_CTX)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004785 verbose(env, "same insn cannot be used with different pointers\n");
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004786 return -EINVAL;
4787 }
4788
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004789 } else if (class == BPF_ST) {
4790 if (BPF_MODE(insn->code) != BPF_MEM ||
4791 insn->src_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004792 verbose(env, "BPF_ST uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004793 return -EINVAL;
4794 }
4795 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01004796 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004797 if (err)
4798 return err;
4799
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01004800 if (is_ctx_reg(env, insn->dst_reg)) {
4801 verbose(env, "BPF_ST stores into R%d context is not allowed\n",
4802 insn->dst_reg);
4803 return -EACCES;
4804 }
4805
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004806 /* check that memory (dst_reg + off) is writeable */
Yonghong Song31fd8582017-06-13 15:52:13 -07004807 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004808 BPF_SIZE(insn->code), BPF_WRITE,
Daniel Borkmannca369602018-02-23 22:29:05 +01004809 -1, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004810 if (err)
4811 return err;
4812
4813 } else if (class == BPF_JMP) {
4814 u8 opcode = BPF_OP(insn->code);
4815
4816 if (opcode == BPF_CALL) {
4817 if (BPF_SRC(insn->code) != BPF_K ||
4818 insn->off != 0 ||
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004819 (insn->src_reg != BPF_REG_0 &&
4820 insn->src_reg != BPF_PSEUDO_CALL) ||
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004821 insn->dst_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004822 verbose(env, "BPF_CALL uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004823 return -EINVAL;
4824 }
4825
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004826 if (insn->src_reg == BPF_PSEUDO_CALL)
4827 err = check_func_call(env, insn, &insn_idx);
4828 else
4829 err = check_helper_call(env, insn->imm, insn_idx);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004830 if (err)
4831 return err;
4832
4833 } else if (opcode == BPF_JA) {
4834 if (BPF_SRC(insn->code) != BPF_K ||
4835 insn->imm != 0 ||
4836 insn->src_reg != BPF_REG_0 ||
4837 insn->dst_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004838 verbose(env, "BPF_JA uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004839 return -EINVAL;
4840 }
4841
4842 insn_idx += insn->off + 1;
4843 continue;
4844
4845 } else if (opcode == BPF_EXIT) {
4846 if (BPF_SRC(insn->code) != BPF_K ||
4847 insn->imm != 0 ||
4848 insn->src_reg != BPF_REG_0 ||
4849 insn->dst_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004850 verbose(env, "BPF_EXIT uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004851 return -EINVAL;
4852 }
4853
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004854 if (state->curframe) {
4855 /* exit from nested function */
4856 prev_insn_idx = insn_idx;
4857 err = prepare_func_exit(env, &insn_idx);
4858 if (err)
4859 return err;
4860 do_print_state = true;
4861 continue;
4862 }
4863
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004864 /* eBPF calling convetion is such that R0 is used
4865 * to return the value from eBPF program.
4866 * Make sure that it's readable at this time
4867 * of bpf_exit, which means that program wrote
4868 * something into it earlier
4869 */
Edward Creedc503a82017-08-15 20:34:35 +01004870 err = check_reg_arg(env, BPF_REG_0, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004871 if (err)
4872 return err;
4873
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004874 if (is_pointer_value(env, BPF_REG_0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004875 verbose(env, "R0 leaks addr as return value\n");
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004876 return -EACCES;
4877 }
4878
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07004879 err = check_return_code(env);
4880 if (err)
4881 return err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004882process_bpf_exit:
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004883 err = pop_stack(env, &prev_insn_idx, &insn_idx);
4884 if (err < 0) {
4885 if (err != -ENOENT)
4886 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004887 break;
4888 } else {
4889 do_print_state = true;
4890 continue;
4891 }
4892 } else {
4893 err = check_cond_jmp_op(env, insn, &insn_idx);
4894 if (err)
4895 return err;
4896 }
4897 } else if (class == BPF_LD) {
4898 u8 mode = BPF_MODE(insn->code);
4899
4900 if (mode == BPF_ABS || mode == BPF_IND) {
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004901 err = check_ld_abs(env, insn);
4902 if (err)
4903 return err;
4904
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004905 } else if (mode == BPF_IMM) {
4906 err = check_ld_imm(env, insn);
4907 if (err)
4908 return err;
4909
4910 insn_idx++;
Alexei Starovoitovc1311872017-11-22 16:42:05 -08004911 env->insn_aux_data[insn_idx].seen = true;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004912 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004913 verbose(env, "invalid BPF_LD mode\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004914 return -EINVAL;
4915 }
4916 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004917 verbose(env, "unknown insn class %d\n", class);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004918 return -EINVAL;
4919 }
4920
4921 insn_idx++;
4922 }
4923
Daniel Borkmann4bd95f42018-01-20 01:24:36 +01004924 verbose(env, "processed %d insns (limit %d), stack depth ",
4925 insn_processed, BPF_COMPLEXITY_LIMIT_INSNS);
Jiong Wangf910cef2018-05-02 16:17:17 -04004926 for (i = 0; i < env->subprog_cnt; i++) {
Jiong Wang9c8105b2018-05-02 16:17:18 -04004927 u32 depth = env->subprog_info[i].stack_depth;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004928
4929 verbose(env, "%d", depth);
Jiong Wangf910cef2018-05-02 16:17:17 -04004930 if (i + 1 < env->subprog_cnt)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004931 verbose(env, "+");
4932 }
4933 verbose(env, "\n");
Jiong Wang9c8105b2018-05-02 16:17:18 -04004934 env->prog->aux->stack_depth = env->subprog_info[0].stack_depth;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004935 return 0;
4936}
4937
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004938static int check_map_prealloc(struct bpf_map *map)
4939{
4940 return (map->map_type != BPF_MAP_TYPE_HASH &&
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07004941 map->map_type != BPF_MAP_TYPE_PERCPU_HASH &&
4942 map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) ||
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004943 !(map->map_flags & BPF_F_NO_PREALLOC);
4944}
4945
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004946static int check_map_prog_compatibility(struct bpf_verifier_env *env,
4947 struct bpf_map *map,
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07004948 struct bpf_prog *prog)
4949
4950{
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004951 /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use
4952 * preallocated hash maps, since doing memory allocation
4953 * in overflow_handler can crash depending on where nmi got
4954 * triggered.
4955 */
4956 if (prog->type == BPF_PROG_TYPE_PERF_EVENT) {
4957 if (!check_map_prealloc(map)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004958 verbose(env, "perf_event programs can only use preallocated hash map\n");
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004959 return -EINVAL;
4960 }
4961 if (map->inner_map_meta &&
4962 !check_map_prealloc(map->inner_map_meta)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004963 verbose(env, "perf_event programs can only use preallocated inner hash map\n");
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004964 return -EINVAL;
4965 }
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07004966 }
Jakub Kicinskia3884572018-01-11 20:29:09 -08004967
4968 if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) &&
4969 !bpf_offload_dev_match(prog, map)) {
4970 verbose(env, "offload device mismatch between prog and map\n");
4971 return -EINVAL;
4972 }
4973
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07004974 return 0;
4975}
4976
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004977/* look for pseudo eBPF instructions that access map FDs and
4978 * replace them with actual map pointers
4979 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004980static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004981{
4982 struct bpf_insn *insn = env->prog->insnsi;
4983 int insn_cnt = env->prog->len;
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07004984 int i, j, err;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004985
Daniel Borkmannf1f77142017-01-13 23:38:15 +01004986 err = bpf_prog_calc_tag(env->prog);
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01004987 if (err)
4988 return err;
4989
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004990 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004991 if (BPF_CLASS(insn->code) == BPF_LDX &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004992 (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004993 verbose(env, "BPF_LDX uses reserved fields\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07004994 return -EINVAL;
4995 }
4996
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07004997 if (BPF_CLASS(insn->code) == BPF_STX &&
4998 ((BPF_MODE(insn->code) != BPF_MEM &&
4999 BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005000 verbose(env, "BPF_STX uses reserved fields\n");
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07005001 return -EINVAL;
5002 }
5003
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005004 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
5005 struct bpf_map *map;
5006 struct fd f;
5007
5008 if (i == insn_cnt - 1 || insn[1].code != 0 ||
5009 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
5010 insn[1].off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005011 verbose(env, "invalid bpf_ld_imm64 insn\n");
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005012 return -EINVAL;
5013 }
5014
5015 if (insn->src_reg == 0)
5016 /* valid generic load 64-bit imm */
5017 goto next_insn;
5018
5019 if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005020 verbose(env,
5021 "unrecognized bpf_ld_imm64 insn\n");
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005022 return -EINVAL;
5023 }
5024
5025 f = fdget(insn->imm);
Daniel Borkmannc2101292015-10-29 14:58:07 +01005026 map = __bpf_map_get(f);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005027 if (IS_ERR(map)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005028 verbose(env, "fd %d is not pointing to valid bpf_map\n",
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005029 insn->imm);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005030 return PTR_ERR(map);
5031 }
5032
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005033 err = check_map_prog_compatibility(env, map, env->prog);
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07005034 if (err) {
5035 fdput(f);
5036 return err;
5037 }
5038
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005039 /* store map pointer inside BPF_LD_IMM64 instruction */
5040 insn[0].imm = (u32) (unsigned long) map;
5041 insn[1].imm = ((u64) (unsigned long) map) >> 32;
5042
5043 /* check whether we recorded this map already */
5044 for (j = 0; j < env->used_map_cnt; j++)
5045 if (env->used_maps[j] == map) {
5046 fdput(f);
5047 goto next_insn;
5048 }
5049
5050 if (env->used_map_cnt >= MAX_USED_MAPS) {
5051 fdput(f);
5052 return -E2BIG;
5053 }
5054
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005055 /* hold the map. If the program is rejected by verifier,
5056 * the map will be released by release_maps() or it
5057 * will be used by the valid program until it's unloaded
5058 * and all maps are released in free_bpf_prog_info()
5059 */
Alexei Starovoitov92117d82016-04-27 18:56:20 -07005060 map = bpf_map_inc(map, false);
5061 if (IS_ERR(map)) {
5062 fdput(f);
5063 return PTR_ERR(map);
5064 }
5065 env->used_maps[env->used_map_cnt++] = map;
5066
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005067 fdput(f);
5068next_insn:
5069 insn++;
5070 i++;
Daniel Borkmann5e581da2018-01-26 23:33:38 +01005071 continue;
5072 }
5073
5074 /* Basic sanity check before we invest more work here. */
5075 if (!bpf_opcode_in_insntable(insn->code)) {
5076 verbose(env, "unknown opcode %02x\n", insn->code);
5077 return -EINVAL;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005078 }
5079 }
5080
5081 /* now all pseudo BPF_LD_IMM64 instructions load valid
5082 * 'struct bpf_map *' into a register instead of user map_fd.
5083 * These pointers will be used later by verifier to validate map access.
5084 */
5085 return 0;
5086}
5087
5088/* drop refcnt of maps used by the rejected program */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005089static void release_maps(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005090{
5091 int i;
5092
5093 for (i = 0; i < env->used_map_cnt; i++)
5094 bpf_map_put(env->used_maps[i]);
5095}
5096
5097/* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005098static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005099{
5100 struct bpf_insn *insn = env->prog->insnsi;
5101 int insn_cnt = env->prog->len;
5102 int i;
5103
5104 for (i = 0; i < insn_cnt; i++, insn++)
5105 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
5106 insn->src_reg = 0;
5107}
5108
Alexei Starovoitov80419022017-03-15 18:26:41 -07005109/* single env->prog->insni[off] instruction was replaced with the range
5110 * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
5111 * [0, off) and [off, end) to new locations, so the patched range stays zero
5112 */
5113static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len,
5114 u32 off, u32 cnt)
5115{
5116 struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data;
Alexei Starovoitovc1311872017-11-22 16:42:05 -08005117 int i;
Alexei Starovoitov80419022017-03-15 18:26:41 -07005118
5119 if (cnt == 1)
5120 return 0;
5121 new_data = vzalloc(sizeof(struct bpf_insn_aux_data) * prog_len);
5122 if (!new_data)
5123 return -ENOMEM;
5124 memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off);
5125 memcpy(new_data + off + cnt - 1, old_data + off,
5126 sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
Alexei Starovoitovc1311872017-11-22 16:42:05 -08005127 for (i = off; i < off + cnt - 1; i++)
5128 new_data[i].seen = true;
Alexei Starovoitov80419022017-03-15 18:26:41 -07005129 env->insn_aux_data = new_data;
5130 vfree(old_data);
5131 return 0;
5132}
5133
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08005134static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len)
5135{
5136 int i;
5137
5138 if (len == 1)
5139 return;
Jiong Wang4cb3d992018-05-02 16:17:19 -04005140 /* NOTE: fake 'exit' subprog should be updated as well. */
5141 for (i = 0; i <= env->subprog_cnt; i++) {
Jiong Wang9c8105b2018-05-02 16:17:18 -04005142 if (env->subprog_info[i].start < off)
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08005143 continue;
Jiong Wang9c8105b2018-05-02 16:17:18 -04005144 env->subprog_info[i].start += len - 1;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08005145 }
5146}
5147
Alexei Starovoitov80419022017-03-15 18:26:41 -07005148static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
5149 const struct bpf_insn *patch, u32 len)
5150{
5151 struct bpf_prog *new_prog;
5152
5153 new_prog = bpf_patch_insn_single(env->prog, off, patch, len);
5154 if (!new_prog)
5155 return NULL;
5156 if (adjust_insn_aux_data(env, new_prog->len, off, len))
5157 return NULL;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08005158 adjust_subprog_starts(env, off, len);
Alexei Starovoitov80419022017-03-15 18:26:41 -07005159 return new_prog;
5160}
5161
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01005162/* The verifier does more data flow analysis than llvm and will not
5163 * explore branches that are dead at run time. Malicious programs can
5164 * have dead code too. Therefore replace all dead at-run-time code
5165 * with 'ja -1'.
5166 *
5167 * Just nops are not optimal, e.g. if they would sit at the end of the
5168 * program and through another bug we would manage to jump there, then
5169 * we'd execute beyond program memory otherwise. Returning exception
5170 * code also wouldn't work since we can have subprogs where the dead
5171 * code could be located.
Alexei Starovoitovc1311872017-11-22 16:42:05 -08005172 */
5173static void sanitize_dead_code(struct bpf_verifier_env *env)
5174{
5175 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01005176 struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1);
Alexei Starovoitovc1311872017-11-22 16:42:05 -08005177 struct bpf_insn *insn = env->prog->insnsi;
5178 const int insn_cnt = env->prog->len;
5179 int i;
5180
5181 for (i = 0; i < insn_cnt; i++) {
5182 if (aux_data[i].seen)
5183 continue;
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01005184 memcpy(insn + i, &trap, sizeof(trap));
Alexei Starovoitovc1311872017-11-22 16:42:05 -08005185 }
5186}
5187
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005188/* convert load instructions that access fields of 'struct __sk_buff'
5189 * into sequence of instructions that access fields of 'struct sk_buff'
5190 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005191static int convert_ctx_accesses(struct bpf_verifier_env *env)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005192{
Jakub Kicinski00176a32017-10-16 16:40:54 -07005193 const struct bpf_verifier_ops *ops = env->ops;
Daniel Borkmannf96da092017-07-02 02:13:27 +02005194 int i, cnt, size, ctx_field_size, delta = 0;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005195 const int insn_cnt = env->prog->len;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02005196 struct bpf_insn insn_buf[16], *insn;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005197 struct bpf_prog *new_prog;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07005198 enum bpf_access_type type;
Daniel Borkmannf96da092017-07-02 02:13:27 +02005199 bool is_narrower_load;
5200 u32 target_size;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005201
Daniel Borkmann36bbef52016-09-20 00:26:13 +02005202 if (ops->gen_prologue) {
5203 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
5204 env->prog);
5205 if (cnt >= ARRAY_SIZE(insn_buf)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005206 verbose(env, "bpf verifier is misconfigured\n");
Daniel Borkmann36bbef52016-09-20 00:26:13 +02005207 return -EINVAL;
5208 } else if (cnt) {
Alexei Starovoitov80419022017-03-15 18:26:41 -07005209 new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
Daniel Borkmann36bbef52016-09-20 00:26:13 +02005210 if (!new_prog)
5211 return -ENOMEM;
Alexei Starovoitov80419022017-03-15 18:26:41 -07005212
Daniel Borkmann36bbef52016-09-20 00:26:13 +02005213 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005214 delta += cnt - 1;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02005215 }
5216 }
5217
5218 if (!ops->convert_ctx_access)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005219 return 0;
5220
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005221 insn = env->prog->insnsi + delta;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02005222
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005223 for (i = 0; i < insn_cnt; i++, insn++) {
Daniel Borkmann62c79892017-01-12 11:51:33 +01005224 if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) ||
5225 insn->code == (BPF_LDX | BPF_MEM | BPF_H) ||
5226 insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07005227 insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07005228 type = BPF_READ;
Daniel Borkmann62c79892017-01-12 11:51:33 +01005229 else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) ||
5230 insn->code == (BPF_STX | BPF_MEM | BPF_H) ||
5231 insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07005232 insn->code == (BPF_STX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07005233 type = BPF_WRITE;
5234 else
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005235 continue;
5236
Alexei Starovoitov80419022017-03-15 18:26:41 -07005237 if (env->insn_aux_data[i + delta].ptr_type != PTR_TO_CTX)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005238 continue;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005239
Yonghong Song31fd8582017-06-13 15:52:13 -07005240 ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size;
Daniel Borkmannf96da092017-07-02 02:13:27 +02005241 size = BPF_LDST_BYTES(insn);
Yonghong Song31fd8582017-06-13 15:52:13 -07005242
5243 /* If the read access is a narrower load of the field,
5244 * convert to a 4/8-byte load, to minimum program type specific
5245 * convert_ctx_access changes. If conversion is successful,
5246 * we will apply proper mask to the result.
5247 */
Daniel Borkmannf96da092017-07-02 02:13:27 +02005248 is_narrower_load = size < ctx_field_size;
Yonghong Song31fd8582017-06-13 15:52:13 -07005249 if (is_narrower_load) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02005250 u32 off = insn->off;
5251 u8 size_code;
Yonghong Song31fd8582017-06-13 15:52:13 -07005252
Daniel Borkmannf96da092017-07-02 02:13:27 +02005253 if (type == BPF_WRITE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005254 verbose(env, "bpf verifier narrow ctx access misconfigured\n");
Daniel Borkmannf96da092017-07-02 02:13:27 +02005255 return -EINVAL;
5256 }
5257
5258 size_code = BPF_H;
Yonghong Song31fd8582017-06-13 15:52:13 -07005259 if (ctx_field_size == 4)
5260 size_code = BPF_W;
5261 else if (ctx_field_size == 8)
5262 size_code = BPF_DW;
Daniel Borkmannf96da092017-07-02 02:13:27 +02005263
Yonghong Song31fd8582017-06-13 15:52:13 -07005264 insn->off = off & ~(ctx_field_size - 1);
5265 insn->code = BPF_LDX | BPF_MEM | size_code;
5266 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02005267
5268 target_size = 0;
5269 cnt = ops->convert_ctx_access(type, insn, insn_buf, env->prog,
5270 &target_size);
5271 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) ||
5272 (ctx_field_size && !target_size)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005273 verbose(env, "bpf verifier is misconfigured\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005274 return -EINVAL;
5275 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02005276
5277 if (is_narrower_load && size < target_size) {
Yonghong Song31fd8582017-06-13 15:52:13 -07005278 if (ctx_field_size <= 4)
5279 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02005280 (1 << size * 8) - 1);
Yonghong Song31fd8582017-06-13 15:52:13 -07005281 else
5282 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02005283 (1 << size * 8) - 1);
Yonghong Song31fd8582017-06-13 15:52:13 -07005284 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005285
Alexei Starovoitov80419022017-03-15 18:26:41 -07005286 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005287 if (!new_prog)
5288 return -ENOMEM;
5289
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005290 delta += cnt - 1;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005291
5292 /* keep walking new program and skip insns we just inserted */
5293 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005294 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005295 }
5296
5297 return 0;
5298}
5299
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005300static int jit_subprogs(struct bpf_verifier_env *env)
5301{
5302 struct bpf_prog *prog = env->prog, **func, *tmp;
5303 int i, j, subprog_start, subprog_end = 0, len, subprog;
Daniel Borkmann7105e822017-12-20 13:42:57 +01005304 struct bpf_insn *insn;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005305 void *old_bpf_func;
5306 int err = -ENOMEM;
5307
Jiong Wangf910cef2018-05-02 16:17:17 -04005308 if (env->subprog_cnt <= 1)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005309 return 0;
5310
Daniel Borkmann7105e822017-12-20 13:42:57 +01005311 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005312 if (insn->code != (BPF_JMP | BPF_CALL) ||
5313 insn->src_reg != BPF_PSEUDO_CALL)
5314 continue;
5315 subprog = find_subprog(env, i + insn->imm + 1);
5316 if (subprog < 0) {
5317 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
5318 i + insn->imm + 1);
5319 return -EFAULT;
5320 }
5321 /* temporarily remember subprog id inside insn instead of
5322 * aux_data, since next loop will split up all insns into funcs
5323 */
Jiong Wangf910cef2018-05-02 16:17:17 -04005324 insn->off = subprog;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005325 /* remember original imm in case JIT fails and fallback
5326 * to interpreter will be needed
5327 */
5328 env->insn_aux_data[i].call_imm = insn->imm;
5329 /* point imm to __bpf_call_base+1 from JITs point of view */
5330 insn->imm = 1;
5331 }
5332
Jiong Wangf910cef2018-05-02 16:17:17 -04005333 func = kzalloc(sizeof(prog) * env->subprog_cnt, GFP_KERNEL);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005334 if (!func)
5335 return -ENOMEM;
5336
Jiong Wangf910cef2018-05-02 16:17:17 -04005337 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005338 subprog_start = subprog_end;
Jiong Wang4cb3d992018-05-02 16:17:19 -04005339 subprog_end = env->subprog_info[i + 1].start;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005340
5341 len = subprog_end - subprog_start;
5342 func[i] = bpf_prog_alloc(bpf_prog_size(len), GFP_USER);
5343 if (!func[i])
5344 goto out_free;
5345 memcpy(func[i]->insnsi, &prog->insnsi[subprog_start],
5346 len * sizeof(struct bpf_insn));
Daniel Borkmann4f74d802017-12-20 13:42:56 +01005347 func[i]->type = prog->type;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005348 func[i]->len = len;
Daniel Borkmann4f74d802017-12-20 13:42:56 +01005349 if (bpf_prog_calc_tag(func[i]))
5350 goto out_free;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005351 func[i]->is_func = 1;
5352 /* Use bpf_prog_F_tag to indicate functions in stack traces.
5353 * Long term would need debug info to populate names
5354 */
5355 func[i]->aux->name[0] = 'F';
Jiong Wang9c8105b2018-05-02 16:17:18 -04005356 func[i]->aux->stack_depth = env->subprog_info[i].stack_depth;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005357 func[i]->jit_requested = 1;
5358 func[i] = bpf_int_jit_compile(func[i]);
5359 if (!func[i]->jited) {
5360 err = -ENOTSUPP;
5361 goto out_free;
5362 }
5363 cond_resched();
5364 }
5365 /* at this point all bpf functions were successfully JITed
5366 * now populate all bpf_calls with correct addresses and
5367 * run last pass of JIT
5368 */
Jiong Wangf910cef2018-05-02 16:17:17 -04005369 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005370 insn = func[i]->insnsi;
5371 for (j = 0; j < func[i]->len; j++, insn++) {
5372 if (insn->code != (BPF_JMP | BPF_CALL) ||
5373 insn->src_reg != BPF_PSEUDO_CALL)
5374 continue;
5375 subprog = insn->off;
5376 insn->off = 0;
5377 insn->imm = (u64 (*)(u64, u64, u64, u64, u64))
5378 func[subprog]->bpf_func -
5379 __bpf_call_base;
5380 }
5381 }
Jiong Wangf910cef2018-05-02 16:17:17 -04005382 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005383 old_bpf_func = func[i]->bpf_func;
5384 tmp = bpf_int_jit_compile(func[i]);
5385 if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) {
5386 verbose(env, "JIT doesn't support bpf-to-bpf calls\n");
5387 err = -EFAULT;
5388 goto out_free;
5389 }
5390 cond_resched();
5391 }
5392
5393 /* finally lock prog and jit images for all functions and
5394 * populate kallsysm
5395 */
Jiong Wangf910cef2018-05-02 16:17:17 -04005396 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005397 bpf_prog_lock_ro(func[i]);
5398 bpf_prog_kallsyms_add(func[i]);
5399 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01005400
5401 /* Last step: make now unused interpreter insns from main
5402 * prog consistent for later dump requests, so they can
5403 * later look the same as if they were interpreted only.
5404 */
5405 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
5406 unsigned long addr;
5407
5408 if (insn->code != (BPF_JMP | BPF_CALL) ||
5409 insn->src_reg != BPF_PSEUDO_CALL)
5410 continue;
5411 insn->off = env->insn_aux_data[i].call_imm;
5412 subprog = find_subprog(env, i + insn->off + 1);
Jiong Wangf910cef2018-05-02 16:17:17 -04005413 addr = (unsigned long)func[subprog]->bpf_func;
Daniel Borkmann7105e822017-12-20 13:42:57 +01005414 addr &= PAGE_MASK;
5415 insn->imm = (u64 (*)(u64, u64, u64, u64, u64))
5416 addr - __bpf_call_base;
5417 }
5418
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005419 prog->jited = 1;
5420 prog->bpf_func = func[0]->bpf_func;
5421 prog->aux->func = func;
Jiong Wangf910cef2018-05-02 16:17:17 -04005422 prog->aux->func_cnt = env->subprog_cnt;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005423 return 0;
5424out_free:
Jiong Wangf910cef2018-05-02 16:17:17 -04005425 for (i = 0; i < env->subprog_cnt; i++)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005426 if (func[i])
5427 bpf_jit_free(func[i]);
5428 kfree(func);
5429 /* cleanup main prog to be interpreted */
5430 prog->jit_requested = 0;
5431 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
5432 if (insn->code != (BPF_JMP | BPF_CALL) ||
5433 insn->src_reg != BPF_PSEUDO_CALL)
5434 continue;
5435 insn->off = 0;
5436 insn->imm = env->insn_aux_data[i].call_imm;
5437 }
5438 return err;
5439}
5440
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08005441static int fixup_call_args(struct bpf_verifier_env *env)
5442{
David S. Miller19d28fb2018-01-11 21:27:54 -05005443#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08005444 struct bpf_prog *prog = env->prog;
5445 struct bpf_insn *insn = prog->insnsi;
5446 int i, depth;
David S. Miller19d28fb2018-01-11 21:27:54 -05005447#endif
5448 int err;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08005449
David S. Miller19d28fb2018-01-11 21:27:54 -05005450 err = 0;
5451 if (env->prog->jit_requested) {
5452 err = jit_subprogs(env);
5453 if (err == 0)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08005454 return 0;
David S. Miller19d28fb2018-01-11 21:27:54 -05005455 }
5456#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08005457 for (i = 0; i < prog->len; i++, insn++) {
5458 if (insn->code != (BPF_JMP | BPF_CALL) ||
5459 insn->src_reg != BPF_PSEUDO_CALL)
5460 continue;
5461 depth = get_callee_stack_depth(env, insn, i);
5462 if (depth < 0)
5463 return depth;
5464 bpf_patch_call_args(insn, depth);
5465 }
David S. Miller19d28fb2018-01-11 21:27:54 -05005466 err = 0;
5467#endif
5468 return err;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08005469}
5470
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005471/* fixup insn->imm field of bpf_call instructions
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07005472 * and inline eligible helpers as explicit sequence of BPF instructions
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005473 *
5474 * this function is called after eBPF program passed verification
5475 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005476static int fixup_bpf_calls(struct bpf_verifier_env *env)
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005477{
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005478 struct bpf_prog *prog = env->prog;
5479 struct bpf_insn *insn = prog->insnsi;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005480 const struct bpf_func_proto *fn;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005481 const int insn_cnt = prog->len;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07005482 struct bpf_insn insn_buf[16];
5483 struct bpf_prog *new_prog;
5484 struct bpf_map *map_ptr;
5485 int i, cnt, delta = 0;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005486
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005487 for (i = 0; i < insn_cnt; i++, insn++) {
Daniel Borkmannf6b1b3b2018-01-26 23:33:39 +01005488 if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) ||
5489 insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) ||
5490 insn->code == (BPF_ALU | BPF_MOD | BPF_X) ||
Alexei Starovoitov68fda452018-01-12 18:59:52 -08005491 insn->code == (BPF_ALU | BPF_DIV | BPF_X)) {
Daniel Borkmannf6b1b3b2018-01-26 23:33:39 +01005492 bool is64 = BPF_CLASS(insn->code) == BPF_ALU64;
5493 struct bpf_insn mask_and_div[] = {
5494 BPF_MOV32_REG(insn->src_reg, insn->src_reg),
5495 /* Rx div 0 -> 0 */
5496 BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2),
5497 BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg),
5498 BPF_JMP_IMM(BPF_JA, 0, 0, 1),
5499 *insn,
5500 };
5501 struct bpf_insn mask_and_mod[] = {
5502 BPF_MOV32_REG(insn->src_reg, insn->src_reg),
5503 /* Rx mod 0 -> Rx */
5504 BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1),
5505 *insn,
5506 };
5507 struct bpf_insn *patchlet;
5508
5509 if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) ||
5510 insn->code == (BPF_ALU | BPF_DIV | BPF_X)) {
5511 patchlet = mask_and_div + (is64 ? 1 : 0);
5512 cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0);
5513 } else {
5514 patchlet = mask_and_mod + (is64 ? 1 : 0);
5515 cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0);
5516 }
5517
5518 new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt);
Alexei Starovoitov68fda452018-01-12 18:59:52 -08005519 if (!new_prog)
5520 return -ENOMEM;
5521
5522 delta += cnt - 1;
5523 env->prog = prog = new_prog;
5524 insn = new_prog->insnsi + i + delta;
5525 continue;
5526 }
5527
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +02005528 if (BPF_CLASS(insn->code) == BPF_LD &&
5529 (BPF_MODE(insn->code) == BPF_ABS ||
5530 BPF_MODE(insn->code) == BPF_IND)) {
5531 cnt = env->ops->gen_ld_abs(insn, insn_buf);
5532 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
5533 verbose(env, "bpf verifier is misconfigured\n");
5534 return -EINVAL;
5535 }
5536
5537 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
5538 if (!new_prog)
5539 return -ENOMEM;
5540
5541 delta += cnt - 1;
5542 env->prog = prog = new_prog;
5543 insn = new_prog->insnsi + i + delta;
5544 continue;
5545 }
5546
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005547 if (insn->code != (BPF_JMP | BPF_CALL))
5548 continue;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08005549 if (insn->src_reg == BPF_PSEUDO_CALL)
5550 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005551
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005552 if (insn->imm == BPF_FUNC_get_route_realm)
5553 prog->dst_needed = 1;
5554 if (insn->imm == BPF_FUNC_get_prandom_u32)
5555 bpf_user_rnd_init_once();
Josef Bacik9802d862017-12-11 11:36:48 -05005556 if (insn->imm == BPF_FUNC_override_return)
5557 prog->kprobe_override = 1;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005558 if (insn->imm == BPF_FUNC_tail_call) {
David S. Miller7b9f6da2017-04-20 10:35:33 -04005559 /* If we tail call into other programs, we
5560 * cannot make any assumptions since they can
5561 * be replaced dynamically during runtime in
5562 * the program array.
5563 */
5564 prog->cb_access = 1;
Alexei Starovoitov80a58d02017-05-30 13:31:30 -07005565 env->prog->aux->stack_depth = MAX_BPF_STACK;
David S. Miller7b9f6da2017-04-20 10:35:33 -04005566
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005567 /* mark bpf_tail_call as different opcode to avoid
5568 * conditional branch in the interpeter for every normal
5569 * call and to prevent accidental JITing by JIT compiler
5570 * that doesn't support bpf_tail_call yet
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005571 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005572 insn->imm = 0;
Alexei Starovoitov71189fa2017-05-30 13:31:27 -07005573 insn->code = BPF_JMP | BPF_TAIL_CALL;
Alexei Starovoitovb2157392018-01-07 17:33:02 -08005574
5575 /* instead of changing every JIT dealing with tail_call
5576 * emit two extra insns:
5577 * if (index >= max_entries) goto out;
5578 * index &= array->index_mask;
5579 * to avoid out-of-bounds cpu speculation
5580 */
5581 map_ptr = env->insn_aux_data[i + delta].map_ptr;
5582 if (map_ptr == BPF_MAP_PTR_POISON) {
Colin Ian King40950342018-01-10 09:20:54 +00005583 verbose(env, "tail_call abusing map_ptr\n");
Alexei Starovoitovb2157392018-01-07 17:33:02 -08005584 return -EINVAL;
5585 }
5586 if (!map_ptr->unpriv_array)
5587 continue;
5588 insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3,
5589 map_ptr->max_entries, 2);
5590 insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3,
5591 container_of(map_ptr,
5592 struct bpf_array,
5593 map)->index_mask);
5594 insn_buf[2] = *insn;
5595 cnt = 3;
5596 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
5597 if (!new_prog)
5598 return -ENOMEM;
5599
5600 delta += cnt - 1;
5601 env->prog = prog = new_prog;
5602 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005603 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005604 }
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005605
Daniel Borkmann89c63072017-08-19 03:12:45 +02005606 /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup
5607 * handlers are currently limited to 64 bit only.
5608 */
Alexei Starovoitov60b58afc2017-12-14 17:55:14 -08005609 if (prog->jit_requested && BITS_PER_LONG == 64 &&
Daniel Borkmann89c63072017-08-19 03:12:45 +02005610 insn->imm == BPF_FUNC_map_lookup_elem) {
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07005611 map_ptr = env->insn_aux_data[i + delta].map_ptr;
Martin KaFai Laufad73a12017-03-22 10:00:32 -07005612 if (map_ptr == BPF_MAP_PTR_POISON ||
5613 !map_ptr->ops->map_gen_lookup)
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07005614 goto patch_call_imm;
5615
5616 cnt = map_ptr->ops->map_gen_lookup(map_ptr, insn_buf);
5617 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005618 verbose(env, "bpf verifier is misconfigured\n");
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07005619 return -EINVAL;
5620 }
5621
5622 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf,
5623 cnt);
5624 if (!new_prog)
5625 return -ENOMEM;
5626
5627 delta += cnt - 1;
5628
5629 /* keep walking new program and skip insns we just inserted */
5630 env->prog = prog = new_prog;
5631 insn = new_prog->insnsi + i + delta;
5632 continue;
5633 }
5634
Daniel Borkmann109980b2017-09-08 00:14:51 +02005635 if (insn->imm == BPF_FUNC_redirect_map) {
Daniel Borkmann7c300132017-09-20 00:44:21 +02005636 /* Note, we cannot use prog directly as imm as subsequent
5637 * rewrites would still change the prog pointer. The only
5638 * stable address we can use is aux, which also works with
5639 * prog clones during blinding.
5640 */
5641 u64 addr = (unsigned long)prog->aux;
Daniel Borkmann109980b2017-09-08 00:14:51 +02005642 struct bpf_insn r4_ld[] = {
5643 BPF_LD_IMM64(BPF_REG_4, addr),
5644 *insn,
5645 };
5646 cnt = ARRAY_SIZE(r4_ld);
5647
5648 new_prog = bpf_patch_insn_data(env, i + delta, r4_ld, cnt);
5649 if (!new_prog)
5650 return -ENOMEM;
5651
5652 delta += cnt - 1;
5653 env->prog = prog = new_prog;
5654 insn = new_prog->insnsi + i + delta;
5655 }
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07005656patch_call_imm:
Andrey Ignatov5e43f892018-03-30 15:08:00 -07005657 fn = env->ops->get_func_proto(insn->imm, env->prog);
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005658 /* all functions that have prototype and verifier allowed
5659 * programs to call them, must be real in-kernel functions
5660 */
5661 if (!fn->func) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005662 verbose(env,
5663 "kernel subsystem misconfigured func %s#%d\n",
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005664 func_id_name(insn->imm), insn->imm);
5665 return -EFAULT;
5666 }
5667 insn->imm = fn->func - __bpf_call_base;
5668 }
5669
5670 return 0;
5671}
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005672
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005673static void free_states(struct bpf_verifier_env *env)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005674{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005675 struct bpf_verifier_state_list *sl, *sln;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005676 int i;
5677
5678 if (!env->explored_states)
5679 return;
5680
5681 for (i = 0; i < env->prog->len; i++) {
5682 sl = env->explored_states[i];
5683
5684 if (sl)
5685 while (sl != STATE_LIST_MARK) {
5686 sln = sl->next;
Alexei Starovoitov1969db42017-11-01 00:08:04 -07005687 free_verifier_state(&sl->state, false);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005688 kfree(sl);
5689 sl = sln;
5690 }
5691 }
5692
5693 kfree(env->explored_states);
5694}
5695
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005696int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
Alexei Starovoitov51580e72014-09-26 00:17:02 -07005697{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005698 struct bpf_verifier_env *env;
Martin KaFai Laub9193c12018-03-24 11:44:22 -07005699 struct bpf_verifier_log *log;
Alexei Starovoitov51580e72014-09-26 00:17:02 -07005700 int ret = -EINVAL;
5701
Arnd Bergmanneba0c922017-11-02 12:05:52 +01005702 /* no program is valid */
5703 if (ARRAY_SIZE(bpf_verifier_ops) == 0)
5704 return -EINVAL;
5705
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005706 /* 'struct bpf_verifier_env' can be global, but since it's not small,
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005707 * allocate/free it every time bpf_check() is called
5708 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005709 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005710 if (!env)
5711 return -ENOMEM;
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005712 log = &env->log;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005713
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005714 env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
5715 (*prog)->len);
5716 ret = -ENOMEM;
5717 if (!env->insn_aux_data)
5718 goto err_free_env;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005719 env->prog = *prog;
Jakub Kicinski00176a32017-10-16 16:40:54 -07005720 env->ops = bpf_verifier_ops[env->prog->type];
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005721
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005722 /* grab the mutex to protect few globals used by verifier */
5723 mutex_lock(&bpf_verifier_lock);
5724
5725 if (attr->log_level || attr->log_buf || attr->log_size) {
5726 /* user requested verbose verifier output
5727 * and supplied buffer to store the verification trace
5728 */
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07005729 log->level = attr->log_level;
5730 log->ubuf = (char __user *) (unsigned long) attr->log_buf;
5731 log->len_total = attr->log_size;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005732
5733 ret = -EINVAL;
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07005734 /* log attributes have to be sane */
5735 if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 ||
5736 !log->level || !log->ubuf)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005737 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005738 }
Daniel Borkmann1ad2f582017-05-25 01:05:05 +02005739
5740 env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT);
5741 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
David S. Millere07b98d2017-05-10 11:38:07 -07005742 env->strict_alignment = true;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005743
Jakub Kicinskicae19272017-12-27 18:39:05 -08005744 if (bpf_prog_is_dev_bound(env->prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07005745 ret = bpf_prog_offload_verifier_prep(env);
5746 if (ret)
5747 goto err_unlock;
5748 }
5749
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005750 ret = replace_map_fd_with_map_ptr(env);
5751 if (ret < 0)
5752 goto skip_full_check;
5753
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005754 env->explored_states = kcalloc(env->prog->len,
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005755 sizeof(struct bpf_verifier_state_list *),
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005756 GFP_USER);
5757 ret = -ENOMEM;
5758 if (!env->explored_states)
5759 goto skip_full_check;
5760
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08005761 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
5762
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005763 ret = check_cfg(env);
5764 if (ret < 0)
5765 goto skip_full_check;
5766
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005767 ret = do_check(env);
Craig Gallek8c01c4f2017-11-02 11:18:01 -04005768 if (env->cur_state) {
5769 free_verifier_state(env->cur_state, true);
5770 env->cur_state = NULL;
5771 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005772
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005773skip_full_check:
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005774 while (!pop_stack(env, NULL, NULL));
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005775 free_states(env);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005776
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005777 if (ret == 0)
Alexei Starovoitovc1311872017-11-22 16:42:05 -08005778 sanitize_dead_code(env);
5779
5780 if (ret == 0)
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08005781 ret = check_max_stack_depth(env);
5782
5783 if (ret == 0)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005784 /* program is valid, convert *(u32*)(ctx + off) accesses */
5785 ret = convert_ctx_accesses(env);
5786
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005787 if (ret == 0)
Alexei Starovoitov79741b32017-03-15 18:26:40 -07005788 ret = fixup_bpf_calls(env);
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07005789
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08005790 if (ret == 0)
5791 ret = fixup_call_args(env);
5792
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07005793 if (log->level && bpf_verifier_log_full(log))
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005794 ret = -ENOSPC;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07005795 if (log->level && !log->ubuf) {
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005796 ret = -EFAULT;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07005797 goto err_release_maps;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005798 }
5799
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005800 if (ret == 0 && env->used_map_cnt) {
5801 /* if program passed verifier, update used_maps in bpf_prog_info */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005802 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
5803 sizeof(env->used_maps[0]),
5804 GFP_KERNEL);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005805
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005806 if (!env->prog->aux->used_maps) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005807 ret = -ENOMEM;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07005808 goto err_release_maps;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005809 }
5810
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005811 memcpy(env->prog->aux->used_maps, env->used_maps,
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005812 sizeof(env->used_maps[0]) * env->used_map_cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005813 env->prog->aux->used_map_cnt = env->used_map_cnt;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005814
5815 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
5816 * bpf_ld_imm64 instructions
5817 */
5818 convert_pseudo_ld_imm64(env);
5819 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005820
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07005821err_release_maps:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005822 if (!env->prog->aux->used_maps)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005823 /* if we didn't copy map pointers into bpf_prog_info, release
5824 * them now. Otherwise free_bpf_prog_info() will release them.
5825 */
5826 release_maps(env);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005827 *prog = env->prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005828err_unlock:
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07005829 mutex_unlock(&bpf_verifier_lock);
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005830 vfree(env->insn_aux_data);
5831err_free_env:
5832 kfree(env);
Alexei Starovoitov51580e72014-09-26 00:17:02 -07005833 return ret;
5834}