blob: 2e70b813a1a7530c100877551a12df74c74cfa30 [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
Joe Stringerfd978bf72018-10-02 13:35:35 -07003 * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io
Alexei Starovoitov51580e72014-09-26 00:17:02 -07004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 */
Yonghong Song838e9692018-11-19 15:29:11 -080014#include <uapi/linux/btf.h>
Alexei Starovoitov51580e72014-09-26 00:17:02 -070015#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/bpf.h>
Yonghong Song838e9692018-11-19 15:29:11 -080019#include <linux/btf.h>
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +010020#include <linux/bpf_verifier.h>
Alexei Starovoitov51580e72014-09-26 00:17:02 -070021#include <linux/filter.h>
22#include <net/netlink.h>
23#include <linux/file.h>
24#include <linux/vmalloc.h>
Thomas Grafebb676d2016-10-27 11:23:51 +020025#include <linux/stringify.h>
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -080026#include <linux/bsearch.h>
27#include <linux/sort.h>
Yonghong Songc195651e2018-04-28 22:28:08 -070028#include <linux/perf_event.h>
Alexei Starovoitov51580e72014-09-26 00:17:02 -070029
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -070030#include "disasm.h"
31
Jakub Kicinski00176a32017-10-16 16:40:54 -070032static const struct bpf_verifier_ops * const bpf_verifier_ops[] = {
33#define BPF_PROG_TYPE(_id, _name) \
34 [_id] = & _name ## _verifier_ops,
35#define BPF_MAP_TYPE(_id, _ops)
36#include <linux/bpf_types.h>
37#undef BPF_PROG_TYPE
38#undef BPF_MAP_TYPE
39};
40
Alexei Starovoitov51580e72014-09-26 00:17:02 -070041/* bpf_check() is a static code analyzer that walks eBPF program
42 * instruction by instruction and updates register/stack state.
43 * All paths of conditional branches are analyzed until 'bpf_exit' insn.
44 *
45 * The first pass is depth-first-search to check that the program is a DAG.
46 * It rejects the following programs:
47 * - larger than BPF_MAXINSNS insns
48 * - if loop is present (detected via back-edge)
49 * - unreachable insns exist (shouldn't be a forest. program = one function)
50 * - out of bounds or malformed jumps
51 * The second pass is all possible path descent from the 1st insn.
52 * Since it's analyzing all pathes through the program, the length of the
Gary Lineba38a92017-03-01 16:25:51 +080053 * analysis is limited to 64k insn, which may be hit even if total number of
Alexei Starovoitov51580e72014-09-26 00:17:02 -070054 * insn is less then 4K, but there are too many branches that change stack/regs.
55 * Number of 'branches to be analyzed' is limited to 1k
56 *
57 * On entry to each instruction, each register has a type, and the instruction
58 * changes the types of the registers depending on instruction semantics.
59 * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is
60 * copied to R1.
61 *
62 * All registers are 64-bit.
63 * R0 - return register
64 * R1-R5 argument passing registers
65 * R6-R9 callee saved registers
66 * R10 - frame pointer read-only
67 *
68 * At the start of BPF program the register R1 contains a pointer to bpf_context
69 * and has type PTR_TO_CTX.
70 *
71 * Verifier tracks arithmetic operations on pointers in case:
72 * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
73 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20),
74 * 1st insn copies R10 (which has FRAME_PTR) type into R1
75 * and 2nd arithmetic instruction is pattern matched to recognize
76 * that it wants to construct a pointer to some element within stack.
77 * So after 2nd insn, the register R1 has type PTR_TO_STACK
78 * (and -20 constant is saved for further stack bounds checking).
79 * Meaning that this reg is a pointer to stack plus known immediate constant.
80 *
Edward Creef1174f72017-08-07 15:26:19 +010081 * Most of the time the registers have SCALAR_VALUE type, which
Alexei Starovoitov51580e72014-09-26 00:17:02 -070082 * means the register has some value, but it's not a valid pointer.
Edward Creef1174f72017-08-07 15:26:19 +010083 * (like pointer plus pointer becomes SCALAR_VALUE type)
Alexei Starovoitov51580e72014-09-26 00:17:02 -070084 *
85 * When verifier sees load or store instructions the type of base register
Joe Stringerc64b7982018-10-02 13:35:33 -070086 * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are
87 * four pointer types recognized by check_mem_access() function.
Alexei Starovoitov51580e72014-09-26 00:17:02 -070088 *
89 * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value'
90 * and the range of [ptr, ptr + map's value_size) is accessible.
91 *
92 * registers used to pass values to function calls are checked against
93 * function argument constraints.
94 *
95 * ARG_PTR_TO_MAP_KEY is one of such argument constraints.
96 * It means that the register type passed to this function must be
97 * PTR_TO_STACK and it will be used inside the function as
98 * 'pointer to map element key'
99 *
100 * For example the argument constraints for bpf_map_lookup_elem():
101 * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
102 * .arg1_type = ARG_CONST_MAP_PTR,
103 * .arg2_type = ARG_PTR_TO_MAP_KEY,
104 *
105 * ret_type says that this function returns 'pointer to map elem value or null'
106 * function expects 1st argument to be a const pointer to 'struct bpf_map' and
107 * 2nd argument should be a pointer to stack, which will be used inside
108 * the helper function as a pointer to map element key.
109 *
110 * On the kernel side the helper function looks like:
111 * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
112 * {
113 * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
114 * void *key = (void *) (unsigned long) r2;
115 * void *value;
116 *
117 * here kernel can access 'key' and 'map' pointers safely, knowing that
118 * [key, key + map->key_size) bytes are valid and were initialized on
119 * the stack of eBPF program.
120 * }
121 *
122 * Corresponding eBPF program may look like:
123 * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR
124 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK
125 * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP
126 * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
127 * here verifier looks at prototype of map_lookup_elem() and sees:
128 * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok,
129 * Now verifier knows that this map has key of R1->map_ptr->key_size bytes
130 *
131 * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far,
132 * Now verifier checks that [R2, R2 + map's key_size) are within stack limits
133 * and were initialized prior to this call.
134 * If it's ok, then verifier allows this BPF_CALL insn and looks at
135 * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets
136 * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function
137 * returns ether pointer to map value or NULL.
138 *
139 * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off'
140 * insn, the register holding that pointer in the true branch changes state to
141 * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false
142 * branch. See check_cond_jmp_op().
143 *
144 * After the call R0 is set to return type of the function and registers R1-R5
145 * are set to NOT_INIT to indicate that they are no longer readable.
Joe Stringerfd978bf72018-10-02 13:35:35 -0700146 *
147 * The following reference types represent a potential reference to a kernel
148 * resource which, after first being allocated, must be checked and freed by
149 * the BPF program:
150 * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET
151 *
152 * When the verifier sees a helper call return a reference type, it allocates a
153 * pointer id for the reference and stores it in the current function state.
154 * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into
155 * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type
156 * passes through a NULL-check conditional. For the branch wherein the state is
157 * changed to CONST_IMM, the verifier releases the reference.
Joe Stringer6acc9b42018-10-02 13:35:36 -0700158 *
159 * For each helper function that allocates a reference, such as
160 * bpf_sk_lookup_tcp(), there is a corresponding release function, such as
161 * bpf_sk_release(). When a reference type passes into the release function,
162 * the verifier also releases the reference. If any unchecked or unreleased
163 * reference remains at the end of the program, the verifier rejects it.
Alexei Starovoitov51580e72014-09-26 00:17:02 -0700164 */
165
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700166/* verifier_state + insn_idx are pushed to stack when branch is encountered */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100167struct bpf_verifier_stack_elem {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700168 /* verifer state is 'st'
169 * before processing instruction 'insn_idx'
170 * and after processing instruction 'prev_insn_idx'
171 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100172 struct bpf_verifier_state st;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700173 int insn_idx;
174 int prev_insn_idx;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100175 struct bpf_verifier_stack_elem *next;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700176};
177
Edward Cree8e17c1b2017-08-07 15:30:30 +0100178#define BPF_COMPLEXITY_LIMIT_INSNS 131072
Daniel Borkmann07016152016-04-05 22:33:17 +0200179#define BPF_COMPLEXITY_LIMIT_STACK 1024
180
Daniel Borkmannc93552c2018-05-24 02:32:53 +0200181#define BPF_MAP_PTR_UNPRIV 1UL
182#define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \
183 POISON_POINTER_DELTA))
184#define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV))
185
186static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux)
187{
188 return BPF_MAP_PTR(aux->map_state) == BPF_MAP_PTR_POISON;
189}
190
191static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux)
192{
193 return aux->map_state & BPF_MAP_PTR_UNPRIV;
194}
195
196static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux,
197 const struct bpf_map *map, bool unpriv)
198{
199 BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV);
200 unpriv |= bpf_map_ptr_unpriv(aux);
201 aux->map_state = (unsigned long)map |
202 (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL);
203}
Martin KaFai Laufad73a12017-03-22 10:00:32 -0700204
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200205struct bpf_call_arg_meta {
206 struct bpf_map *map_ptr;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200207 bool raw_mode;
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200208 bool pkt_access;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200209 int regno;
210 int access_size;
Yonghong Song849fa502018-04-28 22:28:09 -0700211 s64 msize_smax_value;
212 u64 msize_umax_value;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700213 int ptr_id;
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200214};
215
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700216static DEFINE_MUTEX(bpf_verifier_lock);
217
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700218void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt,
219 va_list args)
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700220{
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700221 unsigned int n;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700222
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700223 n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args);
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700224
225 WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1,
226 "verifier log line truncated - local buffer too short\n");
227
228 n = min(log->len_total - log->len_used - 1, n);
229 log->kbuf[n] = '\0';
230
231 if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1))
232 log->len_used += n;
233 else
234 log->ubuf = NULL;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700235}
Jiri Olsaabe08842018-03-23 11:41:28 +0100236
237/* log_level controls verbosity level of eBPF verifier.
238 * bpf_verifier_log_write() is used to dump the verification trace to the log,
239 * so the user can figure out what's wrong with the program
Quentin Monnet430e68d2018-01-10 12:26:06 +0000240 */
Jiri Olsaabe08842018-03-23 11:41:28 +0100241__printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env,
242 const char *fmt, ...)
243{
244 va_list args;
245
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700246 if (!bpf_verifier_log_needed(&env->log))
247 return;
248
Jiri Olsaabe08842018-03-23 11:41:28 +0100249 va_start(args, fmt);
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700250 bpf_verifier_vlog(&env->log, fmt, args);
Jiri Olsaabe08842018-03-23 11:41:28 +0100251 va_end(args);
252}
253EXPORT_SYMBOL_GPL(bpf_verifier_log_write);
254
255__printf(2, 3) static void verbose(void *private_data, const char *fmt, ...)
256{
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700257 struct bpf_verifier_env *env = private_data;
Jiri Olsaabe08842018-03-23 11:41:28 +0100258 va_list args;
259
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700260 if (!bpf_verifier_log_needed(&env->log))
261 return;
262
Jiri Olsaabe08842018-03-23 11:41:28 +0100263 va_start(args, fmt);
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700264 bpf_verifier_vlog(&env->log, fmt, args);
Jiri Olsaabe08842018-03-23 11:41:28 +0100265 va_end(args);
266}
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700267
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200268static bool type_is_pkt_pointer(enum bpf_reg_type type)
269{
270 return type == PTR_TO_PACKET ||
271 type == PTR_TO_PACKET_META;
272}
273
Joe Stringer840b9612018-10-02 13:35:32 -0700274static bool reg_type_may_be_null(enum bpf_reg_type type)
275{
Joe Stringerfd978bf72018-10-02 13:35:35 -0700276 return type == PTR_TO_MAP_VALUE_OR_NULL ||
277 type == PTR_TO_SOCKET_OR_NULL;
278}
279
280static bool type_is_refcounted(enum bpf_reg_type type)
281{
282 return type == PTR_TO_SOCKET;
283}
284
285static bool type_is_refcounted_or_null(enum bpf_reg_type type)
286{
287 return type == PTR_TO_SOCKET || type == PTR_TO_SOCKET_OR_NULL;
288}
289
290static bool reg_is_refcounted(const struct bpf_reg_state *reg)
291{
292 return type_is_refcounted(reg->type);
293}
294
295static bool reg_is_refcounted_or_null(const struct bpf_reg_state *reg)
296{
297 return type_is_refcounted_or_null(reg->type);
298}
299
300static bool arg_type_is_refcounted(enum bpf_arg_type type)
301{
302 return type == ARG_PTR_TO_SOCKET;
303}
304
305/* Determine whether the function releases some resources allocated by another
306 * function call. The first reference type argument will be assumed to be
307 * released by release_reference().
308 */
309static bool is_release_function(enum bpf_func_id func_id)
310{
Joe Stringer6acc9b42018-10-02 13:35:36 -0700311 return func_id == BPF_FUNC_sk_release;
Joe Stringer840b9612018-10-02 13:35:32 -0700312}
313
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700314/* string representation of 'enum bpf_reg_type' */
315static const char * const reg_type_str[] = {
316 [NOT_INIT] = "?",
Edward Creef1174f72017-08-07 15:26:19 +0100317 [SCALAR_VALUE] = "inv",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700318 [PTR_TO_CTX] = "ctx",
319 [CONST_PTR_TO_MAP] = "map_ptr",
320 [PTR_TO_MAP_VALUE] = "map_value",
321 [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700322 [PTR_TO_STACK] = "fp",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700323 [PTR_TO_PACKET] = "pkt",
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200324 [PTR_TO_PACKET_META] = "pkt_meta",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700325 [PTR_TO_PACKET_END] = "pkt_end",
Petar Penkovd58e4682018-09-14 07:46:18 -0700326 [PTR_TO_FLOW_KEYS] = "flow_keys",
Joe Stringerc64b7982018-10-02 13:35:33 -0700327 [PTR_TO_SOCKET] = "sock",
328 [PTR_TO_SOCKET_OR_NULL] = "sock_or_null",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700329};
330
Edward Cree8efea212018-08-22 20:02:44 +0100331static char slot_type_char[] = {
332 [STACK_INVALID] = '?',
333 [STACK_SPILL] = 'r',
334 [STACK_MISC] = 'm',
335 [STACK_ZERO] = '0',
336};
337
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800338static void print_liveness(struct bpf_verifier_env *env,
339 enum bpf_reg_liveness live)
340{
341 if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN))
342 verbose(env, "_");
343 if (live & REG_LIVE_READ)
344 verbose(env, "r");
345 if (live & REG_LIVE_WRITTEN)
346 verbose(env, "w");
347}
348
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800349static struct bpf_func_state *func(struct bpf_verifier_env *env,
350 const struct bpf_reg_state *reg)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700351{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800352 struct bpf_verifier_state *cur = env->cur_state;
353
354 return cur->frame[reg->frameno];
355}
356
357static void print_verifier_state(struct bpf_verifier_env *env,
358 const struct bpf_func_state *state)
359{
360 const struct bpf_reg_state *reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700361 enum bpf_reg_type t;
362 int i;
363
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800364 if (state->frameno)
365 verbose(env, " frame%d:", state->frameno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700366 for (i = 0; i < MAX_BPF_REG; i++) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700367 reg = &state->regs[i];
368 t = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700369 if (t == NOT_INIT)
370 continue;
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800371 verbose(env, " R%d", i);
372 print_liveness(env, reg->live);
373 verbose(env, "=%s", reg_type_str[t]);
Edward Creef1174f72017-08-07 15:26:19 +0100374 if ((t == SCALAR_VALUE || t == PTR_TO_STACK) &&
375 tnum_is_const(reg->var_off)) {
376 /* reg->off should be 0 for SCALAR_VALUE */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700377 verbose(env, "%lld", reg->var_off.value + reg->off);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800378 if (t == PTR_TO_STACK)
379 verbose(env, ",call_%d", func(env, reg)->callsite);
Edward Creef1174f72017-08-07 15:26:19 +0100380 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700381 verbose(env, "(id=%d", reg->id);
Edward Creef1174f72017-08-07 15:26:19 +0100382 if (t != SCALAR_VALUE)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700383 verbose(env, ",off=%d", reg->off);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200384 if (type_is_pkt_pointer(t))
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700385 verbose(env, ",r=%d", reg->range);
Edward Creef1174f72017-08-07 15:26:19 +0100386 else if (t == CONST_PTR_TO_MAP ||
387 t == PTR_TO_MAP_VALUE ||
388 t == PTR_TO_MAP_VALUE_OR_NULL)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700389 verbose(env, ",ks=%d,vs=%d",
Edward Creef1174f72017-08-07 15:26:19 +0100390 reg->map_ptr->key_size,
391 reg->map_ptr->value_size);
Edward Cree7d1238f2017-08-07 15:26:56 +0100392 if (tnum_is_const(reg->var_off)) {
393 /* Typically an immediate SCALAR_VALUE, but
394 * could be a pointer whose offset is too big
395 * for reg->off
396 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700397 verbose(env, ",imm=%llx", reg->var_off.value);
Edward Cree7d1238f2017-08-07 15:26:56 +0100398 } else {
399 if (reg->smin_value != reg->umin_value &&
400 reg->smin_value != S64_MIN)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700401 verbose(env, ",smin_value=%lld",
Edward Cree7d1238f2017-08-07 15:26:56 +0100402 (long long)reg->smin_value);
403 if (reg->smax_value != reg->umax_value &&
404 reg->smax_value != S64_MAX)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700405 verbose(env, ",smax_value=%lld",
Edward Cree7d1238f2017-08-07 15:26:56 +0100406 (long long)reg->smax_value);
407 if (reg->umin_value != 0)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700408 verbose(env, ",umin_value=%llu",
Edward Cree7d1238f2017-08-07 15:26:56 +0100409 (unsigned long long)reg->umin_value);
410 if (reg->umax_value != U64_MAX)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700411 verbose(env, ",umax_value=%llu",
Edward Cree7d1238f2017-08-07 15:26:56 +0100412 (unsigned long long)reg->umax_value);
413 if (!tnum_is_unknown(reg->var_off)) {
414 char tn_buf[48];
Edward Creef1174f72017-08-07 15:26:19 +0100415
Edward Cree7d1238f2017-08-07 15:26:56 +0100416 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700417 verbose(env, ",var_off=%s", tn_buf);
Edward Cree7d1238f2017-08-07 15:26:56 +0100418 }
Edward Creef1174f72017-08-07 15:26:19 +0100419 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700420 verbose(env, ")");
Edward Creef1174f72017-08-07 15:26:19 +0100421 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700422 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700423 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
Edward Cree8efea212018-08-22 20:02:44 +0100424 char types_buf[BPF_REG_SIZE + 1];
425 bool valid = false;
426 int j;
427
428 for (j = 0; j < BPF_REG_SIZE; j++) {
429 if (state->stack[i].slot_type[j] != STACK_INVALID)
430 valid = true;
431 types_buf[j] = slot_type_char[
432 state->stack[i].slot_type[j]];
433 }
434 types_buf[BPF_REG_SIZE] = 0;
435 if (!valid)
436 continue;
437 verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE);
438 print_liveness(env, state->stack[i].spilled_ptr.live);
439 if (state->stack[i].slot_type[0] == STACK_SPILL)
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800440 verbose(env, "=%s",
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700441 reg_type_str[state->stack[i].spilled_ptr.type]);
Edward Cree8efea212018-08-22 20:02:44 +0100442 else
443 verbose(env, "=%s", types_buf);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700444 }
Joe Stringerfd978bf72018-10-02 13:35:35 -0700445 if (state->acquired_refs && state->refs[0].id) {
446 verbose(env, " refs=%d", state->refs[0].id);
447 for (i = 1; i < state->acquired_refs; i++)
448 if (state->refs[i].id)
449 verbose(env, ",%d", state->refs[i].id);
450 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700451 verbose(env, "\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700452}
453
Joe Stringer84dbf352018-10-02 13:35:34 -0700454#define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \
455static int copy_##NAME##_state(struct bpf_func_state *dst, \
456 const struct bpf_func_state *src) \
457{ \
458 if (!src->FIELD) \
459 return 0; \
460 if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \
461 /* internal bug, make state invalid to reject the program */ \
462 memset(dst, 0, sizeof(*dst)); \
463 return -EFAULT; \
464 } \
465 memcpy(dst->FIELD, src->FIELD, \
466 sizeof(*src->FIELD) * (src->COUNT / SIZE)); \
467 return 0; \
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700468}
Joe Stringerfd978bf72018-10-02 13:35:35 -0700469/* copy_reference_state() */
470COPY_STATE_FN(reference, acquired_refs, refs, 1)
Joe Stringer84dbf352018-10-02 13:35:34 -0700471/* copy_stack_state() */
472COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE)
473#undef COPY_STATE_FN
474
475#define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \
476static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \
477 bool copy_old) \
478{ \
479 u32 old_size = state->COUNT; \
480 struct bpf_##NAME##_state *new_##FIELD; \
481 int slot = size / SIZE; \
482 \
483 if (size <= old_size || !size) { \
484 if (copy_old) \
485 return 0; \
486 state->COUNT = slot * SIZE; \
487 if (!size && old_size) { \
488 kfree(state->FIELD); \
489 state->FIELD = NULL; \
490 } \
491 return 0; \
492 } \
493 new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \
494 GFP_KERNEL); \
495 if (!new_##FIELD) \
496 return -ENOMEM; \
497 if (copy_old) { \
498 if (state->FIELD) \
499 memcpy(new_##FIELD, state->FIELD, \
500 sizeof(*new_##FIELD) * (old_size / SIZE)); \
501 memset(new_##FIELD + old_size / SIZE, 0, \
502 sizeof(*new_##FIELD) * (size - old_size) / SIZE); \
503 } \
504 state->COUNT = slot * SIZE; \
505 kfree(state->FIELD); \
506 state->FIELD = new_##FIELD; \
507 return 0; \
508}
Joe Stringerfd978bf72018-10-02 13:35:35 -0700509/* realloc_reference_state() */
510REALLOC_STATE_FN(reference, acquired_refs, refs, 1)
Joe Stringer84dbf352018-10-02 13:35:34 -0700511/* realloc_stack_state() */
512REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE)
513#undef REALLOC_STATE_FN
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700514
515/* do_check() starts with zero-sized stack in struct bpf_verifier_state to
516 * make it consume minimal amount of memory. check_stack_write() access from
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800517 * the program calls into realloc_func_state() to grow the stack size.
Joe Stringer84dbf352018-10-02 13:35:34 -0700518 * Note there is a non-zero 'parent' pointer inside bpf_verifier_state
519 * which realloc_stack_state() copies over. It points to previous
520 * bpf_verifier_state which is never reallocated.
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700521 */
Joe Stringerfd978bf72018-10-02 13:35:35 -0700522static int realloc_func_state(struct bpf_func_state *state, int stack_size,
523 int refs_size, bool copy_old)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700524{
Joe Stringerfd978bf72018-10-02 13:35:35 -0700525 int err = realloc_reference_state(state, refs_size, copy_old);
526 if (err)
527 return err;
528 return realloc_stack_state(state, stack_size, copy_old);
529}
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700530
Joe Stringerfd978bf72018-10-02 13:35:35 -0700531/* Acquire a pointer id from the env and update the state->refs to include
532 * this new pointer reference.
533 * On success, returns a valid pointer id to associate with the register
534 * On failure, returns a negative errno.
535 */
536static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx)
537{
538 struct bpf_func_state *state = cur_func(env);
539 int new_ofs = state->acquired_refs;
540 int id, err;
541
542 err = realloc_reference_state(state, state->acquired_refs + 1, true);
543 if (err)
544 return err;
545 id = ++env->id_gen;
546 state->refs[new_ofs].id = id;
547 state->refs[new_ofs].insn_idx = insn_idx;
548
549 return id;
550}
551
552/* release function corresponding to acquire_reference_state(). Idempotent. */
553static int __release_reference_state(struct bpf_func_state *state, int ptr_id)
554{
555 int i, last_idx;
556
557 if (!ptr_id)
558 return -EFAULT;
559
560 last_idx = state->acquired_refs - 1;
561 for (i = 0; i < state->acquired_refs; i++) {
562 if (state->refs[i].id == ptr_id) {
563 if (last_idx && i != last_idx)
564 memcpy(&state->refs[i], &state->refs[last_idx],
565 sizeof(*state->refs));
566 memset(&state->refs[last_idx], 0, sizeof(*state->refs));
567 state->acquired_refs--;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700568 return 0;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700569 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700570 }
Joe Stringerfd978bf72018-10-02 13:35:35 -0700571 return -EFAULT;
572}
573
574/* variation on the above for cases where we expect that there must be an
575 * outstanding reference for the specified ptr_id.
576 */
577static int release_reference_state(struct bpf_verifier_env *env, int ptr_id)
578{
579 struct bpf_func_state *state = cur_func(env);
580 int err;
581
582 err = __release_reference_state(state, ptr_id);
583 if (WARN_ON_ONCE(err != 0))
584 verbose(env, "verifier internal error: can't release reference\n");
585 return err;
586}
587
588static int transfer_reference_state(struct bpf_func_state *dst,
589 struct bpf_func_state *src)
590{
591 int err = realloc_reference_state(dst, src->acquired_refs, false);
592 if (err)
593 return err;
594 err = copy_reference_state(dst, src);
595 if (err)
596 return err;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700597 return 0;
598}
599
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800600static void free_func_state(struct bpf_func_state *state)
601{
Alexei Starovoitov58963512018-01-08 07:51:17 -0800602 if (!state)
603 return;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700604 kfree(state->refs);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800605 kfree(state->stack);
606 kfree(state);
607}
608
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700609static void free_verifier_state(struct bpf_verifier_state *state,
610 bool free_self)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700611{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800612 int i;
613
614 for (i = 0; i <= state->curframe; i++) {
615 free_func_state(state->frame[i]);
616 state->frame[i] = NULL;
617 }
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700618 if (free_self)
619 kfree(state);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700620}
621
622/* copy verifier state from src to dst growing dst stack space
623 * when necessary to accommodate larger src stack
624 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800625static int copy_func_state(struct bpf_func_state *dst,
626 const struct bpf_func_state *src)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700627{
628 int err;
629
Joe Stringerfd978bf72018-10-02 13:35:35 -0700630 err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs,
631 false);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700632 if (err)
633 return err;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700634 memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs));
635 err = copy_reference_state(dst, src);
636 if (err)
637 return err;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700638 return copy_stack_state(dst, src);
639}
640
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800641static int copy_verifier_state(struct bpf_verifier_state *dst_state,
642 const struct bpf_verifier_state *src)
643{
644 struct bpf_func_state *dst;
645 int i, err;
646
647 /* if dst has more stack frames then src frame, free them */
648 for (i = src->curframe + 1; i <= dst_state->curframe; i++) {
649 free_func_state(dst_state->frame[i]);
650 dst_state->frame[i] = NULL;
651 }
652 dst_state->curframe = src->curframe;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800653 for (i = 0; i <= src->curframe; i++) {
654 dst = dst_state->frame[i];
655 if (!dst) {
656 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
657 if (!dst)
658 return -ENOMEM;
659 dst_state->frame[i] = dst;
660 }
661 err = copy_func_state(dst, src->frame[i]);
662 if (err)
663 return err;
664 }
665 return 0;
666}
667
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700668static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx,
669 int *insn_idx)
670{
671 struct bpf_verifier_state *cur = env->cur_state;
672 struct bpf_verifier_stack_elem *elem, *head = env->head;
673 int err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700674
675 if (env->head == NULL)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700676 return -ENOENT;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700677
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700678 if (cur) {
679 err = copy_verifier_state(cur, &head->st);
680 if (err)
681 return err;
682 }
683 if (insn_idx)
684 *insn_idx = head->insn_idx;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700685 if (prev_insn_idx)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700686 *prev_insn_idx = head->prev_insn_idx;
687 elem = head->next;
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700688 free_verifier_state(&head->st, false);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700689 kfree(head);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700690 env->head = elem;
691 env->stack_size--;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700692 return 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700693}
694
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100695static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,
696 int insn_idx, int prev_insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700697{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700698 struct bpf_verifier_state *cur = env->cur_state;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100699 struct bpf_verifier_stack_elem *elem;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700700 int err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700701
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700702 elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700703 if (!elem)
704 goto err;
705
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700706 elem->insn_idx = insn_idx;
707 elem->prev_insn_idx = prev_insn_idx;
708 elem->next = env->head;
709 env->head = elem;
710 env->stack_size++;
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700711 err = copy_verifier_state(&elem->st, cur);
712 if (err)
713 goto err;
Daniel Borkmann07016152016-04-05 22:33:17 +0200714 if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700715 verbose(env, "BPF program is too complex\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700716 goto err;
717 }
718 return &elem->st;
719err:
Alexei Starovoitov58963512018-01-08 07:51:17 -0800720 free_verifier_state(env->cur_state, true);
721 env->cur_state = NULL;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700722 /* pop all elements and return */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700723 while (!pop_stack(env, NULL, NULL));
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700724 return NULL;
725}
726
727#define CALLER_SAVED_REGS 6
728static const int caller_saved[CALLER_SAVED_REGS] = {
729 BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
730};
731
Edward Creef1174f72017-08-07 15:26:19 +0100732static void __mark_reg_not_init(struct bpf_reg_state *reg);
733
Edward Creeb03c9f92017-08-07 15:26:36 +0100734/* Mark the unknown part of a register (variable offset or scalar value) as
735 * known to have the value @imm.
736 */
737static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
738{
Alexei Starovoitova9c676b2018-09-04 19:13:44 -0700739 /* Clear id, off, and union(map_ptr, range) */
740 memset(((u8 *)reg) + sizeof(reg->type), 0,
741 offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type));
Edward Creeb03c9f92017-08-07 15:26:36 +0100742 reg->var_off = tnum_const(imm);
743 reg->smin_value = (s64)imm;
744 reg->smax_value = (s64)imm;
745 reg->umin_value = imm;
746 reg->umax_value = imm;
747}
748
Edward Creef1174f72017-08-07 15:26:19 +0100749/* Mark the 'variable offset' part of a register as zero. This should be
750 * used only on registers holding a pointer type.
751 */
752static void __mark_reg_known_zero(struct bpf_reg_state *reg)
753{
Edward Creeb03c9f92017-08-07 15:26:36 +0100754 __mark_reg_known(reg, 0);
Edward Creef1174f72017-08-07 15:26:19 +0100755}
756
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800757static void __mark_reg_const_zero(struct bpf_reg_state *reg)
758{
759 __mark_reg_known(reg, 0);
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800760 reg->type = SCALAR_VALUE;
761}
762
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700763static void mark_reg_known_zero(struct bpf_verifier_env *env,
764 struct bpf_reg_state *regs, u32 regno)
Edward Creef1174f72017-08-07 15:26:19 +0100765{
766 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700767 verbose(env, "mark_reg_known_zero(regs, %u)\n", regno);
Edward Creef1174f72017-08-07 15:26:19 +0100768 /* Something bad happened, let's kill all regs */
769 for (regno = 0; regno < MAX_BPF_REG; regno++)
770 __mark_reg_not_init(regs + regno);
771 return;
772 }
773 __mark_reg_known_zero(regs + regno);
774}
775
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200776static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg)
777{
778 return type_is_pkt_pointer(reg->type);
779}
780
781static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg)
782{
783 return reg_is_pkt_pointer(reg) ||
784 reg->type == PTR_TO_PACKET_END;
785}
786
787/* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */
788static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg,
789 enum bpf_reg_type which)
790{
791 /* The register can already have a range from prior markings.
792 * This is fine as long as it hasn't been advanced from its
793 * origin.
794 */
795 return reg->type == which &&
796 reg->id == 0 &&
797 reg->off == 0 &&
798 tnum_equals_const(reg->var_off, 0);
799}
800
Edward Creeb03c9f92017-08-07 15:26:36 +0100801/* Attempts to improve min/max values based on var_off information */
802static void __update_reg_bounds(struct bpf_reg_state *reg)
803{
804 /* min signed is max(sign bit) | min(other bits) */
805 reg->smin_value = max_t(s64, reg->smin_value,
806 reg->var_off.value | (reg->var_off.mask & S64_MIN));
807 /* max signed is min(sign bit) | max(other bits) */
808 reg->smax_value = min_t(s64, reg->smax_value,
809 reg->var_off.value | (reg->var_off.mask & S64_MAX));
810 reg->umin_value = max(reg->umin_value, reg->var_off.value);
811 reg->umax_value = min(reg->umax_value,
812 reg->var_off.value | reg->var_off.mask);
813}
814
815/* Uses signed min/max values to inform unsigned, and vice-versa */
816static void __reg_deduce_bounds(struct bpf_reg_state *reg)
817{
818 /* Learn sign from signed bounds.
819 * If we cannot cross the sign boundary, then signed and unsigned bounds
820 * are the same, so combine. This works even in the negative case, e.g.
821 * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff.
822 */
823 if (reg->smin_value >= 0 || reg->smax_value < 0) {
824 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
825 reg->umin_value);
826 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
827 reg->umax_value);
828 return;
829 }
830 /* Learn sign from unsigned bounds. Signed bounds cross the sign
831 * boundary, so we must be careful.
832 */
833 if ((s64)reg->umax_value >= 0) {
834 /* Positive. We can't learn anything from the smin, but smax
835 * is positive, hence safe.
836 */
837 reg->smin_value = reg->umin_value;
838 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
839 reg->umax_value);
840 } else if ((s64)reg->umin_value < 0) {
841 /* Negative. We can't learn anything from the smax, but smin
842 * is negative, hence safe.
843 */
844 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
845 reg->umin_value);
846 reg->smax_value = reg->umax_value;
847 }
848}
849
850/* Attempts to improve var_off based on unsigned min/max information */
851static void __reg_bound_offset(struct bpf_reg_state *reg)
852{
853 reg->var_off = tnum_intersect(reg->var_off,
854 tnum_range(reg->umin_value,
855 reg->umax_value));
856}
857
858/* Reset the min/max bounds of a register */
859static void __mark_reg_unbounded(struct bpf_reg_state *reg)
860{
861 reg->smin_value = S64_MIN;
862 reg->smax_value = S64_MAX;
863 reg->umin_value = 0;
864 reg->umax_value = U64_MAX;
865}
866
Edward Creef1174f72017-08-07 15:26:19 +0100867/* Mark a register as having a completely unknown (scalar) value. */
868static void __mark_reg_unknown(struct bpf_reg_state *reg)
869{
Alexei Starovoitova9c676b2018-09-04 19:13:44 -0700870 /*
871 * Clear type, id, off, and union(map_ptr, range) and
872 * padding between 'type' and union
873 */
874 memset(reg, 0, offsetof(struct bpf_reg_state, var_off));
Edward Creef1174f72017-08-07 15:26:19 +0100875 reg->type = SCALAR_VALUE;
Edward Creef1174f72017-08-07 15:26:19 +0100876 reg->var_off = tnum_unknown;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800877 reg->frameno = 0;
Edward Creeb03c9f92017-08-07 15:26:36 +0100878 __mark_reg_unbounded(reg);
Edward Creef1174f72017-08-07 15:26:19 +0100879}
880
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700881static void mark_reg_unknown(struct bpf_verifier_env *env,
882 struct bpf_reg_state *regs, u32 regno)
Edward Creef1174f72017-08-07 15:26:19 +0100883{
884 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700885 verbose(env, "mark_reg_unknown(regs, %u)\n", regno);
Alexei Starovoitov19ceb412017-11-30 21:31:37 -0800886 /* Something bad happened, let's kill all regs except FP */
887 for (regno = 0; regno < BPF_REG_FP; regno++)
Edward Creef1174f72017-08-07 15:26:19 +0100888 __mark_reg_not_init(regs + regno);
889 return;
890 }
891 __mark_reg_unknown(regs + regno);
892}
893
894static void __mark_reg_not_init(struct bpf_reg_state *reg)
895{
896 __mark_reg_unknown(reg);
897 reg->type = NOT_INIT;
898}
899
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700900static void mark_reg_not_init(struct bpf_verifier_env *env,
901 struct bpf_reg_state *regs, u32 regno)
Daniel Borkmanna9789ef2017-05-25 01:05:06 +0200902{
Edward Creef1174f72017-08-07 15:26:19 +0100903 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700904 verbose(env, "mark_reg_not_init(regs, %u)\n", regno);
Alexei Starovoitov19ceb412017-11-30 21:31:37 -0800905 /* Something bad happened, let's kill all regs except FP */
906 for (regno = 0; regno < BPF_REG_FP; regno++)
Edward Creef1174f72017-08-07 15:26:19 +0100907 __mark_reg_not_init(regs + regno);
908 return;
909 }
910 __mark_reg_not_init(regs + regno);
Daniel Borkmanna9789ef2017-05-25 01:05:06 +0200911}
912
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700913static void init_reg_state(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800914 struct bpf_func_state *state)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700915{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800916 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700917 int i;
918
Edward Creedc503a82017-08-15 20:34:35 +0100919 for (i = 0; i < MAX_BPF_REG; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700920 mark_reg_not_init(env, regs, i);
Edward Creedc503a82017-08-15 20:34:35 +0100921 regs[i].live = REG_LIVE_NONE;
Edward Cree679c7822018-08-22 20:02:19 +0100922 regs[i].parent = NULL;
Edward Creedc503a82017-08-15 20:34:35 +0100923 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700924
925 /* frame pointer */
Edward Creef1174f72017-08-07 15:26:19 +0100926 regs[BPF_REG_FP].type = PTR_TO_STACK;
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700927 mark_reg_known_zero(env, regs, BPF_REG_FP);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800928 regs[BPF_REG_FP].frameno = state->frameno;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700929
930 /* 1st arg to a function */
931 regs[BPF_REG_1].type = PTR_TO_CTX;
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700932 mark_reg_known_zero(env, regs, BPF_REG_1);
Daniel Borkmann6760bf22016-12-18 01:52:59 +0100933}
934
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800935#define BPF_MAIN_FUNC (-1)
936static void init_func_state(struct bpf_verifier_env *env,
937 struct bpf_func_state *state,
938 int callsite, int frameno, int subprogno)
939{
940 state->callsite = callsite;
941 state->frameno = frameno;
942 state->subprogno = subprogno;
943 init_reg_state(env, state);
944}
945
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700946enum reg_arg_type {
947 SRC_OP, /* register is used as source operand */
948 DST_OP, /* register is used as destination operand */
949 DST_OP_NO_MARK /* same as above, check only, don't mark */
950};
951
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800952static int cmp_subprogs(const void *a, const void *b)
953{
Jiong Wang9c8105b2018-05-02 16:17:18 -0400954 return ((struct bpf_subprog_info *)a)->start -
955 ((struct bpf_subprog_info *)b)->start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800956}
957
958static int find_subprog(struct bpf_verifier_env *env, int off)
959{
Jiong Wang9c8105b2018-05-02 16:17:18 -0400960 struct bpf_subprog_info *p;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800961
Jiong Wang9c8105b2018-05-02 16:17:18 -0400962 p = bsearch(&off, env->subprog_info, env->subprog_cnt,
963 sizeof(env->subprog_info[0]), cmp_subprogs);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800964 if (!p)
965 return -ENOENT;
Jiong Wang9c8105b2018-05-02 16:17:18 -0400966 return p - env->subprog_info;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800967
968}
969
970static int add_subprog(struct bpf_verifier_env *env, int off)
971{
972 int insn_cnt = env->prog->len;
973 int ret;
974
975 if (off >= insn_cnt || off < 0) {
976 verbose(env, "call to invalid destination\n");
977 return -EINVAL;
978 }
979 ret = find_subprog(env, off);
980 if (ret >= 0)
981 return 0;
Jiong Wang4cb3d992018-05-02 16:17:19 -0400982 if (env->subprog_cnt >= BPF_MAX_SUBPROGS) {
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800983 verbose(env, "too many subprograms\n");
984 return -E2BIG;
985 }
Jiong Wang9c8105b2018-05-02 16:17:18 -0400986 env->subprog_info[env->subprog_cnt++].start = off;
987 sort(env->subprog_info, env->subprog_cnt,
988 sizeof(env->subprog_info[0]), cmp_subprogs, NULL);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800989 return 0;
990}
991
992static int check_subprogs(struct bpf_verifier_env *env)
993{
994 int i, ret, subprog_start, subprog_end, off, cur_subprog = 0;
Jiong Wang9c8105b2018-05-02 16:17:18 -0400995 struct bpf_subprog_info *subprog = env->subprog_info;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800996 struct bpf_insn *insn = env->prog->insnsi;
997 int insn_cnt = env->prog->len;
998
Jiong Wangf910cef2018-05-02 16:17:17 -0400999 /* Add entry function. */
1000 ret = add_subprog(env, 0);
1001 if (ret < 0)
1002 return ret;
1003
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001004 /* determine subprog starts. The end is one before the next starts */
1005 for (i = 0; i < insn_cnt; i++) {
1006 if (insn[i].code != (BPF_JMP | BPF_CALL))
1007 continue;
1008 if (insn[i].src_reg != BPF_PSEUDO_CALL)
1009 continue;
1010 if (!env->allow_ptr_leaks) {
1011 verbose(env, "function calls to other bpf functions are allowed for root only\n");
1012 return -EPERM;
1013 }
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001014 ret = add_subprog(env, i + insn[i].imm + 1);
1015 if (ret < 0)
1016 return ret;
1017 }
1018
Jiong Wang4cb3d992018-05-02 16:17:19 -04001019 /* Add a fake 'exit' subprog which could simplify subprog iteration
1020 * logic. 'subprog_cnt' should not be increased.
1021 */
1022 subprog[env->subprog_cnt].start = insn_cnt;
1023
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001024 if (env->log.level > 1)
1025 for (i = 0; i < env->subprog_cnt; i++)
Jiong Wang9c8105b2018-05-02 16:17:18 -04001026 verbose(env, "func#%d @%d\n", i, subprog[i].start);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001027
1028 /* now check that all jumps are within the same subprog */
Jiong Wang4cb3d992018-05-02 16:17:19 -04001029 subprog_start = subprog[cur_subprog].start;
1030 subprog_end = subprog[cur_subprog + 1].start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001031 for (i = 0; i < insn_cnt; i++) {
1032 u8 code = insn[i].code;
1033
1034 if (BPF_CLASS(code) != BPF_JMP)
1035 goto next;
1036 if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL)
1037 goto next;
1038 off = i + insn[i].off + 1;
1039 if (off < subprog_start || off >= subprog_end) {
1040 verbose(env, "jump out of range from insn %d to %d\n", i, off);
1041 return -EINVAL;
1042 }
1043next:
1044 if (i == subprog_end - 1) {
1045 /* to avoid fall-through from one subprog into another
1046 * the last insn of the subprog should be either exit
1047 * or unconditional jump back
1048 */
1049 if (code != (BPF_JMP | BPF_EXIT) &&
1050 code != (BPF_JMP | BPF_JA)) {
1051 verbose(env, "last insn is not an exit or jmp\n");
1052 return -EINVAL;
1053 }
1054 subprog_start = subprog_end;
Jiong Wang4cb3d992018-05-02 16:17:19 -04001055 cur_subprog++;
1056 if (cur_subprog < env->subprog_cnt)
Jiong Wang9c8105b2018-05-02 16:17:18 -04001057 subprog_end = subprog[cur_subprog + 1].start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001058 }
1059 }
1060 return 0;
1061}
1062
Edward Cree679c7822018-08-22 20:02:19 +01001063/* Parentage chain of this register (or stack slot) should take care of all
1064 * issues like callee-saved registers, stack slot allocation time, etc.
1065 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001066static int mark_reg_read(struct bpf_verifier_env *env,
Edward Cree679c7822018-08-22 20:02:19 +01001067 const struct bpf_reg_state *state,
1068 struct bpf_reg_state *parent)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001069{
1070 bool writes = parent == state->parent; /* Observe write marks */
Edward Creedc503a82017-08-15 20:34:35 +01001071
1072 while (parent) {
1073 /* if read wasn't screened by an earlier write ... */
Edward Cree679c7822018-08-22 20:02:19 +01001074 if (writes && state->live & REG_LIVE_WRITTEN)
Edward Creedc503a82017-08-15 20:34:35 +01001075 break;
1076 /* ... then we depend on parent's value */
Edward Cree679c7822018-08-22 20:02:19 +01001077 parent->live |= REG_LIVE_READ;
Edward Creedc503a82017-08-15 20:34:35 +01001078 state = parent;
1079 parent = state->parent;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001080 writes = true;
Edward Creedc503a82017-08-15 20:34:35 +01001081 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001082 return 0;
Edward Creedc503a82017-08-15 20:34:35 +01001083}
1084
1085static int check_reg_arg(struct bpf_verifier_env *env, u32 regno,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001086 enum reg_arg_type t)
1087{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001088 struct bpf_verifier_state *vstate = env->cur_state;
1089 struct bpf_func_state *state = vstate->frame[vstate->curframe];
1090 struct bpf_reg_state *regs = state->regs;
Edward Creedc503a82017-08-15 20:34:35 +01001091
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001092 if (regno >= MAX_BPF_REG) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001093 verbose(env, "R%d is invalid\n", regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001094 return -EINVAL;
1095 }
1096
1097 if (t == SRC_OP) {
1098 /* check whether register used as source operand can be read */
1099 if (regs[regno].type == NOT_INIT) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001100 verbose(env, "R%d !read_ok\n", regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001101 return -EACCES;
1102 }
Edward Cree679c7822018-08-22 20:02:19 +01001103 /* We don't need to worry about FP liveness because it's read-only */
1104 if (regno != BPF_REG_FP)
1105 return mark_reg_read(env, &regs[regno],
1106 regs[regno].parent);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001107 } else {
1108 /* check whether register used as dest operand can be written to */
1109 if (regno == BPF_REG_FP) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001110 verbose(env, "frame pointer is read only\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001111 return -EACCES;
1112 }
Edward Creedc503a82017-08-15 20:34:35 +01001113 regs[regno].live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001114 if (t == DST_OP)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001115 mark_reg_unknown(env, regs, regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001116 }
1117 return 0;
1118}
1119
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001120static bool is_spillable_regtype(enum bpf_reg_type type)
1121{
1122 switch (type) {
1123 case PTR_TO_MAP_VALUE:
1124 case PTR_TO_MAP_VALUE_OR_NULL:
1125 case PTR_TO_STACK:
1126 case PTR_TO_CTX:
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001127 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001128 case PTR_TO_PACKET_META:
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001129 case PTR_TO_PACKET_END:
Petar Penkovd58e4682018-09-14 07:46:18 -07001130 case PTR_TO_FLOW_KEYS:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001131 case CONST_PTR_TO_MAP:
Joe Stringerc64b7982018-10-02 13:35:33 -07001132 case PTR_TO_SOCKET:
1133 case PTR_TO_SOCKET_OR_NULL:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001134 return true;
1135 default:
1136 return false;
1137 }
1138}
1139
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001140/* Does this register contain a constant zero? */
1141static bool register_is_null(struct bpf_reg_state *reg)
1142{
1143 return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0);
1144}
1145
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001146/* check_stack_read/write functions track spill/fill of registers,
1147 * stack boundary and alignment are checked in check_mem_access()
1148 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001149static int check_stack_write(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001150 struct bpf_func_state *state, /* func where register points to */
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07001151 int off, int size, int value_regno, int insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001152{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001153 struct bpf_func_state *cur; /* state of the current function */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001154 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001155 enum bpf_reg_type type;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001156
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001157 err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE),
Joe Stringerfd978bf72018-10-02 13:35:35 -07001158 state->acquired_refs, true);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001159 if (err)
1160 return err;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001161 /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
1162 * so it's aligned access and [off, off + size) are within stack limits
1163 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001164 if (!env->allow_ptr_leaks &&
1165 state->stack[spi].slot_type[0] == STACK_SPILL &&
1166 size != BPF_REG_SIZE) {
1167 verbose(env, "attempt to corrupt spilled pointer on stack\n");
1168 return -EACCES;
1169 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001170
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001171 cur = env->cur_state->frame[env->cur_state->curframe];
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001172 if (value_regno >= 0 &&
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001173 is_spillable_regtype((type = cur->regs[value_regno].type))) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001174
1175 /* register containing pointer is being spilled into stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001176 if (size != BPF_REG_SIZE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001177 verbose(env, "invalid size of register spill\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001178 return -EACCES;
1179 }
1180
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001181 if (state != cur && type == PTR_TO_STACK) {
1182 verbose(env, "cannot spill pointers to stack into stack frame of the caller\n");
1183 return -EINVAL;
1184 }
1185
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001186 /* save register state */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001187 state->stack[spi].spilled_ptr = cur->regs[value_regno];
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001188 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001189
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07001190 for (i = 0; i < BPF_REG_SIZE; i++) {
1191 if (state->stack[spi].slot_type[i] == STACK_MISC &&
1192 !env->allow_ptr_leaks) {
1193 int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off;
1194 int soff = (-spi - 1) * BPF_REG_SIZE;
1195
1196 /* detected reuse of integer stack slot with a pointer
1197 * which means either llvm is reusing stack slot or
1198 * an attacker is trying to exploit CVE-2018-3639
1199 * (speculative store bypass)
1200 * Have to sanitize that slot with preemptive
1201 * store of zero.
1202 */
1203 if (*poff && *poff != soff) {
1204 /* disallow programs where single insn stores
1205 * into two different stack slots, since verifier
1206 * cannot sanitize them
1207 */
1208 verbose(env,
1209 "insn %d cannot access two stack slots fp%d and fp%d",
1210 insn_idx, *poff, soff);
1211 return -EINVAL;
1212 }
1213 *poff = soff;
1214 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001215 state->stack[spi].slot_type[i] = STACK_SPILL;
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07001216 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001217 } else {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001218 u8 type = STACK_MISC;
1219
Edward Cree679c7822018-08-22 20:02:19 +01001220 /* regular write of data into stack destroys any spilled ptr */
1221 state->stack[spi].spilled_ptr.type = NOT_INIT;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001222
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001223 /* only mark the slot as written if all 8 bytes were written
1224 * otherwise read propagation may incorrectly stop too soon
1225 * when stack slots are partially written.
1226 * This heuristic means that read propagation will be
1227 * conservative, since it will add reg_live_read marks
1228 * to stack slots all the way to first state when programs
1229 * writes+reads less than 8 bytes
1230 */
1231 if (size == BPF_REG_SIZE)
1232 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
1233
1234 /* when we zero initialize stack slots mark them as such */
1235 if (value_regno >= 0 &&
1236 register_is_null(&cur->regs[value_regno]))
1237 type = STACK_ZERO;
1238
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001239 for (i = 0; i < size; i++)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001240 state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] =
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001241 type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001242 }
1243 return 0;
1244}
1245
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001246static int check_stack_read(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001247 struct bpf_func_state *reg_state /* func where register points to */,
1248 int off, int size, int value_regno)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001249{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001250 struct bpf_verifier_state *vstate = env->cur_state;
1251 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001252 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE;
1253 u8 *stype;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001254
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001255 if (reg_state->allocated_stack <= slot) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001256 verbose(env, "invalid read from stack off %d+0 size %d\n",
1257 off, size);
1258 return -EACCES;
1259 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001260 stype = reg_state->stack[spi].slot_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001261
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001262 if (stype[0] == STACK_SPILL) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001263 if (size != BPF_REG_SIZE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001264 verbose(env, "invalid size of register spill\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001265 return -EACCES;
1266 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001267 for (i = 1; i < BPF_REG_SIZE; i++) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001268 if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001269 verbose(env, "corrupted spill memory\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001270 return -EACCES;
1271 }
1272 }
1273
Edward Creedc503a82017-08-15 20:34:35 +01001274 if (value_regno >= 0) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001275 /* restore register state from stack */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001276 state->regs[value_regno] = reg_state->stack[spi].spilled_ptr;
Alexei Starovoitov2f18f622017-11-30 21:31:38 -08001277 /* mark reg as written since spilled pointer state likely
1278 * has its liveness marks cleared by is_state_visited()
1279 * which resets stack/reg liveness for state transitions
1280 */
1281 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
Edward Creedc503a82017-08-15 20:34:35 +01001282 }
Edward Cree679c7822018-08-22 20:02:19 +01001283 mark_reg_read(env, &reg_state->stack[spi].spilled_ptr,
1284 reg_state->stack[spi].spilled_ptr.parent);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001285 return 0;
1286 } else {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001287 int zeros = 0;
1288
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001289 for (i = 0; i < size; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001290 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC)
1291 continue;
1292 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) {
1293 zeros++;
1294 continue;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001295 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001296 verbose(env, "invalid read from stack off %d+%d size %d\n",
1297 off, i, size);
1298 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001299 }
Edward Cree679c7822018-08-22 20:02:19 +01001300 mark_reg_read(env, &reg_state->stack[spi].spilled_ptr,
1301 reg_state->stack[spi].spilled_ptr.parent);
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001302 if (value_regno >= 0) {
1303 if (zeros == size) {
1304 /* any size read into register is zero extended,
1305 * so the whole register == const_zero
1306 */
1307 __mark_reg_const_zero(&state->regs[value_regno]);
1308 } else {
1309 /* have read misc data from the stack */
1310 mark_reg_unknown(env, state->regs, value_regno);
1311 }
1312 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
1313 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001314 return 0;
1315 }
1316}
1317
1318/* check read/write into map element returned by bpf_map_lookup_elem() */
Edward Creef1174f72017-08-07 15:26:19 +01001319static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001320 int size, bool zero_size_allowed)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001321{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001322 struct bpf_reg_state *regs = cur_regs(env);
1323 struct bpf_map *map = regs[regno].map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001324
Yonghong Song9fd29c02017-11-12 14:49:09 -08001325 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
1326 off + size > map->value_size) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001327 verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001328 map->value_size, off, size);
1329 return -EACCES;
1330 }
1331 return 0;
1332}
1333
Edward Creef1174f72017-08-07 15:26:19 +01001334/* check read/write into a map element with possible variable offset */
1335static int check_map_access(struct bpf_verifier_env *env, u32 regno,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001336 int off, int size, bool zero_size_allowed)
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001337{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001338 struct bpf_verifier_state *vstate = env->cur_state;
1339 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001340 struct bpf_reg_state *reg = &state->regs[regno];
1341 int err;
1342
Edward Creef1174f72017-08-07 15:26:19 +01001343 /* We may have adjusted the register to this map value, so we
1344 * need to try adding each of min_value and max_value to off
1345 * to make sure our theoretical access will be safe.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001346 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001347 if (env->log.level)
1348 print_verifier_state(env, state);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001349 /* The minimum value is only important with signed
1350 * comparisons where we can't assume the floor of a
1351 * value is 0. If we are using signed variables for our
1352 * index'es we need to make sure that whatever we use
1353 * will have a set floor within our range.
1354 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001355 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001356 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 -08001357 regno);
1358 return -EACCES;
1359 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001360 err = __check_map_access(env, regno, reg->smin_value + off, size,
1361 zero_size_allowed);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001362 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001363 verbose(env, "R%d min value is outside of the array range\n",
1364 regno);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001365 return err;
1366 }
1367
Edward Creeb03c9f92017-08-07 15:26:36 +01001368 /* If we haven't set a max value then we need to bail since we can't be
1369 * sure we won't do bad things.
1370 * If reg->umax_value + off could overflow, treat that as unbounded too.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001371 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001372 if (reg->umax_value >= BPF_MAX_VAR_OFF) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001373 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 -08001374 regno);
1375 return -EACCES;
1376 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001377 err = __check_map_access(env, regno, reg->umax_value + off, size,
1378 zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01001379 if (err)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001380 verbose(env, "R%d max value is outside of the array range\n",
1381 regno);
Edward Creef1174f72017-08-07 15:26:19 +01001382 return err;
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001383}
1384
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001385#define MAX_PACKET_OFF 0xffff
1386
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001387static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001388 const struct bpf_call_arg_meta *meta,
1389 enum bpf_access_type t)
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001390{
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001391 switch (env->prog->type) {
Daniel Borkmann5d66fa72018-10-24 22:05:45 +02001392 /* Program types only with direct read access go here! */
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001393 case BPF_PROG_TYPE_LWT_IN:
1394 case BPF_PROG_TYPE_LWT_OUT:
Mathieu Xhonneux004d4b22018-05-20 14:58:16 +01001395 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07001396 case BPF_PROG_TYPE_SK_REUSEPORT:
Daniel Borkmann5d66fa72018-10-24 22:05:45 +02001397 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Daniel Borkmannd5563d32018-10-24 22:05:46 +02001398 case BPF_PROG_TYPE_CGROUP_SKB:
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001399 if (t == BPF_WRITE)
1400 return false;
Alexander Alemayhu7e57fbb2017-02-14 00:02:35 +01001401 /* fallthrough */
Daniel Borkmann5d66fa72018-10-24 22:05:45 +02001402
1403 /* Program types with direct read + write access go here! */
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001404 case BPF_PROG_TYPE_SCHED_CLS:
1405 case BPF_PROG_TYPE_SCHED_ACT:
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001406 case BPF_PROG_TYPE_XDP:
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001407 case BPF_PROG_TYPE_LWT_XMIT:
John Fastabend8a31db52017-08-15 22:33:09 -07001408 case BPF_PROG_TYPE_SK_SKB:
John Fastabend4f738ad2018-03-18 12:57:10 -07001409 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001410 if (meta)
1411 return meta->pkt_access;
1412
1413 env->seen_direct_write = true;
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001414 return true;
1415 default:
1416 return false;
1417 }
1418}
1419
Edward Creef1174f72017-08-07 15:26:19 +01001420static int __check_packet_access(struct bpf_verifier_env *env, u32 regno,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001421 int off, int size, bool zero_size_allowed)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001422{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001423 struct bpf_reg_state *regs = cur_regs(env);
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001424 struct bpf_reg_state *reg = &regs[regno];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001425
Yonghong Song9fd29c02017-11-12 14:49:09 -08001426 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
1427 (u64)off + size > reg->range) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001428 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 -07001429 off, size, regno, reg->id, reg->off, reg->range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001430 return -EACCES;
1431 }
1432 return 0;
1433}
1434
Edward Creef1174f72017-08-07 15:26:19 +01001435static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001436 int size, bool zero_size_allowed)
Edward Creef1174f72017-08-07 15:26:19 +01001437{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001438 struct bpf_reg_state *regs = cur_regs(env);
Edward Creef1174f72017-08-07 15:26:19 +01001439 struct bpf_reg_state *reg = &regs[regno];
1440 int err;
1441
1442 /* We may have added a variable offset to the packet pointer; but any
1443 * reg->range we have comes after that. We are only checking the fixed
1444 * offset.
1445 */
1446
1447 /* We don't allow negative numbers, because we aren't tracking enough
1448 * detail to prove they're safe.
1449 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001450 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001451 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 +01001452 regno);
1453 return -EACCES;
1454 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001455 err = __check_packet_access(env, regno, off, size, zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01001456 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001457 verbose(env, "R%d offset is outside of the packet\n", regno);
Edward Creef1174f72017-08-07 15:26:19 +01001458 return err;
1459 }
Jiong Wange6478152018-11-08 04:08:42 -05001460
1461 /* __check_packet_access has made sure "off + size - 1" is within u16.
1462 * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff,
1463 * otherwise find_good_pkt_pointers would have refused to set range info
1464 * that __check_packet_access would have rejected this pkt access.
1465 * Therefore, "off + reg->umax_value + size - 1" won't overflow u32.
1466 */
1467 env->prog->aux->max_pkt_offset =
1468 max_t(u32, env->prog->aux->max_pkt_offset,
1469 off + reg->umax_value + size - 1);
1470
Edward Creef1174f72017-08-07 15:26:19 +01001471 return err;
1472}
1473
1474/* check access to 'struct bpf_context' fields. Supports fixed offsets only */
Yonghong Song31fd8582017-06-13 15:52:13 -07001475static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07001476 enum bpf_access_type t, enum bpf_reg_type *reg_type)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001477{
Daniel Borkmannf96da092017-07-02 02:13:27 +02001478 struct bpf_insn_access_aux info = {
1479 .reg_type = *reg_type,
1480 };
Yonghong Song31fd8582017-06-13 15:52:13 -07001481
Jakub Kicinski4f9218a2017-10-16 16:40:55 -07001482 if (env->ops->is_valid_access &&
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001483 env->ops->is_valid_access(off, size, t, env->prog, &info)) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02001484 /* A non zero info.ctx_field_size indicates that this field is a
1485 * candidate for later verifier transformation to load the whole
1486 * field and then apply a mask when accessed with a narrower
1487 * access than actual ctx access size. A zero info.ctx_field_size
1488 * will only allow for whole field access and rejects any other
1489 * type of narrower access.
Yonghong Song31fd8582017-06-13 15:52:13 -07001490 */
Yonghong Song23994632017-06-22 15:07:39 -07001491 *reg_type = info.reg_type;
Yonghong Song31fd8582017-06-13 15:52:13 -07001492
Jakub Kicinski4f9218a2017-10-16 16:40:55 -07001493 env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07001494 /* remember the offset of last byte accessed in ctx */
1495 if (env->prog->aux->max_ctx_offset < off + size)
1496 env->prog->aux->max_ctx_offset = off + size;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001497 return 0;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07001498 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001499
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001500 verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001501 return -EACCES;
1502}
1503
Petar Penkovd58e4682018-09-14 07:46:18 -07001504static int check_flow_keys_access(struct bpf_verifier_env *env, int off,
1505 int size)
1506{
1507 if (size < 0 || off < 0 ||
1508 (u64)off + size > sizeof(struct bpf_flow_keys)) {
1509 verbose(env, "invalid access to flow keys off=%d size=%d\n",
1510 off, size);
1511 return -EACCES;
1512 }
1513 return 0;
1514}
1515
Joe Stringerc64b7982018-10-02 13:35:33 -07001516static int check_sock_access(struct bpf_verifier_env *env, u32 regno, int off,
1517 int size, enum bpf_access_type t)
1518{
1519 struct bpf_reg_state *regs = cur_regs(env);
1520 struct bpf_reg_state *reg = &regs[regno];
1521 struct bpf_insn_access_aux info;
1522
1523 if (reg->smin_value < 0) {
1524 verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
1525 regno);
1526 return -EACCES;
1527 }
1528
1529 if (!bpf_sock_is_valid_access(off, size, t, &info)) {
1530 verbose(env, "invalid bpf_sock access off=%d size=%d\n",
1531 off, size);
1532 return -EACCES;
1533 }
1534
1535 return 0;
1536}
1537
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001538static bool __is_pointer_value(bool allow_ptr_leaks,
1539 const struct bpf_reg_state *reg)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001540{
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001541 if (allow_ptr_leaks)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001542 return false;
1543
Edward Creef1174f72017-08-07 15:26:19 +01001544 return reg->type != SCALAR_VALUE;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001545}
1546
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001547static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno)
1548{
1549 return cur_regs(env) + regno;
1550}
1551
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001552static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
1553{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001554 return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno));
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001555}
1556
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01001557static bool is_ctx_reg(struct bpf_verifier_env *env, int regno)
1558{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001559 const struct bpf_reg_state *reg = reg_state(env, regno);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01001560
Joe Stringerfd978bf72018-10-02 13:35:35 -07001561 return reg->type == PTR_TO_CTX ||
1562 reg->type == PTR_TO_SOCKET;
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01001563}
1564
Daniel Borkmannca369602018-02-23 22:29:05 +01001565static bool is_pkt_reg(struct bpf_verifier_env *env, int regno)
1566{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001567 const struct bpf_reg_state *reg = reg_state(env, regno);
Daniel Borkmannca369602018-02-23 22:29:05 +01001568
1569 return type_is_pkt_pointer(reg->type);
1570}
1571
Daniel Borkmann4b5defd2018-10-21 02:09:25 +02001572static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno)
1573{
1574 const struct bpf_reg_state *reg = reg_state(env, regno);
1575
1576 /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */
1577 return reg->type == PTR_TO_FLOW_KEYS;
1578}
1579
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001580static int check_pkt_ptr_alignment(struct bpf_verifier_env *env,
1581 const struct bpf_reg_state *reg,
David S. Millerd1174412017-05-10 11:22:52 -07001582 int off, int size, bool strict)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001583{
Edward Creef1174f72017-08-07 15:26:19 +01001584 struct tnum reg_off;
David S. Millere07b98d2017-05-10 11:38:07 -07001585 int ip_align;
David S. Millerd1174412017-05-10 11:22:52 -07001586
1587 /* Byte size accesses are always allowed. */
1588 if (!strict || size == 1)
1589 return 0;
1590
David S. Millere4eda882017-05-22 12:27:07 -04001591 /* For platforms that do not have a Kconfig enabling
1592 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of
1593 * NET_IP_ALIGN is universally set to '2'. And on platforms
1594 * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get
1595 * to this code only in strict mode where we want to emulate
1596 * the NET_IP_ALIGN==2 checking. Therefore use an
1597 * unconditional IP align value of '2'.
David S. Millere07b98d2017-05-10 11:38:07 -07001598 */
David S. Millere4eda882017-05-22 12:27:07 -04001599 ip_align = 2;
Edward Creef1174f72017-08-07 15:26:19 +01001600
1601 reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off));
1602 if (!tnum_is_aligned(reg_off, size)) {
1603 char tn_buf[48];
1604
1605 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001606 verbose(env,
1607 "misaligned packet access off %d+%s+%d+%d size %d\n",
Edward Creef1174f72017-08-07 15:26:19 +01001608 ip_align, tn_buf, reg->off, off, size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001609 return -EACCES;
1610 }
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001611
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001612 return 0;
1613}
1614
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001615static int check_generic_ptr_alignment(struct bpf_verifier_env *env,
1616 const struct bpf_reg_state *reg,
Edward Creef1174f72017-08-07 15:26:19 +01001617 const char *pointer_desc,
1618 int off, int size, bool strict)
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001619{
Edward Creef1174f72017-08-07 15:26:19 +01001620 struct tnum reg_off;
1621
1622 /* Byte size accesses are always allowed. */
1623 if (!strict || size == 1)
1624 return 0;
1625
1626 reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off));
1627 if (!tnum_is_aligned(reg_off, size)) {
1628 char tn_buf[48];
1629
1630 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001631 verbose(env, "misaligned %saccess off %s+%d+%d size %d\n",
Edward Creef1174f72017-08-07 15:26:19 +01001632 pointer_desc, tn_buf, reg->off, off, size);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001633 return -EACCES;
1634 }
1635
1636 return 0;
1637}
1638
David S. Millere07b98d2017-05-10 11:38:07 -07001639static int check_ptr_alignment(struct bpf_verifier_env *env,
Daniel Borkmannca369602018-02-23 22:29:05 +01001640 const struct bpf_reg_state *reg, int off,
1641 int size, bool strict_alignment_once)
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001642{
Daniel Borkmannca369602018-02-23 22:29:05 +01001643 bool strict = env->strict_alignment || strict_alignment_once;
Edward Creef1174f72017-08-07 15:26:19 +01001644 const char *pointer_desc = "";
David S. Millerd1174412017-05-10 11:22:52 -07001645
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001646 switch (reg->type) {
1647 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001648 case PTR_TO_PACKET_META:
1649 /* Special case, because of NET_IP_ALIGN. Given metadata sits
1650 * right in front, treat it the very same way.
1651 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001652 return check_pkt_ptr_alignment(env, reg, off, size, strict);
Petar Penkovd58e4682018-09-14 07:46:18 -07001653 case PTR_TO_FLOW_KEYS:
1654 pointer_desc = "flow keys ";
1655 break;
Edward Creef1174f72017-08-07 15:26:19 +01001656 case PTR_TO_MAP_VALUE:
1657 pointer_desc = "value ";
1658 break;
1659 case PTR_TO_CTX:
1660 pointer_desc = "context ";
1661 break;
1662 case PTR_TO_STACK:
1663 pointer_desc = "stack ";
Jann Horna5ec6ae2017-12-18 20:11:58 -08001664 /* The stack spill tracking logic in check_stack_write()
1665 * and check_stack_read() relies on stack accesses being
1666 * aligned.
1667 */
1668 strict = true;
Edward Creef1174f72017-08-07 15:26:19 +01001669 break;
Joe Stringerc64b7982018-10-02 13:35:33 -07001670 case PTR_TO_SOCKET:
1671 pointer_desc = "sock ";
1672 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001673 default:
Edward Creef1174f72017-08-07 15:26:19 +01001674 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001675 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001676 return check_generic_ptr_alignment(env, reg, pointer_desc, off, size,
1677 strict);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001678}
1679
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001680static int update_stack_depth(struct bpf_verifier_env *env,
1681 const struct bpf_func_state *func,
1682 int off)
1683{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001684 u16 stack = env->subprog_info[func->subprogno].stack_depth;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001685
1686 if (stack >= -off)
1687 return 0;
1688
1689 /* update known max for given subprogram */
Jiong Wang9c8105b2018-05-02 16:17:18 -04001690 env->subprog_info[func->subprogno].stack_depth = -off;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001691 return 0;
1692}
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001693
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001694/* starting from main bpf function walk all instructions of the function
1695 * and recursively walk all callees that given function can call.
1696 * Ignore jump and exit insns.
1697 * Since recursion is prevented by check_cfg() this algorithm
1698 * only needs a local stack of MAX_CALL_FRAMES to remember callsites
1699 */
1700static int check_max_stack_depth(struct bpf_verifier_env *env)
1701{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001702 int depth = 0, frame = 0, idx = 0, i = 0, subprog_end;
1703 struct bpf_subprog_info *subprog = env->subprog_info;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001704 struct bpf_insn *insn = env->prog->insnsi;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001705 int ret_insn[MAX_CALL_FRAMES];
1706 int ret_prog[MAX_CALL_FRAMES];
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001707
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001708process_func:
1709 /* round up to 32-bytes, since this is granularity
1710 * of interpreter stack size
1711 */
Jiong Wang9c8105b2018-05-02 16:17:18 -04001712 depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32);
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001713 if (depth > MAX_BPF_STACK) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001714 verbose(env, "combined stack size of %d calls is %d. Too large\n",
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001715 frame + 1, depth);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001716 return -EACCES;
1717 }
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001718continue_func:
Jiong Wang4cb3d992018-05-02 16:17:19 -04001719 subprog_end = subprog[idx + 1].start;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001720 for (; i < subprog_end; i++) {
1721 if (insn[i].code != (BPF_JMP | BPF_CALL))
1722 continue;
1723 if (insn[i].src_reg != BPF_PSEUDO_CALL)
1724 continue;
1725 /* remember insn and function to return to */
1726 ret_insn[frame] = i + 1;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001727 ret_prog[frame] = idx;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001728
1729 /* find the callee */
1730 i = i + insn[i].imm + 1;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001731 idx = find_subprog(env, i);
1732 if (idx < 0) {
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001733 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
1734 i);
1735 return -EFAULT;
1736 }
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001737 frame++;
1738 if (frame >= MAX_CALL_FRAMES) {
1739 WARN_ONCE(1, "verifier bug. Call stack is too deep\n");
1740 return -EFAULT;
1741 }
1742 goto process_func;
1743 }
1744 /* end of for() loop means the last insn of the 'subprog'
1745 * was reached. Doesn't matter whether it was JA or EXIT
1746 */
1747 if (frame == 0)
1748 return 0;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001749 depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32);
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001750 frame--;
1751 i = ret_insn[frame];
Jiong Wang9c8105b2018-05-02 16:17:18 -04001752 idx = ret_prog[frame];
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001753 goto continue_func;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001754}
1755
David S. Miller19d28fb2018-01-11 21:27:54 -05001756#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001757static int get_callee_stack_depth(struct bpf_verifier_env *env,
1758 const struct bpf_insn *insn, int idx)
1759{
1760 int start = idx + insn->imm + 1, subprog;
1761
1762 subprog = find_subprog(env, start);
1763 if (subprog < 0) {
1764 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
1765 start);
1766 return -EFAULT;
1767 }
Jiong Wang9c8105b2018-05-02 16:17:18 -04001768 return env->subprog_info[subprog].stack_depth;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001769}
David S. Miller19d28fb2018-01-11 21:27:54 -05001770#endif
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001771
Daniel Borkmann58990d12018-06-07 17:40:03 +02001772static int check_ctx_reg(struct bpf_verifier_env *env,
1773 const struct bpf_reg_state *reg, int regno)
1774{
1775 /* Access to ctx or passing it to a helper is only allowed in
1776 * its original, unmodified form.
1777 */
1778
1779 if (reg->off) {
1780 verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n",
1781 regno, reg->off);
1782 return -EACCES;
1783 }
1784
1785 if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
1786 char tn_buf[48];
1787
1788 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
1789 verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf);
1790 return -EACCES;
1791 }
1792
1793 return 0;
1794}
1795
Jann Horn0c17d1d2017-12-18 20:11:55 -08001796/* truncate register to smaller size (in bytes)
1797 * must be called with size < BPF_REG_SIZE
1798 */
1799static void coerce_reg_to_size(struct bpf_reg_state *reg, int size)
1800{
1801 u64 mask;
1802
1803 /* clear high bits in bit representation */
1804 reg->var_off = tnum_cast(reg->var_off, size);
1805
1806 /* fix arithmetic bounds */
1807 mask = ((u64)1 << (size * 8)) - 1;
1808 if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) {
1809 reg->umin_value &= mask;
1810 reg->umax_value &= mask;
1811 } else {
1812 reg->umin_value = 0;
1813 reg->umax_value = mask;
1814 }
1815 reg->smin_value = reg->umin_value;
1816 reg->smax_value = reg->umax_value;
1817}
1818
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001819/* check whether memory at (regno + off) is accessible for t = (read | write)
1820 * if t==write, value_regno is a register which value is stored into memory
1821 * if t==read, value_regno is a register which will receive the value from memory
1822 * if t==write && value_regno==-1, some unknown value is stored into memory
1823 * if t==read && value_regno==-1, don't care what we read from memory
1824 */
Daniel Borkmannca369602018-02-23 22:29:05 +01001825static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno,
1826 int off, int bpf_size, enum bpf_access_type t,
1827 int value_regno, bool strict_alignment_once)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001828{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001829 struct bpf_reg_state *regs = cur_regs(env);
1830 struct bpf_reg_state *reg = regs + regno;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001831 struct bpf_func_state *state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001832 int size, err = 0;
1833
1834 size = bpf_size_to_bytes(bpf_size);
1835 if (size < 0)
1836 return size;
1837
Edward Creef1174f72017-08-07 15:26:19 +01001838 /* alignment checks will add in reg->off themselves */
Daniel Borkmannca369602018-02-23 22:29:05 +01001839 err = check_ptr_alignment(env, reg, off, size, strict_alignment_once);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001840 if (err)
1841 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001842
Edward Creef1174f72017-08-07 15:26:19 +01001843 /* for access checks, reg->off is just part of off */
1844 off += reg->off;
1845
1846 if (reg->type == PTR_TO_MAP_VALUE) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001847 if (t == BPF_WRITE && value_regno >= 0 &&
1848 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001849 verbose(env, "R%d leaks addr into map\n", value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001850 return -EACCES;
1851 }
Josef Bacik48461132016-09-28 10:54:32 -04001852
Yonghong Song9fd29c02017-11-12 14:49:09 -08001853 err = check_map_access(env, regno, off, size, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001854 if (!err && t == BPF_READ && value_regno >= 0)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001855 mark_reg_unknown(env, regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001856
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07001857 } else if (reg->type == PTR_TO_CTX) {
Edward Creef1174f72017-08-07 15:26:19 +01001858 enum bpf_reg_type reg_type = SCALAR_VALUE;
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07001859
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001860 if (t == BPF_WRITE && value_regno >= 0 &&
1861 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001862 verbose(env, "R%d leaks addr into ctx\n", value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001863 return -EACCES;
1864 }
Edward Creef1174f72017-08-07 15:26:19 +01001865
Daniel Borkmann58990d12018-06-07 17:40:03 +02001866 err = check_ctx_reg(env, reg, regno);
1867 if (err < 0)
1868 return err;
1869
Yonghong Song31fd8582017-06-13 15:52:13 -07001870 err = check_ctx_access(env, insn_idx, off, size, t, &reg_type);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001871 if (!err && t == BPF_READ && value_regno >= 0) {
Edward Creef1174f72017-08-07 15:26:19 +01001872 /* ctx access returns either a scalar, or a
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001873 * PTR_TO_PACKET[_META,_END]. In the latter
1874 * case, we know the offset is zero.
Edward Creef1174f72017-08-07 15:26:19 +01001875 */
1876 if (reg_type == SCALAR_VALUE)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001877 mark_reg_unknown(env, regs, value_regno);
Edward Creef1174f72017-08-07 15:26:19 +01001878 else
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001879 mark_reg_known_zero(env, regs,
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001880 value_regno);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001881 regs[value_regno].type = reg_type;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001882 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001883
Edward Creef1174f72017-08-07 15:26:19 +01001884 } else if (reg->type == PTR_TO_STACK) {
1885 /* stack accesses must be at a fixed offset, so that we can
1886 * determine what type of data were returned.
1887 * See check_stack_read().
1888 */
1889 if (!tnum_is_const(reg->var_off)) {
1890 char tn_buf[48];
1891
1892 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001893 verbose(env, "variable stack access var_off=%s off=%d size=%d",
Edward Creef1174f72017-08-07 15:26:19 +01001894 tn_buf, off, size);
1895 return -EACCES;
1896 }
1897 off += reg->var_off.value;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001898 if (off >= 0 || off < -MAX_BPF_STACK) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001899 verbose(env, "invalid stack off=%d size=%d\n", off,
1900 size);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001901 return -EACCES;
1902 }
Alexei Starovoitov87266792017-05-30 13:31:29 -07001903
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001904 state = func(env, reg);
1905 err = update_stack_depth(env, state, off);
1906 if (err)
1907 return err;
Alexei Starovoitov87266792017-05-30 13:31:29 -07001908
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001909 if (t == BPF_WRITE)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001910 err = check_stack_write(env, state, off, size,
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07001911 value_regno, insn_idx);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001912 else
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001913 err = check_stack_read(env, state, off, size,
1914 value_regno);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001915 } else if (reg_is_pkt_pointer(reg)) {
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001916 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001917 verbose(env, "cannot write into packet\n");
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001918 return -EACCES;
1919 }
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001920 if (t == BPF_WRITE && value_regno >= 0 &&
1921 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001922 verbose(env, "R%d leaks addr into packet\n",
1923 value_regno);
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001924 return -EACCES;
1925 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001926 err = check_packet_access(env, regno, off, size, false);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001927 if (!err && t == BPF_READ && value_regno >= 0)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001928 mark_reg_unknown(env, regs, value_regno);
Petar Penkovd58e4682018-09-14 07:46:18 -07001929 } else if (reg->type == PTR_TO_FLOW_KEYS) {
1930 if (t == BPF_WRITE && value_regno >= 0 &&
1931 is_pointer_value(env, value_regno)) {
1932 verbose(env, "R%d leaks addr into flow keys\n",
1933 value_regno);
1934 return -EACCES;
1935 }
1936
1937 err = check_flow_keys_access(env, off, size);
1938 if (!err && t == BPF_READ && value_regno >= 0)
1939 mark_reg_unknown(env, regs, value_regno);
Joe Stringerc64b7982018-10-02 13:35:33 -07001940 } else if (reg->type == PTR_TO_SOCKET) {
1941 if (t == BPF_WRITE) {
1942 verbose(env, "cannot write into socket\n");
1943 return -EACCES;
1944 }
1945 err = check_sock_access(env, regno, off, size, t);
1946 if (!err && value_regno >= 0)
1947 mark_reg_unknown(env, regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001948 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001949 verbose(env, "R%d invalid mem access '%s'\n", regno,
1950 reg_type_str[reg->type]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001951 return -EACCES;
1952 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001953
Edward Creef1174f72017-08-07 15:26:19 +01001954 if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ &&
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001955 regs[value_regno].type == SCALAR_VALUE) {
Edward Creef1174f72017-08-07 15:26:19 +01001956 /* b/h/w load zero-extends, mark upper bits as known 0 */
Jann Horn0c17d1d2017-12-18 20:11:55 -08001957 coerce_reg_to_size(&regs[value_regno], size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001958 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001959 return err;
1960}
1961
Yonghong Song31fd8582017-06-13 15:52:13 -07001962static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001963{
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001964 int err;
1965
1966 if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
1967 insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001968 verbose(env, "BPF_XADD uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001969 return -EINVAL;
1970 }
1971
1972 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01001973 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001974 if (err)
1975 return err;
1976
1977 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01001978 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001979 if (err)
1980 return err;
1981
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +02001982 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001983 verbose(env, "R%d leaks addr into mem\n", insn->src_reg);
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +02001984 return -EACCES;
1985 }
1986
Daniel Borkmannca369602018-02-23 22:29:05 +01001987 if (is_ctx_reg(env, insn->dst_reg) ||
Daniel Borkmann4b5defd2018-10-21 02:09:25 +02001988 is_pkt_reg(env, insn->dst_reg) ||
1989 is_flow_key_reg(env, insn->dst_reg)) {
Daniel Borkmannca369602018-02-23 22:29:05 +01001990 verbose(env, "BPF_XADD stores into R%d %s is not allowed\n",
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001991 insn->dst_reg,
1992 reg_type_str[reg_state(env, insn->dst_reg)->type]);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01001993 return -EACCES;
1994 }
1995
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001996 /* check whether atomic_add can read the memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07001997 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Daniel Borkmannca369602018-02-23 22:29:05 +01001998 BPF_SIZE(insn->code), BPF_READ, -1, true);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001999 if (err)
2000 return err;
2001
2002 /* check whether atomic_add can write into the same memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07002003 return check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Daniel Borkmannca369602018-02-23 22:29:05 +01002004 BPF_SIZE(insn->code), BPF_WRITE, -1, true);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002005}
2006
2007/* when register 'regno' is passed into function that will read 'access_size'
2008 * bytes from that pointer, make sure that it's within stack boundary
Edward Creef1174f72017-08-07 15:26:19 +01002009 * and all elements of stack are initialized.
2010 * Unlike most pointer bounds-checking functions, this one doesn't take an
2011 * 'off' argument, so it has to add in reg->off itself.
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002012 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002013static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
Daniel Borkmann435faee12016-04-13 00:10:51 +02002014 int access_size, bool zero_size_allowed,
2015 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002016{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02002017 struct bpf_reg_state *reg = reg_state(env, regno);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002018 struct bpf_func_state *state = func(env, reg);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002019 int off, i, slot, spi;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002020
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002021 if (reg->type != PTR_TO_STACK) {
Edward Creef1174f72017-08-07 15:26:19 +01002022 /* Allow zero-byte read from NULL, regardless of pointer type */
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002023 if (zero_size_allowed && access_size == 0 &&
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002024 register_is_null(reg))
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002025 return 0;
2026
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002027 verbose(env, "R%d type=%s expected=%s\n", regno,
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002028 reg_type_str[reg->type],
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002029 reg_type_str[PTR_TO_STACK]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002030 return -EACCES;
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002031 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002032
Edward Creef1174f72017-08-07 15:26:19 +01002033 /* Only allow fixed-offset stack reads */
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002034 if (!tnum_is_const(reg->var_off)) {
Edward Creef1174f72017-08-07 15:26:19 +01002035 char tn_buf[48];
2036
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002037 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002038 verbose(env, "invalid variable stack read R%d var_off=%s\n",
Edward Creef1174f72017-08-07 15:26:19 +01002039 regno, tn_buf);
Jann Hornea25f912017-12-18 20:11:57 -08002040 return -EACCES;
Edward Creef1174f72017-08-07 15:26:19 +01002041 }
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002042 off = reg->off + reg->var_off.value;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002043 if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
Yonghong Song9fd29c02017-11-12 14:49:09 -08002044 access_size < 0 || (access_size == 0 && !zero_size_allowed)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002045 verbose(env, "invalid stack type R%d off=%d access_size=%d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002046 regno, off, access_size);
2047 return -EACCES;
2048 }
2049
Daniel Borkmann435faee12016-04-13 00:10:51 +02002050 if (meta && meta->raw_mode) {
2051 meta->access_size = access_size;
2052 meta->regno = regno;
2053 return 0;
2054 }
2055
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002056 for (i = 0; i < access_size; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002057 u8 *stype;
2058
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002059 slot = -(off + i) - 1;
2060 spi = slot / BPF_REG_SIZE;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002061 if (state->allocated_stack <= slot)
2062 goto err;
2063 stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE];
2064 if (*stype == STACK_MISC)
2065 goto mark;
2066 if (*stype == STACK_ZERO) {
2067 /* helper can write anything into the stack */
2068 *stype = STACK_MISC;
2069 goto mark;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002070 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002071err:
2072 verbose(env, "invalid indirect read from stack off %d+%d size %d\n",
2073 off, i, access_size);
2074 return -EACCES;
2075mark:
2076 /* reading any byte out of 8-byte 'spill_slot' will cause
2077 * the whole slot to be marked as 'read'
2078 */
Edward Cree679c7822018-08-22 20:02:19 +01002079 mark_reg_read(env, &state->stack[spi].spilled_ptr,
2080 state->stack[spi].spilled_ptr.parent);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002081 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002082 return update_stack_depth(env, state, off);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002083}
2084
Gianluca Borello06c1c042017-01-09 10:19:49 -08002085static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
2086 int access_size, bool zero_size_allowed,
2087 struct bpf_call_arg_meta *meta)
2088{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002089 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
Gianluca Borello06c1c042017-01-09 10:19:49 -08002090
Edward Creef1174f72017-08-07 15:26:19 +01002091 switch (reg->type) {
Gianluca Borello06c1c042017-01-09 10:19:49 -08002092 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002093 case PTR_TO_PACKET_META:
Yonghong Song9fd29c02017-11-12 14:49:09 -08002094 return check_packet_access(env, regno, reg->off, access_size,
2095 zero_size_allowed);
Gianluca Borello06c1c042017-01-09 10:19:49 -08002096 case PTR_TO_MAP_VALUE:
Yonghong Song9fd29c02017-11-12 14:49:09 -08002097 return check_map_access(env, regno, reg->off, access_size,
2098 zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01002099 default: /* scalar_value|ptr_to_stack or invalid ptr */
Gianluca Borello06c1c042017-01-09 10:19:49 -08002100 return check_stack_boundary(env, regno, access_size,
2101 zero_size_allowed, meta);
2102 }
2103}
2104
Daniel Borkmann90133412018-01-20 01:24:29 +01002105static bool arg_type_is_mem_ptr(enum bpf_arg_type type)
2106{
2107 return type == ARG_PTR_TO_MEM ||
2108 type == ARG_PTR_TO_MEM_OR_NULL ||
2109 type == ARG_PTR_TO_UNINIT_MEM;
2110}
2111
2112static bool arg_type_is_mem_size(enum bpf_arg_type type)
2113{
2114 return type == ARG_CONST_SIZE ||
2115 type == ARG_CONST_SIZE_OR_ZERO;
2116}
2117
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002118static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002119 enum bpf_arg_type arg_type,
2120 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002121{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002122 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002123 enum bpf_reg_type expected_type, type = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002124 int err = 0;
2125
Daniel Borkmann80f1d682015-03-12 17:21:42 +01002126 if (arg_type == ARG_DONTCARE)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002127 return 0;
2128
Edward Creedc503a82017-08-15 20:34:35 +01002129 err = check_reg_arg(env, regno, SRC_OP);
2130 if (err)
2131 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002132
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002133 if (arg_type == ARG_ANYTHING) {
2134 if (is_pointer_value(env, regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002135 verbose(env, "R%d leaks addr into helper function\n",
2136 regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002137 return -EACCES;
2138 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01002139 return 0;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002140 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01002141
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002142 if (type_is_pkt_pointer(type) &&
Thomas Graf3a0af8f2016-11-30 17:10:10 +01002143 !may_access_direct_pkt_data(env, meta, BPF_READ)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002144 verbose(env, "helper access to the packet is not allowed\n");
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002145 return -EACCES;
2146 }
2147
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002148 if (arg_type == ARG_PTR_TO_MAP_KEY ||
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002149 arg_type == ARG_PTR_TO_MAP_VALUE ||
2150 arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002151 expected_type = PTR_TO_STACK;
Paul Chaignond71962f2018-04-24 15:07:54 +02002152 if (!type_is_pkt_pointer(type) && type != PTR_TO_MAP_VALUE &&
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002153 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002154 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002155 } else if (arg_type == ARG_CONST_SIZE ||
2156 arg_type == ARG_CONST_SIZE_OR_ZERO) {
Edward Creef1174f72017-08-07 15:26:19 +01002157 expected_type = SCALAR_VALUE;
2158 if (type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002159 goto err_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002160 } else if (arg_type == ARG_CONST_MAP_PTR) {
2161 expected_type = CONST_PTR_TO_MAP;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002162 if (type != expected_type)
2163 goto err_type;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002164 } else if (arg_type == ARG_PTR_TO_CTX) {
2165 expected_type = PTR_TO_CTX;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002166 if (type != expected_type)
2167 goto err_type;
Daniel Borkmann58990d12018-06-07 17:40:03 +02002168 err = check_ctx_reg(env, reg, regno);
2169 if (err < 0)
2170 return err;
Joe Stringerc64b7982018-10-02 13:35:33 -07002171 } else if (arg_type == ARG_PTR_TO_SOCKET) {
2172 expected_type = PTR_TO_SOCKET;
2173 if (type != expected_type)
2174 goto err_type;
Joe Stringerfd978bf72018-10-02 13:35:35 -07002175 if (meta->ptr_id || !reg->id) {
2176 verbose(env, "verifier internal error: mismatched references meta=%d, reg=%d\n",
2177 meta->ptr_id, reg->id);
2178 return -EFAULT;
2179 }
2180 meta->ptr_id = reg->id;
Daniel Borkmann90133412018-01-20 01:24:29 +01002181 } else if (arg_type_is_mem_ptr(arg_type)) {
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002182 expected_type = PTR_TO_STACK;
2183 /* One exception here. In case function allows for NULL to be
Edward Creef1174f72017-08-07 15:26:19 +01002184 * passed in as argument, it's a SCALAR_VALUE type. Final test
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002185 * happens during stack boundary checking.
2186 */
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002187 if (register_is_null(reg) &&
Gianluca Borellodb1ac492017-11-22 18:32:53 +00002188 arg_type == ARG_PTR_TO_MEM_OR_NULL)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002189 /* final test in check_stack_boundary() */;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002190 else if (!type_is_pkt_pointer(type) &&
2191 type != PTR_TO_MAP_VALUE &&
Edward Creef1174f72017-08-07 15:26:19 +01002192 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002193 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002194 meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002195 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002196 verbose(env, "unsupported arg_type %d\n", arg_type);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002197 return -EFAULT;
2198 }
2199
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002200 if (arg_type == ARG_CONST_MAP_PTR) {
2201 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002202 meta->map_ptr = reg->map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002203 } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
2204 /* bpf_map_xxx(..., map_ptr, ..., key) call:
2205 * check that [key, key + map->key_size) are within
2206 * stack limits and initialized
2207 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002208 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002209 /* in function declaration map_ptr must come before
2210 * map_key, so that it's verified and known before
2211 * we have to check map_key here. Otherwise it means
2212 * that kernel subsystem misconfigured verifier
2213 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002214 verbose(env, "invalid map_ptr to access map->key\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002215 return -EACCES;
2216 }
Paul Chaignond71962f2018-04-24 15:07:54 +02002217 err = check_helper_mem_access(env, regno,
2218 meta->map_ptr->key_size, false,
2219 NULL);
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002220 } else if (arg_type == ARG_PTR_TO_MAP_VALUE ||
2221 arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002222 /* bpf_map_xxx(..., map_ptr, ..., value) call:
2223 * check [value, value + map->value_size) validity
2224 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002225 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002226 /* kernel subsystem misconfigured verifier */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002227 verbose(env, "invalid map_ptr to access map->value\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002228 return -EACCES;
2229 }
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002230 meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE);
Paul Chaignond71962f2018-04-24 15:07:54 +02002231 err = check_helper_mem_access(env, regno,
2232 meta->map_ptr->value_size, false,
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002233 meta);
Daniel Borkmann90133412018-01-20 01:24:29 +01002234 } else if (arg_type_is_mem_size(arg_type)) {
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002235 bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002236
Yonghong Song849fa502018-04-28 22:28:09 -07002237 /* remember the mem_size which may be used later
2238 * to refine return values.
2239 */
2240 meta->msize_smax_value = reg->smax_value;
2241 meta->msize_umax_value = reg->umax_value;
2242
Edward Creef1174f72017-08-07 15:26:19 +01002243 /* The register is SCALAR_VALUE; the access check
2244 * happens using its boundaries.
Gianluca Borello06c1c042017-01-09 10:19:49 -08002245 */
Edward Creef1174f72017-08-07 15:26:19 +01002246 if (!tnum_is_const(reg->var_off))
Gianluca Borello06c1c042017-01-09 10:19:49 -08002247 /* For unprivileged variable accesses, disable raw
2248 * mode so that the program is required to
2249 * initialize all the memory that the helper could
2250 * just partially fill up.
2251 */
2252 meta = NULL;
2253
Edward Creeb03c9f92017-08-07 15:26:36 +01002254 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002255 verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n",
Edward Creef1174f72017-08-07 15:26:19 +01002256 regno);
2257 return -EACCES;
2258 }
Gianluca Borello06c1c042017-01-09 10:19:49 -08002259
Edward Creeb03c9f92017-08-07 15:26:36 +01002260 if (reg->umin_value == 0) {
Edward Creef1174f72017-08-07 15:26:19 +01002261 err = check_helper_mem_access(env, regno - 1, 0,
2262 zero_size_allowed,
2263 meta);
Gianluca Borello06c1c042017-01-09 10:19:49 -08002264 if (err)
2265 return err;
Gianluca Borello06c1c042017-01-09 10:19:49 -08002266 }
Edward Creef1174f72017-08-07 15:26:19 +01002267
Edward Creeb03c9f92017-08-07 15:26:36 +01002268 if (reg->umax_value >= BPF_MAX_VAR_SIZ) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002269 verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
Edward Creef1174f72017-08-07 15:26:19 +01002270 regno);
2271 return -EACCES;
2272 }
2273 err = check_helper_mem_access(env, regno - 1,
Edward Creeb03c9f92017-08-07 15:26:36 +01002274 reg->umax_value,
Edward Creef1174f72017-08-07 15:26:19 +01002275 zero_size_allowed, meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002276 }
2277
2278 return err;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002279err_type:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002280 verbose(env, "R%d type=%s expected=%s\n", regno,
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002281 reg_type_str[type], reg_type_str[expected_type]);
2282 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002283}
2284
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002285static int check_map_func_compatibility(struct bpf_verifier_env *env,
2286 struct bpf_map *map, int func_id)
Kaixu Xia35578d72015-08-06 07:02:35 +00002287{
Kaixu Xia35578d72015-08-06 07:02:35 +00002288 if (!map)
2289 return 0;
2290
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002291 /* We need a two way check, first is from map perspective ... */
2292 switch (map->map_type) {
2293 case BPF_MAP_TYPE_PROG_ARRAY:
2294 if (func_id != BPF_FUNC_tail_call)
2295 goto error;
2296 break;
2297 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
2298 if (func_id != BPF_FUNC_perf_event_read &&
Yonghong Song908432c2017-10-05 09:19:20 -07002299 func_id != BPF_FUNC_perf_event_output &&
2300 func_id != BPF_FUNC_perf_event_read_value)
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002301 goto error;
2302 break;
2303 case BPF_MAP_TYPE_STACK_TRACE:
2304 if (func_id != BPF_FUNC_get_stackid)
2305 goto error;
2306 break;
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -07002307 case BPF_MAP_TYPE_CGROUP_ARRAY:
David S. Miller60747ef2016-08-18 01:17:32 -04002308 if (func_id != BPF_FUNC_skb_under_cgroup &&
Sargun Dhillon60d20f92016-08-12 08:56:52 -07002309 func_id != BPF_FUNC_current_task_under_cgroup)
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002310 goto error;
2311 break;
Roman Gushchincd339432018-08-02 14:27:24 -07002312 case BPF_MAP_TYPE_CGROUP_STORAGE:
Roman Gushchinb741f162018-09-28 14:45:43 +00002313 case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE:
Roman Gushchincd339432018-08-02 14:27:24 -07002314 if (func_id != BPF_FUNC_get_local_storage)
2315 goto error;
2316 break;
John Fastabend546ac1f2017-07-17 09:28:56 -07002317 /* devmap returns a pointer to a live net_device ifindex that we cannot
2318 * allow to be modified from bpf side. So do not allow lookup elements
2319 * for now.
2320 */
2321 case BPF_MAP_TYPE_DEVMAP:
John Fastabend2ddf71e2017-07-17 09:30:02 -07002322 if (func_id != BPF_FUNC_redirect_map)
John Fastabend546ac1f2017-07-17 09:28:56 -07002323 goto error;
2324 break;
Björn Töpelfbfc504a2018-05-02 13:01:28 +02002325 /* Restrict bpf side of cpumap and xskmap, open when use-cases
2326 * appear.
2327 */
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02002328 case BPF_MAP_TYPE_CPUMAP:
Björn Töpelfbfc504a2018-05-02 13:01:28 +02002329 case BPF_MAP_TYPE_XSKMAP:
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02002330 if (func_id != BPF_FUNC_redirect_map)
2331 goto error;
2332 break;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07002333 case BPF_MAP_TYPE_ARRAY_OF_MAPS:
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07002334 case BPF_MAP_TYPE_HASH_OF_MAPS:
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07002335 if (func_id != BPF_FUNC_map_lookup_elem)
2336 goto error;
Martin KaFai Lau16a43622017-08-17 18:14:43 -07002337 break;
John Fastabend174a79f2017-08-15 22:32:47 -07002338 case BPF_MAP_TYPE_SOCKMAP:
2339 if (func_id != BPF_FUNC_sk_redirect_map &&
2340 func_id != BPF_FUNC_sock_map_update &&
John Fastabend4f738ad2018-03-18 12:57:10 -07002341 func_id != BPF_FUNC_map_delete_elem &&
2342 func_id != BPF_FUNC_msg_redirect_map)
John Fastabend174a79f2017-08-15 22:32:47 -07002343 goto error;
2344 break;
John Fastabend81110382018-05-14 10:00:17 -07002345 case BPF_MAP_TYPE_SOCKHASH:
2346 if (func_id != BPF_FUNC_sk_redirect_hash &&
2347 func_id != BPF_FUNC_sock_hash_update &&
2348 func_id != BPF_FUNC_map_delete_elem &&
2349 func_id != BPF_FUNC_msg_redirect_hash)
2350 goto error;
2351 break;
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07002352 case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY:
2353 if (func_id != BPF_FUNC_sk_select_reuseport)
2354 goto error;
2355 break;
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02002356 case BPF_MAP_TYPE_QUEUE:
2357 case BPF_MAP_TYPE_STACK:
2358 if (func_id != BPF_FUNC_map_peek_elem &&
2359 func_id != BPF_FUNC_map_pop_elem &&
2360 func_id != BPF_FUNC_map_push_elem)
2361 goto error;
2362 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002363 default:
2364 break;
2365 }
2366
2367 /* ... and second from the function itself. */
2368 switch (func_id) {
2369 case BPF_FUNC_tail_call:
2370 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
2371 goto error;
Jiong Wangf910cef2018-05-02 16:17:17 -04002372 if (env->subprog_cnt > 1) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002373 verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n");
2374 return -EINVAL;
2375 }
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002376 break;
2377 case BPF_FUNC_perf_event_read:
2378 case BPF_FUNC_perf_event_output:
Yonghong Song908432c2017-10-05 09:19:20 -07002379 case BPF_FUNC_perf_event_read_value:
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002380 if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
2381 goto error;
2382 break;
2383 case BPF_FUNC_get_stackid:
2384 if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
2385 goto error;
2386 break;
Sargun Dhillon60d20f92016-08-12 08:56:52 -07002387 case BPF_FUNC_current_task_under_cgroup:
Daniel Borkmann747ea552016-08-12 22:17:17 +02002388 case BPF_FUNC_skb_under_cgroup:
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002389 if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
2390 goto error;
2391 break;
John Fastabend97f91a72017-07-17 09:29:18 -07002392 case BPF_FUNC_redirect_map:
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02002393 if (map->map_type != BPF_MAP_TYPE_DEVMAP &&
Björn Töpelfbfc504a2018-05-02 13:01:28 +02002394 map->map_type != BPF_MAP_TYPE_CPUMAP &&
2395 map->map_type != BPF_MAP_TYPE_XSKMAP)
John Fastabend97f91a72017-07-17 09:29:18 -07002396 goto error;
2397 break;
John Fastabend174a79f2017-08-15 22:32:47 -07002398 case BPF_FUNC_sk_redirect_map:
John Fastabend4f738ad2018-03-18 12:57:10 -07002399 case BPF_FUNC_msg_redirect_map:
John Fastabend81110382018-05-14 10:00:17 -07002400 case BPF_FUNC_sock_map_update:
John Fastabend174a79f2017-08-15 22:32:47 -07002401 if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
2402 goto error;
2403 break;
John Fastabend81110382018-05-14 10:00:17 -07002404 case BPF_FUNC_sk_redirect_hash:
2405 case BPF_FUNC_msg_redirect_hash:
2406 case BPF_FUNC_sock_hash_update:
2407 if (map->map_type != BPF_MAP_TYPE_SOCKHASH)
John Fastabend174a79f2017-08-15 22:32:47 -07002408 goto error;
2409 break;
Roman Gushchincd339432018-08-02 14:27:24 -07002410 case BPF_FUNC_get_local_storage:
Roman Gushchinb741f162018-09-28 14:45:43 +00002411 if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
2412 map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Roman Gushchincd339432018-08-02 14:27:24 -07002413 goto error;
2414 break;
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07002415 case BPF_FUNC_sk_select_reuseport:
2416 if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY)
2417 goto error;
2418 break;
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02002419 case BPF_FUNC_map_peek_elem:
2420 case BPF_FUNC_map_pop_elem:
2421 case BPF_FUNC_map_push_elem:
2422 if (map->map_type != BPF_MAP_TYPE_QUEUE &&
2423 map->map_type != BPF_MAP_TYPE_STACK)
2424 goto error;
2425 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002426 default:
2427 break;
Kaixu Xia35578d72015-08-06 07:02:35 +00002428 }
2429
2430 return 0;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002431error:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002432 verbose(env, "cannot pass map_type %d into func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02002433 map->map_type, func_id_name(func_id), func_id);
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002434 return -EINVAL;
Kaixu Xia35578d72015-08-06 07:02:35 +00002435}
2436
Daniel Borkmann90133412018-01-20 01:24:29 +01002437static bool check_raw_mode_ok(const struct bpf_func_proto *fn)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002438{
2439 int count = 0;
2440
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002441 if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002442 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002443 if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002444 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002445 if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002446 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002447 if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002448 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002449 if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002450 count++;
2451
Daniel Borkmann90133412018-01-20 01:24:29 +01002452 /* We only support one arg being in raw mode at the moment,
2453 * which is sufficient for the helper functions we have
2454 * right now.
2455 */
2456 return count <= 1;
2457}
2458
2459static bool check_args_pair_invalid(enum bpf_arg_type arg_curr,
2460 enum bpf_arg_type arg_next)
2461{
2462 return (arg_type_is_mem_ptr(arg_curr) &&
2463 !arg_type_is_mem_size(arg_next)) ||
2464 (!arg_type_is_mem_ptr(arg_curr) &&
2465 arg_type_is_mem_size(arg_next));
2466}
2467
2468static bool check_arg_pair_ok(const struct bpf_func_proto *fn)
2469{
2470 /* bpf_xxx(..., buf, len) call will access 'len'
2471 * bytes from memory 'buf'. Both arg types need
2472 * to be paired, so make sure there's no buggy
2473 * helper function specification.
2474 */
2475 if (arg_type_is_mem_size(fn->arg1_type) ||
2476 arg_type_is_mem_ptr(fn->arg5_type) ||
2477 check_args_pair_invalid(fn->arg1_type, fn->arg2_type) ||
2478 check_args_pair_invalid(fn->arg2_type, fn->arg3_type) ||
2479 check_args_pair_invalid(fn->arg3_type, fn->arg4_type) ||
2480 check_args_pair_invalid(fn->arg4_type, fn->arg5_type))
2481 return false;
2482
2483 return true;
2484}
2485
Joe Stringerfd978bf72018-10-02 13:35:35 -07002486static bool check_refcount_ok(const struct bpf_func_proto *fn)
2487{
2488 int count = 0;
2489
2490 if (arg_type_is_refcounted(fn->arg1_type))
2491 count++;
2492 if (arg_type_is_refcounted(fn->arg2_type))
2493 count++;
2494 if (arg_type_is_refcounted(fn->arg3_type))
2495 count++;
2496 if (arg_type_is_refcounted(fn->arg4_type))
2497 count++;
2498 if (arg_type_is_refcounted(fn->arg5_type))
2499 count++;
2500
2501 /* We only support one arg being unreferenced at the moment,
2502 * which is sufficient for the helper functions we have right now.
2503 */
2504 return count <= 1;
2505}
2506
Daniel Borkmann90133412018-01-20 01:24:29 +01002507static int check_func_proto(const struct bpf_func_proto *fn)
2508{
2509 return check_raw_mode_ok(fn) &&
Joe Stringerfd978bf72018-10-02 13:35:35 -07002510 check_arg_pair_ok(fn) &&
2511 check_refcount_ok(fn) ? 0 : -EINVAL;
Daniel Borkmann435faee12016-04-13 00:10:51 +02002512}
2513
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002514/* Packet data might have moved, any old PTR_TO_PACKET[_META,_END]
2515 * are now invalid, so turn them into unknown SCALAR_VALUE.
Edward Creef1174f72017-08-07 15:26:19 +01002516 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002517static void __clear_all_pkt_pointers(struct bpf_verifier_env *env,
2518 struct bpf_func_state *state)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002519{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002520 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002521 int i;
2522
2523 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002524 if (reg_is_pkt_pointer_any(&regs[i]))
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002525 mark_reg_unknown(env, regs, i);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002526
Joe Stringerf3709f62018-10-02 13:35:29 -07002527 bpf_for_each_spilled_reg(i, state, reg) {
2528 if (!reg)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002529 continue;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002530 if (reg_is_pkt_pointer_any(reg))
2531 __mark_reg_unknown(reg);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002532 }
2533}
2534
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002535static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
2536{
2537 struct bpf_verifier_state *vstate = env->cur_state;
2538 int i;
2539
2540 for (i = 0; i <= vstate->curframe; i++)
2541 __clear_all_pkt_pointers(env, vstate->frame[i]);
2542}
2543
Joe Stringerfd978bf72018-10-02 13:35:35 -07002544static void release_reg_references(struct bpf_verifier_env *env,
2545 struct bpf_func_state *state, int id)
2546{
2547 struct bpf_reg_state *regs = state->regs, *reg;
2548 int i;
2549
2550 for (i = 0; i < MAX_BPF_REG; i++)
2551 if (regs[i].id == id)
2552 mark_reg_unknown(env, regs, i);
2553
2554 bpf_for_each_spilled_reg(i, state, reg) {
2555 if (!reg)
2556 continue;
2557 if (reg_is_refcounted(reg) && reg->id == id)
2558 __mark_reg_unknown(reg);
2559 }
2560}
2561
2562/* The pointer with the specified id has released its reference to kernel
2563 * resources. Identify all copies of the same pointer and clear the reference.
2564 */
2565static int release_reference(struct bpf_verifier_env *env,
2566 struct bpf_call_arg_meta *meta)
2567{
2568 struct bpf_verifier_state *vstate = env->cur_state;
2569 int i;
2570
2571 for (i = 0; i <= vstate->curframe; i++)
2572 release_reg_references(env, vstate->frame[i], meta->ptr_id);
2573
2574 return release_reference_state(env, meta->ptr_id);
2575}
2576
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002577static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
2578 int *insn_idx)
2579{
2580 struct bpf_verifier_state *state = env->cur_state;
2581 struct bpf_func_state *caller, *callee;
Joe Stringerfd978bf72018-10-02 13:35:35 -07002582 int i, err, subprog, target_insn;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002583
Alexei Starovoitovaada9ce2017-12-25 13:15:42 -08002584 if (state->curframe + 1 >= MAX_CALL_FRAMES) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002585 verbose(env, "the call stack of %d frames is too deep\n",
Alexei Starovoitovaada9ce2017-12-25 13:15:42 -08002586 state->curframe + 2);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002587 return -E2BIG;
2588 }
2589
2590 target_insn = *insn_idx + insn->imm;
2591 subprog = find_subprog(env, target_insn + 1);
2592 if (subprog < 0) {
2593 verbose(env, "verifier bug. No program starts at insn %d\n",
2594 target_insn + 1);
2595 return -EFAULT;
2596 }
2597
2598 caller = state->frame[state->curframe];
2599 if (state->frame[state->curframe + 1]) {
2600 verbose(env, "verifier bug. Frame %d already allocated\n",
2601 state->curframe + 1);
2602 return -EFAULT;
2603 }
2604
2605 callee = kzalloc(sizeof(*callee), GFP_KERNEL);
2606 if (!callee)
2607 return -ENOMEM;
2608 state->frame[state->curframe + 1] = callee;
2609
2610 /* callee cannot access r0, r6 - r9 for reading and has to write
2611 * into its own stack before reading from it.
2612 * callee can read/write into caller's stack
2613 */
2614 init_func_state(env, callee,
2615 /* remember the callsite, it will be used by bpf_exit */
2616 *insn_idx /* callsite */,
2617 state->curframe + 1 /* frameno within this callchain */,
Jiong Wangf910cef2018-05-02 16:17:17 -04002618 subprog /* subprog number within this prog */);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002619
Joe Stringerfd978bf72018-10-02 13:35:35 -07002620 /* Transfer references to the callee */
2621 err = transfer_reference_state(callee, caller);
2622 if (err)
2623 return err;
2624
Edward Cree679c7822018-08-22 20:02:19 +01002625 /* copy r1 - r5 args that callee can access. The copy includes parent
2626 * pointers, which connects us up to the liveness chain
2627 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002628 for (i = BPF_REG_1; i <= BPF_REG_5; i++)
2629 callee->regs[i] = caller->regs[i];
2630
Edward Cree679c7822018-08-22 20:02:19 +01002631 /* after the call registers r0 - r5 were scratched */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002632 for (i = 0; i < CALLER_SAVED_REGS; i++) {
2633 mark_reg_not_init(env, caller->regs, caller_saved[i]);
2634 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
2635 }
2636
2637 /* only increment it after check_reg_arg() finished */
2638 state->curframe++;
2639
2640 /* and go analyze first insn of the callee */
2641 *insn_idx = target_insn;
2642
2643 if (env->log.level) {
2644 verbose(env, "caller:\n");
2645 print_verifier_state(env, caller);
2646 verbose(env, "callee:\n");
2647 print_verifier_state(env, callee);
2648 }
2649 return 0;
2650}
2651
2652static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
2653{
2654 struct bpf_verifier_state *state = env->cur_state;
2655 struct bpf_func_state *caller, *callee;
2656 struct bpf_reg_state *r0;
Joe Stringerfd978bf72018-10-02 13:35:35 -07002657 int err;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002658
2659 callee = state->frame[state->curframe];
2660 r0 = &callee->regs[BPF_REG_0];
2661 if (r0->type == PTR_TO_STACK) {
2662 /* technically it's ok to return caller's stack pointer
2663 * (or caller's caller's pointer) back to the caller,
2664 * since these pointers are valid. Only current stack
2665 * pointer will be invalid as soon as function exits,
2666 * but let's be conservative
2667 */
2668 verbose(env, "cannot return stack pointer to the caller\n");
2669 return -EINVAL;
2670 }
2671
2672 state->curframe--;
2673 caller = state->frame[state->curframe];
2674 /* return to the caller whatever r0 had in the callee */
2675 caller->regs[BPF_REG_0] = *r0;
2676
Joe Stringerfd978bf72018-10-02 13:35:35 -07002677 /* Transfer references to the caller */
2678 err = transfer_reference_state(caller, callee);
2679 if (err)
2680 return err;
2681
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002682 *insn_idx = callee->callsite + 1;
2683 if (env->log.level) {
2684 verbose(env, "returning from callee:\n");
2685 print_verifier_state(env, callee);
2686 verbose(env, "to caller at %d:\n", *insn_idx);
2687 print_verifier_state(env, caller);
2688 }
2689 /* clear everything in the callee */
2690 free_func_state(callee);
2691 state->frame[state->curframe + 1] = NULL;
2692 return 0;
2693}
2694
Yonghong Song849fa502018-04-28 22:28:09 -07002695static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type,
2696 int func_id,
2697 struct bpf_call_arg_meta *meta)
2698{
2699 struct bpf_reg_state *ret_reg = &regs[BPF_REG_0];
2700
2701 if (ret_type != RET_INTEGER ||
2702 (func_id != BPF_FUNC_get_stack &&
2703 func_id != BPF_FUNC_probe_read_str))
2704 return;
2705
2706 ret_reg->smax_value = meta->msize_smax_value;
2707 ret_reg->umax_value = meta->msize_umax_value;
2708 __reg_deduce_bounds(ret_reg);
2709 __reg_bound_offset(ret_reg);
2710}
2711
Daniel Borkmannc93552c2018-05-24 02:32:53 +02002712static int
2713record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,
2714 int func_id, int insn_idx)
2715{
2716 struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx];
2717
2718 if (func_id != BPF_FUNC_tail_call &&
Daniel Borkmann09772d92018-06-02 23:06:35 +02002719 func_id != BPF_FUNC_map_lookup_elem &&
2720 func_id != BPF_FUNC_map_update_elem &&
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02002721 func_id != BPF_FUNC_map_delete_elem &&
2722 func_id != BPF_FUNC_map_push_elem &&
2723 func_id != BPF_FUNC_map_pop_elem &&
2724 func_id != BPF_FUNC_map_peek_elem)
Daniel Borkmannc93552c2018-05-24 02:32:53 +02002725 return 0;
Daniel Borkmann09772d92018-06-02 23:06:35 +02002726
Daniel Borkmannc93552c2018-05-24 02:32:53 +02002727 if (meta->map_ptr == NULL) {
2728 verbose(env, "kernel subsystem misconfigured verifier\n");
2729 return -EINVAL;
2730 }
2731
2732 if (!BPF_MAP_PTR(aux->map_state))
2733 bpf_map_ptr_store(aux, meta->map_ptr,
2734 meta->map_ptr->unpriv_array);
2735 else if (BPF_MAP_PTR(aux->map_state) != meta->map_ptr)
2736 bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON,
2737 meta->map_ptr->unpriv_array);
2738 return 0;
2739}
2740
Joe Stringerfd978bf72018-10-02 13:35:35 -07002741static int check_reference_leak(struct bpf_verifier_env *env)
2742{
2743 struct bpf_func_state *state = cur_func(env);
2744 int i;
2745
2746 for (i = 0; i < state->acquired_refs; i++) {
2747 verbose(env, "Unreleased reference id=%d alloc_insn=%d\n",
2748 state->refs[i].id, state->refs[i].insn_idx);
2749 }
2750 return state->acquired_refs ? -EINVAL : 0;
2751}
2752
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002753static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002754{
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002755 const struct bpf_func_proto *fn = NULL;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002756 struct bpf_reg_state *regs;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002757 struct bpf_call_arg_meta meta;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002758 bool changes_data;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002759 int i, err;
2760
2761 /* find function prototype */
2762 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002763 verbose(env, "invalid func %s#%d\n", func_id_name(func_id),
2764 func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002765 return -EINVAL;
2766 }
2767
Jakub Kicinski00176a32017-10-16 16:40:54 -07002768 if (env->ops->get_func_proto)
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002769 fn = env->ops->get_func_proto(func_id, env->prog);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002770 if (!fn) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002771 verbose(env, "unknown func %s#%d\n", func_id_name(func_id),
2772 func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002773 return -EINVAL;
2774 }
2775
2776 /* eBPF programs must be GPL compatible to use GPL-ed functions */
Daniel Borkmann24701ec2015-03-01 12:31:47 +01002777 if (!env->prog->gpl_compatible && fn->gpl_only) {
Daniel Borkmann3fe28672018-06-02 23:06:33 +02002778 verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002779 return -EINVAL;
2780 }
2781
Daniel Borkmann04514d12017-12-14 21:07:25 +01002782 /* With LD_ABS/IND some JITs save/restore skb from r1. */
Martin KaFai Lau17bedab2016-12-07 15:53:11 -08002783 changes_data = bpf_helper_changes_pkt_data(fn->func);
Daniel Borkmann04514d12017-12-14 21:07:25 +01002784 if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) {
2785 verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n",
2786 func_id_name(func_id), func_id);
2787 return -EINVAL;
2788 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002789
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002790 memset(&meta, 0, sizeof(meta));
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002791 meta.pkt_access = fn->pkt_access;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002792
Daniel Borkmann90133412018-01-20 01:24:29 +01002793 err = check_func_proto(fn);
Daniel Borkmann435faee12016-04-13 00:10:51 +02002794 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002795 verbose(env, "kernel subsystem misconfigured func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02002796 func_id_name(func_id), func_id);
Daniel Borkmann435faee12016-04-13 00:10:51 +02002797 return err;
2798 }
2799
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002800 /* check args */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002801 err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002802 if (err)
2803 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002804 err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002805 if (err)
2806 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002807 err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002808 if (err)
2809 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002810 err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002811 if (err)
2812 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002813 err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002814 if (err)
2815 return err;
2816
Daniel Borkmannc93552c2018-05-24 02:32:53 +02002817 err = record_func_map(env, &meta, func_id, insn_idx);
2818 if (err)
2819 return err;
2820
Daniel Borkmann435faee12016-04-13 00:10:51 +02002821 /* Mark slots with STACK_MISC in case of raw mode, stack offset
2822 * is inferred from register state.
2823 */
2824 for (i = 0; i < meta.access_size; i++) {
Daniel Borkmannca369602018-02-23 22:29:05 +01002825 err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B,
2826 BPF_WRITE, -1, false);
Daniel Borkmann435faee12016-04-13 00:10:51 +02002827 if (err)
2828 return err;
2829 }
2830
Joe Stringerfd978bf72018-10-02 13:35:35 -07002831 if (func_id == BPF_FUNC_tail_call) {
2832 err = check_reference_leak(env);
2833 if (err) {
2834 verbose(env, "tail_call would lead to reference leak\n");
2835 return err;
2836 }
2837 } else if (is_release_function(func_id)) {
2838 err = release_reference(env, &meta);
2839 if (err)
2840 return err;
2841 }
2842
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002843 regs = cur_regs(env);
Roman Gushchincd339432018-08-02 14:27:24 -07002844
2845 /* check that flags argument in get_local_storage(map, flags) is 0,
2846 * this is required because get_local_storage() can't return an error.
2847 */
2848 if (func_id == BPF_FUNC_get_local_storage &&
2849 !register_is_null(&regs[BPF_REG_2])) {
2850 verbose(env, "get_local_storage() doesn't support non-zero flags\n");
2851 return -EINVAL;
2852 }
2853
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002854 /* reset caller saved regs */
Edward Creedc503a82017-08-15 20:34:35 +01002855 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002856 mark_reg_not_init(env, regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01002857 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
2858 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002859
Edward Creedc503a82017-08-15 20:34:35 +01002860 /* update return register (already marked as written above) */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002861 if (fn->ret_type == RET_INTEGER) {
Edward Creef1174f72017-08-07 15:26:19 +01002862 /* sets type to SCALAR_VALUE */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002863 mark_reg_unknown(env, regs, BPF_REG_0);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002864 } else if (fn->ret_type == RET_VOID) {
2865 regs[BPF_REG_0].type = NOT_INIT;
Roman Gushchin3e6a4b32018-08-02 14:27:22 -07002866 } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL ||
2867 fn->ret_type == RET_PTR_TO_MAP_VALUE) {
Edward Creef1174f72017-08-07 15:26:19 +01002868 /* There is no offset yet applied, variable or fixed */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002869 mark_reg_known_zero(env, regs, BPF_REG_0);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002870 /* remember map_ptr, so that check_map_access()
2871 * can check 'value_size' boundary of memory access
2872 * to map element returned from bpf_map_lookup_elem()
2873 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002874 if (meta.map_ptr == NULL) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002875 verbose(env,
2876 "kernel subsystem misconfigured verifier\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002877 return -EINVAL;
2878 }
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002879 regs[BPF_REG_0].map_ptr = meta.map_ptr;
Daniel Borkmann4d31f302018-11-01 00:05:53 +01002880 if (fn->ret_type == RET_PTR_TO_MAP_VALUE) {
2881 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE;
2882 } else {
2883 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
2884 regs[BPF_REG_0].id = ++env->id_gen;
2885 }
Joe Stringerc64b7982018-10-02 13:35:33 -07002886 } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) {
Joe Stringerfd978bf72018-10-02 13:35:35 -07002887 int id = acquire_reference_state(env, insn_idx);
2888 if (id < 0)
2889 return id;
Joe Stringerc64b7982018-10-02 13:35:33 -07002890 mark_reg_known_zero(env, regs, BPF_REG_0);
2891 regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL;
Joe Stringerfd978bf72018-10-02 13:35:35 -07002892 regs[BPF_REG_0].id = id;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002893 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002894 verbose(env, "unknown return type %d of func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02002895 fn->ret_type, func_id_name(func_id), func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002896 return -EINVAL;
2897 }
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07002898
Yonghong Song849fa502018-04-28 22:28:09 -07002899 do_refine_retval_range(regs, fn->ret_type, func_id, &meta);
2900
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002901 err = check_map_func_compatibility(env, meta.map_ptr, func_id);
Kaixu Xia35578d72015-08-06 07:02:35 +00002902 if (err)
2903 return err;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07002904
Yonghong Songc195651e2018-04-28 22:28:08 -07002905 if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) {
2906 const char *err_str;
2907
2908#ifdef CONFIG_PERF_EVENTS
2909 err = get_callchain_buffers(sysctl_perf_event_max_stack);
2910 err_str = "cannot get callchain buffer for func %s#%d\n";
2911#else
2912 err = -ENOTSUPP;
2913 err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n";
2914#endif
2915 if (err) {
2916 verbose(env, err_str, func_id_name(func_id), func_id);
2917 return err;
2918 }
2919
2920 env->prog->has_callchain_buf = true;
2921 }
2922
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002923 if (changes_data)
2924 clear_all_pkt_pointers(env);
2925 return 0;
2926}
2927
Edward Creeb03c9f92017-08-07 15:26:36 +01002928static bool signed_add_overflows(s64 a, s64 b)
2929{
2930 /* Do the add in u64, where overflow is well-defined */
2931 s64 res = (s64)((u64)a + (u64)b);
2932
2933 if (b < 0)
2934 return res > a;
2935 return res < a;
2936}
2937
2938static bool signed_sub_overflows(s64 a, s64 b)
2939{
2940 /* Do the sub in u64, where overflow is well-defined */
2941 s64 res = (s64)((u64)a - (u64)b);
2942
2943 if (b < 0)
2944 return res < a;
2945 return res > a;
David S. Millerd1174412017-05-10 11:22:52 -07002946}
2947
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08002948static bool check_reg_sane_offset(struct bpf_verifier_env *env,
2949 const struct bpf_reg_state *reg,
2950 enum bpf_reg_type type)
2951{
2952 bool known = tnum_is_const(reg->var_off);
2953 s64 val = reg->var_off.value;
2954 s64 smin = reg->smin_value;
2955
2956 if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) {
2957 verbose(env, "math between %s pointer and %lld is not allowed\n",
2958 reg_type_str[type], val);
2959 return false;
2960 }
2961
2962 if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) {
2963 verbose(env, "%s pointer offset %d is not allowed\n",
2964 reg_type_str[type], reg->off);
2965 return false;
2966 }
2967
2968 if (smin == S64_MIN) {
2969 verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n",
2970 reg_type_str[type]);
2971 return false;
2972 }
2973
2974 if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) {
2975 verbose(env, "value %lld makes %s pointer be out of bounds\n",
2976 smin, reg_type_str[type]);
2977 return false;
2978 }
2979
2980 return true;
2981}
2982
Edward Creef1174f72017-08-07 15:26:19 +01002983/* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off.
Edward Creef1174f72017-08-07 15:26:19 +01002984 * Caller should also handle BPF_MOV case separately.
2985 * If we return -EACCES, caller may want to try again treating pointer as a
2986 * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks.
2987 */
2988static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
2989 struct bpf_insn *insn,
2990 const struct bpf_reg_state *ptr_reg,
2991 const struct bpf_reg_state *off_reg)
Josef Bacik48461132016-09-28 10:54:32 -04002992{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002993 struct bpf_verifier_state *vstate = env->cur_state;
2994 struct bpf_func_state *state = vstate->frame[vstate->curframe];
2995 struct bpf_reg_state *regs = state->regs, *dst_reg;
Edward Creef1174f72017-08-07 15:26:19 +01002996 bool known = tnum_is_const(off_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01002997 s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value,
2998 smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
2999 u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value,
3000 umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value;
Josef Bacik48461132016-09-28 10:54:32 -04003001 u8 opcode = BPF_OP(insn->code);
Edward Creef1174f72017-08-07 15:26:19 +01003002 u32 dst = insn->dst_reg;
Josef Bacik48461132016-09-28 10:54:32 -04003003
Edward Creef1174f72017-08-07 15:26:19 +01003004 dst_reg = &regs[dst];
Josef Bacik48461132016-09-28 10:54:32 -04003005
Daniel Borkmann6f161012018-01-18 01:15:21 +01003006 if ((known && (smin_val != smax_val || umin_val != umax_val)) ||
3007 smin_val > smax_val || umin_val > umax_val) {
3008 /* Taint dst register if offset had invalid bounds derived from
3009 * e.g. dead branches.
3010 */
3011 __mark_reg_unknown(dst_reg);
3012 return 0;
Josef Bacik48461132016-09-28 10:54:32 -04003013 }
3014
Edward Creef1174f72017-08-07 15:26:19 +01003015 if (BPF_CLASS(insn->code) != BPF_ALU64) {
3016 /* 32-bit ALU ops on pointers produce (meaningless) scalars */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003017 verbose(env,
3018 "R%d 32-bit pointer arithmetic prohibited\n",
3019 dst);
Edward Creef1174f72017-08-07 15:26:19 +01003020 return -EACCES;
3021 }
David S. Millerd1174412017-05-10 11:22:52 -07003022
Joe Stringeraad2eea2018-10-02 13:35:30 -07003023 switch (ptr_reg->type) {
3024 case PTR_TO_MAP_VALUE_OR_NULL:
3025 verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n",
3026 dst, reg_type_str[ptr_reg->type]);
Edward Creef1174f72017-08-07 15:26:19 +01003027 return -EACCES;
Joe Stringeraad2eea2018-10-02 13:35:30 -07003028 case CONST_PTR_TO_MAP:
3029 case PTR_TO_PACKET_END:
Joe Stringerc64b7982018-10-02 13:35:33 -07003030 case PTR_TO_SOCKET:
3031 case PTR_TO_SOCKET_OR_NULL:
Joe Stringeraad2eea2018-10-02 13:35:30 -07003032 verbose(env, "R%d pointer arithmetic on %s prohibited\n",
3033 dst, reg_type_str[ptr_reg->type]);
Edward Creef1174f72017-08-07 15:26:19 +01003034 return -EACCES;
Joe Stringeraad2eea2018-10-02 13:35:30 -07003035 default:
3036 break;
Edward Creef1174f72017-08-07 15:26:19 +01003037 }
3038
3039 /* In case of 'scalar += pointer', dst_reg inherits pointer type and id.
3040 * The id may be overwritten later if we create a new variable offset.
Josef Bacik48461132016-09-28 10:54:32 -04003041 */
Edward Creef1174f72017-08-07 15:26:19 +01003042 dst_reg->type = ptr_reg->type;
3043 dst_reg->id = ptr_reg->id;
Josef Bacikf23cc642016-11-14 15:45:36 -05003044
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08003045 if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) ||
3046 !check_reg_sane_offset(env, ptr_reg, ptr_reg->type))
3047 return -EINVAL;
3048
Josef Bacik48461132016-09-28 10:54:32 -04003049 switch (opcode) {
3050 case BPF_ADD:
Edward Creef1174f72017-08-07 15:26:19 +01003051 /* We can take a fixed offset as long as it doesn't overflow
3052 * the s32 'off' field
3053 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003054 if (known && (ptr_reg->off + smin_val ==
3055 (s64)(s32)(ptr_reg->off + smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01003056 /* pointer += K. Accumulate it into fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01003057 dst_reg->smin_value = smin_ptr;
3058 dst_reg->smax_value = smax_ptr;
3059 dst_reg->umin_value = umin_ptr;
3060 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01003061 dst_reg->var_off = ptr_reg->var_off;
Edward Creeb03c9f92017-08-07 15:26:36 +01003062 dst_reg->off = ptr_reg->off + smin_val;
Daniel Borkmann09625902018-11-01 00:05:52 +01003063 dst_reg->raw = ptr_reg->raw;
Edward Creef1174f72017-08-07 15:26:19 +01003064 break;
3065 }
Edward Creef1174f72017-08-07 15:26:19 +01003066 /* A new variable offset is created. Note that off_reg->off
3067 * == 0, since it's a scalar.
3068 * dst_reg gets the pointer type and since some positive
3069 * integer value was added to the pointer, give it a new 'id'
3070 * if it's a PTR_TO_PACKET.
3071 * this creates a new 'base' pointer, off_reg (variable) gets
3072 * added into the variable offset, and we copy the fixed offset
3073 * from ptr_reg.
3074 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003075 if (signed_add_overflows(smin_ptr, smin_val) ||
3076 signed_add_overflows(smax_ptr, smax_val)) {
3077 dst_reg->smin_value = S64_MIN;
3078 dst_reg->smax_value = S64_MAX;
3079 } else {
3080 dst_reg->smin_value = smin_ptr + smin_val;
3081 dst_reg->smax_value = smax_ptr + smax_val;
3082 }
3083 if (umin_ptr + umin_val < umin_ptr ||
3084 umax_ptr + umax_val < umax_ptr) {
3085 dst_reg->umin_value = 0;
3086 dst_reg->umax_value = U64_MAX;
3087 } else {
3088 dst_reg->umin_value = umin_ptr + umin_val;
3089 dst_reg->umax_value = umax_ptr + umax_val;
3090 }
Edward Creef1174f72017-08-07 15:26:19 +01003091 dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off);
3092 dst_reg->off = ptr_reg->off;
Daniel Borkmann09625902018-11-01 00:05:52 +01003093 dst_reg->raw = ptr_reg->raw;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003094 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01003095 dst_reg->id = ++env->id_gen;
3096 /* something was added to pkt_ptr, set range to zero */
Daniel Borkmann09625902018-11-01 00:05:52 +01003097 dst_reg->raw = 0;
Edward Creef1174f72017-08-07 15:26:19 +01003098 }
Josef Bacik48461132016-09-28 10:54:32 -04003099 break;
3100 case BPF_SUB:
Edward Creef1174f72017-08-07 15:26:19 +01003101 if (dst_reg == off_reg) {
3102 /* scalar -= pointer. Creates an unknown scalar */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003103 verbose(env, "R%d tried to subtract pointer from scalar\n",
3104 dst);
Edward Creef1174f72017-08-07 15:26:19 +01003105 return -EACCES;
3106 }
3107 /* We don't allow subtraction from FP, because (according to
3108 * test_verifier.c test "invalid fp arithmetic", JITs might not
3109 * be able to deal with it.
Edward Cree93057062017-07-21 14:37:34 +01003110 */
Edward Creef1174f72017-08-07 15:26:19 +01003111 if (ptr_reg->type == PTR_TO_STACK) {
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003112 verbose(env, "R%d subtraction from stack pointer prohibited\n",
3113 dst);
Edward Creef1174f72017-08-07 15:26:19 +01003114 return -EACCES;
3115 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003116 if (known && (ptr_reg->off - smin_val ==
3117 (s64)(s32)(ptr_reg->off - smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01003118 /* pointer -= K. Subtract it from fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01003119 dst_reg->smin_value = smin_ptr;
3120 dst_reg->smax_value = smax_ptr;
3121 dst_reg->umin_value = umin_ptr;
3122 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01003123 dst_reg->var_off = ptr_reg->var_off;
3124 dst_reg->id = ptr_reg->id;
Edward Creeb03c9f92017-08-07 15:26:36 +01003125 dst_reg->off = ptr_reg->off - smin_val;
Daniel Borkmann09625902018-11-01 00:05:52 +01003126 dst_reg->raw = ptr_reg->raw;
Edward Creef1174f72017-08-07 15:26:19 +01003127 break;
3128 }
Edward Creef1174f72017-08-07 15:26:19 +01003129 /* A new variable offset is created. If the subtrahend is known
3130 * nonnegative, then any reg->range we had before is still good.
3131 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003132 if (signed_sub_overflows(smin_ptr, smax_val) ||
3133 signed_sub_overflows(smax_ptr, smin_val)) {
3134 /* Overflow possible, we know nothing */
3135 dst_reg->smin_value = S64_MIN;
3136 dst_reg->smax_value = S64_MAX;
3137 } else {
3138 dst_reg->smin_value = smin_ptr - smax_val;
3139 dst_reg->smax_value = smax_ptr - smin_val;
3140 }
3141 if (umin_ptr < umax_val) {
3142 /* Overflow possible, we know nothing */
3143 dst_reg->umin_value = 0;
3144 dst_reg->umax_value = U64_MAX;
3145 } else {
3146 /* Cannot overflow (as long as bounds are consistent) */
3147 dst_reg->umin_value = umin_ptr - umax_val;
3148 dst_reg->umax_value = umax_ptr - umin_val;
3149 }
Edward Creef1174f72017-08-07 15:26:19 +01003150 dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off);
3151 dst_reg->off = ptr_reg->off;
Daniel Borkmann09625902018-11-01 00:05:52 +01003152 dst_reg->raw = ptr_reg->raw;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003153 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01003154 dst_reg->id = ++env->id_gen;
3155 /* something was added to pkt_ptr, set range to zero */
Edward Creeb03c9f92017-08-07 15:26:36 +01003156 if (smin_val < 0)
Daniel Borkmann09625902018-11-01 00:05:52 +01003157 dst_reg->raw = 0;
Edward Creef1174f72017-08-07 15:26:19 +01003158 }
3159 break;
3160 case BPF_AND:
3161 case BPF_OR:
3162 case BPF_XOR:
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003163 /* bitwise ops on pointers are troublesome, prohibit. */
3164 verbose(env, "R%d bitwise operator %s on pointer prohibited\n",
3165 dst, bpf_alu_string[opcode >> 4]);
Edward Creef1174f72017-08-07 15:26:19 +01003166 return -EACCES;
3167 default:
3168 /* other operators (e.g. MUL,LSH) produce non-pointer results */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003169 verbose(env, "R%d pointer arithmetic with %s operator prohibited\n",
3170 dst, bpf_alu_string[opcode >> 4]);
Edward Creef1174f72017-08-07 15:26:19 +01003171 return -EACCES;
3172 }
3173
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08003174 if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type))
3175 return -EINVAL;
3176
Edward Creeb03c9f92017-08-07 15:26:36 +01003177 __update_reg_bounds(dst_reg);
3178 __reg_deduce_bounds(dst_reg);
3179 __reg_bound_offset(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003180 return 0;
3181}
3182
Jann Horn468f6ea2017-12-18 20:11:56 -08003183/* WARNING: This function does calculations on 64-bit values, but the actual
3184 * execution may occur on 32-bit values. Therefore, things like bitshifts
3185 * need extra checks in the 32-bit case.
3186 */
Edward Creef1174f72017-08-07 15:26:19 +01003187static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
3188 struct bpf_insn *insn,
3189 struct bpf_reg_state *dst_reg,
3190 struct bpf_reg_state src_reg)
3191{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003192 struct bpf_reg_state *regs = cur_regs(env);
Edward Creef1174f72017-08-07 15:26:19 +01003193 u8 opcode = BPF_OP(insn->code);
3194 bool src_known, dst_known;
Edward Creeb03c9f92017-08-07 15:26:36 +01003195 s64 smin_val, smax_val;
3196 u64 umin_val, umax_val;
Jann Horn468f6ea2017-12-18 20:11:56 -08003197 u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32;
Edward Creef1174f72017-08-07 15:26:19 +01003198
Jann Hornb7992072018-10-05 18:17:59 +02003199 if (insn_bitness == 32) {
3200 /* Relevant for 32-bit RSH: Information can propagate towards
3201 * LSB, so it isn't sufficient to only truncate the output to
3202 * 32 bits.
3203 */
3204 coerce_reg_to_size(dst_reg, 4);
3205 coerce_reg_to_size(&src_reg, 4);
3206 }
3207
Edward Creeb03c9f92017-08-07 15:26:36 +01003208 smin_val = src_reg.smin_value;
3209 smax_val = src_reg.smax_value;
3210 umin_val = src_reg.umin_value;
3211 umax_val = src_reg.umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01003212 src_known = tnum_is_const(src_reg.var_off);
3213 dst_known = tnum_is_const(dst_reg->var_off);
3214
Daniel Borkmann6f161012018-01-18 01:15:21 +01003215 if ((src_known && (smin_val != smax_val || umin_val != umax_val)) ||
3216 smin_val > smax_val || umin_val > umax_val) {
3217 /* Taint dst register if offset had invalid bounds derived from
3218 * e.g. dead branches.
3219 */
3220 __mark_reg_unknown(dst_reg);
3221 return 0;
3222 }
3223
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08003224 if (!src_known &&
3225 opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) {
3226 __mark_reg_unknown(dst_reg);
3227 return 0;
3228 }
3229
Edward Creef1174f72017-08-07 15:26:19 +01003230 switch (opcode) {
3231 case BPF_ADD:
Edward Creeb03c9f92017-08-07 15:26:36 +01003232 if (signed_add_overflows(dst_reg->smin_value, smin_val) ||
3233 signed_add_overflows(dst_reg->smax_value, smax_val)) {
3234 dst_reg->smin_value = S64_MIN;
3235 dst_reg->smax_value = S64_MAX;
3236 } else {
3237 dst_reg->smin_value += smin_val;
3238 dst_reg->smax_value += smax_val;
3239 }
3240 if (dst_reg->umin_value + umin_val < umin_val ||
3241 dst_reg->umax_value + umax_val < umax_val) {
3242 dst_reg->umin_value = 0;
3243 dst_reg->umax_value = U64_MAX;
3244 } else {
3245 dst_reg->umin_value += umin_val;
3246 dst_reg->umax_value += umax_val;
3247 }
Edward Creef1174f72017-08-07 15:26:19 +01003248 dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off);
3249 break;
3250 case BPF_SUB:
Edward Creeb03c9f92017-08-07 15:26:36 +01003251 if (signed_sub_overflows(dst_reg->smin_value, smax_val) ||
3252 signed_sub_overflows(dst_reg->smax_value, smin_val)) {
3253 /* Overflow possible, we know nothing */
3254 dst_reg->smin_value = S64_MIN;
3255 dst_reg->smax_value = S64_MAX;
3256 } else {
3257 dst_reg->smin_value -= smax_val;
3258 dst_reg->smax_value -= smin_val;
3259 }
3260 if (dst_reg->umin_value < umax_val) {
3261 /* Overflow possible, we know nothing */
3262 dst_reg->umin_value = 0;
3263 dst_reg->umax_value = U64_MAX;
3264 } else {
3265 /* Cannot overflow (as long as bounds are consistent) */
3266 dst_reg->umin_value -= umax_val;
3267 dst_reg->umax_value -= umin_val;
3268 }
Edward Creef1174f72017-08-07 15:26:19 +01003269 dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off);
Josef Bacik48461132016-09-28 10:54:32 -04003270 break;
3271 case BPF_MUL:
Edward Creeb03c9f92017-08-07 15:26:36 +01003272 dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off);
3273 if (smin_val < 0 || dst_reg->smin_value < 0) {
Edward Creef1174f72017-08-07 15:26:19 +01003274 /* Ain't nobody got time to multiply that sign */
Edward Creeb03c9f92017-08-07 15:26:36 +01003275 __mark_reg_unbounded(dst_reg);
3276 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003277 break;
3278 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003279 /* Both values are positive, so we can work with unsigned and
3280 * copy the result to signed (unless it exceeds S64_MAX).
Edward Creef1174f72017-08-07 15:26:19 +01003281 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003282 if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) {
3283 /* Potential overflow, we know nothing */
3284 __mark_reg_unbounded(dst_reg);
3285 /* (except what we can learn from the var_off) */
3286 __update_reg_bounds(dst_reg);
3287 break;
3288 }
3289 dst_reg->umin_value *= umin_val;
3290 dst_reg->umax_value *= umax_val;
3291 if (dst_reg->umax_value > S64_MAX) {
3292 /* Overflow possible, we know nothing */
3293 dst_reg->smin_value = S64_MIN;
3294 dst_reg->smax_value = S64_MAX;
3295 } else {
3296 dst_reg->smin_value = dst_reg->umin_value;
3297 dst_reg->smax_value = dst_reg->umax_value;
3298 }
Josef Bacik48461132016-09-28 10:54:32 -04003299 break;
3300 case BPF_AND:
Edward Creef1174f72017-08-07 15:26:19 +01003301 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01003302 __mark_reg_known(dst_reg, dst_reg->var_off.value &
3303 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01003304 break;
3305 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003306 /* We get our minimum from the var_off, since that's inherently
3307 * bitwise. Our maximum is the minimum of the operands' maxima.
Josef Bacikf23cc642016-11-14 15:45:36 -05003308 */
Edward Creef1174f72017-08-07 15:26:19 +01003309 dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01003310 dst_reg->umin_value = dst_reg->var_off.value;
3311 dst_reg->umax_value = min(dst_reg->umax_value, umax_val);
3312 if (dst_reg->smin_value < 0 || smin_val < 0) {
3313 /* Lose signed bounds when ANDing negative numbers,
3314 * ain't nobody got time for that.
3315 */
3316 dst_reg->smin_value = S64_MIN;
3317 dst_reg->smax_value = S64_MAX;
3318 } else {
3319 /* ANDing two positives gives a positive, so safe to
3320 * cast result into s64.
3321 */
3322 dst_reg->smin_value = dst_reg->umin_value;
3323 dst_reg->smax_value = dst_reg->umax_value;
3324 }
3325 /* We may learn something more from the var_off */
3326 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003327 break;
3328 case BPF_OR:
3329 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01003330 __mark_reg_known(dst_reg, dst_reg->var_off.value |
3331 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01003332 break;
3333 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003334 /* We get our maximum from the var_off, and our minimum is the
3335 * maximum of the operands' minima
Edward Creef1174f72017-08-07 15:26:19 +01003336 */
3337 dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01003338 dst_reg->umin_value = max(dst_reg->umin_value, umin_val);
3339 dst_reg->umax_value = dst_reg->var_off.value |
3340 dst_reg->var_off.mask;
3341 if (dst_reg->smin_value < 0 || smin_val < 0) {
3342 /* Lose signed bounds when ORing negative numbers,
3343 * ain't nobody got time for that.
3344 */
3345 dst_reg->smin_value = S64_MIN;
3346 dst_reg->smax_value = S64_MAX;
Edward Creef1174f72017-08-07 15:26:19 +01003347 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01003348 /* ORing two positives gives a positive, so safe to
3349 * cast result into s64.
3350 */
3351 dst_reg->smin_value = dst_reg->umin_value;
3352 dst_reg->smax_value = dst_reg->umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01003353 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003354 /* We may learn something more from the var_off */
3355 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04003356 break;
3357 case BPF_LSH:
Jann Horn468f6ea2017-12-18 20:11:56 -08003358 if (umax_val >= insn_bitness) {
3359 /* Shifts greater than 31 or 63 are undefined.
3360 * This includes shifts by a negative number.
Edward Creeb03c9f92017-08-07 15:26:36 +01003361 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003362 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003363 break;
3364 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003365 /* We lose all sign bit information (except what we can pick
3366 * up from var_off)
Josef Bacik48461132016-09-28 10:54:32 -04003367 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003368 dst_reg->smin_value = S64_MIN;
3369 dst_reg->smax_value = S64_MAX;
3370 /* If we might shift our top bit out, then we know nothing */
3371 if (dst_reg->umax_value > 1ULL << (63 - umax_val)) {
3372 dst_reg->umin_value = 0;
3373 dst_reg->umax_value = U64_MAX;
David S. Millerd1174412017-05-10 11:22:52 -07003374 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01003375 dst_reg->umin_value <<= umin_val;
3376 dst_reg->umax_value <<= umax_val;
David S. Millerd1174412017-05-10 11:22:52 -07003377 }
Yonghong Songafbe1a52018-04-28 22:28:10 -07003378 dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val);
Edward Creeb03c9f92017-08-07 15:26:36 +01003379 /* We may learn something more from the var_off */
3380 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04003381 break;
3382 case BPF_RSH:
Jann Horn468f6ea2017-12-18 20:11:56 -08003383 if (umax_val >= insn_bitness) {
3384 /* Shifts greater than 31 or 63 are undefined.
3385 * This includes shifts by a negative number.
Edward Creeb03c9f92017-08-07 15:26:36 +01003386 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003387 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003388 break;
3389 }
Edward Cree4374f252017-12-18 20:11:53 -08003390 /* BPF_RSH is an unsigned shift. If the value in dst_reg might
3391 * be negative, then either:
3392 * 1) src_reg might be zero, so the sign bit of the result is
3393 * unknown, so we lose our signed bounds
3394 * 2) it's known negative, thus the unsigned bounds capture the
3395 * signed bounds
3396 * 3) the signed bounds cross zero, so they tell us nothing
3397 * about the result
3398 * If the value in dst_reg is known nonnegative, then again the
3399 * unsigned bounts capture the signed bounds.
3400 * Thus, in all cases it suffices to blow away our signed bounds
3401 * and rely on inferring new ones from the unsigned bounds and
3402 * var_off of the result.
3403 */
3404 dst_reg->smin_value = S64_MIN;
3405 dst_reg->smax_value = S64_MAX;
Yonghong Songafbe1a52018-04-28 22:28:10 -07003406 dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val);
Edward Creeb03c9f92017-08-07 15:26:36 +01003407 dst_reg->umin_value >>= umax_val;
3408 dst_reg->umax_value >>= umin_val;
3409 /* We may learn something more from the var_off */
3410 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04003411 break;
Yonghong Song9cbe1f5a2018-04-28 22:28:11 -07003412 case BPF_ARSH:
3413 if (umax_val >= insn_bitness) {
3414 /* Shifts greater than 31 or 63 are undefined.
3415 * This includes shifts by a negative number.
3416 */
3417 mark_reg_unknown(env, regs, insn->dst_reg);
3418 break;
3419 }
3420
3421 /* Upon reaching here, src_known is true and
3422 * umax_val is equal to umin_val.
3423 */
3424 dst_reg->smin_value >>= umin_val;
3425 dst_reg->smax_value >>= umin_val;
3426 dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val);
3427
3428 /* blow away the dst_reg umin_value/umax_value and rely on
3429 * dst_reg var_off to refine the result.
3430 */
3431 dst_reg->umin_value = 0;
3432 dst_reg->umax_value = U64_MAX;
3433 __update_reg_bounds(dst_reg);
3434 break;
Josef Bacik48461132016-09-28 10:54:32 -04003435 default:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003436 mark_reg_unknown(env, regs, insn->dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04003437 break;
3438 }
3439
Jann Horn468f6ea2017-12-18 20:11:56 -08003440 if (BPF_CLASS(insn->code) != BPF_ALU64) {
3441 /* 32-bit ALU ops are (32,32)->32 */
3442 coerce_reg_to_size(dst_reg, 4);
Jann Horn468f6ea2017-12-18 20:11:56 -08003443 }
3444
Edward Creeb03c9f92017-08-07 15:26:36 +01003445 __reg_deduce_bounds(dst_reg);
3446 __reg_bound_offset(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003447 return 0;
3448}
3449
3450/* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max
3451 * and var_off.
3452 */
3453static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
3454 struct bpf_insn *insn)
3455{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003456 struct bpf_verifier_state *vstate = env->cur_state;
3457 struct bpf_func_state *state = vstate->frame[vstate->curframe];
3458 struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg;
Edward Creef1174f72017-08-07 15:26:19 +01003459 struct bpf_reg_state *ptr_reg = NULL, off_reg = {0};
3460 u8 opcode = BPF_OP(insn->code);
Edward Creef1174f72017-08-07 15:26:19 +01003461
3462 dst_reg = &regs[insn->dst_reg];
Edward Creef1174f72017-08-07 15:26:19 +01003463 src_reg = NULL;
3464 if (dst_reg->type != SCALAR_VALUE)
3465 ptr_reg = dst_reg;
3466 if (BPF_SRC(insn->code) == BPF_X) {
3467 src_reg = &regs[insn->src_reg];
Edward Creef1174f72017-08-07 15:26:19 +01003468 if (src_reg->type != SCALAR_VALUE) {
3469 if (dst_reg->type != SCALAR_VALUE) {
3470 /* Combining two pointers by any ALU op yields
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003471 * an arbitrary scalar. Disallow all math except
3472 * pointer subtraction
Edward Creef1174f72017-08-07 15:26:19 +01003473 */
Alexei Starovoitovdd066822018-09-12 14:06:10 -07003474 if (opcode == BPF_SUB && env->allow_ptr_leaks) {
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003475 mark_reg_unknown(env, regs, insn->dst_reg);
3476 return 0;
Edward Creef1174f72017-08-07 15:26:19 +01003477 }
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003478 verbose(env, "R%d pointer %s pointer prohibited\n",
3479 insn->dst_reg,
3480 bpf_alu_string[opcode >> 4]);
3481 return -EACCES;
Edward Creef1174f72017-08-07 15:26:19 +01003482 } else {
3483 /* scalar += pointer
3484 * This is legal, but we have to reverse our
3485 * src/dest handling in computing the range
3486 */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003487 return adjust_ptr_min_max_vals(env, insn,
3488 src_reg, dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003489 }
3490 } else if (ptr_reg) {
3491 /* pointer += scalar */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003492 return adjust_ptr_min_max_vals(env, insn,
3493 dst_reg, src_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003494 }
3495 } else {
3496 /* Pretend the src is a reg with a known value, since we only
3497 * need to be able to read from this state.
3498 */
3499 off_reg.type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01003500 __mark_reg_known(&off_reg, insn->imm);
Edward Creef1174f72017-08-07 15:26:19 +01003501 src_reg = &off_reg;
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003502 if (ptr_reg) /* pointer += K */
3503 return adjust_ptr_min_max_vals(env, insn,
3504 ptr_reg, src_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003505 }
3506
3507 /* Got here implies adding two SCALAR_VALUEs */
3508 if (WARN_ON_ONCE(ptr_reg)) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003509 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003510 verbose(env, "verifier internal error: unexpected ptr_reg\n");
Edward Creef1174f72017-08-07 15:26:19 +01003511 return -EINVAL;
3512 }
3513 if (WARN_ON(!src_reg)) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003514 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003515 verbose(env, "verifier internal error: no src_reg\n");
Edward Creef1174f72017-08-07 15:26:19 +01003516 return -EINVAL;
3517 }
3518 return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg);
Josef Bacik48461132016-09-28 10:54:32 -04003519}
3520
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003521/* check validity of 32-bit and 64-bit arithmetic operations */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003522static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003523{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003524 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003525 u8 opcode = BPF_OP(insn->code);
3526 int err;
3527
3528 if (opcode == BPF_END || opcode == BPF_NEG) {
3529 if (opcode == BPF_NEG) {
3530 if (BPF_SRC(insn->code) != 0 ||
3531 insn->src_reg != BPF_REG_0 ||
3532 insn->off != 0 || insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003533 verbose(env, "BPF_NEG uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003534 return -EINVAL;
3535 }
3536 } else {
3537 if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
Edward Creee67b8a62017-09-15 14:37:38 +01003538 (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) ||
3539 BPF_CLASS(insn->code) == BPF_ALU64) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003540 verbose(env, "BPF_END uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003541 return -EINVAL;
3542 }
3543 }
3544
3545 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01003546 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003547 if (err)
3548 return err;
3549
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003550 if (is_pointer_value(env, insn->dst_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003551 verbose(env, "R%d pointer arithmetic prohibited\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003552 insn->dst_reg);
3553 return -EACCES;
3554 }
3555
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003556 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01003557 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003558 if (err)
3559 return err;
3560
3561 } else if (opcode == BPF_MOV) {
3562
3563 if (BPF_SRC(insn->code) == BPF_X) {
3564 if (insn->imm != 0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003565 verbose(env, "BPF_MOV uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003566 return -EINVAL;
3567 }
3568
3569 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01003570 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003571 if (err)
3572 return err;
3573 } else {
3574 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003575 verbose(env, "BPF_MOV uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003576 return -EINVAL;
3577 }
3578 }
3579
Arthur Fabrefbeb1602018-07-31 18:17:22 +01003580 /* check dest operand, mark as required later */
3581 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003582 if (err)
3583 return err;
3584
3585 if (BPF_SRC(insn->code) == BPF_X) {
Jiong Wange434b8c2018-12-07 12:16:18 -05003586 struct bpf_reg_state *src_reg = regs + insn->src_reg;
3587 struct bpf_reg_state *dst_reg = regs + insn->dst_reg;
3588
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003589 if (BPF_CLASS(insn->code) == BPF_ALU64) {
3590 /* case: R1 = R2
3591 * copy register state to dest reg
3592 */
Jiong Wange434b8c2018-12-07 12:16:18 -05003593 *dst_reg = *src_reg;
3594 dst_reg->live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003595 } else {
Edward Creef1174f72017-08-07 15:26:19 +01003596 /* R1 = (u32) R2 */
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003597 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003598 verbose(env,
3599 "R%d partial copy of pointer\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003600 insn->src_reg);
3601 return -EACCES;
Jiong Wange434b8c2018-12-07 12:16:18 -05003602 } else if (src_reg->type == SCALAR_VALUE) {
3603 *dst_reg = *src_reg;
3604 dst_reg->live |= REG_LIVE_WRITTEN;
3605 } else {
3606 mark_reg_unknown(env, regs,
3607 insn->dst_reg);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003608 }
Jiong Wange434b8c2018-12-07 12:16:18 -05003609 coerce_reg_to_size(dst_reg, 4);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003610 }
3611 } else {
3612 /* case: R = imm
3613 * remember the value we stored into this reg
3614 */
Arthur Fabrefbeb1602018-07-31 18:17:22 +01003615 /* clear any state __mark_reg_known doesn't set */
3616 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003617 regs[insn->dst_reg].type = SCALAR_VALUE;
Jann Horn95a762e2017-12-18 20:11:54 -08003618 if (BPF_CLASS(insn->code) == BPF_ALU64) {
3619 __mark_reg_known(regs + insn->dst_reg,
3620 insn->imm);
3621 } else {
3622 __mark_reg_known(regs + insn->dst_reg,
3623 (u32)insn->imm);
3624 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003625 }
3626
3627 } else if (opcode > BPF_END) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003628 verbose(env, "invalid BPF_ALU opcode %x\n", opcode);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003629 return -EINVAL;
3630
3631 } else { /* all other ALU ops: and, sub, xor, add, ... */
3632
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003633 if (BPF_SRC(insn->code) == BPF_X) {
3634 if (insn->imm != 0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003635 verbose(env, "BPF_ALU uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003636 return -EINVAL;
3637 }
3638 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01003639 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003640 if (err)
3641 return err;
3642 } else {
3643 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003644 verbose(env, "BPF_ALU uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003645 return -EINVAL;
3646 }
3647 }
3648
3649 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01003650 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003651 if (err)
3652 return err;
3653
3654 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
3655 BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003656 verbose(env, "div by zero\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003657 return -EINVAL;
3658 }
3659
Rabin Vincent229394e82016-01-12 20:17:08 +01003660 if ((opcode == BPF_LSH || opcode == BPF_RSH ||
3661 opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
3662 int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
3663
3664 if (insn->imm < 0 || insn->imm >= size) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003665 verbose(env, "invalid shift %d\n", insn->imm);
Rabin Vincent229394e82016-01-12 20:17:08 +01003666 return -EINVAL;
3667 }
3668 }
3669
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003670 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01003671 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003672 if (err)
3673 return err;
3674
Edward Creef1174f72017-08-07 15:26:19 +01003675 return adjust_reg_min_max_vals(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003676 }
3677
3678 return 0;
3679}
3680
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003681static void find_good_pkt_pointers(struct bpf_verifier_state *vstate,
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003682 struct bpf_reg_state *dst_reg,
David S. Millerf8ddadc2017-10-22 13:36:53 +01003683 enum bpf_reg_type type,
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003684 bool range_right_open)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003685{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003686 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003687 struct bpf_reg_state *regs = state->regs, *reg;
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003688 u16 new_range;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003689 int i, j;
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003690
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003691 if (dst_reg->off < 0 ||
3692 (dst_reg->off == 0 && range_right_open))
Edward Creef1174f72017-08-07 15:26:19 +01003693 /* This doesn't give us any range */
3694 return;
3695
Edward Creeb03c9f92017-08-07 15:26:36 +01003696 if (dst_reg->umax_value > MAX_PACKET_OFF ||
3697 dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF)
Edward Creef1174f72017-08-07 15:26:19 +01003698 /* Risk of overflow. For instance, ptr + (1<<63) may be less
3699 * than pkt_end, but that's because it's also less than pkt.
3700 */
3701 return;
3702
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003703 new_range = dst_reg->off;
3704 if (range_right_open)
3705 new_range--;
3706
3707 /* Examples for register markings:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003708 *
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003709 * pkt_data in dst register:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003710 *
3711 * r2 = r3;
3712 * r2 += 8;
3713 * if (r2 > pkt_end) goto <handle exception>
3714 * <access okay>
3715 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003716 * r2 = r3;
3717 * r2 += 8;
3718 * if (r2 < pkt_end) goto <access okay>
3719 * <handle exception>
3720 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003721 * Where:
3722 * r2 == dst_reg, pkt_end == src_reg
3723 * r2=pkt(id=n,off=8,r=0)
3724 * r3=pkt(id=n,off=0,r=0)
3725 *
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003726 * pkt_data in src register:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003727 *
3728 * r2 = r3;
3729 * r2 += 8;
3730 * if (pkt_end >= r2) goto <access okay>
3731 * <handle exception>
3732 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003733 * r2 = r3;
3734 * r2 += 8;
3735 * if (pkt_end <= r2) goto <handle exception>
3736 * <access okay>
3737 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003738 * Where:
3739 * pkt_end == dst_reg, r2 == src_reg
3740 * r2=pkt(id=n,off=8,r=0)
3741 * r3=pkt(id=n,off=0,r=0)
3742 *
3743 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003744 * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8)
3745 * and [r3, r3 + 8-1) respectively is safe to access depending on
3746 * the check.
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003747 */
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003748
Edward Creef1174f72017-08-07 15:26:19 +01003749 /* If our ids match, then we must have the same max_value. And we
3750 * don't care about the other reg's fixed offset, since if it's too big
3751 * the range won't allow anything.
3752 * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16.
3753 */
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003754 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003755 if (regs[i].type == type && regs[i].id == dst_reg->id)
Alexei Starovoitovb1977682017-03-24 15:57:33 -07003756 /* keep the maximum range already checked */
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003757 regs[i].range = max(regs[i].range, new_range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003758
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003759 for (j = 0; j <= vstate->curframe; j++) {
3760 state = vstate->frame[j];
Joe Stringerf3709f62018-10-02 13:35:29 -07003761 bpf_for_each_spilled_reg(i, state, reg) {
3762 if (!reg)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003763 continue;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003764 if (reg->type == type && reg->id == dst_reg->id)
3765 reg->range = max(reg->range, new_range);
3766 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003767 }
3768}
3769
Josef Bacik48461132016-09-28 10:54:32 -04003770/* Adjusts the register min/max values in the case that the dst_reg is the
3771 * variable register that we are working on, and src_reg is a constant or we're
3772 * simply doing a BPF_K check.
Edward Creef1174f72017-08-07 15:26:19 +01003773 * In JEQ/JNE cases we also adjust the var_off values.
Josef Bacik48461132016-09-28 10:54:32 -04003774 */
3775static void reg_set_min_max(struct bpf_reg_state *true_reg,
3776 struct bpf_reg_state *false_reg, u64 val,
3777 u8 opcode)
3778{
Edward Creef1174f72017-08-07 15:26:19 +01003779 /* If the dst_reg is a pointer, we can't learn anything about its
3780 * variable offset from the compare (unless src_reg were a pointer into
3781 * the same object, but we don't bother with that.
3782 * Since false_reg and true_reg have the same type by construction, we
3783 * only need to check one of them for pointerness.
3784 */
3785 if (__is_pointer_value(false, false_reg))
3786 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02003787
Josef Bacik48461132016-09-28 10:54:32 -04003788 switch (opcode) {
3789 case BPF_JEQ:
3790 /* If this is false then we know nothing Jon Snow, but if it is
3791 * true then we know for sure.
3792 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003793 __mark_reg_known(true_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04003794 break;
3795 case BPF_JNE:
3796 /* If this is true we know nothing Jon Snow, but if it is false
3797 * we know the value for sure;
3798 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003799 __mark_reg_known(false_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04003800 break;
3801 case BPF_JGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01003802 false_reg->umax_value = min(false_reg->umax_value, val);
3803 true_reg->umin_value = max(true_reg->umin_value, val + 1);
3804 break;
Josef Bacik48461132016-09-28 10:54:32 -04003805 case BPF_JSGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01003806 false_reg->smax_value = min_t(s64, false_reg->smax_value, val);
3807 true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1);
Josef Bacik48461132016-09-28 10:54:32 -04003808 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003809 case BPF_JLT:
3810 false_reg->umin_value = max(false_reg->umin_value, val);
3811 true_reg->umax_value = min(true_reg->umax_value, val - 1);
3812 break;
3813 case BPF_JSLT:
3814 false_reg->smin_value = max_t(s64, false_reg->smin_value, val);
3815 true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1);
3816 break;
Josef Bacik48461132016-09-28 10:54:32 -04003817 case BPF_JGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01003818 false_reg->umax_value = min(false_reg->umax_value, val - 1);
3819 true_reg->umin_value = max(true_reg->umin_value, val);
3820 break;
Josef Bacik48461132016-09-28 10:54:32 -04003821 case BPF_JSGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01003822 false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1);
3823 true_reg->smin_value = max_t(s64, true_reg->smin_value, val);
Josef Bacik48461132016-09-28 10:54:32 -04003824 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003825 case BPF_JLE:
3826 false_reg->umin_value = max(false_reg->umin_value, val + 1);
3827 true_reg->umax_value = min(true_reg->umax_value, val);
3828 break;
3829 case BPF_JSLE:
3830 false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1);
3831 true_reg->smax_value = min_t(s64, true_reg->smax_value, val);
3832 break;
Josef Bacik48461132016-09-28 10:54:32 -04003833 default:
3834 break;
3835 }
3836
Edward Creeb03c9f92017-08-07 15:26:36 +01003837 __reg_deduce_bounds(false_reg);
3838 __reg_deduce_bounds(true_reg);
3839 /* We might have learned some bits from the bounds. */
3840 __reg_bound_offset(false_reg);
3841 __reg_bound_offset(true_reg);
3842 /* Intersecting with the old var_off might have improved our bounds
3843 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
3844 * then new var_off is (0; 0x7f...fc) which improves our umax.
3845 */
3846 __update_reg_bounds(false_reg);
3847 __update_reg_bounds(true_reg);
Josef Bacik48461132016-09-28 10:54:32 -04003848}
3849
Edward Creef1174f72017-08-07 15:26:19 +01003850/* Same as above, but for the case that dst_reg holds a constant and src_reg is
3851 * the variable reg.
Josef Bacik48461132016-09-28 10:54:32 -04003852 */
3853static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
3854 struct bpf_reg_state *false_reg, u64 val,
3855 u8 opcode)
3856{
Edward Creef1174f72017-08-07 15:26:19 +01003857 if (__is_pointer_value(false, false_reg))
3858 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02003859
Josef Bacik48461132016-09-28 10:54:32 -04003860 switch (opcode) {
3861 case BPF_JEQ:
3862 /* If this is false then we know nothing Jon Snow, but if it is
3863 * true then we know for sure.
3864 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003865 __mark_reg_known(true_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04003866 break;
3867 case BPF_JNE:
3868 /* If this is true we know nothing Jon Snow, but if it is false
3869 * we know the value for sure;
3870 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003871 __mark_reg_known(false_reg, val);
Josef Bacik48461132016-09-28 10:54:32 -04003872 break;
3873 case BPF_JGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01003874 true_reg->umax_value = min(true_reg->umax_value, val - 1);
3875 false_reg->umin_value = max(false_reg->umin_value, val);
3876 break;
Josef Bacik48461132016-09-28 10:54:32 -04003877 case BPF_JSGT:
Edward Creeb03c9f92017-08-07 15:26:36 +01003878 true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1);
3879 false_reg->smin_value = max_t(s64, false_reg->smin_value, val);
Josef Bacik48461132016-09-28 10:54:32 -04003880 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003881 case BPF_JLT:
3882 true_reg->umin_value = max(true_reg->umin_value, val + 1);
3883 false_reg->umax_value = min(false_reg->umax_value, val);
3884 break;
3885 case BPF_JSLT:
3886 true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1);
3887 false_reg->smax_value = min_t(s64, false_reg->smax_value, val);
3888 break;
Josef Bacik48461132016-09-28 10:54:32 -04003889 case BPF_JGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01003890 true_reg->umax_value = min(true_reg->umax_value, val);
3891 false_reg->umin_value = max(false_reg->umin_value, val + 1);
3892 break;
Josef Bacik48461132016-09-28 10:54:32 -04003893 case BPF_JSGE:
Edward Creeb03c9f92017-08-07 15:26:36 +01003894 true_reg->smax_value = min_t(s64, true_reg->smax_value, val);
3895 false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1);
Josef Bacik48461132016-09-28 10:54:32 -04003896 break;
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003897 case BPF_JLE:
3898 true_reg->umin_value = max(true_reg->umin_value, val);
3899 false_reg->umax_value = min(false_reg->umax_value, val - 1);
3900 break;
3901 case BPF_JSLE:
3902 true_reg->smin_value = max_t(s64, true_reg->smin_value, val);
3903 false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1);
3904 break;
Josef Bacik48461132016-09-28 10:54:32 -04003905 default:
3906 break;
3907 }
3908
Edward Creeb03c9f92017-08-07 15:26:36 +01003909 __reg_deduce_bounds(false_reg);
3910 __reg_deduce_bounds(true_reg);
3911 /* We might have learned some bits from the bounds. */
3912 __reg_bound_offset(false_reg);
3913 __reg_bound_offset(true_reg);
3914 /* Intersecting with the old var_off might have improved our bounds
3915 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
3916 * then new var_off is (0; 0x7f...fc) which improves our umax.
3917 */
3918 __update_reg_bounds(false_reg);
3919 __update_reg_bounds(true_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003920}
3921
3922/* Regs are known to be equal, so intersect their min/max/var_off */
3923static void __reg_combine_min_max(struct bpf_reg_state *src_reg,
3924 struct bpf_reg_state *dst_reg)
3925{
Edward Creeb03c9f92017-08-07 15:26:36 +01003926 src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value,
3927 dst_reg->umin_value);
3928 src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value,
3929 dst_reg->umax_value);
3930 src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value,
3931 dst_reg->smin_value);
3932 src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value,
3933 dst_reg->smax_value);
Edward Creef1174f72017-08-07 15:26:19 +01003934 src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off,
3935 dst_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01003936 /* We might have learned new bounds from the var_off. */
3937 __update_reg_bounds(src_reg);
3938 __update_reg_bounds(dst_reg);
3939 /* We might have learned something about the sign bit. */
3940 __reg_deduce_bounds(src_reg);
3941 __reg_deduce_bounds(dst_reg);
3942 /* We might have learned some bits from the bounds. */
3943 __reg_bound_offset(src_reg);
3944 __reg_bound_offset(dst_reg);
3945 /* Intersecting with the old var_off might have improved our bounds
3946 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
3947 * then new var_off is (0; 0x7f...fc) which improves our umax.
3948 */
3949 __update_reg_bounds(src_reg);
3950 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003951}
3952
3953static void reg_combine_min_max(struct bpf_reg_state *true_src,
3954 struct bpf_reg_state *true_dst,
3955 struct bpf_reg_state *false_src,
3956 struct bpf_reg_state *false_dst,
3957 u8 opcode)
3958{
3959 switch (opcode) {
3960 case BPF_JEQ:
3961 __reg_combine_min_max(true_src, true_dst);
3962 break;
3963 case BPF_JNE:
3964 __reg_combine_min_max(false_src, false_dst);
Edward Creeb03c9f92017-08-07 15:26:36 +01003965 break;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02003966 }
Josef Bacik48461132016-09-28 10:54:32 -04003967}
3968
Joe Stringerfd978bf72018-10-02 13:35:35 -07003969static void mark_ptr_or_null_reg(struct bpf_func_state *state,
3970 struct bpf_reg_state *reg, u32 id,
Joe Stringer840b9612018-10-02 13:35:32 -07003971 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02003972{
Joe Stringer840b9612018-10-02 13:35:32 -07003973 if (reg_type_may_be_null(reg->type) && reg->id == id) {
Edward Creef1174f72017-08-07 15:26:19 +01003974 /* Old offset (both fixed and variable parts) should
3975 * have been known-zero, because we don't allow pointer
3976 * arithmetic on pointers that might be NULL.
3977 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003978 if (WARN_ON_ONCE(reg->smin_value || reg->smax_value ||
3979 !tnum_equals_const(reg->var_off, 0) ||
Edward Creef1174f72017-08-07 15:26:19 +01003980 reg->off)) {
Edward Creeb03c9f92017-08-07 15:26:36 +01003981 __mark_reg_known_zero(reg);
3982 reg->off = 0;
Edward Creef1174f72017-08-07 15:26:19 +01003983 }
3984 if (is_null) {
3985 reg->type = SCALAR_VALUE;
Joe Stringer840b9612018-10-02 13:35:32 -07003986 } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
3987 if (reg->map_ptr->inner_map_meta) {
3988 reg->type = CONST_PTR_TO_MAP;
3989 reg->map_ptr = reg->map_ptr->inner_map_meta;
3990 } else {
3991 reg->type = PTR_TO_MAP_VALUE;
3992 }
Joe Stringerc64b7982018-10-02 13:35:33 -07003993 } else if (reg->type == PTR_TO_SOCKET_OR_NULL) {
3994 reg->type = PTR_TO_SOCKET;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07003995 }
Joe Stringerfd978bf72018-10-02 13:35:35 -07003996 if (is_null || !reg_is_refcounted(reg)) {
3997 /* We don't need id from this point onwards anymore,
3998 * thus we should better reset it, so that state
3999 * pruning has chances to take effect.
4000 */
4001 reg->id = 0;
4002 }
Thomas Graf57a09bf2016-10-18 19:51:19 +02004003 }
4004}
4005
4006/* The logic is similar to find_good_pkt_pointers(), both could eventually
4007 * be folded together at some point.
4008 */
Joe Stringer840b9612018-10-02 13:35:32 -07004009static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno,
4010 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02004011{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004012 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Joe Stringerf3709f62018-10-02 13:35:29 -07004013 struct bpf_reg_state *reg, *regs = state->regs;
Daniel Borkmanna08dd0d2016-12-15 01:30:06 +01004014 u32 id = regs[regno].id;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004015 int i, j;
Thomas Graf57a09bf2016-10-18 19:51:19 +02004016
Joe Stringerfd978bf72018-10-02 13:35:35 -07004017 if (reg_is_refcounted_or_null(&regs[regno]) && is_null)
4018 __release_reference_state(state, id);
4019
Thomas Graf57a09bf2016-10-18 19:51:19 +02004020 for (i = 0; i < MAX_BPF_REG; i++)
Joe Stringerfd978bf72018-10-02 13:35:35 -07004021 mark_ptr_or_null_reg(state, &regs[i], id, is_null);
Thomas Graf57a09bf2016-10-18 19:51:19 +02004022
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004023 for (j = 0; j <= vstate->curframe; j++) {
4024 state = vstate->frame[j];
Joe Stringerf3709f62018-10-02 13:35:29 -07004025 bpf_for_each_spilled_reg(i, state, reg) {
4026 if (!reg)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004027 continue;
Joe Stringerfd978bf72018-10-02 13:35:35 -07004028 mark_ptr_or_null_reg(state, reg, id, is_null);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004029 }
Thomas Graf57a09bf2016-10-18 19:51:19 +02004030 }
4031}
4032
Daniel Borkmann5beca082017-11-01 23:58:10 +01004033static bool try_match_pkt_pointers(const struct bpf_insn *insn,
4034 struct bpf_reg_state *dst_reg,
4035 struct bpf_reg_state *src_reg,
4036 struct bpf_verifier_state *this_branch,
4037 struct bpf_verifier_state *other_branch)
4038{
4039 if (BPF_SRC(insn->code) != BPF_X)
4040 return false;
4041
4042 switch (BPF_OP(insn->code)) {
4043 case BPF_JGT:
4044 if ((dst_reg->type == PTR_TO_PACKET &&
4045 src_reg->type == PTR_TO_PACKET_END) ||
4046 (dst_reg->type == PTR_TO_PACKET_META &&
4047 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
4048 /* pkt_data' > pkt_end, pkt_meta' > pkt_data */
4049 find_good_pkt_pointers(this_branch, dst_reg,
4050 dst_reg->type, false);
4051 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
4052 src_reg->type == PTR_TO_PACKET) ||
4053 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
4054 src_reg->type == PTR_TO_PACKET_META)) {
4055 /* pkt_end > pkt_data', pkt_data > pkt_meta' */
4056 find_good_pkt_pointers(other_branch, src_reg,
4057 src_reg->type, true);
4058 } else {
4059 return false;
4060 }
4061 break;
4062 case BPF_JLT:
4063 if ((dst_reg->type == PTR_TO_PACKET &&
4064 src_reg->type == PTR_TO_PACKET_END) ||
4065 (dst_reg->type == PTR_TO_PACKET_META &&
4066 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
4067 /* pkt_data' < pkt_end, pkt_meta' < pkt_data */
4068 find_good_pkt_pointers(other_branch, dst_reg,
4069 dst_reg->type, true);
4070 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
4071 src_reg->type == PTR_TO_PACKET) ||
4072 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
4073 src_reg->type == PTR_TO_PACKET_META)) {
4074 /* pkt_end < pkt_data', pkt_data > pkt_meta' */
4075 find_good_pkt_pointers(this_branch, src_reg,
4076 src_reg->type, false);
4077 } else {
4078 return false;
4079 }
4080 break;
4081 case BPF_JGE:
4082 if ((dst_reg->type == PTR_TO_PACKET &&
4083 src_reg->type == PTR_TO_PACKET_END) ||
4084 (dst_reg->type == PTR_TO_PACKET_META &&
4085 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
4086 /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */
4087 find_good_pkt_pointers(this_branch, dst_reg,
4088 dst_reg->type, true);
4089 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
4090 src_reg->type == PTR_TO_PACKET) ||
4091 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
4092 src_reg->type == PTR_TO_PACKET_META)) {
4093 /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */
4094 find_good_pkt_pointers(other_branch, src_reg,
4095 src_reg->type, false);
4096 } else {
4097 return false;
4098 }
4099 break;
4100 case BPF_JLE:
4101 if ((dst_reg->type == PTR_TO_PACKET &&
4102 src_reg->type == PTR_TO_PACKET_END) ||
4103 (dst_reg->type == PTR_TO_PACKET_META &&
4104 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
4105 /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */
4106 find_good_pkt_pointers(other_branch, dst_reg,
4107 dst_reg->type, false);
4108 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
4109 src_reg->type == PTR_TO_PACKET) ||
4110 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
4111 src_reg->type == PTR_TO_PACKET_META)) {
4112 /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */
4113 find_good_pkt_pointers(this_branch, src_reg,
4114 src_reg->type, true);
4115 } else {
4116 return false;
4117 }
4118 break;
4119 default:
4120 return false;
4121 }
4122
4123 return true;
4124}
4125
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004126static int check_cond_jmp_op(struct bpf_verifier_env *env,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004127 struct bpf_insn *insn, int *insn_idx)
4128{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004129 struct bpf_verifier_state *this_branch = env->cur_state;
4130 struct bpf_verifier_state *other_branch;
4131 struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs;
4132 struct bpf_reg_state *dst_reg, *other_branch_regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004133 u8 opcode = BPF_OP(insn->code);
4134 int err;
4135
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004136 if (opcode > BPF_JSLE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004137 verbose(env, "invalid BPF_JMP opcode %x\n", opcode);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004138 return -EINVAL;
4139 }
4140
4141 if (BPF_SRC(insn->code) == BPF_X) {
4142 if (insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004143 verbose(env, "BPF_JMP uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004144 return -EINVAL;
4145 }
4146
4147 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01004148 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004149 if (err)
4150 return err;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004151
4152 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004153 verbose(env, "R%d pointer comparison prohibited\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004154 insn->src_reg);
4155 return -EACCES;
4156 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004157 } else {
4158 if (insn->src_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004159 verbose(env, "BPF_JMP uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004160 return -EINVAL;
4161 }
4162 }
4163
4164 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01004165 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004166 if (err)
4167 return err;
4168
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07004169 dst_reg = &regs[insn->dst_reg];
4170
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004171 /* detect if R == 0 where R was initialized to zero earlier */
4172 if (BPF_SRC(insn->code) == BPF_K &&
4173 (opcode == BPF_JEQ || opcode == BPF_JNE) &&
Edward Creef1174f72017-08-07 15:26:19 +01004174 dst_reg->type == SCALAR_VALUE &&
Alexei Starovoitov3bf15922017-11-30 21:31:39 -08004175 tnum_is_const(dst_reg->var_off)) {
4176 if ((opcode == BPF_JEQ && dst_reg->var_off.value == insn->imm) ||
4177 (opcode == BPF_JNE && dst_reg->var_off.value != insn->imm)) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004178 /* if (imm == imm) goto pc+off;
4179 * only follow the goto, ignore fall-through
4180 */
4181 *insn_idx += insn->off;
4182 return 0;
4183 } else {
4184 /* if (imm != imm) goto pc+off;
4185 * only follow fall-through branch, since
4186 * that's where the program will go
4187 */
4188 return 0;
4189 }
4190 }
4191
4192 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx);
4193 if (!other_branch)
4194 return -EFAULT;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004195 other_branch_regs = other_branch->frame[other_branch->curframe]->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004196
Josef Bacik48461132016-09-28 10:54:32 -04004197 /* detect if we are comparing against a constant value so we can adjust
4198 * our min/max values for our dst register.
Edward Creef1174f72017-08-07 15:26:19 +01004199 * this is only legit if both are scalars (or pointers to the same
4200 * object, I suppose, but we don't support that right now), because
4201 * otherwise the different base pointers mean the offsets aren't
4202 * comparable.
Josef Bacik48461132016-09-28 10:54:32 -04004203 */
4204 if (BPF_SRC(insn->code) == BPF_X) {
Edward Creef1174f72017-08-07 15:26:19 +01004205 if (dst_reg->type == SCALAR_VALUE &&
4206 regs[insn->src_reg].type == SCALAR_VALUE) {
4207 if (tnum_is_const(regs[insn->src_reg].var_off))
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004208 reg_set_min_max(&other_branch_regs[insn->dst_reg],
Edward Creef1174f72017-08-07 15:26:19 +01004209 dst_reg, regs[insn->src_reg].var_off.value,
4210 opcode);
4211 else if (tnum_is_const(dst_reg->var_off))
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004212 reg_set_min_max_inv(&other_branch_regs[insn->src_reg],
Edward Creef1174f72017-08-07 15:26:19 +01004213 &regs[insn->src_reg],
4214 dst_reg->var_off.value, opcode);
4215 else if (opcode == BPF_JEQ || opcode == BPF_JNE)
4216 /* Comparing for equality, we can combine knowledge */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004217 reg_combine_min_max(&other_branch_regs[insn->src_reg],
4218 &other_branch_regs[insn->dst_reg],
Edward Creef1174f72017-08-07 15:26:19 +01004219 &regs[insn->src_reg],
4220 &regs[insn->dst_reg], opcode);
4221 }
4222 } else if (dst_reg->type == SCALAR_VALUE) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004223 reg_set_min_max(&other_branch_regs[insn->dst_reg],
Josef Bacik48461132016-09-28 10:54:32 -04004224 dst_reg, insn->imm, opcode);
4225 }
4226
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004227 /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004228 if (BPF_SRC(insn->code) == BPF_K &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07004229 insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
Joe Stringer840b9612018-10-02 13:35:32 -07004230 reg_type_may_be_null(dst_reg->type)) {
4231 /* Mark all identical registers in each branch as either
Thomas Graf57a09bf2016-10-18 19:51:19 +02004232 * safe or unknown depending R == 0 or R != 0 conditional.
4233 */
Joe Stringer840b9612018-10-02 13:35:32 -07004234 mark_ptr_or_null_regs(this_branch, insn->dst_reg,
4235 opcode == BPF_JNE);
4236 mark_ptr_or_null_regs(other_branch, insn->dst_reg,
4237 opcode == BPF_JEQ);
Daniel Borkmann5beca082017-11-01 23:58:10 +01004238 } else if (!try_match_pkt_pointers(insn, dst_reg, &regs[insn->src_reg],
4239 this_branch, other_branch) &&
4240 is_pointer_value(env, insn->dst_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004241 verbose(env, "R%d pointer comparison prohibited\n",
4242 insn->dst_reg);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004243 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004244 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004245 if (env->log.level)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004246 print_verifier_state(env, this_branch->frame[this_branch->curframe]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004247 return 0;
4248}
4249
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004250/* return the map pointer stored inside BPF_LD_IMM64 instruction */
4251static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
4252{
4253 u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
4254
4255 return (struct bpf_map *) (unsigned long) imm64;
4256}
4257
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004258/* verify BPF_LD_IMM64 instruction */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004259static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004260{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004261 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004262 int err;
4263
4264 if (BPF_SIZE(insn->code) != BPF_DW) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004265 verbose(env, "invalid BPF_LD_IMM insn\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004266 return -EINVAL;
4267 }
4268 if (insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004269 verbose(env, "BPF_LD_IMM64 uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004270 return -EINVAL;
4271 }
4272
Edward Creedc503a82017-08-15 20:34:35 +01004273 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004274 if (err)
4275 return err;
4276
Jakub Kicinski6b173872016-09-21 11:43:59 +01004277 if (insn->src_reg == 0) {
Jakub Kicinski6b173872016-09-21 11:43:59 +01004278 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
4279
Edward Creef1174f72017-08-07 15:26:19 +01004280 regs[insn->dst_reg].type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01004281 __mark_reg_known(&regs[insn->dst_reg], imm);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004282 return 0;
Jakub Kicinski6b173872016-09-21 11:43:59 +01004283 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004284
4285 /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
4286 BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
4287
4288 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
4289 regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
4290 return 0;
4291}
4292
Daniel Borkmann96be4322015-03-01 12:31:46 +01004293static bool may_access_skb(enum bpf_prog_type type)
4294{
4295 switch (type) {
4296 case BPF_PROG_TYPE_SOCKET_FILTER:
4297 case BPF_PROG_TYPE_SCHED_CLS:
Daniel Borkmann94caee8c2015-03-20 15:11:11 +01004298 case BPF_PROG_TYPE_SCHED_ACT:
Daniel Borkmann96be4322015-03-01 12:31:46 +01004299 return true;
4300 default:
4301 return false;
4302 }
4303}
4304
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004305/* verify safety of LD_ABS|LD_IND instructions:
4306 * - they can only appear in the programs where ctx == skb
4307 * - since they are wrappers of function calls, they scratch R1-R5 registers,
4308 * preserve R6-R9, and store return value into R0
4309 *
4310 * Implicit input:
4311 * ctx == skb == R6 == CTX
4312 *
4313 * Explicit input:
4314 * SRC == any register
4315 * IMM == 32-bit immediate
4316 *
4317 * Output:
4318 * R0 - 8/16/32-bit skb data converted to cpu endianness
4319 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004320static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004321{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004322 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004323 u8 mode = BPF_MODE(insn->code);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004324 int i, err;
4325
Daniel Borkmann24701ec2015-03-01 12:31:47 +01004326 if (!may_access_skb(env->prog->type)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004327 verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004328 return -EINVAL;
4329 }
4330
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +02004331 if (!env->ops->gen_ld_abs) {
4332 verbose(env, "bpf verifier is misconfigured\n");
4333 return -EINVAL;
4334 }
4335
Jiong Wangf910cef2018-05-02 16:17:17 -04004336 if (env->subprog_cnt > 1) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004337 /* when program has LD_ABS insn JITs and interpreter assume
4338 * that r1 == ctx == skb which is not the case for callees
4339 * that can have arbitrary arguments. It's problematic
4340 * for main prog as well since JITs would need to analyze
4341 * all functions in order to make proper register save/restore
4342 * decisions in the main prog. Hence disallow LD_ABS with calls
4343 */
4344 verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n");
4345 return -EINVAL;
4346 }
4347
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004348 if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
Alexei Starovoitovd82bccc2016-04-12 10:26:19 -07004349 BPF_SIZE(insn->code) == BPF_DW ||
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004350 (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004351 verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004352 return -EINVAL;
4353 }
4354
4355 /* check whether implicit source operand (register R6) is readable */
Edward Creedc503a82017-08-15 20:34:35 +01004356 err = check_reg_arg(env, BPF_REG_6, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004357 if (err)
4358 return err;
4359
Joe Stringerfd978bf72018-10-02 13:35:35 -07004360 /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as
4361 * gen_ld_abs() may terminate the program at runtime, leading to
4362 * reference leak.
4363 */
4364 err = check_reference_leak(env);
4365 if (err) {
4366 verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n");
4367 return err;
4368 }
4369
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004370 if (regs[BPF_REG_6].type != PTR_TO_CTX) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004371 verbose(env,
4372 "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004373 return -EINVAL;
4374 }
4375
4376 if (mode == BPF_IND) {
4377 /* check explicit source operand */
Edward Creedc503a82017-08-15 20:34:35 +01004378 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004379 if (err)
4380 return err;
4381 }
4382
4383 /* reset caller saved regs to unreadable */
Edward Creedc503a82017-08-15 20:34:35 +01004384 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004385 mark_reg_not_init(env, regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01004386 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
4387 }
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004388
4389 /* mark destination R0 register as readable, since it contains
Edward Creedc503a82017-08-15 20:34:35 +01004390 * the value fetched from the packet.
4391 * Already marked as written above.
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004392 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004393 mark_reg_unknown(env, regs, BPF_REG_0);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004394 return 0;
4395}
4396
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07004397static int check_return_code(struct bpf_verifier_env *env)
4398{
4399 struct bpf_reg_state *reg;
4400 struct tnum range = tnum_range(0, 1);
4401
4402 switch (env->prog->type) {
4403 case BPF_PROG_TYPE_CGROUP_SKB:
4404 case BPF_PROG_TYPE_CGROUP_SOCK:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07004405 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07004406 case BPF_PROG_TYPE_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05004407 case BPF_PROG_TYPE_CGROUP_DEVICE:
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07004408 break;
4409 default:
4410 return 0;
4411 }
4412
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004413 reg = cur_regs(env) + BPF_REG_0;
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07004414 if (reg->type != SCALAR_VALUE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004415 verbose(env, "At program exit the register R0 is not a known value (%s)\n",
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07004416 reg_type_str[reg->type]);
4417 return -EINVAL;
4418 }
4419
4420 if (!tnum_in(range, reg->var_off)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004421 verbose(env, "At program exit the register R0 ");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07004422 if (!tnum_is_unknown(reg->var_off)) {
4423 char tn_buf[48];
4424
4425 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004426 verbose(env, "has value %s", tn_buf);
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07004427 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004428 verbose(env, "has unknown scalar value");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07004429 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004430 verbose(env, " should have been 0 or 1\n");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07004431 return -EINVAL;
4432 }
4433 return 0;
4434}
4435
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004436/* non-recursive DFS pseudo code
4437 * 1 procedure DFS-iterative(G,v):
4438 * 2 label v as discovered
4439 * 3 let S be a stack
4440 * 4 S.push(v)
4441 * 5 while S is not empty
4442 * 6 t <- S.pop()
4443 * 7 if t is what we're looking for:
4444 * 8 return t
4445 * 9 for all edges e in G.adjacentEdges(t) do
4446 * 10 if edge e is already labelled
4447 * 11 continue with the next edge
4448 * 12 w <- G.adjacentVertex(t,e)
4449 * 13 if vertex w is not discovered and not explored
4450 * 14 label e as tree-edge
4451 * 15 label w as discovered
4452 * 16 S.push(w)
4453 * 17 continue at 5
4454 * 18 else if vertex w is discovered
4455 * 19 label e as back-edge
4456 * 20 else
4457 * 21 // vertex w is explored
4458 * 22 label e as forward- or cross-edge
4459 * 23 label t as explored
4460 * 24 S.pop()
4461 *
4462 * convention:
4463 * 0x10 - discovered
4464 * 0x11 - discovered and fall-through edge labelled
4465 * 0x12 - discovered and fall-through and branch edges labelled
4466 * 0x20 - explored
4467 */
4468
4469enum {
4470 DISCOVERED = 0x10,
4471 EXPLORED = 0x20,
4472 FALLTHROUGH = 1,
4473 BRANCH = 2,
4474};
4475
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004476#define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004477
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004478static int *insn_stack; /* stack of insns to process */
4479static int cur_stack; /* current stack index */
4480static int *insn_state;
4481
4482/* t, w, e - match pseudo-code above:
4483 * t - index of current instruction
4484 * w - next instruction
4485 * e - edge
4486 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004487static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004488{
4489 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
4490 return 0;
4491
4492 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
4493 return 0;
4494
4495 if (w < 0 || w >= env->prog->len) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004496 verbose(env, "jump out of range from insn %d to %d\n", t, w);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004497 return -EINVAL;
4498 }
4499
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004500 if (e == BRANCH)
4501 /* mark branch target for state pruning */
4502 env->explored_states[w] = STATE_LIST_MARK;
4503
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004504 if (insn_state[w] == 0) {
4505 /* tree-edge */
4506 insn_state[t] = DISCOVERED | e;
4507 insn_state[w] = DISCOVERED;
4508 if (cur_stack >= env->prog->len)
4509 return -E2BIG;
4510 insn_stack[cur_stack++] = w;
4511 return 1;
4512 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004513 verbose(env, "back-edge from insn %d to %d\n", t, w);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004514 return -EINVAL;
4515 } else if (insn_state[w] == EXPLORED) {
4516 /* forward- or cross-edge */
4517 insn_state[t] = DISCOVERED | e;
4518 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004519 verbose(env, "insn state internal bug\n");
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004520 return -EFAULT;
4521 }
4522 return 0;
4523}
4524
4525/* non-recursive depth-first-search to detect loops in BPF program
4526 * loop == back-edge in directed graph
4527 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004528static int check_cfg(struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004529{
4530 struct bpf_insn *insns = env->prog->insnsi;
4531 int insn_cnt = env->prog->len;
4532 int ret = 0;
4533 int i, t;
4534
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08004535 ret = check_subprogs(env);
4536 if (ret < 0)
4537 return ret;
4538
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004539 insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
4540 if (!insn_state)
4541 return -ENOMEM;
4542
4543 insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
4544 if (!insn_stack) {
4545 kfree(insn_state);
4546 return -ENOMEM;
4547 }
4548
4549 insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
4550 insn_stack[0] = 0; /* 0 is the first instruction */
4551 cur_stack = 1;
4552
4553peek_stack:
4554 if (cur_stack == 0)
4555 goto check_state;
4556 t = insn_stack[cur_stack - 1];
4557
4558 if (BPF_CLASS(insns[t].code) == BPF_JMP) {
4559 u8 opcode = BPF_OP(insns[t].code);
4560
4561 if (opcode == BPF_EXIT) {
4562 goto mark_explored;
4563 } else if (opcode == BPF_CALL) {
4564 ret = push_insn(t, t + 1, FALLTHROUGH, env);
4565 if (ret == 1)
4566 goto peek_stack;
4567 else if (ret < 0)
4568 goto err_free;
Daniel Borkmann07016152016-04-05 22:33:17 +02004569 if (t + 1 < insn_cnt)
4570 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08004571 if (insns[t].src_reg == BPF_PSEUDO_CALL) {
4572 env->explored_states[t] = STATE_LIST_MARK;
4573 ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env);
4574 if (ret == 1)
4575 goto peek_stack;
4576 else if (ret < 0)
4577 goto err_free;
4578 }
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004579 } else if (opcode == BPF_JA) {
4580 if (BPF_SRC(insns[t].code) != BPF_K) {
4581 ret = -EINVAL;
4582 goto err_free;
4583 }
4584 /* unconditional jump with single edge */
4585 ret = push_insn(t, t + insns[t].off + 1,
4586 FALLTHROUGH, env);
4587 if (ret == 1)
4588 goto peek_stack;
4589 else if (ret < 0)
4590 goto err_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004591 /* tell verifier to check for equivalent states
4592 * after every call and jump
4593 */
Alexei Starovoitovc3de6312015-04-14 15:57:13 -07004594 if (t + 1 < insn_cnt)
4595 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004596 } else {
4597 /* conditional jump with two edges */
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02004598 env->explored_states[t] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004599 ret = push_insn(t, t + 1, FALLTHROUGH, env);
4600 if (ret == 1)
4601 goto peek_stack;
4602 else if (ret < 0)
4603 goto err_free;
4604
4605 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
4606 if (ret == 1)
4607 goto peek_stack;
4608 else if (ret < 0)
4609 goto err_free;
4610 }
4611 } else {
4612 /* all other non-branch instructions with single
4613 * fall-through edge
4614 */
4615 ret = push_insn(t, t + 1, FALLTHROUGH, env);
4616 if (ret == 1)
4617 goto peek_stack;
4618 else if (ret < 0)
4619 goto err_free;
4620 }
4621
4622mark_explored:
4623 insn_state[t] = EXPLORED;
4624 if (cur_stack-- <= 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004625 verbose(env, "pop stack internal bug\n");
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004626 ret = -EFAULT;
4627 goto err_free;
4628 }
4629 goto peek_stack;
4630
4631check_state:
4632 for (i = 0; i < insn_cnt; i++) {
4633 if (insn_state[i] != EXPLORED) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004634 verbose(env, "unreachable insn %d\n", i);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004635 ret = -EINVAL;
4636 goto err_free;
4637 }
4638 }
4639 ret = 0; /* cfg looks good */
4640
4641err_free:
4642 kfree(insn_state);
4643 kfree(insn_stack);
4644 return ret;
4645}
4646
Yonghong Song838e9692018-11-19 15:29:11 -08004647/* The minimum supported BTF func info size */
4648#define MIN_BPF_FUNCINFO_SIZE 8
4649#define MAX_FUNCINFO_REC_SIZE 252
4650
Martin KaFai Lauc454a462018-12-07 16:42:25 -08004651static int check_btf_func(struct bpf_verifier_env *env,
4652 const union bpf_attr *attr,
4653 union bpf_attr __user *uattr)
Yonghong Song838e9692018-11-19 15:29:11 -08004654{
4655 u32 i, nfuncs, urec_size, min_size, prev_offset;
4656 u32 krec_size = sizeof(struct bpf_func_info);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08004657 struct bpf_func_info *krecord;
Yonghong Song838e9692018-11-19 15:29:11 -08004658 const struct btf_type *type;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08004659 struct bpf_prog *prog;
4660 const struct btf *btf;
Yonghong Song838e9692018-11-19 15:29:11 -08004661 void __user *urecord;
Yonghong Song838e9692018-11-19 15:29:11 -08004662 int ret = 0;
4663
4664 nfuncs = attr->func_info_cnt;
4665 if (!nfuncs)
4666 return 0;
4667
4668 if (nfuncs != env->subprog_cnt) {
4669 verbose(env, "number of funcs in func_info doesn't match number of subprogs\n");
4670 return -EINVAL;
4671 }
4672
4673 urec_size = attr->func_info_rec_size;
4674 if (urec_size < MIN_BPF_FUNCINFO_SIZE ||
4675 urec_size > MAX_FUNCINFO_REC_SIZE ||
4676 urec_size % sizeof(u32)) {
4677 verbose(env, "invalid func info rec size %u\n", urec_size);
4678 return -EINVAL;
4679 }
4680
Martin KaFai Lauc454a462018-12-07 16:42:25 -08004681 prog = env->prog;
4682 btf = prog->aux->btf;
Yonghong Song838e9692018-11-19 15:29:11 -08004683
4684 urecord = u64_to_user_ptr(attr->func_info);
4685 min_size = min_t(u32, krec_size, urec_size);
4686
Yonghong Songba64e7d2018-11-24 23:20:44 -08004687 krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08004688 if (!krecord)
4689 return -ENOMEM;
Yonghong Songba64e7d2018-11-24 23:20:44 -08004690
Yonghong Song838e9692018-11-19 15:29:11 -08004691 for (i = 0; i < nfuncs; i++) {
4692 ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size);
4693 if (ret) {
4694 if (ret == -E2BIG) {
4695 verbose(env, "nonzero tailing record in func info");
4696 /* set the size kernel expects so loader can zero
4697 * out the rest of the record.
4698 */
4699 if (put_user(min_size, &uattr->func_info_rec_size))
4700 ret = -EFAULT;
4701 }
Martin KaFai Lauc454a462018-12-07 16:42:25 -08004702 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08004703 }
4704
Yonghong Songba64e7d2018-11-24 23:20:44 -08004705 if (copy_from_user(&krecord[i], urecord, min_size)) {
Yonghong Song838e9692018-11-19 15:29:11 -08004706 ret = -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08004707 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08004708 }
4709
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08004710 /* check insn_off */
Yonghong Song838e9692018-11-19 15:29:11 -08004711 if (i == 0) {
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08004712 if (krecord[i].insn_off) {
Yonghong Song838e9692018-11-19 15:29:11 -08004713 verbose(env,
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08004714 "nonzero insn_off %u for the first func info record",
4715 krecord[i].insn_off);
Yonghong Song838e9692018-11-19 15:29:11 -08004716 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08004717 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08004718 }
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08004719 } else if (krecord[i].insn_off <= prev_offset) {
Yonghong Song838e9692018-11-19 15:29:11 -08004720 verbose(env,
4721 "same or smaller insn offset (%u) than previous func info record (%u)",
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08004722 krecord[i].insn_off, prev_offset);
Yonghong Song838e9692018-11-19 15:29:11 -08004723 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08004724 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08004725 }
4726
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08004727 if (env->subprog_info[i].start != krecord[i].insn_off) {
Yonghong Song838e9692018-11-19 15:29:11 -08004728 verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n");
4729 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08004730 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08004731 }
4732
4733 /* check type_id */
Yonghong Songba64e7d2018-11-24 23:20:44 -08004734 type = btf_type_by_id(btf, krecord[i].type_id);
Yonghong Song838e9692018-11-19 15:29:11 -08004735 if (!type || BTF_INFO_KIND(type->info) != BTF_KIND_FUNC) {
4736 verbose(env, "invalid type id %d in func info",
Yonghong Songba64e7d2018-11-24 23:20:44 -08004737 krecord[i].type_id);
Yonghong Song838e9692018-11-19 15:29:11 -08004738 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08004739 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08004740 }
4741
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08004742 prev_offset = krecord[i].insn_off;
Yonghong Song838e9692018-11-19 15:29:11 -08004743 urecord += urec_size;
4744 }
4745
Yonghong Songba64e7d2018-11-24 23:20:44 -08004746 prog->aux->func_info = krecord;
4747 prog->aux->func_info_cnt = nfuncs;
Yonghong Song838e9692018-11-19 15:29:11 -08004748 return 0;
4749
Martin KaFai Lauc454a462018-12-07 16:42:25 -08004750err_free:
Yonghong Songba64e7d2018-11-24 23:20:44 -08004751 kvfree(krecord);
Yonghong Song838e9692018-11-19 15:29:11 -08004752 return ret;
4753}
4754
Yonghong Songba64e7d2018-11-24 23:20:44 -08004755static void adjust_btf_func(struct bpf_verifier_env *env)
4756{
4757 int i;
4758
4759 if (!env->prog->aux->func_info)
4760 return;
4761
4762 for (i = 0; i < env->subprog_cnt; i++)
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08004763 env->prog->aux->func_info[i].insn_off = env->subprog_info[i].start;
Yonghong Songba64e7d2018-11-24 23:20:44 -08004764}
4765
Martin KaFai Lauc454a462018-12-07 16:42:25 -08004766#define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \
4767 sizeof(((struct bpf_line_info *)(0))->line_col))
4768#define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE
4769
4770static int check_btf_line(struct bpf_verifier_env *env,
4771 const union bpf_attr *attr,
4772 union bpf_attr __user *uattr)
4773{
4774 u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0;
4775 struct bpf_subprog_info *sub;
4776 struct bpf_line_info *linfo;
4777 struct bpf_prog *prog;
4778 const struct btf *btf;
4779 void __user *ulinfo;
4780 int err;
4781
4782 nr_linfo = attr->line_info_cnt;
4783 if (!nr_linfo)
4784 return 0;
4785
4786 rec_size = attr->line_info_rec_size;
4787 if (rec_size < MIN_BPF_LINEINFO_SIZE ||
4788 rec_size > MAX_LINEINFO_REC_SIZE ||
4789 rec_size & (sizeof(u32) - 1))
4790 return -EINVAL;
4791
4792 /* Need to zero it in case the userspace may
4793 * pass in a smaller bpf_line_info object.
4794 */
4795 linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info),
4796 GFP_KERNEL | __GFP_NOWARN);
4797 if (!linfo)
4798 return -ENOMEM;
4799
4800 prog = env->prog;
4801 btf = prog->aux->btf;
4802
4803 s = 0;
4804 sub = env->subprog_info;
4805 ulinfo = u64_to_user_ptr(attr->line_info);
4806 expected_size = sizeof(struct bpf_line_info);
4807 ncopy = min_t(u32, expected_size, rec_size);
4808 for (i = 0; i < nr_linfo; i++) {
4809 err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size);
4810 if (err) {
4811 if (err == -E2BIG) {
4812 verbose(env, "nonzero tailing record in line_info");
4813 if (put_user(expected_size,
4814 &uattr->line_info_rec_size))
4815 err = -EFAULT;
4816 }
4817 goto err_free;
4818 }
4819
4820 if (copy_from_user(&linfo[i], ulinfo, ncopy)) {
4821 err = -EFAULT;
4822 goto err_free;
4823 }
4824
4825 /*
4826 * Check insn_off to ensure
4827 * 1) strictly increasing AND
4828 * 2) bounded by prog->len
4829 *
4830 * The linfo[0].insn_off == 0 check logically falls into
4831 * the later "missing bpf_line_info for func..." case
4832 * because the first linfo[0].insn_off must be the
4833 * first sub also and the first sub must have
4834 * subprog_info[0].start == 0.
4835 */
4836 if ((i && linfo[i].insn_off <= prev_offset) ||
4837 linfo[i].insn_off >= prog->len) {
4838 verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n",
4839 i, linfo[i].insn_off, prev_offset,
4840 prog->len);
4841 err = -EINVAL;
4842 goto err_free;
4843 }
4844
4845 if (!btf_name_offset_valid(btf, linfo[i].line_off) ||
4846 !btf_name_offset_valid(btf, linfo[i].file_name_off)) {
4847 verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i);
4848 err = -EINVAL;
4849 goto err_free;
4850 }
4851
4852 if (s != env->subprog_cnt) {
4853 if (linfo[i].insn_off == sub[s].start) {
4854 sub[s].linfo_idx = i;
4855 s++;
4856 } else if (sub[s].start < linfo[i].insn_off) {
4857 verbose(env, "missing bpf_line_info for func#%u\n", s);
4858 err = -EINVAL;
4859 goto err_free;
4860 }
4861 }
4862
4863 prev_offset = linfo[i].insn_off;
4864 ulinfo += rec_size;
4865 }
4866
4867 if (s != env->subprog_cnt) {
4868 verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n",
4869 env->subprog_cnt - s, s);
4870 err = -EINVAL;
4871 goto err_free;
4872 }
4873
4874 prog->aux->linfo = linfo;
4875 prog->aux->nr_linfo = nr_linfo;
4876
4877 return 0;
4878
4879err_free:
4880 kvfree(linfo);
4881 return err;
4882}
4883
4884static int check_btf_info(struct bpf_verifier_env *env,
4885 const union bpf_attr *attr,
4886 union bpf_attr __user *uattr)
4887{
4888 struct btf *btf;
4889 int err;
4890
4891 if (!attr->func_info_cnt && !attr->line_info_cnt)
4892 return 0;
4893
4894 btf = btf_get_by_fd(attr->prog_btf_fd);
4895 if (IS_ERR(btf))
4896 return PTR_ERR(btf);
4897 env->prog->aux->btf = btf;
4898
4899 err = check_btf_func(env, attr, uattr);
4900 if (err)
4901 return err;
4902
4903 err = check_btf_line(env, attr, uattr);
4904 if (err)
4905 return err;
4906
4907 return 0;
4908}
4909
Edward Creef1174f72017-08-07 15:26:19 +01004910/* check %cur's range satisfies %old's */
4911static bool range_within(struct bpf_reg_state *old,
4912 struct bpf_reg_state *cur)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004913{
Edward Creeb03c9f92017-08-07 15:26:36 +01004914 return old->umin_value <= cur->umin_value &&
4915 old->umax_value >= cur->umax_value &&
4916 old->smin_value <= cur->smin_value &&
4917 old->smax_value >= cur->smax_value;
Edward Creef1174f72017-08-07 15:26:19 +01004918}
4919
4920/* Maximum number of register states that can exist at once */
4921#define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE)
4922struct idpair {
4923 u32 old;
4924 u32 cur;
4925};
4926
4927/* If in the old state two registers had the same id, then they need to have
4928 * the same id in the new state as well. But that id could be different from
4929 * the old state, so we need to track the mapping from old to new ids.
4930 * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent
4931 * regs with old id 5 must also have new id 9 for the new state to be safe. But
4932 * regs with a different old id could still have new id 9, we don't care about
4933 * that.
4934 * So we look through our idmap to see if this old id has been seen before. If
4935 * so, we require the new id to match; otherwise, we add the id pair to the map.
4936 */
4937static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap)
4938{
4939 unsigned int i;
4940
4941 for (i = 0; i < ID_MAP_SIZE; i++) {
4942 if (!idmap[i].old) {
4943 /* Reached an empty slot; haven't seen this id before */
4944 idmap[i].old = old_id;
4945 idmap[i].cur = cur_id;
4946 return true;
4947 }
4948 if (idmap[i].old == old_id)
4949 return idmap[i].cur == cur_id;
4950 }
4951 /* We ran out of idmap slots, which should be impossible */
4952 WARN_ON_ONCE(1);
4953 return false;
4954}
4955
4956/* Returns true if (rold safe implies rcur safe) */
Edward Cree1b688a12017-08-23 15:10:50 +01004957static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
4958 struct idpair *idmap)
Edward Creef1174f72017-08-07 15:26:19 +01004959{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004960 bool equal;
4961
Edward Creedc503a82017-08-15 20:34:35 +01004962 if (!(rold->live & REG_LIVE_READ))
4963 /* explored state didn't use this */
4964 return true;
4965
Edward Cree679c7822018-08-22 20:02:19 +01004966 equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004967
4968 if (rold->type == PTR_TO_STACK)
4969 /* two stack pointers are equal only if they're pointing to
4970 * the same stack frame, since fp-8 in foo != fp-8 in bar
4971 */
4972 return equal && rold->frameno == rcur->frameno;
4973
4974 if (equal)
Edward Creef1174f72017-08-07 15:26:19 +01004975 return true;
4976
4977 if (rold->type == NOT_INIT)
4978 /* explored state can't have used this */
4979 return true;
4980 if (rcur->type == NOT_INIT)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004981 return false;
Edward Creef1174f72017-08-07 15:26:19 +01004982 switch (rold->type) {
4983 case SCALAR_VALUE:
4984 if (rcur->type == SCALAR_VALUE) {
4985 /* new val must satisfy old val knowledge */
4986 return range_within(rold, rcur) &&
4987 tnum_in(rold->var_off, rcur->var_off);
4988 } else {
Jann Horn179d1c52017-12-18 20:11:59 -08004989 /* We're trying to use a pointer in place of a scalar.
4990 * Even if the scalar was unbounded, this could lead to
4991 * pointer leaks because scalars are allowed to leak
4992 * while pointers are not. We could make this safe in
4993 * special cases if root is calling us, but it's
4994 * probably not worth the hassle.
Edward Creef1174f72017-08-07 15:26:19 +01004995 */
Jann Horn179d1c52017-12-18 20:11:59 -08004996 return false;
Edward Creef1174f72017-08-07 15:26:19 +01004997 }
4998 case PTR_TO_MAP_VALUE:
Edward Cree1b688a12017-08-23 15:10:50 +01004999 /* If the new min/max/var_off satisfy the old ones and
5000 * everything else matches, we are OK.
5001 * We don't care about the 'id' value, because nothing
5002 * uses it for PTR_TO_MAP_VALUE (only for ..._OR_NULL)
5003 */
5004 return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 &&
5005 range_within(rold, rcur) &&
5006 tnum_in(rold->var_off, rcur->var_off);
Edward Creef1174f72017-08-07 15:26:19 +01005007 case PTR_TO_MAP_VALUE_OR_NULL:
5008 /* a PTR_TO_MAP_VALUE could be safe to use as a
5009 * PTR_TO_MAP_VALUE_OR_NULL into the same map.
5010 * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL-
5011 * checked, doing so could have affected others with the same
5012 * id, and we can't check for that because we lost the id when
5013 * we converted to a PTR_TO_MAP_VALUE.
5014 */
5015 if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL)
5016 return false;
5017 if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)))
5018 return false;
5019 /* Check our ids match any regs they're supposed to */
5020 return check_ids(rold->id, rcur->id, idmap);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02005021 case PTR_TO_PACKET_META:
Edward Creef1174f72017-08-07 15:26:19 +01005022 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02005023 if (rcur->type != rold->type)
Edward Creef1174f72017-08-07 15:26:19 +01005024 return false;
5025 /* We must have at least as much range as the old ptr
5026 * did, so that any accesses which were safe before are
5027 * still safe. This is true even if old range < old off,
5028 * since someone could have accessed through (ptr - k), or
5029 * even done ptr -= k in a register, to get a safe access.
5030 */
5031 if (rold->range > rcur->range)
5032 return false;
5033 /* If the offsets don't match, we can't trust our alignment;
5034 * nor can we be sure that we won't fall out of range.
5035 */
5036 if (rold->off != rcur->off)
5037 return false;
5038 /* id relations must be preserved */
5039 if (rold->id && !check_ids(rold->id, rcur->id, idmap))
5040 return false;
5041 /* new val must satisfy old val knowledge */
5042 return range_within(rold, rcur) &&
5043 tnum_in(rold->var_off, rcur->var_off);
5044 case PTR_TO_CTX:
5045 case CONST_PTR_TO_MAP:
Edward Creef1174f72017-08-07 15:26:19 +01005046 case PTR_TO_PACKET_END:
Petar Penkovd58e4682018-09-14 07:46:18 -07005047 case PTR_TO_FLOW_KEYS:
Joe Stringerc64b7982018-10-02 13:35:33 -07005048 case PTR_TO_SOCKET:
5049 case PTR_TO_SOCKET_OR_NULL:
Edward Creef1174f72017-08-07 15:26:19 +01005050 /* Only valid matches are exact, which memcmp() above
5051 * would have accepted
5052 */
5053 default:
5054 /* Don't know what's going on, just say it's not safe */
5055 return false;
5056 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07005057
Edward Creef1174f72017-08-07 15:26:19 +01005058 /* Shouldn't get here; if we do, say it's not safe */
5059 WARN_ON_ONCE(1);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07005060 return false;
5061}
5062
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005063static bool stacksafe(struct bpf_func_state *old,
5064 struct bpf_func_state *cur,
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005065 struct idpair *idmap)
5066{
5067 int i, spi;
5068
5069 /* if explored stack has more populated slots than current stack
5070 * such stacks are not equivalent
5071 */
5072 if (old->allocated_stack > cur->allocated_stack)
5073 return false;
5074
5075 /* walk slots of the explored stack and ignore any additional
5076 * slots in the current stack, since explored(safe) state
5077 * didn't use them
5078 */
5079 for (i = 0; i < old->allocated_stack; i++) {
5080 spi = i / BPF_REG_SIZE;
5081
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08005082 if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ))
5083 /* explored state didn't use this */
Gianluca Borellofd05e572017-12-23 10:09:55 +00005084 continue;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08005085
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005086 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID)
5087 continue;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08005088 /* if old state was safe with misc data in the stack
5089 * it will be safe with zero-initialized stack.
5090 * The opposite is not true
5091 */
5092 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC &&
5093 cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO)
5094 continue;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005095 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] !=
5096 cur->stack[spi].slot_type[i % BPF_REG_SIZE])
5097 /* Ex: old explored (safe) state has STACK_SPILL in
5098 * this stack slot, but current has has STACK_MISC ->
5099 * this verifier states are not equivalent,
5100 * return false to continue verification of this path
5101 */
5102 return false;
5103 if (i % BPF_REG_SIZE)
5104 continue;
5105 if (old->stack[spi].slot_type[0] != STACK_SPILL)
5106 continue;
5107 if (!regsafe(&old->stack[spi].spilled_ptr,
5108 &cur->stack[spi].spilled_ptr,
5109 idmap))
5110 /* when explored and current stack slot are both storing
5111 * spilled registers, check that stored pointers types
5112 * are the same as well.
5113 * Ex: explored safe path could have stored
5114 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8}
5115 * but current path has stored:
5116 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16}
5117 * such verifier states are not equivalent.
5118 * return false to continue verification of this path
5119 */
5120 return false;
5121 }
5122 return true;
5123}
5124
Joe Stringerfd978bf72018-10-02 13:35:35 -07005125static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur)
5126{
5127 if (old->acquired_refs != cur->acquired_refs)
5128 return false;
5129 return !memcmp(old->refs, cur->refs,
5130 sizeof(*old->refs) * old->acquired_refs);
5131}
5132
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005133/* compare two verifier states
5134 *
5135 * all states stored in state_list are known to be valid, since
5136 * verifier reached 'bpf_exit' instruction through them
5137 *
5138 * this function is called when verifier exploring different branches of
5139 * execution popped from the state stack. If it sees an old state that has
5140 * more strict register state and more strict stack state then this execution
5141 * branch doesn't need to be explored further, since verifier already
5142 * concluded that more strict state leads to valid finish.
5143 *
5144 * Therefore two states are equivalent if register state is more conservative
5145 * and explored stack state is more conservative than the current one.
5146 * Example:
5147 * explored current
5148 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
5149 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
5150 *
5151 * In other words if current stack state (one being explored) has more
5152 * valid slots than old one that already passed validation, it means
5153 * the verifier can stop exploring and conclude that current state is valid too
5154 *
5155 * Similarly with registers. If explored state has register type as invalid
5156 * whereas register type in current state is meaningful, it means that
5157 * the current state will reach 'bpf_exit' instruction safely
5158 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005159static bool func_states_equal(struct bpf_func_state *old,
5160 struct bpf_func_state *cur)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005161{
Edward Creef1174f72017-08-07 15:26:19 +01005162 struct idpair *idmap;
5163 bool ret = false;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005164 int i;
5165
Edward Creef1174f72017-08-07 15:26:19 +01005166 idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL);
5167 /* If we failed to allocate the idmap, just say it's not safe */
5168 if (!idmap)
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07005169 return false;
Edward Creef1174f72017-08-07 15:26:19 +01005170
5171 for (i = 0; i < MAX_BPF_REG; i++) {
Edward Cree1b688a12017-08-23 15:10:50 +01005172 if (!regsafe(&old->regs[i], &cur->regs[i], idmap))
Edward Creef1174f72017-08-07 15:26:19 +01005173 goto out_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005174 }
5175
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005176 if (!stacksafe(old, cur, idmap))
5177 goto out_free;
Joe Stringerfd978bf72018-10-02 13:35:35 -07005178
5179 if (!refsafe(old, cur))
5180 goto out_free;
Edward Creef1174f72017-08-07 15:26:19 +01005181 ret = true;
5182out_free:
5183 kfree(idmap);
5184 return ret;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005185}
5186
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005187static bool states_equal(struct bpf_verifier_env *env,
5188 struct bpf_verifier_state *old,
5189 struct bpf_verifier_state *cur)
Edward Creedc503a82017-08-15 20:34:35 +01005190{
Edward Creedc503a82017-08-15 20:34:35 +01005191 int i;
5192
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005193 if (old->curframe != cur->curframe)
5194 return false;
5195
5196 /* for states to be equal callsites have to be the same
5197 * and all frame states need to be equivalent
5198 */
5199 for (i = 0; i <= old->curframe; i++) {
5200 if (old->frame[i]->callsite != cur->frame[i]->callsite)
5201 return false;
5202 if (!func_states_equal(old->frame[i], cur->frame[i]))
5203 return false;
5204 }
5205 return true;
5206}
5207
5208/* A write screens off any subsequent reads; but write marks come from the
5209 * straight-line code between a state and its parent. When we arrive at an
5210 * equivalent state (jump target or such) we didn't arrive by the straight-line
5211 * code, so read marks in the state must propagate to the parent regardless
5212 * of the state's write marks. That's what 'parent == state->parent' comparison
Edward Cree679c7822018-08-22 20:02:19 +01005213 * in mark_reg_read() is for.
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005214 */
5215static int propagate_liveness(struct bpf_verifier_env *env,
5216 const struct bpf_verifier_state *vstate,
5217 struct bpf_verifier_state *vparent)
5218{
5219 int i, frame, err = 0;
5220 struct bpf_func_state *state, *parent;
5221
5222 if (vparent->curframe != vstate->curframe) {
5223 WARN(1, "propagate_live: parent frame %d current frame %d\n",
5224 vparent->curframe, vstate->curframe);
5225 return -EFAULT;
5226 }
Edward Creedc503a82017-08-15 20:34:35 +01005227 /* Propagate read liveness of registers... */
5228 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
5229 /* We don't need to worry about FP liveness because it's read-only */
5230 for (i = 0; i < BPF_REG_FP; i++) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005231 if (vparent->frame[vparent->curframe]->regs[i].live & REG_LIVE_READ)
Edward Creedc503a82017-08-15 20:34:35 +01005232 continue;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005233 if (vstate->frame[vstate->curframe]->regs[i].live & REG_LIVE_READ) {
Edward Cree679c7822018-08-22 20:02:19 +01005234 err = mark_reg_read(env, &vstate->frame[vstate->curframe]->regs[i],
5235 &vparent->frame[vstate->curframe]->regs[i]);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005236 if (err)
5237 return err;
Edward Creedc503a82017-08-15 20:34:35 +01005238 }
5239 }
Edward Creedc503a82017-08-15 20:34:35 +01005240
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005241 /* ... and stack slots */
5242 for (frame = 0; frame <= vstate->curframe; frame++) {
5243 state = vstate->frame[frame];
5244 parent = vparent->frame[frame];
5245 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE &&
5246 i < parent->allocated_stack / BPF_REG_SIZE; i++) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005247 if (parent->stack[i].spilled_ptr.live & REG_LIVE_READ)
5248 continue;
5249 if (state->stack[i].spilled_ptr.live & REG_LIVE_READ)
Edward Cree679c7822018-08-22 20:02:19 +01005250 mark_reg_read(env, &state->stack[i].spilled_ptr,
5251 &parent->stack[i].spilled_ptr);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005252 }
Edward Creedc503a82017-08-15 20:34:35 +01005253 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005254 return err;
Edward Creedc503a82017-08-15 20:34:35 +01005255}
5256
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005257static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005258{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005259 struct bpf_verifier_state_list *new_sl;
5260 struct bpf_verifier_state_list *sl;
Edward Cree679c7822018-08-22 20:02:19 +01005261 struct bpf_verifier_state *cur = env->cur_state, *new;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005262 int i, j, err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005263
5264 sl = env->explored_states[insn_idx];
5265 if (!sl)
5266 /* this 'insn_idx' instruction wasn't marked, so we will not
5267 * be doing state search here
5268 */
5269 return 0;
5270
5271 while (sl != STATE_LIST_MARK) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005272 if (states_equal(env, &sl->state, cur)) {
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005273 /* reached equivalent register/stack state,
Edward Creedc503a82017-08-15 20:34:35 +01005274 * prune the search.
5275 * Registers read by the continuation are read by us.
Edward Cree8e9cd9c2017-08-23 15:11:21 +01005276 * If we have any write marks in env->cur_state, they
5277 * will prevent corresponding reads in the continuation
5278 * from reaching our parent (an explored_state). Our
5279 * own state will get the read marks recorded, but
5280 * they'll be immediately forgotten as we're pruning
5281 * this state and will pop a new one.
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005282 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005283 err = propagate_liveness(env, &sl->state, cur);
5284 if (err)
5285 return err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005286 return 1;
Edward Creedc503a82017-08-15 20:34:35 +01005287 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005288 sl = sl->next;
5289 }
5290
5291 /* there were no equivalent states, remember current one.
5292 * technically the current state is not proven to be safe yet,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005293 * but it will either reach outer most bpf_exit (which means it's safe)
5294 * or it will be rejected. Since there are no loops, we won't be
5295 * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx)
5296 * again on the way to bpf_exit
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005297 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005298 new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005299 if (!new_sl)
5300 return -ENOMEM;
5301
5302 /* add new state to the head of linked list */
Edward Cree679c7822018-08-22 20:02:19 +01005303 new = &new_sl->state;
5304 err = copy_verifier_state(new, cur);
Alexei Starovoitov1969db42017-11-01 00:08:04 -07005305 if (err) {
Edward Cree679c7822018-08-22 20:02:19 +01005306 free_verifier_state(new, false);
Alexei Starovoitov1969db42017-11-01 00:08:04 -07005307 kfree(new_sl);
5308 return err;
5309 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005310 new_sl->next = env->explored_states[insn_idx];
5311 env->explored_states[insn_idx] = new_sl;
Edward Creedc503a82017-08-15 20:34:35 +01005312 /* connect new state to parentage chain */
Edward Cree679c7822018-08-22 20:02:19 +01005313 for (i = 0; i < BPF_REG_FP; i++)
5314 cur_regs(env)[i].parent = &new->frame[new->curframe]->regs[i];
Edward Cree8e9cd9c2017-08-23 15:11:21 +01005315 /* clear write marks in current state: the writes we did are not writes
5316 * our child did, so they don't screen off its reads from us.
5317 * (There are no read marks in current state, because reads always mark
5318 * their parent and current state never has children yet. Only
5319 * explored_states can get read marks.)
5320 */
Edward Creedc503a82017-08-15 20:34:35 +01005321 for (i = 0; i < BPF_REG_FP; i++)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005322 cur->frame[cur->curframe]->regs[i].live = REG_LIVE_NONE;
5323
5324 /* all stack frames are accessible from callee, clear them all */
5325 for (j = 0; j <= cur->curframe; j++) {
5326 struct bpf_func_state *frame = cur->frame[j];
Edward Cree679c7822018-08-22 20:02:19 +01005327 struct bpf_func_state *newframe = new->frame[j];
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005328
Edward Cree679c7822018-08-22 20:02:19 +01005329 for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08005330 frame->stack[i].spilled_ptr.live = REG_LIVE_NONE;
Edward Cree679c7822018-08-22 20:02:19 +01005331 frame->stack[i].spilled_ptr.parent =
5332 &newframe->stack[i].spilled_ptr;
5333 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005334 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005335 return 0;
5336}
5337
Joe Stringerc64b7982018-10-02 13:35:33 -07005338/* Return true if it's OK to have the same insn return a different type. */
5339static bool reg_type_mismatch_ok(enum bpf_reg_type type)
5340{
5341 switch (type) {
5342 case PTR_TO_CTX:
5343 case PTR_TO_SOCKET:
5344 case PTR_TO_SOCKET_OR_NULL:
5345 return false;
5346 default:
5347 return true;
5348 }
5349}
5350
5351/* If an instruction was previously used with particular pointer types, then we
5352 * need to be careful to avoid cases such as the below, where it may be ok
5353 * for one branch accessing the pointer, but not ok for the other branch:
5354 *
5355 * R1 = sock_ptr
5356 * goto X;
5357 * ...
5358 * R1 = some_other_valid_ptr;
5359 * goto X;
5360 * ...
5361 * R2 = *(u32 *)(R1 + 0);
5362 */
5363static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev)
5364{
5365 return src != prev && (!reg_type_mismatch_ok(src) ||
5366 !reg_type_mismatch_ok(prev));
5367}
5368
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005369static int do_check(struct bpf_verifier_env *env)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005370{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005371 struct bpf_verifier_state *state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005372 struct bpf_insn *insns = env->prog->insnsi;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005373 struct bpf_reg_state *regs;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005374 int insn_cnt = env->prog->len, i;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005375 int insn_idx, prev_insn_idx = 0;
5376 int insn_processed = 0;
5377 bool do_print_state = false;
5378
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005379 state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL);
5380 if (!state)
5381 return -ENOMEM;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005382 state->curframe = 0;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005383 state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL);
5384 if (!state->frame[0]) {
5385 kfree(state);
5386 return -ENOMEM;
5387 }
5388 env->cur_state = state;
5389 init_func_state(env, state->frame[0],
5390 BPF_MAIN_FUNC /* callsite */,
5391 0 /* frameno */,
5392 0 /* subprogno, zero == main subprog */);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005393 insn_idx = 0;
5394 for (;;) {
5395 struct bpf_insn *insn;
5396 u8 class;
5397 int err;
5398
5399 if (insn_idx >= insn_cnt) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005400 verbose(env, "invalid insn idx %d insn_cnt %d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005401 insn_idx, insn_cnt);
5402 return -EFAULT;
5403 }
5404
5405 insn = &insns[insn_idx];
5406 class = BPF_CLASS(insn->code);
5407
Daniel Borkmann07016152016-04-05 22:33:17 +02005408 if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005409 verbose(env,
5410 "BPF program is too large. Processed %d insn\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005411 insn_processed);
5412 return -E2BIG;
5413 }
5414
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005415 err = is_state_visited(env, insn_idx);
5416 if (err < 0)
5417 return err;
5418 if (err == 1) {
5419 /* found equivalent state, can prune the search */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005420 if (env->log.level) {
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005421 if (do_print_state)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005422 verbose(env, "\nfrom %d to %d: safe\n",
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005423 prev_insn_idx, insn_idx);
5424 else
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005425 verbose(env, "%d: safe\n", insn_idx);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005426 }
5427 goto process_bpf_exit;
5428 }
5429
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02005430 if (need_resched())
5431 cond_resched();
5432
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005433 if (env->log.level > 1 || (env->log.level && do_print_state)) {
5434 if (env->log.level > 1)
5435 verbose(env, "%d:", insn_idx);
David S. Millerc5fc9692017-05-10 11:25:17 -07005436 else
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005437 verbose(env, "\nfrom %d to %d:",
David S. Millerc5fc9692017-05-10 11:25:17 -07005438 prev_insn_idx, insn_idx);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005439 print_verifier_state(env, state->frame[state->curframe]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005440 do_print_state = false;
5441 }
5442
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005443 if (env->log.level) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01005444 const struct bpf_insn_cbs cbs = {
5445 .cb_print = verbose,
Jiri Olsaabe08842018-03-23 11:41:28 +01005446 .private_data = env,
Daniel Borkmann7105e822017-12-20 13:42:57 +01005447 };
5448
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005449 verbose(env, "%d: ", insn_idx);
Jiri Olsaabe08842018-03-23 11:41:28 +01005450 print_bpf_insn(&cbs, insn, env->allow_ptr_leaks);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005451 }
5452
Jakub Kicinskicae19272017-12-27 18:39:05 -08005453 if (bpf_prog_is_dev_bound(env->prog->aux)) {
5454 err = bpf_prog_offload_verify_insn(env, insn_idx,
5455 prev_insn_idx);
5456 if (err)
5457 return err;
5458 }
Jakub Kicinski13a27df2016-09-21 11:43:58 +01005459
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005460 regs = cur_regs(env);
Alexei Starovoitovc1311872017-11-22 16:42:05 -08005461 env->insn_aux_data[insn_idx].seen = true;
Joe Stringerfd978bf72018-10-02 13:35:35 -07005462
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005463 if (class == BPF_ALU || class == BPF_ALU64) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07005464 err = check_alu_op(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005465 if (err)
5466 return err;
5467
5468 } else if (class == BPF_LDX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005469 enum bpf_reg_type *prev_src_type, src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005470
5471 /* check for reserved fields is already done */
5472
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005473 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01005474 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005475 if (err)
5476 return err;
5477
Edward Creedc503a82017-08-15 20:34:35 +01005478 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005479 if (err)
5480 return err;
5481
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07005482 src_reg_type = regs[insn->src_reg].type;
5483
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005484 /* check that memory (src_reg + off) is readable,
5485 * the state of dst_reg will be updated by this func
5486 */
Yonghong Song31fd8582017-06-13 15:52:13 -07005487 err = check_mem_access(env, insn_idx, insn->src_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005488 BPF_SIZE(insn->code), BPF_READ,
Daniel Borkmannca369602018-02-23 22:29:05 +01005489 insn->dst_reg, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005490 if (err)
5491 return err;
5492
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005493 prev_src_type = &env->insn_aux_data[insn_idx].ptr_type;
5494
5495 if (*prev_src_type == NOT_INIT) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005496 /* saw a valid insn
5497 * dst_reg = *(u32 *)(src_reg + off)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005498 * save type to validate intersecting paths
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005499 */
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005500 *prev_src_type = src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005501
Joe Stringerc64b7982018-10-02 13:35:33 -07005502 } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005503 /* ABuser program is trying to use the same insn
5504 * dst_reg = *(u32*) (src_reg + off)
5505 * with different pointer types:
5506 * src_reg == ctx in one branch and
5507 * src_reg == stack|map in some other branch.
5508 * Reject it.
5509 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005510 verbose(env, "same insn cannot be used with different pointers\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005511 return -EINVAL;
5512 }
5513
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005514 } else if (class == BPF_STX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005515 enum bpf_reg_type *prev_dst_type, dst_reg_type;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07005516
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005517 if (BPF_MODE(insn->code) == BPF_XADD) {
Yonghong Song31fd8582017-06-13 15:52:13 -07005518 err = check_xadd(env, insn_idx, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005519 if (err)
5520 return err;
5521 insn_idx++;
5522 continue;
5523 }
5524
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005525 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01005526 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005527 if (err)
5528 return err;
5529 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01005530 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005531 if (err)
5532 return err;
5533
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07005534 dst_reg_type = regs[insn->dst_reg].type;
5535
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005536 /* check that memory (dst_reg + off) is writeable */
Yonghong Song31fd8582017-06-13 15:52:13 -07005537 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005538 BPF_SIZE(insn->code), BPF_WRITE,
Daniel Borkmannca369602018-02-23 22:29:05 +01005539 insn->src_reg, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005540 if (err)
5541 return err;
5542
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005543 prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type;
5544
5545 if (*prev_dst_type == NOT_INIT) {
5546 *prev_dst_type = dst_reg_type;
Joe Stringerc64b7982018-10-02 13:35:33 -07005547 } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005548 verbose(env, "same insn cannot be used with different pointers\n");
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07005549 return -EINVAL;
5550 }
5551
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005552 } else if (class == BPF_ST) {
5553 if (BPF_MODE(insn->code) != BPF_MEM ||
5554 insn->src_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005555 verbose(env, "BPF_ST uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005556 return -EINVAL;
5557 }
5558 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01005559 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005560 if (err)
5561 return err;
5562
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01005563 if (is_ctx_reg(env, insn->dst_reg)) {
Joe Stringer9d2be442018-10-02 13:35:31 -07005564 verbose(env, "BPF_ST stores into R%d %s is not allowed\n",
Daniel Borkmann2a159c62018-10-21 02:09:24 +02005565 insn->dst_reg,
5566 reg_type_str[reg_state(env, insn->dst_reg)->type]);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01005567 return -EACCES;
5568 }
5569
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005570 /* check that memory (dst_reg + off) is writeable */
Yonghong Song31fd8582017-06-13 15:52:13 -07005571 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005572 BPF_SIZE(insn->code), BPF_WRITE,
Daniel Borkmannca369602018-02-23 22:29:05 +01005573 -1, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005574 if (err)
5575 return err;
5576
5577 } else if (class == BPF_JMP) {
5578 u8 opcode = BPF_OP(insn->code);
5579
5580 if (opcode == BPF_CALL) {
5581 if (BPF_SRC(insn->code) != BPF_K ||
5582 insn->off != 0 ||
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005583 (insn->src_reg != BPF_REG_0 &&
5584 insn->src_reg != BPF_PSEUDO_CALL) ||
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005585 insn->dst_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005586 verbose(env, "BPF_CALL uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005587 return -EINVAL;
5588 }
5589
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005590 if (insn->src_reg == BPF_PSEUDO_CALL)
5591 err = check_func_call(env, insn, &insn_idx);
5592 else
5593 err = check_helper_call(env, insn->imm, insn_idx);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005594 if (err)
5595 return err;
5596
5597 } else if (opcode == BPF_JA) {
5598 if (BPF_SRC(insn->code) != BPF_K ||
5599 insn->imm != 0 ||
5600 insn->src_reg != BPF_REG_0 ||
5601 insn->dst_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005602 verbose(env, "BPF_JA uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005603 return -EINVAL;
5604 }
5605
5606 insn_idx += insn->off + 1;
5607 continue;
5608
5609 } else if (opcode == BPF_EXIT) {
5610 if (BPF_SRC(insn->code) != BPF_K ||
5611 insn->imm != 0 ||
5612 insn->src_reg != BPF_REG_0 ||
5613 insn->dst_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005614 verbose(env, "BPF_EXIT uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005615 return -EINVAL;
5616 }
5617
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005618 if (state->curframe) {
5619 /* exit from nested function */
5620 prev_insn_idx = insn_idx;
5621 err = prepare_func_exit(env, &insn_idx);
5622 if (err)
5623 return err;
5624 do_print_state = true;
5625 continue;
5626 }
5627
Joe Stringerfd978bf72018-10-02 13:35:35 -07005628 err = check_reference_leak(env);
5629 if (err)
5630 return err;
5631
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005632 /* eBPF calling convetion is such that R0 is used
5633 * to return the value from eBPF program.
5634 * Make sure that it's readable at this time
5635 * of bpf_exit, which means that program wrote
5636 * something into it earlier
5637 */
Edward Creedc503a82017-08-15 20:34:35 +01005638 err = check_reg_arg(env, BPF_REG_0, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005639 if (err)
5640 return err;
5641
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07005642 if (is_pointer_value(env, BPF_REG_0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005643 verbose(env, "R0 leaks addr as return value\n");
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07005644 return -EACCES;
5645 }
5646
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005647 err = check_return_code(env);
5648 if (err)
5649 return err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005650process_bpf_exit:
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005651 err = pop_stack(env, &prev_insn_idx, &insn_idx);
5652 if (err < 0) {
5653 if (err != -ENOENT)
5654 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005655 break;
5656 } else {
5657 do_print_state = true;
5658 continue;
5659 }
5660 } else {
5661 err = check_cond_jmp_op(env, insn, &insn_idx);
5662 if (err)
5663 return err;
5664 }
5665 } else if (class == BPF_LD) {
5666 u8 mode = BPF_MODE(insn->code);
5667
5668 if (mode == BPF_ABS || mode == BPF_IND) {
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005669 err = check_ld_abs(env, insn);
5670 if (err)
5671 return err;
5672
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005673 } else if (mode == BPF_IMM) {
5674 err = check_ld_imm(env, insn);
5675 if (err)
5676 return err;
5677
5678 insn_idx++;
Alexei Starovoitovc1311872017-11-22 16:42:05 -08005679 env->insn_aux_data[insn_idx].seen = true;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005680 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005681 verbose(env, "invalid BPF_LD mode\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005682 return -EINVAL;
5683 }
5684 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005685 verbose(env, "unknown insn class %d\n", class);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005686 return -EINVAL;
5687 }
5688
5689 insn_idx++;
5690 }
5691
Daniel Borkmann4bd95f42018-01-20 01:24:36 +01005692 verbose(env, "processed %d insns (limit %d), stack depth ",
5693 insn_processed, BPF_COMPLEXITY_LIMIT_INSNS);
Jiong Wangf910cef2018-05-02 16:17:17 -04005694 for (i = 0; i < env->subprog_cnt; i++) {
Jiong Wang9c8105b2018-05-02 16:17:18 -04005695 u32 depth = env->subprog_info[i].stack_depth;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005696
5697 verbose(env, "%d", depth);
Jiong Wangf910cef2018-05-02 16:17:17 -04005698 if (i + 1 < env->subprog_cnt)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005699 verbose(env, "+");
5700 }
5701 verbose(env, "\n");
Jiong Wang9c8105b2018-05-02 16:17:18 -04005702 env->prog->aux->stack_depth = env->subprog_info[0].stack_depth;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005703 return 0;
5704}
5705
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07005706static int check_map_prealloc(struct bpf_map *map)
5707{
5708 return (map->map_type != BPF_MAP_TYPE_HASH &&
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07005709 map->map_type != BPF_MAP_TYPE_PERCPU_HASH &&
5710 map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) ||
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07005711 !(map->map_flags & BPF_F_NO_PREALLOC);
5712}
5713
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005714static int check_map_prog_compatibility(struct bpf_verifier_env *env,
5715 struct bpf_map *map,
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07005716 struct bpf_prog *prog)
5717
5718{
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07005719 /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use
5720 * preallocated hash maps, since doing memory allocation
5721 * in overflow_handler can crash depending on where nmi got
5722 * triggered.
5723 */
5724 if (prog->type == BPF_PROG_TYPE_PERF_EVENT) {
5725 if (!check_map_prealloc(map)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005726 verbose(env, "perf_event programs can only use preallocated hash map\n");
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07005727 return -EINVAL;
5728 }
5729 if (map->inner_map_meta &&
5730 !check_map_prealloc(map->inner_map_meta)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005731 verbose(env, "perf_event programs can only use preallocated inner hash map\n");
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07005732 return -EINVAL;
5733 }
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07005734 }
Jakub Kicinskia3884572018-01-11 20:29:09 -08005735
5736 if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) &&
Jakub Kicinski09728262018-07-17 10:53:23 -07005737 !bpf_offload_prog_map_match(prog, map)) {
Jakub Kicinskia3884572018-01-11 20:29:09 -08005738 verbose(env, "offload device mismatch between prog and map\n");
5739 return -EINVAL;
5740 }
5741
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07005742 return 0;
5743}
5744
Roman Gushchinb741f162018-09-28 14:45:43 +00005745static bool bpf_map_is_cgroup_storage(struct bpf_map *map)
5746{
5747 return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE ||
5748 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE);
5749}
5750
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005751/* look for pseudo eBPF instructions that access map FDs and
5752 * replace them with actual map pointers
5753 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005754static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005755{
5756 struct bpf_insn *insn = env->prog->insnsi;
5757 int insn_cnt = env->prog->len;
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07005758 int i, j, err;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005759
Daniel Borkmannf1f77142017-01-13 23:38:15 +01005760 err = bpf_prog_calc_tag(env->prog);
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01005761 if (err)
5762 return err;
5763
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005764 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005765 if (BPF_CLASS(insn->code) == BPF_LDX &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07005766 (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005767 verbose(env, "BPF_LDX uses reserved fields\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005768 return -EINVAL;
5769 }
5770
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07005771 if (BPF_CLASS(insn->code) == BPF_STX &&
5772 ((BPF_MODE(insn->code) != BPF_MEM &&
5773 BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005774 verbose(env, "BPF_STX uses reserved fields\n");
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07005775 return -EINVAL;
5776 }
5777
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005778 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
5779 struct bpf_map *map;
5780 struct fd f;
5781
5782 if (i == insn_cnt - 1 || insn[1].code != 0 ||
5783 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
5784 insn[1].off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005785 verbose(env, "invalid bpf_ld_imm64 insn\n");
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005786 return -EINVAL;
5787 }
5788
5789 if (insn->src_reg == 0)
5790 /* valid generic load 64-bit imm */
5791 goto next_insn;
5792
5793 if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005794 verbose(env,
5795 "unrecognized bpf_ld_imm64 insn\n");
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005796 return -EINVAL;
5797 }
5798
5799 f = fdget(insn->imm);
Daniel Borkmannc2101292015-10-29 14:58:07 +01005800 map = __bpf_map_get(f);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005801 if (IS_ERR(map)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005802 verbose(env, "fd %d is not pointing to valid bpf_map\n",
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005803 insn->imm);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005804 return PTR_ERR(map);
5805 }
5806
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005807 err = check_map_prog_compatibility(env, map, env->prog);
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07005808 if (err) {
5809 fdput(f);
5810 return err;
5811 }
5812
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005813 /* store map pointer inside BPF_LD_IMM64 instruction */
5814 insn[0].imm = (u32) (unsigned long) map;
5815 insn[1].imm = ((u64) (unsigned long) map) >> 32;
5816
5817 /* check whether we recorded this map already */
5818 for (j = 0; j < env->used_map_cnt; j++)
5819 if (env->used_maps[j] == map) {
5820 fdput(f);
5821 goto next_insn;
5822 }
5823
5824 if (env->used_map_cnt >= MAX_USED_MAPS) {
5825 fdput(f);
5826 return -E2BIG;
5827 }
5828
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005829 /* hold the map. If the program is rejected by verifier,
5830 * the map will be released by release_maps() or it
5831 * will be used by the valid program until it's unloaded
Jakub Kicinskiab7f5bf2018-05-03 18:37:17 -07005832 * and all maps are released in free_used_maps()
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005833 */
Alexei Starovoitov92117d82016-04-27 18:56:20 -07005834 map = bpf_map_inc(map, false);
5835 if (IS_ERR(map)) {
5836 fdput(f);
5837 return PTR_ERR(map);
5838 }
5839 env->used_maps[env->used_map_cnt++] = map;
5840
Roman Gushchinb741f162018-09-28 14:45:43 +00005841 if (bpf_map_is_cgroup_storage(map) &&
Roman Gushchinde9cbba2018-08-02 14:27:18 -07005842 bpf_cgroup_storage_assign(env->prog, map)) {
Roman Gushchinb741f162018-09-28 14:45:43 +00005843 verbose(env, "only one cgroup storage of each type is allowed\n");
Roman Gushchinde9cbba2018-08-02 14:27:18 -07005844 fdput(f);
5845 return -EBUSY;
5846 }
5847
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005848 fdput(f);
5849next_insn:
5850 insn++;
5851 i++;
Daniel Borkmann5e581da2018-01-26 23:33:38 +01005852 continue;
5853 }
5854
5855 /* Basic sanity check before we invest more work here. */
5856 if (!bpf_opcode_in_insntable(insn->code)) {
5857 verbose(env, "unknown opcode %02x\n", insn->code);
5858 return -EINVAL;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005859 }
5860 }
5861
5862 /* now all pseudo BPF_LD_IMM64 instructions load valid
5863 * 'struct bpf_map *' into a register instead of user map_fd.
5864 * These pointers will be used later by verifier to validate map access.
5865 */
5866 return 0;
5867}
5868
5869/* drop refcnt of maps used by the rejected program */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005870static void release_maps(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005871{
Roman Gushchin8bad74f2018-09-28 14:45:36 +00005872 enum bpf_cgroup_storage_type stype;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005873 int i;
5874
Roman Gushchin8bad74f2018-09-28 14:45:36 +00005875 for_each_cgroup_storage_type(stype) {
5876 if (!env->prog->aux->cgroup_storage[stype])
5877 continue;
Roman Gushchinde9cbba2018-08-02 14:27:18 -07005878 bpf_cgroup_storage_release(env->prog,
Roman Gushchin8bad74f2018-09-28 14:45:36 +00005879 env->prog->aux->cgroup_storage[stype]);
5880 }
Roman Gushchinde9cbba2018-08-02 14:27:18 -07005881
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005882 for (i = 0; i < env->used_map_cnt; i++)
5883 bpf_map_put(env->used_maps[i]);
5884}
5885
5886/* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005887static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005888{
5889 struct bpf_insn *insn = env->prog->insnsi;
5890 int insn_cnt = env->prog->len;
5891 int i;
5892
5893 for (i = 0; i < insn_cnt; i++, insn++)
5894 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
5895 insn->src_reg = 0;
5896}
5897
Alexei Starovoitov80419022017-03-15 18:26:41 -07005898/* single env->prog->insni[off] instruction was replaced with the range
5899 * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
5900 * [0, off) and [off, end) to new locations, so the patched range stays zero
5901 */
5902static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len,
5903 u32 off, u32 cnt)
5904{
5905 struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data;
Alexei Starovoitovc1311872017-11-22 16:42:05 -08005906 int i;
Alexei Starovoitov80419022017-03-15 18:26:41 -07005907
5908 if (cnt == 1)
5909 return 0;
Kees Cookfad953c2018-06-12 14:27:37 -07005910 new_data = vzalloc(array_size(prog_len,
5911 sizeof(struct bpf_insn_aux_data)));
Alexei Starovoitov80419022017-03-15 18:26:41 -07005912 if (!new_data)
5913 return -ENOMEM;
5914 memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off);
5915 memcpy(new_data + off + cnt - 1, old_data + off,
5916 sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
Alexei Starovoitovc1311872017-11-22 16:42:05 -08005917 for (i = off; i < off + cnt - 1; i++)
5918 new_data[i].seen = true;
Alexei Starovoitov80419022017-03-15 18:26:41 -07005919 env->insn_aux_data = new_data;
5920 vfree(old_data);
5921 return 0;
5922}
5923
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08005924static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len)
5925{
5926 int i;
5927
5928 if (len == 1)
5929 return;
Jiong Wang4cb3d992018-05-02 16:17:19 -04005930 /* NOTE: fake 'exit' subprog should be updated as well. */
5931 for (i = 0; i <= env->subprog_cnt; i++) {
Edward Creeafd59422018-11-16 12:00:07 +00005932 if (env->subprog_info[i].start <= off)
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08005933 continue;
Jiong Wang9c8105b2018-05-02 16:17:18 -04005934 env->subprog_info[i].start += len - 1;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08005935 }
5936}
5937
Alexei Starovoitov80419022017-03-15 18:26:41 -07005938static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
5939 const struct bpf_insn *patch, u32 len)
5940{
5941 struct bpf_prog *new_prog;
5942
5943 new_prog = bpf_patch_insn_single(env->prog, off, patch, len);
5944 if (!new_prog)
5945 return NULL;
5946 if (adjust_insn_aux_data(env, new_prog->len, off, len))
5947 return NULL;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08005948 adjust_subprog_starts(env, off, len);
Alexei Starovoitov80419022017-03-15 18:26:41 -07005949 return new_prog;
5950}
5951
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01005952/* The verifier does more data flow analysis than llvm and will not
5953 * explore branches that are dead at run time. Malicious programs can
5954 * have dead code too. Therefore replace all dead at-run-time code
5955 * with 'ja -1'.
5956 *
5957 * Just nops are not optimal, e.g. if they would sit at the end of the
5958 * program and through another bug we would manage to jump there, then
5959 * we'd execute beyond program memory otherwise. Returning exception
5960 * code also wouldn't work since we can have subprogs where the dead
5961 * code could be located.
Alexei Starovoitovc1311872017-11-22 16:42:05 -08005962 */
5963static void sanitize_dead_code(struct bpf_verifier_env *env)
5964{
5965 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01005966 struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1);
Alexei Starovoitovc1311872017-11-22 16:42:05 -08005967 struct bpf_insn *insn = env->prog->insnsi;
5968 const int insn_cnt = env->prog->len;
5969 int i;
5970
5971 for (i = 0; i < insn_cnt; i++) {
5972 if (aux_data[i].seen)
5973 continue;
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01005974 memcpy(insn + i, &trap, sizeof(trap));
Alexei Starovoitovc1311872017-11-22 16:42:05 -08005975 }
5976}
5977
Joe Stringerc64b7982018-10-02 13:35:33 -07005978/* convert load instructions that access fields of a context type into a
5979 * sequence of instructions that access fields of the underlying structure:
5980 * struct __sk_buff -> struct sk_buff
5981 * struct bpf_sock_ops -> struct sock
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005982 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005983static int convert_ctx_accesses(struct bpf_verifier_env *env)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005984{
Jakub Kicinski00176a32017-10-16 16:40:54 -07005985 const struct bpf_verifier_ops *ops = env->ops;
Daniel Borkmannf96da092017-07-02 02:13:27 +02005986 int i, cnt, size, ctx_field_size, delta = 0;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01005987 const int insn_cnt = env->prog->len;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02005988 struct bpf_insn insn_buf[16], *insn;
Andrey Ignatov46f53a62018-11-10 22:15:13 -08005989 u32 target_size, size_default, off;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005990 struct bpf_prog *new_prog;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07005991 enum bpf_access_type type;
Daniel Borkmannf96da092017-07-02 02:13:27 +02005992 bool is_narrower_load;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07005993
Daniel Borkmannb09928b2018-10-24 22:05:49 +02005994 if (ops->gen_prologue || env->seen_direct_write) {
5995 if (!ops->gen_prologue) {
5996 verbose(env, "bpf verifier is misconfigured\n");
5997 return -EINVAL;
5998 }
Daniel Borkmann36bbef52016-09-20 00:26:13 +02005999 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
6000 env->prog);
6001 if (cnt >= ARRAY_SIZE(insn_buf)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006002 verbose(env, "bpf verifier is misconfigured\n");
Daniel Borkmann36bbef52016-09-20 00:26:13 +02006003 return -EINVAL;
6004 } else if (cnt) {
Alexei Starovoitov80419022017-03-15 18:26:41 -07006005 new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
Daniel Borkmann36bbef52016-09-20 00:26:13 +02006006 if (!new_prog)
6007 return -ENOMEM;
Alexei Starovoitov80419022017-03-15 18:26:41 -07006008
Daniel Borkmann36bbef52016-09-20 00:26:13 +02006009 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006010 delta += cnt - 1;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02006011 }
6012 }
6013
Joe Stringerc64b7982018-10-02 13:35:33 -07006014 if (bpf_prog_is_dev_bound(env->prog->aux))
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006015 return 0;
6016
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006017 insn = env->prog->insnsi + delta;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02006018
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006019 for (i = 0; i < insn_cnt; i++, insn++) {
Joe Stringerc64b7982018-10-02 13:35:33 -07006020 bpf_convert_ctx_access_t convert_ctx_access;
6021
Daniel Borkmann62c79892017-01-12 11:51:33 +01006022 if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) ||
6023 insn->code == (BPF_LDX | BPF_MEM | BPF_H) ||
6024 insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07006025 insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006026 type = BPF_READ;
Daniel Borkmann62c79892017-01-12 11:51:33 +01006027 else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) ||
6028 insn->code == (BPF_STX | BPF_MEM | BPF_H) ||
6029 insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07006030 insn->code == (BPF_STX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006031 type = BPF_WRITE;
6032 else
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006033 continue;
6034
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07006035 if (type == BPF_WRITE &&
6036 env->insn_aux_data[i + delta].sanitize_stack_off) {
6037 struct bpf_insn patch[] = {
6038 /* Sanitize suspicious stack slot with zero.
6039 * There are no memory dependencies for this store,
6040 * since it's only using frame pointer and immediate
6041 * constant of zero
6042 */
6043 BPF_ST_MEM(BPF_DW, BPF_REG_FP,
6044 env->insn_aux_data[i + delta].sanitize_stack_off,
6045 0),
6046 /* the original STX instruction will immediately
6047 * overwrite the same stack slot with appropriate value
6048 */
6049 *insn,
6050 };
6051
6052 cnt = ARRAY_SIZE(patch);
6053 new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt);
6054 if (!new_prog)
6055 return -ENOMEM;
6056
6057 delta += cnt - 1;
6058 env->prog = new_prog;
6059 insn = new_prog->insnsi + i + delta;
6060 continue;
6061 }
6062
Joe Stringerc64b7982018-10-02 13:35:33 -07006063 switch (env->insn_aux_data[i + delta].ptr_type) {
6064 case PTR_TO_CTX:
6065 if (!ops->convert_ctx_access)
6066 continue;
6067 convert_ctx_access = ops->convert_ctx_access;
6068 break;
6069 case PTR_TO_SOCKET:
6070 convert_ctx_access = bpf_sock_convert_ctx_access;
6071 break;
6072 default:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006073 continue;
Joe Stringerc64b7982018-10-02 13:35:33 -07006074 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006075
Yonghong Song31fd8582017-06-13 15:52:13 -07006076 ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size;
Daniel Borkmannf96da092017-07-02 02:13:27 +02006077 size = BPF_LDST_BYTES(insn);
Yonghong Song31fd8582017-06-13 15:52:13 -07006078
6079 /* If the read access is a narrower load of the field,
6080 * convert to a 4/8-byte load, to minimum program type specific
6081 * convert_ctx_access changes. If conversion is successful,
6082 * we will apply proper mask to the result.
6083 */
Daniel Borkmannf96da092017-07-02 02:13:27 +02006084 is_narrower_load = size < ctx_field_size;
Andrey Ignatov46f53a62018-11-10 22:15:13 -08006085 size_default = bpf_ctx_off_adjust_machine(ctx_field_size);
6086 off = insn->off;
Yonghong Song31fd8582017-06-13 15:52:13 -07006087 if (is_narrower_load) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02006088 u8 size_code;
Yonghong Song31fd8582017-06-13 15:52:13 -07006089
Daniel Borkmannf96da092017-07-02 02:13:27 +02006090 if (type == BPF_WRITE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006091 verbose(env, "bpf verifier narrow ctx access misconfigured\n");
Daniel Borkmannf96da092017-07-02 02:13:27 +02006092 return -EINVAL;
6093 }
6094
6095 size_code = BPF_H;
Yonghong Song31fd8582017-06-13 15:52:13 -07006096 if (ctx_field_size == 4)
6097 size_code = BPF_W;
6098 else if (ctx_field_size == 8)
6099 size_code = BPF_DW;
Daniel Borkmannf96da092017-07-02 02:13:27 +02006100
Daniel Borkmannbc231052018-06-02 23:06:39 +02006101 insn->off = off & ~(size_default - 1);
Yonghong Song31fd8582017-06-13 15:52:13 -07006102 insn->code = BPF_LDX | BPF_MEM | size_code;
6103 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02006104
6105 target_size = 0;
Joe Stringerc64b7982018-10-02 13:35:33 -07006106 cnt = convert_ctx_access(type, insn, insn_buf, env->prog,
6107 &target_size);
Daniel Borkmannf96da092017-07-02 02:13:27 +02006108 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) ||
6109 (ctx_field_size && !target_size)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006110 verbose(env, "bpf verifier is misconfigured\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006111 return -EINVAL;
6112 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02006113
6114 if (is_narrower_load && size < target_size) {
Andrey Ignatov46f53a62018-11-10 22:15:13 -08006115 u8 shift = (off & (size_default - 1)) * 8;
6116
6117 if (ctx_field_size <= 4) {
6118 if (shift)
6119 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH,
6120 insn->dst_reg,
6121 shift);
Yonghong Song31fd8582017-06-13 15:52:13 -07006122 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02006123 (1 << size * 8) - 1);
Andrey Ignatov46f53a62018-11-10 22:15:13 -08006124 } else {
6125 if (shift)
6126 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH,
6127 insn->dst_reg,
6128 shift);
Yonghong Song31fd8582017-06-13 15:52:13 -07006129 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02006130 (1 << size * 8) - 1);
Andrey Ignatov46f53a62018-11-10 22:15:13 -08006131 }
Yonghong Song31fd8582017-06-13 15:52:13 -07006132 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006133
Alexei Starovoitov80419022017-03-15 18:26:41 -07006134 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006135 if (!new_prog)
6136 return -ENOMEM;
6137
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006138 delta += cnt - 1;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006139
6140 /* keep walking new program and skip insns we just inserted */
6141 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006142 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006143 }
6144
6145 return 0;
6146}
6147
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006148static int jit_subprogs(struct bpf_verifier_env *env)
6149{
6150 struct bpf_prog *prog = env->prog, **func, *tmp;
6151 int i, j, subprog_start, subprog_end = 0, len, subprog;
Daniel Borkmann7105e822017-12-20 13:42:57 +01006152 struct bpf_insn *insn;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006153 void *old_bpf_func;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08006154 int err;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006155
Jiong Wangf910cef2018-05-02 16:17:17 -04006156 if (env->subprog_cnt <= 1)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006157 return 0;
6158
Daniel Borkmann7105e822017-12-20 13:42:57 +01006159 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006160 if (insn->code != (BPF_JMP | BPF_CALL) ||
6161 insn->src_reg != BPF_PSEUDO_CALL)
6162 continue;
Daniel Borkmannc7a89782018-07-12 21:44:28 +02006163 /* Upon error here we cannot fall back to interpreter but
6164 * need a hard reject of the program. Thus -EFAULT is
6165 * propagated in any case.
6166 */
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006167 subprog = find_subprog(env, i + insn->imm + 1);
6168 if (subprog < 0) {
6169 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
6170 i + insn->imm + 1);
6171 return -EFAULT;
6172 }
6173 /* temporarily remember subprog id inside insn instead of
6174 * aux_data, since next loop will split up all insns into funcs
6175 */
Jiong Wangf910cef2018-05-02 16:17:17 -04006176 insn->off = subprog;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006177 /* remember original imm in case JIT fails and fallback
6178 * to interpreter will be needed
6179 */
6180 env->insn_aux_data[i].call_imm = insn->imm;
6181 /* point imm to __bpf_call_base+1 from JITs point of view */
6182 insn->imm = 1;
6183 }
6184
Martin KaFai Lauc454a462018-12-07 16:42:25 -08006185 err = bpf_prog_alloc_jited_linfo(prog);
6186 if (err)
6187 goto out_undo_insn;
6188
6189 err = -ENOMEM;
Kees Cook6396bb22018-06-12 14:03:40 -07006190 func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006191 if (!func)
Daniel Borkmannc7a89782018-07-12 21:44:28 +02006192 goto out_undo_insn;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006193
Jiong Wangf910cef2018-05-02 16:17:17 -04006194 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006195 subprog_start = subprog_end;
Jiong Wang4cb3d992018-05-02 16:17:19 -04006196 subprog_end = env->subprog_info[i + 1].start;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006197
6198 len = subprog_end - subprog_start;
6199 func[i] = bpf_prog_alloc(bpf_prog_size(len), GFP_USER);
6200 if (!func[i])
6201 goto out_free;
6202 memcpy(func[i]->insnsi, &prog->insnsi[subprog_start],
6203 len * sizeof(struct bpf_insn));
Daniel Borkmann4f74d802017-12-20 13:42:56 +01006204 func[i]->type = prog->type;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006205 func[i]->len = len;
Daniel Borkmann4f74d802017-12-20 13:42:56 +01006206 if (bpf_prog_calc_tag(func[i]))
6207 goto out_free;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006208 func[i]->is_func = 1;
Yonghong Songba64e7d2018-11-24 23:20:44 -08006209 func[i]->aux->func_idx = i;
6210 /* the btf and func_info will be freed only at prog->aux */
6211 func[i]->aux->btf = prog->aux->btf;
6212 func[i]->aux->func_info = prog->aux->func_info;
6213
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006214 /* Use bpf_prog_F_tag to indicate functions in stack traces.
6215 * Long term would need debug info to populate names
6216 */
6217 func[i]->aux->name[0] = 'F';
Jiong Wang9c8105b2018-05-02 16:17:18 -04006218 func[i]->aux->stack_depth = env->subprog_info[i].stack_depth;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006219 func[i]->jit_requested = 1;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08006220 func[i]->aux->linfo = prog->aux->linfo;
6221 func[i]->aux->nr_linfo = prog->aux->nr_linfo;
6222 func[i]->aux->jited_linfo = prog->aux->jited_linfo;
6223 func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006224 func[i] = bpf_int_jit_compile(func[i]);
6225 if (!func[i]->jited) {
6226 err = -ENOTSUPP;
6227 goto out_free;
6228 }
6229 cond_resched();
6230 }
6231 /* at this point all bpf functions were successfully JITed
6232 * now populate all bpf_calls with correct addresses and
6233 * run last pass of JIT
6234 */
Jiong Wangf910cef2018-05-02 16:17:17 -04006235 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006236 insn = func[i]->insnsi;
6237 for (j = 0; j < func[i]->len; j++, insn++) {
6238 if (insn->code != (BPF_JMP | BPF_CALL) ||
6239 insn->src_reg != BPF_PSEUDO_CALL)
6240 continue;
6241 subprog = insn->off;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006242 insn->imm = (u64 (*)(u64, u64, u64, u64, u64))
6243 func[subprog]->bpf_func -
6244 __bpf_call_base;
6245 }
Sandipan Das2162fed2018-05-24 12:26:45 +05306246
6247 /* we use the aux data to keep a list of the start addresses
6248 * of the JITed images for each function in the program
6249 *
6250 * for some architectures, such as powerpc64, the imm field
6251 * might not be large enough to hold the offset of the start
6252 * address of the callee's JITed image from __bpf_call_base
6253 *
6254 * in such cases, we can lookup the start address of a callee
6255 * by using its subprog id, available from the off field of
6256 * the call instruction, as an index for this list
6257 */
6258 func[i]->aux->func = func;
6259 func[i]->aux->func_cnt = env->subprog_cnt;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006260 }
Jiong Wangf910cef2018-05-02 16:17:17 -04006261 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006262 old_bpf_func = func[i]->bpf_func;
6263 tmp = bpf_int_jit_compile(func[i]);
6264 if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) {
6265 verbose(env, "JIT doesn't support bpf-to-bpf calls\n");
Daniel Borkmannc7a89782018-07-12 21:44:28 +02006266 err = -ENOTSUPP;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006267 goto out_free;
6268 }
6269 cond_resched();
6270 }
6271
6272 /* finally lock prog and jit images for all functions and
6273 * populate kallsysm
6274 */
Jiong Wangf910cef2018-05-02 16:17:17 -04006275 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006276 bpf_prog_lock_ro(func[i]);
6277 bpf_prog_kallsyms_add(func[i]);
6278 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01006279
6280 /* Last step: make now unused interpreter insns from main
6281 * prog consistent for later dump requests, so they can
6282 * later look the same as if they were interpreted only.
6283 */
6284 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01006285 if (insn->code != (BPF_JMP | BPF_CALL) ||
6286 insn->src_reg != BPF_PSEUDO_CALL)
6287 continue;
6288 insn->off = env->insn_aux_data[i].call_imm;
6289 subprog = find_subprog(env, i + insn->off + 1);
Sandipan Dasdbecd732018-05-24 12:26:48 +05306290 insn->imm = subprog;
Daniel Borkmann7105e822017-12-20 13:42:57 +01006291 }
6292
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006293 prog->jited = 1;
6294 prog->bpf_func = func[0]->bpf_func;
6295 prog->aux->func = func;
Jiong Wangf910cef2018-05-02 16:17:17 -04006296 prog->aux->func_cnt = env->subprog_cnt;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08006297 bpf_prog_free_unused_jited_linfo(prog);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006298 return 0;
6299out_free:
Jiong Wangf910cef2018-05-02 16:17:17 -04006300 for (i = 0; i < env->subprog_cnt; i++)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006301 if (func[i])
6302 bpf_jit_free(func[i]);
6303 kfree(func);
Daniel Borkmannc7a89782018-07-12 21:44:28 +02006304out_undo_insn:
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006305 /* cleanup main prog to be interpreted */
6306 prog->jit_requested = 0;
6307 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
6308 if (insn->code != (BPF_JMP | BPF_CALL) ||
6309 insn->src_reg != BPF_PSEUDO_CALL)
6310 continue;
6311 insn->off = 0;
6312 insn->imm = env->insn_aux_data[i].call_imm;
6313 }
Martin KaFai Lauc454a462018-12-07 16:42:25 -08006314 bpf_prog_free_jited_linfo(prog);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006315 return err;
6316}
6317
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08006318static int fixup_call_args(struct bpf_verifier_env *env)
6319{
David S. Miller19d28fb2018-01-11 21:27:54 -05006320#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08006321 struct bpf_prog *prog = env->prog;
6322 struct bpf_insn *insn = prog->insnsi;
6323 int i, depth;
David S. Miller19d28fb2018-01-11 21:27:54 -05006324#endif
Quentin Monnete4052d02018-10-07 12:56:58 +01006325 int err = 0;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08006326
Quentin Monnete4052d02018-10-07 12:56:58 +01006327 if (env->prog->jit_requested &&
6328 !bpf_prog_is_dev_bound(env->prog->aux)) {
David S. Miller19d28fb2018-01-11 21:27:54 -05006329 err = jit_subprogs(env);
6330 if (err == 0)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08006331 return 0;
Daniel Borkmannc7a89782018-07-12 21:44:28 +02006332 if (err == -EFAULT)
6333 return err;
David S. Miller19d28fb2018-01-11 21:27:54 -05006334 }
6335#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08006336 for (i = 0; i < prog->len; i++, insn++) {
6337 if (insn->code != (BPF_JMP | BPF_CALL) ||
6338 insn->src_reg != BPF_PSEUDO_CALL)
6339 continue;
6340 depth = get_callee_stack_depth(env, insn, i);
6341 if (depth < 0)
6342 return depth;
6343 bpf_patch_call_args(insn, depth);
6344 }
David S. Miller19d28fb2018-01-11 21:27:54 -05006345 err = 0;
6346#endif
6347 return err;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08006348}
6349
Alexei Starovoitov79741b32017-03-15 18:26:40 -07006350/* fixup insn->imm field of bpf_call instructions
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07006351 * and inline eligible helpers as explicit sequence of BPF instructions
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07006352 *
6353 * this function is called after eBPF program passed verification
6354 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07006355static int fixup_bpf_calls(struct bpf_verifier_env *env)
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07006356{
Alexei Starovoitov79741b32017-03-15 18:26:40 -07006357 struct bpf_prog *prog = env->prog;
6358 struct bpf_insn *insn = prog->insnsi;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07006359 const struct bpf_func_proto *fn;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07006360 const int insn_cnt = prog->len;
Daniel Borkmann09772d92018-06-02 23:06:35 +02006361 const struct bpf_map_ops *ops;
Daniel Borkmannc93552c2018-05-24 02:32:53 +02006362 struct bpf_insn_aux_data *aux;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07006363 struct bpf_insn insn_buf[16];
6364 struct bpf_prog *new_prog;
6365 struct bpf_map *map_ptr;
6366 int i, cnt, delta = 0;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07006367
Alexei Starovoitov79741b32017-03-15 18:26:40 -07006368 for (i = 0; i < insn_cnt; i++, insn++) {
Daniel Borkmannf6b1b3b2018-01-26 23:33:39 +01006369 if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) ||
6370 insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) ||
6371 insn->code == (BPF_ALU | BPF_MOD | BPF_X) ||
Alexei Starovoitov68fda452018-01-12 18:59:52 -08006372 insn->code == (BPF_ALU | BPF_DIV | BPF_X)) {
Daniel Borkmannf6b1b3b2018-01-26 23:33:39 +01006373 bool is64 = BPF_CLASS(insn->code) == BPF_ALU64;
6374 struct bpf_insn mask_and_div[] = {
6375 BPF_MOV32_REG(insn->src_reg, insn->src_reg),
6376 /* Rx div 0 -> 0 */
6377 BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2),
6378 BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg),
6379 BPF_JMP_IMM(BPF_JA, 0, 0, 1),
6380 *insn,
6381 };
6382 struct bpf_insn mask_and_mod[] = {
6383 BPF_MOV32_REG(insn->src_reg, insn->src_reg),
6384 /* Rx mod 0 -> Rx */
6385 BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1),
6386 *insn,
6387 };
6388 struct bpf_insn *patchlet;
6389
6390 if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) ||
6391 insn->code == (BPF_ALU | BPF_DIV | BPF_X)) {
6392 patchlet = mask_and_div + (is64 ? 1 : 0);
6393 cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0);
6394 } else {
6395 patchlet = mask_and_mod + (is64 ? 1 : 0);
6396 cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0);
6397 }
6398
6399 new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt);
Alexei Starovoitov68fda452018-01-12 18:59:52 -08006400 if (!new_prog)
6401 return -ENOMEM;
6402
6403 delta += cnt - 1;
6404 env->prog = prog = new_prog;
6405 insn = new_prog->insnsi + i + delta;
6406 continue;
6407 }
6408
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +02006409 if (BPF_CLASS(insn->code) == BPF_LD &&
6410 (BPF_MODE(insn->code) == BPF_ABS ||
6411 BPF_MODE(insn->code) == BPF_IND)) {
6412 cnt = env->ops->gen_ld_abs(insn, insn_buf);
6413 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
6414 verbose(env, "bpf verifier is misconfigured\n");
6415 return -EINVAL;
6416 }
6417
6418 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
6419 if (!new_prog)
6420 return -ENOMEM;
6421
6422 delta += cnt - 1;
6423 env->prog = prog = new_prog;
6424 insn = new_prog->insnsi + i + delta;
6425 continue;
6426 }
6427
Alexei Starovoitov79741b32017-03-15 18:26:40 -07006428 if (insn->code != (BPF_JMP | BPF_CALL))
6429 continue;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08006430 if (insn->src_reg == BPF_PSEUDO_CALL)
6431 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07006432
Alexei Starovoitov79741b32017-03-15 18:26:40 -07006433 if (insn->imm == BPF_FUNC_get_route_realm)
6434 prog->dst_needed = 1;
6435 if (insn->imm == BPF_FUNC_get_prandom_u32)
6436 bpf_user_rnd_init_once();
Josef Bacik9802d862017-12-11 11:36:48 -05006437 if (insn->imm == BPF_FUNC_override_return)
6438 prog->kprobe_override = 1;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07006439 if (insn->imm == BPF_FUNC_tail_call) {
David S. Miller7b9f6da2017-04-20 10:35:33 -04006440 /* If we tail call into other programs, we
6441 * cannot make any assumptions since they can
6442 * be replaced dynamically during runtime in
6443 * the program array.
6444 */
6445 prog->cb_access = 1;
Alexei Starovoitov80a58d02017-05-30 13:31:30 -07006446 env->prog->aux->stack_depth = MAX_BPF_STACK;
Jiong Wange6478152018-11-08 04:08:42 -05006447 env->prog->aux->max_pkt_offset = MAX_PACKET_OFF;
David S. Miller7b9f6da2017-04-20 10:35:33 -04006448
Alexei Starovoitov79741b32017-03-15 18:26:40 -07006449 /* mark bpf_tail_call as different opcode to avoid
6450 * conditional branch in the interpeter for every normal
6451 * call and to prevent accidental JITing by JIT compiler
6452 * that doesn't support bpf_tail_call yet
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07006453 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07006454 insn->imm = 0;
Alexei Starovoitov71189fa2017-05-30 13:31:27 -07006455 insn->code = BPF_JMP | BPF_TAIL_CALL;
Alexei Starovoitovb2157392018-01-07 17:33:02 -08006456
Daniel Borkmannc93552c2018-05-24 02:32:53 +02006457 aux = &env->insn_aux_data[i + delta];
6458 if (!bpf_map_ptr_unpriv(aux))
6459 continue;
6460
Alexei Starovoitovb2157392018-01-07 17:33:02 -08006461 /* instead of changing every JIT dealing with tail_call
6462 * emit two extra insns:
6463 * if (index >= max_entries) goto out;
6464 * index &= array->index_mask;
6465 * to avoid out-of-bounds cpu speculation
6466 */
Daniel Borkmannc93552c2018-05-24 02:32:53 +02006467 if (bpf_map_ptr_poisoned(aux)) {
Colin Ian King40950342018-01-10 09:20:54 +00006468 verbose(env, "tail_call abusing map_ptr\n");
Alexei Starovoitovb2157392018-01-07 17:33:02 -08006469 return -EINVAL;
6470 }
Daniel Borkmannc93552c2018-05-24 02:32:53 +02006471
6472 map_ptr = BPF_MAP_PTR(aux->map_state);
Alexei Starovoitovb2157392018-01-07 17:33:02 -08006473 insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3,
6474 map_ptr->max_entries, 2);
6475 insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3,
6476 container_of(map_ptr,
6477 struct bpf_array,
6478 map)->index_mask);
6479 insn_buf[2] = *insn;
6480 cnt = 3;
6481 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
6482 if (!new_prog)
6483 return -ENOMEM;
6484
6485 delta += cnt - 1;
6486 env->prog = prog = new_prog;
6487 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07006488 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07006489 }
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07006490
Daniel Borkmann89c63072017-08-19 03:12:45 +02006491 /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup
Daniel Borkmann09772d92018-06-02 23:06:35 +02006492 * and other inlining handlers are currently limited to 64 bit
6493 * only.
Daniel Borkmann89c63072017-08-19 03:12:45 +02006494 */
Alexei Starovoitov60b58afc2017-12-14 17:55:14 -08006495 if (prog->jit_requested && BITS_PER_LONG == 64 &&
Daniel Borkmann09772d92018-06-02 23:06:35 +02006496 (insn->imm == BPF_FUNC_map_lookup_elem ||
6497 insn->imm == BPF_FUNC_map_update_elem ||
Daniel Borkmann84430d42018-10-21 02:09:27 +02006498 insn->imm == BPF_FUNC_map_delete_elem ||
6499 insn->imm == BPF_FUNC_map_push_elem ||
6500 insn->imm == BPF_FUNC_map_pop_elem ||
6501 insn->imm == BPF_FUNC_map_peek_elem)) {
Daniel Borkmannc93552c2018-05-24 02:32:53 +02006502 aux = &env->insn_aux_data[i + delta];
6503 if (bpf_map_ptr_poisoned(aux))
6504 goto patch_call_imm;
6505
6506 map_ptr = BPF_MAP_PTR(aux->map_state);
Daniel Borkmann09772d92018-06-02 23:06:35 +02006507 ops = map_ptr->ops;
6508 if (insn->imm == BPF_FUNC_map_lookup_elem &&
6509 ops->map_gen_lookup) {
6510 cnt = ops->map_gen_lookup(map_ptr, insn_buf);
6511 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
6512 verbose(env, "bpf verifier is misconfigured\n");
6513 return -EINVAL;
6514 }
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07006515
Daniel Borkmann09772d92018-06-02 23:06:35 +02006516 new_prog = bpf_patch_insn_data(env, i + delta,
6517 insn_buf, cnt);
6518 if (!new_prog)
6519 return -ENOMEM;
6520
6521 delta += cnt - 1;
6522 env->prog = prog = new_prog;
6523 insn = new_prog->insnsi + i + delta;
6524 continue;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07006525 }
6526
Daniel Borkmann09772d92018-06-02 23:06:35 +02006527 BUILD_BUG_ON(!__same_type(ops->map_lookup_elem,
6528 (void *(*)(struct bpf_map *map, void *key))NULL));
6529 BUILD_BUG_ON(!__same_type(ops->map_delete_elem,
6530 (int (*)(struct bpf_map *map, void *key))NULL));
6531 BUILD_BUG_ON(!__same_type(ops->map_update_elem,
6532 (int (*)(struct bpf_map *map, void *key, void *value,
6533 u64 flags))NULL));
Daniel Borkmann84430d42018-10-21 02:09:27 +02006534 BUILD_BUG_ON(!__same_type(ops->map_push_elem,
6535 (int (*)(struct bpf_map *map, void *value,
6536 u64 flags))NULL));
6537 BUILD_BUG_ON(!__same_type(ops->map_pop_elem,
6538 (int (*)(struct bpf_map *map, void *value))NULL));
6539 BUILD_BUG_ON(!__same_type(ops->map_peek_elem,
6540 (int (*)(struct bpf_map *map, void *value))NULL));
6541
Daniel Borkmann09772d92018-06-02 23:06:35 +02006542 switch (insn->imm) {
6543 case BPF_FUNC_map_lookup_elem:
6544 insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) -
6545 __bpf_call_base;
6546 continue;
6547 case BPF_FUNC_map_update_elem:
6548 insn->imm = BPF_CAST_CALL(ops->map_update_elem) -
6549 __bpf_call_base;
6550 continue;
6551 case BPF_FUNC_map_delete_elem:
6552 insn->imm = BPF_CAST_CALL(ops->map_delete_elem) -
6553 __bpf_call_base;
6554 continue;
Daniel Borkmann84430d42018-10-21 02:09:27 +02006555 case BPF_FUNC_map_push_elem:
6556 insn->imm = BPF_CAST_CALL(ops->map_push_elem) -
6557 __bpf_call_base;
6558 continue;
6559 case BPF_FUNC_map_pop_elem:
6560 insn->imm = BPF_CAST_CALL(ops->map_pop_elem) -
6561 __bpf_call_base;
6562 continue;
6563 case BPF_FUNC_map_peek_elem:
6564 insn->imm = BPF_CAST_CALL(ops->map_peek_elem) -
6565 __bpf_call_base;
6566 continue;
Daniel Borkmann09772d92018-06-02 23:06:35 +02006567 }
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07006568
Daniel Borkmann09772d92018-06-02 23:06:35 +02006569 goto patch_call_imm;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07006570 }
6571
6572patch_call_imm:
Andrey Ignatov5e43f892018-03-30 15:08:00 -07006573 fn = env->ops->get_func_proto(insn->imm, env->prog);
Alexei Starovoitov79741b32017-03-15 18:26:40 -07006574 /* all functions that have prototype and verifier allowed
6575 * programs to call them, must be real in-kernel functions
6576 */
6577 if (!fn->func) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006578 verbose(env,
6579 "kernel subsystem misconfigured func %s#%d\n",
Alexei Starovoitov79741b32017-03-15 18:26:40 -07006580 func_id_name(insn->imm), insn->imm);
6581 return -EFAULT;
6582 }
6583 insn->imm = fn->func - __bpf_call_base;
6584 }
6585
6586 return 0;
6587}
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07006588
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006589static void free_states(struct bpf_verifier_env *env)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006590{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006591 struct bpf_verifier_state_list *sl, *sln;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006592 int i;
6593
6594 if (!env->explored_states)
6595 return;
6596
6597 for (i = 0; i < env->prog->len; i++) {
6598 sl = env->explored_states[i];
6599
6600 if (sl)
6601 while (sl != STATE_LIST_MARK) {
6602 sln = sl->next;
Alexei Starovoitov1969db42017-11-01 00:08:04 -07006603 free_verifier_state(&sl->state, false);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006604 kfree(sl);
6605 sl = sln;
6606 }
6607 }
6608
6609 kfree(env->explored_states);
6610}
6611
Yonghong Song838e9692018-11-19 15:29:11 -08006612int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,
6613 union bpf_attr __user *uattr)
Alexei Starovoitov51580e72014-09-26 00:17:02 -07006614{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006615 struct bpf_verifier_env *env;
Martin KaFai Laub9193c12018-03-24 11:44:22 -07006616 struct bpf_verifier_log *log;
Alexei Starovoitov51580e72014-09-26 00:17:02 -07006617 int ret = -EINVAL;
6618
Arnd Bergmanneba0c922017-11-02 12:05:52 +01006619 /* no program is valid */
6620 if (ARRAY_SIZE(bpf_verifier_ops) == 0)
6621 return -EINVAL;
6622
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006623 /* 'struct bpf_verifier_env' can be global, but since it's not small,
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07006624 * allocate/free it every time bpf_check() is called
6625 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006626 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07006627 if (!env)
6628 return -ENOMEM;
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006629 log = &env->log;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07006630
Kees Cookfad953c2018-06-12 14:27:37 -07006631 env->insn_aux_data =
6632 vzalloc(array_size(sizeof(struct bpf_insn_aux_data),
6633 (*prog)->len));
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006634 ret = -ENOMEM;
6635 if (!env->insn_aux_data)
6636 goto err_free_env;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006637 env->prog = *prog;
Jakub Kicinski00176a32017-10-16 16:40:54 -07006638 env->ops = bpf_verifier_ops[env->prog->type];
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006639
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07006640 /* grab the mutex to protect few globals used by verifier */
6641 mutex_lock(&bpf_verifier_lock);
6642
6643 if (attr->log_level || attr->log_buf || attr->log_size) {
6644 /* user requested verbose verifier output
6645 * and supplied buffer to store the verification trace
6646 */
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07006647 log->level = attr->log_level;
6648 log->ubuf = (char __user *) (unsigned long) attr->log_buf;
6649 log->len_total = attr->log_size;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07006650
6651 ret = -EINVAL;
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07006652 /* log attributes have to be sane */
6653 if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 ||
6654 !log->level || !log->ubuf)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006655 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07006656 }
Daniel Borkmann1ad2f582017-05-25 01:05:05 +02006657
6658 env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT);
6659 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
David S. Millere07b98d2017-05-10 11:38:07 -07006660 env->strict_alignment = true;
David Millere9ee9ef2018-11-30 21:08:14 -08006661 if (attr->prog_flags & BPF_F_ANY_ALIGNMENT)
6662 env->strict_alignment = false;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07006663
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006664 ret = replace_map_fd_with_map_ptr(env);
6665 if (ret < 0)
6666 goto skip_full_check;
6667
Jakub Kicinskif4e3ec02018-05-03 18:37:11 -07006668 if (bpf_prog_is_dev_bound(env->prog->aux)) {
Quentin Monneta40a2632018-11-09 13:03:31 +00006669 ret = bpf_prog_offload_verifier_prep(env->prog);
Jakub Kicinskif4e3ec02018-05-03 18:37:11 -07006670 if (ret)
6671 goto skip_full_check;
6672 }
6673
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006674 env->explored_states = kcalloc(env->prog->len,
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006675 sizeof(struct bpf_verifier_state_list *),
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006676 GFP_USER);
6677 ret = -ENOMEM;
6678 if (!env->explored_states)
6679 goto skip_full_check;
6680
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08006681 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
6682
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006683 ret = check_cfg(env);
6684 if (ret < 0)
6685 goto skip_full_check;
6686
Martin KaFai Lauc454a462018-12-07 16:42:25 -08006687 ret = check_btf_info(env, attr, uattr);
Yonghong Song838e9692018-11-19 15:29:11 -08006688 if (ret < 0)
6689 goto skip_full_check;
6690
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006691 ret = do_check(env);
Craig Gallek8c01c4f2017-11-02 11:18:01 -04006692 if (env->cur_state) {
6693 free_verifier_state(env->cur_state, true);
6694 env->cur_state = NULL;
6695 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07006696
Quentin Monnetc941ce92018-10-07 12:56:47 +01006697 if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux))
6698 ret = bpf_prog_offload_finalize(env);
6699
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006700skip_full_check:
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006701 while (!pop_stack(env, NULL, NULL));
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006702 free_states(env);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006703
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006704 if (ret == 0)
Alexei Starovoitovc1311872017-11-22 16:42:05 -08006705 sanitize_dead_code(env);
6706
6707 if (ret == 0)
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08006708 ret = check_max_stack_depth(env);
6709
6710 if (ret == 0)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006711 /* program is valid, convert *(u32*)(ctx + off) accesses */
6712 ret = convert_ctx_accesses(env);
6713
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07006714 if (ret == 0)
Alexei Starovoitov79741b32017-03-15 18:26:40 -07006715 ret = fixup_bpf_calls(env);
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07006716
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08006717 if (ret == 0)
6718 ret = fixup_call_args(env);
6719
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07006720 if (log->level && bpf_verifier_log_full(log))
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07006721 ret = -ENOSPC;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07006722 if (log->level && !log->ubuf) {
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07006723 ret = -EFAULT;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07006724 goto err_release_maps;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07006725 }
6726
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006727 if (ret == 0 && env->used_map_cnt) {
6728 /* if program passed verifier, update used_maps in bpf_prog_info */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006729 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
6730 sizeof(env->used_maps[0]),
6731 GFP_KERNEL);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006732
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006733 if (!env->prog->aux->used_maps) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006734 ret = -ENOMEM;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07006735 goto err_release_maps;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006736 }
6737
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006738 memcpy(env->prog->aux->used_maps, env->used_maps,
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006739 sizeof(env->used_maps[0]) * env->used_map_cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006740 env->prog->aux->used_map_cnt = env->used_map_cnt;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006741
6742 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
6743 * bpf_ld_imm64 instructions
6744 */
6745 convert_pseudo_ld_imm64(env);
6746 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07006747
Yonghong Songba64e7d2018-11-24 23:20:44 -08006748 if (ret == 0)
6749 adjust_btf_func(env);
6750
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07006751err_release_maps:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006752 if (!env->prog->aux->used_maps)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006753 /* if we didn't copy map pointers into bpf_prog_info, release
Jakub Kicinskiab7f5bf2018-05-03 18:37:17 -07006754 * them now. Otherwise free_used_maps() will release them.
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006755 */
6756 release_maps(env);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006757 *prog = env->prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006758err_unlock:
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07006759 mutex_unlock(&bpf_verifier_lock);
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006760 vfree(env->insn_aux_data);
6761err_free_env:
6762 kfree(env);
Alexei Starovoitov51580e72014-09-26 00:17:02 -07006763 return ret;
6764}