blob: 423f242a5efb19b6ede76942ce2cfd9ace06de0d [file] [log] [blame]
Alexei Starovoitov51580e72014-09-26 00:17:02 -07001/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002 * Copyright (c) 2016 Facebook
Joe Stringerfd978bf72018-10-02 13:35:35 -07003 * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io
Alexei Starovoitov51580e72014-09-26 00:17:02 -07004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 */
Yonghong Song838e9692018-11-19 15:29:11 -080014#include <uapi/linux/btf.h>
Alexei Starovoitov51580e72014-09-26 00:17:02 -070015#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/bpf.h>
Yonghong Song838e9692018-11-19 15:29:11 -080019#include <linux/btf.h>
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +010020#include <linux/bpf_verifier.h>
Alexei Starovoitov51580e72014-09-26 00:17:02 -070021#include <linux/filter.h>
22#include <net/netlink.h>
23#include <linux/file.h>
24#include <linux/vmalloc.h>
Thomas Grafebb676d2016-10-27 11:23:51 +020025#include <linux/stringify.h>
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -080026#include <linux/bsearch.h>
27#include <linux/sort.h>
Yonghong Songc195651e2018-04-28 22:28:08 -070028#include <linux/perf_event.h>
Martin KaFai Laud9762e82018-12-13 10:41:48 -080029#include <linux/ctype.h>
Alexei Starovoitov51580e72014-09-26 00:17:02 -070030
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -070031#include "disasm.h"
32
Jakub Kicinski00176a32017-10-16 16:40:54 -070033static const struct bpf_verifier_ops * const bpf_verifier_ops[] = {
34#define BPF_PROG_TYPE(_id, _name) \
35 [_id] = & _name ## _verifier_ops,
36#define BPF_MAP_TYPE(_id, _ops)
37#include <linux/bpf_types.h>
38#undef BPF_PROG_TYPE
39#undef BPF_MAP_TYPE
40};
41
Alexei Starovoitov51580e72014-09-26 00:17:02 -070042/* bpf_check() is a static code analyzer that walks eBPF program
43 * instruction by instruction and updates register/stack state.
44 * All paths of conditional branches are analyzed until 'bpf_exit' insn.
45 *
46 * The first pass is depth-first-search to check that the program is a DAG.
47 * It rejects the following programs:
48 * - larger than BPF_MAXINSNS insns
49 * - if loop is present (detected via back-edge)
50 * - unreachable insns exist (shouldn't be a forest. program = one function)
51 * - out of bounds or malformed jumps
52 * The second pass is all possible path descent from the 1st insn.
53 * Since it's analyzing all pathes through the program, the length of the
Gary Lineba38a92017-03-01 16:25:51 +080054 * analysis is limited to 64k insn, which may be hit even if total number of
Alexei Starovoitov51580e72014-09-26 00:17:02 -070055 * insn is less then 4K, but there are too many branches that change stack/regs.
56 * Number of 'branches to be analyzed' is limited to 1k
57 *
58 * On entry to each instruction, each register has a type, and the instruction
59 * changes the types of the registers depending on instruction semantics.
60 * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is
61 * copied to R1.
62 *
63 * All registers are 64-bit.
64 * R0 - return register
65 * R1-R5 argument passing registers
66 * R6-R9 callee saved registers
67 * R10 - frame pointer read-only
68 *
69 * At the start of BPF program the register R1 contains a pointer to bpf_context
70 * and has type PTR_TO_CTX.
71 *
72 * Verifier tracks arithmetic operations on pointers in case:
73 * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
74 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20),
75 * 1st insn copies R10 (which has FRAME_PTR) type into R1
76 * and 2nd arithmetic instruction is pattern matched to recognize
77 * that it wants to construct a pointer to some element within stack.
78 * So after 2nd insn, the register R1 has type PTR_TO_STACK
79 * (and -20 constant is saved for further stack bounds checking).
80 * Meaning that this reg is a pointer to stack plus known immediate constant.
81 *
Edward Creef1174f72017-08-07 15:26:19 +010082 * Most of the time the registers have SCALAR_VALUE type, which
Alexei Starovoitov51580e72014-09-26 00:17:02 -070083 * means the register has some value, but it's not a valid pointer.
Edward Creef1174f72017-08-07 15:26:19 +010084 * (like pointer plus pointer becomes SCALAR_VALUE type)
Alexei Starovoitov51580e72014-09-26 00:17:02 -070085 *
86 * When verifier sees load or store instructions the type of base register
Joe Stringerc64b7982018-10-02 13:35:33 -070087 * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are
88 * four pointer types recognized by check_mem_access() function.
Alexei Starovoitov51580e72014-09-26 00:17:02 -070089 *
90 * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value'
91 * and the range of [ptr, ptr + map's value_size) is accessible.
92 *
93 * registers used to pass values to function calls are checked against
94 * function argument constraints.
95 *
96 * ARG_PTR_TO_MAP_KEY is one of such argument constraints.
97 * It means that the register type passed to this function must be
98 * PTR_TO_STACK and it will be used inside the function as
99 * 'pointer to map element key'
100 *
101 * For example the argument constraints for bpf_map_lookup_elem():
102 * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
103 * .arg1_type = ARG_CONST_MAP_PTR,
104 * .arg2_type = ARG_PTR_TO_MAP_KEY,
105 *
106 * ret_type says that this function returns 'pointer to map elem value or null'
107 * function expects 1st argument to be a const pointer to 'struct bpf_map' and
108 * 2nd argument should be a pointer to stack, which will be used inside
109 * the helper function as a pointer to map element key.
110 *
111 * On the kernel side the helper function looks like:
112 * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
113 * {
114 * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
115 * void *key = (void *) (unsigned long) r2;
116 * void *value;
117 *
118 * here kernel can access 'key' and 'map' pointers safely, knowing that
119 * [key, key + map->key_size) bytes are valid and were initialized on
120 * the stack of eBPF program.
121 * }
122 *
123 * Corresponding eBPF program may look like:
124 * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR
125 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK
126 * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP
127 * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
128 * here verifier looks at prototype of map_lookup_elem() and sees:
129 * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok,
130 * Now verifier knows that this map has key of R1->map_ptr->key_size bytes
131 *
132 * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far,
133 * Now verifier checks that [R2, R2 + map's key_size) are within stack limits
134 * and were initialized prior to this call.
135 * If it's ok, then verifier allows this BPF_CALL insn and looks at
136 * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets
137 * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function
138 * returns ether pointer to map value or NULL.
139 *
140 * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off'
141 * insn, the register holding that pointer in the true branch changes state to
142 * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false
143 * branch. See check_cond_jmp_op().
144 *
145 * After the call R0 is set to return type of the function and registers R1-R5
146 * are set to NOT_INIT to indicate that they are no longer readable.
Joe Stringerfd978bf72018-10-02 13:35:35 -0700147 *
148 * The following reference types represent a potential reference to a kernel
149 * resource which, after first being allocated, must be checked and freed by
150 * the BPF program:
151 * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET
152 *
153 * When the verifier sees a helper call return a reference type, it allocates a
154 * pointer id for the reference and stores it in the current function state.
155 * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into
156 * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type
157 * passes through a NULL-check conditional. For the branch wherein the state is
158 * changed to CONST_IMM, the verifier releases the reference.
Joe Stringer6acc9b42018-10-02 13:35:36 -0700159 *
160 * For each helper function that allocates a reference, such as
161 * bpf_sk_lookup_tcp(), there is a corresponding release function, such as
162 * bpf_sk_release(). When a reference type passes into the release function,
163 * the verifier also releases the reference. If any unchecked or unreleased
164 * reference remains at the end of the program, the verifier rejects it.
Alexei Starovoitov51580e72014-09-26 00:17:02 -0700165 */
166
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700167/* verifier_state + insn_idx are pushed to stack when branch is encountered */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100168struct bpf_verifier_stack_elem {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700169 /* verifer state is 'st'
170 * before processing instruction 'insn_idx'
171 * and after processing instruction 'prev_insn_idx'
172 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100173 struct bpf_verifier_state st;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700174 int insn_idx;
175 int prev_insn_idx;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100176 struct bpf_verifier_stack_elem *next;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700177};
178
Daniel Borkmann07016152016-04-05 22:33:17 +0200179#define BPF_COMPLEXITY_LIMIT_STACK 1024
Alexei Starovoitovceefbc92018-12-03 22:46:06 -0800180#define BPF_COMPLEXITY_LIMIT_STATES 64
Daniel Borkmann07016152016-04-05 22:33:17 +0200181
Daniel Borkmannc93552c2018-05-24 02:32:53 +0200182#define BPF_MAP_PTR_UNPRIV 1UL
183#define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \
184 POISON_POINTER_DELTA))
185#define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV))
186
187static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux)
188{
189 return BPF_MAP_PTR(aux->map_state) == BPF_MAP_PTR_POISON;
190}
191
192static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux)
193{
194 return aux->map_state & BPF_MAP_PTR_UNPRIV;
195}
196
197static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux,
198 const struct bpf_map *map, bool unpriv)
199{
200 BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV);
201 unpriv |= bpf_map_ptr_unpriv(aux);
202 aux->map_state = (unsigned long)map |
203 (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL);
204}
Martin KaFai Laufad73a12017-03-22 10:00:32 -0700205
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200206struct bpf_call_arg_meta {
207 struct bpf_map *map_ptr;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200208 bool raw_mode;
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200209 bool pkt_access;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200210 int regno;
211 int access_size;
Yonghong Song849fa502018-04-28 22:28:09 -0700212 s64 msize_smax_value;
213 u64 msize_umax_value;
Martin KaFai Lau1b986582019-03-12 10:23:02 -0700214 int ref_obj_id;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800215 int func_id;
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200216};
217
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700218static DEFINE_MUTEX(bpf_verifier_lock);
219
Martin KaFai Laud9762e82018-12-13 10:41:48 -0800220static const struct bpf_line_info *
221find_linfo(const struct bpf_verifier_env *env, u32 insn_off)
222{
223 const struct bpf_line_info *linfo;
224 const struct bpf_prog *prog;
225 u32 i, nr_linfo;
226
227 prog = env->prog;
228 nr_linfo = prog->aux->nr_linfo;
229
230 if (!nr_linfo || insn_off >= prog->len)
231 return NULL;
232
233 linfo = prog->aux->linfo;
234 for (i = 1; i < nr_linfo; i++)
235 if (insn_off < linfo[i].insn_off)
236 break;
237
238 return &linfo[i - 1];
239}
240
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700241void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt,
242 va_list args)
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700243{
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700244 unsigned int n;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700245
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700246 n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args);
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700247
248 WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1,
249 "verifier log line truncated - local buffer too short\n");
250
251 n = min(log->len_total - log->len_used - 1, n);
252 log->kbuf[n] = '\0';
253
254 if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1))
255 log->len_used += n;
256 else
257 log->ubuf = NULL;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700258}
Jiri Olsaabe08842018-03-23 11:41:28 +0100259
260/* log_level controls verbosity level of eBPF verifier.
261 * bpf_verifier_log_write() is used to dump the verification trace to the log,
262 * so the user can figure out what's wrong with the program
Quentin Monnet430e68d2018-01-10 12:26:06 +0000263 */
Jiri Olsaabe08842018-03-23 11:41:28 +0100264__printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env,
265 const char *fmt, ...)
266{
267 va_list args;
268
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700269 if (!bpf_verifier_log_needed(&env->log))
270 return;
271
Jiri Olsaabe08842018-03-23 11:41:28 +0100272 va_start(args, fmt);
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700273 bpf_verifier_vlog(&env->log, fmt, args);
Jiri Olsaabe08842018-03-23 11:41:28 +0100274 va_end(args);
275}
276EXPORT_SYMBOL_GPL(bpf_verifier_log_write);
277
278__printf(2, 3) static void verbose(void *private_data, const char *fmt, ...)
279{
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700280 struct bpf_verifier_env *env = private_data;
Jiri Olsaabe08842018-03-23 11:41:28 +0100281 va_list args;
282
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700283 if (!bpf_verifier_log_needed(&env->log))
284 return;
285
Jiri Olsaabe08842018-03-23 11:41:28 +0100286 va_start(args, fmt);
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700287 bpf_verifier_vlog(&env->log, fmt, args);
Jiri Olsaabe08842018-03-23 11:41:28 +0100288 va_end(args);
289}
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700290
Martin KaFai Laud9762e82018-12-13 10:41:48 -0800291static const char *ltrim(const char *s)
292{
293 while (isspace(*s))
294 s++;
295
296 return s;
297}
298
299__printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env,
300 u32 insn_off,
301 const char *prefix_fmt, ...)
302{
303 const struct bpf_line_info *linfo;
304
305 if (!bpf_verifier_log_needed(&env->log))
306 return;
307
308 linfo = find_linfo(env, insn_off);
309 if (!linfo || linfo == env->prev_linfo)
310 return;
311
312 if (prefix_fmt) {
313 va_list args;
314
315 va_start(args, prefix_fmt);
316 bpf_verifier_vlog(&env->log, prefix_fmt, args);
317 va_end(args);
318 }
319
320 verbose(env, "%s\n",
321 ltrim(btf_name_by_offset(env->prog->aux->btf,
322 linfo->line_off)));
323
324 env->prev_linfo = linfo;
325}
326
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200327static bool type_is_pkt_pointer(enum bpf_reg_type type)
328{
329 return type == PTR_TO_PACKET ||
330 type == PTR_TO_PACKET_META;
331}
332
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800333static bool type_is_sk_pointer(enum bpf_reg_type type)
334{
335 return type == PTR_TO_SOCKET ||
Martin KaFai Lau655a51e2019-02-09 23:22:24 -0800336 type == PTR_TO_SOCK_COMMON ||
337 type == PTR_TO_TCP_SOCK;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800338}
339
Joe Stringer840b9612018-10-02 13:35:32 -0700340static bool reg_type_may_be_null(enum bpf_reg_type type)
341{
Joe Stringerfd978bf72018-10-02 13:35:35 -0700342 return type == PTR_TO_MAP_VALUE_OR_NULL ||
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800343 type == PTR_TO_SOCKET_OR_NULL ||
Martin KaFai Lau655a51e2019-02-09 23:22:24 -0800344 type == PTR_TO_SOCK_COMMON_OR_NULL ||
345 type == PTR_TO_TCP_SOCK_OR_NULL;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700346}
347
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800348static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg)
349{
350 return reg->type == PTR_TO_MAP_VALUE &&
351 map_value_has_spin_lock(reg->map_ptr);
352}
353
Martin KaFai Laucba368c2019-03-18 10:37:13 -0700354static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type)
355{
356 return type == PTR_TO_SOCKET ||
357 type == PTR_TO_SOCKET_OR_NULL ||
358 type == PTR_TO_TCP_SOCK ||
359 type == PTR_TO_TCP_SOCK_OR_NULL;
360}
361
Martin KaFai Lau1b986582019-03-12 10:23:02 -0700362static bool arg_type_may_be_refcounted(enum bpf_arg_type type)
Joe Stringerfd978bf72018-10-02 13:35:35 -0700363{
Martin KaFai Lau1b986582019-03-12 10:23:02 -0700364 return type == ARG_PTR_TO_SOCK_COMMON;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700365}
366
367/* Determine whether the function releases some resources allocated by another
368 * function call. The first reference type argument will be assumed to be
369 * released by release_reference().
370 */
371static bool is_release_function(enum bpf_func_id func_id)
372{
Joe Stringer6acc9b42018-10-02 13:35:36 -0700373 return func_id == BPF_FUNC_sk_release;
Joe Stringer840b9612018-10-02 13:35:32 -0700374}
375
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800376static bool is_acquire_function(enum bpf_func_id func_id)
377{
378 return func_id == BPF_FUNC_sk_lookup_tcp ||
Lorenz Baueredbf8c02019-03-22 09:54:01 +0800379 func_id == BPF_FUNC_sk_lookup_udp ||
380 func_id == BPF_FUNC_skc_lookup_tcp;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800381}
382
Martin KaFai Lau1b986582019-03-12 10:23:02 -0700383static bool is_ptr_cast_function(enum bpf_func_id func_id)
384{
385 return func_id == BPF_FUNC_tcp_sock ||
386 func_id == BPF_FUNC_sk_fullsock;
387}
388
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700389/* string representation of 'enum bpf_reg_type' */
390static const char * const reg_type_str[] = {
391 [NOT_INIT] = "?",
Edward Creef1174f72017-08-07 15:26:19 +0100392 [SCALAR_VALUE] = "inv",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700393 [PTR_TO_CTX] = "ctx",
394 [CONST_PTR_TO_MAP] = "map_ptr",
395 [PTR_TO_MAP_VALUE] = "map_value",
396 [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700397 [PTR_TO_STACK] = "fp",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700398 [PTR_TO_PACKET] = "pkt",
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200399 [PTR_TO_PACKET_META] = "pkt_meta",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700400 [PTR_TO_PACKET_END] = "pkt_end",
Petar Penkovd58e4682018-09-14 07:46:18 -0700401 [PTR_TO_FLOW_KEYS] = "flow_keys",
Joe Stringerc64b7982018-10-02 13:35:33 -0700402 [PTR_TO_SOCKET] = "sock",
403 [PTR_TO_SOCKET_OR_NULL] = "sock_or_null",
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800404 [PTR_TO_SOCK_COMMON] = "sock_common",
405 [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null",
Martin KaFai Lau655a51e2019-02-09 23:22:24 -0800406 [PTR_TO_TCP_SOCK] = "tcp_sock",
407 [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700408};
409
Edward Cree8efea212018-08-22 20:02:44 +0100410static char slot_type_char[] = {
411 [STACK_INVALID] = '?',
412 [STACK_SPILL] = 'r',
413 [STACK_MISC] = 'm',
414 [STACK_ZERO] = '0',
415};
416
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800417static void print_liveness(struct bpf_verifier_env *env,
418 enum bpf_reg_liveness live)
419{
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -0800420 if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE))
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800421 verbose(env, "_");
422 if (live & REG_LIVE_READ)
423 verbose(env, "r");
424 if (live & REG_LIVE_WRITTEN)
425 verbose(env, "w");
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -0800426 if (live & REG_LIVE_DONE)
427 verbose(env, "D");
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800428}
429
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800430static struct bpf_func_state *func(struct bpf_verifier_env *env,
431 const struct bpf_reg_state *reg)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700432{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800433 struct bpf_verifier_state *cur = env->cur_state;
434
435 return cur->frame[reg->frameno];
436}
437
438static void print_verifier_state(struct bpf_verifier_env *env,
439 const struct bpf_func_state *state)
440{
441 const struct bpf_reg_state *reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700442 enum bpf_reg_type t;
443 int i;
444
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800445 if (state->frameno)
446 verbose(env, " frame%d:", state->frameno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700447 for (i = 0; i < MAX_BPF_REG; i++) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700448 reg = &state->regs[i];
449 t = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700450 if (t == NOT_INIT)
451 continue;
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800452 verbose(env, " R%d", i);
453 print_liveness(env, reg->live);
454 verbose(env, "=%s", reg_type_str[t]);
Edward Creef1174f72017-08-07 15:26:19 +0100455 if ((t == SCALAR_VALUE || t == PTR_TO_STACK) &&
456 tnum_is_const(reg->var_off)) {
457 /* reg->off should be 0 for SCALAR_VALUE */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700458 verbose(env, "%lld", reg->var_off.value + reg->off);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800459 if (t == PTR_TO_STACK)
460 verbose(env, ",call_%d", func(env, reg)->callsite);
Edward Creef1174f72017-08-07 15:26:19 +0100461 } else {
Martin KaFai Laucba368c2019-03-18 10:37:13 -0700462 verbose(env, "(id=%d", reg->id);
463 if (reg_type_may_be_refcounted_or_null(t))
464 verbose(env, ",ref_obj_id=%d", reg->ref_obj_id);
Edward Creef1174f72017-08-07 15:26:19 +0100465 if (t != SCALAR_VALUE)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700466 verbose(env, ",off=%d", reg->off);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200467 if (type_is_pkt_pointer(t))
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700468 verbose(env, ",r=%d", reg->range);
Edward Creef1174f72017-08-07 15:26:19 +0100469 else if (t == CONST_PTR_TO_MAP ||
470 t == PTR_TO_MAP_VALUE ||
471 t == PTR_TO_MAP_VALUE_OR_NULL)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700472 verbose(env, ",ks=%d,vs=%d",
Edward Creef1174f72017-08-07 15:26:19 +0100473 reg->map_ptr->key_size,
474 reg->map_ptr->value_size);
Edward Cree7d1238f2017-08-07 15:26:56 +0100475 if (tnum_is_const(reg->var_off)) {
476 /* Typically an immediate SCALAR_VALUE, but
477 * could be a pointer whose offset is too big
478 * for reg->off
479 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700480 verbose(env, ",imm=%llx", reg->var_off.value);
Edward Cree7d1238f2017-08-07 15:26:56 +0100481 } else {
482 if (reg->smin_value != reg->umin_value &&
483 reg->smin_value != S64_MIN)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700484 verbose(env, ",smin_value=%lld",
Edward Cree7d1238f2017-08-07 15:26:56 +0100485 (long long)reg->smin_value);
486 if (reg->smax_value != reg->umax_value &&
487 reg->smax_value != S64_MAX)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700488 verbose(env, ",smax_value=%lld",
Edward Cree7d1238f2017-08-07 15:26:56 +0100489 (long long)reg->smax_value);
490 if (reg->umin_value != 0)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700491 verbose(env, ",umin_value=%llu",
Edward Cree7d1238f2017-08-07 15:26:56 +0100492 (unsigned long long)reg->umin_value);
493 if (reg->umax_value != U64_MAX)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700494 verbose(env, ",umax_value=%llu",
Edward Cree7d1238f2017-08-07 15:26:56 +0100495 (unsigned long long)reg->umax_value);
496 if (!tnum_is_unknown(reg->var_off)) {
497 char tn_buf[48];
Edward Creef1174f72017-08-07 15:26:19 +0100498
Edward Cree7d1238f2017-08-07 15:26:56 +0100499 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700500 verbose(env, ",var_off=%s", tn_buf);
Edward Cree7d1238f2017-08-07 15:26:56 +0100501 }
Edward Creef1174f72017-08-07 15:26:19 +0100502 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700503 verbose(env, ")");
Edward Creef1174f72017-08-07 15:26:19 +0100504 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700505 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700506 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
Edward Cree8efea212018-08-22 20:02:44 +0100507 char types_buf[BPF_REG_SIZE + 1];
508 bool valid = false;
509 int j;
510
511 for (j = 0; j < BPF_REG_SIZE; j++) {
512 if (state->stack[i].slot_type[j] != STACK_INVALID)
513 valid = true;
514 types_buf[j] = slot_type_char[
515 state->stack[i].slot_type[j]];
516 }
517 types_buf[BPF_REG_SIZE] = 0;
518 if (!valid)
519 continue;
520 verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE);
521 print_liveness(env, state->stack[i].spilled_ptr.live);
522 if (state->stack[i].slot_type[0] == STACK_SPILL)
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800523 verbose(env, "=%s",
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700524 reg_type_str[state->stack[i].spilled_ptr.type]);
Edward Cree8efea212018-08-22 20:02:44 +0100525 else
526 verbose(env, "=%s", types_buf);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700527 }
Joe Stringerfd978bf72018-10-02 13:35:35 -0700528 if (state->acquired_refs && state->refs[0].id) {
529 verbose(env, " refs=%d", state->refs[0].id);
530 for (i = 1; i < state->acquired_refs; i++)
531 if (state->refs[i].id)
532 verbose(env, ",%d", state->refs[i].id);
533 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700534 verbose(env, "\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700535}
536
Joe Stringer84dbf352018-10-02 13:35:34 -0700537#define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \
538static int copy_##NAME##_state(struct bpf_func_state *dst, \
539 const struct bpf_func_state *src) \
540{ \
541 if (!src->FIELD) \
542 return 0; \
543 if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \
544 /* internal bug, make state invalid to reject the program */ \
545 memset(dst, 0, sizeof(*dst)); \
546 return -EFAULT; \
547 } \
548 memcpy(dst->FIELD, src->FIELD, \
549 sizeof(*src->FIELD) * (src->COUNT / SIZE)); \
550 return 0; \
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700551}
Joe Stringerfd978bf72018-10-02 13:35:35 -0700552/* copy_reference_state() */
553COPY_STATE_FN(reference, acquired_refs, refs, 1)
Joe Stringer84dbf352018-10-02 13:35:34 -0700554/* copy_stack_state() */
555COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE)
556#undef COPY_STATE_FN
557
558#define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \
559static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \
560 bool copy_old) \
561{ \
562 u32 old_size = state->COUNT; \
563 struct bpf_##NAME##_state *new_##FIELD; \
564 int slot = size / SIZE; \
565 \
566 if (size <= old_size || !size) { \
567 if (copy_old) \
568 return 0; \
569 state->COUNT = slot * SIZE; \
570 if (!size && old_size) { \
571 kfree(state->FIELD); \
572 state->FIELD = NULL; \
573 } \
574 return 0; \
575 } \
576 new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \
577 GFP_KERNEL); \
578 if (!new_##FIELD) \
579 return -ENOMEM; \
580 if (copy_old) { \
581 if (state->FIELD) \
582 memcpy(new_##FIELD, state->FIELD, \
583 sizeof(*new_##FIELD) * (old_size / SIZE)); \
584 memset(new_##FIELD + old_size / SIZE, 0, \
585 sizeof(*new_##FIELD) * (size - old_size) / SIZE); \
586 } \
587 state->COUNT = slot * SIZE; \
588 kfree(state->FIELD); \
589 state->FIELD = new_##FIELD; \
590 return 0; \
591}
Joe Stringerfd978bf72018-10-02 13:35:35 -0700592/* realloc_reference_state() */
593REALLOC_STATE_FN(reference, acquired_refs, refs, 1)
Joe Stringer84dbf352018-10-02 13:35:34 -0700594/* realloc_stack_state() */
595REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE)
596#undef REALLOC_STATE_FN
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700597
598/* do_check() starts with zero-sized stack in struct bpf_verifier_state to
599 * make it consume minimal amount of memory. check_stack_write() access from
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800600 * the program calls into realloc_func_state() to grow the stack size.
Joe Stringer84dbf352018-10-02 13:35:34 -0700601 * Note there is a non-zero 'parent' pointer inside bpf_verifier_state
602 * which realloc_stack_state() copies over. It points to previous
603 * bpf_verifier_state which is never reallocated.
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700604 */
Joe Stringerfd978bf72018-10-02 13:35:35 -0700605static int realloc_func_state(struct bpf_func_state *state, int stack_size,
606 int refs_size, bool copy_old)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700607{
Joe Stringerfd978bf72018-10-02 13:35:35 -0700608 int err = realloc_reference_state(state, refs_size, copy_old);
609 if (err)
610 return err;
611 return realloc_stack_state(state, stack_size, copy_old);
612}
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700613
Joe Stringerfd978bf72018-10-02 13:35:35 -0700614/* Acquire a pointer id from the env and update the state->refs to include
615 * this new pointer reference.
616 * On success, returns a valid pointer id to associate with the register
617 * On failure, returns a negative errno.
618 */
619static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx)
620{
621 struct bpf_func_state *state = cur_func(env);
622 int new_ofs = state->acquired_refs;
623 int id, err;
624
625 err = realloc_reference_state(state, state->acquired_refs + 1, true);
626 if (err)
627 return err;
628 id = ++env->id_gen;
629 state->refs[new_ofs].id = id;
630 state->refs[new_ofs].insn_idx = insn_idx;
631
632 return id;
633}
634
635/* release function corresponding to acquire_reference_state(). Idempotent. */
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800636static int release_reference_state(struct bpf_func_state *state, int ptr_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -0700637{
638 int i, last_idx;
639
Joe Stringerfd978bf72018-10-02 13:35:35 -0700640 last_idx = state->acquired_refs - 1;
641 for (i = 0; i < state->acquired_refs; i++) {
642 if (state->refs[i].id == ptr_id) {
643 if (last_idx && i != last_idx)
644 memcpy(&state->refs[i], &state->refs[last_idx],
645 sizeof(*state->refs));
646 memset(&state->refs[last_idx], 0, sizeof(*state->refs));
647 state->acquired_refs--;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700648 return 0;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700649 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700650 }
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800651 return -EINVAL;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700652}
653
654static int transfer_reference_state(struct bpf_func_state *dst,
655 struct bpf_func_state *src)
656{
657 int err = realloc_reference_state(dst, src->acquired_refs, false);
658 if (err)
659 return err;
660 err = copy_reference_state(dst, src);
661 if (err)
662 return err;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700663 return 0;
664}
665
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800666static void free_func_state(struct bpf_func_state *state)
667{
Alexei Starovoitov58963512018-01-08 07:51:17 -0800668 if (!state)
669 return;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700670 kfree(state->refs);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800671 kfree(state->stack);
672 kfree(state);
673}
674
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700675static void free_verifier_state(struct bpf_verifier_state *state,
676 bool free_self)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700677{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800678 int i;
679
680 for (i = 0; i <= state->curframe; i++) {
681 free_func_state(state->frame[i]);
682 state->frame[i] = NULL;
683 }
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700684 if (free_self)
685 kfree(state);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700686}
687
688/* copy verifier state from src to dst growing dst stack space
689 * when necessary to accommodate larger src stack
690 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800691static int copy_func_state(struct bpf_func_state *dst,
692 const struct bpf_func_state *src)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700693{
694 int err;
695
Joe Stringerfd978bf72018-10-02 13:35:35 -0700696 err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs,
697 false);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700698 if (err)
699 return err;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700700 memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs));
701 err = copy_reference_state(dst, src);
702 if (err)
703 return err;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700704 return copy_stack_state(dst, src);
705}
706
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800707static int copy_verifier_state(struct bpf_verifier_state *dst_state,
708 const struct bpf_verifier_state *src)
709{
710 struct bpf_func_state *dst;
711 int i, err;
712
713 /* if dst has more stack frames then src frame, free them */
714 for (i = src->curframe + 1; i <= dst_state->curframe; i++) {
715 free_func_state(dst_state->frame[i]);
716 dst_state->frame[i] = NULL;
717 }
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100718 dst_state->speculative = src->speculative;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800719 dst_state->curframe = src->curframe;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800720 dst_state->active_spin_lock = src->active_spin_lock;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800721 for (i = 0; i <= src->curframe; i++) {
722 dst = dst_state->frame[i];
723 if (!dst) {
724 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
725 if (!dst)
726 return -ENOMEM;
727 dst_state->frame[i] = dst;
728 }
729 err = copy_func_state(dst, src->frame[i]);
730 if (err)
731 return err;
732 }
733 return 0;
734}
735
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700736static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx,
737 int *insn_idx)
738{
739 struct bpf_verifier_state *cur = env->cur_state;
740 struct bpf_verifier_stack_elem *elem, *head = env->head;
741 int err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700742
743 if (env->head == NULL)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700744 return -ENOENT;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700745
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700746 if (cur) {
747 err = copy_verifier_state(cur, &head->st);
748 if (err)
749 return err;
750 }
751 if (insn_idx)
752 *insn_idx = head->insn_idx;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700753 if (prev_insn_idx)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700754 *prev_insn_idx = head->prev_insn_idx;
755 elem = head->next;
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700756 free_verifier_state(&head->st, false);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700757 kfree(head);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700758 env->head = elem;
759 env->stack_size--;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700760 return 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700761}
762
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100763static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100764 int insn_idx, int prev_insn_idx,
765 bool speculative)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700766{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700767 struct bpf_verifier_state *cur = env->cur_state;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100768 struct bpf_verifier_stack_elem *elem;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700769 int err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700770
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700771 elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700772 if (!elem)
773 goto err;
774
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700775 elem->insn_idx = insn_idx;
776 elem->prev_insn_idx = prev_insn_idx;
777 elem->next = env->head;
778 env->head = elem;
779 env->stack_size++;
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700780 err = copy_verifier_state(&elem->st, cur);
781 if (err)
782 goto err;
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100783 elem->st.speculative |= speculative;
Daniel Borkmann07016152016-04-05 22:33:17 +0200784 if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700785 verbose(env, "BPF program is too complex\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700786 goto err;
787 }
788 return &elem->st;
789err:
Alexei Starovoitov58963512018-01-08 07:51:17 -0800790 free_verifier_state(env->cur_state, true);
791 env->cur_state = NULL;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700792 /* pop all elements and return */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700793 while (!pop_stack(env, NULL, NULL));
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700794 return NULL;
795}
796
797#define CALLER_SAVED_REGS 6
798static const int caller_saved[CALLER_SAVED_REGS] = {
799 BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
800};
801
Edward Creef1174f72017-08-07 15:26:19 +0100802static void __mark_reg_not_init(struct bpf_reg_state *reg);
803
Edward Creeb03c9f92017-08-07 15:26:36 +0100804/* Mark the unknown part of a register (variable offset or scalar value) as
805 * known to have the value @imm.
806 */
807static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
808{
Alexei Starovoitova9c676b2018-09-04 19:13:44 -0700809 /* Clear id, off, and union(map_ptr, range) */
810 memset(((u8 *)reg) + sizeof(reg->type), 0,
811 offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type));
Edward Creeb03c9f92017-08-07 15:26:36 +0100812 reg->var_off = tnum_const(imm);
813 reg->smin_value = (s64)imm;
814 reg->smax_value = (s64)imm;
815 reg->umin_value = imm;
816 reg->umax_value = imm;
817}
818
Edward Creef1174f72017-08-07 15:26:19 +0100819/* Mark the 'variable offset' part of a register as zero. This should be
820 * used only on registers holding a pointer type.
821 */
822static void __mark_reg_known_zero(struct bpf_reg_state *reg)
823{
Edward Creeb03c9f92017-08-07 15:26:36 +0100824 __mark_reg_known(reg, 0);
Edward Creef1174f72017-08-07 15:26:19 +0100825}
826
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800827static void __mark_reg_const_zero(struct bpf_reg_state *reg)
828{
829 __mark_reg_known(reg, 0);
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800830 reg->type = SCALAR_VALUE;
831}
832
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700833static void mark_reg_known_zero(struct bpf_verifier_env *env,
834 struct bpf_reg_state *regs, u32 regno)
Edward Creef1174f72017-08-07 15:26:19 +0100835{
836 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700837 verbose(env, "mark_reg_known_zero(regs, %u)\n", regno);
Edward Creef1174f72017-08-07 15:26:19 +0100838 /* Something bad happened, let's kill all regs */
839 for (regno = 0; regno < MAX_BPF_REG; regno++)
840 __mark_reg_not_init(regs + regno);
841 return;
842 }
843 __mark_reg_known_zero(regs + regno);
844}
845
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200846static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg)
847{
848 return type_is_pkt_pointer(reg->type);
849}
850
851static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg)
852{
853 return reg_is_pkt_pointer(reg) ||
854 reg->type == PTR_TO_PACKET_END;
855}
856
857/* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */
858static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg,
859 enum bpf_reg_type which)
860{
861 /* The register can already have a range from prior markings.
862 * This is fine as long as it hasn't been advanced from its
863 * origin.
864 */
865 return reg->type == which &&
866 reg->id == 0 &&
867 reg->off == 0 &&
868 tnum_equals_const(reg->var_off, 0);
869}
870
Edward Creeb03c9f92017-08-07 15:26:36 +0100871/* Attempts to improve min/max values based on var_off information */
872static void __update_reg_bounds(struct bpf_reg_state *reg)
873{
874 /* min signed is max(sign bit) | min(other bits) */
875 reg->smin_value = max_t(s64, reg->smin_value,
876 reg->var_off.value | (reg->var_off.mask & S64_MIN));
877 /* max signed is min(sign bit) | max(other bits) */
878 reg->smax_value = min_t(s64, reg->smax_value,
879 reg->var_off.value | (reg->var_off.mask & S64_MAX));
880 reg->umin_value = max(reg->umin_value, reg->var_off.value);
881 reg->umax_value = min(reg->umax_value,
882 reg->var_off.value | reg->var_off.mask);
883}
884
885/* Uses signed min/max values to inform unsigned, and vice-versa */
886static void __reg_deduce_bounds(struct bpf_reg_state *reg)
887{
888 /* Learn sign from signed bounds.
889 * If we cannot cross the sign boundary, then signed and unsigned bounds
890 * are the same, so combine. This works even in the negative case, e.g.
891 * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff.
892 */
893 if (reg->smin_value >= 0 || reg->smax_value < 0) {
894 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
895 reg->umin_value);
896 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
897 reg->umax_value);
898 return;
899 }
900 /* Learn sign from unsigned bounds. Signed bounds cross the sign
901 * boundary, so we must be careful.
902 */
903 if ((s64)reg->umax_value >= 0) {
904 /* Positive. We can't learn anything from the smin, but smax
905 * is positive, hence safe.
906 */
907 reg->smin_value = reg->umin_value;
908 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
909 reg->umax_value);
910 } else if ((s64)reg->umin_value < 0) {
911 /* Negative. We can't learn anything from the smax, but smin
912 * is negative, hence safe.
913 */
914 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
915 reg->umin_value);
916 reg->smax_value = reg->umax_value;
917 }
918}
919
920/* Attempts to improve var_off based on unsigned min/max information */
921static void __reg_bound_offset(struct bpf_reg_state *reg)
922{
923 reg->var_off = tnum_intersect(reg->var_off,
924 tnum_range(reg->umin_value,
925 reg->umax_value));
926}
927
928/* Reset the min/max bounds of a register */
929static void __mark_reg_unbounded(struct bpf_reg_state *reg)
930{
931 reg->smin_value = S64_MIN;
932 reg->smax_value = S64_MAX;
933 reg->umin_value = 0;
934 reg->umax_value = U64_MAX;
935}
936
Edward Creef1174f72017-08-07 15:26:19 +0100937/* Mark a register as having a completely unknown (scalar) value. */
938static void __mark_reg_unknown(struct bpf_reg_state *reg)
939{
Alexei Starovoitova9c676b2018-09-04 19:13:44 -0700940 /*
941 * Clear type, id, off, and union(map_ptr, range) and
942 * padding between 'type' and union
943 */
944 memset(reg, 0, offsetof(struct bpf_reg_state, var_off));
Edward Creef1174f72017-08-07 15:26:19 +0100945 reg->type = SCALAR_VALUE;
Edward Creef1174f72017-08-07 15:26:19 +0100946 reg->var_off = tnum_unknown;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800947 reg->frameno = 0;
Edward Creeb03c9f92017-08-07 15:26:36 +0100948 __mark_reg_unbounded(reg);
Edward Creef1174f72017-08-07 15:26:19 +0100949}
950
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700951static void mark_reg_unknown(struct bpf_verifier_env *env,
952 struct bpf_reg_state *regs, u32 regno)
Edward Creef1174f72017-08-07 15:26:19 +0100953{
954 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700955 verbose(env, "mark_reg_unknown(regs, %u)\n", regno);
Alexei Starovoitov19ceb412017-11-30 21:31:37 -0800956 /* Something bad happened, let's kill all regs except FP */
957 for (regno = 0; regno < BPF_REG_FP; regno++)
Edward Creef1174f72017-08-07 15:26:19 +0100958 __mark_reg_not_init(regs + regno);
959 return;
960 }
961 __mark_reg_unknown(regs + regno);
962}
963
964static void __mark_reg_not_init(struct bpf_reg_state *reg)
965{
966 __mark_reg_unknown(reg);
967 reg->type = NOT_INIT;
968}
969
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700970static void mark_reg_not_init(struct bpf_verifier_env *env,
971 struct bpf_reg_state *regs, u32 regno)
Daniel Borkmanna9789ef2017-05-25 01:05:06 +0200972{
Edward Creef1174f72017-08-07 15:26:19 +0100973 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700974 verbose(env, "mark_reg_not_init(regs, %u)\n", regno);
Alexei Starovoitov19ceb412017-11-30 21:31:37 -0800975 /* Something bad happened, let's kill all regs except FP */
976 for (regno = 0; regno < BPF_REG_FP; regno++)
Edward Creef1174f72017-08-07 15:26:19 +0100977 __mark_reg_not_init(regs + regno);
978 return;
979 }
980 __mark_reg_not_init(regs + regno);
Daniel Borkmanna9789ef2017-05-25 01:05:06 +0200981}
982
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700983static void init_reg_state(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800984 struct bpf_func_state *state)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700985{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800986 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700987 int i;
988
Edward Creedc503a82017-08-15 20:34:35 +0100989 for (i = 0; i < MAX_BPF_REG; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700990 mark_reg_not_init(env, regs, i);
Edward Creedc503a82017-08-15 20:34:35 +0100991 regs[i].live = REG_LIVE_NONE;
Edward Cree679c7822018-08-22 20:02:19 +0100992 regs[i].parent = NULL;
Edward Creedc503a82017-08-15 20:34:35 +0100993 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700994
995 /* frame pointer */
Edward Creef1174f72017-08-07 15:26:19 +0100996 regs[BPF_REG_FP].type = PTR_TO_STACK;
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700997 mark_reg_known_zero(env, regs, BPF_REG_FP);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800998 regs[BPF_REG_FP].frameno = state->frameno;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700999
1000 /* 1st arg to a function */
1001 regs[BPF_REG_1].type = PTR_TO_CTX;
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001002 mark_reg_known_zero(env, regs, BPF_REG_1);
Daniel Borkmann6760bf22016-12-18 01:52:59 +01001003}
1004
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001005#define BPF_MAIN_FUNC (-1)
1006static void init_func_state(struct bpf_verifier_env *env,
1007 struct bpf_func_state *state,
1008 int callsite, int frameno, int subprogno)
1009{
1010 state->callsite = callsite;
1011 state->frameno = frameno;
1012 state->subprogno = subprogno;
1013 init_reg_state(env, state);
1014}
1015
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001016enum reg_arg_type {
1017 SRC_OP, /* register is used as source operand */
1018 DST_OP, /* register is used as destination operand */
1019 DST_OP_NO_MARK /* same as above, check only, don't mark */
1020};
1021
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001022static int cmp_subprogs(const void *a, const void *b)
1023{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001024 return ((struct bpf_subprog_info *)a)->start -
1025 ((struct bpf_subprog_info *)b)->start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001026}
1027
1028static int find_subprog(struct bpf_verifier_env *env, int off)
1029{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001030 struct bpf_subprog_info *p;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001031
Jiong Wang9c8105b2018-05-02 16:17:18 -04001032 p = bsearch(&off, env->subprog_info, env->subprog_cnt,
1033 sizeof(env->subprog_info[0]), cmp_subprogs);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001034 if (!p)
1035 return -ENOENT;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001036 return p - env->subprog_info;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001037
1038}
1039
1040static int add_subprog(struct bpf_verifier_env *env, int off)
1041{
1042 int insn_cnt = env->prog->len;
1043 int ret;
1044
1045 if (off >= insn_cnt || off < 0) {
1046 verbose(env, "call to invalid destination\n");
1047 return -EINVAL;
1048 }
1049 ret = find_subprog(env, off);
1050 if (ret >= 0)
1051 return 0;
Jiong Wang4cb3d992018-05-02 16:17:19 -04001052 if (env->subprog_cnt >= BPF_MAX_SUBPROGS) {
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001053 verbose(env, "too many subprograms\n");
1054 return -E2BIG;
1055 }
Jiong Wang9c8105b2018-05-02 16:17:18 -04001056 env->subprog_info[env->subprog_cnt++].start = off;
1057 sort(env->subprog_info, env->subprog_cnt,
1058 sizeof(env->subprog_info[0]), cmp_subprogs, NULL);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001059 return 0;
1060}
1061
1062static int check_subprogs(struct bpf_verifier_env *env)
1063{
1064 int i, ret, subprog_start, subprog_end, off, cur_subprog = 0;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001065 struct bpf_subprog_info *subprog = env->subprog_info;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001066 struct bpf_insn *insn = env->prog->insnsi;
1067 int insn_cnt = env->prog->len;
1068
Jiong Wangf910cef2018-05-02 16:17:17 -04001069 /* Add entry function. */
1070 ret = add_subprog(env, 0);
1071 if (ret < 0)
1072 return ret;
1073
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001074 /* determine subprog starts. The end is one before the next starts */
1075 for (i = 0; i < insn_cnt; i++) {
1076 if (insn[i].code != (BPF_JMP | BPF_CALL))
1077 continue;
1078 if (insn[i].src_reg != BPF_PSEUDO_CALL)
1079 continue;
1080 if (!env->allow_ptr_leaks) {
1081 verbose(env, "function calls to other bpf functions are allowed for root only\n");
1082 return -EPERM;
1083 }
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001084 ret = add_subprog(env, i + insn[i].imm + 1);
1085 if (ret < 0)
1086 return ret;
1087 }
1088
Jiong Wang4cb3d992018-05-02 16:17:19 -04001089 /* Add a fake 'exit' subprog which could simplify subprog iteration
1090 * logic. 'subprog_cnt' should not be increased.
1091 */
1092 subprog[env->subprog_cnt].start = insn_cnt;
1093
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07001094 if (env->log.level & BPF_LOG_LEVEL2)
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001095 for (i = 0; i < env->subprog_cnt; i++)
Jiong Wang9c8105b2018-05-02 16:17:18 -04001096 verbose(env, "func#%d @%d\n", i, subprog[i].start);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001097
1098 /* now check that all jumps are within the same subprog */
Jiong Wang4cb3d992018-05-02 16:17:19 -04001099 subprog_start = subprog[cur_subprog].start;
1100 subprog_end = subprog[cur_subprog + 1].start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001101 for (i = 0; i < insn_cnt; i++) {
1102 u8 code = insn[i].code;
1103
Jiong Wang092ed092019-01-26 12:26:01 -05001104 if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32)
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001105 goto next;
1106 if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL)
1107 goto next;
1108 off = i + insn[i].off + 1;
1109 if (off < subprog_start || off >= subprog_end) {
1110 verbose(env, "jump out of range from insn %d to %d\n", i, off);
1111 return -EINVAL;
1112 }
1113next:
1114 if (i == subprog_end - 1) {
1115 /* to avoid fall-through from one subprog into another
1116 * the last insn of the subprog should be either exit
1117 * or unconditional jump back
1118 */
1119 if (code != (BPF_JMP | BPF_EXIT) &&
1120 code != (BPF_JMP | BPF_JA)) {
1121 verbose(env, "last insn is not an exit or jmp\n");
1122 return -EINVAL;
1123 }
1124 subprog_start = subprog_end;
Jiong Wang4cb3d992018-05-02 16:17:19 -04001125 cur_subprog++;
1126 if (cur_subprog < env->subprog_cnt)
Jiong Wang9c8105b2018-05-02 16:17:18 -04001127 subprog_end = subprog[cur_subprog + 1].start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001128 }
1129 }
1130 return 0;
1131}
1132
Edward Cree679c7822018-08-22 20:02:19 +01001133/* Parentage chain of this register (or stack slot) should take care of all
1134 * issues like callee-saved registers, stack slot allocation time, etc.
1135 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001136static int mark_reg_read(struct bpf_verifier_env *env,
Edward Cree679c7822018-08-22 20:02:19 +01001137 const struct bpf_reg_state *state,
1138 struct bpf_reg_state *parent)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001139{
1140 bool writes = parent == state->parent; /* Observe write marks */
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07001141 int cnt = 0;
Edward Creedc503a82017-08-15 20:34:35 +01001142
1143 while (parent) {
1144 /* if read wasn't screened by an earlier write ... */
Edward Cree679c7822018-08-22 20:02:19 +01001145 if (writes && state->live & REG_LIVE_WRITTEN)
Edward Creedc503a82017-08-15 20:34:35 +01001146 break;
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -08001147 if (parent->live & REG_LIVE_DONE) {
1148 verbose(env, "verifier BUG type %s var_off %lld off %d\n",
1149 reg_type_str[parent->type],
1150 parent->var_off.value, parent->off);
1151 return -EFAULT;
1152 }
Alexei Starovoitov25af32d2019-04-01 21:27:42 -07001153 if (parent->live & REG_LIVE_READ)
1154 /* The parentage chain never changes and
1155 * this parent was already marked as LIVE_READ.
1156 * There is no need to keep walking the chain again and
1157 * keep re-marking all parents as LIVE_READ.
1158 * This case happens when the same register is read
1159 * multiple times without writes into it in-between.
1160 */
1161 break;
Edward Creedc503a82017-08-15 20:34:35 +01001162 /* ... then we depend on parent's value */
Edward Cree679c7822018-08-22 20:02:19 +01001163 parent->live |= REG_LIVE_READ;
Edward Creedc503a82017-08-15 20:34:35 +01001164 state = parent;
1165 parent = state->parent;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001166 writes = true;
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07001167 cnt++;
Edward Creedc503a82017-08-15 20:34:35 +01001168 }
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07001169
1170 if (env->longest_mark_read_walk < cnt)
1171 env->longest_mark_read_walk = cnt;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001172 return 0;
Edward Creedc503a82017-08-15 20:34:35 +01001173}
1174
1175static int check_reg_arg(struct bpf_verifier_env *env, u32 regno,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001176 enum reg_arg_type t)
1177{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001178 struct bpf_verifier_state *vstate = env->cur_state;
1179 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Jiong Wangc342dc12019-04-12 22:59:37 +01001180 struct bpf_reg_state *reg, *regs = state->regs;
Edward Creedc503a82017-08-15 20:34:35 +01001181
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001182 if (regno >= MAX_BPF_REG) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001183 verbose(env, "R%d is invalid\n", regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001184 return -EINVAL;
1185 }
1186
Jiong Wangc342dc12019-04-12 22:59:37 +01001187 reg = &regs[regno];
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001188 if (t == SRC_OP) {
1189 /* check whether register used as source operand can be read */
Jiong Wangc342dc12019-04-12 22:59:37 +01001190 if (reg->type == NOT_INIT) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001191 verbose(env, "R%d !read_ok\n", regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001192 return -EACCES;
1193 }
Edward Cree679c7822018-08-22 20:02:19 +01001194 /* We don't need to worry about FP liveness because it's read-only */
Jiong Wangc342dc12019-04-12 22:59:37 +01001195 if (regno == BPF_REG_FP)
1196 return 0;
1197
1198 return mark_reg_read(env, reg, reg->parent);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001199 } else {
1200 /* check whether register used as dest operand can be written to */
1201 if (regno == BPF_REG_FP) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001202 verbose(env, "frame pointer is read only\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001203 return -EACCES;
1204 }
Jiong Wangc342dc12019-04-12 22:59:37 +01001205 reg->live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001206 if (t == DST_OP)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001207 mark_reg_unknown(env, regs, regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001208 }
1209 return 0;
1210}
1211
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001212static bool is_spillable_regtype(enum bpf_reg_type type)
1213{
1214 switch (type) {
1215 case PTR_TO_MAP_VALUE:
1216 case PTR_TO_MAP_VALUE_OR_NULL:
1217 case PTR_TO_STACK:
1218 case PTR_TO_CTX:
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001219 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001220 case PTR_TO_PACKET_META:
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001221 case PTR_TO_PACKET_END:
Petar Penkovd58e4682018-09-14 07:46:18 -07001222 case PTR_TO_FLOW_KEYS:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001223 case CONST_PTR_TO_MAP:
Joe Stringerc64b7982018-10-02 13:35:33 -07001224 case PTR_TO_SOCKET:
1225 case PTR_TO_SOCKET_OR_NULL:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001226 case PTR_TO_SOCK_COMMON:
1227 case PTR_TO_SOCK_COMMON_OR_NULL:
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08001228 case PTR_TO_TCP_SOCK:
1229 case PTR_TO_TCP_SOCK_OR_NULL:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001230 return true;
1231 default:
1232 return false;
1233 }
1234}
1235
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001236/* Does this register contain a constant zero? */
1237static bool register_is_null(struct bpf_reg_state *reg)
1238{
1239 return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0);
1240}
1241
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001242/* check_stack_read/write functions track spill/fill of registers,
1243 * stack boundary and alignment are checked in check_mem_access()
1244 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001245static int check_stack_write(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001246 struct bpf_func_state *state, /* func where register points to */
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07001247 int off, int size, int value_regno, int insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001248{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001249 struct bpf_func_state *cur; /* state of the current function */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001250 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001251 enum bpf_reg_type type;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001252
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001253 err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE),
Joe Stringerfd978bf72018-10-02 13:35:35 -07001254 state->acquired_refs, true);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001255 if (err)
1256 return err;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001257 /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
1258 * so it's aligned access and [off, off + size) are within stack limits
1259 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001260 if (!env->allow_ptr_leaks &&
1261 state->stack[spi].slot_type[0] == STACK_SPILL &&
1262 size != BPF_REG_SIZE) {
1263 verbose(env, "attempt to corrupt spilled pointer on stack\n");
1264 return -EACCES;
1265 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001266
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001267 cur = env->cur_state->frame[env->cur_state->curframe];
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001268 if (value_regno >= 0 &&
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001269 is_spillable_regtype((type = cur->regs[value_regno].type))) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001270
1271 /* register containing pointer is being spilled into stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001272 if (size != BPF_REG_SIZE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001273 verbose(env, "invalid size of register spill\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001274 return -EACCES;
1275 }
1276
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001277 if (state != cur && type == PTR_TO_STACK) {
1278 verbose(env, "cannot spill pointers to stack into stack frame of the caller\n");
1279 return -EINVAL;
1280 }
1281
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001282 /* save register state */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001283 state->stack[spi].spilled_ptr = cur->regs[value_regno];
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001284 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001285
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07001286 for (i = 0; i < BPF_REG_SIZE; i++) {
1287 if (state->stack[spi].slot_type[i] == STACK_MISC &&
1288 !env->allow_ptr_leaks) {
1289 int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off;
1290 int soff = (-spi - 1) * BPF_REG_SIZE;
1291
1292 /* detected reuse of integer stack slot with a pointer
1293 * which means either llvm is reusing stack slot or
1294 * an attacker is trying to exploit CVE-2018-3639
1295 * (speculative store bypass)
1296 * Have to sanitize that slot with preemptive
1297 * store of zero.
1298 */
1299 if (*poff && *poff != soff) {
1300 /* disallow programs where single insn stores
1301 * into two different stack slots, since verifier
1302 * cannot sanitize them
1303 */
1304 verbose(env,
1305 "insn %d cannot access two stack slots fp%d and fp%d",
1306 insn_idx, *poff, soff);
1307 return -EINVAL;
1308 }
1309 *poff = soff;
1310 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001311 state->stack[spi].slot_type[i] = STACK_SPILL;
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07001312 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001313 } else {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001314 u8 type = STACK_MISC;
1315
Edward Cree679c7822018-08-22 20:02:19 +01001316 /* regular write of data into stack destroys any spilled ptr */
1317 state->stack[spi].spilled_ptr.type = NOT_INIT;
Jiong Wang0bae2d42018-12-15 03:34:40 -05001318 /* Mark slots as STACK_MISC if they belonged to spilled ptr. */
1319 if (state->stack[spi].slot_type[0] == STACK_SPILL)
1320 for (i = 0; i < BPF_REG_SIZE; i++)
1321 state->stack[spi].slot_type[i] = STACK_MISC;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001322
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001323 /* only mark the slot as written if all 8 bytes were written
1324 * otherwise read propagation may incorrectly stop too soon
1325 * when stack slots are partially written.
1326 * This heuristic means that read propagation will be
1327 * conservative, since it will add reg_live_read marks
1328 * to stack slots all the way to first state when programs
1329 * writes+reads less than 8 bytes
1330 */
1331 if (size == BPF_REG_SIZE)
1332 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
1333
1334 /* when we zero initialize stack slots mark them as such */
1335 if (value_regno >= 0 &&
1336 register_is_null(&cur->regs[value_regno]))
1337 type = STACK_ZERO;
1338
Jiong Wang0bae2d42018-12-15 03:34:40 -05001339 /* Mark slots affected by this stack write. */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001340 for (i = 0; i < size; i++)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001341 state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] =
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001342 type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001343 }
1344 return 0;
1345}
1346
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001347static int check_stack_read(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001348 struct bpf_func_state *reg_state /* func where register points to */,
1349 int off, int size, int value_regno)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001350{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001351 struct bpf_verifier_state *vstate = env->cur_state;
1352 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001353 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE;
1354 u8 *stype;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001355
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001356 if (reg_state->allocated_stack <= slot) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001357 verbose(env, "invalid read from stack off %d+0 size %d\n",
1358 off, size);
1359 return -EACCES;
1360 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001361 stype = reg_state->stack[spi].slot_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001362
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001363 if (stype[0] == STACK_SPILL) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001364 if (size != BPF_REG_SIZE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001365 verbose(env, "invalid size of register spill\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001366 return -EACCES;
1367 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001368 for (i = 1; i < BPF_REG_SIZE; i++) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001369 if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001370 verbose(env, "corrupted spill memory\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001371 return -EACCES;
1372 }
1373 }
1374
Edward Creedc503a82017-08-15 20:34:35 +01001375 if (value_regno >= 0) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001376 /* restore register state from stack */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001377 state->regs[value_regno] = reg_state->stack[spi].spilled_ptr;
Alexei Starovoitov2f18f622017-11-30 21:31:38 -08001378 /* mark reg as written since spilled pointer state likely
1379 * has its liveness marks cleared by is_state_visited()
1380 * which resets stack/reg liveness for state transitions
1381 */
1382 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
Edward Creedc503a82017-08-15 20:34:35 +01001383 }
Edward Cree679c7822018-08-22 20:02:19 +01001384 mark_reg_read(env, &reg_state->stack[spi].spilled_ptr,
1385 reg_state->stack[spi].spilled_ptr.parent);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001386 return 0;
1387 } else {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001388 int zeros = 0;
1389
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001390 for (i = 0; i < size; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001391 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC)
1392 continue;
1393 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) {
1394 zeros++;
1395 continue;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001396 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001397 verbose(env, "invalid read from stack off %d+%d size %d\n",
1398 off, i, size);
1399 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001400 }
Edward Cree679c7822018-08-22 20:02:19 +01001401 mark_reg_read(env, &reg_state->stack[spi].spilled_ptr,
1402 reg_state->stack[spi].spilled_ptr.parent);
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001403 if (value_regno >= 0) {
1404 if (zeros == size) {
1405 /* any size read into register is zero extended,
1406 * so the whole register == const_zero
1407 */
1408 __mark_reg_const_zero(&state->regs[value_regno]);
1409 } else {
1410 /* have read misc data from the stack */
1411 mark_reg_unknown(env, state->regs, value_regno);
1412 }
1413 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
1414 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001415 return 0;
1416 }
1417}
1418
Daniel Borkmanne4298d22019-01-03 00:58:31 +01001419static int check_stack_access(struct bpf_verifier_env *env,
1420 const struct bpf_reg_state *reg,
1421 int off, int size)
1422{
1423 /* Stack accesses must be at a fixed offset, so that we
1424 * can determine what type of data were returned. See
1425 * check_stack_read().
1426 */
1427 if (!tnum_is_const(reg->var_off)) {
1428 char tn_buf[48];
1429
1430 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Andrey Ignatov1fbd20f2019-04-03 23:22:43 -07001431 verbose(env, "variable stack access var_off=%s off=%d size=%d\n",
Daniel Borkmanne4298d22019-01-03 00:58:31 +01001432 tn_buf, off, size);
1433 return -EACCES;
1434 }
1435
1436 if (off >= 0 || off < -MAX_BPF_STACK) {
1437 verbose(env, "invalid stack off=%d size=%d\n", off, size);
1438 return -EACCES;
1439 }
1440
1441 return 0;
1442}
1443
Daniel Borkmann591fe982019-04-09 23:20:05 +02001444static int check_map_access_type(struct bpf_verifier_env *env, u32 regno,
1445 int off, int size, enum bpf_access_type type)
1446{
1447 struct bpf_reg_state *regs = cur_regs(env);
1448 struct bpf_map *map = regs[regno].map_ptr;
1449 u32 cap = bpf_map_flags_to_cap(map);
1450
1451 if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) {
1452 verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n",
1453 map->value_size, off, size);
1454 return -EACCES;
1455 }
1456
1457 if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) {
1458 verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n",
1459 map->value_size, off, size);
1460 return -EACCES;
1461 }
1462
1463 return 0;
1464}
1465
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001466/* check read/write into map element returned by bpf_map_lookup_elem() */
Edward Creef1174f72017-08-07 15:26:19 +01001467static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001468 int size, bool zero_size_allowed)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001469{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001470 struct bpf_reg_state *regs = cur_regs(env);
1471 struct bpf_map *map = regs[regno].map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001472
Yonghong Song9fd29c02017-11-12 14:49:09 -08001473 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
1474 off + size > map->value_size) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001475 verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001476 map->value_size, off, size);
1477 return -EACCES;
1478 }
1479 return 0;
1480}
1481
Edward Creef1174f72017-08-07 15:26:19 +01001482/* check read/write into a map element with possible variable offset */
1483static int check_map_access(struct bpf_verifier_env *env, u32 regno,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001484 int off, int size, bool zero_size_allowed)
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001485{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001486 struct bpf_verifier_state *vstate = env->cur_state;
1487 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001488 struct bpf_reg_state *reg = &state->regs[regno];
1489 int err;
1490
Edward Creef1174f72017-08-07 15:26:19 +01001491 /* We may have adjusted the register to this map value, so we
1492 * need to try adding each of min_value and max_value to off
1493 * to make sure our theoretical access will be safe.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001494 */
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07001495 if (env->log.level & BPF_LOG_LEVEL)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001496 print_verifier_state(env, state);
Daniel Borkmannb7137c42019-01-03 00:58:33 +01001497
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001498 /* The minimum value is only important with signed
1499 * comparisons where we can't assume the floor of a
1500 * value is 0. If we are using signed variables for our
1501 * index'es we need to make sure that whatever we use
1502 * will have a set floor within our range.
1503 */
Daniel Borkmannb7137c42019-01-03 00:58:33 +01001504 if (reg->smin_value < 0 &&
1505 (reg->smin_value == S64_MIN ||
1506 (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) ||
1507 reg->smin_value + off < 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001508 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 -08001509 regno);
1510 return -EACCES;
1511 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001512 err = __check_map_access(env, regno, reg->smin_value + off, size,
1513 zero_size_allowed);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001514 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001515 verbose(env, "R%d min value is outside of the array range\n",
1516 regno);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001517 return err;
1518 }
1519
Edward Creeb03c9f92017-08-07 15:26:36 +01001520 /* If we haven't set a max value then we need to bail since we can't be
1521 * sure we won't do bad things.
1522 * If reg->umax_value + off could overflow, treat that as unbounded too.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001523 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001524 if (reg->umax_value >= BPF_MAX_VAR_OFF) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001525 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 -08001526 regno);
1527 return -EACCES;
1528 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001529 err = __check_map_access(env, regno, reg->umax_value + off, size,
1530 zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01001531 if (err)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001532 verbose(env, "R%d max value is outside of the array range\n",
1533 regno);
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08001534
1535 if (map_value_has_spin_lock(reg->map_ptr)) {
1536 u32 lock = reg->map_ptr->spin_lock_off;
1537
1538 /* if any part of struct bpf_spin_lock can be touched by
1539 * load/store reject this program.
1540 * To check that [x1, x2) overlaps with [y1, y2)
1541 * it is sufficient to check x1 < y2 && y1 < x2.
1542 */
1543 if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) &&
1544 lock < reg->umax_value + off + size) {
1545 verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n");
1546 return -EACCES;
1547 }
1548 }
Edward Creef1174f72017-08-07 15:26:19 +01001549 return err;
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001550}
1551
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001552#define MAX_PACKET_OFF 0xffff
1553
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001554static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001555 const struct bpf_call_arg_meta *meta,
1556 enum bpf_access_type t)
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001557{
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001558 switch (env->prog->type) {
Daniel Borkmann5d66fa72018-10-24 22:05:45 +02001559 /* Program types only with direct read access go here! */
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001560 case BPF_PROG_TYPE_LWT_IN:
1561 case BPF_PROG_TYPE_LWT_OUT:
Mathieu Xhonneux004d4b22018-05-20 14:58:16 +01001562 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07001563 case BPF_PROG_TYPE_SK_REUSEPORT:
Daniel Borkmann5d66fa72018-10-24 22:05:45 +02001564 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Daniel Borkmannd5563d32018-10-24 22:05:46 +02001565 case BPF_PROG_TYPE_CGROUP_SKB:
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001566 if (t == BPF_WRITE)
1567 return false;
Alexander Alemayhu7e57fbb2017-02-14 00:02:35 +01001568 /* fallthrough */
Daniel Borkmann5d66fa72018-10-24 22:05:45 +02001569
1570 /* Program types with direct read + write access go here! */
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001571 case BPF_PROG_TYPE_SCHED_CLS:
1572 case BPF_PROG_TYPE_SCHED_ACT:
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001573 case BPF_PROG_TYPE_XDP:
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001574 case BPF_PROG_TYPE_LWT_XMIT:
John Fastabend8a31db52017-08-15 22:33:09 -07001575 case BPF_PROG_TYPE_SK_SKB:
John Fastabend4f738ad2018-03-18 12:57:10 -07001576 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001577 if (meta)
1578 return meta->pkt_access;
1579
1580 env->seen_direct_write = true;
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001581 return true;
1582 default:
1583 return false;
1584 }
1585}
1586
Edward Creef1174f72017-08-07 15:26:19 +01001587static int __check_packet_access(struct bpf_verifier_env *env, u32 regno,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001588 int off, int size, bool zero_size_allowed)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001589{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001590 struct bpf_reg_state *regs = cur_regs(env);
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001591 struct bpf_reg_state *reg = &regs[regno];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001592
Yonghong Song9fd29c02017-11-12 14:49:09 -08001593 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
1594 (u64)off + size > reg->range) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001595 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 -07001596 off, size, regno, reg->id, reg->off, reg->range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001597 return -EACCES;
1598 }
1599 return 0;
1600}
1601
Edward Creef1174f72017-08-07 15:26:19 +01001602static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001603 int size, bool zero_size_allowed)
Edward Creef1174f72017-08-07 15:26:19 +01001604{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001605 struct bpf_reg_state *regs = cur_regs(env);
Edward Creef1174f72017-08-07 15:26:19 +01001606 struct bpf_reg_state *reg = &regs[regno];
1607 int err;
1608
1609 /* We may have added a variable offset to the packet pointer; but any
1610 * reg->range we have comes after that. We are only checking the fixed
1611 * offset.
1612 */
1613
1614 /* We don't allow negative numbers, because we aren't tracking enough
1615 * detail to prove they're safe.
1616 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001617 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001618 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 +01001619 regno);
1620 return -EACCES;
1621 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001622 err = __check_packet_access(env, regno, off, size, zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01001623 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001624 verbose(env, "R%d offset is outside of the packet\n", regno);
Edward Creef1174f72017-08-07 15:26:19 +01001625 return err;
1626 }
Jiong Wange6478152018-11-08 04:08:42 -05001627
1628 /* __check_packet_access has made sure "off + size - 1" is within u16.
1629 * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff,
1630 * otherwise find_good_pkt_pointers would have refused to set range info
1631 * that __check_packet_access would have rejected this pkt access.
1632 * Therefore, "off + reg->umax_value + size - 1" won't overflow u32.
1633 */
1634 env->prog->aux->max_pkt_offset =
1635 max_t(u32, env->prog->aux->max_pkt_offset,
1636 off + reg->umax_value + size - 1);
1637
Edward Creef1174f72017-08-07 15:26:19 +01001638 return err;
1639}
1640
1641/* check access to 'struct bpf_context' fields. Supports fixed offsets only */
Yonghong Song31fd8582017-06-13 15:52:13 -07001642static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07001643 enum bpf_access_type t, enum bpf_reg_type *reg_type)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001644{
Daniel Borkmannf96da092017-07-02 02:13:27 +02001645 struct bpf_insn_access_aux info = {
1646 .reg_type = *reg_type,
1647 };
Yonghong Song31fd8582017-06-13 15:52:13 -07001648
Jakub Kicinski4f9218a2017-10-16 16:40:55 -07001649 if (env->ops->is_valid_access &&
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001650 env->ops->is_valid_access(off, size, t, env->prog, &info)) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02001651 /* A non zero info.ctx_field_size indicates that this field is a
1652 * candidate for later verifier transformation to load the whole
1653 * field and then apply a mask when accessed with a narrower
1654 * access than actual ctx access size. A zero info.ctx_field_size
1655 * will only allow for whole field access and rejects any other
1656 * type of narrower access.
Yonghong Song31fd8582017-06-13 15:52:13 -07001657 */
Yonghong Song23994632017-06-22 15:07:39 -07001658 *reg_type = info.reg_type;
Yonghong Song31fd8582017-06-13 15:52:13 -07001659
Jakub Kicinski4f9218a2017-10-16 16:40:55 -07001660 env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07001661 /* remember the offset of last byte accessed in ctx */
1662 if (env->prog->aux->max_ctx_offset < off + size)
1663 env->prog->aux->max_ctx_offset = off + size;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001664 return 0;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07001665 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001666
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001667 verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001668 return -EACCES;
1669}
1670
Petar Penkovd58e4682018-09-14 07:46:18 -07001671static int check_flow_keys_access(struct bpf_verifier_env *env, int off,
1672 int size)
1673{
1674 if (size < 0 || off < 0 ||
1675 (u64)off + size > sizeof(struct bpf_flow_keys)) {
1676 verbose(env, "invalid access to flow keys off=%d size=%d\n",
1677 off, size);
1678 return -EACCES;
1679 }
1680 return 0;
1681}
1682
Martin KaFai Lau5f456642019-02-08 22:25:54 -08001683static int check_sock_access(struct bpf_verifier_env *env, int insn_idx,
1684 u32 regno, int off, int size,
1685 enum bpf_access_type t)
Joe Stringerc64b7982018-10-02 13:35:33 -07001686{
1687 struct bpf_reg_state *regs = cur_regs(env);
1688 struct bpf_reg_state *reg = &regs[regno];
Martin KaFai Lau5f456642019-02-08 22:25:54 -08001689 struct bpf_insn_access_aux info = {};
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001690 bool valid;
Joe Stringerc64b7982018-10-02 13:35:33 -07001691
1692 if (reg->smin_value < 0) {
1693 verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
1694 regno);
1695 return -EACCES;
1696 }
1697
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001698 switch (reg->type) {
1699 case PTR_TO_SOCK_COMMON:
1700 valid = bpf_sock_common_is_valid_access(off, size, t, &info);
1701 break;
1702 case PTR_TO_SOCKET:
1703 valid = bpf_sock_is_valid_access(off, size, t, &info);
1704 break;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08001705 case PTR_TO_TCP_SOCK:
1706 valid = bpf_tcp_sock_is_valid_access(off, size, t, &info);
1707 break;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001708 default:
1709 valid = false;
Joe Stringerc64b7982018-10-02 13:35:33 -07001710 }
1711
Martin KaFai Lau5f456642019-02-08 22:25:54 -08001712
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001713 if (valid) {
1714 env->insn_aux_data[insn_idx].ctx_field_size =
1715 info.ctx_field_size;
1716 return 0;
1717 }
1718
1719 verbose(env, "R%d invalid %s access off=%d size=%d\n",
1720 regno, reg_type_str[reg->type], off, size);
1721
1722 return -EACCES;
Joe Stringerc64b7982018-10-02 13:35:33 -07001723}
1724
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001725static bool __is_pointer_value(bool allow_ptr_leaks,
1726 const struct bpf_reg_state *reg)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001727{
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001728 if (allow_ptr_leaks)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001729 return false;
1730
Edward Creef1174f72017-08-07 15:26:19 +01001731 return reg->type != SCALAR_VALUE;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001732}
1733
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001734static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno)
1735{
1736 return cur_regs(env) + regno;
1737}
1738
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001739static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
1740{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001741 return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno));
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001742}
1743
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01001744static bool is_ctx_reg(struct bpf_verifier_env *env, int regno)
1745{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001746 const struct bpf_reg_state *reg = reg_state(env, regno);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01001747
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001748 return reg->type == PTR_TO_CTX;
1749}
1750
1751static bool is_sk_reg(struct bpf_verifier_env *env, int regno)
1752{
1753 const struct bpf_reg_state *reg = reg_state(env, regno);
1754
1755 return type_is_sk_pointer(reg->type);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01001756}
1757
Daniel Borkmannca369602018-02-23 22:29:05 +01001758static bool is_pkt_reg(struct bpf_verifier_env *env, int regno)
1759{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001760 const struct bpf_reg_state *reg = reg_state(env, regno);
Daniel Borkmannca369602018-02-23 22:29:05 +01001761
1762 return type_is_pkt_pointer(reg->type);
1763}
1764
Daniel Borkmann4b5defd2018-10-21 02:09:25 +02001765static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno)
1766{
1767 const struct bpf_reg_state *reg = reg_state(env, regno);
1768
1769 /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */
1770 return reg->type == PTR_TO_FLOW_KEYS;
1771}
1772
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001773static int check_pkt_ptr_alignment(struct bpf_verifier_env *env,
1774 const struct bpf_reg_state *reg,
David S. Millerd1174412017-05-10 11:22:52 -07001775 int off, int size, bool strict)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001776{
Edward Creef1174f72017-08-07 15:26:19 +01001777 struct tnum reg_off;
David S. Millere07b98d2017-05-10 11:38:07 -07001778 int ip_align;
David S. Millerd1174412017-05-10 11:22:52 -07001779
1780 /* Byte size accesses are always allowed. */
1781 if (!strict || size == 1)
1782 return 0;
1783
David S. Millere4eda882017-05-22 12:27:07 -04001784 /* For platforms that do not have a Kconfig enabling
1785 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of
1786 * NET_IP_ALIGN is universally set to '2'. And on platforms
1787 * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get
1788 * to this code only in strict mode where we want to emulate
1789 * the NET_IP_ALIGN==2 checking. Therefore use an
1790 * unconditional IP align value of '2'.
David S. Millere07b98d2017-05-10 11:38:07 -07001791 */
David S. Millere4eda882017-05-22 12:27:07 -04001792 ip_align = 2;
Edward Creef1174f72017-08-07 15:26:19 +01001793
1794 reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off));
1795 if (!tnum_is_aligned(reg_off, size)) {
1796 char tn_buf[48];
1797
1798 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001799 verbose(env,
1800 "misaligned packet access off %d+%s+%d+%d size %d\n",
Edward Creef1174f72017-08-07 15:26:19 +01001801 ip_align, tn_buf, reg->off, off, size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001802 return -EACCES;
1803 }
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001804
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001805 return 0;
1806}
1807
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001808static int check_generic_ptr_alignment(struct bpf_verifier_env *env,
1809 const struct bpf_reg_state *reg,
Edward Creef1174f72017-08-07 15:26:19 +01001810 const char *pointer_desc,
1811 int off, int size, bool strict)
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001812{
Edward Creef1174f72017-08-07 15:26:19 +01001813 struct tnum reg_off;
1814
1815 /* Byte size accesses are always allowed. */
1816 if (!strict || size == 1)
1817 return 0;
1818
1819 reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off));
1820 if (!tnum_is_aligned(reg_off, size)) {
1821 char tn_buf[48];
1822
1823 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001824 verbose(env, "misaligned %saccess off %s+%d+%d size %d\n",
Edward Creef1174f72017-08-07 15:26:19 +01001825 pointer_desc, tn_buf, reg->off, off, size);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001826 return -EACCES;
1827 }
1828
1829 return 0;
1830}
1831
David S. Millere07b98d2017-05-10 11:38:07 -07001832static int check_ptr_alignment(struct bpf_verifier_env *env,
Daniel Borkmannca369602018-02-23 22:29:05 +01001833 const struct bpf_reg_state *reg, int off,
1834 int size, bool strict_alignment_once)
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001835{
Daniel Borkmannca369602018-02-23 22:29:05 +01001836 bool strict = env->strict_alignment || strict_alignment_once;
Edward Creef1174f72017-08-07 15:26:19 +01001837 const char *pointer_desc = "";
David S. Millerd1174412017-05-10 11:22:52 -07001838
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001839 switch (reg->type) {
1840 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001841 case PTR_TO_PACKET_META:
1842 /* Special case, because of NET_IP_ALIGN. Given metadata sits
1843 * right in front, treat it the very same way.
1844 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001845 return check_pkt_ptr_alignment(env, reg, off, size, strict);
Petar Penkovd58e4682018-09-14 07:46:18 -07001846 case PTR_TO_FLOW_KEYS:
1847 pointer_desc = "flow keys ";
1848 break;
Edward Creef1174f72017-08-07 15:26:19 +01001849 case PTR_TO_MAP_VALUE:
1850 pointer_desc = "value ";
1851 break;
1852 case PTR_TO_CTX:
1853 pointer_desc = "context ";
1854 break;
1855 case PTR_TO_STACK:
1856 pointer_desc = "stack ";
Jann Horna5ec6ae2017-12-18 20:11:58 -08001857 /* The stack spill tracking logic in check_stack_write()
1858 * and check_stack_read() relies on stack accesses being
1859 * aligned.
1860 */
1861 strict = true;
Edward Creef1174f72017-08-07 15:26:19 +01001862 break;
Joe Stringerc64b7982018-10-02 13:35:33 -07001863 case PTR_TO_SOCKET:
1864 pointer_desc = "sock ";
1865 break;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001866 case PTR_TO_SOCK_COMMON:
1867 pointer_desc = "sock_common ";
1868 break;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08001869 case PTR_TO_TCP_SOCK:
1870 pointer_desc = "tcp_sock ";
1871 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001872 default:
Edward Creef1174f72017-08-07 15:26:19 +01001873 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001874 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001875 return check_generic_ptr_alignment(env, reg, pointer_desc, off, size,
1876 strict);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001877}
1878
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001879static int update_stack_depth(struct bpf_verifier_env *env,
1880 const struct bpf_func_state *func,
1881 int off)
1882{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001883 u16 stack = env->subprog_info[func->subprogno].stack_depth;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001884
1885 if (stack >= -off)
1886 return 0;
1887
1888 /* update known max for given subprogram */
Jiong Wang9c8105b2018-05-02 16:17:18 -04001889 env->subprog_info[func->subprogno].stack_depth = -off;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001890 return 0;
1891}
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001892
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001893/* starting from main bpf function walk all instructions of the function
1894 * and recursively walk all callees that given function can call.
1895 * Ignore jump and exit insns.
1896 * Since recursion is prevented by check_cfg() this algorithm
1897 * only needs a local stack of MAX_CALL_FRAMES to remember callsites
1898 */
1899static int check_max_stack_depth(struct bpf_verifier_env *env)
1900{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001901 int depth = 0, frame = 0, idx = 0, i = 0, subprog_end;
1902 struct bpf_subprog_info *subprog = env->subprog_info;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001903 struct bpf_insn *insn = env->prog->insnsi;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001904 int ret_insn[MAX_CALL_FRAMES];
1905 int ret_prog[MAX_CALL_FRAMES];
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001906
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001907process_func:
1908 /* round up to 32-bytes, since this is granularity
1909 * of interpreter stack size
1910 */
Jiong Wang9c8105b2018-05-02 16:17:18 -04001911 depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32);
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001912 if (depth > MAX_BPF_STACK) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001913 verbose(env, "combined stack size of %d calls is %d. Too large\n",
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001914 frame + 1, depth);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001915 return -EACCES;
1916 }
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001917continue_func:
Jiong Wang4cb3d992018-05-02 16:17:19 -04001918 subprog_end = subprog[idx + 1].start;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001919 for (; i < subprog_end; i++) {
1920 if (insn[i].code != (BPF_JMP | BPF_CALL))
1921 continue;
1922 if (insn[i].src_reg != BPF_PSEUDO_CALL)
1923 continue;
1924 /* remember insn and function to return to */
1925 ret_insn[frame] = i + 1;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001926 ret_prog[frame] = idx;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001927
1928 /* find the callee */
1929 i = i + insn[i].imm + 1;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001930 idx = find_subprog(env, i);
1931 if (idx < 0) {
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001932 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
1933 i);
1934 return -EFAULT;
1935 }
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001936 frame++;
1937 if (frame >= MAX_CALL_FRAMES) {
Paul Chaignon927cb782019-03-20 13:58:27 +01001938 verbose(env, "the call stack of %d frames is too deep !\n",
1939 frame);
1940 return -E2BIG;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001941 }
1942 goto process_func;
1943 }
1944 /* end of for() loop means the last insn of the 'subprog'
1945 * was reached. Doesn't matter whether it was JA or EXIT
1946 */
1947 if (frame == 0)
1948 return 0;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001949 depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32);
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001950 frame--;
1951 i = ret_insn[frame];
Jiong Wang9c8105b2018-05-02 16:17:18 -04001952 idx = ret_prog[frame];
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001953 goto continue_func;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001954}
1955
David S. Miller19d28fb2018-01-11 21:27:54 -05001956#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001957static int get_callee_stack_depth(struct bpf_verifier_env *env,
1958 const struct bpf_insn *insn, int idx)
1959{
1960 int start = idx + insn->imm + 1, subprog;
1961
1962 subprog = find_subprog(env, start);
1963 if (subprog < 0) {
1964 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
1965 start);
1966 return -EFAULT;
1967 }
Jiong Wang9c8105b2018-05-02 16:17:18 -04001968 return env->subprog_info[subprog].stack_depth;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001969}
David S. Miller19d28fb2018-01-11 21:27:54 -05001970#endif
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001971
Daniel Borkmann58990d12018-06-07 17:40:03 +02001972static int check_ctx_reg(struct bpf_verifier_env *env,
1973 const struct bpf_reg_state *reg, int regno)
1974{
1975 /* Access to ctx or passing it to a helper is only allowed in
1976 * its original, unmodified form.
1977 */
1978
1979 if (reg->off) {
1980 verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n",
1981 regno, reg->off);
1982 return -EACCES;
1983 }
1984
1985 if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
1986 char tn_buf[48];
1987
1988 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
1989 verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf);
1990 return -EACCES;
1991 }
1992
1993 return 0;
1994}
1995
Jann Horn0c17d1d2017-12-18 20:11:55 -08001996/* truncate register to smaller size (in bytes)
1997 * must be called with size < BPF_REG_SIZE
1998 */
1999static void coerce_reg_to_size(struct bpf_reg_state *reg, int size)
2000{
2001 u64 mask;
2002
2003 /* clear high bits in bit representation */
2004 reg->var_off = tnum_cast(reg->var_off, size);
2005
2006 /* fix arithmetic bounds */
2007 mask = ((u64)1 << (size * 8)) - 1;
2008 if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) {
2009 reg->umin_value &= mask;
2010 reg->umax_value &= mask;
2011 } else {
2012 reg->umin_value = 0;
2013 reg->umax_value = mask;
2014 }
2015 reg->smin_value = reg->umin_value;
2016 reg->smax_value = reg->umax_value;
2017}
2018
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002019/* check whether memory at (regno + off) is accessible for t = (read | write)
2020 * if t==write, value_regno is a register which value is stored into memory
2021 * if t==read, value_regno is a register which will receive the value from memory
2022 * if t==write && value_regno==-1, some unknown value is stored into memory
2023 * if t==read && value_regno==-1, don't care what we read from memory
2024 */
Daniel Borkmannca369602018-02-23 22:29:05 +01002025static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno,
2026 int off, int bpf_size, enum bpf_access_type t,
2027 int value_regno, bool strict_alignment_once)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002028{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002029 struct bpf_reg_state *regs = cur_regs(env);
2030 struct bpf_reg_state *reg = regs + regno;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002031 struct bpf_func_state *state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002032 int size, err = 0;
2033
2034 size = bpf_size_to_bytes(bpf_size);
2035 if (size < 0)
2036 return size;
2037
Edward Creef1174f72017-08-07 15:26:19 +01002038 /* alignment checks will add in reg->off themselves */
Daniel Borkmannca369602018-02-23 22:29:05 +01002039 err = check_ptr_alignment(env, reg, off, size, strict_alignment_once);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002040 if (err)
2041 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002042
Edward Creef1174f72017-08-07 15:26:19 +01002043 /* for access checks, reg->off is just part of off */
2044 off += reg->off;
2045
2046 if (reg->type == PTR_TO_MAP_VALUE) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002047 if (t == BPF_WRITE && value_regno >= 0 &&
2048 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002049 verbose(env, "R%d leaks addr into map\n", value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002050 return -EACCES;
2051 }
Daniel Borkmann591fe982019-04-09 23:20:05 +02002052 err = check_map_access_type(env, regno, off, size, t);
2053 if (err)
2054 return err;
Yonghong Song9fd29c02017-11-12 14:49:09 -08002055 err = check_map_access(env, regno, off, size, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002056 if (!err && t == BPF_READ && value_regno >= 0)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002057 mark_reg_unknown(env, regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002058
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002059 } else if (reg->type == PTR_TO_CTX) {
Edward Creef1174f72017-08-07 15:26:19 +01002060 enum bpf_reg_type reg_type = SCALAR_VALUE;
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07002061
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002062 if (t == BPF_WRITE && value_regno >= 0 &&
2063 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002064 verbose(env, "R%d leaks addr into ctx\n", value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002065 return -EACCES;
2066 }
Edward Creef1174f72017-08-07 15:26:19 +01002067
Daniel Borkmann58990d12018-06-07 17:40:03 +02002068 err = check_ctx_reg(env, reg, regno);
2069 if (err < 0)
2070 return err;
2071
Yonghong Song31fd8582017-06-13 15:52:13 -07002072 err = check_ctx_access(env, insn_idx, off, size, t, &reg_type);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002073 if (!err && t == BPF_READ && value_regno >= 0) {
Edward Creef1174f72017-08-07 15:26:19 +01002074 /* ctx access returns either a scalar, or a
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002075 * PTR_TO_PACKET[_META,_END]. In the latter
2076 * case, we know the offset is zero.
Edward Creef1174f72017-08-07 15:26:19 +01002077 */
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002078 if (reg_type == SCALAR_VALUE) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002079 mark_reg_unknown(env, regs, value_regno);
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002080 } else {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002081 mark_reg_known_zero(env, regs,
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002082 value_regno);
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002083 if (reg_type_may_be_null(reg_type))
2084 regs[value_regno].id = ++env->id_gen;
2085 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002086 regs[value_regno].type = reg_type;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002087 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002088
Edward Creef1174f72017-08-07 15:26:19 +01002089 } else if (reg->type == PTR_TO_STACK) {
Edward Creef1174f72017-08-07 15:26:19 +01002090 off += reg->var_off.value;
Daniel Borkmanne4298d22019-01-03 00:58:31 +01002091 err = check_stack_access(env, reg, off, size);
2092 if (err)
2093 return err;
Alexei Starovoitov87266792017-05-30 13:31:29 -07002094
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002095 state = func(env, reg);
2096 err = update_stack_depth(env, state, off);
2097 if (err)
2098 return err;
Alexei Starovoitov87266792017-05-30 13:31:29 -07002099
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002100 if (t == BPF_WRITE)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002101 err = check_stack_write(env, state, off, size,
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07002102 value_regno, insn_idx);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002103 else
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002104 err = check_stack_read(env, state, off, size,
2105 value_regno);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002106 } else if (reg_is_pkt_pointer(reg)) {
Thomas Graf3a0af8f2016-11-30 17:10:10 +01002107 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002108 verbose(env, "cannot write into packet\n");
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002109 return -EACCES;
2110 }
Brenden Blanco4acf6c02016-07-19 12:16:56 -07002111 if (t == BPF_WRITE && value_regno >= 0 &&
2112 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002113 verbose(env, "R%d leaks addr into packet\n",
2114 value_regno);
Brenden Blanco4acf6c02016-07-19 12:16:56 -07002115 return -EACCES;
2116 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08002117 err = check_packet_access(env, regno, off, size, false);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002118 if (!err && t == BPF_READ && value_regno >= 0)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002119 mark_reg_unknown(env, regs, value_regno);
Petar Penkovd58e4682018-09-14 07:46:18 -07002120 } else if (reg->type == PTR_TO_FLOW_KEYS) {
2121 if (t == BPF_WRITE && value_regno >= 0 &&
2122 is_pointer_value(env, value_regno)) {
2123 verbose(env, "R%d leaks addr into flow keys\n",
2124 value_regno);
2125 return -EACCES;
2126 }
2127
2128 err = check_flow_keys_access(env, off, size);
2129 if (!err && t == BPF_READ && value_regno >= 0)
2130 mark_reg_unknown(env, regs, value_regno);
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002131 } else if (type_is_sk_pointer(reg->type)) {
Joe Stringerc64b7982018-10-02 13:35:33 -07002132 if (t == BPF_WRITE) {
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002133 verbose(env, "R%d cannot write into %s\n",
2134 regno, reg_type_str[reg->type]);
Joe Stringerc64b7982018-10-02 13:35:33 -07002135 return -EACCES;
2136 }
Martin KaFai Lau5f456642019-02-08 22:25:54 -08002137 err = check_sock_access(env, insn_idx, regno, off, size, t);
Joe Stringerc64b7982018-10-02 13:35:33 -07002138 if (!err && value_regno >= 0)
2139 mark_reg_unknown(env, regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002140 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002141 verbose(env, "R%d invalid mem access '%s'\n", regno,
2142 reg_type_str[reg->type]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002143 return -EACCES;
2144 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002145
Edward Creef1174f72017-08-07 15:26:19 +01002146 if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ &&
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002147 regs[value_regno].type == SCALAR_VALUE) {
Edward Creef1174f72017-08-07 15:26:19 +01002148 /* b/h/w load zero-extends, mark upper bits as known 0 */
Jann Horn0c17d1d2017-12-18 20:11:55 -08002149 coerce_reg_to_size(&regs[value_regno], size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002150 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002151 return err;
2152}
2153
Yonghong Song31fd8582017-06-13 15:52:13 -07002154static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002155{
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002156 int err;
2157
2158 if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
2159 insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002160 verbose(env, "BPF_XADD uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002161 return -EINVAL;
2162 }
2163
2164 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01002165 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002166 if (err)
2167 return err;
2168
2169 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01002170 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002171 if (err)
2172 return err;
2173
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +02002174 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002175 verbose(env, "R%d leaks addr into mem\n", insn->src_reg);
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +02002176 return -EACCES;
2177 }
2178
Daniel Borkmannca369602018-02-23 22:29:05 +01002179 if (is_ctx_reg(env, insn->dst_reg) ||
Daniel Borkmann4b5defd2018-10-21 02:09:25 +02002180 is_pkt_reg(env, insn->dst_reg) ||
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002181 is_flow_key_reg(env, insn->dst_reg) ||
2182 is_sk_reg(env, insn->dst_reg)) {
Daniel Borkmannca369602018-02-23 22:29:05 +01002183 verbose(env, "BPF_XADD stores into R%d %s is not allowed\n",
Daniel Borkmann2a159c62018-10-21 02:09:24 +02002184 insn->dst_reg,
2185 reg_type_str[reg_state(env, insn->dst_reg)->type]);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01002186 return -EACCES;
2187 }
2188
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002189 /* check whether atomic_add can read the memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07002190 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Daniel Borkmannca369602018-02-23 22:29:05 +01002191 BPF_SIZE(insn->code), BPF_READ, -1, true);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002192 if (err)
2193 return err;
2194
2195 /* check whether atomic_add can write into the same memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07002196 return check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Daniel Borkmannca369602018-02-23 22:29:05 +01002197 BPF_SIZE(insn->code), BPF_WRITE, -1, true);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002198}
2199
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002200static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno,
2201 int off, int access_size,
2202 bool zero_size_allowed)
2203{
2204 struct bpf_reg_state *reg = reg_state(env, regno);
2205
2206 if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
2207 access_size < 0 || (access_size == 0 && !zero_size_allowed)) {
2208 if (tnum_is_const(reg->var_off)) {
2209 verbose(env, "invalid stack type R%d off=%d access_size=%d\n",
2210 regno, off, access_size);
2211 } else {
2212 char tn_buf[48];
2213
2214 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2215 verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n",
2216 regno, tn_buf, access_size);
2217 }
2218 return -EACCES;
2219 }
2220 return 0;
2221}
2222
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002223/* when register 'regno' is passed into function that will read 'access_size'
2224 * bytes from that pointer, make sure that it's within stack boundary
Edward Creef1174f72017-08-07 15:26:19 +01002225 * and all elements of stack are initialized.
2226 * Unlike most pointer bounds-checking functions, this one doesn't take an
2227 * 'off' argument, so it has to add in reg->off itself.
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002228 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002229static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
Daniel Borkmann435faee12016-04-13 00:10:51 +02002230 int access_size, bool zero_size_allowed,
2231 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002232{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02002233 struct bpf_reg_state *reg = reg_state(env, regno);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002234 struct bpf_func_state *state = func(env, reg);
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002235 int err, min_off, max_off, i, slot, spi;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002236
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002237 if (reg->type != PTR_TO_STACK) {
Edward Creef1174f72017-08-07 15:26:19 +01002238 /* Allow zero-byte read from NULL, regardless of pointer type */
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002239 if (zero_size_allowed && access_size == 0 &&
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002240 register_is_null(reg))
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002241 return 0;
2242
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002243 verbose(env, "R%d type=%s expected=%s\n", regno,
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002244 reg_type_str[reg->type],
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002245 reg_type_str[PTR_TO_STACK]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002246 return -EACCES;
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002247 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002248
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002249 if (tnum_is_const(reg->var_off)) {
2250 min_off = max_off = reg->var_off.value + reg->off;
2251 err = __check_stack_boundary(env, regno, min_off, access_size,
2252 zero_size_allowed);
2253 if (err)
2254 return err;
2255 } else {
Andrey Ignatov088ec262019-04-03 23:22:39 -07002256 /* Variable offset is prohibited for unprivileged mode for
2257 * simplicity since it requires corresponding support in
2258 * Spectre masking for stack ALU.
2259 * See also retrieve_ptr_limit().
2260 */
2261 if (!env->allow_ptr_leaks) {
2262 char tn_buf[48];
Edward Creef1174f72017-08-07 15:26:19 +01002263
Andrey Ignatov088ec262019-04-03 23:22:39 -07002264 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2265 verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n",
2266 regno, tn_buf);
2267 return -EACCES;
2268 }
Andrey Ignatovf2bcd052019-04-03 23:22:37 -07002269 /* Only initialized buffer on stack is allowed to be accessed
2270 * with variable offset. With uninitialized buffer it's hard to
2271 * guarantee that whole memory is marked as initialized on
2272 * helper return since specific bounds are unknown what may
2273 * cause uninitialized stack leaking.
2274 */
2275 if (meta && meta->raw_mode)
2276 meta = NULL;
2277
Andrey Ignatov107c26a72019-04-03 23:22:41 -07002278 if (reg->smax_value >= BPF_MAX_VAR_OFF ||
2279 reg->smax_value <= -BPF_MAX_VAR_OFF) {
2280 verbose(env, "R%d unbounded indirect variable offset stack access\n",
2281 regno);
2282 return -EACCES;
2283 }
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002284 min_off = reg->smin_value + reg->off;
Andrey Ignatov107c26a72019-04-03 23:22:41 -07002285 max_off = reg->smax_value + reg->off;
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002286 err = __check_stack_boundary(env, regno, min_off, access_size,
2287 zero_size_allowed);
Andrey Ignatov107c26a72019-04-03 23:22:41 -07002288 if (err) {
2289 verbose(env, "R%d min value is outside of stack bound\n",
2290 regno);
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002291 return err;
Andrey Ignatov107c26a72019-04-03 23:22:41 -07002292 }
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002293 err = __check_stack_boundary(env, regno, max_off, access_size,
2294 zero_size_allowed);
Andrey Ignatov107c26a72019-04-03 23:22:41 -07002295 if (err) {
2296 verbose(env, "R%d max value is outside of stack bound\n",
2297 regno);
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002298 return err;
Andrey Ignatov107c26a72019-04-03 23:22:41 -07002299 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002300 }
2301
Daniel Borkmann435faee12016-04-13 00:10:51 +02002302 if (meta && meta->raw_mode) {
2303 meta->access_size = access_size;
2304 meta->regno = regno;
2305 return 0;
2306 }
2307
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002308 for (i = min_off; i < max_off + access_size; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002309 u8 *stype;
2310
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002311 slot = -i - 1;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002312 spi = slot / BPF_REG_SIZE;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002313 if (state->allocated_stack <= slot)
2314 goto err;
2315 stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE];
2316 if (*stype == STACK_MISC)
2317 goto mark;
2318 if (*stype == STACK_ZERO) {
2319 /* helper can write anything into the stack */
2320 *stype = STACK_MISC;
2321 goto mark;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002322 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002323err:
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002324 if (tnum_is_const(reg->var_off)) {
2325 verbose(env, "invalid indirect read from stack off %d+%d size %d\n",
2326 min_off, i - min_off, access_size);
2327 } else {
2328 char tn_buf[48];
2329
2330 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2331 verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n",
2332 tn_buf, i - min_off, access_size);
2333 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002334 return -EACCES;
2335mark:
2336 /* reading any byte out of 8-byte 'spill_slot' will cause
2337 * the whole slot to be marked as 'read'
2338 */
Edward Cree679c7822018-08-22 20:02:19 +01002339 mark_reg_read(env, &state->stack[spi].spilled_ptr,
2340 state->stack[spi].spilled_ptr.parent);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002341 }
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002342 return update_stack_depth(env, state, min_off);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002343}
2344
Gianluca Borello06c1c042017-01-09 10:19:49 -08002345static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
2346 int access_size, bool zero_size_allowed,
2347 struct bpf_call_arg_meta *meta)
2348{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002349 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
Gianluca Borello06c1c042017-01-09 10:19:49 -08002350
Edward Creef1174f72017-08-07 15:26:19 +01002351 switch (reg->type) {
Gianluca Borello06c1c042017-01-09 10:19:49 -08002352 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002353 case PTR_TO_PACKET_META:
Yonghong Song9fd29c02017-11-12 14:49:09 -08002354 return check_packet_access(env, regno, reg->off, access_size,
2355 zero_size_allowed);
Gianluca Borello06c1c042017-01-09 10:19:49 -08002356 case PTR_TO_MAP_VALUE:
Daniel Borkmann591fe982019-04-09 23:20:05 +02002357 if (check_map_access_type(env, regno, reg->off, access_size,
2358 meta && meta->raw_mode ? BPF_WRITE :
2359 BPF_READ))
2360 return -EACCES;
Yonghong Song9fd29c02017-11-12 14:49:09 -08002361 return check_map_access(env, regno, reg->off, access_size,
2362 zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01002363 default: /* scalar_value|ptr_to_stack or invalid ptr */
Gianluca Borello06c1c042017-01-09 10:19:49 -08002364 return check_stack_boundary(env, regno, access_size,
2365 zero_size_allowed, meta);
2366 }
2367}
2368
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08002369/* Implementation details:
2370 * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL
2371 * Two bpf_map_lookups (even with the same key) will have different reg->id.
2372 * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after
2373 * value_or_null->value transition, since the verifier only cares about
2374 * the range of access to valid map value pointer and doesn't care about actual
2375 * address of the map element.
2376 * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps
2377 * reg->id > 0 after value_or_null->value transition. By doing so
2378 * two bpf_map_lookups will be considered two different pointers that
2379 * point to different bpf_spin_locks.
2380 * The verifier allows taking only one bpf_spin_lock at a time to avoid
2381 * dead-locks.
2382 * Since only one bpf_spin_lock is allowed the checks are simpler than
2383 * reg_is_refcounted() logic. The verifier needs to remember only
2384 * one spin_lock instead of array of acquired_refs.
2385 * cur_state->active_spin_lock remembers which map value element got locked
2386 * and clears it after bpf_spin_unlock.
2387 */
2388static int process_spin_lock(struct bpf_verifier_env *env, int regno,
2389 bool is_lock)
2390{
2391 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
2392 struct bpf_verifier_state *cur = env->cur_state;
2393 bool is_const = tnum_is_const(reg->var_off);
2394 struct bpf_map *map = reg->map_ptr;
2395 u64 val = reg->var_off.value;
2396
2397 if (reg->type != PTR_TO_MAP_VALUE) {
2398 verbose(env, "R%d is not a pointer to map_value\n", regno);
2399 return -EINVAL;
2400 }
2401 if (!is_const) {
2402 verbose(env,
2403 "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n",
2404 regno);
2405 return -EINVAL;
2406 }
2407 if (!map->btf) {
2408 verbose(env,
2409 "map '%s' has to have BTF in order to use bpf_spin_lock\n",
2410 map->name);
2411 return -EINVAL;
2412 }
2413 if (!map_value_has_spin_lock(map)) {
2414 if (map->spin_lock_off == -E2BIG)
2415 verbose(env,
2416 "map '%s' has more than one 'struct bpf_spin_lock'\n",
2417 map->name);
2418 else if (map->spin_lock_off == -ENOENT)
2419 verbose(env,
2420 "map '%s' doesn't have 'struct bpf_spin_lock'\n",
2421 map->name);
2422 else
2423 verbose(env,
2424 "map '%s' is not a struct type or bpf_spin_lock is mangled\n",
2425 map->name);
2426 return -EINVAL;
2427 }
2428 if (map->spin_lock_off != val + reg->off) {
2429 verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n",
2430 val + reg->off);
2431 return -EINVAL;
2432 }
2433 if (is_lock) {
2434 if (cur->active_spin_lock) {
2435 verbose(env,
2436 "Locking two bpf_spin_locks are not allowed\n");
2437 return -EINVAL;
2438 }
2439 cur->active_spin_lock = reg->id;
2440 } else {
2441 if (!cur->active_spin_lock) {
2442 verbose(env, "bpf_spin_unlock without taking a lock\n");
2443 return -EINVAL;
2444 }
2445 if (cur->active_spin_lock != reg->id) {
2446 verbose(env, "bpf_spin_unlock of different lock\n");
2447 return -EINVAL;
2448 }
2449 cur->active_spin_lock = 0;
2450 }
2451 return 0;
2452}
2453
Daniel Borkmann90133412018-01-20 01:24:29 +01002454static bool arg_type_is_mem_ptr(enum bpf_arg_type type)
2455{
2456 return type == ARG_PTR_TO_MEM ||
2457 type == ARG_PTR_TO_MEM_OR_NULL ||
2458 type == ARG_PTR_TO_UNINIT_MEM;
2459}
2460
2461static bool arg_type_is_mem_size(enum bpf_arg_type type)
2462{
2463 return type == ARG_CONST_SIZE ||
2464 type == ARG_CONST_SIZE_OR_ZERO;
2465}
2466
Andrey Ignatov57c3bb72019-03-18 16:57:10 -07002467static bool arg_type_is_int_ptr(enum bpf_arg_type type)
2468{
2469 return type == ARG_PTR_TO_INT ||
2470 type == ARG_PTR_TO_LONG;
2471}
2472
2473static int int_ptr_type_to_size(enum bpf_arg_type type)
2474{
2475 if (type == ARG_PTR_TO_INT)
2476 return sizeof(u32);
2477 else if (type == ARG_PTR_TO_LONG)
2478 return sizeof(u64);
2479
2480 return -EINVAL;
2481}
2482
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002483static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002484 enum bpf_arg_type arg_type,
2485 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002486{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002487 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002488 enum bpf_reg_type expected_type, type = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002489 int err = 0;
2490
Daniel Borkmann80f1d682015-03-12 17:21:42 +01002491 if (arg_type == ARG_DONTCARE)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002492 return 0;
2493
Edward Creedc503a82017-08-15 20:34:35 +01002494 err = check_reg_arg(env, regno, SRC_OP);
2495 if (err)
2496 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002497
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002498 if (arg_type == ARG_ANYTHING) {
2499 if (is_pointer_value(env, regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002500 verbose(env, "R%d leaks addr into helper function\n",
2501 regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002502 return -EACCES;
2503 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01002504 return 0;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002505 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01002506
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002507 if (type_is_pkt_pointer(type) &&
Thomas Graf3a0af8f2016-11-30 17:10:10 +01002508 !may_access_direct_pkt_data(env, meta, BPF_READ)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002509 verbose(env, "helper access to the packet is not allowed\n");
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002510 return -EACCES;
2511 }
2512
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002513 if (arg_type == ARG_PTR_TO_MAP_KEY ||
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002514 arg_type == ARG_PTR_TO_MAP_VALUE ||
2515 arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002516 expected_type = PTR_TO_STACK;
Paul Chaignond71962f2018-04-24 15:07:54 +02002517 if (!type_is_pkt_pointer(type) && type != PTR_TO_MAP_VALUE &&
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002518 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002519 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002520 } else if (arg_type == ARG_CONST_SIZE ||
2521 arg_type == ARG_CONST_SIZE_OR_ZERO) {
Edward Creef1174f72017-08-07 15:26:19 +01002522 expected_type = SCALAR_VALUE;
2523 if (type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002524 goto err_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002525 } else if (arg_type == ARG_CONST_MAP_PTR) {
2526 expected_type = CONST_PTR_TO_MAP;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002527 if (type != expected_type)
2528 goto err_type;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002529 } else if (arg_type == ARG_PTR_TO_CTX) {
2530 expected_type = PTR_TO_CTX;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002531 if (type != expected_type)
2532 goto err_type;
Daniel Borkmann58990d12018-06-07 17:40:03 +02002533 err = check_ctx_reg(env, reg, regno);
2534 if (err < 0)
2535 return err;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002536 } else if (arg_type == ARG_PTR_TO_SOCK_COMMON) {
2537 expected_type = PTR_TO_SOCK_COMMON;
2538 /* Any sk pointer can be ARG_PTR_TO_SOCK_COMMON */
2539 if (!type_is_sk_pointer(type))
2540 goto err_type;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002541 if (reg->ref_obj_id) {
2542 if (meta->ref_obj_id) {
2543 verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n",
2544 regno, reg->ref_obj_id,
2545 meta->ref_obj_id);
2546 return -EFAULT;
2547 }
2548 meta->ref_obj_id = reg->ref_obj_id;
Joe Stringerfd978bf72018-10-02 13:35:35 -07002549 }
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08002550 } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) {
2551 if (meta->func_id == BPF_FUNC_spin_lock) {
2552 if (process_spin_lock(env, regno, true))
2553 return -EACCES;
2554 } else if (meta->func_id == BPF_FUNC_spin_unlock) {
2555 if (process_spin_lock(env, regno, false))
2556 return -EACCES;
2557 } else {
2558 verbose(env, "verifier internal error\n");
2559 return -EFAULT;
2560 }
Daniel Borkmann90133412018-01-20 01:24:29 +01002561 } else if (arg_type_is_mem_ptr(arg_type)) {
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002562 expected_type = PTR_TO_STACK;
2563 /* One exception here. In case function allows for NULL to be
Edward Creef1174f72017-08-07 15:26:19 +01002564 * passed in as argument, it's a SCALAR_VALUE type. Final test
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002565 * happens during stack boundary checking.
2566 */
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002567 if (register_is_null(reg) &&
Gianluca Borellodb1ac492017-11-22 18:32:53 +00002568 arg_type == ARG_PTR_TO_MEM_OR_NULL)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002569 /* final test in check_stack_boundary() */;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002570 else if (!type_is_pkt_pointer(type) &&
2571 type != PTR_TO_MAP_VALUE &&
Edward Creef1174f72017-08-07 15:26:19 +01002572 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002573 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002574 meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM;
Andrey Ignatov57c3bb72019-03-18 16:57:10 -07002575 } else if (arg_type_is_int_ptr(arg_type)) {
2576 expected_type = PTR_TO_STACK;
2577 if (!type_is_pkt_pointer(type) &&
2578 type != PTR_TO_MAP_VALUE &&
2579 type != expected_type)
2580 goto err_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002581 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002582 verbose(env, "unsupported arg_type %d\n", arg_type);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002583 return -EFAULT;
2584 }
2585
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002586 if (arg_type == ARG_CONST_MAP_PTR) {
2587 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002588 meta->map_ptr = reg->map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002589 } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
2590 /* bpf_map_xxx(..., map_ptr, ..., key) call:
2591 * check that [key, key + map->key_size) are within
2592 * stack limits and initialized
2593 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002594 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002595 /* in function declaration map_ptr must come before
2596 * map_key, so that it's verified and known before
2597 * we have to check map_key here. Otherwise it means
2598 * that kernel subsystem misconfigured verifier
2599 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002600 verbose(env, "invalid map_ptr to access map->key\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002601 return -EACCES;
2602 }
Paul Chaignond71962f2018-04-24 15:07:54 +02002603 err = check_helper_mem_access(env, regno,
2604 meta->map_ptr->key_size, false,
2605 NULL);
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002606 } else if (arg_type == ARG_PTR_TO_MAP_VALUE ||
2607 arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002608 /* bpf_map_xxx(..., map_ptr, ..., value) call:
2609 * check [value, value + map->value_size) validity
2610 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002611 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002612 /* kernel subsystem misconfigured verifier */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002613 verbose(env, "invalid map_ptr to access map->value\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002614 return -EACCES;
2615 }
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002616 meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE);
Paul Chaignond71962f2018-04-24 15:07:54 +02002617 err = check_helper_mem_access(env, regno,
2618 meta->map_ptr->value_size, false,
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002619 meta);
Daniel Borkmann90133412018-01-20 01:24:29 +01002620 } else if (arg_type_is_mem_size(arg_type)) {
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002621 bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002622
Yonghong Song849fa502018-04-28 22:28:09 -07002623 /* remember the mem_size which may be used later
2624 * to refine return values.
2625 */
2626 meta->msize_smax_value = reg->smax_value;
2627 meta->msize_umax_value = reg->umax_value;
2628
Edward Creef1174f72017-08-07 15:26:19 +01002629 /* The register is SCALAR_VALUE; the access check
2630 * happens using its boundaries.
Gianluca Borello06c1c042017-01-09 10:19:49 -08002631 */
Edward Creef1174f72017-08-07 15:26:19 +01002632 if (!tnum_is_const(reg->var_off))
Gianluca Borello06c1c042017-01-09 10:19:49 -08002633 /* For unprivileged variable accesses, disable raw
2634 * mode so that the program is required to
2635 * initialize all the memory that the helper could
2636 * just partially fill up.
2637 */
2638 meta = NULL;
2639
Edward Creeb03c9f92017-08-07 15:26:36 +01002640 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002641 verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n",
Edward Creef1174f72017-08-07 15:26:19 +01002642 regno);
2643 return -EACCES;
2644 }
Gianluca Borello06c1c042017-01-09 10:19:49 -08002645
Edward Creeb03c9f92017-08-07 15:26:36 +01002646 if (reg->umin_value == 0) {
Edward Creef1174f72017-08-07 15:26:19 +01002647 err = check_helper_mem_access(env, regno - 1, 0,
2648 zero_size_allowed,
2649 meta);
Gianluca Borello06c1c042017-01-09 10:19:49 -08002650 if (err)
2651 return err;
Gianluca Borello06c1c042017-01-09 10:19:49 -08002652 }
Edward Creef1174f72017-08-07 15:26:19 +01002653
Edward Creeb03c9f92017-08-07 15:26:36 +01002654 if (reg->umax_value >= BPF_MAX_VAR_SIZ) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002655 verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
Edward Creef1174f72017-08-07 15:26:19 +01002656 regno);
2657 return -EACCES;
2658 }
2659 err = check_helper_mem_access(env, regno - 1,
Edward Creeb03c9f92017-08-07 15:26:36 +01002660 reg->umax_value,
Edward Creef1174f72017-08-07 15:26:19 +01002661 zero_size_allowed, meta);
Andrey Ignatov57c3bb72019-03-18 16:57:10 -07002662 } else if (arg_type_is_int_ptr(arg_type)) {
2663 int size = int_ptr_type_to_size(arg_type);
2664
2665 err = check_helper_mem_access(env, regno, size, false, meta);
2666 if (err)
2667 return err;
2668 err = check_ptr_alignment(env, reg, 0, size, true);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002669 }
2670
2671 return err;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002672err_type:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002673 verbose(env, "R%d type=%s expected=%s\n", regno,
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002674 reg_type_str[type], reg_type_str[expected_type]);
2675 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002676}
2677
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002678static int check_map_func_compatibility(struct bpf_verifier_env *env,
2679 struct bpf_map *map, int func_id)
Kaixu Xia35578d72015-08-06 07:02:35 +00002680{
Kaixu Xia35578d72015-08-06 07:02:35 +00002681 if (!map)
2682 return 0;
2683
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002684 /* We need a two way check, first is from map perspective ... */
2685 switch (map->map_type) {
2686 case BPF_MAP_TYPE_PROG_ARRAY:
2687 if (func_id != BPF_FUNC_tail_call)
2688 goto error;
2689 break;
2690 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
2691 if (func_id != BPF_FUNC_perf_event_read &&
Yonghong Song908432c2017-10-05 09:19:20 -07002692 func_id != BPF_FUNC_perf_event_output &&
2693 func_id != BPF_FUNC_perf_event_read_value)
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002694 goto error;
2695 break;
2696 case BPF_MAP_TYPE_STACK_TRACE:
2697 if (func_id != BPF_FUNC_get_stackid)
2698 goto error;
2699 break;
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -07002700 case BPF_MAP_TYPE_CGROUP_ARRAY:
David S. Miller60747ef2016-08-18 01:17:32 -04002701 if (func_id != BPF_FUNC_skb_under_cgroup &&
Sargun Dhillon60d20f92016-08-12 08:56:52 -07002702 func_id != BPF_FUNC_current_task_under_cgroup)
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002703 goto error;
2704 break;
Roman Gushchincd339432018-08-02 14:27:24 -07002705 case BPF_MAP_TYPE_CGROUP_STORAGE:
Roman Gushchinb741f162018-09-28 14:45:43 +00002706 case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE:
Roman Gushchincd339432018-08-02 14:27:24 -07002707 if (func_id != BPF_FUNC_get_local_storage)
2708 goto error;
2709 break;
John Fastabend546ac1f2017-07-17 09:28:56 -07002710 /* devmap returns a pointer to a live net_device ifindex that we cannot
2711 * allow to be modified from bpf side. So do not allow lookup elements
2712 * for now.
2713 */
2714 case BPF_MAP_TYPE_DEVMAP:
John Fastabend2ddf71e2017-07-17 09:30:02 -07002715 if (func_id != BPF_FUNC_redirect_map)
John Fastabend546ac1f2017-07-17 09:28:56 -07002716 goto error;
2717 break;
Björn Töpelfbfc504a2018-05-02 13:01:28 +02002718 /* Restrict bpf side of cpumap and xskmap, open when use-cases
2719 * appear.
2720 */
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02002721 case BPF_MAP_TYPE_CPUMAP:
Björn Töpelfbfc504a2018-05-02 13:01:28 +02002722 case BPF_MAP_TYPE_XSKMAP:
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02002723 if (func_id != BPF_FUNC_redirect_map)
2724 goto error;
2725 break;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07002726 case BPF_MAP_TYPE_ARRAY_OF_MAPS:
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07002727 case BPF_MAP_TYPE_HASH_OF_MAPS:
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07002728 if (func_id != BPF_FUNC_map_lookup_elem)
2729 goto error;
Martin KaFai Lau16a43622017-08-17 18:14:43 -07002730 break;
John Fastabend174a79f2017-08-15 22:32:47 -07002731 case BPF_MAP_TYPE_SOCKMAP:
2732 if (func_id != BPF_FUNC_sk_redirect_map &&
2733 func_id != BPF_FUNC_sock_map_update &&
John Fastabend4f738ad2018-03-18 12:57:10 -07002734 func_id != BPF_FUNC_map_delete_elem &&
2735 func_id != BPF_FUNC_msg_redirect_map)
John Fastabend174a79f2017-08-15 22:32:47 -07002736 goto error;
2737 break;
John Fastabend81110382018-05-14 10:00:17 -07002738 case BPF_MAP_TYPE_SOCKHASH:
2739 if (func_id != BPF_FUNC_sk_redirect_hash &&
2740 func_id != BPF_FUNC_sock_hash_update &&
2741 func_id != BPF_FUNC_map_delete_elem &&
2742 func_id != BPF_FUNC_msg_redirect_hash)
2743 goto error;
2744 break;
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07002745 case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY:
2746 if (func_id != BPF_FUNC_sk_select_reuseport)
2747 goto error;
2748 break;
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02002749 case BPF_MAP_TYPE_QUEUE:
2750 case BPF_MAP_TYPE_STACK:
2751 if (func_id != BPF_FUNC_map_peek_elem &&
2752 func_id != BPF_FUNC_map_pop_elem &&
2753 func_id != BPF_FUNC_map_push_elem)
2754 goto error;
2755 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002756 default:
2757 break;
2758 }
2759
2760 /* ... and second from the function itself. */
2761 switch (func_id) {
2762 case BPF_FUNC_tail_call:
2763 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
2764 goto error;
Jiong Wangf910cef2018-05-02 16:17:17 -04002765 if (env->subprog_cnt > 1) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002766 verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n");
2767 return -EINVAL;
2768 }
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002769 break;
2770 case BPF_FUNC_perf_event_read:
2771 case BPF_FUNC_perf_event_output:
Yonghong Song908432c2017-10-05 09:19:20 -07002772 case BPF_FUNC_perf_event_read_value:
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002773 if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
2774 goto error;
2775 break;
2776 case BPF_FUNC_get_stackid:
2777 if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
2778 goto error;
2779 break;
Sargun Dhillon60d20f92016-08-12 08:56:52 -07002780 case BPF_FUNC_current_task_under_cgroup:
Daniel Borkmann747ea552016-08-12 22:17:17 +02002781 case BPF_FUNC_skb_under_cgroup:
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002782 if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
2783 goto error;
2784 break;
John Fastabend97f91a72017-07-17 09:29:18 -07002785 case BPF_FUNC_redirect_map:
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02002786 if (map->map_type != BPF_MAP_TYPE_DEVMAP &&
Björn Töpelfbfc504a2018-05-02 13:01:28 +02002787 map->map_type != BPF_MAP_TYPE_CPUMAP &&
2788 map->map_type != BPF_MAP_TYPE_XSKMAP)
John Fastabend97f91a72017-07-17 09:29:18 -07002789 goto error;
2790 break;
John Fastabend174a79f2017-08-15 22:32:47 -07002791 case BPF_FUNC_sk_redirect_map:
John Fastabend4f738ad2018-03-18 12:57:10 -07002792 case BPF_FUNC_msg_redirect_map:
John Fastabend81110382018-05-14 10:00:17 -07002793 case BPF_FUNC_sock_map_update:
John Fastabend174a79f2017-08-15 22:32:47 -07002794 if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
2795 goto error;
2796 break;
John Fastabend81110382018-05-14 10:00:17 -07002797 case BPF_FUNC_sk_redirect_hash:
2798 case BPF_FUNC_msg_redirect_hash:
2799 case BPF_FUNC_sock_hash_update:
2800 if (map->map_type != BPF_MAP_TYPE_SOCKHASH)
John Fastabend174a79f2017-08-15 22:32:47 -07002801 goto error;
2802 break;
Roman Gushchincd339432018-08-02 14:27:24 -07002803 case BPF_FUNC_get_local_storage:
Roman Gushchinb741f162018-09-28 14:45:43 +00002804 if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
2805 map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Roman Gushchincd339432018-08-02 14:27:24 -07002806 goto error;
2807 break;
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07002808 case BPF_FUNC_sk_select_reuseport:
2809 if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY)
2810 goto error;
2811 break;
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02002812 case BPF_FUNC_map_peek_elem:
2813 case BPF_FUNC_map_pop_elem:
2814 case BPF_FUNC_map_push_elem:
2815 if (map->map_type != BPF_MAP_TYPE_QUEUE &&
2816 map->map_type != BPF_MAP_TYPE_STACK)
2817 goto error;
2818 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002819 default:
2820 break;
Kaixu Xia35578d72015-08-06 07:02:35 +00002821 }
2822
2823 return 0;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002824error:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002825 verbose(env, "cannot pass map_type %d into func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02002826 map->map_type, func_id_name(func_id), func_id);
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002827 return -EINVAL;
Kaixu Xia35578d72015-08-06 07:02:35 +00002828}
2829
Daniel Borkmann90133412018-01-20 01:24:29 +01002830static bool check_raw_mode_ok(const struct bpf_func_proto *fn)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002831{
2832 int count = 0;
2833
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002834 if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002835 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002836 if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002837 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002838 if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002839 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002840 if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002841 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002842 if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002843 count++;
2844
Daniel Borkmann90133412018-01-20 01:24:29 +01002845 /* We only support one arg being in raw mode at the moment,
2846 * which is sufficient for the helper functions we have
2847 * right now.
2848 */
2849 return count <= 1;
2850}
2851
2852static bool check_args_pair_invalid(enum bpf_arg_type arg_curr,
2853 enum bpf_arg_type arg_next)
2854{
2855 return (arg_type_is_mem_ptr(arg_curr) &&
2856 !arg_type_is_mem_size(arg_next)) ||
2857 (!arg_type_is_mem_ptr(arg_curr) &&
2858 arg_type_is_mem_size(arg_next));
2859}
2860
2861static bool check_arg_pair_ok(const struct bpf_func_proto *fn)
2862{
2863 /* bpf_xxx(..., buf, len) call will access 'len'
2864 * bytes from memory 'buf'. Both arg types need
2865 * to be paired, so make sure there's no buggy
2866 * helper function specification.
2867 */
2868 if (arg_type_is_mem_size(fn->arg1_type) ||
2869 arg_type_is_mem_ptr(fn->arg5_type) ||
2870 check_args_pair_invalid(fn->arg1_type, fn->arg2_type) ||
2871 check_args_pair_invalid(fn->arg2_type, fn->arg3_type) ||
2872 check_args_pair_invalid(fn->arg3_type, fn->arg4_type) ||
2873 check_args_pair_invalid(fn->arg4_type, fn->arg5_type))
2874 return false;
2875
2876 return true;
2877}
2878
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002879static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07002880{
2881 int count = 0;
2882
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002883 if (arg_type_may_be_refcounted(fn->arg1_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07002884 count++;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002885 if (arg_type_may_be_refcounted(fn->arg2_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07002886 count++;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002887 if (arg_type_may_be_refcounted(fn->arg3_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07002888 count++;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002889 if (arg_type_may_be_refcounted(fn->arg4_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07002890 count++;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002891 if (arg_type_may_be_refcounted(fn->arg5_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07002892 count++;
2893
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002894 /* A reference acquiring function cannot acquire
2895 * another refcounted ptr.
2896 */
2897 if (is_acquire_function(func_id) && count)
2898 return false;
2899
Joe Stringerfd978bf72018-10-02 13:35:35 -07002900 /* We only support one arg being unreferenced at the moment,
2901 * which is sufficient for the helper functions we have right now.
2902 */
2903 return count <= 1;
2904}
2905
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002906static int check_func_proto(const struct bpf_func_proto *fn, int func_id)
Daniel Borkmann90133412018-01-20 01:24:29 +01002907{
2908 return check_raw_mode_ok(fn) &&
Joe Stringerfd978bf72018-10-02 13:35:35 -07002909 check_arg_pair_ok(fn) &&
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002910 check_refcount_ok(fn, func_id) ? 0 : -EINVAL;
Daniel Borkmann435faee12016-04-13 00:10:51 +02002911}
2912
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002913/* Packet data might have moved, any old PTR_TO_PACKET[_META,_END]
2914 * are now invalid, so turn them into unknown SCALAR_VALUE.
Edward Creef1174f72017-08-07 15:26:19 +01002915 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002916static void __clear_all_pkt_pointers(struct bpf_verifier_env *env,
2917 struct bpf_func_state *state)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002918{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002919 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002920 int i;
2921
2922 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002923 if (reg_is_pkt_pointer_any(&regs[i]))
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002924 mark_reg_unknown(env, regs, i);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002925
Joe Stringerf3709f62018-10-02 13:35:29 -07002926 bpf_for_each_spilled_reg(i, state, reg) {
2927 if (!reg)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002928 continue;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002929 if (reg_is_pkt_pointer_any(reg))
2930 __mark_reg_unknown(reg);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002931 }
2932}
2933
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002934static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
2935{
2936 struct bpf_verifier_state *vstate = env->cur_state;
2937 int i;
2938
2939 for (i = 0; i <= vstate->curframe; i++)
2940 __clear_all_pkt_pointers(env, vstate->frame[i]);
2941}
2942
Joe Stringerfd978bf72018-10-02 13:35:35 -07002943static void release_reg_references(struct bpf_verifier_env *env,
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002944 struct bpf_func_state *state,
2945 int ref_obj_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07002946{
2947 struct bpf_reg_state *regs = state->regs, *reg;
2948 int i;
2949
2950 for (i = 0; i < MAX_BPF_REG; i++)
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002951 if (regs[i].ref_obj_id == ref_obj_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07002952 mark_reg_unknown(env, regs, i);
2953
2954 bpf_for_each_spilled_reg(i, state, reg) {
2955 if (!reg)
2956 continue;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002957 if (reg->ref_obj_id == ref_obj_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07002958 __mark_reg_unknown(reg);
2959 }
2960}
2961
2962/* The pointer with the specified id has released its reference to kernel
2963 * resources. Identify all copies of the same pointer and clear the reference.
2964 */
2965static int release_reference(struct bpf_verifier_env *env,
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002966 int ref_obj_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07002967{
2968 struct bpf_verifier_state *vstate = env->cur_state;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002969 int err;
Joe Stringerfd978bf72018-10-02 13:35:35 -07002970 int i;
2971
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002972 err = release_reference_state(cur_func(env), ref_obj_id);
2973 if (err)
2974 return err;
Joe Stringerfd978bf72018-10-02 13:35:35 -07002975
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002976 for (i = 0; i <= vstate->curframe; i++)
2977 release_reg_references(env, vstate->frame[i], ref_obj_id);
2978
2979 return 0;
Joe Stringerfd978bf72018-10-02 13:35:35 -07002980}
2981
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002982static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
2983 int *insn_idx)
2984{
2985 struct bpf_verifier_state *state = env->cur_state;
2986 struct bpf_func_state *caller, *callee;
Joe Stringerfd978bf72018-10-02 13:35:35 -07002987 int i, err, subprog, target_insn;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002988
Alexei Starovoitovaada9ce2017-12-25 13:15:42 -08002989 if (state->curframe + 1 >= MAX_CALL_FRAMES) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002990 verbose(env, "the call stack of %d frames is too deep\n",
Alexei Starovoitovaada9ce2017-12-25 13:15:42 -08002991 state->curframe + 2);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002992 return -E2BIG;
2993 }
2994
2995 target_insn = *insn_idx + insn->imm;
2996 subprog = find_subprog(env, target_insn + 1);
2997 if (subprog < 0) {
2998 verbose(env, "verifier bug. No program starts at insn %d\n",
2999 target_insn + 1);
3000 return -EFAULT;
3001 }
3002
3003 caller = state->frame[state->curframe];
3004 if (state->frame[state->curframe + 1]) {
3005 verbose(env, "verifier bug. Frame %d already allocated\n",
3006 state->curframe + 1);
3007 return -EFAULT;
3008 }
3009
3010 callee = kzalloc(sizeof(*callee), GFP_KERNEL);
3011 if (!callee)
3012 return -ENOMEM;
3013 state->frame[state->curframe + 1] = callee;
3014
3015 /* callee cannot access r0, r6 - r9 for reading and has to write
3016 * into its own stack before reading from it.
3017 * callee can read/write into caller's stack
3018 */
3019 init_func_state(env, callee,
3020 /* remember the callsite, it will be used by bpf_exit */
3021 *insn_idx /* callsite */,
3022 state->curframe + 1 /* frameno within this callchain */,
Jiong Wangf910cef2018-05-02 16:17:17 -04003023 subprog /* subprog number within this prog */);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003024
Joe Stringerfd978bf72018-10-02 13:35:35 -07003025 /* Transfer references to the callee */
3026 err = transfer_reference_state(callee, caller);
3027 if (err)
3028 return err;
3029
Edward Cree679c7822018-08-22 20:02:19 +01003030 /* copy r1 - r5 args that callee can access. The copy includes parent
3031 * pointers, which connects us up to the liveness chain
3032 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003033 for (i = BPF_REG_1; i <= BPF_REG_5; i++)
3034 callee->regs[i] = caller->regs[i];
3035
Edward Cree679c7822018-08-22 20:02:19 +01003036 /* after the call registers r0 - r5 were scratched */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003037 for (i = 0; i < CALLER_SAVED_REGS; i++) {
3038 mark_reg_not_init(env, caller->regs, caller_saved[i]);
3039 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
3040 }
3041
3042 /* only increment it after check_reg_arg() finished */
3043 state->curframe++;
3044
3045 /* and go analyze first insn of the callee */
3046 *insn_idx = target_insn;
3047
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07003048 if (env->log.level & BPF_LOG_LEVEL) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003049 verbose(env, "caller:\n");
3050 print_verifier_state(env, caller);
3051 verbose(env, "callee:\n");
3052 print_verifier_state(env, callee);
3053 }
3054 return 0;
3055}
3056
3057static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
3058{
3059 struct bpf_verifier_state *state = env->cur_state;
3060 struct bpf_func_state *caller, *callee;
3061 struct bpf_reg_state *r0;
Joe Stringerfd978bf72018-10-02 13:35:35 -07003062 int err;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003063
3064 callee = state->frame[state->curframe];
3065 r0 = &callee->regs[BPF_REG_0];
3066 if (r0->type == PTR_TO_STACK) {
3067 /* technically it's ok to return caller's stack pointer
3068 * (or caller's caller's pointer) back to the caller,
3069 * since these pointers are valid. Only current stack
3070 * pointer will be invalid as soon as function exits,
3071 * but let's be conservative
3072 */
3073 verbose(env, "cannot return stack pointer to the caller\n");
3074 return -EINVAL;
3075 }
3076
3077 state->curframe--;
3078 caller = state->frame[state->curframe];
3079 /* return to the caller whatever r0 had in the callee */
3080 caller->regs[BPF_REG_0] = *r0;
3081
Joe Stringerfd978bf72018-10-02 13:35:35 -07003082 /* Transfer references to the caller */
3083 err = transfer_reference_state(caller, callee);
3084 if (err)
3085 return err;
3086
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003087 *insn_idx = callee->callsite + 1;
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07003088 if (env->log.level & BPF_LOG_LEVEL) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003089 verbose(env, "returning from callee:\n");
3090 print_verifier_state(env, callee);
3091 verbose(env, "to caller at %d:\n", *insn_idx);
3092 print_verifier_state(env, caller);
3093 }
3094 /* clear everything in the callee */
3095 free_func_state(callee);
3096 state->frame[state->curframe + 1] = NULL;
3097 return 0;
3098}
3099
Yonghong Song849fa502018-04-28 22:28:09 -07003100static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type,
3101 int func_id,
3102 struct bpf_call_arg_meta *meta)
3103{
3104 struct bpf_reg_state *ret_reg = &regs[BPF_REG_0];
3105
3106 if (ret_type != RET_INTEGER ||
3107 (func_id != BPF_FUNC_get_stack &&
3108 func_id != BPF_FUNC_probe_read_str))
3109 return;
3110
3111 ret_reg->smax_value = meta->msize_smax_value;
3112 ret_reg->umax_value = meta->msize_umax_value;
3113 __reg_deduce_bounds(ret_reg);
3114 __reg_bound_offset(ret_reg);
3115}
3116
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003117static int
3118record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,
3119 int func_id, int insn_idx)
3120{
3121 struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx];
Daniel Borkmann591fe982019-04-09 23:20:05 +02003122 struct bpf_map *map = meta->map_ptr;
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003123
3124 if (func_id != BPF_FUNC_tail_call &&
Daniel Borkmann09772d92018-06-02 23:06:35 +02003125 func_id != BPF_FUNC_map_lookup_elem &&
3126 func_id != BPF_FUNC_map_update_elem &&
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02003127 func_id != BPF_FUNC_map_delete_elem &&
3128 func_id != BPF_FUNC_map_push_elem &&
3129 func_id != BPF_FUNC_map_pop_elem &&
3130 func_id != BPF_FUNC_map_peek_elem)
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003131 return 0;
Daniel Borkmann09772d92018-06-02 23:06:35 +02003132
Daniel Borkmann591fe982019-04-09 23:20:05 +02003133 if (map == NULL) {
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003134 verbose(env, "kernel subsystem misconfigured verifier\n");
3135 return -EINVAL;
3136 }
3137
Daniel Borkmann591fe982019-04-09 23:20:05 +02003138 /* In case of read-only, some additional restrictions
3139 * need to be applied in order to prevent altering the
3140 * state of the map from program side.
3141 */
3142 if ((map->map_flags & BPF_F_RDONLY_PROG) &&
3143 (func_id == BPF_FUNC_map_delete_elem ||
3144 func_id == BPF_FUNC_map_update_elem ||
3145 func_id == BPF_FUNC_map_push_elem ||
3146 func_id == BPF_FUNC_map_pop_elem)) {
3147 verbose(env, "write into map forbidden\n");
3148 return -EACCES;
3149 }
3150
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003151 if (!BPF_MAP_PTR(aux->map_state))
3152 bpf_map_ptr_store(aux, meta->map_ptr,
3153 meta->map_ptr->unpriv_array);
3154 else if (BPF_MAP_PTR(aux->map_state) != meta->map_ptr)
3155 bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON,
3156 meta->map_ptr->unpriv_array);
3157 return 0;
3158}
3159
Joe Stringerfd978bf72018-10-02 13:35:35 -07003160static int check_reference_leak(struct bpf_verifier_env *env)
3161{
3162 struct bpf_func_state *state = cur_func(env);
3163 int i;
3164
3165 for (i = 0; i < state->acquired_refs; i++) {
3166 verbose(env, "Unreleased reference id=%d alloc_insn=%d\n",
3167 state->refs[i].id, state->refs[i].insn_idx);
3168 }
3169 return state->acquired_refs ? -EINVAL : 0;
3170}
3171
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003172static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003173{
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003174 const struct bpf_func_proto *fn = NULL;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003175 struct bpf_reg_state *regs;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003176 struct bpf_call_arg_meta meta;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003177 bool changes_data;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003178 int i, err;
3179
3180 /* find function prototype */
3181 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003182 verbose(env, "invalid func %s#%d\n", func_id_name(func_id),
3183 func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003184 return -EINVAL;
3185 }
3186
Jakub Kicinski00176a32017-10-16 16:40:54 -07003187 if (env->ops->get_func_proto)
Andrey Ignatov5e43f892018-03-30 15:08:00 -07003188 fn = env->ops->get_func_proto(func_id, env->prog);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003189 if (!fn) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003190 verbose(env, "unknown func %s#%d\n", func_id_name(func_id),
3191 func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003192 return -EINVAL;
3193 }
3194
3195 /* eBPF programs must be GPL compatible to use GPL-ed functions */
Daniel Borkmann24701ec2015-03-01 12:31:47 +01003196 if (!env->prog->gpl_compatible && fn->gpl_only) {
Daniel Borkmann3fe28672018-06-02 23:06:33 +02003197 verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003198 return -EINVAL;
3199 }
3200
Daniel Borkmann04514d12017-12-14 21:07:25 +01003201 /* With LD_ABS/IND some JITs save/restore skb from r1. */
Martin KaFai Lau17bedab2016-12-07 15:53:11 -08003202 changes_data = bpf_helper_changes_pkt_data(fn->func);
Daniel Borkmann04514d12017-12-14 21:07:25 +01003203 if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) {
3204 verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n",
3205 func_id_name(func_id), func_id);
3206 return -EINVAL;
3207 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003208
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003209 memset(&meta, 0, sizeof(meta));
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003210 meta.pkt_access = fn->pkt_access;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003211
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003212 err = check_func_proto(fn, func_id);
Daniel Borkmann435faee12016-04-13 00:10:51 +02003213 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003214 verbose(env, "kernel subsystem misconfigured func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02003215 func_id_name(func_id), func_id);
Daniel Borkmann435faee12016-04-13 00:10:51 +02003216 return err;
3217 }
3218
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08003219 meta.func_id = func_id;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003220 /* check args */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003221 err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003222 if (err)
3223 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003224 err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003225 if (err)
3226 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003227 err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003228 if (err)
3229 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003230 err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003231 if (err)
3232 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003233 err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003234 if (err)
3235 return err;
3236
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003237 err = record_func_map(env, &meta, func_id, insn_idx);
3238 if (err)
3239 return err;
3240
Daniel Borkmann435faee12016-04-13 00:10:51 +02003241 /* Mark slots with STACK_MISC in case of raw mode, stack offset
3242 * is inferred from register state.
3243 */
3244 for (i = 0; i < meta.access_size; i++) {
Daniel Borkmannca369602018-02-23 22:29:05 +01003245 err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B,
3246 BPF_WRITE, -1, false);
Daniel Borkmann435faee12016-04-13 00:10:51 +02003247 if (err)
3248 return err;
3249 }
3250
Joe Stringerfd978bf72018-10-02 13:35:35 -07003251 if (func_id == BPF_FUNC_tail_call) {
3252 err = check_reference_leak(env);
3253 if (err) {
3254 verbose(env, "tail_call would lead to reference leak\n");
3255 return err;
3256 }
3257 } else if (is_release_function(func_id)) {
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003258 err = release_reference(env, meta.ref_obj_id);
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08003259 if (err) {
3260 verbose(env, "func %s#%d reference has not been acquired before\n",
3261 func_id_name(func_id), func_id);
Joe Stringerfd978bf72018-10-02 13:35:35 -07003262 return err;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08003263 }
Joe Stringerfd978bf72018-10-02 13:35:35 -07003264 }
3265
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003266 regs = cur_regs(env);
Roman Gushchincd339432018-08-02 14:27:24 -07003267
3268 /* check that flags argument in get_local_storage(map, flags) is 0,
3269 * this is required because get_local_storage() can't return an error.
3270 */
3271 if (func_id == BPF_FUNC_get_local_storage &&
3272 !register_is_null(&regs[BPF_REG_2])) {
3273 verbose(env, "get_local_storage() doesn't support non-zero flags\n");
3274 return -EINVAL;
3275 }
3276
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003277 /* reset caller saved regs */
Edward Creedc503a82017-08-15 20:34:35 +01003278 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003279 mark_reg_not_init(env, regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01003280 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
3281 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003282
Edward Creedc503a82017-08-15 20:34:35 +01003283 /* update return register (already marked as written above) */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003284 if (fn->ret_type == RET_INTEGER) {
Edward Creef1174f72017-08-07 15:26:19 +01003285 /* sets type to SCALAR_VALUE */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003286 mark_reg_unknown(env, regs, BPF_REG_0);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003287 } else if (fn->ret_type == RET_VOID) {
3288 regs[BPF_REG_0].type = NOT_INIT;
Roman Gushchin3e6a4b32018-08-02 14:27:22 -07003289 } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL ||
3290 fn->ret_type == RET_PTR_TO_MAP_VALUE) {
Edward Creef1174f72017-08-07 15:26:19 +01003291 /* There is no offset yet applied, variable or fixed */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003292 mark_reg_known_zero(env, regs, BPF_REG_0);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003293 /* remember map_ptr, so that check_map_access()
3294 * can check 'value_size' boundary of memory access
3295 * to map element returned from bpf_map_lookup_elem()
3296 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003297 if (meta.map_ptr == NULL) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003298 verbose(env,
3299 "kernel subsystem misconfigured verifier\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003300 return -EINVAL;
3301 }
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003302 regs[BPF_REG_0].map_ptr = meta.map_ptr;
Daniel Borkmann4d31f302018-11-01 00:05:53 +01003303 if (fn->ret_type == RET_PTR_TO_MAP_VALUE) {
3304 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE;
Alexei Starovoitove16d2f12019-01-31 15:40:05 -08003305 if (map_value_has_spin_lock(meta.map_ptr))
3306 regs[BPF_REG_0].id = ++env->id_gen;
Daniel Borkmann4d31f302018-11-01 00:05:53 +01003307 } else {
3308 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
3309 regs[BPF_REG_0].id = ++env->id_gen;
3310 }
Joe Stringerc64b7982018-10-02 13:35:33 -07003311 } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) {
3312 mark_reg_known_zero(env, regs, BPF_REG_0);
3313 regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL;
Lorenz Bauer0f3adc22019-03-22 09:53:59 +08003314 regs[BPF_REG_0].id = ++env->id_gen;
Lorenz Bauer85a51f82019-03-22 09:54:00 +08003315 } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) {
3316 mark_reg_known_zero(env, regs, BPF_REG_0);
3317 regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL;
3318 regs[BPF_REG_0].id = ++env->id_gen;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08003319 } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) {
3320 mark_reg_known_zero(env, regs, BPF_REG_0);
3321 regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL;
3322 regs[BPF_REG_0].id = ++env->id_gen;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003323 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003324 verbose(env, "unknown return type %d of func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02003325 fn->ret_type, func_id_name(func_id), func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003326 return -EINVAL;
3327 }
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07003328
Lorenz Bauer0f3adc22019-03-22 09:53:59 +08003329 if (is_ptr_cast_function(func_id)) {
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003330 /* For release_reference() */
3331 regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id;
Lorenz Bauer0f3adc22019-03-22 09:53:59 +08003332 } else if (is_acquire_function(func_id)) {
3333 int id = acquire_reference_state(env, insn_idx);
3334
3335 if (id < 0)
3336 return id;
3337 /* For mark_ptr_or_null_reg() */
3338 regs[BPF_REG_0].id = id;
3339 /* For release_reference() */
3340 regs[BPF_REG_0].ref_obj_id = id;
3341 }
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003342
Yonghong Song849fa502018-04-28 22:28:09 -07003343 do_refine_retval_range(regs, fn->ret_type, func_id, &meta);
3344
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003345 err = check_map_func_compatibility(env, meta.map_ptr, func_id);
Kaixu Xia35578d72015-08-06 07:02:35 +00003346 if (err)
3347 return err;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07003348
Yonghong Songc195651e2018-04-28 22:28:08 -07003349 if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) {
3350 const char *err_str;
3351
3352#ifdef CONFIG_PERF_EVENTS
3353 err = get_callchain_buffers(sysctl_perf_event_max_stack);
3354 err_str = "cannot get callchain buffer for func %s#%d\n";
3355#else
3356 err = -ENOTSUPP;
3357 err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n";
3358#endif
3359 if (err) {
3360 verbose(env, err_str, func_id_name(func_id), func_id);
3361 return err;
3362 }
3363
3364 env->prog->has_callchain_buf = true;
3365 }
3366
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003367 if (changes_data)
3368 clear_all_pkt_pointers(env);
3369 return 0;
3370}
3371
Edward Creeb03c9f92017-08-07 15:26:36 +01003372static bool signed_add_overflows(s64 a, s64 b)
3373{
3374 /* Do the add in u64, where overflow is well-defined */
3375 s64 res = (s64)((u64)a + (u64)b);
3376
3377 if (b < 0)
3378 return res > a;
3379 return res < a;
3380}
3381
3382static bool signed_sub_overflows(s64 a, s64 b)
3383{
3384 /* Do the sub in u64, where overflow is well-defined */
3385 s64 res = (s64)((u64)a - (u64)b);
3386
3387 if (b < 0)
3388 return res < a;
3389 return res > a;
David S. Millerd1174412017-05-10 11:22:52 -07003390}
3391
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08003392static bool check_reg_sane_offset(struct bpf_verifier_env *env,
3393 const struct bpf_reg_state *reg,
3394 enum bpf_reg_type type)
3395{
3396 bool known = tnum_is_const(reg->var_off);
3397 s64 val = reg->var_off.value;
3398 s64 smin = reg->smin_value;
3399
3400 if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) {
3401 verbose(env, "math between %s pointer and %lld is not allowed\n",
3402 reg_type_str[type], val);
3403 return false;
3404 }
3405
3406 if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) {
3407 verbose(env, "%s pointer offset %d is not allowed\n",
3408 reg_type_str[type], reg->off);
3409 return false;
3410 }
3411
3412 if (smin == S64_MIN) {
3413 verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n",
3414 reg_type_str[type]);
3415 return false;
3416 }
3417
3418 if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) {
3419 verbose(env, "value %lld makes %s pointer be out of bounds\n",
3420 smin, reg_type_str[type]);
3421 return false;
3422 }
3423
3424 return true;
3425}
3426
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003427static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env)
3428{
3429 return &env->insn_aux_data[env->insn_idx];
3430}
3431
3432static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
3433 u32 *ptr_limit, u8 opcode, bool off_is_neg)
3434{
3435 bool mask_to_left = (opcode == BPF_ADD && off_is_neg) ||
3436 (opcode == BPF_SUB && !off_is_neg);
3437 u32 off;
3438
3439 switch (ptr_reg->type) {
3440 case PTR_TO_STACK:
Andrey Ignatov088ec262019-04-03 23:22:39 -07003441 /* Indirect variable offset stack access is prohibited in
3442 * unprivileged mode so it's not handled here.
3443 */
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003444 off = ptr_reg->off + ptr_reg->var_off.value;
3445 if (mask_to_left)
3446 *ptr_limit = MAX_BPF_STACK + off;
3447 else
3448 *ptr_limit = -off;
3449 return 0;
3450 case PTR_TO_MAP_VALUE:
3451 if (mask_to_left) {
3452 *ptr_limit = ptr_reg->umax_value + ptr_reg->off;
3453 } else {
3454 off = ptr_reg->smin_value + ptr_reg->off;
3455 *ptr_limit = ptr_reg->map_ptr->value_size - off;
3456 }
3457 return 0;
3458 default:
3459 return -EINVAL;
3460 }
3461}
3462
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003463static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env,
3464 const struct bpf_insn *insn)
3465{
3466 return env->allow_ptr_leaks || BPF_SRC(insn->code) == BPF_K;
3467}
3468
3469static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux,
3470 u32 alu_state, u32 alu_limit)
3471{
3472 /* If we arrived here from different branches with different
3473 * state or limits to sanitize, then this won't work.
3474 */
3475 if (aux->alu_state &&
3476 (aux->alu_state != alu_state ||
3477 aux->alu_limit != alu_limit))
3478 return -EACCES;
3479
3480 /* Corresponding fixup done in fixup_bpf_calls(). */
3481 aux->alu_state = alu_state;
3482 aux->alu_limit = alu_limit;
3483 return 0;
3484}
3485
3486static int sanitize_val_alu(struct bpf_verifier_env *env,
3487 struct bpf_insn *insn)
3488{
3489 struct bpf_insn_aux_data *aux = cur_aux(env);
3490
3491 if (can_skip_alu_sanitation(env, insn))
3492 return 0;
3493
3494 return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0);
3495}
3496
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003497static int sanitize_ptr_alu(struct bpf_verifier_env *env,
3498 struct bpf_insn *insn,
3499 const struct bpf_reg_state *ptr_reg,
3500 struct bpf_reg_state *dst_reg,
3501 bool off_is_neg)
3502{
3503 struct bpf_verifier_state *vstate = env->cur_state;
3504 struct bpf_insn_aux_data *aux = cur_aux(env);
3505 bool ptr_is_dst_reg = ptr_reg == dst_reg;
3506 u8 opcode = BPF_OP(insn->code);
3507 u32 alu_state, alu_limit;
3508 struct bpf_reg_state tmp;
3509 bool ret;
3510
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003511 if (can_skip_alu_sanitation(env, insn))
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003512 return 0;
3513
3514 /* We already marked aux for masking from non-speculative
3515 * paths, thus we got here in the first place. We only care
3516 * to explore bad access from here.
3517 */
3518 if (vstate->speculative)
3519 goto do_sim;
3520
3521 alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0;
3522 alu_state |= ptr_is_dst_reg ?
3523 BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST;
3524
3525 if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg))
3526 return 0;
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003527 if (update_alu_sanitation_state(aux, alu_state, alu_limit))
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003528 return -EACCES;
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003529do_sim:
3530 /* Simulate and find potential out-of-bounds access under
3531 * speculative execution from truncation as a result of
3532 * masking when off was not within expected range. If off
3533 * sits in dst, then we temporarily need to move ptr there
3534 * to simulate dst (== 0) +/-= ptr. Needed, for example,
3535 * for cases where we use K-based arithmetic in one direction
3536 * and truncated reg-based in the other in order to explore
3537 * bad access.
3538 */
3539 if (!ptr_is_dst_reg) {
3540 tmp = *dst_reg;
3541 *dst_reg = *ptr_reg;
3542 }
3543 ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true);
Xu Yu08032782019-03-21 18:00:35 +08003544 if (!ptr_is_dst_reg && ret)
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003545 *dst_reg = tmp;
3546 return !ret ? -EFAULT : 0;
3547}
3548
Edward Creef1174f72017-08-07 15:26:19 +01003549/* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off.
Edward Creef1174f72017-08-07 15:26:19 +01003550 * Caller should also handle BPF_MOV case separately.
3551 * If we return -EACCES, caller may want to try again treating pointer as a
3552 * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks.
3553 */
3554static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
3555 struct bpf_insn *insn,
3556 const struct bpf_reg_state *ptr_reg,
3557 const struct bpf_reg_state *off_reg)
Josef Bacik48461132016-09-28 10:54:32 -04003558{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003559 struct bpf_verifier_state *vstate = env->cur_state;
3560 struct bpf_func_state *state = vstate->frame[vstate->curframe];
3561 struct bpf_reg_state *regs = state->regs, *dst_reg;
Edward Creef1174f72017-08-07 15:26:19 +01003562 bool known = tnum_is_const(off_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01003563 s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value,
3564 smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
3565 u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value,
3566 umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value;
Daniel Borkmann9d7ecee2019-01-03 00:58:32 +01003567 u32 dst = insn->dst_reg, src = insn->src_reg;
Josef Bacik48461132016-09-28 10:54:32 -04003568 u8 opcode = BPF_OP(insn->code);
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003569 int ret;
Josef Bacik48461132016-09-28 10:54:32 -04003570
Edward Creef1174f72017-08-07 15:26:19 +01003571 dst_reg = &regs[dst];
Josef Bacik48461132016-09-28 10:54:32 -04003572
Daniel Borkmann6f161012018-01-18 01:15:21 +01003573 if ((known && (smin_val != smax_val || umin_val != umax_val)) ||
3574 smin_val > smax_val || umin_val > umax_val) {
3575 /* Taint dst register if offset had invalid bounds derived from
3576 * e.g. dead branches.
3577 */
3578 __mark_reg_unknown(dst_reg);
3579 return 0;
Josef Bacik48461132016-09-28 10:54:32 -04003580 }
3581
Edward Creef1174f72017-08-07 15:26:19 +01003582 if (BPF_CLASS(insn->code) != BPF_ALU64) {
3583 /* 32-bit ALU ops on pointers produce (meaningless) scalars */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003584 verbose(env,
3585 "R%d 32-bit pointer arithmetic prohibited\n",
3586 dst);
Edward Creef1174f72017-08-07 15:26:19 +01003587 return -EACCES;
3588 }
David S. Millerd1174412017-05-10 11:22:52 -07003589
Joe Stringeraad2eea2018-10-02 13:35:30 -07003590 switch (ptr_reg->type) {
3591 case PTR_TO_MAP_VALUE_OR_NULL:
3592 verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n",
3593 dst, reg_type_str[ptr_reg->type]);
Edward Creef1174f72017-08-07 15:26:19 +01003594 return -EACCES;
Joe Stringeraad2eea2018-10-02 13:35:30 -07003595 case CONST_PTR_TO_MAP:
3596 case PTR_TO_PACKET_END:
Joe Stringerc64b7982018-10-02 13:35:33 -07003597 case PTR_TO_SOCKET:
3598 case PTR_TO_SOCKET_OR_NULL:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08003599 case PTR_TO_SOCK_COMMON:
3600 case PTR_TO_SOCK_COMMON_OR_NULL:
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08003601 case PTR_TO_TCP_SOCK:
3602 case PTR_TO_TCP_SOCK_OR_NULL:
Joe Stringeraad2eea2018-10-02 13:35:30 -07003603 verbose(env, "R%d pointer arithmetic on %s prohibited\n",
3604 dst, reg_type_str[ptr_reg->type]);
Edward Creef1174f72017-08-07 15:26:19 +01003605 return -EACCES;
Daniel Borkmann9d7ecee2019-01-03 00:58:32 +01003606 case PTR_TO_MAP_VALUE:
3607 if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) {
3608 verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n",
3609 off_reg == dst_reg ? dst : src);
3610 return -EACCES;
3611 }
3612 /* fall-through */
Joe Stringeraad2eea2018-10-02 13:35:30 -07003613 default:
3614 break;
Edward Creef1174f72017-08-07 15:26:19 +01003615 }
3616
3617 /* In case of 'scalar += pointer', dst_reg inherits pointer type and id.
3618 * The id may be overwritten later if we create a new variable offset.
Josef Bacik48461132016-09-28 10:54:32 -04003619 */
Edward Creef1174f72017-08-07 15:26:19 +01003620 dst_reg->type = ptr_reg->type;
3621 dst_reg->id = ptr_reg->id;
Josef Bacikf23cc642016-11-14 15:45:36 -05003622
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08003623 if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) ||
3624 !check_reg_sane_offset(env, ptr_reg, ptr_reg->type))
3625 return -EINVAL;
3626
Josef Bacik48461132016-09-28 10:54:32 -04003627 switch (opcode) {
3628 case BPF_ADD:
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003629 ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0);
3630 if (ret < 0) {
3631 verbose(env, "R%d tried to add from different maps or paths\n", dst);
3632 return ret;
3633 }
Edward Creef1174f72017-08-07 15:26:19 +01003634 /* We can take a fixed offset as long as it doesn't overflow
3635 * the s32 'off' field
3636 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003637 if (known && (ptr_reg->off + smin_val ==
3638 (s64)(s32)(ptr_reg->off + smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01003639 /* pointer += K. Accumulate it into fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01003640 dst_reg->smin_value = smin_ptr;
3641 dst_reg->smax_value = smax_ptr;
3642 dst_reg->umin_value = umin_ptr;
3643 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01003644 dst_reg->var_off = ptr_reg->var_off;
Edward Creeb03c9f92017-08-07 15:26:36 +01003645 dst_reg->off = ptr_reg->off + smin_val;
Daniel Borkmann09625902018-11-01 00:05:52 +01003646 dst_reg->raw = ptr_reg->raw;
Edward Creef1174f72017-08-07 15:26:19 +01003647 break;
3648 }
Edward Creef1174f72017-08-07 15:26:19 +01003649 /* A new variable offset is created. Note that off_reg->off
3650 * == 0, since it's a scalar.
3651 * dst_reg gets the pointer type and since some positive
3652 * integer value was added to the pointer, give it a new 'id'
3653 * if it's a PTR_TO_PACKET.
3654 * this creates a new 'base' pointer, off_reg (variable) gets
3655 * added into the variable offset, and we copy the fixed offset
3656 * from ptr_reg.
3657 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003658 if (signed_add_overflows(smin_ptr, smin_val) ||
3659 signed_add_overflows(smax_ptr, smax_val)) {
3660 dst_reg->smin_value = S64_MIN;
3661 dst_reg->smax_value = S64_MAX;
3662 } else {
3663 dst_reg->smin_value = smin_ptr + smin_val;
3664 dst_reg->smax_value = smax_ptr + smax_val;
3665 }
3666 if (umin_ptr + umin_val < umin_ptr ||
3667 umax_ptr + umax_val < umax_ptr) {
3668 dst_reg->umin_value = 0;
3669 dst_reg->umax_value = U64_MAX;
3670 } else {
3671 dst_reg->umin_value = umin_ptr + umin_val;
3672 dst_reg->umax_value = umax_ptr + umax_val;
3673 }
Edward Creef1174f72017-08-07 15:26:19 +01003674 dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off);
3675 dst_reg->off = ptr_reg->off;
Daniel Borkmann09625902018-11-01 00:05:52 +01003676 dst_reg->raw = ptr_reg->raw;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003677 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01003678 dst_reg->id = ++env->id_gen;
3679 /* something was added to pkt_ptr, set range to zero */
Daniel Borkmann09625902018-11-01 00:05:52 +01003680 dst_reg->raw = 0;
Edward Creef1174f72017-08-07 15:26:19 +01003681 }
Josef Bacik48461132016-09-28 10:54:32 -04003682 break;
3683 case BPF_SUB:
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003684 ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0);
3685 if (ret < 0) {
3686 verbose(env, "R%d tried to sub from different maps or paths\n", dst);
3687 return ret;
3688 }
Edward Creef1174f72017-08-07 15:26:19 +01003689 if (dst_reg == off_reg) {
3690 /* scalar -= pointer. Creates an unknown scalar */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003691 verbose(env, "R%d tried to subtract pointer from scalar\n",
3692 dst);
Edward Creef1174f72017-08-07 15:26:19 +01003693 return -EACCES;
3694 }
3695 /* We don't allow subtraction from FP, because (according to
3696 * test_verifier.c test "invalid fp arithmetic", JITs might not
3697 * be able to deal with it.
Edward Cree93057062017-07-21 14:37:34 +01003698 */
Edward Creef1174f72017-08-07 15:26:19 +01003699 if (ptr_reg->type == PTR_TO_STACK) {
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003700 verbose(env, "R%d subtraction from stack pointer prohibited\n",
3701 dst);
Edward Creef1174f72017-08-07 15:26:19 +01003702 return -EACCES;
3703 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003704 if (known && (ptr_reg->off - smin_val ==
3705 (s64)(s32)(ptr_reg->off - smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01003706 /* pointer -= K. Subtract it from fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01003707 dst_reg->smin_value = smin_ptr;
3708 dst_reg->smax_value = smax_ptr;
3709 dst_reg->umin_value = umin_ptr;
3710 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01003711 dst_reg->var_off = ptr_reg->var_off;
3712 dst_reg->id = ptr_reg->id;
Edward Creeb03c9f92017-08-07 15:26:36 +01003713 dst_reg->off = ptr_reg->off - smin_val;
Daniel Borkmann09625902018-11-01 00:05:52 +01003714 dst_reg->raw = ptr_reg->raw;
Edward Creef1174f72017-08-07 15:26:19 +01003715 break;
3716 }
Edward Creef1174f72017-08-07 15:26:19 +01003717 /* A new variable offset is created. If the subtrahend is known
3718 * nonnegative, then any reg->range we had before is still good.
3719 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003720 if (signed_sub_overflows(smin_ptr, smax_val) ||
3721 signed_sub_overflows(smax_ptr, smin_val)) {
3722 /* Overflow possible, we know nothing */
3723 dst_reg->smin_value = S64_MIN;
3724 dst_reg->smax_value = S64_MAX;
3725 } else {
3726 dst_reg->smin_value = smin_ptr - smax_val;
3727 dst_reg->smax_value = smax_ptr - smin_val;
3728 }
3729 if (umin_ptr < umax_val) {
3730 /* Overflow possible, we know nothing */
3731 dst_reg->umin_value = 0;
3732 dst_reg->umax_value = U64_MAX;
3733 } else {
3734 /* Cannot overflow (as long as bounds are consistent) */
3735 dst_reg->umin_value = umin_ptr - umax_val;
3736 dst_reg->umax_value = umax_ptr - umin_val;
3737 }
Edward Creef1174f72017-08-07 15:26:19 +01003738 dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off);
3739 dst_reg->off = ptr_reg->off;
Daniel Borkmann09625902018-11-01 00:05:52 +01003740 dst_reg->raw = ptr_reg->raw;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003741 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01003742 dst_reg->id = ++env->id_gen;
3743 /* something was added to pkt_ptr, set range to zero */
Edward Creeb03c9f92017-08-07 15:26:36 +01003744 if (smin_val < 0)
Daniel Borkmann09625902018-11-01 00:05:52 +01003745 dst_reg->raw = 0;
Edward Creef1174f72017-08-07 15:26:19 +01003746 }
3747 break;
3748 case BPF_AND:
3749 case BPF_OR:
3750 case BPF_XOR:
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003751 /* bitwise ops on pointers are troublesome, prohibit. */
3752 verbose(env, "R%d bitwise operator %s on pointer prohibited\n",
3753 dst, bpf_alu_string[opcode >> 4]);
Edward Creef1174f72017-08-07 15:26:19 +01003754 return -EACCES;
3755 default:
3756 /* other operators (e.g. MUL,LSH) produce non-pointer results */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003757 verbose(env, "R%d pointer arithmetic with %s operator prohibited\n",
3758 dst, bpf_alu_string[opcode >> 4]);
Edward Creef1174f72017-08-07 15:26:19 +01003759 return -EACCES;
3760 }
3761
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08003762 if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type))
3763 return -EINVAL;
3764
Edward Creeb03c9f92017-08-07 15:26:36 +01003765 __update_reg_bounds(dst_reg);
3766 __reg_deduce_bounds(dst_reg);
3767 __reg_bound_offset(dst_reg);
Daniel Borkmann0d6303d2019-01-03 00:58:30 +01003768
3769 /* For unprivileged we require that resulting offset must be in bounds
3770 * in order to be able to sanitize access later on.
3771 */
Daniel Borkmanne4298d22019-01-03 00:58:31 +01003772 if (!env->allow_ptr_leaks) {
3773 if (dst_reg->type == PTR_TO_MAP_VALUE &&
3774 check_map_access(env, dst, dst_reg->off, 1, false)) {
3775 verbose(env, "R%d pointer arithmetic of map value goes out of range, "
3776 "prohibited for !root\n", dst);
3777 return -EACCES;
3778 } else if (dst_reg->type == PTR_TO_STACK &&
3779 check_stack_access(env, dst_reg, dst_reg->off +
3780 dst_reg->var_off.value, 1)) {
3781 verbose(env, "R%d stack pointer arithmetic goes out of range, "
3782 "prohibited for !root\n", dst);
3783 return -EACCES;
3784 }
Daniel Borkmann0d6303d2019-01-03 00:58:30 +01003785 }
3786
Edward Creef1174f72017-08-07 15:26:19 +01003787 return 0;
3788}
3789
Jann Horn468f6ea2017-12-18 20:11:56 -08003790/* WARNING: This function does calculations on 64-bit values, but the actual
3791 * execution may occur on 32-bit values. Therefore, things like bitshifts
3792 * need extra checks in the 32-bit case.
3793 */
Edward Creef1174f72017-08-07 15:26:19 +01003794static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
3795 struct bpf_insn *insn,
3796 struct bpf_reg_state *dst_reg,
3797 struct bpf_reg_state src_reg)
3798{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003799 struct bpf_reg_state *regs = cur_regs(env);
Edward Creef1174f72017-08-07 15:26:19 +01003800 u8 opcode = BPF_OP(insn->code);
3801 bool src_known, dst_known;
Edward Creeb03c9f92017-08-07 15:26:36 +01003802 s64 smin_val, smax_val;
3803 u64 umin_val, umax_val;
Jann Horn468f6ea2017-12-18 20:11:56 -08003804 u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32;
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003805 u32 dst = insn->dst_reg;
3806 int ret;
Edward Creef1174f72017-08-07 15:26:19 +01003807
Jann Hornb7992072018-10-05 18:17:59 +02003808 if (insn_bitness == 32) {
3809 /* Relevant for 32-bit RSH: Information can propagate towards
3810 * LSB, so it isn't sufficient to only truncate the output to
3811 * 32 bits.
3812 */
3813 coerce_reg_to_size(dst_reg, 4);
3814 coerce_reg_to_size(&src_reg, 4);
3815 }
3816
Edward Creeb03c9f92017-08-07 15:26:36 +01003817 smin_val = src_reg.smin_value;
3818 smax_val = src_reg.smax_value;
3819 umin_val = src_reg.umin_value;
3820 umax_val = src_reg.umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01003821 src_known = tnum_is_const(src_reg.var_off);
3822 dst_known = tnum_is_const(dst_reg->var_off);
3823
Daniel Borkmann6f161012018-01-18 01:15:21 +01003824 if ((src_known && (smin_val != smax_val || umin_val != umax_val)) ||
3825 smin_val > smax_val || umin_val > umax_val) {
3826 /* Taint dst register if offset had invalid bounds derived from
3827 * e.g. dead branches.
3828 */
3829 __mark_reg_unknown(dst_reg);
3830 return 0;
3831 }
3832
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08003833 if (!src_known &&
3834 opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) {
3835 __mark_reg_unknown(dst_reg);
3836 return 0;
3837 }
3838
Edward Creef1174f72017-08-07 15:26:19 +01003839 switch (opcode) {
3840 case BPF_ADD:
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003841 ret = sanitize_val_alu(env, insn);
3842 if (ret < 0) {
3843 verbose(env, "R%d tried to add from different pointers or scalars\n", dst);
3844 return ret;
3845 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003846 if (signed_add_overflows(dst_reg->smin_value, smin_val) ||
3847 signed_add_overflows(dst_reg->smax_value, smax_val)) {
3848 dst_reg->smin_value = S64_MIN;
3849 dst_reg->smax_value = S64_MAX;
3850 } else {
3851 dst_reg->smin_value += smin_val;
3852 dst_reg->smax_value += smax_val;
3853 }
3854 if (dst_reg->umin_value + umin_val < umin_val ||
3855 dst_reg->umax_value + umax_val < umax_val) {
3856 dst_reg->umin_value = 0;
3857 dst_reg->umax_value = U64_MAX;
3858 } else {
3859 dst_reg->umin_value += umin_val;
3860 dst_reg->umax_value += umax_val;
3861 }
Edward Creef1174f72017-08-07 15:26:19 +01003862 dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off);
3863 break;
3864 case BPF_SUB:
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003865 ret = sanitize_val_alu(env, insn);
3866 if (ret < 0) {
3867 verbose(env, "R%d tried to sub from different pointers or scalars\n", dst);
3868 return ret;
3869 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003870 if (signed_sub_overflows(dst_reg->smin_value, smax_val) ||
3871 signed_sub_overflows(dst_reg->smax_value, smin_val)) {
3872 /* Overflow possible, we know nothing */
3873 dst_reg->smin_value = S64_MIN;
3874 dst_reg->smax_value = S64_MAX;
3875 } else {
3876 dst_reg->smin_value -= smax_val;
3877 dst_reg->smax_value -= smin_val;
3878 }
3879 if (dst_reg->umin_value < umax_val) {
3880 /* Overflow possible, we know nothing */
3881 dst_reg->umin_value = 0;
3882 dst_reg->umax_value = U64_MAX;
3883 } else {
3884 /* Cannot overflow (as long as bounds are consistent) */
3885 dst_reg->umin_value -= umax_val;
3886 dst_reg->umax_value -= umin_val;
3887 }
Edward Creef1174f72017-08-07 15:26:19 +01003888 dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off);
Josef Bacik48461132016-09-28 10:54:32 -04003889 break;
3890 case BPF_MUL:
Edward Creeb03c9f92017-08-07 15:26:36 +01003891 dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off);
3892 if (smin_val < 0 || dst_reg->smin_value < 0) {
Edward Creef1174f72017-08-07 15:26:19 +01003893 /* Ain't nobody got time to multiply that sign */
Edward Creeb03c9f92017-08-07 15:26:36 +01003894 __mark_reg_unbounded(dst_reg);
3895 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003896 break;
3897 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003898 /* Both values are positive, so we can work with unsigned and
3899 * copy the result to signed (unless it exceeds S64_MAX).
Edward Creef1174f72017-08-07 15:26:19 +01003900 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003901 if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) {
3902 /* Potential overflow, we know nothing */
3903 __mark_reg_unbounded(dst_reg);
3904 /* (except what we can learn from the var_off) */
3905 __update_reg_bounds(dst_reg);
3906 break;
3907 }
3908 dst_reg->umin_value *= umin_val;
3909 dst_reg->umax_value *= umax_val;
3910 if (dst_reg->umax_value > S64_MAX) {
3911 /* Overflow possible, we know nothing */
3912 dst_reg->smin_value = S64_MIN;
3913 dst_reg->smax_value = S64_MAX;
3914 } else {
3915 dst_reg->smin_value = dst_reg->umin_value;
3916 dst_reg->smax_value = dst_reg->umax_value;
3917 }
Josef Bacik48461132016-09-28 10:54:32 -04003918 break;
3919 case BPF_AND:
Edward Creef1174f72017-08-07 15:26:19 +01003920 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01003921 __mark_reg_known(dst_reg, dst_reg->var_off.value &
3922 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01003923 break;
3924 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003925 /* We get our minimum from the var_off, since that's inherently
3926 * bitwise. Our maximum is the minimum of the operands' maxima.
Josef Bacikf23cc642016-11-14 15:45:36 -05003927 */
Edward Creef1174f72017-08-07 15:26:19 +01003928 dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01003929 dst_reg->umin_value = dst_reg->var_off.value;
3930 dst_reg->umax_value = min(dst_reg->umax_value, umax_val);
3931 if (dst_reg->smin_value < 0 || smin_val < 0) {
3932 /* Lose signed bounds when ANDing negative numbers,
3933 * ain't nobody got time for that.
3934 */
3935 dst_reg->smin_value = S64_MIN;
3936 dst_reg->smax_value = S64_MAX;
3937 } else {
3938 /* ANDing two positives gives a positive, so safe to
3939 * cast result into s64.
3940 */
3941 dst_reg->smin_value = dst_reg->umin_value;
3942 dst_reg->smax_value = dst_reg->umax_value;
3943 }
3944 /* We may learn something more from the var_off */
3945 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003946 break;
3947 case BPF_OR:
3948 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01003949 __mark_reg_known(dst_reg, dst_reg->var_off.value |
3950 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01003951 break;
3952 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003953 /* We get our maximum from the var_off, and our minimum is the
3954 * maximum of the operands' minima
Edward Creef1174f72017-08-07 15:26:19 +01003955 */
3956 dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01003957 dst_reg->umin_value = max(dst_reg->umin_value, umin_val);
3958 dst_reg->umax_value = dst_reg->var_off.value |
3959 dst_reg->var_off.mask;
3960 if (dst_reg->smin_value < 0 || smin_val < 0) {
3961 /* Lose signed bounds when ORing negative numbers,
3962 * ain't nobody got time for that.
3963 */
3964 dst_reg->smin_value = S64_MIN;
3965 dst_reg->smax_value = S64_MAX;
Edward Creef1174f72017-08-07 15:26:19 +01003966 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01003967 /* ORing two positives gives a positive, so safe to
3968 * cast result into s64.
3969 */
3970 dst_reg->smin_value = dst_reg->umin_value;
3971 dst_reg->smax_value = dst_reg->umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01003972 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003973 /* We may learn something more from the var_off */
3974 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04003975 break;
3976 case BPF_LSH:
Jann Horn468f6ea2017-12-18 20:11:56 -08003977 if (umax_val >= insn_bitness) {
3978 /* Shifts greater than 31 or 63 are undefined.
3979 * This includes shifts by a negative number.
Edward Creeb03c9f92017-08-07 15:26:36 +01003980 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003981 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003982 break;
3983 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003984 /* We lose all sign bit information (except what we can pick
3985 * up from var_off)
Josef Bacik48461132016-09-28 10:54:32 -04003986 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003987 dst_reg->smin_value = S64_MIN;
3988 dst_reg->smax_value = S64_MAX;
3989 /* If we might shift our top bit out, then we know nothing */
3990 if (dst_reg->umax_value > 1ULL << (63 - umax_val)) {
3991 dst_reg->umin_value = 0;
3992 dst_reg->umax_value = U64_MAX;
David S. Millerd1174412017-05-10 11:22:52 -07003993 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01003994 dst_reg->umin_value <<= umin_val;
3995 dst_reg->umax_value <<= umax_val;
David S. Millerd1174412017-05-10 11:22:52 -07003996 }
Yonghong Songafbe1a52018-04-28 22:28:10 -07003997 dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val);
Edward Creeb03c9f92017-08-07 15:26:36 +01003998 /* We may learn something more from the var_off */
3999 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004000 break;
4001 case BPF_RSH:
Jann Horn468f6ea2017-12-18 20:11:56 -08004002 if (umax_val >= insn_bitness) {
4003 /* Shifts greater than 31 or 63 are undefined.
4004 * This includes shifts by a negative number.
Edward Creeb03c9f92017-08-07 15:26:36 +01004005 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004006 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004007 break;
4008 }
Edward Cree4374f252017-12-18 20:11:53 -08004009 /* BPF_RSH is an unsigned shift. If the value in dst_reg might
4010 * be negative, then either:
4011 * 1) src_reg might be zero, so the sign bit of the result is
4012 * unknown, so we lose our signed bounds
4013 * 2) it's known negative, thus the unsigned bounds capture the
4014 * signed bounds
4015 * 3) the signed bounds cross zero, so they tell us nothing
4016 * about the result
4017 * If the value in dst_reg is known nonnegative, then again the
4018 * unsigned bounts capture the signed bounds.
4019 * Thus, in all cases it suffices to blow away our signed bounds
4020 * and rely on inferring new ones from the unsigned bounds and
4021 * var_off of the result.
4022 */
4023 dst_reg->smin_value = S64_MIN;
4024 dst_reg->smax_value = S64_MAX;
Yonghong Songafbe1a52018-04-28 22:28:10 -07004025 dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val);
Edward Creeb03c9f92017-08-07 15:26:36 +01004026 dst_reg->umin_value >>= umax_val;
4027 dst_reg->umax_value >>= umin_val;
4028 /* We may learn something more from the var_off */
4029 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004030 break;
Yonghong Song9cbe1f5a2018-04-28 22:28:11 -07004031 case BPF_ARSH:
4032 if (umax_val >= insn_bitness) {
4033 /* Shifts greater than 31 or 63 are undefined.
4034 * This includes shifts by a negative number.
4035 */
4036 mark_reg_unknown(env, regs, insn->dst_reg);
4037 break;
4038 }
4039
4040 /* Upon reaching here, src_known is true and
4041 * umax_val is equal to umin_val.
4042 */
4043 dst_reg->smin_value >>= umin_val;
4044 dst_reg->smax_value >>= umin_val;
4045 dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val);
4046
4047 /* blow away the dst_reg umin_value/umax_value and rely on
4048 * dst_reg var_off to refine the result.
4049 */
4050 dst_reg->umin_value = 0;
4051 dst_reg->umax_value = U64_MAX;
4052 __update_reg_bounds(dst_reg);
4053 break;
Josef Bacik48461132016-09-28 10:54:32 -04004054 default:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004055 mark_reg_unknown(env, regs, insn->dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004056 break;
4057 }
4058
Jann Horn468f6ea2017-12-18 20:11:56 -08004059 if (BPF_CLASS(insn->code) != BPF_ALU64) {
4060 /* 32-bit ALU ops are (32,32)->32 */
4061 coerce_reg_to_size(dst_reg, 4);
Jann Horn468f6ea2017-12-18 20:11:56 -08004062 }
4063
Edward Creeb03c9f92017-08-07 15:26:36 +01004064 __reg_deduce_bounds(dst_reg);
4065 __reg_bound_offset(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004066 return 0;
4067}
4068
4069/* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max
4070 * and var_off.
4071 */
4072static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
4073 struct bpf_insn *insn)
4074{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004075 struct bpf_verifier_state *vstate = env->cur_state;
4076 struct bpf_func_state *state = vstate->frame[vstate->curframe];
4077 struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg;
Edward Creef1174f72017-08-07 15:26:19 +01004078 struct bpf_reg_state *ptr_reg = NULL, off_reg = {0};
4079 u8 opcode = BPF_OP(insn->code);
Edward Creef1174f72017-08-07 15:26:19 +01004080
4081 dst_reg = &regs[insn->dst_reg];
Edward Creef1174f72017-08-07 15:26:19 +01004082 src_reg = NULL;
4083 if (dst_reg->type != SCALAR_VALUE)
4084 ptr_reg = dst_reg;
4085 if (BPF_SRC(insn->code) == BPF_X) {
4086 src_reg = &regs[insn->src_reg];
Edward Creef1174f72017-08-07 15:26:19 +01004087 if (src_reg->type != SCALAR_VALUE) {
4088 if (dst_reg->type != SCALAR_VALUE) {
4089 /* Combining two pointers by any ALU op yields
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004090 * an arbitrary scalar. Disallow all math except
4091 * pointer subtraction
Edward Creef1174f72017-08-07 15:26:19 +01004092 */
Alexei Starovoitovdd066822018-09-12 14:06:10 -07004093 if (opcode == BPF_SUB && env->allow_ptr_leaks) {
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004094 mark_reg_unknown(env, regs, insn->dst_reg);
4095 return 0;
Edward Creef1174f72017-08-07 15:26:19 +01004096 }
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004097 verbose(env, "R%d pointer %s pointer prohibited\n",
4098 insn->dst_reg,
4099 bpf_alu_string[opcode >> 4]);
4100 return -EACCES;
Edward Creef1174f72017-08-07 15:26:19 +01004101 } else {
4102 /* scalar += pointer
4103 * This is legal, but we have to reverse our
4104 * src/dest handling in computing the range
4105 */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004106 return adjust_ptr_min_max_vals(env, insn,
4107 src_reg, dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004108 }
4109 } else if (ptr_reg) {
4110 /* pointer += scalar */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004111 return adjust_ptr_min_max_vals(env, insn,
4112 dst_reg, src_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004113 }
4114 } else {
4115 /* Pretend the src is a reg with a known value, since we only
4116 * need to be able to read from this state.
4117 */
4118 off_reg.type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01004119 __mark_reg_known(&off_reg, insn->imm);
Edward Creef1174f72017-08-07 15:26:19 +01004120 src_reg = &off_reg;
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004121 if (ptr_reg) /* pointer += K */
4122 return adjust_ptr_min_max_vals(env, insn,
4123 ptr_reg, src_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004124 }
4125
4126 /* Got here implies adding two SCALAR_VALUEs */
4127 if (WARN_ON_ONCE(ptr_reg)) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004128 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004129 verbose(env, "verifier internal error: unexpected ptr_reg\n");
Edward Creef1174f72017-08-07 15:26:19 +01004130 return -EINVAL;
4131 }
4132 if (WARN_ON(!src_reg)) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004133 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004134 verbose(env, "verifier internal error: no src_reg\n");
Edward Creef1174f72017-08-07 15:26:19 +01004135 return -EINVAL;
4136 }
4137 return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004138}
4139
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004140/* check validity of 32-bit and 64-bit arithmetic operations */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004141static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004142{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004143 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004144 u8 opcode = BPF_OP(insn->code);
4145 int err;
4146
4147 if (opcode == BPF_END || opcode == BPF_NEG) {
4148 if (opcode == BPF_NEG) {
4149 if (BPF_SRC(insn->code) != 0 ||
4150 insn->src_reg != BPF_REG_0 ||
4151 insn->off != 0 || insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004152 verbose(env, "BPF_NEG uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004153 return -EINVAL;
4154 }
4155 } else {
4156 if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
Edward Creee67b8a62017-09-15 14:37:38 +01004157 (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) ||
4158 BPF_CLASS(insn->code) == BPF_ALU64) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004159 verbose(env, "BPF_END uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004160 return -EINVAL;
4161 }
4162 }
4163
4164 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01004165 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004166 if (err)
4167 return err;
4168
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004169 if (is_pointer_value(env, insn->dst_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004170 verbose(env, "R%d pointer arithmetic prohibited\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004171 insn->dst_reg);
4172 return -EACCES;
4173 }
4174
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004175 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01004176 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004177 if (err)
4178 return err;
4179
4180 } else if (opcode == BPF_MOV) {
4181
4182 if (BPF_SRC(insn->code) == BPF_X) {
4183 if (insn->imm != 0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004184 verbose(env, "BPF_MOV uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004185 return -EINVAL;
4186 }
4187
4188 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01004189 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004190 if (err)
4191 return err;
4192 } else {
4193 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004194 verbose(env, "BPF_MOV uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004195 return -EINVAL;
4196 }
4197 }
4198
Arthur Fabrefbeb1602018-07-31 18:17:22 +01004199 /* check dest operand, mark as required later */
4200 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004201 if (err)
4202 return err;
4203
4204 if (BPF_SRC(insn->code) == BPF_X) {
Jiong Wange434b8c2018-12-07 12:16:18 -05004205 struct bpf_reg_state *src_reg = regs + insn->src_reg;
4206 struct bpf_reg_state *dst_reg = regs + insn->dst_reg;
4207
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004208 if (BPF_CLASS(insn->code) == BPF_ALU64) {
4209 /* case: R1 = R2
4210 * copy register state to dest reg
4211 */
Jiong Wange434b8c2018-12-07 12:16:18 -05004212 *dst_reg = *src_reg;
4213 dst_reg->live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004214 } else {
Edward Creef1174f72017-08-07 15:26:19 +01004215 /* R1 = (u32) R2 */
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004216 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004217 verbose(env,
4218 "R%d partial copy of pointer\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004219 insn->src_reg);
4220 return -EACCES;
Jiong Wange434b8c2018-12-07 12:16:18 -05004221 } else if (src_reg->type == SCALAR_VALUE) {
4222 *dst_reg = *src_reg;
4223 dst_reg->live |= REG_LIVE_WRITTEN;
4224 } else {
4225 mark_reg_unknown(env, regs,
4226 insn->dst_reg);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004227 }
Jiong Wange434b8c2018-12-07 12:16:18 -05004228 coerce_reg_to_size(dst_reg, 4);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004229 }
4230 } else {
4231 /* case: R = imm
4232 * remember the value we stored into this reg
4233 */
Arthur Fabrefbeb1602018-07-31 18:17:22 +01004234 /* clear any state __mark_reg_known doesn't set */
4235 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004236 regs[insn->dst_reg].type = SCALAR_VALUE;
Jann Horn95a762e2017-12-18 20:11:54 -08004237 if (BPF_CLASS(insn->code) == BPF_ALU64) {
4238 __mark_reg_known(regs + insn->dst_reg,
4239 insn->imm);
4240 } else {
4241 __mark_reg_known(regs + insn->dst_reg,
4242 (u32)insn->imm);
4243 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004244 }
4245
4246 } else if (opcode > BPF_END) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004247 verbose(env, "invalid BPF_ALU opcode %x\n", opcode);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004248 return -EINVAL;
4249
4250 } else { /* all other ALU ops: and, sub, xor, add, ... */
4251
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004252 if (BPF_SRC(insn->code) == BPF_X) {
4253 if (insn->imm != 0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004254 verbose(env, "BPF_ALU uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004255 return -EINVAL;
4256 }
4257 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01004258 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004259 if (err)
4260 return err;
4261 } else {
4262 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004263 verbose(env, "BPF_ALU uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004264 return -EINVAL;
4265 }
4266 }
4267
4268 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01004269 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004270 if (err)
4271 return err;
4272
4273 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
4274 BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004275 verbose(env, "div by zero\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004276 return -EINVAL;
4277 }
4278
Rabin Vincent229394e82016-01-12 20:17:08 +01004279 if ((opcode == BPF_LSH || opcode == BPF_RSH ||
4280 opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
4281 int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
4282
4283 if (insn->imm < 0 || insn->imm >= size) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004284 verbose(env, "invalid shift %d\n", insn->imm);
Rabin Vincent229394e82016-01-12 20:17:08 +01004285 return -EINVAL;
4286 }
4287 }
4288
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07004289 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01004290 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07004291 if (err)
4292 return err;
4293
Edward Creef1174f72017-08-07 15:26:19 +01004294 return adjust_reg_min_max_vals(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004295 }
4296
4297 return 0;
4298}
4299
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004300static void find_good_pkt_pointers(struct bpf_verifier_state *vstate,
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004301 struct bpf_reg_state *dst_reg,
David S. Millerf8ddadc2017-10-22 13:36:53 +01004302 enum bpf_reg_type type,
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004303 bool range_right_open)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004304{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004305 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004306 struct bpf_reg_state *regs = state->regs, *reg;
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004307 u16 new_range;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004308 int i, j;
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004309
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004310 if (dst_reg->off < 0 ||
4311 (dst_reg->off == 0 && range_right_open))
Edward Creef1174f72017-08-07 15:26:19 +01004312 /* This doesn't give us any range */
4313 return;
4314
Edward Creeb03c9f92017-08-07 15:26:36 +01004315 if (dst_reg->umax_value > MAX_PACKET_OFF ||
4316 dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF)
Edward Creef1174f72017-08-07 15:26:19 +01004317 /* Risk of overflow. For instance, ptr + (1<<63) may be less
4318 * than pkt_end, but that's because it's also less than pkt.
4319 */
4320 return;
4321
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004322 new_range = dst_reg->off;
4323 if (range_right_open)
4324 new_range--;
4325
4326 /* Examples for register markings:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004327 *
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004328 * pkt_data in dst register:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004329 *
4330 * r2 = r3;
4331 * r2 += 8;
4332 * if (r2 > pkt_end) goto <handle exception>
4333 * <access okay>
4334 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004335 * r2 = r3;
4336 * r2 += 8;
4337 * if (r2 < pkt_end) goto <access okay>
4338 * <handle exception>
4339 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004340 * Where:
4341 * r2 == dst_reg, pkt_end == src_reg
4342 * r2=pkt(id=n,off=8,r=0)
4343 * r3=pkt(id=n,off=0,r=0)
4344 *
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004345 * pkt_data in src register:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004346 *
4347 * r2 = r3;
4348 * r2 += 8;
4349 * if (pkt_end >= r2) goto <access okay>
4350 * <handle exception>
4351 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004352 * r2 = r3;
4353 * r2 += 8;
4354 * if (pkt_end <= r2) goto <handle exception>
4355 * <access okay>
4356 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004357 * Where:
4358 * pkt_end == dst_reg, r2 == src_reg
4359 * r2=pkt(id=n,off=8,r=0)
4360 * r3=pkt(id=n,off=0,r=0)
4361 *
4362 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004363 * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8)
4364 * and [r3, r3 + 8-1) respectively is safe to access depending on
4365 * the check.
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004366 */
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004367
Edward Creef1174f72017-08-07 15:26:19 +01004368 /* If our ids match, then we must have the same max_value. And we
4369 * don't care about the other reg's fixed offset, since if it's too big
4370 * the range won't allow anything.
4371 * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16.
4372 */
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004373 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004374 if (regs[i].type == type && regs[i].id == dst_reg->id)
Alexei Starovoitovb1977682017-03-24 15:57:33 -07004375 /* keep the maximum range already checked */
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004376 regs[i].range = max(regs[i].range, new_range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004377
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004378 for (j = 0; j <= vstate->curframe; j++) {
4379 state = vstate->frame[j];
Joe Stringerf3709f62018-10-02 13:35:29 -07004380 bpf_for_each_spilled_reg(i, state, reg) {
4381 if (!reg)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004382 continue;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004383 if (reg->type == type && reg->id == dst_reg->id)
4384 reg->range = max(reg->range, new_range);
4385 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004386 }
4387}
4388
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004389/* compute branch direction of the expression "if (reg opcode val) goto target;"
4390 * and return:
4391 * 1 - branch will be taken and "goto target" will be executed
4392 * 0 - branch will not be taken and fall-through to next insn
4393 * -1 - unknown. Example: "if (reg < 5)" is unknown when register value range [0,10]
4394 */
Jiong Wang092ed092019-01-26 12:26:01 -05004395static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode,
4396 bool is_jmp32)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004397{
Jiong Wang092ed092019-01-26 12:26:01 -05004398 struct bpf_reg_state reg_lo;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004399 s64 sval;
4400
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004401 if (__is_pointer_value(false, reg))
4402 return -1;
4403
Jiong Wang092ed092019-01-26 12:26:01 -05004404 if (is_jmp32) {
4405 reg_lo = *reg;
4406 reg = &reg_lo;
4407 /* For JMP32, only low 32 bits are compared, coerce_reg_to_size
4408 * could truncate high bits and update umin/umax according to
4409 * information of low bits.
4410 */
4411 coerce_reg_to_size(reg, 4);
4412 /* smin/smax need special handling. For example, after coerce,
4413 * if smin_value is 0x00000000ffffffffLL, the value is -1 when
4414 * used as operand to JMP32. It is a negative number from s32's
4415 * point of view, while it is a positive number when seen as
4416 * s64. The smin/smax are kept as s64, therefore, when used with
4417 * JMP32, they need to be transformed into s32, then sign
4418 * extended back to s64.
4419 *
4420 * Also, smin/smax were copied from umin/umax. If umin/umax has
4421 * different sign bit, then min/max relationship doesn't
4422 * maintain after casting into s32, for this case, set smin/smax
4423 * to safest range.
4424 */
4425 if ((reg->umax_value ^ reg->umin_value) &
4426 (1ULL << 31)) {
4427 reg->smin_value = S32_MIN;
4428 reg->smax_value = S32_MAX;
4429 }
4430 reg->smin_value = (s64)(s32)reg->smin_value;
4431 reg->smax_value = (s64)(s32)reg->smax_value;
4432
4433 val = (u32)val;
4434 sval = (s64)(s32)val;
4435 } else {
4436 sval = (s64)val;
4437 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004438
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004439 switch (opcode) {
4440 case BPF_JEQ:
4441 if (tnum_is_const(reg->var_off))
4442 return !!tnum_equals_const(reg->var_off, val);
4443 break;
4444 case BPF_JNE:
4445 if (tnum_is_const(reg->var_off))
4446 return !tnum_equals_const(reg->var_off, val);
4447 break;
Jakub Kicinski960ea052018-12-19 22:13:04 -08004448 case BPF_JSET:
4449 if ((~reg->var_off.mask & reg->var_off.value) & val)
4450 return 1;
4451 if (!((reg->var_off.mask | reg->var_off.value) & val))
4452 return 0;
4453 break;
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004454 case BPF_JGT:
4455 if (reg->umin_value > val)
4456 return 1;
4457 else if (reg->umax_value <= val)
4458 return 0;
4459 break;
4460 case BPF_JSGT:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004461 if (reg->smin_value > sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004462 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004463 else if (reg->smax_value < sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004464 return 0;
4465 break;
4466 case BPF_JLT:
4467 if (reg->umax_value < val)
4468 return 1;
4469 else if (reg->umin_value >= val)
4470 return 0;
4471 break;
4472 case BPF_JSLT:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004473 if (reg->smax_value < sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004474 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004475 else if (reg->smin_value >= sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004476 return 0;
4477 break;
4478 case BPF_JGE:
4479 if (reg->umin_value >= val)
4480 return 1;
4481 else if (reg->umax_value < val)
4482 return 0;
4483 break;
4484 case BPF_JSGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004485 if (reg->smin_value >= sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004486 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004487 else if (reg->smax_value < sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004488 return 0;
4489 break;
4490 case BPF_JLE:
4491 if (reg->umax_value <= val)
4492 return 1;
4493 else if (reg->umin_value > val)
4494 return 0;
4495 break;
4496 case BPF_JSLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004497 if (reg->smax_value <= sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004498 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004499 else if (reg->smin_value > sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004500 return 0;
4501 break;
4502 }
4503
4504 return -1;
4505}
4506
Jiong Wang092ed092019-01-26 12:26:01 -05004507/* Generate min value of the high 32-bit from TNUM info. */
4508static u64 gen_hi_min(struct tnum var)
4509{
4510 return var.value & ~0xffffffffULL;
4511}
4512
4513/* Generate max value of the high 32-bit from TNUM info. */
4514static u64 gen_hi_max(struct tnum var)
4515{
4516 return (var.value | var.mask) & ~0xffffffffULL;
4517}
4518
4519/* Return true if VAL is compared with a s64 sign extended from s32, and they
4520 * are with the same signedness.
4521 */
4522static bool cmp_val_with_extended_s64(s64 sval, struct bpf_reg_state *reg)
4523{
4524 return ((s32)sval >= 0 &&
4525 reg->smin_value >= 0 && reg->smax_value <= S32_MAX) ||
4526 ((s32)sval < 0 &&
4527 reg->smax_value <= 0 && reg->smin_value >= S32_MIN);
4528}
4529
Josef Bacik48461132016-09-28 10:54:32 -04004530/* Adjusts the register min/max values in the case that the dst_reg is the
4531 * variable register that we are working on, and src_reg is a constant or we're
4532 * simply doing a BPF_K check.
Edward Creef1174f72017-08-07 15:26:19 +01004533 * In JEQ/JNE cases we also adjust the var_off values.
Josef Bacik48461132016-09-28 10:54:32 -04004534 */
4535static void reg_set_min_max(struct bpf_reg_state *true_reg,
4536 struct bpf_reg_state *false_reg, u64 val,
Jiong Wang092ed092019-01-26 12:26:01 -05004537 u8 opcode, bool is_jmp32)
Josef Bacik48461132016-09-28 10:54:32 -04004538{
Jiong Wanga72dafa2019-01-26 12:26:00 -05004539 s64 sval;
4540
Edward Creef1174f72017-08-07 15:26:19 +01004541 /* If the dst_reg is a pointer, we can't learn anything about its
4542 * variable offset from the compare (unless src_reg were a pointer into
4543 * the same object, but we don't bother with that.
4544 * Since false_reg and true_reg have the same type by construction, we
4545 * only need to check one of them for pointerness.
4546 */
4547 if (__is_pointer_value(false, false_reg))
4548 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02004549
Jiong Wang092ed092019-01-26 12:26:01 -05004550 val = is_jmp32 ? (u32)val : val;
4551 sval = is_jmp32 ? (s64)(s32)val : (s64)val;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004552
Josef Bacik48461132016-09-28 10:54:32 -04004553 switch (opcode) {
4554 case BPF_JEQ:
Josef Bacik48461132016-09-28 10:54:32 -04004555 case BPF_JNE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004556 {
4557 struct bpf_reg_state *reg =
4558 opcode == BPF_JEQ ? true_reg : false_reg;
4559
4560 /* For BPF_JEQ, if this is false we know nothing Jon Snow, but
4561 * if it is true we know the value for sure. Likewise for
4562 * BPF_JNE.
Josef Bacik48461132016-09-28 10:54:32 -04004563 */
Jiong Wang092ed092019-01-26 12:26:01 -05004564 if (is_jmp32) {
4565 u64 old_v = reg->var_off.value;
4566 u64 hi_mask = ~0xffffffffULL;
4567
4568 reg->var_off.value = (old_v & hi_mask) | val;
4569 reg->var_off.mask &= hi_mask;
4570 } else {
4571 __mark_reg_known(reg, val);
4572 }
Josef Bacik48461132016-09-28 10:54:32 -04004573 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004574 }
Jakub Kicinski960ea052018-12-19 22:13:04 -08004575 case BPF_JSET:
4576 false_reg->var_off = tnum_and(false_reg->var_off,
4577 tnum_const(~val));
4578 if (is_power_of_2(val))
4579 true_reg->var_off = tnum_or(true_reg->var_off,
4580 tnum_const(val));
4581 break;
Josef Bacik48461132016-09-28 10:54:32 -04004582 case BPF_JGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004583 case BPF_JGT:
4584 {
4585 u64 false_umax = opcode == BPF_JGT ? val : val - 1;
4586 u64 true_umin = opcode == BPF_JGT ? val + 1 : val;
4587
Jiong Wang092ed092019-01-26 12:26:01 -05004588 if (is_jmp32) {
4589 false_umax += gen_hi_max(false_reg->var_off);
4590 true_umin += gen_hi_min(true_reg->var_off);
4591 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004592 false_reg->umax_value = min(false_reg->umax_value, false_umax);
4593 true_reg->umin_value = max(true_reg->umin_value, true_umin);
Edward Creeb03c9f92017-08-07 15:26:36 +01004594 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004595 }
Josef Bacik48461132016-09-28 10:54:32 -04004596 case BPF_JSGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004597 case BPF_JSGT:
4598 {
4599 s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1;
4600 s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval;
4601
Jiong Wang092ed092019-01-26 12:26:01 -05004602 /* If the full s64 was not sign-extended from s32 then don't
4603 * deduct further info.
4604 */
4605 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
4606 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004607 false_reg->smax_value = min(false_reg->smax_value, false_smax);
4608 true_reg->smin_value = max(true_reg->smin_value, true_smin);
Josef Bacik48461132016-09-28 10:54:32 -04004609 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004610 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004611 case BPF_JLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004612 case BPF_JLT:
4613 {
4614 u64 false_umin = opcode == BPF_JLT ? val : val + 1;
4615 u64 true_umax = opcode == BPF_JLT ? val - 1 : val;
4616
Jiong Wang092ed092019-01-26 12:26:01 -05004617 if (is_jmp32) {
4618 false_umin += gen_hi_min(false_reg->var_off);
4619 true_umax += gen_hi_max(true_reg->var_off);
4620 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004621 false_reg->umin_value = max(false_reg->umin_value, false_umin);
4622 true_reg->umax_value = min(true_reg->umax_value, true_umax);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004623 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004624 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004625 case BPF_JSLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004626 case BPF_JSLT:
4627 {
4628 s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1;
4629 s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval;
4630
Jiong Wang092ed092019-01-26 12:26:01 -05004631 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
4632 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004633 false_reg->smin_value = max(false_reg->smin_value, false_smin);
4634 true_reg->smax_value = min(true_reg->smax_value, true_smax);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004635 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004636 }
Josef Bacik48461132016-09-28 10:54:32 -04004637 default:
4638 break;
4639 }
4640
Edward Creeb03c9f92017-08-07 15:26:36 +01004641 __reg_deduce_bounds(false_reg);
4642 __reg_deduce_bounds(true_reg);
4643 /* We might have learned some bits from the bounds. */
4644 __reg_bound_offset(false_reg);
4645 __reg_bound_offset(true_reg);
4646 /* Intersecting with the old var_off might have improved our bounds
4647 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
4648 * then new var_off is (0; 0x7f...fc) which improves our umax.
4649 */
4650 __update_reg_bounds(false_reg);
4651 __update_reg_bounds(true_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004652}
4653
Edward Creef1174f72017-08-07 15:26:19 +01004654/* Same as above, but for the case that dst_reg holds a constant and src_reg is
4655 * the variable reg.
Josef Bacik48461132016-09-28 10:54:32 -04004656 */
4657static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
4658 struct bpf_reg_state *false_reg, u64 val,
Jiong Wang092ed092019-01-26 12:26:01 -05004659 u8 opcode, bool is_jmp32)
Josef Bacik48461132016-09-28 10:54:32 -04004660{
Jiong Wanga72dafa2019-01-26 12:26:00 -05004661 s64 sval;
4662
Edward Creef1174f72017-08-07 15:26:19 +01004663 if (__is_pointer_value(false, false_reg))
4664 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02004665
Jiong Wang092ed092019-01-26 12:26:01 -05004666 val = is_jmp32 ? (u32)val : val;
4667 sval = is_jmp32 ? (s64)(s32)val : (s64)val;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004668
Josef Bacik48461132016-09-28 10:54:32 -04004669 switch (opcode) {
4670 case BPF_JEQ:
Josef Bacik48461132016-09-28 10:54:32 -04004671 case BPF_JNE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004672 {
4673 struct bpf_reg_state *reg =
4674 opcode == BPF_JEQ ? true_reg : false_reg;
4675
Jiong Wang092ed092019-01-26 12:26:01 -05004676 if (is_jmp32) {
4677 u64 old_v = reg->var_off.value;
4678 u64 hi_mask = ~0xffffffffULL;
4679
4680 reg->var_off.value = (old_v & hi_mask) | val;
4681 reg->var_off.mask &= hi_mask;
4682 } else {
4683 __mark_reg_known(reg, val);
4684 }
Josef Bacik48461132016-09-28 10:54:32 -04004685 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004686 }
Jakub Kicinski960ea052018-12-19 22:13:04 -08004687 case BPF_JSET:
4688 false_reg->var_off = tnum_and(false_reg->var_off,
4689 tnum_const(~val));
4690 if (is_power_of_2(val))
4691 true_reg->var_off = tnum_or(true_reg->var_off,
4692 tnum_const(val));
4693 break;
Josef Bacik48461132016-09-28 10:54:32 -04004694 case BPF_JGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004695 case BPF_JGT:
4696 {
4697 u64 false_umin = opcode == BPF_JGT ? val : val + 1;
4698 u64 true_umax = opcode == BPF_JGT ? val - 1 : val;
4699
Jiong Wang092ed092019-01-26 12:26:01 -05004700 if (is_jmp32) {
4701 false_umin += gen_hi_min(false_reg->var_off);
4702 true_umax += gen_hi_max(true_reg->var_off);
4703 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004704 false_reg->umin_value = max(false_reg->umin_value, false_umin);
4705 true_reg->umax_value = min(true_reg->umax_value, true_umax);
Edward Creeb03c9f92017-08-07 15:26:36 +01004706 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004707 }
Josef Bacik48461132016-09-28 10:54:32 -04004708 case BPF_JSGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004709 case BPF_JSGT:
4710 {
4711 s64 false_smin = opcode == BPF_JSGT ? sval : sval + 1;
4712 s64 true_smax = opcode == BPF_JSGT ? sval - 1 : sval;
4713
Jiong Wang092ed092019-01-26 12:26:01 -05004714 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
4715 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004716 false_reg->smin_value = max(false_reg->smin_value, false_smin);
4717 true_reg->smax_value = min(true_reg->smax_value, true_smax);
Josef Bacik48461132016-09-28 10:54:32 -04004718 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004719 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004720 case BPF_JLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004721 case BPF_JLT:
4722 {
4723 u64 false_umax = opcode == BPF_JLT ? val : val - 1;
4724 u64 true_umin = opcode == BPF_JLT ? val + 1 : val;
4725
Jiong Wang092ed092019-01-26 12:26:01 -05004726 if (is_jmp32) {
4727 false_umax += gen_hi_max(false_reg->var_off);
4728 true_umin += gen_hi_min(true_reg->var_off);
4729 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004730 false_reg->umax_value = min(false_reg->umax_value, false_umax);
4731 true_reg->umin_value = max(true_reg->umin_value, true_umin);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004732 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004733 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004734 case BPF_JSLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004735 case BPF_JSLT:
4736 {
4737 s64 false_smax = opcode == BPF_JSLT ? sval : sval - 1;
4738 s64 true_smin = opcode == BPF_JSLT ? sval + 1 : sval;
4739
Jiong Wang092ed092019-01-26 12:26:01 -05004740 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
4741 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004742 false_reg->smax_value = min(false_reg->smax_value, false_smax);
4743 true_reg->smin_value = max(true_reg->smin_value, true_smin);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004744 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004745 }
Josef Bacik48461132016-09-28 10:54:32 -04004746 default:
4747 break;
4748 }
4749
Edward Creeb03c9f92017-08-07 15:26:36 +01004750 __reg_deduce_bounds(false_reg);
4751 __reg_deduce_bounds(true_reg);
4752 /* We might have learned some bits from the bounds. */
4753 __reg_bound_offset(false_reg);
4754 __reg_bound_offset(true_reg);
4755 /* Intersecting with the old var_off might have improved our bounds
4756 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
4757 * then new var_off is (0; 0x7f...fc) which improves our umax.
4758 */
4759 __update_reg_bounds(false_reg);
4760 __update_reg_bounds(true_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004761}
4762
4763/* Regs are known to be equal, so intersect their min/max/var_off */
4764static void __reg_combine_min_max(struct bpf_reg_state *src_reg,
4765 struct bpf_reg_state *dst_reg)
4766{
Edward Creeb03c9f92017-08-07 15:26:36 +01004767 src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value,
4768 dst_reg->umin_value);
4769 src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value,
4770 dst_reg->umax_value);
4771 src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value,
4772 dst_reg->smin_value);
4773 src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value,
4774 dst_reg->smax_value);
Edward Creef1174f72017-08-07 15:26:19 +01004775 src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off,
4776 dst_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01004777 /* We might have learned new bounds from the var_off. */
4778 __update_reg_bounds(src_reg);
4779 __update_reg_bounds(dst_reg);
4780 /* We might have learned something about the sign bit. */
4781 __reg_deduce_bounds(src_reg);
4782 __reg_deduce_bounds(dst_reg);
4783 /* We might have learned some bits from the bounds. */
4784 __reg_bound_offset(src_reg);
4785 __reg_bound_offset(dst_reg);
4786 /* Intersecting with the old var_off might have improved our bounds
4787 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
4788 * then new var_off is (0; 0x7f...fc) which improves our umax.
4789 */
4790 __update_reg_bounds(src_reg);
4791 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004792}
4793
4794static void reg_combine_min_max(struct bpf_reg_state *true_src,
4795 struct bpf_reg_state *true_dst,
4796 struct bpf_reg_state *false_src,
4797 struct bpf_reg_state *false_dst,
4798 u8 opcode)
4799{
4800 switch (opcode) {
4801 case BPF_JEQ:
4802 __reg_combine_min_max(true_src, true_dst);
4803 break;
4804 case BPF_JNE:
4805 __reg_combine_min_max(false_src, false_dst);
Edward Creeb03c9f92017-08-07 15:26:36 +01004806 break;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02004807 }
Josef Bacik48461132016-09-28 10:54:32 -04004808}
4809
Joe Stringerfd978bf72018-10-02 13:35:35 -07004810static void mark_ptr_or_null_reg(struct bpf_func_state *state,
4811 struct bpf_reg_state *reg, u32 id,
Joe Stringer840b9612018-10-02 13:35:32 -07004812 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02004813{
Joe Stringer840b9612018-10-02 13:35:32 -07004814 if (reg_type_may_be_null(reg->type) && reg->id == id) {
Edward Creef1174f72017-08-07 15:26:19 +01004815 /* Old offset (both fixed and variable parts) should
4816 * have been known-zero, because we don't allow pointer
4817 * arithmetic on pointers that might be NULL.
4818 */
Edward Creeb03c9f92017-08-07 15:26:36 +01004819 if (WARN_ON_ONCE(reg->smin_value || reg->smax_value ||
4820 !tnum_equals_const(reg->var_off, 0) ||
Edward Creef1174f72017-08-07 15:26:19 +01004821 reg->off)) {
Edward Creeb03c9f92017-08-07 15:26:36 +01004822 __mark_reg_known_zero(reg);
4823 reg->off = 0;
Edward Creef1174f72017-08-07 15:26:19 +01004824 }
4825 if (is_null) {
4826 reg->type = SCALAR_VALUE;
Joe Stringer840b9612018-10-02 13:35:32 -07004827 } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
4828 if (reg->map_ptr->inner_map_meta) {
4829 reg->type = CONST_PTR_TO_MAP;
4830 reg->map_ptr = reg->map_ptr->inner_map_meta;
4831 } else {
4832 reg->type = PTR_TO_MAP_VALUE;
4833 }
Joe Stringerc64b7982018-10-02 13:35:33 -07004834 } else if (reg->type == PTR_TO_SOCKET_OR_NULL) {
4835 reg->type = PTR_TO_SOCKET;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08004836 } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) {
4837 reg->type = PTR_TO_SOCK_COMMON;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08004838 } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) {
4839 reg->type = PTR_TO_TCP_SOCK;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004840 }
Martin KaFai Lau1b986582019-03-12 10:23:02 -07004841 if (is_null) {
4842 /* We don't need id and ref_obj_id from this point
4843 * onwards anymore, thus we should better reset it,
4844 * so that state pruning has chances to take effect.
4845 */
4846 reg->id = 0;
4847 reg->ref_obj_id = 0;
4848 } else if (!reg_may_point_to_spin_lock(reg)) {
4849 /* For not-NULL ptr, reg->ref_obj_id will be reset
4850 * in release_reg_references().
4851 *
4852 * reg->id is still used by spin_lock ptr. Other
4853 * than spin_lock ptr type, reg->id can be reset.
Joe Stringerfd978bf72018-10-02 13:35:35 -07004854 */
4855 reg->id = 0;
4856 }
Thomas Graf57a09bf2016-10-18 19:51:19 +02004857 }
4858}
4859
4860/* The logic is similar to find_good_pkt_pointers(), both could eventually
4861 * be folded together at some point.
4862 */
Joe Stringer840b9612018-10-02 13:35:32 -07004863static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno,
4864 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02004865{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004866 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Joe Stringerf3709f62018-10-02 13:35:29 -07004867 struct bpf_reg_state *reg, *regs = state->regs;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07004868 u32 ref_obj_id = regs[regno].ref_obj_id;
Daniel Borkmanna08dd0d2016-12-15 01:30:06 +01004869 u32 id = regs[regno].id;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004870 int i, j;
Thomas Graf57a09bf2016-10-18 19:51:19 +02004871
Martin KaFai Lau1b986582019-03-12 10:23:02 -07004872 if (ref_obj_id && ref_obj_id == id && is_null)
4873 /* regs[regno] is in the " == NULL" branch.
4874 * No one could have freed the reference state before
4875 * doing the NULL check.
4876 */
4877 WARN_ON_ONCE(release_reference_state(state, id));
Joe Stringerfd978bf72018-10-02 13:35:35 -07004878
Thomas Graf57a09bf2016-10-18 19:51:19 +02004879 for (i = 0; i < MAX_BPF_REG; i++)
Joe Stringerfd978bf72018-10-02 13:35:35 -07004880 mark_ptr_or_null_reg(state, &regs[i], id, is_null);
Thomas Graf57a09bf2016-10-18 19:51:19 +02004881
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004882 for (j = 0; j <= vstate->curframe; j++) {
4883 state = vstate->frame[j];
Joe Stringerf3709f62018-10-02 13:35:29 -07004884 bpf_for_each_spilled_reg(i, state, reg) {
4885 if (!reg)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004886 continue;
Joe Stringerfd978bf72018-10-02 13:35:35 -07004887 mark_ptr_or_null_reg(state, reg, id, is_null);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004888 }
Thomas Graf57a09bf2016-10-18 19:51:19 +02004889 }
4890}
4891
Daniel Borkmann5beca082017-11-01 23:58:10 +01004892static bool try_match_pkt_pointers(const struct bpf_insn *insn,
4893 struct bpf_reg_state *dst_reg,
4894 struct bpf_reg_state *src_reg,
4895 struct bpf_verifier_state *this_branch,
4896 struct bpf_verifier_state *other_branch)
4897{
4898 if (BPF_SRC(insn->code) != BPF_X)
4899 return false;
4900
Jiong Wang092ed092019-01-26 12:26:01 -05004901 /* Pointers are always 64-bit. */
4902 if (BPF_CLASS(insn->code) == BPF_JMP32)
4903 return false;
4904
Daniel Borkmann5beca082017-11-01 23:58:10 +01004905 switch (BPF_OP(insn->code)) {
4906 case BPF_JGT:
4907 if ((dst_reg->type == PTR_TO_PACKET &&
4908 src_reg->type == PTR_TO_PACKET_END) ||
4909 (dst_reg->type == PTR_TO_PACKET_META &&
4910 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
4911 /* pkt_data' > pkt_end, pkt_meta' > pkt_data */
4912 find_good_pkt_pointers(this_branch, dst_reg,
4913 dst_reg->type, false);
4914 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
4915 src_reg->type == PTR_TO_PACKET) ||
4916 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
4917 src_reg->type == PTR_TO_PACKET_META)) {
4918 /* pkt_end > pkt_data', pkt_data > pkt_meta' */
4919 find_good_pkt_pointers(other_branch, src_reg,
4920 src_reg->type, true);
4921 } else {
4922 return false;
4923 }
4924 break;
4925 case BPF_JLT:
4926 if ((dst_reg->type == PTR_TO_PACKET &&
4927 src_reg->type == PTR_TO_PACKET_END) ||
4928 (dst_reg->type == PTR_TO_PACKET_META &&
4929 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
4930 /* pkt_data' < pkt_end, pkt_meta' < pkt_data */
4931 find_good_pkt_pointers(other_branch, dst_reg,
4932 dst_reg->type, true);
4933 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
4934 src_reg->type == PTR_TO_PACKET) ||
4935 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
4936 src_reg->type == PTR_TO_PACKET_META)) {
4937 /* pkt_end < pkt_data', pkt_data > pkt_meta' */
4938 find_good_pkt_pointers(this_branch, src_reg,
4939 src_reg->type, false);
4940 } else {
4941 return false;
4942 }
4943 break;
4944 case BPF_JGE:
4945 if ((dst_reg->type == PTR_TO_PACKET &&
4946 src_reg->type == PTR_TO_PACKET_END) ||
4947 (dst_reg->type == PTR_TO_PACKET_META &&
4948 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
4949 /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */
4950 find_good_pkt_pointers(this_branch, dst_reg,
4951 dst_reg->type, true);
4952 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
4953 src_reg->type == PTR_TO_PACKET) ||
4954 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
4955 src_reg->type == PTR_TO_PACKET_META)) {
4956 /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */
4957 find_good_pkt_pointers(other_branch, src_reg,
4958 src_reg->type, false);
4959 } else {
4960 return false;
4961 }
4962 break;
4963 case BPF_JLE:
4964 if ((dst_reg->type == PTR_TO_PACKET &&
4965 src_reg->type == PTR_TO_PACKET_END) ||
4966 (dst_reg->type == PTR_TO_PACKET_META &&
4967 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
4968 /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */
4969 find_good_pkt_pointers(other_branch, dst_reg,
4970 dst_reg->type, false);
4971 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
4972 src_reg->type == PTR_TO_PACKET) ||
4973 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
4974 src_reg->type == PTR_TO_PACKET_META)) {
4975 /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */
4976 find_good_pkt_pointers(this_branch, src_reg,
4977 src_reg->type, true);
4978 } else {
4979 return false;
4980 }
4981 break;
4982 default:
4983 return false;
4984 }
4985
4986 return true;
4987}
4988
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004989static int check_cond_jmp_op(struct bpf_verifier_env *env,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004990 struct bpf_insn *insn, int *insn_idx)
4991{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004992 struct bpf_verifier_state *this_branch = env->cur_state;
4993 struct bpf_verifier_state *other_branch;
4994 struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs;
4995 struct bpf_reg_state *dst_reg, *other_branch_regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004996 u8 opcode = BPF_OP(insn->code);
Jiong Wang092ed092019-01-26 12:26:01 -05004997 bool is_jmp32;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004998 int err;
4999
Jiong Wang092ed092019-01-26 12:26:01 -05005000 /* Only conditional jumps are expected to reach here. */
5001 if (opcode == BPF_JA || opcode > BPF_JSLE) {
5002 verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005003 return -EINVAL;
5004 }
5005
5006 if (BPF_SRC(insn->code) == BPF_X) {
5007 if (insn->imm != 0) {
Jiong Wang092ed092019-01-26 12:26:01 -05005008 verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005009 return -EINVAL;
5010 }
5011
5012 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01005013 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005014 if (err)
5015 return err;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07005016
5017 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005018 verbose(env, "R%d pointer comparison prohibited\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07005019 insn->src_reg);
5020 return -EACCES;
5021 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005022 } else {
5023 if (insn->src_reg != BPF_REG_0) {
Jiong Wang092ed092019-01-26 12:26:01 -05005024 verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005025 return -EINVAL;
5026 }
5027 }
5028
5029 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01005030 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005031 if (err)
5032 return err;
5033
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07005034 dst_reg = &regs[insn->dst_reg];
Jiong Wang092ed092019-01-26 12:26:01 -05005035 is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32;
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07005036
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08005037 if (BPF_SRC(insn->code) == BPF_K) {
Jiong Wang092ed092019-01-26 12:26:01 -05005038 int pred = is_branch_taken(dst_reg, insn->imm, opcode,
5039 is_jmp32);
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08005040
5041 if (pred == 1) {
5042 /* only follow the goto, ignore fall-through */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005043 *insn_idx += insn->off;
5044 return 0;
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08005045 } else if (pred == 0) {
5046 /* only follow fall-through branch, since
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005047 * that's where the program will go
5048 */
5049 return 0;
5050 }
5051 }
5052
Daniel Borkmann979d63d2019-01-03 00:58:34 +01005053 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx,
5054 false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005055 if (!other_branch)
5056 return -EFAULT;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005057 other_branch_regs = other_branch->frame[other_branch->curframe]->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005058
Josef Bacik48461132016-09-28 10:54:32 -04005059 /* detect if we are comparing against a constant value so we can adjust
5060 * our min/max values for our dst register.
Edward Creef1174f72017-08-07 15:26:19 +01005061 * this is only legit if both are scalars (or pointers to the same
5062 * object, I suppose, but we don't support that right now), because
5063 * otherwise the different base pointers mean the offsets aren't
5064 * comparable.
Josef Bacik48461132016-09-28 10:54:32 -04005065 */
5066 if (BPF_SRC(insn->code) == BPF_X) {
Jiong Wang092ed092019-01-26 12:26:01 -05005067 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
5068 struct bpf_reg_state lo_reg0 = *dst_reg;
5069 struct bpf_reg_state lo_reg1 = *src_reg;
5070 struct bpf_reg_state *src_lo, *dst_lo;
5071
5072 dst_lo = &lo_reg0;
5073 src_lo = &lo_reg1;
5074 coerce_reg_to_size(dst_lo, 4);
5075 coerce_reg_to_size(src_lo, 4);
5076
Edward Creef1174f72017-08-07 15:26:19 +01005077 if (dst_reg->type == SCALAR_VALUE &&
Jiong Wang092ed092019-01-26 12:26:01 -05005078 src_reg->type == SCALAR_VALUE) {
5079 if (tnum_is_const(src_reg->var_off) ||
5080 (is_jmp32 && tnum_is_const(src_lo->var_off)))
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005081 reg_set_min_max(&other_branch_regs[insn->dst_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05005082 dst_reg,
5083 is_jmp32
5084 ? src_lo->var_off.value
5085 : src_reg->var_off.value,
5086 opcode, is_jmp32);
5087 else if (tnum_is_const(dst_reg->var_off) ||
5088 (is_jmp32 && tnum_is_const(dst_lo->var_off)))
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005089 reg_set_min_max_inv(&other_branch_regs[insn->src_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05005090 src_reg,
5091 is_jmp32
5092 ? dst_lo->var_off.value
5093 : dst_reg->var_off.value,
5094 opcode, is_jmp32);
5095 else if (!is_jmp32 &&
5096 (opcode == BPF_JEQ || opcode == BPF_JNE))
Edward Creef1174f72017-08-07 15:26:19 +01005097 /* Comparing for equality, we can combine knowledge */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005098 reg_combine_min_max(&other_branch_regs[insn->src_reg],
5099 &other_branch_regs[insn->dst_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05005100 src_reg, dst_reg, opcode);
Edward Creef1174f72017-08-07 15:26:19 +01005101 }
5102 } else if (dst_reg->type == SCALAR_VALUE) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005103 reg_set_min_max(&other_branch_regs[insn->dst_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05005104 dst_reg, insn->imm, opcode, is_jmp32);
Josef Bacik48461132016-09-28 10:54:32 -04005105 }
5106
Jiong Wang092ed092019-01-26 12:26:01 -05005107 /* detect if R == 0 where R is returned from bpf_map_lookup_elem().
5108 * NOTE: these optimizations below are related with pointer comparison
5109 * which will never be JMP32.
5110 */
5111 if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07005112 insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
Joe Stringer840b9612018-10-02 13:35:32 -07005113 reg_type_may_be_null(dst_reg->type)) {
5114 /* Mark all identical registers in each branch as either
Thomas Graf57a09bf2016-10-18 19:51:19 +02005115 * safe or unknown depending R == 0 or R != 0 conditional.
5116 */
Joe Stringer840b9612018-10-02 13:35:32 -07005117 mark_ptr_or_null_regs(this_branch, insn->dst_reg,
5118 opcode == BPF_JNE);
5119 mark_ptr_or_null_regs(other_branch, insn->dst_reg,
5120 opcode == BPF_JEQ);
Daniel Borkmann5beca082017-11-01 23:58:10 +01005121 } else if (!try_match_pkt_pointers(insn, dst_reg, &regs[insn->src_reg],
5122 this_branch, other_branch) &&
5123 is_pointer_value(env, insn->dst_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005124 verbose(env, "R%d pointer comparison prohibited\n",
5125 insn->dst_reg);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07005126 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005127 }
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07005128 if (env->log.level & BPF_LOG_LEVEL)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005129 print_verifier_state(env, this_branch->frame[this_branch->curframe]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005130 return 0;
5131}
5132
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005133/* verify BPF_LD_IMM64 instruction */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005134static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005135{
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02005136 struct bpf_insn_aux_data *aux = cur_aux(env);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005137 struct bpf_reg_state *regs = cur_regs(env);
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02005138 struct bpf_map *map;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005139 int err;
5140
5141 if (BPF_SIZE(insn->code) != BPF_DW) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005142 verbose(env, "invalid BPF_LD_IMM insn\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005143 return -EINVAL;
5144 }
5145 if (insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005146 verbose(env, "BPF_LD_IMM64 uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005147 return -EINVAL;
5148 }
5149
Edward Creedc503a82017-08-15 20:34:35 +01005150 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005151 if (err)
5152 return err;
5153
Jakub Kicinski6b173872016-09-21 11:43:59 +01005154 if (insn->src_reg == 0) {
Jakub Kicinski6b173872016-09-21 11:43:59 +01005155 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
5156
Edward Creef1174f72017-08-07 15:26:19 +01005157 regs[insn->dst_reg].type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01005158 __mark_reg_known(&regs[insn->dst_reg], imm);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005159 return 0;
Jakub Kicinski6b173872016-09-21 11:43:59 +01005160 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005161
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02005162 map = env->used_maps[aux->map_index];
5163 mark_reg_known_zero(env, regs, insn->dst_reg);
5164 regs[insn->dst_reg].map_ptr = map;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005165
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02005166 if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) {
5167 regs[insn->dst_reg].type = PTR_TO_MAP_VALUE;
5168 regs[insn->dst_reg].off = aux->map_off;
5169 if (map_value_has_spin_lock(map))
5170 regs[insn->dst_reg].id = ++env->id_gen;
5171 } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) {
5172 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
5173 } else {
5174 verbose(env, "bpf verifier is misconfigured\n");
5175 return -EINVAL;
5176 }
5177
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005178 return 0;
5179}
5180
Daniel Borkmann96be4322015-03-01 12:31:46 +01005181static bool may_access_skb(enum bpf_prog_type type)
5182{
5183 switch (type) {
5184 case BPF_PROG_TYPE_SOCKET_FILTER:
5185 case BPF_PROG_TYPE_SCHED_CLS:
Daniel Borkmann94caee8c2015-03-20 15:11:11 +01005186 case BPF_PROG_TYPE_SCHED_ACT:
Daniel Borkmann96be4322015-03-01 12:31:46 +01005187 return true;
5188 default:
5189 return false;
5190 }
5191}
5192
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005193/* verify safety of LD_ABS|LD_IND instructions:
5194 * - they can only appear in the programs where ctx == skb
5195 * - since they are wrappers of function calls, they scratch R1-R5 registers,
5196 * preserve R6-R9, and store return value into R0
5197 *
5198 * Implicit input:
5199 * ctx == skb == R6 == CTX
5200 *
5201 * Explicit input:
5202 * SRC == any register
5203 * IMM == 32-bit immediate
5204 *
5205 * Output:
5206 * R0 - 8/16/32-bit skb data converted to cpu endianness
5207 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005208static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005209{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005210 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005211 u8 mode = BPF_MODE(insn->code);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005212 int i, err;
5213
Daniel Borkmann24701ec2015-03-01 12:31:47 +01005214 if (!may_access_skb(env->prog->type)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005215 verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005216 return -EINVAL;
5217 }
5218
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +02005219 if (!env->ops->gen_ld_abs) {
5220 verbose(env, "bpf verifier is misconfigured\n");
5221 return -EINVAL;
5222 }
5223
Jiong Wangf910cef2018-05-02 16:17:17 -04005224 if (env->subprog_cnt > 1) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005225 /* when program has LD_ABS insn JITs and interpreter assume
5226 * that r1 == ctx == skb which is not the case for callees
5227 * that can have arbitrary arguments. It's problematic
5228 * for main prog as well since JITs would need to analyze
5229 * all functions in order to make proper register save/restore
5230 * decisions in the main prog. Hence disallow LD_ABS with calls
5231 */
5232 verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n");
5233 return -EINVAL;
5234 }
5235
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005236 if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
Alexei Starovoitovd82bccc2016-04-12 10:26:19 -07005237 BPF_SIZE(insn->code) == BPF_DW ||
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005238 (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005239 verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005240 return -EINVAL;
5241 }
5242
5243 /* check whether implicit source operand (register R6) is readable */
Edward Creedc503a82017-08-15 20:34:35 +01005244 err = check_reg_arg(env, BPF_REG_6, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005245 if (err)
5246 return err;
5247
Joe Stringerfd978bf72018-10-02 13:35:35 -07005248 /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as
5249 * gen_ld_abs() may terminate the program at runtime, leading to
5250 * reference leak.
5251 */
5252 err = check_reference_leak(env);
5253 if (err) {
5254 verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n");
5255 return err;
5256 }
5257
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08005258 if (env->cur_state->active_spin_lock) {
5259 verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n");
5260 return -EINVAL;
5261 }
5262
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005263 if (regs[BPF_REG_6].type != PTR_TO_CTX) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005264 verbose(env,
5265 "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005266 return -EINVAL;
5267 }
5268
5269 if (mode == BPF_IND) {
5270 /* check explicit source operand */
Edward Creedc503a82017-08-15 20:34:35 +01005271 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005272 if (err)
5273 return err;
5274 }
5275
5276 /* reset caller saved regs to unreadable */
Edward Creedc503a82017-08-15 20:34:35 +01005277 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005278 mark_reg_not_init(env, regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01005279 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
5280 }
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005281
5282 /* mark destination R0 register as readable, since it contains
Edward Creedc503a82017-08-15 20:34:35 +01005283 * the value fetched from the packet.
5284 * Already marked as written above.
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005285 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005286 mark_reg_unknown(env, regs, BPF_REG_0);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005287 return 0;
5288}
5289
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005290static int check_return_code(struct bpf_verifier_env *env)
5291{
5292 struct bpf_reg_state *reg;
5293 struct tnum range = tnum_range(0, 1);
5294
5295 switch (env->prog->type) {
5296 case BPF_PROG_TYPE_CGROUP_SKB:
5297 case BPF_PROG_TYPE_CGROUP_SOCK:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07005298 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005299 case BPF_PROG_TYPE_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05005300 case BPF_PROG_TYPE_CGROUP_DEVICE:
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08005301 case BPF_PROG_TYPE_CGROUP_SYSCTL:
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005302 break;
5303 default:
5304 return 0;
5305 }
5306
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005307 reg = cur_regs(env) + BPF_REG_0;
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005308 if (reg->type != SCALAR_VALUE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005309 verbose(env, "At program exit the register R0 is not a known value (%s)\n",
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005310 reg_type_str[reg->type]);
5311 return -EINVAL;
5312 }
5313
5314 if (!tnum_in(range, reg->var_off)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005315 verbose(env, "At program exit the register R0 ");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005316 if (!tnum_is_unknown(reg->var_off)) {
5317 char tn_buf[48];
5318
5319 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005320 verbose(env, "has value %s", tn_buf);
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005321 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005322 verbose(env, "has unknown scalar value");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005323 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005324 verbose(env, " should have been 0 or 1\n");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005325 return -EINVAL;
5326 }
5327 return 0;
5328}
5329
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005330/* non-recursive DFS pseudo code
5331 * 1 procedure DFS-iterative(G,v):
5332 * 2 label v as discovered
5333 * 3 let S be a stack
5334 * 4 S.push(v)
5335 * 5 while S is not empty
5336 * 6 t <- S.pop()
5337 * 7 if t is what we're looking for:
5338 * 8 return t
5339 * 9 for all edges e in G.adjacentEdges(t) do
5340 * 10 if edge e is already labelled
5341 * 11 continue with the next edge
5342 * 12 w <- G.adjacentVertex(t,e)
5343 * 13 if vertex w is not discovered and not explored
5344 * 14 label e as tree-edge
5345 * 15 label w as discovered
5346 * 16 S.push(w)
5347 * 17 continue at 5
5348 * 18 else if vertex w is discovered
5349 * 19 label e as back-edge
5350 * 20 else
5351 * 21 // vertex w is explored
5352 * 22 label e as forward- or cross-edge
5353 * 23 label t as explored
5354 * 24 S.pop()
5355 *
5356 * convention:
5357 * 0x10 - discovered
5358 * 0x11 - discovered and fall-through edge labelled
5359 * 0x12 - discovered and fall-through and branch edges labelled
5360 * 0x20 - explored
5361 */
5362
5363enum {
5364 DISCOVERED = 0x10,
5365 EXPLORED = 0x20,
5366 FALLTHROUGH = 1,
5367 BRANCH = 2,
5368};
5369
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005370#define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005371
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005372/* t, w, e - match pseudo-code above:
5373 * t - index of current instruction
5374 * w - next instruction
5375 * e - edge
5376 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005377static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005378{
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005379 int *insn_stack = env->cfg.insn_stack;
5380 int *insn_state = env->cfg.insn_state;
5381
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005382 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
5383 return 0;
5384
5385 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
5386 return 0;
5387
5388 if (w < 0 || w >= env->prog->len) {
Martin KaFai Laud9762e82018-12-13 10:41:48 -08005389 verbose_linfo(env, t, "%d: ", t);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005390 verbose(env, "jump out of range from insn %d to %d\n", t, w);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005391 return -EINVAL;
5392 }
5393
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005394 if (e == BRANCH)
5395 /* mark branch target for state pruning */
5396 env->explored_states[w] = STATE_LIST_MARK;
5397
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005398 if (insn_state[w] == 0) {
5399 /* tree-edge */
5400 insn_state[t] = DISCOVERED | e;
5401 insn_state[w] = DISCOVERED;
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005402 if (env->cfg.cur_stack >= env->prog->len)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005403 return -E2BIG;
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005404 insn_stack[env->cfg.cur_stack++] = w;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005405 return 1;
5406 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
Martin KaFai Laud9762e82018-12-13 10:41:48 -08005407 verbose_linfo(env, t, "%d: ", t);
5408 verbose_linfo(env, w, "%d: ", w);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005409 verbose(env, "back-edge from insn %d to %d\n", t, w);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005410 return -EINVAL;
5411 } else if (insn_state[w] == EXPLORED) {
5412 /* forward- or cross-edge */
5413 insn_state[t] = DISCOVERED | e;
5414 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005415 verbose(env, "insn state internal bug\n");
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005416 return -EFAULT;
5417 }
5418 return 0;
5419}
5420
5421/* non-recursive depth-first-search to detect loops in BPF program
5422 * loop == back-edge in directed graph
5423 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005424static int check_cfg(struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005425{
5426 struct bpf_insn *insns = env->prog->insnsi;
5427 int insn_cnt = env->prog->len;
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005428 int *insn_stack, *insn_state;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005429 int ret = 0;
5430 int i, t;
5431
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005432 insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005433 if (!insn_state)
5434 return -ENOMEM;
5435
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005436 insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005437 if (!insn_stack) {
Alexei Starovoitov71dde682019-04-01 21:27:43 -07005438 kvfree(insn_state);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005439 return -ENOMEM;
5440 }
5441
5442 insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
5443 insn_stack[0] = 0; /* 0 is the first instruction */
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005444 env->cfg.cur_stack = 1;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005445
5446peek_stack:
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005447 if (env->cfg.cur_stack == 0)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005448 goto check_state;
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005449 t = insn_stack[env->cfg.cur_stack - 1];
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005450
Jiong Wang092ed092019-01-26 12:26:01 -05005451 if (BPF_CLASS(insns[t].code) == BPF_JMP ||
5452 BPF_CLASS(insns[t].code) == BPF_JMP32) {
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005453 u8 opcode = BPF_OP(insns[t].code);
5454
5455 if (opcode == BPF_EXIT) {
5456 goto mark_explored;
5457 } else if (opcode == BPF_CALL) {
5458 ret = push_insn(t, t + 1, FALLTHROUGH, env);
5459 if (ret == 1)
5460 goto peek_stack;
5461 else if (ret < 0)
5462 goto err_free;
Daniel Borkmann07016152016-04-05 22:33:17 +02005463 if (t + 1 < insn_cnt)
5464 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08005465 if (insns[t].src_reg == BPF_PSEUDO_CALL) {
5466 env->explored_states[t] = STATE_LIST_MARK;
5467 ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env);
5468 if (ret == 1)
5469 goto peek_stack;
5470 else if (ret < 0)
5471 goto err_free;
5472 }
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005473 } else if (opcode == BPF_JA) {
5474 if (BPF_SRC(insns[t].code) != BPF_K) {
5475 ret = -EINVAL;
5476 goto err_free;
5477 }
5478 /* unconditional jump with single edge */
5479 ret = push_insn(t, t + insns[t].off + 1,
5480 FALLTHROUGH, env);
5481 if (ret == 1)
5482 goto peek_stack;
5483 else if (ret < 0)
5484 goto err_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005485 /* tell verifier to check for equivalent states
5486 * after every call and jump
5487 */
Alexei Starovoitovc3de6312015-04-14 15:57:13 -07005488 if (t + 1 < insn_cnt)
5489 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005490 } else {
5491 /* conditional jump with two edges */
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02005492 env->explored_states[t] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005493 ret = push_insn(t, t + 1, FALLTHROUGH, env);
5494 if (ret == 1)
5495 goto peek_stack;
5496 else if (ret < 0)
5497 goto err_free;
5498
5499 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
5500 if (ret == 1)
5501 goto peek_stack;
5502 else if (ret < 0)
5503 goto err_free;
5504 }
5505 } else {
5506 /* all other non-branch instructions with single
5507 * fall-through edge
5508 */
5509 ret = push_insn(t, t + 1, FALLTHROUGH, env);
5510 if (ret == 1)
5511 goto peek_stack;
5512 else if (ret < 0)
5513 goto err_free;
5514 }
5515
5516mark_explored:
5517 insn_state[t] = EXPLORED;
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005518 if (env->cfg.cur_stack-- <= 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005519 verbose(env, "pop stack internal bug\n");
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005520 ret = -EFAULT;
5521 goto err_free;
5522 }
5523 goto peek_stack;
5524
5525check_state:
5526 for (i = 0; i < insn_cnt; i++) {
5527 if (insn_state[i] != EXPLORED) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005528 verbose(env, "unreachable insn %d\n", i);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005529 ret = -EINVAL;
5530 goto err_free;
5531 }
5532 }
5533 ret = 0; /* cfg looks good */
5534
5535err_free:
Alexei Starovoitov71dde682019-04-01 21:27:43 -07005536 kvfree(insn_state);
5537 kvfree(insn_stack);
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005538 env->cfg.insn_state = env->cfg.insn_stack = NULL;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005539 return ret;
5540}
5541
Yonghong Song838e9692018-11-19 15:29:11 -08005542/* The minimum supported BTF func info size */
5543#define MIN_BPF_FUNCINFO_SIZE 8
5544#define MAX_FUNCINFO_REC_SIZE 252
5545
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005546static int check_btf_func(struct bpf_verifier_env *env,
5547 const union bpf_attr *attr,
5548 union bpf_attr __user *uattr)
Yonghong Song838e9692018-11-19 15:29:11 -08005549{
Peter Oskolkovd0b28182019-01-16 10:43:01 -08005550 u32 i, nfuncs, urec_size, min_size;
Yonghong Song838e9692018-11-19 15:29:11 -08005551 u32 krec_size = sizeof(struct bpf_func_info);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005552 struct bpf_func_info *krecord;
Yonghong Song838e9692018-11-19 15:29:11 -08005553 const struct btf_type *type;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005554 struct bpf_prog *prog;
5555 const struct btf *btf;
Yonghong Song838e9692018-11-19 15:29:11 -08005556 void __user *urecord;
Peter Oskolkovd0b28182019-01-16 10:43:01 -08005557 u32 prev_offset = 0;
Yonghong Song838e9692018-11-19 15:29:11 -08005558 int ret = 0;
5559
5560 nfuncs = attr->func_info_cnt;
5561 if (!nfuncs)
5562 return 0;
5563
5564 if (nfuncs != env->subprog_cnt) {
5565 verbose(env, "number of funcs in func_info doesn't match number of subprogs\n");
5566 return -EINVAL;
5567 }
5568
5569 urec_size = attr->func_info_rec_size;
5570 if (urec_size < MIN_BPF_FUNCINFO_SIZE ||
5571 urec_size > MAX_FUNCINFO_REC_SIZE ||
5572 urec_size % sizeof(u32)) {
5573 verbose(env, "invalid func info rec size %u\n", urec_size);
5574 return -EINVAL;
5575 }
5576
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005577 prog = env->prog;
5578 btf = prog->aux->btf;
Yonghong Song838e9692018-11-19 15:29:11 -08005579
5580 urecord = u64_to_user_ptr(attr->func_info);
5581 min_size = min_t(u32, krec_size, urec_size);
5582
Yonghong Songba64e7d2018-11-24 23:20:44 -08005583 krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005584 if (!krecord)
5585 return -ENOMEM;
Yonghong Songba64e7d2018-11-24 23:20:44 -08005586
Yonghong Song838e9692018-11-19 15:29:11 -08005587 for (i = 0; i < nfuncs; i++) {
5588 ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size);
5589 if (ret) {
5590 if (ret == -E2BIG) {
5591 verbose(env, "nonzero tailing record in func info");
5592 /* set the size kernel expects so loader can zero
5593 * out the rest of the record.
5594 */
5595 if (put_user(min_size, &uattr->func_info_rec_size))
5596 ret = -EFAULT;
5597 }
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005598 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005599 }
5600
Yonghong Songba64e7d2018-11-24 23:20:44 -08005601 if (copy_from_user(&krecord[i], urecord, min_size)) {
Yonghong Song838e9692018-11-19 15:29:11 -08005602 ret = -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005603 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005604 }
5605
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005606 /* check insn_off */
Yonghong Song838e9692018-11-19 15:29:11 -08005607 if (i == 0) {
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005608 if (krecord[i].insn_off) {
Yonghong Song838e9692018-11-19 15:29:11 -08005609 verbose(env,
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005610 "nonzero insn_off %u for the first func info record",
5611 krecord[i].insn_off);
Yonghong Song838e9692018-11-19 15:29:11 -08005612 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005613 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005614 }
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005615 } else if (krecord[i].insn_off <= prev_offset) {
Yonghong Song838e9692018-11-19 15:29:11 -08005616 verbose(env,
5617 "same or smaller insn offset (%u) than previous func info record (%u)",
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005618 krecord[i].insn_off, prev_offset);
Yonghong Song838e9692018-11-19 15:29:11 -08005619 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005620 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005621 }
5622
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005623 if (env->subprog_info[i].start != krecord[i].insn_off) {
Yonghong Song838e9692018-11-19 15:29:11 -08005624 verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n");
5625 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005626 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005627 }
5628
5629 /* check type_id */
Yonghong Songba64e7d2018-11-24 23:20:44 -08005630 type = btf_type_by_id(btf, krecord[i].type_id);
Yonghong Song838e9692018-11-19 15:29:11 -08005631 if (!type || BTF_INFO_KIND(type->info) != BTF_KIND_FUNC) {
5632 verbose(env, "invalid type id %d in func info",
Yonghong Songba64e7d2018-11-24 23:20:44 -08005633 krecord[i].type_id);
Yonghong Song838e9692018-11-19 15:29:11 -08005634 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005635 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005636 }
5637
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005638 prev_offset = krecord[i].insn_off;
Yonghong Song838e9692018-11-19 15:29:11 -08005639 urecord += urec_size;
5640 }
5641
Yonghong Songba64e7d2018-11-24 23:20:44 -08005642 prog->aux->func_info = krecord;
5643 prog->aux->func_info_cnt = nfuncs;
Yonghong Song838e9692018-11-19 15:29:11 -08005644 return 0;
5645
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005646err_free:
Yonghong Songba64e7d2018-11-24 23:20:44 -08005647 kvfree(krecord);
Yonghong Song838e9692018-11-19 15:29:11 -08005648 return ret;
5649}
5650
Yonghong Songba64e7d2018-11-24 23:20:44 -08005651static void adjust_btf_func(struct bpf_verifier_env *env)
5652{
5653 int i;
5654
5655 if (!env->prog->aux->func_info)
5656 return;
5657
5658 for (i = 0; i < env->subprog_cnt; i++)
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005659 env->prog->aux->func_info[i].insn_off = env->subprog_info[i].start;
Yonghong Songba64e7d2018-11-24 23:20:44 -08005660}
5661
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005662#define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \
5663 sizeof(((struct bpf_line_info *)(0))->line_col))
5664#define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE
5665
5666static int check_btf_line(struct bpf_verifier_env *env,
5667 const union bpf_attr *attr,
5668 union bpf_attr __user *uattr)
5669{
5670 u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0;
5671 struct bpf_subprog_info *sub;
5672 struct bpf_line_info *linfo;
5673 struct bpf_prog *prog;
5674 const struct btf *btf;
5675 void __user *ulinfo;
5676 int err;
5677
5678 nr_linfo = attr->line_info_cnt;
5679 if (!nr_linfo)
5680 return 0;
5681
5682 rec_size = attr->line_info_rec_size;
5683 if (rec_size < MIN_BPF_LINEINFO_SIZE ||
5684 rec_size > MAX_LINEINFO_REC_SIZE ||
5685 rec_size & (sizeof(u32) - 1))
5686 return -EINVAL;
5687
5688 /* Need to zero it in case the userspace may
5689 * pass in a smaller bpf_line_info object.
5690 */
5691 linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info),
5692 GFP_KERNEL | __GFP_NOWARN);
5693 if (!linfo)
5694 return -ENOMEM;
5695
5696 prog = env->prog;
5697 btf = prog->aux->btf;
5698
5699 s = 0;
5700 sub = env->subprog_info;
5701 ulinfo = u64_to_user_ptr(attr->line_info);
5702 expected_size = sizeof(struct bpf_line_info);
5703 ncopy = min_t(u32, expected_size, rec_size);
5704 for (i = 0; i < nr_linfo; i++) {
5705 err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size);
5706 if (err) {
5707 if (err == -E2BIG) {
5708 verbose(env, "nonzero tailing record in line_info");
5709 if (put_user(expected_size,
5710 &uattr->line_info_rec_size))
5711 err = -EFAULT;
5712 }
5713 goto err_free;
5714 }
5715
5716 if (copy_from_user(&linfo[i], ulinfo, ncopy)) {
5717 err = -EFAULT;
5718 goto err_free;
5719 }
5720
5721 /*
5722 * Check insn_off to ensure
5723 * 1) strictly increasing AND
5724 * 2) bounded by prog->len
5725 *
5726 * The linfo[0].insn_off == 0 check logically falls into
5727 * the later "missing bpf_line_info for func..." case
5728 * because the first linfo[0].insn_off must be the
5729 * first sub also and the first sub must have
5730 * subprog_info[0].start == 0.
5731 */
5732 if ((i && linfo[i].insn_off <= prev_offset) ||
5733 linfo[i].insn_off >= prog->len) {
5734 verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n",
5735 i, linfo[i].insn_off, prev_offset,
5736 prog->len);
5737 err = -EINVAL;
5738 goto err_free;
5739 }
5740
Martin KaFai Laufdbaa0b2018-12-19 13:01:01 -08005741 if (!prog->insnsi[linfo[i].insn_off].code) {
5742 verbose(env,
5743 "Invalid insn code at line_info[%u].insn_off\n",
5744 i);
5745 err = -EINVAL;
5746 goto err_free;
5747 }
5748
Martin KaFai Lau23127b32018-12-13 10:41:46 -08005749 if (!btf_name_by_offset(btf, linfo[i].line_off) ||
5750 !btf_name_by_offset(btf, linfo[i].file_name_off)) {
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005751 verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i);
5752 err = -EINVAL;
5753 goto err_free;
5754 }
5755
5756 if (s != env->subprog_cnt) {
5757 if (linfo[i].insn_off == sub[s].start) {
5758 sub[s].linfo_idx = i;
5759 s++;
5760 } else if (sub[s].start < linfo[i].insn_off) {
5761 verbose(env, "missing bpf_line_info for func#%u\n", s);
5762 err = -EINVAL;
5763 goto err_free;
5764 }
5765 }
5766
5767 prev_offset = linfo[i].insn_off;
5768 ulinfo += rec_size;
5769 }
5770
5771 if (s != env->subprog_cnt) {
5772 verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n",
5773 env->subprog_cnt - s, s);
5774 err = -EINVAL;
5775 goto err_free;
5776 }
5777
5778 prog->aux->linfo = linfo;
5779 prog->aux->nr_linfo = nr_linfo;
5780
5781 return 0;
5782
5783err_free:
5784 kvfree(linfo);
5785 return err;
5786}
5787
5788static int check_btf_info(struct bpf_verifier_env *env,
5789 const union bpf_attr *attr,
5790 union bpf_attr __user *uattr)
5791{
5792 struct btf *btf;
5793 int err;
5794
5795 if (!attr->func_info_cnt && !attr->line_info_cnt)
5796 return 0;
5797
5798 btf = btf_get_by_fd(attr->prog_btf_fd);
5799 if (IS_ERR(btf))
5800 return PTR_ERR(btf);
5801 env->prog->aux->btf = btf;
5802
5803 err = check_btf_func(env, attr, uattr);
5804 if (err)
5805 return err;
5806
5807 err = check_btf_line(env, attr, uattr);
5808 if (err)
5809 return err;
5810
5811 return 0;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005812}
5813
Edward Creef1174f72017-08-07 15:26:19 +01005814/* check %cur's range satisfies %old's */
5815static bool range_within(struct bpf_reg_state *old,
5816 struct bpf_reg_state *cur)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07005817{
Edward Creeb03c9f92017-08-07 15:26:36 +01005818 return old->umin_value <= cur->umin_value &&
5819 old->umax_value >= cur->umax_value &&
5820 old->smin_value <= cur->smin_value &&
5821 old->smax_value >= cur->smax_value;
Edward Creef1174f72017-08-07 15:26:19 +01005822}
5823
5824/* Maximum number of register states that can exist at once */
5825#define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE)
5826struct idpair {
5827 u32 old;
5828 u32 cur;
5829};
5830
5831/* If in the old state two registers had the same id, then they need to have
5832 * the same id in the new state as well. But that id could be different from
5833 * the old state, so we need to track the mapping from old to new ids.
5834 * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent
5835 * regs with old id 5 must also have new id 9 for the new state to be safe. But
5836 * regs with a different old id could still have new id 9, we don't care about
5837 * that.
5838 * So we look through our idmap to see if this old id has been seen before. If
5839 * so, we require the new id to match; otherwise, we add the id pair to the map.
5840 */
5841static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap)
5842{
5843 unsigned int i;
5844
5845 for (i = 0; i < ID_MAP_SIZE; i++) {
5846 if (!idmap[i].old) {
5847 /* Reached an empty slot; haven't seen this id before */
5848 idmap[i].old = old_id;
5849 idmap[i].cur = cur_id;
5850 return true;
5851 }
5852 if (idmap[i].old == old_id)
5853 return idmap[i].cur == cur_id;
5854 }
5855 /* We ran out of idmap slots, which should be impossible */
5856 WARN_ON_ONCE(1);
5857 return false;
5858}
5859
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -08005860static void clean_func_state(struct bpf_verifier_env *env,
5861 struct bpf_func_state *st)
5862{
5863 enum bpf_reg_liveness live;
5864 int i, j;
5865
5866 for (i = 0; i < BPF_REG_FP; i++) {
5867 live = st->regs[i].live;
5868 /* liveness must not touch this register anymore */
5869 st->regs[i].live |= REG_LIVE_DONE;
5870 if (!(live & REG_LIVE_READ))
5871 /* since the register is unused, clear its state
5872 * to make further comparison simpler
5873 */
5874 __mark_reg_not_init(&st->regs[i]);
5875 }
5876
5877 for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) {
5878 live = st->stack[i].spilled_ptr.live;
5879 /* liveness must not touch this stack slot anymore */
5880 st->stack[i].spilled_ptr.live |= REG_LIVE_DONE;
5881 if (!(live & REG_LIVE_READ)) {
5882 __mark_reg_not_init(&st->stack[i].spilled_ptr);
5883 for (j = 0; j < BPF_REG_SIZE; j++)
5884 st->stack[i].slot_type[j] = STACK_INVALID;
5885 }
5886 }
5887}
5888
5889static void clean_verifier_state(struct bpf_verifier_env *env,
5890 struct bpf_verifier_state *st)
5891{
5892 int i;
5893
5894 if (st->frame[0]->regs[0].live & REG_LIVE_DONE)
5895 /* all regs in this state in all frames were already marked */
5896 return;
5897
5898 for (i = 0; i <= st->curframe; i++)
5899 clean_func_state(env, st->frame[i]);
5900}
5901
5902/* the parentage chains form a tree.
5903 * the verifier states are added to state lists at given insn and
5904 * pushed into state stack for future exploration.
5905 * when the verifier reaches bpf_exit insn some of the verifer states
5906 * stored in the state lists have their final liveness state already,
5907 * but a lot of states will get revised from liveness point of view when
5908 * the verifier explores other branches.
5909 * Example:
5910 * 1: r0 = 1
5911 * 2: if r1 == 100 goto pc+1
5912 * 3: r0 = 2
5913 * 4: exit
5914 * when the verifier reaches exit insn the register r0 in the state list of
5915 * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch
5916 * of insn 2 and goes exploring further. At the insn 4 it will walk the
5917 * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ.
5918 *
5919 * Since the verifier pushes the branch states as it sees them while exploring
5920 * the program the condition of walking the branch instruction for the second
5921 * time means that all states below this branch were already explored and
5922 * their final liveness markes are already propagated.
5923 * Hence when the verifier completes the search of state list in is_state_visited()
5924 * we can call this clean_live_states() function to mark all liveness states
5925 * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state'
5926 * will not be used.
5927 * This function also clears the registers and stack for states that !READ
5928 * to simplify state merging.
5929 *
5930 * Important note here that walking the same branch instruction in the callee
5931 * doesn't meant that the states are DONE. The verifier has to compare
5932 * the callsites
5933 */
5934static void clean_live_states(struct bpf_verifier_env *env, int insn,
5935 struct bpf_verifier_state *cur)
5936{
5937 struct bpf_verifier_state_list *sl;
5938 int i;
5939
5940 sl = env->explored_states[insn];
5941 if (!sl)
5942 return;
5943
5944 while (sl != STATE_LIST_MARK) {
5945 if (sl->state.curframe != cur->curframe)
5946 goto next;
5947 for (i = 0; i <= cur->curframe; i++)
5948 if (sl->state.frame[i]->callsite != cur->frame[i]->callsite)
5949 goto next;
5950 clean_verifier_state(env, &sl->state);
5951next:
5952 sl = sl->next;
5953 }
5954}
5955
Edward Creef1174f72017-08-07 15:26:19 +01005956/* Returns true if (rold safe implies rcur safe) */
Edward Cree1b688a12017-08-23 15:10:50 +01005957static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
5958 struct idpair *idmap)
Edward Creef1174f72017-08-07 15:26:19 +01005959{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005960 bool equal;
5961
Edward Creedc503a82017-08-15 20:34:35 +01005962 if (!(rold->live & REG_LIVE_READ))
5963 /* explored state didn't use this */
5964 return true;
5965
Edward Cree679c7822018-08-22 20:02:19 +01005966 equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005967
5968 if (rold->type == PTR_TO_STACK)
5969 /* two stack pointers are equal only if they're pointing to
5970 * the same stack frame, since fp-8 in foo != fp-8 in bar
5971 */
5972 return equal && rold->frameno == rcur->frameno;
5973
5974 if (equal)
Edward Creef1174f72017-08-07 15:26:19 +01005975 return true;
5976
5977 if (rold->type == NOT_INIT)
5978 /* explored state can't have used this */
5979 return true;
5980 if (rcur->type == NOT_INIT)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07005981 return false;
Edward Creef1174f72017-08-07 15:26:19 +01005982 switch (rold->type) {
5983 case SCALAR_VALUE:
5984 if (rcur->type == SCALAR_VALUE) {
5985 /* new val must satisfy old val knowledge */
5986 return range_within(rold, rcur) &&
5987 tnum_in(rold->var_off, rcur->var_off);
5988 } else {
Jann Horn179d1c52017-12-18 20:11:59 -08005989 /* We're trying to use a pointer in place of a scalar.
5990 * Even if the scalar was unbounded, this could lead to
5991 * pointer leaks because scalars are allowed to leak
5992 * while pointers are not. We could make this safe in
5993 * special cases if root is calling us, but it's
5994 * probably not worth the hassle.
Edward Creef1174f72017-08-07 15:26:19 +01005995 */
Jann Horn179d1c52017-12-18 20:11:59 -08005996 return false;
Edward Creef1174f72017-08-07 15:26:19 +01005997 }
5998 case PTR_TO_MAP_VALUE:
Edward Cree1b688a12017-08-23 15:10:50 +01005999 /* If the new min/max/var_off satisfy the old ones and
6000 * everything else matches, we are OK.
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08006001 * 'id' is not compared, since it's only used for maps with
6002 * bpf_spin_lock inside map element and in such cases if
6003 * the rest of the prog is valid for one map element then
6004 * it's valid for all map elements regardless of the key
6005 * used in bpf_map_lookup()
Edward Cree1b688a12017-08-23 15:10:50 +01006006 */
6007 return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 &&
6008 range_within(rold, rcur) &&
6009 tnum_in(rold->var_off, rcur->var_off);
Edward Creef1174f72017-08-07 15:26:19 +01006010 case PTR_TO_MAP_VALUE_OR_NULL:
6011 /* a PTR_TO_MAP_VALUE could be safe to use as a
6012 * PTR_TO_MAP_VALUE_OR_NULL into the same map.
6013 * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL-
6014 * checked, doing so could have affected others with the same
6015 * id, and we can't check for that because we lost the id when
6016 * we converted to a PTR_TO_MAP_VALUE.
6017 */
6018 if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL)
6019 return false;
6020 if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)))
6021 return false;
6022 /* Check our ids match any regs they're supposed to */
6023 return check_ids(rold->id, rcur->id, idmap);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02006024 case PTR_TO_PACKET_META:
Edward Creef1174f72017-08-07 15:26:19 +01006025 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02006026 if (rcur->type != rold->type)
Edward Creef1174f72017-08-07 15:26:19 +01006027 return false;
6028 /* We must have at least as much range as the old ptr
6029 * did, so that any accesses which were safe before are
6030 * still safe. This is true even if old range < old off,
6031 * since someone could have accessed through (ptr - k), or
6032 * even done ptr -= k in a register, to get a safe access.
6033 */
6034 if (rold->range > rcur->range)
6035 return false;
6036 /* If the offsets don't match, we can't trust our alignment;
6037 * nor can we be sure that we won't fall out of range.
6038 */
6039 if (rold->off != rcur->off)
6040 return false;
6041 /* id relations must be preserved */
6042 if (rold->id && !check_ids(rold->id, rcur->id, idmap))
6043 return false;
6044 /* new val must satisfy old val knowledge */
6045 return range_within(rold, rcur) &&
6046 tnum_in(rold->var_off, rcur->var_off);
6047 case PTR_TO_CTX:
6048 case CONST_PTR_TO_MAP:
Edward Creef1174f72017-08-07 15:26:19 +01006049 case PTR_TO_PACKET_END:
Petar Penkovd58e4682018-09-14 07:46:18 -07006050 case PTR_TO_FLOW_KEYS:
Joe Stringerc64b7982018-10-02 13:35:33 -07006051 case PTR_TO_SOCKET:
6052 case PTR_TO_SOCKET_OR_NULL:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08006053 case PTR_TO_SOCK_COMMON:
6054 case PTR_TO_SOCK_COMMON_OR_NULL:
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08006055 case PTR_TO_TCP_SOCK:
6056 case PTR_TO_TCP_SOCK_OR_NULL:
Edward Creef1174f72017-08-07 15:26:19 +01006057 /* Only valid matches are exact, which memcmp() above
6058 * would have accepted
6059 */
6060 default:
6061 /* Don't know what's going on, just say it's not safe */
6062 return false;
6063 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07006064
Edward Creef1174f72017-08-07 15:26:19 +01006065 /* Shouldn't get here; if we do, say it's not safe */
6066 WARN_ON_ONCE(1);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07006067 return false;
6068}
6069
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006070static bool stacksafe(struct bpf_func_state *old,
6071 struct bpf_func_state *cur,
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006072 struct idpair *idmap)
6073{
6074 int i, spi;
6075
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006076 /* walk slots of the explored stack and ignore any additional
6077 * slots in the current stack, since explored(safe) state
6078 * didn't use them
6079 */
6080 for (i = 0; i < old->allocated_stack; i++) {
6081 spi = i / BPF_REG_SIZE;
6082
Alexei Starovoitovb2339202018-12-13 11:42:31 -08006083 if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) {
6084 i += BPF_REG_SIZE - 1;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08006085 /* explored state didn't use this */
Gianluca Borellofd05e572017-12-23 10:09:55 +00006086 continue;
Alexei Starovoitovb2339202018-12-13 11:42:31 -08006087 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08006088
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006089 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID)
6090 continue;
Alexei Starovoitov19e2dbb2018-12-13 11:42:33 -08006091
6092 /* explored stack has more populated slots than current stack
6093 * and these slots were used
6094 */
6095 if (i >= cur->allocated_stack)
6096 return false;
6097
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08006098 /* if old state was safe with misc data in the stack
6099 * it will be safe with zero-initialized stack.
6100 * The opposite is not true
6101 */
6102 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC &&
6103 cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO)
6104 continue;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006105 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] !=
6106 cur->stack[spi].slot_type[i % BPF_REG_SIZE])
6107 /* Ex: old explored (safe) state has STACK_SPILL in
6108 * this stack slot, but current has has STACK_MISC ->
6109 * this verifier states are not equivalent,
6110 * return false to continue verification of this path
6111 */
6112 return false;
6113 if (i % BPF_REG_SIZE)
6114 continue;
6115 if (old->stack[spi].slot_type[0] != STACK_SPILL)
6116 continue;
6117 if (!regsafe(&old->stack[spi].spilled_ptr,
6118 &cur->stack[spi].spilled_ptr,
6119 idmap))
6120 /* when explored and current stack slot are both storing
6121 * spilled registers, check that stored pointers types
6122 * are the same as well.
6123 * Ex: explored safe path could have stored
6124 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8}
6125 * but current path has stored:
6126 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16}
6127 * such verifier states are not equivalent.
6128 * return false to continue verification of this path
6129 */
6130 return false;
6131 }
6132 return true;
6133}
6134
Joe Stringerfd978bf72018-10-02 13:35:35 -07006135static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur)
6136{
6137 if (old->acquired_refs != cur->acquired_refs)
6138 return false;
6139 return !memcmp(old->refs, cur->refs,
6140 sizeof(*old->refs) * old->acquired_refs);
6141}
6142
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006143/* compare two verifier states
6144 *
6145 * all states stored in state_list are known to be valid, since
6146 * verifier reached 'bpf_exit' instruction through them
6147 *
6148 * this function is called when verifier exploring different branches of
6149 * execution popped from the state stack. If it sees an old state that has
6150 * more strict register state and more strict stack state then this execution
6151 * branch doesn't need to be explored further, since verifier already
6152 * concluded that more strict state leads to valid finish.
6153 *
6154 * Therefore two states are equivalent if register state is more conservative
6155 * and explored stack state is more conservative than the current one.
6156 * Example:
6157 * explored current
6158 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
6159 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
6160 *
6161 * In other words if current stack state (one being explored) has more
6162 * valid slots than old one that already passed validation, it means
6163 * the verifier can stop exploring and conclude that current state is valid too
6164 *
6165 * Similarly with registers. If explored state has register type as invalid
6166 * whereas register type in current state is meaningful, it means that
6167 * the current state will reach 'bpf_exit' instruction safely
6168 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006169static bool func_states_equal(struct bpf_func_state *old,
6170 struct bpf_func_state *cur)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006171{
Edward Creef1174f72017-08-07 15:26:19 +01006172 struct idpair *idmap;
6173 bool ret = false;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006174 int i;
6175
Edward Creef1174f72017-08-07 15:26:19 +01006176 idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL);
6177 /* If we failed to allocate the idmap, just say it's not safe */
6178 if (!idmap)
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07006179 return false;
Edward Creef1174f72017-08-07 15:26:19 +01006180
6181 for (i = 0; i < MAX_BPF_REG; i++) {
Edward Cree1b688a12017-08-23 15:10:50 +01006182 if (!regsafe(&old->regs[i], &cur->regs[i], idmap))
Edward Creef1174f72017-08-07 15:26:19 +01006183 goto out_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006184 }
6185
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006186 if (!stacksafe(old, cur, idmap))
6187 goto out_free;
Joe Stringerfd978bf72018-10-02 13:35:35 -07006188
6189 if (!refsafe(old, cur))
6190 goto out_free;
Edward Creef1174f72017-08-07 15:26:19 +01006191 ret = true;
6192out_free:
6193 kfree(idmap);
6194 return ret;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006195}
6196
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006197static bool states_equal(struct bpf_verifier_env *env,
6198 struct bpf_verifier_state *old,
6199 struct bpf_verifier_state *cur)
Edward Creedc503a82017-08-15 20:34:35 +01006200{
Edward Creedc503a82017-08-15 20:34:35 +01006201 int i;
6202
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006203 if (old->curframe != cur->curframe)
6204 return false;
6205
Daniel Borkmann979d63d2019-01-03 00:58:34 +01006206 /* Verification state from speculative execution simulation
6207 * must never prune a non-speculative execution one.
6208 */
6209 if (old->speculative && !cur->speculative)
6210 return false;
6211
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08006212 if (old->active_spin_lock != cur->active_spin_lock)
6213 return false;
6214
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006215 /* for states to be equal callsites have to be the same
6216 * and all frame states need to be equivalent
6217 */
6218 for (i = 0; i <= old->curframe; i++) {
6219 if (old->frame[i]->callsite != cur->frame[i]->callsite)
6220 return false;
6221 if (!func_states_equal(old->frame[i], cur->frame[i]))
6222 return false;
6223 }
6224 return true;
6225}
6226
Jiong Wang55e7f3b2019-04-12 22:59:36 +01006227static int propagate_liveness_reg(struct bpf_verifier_env *env,
6228 struct bpf_reg_state *reg,
6229 struct bpf_reg_state *parent_reg)
6230{
6231 int err;
6232
6233 if (parent_reg->live & REG_LIVE_READ || !(reg->live & REG_LIVE_READ))
6234 return 0;
6235
6236 err = mark_reg_read(env, reg, parent_reg);
6237 if (err)
6238 return err;
6239
6240 return 0;
6241}
6242
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006243/* A write screens off any subsequent reads; but write marks come from the
6244 * straight-line code between a state and its parent. When we arrive at an
6245 * equivalent state (jump target or such) we didn't arrive by the straight-line
6246 * code, so read marks in the state must propagate to the parent regardless
6247 * of the state's write marks. That's what 'parent == state->parent' comparison
Edward Cree679c7822018-08-22 20:02:19 +01006248 * in mark_reg_read() is for.
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006249 */
6250static int propagate_liveness(struct bpf_verifier_env *env,
6251 const struct bpf_verifier_state *vstate,
6252 struct bpf_verifier_state *vparent)
6253{
Jiong Wang3f8cafa2019-04-12 22:59:35 +01006254 struct bpf_reg_state *state_reg, *parent_reg;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006255 struct bpf_func_state *state, *parent;
Jiong Wang3f8cafa2019-04-12 22:59:35 +01006256 int i, frame, err = 0;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006257
6258 if (vparent->curframe != vstate->curframe) {
6259 WARN(1, "propagate_live: parent frame %d current frame %d\n",
6260 vparent->curframe, vstate->curframe);
6261 return -EFAULT;
6262 }
Edward Creedc503a82017-08-15 20:34:35 +01006263 /* Propagate read liveness of registers... */
6264 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
Jakub Kicinski83d16312019-03-21 14:34:36 -07006265 for (frame = 0; frame <= vstate->curframe; frame++) {
Jiong Wang3f8cafa2019-04-12 22:59:35 +01006266 parent = vparent->frame[frame];
6267 state = vstate->frame[frame];
6268 parent_reg = parent->regs;
6269 state_reg = state->regs;
Jakub Kicinski83d16312019-03-21 14:34:36 -07006270 /* We don't need to worry about FP liveness, it's read-only */
6271 for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) {
Jiong Wang55e7f3b2019-04-12 22:59:36 +01006272 err = propagate_liveness_reg(env, &state_reg[i],
6273 &parent_reg[i]);
Jiong Wang3f8cafa2019-04-12 22:59:35 +01006274 if (err)
6275 return err;
Edward Creedc503a82017-08-15 20:34:35 +01006276 }
Edward Creedc503a82017-08-15 20:34:35 +01006277
Jiong Wang1b04aee2019-04-12 22:59:34 +01006278 /* Propagate stack slots. */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006279 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE &&
6280 i < parent->allocated_stack / BPF_REG_SIZE; i++) {
Jiong Wang3f8cafa2019-04-12 22:59:35 +01006281 parent_reg = &parent->stack[i].spilled_ptr;
6282 state_reg = &state->stack[i].spilled_ptr;
Jiong Wang55e7f3b2019-04-12 22:59:36 +01006283 err = propagate_liveness_reg(env, state_reg,
6284 parent_reg);
Jiong Wang3f8cafa2019-04-12 22:59:35 +01006285 if (err)
6286 return err;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006287 }
Edward Creedc503a82017-08-15 20:34:35 +01006288 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006289 return err;
Edward Creedc503a82017-08-15 20:34:35 +01006290}
6291
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006292static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006293{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006294 struct bpf_verifier_state_list *new_sl;
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -07006295 struct bpf_verifier_state_list *sl, **pprev;
Edward Cree679c7822018-08-22 20:02:19 +01006296 struct bpf_verifier_state *cur = env->cur_state, *new;
Alexei Starovoitovceefbc92018-12-03 22:46:06 -08006297 int i, j, err, states_cnt = 0;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006298
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -07006299 pprev = &env->explored_states[insn_idx];
6300 sl = *pprev;
6301
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006302 if (!sl)
6303 /* this 'insn_idx' instruction wasn't marked, so we will not
6304 * be doing state search here
6305 */
6306 return 0;
6307
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -08006308 clean_live_states(env, insn_idx, cur);
6309
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006310 while (sl != STATE_LIST_MARK) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006311 if (states_equal(env, &sl->state, cur)) {
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -07006312 sl->hit_cnt++;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006313 /* reached equivalent register/stack state,
Edward Creedc503a82017-08-15 20:34:35 +01006314 * prune the search.
6315 * Registers read by the continuation are read by us.
Edward Cree8e9cd9c2017-08-23 15:11:21 +01006316 * If we have any write marks in env->cur_state, they
6317 * will prevent corresponding reads in the continuation
6318 * from reaching our parent (an explored_state). Our
6319 * own state will get the read marks recorded, but
6320 * they'll be immediately forgotten as we're pruning
6321 * this state and will pop a new one.
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006322 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006323 err = propagate_liveness(env, &sl->state, cur);
6324 if (err)
6325 return err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006326 return 1;
Edward Creedc503a82017-08-15 20:34:35 +01006327 }
Alexei Starovoitovceefbc92018-12-03 22:46:06 -08006328 states_cnt++;
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -07006329 sl->miss_cnt++;
6330 /* heuristic to determine whether this state is beneficial
6331 * to keep checking from state equivalence point of view.
6332 * Higher numbers increase max_states_per_insn and verification time,
6333 * but do not meaningfully decrease insn_processed.
6334 */
6335 if (sl->miss_cnt > sl->hit_cnt * 3 + 3) {
6336 /* the state is unlikely to be useful. Remove it to
6337 * speed up verification
6338 */
6339 *pprev = sl->next;
6340 if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) {
6341 free_verifier_state(&sl->state, false);
6342 kfree(sl);
6343 env->peak_states--;
6344 } else {
6345 /* cannot free this state, since parentage chain may
6346 * walk it later. Add it for free_list instead to
6347 * be freed at the end of verification
6348 */
6349 sl->next = env->free_list;
6350 env->free_list = sl;
6351 }
6352 sl = *pprev;
6353 continue;
6354 }
6355 pprev = &sl->next;
6356 sl = *pprev;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006357 }
6358
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006359 if (env->max_states_per_insn < states_cnt)
6360 env->max_states_per_insn = states_cnt;
6361
Alexei Starovoitovceefbc92018-12-03 22:46:06 -08006362 if (!env->allow_ptr_leaks && states_cnt > BPF_COMPLEXITY_LIMIT_STATES)
6363 return 0;
6364
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006365 /* there were no equivalent states, remember current one.
6366 * technically the current state is not proven to be safe yet,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006367 * but it will either reach outer most bpf_exit (which means it's safe)
6368 * or it will be rejected. Since there are no loops, we won't be
6369 * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx)
6370 * again on the way to bpf_exit
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006371 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006372 new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006373 if (!new_sl)
6374 return -ENOMEM;
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006375 env->total_states++;
6376 env->peak_states++;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006377
6378 /* add new state to the head of linked list */
Edward Cree679c7822018-08-22 20:02:19 +01006379 new = &new_sl->state;
6380 err = copy_verifier_state(new, cur);
Alexei Starovoitov1969db42017-11-01 00:08:04 -07006381 if (err) {
Edward Cree679c7822018-08-22 20:02:19 +01006382 free_verifier_state(new, false);
Alexei Starovoitov1969db42017-11-01 00:08:04 -07006383 kfree(new_sl);
6384 return err;
6385 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006386 new_sl->next = env->explored_states[insn_idx];
6387 env->explored_states[insn_idx] = new_sl;
Jakub Kicinski7640ead2018-12-12 16:29:07 -08006388 /* connect new state to parentage chain. Current frame needs all
6389 * registers connected. Only r6 - r9 of the callers are alive (pushed
6390 * to the stack implicitly by JITs) so in callers' frames connect just
6391 * r6 - r9 as an optimization. Callers will have r1 - r5 connected to
6392 * the state of the call instruction (with WRITTEN set), and r0 comes
6393 * from callee with its full parentage chain, anyway.
6394 */
6395 for (j = 0; j <= cur->curframe; j++)
6396 for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++)
6397 cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i];
Edward Cree8e9cd9c2017-08-23 15:11:21 +01006398 /* clear write marks in current state: the writes we did are not writes
6399 * our child did, so they don't screen off its reads from us.
6400 * (There are no read marks in current state, because reads always mark
6401 * their parent and current state never has children yet. Only
6402 * explored_states can get read marks.)
6403 */
Edward Creedc503a82017-08-15 20:34:35 +01006404 for (i = 0; i < BPF_REG_FP; i++)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006405 cur->frame[cur->curframe]->regs[i].live = REG_LIVE_NONE;
6406
6407 /* all stack frames are accessible from callee, clear them all */
6408 for (j = 0; j <= cur->curframe; j++) {
6409 struct bpf_func_state *frame = cur->frame[j];
Edward Cree679c7822018-08-22 20:02:19 +01006410 struct bpf_func_state *newframe = new->frame[j];
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006411
Edward Cree679c7822018-08-22 20:02:19 +01006412 for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08006413 frame->stack[i].spilled_ptr.live = REG_LIVE_NONE;
Edward Cree679c7822018-08-22 20:02:19 +01006414 frame->stack[i].spilled_ptr.parent =
6415 &newframe->stack[i].spilled_ptr;
6416 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006417 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006418 return 0;
6419}
6420
Joe Stringerc64b7982018-10-02 13:35:33 -07006421/* Return true if it's OK to have the same insn return a different type. */
6422static bool reg_type_mismatch_ok(enum bpf_reg_type type)
6423{
6424 switch (type) {
6425 case PTR_TO_CTX:
6426 case PTR_TO_SOCKET:
6427 case PTR_TO_SOCKET_OR_NULL:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08006428 case PTR_TO_SOCK_COMMON:
6429 case PTR_TO_SOCK_COMMON_OR_NULL:
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08006430 case PTR_TO_TCP_SOCK:
6431 case PTR_TO_TCP_SOCK_OR_NULL:
Joe Stringerc64b7982018-10-02 13:35:33 -07006432 return false;
6433 default:
6434 return true;
6435 }
6436}
6437
6438/* If an instruction was previously used with particular pointer types, then we
6439 * need to be careful to avoid cases such as the below, where it may be ok
6440 * for one branch accessing the pointer, but not ok for the other branch:
6441 *
6442 * R1 = sock_ptr
6443 * goto X;
6444 * ...
6445 * R1 = some_other_valid_ptr;
6446 * goto X;
6447 * ...
6448 * R2 = *(u32 *)(R1 + 0);
6449 */
6450static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev)
6451{
6452 return src != prev && (!reg_type_mismatch_ok(src) ||
6453 !reg_type_mismatch_ok(prev));
6454}
6455
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006456static int do_check(struct bpf_verifier_env *env)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006457{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006458 struct bpf_verifier_state *state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006459 struct bpf_insn *insns = env->prog->insnsi;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006460 struct bpf_reg_state *regs;
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006461 int insn_cnt = env->prog->len;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006462 bool do_print_state = false;
6463
Martin KaFai Laud9762e82018-12-13 10:41:48 -08006464 env->prev_linfo = NULL;
6465
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006466 state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL);
6467 if (!state)
6468 return -ENOMEM;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006469 state->curframe = 0;
Daniel Borkmann979d63d2019-01-03 00:58:34 +01006470 state->speculative = false;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006471 state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL);
6472 if (!state->frame[0]) {
6473 kfree(state);
6474 return -ENOMEM;
6475 }
6476 env->cur_state = state;
6477 init_func_state(env, state->frame[0],
6478 BPF_MAIN_FUNC /* callsite */,
6479 0 /* frameno */,
6480 0 /* subprogno, zero == main subprog */);
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006481
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006482 for (;;) {
6483 struct bpf_insn *insn;
6484 u8 class;
6485 int err;
6486
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006487 if (env->insn_idx >= insn_cnt) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006488 verbose(env, "invalid insn idx %d insn_cnt %d\n",
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006489 env->insn_idx, insn_cnt);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006490 return -EFAULT;
6491 }
6492
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006493 insn = &insns[env->insn_idx];
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006494 class = BPF_CLASS(insn->code);
6495
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006496 if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006497 verbose(env,
6498 "BPF program is too large. Processed %d insn\n",
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006499 env->insn_processed);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006500 return -E2BIG;
6501 }
6502
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006503 err = is_state_visited(env, env->insn_idx);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006504 if (err < 0)
6505 return err;
6506 if (err == 1) {
6507 /* found equivalent state, can prune the search */
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006508 if (env->log.level & BPF_LOG_LEVEL) {
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006509 if (do_print_state)
Daniel Borkmann979d63d2019-01-03 00:58:34 +01006510 verbose(env, "\nfrom %d to %d%s: safe\n",
6511 env->prev_insn_idx, env->insn_idx,
6512 env->cur_state->speculative ?
6513 " (speculative execution)" : "");
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006514 else
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006515 verbose(env, "%d: safe\n", env->insn_idx);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006516 }
6517 goto process_bpf_exit;
6518 }
6519
Alexei Starovoitovc3494802018-12-03 22:46:04 -08006520 if (signal_pending(current))
6521 return -EAGAIN;
6522
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02006523 if (need_resched())
6524 cond_resched();
6525
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006526 if (env->log.level & BPF_LOG_LEVEL2 ||
6527 (env->log.level & BPF_LOG_LEVEL && do_print_state)) {
6528 if (env->log.level & BPF_LOG_LEVEL2)
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006529 verbose(env, "%d:", env->insn_idx);
David S. Millerc5fc9692017-05-10 11:25:17 -07006530 else
Daniel Borkmann979d63d2019-01-03 00:58:34 +01006531 verbose(env, "\nfrom %d to %d%s:",
6532 env->prev_insn_idx, env->insn_idx,
6533 env->cur_state->speculative ?
6534 " (speculative execution)" : "");
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006535 print_verifier_state(env, state->frame[state->curframe]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006536 do_print_state = false;
6537 }
6538
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006539 if (env->log.level & BPF_LOG_LEVEL) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01006540 const struct bpf_insn_cbs cbs = {
6541 .cb_print = verbose,
Jiri Olsaabe08842018-03-23 11:41:28 +01006542 .private_data = env,
Daniel Borkmann7105e822017-12-20 13:42:57 +01006543 };
6544
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006545 verbose_linfo(env, env->insn_idx, "; ");
6546 verbose(env, "%d: ", env->insn_idx);
Jiri Olsaabe08842018-03-23 11:41:28 +01006547 print_bpf_insn(&cbs, insn, env->allow_ptr_leaks);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006548 }
6549
Jakub Kicinskicae19272017-12-27 18:39:05 -08006550 if (bpf_prog_is_dev_bound(env->prog->aux)) {
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006551 err = bpf_prog_offload_verify_insn(env, env->insn_idx,
6552 env->prev_insn_idx);
Jakub Kicinskicae19272017-12-27 18:39:05 -08006553 if (err)
6554 return err;
6555 }
Jakub Kicinski13a27df2016-09-21 11:43:58 +01006556
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006557 regs = cur_regs(env);
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006558 env->insn_aux_data[env->insn_idx].seen = true;
Joe Stringerfd978bf72018-10-02 13:35:35 -07006559
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006560 if (class == BPF_ALU || class == BPF_ALU64) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07006561 err = check_alu_op(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006562 if (err)
6563 return err;
6564
6565 } else if (class == BPF_LDX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006566 enum bpf_reg_type *prev_src_type, src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006567
6568 /* check for reserved fields is already done */
6569
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006570 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01006571 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006572 if (err)
6573 return err;
6574
Edward Creedc503a82017-08-15 20:34:35 +01006575 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006576 if (err)
6577 return err;
6578
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07006579 src_reg_type = regs[insn->src_reg].type;
6580
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006581 /* check that memory (src_reg + off) is readable,
6582 * the state of dst_reg will be updated by this func
6583 */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006584 err = check_mem_access(env, env->insn_idx, insn->src_reg,
6585 insn->off, BPF_SIZE(insn->code),
6586 BPF_READ, insn->dst_reg, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006587 if (err)
6588 return err;
6589
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006590 prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006591
6592 if (*prev_src_type == NOT_INIT) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006593 /* saw a valid insn
6594 * dst_reg = *(u32 *)(src_reg + off)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006595 * save type to validate intersecting paths
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006596 */
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006597 *prev_src_type = src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006598
Joe Stringerc64b7982018-10-02 13:35:33 -07006599 } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006600 /* ABuser program is trying to use the same insn
6601 * dst_reg = *(u32*) (src_reg + off)
6602 * with different pointer types:
6603 * src_reg == ctx in one branch and
6604 * src_reg == stack|map in some other branch.
6605 * Reject it.
6606 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006607 verbose(env, "same insn cannot be used with different pointers\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006608 return -EINVAL;
6609 }
6610
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006611 } else if (class == BPF_STX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006612 enum bpf_reg_type *prev_dst_type, dst_reg_type;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006613
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006614 if (BPF_MODE(insn->code) == BPF_XADD) {
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006615 err = check_xadd(env, env->insn_idx, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006616 if (err)
6617 return err;
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006618 env->insn_idx++;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006619 continue;
6620 }
6621
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006622 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01006623 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006624 if (err)
6625 return err;
6626 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01006627 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006628 if (err)
6629 return err;
6630
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006631 dst_reg_type = regs[insn->dst_reg].type;
6632
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006633 /* check that memory (dst_reg + off) is writeable */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006634 err = check_mem_access(env, env->insn_idx, insn->dst_reg,
6635 insn->off, BPF_SIZE(insn->code),
6636 BPF_WRITE, insn->src_reg, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006637 if (err)
6638 return err;
6639
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006640 prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006641
6642 if (*prev_dst_type == NOT_INIT) {
6643 *prev_dst_type = dst_reg_type;
Joe Stringerc64b7982018-10-02 13:35:33 -07006644 } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006645 verbose(env, "same insn cannot be used with different pointers\n");
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006646 return -EINVAL;
6647 }
6648
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006649 } else if (class == BPF_ST) {
6650 if (BPF_MODE(insn->code) != BPF_MEM ||
6651 insn->src_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006652 verbose(env, "BPF_ST uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006653 return -EINVAL;
6654 }
6655 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01006656 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006657 if (err)
6658 return err;
6659
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01006660 if (is_ctx_reg(env, insn->dst_reg)) {
Joe Stringer9d2be442018-10-02 13:35:31 -07006661 verbose(env, "BPF_ST stores into R%d %s is not allowed\n",
Daniel Borkmann2a159c62018-10-21 02:09:24 +02006662 insn->dst_reg,
6663 reg_type_str[reg_state(env, insn->dst_reg)->type]);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01006664 return -EACCES;
6665 }
6666
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006667 /* check that memory (dst_reg + off) is writeable */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006668 err = check_mem_access(env, env->insn_idx, insn->dst_reg,
6669 insn->off, BPF_SIZE(insn->code),
6670 BPF_WRITE, -1, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006671 if (err)
6672 return err;
6673
Jiong Wang092ed092019-01-26 12:26:01 -05006674 } else if (class == BPF_JMP || class == BPF_JMP32) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006675 u8 opcode = BPF_OP(insn->code);
6676
6677 if (opcode == BPF_CALL) {
6678 if (BPF_SRC(insn->code) != BPF_K ||
6679 insn->off != 0 ||
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006680 (insn->src_reg != BPF_REG_0 &&
6681 insn->src_reg != BPF_PSEUDO_CALL) ||
Jiong Wang092ed092019-01-26 12:26:01 -05006682 insn->dst_reg != BPF_REG_0 ||
6683 class == BPF_JMP32) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006684 verbose(env, "BPF_CALL uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006685 return -EINVAL;
6686 }
6687
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08006688 if (env->cur_state->active_spin_lock &&
6689 (insn->src_reg == BPF_PSEUDO_CALL ||
6690 insn->imm != BPF_FUNC_spin_unlock)) {
6691 verbose(env, "function calls are not allowed while holding a lock\n");
6692 return -EINVAL;
6693 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006694 if (insn->src_reg == BPF_PSEUDO_CALL)
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006695 err = check_func_call(env, insn, &env->insn_idx);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006696 else
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006697 err = check_helper_call(env, insn->imm, env->insn_idx);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006698 if (err)
6699 return err;
6700
6701 } else if (opcode == BPF_JA) {
6702 if (BPF_SRC(insn->code) != BPF_K ||
6703 insn->imm != 0 ||
6704 insn->src_reg != BPF_REG_0 ||
Jiong Wang092ed092019-01-26 12:26:01 -05006705 insn->dst_reg != BPF_REG_0 ||
6706 class == BPF_JMP32) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006707 verbose(env, "BPF_JA uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006708 return -EINVAL;
6709 }
6710
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006711 env->insn_idx += insn->off + 1;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006712 continue;
6713
6714 } else if (opcode == BPF_EXIT) {
6715 if (BPF_SRC(insn->code) != BPF_K ||
6716 insn->imm != 0 ||
6717 insn->src_reg != BPF_REG_0 ||
Jiong Wang092ed092019-01-26 12:26:01 -05006718 insn->dst_reg != BPF_REG_0 ||
6719 class == BPF_JMP32) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006720 verbose(env, "BPF_EXIT uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006721 return -EINVAL;
6722 }
6723
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08006724 if (env->cur_state->active_spin_lock) {
6725 verbose(env, "bpf_spin_unlock is missing\n");
6726 return -EINVAL;
6727 }
6728
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006729 if (state->curframe) {
6730 /* exit from nested function */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006731 env->prev_insn_idx = env->insn_idx;
6732 err = prepare_func_exit(env, &env->insn_idx);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006733 if (err)
6734 return err;
6735 do_print_state = true;
6736 continue;
6737 }
6738
Joe Stringerfd978bf72018-10-02 13:35:35 -07006739 err = check_reference_leak(env);
6740 if (err)
6741 return err;
6742
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006743 /* eBPF calling convetion is such that R0 is used
6744 * to return the value from eBPF program.
6745 * Make sure that it's readable at this time
6746 * of bpf_exit, which means that program wrote
6747 * something into it earlier
6748 */
Edward Creedc503a82017-08-15 20:34:35 +01006749 err = check_reg_arg(env, BPF_REG_0, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006750 if (err)
6751 return err;
6752
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07006753 if (is_pointer_value(env, BPF_REG_0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006754 verbose(env, "R0 leaks addr as return value\n");
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07006755 return -EACCES;
6756 }
6757
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07006758 err = check_return_code(env);
6759 if (err)
6760 return err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006761process_bpf_exit:
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006762 err = pop_stack(env, &env->prev_insn_idx,
6763 &env->insn_idx);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006764 if (err < 0) {
6765 if (err != -ENOENT)
6766 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006767 break;
6768 } else {
6769 do_print_state = true;
6770 continue;
6771 }
6772 } else {
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006773 err = check_cond_jmp_op(env, insn, &env->insn_idx);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006774 if (err)
6775 return err;
6776 }
6777 } else if (class == BPF_LD) {
6778 u8 mode = BPF_MODE(insn->code);
6779
6780 if (mode == BPF_ABS || mode == BPF_IND) {
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08006781 err = check_ld_abs(env, insn);
6782 if (err)
6783 return err;
6784
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006785 } else if (mode == BPF_IMM) {
6786 err = check_ld_imm(env, insn);
6787 if (err)
6788 return err;
6789
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006790 env->insn_idx++;
6791 env->insn_aux_data[env->insn_idx].seen = true;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006792 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006793 verbose(env, "invalid BPF_LD mode\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006794 return -EINVAL;
6795 }
6796 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006797 verbose(env, "unknown insn class %d\n", class);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006798 return -EINVAL;
6799 }
6800
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006801 env->insn_idx++;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006802 }
6803
Jiong Wang9c8105b2018-05-02 16:17:18 -04006804 env->prog->aux->stack_depth = env->subprog_info[0].stack_depth;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006805 return 0;
6806}
6807
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07006808static int check_map_prealloc(struct bpf_map *map)
6809{
6810 return (map->map_type != BPF_MAP_TYPE_HASH &&
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07006811 map->map_type != BPF_MAP_TYPE_PERCPU_HASH &&
6812 map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) ||
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07006813 !(map->map_flags & BPF_F_NO_PREALLOC);
6814}
6815
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08006816static bool is_tracing_prog_type(enum bpf_prog_type type)
6817{
6818 switch (type) {
6819 case BPF_PROG_TYPE_KPROBE:
6820 case BPF_PROG_TYPE_TRACEPOINT:
6821 case BPF_PROG_TYPE_PERF_EVENT:
6822 case BPF_PROG_TYPE_RAW_TRACEPOINT:
6823 return true;
6824 default:
6825 return false;
6826 }
6827}
6828
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006829static int check_map_prog_compatibility(struct bpf_verifier_env *env,
6830 struct bpf_map *map,
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07006831 struct bpf_prog *prog)
6832
6833{
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07006834 /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use
6835 * preallocated hash maps, since doing memory allocation
6836 * in overflow_handler can crash depending on where nmi got
6837 * triggered.
6838 */
6839 if (prog->type == BPF_PROG_TYPE_PERF_EVENT) {
6840 if (!check_map_prealloc(map)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006841 verbose(env, "perf_event programs can only use preallocated hash map\n");
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07006842 return -EINVAL;
6843 }
6844 if (map->inner_map_meta &&
6845 !check_map_prealloc(map->inner_map_meta)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006846 verbose(env, "perf_event programs can only use preallocated inner hash map\n");
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07006847 return -EINVAL;
6848 }
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07006849 }
Jakub Kicinskia3884572018-01-11 20:29:09 -08006850
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08006851 if ((is_tracing_prog_type(prog->type) ||
6852 prog->type == BPF_PROG_TYPE_SOCKET_FILTER) &&
6853 map_value_has_spin_lock(map)) {
6854 verbose(env, "tracing progs cannot use bpf_spin_lock yet\n");
6855 return -EINVAL;
6856 }
6857
Jakub Kicinskia3884572018-01-11 20:29:09 -08006858 if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) &&
Jakub Kicinski09728262018-07-17 10:53:23 -07006859 !bpf_offload_prog_map_match(prog, map)) {
Jakub Kicinskia3884572018-01-11 20:29:09 -08006860 verbose(env, "offload device mismatch between prog and map\n");
6861 return -EINVAL;
6862 }
6863
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07006864 return 0;
6865}
6866
Roman Gushchinb741f162018-09-28 14:45:43 +00006867static bool bpf_map_is_cgroup_storage(struct bpf_map *map)
6868{
6869 return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE ||
6870 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE);
6871}
6872
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006873/* look for pseudo eBPF instructions that access map FDs and
6874 * replace them with actual map pointers
6875 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006876static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006877{
6878 struct bpf_insn *insn = env->prog->insnsi;
6879 int insn_cnt = env->prog->len;
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07006880 int i, j, err;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006881
Daniel Borkmannf1f77142017-01-13 23:38:15 +01006882 err = bpf_prog_calc_tag(env->prog);
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01006883 if (err)
6884 return err;
6885
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006886 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006887 if (BPF_CLASS(insn->code) == BPF_LDX &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006888 (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006889 verbose(env, "BPF_LDX uses reserved fields\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006890 return -EINVAL;
6891 }
6892
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006893 if (BPF_CLASS(insn->code) == BPF_STX &&
6894 ((BPF_MODE(insn->code) != BPF_MEM &&
6895 BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006896 verbose(env, "BPF_STX uses reserved fields\n");
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006897 return -EINVAL;
6898 }
6899
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006900 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02006901 struct bpf_insn_aux_data *aux;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006902 struct bpf_map *map;
6903 struct fd f;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02006904 u64 addr;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006905
6906 if (i == insn_cnt - 1 || insn[1].code != 0 ||
6907 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
6908 insn[1].off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006909 verbose(env, "invalid bpf_ld_imm64 insn\n");
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006910 return -EINVAL;
6911 }
6912
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02006913 if (insn[0].src_reg == 0)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006914 /* valid generic load 64-bit imm */
6915 goto next_insn;
6916
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02006917 /* In final convert_pseudo_ld_imm64() step, this is
6918 * converted into regular 64-bit imm load insn.
6919 */
6920 if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD &&
6921 insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) ||
6922 (insn[0].src_reg == BPF_PSEUDO_MAP_FD &&
6923 insn[1].imm != 0)) {
6924 verbose(env,
6925 "unrecognized bpf_ld_imm64 insn\n");
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006926 return -EINVAL;
6927 }
6928
Daniel Borkmann20182392019-03-04 21:08:53 +01006929 f = fdget(insn[0].imm);
Daniel Borkmannc2101292015-10-29 14:58:07 +01006930 map = __bpf_map_get(f);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006931 if (IS_ERR(map)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006932 verbose(env, "fd %d is not pointing to valid bpf_map\n",
Daniel Borkmann20182392019-03-04 21:08:53 +01006933 insn[0].imm);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006934 return PTR_ERR(map);
6935 }
6936
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006937 err = check_map_prog_compatibility(env, map, env->prog);
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07006938 if (err) {
6939 fdput(f);
6940 return err;
6941 }
6942
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02006943 aux = &env->insn_aux_data[i];
6944 if (insn->src_reg == BPF_PSEUDO_MAP_FD) {
6945 addr = (unsigned long)map;
6946 } else {
6947 u32 off = insn[1].imm;
6948
6949 if (off >= BPF_MAX_VAR_OFF) {
6950 verbose(env, "direct value offset of %u is not allowed\n", off);
6951 fdput(f);
6952 return -EINVAL;
6953 }
6954
6955 if (!map->ops->map_direct_value_addr) {
6956 verbose(env, "no direct value access support for this map type\n");
6957 fdput(f);
6958 return -EINVAL;
6959 }
6960
6961 err = map->ops->map_direct_value_addr(map, &addr, off);
6962 if (err) {
6963 verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n",
6964 map->value_size, off);
6965 fdput(f);
6966 return err;
6967 }
6968
6969 aux->map_off = off;
6970 addr += off;
6971 }
6972
6973 insn[0].imm = (u32)addr;
6974 insn[1].imm = addr >> 32;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006975
6976 /* check whether we recorded this map already */
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02006977 for (j = 0; j < env->used_map_cnt; j++) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006978 if (env->used_maps[j] == map) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02006979 aux->map_index = j;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006980 fdput(f);
6981 goto next_insn;
6982 }
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02006983 }
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006984
6985 if (env->used_map_cnt >= MAX_USED_MAPS) {
6986 fdput(f);
6987 return -E2BIG;
6988 }
6989
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006990 /* hold the map. If the program is rejected by verifier,
6991 * the map will be released by release_maps() or it
6992 * will be used by the valid program until it's unloaded
Jakub Kicinskiab7f5bf2018-05-03 18:37:17 -07006993 * and all maps are released in free_used_maps()
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006994 */
Alexei Starovoitov92117d82016-04-27 18:56:20 -07006995 map = bpf_map_inc(map, false);
6996 if (IS_ERR(map)) {
6997 fdput(f);
6998 return PTR_ERR(map);
6999 }
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02007000
7001 aux->map_index = env->used_map_cnt;
Alexei Starovoitov92117d82016-04-27 18:56:20 -07007002 env->used_maps[env->used_map_cnt++] = map;
7003
Roman Gushchinb741f162018-09-28 14:45:43 +00007004 if (bpf_map_is_cgroup_storage(map) &&
Roman Gushchinde9cbba2018-08-02 14:27:18 -07007005 bpf_cgroup_storage_assign(env->prog, map)) {
Roman Gushchinb741f162018-09-28 14:45:43 +00007006 verbose(env, "only one cgroup storage of each type is allowed\n");
Roman Gushchinde9cbba2018-08-02 14:27:18 -07007007 fdput(f);
7008 return -EBUSY;
7009 }
7010
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007011 fdput(f);
7012next_insn:
7013 insn++;
7014 i++;
Daniel Borkmann5e581da2018-01-26 23:33:38 +01007015 continue;
7016 }
7017
7018 /* Basic sanity check before we invest more work here. */
7019 if (!bpf_opcode_in_insntable(insn->code)) {
7020 verbose(env, "unknown opcode %02x\n", insn->code);
7021 return -EINVAL;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007022 }
7023 }
7024
7025 /* now all pseudo BPF_LD_IMM64 instructions load valid
7026 * 'struct bpf_map *' into a register instead of user map_fd.
7027 * These pointers will be used later by verifier to validate map access.
7028 */
7029 return 0;
7030}
7031
7032/* drop refcnt of maps used by the rejected program */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007033static void release_maps(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007034{
Roman Gushchin8bad74f2018-09-28 14:45:36 +00007035 enum bpf_cgroup_storage_type stype;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007036 int i;
7037
Roman Gushchin8bad74f2018-09-28 14:45:36 +00007038 for_each_cgroup_storage_type(stype) {
7039 if (!env->prog->aux->cgroup_storage[stype])
7040 continue;
Roman Gushchinde9cbba2018-08-02 14:27:18 -07007041 bpf_cgroup_storage_release(env->prog,
Roman Gushchin8bad74f2018-09-28 14:45:36 +00007042 env->prog->aux->cgroup_storage[stype]);
7043 }
Roman Gushchinde9cbba2018-08-02 14:27:18 -07007044
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007045 for (i = 0; i < env->used_map_cnt; i++)
7046 bpf_map_put(env->used_maps[i]);
7047}
7048
7049/* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007050static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007051{
7052 struct bpf_insn *insn = env->prog->insnsi;
7053 int insn_cnt = env->prog->len;
7054 int i;
7055
7056 for (i = 0; i < insn_cnt; i++, insn++)
7057 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
7058 insn->src_reg = 0;
7059}
7060
Alexei Starovoitov80419022017-03-15 18:26:41 -07007061/* single env->prog->insni[off] instruction was replaced with the range
7062 * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
7063 * [0, off) and [off, end) to new locations, so the patched range stays zero
7064 */
7065static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len,
7066 u32 off, u32 cnt)
7067{
7068 struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data;
Alexei Starovoitovc1311872017-11-22 16:42:05 -08007069 int i;
Alexei Starovoitov80419022017-03-15 18:26:41 -07007070
7071 if (cnt == 1)
7072 return 0;
Kees Cookfad953c2018-06-12 14:27:37 -07007073 new_data = vzalloc(array_size(prog_len,
7074 sizeof(struct bpf_insn_aux_data)));
Alexei Starovoitov80419022017-03-15 18:26:41 -07007075 if (!new_data)
7076 return -ENOMEM;
7077 memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off);
7078 memcpy(new_data + off + cnt - 1, old_data + off,
7079 sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
Alexei Starovoitovc1311872017-11-22 16:42:05 -08007080 for (i = off; i < off + cnt - 1; i++)
7081 new_data[i].seen = true;
Alexei Starovoitov80419022017-03-15 18:26:41 -07007082 env->insn_aux_data = new_data;
7083 vfree(old_data);
7084 return 0;
7085}
7086
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08007087static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len)
7088{
7089 int i;
7090
7091 if (len == 1)
7092 return;
Jiong Wang4cb3d992018-05-02 16:17:19 -04007093 /* NOTE: fake 'exit' subprog should be updated as well. */
7094 for (i = 0; i <= env->subprog_cnt; i++) {
Edward Creeafd59422018-11-16 12:00:07 +00007095 if (env->subprog_info[i].start <= off)
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08007096 continue;
Jiong Wang9c8105b2018-05-02 16:17:18 -04007097 env->subprog_info[i].start += len - 1;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08007098 }
7099}
7100
Alexei Starovoitov80419022017-03-15 18:26:41 -07007101static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
7102 const struct bpf_insn *patch, u32 len)
7103{
7104 struct bpf_prog *new_prog;
7105
7106 new_prog = bpf_patch_insn_single(env->prog, off, patch, len);
Alexei Starovoitov4f733792019-04-01 21:27:44 -07007107 if (IS_ERR(new_prog)) {
7108 if (PTR_ERR(new_prog) == -ERANGE)
7109 verbose(env,
7110 "insn %d cannot be patched due to 16-bit range\n",
7111 env->insn_aux_data[off].orig_idx);
Alexei Starovoitov80419022017-03-15 18:26:41 -07007112 return NULL;
Alexei Starovoitov4f733792019-04-01 21:27:44 -07007113 }
Alexei Starovoitov80419022017-03-15 18:26:41 -07007114 if (adjust_insn_aux_data(env, new_prog->len, off, len))
7115 return NULL;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08007116 adjust_subprog_starts(env, off, len);
Alexei Starovoitov80419022017-03-15 18:26:41 -07007117 return new_prog;
7118}
7119
Jakub Kicinski52875a02019-01-22 22:45:20 -08007120static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env,
7121 u32 off, u32 cnt)
7122{
7123 int i, j;
7124
7125 /* find first prog starting at or after off (first to remove) */
7126 for (i = 0; i < env->subprog_cnt; i++)
7127 if (env->subprog_info[i].start >= off)
7128 break;
7129 /* find first prog starting at or after off + cnt (first to stay) */
7130 for (j = i; j < env->subprog_cnt; j++)
7131 if (env->subprog_info[j].start >= off + cnt)
7132 break;
7133 /* if j doesn't start exactly at off + cnt, we are just removing
7134 * the front of previous prog
7135 */
7136 if (env->subprog_info[j].start != off + cnt)
7137 j--;
7138
7139 if (j > i) {
7140 struct bpf_prog_aux *aux = env->prog->aux;
7141 int move;
7142
7143 /* move fake 'exit' subprog as well */
7144 move = env->subprog_cnt + 1 - j;
7145
7146 memmove(env->subprog_info + i,
7147 env->subprog_info + j,
7148 sizeof(*env->subprog_info) * move);
7149 env->subprog_cnt -= j - i;
7150
7151 /* remove func_info */
7152 if (aux->func_info) {
7153 move = aux->func_info_cnt - j;
7154
7155 memmove(aux->func_info + i,
7156 aux->func_info + j,
7157 sizeof(*aux->func_info) * move);
7158 aux->func_info_cnt -= j - i;
7159 /* func_info->insn_off is set after all code rewrites,
7160 * in adjust_btf_func() - no need to adjust
7161 */
7162 }
7163 } else {
7164 /* convert i from "first prog to remove" to "first to adjust" */
7165 if (env->subprog_info[i].start == off)
7166 i++;
7167 }
7168
7169 /* update fake 'exit' subprog as well */
7170 for (; i <= env->subprog_cnt; i++)
7171 env->subprog_info[i].start -= cnt;
7172
7173 return 0;
7174}
7175
7176static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off,
7177 u32 cnt)
7178{
7179 struct bpf_prog *prog = env->prog;
7180 u32 i, l_off, l_cnt, nr_linfo;
7181 struct bpf_line_info *linfo;
7182
7183 nr_linfo = prog->aux->nr_linfo;
7184 if (!nr_linfo)
7185 return 0;
7186
7187 linfo = prog->aux->linfo;
7188
7189 /* find first line info to remove, count lines to be removed */
7190 for (i = 0; i < nr_linfo; i++)
7191 if (linfo[i].insn_off >= off)
7192 break;
7193
7194 l_off = i;
7195 l_cnt = 0;
7196 for (; i < nr_linfo; i++)
7197 if (linfo[i].insn_off < off + cnt)
7198 l_cnt++;
7199 else
7200 break;
7201
7202 /* First live insn doesn't match first live linfo, it needs to "inherit"
7203 * last removed linfo. prog is already modified, so prog->len == off
7204 * means no live instructions after (tail of the program was removed).
7205 */
7206 if (prog->len != off && l_cnt &&
7207 (i == nr_linfo || linfo[i].insn_off != off + cnt)) {
7208 l_cnt--;
7209 linfo[--i].insn_off = off + cnt;
7210 }
7211
7212 /* remove the line info which refer to the removed instructions */
7213 if (l_cnt) {
7214 memmove(linfo + l_off, linfo + i,
7215 sizeof(*linfo) * (nr_linfo - i));
7216
7217 prog->aux->nr_linfo -= l_cnt;
7218 nr_linfo = prog->aux->nr_linfo;
7219 }
7220
7221 /* pull all linfo[i].insn_off >= off + cnt in by cnt */
7222 for (i = l_off; i < nr_linfo; i++)
7223 linfo[i].insn_off -= cnt;
7224
7225 /* fix up all subprogs (incl. 'exit') which start >= off */
7226 for (i = 0; i <= env->subprog_cnt; i++)
7227 if (env->subprog_info[i].linfo_idx > l_off) {
7228 /* program may have started in the removed region but
7229 * may not be fully removed
7230 */
7231 if (env->subprog_info[i].linfo_idx >= l_off + l_cnt)
7232 env->subprog_info[i].linfo_idx -= l_cnt;
7233 else
7234 env->subprog_info[i].linfo_idx = l_off;
7235 }
7236
7237 return 0;
7238}
7239
7240static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)
7241{
7242 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
7243 unsigned int orig_prog_len = env->prog->len;
7244 int err;
7245
Jakub Kicinski08ca90a2019-01-22 22:45:24 -08007246 if (bpf_prog_is_dev_bound(env->prog->aux))
7247 bpf_prog_offload_remove_insns(env, off, cnt);
7248
Jakub Kicinski52875a02019-01-22 22:45:20 -08007249 err = bpf_remove_insns(env->prog, off, cnt);
7250 if (err)
7251 return err;
7252
7253 err = adjust_subprog_starts_after_remove(env, off, cnt);
7254 if (err)
7255 return err;
7256
7257 err = bpf_adj_linfo_after_remove(env, off, cnt);
7258 if (err)
7259 return err;
7260
7261 memmove(aux_data + off, aux_data + off + cnt,
7262 sizeof(*aux_data) * (orig_prog_len - off - cnt));
7263
7264 return 0;
7265}
7266
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01007267/* The verifier does more data flow analysis than llvm and will not
7268 * explore branches that are dead at run time. Malicious programs can
7269 * have dead code too. Therefore replace all dead at-run-time code
7270 * with 'ja -1'.
7271 *
7272 * Just nops are not optimal, e.g. if they would sit at the end of the
7273 * program and through another bug we would manage to jump there, then
7274 * we'd execute beyond program memory otherwise. Returning exception
7275 * code also wouldn't work since we can have subprogs where the dead
7276 * code could be located.
Alexei Starovoitovc1311872017-11-22 16:42:05 -08007277 */
7278static void sanitize_dead_code(struct bpf_verifier_env *env)
7279{
7280 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01007281 struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1);
Alexei Starovoitovc1311872017-11-22 16:42:05 -08007282 struct bpf_insn *insn = env->prog->insnsi;
7283 const int insn_cnt = env->prog->len;
7284 int i;
7285
7286 for (i = 0; i < insn_cnt; i++) {
7287 if (aux_data[i].seen)
7288 continue;
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01007289 memcpy(insn + i, &trap, sizeof(trap));
Alexei Starovoitovc1311872017-11-22 16:42:05 -08007290 }
7291}
7292
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08007293static bool insn_is_cond_jump(u8 code)
7294{
7295 u8 op;
7296
Jiong Wang092ed092019-01-26 12:26:01 -05007297 if (BPF_CLASS(code) == BPF_JMP32)
7298 return true;
7299
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08007300 if (BPF_CLASS(code) != BPF_JMP)
7301 return false;
7302
7303 op = BPF_OP(code);
7304 return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL;
7305}
7306
7307static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env)
7308{
7309 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
7310 struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0);
7311 struct bpf_insn *insn = env->prog->insnsi;
7312 const int insn_cnt = env->prog->len;
7313 int i;
7314
7315 for (i = 0; i < insn_cnt; i++, insn++) {
7316 if (!insn_is_cond_jump(insn->code))
7317 continue;
7318
7319 if (!aux_data[i + 1].seen)
7320 ja.off = insn->off;
7321 else if (!aux_data[i + 1 + insn->off].seen)
7322 ja.off = 0;
7323 else
7324 continue;
7325
Jakub Kicinski08ca90a2019-01-22 22:45:24 -08007326 if (bpf_prog_is_dev_bound(env->prog->aux))
7327 bpf_prog_offload_replace_insn(env, i, &ja);
7328
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08007329 memcpy(insn, &ja, sizeof(ja));
7330 }
7331}
7332
Jakub Kicinski52875a02019-01-22 22:45:20 -08007333static int opt_remove_dead_code(struct bpf_verifier_env *env)
7334{
7335 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
7336 int insn_cnt = env->prog->len;
7337 int i, err;
7338
7339 for (i = 0; i < insn_cnt; i++) {
7340 int j;
7341
7342 j = 0;
7343 while (i + j < insn_cnt && !aux_data[i + j].seen)
7344 j++;
7345 if (!j)
7346 continue;
7347
7348 err = verifier_remove_insns(env, i, j);
7349 if (err)
7350 return err;
7351 insn_cnt = env->prog->len;
7352 }
7353
7354 return 0;
7355}
7356
Jakub Kicinskia1b14ab2019-01-22 22:45:21 -08007357static int opt_remove_nops(struct bpf_verifier_env *env)
7358{
7359 const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0);
7360 struct bpf_insn *insn = env->prog->insnsi;
7361 int insn_cnt = env->prog->len;
7362 int i, err;
7363
7364 for (i = 0; i < insn_cnt; i++) {
7365 if (memcmp(&insn[i], &ja, sizeof(ja)))
7366 continue;
7367
7368 err = verifier_remove_insns(env, i, 1);
7369 if (err)
7370 return err;
7371 insn_cnt--;
7372 i--;
7373 }
7374
7375 return 0;
7376}
7377
Joe Stringerc64b7982018-10-02 13:35:33 -07007378/* convert load instructions that access fields of a context type into a
7379 * sequence of instructions that access fields of the underlying structure:
7380 * struct __sk_buff -> struct sk_buff
7381 * struct bpf_sock_ops -> struct sock
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007382 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007383static int convert_ctx_accesses(struct bpf_verifier_env *env)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007384{
Jakub Kicinski00176a32017-10-16 16:40:54 -07007385 const struct bpf_verifier_ops *ops = env->ops;
Daniel Borkmannf96da092017-07-02 02:13:27 +02007386 int i, cnt, size, ctx_field_size, delta = 0;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007387 const int insn_cnt = env->prog->len;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007388 struct bpf_insn insn_buf[16], *insn;
Andrey Ignatov46f53a62018-11-10 22:15:13 -08007389 u32 target_size, size_default, off;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007390 struct bpf_prog *new_prog;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007391 enum bpf_access_type type;
Daniel Borkmannf96da092017-07-02 02:13:27 +02007392 bool is_narrower_load;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007393
Daniel Borkmannb09928b2018-10-24 22:05:49 +02007394 if (ops->gen_prologue || env->seen_direct_write) {
7395 if (!ops->gen_prologue) {
7396 verbose(env, "bpf verifier is misconfigured\n");
7397 return -EINVAL;
7398 }
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007399 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
7400 env->prog);
7401 if (cnt >= ARRAY_SIZE(insn_buf)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007402 verbose(env, "bpf verifier is misconfigured\n");
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007403 return -EINVAL;
7404 } else if (cnt) {
Alexei Starovoitov80419022017-03-15 18:26:41 -07007405 new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007406 if (!new_prog)
7407 return -ENOMEM;
Alexei Starovoitov80419022017-03-15 18:26:41 -07007408
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007409 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007410 delta += cnt - 1;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007411 }
7412 }
7413
Joe Stringerc64b7982018-10-02 13:35:33 -07007414 if (bpf_prog_is_dev_bound(env->prog->aux))
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007415 return 0;
7416
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007417 insn = env->prog->insnsi + delta;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007418
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007419 for (i = 0; i < insn_cnt; i++, insn++) {
Joe Stringerc64b7982018-10-02 13:35:33 -07007420 bpf_convert_ctx_access_t convert_ctx_access;
7421
Daniel Borkmann62c79892017-01-12 11:51:33 +01007422 if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) ||
7423 insn->code == (BPF_LDX | BPF_MEM | BPF_H) ||
7424 insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07007425 insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007426 type = BPF_READ;
Daniel Borkmann62c79892017-01-12 11:51:33 +01007427 else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) ||
7428 insn->code == (BPF_STX | BPF_MEM | BPF_H) ||
7429 insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07007430 insn->code == (BPF_STX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007431 type = BPF_WRITE;
7432 else
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007433 continue;
7434
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07007435 if (type == BPF_WRITE &&
7436 env->insn_aux_data[i + delta].sanitize_stack_off) {
7437 struct bpf_insn patch[] = {
7438 /* Sanitize suspicious stack slot with zero.
7439 * There are no memory dependencies for this store,
7440 * since it's only using frame pointer and immediate
7441 * constant of zero
7442 */
7443 BPF_ST_MEM(BPF_DW, BPF_REG_FP,
7444 env->insn_aux_data[i + delta].sanitize_stack_off,
7445 0),
7446 /* the original STX instruction will immediately
7447 * overwrite the same stack slot with appropriate value
7448 */
7449 *insn,
7450 };
7451
7452 cnt = ARRAY_SIZE(patch);
7453 new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt);
7454 if (!new_prog)
7455 return -ENOMEM;
7456
7457 delta += cnt - 1;
7458 env->prog = new_prog;
7459 insn = new_prog->insnsi + i + delta;
7460 continue;
7461 }
7462
Joe Stringerc64b7982018-10-02 13:35:33 -07007463 switch (env->insn_aux_data[i + delta].ptr_type) {
7464 case PTR_TO_CTX:
7465 if (!ops->convert_ctx_access)
7466 continue;
7467 convert_ctx_access = ops->convert_ctx_access;
7468 break;
7469 case PTR_TO_SOCKET:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08007470 case PTR_TO_SOCK_COMMON:
Joe Stringerc64b7982018-10-02 13:35:33 -07007471 convert_ctx_access = bpf_sock_convert_ctx_access;
7472 break;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08007473 case PTR_TO_TCP_SOCK:
7474 convert_ctx_access = bpf_tcp_sock_convert_ctx_access;
7475 break;
Joe Stringerc64b7982018-10-02 13:35:33 -07007476 default:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007477 continue;
Joe Stringerc64b7982018-10-02 13:35:33 -07007478 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007479
Yonghong Song31fd8582017-06-13 15:52:13 -07007480 ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size;
Daniel Borkmannf96da092017-07-02 02:13:27 +02007481 size = BPF_LDST_BYTES(insn);
Yonghong Song31fd8582017-06-13 15:52:13 -07007482
7483 /* If the read access is a narrower load of the field,
7484 * convert to a 4/8-byte load, to minimum program type specific
7485 * convert_ctx_access changes. If conversion is successful,
7486 * we will apply proper mask to the result.
7487 */
Daniel Borkmannf96da092017-07-02 02:13:27 +02007488 is_narrower_load = size < ctx_field_size;
Andrey Ignatov46f53a62018-11-10 22:15:13 -08007489 size_default = bpf_ctx_off_adjust_machine(ctx_field_size);
7490 off = insn->off;
Yonghong Song31fd8582017-06-13 15:52:13 -07007491 if (is_narrower_load) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02007492 u8 size_code;
Yonghong Song31fd8582017-06-13 15:52:13 -07007493
Daniel Borkmannf96da092017-07-02 02:13:27 +02007494 if (type == BPF_WRITE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007495 verbose(env, "bpf verifier narrow ctx access misconfigured\n");
Daniel Borkmannf96da092017-07-02 02:13:27 +02007496 return -EINVAL;
7497 }
7498
7499 size_code = BPF_H;
Yonghong Song31fd8582017-06-13 15:52:13 -07007500 if (ctx_field_size == 4)
7501 size_code = BPF_W;
7502 else if (ctx_field_size == 8)
7503 size_code = BPF_DW;
Daniel Borkmannf96da092017-07-02 02:13:27 +02007504
Daniel Borkmannbc231052018-06-02 23:06:39 +02007505 insn->off = off & ~(size_default - 1);
Yonghong Song31fd8582017-06-13 15:52:13 -07007506 insn->code = BPF_LDX | BPF_MEM | size_code;
7507 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02007508
7509 target_size = 0;
Joe Stringerc64b7982018-10-02 13:35:33 -07007510 cnt = convert_ctx_access(type, insn, insn_buf, env->prog,
7511 &target_size);
Daniel Borkmannf96da092017-07-02 02:13:27 +02007512 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) ||
7513 (ctx_field_size && !target_size)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007514 verbose(env, "bpf verifier is misconfigured\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007515 return -EINVAL;
7516 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02007517
7518 if (is_narrower_load && size < target_size) {
Andrey Ignatov46f53a62018-11-10 22:15:13 -08007519 u8 shift = (off & (size_default - 1)) * 8;
7520
7521 if (ctx_field_size <= 4) {
7522 if (shift)
7523 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH,
7524 insn->dst_reg,
7525 shift);
Yonghong Song31fd8582017-06-13 15:52:13 -07007526 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02007527 (1 << size * 8) - 1);
Andrey Ignatov46f53a62018-11-10 22:15:13 -08007528 } else {
7529 if (shift)
7530 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH,
7531 insn->dst_reg,
7532 shift);
Yonghong Song31fd8582017-06-13 15:52:13 -07007533 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02007534 (1 << size * 8) - 1);
Andrey Ignatov46f53a62018-11-10 22:15:13 -08007535 }
Yonghong Song31fd8582017-06-13 15:52:13 -07007536 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007537
Alexei Starovoitov80419022017-03-15 18:26:41 -07007538 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007539 if (!new_prog)
7540 return -ENOMEM;
7541
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007542 delta += cnt - 1;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007543
7544 /* keep walking new program and skip insns we just inserted */
7545 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007546 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007547 }
7548
7549 return 0;
7550}
7551
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007552static int jit_subprogs(struct bpf_verifier_env *env)
7553{
7554 struct bpf_prog *prog = env->prog, **func, *tmp;
7555 int i, j, subprog_start, subprog_end = 0, len, subprog;
Daniel Borkmann7105e822017-12-20 13:42:57 +01007556 struct bpf_insn *insn;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007557 void *old_bpf_func;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08007558 int err;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007559
Jiong Wangf910cef2018-05-02 16:17:17 -04007560 if (env->subprog_cnt <= 1)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007561 return 0;
7562
Daniel Borkmann7105e822017-12-20 13:42:57 +01007563 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007564 if (insn->code != (BPF_JMP | BPF_CALL) ||
7565 insn->src_reg != BPF_PSEUDO_CALL)
7566 continue;
Daniel Borkmannc7a89782018-07-12 21:44:28 +02007567 /* Upon error here we cannot fall back to interpreter but
7568 * need a hard reject of the program. Thus -EFAULT is
7569 * propagated in any case.
7570 */
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007571 subprog = find_subprog(env, i + insn->imm + 1);
7572 if (subprog < 0) {
7573 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
7574 i + insn->imm + 1);
7575 return -EFAULT;
7576 }
7577 /* temporarily remember subprog id inside insn instead of
7578 * aux_data, since next loop will split up all insns into funcs
7579 */
Jiong Wangf910cef2018-05-02 16:17:17 -04007580 insn->off = subprog;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007581 /* remember original imm in case JIT fails and fallback
7582 * to interpreter will be needed
7583 */
7584 env->insn_aux_data[i].call_imm = insn->imm;
7585 /* point imm to __bpf_call_base+1 from JITs point of view */
7586 insn->imm = 1;
7587 }
7588
Martin KaFai Lauc454a462018-12-07 16:42:25 -08007589 err = bpf_prog_alloc_jited_linfo(prog);
7590 if (err)
7591 goto out_undo_insn;
7592
7593 err = -ENOMEM;
Kees Cook6396bb22018-06-12 14:03:40 -07007594 func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007595 if (!func)
Daniel Borkmannc7a89782018-07-12 21:44:28 +02007596 goto out_undo_insn;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007597
Jiong Wangf910cef2018-05-02 16:17:17 -04007598 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007599 subprog_start = subprog_end;
Jiong Wang4cb3d992018-05-02 16:17:19 -04007600 subprog_end = env->subprog_info[i + 1].start;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007601
7602 len = subprog_end - subprog_start;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08007603 /* BPF_PROG_RUN doesn't call subprogs directly,
7604 * hence main prog stats include the runtime of subprogs.
7605 * subprogs don't have IDs and not reachable via prog_get_next_id
7606 * func[i]->aux->stats will never be accessed and stays NULL
7607 */
7608 func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007609 if (!func[i])
7610 goto out_free;
7611 memcpy(func[i]->insnsi, &prog->insnsi[subprog_start],
7612 len * sizeof(struct bpf_insn));
Daniel Borkmann4f74d802017-12-20 13:42:56 +01007613 func[i]->type = prog->type;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007614 func[i]->len = len;
Daniel Borkmann4f74d802017-12-20 13:42:56 +01007615 if (bpf_prog_calc_tag(func[i]))
7616 goto out_free;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007617 func[i]->is_func = 1;
Yonghong Songba64e7d2018-11-24 23:20:44 -08007618 func[i]->aux->func_idx = i;
7619 /* the btf and func_info will be freed only at prog->aux */
7620 func[i]->aux->btf = prog->aux->btf;
7621 func[i]->aux->func_info = prog->aux->func_info;
7622
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007623 /* Use bpf_prog_F_tag to indicate functions in stack traces.
7624 * Long term would need debug info to populate names
7625 */
7626 func[i]->aux->name[0] = 'F';
Jiong Wang9c8105b2018-05-02 16:17:18 -04007627 func[i]->aux->stack_depth = env->subprog_info[i].stack_depth;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007628 func[i]->jit_requested = 1;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08007629 func[i]->aux->linfo = prog->aux->linfo;
7630 func[i]->aux->nr_linfo = prog->aux->nr_linfo;
7631 func[i]->aux->jited_linfo = prog->aux->jited_linfo;
7632 func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007633 func[i] = bpf_int_jit_compile(func[i]);
7634 if (!func[i]->jited) {
7635 err = -ENOTSUPP;
7636 goto out_free;
7637 }
7638 cond_resched();
7639 }
7640 /* at this point all bpf functions were successfully JITed
7641 * now populate all bpf_calls with correct addresses and
7642 * run last pass of JIT
7643 */
Jiong Wangf910cef2018-05-02 16:17:17 -04007644 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007645 insn = func[i]->insnsi;
7646 for (j = 0; j < func[i]->len; j++, insn++) {
7647 if (insn->code != (BPF_JMP | BPF_CALL) ||
7648 insn->src_reg != BPF_PSEUDO_CALL)
7649 continue;
7650 subprog = insn->off;
Prashant Bhole0d306c32019-04-16 18:13:01 +09007651 insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) -
7652 __bpf_call_base;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007653 }
Sandipan Das2162fed2018-05-24 12:26:45 +05307654
7655 /* we use the aux data to keep a list of the start addresses
7656 * of the JITed images for each function in the program
7657 *
7658 * for some architectures, such as powerpc64, the imm field
7659 * might not be large enough to hold the offset of the start
7660 * address of the callee's JITed image from __bpf_call_base
7661 *
7662 * in such cases, we can lookup the start address of a callee
7663 * by using its subprog id, available from the off field of
7664 * the call instruction, as an index for this list
7665 */
7666 func[i]->aux->func = func;
7667 func[i]->aux->func_cnt = env->subprog_cnt;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007668 }
Jiong Wangf910cef2018-05-02 16:17:17 -04007669 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007670 old_bpf_func = func[i]->bpf_func;
7671 tmp = bpf_int_jit_compile(func[i]);
7672 if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) {
7673 verbose(env, "JIT doesn't support bpf-to-bpf calls\n");
Daniel Borkmannc7a89782018-07-12 21:44:28 +02007674 err = -ENOTSUPP;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007675 goto out_free;
7676 }
7677 cond_resched();
7678 }
7679
7680 /* finally lock prog and jit images for all functions and
7681 * populate kallsysm
7682 */
Jiong Wangf910cef2018-05-02 16:17:17 -04007683 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007684 bpf_prog_lock_ro(func[i]);
7685 bpf_prog_kallsyms_add(func[i]);
7686 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01007687
7688 /* Last step: make now unused interpreter insns from main
7689 * prog consistent for later dump requests, so they can
7690 * later look the same as if they were interpreted only.
7691 */
7692 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01007693 if (insn->code != (BPF_JMP | BPF_CALL) ||
7694 insn->src_reg != BPF_PSEUDO_CALL)
7695 continue;
7696 insn->off = env->insn_aux_data[i].call_imm;
7697 subprog = find_subprog(env, i + insn->off + 1);
Sandipan Dasdbecd732018-05-24 12:26:48 +05307698 insn->imm = subprog;
Daniel Borkmann7105e822017-12-20 13:42:57 +01007699 }
7700
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007701 prog->jited = 1;
7702 prog->bpf_func = func[0]->bpf_func;
7703 prog->aux->func = func;
Jiong Wangf910cef2018-05-02 16:17:17 -04007704 prog->aux->func_cnt = env->subprog_cnt;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08007705 bpf_prog_free_unused_jited_linfo(prog);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007706 return 0;
7707out_free:
Jiong Wangf910cef2018-05-02 16:17:17 -04007708 for (i = 0; i < env->subprog_cnt; i++)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007709 if (func[i])
7710 bpf_jit_free(func[i]);
7711 kfree(func);
Daniel Borkmannc7a89782018-07-12 21:44:28 +02007712out_undo_insn:
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007713 /* cleanup main prog to be interpreted */
7714 prog->jit_requested = 0;
7715 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
7716 if (insn->code != (BPF_JMP | BPF_CALL) ||
7717 insn->src_reg != BPF_PSEUDO_CALL)
7718 continue;
7719 insn->off = 0;
7720 insn->imm = env->insn_aux_data[i].call_imm;
7721 }
Martin KaFai Lauc454a462018-12-07 16:42:25 -08007722 bpf_prog_free_jited_linfo(prog);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007723 return err;
7724}
7725
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08007726static int fixup_call_args(struct bpf_verifier_env *env)
7727{
David S. Miller19d28fb2018-01-11 21:27:54 -05007728#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08007729 struct bpf_prog *prog = env->prog;
7730 struct bpf_insn *insn = prog->insnsi;
7731 int i, depth;
David S. Miller19d28fb2018-01-11 21:27:54 -05007732#endif
Quentin Monnete4052d02018-10-07 12:56:58 +01007733 int err = 0;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08007734
Quentin Monnete4052d02018-10-07 12:56:58 +01007735 if (env->prog->jit_requested &&
7736 !bpf_prog_is_dev_bound(env->prog->aux)) {
David S. Miller19d28fb2018-01-11 21:27:54 -05007737 err = jit_subprogs(env);
7738 if (err == 0)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007739 return 0;
Daniel Borkmannc7a89782018-07-12 21:44:28 +02007740 if (err == -EFAULT)
7741 return err;
David S. Miller19d28fb2018-01-11 21:27:54 -05007742 }
7743#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08007744 for (i = 0; i < prog->len; i++, insn++) {
7745 if (insn->code != (BPF_JMP | BPF_CALL) ||
7746 insn->src_reg != BPF_PSEUDO_CALL)
7747 continue;
7748 depth = get_callee_stack_depth(env, insn, i);
7749 if (depth < 0)
7750 return depth;
7751 bpf_patch_call_args(insn, depth);
7752 }
David S. Miller19d28fb2018-01-11 21:27:54 -05007753 err = 0;
7754#endif
7755 return err;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08007756}
7757
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007758/* fixup insn->imm field of bpf_call instructions
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07007759 * and inline eligible helpers as explicit sequence of BPF instructions
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007760 *
7761 * this function is called after eBPF program passed verification
7762 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007763static int fixup_bpf_calls(struct bpf_verifier_env *env)
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007764{
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007765 struct bpf_prog *prog = env->prog;
7766 struct bpf_insn *insn = prog->insnsi;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007767 const struct bpf_func_proto *fn;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007768 const int insn_cnt = prog->len;
Daniel Borkmann09772d92018-06-02 23:06:35 +02007769 const struct bpf_map_ops *ops;
Daniel Borkmannc93552c2018-05-24 02:32:53 +02007770 struct bpf_insn_aux_data *aux;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07007771 struct bpf_insn insn_buf[16];
7772 struct bpf_prog *new_prog;
7773 struct bpf_map *map_ptr;
7774 int i, cnt, delta = 0;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007775
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007776 for (i = 0; i < insn_cnt; i++, insn++) {
Daniel Borkmannf6b1b3b2018-01-26 23:33:39 +01007777 if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) ||
7778 insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) ||
7779 insn->code == (BPF_ALU | BPF_MOD | BPF_X) ||
Alexei Starovoitov68fda452018-01-12 18:59:52 -08007780 insn->code == (BPF_ALU | BPF_DIV | BPF_X)) {
Daniel Borkmannf6b1b3b2018-01-26 23:33:39 +01007781 bool is64 = BPF_CLASS(insn->code) == BPF_ALU64;
7782 struct bpf_insn mask_and_div[] = {
7783 BPF_MOV32_REG(insn->src_reg, insn->src_reg),
7784 /* Rx div 0 -> 0 */
7785 BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2),
7786 BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg),
7787 BPF_JMP_IMM(BPF_JA, 0, 0, 1),
7788 *insn,
7789 };
7790 struct bpf_insn mask_and_mod[] = {
7791 BPF_MOV32_REG(insn->src_reg, insn->src_reg),
7792 /* Rx mod 0 -> Rx */
7793 BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1),
7794 *insn,
7795 };
7796 struct bpf_insn *patchlet;
7797
7798 if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) ||
7799 insn->code == (BPF_ALU | BPF_DIV | BPF_X)) {
7800 patchlet = mask_and_div + (is64 ? 1 : 0);
7801 cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0);
7802 } else {
7803 patchlet = mask_and_mod + (is64 ? 1 : 0);
7804 cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0);
7805 }
7806
7807 new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt);
Alexei Starovoitov68fda452018-01-12 18:59:52 -08007808 if (!new_prog)
7809 return -ENOMEM;
7810
7811 delta += cnt - 1;
7812 env->prog = prog = new_prog;
7813 insn = new_prog->insnsi + i + delta;
7814 continue;
7815 }
7816
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +02007817 if (BPF_CLASS(insn->code) == BPF_LD &&
7818 (BPF_MODE(insn->code) == BPF_ABS ||
7819 BPF_MODE(insn->code) == BPF_IND)) {
7820 cnt = env->ops->gen_ld_abs(insn, insn_buf);
7821 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
7822 verbose(env, "bpf verifier is misconfigured\n");
7823 return -EINVAL;
7824 }
7825
7826 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
7827 if (!new_prog)
7828 return -ENOMEM;
7829
7830 delta += cnt - 1;
7831 env->prog = prog = new_prog;
7832 insn = new_prog->insnsi + i + delta;
7833 continue;
7834 }
7835
Daniel Borkmann979d63d2019-01-03 00:58:34 +01007836 if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) ||
7837 insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) {
7838 const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X;
7839 const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X;
7840 struct bpf_insn insn_buf[16];
7841 struct bpf_insn *patch = &insn_buf[0];
7842 bool issrc, isneg;
7843 u32 off_reg;
7844
7845 aux = &env->insn_aux_data[i + delta];
Daniel Borkmann3612af72019-03-01 22:05:29 +01007846 if (!aux->alu_state ||
7847 aux->alu_state == BPF_ALU_NON_POINTER)
Daniel Borkmann979d63d2019-01-03 00:58:34 +01007848 continue;
7849
7850 isneg = aux->alu_state & BPF_ALU_NEG_VALUE;
7851 issrc = (aux->alu_state & BPF_ALU_SANITIZE) ==
7852 BPF_ALU_SANITIZE_SRC;
7853
7854 off_reg = issrc ? insn->src_reg : insn->dst_reg;
7855 if (isneg)
7856 *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1);
7857 *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1);
7858 *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg);
7859 *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg);
7860 *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0);
7861 *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63);
7862 if (issrc) {
7863 *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX,
7864 off_reg);
7865 insn->src_reg = BPF_REG_AX;
7866 } else {
7867 *patch++ = BPF_ALU64_REG(BPF_AND, off_reg,
7868 BPF_REG_AX);
7869 }
7870 if (isneg)
7871 insn->code = insn->code == code_add ?
7872 code_sub : code_add;
7873 *patch++ = *insn;
7874 if (issrc && isneg)
7875 *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1);
7876 cnt = patch - insn_buf;
7877
7878 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
7879 if (!new_prog)
7880 return -ENOMEM;
7881
7882 delta += cnt - 1;
7883 env->prog = prog = new_prog;
7884 insn = new_prog->insnsi + i + delta;
7885 continue;
7886 }
7887
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007888 if (insn->code != (BPF_JMP | BPF_CALL))
7889 continue;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08007890 if (insn->src_reg == BPF_PSEUDO_CALL)
7891 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007892
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007893 if (insn->imm == BPF_FUNC_get_route_realm)
7894 prog->dst_needed = 1;
7895 if (insn->imm == BPF_FUNC_get_prandom_u32)
7896 bpf_user_rnd_init_once();
Josef Bacik9802d862017-12-11 11:36:48 -05007897 if (insn->imm == BPF_FUNC_override_return)
7898 prog->kprobe_override = 1;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007899 if (insn->imm == BPF_FUNC_tail_call) {
David S. Miller7b9f6da2017-04-20 10:35:33 -04007900 /* If we tail call into other programs, we
7901 * cannot make any assumptions since they can
7902 * be replaced dynamically during runtime in
7903 * the program array.
7904 */
7905 prog->cb_access = 1;
Alexei Starovoitov80a58d02017-05-30 13:31:30 -07007906 env->prog->aux->stack_depth = MAX_BPF_STACK;
Jiong Wange6478152018-11-08 04:08:42 -05007907 env->prog->aux->max_pkt_offset = MAX_PACKET_OFF;
David S. Miller7b9f6da2017-04-20 10:35:33 -04007908
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007909 /* mark bpf_tail_call as different opcode to avoid
7910 * conditional branch in the interpeter for every normal
7911 * call and to prevent accidental JITing by JIT compiler
7912 * that doesn't support bpf_tail_call yet
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007913 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007914 insn->imm = 0;
Alexei Starovoitov71189fa2017-05-30 13:31:27 -07007915 insn->code = BPF_JMP | BPF_TAIL_CALL;
Alexei Starovoitovb2157392018-01-07 17:33:02 -08007916
Daniel Borkmannc93552c2018-05-24 02:32:53 +02007917 aux = &env->insn_aux_data[i + delta];
7918 if (!bpf_map_ptr_unpriv(aux))
7919 continue;
7920
Alexei Starovoitovb2157392018-01-07 17:33:02 -08007921 /* instead of changing every JIT dealing with tail_call
7922 * emit two extra insns:
7923 * if (index >= max_entries) goto out;
7924 * index &= array->index_mask;
7925 * to avoid out-of-bounds cpu speculation
7926 */
Daniel Borkmannc93552c2018-05-24 02:32:53 +02007927 if (bpf_map_ptr_poisoned(aux)) {
Colin Ian King40950342018-01-10 09:20:54 +00007928 verbose(env, "tail_call abusing map_ptr\n");
Alexei Starovoitovb2157392018-01-07 17:33:02 -08007929 return -EINVAL;
7930 }
Daniel Borkmannc93552c2018-05-24 02:32:53 +02007931
7932 map_ptr = BPF_MAP_PTR(aux->map_state);
Alexei Starovoitovb2157392018-01-07 17:33:02 -08007933 insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3,
7934 map_ptr->max_entries, 2);
7935 insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3,
7936 container_of(map_ptr,
7937 struct bpf_array,
7938 map)->index_mask);
7939 insn_buf[2] = *insn;
7940 cnt = 3;
7941 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
7942 if (!new_prog)
7943 return -ENOMEM;
7944
7945 delta += cnt - 1;
7946 env->prog = prog = new_prog;
7947 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007948 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007949 }
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007950
Daniel Borkmann89c63072017-08-19 03:12:45 +02007951 /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup
Daniel Borkmann09772d92018-06-02 23:06:35 +02007952 * and other inlining handlers are currently limited to 64 bit
7953 * only.
Daniel Borkmann89c63072017-08-19 03:12:45 +02007954 */
Alexei Starovoitov60b58afc2017-12-14 17:55:14 -08007955 if (prog->jit_requested && BITS_PER_LONG == 64 &&
Daniel Borkmann09772d92018-06-02 23:06:35 +02007956 (insn->imm == BPF_FUNC_map_lookup_elem ||
7957 insn->imm == BPF_FUNC_map_update_elem ||
Daniel Borkmann84430d42018-10-21 02:09:27 +02007958 insn->imm == BPF_FUNC_map_delete_elem ||
7959 insn->imm == BPF_FUNC_map_push_elem ||
7960 insn->imm == BPF_FUNC_map_pop_elem ||
7961 insn->imm == BPF_FUNC_map_peek_elem)) {
Daniel Borkmannc93552c2018-05-24 02:32:53 +02007962 aux = &env->insn_aux_data[i + delta];
7963 if (bpf_map_ptr_poisoned(aux))
7964 goto patch_call_imm;
7965
7966 map_ptr = BPF_MAP_PTR(aux->map_state);
Daniel Borkmann09772d92018-06-02 23:06:35 +02007967 ops = map_ptr->ops;
7968 if (insn->imm == BPF_FUNC_map_lookup_elem &&
7969 ops->map_gen_lookup) {
7970 cnt = ops->map_gen_lookup(map_ptr, insn_buf);
7971 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
7972 verbose(env, "bpf verifier is misconfigured\n");
7973 return -EINVAL;
7974 }
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07007975
Daniel Borkmann09772d92018-06-02 23:06:35 +02007976 new_prog = bpf_patch_insn_data(env, i + delta,
7977 insn_buf, cnt);
7978 if (!new_prog)
7979 return -ENOMEM;
7980
7981 delta += cnt - 1;
7982 env->prog = prog = new_prog;
7983 insn = new_prog->insnsi + i + delta;
7984 continue;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07007985 }
7986
Daniel Borkmann09772d92018-06-02 23:06:35 +02007987 BUILD_BUG_ON(!__same_type(ops->map_lookup_elem,
7988 (void *(*)(struct bpf_map *map, void *key))NULL));
7989 BUILD_BUG_ON(!__same_type(ops->map_delete_elem,
7990 (int (*)(struct bpf_map *map, void *key))NULL));
7991 BUILD_BUG_ON(!__same_type(ops->map_update_elem,
7992 (int (*)(struct bpf_map *map, void *key, void *value,
7993 u64 flags))NULL));
Daniel Borkmann84430d42018-10-21 02:09:27 +02007994 BUILD_BUG_ON(!__same_type(ops->map_push_elem,
7995 (int (*)(struct bpf_map *map, void *value,
7996 u64 flags))NULL));
7997 BUILD_BUG_ON(!__same_type(ops->map_pop_elem,
7998 (int (*)(struct bpf_map *map, void *value))NULL));
7999 BUILD_BUG_ON(!__same_type(ops->map_peek_elem,
8000 (int (*)(struct bpf_map *map, void *value))NULL));
8001
Daniel Borkmann09772d92018-06-02 23:06:35 +02008002 switch (insn->imm) {
8003 case BPF_FUNC_map_lookup_elem:
8004 insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) -
8005 __bpf_call_base;
8006 continue;
8007 case BPF_FUNC_map_update_elem:
8008 insn->imm = BPF_CAST_CALL(ops->map_update_elem) -
8009 __bpf_call_base;
8010 continue;
8011 case BPF_FUNC_map_delete_elem:
8012 insn->imm = BPF_CAST_CALL(ops->map_delete_elem) -
8013 __bpf_call_base;
8014 continue;
Daniel Borkmann84430d42018-10-21 02:09:27 +02008015 case BPF_FUNC_map_push_elem:
8016 insn->imm = BPF_CAST_CALL(ops->map_push_elem) -
8017 __bpf_call_base;
8018 continue;
8019 case BPF_FUNC_map_pop_elem:
8020 insn->imm = BPF_CAST_CALL(ops->map_pop_elem) -
8021 __bpf_call_base;
8022 continue;
8023 case BPF_FUNC_map_peek_elem:
8024 insn->imm = BPF_CAST_CALL(ops->map_peek_elem) -
8025 __bpf_call_base;
8026 continue;
Daniel Borkmann09772d92018-06-02 23:06:35 +02008027 }
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07008028
Daniel Borkmann09772d92018-06-02 23:06:35 +02008029 goto patch_call_imm;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07008030 }
8031
8032patch_call_imm:
Andrey Ignatov5e43f892018-03-30 15:08:00 -07008033 fn = env->ops->get_func_proto(insn->imm, env->prog);
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008034 /* all functions that have prototype and verifier allowed
8035 * programs to call them, must be real in-kernel functions
8036 */
8037 if (!fn->func) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07008038 verbose(env,
8039 "kernel subsystem misconfigured func %s#%d\n",
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008040 func_id_name(insn->imm), insn->imm);
8041 return -EFAULT;
8042 }
8043 insn->imm = fn->func - __bpf_call_base;
8044 }
8045
8046 return 0;
8047}
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008048
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01008049static void free_states(struct bpf_verifier_env *env)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07008050{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01008051 struct bpf_verifier_state_list *sl, *sln;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07008052 int i;
8053
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -07008054 sl = env->free_list;
8055 while (sl) {
8056 sln = sl->next;
8057 free_verifier_state(&sl->state, false);
8058 kfree(sl);
8059 sl = sln;
8060 }
8061
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07008062 if (!env->explored_states)
8063 return;
8064
8065 for (i = 0; i < env->prog->len; i++) {
8066 sl = env->explored_states[i];
8067
8068 if (sl)
8069 while (sl != STATE_LIST_MARK) {
8070 sln = sl->next;
Alexei Starovoitov1969db42017-11-01 00:08:04 -07008071 free_verifier_state(&sl->state, false);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07008072 kfree(sl);
8073 sl = sln;
8074 }
8075 }
8076
Alexei Starovoitov71dde682019-04-01 21:27:43 -07008077 kvfree(env->explored_states);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07008078}
8079
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07008080static void print_verification_stats(struct bpf_verifier_env *env)
8081{
8082 int i;
8083
8084 if (env->log.level & BPF_LOG_STATS) {
8085 verbose(env, "verification time %lld usec\n",
8086 div_u64(env->verification_time, 1000));
8087 verbose(env, "stack depth ");
8088 for (i = 0; i < env->subprog_cnt; i++) {
8089 u32 depth = env->subprog_info[i].stack_depth;
8090
8091 verbose(env, "%d", depth);
8092 if (i + 1 < env->subprog_cnt)
8093 verbose(env, "+");
8094 }
8095 verbose(env, "\n");
8096 }
8097 verbose(env, "processed %d insns (limit %d) max_states_per_insn %d "
8098 "total_states %d peak_states %d mark_read %d\n",
8099 env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS,
8100 env->max_states_per_insn, env->total_states,
8101 env->peak_states, env->longest_mark_read_walk);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008102}
8103
Yonghong Song838e9692018-11-19 15:29:11 -08008104int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,
8105 union bpf_attr __user *uattr)
Alexei Starovoitov51580e72014-09-26 00:17:02 -07008106{
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07008107 u64 start_time = ktime_get_ns();
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01008108 struct bpf_verifier_env *env;
Martin KaFai Laub9193c12018-03-24 11:44:22 -07008109 struct bpf_verifier_log *log;
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08008110 int i, len, ret = -EINVAL;
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08008111 bool is_priv;
Alexei Starovoitov51580e72014-09-26 00:17:02 -07008112
Arnd Bergmanneba0c922017-11-02 12:05:52 +01008113 /* no program is valid */
8114 if (ARRAY_SIZE(bpf_verifier_ops) == 0)
8115 return -EINVAL;
8116
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01008117 /* 'struct bpf_verifier_env' can be global, but since it's not small,
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008118 * allocate/free it every time bpf_check() is called
8119 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01008120 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008121 if (!env)
8122 return -ENOMEM;
Jakub Kicinski61bd5212017-10-09 10:30:11 -07008123 log = &env->log;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008124
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08008125 len = (*prog)->len;
Kees Cookfad953c2018-06-12 14:27:37 -07008126 env->insn_aux_data =
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08008127 vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len));
Jakub Kicinski3df126f2016-09-21 11:43:56 +01008128 ret = -ENOMEM;
8129 if (!env->insn_aux_data)
8130 goto err_free_env;
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08008131 for (i = 0; i < len; i++)
8132 env->insn_aux_data[i].orig_idx = i;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008133 env->prog = *prog;
Jakub Kicinski00176a32017-10-16 16:40:54 -07008134 env->ops = bpf_verifier_ops[env->prog->type];
Alexei Starovoitov45a73c12019-04-19 07:44:55 -07008135 is_priv = capable(CAP_SYS_ADMIN);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008136
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008137 /* grab the mutex to protect few globals used by verifier */
Alexei Starovoitov45a73c12019-04-19 07:44:55 -07008138 if (!is_priv)
8139 mutex_lock(&bpf_verifier_lock);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008140
8141 if (attr->log_level || attr->log_buf || attr->log_size) {
8142 /* user requested verbose verifier output
8143 * and supplied buffer to store the verification trace
8144 */
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07008145 log->level = attr->log_level;
8146 log->ubuf = (char __user *) (unsigned long) attr->log_buf;
8147 log->len_total = attr->log_size;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008148
8149 ret = -EINVAL;
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07008150 /* log attributes have to be sane */
Alexei Starovoitov7a9f5c62019-04-01 21:27:46 -07008151 if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 ||
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07008152 !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01008153 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008154 }
Daniel Borkmann1ad2f582017-05-25 01:05:05 +02008155
8156 env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT);
8157 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
David S. Millere07b98d2017-05-10 11:38:07 -07008158 env->strict_alignment = true;
David Millere9ee9ef2018-11-30 21:08:14 -08008159 if (attr->prog_flags & BPF_F_ANY_ALIGNMENT)
8160 env->strict_alignment = false;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008161
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08008162 env->allow_ptr_leaks = is_priv;
8163
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008164 ret = replace_map_fd_with_map_ptr(env);
8165 if (ret < 0)
8166 goto skip_full_check;
8167
Jakub Kicinskif4e3ec02018-05-03 18:37:11 -07008168 if (bpf_prog_is_dev_bound(env->prog->aux)) {
Quentin Monneta40a2632018-11-09 13:03:31 +00008169 ret = bpf_prog_offload_verifier_prep(env->prog);
Jakub Kicinskif4e3ec02018-05-03 18:37:11 -07008170 if (ret)
8171 goto skip_full_check;
8172 }
8173
Alexei Starovoitov71dde682019-04-01 21:27:43 -07008174 env->explored_states = kvcalloc(env->prog->len,
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01008175 sizeof(struct bpf_verifier_state_list *),
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07008176 GFP_USER);
8177 ret = -ENOMEM;
8178 if (!env->explored_states)
8179 goto skip_full_check;
8180
Martin KaFai Laud9762e82018-12-13 10:41:48 -08008181 ret = check_subprogs(env);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07008182 if (ret < 0)
8183 goto skip_full_check;
8184
Martin KaFai Lauc454a462018-12-07 16:42:25 -08008185 ret = check_btf_info(env, attr, uattr);
Yonghong Song838e9692018-11-19 15:29:11 -08008186 if (ret < 0)
8187 goto skip_full_check;
8188
Martin KaFai Laud9762e82018-12-13 10:41:48 -08008189 ret = check_cfg(env);
8190 if (ret < 0)
8191 goto skip_full_check;
8192
Alexei Starovoitov17a52672014-09-26 00:17:06 -07008193 ret = do_check(env);
Craig Gallek8c01c4f2017-11-02 11:18:01 -04008194 if (env->cur_state) {
8195 free_verifier_state(env->cur_state, true);
8196 env->cur_state = NULL;
8197 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008198
Quentin Monnetc941ce92018-10-07 12:56:47 +01008199 if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux))
8200 ret = bpf_prog_offload_finalize(env);
8201
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008202skip_full_check:
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07008203 while (!pop_stack(env, NULL, NULL));
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07008204 free_states(env);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008205
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008206 if (ret == 0)
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08008207 ret = check_max_stack_depth(env);
8208
Jakub Kicinski9b38c402018-12-19 22:13:06 -08008209 /* instruction rewrites happen after this point */
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08008210 if (is_priv) {
8211 if (ret == 0)
8212 opt_hard_wire_dead_code_branches(env);
Jakub Kicinski52875a02019-01-22 22:45:20 -08008213 if (ret == 0)
8214 ret = opt_remove_dead_code(env);
Jakub Kicinskia1b14ab2019-01-22 22:45:21 -08008215 if (ret == 0)
8216 ret = opt_remove_nops(env);
Jakub Kicinski52875a02019-01-22 22:45:20 -08008217 } else {
8218 if (ret == 0)
8219 sanitize_dead_code(env);
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08008220 }
8221
Jakub Kicinski9b38c402018-12-19 22:13:06 -08008222 if (ret == 0)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008223 /* program is valid, convert *(u32*)(ctx + off) accesses */
8224 ret = convert_ctx_accesses(env);
8225
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008226 if (ret == 0)
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008227 ret = fixup_bpf_calls(env);
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008228
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08008229 if (ret == 0)
8230 ret = fixup_call_args(env);
8231
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07008232 env->verification_time = ktime_get_ns() - start_time;
8233 print_verification_stats(env);
8234
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07008235 if (log->level && bpf_verifier_log_full(log))
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008236 ret = -ENOSPC;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07008237 if (log->level && !log->ubuf) {
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008238 ret = -EFAULT;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07008239 goto err_release_maps;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008240 }
8241
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008242 if (ret == 0 && env->used_map_cnt) {
8243 /* if program passed verifier, update used_maps in bpf_prog_info */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008244 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
8245 sizeof(env->used_maps[0]),
8246 GFP_KERNEL);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008247
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008248 if (!env->prog->aux->used_maps) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008249 ret = -ENOMEM;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07008250 goto err_release_maps;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008251 }
8252
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008253 memcpy(env->prog->aux->used_maps, env->used_maps,
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008254 sizeof(env->used_maps[0]) * env->used_map_cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008255 env->prog->aux->used_map_cnt = env->used_map_cnt;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008256
8257 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
8258 * bpf_ld_imm64 instructions
8259 */
8260 convert_pseudo_ld_imm64(env);
8261 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008262
Yonghong Songba64e7d2018-11-24 23:20:44 -08008263 if (ret == 0)
8264 adjust_btf_func(env);
8265
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07008266err_release_maps:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008267 if (!env->prog->aux->used_maps)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008268 /* if we didn't copy map pointers into bpf_prog_info, release
Jakub Kicinskiab7f5bf2018-05-03 18:37:17 -07008269 * them now. Otherwise free_used_maps() will release them.
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008270 */
8271 release_maps(env);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008272 *prog = env->prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01008273err_unlock:
Alexei Starovoitov45a73c12019-04-19 07:44:55 -07008274 if (!is_priv)
8275 mutex_unlock(&bpf_verifier_lock);
Jakub Kicinski3df126f2016-09-21 11:43:56 +01008276 vfree(env->insn_aux_data);
8277err_free_env:
8278 kfree(env);
Alexei Starovoitov51580e72014-09-26 00:17:02 -07008279 return ret;
8280}