blob: def87e9cc9c79ee3afb1eb1a67e7f212d27dcedc [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
Alexei Starovoitovb285fcb2019-05-21 20:14:19 -0700171#define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192
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 ||
Jonathan Lemonfada7fd2019-06-06 13:59:40 -0700329 type == PTR_TO_TCP_SOCK ||
330 type == PTR_TO_XDP_SOCK;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800331}
332
Joe Stringer840b9612018-10-02 13:35:32 -0700333static bool reg_type_may_be_null(enum bpf_reg_type type)
334{
Joe Stringerfd978bf72018-10-02 13:35:35 -0700335 return type == PTR_TO_MAP_VALUE_OR_NULL ||
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800336 type == PTR_TO_SOCKET_OR_NULL ||
Martin KaFai Lau655a51e2019-02-09 23:22:24 -0800337 type == PTR_TO_SOCK_COMMON_OR_NULL ||
338 type == PTR_TO_TCP_SOCK_OR_NULL;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700339}
340
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800341static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg)
342{
343 return reg->type == PTR_TO_MAP_VALUE &&
344 map_value_has_spin_lock(reg->map_ptr);
345}
346
Martin KaFai Laucba368c2019-03-18 10:37:13 -0700347static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type)
348{
349 return type == PTR_TO_SOCKET ||
350 type == PTR_TO_SOCKET_OR_NULL ||
351 type == PTR_TO_TCP_SOCK ||
352 type == PTR_TO_TCP_SOCK_OR_NULL;
353}
354
Martin KaFai Lau1b986582019-03-12 10:23:02 -0700355static bool arg_type_may_be_refcounted(enum bpf_arg_type type)
Joe Stringerfd978bf72018-10-02 13:35:35 -0700356{
Martin KaFai Lau1b986582019-03-12 10:23:02 -0700357 return type == ARG_PTR_TO_SOCK_COMMON;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700358}
359
360/* Determine whether the function releases some resources allocated by another
361 * function call. The first reference type argument will be assumed to be
362 * released by release_reference().
363 */
364static bool is_release_function(enum bpf_func_id func_id)
365{
Joe Stringer6acc9b42018-10-02 13:35:36 -0700366 return func_id == BPF_FUNC_sk_release;
Joe Stringer840b9612018-10-02 13:35:32 -0700367}
368
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800369static bool is_acquire_function(enum bpf_func_id func_id)
370{
371 return func_id == BPF_FUNC_sk_lookup_tcp ||
Lorenz Baueredbf8c02019-03-22 09:54:01 +0800372 func_id == BPF_FUNC_sk_lookup_udp ||
373 func_id == BPF_FUNC_skc_lookup_tcp;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800374}
375
Martin KaFai Lau1b986582019-03-12 10:23:02 -0700376static bool is_ptr_cast_function(enum bpf_func_id func_id)
377{
378 return func_id == BPF_FUNC_tcp_sock ||
379 func_id == BPF_FUNC_sk_fullsock;
380}
381
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700382/* string representation of 'enum bpf_reg_type' */
383static const char * const reg_type_str[] = {
384 [NOT_INIT] = "?",
Edward Creef1174f72017-08-07 15:26:19 +0100385 [SCALAR_VALUE] = "inv",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700386 [PTR_TO_CTX] = "ctx",
387 [CONST_PTR_TO_MAP] = "map_ptr",
388 [PTR_TO_MAP_VALUE] = "map_value",
389 [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700390 [PTR_TO_STACK] = "fp",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700391 [PTR_TO_PACKET] = "pkt",
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200392 [PTR_TO_PACKET_META] = "pkt_meta",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700393 [PTR_TO_PACKET_END] = "pkt_end",
Petar Penkovd58e4682018-09-14 07:46:18 -0700394 [PTR_TO_FLOW_KEYS] = "flow_keys",
Joe Stringerc64b7982018-10-02 13:35:33 -0700395 [PTR_TO_SOCKET] = "sock",
396 [PTR_TO_SOCKET_OR_NULL] = "sock_or_null",
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800397 [PTR_TO_SOCK_COMMON] = "sock_common",
398 [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null",
Martin KaFai Lau655a51e2019-02-09 23:22:24 -0800399 [PTR_TO_TCP_SOCK] = "tcp_sock",
400 [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null",
Matt Mullins9df1c282019-04-26 11:49:47 -0700401 [PTR_TO_TP_BUFFER] = "tp_buffer",
Jonathan Lemonfada7fd2019-06-06 13:59:40 -0700402 [PTR_TO_XDP_SOCK] = "xdp_sock",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700403};
404
Edward Cree8efea212018-08-22 20:02:44 +0100405static char slot_type_char[] = {
406 [STACK_INVALID] = '?',
407 [STACK_SPILL] = 'r',
408 [STACK_MISC] = 'm',
409 [STACK_ZERO] = '0',
410};
411
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800412static void print_liveness(struct bpf_verifier_env *env,
413 enum bpf_reg_liveness live)
414{
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -0800415 if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE))
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800416 verbose(env, "_");
417 if (live & REG_LIVE_READ)
418 verbose(env, "r");
419 if (live & REG_LIVE_WRITTEN)
420 verbose(env, "w");
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -0800421 if (live & REG_LIVE_DONE)
422 verbose(env, "D");
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800423}
424
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800425static struct bpf_func_state *func(struct bpf_verifier_env *env,
426 const struct bpf_reg_state *reg)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700427{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800428 struct bpf_verifier_state *cur = env->cur_state;
429
430 return cur->frame[reg->frameno];
431}
432
433static void print_verifier_state(struct bpf_verifier_env *env,
434 const struct bpf_func_state *state)
435{
436 const struct bpf_reg_state *reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700437 enum bpf_reg_type t;
438 int i;
439
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800440 if (state->frameno)
441 verbose(env, " frame%d:", state->frameno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700442 for (i = 0; i < MAX_BPF_REG; i++) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700443 reg = &state->regs[i];
444 t = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700445 if (t == NOT_INIT)
446 continue;
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800447 verbose(env, " R%d", i);
448 print_liveness(env, reg->live);
449 verbose(env, "=%s", reg_type_str[t]);
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -0700450 if (t == SCALAR_VALUE && reg->precise)
451 verbose(env, "P");
Edward Creef1174f72017-08-07 15:26:19 +0100452 if ((t == SCALAR_VALUE || t == PTR_TO_STACK) &&
453 tnum_is_const(reg->var_off)) {
454 /* reg->off should be 0 for SCALAR_VALUE */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700455 verbose(env, "%lld", reg->var_off.value + reg->off);
Edward Creef1174f72017-08-07 15:26:19 +0100456 } else {
Martin KaFai Laucba368c2019-03-18 10:37:13 -0700457 verbose(env, "(id=%d", reg->id);
458 if (reg_type_may_be_refcounted_or_null(t))
459 verbose(env, ",ref_obj_id=%d", reg->ref_obj_id);
Edward Creef1174f72017-08-07 15:26:19 +0100460 if (t != SCALAR_VALUE)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700461 verbose(env, ",off=%d", reg->off);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200462 if (type_is_pkt_pointer(t))
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700463 verbose(env, ",r=%d", reg->range);
Edward Creef1174f72017-08-07 15:26:19 +0100464 else if (t == CONST_PTR_TO_MAP ||
465 t == PTR_TO_MAP_VALUE ||
466 t == PTR_TO_MAP_VALUE_OR_NULL)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700467 verbose(env, ",ks=%d,vs=%d",
Edward Creef1174f72017-08-07 15:26:19 +0100468 reg->map_ptr->key_size,
469 reg->map_ptr->value_size);
Edward Cree7d1238f2017-08-07 15:26:56 +0100470 if (tnum_is_const(reg->var_off)) {
471 /* Typically an immediate SCALAR_VALUE, but
472 * could be a pointer whose offset is too big
473 * for reg->off
474 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700475 verbose(env, ",imm=%llx", reg->var_off.value);
Edward Cree7d1238f2017-08-07 15:26:56 +0100476 } else {
477 if (reg->smin_value != reg->umin_value &&
478 reg->smin_value != S64_MIN)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700479 verbose(env, ",smin_value=%lld",
Edward Cree7d1238f2017-08-07 15:26:56 +0100480 (long long)reg->smin_value);
481 if (reg->smax_value != reg->umax_value &&
482 reg->smax_value != S64_MAX)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700483 verbose(env, ",smax_value=%lld",
Edward Cree7d1238f2017-08-07 15:26:56 +0100484 (long long)reg->smax_value);
485 if (reg->umin_value != 0)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700486 verbose(env, ",umin_value=%llu",
Edward Cree7d1238f2017-08-07 15:26:56 +0100487 (unsigned long long)reg->umin_value);
488 if (reg->umax_value != U64_MAX)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700489 verbose(env, ",umax_value=%llu",
Edward Cree7d1238f2017-08-07 15:26:56 +0100490 (unsigned long long)reg->umax_value);
491 if (!tnum_is_unknown(reg->var_off)) {
492 char tn_buf[48];
Edward Creef1174f72017-08-07 15:26:19 +0100493
Edward Cree7d1238f2017-08-07 15:26:56 +0100494 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700495 verbose(env, ",var_off=%s", tn_buf);
Edward Cree7d1238f2017-08-07 15:26:56 +0100496 }
Edward Creef1174f72017-08-07 15:26:19 +0100497 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700498 verbose(env, ")");
Edward Creef1174f72017-08-07 15:26:19 +0100499 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700500 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700501 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
Edward Cree8efea212018-08-22 20:02:44 +0100502 char types_buf[BPF_REG_SIZE + 1];
503 bool valid = false;
504 int j;
505
506 for (j = 0; j < BPF_REG_SIZE; j++) {
507 if (state->stack[i].slot_type[j] != STACK_INVALID)
508 valid = true;
509 types_buf[j] = slot_type_char[
510 state->stack[i].slot_type[j]];
511 }
512 types_buf[BPF_REG_SIZE] = 0;
513 if (!valid)
514 continue;
515 verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE);
516 print_liveness(env, state->stack[i].spilled_ptr.live);
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -0700517 if (state->stack[i].slot_type[0] == STACK_SPILL) {
518 reg = &state->stack[i].spilled_ptr;
519 t = reg->type;
520 verbose(env, "=%s", reg_type_str[t]);
521 if (t == SCALAR_VALUE && reg->precise)
522 verbose(env, "P");
523 if (t == SCALAR_VALUE && tnum_is_const(reg->var_off))
524 verbose(env, "%lld", reg->var_off.value + reg->off);
525 } else {
Edward Cree8efea212018-08-22 20:02:44 +0100526 verbose(env, "=%s", types_buf);
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -0700527 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700528 }
Joe Stringerfd978bf72018-10-02 13:35:35 -0700529 if (state->acquired_refs && state->refs[0].id) {
530 verbose(env, " refs=%d", state->refs[0].id);
531 for (i = 1; i < state->acquired_refs; i++)
532 if (state->refs[i].id)
533 verbose(env, ",%d", state->refs[i].id);
534 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700535 verbose(env, "\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700536}
537
Joe Stringer84dbf352018-10-02 13:35:34 -0700538#define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \
539static int copy_##NAME##_state(struct bpf_func_state *dst, \
540 const struct bpf_func_state *src) \
541{ \
542 if (!src->FIELD) \
543 return 0; \
544 if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \
545 /* internal bug, make state invalid to reject the program */ \
546 memset(dst, 0, sizeof(*dst)); \
547 return -EFAULT; \
548 } \
549 memcpy(dst->FIELD, src->FIELD, \
550 sizeof(*src->FIELD) * (src->COUNT / SIZE)); \
551 return 0; \
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700552}
Joe Stringerfd978bf72018-10-02 13:35:35 -0700553/* copy_reference_state() */
554COPY_STATE_FN(reference, acquired_refs, refs, 1)
Joe Stringer84dbf352018-10-02 13:35:34 -0700555/* copy_stack_state() */
556COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE)
557#undef COPY_STATE_FN
558
559#define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \
560static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \
561 bool copy_old) \
562{ \
563 u32 old_size = state->COUNT; \
564 struct bpf_##NAME##_state *new_##FIELD; \
565 int slot = size / SIZE; \
566 \
567 if (size <= old_size || !size) { \
568 if (copy_old) \
569 return 0; \
570 state->COUNT = slot * SIZE; \
571 if (!size && old_size) { \
572 kfree(state->FIELD); \
573 state->FIELD = NULL; \
574 } \
575 return 0; \
576 } \
577 new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \
578 GFP_KERNEL); \
579 if (!new_##FIELD) \
580 return -ENOMEM; \
581 if (copy_old) { \
582 if (state->FIELD) \
583 memcpy(new_##FIELD, state->FIELD, \
584 sizeof(*new_##FIELD) * (old_size / SIZE)); \
585 memset(new_##FIELD + old_size / SIZE, 0, \
586 sizeof(*new_##FIELD) * (size - old_size) / SIZE); \
587 } \
588 state->COUNT = slot * SIZE; \
589 kfree(state->FIELD); \
590 state->FIELD = new_##FIELD; \
591 return 0; \
592}
Joe Stringerfd978bf72018-10-02 13:35:35 -0700593/* realloc_reference_state() */
594REALLOC_STATE_FN(reference, acquired_refs, refs, 1)
Joe Stringer84dbf352018-10-02 13:35:34 -0700595/* realloc_stack_state() */
596REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE)
597#undef REALLOC_STATE_FN
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700598
599/* do_check() starts with zero-sized stack in struct bpf_verifier_state to
600 * make it consume minimal amount of memory. check_stack_write() access from
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800601 * the program calls into realloc_func_state() to grow the stack size.
Joe Stringer84dbf352018-10-02 13:35:34 -0700602 * Note there is a non-zero 'parent' pointer inside bpf_verifier_state
603 * which realloc_stack_state() copies over. It points to previous
604 * bpf_verifier_state which is never reallocated.
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700605 */
Joe Stringerfd978bf72018-10-02 13:35:35 -0700606static int realloc_func_state(struct bpf_func_state *state, int stack_size,
607 int refs_size, bool copy_old)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700608{
Joe Stringerfd978bf72018-10-02 13:35:35 -0700609 int err = realloc_reference_state(state, refs_size, copy_old);
610 if (err)
611 return err;
612 return realloc_stack_state(state, stack_size, copy_old);
613}
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700614
Joe Stringerfd978bf72018-10-02 13:35:35 -0700615/* Acquire a pointer id from the env and update the state->refs to include
616 * this new pointer reference.
617 * On success, returns a valid pointer id to associate with the register
618 * On failure, returns a negative errno.
619 */
620static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx)
621{
622 struct bpf_func_state *state = cur_func(env);
623 int new_ofs = state->acquired_refs;
624 int id, err;
625
626 err = realloc_reference_state(state, state->acquired_refs + 1, true);
627 if (err)
628 return err;
629 id = ++env->id_gen;
630 state->refs[new_ofs].id = id;
631 state->refs[new_ofs].insn_idx = insn_idx;
632
633 return id;
634}
635
636/* release function corresponding to acquire_reference_state(). Idempotent. */
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800637static int release_reference_state(struct bpf_func_state *state, int ptr_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -0700638{
639 int i, last_idx;
640
Joe Stringerfd978bf72018-10-02 13:35:35 -0700641 last_idx = state->acquired_refs - 1;
642 for (i = 0; i < state->acquired_refs; i++) {
643 if (state->refs[i].id == ptr_id) {
644 if (last_idx && i != last_idx)
645 memcpy(&state->refs[i], &state->refs[last_idx],
646 sizeof(*state->refs));
647 memset(&state->refs[last_idx], 0, sizeof(*state->refs));
648 state->acquired_refs--;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700649 return 0;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700650 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700651 }
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800652 return -EINVAL;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700653}
654
655static int transfer_reference_state(struct bpf_func_state *dst,
656 struct bpf_func_state *src)
657{
658 int err = realloc_reference_state(dst, src->acquired_refs, false);
659 if (err)
660 return err;
661 err = copy_reference_state(dst, src);
662 if (err)
663 return err;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700664 return 0;
665}
666
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800667static void free_func_state(struct bpf_func_state *state)
668{
Alexei Starovoitov58963512018-01-08 07:51:17 -0800669 if (!state)
670 return;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700671 kfree(state->refs);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800672 kfree(state->stack);
673 kfree(state);
674}
675
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -0700676static void clear_jmp_history(struct bpf_verifier_state *state)
677{
678 kfree(state->jmp_history);
679 state->jmp_history = NULL;
680 state->jmp_history_cnt = 0;
681}
682
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700683static void free_verifier_state(struct bpf_verifier_state *state,
684 bool free_self)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700685{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800686 int i;
687
688 for (i = 0; i <= state->curframe; i++) {
689 free_func_state(state->frame[i]);
690 state->frame[i] = NULL;
691 }
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -0700692 clear_jmp_history(state);
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700693 if (free_self)
694 kfree(state);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700695}
696
697/* copy verifier state from src to dst growing dst stack space
698 * when necessary to accommodate larger src stack
699 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800700static int copy_func_state(struct bpf_func_state *dst,
701 const struct bpf_func_state *src)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700702{
703 int err;
704
Joe Stringerfd978bf72018-10-02 13:35:35 -0700705 err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs,
706 false);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700707 if (err)
708 return err;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700709 memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs));
710 err = copy_reference_state(dst, src);
711 if (err)
712 return err;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700713 return copy_stack_state(dst, src);
714}
715
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800716static int copy_verifier_state(struct bpf_verifier_state *dst_state,
717 const struct bpf_verifier_state *src)
718{
719 struct bpf_func_state *dst;
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -0700720 u32 jmp_sz = sizeof(struct bpf_idx_pair) * src->jmp_history_cnt;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800721 int i, err;
722
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -0700723 if (dst_state->jmp_history_cnt < src->jmp_history_cnt) {
724 kfree(dst_state->jmp_history);
725 dst_state->jmp_history = kmalloc(jmp_sz, GFP_USER);
726 if (!dst_state->jmp_history)
727 return -ENOMEM;
728 }
729 memcpy(dst_state->jmp_history, src->jmp_history, jmp_sz);
730 dst_state->jmp_history_cnt = src->jmp_history_cnt;
731
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800732 /* if dst has more stack frames then src frame, free them */
733 for (i = src->curframe + 1; i <= dst_state->curframe; i++) {
734 free_func_state(dst_state->frame[i]);
735 dst_state->frame[i] = NULL;
736 }
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100737 dst_state->speculative = src->speculative;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800738 dst_state->curframe = src->curframe;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800739 dst_state->active_spin_lock = src->active_spin_lock;
Alexei Starovoitov25897262019-06-15 12:12:20 -0700740 dst_state->branches = src->branches;
741 dst_state->parent = src->parent;
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -0700742 dst_state->first_insn_idx = src->first_insn_idx;
743 dst_state->last_insn_idx = src->last_insn_idx;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800744 for (i = 0; i <= src->curframe; i++) {
745 dst = dst_state->frame[i];
746 if (!dst) {
747 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
748 if (!dst)
749 return -ENOMEM;
750 dst_state->frame[i] = dst;
751 }
752 err = copy_func_state(dst, src->frame[i]);
753 if (err)
754 return err;
755 }
756 return 0;
757}
758
Alexei Starovoitov25897262019-06-15 12:12:20 -0700759static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st)
760{
761 while (st) {
762 u32 br = --st->branches;
763
764 /* WARN_ON(br > 1) technically makes sense here,
765 * but see comment in push_stack(), hence:
766 */
767 WARN_ONCE((int)br < 0,
768 "BUG update_branch_counts:branches_to_explore=%d\n",
769 br);
770 if (br)
771 break;
772 st = st->parent;
773 }
774}
775
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700776static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx,
777 int *insn_idx)
778{
779 struct bpf_verifier_state *cur = env->cur_state;
780 struct bpf_verifier_stack_elem *elem, *head = env->head;
781 int err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700782
783 if (env->head == NULL)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700784 return -ENOENT;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700785
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700786 if (cur) {
787 err = copy_verifier_state(cur, &head->st);
788 if (err)
789 return err;
790 }
791 if (insn_idx)
792 *insn_idx = head->insn_idx;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700793 if (prev_insn_idx)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700794 *prev_insn_idx = head->prev_insn_idx;
795 elem = head->next;
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700796 free_verifier_state(&head->st, false);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700797 kfree(head);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700798 env->head = elem;
799 env->stack_size--;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700800 return 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700801}
802
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100803static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100804 int insn_idx, int prev_insn_idx,
805 bool speculative)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700806{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700807 struct bpf_verifier_state *cur = env->cur_state;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100808 struct bpf_verifier_stack_elem *elem;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700809 int err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700810
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700811 elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700812 if (!elem)
813 goto err;
814
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700815 elem->insn_idx = insn_idx;
816 elem->prev_insn_idx = prev_insn_idx;
817 elem->next = env->head;
818 env->head = elem;
819 env->stack_size++;
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700820 err = copy_verifier_state(&elem->st, cur);
821 if (err)
822 goto err;
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100823 elem->st.speculative |= speculative;
Alexei Starovoitovb285fcb2019-05-21 20:14:19 -0700824 if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) {
825 verbose(env, "The sequence of %d jumps is too complex.\n",
826 env->stack_size);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700827 goto err;
828 }
Alexei Starovoitov25897262019-06-15 12:12:20 -0700829 if (elem->st.parent) {
830 ++elem->st.parent->branches;
831 /* WARN_ON(branches > 2) technically makes sense here,
832 * but
833 * 1. speculative states will bump 'branches' for non-branch
834 * instructions
835 * 2. is_state_visited() heuristics may decide not to create
836 * a new state for a sequence of branches and all such current
837 * and cloned states will be pointing to a single parent state
838 * which might have large 'branches' count.
839 */
840 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700841 return &elem->st;
842err:
Alexei Starovoitov58963512018-01-08 07:51:17 -0800843 free_verifier_state(env->cur_state, true);
844 env->cur_state = NULL;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700845 /* pop all elements and return */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700846 while (!pop_stack(env, NULL, NULL));
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700847 return NULL;
848}
849
850#define CALLER_SAVED_REGS 6
851static const int caller_saved[CALLER_SAVED_REGS] = {
852 BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
853};
854
Edward Creef1174f72017-08-07 15:26:19 +0100855static void __mark_reg_not_init(struct bpf_reg_state *reg);
856
Edward Creeb03c9f92017-08-07 15:26:36 +0100857/* Mark the unknown part of a register (variable offset or scalar value) as
858 * known to have the value @imm.
859 */
860static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
861{
Alexei Starovoitova9c676b2018-09-04 19:13:44 -0700862 /* Clear id, off, and union(map_ptr, range) */
863 memset(((u8 *)reg) + sizeof(reg->type), 0,
864 offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type));
Edward Creeb03c9f92017-08-07 15:26:36 +0100865 reg->var_off = tnum_const(imm);
866 reg->smin_value = (s64)imm;
867 reg->smax_value = (s64)imm;
868 reg->umin_value = imm;
869 reg->umax_value = imm;
870}
871
Edward Creef1174f72017-08-07 15:26:19 +0100872/* Mark the 'variable offset' part of a register as zero. This should be
873 * used only on registers holding a pointer type.
874 */
875static void __mark_reg_known_zero(struct bpf_reg_state *reg)
876{
Edward Creeb03c9f92017-08-07 15:26:36 +0100877 __mark_reg_known(reg, 0);
Edward Creef1174f72017-08-07 15:26:19 +0100878}
879
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800880static void __mark_reg_const_zero(struct bpf_reg_state *reg)
881{
882 __mark_reg_known(reg, 0);
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800883 reg->type = SCALAR_VALUE;
884}
885
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700886static void mark_reg_known_zero(struct bpf_verifier_env *env,
887 struct bpf_reg_state *regs, u32 regno)
Edward Creef1174f72017-08-07 15:26:19 +0100888{
889 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700890 verbose(env, "mark_reg_known_zero(regs, %u)\n", regno);
Edward Creef1174f72017-08-07 15:26:19 +0100891 /* Something bad happened, let's kill all regs */
892 for (regno = 0; regno < MAX_BPF_REG; regno++)
893 __mark_reg_not_init(regs + regno);
894 return;
895 }
896 __mark_reg_known_zero(regs + regno);
897}
898
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200899static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg)
900{
901 return type_is_pkt_pointer(reg->type);
902}
903
904static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg)
905{
906 return reg_is_pkt_pointer(reg) ||
907 reg->type == PTR_TO_PACKET_END;
908}
909
910/* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */
911static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg,
912 enum bpf_reg_type which)
913{
914 /* The register can already have a range from prior markings.
915 * This is fine as long as it hasn't been advanced from its
916 * origin.
917 */
918 return reg->type == which &&
919 reg->id == 0 &&
920 reg->off == 0 &&
921 tnum_equals_const(reg->var_off, 0);
922}
923
Edward Creeb03c9f92017-08-07 15:26:36 +0100924/* Attempts to improve min/max values based on var_off information */
925static void __update_reg_bounds(struct bpf_reg_state *reg)
926{
927 /* min signed is max(sign bit) | min(other bits) */
928 reg->smin_value = max_t(s64, reg->smin_value,
929 reg->var_off.value | (reg->var_off.mask & S64_MIN));
930 /* max signed is min(sign bit) | max(other bits) */
931 reg->smax_value = min_t(s64, reg->smax_value,
932 reg->var_off.value | (reg->var_off.mask & S64_MAX));
933 reg->umin_value = max(reg->umin_value, reg->var_off.value);
934 reg->umax_value = min(reg->umax_value,
935 reg->var_off.value | reg->var_off.mask);
936}
937
938/* Uses signed min/max values to inform unsigned, and vice-versa */
939static void __reg_deduce_bounds(struct bpf_reg_state *reg)
940{
941 /* Learn sign from signed bounds.
942 * If we cannot cross the sign boundary, then signed and unsigned bounds
943 * are the same, so combine. This works even in the negative case, e.g.
944 * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff.
945 */
946 if (reg->smin_value >= 0 || reg->smax_value < 0) {
947 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
948 reg->umin_value);
949 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
950 reg->umax_value);
951 return;
952 }
953 /* Learn sign from unsigned bounds. Signed bounds cross the sign
954 * boundary, so we must be careful.
955 */
956 if ((s64)reg->umax_value >= 0) {
957 /* Positive. We can't learn anything from the smin, but smax
958 * is positive, hence safe.
959 */
960 reg->smin_value = reg->umin_value;
961 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
962 reg->umax_value);
963 } else if ((s64)reg->umin_value < 0) {
964 /* Negative. We can't learn anything from the smax, but smin
965 * is negative, hence safe.
966 */
967 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
968 reg->umin_value);
969 reg->smax_value = reg->umax_value;
970 }
971}
972
973/* Attempts to improve var_off based on unsigned min/max information */
974static void __reg_bound_offset(struct bpf_reg_state *reg)
975{
976 reg->var_off = tnum_intersect(reg->var_off,
977 tnum_range(reg->umin_value,
978 reg->umax_value));
979}
980
981/* Reset the min/max bounds of a register */
982static void __mark_reg_unbounded(struct bpf_reg_state *reg)
983{
984 reg->smin_value = S64_MIN;
985 reg->smax_value = S64_MAX;
986 reg->umin_value = 0;
987 reg->umax_value = U64_MAX;
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -0700988
989 /* constant backtracking is enabled for root only for now */
990 reg->precise = capable(CAP_SYS_ADMIN) ? false : true;
Edward Creeb03c9f92017-08-07 15:26:36 +0100991}
992
Edward Creef1174f72017-08-07 15:26:19 +0100993/* Mark a register as having a completely unknown (scalar) value. */
994static void __mark_reg_unknown(struct bpf_reg_state *reg)
995{
Alexei Starovoitova9c676b2018-09-04 19:13:44 -0700996 /*
997 * Clear type, id, off, and union(map_ptr, range) and
998 * padding between 'type' and union
999 */
1000 memset(reg, 0, offsetof(struct bpf_reg_state, var_off));
Edward Creef1174f72017-08-07 15:26:19 +01001001 reg->type = SCALAR_VALUE;
Edward Creef1174f72017-08-07 15:26:19 +01001002 reg->var_off = tnum_unknown;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001003 reg->frameno = 0;
Edward Creeb03c9f92017-08-07 15:26:36 +01001004 __mark_reg_unbounded(reg);
Edward Creef1174f72017-08-07 15:26:19 +01001005}
1006
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001007static void mark_reg_unknown(struct bpf_verifier_env *env,
1008 struct bpf_reg_state *regs, u32 regno)
Edward Creef1174f72017-08-07 15:26:19 +01001009{
1010 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001011 verbose(env, "mark_reg_unknown(regs, %u)\n", regno);
Alexei Starovoitov19ceb412017-11-30 21:31:37 -08001012 /* Something bad happened, let's kill all regs except FP */
1013 for (regno = 0; regno < BPF_REG_FP; regno++)
Edward Creef1174f72017-08-07 15:26:19 +01001014 __mark_reg_not_init(regs + regno);
1015 return;
1016 }
1017 __mark_reg_unknown(regs + regno);
1018}
1019
1020static void __mark_reg_not_init(struct bpf_reg_state *reg)
1021{
1022 __mark_reg_unknown(reg);
1023 reg->type = NOT_INIT;
1024}
1025
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001026static void mark_reg_not_init(struct bpf_verifier_env *env,
1027 struct bpf_reg_state *regs, u32 regno)
Daniel Borkmanna9789ef2017-05-25 01:05:06 +02001028{
Edward Creef1174f72017-08-07 15:26:19 +01001029 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001030 verbose(env, "mark_reg_not_init(regs, %u)\n", regno);
Alexei Starovoitov19ceb412017-11-30 21:31:37 -08001031 /* Something bad happened, let's kill all regs except FP */
1032 for (regno = 0; regno < BPF_REG_FP; regno++)
Edward Creef1174f72017-08-07 15:26:19 +01001033 __mark_reg_not_init(regs + regno);
1034 return;
1035 }
1036 __mark_reg_not_init(regs + regno);
Daniel Borkmanna9789ef2017-05-25 01:05:06 +02001037}
1038
Jiong Wang5327ed32019-05-24 23:25:12 +01001039#define DEF_NOT_SUBREG (0)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001040static void init_reg_state(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001041 struct bpf_func_state *state)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001042{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001043 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001044 int i;
1045
Edward Creedc503a82017-08-15 20:34:35 +01001046 for (i = 0; i < MAX_BPF_REG; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001047 mark_reg_not_init(env, regs, i);
Edward Creedc503a82017-08-15 20:34:35 +01001048 regs[i].live = REG_LIVE_NONE;
Edward Cree679c7822018-08-22 20:02:19 +01001049 regs[i].parent = NULL;
Jiong Wang5327ed32019-05-24 23:25:12 +01001050 regs[i].subreg_def = DEF_NOT_SUBREG;
Edward Creedc503a82017-08-15 20:34:35 +01001051 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001052
1053 /* frame pointer */
Edward Creef1174f72017-08-07 15:26:19 +01001054 regs[BPF_REG_FP].type = PTR_TO_STACK;
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001055 mark_reg_known_zero(env, regs, BPF_REG_FP);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001056 regs[BPF_REG_FP].frameno = state->frameno;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001057
1058 /* 1st arg to a function */
1059 regs[BPF_REG_1].type = PTR_TO_CTX;
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001060 mark_reg_known_zero(env, regs, BPF_REG_1);
Daniel Borkmann6760bf22016-12-18 01:52:59 +01001061}
1062
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001063#define BPF_MAIN_FUNC (-1)
1064static void init_func_state(struct bpf_verifier_env *env,
1065 struct bpf_func_state *state,
1066 int callsite, int frameno, int subprogno)
1067{
1068 state->callsite = callsite;
1069 state->frameno = frameno;
1070 state->subprogno = subprogno;
1071 init_reg_state(env, state);
1072}
1073
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001074enum reg_arg_type {
1075 SRC_OP, /* register is used as source operand */
1076 DST_OP, /* register is used as destination operand */
1077 DST_OP_NO_MARK /* same as above, check only, don't mark */
1078};
1079
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001080static int cmp_subprogs(const void *a, const void *b)
1081{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001082 return ((struct bpf_subprog_info *)a)->start -
1083 ((struct bpf_subprog_info *)b)->start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001084}
1085
1086static int find_subprog(struct bpf_verifier_env *env, int off)
1087{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001088 struct bpf_subprog_info *p;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001089
Jiong Wang9c8105b2018-05-02 16:17:18 -04001090 p = bsearch(&off, env->subprog_info, env->subprog_cnt,
1091 sizeof(env->subprog_info[0]), cmp_subprogs);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001092 if (!p)
1093 return -ENOENT;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001094 return p - env->subprog_info;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001095
1096}
1097
1098static int add_subprog(struct bpf_verifier_env *env, int off)
1099{
1100 int insn_cnt = env->prog->len;
1101 int ret;
1102
1103 if (off >= insn_cnt || off < 0) {
1104 verbose(env, "call to invalid destination\n");
1105 return -EINVAL;
1106 }
1107 ret = find_subprog(env, off);
1108 if (ret >= 0)
1109 return 0;
Jiong Wang4cb3d992018-05-02 16:17:19 -04001110 if (env->subprog_cnt >= BPF_MAX_SUBPROGS) {
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001111 verbose(env, "too many subprograms\n");
1112 return -E2BIG;
1113 }
Jiong Wang9c8105b2018-05-02 16:17:18 -04001114 env->subprog_info[env->subprog_cnt++].start = off;
1115 sort(env->subprog_info, env->subprog_cnt,
1116 sizeof(env->subprog_info[0]), cmp_subprogs, NULL);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001117 return 0;
1118}
1119
1120static int check_subprogs(struct bpf_verifier_env *env)
1121{
1122 int i, ret, subprog_start, subprog_end, off, cur_subprog = 0;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001123 struct bpf_subprog_info *subprog = env->subprog_info;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001124 struct bpf_insn *insn = env->prog->insnsi;
1125 int insn_cnt = env->prog->len;
1126
Jiong Wangf910cef2018-05-02 16:17:17 -04001127 /* Add entry function. */
1128 ret = add_subprog(env, 0);
1129 if (ret < 0)
1130 return ret;
1131
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001132 /* determine subprog starts. The end is one before the next starts */
1133 for (i = 0; i < insn_cnt; i++) {
1134 if (insn[i].code != (BPF_JMP | BPF_CALL))
1135 continue;
1136 if (insn[i].src_reg != BPF_PSEUDO_CALL)
1137 continue;
1138 if (!env->allow_ptr_leaks) {
1139 verbose(env, "function calls to other bpf functions are allowed for root only\n");
1140 return -EPERM;
1141 }
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001142 ret = add_subprog(env, i + insn[i].imm + 1);
1143 if (ret < 0)
1144 return ret;
1145 }
1146
Jiong Wang4cb3d992018-05-02 16:17:19 -04001147 /* Add a fake 'exit' subprog which could simplify subprog iteration
1148 * logic. 'subprog_cnt' should not be increased.
1149 */
1150 subprog[env->subprog_cnt].start = insn_cnt;
1151
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07001152 if (env->log.level & BPF_LOG_LEVEL2)
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001153 for (i = 0; i < env->subprog_cnt; i++)
Jiong Wang9c8105b2018-05-02 16:17:18 -04001154 verbose(env, "func#%d @%d\n", i, subprog[i].start);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001155
1156 /* now check that all jumps are within the same subprog */
Jiong Wang4cb3d992018-05-02 16:17:19 -04001157 subprog_start = subprog[cur_subprog].start;
1158 subprog_end = subprog[cur_subprog + 1].start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001159 for (i = 0; i < insn_cnt; i++) {
1160 u8 code = insn[i].code;
1161
Jiong Wang092ed092019-01-26 12:26:01 -05001162 if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32)
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001163 goto next;
1164 if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL)
1165 goto next;
1166 off = i + insn[i].off + 1;
1167 if (off < subprog_start || off >= subprog_end) {
1168 verbose(env, "jump out of range from insn %d to %d\n", i, off);
1169 return -EINVAL;
1170 }
1171next:
1172 if (i == subprog_end - 1) {
1173 /* to avoid fall-through from one subprog into another
1174 * the last insn of the subprog should be either exit
1175 * or unconditional jump back
1176 */
1177 if (code != (BPF_JMP | BPF_EXIT) &&
1178 code != (BPF_JMP | BPF_JA)) {
1179 verbose(env, "last insn is not an exit or jmp\n");
1180 return -EINVAL;
1181 }
1182 subprog_start = subprog_end;
Jiong Wang4cb3d992018-05-02 16:17:19 -04001183 cur_subprog++;
1184 if (cur_subprog < env->subprog_cnt)
Jiong Wang9c8105b2018-05-02 16:17:18 -04001185 subprog_end = subprog[cur_subprog + 1].start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001186 }
1187 }
1188 return 0;
1189}
1190
Edward Cree679c7822018-08-22 20:02:19 +01001191/* Parentage chain of this register (or stack slot) should take care of all
1192 * issues like callee-saved registers, stack slot allocation time, etc.
1193 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001194static int mark_reg_read(struct bpf_verifier_env *env,
Edward Cree679c7822018-08-22 20:02:19 +01001195 const struct bpf_reg_state *state,
Jiong Wang5327ed32019-05-24 23:25:12 +01001196 struct bpf_reg_state *parent, u8 flag)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001197{
1198 bool writes = parent == state->parent; /* Observe write marks */
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07001199 int cnt = 0;
Edward Creedc503a82017-08-15 20:34:35 +01001200
1201 while (parent) {
1202 /* if read wasn't screened by an earlier write ... */
Edward Cree679c7822018-08-22 20:02:19 +01001203 if (writes && state->live & REG_LIVE_WRITTEN)
Edward Creedc503a82017-08-15 20:34:35 +01001204 break;
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -08001205 if (parent->live & REG_LIVE_DONE) {
1206 verbose(env, "verifier BUG type %s var_off %lld off %d\n",
1207 reg_type_str[parent->type],
1208 parent->var_off.value, parent->off);
1209 return -EFAULT;
1210 }
Jiong Wang5327ed32019-05-24 23:25:12 +01001211 /* The first condition is more likely to be true than the
1212 * second, checked it first.
1213 */
1214 if ((parent->live & REG_LIVE_READ) == flag ||
1215 parent->live & REG_LIVE_READ64)
Alexei Starovoitov25af32d2019-04-01 21:27:42 -07001216 /* The parentage chain never changes and
1217 * this parent was already marked as LIVE_READ.
1218 * There is no need to keep walking the chain again and
1219 * keep re-marking all parents as LIVE_READ.
1220 * This case happens when the same register is read
1221 * multiple times without writes into it in-between.
Jiong Wang5327ed32019-05-24 23:25:12 +01001222 * Also, if parent has the stronger REG_LIVE_READ64 set,
1223 * then no need to set the weak REG_LIVE_READ32.
Alexei Starovoitov25af32d2019-04-01 21:27:42 -07001224 */
1225 break;
Edward Creedc503a82017-08-15 20:34:35 +01001226 /* ... then we depend on parent's value */
Jiong Wang5327ed32019-05-24 23:25:12 +01001227 parent->live |= flag;
1228 /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */
1229 if (flag == REG_LIVE_READ64)
1230 parent->live &= ~REG_LIVE_READ32;
Edward Creedc503a82017-08-15 20:34:35 +01001231 state = parent;
1232 parent = state->parent;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001233 writes = true;
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07001234 cnt++;
Edward Creedc503a82017-08-15 20:34:35 +01001235 }
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07001236
1237 if (env->longest_mark_read_walk < cnt)
1238 env->longest_mark_read_walk = cnt;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001239 return 0;
Edward Creedc503a82017-08-15 20:34:35 +01001240}
1241
Jiong Wang5327ed32019-05-24 23:25:12 +01001242/* This function is supposed to be used by the following 32-bit optimization
1243 * code only. It returns TRUE if the source or destination register operates
1244 * on 64-bit, otherwise return FALSE.
1245 */
1246static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn,
1247 u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t)
1248{
1249 u8 code, class, op;
1250
1251 code = insn->code;
1252 class = BPF_CLASS(code);
1253 op = BPF_OP(code);
1254 if (class == BPF_JMP) {
1255 /* BPF_EXIT for "main" will reach here. Return TRUE
1256 * conservatively.
1257 */
1258 if (op == BPF_EXIT)
1259 return true;
1260 if (op == BPF_CALL) {
1261 /* BPF to BPF call will reach here because of marking
1262 * caller saved clobber with DST_OP_NO_MARK for which we
1263 * don't care the register def because they are anyway
1264 * marked as NOT_INIT already.
1265 */
1266 if (insn->src_reg == BPF_PSEUDO_CALL)
1267 return false;
1268 /* Helper call will reach here because of arg type
1269 * check, conservatively return TRUE.
1270 */
1271 if (t == SRC_OP)
1272 return true;
1273
1274 return false;
1275 }
1276 }
1277
1278 if (class == BPF_ALU64 || class == BPF_JMP ||
1279 /* BPF_END always use BPF_ALU class. */
1280 (class == BPF_ALU && op == BPF_END && insn->imm == 64))
1281 return true;
1282
1283 if (class == BPF_ALU || class == BPF_JMP32)
1284 return false;
1285
1286 if (class == BPF_LDX) {
1287 if (t != SRC_OP)
1288 return BPF_SIZE(code) == BPF_DW;
1289 /* LDX source must be ptr. */
1290 return true;
1291 }
1292
1293 if (class == BPF_STX) {
1294 if (reg->type != SCALAR_VALUE)
1295 return true;
1296 return BPF_SIZE(code) == BPF_DW;
1297 }
1298
1299 if (class == BPF_LD) {
1300 u8 mode = BPF_MODE(code);
1301
1302 /* LD_IMM64 */
1303 if (mode == BPF_IMM)
1304 return true;
1305
1306 /* Both LD_IND and LD_ABS return 32-bit data. */
1307 if (t != SRC_OP)
1308 return false;
1309
1310 /* Implicit ctx ptr. */
1311 if (regno == BPF_REG_6)
1312 return true;
1313
1314 /* Explicit source could be any width. */
1315 return true;
1316 }
1317
1318 if (class == BPF_ST)
1319 /* The only source register for BPF_ST is a ptr. */
1320 return true;
1321
1322 /* Conservatively return true at default. */
1323 return true;
1324}
1325
Jiong Wangb325fbc2019-05-24 23:25:13 +01001326/* Return TRUE if INSN doesn't have explicit value define. */
1327static bool insn_no_def(struct bpf_insn *insn)
1328{
1329 u8 class = BPF_CLASS(insn->code);
1330
1331 return (class == BPF_JMP || class == BPF_JMP32 ||
1332 class == BPF_STX || class == BPF_ST);
1333}
1334
1335/* Return TRUE if INSN has defined any 32-bit value explicitly. */
1336static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn)
1337{
1338 if (insn_no_def(insn))
1339 return false;
1340
1341 return !is_reg64(env, insn, insn->dst_reg, NULL, DST_OP);
1342}
1343
Jiong Wang5327ed32019-05-24 23:25:12 +01001344static void mark_insn_zext(struct bpf_verifier_env *env,
1345 struct bpf_reg_state *reg)
1346{
1347 s32 def_idx = reg->subreg_def;
1348
1349 if (def_idx == DEF_NOT_SUBREG)
1350 return;
1351
1352 env->insn_aux_data[def_idx - 1].zext_dst = true;
1353 /* The dst will be zero extended, so won't be sub-register anymore. */
1354 reg->subreg_def = DEF_NOT_SUBREG;
1355}
1356
Edward Creedc503a82017-08-15 20:34:35 +01001357static int check_reg_arg(struct bpf_verifier_env *env, u32 regno,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001358 enum reg_arg_type t)
1359{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001360 struct bpf_verifier_state *vstate = env->cur_state;
1361 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Jiong Wang5327ed32019-05-24 23:25:12 +01001362 struct bpf_insn *insn = env->prog->insnsi + env->insn_idx;
Jiong Wangc342dc12019-04-12 22:59:37 +01001363 struct bpf_reg_state *reg, *regs = state->regs;
Jiong Wang5327ed32019-05-24 23:25:12 +01001364 bool rw64;
Edward Creedc503a82017-08-15 20:34:35 +01001365
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001366 if (regno >= MAX_BPF_REG) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001367 verbose(env, "R%d is invalid\n", regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001368 return -EINVAL;
1369 }
1370
Jiong Wangc342dc12019-04-12 22:59:37 +01001371 reg = &regs[regno];
Jiong Wang5327ed32019-05-24 23:25:12 +01001372 rw64 = is_reg64(env, insn, regno, reg, t);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001373 if (t == SRC_OP) {
1374 /* check whether register used as source operand can be read */
Jiong Wangc342dc12019-04-12 22:59:37 +01001375 if (reg->type == NOT_INIT) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001376 verbose(env, "R%d !read_ok\n", regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001377 return -EACCES;
1378 }
Edward Cree679c7822018-08-22 20:02:19 +01001379 /* We don't need to worry about FP liveness because it's read-only */
Jiong Wangc342dc12019-04-12 22:59:37 +01001380 if (regno == BPF_REG_FP)
1381 return 0;
1382
Jiong Wang5327ed32019-05-24 23:25:12 +01001383 if (rw64)
1384 mark_insn_zext(env, reg);
1385
1386 return mark_reg_read(env, reg, reg->parent,
1387 rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001388 } else {
1389 /* check whether register used as dest operand can be written to */
1390 if (regno == BPF_REG_FP) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001391 verbose(env, "frame pointer is read only\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001392 return -EACCES;
1393 }
Jiong Wangc342dc12019-04-12 22:59:37 +01001394 reg->live |= REG_LIVE_WRITTEN;
Jiong Wang5327ed32019-05-24 23:25:12 +01001395 reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001396 if (t == DST_OP)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001397 mark_reg_unknown(env, regs, regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001398 }
1399 return 0;
1400}
1401
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001402/* for any branch, call, exit record the history of jmps in the given state */
1403static int push_jmp_history(struct bpf_verifier_env *env,
1404 struct bpf_verifier_state *cur)
1405{
1406 u32 cnt = cur->jmp_history_cnt;
1407 struct bpf_idx_pair *p;
1408
1409 cnt++;
1410 p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER);
1411 if (!p)
1412 return -ENOMEM;
1413 p[cnt - 1].idx = env->insn_idx;
1414 p[cnt - 1].prev_idx = env->prev_insn_idx;
1415 cur->jmp_history = p;
1416 cur->jmp_history_cnt = cnt;
1417 return 0;
1418}
1419
1420/* Backtrack one insn at a time. If idx is not at the top of recorded
1421 * history then previous instruction came from straight line execution.
1422 */
1423static int get_prev_insn_idx(struct bpf_verifier_state *st, int i,
1424 u32 *history)
1425{
1426 u32 cnt = *history;
1427
1428 if (cnt && st->jmp_history[cnt - 1].idx == i) {
1429 i = st->jmp_history[cnt - 1].prev_idx;
1430 (*history)--;
1431 } else {
1432 i--;
1433 }
1434 return i;
1435}
1436
1437/* For given verifier state backtrack_insn() is called from the last insn to
1438 * the first insn. Its purpose is to compute a bitmask of registers and
1439 * stack slots that needs precision in the parent verifier state.
1440 */
1441static int backtrack_insn(struct bpf_verifier_env *env, int idx,
1442 u32 *reg_mask, u64 *stack_mask)
1443{
1444 const struct bpf_insn_cbs cbs = {
1445 .cb_print = verbose,
1446 .private_data = env,
1447 };
1448 struct bpf_insn *insn = env->prog->insnsi + idx;
1449 u8 class = BPF_CLASS(insn->code);
1450 u8 opcode = BPF_OP(insn->code);
1451 u8 mode = BPF_MODE(insn->code);
1452 u32 dreg = 1u << insn->dst_reg;
1453 u32 sreg = 1u << insn->src_reg;
1454 u32 spi;
1455
1456 if (insn->code == 0)
1457 return 0;
1458 if (env->log.level & BPF_LOG_LEVEL) {
1459 verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask);
1460 verbose(env, "%d: ", idx);
1461 print_bpf_insn(&cbs, insn, env->allow_ptr_leaks);
1462 }
1463
1464 if (class == BPF_ALU || class == BPF_ALU64) {
1465 if (!(*reg_mask & dreg))
1466 return 0;
1467 if (opcode == BPF_MOV) {
1468 if (BPF_SRC(insn->code) == BPF_X) {
1469 /* dreg = sreg
1470 * dreg needs precision after this insn
1471 * sreg needs precision before this insn
1472 */
1473 *reg_mask &= ~dreg;
1474 *reg_mask |= sreg;
1475 } else {
1476 /* dreg = K
1477 * dreg needs precision after this insn.
1478 * Corresponding register is already marked
1479 * as precise=true in this verifier state.
1480 * No further markings in parent are necessary
1481 */
1482 *reg_mask &= ~dreg;
1483 }
1484 } else {
1485 if (BPF_SRC(insn->code) == BPF_X) {
1486 /* dreg += sreg
1487 * both dreg and sreg need precision
1488 * before this insn
1489 */
1490 *reg_mask |= sreg;
1491 } /* else dreg += K
1492 * dreg still needs precision before this insn
1493 */
1494 }
1495 } else if (class == BPF_LDX) {
1496 if (!(*reg_mask & dreg))
1497 return 0;
1498 *reg_mask &= ~dreg;
1499
1500 /* scalars can only be spilled into stack w/o losing precision.
1501 * Load from any other memory can be zero extended.
1502 * The desire to keep that precision is already indicated
1503 * by 'precise' mark in corresponding register of this state.
1504 * No further tracking necessary.
1505 */
1506 if (insn->src_reg != BPF_REG_FP)
1507 return 0;
1508 if (BPF_SIZE(insn->code) != BPF_DW)
1509 return 0;
1510
1511 /* dreg = *(u64 *)[fp - off] was a fill from the stack.
1512 * that [fp - off] slot contains scalar that needs to be
1513 * tracked with precision
1514 */
1515 spi = (-insn->off - 1) / BPF_REG_SIZE;
1516 if (spi >= 64) {
1517 verbose(env, "BUG spi %d\n", spi);
1518 WARN_ONCE(1, "verifier backtracking bug");
1519 return -EFAULT;
1520 }
1521 *stack_mask |= 1ull << spi;
Andrii Nakryikob3b50f02019-07-08 20:32:44 -07001522 } else if (class == BPF_STX || class == BPF_ST) {
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001523 if (*reg_mask & dreg)
Andrii Nakryikob3b50f02019-07-08 20:32:44 -07001524 /* stx & st shouldn't be using _scalar_ dst_reg
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001525 * to access memory. It means backtracking
1526 * encountered a case of pointer subtraction.
1527 */
1528 return -ENOTSUPP;
1529 /* scalars can only be spilled into stack */
1530 if (insn->dst_reg != BPF_REG_FP)
1531 return 0;
1532 if (BPF_SIZE(insn->code) != BPF_DW)
1533 return 0;
1534 spi = (-insn->off - 1) / BPF_REG_SIZE;
1535 if (spi >= 64) {
1536 verbose(env, "BUG spi %d\n", spi);
1537 WARN_ONCE(1, "verifier backtracking bug");
1538 return -EFAULT;
1539 }
1540 if (!(*stack_mask & (1ull << spi)))
1541 return 0;
1542 *stack_mask &= ~(1ull << spi);
Andrii Nakryikob3b50f02019-07-08 20:32:44 -07001543 if (class == BPF_STX)
1544 *reg_mask |= sreg;
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001545 } else if (class == BPF_JMP || class == BPF_JMP32) {
1546 if (opcode == BPF_CALL) {
1547 if (insn->src_reg == BPF_PSEUDO_CALL)
1548 return -ENOTSUPP;
1549 /* regular helper call sets R0 */
1550 *reg_mask &= ~1;
1551 if (*reg_mask & 0x3f) {
1552 /* if backtracing was looking for registers R1-R5
1553 * they should have been found already.
1554 */
1555 verbose(env, "BUG regs %x\n", *reg_mask);
1556 WARN_ONCE(1, "verifier backtracking bug");
1557 return -EFAULT;
1558 }
1559 } else if (opcode == BPF_EXIT) {
1560 return -ENOTSUPP;
1561 }
1562 } else if (class == BPF_LD) {
1563 if (!(*reg_mask & dreg))
1564 return 0;
1565 *reg_mask &= ~dreg;
1566 /* It's ld_imm64 or ld_abs or ld_ind.
1567 * For ld_imm64 no further tracking of precision
1568 * into parent is necessary
1569 */
1570 if (mode == BPF_IND || mode == BPF_ABS)
1571 /* to be analyzed */
1572 return -ENOTSUPP;
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001573 }
1574 return 0;
1575}
1576
1577/* the scalar precision tracking algorithm:
1578 * . at the start all registers have precise=false.
1579 * . scalar ranges are tracked as normal through alu and jmp insns.
1580 * . once precise value of the scalar register is used in:
1581 * . ptr + scalar alu
1582 * . if (scalar cond K|scalar)
1583 * . helper_call(.., scalar, ...) where ARG_CONST is expected
1584 * backtrack through the verifier states and mark all registers and
1585 * stack slots with spilled constants that these scalar regisers
1586 * should be precise.
1587 * . during state pruning two registers (or spilled stack slots)
1588 * are equivalent if both are not precise.
1589 *
1590 * Note the verifier cannot simply walk register parentage chain,
1591 * since many different registers and stack slots could have been
1592 * used to compute single precise scalar.
1593 *
1594 * The approach of starting with precise=true for all registers and then
1595 * backtrack to mark a register as not precise when the verifier detects
1596 * that program doesn't care about specific value (e.g., when helper
1597 * takes register as ARG_ANYTHING parameter) is not safe.
1598 *
1599 * It's ok to walk single parentage chain of the verifier states.
1600 * It's possible that this backtracking will go all the way till 1st insn.
1601 * All other branches will be explored for needing precision later.
1602 *
1603 * The backtracking needs to deal with cases like:
1604 * R8=map_value(id=0,off=0,ks=4,vs=1952,imm=0) R9_w=map_value(id=0,off=40,ks=4,vs=1952,imm=0)
1605 * r9 -= r8
1606 * r5 = r9
1607 * if r5 > 0x79f goto pc+7
1608 * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff))
1609 * r5 += 1
1610 * ...
1611 * call bpf_perf_event_output#25
1612 * where .arg5_type = ARG_CONST_SIZE_OR_ZERO
1613 *
1614 * and this case:
1615 * r6 = 1
1616 * call foo // uses callee's r6 inside to compute r0
1617 * r0 += r6
1618 * if r0 == 0 goto
1619 *
1620 * to track above reg_mask/stack_mask needs to be independent for each frame.
1621 *
1622 * Also if parent's curframe > frame where backtracking started,
1623 * the verifier need to mark registers in both frames, otherwise callees
1624 * may incorrectly prune callers. This is similar to
1625 * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences")
1626 *
1627 * For now backtracking falls back into conservative marking.
1628 */
1629static void mark_all_scalars_precise(struct bpf_verifier_env *env,
1630 struct bpf_verifier_state *st)
1631{
1632 struct bpf_func_state *func;
1633 struct bpf_reg_state *reg;
1634 int i, j;
1635
1636 /* big hammer: mark all scalars precise in this path.
1637 * pop_stack may still get !precise scalars.
1638 */
1639 for (; st; st = st->parent)
1640 for (i = 0; i <= st->curframe; i++) {
1641 func = st->frame[i];
1642 for (j = 0; j < BPF_REG_FP; j++) {
1643 reg = &func->regs[j];
1644 if (reg->type != SCALAR_VALUE)
1645 continue;
1646 reg->precise = true;
1647 }
1648 for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) {
1649 if (func->stack[j].slot_type[0] != STACK_SPILL)
1650 continue;
1651 reg = &func->stack[j].spilled_ptr;
1652 if (reg->type != SCALAR_VALUE)
1653 continue;
1654 reg->precise = true;
1655 }
1656 }
1657}
1658
Alexei Starovoitova3ce6852019-06-28 09:24:09 -07001659static int __mark_chain_precision(struct bpf_verifier_env *env, int regno,
1660 int spi)
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001661{
1662 struct bpf_verifier_state *st = env->cur_state;
1663 int first_idx = st->first_insn_idx;
1664 int last_idx = env->insn_idx;
1665 struct bpf_func_state *func;
1666 struct bpf_reg_state *reg;
Alexei Starovoitova3ce6852019-06-28 09:24:09 -07001667 u32 reg_mask = regno >= 0 ? 1u << regno : 0;
1668 u64 stack_mask = spi >= 0 ? 1ull << spi : 0;
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001669 bool skip_first = true;
Alexei Starovoitova3ce6852019-06-28 09:24:09 -07001670 bool new_marks = false;
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001671 int i, err;
1672
1673 if (!env->allow_ptr_leaks)
1674 /* backtracking is root only for now */
1675 return 0;
1676
1677 func = st->frame[st->curframe];
Alexei Starovoitova3ce6852019-06-28 09:24:09 -07001678 if (regno >= 0) {
1679 reg = &func->regs[regno];
1680 if (reg->type != SCALAR_VALUE) {
1681 WARN_ONCE(1, "backtracing misuse");
1682 return -EFAULT;
1683 }
1684 if (!reg->precise)
1685 new_marks = true;
1686 else
1687 reg_mask = 0;
1688 reg->precise = true;
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001689 }
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001690
Alexei Starovoitova3ce6852019-06-28 09:24:09 -07001691 while (spi >= 0) {
1692 if (func->stack[spi].slot_type[0] != STACK_SPILL) {
1693 stack_mask = 0;
1694 break;
1695 }
1696 reg = &func->stack[spi].spilled_ptr;
1697 if (reg->type != SCALAR_VALUE) {
1698 stack_mask = 0;
1699 break;
1700 }
1701 if (!reg->precise)
1702 new_marks = true;
1703 else
1704 stack_mask = 0;
1705 reg->precise = true;
1706 break;
1707 }
1708
1709 if (!new_marks)
1710 return 0;
1711 if (!reg_mask && !stack_mask)
1712 return 0;
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001713 for (;;) {
1714 DECLARE_BITMAP(mask, 64);
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001715 u32 history = st->jmp_history_cnt;
1716
1717 if (env->log.level & BPF_LOG_LEVEL)
1718 verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx);
1719 for (i = last_idx;;) {
1720 if (skip_first) {
1721 err = 0;
1722 skip_first = false;
1723 } else {
1724 err = backtrack_insn(env, i, &reg_mask, &stack_mask);
1725 }
1726 if (err == -ENOTSUPP) {
1727 mark_all_scalars_precise(env, st);
1728 return 0;
1729 } else if (err) {
1730 return err;
1731 }
1732 if (!reg_mask && !stack_mask)
1733 /* Found assignment(s) into tracked register in this state.
1734 * Since this state is already marked, just return.
1735 * Nothing to be tracked further in the parent state.
1736 */
1737 return 0;
1738 if (i == first_idx)
1739 break;
1740 i = get_prev_insn_idx(st, i, &history);
1741 if (i >= env->prog->len) {
1742 /* This can happen if backtracking reached insn 0
1743 * and there are still reg_mask or stack_mask
1744 * to backtrack.
1745 * It means the backtracking missed the spot where
1746 * particular register was initialized with a constant.
1747 */
1748 verbose(env, "BUG backtracking idx %d\n", i);
1749 WARN_ONCE(1, "verifier backtracking bug");
1750 return -EFAULT;
1751 }
1752 }
1753 st = st->parent;
1754 if (!st)
1755 break;
1756
Alexei Starovoitova3ce6852019-06-28 09:24:09 -07001757 new_marks = false;
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001758 func = st->frame[st->curframe];
1759 bitmap_from_u64(mask, reg_mask);
1760 for_each_set_bit(i, mask, 32) {
1761 reg = &func->regs[i];
Alexei Starovoitova3ce6852019-06-28 09:24:09 -07001762 if (reg->type != SCALAR_VALUE) {
1763 reg_mask &= ~(1u << i);
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001764 continue;
Alexei Starovoitova3ce6852019-06-28 09:24:09 -07001765 }
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001766 if (!reg->precise)
1767 new_marks = true;
1768 reg->precise = true;
1769 }
1770
1771 bitmap_from_u64(mask, stack_mask);
1772 for_each_set_bit(i, mask, 64) {
1773 if (i >= func->allocated_stack / BPF_REG_SIZE) {
1774 /* This can happen if backtracking
1775 * is propagating stack precision where
1776 * caller has larger stack frame
1777 * than callee, but backtrack_insn() should
1778 * have returned -ENOTSUPP.
1779 */
1780 verbose(env, "BUG spi %d stack_size %d\n",
1781 i, func->allocated_stack);
1782 WARN_ONCE(1, "verifier backtracking bug");
1783 return -EFAULT;
1784 }
1785
Alexei Starovoitova3ce6852019-06-28 09:24:09 -07001786 if (func->stack[i].slot_type[0] != STACK_SPILL) {
1787 stack_mask &= ~(1ull << i);
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001788 continue;
Alexei Starovoitova3ce6852019-06-28 09:24:09 -07001789 }
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001790 reg = &func->stack[i].spilled_ptr;
Alexei Starovoitova3ce6852019-06-28 09:24:09 -07001791 if (reg->type != SCALAR_VALUE) {
1792 stack_mask &= ~(1ull << i);
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001793 continue;
Alexei Starovoitova3ce6852019-06-28 09:24:09 -07001794 }
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001795 if (!reg->precise)
1796 new_marks = true;
1797 reg->precise = true;
1798 }
1799 if (env->log.level & BPF_LOG_LEVEL) {
1800 print_verifier_state(env, func);
1801 verbose(env, "parent %s regs=%x stack=%llx marks\n",
1802 new_marks ? "didn't have" : "already had",
1803 reg_mask, stack_mask);
1804 }
1805
Alexei Starovoitova3ce6852019-06-28 09:24:09 -07001806 if (!reg_mask && !stack_mask)
1807 break;
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001808 if (!new_marks)
1809 break;
1810
1811 last_idx = st->last_insn_idx;
1812 first_idx = st->first_insn_idx;
1813 }
1814 return 0;
1815}
1816
Alexei Starovoitova3ce6852019-06-28 09:24:09 -07001817static int mark_chain_precision(struct bpf_verifier_env *env, int regno)
1818{
1819 return __mark_chain_precision(env, regno, -1);
1820}
1821
1822static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi)
1823{
1824 return __mark_chain_precision(env, -1, spi);
1825}
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001826
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001827static bool is_spillable_regtype(enum bpf_reg_type type)
1828{
1829 switch (type) {
1830 case PTR_TO_MAP_VALUE:
1831 case PTR_TO_MAP_VALUE_OR_NULL:
1832 case PTR_TO_STACK:
1833 case PTR_TO_CTX:
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001834 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001835 case PTR_TO_PACKET_META:
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001836 case PTR_TO_PACKET_END:
Petar Penkovd58e4682018-09-14 07:46:18 -07001837 case PTR_TO_FLOW_KEYS:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001838 case CONST_PTR_TO_MAP:
Joe Stringerc64b7982018-10-02 13:35:33 -07001839 case PTR_TO_SOCKET:
1840 case PTR_TO_SOCKET_OR_NULL:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001841 case PTR_TO_SOCK_COMMON:
1842 case PTR_TO_SOCK_COMMON_OR_NULL:
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08001843 case PTR_TO_TCP_SOCK:
1844 case PTR_TO_TCP_SOCK_OR_NULL:
Jonathan Lemonfada7fd2019-06-06 13:59:40 -07001845 case PTR_TO_XDP_SOCK:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001846 return true;
1847 default:
1848 return false;
1849 }
1850}
1851
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001852/* Does this register contain a constant zero? */
1853static bool register_is_null(struct bpf_reg_state *reg)
1854{
1855 return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0);
1856}
1857
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001858static bool register_is_const(struct bpf_reg_state *reg)
1859{
1860 return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off);
1861}
1862
1863static void save_register_state(struct bpf_func_state *state,
1864 int spi, struct bpf_reg_state *reg)
1865{
1866 int i;
1867
1868 state->stack[spi].spilled_ptr = *reg;
1869 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
1870
1871 for (i = 0; i < BPF_REG_SIZE; i++)
1872 state->stack[spi].slot_type[i] = STACK_SPILL;
1873}
1874
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001875/* check_stack_read/write functions track spill/fill of registers,
1876 * stack boundary and alignment are checked in check_mem_access()
1877 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001878static int check_stack_write(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001879 struct bpf_func_state *state, /* func where register points to */
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07001880 int off, int size, int value_regno, int insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001881{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001882 struct bpf_func_state *cur; /* state of the current function */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001883 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err;
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001884 u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg;
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001885 struct bpf_reg_state *reg = NULL;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001886
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001887 err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE),
Joe Stringerfd978bf72018-10-02 13:35:35 -07001888 state->acquired_refs, true);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001889 if (err)
1890 return err;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001891 /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
1892 * so it's aligned access and [off, off + size) are within stack limits
1893 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001894 if (!env->allow_ptr_leaks &&
1895 state->stack[spi].slot_type[0] == STACK_SPILL &&
1896 size != BPF_REG_SIZE) {
1897 verbose(env, "attempt to corrupt spilled pointer on stack\n");
1898 return -EACCES;
1899 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001900
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001901 cur = env->cur_state->frame[env->cur_state->curframe];
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001902 if (value_regno >= 0)
1903 reg = &cur->regs[value_regno];
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001904
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001905 if (reg && size == BPF_REG_SIZE && register_is_const(reg) &&
1906 !register_is_null(reg) && env->allow_ptr_leaks) {
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001907 if (dst_reg != BPF_REG_FP) {
1908 /* The backtracking logic can only recognize explicit
1909 * stack slot address like [fp - 8]. Other spill of
1910 * scalar via different register has to be conervative.
1911 * Backtrack from here and mark all registers as precise
1912 * that contributed into 'reg' being a constant.
1913 */
1914 err = mark_chain_precision(env, value_regno);
1915 if (err)
1916 return err;
1917 }
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001918 save_register_state(state, spi, reg);
1919 } else if (reg && is_spillable_regtype(reg->type)) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001920 /* register containing pointer is being spilled into stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001921 if (size != BPF_REG_SIZE) {
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001922 verbose_linfo(env, insn_idx, "; ");
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001923 verbose(env, "invalid size of register spill\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001924 return -EACCES;
1925 }
1926
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001927 if (state != cur && reg->type == PTR_TO_STACK) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001928 verbose(env, "cannot spill pointers to stack into stack frame of the caller\n");
1929 return -EINVAL;
1930 }
1931
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001932 if (!env->allow_ptr_leaks) {
1933 bool sanitize = false;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001934
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001935 if (state->stack[spi].slot_type[0] == STACK_SPILL &&
1936 register_is_const(&state->stack[spi].spilled_ptr))
1937 sanitize = true;
1938 for (i = 0; i < BPF_REG_SIZE; i++)
1939 if (state->stack[spi].slot_type[i] == STACK_MISC) {
1940 sanitize = true;
1941 break;
1942 }
1943 if (sanitize) {
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07001944 int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off;
1945 int soff = (-spi - 1) * BPF_REG_SIZE;
1946
1947 /* detected reuse of integer stack slot with a pointer
1948 * which means either llvm is reusing stack slot or
1949 * an attacker is trying to exploit CVE-2018-3639
1950 * (speculative store bypass)
1951 * Have to sanitize that slot with preemptive
1952 * store of zero.
1953 */
1954 if (*poff && *poff != soff) {
1955 /* disallow programs where single insn stores
1956 * into two different stack slots, since verifier
1957 * cannot sanitize them
1958 */
1959 verbose(env,
1960 "insn %d cannot access two stack slots fp%d and fp%d",
1961 insn_idx, *poff, soff);
1962 return -EINVAL;
1963 }
1964 *poff = soff;
1965 }
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07001966 }
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001967 save_register_state(state, spi, reg);
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001968 } else {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001969 u8 type = STACK_MISC;
1970
Edward Cree679c7822018-08-22 20:02:19 +01001971 /* regular write of data into stack destroys any spilled ptr */
1972 state->stack[spi].spilled_ptr.type = NOT_INIT;
Jiong Wang0bae2d42018-12-15 03:34:40 -05001973 /* Mark slots as STACK_MISC if they belonged to spilled ptr. */
1974 if (state->stack[spi].slot_type[0] == STACK_SPILL)
1975 for (i = 0; i < BPF_REG_SIZE; i++)
1976 state->stack[spi].slot_type[i] = STACK_MISC;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001977
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001978 /* only mark the slot as written if all 8 bytes were written
1979 * otherwise read propagation may incorrectly stop too soon
1980 * when stack slots are partially written.
1981 * This heuristic means that read propagation will be
1982 * conservative, since it will add reg_live_read marks
1983 * to stack slots all the way to first state when programs
1984 * writes+reads less than 8 bytes
1985 */
1986 if (size == BPF_REG_SIZE)
1987 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
1988
1989 /* when we zero initialize stack slots mark them as such */
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001990 if (reg && register_is_null(reg)) {
1991 /* backtracking doesn't work for STACK_ZERO yet. */
1992 err = mark_chain_precision(env, value_regno);
1993 if (err)
1994 return err;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001995 type = STACK_ZERO;
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07001996 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001997
Jiong Wang0bae2d42018-12-15 03:34:40 -05001998 /* Mark slots affected by this stack write. */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001999 for (i = 0; i < size; i++)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002000 state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] =
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002001 type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002002 }
2003 return 0;
2004}
2005
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002006static int check_stack_read(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002007 struct bpf_func_state *reg_state /* func where register points to */,
2008 int off, int size, int value_regno)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002009{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002010 struct bpf_verifier_state *vstate = env->cur_state;
2011 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002012 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE;
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07002013 struct bpf_reg_state *reg;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002014 u8 *stype;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002015
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002016 if (reg_state->allocated_stack <= slot) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002017 verbose(env, "invalid read from stack off %d+0 size %d\n",
2018 off, size);
2019 return -EACCES;
2020 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002021 stype = reg_state->stack[spi].slot_type;
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07002022 reg = &reg_state->stack[spi].spilled_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002023
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002024 if (stype[0] == STACK_SPILL) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002025 if (size != BPF_REG_SIZE) {
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07002026 if (reg->type != SCALAR_VALUE) {
2027 verbose_linfo(env, env->insn_idx, "; ");
2028 verbose(env, "invalid size of register fill\n");
2029 return -EACCES;
2030 }
2031 if (value_regno >= 0) {
2032 mark_reg_unknown(env, state->regs, value_regno);
2033 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
2034 }
2035 mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64);
2036 return 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002037 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07002038 for (i = 1; i < BPF_REG_SIZE; i++) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002039 if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002040 verbose(env, "corrupted spill memory\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002041 return -EACCES;
2042 }
2043 }
2044
Edward Creedc503a82017-08-15 20:34:35 +01002045 if (value_regno >= 0) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002046 /* restore register state from stack */
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07002047 state->regs[value_regno] = *reg;
Alexei Starovoitov2f18f622017-11-30 21:31:38 -08002048 /* mark reg as written since spilled pointer state likely
2049 * has its liveness marks cleared by is_state_visited()
2050 * which resets stack/reg liveness for state transitions
2051 */
2052 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
Edward Creedc503a82017-08-15 20:34:35 +01002053 }
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07002054 mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002055 } else {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002056 int zeros = 0;
2057
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002058 for (i = 0; i < size; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002059 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC)
2060 continue;
2061 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) {
2062 zeros++;
2063 continue;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002064 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002065 verbose(env, "invalid read from stack off %d+%d size %d\n",
2066 off, i, size);
2067 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002068 }
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07002069 mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64);
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002070 if (value_regno >= 0) {
2071 if (zeros == size) {
2072 /* any size read into register is zero extended,
2073 * so the whole register == const_zero
2074 */
2075 __mark_reg_const_zero(&state->regs[value_regno]);
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07002076 /* backtracking doesn't support STACK_ZERO yet,
2077 * so mark it precise here, so that later
2078 * backtracking can stop here.
2079 * Backtracking may not need this if this register
2080 * doesn't participate in pointer adjustment.
2081 * Forward propagation of precise flag is not
2082 * necessary either. This mark is only to stop
2083 * backtracking. Any register that contributed
2084 * to const 0 was marked precise before spill.
2085 */
2086 state->regs[value_regno].precise = true;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002087 } else {
2088 /* have read misc data from the stack */
2089 mark_reg_unknown(env, state->regs, value_regno);
2090 }
2091 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
2092 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002093 }
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07002094 return 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002095}
2096
Daniel Borkmanne4298d22019-01-03 00:58:31 +01002097static int check_stack_access(struct bpf_verifier_env *env,
2098 const struct bpf_reg_state *reg,
2099 int off, int size)
2100{
2101 /* Stack accesses must be at a fixed offset, so that we
2102 * can determine what type of data were returned. See
2103 * check_stack_read().
2104 */
2105 if (!tnum_is_const(reg->var_off)) {
2106 char tn_buf[48];
2107
2108 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Andrey Ignatov1fbd20f2019-04-03 23:22:43 -07002109 verbose(env, "variable stack access var_off=%s off=%d size=%d\n",
Daniel Borkmanne4298d22019-01-03 00:58:31 +01002110 tn_buf, off, size);
2111 return -EACCES;
2112 }
2113
2114 if (off >= 0 || off < -MAX_BPF_STACK) {
2115 verbose(env, "invalid stack off=%d size=%d\n", off, size);
2116 return -EACCES;
2117 }
2118
2119 return 0;
2120}
2121
Daniel Borkmann591fe982019-04-09 23:20:05 +02002122static int check_map_access_type(struct bpf_verifier_env *env, u32 regno,
2123 int off, int size, enum bpf_access_type type)
2124{
2125 struct bpf_reg_state *regs = cur_regs(env);
2126 struct bpf_map *map = regs[regno].map_ptr;
2127 u32 cap = bpf_map_flags_to_cap(map);
2128
2129 if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) {
2130 verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n",
2131 map->value_size, off, size);
2132 return -EACCES;
2133 }
2134
2135 if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) {
2136 verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n",
2137 map->value_size, off, size);
2138 return -EACCES;
2139 }
2140
2141 return 0;
2142}
2143
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002144/* check read/write into map element returned by bpf_map_lookup_elem() */
Edward Creef1174f72017-08-07 15:26:19 +01002145static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08002146 int size, bool zero_size_allowed)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002147{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002148 struct bpf_reg_state *regs = cur_regs(env);
2149 struct bpf_map *map = regs[regno].map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002150
Yonghong Song9fd29c02017-11-12 14:49:09 -08002151 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
2152 off + size > map->value_size) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002153 verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002154 map->value_size, off, size);
2155 return -EACCES;
2156 }
2157 return 0;
2158}
2159
Edward Creef1174f72017-08-07 15:26:19 +01002160/* check read/write into a map element with possible variable offset */
2161static int check_map_access(struct bpf_verifier_env *env, u32 regno,
Yonghong Song9fd29c02017-11-12 14:49:09 -08002162 int off, int size, bool zero_size_allowed)
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08002163{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002164 struct bpf_verifier_state *vstate = env->cur_state;
2165 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08002166 struct bpf_reg_state *reg = &state->regs[regno];
2167 int err;
2168
Edward Creef1174f72017-08-07 15:26:19 +01002169 /* We may have adjusted the register to this map value, so we
2170 * need to try adding each of min_value and max_value to off
2171 * to make sure our theoretical access will be safe.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08002172 */
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07002173 if (env->log.level & BPF_LOG_LEVEL)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002174 print_verifier_state(env, state);
Daniel Borkmannb7137c42019-01-03 00:58:33 +01002175
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08002176 /* The minimum value is only important with signed
2177 * comparisons where we can't assume the floor of a
2178 * value is 0. If we are using signed variables for our
2179 * index'es we need to make sure that whatever we use
2180 * will have a set floor within our range.
2181 */
Daniel Borkmannb7137c42019-01-03 00:58:33 +01002182 if (reg->smin_value < 0 &&
2183 (reg->smin_value == S64_MIN ||
2184 (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) ||
2185 reg->smin_value + off < 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002186 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 -08002187 regno);
2188 return -EACCES;
2189 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08002190 err = __check_map_access(env, regno, reg->smin_value + off, size,
2191 zero_size_allowed);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08002192 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002193 verbose(env, "R%d min value is outside of the array range\n",
2194 regno);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08002195 return err;
2196 }
2197
Edward Creeb03c9f92017-08-07 15:26:36 +01002198 /* If we haven't set a max value then we need to bail since we can't be
2199 * sure we won't do bad things.
2200 * If reg->umax_value + off could overflow, treat that as unbounded too.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08002201 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002202 if (reg->umax_value >= BPF_MAX_VAR_OFF) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002203 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 -08002204 regno);
2205 return -EACCES;
2206 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08002207 err = __check_map_access(env, regno, reg->umax_value + off, size,
2208 zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01002209 if (err)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002210 verbose(env, "R%d max value is outside of the array range\n",
2211 regno);
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08002212
2213 if (map_value_has_spin_lock(reg->map_ptr)) {
2214 u32 lock = reg->map_ptr->spin_lock_off;
2215
2216 /* if any part of struct bpf_spin_lock can be touched by
2217 * load/store reject this program.
2218 * To check that [x1, x2) overlaps with [y1, y2)
2219 * it is sufficient to check x1 < y2 && y1 < x2.
2220 */
2221 if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) &&
2222 lock < reg->umax_value + off + size) {
2223 verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n");
2224 return -EACCES;
2225 }
2226 }
Edward Creef1174f72017-08-07 15:26:19 +01002227 return err;
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08002228}
2229
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002230#define MAX_PACKET_OFF 0xffff
2231
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002232static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
Thomas Graf3a0af8f2016-11-30 17:10:10 +01002233 const struct bpf_call_arg_meta *meta,
2234 enum bpf_access_type t)
Brenden Blanco4acf6c02016-07-19 12:16:56 -07002235{
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002236 switch (env->prog->type) {
Daniel Borkmann5d66fa72018-10-24 22:05:45 +02002237 /* Program types only with direct read access go here! */
Thomas Graf3a0af8f2016-11-30 17:10:10 +01002238 case BPF_PROG_TYPE_LWT_IN:
2239 case BPF_PROG_TYPE_LWT_OUT:
Mathieu Xhonneux004d4b22018-05-20 14:58:16 +01002240 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07002241 case BPF_PROG_TYPE_SK_REUSEPORT:
Daniel Borkmann5d66fa72018-10-24 22:05:45 +02002242 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Daniel Borkmannd5563d32018-10-24 22:05:46 +02002243 case BPF_PROG_TYPE_CGROUP_SKB:
Thomas Graf3a0af8f2016-11-30 17:10:10 +01002244 if (t == BPF_WRITE)
2245 return false;
Alexander Alemayhu7e57fbb2017-02-14 00:02:35 +01002246 /* fallthrough */
Daniel Borkmann5d66fa72018-10-24 22:05:45 +02002247
2248 /* Program types with direct read + write access go here! */
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002249 case BPF_PROG_TYPE_SCHED_CLS:
2250 case BPF_PROG_TYPE_SCHED_ACT:
Brenden Blanco4acf6c02016-07-19 12:16:56 -07002251 case BPF_PROG_TYPE_XDP:
Thomas Graf3a0af8f2016-11-30 17:10:10 +01002252 case BPF_PROG_TYPE_LWT_XMIT:
John Fastabend8a31db52017-08-15 22:33:09 -07002253 case BPF_PROG_TYPE_SK_SKB:
John Fastabend4f738ad2018-03-18 12:57:10 -07002254 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002255 if (meta)
2256 return meta->pkt_access;
2257
2258 env->seen_direct_write = true;
Brenden Blanco4acf6c02016-07-19 12:16:56 -07002259 return true;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002260
2261 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2262 if (t == BPF_WRITE)
2263 env->seen_direct_write = true;
2264
2265 return true;
2266
Brenden Blanco4acf6c02016-07-19 12:16:56 -07002267 default:
2268 return false;
2269 }
2270}
2271
Edward Creef1174f72017-08-07 15:26:19 +01002272static int __check_packet_access(struct bpf_verifier_env *env, u32 regno,
Yonghong Song9fd29c02017-11-12 14:49:09 -08002273 int off, int size, bool zero_size_allowed)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002274{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002275 struct bpf_reg_state *regs = cur_regs(env);
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002276 struct bpf_reg_state *reg = &regs[regno];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002277
Yonghong Song9fd29c02017-11-12 14:49:09 -08002278 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
2279 (u64)off + size > reg->range) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002280 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 -07002281 off, size, regno, reg->id, reg->off, reg->range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002282 return -EACCES;
2283 }
2284 return 0;
2285}
2286
Edward Creef1174f72017-08-07 15:26:19 +01002287static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08002288 int size, bool zero_size_allowed)
Edward Creef1174f72017-08-07 15:26:19 +01002289{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002290 struct bpf_reg_state *regs = cur_regs(env);
Edward Creef1174f72017-08-07 15:26:19 +01002291 struct bpf_reg_state *reg = &regs[regno];
2292 int err;
2293
2294 /* We may have added a variable offset to the packet pointer; but any
2295 * reg->range we have comes after that. We are only checking the fixed
2296 * offset.
2297 */
2298
2299 /* We don't allow negative numbers, because we aren't tracking enough
2300 * detail to prove they're safe.
2301 */
Edward Creeb03c9f92017-08-07 15:26:36 +01002302 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002303 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 +01002304 regno);
2305 return -EACCES;
2306 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08002307 err = __check_packet_access(env, regno, off, size, zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01002308 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002309 verbose(env, "R%d offset is outside of the packet\n", regno);
Edward Creef1174f72017-08-07 15:26:19 +01002310 return err;
2311 }
Jiong Wange6478152018-11-08 04:08:42 -05002312
2313 /* __check_packet_access has made sure "off + size - 1" is within u16.
2314 * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff,
2315 * otherwise find_good_pkt_pointers would have refused to set range info
2316 * that __check_packet_access would have rejected this pkt access.
2317 * Therefore, "off + reg->umax_value + size - 1" won't overflow u32.
2318 */
2319 env->prog->aux->max_pkt_offset =
2320 max_t(u32, env->prog->aux->max_pkt_offset,
2321 off + reg->umax_value + size - 1);
2322
Edward Creef1174f72017-08-07 15:26:19 +01002323 return err;
2324}
2325
2326/* check access to 'struct bpf_context' fields. Supports fixed offsets only */
Yonghong Song31fd8582017-06-13 15:52:13 -07002327static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07002328 enum bpf_access_type t, enum bpf_reg_type *reg_type)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002329{
Daniel Borkmannf96da092017-07-02 02:13:27 +02002330 struct bpf_insn_access_aux info = {
2331 .reg_type = *reg_type,
2332 };
Yonghong Song31fd8582017-06-13 15:52:13 -07002333
Jakub Kicinski4f9218a2017-10-16 16:40:55 -07002334 if (env->ops->is_valid_access &&
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002335 env->ops->is_valid_access(off, size, t, env->prog, &info)) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02002336 /* A non zero info.ctx_field_size indicates that this field is a
2337 * candidate for later verifier transformation to load the whole
2338 * field and then apply a mask when accessed with a narrower
2339 * access than actual ctx access size. A zero info.ctx_field_size
2340 * will only allow for whole field access and rejects any other
2341 * type of narrower access.
Yonghong Song31fd8582017-06-13 15:52:13 -07002342 */
Yonghong Song23994632017-06-22 15:07:39 -07002343 *reg_type = info.reg_type;
Yonghong Song31fd8582017-06-13 15:52:13 -07002344
Jakub Kicinski4f9218a2017-10-16 16:40:55 -07002345 env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07002346 /* remember the offset of last byte accessed in ctx */
2347 if (env->prog->aux->max_ctx_offset < off + size)
2348 env->prog->aux->max_ctx_offset = off + size;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002349 return 0;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07002350 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002351
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002352 verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002353 return -EACCES;
2354}
2355
Petar Penkovd58e4682018-09-14 07:46:18 -07002356static int check_flow_keys_access(struct bpf_verifier_env *env, int off,
2357 int size)
2358{
2359 if (size < 0 || off < 0 ||
2360 (u64)off + size > sizeof(struct bpf_flow_keys)) {
2361 verbose(env, "invalid access to flow keys off=%d size=%d\n",
2362 off, size);
2363 return -EACCES;
2364 }
2365 return 0;
2366}
2367
Martin KaFai Lau5f456642019-02-08 22:25:54 -08002368static int check_sock_access(struct bpf_verifier_env *env, int insn_idx,
2369 u32 regno, int off, int size,
2370 enum bpf_access_type t)
Joe Stringerc64b7982018-10-02 13:35:33 -07002371{
2372 struct bpf_reg_state *regs = cur_regs(env);
2373 struct bpf_reg_state *reg = &regs[regno];
Martin KaFai Lau5f456642019-02-08 22:25:54 -08002374 struct bpf_insn_access_aux info = {};
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002375 bool valid;
Joe Stringerc64b7982018-10-02 13:35:33 -07002376
2377 if (reg->smin_value < 0) {
2378 verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
2379 regno);
2380 return -EACCES;
2381 }
2382
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002383 switch (reg->type) {
2384 case PTR_TO_SOCK_COMMON:
2385 valid = bpf_sock_common_is_valid_access(off, size, t, &info);
2386 break;
2387 case PTR_TO_SOCKET:
2388 valid = bpf_sock_is_valid_access(off, size, t, &info);
2389 break;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08002390 case PTR_TO_TCP_SOCK:
2391 valid = bpf_tcp_sock_is_valid_access(off, size, t, &info);
2392 break;
Jonathan Lemonfada7fd2019-06-06 13:59:40 -07002393 case PTR_TO_XDP_SOCK:
2394 valid = bpf_xdp_sock_is_valid_access(off, size, t, &info);
2395 break;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002396 default:
2397 valid = false;
Joe Stringerc64b7982018-10-02 13:35:33 -07002398 }
2399
Martin KaFai Lau5f456642019-02-08 22:25:54 -08002400
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002401 if (valid) {
2402 env->insn_aux_data[insn_idx].ctx_field_size =
2403 info.ctx_field_size;
2404 return 0;
2405 }
2406
2407 verbose(env, "R%d invalid %s access off=%d size=%d\n",
2408 regno, reg_type_str[reg->type], off, size);
2409
2410 return -EACCES;
Joe Stringerc64b7982018-10-02 13:35:33 -07002411}
2412
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02002413static bool __is_pointer_value(bool allow_ptr_leaks,
2414 const struct bpf_reg_state *reg)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002415{
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02002416 if (allow_ptr_leaks)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002417 return false;
2418
Edward Creef1174f72017-08-07 15:26:19 +01002419 return reg->type != SCALAR_VALUE;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002420}
2421
Daniel Borkmann2a159c62018-10-21 02:09:24 +02002422static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno)
2423{
2424 return cur_regs(env) + regno;
2425}
2426
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02002427static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
2428{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02002429 return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno));
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02002430}
2431
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01002432static bool is_ctx_reg(struct bpf_verifier_env *env, int regno)
2433{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02002434 const struct bpf_reg_state *reg = reg_state(env, regno);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01002435
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002436 return reg->type == PTR_TO_CTX;
2437}
2438
2439static bool is_sk_reg(struct bpf_verifier_env *env, int regno)
2440{
2441 const struct bpf_reg_state *reg = reg_state(env, regno);
2442
2443 return type_is_sk_pointer(reg->type);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01002444}
2445
Daniel Borkmannca369602018-02-23 22:29:05 +01002446static bool is_pkt_reg(struct bpf_verifier_env *env, int regno)
2447{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02002448 const struct bpf_reg_state *reg = reg_state(env, regno);
Daniel Borkmannca369602018-02-23 22:29:05 +01002449
2450 return type_is_pkt_pointer(reg->type);
2451}
2452
Daniel Borkmann4b5defd2018-10-21 02:09:25 +02002453static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno)
2454{
2455 const struct bpf_reg_state *reg = reg_state(env, regno);
2456
2457 /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */
2458 return reg->type == PTR_TO_FLOW_KEYS;
2459}
2460
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002461static int check_pkt_ptr_alignment(struct bpf_verifier_env *env,
2462 const struct bpf_reg_state *reg,
David S. Millerd1174412017-05-10 11:22:52 -07002463 int off, int size, bool strict)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002464{
Edward Creef1174f72017-08-07 15:26:19 +01002465 struct tnum reg_off;
David S. Millere07b98d2017-05-10 11:38:07 -07002466 int ip_align;
David S. Millerd1174412017-05-10 11:22:52 -07002467
2468 /* Byte size accesses are always allowed. */
2469 if (!strict || size == 1)
2470 return 0;
2471
David S. Millere4eda882017-05-22 12:27:07 -04002472 /* For platforms that do not have a Kconfig enabling
2473 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of
2474 * NET_IP_ALIGN is universally set to '2'. And on platforms
2475 * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get
2476 * to this code only in strict mode where we want to emulate
2477 * the NET_IP_ALIGN==2 checking. Therefore use an
2478 * unconditional IP align value of '2'.
David S. Millere07b98d2017-05-10 11:38:07 -07002479 */
David S. Millere4eda882017-05-22 12:27:07 -04002480 ip_align = 2;
Edward Creef1174f72017-08-07 15:26:19 +01002481
2482 reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off));
2483 if (!tnum_is_aligned(reg_off, size)) {
2484 char tn_buf[48];
2485
2486 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002487 verbose(env,
2488 "misaligned packet access off %d+%s+%d+%d size %d\n",
Edward Creef1174f72017-08-07 15:26:19 +01002489 ip_align, tn_buf, reg->off, off, size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002490 return -EACCES;
2491 }
Daniel Borkmann79adffc2017-03-31 02:24:03 +02002492
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002493 return 0;
2494}
2495
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002496static int check_generic_ptr_alignment(struct bpf_verifier_env *env,
2497 const struct bpf_reg_state *reg,
Edward Creef1174f72017-08-07 15:26:19 +01002498 const char *pointer_desc,
2499 int off, int size, bool strict)
Daniel Borkmann79adffc2017-03-31 02:24:03 +02002500{
Edward Creef1174f72017-08-07 15:26:19 +01002501 struct tnum reg_off;
2502
2503 /* Byte size accesses are always allowed. */
2504 if (!strict || size == 1)
2505 return 0;
2506
2507 reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off));
2508 if (!tnum_is_aligned(reg_off, size)) {
2509 char tn_buf[48];
2510
2511 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002512 verbose(env, "misaligned %saccess off %s+%d+%d size %d\n",
Edward Creef1174f72017-08-07 15:26:19 +01002513 pointer_desc, tn_buf, reg->off, off, size);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02002514 return -EACCES;
2515 }
2516
2517 return 0;
2518}
2519
David S. Millere07b98d2017-05-10 11:38:07 -07002520static int check_ptr_alignment(struct bpf_verifier_env *env,
Daniel Borkmannca369602018-02-23 22:29:05 +01002521 const struct bpf_reg_state *reg, int off,
2522 int size, bool strict_alignment_once)
Daniel Borkmann79adffc2017-03-31 02:24:03 +02002523{
Daniel Borkmannca369602018-02-23 22:29:05 +01002524 bool strict = env->strict_alignment || strict_alignment_once;
Edward Creef1174f72017-08-07 15:26:19 +01002525 const char *pointer_desc = "";
David S. Millerd1174412017-05-10 11:22:52 -07002526
Daniel Borkmann79adffc2017-03-31 02:24:03 +02002527 switch (reg->type) {
2528 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002529 case PTR_TO_PACKET_META:
2530 /* Special case, because of NET_IP_ALIGN. Given metadata sits
2531 * right in front, treat it the very same way.
2532 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002533 return check_pkt_ptr_alignment(env, reg, off, size, strict);
Petar Penkovd58e4682018-09-14 07:46:18 -07002534 case PTR_TO_FLOW_KEYS:
2535 pointer_desc = "flow keys ";
2536 break;
Edward Creef1174f72017-08-07 15:26:19 +01002537 case PTR_TO_MAP_VALUE:
2538 pointer_desc = "value ";
2539 break;
2540 case PTR_TO_CTX:
2541 pointer_desc = "context ";
2542 break;
2543 case PTR_TO_STACK:
2544 pointer_desc = "stack ";
Jann Horna5ec6ae2017-12-18 20:11:58 -08002545 /* The stack spill tracking logic in check_stack_write()
2546 * and check_stack_read() relies on stack accesses being
2547 * aligned.
2548 */
2549 strict = true;
Edward Creef1174f72017-08-07 15:26:19 +01002550 break;
Joe Stringerc64b7982018-10-02 13:35:33 -07002551 case PTR_TO_SOCKET:
2552 pointer_desc = "sock ";
2553 break;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002554 case PTR_TO_SOCK_COMMON:
2555 pointer_desc = "sock_common ";
2556 break;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08002557 case PTR_TO_TCP_SOCK:
2558 pointer_desc = "tcp_sock ";
2559 break;
Jonathan Lemonfada7fd2019-06-06 13:59:40 -07002560 case PTR_TO_XDP_SOCK:
2561 pointer_desc = "xdp_sock ";
2562 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02002563 default:
Edward Creef1174f72017-08-07 15:26:19 +01002564 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02002565 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002566 return check_generic_ptr_alignment(env, reg, pointer_desc, off, size,
2567 strict);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02002568}
2569
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002570static int update_stack_depth(struct bpf_verifier_env *env,
2571 const struct bpf_func_state *func,
2572 int off)
2573{
Jiong Wang9c8105b2018-05-02 16:17:18 -04002574 u16 stack = env->subprog_info[func->subprogno].stack_depth;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002575
2576 if (stack >= -off)
2577 return 0;
2578
2579 /* update known max for given subprogram */
Jiong Wang9c8105b2018-05-02 16:17:18 -04002580 env->subprog_info[func->subprogno].stack_depth = -off;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002581 return 0;
2582}
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002583
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002584/* starting from main bpf function walk all instructions of the function
2585 * and recursively walk all callees that given function can call.
2586 * Ignore jump and exit insns.
2587 * Since recursion is prevented by check_cfg() this algorithm
2588 * only needs a local stack of MAX_CALL_FRAMES to remember callsites
2589 */
2590static int check_max_stack_depth(struct bpf_verifier_env *env)
2591{
Jiong Wang9c8105b2018-05-02 16:17:18 -04002592 int depth = 0, frame = 0, idx = 0, i = 0, subprog_end;
2593 struct bpf_subprog_info *subprog = env->subprog_info;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002594 struct bpf_insn *insn = env->prog->insnsi;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002595 int ret_insn[MAX_CALL_FRAMES];
2596 int ret_prog[MAX_CALL_FRAMES];
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002597
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002598process_func:
2599 /* round up to 32-bytes, since this is granularity
2600 * of interpreter stack size
2601 */
Jiong Wang9c8105b2018-05-02 16:17:18 -04002602 depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32);
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002603 if (depth > MAX_BPF_STACK) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002604 verbose(env, "combined stack size of %d calls is %d. Too large\n",
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002605 frame + 1, depth);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002606 return -EACCES;
2607 }
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002608continue_func:
Jiong Wang4cb3d992018-05-02 16:17:19 -04002609 subprog_end = subprog[idx + 1].start;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002610 for (; i < subprog_end; i++) {
2611 if (insn[i].code != (BPF_JMP | BPF_CALL))
2612 continue;
2613 if (insn[i].src_reg != BPF_PSEUDO_CALL)
2614 continue;
2615 /* remember insn and function to return to */
2616 ret_insn[frame] = i + 1;
Jiong Wang9c8105b2018-05-02 16:17:18 -04002617 ret_prog[frame] = idx;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002618
2619 /* find the callee */
2620 i = i + insn[i].imm + 1;
Jiong Wang9c8105b2018-05-02 16:17:18 -04002621 idx = find_subprog(env, i);
2622 if (idx < 0) {
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002623 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
2624 i);
2625 return -EFAULT;
2626 }
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002627 frame++;
2628 if (frame >= MAX_CALL_FRAMES) {
Paul Chaignon927cb782019-03-20 13:58:27 +01002629 verbose(env, "the call stack of %d frames is too deep !\n",
2630 frame);
2631 return -E2BIG;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002632 }
2633 goto process_func;
2634 }
2635 /* end of for() loop means the last insn of the 'subprog'
2636 * was reached. Doesn't matter whether it was JA or EXIT
2637 */
2638 if (frame == 0)
2639 return 0;
Jiong Wang9c8105b2018-05-02 16:17:18 -04002640 depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32);
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002641 frame--;
2642 i = ret_insn[frame];
Jiong Wang9c8105b2018-05-02 16:17:18 -04002643 idx = ret_prog[frame];
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002644 goto continue_func;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002645}
2646
David S. Miller19d28fb2018-01-11 21:27:54 -05002647#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08002648static int get_callee_stack_depth(struct bpf_verifier_env *env,
2649 const struct bpf_insn *insn, int idx)
2650{
2651 int start = idx + insn->imm + 1, subprog;
2652
2653 subprog = find_subprog(env, start);
2654 if (subprog < 0) {
2655 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
2656 start);
2657 return -EFAULT;
2658 }
Jiong Wang9c8105b2018-05-02 16:17:18 -04002659 return env->subprog_info[subprog].stack_depth;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08002660}
David S. Miller19d28fb2018-01-11 21:27:54 -05002661#endif
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08002662
Daniel Borkmann58990d12018-06-07 17:40:03 +02002663static int check_ctx_reg(struct bpf_verifier_env *env,
2664 const struct bpf_reg_state *reg, int regno)
2665{
2666 /* Access to ctx or passing it to a helper is only allowed in
2667 * its original, unmodified form.
2668 */
2669
2670 if (reg->off) {
2671 verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n",
2672 regno, reg->off);
2673 return -EACCES;
2674 }
2675
2676 if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
2677 char tn_buf[48];
2678
2679 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2680 verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf);
2681 return -EACCES;
2682 }
2683
2684 return 0;
2685}
2686
Matt Mullins9df1c282019-04-26 11:49:47 -07002687static int check_tp_buffer_access(struct bpf_verifier_env *env,
2688 const struct bpf_reg_state *reg,
2689 int regno, int off, int size)
2690{
2691 if (off < 0) {
2692 verbose(env,
2693 "R%d invalid tracepoint buffer access: off=%d, size=%d",
2694 regno, off, size);
2695 return -EACCES;
2696 }
2697 if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
2698 char tn_buf[48];
2699
2700 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2701 verbose(env,
2702 "R%d invalid variable buffer offset: off=%d, var_off=%s",
2703 regno, off, tn_buf);
2704 return -EACCES;
2705 }
2706 if (off + size > env->prog->aux->max_tp_access)
2707 env->prog->aux->max_tp_access = off + size;
2708
2709 return 0;
2710}
2711
2712
Jann Horn0c17d1d2017-12-18 20:11:55 -08002713/* truncate register to smaller size (in bytes)
2714 * must be called with size < BPF_REG_SIZE
2715 */
2716static void coerce_reg_to_size(struct bpf_reg_state *reg, int size)
2717{
2718 u64 mask;
2719
2720 /* clear high bits in bit representation */
2721 reg->var_off = tnum_cast(reg->var_off, size);
2722
2723 /* fix arithmetic bounds */
2724 mask = ((u64)1 << (size * 8)) - 1;
2725 if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) {
2726 reg->umin_value &= mask;
2727 reg->umax_value &= mask;
2728 } else {
2729 reg->umin_value = 0;
2730 reg->umax_value = mask;
2731 }
2732 reg->smin_value = reg->umin_value;
2733 reg->smax_value = reg->umax_value;
2734}
2735
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002736/* check whether memory at (regno + off) is accessible for t = (read | write)
2737 * if t==write, value_regno is a register which value is stored into memory
2738 * if t==read, value_regno is a register which will receive the value from memory
2739 * if t==write && value_regno==-1, some unknown value is stored into memory
2740 * if t==read && value_regno==-1, don't care what we read from memory
2741 */
Daniel Borkmannca369602018-02-23 22:29:05 +01002742static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno,
2743 int off, int bpf_size, enum bpf_access_type t,
2744 int value_regno, bool strict_alignment_once)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002745{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002746 struct bpf_reg_state *regs = cur_regs(env);
2747 struct bpf_reg_state *reg = regs + regno;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002748 struct bpf_func_state *state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002749 int size, err = 0;
2750
2751 size = bpf_size_to_bytes(bpf_size);
2752 if (size < 0)
2753 return size;
2754
Edward Creef1174f72017-08-07 15:26:19 +01002755 /* alignment checks will add in reg->off themselves */
Daniel Borkmannca369602018-02-23 22:29:05 +01002756 err = check_ptr_alignment(env, reg, off, size, strict_alignment_once);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002757 if (err)
2758 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002759
Edward Creef1174f72017-08-07 15:26:19 +01002760 /* for access checks, reg->off is just part of off */
2761 off += reg->off;
2762
2763 if (reg->type == PTR_TO_MAP_VALUE) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002764 if (t == BPF_WRITE && value_regno >= 0 &&
2765 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002766 verbose(env, "R%d leaks addr into map\n", value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002767 return -EACCES;
2768 }
Daniel Borkmann591fe982019-04-09 23:20:05 +02002769 err = check_map_access_type(env, regno, off, size, t);
2770 if (err)
2771 return err;
Yonghong Song9fd29c02017-11-12 14:49:09 -08002772 err = check_map_access(env, regno, off, size, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002773 if (!err && t == BPF_READ && value_regno >= 0)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002774 mark_reg_unknown(env, regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002775
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002776 } else if (reg->type == PTR_TO_CTX) {
Edward Creef1174f72017-08-07 15:26:19 +01002777 enum bpf_reg_type reg_type = SCALAR_VALUE;
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07002778
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002779 if (t == BPF_WRITE && value_regno >= 0 &&
2780 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002781 verbose(env, "R%d leaks addr into ctx\n", value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002782 return -EACCES;
2783 }
Edward Creef1174f72017-08-07 15:26:19 +01002784
Daniel Borkmann58990d12018-06-07 17:40:03 +02002785 err = check_ctx_reg(env, reg, regno);
2786 if (err < 0)
2787 return err;
2788
Yonghong Song31fd8582017-06-13 15:52:13 -07002789 err = check_ctx_access(env, insn_idx, off, size, t, &reg_type);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002790 if (!err && t == BPF_READ && value_regno >= 0) {
Edward Creef1174f72017-08-07 15:26:19 +01002791 /* ctx access returns either a scalar, or a
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002792 * PTR_TO_PACKET[_META,_END]. In the latter
2793 * case, we know the offset is zero.
Edward Creef1174f72017-08-07 15:26:19 +01002794 */
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002795 if (reg_type == SCALAR_VALUE) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002796 mark_reg_unknown(env, regs, value_regno);
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002797 } else {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002798 mark_reg_known_zero(env, regs,
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002799 value_regno);
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002800 if (reg_type_may_be_null(reg_type))
2801 regs[value_regno].id = ++env->id_gen;
Jiong Wang5327ed32019-05-24 23:25:12 +01002802 /* A load of ctx field could have different
2803 * actual load size with the one encoded in the
2804 * insn. When the dst is PTR, it is for sure not
2805 * a sub-register.
2806 */
2807 regs[value_regno].subreg_def = DEF_NOT_SUBREG;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002808 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002809 regs[value_regno].type = reg_type;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002810 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002811
Edward Creef1174f72017-08-07 15:26:19 +01002812 } else if (reg->type == PTR_TO_STACK) {
Edward Creef1174f72017-08-07 15:26:19 +01002813 off += reg->var_off.value;
Daniel Borkmanne4298d22019-01-03 00:58:31 +01002814 err = check_stack_access(env, reg, off, size);
2815 if (err)
2816 return err;
Alexei Starovoitov87266792017-05-30 13:31:29 -07002817
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002818 state = func(env, reg);
2819 err = update_stack_depth(env, state, off);
2820 if (err)
2821 return err;
Alexei Starovoitov87266792017-05-30 13:31:29 -07002822
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002823 if (t == BPF_WRITE)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002824 err = check_stack_write(env, state, off, size,
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07002825 value_regno, insn_idx);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002826 else
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002827 err = check_stack_read(env, state, off, size,
2828 value_regno);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002829 } else if (reg_is_pkt_pointer(reg)) {
Thomas Graf3a0af8f2016-11-30 17:10:10 +01002830 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002831 verbose(env, "cannot write into packet\n");
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002832 return -EACCES;
2833 }
Brenden Blanco4acf6c02016-07-19 12:16:56 -07002834 if (t == BPF_WRITE && value_regno >= 0 &&
2835 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002836 verbose(env, "R%d leaks addr into packet\n",
2837 value_regno);
Brenden Blanco4acf6c02016-07-19 12:16:56 -07002838 return -EACCES;
2839 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08002840 err = check_packet_access(env, regno, off, size, false);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002841 if (!err && t == BPF_READ && value_regno >= 0)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002842 mark_reg_unknown(env, regs, value_regno);
Petar Penkovd58e4682018-09-14 07:46:18 -07002843 } else if (reg->type == PTR_TO_FLOW_KEYS) {
2844 if (t == BPF_WRITE && value_regno >= 0 &&
2845 is_pointer_value(env, value_regno)) {
2846 verbose(env, "R%d leaks addr into flow keys\n",
2847 value_regno);
2848 return -EACCES;
2849 }
2850
2851 err = check_flow_keys_access(env, off, size);
2852 if (!err && t == BPF_READ && value_regno >= 0)
2853 mark_reg_unknown(env, regs, value_regno);
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002854 } else if (type_is_sk_pointer(reg->type)) {
Joe Stringerc64b7982018-10-02 13:35:33 -07002855 if (t == BPF_WRITE) {
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002856 verbose(env, "R%d cannot write into %s\n",
2857 regno, reg_type_str[reg->type]);
Joe Stringerc64b7982018-10-02 13:35:33 -07002858 return -EACCES;
2859 }
Martin KaFai Lau5f456642019-02-08 22:25:54 -08002860 err = check_sock_access(env, insn_idx, regno, off, size, t);
Joe Stringerc64b7982018-10-02 13:35:33 -07002861 if (!err && value_regno >= 0)
2862 mark_reg_unknown(env, regs, value_regno);
Matt Mullins9df1c282019-04-26 11:49:47 -07002863 } else if (reg->type == PTR_TO_TP_BUFFER) {
2864 err = check_tp_buffer_access(env, reg, regno, off, size);
2865 if (!err && t == BPF_READ && value_regno >= 0)
2866 mark_reg_unknown(env, regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002867 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002868 verbose(env, "R%d invalid mem access '%s'\n", regno,
2869 reg_type_str[reg->type]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002870 return -EACCES;
2871 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002872
Edward Creef1174f72017-08-07 15:26:19 +01002873 if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ &&
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002874 regs[value_regno].type == SCALAR_VALUE) {
Edward Creef1174f72017-08-07 15:26:19 +01002875 /* b/h/w load zero-extends, mark upper bits as known 0 */
Jann Horn0c17d1d2017-12-18 20:11:55 -08002876 coerce_reg_to_size(&regs[value_regno], size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002877 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002878 return err;
2879}
2880
Yonghong Song31fd8582017-06-13 15:52:13 -07002881static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002882{
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002883 int err;
2884
2885 if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
2886 insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002887 verbose(env, "BPF_XADD uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002888 return -EINVAL;
2889 }
2890
2891 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01002892 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002893 if (err)
2894 return err;
2895
2896 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01002897 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002898 if (err)
2899 return err;
2900
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +02002901 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002902 verbose(env, "R%d leaks addr into mem\n", insn->src_reg);
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +02002903 return -EACCES;
2904 }
2905
Daniel Borkmannca369602018-02-23 22:29:05 +01002906 if (is_ctx_reg(env, insn->dst_reg) ||
Daniel Borkmann4b5defd2018-10-21 02:09:25 +02002907 is_pkt_reg(env, insn->dst_reg) ||
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002908 is_flow_key_reg(env, insn->dst_reg) ||
2909 is_sk_reg(env, insn->dst_reg)) {
Daniel Borkmannca369602018-02-23 22:29:05 +01002910 verbose(env, "BPF_XADD stores into R%d %s is not allowed\n",
Daniel Borkmann2a159c62018-10-21 02:09:24 +02002911 insn->dst_reg,
2912 reg_type_str[reg_state(env, insn->dst_reg)->type]);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01002913 return -EACCES;
2914 }
2915
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002916 /* check whether atomic_add can read the memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07002917 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Daniel Borkmannca369602018-02-23 22:29:05 +01002918 BPF_SIZE(insn->code), BPF_READ, -1, true);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002919 if (err)
2920 return err;
2921
2922 /* check whether atomic_add can write into the same memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07002923 return check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Daniel Borkmannca369602018-02-23 22:29:05 +01002924 BPF_SIZE(insn->code), BPF_WRITE, -1, true);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002925}
2926
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002927static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno,
2928 int off, int access_size,
2929 bool zero_size_allowed)
2930{
2931 struct bpf_reg_state *reg = reg_state(env, regno);
2932
2933 if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
2934 access_size < 0 || (access_size == 0 && !zero_size_allowed)) {
2935 if (tnum_is_const(reg->var_off)) {
2936 verbose(env, "invalid stack type R%d off=%d access_size=%d\n",
2937 regno, off, access_size);
2938 } else {
2939 char tn_buf[48];
2940
2941 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2942 verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n",
2943 regno, tn_buf, access_size);
2944 }
2945 return -EACCES;
2946 }
2947 return 0;
2948}
2949
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002950/* when register 'regno' is passed into function that will read 'access_size'
2951 * bytes from that pointer, make sure that it's within stack boundary
Edward Creef1174f72017-08-07 15:26:19 +01002952 * and all elements of stack are initialized.
2953 * Unlike most pointer bounds-checking functions, this one doesn't take an
2954 * 'off' argument, so it has to add in reg->off itself.
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002955 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002956static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
Daniel Borkmann435faee12016-04-13 00:10:51 +02002957 int access_size, bool zero_size_allowed,
2958 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002959{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02002960 struct bpf_reg_state *reg = reg_state(env, regno);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002961 struct bpf_func_state *state = func(env, reg);
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07002962 int err, min_off, max_off, i, j, slot, spi;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002963
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002964 if (reg->type != PTR_TO_STACK) {
Edward Creef1174f72017-08-07 15:26:19 +01002965 /* Allow zero-byte read from NULL, regardless of pointer type */
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002966 if (zero_size_allowed && access_size == 0 &&
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002967 register_is_null(reg))
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002968 return 0;
2969
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002970 verbose(env, "R%d type=%s expected=%s\n", regno,
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002971 reg_type_str[reg->type],
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002972 reg_type_str[PTR_TO_STACK]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002973 return -EACCES;
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002974 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002975
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002976 if (tnum_is_const(reg->var_off)) {
2977 min_off = max_off = reg->var_off.value + reg->off;
2978 err = __check_stack_boundary(env, regno, min_off, access_size,
2979 zero_size_allowed);
2980 if (err)
2981 return err;
2982 } else {
Andrey Ignatov088ec262019-04-03 23:22:39 -07002983 /* Variable offset is prohibited for unprivileged mode for
2984 * simplicity since it requires corresponding support in
2985 * Spectre masking for stack ALU.
2986 * See also retrieve_ptr_limit().
2987 */
2988 if (!env->allow_ptr_leaks) {
2989 char tn_buf[48];
Edward Creef1174f72017-08-07 15:26:19 +01002990
Andrey Ignatov088ec262019-04-03 23:22:39 -07002991 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2992 verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n",
2993 regno, tn_buf);
2994 return -EACCES;
2995 }
Andrey Ignatovf2bcd052019-04-03 23:22:37 -07002996 /* Only initialized buffer on stack is allowed to be accessed
2997 * with variable offset. With uninitialized buffer it's hard to
2998 * guarantee that whole memory is marked as initialized on
2999 * helper return since specific bounds are unknown what may
3000 * cause uninitialized stack leaking.
3001 */
3002 if (meta && meta->raw_mode)
3003 meta = NULL;
3004
Andrey Ignatov107c26a72019-04-03 23:22:41 -07003005 if (reg->smax_value >= BPF_MAX_VAR_OFF ||
3006 reg->smax_value <= -BPF_MAX_VAR_OFF) {
3007 verbose(env, "R%d unbounded indirect variable offset stack access\n",
3008 regno);
3009 return -EACCES;
3010 }
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07003011 min_off = reg->smin_value + reg->off;
Andrey Ignatov107c26a72019-04-03 23:22:41 -07003012 max_off = reg->smax_value + reg->off;
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07003013 err = __check_stack_boundary(env, regno, min_off, access_size,
3014 zero_size_allowed);
Andrey Ignatov107c26a72019-04-03 23:22:41 -07003015 if (err) {
3016 verbose(env, "R%d min value is outside of stack bound\n",
3017 regno);
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07003018 return err;
Andrey Ignatov107c26a72019-04-03 23:22:41 -07003019 }
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07003020 err = __check_stack_boundary(env, regno, max_off, access_size,
3021 zero_size_allowed);
Andrey Ignatov107c26a72019-04-03 23:22:41 -07003022 if (err) {
3023 verbose(env, "R%d max value is outside of stack bound\n",
3024 regno);
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07003025 return err;
Andrey Ignatov107c26a72019-04-03 23:22:41 -07003026 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003027 }
3028
Daniel Borkmann435faee12016-04-13 00:10:51 +02003029 if (meta && meta->raw_mode) {
3030 meta->access_size = access_size;
3031 meta->regno = regno;
3032 return 0;
3033 }
3034
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07003035 for (i = min_off; i < max_off + access_size; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08003036 u8 *stype;
3037
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07003038 slot = -i - 1;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003039 spi = slot / BPF_REG_SIZE;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08003040 if (state->allocated_stack <= slot)
3041 goto err;
3042 stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE];
3043 if (*stype == STACK_MISC)
3044 goto mark;
3045 if (*stype == STACK_ZERO) {
3046 /* helper can write anything into the stack */
3047 *stype = STACK_MISC;
3048 goto mark;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003049 }
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07003050 if (state->stack[spi].slot_type[0] == STACK_SPILL &&
3051 state->stack[spi].spilled_ptr.type == SCALAR_VALUE) {
3052 __mark_reg_unknown(&state->stack[spi].spilled_ptr);
3053 for (j = 0; j < BPF_REG_SIZE; j++)
3054 state->stack[spi].slot_type[j] = STACK_MISC;
3055 goto mark;
3056 }
3057
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08003058err:
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07003059 if (tnum_is_const(reg->var_off)) {
3060 verbose(env, "invalid indirect read from stack off %d+%d size %d\n",
3061 min_off, i - min_off, access_size);
3062 } else {
3063 char tn_buf[48];
3064
3065 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
3066 verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n",
3067 tn_buf, i - min_off, access_size);
3068 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08003069 return -EACCES;
3070mark:
3071 /* reading any byte out of 8-byte 'spill_slot' will cause
3072 * the whole slot to be marked as 'read'
3073 */
Edward Cree679c7822018-08-22 20:02:19 +01003074 mark_reg_read(env, &state->stack[spi].spilled_ptr,
Jiong Wang5327ed32019-05-24 23:25:12 +01003075 state->stack[spi].spilled_ptr.parent,
3076 REG_LIVE_READ64);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003077 }
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07003078 return update_stack_depth(env, state, min_off);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003079}
3080
Gianluca Borello06c1c042017-01-09 10:19:49 -08003081static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
3082 int access_size, bool zero_size_allowed,
3083 struct bpf_call_arg_meta *meta)
3084{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003085 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
Gianluca Borello06c1c042017-01-09 10:19:49 -08003086
Edward Creef1174f72017-08-07 15:26:19 +01003087 switch (reg->type) {
Gianluca Borello06c1c042017-01-09 10:19:49 -08003088 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003089 case PTR_TO_PACKET_META:
Yonghong Song9fd29c02017-11-12 14:49:09 -08003090 return check_packet_access(env, regno, reg->off, access_size,
3091 zero_size_allowed);
Gianluca Borello06c1c042017-01-09 10:19:49 -08003092 case PTR_TO_MAP_VALUE:
Daniel Borkmann591fe982019-04-09 23:20:05 +02003093 if (check_map_access_type(env, regno, reg->off, access_size,
3094 meta && meta->raw_mode ? BPF_WRITE :
3095 BPF_READ))
3096 return -EACCES;
Yonghong Song9fd29c02017-11-12 14:49:09 -08003097 return check_map_access(env, regno, reg->off, access_size,
3098 zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01003099 default: /* scalar_value|ptr_to_stack or invalid ptr */
Gianluca Borello06c1c042017-01-09 10:19:49 -08003100 return check_stack_boundary(env, regno, access_size,
3101 zero_size_allowed, meta);
3102 }
3103}
3104
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08003105/* Implementation details:
3106 * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL
3107 * Two bpf_map_lookups (even with the same key) will have different reg->id.
3108 * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after
3109 * value_or_null->value transition, since the verifier only cares about
3110 * the range of access to valid map value pointer and doesn't care about actual
3111 * address of the map element.
3112 * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps
3113 * reg->id > 0 after value_or_null->value transition. By doing so
3114 * two bpf_map_lookups will be considered two different pointers that
3115 * point to different bpf_spin_locks.
3116 * The verifier allows taking only one bpf_spin_lock at a time to avoid
3117 * dead-locks.
3118 * Since only one bpf_spin_lock is allowed the checks are simpler than
3119 * reg_is_refcounted() logic. The verifier needs to remember only
3120 * one spin_lock instead of array of acquired_refs.
3121 * cur_state->active_spin_lock remembers which map value element got locked
3122 * and clears it after bpf_spin_unlock.
3123 */
3124static int process_spin_lock(struct bpf_verifier_env *env, int regno,
3125 bool is_lock)
3126{
3127 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
3128 struct bpf_verifier_state *cur = env->cur_state;
3129 bool is_const = tnum_is_const(reg->var_off);
3130 struct bpf_map *map = reg->map_ptr;
3131 u64 val = reg->var_off.value;
3132
3133 if (reg->type != PTR_TO_MAP_VALUE) {
3134 verbose(env, "R%d is not a pointer to map_value\n", regno);
3135 return -EINVAL;
3136 }
3137 if (!is_const) {
3138 verbose(env,
3139 "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n",
3140 regno);
3141 return -EINVAL;
3142 }
3143 if (!map->btf) {
3144 verbose(env,
3145 "map '%s' has to have BTF in order to use bpf_spin_lock\n",
3146 map->name);
3147 return -EINVAL;
3148 }
3149 if (!map_value_has_spin_lock(map)) {
3150 if (map->spin_lock_off == -E2BIG)
3151 verbose(env,
3152 "map '%s' has more than one 'struct bpf_spin_lock'\n",
3153 map->name);
3154 else if (map->spin_lock_off == -ENOENT)
3155 verbose(env,
3156 "map '%s' doesn't have 'struct bpf_spin_lock'\n",
3157 map->name);
3158 else
3159 verbose(env,
3160 "map '%s' is not a struct type or bpf_spin_lock is mangled\n",
3161 map->name);
3162 return -EINVAL;
3163 }
3164 if (map->spin_lock_off != val + reg->off) {
3165 verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n",
3166 val + reg->off);
3167 return -EINVAL;
3168 }
3169 if (is_lock) {
3170 if (cur->active_spin_lock) {
3171 verbose(env,
3172 "Locking two bpf_spin_locks are not allowed\n");
3173 return -EINVAL;
3174 }
3175 cur->active_spin_lock = reg->id;
3176 } else {
3177 if (!cur->active_spin_lock) {
3178 verbose(env, "bpf_spin_unlock without taking a lock\n");
3179 return -EINVAL;
3180 }
3181 if (cur->active_spin_lock != reg->id) {
3182 verbose(env, "bpf_spin_unlock of different lock\n");
3183 return -EINVAL;
3184 }
3185 cur->active_spin_lock = 0;
3186 }
3187 return 0;
3188}
3189
Daniel Borkmann90133412018-01-20 01:24:29 +01003190static bool arg_type_is_mem_ptr(enum bpf_arg_type type)
3191{
3192 return type == ARG_PTR_TO_MEM ||
3193 type == ARG_PTR_TO_MEM_OR_NULL ||
3194 type == ARG_PTR_TO_UNINIT_MEM;
3195}
3196
3197static bool arg_type_is_mem_size(enum bpf_arg_type type)
3198{
3199 return type == ARG_CONST_SIZE ||
3200 type == ARG_CONST_SIZE_OR_ZERO;
3201}
3202
Andrey Ignatov57c3bb72019-03-18 16:57:10 -07003203static bool arg_type_is_int_ptr(enum bpf_arg_type type)
3204{
3205 return type == ARG_PTR_TO_INT ||
3206 type == ARG_PTR_TO_LONG;
3207}
3208
3209static int int_ptr_type_to_size(enum bpf_arg_type type)
3210{
3211 if (type == ARG_PTR_TO_INT)
3212 return sizeof(u32);
3213 else if (type == ARG_PTR_TO_LONG)
3214 return sizeof(u64);
3215
3216 return -EINVAL;
3217}
3218
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003219static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003220 enum bpf_arg_type arg_type,
3221 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003222{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003223 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
Alexei Starovoitov6841de82016-08-11 18:17:16 -07003224 enum bpf_reg_type expected_type, type = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003225 int err = 0;
3226
Daniel Borkmann80f1d682015-03-12 17:21:42 +01003227 if (arg_type == ARG_DONTCARE)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003228 return 0;
3229
Edward Creedc503a82017-08-15 20:34:35 +01003230 err = check_reg_arg(env, regno, SRC_OP);
3231 if (err)
3232 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003233
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003234 if (arg_type == ARG_ANYTHING) {
3235 if (is_pointer_value(env, regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003236 verbose(env, "R%d leaks addr into helper function\n",
3237 regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003238 return -EACCES;
3239 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01003240 return 0;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003241 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01003242
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003243 if (type_is_pkt_pointer(type) &&
Thomas Graf3a0af8f2016-11-30 17:10:10 +01003244 !may_access_direct_pkt_data(env, meta, BPF_READ)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003245 verbose(env, "helper access to the packet is not allowed\n");
Alexei Starovoitov6841de82016-08-11 18:17:16 -07003246 return -EACCES;
3247 }
3248
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01003249 if (arg_type == ARG_PTR_TO_MAP_KEY ||
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02003250 arg_type == ARG_PTR_TO_MAP_VALUE ||
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -07003251 arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE ||
3252 arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003253 expected_type = PTR_TO_STACK;
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -07003254 if (register_is_null(reg) &&
3255 arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL)
3256 /* final test in check_stack_boundary() */;
3257 else if (!type_is_pkt_pointer(type) &&
3258 type != PTR_TO_MAP_VALUE &&
3259 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07003260 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08003261 } else if (arg_type == ARG_CONST_SIZE ||
3262 arg_type == ARG_CONST_SIZE_OR_ZERO) {
Edward Creef1174f72017-08-07 15:26:19 +01003263 expected_type = SCALAR_VALUE;
3264 if (type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07003265 goto err_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003266 } else if (arg_type == ARG_CONST_MAP_PTR) {
3267 expected_type = CONST_PTR_TO_MAP;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07003268 if (type != expected_type)
3269 goto err_type;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07003270 } else if (arg_type == ARG_PTR_TO_CTX) {
3271 expected_type = PTR_TO_CTX;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07003272 if (type != expected_type)
3273 goto err_type;
Daniel Borkmann58990d12018-06-07 17:40:03 +02003274 err = check_ctx_reg(env, reg, regno);
3275 if (err < 0)
3276 return err;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08003277 } else if (arg_type == ARG_PTR_TO_SOCK_COMMON) {
3278 expected_type = PTR_TO_SOCK_COMMON;
3279 /* Any sk pointer can be ARG_PTR_TO_SOCK_COMMON */
3280 if (!type_is_sk_pointer(type))
3281 goto err_type;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003282 if (reg->ref_obj_id) {
3283 if (meta->ref_obj_id) {
3284 verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n",
3285 regno, reg->ref_obj_id,
3286 meta->ref_obj_id);
3287 return -EFAULT;
3288 }
3289 meta->ref_obj_id = reg->ref_obj_id;
Joe Stringerfd978bf72018-10-02 13:35:35 -07003290 }
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -07003291 } else if (arg_type == ARG_PTR_TO_SOCKET) {
3292 expected_type = PTR_TO_SOCKET;
3293 if (type != expected_type)
3294 goto err_type;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08003295 } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) {
3296 if (meta->func_id == BPF_FUNC_spin_lock) {
3297 if (process_spin_lock(env, regno, true))
3298 return -EACCES;
3299 } else if (meta->func_id == BPF_FUNC_spin_unlock) {
3300 if (process_spin_lock(env, regno, false))
3301 return -EACCES;
3302 } else {
3303 verbose(env, "verifier internal error\n");
3304 return -EFAULT;
3305 }
Daniel Borkmann90133412018-01-20 01:24:29 +01003306 } else if (arg_type_is_mem_ptr(arg_type)) {
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01003307 expected_type = PTR_TO_STACK;
3308 /* One exception here. In case function allows for NULL to be
Edward Creef1174f72017-08-07 15:26:19 +01003309 * passed in as argument, it's a SCALAR_VALUE type. Final test
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01003310 * happens during stack boundary checking.
3311 */
Alexei Starovoitov914cb782017-11-30 21:31:40 -08003312 if (register_is_null(reg) &&
Gianluca Borellodb1ac492017-11-22 18:32:53 +00003313 arg_type == ARG_PTR_TO_MEM_OR_NULL)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07003314 /* final test in check_stack_boundary() */;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003315 else if (!type_is_pkt_pointer(type) &&
3316 type != PTR_TO_MAP_VALUE &&
Edward Creef1174f72017-08-07 15:26:19 +01003317 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07003318 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08003319 meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM;
Andrey Ignatov57c3bb72019-03-18 16:57:10 -07003320 } else if (arg_type_is_int_ptr(arg_type)) {
3321 expected_type = PTR_TO_STACK;
3322 if (!type_is_pkt_pointer(type) &&
3323 type != PTR_TO_MAP_VALUE &&
3324 type != expected_type)
3325 goto err_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003326 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003327 verbose(env, "unsupported arg_type %d\n", arg_type);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003328 return -EFAULT;
3329 }
3330
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003331 if (arg_type == ARG_CONST_MAP_PTR) {
3332 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003333 meta->map_ptr = reg->map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003334 } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
3335 /* bpf_map_xxx(..., map_ptr, ..., key) call:
3336 * check that [key, key + map->key_size) are within
3337 * stack limits and initialized
3338 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003339 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003340 /* in function declaration map_ptr must come before
3341 * map_key, so that it's verified and known before
3342 * we have to check map_key here. Otherwise it means
3343 * that kernel subsystem misconfigured verifier
3344 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003345 verbose(env, "invalid map_ptr to access map->key\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003346 return -EACCES;
3347 }
Paul Chaignond71962f2018-04-24 15:07:54 +02003348 err = check_helper_mem_access(env, regno,
3349 meta->map_ptr->key_size, false,
3350 NULL);
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02003351 } else if (arg_type == ARG_PTR_TO_MAP_VALUE ||
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -07003352 (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL &&
3353 !register_is_null(reg)) ||
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02003354 arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003355 /* bpf_map_xxx(..., map_ptr, ..., value) call:
3356 * check [value, value + map->value_size) validity
3357 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003358 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003359 /* kernel subsystem misconfigured verifier */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003360 verbose(env, "invalid map_ptr to access map->value\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003361 return -EACCES;
3362 }
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02003363 meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE);
Paul Chaignond71962f2018-04-24 15:07:54 +02003364 err = check_helper_mem_access(env, regno,
3365 meta->map_ptr->value_size, false,
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02003366 meta);
Daniel Borkmann90133412018-01-20 01:24:29 +01003367 } else if (arg_type_is_mem_size(arg_type)) {
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08003368 bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003369
Yonghong Song849fa502018-04-28 22:28:09 -07003370 /* remember the mem_size which may be used later
3371 * to refine return values.
3372 */
3373 meta->msize_smax_value = reg->smax_value;
3374 meta->msize_umax_value = reg->umax_value;
3375
Edward Creef1174f72017-08-07 15:26:19 +01003376 /* The register is SCALAR_VALUE; the access check
3377 * happens using its boundaries.
Gianluca Borello06c1c042017-01-09 10:19:49 -08003378 */
Edward Creef1174f72017-08-07 15:26:19 +01003379 if (!tnum_is_const(reg->var_off))
Gianluca Borello06c1c042017-01-09 10:19:49 -08003380 /* For unprivileged variable accesses, disable raw
3381 * mode so that the program is required to
3382 * initialize all the memory that the helper could
3383 * just partially fill up.
3384 */
3385 meta = NULL;
3386
Edward Creeb03c9f92017-08-07 15:26:36 +01003387 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003388 verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n",
Edward Creef1174f72017-08-07 15:26:19 +01003389 regno);
3390 return -EACCES;
3391 }
Gianluca Borello06c1c042017-01-09 10:19:49 -08003392
Edward Creeb03c9f92017-08-07 15:26:36 +01003393 if (reg->umin_value == 0) {
Edward Creef1174f72017-08-07 15:26:19 +01003394 err = check_helper_mem_access(env, regno - 1, 0,
3395 zero_size_allowed,
3396 meta);
Gianluca Borello06c1c042017-01-09 10:19:49 -08003397 if (err)
3398 return err;
Gianluca Borello06c1c042017-01-09 10:19:49 -08003399 }
Edward Creef1174f72017-08-07 15:26:19 +01003400
Edward Creeb03c9f92017-08-07 15:26:36 +01003401 if (reg->umax_value >= BPF_MAX_VAR_SIZ) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003402 verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
Edward Creef1174f72017-08-07 15:26:19 +01003403 regno);
3404 return -EACCES;
3405 }
3406 err = check_helper_mem_access(env, regno - 1,
Edward Creeb03c9f92017-08-07 15:26:36 +01003407 reg->umax_value,
Edward Creef1174f72017-08-07 15:26:19 +01003408 zero_size_allowed, meta);
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07003409 if (!err)
3410 err = mark_chain_precision(env, regno);
Andrey Ignatov57c3bb72019-03-18 16:57:10 -07003411 } else if (arg_type_is_int_ptr(arg_type)) {
3412 int size = int_ptr_type_to_size(arg_type);
3413
3414 err = check_helper_mem_access(env, regno, size, false, meta);
3415 if (err)
3416 return err;
3417 err = check_ptr_alignment(env, reg, 0, size, true);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003418 }
3419
3420 return err;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07003421err_type:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003422 verbose(env, "R%d type=%s expected=%s\n", regno,
Alexei Starovoitov6841de82016-08-11 18:17:16 -07003423 reg_type_str[type], reg_type_str[expected_type]);
3424 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003425}
3426
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003427static int check_map_func_compatibility(struct bpf_verifier_env *env,
3428 struct bpf_map *map, int func_id)
Kaixu Xia35578d72015-08-06 07:02:35 +00003429{
Kaixu Xia35578d72015-08-06 07:02:35 +00003430 if (!map)
3431 return 0;
3432
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07003433 /* We need a two way check, first is from map perspective ... */
3434 switch (map->map_type) {
3435 case BPF_MAP_TYPE_PROG_ARRAY:
3436 if (func_id != BPF_FUNC_tail_call)
3437 goto error;
3438 break;
3439 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
3440 if (func_id != BPF_FUNC_perf_event_read &&
Yonghong Song908432c2017-10-05 09:19:20 -07003441 func_id != BPF_FUNC_perf_event_output &&
3442 func_id != BPF_FUNC_perf_event_read_value)
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07003443 goto error;
3444 break;
3445 case BPF_MAP_TYPE_STACK_TRACE:
3446 if (func_id != BPF_FUNC_get_stackid)
3447 goto error;
3448 break;
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -07003449 case BPF_MAP_TYPE_CGROUP_ARRAY:
David S. Miller60747ef2016-08-18 01:17:32 -04003450 if (func_id != BPF_FUNC_skb_under_cgroup &&
Sargun Dhillon60d20f92016-08-12 08:56:52 -07003451 func_id != BPF_FUNC_current_task_under_cgroup)
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07003452 goto error;
3453 break;
Roman Gushchincd339432018-08-02 14:27:24 -07003454 case BPF_MAP_TYPE_CGROUP_STORAGE:
Roman Gushchinb741f162018-09-28 14:45:43 +00003455 case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE:
Roman Gushchincd339432018-08-02 14:27:24 -07003456 if (func_id != BPF_FUNC_get_local_storage)
3457 goto error;
3458 break;
John Fastabend546ac1f2017-07-17 09:28:56 -07003459 case BPF_MAP_TYPE_DEVMAP:
Toke Høiland-Jørgensen0cdbb4b2019-06-28 11:12:35 +02003460 if (func_id != BPF_FUNC_redirect_map &&
3461 func_id != BPF_FUNC_map_lookup_elem)
John Fastabend546ac1f2017-07-17 09:28:56 -07003462 goto error;
3463 break;
Björn Töpelfbfc504a2018-05-02 13:01:28 +02003464 /* Restrict bpf side of cpumap and xskmap, open when use-cases
3465 * appear.
3466 */
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02003467 case BPF_MAP_TYPE_CPUMAP:
3468 if (func_id != BPF_FUNC_redirect_map)
3469 goto error;
3470 break;
Jonathan Lemonfada7fd2019-06-06 13:59:40 -07003471 case BPF_MAP_TYPE_XSKMAP:
3472 if (func_id != BPF_FUNC_redirect_map &&
3473 func_id != BPF_FUNC_map_lookup_elem)
3474 goto error;
3475 break;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07003476 case BPF_MAP_TYPE_ARRAY_OF_MAPS:
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07003477 case BPF_MAP_TYPE_HASH_OF_MAPS:
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07003478 if (func_id != BPF_FUNC_map_lookup_elem)
3479 goto error;
Martin KaFai Lau16a43622017-08-17 18:14:43 -07003480 break;
John Fastabend174a79f2017-08-15 22:32:47 -07003481 case BPF_MAP_TYPE_SOCKMAP:
3482 if (func_id != BPF_FUNC_sk_redirect_map &&
3483 func_id != BPF_FUNC_sock_map_update &&
John Fastabend4f738ad2018-03-18 12:57:10 -07003484 func_id != BPF_FUNC_map_delete_elem &&
3485 func_id != BPF_FUNC_msg_redirect_map)
John Fastabend174a79f2017-08-15 22:32:47 -07003486 goto error;
3487 break;
John Fastabend81110382018-05-14 10:00:17 -07003488 case BPF_MAP_TYPE_SOCKHASH:
3489 if (func_id != BPF_FUNC_sk_redirect_hash &&
3490 func_id != BPF_FUNC_sock_hash_update &&
3491 func_id != BPF_FUNC_map_delete_elem &&
3492 func_id != BPF_FUNC_msg_redirect_hash)
3493 goto error;
3494 break;
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07003495 case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY:
3496 if (func_id != BPF_FUNC_sk_select_reuseport)
3497 goto error;
3498 break;
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02003499 case BPF_MAP_TYPE_QUEUE:
3500 case BPF_MAP_TYPE_STACK:
3501 if (func_id != BPF_FUNC_map_peek_elem &&
3502 func_id != BPF_FUNC_map_pop_elem &&
3503 func_id != BPF_FUNC_map_push_elem)
3504 goto error;
3505 break;
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -07003506 case BPF_MAP_TYPE_SK_STORAGE:
3507 if (func_id != BPF_FUNC_sk_storage_get &&
3508 func_id != BPF_FUNC_sk_storage_delete)
3509 goto error;
3510 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07003511 default:
3512 break;
3513 }
3514
3515 /* ... and second from the function itself. */
3516 switch (func_id) {
3517 case BPF_FUNC_tail_call:
3518 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
3519 goto error;
Jiong Wangf910cef2018-05-02 16:17:17 -04003520 if (env->subprog_cnt > 1) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003521 verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n");
3522 return -EINVAL;
3523 }
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07003524 break;
3525 case BPF_FUNC_perf_event_read:
3526 case BPF_FUNC_perf_event_output:
Yonghong Song908432c2017-10-05 09:19:20 -07003527 case BPF_FUNC_perf_event_read_value:
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07003528 if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
3529 goto error;
3530 break;
3531 case BPF_FUNC_get_stackid:
3532 if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
3533 goto error;
3534 break;
Sargun Dhillon60d20f92016-08-12 08:56:52 -07003535 case BPF_FUNC_current_task_under_cgroup:
Daniel Borkmann747ea552016-08-12 22:17:17 +02003536 case BPF_FUNC_skb_under_cgroup:
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07003537 if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
3538 goto error;
3539 break;
John Fastabend97f91a72017-07-17 09:29:18 -07003540 case BPF_FUNC_redirect_map:
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02003541 if (map->map_type != BPF_MAP_TYPE_DEVMAP &&
Björn Töpelfbfc504a2018-05-02 13:01:28 +02003542 map->map_type != BPF_MAP_TYPE_CPUMAP &&
3543 map->map_type != BPF_MAP_TYPE_XSKMAP)
John Fastabend97f91a72017-07-17 09:29:18 -07003544 goto error;
3545 break;
John Fastabend174a79f2017-08-15 22:32:47 -07003546 case BPF_FUNC_sk_redirect_map:
John Fastabend4f738ad2018-03-18 12:57:10 -07003547 case BPF_FUNC_msg_redirect_map:
John Fastabend81110382018-05-14 10:00:17 -07003548 case BPF_FUNC_sock_map_update:
John Fastabend174a79f2017-08-15 22:32:47 -07003549 if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
3550 goto error;
3551 break;
John Fastabend81110382018-05-14 10:00:17 -07003552 case BPF_FUNC_sk_redirect_hash:
3553 case BPF_FUNC_msg_redirect_hash:
3554 case BPF_FUNC_sock_hash_update:
3555 if (map->map_type != BPF_MAP_TYPE_SOCKHASH)
John Fastabend174a79f2017-08-15 22:32:47 -07003556 goto error;
3557 break;
Roman Gushchincd339432018-08-02 14:27:24 -07003558 case BPF_FUNC_get_local_storage:
Roman Gushchinb741f162018-09-28 14:45:43 +00003559 if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
3560 map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Roman Gushchincd339432018-08-02 14:27:24 -07003561 goto error;
3562 break;
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07003563 case BPF_FUNC_sk_select_reuseport:
3564 if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY)
3565 goto error;
3566 break;
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02003567 case BPF_FUNC_map_peek_elem:
3568 case BPF_FUNC_map_pop_elem:
3569 case BPF_FUNC_map_push_elem:
3570 if (map->map_type != BPF_MAP_TYPE_QUEUE &&
3571 map->map_type != BPF_MAP_TYPE_STACK)
3572 goto error;
3573 break;
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -07003574 case BPF_FUNC_sk_storage_get:
3575 case BPF_FUNC_sk_storage_delete:
3576 if (map->map_type != BPF_MAP_TYPE_SK_STORAGE)
3577 goto error;
3578 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07003579 default:
3580 break;
Kaixu Xia35578d72015-08-06 07:02:35 +00003581 }
3582
3583 return 0;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07003584error:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003585 verbose(env, "cannot pass map_type %d into func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02003586 map->map_type, func_id_name(func_id), func_id);
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07003587 return -EINVAL;
Kaixu Xia35578d72015-08-06 07:02:35 +00003588}
3589
Daniel Borkmann90133412018-01-20 01:24:29 +01003590static bool check_raw_mode_ok(const struct bpf_func_proto *fn)
Daniel Borkmann435faee12016-04-13 00:10:51 +02003591{
3592 int count = 0;
3593
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08003594 if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02003595 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08003596 if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02003597 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08003598 if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02003599 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08003600 if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02003601 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08003602 if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02003603 count++;
3604
Daniel Borkmann90133412018-01-20 01:24:29 +01003605 /* We only support one arg being in raw mode at the moment,
3606 * which is sufficient for the helper functions we have
3607 * right now.
3608 */
3609 return count <= 1;
3610}
3611
3612static bool check_args_pair_invalid(enum bpf_arg_type arg_curr,
3613 enum bpf_arg_type arg_next)
3614{
3615 return (arg_type_is_mem_ptr(arg_curr) &&
3616 !arg_type_is_mem_size(arg_next)) ||
3617 (!arg_type_is_mem_ptr(arg_curr) &&
3618 arg_type_is_mem_size(arg_next));
3619}
3620
3621static bool check_arg_pair_ok(const struct bpf_func_proto *fn)
3622{
3623 /* bpf_xxx(..., buf, len) call will access 'len'
3624 * bytes from memory 'buf'. Both arg types need
3625 * to be paired, so make sure there's no buggy
3626 * helper function specification.
3627 */
3628 if (arg_type_is_mem_size(fn->arg1_type) ||
3629 arg_type_is_mem_ptr(fn->arg5_type) ||
3630 check_args_pair_invalid(fn->arg1_type, fn->arg2_type) ||
3631 check_args_pair_invalid(fn->arg2_type, fn->arg3_type) ||
3632 check_args_pair_invalid(fn->arg3_type, fn->arg4_type) ||
3633 check_args_pair_invalid(fn->arg4_type, fn->arg5_type))
3634 return false;
3635
3636 return true;
3637}
3638
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003639static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07003640{
3641 int count = 0;
3642
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003643 if (arg_type_may_be_refcounted(fn->arg1_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07003644 count++;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003645 if (arg_type_may_be_refcounted(fn->arg2_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07003646 count++;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003647 if (arg_type_may_be_refcounted(fn->arg3_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07003648 count++;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003649 if (arg_type_may_be_refcounted(fn->arg4_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07003650 count++;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003651 if (arg_type_may_be_refcounted(fn->arg5_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07003652 count++;
3653
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003654 /* A reference acquiring function cannot acquire
3655 * another refcounted ptr.
3656 */
3657 if (is_acquire_function(func_id) && count)
3658 return false;
3659
Joe Stringerfd978bf72018-10-02 13:35:35 -07003660 /* We only support one arg being unreferenced at the moment,
3661 * which is sufficient for the helper functions we have right now.
3662 */
3663 return count <= 1;
3664}
3665
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003666static int check_func_proto(const struct bpf_func_proto *fn, int func_id)
Daniel Borkmann90133412018-01-20 01:24:29 +01003667{
3668 return check_raw_mode_ok(fn) &&
Joe Stringerfd978bf72018-10-02 13:35:35 -07003669 check_arg_pair_ok(fn) &&
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003670 check_refcount_ok(fn, func_id) ? 0 : -EINVAL;
Daniel Borkmann435faee12016-04-13 00:10:51 +02003671}
3672
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003673/* Packet data might have moved, any old PTR_TO_PACKET[_META,_END]
3674 * are now invalid, so turn them into unknown SCALAR_VALUE.
Edward Creef1174f72017-08-07 15:26:19 +01003675 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003676static void __clear_all_pkt_pointers(struct bpf_verifier_env *env,
3677 struct bpf_func_state *state)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003678{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003679 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003680 int i;
3681
3682 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003683 if (reg_is_pkt_pointer_any(&regs[i]))
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003684 mark_reg_unknown(env, regs, i);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003685
Joe Stringerf3709f62018-10-02 13:35:29 -07003686 bpf_for_each_spilled_reg(i, state, reg) {
3687 if (!reg)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003688 continue;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003689 if (reg_is_pkt_pointer_any(reg))
3690 __mark_reg_unknown(reg);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003691 }
3692}
3693
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003694static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
3695{
3696 struct bpf_verifier_state *vstate = env->cur_state;
3697 int i;
3698
3699 for (i = 0; i <= vstate->curframe; i++)
3700 __clear_all_pkt_pointers(env, vstate->frame[i]);
3701}
3702
Joe Stringerfd978bf72018-10-02 13:35:35 -07003703static void release_reg_references(struct bpf_verifier_env *env,
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003704 struct bpf_func_state *state,
3705 int ref_obj_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07003706{
3707 struct bpf_reg_state *regs = state->regs, *reg;
3708 int i;
3709
3710 for (i = 0; i < MAX_BPF_REG; i++)
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003711 if (regs[i].ref_obj_id == ref_obj_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07003712 mark_reg_unknown(env, regs, i);
3713
3714 bpf_for_each_spilled_reg(i, state, reg) {
3715 if (!reg)
3716 continue;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003717 if (reg->ref_obj_id == ref_obj_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07003718 __mark_reg_unknown(reg);
3719 }
3720}
3721
3722/* The pointer with the specified id has released its reference to kernel
3723 * resources. Identify all copies of the same pointer and clear the reference.
3724 */
3725static int release_reference(struct bpf_verifier_env *env,
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003726 int ref_obj_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07003727{
3728 struct bpf_verifier_state *vstate = env->cur_state;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003729 int err;
Joe Stringerfd978bf72018-10-02 13:35:35 -07003730 int i;
3731
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003732 err = release_reference_state(cur_func(env), ref_obj_id);
3733 if (err)
3734 return err;
Joe Stringerfd978bf72018-10-02 13:35:35 -07003735
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003736 for (i = 0; i <= vstate->curframe; i++)
3737 release_reg_references(env, vstate->frame[i], ref_obj_id);
3738
3739 return 0;
Joe Stringerfd978bf72018-10-02 13:35:35 -07003740}
3741
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003742static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
3743 int *insn_idx)
3744{
3745 struct bpf_verifier_state *state = env->cur_state;
3746 struct bpf_func_state *caller, *callee;
Joe Stringerfd978bf72018-10-02 13:35:35 -07003747 int i, err, subprog, target_insn;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003748
Alexei Starovoitovaada9ce2017-12-25 13:15:42 -08003749 if (state->curframe + 1 >= MAX_CALL_FRAMES) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003750 verbose(env, "the call stack of %d frames is too deep\n",
Alexei Starovoitovaada9ce2017-12-25 13:15:42 -08003751 state->curframe + 2);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003752 return -E2BIG;
3753 }
3754
3755 target_insn = *insn_idx + insn->imm;
3756 subprog = find_subprog(env, target_insn + 1);
3757 if (subprog < 0) {
3758 verbose(env, "verifier bug. No program starts at insn %d\n",
3759 target_insn + 1);
3760 return -EFAULT;
3761 }
3762
3763 caller = state->frame[state->curframe];
3764 if (state->frame[state->curframe + 1]) {
3765 verbose(env, "verifier bug. Frame %d already allocated\n",
3766 state->curframe + 1);
3767 return -EFAULT;
3768 }
3769
3770 callee = kzalloc(sizeof(*callee), GFP_KERNEL);
3771 if (!callee)
3772 return -ENOMEM;
3773 state->frame[state->curframe + 1] = callee;
3774
3775 /* callee cannot access r0, r6 - r9 for reading and has to write
3776 * into its own stack before reading from it.
3777 * callee can read/write into caller's stack
3778 */
3779 init_func_state(env, callee,
3780 /* remember the callsite, it will be used by bpf_exit */
3781 *insn_idx /* callsite */,
3782 state->curframe + 1 /* frameno within this callchain */,
Jiong Wangf910cef2018-05-02 16:17:17 -04003783 subprog /* subprog number within this prog */);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003784
Joe Stringerfd978bf72018-10-02 13:35:35 -07003785 /* Transfer references to the callee */
3786 err = transfer_reference_state(callee, caller);
3787 if (err)
3788 return err;
3789
Edward Cree679c7822018-08-22 20:02:19 +01003790 /* copy r1 - r5 args that callee can access. The copy includes parent
3791 * pointers, which connects us up to the liveness chain
3792 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003793 for (i = BPF_REG_1; i <= BPF_REG_5; i++)
3794 callee->regs[i] = caller->regs[i];
3795
Edward Cree679c7822018-08-22 20:02:19 +01003796 /* after the call registers r0 - r5 were scratched */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003797 for (i = 0; i < CALLER_SAVED_REGS; i++) {
3798 mark_reg_not_init(env, caller->regs, caller_saved[i]);
3799 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
3800 }
3801
3802 /* only increment it after check_reg_arg() finished */
3803 state->curframe++;
3804
3805 /* and go analyze first insn of the callee */
3806 *insn_idx = target_insn;
3807
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07003808 if (env->log.level & BPF_LOG_LEVEL) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003809 verbose(env, "caller:\n");
3810 print_verifier_state(env, caller);
3811 verbose(env, "callee:\n");
3812 print_verifier_state(env, callee);
3813 }
3814 return 0;
3815}
3816
3817static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
3818{
3819 struct bpf_verifier_state *state = env->cur_state;
3820 struct bpf_func_state *caller, *callee;
3821 struct bpf_reg_state *r0;
Joe Stringerfd978bf72018-10-02 13:35:35 -07003822 int err;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003823
3824 callee = state->frame[state->curframe];
3825 r0 = &callee->regs[BPF_REG_0];
3826 if (r0->type == PTR_TO_STACK) {
3827 /* technically it's ok to return caller's stack pointer
3828 * (or caller's caller's pointer) back to the caller,
3829 * since these pointers are valid. Only current stack
3830 * pointer will be invalid as soon as function exits,
3831 * but let's be conservative
3832 */
3833 verbose(env, "cannot return stack pointer to the caller\n");
3834 return -EINVAL;
3835 }
3836
3837 state->curframe--;
3838 caller = state->frame[state->curframe];
3839 /* return to the caller whatever r0 had in the callee */
3840 caller->regs[BPF_REG_0] = *r0;
3841
Joe Stringerfd978bf72018-10-02 13:35:35 -07003842 /* Transfer references to the caller */
3843 err = transfer_reference_state(caller, callee);
3844 if (err)
3845 return err;
3846
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003847 *insn_idx = callee->callsite + 1;
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07003848 if (env->log.level & BPF_LOG_LEVEL) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003849 verbose(env, "returning from callee:\n");
3850 print_verifier_state(env, callee);
3851 verbose(env, "to caller at %d:\n", *insn_idx);
3852 print_verifier_state(env, caller);
3853 }
3854 /* clear everything in the callee */
3855 free_func_state(callee);
3856 state->frame[state->curframe + 1] = NULL;
3857 return 0;
3858}
3859
Yonghong Song849fa502018-04-28 22:28:09 -07003860static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type,
3861 int func_id,
3862 struct bpf_call_arg_meta *meta)
3863{
3864 struct bpf_reg_state *ret_reg = &regs[BPF_REG_0];
3865
3866 if (ret_type != RET_INTEGER ||
3867 (func_id != BPF_FUNC_get_stack &&
3868 func_id != BPF_FUNC_probe_read_str))
3869 return;
3870
3871 ret_reg->smax_value = meta->msize_smax_value;
3872 ret_reg->umax_value = meta->msize_umax_value;
3873 __reg_deduce_bounds(ret_reg);
3874 __reg_bound_offset(ret_reg);
3875}
3876
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003877static int
3878record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,
3879 int func_id, int insn_idx)
3880{
3881 struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx];
Daniel Borkmann591fe982019-04-09 23:20:05 +02003882 struct bpf_map *map = meta->map_ptr;
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003883
3884 if (func_id != BPF_FUNC_tail_call &&
Daniel Borkmann09772d92018-06-02 23:06:35 +02003885 func_id != BPF_FUNC_map_lookup_elem &&
3886 func_id != BPF_FUNC_map_update_elem &&
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02003887 func_id != BPF_FUNC_map_delete_elem &&
3888 func_id != BPF_FUNC_map_push_elem &&
3889 func_id != BPF_FUNC_map_pop_elem &&
3890 func_id != BPF_FUNC_map_peek_elem)
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003891 return 0;
Daniel Borkmann09772d92018-06-02 23:06:35 +02003892
Daniel Borkmann591fe982019-04-09 23:20:05 +02003893 if (map == NULL) {
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003894 verbose(env, "kernel subsystem misconfigured verifier\n");
3895 return -EINVAL;
3896 }
3897
Daniel Borkmann591fe982019-04-09 23:20:05 +02003898 /* In case of read-only, some additional restrictions
3899 * need to be applied in order to prevent altering the
3900 * state of the map from program side.
3901 */
3902 if ((map->map_flags & BPF_F_RDONLY_PROG) &&
3903 (func_id == BPF_FUNC_map_delete_elem ||
3904 func_id == BPF_FUNC_map_update_elem ||
3905 func_id == BPF_FUNC_map_push_elem ||
3906 func_id == BPF_FUNC_map_pop_elem)) {
3907 verbose(env, "write into map forbidden\n");
3908 return -EACCES;
3909 }
3910
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003911 if (!BPF_MAP_PTR(aux->map_state))
3912 bpf_map_ptr_store(aux, meta->map_ptr,
3913 meta->map_ptr->unpriv_array);
3914 else if (BPF_MAP_PTR(aux->map_state) != meta->map_ptr)
3915 bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON,
3916 meta->map_ptr->unpriv_array);
3917 return 0;
3918}
3919
Joe Stringerfd978bf72018-10-02 13:35:35 -07003920static int check_reference_leak(struct bpf_verifier_env *env)
3921{
3922 struct bpf_func_state *state = cur_func(env);
3923 int i;
3924
3925 for (i = 0; i < state->acquired_refs; i++) {
3926 verbose(env, "Unreleased reference id=%d alloc_insn=%d\n",
3927 state->refs[i].id, state->refs[i].insn_idx);
3928 }
3929 return state->acquired_refs ? -EINVAL : 0;
3930}
3931
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003932static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003933{
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003934 const struct bpf_func_proto *fn = NULL;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003935 struct bpf_reg_state *regs;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003936 struct bpf_call_arg_meta meta;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003937 bool changes_data;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003938 int i, err;
3939
3940 /* find function prototype */
3941 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003942 verbose(env, "invalid func %s#%d\n", func_id_name(func_id),
3943 func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003944 return -EINVAL;
3945 }
3946
Jakub Kicinski00176a32017-10-16 16:40:54 -07003947 if (env->ops->get_func_proto)
Andrey Ignatov5e43f892018-03-30 15:08:00 -07003948 fn = env->ops->get_func_proto(func_id, env->prog);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003949 if (!fn) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003950 verbose(env, "unknown func %s#%d\n", func_id_name(func_id),
3951 func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003952 return -EINVAL;
3953 }
3954
3955 /* eBPF programs must be GPL compatible to use GPL-ed functions */
Daniel Borkmann24701ec2015-03-01 12:31:47 +01003956 if (!env->prog->gpl_compatible && fn->gpl_only) {
Daniel Borkmann3fe28672018-06-02 23:06:33 +02003957 verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003958 return -EINVAL;
3959 }
3960
Daniel Borkmann04514d12017-12-14 21:07:25 +01003961 /* With LD_ABS/IND some JITs save/restore skb from r1. */
Martin KaFai Lau17bedab2016-12-07 15:53:11 -08003962 changes_data = bpf_helper_changes_pkt_data(fn->func);
Daniel Borkmann04514d12017-12-14 21:07:25 +01003963 if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) {
3964 verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n",
3965 func_id_name(func_id), func_id);
3966 return -EINVAL;
3967 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003968
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003969 memset(&meta, 0, sizeof(meta));
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003970 meta.pkt_access = fn->pkt_access;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003971
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003972 err = check_func_proto(fn, func_id);
Daniel Borkmann435faee12016-04-13 00:10:51 +02003973 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003974 verbose(env, "kernel subsystem misconfigured func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02003975 func_id_name(func_id), func_id);
Daniel Borkmann435faee12016-04-13 00:10:51 +02003976 return err;
3977 }
3978
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08003979 meta.func_id = func_id;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003980 /* check args */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003981 err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003982 if (err)
3983 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003984 err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003985 if (err)
3986 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003987 err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003988 if (err)
3989 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003990 err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003991 if (err)
3992 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003993 err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003994 if (err)
3995 return err;
3996
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003997 err = record_func_map(env, &meta, func_id, insn_idx);
3998 if (err)
3999 return err;
4000
Daniel Borkmann435faee12016-04-13 00:10:51 +02004001 /* Mark slots with STACK_MISC in case of raw mode, stack offset
4002 * is inferred from register state.
4003 */
4004 for (i = 0; i < meta.access_size; i++) {
Daniel Borkmannca369602018-02-23 22:29:05 +01004005 err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B,
4006 BPF_WRITE, -1, false);
Daniel Borkmann435faee12016-04-13 00:10:51 +02004007 if (err)
4008 return err;
4009 }
4010
Joe Stringerfd978bf72018-10-02 13:35:35 -07004011 if (func_id == BPF_FUNC_tail_call) {
4012 err = check_reference_leak(env);
4013 if (err) {
4014 verbose(env, "tail_call would lead to reference leak\n");
4015 return err;
4016 }
4017 } else if (is_release_function(func_id)) {
Martin KaFai Lau1b986582019-03-12 10:23:02 -07004018 err = release_reference(env, meta.ref_obj_id);
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08004019 if (err) {
4020 verbose(env, "func %s#%d reference has not been acquired before\n",
4021 func_id_name(func_id), func_id);
Joe Stringerfd978bf72018-10-02 13:35:35 -07004022 return err;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08004023 }
Joe Stringerfd978bf72018-10-02 13:35:35 -07004024 }
4025
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004026 regs = cur_regs(env);
Roman Gushchincd339432018-08-02 14:27:24 -07004027
4028 /* check that flags argument in get_local_storage(map, flags) is 0,
4029 * this is required because get_local_storage() can't return an error.
4030 */
4031 if (func_id == BPF_FUNC_get_local_storage &&
4032 !register_is_null(&regs[BPF_REG_2])) {
4033 verbose(env, "get_local_storage() doesn't support non-zero flags\n");
4034 return -EINVAL;
4035 }
4036
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004037 /* reset caller saved regs */
Edward Creedc503a82017-08-15 20:34:35 +01004038 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004039 mark_reg_not_init(env, regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01004040 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
4041 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004042
Jiong Wang5327ed32019-05-24 23:25:12 +01004043 /* helper call returns 64-bit value. */
4044 regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG;
4045
Edward Creedc503a82017-08-15 20:34:35 +01004046 /* update return register (already marked as written above) */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004047 if (fn->ret_type == RET_INTEGER) {
Edward Creef1174f72017-08-07 15:26:19 +01004048 /* sets type to SCALAR_VALUE */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004049 mark_reg_unknown(env, regs, BPF_REG_0);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004050 } else if (fn->ret_type == RET_VOID) {
4051 regs[BPF_REG_0].type = NOT_INIT;
Roman Gushchin3e6a4b32018-08-02 14:27:22 -07004052 } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL ||
4053 fn->ret_type == RET_PTR_TO_MAP_VALUE) {
Edward Creef1174f72017-08-07 15:26:19 +01004054 /* There is no offset yet applied, variable or fixed */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004055 mark_reg_known_zero(env, regs, BPF_REG_0);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004056 /* remember map_ptr, so that check_map_access()
4057 * can check 'value_size' boundary of memory access
4058 * to map element returned from bpf_map_lookup_elem()
4059 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02004060 if (meta.map_ptr == NULL) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004061 verbose(env,
4062 "kernel subsystem misconfigured verifier\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004063 return -EINVAL;
4064 }
Daniel Borkmann33ff9822016-04-13 00:10:50 +02004065 regs[BPF_REG_0].map_ptr = meta.map_ptr;
Daniel Borkmann4d31f302018-11-01 00:05:53 +01004066 if (fn->ret_type == RET_PTR_TO_MAP_VALUE) {
4067 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE;
Alexei Starovoitove16d2f12019-01-31 15:40:05 -08004068 if (map_value_has_spin_lock(meta.map_ptr))
4069 regs[BPF_REG_0].id = ++env->id_gen;
Daniel Borkmann4d31f302018-11-01 00:05:53 +01004070 } else {
4071 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
4072 regs[BPF_REG_0].id = ++env->id_gen;
4073 }
Joe Stringerc64b7982018-10-02 13:35:33 -07004074 } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) {
4075 mark_reg_known_zero(env, regs, BPF_REG_0);
4076 regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL;
Lorenz Bauer0f3adc22019-03-22 09:53:59 +08004077 regs[BPF_REG_0].id = ++env->id_gen;
Lorenz Bauer85a51f82019-03-22 09:54:00 +08004078 } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) {
4079 mark_reg_known_zero(env, regs, BPF_REG_0);
4080 regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL;
4081 regs[BPF_REG_0].id = ++env->id_gen;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08004082 } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) {
4083 mark_reg_known_zero(env, regs, BPF_REG_0);
4084 regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL;
4085 regs[BPF_REG_0].id = ++env->id_gen;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004086 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004087 verbose(env, "unknown return type %d of func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02004088 fn->ret_type, func_id_name(func_id), func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004089 return -EINVAL;
4090 }
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07004091
Lorenz Bauer0f3adc22019-03-22 09:53:59 +08004092 if (is_ptr_cast_function(func_id)) {
Martin KaFai Lau1b986582019-03-12 10:23:02 -07004093 /* For release_reference() */
4094 regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id;
Lorenz Bauer0f3adc22019-03-22 09:53:59 +08004095 } else if (is_acquire_function(func_id)) {
4096 int id = acquire_reference_state(env, insn_idx);
4097
4098 if (id < 0)
4099 return id;
4100 /* For mark_ptr_or_null_reg() */
4101 regs[BPF_REG_0].id = id;
4102 /* For release_reference() */
4103 regs[BPF_REG_0].ref_obj_id = id;
4104 }
Martin KaFai Lau1b986582019-03-12 10:23:02 -07004105
Yonghong Song849fa502018-04-28 22:28:09 -07004106 do_refine_retval_range(regs, fn->ret_type, func_id, &meta);
4107
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004108 err = check_map_func_compatibility(env, meta.map_ptr, func_id);
Kaixu Xia35578d72015-08-06 07:02:35 +00004109 if (err)
4110 return err;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07004111
Yonghong Songc195651e2018-04-28 22:28:08 -07004112 if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) {
4113 const char *err_str;
4114
4115#ifdef CONFIG_PERF_EVENTS
4116 err = get_callchain_buffers(sysctl_perf_event_max_stack);
4117 err_str = "cannot get callchain buffer for func %s#%d\n";
4118#else
4119 err = -ENOTSUPP;
4120 err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n";
4121#endif
4122 if (err) {
4123 verbose(env, err_str, func_id_name(func_id), func_id);
4124 return err;
4125 }
4126
4127 env->prog->has_callchain_buf = true;
4128 }
4129
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004130 if (changes_data)
4131 clear_all_pkt_pointers(env);
4132 return 0;
4133}
4134
Edward Creeb03c9f92017-08-07 15:26:36 +01004135static bool signed_add_overflows(s64 a, s64 b)
4136{
4137 /* Do the add in u64, where overflow is well-defined */
4138 s64 res = (s64)((u64)a + (u64)b);
4139
4140 if (b < 0)
4141 return res > a;
4142 return res < a;
4143}
4144
4145static bool signed_sub_overflows(s64 a, s64 b)
4146{
4147 /* Do the sub in u64, where overflow is well-defined */
4148 s64 res = (s64)((u64)a - (u64)b);
4149
4150 if (b < 0)
4151 return res < a;
4152 return res > a;
David S. Millerd1174412017-05-10 11:22:52 -07004153}
4154
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08004155static bool check_reg_sane_offset(struct bpf_verifier_env *env,
4156 const struct bpf_reg_state *reg,
4157 enum bpf_reg_type type)
4158{
4159 bool known = tnum_is_const(reg->var_off);
4160 s64 val = reg->var_off.value;
4161 s64 smin = reg->smin_value;
4162
4163 if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) {
4164 verbose(env, "math between %s pointer and %lld is not allowed\n",
4165 reg_type_str[type], val);
4166 return false;
4167 }
4168
4169 if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) {
4170 verbose(env, "%s pointer offset %d is not allowed\n",
4171 reg_type_str[type], reg->off);
4172 return false;
4173 }
4174
4175 if (smin == S64_MIN) {
4176 verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n",
4177 reg_type_str[type]);
4178 return false;
4179 }
4180
4181 if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) {
4182 verbose(env, "value %lld makes %s pointer be out of bounds\n",
4183 smin, reg_type_str[type]);
4184 return false;
4185 }
4186
4187 return true;
4188}
4189
Daniel Borkmann979d63d2019-01-03 00:58:34 +01004190static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env)
4191{
4192 return &env->insn_aux_data[env->insn_idx];
4193}
4194
4195static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
4196 u32 *ptr_limit, u8 opcode, bool off_is_neg)
4197{
4198 bool mask_to_left = (opcode == BPF_ADD && off_is_neg) ||
4199 (opcode == BPF_SUB && !off_is_neg);
4200 u32 off;
4201
4202 switch (ptr_reg->type) {
4203 case PTR_TO_STACK:
Andrey Ignatov088ec262019-04-03 23:22:39 -07004204 /* Indirect variable offset stack access is prohibited in
4205 * unprivileged mode so it's not handled here.
4206 */
Daniel Borkmann979d63d2019-01-03 00:58:34 +01004207 off = ptr_reg->off + ptr_reg->var_off.value;
4208 if (mask_to_left)
4209 *ptr_limit = MAX_BPF_STACK + off;
4210 else
4211 *ptr_limit = -off;
4212 return 0;
4213 case PTR_TO_MAP_VALUE:
4214 if (mask_to_left) {
4215 *ptr_limit = ptr_reg->umax_value + ptr_reg->off;
4216 } else {
4217 off = ptr_reg->smin_value + ptr_reg->off;
4218 *ptr_limit = ptr_reg->map_ptr->value_size - off;
4219 }
4220 return 0;
4221 default:
4222 return -EINVAL;
4223 }
4224}
4225
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01004226static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env,
4227 const struct bpf_insn *insn)
4228{
4229 return env->allow_ptr_leaks || BPF_SRC(insn->code) == BPF_K;
4230}
4231
4232static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux,
4233 u32 alu_state, u32 alu_limit)
4234{
4235 /* If we arrived here from different branches with different
4236 * state or limits to sanitize, then this won't work.
4237 */
4238 if (aux->alu_state &&
4239 (aux->alu_state != alu_state ||
4240 aux->alu_limit != alu_limit))
4241 return -EACCES;
4242
4243 /* Corresponding fixup done in fixup_bpf_calls(). */
4244 aux->alu_state = alu_state;
4245 aux->alu_limit = alu_limit;
4246 return 0;
4247}
4248
4249static int sanitize_val_alu(struct bpf_verifier_env *env,
4250 struct bpf_insn *insn)
4251{
4252 struct bpf_insn_aux_data *aux = cur_aux(env);
4253
4254 if (can_skip_alu_sanitation(env, insn))
4255 return 0;
4256
4257 return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0);
4258}
4259
Daniel Borkmann979d63d2019-01-03 00:58:34 +01004260static int sanitize_ptr_alu(struct bpf_verifier_env *env,
4261 struct bpf_insn *insn,
4262 const struct bpf_reg_state *ptr_reg,
4263 struct bpf_reg_state *dst_reg,
4264 bool off_is_neg)
4265{
4266 struct bpf_verifier_state *vstate = env->cur_state;
4267 struct bpf_insn_aux_data *aux = cur_aux(env);
4268 bool ptr_is_dst_reg = ptr_reg == dst_reg;
4269 u8 opcode = BPF_OP(insn->code);
4270 u32 alu_state, alu_limit;
4271 struct bpf_reg_state tmp;
4272 bool ret;
4273
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01004274 if (can_skip_alu_sanitation(env, insn))
Daniel Borkmann979d63d2019-01-03 00:58:34 +01004275 return 0;
4276
4277 /* We already marked aux for masking from non-speculative
4278 * paths, thus we got here in the first place. We only care
4279 * to explore bad access from here.
4280 */
4281 if (vstate->speculative)
4282 goto do_sim;
4283
4284 alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0;
4285 alu_state |= ptr_is_dst_reg ?
4286 BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST;
4287
4288 if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg))
4289 return 0;
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01004290 if (update_alu_sanitation_state(aux, alu_state, alu_limit))
Daniel Borkmann979d63d2019-01-03 00:58:34 +01004291 return -EACCES;
Daniel Borkmann979d63d2019-01-03 00:58:34 +01004292do_sim:
4293 /* Simulate and find potential out-of-bounds access under
4294 * speculative execution from truncation as a result of
4295 * masking when off was not within expected range. If off
4296 * sits in dst, then we temporarily need to move ptr there
4297 * to simulate dst (== 0) +/-= ptr. Needed, for example,
4298 * for cases where we use K-based arithmetic in one direction
4299 * and truncated reg-based in the other in order to explore
4300 * bad access.
4301 */
4302 if (!ptr_is_dst_reg) {
4303 tmp = *dst_reg;
4304 *dst_reg = *ptr_reg;
4305 }
4306 ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true);
Xu Yu08032782019-03-21 18:00:35 +08004307 if (!ptr_is_dst_reg && ret)
Daniel Borkmann979d63d2019-01-03 00:58:34 +01004308 *dst_reg = tmp;
4309 return !ret ? -EFAULT : 0;
4310}
4311
Edward Creef1174f72017-08-07 15:26:19 +01004312/* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off.
Edward Creef1174f72017-08-07 15:26:19 +01004313 * Caller should also handle BPF_MOV case separately.
4314 * If we return -EACCES, caller may want to try again treating pointer as a
4315 * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks.
4316 */
4317static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
4318 struct bpf_insn *insn,
4319 const struct bpf_reg_state *ptr_reg,
4320 const struct bpf_reg_state *off_reg)
Josef Bacik48461132016-09-28 10:54:32 -04004321{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004322 struct bpf_verifier_state *vstate = env->cur_state;
4323 struct bpf_func_state *state = vstate->frame[vstate->curframe];
4324 struct bpf_reg_state *regs = state->regs, *dst_reg;
Edward Creef1174f72017-08-07 15:26:19 +01004325 bool known = tnum_is_const(off_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01004326 s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value,
4327 smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
4328 u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value,
4329 umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value;
Daniel Borkmann9d7ecee2019-01-03 00:58:32 +01004330 u32 dst = insn->dst_reg, src = insn->src_reg;
Josef Bacik48461132016-09-28 10:54:32 -04004331 u8 opcode = BPF_OP(insn->code);
Daniel Borkmann979d63d2019-01-03 00:58:34 +01004332 int ret;
Josef Bacik48461132016-09-28 10:54:32 -04004333
Edward Creef1174f72017-08-07 15:26:19 +01004334 dst_reg = &regs[dst];
Josef Bacik48461132016-09-28 10:54:32 -04004335
Daniel Borkmann6f161012018-01-18 01:15:21 +01004336 if ((known && (smin_val != smax_val || umin_val != umax_val)) ||
4337 smin_val > smax_val || umin_val > umax_val) {
4338 /* Taint dst register if offset had invalid bounds derived from
4339 * e.g. dead branches.
4340 */
4341 __mark_reg_unknown(dst_reg);
4342 return 0;
Josef Bacik48461132016-09-28 10:54:32 -04004343 }
4344
Edward Creef1174f72017-08-07 15:26:19 +01004345 if (BPF_CLASS(insn->code) != BPF_ALU64) {
4346 /* 32-bit ALU ops on pointers produce (meaningless) scalars */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004347 verbose(env,
4348 "R%d 32-bit pointer arithmetic prohibited\n",
4349 dst);
Edward Creef1174f72017-08-07 15:26:19 +01004350 return -EACCES;
4351 }
David S. Millerd1174412017-05-10 11:22:52 -07004352
Joe Stringeraad2eea2018-10-02 13:35:30 -07004353 switch (ptr_reg->type) {
4354 case PTR_TO_MAP_VALUE_OR_NULL:
4355 verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n",
4356 dst, reg_type_str[ptr_reg->type]);
Edward Creef1174f72017-08-07 15:26:19 +01004357 return -EACCES;
Joe Stringeraad2eea2018-10-02 13:35:30 -07004358 case CONST_PTR_TO_MAP:
4359 case PTR_TO_PACKET_END:
Joe Stringerc64b7982018-10-02 13:35:33 -07004360 case PTR_TO_SOCKET:
4361 case PTR_TO_SOCKET_OR_NULL:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08004362 case PTR_TO_SOCK_COMMON:
4363 case PTR_TO_SOCK_COMMON_OR_NULL:
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08004364 case PTR_TO_TCP_SOCK:
4365 case PTR_TO_TCP_SOCK_OR_NULL:
Jonathan Lemonfada7fd2019-06-06 13:59:40 -07004366 case PTR_TO_XDP_SOCK:
Joe Stringeraad2eea2018-10-02 13:35:30 -07004367 verbose(env, "R%d pointer arithmetic on %s prohibited\n",
4368 dst, reg_type_str[ptr_reg->type]);
Edward Creef1174f72017-08-07 15:26:19 +01004369 return -EACCES;
Daniel Borkmann9d7ecee2019-01-03 00:58:32 +01004370 case PTR_TO_MAP_VALUE:
4371 if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) {
4372 verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n",
4373 off_reg == dst_reg ? dst : src);
4374 return -EACCES;
4375 }
4376 /* fall-through */
Joe Stringeraad2eea2018-10-02 13:35:30 -07004377 default:
4378 break;
Edward Creef1174f72017-08-07 15:26:19 +01004379 }
4380
4381 /* In case of 'scalar += pointer', dst_reg inherits pointer type and id.
4382 * The id may be overwritten later if we create a new variable offset.
Josef Bacik48461132016-09-28 10:54:32 -04004383 */
Edward Creef1174f72017-08-07 15:26:19 +01004384 dst_reg->type = ptr_reg->type;
4385 dst_reg->id = ptr_reg->id;
Josef Bacikf23cc642016-11-14 15:45:36 -05004386
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08004387 if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) ||
4388 !check_reg_sane_offset(env, ptr_reg, ptr_reg->type))
4389 return -EINVAL;
4390
Josef Bacik48461132016-09-28 10:54:32 -04004391 switch (opcode) {
4392 case BPF_ADD:
Daniel Borkmann979d63d2019-01-03 00:58:34 +01004393 ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0);
4394 if (ret < 0) {
4395 verbose(env, "R%d tried to add from different maps or paths\n", dst);
4396 return ret;
4397 }
Edward Creef1174f72017-08-07 15:26:19 +01004398 /* We can take a fixed offset as long as it doesn't overflow
4399 * the s32 'off' field
4400 */
Edward Creeb03c9f92017-08-07 15:26:36 +01004401 if (known && (ptr_reg->off + smin_val ==
4402 (s64)(s32)(ptr_reg->off + smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01004403 /* pointer += K. Accumulate it into fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01004404 dst_reg->smin_value = smin_ptr;
4405 dst_reg->smax_value = smax_ptr;
4406 dst_reg->umin_value = umin_ptr;
4407 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01004408 dst_reg->var_off = ptr_reg->var_off;
Edward Creeb03c9f92017-08-07 15:26:36 +01004409 dst_reg->off = ptr_reg->off + smin_val;
Daniel Borkmann09625902018-11-01 00:05:52 +01004410 dst_reg->raw = ptr_reg->raw;
Edward Creef1174f72017-08-07 15:26:19 +01004411 break;
4412 }
Edward Creef1174f72017-08-07 15:26:19 +01004413 /* A new variable offset is created. Note that off_reg->off
4414 * == 0, since it's a scalar.
4415 * dst_reg gets the pointer type and since some positive
4416 * integer value was added to the pointer, give it a new 'id'
4417 * if it's a PTR_TO_PACKET.
4418 * this creates a new 'base' pointer, off_reg (variable) gets
4419 * added into the variable offset, and we copy the fixed offset
4420 * from ptr_reg.
4421 */
Edward Creeb03c9f92017-08-07 15:26:36 +01004422 if (signed_add_overflows(smin_ptr, smin_val) ||
4423 signed_add_overflows(smax_ptr, smax_val)) {
4424 dst_reg->smin_value = S64_MIN;
4425 dst_reg->smax_value = S64_MAX;
4426 } else {
4427 dst_reg->smin_value = smin_ptr + smin_val;
4428 dst_reg->smax_value = smax_ptr + smax_val;
4429 }
4430 if (umin_ptr + umin_val < umin_ptr ||
4431 umax_ptr + umax_val < umax_ptr) {
4432 dst_reg->umin_value = 0;
4433 dst_reg->umax_value = U64_MAX;
4434 } else {
4435 dst_reg->umin_value = umin_ptr + umin_val;
4436 dst_reg->umax_value = umax_ptr + umax_val;
4437 }
Edward Creef1174f72017-08-07 15:26:19 +01004438 dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off);
4439 dst_reg->off = ptr_reg->off;
Daniel Borkmann09625902018-11-01 00:05:52 +01004440 dst_reg->raw = ptr_reg->raw;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004441 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01004442 dst_reg->id = ++env->id_gen;
4443 /* something was added to pkt_ptr, set range to zero */
Daniel Borkmann09625902018-11-01 00:05:52 +01004444 dst_reg->raw = 0;
Edward Creef1174f72017-08-07 15:26:19 +01004445 }
Josef Bacik48461132016-09-28 10:54:32 -04004446 break;
4447 case BPF_SUB:
Daniel Borkmann979d63d2019-01-03 00:58:34 +01004448 ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0);
4449 if (ret < 0) {
4450 verbose(env, "R%d tried to sub from different maps or paths\n", dst);
4451 return ret;
4452 }
Edward Creef1174f72017-08-07 15:26:19 +01004453 if (dst_reg == off_reg) {
4454 /* scalar -= pointer. Creates an unknown scalar */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004455 verbose(env, "R%d tried to subtract pointer from scalar\n",
4456 dst);
Edward Creef1174f72017-08-07 15:26:19 +01004457 return -EACCES;
4458 }
4459 /* We don't allow subtraction from FP, because (according to
4460 * test_verifier.c test "invalid fp arithmetic", JITs might not
4461 * be able to deal with it.
Edward Cree93057062017-07-21 14:37:34 +01004462 */
Edward Creef1174f72017-08-07 15:26:19 +01004463 if (ptr_reg->type == PTR_TO_STACK) {
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004464 verbose(env, "R%d subtraction from stack pointer prohibited\n",
4465 dst);
Edward Creef1174f72017-08-07 15:26:19 +01004466 return -EACCES;
4467 }
Edward Creeb03c9f92017-08-07 15:26:36 +01004468 if (known && (ptr_reg->off - smin_val ==
4469 (s64)(s32)(ptr_reg->off - smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01004470 /* pointer -= K. Subtract it from fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01004471 dst_reg->smin_value = smin_ptr;
4472 dst_reg->smax_value = smax_ptr;
4473 dst_reg->umin_value = umin_ptr;
4474 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01004475 dst_reg->var_off = ptr_reg->var_off;
4476 dst_reg->id = ptr_reg->id;
Edward Creeb03c9f92017-08-07 15:26:36 +01004477 dst_reg->off = ptr_reg->off - smin_val;
Daniel Borkmann09625902018-11-01 00:05:52 +01004478 dst_reg->raw = ptr_reg->raw;
Edward Creef1174f72017-08-07 15:26:19 +01004479 break;
4480 }
Edward Creef1174f72017-08-07 15:26:19 +01004481 /* A new variable offset is created. If the subtrahend is known
4482 * nonnegative, then any reg->range we had before is still good.
4483 */
Edward Creeb03c9f92017-08-07 15:26:36 +01004484 if (signed_sub_overflows(smin_ptr, smax_val) ||
4485 signed_sub_overflows(smax_ptr, smin_val)) {
4486 /* Overflow possible, we know nothing */
4487 dst_reg->smin_value = S64_MIN;
4488 dst_reg->smax_value = S64_MAX;
4489 } else {
4490 dst_reg->smin_value = smin_ptr - smax_val;
4491 dst_reg->smax_value = smax_ptr - smin_val;
4492 }
4493 if (umin_ptr < umax_val) {
4494 /* Overflow possible, we know nothing */
4495 dst_reg->umin_value = 0;
4496 dst_reg->umax_value = U64_MAX;
4497 } else {
4498 /* Cannot overflow (as long as bounds are consistent) */
4499 dst_reg->umin_value = umin_ptr - umax_val;
4500 dst_reg->umax_value = umax_ptr - umin_val;
4501 }
Edward Creef1174f72017-08-07 15:26:19 +01004502 dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off);
4503 dst_reg->off = ptr_reg->off;
Daniel Borkmann09625902018-11-01 00:05:52 +01004504 dst_reg->raw = ptr_reg->raw;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004505 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01004506 dst_reg->id = ++env->id_gen;
4507 /* something was added to pkt_ptr, set range to zero */
Edward Creeb03c9f92017-08-07 15:26:36 +01004508 if (smin_val < 0)
Daniel Borkmann09625902018-11-01 00:05:52 +01004509 dst_reg->raw = 0;
Edward Creef1174f72017-08-07 15:26:19 +01004510 }
4511 break;
4512 case BPF_AND:
4513 case BPF_OR:
4514 case BPF_XOR:
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004515 /* bitwise ops on pointers are troublesome, prohibit. */
4516 verbose(env, "R%d bitwise operator %s on pointer prohibited\n",
4517 dst, bpf_alu_string[opcode >> 4]);
Edward Creef1174f72017-08-07 15:26:19 +01004518 return -EACCES;
4519 default:
4520 /* other operators (e.g. MUL,LSH) produce non-pointer results */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004521 verbose(env, "R%d pointer arithmetic with %s operator prohibited\n",
4522 dst, bpf_alu_string[opcode >> 4]);
Edward Creef1174f72017-08-07 15:26:19 +01004523 return -EACCES;
4524 }
4525
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08004526 if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type))
4527 return -EINVAL;
4528
Edward Creeb03c9f92017-08-07 15:26:36 +01004529 __update_reg_bounds(dst_reg);
4530 __reg_deduce_bounds(dst_reg);
4531 __reg_bound_offset(dst_reg);
Daniel Borkmann0d6303d2019-01-03 00:58:30 +01004532
4533 /* For unprivileged we require that resulting offset must be in bounds
4534 * in order to be able to sanitize access later on.
4535 */
Daniel Borkmanne4298d22019-01-03 00:58:31 +01004536 if (!env->allow_ptr_leaks) {
4537 if (dst_reg->type == PTR_TO_MAP_VALUE &&
4538 check_map_access(env, dst, dst_reg->off, 1, false)) {
4539 verbose(env, "R%d pointer arithmetic of map value goes out of range, "
4540 "prohibited for !root\n", dst);
4541 return -EACCES;
4542 } else if (dst_reg->type == PTR_TO_STACK &&
4543 check_stack_access(env, dst_reg, dst_reg->off +
4544 dst_reg->var_off.value, 1)) {
4545 verbose(env, "R%d stack pointer arithmetic goes out of range, "
4546 "prohibited for !root\n", dst);
4547 return -EACCES;
4548 }
Daniel Borkmann0d6303d2019-01-03 00:58:30 +01004549 }
4550
Edward Creef1174f72017-08-07 15:26:19 +01004551 return 0;
4552}
4553
Jann Horn468f6ea2017-12-18 20:11:56 -08004554/* WARNING: This function does calculations on 64-bit values, but the actual
4555 * execution may occur on 32-bit values. Therefore, things like bitshifts
4556 * need extra checks in the 32-bit case.
4557 */
Edward Creef1174f72017-08-07 15:26:19 +01004558static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
4559 struct bpf_insn *insn,
4560 struct bpf_reg_state *dst_reg,
4561 struct bpf_reg_state src_reg)
4562{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004563 struct bpf_reg_state *regs = cur_regs(env);
Edward Creef1174f72017-08-07 15:26:19 +01004564 u8 opcode = BPF_OP(insn->code);
4565 bool src_known, dst_known;
Edward Creeb03c9f92017-08-07 15:26:36 +01004566 s64 smin_val, smax_val;
4567 u64 umin_val, umax_val;
Jann Horn468f6ea2017-12-18 20:11:56 -08004568 u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32;
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01004569 u32 dst = insn->dst_reg;
4570 int ret;
Edward Creef1174f72017-08-07 15:26:19 +01004571
Jann Hornb7992072018-10-05 18:17:59 +02004572 if (insn_bitness == 32) {
4573 /* Relevant for 32-bit RSH: Information can propagate towards
4574 * LSB, so it isn't sufficient to only truncate the output to
4575 * 32 bits.
4576 */
4577 coerce_reg_to_size(dst_reg, 4);
4578 coerce_reg_to_size(&src_reg, 4);
4579 }
4580
Edward Creeb03c9f92017-08-07 15:26:36 +01004581 smin_val = src_reg.smin_value;
4582 smax_val = src_reg.smax_value;
4583 umin_val = src_reg.umin_value;
4584 umax_val = src_reg.umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01004585 src_known = tnum_is_const(src_reg.var_off);
4586 dst_known = tnum_is_const(dst_reg->var_off);
4587
Daniel Borkmann6f161012018-01-18 01:15:21 +01004588 if ((src_known && (smin_val != smax_val || umin_val != umax_val)) ||
4589 smin_val > smax_val || umin_val > umax_val) {
4590 /* Taint dst register if offset had invalid bounds derived from
4591 * e.g. dead branches.
4592 */
4593 __mark_reg_unknown(dst_reg);
4594 return 0;
4595 }
4596
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08004597 if (!src_known &&
4598 opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) {
4599 __mark_reg_unknown(dst_reg);
4600 return 0;
4601 }
4602
Edward Creef1174f72017-08-07 15:26:19 +01004603 switch (opcode) {
4604 case BPF_ADD:
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01004605 ret = sanitize_val_alu(env, insn);
4606 if (ret < 0) {
4607 verbose(env, "R%d tried to add from different pointers or scalars\n", dst);
4608 return ret;
4609 }
Edward Creeb03c9f92017-08-07 15:26:36 +01004610 if (signed_add_overflows(dst_reg->smin_value, smin_val) ||
4611 signed_add_overflows(dst_reg->smax_value, smax_val)) {
4612 dst_reg->smin_value = S64_MIN;
4613 dst_reg->smax_value = S64_MAX;
4614 } else {
4615 dst_reg->smin_value += smin_val;
4616 dst_reg->smax_value += smax_val;
4617 }
4618 if (dst_reg->umin_value + umin_val < umin_val ||
4619 dst_reg->umax_value + umax_val < umax_val) {
4620 dst_reg->umin_value = 0;
4621 dst_reg->umax_value = U64_MAX;
4622 } else {
4623 dst_reg->umin_value += umin_val;
4624 dst_reg->umax_value += umax_val;
4625 }
Edward Creef1174f72017-08-07 15:26:19 +01004626 dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off);
4627 break;
4628 case BPF_SUB:
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01004629 ret = sanitize_val_alu(env, insn);
4630 if (ret < 0) {
4631 verbose(env, "R%d tried to sub from different pointers or scalars\n", dst);
4632 return ret;
4633 }
Edward Creeb03c9f92017-08-07 15:26:36 +01004634 if (signed_sub_overflows(dst_reg->smin_value, smax_val) ||
4635 signed_sub_overflows(dst_reg->smax_value, smin_val)) {
4636 /* Overflow possible, we know nothing */
4637 dst_reg->smin_value = S64_MIN;
4638 dst_reg->smax_value = S64_MAX;
4639 } else {
4640 dst_reg->smin_value -= smax_val;
4641 dst_reg->smax_value -= smin_val;
4642 }
4643 if (dst_reg->umin_value < umax_val) {
4644 /* Overflow possible, we know nothing */
4645 dst_reg->umin_value = 0;
4646 dst_reg->umax_value = U64_MAX;
4647 } else {
4648 /* Cannot overflow (as long as bounds are consistent) */
4649 dst_reg->umin_value -= umax_val;
4650 dst_reg->umax_value -= umin_val;
4651 }
Edward Creef1174f72017-08-07 15:26:19 +01004652 dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off);
Josef Bacik48461132016-09-28 10:54:32 -04004653 break;
4654 case BPF_MUL:
Edward Creeb03c9f92017-08-07 15:26:36 +01004655 dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off);
4656 if (smin_val < 0 || dst_reg->smin_value < 0) {
Edward Creef1174f72017-08-07 15:26:19 +01004657 /* Ain't nobody got time to multiply that sign */
Edward Creeb03c9f92017-08-07 15:26:36 +01004658 __mark_reg_unbounded(dst_reg);
4659 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004660 break;
4661 }
Edward Creeb03c9f92017-08-07 15:26:36 +01004662 /* Both values are positive, so we can work with unsigned and
4663 * copy the result to signed (unless it exceeds S64_MAX).
Edward Creef1174f72017-08-07 15:26:19 +01004664 */
Edward Creeb03c9f92017-08-07 15:26:36 +01004665 if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) {
4666 /* Potential overflow, we know nothing */
4667 __mark_reg_unbounded(dst_reg);
4668 /* (except what we can learn from the var_off) */
4669 __update_reg_bounds(dst_reg);
4670 break;
4671 }
4672 dst_reg->umin_value *= umin_val;
4673 dst_reg->umax_value *= umax_val;
4674 if (dst_reg->umax_value > S64_MAX) {
4675 /* Overflow possible, we know nothing */
4676 dst_reg->smin_value = S64_MIN;
4677 dst_reg->smax_value = S64_MAX;
4678 } else {
4679 dst_reg->smin_value = dst_reg->umin_value;
4680 dst_reg->smax_value = dst_reg->umax_value;
4681 }
Josef Bacik48461132016-09-28 10:54:32 -04004682 break;
4683 case BPF_AND:
Edward Creef1174f72017-08-07 15:26:19 +01004684 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01004685 __mark_reg_known(dst_reg, dst_reg->var_off.value &
4686 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01004687 break;
4688 }
Edward Creeb03c9f92017-08-07 15:26:36 +01004689 /* We get our minimum from the var_off, since that's inherently
4690 * bitwise. Our maximum is the minimum of the operands' maxima.
Josef Bacikf23cc642016-11-14 15:45:36 -05004691 */
Edward Creef1174f72017-08-07 15:26:19 +01004692 dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01004693 dst_reg->umin_value = dst_reg->var_off.value;
4694 dst_reg->umax_value = min(dst_reg->umax_value, umax_val);
4695 if (dst_reg->smin_value < 0 || smin_val < 0) {
4696 /* Lose signed bounds when ANDing negative numbers,
4697 * ain't nobody got time for that.
4698 */
4699 dst_reg->smin_value = S64_MIN;
4700 dst_reg->smax_value = S64_MAX;
4701 } else {
4702 /* ANDing two positives gives a positive, so safe to
4703 * cast result into s64.
4704 */
4705 dst_reg->smin_value = dst_reg->umin_value;
4706 dst_reg->smax_value = dst_reg->umax_value;
4707 }
4708 /* We may learn something more from the var_off */
4709 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004710 break;
4711 case BPF_OR:
4712 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01004713 __mark_reg_known(dst_reg, dst_reg->var_off.value |
4714 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01004715 break;
4716 }
Edward Creeb03c9f92017-08-07 15:26:36 +01004717 /* We get our maximum from the var_off, and our minimum is the
4718 * maximum of the operands' minima
Edward Creef1174f72017-08-07 15:26:19 +01004719 */
4720 dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01004721 dst_reg->umin_value = max(dst_reg->umin_value, umin_val);
4722 dst_reg->umax_value = dst_reg->var_off.value |
4723 dst_reg->var_off.mask;
4724 if (dst_reg->smin_value < 0 || smin_val < 0) {
4725 /* Lose signed bounds when ORing negative numbers,
4726 * ain't nobody got time for that.
4727 */
4728 dst_reg->smin_value = S64_MIN;
4729 dst_reg->smax_value = S64_MAX;
Edward Creef1174f72017-08-07 15:26:19 +01004730 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01004731 /* ORing two positives gives a positive, so safe to
4732 * cast result into s64.
4733 */
4734 dst_reg->smin_value = dst_reg->umin_value;
4735 dst_reg->smax_value = dst_reg->umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01004736 }
Edward Creeb03c9f92017-08-07 15:26:36 +01004737 /* We may learn something more from the var_off */
4738 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004739 break;
4740 case BPF_LSH:
Jann Horn468f6ea2017-12-18 20:11:56 -08004741 if (umax_val >= insn_bitness) {
4742 /* Shifts greater than 31 or 63 are undefined.
4743 * This includes shifts by a negative number.
Edward Creeb03c9f92017-08-07 15:26:36 +01004744 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004745 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004746 break;
4747 }
Edward Creeb03c9f92017-08-07 15:26:36 +01004748 /* We lose all sign bit information (except what we can pick
4749 * up from var_off)
Josef Bacik48461132016-09-28 10:54:32 -04004750 */
Edward Creeb03c9f92017-08-07 15:26:36 +01004751 dst_reg->smin_value = S64_MIN;
4752 dst_reg->smax_value = S64_MAX;
4753 /* If we might shift our top bit out, then we know nothing */
4754 if (dst_reg->umax_value > 1ULL << (63 - umax_val)) {
4755 dst_reg->umin_value = 0;
4756 dst_reg->umax_value = U64_MAX;
David S. Millerd1174412017-05-10 11:22:52 -07004757 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01004758 dst_reg->umin_value <<= umin_val;
4759 dst_reg->umax_value <<= umax_val;
David S. Millerd1174412017-05-10 11:22:52 -07004760 }
Yonghong Songafbe1a52018-04-28 22:28:10 -07004761 dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val);
Edward Creeb03c9f92017-08-07 15:26:36 +01004762 /* We may learn something more from the var_off */
4763 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004764 break;
4765 case BPF_RSH:
Jann Horn468f6ea2017-12-18 20:11:56 -08004766 if (umax_val >= insn_bitness) {
4767 /* Shifts greater than 31 or 63 are undefined.
4768 * This includes shifts by a negative number.
Edward Creeb03c9f92017-08-07 15:26:36 +01004769 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004770 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004771 break;
4772 }
Edward Cree4374f252017-12-18 20:11:53 -08004773 /* BPF_RSH is an unsigned shift. If the value in dst_reg might
4774 * be negative, then either:
4775 * 1) src_reg might be zero, so the sign bit of the result is
4776 * unknown, so we lose our signed bounds
4777 * 2) it's known negative, thus the unsigned bounds capture the
4778 * signed bounds
4779 * 3) the signed bounds cross zero, so they tell us nothing
4780 * about the result
4781 * If the value in dst_reg is known nonnegative, then again the
4782 * unsigned bounts capture the signed bounds.
4783 * Thus, in all cases it suffices to blow away our signed bounds
4784 * and rely on inferring new ones from the unsigned bounds and
4785 * var_off of the result.
4786 */
4787 dst_reg->smin_value = S64_MIN;
4788 dst_reg->smax_value = S64_MAX;
Yonghong Songafbe1a52018-04-28 22:28:10 -07004789 dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val);
Edward Creeb03c9f92017-08-07 15:26:36 +01004790 dst_reg->umin_value >>= umax_val;
4791 dst_reg->umax_value >>= umin_val;
4792 /* We may learn something more from the var_off */
4793 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004794 break;
Yonghong Song9cbe1f5a2018-04-28 22:28:11 -07004795 case BPF_ARSH:
4796 if (umax_val >= insn_bitness) {
4797 /* Shifts greater than 31 or 63 are undefined.
4798 * This includes shifts by a negative number.
4799 */
4800 mark_reg_unknown(env, regs, insn->dst_reg);
4801 break;
4802 }
4803
4804 /* Upon reaching here, src_known is true and
4805 * umax_val is equal to umin_val.
4806 */
4807 dst_reg->smin_value >>= umin_val;
4808 dst_reg->smax_value >>= umin_val;
4809 dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val);
4810
4811 /* blow away the dst_reg umin_value/umax_value and rely on
4812 * dst_reg var_off to refine the result.
4813 */
4814 dst_reg->umin_value = 0;
4815 dst_reg->umax_value = U64_MAX;
4816 __update_reg_bounds(dst_reg);
4817 break;
Josef Bacik48461132016-09-28 10:54:32 -04004818 default:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004819 mark_reg_unknown(env, regs, insn->dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004820 break;
4821 }
4822
Jann Horn468f6ea2017-12-18 20:11:56 -08004823 if (BPF_CLASS(insn->code) != BPF_ALU64) {
4824 /* 32-bit ALU ops are (32,32)->32 */
4825 coerce_reg_to_size(dst_reg, 4);
Jann Horn468f6ea2017-12-18 20:11:56 -08004826 }
4827
Edward Creeb03c9f92017-08-07 15:26:36 +01004828 __reg_deduce_bounds(dst_reg);
4829 __reg_bound_offset(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004830 return 0;
4831}
4832
4833/* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max
4834 * and var_off.
4835 */
4836static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
4837 struct bpf_insn *insn)
4838{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004839 struct bpf_verifier_state *vstate = env->cur_state;
4840 struct bpf_func_state *state = vstate->frame[vstate->curframe];
4841 struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg;
Edward Creef1174f72017-08-07 15:26:19 +01004842 struct bpf_reg_state *ptr_reg = NULL, off_reg = {0};
4843 u8 opcode = BPF_OP(insn->code);
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07004844 int err;
Edward Creef1174f72017-08-07 15:26:19 +01004845
4846 dst_reg = &regs[insn->dst_reg];
Edward Creef1174f72017-08-07 15:26:19 +01004847 src_reg = NULL;
4848 if (dst_reg->type != SCALAR_VALUE)
4849 ptr_reg = dst_reg;
4850 if (BPF_SRC(insn->code) == BPF_X) {
4851 src_reg = &regs[insn->src_reg];
Edward Creef1174f72017-08-07 15:26:19 +01004852 if (src_reg->type != SCALAR_VALUE) {
4853 if (dst_reg->type != SCALAR_VALUE) {
4854 /* Combining two pointers by any ALU op yields
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004855 * an arbitrary scalar. Disallow all math except
4856 * pointer subtraction
Edward Creef1174f72017-08-07 15:26:19 +01004857 */
Alexei Starovoitovdd066822018-09-12 14:06:10 -07004858 if (opcode == BPF_SUB && env->allow_ptr_leaks) {
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004859 mark_reg_unknown(env, regs, insn->dst_reg);
4860 return 0;
Edward Creef1174f72017-08-07 15:26:19 +01004861 }
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004862 verbose(env, "R%d pointer %s pointer prohibited\n",
4863 insn->dst_reg,
4864 bpf_alu_string[opcode >> 4]);
4865 return -EACCES;
Edward Creef1174f72017-08-07 15:26:19 +01004866 } else {
4867 /* scalar += pointer
4868 * This is legal, but we have to reverse our
4869 * src/dest handling in computing the range
4870 */
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07004871 err = mark_chain_precision(env, insn->dst_reg);
4872 if (err)
4873 return err;
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004874 return adjust_ptr_min_max_vals(env, insn,
4875 src_reg, dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004876 }
4877 } else if (ptr_reg) {
4878 /* pointer += scalar */
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07004879 err = mark_chain_precision(env, insn->src_reg);
4880 if (err)
4881 return err;
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004882 return adjust_ptr_min_max_vals(env, insn,
4883 dst_reg, src_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004884 }
4885 } else {
4886 /* Pretend the src is a reg with a known value, since we only
4887 * need to be able to read from this state.
4888 */
4889 off_reg.type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01004890 __mark_reg_known(&off_reg, insn->imm);
Edward Creef1174f72017-08-07 15:26:19 +01004891 src_reg = &off_reg;
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004892 if (ptr_reg) /* pointer += K */
4893 return adjust_ptr_min_max_vals(env, insn,
4894 ptr_reg, src_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004895 }
4896
4897 /* Got here implies adding two SCALAR_VALUEs */
4898 if (WARN_ON_ONCE(ptr_reg)) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004899 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004900 verbose(env, "verifier internal error: unexpected ptr_reg\n");
Edward Creef1174f72017-08-07 15:26:19 +01004901 return -EINVAL;
4902 }
4903 if (WARN_ON(!src_reg)) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004904 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004905 verbose(env, "verifier internal error: no src_reg\n");
Edward Creef1174f72017-08-07 15:26:19 +01004906 return -EINVAL;
4907 }
4908 return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004909}
4910
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004911/* check validity of 32-bit and 64-bit arithmetic operations */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004912static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004913{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004914 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004915 u8 opcode = BPF_OP(insn->code);
4916 int err;
4917
4918 if (opcode == BPF_END || opcode == BPF_NEG) {
4919 if (opcode == BPF_NEG) {
4920 if (BPF_SRC(insn->code) != 0 ||
4921 insn->src_reg != BPF_REG_0 ||
4922 insn->off != 0 || insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004923 verbose(env, "BPF_NEG uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004924 return -EINVAL;
4925 }
4926 } else {
4927 if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
Edward Creee67b8a62017-09-15 14:37:38 +01004928 (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) ||
4929 BPF_CLASS(insn->code) == BPF_ALU64) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004930 verbose(env, "BPF_END uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004931 return -EINVAL;
4932 }
4933 }
4934
4935 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01004936 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004937 if (err)
4938 return err;
4939
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004940 if (is_pointer_value(env, insn->dst_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004941 verbose(env, "R%d pointer arithmetic prohibited\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004942 insn->dst_reg);
4943 return -EACCES;
4944 }
4945
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004946 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01004947 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004948 if (err)
4949 return err;
4950
4951 } else if (opcode == BPF_MOV) {
4952
4953 if (BPF_SRC(insn->code) == BPF_X) {
4954 if (insn->imm != 0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004955 verbose(env, "BPF_MOV uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004956 return -EINVAL;
4957 }
4958
4959 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01004960 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004961 if (err)
4962 return err;
4963 } else {
4964 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004965 verbose(env, "BPF_MOV uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004966 return -EINVAL;
4967 }
4968 }
4969
Arthur Fabrefbeb1602018-07-31 18:17:22 +01004970 /* check dest operand, mark as required later */
4971 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004972 if (err)
4973 return err;
4974
4975 if (BPF_SRC(insn->code) == BPF_X) {
Jiong Wange434b8c2018-12-07 12:16:18 -05004976 struct bpf_reg_state *src_reg = regs + insn->src_reg;
4977 struct bpf_reg_state *dst_reg = regs + insn->dst_reg;
4978
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004979 if (BPF_CLASS(insn->code) == BPF_ALU64) {
4980 /* case: R1 = R2
4981 * copy register state to dest reg
4982 */
Jiong Wange434b8c2018-12-07 12:16:18 -05004983 *dst_reg = *src_reg;
4984 dst_reg->live |= REG_LIVE_WRITTEN;
Jiong Wang5327ed32019-05-24 23:25:12 +01004985 dst_reg->subreg_def = DEF_NOT_SUBREG;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004986 } else {
Edward Creef1174f72017-08-07 15:26:19 +01004987 /* R1 = (u32) R2 */
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004988 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004989 verbose(env,
4990 "R%d partial copy of pointer\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004991 insn->src_reg);
4992 return -EACCES;
Jiong Wange434b8c2018-12-07 12:16:18 -05004993 } else if (src_reg->type == SCALAR_VALUE) {
4994 *dst_reg = *src_reg;
4995 dst_reg->live |= REG_LIVE_WRITTEN;
Jiong Wang5327ed32019-05-24 23:25:12 +01004996 dst_reg->subreg_def = env->insn_idx + 1;
Jiong Wange434b8c2018-12-07 12:16:18 -05004997 } else {
4998 mark_reg_unknown(env, regs,
4999 insn->dst_reg);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07005000 }
Jiong Wange434b8c2018-12-07 12:16:18 -05005001 coerce_reg_to_size(dst_reg, 4);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005002 }
5003 } else {
5004 /* case: R = imm
5005 * remember the value we stored into this reg
5006 */
Arthur Fabrefbeb1602018-07-31 18:17:22 +01005007 /* clear any state __mark_reg_known doesn't set */
5008 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01005009 regs[insn->dst_reg].type = SCALAR_VALUE;
Jann Horn95a762e2017-12-18 20:11:54 -08005010 if (BPF_CLASS(insn->code) == BPF_ALU64) {
5011 __mark_reg_known(regs + insn->dst_reg,
5012 insn->imm);
5013 } else {
5014 __mark_reg_known(regs + insn->dst_reg,
5015 (u32)insn->imm);
5016 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005017 }
5018
5019 } else if (opcode > BPF_END) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005020 verbose(env, "invalid BPF_ALU opcode %x\n", opcode);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005021 return -EINVAL;
5022
5023 } else { /* all other ALU ops: and, sub, xor, add, ... */
5024
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005025 if (BPF_SRC(insn->code) == BPF_X) {
5026 if (insn->imm != 0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005027 verbose(env, "BPF_ALU uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005028 return -EINVAL;
5029 }
5030 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01005031 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005032 if (err)
5033 return err;
5034 } else {
5035 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005036 verbose(env, "BPF_ALU uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005037 return -EINVAL;
5038 }
5039 }
5040
5041 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01005042 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005043 if (err)
5044 return err;
5045
5046 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
5047 BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005048 verbose(env, "div by zero\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005049 return -EINVAL;
5050 }
5051
Rabin Vincent229394e82016-01-12 20:17:08 +01005052 if ((opcode == BPF_LSH || opcode == BPF_RSH ||
5053 opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
5054 int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
5055
5056 if (insn->imm < 0 || insn->imm >= size) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005057 verbose(env, "invalid shift %d\n", insn->imm);
Rabin Vincent229394e82016-01-12 20:17:08 +01005058 return -EINVAL;
5059 }
5060 }
5061
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07005062 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01005063 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07005064 if (err)
5065 return err;
5066
Edward Creef1174f72017-08-07 15:26:19 +01005067 return adjust_reg_min_max_vals(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005068 }
5069
5070 return 0;
5071}
5072
Paul Chaignonc6a9efa2019-04-24 21:50:42 +02005073static void __find_good_pkt_pointers(struct bpf_func_state *state,
5074 struct bpf_reg_state *dst_reg,
5075 enum bpf_reg_type type, u16 new_range)
5076{
5077 struct bpf_reg_state *reg;
5078 int i;
5079
5080 for (i = 0; i < MAX_BPF_REG; i++) {
5081 reg = &state->regs[i];
5082 if (reg->type == type && reg->id == dst_reg->id)
5083 /* keep the maximum range already checked */
5084 reg->range = max(reg->range, new_range);
5085 }
5086
5087 bpf_for_each_spilled_reg(i, state, reg) {
5088 if (!reg)
5089 continue;
5090 if (reg->type == type && reg->id == dst_reg->id)
5091 reg->range = max(reg->range, new_range);
5092 }
5093}
5094
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005095static void find_good_pkt_pointers(struct bpf_verifier_state *vstate,
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02005096 struct bpf_reg_state *dst_reg,
David S. Millerf8ddadc2017-10-22 13:36:53 +01005097 enum bpf_reg_type type,
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02005098 bool range_right_open)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07005099{
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02005100 u16 new_range;
Paul Chaignonc6a9efa2019-04-24 21:50:42 +02005101 int i;
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02005102
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02005103 if (dst_reg->off < 0 ||
5104 (dst_reg->off == 0 && range_right_open))
Edward Creef1174f72017-08-07 15:26:19 +01005105 /* This doesn't give us any range */
5106 return;
5107
Edward Creeb03c9f92017-08-07 15:26:36 +01005108 if (dst_reg->umax_value > MAX_PACKET_OFF ||
5109 dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF)
Edward Creef1174f72017-08-07 15:26:19 +01005110 /* Risk of overflow. For instance, ptr + (1<<63) may be less
5111 * than pkt_end, but that's because it's also less than pkt.
5112 */
5113 return;
5114
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02005115 new_range = dst_reg->off;
5116 if (range_right_open)
5117 new_range--;
5118
5119 /* Examples for register markings:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02005120 *
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02005121 * pkt_data in dst register:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02005122 *
5123 * r2 = r3;
5124 * r2 += 8;
5125 * if (r2 > pkt_end) goto <handle exception>
5126 * <access okay>
5127 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02005128 * r2 = r3;
5129 * r2 += 8;
5130 * if (r2 < pkt_end) goto <access okay>
5131 * <handle exception>
5132 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02005133 * Where:
5134 * r2 == dst_reg, pkt_end == src_reg
5135 * r2=pkt(id=n,off=8,r=0)
5136 * r3=pkt(id=n,off=0,r=0)
5137 *
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02005138 * pkt_data in src register:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02005139 *
5140 * r2 = r3;
5141 * r2 += 8;
5142 * if (pkt_end >= r2) goto <access okay>
5143 * <handle exception>
5144 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02005145 * r2 = r3;
5146 * r2 += 8;
5147 * if (pkt_end <= r2) goto <handle exception>
5148 * <access okay>
5149 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02005150 * Where:
5151 * pkt_end == dst_reg, r2 == src_reg
5152 * r2=pkt(id=n,off=8,r=0)
5153 * r3=pkt(id=n,off=0,r=0)
5154 *
5155 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02005156 * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8)
5157 * and [r3, r3 + 8-1) respectively is safe to access depending on
5158 * the check.
Alexei Starovoitov969bf052016-05-05 19:49:10 -07005159 */
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02005160
Edward Creef1174f72017-08-07 15:26:19 +01005161 /* If our ids match, then we must have the same max_value. And we
5162 * don't care about the other reg's fixed offset, since if it's too big
5163 * the range won't allow anything.
5164 * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16.
5165 */
Paul Chaignonc6a9efa2019-04-24 21:50:42 +02005166 for (i = 0; i <= vstate->curframe; i++)
5167 __find_good_pkt_pointers(vstate->frame[i], dst_reg, type,
5168 new_range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07005169}
5170
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08005171/* compute branch direction of the expression "if (reg opcode val) goto target;"
5172 * and return:
5173 * 1 - branch will be taken and "goto target" will be executed
5174 * 0 - branch will not be taken and fall-through to next insn
5175 * -1 - unknown. Example: "if (reg < 5)" is unknown when register value range [0,10]
5176 */
Jiong Wang092ed092019-01-26 12:26:01 -05005177static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode,
5178 bool is_jmp32)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08005179{
Jiong Wang092ed092019-01-26 12:26:01 -05005180 struct bpf_reg_state reg_lo;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005181 s64 sval;
5182
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08005183 if (__is_pointer_value(false, reg))
5184 return -1;
5185
Jiong Wang092ed092019-01-26 12:26:01 -05005186 if (is_jmp32) {
5187 reg_lo = *reg;
5188 reg = &reg_lo;
5189 /* For JMP32, only low 32 bits are compared, coerce_reg_to_size
5190 * could truncate high bits and update umin/umax according to
5191 * information of low bits.
5192 */
5193 coerce_reg_to_size(reg, 4);
5194 /* smin/smax need special handling. For example, after coerce,
5195 * if smin_value is 0x00000000ffffffffLL, the value is -1 when
5196 * used as operand to JMP32. It is a negative number from s32's
5197 * point of view, while it is a positive number when seen as
5198 * s64. The smin/smax are kept as s64, therefore, when used with
5199 * JMP32, they need to be transformed into s32, then sign
5200 * extended back to s64.
5201 *
5202 * Also, smin/smax were copied from umin/umax. If umin/umax has
5203 * different sign bit, then min/max relationship doesn't
5204 * maintain after casting into s32, for this case, set smin/smax
5205 * to safest range.
5206 */
5207 if ((reg->umax_value ^ reg->umin_value) &
5208 (1ULL << 31)) {
5209 reg->smin_value = S32_MIN;
5210 reg->smax_value = S32_MAX;
5211 }
5212 reg->smin_value = (s64)(s32)reg->smin_value;
5213 reg->smax_value = (s64)(s32)reg->smax_value;
5214
5215 val = (u32)val;
5216 sval = (s64)(s32)val;
5217 } else {
5218 sval = (s64)val;
5219 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05005220
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08005221 switch (opcode) {
5222 case BPF_JEQ:
5223 if (tnum_is_const(reg->var_off))
5224 return !!tnum_equals_const(reg->var_off, val);
5225 break;
5226 case BPF_JNE:
5227 if (tnum_is_const(reg->var_off))
5228 return !tnum_equals_const(reg->var_off, val);
5229 break;
Jakub Kicinski960ea052018-12-19 22:13:04 -08005230 case BPF_JSET:
5231 if ((~reg->var_off.mask & reg->var_off.value) & val)
5232 return 1;
5233 if (!((reg->var_off.mask | reg->var_off.value) & val))
5234 return 0;
5235 break;
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08005236 case BPF_JGT:
5237 if (reg->umin_value > val)
5238 return 1;
5239 else if (reg->umax_value <= val)
5240 return 0;
5241 break;
5242 case BPF_JSGT:
Jiong Wanga72dafa2019-01-26 12:26:00 -05005243 if (reg->smin_value > sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08005244 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005245 else if (reg->smax_value < sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08005246 return 0;
5247 break;
5248 case BPF_JLT:
5249 if (reg->umax_value < val)
5250 return 1;
5251 else if (reg->umin_value >= val)
5252 return 0;
5253 break;
5254 case BPF_JSLT:
Jiong Wanga72dafa2019-01-26 12:26:00 -05005255 if (reg->smax_value < sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08005256 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005257 else if (reg->smin_value >= sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08005258 return 0;
5259 break;
5260 case BPF_JGE:
5261 if (reg->umin_value >= val)
5262 return 1;
5263 else if (reg->umax_value < val)
5264 return 0;
5265 break;
5266 case BPF_JSGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05005267 if (reg->smin_value >= sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08005268 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005269 else if (reg->smax_value < sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08005270 return 0;
5271 break;
5272 case BPF_JLE:
5273 if (reg->umax_value <= val)
5274 return 1;
5275 else if (reg->umin_value > val)
5276 return 0;
5277 break;
5278 case BPF_JSLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05005279 if (reg->smax_value <= sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08005280 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005281 else if (reg->smin_value > sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08005282 return 0;
5283 break;
5284 }
5285
5286 return -1;
5287}
5288
Jiong Wang092ed092019-01-26 12:26:01 -05005289/* Generate min value of the high 32-bit from TNUM info. */
5290static u64 gen_hi_min(struct tnum var)
5291{
5292 return var.value & ~0xffffffffULL;
5293}
5294
5295/* Generate max value of the high 32-bit from TNUM info. */
5296static u64 gen_hi_max(struct tnum var)
5297{
5298 return (var.value | var.mask) & ~0xffffffffULL;
5299}
5300
5301/* Return true if VAL is compared with a s64 sign extended from s32, and they
5302 * are with the same signedness.
5303 */
5304static bool cmp_val_with_extended_s64(s64 sval, struct bpf_reg_state *reg)
5305{
5306 return ((s32)sval >= 0 &&
5307 reg->smin_value >= 0 && reg->smax_value <= S32_MAX) ||
5308 ((s32)sval < 0 &&
5309 reg->smax_value <= 0 && reg->smin_value >= S32_MIN);
5310}
5311
Josef Bacik48461132016-09-28 10:54:32 -04005312/* Adjusts the register min/max values in the case that the dst_reg is the
5313 * variable register that we are working on, and src_reg is a constant or we're
5314 * simply doing a BPF_K check.
Edward Creef1174f72017-08-07 15:26:19 +01005315 * In JEQ/JNE cases we also adjust the var_off values.
Josef Bacik48461132016-09-28 10:54:32 -04005316 */
5317static void reg_set_min_max(struct bpf_reg_state *true_reg,
5318 struct bpf_reg_state *false_reg, u64 val,
Jiong Wang092ed092019-01-26 12:26:01 -05005319 u8 opcode, bool is_jmp32)
Josef Bacik48461132016-09-28 10:54:32 -04005320{
Jiong Wanga72dafa2019-01-26 12:26:00 -05005321 s64 sval;
5322
Edward Creef1174f72017-08-07 15:26:19 +01005323 /* If the dst_reg is a pointer, we can't learn anything about its
5324 * variable offset from the compare (unless src_reg were a pointer into
5325 * the same object, but we don't bother with that.
5326 * Since false_reg and true_reg have the same type by construction, we
5327 * only need to check one of them for pointerness.
5328 */
5329 if (__is_pointer_value(false, false_reg))
5330 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02005331
Jiong Wang092ed092019-01-26 12:26:01 -05005332 val = is_jmp32 ? (u32)val : val;
5333 sval = is_jmp32 ? (s64)(s32)val : (s64)val;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005334
Josef Bacik48461132016-09-28 10:54:32 -04005335 switch (opcode) {
5336 case BPF_JEQ:
Josef Bacik48461132016-09-28 10:54:32 -04005337 case BPF_JNE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05005338 {
5339 struct bpf_reg_state *reg =
5340 opcode == BPF_JEQ ? true_reg : false_reg;
5341
5342 /* For BPF_JEQ, if this is false we know nothing Jon Snow, but
5343 * if it is true we know the value for sure. Likewise for
5344 * BPF_JNE.
Josef Bacik48461132016-09-28 10:54:32 -04005345 */
Jiong Wang092ed092019-01-26 12:26:01 -05005346 if (is_jmp32) {
5347 u64 old_v = reg->var_off.value;
5348 u64 hi_mask = ~0xffffffffULL;
5349
5350 reg->var_off.value = (old_v & hi_mask) | val;
5351 reg->var_off.mask &= hi_mask;
5352 } else {
5353 __mark_reg_known(reg, val);
5354 }
Josef Bacik48461132016-09-28 10:54:32 -04005355 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005356 }
Jakub Kicinski960ea052018-12-19 22:13:04 -08005357 case BPF_JSET:
5358 false_reg->var_off = tnum_and(false_reg->var_off,
5359 tnum_const(~val));
5360 if (is_power_of_2(val))
5361 true_reg->var_off = tnum_or(true_reg->var_off,
5362 tnum_const(val));
5363 break;
Josef Bacik48461132016-09-28 10:54:32 -04005364 case BPF_JGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05005365 case BPF_JGT:
5366 {
5367 u64 false_umax = opcode == BPF_JGT ? val : val - 1;
5368 u64 true_umin = opcode == BPF_JGT ? val + 1 : val;
5369
Jiong Wang092ed092019-01-26 12:26:01 -05005370 if (is_jmp32) {
5371 false_umax += gen_hi_max(false_reg->var_off);
5372 true_umin += gen_hi_min(true_reg->var_off);
5373 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05005374 false_reg->umax_value = min(false_reg->umax_value, false_umax);
5375 true_reg->umin_value = max(true_reg->umin_value, true_umin);
Edward Creeb03c9f92017-08-07 15:26:36 +01005376 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005377 }
Josef Bacik48461132016-09-28 10:54:32 -04005378 case BPF_JSGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05005379 case BPF_JSGT:
5380 {
5381 s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1;
5382 s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval;
5383
Jiong Wang092ed092019-01-26 12:26:01 -05005384 /* If the full s64 was not sign-extended from s32 then don't
5385 * deduct further info.
5386 */
5387 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
5388 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005389 false_reg->smax_value = min(false_reg->smax_value, false_smax);
5390 true_reg->smin_value = max(true_reg->smin_value, true_smin);
Josef Bacik48461132016-09-28 10:54:32 -04005391 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005392 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02005393 case BPF_JLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05005394 case BPF_JLT:
5395 {
5396 u64 false_umin = opcode == BPF_JLT ? val : val + 1;
5397 u64 true_umax = opcode == BPF_JLT ? val - 1 : val;
5398
Jiong Wang092ed092019-01-26 12:26:01 -05005399 if (is_jmp32) {
5400 false_umin += gen_hi_min(false_reg->var_off);
5401 true_umax += gen_hi_max(true_reg->var_off);
5402 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05005403 false_reg->umin_value = max(false_reg->umin_value, false_umin);
5404 true_reg->umax_value = min(true_reg->umax_value, true_umax);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02005405 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005406 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02005407 case BPF_JSLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05005408 case BPF_JSLT:
5409 {
5410 s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1;
5411 s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval;
5412
Jiong Wang092ed092019-01-26 12:26:01 -05005413 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
5414 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005415 false_reg->smin_value = max(false_reg->smin_value, false_smin);
5416 true_reg->smax_value = min(true_reg->smax_value, true_smax);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02005417 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005418 }
Josef Bacik48461132016-09-28 10:54:32 -04005419 default:
5420 break;
5421 }
5422
Edward Creeb03c9f92017-08-07 15:26:36 +01005423 __reg_deduce_bounds(false_reg);
5424 __reg_deduce_bounds(true_reg);
5425 /* We might have learned some bits from the bounds. */
5426 __reg_bound_offset(false_reg);
5427 __reg_bound_offset(true_reg);
5428 /* Intersecting with the old var_off might have improved our bounds
5429 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
5430 * then new var_off is (0; 0x7f...fc) which improves our umax.
5431 */
5432 __update_reg_bounds(false_reg);
5433 __update_reg_bounds(true_reg);
Josef Bacik48461132016-09-28 10:54:32 -04005434}
5435
Edward Creef1174f72017-08-07 15:26:19 +01005436/* Same as above, but for the case that dst_reg holds a constant and src_reg is
5437 * the variable reg.
Josef Bacik48461132016-09-28 10:54:32 -04005438 */
5439static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
5440 struct bpf_reg_state *false_reg, u64 val,
Jiong Wang092ed092019-01-26 12:26:01 -05005441 u8 opcode, bool is_jmp32)
Josef Bacik48461132016-09-28 10:54:32 -04005442{
Jiong Wanga72dafa2019-01-26 12:26:00 -05005443 s64 sval;
5444
Edward Creef1174f72017-08-07 15:26:19 +01005445 if (__is_pointer_value(false, false_reg))
5446 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02005447
Jiong Wang092ed092019-01-26 12:26:01 -05005448 val = is_jmp32 ? (u32)val : val;
5449 sval = is_jmp32 ? (s64)(s32)val : (s64)val;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005450
Josef Bacik48461132016-09-28 10:54:32 -04005451 switch (opcode) {
5452 case BPF_JEQ:
Josef Bacik48461132016-09-28 10:54:32 -04005453 case BPF_JNE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05005454 {
5455 struct bpf_reg_state *reg =
5456 opcode == BPF_JEQ ? true_reg : false_reg;
5457
Jiong Wang092ed092019-01-26 12:26:01 -05005458 if (is_jmp32) {
5459 u64 old_v = reg->var_off.value;
5460 u64 hi_mask = ~0xffffffffULL;
5461
5462 reg->var_off.value = (old_v & hi_mask) | val;
5463 reg->var_off.mask &= hi_mask;
5464 } else {
5465 __mark_reg_known(reg, val);
5466 }
Josef Bacik48461132016-09-28 10:54:32 -04005467 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005468 }
Jakub Kicinski960ea052018-12-19 22:13:04 -08005469 case BPF_JSET:
5470 false_reg->var_off = tnum_and(false_reg->var_off,
5471 tnum_const(~val));
5472 if (is_power_of_2(val))
5473 true_reg->var_off = tnum_or(true_reg->var_off,
5474 tnum_const(val));
5475 break;
Josef Bacik48461132016-09-28 10:54:32 -04005476 case BPF_JGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05005477 case BPF_JGT:
5478 {
5479 u64 false_umin = opcode == BPF_JGT ? val : val + 1;
5480 u64 true_umax = opcode == BPF_JGT ? val - 1 : val;
5481
Jiong Wang092ed092019-01-26 12:26:01 -05005482 if (is_jmp32) {
5483 false_umin += gen_hi_min(false_reg->var_off);
5484 true_umax += gen_hi_max(true_reg->var_off);
5485 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05005486 false_reg->umin_value = max(false_reg->umin_value, false_umin);
5487 true_reg->umax_value = min(true_reg->umax_value, true_umax);
Edward Creeb03c9f92017-08-07 15:26:36 +01005488 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005489 }
Josef Bacik48461132016-09-28 10:54:32 -04005490 case BPF_JSGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05005491 case BPF_JSGT:
5492 {
5493 s64 false_smin = opcode == BPF_JSGT ? sval : sval + 1;
5494 s64 true_smax = opcode == BPF_JSGT ? sval - 1 : sval;
5495
Jiong Wang092ed092019-01-26 12:26:01 -05005496 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
5497 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005498 false_reg->smin_value = max(false_reg->smin_value, false_smin);
5499 true_reg->smax_value = min(true_reg->smax_value, true_smax);
Josef Bacik48461132016-09-28 10:54:32 -04005500 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005501 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02005502 case BPF_JLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05005503 case BPF_JLT:
5504 {
5505 u64 false_umax = opcode == BPF_JLT ? val : val - 1;
5506 u64 true_umin = opcode == BPF_JLT ? val + 1 : val;
5507
Jiong Wang092ed092019-01-26 12:26:01 -05005508 if (is_jmp32) {
5509 false_umax += gen_hi_max(false_reg->var_off);
5510 true_umin += gen_hi_min(true_reg->var_off);
5511 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05005512 false_reg->umax_value = min(false_reg->umax_value, false_umax);
5513 true_reg->umin_value = max(true_reg->umin_value, true_umin);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02005514 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005515 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02005516 case BPF_JSLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05005517 case BPF_JSLT:
5518 {
5519 s64 false_smax = opcode == BPF_JSLT ? sval : sval - 1;
5520 s64 true_smin = opcode == BPF_JSLT ? sval + 1 : sval;
5521
Jiong Wang092ed092019-01-26 12:26:01 -05005522 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
5523 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005524 false_reg->smax_value = min(false_reg->smax_value, false_smax);
5525 true_reg->smin_value = max(true_reg->smin_value, true_smin);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02005526 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005527 }
Josef Bacik48461132016-09-28 10:54:32 -04005528 default:
5529 break;
5530 }
5531
Edward Creeb03c9f92017-08-07 15:26:36 +01005532 __reg_deduce_bounds(false_reg);
5533 __reg_deduce_bounds(true_reg);
5534 /* We might have learned some bits from the bounds. */
5535 __reg_bound_offset(false_reg);
5536 __reg_bound_offset(true_reg);
5537 /* Intersecting with the old var_off might have improved our bounds
5538 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
5539 * then new var_off is (0; 0x7f...fc) which improves our umax.
5540 */
5541 __update_reg_bounds(false_reg);
5542 __update_reg_bounds(true_reg);
Edward Creef1174f72017-08-07 15:26:19 +01005543}
5544
5545/* Regs are known to be equal, so intersect their min/max/var_off */
5546static void __reg_combine_min_max(struct bpf_reg_state *src_reg,
5547 struct bpf_reg_state *dst_reg)
5548{
Edward Creeb03c9f92017-08-07 15:26:36 +01005549 src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value,
5550 dst_reg->umin_value);
5551 src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value,
5552 dst_reg->umax_value);
5553 src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value,
5554 dst_reg->smin_value);
5555 src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value,
5556 dst_reg->smax_value);
Edward Creef1174f72017-08-07 15:26:19 +01005557 src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off,
5558 dst_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01005559 /* We might have learned new bounds from the var_off. */
5560 __update_reg_bounds(src_reg);
5561 __update_reg_bounds(dst_reg);
5562 /* We might have learned something about the sign bit. */
5563 __reg_deduce_bounds(src_reg);
5564 __reg_deduce_bounds(dst_reg);
5565 /* We might have learned some bits from the bounds. */
5566 __reg_bound_offset(src_reg);
5567 __reg_bound_offset(dst_reg);
5568 /* Intersecting with the old var_off might have improved our bounds
5569 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
5570 * then new var_off is (0; 0x7f...fc) which improves our umax.
5571 */
5572 __update_reg_bounds(src_reg);
5573 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01005574}
5575
5576static void reg_combine_min_max(struct bpf_reg_state *true_src,
5577 struct bpf_reg_state *true_dst,
5578 struct bpf_reg_state *false_src,
5579 struct bpf_reg_state *false_dst,
5580 u8 opcode)
5581{
5582 switch (opcode) {
5583 case BPF_JEQ:
5584 __reg_combine_min_max(true_src, true_dst);
5585 break;
5586 case BPF_JNE:
5587 __reg_combine_min_max(false_src, false_dst);
Edward Creeb03c9f92017-08-07 15:26:36 +01005588 break;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02005589 }
Josef Bacik48461132016-09-28 10:54:32 -04005590}
5591
Joe Stringerfd978bf72018-10-02 13:35:35 -07005592static void mark_ptr_or_null_reg(struct bpf_func_state *state,
5593 struct bpf_reg_state *reg, u32 id,
Joe Stringer840b9612018-10-02 13:35:32 -07005594 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02005595{
Joe Stringer840b9612018-10-02 13:35:32 -07005596 if (reg_type_may_be_null(reg->type) && reg->id == id) {
Edward Creef1174f72017-08-07 15:26:19 +01005597 /* Old offset (both fixed and variable parts) should
5598 * have been known-zero, because we don't allow pointer
5599 * arithmetic on pointers that might be NULL.
5600 */
Edward Creeb03c9f92017-08-07 15:26:36 +01005601 if (WARN_ON_ONCE(reg->smin_value || reg->smax_value ||
5602 !tnum_equals_const(reg->var_off, 0) ||
Edward Creef1174f72017-08-07 15:26:19 +01005603 reg->off)) {
Edward Creeb03c9f92017-08-07 15:26:36 +01005604 __mark_reg_known_zero(reg);
5605 reg->off = 0;
Edward Creef1174f72017-08-07 15:26:19 +01005606 }
5607 if (is_null) {
5608 reg->type = SCALAR_VALUE;
Joe Stringer840b9612018-10-02 13:35:32 -07005609 } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
5610 if (reg->map_ptr->inner_map_meta) {
5611 reg->type = CONST_PTR_TO_MAP;
5612 reg->map_ptr = reg->map_ptr->inner_map_meta;
Jonathan Lemonfada7fd2019-06-06 13:59:40 -07005613 } else if (reg->map_ptr->map_type ==
5614 BPF_MAP_TYPE_XSKMAP) {
5615 reg->type = PTR_TO_XDP_SOCK;
Joe Stringer840b9612018-10-02 13:35:32 -07005616 } else {
5617 reg->type = PTR_TO_MAP_VALUE;
5618 }
Joe Stringerc64b7982018-10-02 13:35:33 -07005619 } else if (reg->type == PTR_TO_SOCKET_OR_NULL) {
5620 reg->type = PTR_TO_SOCKET;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08005621 } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) {
5622 reg->type = PTR_TO_SOCK_COMMON;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08005623 } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) {
5624 reg->type = PTR_TO_TCP_SOCK;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07005625 }
Martin KaFai Lau1b986582019-03-12 10:23:02 -07005626 if (is_null) {
5627 /* We don't need id and ref_obj_id from this point
5628 * onwards anymore, thus we should better reset it,
5629 * so that state pruning has chances to take effect.
5630 */
5631 reg->id = 0;
5632 reg->ref_obj_id = 0;
5633 } else if (!reg_may_point_to_spin_lock(reg)) {
5634 /* For not-NULL ptr, reg->ref_obj_id will be reset
5635 * in release_reg_references().
5636 *
5637 * reg->id is still used by spin_lock ptr. Other
5638 * than spin_lock ptr type, reg->id can be reset.
Joe Stringerfd978bf72018-10-02 13:35:35 -07005639 */
5640 reg->id = 0;
5641 }
Thomas Graf57a09bf2016-10-18 19:51:19 +02005642 }
5643}
5644
Paul Chaignonc6a9efa2019-04-24 21:50:42 +02005645static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id,
5646 bool is_null)
5647{
5648 struct bpf_reg_state *reg;
5649 int i;
5650
5651 for (i = 0; i < MAX_BPF_REG; i++)
5652 mark_ptr_or_null_reg(state, &state->regs[i], id, is_null);
5653
5654 bpf_for_each_spilled_reg(i, state, reg) {
5655 if (!reg)
5656 continue;
5657 mark_ptr_or_null_reg(state, reg, id, is_null);
5658 }
5659}
5660
Thomas Graf57a09bf2016-10-18 19:51:19 +02005661/* The logic is similar to find_good_pkt_pointers(), both could eventually
5662 * be folded together at some point.
5663 */
Joe Stringer840b9612018-10-02 13:35:32 -07005664static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno,
5665 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02005666{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005667 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Paul Chaignonc6a9efa2019-04-24 21:50:42 +02005668 struct bpf_reg_state *regs = state->regs;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07005669 u32 ref_obj_id = regs[regno].ref_obj_id;
Daniel Borkmanna08dd0d2016-12-15 01:30:06 +01005670 u32 id = regs[regno].id;
Paul Chaignonc6a9efa2019-04-24 21:50:42 +02005671 int i;
Thomas Graf57a09bf2016-10-18 19:51:19 +02005672
Martin KaFai Lau1b986582019-03-12 10:23:02 -07005673 if (ref_obj_id && ref_obj_id == id && is_null)
5674 /* regs[regno] is in the " == NULL" branch.
5675 * No one could have freed the reference state before
5676 * doing the NULL check.
5677 */
5678 WARN_ON_ONCE(release_reference_state(state, id));
Joe Stringerfd978bf72018-10-02 13:35:35 -07005679
Paul Chaignonc6a9efa2019-04-24 21:50:42 +02005680 for (i = 0; i <= vstate->curframe; i++)
5681 __mark_ptr_or_null_regs(vstate->frame[i], id, is_null);
Thomas Graf57a09bf2016-10-18 19:51:19 +02005682}
5683
Daniel Borkmann5beca082017-11-01 23:58:10 +01005684static bool try_match_pkt_pointers(const struct bpf_insn *insn,
5685 struct bpf_reg_state *dst_reg,
5686 struct bpf_reg_state *src_reg,
5687 struct bpf_verifier_state *this_branch,
5688 struct bpf_verifier_state *other_branch)
5689{
5690 if (BPF_SRC(insn->code) != BPF_X)
5691 return false;
5692
Jiong Wang092ed092019-01-26 12:26:01 -05005693 /* Pointers are always 64-bit. */
5694 if (BPF_CLASS(insn->code) == BPF_JMP32)
5695 return false;
5696
Daniel Borkmann5beca082017-11-01 23:58:10 +01005697 switch (BPF_OP(insn->code)) {
5698 case BPF_JGT:
5699 if ((dst_reg->type == PTR_TO_PACKET &&
5700 src_reg->type == PTR_TO_PACKET_END) ||
5701 (dst_reg->type == PTR_TO_PACKET_META &&
5702 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
5703 /* pkt_data' > pkt_end, pkt_meta' > pkt_data */
5704 find_good_pkt_pointers(this_branch, dst_reg,
5705 dst_reg->type, false);
5706 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
5707 src_reg->type == PTR_TO_PACKET) ||
5708 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
5709 src_reg->type == PTR_TO_PACKET_META)) {
5710 /* pkt_end > pkt_data', pkt_data > pkt_meta' */
5711 find_good_pkt_pointers(other_branch, src_reg,
5712 src_reg->type, true);
5713 } else {
5714 return false;
5715 }
5716 break;
5717 case BPF_JLT:
5718 if ((dst_reg->type == PTR_TO_PACKET &&
5719 src_reg->type == PTR_TO_PACKET_END) ||
5720 (dst_reg->type == PTR_TO_PACKET_META &&
5721 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
5722 /* pkt_data' < pkt_end, pkt_meta' < pkt_data */
5723 find_good_pkt_pointers(other_branch, dst_reg,
5724 dst_reg->type, true);
5725 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
5726 src_reg->type == PTR_TO_PACKET) ||
5727 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
5728 src_reg->type == PTR_TO_PACKET_META)) {
5729 /* pkt_end < pkt_data', pkt_data > pkt_meta' */
5730 find_good_pkt_pointers(this_branch, src_reg,
5731 src_reg->type, false);
5732 } else {
5733 return false;
5734 }
5735 break;
5736 case BPF_JGE:
5737 if ((dst_reg->type == PTR_TO_PACKET &&
5738 src_reg->type == PTR_TO_PACKET_END) ||
5739 (dst_reg->type == PTR_TO_PACKET_META &&
5740 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
5741 /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */
5742 find_good_pkt_pointers(this_branch, dst_reg,
5743 dst_reg->type, true);
5744 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
5745 src_reg->type == PTR_TO_PACKET) ||
5746 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
5747 src_reg->type == PTR_TO_PACKET_META)) {
5748 /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */
5749 find_good_pkt_pointers(other_branch, src_reg,
5750 src_reg->type, false);
5751 } else {
5752 return false;
5753 }
5754 break;
5755 case BPF_JLE:
5756 if ((dst_reg->type == PTR_TO_PACKET &&
5757 src_reg->type == PTR_TO_PACKET_END) ||
5758 (dst_reg->type == PTR_TO_PACKET_META &&
5759 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
5760 /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */
5761 find_good_pkt_pointers(other_branch, dst_reg,
5762 dst_reg->type, false);
5763 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
5764 src_reg->type == PTR_TO_PACKET) ||
5765 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
5766 src_reg->type == PTR_TO_PACKET_META)) {
5767 /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */
5768 find_good_pkt_pointers(this_branch, src_reg,
5769 src_reg->type, true);
5770 } else {
5771 return false;
5772 }
5773 break;
5774 default:
5775 return false;
5776 }
5777
5778 return true;
5779}
5780
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005781static int check_cond_jmp_op(struct bpf_verifier_env *env,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005782 struct bpf_insn *insn, int *insn_idx)
5783{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005784 struct bpf_verifier_state *this_branch = env->cur_state;
5785 struct bpf_verifier_state *other_branch;
5786 struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs;
Alexei Starovoitovfb8d2512019-06-15 12:12:19 -07005787 struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005788 u8 opcode = BPF_OP(insn->code);
Jiong Wang092ed092019-01-26 12:26:01 -05005789 bool is_jmp32;
Alexei Starovoitovfb8d2512019-06-15 12:12:19 -07005790 int pred = -1;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005791 int err;
5792
Jiong Wang092ed092019-01-26 12:26:01 -05005793 /* Only conditional jumps are expected to reach here. */
5794 if (opcode == BPF_JA || opcode > BPF_JSLE) {
5795 verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005796 return -EINVAL;
5797 }
5798
5799 if (BPF_SRC(insn->code) == BPF_X) {
5800 if (insn->imm != 0) {
Jiong Wang092ed092019-01-26 12:26:01 -05005801 verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005802 return -EINVAL;
5803 }
5804
5805 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01005806 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005807 if (err)
5808 return err;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07005809
5810 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005811 verbose(env, "R%d pointer comparison prohibited\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07005812 insn->src_reg);
5813 return -EACCES;
5814 }
Alexei Starovoitovfb8d2512019-06-15 12:12:19 -07005815 src_reg = &regs[insn->src_reg];
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005816 } else {
5817 if (insn->src_reg != BPF_REG_0) {
Jiong Wang092ed092019-01-26 12:26:01 -05005818 verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005819 return -EINVAL;
5820 }
5821 }
5822
5823 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01005824 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005825 if (err)
5826 return err;
5827
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07005828 dst_reg = &regs[insn->dst_reg];
Jiong Wang092ed092019-01-26 12:26:01 -05005829 is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32;
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07005830
Alexei Starovoitovfb8d2512019-06-15 12:12:19 -07005831 if (BPF_SRC(insn->code) == BPF_K)
5832 pred = is_branch_taken(dst_reg, insn->imm,
5833 opcode, is_jmp32);
5834 else if (src_reg->type == SCALAR_VALUE &&
5835 tnum_is_const(src_reg->var_off))
5836 pred = is_branch_taken(dst_reg, src_reg->var_off.value,
5837 opcode, is_jmp32);
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07005838 if (pred >= 0) {
5839 err = mark_chain_precision(env, insn->dst_reg);
5840 if (BPF_SRC(insn->code) == BPF_X && !err)
5841 err = mark_chain_precision(env, insn->src_reg);
5842 if (err)
5843 return err;
5844 }
Alexei Starovoitovfb8d2512019-06-15 12:12:19 -07005845 if (pred == 1) {
5846 /* only follow the goto, ignore fall-through */
5847 *insn_idx += insn->off;
5848 return 0;
5849 } else if (pred == 0) {
5850 /* only follow fall-through branch, since
5851 * that's where the program will go
5852 */
5853 return 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005854 }
5855
Daniel Borkmann979d63d2019-01-03 00:58:34 +01005856 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx,
5857 false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005858 if (!other_branch)
5859 return -EFAULT;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005860 other_branch_regs = other_branch->frame[other_branch->curframe]->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005861
Josef Bacik48461132016-09-28 10:54:32 -04005862 /* detect if we are comparing against a constant value so we can adjust
5863 * our min/max values for our dst register.
Edward Creef1174f72017-08-07 15:26:19 +01005864 * this is only legit if both are scalars (or pointers to the same
5865 * object, I suppose, but we don't support that right now), because
5866 * otherwise the different base pointers mean the offsets aren't
5867 * comparable.
Josef Bacik48461132016-09-28 10:54:32 -04005868 */
5869 if (BPF_SRC(insn->code) == BPF_X) {
Jiong Wang092ed092019-01-26 12:26:01 -05005870 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
5871 struct bpf_reg_state lo_reg0 = *dst_reg;
5872 struct bpf_reg_state lo_reg1 = *src_reg;
5873 struct bpf_reg_state *src_lo, *dst_lo;
5874
5875 dst_lo = &lo_reg0;
5876 src_lo = &lo_reg1;
5877 coerce_reg_to_size(dst_lo, 4);
5878 coerce_reg_to_size(src_lo, 4);
5879
Edward Creef1174f72017-08-07 15:26:19 +01005880 if (dst_reg->type == SCALAR_VALUE &&
Jiong Wang092ed092019-01-26 12:26:01 -05005881 src_reg->type == SCALAR_VALUE) {
5882 if (tnum_is_const(src_reg->var_off) ||
5883 (is_jmp32 && tnum_is_const(src_lo->var_off)))
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005884 reg_set_min_max(&other_branch_regs[insn->dst_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05005885 dst_reg,
5886 is_jmp32
5887 ? src_lo->var_off.value
5888 : src_reg->var_off.value,
5889 opcode, is_jmp32);
5890 else if (tnum_is_const(dst_reg->var_off) ||
5891 (is_jmp32 && tnum_is_const(dst_lo->var_off)))
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005892 reg_set_min_max_inv(&other_branch_regs[insn->src_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05005893 src_reg,
5894 is_jmp32
5895 ? dst_lo->var_off.value
5896 : dst_reg->var_off.value,
5897 opcode, is_jmp32);
5898 else if (!is_jmp32 &&
5899 (opcode == BPF_JEQ || opcode == BPF_JNE))
Edward Creef1174f72017-08-07 15:26:19 +01005900 /* Comparing for equality, we can combine knowledge */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005901 reg_combine_min_max(&other_branch_regs[insn->src_reg],
5902 &other_branch_regs[insn->dst_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05005903 src_reg, dst_reg, opcode);
Edward Creef1174f72017-08-07 15:26:19 +01005904 }
5905 } else if (dst_reg->type == SCALAR_VALUE) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005906 reg_set_min_max(&other_branch_regs[insn->dst_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05005907 dst_reg, insn->imm, opcode, is_jmp32);
Josef Bacik48461132016-09-28 10:54:32 -04005908 }
5909
Jiong Wang092ed092019-01-26 12:26:01 -05005910 /* detect if R == 0 where R is returned from bpf_map_lookup_elem().
5911 * NOTE: these optimizations below are related with pointer comparison
5912 * which will never be JMP32.
5913 */
5914 if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07005915 insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
Joe Stringer840b9612018-10-02 13:35:32 -07005916 reg_type_may_be_null(dst_reg->type)) {
5917 /* Mark all identical registers in each branch as either
Thomas Graf57a09bf2016-10-18 19:51:19 +02005918 * safe or unknown depending R == 0 or R != 0 conditional.
5919 */
Joe Stringer840b9612018-10-02 13:35:32 -07005920 mark_ptr_or_null_regs(this_branch, insn->dst_reg,
5921 opcode == BPF_JNE);
5922 mark_ptr_or_null_regs(other_branch, insn->dst_reg,
5923 opcode == BPF_JEQ);
Daniel Borkmann5beca082017-11-01 23:58:10 +01005924 } else if (!try_match_pkt_pointers(insn, dst_reg, &regs[insn->src_reg],
5925 this_branch, other_branch) &&
5926 is_pointer_value(env, insn->dst_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005927 verbose(env, "R%d pointer comparison prohibited\n",
5928 insn->dst_reg);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07005929 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005930 }
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07005931 if (env->log.level & BPF_LOG_LEVEL)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005932 print_verifier_state(env, this_branch->frame[this_branch->curframe]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005933 return 0;
5934}
5935
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005936/* verify BPF_LD_IMM64 instruction */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005937static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005938{
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02005939 struct bpf_insn_aux_data *aux = cur_aux(env);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005940 struct bpf_reg_state *regs = cur_regs(env);
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02005941 struct bpf_map *map;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005942 int err;
5943
5944 if (BPF_SIZE(insn->code) != BPF_DW) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005945 verbose(env, "invalid BPF_LD_IMM insn\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005946 return -EINVAL;
5947 }
5948 if (insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005949 verbose(env, "BPF_LD_IMM64 uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005950 return -EINVAL;
5951 }
5952
Edward Creedc503a82017-08-15 20:34:35 +01005953 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005954 if (err)
5955 return err;
5956
Jakub Kicinski6b173872016-09-21 11:43:59 +01005957 if (insn->src_reg == 0) {
Jakub Kicinski6b173872016-09-21 11:43:59 +01005958 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
5959
Edward Creef1174f72017-08-07 15:26:19 +01005960 regs[insn->dst_reg].type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01005961 __mark_reg_known(&regs[insn->dst_reg], imm);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005962 return 0;
Jakub Kicinski6b173872016-09-21 11:43:59 +01005963 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005964
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02005965 map = env->used_maps[aux->map_index];
5966 mark_reg_known_zero(env, regs, insn->dst_reg);
5967 regs[insn->dst_reg].map_ptr = map;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005968
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02005969 if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) {
5970 regs[insn->dst_reg].type = PTR_TO_MAP_VALUE;
5971 regs[insn->dst_reg].off = aux->map_off;
5972 if (map_value_has_spin_lock(map))
5973 regs[insn->dst_reg].id = ++env->id_gen;
5974 } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) {
5975 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
5976 } else {
5977 verbose(env, "bpf verifier is misconfigured\n");
5978 return -EINVAL;
5979 }
5980
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005981 return 0;
5982}
5983
Daniel Borkmann96be4322015-03-01 12:31:46 +01005984static bool may_access_skb(enum bpf_prog_type type)
5985{
5986 switch (type) {
5987 case BPF_PROG_TYPE_SOCKET_FILTER:
5988 case BPF_PROG_TYPE_SCHED_CLS:
Daniel Borkmann94caee8c2015-03-20 15:11:11 +01005989 case BPF_PROG_TYPE_SCHED_ACT:
Daniel Borkmann96be4322015-03-01 12:31:46 +01005990 return true;
5991 default:
5992 return false;
5993 }
5994}
5995
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005996/* verify safety of LD_ABS|LD_IND instructions:
5997 * - they can only appear in the programs where ctx == skb
5998 * - since they are wrappers of function calls, they scratch R1-R5 registers,
5999 * preserve R6-R9, and store return value into R0
6000 *
6001 * Implicit input:
6002 * ctx == skb == R6 == CTX
6003 *
6004 * Explicit input:
6005 * SRC == any register
6006 * IMM == 32-bit immediate
6007 *
6008 * Output:
6009 * R0 - 8/16/32-bit skb data converted to cpu endianness
6010 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006011static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08006012{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006013 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08006014 u8 mode = BPF_MODE(insn->code);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08006015 int i, err;
6016
Daniel Borkmann24701ec2015-03-01 12:31:47 +01006017 if (!may_access_skb(env->prog->type)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006018 verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08006019 return -EINVAL;
6020 }
6021
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +02006022 if (!env->ops->gen_ld_abs) {
6023 verbose(env, "bpf verifier is misconfigured\n");
6024 return -EINVAL;
6025 }
6026
Jiong Wangf910cef2018-05-02 16:17:17 -04006027 if (env->subprog_cnt > 1) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006028 /* when program has LD_ABS insn JITs and interpreter assume
6029 * that r1 == ctx == skb which is not the case for callees
6030 * that can have arbitrary arguments. It's problematic
6031 * for main prog as well since JITs would need to analyze
6032 * all functions in order to make proper register save/restore
6033 * decisions in the main prog. Hence disallow LD_ABS with calls
6034 */
6035 verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n");
6036 return -EINVAL;
6037 }
6038
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08006039 if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
Alexei Starovoitovd82bccc2016-04-12 10:26:19 -07006040 BPF_SIZE(insn->code) == BPF_DW ||
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08006041 (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006042 verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08006043 return -EINVAL;
6044 }
6045
6046 /* check whether implicit source operand (register R6) is readable */
Edward Creedc503a82017-08-15 20:34:35 +01006047 err = check_reg_arg(env, BPF_REG_6, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08006048 if (err)
6049 return err;
6050
Joe Stringerfd978bf72018-10-02 13:35:35 -07006051 /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as
6052 * gen_ld_abs() may terminate the program at runtime, leading to
6053 * reference leak.
6054 */
6055 err = check_reference_leak(env);
6056 if (err) {
6057 verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n");
6058 return err;
6059 }
6060
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08006061 if (env->cur_state->active_spin_lock) {
6062 verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n");
6063 return -EINVAL;
6064 }
6065
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08006066 if (regs[BPF_REG_6].type != PTR_TO_CTX) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006067 verbose(env,
6068 "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08006069 return -EINVAL;
6070 }
6071
6072 if (mode == BPF_IND) {
6073 /* check explicit source operand */
Edward Creedc503a82017-08-15 20:34:35 +01006074 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08006075 if (err)
6076 return err;
6077 }
6078
6079 /* reset caller saved regs to unreadable */
Edward Creedc503a82017-08-15 20:34:35 +01006080 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006081 mark_reg_not_init(env, regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01006082 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
6083 }
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08006084
6085 /* mark destination R0 register as readable, since it contains
Edward Creedc503a82017-08-15 20:34:35 +01006086 * the value fetched from the packet.
6087 * Already marked as written above.
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08006088 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006089 mark_reg_unknown(env, regs, BPF_REG_0);
Jiong Wang5327ed32019-05-24 23:25:12 +01006090 /* ld_abs load up to 32-bit skb data. */
6091 regs[BPF_REG_0].subreg_def = env->insn_idx + 1;
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08006092 return 0;
6093}
6094
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07006095static int check_return_code(struct bpf_verifier_env *env)
6096{
brakmo5cf1e912019-05-28 16:59:36 -07006097 struct tnum enforce_attach_type_range = tnum_unknown;
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07006098 struct bpf_reg_state *reg;
6099 struct tnum range = tnum_range(0, 1);
6100
6101 switch (env->prog->type) {
Daniel Borkmann983695f2019-06-07 01:48:57 +02006102 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
6103 if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG ||
6104 env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG)
6105 range = tnum_range(1, 1);
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07006106 case BPF_PROG_TYPE_CGROUP_SKB:
brakmo5cf1e912019-05-28 16:59:36 -07006107 if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) {
6108 range = tnum_range(0, 3);
6109 enforce_attach_type_range = tnum_range(2, 3);
6110 }
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07006111 case BPF_PROG_TYPE_CGROUP_SOCK:
6112 case BPF_PROG_TYPE_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05006113 case BPF_PROG_TYPE_CGROUP_DEVICE:
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08006114 case BPF_PROG_TYPE_CGROUP_SYSCTL:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07006115 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07006116 break;
6117 default:
6118 return 0;
6119 }
6120
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006121 reg = cur_regs(env) + BPF_REG_0;
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07006122 if (reg->type != SCALAR_VALUE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006123 verbose(env, "At program exit the register R0 is not a known value (%s)\n",
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07006124 reg_type_str[reg->type]);
6125 return -EINVAL;
6126 }
6127
6128 if (!tnum_in(range, reg->var_off)) {
brakmo5cf1e912019-05-28 16:59:36 -07006129 char tn_buf[48];
6130
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006131 verbose(env, "At program exit the register R0 ");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07006132 if (!tnum_is_unknown(reg->var_off)) {
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07006133 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006134 verbose(env, "has value %s", tn_buf);
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07006135 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006136 verbose(env, "has unknown scalar value");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07006137 }
brakmo5cf1e912019-05-28 16:59:36 -07006138 tnum_strn(tn_buf, sizeof(tn_buf), range);
Daniel Borkmann983695f2019-06-07 01:48:57 +02006139 verbose(env, " should have been in %s\n", tn_buf);
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07006140 return -EINVAL;
6141 }
brakmo5cf1e912019-05-28 16:59:36 -07006142
6143 if (!tnum_is_unknown(enforce_attach_type_range) &&
6144 tnum_in(enforce_attach_type_range, reg->var_off))
6145 env->prog->enforce_expected_attach_type = 1;
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07006146 return 0;
6147}
6148
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006149/* non-recursive DFS pseudo code
6150 * 1 procedure DFS-iterative(G,v):
6151 * 2 label v as discovered
6152 * 3 let S be a stack
6153 * 4 S.push(v)
6154 * 5 while S is not empty
6155 * 6 t <- S.pop()
6156 * 7 if t is what we're looking for:
6157 * 8 return t
6158 * 9 for all edges e in G.adjacentEdges(t) do
6159 * 10 if edge e is already labelled
6160 * 11 continue with the next edge
6161 * 12 w <- G.adjacentVertex(t,e)
6162 * 13 if vertex w is not discovered and not explored
6163 * 14 label e as tree-edge
6164 * 15 label w as discovered
6165 * 16 S.push(w)
6166 * 17 continue at 5
6167 * 18 else if vertex w is discovered
6168 * 19 label e as back-edge
6169 * 20 else
6170 * 21 // vertex w is explored
6171 * 22 label e as forward- or cross-edge
6172 * 23 label t as explored
6173 * 24 S.pop()
6174 *
6175 * convention:
6176 * 0x10 - discovered
6177 * 0x11 - discovered and fall-through edge labelled
6178 * 0x12 - discovered and fall-through and branch edges labelled
6179 * 0x20 - explored
6180 */
6181
6182enum {
6183 DISCOVERED = 0x10,
6184 EXPLORED = 0x20,
6185 FALLTHROUGH = 1,
6186 BRANCH = 2,
6187};
6188
Alexei Starovoitovdc2a4eb2019-05-21 20:17:07 -07006189static u32 state_htab_size(struct bpf_verifier_env *env)
6190{
6191 return env->prog->len;
6192}
6193
Alexei Starovoitov5d839022019-05-21 20:17:05 -07006194static struct bpf_verifier_state_list **explored_state(
6195 struct bpf_verifier_env *env,
6196 int idx)
6197{
Alexei Starovoitovdc2a4eb2019-05-21 20:17:07 -07006198 struct bpf_verifier_state *cur = env->cur_state;
6199 struct bpf_func_state *state = cur->frame[cur->curframe];
6200
6201 return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)];
Alexei Starovoitov5d839022019-05-21 20:17:05 -07006202}
6203
6204static void init_explored_state(struct bpf_verifier_env *env, int idx)
6205{
Alexei Starovoitova8f500a2019-05-21 20:17:06 -07006206 env->insn_aux_data[idx].prune_point = true;
Alexei Starovoitov5d839022019-05-21 20:17:05 -07006207}
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006208
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006209/* t, w, e - match pseudo-code above:
6210 * t - index of current instruction
6211 * w - next instruction
6212 * e - edge
6213 */
Alexei Starovoitov25897262019-06-15 12:12:20 -07006214static int push_insn(int t, int w, int e, struct bpf_verifier_env *env,
6215 bool loop_ok)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006216{
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07006217 int *insn_stack = env->cfg.insn_stack;
6218 int *insn_state = env->cfg.insn_state;
6219
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006220 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
6221 return 0;
6222
6223 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
6224 return 0;
6225
6226 if (w < 0 || w >= env->prog->len) {
Martin KaFai Laud9762e82018-12-13 10:41:48 -08006227 verbose_linfo(env, t, "%d: ", t);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006228 verbose(env, "jump out of range from insn %d to %d\n", t, w);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006229 return -EINVAL;
6230 }
6231
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006232 if (e == BRANCH)
6233 /* mark branch target for state pruning */
Alexei Starovoitov5d839022019-05-21 20:17:05 -07006234 init_explored_state(env, w);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006235
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006236 if (insn_state[w] == 0) {
6237 /* tree-edge */
6238 insn_state[t] = DISCOVERED | e;
6239 insn_state[w] = DISCOVERED;
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07006240 if (env->cfg.cur_stack >= env->prog->len)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006241 return -E2BIG;
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07006242 insn_stack[env->cfg.cur_stack++] = w;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006243 return 1;
6244 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
Alexei Starovoitov25897262019-06-15 12:12:20 -07006245 if (loop_ok && env->allow_ptr_leaks)
6246 return 0;
Martin KaFai Laud9762e82018-12-13 10:41:48 -08006247 verbose_linfo(env, t, "%d: ", t);
6248 verbose_linfo(env, w, "%d: ", w);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006249 verbose(env, "back-edge from insn %d to %d\n", t, w);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006250 return -EINVAL;
6251 } else if (insn_state[w] == EXPLORED) {
6252 /* forward- or cross-edge */
6253 insn_state[t] = DISCOVERED | e;
6254 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006255 verbose(env, "insn state internal bug\n");
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006256 return -EFAULT;
6257 }
6258 return 0;
6259}
6260
6261/* non-recursive depth-first-search to detect loops in BPF program
6262 * loop == back-edge in directed graph
6263 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006264static int check_cfg(struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006265{
6266 struct bpf_insn *insns = env->prog->insnsi;
6267 int insn_cnt = env->prog->len;
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07006268 int *insn_stack, *insn_state;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006269 int ret = 0;
6270 int i, t;
6271
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07006272 insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006273 if (!insn_state)
6274 return -ENOMEM;
6275
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07006276 insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006277 if (!insn_stack) {
Alexei Starovoitov71dde682019-04-01 21:27:43 -07006278 kvfree(insn_state);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006279 return -ENOMEM;
6280 }
6281
6282 insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
6283 insn_stack[0] = 0; /* 0 is the first instruction */
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07006284 env->cfg.cur_stack = 1;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006285
6286peek_stack:
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07006287 if (env->cfg.cur_stack == 0)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006288 goto check_state;
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07006289 t = insn_stack[env->cfg.cur_stack - 1];
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006290
Jiong Wang092ed092019-01-26 12:26:01 -05006291 if (BPF_CLASS(insns[t].code) == BPF_JMP ||
6292 BPF_CLASS(insns[t].code) == BPF_JMP32) {
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006293 u8 opcode = BPF_OP(insns[t].code);
6294
6295 if (opcode == BPF_EXIT) {
6296 goto mark_explored;
6297 } else if (opcode == BPF_CALL) {
Alexei Starovoitov25897262019-06-15 12:12:20 -07006298 ret = push_insn(t, t + 1, FALLTHROUGH, env, false);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006299 if (ret == 1)
6300 goto peek_stack;
6301 else if (ret < 0)
6302 goto err_free;
Daniel Borkmann07016152016-04-05 22:33:17 +02006303 if (t + 1 < insn_cnt)
Alexei Starovoitov5d839022019-05-21 20:17:05 -07006304 init_explored_state(env, t + 1);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08006305 if (insns[t].src_reg == BPF_PSEUDO_CALL) {
Alexei Starovoitov5d839022019-05-21 20:17:05 -07006306 init_explored_state(env, t);
Alexei Starovoitov25897262019-06-15 12:12:20 -07006307 ret = push_insn(t, t + insns[t].imm + 1, BRANCH,
6308 env, false);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08006309 if (ret == 1)
6310 goto peek_stack;
6311 else if (ret < 0)
6312 goto err_free;
6313 }
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006314 } else if (opcode == BPF_JA) {
6315 if (BPF_SRC(insns[t].code) != BPF_K) {
6316 ret = -EINVAL;
6317 goto err_free;
6318 }
6319 /* unconditional jump with single edge */
6320 ret = push_insn(t, t + insns[t].off + 1,
Alexei Starovoitov25897262019-06-15 12:12:20 -07006321 FALLTHROUGH, env, true);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006322 if (ret == 1)
6323 goto peek_stack;
6324 else if (ret < 0)
6325 goto err_free;
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07006326 /* unconditional jmp is not a good pruning point,
6327 * but it's marked, since backtracking needs
6328 * to record jmp history in is_state_visited().
6329 */
6330 init_explored_state(env, t + insns[t].off + 1);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006331 /* tell verifier to check for equivalent states
6332 * after every call and jump
6333 */
Alexei Starovoitovc3de6312015-04-14 15:57:13 -07006334 if (t + 1 < insn_cnt)
Alexei Starovoitov5d839022019-05-21 20:17:05 -07006335 init_explored_state(env, t + 1);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006336 } else {
6337 /* conditional jump with two edges */
Alexei Starovoitov5d839022019-05-21 20:17:05 -07006338 init_explored_state(env, t);
Alexei Starovoitov25897262019-06-15 12:12:20 -07006339 ret = push_insn(t, t + 1, FALLTHROUGH, env, true);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006340 if (ret == 1)
6341 goto peek_stack;
6342 else if (ret < 0)
6343 goto err_free;
6344
Alexei Starovoitov25897262019-06-15 12:12:20 -07006345 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env, true);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006346 if (ret == 1)
6347 goto peek_stack;
6348 else if (ret < 0)
6349 goto err_free;
6350 }
6351 } else {
6352 /* all other non-branch instructions with single
6353 * fall-through edge
6354 */
Alexei Starovoitov25897262019-06-15 12:12:20 -07006355 ret = push_insn(t, t + 1, FALLTHROUGH, env, false);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006356 if (ret == 1)
6357 goto peek_stack;
6358 else if (ret < 0)
6359 goto err_free;
6360 }
6361
6362mark_explored:
6363 insn_state[t] = EXPLORED;
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07006364 if (env->cfg.cur_stack-- <= 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006365 verbose(env, "pop stack internal bug\n");
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006366 ret = -EFAULT;
6367 goto err_free;
6368 }
6369 goto peek_stack;
6370
6371check_state:
6372 for (i = 0; i < insn_cnt; i++) {
6373 if (insn_state[i] != EXPLORED) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006374 verbose(env, "unreachable insn %d\n", i);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006375 ret = -EINVAL;
6376 goto err_free;
6377 }
6378 }
6379 ret = 0; /* cfg looks good */
6380
6381err_free:
Alexei Starovoitov71dde682019-04-01 21:27:43 -07006382 kvfree(insn_state);
6383 kvfree(insn_stack);
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07006384 env->cfg.insn_state = env->cfg.insn_stack = NULL;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07006385 return ret;
6386}
6387
Yonghong Song838e9692018-11-19 15:29:11 -08006388/* The minimum supported BTF func info size */
6389#define MIN_BPF_FUNCINFO_SIZE 8
6390#define MAX_FUNCINFO_REC_SIZE 252
6391
Martin KaFai Lauc454a462018-12-07 16:42:25 -08006392static int check_btf_func(struct bpf_verifier_env *env,
6393 const union bpf_attr *attr,
6394 union bpf_attr __user *uattr)
Yonghong Song838e9692018-11-19 15:29:11 -08006395{
Peter Oskolkovd0b28182019-01-16 10:43:01 -08006396 u32 i, nfuncs, urec_size, min_size;
Yonghong Song838e9692018-11-19 15:29:11 -08006397 u32 krec_size = sizeof(struct bpf_func_info);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08006398 struct bpf_func_info *krecord;
Yonghong Song838e9692018-11-19 15:29:11 -08006399 const struct btf_type *type;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08006400 struct bpf_prog *prog;
6401 const struct btf *btf;
Yonghong Song838e9692018-11-19 15:29:11 -08006402 void __user *urecord;
Peter Oskolkovd0b28182019-01-16 10:43:01 -08006403 u32 prev_offset = 0;
Yonghong Song838e9692018-11-19 15:29:11 -08006404 int ret = 0;
6405
6406 nfuncs = attr->func_info_cnt;
6407 if (!nfuncs)
6408 return 0;
6409
6410 if (nfuncs != env->subprog_cnt) {
6411 verbose(env, "number of funcs in func_info doesn't match number of subprogs\n");
6412 return -EINVAL;
6413 }
6414
6415 urec_size = attr->func_info_rec_size;
6416 if (urec_size < MIN_BPF_FUNCINFO_SIZE ||
6417 urec_size > MAX_FUNCINFO_REC_SIZE ||
6418 urec_size % sizeof(u32)) {
6419 verbose(env, "invalid func info rec size %u\n", urec_size);
6420 return -EINVAL;
6421 }
6422
Martin KaFai Lauc454a462018-12-07 16:42:25 -08006423 prog = env->prog;
6424 btf = prog->aux->btf;
Yonghong Song838e9692018-11-19 15:29:11 -08006425
6426 urecord = u64_to_user_ptr(attr->func_info);
6427 min_size = min_t(u32, krec_size, urec_size);
6428
Yonghong Songba64e7d2018-11-24 23:20:44 -08006429 krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08006430 if (!krecord)
6431 return -ENOMEM;
Yonghong Songba64e7d2018-11-24 23:20:44 -08006432
Yonghong Song838e9692018-11-19 15:29:11 -08006433 for (i = 0; i < nfuncs; i++) {
6434 ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size);
6435 if (ret) {
6436 if (ret == -E2BIG) {
6437 verbose(env, "nonzero tailing record in func info");
6438 /* set the size kernel expects so loader can zero
6439 * out the rest of the record.
6440 */
6441 if (put_user(min_size, &uattr->func_info_rec_size))
6442 ret = -EFAULT;
6443 }
Martin KaFai Lauc454a462018-12-07 16:42:25 -08006444 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08006445 }
6446
Yonghong Songba64e7d2018-11-24 23:20:44 -08006447 if (copy_from_user(&krecord[i], urecord, min_size)) {
Yonghong Song838e9692018-11-19 15:29:11 -08006448 ret = -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08006449 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08006450 }
6451
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08006452 /* check insn_off */
Yonghong Song838e9692018-11-19 15:29:11 -08006453 if (i == 0) {
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08006454 if (krecord[i].insn_off) {
Yonghong Song838e9692018-11-19 15:29:11 -08006455 verbose(env,
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08006456 "nonzero insn_off %u for the first func info record",
6457 krecord[i].insn_off);
Yonghong Song838e9692018-11-19 15:29:11 -08006458 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08006459 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08006460 }
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08006461 } else if (krecord[i].insn_off <= prev_offset) {
Yonghong Song838e9692018-11-19 15:29:11 -08006462 verbose(env,
6463 "same or smaller insn offset (%u) than previous func info record (%u)",
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08006464 krecord[i].insn_off, prev_offset);
Yonghong Song838e9692018-11-19 15:29:11 -08006465 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08006466 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08006467 }
6468
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08006469 if (env->subprog_info[i].start != krecord[i].insn_off) {
Yonghong Song838e9692018-11-19 15:29:11 -08006470 verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n");
6471 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08006472 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08006473 }
6474
6475 /* check type_id */
Yonghong Songba64e7d2018-11-24 23:20:44 -08006476 type = btf_type_by_id(btf, krecord[i].type_id);
Yonghong Song838e9692018-11-19 15:29:11 -08006477 if (!type || BTF_INFO_KIND(type->info) != BTF_KIND_FUNC) {
6478 verbose(env, "invalid type id %d in func info",
Yonghong Songba64e7d2018-11-24 23:20:44 -08006479 krecord[i].type_id);
Yonghong Song838e9692018-11-19 15:29:11 -08006480 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08006481 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08006482 }
6483
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08006484 prev_offset = krecord[i].insn_off;
Yonghong Song838e9692018-11-19 15:29:11 -08006485 urecord += urec_size;
6486 }
6487
Yonghong Songba64e7d2018-11-24 23:20:44 -08006488 prog->aux->func_info = krecord;
6489 prog->aux->func_info_cnt = nfuncs;
Yonghong Song838e9692018-11-19 15:29:11 -08006490 return 0;
6491
Martin KaFai Lauc454a462018-12-07 16:42:25 -08006492err_free:
Yonghong Songba64e7d2018-11-24 23:20:44 -08006493 kvfree(krecord);
Yonghong Song838e9692018-11-19 15:29:11 -08006494 return ret;
6495}
6496
Yonghong Songba64e7d2018-11-24 23:20:44 -08006497static void adjust_btf_func(struct bpf_verifier_env *env)
6498{
6499 int i;
6500
6501 if (!env->prog->aux->func_info)
6502 return;
6503
6504 for (i = 0; i < env->subprog_cnt; i++)
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08006505 env->prog->aux->func_info[i].insn_off = env->subprog_info[i].start;
Yonghong Songba64e7d2018-11-24 23:20:44 -08006506}
6507
Martin KaFai Lauc454a462018-12-07 16:42:25 -08006508#define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \
6509 sizeof(((struct bpf_line_info *)(0))->line_col))
6510#define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE
6511
6512static int check_btf_line(struct bpf_verifier_env *env,
6513 const union bpf_attr *attr,
6514 union bpf_attr __user *uattr)
6515{
6516 u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0;
6517 struct bpf_subprog_info *sub;
6518 struct bpf_line_info *linfo;
6519 struct bpf_prog *prog;
6520 const struct btf *btf;
6521 void __user *ulinfo;
6522 int err;
6523
6524 nr_linfo = attr->line_info_cnt;
6525 if (!nr_linfo)
6526 return 0;
6527
6528 rec_size = attr->line_info_rec_size;
6529 if (rec_size < MIN_BPF_LINEINFO_SIZE ||
6530 rec_size > MAX_LINEINFO_REC_SIZE ||
6531 rec_size & (sizeof(u32) - 1))
6532 return -EINVAL;
6533
6534 /* Need to zero it in case the userspace may
6535 * pass in a smaller bpf_line_info object.
6536 */
6537 linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info),
6538 GFP_KERNEL | __GFP_NOWARN);
6539 if (!linfo)
6540 return -ENOMEM;
6541
6542 prog = env->prog;
6543 btf = prog->aux->btf;
6544
6545 s = 0;
6546 sub = env->subprog_info;
6547 ulinfo = u64_to_user_ptr(attr->line_info);
6548 expected_size = sizeof(struct bpf_line_info);
6549 ncopy = min_t(u32, expected_size, rec_size);
6550 for (i = 0; i < nr_linfo; i++) {
6551 err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size);
6552 if (err) {
6553 if (err == -E2BIG) {
6554 verbose(env, "nonzero tailing record in line_info");
6555 if (put_user(expected_size,
6556 &uattr->line_info_rec_size))
6557 err = -EFAULT;
6558 }
6559 goto err_free;
6560 }
6561
6562 if (copy_from_user(&linfo[i], ulinfo, ncopy)) {
6563 err = -EFAULT;
6564 goto err_free;
6565 }
6566
6567 /*
6568 * Check insn_off to ensure
6569 * 1) strictly increasing AND
6570 * 2) bounded by prog->len
6571 *
6572 * The linfo[0].insn_off == 0 check logically falls into
6573 * the later "missing bpf_line_info for func..." case
6574 * because the first linfo[0].insn_off must be the
6575 * first sub also and the first sub must have
6576 * subprog_info[0].start == 0.
6577 */
6578 if ((i && linfo[i].insn_off <= prev_offset) ||
6579 linfo[i].insn_off >= prog->len) {
6580 verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n",
6581 i, linfo[i].insn_off, prev_offset,
6582 prog->len);
6583 err = -EINVAL;
6584 goto err_free;
6585 }
6586
Martin KaFai Laufdbaa0b2018-12-19 13:01:01 -08006587 if (!prog->insnsi[linfo[i].insn_off].code) {
6588 verbose(env,
6589 "Invalid insn code at line_info[%u].insn_off\n",
6590 i);
6591 err = -EINVAL;
6592 goto err_free;
6593 }
6594
Martin KaFai Lau23127b32018-12-13 10:41:46 -08006595 if (!btf_name_by_offset(btf, linfo[i].line_off) ||
6596 !btf_name_by_offset(btf, linfo[i].file_name_off)) {
Martin KaFai Lauc454a462018-12-07 16:42:25 -08006597 verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i);
6598 err = -EINVAL;
6599 goto err_free;
6600 }
6601
6602 if (s != env->subprog_cnt) {
6603 if (linfo[i].insn_off == sub[s].start) {
6604 sub[s].linfo_idx = i;
6605 s++;
6606 } else if (sub[s].start < linfo[i].insn_off) {
6607 verbose(env, "missing bpf_line_info for func#%u\n", s);
6608 err = -EINVAL;
6609 goto err_free;
6610 }
6611 }
6612
6613 prev_offset = linfo[i].insn_off;
6614 ulinfo += rec_size;
6615 }
6616
6617 if (s != env->subprog_cnt) {
6618 verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n",
6619 env->subprog_cnt - s, s);
6620 err = -EINVAL;
6621 goto err_free;
6622 }
6623
6624 prog->aux->linfo = linfo;
6625 prog->aux->nr_linfo = nr_linfo;
6626
6627 return 0;
6628
6629err_free:
6630 kvfree(linfo);
6631 return err;
6632}
6633
6634static int check_btf_info(struct bpf_verifier_env *env,
6635 const union bpf_attr *attr,
6636 union bpf_attr __user *uattr)
6637{
6638 struct btf *btf;
6639 int err;
6640
6641 if (!attr->func_info_cnt && !attr->line_info_cnt)
6642 return 0;
6643
6644 btf = btf_get_by_fd(attr->prog_btf_fd);
6645 if (IS_ERR(btf))
6646 return PTR_ERR(btf);
6647 env->prog->aux->btf = btf;
6648
6649 err = check_btf_func(env, attr, uattr);
6650 if (err)
6651 return err;
6652
6653 err = check_btf_line(env, attr, uattr);
6654 if (err)
6655 return err;
6656
6657 return 0;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006658}
6659
Edward Creef1174f72017-08-07 15:26:19 +01006660/* check %cur's range satisfies %old's */
6661static bool range_within(struct bpf_reg_state *old,
6662 struct bpf_reg_state *cur)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07006663{
Edward Creeb03c9f92017-08-07 15:26:36 +01006664 return old->umin_value <= cur->umin_value &&
6665 old->umax_value >= cur->umax_value &&
6666 old->smin_value <= cur->smin_value &&
6667 old->smax_value >= cur->smax_value;
Edward Creef1174f72017-08-07 15:26:19 +01006668}
6669
6670/* Maximum number of register states that can exist at once */
6671#define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE)
6672struct idpair {
6673 u32 old;
6674 u32 cur;
6675};
6676
6677/* If in the old state two registers had the same id, then they need to have
6678 * the same id in the new state as well. But that id could be different from
6679 * the old state, so we need to track the mapping from old to new ids.
6680 * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent
6681 * regs with old id 5 must also have new id 9 for the new state to be safe. But
6682 * regs with a different old id could still have new id 9, we don't care about
6683 * that.
6684 * So we look through our idmap to see if this old id has been seen before. If
6685 * so, we require the new id to match; otherwise, we add the id pair to the map.
6686 */
6687static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap)
6688{
6689 unsigned int i;
6690
6691 for (i = 0; i < ID_MAP_SIZE; i++) {
6692 if (!idmap[i].old) {
6693 /* Reached an empty slot; haven't seen this id before */
6694 idmap[i].old = old_id;
6695 idmap[i].cur = cur_id;
6696 return true;
6697 }
6698 if (idmap[i].old == old_id)
6699 return idmap[i].cur == cur_id;
6700 }
6701 /* We ran out of idmap slots, which should be impossible */
6702 WARN_ON_ONCE(1);
6703 return false;
6704}
6705
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -08006706static void clean_func_state(struct bpf_verifier_env *env,
6707 struct bpf_func_state *st)
6708{
6709 enum bpf_reg_liveness live;
6710 int i, j;
6711
6712 for (i = 0; i < BPF_REG_FP; i++) {
6713 live = st->regs[i].live;
6714 /* liveness must not touch this register anymore */
6715 st->regs[i].live |= REG_LIVE_DONE;
6716 if (!(live & REG_LIVE_READ))
6717 /* since the register is unused, clear its state
6718 * to make further comparison simpler
6719 */
6720 __mark_reg_not_init(&st->regs[i]);
6721 }
6722
6723 for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) {
6724 live = st->stack[i].spilled_ptr.live;
6725 /* liveness must not touch this stack slot anymore */
6726 st->stack[i].spilled_ptr.live |= REG_LIVE_DONE;
6727 if (!(live & REG_LIVE_READ)) {
6728 __mark_reg_not_init(&st->stack[i].spilled_ptr);
6729 for (j = 0; j < BPF_REG_SIZE; j++)
6730 st->stack[i].slot_type[j] = STACK_INVALID;
6731 }
6732 }
6733}
6734
6735static void clean_verifier_state(struct bpf_verifier_env *env,
6736 struct bpf_verifier_state *st)
6737{
6738 int i;
6739
6740 if (st->frame[0]->regs[0].live & REG_LIVE_DONE)
6741 /* all regs in this state in all frames were already marked */
6742 return;
6743
6744 for (i = 0; i <= st->curframe; i++)
6745 clean_func_state(env, st->frame[i]);
6746}
6747
6748/* the parentage chains form a tree.
6749 * the verifier states are added to state lists at given insn and
6750 * pushed into state stack for future exploration.
6751 * when the verifier reaches bpf_exit insn some of the verifer states
6752 * stored in the state lists have their final liveness state already,
6753 * but a lot of states will get revised from liveness point of view when
6754 * the verifier explores other branches.
6755 * Example:
6756 * 1: r0 = 1
6757 * 2: if r1 == 100 goto pc+1
6758 * 3: r0 = 2
6759 * 4: exit
6760 * when the verifier reaches exit insn the register r0 in the state list of
6761 * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch
6762 * of insn 2 and goes exploring further. At the insn 4 it will walk the
6763 * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ.
6764 *
6765 * Since the verifier pushes the branch states as it sees them while exploring
6766 * the program the condition of walking the branch instruction for the second
6767 * time means that all states below this branch were already explored and
6768 * their final liveness markes are already propagated.
6769 * Hence when the verifier completes the search of state list in is_state_visited()
6770 * we can call this clean_live_states() function to mark all liveness states
6771 * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state'
6772 * will not be used.
6773 * This function also clears the registers and stack for states that !READ
6774 * to simplify state merging.
6775 *
6776 * Important note here that walking the same branch instruction in the callee
6777 * doesn't meant that the states are DONE. The verifier has to compare
6778 * the callsites
6779 */
6780static void clean_live_states(struct bpf_verifier_env *env, int insn,
6781 struct bpf_verifier_state *cur)
6782{
6783 struct bpf_verifier_state_list *sl;
6784 int i;
6785
Alexei Starovoitov5d839022019-05-21 20:17:05 -07006786 sl = *explored_state(env, insn);
Alexei Starovoitova8f500a2019-05-21 20:17:06 -07006787 while (sl) {
Alexei Starovoitov25897262019-06-15 12:12:20 -07006788 if (sl->state.branches)
6789 goto next;
Alexei Starovoitovdc2a4eb2019-05-21 20:17:07 -07006790 if (sl->state.insn_idx != insn ||
6791 sl->state.curframe != cur->curframe)
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -08006792 goto next;
6793 for (i = 0; i <= cur->curframe; i++)
6794 if (sl->state.frame[i]->callsite != cur->frame[i]->callsite)
6795 goto next;
6796 clean_verifier_state(env, &sl->state);
6797next:
6798 sl = sl->next;
6799 }
6800}
6801
Edward Creef1174f72017-08-07 15:26:19 +01006802/* Returns true if (rold safe implies rcur safe) */
Edward Cree1b688a12017-08-23 15:10:50 +01006803static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
6804 struct idpair *idmap)
Edward Creef1174f72017-08-07 15:26:19 +01006805{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006806 bool equal;
6807
Edward Creedc503a82017-08-15 20:34:35 +01006808 if (!(rold->live & REG_LIVE_READ))
6809 /* explored state didn't use this */
6810 return true;
6811
Edward Cree679c7822018-08-22 20:02:19 +01006812 equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006813
6814 if (rold->type == PTR_TO_STACK)
6815 /* two stack pointers are equal only if they're pointing to
6816 * the same stack frame, since fp-8 in foo != fp-8 in bar
6817 */
6818 return equal && rold->frameno == rcur->frameno;
6819
6820 if (equal)
Edward Creef1174f72017-08-07 15:26:19 +01006821 return true;
6822
6823 if (rold->type == NOT_INIT)
6824 /* explored state can't have used this */
6825 return true;
6826 if (rcur->type == NOT_INIT)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07006827 return false;
Edward Creef1174f72017-08-07 15:26:19 +01006828 switch (rold->type) {
6829 case SCALAR_VALUE:
6830 if (rcur->type == SCALAR_VALUE) {
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07006831 if (!rold->precise && !rcur->precise)
6832 return true;
Edward Creef1174f72017-08-07 15:26:19 +01006833 /* new val must satisfy old val knowledge */
6834 return range_within(rold, rcur) &&
6835 tnum_in(rold->var_off, rcur->var_off);
6836 } else {
Jann Horn179d1c52017-12-18 20:11:59 -08006837 /* We're trying to use a pointer in place of a scalar.
6838 * Even if the scalar was unbounded, this could lead to
6839 * pointer leaks because scalars are allowed to leak
6840 * while pointers are not. We could make this safe in
6841 * special cases if root is calling us, but it's
6842 * probably not worth the hassle.
Edward Creef1174f72017-08-07 15:26:19 +01006843 */
Jann Horn179d1c52017-12-18 20:11:59 -08006844 return false;
Edward Creef1174f72017-08-07 15:26:19 +01006845 }
6846 case PTR_TO_MAP_VALUE:
Edward Cree1b688a12017-08-23 15:10:50 +01006847 /* If the new min/max/var_off satisfy the old ones and
6848 * everything else matches, we are OK.
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08006849 * 'id' is not compared, since it's only used for maps with
6850 * bpf_spin_lock inside map element and in such cases if
6851 * the rest of the prog is valid for one map element then
6852 * it's valid for all map elements regardless of the key
6853 * used in bpf_map_lookup()
Edward Cree1b688a12017-08-23 15:10:50 +01006854 */
6855 return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 &&
6856 range_within(rold, rcur) &&
6857 tnum_in(rold->var_off, rcur->var_off);
Edward Creef1174f72017-08-07 15:26:19 +01006858 case PTR_TO_MAP_VALUE_OR_NULL:
6859 /* a PTR_TO_MAP_VALUE could be safe to use as a
6860 * PTR_TO_MAP_VALUE_OR_NULL into the same map.
6861 * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL-
6862 * checked, doing so could have affected others with the same
6863 * id, and we can't check for that because we lost the id when
6864 * we converted to a PTR_TO_MAP_VALUE.
6865 */
6866 if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL)
6867 return false;
6868 if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)))
6869 return false;
6870 /* Check our ids match any regs they're supposed to */
6871 return check_ids(rold->id, rcur->id, idmap);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02006872 case PTR_TO_PACKET_META:
Edward Creef1174f72017-08-07 15:26:19 +01006873 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02006874 if (rcur->type != rold->type)
Edward Creef1174f72017-08-07 15:26:19 +01006875 return false;
6876 /* We must have at least as much range as the old ptr
6877 * did, so that any accesses which were safe before are
6878 * still safe. This is true even if old range < old off,
6879 * since someone could have accessed through (ptr - k), or
6880 * even done ptr -= k in a register, to get a safe access.
6881 */
6882 if (rold->range > rcur->range)
6883 return false;
6884 /* If the offsets don't match, we can't trust our alignment;
6885 * nor can we be sure that we won't fall out of range.
6886 */
6887 if (rold->off != rcur->off)
6888 return false;
6889 /* id relations must be preserved */
6890 if (rold->id && !check_ids(rold->id, rcur->id, idmap))
6891 return false;
6892 /* new val must satisfy old val knowledge */
6893 return range_within(rold, rcur) &&
6894 tnum_in(rold->var_off, rcur->var_off);
6895 case PTR_TO_CTX:
6896 case CONST_PTR_TO_MAP:
Edward Creef1174f72017-08-07 15:26:19 +01006897 case PTR_TO_PACKET_END:
Petar Penkovd58e4682018-09-14 07:46:18 -07006898 case PTR_TO_FLOW_KEYS:
Joe Stringerc64b7982018-10-02 13:35:33 -07006899 case PTR_TO_SOCKET:
6900 case PTR_TO_SOCKET_OR_NULL:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08006901 case PTR_TO_SOCK_COMMON:
6902 case PTR_TO_SOCK_COMMON_OR_NULL:
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08006903 case PTR_TO_TCP_SOCK:
6904 case PTR_TO_TCP_SOCK_OR_NULL:
Jonathan Lemonfada7fd2019-06-06 13:59:40 -07006905 case PTR_TO_XDP_SOCK:
Edward Creef1174f72017-08-07 15:26:19 +01006906 /* Only valid matches are exact, which memcmp() above
6907 * would have accepted
6908 */
6909 default:
6910 /* Don't know what's going on, just say it's not safe */
6911 return false;
6912 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07006913
Edward Creef1174f72017-08-07 15:26:19 +01006914 /* Shouldn't get here; if we do, say it's not safe */
6915 WARN_ON_ONCE(1);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07006916 return false;
6917}
6918
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006919static bool stacksafe(struct bpf_func_state *old,
6920 struct bpf_func_state *cur,
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006921 struct idpair *idmap)
6922{
6923 int i, spi;
6924
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006925 /* walk slots of the explored stack and ignore any additional
6926 * slots in the current stack, since explored(safe) state
6927 * didn't use them
6928 */
6929 for (i = 0; i < old->allocated_stack; i++) {
6930 spi = i / BPF_REG_SIZE;
6931
Alexei Starovoitovb2339202018-12-13 11:42:31 -08006932 if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) {
6933 i += BPF_REG_SIZE - 1;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08006934 /* explored state didn't use this */
Gianluca Borellofd05e572017-12-23 10:09:55 +00006935 continue;
Alexei Starovoitovb2339202018-12-13 11:42:31 -08006936 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08006937
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006938 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID)
6939 continue;
Alexei Starovoitov19e2dbb2018-12-13 11:42:33 -08006940
6941 /* explored stack has more populated slots than current stack
6942 * and these slots were used
6943 */
6944 if (i >= cur->allocated_stack)
6945 return false;
6946
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08006947 /* if old state was safe with misc data in the stack
6948 * it will be safe with zero-initialized stack.
6949 * The opposite is not true
6950 */
6951 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC &&
6952 cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO)
6953 continue;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006954 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] !=
6955 cur->stack[spi].slot_type[i % BPF_REG_SIZE])
6956 /* Ex: old explored (safe) state has STACK_SPILL in
6957 * this stack slot, but current has has STACK_MISC ->
6958 * this verifier states are not equivalent,
6959 * return false to continue verification of this path
6960 */
6961 return false;
6962 if (i % BPF_REG_SIZE)
6963 continue;
6964 if (old->stack[spi].slot_type[0] != STACK_SPILL)
6965 continue;
6966 if (!regsafe(&old->stack[spi].spilled_ptr,
6967 &cur->stack[spi].spilled_ptr,
6968 idmap))
6969 /* when explored and current stack slot are both storing
6970 * spilled registers, check that stored pointers types
6971 * are the same as well.
6972 * Ex: explored safe path could have stored
6973 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8}
6974 * but current path has stored:
6975 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16}
6976 * such verifier states are not equivalent.
6977 * return false to continue verification of this path
6978 */
6979 return false;
6980 }
6981 return true;
6982}
6983
Joe Stringerfd978bf72018-10-02 13:35:35 -07006984static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur)
6985{
6986 if (old->acquired_refs != cur->acquired_refs)
6987 return false;
6988 return !memcmp(old->refs, cur->refs,
6989 sizeof(*old->refs) * old->acquired_refs);
6990}
6991
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006992/* compare two verifier states
6993 *
6994 * all states stored in state_list are known to be valid, since
6995 * verifier reached 'bpf_exit' instruction through them
6996 *
6997 * this function is called when verifier exploring different branches of
6998 * execution popped from the state stack. If it sees an old state that has
6999 * more strict register state and more strict stack state then this execution
7000 * branch doesn't need to be explored further, since verifier already
7001 * concluded that more strict state leads to valid finish.
7002 *
7003 * Therefore two states are equivalent if register state is more conservative
7004 * and explored stack state is more conservative than the current one.
7005 * Example:
7006 * explored current
7007 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
7008 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
7009 *
7010 * In other words if current stack state (one being explored) has more
7011 * valid slots than old one that already passed validation, it means
7012 * the verifier can stop exploring and conclude that current state is valid too
7013 *
7014 * Similarly with registers. If explored state has register type as invalid
7015 * whereas register type in current state is meaningful, it means that
7016 * the current state will reach 'bpf_exit' instruction safely
7017 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007018static bool func_states_equal(struct bpf_func_state *old,
7019 struct bpf_func_state *cur)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007020{
Edward Creef1174f72017-08-07 15:26:19 +01007021 struct idpair *idmap;
7022 bool ret = false;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007023 int i;
7024
Edward Creef1174f72017-08-07 15:26:19 +01007025 idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL);
7026 /* If we failed to allocate the idmap, just say it's not safe */
7027 if (!idmap)
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07007028 return false;
Edward Creef1174f72017-08-07 15:26:19 +01007029
7030 for (i = 0; i < MAX_BPF_REG; i++) {
Edward Cree1b688a12017-08-23 15:10:50 +01007031 if (!regsafe(&old->regs[i], &cur->regs[i], idmap))
Edward Creef1174f72017-08-07 15:26:19 +01007032 goto out_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007033 }
7034
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07007035 if (!stacksafe(old, cur, idmap))
7036 goto out_free;
Joe Stringerfd978bf72018-10-02 13:35:35 -07007037
7038 if (!refsafe(old, cur))
7039 goto out_free;
Edward Creef1174f72017-08-07 15:26:19 +01007040 ret = true;
7041out_free:
7042 kfree(idmap);
7043 return ret;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007044}
7045
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007046static bool states_equal(struct bpf_verifier_env *env,
7047 struct bpf_verifier_state *old,
7048 struct bpf_verifier_state *cur)
Edward Creedc503a82017-08-15 20:34:35 +01007049{
Edward Creedc503a82017-08-15 20:34:35 +01007050 int i;
7051
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007052 if (old->curframe != cur->curframe)
7053 return false;
7054
Daniel Borkmann979d63d2019-01-03 00:58:34 +01007055 /* Verification state from speculative execution simulation
7056 * must never prune a non-speculative execution one.
7057 */
7058 if (old->speculative && !cur->speculative)
7059 return false;
7060
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08007061 if (old->active_spin_lock != cur->active_spin_lock)
7062 return false;
7063
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007064 /* for states to be equal callsites have to be the same
7065 * and all frame states need to be equivalent
7066 */
7067 for (i = 0; i <= old->curframe; i++) {
7068 if (old->frame[i]->callsite != cur->frame[i]->callsite)
7069 return false;
7070 if (!func_states_equal(old->frame[i], cur->frame[i]))
7071 return false;
7072 }
7073 return true;
7074}
7075
Jiong Wang5327ed32019-05-24 23:25:12 +01007076/* Return 0 if no propagation happened. Return negative error code if error
7077 * happened. Otherwise, return the propagated bit.
7078 */
Jiong Wang55e7f3b2019-04-12 22:59:36 +01007079static int propagate_liveness_reg(struct bpf_verifier_env *env,
7080 struct bpf_reg_state *reg,
7081 struct bpf_reg_state *parent_reg)
7082{
Jiong Wang5327ed32019-05-24 23:25:12 +01007083 u8 parent_flag = parent_reg->live & REG_LIVE_READ;
7084 u8 flag = reg->live & REG_LIVE_READ;
Jiong Wang55e7f3b2019-04-12 22:59:36 +01007085 int err;
7086
Jiong Wang5327ed32019-05-24 23:25:12 +01007087 /* When comes here, read flags of PARENT_REG or REG could be any of
7088 * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need
7089 * of propagation if PARENT_REG has strongest REG_LIVE_READ64.
7090 */
7091 if (parent_flag == REG_LIVE_READ64 ||
7092 /* Or if there is no read flag from REG. */
7093 !flag ||
7094 /* Or if the read flag from REG is the same as PARENT_REG. */
7095 parent_flag == flag)
Jiong Wang55e7f3b2019-04-12 22:59:36 +01007096 return 0;
7097
Jiong Wang5327ed32019-05-24 23:25:12 +01007098 err = mark_reg_read(env, reg, parent_reg, flag);
Jiong Wang55e7f3b2019-04-12 22:59:36 +01007099 if (err)
7100 return err;
7101
Jiong Wang5327ed32019-05-24 23:25:12 +01007102 return flag;
Jiong Wang55e7f3b2019-04-12 22:59:36 +01007103}
7104
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007105/* A write screens off any subsequent reads; but write marks come from the
7106 * straight-line code between a state and its parent. When we arrive at an
7107 * equivalent state (jump target or such) we didn't arrive by the straight-line
7108 * code, so read marks in the state must propagate to the parent regardless
7109 * of the state's write marks. That's what 'parent == state->parent' comparison
Edward Cree679c7822018-08-22 20:02:19 +01007110 * in mark_reg_read() is for.
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007111 */
7112static int propagate_liveness(struct bpf_verifier_env *env,
7113 const struct bpf_verifier_state *vstate,
7114 struct bpf_verifier_state *vparent)
7115{
Jiong Wang3f8cafa2019-04-12 22:59:35 +01007116 struct bpf_reg_state *state_reg, *parent_reg;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007117 struct bpf_func_state *state, *parent;
Jiong Wang3f8cafa2019-04-12 22:59:35 +01007118 int i, frame, err = 0;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007119
7120 if (vparent->curframe != vstate->curframe) {
7121 WARN(1, "propagate_live: parent frame %d current frame %d\n",
7122 vparent->curframe, vstate->curframe);
7123 return -EFAULT;
7124 }
Edward Creedc503a82017-08-15 20:34:35 +01007125 /* Propagate read liveness of registers... */
7126 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
Jakub Kicinski83d16312019-03-21 14:34:36 -07007127 for (frame = 0; frame <= vstate->curframe; frame++) {
Jiong Wang3f8cafa2019-04-12 22:59:35 +01007128 parent = vparent->frame[frame];
7129 state = vstate->frame[frame];
7130 parent_reg = parent->regs;
7131 state_reg = state->regs;
Jakub Kicinski83d16312019-03-21 14:34:36 -07007132 /* We don't need to worry about FP liveness, it's read-only */
7133 for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) {
Jiong Wang55e7f3b2019-04-12 22:59:36 +01007134 err = propagate_liveness_reg(env, &state_reg[i],
7135 &parent_reg[i]);
Jiong Wang5327ed32019-05-24 23:25:12 +01007136 if (err < 0)
Jiong Wang3f8cafa2019-04-12 22:59:35 +01007137 return err;
Jiong Wang5327ed32019-05-24 23:25:12 +01007138 if (err == REG_LIVE_READ64)
7139 mark_insn_zext(env, &parent_reg[i]);
Edward Creedc503a82017-08-15 20:34:35 +01007140 }
Edward Creedc503a82017-08-15 20:34:35 +01007141
Jiong Wang1b04aee2019-04-12 22:59:34 +01007142 /* Propagate stack slots. */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007143 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE &&
7144 i < parent->allocated_stack / BPF_REG_SIZE; i++) {
Jiong Wang3f8cafa2019-04-12 22:59:35 +01007145 parent_reg = &parent->stack[i].spilled_ptr;
7146 state_reg = &state->stack[i].spilled_ptr;
Jiong Wang55e7f3b2019-04-12 22:59:36 +01007147 err = propagate_liveness_reg(env, state_reg,
7148 parent_reg);
Jiong Wang5327ed32019-05-24 23:25:12 +01007149 if (err < 0)
Jiong Wang3f8cafa2019-04-12 22:59:35 +01007150 return err;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007151 }
Edward Creedc503a82017-08-15 20:34:35 +01007152 }
Jiong Wang5327ed32019-05-24 23:25:12 +01007153 return 0;
Edward Creedc503a82017-08-15 20:34:35 +01007154}
7155
Alexei Starovoitova3ce6852019-06-28 09:24:09 -07007156/* find precise scalars in the previous equivalent state and
7157 * propagate them into the current state
7158 */
7159static int propagate_precision(struct bpf_verifier_env *env,
7160 const struct bpf_verifier_state *old)
7161{
7162 struct bpf_reg_state *state_reg;
7163 struct bpf_func_state *state;
7164 int i, err = 0;
7165
7166 state = old->frame[old->curframe];
7167 state_reg = state->regs;
7168 for (i = 0; i < BPF_REG_FP; i++, state_reg++) {
7169 if (state_reg->type != SCALAR_VALUE ||
7170 !state_reg->precise)
7171 continue;
7172 if (env->log.level & BPF_LOG_LEVEL2)
7173 verbose(env, "propagating r%d\n", i);
7174 err = mark_chain_precision(env, i);
7175 if (err < 0)
7176 return err;
7177 }
7178
7179 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
7180 if (state->stack[i].slot_type[0] != STACK_SPILL)
7181 continue;
7182 state_reg = &state->stack[i].spilled_ptr;
7183 if (state_reg->type != SCALAR_VALUE ||
7184 !state_reg->precise)
7185 continue;
7186 if (env->log.level & BPF_LOG_LEVEL2)
7187 verbose(env, "propagating fp%d\n",
7188 (-i - 1) * BPF_REG_SIZE);
7189 err = mark_chain_precision_stack(env, i);
7190 if (err < 0)
7191 return err;
7192 }
7193 return 0;
7194}
7195
Alexei Starovoitov25897262019-06-15 12:12:20 -07007196static bool states_maybe_looping(struct bpf_verifier_state *old,
7197 struct bpf_verifier_state *cur)
7198{
7199 struct bpf_func_state *fold, *fcur;
7200 int i, fr = cur->curframe;
7201
7202 if (old->curframe != fr)
7203 return false;
7204
7205 fold = old->frame[fr];
7206 fcur = cur->frame[fr];
7207 for (i = 0; i < MAX_BPF_REG; i++)
7208 if (memcmp(&fold->regs[i], &fcur->regs[i],
7209 offsetof(struct bpf_reg_state, parent)))
7210 return false;
7211 return true;
7212}
7213
7214
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007215static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007216{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007217 struct bpf_verifier_state_list *new_sl;
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -07007218 struct bpf_verifier_state_list *sl, **pprev;
Edward Cree679c7822018-08-22 20:02:19 +01007219 struct bpf_verifier_state *cur = env->cur_state, *new;
Alexei Starovoitovceefbc92018-12-03 22:46:06 -08007220 int i, j, err, states_cnt = 0;
Alexei Starovoitov25897262019-06-15 12:12:20 -07007221 bool add_new_state = false;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007222
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07007223 cur->last_insn_idx = env->prev_insn_idx;
Alexei Starovoitova8f500a2019-05-21 20:17:06 -07007224 if (!env->insn_aux_data[insn_idx].prune_point)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007225 /* this 'insn_idx' instruction wasn't marked, so we will not
7226 * be doing state search here
7227 */
7228 return 0;
7229
Alexei Starovoitov25897262019-06-15 12:12:20 -07007230 /* bpf progs typically have pruning point every 4 instructions
7231 * http://vger.kernel.org/bpfconf2019.html#session-1
7232 * Do not add new state for future pruning if the verifier hasn't seen
7233 * at least 2 jumps and at least 8 instructions.
7234 * This heuristics helps decrease 'total_states' and 'peak_states' metric.
7235 * In tests that amounts to up to 50% reduction into total verifier
7236 * memory consumption and 20% verifier time speedup.
7237 */
7238 if (env->jmps_processed - env->prev_jmps_processed >= 2 &&
7239 env->insn_processed - env->prev_insn_processed >= 8)
7240 add_new_state = true;
7241
Alexei Starovoitova8f500a2019-05-21 20:17:06 -07007242 pprev = explored_state(env, insn_idx);
7243 sl = *pprev;
7244
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -08007245 clean_live_states(env, insn_idx, cur);
7246
Alexei Starovoitova8f500a2019-05-21 20:17:06 -07007247 while (sl) {
Alexei Starovoitovdc2a4eb2019-05-21 20:17:07 -07007248 states_cnt++;
7249 if (sl->state.insn_idx != insn_idx)
7250 goto next;
Alexei Starovoitov25897262019-06-15 12:12:20 -07007251 if (sl->state.branches) {
7252 if (states_maybe_looping(&sl->state, cur) &&
7253 states_equal(env, &sl->state, cur)) {
7254 verbose_linfo(env, insn_idx, "; ");
7255 verbose(env, "infinite loop detected at insn %d\n", insn_idx);
7256 return -EINVAL;
7257 }
7258 /* if the verifier is processing a loop, avoid adding new state
7259 * too often, since different loop iterations have distinct
7260 * states and may not help future pruning.
7261 * This threshold shouldn't be too low to make sure that
7262 * a loop with large bound will be rejected quickly.
7263 * The most abusive loop will be:
7264 * r1 += 1
7265 * if r1 < 1000000 goto pc-2
7266 * 1M insn_procssed limit / 100 == 10k peak states.
7267 * This threshold shouldn't be too high either, since states
7268 * at the end of the loop are likely to be useful in pruning.
7269 */
7270 if (env->jmps_processed - env->prev_jmps_processed < 20 &&
7271 env->insn_processed - env->prev_insn_processed < 100)
7272 add_new_state = false;
7273 goto miss;
7274 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07007275 if (states_equal(env, &sl->state, cur)) {
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -07007276 sl->hit_cnt++;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007277 /* reached equivalent register/stack state,
Edward Creedc503a82017-08-15 20:34:35 +01007278 * prune the search.
7279 * Registers read by the continuation are read by us.
Edward Cree8e9cd9c2017-08-23 15:11:21 +01007280 * If we have any write marks in env->cur_state, they
7281 * will prevent corresponding reads in the continuation
7282 * from reaching our parent (an explored_state). Our
7283 * own state will get the read marks recorded, but
7284 * they'll be immediately forgotten as we're pruning
7285 * this state and will pop a new one.
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007286 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007287 err = propagate_liveness(env, &sl->state, cur);
Alexei Starovoitova3ce6852019-06-28 09:24:09 -07007288
7289 /* if previous state reached the exit with precision and
7290 * current state is equivalent to it (except precsion marks)
7291 * the precision needs to be propagated back in
7292 * the current state.
7293 */
7294 err = err ? : push_jmp_history(env, cur);
7295 err = err ? : propagate_precision(env, &sl->state);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007296 if (err)
7297 return err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007298 return 1;
Edward Creedc503a82017-08-15 20:34:35 +01007299 }
Alexei Starovoitov25897262019-06-15 12:12:20 -07007300miss:
7301 /* when new state is not going to be added do not increase miss count.
7302 * Otherwise several loop iterations will remove the state
7303 * recorded earlier. The goal of these heuristics is to have
7304 * states from some iterations of the loop (some in the beginning
7305 * and some at the end) to help pruning.
7306 */
7307 if (add_new_state)
7308 sl->miss_cnt++;
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -07007309 /* heuristic to determine whether this state is beneficial
7310 * to keep checking from state equivalence point of view.
7311 * Higher numbers increase max_states_per_insn and verification time,
7312 * but do not meaningfully decrease insn_processed.
7313 */
7314 if (sl->miss_cnt > sl->hit_cnt * 3 + 3) {
7315 /* the state is unlikely to be useful. Remove it to
7316 * speed up verification
7317 */
7318 *pprev = sl->next;
7319 if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) {
Alexei Starovoitov25897262019-06-15 12:12:20 -07007320 u32 br = sl->state.branches;
7321
7322 WARN_ONCE(br,
7323 "BUG live_done but branches_to_explore %d\n",
7324 br);
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -07007325 free_verifier_state(&sl->state, false);
7326 kfree(sl);
7327 env->peak_states--;
7328 } else {
7329 /* cannot free this state, since parentage chain may
7330 * walk it later. Add it for free_list instead to
7331 * be freed at the end of verification
7332 */
7333 sl->next = env->free_list;
7334 env->free_list = sl;
7335 }
7336 sl = *pprev;
7337 continue;
7338 }
Alexei Starovoitovdc2a4eb2019-05-21 20:17:07 -07007339next:
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -07007340 pprev = &sl->next;
7341 sl = *pprev;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007342 }
7343
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07007344 if (env->max_states_per_insn < states_cnt)
7345 env->max_states_per_insn = states_cnt;
7346
Alexei Starovoitovceefbc92018-12-03 22:46:06 -08007347 if (!env->allow_ptr_leaks && states_cnt > BPF_COMPLEXITY_LIMIT_STATES)
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07007348 return push_jmp_history(env, cur);
Alexei Starovoitovceefbc92018-12-03 22:46:06 -08007349
Alexei Starovoitov25897262019-06-15 12:12:20 -07007350 if (!add_new_state)
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07007351 return push_jmp_history(env, cur);
Alexei Starovoitov25897262019-06-15 12:12:20 -07007352
7353 /* There were no equivalent states, remember the current one.
7354 * Technically the current state is not proven to be safe yet,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007355 * but it will either reach outer most bpf_exit (which means it's safe)
Alexei Starovoitov25897262019-06-15 12:12:20 -07007356 * or it will be rejected. When there are no loops the verifier won't be
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007357 * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx)
Alexei Starovoitov25897262019-06-15 12:12:20 -07007358 * again on the way to bpf_exit.
7359 * When looping the sl->state.branches will be > 0 and this state
7360 * will not be considered for equivalence until branches == 0.
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007361 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07007362 new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007363 if (!new_sl)
7364 return -ENOMEM;
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07007365 env->total_states++;
7366 env->peak_states++;
Alexei Starovoitov25897262019-06-15 12:12:20 -07007367 env->prev_jmps_processed = env->jmps_processed;
7368 env->prev_insn_processed = env->insn_processed;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007369
7370 /* add new state to the head of linked list */
Edward Cree679c7822018-08-22 20:02:19 +01007371 new = &new_sl->state;
7372 err = copy_verifier_state(new, cur);
Alexei Starovoitov1969db42017-11-01 00:08:04 -07007373 if (err) {
Edward Cree679c7822018-08-22 20:02:19 +01007374 free_verifier_state(new, false);
Alexei Starovoitov1969db42017-11-01 00:08:04 -07007375 kfree(new_sl);
7376 return err;
7377 }
Alexei Starovoitovdc2a4eb2019-05-21 20:17:07 -07007378 new->insn_idx = insn_idx;
Alexei Starovoitov25897262019-06-15 12:12:20 -07007379 WARN_ONCE(new->branches != 1,
7380 "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx);
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07007381
Alexei Starovoitov25897262019-06-15 12:12:20 -07007382 cur->parent = new;
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07007383 cur->first_insn_idx = insn_idx;
7384 clear_jmp_history(cur);
Alexei Starovoitov5d839022019-05-21 20:17:05 -07007385 new_sl->next = *explored_state(env, insn_idx);
7386 *explored_state(env, insn_idx) = new_sl;
Jakub Kicinski7640ead2018-12-12 16:29:07 -08007387 /* connect new state to parentage chain. Current frame needs all
7388 * registers connected. Only r6 - r9 of the callers are alive (pushed
7389 * to the stack implicitly by JITs) so in callers' frames connect just
7390 * r6 - r9 as an optimization. Callers will have r1 - r5 connected to
7391 * the state of the call instruction (with WRITTEN set), and r0 comes
7392 * from callee with its full parentage chain, anyway.
7393 */
Edward Cree8e9cd9c2017-08-23 15:11:21 +01007394 /* clear write marks in current state: the writes we did are not writes
7395 * our child did, so they don't screen off its reads from us.
7396 * (There are no read marks in current state, because reads always mark
7397 * their parent and current state never has children yet. Only
7398 * explored_states can get read marks.)
7399 */
Alexei Starovoitoveea1c222019-06-15 12:12:21 -07007400 for (j = 0; j <= cur->curframe; j++) {
7401 for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++)
7402 cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i];
7403 for (i = 0; i < BPF_REG_FP; i++)
7404 cur->frame[j]->regs[i].live = REG_LIVE_NONE;
7405 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007406
7407 /* all stack frames are accessible from callee, clear them all */
7408 for (j = 0; j <= cur->curframe; j++) {
7409 struct bpf_func_state *frame = cur->frame[j];
Edward Cree679c7822018-08-22 20:02:19 +01007410 struct bpf_func_state *newframe = new->frame[j];
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007411
Edward Cree679c7822018-08-22 20:02:19 +01007412 for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08007413 frame->stack[i].spilled_ptr.live = REG_LIVE_NONE;
Edward Cree679c7822018-08-22 20:02:19 +01007414 frame->stack[i].spilled_ptr.parent =
7415 &newframe->stack[i].spilled_ptr;
7416 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007417 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007418 return 0;
7419}
7420
Joe Stringerc64b7982018-10-02 13:35:33 -07007421/* Return true if it's OK to have the same insn return a different type. */
7422static bool reg_type_mismatch_ok(enum bpf_reg_type type)
7423{
7424 switch (type) {
7425 case PTR_TO_CTX:
7426 case PTR_TO_SOCKET:
7427 case PTR_TO_SOCKET_OR_NULL:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08007428 case PTR_TO_SOCK_COMMON:
7429 case PTR_TO_SOCK_COMMON_OR_NULL:
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08007430 case PTR_TO_TCP_SOCK:
7431 case PTR_TO_TCP_SOCK_OR_NULL:
Jonathan Lemonfada7fd2019-06-06 13:59:40 -07007432 case PTR_TO_XDP_SOCK:
Joe Stringerc64b7982018-10-02 13:35:33 -07007433 return false;
7434 default:
7435 return true;
7436 }
7437}
7438
7439/* If an instruction was previously used with particular pointer types, then we
7440 * need to be careful to avoid cases such as the below, where it may be ok
7441 * for one branch accessing the pointer, but not ok for the other branch:
7442 *
7443 * R1 = sock_ptr
7444 * goto X;
7445 * ...
7446 * R1 = some_other_valid_ptr;
7447 * goto X;
7448 * ...
7449 * R2 = *(u32 *)(R1 + 0);
7450 */
7451static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev)
7452{
7453 return src != prev && (!reg_type_mismatch_ok(src) ||
7454 !reg_type_mismatch_ok(prev));
7455}
7456
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007457static int do_check(struct bpf_verifier_env *env)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007458{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07007459 struct bpf_verifier_state *state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007460 struct bpf_insn *insns = env->prog->insnsi;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07007461 struct bpf_reg_state *regs;
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07007462 int insn_cnt = env->prog->len;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007463 bool do_print_state = false;
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07007464 int prev_insn_idx = -1;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007465
Martin KaFai Laud9762e82018-12-13 10:41:48 -08007466 env->prev_linfo = NULL;
7467
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07007468 state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL);
7469 if (!state)
7470 return -ENOMEM;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007471 state->curframe = 0;
Daniel Borkmann979d63d2019-01-03 00:58:34 +01007472 state->speculative = false;
Alexei Starovoitov25897262019-06-15 12:12:20 -07007473 state->branches = 1;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007474 state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL);
7475 if (!state->frame[0]) {
7476 kfree(state);
7477 return -ENOMEM;
7478 }
7479 env->cur_state = state;
7480 init_func_state(env, state->frame[0],
7481 BPF_MAIN_FUNC /* callsite */,
7482 0 /* frameno */,
7483 0 /* subprogno, zero == main subprog */);
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007484
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007485 for (;;) {
7486 struct bpf_insn *insn;
7487 u8 class;
7488 int err;
7489
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07007490 env->prev_insn_idx = prev_insn_idx;
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007491 if (env->insn_idx >= insn_cnt) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007492 verbose(env, "invalid insn idx %d insn_cnt %d\n",
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007493 env->insn_idx, insn_cnt);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007494 return -EFAULT;
7495 }
7496
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007497 insn = &insns[env->insn_idx];
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007498 class = BPF_CLASS(insn->code);
7499
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07007500 if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007501 verbose(env,
7502 "BPF program is too large. Processed %d insn\n",
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07007503 env->insn_processed);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007504 return -E2BIG;
7505 }
7506
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007507 err = is_state_visited(env, env->insn_idx);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007508 if (err < 0)
7509 return err;
7510 if (err == 1) {
7511 /* found equivalent state, can prune the search */
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07007512 if (env->log.level & BPF_LOG_LEVEL) {
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007513 if (do_print_state)
Daniel Borkmann979d63d2019-01-03 00:58:34 +01007514 verbose(env, "\nfrom %d to %d%s: safe\n",
7515 env->prev_insn_idx, env->insn_idx,
7516 env->cur_state->speculative ?
7517 " (speculative execution)" : "");
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007518 else
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007519 verbose(env, "%d: safe\n", env->insn_idx);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007520 }
7521 goto process_bpf_exit;
7522 }
7523
Alexei Starovoitovc3494802018-12-03 22:46:04 -08007524 if (signal_pending(current))
7525 return -EAGAIN;
7526
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02007527 if (need_resched())
7528 cond_resched();
7529
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07007530 if (env->log.level & BPF_LOG_LEVEL2 ||
7531 (env->log.level & BPF_LOG_LEVEL && do_print_state)) {
7532 if (env->log.level & BPF_LOG_LEVEL2)
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007533 verbose(env, "%d:", env->insn_idx);
David S. Millerc5fc9692017-05-10 11:25:17 -07007534 else
Daniel Borkmann979d63d2019-01-03 00:58:34 +01007535 verbose(env, "\nfrom %d to %d%s:",
7536 env->prev_insn_idx, env->insn_idx,
7537 env->cur_state->speculative ?
7538 " (speculative execution)" : "");
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007539 print_verifier_state(env, state->frame[state->curframe]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007540 do_print_state = false;
7541 }
7542
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07007543 if (env->log.level & BPF_LOG_LEVEL) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01007544 const struct bpf_insn_cbs cbs = {
7545 .cb_print = verbose,
Jiri Olsaabe08842018-03-23 11:41:28 +01007546 .private_data = env,
Daniel Borkmann7105e822017-12-20 13:42:57 +01007547 };
7548
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007549 verbose_linfo(env, env->insn_idx, "; ");
7550 verbose(env, "%d: ", env->insn_idx);
Jiri Olsaabe08842018-03-23 11:41:28 +01007551 print_bpf_insn(&cbs, insn, env->allow_ptr_leaks);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007552 }
7553
Jakub Kicinskicae19272017-12-27 18:39:05 -08007554 if (bpf_prog_is_dev_bound(env->prog->aux)) {
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007555 err = bpf_prog_offload_verify_insn(env, env->insn_idx,
7556 env->prev_insn_idx);
Jakub Kicinskicae19272017-12-27 18:39:05 -08007557 if (err)
7558 return err;
7559 }
Jakub Kicinski13a27df2016-09-21 11:43:58 +01007560
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07007561 regs = cur_regs(env);
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007562 env->insn_aux_data[env->insn_idx].seen = true;
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07007563 prev_insn_idx = env->insn_idx;
Joe Stringerfd978bf72018-10-02 13:35:35 -07007564
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007565 if (class == BPF_ALU || class == BPF_ALU64) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07007566 err = check_alu_op(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007567 if (err)
7568 return err;
7569
7570 } else if (class == BPF_LDX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007571 enum bpf_reg_type *prev_src_type, src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007572
7573 /* check for reserved fields is already done */
7574
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007575 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01007576 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007577 if (err)
7578 return err;
7579
Edward Creedc503a82017-08-15 20:34:35 +01007580 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007581 if (err)
7582 return err;
7583
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07007584 src_reg_type = regs[insn->src_reg].type;
7585
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007586 /* check that memory (src_reg + off) is readable,
7587 * the state of dst_reg will be updated by this func
7588 */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007589 err = check_mem_access(env, env->insn_idx, insn->src_reg,
7590 insn->off, BPF_SIZE(insn->code),
7591 BPF_READ, insn->dst_reg, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007592 if (err)
7593 return err;
7594
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007595 prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007596
7597 if (*prev_src_type == NOT_INIT) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007598 /* saw a valid insn
7599 * dst_reg = *(u32 *)(src_reg + off)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007600 * save type to validate intersecting paths
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007601 */
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007602 *prev_src_type = src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007603
Joe Stringerc64b7982018-10-02 13:35:33 -07007604 } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007605 /* ABuser program is trying to use the same insn
7606 * dst_reg = *(u32*) (src_reg + off)
7607 * with different pointer types:
7608 * src_reg == ctx in one branch and
7609 * src_reg == stack|map in some other branch.
7610 * Reject it.
7611 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007612 verbose(env, "same insn cannot be used with different pointers\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007613 return -EINVAL;
7614 }
7615
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007616 } else if (class == BPF_STX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007617 enum bpf_reg_type *prev_dst_type, dst_reg_type;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007618
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007619 if (BPF_MODE(insn->code) == BPF_XADD) {
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007620 err = check_xadd(env, env->insn_idx, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007621 if (err)
7622 return err;
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007623 env->insn_idx++;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007624 continue;
7625 }
7626
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007627 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01007628 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007629 if (err)
7630 return err;
7631 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01007632 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007633 if (err)
7634 return err;
7635
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007636 dst_reg_type = regs[insn->dst_reg].type;
7637
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007638 /* check that memory (dst_reg + off) is writeable */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007639 err = check_mem_access(env, env->insn_idx, insn->dst_reg,
7640 insn->off, BPF_SIZE(insn->code),
7641 BPF_WRITE, insn->src_reg, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007642 if (err)
7643 return err;
7644
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007645 prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007646
7647 if (*prev_dst_type == NOT_INIT) {
7648 *prev_dst_type = dst_reg_type;
Joe Stringerc64b7982018-10-02 13:35:33 -07007649 } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007650 verbose(env, "same insn cannot be used with different pointers\n");
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007651 return -EINVAL;
7652 }
7653
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007654 } else if (class == BPF_ST) {
7655 if (BPF_MODE(insn->code) != BPF_MEM ||
7656 insn->src_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007657 verbose(env, "BPF_ST uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007658 return -EINVAL;
7659 }
7660 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01007661 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007662 if (err)
7663 return err;
7664
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01007665 if (is_ctx_reg(env, insn->dst_reg)) {
Joe Stringer9d2be442018-10-02 13:35:31 -07007666 verbose(env, "BPF_ST stores into R%d %s is not allowed\n",
Daniel Borkmann2a159c62018-10-21 02:09:24 +02007667 insn->dst_reg,
7668 reg_type_str[reg_state(env, insn->dst_reg)->type]);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01007669 return -EACCES;
7670 }
7671
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007672 /* check that memory (dst_reg + off) is writeable */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007673 err = check_mem_access(env, env->insn_idx, insn->dst_reg,
7674 insn->off, BPF_SIZE(insn->code),
7675 BPF_WRITE, -1, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007676 if (err)
7677 return err;
7678
Jiong Wang092ed092019-01-26 12:26:01 -05007679 } else if (class == BPF_JMP || class == BPF_JMP32) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007680 u8 opcode = BPF_OP(insn->code);
7681
Alexei Starovoitov25897262019-06-15 12:12:20 -07007682 env->jmps_processed++;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007683 if (opcode == BPF_CALL) {
7684 if (BPF_SRC(insn->code) != BPF_K ||
7685 insn->off != 0 ||
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007686 (insn->src_reg != BPF_REG_0 &&
7687 insn->src_reg != BPF_PSEUDO_CALL) ||
Jiong Wang092ed092019-01-26 12:26:01 -05007688 insn->dst_reg != BPF_REG_0 ||
7689 class == BPF_JMP32) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007690 verbose(env, "BPF_CALL uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007691 return -EINVAL;
7692 }
7693
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08007694 if (env->cur_state->active_spin_lock &&
7695 (insn->src_reg == BPF_PSEUDO_CALL ||
7696 insn->imm != BPF_FUNC_spin_unlock)) {
7697 verbose(env, "function calls are not allowed while holding a lock\n");
7698 return -EINVAL;
7699 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007700 if (insn->src_reg == BPF_PSEUDO_CALL)
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007701 err = check_func_call(env, insn, &env->insn_idx);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007702 else
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007703 err = check_helper_call(env, insn->imm, env->insn_idx);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007704 if (err)
7705 return err;
7706
7707 } else if (opcode == BPF_JA) {
7708 if (BPF_SRC(insn->code) != BPF_K ||
7709 insn->imm != 0 ||
7710 insn->src_reg != BPF_REG_0 ||
Jiong Wang092ed092019-01-26 12:26:01 -05007711 insn->dst_reg != BPF_REG_0 ||
7712 class == BPF_JMP32) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007713 verbose(env, "BPF_JA uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007714 return -EINVAL;
7715 }
7716
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007717 env->insn_idx += insn->off + 1;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007718 continue;
7719
7720 } else if (opcode == BPF_EXIT) {
7721 if (BPF_SRC(insn->code) != BPF_K ||
7722 insn->imm != 0 ||
7723 insn->src_reg != BPF_REG_0 ||
Jiong Wang092ed092019-01-26 12:26:01 -05007724 insn->dst_reg != BPF_REG_0 ||
7725 class == BPF_JMP32) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007726 verbose(env, "BPF_EXIT uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007727 return -EINVAL;
7728 }
7729
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08007730 if (env->cur_state->active_spin_lock) {
7731 verbose(env, "bpf_spin_unlock is missing\n");
7732 return -EINVAL;
7733 }
7734
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007735 if (state->curframe) {
7736 /* exit from nested function */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007737 err = prepare_func_exit(env, &env->insn_idx);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007738 if (err)
7739 return err;
7740 do_print_state = true;
7741 continue;
7742 }
7743
Joe Stringerfd978bf72018-10-02 13:35:35 -07007744 err = check_reference_leak(env);
7745 if (err)
7746 return err;
7747
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007748 /* eBPF calling convetion is such that R0 is used
7749 * to return the value from eBPF program.
7750 * Make sure that it's readable at this time
7751 * of bpf_exit, which means that program wrote
7752 * something into it earlier
7753 */
Edward Creedc503a82017-08-15 20:34:35 +01007754 err = check_reg_arg(env, BPF_REG_0, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007755 if (err)
7756 return err;
7757
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07007758 if (is_pointer_value(env, BPF_REG_0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007759 verbose(env, "R0 leaks addr as return value\n");
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07007760 return -EACCES;
7761 }
7762
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07007763 err = check_return_code(env);
7764 if (err)
7765 return err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007766process_bpf_exit:
Alexei Starovoitov25897262019-06-15 12:12:20 -07007767 update_branch_counts(env, env->cur_state);
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -07007768 err = pop_stack(env, &prev_insn_idx,
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007769 &env->insn_idx);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07007770 if (err < 0) {
7771 if (err != -ENOENT)
7772 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007773 break;
7774 } else {
7775 do_print_state = true;
7776 continue;
7777 }
7778 } else {
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007779 err = check_cond_jmp_op(env, insn, &env->insn_idx);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007780 if (err)
7781 return err;
7782 }
7783 } else if (class == BPF_LD) {
7784 u8 mode = BPF_MODE(insn->code);
7785
7786 if (mode == BPF_ABS || mode == BPF_IND) {
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08007787 err = check_ld_abs(env, insn);
7788 if (err)
7789 return err;
7790
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007791 } else if (mode == BPF_IMM) {
7792 err = check_ld_imm(env, insn);
7793 if (err)
7794 return err;
7795
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007796 env->insn_idx++;
7797 env->insn_aux_data[env->insn_idx].seen = true;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007798 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007799 verbose(env, "invalid BPF_LD mode\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007800 return -EINVAL;
7801 }
7802 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007803 verbose(env, "unknown insn class %d\n", class);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007804 return -EINVAL;
7805 }
7806
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007807 env->insn_idx++;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007808 }
7809
Jiong Wang9c8105b2018-05-02 16:17:18 -04007810 env->prog->aux->stack_depth = env->subprog_info[0].stack_depth;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007811 return 0;
7812}
7813
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07007814static int check_map_prealloc(struct bpf_map *map)
7815{
7816 return (map->map_type != BPF_MAP_TYPE_HASH &&
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07007817 map->map_type != BPF_MAP_TYPE_PERCPU_HASH &&
7818 map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) ||
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07007819 !(map->map_flags & BPF_F_NO_PREALLOC);
7820}
7821
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08007822static bool is_tracing_prog_type(enum bpf_prog_type type)
7823{
7824 switch (type) {
7825 case BPF_PROG_TYPE_KPROBE:
7826 case BPF_PROG_TYPE_TRACEPOINT:
7827 case BPF_PROG_TYPE_PERF_EVENT:
7828 case BPF_PROG_TYPE_RAW_TRACEPOINT:
7829 return true;
7830 default:
7831 return false;
7832 }
7833}
7834
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007835static int check_map_prog_compatibility(struct bpf_verifier_env *env,
7836 struct bpf_map *map,
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07007837 struct bpf_prog *prog)
7838
7839{
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07007840 /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use
7841 * preallocated hash maps, since doing memory allocation
7842 * in overflow_handler can crash depending on where nmi got
7843 * triggered.
7844 */
7845 if (prog->type == BPF_PROG_TYPE_PERF_EVENT) {
7846 if (!check_map_prealloc(map)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007847 verbose(env, "perf_event programs can only use preallocated hash map\n");
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07007848 return -EINVAL;
7849 }
7850 if (map->inner_map_meta &&
7851 !check_map_prealloc(map->inner_map_meta)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007852 verbose(env, "perf_event programs can only use preallocated inner hash map\n");
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07007853 return -EINVAL;
7854 }
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07007855 }
Jakub Kicinskia3884572018-01-11 20:29:09 -08007856
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08007857 if ((is_tracing_prog_type(prog->type) ||
7858 prog->type == BPF_PROG_TYPE_SOCKET_FILTER) &&
7859 map_value_has_spin_lock(map)) {
7860 verbose(env, "tracing progs cannot use bpf_spin_lock yet\n");
7861 return -EINVAL;
7862 }
7863
Jakub Kicinskia3884572018-01-11 20:29:09 -08007864 if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) &&
Jakub Kicinski09728262018-07-17 10:53:23 -07007865 !bpf_offload_prog_map_match(prog, map)) {
Jakub Kicinskia3884572018-01-11 20:29:09 -08007866 verbose(env, "offload device mismatch between prog and map\n");
7867 return -EINVAL;
7868 }
7869
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07007870 return 0;
7871}
7872
Roman Gushchinb741f162018-09-28 14:45:43 +00007873static bool bpf_map_is_cgroup_storage(struct bpf_map *map)
7874{
7875 return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE ||
7876 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE);
7877}
7878
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007879/* look for pseudo eBPF instructions that access map FDs and
7880 * replace them with actual map pointers
7881 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007882static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007883{
7884 struct bpf_insn *insn = env->prog->insnsi;
7885 int insn_cnt = env->prog->len;
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07007886 int i, j, err;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007887
Daniel Borkmannf1f77142017-01-13 23:38:15 +01007888 err = bpf_prog_calc_tag(env->prog);
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01007889 if (err)
7890 return err;
7891
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007892 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007893 if (BPF_CLASS(insn->code) == BPF_LDX &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007894 (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007895 verbose(env, "BPF_LDX uses reserved fields\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007896 return -EINVAL;
7897 }
7898
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007899 if (BPF_CLASS(insn->code) == BPF_STX &&
7900 ((BPF_MODE(insn->code) != BPF_MEM &&
7901 BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007902 verbose(env, "BPF_STX uses reserved fields\n");
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007903 return -EINVAL;
7904 }
7905
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007906 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02007907 struct bpf_insn_aux_data *aux;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007908 struct bpf_map *map;
7909 struct fd f;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02007910 u64 addr;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007911
7912 if (i == insn_cnt - 1 || insn[1].code != 0 ||
7913 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
7914 insn[1].off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007915 verbose(env, "invalid bpf_ld_imm64 insn\n");
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007916 return -EINVAL;
7917 }
7918
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02007919 if (insn[0].src_reg == 0)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007920 /* valid generic load 64-bit imm */
7921 goto next_insn;
7922
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02007923 /* In final convert_pseudo_ld_imm64() step, this is
7924 * converted into regular 64-bit imm load insn.
7925 */
7926 if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD &&
7927 insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) ||
7928 (insn[0].src_reg == BPF_PSEUDO_MAP_FD &&
7929 insn[1].imm != 0)) {
7930 verbose(env,
7931 "unrecognized bpf_ld_imm64 insn\n");
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007932 return -EINVAL;
7933 }
7934
Daniel Borkmann20182392019-03-04 21:08:53 +01007935 f = fdget(insn[0].imm);
Daniel Borkmannc2101292015-10-29 14:58:07 +01007936 map = __bpf_map_get(f);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007937 if (IS_ERR(map)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007938 verbose(env, "fd %d is not pointing to valid bpf_map\n",
Daniel Borkmann20182392019-03-04 21:08:53 +01007939 insn[0].imm);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007940 return PTR_ERR(map);
7941 }
7942
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007943 err = check_map_prog_compatibility(env, map, env->prog);
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07007944 if (err) {
7945 fdput(f);
7946 return err;
7947 }
7948
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02007949 aux = &env->insn_aux_data[i];
7950 if (insn->src_reg == BPF_PSEUDO_MAP_FD) {
7951 addr = (unsigned long)map;
7952 } else {
7953 u32 off = insn[1].imm;
7954
7955 if (off >= BPF_MAX_VAR_OFF) {
7956 verbose(env, "direct value offset of %u is not allowed\n", off);
7957 fdput(f);
7958 return -EINVAL;
7959 }
7960
7961 if (!map->ops->map_direct_value_addr) {
7962 verbose(env, "no direct value access support for this map type\n");
7963 fdput(f);
7964 return -EINVAL;
7965 }
7966
7967 err = map->ops->map_direct_value_addr(map, &addr, off);
7968 if (err) {
7969 verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n",
7970 map->value_size, off);
7971 fdput(f);
7972 return err;
7973 }
7974
7975 aux->map_off = off;
7976 addr += off;
7977 }
7978
7979 insn[0].imm = (u32)addr;
7980 insn[1].imm = addr >> 32;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007981
7982 /* check whether we recorded this map already */
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02007983 for (j = 0; j < env->used_map_cnt; j++) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007984 if (env->used_maps[j] == map) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02007985 aux->map_index = j;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007986 fdput(f);
7987 goto next_insn;
7988 }
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02007989 }
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007990
7991 if (env->used_map_cnt >= MAX_USED_MAPS) {
7992 fdput(f);
7993 return -E2BIG;
7994 }
7995
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007996 /* hold the map. If the program is rejected by verifier,
7997 * the map will be released by release_maps() or it
7998 * will be used by the valid program until it's unloaded
Jakub Kicinskiab7f5bf2018-05-03 18:37:17 -07007999 * and all maps are released in free_used_maps()
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008000 */
Alexei Starovoitov92117d82016-04-27 18:56:20 -07008001 map = bpf_map_inc(map, false);
8002 if (IS_ERR(map)) {
8003 fdput(f);
8004 return PTR_ERR(map);
8005 }
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02008006
8007 aux->map_index = env->used_map_cnt;
Alexei Starovoitov92117d82016-04-27 18:56:20 -07008008 env->used_maps[env->used_map_cnt++] = map;
8009
Roman Gushchinb741f162018-09-28 14:45:43 +00008010 if (bpf_map_is_cgroup_storage(map) &&
Roman Gushchinde9cbba2018-08-02 14:27:18 -07008011 bpf_cgroup_storage_assign(env->prog, map)) {
Roman Gushchinb741f162018-09-28 14:45:43 +00008012 verbose(env, "only one cgroup storage of each type is allowed\n");
Roman Gushchinde9cbba2018-08-02 14:27:18 -07008013 fdput(f);
8014 return -EBUSY;
8015 }
8016
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008017 fdput(f);
8018next_insn:
8019 insn++;
8020 i++;
Daniel Borkmann5e581da2018-01-26 23:33:38 +01008021 continue;
8022 }
8023
8024 /* Basic sanity check before we invest more work here. */
8025 if (!bpf_opcode_in_insntable(insn->code)) {
8026 verbose(env, "unknown opcode %02x\n", insn->code);
8027 return -EINVAL;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008028 }
8029 }
8030
8031 /* now all pseudo BPF_LD_IMM64 instructions load valid
8032 * 'struct bpf_map *' into a register instead of user map_fd.
8033 * These pointers will be used later by verifier to validate map access.
8034 */
8035 return 0;
8036}
8037
8038/* drop refcnt of maps used by the rejected program */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01008039static void release_maps(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008040{
Roman Gushchin8bad74f2018-09-28 14:45:36 +00008041 enum bpf_cgroup_storage_type stype;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008042 int i;
8043
Roman Gushchin8bad74f2018-09-28 14:45:36 +00008044 for_each_cgroup_storage_type(stype) {
8045 if (!env->prog->aux->cgroup_storage[stype])
8046 continue;
Roman Gushchinde9cbba2018-08-02 14:27:18 -07008047 bpf_cgroup_storage_release(env->prog,
Roman Gushchin8bad74f2018-09-28 14:45:36 +00008048 env->prog->aux->cgroup_storage[stype]);
8049 }
Roman Gushchinde9cbba2018-08-02 14:27:18 -07008050
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008051 for (i = 0; i < env->used_map_cnt; i++)
8052 bpf_map_put(env->used_maps[i]);
8053}
8054
8055/* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01008056static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008057{
8058 struct bpf_insn *insn = env->prog->insnsi;
8059 int insn_cnt = env->prog->len;
8060 int i;
8061
8062 for (i = 0; i < insn_cnt; i++, insn++)
8063 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
8064 insn->src_reg = 0;
8065}
8066
Alexei Starovoitov80419022017-03-15 18:26:41 -07008067/* single env->prog->insni[off] instruction was replaced with the range
8068 * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
8069 * [0, off) and [off, end) to new locations, so the patched range stays zero
8070 */
Jiong Wangb325fbc2019-05-24 23:25:13 +01008071static int adjust_insn_aux_data(struct bpf_verifier_env *env,
8072 struct bpf_prog *new_prog, u32 off, u32 cnt)
Alexei Starovoitov80419022017-03-15 18:26:41 -07008073{
8074 struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data;
Jiong Wangb325fbc2019-05-24 23:25:13 +01008075 struct bpf_insn *insn = new_prog->insnsi;
8076 u32 prog_len;
Alexei Starovoitovc1311872017-11-22 16:42:05 -08008077 int i;
Alexei Starovoitov80419022017-03-15 18:26:41 -07008078
Jiong Wangb325fbc2019-05-24 23:25:13 +01008079 /* aux info at OFF always needs adjustment, no matter fast path
8080 * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the
8081 * original insn at old prog.
8082 */
8083 old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1);
8084
Alexei Starovoitov80419022017-03-15 18:26:41 -07008085 if (cnt == 1)
8086 return 0;
Jiong Wangb325fbc2019-05-24 23:25:13 +01008087 prog_len = new_prog->len;
Kees Cookfad953c2018-06-12 14:27:37 -07008088 new_data = vzalloc(array_size(prog_len,
8089 sizeof(struct bpf_insn_aux_data)));
Alexei Starovoitov80419022017-03-15 18:26:41 -07008090 if (!new_data)
8091 return -ENOMEM;
8092 memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off);
8093 memcpy(new_data + off + cnt - 1, old_data + off,
8094 sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
Jiong Wangb325fbc2019-05-24 23:25:13 +01008095 for (i = off; i < off + cnt - 1; i++) {
Alexei Starovoitovc1311872017-11-22 16:42:05 -08008096 new_data[i].seen = true;
Jiong Wangb325fbc2019-05-24 23:25:13 +01008097 new_data[i].zext_dst = insn_has_def32(env, insn + i);
8098 }
Alexei Starovoitov80419022017-03-15 18:26:41 -07008099 env->insn_aux_data = new_data;
8100 vfree(old_data);
8101 return 0;
8102}
8103
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08008104static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len)
8105{
8106 int i;
8107
8108 if (len == 1)
8109 return;
Jiong Wang4cb3d992018-05-02 16:17:19 -04008110 /* NOTE: fake 'exit' subprog should be updated as well. */
8111 for (i = 0; i <= env->subprog_cnt; i++) {
Edward Creeafd59422018-11-16 12:00:07 +00008112 if (env->subprog_info[i].start <= off)
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08008113 continue;
Jiong Wang9c8105b2018-05-02 16:17:18 -04008114 env->subprog_info[i].start += len - 1;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08008115 }
8116}
8117
Alexei Starovoitov80419022017-03-15 18:26:41 -07008118static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
8119 const struct bpf_insn *patch, u32 len)
8120{
8121 struct bpf_prog *new_prog;
8122
8123 new_prog = bpf_patch_insn_single(env->prog, off, patch, len);
Alexei Starovoitov4f733792019-04-01 21:27:44 -07008124 if (IS_ERR(new_prog)) {
8125 if (PTR_ERR(new_prog) == -ERANGE)
8126 verbose(env,
8127 "insn %d cannot be patched due to 16-bit range\n",
8128 env->insn_aux_data[off].orig_idx);
Alexei Starovoitov80419022017-03-15 18:26:41 -07008129 return NULL;
Alexei Starovoitov4f733792019-04-01 21:27:44 -07008130 }
Jiong Wangb325fbc2019-05-24 23:25:13 +01008131 if (adjust_insn_aux_data(env, new_prog, off, len))
Alexei Starovoitov80419022017-03-15 18:26:41 -07008132 return NULL;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08008133 adjust_subprog_starts(env, off, len);
Alexei Starovoitov80419022017-03-15 18:26:41 -07008134 return new_prog;
8135}
8136
Jakub Kicinski52875a02019-01-22 22:45:20 -08008137static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env,
8138 u32 off, u32 cnt)
8139{
8140 int i, j;
8141
8142 /* find first prog starting at or after off (first to remove) */
8143 for (i = 0; i < env->subprog_cnt; i++)
8144 if (env->subprog_info[i].start >= off)
8145 break;
8146 /* find first prog starting at or after off + cnt (first to stay) */
8147 for (j = i; j < env->subprog_cnt; j++)
8148 if (env->subprog_info[j].start >= off + cnt)
8149 break;
8150 /* if j doesn't start exactly at off + cnt, we are just removing
8151 * the front of previous prog
8152 */
8153 if (env->subprog_info[j].start != off + cnt)
8154 j--;
8155
8156 if (j > i) {
8157 struct bpf_prog_aux *aux = env->prog->aux;
8158 int move;
8159
8160 /* move fake 'exit' subprog as well */
8161 move = env->subprog_cnt + 1 - j;
8162
8163 memmove(env->subprog_info + i,
8164 env->subprog_info + j,
8165 sizeof(*env->subprog_info) * move);
8166 env->subprog_cnt -= j - i;
8167
8168 /* remove func_info */
8169 if (aux->func_info) {
8170 move = aux->func_info_cnt - j;
8171
8172 memmove(aux->func_info + i,
8173 aux->func_info + j,
8174 sizeof(*aux->func_info) * move);
8175 aux->func_info_cnt -= j - i;
8176 /* func_info->insn_off is set after all code rewrites,
8177 * in adjust_btf_func() - no need to adjust
8178 */
8179 }
8180 } else {
8181 /* convert i from "first prog to remove" to "first to adjust" */
8182 if (env->subprog_info[i].start == off)
8183 i++;
8184 }
8185
8186 /* update fake 'exit' subprog as well */
8187 for (; i <= env->subprog_cnt; i++)
8188 env->subprog_info[i].start -= cnt;
8189
8190 return 0;
8191}
8192
8193static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off,
8194 u32 cnt)
8195{
8196 struct bpf_prog *prog = env->prog;
8197 u32 i, l_off, l_cnt, nr_linfo;
8198 struct bpf_line_info *linfo;
8199
8200 nr_linfo = prog->aux->nr_linfo;
8201 if (!nr_linfo)
8202 return 0;
8203
8204 linfo = prog->aux->linfo;
8205
8206 /* find first line info to remove, count lines to be removed */
8207 for (i = 0; i < nr_linfo; i++)
8208 if (linfo[i].insn_off >= off)
8209 break;
8210
8211 l_off = i;
8212 l_cnt = 0;
8213 for (; i < nr_linfo; i++)
8214 if (linfo[i].insn_off < off + cnt)
8215 l_cnt++;
8216 else
8217 break;
8218
8219 /* First live insn doesn't match first live linfo, it needs to "inherit"
8220 * last removed linfo. prog is already modified, so prog->len == off
8221 * means no live instructions after (tail of the program was removed).
8222 */
8223 if (prog->len != off && l_cnt &&
8224 (i == nr_linfo || linfo[i].insn_off != off + cnt)) {
8225 l_cnt--;
8226 linfo[--i].insn_off = off + cnt;
8227 }
8228
8229 /* remove the line info which refer to the removed instructions */
8230 if (l_cnt) {
8231 memmove(linfo + l_off, linfo + i,
8232 sizeof(*linfo) * (nr_linfo - i));
8233
8234 prog->aux->nr_linfo -= l_cnt;
8235 nr_linfo = prog->aux->nr_linfo;
8236 }
8237
8238 /* pull all linfo[i].insn_off >= off + cnt in by cnt */
8239 for (i = l_off; i < nr_linfo; i++)
8240 linfo[i].insn_off -= cnt;
8241
8242 /* fix up all subprogs (incl. 'exit') which start >= off */
8243 for (i = 0; i <= env->subprog_cnt; i++)
8244 if (env->subprog_info[i].linfo_idx > l_off) {
8245 /* program may have started in the removed region but
8246 * may not be fully removed
8247 */
8248 if (env->subprog_info[i].linfo_idx >= l_off + l_cnt)
8249 env->subprog_info[i].linfo_idx -= l_cnt;
8250 else
8251 env->subprog_info[i].linfo_idx = l_off;
8252 }
8253
8254 return 0;
8255}
8256
8257static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)
8258{
8259 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
8260 unsigned int orig_prog_len = env->prog->len;
8261 int err;
8262
Jakub Kicinski08ca90a2019-01-22 22:45:24 -08008263 if (bpf_prog_is_dev_bound(env->prog->aux))
8264 bpf_prog_offload_remove_insns(env, off, cnt);
8265
Jakub Kicinski52875a02019-01-22 22:45:20 -08008266 err = bpf_remove_insns(env->prog, off, cnt);
8267 if (err)
8268 return err;
8269
8270 err = adjust_subprog_starts_after_remove(env, off, cnt);
8271 if (err)
8272 return err;
8273
8274 err = bpf_adj_linfo_after_remove(env, off, cnt);
8275 if (err)
8276 return err;
8277
8278 memmove(aux_data + off, aux_data + off + cnt,
8279 sizeof(*aux_data) * (orig_prog_len - off - cnt));
8280
8281 return 0;
8282}
8283
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01008284/* The verifier does more data flow analysis than llvm and will not
8285 * explore branches that are dead at run time. Malicious programs can
8286 * have dead code too. Therefore replace all dead at-run-time code
8287 * with 'ja -1'.
8288 *
8289 * Just nops are not optimal, e.g. if they would sit at the end of the
8290 * program and through another bug we would manage to jump there, then
8291 * we'd execute beyond program memory otherwise. Returning exception
8292 * code also wouldn't work since we can have subprogs where the dead
8293 * code could be located.
Alexei Starovoitovc1311872017-11-22 16:42:05 -08008294 */
8295static void sanitize_dead_code(struct bpf_verifier_env *env)
8296{
8297 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01008298 struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1);
Alexei Starovoitovc1311872017-11-22 16:42:05 -08008299 struct bpf_insn *insn = env->prog->insnsi;
8300 const int insn_cnt = env->prog->len;
8301 int i;
8302
8303 for (i = 0; i < insn_cnt; i++) {
8304 if (aux_data[i].seen)
8305 continue;
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01008306 memcpy(insn + i, &trap, sizeof(trap));
Alexei Starovoitovc1311872017-11-22 16:42:05 -08008307 }
8308}
8309
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08008310static bool insn_is_cond_jump(u8 code)
8311{
8312 u8 op;
8313
Jiong Wang092ed092019-01-26 12:26:01 -05008314 if (BPF_CLASS(code) == BPF_JMP32)
8315 return true;
8316
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08008317 if (BPF_CLASS(code) != BPF_JMP)
8318 return false;
8319
8320 op = BPF_OP(code);
8321 return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL;
8322}
8323
8324static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env)
8325{
8326 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
8327 struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0);
8328 struct bpf_insn *insn = env->prog->insnsi;
8329 const int insn_cnt = env->prog->len;
8330 int i;
8331
8332 for (i = 0; i < insn_cnt; i++, insn++) {
8333 if (!insn_is_cond_jump(insn->code))
8334 continue;
8335
8336 if (!aux_data[i + 1].seen)
8337 ja.off = insn->off;
8338 else if (!aux_data[i + 1 + insn->off].seen)
8339 ja.off = 0;
8340 else
8341 continue;
8342
Jakub Kicinski08ca90a2019-01-22 22:45:24 -08008343 if (bpf_prog_is_dev_bound(env->prog->aux))
8344 bpf_prog_offload_replace_insn(env, i, &ja);
8345
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08008346 memcpy(insn, &ja, sizeof(ja));
8347 }
8348}
8349
Jakub Kicinski52875a02019-01-22 22:45:20 -08008350static int opt_remove_dead_code(struct bpf_verifier_env *env)
8351{
8352 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
8353 int insn_cnt = env->prog->len;
8354 int i, err;
8355
8356 for (i = 0; i < insn_cnt; i++) {
8357 int j;
8358
8359 j = 0;
8360 while (i + j < insn_cnt && !aux_data[i + j].seen)
8361 j++;
8362 if (!j)
8363 continue;
8364
8365 err = verifier_remove_insns(env, i, j);
8366 if (err)
8367 return err;
8368 insn_cnt = env->prog->len;
8369 }
8370
8371 return 0;
8372}
8373
Jakub Kicinskia1b14ab2019-01-22 22:45:21 -08008374static int opt_remove_nops(struct bpf_verifier_env *env)
8375{
8376 const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0);
8377 struct bpf_insn *insn = env->prog->insnsi;
8378 int insn_cnt = env->prog->len;
8379 int i, err;
8380
8381 for (i = 0; i < insn_cnt; i++) {
8382 if (memcmp(&insn[i], &ja, sizeof(ja)))
8383 continue;
8384
8385 err = verifier_remove_insns(env, i, 1);
8386 if (err)
8387 return err;
8388 insn_cnt--;
8389 i--;
8390 }
8391
8392 return 0;
8393}
8394
Jiong Wangd6c23082019-05-24 23:25:18 +01008395static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env,
8396 const union bpf_attr *attr)
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01008397{
Jiong Wangd6c23082019-05-24 23:25:18 +01008398 struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4];
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01008399 struct bpf_insn_aux_data *aux = env->insn_aux_data;
Jiong Wangd6c23082019-05-24 23:25:18 +01008400 int i, patch_len, delta = 0, len = env->prog->len;
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01008401 struct bpf_insn *insns = env->prog->insnsi;
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01008402 struct bpf_prog *new_prog;
Jiong Wangd6c23082019-05-24 23:25:18 +01008403 bool rnd_hi32;
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01008404
Jiong Wangd6c23082019-05-24 23:25:18 +01008405 rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32;
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01008406 zext_patch[1] = BPF_ZEXT_REG(0);
Jiong Wangd6c23082019-05-24 23:25:18 +01008407 rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0);
8408 rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
8409 rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX);
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01008410 for (i = 0; i < len; i++) {
8411 int adj_idx = i + delta;
8412 struct bpf_insn insn;
8413
Jiong Wangd6c23082019-05-24 23:25:18 +01008414 insn = insns[adj_idx];
8415 if (!aux[adj_idx].zext_dst) {
8416 u8 code, class;
8417 u32 imm_rnd;
8418
8419 if (!rnd_hi32)
8420 continue;
8421
8422 code = insn.code;
8423 class = BPF_CLASS(code);
8424 if (insn_no_def(&insn))
8425 continue;
8426
8427 /* NOTE: arg "reg" (the fourth one) is only used for
8428 * BPF_STX which has been ruled out in above
8429 * check, it is safe to pass NULL here.
8430 */
8431 if (is_reg64(env, &insn, insn.dst_reg, NULL, DST_OP)) {
8432 if (class == BPF_LD &&
8433 BPF_MODE(code) == BPF_IMM)
8434 i++;
8435 continue;
8436 }
8437
8438 /* ctx load could be transformed into wider load. */
8439 if (class == BPF_LDX &&
8440 aux[adj_idx].ptr_type == PTR_TO_CTX)
8441 continue;
8442
8443 imm_rnd = get_random_int();
8444 rnd_hi32_patch[0] = insn;
8445 rnd_hi32_patch[1].imm = imm_rnd;
8446 rnd_hi32_patch[3].dst_reg = insn.dst_reg;
8447 patch = rnd_hi32_patch;
8448 patch_len = 4;
8449 goto apply_patch_buffer;
8450 }
8451
8452 if (!bpf_jit_needs_zext())
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01008453 continue;
8454
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01008455 zext_patch[0] = insn;
8456 zext_patch[1].dst_reg = insn.dst_reg;
8457 zext_patch[1].src_reg = insn.dst_reg;
Jiong Wangd6c23082019-05-24 23:25:18 +01008458 patch = zext_patch;
8459 patch_len = 2;
8460apply_patch_buffer:
8461 new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len);
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01008462 if (!new_prog)
8463 return -ENOMEM;
8464 env->prog = new_prog;
8465 insns = new_prog->insnsi;
8466 aux = env->insn_aux_data;
Jiong Wangd6c23082019-05-24 23:25:18 +01008467 delta += patch_len - 1;
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01008468 }
8469
8470 return 0;
8471}
8472
Joe Stringerc64b7982018-10-02 13:35:33 -07008473/* convert load instructions that access fields of a context type into a
8474 * sequence of instructions that access fields of the underlying structure:
8475 * struct __sk_buff -> struct sk_buff
8476 * struct bpf_sock_ops -> struct sock
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008477 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01008478static int convert_ctx_accesses(struct bpf_verifier_env *env)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008479{
Jakub Kicinski00176a32017-10-16 16:40:54 -07008480 const struct bpf_verifier_ops *ops = env->ops;
Daniel Borkmannf96da092017-07-02 02:13:27 +02008481 int i, cnt, size, ctx_field_size, delta = 0;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01008482 const int insn_cnt = env->prog->len;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02008483 struct bpf_insn insn_buf[16], *insn;
Andrey Ignatov46f53a62018-11-10 22:15:13 -08008484 u32 target_size, size_default, off;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008485 struct bpf_prog *new_prog;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07008486 enum bpf_access_type type;
Daniel Borkmannf96da092017-07-02 02:13:27 +02008487 bool is_narrower_load;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008488
Daniel Borkmannb09928b2018-10-24 22:05:49 +02008489 if (ops->gen_prologue || env->seen_direct_write) {
8490 if (!ops->gen_prologue) {
8491 verbose(env, "bpf verifier is misconfigured\n");
8492 return -EINVAL;
8493 }
Daniel Borkmann36bbef52016-09-20 00:26:13 +02008494 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
8495 env->prog);
8496 if (cnt >= ARRAY_SIZE(insn_buf)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07008497 verbose(env, "bpf verifier is misconfigured\n");
Daniel Borkmann36bbef52016-09-20 00:26:13 +02008498 return -EINVAL;
8499 } else if (cnt) {
Alexei Starovoitov80419022017-03-15 18:26:41 -07008500 new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
Daniel Borkmann36bbef52016-09-20 00:26:13 +02008501 if (!new_prog)
8502 return -ENOMEM;
Alexei Starovoitov80419022017-03-15 18:26:41 -07008503
Daniel Borkmann36bbef52016-09-20 00:26:13 +02008504 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01008505 delta += cnt - 1;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02008506 }
8507 }
8508
Joe Stringerc64b7982018-10-02 13:35:33 -07008509 if (bpf_prog_is_dev_bound(env->prog->aux))
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008510 return 0;
8511
Jakub Kicinski3df126f2016-09-21 11:43:56 +01008512 insn = env->prog->insnsi + delta;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02008513
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008514 for (i = 0; i < insn_cnt; i++, insn++) {
Joe Stringerc64b7982018-10-02 13:35:33 -07008515 bpf_convert_ctx_access_t convert_ctx_access;
8516
Daniel Borkmann62c79892017-01-12 11:51:33 +01008517 if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) ||
8518 insn->code == (BPF_LDX | BPF_MEM | BPF_H) ||
8519 insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07008520 insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07008521 type = BPF_READ;
Daniel Borkmann62c79892017-01-12 11:51:33 +01008522 else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) ||
8523 insn->code == (BPF_STX | BPF_MEM | BPF_H) ||
8524 insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07008525 insn->code == (BPF_STX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07008526 type = BPF_WRITE;
8527 else
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008528 continue;
8529
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07008530 if (type == BPF_WRITE &&
8531 env->insn_aux_data[i + delta].sanitize_stack_off) {
8532 struct bpf_insn patch[] = {
8533 /* Sanitize suspicious stack slot with zero.
8534 * There are no memory dependencies for this store,
8535 * since it's only using frame pointer and immediate
8536 * constant of zero
8537 */
8538 BPF_ST_MEM(BPF_DW, BPF_REG_FP,
8539 env->insn_aux_data[i + delta].sanitize_stack_off,
8540 0),
8541 /* the original STX instruction will immediately
8542 * overwrite the same stack slot with appropriate value
8543 */
8544 *insn,
8545 };
8546
8547 cnt = ARRAY_SIZE(patch);
8548 new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt);
8549 if (!new_prog)
8550 return -ENOMEM;
8551
8552 delta += cnt - 1;
8553 env->prog = new_prog;
8554 insn = new_prog->insnsi + i + delta;
8555 continue;
8556 }
8557
Joe Stringerc64b7982018-10-02 13:35:33 -07008558 switch (env->insn_aux_data[i + delta].ptr_type) {
8559 case PTR_TO_CTX:
8560 if (!ops->convert_ctx_access)
8561 continue;
8562 convert_ctx_access = ops->convert_ctx_access;
8563 break;
8564 case PTR_TO_SOCKET:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08008565 case PTR_TO_SOCK_COMMON:
Joe Stringerc64b7982018-10-02 13:35:33 -07008566 convert_ctx_access = bpf_sock_convert_ctx_access;
8567 break;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08008568 case PTR_TO_TCP_SOCK:
8569 convert_ctx_access = bpf_tcp_sock_convert_ctx_access;
8570 break;
Jonathan Lemonfada7fd2019-06-06 13:59:40 -07008571 case PTR_TO_XDP_SOCK:
8572 convert_ctx_access = bpf_xdp_sock_convert_ctx_access;
8573 break;
Joe Stringerc64b7982018-10-02 13:35:33 -07008574 default:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008575 continue;
Joe Stringerc64b7982018-10-02 13:35:33 -07008576 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008577
Yonghong Song31fd8582017-06-13 15:52:13 -07008578 ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size;
Daniel Borkmannf96da092017-07-02 02:13:27 +02008579 size = BPF_LDST_BYTES(insn);
Yonghong Song31fd8582017-06-13 15:52:13 -07008580
8581 /* If the read access is a narrower load of the field,
8582 * convert to a 4/8-byte load, to minimum program type specific
8583 * convert_ctx_access changes. If conversion is successful,
8584 * we will apply proper mask to the result.
8585 */
Daniel Borkmannf96da092017-07-02 02:13:27 +02008586 is_narrower_load = size < ctx_field_size;
Andrey Ignatov46f53a62018-11-10 22:15:13 -08008587 size_default = bpf_ctx_off_adjust_machine(ctx_field_size);
8588 off = insn->off;
Yonghong Song31fd8582017-06-13 15:52:13 -07008589 if (is_narrower_load) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02008590 u8 size_code;
Yonghong Song31fd8582017-06-13 15:52:13 -07008591
Daniel Borkmannf96da092017-07-02 02:13:27 +02008592 if (type == BPF_WRITE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07008593 verbose(env, "bpf verifier narrow ctx access misconfigured\n");
Daniel Borkmannf96da092017-07-02 02:13:27 +02008594 return -EINVAL;
8595 }
8596
8597 size_code = BPF_H;
Yonghong Song31fd8582017-06-13 15:52:13 -07008598 if (ctx_field_size == 4)
8599 size_code = BPF_W;
8600 else if (ctx_field_size == 8)
8601 size_code = BPF_DW;
Daniel Borkmannf96da092017-07-02 02:13:27 +02008602
Daniel Borkmannbc231052018-06-02 23:06:39 +02008603 insn->off = off & ~(size_default - 1);
Yonghong Song31fd8582017-06-13 15:52:13 -07008604 insn->code = BPF_LDX | BPF_MEM | size_code;
8605 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02008606
8607 target_size = 0;
Joe Stringerc64b7982018-10-02 13:35:33 -07008608 cnt = convert_ctx_access(type, insn, insn_buf, env->prog,
8609 &target_size);
Daniel Borkmannf96da092017-07-02 02:13:27 +02008610 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) ||
8611 (ctx_field_size && !target_size)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07008612 verbose(env, "bpf verifier is misconfigured\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008613 return -EINVAL;
8614 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02008615
8616 if (is_narrower_load && size < target_size) {
Andrey Ignatov46f53a62018-11-10 22:15:13 -08008617 u8 shift = (off & (size_default - 1)) * 8;
8618
8619 if (ctx_field_size <= 4) {
8620 if (shift)
8621 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH,
8622 insn->dst_reg,
8623 shift);
Yonghong Song31fd8582017-06-13 15:52:13 -07008624 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02008625 (1 << size * 8) - 1);
Andrey Ignatov46f53a62018-11-10 22:15:13 -08008626 } else {
8627 if (shift)
8628 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH,
8629 insn->dst_reg,
8630 shift);
Yonghong Song31fd8582017-06-13 15:52:13 -07008631 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg,
Krzesimir Nowake2f7fc02019-05-08 18:08:58 +02008632 (1ULL << size * 8) - 1);
Andrey Ignatov46f53a62018-11-10 22:15:13 -08008633 }
Yonghong Song31fd8582017-06-13 15:52:13 -07008634 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008635
Alexei Starovoitov80419022017-03-15 18:26:41 -07008636 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008637 if (!new_prog)
8638 return -ENOMEM;
8639
Jakub Kicinski3df126f2016-09-21 11:43:56 +01008640 delta += cnt - 1;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008641
8642 /* keep walking new program and skip insns we just inserted */
8643 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01008644 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008645 }
8646
8647 return 0;
8648}
8649
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008650static int jit_subprogs(struct bpf_verifier_env *env)
8651{
8652 struct bpf_prog *prog = env->prog, **func, *tmp;
8653 int i, j, subprog_start, subprog_end = 0, len, subprog;
Daniel Borkmann7105e822017-12-20 13:42:57 +01008654 struct bpf_insn *insn;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008655 void *old_bpf_func;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08008656 int err;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008657
Jiong Wangf910cef2018-05-02 16:17:17 -04008658 if (env->subprog_cnt <= 1)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008659 return 0;
8660
Daniel Borkmann7105e822017-12-20 13:42:57 +01008661 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008662 if (insn->code != (BPF_JMP | BPF_CALL) ||
8663 insn->src_reg != BPF_PSEUDO_CALL)
8664 continue;
Daniel Borkmannc7a89782018-07-12 21:44:28 +02008665 /* Upon error here we cannot fall back to interpreter but
8666 * need a hard reject of the program. Thus -EFAULT is
8667 * propagated in any case.
8668 */
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008669 subprog = find_subprog(env, i + insn->imm + 1);
8670 if (subprog < 0) {
8671 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
8672 i + insn->imm + 1);
8673 return -EFAULT;
8674 }
8675 /* temporarily remember subprog id inside insn instead of
8676 * aux_data, since next loop will split up all insns into funcs
8677 */
Jiong Wangf910cef2018-05-02 16:17:17 -04008678 insn->off = subprog;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008679 /* remember original imm in case JIT fails and fallback
8680 * to interpreter will be needed
8681 */
8682 env->insn_aux_data[i].call_imm = insn->imm;
8683 /* point imm to __bpf_call_base+1 from JITs point of view */
8684 insn->imm = 1;
8685 }
8686
Martin KaFai Lauc454a462018-12-07 16:42:25 -08008687 err = bpf_prog_alloc_jited_linfo(prog);
8688 if (err)
8689 goto out_undo_insn;
8690
8691 err = -ENOMEM;
Kees Cook6396bb22018-06-12 14:03:40 -07008692 func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008693 if (!func)
Daniel Borkmannc7a89782018-07-12 21:44:28 +02008694 goto out_undo_insn;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008695
Jiong Wangf910cef2018-05-02 16:17:17 -04008696 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008697 subprog_start = subprog_end;
Jiong Wang4cb3d992018-05-02 16:17:19 -04008698 subprog_end = env->subprog_info[i + 1].start;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008699
8700 len = subprog_end - subprog_start;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08008701 /* BPF_PROG_RUN doesn't call subprogs directly,
8702 * hence main prog stats include the runtime of subprogs.
8703 * subprogs don't have IDs and not reachable via prog_get_next_id
8704 * func[i]->aux->stats will never be accessed and stays NULL
8705 */
8706 func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008707 if (!func[i])
8708 goto out_free;
8709 memcpy(func[i]->insnsi, &prog->insnsi[subprog_start],
8710 len * sizeof(struct bpf_insn));
Daniel Borkmann4f74d802017-12-20 13:42:56 +01008711 func[i]->type = prog->type;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008712 func[i]->len = len;
Daniel Borkmann4f74d802017-12-20 13:42:56 +01008713 if (bpf_prog_calc_tag(func[i]))
8714 goto out_free;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008715 func[i]->is_func = 1;
Yonghong Songba64e7d2018-11-24 23:20:44 -08008716 func[i]->aux->func_idx = i;
8717 /* the btf and func_info will be freed only at prog->aux */
8718 func[i]->aux->btf = prog->aux->btf;
8719 func[i]->aux->func_info = prog->aux->func_info;
8720
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008721 /* Use bpf_prog_F_tag to indicate functions in stack traces.
8722 * Long term would need debug info to populate names
8723 */
8724 func[i]->aux->name[0] = 'F';
Jiong Wang9c8105b2018-05-02 16:17:18 -04008725 func[i]->aux->stack_depth = env->subprog_info[i].stack_depth;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008726 func[i]->jit_requested = 1;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08008727 func[i]->aux->linfo = prog->aux->linfo;
8728 func[i]->aux->nr_linfo = prog->aux->nr_linfo;
8729 func[i]->aux->jited_linfo = prog->aux->jited_linfo;
8730 func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008731 func[i] = bpf_int_jit_compile(func[i]);
8732 if (!func[i]->jited) {
8733 err = -ENOTSUPP;
8734 goto out_free;
8735 }
8736 cond_resched();
8737 }
8738 /* at this point all bpf functions were successfully JITed
8739 * now populate all bpf_calls with correct addresses and
8740 * run last pass of JIT
8741 */
Jiong Wangf910cef2018-05-02 16:17:17 -04008742 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008743 insn = func[i]->insnsi;
8744 for (j = 0; j < func[i]->len; j++, insn++) {
8745 if (insn->code != (BPF_JMP | BPF_CALL) ||
8746 insn->src_reg != BPF_PSEUDO_CALL)
8747 continue;
8748 subprog = insn->off;
Prashant Bhole0d306c32019-04-16 18:13:01 +09008749 insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) -
8750 __bpf_call_base;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008751 }
Sandipan Das2162fed2018-05-24 12:26:45 +05308752
8753 /* we use the aux data to keep a list of the start addresses
8754 * of the JITed images for each function in the program
8755 *
8756 * for some architectures, such as powerpc64, the imm field
8757 * might not be large enough to hold the offset of the start
8758 * address of the callee's JITed image from __bpf_call_base
8759 *
8760 * in such cases, we can lookup the start address of a callee
8761 * by using its subprog id, available from the off field of
8762 * the call instruction, as an index for this list
8763 */
8764 func[i]->aux->func = func;
8765 func[i]->aux->func_cnt = env->subprog_cnt;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008766 }
Jiong Wangf910cef2018-05-02 16:17:17 -04008767 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008768 old_bpf_func = func[i]->bpf_func;
8769 tmp = bpf_int_jit_compile(func[i]);
8770 if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) {
8771 verbose(env, "JIT doesn't support bpf-to-bpf calls\n");
Daniel Borkmannc7a89782018-07-12 21:44:28 +02008772 err = -ENOTSUPP;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008773 goto out_free;
8774 }
8775 cond_resched();
8776 }
8777
8778 /* finally lock prog and jit images for all functions and
8779 * populate kallsysm
8780 */
Jiong Wangf910cef2018-05-02 16:17:17 -04008781 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008782 bpf_prog_lock_ro(func[i]);
8783 bpf_prog_kallsyms_add(func[i]);
8784 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01008785
8786 /* Last step: make now unused interpreter insns from main
8787 * prog consistent for later dump requests, so they can
8788 * later look the same as if they were interpreted only.
8789 */
8790 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01008791 if (insn->code != (BPF_JMP | BPF_CALL) ||
8792 insn->src_reg != BPF_PSEUDO_CALL)
8793 continue;
8794 insn->off = env->insn_aux_data[i].call_imm;
8795 subprog = find_subprog(env, i + insn->off + 1);
Sandipan Dasdbecd732018-05-24 12:26:48 +05308796 insn->imm = subprog;
Daniel Borkmann7105e822017-12-20 13:42:57 +01008797 }
8798
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008799 prog->jited = 1;
8800 prog->bpf_func = func[0]->bpf_func;
8801 prog->aux->func = func;
Jiong Wangf910cef2018-05-02 16:17:17 -04008802 prog->aux->func_cnt = env->subprog_cnt;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08008803 bpf_prog_free_unused_jited_linfo(prog);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008804 return 0;
8805out_free:
Jiong Wangf910cef2018-05-02 16:17:17 -04008806 for (i = 0; i < env->subprog_cnt; i++)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008807 if (func[i])
8808 bpf_jit_free(func[i]);
8809 kfree(func);
Daniel Borkmannc7a89782018-07-12 21:44:28 +02008810out_undo_insn:
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008811 /* cleanup main prog to be interpreted */
8812 prog->jit_requested = 0;
8813 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
8814 if (insn->code != (BPF_JMP | BPF_CALL) ||
8815 insn->src_reg != BPF_PSEUDO_CALL)
8816 continue;
8817 insn->off = 0;
8818 insn->imm = env->insn_aux_data[i].call_imm;
8819 }
Martin KaFai Lauc454a462018-12-07 16:42:25 -08008820 bpf_prog_free_jited_linfo(prog);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008821 return err;
8822}
8823
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08008824static int fixup_call_args(struct bpf_verifier_env *env)
8825{
David S. Miller19d28fb2018-01-11 21:27:54 -05008826#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08008827 struct bpf_prog *prog = env->prog;
8828 struct bpf_insn *insn = prog->insnsi;
8829 int i, depth;
David S. Miller19d28fb2018-01-11 21:27:54 -05008830#endif
Quentin Monnete4052d02018-10-07 12:56:58 +01008831 int err = 0;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08008832
Quentin Monnete4052d02018-10-07 12:56:58 +01008833 if (env->prog->jit_requested &&
8834 !bpf_prog_is_dev_bound(env->prog->aux)) {
David S. Miller19d28fb2018-01-11 21:27:54 -05008835 err = jit_subprogs(env);
8836 if (err == 0)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008837 return 0;
Daniel Borkmannc7a89782018-07-12 21:44:28 +02008838 if (err == -EFAULT)
8839 return err;
David S. Miller19d28fb2018-01-11 21:27:54 -05008840 }
8841#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08008842 for (i = 0; i < prog->len; i++, insn++) {
8843 if (insn->code != (BPF_JMP | BPF_CALL) ||
8844 insn->src_reg != BPF_PSEUDO_CALL)
8845 continue;
8846 depth = get_callee_stack_depth(env, insn, i);
8847 if (depth < 0)
8848 return depth;
8849 bpf_patch_call_args(insn, depth);
8850 }
David S. Miller19d28fb2018-01-11 21:27:54 -05008851 err = 0;
8852#endif
8853 return err;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08008854}
8855
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008856/* fixup insn->imm field of bpf_call instructions
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07008857 * and inline eligible helpers as explicit sequence of BPF instructions
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008858 *
8859 * this function is called after eBPF program passed verification
8860 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008861static int fixup_bpf_calls(struct bpf_verifier_env *env)
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008862{
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008863 struct bpf_prog *prog = env->prog;
8864 struct bpf_insn *insn = prog->insnsi;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008865 const struct bpf_func_proto *fn;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008866 const int insn_cnt = prog->len;
Daniel Borkmann09772d92018-06-02 23:06:35 +02008867 const struct bpf_map_ops *ops;
Daniel Borkmannc93552c2018-05-24 02:32:53 +02008868 struct bpf_insn_aux_data *aux;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07008869 struct bpf_insn insn_buf[16];
8870 struct bpf_prog *new_prog;
8871 struct bpf_map *map_ptr;
8872 int i, cnt, delta = 0;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008873
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008874 for (i = 0; i < insn_cnt; i++, insn++) {
Daniel Borkmannf6b1b3b2018-01-26 23:33:39 +01008875 if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) ||
8876 insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) ||
8877 insn->code == (BPF_ALU | BPF_MOD | BPF_X) ||
Alexei Starovoitov68fda452018-01-12 18:59:52 -08008878 insn->code == (BPF_ALU | BPF_DIV | BPF_X)) {
Daniel Borkmannf6b1b3b2018-01-26 23:33:39 +01008879 bool is64 = BPF_CLASS(insn->code) == BPF_ALU64;
8880 struct bpf_insn mask_and_div[] = {
8881 BPF_MOV32_REG(insn->src_reg, insn->src_reg),
8882 /* Rx div 0 -> 0 */
8883 BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2),
8884 BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg),
8885 BPF_JMP_IMM(BPF_JA, 0, 0, 1),
8886 *insn,
8887 };
8888 struct bpf_insn mask_and_mod[] = {
8889 BPF_MOV32_REG(insn->src_reg, insn->src_reg),
8890 /* Rx mod 0 -> Rx */
8891 BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1),
8892 *insn,
8893 };
8894 struct bpf_insn *patchlet;
8895
8896 if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) ||
8897 insn->code == (BPF_ALU | BPF_DIV | BPF_X)) {
8898 patchlet = mask_and_div + (is64 ? 1 : 0);
8899 cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0);
8900 } else {
8901 patchlet = mask_and_mod + (is64 ? 1 : 0);
8902 cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0);
8903 }
8904
8905 new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt);
Alexei Starovoitov68fda452018-01-12 18:59:52 -08008906 if (!new_prog)
8907 return -ENOMEM;
8908
8909 delta += cnt - 1;
8910 env->prog = prog = new_prog;
8911 insn = new_prog->insnsi + i + delta;
8912 continue;
8913 }
8914
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +02008915 if (BPF_CLASS(insn->code) == BPF_LD &&
8916 (BPF_MODE(insn->code) == BPF_ABS ||
8917 BPF_MODE(insn->code) == BPF_IND)) {
8918 cnt = env->ops->gen_ld_abs(insn, insn_buf);
8919 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
8920 verbose(env, "bpf verifier is misconfigured\n");
8921 return -EINVAL;
8922 }
8923
8924 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
8925 if (!new_prog)
8926 return -ENOMEM;
8927
8928 delta += cnt - 1;
8929 env->prog = prog = new_prog;
8930 insn = new_prog->insnsi + i + delta;
8931 continue;
8932 }
8933
Daniel Borkmann979d63d2019-01-03 00:58:34 +01008934 if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) ||
8935 insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) {
8936 const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X;
8937 const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X;
8938 struct bpf_insn insn_buf[16];
8939 struct bpf_insn *patch = &insn_buf[0];
8940 bool issrc, isneg;
8941 u32 off_reg;
8942
8943 aux = &env->insn_aux_data[i + delta];
Daniel Borkmann3612af72019-03-01 22:05:29 +01008944 if (!aux->alu_state ||
8945 aux->alu_state == BPF_ALU_NON_POINTER)
Daniel Borkmann979d63d2019-01-03 00:58:34 +01008946 continue;
8947
8948 isneg = aux->alu_state & BPF_ALU_NEG_VALUE;
8949 issrc = (aux->alu_state & BPF_ALU_SANITIZE) ==
8950 BPF_ALU_SANITIZE_SRC;
8951
8952 off_reg = issrc ? insn->src_reg : insn->dst_reg;
8953 if (isneg)
8954 *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1);
8955 *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1);
8956 *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg);
8957 *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg);
8958 *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0);
8959 *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63);
8960 if (issrc) {
8961 *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX,
8962 off_reg);
8963 insn->src_reg = BPF_REG_AX;
8964 } else {
8965 *patch++ = BPF_ALU64_REG(BPF_AND, off_reg,
8966 BPF_REG_AX);
8967 }
8968 if (isneg)
8969 insn->code = insn->code == code_add ?
8970 code_sub : code_add;
8971 *patch++ = *insn;
8972 if (issrc && isneg)
8973 *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1);
8974 cnt = patch - insn_buf;
8975
8976 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
8977 if (!new_prog)
8978 return -ENOMEM;
8979
8980 delta += cnt - 1;
8981 env->prog = prog = new_prog;
8982 insn = new_prog->insnsi + i + delta;
8983 continue;
8984 }
8985
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008986 if (insn->code != (BPF_JMP | BPF_CALL))
8987 continue;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08008988 if (insn->src_reg == BPF_PSEUDO_CALL)
8989 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008990
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008991 if (insn->imm == BPF_FUNC_get_route_realm)
8992 prog->dst_needed = 1;
8993 if (insn->imm == BPF_FUNC_get_prandom_u32)
8994 bpf_user_rnd_init_once();
Josef Bacik9802d862017-12-11 11:36:48 -05008995 if (insn->imm == BPF_FUNC_override_return)
8996 prog->kprobe_override = 1;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008997 if (insn->imm == BPF_FUNC_tail_call) {
David S. Miller7b9f6da2017-04-20 10:35:33 -04008998 /* If we tail call into other programs, we
8999 * cannot make any assumptions since they can
9000 * be replaced dynamically during runtime in
9001 * the program array.
9002 */
9003 prog->cb_access = 1;
Alexei Starovoitov80a58d02017-05-30 13:31:30 -07009004 env->prog->aux->stack_depth = MAX_BPF_STACK;
Jiong Wange6478152018-11-08 04:08:42 -05009005 env->prog->aux->max_pkt_offset = MAX_PACKET_OFF;
David S. Miller7b9f6da2017-04-20 10:35:33 -04009006
Alexei Starovoitov79741b32017-03-15 18:26:40 -07009007 /* mark bpf_tail_call as different opcode to avoid
9008 * conditional branch in the interpeter for every normal
9009 * call and to prevent accidental JITing by JIT compiler
9010 * that doesn't support bpf_tail_call yet
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07009011 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07009012 insn->imm = 0;
Alexei Starovoitov71189fa2017-05-30 13:31:27 -07009013 insn->code = BPF_JMP | BPF_TAIL_CALL;
Alexei Starovoitovb2157392018-01-07 17:33:02 -08009014
Daniel Borkmannc93552c2018-05-24 02:32:53 +02009015 aux = &env->insn_aux_data[i + delta];
9016 if (!bpf_map_ptr_unpriv(aux))
9017 continue;
9018
Alexei Starovoitovb2157392018-01-07 17:33:02 -08009019 /* instead of changing every JIT dealing with tail_call
9020 * emit two extra insns:
9021 * if (index >= max_entries) goto out;
9022 * index &= array->index_mask;
9023 * to avoid out-of-bounds cpu speculation
9024 */
Daniel Borkmannc93552c2018-05-24 02:32:53 +02009025 if (bpf_map_ptr_poisoned(aux)) {
Colin Ian King40950342018-01-10 09:20:54 +00009026 verbose(env, "tail_call abusing map_ptr\n");
Alexei Starovoitovb2157392018-01-07 17:33:02 -08009027 return -EINVAL;
9028 }
Daniel Borkmannc93552c2018-05-24 02:32:53 +02009029
9030 map_ptr = BPF_MAP_PTR(aux->map_state);
Alexei Starovoitovb2157392018-01-07 17:33:02 -08009031 insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3,
9032 map_ptr->max_entries, 2);
9033 insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3,
9034 container_of(map_ptr,
9035 struct bpf_array,
9036 map)->index_mask);
9037 insn_buf[2] = *insn;
9038 cnt = 3;
9039 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
9040 if (!new_prog)
9041 return -ENOMEM;
9042
9043 delta += cnt - 1;
9044 env->prog = prog = new_prog;
9045 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07009046 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07009047 }
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07009048
Daniel Borkmann89c63072017-08-19 03:12:45 +02009049 /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup
Daniel Borkmann09772d92018-06-02 23:06:35 +02009050 * and other inlining handlers are currently limited to 64 bit
9051 * only.
Daniel Borkmann89c63072017-08-19 03:12:45 +02009052 */
Alexei Starovoitov60b58afc2017-12-14 17:55:14 -08009053 if (prog->jit_requested && BITS_PER_LONG == 64 &&
Daniel Borkmann09772d92018-06-02 23:06:35 +02009054 (insn->imm == BPF_FUNC_map_lookup_elem ||
9055 insn->imm == BPF_FUNC_map_update_elem ||
Daniel Borkmann84430d42018-10-21 02:09:27 +02009056 insn->imm == BPF_FUNC_map_delete_elem ||
9057 insn->imm == BPF_FUNC_map_push_elem ||
9058 insn->imm == BPF_FUNC_map_pop_elem ||
9059 insn->imm == BPF_FUNC_map_peek_elem)) {
Daniel Borkmannc93552c2018-05-24 02:32:53 +02009060 aux = &env->insn_aux_data[i + delta];
9061 if (bpf_map_ptr_poisoned(aux))
9062 goto patch_call_imm;
9063
9064 map_ptr = BPF_MAP_PTR(aux->map_state);
Daniel Borkmann09772d92018-06-02 23:06:35 +02009065 ops = map_ptr->ops;
9066 if (insn->imm == BPF_FUNC_map_lookup_elem &&
9067 ops->map_gen_lookup) {
9068 cnt = ops->map_gen_lookup(map_ptr, insn_buf);
9069 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
9070 verbose(env, "bpf verifier is misconfigured\n");
9071 return -EINVAL;
9072 }
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07009073
Daniel Borkmann09772d92018-06-02 23:06:35 +02009074 new_prog = bpf_patch_insn_data(env, i + delta,
9075 insn_buf, cnt);
9076 if (!new_prog)
9077 return -ENOMEM;
9078
9079 delta += cnt - 1;
9080 env->prog = prog = new_prog;
9081 insn = new_prog->insnsi + i + delta;
9082 continue;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07009083 }
9084
Daniel Borkmann09772d92018-06-02 23:06:35 +02009085 BUILD_BUG_ON(!__same_type(ops->map_lookup_elem,
9086 (void *(*)(struct bpf_map *map, void *key))NULL));
9087 BUILD_BUG_ON(!__same_type(ops->map_delete_elem,
9088 (int (*)(struct bpf_map *map, void *key))NULL));
9089 BUILD_BUG_ON(!__same_type(ops->map_update_elem,
9090 (int (*)(struct bpf_map *map, void *key, void *value,
9091 u64 flags))NULL));
Daniel Borkmann84430d42018-10-21 02:09:27 +02009092 BUILD_BUG_ON(!__same_type(ops->map_push_elem,
9093 (int (*)(struct bpf_map *map, void *value,
9094 u64 flags))NULL));
9095 BUILD_BUG_ON(!__same_type(ops->map_pop_elem,
9096 (int (*)(struct bpf_map *map, void *value))NULL));
9097 BUILD_BUG_ON(!__same_type(ops->map_peek_elem,
9098 (int (*)(struct bpf_map *map, void *value))NULL));
9099
Daniel Borkmann09772d92018-06-02 23:06:35 +02009100 switch (insn->imm) {
9101 case BPF_FUNC_map_lookup_elem:
9102 insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) -
9103 __bpf_call_base;
9104 continue;
9105 case BPF_FUNC_map_update_elem:
9106 insn->imm = BPF_CAST_CALL(ops->map_update_elem) -
9107 __bpf_call_base;
9108 continue;
9109 case BPF_FUNC_map_delete_elem:
9110 insn->imm = BPF_CAST_CALL(ops->map_delete_elem) -
9111 __bpf_call_base;
9112 continue;
Daniel Borkmann84430d42018-10-21 02:09:27 +02009113 case BPF_FUNC_map_push_elem:
9114 insn->imm = BPF_CAST_CALL(ops->map_push_elem) -
9115 __bpf_call_base;
9116 continue;
9117 case BPF_FUNC_map_pop_elem:
9118 insn->imm = BPF_CAST_CALL(ops->map_pop_elem) -
9119 __bpf_call_base;
9120 continue;
9121 case BPF_FUNC_map_peek_elem:
9122 insn->imm = BPF_CAST_CALL(ops->map_peek_elem) -
9123 __bpf_call_base;
9124 continue;
Daniel Borkmann09772d92018-06-02 23:06:35 +02009125 }
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07009126
Daniel Borkmann09772d92018-06-02 23:06:35 +02009127 goto patch_call_imm;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07009128 }
9129
9130patch_call_imm:
Andrey Ignatov5e43f892018-03-30 15:08:00 -07009131 fn = env->ops->get_func_proto(insn->imm, env->prog);
Alexei Starovoitov79741b32017-03-15 18:26:40 -07009132 /* all functions that have prototype and verifier allowed
9133 * programs to call them, must be real in-kernel functions
9134 */
9135 if (!fn->func) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07009136 verbose(env,
9137 "kernel subsystem misconfigured func %s#%d\n",
Alexei Starovoitov79741b32017-03-15 18:26:40 -07009138 func_id_name(insn->imm), insn->imm);
9139 return -EFAULT;
9140 }
9141 insn->imm = fn->func - __bpf_call_base;
9142 }
9143
9144 return 0;
9145}
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07009146
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01009147static void free_states(struct bpf_verifier_env *env)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07009148{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01009149 struct bpf_verifier_state_list *sl, *sln;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07009150 int i;
9151
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -07009152 sl = env->free_list;
9153 while (sl) {
9154 sln = sl->next;
9155 free_verifier_state(&sl->state, false);
9156 kfree(sl);
9157 sl = sln;
9158 }
9159
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07009160 if (!env->explored_states)
9161 return;
9162
Alexei Starovoitovdc2a4eb2019-05-21 20:17:07 -07009163 for (i = 0; i < state_htab_size(env); i++) {
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07009164 sl = env->explored_states[i];
9165
Alexei Starovoitova8f500a2019-05-21 20:17:06 -07009166 while (sl) {
9167 sln = sl->next;
9168 free_verifier_state(&sl->state, false);
9169 kfree(sl);
9170 sl = sln;
9171 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07009172 }
9173
Alexei Starovoitov71dde682019-04-01 21:27:43 -07009174 kvfree(env->explored_states);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07009175}
9176
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07009177static void print_verification_stats(struct bpf_verifier_env *env)
9178{
9179 int i;
9180
9181 if (env->log.level & BPF_LOG_STATS) {
9182 verbose(env, "verification time %lld usec\n",
9183 div_u64(env->verification_time, 1000));
9184 verbose(env, "stack depth ");
9185 for (i = 0; i < env->subprog_cnt; i++) {
9186 u32 depth = env->subprog_info[i].stack_depth;
9187
9188 verbose(env, "%d", depth);
9189 if (i + 1 < env->subprog_cnt)
9190 verbose(env, "+");
9191 }
9192 verbose(env, "\n");
9193 }
9194 verbose(env, "processed %d insns (limit %d) max_states_per_insn %d "
9195 "total_states %d peak_states %d mark_read %d\n",
9196 env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS,
9197 env->max_states_per_insn, env->total_states,
9198 env->peak_states, env->longest_mark_read_walk);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07009199}
9200
Yonghong Song838e9692018-11-19 15:29:11 -08009201int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,
9202 union bpf_attr __user *uattr)
Alexei Starovoitov51580e72014-09-26 00:17:02 -07009203{
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07009204 u64 start_time = ktime_get_ns();
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01009205 struct bpf_verifier_env *env;
Martin KaFai Laub9193c12018-03-24 11:44:22 -07009206 struct bpf_verifier_log *log;
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08009207 int i, len, ret = -EINVAL;
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08009208 bool is_priv;
Alexei Starovoitov51580e72014-09-26 00:17:02 -07009209
Arnd Bergmanneba0c922017-11-02 12:05:52 +01009210 /* no program is valid */
9211 if (ARRAY_SIZE(bpf_verifier_ops) == 0)
9212 return -EINVAL;
9213
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01009214 /* 'struct bpf_verifier_env' can be global, but since it's not small,
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07009215 * allocate/free it every time bpf_check() is called
9216 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01009217 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07009218 if (!env)
9219 return -ENOMEM;
Jakub Kicinski61bd5212017-10-09 10:30:11 -07009220 log = &env->log;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07009221
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08009222 len = (*prog)->len;
Kees Cookfad953c2018-06-12 14:27:37 -07009223 env->insn_aux_data =
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08009224 vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len));
Jakub Kicinski3df126f2016-09-21 11:43:56 +01009225 ret = -ENOMEM;
9226 if (!env->insn_aux_data)
9227 goto err_free_env;
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08009228 for (i = 0; i < len; i++)
9229 env->insn_aux_data[i].orig_idx = i;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07009230 env->prog = *prog;
Jakub Kicinski00176a32017-10-16 16:40:54 -07009231 env->ops = bpf_verifier_ops[env->prog->type];
Alexei Starovoitov45a73c12019-04-19 07:44:55 -07009232 is_priv = capable(CAP_SYS_ADMIN);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07009233
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07009234 /* grab the mutex to protect few globals used by verifier */
Alexei Starovoitov45a73c12019-04-19 07:44:55 -07009235 if (!is_priv)
9236 mutex_lock(&bpf_verifier_lock);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07009237
9238 if (attr->log_level || attr->log_buf || attr->log_size) {
9239 /* user requested verbose verifier output
9240 * and supplied buffer to store the verification trace
9241 */
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07009242 log->level = attr->log_level;
9243 log->ubuf = (char __user *) (unsigned long) attr->log_buf;
9244 log->len_total = attr->log_size;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07009245
9246 ret = -EINVAL;
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07009247 /* log attributes have to be sane */
Alexei Starovoitov7a9f5c62019-04-01 21:27:46 -07009248 if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 ||
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07009249 !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01009250 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07009251 }
Daniel Borkmann1ad2f582017-05-25 01:05:05 +02009252
9253 env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT);
9254 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
David S. Millere07b98d2017-05-10 11:38:07 -07009255 env->strict_alignment = true;
David Millere9ee9ef2018-11-30 21:08:14 -08009256 if (attr->prog_flags & BPF_F_ANY_ALIGNMENT)
9257 env->strict_alignment = false;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07009258
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08009259 env->allow_ptr_leaks = is_priv;
9260
Alexei Starovoitov0246e642014-09-26 00:17:04 -07009261 ret = replace_map_fd_with_map_ptr(env);
9262 if (ret < 0)
9263 goto skip_full_check;
9264
Jakub Kicinskif4e3ec02018-05-03 18:37:11 -07009265 if (bpf_prog_is_dev_bound(env->prog->aux)) {
Quentin Monneta40a2632018-11-09 13:03:31 +00009266 ret = bpf_prog_offload_verifier_prep(env->prog);
Jakub Kicinskif4e3ec02018-05-03 18:37:11 -07009267 if (ret)
9268 goto skip_full_check;
9269 }
9270
Alexei Starovoitovdc2a4eb2019-05-21 20:17:07 -07009271 env->explored_states = kvcalloc(state_htab_size(env),
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01009272 sizeof(struct bpf_verifier_state_list *),
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07009273 GFP_USER);
9274 ret = -ENOMEM;
9275 if (!env->explored_states)
9276 goto skip_full_check;
9277
Martin KaFai Laud9762e82018-12-13 10:41:48 -08009278 ret = check_subprogs(env);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07009279 if (ret < 0)
9280 goto skip_full_check;
9281
Martin KaFai Lauc454a462018-12-07 16:42:25 -08009282 ret = check_btf_info(env, attr, uattr);
Yonghong Song838e9692018-11-19 15:29:11 -08009283 if (ret < 0)
9284 goto skip_full_check;
9285
Martin KaFai Laud9762e82018-12-13 10:41:48 -08009286 ret = check_cfg(env);
9287 if (ret < 0)
9288 goto skip_full_check;
9289
Alexei Starovoitov17a52672014-09-26 00:17:06 -07009290 ret = do_check(env);
Craig Gallek8c01c4f2017-11-02 11:18:01 -04009291 if (env->cur_state) {
9292 free_verifier_state(env->cur_state, true);
9293 env->cur_state = NULL;
9294 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07009295
Quentin Monnetc941ce92018-10-07 12:56:47 +01009296 if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux))
9297 ret = bpf_prog_offload_finalize(env);
9298
Alexei Starovoitov0246e642014-09-26 00:17:04 -07009299skip_full_check:
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07009300 while (!pop_stack(env, NULL, NULL));
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07009301 free_states(env);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07009302
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07009303 if (ret == 0)
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08009304 ret = check_max_stack_depth(env);
9305
Jakub Kicinski9b38c402018-12-19 22:13:06 -08009306 /* instruction rewrites happen after this point */
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08009307 if (is_priv) {
9308 if (ret == 0)
9309 opt_hard_wire_dead_code_branches(env);
Jakub Kicinski52875a02019-01-22 22:45:20 -08009310 if (ret == 0)
9311 ret = opt_remove_dead_code(env);
Jakub Kicinskia1b14ab2019-01-22 22:45:21 -08009312 if (ret == 0)
9313 ret = opt_remove_nops(env);
Jakub Kicinski52875a02019-01-22 22:45:20 -08009314 } else {
9315 if (ret == 0)
9316 sanitize_dead_code(env);
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08009317 }
9318
Jakub Kicinski9b38c402018-12-19 22:13:06 -08009319 if (ret == 0)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07009320 /* program is valid, convert *(u32*)(ctx + off) accesses */
9321 ret = convert_ctx_accesses(env);
9322
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07009323 if (ret == 0)
Alexei Starovoitov79741b32017-03-15 18:26:40 -07009324 ret = fixup_bpf_calls(env);
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07009325
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01009326 /* do 32-bit optimization after insn patching has done so those patched
9327 * insns could be handled correctly.
9328 */
Jiong Wangd6c23082019-05-24 23:25:18 +01009329 if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) {
9330 ret = opt_subreg_zext_lo32_rnd_hi32(env, attr);
9331 env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret
9332 : false;
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01009333 }
9334
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08009335 if (ret == 0)
9336 ret = fixup_call_args(env);
9337
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07009338 env->verification_time = ktime_get_ns() - start_time;
9339 print_verification_stats(env);
9340
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07009341 if (log->level && bpf_verifier_log_full(log))
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07009342 ret = -ENOSPC;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07009343 if (log->level && !log->ubuf) {
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07009344 ret = -EFAULT;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07009345 goto err_release_maps;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07009346 }
9347
Alexei Starovoitov0246e642014-09-26 00:17:04 -07009348 if (ret == 0 && env->used_map_cnt) {
9349 /* if program passed verifier, update used_maps in bpf_prog_info */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07009350 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
9351 sizeof(env->used_maps[0]),
9352 GFP_KERNEL);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07009353
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07009354 if (!env->prog->aux->used_maps) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07009355 ret = -ENOMEM;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07009356 goto err_release_maps;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07009357 }
9358
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07009359 memcpy(env->prog->aux->used_maps, env->used_maps,
Alexei Starovoitov0246e642014-09-26 00:17:04 -07009360 sizeof(env->used_maps[0]) * env->used_map_cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07009361 env->prog->aux->used_map_cnt = env->used_map_cnt;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07009362
9363 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
9364 * bpf_ld_imm64 instructions
9365 */
9366 convert_pseudo_ld_imm64(env);
9367 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07009368
Yonghong Songba64e7d2018-11-24 23:20:44 -08009369 if (ret == 0)
9370 adjust_btf_func(env);
9371
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07009372err_release_maps:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07009373 if (!env->prog->aux->used_maps)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07009374 /* if we didn't copy map pointers into bpf_prog_info, release
Jakub Kicinskiab7f5bf2018-05-03 18:37:17 -07009375 * them now. Otherwise free_used_maps() will release them.
Alexei Starovoitov0246e642014-09-26 00:17:04 -07009376 */
9377 release_maps(env);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07009378 *prog = env->prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01009379err_unlock:
Alexei Starovoitov45a73c12019-04-19 07:44:55 -07009380 if (!is_priv)
9381 mutex_unlock(&bpf_verifier_lock);
Jakub Kicinski3df126f2016-09-21 11:43:56 +01009382 vfree(env->insn_aux_data);
9383err_free_env:
9384 kfree(env);
Alexei Starovoitov51580e72014-09-26 00:17:02 -07009385 return ret;
9386}