blob: d15cc4fafa89b5f7e144307350b4668b80c598f4 [file] [log] [blame]
Thomas Gleixner5b497af2019-05-29 07:18:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Alexei Starovoitov51580e72014-09-26 00:17:02 -07002/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003 * Copyright (c) 2016 Facebook
Joe Stringerfd978bf72018-10-02 13:35:35 -07004 * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io
Alexei Starovoitov51580e72014-09-26 00:17:02 -07005 */
Yonghong Song838e9692018-11-19 15:29:11 -08006#include <uapi/linux/btf.h>
Alexei Starovoitov51580e72014-09-26 00:17:02 -07007#include <linux/kernel.h>
8#include <linux/types.h>
9#include <linux/slab.h>
10#include <linux/bpf.h>
Yonghong Song838e9692018-11-19 15:29:11 -080011#include <linux/btf.h>
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +010012#include <linux/bpf_verifier.h>
Alexei Starovoitov51580e72014-09-26 00:17:02 -070013#include <linux/filter.h>
14#include <net/netlink.h>
15#include <linux/file.h>
16#include <linux/vmalloc.h>
Thomas Grafebb676d2016-10-27 11:23:51 +020017#include <linux/stringify.h>
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -080018#include <linux/bsearch.h>
19#include <linux/sort.h>
Yonghong Songc195651e2018-04-28 22:28:08 -070020#include <linux/perf_event.h>
Martin KaFai Laud9762e82018-12-13 10:41:48 -080021#include <linux/ctype.h>
Alexei Starovoitov51580e72014-09-26 00:17:02 -070022
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -070023#include "disasm.h"
24
Jakub Kicinski00176a32017-10-16 16:40:54 -070025static const struct bpf_verifier_ops * const bpf_verifier_ops[] = {
26#define BPF_PROG_TYPE(_id, _name) \
27 [_id] = & _name ## _verifier_ops,
28#define BPF_MAP_TYPE(_id, _ops)
29#include <linux/bpf_types.h>
30#undef BPF_PROG_TYPE
31#undef BPF_MAP_TYPE
32};
33
Alexei Starovoitov51580e72014-09-26 00:17:02 -070034/* bpf_check() is a static code analyzer that walks eBPF program
35 * instruction by instruction and updates register/stack state.
36 * All paths of conditional branches are analyzed until 'bpf_exit' insn.
37 *
38 * The first pass is depth-first-search to check that the program is a DAG.
39 * It rejects the following programs:
40 * - larger than BPF_MAXINSNS insns
41 * - if loop is present (detected via back-edge)
42 * - unreachable insns exist (shouldn't be a forest. program = one function)
43 * - out of bounds or malformed jumps
44 * The second pass is all possible path descent from the 1st insn.
45 * Since it's analyzing all pathes through the program, the length of the
Gary Lineba38a92017-03-01 16:25:51 +080046 * analysis is limited to 64k insn, which may be hit even if total number of
Alexei Starovoitov51580e72014-09-26 00:17:02 -070047 * insn is less then 4K, but there are too many branches that change stack/regs.
48 * Number of 'branches to be analyzed' is limited to 1k
49 *
50 * On entry to each instruction, each register has a type, and the instruction
51 * changes the types of the registers depending on instruction semantics.
52 * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is
53 * copied to R1.
54 *
55 * All registers are 64-bit.
56 * R0 - return register
57 * R1-R5 argument passing registers
58 * R6-R9 callee saved registers
59 * R10 - frame pointer read-only
60 *
61 * At the start of BPF program the register R1 contains a pointer to bpf_context
62 * and has type PTR_TO_CTX.
63 *
64 * Verifier tracks arithmetic operations on pointers in case:
65 * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
66 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20),
67 * 1st insn copies R10 (which has FRAME_PTR) type into R1
68 * and 2nd arithmetic instruction is pattern matched to recognize
69 * that it wants to construct a pointer to some element within stack.
70 * So after 2nd insn, the register R1 has type PTR_TO_STACK
71 * (and -20 constant is saved for further stack bounds checking).
72 * Meaning that this reg is a pointer to stack plus known immediate constant.
73 *
Edward Creef1174f72017-08-07 15:26:19 +010074 * Most of the time the registers have SCALAR_VALUE type, which
Alexei Starovoitov51580e72014-09-26 00:17:02 -070075 * means the register has some value, but it's not a valid pointer.
Edward Creef1174f72017-08-07 15:26:19 +010076 * (like pointer plus pointer becomes SCALAR_VALUE type)
Alexei Starovoitov51580e72014-09-26 00:17:02 -070077 *
78 * When verifier sees load or store instructions the type of base register
Joe Stringerc64b7982018-10-02 13:35:33 -070079 * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are
80 * four pointer types recognized by check_mem_access() function.
Alexei Starovoitov51580e72014-09-26 00:17:02 -070081 *
82 * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value'
83 * and the range of [ptr, ptr + map's value_size) is accessible.
84 *
85 * registers used to pass values to function calls are checked against
86 * function argument constraints.
87 *
88 * ARG_PTR_TO_MAP_KEY is one of such argument constraints.
89 * It means that the register type passed to this function must be
90 * PTR_TO_STACK and it will be used inside the function as
91 * 'pointer to map element key'
92 *
93 * For example the argument constraints for bpf_map_lookup_elem():
94 * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
95 * .arg1_type = ARG_CONST_MAP_PTR,
96 * .arg2_type = ARG_PTR_TO_MAP_KEY,
97 *
98 * ret_type says that this function returns 'pointer to map elem value or null'
99 * function expects 1st argument to be a const pointer to 'struct bpf_map' and
100 * 2nd argument should be a pointer to stack, which will be used inside
101 * the helper function as a pointer to map element key.
102 *
103 * On the kernel side the helper function looks like:
104 * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
105 * {
106 * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
107 * void *key = (void *) (unsigned long) r2;
108 * void *value;
109 *
110 * here kernel can access 'key' and 'map' pointers safely, knowing that
111 * [key, key + map->key_size) bytes are valid and were initialized on
112 * the stack of eBPF program.
113 * }
114 *
115 * Corresponding eBPF program may look like:
116 * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR
117 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK
118 * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP
119 * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
120 * here verifier looks at prototype of map_lookup_elem() and sees:
121 * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok,
122 * Now verifier knows that this map has key of R1->map_ptr->key_size bytes
123 *
124 * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far,
125 * Now verifier checks that [R2, R2 + map's key_size) are within stack limits
126 * and were initialized prior to this call.
127 * If it's ok, then verifier allows this BPF_CALL insn and looks at
128 * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets
129 * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function
130 * returns ether pointer to map value or NULL.
131 *
132 * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off'
133 * insn, the register holding that pointer in the true branch changes state to
134 * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false
135 * branch. See check_cond_jmp_op().
136 *
137 * After the call R0 is set to return type of the function and registers R1-R5
138 * are set to NOT_INIT to indicate that they are no longer readable.
Joe Stringerfd978bf72018-10-02 13:35:35 -0700139 *
140 * The following reference types represent a potential reference to a kernel
141 * resource which, after first being allocated, must be checked and freed by
142 * the BPF program:
143 * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET
144 *
145 * When the verifier sees a helper call return a reference type, it allocates a
146 * pointer id for the reference and stores it in the current function state.
147 * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into
148 * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type
149 * passes through a NULL-check conditional. For the branch wherein the state is
150 * changed to CONST_IMM, the verifier releases the reference.
Joe Stringer6acc9b42018-10-02 13:35:36 -0700151 *
152 * For each helper function that allocates a reference, such as
153 * bpf_sk_lookup_tcp(), there is a corresponding release function, such as
154 * bpf_sk_release(). When a reference type passes into the release function,
155 * the verifier also releases the reference. If any unchecked or unreleased
156 * reference remains at the end of the program, the verifier rejects it.
Alexei Starovoitov51580e72014-09-26 00:17:02 -0700157 */
158
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700159/* verifier_state + insn_idx are pushed to stack when branch is encountered */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100160struct bpf_verifier_stack_elem {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700161 /* verifer state is 'st'
162 * before processing instruction 'insn_idx'
163 * and after processing instruction 'prev_insn_idx'
164 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100165 struct bpf_verifier_state st;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700166 int insn_idx;
167 int prev_insn_idx;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100168 struct bpf_verifier_stack_elem *next;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700169};
170
Daniel Borkmann07016152016-04-05 22:33:17 +0200171#define BPF_COMPLEXITY_LIMIT_STACK 1024
Alexei Starovoitovceefbc92018-12-03 22:46:06 -0800172#define BPF_COMPLEXITY_LIMIT_STATES 64
Daniel Borkmann07016152016-04-05 22:33:17 +0200173
Daniel Borkmannc93552c2018-05-24 02:32:53 +0200174#define BPF_MAP_PTR_UNPRIV 1UL
175#define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \
176 POISON_POINTER_DELTA))
177#define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV))
178
179static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux)
180{
181 return BPF_MAP_PTR(aux->map_state) == BPF_MAP_PTR_POISON;
182}
183
184static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux)
185{
186 return aux->map_state & BPF_MAP_PTR_UNPRIV;
187}
188
189static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux,
190 const struct bpf_map *map, bool unpriv)
191{
192 BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV);
193 unpriv |= bpf_map_ptr_unpriv(aux);
194 aux->map_state = (unsigned long)map |
195 (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL);
196}
Martin KaFai Laufad73a12017-03-22 10:00:32 -0700197
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200198struct bpf_call_arg_meta {
199 struct bpf_map *map_ptr;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200200 bool raw_mode;
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200201 bool pkt_access;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200202 int regno;
203 int access_size;
Yonghong Song849fa502018-04-28 22:28:09 -0700204 s64 msize_smax_value;
205 u64 msize_umax_value;
Martin KaFai Lau1b986582019-03-12 10:23:02 -0700206 int ref_obj_id;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800207 int func_id;
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200208};
209
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700210static DEFINE_MUTEX(bpf_verifier_lock);
211
Martin KaFai Laud9762e82018-12-13 10:41:48 -0800212static const struct bpf_line_info *
213find_linfo(const struct bpf_verifier_env *env, u32 insn_off)
214{
215 const struct bpf_line_info *linfo;
216 const struct bpf_prog *prog;
217 u32 i, nr_linfo;
218
219 prog = env->prog;
220 nr_linfo = prog->aux->nr_linfo;
221
222 if (!nr_linfo || insn_off >= prog->len)
223 return NULL;
224
225 linfo = prog->aux->linfo;
226 for (i = 1; i < nr_linfo; i++)
227 if (insn_off < linfo[i].insn_off)
228 break;
229
230 return &linfo[i - 1];
231}
232
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700233void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt,
234 va_list args)
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700235{
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700236 unsigned int n;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700237
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700238 n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args);
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700239
240 WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1,
241 "verifier log line truncated - local buffer too short\n");
242
243 n = min(log->len_total - log->len_used - 1, n);
244 log->kbuf[n] = '\0';
245
246 if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1))
247 log->len_used += n;
248 else
249 log->ubuf = NULL;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700250}
Jiri Olsaabe08842018-03-23 11:41:28 +0100251
252/* log_level controls verbosity level of eBPF verifier.
253 * bpf_verifier_log_write() is used to dump the verification trace to the log,
254 * so the user can figure out what's wrong with the program
Quentin Monnet430e68d2018-01-10 12:26:06 +0000255 */
Jiri Olsaabe08842018-03-23 11:41:28 +0100256__printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env,
257 const char *fmt, ...)
258{
259 va_list args;
260
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700261 if (!bpf_verifier_log_needed(&env->log))
262 return;
263
Jiri Olsaabe08842018-03-23 11:41:28 +0100264 va_start(args, fmt);
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700265 bpf_verifier_vlog(&env->log, fmt, args);
Jiri Olsaabe08842018-03-23 11:41:28 +0100266 va_end(args);
267}
268EXPORT_SYMBOL_GPL(bpf_verifier_log_write);
269
270__printf(2, 3) static void verbose(void *private_data, const char *fmt, ...)
271{
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700272 struct bpf_verifier_env *env = private_data;
Jiri Olsaabe08842018-03-23 11:41:28 +0100273 va_list args;
274
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700275 if (!bpf_verifier_log_needed(&env->log))
276 return;
277
Jiri Olsaabe08842018-03-23 11:41:28 +0100278 va_start(args, fmt);
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700279 bpf_verifier_vlog(&env->log, fmt, args);
Jiri Olsaabe08842018-03-23 11:41:28 +0100280 va_end(args);
281}
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700282
Martin KaFai Laud9762e82018-12-13 10:41:48 -0800283static const char *ltrim(const char *s)
284{
285 while (isspace(*s))
286 s++;
287
288 return s;
289}
290
291__printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env,
292 u32 insn_off,
293 const char *prefix_fmt, ...)
294{
295 const struct bpf_line_info *linfo;
296
297 if (!bpf_verifier_log_needed(&env->log))
298 return;
299
300 linfo = find_linfo(env, insn_off);
301 if (!linfo || linfo == env->prev_linfo)
302 return;
303
304 if (prefix_fmt) {
305 va_list args;
306
307 va_start(args, prefix_fmt);
308 bpf_verifier_vlog(&env->log, prefix_fmt, args);
309 va_end(args);
310 }
311
312 verbose(env, "%s\n",
313 ltrim(btf_name_by_offset(env->prog->aux->btf,
314 linfo->line_off)));
315
316 env->prev_linfo = linfo;
317}
318
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200319static bool type_is_pkt_pointer(enum bpf_reg_type type)
320{
321 return type == PTR_TO_PACKET ||
322 type == PTR_TO_PACKET_META;
323}
324
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800325static bool type_is_sk_pointer(enum bpf_reg_type type)
326{
327 return type == PTR_TO_SOCKET ||
Martin KaFai Lau655a51e2019-02-09 23:22:24 -0800328 type == PTR_TO_SOCK_COMMON ||
329 type == PTR_TO_TCP_SOCK;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800330}
331
Joe Stringer840b9612018-10-02 13:35:32 -0700332static bool reg_type_may_be_null(enum bpf_reg_type type)
333{
Joe Stringerfd978bf72018-10-02 13:35:35 -0700334 return type == PTR_TO_MAP_VALUE_OR_NULL ||
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800335 type == PTR_TO_SOCKET_OR_NULL ||
Martin KaFai Lau655a51e2019-02-09 23:22:24 -0800336 type == PTR_TO_SOCK_COMMON_OR_NULL ||
337 type == PTR_TO_TCP_SOCK_OR_NULL;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700338}
339
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800340static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg)
341{
342 return reg->type == PTR_TO_MAP_VALUE &&
343 map_value_has_spin_lock(reg->map_ptr);
344}
345
Martin KaFai Laucba368c2019-03-18 10:37:13 -0700346static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type)
347{
348 return type == PTR_TO_SOCKET ||
349 type == PTR_TO_SOCKET_OR_NULL ||
350 type == PTR_TO_TCP_SOCK ||
351 type == PTR_TO_TCP_SOCK_OR_NULL;
352}
353
Martin KaFai Lau1b986582019-03-12 10:23:02 -0700354static bool arg_type_may_be_refcounted(enum bpf_arg_type type)
Joe Stringerfd978bf72018-10-02 13:35:35 -0700355{
Martin KaFai Lau1b986582019-03-12 10:23:02 -0700356 return type == ARG_PTR_TO_SOCK_COMMON;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700357}
358
359/* Determine whether the function releases some resources allocated by another
360 * function call. The first reference type argument will be assumed to be
361 * released by release_reference().
362 */
363static bool is_release_function(enum bpf_func_id func_id)
364{
Joe Stringer6acc9b42018-10-02 13:35:36 -0700365 return func_id == BPF_FUNC_sk_release;
Joe Stringer840b9612018-10-02 13:35:32 -0700366}
367
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800368static bool is_acquire_function(enum bpf_func_id func_id)
369{
370 return func_id == BPF_FUNC_sk_lookup_tcp ||
Lorenz Baueredbf8c02019-03-22 09:54:01 +0800371 func_id == BPF_FUNC_sk_lookup_udp ||
372 func_id == BPF_FUNC_skc_lookup_tcp;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800373}
374
Martin KaFai Lau1b986582019-03-12 10:23:02 -0700375static bool is_ptr_cast_function(enum bpf_func_id func_id)
376{
377 return func_id == BPF_FUNC_tcp_sock ||
378 func_id == BPF_FUNC_sk_fullsock;
379}
380
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700381/* string representation of 'enum bpf_reg_type' */
382static const char * const reg_type_str[] = {
383 [NOT_INIT] = "?",
Edward Creef1174f72017-08-07 15:26:19 +0100384 [SCALAR_VALUE] = "inv",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700385 [PTR_TO_CTX] = "ctx",
386 [CONST_PTR_TO_MAP] = "map_ptr",
387 [PTR_TO_MAP_VALUE] = "map_value",
388 [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700389 [PTR_TO_STACK] = "fp",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700390 [PTR_TO_PACKET] = "pkt",
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200391 [PTR_TO_PACKET_META] = "pkt_meta",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700392 [PTR_TO_PACKET_END] = "pkt_end",
Petar Penkovd58e4682018-09-14 07:46:18 -0700393 [PTR_TO_FLOW_KEYS] = "flow_keys",
Joe Stringerc64b7982018-10-02 13:35:33 -0700394 [PTR_TO_SOCKET] = "sock",
395 [PTR_TO_SOCKET_OR_NULL] = "sock_or_null",
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800396 [PTR_TO_SOCK_COMMON] = "sock_common",
397 [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null",
Martin KaFai Lau655a51e2019-02-09 23:22:24 -0800398 [PTR_TO_TCP_SOCK] = "tcp_sock",
399 [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null",
Matt Mullins9df1c282019-04-26 11:49:47 -0700400 [PTR_TO_TP_BUFFER] = "tp_buffer",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700401};
402
Edward Cree8efea212018-08-22 20:02:44 +0100403static char slot_type_char[] = {
404 [STACK_INVALID] = '?',
405 [STACK_SPILL] = 'r',
406 [STACK_MISC] = 'm',
407 [STACK_ZERO] = '0',
408};
409
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800410static void print_liveness(struct bpf_verifier_env *env,
411 enum bpf_reg_liveness live)
412{
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -0800413 if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE))
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800414 verbose(env, "_");
415 if (live & REG_LIVE_READ)
416 verbose(env, "r");
417 if (live & REG_LIVE_WRITTEN)
418 verbose(env, "w");
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -0800419 if (live & REG_LIVE_DONE)
420 verbose(env, "D");
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800421}
422
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800423static struct bpf_func_state *func(struct bpf_verifier_env *env,
424 const struct bpf_reg_state *reg)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700425{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800426 struct bpf_verifier_state *cur = env->cur_state;
427
428 return cur->frame[reg->frameno];
429}
430
431static void print_verifier_state(struct bpf_verifier_env *env,
432 const struct bpf_func_state *state)
433{
434 const struct bpf_reg_state *reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700435 enum bpf_reg_type t;
436 int i;
437
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800438 if (state->frameno)
439 verbose(env, " frame%d:", state->frameno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700440 for (i = 0; i < MAX_BPF_REG; i++) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700441 reg = &state->regs[i];
442 t = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700443 if (t == NOT_INIT)
444 continue;
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800445 verbose(env, " R%d", i);
446 print_liveness(env, reg->live);
447 verbose(env, "=%s", reg_type_str[t]);
Edward Creef1174f72017-08-07 15:26:19 +0100448 if ((t == SCALAR_VALUE || t == PTR_TO_STACK) &&
449 tnum_is_const(reg->var_off)) {
450 /* reg->off should be 0 for SCALAR_VALUE */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700451 verbose(env, "%lld", reg->var_off.value + reg->off);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800452 if (t == PTR_TO_STACK)
453 verbose(env, ",call_%d", func(env, reg)->callsite);
Edward Creef1174f72017-08-07 15:26:19 +0100454 } else {
Martin KaFai Laucba368c2019-03-18 10:37:13 -0700455 verbose(env, "(id=%d", reg->id);
456 if (reg_type_may_be_refcounted_or_null(t))
457 verbose(env, ",ref_obj_id=%d", reg->ref_obj_id);
Edward Creef1174f72017-08-07 15:26:19 +0100458 if (t != SCALAR_VALUE)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700459 verbose(env, ",off=%d", reg->off);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200460 if (type_is_pkt_pointer(t))
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700461 verbose(env, ",r=%d", reg->range);
Edward Creef1174f72017-08-07 15:26:19 +0100462 else if (t == CONST_PTR_TO_MAP ||
463 t == PTR_TO_MAP_VALUE ||
464 t == PTR_TO_MAP_VALUE_OR_NULL)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700465 verbose(env, ",ks=%d,vs=%d",
Edward Creef1174f72017-08-07 15:26:19 +0100466 reg->map_ptr->key_size,
467 reg->map_ptr->value_size);
Edward Cree7d1238f2017-08-07 15:26:56 +0100468 if (tnum_is_const(reg->var_off)) {
469 /* Typically an immediate SCALAR_VALUE, but
470 * could be a pointer whose offset is too big
471 * for reg->off
472 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700473 verbose(env, ",imm=%llx", reg->var_off.value);
Edward Cree7d1238f2017-08-07 15:26:56 +0100474 } else {
475 if (reg->smin_value != reg->umin_value &&
476 reg->smin_value != S64_MIN)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700477 verbose(env, ",smin_value=%lld",
Edward Cree7d1238f2017-08-07 15:26:56 +0100478 (long long)reg->smin_value);
479 if (reg->smax_value != reg->umax_value &&
480 reg->smax_value != S64_MAX)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700481 verbose(env, ",smax_value=%lld",
Edward Cree7d1238f2017-08-07 15:26:56 +0100482 (long long)reg->smax_value);
483 if (reg->umin_value != 0)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700484 verbose(env, ",umin_value=%llu",
Edward Cree7d1238f2017-08-07 15:26:56 +0100485 (unsigned long long)reg->umin_value);
486 if (reg->umax_value != U64_MAX)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700487 verbose(env, ",umax_value=%llu",
Edward Cree7d1238f2017-08-07 15:26:56 +0100488 (unsigned long long)reg->umax_value);
489 if (!tnum_is_unknown(reg->var_off)) {
490 char tn_buf[48];
Edward Creef1174f72017-08-07 15:26:19 +0100491
Edward Cree7d1238f2017-08-07 15:26:56 +0100492 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700493 verbose(env, ",var_off=%s", tn_buf);
Edward Cree7d1238f2017-08-07 15:26:56 +0100494 }
Edward Creef1174f72017-08-07 15:26:19 +0100495 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700496 verbose(env, ")");
Edward Creef1174f72017-08-07 15:26:19 +0100497 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700498 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700499 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
Edward Cree8efea212018-08-22 20:02:44 +0100500 char types_buf[BPF_REG_SIZE + 1];
501 bool valid = false;
502 int j;
503
504 for (j = 0; j < BPF_REG_SIZE; j++) {
505 if (state->stack[i].slot_type[j] != STACK_INVALID)
506 valid = true;
507 types_buf[j] = slot_type_char[
508 state->stack[i].slot_type[j]];
509 }
510 types_buf[BPF_REG_SIZE] = 0;
511 if (!valid)
512 continue;
513 verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE);
514 print_liveness(env, state->stack[i].spilled_ptr.live);
515 if (state->stack[i].slot_type[0] == STACK_SPILL)
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800516 verbose(env, "=%s",
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700517 reg_type_str[state->stack[i].spilled_ptr.type]);
Edward Cree8efea212018-08-22 20:02:44 +0100518 else
519 verbose(env, "=%s", types_buf);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700520 }
Joe Stringerfd978bf72018-10-02 13:35:35 -0700521 if (state->acquired_refs && state->refs[0].id) {
522 verbose(env, " refs=%d", state->refs[0].id);
523 for (i = 1; i < state->acquired_refs; i++)
524 if (state->refs[i].id)
525 verbose(env, ",%d", state->refs[i].id);
526 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700527 verbose(env, "\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700528}
529
Joe Stringer84dbf352018-10-02 13:35:34 -0700530#define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \
531static int copy_##NAME##_state(struct bpf_func_state *dst, \
532 const struct bpf_func_state *src) \
533{ \
534 if (!src->FIELD) \
535 return 0; \
536 if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \
537 /* internal bug, make state invalid to reject the program */ \
538 memset(dst, 0, sizeof(*dst)); \
539 return -EFAULT; \
540 } \
541 memcpy(dst->FIELD, src->FIELD, \
542 sizeof(*src->FIELD) * (src->COUNT / SIZE)); \
543 return 0; \
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700544}
Joe Stringerfd978bf72018-10-02 13:35:35 -0700545/* copy_reference_state() */
546COPY_STATE_FN(reference, acquired_refs, refs, 1)
Joe Stringer84dbf352018-10-02 13:35:34 -0700547/* copy_stack_state() */
548COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE)
549#undef COPY_STATE_FN
550
551#define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \
552static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \
553 bool copy_old) \
554{ \
555 u32 old_size = state->COUNT; \
556 struct bpf_##NAME##_state *new_##FIELD; \
557 int slot = size / SIZE; \
558 \
559 if (size <= old_size || !size) { \
560 if (copy_old) \
561 return 0; \
562 state->COUNT = slot * SIZE; \
563 if (!size && old_size) { \
564 kfree(state->FIELD); \
565 state->FIELD = NULL; \
566 } \
567 return 0; \
568 } \
569 new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \
570 GFP_KERNEL); \
571 if (!new_##FIELD) \
572 return -ENOMEM; \
573 if (copy_old) { \
574 if (state->FIELD) \
575 memcpy(new_##FIELD, state->FIELD, \
576 sizeof(*new_##FIELD) * (old_size / SIZE)); \
577 memset(new_##FIELD + old_size / SIZE, 0, \
578 sizeof(*new_##FIELD) * (size - old_size) / SIZE); \
579 } \
580 state->COUNT = slot * SIZE; \
581 kfree(state->FIELD); \
582 state->FIELD = new_##FIELD; \
583 return 0; \
584}
Joe Stringerfd978bf72018-10-02 13:35:35 -0700585/* realloc_reference_state() */
586REALLOC_STATE_FN(reference, acquired_refs, refs, 1)
Joe Stringer84dbf352018-10-02 13:35:34 -0700587/* realloc_stack_state() */
588REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE)
589#undef REALLOC_STATE_FN
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700590
591/* do_check() starts with zero-sized stack in struct bpf_verifier_state to
592 * make it consume minimal amount of memory. check_stack_write() access from
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800593 * the program calls into realloc_func_state() to grow the stack size.
Joe Stringer84dbf352018-10-02 13:35:34 -0700594 * Note there is a non-zero 'parent' pointer inside bpf_verifier_state
595 * which realloc_stack_state() copies over. It points to previous
596 * bpf_verifier_state which is never reallocated.
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700597 */
Joe Stringerfd978bf72018-10-02 13:35:35 -0700598static int realloc_func_state(struct bpf_func_state *state, int stack_size,
599 int refs_size, bool copy_old)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700600{
Joe Stringerfd978bf72018-10-02 13:35:35 -0700601 int err = realloc_reference_state(state, refs_size, copy_old);
602 if (err)
603 return err;
604 return realloc_stack_state(state, stack_size, copy_old);
605}
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700606
Joe Stringerfd978bf72018-10-02 13:35:35 -0700607/* Acquire a pointer id from the env and update the state->refs to include
608 * this new pointer reference.
609 * On success, returns a valid pointer id to associate with the register
610 * On failure, returns a negative errno.
611 */
612static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx)
613{
614 struct bpf_func_state *state = cur_func(env);
615 int new_ofs = state->acquired_refs;
616 int id, err;
617
618 err = realloc_reference_state(state, state->acquired_refs + 1, true);
619 if (err)
620 return err;
621 id = ++env->id_gen;
622 state->refs[new_ofs].id = id;
623 state->refs[new_ofs].insn_idx = insn_idx;
624
625 return id;
626}
627
628/* release function corresponding to acquire_reference_state(). Idempotent. */
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800629static int release_reference_state(struct bpf_func_state *state, int ptr_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -0700630{
631 int i, last_idx;
632
Joe Stringerfd978bf72018-10-02 13:35:35 -0700633 last_idx = state->acquired_refs - 1;
634 for (i = 0; i < state->acquired_refs; i++) {
635 if (state->refs[i].id == ptr_id) {
636 if (last_idx && i != last_idx)
637 memcpy(&state->refs[i], &state->refs[last_idx],
638 sizeof(*state->refs));
639 memset(&state->refs[last_idx], 0, sizeof(*state->refs));
640 state->acquired_refs--;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700641 return 0;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700642 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700643 }
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800644 return -EINVAL;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700645}
646
647static int transfer_reference_state(struct bpf_func_state *dst,
648 struct bpf_func_state *src)
649{
650 int err = realloc_reference_state(dst, src->acquired_refs, false);
651 if (err)
652 return err;
653 err = copy_reference_state(dst, src);
654 if (err)
655 return err;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700656 return 0;
657}
658
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800659static void free_func_state(struct bpf_func_state *state)
660{
Alexei Starovoitov58963512018-01-08 07:51:17 -0800661 if (!state)
662 return;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700663 kfree(state->refs);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800664 kfree(state->stack);
665 kfree(state);
666}
667
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700668static void free_verifier_state(struct bpf_verifier_state *state,
669 bool free_self)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700670{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800671 int i;
672
673 for (i = 0; i <= state->curframe; i++) {
674 free_func_state(state->frame[i]);
675 state->frame[i] = NULL;
676 }
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700677 if (free_self)
678 kfree(state);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700679}
680
681/* copy verifier state from src to dst growing dst stack space
682 * when necessary to accommodate larger src stack
683 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800684static int copy_func_state(struct bpf_func_state *dst,
685 const struct bpf_func_state *src)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700686{
687 int err;
688
Joe Stringerfd978bf72018-10-02 13:35:35 -0700689 err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs,
690 false);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700691 if (err)
692 return err;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700693 memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs));
694 err = copy_reference_state(dst, src);
695 if (err)
696 return err;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700697 return copy_stack_state(dst, src);
698}
699
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800700static int copy_verifier_state(struct bpf_verifier_state *dst_state,
701 const struct bpf_verifier_state *src)
702{
703 struct bpf_func_state *dst;
704 int i, err;
705
706 /* if dst has more stack frames then src frame, free them */
707 for (i = src->curframe + 1; i <= dst_state->curframe; i++) {
708 free_func_state(dst_state->frame[i]);
709 dst_state->frame[i] = NULL;
710 }
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100711 dst_state->speculative = src->speculative;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800712 dst_state->curframe = src->curframe;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800713 dst_state->active_spin_lock = src->active_spin_lock;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800714 for (i = 0; i <= src->curframe; i++) {
715 dst = dst_state->frame[i];
716 if (!dst) {
717 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
718 if (!dst)
719 return -ENOMEM;
720 dst_state->frame[i] = dst;
721 }
722 err = copy_func_state(dst, src->frame[i]);
723 if (err)
724 return err;
725 }
726 return 0;
727}
728
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700729static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx,
730 int *insn_idx)
731{
732 struct bpf_verifier_state *cur = env->cur_state;
733 struct bpf_verifier_stack_elem *elem, *head = env->head;
734 int err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700735
736 if (env->head == NULL)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700737 return -ENOENT;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700738
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700739 if (cur) {
740 err = copy_verifier_state(cur, &head->st);
741 if (err)
742 return err;
743 }
744 if (insn_idx)
745 *insn_idx = head->insn_idx;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700746 if (prev_insn_idx)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700747 *prev_insn_idx = head->prev_insn_idx;
748 elem = head->next;
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700749 free_verifier_state(&head->st, false);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700750 kfree(head);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700751 env->head = elem;
752 env->stack_size--;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700753 return 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700754}
755
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100756static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100757 int insn_idx, int prev_insn_idx,
758 bool speculative)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700759{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700760 struct bpf_verifier_state *cur = env->cur_state;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100761 struct bpf_verifier_stack_elem *elem;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700762 int err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700763
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700764 elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700765 if (!elem)
766 goto err;
767
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700768 elem->insn_idx = insn_idx;
769 elem->prev_insn_idx = prev_insn_idx;
770 elem->next = env->head;
771 env->head = elem;
772 env->stack_size++;
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700773 err = copy_verifier_state(&elem->st, cur);
774 if (err)
775 goto err;
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100776 elem->st.speculative |= speculative;
Daniel Borkmann07016152016-04-05 22:33:17 +0200777 if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700778 verbose(env, "BPF program is too complex\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700779 goto err;
780 }
781 return &elem->st;
782err:
Alexei Starovoitov58963512018-01-08 07:51:17 -0800783 free_verifier_state(env->cur_state, true);
784 env->cur_state = NULL;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700785 /* pop all elements and return */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700786 while (!pop_stack(env, NULL, NULL));
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700787 return NULL;
788}
789
790#define CALLER_SAVED_REGS 6
791static const int caller_saved[CALLER_SAVED_REGS] = {
792 BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
793};
794
Edward Creef1174f72017-08-07 15:26:19 +0100795static void __mark_reg_not_init(struct bpf_reg_state *reg);
796
Edward Creeb03c9f92017-08-07 15:26:36 +0100797/* Mark the unknown part of a register (variable offset or scalar value) as
798 * known to have the value @imm.
799 */
800static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
801{
Alexei Starovoitova9c676b2018-09-04 19:13:44 -0700802 /* Clear id, off, and union(map_ptr, range) */
803 memset(((u8 *)reg) + sizeof(reg->type), 0,
804 offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type));
Edward Creeb03c9f92017-08-07 15:26:36 +0100805 reg->var_off = tnum_const(imm);
806 reg->smin_value = (s64)imm;
807 reg->smax_value = (s64)imm;
808 reg->umin_value = imm;
809 reg->umax_value = imm;
810}
811
Edward Creef1174f72017-08-07 15:26:19 +0100812/* Mark the 'variable offset' part of a register as zero. This should be
813 * used only on registers holding a pointer type.
814 */
815static void __mark_reg_known_zero(struct bpf_reg_state *reg)
816{
Edward Creeb03c9f92017-08-07 15:26:36 +0100817 __mark_reg_known(reg, 0);
Edward Creef1174f72017-08-07 15:26:19 +0100818}
819
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800820static void __mark_reg_const_zero(struct bpf_reg_state *reg)
821{
822 __mark_reg_known(reg, 0);
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800823 reg->type = SCALAR_VALUE;
824}
825
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700826static void mark_reg_known_zero(struct bpf_verifier_env *env,
827 struct bpf_reg_state *regs, u32 regno)
Edward Creef1174f72017-08-07 15:26:19 +0100828{
829 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700830 verbose(env, "mark_reg_known_zero(regs, %u)\n", regno);
Edward Creef1174f72017-08-07 15:26:19 +0100831 /* Something bad happened, let's kill all regs */
832 for (regno = 0; regno < MAX_BPF_REG; regno++)
833 __mark_reg_not_init(regs + regno);
834 return;
835 }
836 __mark_reg_known_zero(regs + regno);
837}
838
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200839static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg)
840{
841 return type_is_pkt_pointer(reg->type);
842}
843
844static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg)
845{
846 return reg_is_pkt_pointer(reg) ||
847 reg->type == PTR_TO_PACKET_END;
848}
849
850/* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */
851static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg,
852 enum bpf_reg_type which)
853{
854 /* The register can already have a range from prior markings.
855 * This is fine as long as it hasn't been advanced from its
856 * origin.
857 */
858 return reg->type == which &&
859 reg->id == 0 &&
860 reg->off == 0 &&
861 tnum_equals_const(reg->var_off, 0);
862}
863
Edward Creeb03c9f92017-08-07 15:26:36 +0100864/* Attempts to improve min/max values based on var_off information */
865static void __update_reg_bounds(struct bpf_reg_state *reg)
866{
867 /* min signed is max(sign bit) | min(other bits) */
868 reg->smin_value = max_t(s64, reg->smin_value,
869 reg->var_off.value | (reg->var_off.mask & S64_MIN));
870 /* max signed is min(sign bit) | max(other bits) */
871 reg->smax_value = min_t(s64, reg->smax_value,
872 reg->var_off.value | (reg->var_off.mask & S64_MAX));
873 reg->umin_value = max(reg->umin_value, reg->var_off.value);
874 reg->umax_value = min(reg->umax_value,
875 reg->var_off.value | reg->var_off.mask);
876}
877
878/* Uses signed min/max values to inform unsigned, and vice-versa */
879static void __reg_deduce_bounds(struct bpf_reg_state *reg)
880{
881 /* Learn sign from signed bounds.
882 * If we cannot cross the sign boundary, then signed and unsigned bounds
883 * are the same, so combine. This works even in the negative case, e.g.
884 * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff.
885 */
886 if (reg->smin_value >= 0 || reg->smax_value < 0) {
887 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
888 reg->umin_value);
889 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
890 reg->umax_value);
891 return;
892 }
893 /* Learn sign from unsigned bounds. Signed bounds cross the sign
894 * boundary, so we must be careful.
895 */
896 if ((s64)reg->umax_value >= 0) {
897 /* Positive. We can't learn anything from the smin, but smax
898 * is positive, hence safe.
899 */
900 reg->smin_value = reg->umin_value;
901 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
902 reg->umax_value);
903 } else if ((s64)reg->umin_value < 0) {
904 /* Negative. We can't learn anything from the smax, but smin
905 * is negative, hence safe.
906 */
907 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
908 reg->umin_value);
909 reg->smax_value = reg->umax_value;
910 }
911}
912
913/* Attempts to improve var_off based on unsigned min/max information */
914static void __reg_bound_offset(struct bpf_reg_state *reg)
915{
916 reg->var_off = tnum_intersect(reg->var_off,
917 tnum_range(reg->umin_value,
918 reg->umax_value));
919}
920
921/* Reset the min/max bounds of a register */
922static void __mark_reg_unbounded(struct bpf_reg_state *reg)
923{
924 reg->smin_value = S64_MIN;
925 reg->smax_value = S64_MAX;
926 reg->umin_value = 0;
927 reg->umax_value = U64_MAX;
928}
929
Edward Creef1174f72017-08-07 15:26:19 +0100930/* Mark a register as having a completely unknown (scalar) value. */
931static void __mark_reg_unknown(struct bpf_reg_state *reg)
932{
Alexei Starovoitova9c676b2018-09-04 19:13:44 -0700933 /*
934 * Clear type, id, off, and union(map_ptr, range) and
935 * padding between 'type' and union
936 */
937 memset(reg, 0, offsetof(struct bpf_reg_state, var_off));
Edward Creef1174f72017-08-07 15:26:19 +0100938 reg->type = SCALAR_VALUE;
Edward Creef1174f72017-08-07 15:26:19 +0100939 reg->var_off = tnum_unknown;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800940 reg->frameno = 0;
Edward Creeb03c9f92017-08-07 15:26:36 +0100941 __mark_reg_unbounded(reg);
Edward Creef1174f72017-08-07 15:26:19 +0100942}
943
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700944static void mark_reg_unknown(struct bpf_verifier_env *env,
945 struct bpf_reg_state *regs, u32 regno)
Edward Creef1174f72017-08-07 15:26:19 +0100946{
947 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700948 verbose(env, "mark_reg_unknown(regs, %u)\n", regno);
Alexei Starovoitov19ceb412017-11-30 21:31:37 -0800949 /* Something bad happened, let's kill all regs except FP */
950 for (regno = 0; regno < BPF_REG_FP; regno++)
Edward Creef1174f72017-08-07 15:26:19 +0100951 __mark_reg_not_init(regs + regno);
952 return;
953 }
954 __mark_reg_unknown(regs + regno);
955}
956
957static void __mark_reg_not_init(struct bpf_reg_state *reg)
958{
959 __mark_reg_unknown(reg);
960 reg->type = NOT_INIT;
961}
962
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700963static void mark_reg_not_init(struct bpf_verifier_env *env,
964 struct bpf_reg_state *regs, u32 regno)
Daniel Borkmanna9789ef2017-05-25 01:05:06 +0200965{
Edward Creef1174f72017-08-07 15:26:19 +0100966 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700967 verbose(env, "mark_reg_not_init(regs, %u)\n", regno);
Alexei Starovoitov19ceb412017-11-30 21:31:37 -0800968 /* Something bad happened, let's kill all regs except FP */
969 for (regno = 0; regno < BPF_REG_FP; regno++)
Edward Creef1174f72017-08-07 15:26:19 +0100970 __mark_reg_not_init(regs + regno);
971 return;
972 }
973 __mark_reg_not_init(regs + regno);
Daniel Borkmanna9789ef2017-05-25 01:05:06 +0200974}
975
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700976static void init_reg_state(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800977 struct bpf_func_state *state)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700978{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800979 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700980 int i;
981
Edward Creedc503a82017-08-15 20:34:35 +0100982 for (i = 0; i < MAX_BPF_REG; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700983 mark_reg_not_init(env, regs, i);
Edward Creedc503a82017-08-15 20:34:35 +0100984 regs[i].live = REG_LIVE_NONE;
Edward Cree679c7822018-08-22 20:02:19 +0100985 regs[i].parent = NULL;
Edward Creedc503a82017-08-15 20:34:35 +0100986 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700987
988 /* frame pointer */
Edward Creef1174f72017-08-07 15:26:19 +0100989 regs[BPF_REG_FP].type = PTR_TO_STACK;
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700990 mark_reg_known_zero(env, regs, BPF_REG_FP);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800991 regs[BPF_REG_FP].frameno = state->frameno;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700992
993 /* 1st arg to a function */
994 regs[BPF_REG_1].type = PTR_TO_CTX;
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700995 mark_reg_known_zero(env, regs, BPF_REG_1);
Daniel Borkmann6760bf22016-12-18 01:52:59 +0100996}
997
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800998#define BPF_MAIN_FUNC (-1)
999static void init_func_state(struct bpf_verifier_env *env,
1000 struct bpf_func_state *state,
1001 int callsite, int frameno, int subprogno)
1002{
1003 state->callsite = callsite;
1004 state->frameno = frameno;
1005 state->subprogno = subprogno;
1006 init_reg_state(env, state);
1007}
1008
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001009enum reg_arg_type {
1010 SRC_OP, /* register is used as source operand */
1011 DST_OP, /* register is used as destination operand */
1012 DST_OP_NO_MARK /* same as above, check only, don't mark */
1013};
1014
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001015static int cmp_subprogs(const void *a, const void *b)
1016{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001017 return ((struct bpf_subprog_info *)a)->start -
1018 ((struct bpf_subprog_info *)b)->start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001019}
1020
1021static int find_subprog(struct bpf_verifier_env *env, int off)
1022{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001023 struct bpf_subprog_info *p;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001024
Jiong Wang9c8105b2018-05-02 16:17:18 -04001025 p = bsearch(&off, env->subprog_info, env->subprog_cnt,
1026 sizeof(env->subprog_info[0]), cmp_subprogs);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001027 if (!p)
1028 return -ENOENT;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001029 return p - env->subprog_info;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001030
1031}
1032
1033static int add_subprog(struct bpf_verifier_env *env, int off)
1034{
1035 int insn_cnt = env->prog->len;
1036 int ret;
1037
1038 if (off >= insn_cnt || off < 0) {
1039 verbose(env, "call to invalid destination\n");
1040 return -EINVAL;
1041 }
1042 ret = find_subprog(env, off);
1043 if (ret >= 0)
1044 return 0;
Jiong Wang4cb3d992018-05-02 16:17:19 -04001045 if (env->subprog_cnt >= BPF_MAX_SUBPROGS) {
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001046 verbose(env, "too many subprograms\n");
1047 return -E2BIG;
1048 }
Jiong Wang9c8105b2018-05-02 16:17:18 -04001049 env->subprog_info[env->subprog_cnt++].start = off;
1050 sort(env->subprog_info, env->subprog_cnt,
1051 sizeof(env->subprog_info[0]), cmp_subprogs, NULL);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001052 return 0;
1053}
1054
1055static int check_subprogs(struct bpf_verifier_env *env)
1056{
1057 int i, ret, subprog_start, subprog_end, off, cur_subprog = 0;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001058 struct bpf_subprog_info *subprog = env->subprog_info;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001059 struct bpf_insn *insn = env->prog->insnsi;
1060 int insn_cnt = env->prog->len;
1061
Jiong Wangf910cef2018-05-02 16:17:17 -04001062 /* Add entry function. */
1063 ret = add_subprog(env, 0);
1064 if (ret < 0)
1065 return ret;
1066
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001067 /* determine subprog starts. The end is one before the next starts */
1068 for (i = 0; i < insn_cnt; i++) {
1069 if (insn[i].code != (BPF_JMP | BPF_CALL))
1070 continue;
1071 if (insn[i].src_reg != BPF_PSEUDO_CALL)
1072 continue;
1073 if (!env->allow_ptr_leaks) {
1074 verbose(env, "function calls to other bpf functions are allowed for root only\n");
1075 return -EPERM;
1076 }
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001077 ret = add_subprog(env, i + insn[i].imm + 1);
1078 if (ret < 0)
1079 return ret;
1080 }
1081
Jiong Wang4cb3d992018-05-02 16:17:19 -04001082 /* Add a fake 'exit' subprog which could simplify subprog iteration
1083 * logic. 'subprog_cnt' should not be increased.
1084 */
1085 subprog[env->subprog_cnt].start = insn_cnt;
1086
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07001087 if (env->log.level & BPF_LOG_LEVEL2)
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001088 for (i = 0; i < env->subprog_cnt; i++)
Jiong Wang9c8105b2018-05-02 16:17:18 -04001089 verbose(env, "func#%d @%d\n", i, subprog[i].start);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001090
1091 /* now check that all jumps are within the same subprog */
Jiong Wang4cb3d992018-05-02 16:17:19 -04001092 subprog_start = subprog[cur_subprog].start;
1093 subprog_end = subprog[cur_subprog + 1].start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001094 for (i = 0; i < insn_cnt; i++) {
1095 u8 code = insn[i].code;
1096
Jiong Wang092ed092019-01-26 12:26:01 -05001097 if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32)
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001098 goto next;
1099 if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL)
1100 goto next;
1101 off = i + insn[i].off + 1;
1102 if (off < subprog_start || off >= subprog_end) {
1103 verbose(env, "jump out of range from insn %d to %d\n", i, off);
1104 return -EINVAL;
1105 }
1106next:
1107 if (i == subprog_end - 1) {
1108 /* to avoid fall-through from one subprog into another
1109 * the last insn of the subprog should be either exit
1110 * or unconditional jump back
1111 */
1112 if (code != (BPF_JMP | BPF_EXIT) &&
1113 code != (BPF_JMP | BPF_JA)) {
1114 verbose(env, "last insn is not an exit or jmp\n");
1115 return -EINVAL;
1116 }
1117 subprog_start = subprog_end;
Jiong Wang4cb3d992018-05-02 16:17:19 -04001118 cur_subprog++;
1119 if (cur_subprog < env->subprog_cnt)
Jiong Wang9c8105b2018-05-02 16:17:18 -04001120 subprog_end = subprog[cur_subprog + 1].start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001121 }
1122 }
1123 return 0;
1124}
1125
Edward Cree679c7822018-08-22 20:02:19 +01001126/* Parentage chain of this register (or stack slot) should take care of all
1127 * issues like callee-saved registers, stack slot allocation time, etc.
1128 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001129static int mark_reg_read(struct bpf_verifier_env *env,
Edward Cree679c7822018-08-22 20:02:19 +01001130 const struct bpf_reg_state *state,
1131 struct bpf_reg_state *parent)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001132{
1133 bool writes = parent == state->parent; /* Observe write marks */
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07001134 int cnt = 0;
Edward Creedc503a82017-08-15 20:34:35 +01001135
1136 while (parent) {
1137 /* if read wasn't screened by an earlier write ... */
Edward Cree679c7822018-08-22 20:02:19 +01001138 if (writes && state->live & REG_LIVE_WRITTEN)
Edward Creedc503a82017-08-15 20:34:35 +01001139 break;
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -08001140 if (parent->live & REG_LIVE_DONE) {
1141 verbose(env, "verifier BUG type %s var_off %lld off %d\n",
1142 reg_type_str[parent->type],
1143 parent->var_off.value, parent->off);
1144 return -EFAULT;
1145 }
Alexei Starovoitov25af32d2019-04-01 21:27:42 -07001146 if (parent->live & REG_LIVE_READ)
1147 /* The parentage chain never changes and
1148 * this parent was already marked as LIVE_READ.
1149 * There is no need to keep walking the chain again and
1150 * keep re-marking all parents as LIVE_READ.
1151 * This case happens when the same register is read
1152 * multiple times without writes into it in-between.
1153 */
1154 break;
Edward Creedc503a82017-08-15 20:34:35 +01001155 /* ... then we depend on parent's value */
Edward Cree679c7822018-08-22 20:02:19 +01001156 parent->live |= REG_LIVE_READ;
Edward Creedc503a82017-08-15 20:34:35 +01001157 state = parent;
1158 parent = state->parent;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001159 writes = true;
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07001160 cnt++;
Edward Creedc503a82017-08-15 20:34:35 +01001161 }
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07001162
1163 if (env->longest_mark_read_walk < cnt)
1164 env->longest_mark_read_walk = cnt;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001165 return 0;
Edward Creedc503a82017-08-15 20:34:35 +01001166}
1167
1168static int check_reg_arg(struct bpf_verifier_env *env, u32 regno,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001169 enum reg_arg_type t)
1170{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001171 struct bpf_verifier_state *vstate = env->cur_state;
1172 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Jiong Wangc342dc12019-04-12 22:59:37 +01001173 struct bpf_reg_state *reg, *regs = state->regs;
Edward Creedc503a82017-08-15 20:34:35 +01001174
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001175 if (regno >= MAX_BPF_REG) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001176 verbose(env, "R%d is invalid\n", regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001177 return -EINVAL;
1178 }
1179
Jiong Wangc342dc12019-04-12 22:59:37 +01001180 reg = &regs[regno];
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001181 if (t == SRC_OP) {
1182 /* check whether register used as source operand can be read */
Jiong Wangc342dc12019-04-12 22:59:37 +01001183 if (reg->type == NOT_INIT) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001184 verbose(env, "R%d !read_ok\n", regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001185 return -EACCES;
1186 }
Edward Cree679c7822018-08-22 20:02:19 +01001187 /* We don't need to worry about FP liveness because it's read-only */
Jiong Wangc342dc12019-04-12 22:59:37 +01001188 if (regno == BPF_REG_FP)
1189 return 0;
1190
1191 return mark_reg_read(env, reg, reg->parent);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001192 } else {
1193 /* check whether register used as dest operand can be written to */
1194 if (regno == BPF_REG_FP) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001195 verbose(env, "frame pointer is read only\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001196 return -EACCES;
1197 }
Jiong Wangc342dc12019-04-12 22:59:37 +01001198 reg->live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001199 if (t == DST_OP)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001200 mark_reg_unknown(env, regs, regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001201 }
1202 return 0;
1203}
1204
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001205static bool is_spillable_regtype(enum bpf_reg_type type)
1206{
1207 switch (type) {
1208 case PTR_TO_MAP_VALUE:
1209 case PTR_TO_MAP_VALUE_OR_NULL:
1210 case PTR_TO_STACK:
1211 case PTR_TO_CTX:
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001212 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001213 case PTR_TO_PACKET_META:
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001214 case PTR_TO_PACKET_END:
Petar Penkovd58e4682018-09-14 07:46:18 -07001215 case PTR_TO_FLOW_KEYS:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001216 case CONST_PTR_TO_MAP:
Joe Stringerc64b7982018-10-02 13:35:33 -07001217 case PTR_TO_SOCKET:
1218 case PTR_TO_SOCKET_OR_NULL:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001219 case PTR_TO_SOCK_COMMON:
1220 case PTR_TO_SOCK_COMMON_OR_NULL:
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08001221 case PTR_TO_TCP_SOCK:
1222 case PTR_TO_TCP_SOCK_OR_NULL:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001223 return true;
1224 default:
1225 return false;
1226 }
1227}
1228
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001229/* Does this register contain a constant zero? */
1230static bool register_is_null(struct bpf_reg_state *reg)
1231{
1232 return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0);
1233}
1234
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001235/* check_stack_read/write functions track spill/fill of registers,
1236 * stack boundary and alignment are checked in check_mem_access()
1237 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001238static int check_stack_write(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001239 struct bpf_func_state *state, /* func where register points to */
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07001240 int off, int size, int value_regno, int insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001241{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001242 struct bpf_func_state *cur; /* state of the current function */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001243 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001244 enum bpf_reg_type type;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001245
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001246 err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE),
Joe Stringerfd978bf72018-10-02 13:35:35 -07001247 state->acquired_refs, true);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001248 if (err)
1249 return err;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001250 /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
1251 * so it's aligned access and [off, off + size) are within stack limits
1252 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001253 if (!env->allow_ptr_leaks &&
1254 state->stack[spi].slot_type[0] == STACK_SPILL &&
1255 size != BPF_REG_SIZE) {
1256 verbose(env, "attempt to corrupt spilled pointer on stack\n");
1257 return -EACCES;
1258 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001259
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001260 cur = env->cur_state->frame[env->cur_state->curframe];
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001261 if (value_regno >= 0 &&
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001262 is_spillable_regtype((type = cur->regs[value_regno].type))) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001263
1264 /* register containing pointer is being spilled into stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001265 if (size != BPF_REG_SIZE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001266 verbose(env, "invalid size of register spill\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001267 return -EACCES;
1268 }
1269
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001270 if (state != cur && type == PTR_TO_STACK) {
1271 verbose(env, "cannot spill pointers to stack into stack frame of the caller\n");
1272 return -EINVAL;
1273 }
1274
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001275 /* save register state */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001276 state->stack[spi].spilled_ptr = cur->regs[value_regno];
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001277 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001278
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07001279 for (i = 0; i < BPF_REG_SIZE; i++) {
1280 if (state->stack[spi].slot_type[i] == STACK_MISC &&
1281 !env->allow_ptr_leaks) {
1282 int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off;
1283 int soff = (-spi - 1) * BPF_REG_SIZE;
1284
1285 /* detected reuse of integer stack slot with a pointer
1286 * which means either llvm is reusing stack slot or
1287 * an attacker is trying to exploit CVE-2018-3639
1288 * (speculative store bypass)
1289 * Have to sanitize that slot with preemptive
1290 * store of zero.
1291 */
1292 if (*poff && *poff != soff) {
1293 /* disallow programs where single insn stores
1294 * into two different stack slots, since verifier
1295 * cannot sanitize them
1296 */
1297 verbose(env,
1298 "insn %d cannot access two stack slots fp%d and fp%d",
1299 insn_idx, *poff, soff);
1300 return -EINVAL;
1301 }
1302 *poff = soff;
1303 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001304 state->stack[spi].slot_type[i] = STACK_SPILL;
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07001305 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001306 } else {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001307 u8 type = STACK_MISC;
1308
Edward Cree679c7822018-08-22 20:02:19 +01001309 /* regular write of data into stack destroys any spilled ptr */
1310 state->stack[spi].spilled_ptr.type = NOT_INIT;
Jiong Wang0bae2d42018-12-15 03:34:40 -05001311 /* Mark slots as STACK_MISC if they belonged to spilled ptr. */
1312 if (state->stack[spi].slot_type[0] == STACK_SPILL)
1313 for (i = 0; i < BPF_REG_SIZE; i++)
1314 state->stack[spi].slot_type[i] = STACK_MISC;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001315
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001316 /* only mark the slot as written if all 8 bytes were written
1317 * otherwise read propagation may incorrectly stop too soon
1318 * when stack slots are partially written.
1319 * This heuristic means that read propagation will be
1320 * conservative, since it will add reg_live_read marks
1321 * to stack slots all the way to first state when programs
1322 * writes+reads less than 8 bytes
1323 */
1324 if (size == BPF_REG_SIZE)
1325 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
1326
1327 /* when we zero initialize stack slots mark them as such */
1328 if (value_regno >= 0 &&
1329 register_is_null(&cur->regs[value_regno]))
1330 type = STACK_ZERO;
1331
Jiong Wang0bae2d42018-12-15 03:34:40 -05001332 /* Mark slots affected by this stack write. */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001333 for (i = 0; i < size; i++)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001334 state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] =
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001335 type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001336 }
1337 return 0;
1338}
1339
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001340static int check_stack_read(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001341 struct bpf_func_state *reg_state /* func where register points to */,
1342 int off, int size, int value_regno)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001343{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001344 struct bpf_verifier_state *vstate = env->cur_state;
1345 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001346 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE;
1347 u8 *stype;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001348
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001349 if (reg_state->allocated_stack <= slot) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001350 verbose(env, "invalid read from stack off %d+0 size %d\n",
1351 off, size);
1352 return -EACCES;
1353 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001354 stype = reg_state->stack[spi].slot_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001355
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001356 if (stype[0] == STACK_SPILL) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001357 if (size != BPF_REG_SIZE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001358 verbose(env, "invalid size of register spill\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001359 return -EACCES;
1360 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001361 for (i = 1; i < BPF_REG_SIZE; i++) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001362 if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001363 verbose(env, "corrupted spill memory\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001364 return -EACCES;
1365 }
1366 }
1367
Edward Creedc503a82017-08-15 20:34:35 +01001368 if (value_regno >= 0) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001369 /* restore register state from stack */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001370 state->regs[value_regno] = reg_state->stack[spi].spilled_ptr;
Alexei Starovoitov2f18f622017-11-30 21:31:38 -08001371 /* mark reg as written since spilled pointer state likely
1372 * has its liveness marks cleared by is_state_visited()
1373 * which resets stack/reg liveness for state transitions
1374 */
1375 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
Edward Creedc503a82017-08-15 20:34:35 +01001376 }
Edward Cree679c7822018-08-22 20:02:19 +01001377 mark_reg_read(env, &reg_state->stack[spi].spilled_ptr,
1378 reg_state->stack[spi].spilled_ptr.parent);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001379 return 0;
1380 } else {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001381 int zeros = 0;
1382
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001383 for (i = 0; i < size; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001384 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC)
1385 continue;
1386 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) {
1387 zeros++;
1388 continue;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001389 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001390 verbose(env, "invalid read from stack off %d+%d size %d\n",
1391 off, i, size);
1392 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001393 }
Edward Cree679c7822018-08-22 20:02:19 +01001394 mark_reg_read(env, &reg_state->stack[spi].spilled_ptr,
1395 reg_state->stack[spi].spilled_ptr.parent);
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001396 if (value_regno >= 0) {
1397 if (zeros == size) {
1398 /* any size read into register is zero extended,
1399 * so the whole register == const_zero
1400 */
1401 __mark_reg_const_zero(&state->regs[value_regno]);
1402 } else {
1403 /* have read misc data from the stack */
1404 mark_reg_unknown(env, state->regs, value_regno);
1405 }
1406 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
1407 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001408 return 0;
1409 }
1410}
1411
Daniel Borkmanne4298d22019-01-03 00:58:31 +01001412static int check_stack_access(struct bpf_verifier_env *env,
1413 const struct bpf_reg_state *reg,
1414 int off, int size)
1415{
1416 /* Stack accesses must be at a fixed offset, so that we
1417 * can determine what type of data were returned. See
1418 * check_stack_read().
1419 */
1420 if (!tnum_is_const(reg->var_off)) {
1421 char tn_buf[48];
1422
1423 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Andrey Ignatov1fbd20f2019-04-03 23:22:43 -07001424 verbose(env, "variable stack access var_off=%s off=%d size=%d\n",
Daniel Borkmanne4298d22019-01-03 00:58:31 +01001425 tn_buf, off, size);
1426 return -EACCES;
1427 }
1428
1429 if (off >= 0 || off < -MAX_BPF_STACK) {
1430 verbose(env, "invalid stack off=%d size=%d\n", off, size);
1431 return -EACCES;
1432 }
1433
1434 return 0;
1435}
1436
Daniel Borkmann591fe982019-04-09 23:20:05 +02001437static int check_map_access_type(struct bpf_verifier_env *env, u32 regno,
1438 int off, int size, enum bpf_access_type type)
1439{
1440 struct bpf_reg_state *regs = cur_regs(env);
1441 struct bpf_map *map = regs[regno].map_ptr;
1442 u32 cap = bpf_map_flags_to_cap(map);
1443
1444 if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) {
1445 verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n",
1446 map->value_size, off, size);
1447 return -EACCES;
1448 }
1449
1450 if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) {
1451 verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n",
1452 map->value_size, off, size);
1453 return -EACCES;
1454 }
1455
1456 return 0;
1457}
1458
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001459/* check read/write into map element returned by bpf_map_lookup_elem() */
Edward Creef1174f72017-08-07 15:26:19 +01001460static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001461 int size, bool zero_size_allowed)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001462{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001463 struct bpf_reg_state *regs = cur_regs(env);
1464 struct bpf_map *map = regs[regno].map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001465
Yonghong Song9fd29c02017-11-12 14:49:09 -08001466 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
1467 off + size > map->value_size) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001468 verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001469 map->value_size, off, size);
1470 return -EACCES;
1471 }
1472 return 0;
1473}
1474
Edward Creef1174f72017-08-07 15:26:19 +01001475/* check read/write into a map element with possible variable offset */
1476static int check_map_access(struct bpf_verifier_env *env, u32 regno,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001477 int off, int size, bool zero_size_allowed)
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001478{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001479 struct bpf_verifier_state *vstate = env->cur_state;
1480 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001481 struct bpf_reg_state *reg = &state->regs[regno];
1482 int err;
1483
Edward Creef1174f72017-08-07 15:26:19 +01001484 /* We may have adjusted the register to this map value, so we
1485 * need to try adding each of min_value and max_value to off
1486 * to make sure our theoretical access will be safe.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001487 */
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07001488 if (env->log.level & BPF_LOG_LEVEL)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001489 print_verifier_state(env, state);
Daniel Borkmannb7137c42019-01-03 00:58:33 +01001490
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001491 /* The minimum value is only important with signed
1492 * comparisons where we can't assume the floor of a
1493 * value is 0. If we are using signed variables for our
1494 * index'es we need to make sure that whatever we use
1495 * will have a set floor within our range.
1496 */
Daniel Borkmannb7137c42019-01-03 00:58:33 +01001497 if (reg->smin_value < 0 &&
1498 (reg->smin_value == S64_MIN ||
1499 (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) ||
1500 reg->smin_value + off < 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001501 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 -08001502 regno);
1503 return -EACCES;
1504 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001505 err = __check_map_access(env, regno, reg->smin_value + off, size,
1506 zero_size_allowed);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001507 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001508 verbose(env, "R%d min value is outside of the array range\n",
1509 regno);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001510 return err;
1511 }
1512
Edward Creeb03c9f92017-08-07 15:26:36 +01001513 /* If we haven't set a max value then we need to bail since we can't be
1514 * sure we won't do bad things.
1515 * If reg->umax_value + off could overflow, treat that as unbounded too.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001516 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001517 if (reg->umax_value >= BPF_MAX_VAR_OFF) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001518 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 -08001519 regno);
1520 return -EACCES;
1521 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001522 err = __check_map_access(env, regno, reg->umax_value + off, size,
1523 zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01001524 if (err)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001525 verbose(env, "R%d max value is outside of the array range\n",
1526 regno);
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08001527
1528 if (map_value_has_spin_lock(reg->map_ptr)) {
1529 u32 lock = reg->map_ptr->spin_lock_off;
1530
1531 /* if any part of struct bpf_spin_lock can be touched by
1532 * load/store reject this program.
1533 * To check that [x1, x2) overlaps with [y1, y2)
1534 * it is sufficient to check x1 < y2 && y1 < x2.
1535 */
1536 if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) &&
1537 lock < reg->umax_value + off + size) {
1538 verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n");
1539 return -EACCES;
1540 }
1541 }
Edward Creef1174f72017-08-07 15:26:19 +01001542 return err;
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001543}
1544
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001545#define MAX_PACKET_OFF 0xffff
1546
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001547static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001548 const struct bpf_call_arg_meta *meta,
1549 enum bpf_access_type t)
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001550{
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001551 switch (env->prog->type) {
Daniel Borkmann5d66fa72018-10-24 22:05:45 +02001552 /* Program types only with direct read access go here! */
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001553 case BPF_PROG_TYPE_LWT_IN:
1554 case BPF_PROG_TYPE_LWT_OUT:
Mathieu Xhonneux004d4b22018-05-20 14:58:16 +01001555 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07001556 case BPF_PROG_TYPE_SK_REUSEPORT:
Daniel Borkmann5d66fa72018-10-24 22:05:45 +02001557 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Daniel Borkmannd5563d32018-10-24 22:05:46 +02001558 case BPF_PROG_TYPE_CGROUP_SKB:
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001559 if (t == BPF_WRITE)
1560 return false;
Alexander Alemayhu7e57fbb2017-02-14 00:02:35 +01001561 /* fallthrough */
Daniel Borkmann5d66fa72018-10-24 22:05:45 +02001562
1563 /* Program types with direct read + write access go here! */
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001564 case BPF_PROG_TYPE_SCHED_CLS:
1565 case BPF_PROG_TYPE_SCHED_ACT:
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001566 case BPF_PROG_TYPE_XDP:
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001567 case BPF_PROG_TYPE_LWT_XMIT:
John Fastabend8a31db52017-08-15 22:33:09 -07001568 case BPF_PROG_TYPE_SK_SKB:
John Fastabend4f738ad2018-03-18 12:57:10 -07001569 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001570 if (meta)
1571 return meta->pkt_access;
1572
1573 env->seen_direct_write = true;
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001574 return true;
1575 default:
1576 return false;
1577 }
1578}
1579
Edward Creef1174f72017-08-07 15:26:19 +01001580static int __check_packet_access(struct bpf_verifier_env *env, u32 regno,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001581 int off, int size, bool zero_size_allowed)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001582{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001583 struct bpf_reg_state *regs = cur_regs(env);
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001584 struct bpf_reg_state *reg = &regs[regno];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001585
Yonghong Song9fd29c02017-11-12 14:49:09 -08001586 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
1587 (u64)off + size > reg->range) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001588 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 -07001589 off, size, regno, reg->id, reg->off, reg->range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001590 return -EACCES;
1591 }
1592 return 0;
1593}
1594
Edward Creef1174f72017-08-07 15:26:19 +01001595static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001596 int size, bool zero_size_allowed)
Edward Creef1174f72017-08-07 15:26:19 +01001597{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001598 struct bpf_reg_state *regs = cur_regs(env);
Edward Creef1174f72017-08-07 15:26:19 +01001599 struct bpf_reg_state *reg = &regs[regno];
1600 int err;
1601
1602 /* We may have added a variable offset to the packet pointer; but any
1603 * reg->range we have comes after that. We are only checking the fixed
1604 * offset.
1605 */
1606
1607 /* We don't allow negative numbers, because we aren't tracking enough
1608 * detail to prove they're safe.
1609 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001610 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001611 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 +01001612 regno);
1613 return -EACCES;
1614 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001615 err = __check_packet_access(env, regno, off, size, zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01001616 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001617 verbose(env, "R%d offset is outside of the packet\n", regno);
Edward Creef1174f72017-08-07 15:26:19 +01001618 return err;
1619 }
Jiong Wange6478152018-11-08 04:08:42 -05001620
1621 /* __check_packet_access has made sure "off + size - 1" is within u16.
1622 * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff,
1623 * otherwise find_good_pkt_pointers would have refused to set range info
1624 * that __check_packet_access would have rejected this pkt access.
1625 * Therefore, "off + reg->umax_value + size - 1" won't overflow u32.
1626 */
1627 env->prog->aux->max_pkt_offset =
1628 max_t(u32, env->prog->aux->max_pkt_offset,
1629 off + reg->umax_value + size - 1);
1630
Edward Creef1174f72017-08-07 15:26:19 +01001631 return err;
1632}
1633
1634/* check access to 'struct bpf_context' fields. Supports fixed offsets only */
Yonghong Song31fd8582017-06-13 15:52:13 -07001635static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07001636 enum bpf_access_type t, enum bpf_reg_type *reg_type)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001637{
Daniel Borkmannf96da092017-07-02 02:13:27 +02001638 struct bpf_insn_access_aux info = {
1639 .reg_type = *reg_type,
1640 };
Yonghong Song31fd8582017-06-13 15:52:13 -07001641
Jakub Kicinski4f9218a2017-10-16 16:40:55 -07001642 if (env->ops->is_valid_access &&
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001643 env->ops->is_valid_access(off, size, t, env->prog, &info)) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02001644 /* A non zero info.ctx_field_size indicates that this field is a
1645 * candidate for later verifier transformation to load the whole
1646 * field and then apply a mask when accessed with a narrower
1647 * access than actual ctx access size. A zero info.ctx_field_size
1648 * will only allow for whole field access and rejects any other
1649 * type of narrower access.
Yonghong Song31fd8582017-06-13 15:52:13 -07001650 */
Yonghong Song23994632017-06-22 15:07:39 -07001651 *reg_type = info.reg_type;
Yonghong Song31fd8582017-06-13 15:52:13 -07001652
Jakub Kicinski4f9218a2017-10-16 16:40:55 -07001653 env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07001654 /* remember the offset of last byte accessed in ctx */
1655 if (env->prog->aux->max_ctx_offset < off + size)
1656 env->prog->aux->max_ctx_offset = off + size;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001657 return 0;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07001658 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001659
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001660 verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001661 return -EACCES;
1662}
1663
Petar Penkovd58e4682018-09-14 07:46:18 -07001664static int check_flow_keys_access(struct bpf_verifier_env *env, int off,
1665 int size)
1666{
1667 if (size < 0 || off < 0 ||
1668 (u64)off + size > sizeof(struct bpf_flow_keys)) {
1669 verbose(env, "invalid access to flow keys off=%d size=%d\n",
1670 off, size);
1671 return -EACCES;
1672 }
1673 return 0;
1674}
1675
Martin KaFai Lau5f456642019-02-08 22:25:54 -08001676static int check_sock_access(struct bpf_verifier_env *env, int insn_idx,
1677 u32 regno, int off, int size,
1678 enum bpf_access_type t)
Joe Stringerc64b7982018-10-02 13:35:33 -07001679{
1680 struct bpf_reg_state *regs = cur_regs(env);
1681 struct bpf_reg_state *reg = &regs[regno];
Martin KaFai Lau5f456642019-02-08 22:25:54 -08001682 struct bpf_insn_access_aux info = {};
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001683 bool valid;
Joe Stringerc64b7982018-10-02 13:35:33 -07001684
1685 if (reg->smin_value < 0) {
1686 verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
1687 regno);
1688 return -EACCES;
1689 }
1690
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001691 switch (reg->type) {
1692 case PTR_TO_SOCK_COMMON:
1693 valid = bpf_sock_common_is_valid_access(off, size, t, &info);
1694 break;
1695 case PTR_TO_SOCKET:
1696 valid = bpf_sock_is_valid_access(off, size, t, &info);
1697 break;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08001698 case PTR_TO_TCP_SOCK:
1699 valid = bpf_tcp_sock_is_valid_access(off, size, t, &info);
1700 break;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001701 default:
1702 valid = false;
Joe Stringerc64b7982018-10-02 13:35:33 -07001703 }
1704
Martin KaFai Lau5f456642019-02-08 22:25:54 -08001705
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001706 if (valid) {
1707 env->insn_aux_data[insn_idx].ctx_field_size =
1708 info.ctx_field_size;
1709 return 0;
1710 }
1711
1712 verbose(env, "R%d invalid %s access off=%d size=%d\n",
1713 regno, reg_type_str[reg->type], off, size);
1714
1715 return -EACCES;
Joe Stringerc64b7982018-10-02 13:35:33 -07001716}
1717
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001718static bool __is_pointer_value(bool allow_ptr_leaks,
1719 const struct bpf_reg_state *reg)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001720{
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001721 if (allow_ptr_leaks)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001722 return false;
1723
Edward Creef1174f72017-08-07 15:26:19 +01001724 return reg->type != SCALAR_VALUE;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001725}
1726
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001727static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno)
1728{
1729 return cur_regs(env) + regno;
1730}
1731
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001732static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
1733{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001734 return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno));
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001735}
1736
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01001737static bool is_ctx_reg(struct bpf_verifier_env *env, int regno)
1738{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001739 const struct bpf_reg_state *reg = reg_state(env, regno);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01001740
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001741 return reg->type == PTR_TO_CTX;
1742}
1743
1744static bool is_sk_reg(struct bpf_verifier_env *env, int regno)
1745{
1746 const struct bpf_reg_state *reg = reg_state(env, regno);
1747
1748 return type_is_sk_pointer(reg->type);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01001749}
1750
Daniel Borkmannca369602018-02-23 22:29:05 +01001751static bool is_pkt_reg(struct bpf_verifier_env *env, int regno)
1752{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001753 const struct bpf_reg_state *reg = reg_state(env, regno);
Daniel Borkmannca369602018-02-23 22:29:05 +01001754
1755 return type_is_pkt_pointer(reg->type);
1756}
1757
Daniel Borkmann4b5defd2018-10-21 02:09:25 +02001758static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno)
1759{
1760 const struct bpf_reg_state *reg = reg_state(env, regno);
1761
1762 /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */
1763 return reg->type == PTR_TO_FLOW_KEYS;
1764}
1765
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001766static int check_pkt_ptr_alignment(struct bpf_verifier_env *env,
1767 const struct bpf_reg_state *reg,
David S. Millerd1174412017-05-10 11:22:52 -07001768 int off, int size, bool strict)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001769{
Edward Creef1174f72017-08-07 15:26:19 +01001770 struct tnum reg_off;
David S. Millere07b98d2017-05-10 11:38:07 -07001771 int ip_align;
David S. Millerd1174412017-05-10 11:22:52 -07001772
1773 /* Byte size accesses are always allowed. */
1774 if (!strict || size == 1)
1775 return 0;
1776
David S. Millere4eda882017-05-22 12:27:07 -04001777 /* For platforms that do not have a Kconfig enabling
1778 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of
1779 * NET_IP_ALIGN is universally set to '2'. And on platforms
1780 * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get
1781 * to this code only in strict mode where we want to emulate
1782 * the NET_IP_ALIGN==2 checking. Therefore use an
1783 * unconditional IP align value of '2'.
David S. Millere07b98d2017-05-10 11:38:07 -07001784 */
David S. Millere4eda882017-05-22 12:27:07 -04001785 ip_align = 2;
Edward Creef1174f72017-08-07 15:26:19 +01001786
1787 reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off));
1788 if (!tnum_is_aligned(reg_off, size)) {
1789 char tn_buf[48];
1790
1791 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001792 verbose(env,
1793 "misaligned packet access off %d+%s+%d+%d size %d\n",
Edward Creef1174f72017-08-07 15:26:19 +01001794 ip_align, tn_buf, reg->off, off, size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001795 return -EACCES;
1796 }
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001797
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001798 return 0;
1799}
1800
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001801static int check_generic_ptr_alignment(struct bpf_verifier_env *env,
1802 const struct bpf_reg_state *reg,
Edward Creef1174f72017-08-07 15:26:19 +01001803 const char *pointer_desc,
1804 int off, int size, bool strict)
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001805{
Edward Creef1174f72017-08-07 15:26:19 +01001806 struct tnum reg_off;
1807
1808 /* Byte size accesses are always allowed. */
1809 if (!strict || size == 1)
1810 return 0;
1811
1812 reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off));
1813 if (!tnum_is_aligned(reg_off, size)) {
1814 char tn_buf[48];
1815
1816 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001817 verbose(env, "misaligned %saccess off %s+%d+%d size %d\n",
Edward Creef1174f72017-08-07 15:26:19 +01001818 pointer_desc, tn_buf, reg->off, off, size);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001819 return -EACCES;
1820 }
1821
1822 return 0;
1823}
1824
David S. Millere07b98d2017-05-10 11:38:07 -07001825static int check_ptr_alignment(struct bpf_verifier_env *env,
Daniel Borkmannca369602018-02-23 22:29:05 +01001826 const struct bpf_reg_state *reg, int off,
1827 int size, bool strict_alignment_once)
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001828{
Daniel Borkmannca369602018-02-23 22:29:05 +01001829 bool strict = env->strict_alignment || strict_alignment_once;
Edward Creef1174f72017-08-07 15:26:19 +01001830 const char *pointer_desc = "";
David S. Millerd1174412017-05-10 11:22:52 -07001831
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001832 switch (reg->type) {
1833 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001834 case PTR_TO_PACKET_META:
1835 /* Special case, because of NET_IP_ALIGN. Given metadata sits
1836 * right in front, treat it the very same way.
1837 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001838 return check_pkt_ptr_alignment(env, reg, off, size, strict);
Petar Penkovd58e4682018-09-14 07:46:18 -07001839 case PTR_TO_FLOW_KEYS:
1840 pointer_desc = "flow keys ";
1841 break;
Edward Creef1174f72017-08-07 15:26:19 +01001842 case PTR_TO_MAP_VALUE:
1843 pointer_desc = "value ";
1844 break;
1845 case PTR_TO_CTX:
1846 pointer_desc = "context ";
1847 break;
1848 case PTR_TO_STACK:
1849 pointer_desc = "stack ";
Jann Horna5ec6ae2017-12-18 20:11:58 -08001850 /* The stack spill tracking logic in check_stack_write()
1851 * and check_stack_read() relies on stack accesses being
1852 * aligned.
1853 */
1854 strict = true;
Edward Creef1174f72017-08-07 15:26:19 +01001855 break;
Joe Stringerc64b7982018-10-02 13:35:33 -07001856 case PTR_TO_SOCKET:
1857 pointer_desc = "sock ";
1858 break;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001859 case PTR_TO_SOCK_COMMON:
1860 pointer_desc = "sock_common ";
1861 break;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08001862 case PTR_TO_TCP_SOCK:
1863 pointer_desc = "tcp_sock ";
1864 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001865 default:
Edward Creef1174f72017-08-07 15:26:19 +01001866 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001867 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001868 return check_generic_ptr_alignment(env, reg, pointer_desc, off, size,
1869 strict);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001870}
1871
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001872static int update_stack_depth(struct bpf_verifier_env *env,
1873 const struct bpf_func_state *func,
1874 int off)
1875{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001876 u16 stack = env->subprog_info[func->subprogno].stack_depth;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001877
1878 if (stack >= -off)
1879 return 0;
1880
1881 /* update known max for given subprogram */
Jiong Wang9c8105b2018-05-02 16:17:18 -04001882 env->subprog_info[func->subprogno].stack_depth = -off;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001883 return 0;
1884}
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001885
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001886/* starting from main bpf function walk all instructions of the function
1887 * and recursively walk all callees that given function can call.
1888 * Ignore jump and exit insns.
1889 * Since recursion is prevented by check_cfg() this algorithm
1890 * only needs a local stack of MAX_CALL_FRAMES to remember callsites
1891 */
1892static int check_max_stack_depth(struct bpf_verifier_env *env)
1893{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001894 int depth = 0, frame = 0, idx = 0, i = 0, subprog_end;
1895 struct bpf_subprog_info *subprog = env->subprog_info;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001896 struct bpf_insn *insn = env->prog->insnsi;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001897 int ret_insn[MAX_CALL_FRAMES];
1898 int ret_prog[MAX_CALL_FRAMES];
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001899
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001900process_func:
1901 /* round up to 32-bytes, since this is granularity
1902 * of interpreter stack size
1903 */
Jiong Wang9c8105b2018-05-02 16:17:18 -04001904 depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32);
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001905 if (depth > MAX_BPF_STACK) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001906 verbose(env, "combined stack size of %d calls is %d. Too large\n",
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001907 frame + 1, depth);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001908 return -EACCES;
1909 }
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001910continue_func:
Jiong Wang4cb3d992018-05-02 16:17:19 -04001911 subprog_end = subprog[idx + 1].start;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001912 for (; i < subprog_end; i++) {
1913 if (insn[i].code != (BPF_JMP | BPF_CALL))
1914 continue;
1915 if (insn[i].src_reg != BPF_PSEUDO_CALL)
1916 continue;
1917 /* remember insn and function to return to */
1918 ret_insn[frame] = i + 1;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001919 ret_prog[frame] = idx;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001920
1921 /* find the callee */
1922 i = i + insn[i].imm + 1;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001923 idx = find_subprog(env, i);
1924 if (idx < 0) {
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001925 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
1926 i);
1927 return -EFAULT;
1928 }
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001929 frame++;
1930 if (frame >= MAX_CALL_FRAMES) {
Paul Chaignon927cb782019-03-20 13:58:27 +01001931 verbose(env, "the call stack of %d frames is too deep !\n",
1932 frame);
1933 return -E2BIG;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001934 }
1935 goto process_func;
1936 }
1937 /* end of for() loop means the last insn of the 'subprog'
1938 * was reached. Doesn't matter whether it was JA or EXIT
1939 */
1940 if (frame == 0)
1941 return 0;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001942 depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32);
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001943 frame--;
1944 i = ret_insn[frame];
Jiong Wang9c8105b2018-05-02 16:17:18 -04001945 idx = ret_prog[frame];
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001946 goto continue_func;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001947}
1948
David S. Miller19d28fb2018-01-11 21:27:54 -05001949#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001950static int get_callee_stack_depth(struct bpf_verifier_env *env,
1951 const struct bpf_insn *insn, int idx)
1952{
1953 int start = idx + insn->imm + 1, subprog;
1954
1955 subprog = find_subprog(env, start);
1956 if (subprog < 0) {
1957 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
1958 start);
1959 return -EFAULT;
1960 }
Jiong Wang9c8105b2018-05-02 16:17:18 -04001961 return env->subprog_info[subprog].stack_depth;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001962}
David S. Miller19d28fb2018-01-11 21:27:54 -05001963#endif
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001964
Daniel Borkmann58990d12018-06-07 17:40:03 +02001965static int check_ctx_reg(struct bpf_verifier_env *env,
1966 const struct bpf_reg_state *reg, int regno)
1967{
1968 /* Access to ctx or passing it to a helper is only allowed in
1969 * its original, unmodified form.
1970 */
1971
1972 if (reg->off) {
1973 verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n",
1974 regno, reg->off);
1975 return -EACCES;
1976 }
1977
1978 if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
1979 char tn_buf[48];
1980
1981 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
1982 verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf);
1983 return -EACCES;
1984 }
1985
1986 return 0;
1987}
1988
Matt Mullins9df1c282019-04-26 11:49:47 -07001989static int check_tp_buffer_access(struct bpf_verifier_env *env,
1990 const struct bpf_reg_state *reg,
1991 int regno, int off, int size)
1992{
1993 if (off < 0) {
1994 verbose(env,
1995 "R%d invalid tracepoint buffer access: off=%d, size=%d",
1996 regno, off, size);
1997 return -EACCES;
1998 }
1999 if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
2000 char tn_buf[48];
2001
2002 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2003 verbose(env,
2004 "R%d invalid variable buffer offset: off=%d, var_off=%s",
2005 regno, off, tn_buf);
2006 return -EACCES;
2007 }
2008 if (off + size > env->prog->aux->max_tp_access)
2009 env->prog->aux->max_tp_access = off + size;
2010
2011 return 0;
2012}
2013
2014
Jann Horn0c17d1d2017-12-18 20:11:55 -08002015/* truncate register to smaller size (in bytes)
2016 * must be called with size < BPF_REG_SIZE
2017 */
2018static void coerce_reg_to_size(struct bpf_reg_state *reg, int size)
2019{
2020 u64 mask;
2021
2022 /* clear high bits in bit representation */
2023 reg->var_off = tnum_cast(reg->var_off, size);
2024
2025 /* fix arithmetic bounds */
2026 mask = ((u64)1 << (size * 8)) - 1;
2027 if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) {
2028 reg->umin_value &= mask;
2029 reg->umax_value &= mask;
2030 } else {
2031 reg->umin_value = 0;
2032 reg->umax_value = mask;
2033 }
2034 reg->smin_value = reg->umin_value;
2035 reg->smax_value = reg->umax_value;
2036}
2037
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002038/* check whether memory at (regno + off) is accessible for t = (read | write)
2039 * if t==write, value_regno is a register which value is stored into memory
2040 * if t==read, value_regno is a register which will receive the value from memory
2041 * if t==write && value_regno==-1, some unknown value is stored into memory
2042 * if t==read && value_regno==-1, don't care what we read from memory
2043 */
Daniel Borkmannca369602018-02-23 22:29:05 +01002044static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno,
2045 int off, int bpf_size, enum bpf_access_type t,
2046 int value_regno, bool strict_alignment_once)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002047{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002048 struct bpf_reg_state *regs = cur_regs(env);
2049 struct bpf_reg_state *reg = regs + regno;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002050 struct bpf_func_state *state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002051 int size, err = 0;
2052
2053 size = bpf_size_to_bytes(bpf_size);
2054 if (size < 0)
2055 return size;
2056
Edward Creef1174f72017-08-07 15:26:19 +01002057 /* alignment checks will add in reg->off themselves */
Daniel Borkmannca369602018-02-23 22:29:05 +01002058 err = check_ptr_alignment(env, reg, off, size, strict_alignment_once);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002059 if (err)
2060 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002061
Edward Creef1174f72017-08-07 15:26:19 +01002062 /* for access checks, reg->off is just part of off */
2063 off += reg->off;
2064
2065 if (reg->type == PTR_TO_MAP_VALUE) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002066 if (t == BPF_WRITE && value_regno >= 0 &&
2067 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002068 verbose(env, "R%d leaks addr into map\n", value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002069 return -EACCES;
2070 }
Daniel Borkmann591fe982019-04-09 23:20:05 +02002071 err = check_map_access_type(env, regno, off, size, t);
2072 if (err)
2073 return err;
Yonghong Song9fd29c02017-11-12 14:49:09 -08002074 err = check_map_access(env, regno, off, size, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002075 if (!err && t == BPF_READ && value_regno >= 0)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002076 mark_reg_unknown(env, regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002077
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002078 } else if (reg->type == PTR_TO_CTX) {
Edward Creef1174f72017-08-07 15:26:19 +01002079 enum bpf_reg_type reg_type = SCALAR_VALUE;
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07002080
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002081 if (t == BPF_WRITE && value_regno >= 0 &&
2082 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002083 verbose(env, "R%d leaks addr into ctx\n", value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002084 return -EACCES;
2085 }
Edward Creef1174f72017-08-07 15:26:19 +01002086
Daniel Borkmann58990d12018-06-07 17:40:03 +02002087 err = check_ctx_reg(env, reg, regno);
2088 if (err < 0)
2089 return err;
2090
Yonghong Song31fd8582017-06-13 15:52:13 -07002091 err = check_ctx_access(env, insn_idx, off, size, t, &reg_type);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002092 if (!err && t == BPF_READ && value_regno >= 0) {
Edward Creef1174f72017-08-07 15:26:19 +01002093 /* ctx access returns either a scalar, or a
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002094 * PTR_TO_PACKET[_META,_END]. In the latter
2095 * case, we know the offset is zero.
Edward Creef1174f72017-08-07 15:26:19 +01002096 */
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002097 if (reg_type == SCALAR_VALUE) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002098 mark_reg_unknown(env, regs, value_regno);
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002099 } else {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002100 mark_reg_known_zero(env, regs,
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002101 value_regno);
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002102 if (reg_type_may_be_null(reg_type))
2103 regs[value_regno].id = ++env->id_gen;
2104 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002105 regs[value_regno].type = reg_type;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002106 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002107
Edward Creef1174f72017-08-07 15:26:19 +01002108 } else if (reg->type == PTR_TO_STACK) {
Edward Creef1174f72017-08-07 15:26:19 +01002109 off += reg->var_off.value;
Daniel Borkmanne4298d22019-01-03 00:58:31 +01002110 err = check_stack_access(env, reg, off, size);
2111 if (err)
2112 return err;
Alexei Starovoitov87266792017-05-30 13:31:29 -07002113
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002114 state = func(env, reg);
2115 err = update_stack_depth(env, state, off);
2116 if (err)
2117 return err;
Alexei Starovoitov87266792017-05-30 13:31:29 -07002118
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002119 if (t == BPF_WRITE)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002120 err = check_stack_write(env, state, off, size,
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07002121 value_regno, insn_idx);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002122 else
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002123 err = check_stack_read(env, state, off, size,
2124 value_regno);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002125 } else if (reg_is_pkt_pointer(reg)) {
Thomas Graf3a0af8f2016-11-30 17:10:10 +01002126 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002127 verbose(env, "cannot write into packet\n");
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002128 return -EACCES;
2129 }
Brenden Blanco4acf6c02016-07-19 12:16:56 -07002130 if (t == BPF_WRITE && value_regno >= 0 &&
2131 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002132 verbose(env, "R%d leaks addr into packet\n",
2133 value_regno);
Brenden Blanco4acf6c02016-07-19 12:16:56 -07002134 return -EACCES;
2135 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08002136 err = check_packet_access(env, regno, off, size, false);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002137 if (!err && t == BPF_READ && value_regno >= 0)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002138 mark_reg_unknown(env, regs, value_regno);
Petar Penkovd58e4682018-09-14 07:46:18 -07002139 } else if (reg->type == PTR_TO_FLOW_KEYS) {
2140 if (t == BPF_WRITE && value_regno >= 0 &&
2141 is_pointer_value(env, value_regno)) {
2142 verbose(env, "R%d leaks addr into flow keys\n",
2143 value_regno);
2144 return -EACCES;
2145 }
2146
2147 err = check_flow_keys_access(env, off, size);
2148 if (!err && t == BPF_READ && value_regno >= 0)
2149 mark_reg_unknown(env, regs, value_regno);
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002150 } else if (type_is_sk_pointer(reg->type)) {
Joe Stringerc64b7982018-10-02 13:35:33 -07002151 if (t == BPF_WRITE) {
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002152 verbose(env, "R%d cannot write into %s\n",
2153 regno, reg_type_str[reg->type]);
Joe Stringerc64b7982018-10-02 13:35:33 -07002154 return -EACCES;
2155 }
Martin KaFai Lau5f456642019-02-08 22:25:54 -08002156 err = check_sock_access(env, insn_idx, regno, off, size, t);
Joe Stringerc64b7982018-10-02 13:35:33 -07002157 if (!err && value_regno >= 0)
2158 mark_reg_unknown(env, regs, value_regno);
Matt Mullins9df1c282019-04-26 11:49:47 -07002159 } else if (reg->type == PTR_TO_TP_BUFFER) {
2160 err = check_tp_buffer_access(env, reg, regno, off, size);
2161 if (!err && t == BPF_READ && value_regno >= 0)
2162 mark_reg_unknown(env, regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002163 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002164 verbose(env, "R%d invalid mem access '%s'\n", regno,
2165 reg_type_str[reg->type]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002166 return -EACCES;
2167 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002168
Edward Creef1174f72017-08-07 15:26:19 +01002169 if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ &&
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002170 regs[value_regno].type == SCALAR_VALUE) {
Edward Creef1174f72017-08-07 15:26:19 +01002171 /* b/h/w load zero-extends, mark upper bits as known 0 */
Jann Horn0c17d1d2017-12-18 20:11:55 -08002172 coerce_reg_to_size(&regs[value_regno], size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002173 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002174 return err;
2175}
2176
Yonghong Song31fd8582017-06-13 15:52:13 -07002177static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002178{
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002179 int err;
2180
2181 if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
2182 insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002183 verbose(env, "BPF_XADD uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002184 return -EINVAL;
2185 }
2186
2187 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01002188 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002189 if (err)
2190 return err;
2191
2192 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01002193 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002194 if (err)
2195 return err;
2196
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +02002197 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002198 verbose(env, "R%d leaks addr into mem\n", insn->src_reg);
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +02002199 return -EACCES;
2200 }
2201
Daniel Borkmannca369602018-02-23 22:29:05 +01002202 if (is_ctx_reg(env, insn->dst_reg) ||
Daniel Borkmann4b5defd2018-10-21 02:09:25 +02002203 is_pkt_reg(env, insn->dst_reg) ||
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002204 is_flow_key_reg(env, insn->dst_reg) ||
2205 is_sk_reg(env, insn->dst_reg)) {
Daniel Borkmannca369602018-02-23 22:29:05 +01002206 verbose(env, "BPF_XADD stores into R%d %s is not allowed\n",
Daniel Borkmann2a159c62018-10-21 02:09:24 +02002207 insn->dst_reg,
2208 reg_type_str[reg_state(env, insn->dst_reg)->type]);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01002209 return -EACCES;
2210 }
2211
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002212 /* check whether atomic_add can read the memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07002213 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Daniel Borkmannca369602018-02-23 22:29:05 +01002214 BPF_SIZE(insn->code), BPF_READ, -1, true);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002215 if (err)
2216 return err;
2217
2218 /* check whether atomic_add can write into the same memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07002219 return check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Daniel Borkmannca369602018-02-23 22:29:05 +01002220 BPF_SIZE(insn->code), BPF_WRITE, -1, true);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002221}
2222
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002223static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno,
2224 int off, int access_size,
2225 bool zero_size_allowed)
2226{
2227 struct bpf_reg_state *reg = reg_state(env, regno);
2228
2229 if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
2230 access_size < 0 || (access_size == 0 && !zero_size_allowed)) {
2231 if (tnum_is_const(reg->var_off)) {
2232 verbose(env, "invalid stack type R%d off=%d access_size=%d\n",
2233 regno, off, access_size);
2234 } else {
2235 char tn_buf[48];
2236
2237 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2238 verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n",
2239 regno, tn_buf, access_size);
2240 }
2241 return -EACCES;
2242 }
2243 return 0;
2244}
2245
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002246/* when register 'regno' is passed into function that will read 'access_size'
2247 * bytes from that pointer, make sure that it's within stack boundary
Edward Creef1174f72017-08-07 15:26:19 +01002248 * and all elements of stack are initialized.
2249 * Unlike most pointer bounds-checking functions, this one doesn't take an
2250 * 'off' argument, so it has to add in reg->off itself.
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002251 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002252static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
Daniel Borkmann435faee12016-04-13 00:10:51 +02002253 int access_size, bool zero_size_allowed,
2254 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002255{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02002256 struct bpf_reg_state *reg = reg_state(env, regno);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002257 struct bpf_func_state *state = func(env, reg);
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002258 int err, min_off, max_off, i, slot, spi;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002259
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002260 if (reg->type != PTR_TO_STACK) {
Edward Creef1174f72017-08-07 15:26:19 +01002261 /* Allow zero-byte read from NULL, regardless of pointer type */
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002262 if (zero_size_allowed && access_size == 0 &&
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002263 register_is_null(reg))
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002264 return 0;
2265
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002266 verbose(env, "R%d type=%s expected=%s\n", regno,
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002267 reg_type_str[reg->type],
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002268 reg_type_str[PTR_TO_STACK]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002269 return -EACCES;
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002270 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002271
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002272 if (tnum_is_const(reg->var_off)) {
2273 min_off = max_off = reg->var_off.value + reg->off;
2274 err = __check_stack_boundary(env, regno, min_off, access_size,
2275 zero_size_allowed);
2276 if (err)
2277 return err;
2278 } else {
Andrey Ignatov088ec262019-04-03 23:22:39 -07002279 /* Variable offset is prohibited for unprivileged mode for
2280 * simplicity since it requires corresponding support in
2281 * Spectre masking for stack ALU.
2282 * See also retrieve_ptr_limit().
2283 */
2284 if (!env->allow_ptr_leaks) {
2285 char tn_buf[48];
Edward Creef1174f72017-08-07 15:26:19 +01002286
Andrey Ignatov088ec262019-04-03 23:22:39 -07002287 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2288 verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n",
2289 regno, tn_buf);
2290 return -EACCES;
2291 }
Andrey Ignatovf2bcd052019-04-03 23:22:37 -07002292 /* Only initialized buffer on stack is allowed to be accessed
2293 * with variable offset. With uninitialized buffer it's hard to
2294 * guarantee that whole memory is marked as initialized on
2295 * helper return since specific bounds are unknown what may
2296 * cause uninitialized stack leaking.
2297 */
2298 if (meta && meta->raw_mode)
2299 meta = NULL;
2300
Andrey Ignatov107c26a72019-04-03 23:22:41 -07002301 if (reg->smax_value >= BPF_MAX_VAR_OFF ||
2302 reg->smax_value <= -BPF_MAX_VAR_OFF) {
2303 verbose(env, "R%d unbounded indirect variable offset stack access\n",
2304 regno);
2305 return -EACCES;
2306 }
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002307 min_off = reg->smin_value + reg->off;
Andrey Ignatov107c26a72019-04-03 23:22:41 -07002308 max_off = reg->smax_value + reg->off;
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002309 err = __check_stack_boundary(env, regno, min_off, access_size,
2310 zero_size_allowed);
Andrey Ignatov107c26a72019-04-03 23:22:41 -07002311 if (err) {
2312 verbose(env, "R%d min value is outside of stack bound\n",
2313 regno);
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002314 return err;
Andrey Ignatov107c26a72019-04-03 23:22:41 -07002315 }
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002316 err = __check_stack_boundary(env, regno, max_off, access_size,
2317 zero_size_allowed);
Andrey Ignatov107c26a72019-04-03 23:22:41 -07002318 if (err) {
2319 verbose(env, "R%d max value is outside of stack bound\n",
2320 regno);
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002321 return err;
Andrey Ignatov107c26a72019-04-03 23:22:41 -07002322 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002323 }
2324
Daniel Borkmann435faee12016-04-13 00:10:51 +02002325 if (meta && meta->raw_mode) {
2326 meta->access_size = access_size;
2327 meta->regno = regno;
2328 return 0;
2329 }
2330
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002331 for (i = min_off; i < max_off + access_size; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002332 u8 *stype;
2333
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002334 slot = -i - 1;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002335 spi = slot / BPF_REG_SIZE;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002336 if (state->allocated_stack <= slot)
2337 goto err;
2338 stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE];
2339 if (*stype == STACK_MISC)
2340 goto mark;
2341 if (*stype == STACK_ZERO) {
2342 /* helper can write anything into the stack */
2343 *stype = STACK_MISC;
2344 goto mark;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002345 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002346err:
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002347 if (tnum_is_const(reg->var_off)) {
2348 verbose(env, "invalid indirect read from stack off %d+%d size %d\n",
2349 min_off, i - min_off, access_size);
2350 } else {
2351 char tn_buf[48];
2352
2353 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2354 verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n",
2355 tn_buf, i - min_off, access_size);
2356 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002357 return -EACCES;
2358mark:
2359 /* reading any byte out of 8-byte 'spill_slot' will cause
2360 * the whole slot to be marked as 'read'
2361 */
Edward Cree679c7822018-08-22 20:02:19 +01002362 mark_reg_read(env, &state->stack[spi].spilled_ptr,
2363 state->stack[spi].spilled_ptr.parent);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002364 }
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002365 return update_stack_depth(env, state, min_off);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002366}
2367
Gianluca Borello06c1c042017-01-09 10:19:49 -08002368static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
2369 int access_size, bool zero_size_allowed,
2370 struct bpf_call_arg_meta *meta)
2371{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002372 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
Gianluca Borello06c1c042017-01-09 10:19:49 -08002373
Edward Creef1174f72017-08-07 15:26:19 +01002374 switch (reg->type) {
Gianluca Borello06c1c042017-01-09 10:19:49 -08002375 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002376 case PTR_TO_PACKET_META:
Yonghong Song9fd29c02017-11-12 14:49:09 -08002377 return check_packet_access(env, regno, reg->off, access_size,
2378 zero_size_allowed);
Gianluca Borello06c1c042017-01-09 10:19:49 -08002379 case PTR_TO_MAP_VALUE:
Daniel Borkmann591fe982019-04-09 23:20:05 +02002380 if (check_map_access_type(env, regno, reg->off, access_size,
2381 meta && meta->raw_mode ? BPF_WRITE :
2382 BPF_READ))
2383 return -EACCES;
Yonghong Song9fd29c02017-11-12 14:49:09 -08002384 return check_map_access(env, regno, reg->off, access_size,
2385 zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01002386 default: /* scalar_value|ptr_to_stack or invalid ptr */
Gianluca Borello06c1c042017-01-09 10:19:49 -08002387 return check_stack_boundary(env, regno, access_size,
2388 zero_size_allowed, meta);
2389 }
2390}
2391
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08002392/* Implementation details:
2393 * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL
2394 * Two bpf_map_lookups (even with the same key) will have different reg->id.
2395 * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after
2396 * value_or_null->value transition, since the verifier only cares about
2397 * the range of access to valid map value pointer and doesn't care about actual
2398 * address of the map element.
2399 * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps
2400 * reg->id > 0 after value_or_null->value transition. By doing so
2401 * two bpf_map_lookups will be considered two different pointers that
2402 * point to different bpf_spin_locks.
2403 * The verifier allows taking only one bpf_spin_lock at a time to avoid
2404 * dead-locks.
2405 * Since only one bpf_spin_lock is allowed the checks are simpler than
2406 * reg_is_refcounted() logic. The verifier needs to remember only
2407 * one spin_lock instead of array of acquired_refs.
2408 * cur_state->active_spin_lock remembers which map value element got locked
2409 * and clears it after bpf_spin_unlock.
2410 */
2411static int process_spin_lock(struct bpf_verifier_env *env, int regno,
2412 bool is_lock)
2413{
2414 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
2415 struct bpf_verifier_state *cur = env->cur_state;
2416 bool is_const = tnum_is_const(reg->var_off);
2417 struct bpf_map *map = reg->map_ptr;
2418 u64 val = reg->var_off.value;
2419
2420 if (reg->type != PTR_TO_MAP_VALUE) {
2421 verbose(env, "R%d is not a pointer to map_value\n", regno);
2422 return -EINVAL;
2423 }
2424 if (!is_const) {
2425 verbose(env,
2426 "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n",
2427 regno);
2428 return -EINVAL;
2429 }
2430 if (!map->btf) {
2431 verbose(env,
2432 "map '%s' has to have BTF in order to use bpf_spin_lock\n",
2433 map->name);
2434 return -EINVAL;
2435 }
2436 if (!map_value_has_spin_lock(map)) {
2437 if (map->spin_lock_off == -E2BIG)
2438 verbose(env,
2439 "map '%s' has more than one 'struct bpf_spin_lock'\n",
2440 map->name);
2441 else if (map->spin_lock_off == -ENOENT)
2442 verbose(env,
2443 "map '%s' doesn't have 'struct bpf_spin_lock'\n",
2444 map->name);
2445 else
2446 verbose(env,
2447 "map '%s' is not a struct type or bpf_spin_lock is mangled\n",
2448 map->name);
2449 return -EINVAL;
2450 }
2451 if (map->spin_lock_off != val + reg->off) {
2452 verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n",
2453 val + reg->off);
2454 return -EINVAL;
2455 }
2456 if (is_lock) {
2457 if (cur->active_spin_lock) {
2458 verbose(env,
2459 "Locking two bpf_spin_locks are not allowed\n");
2460 return -EINVAL;
2461 }
2462 cur->active_spin_lock = reg->id;
2463 } else {
2464 if (!cur->active_spin_lock) {
2465 verbose(env, "bpf_spin_unlock without taking a lock\n");
2466 return -EINVAL;
2467 }
2468 if (cur->active_spin_lock != reg->id) {
2469 verbose(env, "bpf_spin_unlock of different lock\n");
2470 return -EINVAL;
2471 }
2472 cur->active_spin_lock = 0;
2473 }
2474 return 0;
2475}
2476
Daniel Borkmann90133412018-01-20 01:24:29 +01002477static bool arg_type_is_mem_ptr(enum bpf_arg_type type)
2478{
2479 return type == ARG_PTR_TO_MEM ||
2480 type == ARG_PTR_TO_MEM_OR_NULL ||
2481 type == ARG_PTR_TO_UNINIT_MEM;
2482}
2483
2484static bool arg_type_is_mem_size(enum bpf_arg_type type)
2485{
2486 return type == ARG_CONST_SIZE ||
2487 type == ARG_CONST_SIZE_OR_ZERO;
2488}
2489
Andrey Ignatov57c3bb72019-03-18 16:57:10 -07002490static bool arg_type_is_int_ptr(enum bpf_arg_type type)
2491{
2492 return type == ARG_PTR_TO_INT ||
2493 type == ARG_PTR_TO_LONG;
2494}
2495
2496static int int_ptr_type_to_size(enum bpf_arg_type type)
2497{
2498 if (type == ARG_PTR_TO_INT)
2499 return sizeof(u32);
2500 else if (type == ARG_PTR_TO_LONG)
2501 return sizeof(u64);
2502
2503 return -EINVAL;
2504}
2505
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002506static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002507 enum bpf_arg_type arg_type,
2508 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002509{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002510 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002511 enum bpf_reg_type expected_type, type = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002512 int err = 0;
2513
Daniel Borkmann80f1d682015-03-12 17:21:42 +01002514 if (arg_type == ARG_DONTCARE)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002515 return 0;
2516
Edward Creedc503a82017-08-15 20:34:35 +01002517 err = check_reg_arg(env, regno, SRC_OP);
2518 if (err)
2519 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002520
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002521 if (arg_type == ARG_ANYTHING) {
2522 if (is_pointer_value(env, regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002523 verbose(env, "R%d leaks addr into helper function\n",
2524 regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002525 return -EACCES;
2526 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01002527 return 0;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002528 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01002529
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002530 if (type_is_pkt_pointer(type) &&
Thomas Graf3a0af8f2016-11-30 17:10:10 +01002531 !may_access_direct_pkt_data(env, meta, BPF_READ)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002532 verbose(env, "helper access to the packet is not allowed\n");
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002533 return -EACCES;
2534 }
2535
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002536 if (arg_type == ARG_PTR_TO_MAP_KEY ||
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002537 arg_type == ARG_PTR_TO_MAP_VALUE ||
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -07002538 arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE ||
2539 arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002540 expected_type = PTR_TO_STACK;
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -07002541 if (register_is_null(reg) &&
2542 arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL)
2543 /* final test in check_stack_boundary() */;
2544 else if (!type_is_pkt_pointer(type) &&
2545 type != PTR_TO_MAP_VALUE &&
2546 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002547 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002548 } else if (arg_type == ARG_CONST_SIZE ||
2549 arg_type == ARG_CONST_SIZE_OR_ZERO) {
Edward Creef1174f72017-08-07 15:26:19 +01002550 expected_type = SCALAR_VALUE;
2551 if (type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002552 goto err_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002553 } else if (arg_type == ARG_CONST_MAP_PTR) {
2554 expected_type = CONST_PTR_TO_MAP;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002555 if (type != expected_type)
2556 goto err_type;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002557 } else if (arg_type == ARG_PTR_TO_CTX) {
2558 expected_type = PTR_TO_CTX;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002559 if (type != expected_type)
2560 goto err_type;
Daniel Borkmann58990d12018-06-07 17:40:03 +02002561 err = check_ctx_reg(env, reg, regno);
2562 if (err < 0)
2563 return err;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002564 } else if (arg_type == ARG_PTR_TO_SOCK_COMMON) {
2565 expected_type = PTR_TO_SOCK_COMMON;
2566 /* Any sk pointer can be ARG_PTR_TO_SOCK_COMMON */
2567 if (!type_is_sk_pointer(type))
2568 goto err_type;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002569 if (reg->ref_obj_id) {
2570 if (meta->ref_obj_id) {
2571 verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n",
2572 regno, reg->ref_obj_id,
2573 meta->ref_obj_id);
2574 return -EFAULT;
2575 }
2576 meta->ref_obj_id = reg->ref_obj_id;
Joe Stringerfd978bf72018-10-02 13:35:35 -07002577 }
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -07002578 } else if (arg_type == ARG_PTR_TO_SOCKET) {
2579 expected_type = PTR_TO_SOCKET;
2580 if (type != expected_type)
2581 goto err_type;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08002582 } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) {
2583 if (meta->func_id == BPF_FUNC_spin_lock) {
2584 if (process_spin_lock(env, regno, true))
2585 return -EACCES;
2586 } else if (meta->func_id == BPF_FUNC_spin_unlock) {
2587 if (process_spin_lock(env, regno, false))
2588 return -EACCES;
2589 } else {
2590 verbose(env, "verifier internal error\n");
2591 return -EFAULT;
2592 }
Daniel Borkmann90133412018-01-20 01:24:29 +01002593 } else if (arg_type_is_mem_ptr(arg_type)) {
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002594 expected_type = PTR_TO_STACK;
2595 /* One exception here. In case function allows for NULL to be
Edward Creef1174f72017-08-07 15:26:19 +01002596 * passed in as argument, it's a SCALAR_VALUE type. Final test
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002597 * happens during stack boundary checking.
2598 */
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002599 if (register_is_null(reg) &&
Gianluca Borellodb1ac492017-11-22 18:32:53 +00002600 arg_type == ARG_PTR_TO_MEM_OR_NULL)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002601 /* final test in check_stack_boundary() */;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002602 else if (!type_is_pkt_pointer(type) &&
2603 type != PTR_TO_MAP_VALUE &&
Edward Creef1174f72017-08-07 15:26:19 +01002604 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002605 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002606 meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM;
Andrey Ignatov57c3bb72019-03-18 16:57:10 -07002607 } else if (arg_type_is_int_ptr(arg_type)) {
2608 expected_type = PTR_TO_STACK;
2609 if (!type_is_pkt_pointer(type) &&
2610 type != PTR_TO_MAP_VALUE &&
2611 type != expected_type)
2612 goto err_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002613 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002614 verbose(env, "unsupported arg_type %d\n", arg_type);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002615 return -EFAULT;
2616 }
2617
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002618 if (arg_type == ARG_CONST_MAP_PTR) {
2619 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002620 meta->map_ptr = reg->map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002621 } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
2622 /* bpf_map_xxx(..., map_ptr, ..., key) call:
2623 * check that [key, key + map->key_size) are within
2624 * stack limits and initialized
2625 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002626 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002627 /* in function declaration map_ptr must come before
2628 * map_key, so that it's verified and known before
2629 * we have to check map_key here. Otherwise it means
2630 * that kernel subsystem misconfigured verifier
2631 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002632 verbose(env, "invalid map_ptr to access map->key\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002633 return -EACCES;
2634 }
Paul Chaignond71962f2018-04-24 15:07:54 +02002635 err = check_helper_mem_access(env, regno,
2636 meta->map_ptr->key_size, false,
2637 NULL);
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002638 } else if (arg_type == ARG_PTR_TO_MAP_VALUE ||
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -07002639 (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL &&
2640 !register_is_null(reg)) ||
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002641 arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002642 /* bpf_map_xxx(..., map_ptr, ..., value) call:
2643 * check [value, value + map->value_size) validity
2644 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002645 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002646 /* kernel subsystem misconfigured verifier */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002647 verbose(env, "invalid map_ptr to access map->value\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002648 return -EACCES;
2649 }
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002650 meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE);
Paul Chaignond71962f2018-04-24 15:07:54 +02002651 err = check_helper_mem_access(env, regno,
2652 meta->map_ptr->value_size, false,
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002653 meta);
Daniel Borkmann90133412018-01-20 01:24:29 +01002654 } else if (arg_type_is_mem_size(arg_type)) {
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002655 bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002656
Yonghong Song849fa502018-04-28 22:28:09 -07002657 /* remember the mem_size which may be used later
2658 * to refine return values.
2659 */
2660 meta->msize_smax_value = reg->smax_value;
2661 meta->msize_umax_value = reg->umax_value;
2662
Edward Creef1174f72017-08-07 15:26:19 +01002663 /* The register is SCALAR_VALUE; the access check
2664 * happens using its boundaries.
Gianluca Borello06c1c042017-01-09 10:19:49 -08002665 */
Edward Creef1174f72017-08-07 15:26:19 +01002666 if (!tnum_is_const(reg->var_off))
Gianluca Borello06c1c042017-01-09 10:19:49 -08002667 /* For unprivileged variable accesses, disable raw
2668 * mode so that the program is required to
2669 * initialize all the memory that the helper could
2670 * just partially fill up.
2671 */
2672 meta = NULL;
2673
Edward Creeb03c9f92017-08-07 15:26:36 +01002674 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002675 verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n",
Edward Creef1174f72017-08-07 15:26:19 +01002676 regno);
2677 return -EACCES;
2678 }
Gianluca Borello06c1c042017-01-09 10:19:49 -08002679
Edward Creeb03c9f92017-08-07 15:26:36 +01002680 if (reg->umin_value == 0) {
Edward Creef1174f72017-08-07 15:26:19 +01002681 err = check_helper_mem_access(env, regno - 1, 0,
2682 zero_size_allowed,
2683 meta);
Gianluca Borello06c1c042017-01-09 10:19:49 -08002684 if (err)
2685 return err;
Gianluca Borello06c1c042017-01-09 10:19:49 -08002686 }
Edward Creef1174f72017-08-07 15:26:19 +01002687
Edward Creeb03c9f92017-08-07 15:26:36 +01002688 if (reg->umax_value >= BPF_MAX_VAR_SIZ) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002689 verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
Edward Creef1174f72017-08-07 15:26:19 +01002690 regno);
2691 return -EACCES;
2692 }
2693 err = check_helper_mem_access(env, regno - 1,
Edward Creeb03c9f92017-08-07 15:26:36 +01002694 reg->umax_value,
Edward Creef1174f72017-08-07 15:26:19 +01002695 zero_size_allowed, meta);
Andrey Ignatov57c3bb72019-03-18 16:57:10 -07002696 } else if (arg_type_is_int_ptr(arg_type)) {
2697 int size = int_ptr_type_to_size(arg_type);
2698
2699 err = check_helper_mem_access(env, regno, size, false, meta);
2700 if (err)
2701 return err;
2702 err = check_ptr_alignment(env, reg, 0, size, true);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002703 }
2704
2705 return err;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002706err_type:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002707 verbose(env, "R%d type=%s expected=%s\n", regno,
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002708 reg_type_str[type], reg_type_str[expected_type]);
2709 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002710}
2711
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002712static int check_map_func_compatibility(struct bpf_verifier_env *env,
2713 struct bpf_map *map, int func_id)
Kaixu Xia35578d72015-08-06 07:02:35 +00002714{
Kaixu Xia35578d72015-08-06 07:02:35 +00002715 if (!map)
2716 return 0;
2717
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002718 /* We need a two way check, first is from map perspective ... */
2719 switch (map->map_type) {
2720 case BPF_MAP_TYPE_PROG_ARRAY:
2721 if (func_id != BPF_FUNC_tail_call)
2722 goto error;
2723 break;
2724 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
2725 if (func_id != BPF_FUNC_perf_event_read &&
Yonghong Song908432c2017-10-05 09:19:20 -07002726 func_id != BPF_FUNC_perf_event_output &&
2727 func_id != BPF_FUNC_perf_event_read_value)
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002728 goto error;
2729 break;
2730 case BPF_MAP_TYPE_STACK_TRACE:
2731 if (func_id != BPF_FUNC_get_stackid)
2732 goto error;
2733 break;
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -07002734 case BPF_MAP_TYPE_CGROUP_ARRAY:
David S. Miller60747ef2016-08-18 01:17:32 -04002735 if (func_id != BPF_FUNC_skb_under_cgroup &&
Sargun Dhillon60d20f92016-08-12 08:56:52 -07002736 func_id != BPF_FUNC_current_task_under_cgroup)
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002737 goto error;
2738 break;
Roman Gushchincd339432018-08-02 14:27:24 -07002739 case BPF_MAP_TYPE_CGROUP_STORAGE:
Roman Gushchinb741f162018-09-28 14:45:43 +00002740 case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE:
Roman Gushchincd339432018-08-02 14:27:24 -07002741 if (func_id != BPF_FUNC_get_local_storage)
2742 goto error;
2743 break;
John Fastabend546ac1f2017-07-17 09:28:56 -07002744 /* devmap returns a pointer to a live net_device ifindex that we cannot
2745 * allow to be modified from bpf side. So do not allow lookup elements
2746 * for now.
2747 */
2748 case BPF_MAP_TYPE_DEVMAP:
John Fastabend2ddf71e2017-07-17 09:30:02 -07002749 if (func_id != BPF_FUNC_redirect_map)
John Fastabend546ac1f2017-07-17 09:28:56 -07002750 goto error;
2751 break;
Björn Töpelfbfc504a2018-05-02 13:01:28 +02002752 /* Restrict bpf side of cpumap and xskmap, open when use-cases
2753 * appear.
2754 */
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02002755 case BPF_MAP_TYPE_CPUMAP:
Björn Töpelfbfc504a2018-05-02 13:01:28 +02002756 case BPF_MAP_TYPE_XSKMAP:
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02002757 if (func_id != BPF_FUNC_redirect_map)
2758 goto error;
2759 break;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07002760 case BPF_MAP_TYPE_ARRAY_OF_MAPS:
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07002761 case BPF_MAP_TYPE_HASH_OF_MAPS:
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07002762 if (func_id != BPF_FUNC_map_lookup_elem)
2763 goto error;
Martin KaFai Lau16a43622017-08-17 18:14:43 -07002764 break;
John Fastabend174a79f2017-08-15 22:32:47 -07002765 case BPF_MAP_TYPE_SOCKMAP:
2766 if (func_id != BPF_FUNC_sk_redirect_map &&
2767 func_id != BPF_FUNC_sock_map_update &&
John Fastabend4f738ad2018-03-18 12:57:10 -07002768 func_id != BPF_FUNC_map_delete_elem &&
2769 func_id != BPF_FUNC_msg_redirect_map)
John Fastabend174a79f2017-08-15 22:32:47 -07002770 goto error;
2771 break;
John Fastabend81110382018-05-14 10:00:17 -07002772 case BPF_MAP_TYPE_SOCKHASH:
2773 if (func_id != BPF_FUNC_sk_redirect_hash &&
2774 func_id != BPF_FUNC_sock_hash_update &&
2775 func_id != BPF_FUNC_map_delete_elem &&
2776 func_id != BPF_FUNC_msg_redirect_hash)
2777 goto error;
2778 break;
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07002779 case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY:
2780 if (func_id != BPF_FUNC_sk_select_reuseport)
2781 goto error;
2782 break;
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02002783 case BPF_MAP_TYPE_QUEUE:
2784 case BPF_MAP_TYPE_STACK:
2785 if (func_id != BPF_FUNC_map_peek_elem &&
2786 func_id != BPF_FUNC_map_pop_elem &&
2787 func_id != BPF_FUNC_map_push_elem)
2788 goto error;
2789 break;
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -07002790 case BPF_MAP_TYPE_SK_STORAGE:
2791 if (func_id != BPF_FUNC_sk_storage_get &&
2792 func_id != BPF_FUNC_sk_storage_delete)
2793 goto error;
2794 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002795 default:
2796 break;
2797 }
2798
2799 /* ... and second from the function itself. */
2800 switch (func_id) {
2801 case BPF_FUNC_tail_call:
2802 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
2803 goto error;
Jiong Wangf910cef2018-05-02 16:17:17 -04002804 if (env->subprog_cnt > 1) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002805 verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n");
2806 return -EINVAL;
2807 }
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002808 break;
2809 case BPF_FUNC_perf_event_read:
2810 case BPF_FUNC_perf_event_output:
Yonghong Song908432c2017-10-05 09:19:20 -07002811 case BPF_FUNC_perf_event_read_value:
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002812 if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
2813 goto error;
2814 break;
2815 case BPF_FUNC_get_stackid:
2816 if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
2817 goto error;
2818 break;
Sargun Dhillon60d20f92016-08-12 08:56:52 -07002819 case BPF_FUNC_current_task_under_cgroup:
Daniel Borkmann747ea552016-08-12 22:17:17 +02002820 case BPF_FUNC_skb_under_cgroup:
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002821 if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
2822 goto error;
2823 break;
John Fastabend97f91a72017-07-17 09:29:18 -07002824 case BPF_FUNC_redirect_map:
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02002825 if (map->map_type != BPF_MAP_TYPE_DEVMAP &&
Björn Töpelfbfc504a2018-05-02 13:01:28 +02002826 map->map_type != BPF_MAP_TYPE_CPUMAP &&
2827 map->map_type != BPF_MAP_TYPE_XSKMAP)
John Fastabend97f91a72017-07-17 09:29:18 -07002828 goto error;
2829 break;
John Fastabend174a79f2017-08-15 22:32:47 -07002830 case BPF_FUNC_sk_redirect_map:
John Fastabend4f738ad2018-03-18 12:57:10 -07002831 case BPF_FUNC_msg_redirect_map:
John Fastabend81110382018-05-14 10:00:17 -07002832 case BPF_FUNC_sock_map_update:
John Fastabend174a79f2017-08-15 22:32:47 -07002833 if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
2834 goto error;
2835 break;
John Fastabend81110382018-05-14 10:00:17 -07002836 case BPF_FUNC_sk_redirect_hash:
2837 case BPF_FUNC_msg_redirect_hash:
2838 case BPF_FUNC_sock_hash_update:
2839 if (map->map_type != BPF_MAP_TYPE_SOCKHASH)
John Fastabend174a79f2017-08-15 22:32:47 -07002840 goto error;
2841 break;
Roman Gushchincd339432018-08-02 14:27:24 -07002842 case BPF_FUNC_get_local_storage:
Roman Gushchinb741f162018-09-28 14:45:43 +00002843 if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
2844 map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Roman Gushchincd339432018-08-02 14:27:24 -07002845 goto error;
2846 break;
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07002847 case BPF_FUNC_sk_select_reuseport:
2848 if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY)
2849 goto error;
2850 break;
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02002851 case BPF_FUNC_map_peek_elem:
2852 case BPF_FUNC_map_pop_elem:
2853 case BPF_FUNC_map_push_elem:
2854 if (map->map_type != BPF_MAP_TYPE_QUEUE &&
2855 map->map_type != BPF_MAP_TYPE_STACK)
2856 goto error;
2857 break;
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -07002858 case BPF_FUNC_sk_storage_get:
2859 case BPF_FUNC_sk_storage_delete:
2860 if (map->map_type != BPF_MAP_TYPE_SK_STORAGE)
2861 goto error;
2862 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002863 default:
2864 break;
Kaixu Xia35578d72015-08-06 07:02:35 +00002865 }
2866
2867 return 0;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002868error:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002869 verbose(env, "cannot pass map_type %d into func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02002870 map->map_type, func_id_name(func_id), func_id);
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002871 return -EINVAL;
Kaixu Xia35578d72015-08-06 07:02:35 +00002872}
2873
Daniel Borkmann90133412018-01-20 01:24:29 +01002874static bool check_raw_mode_ok(const struct bpf_func_proto *fn)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002875{
2876 int count = 0;
2877
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002878 if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002879 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002880 if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002881 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002882 if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002883 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002884 if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002885 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002886 if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002887 count++;
2888
Daniel Borkmann90133412018-01-20 01:24:29 +01002889 /* We only support one arg being in raw mode at the moment,
2890 * which is sufficient for the helper functions we have
2891 * right now.
2892 */
2893 return count <= 1;
2894}
2895
2896static bool check_args_pair_invalid(enum bpf_arg_type arg_curr,
2897 enum bpf_arg_type arg_next)
2898{
2899 return (arg_type_is_mem_ptr(arg_curr) &&
2900 !arg_type_is_mem_size(arg_next)) ||
2901 (!arg_type_is_mem_ptr(arg_curr) &&
2902 arg_type_is_mem_size(arg_next));
2903}
2904
2905static bool check_arg_pair_ok(const struct bpf_func_proto *fn)
2906{
2907 /* bpf_xxx(..., buf, len) call will access 'len'
2908 * bytes from memory 'buf'. Both arg types need
2909 * to be paired, so make sure there's no buggy
2910 * helper function specification.
2911 */
2912 if (arg_type_is_mem_size(fn->arg1_type) ||
2913 arg_type_is_mem_ptr(fn->arg5_type) ||
2914 check_args_pair_invalid(fn->arg1_type, fn->arg2_type) ||
2915 check_args_pair_invalid(fn->arg2_type, fn->arg3_type) ||
2916 check_args_pair_invalid(fn->arg3_type, fn->arg4_type) ||
2917 check_args_pair_invalid(fn->arg4_type, fn->arg5_type))
2918 return false;
2919
2920 return true;
2921}
2922
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002923static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07002924{
2925 int count = 0;
2926
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002927 if (arg_type_may_be_refcounted(fn->arg1_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07002928 count++;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002929 if (arg_type_may_be_refcounted(fn->arg2_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07002930 count++;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002931 if (arg_type_may_be_refcounted(fn->arg3_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07002932 count++;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002933 if (arg_type_may_be_refcounted(fn->arg4_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07002934 count++;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002935 if (arg_type_may_be_refcounted(fn->arg5_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07002936 count++;
2937
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002938 /* A reference acquiring function cannot acquire
2939 * another refcounted ptr.
2940 */
2941 if (is_acquire_function(func_id) && count)
2942 return false;
2943
Joe Stringerfd978bf72018-10-02 13:35:35 -07002944 /* We only support one arg being unreferenced at the moment,
2945 * which is sufficient for the helper functions we have right now.
2946 */
2947 return count <= 1;
2948}
2949
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002950static int check_func_proto(const struct bpf_func_proto *fn, int func_id)
Daniel Borkmann90133412018-01-20 01:24:29 +01002951{
2952 return check_raw_mode_ok(fn) &&
Joe Stringerfd978bf72018-10-02 13:35:35 -07002953 check_arg_pair_ok(fn) &&
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002954 check_refcount_ok(fn, func_id) ? 0 : -EINVAL;
Daniel Borkmann435faee12016-04-13 00:10:51 +02002955}
2956
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002957/* Packet data might have moved, any old PTR_TO_PACKET[_META,_END]
2958 * are now invalid, so turn them into unknown SCALAR_VALUE.
Edward Creef1174f72017-08-07 15:26:19 +01002959 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002960static void __clear_all_pkt_pointers(struct bpf_verifier_env *env,
2961 struct bpf_func_state *state)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002962{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002963 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002964 int i;
2965
2966 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002967 if (reg_is_pkt_pointer_any(&regs[i]))
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002968 mark_reg_unknown(env, regs, i);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002969
Joe Stringerf3709f62018-10-02 13:35:29 -07002970 bpf_for_each_spilled_reg(i, state, reg) {
2971 if (!reg)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002972 continue;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002973 if (reg_is_pkt_pointer_any(reg))
2974 __mark_reg_unknown(reg);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002975 }
2976}
2977
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002978static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
2979{
2980 struct bpf_verifier_state *vstate = env->cur_state;
2981 int i;
2982
2983 for (i = 0; i <= vstate->curframe; i++)
2984 __clear_all_pkt_pointers(env, vstate->frame[i]);
2985}
2986
Joe Stringerfd978bf72018-10-02 13:35:35 -07002987static void release_reg_references(struct bpf_verifier_env *env,
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002988 struct bpf_func_state *state,
2989 int ref_obj_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07002990{
2991 struct bpf_reg_state *regs = state->regs, *reg;
2992 int i;
2993
2994 for (i = 0; i < MAX_BPF_REG; i++)
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002995 if (regs[i].ref_obj_id == ref_obj_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07002996 mark_reg_unknown(env, regs, i);
2997
2998 bpf_for_each_spilled_reg(i, state, reg) {
2999 if (!reg)
3000 continue;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003001 if (reg->ref_obj_id == ref_obj_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07003002 __mark_reg_unknown(reg);
3003 }
3004}
3005
3006/* The pointer with the specified id has released its reference to kernel
3007 * resources. Identify all copies of the same pointer and clear the reference.
3008 */
3009static int release_reference(struct bpf_verifier_env *env,
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003010 int ref_obj_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07003011{
3012 struct bpf_verifier_state *vstate = env->cur_state;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003013 int err;
Joe Stringerfd978bf72018-10-02 13:35:35 -07003014 int i;
3015
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003016 err = release_reference_state(cur_func(env), ref_obj_id);
3017 if (err)
3018 return err;
Joe Stringerfd978bf72018-10-02 13:35:35 -07003019
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003020 for (i = 0; i <= vstate->curframe; i++)
3021 release_reg_references(env, vstate->frame[i], ref_obj_id);
3022
3023 return 0;
Joe Stringerfd978bf72018-10-02 13:35:35 -07003024}
3025
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003026static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
3027 int *insn_idx)
3028{
3029 struct bpf_verifier_state *state = env->cur_state;
3030 struct bpf_func_state *caller, *callee;
Joe Stringerfd978bf72018-10-02 13:35:35 -07003031 int i, err, subprog, target_insn;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003032
Alexei Starovoitovaada9ce2017-12-25 13:15:42 -08003033 if (state->curframe + 1 >= MAX_CALL_FRAMES) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003034 verbose(env, "the call stack of %d frames is too deep\n",
Alexei Starovoitovaada9ce2017-12-25 13:15:42 -08003035 state->curframe + 2);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003036 return -E2BIG;
3037 }
3038
3039 target_insn = *insn_idx + insn->imm;
3040 subprog = find_subprog(env, target_insn + 1);
3041 if (subprog < 0) {
3042 verbose(env, "verifier bug. No program starts at insn %d\n",
3043 target_insn + 1);
3044 return -EFAULT;
3045 }
3046
3047 caller = state->frame[state->curframe];
3048 if (state->frame[state->curframe + 1]) {
3049 verbose(env, "verifier bug. Frame %d already allocated\n",
3050 state->curframe + 1);
3051 return -EFAULT;
3052 }
3053
3054 callee = kzalloc(sizeof(*callee), GFP_KERNEL);
3055 if (!callee)
3056 return -ENOMEM;
3057 state->frame[state->curframe + 1] = callee;
3058
3059 /* callee cannot access r0, r6 - r9 for reading and has to write
3060 * into its own stack before reading from it.
3061 * callee can read/write into caller's stack
3062 */
3063 init_func_state(env, callee,
3064 /* remember the callsite, it will be used by bpf_exit */
3065 *insn_idx /* callsite */,
3066 state->curframe + 1 /* frameno within this callchain */,
Jiong Wangf910cef2018-05-02 16:17:17 -04003067 subprog /* subprog number within this prog */);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003068
Joe Stringerfd978bf72018-10-02 13:35:35 -07003069 /* Transfer references to the callee */
3070 err = transfer_reference_state(callee, caller);
3071 if (err)
3072 return err;
3073
Edward Cree679c7822018-08-22 20:02:19 +01003074 /* copy r1 - r5 args that callee can access. The copy includes parent
3075 * pointers, which connects us up to the liveness chain
3076 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003077 for (i = BPF_REG_1; i <= BPF_REG_5; i++)
3078 callee->regs[i] = caller->regs[i];
3079
Edward Cree679c7822018-08-22 20:02:19 +01003080 /* after the call registers r0 - r5 were scratched */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003081 for (i = 0; i < CALLER_SAVED_REGS; i++) {
3082 mark_reg_not_init(env, caller->regs, caller_saved[i]);
3083 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
3084 }
3085
3086 /* only increment it after check_reg_arg() finished */
3087 state->curframe++;
3088
3089 /* and go analyze first insn of the callee */
3090 *insn_idx = target_insn;
3091
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07003092 if (env->log.level & BPF_LOG_LEVEL) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003093 verbose(env, "caller:\n");
3094 print_verifier_state(env, caller);
3095 verbose(env, "callee:\n");
3096 print_verifier_state(env, callee);
3097 }
3098 return 0;
3099}
3100
3101static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
3102{
3103 struct bpf_verifier_state *state = env->cur_state;
3104 struct bpf_func_state *caller, *callee;
3105 struct bpf_reg_state *r0;
Joe Stringerfd978bf72018-10-02 13:35:35 -07003106 int err;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003107
3108 callee = state->frame[state->curframe];
3109 r0 = &callee->regs[BPF_REG_0];
3110 if (r0->type == PTR_TO_STACK) {
3111 /* technically it's ok to return caller's stack pointer
3112 * (or caller's caller's pointer) back to the caller,
3113 * since these pointers are valid. Only current stack
3114 * pointer will be invalid as soon as function exits,
3115 * but let's be conservative
3116 */
3117 verbose(env, "cannot return stack pointer to the caller\n");
3118 return -EINVAL;
3119 }
3120
3121 state->curframe--;
3122 caller = state->frame[state->curframe];
3123 /* return to the caller whatever r0 had in the callee */
3124 caller->regs[BPF_REG_0] = *r0;
3125
Joe Stringerfd978bf72018-10-02 13:35:35 -07003126 /* Transfer references to the caller */
3127 err = transfer_reference_state(caller, callee);
3128 if (err)
3129 return err;
3130
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003131 *insn_idx = callee->callsite + 1;
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07003132 if (env->log.level & BPF_LOG_LEVEL) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003133 verbose(env, "returning from callee:\n");
3134 print_verifier_state(env, callee);
3135 verbose(env, "to caller at %d:\n", *insn_idx);
3136 print_verifier_state(env, caller);
3137 }
3138 /* clear everything in the callee */
3139 free_func_state(callee);
3140 state->frame[state->curframe + 1] = NULL;
3141 return 0;
3142}
3143
Yonghong Song849fa502018-04-28 22:28:09 -07003144static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type,
3145 int func_id,
3146 struct bpf_call_arg_meta *meta)
3147{
3148 struct bpf_reg_state *ret_reg = &regs[BPF_REG_0];
3149
3150 if (ret_type != RET_INTEGER ||
3151 (func_id != BPF_FUNC_get_stack &&
3152 func_id != BPF_FUNC_probe_read_str))
3153 return;
3154
3155 ret_reg->smax_value = meta->msize_smax_value;
3156 ret_reg->umax_value = meta->msize_umax_value;
3157 __reg_deduce_bounds(ret_reg);
3158 __reg_bound_offset(ret_reg);
3159}
3160
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003161static int
3162record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,
3163 int func_id, int insn_idx)
3164{
3165 struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx];
Daniel Borkmann591fe982019-04-09 23:20:05 +02003166 struct bpf_map *map = meta->map_ptr;
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003167
3168 if (func_id != BPF_FUNC_tail_call &&
Daniel Borkmann09772d92018-06-02 23:06:35 +02003169 func_id != BPF_FUNC_map_lookup_elem &&
3170 func_id != BPF_FUNC_map_update_elem &&
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02003171 func_id != BPF_FUNC_map_delete_elem &&
3172 func_id != BPF_FUNC_map_push_elem &&
3173 func_id != BPF_FUNC_map_pop_elem &&
3174 func_id != BPF_FUNC_map_peek_elem)
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003175 return 0;
Daniel Borkmann09772d92018-06-02 23:06:35 +02003176
Daniel Borkmann591fe982019-04-09 23:20:05 +02003177 if (map == NULL) {
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003178 verbose(env, "kernel subsystem misconfigured verifier\n");
3179 return -EINVAL;
3180 }
3181
Daniel Borkmann591fe982019-04-09 23:20:05 +02003182 /* In case of read-only, some additional restrictions
3183 * need to be applied in order to prevent altering the
3184 * state of the map from program side.
3185 */
3186 if ((map->map_flags & BPF_F_RDONLY_PROG) &&
3187 (func_id == BPF_FUNC_map_delete_elem ||
3188 func_id == BPF_FUNC_map_update_elem ||
3189 func_id == BPF_FUNC_map_push_elem ||
3190 func_id == BPF_FUNC_map_pop_elem)) {
3191 verbose(env, "write into map forbidden\n");
3192 return -EACCES;
3193 }
3194
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003195 if (!BPF_MAP_PTR(aux->map_state))
3196 bpf_map_ptr_store(aux, meta->map_ptr,
3197 meta->map_ptr->unpriv_array);
3198 else if (BPF_MAP_PTR(aux->map_state) != meta->map_ptr)
3199 bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON,
3200 meta->map_ptr->unpriv_array);
3201 return 0;
3202}
3203
Joe Stringerfd978bf72018-10-02 13:35:35 -07003204static int check_reference_leak(struct bpf_verifier_env *env)
3205{
3206 struct bpf_func_state *state = cur_func(env);
3207 int i;
3208
3209 for (i = 0; i < state->acquired_refs; i++) {
3210 verbose(env, "Unreleased reference id=%d alloc_insn=%d\n",
3211 state->refs[i].id, state->refs[i].insn_idx);
3212 }
3213 return state->acquired_refs ? -EINVAL : 0;
3214}
3215
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003216static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003217{
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003218 const struct bpf_func_proto *fn = NULL;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003219 struct bpf_reg_state *regs;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003220 struct bpf_call_arg_meta meta;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003221 bool changes_data;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003222 int i, err;
3223
3224 /* find function prototype */
3225 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003226 verbose(env, "invalid func %s#%d\n", func_id_name(func_id),
3227 func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003228 return -EINVAL;
3229 }
3230
Jakub Kicinski00176a32017-10-16 16:40:54 -07003231 if (env->ops->get_func_proto)
Andrey Ignatov5e43f892018-03-30 15:08:00 -07003232 fn = env->ops->get_func_proto(func_id, env->prog);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003233 if (!fn) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003234 verbose(env, "unknown func %s#%d\n", func_id_name(func_id),
3235 func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003236 return -EINVAL;
3237 }
3238
3239 /* eBPF programs must be GPL compatible to use GPL-ed functions */
Daniel Borkmann24701ec2015-03-01 12:31:47 +01003240 if (!env->prog->gpl_compatible && fn->gpl_only) {
Daniel Borkmann3fe28672018-06-02 23:06:33 +02003241 verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003242 return -EINVAL;
3243 }
3244
Daniel Borkmann04514d12017-12-14 21:07:25 +01003245 /* With LD_ABS/IND some JITs save/restore skb from r1. */
Martin KaFai Lau17bedab2016-12-07 15:53:11 -08003246 changes_data = bpf_helper_changes_pkt_data(fn->func);
Daniel Borkmann04514d12017-12-14 21:07:25 +01003247 if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) {
3248 verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n",
3249 func_id_name(func_id), func_id);
3250 return -EINVAL;
3251 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003252
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003253 memset(&meta, 0, sizeof(meta));
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003254 meta.pkt_access = fn->pkt_access;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003255
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003256 err = check_func_proto(fn, func_id);
Daniel Borkmann435faee12016-04-13 00:10:51 +02003257 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003258 verbose(env, "kernel subsystem misconfigured func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02003259 func_id_name(func_id), func_id);
Daniel Borkmann435faee12016-04-13 00:10:51 +02003260 return err;
3261 }
3262
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08003263 meta.func_id = func_id;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003264 /* check args */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003265 err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003266 if (err)
3267 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003268 err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003269 if (err)
3270 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003271 err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003272 if (err)
3273 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003274 err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003275 if (err)
3276 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003277 err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003278 if (err)
3279 return err;
3280
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003281 err = record_func_map(env, &meta, func_id, insn_idx);
3282 if (err)
3283 return err;
3284
Daniel Borkmann435faee12016-04-13 00:10:51 +02003285 /* Mark slots with STACK_MISC in case of raw mode, stack offset
3286 * is inferred from register state.
3287 */
3288 for (i = 0; i < meta.access_size; i++) {
Daniel Borkmannca369602018-02-23 22:29:05 +01003289 err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B,
3290 BPF_WRITE, -1, false);
Daniel Borkmann435faee12016-04-13 00:10:51 +02003291 if (err)
3292 return err;
3293 }
3294
Joe Stringerfd978bf72018-10-02 13:35:35 -07003295 if (func_id == BPF_FUNC_tail_call) {
3296 err = check_reference_leak(env);
3297 if (err) {
3298 verbose(env, "tail_call would lead to reference leak\n");
3299 return err;
3300 }
3301 } else if (is_release_function(func_id)) {
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003302 err = release_reference(env, meta.ref_obj_id);
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08003303 if (err) {
3304 verbose(env, "func %s#%d reference has not been acquired before\n",
3305 func_id_name(func_id), func_id);
Joe Stringerfd978bf72018-10-02 13:35:35 -07003306 return err;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08003307 }
Joe Stringerfd978bf72018-10-02 13:35:35 -07003308 }
3309
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003310 regs = cur_regs(env);
Roman Gushchincd339432018-08-02 14:27:24 -07003311
3312 /* check that flags argument in get_local_storage(map, flags) is 0,
3313 * this is required because get_local_storage() can't return an error.
3314 */
3315 if (func_id == BPF_FUNC_get_local_storage &&
3316 !register_is_null(&regs[BPF_REG_2])) {
3317 verbose(env, "get_local_storage() doesn't support non-zero flags\n");
3318 return -EINVAL;
3319 }
3320
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003321 /* reset caller saved regs */
Edward Creedc503a82017-08-15 20:34:35 +01003322 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003323 mark_reg_not_init(env, regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01003324 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
3325 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003326
Edward Creedc503a82017-08-15 20:34:35 +01003327 /* update return register (already marked as written above) */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003328 if (fn->ret_type == RET_INTEGER) {
Edward Creef1174f72017-08-07 15:26:19 +01003329 /* sets type to SCALAR_VALUE */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003330 mark_reg_unknown(env, regs, BPF_REG_0);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003331 } else if (fn->ret_type == RET_VOID) {
3332 regs[BPF_REG_0].type = NOT_INIT;
Roman Gushchin3e6a4b32018-08-02 14:27:22 -07003333 } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL ||
3334 fn->ret_type == RET_PTR_TO_MAP_VALUE) {
Edward Creef1174f72017-08-07 15:26:19 +01003335 /* There is no offset yet applied, variable or fixed */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003336 mark_reg_known_zero(env, regs, BPF_REG_0);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003337 /* remember map_ptr, so that check_map_access()
3338 * can check 'value_size' boundary of memory access
3339 * to map element returned from bpf_map_lookup_elem()
3340 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003341 if (meta.map_ptr == NULL) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003342 verbose(env,
3343 "kernel subsystem misconfigured verifier\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003344 return -EINVAL;
3345 }
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003346 regs[BPF_REG_0].map_ptr = meta.map_ptr;
Daniel Borkmann4d31f302018-11-01 00:05:53 +01003347 if (fn->ret_type == RET_PTR_TO_MAP_VALUE) {
3348 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE;
Alexei Starovoitove16d2f12019-01-31 15:40:05 -08003349 if (map_value_has_spin_lock(meta.map_ptr))
3350 regs[BPF_REG_0].id = ++env->id_gen;
Daniel Borkmann4d31f302018-11-01 00:05:53 +01003351 } else {
3352 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
3353 regs[BPF_REG_0].id = ++env->id_gen;
3354 }
Joe Stringerc64b7982018-10-02 13:35:33 -07003355 } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) {
3356 mark_reg_known_zero(env, regs, BPF_REG_0);
3357 regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL;
Lorenz Bauer0f3adc22019-03-22 09:53:59 +08003358 regs[BPF_REG_0].id = ++env->id_gen;
Lorenz Bauer85a51f82019-03-22 09:54:00 +08003359 } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) {
3360 mark_reg_known_zero(env, regs, BPF_REG_0);
3361 regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL;
3362 regs[BPF_REG_0].id = ++env->id_gen;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08003363 } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) {
3364 mark_reg_known_zero(env, regs, BPF_REG_0);
3365 regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL;
3366 regs[BPF_REG_0].id = ++env->id_gen;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003367 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003368 verbose(env, "unknown return type %d of func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02003369 fn->ret_type, func_id_name(func_id), func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003370 return -EINVAL;
3371 }
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07003372
Lorenz Bauer0f3adc22019-03-22 09:53:59 +08003373 if (is_ptr_cast_function(func_id)) {
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003374 /* For release_reference() */
3375 regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id;
Lorenz Bauer0f3adc22019-03-22 09:53:59 +08003376 } else if (is_acquire_function(func_id)) {
3377 int id = acquire_reference_state(env, insn_idx);
3378
3379 if (id < 0)
3380 return id;
3381 /* For mark_ptr_or_null_reg() */
3382 regs[BPF_REG_0].id = id;
3383 /* For release_reference() */
3384 regs[BPF_REG_0].ref_obj_id = id;
3385 }
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003386
Yonghong Song849fa502018-04-28 22:28:09 -07003387 do_refine_retval_range(regs, fn->ret_type, func_id, &meta);
3388
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003389 err = check_map_func_compatibility(env, meta.map_ptr, func_id);
Kaixu Xia35578d72015-08-06 07:02:35 +00003390 if (err)
3391 return err;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07003392
Yonghong Songc195651e2018-04-28 22:28:08 -07003393 if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) {
3394 const char *err_str;
3395
3396#ifdef CONFIG_PERF_EVENTS
3397 err = get_callchain_buffers(sysctl_perf_event_max_stack);
3398 err_str = "cannot get callchain buffer for func %s#%d\n";
3399#else
3400 err = -ENOTSUPP;
3401 err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n";
3402#endif
3403 if (err) {
3404 verbose(env, err_str, func_id_name(func_id), func_id);
3405 return err;
3406 }
3407
3408 env->prog->has_callchain_buf = true;
3409 }
3410
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003411 if (changes_data)
3412 clear_all_pkt_pointers(env);
3413 return 0;
3414}
3415
Edward Creeb03c9f92017-08-07 15:26:36 +01003416static bool signed_add_overflows(s64 a, s64 b)
3417{
3418 /* Do the add in u64, where overflow is well-defined */
3419 s64 res = (s64)((u64)a + (u64)b);
3420
3421 if (b < 0)
3422 return res > a;
3423 return res < a;
3424}
3425
3426static bool signed_sub_overflows(s64 a, s64 b)
3427{
3428 /* Do the sub in u64, where overflow is well-defined */
3429 s64 res = (s64)((u64)a - (u64)b);
3430
3431 if (b < 0)
3432 return res < a;
3433 return res > a;
David S. Millerd1174412017-05-10 11:22:52 -07003434}
3435
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08003436static bool check_reg_sane_offset(struct bpf_verifier_env *env,
3437 const struct bpf_reg_state *reg,
3438 enum bpf_reg_type type)
3439{
3440 bool known = tnum_is_const(reg->var_off);
3441 s64 val = reg->var_off.value;
3442 s64 smin = reg->smin_value;
3443
3444 if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) {
3445 verbose(env, "math between %s pointer and %lld is not allowed\n",
3446 reg_type_str[type], val);
3447 return false;
3448 }
3449
3450 if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) {
3451 verbose(env, "%s pointer offset %d is not allowed\n",
3452 reg_type_str[type], reg->off);
3453 return false;
3454 }
3455
3456 if (smin == S64_MIN) {
3457 verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n",
3458 reg_type_str[type]);
3459 return false;
3460 }
3461
3462 if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) {
3463 verbose(env, "value %lld makes %s pointer be out of bounds\n",
3464 smin, reg_type_str[type]);
3465 return false;
3466 }
3467
3468 return true;
3469}
3470
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003471static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env)
3472{
3473 return &env->insn_aux_data[env->insn_idx];
3474}
3475
3476static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
3477 u32 *ptr_limit, u8 opcode, bool off_is_neg)
3478{
3479 bool mask_to_left = (opcode == BPF_ADD && off_is_neg) ||
3480 (opcode == BPF_SUB && !off_is_neg);
3481 u32 off;
3482
3483 switch (ptr_reg->type) {
3484 case PTR_TO_STACK:
Andrey Ignatov088ec262019-04-03 23:22:39 -07003485 /* Indirect variable offset stack access is prohibited in
3486 * unprivileged mode so it's not handled here.
3487 */
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003488 off = ptr_reg->off + ptr_reg->var_off.value;
3489 if (mask_to_left)
3490 *ptr_limit = MAX_BPF_STACK + off;
3491 else
3492 *ptr_limit = -off;
3493 return 0;
3494 case PTR_TO_MAP_VALUE:
3495 if (mask_to_left) {
3496 *ptr_limit = ptr_reg->umax_value + ptr_reg->off;
3497 } else {
3498 off = ptr_reg->smin_value + ptr_reg->off;
3499 *ptr_limit = ptr_reg->map_ptr->value_size - off;
3500 }
3501 return 0;
3502 default:
3503 return -EINVAL;
3504 }
3505}
3506
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003507static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env,
3508 const struct bpf_insn *insn)
3509{
3510 return env->allow_ptr_leaks || BPF_SRC(insn->code) == BPF_K;
3511}
3512
3513static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux,
3514 u32 alu_state, u32 alu_limit)
3515{
3516 /* If we arrived here from different branches with different
3517 * state or limits to sanitize, then this won't work.
3518 */
3519 if (aux->alu_state &&
3520 (aux->alu_state != alu_state ||
3521 aux->alu_limit != alu_limit))
3522 return -EACCES;
3523
3524 /* Corresponding fixup done in fixup_bpf_calls(). */
3525 aux->alu_state = alu_state;
3526 aux->alu_limit = alu_limit;
3527 return 0;
3528}
3529
3530static int sanitize_val_alu(struct bpf_verifier_env *env,
3531 struct bpf_insn *insn)
3532{
3533 struct bpf_insn_aux_data *aux = cur_aux(env);
3534
3535 if (can_skip_alu_sanitation(env, insn))
3536 return 0;
3537
3538 return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0);
3539}
3540
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003541static int sanitize_ptr_alu(struct bpf_verifier_env *env,
3542 struct bpf_insn *insn,
3543 const struct bpf_reg_state *ptr_reg,
3544 struct bpf_reg_state *dst_reg,
3545 bool off_is_neg)
3546{
3547 struct bpf_verifier_state *vstate = env->cur_state;
3548 struct bpf_insn_aux_data *aux = cur_aux(env);
3549 bool ptr_is_dst_reg = ptr_reg == dst_reg;
3550 u8 opcode = BPF_OP(insn->code);
3551 u32 alu_state, alu_limit;
3552 struct bpf_reg_state tmp;
3553 bool ret;
3554
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003555 if (can_skip_alu_sanitation(env, insn))
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003556 return 0;
3557
3558 /* We already marked aux for masking from non-speculative
3559 * paths, thus we got here in the first place. We only care
3560 * to explore bad access from here.
3561 */
3562 if (vstate->speculative)
3563 goto do_sim;
3564
3565 alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0;
3566 alu_state |= ptr_is_dst_reg ?
3567 BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST;
3568
3569 if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg))
3570 return 0;
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003571 if (update_alu_sanitation_state(aux, alu_state, alu_limit))
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003572 return -EACCES;
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003573do_sim:
3574 /* Simulate and find potential out-of-bounds access under
3575 * speculative execution from truncation as a result of
3576 * masking when off was not within expected range. If off
3577 * sits in dst, then we temporarily need to move ptr there
3578 * to simulate dst (== 0) +/-= ptr. Needed, for example,
3579 * for cases where we use K-based arithmetic in one direction
3580 * and truncated reg-based in the other in order to explore
3581 * bad access.
3582 */
3583 if (!ptr_is_dst_reg) {
3584 tmp = *dst_reg;
3585 *dst_reg = *ptr_reg;
3586 }
3587 ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true);
Xu Yu08032782019-03-21 18:00:35 +08003588 if (!ptr_is_dst_reg && ret)
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003589 *dst_reg = tmp;
3590 return !ret ? -EFAULT : 0;
3591}
3592
Edward Creef1174f72017-08-07 15:26:19 +01003593/* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off.
Edward Creef1174f72017-08-07 15:26:19 +01003594 * Caller should also handle BPF_MOV case separately.
3595 * If we return -EACCES, caller may want to try again treating pointer as a
3596 * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks.
3597 */
3598static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
3599 struct bpf_insn *insn,
3600 const struct bpf_reg_state *ptr_reg,
3601 const struct bpf_reg_state *off_reg)
Josef Bacik48461132016-09-28 10:54:32 -04003602{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003603 struct bpf_verifier_state *vstate = env->cur_state;
3604 struct bpf_func_state *state = vstate->frame[vstate->curframe];
3605 struct bpf_reg_state *regs = state->regs, *dst_reg;
Edward Creef1174f72017-08-07 15:26:19 +01003606 bool known = tnum_is_const(off_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01003607 s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value,
3608 smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
3609 u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value,
3610 umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value;
Daniel Borkmann9d7ecee2019-01-03 00:58:32 +01003611 u32 dst = insn->dst_reg, src = insn->src_reg;
Josef Bacik48461132016-09-28 10:54:32 -04003612 u8 opcode = BPF_OP(insn->code);
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003613 int ret;
Josef Bacik48461132016-09-28 10:54:32 -04003614
Edward Creef1174f72017-08-07 15:26:19 +01003615 dst_reg = &regs[dst];
Josef Bacik48461132016-09-28 10:54:32 -04003616
Daniel Borkmann6f161012018-01-18 01:15:21 +01003617 if ((known && (smin_val != smax_val || umin_val != umax_val)) ||
3618 smin_val > smax_val || umin_val > umax_val) {
3619 /* Taint dst register if offset had invalid bounds derived from
3620 * e.g. dead branches.
3621 */
3622 __mark_reg_unknown(dst_reg);
3623 return 0;
Josef Bacik48461132016-09-28 10:54:32 -04003624 }
3625
Edward Creef1174f72017-08-07 15:26:19 +01003626 if (BPF_CLASS(insn->code) != BPF_ALU64) {
3627 /* 32-bit ALU ops on pointers produce (meaningless) scalars */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003628 verbose(env,
3629 "R%d 32-bit pointer arithmetic prohibited\n",
3630 dst);
Edward Creef1174f72017-08-07 15:26:19 +01003631 return -EACCES;
3632 }
David S. Millerd1174412017-05-10 11:22:52 -07003633
Joe Stringeraad2eea2018-10-02 13:35:30 -07003634 switch (ptr_reg->type) {
3635 case PTR_TO_MAP_VALUE_OR_NULL:
3636 verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n",
3637 dst, reg_type_str[ptr_reg->type]);
Edward Creef1174f72017-08-07 15:26:19 +01003638 return -EACCES;
Joe Stringeraad2eea2018-10-02 13:35:30 -07003639 case CONST_PTR_TO_MAP:
3640 case PTR_TO_PACKET_END:
Joe Stringerc64b7982018-10-02 13:35:33 -07003641 case PTR_TO_SOCKET:
3642 case PTR_TO_SOCKET_OR_NULL:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08003643 case PTR_TO_SOCK_COMMON:
3644 case PTR_TO_SOCK_COMMON_OR_NULL:
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08003645 case PTR_TO_TCP_SOCK:
3646 case PTR_TO_TCP_SOCK_OR_NULL:
Joe Stringeraad2eea2018-10-02 13:35:30 -07003647 verbose(env, "R%d pointer arithmetic on %s prohibited\n",
3648 dst, reg_type_str[ptr_reg->type]);
Edward Creef1174f72017-08-07 15:26:19 +01003649 return -EACCES;
Daniel Borkmann9d7ecee2019-01-03 00:58:32 +01003650 case PTR_TO_MAP_VALUE:
3651 if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) {
3652 verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n",
3653 off_reg == dst_reg ? dst : src);
3654 return -EACCES;
3655 }
3656 /* fall-through */
Joe Stringeraad2eea2018-10-02 13:35:30 -07003657 default:
3658 break;
Edward Creef1174f72017-08-07 15:26:19 +01003659 }
3660
3661 /* In case of 'scalar += pointer', dst_reg inherits pointer type and id.
3662 * The id may be overwritten later if we create a new variable offset.
Josef Bacik48461132016-09-28 10:54:32 -04003663 */
Edward Creef1174f72017-08-07 15:26:19 +01003664 dst_reg->type = ptr_reg->type;
3665 dst_reg->id = ptr_reg->id;
Josef Bacikf23cc642016-11-14 15:45:36 -05003666
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08003667 if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) ||
3668 !check_reg_sane_offset(env, ptr_reg, ptr_reg->type))
3669 return -EINVAL;
3670
Josef Bacik48461132016-09-28 10:54:32 -04003671 switch (opcode) {
3672 case BPF_ADD:
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003673 ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0);
3674 if (ret < 0) {
3675 verbose(env, "R%d tried to add from different maps or paths\n", dst);
3676 return ret;
3677 }
Edward Creef1174f72017-08-07 15:26:19 +01003678 /* We can take a fixed offset as long as it doesn't overflow
3679 * the s32 'off' field
3680 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003681 if (known && (ptr_reg->off + smin_val ==
3682 (s64)(s32)(ptr_reg->off + smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01003683 /* pointer += K. Accumulate it into fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01003684 dst_reg->smin_value = smin_ptr;
3685 dst_reg->smax_value = smax_ptr;
3686 dst_reg->umin_value = umin_ptr;
3687 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01003688 dst_reg->var_off = ptr_reg->var_off;
Edward Creeb03c9f92017-08-07 15:26:36 +01003689 dst_reg->off = ptr_reg->off + smin_val;
Daniel Borkmann09625902018-11-01 00:05:52 +01003690 dst_reg->raw = ptr_reg->raw;
Edward Creef1174f72017-08-07 15:26:19 +01003691 break;
3692 }
Edward Creef1174f72017-08-07 15:26:19 +01003693 /* A new variable offset is created. Note that off_reg->off
3694 * == 0, since it's a scalar.
3695 * dst_reg gets the pointer type and since some positive
3696 * integer value was added to the pointer, give it a new 'id'
3697 * if it's a PTR_TO_PACKET.
3698 * this creates a new 'base' pointer, off_reg (variable) gets
3699 * added into the variable offset, and we copy the fixed offset
3700 * from ptr_reg.
3701 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003702 if (signed_add_overflows(smin_ptr, smin_val) ||
3703 signed_add_overflows(smax_ptr, smax_val)) {
3704 dst_reg->smin_value = S64_MIN;
3705 dst_reg->smax_value = S64_MAX;
3706 } else {
3707 dst_reg->smin_value = smin_ptr + smin_val;
3708 dst_reg->smax_value = smax_ptr + smax_val;
3709 }
3710 if (umin_ptr + umin_val < umin_ptr ||
3711 umax_ptr + umax_val < umax_ptr) {
3712 dst_reg->umin_value = 0;
3713 dst_reg->umax_value = U64_MAX;
3714 } else {
3715 dst_reg->umin_value = umin_ptr + umin_val;
3716 dst_reg->umax_value = umax_ptr + umax_val;
3717 }
Edward Creef1174f72017-08-07 15:26:19 +01003718 dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off);
3719 dst_reg->off = ptr_reg->off;
Daniel Borkmann09625902018-11-01 00:05:52 +01003720 dst_reg->raw = ptr_reg->raw;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003721 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01003722 dst_reg->id = ++env->id_gen;
3723 /* something was added to pkt_ptr, set range to zero */
Daniel Borkmann09625902018-11-01 00:05:52 +01003724 dst_reg->raw = 0;
Edward Creef1174f72017-08-07 15:26:19 +01003725 }
Josef Bacik48461132016-09-28 10:54:32 -04003726 break;
3727 case BPF_SUB:
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003728 ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0);
3729 if (ret < 0) {
3730 verbose(env, "R%d tried to sub from different maps or paths\n", dst);
3731 return ret;
3732 }
Edward Creef1174f72017-08-07 15:26:19 +01003733 if (dst_reg == off_reg) {
3734 /* scalar -= pointer. Creates an unknown scalar */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003735 verbose(env, "R%d tried to subtract pointer from scalar\n",
3736 dst);
Edward Creef1174f72017-08-07 15:26:19 +01003737 return -EACCES;
3738 }
3739 /* We don't allow subtraction from FP, because (according to
3740 * test_verifier.c test "invalid fp arithmetic", JITs might not
3741 * be able to deal with it.
Edward Cree93057062017-07-21 14:37:34 +01003742 */
Edward Creef1174f72017-08-07 15:26:19 +01003743 if (ptr_reg->type == PTR_TO_STACK) {
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003744 verbose(env, "R%d subtraction from stack pointer prohibited\n",
3745 dst);
Edward Creef1174f72017-08-07 15:26:19 +01003746 return -EACCES;
3747 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003748 if (known && (ptr_reg->off - smin_val ==
3749 (s64)(s32)(ptr_reg->off - smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01003750 /* pointer -= K. Subtract it from fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01003751 dst_reg->smin_value = smin_ptr;
3752 dst_reg->smax_value = smax_ptr;
3753 dst_reg->umin_value = umin_ptr;
3754 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01003755 dst_reg->var_off = ptr_reg->var_off;
3756 dst_reg->id = ptr_reg->id;
Edward Creeb03c9f92017-08-07 15:26:36 +01003757 dst_reg->off = ptr_reg->off - smin_val;
Daniel Borkmann09625902018-11-01 00:05:52 +01003758 dst_reg->raw = ptr_reg->raw;
Edward Creef1174f72017-08-07 15:26:19 +01003759 break;
3760 }
Edward Creef1174f72017-08-07 15:26:19 +01003761 /* A new variable offset is created. If the subtrahend is known
3762 * nonnegative, then any reg->range we had before is still good.
3763 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003764 if (signed_sub_overflows(smin_ptr, smax_val) ||
3765 signed_sub_overflows(smax_ptr, smin_val)) {
3766 /* Overflow possible, we know nothing */
3767 dst_reg->smin_value = S64_MIN;
3768 dst_reg->smax_value = S64_MAX;
3769 } else {
3770 dst_reg->smin_value = smin_ptr - smax_val;
3771 dst_reg->smax_value = smax_ptr - smin_val;
3772 }
3773 if (umin_ptr < umax_val) {
3774 /* Overflow possible, we know nothing */
3775 dst_reg->umin_value = 0;
3776 dst_reg->umax_value = U64_MAX;
3777 } else {
3778 /* Cannot overflow (as long as bounds are consistent) */
3779 dst_reg->umin_value = umin_ptr - umax_val;
3780 dst_reg->umax_value = umax_ptr - umin_val;
3781 }
Edward Creef1174f72017-08-07 15:26:19 +01003782 dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off);
3783 dst_reg->off = ptr_reg->off;
Daniel Borkmann09625902018-11-01 00:05:52 +01003784 dst_reg->raw = ptr_reg->raw;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003785 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01003786 dst_reg->id = ++env->id_gen;
3787 /* something was added to pkt_ptr, set range to zero */
Edward Creeb03c9f92017-08-07 15:26:36 +01003788 if (smin_val < 0)
Daniel Borkmann09625902018-11-01 00:05:52 +01003789 dst_reg->raw = 0;
Edward Creef1174f72017-08-07 15:26:19 +01003790 }
3791 break;
3792 case BPF_AND:
3793 case BPF_OR:
3794 case BPF_XOR:
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003795 /* bitwise ops on pointers are troublesome, prohibit. */
3796 verbose(env, "R%d bitwise operator %s on pointer prohibited\n",
3797 dst, bpf_alu_string[opcode >> 4]);
Edward Creef1174f72017-08-07 15:26:19 +01003798 return -EACCES;
3799 default:
3800 /* other operators (e.g. MUL,LSH) produce non-pointer results */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003801 verbose(env, "R%d pointer arithmetic with %s operator prohibited\n",
3802 dst, bpf_alu_string[opcode >> 4]);
Edward Creef1174f72017-08-07 15:26:19 +01003803 return -EACCES;
3804 }
3805
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08003806 if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type))
3807 return -EINVAL;
3808
Edward Creeb03c9f92017-08-07 15:26:36 +01003809 __update_reg_bounds(dst_reg);
3810 __reg_deduce_bounds(dst_reg);
3811 __reg_bound_offset(dst_reg);
Daniel Borkmann0d6303d2019-01-03 00:58:30 +01003812
3813 /* For unprivileged we require that resulting offset must be in bounds
3814 * in order to be able to sanitize access later on.
3815 */
Daniel Borkmanne4298d22019-01-03 00:58:31 +01003816 if (!env->allow_ptr_leaks) {
3817 if (dst_reg->type == PTR_TO_MAP_VALUE &&
3818 check_map_access(env, dst, dst_reg->off, 1, false)) {
3819 verbose(env, "R%d pointer arithmetic of map value goes out of range, "
3820 "prohibited for !root\n", dst);
3821 return -EACCES;
3822 } else if (dst_reg->type == PTR_TO_STACK &&
3823 check_stack_access(env, dst_reg, dst_reg->off +
3824 dst_reg->var_off.value, 1)) {
3825 verbose(env, "R%d stack pointer arithmetic goes out of range, "
3826 "prohibited for !root\n", dst);
3827 return -EACCES;
3828 }
Daniel Borkmann0d6303d2019-01-03 00:58:30 +01003829 }
3830
Edward Creef1174f72017-08-07 15:26:19 +01003831 return 0;
3832}
3833
Jann Horn468f6ea2017-12-18 20:11:56 -08003834/* WARNING: This function does calculations on 64-bit values, but the actual
3835 * execution may occur on 32-bit values. Therefore, things like bitshifts
3836 * need extra checks in the 32-bit case.
3837 */
Edward Creef1174f72017-08-07 15:26:19 +01003838static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
3839 struct bpf_insn *insn,
3840 struct bpf_reg_state *dst_reg,
3841 struct bpf_reg_state src_reg)
3842{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003843 struct bpf_reg_state *regs = cur_regs(env);
Edward Creef1174f72017-08-07 15:26:19 +01003844 u8 opcode = BPF_OP(insn->code);
3845 bool src_known, dst_known;
Edward Creeb03c9f92017-08-07 15:26:36 +01003846 s64 smin_val, smax_val;
3847 u64 umin_val, umax_val;
Jann Horn468f6ea2017-12-18 20:11:56 -08003848 u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32;
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003849 u32 dst = insn->dst_reg;
3850 int ret;
Edward Creef1174f72017-08-07 15:26:19 +01003851
Jann Hornb7992072018-10-05 18:17:59 +02003852 if (insn_bitness == 32) {
3853 /* Relevant for 32-bit RSH: Information can propagate towards
3854 * LSB, so it isn't sufficient to only truncate the output to
3855 * 32 bits.
3856 */
3857 coerce_reg_to_size(dst_reg, 4);
3858 coerce_reg_to_size(&src_reg, 4);
3859 }
3860
Edward Creeb03c9f92017-08-07 15:26:36 +01003861 smin_val = src_reg.smin_value;
3862 smax_val = src_reg.smax_value;
3863 umin_val = src_reg.umin_value;
3864 umax_val = src_reg.umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01003865 src_known = tnum_is_const(src_reg.var_off);
3866 dst_known = tnum_is_const(dst_reg->var_off);
3867
Daniel Borkmann6f161012018-01-18 01:15:21 +01003868 if ((src_known && (smin_val != smax_val || umin_val != umax_val)) ||
3869 smin_val > smax_val || umin_val > umax_val) {
3870 /* Taint dst register if offset had invalid bounds derived from
3871 * e.g. dead branches.
3872 */
3873 __mark_reg_unknown(dst_reg);
3874 return 0;
3875 }
3876
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08003877 if (!src_known &&
3878 opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) {
3879 __mark_reg_unknown(dst_reg);
3880 return 0;
3881 }
3882
Edward Creef1174f72017-08-07 15:26:19 +01003883 switch (opcode) {
3884 case BPF_ADD:
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003885 ret = sanitize_val_alu(env, insn);
3886 if (ret < 0) {
3887 verbose(env, "R%d tried to add from different pointers or scalars\n", dst);
3888 return ret;
3889 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003890 if (signed_add_overflows(dst_reg->smin_value, smin_val) ||
3891 signed_add_overflows(dst_reg->smax_value, smax_val)) {
3892 dst_reg->smin_value = S64_MIN;
3893 dst_reg->smax_value = S64_MAX;
3894 } else {
3895 dst_reg->smin_value += smin_val;
3896 dst_reg->smax_value += smax_val;
3897 }
3898 if (dst_reg->umin_value + umin_val < umin_val ||
3899 dst_reg->umax_value + umax_val < umax_val) {
3900 dst_reg->umin_value = 0;
3901 dst_reg->umax_value = U64_MAX;
3902 } else {
3903 dst_reg->umin_value += umin_val;
3904 dst_reg->umax_value += umax_val;
3905 }
Edward Creef1174f72017-08-07 15:26:19 +01003906 dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off);
3907 break;
3908 case BPF_SUB:
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003909 ret = sanitize_val_alu(env, insn);
3910 if (ret < 0) {
3911 verbose(env, "R%d tried to sub from different pointers or scalars\n", dst);
3912 return ret;
3913 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003914 if (signed_sub_overflows(dst_reg->smin_value, smax_val) ||
3915 signed_sub_overflows(dst_reg->smax_value, smin_val)) {
3916 /* Overflow possible, we know nothing */
3917 dst_reg->smin_value = S64_MIN;
3918 dst_reg->smax_value = S64_MAX;
3919 } else {
3920 dst_reg->smin_value -= smax_val;
3921 dst_reg->smax_value -= smin_val;
3922 }
3923 if (dst_reg->umin_value < umax_val) {
3924 /* Overflow possible, we know nothing */
3925 dst_reg->umin_value = 0;
3926 dst_reg->umax_value = U64_MAX;
3927 } else {
3928 /* Cannot overflow (as long as bounds are consistent) */
3929 dst_reg->umin_value -= umax_val;
3930 dst_reg->umax_value -= umin_val;
3931 }
Edward Creef1174f72017-08-07 15:26:19 +01003932 dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off);
Josef Bacik48461132016-09-28 10:54:32 -04003933 break;
3934 case BPF_MUL:
Edward Creeb03c9f92017-08-07 15:26:36 +01003935 dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off);
3936 if (smin_val < 0 || dst_reg->smin_value < 0) {
Edward Creef1174f72017-08-07 15:26:19 +01003937 /* Ain't nobody got time to multiply that sign */
Edward Creeb03c9f92017-08-07 15:26:36 +01003938 __mark_reg_unbounded(dst_reg);
3939 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003940 break;
3941 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003942 /* Both values are positive, so we can work with unsigned and
3943 * copy the result to signed (unless it exceeds S64_MAX).
Edward Creef1174f72017-08-07 15:26:19 +01003944 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003945 if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) {
3946 /* Potential overflow, we know nothing */
3947 __mark_reg_unbounded(dst_reg);
3948 /* (except what we can learn from the var_off) */
3949 __update_reg_bounds(dst_reg);
3950 break;
3951 }
3952 dst_reg->umin_value *= umin_val;
3953 dst_reg->umax_value *= umax_val;
3954 if (dst_reg->umax_value > S64_MAX) {
3955 /* Overflow possible, we know nothing */
3956 dst_reg->smin_value = S64_MIN;
3957 dst_reg->smax_value = S64_MAX;
3958 } else {
3959 dst_reg->smin_value = dst_reg->umin_value;
3960 dst_reg->smax_value = dst_reg->umax_value;
3961 }
Josef Bacik48461132016-09-28 10:54:32 -04003962 break;
3963 case BPF_AND:
Edward Creef1174f72017-08-07 15:26:19 +01003964 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01003965 __mark_reg_known(dst_reg, dst_reg->var_off.value &
3966 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01003967 break;
3968 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003969 /* We get our minimum from the var_off, since that's inherently
3970 * bitwise. Our maximum is the minimum of the operands' maxima.
Josef Bacikf23cc642016-11-14 15:45:36 -05003971 */
Edward Creef1174f72017-08-07 15:26:19 +01003972 dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01003973 dst_reg->umin_value = dst_reg->var_off.value;
3974 dst_reg->umax_value = min(dst_reg->umax_value, umax_val);
3975 if (dst_reg->smin_value < 0 || smin_val < 0) {
3976 /* Lose signed bounds when ANDing negative numbers,
3977 * ain't nobody got time for that.
3978 */
3979 dst_reg->smin_value = S64_MIN;
3980 dst_reg->smax_value = S64_MAX;
3981 } else {
3982 /* ANDing two positives gives a positive, so safe to
3983 * cast result into s64.
3984 */
3985 dst_reg->smin_value = dst_reg->umin_value;
3986 dst_reg->smax_value = dst_reg->umax_value;
3987 }
3988 /* We may learn something more from the var_off */
3989 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003990 break;
3991 case BPF_OR:
3992 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01003993 __mark_reg_known(dst_reg, dst_reg->var_off.value |
3994 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01003995 break;
3996 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003997 /* We get our maximum from the var_off, and our minimum is the
3998 * maximum of the operands' minima
Edward Creef1174f72017-08-07 15:26:19 +01003999 */
4000 dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01004001 dst_reg->umin_value = max(dst_reg->umin_value, umin_val);
4002 dst_reg->umax_value = dst_reg->var_off.value |
4003 dst_reg->var_off.mask;
4004 if (dst_reg->smin_value < 0 || smin_val < 0) {
4005 /* Lose signed bounds when ORing negative numbers,
4006 * ain't nobody got time for that.
4007 */
4008 dst_reg->smin_value = S64_MIN;
4009 dst_reg->smax_value = S64_MAX;
Edward Creef1174f72017-08-07 15:26:19 +01004010 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01004011 /* ORing two positives gives a positive, so safe to
4012 * cast result into s64.
4013 */
4014 dst_reg->smin_value = dst_reg->umin_value;
4015 dst_reg->smax_value = dst_reg->umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01004016 }
Edward Creeb03c9f92017-08-07 15:26:36 +01004017 /* We may learn something more from the var_off */
4018 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004019 break;
4020 case BPF_LSH:
Jann Horn468f6ea2017-12-18 20:11:56 -08004021 if (umax_val >= insn_bitness) {
4022 /* Shifts greater than 31 or 63 are undefined.
4023 * This includes shifts by a negative number.
Edward Creeb03c9f92017-08-07 15:26:36 +01004024 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004025 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004026 break;
4027 }
Edward Creeb03c9f92017-08-07 15:26:36 +01004028 /* We lose all sign bit information (except what we can pick
4029 * up from var_off)
Josef Bacik48461132016-09-28 10:54:32 -04004030 */
Edward Creeb03c9f92017-08-07 15:26:36 +01004031 dst_reg->smin_value = S64_MIN;
4032 dst_reg->smax_value = S64_MAX;
4033 /* If we might shift our top bit out, then we know nothing */
4034 if (dst_reg->umax_value > 1ULL << (63 - umax_val)) {
4035 dst_reg->umin_value = 0;
4036 dst_reg->umax_value = U64_MAX;
David S. Millerd1174412017-05-10 11:22:52 -07004037 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01004038 dst_reg->umin_value <<= umin_val;
4039 dst_reg->umax_value <<= umax_val;
David S. Millerd1174412017-05-10 11:22:52 -07004040 }
Yonghong Songafbe1a52018-04-28 22:28:10 -07004041 dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val);
Edward Creeb03c9f92017-08-07 15:26:36 +01004042 /* We may learn something more from the var_off */
4043 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004044 break;
4045 case BPF_RSH:
Jann Horn468f6ea2017-12-18 20:11:56 -08004046 if (umax_val >= insn_bitness) {
4047 /* Shifts greater than 31 or 63 are undefined.
4048 * This includes shifts by a negative number.
Edward Creeb03c9f92017-08-07 15:26:36 +01004049 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004050 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004051 break;
4052 }
Edward Cree4374f252017-12-18 20:11:53 -08004053 /* BPF_RSH is an unsigned shift. If the value in dst_reg might
4054 * be negative, then either:
4055 * 1) src_reg might be zero, so the sign bit of the result is
4056 * unknown, so we lose our signed bounds
4057 * 2) it's known negative, thus the unsigned bounds capture the
4058 * signed bounds
4059 * 3) the signed bounds cross zero, so they tell us nothing
4060 * about the result
4061 * If the value in dst_reg is known nonnegative, then again the
4062 * unsigned bounts capture the signed bounds.
4063 * Thus, in all cases it suffices to blow away our signed bounds
4064 * and rely on inferring new ones from the unsigned bounds and
4065 * var_off of the result.
4066 */
4067 dst_reg->smin_value = S64_MIN;
4068 dst_reg->smax_value = S64_MAX;
Yonghong Songafbe1a52018-04-28 22:28:10 -07004069 dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val);
Edward Creeb03c9f92017-08-07 15:26:36 +01004070 dst_reg->umin_value >>= umax_val;
4071 dst_reg->umax_value >>= umin_val;
4072 /* We may learn something more from the var_off */
4073 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004074 break;
Yonghong Song9cbe1f5a2018-04-28 22:28:11 -07004075 case BPF_ARSH:
4076 if (umax_val >= insn_bitness) {
4077 /* Shifts greater than 31 or 63 are undefined.
4078 * This includes shifts by a negative number.
4079 */
4080 mark_reg_unknown(env, regs, insn->dst_reg);
4081 break;
4082 }
4083
4084 /* Upon reaching here, src_known is true and
4085 * umax_val is equal to umin_val.
4086 */
4087 dst_reg->smin_value >>= umin_val;
4088 dst_reg->smax_value >>= umin_val;
4089 dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val);
4090
4091 /* blow away the dst_reg umin_value/umax_value and rely on
4092 * dst_reg var_off to refine the result.
4093 */
4094 dst_reg->umin_value = 0;
4095 dst_reg->umax_value = U64_MAX;
4096 __update_reg_bounds(dst_reg);
4097 break;
Josef Bacik48461132016-09-28 10:54:32 -04004098 default:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004099 mark_reg_unknown(env, regs, insn->dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004100 break;
4101 }
4102
Jann Horn468f6ea2017-12-18 20:11:56 -08004103 if (BPF_CLASS(insn->code) != BPF_ALU64) {
4104 /* 32-bit ALU ops are (32,32)->32 */
4105 coerce_reg_to_size(dst_reg, 4);
Jann Horn468f6ea2017-12-18 20:11:56 -08004106 }
4107
Edward Creeb03c9f92017-08-07 15:26:36 +01004108 __reg_deduce_bounds(dst_reg);
4109 __reg_bound_offset(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004110 return 0;
4111}
4112
4113/* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max
4114 * and var_off.
4115 */
4116static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
4117 struct bpf_insn *insn)
4118{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004119 struct bpf_verifier_state *vstate = env->cur_state;
4120 struct bpf_func_state *state = vstate->frame[vstate->curframe];
4121 struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg;
Edward Creef1174f72017-08-07 15:26:19 +01004122 struct bpf_reg_state *ptr_reg = NULL, off_reg = {0};
4123 u8 opcode = BPF_OP(insn->code);
Edward Creef1174f72017-08-07 15:26:19 +01004124
4125 dst_reg = &regs[insn->dst_reg];
Edward Creef1174f72017-08-07 15:26:19 +01004126 src_reg = NULL;
4127 if (dst_reg->type != SCALAR_VALUE)
4128 ptr_reg = dst_reg;
4129 if (BPF_SRC(insn->code) == BPF_X) {
4130 src_reg = &regs[insn->src_reg];
Edward Creef1174f72017-08-07 15:26:19 +01004131 if (src_reg->type != SCALAR_VALUE) {
4132 if (dst_reg->type != SCALAR_VALUE) {
4133 /* Combining two pointers by any ALU op yields
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004134 * an arbitrary scalar. Disallow all math except
4135 * pointer subtraction
Edward Creef1174f72017-08-07 15:26:19 +01004136 */
Alexei Starovoitovdd066822018-09-12 14:06:10 -07004137 if (opcode == BPF_SUB && env->allow_ptr_leaks) {
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004138 mark_reg_unknown(env, regs, insn->dst_reg);
4139 return 0;
Edward Creef1174f72017-08-07 15:26:19 +01004140 }
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004141 verbose(env, "R%d pointer %s pointer prohibited\n",
4142 insn->dst_reg,
4143 bpf_alu_string[opcode >> 4]);
4144 return -EACCES;
Edward Creef1174f72017-08-07 15:26:19 +01004145 } else {
4146 /* scalar += pointer
4147 * This is legal, but we have to reverse our
4148 * src/dest handling in computing the range
4149 */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004150 return adjust_ptr_min_max_vals(env, insn,
4151 src_reg, dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004152 }
4153 } else if (ptr_reg) {
4154 /* pointer += scalar */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004155 return adjust_ptr_min_max_vals(env, insn,
4156 dst_reg, src_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004157 }
4158 } else {
4159 /* Pretend the src is a reg with a known value, since we only
4160 * need to be able to read from this state.
4161 */
4162 off_reg.type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01004163 __mark_reg_known(&off_reg, insn->imm);
Edward Creef1174f72017-08-07 15:26:19 +01004164 src_reg = &off_reg;
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004165 if (ptr_reg) /* pointer += K */
4166 return adjust_ptr_min_max_vals(env, insn,
4167 ptr_reg, src_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004168 }
4169
4170 /* Got here implies adding two SCALAR_VALUEs */
4171 if (WARN_ON_ONCE(ptr_reg)) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004172 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004173 verbose(env, "verifier internal error: unexpected ptr_reg\n");
Edward Creef1174f72017-08-07 15:26:19 +01004174 return -EINVAL;
4175 }
4176 if (WARN_ON(!src_reg)) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004177 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004178 verbose(env, "verifier internal error: no src_reg\n");
Edward Creef1174f72017-08-07 15:26:19 +01004179 return -EINVAL;
4180 }
4181 return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004182}
4183
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004184/* check validity of 32-bit and 64-bit arithmetic operations */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004185static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004186{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004187 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004188 u8 opcode = BPF_OP(insn->code);
4189 int err;
4190
4191 if (opcode == BPF_END || opcode == BPF_NEG) {
4192 if (opcode == BPF_NEG) {
4193 if (BPF_SRC(insn->code) != 0 ||
4194 insn->src_reg != BPF_REG_0 ||
4195 insn->off != 0 || insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004196 verbose(env, "BPF_NEG uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004197 return -EINVAL;
4198 }
4199 } else {
4200 if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
Edward Creee67b8a62017-09-15 14:37:38 +01004201 (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) ||
4202 BPF_CLASS(insn->code) == BPF_ALU64) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004203 verbose(env, "BPF_END uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004204 return -EINVAL;
4205 }
4206 }
4207
4208 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01004209 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004210 if (err)
4211 return err;
4212
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004213 if (is_pointer_value(env, insn->dst_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004214 verbose(env, "R%d pointer arithmetic prohibited\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004215 insn->dst_reg);
4216 return -EACCES;
4217 }
4218
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004219 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01004220 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004221 if (err)
4222 return err;
4223
4224 } else if (opcode == BPF_MOV) {
4225
4226 if (BPF_SRC(insn->code) == BPF_X) {
4227 if (insn->imm != 0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004228 verbose(env, "BPF_MOV uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004229 return -EINVAL;
4230 }
4231
4232 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01004233 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004234 if (err)
4235 return err;
4236 } else {
4237 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004238 verbose(env, "BPF_MOV uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004239 return -EINVAL;
4240 }
4241 }
4242
Arthur Fabrefbeb1602018-07-31 18:17:22 +01004243 /* check dest operand, mark as required later */
4244 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004245 if (err)
4246 return err;
4247
4248 if (BPF_SRC(insn->code) == BPF_X) {
Jiong Wange434b8c2018-12-07 12:16:18 -05004249 struct bpf_reg_state *src_reg = regs + insn->src_reg;
4250 struct bpf_reg_state *dst_reg = regs + insn->dst_reg;
4251
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004252 if (BPF_CLASS(insn->code) == BPF_ALU64) {
4253 /* case: R1 = R2
4254 * copy register state to dest reg
4255 */
Jiong Wange434b8c2018-12-07 12:16:18 -05004256 *dst_reg = *src_reg;
4257 dst_reg->live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004258 } else {
Edward Creef1174f72017-08-07 15:26:19 +01004259 /* R1 = (u32) R2 */
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004260 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004261 verbose(env,
4262 "R%d partial copy of pointer\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004263 insn->src_reg);
4264 return -EACCES;
Jiong Wange434b8c2018-12-07 12:16:18 -05004265 } else if (src_reg->type == SCALAR_VALUE) {
4266 *dst_reg = *src_reg;
4267 dst_reg->live |= REG_LIVE_WRITTEN;
4268 } else {
4269 mark_reg_unknown(env, regs,
4270 insn->dst_reg);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004271 }
Jiong Wange434b8c2018-12-07 12:16:18 -05004272 coerce_reg_to_size(dst_reg, 4);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004273 }
4274 } else {
4275 /* case: R = imm
4276 * remember the value we stored into this reg
4277 */
Arthur Fabrefbeb1602018-07-31 18:17:22 +01004278 /* clear any state __mark_reg_known doesn't set */
4279 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004280 regs[insn->dst_reg].type = SCALAR_VALUE;
Jann Horn95a762e2017-12-18 20:11:54 -08004281 if (BPF_CLASS(insn->code) == BPF_ALU64) {
4282 __mark_reg_known(regs + insn->dst_reg,
4283 insn->imm);
4284 } else {
4285 __mark_reg_known(regs + insn->dst_reg,
4286 (u32)insn->imm);
4287 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004288 }
4289
4290 } else if (opcode > BPF_END) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004291 verbose(env, "invalid BPF_ALU opcode %x\n", opcode);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004292 return -EINVAL;
4293
4294 } else { /* all other ALU ops: and, sub, xor, add, ... */
4295
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004296 if (BPF_SRC(insn->code) == BPF_X) {
4297 if (insn->imm != 0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004298 verbose(env, "BPF_ALU uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004299 return -EINVAL;
4300 }
4301 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01004302 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004303 if (err)
4304 return err;
4305 } else {
4306 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004307 verbose(env, "BPF_ALU uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004308 return -EINVAL;
4309 }
4310 }
4311
4312 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01004313 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004314 if (err)
4315 return err;
4316
4317 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
4318 BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004319 verbose(env, "div by zero\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004320 return -EINVAL;
4321 }
4322
Rabin Vincent229394e82016-01-12 20:17:08 +01004323 if ((opcode == BPF_LSH || opcode == BPF_RSH ||
4324 opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
4325 int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
4326
4327 if (insn->imm < 0 || insn->imm >= size) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004328 verbose(env, "invalid shift %d\n", insn->imm);
Rabin Vincent229394e82016-01-12 20:17:08 +01004329 return -EINVAL;
4330 }
4331 }
4332
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07004333 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01004334 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07004335 if (err)
4336 return err;
4337
Edward Creef1174f72017-08-07 15:26:19 +01004338 return adjust_reg_min_max_vals(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004339 }
4340
4341 return 0;
4342}
4343
Paul Chaignonc6a9efa2019-04-24 21:50:42 +02004344static void __find_good_pkt_pointers(struct bpf_func_state *state,
4345 struct bpf_reg_state *dst_reg,
4346 enum bpf_reg_type type, u16 new_range)
4347{
4348 struct bpf_reg_state *reg;
4349 int i;
4350
4351 for (i = 0; i < MAX_BPF_REG; i++) {
4352 reg = &state->regs[i];
4353 if (reg->type == type && reg->id == dst_reg->id)
4354 /* keep the maximum range already checked */
4355 reg->range = max(reg->range, new_range);
4356 }
4357
4358 bpf_for_each_spilled_reg(i, state, reg) {
4359 if (!reg)
4360 continue;
4361 if (reg->type == type && reg->id == dst_reg->id)
4362 reg->range = max(reg->range, new_range);
4363 }
4364}
4365
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004366static void find_good_pkt_pointers(struct bpf_verifier_state *vstate,
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004367 struct bpf_reg_state *dst_reg,
David S. Millerf8ddadc2017-10-22 13:36:53 +01004368 enum bpf_reg_type type,
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004369 bool range_right_open)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004370{
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004371 u16 new_range;
Paul Chaignonc6a9efa2019-04-24 21:50:42 +02004372 int i;
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004373
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004374 if (dst_reg->off < 0 ||
4375 (dst_reg->off == 0 && range_right_open))
Edward Creef1174f72017-08-07 15:26:19 +01004376 /* This doesn't give us any range */
4377 return;
4378
Edward Creeb03c9f92017-08-07 15:26:36 +01004379 if (dst_reg->umax_value > MAX_PACKET_OFF ||
4380 dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF)
Edward Creef1174f72017-08-07 15:26:19 +01004381 /* Risk of overflow. For instance, ptr + (1<<63) may be less
4382 * than pkt_end, but that's because it's also less than pkt.
4383 */
4384 return;
4385
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004386 new_range = dst_reg->off;
4387 if (range_right_open)
4388 new_range--;
4389
4390 /* Examples for register markings:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004391 *
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004392 * pkt_data in dst register:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004393 *
4394 * r2 = r3;
4395 * r2 += 8;
4396 * if (r2 > pkt_end) goto <handle exception>
4397 * <access okay>
4398 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004399 * r2 = r3;
4400 * r2 += 8;
4401 * if (r2 < pkt_end) goto <access okay>
4402 * <handle exception>
4403 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004404 * Where:
4405 * r2 == dst_reg, pkt_end == src_reg
4406 * r2=pkt(id=n,off=8,r=0)
4407 * r3=pkt(id=n,off=0,r=0)
4408 *
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004409 * pkt_data in src register:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004410 *
4411 * r2 = r3;
4412 * r2 += 8;
4413 * if (pkt_end >= r2) goto <access okay>
4414 * <handle exception>
4415 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004416 * r2 = r3;
4417 * r2 += 8;
4418 * if (pkt_end <= r2) goto <handle exception>
4419 * <access okay>
4420 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004421 * Where:
4422 * pkt_end == dst_reg, r2 == src_reg
4423 * r2=pkt(id=n,off=8,r=0)
4424 * r3=pkt(id=n,off=0,r=0)
4425 *
4426 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004427 * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8)
4428 * and [r3, r3 + 8-1) respectively is safe to access depending on
4429 * the check.
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004430 */
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004431
Edward Creef1174f72017-08-07 15:26:19 +01004432 /* If our ids match, then we must have the same max_value. And we
4433 * don't care about the other reg's fixed offset, since if it's too big
4434 * the range won't allow anything.
4435 * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16.
4436 */
Paul Chaignonc6a9efa2019-04-24 21:50:42 +02004437 for (i = 0; i <= vstate->curframe; i++)
4438 __find_good_pkt_pointers(vstate->frame[i], dst_reg, type,
4439 new_range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004440}
4441
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004442/* compute branch direction of the expression "if (reg opcode val) goto target;"
4443 * and return:
4444 * 1 - branch will be taken and "goto target" will be executed
4445 * 0 - branch will not be taken and fall-through to next insn
4446 * -1 - unknown. Example: "if (reg < 5)" is unknown when register value range [0,10]
4447 */
Jiong Wang092ed092019-01-26 12:26:01 -05004448static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode,
4449 bool is_jmp32)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004450{
Jiong Wang092ed092019-01-26 12:26:01 -05004451 struct bpf_reg_state reg_lo;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004452 s64 sval;
4453
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004454 if (__is_pointer_value(false, reg))
4455 return -1;
4456
Jiong Wang092ed092019-01-26 12:26:01 -05004457 if (is_jmp32) {
4458 reg_lo = *reg;
4459 reg = &reg_lo;
4460 /* For JMP32, only low 32 bits are compared, coerce_reg_to_size
4461 * could truncate high bits and update umin/umax according to
4462 * information of low bits.
4463 */
4464 coerce_reg_to_size(reg, 4);
4465 /* smin/smax need special handling. For example, after coerce,
4466 * if smin_value is 0x00000000ffffffffLL, the value is -1 when
4467 * used as operand to JMP32. It is a negative number from s32's
4468 * point of view, while it is a positive number when seen as
4469 * s64. The smin/smax are kept as s64, therefore, when used with
4470 * JMP32, they need to be transformed into s32, then sign
4471 * extended back to s64.
4472 *
4473 * Also, smin/smax were copied from umin/umax. If umin/umax has
4474 * different sign bit, then min/max relationship doesn't
4475 * maintain after casting into s32, for this case, set smin/smax
4476 * to safest range.
4477 */
4478 if ((reg->umax_value ^ reg->umin_value) &
4479 (1ULL << 31)) {
4480 reg->smin_value = S32_MIN;
4481 reg->smax_value = S32_MAX;
4482 }
4483 reg->smin_value = (s64)(s32)reg->smin_value;
4484 reg->smax_value = (s64)(s32)reg->smax_value;
4485
4486 val = (u32)val;
4487 sval = (s64)(s32)val;
4488 } else {
4489 sval = (s64)val;
4490 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004491
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004492 switch (opcode) {
4493 case BPF_JEQ:
4494 if (tnum_is_const(reg->var_off))
4495 return !!tnum_equals_const(reg->var_off, val);
4496 break;
4497 case BPF_JNE:
4498 if (tnum_is_const(reg->var_off))
4499 return !tnum_equals_const(reg->var_off, val);
4500 break;
Jakub Kicinski960ea052018-12-19 22:13:04 -08004501 case BPF_JSET:
4502 if ((~reg->var_off.mask & reg->var_off.value) & val)
4503 return 1;
4504 if (!((reg->var_off.mask | reg->var_off.value) & val))
4505 return 0;
4506 break;
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004507 case BPF_JGT:
4508 if (reg->umin_value > val)
4509 return 1;
4510 else if (reg->umax_value <= val)
4511 return 0;
4512 break;
4513 case BPF_JSGT:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004514 if (reg->smin_value > sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004515 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004516 else if (reg->smax_value < sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004517 return 0;
4518 break;
4519 case BPF_JLT:
4520 if (reg->umax_value < val)
4521 return 1;
4522 else if (reg->umin_value >= val)
4523 return 0;
4524 break;
4525 case BPF_JSLT:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004526 if (reg->smax_value < sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004527 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004528 else if (reg->smin_value >= sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004529 return 0;
4530 break;
4531 case BPF_JGE:
4532 if (reg->umin_value >= val)
4533 return 1;
4534 else if (reg->umax_value < val)
4535 return 0;
4536 break;
4537 case BPF_JSGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004538 if (reg->smin_value >= sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004539 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004540 else if (reg->smax_value < sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004541 return 0;
4542 break;
4543 case BPF_JLE:
4544 if (reg->umax_value <= val)
4545 return 1;
4546 else if (reg->umin_value > val)
4547 return 0;
4548 break;
4549 case BPF_JSLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004550 if (reg->smax_value <= sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004551 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004552 else if (reg->smin_value > sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004553 return 0;
4554 break;
4555 }
4556
4557 return -1;
4558}
4559
Jiong Wang092ed092019-01-26 12:26:01 -05004560/* Generate min value of the high 32-bit from TNUM info. */
4561static u64 gen_hi_min(struct tnum var)
4562{
4563 return var.value & ~0xffffffffULL;
4564}
4565
4566/* Generate max value of the high 32-bit from TNUM info. */
4567static u64 gen_hi_max(struct tnum var)
4568{
4569 return (var.value | var.mask) & ~0xffffffffULL;
4570}
4571
4572/* Return true if VAL is compared with a s64 sign extended from s32, and they
4573 * are with the same signedness.
4574 */
4575static bool cmp_val_with_extended_s64(s64 sval, struct bpf_reg_state *reg)
4576{
4577 return ((s32)sval >= 0 &&
4578 reg->smin_value >= 0 && reg->smax_value <= S32_MAX) ||
4579 ((s32)sval < 0 &&
4580 reg->smax_value <= 0 && reg->smin_value >= S32_MIN);
4581}
4582
Josef Bacik48461132016-09-28 10:54:32 -04004583/* Adjusts the register min/max values in the case that the dst_reg is the
4584 * variable register that we are working on, and src_reg is a constant or we're
4585 * simply doing a BPF_K check.
Edward Creef1174f72017-08-07 15:26:19 +01004586 * In JEQ/JNE cases we also adjust the var_off values.
Josef Bacik48461132016-09-28 10:54:32 -04004587 */
4588static void reg_set_min_max(struct bpf_reg_state *true_reg,
4589 struct bpf_reg_state *false_reg, u64 val,
Jiong Wang092ed092019-01-26 12:26:01 -05004590 u8 opcode, bool is_jmp32)
Josef Bacik48461132016-09-28 10:54:32 -04004591{
Jiong Wanga72dafa2019-01-26 12:26:00 -05004592 s64 sval;
4593
Edward Creef1174f72017-08-07 15:26:19 +01004594 /* If the dst_reg is a pointer, we can't learn anything about its
4595 * variable offset from the compare (unless src_reg were a pointer into
4596 * the same object, but we don't bother with that.
4597 * Since false_reg and true_reg have the same type by construction, we
4598 * only need to check one of them for pointerness.
4599 */
4600 if (__is_pointer_value(false, false_reg))
4601 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02004602
Jiong Wang092ed092019-01-26 12:26:01 -05004603 val = is_jmp32 ? (u32)val : val;
4604 sval = is_jmp32 ? (s64)(s32)val : (s64)val;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004605
Josef Bacik48461132016-09-28 10:54:32 -04004606 switch (opcode) {
4607 case BPF_JEQ:
Josef Bacik48461132016-09-28 10:54:32 -04004608 case BPF_JNE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004609 {
4610 struct bpf_reg_state *reg =
4611 opcode == BPF_JEQ ? true_reg : false_reg;
4612
4613 /* For BPF_JEQ, if this is false we know nothing Jon Snow, but
4614 * if it is true we know the value for sure. Likewise for
4615 * BPF_JNE.
Josef Bacik48461132016-09-28 10:54:32 -04004616 */
Jiong Wang092ed092019-01-26 12:26:01 -05004617 if (is_jmp32) {
4618 u64 old_v = reg->var_off.value;
4619 u64 hi_mask = ~0xffffffffULL;
4620
4621 reg->var_off.value = (old_v & hi_mask) | val;
4622 reg->var_off.mask &= hi_mask;
4623 } else {
4624 __mark_reg_known(reg, val);
4625 }
Josef Bacik48461132016-09-28 10:54:32 -04004626 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004627 }
Jakub Kicinski960ea052018-12-19 22:13:04 -08004628 case BPF_JSET:
4629 false_reg->var_off = tnum_and(false_reg->var_off,
4630 tnum_const(~val));
4631 if (is_power_of_2(val))
4632 true_reg->var_off = tnum_or(true_reg->var_off,
4633 tnum_const(val));
4634 break;
Josef Bacik48461132016-09-28 10:54:32 -04004635 case BPF_JGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004636 case BPF_JGT:
4637 {
4638 u64 false_umax = opcode == BPF_JGT ? val : val - 1;
4639 u64 true_umin = opcode == BPF_JGT ? val + 1 : val;
4640
Jiong Wang092ed092019-01-26 12:26:01 -05004641 if (is_jmp32) {
4642 false_umax += gen_hi_max(false_reg->var_off);
4643 true_umin += gen_hi_min(true_reg->var_off);
4644 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004645 false_reg->umax_value = min(false_reg->umax_value, false_umax);
4646 true_reg->umin_value = max(true_reg->umin_value, true_umin);
Edward Creeb03c9f92017-08-07 15:26:36 +01004647 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004648 }
Josef Bacik48461132016-09-28 10:54:32 -04004649 case BPF_JSGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004650 case BPF_JSGT:
4651 {
4652 s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1;
4653 s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval;
4654
Jiong Wang092ed092019-01-26 12:26:01 -05004655 /* If the full s64 was not sign-extended from s32 then don't
4656 * deduct further info.
4657 */
4658 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
4659 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004660 false_reg->smax_value = min(false_reg->smax_value, false_smax);
4661 true_reg->smin_value = max(true_reg->smin_value, true_smin);
Josef Bacik48461132016-09-28 10:54:32 -04004662 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004663 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004664 case BPF_JLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004665 case BPF_JLT:
4666 {
4667 u64 false_umin = opcode == BPF_JLT ? val : val + 1;
4668 u64 true_umax = opcode == BPF_JLT ? val - 1 : val;
4669
Jiong Wang092ed092019-01-26 12:26:01 -05004670 if (is_jmp32) {
4671 false_umin += gen_hi_min(false_reg->var_off);
4672 true_umax += gen_hi_max(true_reg->var_off);
4673 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004674 false_reg->umin_value = max(false_reg->umin_value, false_umin);
4675 true_reg->umax_value = min(true_reg->umax_value, true_umax);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004676 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004677 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004678 case BPF_JSLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004679 case BPF_JSLT:
4680 {
4681 s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1;
4682 s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval;
4683
Jiong Wang092ed092019-01-26 12:26:01 -05004684 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
4685 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004686 false_reg->smin_value = max(false_reg->smin_value, false_smin);
4687 true_reg->smax_value = min(true_reg->smax_value, true_smax);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004688 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004689 }
Josef Bacik48461132016-09-28 10:54:32 -04004690 default:
4691 break;
4692 }
4693
Edward Creeb03c9f92017-08-07 15:26:36 +01004694 __reg_deduce_bounds(false_reg);
4695 __reg_deduce_bounds(true_reg);
4696 /* We might have learned some bits from the bounds. */
4697 __reg_bound_offset(false_reg);
4698 __reg_bound_offset(true_reg);
4699 /* Intersecting with the old var_off might have improved our bounds
4700 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
4701 * then new var_off is (0; 0x7f...fc) which improves our umax.
4702 */
4703 __update_reg_bounds(false_reg);
4704 __update_reg_bounds(true_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004705}
4706
Edward Creef1174f72017-08-07 15:26:19 +01004707/* Same as above, but for the case that dst_reg holds a constant and src_reg is
4708 * the variable reg.
Josef Bacik48461132016-09-28 10:54:32 -04004709 */
4710static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
4711 struct bpf_reg_state *false_reg, u64 val,
Jiong Wang092ed092019-01-26 12:26:01 -05004712 u8 opcode, bool is_jmp32)
Josef Bacik48461132016-09-28 10:54:32 -04004713{
Jiong Wanga72dafa2019-01-26 12:26:00 -05004714 s64 sval;
4715
Edward Creef1174f72017-08-07 15:26:19 +01004716 if (__is_pointer_value(false, false_reg))
4717 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02004718
Jiong Wang092ed092019-01-26 12:26:01 -05004719 val = is_jmp32 ? (u32)val : val;
4720 sval = is_jmp32 ? (s64)(s32)val : (s64)val;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004721
Josef Bacik48461132016-09-28 10:54:32 -04004722 switch (opcode) {
4723 case BPF_JEQ:
Josef Bacik48461132016-09-28 10:54:32 -04004724 case BPF_JNE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004725 {
4726 struct bpf_reg_state *reg =
4727 opcode == BPF_JEQ ? true_reg : false_reg;
4728
Jiong Wang092ed092019-01-26 12:26:01 -05004729 if (is_jmp32) {
4730 u64 old_v = reg->var_off.value;
4731 u64 hi_mask = ~0xffffffffULL;
4732
4733 reg->var_off.value = (old_v & hi_mask) | val;
4734 reg->var_off.mask &= hi_mask;
4735 } else {
4736 __mark_reg_known(reg, val);
4737 }
Josef Bacik48461132016-09-28 10:54:32 -04004738 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004739 }
Jakub Kicinski960ea052018-12-19 22:13:04 -08004740 case BPF_JSET:
4741 false_reg->var_off = tnum_and(false_reg->var_off,
4742 tnum_const(~val));
4743 if (is_power_of_2(val))
4744 true_reg->var_off = tnum_or(true_reg->var_off,
4745 tnum_const(val));
4746 break;
Josef Bacik48461132016-09-28 10:54:32 -04004747 case BPF_JGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004748 case BPF_JGT:
4749 {
4750 u64 false_umin = opcode == BPF_JGT ? val : val + 1;
4751 u64 true_umax = opcode == BPF_JGT ? val - 1 : val;
4752
Jiong Wang092ed092019-01-26 12:26:01 -05004753 if (is_jmp32) {
4754 false_umin += gen_hi_min(false_reg->var_off);
4755 true_umax += gen_hi_max(true_reg->var_off);
4756 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004757 false_reg->umin_value = max(false_reg->umin_value, false_umin);
4758 true_reg->umax_value = min(true_reg->umax_value, true_umax);
Edward Creeb03c9f92017-08-07 15:26:36 +01004759 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004760 }
Josef Bacik48461132016-09-28 10:54:32 -04004761 case BPF_JSGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004762 case BPF_JSGT:
4763 {
4764 s64 false_smin = opcode == BPF_JSGT ? sval : sval + 1;
4765 s64 true_smax = opcode == BPF_JSGT ? sval - 1 : sval;
4766
Jiong Wang092ed092019-01-26 12:26:01 -05004767 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
4768 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004769 false_reg->smin_value = max(false_reg->smin_value, false_smin);
4770 true_reg->smax_value = min(true_reg->smax_value, true_smax);
Josef Bacik48461132016-09-28 10:54:32 -04004771 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004772 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004773 case BPF_JLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004774 case BPF_JLT:
4775 {
4776 u64 false_umax = opcode == BPF_JLT ? val : val - 1;
4777 u64 true_umin = opcode == BPF_JLT ? val + 1 : val;
4778
Jiong Wang092ed092019-01-26 12:26:01 -05004779 if (is_jmp32) {
4780 false_umax += gen_hi_max(false_reg->var_off);
4781 true_umin += gen_hi_min(true_reg->var_off);
4782 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004783 false_reg->umax_value = min(false_reg->umax_value, false_umax);
4784 true_reg->umin_value = max(true_reg->umin_value, true_umin);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004785 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004786 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004787 case BPF_JSLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004788 case BPF_JSLT:
4789 {
4790 s64 false_smax = opcode == BPF_JSLT ? sval : sval - 1;
4791 s64 true_smin = opcode == BPF_JSLT ? sval + 1 : sval;
4792
Jiong Wang092ed092019-01-26 12:26:01 -05004793 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
4794 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004795 false_reg->smax_value = min(false_reg->smax_value, false_smax);
4796 true_reg->smin_value = max(true_reg->smin_value, true_smin);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004797 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004798 }
Josef Bacik48461132016-09-28 10:54:32 -04004799 default:
4800 break;
4801 }
4802
Edward Creeb03c9f92017-08-07 15:26:36 +01004803 __reg_deduce_bounds(false_reg);
4804 __reg_deduce_bounds(true_reg);
4805 /* We might have learned some bits from the bounds. */
4806 __reg_bound_offset(false_reg);
4807 __reg_bound_offset(true_reg);
4808 /* Intersecting with the old var_off might have improved our bounds
4809 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
4810 * then new var_off is (0; 0x7f...fc) which improves our umax.
4811 */
4812 __update_reg_bounds(false_reg);
4813 __update_reg_bounds(true_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004814}
4815
4816/* Regs are known to be equal, so intersect their min/max/var_off */
4817static void __reg_combine_min_max(struct bpf_reg_state *src_reg,
4818 struct bpf_reg_state *dst_reg)
4819{
Edward Creeb03c9f92017-08-07 15:26:36 +01004820 src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value,
4821 dst_reg->umin_value);
4822 src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value,
4823 dst_reg->umax_value);
4824 src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value,
4825 dst_reg->smin_value);
4826 src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value,
4827 dst_reg->smax_value);
Edward Creef1174f72017-08-07 15:26:19 +01004828 src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off,
4829 dst_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01004830 /* We might have learned new bounds from the var_off. */
4831 __update_reg_bounds(src_reg);
4832 __update_reg_bounds(dst_reg);
4833 /* We might have learned something about the sign bit. */
4834 __reg_deduce_bounds(src_reg);
4835 __reg_deduce_bounds(dst_reg);
4836 /* We might have learned some bits from the bounds. */
4837 __reg_bound_offset(src_reg);
4838 __reg_bound_offset(dst_reg);
4839 /* Intersecting with the old var_off might have improved our bounds
4840 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
4841 * then new var_off is (0; 0x7f...fc) which improves our umax.
4842 */
4843 __update_reg_bounds(src_reg);
4844 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004845}
4846
4847static void reg_combine_min_max(struct bpf_reg_state *true_src,
4848 struct bpf_reg_state *true_dst,
4849 struct bpf_reg_state *false_src,
4850 struct bpf_reg_state *false_dst,
4851 u8 opcode)
4852{
4853 switch (opcode) {
4854 case BPF_JEQ:
4855 __reg_combine_min_max(true_src, true_dst);
4856 break;
4857 case BPF_JNE:
4858 __reg_combine_min_max(false_src, false_dst);
Edward Creeb03c9f92017-08-07 15:26:36 +01004859 break;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02004860 }
Josef Bacik48461132016-09-28 10:54:32 -04004861}
4862
Joe Stringerfd978bf72018-10-02 13:35:35 -07004863static void mark_ptr_or_null_reg(struct bpf_func_state *state,
4864 struct bpf_reg_state *reg, u32 id,
Joe Stringer840b9612018-10-02 13:35:32 -07004865 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02004866{
Joe Stringer840b9612018-10-02 13:35:32 -07004867 if (reg_type_may_be_null(reg->type) && reg->id == id) {
Edward Creef1174f72017-08-07 15:26:19 +01004868 /* Old offset (both fixed and variable parts) should
4869 * have been known-zero, because we don't allow pointer
4870 * arithmetic on pointers that might be NULL.
4871 */
Edward Creeb03c9f92017-08-07 15:26:36 +01004872 if (WARN_ON_ONCE(reg->smin_value || reg->smax_value ||
4873 !tnum_equals_const(reg->var_off, 0) ||
Edward Creef1174f72017-08-07 15:26:19 +01004874 reg->off)) {
Edward Creeb03c9f92017-08-07 15:26:36 +01004875 __mark_reg_known_zero(reg);
4876 reg->off = 0;
Edward Creef1174f72017-08-07 15:26:19 +01004877 }
4878 if (is_null) {
4879 reg->type = SCALAR_VALUE;
Joe Stringer840b9612018-10-02 13:35:32 -07004880 } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
4881 if (reg->map_ptr->inner_map_meta) {
4882 reg->type = CONST_PTR_TO_MAP;
4883 reg->map_ptr = reg->map_ptr->inner_map_meta;
4884 } else {
4885 reg->type = PTR_TO_MAP_VALUE;
4886 }
Joe Stringerc64b7982018-10-02 13:35:33 -07004887 } else if (reg->type == PTR_TO_SOCKET_OR_NULL) {
4888 reg->type = PTR_TO_SOCKET;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08004889 } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) {
4890 reg->type = PTR_TO_SOCK_COMMON;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08004891 } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) {
4892 reg->type = PTR_TO_TCP_SOCK;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004893 }
Martin KaFai Lau1b986582019-03-12 10:23:02 -07004894 if (is_null) {
4895 /* We don't need id and ref_obj_id from this point
4896 * onwards anymore, thus we should better reset it,
4897 * so that state pruning has chances to take effect.
4898 */
4899 reg->id = 0;
4900 reg->ref_obj_id = 0;
4901 } else if (!reg_may_point_to_spin_lock(reg)) {
4902 /* For not-NULL ptr, reg->ref_obj_id will be reset
4903 * in release_reg_references().
4904 *
4905 * reg->id is still used by spin_lock ptr. Other
4906 * than spin_lock ptr type, reg->id can be reset.
Joe Stringerfd978bf72018-10-02 13:35:35 -07004907 */
4908 reg->id = 0;
4909 }
Thomas Graf57a09bf2016-10-18 19:51:19 +02004910 }
4911}
4912
Paul Chaignonc6a9efa2019-04-24 21:50:42 +02004913static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id,
4914 bool is_null)
4915{
4916 struct bpf_reg_state *reg;
4917 int i;
4918
4919 for (i = 0; i < MAX_BPF_REG; i++)
4920 mark_ptr_or_null_reg(state, &state->regs[i], id, is_null);
4921
4922 bpf_for_each_spilled_reg(i, state, reg) {
4923 if (!reg)
4924 continue;
4925 mark_ptr_or_null_reg(state, reg, id, is_null);
4926 }
4927}
4928
Thomas Graf57a09bf2016-10-18 19:51:19 +02004929/* The logic is similar to find_good_pkt_pointers(), both could eventually
4930 * be folded together at some point.
4931 */
Joe Stringer840b9612018-10-02 13:35:32 -07004932static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno,
4933 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02004934{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004935 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Paul Chaignonc6a9efa2019-04-24 21:50:42 +02004936 struct bpf_reg_state *regs = state->regs;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07004937 u32 ref_obj_id = regs[regno].ref_obj_id;
Daniel Borkmanna08dd0d2016-12-15 01:30:06 +01004938 u32 id = regs[regno].id;
Paul Chaignonc6a9efa2019-04-24 21:50:42 +02004939 int i;
Thomas Graf57a09bf2016-10-18 19:51:19 +02004940
Martin KaFai Lau1b986582019-03-12 10:23:02 -07004941 if (ref_obj_id && ref_obj_id == id && is_null)
4942 /* regs[regno] is in the " == NULL" branch.
4943 * No one could have freed the reference state before
4944 * doing the NULL check.
4945 */
4946 WARN_ON_ONCE(release_reference_state(state, id));
Joe Stringerfd978bf72018-10-02 13:35:35 -07004947
Paul Chaignonc6a9efa2019-04-24 21:50:42 +02004948 for (i = 0; i <= vstate->curframe; i++)
4949 __mark_ptr_or_null_regs(vstate->frame[i], id, is_null);
Thomas Graf57a09bf2016-10-18 19:51:19 +02004950}
4951
Daniel Borkmann5beca082017-11-01 23:58:10 +01004952static bool try_match_pkt_pointers(const struct bpf_insn *insn,
4953 struct bpf_reg_state *dst_reg,
4954 struct bpf_reg_state *src_reg,
4955 struct bpf_verifier_state *this_branch,
4956 struct bpf_verifier_state *other_branch)
4957{
4958 if (BPF_SRC(insn->code) != BPF_X)
4959 return false;
4960
Jiong Wang092ed092019-01-26 12:26:01 -05004961 /* Pointers are always 64-bit. */
4962 if (BPF_CLASS(insn->code) == BPF_JMP32)
4963 return false;
4964
Daniel Borkmann5beca082017-11-01 23:58:10 +01004965 switch (BPF_OP(insn->code)) {
4966 case BPF_JGT:
4967 if ((dst_reg->type == PTR_TO_PACKET &&
4968 src_reg->type == PTR_TO_PACKET_END) ||
4969 (dst_reg->type == PTR_TO_PACKET_META &&
4970 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
4971 /* pkt_data' > pkt_end, pkt_meta' > pkt_data */
4972 find_good_pkt_pointers(this_branch, dst_reg,
4973 dst_reg->type, false);
4974 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
4975 src_reg->type == PTR_TO_PACKET) ||
4976 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
4977 src_reg->type == PTR_TO_PACKET_META)) {
4978 /* pkt_end > pkt_data', pkt_data > pkt_meta' */
4979 find_good_pkt_pointers(other_branch, src_reg,
4980 src_reg->type, true);
4981 } else {
4982 return false;
4983 }
4984 break;
4985 case BPF_JLT:
4986 if ((dst_reg->type == PTR_TO_PACKET &&
4987 src_reg->type == PTR_TO_PACKET_END) ||
4988 (dst_reg->type == PTR_TO_PACKET_META &&
4989 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
4990 /* pkt_data' < pkt_end, pkt_meta' < pkt_data */
4991 find_good_pkt_pointers(other_branch, dst_reg,
4992 dst_reg->type, true);
4993 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
4994 src_reg->type == PTR_TO_PACKET) ||
4995 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
4996 src_reg->type == PTR_TO_PACKET_META)) {
4997 /* pkt_end < pkt_data', pkt_data > pkt_meta' */
4998 find_good_pkt_pointers(this_branch, src_reg,
4999 src_reg->type, false);
5000 } else {
5001 return false;
5002 }
5003 break;
5004 case BPF_JGE:
5005 if ((dst_reg->type == PTR_TO_PACKET &&
5006 src_reg->type == PTR_TO_PACKET_END) ||
5007 (dst_reg->type == PTR_TO_PACKET_META &&
5008 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
5009 /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */
5010 find_good_pkt_pointers(this_branch, dst_reg,
5011 dst_reg->type, true);
5012 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
5013 src_reg->type == PTR_TO_PACKET) ||
5014 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
5015 src_reg->type == PTR_TO_PACKET_META)) {
5016 /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */
5017 find_good_pkt_pointers(other_branch, src_reg,
5018 src_reg->type, false);
5019 } else {
5020 return false;
5021 }
5022 break;
5023 case BPF_JLE:
5024 if ((dst_reg->type == PTR_TO_PACKET &&
5025 src_reg->type == PTR_TO_PACKET_END) ||
5026 (dst_reg->type == PTR_TO_PACKET_META &&
5027 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
5028 /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */
5029 find_good_pkt_pointers(other_branch, dst_reg,
5030 dst_reg->type, false);
5031 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
5032 src_reg->type == PTR_TO_PACKET) ||
5033 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
5034 src_reg->type == PTR_TO_PACKET_META)) {
5035 /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */
5036 find_good_pkt_pointers(this_branch, src_reg,
5037 src_reg->type, true);
5038 } else {
5039 return false;
5040 }
5041 break;
5042 default:
5043 return false;
5044 }
5045
5046 return true;
5047}
5048
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005049static int check_cond_jmp_op(struct bpf_verifier_env *env,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005050 struct bpf_insn *insn, int *insn_idx)
5051{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005052 struct bpf_verifier_state *this_branch = env->cur_state;
5053 struct bpf_verifier_state *other_branch;
5054 struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs;
5055 struct bpf_reg_state *dst_reg, *other_branch_regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005056 u8 opcode = BPF_OP(insn->code);
Jiong Wang092ed092019-01-26 12:26:01 -05005057 bool is_jmp32;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005058 int err;
5059
Jiong Wang092ed092019-01-26 12:26:01 -05005060 /* Only conditional jumps are expected to reach here. */
5061 if (opcode == BPF_JA || opcode > BPF_JSLE) {
5062 verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005063 return -EINVAL;
5064 }
5065
5066 if (BPF_SRC(insn->code) == BPF_X) {
5067 if (insn->imm != 0) {
Jiong Wang092ed092019-01-26 12:26:01 -05005068 verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005069 return -EINVAL;
5070 }
5071
5072 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01005073 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005074 if (err)
5075 return err;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07005076
5077 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005078 verbose(env, "R%d pointer comparison prohibited\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07005079 insn->src_reg);
5080 return -EACCES;
5081 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005082 } else {
5083 if (insn->src_reg != BPF_REG_0) {
Jiong Wang092ed092019-01-26 12:26:01 -05005084 verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005085 return -EINVAL;
5086 }
5087 }
5088
5089 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01005090 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005091 if (err)
5092 return err;
5093
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07005094 dst_reg = &regs[insn->dst_reg];
Jiong Wang092ed092019-01-26 12:26:01 -05005095 is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32;
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07005096
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08005097 if (BPF_SRC(insn->code) == BPF_K) {
Jiong Wang092ed092019-01-26 12:26:01 -05005098 int pred = is_branch_taken(dst_reg, insn->imm, opcode,
5099 is_jmp32);
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08005100
5101 if (pred == 1) {
5102 /* only follow the goto, ignore fall-through */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005103 *insn_idx += insn->off;
5104 return 0;
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08005105 } else if (pred == 0) {
5106 /* only follow fall-through branch, since
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005107 * that's where the program will go
5108 */
5109 return 0;
5110 }
5111 }
5112
Daniel Borkmann979d63d2019-01-03 00:58:34 +01005113 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx,
5114 false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005115 if (!other_branch)
5116 return -EFAULT;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005117 other_branch_regs = other_branch->frame[other_branch->curframe]->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005118
Josef Bacik48461132016-09-28 10:54:32 -04005119 /* detect if we are comparing against a constant value so we can adjust
5120 * our min/max values for our dst register.
Edward Creef1174f72017-08-07 15:26:19 +01005121 * this is only legit if both are scalars (or pointers to the same
5122 * object, I suppose, but we don't support that right now), because
5123 * otherwise the different base pointers mean the offsets aren't
5124 * comparable.
Josef Bacik48461132016-09-28 10:54:32 -04005125 */
5126 if (BPF_SRC(insn->code) == BPF_X) {
Jiong Wang092ed092019-01-26 12:26:01 -05005127 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
5128 struct bpf_reg_state lo_reg0 = *dst_reg;
5129 struct bpf_reg_state lo_reg1 = *src_reg;
5130 struct bpf_reg_state *src_lo, *dst_lo;
5131
5132 dst_lo = &lo_reg0;
5133 src_lo = &lo_reg1;
5134 coerce_reg_to_size(dst_lo, 4);
5135 coerce_reg_to_size(src_lo, 4);
5136
Edward Creef1174f72017-08-07 15:26:19 +01005137 if (dst_reg->type == SCALAR_VALUE &&
Jiong Wang092ed092019-01-26 12:26:01 -05005138 src_reg->type == SCALAR_VALUE) {
5139 if (tnum_is_const(src_reg->var_off) ||
5140 (is_jmp32 && tnum_is_const(src_lo->var_off)))
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005141 reg_set_min_max(&other_branch_regs[insn->dst_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05005142 dst_reg,
5143 is_jmp32
5144 ? src_lo->var_off.value
5145 : src_reg->var_off.value,
5146 opcode, is_jmp32);
5147 else if (tnum_is_const(dst_reg->var_off) ||
5148 (is_jmp32 && tnum_is_const(dst_lo->var_off)))
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005149 reg_set_min_max_inv(&other_branch_regs[insn->src_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05005150 src_reg,
5151 is_jmp32
5152 ? dst_lo->var_off.value
5153 : dst_reg->var_off.value,
5154 opcode, is_jmp32);
5155 else if (!is_jmp32 &&
5156 (opcode == BPF_JEQ || opcode == BPF_JNE))
Edward Creef1174f72017-08-07 15:26:19 +01005157 /* Comparing for equality, we can combine knowledge */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005158 reg_combine_min_max(&other_branch_regs[insn->src_reg],
5159 &other_branch_regs[insn->dst_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05005160 src_reg, dst_reg, opcode);
Edward Creef1174f72017-08-07 15:26:19 +01005161 }
5162 } else if (dst_reg->type == SCALAR_VALUE) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005163 reg_set_min_max(&other_branch_regs[insn->dst_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05005164 dst_reg, insn->imm, opcode, is_jmp32);
Josef Bacik48461132016-09-28 10:54:32 -04005165 }
5166
Jiong Wang092ed092019-01-26 12:26:01 -05005167 /* detect if R == 0 where R is returned from bpf_map_lookup_elem().
5168 * NOTE: these optimizations below are related with pointer comparison
5169 * which will never be JMP32.
5170 */
5171 if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07005172 insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
Joe Stringer840b9612018-10-02 13:35:32 -07005173 reg_type_may_be_null(dst_reg->type)) {
5174 /* Mark all identical registers in each branch as either
Thomas Graf57a09bf2016-10-18 19:51:19 +02005175 * safe or unknown depending R == 0 or R != 0 conditional.
5176 */
Joe Stringer840b9612018-10-02 13:35:32 -07005177 mark_ptr_or_null_regs(this_branch, insn->dst_reg,
5178 opcode == BPF_JNE);
5179 mark_ptr_or_null_regs(other_branch, insn->dst_reg,
5180 opcode == BPF_JEQ);
Daniel Borkmann5beca082017-11-01 23:58:10 +01005181 } else if (!try_match_pkt_pointers(insn, dst_reg, &regs[insn->src_reg],
5182 this_branch, other_branch) &&
5183 is_pointer_value(env, insn->dst_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005184 verbose(env, "R%d pointer comparison prohibited\n",
5185 insn->dst_reg);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07005186 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005187 }
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07005188 if (env->log.level & BPF_LOG_LEVEL)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005189 print_verifier_state(env, this_branch->frame[this_branch->curframe]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005190 return 0;
5191}
5192
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005193/* verify BPF_LD_IMM64 instruction */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005194static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005195{
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02005196 struct bpf_insn_aux_data *aux = cur_aux(env);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005197 struct bpf_reg_state *regs = cur_regs(env);
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02005198 struct bpf_map *map;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005199 int err;
5200
5201 if (BPF_SIZE(insn->code) != BPF_DW) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005202 verbose(env, "invalid BPF_LD_IMM insn\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005203 return -EINVAL;
5204 }
5205 if (insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005206 verbose(env, "BPF_LD_IMM64 uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005207 return -EINVAL;
5208 }
5209
Edward Creedc503a82017-08-15 20:34:35 +01005210 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005211 if (err)
5212 return err;
5213
Jakub Kicinski6b173872016-09-21 11:43:59 +01005214 if (insn->src_reg == 0) {
Jakub Kicinski6b173872016-09-21 11:43:59 +01005215 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
5216
Edward Creef1174f72017-08-07 15:26:19 +01005217 regs[insn->dst_reg].type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01005218 __mark_reg_known(&regs[insn->dst_reg], imm);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005219 return 0;
Jakub Kicinski6b173872016-09-21 11:43:59 +01005220 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005221
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02005222 map = env->used_maps[aux->map_index];
5223 mark_reg_known_zero(env, regs, insn->dst_reg);
5224 regs[insn->dst_reg].map_ptr = map;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005225
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02005226 if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) {
5227 regs[insn->dst_reg].type = PTR_TO_MAP_VALUE;
5228 regs[insn->dst_reg].off = aux->map_off;
5229 if (map_value_has_spin_lock(map))
5230 regs[insn->dst_reg].id = ++env->id_gen;
5231 } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) {
5232 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
5233 } else {
5234 verbose(env, "bpf verifier is misconfigured\n");
5235 return -EINVAL;
5236 }
5237
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005238 return 0;
5239}
5240
Daniel Borkmann96be4322015-03-01 12:31:46 +01005241static bool may_access_skb(enum bpf_prog_type type)
5242{
5243 switch (type) {
5244 case BPF_PROG_TYPE_SOCKET_FILTER:
5245 case BPF_PROG_TYPE_SCHED_CLS:
Daniel Borkmann94caee8c2015-03-20 15:11:11 +01005246 case BPF_PROG_TYPE_SCHED_ACT:
Daniel Borkmann96be4322015-03-01 12:31:46 +01005247 return true;
5248 default:
5249 return false;
5250 }
5251}
5252
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005253/* verify safety of LD_ABS|LD_IND instructions:
5254 * - they can only appear in the programs where ctx == skb
5255 * - since they are wrappers of function calls, they scratch R1-R5 registers,
5256 * preserve R6-R9, and store return value into R0
5257 *
5258 * Implicit input:
5259 * ctx == skb == R6 == CTX
5260 *
5261 * Explicit input:
5262 * SRC == any register
5263 * IMM == 32-bit immediate
5264 *
5265 * Output:
5266 * R0 - 8/16/32-bit skb data converted to cpu endianness
5267 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005268static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005269{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005270 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005271 u8 mode = BPF_MODE(insn->code);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005272 int i, err;
5273
Daniel Borkmann24701ec2015-03-01 12:31:47 +01005274 if (!may_access_skb(env->prog->type)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005275 verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005276 return -EINVAL;
5277 }
5278
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +02005279 if (!env->ops->gen_ld_abs) {
5280 verbose(env, "bpf verifier is misconfigured\n");
5281 return -EINVAL;
5282 }
5283
Jiong Wangf910cef2018-05-02 16:17:17 -04005284 if (env->subprog_cnt > 1) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005285 /* when program has LD_ABS insn JITs and interpreter assume
5286 * that r1 == ctx == skb which is not the case for callees
5287 * that can have arbitrary arguments. It's problematic
5288 * for main prog as well since JITs would need to analyze
5289 * all functions in order to make proper register save/restore
5290 * decisions in the main prog. Hence disallow LD_ABS with calls
5291 */
5292 verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n");
5293 return -EINVAL;
5294 }
5295
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005296 if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
Alexei Starovoitovd82bccc2016-04-12 10:26:19 -07005297 BPF_SIZE(insn->code) == BPF_DW ||
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005298 (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005299 verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005300 return -EINVAL;
5301 }
5302
5303 /* check whether implicit source operand (register R6) is readable */
Edward Creedc503a82017-08-15 20:34:35 +01005304 err = check_reg_arg(env, BPF_REG_6, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005305 if (err)
5306 return err;
5307
Joe Stringerfd978bf72018-10-02 13:35:35 -07005308 /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as
5309 * gen_ld_abs() may terminate the program at runtime, leading to
5310 * reference leak.
5311 */
5312 err = check_reference_leak(env);
5313 if (err) {
5314 verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n");
5315 return err;
5316 }
5317
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08005318 if (env->cur_state->active_spin_lock) {
5319 verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n");
5320 return -EINVAL;
5321 }
5322
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005323 if (regs[BPF_REG_6].type != PTR_TO_CTX) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005324 verbose(env,
5325 "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005326 return -EINVAL;
5327 }
5328
5329 if (mode == BPF_IND) {
5330 /* check explicit source operand */
Edward Creedc503a82017-08-15 20:34:35 +01005331 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005332 if (err)
5333 return err;
5334 }
5335
5336 /* reset caller saved regs to unreadable */
Edward Creedc503a82017-08-15 20:34:35 +01005337 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005338 mark_reg_not_init(env, regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01005339 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
5340 }
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005341
5342 /* mark destination R0 register as readable, since it contains
Edward Creedc503a82017-08-15 20:34:35 +01005343 * the value fetched from the packet.
5344 * Already marked as written above.
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005345 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005346 mark_reg_unknown(env, regs, BPF_REG_0);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005347 return 0;
5348}
5349
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005350static int check_return_code(struct bpf_verifier_env *env)
5351{
5352 struct bpf_reg_state *reg;
5353 struct tnum range = tnum_range(0, 1);
5354
5355 switch (env->prog->type) {
5356 case BPF_PROG_TYPE_CGROUP_SKB:
5357 case BPF_PROG_TYPE_CGROUP_SOCK:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07005358 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005359 case BPF_PROG_TYPE_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05005360 case BPF_PROG_TYPE_CGROUP_DEVICE:
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08005361 case BPF_PROG_TYPE_CGROUP_SYSCTL:
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005362 break;
5363 default:
5364 return 0;
5365 }
5366
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005367 reg = cur_regs(env) + BPF_REG_0;
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005368 if (reg->type != SCALAR_VALUE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005369 verbose(env, "At program exit the register R0 is not a known value (%s)\n",
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005370 reg_type_str[reg->type]);
5371 return -EINVAL;
5372 }
5373
5374 if (!tnum_in(range, reg->var_off)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005375 verbose(env, "At program exit the register R0 ");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005376 if (!tnum_is_unknown(reg->var_off)) {
5377 char tn_buf[48];
5378
5379 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005380 verbose(env, "has value %s", tn_buf);
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005381 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005382 verbose(env, "has unknown scalar value");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005383 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005384 verbose(env, " should have been 0 or 1\n");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005385 return -EINVAL;
5386 }
5387 return 0;
5388}
5389
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005390/* non-recursive DFS pseudo code
5391 * 1 procedure DFS-iterative(G,v):
5392 * 2 label v as discovered
5393 * 3 let S be a stack
5394 * 4 S.push(v)
5395 * 5 while S is not empty
5396 * 6 t <- S.pop()
5397 * 7 if t is what we're looking for:
5398 * 8 return t
5399 * 9 for all edges e in G.adjacentEdges(t) do
5400 * 10 if edge e is already labelled
5401 * 11 continue with the next edge
5402 * 12 w <- G.adjacentVertex(t,e)
5403 * 13 if vertex w is not discovered and not explored
5404 * 14 label e as tree-edge
5405 * 15 label w as discovered
5406 * 16 S.push(w)
5407 * 17 continue at 5
5408 * 18 else if vertex w is discovered
5409 * 19 label e as back-edge
5410 * 20 else
5411 * 21 // vertex w is explored
5412 * 22 label e as forward- or cross-edge
5413 * 23 label t as explored
5414 * 24 S.pop()
5415 *
5416 * convention:
5417 * 0x10 - discovered
5418 * 0x11 - discovered and fall-through edge labelled
5419 * 0x12 - discovered and fall-through and branch edges labelled
5420 * 0x20 - explored
5421 */
5422
5423enum {
5424 DISCOVERED = 0x10,
5425 EXPLORED = 0x20,
5426 FALLTHROUGH = 1,
5427 BRANCH = 2,
5428};
5429
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005430#define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005431
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005432/* t, w, e - match pseudo-code above:
5433 * t - index of current instruction
5434 * w - next instruction
5435 * e - edge
5436 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005437static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005438{
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005439 int *insn_stack = env->cfg.insn_stack;
5440 int *insn_state = env->cfg.insn_state;
5441
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005442 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
5443 return 0;
5444
5445 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
5446 return 0;
5447
5448 if (w < 0 || w >= env->prog->len) {
Martin KaFai Laud9762e82018-12-13 10:41:48 -08005449 verbose_linfo(env, t, "%d: ", t);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005450 verbose(env, "jump out of range from insn %d to %d\n", t, w);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005451 return -EINVAL;
5452 }
5453
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005454 if (e == BRANCH)
5455 /* mark branch target for state pruning */
5456 env->explored_states[w] = STATE_LIST_MARK;
5457
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005458 if (insn_state[w] == 0) {
5459 /* tree-edge */
5460 insn_state[t] = DISCOVERED | e;
5461 insn_state[w] = DISCOVERED;
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005462 if (env->cfg.cur_stack >= env->prog->len)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005463 return -E2BIG;
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005464 insn_stack[env->cfg.cur_stack++] = w;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005465 return 1;
5466 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
Martin KaFai Laud9762e82018-12-13 10:41:48 -08005467 verbose_linfo(env, t, "%d: ", t);
5468 verbose_linfo(env, w, "%d: ", w);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005469 verbose(env, "back-edge from insn %d to %d\n", t, w);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005470 return -EINVAL;
5471 } else if (insn_state[w] == EXPLORED) {
5472 /* forward- or cross-edge */
5473 insn_state[t] = DISCOVERED | e;
5474 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005475 verbose(env, "insn state internal bug\n");
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005476 return -EFAULT;
5477 }
5478 return 0;
5479}
5480
5481/* non-recursive depth-first-search to detect loops in BPF program
5482 * loop == back-edge in directed graph
5483 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005484static int check_cfg(struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005485{
5486 struct bpf_insn *insns = env->prog->insnsi;
5487 int insn_cnt = env->prog->len;
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005488 int *insn_stack, *insn_state;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005489 int ret = 0;
5490 int i, t;
5491
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005492 insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005493 if (!insn_state)
5494 return -ENOMEM;
5495
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005496 insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005497 if (!insn_stack) {
Alexei Starovoitov71dde682019-04-01 21:27:43 -07005498 kvfree(insn_state);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005499 return -ENOMEM;
5500 }
5501
5502 insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
5503 insn_stack[0] = 0; /* 0 is the first instruction */
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005504 env->cfg.cur_stack = 1;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005505
5506peek_stack:
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005507 if (env->cfg.cur_stack == 0)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005508 goto check_state;
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005509 t = insn_stack[env->cfg.cur_stack - 1];
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005510
Jiong Wang092ed092019-01-26 12:26:01 -05005511 if (BPF_CLASS(insns[t].code) == BPF_JMP ||
5512 BPF_CLASS(insns[t].code) == BPF_JMP32) {
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005513 u8 opcode = BPF_OP(insns[t].code);
5514
5515 if (opcode == BPF_EXIT) {
5516 goto mark_explored;
5517 } else if (opcode == BPF_CALL) {
5518 ret = push_insn(t, t + 1, FALLTHROUGH, env);
5519 if (ret == 1)
5520 goto peek_stack;
5521 else if (ret < 0)
5522 goto err_free;
Daniel Borkmann07016152016-04-05 22:33:17 +02005523 if (t + 1 < insn_cnt)
5524 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08005525 if (insns[t].src_reg == BPF_PSEUDO_CALL) {
5526 env->explored_states[t] = STATE_LIST_MARK;
5527 ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env);
5528 if (ret == 1)
5529 goto peek_stack;
5530 else if (ret < 0)
5531 goto err_free;
5532 }
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005533 } else if (opcode == BPF_JA) {
5534 if (BPF_SRC(insns[t].code) != BPF_K) {
5535 ret = -EINVAL;
5536 goto err_free;
5537 }
5538 /* unconditional jump with single edge */
5539 ret = push_insn(t, t + insns[t].off + 1,
5540 FALLTHROUGH, env);
5541 if (ret == 1)
5542 goto peek_stack;
5543 else if (ret < 0)
5544 goto err_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005545 /* tell verifier to check for equivalent states
5546 * after every call and jump
5547 */
Alexei Starovoitovc3de6312015-04-14 15:57:13 -07005548 if (t + 1 < insn_cnt)
5549 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005550 } else {
5551 /* conditional jump with two edges */
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02005552 env->explored_states[t] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005553 ret = push_insn(t, t + 1, FALLTHROUGH, env);
5554 if (ret == 1)
5555 goto peek_stack;
5556 else if (ret < 0)
5557 goto err_free;
5558
5559 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
5560 if (ret == 1)
5561 goto peek_stack;
5562 else if (ret < 0)
5563 goto err_free;
5564 }
5565 } else {
5566 /* all other non-branch instructions with single
5567 * fall-through edge
5568 */
5569 ret = push_insn(t, t + 1, FALLTHROUGH, env);
5570 if (ret == 1)
5571 goto peek_stack;
5572 else if (ret < 0)
5573 goto err_free;
5574 }
5575
5576mark_explored:
5577 insn_state[t] = EXPLORED;
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005578 if (env->cfg.cur_stack-- <= 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005579 verbose(env, "pop stack internal bug\n");
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005580 ret = -EFAULT;
5581 goto err_free;
5582 }
5583 goto peek_stack;
5584
5585check_state:
5586 for (i = 0; i < insn_cnt; i++) {
5587 if (insn_state[i] != EXPLORED) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005588 verbose(env, "unreachable insn %d\n", i);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005589 ret = -EINVAL;
5590 goto err_free;
5591 }
5592 }
5593 ret = 0; /* cfg looks good */
5594
5595err_free:
Alexei Starovoitov71dde682019-04-01 21:27:43 -07005596 kvfree(insn_state);
5597 kvfree(insn_stack);
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005598 env->cfg.insn_state = env->cfg.insn_stack = NULL;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005599 return ret;
5600}
5601
Yonghong Song838e9692018-11-19 15:29:11 -08005602/* The minimum supported BTF func info size */
5603#define MIN_BPF_FUNCINFO_SIZE 8
5604#define MAX_FUNCINFO_REC_SIZE 252
5605
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005606static int check_btf_func(struct bpf_verifier_env *env,
5607 const union bpf_attr *attr,
5608 union bpf_attr __user *uattr)
Yonghong Song838e9692018-11-19 15:29:11 -08005609{
Peter Oskolkovd0b28182019-01-16 10:43:01 -08005610 u32 i, nfuncs, urec_size, min_size;
Yonghong Song838e9692018-11-19 15:29:11 -08005611 u32 krec_size = sizeof(struct bpf_func_info);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005612 struct bpf_func_info *krecord;
Yonghong Song838e9692018-11-19 15:29:11 -08005613 const struct btf_type *type;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005614 struct bpf_prog *prog;
5615 const struct btf *btf;
Yonghong Song838e9692018-11-19 15:29:11 -08005616 void __user *urecord;
Peter Oskolkovd0b28182019-01-16 10:43:01 -08005617 u32 prev_offset = 0;
Yonghong Song838e9692018-11-19 15:29:11 -08005618 int ret = 0;
5619
5620 nfuncs = attr->func_info_cnt;
5621 if (!nfuncs)
5622 return 0;
5623
5624 if (nfuncs != env->subprog_cnt) {
5625 verbose(env, "number of funcs in func_info doesn't match number of subprogs\n");
5626 return -EINVAL;
5627 }
5628
5629 urec_size = attr->func_info_rec_size;
5630 if (urec_size < MIN_BPF_FUNCINFO_SIZE ||
5631 urec_size > MAX_FUNCINFO_REC_SIZE ||
5632 urec_size % sizeof(u32)) {
5633 verbose(env, "invalid func info rec size %u\n", urec_size);
5634 return -EINVAL;
5635 }
5636
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005637 prog = env->prog;
5638 btf = prog->aux->btf;
Yonghong Song838e9692018-11-19 15:29:11 -08005639
5640 urecord = u64_to_user_ptr(attr->func_info);
5641 min_size = min_t(u32, krec_size, urec_size);
5642
Yonghong Songba64e7d2018-11-24 23:20:44 -08005643 krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005644 if (!krecord)
5645 return -ENOMEM;
Yonghong Songba64e7d2018-11-24 23:20:44 -08005646
Yonghong Song838e9692018-11-19 15:29:11 -08005647 for (i = 0; i < nfuncs; i++) {
5648 ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size);
5649 if (ret) {
5650 if (ret == -E2BIG) {
5651 verbose(env, "nonzero tailing record in func info");
5652 /* set the size kernel expects so loader can zero
5653 * out the rest of the record.
5654 */
5655 if (put_user(min_size, &uattr->func_info_rec_size))
5656 ret = -EFAULT;
5657 }
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005658 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005659 }
5660
Yonghong Songba64e7d2018-11-24 23:20:44 -08005661 if (copy_from_user(&krecord[i], urecord, min_size)) {
Yonghong Song838e9692018-11-19 15:29:11 -08005662 ret = -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005663 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005664 }
5665
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005666 /* check insn_off */
Yonghong Song838e9692018-11-19 15:29:11 -08005667 if (i == 0) {
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005668 if (krecord[i].insn_off) {
Yonghong Song838e9692018-11-19 15:29:11 -08005669 verbose(env,
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005670 "nonzero insn_off %u for the first func info record",
5671 krecord[i].insn_off);
Yonghong Song838e9692018-11-19 15:29:11 -08005672 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005673 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005674 }
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005675 } else if (krecord[i].insn_off <= prev_offset) {
Yonghong Song838e9692018-11-19 15:29:11 -08005676 verbose(env,
5677 "same or smaller insn offset (%u) than previous func info record (%u)",
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005678 krecord[i].insn_off, prev_offset);
Yonghong Song838e9692018-11-19 15:29:11 -08005679 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005680 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005681 }
5682
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005683 if (env->subprog_info[i].start != krecord[i].insn_off) {
Yonghong Song838e9692018-11-19 15:29:11 -08005684 verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n");
5685 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005686 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005687 }
5688
5689 /* check type_id */
Yonghong Songba64e7d2018-11-24 23:20:44 -08005690 type = btf_type_by_id(btf, krecord[i].type_id);
Yonghong Song838e9692018-11-19 15:29:11 -08005691 if (!type || BTF_INFO_KIND(type->info) != BTF_KIND_FUNC) {
5692 verbose(env, "invalid type id %d in func info",
Yonghong Songba64e7d2018-11-24 23:20:44 -08005693 krecord[i].type_id);
Yonghong Song838e9692018-11-19 15:29:11 -08005694 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005695 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005696 }
5697
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005698 prev_offset = krecord[i].insn_off;
Yonghong Song838e9692018-11-19 15:29:11 -08005699 urecord += urec_size;
5700 }
5701
Yonghong Songba64e7d2018-11-24 23:20:44 -08005702 prog->aux->func_info = krecord;
5703 prog->aux->func_info_cnt = nfuncs;
Yonghong Song838e9692018-11-19 15:29:11 -08005704 return 0;
5705
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005706err_free:
Yonghong Songba64e7d2018-11-24 23:20:44 -08005707 kvfree(krecord);
Yonghong Song838e9692018-11-19 15:29:11 -08005708 return ret;
5709}
5710
Yonghong Songba64e7d2018-11-24 23:20:44 -08005711static void adjust_btf_func(struct bpf_verifier_env *env)
5712{
5713 int i;
5714
5715 if (!env->prog->aux->func_info)
5716 return;
5717
5718 for (i = 0; i < env->subprog_cnt; i++)
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005719 env->prog->aux->func_info[i].insn_off = env->subprog_info[i].start;
Yonghong Songba64e7d2018-11-24 23:20:44 -08005720}
5721
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005722#define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \
5723 sizeof(((struct bpf_line_info *)(0))->line_col))
5724#define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE
5725
5726static int check_btf_line(struct bpf_verifier_env *env,
5727 const union bpf_attr *attr,
5728 union bpf_attr __user *uattr)
5729{
5730 u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0;
5731 struct bpf_subprog_info *sub;
5732 struct bpf_line_info *linfo;
5733 struct bpf_prog *prog;
5734 const struct btf *btf;
5735 void __user *ulinfo;
5736 int err;
5737
5738 nr_linfo = attr->line_info_cnt;
5739 if (!nr_linfo)
5740 return 0;
5741
5742 rec_size = attr->line_info_rec_size;
5743 if (rec_size < MIN_BPF_LINEINFO_SIZE ||
5744 rec_size > MAX_LINEINFO_REC_SIZE ||
5745 rec_size & (sizeof(u32) - 1))
5746 return -EINVAL;
5747
5748 /* Need to zero it in case the userspace may
5749 * pass in a smaller bpf_line_info object.
5750 */
5751 linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info),
5752 GFP_KERNEL | __GFP_NOWARN);
5753 if (!linfo)
5754 return -ENOMEM;
5755
5756 prog = env->prog;
5757 btf = prog->aux->btf;
5758
5759 s = 0;
5760 sub = env->subprog_info;
5761 ulinfo = u64_to_user_ptr(attr->line_info);
5762 expected_size = sizeof(struct bpf_line_info);
5763 ncopy = min_t(u32, expected_size, rec_size);
5764 for (i = 0; i < nr_linfo; i++) {
5765 err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size);
5766 if (err) {
5767 if (err == -E2BIG) {
5768 verbose(env, "nonzero tailing record in line_info");
5769 if (put_user(expected_size,
5770 &uattr->line_info_rec_size))
5771 err = -EFAULT;
5772 }
5773 goto err_free;
5774 }
5775
5776 if (copy_from_user(&linfo[i], ulinfo, ncopy)) {
5777 err = -EFAULT;
5778 goto err_free;
5779 }
5780
5781 /*
5782 * Check insn_off to ensure
5783 * 1) strictly increasing AND
5784 * 2) bounded by prog->len
5785 *
5786 * The linfo[0].insn_off == 0 check logically falls into
5787 * the later "missing bpf_line_info for func..." case
5788 * because the first linfo[0].insn_off must be the
5789 * first sub also and the first sub must have
5790 * subprog_info[0].start == 0.
5791 */
5792 if ((i && linfo[i].insn_off <= prev_offset) ||
5793 linfo[i].insn_off >= prog->len) {
5794 verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n",
5795 i, linfo[i].insn_off, prev_offset,
5796 prog->len);
5797 err = -EINVAL;
5798 goto err_free;
5799 }
5800
Martin KaFai Laufdbaa0b2018-12-19 13:01:01 -08005801 if (!prog->insnsi[linfo[i].insn_off].code) {
5802 verbose(env,
5803 "Invalid insn code at line_info[%u].insn_off\n",
5804 i);
5805 err = -EINVAL;
5806 goto err_free;
5807 }
5808
Martin KaFai Lau23127b32018-12-13 10:41:46 -08005809 if (!btf_name_by_offset(btf, linfo[i].line_off) ||
5810 !btf_name_by_offset(btf, linfo[i].file_name_off)) {
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005811 verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i);
5812 err = -EINVAL;
5813 goto err_free;
5814 }
5815
5816 if (s != env->subprog_cnt) {
5817 if (linfo[i].insn_off == sub[s].start) {
5818 sub[s].linfo_idx = i;
5819 s++;
5820 } else if (sub[s].start < linfo[i].insn_off) {
5821 verbose(env, "missing bpf_line_info for func#%u\n", s);
5822 err = -EINVAL;
5823 goto err_free;
5824 }
5825 }
5826
5827 prev_offset = linfo[i].insn_off;
5828 ulinfo += rec_size;
5829 }
5830
5831 if (s != env->subprog_cnt) {
5832 verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n",
5833 env->subprog_cnt - s, s);
5834 err = -EINVAL;
5835 goto err_free;
5836 }
5837
5838 prog->aux->linfo = linfo;
5839 prog->aux->nr_linfo = nr_linfo;
5840
5841 return 0;
5842
5843err_free:
5844 kvfree(linfo);
5845 return err;
5846}
5847
5848static int check_btf_info(struct bpf_verifier_env *env,
5849 const union bpf_attr *attr,
5850 union bpf_attr __user *uattr)
5851{
5852 struct btf *btf;
5853 int err;
5854
5855 if (!attr->func_info_cnt && !attr->line_info_cnt)
5856 return 0;
5857
5858 btf = btf_get_by_fd(attr->prog_btf_fd);
5859 if (IS_ERR(btf))
5860 return PTR_ERR(btf);
5861 env->prog->aux->btf = btf;
5862
5863 err = check_btf_func(env, attr, uattr);
5864 if (err)
5865 return err;
5866
5867 err = check_btf_line(env, attr, uattr);
5868 if (err)
5869 return err;
5870
5871 return 0;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005872}
5873
Edward Creef1174f72017-08-07 15:26:19 +01005874/* check %cur's range satisfies %old's */
5875static bool range_within(struct bpf_reg_state *old,
5876 struct bpf_reg_state *cur)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07005877{
Edward Creeb03c9f92017-08-07 15:26:36 +01005878 return old->umin_value <= cur->umin_value &&
5879 old->umax_value >= cur->umax_value &&
5880 old->smin_value <= cur->smin_value &&
5881 old->smax_value >= cur->smax_value;
Edward Creef1174f72017-08-07 15:26:19 +01005882}
5883
5884/* Maximum number of register states that can exist at once */
5885#define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE)
5886struct idpair {
5887 u32 old;
5888 u32 cur;
5889};
5890
5891/* If in the old state two registers had the same id, then they need to have
5892 * the same id in the new state as well. But that id could be different from
5893 * the old state, so we need to track the mapping from old to new ids.
5894 * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent
5895 * regs with old id 5 must also have new id 9 for the new state to be safe. But
5896 * regs with a different old id could still have new id 9, we don't care about
5897 * that.
5898 * So we look through our idmap to see if this old id has been seen before. If
5899 * so, we require the new id to match; otherwise, we add the id pair to the map.
5900 */
5901static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap)
5902{
5903 unsigned int i;
5904
5905 for (i = 0; i < ID_MAP_SIZE; i++) {
5906 if (!idmap[i].old) {
5907 /* Reached an empty slot; haven't seen this id before */
5908 idmap[i].old = old_id;
5909 idmap[i].cur = cur_id;
5910 return true;
5911 }
5912 if (idmap[i].old == old_id)
5913 return idmap[i].cur == cur_id;
5914 }
5915 /* We ran out of idmap slots, which should be impossible */
5916 WARN_ON_ONCE(1);
5917 return false;
5918}
5919
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -08005920static void clean_func_state(struct bpf_verifier_env *env,
5921 struct bpf_func_state *st)
5922{
5923 enum bpf_reg_liveness live;
5924 int i, j;
5925
5926 for (i = 0; i < BPF_REG_FP; i++) {
5927 live = st->regs[i].live;
5928 /* liveness must not touch this register anymore */
5929 st->regs[i].live |= REG_LIVE_DONE;
5930 if (!(live & REG_LIVE_READ))
5931 /* since the register is unused, clear its state
5932 * to make further comparison simpler
5933 */
5934 __mark_reg_not_init(&st->regs[i]);
5935 }
5936
5937 for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) {
5938 live = st->stack[i].spilled_ptr.live;
5939 /* liveness must not touch this stack slot anymore */
5940 st->stack[i].spilled_ptr.live |= REG_LIVE_DONE;
5941 if (!(live & REG_LIVE_READ)) {
5942 __mark_reg_not_init(&st->stack[i].spilled_ptr);
5943 for (j = 0; j < BPF_REG_SIZE; j++)
5944 st->stack[i].slot_type[j] = STACK_INVALID;
5945 }
5946 }
5947}
5948
5949static void clean_verifier_state(struct bpf_verifier_env *env,
5950 struct bpf_verifier_state *st)
5951{
5952 int i;
5953
5954 if (st->frame[0]->regs[0].live & REG_LIVE_DONE)
5955 /* all regs in this state in all frames were already marked */
5956 return;
5957
5958 for (i = 0; i <= st->curframe; i++)
5959 clean_func_state(env, st->frame[i]);
5960}
5961
5962/* the parentage chains form a tree.
5963 * the verifier states are added to state lists at given insn and
5964 * pushed into state stack for future exploration.
5965 * when the verifier reaches bpf_exit insn some of the verifer states
5966 * stored in the state lists have their final liveness state already,
5967 * but a lot of states will get revised from liveness point of view when
5968 * the verifier explores other branches.
5969 * Example:
5970 * 1: r0 = 1
5971 * 2: if r1 == 100 goto pc+1
5972 * 3: r0 = 2
5973 * 4: exit
5974 * when the verifier reaches exit insn the register r0 in the state list of
5975 * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch
5976 * of insn 2 and goes exploring further. At the insn 4 it will walk the
5977 * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ.
5978 *
5979 * Since the verifier pushes the branch states as it sees them while exploring
5980 * the program the condition of walking the branch instruction for the second
5981 * time means that all states below this branch were already explored and
5982 * their final liveness markes are already propagated.
5983 * Hence when the verifier completes the search of state list in is_state_visited()
5984 * we can call this clean_live_states() function to mark all liveness states
5985 * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state'
5986 * will not be used.
5987 * This function also clears the registers and stack for states that !READ
5988 * to simplify state merging.
5989 *
5990 * Important note here that walking the same branch instruction in the callee
5991 * doesn't meant that the states are DONE. The verifier has to compare
5992 * the callsites
5993 */
5994static void clean_live_states(struct bpf_verifier_env *env, int insn,
5995 struct bpf_verifier_state *cur)
5996{
5997 struct bpf_verifier_state_list *sl;
5998 int i;
5999
6000 sl = env->explored_states[insn];
6001 if (!sl)
6002 return;
6003
6004 while (sl != STATE_LIST_MARK) {
6005 if (sl->state.curframe != cur->curframe)
6006 goto next;
6007 for (i = 0; i <= cur->curframe; i++)
6008 if (sl->state.frame[i]->callsite != cur->frame[i]->callsite)
6009 goto next;
6010 clean_verifier_state(env, &sl->state);
6011next:
6012 sl = sl->next;
6013 }
6014}
6015
Edward Creef1174f72017-08-07 15:26:19 +01006016/* Returns true if (rold safe implies rcur safe) */
Edward Cree1b688a12017-08-23 15:10:50 +01006017static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
6018 struct idpair *idmap)
Edward Creef1174f72017-08-07 15:26:19 +01006019{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006020 bool equal;
6021
Edward Creedc503a82017-08-15 20:34:35 +01006022 if (!(rold->live & REG_LIVE_READ))
6023 /* explored state didn't use this */
6024 return true;
6025
Edward Cree679c7822018-08-22 20:02:19 +01006026 equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006027
6028 if (rold->type == PTR_TO_STACK)
6029 /* two stack pointers are equal only if they're pointing to
6030 * the same stack frame, since fp-8 in foo != fp-8 in bar
6031 */
6032 return equal && rold->frameno == rcur->frameno;
6033
6034 if (equal)
Edward Creef1174f72017-08-07 15:26:19 +01006035 return true;
6036
6037 if (rold->type == NOT_INIT)
6038 /* explored state can't have used this */
6039 return true;
6040 if (rcur->type == NOT_INIT)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07006041 return false;
Edward Creef1174f72017-08-07 15:26:19 +01006042 switch (rold->type) {
6043 case SCALAR_VALUE:
6044 if (rcur->type == SCALAR_VALUE) {
6045 /* new val must satisfy old val knowledge */
6046 return range_within(rold, rcur) &&
6047 tnum_in(rold->var_off, rcur->var_off);
6048 } else {
Jann Horn179d1c52017-12-18 20:11:59 -08006049 /* We're trying to use a pointer in place of a scalar.
6050 * Even if the scalar was unbounded, this could lead to
6051 * pointer leaks because scalars are allowed to leak
6052 * while pointers are not. We could make this safe in
6053 * special cases if root is calling us, but it's
6054 * probably not worth the hassle.
Edward Creef1174f72017-08-07 15:26:19 +01006055 */
Jann Horn179d1c52017-12-18 20:11:59 -08006056 return false;
Edward Creef1174f72017-08-07 15:26:19 +01006057 }
6058 case PTR_TO_MAP_VALUE:
Edward Cree1b688a12017-08-23 15:10:50 +01006059 /* If the new min/max/var_off satisfy the old ones and
6060 * everything else matches, we are OK.
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08006061 * 'id' is not compared, since it's only used for maps with
6062 * bpf_spin_lock inside map element and in such cases if
6063 * the rest of the prog is valid for one map element then
6064 * it's valid for all map elements regardless of the key
6065 * used in bpf_map_lookup()
Edward Cree1b688a12017-08-23 15:10:50 +01006066 */
6067 return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 &&
6068 range_within(rold, rcur) &&
6069 tnum_in(rold->var_off, rcur->var_off);
Edward Creef1174f72017-08-07 15:26:19 +01006070 case PTR_TO_MAP_VALUE_OR_NULL:
6071 /* a PTR_TO_MAP_VALUE could be safe to use as a
6072 * PTR_TO_MAP_VALUE_OR_NULL into the same map.
6073 * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL-
6074 * checked, doing so could have affected others with the same
6075 * id, and we can't check for that because we lost the id when
6076 * we converted to a PTR_TO_MAP_VALUE.
6077 */
6078 if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL)
6079 return false;
6080 if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)))
6081 return false;
6082 /* Check our ids match any regs they're supposed to */
6083 return check_ids(rold->id, rcur->id, idmap);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02006084 case PTR_TO_PACKET_META:
Edward Creef1174f72017-08-07 15:26:19 +01006085 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02006086 if (rcur->type != rold->type)
Edward Creef1174f72017-08-07 15:26:19 +01006087 return false;
6088 /* We must have at least as much range as the old ptr
6089 * did, so that any accesses which were safe before are
6090 * still safe. This is true even if old range < old off,
6091 * since someone could have accessed through (ptr - k), or
6092 * even done ptr -= k in a register, to get a safe access.
6093 */
6094 if (rold->range > rcur->range)
6095 return false;
6096 /* If the offsets don't match, we can't trust our alignment;
6097 * nor can we be sure that we won't fall out of range.
6098 */
6099 if (rold->off != rcur->off)
6100 return false;
6101 /* id relations must be preserved */
6102 if (rold->id && !check_ids(rold->id, rcur->id, idmap))
6103 return false;
6104 /* new val must satisfy old val knowledge */
6105 return range_within(rold, rcur) &&
6106 tnum_in(rold->var_off, rcur->var_off);
6107 case PTR_TO_CTX:
6108 case CONST_PTR_TO_MAP:
Edward Creef1174f72017-08-07 15:26:19 +01006109 case PTR_TO_PACKET_END:
Petar Penkovd58e4682018-09-14 07:46:18 -07006110 case PTR_TO_FLOW_KEYS:
Joe Stringerc64b7982018-10-02 13:35:33 -07006111 case PTR_TO_SOCKET:
6112 case PTR_TO_SOCKET_OR_NULL:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08006113 case PTR_TO_SOCK_COMMON:
6114 case PTR_TO_SOCK_COMMON_OR_NULL:
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08006115 case PTR_TO_TCP_SOCK:
6116 case PTR_TO_TCP_SOCK_OR_NULL:
Edward Creef1174f72017-08-07 15:26:19 +01006117 /* Only valid matches are exact, which memcmp() above
6118 * would have accepted
6119 */
6120 default:
6121 /* Don't know what's going on, just say it's not safe */
6122 return false;
6123 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07006124
Edward Creef1174f72017-08-07 15:26:19 +01006125 /* Shouldn't get here; if we do, say it's not safe */
6126 WARN_ON_ONCE(1);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07006127 return false;
6128}
6129
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006130static bool stacksafe(struct bpf_func_state *old,
6131 struct bpf_func_state *cur,
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006132 struct idpair *idmap)
6133{
6134 int i, spi;
6135
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006136 /* walk slots of the explored stack and ignore any additional
6137 * slots in the current stack, since explored(safe) state
6138 * didn't use them
6139 */
6140 for (i = 0; i < old->allocated_stack; i++) {
6141 spi = i / BPF_REG_SIZE;
6142
Alexei Starovoitovb2339202018-12-13 11:42:31 -08006143 if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) {
6144 i += BPF_REG_SIZE - 1;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08006145 /* explored state didn't use this */
Gianluca Borellofd05e572017-12-23 10:09:55 +00006146 continue;
Alexei Starovoitovb2339202018-12-13 11:42:31 -08006147 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08006148
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006149 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID)
6150 continue;
Alexei Starovoitov19e2dbb2018-12-13 11:42:33 -08006151
6152 /* explored stack has more populated slots than current stack
6153 * and these slots were used
6154 */
6155 if (i >= cur->allocated_stack)
6156 return false;
6157
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08006158 /* if old state was safe with misc data in the stack
6159 * it will be safe with zero-initialized stack.
6160 * The opposite is not true
6161 */
6162 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC &&
6163 cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO)
6164 continue;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006165 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] !=
6166 cur->stack[spi].slot_type[i % BPF_REG_SIZE])
6167 /* Ex: old explored (safe) state has STACK_SPILL in
6168 * this stack slot, but current has has STACK_MISC ->
6169 * this verifier states are not equivalent,
6170 * return false to continue verification of this path
6171 */
6172 return false;
6173 if (i % BPF_REG_SIZE)
6174 continue;
6175 if (old->stack[spi].slot_type[0] != STACK_SPILL)
6176 continue;
6177 if (!regsafe(&old->stack[spi].spilled_ptr,
6178 &cur->stack[spi].spilled_ptr,
6179 idmap))
6180 /* when explored and current stack slot are both storing
6181 * spilled registers, check that stored pointers types
6182 * are the same as well.
6183 * Ex: explored safe path could have stored
6184 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8}
6185 * but current path has stored:
6186 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16}
6187 * such verifier states are not equivalent.
6188 * return false to continue verification of this path
6189 */
6190 return false;
6191 }
6192 return true;
6193}
6194
Joe Stringerfd978bf72018-10-02 13:35:35 -07006195static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur)
6196{
6197 if (old->acquired_refs != cur->acquired_refs)
6198 return false;
6199 return !memcmp(old->refs, cur->refs,
6200 sizeof(*old->refs) * old->acquired_refs);
6201}
6202
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006203/* compare two verifier states
6204 *
6205 * all states stored in state_list are known to be valid, since
6206 * verifier reached 'bpf_exit' instruction through them
6207 *
6208 * this function is called when verifier exploring different branches of
6209 * execution popped from the state stack. If it sees an old state that has
6210 * more strict register state and more strict stack state then this execution
6211 * branch doesn't need to be explored further, since verifier already
6212 * concluded that more strict state leads to valid finish.
6213 *
6214 * Therefore two states are equivalent if register state is more conservative
6215 * and explored stack state is more conservative than the current one.
6216 * Example:
6217 * explored current
6218 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
6219 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
6220 *
6221 * In other words if current stack state (one being explored) has more
6222 * valid slots than old one that already passed validation, it means
6223 * the verifier can stop exploring and conclude that current state is valid too
6224 *
6225 * Similarly with registers. If explored state has register type as invalid
6226 * whereas register type in current state is meaningful, it means that
6227 * the current state will reach 'bpf_exit' instruction safely
6228 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006229static bool func_states_equal(struct bpf_func_state *old,
6230 struct bpf_func_state *cur)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006231{
Edward Creef1174f72017-08-07 15:26:19 +01006232 struct idpair *idmap;
6233 bool ret = false;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006234 int i;
6235
Edward Creef1174f72017-08-07 15:26:19 +01006236 idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL);
6237 /* If we failed to allocate the idmap, just say it's not safe */
6238 if (!idmap)
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07006239 return false;
Edward Creef1174f72017-08-07 15:26:19 +01006240
6241 for (i = 0; i < MAX_BPF_REG; i++) {
Edward Cree1b688a12017-08-23 15:10:50 +01006242 if (!regsafe(&old->regs[i], &cur->regs[i], idmap))
Edward Creef1174f72017-08-07 15:26:19 +01006243 goto out_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006244 }
6245
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006246 if (!stacksafe(old, cur, idmap))
6247 goto out_free;
Joe Stringerfd978bf72018-10-02 13:35:35 -07006248
6249 if (!refsafe(old, cur))
6250 goto out_free;
Edward Creef1174f72017-08-07 15:26:19 +01006251 ret = true;
6252out_free:
6253 kfree(idmap);
6254 return ret;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006255}
6256
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006257static bool states_equal(struct bpf_verifier_env *env,
6258 struct bpf_verifier_state *old,
6259 struct bpf_verifier_state *cur)
Edward Creedc503a82017-08-15 20:34:35 +01006260{
Edward Creedc503a82017-08-15 20:34:35 +01006261 int i;
6262
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006263 if (old->curframe != cur->curframe)
6264 return false;
6265
Daniel Borkmann979d63d2019-01-03 00:58:34 +01006266 /* Verification state from speculative execution simulation
6267 * must never prune a non-speculative execution one.
6268 */
6269 if (old->speculative && !cur->speculative)
6270 return false;
6271
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08006272 if (old->active_spin_lock != cur->active_spin_lock)
6273 return false;
6274
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006275 /* for states to be equal callsites have to be the same
6276 * and all frame states need to be equivalent
6277 */
6278 for (i = 0; i <= old->curframe; i++) {
6279 if (old->frame[i]->callsite != cur->frame[i]->callsite)
6280 return false;
6281 if (!func_states_equal(old->frame[i], cur->frame[i]))
6282 return false;
6283 }
6284 return true;
6285}
6286
Jiong Wang55e7f3b2019-04-12 22:59:36 +01006287static int propagate_liveness_reg(struct bpf_verifier_env *env,
6288 struct bpf_reg_state *reg,
6289 struct bpf_reg_state *parent_reg)
6290{
6291 int err;
6292
6293 if (parent_reg->live & REG_LIVE_READ || !(reg->live & REG_LIVE_READ))
6294 return 0;
6295
6296 err = mark_reg_read(env, reg, parent_reg);
6297 if (err)
6298 return err;
6299
6300 return 0;
6301}
6302
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006303/* A write screens off any subsequent reads; but write marks come from the
6304 * straight-line code between a state and its parent. When we arrive at an
6305 * equivalent state (jump target or such) we didn't arrive by the straight-line
6306 * code, so read marks in the state must propagate to the parent regardless
6307 * of the state's write marks. That's what 'parent == state->parent' comparison
Edward Cree679c7822018-08-22 20:02:19 +01006308 * in mark_reg_read() is for.
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006309 */
6310static int propagate_liveness(struct bpf_verifier_env *env,
6311 const struct bpf_verifier_state *vstate,
6312 struct bpf_verifier_state *vparent)
6313{
Jiong Wang3f8cafa2019-04-12 22:59:35 +01006314 struct bpf_reg_state *state_reg, *parent_reg;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006315 struct bpf_func_state *state, *parent;
Jiong Wang3f8cafa2019-04-12 22:59:35 +01006316 int i, frame, err = 0;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006317
6318 if (vparent->curframe != vstate->curframe) {
6319 WARN(1, "propagate_live: parent frame %d current frame %d\n",
6320 vparent->curframe, vstate->curframe);
6321 return -EFAULT;
6322 }
Edward Creedc503a82017-08-15 20:34:35 +01006323 /* Propagate read liveness of registers... */
6324 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
Jakub Kicinski83d16312019-03-21 14:34:36 -07006325 for (frame = 0; frame <= vstate->curframe; frame++) {
Jiong Wang3f8cafa2019-04-12 22:59:35 +01006326 parent = vparent->frame[frame];
6327 state = vstate->frame[frame];
6328 parent_reg = parent->regs;
6329 state_reg = state->regs;
Jakub Kicinski83d16312019-03-21 14:34:36 -07006330 /* We don't need to worry about FP liveness, it's read-only */
6331 for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) {
Jiong Wang55e7f3b2019-04-12 22:59:36 +01006332 err = propagate_liveness_reg(env, &state_reg[i],
6333 &parent_reg[i]);
Jiong Wang3f8cafa2019-04-12 22:59:35 +01006334 if (err)
6335 return err;
Edward Creedc503a82017-08-15 20:34:35 +01006336 }
Edward Creedc503a82017-08-15 20:34:35 +01006337
Jiong Wang1b04aee2019-04-12 22:59:34 +01006338 /* Propagate stack slots. */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006339 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE &&
6340 i < parent->allocated_stack / BPF_REG_SIZE; i++) {
Jiong Wang3f8cafa2019-04-12 22:59:35 +01006341 parent_reg = &parent->stack[i].spilled_ptr;
6342 state_reg = &state->stack[i].spilled_ptr;
Jiong Wang55e7f3b2019-04-12 22:59:36 +01006343 err = propagate_liveness_reg(env, state_reg,
6344 parent_reg);
Jiong Wang3f8cafa2019-04-12 22:59:35 +01006345 if (err)
6346 return err;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006347 }
Edward Creedc503a82017-08-15 20:34:35 +01006348 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006349 return err;
Edward Creedc503a82017-08-15 20:34:35 +01006350}
6351
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006352static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006353{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006354 struct bpf_verifier_state_list *new_sl;
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -07006355 struct bpf_verifier_state_list *sl, **pprev;
Edward Cree679c7822018-08-22 20:02:19 +01006356 struct bpf_verifier_state *cur = env->cur_state, *new;
Alexei Starovoitovceefbc92018-12-03 22:46:06 -08006357 int i, j, err, states_cnt = 0;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006358
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -07006359 pprev = &env->explored_states[insn_idx];
6360 sl = *pprev;
6361
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006362 if (!sl)
6363 /* this 'insn_idx' instruction wasn't marked, so we will not
6364 * be doing state search here
6365 */
6366 return 0;
6367
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -08006368 clean_live_states(env, insn_idx, cur);
6369
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006370 while (sl != STATE_LIST_MARK) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006371 if (states_equal(env, &sl->state, cur)) {
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -07006372 sl->hit_cnt++;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006373 /* reached equivalent register/stack state,
Edward Creedc503a82017-08-15 20:34:35 +01006374 * prune the search.
6375 * Registers read by the continuation are read by us.
Edward Cree8e9cd9c2017-08-23 15:11:21 +01006376 * If we have any write marks in env->cur_state, they
6377 * will prevent corresponding reads in the continuation
6378 * from reaching our parent (an explored_state). Our
6379 * own state will get the read marks recorded, but
6380 * they'll be immediately forgotten as we're pruning
6381 * this state and will pop a new one.
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006382 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006383 err = propagate_liveness(env, &sl->state, cur);
6384 if (err)
6385 return err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006386 return 1;
Edward Creedc503a82017-08-15 20:34:35 +01006387 }
Alexei Starovoitovceefbc92018-12-03 22:46:06 -08006388 states_cnt++;
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -07006389 sl->miss_cnt++;
6390 /* heuristic to determine whether this state is beneficial
6391 * to keep checking from state equivalence point of view.
6392 * Higher numbers increase max_states_per_insn and verification time,
6393 * but do not meaningfully decrease insn_processed.
6394 */
6395 if (sl->miss_cnt > sl->hit_cnt * 3 + 3) {
6396 /* the state is unlikely to be useful. Remove it to
6397 * speed up verification
6398 */
6399 *pprev = sl->next;
6400 if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) {
6401 free_verifier_state(&sl->state, false);
6402 kfree(sl);
6403 env->peak_states--;
6404 } else {
6405 /* cannot free this state, since parentage chain may
6406 * walk it later. Add it for free_list instead to
6407 * be freed at the end of verification
6408 */
6409 sl->next = env->free_list;
6410 env->free_list = sl;
6411 }
6412 sl = *pprev;
6413 continue;
6414 }
6415 pprev = &sl->next;
6416 sl = *pprev;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006417 }
6418
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006419 if (env->max_states_per_insn < states_cnt)
6420 env->max_states_per_insn = states_cnt;
6421
Alexei Starovoitovceefbc92018-12-03 22:46:06 -08006422 if (!env->allow_ptr_leaks && states_cnt > BPF_COMPLEXITY_LIMIT_STATES)
6423 return 0;
6424
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006425 /* there were no equivalent states, remember current one.
6426 * technically the current state is not proven to be safe yet,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006427 * but it will either reach outer most bpf_exit (which means it's safe)
6428 * or it will be rejected. Since there are no loops, we won't be
6429 * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx)
6430 * again on the way to bpf_exit
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006431 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006432 new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006433 if (!new_sl)
6434 return -ENOMEM;
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006435 env->total_states++;
6436 env->peak_states++;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006437
6438 /* add new state to the head of linked list */
Edward Cree679c7822018-08-22 20:02:19 +01006439 new = &new_sl->state;
6440 err = copy_verifier_state(new, cur);
Alexei Starovoitov1969db42017-11-01 00:08:04 -07006441 if (err) {
Edward Cree679c7822018-08-22 20:02:19 +01006442 free_verifier_state(new, false);
Alexei Starovoitov1969db42017-11-01 00:08:04 -07006443 kfree(new_sl);
6444 return err;
6445 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006446 new_sl->next = env->explored_states[insn_idx];
6447 env->explored_states[insn_idx] = new_sl;
Jakub Kicinski7640ead2018-12-12 16:29:07 -08006448 /* connect new state to parentage chain. Current frame needs all
6449 * registers connected. Only r6 - r9 of the callers are alive (pushed
6450 * to the stack implicitly by JITs) so in callers' frames connect just
6451 * r6 - r9 as an optimization. Callers will have r1 - r5 connected to
6452 * the state of the call instruction (with WRITTEN set), and r0 comes
6453 * from callee with its full parentage chain, anyway.
6454 */
6455 for (j = 0; j <= cur->curframe; j++)
6456 for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++)
6457 cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i];
Edward Cree8e9cd9c2017-08-23 15:11:21 +01006458 /* clear write marks in current state: the writes we did are not writes
6459 * our child did, so they don't screen off its reads from us.
6460 * (There are no read marks in current state, because reads always mark
6461 * their parent and current state never has children yet. Only
6462 * explored_states can get read marks.)
6463 */
Edward Creedc503a82017-08-15 20:34:35 +01006464 for (i = 0; i < BPF_REG_FP; i++)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006465 cur->frame[cur->curframe]->regs[i].live = REG_LIVE_NONE;
6466
6467 /* all stack frames are accessible from callee, clear them all */
6468 for (j = 0; j <= cur->curframe; j++) {
6469 struct bpf_func_state *frame = cur->frame[j];
Edward Cree679c7822018-08-22 20:02:19 +01006470 struct bpf_func_state *newframe = new->frame[j];
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006471
Edward Cree679c7822018-08-22 20:02:19 +01006472 for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08006473 frame->stack[i].spilled_ptr.live = REG_LIVE_NONE;
Edward Cree679c7822018-08-22 20:02:19 +01006474 frame->stack[i].spilled_ptr.parent =
6475 &newframe->stack[i].spilled_ptr;
6476 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006477 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006478 return 0;
6479}
6480
Joe Stringerc64b7982018-10-02 13:35:33 -07006481/* Return true if it's OK to have the same insn return a different type. */
6482static bool reg_type_mismatch_ok(enum bpf_reg_type type)
6483{
6484 switch (type) {
6485 case PTR_TO_CTX:
6486 case PTR_TO_SOCKET:
6487 case PTR_TO_SOCKET_OR_NULL:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08006488 case PTR_TO_SOCK_COMMON:
6489 case PTR_TO_SOCK_COMMON_OR_NULL:
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08006490 case PTR_TO_TCP_SOCK:
6491 case PTR_TO_TCP_SOCK_OR_NULL:
Joe Stringerc64b7982018-10-02 13:35:33 -07006492 return false;
6493 default:
6494 return true;
6495 }
6496}
6497
6498/* If an instruction was previously used with particular pointer types, then we
6499 * need to be careful to avoid cases such as the below, where it may be ok
6500 * for one branch accessing the pointer, but not ok for the other branch:
6501 *
6502 * R1 = sock_ptr
6503 * goto X;
6504 * ...
6505 * R1 = some_other_valid_ptr;
6506 * goto X;
6507 * ...
6508 * R2 = *(u32 *)(R1 + 0);
6509 */
6510static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev)
6511{
6512 return src != prev && (!reg_type_mismatch_ok(src) ||
6513 !reg_type_mismatch_ok(prev));
6514}
6515
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006516static int do_check(struct bpf_verifier_env *env)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006517{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006518 struct bpf_verifier_state *state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006519 struct bpf_insn *insns = env->prog->insnsi;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006520 struct bpf_reg_state *regs;
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006521 int insn_cnt = env->prog->len;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006522 bool do_print_state = false;
6523
Martin KaFai Laud9762e82018-12-13 10:41:48 -08006524 env->prev_linfo = NULL;
6525
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006526 state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL);
6527 if (!state)
6528 return -ENOMEM;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006529 state->curframe = 0;
Daniel Borkmann979d63d2019-01-03 00:58:34 +01006530 state->speculative = false;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006531 state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL);
6532 if (!state->frame[0]) {
6533 kfree(state);
6534 return -ENOMEM;
6535 }
6536 env->cur_state = state;
6537 init_func_state(env, state->frame[0],
6538 BPF_MAIN_FUNC /* callsite */,
6539 0 /* frameno */,
6540 0 /* subprogno, zero == main subprog */);
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006541
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006542 for (;;) {
6543 struct bpf_insn *insn;
6544 u8 class;
6545 int err;
6546
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006547 if (env->insn_idx >= insn_cnt) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006548 verbose(env, "invalid insn idx %d insn_cnt %d\n",
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006549 env->insn_idx, insn_cnt);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006550 return -EFAULT;
6551 }
6552
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006553 insn = &insns[env->insn_idx];
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006554 class = BPF_CLASS(insn->code);
6555
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006556 if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006557 verbose(env,
6558 "BPF program is too large. Processed %d insn\n",
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006559 env->insn_processed);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006560 return -E2BIG;
6561 }
6562
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006563 err = is_state_visited(env, env->insn_idx);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006564 if (err < 0)
6565 return err;
6566 if (err == 1) {
6567 /* found equivalent state, can prune the search */
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006568 if (env->log.level & BPF_LOG_LEVEL) {
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006569 if (do_print_state)
Daniel Borkmann979d63d2019-01-03 00:58:34 +01006570 verbose(env, "\nfrom %d to %d%s: safe\n",
6571 env->prev_insn_idx, env->insn_idx,
6572 env->cur_state->speculative ?
6573 " (speculative execution)" : "");
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006574 else
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006575 verbose(env, "%d: safe\n", env->insn_idx);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006576 }
6577 goto process_bpf_exit;
6578 }
6579
Alexei Starovoitovc3494802018-12-03 22:46:04 -08006580 if (signal_pending(current))
6581 return -EAGAIN;
6582
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02006583 if (need_resched())
6584 cond_resched();
6585
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006586 if (env->log.level & BPF_LOG_LEVEL2 ||
6587 (env->log.level & BPF_LOG_LEVEL && do_print_state)) {
6588 if (env->log.level & BPF_LOG_LEVEL2)
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006589 verbose(env, "%d:", env->insn_idx);
David S. Millerc5fc9692017-05-10 11:25:17 -07006590 else
Daniel Borkmann979d63d2019-01-03 00:58:34 +01006591 verbose(env, "\nfrom %d to %d%s:",
6592 env->prev_insn_idx, env->insn_idx,
6593 env->cur_state->speculative ?
6594 " (speculative execution)" : "");
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006595 print_verifier_state(env, state->frame[state->curframe]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006596 do_print_state = false;
6597 }
6598
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006599 if (env->log.level & BPF_LOG_LEVEL) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01006600 const struct bpf_insn_cbs cbs = {
6601 .cb_print = verbose,
Jiri Olsaabe08842018-03-23 11:41:28 +01006602 .private_data = env,
Daniel Borkmann7105e822017-12-20 13:42:57 +01006603 };
6604
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006605 verbose_linfo(env, env->insn_idx, "; ");
6606 verbose(env, "%d: ", env->insn_idx);
Jiri Olsaabe08842018-03-23 11:41:28 +01006607 print_bpf_insn(&cbs, insn, env->allow_ptr_leaks);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006608 }
6609
Jakub Kicinskicae19272017-12-27 18:39:05 -08006610 if (bpf_prog_is_dev_bound(env->prog->aux)) {
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006611 err = bpf_prog_offload_verify_insn(env, env->insn_idx,
6612 env->prev_insn_idx);
Jakub Kicinskicae19272017-12-27 18:39:05 -08006613 if (err)
6614 return err;
6615 }
Jakub Kicinski13a27df2016-09-21 11:43:58 +01006616
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006617 regs = cur_regs(env);
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006618 env->insn_aux_data[env->insn_idx].seen = true;
Joe Stringerfd978bf72018-10-02 13:35:35 -07006619
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006620 if (class == BPF_ALU || class == BPF_ALU64) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07006621 err = check_alu_op(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006622 if (err)
6623 return err;
6624
6625 } else if (class == BPF_LDX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006626 enum bpf_reg_type *prev_src_type, src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006627
6628 /* check for reserved fields is already done */
6629
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006630 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01006631 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006632 if (err)
6633 return err;
6634
Edward Creedc503a82017-08-15 20:34:35 +01006635 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006636 if (err)
6637 return err;
6638
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07006639 src_reg_type = regs[insn->src_reg].type;
6640
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006641 /* check that memory (src_reg + off) is readable,
6642 * the state of dst_reg will be updated by this func
6643 */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006644 err = check_mem_access(env, env->insn_idx, insn->src_reg,
6645 insn->off, BPF_SIZE(insn->code),
6646 BPF_READ, insn->dst_reg, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006647 if (err)
6648 return err;
6649
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006650 prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006651
6652 if (*prev_src_type == NOT_INIT) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006653 /* saw a valid insn
6654 * dst_reg = *(u32 *)(src_reg + off)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006655 * save type to validate intersecting paths
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006656 */
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006657 *prev_src_type = src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006658
Joe Stringerc64b7982018-10-02 13:35:33 -07006659 } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006660 /* ABuser program is trying to use the same insn
6661 * dst_reg = *(u32*) (src_reg + off)
6662 * with different pointer types:
6663 * src_reg == ctx in one branch and
6664 * src_reg == stack|map in some other branch.
6665 * Reject it.
6666 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006667 verbose(env, "same insn cannot be used with different pointers\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006668 return -EINVAL;
6669 }
6670
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006671 } else if (class == BPF_STX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006672 enum bpf_reg_type *prev_dst_type, dst_reg_type;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006673
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006674 if (BPF_MODE(insn->code) == BPF_XADD) {
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006675 err = check_xadd(env, env->insn_idx, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006676 if (err)
6677 return err;
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006678 env->insn_idx++;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006679 continue;
6680 }
6681
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006682 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01006683 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006684 if (err)
6685 return err;
6686 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01006687 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006688 if (err)
6689 return err;
6690
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006691 dst_reg_type = regs[insn->dst_reg].type;
6692
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006693 /* check that memory (dst_reg + off) is writeable */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006694 err = check_mem_access(env, env->insn_idx, insn->dst_reg,
6695 insn->off, BPF_SIZE(insn->code),
6696 BPF_WRITE, insn->src_reg, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006697 if (err)
6698 return err;
6699
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006700 prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006701
6702 if (*prev_dst_type == NOT_INIT) {
6703 *prev_dst_type = dst_reg_type;
Joe Stringerc64b7982018-10-02 13:35:33 -07006704 } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006705 verbose(env, "same insn cannot be used with different pointers\n");
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006706 return -EINVAL;
6707 }
6708
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006709 } else if (class == BPF_ST) {
6710 if (BPF_MODE(insn->code) != BPF_MEM ||
6711 insn->src_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006712 verbose(env, "BPF_ST uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006713 return -EINVAL;
6714 }
6715 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01006716 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006717 if (err)
6718 return err;
6719
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01006720 if (is_ctx_reg(env, insn->dst_reg)) {
Joe Stringer9d2be442018-10-02 13:35:31 -07006721 verbose(env, "BPF_ST stores into R%d %s is not allowed\n",
Daniel Borkmann2a159c62018-10-21 02:09:24 +02006722 insn->dst_reg,
6723 reg_type_str[reg_state(env, insn->dst_reg)->type]);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01006724 return -EACCES;
6725 }
6726
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006727 /* check that memory (dst_reg + off) is writeable */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006728 err = check_mem_access(env, env->insn_idx, insn->dst_reg,
6729 insn->off, BPF_SIZE(insn->code),
6730 BPF_WRITE, -1, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006731 if (err)
6732 return err;
6733
Jiong Wang092ed092019-01-26 12:26:01 -05006734 } else if (class == BPF_JMP || class == BPF_JMP32) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006735 u8 opcode = BPF_OP(insn->code);
6736
6737 if (opcode == BPF_CALL) {
6738 if (BPF_SRC(insn->code) != BPF_K ||
6739 insn->off != 0 ||
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006740 (insn->src_reg != BPF_REG_0 &&
6741 insn->src_reg != BPF_PSEUDO_CALL) ||
Jiong Wang092ed092019-01-26 12:26:01 -05006742 insn->dst_reg != BPF_REG_0 ||
6743 class == BPF_JMP32) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006744 verbose(env, "BPF_CALL uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006745 return -EINVAL;
6746 }
6747
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08006748 if (env->cur_state->active_spin_lock &&
6749 (insn->src_reg == BPF_PSEUDO_CALL ||
6750 insn->imm != BPF_FUNC_spin_unlock)) {
6751 verbose(env, "function calls are not allowed while holding a lock\n");
6752 return -EINVAL;
6753 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006754 if (insn->src_reg == BPF_PSEUDO_CALL)
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006755 err = check_func_call(env, insn, &env->insn_idx);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006756 else
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006757 err = check_helper_call(env, insn->imm, env->insn_idx);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006758 if (err)
6759 return err;
6760
6761 } else if (opcode == BPF_JA) {
6762 if (BPF_SRC(insn->code) != BPF_K ||
6763 insn->imm != 0 ||
6764 insn->src_reg != BPF_REG_0 ||
Jiong Wang092ed092019-01-26 12:26:01 -05006765 insn->dst_reg != BPF_REG_0 ||
6766 class == BPF_JMP32) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006767 verbose(env, "BPF_JA uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006768 return -EINVAL;
6769 }
6770
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006771 env->insn_idx += insn->off + 1;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006772 continue;
6773
6774 } else if (opcode == BPF_EXIT) {
6775 if (BPF_SRC(insn->code) != BPF_K ||
6776 insn->imm != 0 ||
6777 insn->src_reg != BPF_REG_0 ||
Jiong Wang092ed092019-01-26 12:26:01 -05006778 insn->dst_reg != BPF_REG_0 ||
6779 class == BPF_JMP32) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006780 verbose(env, "BPF_EXIT uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006781 return -EINVAL;
6782 }
6783
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08006784 if (env->cur_state->active_spin_lock) {
6785 verbose(env, "bpf_spin_unlock is missing\n");
6786 return -EINVAL;
6787 }
6788
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006789 if (state->curframe) {
6790 /* exit from nested function */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006791 env->prev_insn_idx = env->insn_idx;
6792 err = prepare_func_exit(env, &env->insn_idx);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006793 if (err)
6794 return err;
6795 do_print_state = true;
6796 continue;
6797 }
6798
Joe Stringerfd978bf72018-10-02 13:35:35 -07006799 err = check_reference_leak(env);
6800 if (err)
6801 return err;
6802
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006803 /* eBPF calling convetion is such that R0 is used
6804 * to return the value from eBPF program.
6805 * Make sure that it's readable at this time
6806 * of bpf_exit, which means that program wrote
6807 * something into it earlier
6808 */
Edward Creedc503a82017-08-15 20:34:35 +01006809 err = check_reg_arg(env, BPF_REG_0, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006810 if (err)
6811 return err;
6812
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07006813 if (is_pointer_value(env, BPF_REG_0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006814 verbose(env, "R0 leaks addr as return value\n");
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07006815 return -EACCES;
6816 }
6817
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07006818 err = check_return_code(env);
6819 if (err)
6820 return err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006821process_bpf_exit:
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006822 err = pop_stack(env, &env->prev_insn_idx,
6823 &env->insn_idx);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006824 if (err < 0) {
6825 if (err != -ENOENT)
6826 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006827 break;
6828 } else {
6829 do_print_state = true;
6830 continue;
6831 }
6832 } else {
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006833 err = check_cond_jmp_op(env, insn, &env->insn_idx);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006834 if (err)
6835 return err;
6836 }
6837 } else if (class == BPF_LD) {
6838 u8 mode = BPF_MODE(insn->code);
6839
6840 if (mode == BPF_ABS || mode == BPF_IND) {
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08006841 err = check_ld_abs(env, insn);
6842 if (err)
6843 return err;
6844
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006845 } else if (mode == BPF_IMM) {
6846 err = check_ld_imm(env, insn);
6847 if (err)
6848 return err;
6849
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006850 env->insn_idx++;
6851 env->insn_aux_data[env->insn_idx].seen = true;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006852 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006853 verbose(env, "invalid BPF_LD mode\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006854 return -EINVAL;
6855 }
6856 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006857 verbose(env, "unknown insn class %d\n", class);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006858 return -EINVAL;
6859 }
6860
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006861 env->insn_idx++;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006862 }
6863
Jiong Wang9c8105b2018-05-02 16:17:18 -04006864 env->prog->aux->stack_depth = env->subprog_info[0].stack_depth;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006865 return 0;
6866}
6867
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07006868static int check_map_prealloc(struct bpf_map *map)
6869{
6870 return (map->map_type != BPF_MAP_TYPE_HASH &&
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07006871 map->map_type != BPF_MAP_TYPE_PERCPU_HASH &&
6872 map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) ||
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07006873 !(map->map_flags & BPF_F_NO_PREALLOC);
6874}
6875
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08006876static bool is_tracing_prog_type(enum bpf_prog_type type)
6877{
6878 switch (type) {
6879 case BPF_PROG_TYPE_KPROBE:
6880 case BPF_PROG_TYPE_TRACEPOINT:
6881 case BPF_PROG_TYPE_PERF_EVENT:
6882 case BPF_PROG_TYPE_RAW_TRACEPOINT:
6883 return true;
6884 default:
6885 return false;
6886 }
6887}
6888
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006889static int check_map_prog_compatibility(struct bpf_verifier_env *env,
6890 struct bpf_map *map,
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07006891 struct bpf_prog *prog)
6892
6893{
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07006894 /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use
6895 * preallocated hash maps, since doing memory allocation
6896 * in overflow_handler can crash depending on where nmi got
6897 * triggered.
6898 */
6899 if (prog->type == BPF_PROG_TYPE_PERF_EVENT) {
6900 if (!check_map_prealloc(map)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006901 verbose(env, "perf_event programs can only use preallocated hash map\n");
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07006902 return -EINVAL;
6903 }
6904 if (map->inner_map_meta &&
6905 !check_map_prealloc(map->inner_map_meta)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006906 verbose(env, "perf_event programs can only use preallocated inner hash map\n");
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07006907 return -EINVAL;
6908 }
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07006909 }
Jakub Kicinskia3884572018-01-11 20:29:09 -08006910
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08006911 if ((is_tracing_prog_type(prog->type) ||
6912 prog->type == BPF_PROG_TYPE_SOCKET_FILTER) &&
6913 map_value_has_spin_lock(map)) {
6914 verbose(env, "tracing progs cannot use bpf_spin_lock yet\n");
6915 return -EINVAL;
6916 }
6917
Jakub Kicinskia3884572018-01-11 20:29:09 -08006918 if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) &&
Jakub Kicinski09728262018-07-17 10:53:23 -07006919 !bpf_offload_prog_map_match(prog, map)) {
Jakub Kicinskia3884572018-01-11 20:29:09 -08006920 verbose(env, "offload device mismatch between prog and map\n");
6921 return -EINVAL;
6922 }
6923
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07006924 return 0;
6925}
6926
Roman Gushchinb741f162018-09-28 14:45:43 +00006927static bool bpf_map_is_cgroup_storage(struct bpf_map *map)
6928{
6929 return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE ||
6930 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE);
6931}
6932
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006933/* look for pseudo eBPF instructions that access map FDs and
6934 * replace them with actual map pointers
6935 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006936static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006937{
6938 struct bpf_insn *insn = env->prog->insnsi;
6939 int insn_cnt = env->prog->len;
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07006940 int i, j, err;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006941
Daniel Borkmannf1f77142017-01-13 23:38:15 +01006942 err = bpf_prog_calc_tag(env->prog);
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01006943 if (err)
6944 return err;
6945
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006946 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006947 if (BPF_CLASS(insn->code) == BPF_LDX &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006948 (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006949 verbose(env, "BPF_LDX uses reserved fields\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006950 return -EINVAL;
6951 }
6952
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006953 if (BPF_CLASS(insn->code) == BPF_STX &&
6954 ((BPF_MODE(insn->code) != BPF_MEM &&
6955 BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006956 verbose(env, "BPF_STX uses reserved fields\n");
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006957 return -EINVAL;
6958 }
6959
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006960 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02006961 struct bpf_insn_aux_data *aux;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006962 struct bpf_map *map;
6963 struct fd f;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02006964 u64 addr;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006965
6966 if (i == insn_cnt - 1 || insn[1].code != 0 ||
6967 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
6968 insn[1].off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006969 verbose(env, "invalid bpf_ld_imm64 insn\n");
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006970 return -EINVAL;
6971 }
6972
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02006973 if (insn[0].src_reg == 0)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006974 /* valid generic load 64-bit imm */
6975 goto next_insn;
6976
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02006977 /* In final convert_pseudo_ld_imm64() step, this is
6978 * converted into regular 64-bit imm load insn.
6979 */
6980 if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD &&
6981 insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) ||
6982 (insn[0].src_reg == BPF_PSEUDO_MAP_FD &&
6983 insn[1].imm != 0)) {
6984 verbose(env,
6985 "unrecognized bpf_ld_imm64 insn\n");
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006986 return -EINVAL;
6987 }
6988
Daniel Borkmann20182392019-03-04 21:08:53 +01006989 f = fdget(insn[0].imm);
Daniel Borkmannc2101292015-10-29 14:58:07 +01006990 map = __bpf_map_get(f);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006991 if (IS_ERR(map)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006992 verbose(env, "fd %d is not pointing to valid bpf_map\n",
Daniel Borkmann20182392019-03-04 21:08:53 +01006993 insn[0].imm);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006994 return PTR_ERR(map);
6995 }
6996
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006997 err = check_map_prog_compatibility(env, map, env->prog);
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07006998 if (err) {
6999 fdput(f);
7000 return err;
7001 }
7002
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02007003 aux = &env->insn_aux_data[i];
7004 if (insn->src_reg == BPF_PSEUDO_MAP_FD) {
7005 addr = (unsigned long)map;
7006 } else {
7007 u32 off = insn[1].imm;
7008
7009 if (off >= BPF_MAX_VAR_OFF) {
7010 verbose(env, "direct value offset of %u is not allowed\n", off);
7011 fdput(f);
7012 return -EINVAL;
7013 }
7014
7015 if (!map->ops->map_direct_value_addr) {
7016 verbose(env, "no direct value access support for this map type\n");
7017 fdput(f);
7018 return -EINVAL;
7019 }
7020
7021 err = map->ops->map_direct_value_addr(map, &addr, off);
7022 if (err) {
7023 verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n",
7024 map->value_size, off);
7025 fdput(f);
7026 return err;
7027 }
7028
7029 aux->map_off = off;
7030 addr += off;
7031 }
7032
7033 insn[0].imm = (u32)addr;
7034 insn[1].imm = addr >> 32;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007035
7036 /* check whether we recorded this map already */
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02007037 for (j = 0; j < env->used_map_cnt; j++) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007038 if (env->used_maps[j] == map) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02007039 aux->map_index = j;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007040 fdput(f);
7041 goto next_insn;
7042 }
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02007043 }
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007044
7045 if (env->used_map_cnt >= MAX_USED_MAPS) {
7046 fdput(f);
7047 return -E2BIG;
7048 }
7049
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007050 /* hold the map. If the program is rejected by verifier,
7051 * the map will be released by release_maps() or it
7052 * will be used by the valid program until it's unloaded
Jakub Kicinskiab7f5bf2018-05-03 18:37:17 -07007053 * and all maps are released in free_used_maps()
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007054 */
Alexei Starovoitov92117d82016-04-27 18:56:20 -07007055 map = bpf_map_inc(map, false);
7056 if (IS_ERR(map)) {
7057 fdput(f);
7058 return PTR_ERR(map);
7059 }
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02007060
7061 aux->map_index = env->used_map_cnt;
Alexei Starovoitov92117d82016-04-27 18:56:20 -07007062 env->used_maps[env->used_map_cnt++] = map;
7063
Roman Gushchinb741f162018-09-28 14:45:43 +00007064 if (bpf_map_is_cgroup_storage(map) &&
Roman Gushchinde9cbba2018-08-02 14:27:18 -07007065 bpf_cgroup_storage_assign(env->prog, map)) {
Roman Gushchinb741f162018-09-28 14:45:43 +00007066 verbose(env, "only one cgroup storage of each type is allowed\n");
Roman Gushchinde9cbba2018-08-02 14:27:18 -07007067 fdput(f);
7068 return -EBUSY;
7069 }
7070
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007071 fdput(f);
7072next_insn:
7073 insn++;
7074 i++;
Daniel Borkmann5e581da2018-01-26 23:33:38 +01007075 continue;
7076 }
7077
7078 /* Basic sanity check before we invest more work here. */
7079 if (!bpf_opcode_in_insntable(insn->code)) {
7080 verbose(env, "unknown opcode %02x\n", insn->code);
7081 return -EINVAL;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007082 }
7083 }
7084
7085 /* now all pseudo BPF_LD_IMM64 instructions load valid
7086 * 'struct bpf_map *' into a register instead of user map_fd.
7087 * These pointers will be used later by verifier to validate map access.
7088 */
7089 return 0;
7090}
7091
7092/* drop refcnt of maps used by the rejected program */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007093static void release_maps(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007094{
Roman Gushchin8bad74f2018-09-28 14:45:36 +00007095 enum bpf_cgroup_storage_type stype;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007096 int i;
7097
Roman Gushchin8bad74f2018-09-28 14:45:36 +00007098 for_each_cgroup_storage_type(stype) {
7099 if (!env->prog->aux->cgroup_storage[stype])
7100 continue;
Roman Gushchinde9cbba2018-08-02 14:27:18 -07007101 bpf_cgroup_storage_release(env->prog,
Roman Gushchin8bad74f2018-09-28 14:45:36 +00007102 env->prog->aux->cgroup_storage[stype]);
7103 }
Roman Gushchinde9cbba2018-08-02 14:27:18 -07007104
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007105 for (i = 0; i < env->used_map_cnt; i++)
7106 bpf_map_put(env->used_maps[i]);
7107}
7108
7109/* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007110static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007111{
7112 struct bpf_insn *insn = env->prog->insnsi;
7113 int insn_cnt = env->prog->len;
7114 int i;
7115
7116 for (i = 0; i < insn_cnt; i++, insn++)
7117 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
7118 insn->src_reg = 0;
7119}
7120
Alexei Starovoitov80419022017-03-15 18:26:41 -07007121/* single env->prog->insni[off] instruction was replaced with the range
7122 * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
7123 * [0, off) and [off, end) to new locations, so the patched range stays zero
7124 */
7125static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len,
7126 u32 off, u32 cnt)
7127{
7128 struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data;
Alexei Starovoitovc1311872017-11-22 16:42:05 -08007129 int i;
Alexei Starovoitov80419022017-03-15 18:26:41 -07007130
7131 if (cnt == 1)
7132 return 0;
Kees Cookfad953c2018-06-12 14:27:37 -07007133 new_data = vzalloc(array_size(prog_len,
7134 sizeof(struct bpf_insn_aux_data)));
Alexei Starovoitov80419022017-03-15 18:26:41 -07007135 if (!new_data)
7136 return -ENOMEM;
7137 memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off);
7138 memcpy(new_data + off + cnt - 1, old_data + off,
7139 sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
Alexei Starovoitovc1311872017-11-22 16:42:05 -08007140 for (i = off; i < off + cnt - 1; i++)
7141 new_data[i].seen = true;
Alexei Starovoitov80419022017-03-15 18:26:41 -07007142 env->insn_aux_data = new_data;
7143 vfree(old_data);
7144 return 0;
7145}
7146
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08007147static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len)
7148{
7149 int i;
7150
7151 if (len == 1)
7152 return;
Jiong Wang4cb3d992018-05-02 16:17:19 -04007153 /* NOTE: fake 'exit' subprog should be updated as well. */
7154 for (i = 0; i <= env->subprog_cnt; i++) {
Edward Creeafd59422018-11-16 12:00:07 +00007155 if (env->subprog_info[i].start <= off)
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08007156 continue;
Jiong Wang9c8105b2018-05-02 16:17:18 -04007157 env->subprog_info[i].start += len - 1;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08007158 }
7159}
7160
Alexei Starovoitov80419022017-03-15 18:26:41 -07007161static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
7162 const struct bpf_insn *patch, u32 len)
7163{
7164 struct bpf_prog *new_prog;
7165
7166 new_prog = bpf_patch_insn_single(env->prog, off, patch, len);
Alexei Starovoitov4f733792019-04-01 21:27:44 -07007167 if (IS_ERR(new_prog)) {
7168 if (PTR_ERR(new_prog) == -ERANGE)
7169 verbose(env,
7170 "insn %d cannot be patched due to 16-bit range\n",
7171 env->insn_aux_data[off].orig_idx);
Alexei Starovoitov80419022017-03-15 18:26:41 -07007172 return NULL;
Alexei Starovoitov4f733792019-04-01 21:27:44 -07007173 }
Alexei Starovoitov80419022017-03-15 18:26:41 -07007174 if (adjust_insn_aux_data(env, new_prog->len, off, len))
7175 return NULL;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08007176 adjust_subprog_starts(env, off, len);
Alexei Starovoitov80419022017-03-15 18:26:41 -07007177 return new_prog;
7178}
7179
Jakub Kicinski52875a02019-01-22 22:45:20 -08007180static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env,
7181 u32 off, u32 cnt)
7182{
7183 int i, j;
7184
7185 /* find first prog starting at or after off (first to remove) */
7186 for (i = 0; i < env->subprog_cnt; i++)
7187 if (env->subprog_info[i].start >= off)
7188 break;
7189 /* find first prog starting at or after off + cnt (first to stay) */
7190 for (j = i; j < env->subprog_cnt; j++)
7191 if (env->subprog_info[j].start >= off + cnt)
7192 break;
7193 /* if j doesn't start exactly at off + cnt, we are just removing
7194 * the front of previous prog
7195 */
7196 if (env->subprog_info[j].start != off + cnt)
7197 j--;
7198
7199 if (j > i) {
7200 struct bpf_prog_aux *aux = env->prog->aux;
7201 int move;
7202
7203 /* move fake 'exit' subprog as well */
7204 move = env->subprog_cnt + 1 - j;
7205
7206 memmove(env->subprog_info + i,
7207 env->subprog_info + j,
7208 sizeof(*env->subprog_info) * move);
7209 env->subprog_cnt -= j - i;
7210
7211 /* remove func_info */
7212 if (aux->func_info) {
7213 move = aux->func_info_cnt - j;
7214
7215 memmove(aux->func_info + i,
7216 aux->func_info + j,
7217 sizeof(*aux->func_info) * move);
7218 aux->func_info_cnt -= j - i;
7219 /* func_info->insn_off is set after all code rewrites,
7220 * in adjust_btf_func() - no need to adjust
7221 */
7222 }
7223 } else {
7224 /* convert i from "first prog to remove" to "first to adjust" */
7225 if (env->subprog_info[i].start == off)
7226 i++;
7227 }
7228
7229 /* update fake 'exit' subprog as well */
7230 for (; i <= env->subprog_cnt; i++)
7231 env->subprog_info[i].start -= cnt;
7232
7233 return 0;
7234}
7235
7236static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off,
7237 u32 cnt)
7238{
7239 struct bpf_prog *prog = env->prog;
7240 u32 i, l_off, l_cnt, nr_linfo;
7241 struct bpf_line_info *linfo;
7242
7243 nr_linfo = prog->aux->nr_linfo;
7244 if (!nr_linfo)
7245 return 0;
7246
7247 linfo = prog->aux->linfo;
7248
7249 /* find first line info to remove, count lines to be removed */
7250 for (i = 0; i < nr_linfo; i++)
7251 if (linfo[i].insn_off >= off)
7252 break;
7253
7254 l_off = i;
7255 l_cnt = 0;
7256 for (; i < nr_linfo; i++)
7257 if (linfo[i].insn_off < off + cnt)
7258 l_cnt++;
7259 else
7260 break;
7261
7262 /* First live insn doesn't match first live linfo, it needs to "inherit"
7263 * last removed linfo. prog is already modified, so prog->len == off
7264 * means no live instructions after (tail of the program was removed).
7265 */
7266 if (prog->len != off && l_cnt &&
7267 (i == nr_linfo || linfo[i].insn_off != off + cnt)) {
7268 l_cnt--;
7269 linfo[--i].insn_off = off + cnt;
7270 }
7271
7272 /* remove the line info which refer to the removed instructions */
7273 if (l_cnt) {
7274 memmove(linfo + l_off, linfo + i,
7275 sizeof(*linfo) * (nr_linfo - i));
7276
7277 prog->aux->nr_linfo -= l_cnt;
7278 nr_linfo = prog->aux->nr_linfo;
7279 }
7280
7281 /* pull all linfo[i].insn_off >= off + cnt in by cnt */
7282 for (i = l_off; i < nr_linfo; i++)
7283 linfo[i].insn_off -= cnt;
7284
7285 /* fix up all subprogs (incl. 'exit') which start >= off */
7286 for (i = 0; i <= env->subprog_cnt; i++)
7287 if (env->subprog_info[i].linfo_idx > l_off) {
7288 /* program may have started in the removed region but
7289 * may not be fully removed
7290 */
7291 if (env->subprog_info[i].linfo_idx >= l_off + l_cnt)
7292 env->subprog_info[i].linfo_idx -= l_cnt;
7293 else
7294 env->subprog_info[i].linfo_idx = l_off;
7295 }
7296
7297 return 0;
7298}
7299
7300static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)
7301{
7302 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
7303 unsigned int orig_prog_len = env->prog->len;
7304 int err;
7305
Jakub Kicinski08ca90a2019-01-22 22:45:24 -08007306 if (bpf_prog_is_dev_bound(env->prog->aux))
7307 bpf_prog_offload_remove_insns(env, off, cnt);
7308
Jakub Kicinski52875a02019-01-22 22:45:20 -08007309 err = bpf_remove_insns(env->prog, off, cnt);
7310 if (err)
7311 return err;
7312
7313 err = adjust_subprog_starts_after_remove(env, off, cnt);
7314 if (err)
7315 return err;
7316
7317 err = bpf_adj_linfo_after_remove(env, off, cnt);
7318 if (err)
7319 return err;
7320
7321 memmove(aux_data + off, aux_data + off + cnt,
7322 sizeof(*aux_data) * (orig_prog_len - off - cnt));
7323
7324 return 0;
7325}
7326
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01007327/* The verifier does more data flow analysis than llvm and will not
7328 * explore branches that are dead at run time. Malicious programs can
7329 * have dead code too. Therefore replace all dead at-run-time code
7330 * with 'ja -1'.
7331 *
7332 * Just nops are not optimal, e.g. if they would sit at the end of the
7333 * program and through another bug we would manage to jump there, then
7334 * we'd execute beyond program memory otherwise. Returning exception
7335 * code also wouldn't work since we can have subprogs where the dead
7336 * code could be located.
Alexei Starovoitovc1311872017-11-22 16:42:05 -08007337 */
7338static void sanitize_dead_code(struct bpf_verifier_env *env)
7339{
7340 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01007341 struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1);
Alexei Starovoitovc1311872017-11-22 16:42:05 -08007342 struct bpf_insn *insn = env->prog->insnsi;
7343 const int insn_cnt = env->prog->len;
7344 int i;
7345
7346 for (i = 0; i < insn_cnt; i++) {
7347 if (aux_data[i].seen)
7348 continue;
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01007349 memcpy(insn + i, &trap, sizeof(trap));
Alexei Starovoitovc1311872017-11-22 16:42:05 -08007350 }
7351}
7352
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08007353static bool insn_is_cond_jump(u8 code)
7354{
7355 u8 op;
7356
Jiong Wang092ed092019-01-26 12:26:01 -05007357 if (BPF_CLASS(code) == BPF_JMP32)
7358 return true;
7359
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08007360 if (BPF_CLASS(code) != BPF_JMP)
7361 return false;
7362
7363 op = BPF_OP(code);
7364 return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL;
7365}
7366
7367static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env)
7368{
7369 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
7370 struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0);
7371 struct bpf_insn *insn = env->prog->insnsi;
7372 const int insn_cnt = env->prog->len;
7373 int i;
7374
7375 for (i = 0; i < insn_cnt; i++, insn++) {
7376 if (!insn_is_cond_jump(insn->code))
7377 continue;
7378
7379 if (!aux_data[i + 1].seen)
7380 ja.off = insn->off;
7381 else if (!aux_data[i + 1 + insn->off].seen)
7382 ja.off = 0;
7383 else
7384 continue;
7385
Jakub Kicinski08ca90a2019-01-22 22:45:24 -08007386 if (bpf_prog_is_dev_bound(env->prog->aux))
7387 bpf_prog_offload_replace_insn(env, i, &ja);
7388
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08007389 memcpy(insn, &ja, sizeof(ja));
7390 }
7391}
7392
Jakub Kicinski52875a02019-01-22 22:45:20 -08007393static int opt_remove_dead_code(struct bpf_verifier_env *env)
7394{
7395 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
7396 int insn_cnt = env->prog->len;
7397 int i, err;
7398
7399 for (i = 0; i < insn_cnt; i++) {
7400 int j;
7401
7402 j = 0;
7403 while (i + j < insn_cnt && !aux_data[i + j].seen)
7404 j++;
7405 if (!j)
7406 continue;
7407
7408 err = verifier_remove_insns(env, i, j);
7409 if (err)
7410 return err;
7411 insn_cnt = env->prog->len;
7412 }
7413
7414 return 0;
7415}
7416
Jakub Kicinskia1b14ab2019-01-22 22:45:21 -08007417static int opt_remove_nops(struct bpf_verifier_env *env)
7418{
7419 const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0);
7420 struct bpf_insn *insn = env->prog->insnsi;
7421 int insn_cnt = env->prog->len;
7422 int i, err;
7423
7424 for (i = 0; i < insn_cnt; i++) {
7425 if (memcmp(&insn[i], &ja, sizeof(ja)))
7426 continue;
7427
7428 err = verifier_remove_insns(env, i, 1);
7429 if (err)
7430 return err;
7431 insn_cnt--;
7432 i--;
7433 }
7434
7435 return 0;
7436}
7437
Joe Stringerc64b7982018-10-02 13:35:33 -07007438/* convert load instructions that access fields of a context type into a
7439 * sequence of instructions that access fields of the underlying structure:
7440 * struct __sk_buff -> struct sk_buff
7441 * struct bpf_sock_ops -> struct sock
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007442 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007443static int convert_ctx_accesses(struct bpf_verifier_env *env)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007444{
Jakub Kicinski00176a32017-10-16 16:40:54 -07007445 const struct bpf_verifier_ops *ops = env->ops;
Daniel Borkmannf96da092017-07-02 02:13:27 +02007446 int i, cnt, size, ctx_field_size, delta = 0;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007447 const int insn_cnt = env->prog->len;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007448 struct bpf_insn insn_buf[16], *insn;
Andrey Ignatov46f53a62018-11-10 22:15:13 -08007449 u32 target_size, size_default, off;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007450 struct bpf_prog *new_prog;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007451 enum bpf_access_type type;
Daniel Borkmannf96da092017-07-02 02:13:27 +02007452 bool is_narrower_load;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007453
Daniel Borkmannb09928b2018-10-24 22:05:49 +02007454 if (ops->gen_prologue || env->seen_direct_write) {
7455 if (!ops->gen_prologue) {
7456 verbose(env, "bpf verifier is misconfigured\n");
7457 return -EINVAL;
7458 }
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007459 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
7460 env->prog);
7461 if (cnt >= ARRAY_SIZE(insn_buf)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007462 verbose(env, "bpf verifier is misconfigured\n");
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007463 return -EINVAL;
7464 } else if (cnt) {
Alexei Starovoitov80419022017-03-15 18:26:41 -07007465 new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007466 if (!new_prog)
7467 return -ENOMEM;
Alexei Starovoitov80419022017-03-15 18:26:41 -07007468
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007469 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007470 delta += cnt - 1;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007471 }
7472 }
7473
Joe Stringerc64b7982018-10-02 13:35:33 -07007474 if (bpf_prog_is_dev_bound(env->prog->aux))
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007475 return 0;
7476
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007477 insn = env->prog->insnsi + delta;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007478
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007479 for (i = 0; i < insn_cnt; i++, insn++) {
Joe Stringerc64b7982018-10-02 13:35:33 -07007480 bpf_convert_ctx_access_t convert_ctx_access;
7481
Daniel Borkmann62c79892017-01-12 11:51:33 +01007482 if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) ||
7483 insn->code == (BPF_LDX | BPF_MEM | BPF_H) ||
7484 insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07007485 insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007486 type = BPF_READ;
Daniel Borkmann62c79892017-01-12 11:51:33 +01007487 else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) ||
7488 insn->code == (BPF_STX | BPF_MEM | BPF_H) ||
7489 insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07007490 insn->code == (BPF_STX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007491 type = BPF_WRITE;
7492 else
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007493 continue;
7494
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07007495 if (type == BPF_WRITE &&
7496 env->insn_aux_data[i + delta].sanitize_stack_off) {
7497 struct bpf_insn patch[] = {
7498 /* Sanitize suspicious stack slot with zero.
7499 * There are no memory dependencies for this store,
7500 * since it's only using frame pointer and immediate
7501 * constant of zero
7502 */
7503 BPF_ST_MEM(BPF_DW, BPF_REG_FP,
7504 env->insn_aux_data[i + delta].sanitize_stack_off,
7505 0),
7506 /* the original STX instruction will immediately
7507 * overwrite the same stack slot with appropriate value
7508 */
7509 *insn,
7510 };
7511
7512 cnt = ARRAY_SIZE(patch);
7513 new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt);
7514 if (!new_prog)
7515 return -ENOMEM;
7516
7517 delta += cnt - 1;
7518 env->prog = new_prog;
7519 insn = new_prog->insnsi + i + delta;
7520 continue;
7521 }
7522
Joe Stringerc64b7982018-10-02 13:35:33 -07007523 switch (env->insn_aux_data[i + delta].ptr_type) {
7524 case PTR_TO_CTX:
7525 if (!ops->convert_ctx_access)
7526 continue;
7527 convert_ctx_access = ops->convert_ctx_access;
7528 break;
7529 case PTR_TO_SOCKET:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08007530 case PTR_TO_SOCK_COMMON:
Joe Stringerc64b7982018-10-02 13:35:33 -07007531 convert_ctx_access = bpf_sock_convert_ctx_access;
7532 break;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08007533 case PTR_TO_TCP_SOCK:
7534 convert_ctx_access = bpf_tcp_sock_convert_ctx_access;
7535 break;
Joe Stringerc64b7982018-10-02 13:35:33 -07007536 default:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007537 continue;
Joe Stringerc64b7982018-10-02 13:35:33 -07007538 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007539
Yonghong Song31fd8582017-06-13 15:52:13 -07007540 ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size;
Daniel Borkmannf96da092017-07-02 02:13:27 +02007541 size = BPF_LDST_BYTES(insn);
Yonghong Song31fd8582017-06-13 15:52:13 -07007542
7543 /* If the read access is a narrower load of the field,
7544 * convert to a 4/8-byte load, to minimum program type specific
7545 * convert_ctx_access changes. If conversion is successful,
7546 * we will apply proper mask to the result.
7547 */
Daniel Borkmannf96da092017-07-02 02:13:27 +02007548 is_narrower_load = size < ctx_field_size;
Andrey Ignatov46f53a62018-11-10 22:15:13 -08007549 size_default = bpf_ctx_off_adjust_machine(ctx_field_size);
7550 off = insn->off;
Yonghong Song31fd8582017-06-13 15:52:13 -07007551 if (is_narrower_load) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02007552 u8 size_code;
Yonghong Song31fd8582017-06-13 15:52:13 -07007553
Daniel Borkmannf96da092017-07-02 02:13:27 +02007554 if (type == BPF_WRITE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007555 verbose(env, "bpf verifier narrow ctx access misconfigured\n");
Daniel Borkmannf96da092017-07-02 02:13:27 +02007556 return -EINVAL;
7557 }
7558
7559 size_code = BPF_H;
Yonghong Song31fd8582017-06-13 15:52:13 -07007560 if (ctx_field_size == 4)
7561 size_code = BPF_W;
7562 else if (ctx_field_size == 8)
7563 size_code = BPF_DW;
Daniel Borkmannf96da092017-07-02 02:13:27 +02007564
Daniel Borkmannbc231052018-06-02 23:06:39 +02007565 insn->off = off & ~(size_default - 1);
Yonghong Song31fd8582017-06-13 15:52:13 -07007566 insn->code = BPF_LDX | BPF_MEM | size_code;
7567 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02007568
7569 target_size = 0;
Joe Stringerc64b7982018-10-02 13:35:33 -07007570 cnt = convert_ctx_access(type, insn, insn_buf, env->prog,
7571 &target_size);
Daniel Borkmannf96da092017-07-02 02:13:27 +02007572 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) ||
7573 (ctx_field_size && !target_size)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007574 verbose(env, "bpf verifier is misconfigured\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007575 return -EINVAL;
7576 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02007577
7578 if (is_narrower_load && size < target_size) {
Andrey Ignatov46f53a62018-11-10 22:15:13 -08007579 u8 shift = (off & (size_default - 1)) * 8;
7580
7581 if (ctx_field_size <= 4) {
7582 if (shift)
7583 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH,
7584 insn->dst_reg,
7585 shift);
Yonghong Song31fd8582017-06-13 15:52:13 -07007586 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02007587 (1 << size * 8) - 1);
Andrey Ignatov46f53a62018-11-10 22:15:13 -08007588 } else {
7589 if (shift)
7590 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH,
7591 insn->dst_reg,
7592 shift);
Yonghong Song31fd8582017-06-13 15:52:13 -07007593 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg,
Krzesimir Nowake2f7fc02019-05-08 18:08:58 +02007594 (1ULL << size * 8) - 1);
Andrey Ignatov46f53a62018-11-10 22:15:13 -08007595 }
Yonghong Song31fd8582017-06-13 15:52:13 -07007596 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007597
Alexei Starovoitov80419022017-03-15 18:26:41 -07007598 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007599 if (!new_prog)
7600 return -ENOMEM;
7601
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007602 delta += cnt - 1;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007603
7604 /* keep walking new program and skip insns we just inserted */
7605 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007606 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007607 }
7608
7609 return 0;
7610}
7611
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007612static int jit_subprogs(struct bpf_verifier_env *env)
7613{
7614 struct bpf_prog *prog = env->prog, **func, *tmp;
7615 int i, j, subprog_start, subprog_end = 0, len, subprog;
Daniel Borkmann7105e822017-12-20 13:42:57 +01007616 struct bpf_insn *insn;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007617 void *old_bpf_func;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08007618 int err;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007619
Jiong Wangf910cef2018-05-02 16:17:17 -04007620 if (env->subprog_cnt <= 1)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007621 return 0;
7622
Daniel Borkmann7105e822017-12-20 13:42:57 +01007623 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007624 if (insn->code != (BPF_JMP | BPF_CALL) ||
7625 insn->src_reg != BPF_PSEUDO_CALL)
7626 continue;
Daniel Borkmannc7a89782018-07-12 21:44:28 +02007627 /* Upon error here we cannot fall back to interpreter but
7628 * need a hard reject of the program. Thus -EFAULT is
7629 * propagated in any case.
7630 */
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007631 subprog = find_subprog(env, i + insn->imm + 1);
7632 if (subprog < 0) {
7633 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
7634 i + insn->imm + 1);
7635 return -EFAULT;
7636 }
7637 /* temporarily remember subprog id inside insn instead of
7638 * aux_data, since next loop will split up all insns into funcs
7639 */
Jiong Wangf910cef2018-05-02 16:17:17 -04007640 insn->off = subprog;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007641 /* remember original imm in case JIT fails and fallback
7642 * to interpreter will be needed
7643 */
7644 env->insn_aux_data[i].call_imm = insn->imm;
7645 /* point imm to __bpf_call_base+1 from JITs point of view */
7646 insn->imm = 1;
7647 }
7648
Martin KaFai Lauc454a462018-12-07 16:42:25 -08007649 err = bpf_prog_alloc_jited_linfo(prog);
7650 if (err)
7651 goto out_undo_insn;
7652
7653 err = -ENOMEM;
Kees Cook6396bb22018-06-12 14:03:40 -07007654 func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007655 if (!func)
Daniel Borkmannc7a89782018-07-12 21:44:28 +02007656 goto out_undo_insn;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007657
Jiong Wangf910cef2018-05-02 16:17:17 -04007658 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007659 subprog_start = subprog_end;
Jiong Wang4cb3d992018-05-02 16:17:19 -04007660 subprog_end = env->subprog_info[i + 1].start;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007661
7662 len = subprog_end - subprog_start;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08007663 /* BPF_PROG_RUN doesn't call subprogs directly,
7664 * hence main prog stats include the runtime of subprogs.
7665 * subprogs don't have IDs and not reachable via prog_get_next_id
7666 * func[i]->aux->stats will never be accessed and stays NULL
7667 */
7668 func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007669 if (!func[i])
7670 goto out_free;
7671 memcpy(func[i]->insnsi, &prog->insnsi[subprog_start],
7672 len * sizeof(struct bpf_insn));
Daniel Borkmann4f74d802017-12-20 13:42:56 +01007673 func[i]->type = prog->type;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007674 func[i]->len = len;
Daniel Borkmann4f74d802017-12-20 13:42:56 +01007675 if (bpf_prog_calc_tag(func[i]))
7676 goto out_free;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007677 func[i]->is_func = 1;
Yonghong Songba64e7d2018-11-24 23:20:44 -08007678 func[i]->aux->func_idx = i;
7679 /* the btf and func_info will be freed only at prog->aux */
7680 func[i]->aux->btf = prog->aux->btf;
7681 func[i]->aux->func_info = prog->aux->func_info;
7682
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007683 /* Use bpf_prog_F_tag to indicate functions in stack traces.
7684 * Long term would need debug info to populate names
7685 */
7686 func[i]->aux->name[0] = 'F';
Jiong Wang9c8105b2018-05-02 16:17:18 -04007687 func[i]->aux->stack_depth = env->subprog_info[i].stack_depth;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007688 func[i]->jit_requested = 1;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08007689 func[i]->aux->linfo = prog->aux->linfo;
7690 func[i]->aux->nr_linfo = prog->aux->nr_linfo;
7691 func[i]->aux->jited_linfo = prog->aux->jited_linfo;
7692 func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007693 func[i] = bpf_int_jit_compile(func[i]);
7694 if (!func[i]->jited) {
7695 err = -ENOTSUPP;
7696 goto out_free;
7697 }
7698 cond_resched();
7699 }
7700 /* at this point all bpf functions were successfully JITed
7701 * now populate all bpf_calls with correct addresses and
7702 * run last pass of JIT
7703 */
Jiong Wangf910cef2018-05-02 16:17:17 -04007704 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007705 insn = func[i]->insnsi;
7706 for (j = 0; j < func[i]->len; j++, insn++) {
7707 if (insn->code != (BPF_JMP | BPF_CALL) ||
7708 insn->src_reg != BPF_PSEUDO_CALL)
7709 continue;
7710 subprog = insn->off;
Prashant Bhole0d306c32019-04-16 18:13:01 +09007711 insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) -
7712 __bpf_call_base;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007713 }
Sandipan Das2162fed2018-05-24 12:26:45 +05307714
7715 /* we use the aux data to keep a list of the start addresses
7716 * of the JITed images for each function in the program
7717 *
7718 * for some architectures, such as powerpc64, the imm field
7719 * might not be large enough to hold the offset of the start
7720 * address of the callee's JITed image from __bpf_call_base
7721 *
7722 * in such cases, we can lookup the start address of a callee
7723 * by using its subprog id, available from the off field of
7724 * the call instruction, as an index for this list
7725 */
7726 func[i]->aux->func = func;
7727 func[i]->aux->func_cnt = env->subprog_cnt;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007728 }
Jiong Wangf910cef2018-05-02 16:17:17 -04007729 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007730 old_bpf_func = func[i]->bpf_func;
7731 tmp = bpf_int_jit_compile(func[i]);
7732 if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) {
7733 verbose(env, "JIT doesn't support bpf-to-bpf calls\n");
Daniel Borkmannc7a89782018-07-12 21:44:28 +02007734 err = -ENOTSUPP;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007735 goto out_free;
7736 }
7737 cond_resched();
7738 }
7739
7740 /* finally lock prog and jit images for all functions and
7741 * populate kallsysm
7742 */
Jiong Wangf910cef2018-05-02 16:17:17 -04007743 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007744 bpf_prog_lock_ro(func[i]);
7745 bpf_prog_kallsyms_add(func[i]);
7746 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01007747
7748 /* Last step: make now unused interpreter insns from main
7749 * prog consistent for later dump requests, so they can
7750 * later look the same as if they were interpreted only.
7751 */
7752 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01007753 if (insn->code != (BPF_JMP | BPF_CALL) ||
7754 insn->src_reg != BPF_PSEUDO_CALL)
7755 continue;
7756 insn->off = env->insn_aux_data[i].call_imm;
7757 subprog = find_subprog(env, i + insn->off + 1);
Sandipan Dasdbecd732018-05-24 12:26:48 +05307758 insn->imm = subprog;
Daniel Borkmann7105e822017-12-20 13:42:57 +01007759 }
7760
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007761 prog->jited = 1;
7762 prog->bpf_func = func[0]->bpf_func;
7763 prog->aux->func = func;
Jiong Wangf910cef2018-05-02 16:17:17 -04007764 prog->aux->func_cnt = env->subprog_cnt;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08007765 bpf_prog_free_unused_jited_linfo(prog);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007766 return 0;
7767out_free:
Jiong Wangf910cef2018-05-02 16:17:17 -04007768 for (i = 0; i < env->subprog_cnt; i++)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007769 if (func[i])
7770 bpf_jit_free(func[i]);
7771 kfree(func);
Daniel Borkmannc7a89782018-07-12 21:44:28 +02007772out_undo_insn:
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007773 /* cleanup main prog to be interpreted */
7774 prog->jit_requested = 0;
7775 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
7776 if (insn->code != (BPF_JMP | BPF_CALL) ||
7777 insn->src_reg != BPF_PSEUDO_CALL)
7778 continue;
7779 insn->off = 0;
7780 insn->imm = env->insn_aux_data[i].call_imm;
7781 }
Martin KaFai Lauc454a462018-12-07 16:42:25 -08007782 bpf_prog_free_jited_linfo(prog);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007783 return err;
7784}
7785
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08007786static int fixup_call_args(struct bpf_verifier_env *env)
7787{
David S. Miller19d28fb2018-01-11 21:27:54 -05007788#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08007789 struct bpf_prog *prog = env->prog;
7790 struct bpf_insn *insn = prog->insnsi;
7791 int i, depth;
David S. Miller19d28fb2018-01-11 21:27:54 -05007792#endif
Quentin Monnete4052d02018-10-07 12:56:58 +01007793 int err = 0;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08007794
Quentin Monnete4052d02018-10-07 12:56:58 +01007795 if (env->prog->jit_requested &&
7796 !bpf_prog_is_dev_bound(env->prog->aux)) {
David S. Miller19d28fb2018-01-11 21:27:54 -05007797 err = jit_subprogs(env);
7798 if (err == 0)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007799 return 0;
Daniel Borkmannc7a89782018-07-12 21:44:28 +02007800 if (err == -EFAULT)
7801 return err;
David S. Miller19d28fb2018-01-11 21:27:54 -05007802 }
7803#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08007804 for (i = 0; i < prog->len; i++, insn++) {
7805 if (insn->code != (BPF_JMP | BPF_CALL) ||
7806 insn->src_reg != BPF_PSEUDO_CALL)
7807 continue;
7808 depth = get_callee_stack_depth(env, insn, i);
7809 if (depth < 0)
7810 return depth;
7811 bpf_patch_call_args(insn, depth);
7812 }
David S. Miller19d28fb2018-01-11 21:27:54 -05007813 err = 0;
7814#endif
7815 return err;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08007816}
7817
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007818/* fixup insn->imm field of bpf_call instructions
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07007819 * and inline eligible helpers as explicit sequence of BPF instructions
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007820 *
7821 * this function is called after eBPF program passed verification
7822 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007823static int fixup_bpf_calls(struct bpf_verifier_env *env)
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007824{
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007825 struct bpf_prog *prog = env->prog;
7826 struct bpf_insn *insn = prog->insnsi;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007827 const struct bpf_func_proto *fn;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007828 const int insn_cnt = prog->len;
Daniel Borkmann09772d92018-06-02 23:06:35 +02007829 const struct bpf_map_ops *ops;
Daniel Borkmannc93552c2018-05-24 02:32:53 +02007830 struct bpf_insn_aux_data *aux;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07007831 struct bpf_insn insn_buf[16];
7832 struct bpf_prog *new_prog;
7833 struct bpf_map *map_ptr;
7834 int i, cnt, delta = 0;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007835
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007836 for (i = 0; i < insn_cnt; i++, insn++) {
Daniel Borkmannf6b1b3b2018-01-26 23:33:39 +01007837 if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) ||
7838 insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) ||
7839 insn->code == (BPF_ALU | BPF_MOD | BPF_X) ||
Alexei Starovoitov68fda452018-01-12 18:59:52 -08007840 insn->code == (BPF_ALU | BPF_DIV | BPF_X)) {
Daniel Borkmannf6b1b3b2018-01-26 23:33:39 +01007841 bool is64 = BPF_CLASS(insn->code) == BPF_ALU64;
7842 struct bpf_insn mask_and_div[] = {
7843 BPF_MOV32_REG(insn->src_reg, insn->src_reg),
7844 /* Rx div 0 -> 0 */
7845 BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2),
7846 BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg),
7847 BPF_JMP_IMM(BPF_JA, 0, 0, 1),
7848 *insn,
7849 };
7850 struct bpf_insn mask_and_mod[] = {
7851 BPF_MOV32_REG(insn->src_reg, insn->src_reg),
7852 /* Rx mod 0 -> Rx */
7853 BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1),
7854 *insn,
7855 };
7856 struct bpf_insn *patchlet;
7857
7858 if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) ||
7859 insn->code == (BPF_ALU | BPF_DIV | BPF_X)) {
7860 patchlet = mask_and_div + (is64 ? 1 : 0);
7861 cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0);
7862 } else {
7863 patchlet = mask_and_mod + (is64 ? 1 : 0);
7864 cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0);
7865 }
7866
7867 new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt);
Alexei Starovoitov68fda452018-01-12 18:59:52 -08007868 if (!new_prog)
7869 return -ENOMEM;
7870
7871 delta += cnt - 1;
7872 env->prog = prog = new_prog;
7873 insn = new_prog->insnsi + i + delta;
7874 continue;
7875 }
7876
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +02007877 if (BPF_CLASS(insn->code) == BPF_LD &&
7878 (BPF_MODE(insn->code) == BPF_ABS ||
7879 BPF_MODE(insn->code) == BPF_IND)) {
7880 cnt = env->ops->gen_ld_abs(insn, insn_buf);
7881 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
7882 verbose(env, "bpf verifier is misconfigured\n");
7883 return -EINVAL;
7884 }
7885
7886 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
7887 if (!new_prog)
7888 return -ENOMEM;
7889
7890 delta += cnt - 1;
7891 env->prog = prog = new_prog;
7892 insn = new_prog->insnsi + i + delta;
7893 continue;
7894 }
7895
Daniel Borkmann979d63d2019-01-03 00:58:34 +01007896 if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) ||
7897 insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) {
7898 const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X;
7899 const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X;
7900 struct bpf_insn insn_buf[16];
7901 struct bpf_insn *patch = &insn_buf[0];
7902 bool issrc, isneg;
7903 u32 off_reg;
7904
7905 aux = &env->insn_aux_data[i + delta];
Daniel Borkmann3612af72019-03-01 22:05:29 +01007906 if (!aux->alu_state ||
7907 aux->alu_state == BPF_ALU_NON_POINTER)
Daniel Borkmann979d63d2019-01-03 00:58:34 +01007908 continue;
7909
7910 isneg = aux->alu_state & BPF_ALU_NEG_VALUE;
7911 issrc = (aux->alu_state & BPF_ALU_SANITIZE) ==
7912 BPF_ALU_SANITIZE_SRC;
7913
7914 off_reg = issrc ? insn->src_reg : insn->dst_reg;
7915 if (isneg)
7916 *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1);
7917 *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1);
7918 *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg);
7919 *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg);
7920 *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0);
7921 *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63);
7922 if (issrc) {
7923 *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX,
7924 off_reg);
7925 insn->src_reg = BPF_REG_AX;
7926 } else {
7927 *patch++ = BPF_ALU64_REG(BPF_AND, off_reg,
7928 BPF_REG_AX);
7929 }
7930 if (isneg)
7931 insn->code = insn->code == code_add ?
7932 code_sub : code_add;
7933 *patch++ = *insn;
7934 if (issrc && isneg)
7935 *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1);
7936 cnt = patch - insn_buf;
7937
7938 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
7939 if (!new_prog)
7940 return -ENOMEM;
7941
7942 delta += cnt - 1;
7943 env->prog = prog = new_prog;
7944 insn = new_prog->insnsi + i + delta;
7945 continue;
7946 }
7947
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007948 if (insn->code != (BPF_JMP | BPF_CALL))
7949 continue;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08007950 if (insn->src_reg == BPF_PSEUDO_CALL)
7951 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007952
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007953 if (insn->imm == BPF_FUNC_get_route_realm)
7954 prog->dst_needed = 1;
7955 if (insn->imm == BPF_FUNC_get_prandom_u32)
7956 bpf_user_rnd_init_once();
Josef Bacik9802d862017-12-11 11:36:48 -05007957 if (insn->imm == BPF_FUNC_override_return)
7958 prog->kprobe_override = 1;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007959 if (insn->imm == BPF_FUNC_tail_call) {
David S. Miller7b9f6da2017-04-20 10:35:33 -04007960 /* If we tail call into other programs, we
7961 * cannot make any assumptions since they can
7962 * be replaced dynamically during runtime in
7963 * the program array.
7964 */
7965 prog->cb_access = 1;
Alexei Starovoitov80a58d02017-05-30 13:31:30 -07007966 env->prog->aux->stack_depth = MAX_BPF_STACK;
Jiong Wange6478152018-11-08 04:08:42 -05007967 env->prog->aux->max_pkt_offset = MAX_PACKET_OFF;
David S. Miller7b9f6da2017-04-20 10:35:33 -04007968
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007969 /* mark bpf_tail_call as different opcode to avoid
7970 * conditional branch in the interpeter for every normal
7971 * call and to prevent accidental JITing by JIT compiler
7972 * that doesn't support bpf_tail_call yet
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007973 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007974 insn->imm = 0;
Alexei Starovoitov71189fa2017-05-30 13:31:27 -07007975 insn->code = BPF_JMP | BPF_TAIL_CALL;
Alexei Starovoitovb2157392018-01-07 17:33:02 -08007976
Daniel Borkmannc93552c2018-05-24 02:32:53 +02007977 aux = &env->insn_aux_data[i + delta];
7978 if (!bpf_map_ptr_unpriv(aux))
7979 continue;
7980
Alexei Starovoitovb2157392018-01-07 17:33:02 -08007981 /* instead of changing every JIT dealing with tail_call
7982 * emit two extra insns:
7983 * if (index >= max_entries) goto out;
7984 * index &= array->index_mask;
7985 * to avoid out-of-bounds cpu speculation
7986 */
Daniel Borkmannc93552c2018-05-24 02:32:53 +02007987 if (bpf_map_ptr_poisoned(aux)) {
Colin Ian King40950342018-01-10 09:20:54 +00007988 verbose(env, "tail_call abusing map_ptr\n");
Alexei Starovoitovb2157392018-01-07 17:33:02 -08007989 return -EINVAL;
7990 }
Daniel Borkmannc93552c2018-05-24 02:32:53 +02007991
7992 map_ptr = BPF_MAP_PTR(aux->map_state);
Alexei Starovoitovb2157392018-01-07 17:33:02 -08007993 insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3,
7994 map_ptr->max_entries, 2);
7995 insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3,
7996 container_of(map_ptr,
7997 struct bpf_array,
7998 map)->index_mask);
7999 insn_buf[2] = *insn;
8000 cnt = 3;
8001 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
8002 if (!new_prog)
8003 return -ENOMEM;
8004
8005 delta += cnt - 1;
8006 env->prog = prog = new_prog;
8007 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008008 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008009 }
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008010
Daniel Borkmann89c63072017-08-19 03:12:45 +02008011 /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup
Daniel Borkmann09772d92018-06-02 23:06:35 +02008012 * and other inlining handlers are currently limited to 64 bit
8013 * only.
Daniel Borkmann89c63072017-08-19 03:12:45 +02008014 */
Alexei Starovoitov60b58afc2017-12-14 17:55:14 -08008015 if (prog->jit_requested && BITS_PER_LONG == 64 &&
Daniel Borkmann09772d92018-06-02 23:06:35 +02008016 (insn->imm == BPF_FUNC_map_lookup_elem ||
8017 insn->imm == BPF_FUNC_map_update_elem ||
Daniel Borkmann84430d42018-10-21 02:09:27 +02008018 insn->imm == BPF_FUNC_map_delete_elem ||
8019 insn->imm == BPF_FUNC_map_push_elem ||
8020 insn->imm == BPF_FUNC_map_pop_elem ||
8021 insn->imm == BPF_FUNC_map_peek_elem)) {
Daniel Borkmannc93552c2018-05-24 02:32:53 +02008022 aux = &env->insn_aux_data[i + delta];
8023 if (bpf_map_ptr_poisoned(aux))
8024 goto patch_call_imm;
8025
8026 map_ptr = BPF_MAP_PTR(aux->map_state);
Daniel Borkmann09772d92018-06-02 23:06:35 +02008027 ops = map_ptr->ops;
8028 if (insn->imm == BPF_FUNC_map_lookup_elem &&
8029 ops->map_gen_lookup) {
8030 cnt = ops->map_gen_lookup(map_ptr, insn_buf);
8031 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
8032 verbose(env, "bpf verifier is misconfigured\n");
8033 return -EINVAL;
8034 }
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07008035
Daniel Borkmann09772d92018-06-02 23:06:35 +02008036 new_prog = bpf_patch_insn_data(env, i + delta,
8037 insn_buf, cnt);
8038 if (!new_prog)
8039 return -ENOMEM;
8040
8041 delta += cnt - 1;
8042 env->prog = prog = new_prog;
8043 insn = new_prog->insnsi + i + delta;
8044 continue;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07008045 }
8046
Daniel Borkmann09772d92018-06-02 23:06:35 +02008047 BUILD_BUG_ON(!__same_type(ops->map_lookup_elem,
8048 (void *(*)(struct bpf_map *map, void *key))NULL));
8049 BUILD_BUG_ON(!__same_type(ops->map_delete_elem,
8050 (int (*)(struct bpf_map *map, void *key))NULL));
8051 BUILD_BUG_ON(!__same_type(ops->map_update_elem,
8052 (int (*)(struct bpf_map *map, void *key, void *value,
8053 u64 flags))NULL));
Daniel Borkmann84430d42018-10-21 02:09:27 +02008054 BUILD_BUG_ON(!__same_type(ops->map_push_elem,
8055 (int (*)(struct bpf_map *map, void *value,
8056 u64 flags))NULL));
8057 BUILD_BUG_ON(!__same_type(ops->map_pop_elem,
8058 (int (*)(struct bpf_map *map, void *value))NULL));
8059 BUILD_BUG_ON(!__same_type(ops->map_peek_elem,
8060 (int (*)(struct bpf_map *map, void *value))NULL));
8061
Daniel Borkmann09772d92018-06-02 23:06:35 +02008062 switch (insn->imm) {
8063 case BPF_FUNC_map_lookup_elem:
8064 insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) -
8065 __bpf_call_base;
8066 continue;
8067 case BPF_FUNC_map_update_elem:
8068 insn->imm = BPF_CAST_CALL(ops->map_update_elem) -
8069 __bpf_call_base;
8070 continue;
8071 case BPF_FUNC_map_delete_elem:
8072 insn->imm = BPF_CAST_CALL(ops->map_delete_elem) -
8073 __bpf_call_base;
8074 continue;
Daniel Borkmann84430d42018-10-21 02:09:27 +02008075 case BPF_FUNC_map_push_elem:
8076 insn->imm = BPF_CAST_CALL(ops->map_push_elem) -
8077 __bpf_call_base;
8078 continue;
8079 case BPF_FUNC_map_pop_elem:
8080 insn->imm = BPF_CAST_CALL(ops->map_pop_elem) -
8081 __bpf_call_base;
8082 continue;
8083 case BPF_FUNC_map_peek_elem:
8084 insn->imm = BPF_CAST_CALL(ops->map_peek_elem) -
8085 __bpf_call_base;
8086 continue;
Daniel Borkmann09772d92018-06-02 23:06:35 +02008087 }
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07008088
Daniel Borkmann09772d92018-06-02 23:06:35 +02008089 goto patch_call_imm;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07008090 }
8091
8092patch_call_imm:
Andrey Ignatov5e43f892018-03-30 15:08:00 -07008093 fn = env->ops->get_func_proto(insn->imm, env->prog);
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008094 /* all functions that have prototype and verifier allowed
8095 * programs to call them, must be real in-kernel functions
8096 */
8097 if (!fn->func) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07008098 verbose(env,
8099 "kernel subsystem misconfigured func %s#%d\n",
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008100 func_id_name(insn->imm), insn->imm);
8101 return -EFAULT;
8102 }
8103 insn->imm = fn->func - __bpf_call_base;
8104 }
8105
8106 return 0;
8107}
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008108
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01008109static void free_states(struct bpf_verifier_env *env)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07008110{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01008111 struct bpf_verifier_state_list *sl, *sln;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07008112 int i;
8113
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -07008114 sl = env->free_list;
8115 while (sl) {
8116 sln = sl->next;
8117 free_verifier_state(&sl->state, false);
8118 kfree(sl);
8119 sl = sln;
8120 }
8121
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07008122 if (!env->explored_states)
8123 return;
8124
8125 for (i = 0; i < env->prog->len; i++) {
8126 sl = env->explored_states[i];
8127
8128 if (sl)
8129 while (sl != STATE_LIST_MARK) {
8130 sln = sl->next;
Alexei Starovoitov1969db42017-11-01 00:08:04 -07008131 free_verifier_state(&sl->state, false);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07008132 kfree(sl);
8133 sl = sln;
8134 }
8135 }
8136
Alexei Starovoitov71dde682019-04-01 21:27:43 -07008137 kvfree(env->explored_states);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07008138}
8139
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07008140static void print_verification_stats(struct bpf_verifier_env *env)
8141{
8142 int i;
8143
8144 if (env->log.level & BPF_LOG_STATS) {
8145 verbose(env, "verification time %lld usec\n",
8146 div_u64(env->verification_time, 1000));
8147 verbose(env, "stack depth ");
8148 for (i = 0; i < env->subprog_cnt; i++) {
8149 u32 depth = env->subprog_info[i].stack_depth;
8150
8151 verbose(env, "%d", depth);
8152 if (i + 1 < env->subprog_cnt)
8153 verbose(env, "+");
8154 }
8155 verbose(env, "\n");
8156 }
8157 verbose(env, "processed %d insns (limit %d) max_states_per_insn %d "
8158 "total_states %d peak_states %d mark_read %d\n",
8159 env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS,
8160 env->max_states_per_insn, env->total_states,
8161 env->peak_states, env->longest_mark_read_walk);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008162}
8163
Yonghong Song838e9692018-11-19 15:29:11 -08008164int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,
8165 union bpf_attr __user *uattr)
Alexei Starovoitov51580e72014-09-26 00:17:02 -07008166{
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07008167 u64 start_time = ktime_get_ns();
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01008168 struct bpf_verifier_env *env;
Martin KaFai Laub9193c12018-03-24 11:44:22 -07008169 struct bpf_verifier_log *log;
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08008170 int i, len, ret = -EINVAL;
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08008171 bool is_priv;
Alexei Starovoitov51580e72014-09-26 00:17:02 -07008172
Arnd Bergmanneba0c922017-11-02 12:05:52 +01008173 /* no program is valid */
8174 if (ARRAY_SIZE(bpf_verifier_ops) == 0)
8175 return -EINVAL;
8176
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01008177 /* 'struct bpf_verifier_env' can be global, but since it's not small,
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008178 * allocate/free it every time bpf_check() is called
8179 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01008180 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008181 if (!env)
8182 return -ENOMEM;
Jakub Kicinski61bd5212017-10-09 10:30:11 -07008183 log = &env->log;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008184
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08008185 len = (*prog)->len;
Kees Cookfad953c2018-06-12 14:27:37 -07008186 env->insn_aux_data =
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08008187 vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len));
Jakub Kicinski3df126f2016-09-21 11:43:56 +01008188 ret = -ENOMEM;
8189 if (!env->insn_aux_data)
8190 goto err_free_env;
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08008191 for (i = 0; i < len; i++)
8192 env->insn_aux_data[i].orig_idx = i;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008193 env->prog = *prog;
Jakub Kicinski00176a32017-10-16 16:40:54 -07008194 env->ops = bpf_verifier_ops[env->prog->type];
Alexei Starovoitov45a73c12019-04-19 07:44:55 -07008195 is_priv = capable(CAP_SYS_ADMIN);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008196
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008197 /* grab the mutex to protect few globals used by verifier */
Alexei Starovoitov45a73c12019-04-19 07:44:55 -07008198 if (!is_priv)
8199 mutex_lock(&bpf_verifier_lock);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008200
8201 if (attr->log_level || attr->log_buf || attr->log_size) {
8202 /* user requested verbose verifier output
8203 * and supplied buffer to store the verification trace
8204 */
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07008205 log->level = attr->log_level;
8206 log->ubuf = (char __user *) (unsigned long) attr->log_buf;
8207 log->len_total = attr->log_size;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008208
8209 ret = -EINVAL;
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07008210 /* log attributes have to be sane */
Alexei Starovoitov7a9f5c62019-04-01 21:27:46 -07008211 if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 ||
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07008212 !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01008213 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008214 }
Daniel Borkmann1ad2f582017-05-25 01:05:05 +02008215
8216 env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT);
8217 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
David S. Millere07b98d2017-05-10 11:38:07 -07008218 env->strict_alignment = true;
David Millere9ee9ef2018-11-30 21:08:14 -08008219 if (attr->prog_flags & BPF_F_ANY_ALIGNMENT)
8220 env->strict_alignment = false;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008221
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08008222 env->allow_ptr_leaks = is_priv;
8223
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008224 ret = replace_map_fd_with_map_ptr(env);
8225 if (ret < 0)
8226 goto skip_full_check;
8227
Jakub Kicinskif4e3ec02018-05-03 18:37:11 -07008228 if (bpf_prog_is_dev_bound(env->prog->aux)) {
Quentin Monneta40a2632018-11-09 13:03:31 +00008229 ret = bpf_prog_offload_verifier_prep(env->prog);
Jakub Kicinskif4e3ec02018-05-03 18:37:11 -07008230 if (ret)
8231 goto skip_full_check;
8232 }
8233
Alexei Starovoitov71dde682019-04-01 21:27:43 -07008234 env->explored_states = kvcalloc(env->prog->len,
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01008235 sizeof(struct bpf_verifier_state_list *),
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07008236 GFP_USER);
8237 ret = -ENOMEM;
8238 if (!env->explored_states)
8239 goto skip_full_check;
8240
Martin KaFai Laud9762e82018-12-13 10:41:48 -08008241 ret = check_subprogs(env);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07008242 if (ret < 0)
8243 goto skip_full_check;
8244
Martin KaFai Lauc454a462018-12-07 16:42:25 -08008245 ret = check_btf_info(env, attr, uattr);
Yonghong Song838e9692018-11-19 15:29:11 -08008246 if (ret < 0)
8247 goto skip_full_check;
8248
Martin KaFai Laud9762e82018-12-13 10:41:48 -08008249 ret = check_cfg(env);
8250 if (ret < 0)
8251 goto skip_full_check;
8252
Alexei Starovoitov17a52672014-09-26 00:17:06 -07008253 ret = do_check(env);
Craig Gallek8c01c4f2017-11-02 11:18:01 -04008254 if (env->cur_state) {
8255 free_verifier_state(env->cur_state, true);
8256 env->cur_state = NULL;
8257 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008258
Quentin Monnetc941ce92018-10-07 12:56:47 +01008259 if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux))
8260 ret = bpf_prog_offload_finalize(env);
8261
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008262skip_full_check:
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07008263 while (!pop_stack(env, NULL, NULL));
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07008264 free_states(env);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008265
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008266 if (ret == 0)
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08008267 ret = check_max_stack_depth(env);
8268
Jakub Kicinski9b38c402018-12-19 22:13:06 -08008269 /* instruction rewrites happen after this point */
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08008270 if (is_priv) {
8271 if (ret == 0)
8272 opt_hard_wire_dead_code_branches(env);
Jakub Kicinski52875a02019-01-22 22:45:20 -08008273 if (ret == 0)
8274 ret = opt_remove_dead_code(env);
Jakub Kicinskia1b14ab2019-01-22 22:45:21 -08008275 if (ret == 0)
8276 ret = opt_remove_nops(env);
Jakub Kicinski52875a02019-01-22 22:45:20 -08008277 } else {
8278 if (ret == 0)
8279 sanitize_dead_code(env);
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08008280 }
8281
Jakub Kicinski9b38c402018-12-19 22:13:06 -08008282 if (ret == 0)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008283 /* program is valid, convert *(u32*)(ctx + off) accesses */
8284 ret = convert_ctx_accesses(env);
8285
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008286 if (ret == 0)
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008287 ret = fixup_bpf_calls(env);
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008288
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08008289 if (ret == 0)
8290 ret = fixup_call_args(env);
8291
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07008292 env->verification_time = ktime_get_ns() - start_time;
8293 print_verification_stats(env);
8294
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07008295 if (log->level && bpf_verifier_log_full(log))
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008296 ret = -ENOSPC;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07008297 if (log->level && !log->ubuf) {
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008298 ret = -EFAULT;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07008299 goto err_release_maps;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008300 }
8301
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008302 if (ret == 0 && env->used_map_cnt) {
8303 /* if program passed verifier, update used_maps in bpf_prog_info */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008304 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
8305 sizeof(env->used_maps[0]),
8306 GFP_KERNEL);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008307
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008308 if (!env->prog->aux->used_maps) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008309 ret = -ENOMEM;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07008310 goto err_release_maps;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008311 }
8312
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008313 memcpy(env->prog->aux->used_maps, env->used_maps,
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008314 sizeof(env->used_maps[0]) * env->used_map_cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008315 env->prog->aux->used_map_cnt = env->used_map_cnt;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008316
8317 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
8318 * bpf_ld_imm64 instructions
8319 */
8320 convert_pseudo_ld_imm64(env);
8321 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008322
Yonghong Songba64e7d2018-11-24 23:20:44 -08008323 if (ret == 0)
8324 adjust_btf_func(env);
8325
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07008326err_release_maps:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008327 if (!env->prog->aux->used_maps)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008328 /* if we didn't copy map pointers into bpf_prog_info, release
Jakub Kicinskiab7f5bf2018-05-03 18:37:17 -07008329 * them now. Otherwise free_used_maps() will release them.
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008330 */
8331 release_maps(env);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008332 *prog = env->prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01008333err_unlock:
Alexei Starovoitov45a73c12019-04-19 07:44:55 -07008334 if (!is_priv)
8335 mutex_unlock(&bpf_verifier_lock);
Jakub Kicinski3df126f2016-09-21 11:43:56 +01008336 vfree(env->insn_aux_data);
8337err_free_env:
8338 kfree(env);
Alexei Starovoitov51580e72014-09-26 00:17:02 -07008339 return ret;
8340}